Compare commits

..

1 Commits

Author SHA1 Message Date
Joe Julian 9bfa6bc1c2 app: relock contexts after refresh
This fixes an Android regression introduced by

  3e601e73c4
  app: optimize window context locking

On Android, that change can leave the app window visible but unrendered:
the screen stays black instead of drawing the UI.

The regression is in app/window.go: Refresh can update the underlying
surface state without guaranteeing a subsequent Lock before rendering.

That is a problem for Android, where Refresh and Lock have distinct
roles:

- androidContext.Refresh stages the native window for the EGL surface
- androidContext.Lock performs CreateSurface, when needed, and MakeCurrent

In other words, after Refresh, Android may require another Lock before
GPU work can proceed correctly.

This patch keeps explicit locking, but avoids unconditional per-frame
Lock/Unlock calls. It adds a small amount of state to Window:

- ctxNeedsLock is set when creating a context
- ctxNeedsLock is set after Refresh
- ctxNeedsLock is set after explicit unlock/release paths
- Lock is called before GPU work only when ctxNeedsLock is true

That keeps the optimized steady-state path while restoring the required
Refresh -> Lock sequencing.

I also added a regression test for validateAndProcess that checks:

- Refresh forces a re-lock before drawing/present
- steady-state frames do not re-lock redundantly

Verification:
- go test ./app

Signed-off-by: Joe Julian <me@joejulian.name>
2026-04-16 15:00:49 -07:00
5 changed files with 122 additions and 23 deletions
+3
View File
@@ -115,6 +115,9 @@ func (c *context) Unlock() {
}
func (c *context) Refresh() error {
if C.gio_makeCurrent(c.ctx) == 0 {
return errors.New("[EAGLContext setCurrentContext] failed")
}
if !c.init {
c.init = true
c.frameBuffer = c.c.CreateFramebuffer()
+2
View File
@@ -111,6 +111,8 @@ func (c *glContext) Unlock() {
}
func (c *glContext) Refresh() error {
c.Lock()
defer c.Unlock()
C.gio_updateContext(c.ctx)
return nil
}
-2
View File
@@ -165,8 +165,6 @@ type frameEvent struct {
Sync bool
}
// The caller must hold the context lock while using API, Refresh,
// RenderTarget, or Present.
type context interface {
API() gpu.API
RenderTarget() (gpu.RenderTarget, error)
+10 -21
View File
@@ -154,10 +154,6 @@ func (w *Window) validateAndProcess(size image.Point, sync bool, frame *op.Ops,
sync = true
}
}
if err := w.lockContext(); err != nil {
w.destroyGPU()
return err
}
if sync && w.ctx != nil {
if err := w.ctx.Refresh(); err != nil {
if errors.Is(err, errOutOfDate) {
@@ -171,11 +167,14 @@ func (w *Window) validateAndProcess(size image.Point, sync bool, frame *op.Ops,
}
return err
}
w.unlockContext()
if err := w.lockContext(); err != nil {
w.ctxNeedsLock = true
}
if w.ctx != nil && w.ctxNeedsLock {
if err := w.ctx.Lock(); err != nil {
w.destroyGPU()
return err
}
w.ctxNeedsLock = false
}
if w.gpu == nil && !w.nocontext {
gpu, err := gpu.New(w.ctx.API())
@@ -510,17 +509,6 @@ func (c *callbacks) ActionAt(p f32.Point) (system.Action, bool) {
return c.w.queue.ActionAt(p)
}
func (w *Window) lockContext() error {
if w.ctx == nil || !w.ctxNeedsLock {
return nil
}
if err := w.ctx.Lock(); err != nil {
return err
}
w.ctxNeedsLock = false
return nil
}
func (w *Window) unlockContext() {
if w.ctx == nil || w.ctxNeedsLock {
return
@@ -531,9 +519,9 @@ func (w *Window) unlockContext() {
func (w *Window) destroyGPU() {
if w.gpu != nil {
if err := w.lockContext(); err == nil {
if err := w.ctx.Lock(); err == nil {
w.gpu.Release()
w.unlockContext()
w.ctx.Unlock()
}
w.gpu = nil
}
@@ -683,11 +671,12 @@ func (w *Window) processEvent(e event.Event) bool {
w.coalesced.destroy = &e2
case ViewEvent:
if !e2.Valid() && w.gpu != nil {
if err := w.lockContext(); err == nil {
if err := w.ctx.Lock(); err == nil {
w.gpu.Release()
w.unlockContext()
w.ctx.Unlock()
}
w.gpu = nil
w.ctxNeedsLock = true
}
w.coalesced.view = &e2
case ConfigEvent:
+107
View File
@@ -0,0 +1,107 @@
// SPDX-License-Identifier: Unlicense OR MIT
package app
import (
"image"
"image/color"
"testing"
"gioui.org/gpu"
"gioui.org/op"
)
func TestValidateAndProcessRelocksAfterRefresh(t *testing.T) {
ctx := &testContext{}
w := &Window{
ctx: ctx,
gpu: &testGPU{},
ctxNeedsLock: false,
}
if err := w.validateAndProcess(image.Pt(320, 240), true, new(op.Ops), nil); err != nil {
t.Fatalf("validateAndProcess returned error: %v", err)
}
want := []string{"refresh", "lock", "render-target", "present"}
if got := ctx.ops; !equalStringSlices(got, want) {
t.Fatalf("unexpected call order:\n got %v\n want %v", got, want)
}
if w.ctxNeedsLock {
t.Fatalf("context should remain current after a successful frame")
}
}
func TestValidateAndProcessSkipsRedundantRelock(t *testing.T) {
ctx := &testContext{}
w := &Window{
ctx: ctx,
gpu: &testGPU{},
ctxNeedsLock: false,
}
if err := w.validateAndProcess(image.Pt(320, 240), false, new(op.Ops), nil); err != nil {
t.Fatalf("validateAndProcess returned error: %v", err)
}
want := []string{"render-target", "present"}
if got := ctx.ops; !equalStringSlices(got, want) {
t.Fatalf("unexpected call order:\n got %v\n want %v", got, want)
}
}
type testContext struct {
ops []string
}
func (c *testContext) API() gpu.API {
return nil
}
func (c *testContext) RenderTarget() (gpu.RenderTarget, error) {
c.ops = append(c.ops, "render-target")
return gpu.OpenGLRenderTarget{}, nil
}
func (c *testContext) Present() error {
c.ops = append(c.ops, "present")
return nil
}
func (c *testContext) Refresh() error {
c.ops = append(c.ops, "refresh")
return nil
}
func (c *testContext) Release() {}
func (c *testContext) Lock() error {
c.ops = append(c.ops, "lock")
return nil
}
func (c *testContext) Unlock() {
c.ops = append(c.ops, "unlock")
}
type testGPU struct{}
func (g *testGPU) Release() {}
func (g *testGPU) Clear(color.NRGBA) {}
func (g *testGPU) Frame(*op.Ops, gpu.RenderTarget, image.Point) error {
return nil
}
func equalStringSlices(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}