aboutsummaryrefslogtreecommitdiff
path: root/vendor/gioui.org/app/gl_js.go
blob: e3aee8d716852bad254bf9941eb7e5a5caba12aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// SPDX-License-Identifier: Unlicense OR MIT

package app

import (
	"errors"
	"syscall/js"

	"gioui.org/gpu"
	"gioui.org/internal/gl"
)

type glContext struct {
	ctx js.Value
	cnv js.Value
}

func newContext(w *window) (*glContext, 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 := &glContext{
		ctx: ctx,
		cnv: w.cnv,
	}
	return c, nil
}

func (c *glContext) RenderTarget() (gpu.RenderTarget, error) {
	return gpu.OpenGLRenderTarget{}, nil
}

func (c *glContext) API() gpu.API {
	return gpu.OpenGL{Context: gl.Context(c.ctx)}
}

func (c *glContext) Release() {
}

func (c *glContext) Present() error {
	if c.ctx.Call("isContextLost").Bool() {
		return errors.New("context lost")
	}
	return nil
}

func (c *glContext) Lock() error {
	return nil
}

func (c *glContext) Unlock() {}

func (c *glContext) Refresh() error {
	return nil
}

func (w *window) NewContext() (context, error) {
	return newContext(w)
}