github.com/cybriq/giocore@v0.0.7-0.20210703034601-cfb9cb5f3900/app/internal/wm/egl_wayland.go (about) 1 // SPDX-License-Identifier: Unlicense OR MIT 2 3 // +build linux,!android,!nowayland freebsd 4 5 package wm 6 7 import ( 8 "errors" 9 "unsafe" 10 11 "github.com/cybriq/giocore/internal/egl" 12 ) 13 14 /* 15 #cgo linux pkg-config: egl wayland-egl 16 #cgo freebsd openbsd LDFLAGS: -lwayland-egl 17 #cgo CFLAGS: -DEGL_NO_X11 18 19 #include <EGL/egl.h> 20 #include <wayland-client.h> 21 #include <wayland-egl.h> 22 */ 23 import "C" 24 25 type context struct { 26 win *window 27 *egl.Context 28 eglWin *C.struct_wl_egl_window 29 } 30 31 func (w *window) NewContext() (Context, error) { 32 disp := egl.NativeDisplayType(unsafe.Pointer(w.display())) 33 ctx, err := egl.NewContext(disp) 34 if err != nil { 35 return nil, err 36 } 37 return &context{Context: ctx, win: w}, nil 38 } 39 40 func (c *context) Release() { 41 if c.Context != nil { 42 c.Context.Release() 43 c.Context = nil 44 } 45 if c.eglWin != nil { 46 C.wl_egl_window_destroy(c.eglWin) 47 c.eglWin = nil 48 } 49 } 50 51 func (c *context) Refresh() error { 52 return nil 53 } 54 55 func (c *context) MakeCurrent() error { 56 c.Context.ReleaseSurface() 57 if c.eglWin != nil { 58 C.wl_egl_window_destroy(c.eglWin) 59 c.eglWin = nil 60 } 61 surf, width, height := c.win.surface() 62 if surf == nil { 63 return errors.New("wayland: no surface") 64 } 65 eglWin := C.wl_egl_window_create(surf, C.int(width), C.int(height)) 66 if eglWin == nil { 67 return errors.New("wayland: wl_egl_window_create failed") 68 } 69 c.eglWin = eglWin 70 eglSurf := egl.NativeWindowType(uintptr(unsafe.Pointer(eglWin))) 71 if err := c.Context.CreateSurface(eglSurf, width, height); err != nil { 72 return err 73 } 74 return c.Context.MakeCurrent() 75 } 76 77 func (c *context) Lock() {} 78 79 func (c *context) Unlock() {}