github.com/Seikaijyu/gio@v0.0.1/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  	"github.com/Seikaijyu/gio/internal/egl"
    17  )
    18  
    19  type androidContext struct {
    20  	win           *window
    21  	eglSurf       egl.NativeWindowType
    22  	width, height int
    23  	*egl.Context
    24  }
    25  
    26  func init() {
    27  	newAndroidGLESContext = func(w *window) (context, error) {
    28  		ctx, err := egl.NewContext(nil)
    29  		if err != nil {
    30  			return nil, err
    31  		}
    32  		return &androidContext{win: w, Context: ctx}, nil
    33  	}
    34  }
    35  
    36  func (c *androidContext) Release() {
    37  	if c.Context != nil {
    38  		c.Context.Release()
    39  		c.Context = nil
    40  	}
    41  }
    42  
    43  func (c *androidContext) Refresh() error {
    44  	c.Context.ReleaseSurface()
    45  	if err := c.win.setVisual(c.Context.VisualID()); err != nil {
    46  		return err
    47  	}
    48  	win, width, height := c.win.nativeWindow()
    49  	c.eglSurf = egl.NativeWindowType(unsafe.Pointer(win))
    50  	c.width, c.height = width, height
    51  	return nil
    52  }
    53  
    54  func (c *androidContext) Lock() error {
    55  	// The Android emulator creates a broken surface if it is not
    56  	// created on the same thread as the context is made current.
    57  	if c.eglSurf != nil {
    58  		if err := c.Context.CreateSurface(c.eglSurf, c.width, c.height); err != nil {
    59  			return err
    60  		}
    61  		c.eglSurf = nil
    62  	}
    63  	return c.Context.MakeCurrent()
    64  }
    65  
    66  func (c *androidContext) Unlock() {
    67  	c.Context.ReleaseCurrent()
    68  }