aboutsummaryrefslogtreecommitdiff
path: root/vendor/gioui.org/app/internal/window/egl_wayland.go
blob: b4878b4c92a43594587afdebce511f1999bb0a17 (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
70
71
72
73
74
75
// SPDX-License-Identifier: Unlicense OR MIT

// +build linux,!android,!nowayland freebsd

package window

import (
	"errors"
	"unsafe"

	"gioui.org/app/internal/egl"
)

/*
#cgo linux pkg-config: egl wayland-egl
#cgo freebsd openbsd LDFLAGS: -lwayland-egl
#cgo CFLAGS: -DMESA_EGL_NO_X11_HEADERS

#include <EGL/egl.h>
#include <wayland-client.h>
#include <wayland-egl.h>
*/
import "C"

type context struct {
	win *window
	*egl.Context
	eglWin *C.struct_wl_egl_window
}

func (w *window) NewContext() (Context, error) {
	disp := egl.NativeDisplayType(unsafe.Pointer(w.display()))
	ctx, err := egl.NewContext(disp)
	if err != nil {
		return nil, err
	}
	return &context{Context: ctx, win: w}, nil
}

func (c *context) Release() {
	if c.Context != nil {
		c.Context.Release()
		c.Context = nil
	}
	if c.eglWin != nil {
		C.wl_egl_window_destroy(c.eglWin)
		c.eglWin = nil
	}
}

func (c *context) MakeCurrent() error {
	c.Context.ReleaseSurface()
	if c.eglWin != nil {
		C.wl_egl_window_destroy(c.eglWin)
		c.eglWin = nil
	}
	surf, width, height := c.win.surface()
	if surf == nil {
		return errors.New("wayland: no surface")
	}
	eglWin := C.wl_egl_window_create(surf, C.int(width), C.int(height))
	if eglWin == nil {
		return errors.New("wayland: wl_egl_window_create failed")
	}
	c.eglWin = eglWin
	eglSurf := egl.NativeWindowType(uintptr(unsafe.Pointer(eglWin)))
	if err := c.Context.CreateSurface(eglSurf, width, height); err != nil {
		return err
	}
	return c.Context.MakeCurrent()
}

func (c *context) Lock() {}

func (c *context) Unlock() {}