gpu/backend: move backend interface types to a separate package

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-02-23 12:46:17 +01:00
parent 50e98d1e13
commit 5cd5d49108
17 changed files with 214 additions and 206 deletions
+17 -16
View File
@@ -9,21 +9,22 @@ import (
"runtime"
"gioui.org/gpu"
"gioui.org/gpu/backend"
"gioui.org/op"
)
// Window is a headless window.
type Window struct {
size image.Point
ctx backend
backend gpu.Backend
ctx context
backend backend.Device
gpu *gpu.GPU
fboTex gpu.Texture
fbo gpu.Framebuffer
fboTex backend.Texture
fbo backend.Framebuffer
}
type backend interface {
Backend() (gpu.Backend, error)
type context interface {
Backend() (backend.Device, error)
MakeCurrent() error
ReleaseCurrent()
Release()
@@ -40,27 +41,27 @@ func NewWindow(width, height int) (*Window, error) {
ctx: ctx,
}
err = contextDo(ctx, func() error {
backend, err := ctx.Backend()
dev, err := ctx.Backend()
if err != nil {
return err
}
fboTex, err := backend.NewTexture(
gpu.TextureFormatSRGB,
fboTex, err := dev.NewTexture(
backend.TextureFormatSRGB,
width, height,
gpu.FilterNearest, gpu.FilterNearest,
gpu.BufferBindingFramebuffer,
backend.FilterNearest, backend.FilterNearest,
backend.BufferBindingFramebuffer,
)
if err != nil {
return nil
}
const depthBits = 16
fbo, err := backend.NewFramebuffer(fboTex, depthBits)
fbo, err := dev.NewFramebuffer(fboTex, depthBits)
if err != nil {
fboTex.Release()
return err
}
backend.BindFramebuffer(fbo)
gp, err := gpu.New(backend)
dev.BindFramebuffer(fbo)
gp, err := gpu.New(dev)
if err != nil {
fbo.Release()
fboTex.Release()
@@ -69,7 +70,7 @@ func NewWindow(width, height int) (*Window, error) {
w.fboTex = fboTex
w.fbo = fbo
w.gpu = gp
w.backend = backend
w.backend = dev
return err
})
if err != nil {
@@ -139,7 +140,7 @@ func (w *Window) Screenshot() (*image.RGBA, error) {
return img, nil
}
func contextDo(ctx backend, f func() error) error {
func contextDo(ctx context, f func() error) error {
errCh := make(chan error)
go func() {
runtime.LockOSThread()
+3 -3
View File
@@ -4,7 +4,7 @@ package headless
import (
"gioui.org/app/internal/glimpl"
"gioui.org/gpu"
"gioui.org/gpu/backend"
"gioui.org/gpu/gl"
)
@@ -22,7 +22,7 @@ type nsContext struct {
prepared bool
}
func newGLContext() (backend, error) {
func newGLContext() (context, error) {
ctx := C.gio_headless_newContext()
return &nsContext{ctx: ctx, c: new(glimpl.Functions)}, nil
}
@@ -40,7 +40,7 @@ func (c *nsContext) ReleaseCurrent() {
C.gio_headless_clearCurrentContext(c.ctx)
}
func (c *nsContext) Backend() (gpu.Backend, error) {
func (c *nsContext) Backend() (backend.Device, error) {
return gl.NewBackend(c.c)
}
+1 -1
View File
@@ -8,6 +8,6 @@ import (
"gioui.org/app/internal/egl"
)
func newGLContext() (backend, error) {
func newGLContext() (context, error) {
return egl.NewContext(egl.EGL_DEFAULT_DISPLAY)
}
+1 -1
View File
@@ -2,6 +2,6 @@
package headless
func newContext() (backend, error) {
func newContext() (context, error) {
return newGLContext()
}
+3 -3
View File
@@ -7,7 +7,7 @@ import (
"syscall/js"
"gioui.org/app/internal/glimpl"
"gioui.org/gpu"
"gioui.org/gpu/backend"
"gioui.org/gpu/gl"
)
@@ -16,7 +16,7 @@ type jsContext struct {
f *glimpl.Functions
}
func newGLContext() (backend, error) {
func newGLContext() (context, error) {
version := 2
doc := js.Global().Get("document")
cnv := doc.Call("createElement", "canvas")
@@ -39,7 +39,7 @@ func newGLContext() (backend, error) {
return c, nil
}
func (c *jsContext) Backend() (gpu.Backend, error) {
func (c *jsContext) Backend() (backend.Device, error) {
return gl.NewBackend(c.f)
}
+2 -2
View File
@@ -12,7 +12,7 @@ import (
"gioui.org/app/internal/glimpl"
"gioui.org/app/internal/srgb"
"gioui.org/gpu"
"gioui.org/gpu/backend"
"gioui.org/gpu/gl"
)
@@ -121,7 +121,7 @@ func (c *Context) Functions() *glimpl.Functions {
return c.c
}
func (c *Context) Backend() (gpu.Backend, error) {
func (c *Context) Backend() (backend.Device, error) {
return gl.NewBackend(c.c)
}
+2 -2
View File
@@ -17,7 +17,7 @@ import (
"fmt"
"gioui.org/app/internal/glimpl"
"gioui.org/gpu"
"gioui.org/gpu/backend"
"gioui.org/gpu/gl"
)
@@ -51,7 +51,7 @@ func newContext(w *window) (*context, error) {
return c, nil
}
func (c *context) Backend() (gpu.Backend, error) {
func (c *context) Backend() (backend.Device, error) {
return gl.NewBackend(c.c)
}
+2 -2
View File
@@ -8,7 +8,7 @@ import (
"gioui.org/app/internal/glimpl"
"gioui.org/app/internal/srgb"
"gioui.org/gpu"
"gioui.org/gpu/backend"
"gioui.org/gpu/gl"
)
@@ -47,7 +47,7 @@ func newContext(w *window) (*context, error) {
return c, nil
}
func (c *context) Backend() (gpu.Backend, error) {
func (c *context) Backend() (backend.Device, error) {
return gl.NewBackend(c.f)
}
+2 -2
View File
@@ -6,7 +6,7 @@ package window
import (
"gioui.org/app/internal/glimpl"
"gioui.org/gpu"
"gioui.org/gpu/backend"
"gioui.org/gpu/gl"
)
@@ -42,7 +42,7 @@ func newContext(w *window) (*context, error) {
return c, nil
}
func (c *context) Backend() (gpu.Backend, error) {
func (c *context) Backend() (backend.Device, error) {
return gl.NewBackend(c.c)
}
+2 -2
View File
@@ -9,7 +9,7 @@ import (
"math"
"time"
"gioui.org/gpu"
"gioui.org/gpu/backend"
"gioui.org/io/event"
"gioui.org/io/system"
"gioui.org/unit"
@@ -32,7 +32,7 @@ type Callbacks interface {
}
type Context interface {
Backend() (gpu.Backend, error)
Backend() (backend.Device, error)
Present() error
MakeCurrent() error
Release()