mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
app,app/headless: expose Backends from window and headless contexts
This is a refactoring change to prepare for another gpu.Backend implementations. Notably, app/loop.go no longer imports gpu/gl. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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()
|
||||
|
||||
+9
-10
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user