egl_wayland.go raw

   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/p9c/p9/pkg/gel/gio/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) MakeCurrent() error {
  52  	c.Context.ReleaseSurface()
  53  	if c.eglWin != nil {
  54  		C.wl_egl_window_destroy(c.eglWin)
  55  		c.eglWin = nil
  56  	}
  57  	surf, width, height := c.win.surface()
  58  	if surf == nil {
  59  		return errors.New("wayland: no surface")
  60  	}
  61  	eglWin := C.wl_egl_window_create(surf, C.int(width), C.int(height))
  62  	if eglWin == nil {
  63  		return errors.New("wayland: wl_egl_window_create failed")
  64  	}
  65  	c.eglWin = eglWin
  66  	eglSurf := egl.NativeWindowType(uintptr(unsafe.Pointer(eglWin)))
  67  	if err := c.Context.CreateSurface(eglSurf, width, height); err != nil {
  68  		return err
  69  	}
  70  	return c.Context.MakeCurrent()
  71  }
  72  
  73  func (c *context) Lock() {}
  74  
  75  func (c *context) Unlock() {}
  76