gioui.org@v0.6.1-0.20240506124620-7a9ce51988ce/app/egl_x11.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  //go:build ((linux && !android) || freebsd || openbsd) && !nox11 && !noopengl
     4  // +build linux,!android freebsd openbsd
     5  // +build !nox11
     6  // +build !noopengl
     7  
     8  package app
     9  
    10  import (
    11  	"unsafe"
    12  
    13  	"gioui.org/internal/egl"
    14  )
    15  
    16  type x11Context struct {
    17  	win *x11Window
    18  	*egl.Context
    19  }
    20  
    21  func init() {
    22  	newX11EGLContext = func(w *x11Window) (context, error) {
    23  		disp := egl.NativeDisplayType(unsafe.Pointer(w.display()))
    24  		ctx, err := egl.NewContext(disp)
    25  		if err != nil {
    26  			return nil, err
    27  		}
    28  		win, _, _ := w.window()
    29  		eglSurf := egl.NativeWindowType(uintptr(win))
    30  		if err := ctx.CreateSurface(eglSurf); err != nil {
    31  			ctx.Release()
    32  			return nil, err
    33  		}
    34  		if err := ctx.MakeCurrent(); err != nil {
    35  			ctx.Release()
    36  			return nil, err
    37  		}
    38  		defer ctx.ReleaseCurrent()
    39  		ctx.EnableVSync(true)
    40  		return &x11Context{win: w, Context: ctx}, nil
    41  	}
    42  }
    43  
    44  func (c *x11Context) Release() {
    45  	if c.Context != nil {
    46  		c.Context.Release()
    47  		c.Context = nil
    48  	}
    49  }
    50  
    51  func (c *x11Context) Refresh() error {
    52  	return nil
    53  }
    54  
    55  func (c *x11Context) Lock() error {
    56  	return c.Context.MakeCurrent()
    57  }
    58  
    59  func (c *x11Context) Unlock() {
    60  	c.Context.ReleaseCurrent()
    61  }