app,app/internal/wm: fold context MakeCurrent/ReleaseCurrent into Lock/Unlock

While here, make context Refresh useful and remove the redundant
MakeCurrent from the window loop.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2021-08-04 12:03:04 +02:00
parent 4dd6a50670
commit 0bdc2e0432
11 changed files with 83 additions and 132 deletions
+5 -13
View File
@@ -72,16 +72,9 @@ func (c *d3d11Context) Present() error {
} }
func (c *d3d11Context) Refresh() error { func (c *d3d11Context) Refresh() error {
return nil
}
func (c *d3d11Context) MakeCurrent() error {
var width, height int var width, height int
c.win.w.Run(func() { _, width, height = c.win.HWND()
_, width, height = c.win.HWND()
})
if c.renderTarget != nil && width == c.width && height == c.height { if c.renderTarget != nil && width == c.width && height == c.height {
c.ctx.OMSetRenderTargets(c.renderTarget, c.depthView)
return nil return nil
} }
c.releaseFBO() c.releaseFBO()
@@ -112,14 +105,13 @@ func (c *d3d11Context) MakeCurrent() error {
} }
c.renderTarget = renderTarget c.renderTarget = renderTarget
c.depthView = depthView c.depthView = depthView
c.ctx.OMSetRenderTargets(c.renderTarget, c.depthView)
return nil return nil
} }
func (c *d3d11Context) ReleaseCurrent() {} func (c *d3d11Context) Lock() error {
c.ctx.OMSetRenderTargets(c.renderTarget, c.depthView)
func (c *d3d11Context) Lock() {} return nil
}
func (c *d3d11Context) Unlock() {} func (c *d3d11Context) Unlock() {}
+8 -18
View File
@@ -35,33 +35,23 @@ func (c *context) Release() {
} }
func (c *context) Refresh() error { func (c *context) Refresh() error {
return nil
}
func (c *context) MakeCurrent() error {
c.Context.ReleaseSurface() c.Context.ReleaseSurface()
var ( var (
win *C.ANativeWindow win *C.ANativeWindow
width, height int width, height int
) )
// Run on main thread. Deadlock is avoided because MakeCurrent is only win, width, height = c.win.nativeWindow(c.Context.VisualID())
// called during a FrameEvent.
c.win.callbacks.Run(func() {
win, width, height = c.win.nativeWindow(c.Context.VisualID())
})
if win == nil { if win == nil {
return nil return nil
} }
eglSurf := egl.NativeWindowType(unsafe.Pointer(win)) eglSurf := egl.NativeWindowType(unsafe.Pointer(win))
if err := c.Context.CreateSurface(eglSurf, width, height); err != nil { return c.Context.CreateSurface(eglSurf, width, height)
return err
}
if err := c.Context.MakeCurrent(); err != nil {
return err
}
return nil
} }
func (c *context) Lock() {} func (c *context) Lock() error {
return c.Context.MakeCurrent()
}
func (c *context) Unlock() {} func (c *context) Unlock() {
c.Context.ReleaseCurrent()
}
+7 -10
View File
@@ -50,10 +50,6 @@ func (c *context) Release() {
} }
func (c *context) Refresh() error { func (c *context) Refresh() error {
return nil
}
func (c *context) MakeCurrent() error {
c.Context.ReleaseSurface() c.Context.ReleaseSurface()
if c.eglWin != nil { if c.eglWin != nil {
C.wl_egl_window_destroy(c.eglWin) C.wl_egl_window_destroy(c.eglWin)
@@ -69,12 +65,13 @@ func (c *context) MakeCurrent() error {
} }
c.eglWin = eglWin c.eglWin = eglWin
eglSurf := egl.NativeWindowType(uintptr(unsafe.Pointer(eglWin))) eglSurf := egl.NativeWindowType(uintptr(unsafe.Pointer(eglWin)))
if err := c.Context.CreateSurface(eglSurf, width, height); err != nil { return c.Context.CreateSurface(eglSurf, width, height)
return err }
}
func (c *context) Lock() error {
return c.Context.MakeCurrent() return c.Context.MakeCurrent()
} }
func (c *context) Lock() {} func (c *context) Unlock() {
c.Context.ReleaseCurrent()
func (c *context) Unlock() {} }
+8 -9
View File
@@ -35,18 +35,12 @@ func (c *glContext) Release() {
} }
func (c *glContext) Refresh() error { func (c *glContext) Refresh() error {
return nil
}
func (c *glContext) MakeCurrent() error {
c.Context.ReleaseSurface() c.Context.ReleaseSurface()
var ( var (
win windows.Handle win windows.Handle
width, height int width, height int
) )
c.win.w.Run(func() { win, width, height = c.win.HWND()
win, width, height = c.win.HWND()
})
eglSurf := egl.NativeWindowType(win) eglSurf := egl.NativeWindowType(win)
if err := c.Context.CreateSurface(eglSurf, width, height); err != nil { if err := c.Context.CreateSurface(eglSurf, width, height); err != nil {
return err return err
@@ -55,9 +49,14 @@ func (c *glContext) MakeCurrent() error {
return err return err
} }
c.Context.EnableVSync(true) c.Context.EnableVSync(true)
c.Context.ReleaseCurrent()
return nil return nil
} }
func (c *glContext) Lock() {} func (c *glContext) Lock() error {
return c.Context.MakeCurrent()
}
func (c *glContext) Unlock() {} func (c *glContext) Unlock() {
c.Context.ReleaseCurrent()
}
+7 -6
View File
@@ -33,10 +33,6 @@ func (c *x11Context) Release() {
} }
func (c *x11Context) Refresh() error { func (c *x11Context) Refresh() error {
return nil
}
func (c *x11Context) MakeCurrent() error {
c.Context.ReleaseSurface() c.Context.ReleaseSurface()
win, width, height := c.win.window() win, width, height := c.win.window()
eglSurf := egl.NativeWindowType(uintptr(win)) eglSurf := egl.NativeWindowType(uintptr(win))
@@ -47,9 +43,14 @@ func (c *x11Context) MakeCurrent() error {
return err return err
} }
c.Context.EnableVSync(true) c.Context.EnableVSync(true)
c.Context.ReleaseCurrent()
return nil return nil
} }
func (c *x11Context) Lock() {} func (c *x11Context) Lock() error {
return c.Context.MakeCurrent()
}
func (c *x11Context) Unlock() {} func (c *x11Context) Unlock() {
c.Context.ReleaseCurrent()
}
+9 -10
View File
@@ -97,15 +97,18 @@ func (c *context) Present() error {
return nil return nil
} }
func (c *context) Lock() {} func (c *context) Lock() error {
if C.gio_makeCurrent(c.ctx) == 0 {
func (c *context) Unlock() {} return errors.New("[EAGLContext setCurrentContext] failed")
}
func (c *context) Refresh() error {
return nil return nil
} }
func (c *context) MakeCurrent() error { func (c *context) Unlock() {
C.gio_makeCurrent(0)
}
func (c *context) Refresh() error {
if C.gio_makeCurrent(c.ctx) == 0 { if C.gio_makeCurrent(c.ctx) == 0 {
return errors.New("[EAGLContext setCurrentContext] failed") return errors.New("[EAGLContext setCurrentContext] failed")
} }
@@ -139,10 +142,6 @@ func (c *context) MakeCurrent() error {
return nil return nil
} }
func (c *context) ReleaseCurrent() {
C.gio_makeCurrent(0)
}
func (w *window) NewContext() (Context, error) { func (w *window) NewContext() (Context, error) {
return newContext(w) return newContext(w)
} }
+3 -7
View File
@@ -50,7 +50,9 @@ func (c *context) Present() error {
return nil return nil
} }
func (c *context) Lock() {} func (c *context) Lock() error {
return nil
}
func (c *context) Unlock() {} func (c *context) Unlock() {}
@@ -58,12 +60,6 @@ func (c *context) Refresh() error {
return nil return nil
} }
func (c *context) MakeCurrent() error {
return nil
}
func (c *context) ReleaseCurrent() {}
func (w *window) NewContext() (Context, error) { func (w *window) NewContext() (Context, error) {
return newContext(w) return newContext(w)
} }
+4 -12
View File
@@ -68,11 +68,14 @@ func (c *context) Present() error {
return nil return nil
} }
func (c *context) Lock() { func (c *context) Lock() error {
C.gio_lockContext(c.ctx) C.gio_lockContext(c.ctx)
C.gio_makeCurrentContext(c.ctx)
return nil
} }
func (c *context) Unlock() { func (c *context) Unlock() {
C.gio_clearCurrentContext()
C.gio_unlockContext(c.ctx) C.gio_unlockContext(c.ctx)
} }
@@ -83,17 +86,6 @@ func (c *context) Refresh() error {
return nil return nil
} }
func (c *context) MakeCurrent() error {
c.Lock()
defer c.Unlock()
C.gio_makeCurrentContext(c.ctx)
return nil
}
func (c *context) ReleaseCurrent() {
C.gio_clearCurrentContext()
}
func (w *window) NewContext() (Context, error) { func (w *window) NewContext() (Context, error) {
return newContext(w) return newContext(w)
} }
+1 -3
View File
@@ -69,11 +69,9 @@ type Callbacks interface {
type Context interface { type Context interface {
API() gpu.API API() gpu.API
Present() error Present() error
MakeCurrent() error
ReleaseCurrent()
Refresh() error Refresh() error
Release() Release()
Lock() Lock() error
Unlock() Unlock()
} }
+25 -30
View File
@@ -17,14 +17,12 @@ type renderLoop struct {
drawing bool drawing bool
err error err error
ctx wm.Context ctx wm.Context
frames chan frame frames chan frame
results chan frameResult results chan frameResult
refresh chan struct{} ack chan struct{}
refreshErr chan error stop chan struct{}
ack chan struct{} stopped chan struct{}
stop chan struct{}
stopped chan struct{}
} }
type frame struct { type frame struct {
@@ -39,11 +37,9 @@ type frameResult struct {
func newLoop(ctx wm.Context) (*renderLoop, error) { func newLoop(ctx wm.Context) (*renderLoop, error) {
l := &renderLoop{ l := &renderLoop{
ctx: ctx, ctx: ctx,
frames: make(chan frame), frames: make(chan frame),
results: make(chan frameResult), results: make(chan frameResult),
refresh: make(chan struct{}),
refreshErr: make(chan error),
// Ack is buffered so GPU commands can be issued after // Ack is buffered so GPU commands can be issued after
// ack'ing the frame. // ack'ing the frame.
ack: make(chan struct{}, 1), ack: make(chan struct{}, 1),
@@ -65,25 +61,35 @@ func (l *renderLoop) renderLoop(ctx wm.Context) error {
runtime.LockOSThread() runtime.LockOSThread()
// Don't UnlockOSThread to avoid reuse by the Go runtime. // Don't UnlockOSThread to avoid reuse by the Go runtime.
if err := ctx.MakeCurrent(); err != nil { if err := ctx.Lock(); err != nil {
initErr <- err initErr <- err
return return
} }
defer ctx.ReleaseCurrent()
g, err := gpu.New(ctx.API()) g, err := gpu.New(ctx.API())
if err != nil { if err != nil {
ctx.Unlock()
initErr <- err initErr <- err
return return
} }
defer g.Release() defer func() {
if err := ctx.Lock(); err != nil {
return
}
defer ctx.Unlock()
g.Release()
}()
ctx.Unlock()
initErr <- nil initErr <- nil
loop: loop:
for { for {
select { select {
case <-l.refresh:
l.refreshErr <- ctx.MakeCurrent()
case frame := <-l.frames: case frame := <-l.frames:
ctx.Lock() var res frameResult
res.err = ctx.Lock()
if res.err != nil {
l.results <- res
break
}
if runtime.GOOS == "js" { if runtime.GOOS == "js" {
// Use transparent black when Gio is embedded, to allow mixing of Gio and // Use transparent black when Gio is embedded, to allow mixing of Gio and
// foreign content below. // foreign content below.
@@ -94,7 +100,6 @@ func (l *renderLoop) renderLoop(ctx wm.Context) error {
g.Collect(frame.viewport, frame.ops) g.Collect(frame.viewport, frame.ops)
// Signal that we're done with the frame ops. // Signal that we're done with the frame ops.
l.ack <- struct{}{} l.ack <- struct{}{}
var res frameResult
res.err = g.Frame() res.err = g.Frame()
if res.err == nil { if res.err == nil {
res.err = ctx.Present() res.err = ctx.Present()
@@ -134,16 +139,6 @@ func (l *renderLoop) Summary() string {
return l.summary return l.summary
} }
func (l *renderLoop) Refresh() {
if l.err != nil {
return
}
// Make sure any pending frame is complete.
l.Flush()
l.refresh <- struct{}{}
l.setErr(<-l.refreshErr)
}
// Draw initiates a draw of a frame. It returns a channel // Draw initiates a draw of a frame. It returns a channel
// than signals when the frame is no longer being accessed. // than signals when the frame is no longer being accessed.
func (l *renderLoop) Draw(viewport image.Point, frameOps *op.Ops) <-chan struct{} { func (l *renderLoop) Draw(viewport image.Point, frameOps *op.Ops) <-chan struct{} {
+6 -14
View File
@@ -149,6 +149,7 @@ func (w *Window) validateAndProcess(driver wm.Driver, frameStart time.Time, size
if err != nil { if err != nil {
return err return err
} }
sync = true
} }
w.loop, err = newLoop(w.ctx) w.loop, err = newLoop(w.ctx)
if err != nil { if err != nil {
@@ -156,6 +157,11 @@ func (w *Window) validateAndProcess(driver wm.Driver, frameStart time.Time, size
return err return err
} }
} }
if sync && w.ctx != nil {
w.driverRun(func(_ wm.Driver) {
w.ctx.Refresh()
})
}
w.processFrame(frameStart, size, frame) w.processFrame(frameStart, size, frame)
if sync && w.loop != nil { if sync && w.loop != nil {
if err := w.loop.Flush(); err != nil { if err := w.loop.Flush(); err != nil {
@@ -416,13 +422,6 @@ func (w *Window) destroy(err error) {
} }
} }
func (w *Window) refresh() {
w.driverRun(func(_ wm.Driver) {
w.ctx.Refresh()
})
w.loop.Refresh()
}
func (w *Window) destroyGPU() { func (w *Window) destroyGPU() {
if w.loop != nil { if w.loop != nil {
w.loop.Release() w.loop.Release()
@@ -484,8 +483,6 @@ func (w *Window) run(opts *wm.Options) {
w.loop.Release() w.loop.Release()
w.loop = nil w.loop = nil
} }
} else {
w.refresh()
} }
} }
w.stage = e2.Stage w.stage = e2.Stage
@@ -505,11 +502,6 @@ func (w *Window) run(opts *wm.Options) {
e2.Frame = w.update e2.Frame = w.update
e2.Queue = &w.queue e2.Queue = &w.queue
w.out <- e2.FrameEvent w.out <- e2.FrameEvent
if w.loop != nil {
if e2.Sync {
w.refresh()
}
}
frame, gotFrame := w.waitFrame() frame, gotFrame := w.waitFrame()
err := w.validateAndProcess(driver, frameStart, e2.Size, e2.Sync, frame) err := w.validateAndProcess(driver, frameStart, e2.Size, e2.Sync, frame)
if gotFrame { if gotFrame {