Files
openchamber-android-native/third_party/mygo/internal/gpu/metal/offscreen.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

111 lines
2.9 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.
//go:build darwin
package metal
import (
"errors"
"fmt"
"time"
"github.com/ebitengine/purego"
"github.com/egoist/mygo/internal/scene"
)
// RenderOffscreen draws s with a renderer of its own into a texture,
// without a layer, and returns its premultiplied BGRA rows: for tests, as
// packages drawing effects have.
func RenderOffscreen(s *scene.Scene) ([]byte, error) {
r, err := newRenderer()
if err != nil {
return nil, err
}
defer r.Release()
return r.renderOffscreen(s)
}
// Offscreen draws frames with a renderer of its own into a texture,
// without a layer, waiting for each and timing it on the GPU: for
// benchmarks, as those of package ui drawing its frames.
type Offscreen struct {
r *Renderer
tex id
w, h int
// encoded is how long encoding the last frame took on the CPU.
encoded time.Duration
}
var msgDouble func(obj id, sel uintptr) float64
// NewOffscreen returns an Offscreen drawing frames of w×h pixels.
func NewOffscreen(w, h int) (*Offscreen, error) {
r, err := newRenderer()
if err != nil {
return nil, err
}
if msgDouble == nil {
purego.RegisterFunc(&msgDouble, msgSend)
}
o := &Offscreen{r: r}
pool(func() { err = o.target(w, h) })
if err != nil {
o.Release()
return nil, err
}
return o, nil
}
// target makes the texture drawn into w×h pixels.
func (o *Offscreen) target(w, h int) error {
if o.tex != 0 && o.w == w && o.h == h {
return nil
}
release(&o.tex)
o.r.cur = &o.r.formats[0]
o.tex = o.r.newTexture(w, h, pixelFormatBGRA8Unorm, usageRenderTarget|usageShaderRead, nil, 0)
if o.tex == 0 {
return errors.New("metal: cannot create a target")
}
o.w, o.h = w, h
return nil
}
// Render draws s, a texture of its size taking the place of one of
// another, waits for the GPU to finish it, and returns the time the GPU
// spent on it, by its command buffer's timestamps.
func (o *Offscreen) Render(s *scene.Scene) (gpu time.Duration, err error) {
pool(func() {
r := o.r
r.waitLast()
if err = o.target(s.Width, s.Height); err != nil {
return
}
var cb id
start := time.Now()
if cb, err = r.encode(s, o.tex); err != nil {
return
}
o.encoded = time.Since(start)
send(cb, "commit")
send(cb, "waitUntilCompleted")
if int(send(cb, "status")) == statusError {
err = fmt.Errorf("metal: the frame failed: %s", describe(send(cb, "error")))
return
}
secs := msgDouble(cb, uintptr(sel("GPUEndTime"))) - msgDouble(cb, uintptr(sel("GPUStartTime")))
gpu = time.Duration(secs * float64(time.Second))
})
return gpu, err
}
// Encoded returns how long the CPU took to encode the last frame Render
// drew: building its instances (gpu.Builder), uploading what changed in
// its atlases, and encoding its passes, before committing them.
func (o *Offscreen) Encoded() time.Duration { return o.encoded }
// Release frees the texture and the renderer.
func (o *Offscreen) Release() {
pool(func() { release(&o.tex) })
o.r.Release()
}