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

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  //go:build !noopengl
     4  
     5  package app
     6  
     7  /*
     8  #include <android/native_window_jni.h>
     9  #include <EGL/egl.h>
    10  */
    11  import "C"
    12  
    13  import (
    14  	"unsafe"
    15  
    16  	"gioui.org/internal/egl"
    17  )
    18  
    19  type androidContext struct {
    20  	win     *window
    21  	eglSurf egl.NativeWindowType
    22  	*egl.Context
    23  }
    24  
    25  func init() {
    26  	newAndroidGLESContext = func(w *window) (context, error) {
    27  		ctx, err := egl.NewContext(nil)
    28  		if err != nil {
    29  			return nil, err
    30  		}
    31  		return &androidContext{win: w, Context: ctx}, nil
    32  	}
    33  }
    34  
    35  func (c *androidContext) Release() {
    36  	if c.Context != nil {
    37  		c.Context.Release()
    38  		c.Context = nil
    39  	}
    40  }
    41  
    42  func (c *androidContext) Refresh() error {
    43  	c.Context.ReleaseSurface()
    44  	if err := c.win.setVisual(c.Context.VisualID()); err != nil {
    45  		return err
    46  	}
    47  	win, _, _ := c.win.nativeWindow()
    48  	c.eglSurf = egl.NativeWindowType(unsafe.Pointer(win))
    49  	return nil
    50  }
    51  
    52  func (c *androidContext) Lock() error {
    53  	// The Android emulator creates a broken surface if it is not
    54  	// created on the same thread as the context is made current.
    55  	if c.eglSurf != nil {
    56  		if err := c.Context.CreateSurface(c.eglSurf); err != nil {
    57  			return err
    58  		}
    59  		c.eglSurf = nil
    60  	}
    61  	return c.Context.MakeCurrent()
    62  }
    63  
    64  func (c *androidContext) Unlock() {
    65  	c.Context.ReleaseCurrent()
    66  }