~eliasnaur/gio-example

ref: 3efe9b7e1c636f6617452bff2475256dd0ac3939 gio-example/opengl/main.go -rw-r--r-- 6.5 KiB
3efe9b7eElias Naur opengl: fix build error on Windows 5 months ago
                                                                                
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// SPDX-License-Identifier: Unlicense OR MIT

// +build darwin windows

package main

// This program demonstrates the use of a custom OpenGL ES context with
// app.Window. It is similar to the GLFW example, but uses Gio's window
// implementation instead of the one in GLFW.
//
// The example runs on macOS and Windows using ANGLE.

import (
	"errors"
	"fmt"
	"log"
	"os"
	"runtime"
	"strings"
	"unsafe"

	"gioui.org/app"
	"gioui.org/gpu"
	"gioui.org/io/pointer"
	"gioui.org/io/system"
	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/widget"
	"gioui.org/widget/material"

	"gioui.org/font/gofont"
)

/*
#cgo CFLAGS: -DEGL_NO_X11
#cgo LDFLAGS: -lEGL -lGLESv2

#include <EGL/egl.h>
#include <GLES2/gl2.h>
#define EGL_EGLEXT_PROTOTYPES
#include <EGL/eglext.h>

*/
import "C"

type eglContext struct {
	disp C.EGLDisplay
	ctx  C.EGLContext
	surf C.EGLSurface
}

const (
	// needDepthBuffer must be true when the program needs a depth buffer, or
	// when using the old non-compute Gio renderer.
	needDepthBuffer = true
)

func main() {
	go func() {
		// Set CustomRenderer so we can provide our own rendering context.
		w := app.NewWindow(app.CustomRenderer(true))
		if err := loop(w); err != nil {
			log.Fatal(err)
		}
		os.Exit(0)
	}()
	app.Main()
}

var button widget.Clickable

func loop(w *app.Window) error {
	// OpenGL stores the current context in thread local storage.
	runtime.LockOSThread()

	th := material.NewTheme(gofont.Collection())
	var ops op.Ops
	var (
		ctx    *eglContext
		gioCtx gpu.GPU
	)
	for e := range w.Events() {
		switch e := e.(type) {
		case app.ViewEvent:
			w.Run(func() {
				if gioCtx != nil {
					gioCtx.Release()
					gioCtx = nil
				}
				if ctx != nil {
					ctx.Release()
					ctx = nil
				}
				view := nativeViewFor(e)
				var nilv C.EGLNativeWindowType
				if view == nilv {
					return
				}
				c, err := createContext(view)
				if err != nil {
					log.Fatal(err)
				}
				ctx = c
				if ok := C.eglMakeCurrent(ctx.disp, ctx.surf, ctx.surf, ctx.ctx); ok != C.EGL_TRUE {
					err := fmt.Errorf("eglMakeCurrent failed (%#x)", C.eglGetError())
					log.Fatal(err)
				}
				glGetString := func(e C.GLenum) string {
					return C.GoString((*C.char)(unsafe.Pointer(C.glGetString(e))))
				}
				fmt.Printf("GL_VERSION: %s\nGL_RENDERER: %s\n", glGetString(C.GL_VERSION), glGetString(C.GL_RENDERER))
				gioCtx, err = gpu.New(gpu.OpenGL{ES: true})
				if err != nil {
					log.Fatal(err)
				}
				C.eglMakeCurrent(ctx.disp, nil, nil, nil)
			})
		case system.DestroyEvent:
			return e.Err
		case system.FrameEvent:
			if gioCtx == nil {
				break
			}
			// Build ops.
			gtx := layout.NewContext(&ops, e)
			// Catch pointer events not hitting UI.
			types := pointer.Move | pointer.Press | pointer.Release
			pointer.InputOp{Tag: w, Types: types}.Add(gtx.Ops)
			for _, e := range gtx.Events(w) {
				log.Println("Event:", e)
			}
			drawUI(th, gtx)
			w.Run(func() {
				if ok := C.eglMakeCurrent(ctx.disp, ctx.surf, ctx.surf, ctx.ctx); ok != C.EGL_TRUE {
					err := fmt.Errorf("eglMakeCurrent failed (%#x)", C.eglGetError())
					log.Fatal(err)
				}
				// Trigger window resize detection in ANGLE.
				C.eglWaitClient()
				// Draw custom OpenGL content.
				drawGL()

				// Render drawing ops.
				gioCtx.Collect(e.Size, gtx.Ops)
				gioCtx.Frame()

				if ok := C.eglSwapBuffers(ctx.disp, ctx.surf); ok != C.EGL_TRUE {
					log.Fatal(fmt.Errorf("swap failed: %v", C.eglGetError()))
				}
				C.eglMakeCurrent(ctx.disp, nil, nil, nil)
			})

			// Process non-drawing ops.
			e.Frame(gtx.Ops)
		}
	}
	return nil
}

