github.com/gop9/olt@v0.0.0-20200202132135-d956aad50b08/gio/app/internal/window/egl_wayland.go (about)

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