mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-03 08:25:34 +00:00
gpu/backend: move backend interface types to a separate package
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+17
-16
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
|
||||
package headless
|
||||
|
||||
func newContext() (backend, error) {
|
||||
func newContext() (context, error) {
|
||||
return newGLContext()
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user