egl_android.go 1.2 KB

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