Files
openchamber-android-native/third_party/mygo/internal/raster/damage.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

100 lines
2.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package raster
import (
"image"
"runtime"
"github.com/egoist/mygo/internal/scene"
)
// Renderer draws the successive scenes of a window into Image, redrawing
// only where a scene differs from the one before (scene.Differ): a
// blinking caret, a ticking clock or a button under the pointer redraws
// only itself.
type Renderer struct {
// Image holds the last scene drawn, in mem.
Image Image
mem *pixels
// d draws the bands of large damage.
d drawer
diff scene.Differ
damage []image.Rectangle
// canvas is allocated only when a canvas needs drawing in memory,
// reused between its redraws, and dropped with the frame by Release.
canvas *canvasScratch
}
// Render draws s, and returns the rectangles of Image it changed. s may
// change once Render returns.
func (r *Renderer) Render(s *scene.Scene) []image.Rectangle {
r.drawCanvases(s)
if r.whole(s) || r.Image.W != s.Width || r.Image.H != s.Height {
// A new size, or Image was released (Release).
r.resize(s.Width, s.Height)
r.damage = append(r.damage[:0], image.Rect(0, 0, s.Width, s.Height))
}
bounds := r.diff.Bounds()
for _, d := range r.damage {
r.d.draw(&r.Image, s, d, bounds)
}
r.diff.Remember(s)
return r.damage
}
// pixels is the memory of a Renderer's image (allocPixels).
type pixels struct {
b []byte
mapped bool
cleanup runtime.Cleanup
}
// resize makes Image w×h, reusing its memory when it can.
func (r *Renderer) resize(w, h int) {
n := 4 * w * h
if r.mem == nil || len(r.mem.b) < n {
r.mem.free()
r.mem = allocPixels(n)
}
r.Image = Image{W: w, H: h, Stride: 4 * w, Pix: r.mem.b[:n]}
}
// Release frees the image and what Render remembers: the next Render
// draws everything. Image must not be used meanwhile.
func (r *Renderer) Release() {
r.mem.free()
*r = Renderer{}
}
func area(rects []image.Rectangle) int {
n := 0
for _, d := range rects {
n += d.Dx() * d.Dy()
}
return n
}
// whole sets the damage of s against the last scene, and reports whether
// s must be drawn whole: there is none, or s changes too much of it.
func (r *Renderer) whole(s *scene.Scene) bool {
r.diff.Reach = 1 // a pixel's antialiased edge
damage, ok := r.diff.Diff(s)
if !ok {
return true
}
r.damage = r.diff.AddBackdrops(s, append(r.damage[:0], damage...))
// Past half the window, drawing it whole costs about the same.
return area(r.damage)*2 >= s.Width*s.Height
}
// Invalidate makes the next Render draw everything.
func (r *Renderer) Invalidate() { r.diff.Invalidate() }
// opBounds returns where each operation of s draws, within the clips
// around it, for a renderer without a last scene.
func (r *Renderer) opBounds(s *scene.Scene) []image.Rectangle {
r.diff.Reach = 1
r.diff.Diff(s)
return r.diff.Bounds()
}