Files
gio/app/internal/window/gl_js.go
T
Elias Naur ffe5ab51a2 gpu/gl: remove OpenGL functions parameter from NewBackend
As a consequence, most API is gone from gpu/gl, and embedding Gio in
foreign frameworks don't need to provide an OpenGL implementation.

The next change simplifies the GLFW embedding example accordingly.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-12-04 20:50:22 +01:00

91 lines
1.7 KiB
Go

// SPDX-License-Identifier: Unlicense OR MIT
package window
import (
"errors"
"syscall/js"
"gioui.org/app/internal/srgb"
"gioui.org/gpu/backend"
"gioui.org/gpu/gl"
"gioui.org/internal/glimpl"
)
type context struct {
ctx js.Value
cnv js.Value
srgbFBO *srgb.FBO
}
func newContext(w *window) (*context, error) {
args := map[string]interface{}{
// Enable low latency rendering.
// See https://developers.google.com/web/updates/2019/05/desynchronized.
"desynchronized": true,
"preserveDrawingBuffer": true,
}
ctx := w.cnv.Call("getContext", "webgl2", args)
if ctx.IsNull() {
ctx = w.cnv.Call("getContext", "webgl", args)
}
if ctx.IsNull() {
return nil, errors.New("app: webgl is not supported")
}
c := &context{
ctx: ctx,
cnv: w.cnv,
}
return c, nil
}
func (c *context) Backend() (backend.Device, error) {
return gl.NewBackend(glimpl.Context(c.ctx))
}
func (c *context) Release() {
if c.srgbFBO != nil {
c.srgbFBO.Release()
c.srgbFBO = nil
}
}
func (c *context) Present() error {
if c.srgbFBO != nil {
c.srgbFBO.Blit()
}
if c.srgbFBO != nil {
c.srgbFBO.AfterPresent()
}
if c.ctx.Call("isContextLost").Bool() {
return errors.New("context lost")
}
return nil
}
func (c *context) Lock() {}
func (c *context) Unlock() {}
func (c *context) MakeCurrent() error {
if c.srgbFBO == nil {
var err error
c.srgbFBO, err = srgb.New(glimpl.Context(c.ctx))
if err != nil {
c.Release()
c.srgbFBO = nil
return err
}
}
w, h := c.cnv.Get("width").Int(), c.cnv.Get("height").Int()
if err := c.srgbFBO.Refresh(w, h); err != nil {
c.Release()
return err
}
return nil
}
func (w *window) NewContext() (Context, error) {
return newContext(w)
}