Files
gio-patched/app/egl_android.go
T
Elias Naur fd008f39af app: [Android] work around broken EGL surfaces on the emulator
The Android emulator creates a broken EGL surface if it is not created
on the same thread as the context is made current.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-08-29 22:00:24 +02:00

65 lines
1.2 KiB
Go

// SPDX-License-Identifier: Unlicense OR MIT
package app
/*
#include <android/native_window_jni.h>
#include <EGL/egl.h>
*/
import "C"
import (
"unsafe"
"gioui.org/internal/egl"
)
type androidContext struct {
win *window
eglSurf egl.NativeWindowType
width, height int
*egl.Context
}
func (w *window) NewContext() (context, error) {
ctx, err := egl.NewContext(nil)
if err != nil {
return nil, err
}
return &androidContext{win: w, Context: ctx}, nil
}
func (c *androidContext) Release() {
if c.Context != nil {
c.Context.Release()
c.Context = nil
}
}
func (c *androidContext) Refresh() error {
c.Context.ReleaseSurface()
win, width, height := c.win.nativeWindow(c.Context.VisualID())
if win == nil {
return nil
}
c.eglSurf = egl.NativeWindowType(unsafe.Pointer(win))
c.width, c.height = width, height
return nil
}
func (c *androidContext) Lock() error {
// The Android emulator creates a broken surface if it is not
// created on the same thread as the context is made current.
if c.eglSurf != nil {
if err := c.Context.CreateSurface(c.eglSurf, c.width, c.height); err != nil {
return err
}
c.eglSurf = nil
}
return c.Context.MakeCurrent()
}
func (c *androidContext) Unlock() {
c.Context.ReleaseCurrent()
}