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.
177 lines
4.6 KiB
Go
177 lines
4.6 KiB
Go
//go:build darwin
|
||
|
||
package metal
|
||
|
||
import (
|
||
"errors"
|
||
"fmt"
|
||
"unsafe"
|
||
|
||
"github.com/egoist/mygo/internal/scene"
|
||
)
|
||
|
||
// Frames draws frames one after another without a layer, for videos
|
||
// (plugins/render): into a texture of its own, whose pixels Read copies,
|
||
// or into IOSurfaces of the caller's, as the CVPixelBuffers an encoder
|
||
// takes, without a copy. A frame is committed without waiting for the
|
||
// GPU, which draws it while the caller builds the next one; Wait waits
|
||
// for it.
|
||
//
|
||
// Frames is not safe for concurrent use.
|
||
type Frames struct {
|
||
r *Renderer
|
||
// tex is the texture Draw draws into, of w×h pixels.
|
||
tex id
|
||
w, h int
|
||
// last is the command buffer of the last frame, retained until Wait
|
||
// sees it done, and surface the texture wrapping the IOSurface it drew
|
||
// into, released then too.
|
||
last, surface id
|
||
}
|
||
|
||
// NewFrames returns a Frames on the system's default GPU.
|
||
func NewFrames() (*Frames, error) {
|
||
r, err := newRenderer()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
f := &Frames{r: r}
|
||
pool(func() { err = r.initGPU() })
|
||
if err != nil {
|
||
r.Release()
|
||
return nil, err
|
||
}
|
||
return f, nil
|
||
}
|
||
|
||
// Name returns the name of the GPU, as "Apple M2".
|
||
func (f *Frames) Name() (name string) {
|
||
pool(func() { name = goString(send(f.r.device, "name")) })
|
||
return name
|
||
}
|
||
|
||
// Draw draws s into the Frames' own texture, after waiting for the frame
|
||
// before, whose pixels the caller read. Read copies its pixels once Wait
|
||
// returned.
|
||
func (f *Frames) Draw(s *scene.Scene) (err error) {
|
||
pool(func() {
|
||
if err = f.wait(); err != nil {
|
||
return
|
||
}
|
||
if f.tex == 0 || f.w != s.Width || f.h != s.Height {
|
||
release(&f.tex)
|
||
f.r.cur = &f.r.formats[0]
|
||
if f.tex = f.r.newTexture(s.Width, s.Height, pixelFormatBGRA8Unorm, usageRenderTarget|usageShaderRead, nil, 0); f.tex == 0 {
|
||
err = errors.New("metal: cannot create a target")
|
||
return
|
||
}
|
||
f.w, f.h = s.Width, s.Height
|
||
}
|
||
err = f.commit(s, f.tex, true)
|
||
})
|
||
return err
|
||
}
|
||
|
||
// DrawSurface draws s into surface, an IOSurfaceRef of s's size in BGRA,
|
||
// as a CVPixelBuffer's from a pool, after waiting for the frame before.
|
||
// The surface holds the frame once Wait returned.
|
||
func (f *Frames) DrawSurface(s *scene.Scene, surface uintptr) (err error) {
|
||
pool(func() {
|
||
if err = f.wait(); err != nil {
|
||
return
|
||
}
|
||
desc := send(class("MTLTextureDescriptor"), "texture2DDescriptorWithPixelFormat:width:height:mipmapped:",
|
||
pixelFormatBGRA8Unorm, uintptr(s.Width), uintptr(s.Height), 0)
|
||
send(desc, "setUsage:", usageRenderTarget|usageShaderRead)
|
||
tex := send(f.r.device, "newTextureWithDescriptor:iosurface:plane:", desc, surface, 0)
|
||
if tex == 0 {
|
||
err = errors.New("metal: cannot draw into the IOSurface")
|
||
return
|
||
}
|
||
f.surface = tex
|
||
f.r.cur = &f.r.formats[0]
|
||
if err = f.commit(s, tex, false); err != nil {
|
||
release(&f.surface)
|
||
}
|
||
})
|
||
return err
|
||
}
|
||
|
||
// commit encodes s into target and commits it, keeping its command
|
||
// buffer for Wait; read synchronizes a managed texture for the CPU.
|
||
func (f *Frames) commit(s *scene.Scene, target id, read bool) error {
|
||
cb, err := f.r.encode(s, target)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if read {
|
||
blit := send(cb, "blitCommandEncoder")
|
||
send(blit, "synchronizeResource:", target)
|
||
send(blit, "endEncoding")
|
||
}
|
||
send(cb, "commit")
|
||
f.r.track(cb)
|
||
f.last = send(cb, "retain")
|
||
f.r.sweepIfDue()
|
||
return nil
|
||
}
|
||
|
||
// Wait waits for the GPU to finish the last frame drawn.
|
||
func (f *Frames) Wait() (err error) {
|
||
pool(func() { err = f.wait() })
|
||
return err
|
||
}
|
||
|
||
func (f *Frames) wait() error {
|
||
if f.last == 0 {
|
||
return nil
|
||
}
|
||
send(f.last, "waitUntilCompleted")
|
||
var err error
|
||
if int(send(f.last, "status")) == statusError {
|
||
err = fmt.Errorf("metal: the frame failed: %s", describe(send(f.last, "error")))
|
||
}
|
||
release(&f.last)
|
||
release(&f.surface)
|
||
return err
|
||
}
|
||
|
||
// Read copies the pixels of the frame Draw drew last into dst, once Wait
|
||
// returned: premultiplied BGRA rows from the top, 4×width bytes each.
|
||
func (f *Frames) Read(dst []byte) error {
|
||
if f.tex == 0 || len(dst) < f.w*f.h*4 {
|
||
return errors.New("metal: no frame to read, or too small a buffer")
|
||
}
|
||
pool(func() {
|
||
msgGetBytes(f.tex, sel("getBytes:bytesPerRow:fromRegion:mipmapLevel:"), unsafe.Pointer(&dst[0]), uint(f.w*4),
|
||
mtlRegion{W: uint(f.w), H: uint(f.h), D: 1}, 0)
|
||
})
|
||
return nil
|
||
}
|
||
|
||
// Release waits for the GPU, and frees the texture and the renderer.
|
||
func (f *Frames) Release() {
|
||
pool(func() {
|
||
f.wait()
|
||
release(&f.surface)
|
||
release(&f.tex)
|
||
})
|
||
f.r.Release()
|
||
}
|
||
|
||
// goString returns the text of an NSString.
|
||
func goString(str id) string {
|
||
if str == 0 {
|
||
return ""
|
||
}
|
||
p := send(str, "UTF8String")
|
||
if p == 0 {
|
||
return ""
|
||
}
|
||
n := 0
|
||
for *(*byte)(ptr(p + uintptr(n))) != 0 {
|
||
n++
|
||
}
|
||
return string(unsafe.Slice((*byte)(ptr(p)), n))
|
||
}
|