github.com/cybriq/giocore@v0.0.7-0.20210703034601-cfb9cb5f3900/app/internal/wm/egl_android.go (about) 1 // SPDX-License-Identifier: Unlicense OR MIT 2 3 package wm 4 5 /* 6 #include <android/native_window_jni.h> 7 #include <EGL/egl.h> 8 */ 9 import "C" 10 11 import ( 12 "unsafe" 13 14 "github.com/cybriq/giocore/internal/egl" 15 ) 16 17 type context struct { 18 win *window 19 *egl.Context 20 } 21 22 func (w *window) NewContext() (Context, error) { 23 ctx, err := egl.NewContext(nil) 24 if err != nil { 25 return nil, err 26 } 27 return &context{win: w, Context: ctx}, nil 28 } 29 30 func (c *context) Release() { 31 if c.Context != nil { 32 c.Context.Release() 33 c.Context = nil 34 } 35 } 36 37 func (c *context) Refresh() error { 38 return nil 39 } 40 41 func (c *context) MakeCurrent() error { 42 c.Context.ReleaseSurface() 43 var ( 44 win *C.ANativeWindow 45 width, height int 46 ) 47 // Run on main thread. Deadlock is avoided because MakeCurrent is only 48 // called during a FrameEvent. 49 c.win.callbacks.Run(func() { 50 win, width, height = c.win.nativeWindow(c.Context.VisualID()) 51 }) 52 if win == nil { 53 return nil 54 } 55 eglSurf := egl.NativeWindowType(unsafe.Pointer(win)) 56 if err := c.Context.CreateSurface(eglSurf, width, height); err != nil { 57 return err 58 } 59 if err := c.Context.MakeCurrent(); err != nil { 60 return err 61 } 62 return nil 63 } 64 65 func (c *context) Lock() {} 66 67 func (c *context) Unlock() {}