func drawGL() {
	C.glClearColor(.5, .5, 0, 1)
	C.glClear(C.GL_COLOR_BUFFER_BIT | C.GL_DEPTH_BUFFER_BIT)
}

func drawUI(th *material.Theme, gtx layout.Context) layout.Dimensions {
	return layout.Center.Layout(gtx,
		material.Button(th, &button, "Button").Layout,
	)
}

func createContext(view C.EGLNativeWindowType) (*eglContext, error) {
	var EGL_NO_DISPLAY C.EGLDisplay
	platformExts := strings.Split(C.GoString(C.eglQueryString(EGL_NO_DISPLAY, C.EGL_EXTENSIONS)), " ")
	platformType := C.EGLint(C.EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE)
	if hasExtension(platformExts, "EGL_ANGLE_platform_angle_metal") {
		// The Metal backend works better than the OpenGL backend.
		platformType = C.EGL_PLATFORM_ANGLE_TYPE_METAL_ANGLE
	}
	attrs := []C.EGLint{
		C.EGL_PLATFORM_ANGLE_TYPE_ANGLE,
		platformType,
		C.EGL_NONE,
	}
	disp := C.eglGetPlatformDisplayEXT(C.EGL_PLATFORM_ANGLE_ANGLE, nil, &attrs[0])
	if disp == 0 {
		return nil, fmt.Errorf("eglGetPlatformDisplay failed: 0x%x", C.eglGetError())
	}
	var major, minor C.EGLint
	if ok := C.eglInitialize(disp, &major, &minor); ok != C.EGL_TRUE {
		return nil, fmt.Errorf("eglInitialize failed: 0x%x", C.eglGetError())
	}
	exts := strings.Split(C.GoString(C.eglQueryString(disp, C.EGL_EXTENSIONS)), " ")
	srgb := hasExtension(exts, "EGL_KHR_gl_colorspace")
	attribs := []C.EGLint{
		C.EGL_RENDERABLE_TYPE, C.EGL_OPENGL_ES2_BIT,
		C.EGL_SURFACE_TYPE, C.EGL_WINDOW_BIT,
		C.EGL_BLUE_SIZE, 8,
		C.EGL_GREEN_SIZE, 8,
		C.EGL_RED_SIZE, 8,
		C.EGL_CONFIG_CAVEAT, C.EGL_NONE,
	}
	if srgb {
		// Some drivers need alpha for sRGB framebuffers to work.
		attribs = append(attribs, C.EGL_ALPHA_SIZE, 8)
	}
	if needDepthBuffer {
		attribs = append(attribs, C.EGL_DEPTH_SIZE, 16)
	}
	attribs = append(attribs, C.EGL_NONE)
	var (
		cfg     C.EGLConfig
		numCfgs C.EGLint
	)
	if ok := C.eglChooseConfig(disp, &attribs[0], &cfg, 1, &numCfgs); ok != C.EGL_TRUE {
		return nil, fmt.Errorf("eglChooseConfig failed: 0x%x", C.eglGetError())
	}
	if numCfgs == 0 {
		supportsNoCfg := hasExtension(exts, "EGL_KHR_no_config_context")
		if !supportsNoCfg {
			return nil, errors.New("eglChooseConfig returned no configs")
		}
	}
	ctxAttribs := []C.EGLint{
		C.EGL_CONTEXT_CLIENT_VERSION, 3,
		C.EGL_NONE,
	}
	ctx := C.eglCreateContext(disp, cfg, nil, &ctxAttribs[0])
	if ctx == nil {
		return nil, fmt.Errorf("eglCreateContext failed: 0x%x", C.eglGetError())
	}
	var surfAttribs []C.EGLint
	if srgb {
		surfAttribs = append(surfAttribs, C.EGL_GL_COLORSPACE, C.EGL_GL_COLORSPACE_SRGB)
	}
	surfAttribs = append(surfAttribs, C.EGL_NONE)
	surf := C.eglCreateWindowSurface(disp, cfg, view, &surfAttribs[0])
	if surf == nil {
		return nil, fmt.Errorf("eglCreateWindowSurface failed (0x%x)", C.eglGetError())
	}
	return &eglContext{disp: disp, ctx: ctx, surf: surf}, nil
}

func (c *eglContext) Release() {
	if c.ctx != nil {
		C.eglDestroyContext(c.disp, c.ctx)
	}
	if c.surf != nil {
		C.eglDestroySurface(c.disp, c.surf)
	}
	*c = eglContext{}
}

func hasExtension(exts []string, ext string) bool {
	for _, e := range exts {
		if ext == e {
			return true
		}
	}
	return false
}