diff --git a/app/headless/headless.go b/app/headless/headless.go index 8ad772dd..892e5a4f 100644 --- a/app/headless/headless.go +++ b/app/headless/headless.go @@ -26,6 +26,7 @@ type Window struct { type context interface { Functions() *glimpl.Functions + Backend() (gpu.Backend, error) MakeCurrent() error ReleaseCurrent() Release() @@ -53,7 +54,7 @@ func NewWindow(width, height int) (*Window, error) { ctx.Release() return err } - backend, err := gl.NewBackend(f) + backend, err := ctx.Backend() if err != nil { fbo.Release() ctx.Release() diff --git a/app/headless/headless_darwin.go b/app/headless/headless_darwin.go index 998173ac..5d8c5ecf 100644 --- a/app/headless/headless_darwin.go +++ b/app/headless/headless_darwin.go @@ -4,6 +4,8 @@ package headless import ( "gioui.org/app/internal/glimpl" + "gioui.org/gpu" + "gioui.org/gpu/gl" ) /* @@ -38,6 +40,10 @@ func (c *nsContext) ReleaseCurrent() { C.gio_headless_clearCurrentContext(c.ctx) } +func (c *nsContext) Backend() (gpu.Backend, error) { + return gl.NewBackend(c.c) +} + func (c *nsContext) Functions() *glimpl.Functions { return c.c } diff --git a/app/headless/headless_js.go b/app/headless/headless_js.go index 90ca85eb..c8815ea5 100644 --- a/app/headless/headless_js.go +++ b/app/headless/headless_js.go @@ -7,6 +7,8 @@ import ( "syscall/js" "gioui.org/app/internal/glimpl" + "gioui.org/gpu" + "gioui.org/gpu/gl" ) type jsContext struct { @@ -37,6 +39,10 @@ func newContext() (*jsContext, error) { return c, nil } +func (c *jsContext) Backend() (gpu.Backend, error) { + return gl.NewBackend(c.f) +} + func (c *jsContext) Functions() *glimpl.Functions { return c.f } diff --git a/app/internal/egl/egl.go b/app/internal/egl/egl.go index c6b6a628..4a34390e 100644 --- a/app/internal/egl/egl.go +++ b/app/internal/egl/egl.go @@ -12,6 +12,8 @@ import ( "gioui.org/app/internal/glimpl" "gioui.org/app/internal/srgb" + "gioui.org/gpu" + "gioui.org/gpu/gl" ) type Context struct { @@ -119,6 +121,10 @@ func (c *Context) Functions() *glimpl.Functions { return c.c } +func (c *Context) Backend() (gpu.Backend, error) { + return gl.NewBackend(c.c) +} + func (c *Context) ReleaseSurface() { if c.eglSurf == nilEGLSurface { return diff --git a/app/internal/window/gl_ios.go b/app/internal/window/gl_ios.go index 5e340c2f..4bc03534 100644 --- a/app/internal/window/gl_ios.go +++ b/app/internal/window/gl_ios.go @@ -17,6 +17,7 @@ import ( "fmt" "gioui.org/app/internal/glimpl" + "gioui.org/gpu" "gioui.org/gpu/gl" ) @@ -50,8 +51,8 @@ func newContext(w *window) (*context, error) { return c, nil } -func (c *context) Functions() *glimpl.Functions { - return c.c +func (c *context) Backend() (gpu.Backend, error) { + return gl.NewBackend(c.c) } func (c *context) Release() { diff --git a/app/internal/window/gl_js.go b/app/internal/window/gl_js.go index 60f07e49..64f691a8 100644 --- a/app/internal/window/gl_js.go +++ b/app/internal/window/gl_js.go @@ -8,6 +8,8 @@ import ( "gioui.org/app/internal/glimpl" "gioui.org/app/internal/srgb" + "gioui.org/gpu" + "gioui.org/gpu/gl" ) type context struct { @@ -45,8 +47,8 @@ func newContext(w *window) (*context, error) { return c, nil } -func (c *context) Functions() *glimpl.Functions { - return c.f +func (c *context) Backend() (gpu.Backend, error) { + return gl.NewBackend(c.f) } func (c *context) Release() { diff --git a/app/internal/window/gl_macos.go b/app/internal/window/gl_macos.go index 5c1f34d1..944b2327 100644 --- a/app/internal/window/gl_macos.go +++ b/app/internal/window/gl_macos.go @@ -6,6 +6,8 @@ package window import ( "gioui.org/app/internal/glimpl" + "gioui.org/gpu" + "gioui.org/gpu/gl" ) /* @@ -40,8 +42,8 @@ func newContext(w *window) (*context, error) { return c, nil } -func (c *context) Functions() *glimpl.Functions { - return c.c +func (c *context) Backend() (gpu.Backend, error) { + return gl.NewBackend(c.c) } func (c *context) Release() { diff --git a/app/internal/window/window.go b/app/internal/window/window.go index 10083d8d..36f7e16c 100644 --- a/app/internal/window/window.go +++ b/app/internal/window/window.go @@ -9,7 +9,7 @@ import ( "math" "time" - "gioui.org/app/internal/glimpl" + "gioui.org/gpu" "gioui.org/io/event" "gioui.org/io/system" "gioui.org/unit" @@ -32,7 +32,7 @@ type Callbacks interface { } type Context interface { - Functions() *glimpl.Functions + Backend() (gpu.Backend, error) Present() error MakeCurrent() error Release() diff --git a/app/loop.go b/app/loop.go index 49f8cb08..0b2195a2 100644 --- a/app/loop.go +++ b/app/loop.go @@ -8,7 +8,6 @@ import ( "gioui.org/app/internal/window" "gioui.org/gpu" - "gioui.org/gpu/gl" "gioui.org/op" ) @@ -54,7 +53,7 @@ func newLoop(ctx window.Context) (*renderLoop, error) { return l, nil } -func (l *renderLoop) renderLoop(glctx window.Context) error { +func (l *renderLoop) renderLoop(ctx window.Context) error { // GL Operations must happen on a single OS thread, so // pass initialization result through a channel. initErr := make(chan error) @@ -63,38 +62,38 @@ func (l *renderLoop) renderLoop(glctx window.Context) error { runtime.LockOSThread() // Don't UnlockOSThread to avoid reuse by the Go runtime. - if err := glctx.MakeCurrent(); err != nil { + if err := ctx.MakeCurrent(); err != nil { initErr <- err return } - ctx, err := gl.NewBackend(glctx.Functions()) + b, err := ctx.Backend() if err != nil { initErr <- err return } - g, err := gpu.New(ctx) + g, err := gpu.New(b) if err != nil { initErr <- err return } - defer glctx.Release() + defer ctx.Release() initErr <- nil loop: for { select { case <-l.refresh: - l.refreshErr <- glctx.MakeCurrent() + l.refreshErr <- ctx.MakeCurrent() case frame := <-l.frames: - glctx.Lock() + ctx.Lock() g.Collect(frame.viewport, frame.ops) // Signal that we're done with the frame ops. l.ack <- struct{}{} g.BeginFrame() var res frameResult - res.err = glctx.Present() + res.err = ctx.Present() g.EndFrame() res.profile = g.Profile() - glctx.Unlock() + ctx.Unlock() l.results <- res case <-l.stop: break loop