Flutter Linux Embedder
fl_opengl_manager.cc File Reference
#include <epoxy/egl.h>
#include <epoxy/gl.h>
#include <gdk/gdkwayland.h>
#include <cstring>
#include "flutter/shell/platform/linux/fl_opengl_manager.h"

Go to the source code of this file.

Classes

struct  _FlOpenGLManager
 

Functions

static void fl_opengl_manager_dispose (GObject *object)
 
static void fl_opengl_manager_class_init (FlOpenGLManagerClass *klass)
 
static void fl_opengl_manager_init (FlOpenGLManager *self)
 
FlOpenGLManager * fl_opengl_manager_new ()
 
gboolean fl_opengl_manager_make_current (FlOpenGLManager *self)
 
gboolean fl_opengl_manager_make_resource_current (FlOpenGLManager *self)
 
gboolean fl_opengl_manager_make_platform_current (FlOpenGLManager *self)
 
gboolean fl_opengl_manager_clear_current (FlOpenGLManager *self)
 
EGLDisplay fl_opengl_manager_get_display (FlOpenGLManager *self)
 
EGLContext fl_opengl_manager_get_context (FlOpenGLManager *self)
 
static gboolean driver_supports_blit ()
 
gboolean fl_opengl_manager_can_blit (FlOpenGLManager *self)
 
static gboolean has_extension (FlOpenGLManager *self, const gchar *extension)
 
gboolean fl_opengl_manager_can_fence (FlOpenGLManager *self)
 
gboolean fl_opengl_manager_can_wait_sync (FlOpenGLManager *self)
 

Function Documentation

◆ driver_supports_blit()

static gboolean driver_supports_blit ( )
static

Definition at line 131 of file fl_opengl_manager.cc.

131  {
132  const gchar* vendor = reinterpret_cast<const gchar*>(glGetString(GL_VENDOR));
133  if (vendor == nullptr) {
134  return TRUE;
135  }
136 
137  // Note: List of unsupported vendors due to issue
138  // https://github.com/flutter/flutter/issues/152099
139  const char* unsupported_vendors_exact[] = {"Vivante Corporation", "ARM"};
140  const char* unsupported_vendors_fuzzy[] = {"NVIDIA"};
141 
142  for (const char* unsupported : unsupported_vendors_fuzzy) {
143  if (strstr(vendor, unsupported) != nullptr) {
144  return FALSE;
145  }
146  }
147  for (const char* unsupported : unsupported_vendors_exact) {
148  if (strcmp(vendor, unsupported) == 0) {
149  return FALSE;
150  }
151  }
152  return TRUE;
153 }

References TRUE.

Referenced by fl_opengl_manager_can_blit().

◆ fl_opengl_manager_can_blit()

gboolean fl_opengl_manager_can_blit ( FlOpenGLManager *  manager)

fl_opengl_manager_can_blit: @manager: an #FlOpenGLManager.

Checks whether glBlitFramebuffer can be used on the currently current OpenGL context. glBlitFramebuffer is a GLES3 / OpenGL 3.0 function that may be missing or broken on some drivers, so callers should fall back to shader compositing when this returns FALSE. Requires an OpenGL context to be current.

Returns: TRUE if glBlitFramebuffer is available.

Definition at line 155 of file fl_opengl_manager.cc.

155  {
156  g_return_val_if_fail(FL_IS_OPENGL_MANAGER(self), FALSE);
157 
158  // glBlitFramebuffer is a GLES3 / OpenGL 3.0 function and may not be present
159  // on older drivers, so treat it as optional. Evaluated for whichever OpenGL
160  // context is current.
161  return driver_supports_blit() &&
162  (epoxy_gl_version() >= 30 ||
163  epoxy_has_gl_extension("GL_EXT_framebuffer_blit"));
164 }
static gboolean driver_supports_blit()

References driver_supports_blit().

Referenced by fl_compositor_opengl_new(), and setup().

◆ fl_opengl_manager_can_fence()

gboolean fl_opengl_manager_can_fence ( FlOpenGLManager *  manager)

fl_opengl_manager_can_fence: @manager: an #FlOpenGLManager.

Checks whether fences can be used to synchronize rendering between OpenGL contexts. Fences are an extension that may be missing on some drivers, so callers should fall back to waiting for the rendering to complete, e.g. with glFinish(), when this returns FALSE.

Returns: TRUE if fences are available.

Definition at line 178 of file fl_opengl_manager.cc.

178  {
179  return has_extension(self, "EGL_KHR_fence_sync");
180 }
static gboolean has_extension(FlOpenGLManager *self, const gchar *extension)

References has_extension().

Referenced by fl_compositor_opengl_new().

◆ fl_opengl_manager_can_wait_sync()

gboolean fl_opengl_manager_can_wait_sync ( FlOpenGLManager *  manager)

fl_opengl_manager_can_wait_sync: @manager: an #FlOpenGLManager.

Checks whether a fence can be waited for by the GPU, i.e. without blocking the thread doing the waiting. This is an extension that may be missing on some drivers, so callers should fall back to blocking when this returns FALSE.

Returns: TRUE if fences can be waited for without blocking.

Definition at line 182 of file fl_opengl_manager.cc.

182  {
183  return has_extension(self, "EGL_KHR_wait_sync");
184 }

References has_extension().

Referenced by fl_gl_fence_new().

◆ fl_opengl_manager_class_init()

static void fl_opengl_manager_class_init ( FlOpenGLManagerClass *  klass)
static

Definition at line 45 of file fl_opengl_manager.cc.

45  {
46  G_OBJECT_CLASS(klass)->dispose = fl_opengl_manager_dispose;
47 }
static void fl_opengl_manager_dispose(GObject *object)

References fl_opengl_manager_dispose().

◆ fl_opengl_manager_clear_current()

gboolean fl_opengl_manager_clear_current ( FlOpenGLManager *  manager)

fl_opengl_manager_clear_current: @manager: an #FlOpenGLManager.

Clears the current rendering context.

Returns: TRUE if the context cleared.

Definition at line 116 of file fl_opengl_manager.cc.

116  {
117  return eglMakeCurrent(self->display, EGL_NO_SURFACE, EGL_NO_SURFACE,
118  EGL_NO_CONTEXT) == EGL_TRUE;
119 }

Referenced by fl_engine_gl_clear_current(), realize_cb(), and setup().

◆ fl_opengl_manager_dispose()

static void fl_opengl_manager_dispose ( GObject *  object)
static

Definition at line 34 of file fl_opengl_manager.cc.

34  {
35  FlOpenGLManager* self = FL_OPENGL_MANAGER(object);
36 
37  eglDestroyContext(self->display, self->render_context);
38  eglDestroyContext(self->display, self->resource_context);
39  eglDestroyContext(self->display, self->platform_context);
40  eglTerminate(self->display);
41 
42  G_OBJECT_CLASS(fl_opengl_manager_parent_class)->dispose(object);
43 }

Referenced by fl_opengl_manager_class_init().

◆ fl_opengl_manager_get_context()

EGLContext fl_opengl_manager_get_context ( FlOpenGLManager *  manager)

fl_opengl_manager_get_context: @manager: an #FlOpenGLManager.

Gets the EGL context the engine renders with. This can be used as a share context when creating additional EGL contexts so they can access textures rendered by the engine directly (without using EGLImage).

Returns: an EGLContext.

Definition at line 125 of file fl_opengl_manager.cc.

125  {
126  return self->render_context;
127 }

Referenced by setup().

◆ fl_opengl_manager_get_display()

EGLDisplay fl_opengl_manager_get_display ( FlOpenGLManager *  manager)

fl_opengl_manager_get_display: @manager: an #FlOpenGLManager.

Gets the EGL display the engine renders to. This can be used to create additional EGL contexts that share resources with the engine.

Returns: an EGLDisplay.

Definition at line 121 of file fl_opengl_manager.cc.

121  {
122  return self->display;
123 }

Referenced by fl_gl_fence_new(), and get_display().

◆ fl_opengl_manager_init()

static void fl_opengl_manager_init ( FlOpenGLManager *  self)
static

Definition at line 49 of file fl_opengl_manager.cc.

49  {
50  GdkDisplay* display = gdk_display_get_default();
51  if (GDK_IS_WAYLAND_DISPLAY(display)) {
52  self->display = eglGetPlatformDisplayEXT(
53  EGL_PLATFORM_WAYLAND_EXT, gdk_wayland_display_get_wl_display(display),
54  NULL);
55 #ifdef GDK_WINDOWING_X11
56  } else if (GDK_IS_X11_DISPLAY(display)) {
57  self->display = eglGetPlatformDisplayEXT(
58  EGL_PLATFORM_X11_EXT, gdk_x11_display_get_xdisplay(display), NULL);
59 #endif
60  } else {
61  g_critical("Unsupported GDK backend, unable to get EGL display");
62  }
63 
64  eglInitialize(self->display, nullptr, nullptr);
65 
66  const EGLint config_attributes[] = {EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8,
67  EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8,
68  EGL_DEPTH_SIZE, 8, EGL_STENCIL_SIZE, 8,
69  EGL_NONE};
70  EGLConfig config = nullptr;
71  EGLint num_config = 0;
72  eglChooseConfig(self->display, config_attributes, &config, 1, &num_config);
73 
74  const EGLint context_attributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
75 
76  self->render_context = eglCreateContext(self->display, config, EGL_NO_CONTEXT,
77  context_attributes);
78  if (self->render_context == EGL_NO_CONTEXT) {
79  g_warning("Failed to create EGL context for rendering");
80  }
81 
82  self->resource_context = eglCreateContext(
83  self->display, config, self->render_context, context_attributes);
84  if (self->resource_context == EGL_NO_CONTEXT) {
85  g_warning("Failed to create EGL context for resource sharing");
86  }
87 
88  self->platform_context = eglCreateContext(
89  self->display, config, self->render_context, context_attributes);
90  if (self->platform_context == EGL_NO_CONTEXT) {
91  g_warning("Failed to create EGL context for platform thread");
92  }
93 }

◆ fl_opengl_manager_make_current()

gboolean fl_opengl_manager_make_current ( FlOpenGLManager *  manager)

fl_opengl_manager_make_current: @manager: an #FlOpenGLManager.

Makes the rendering context current.

Returns: TRUE if the context made current.

Definition at line 101 of file fl_opengl_manager.cc.

101  {
102  return eglMakeCurrent(self->display, EGL_NO_SURFACE, EGL_NO_SURFACE,
103  self->render_context) == EGL_TRUE;
104 }

Referenced by collect_opengl_backing_store(), create_opengl_backing_store(), fl_compositor_opengl_new(), fl_engine_gl_make_current(), and fl_subsurface_egl_present().

◆ fl_opengl_manager_make_platform_current()

gboolean fl_opengl_manager_make_platform_current ( FlOpenGLManager *  manager)

fl_opengl_manager_make_platform_current: @manager: an #FlOpenGLManager.

Makes the platform rendering context current.

Returns: TRUE if the context made current.

Definition at line 111 of file fl_opengl_manager.cc.

111  {
112  return eglMakeCurrent(self->display, EGL_NO_SURFACE, EGL_NO_SURFACE,
113  self->platform_context) == EGL_TRUE;
114 }

Referenced by fl_compositor_opengl_shader_dispose(), and setup_shader().

◆ fl_opengl_manager_make_resource_current()

gboolean fl_opengl_manager_make_resource_current ( FlOpenGLManager *  manager)

fl_opengl_manager_make_resource_current: @manager: an #FlOpenGLManager.

Makes the resource rendering context current.

Returns: TRUE if the context made current.

Definition at line 106 of file fl_opengl_manager.cc.

106  {
107  return eglMakeCurrent(self->display, EGL_NO_SURFACE, EGL_NO_SURFACE,
108  self->resource_context) == EGL_TRUE;
109 }

Referenced by fl_engine_gl_make_resource_current().

◆ fl_opengl_manager_new()

FlOpenGLManager* fl_opengl_manager_new ( )

Definition at line 95 of file fl_opengl_manager.cc.

95  {
96  FlOpenGLManager* self =
97  FL_OPENGL_MANAGER(g_object_new(fl_opengl_manager_get_type(), nullptr));
98  return self;
99 }

Referenced by fl_engine_init(), FlCompositorOpenGLTest::SetUp(), FlGLFenceTest::SetUp(), FlOpenGLFrameTest::SetUp(), and FlSubsurfaceEGLTest::SetUp().

◆ has_extension()

static gboolean has_extension ( FlOpenGLManager *  self,
const gchar *  extension 
)
static

Definition at line 168 of file fl_opengl_manager.cc.

168  {
169  g_return_val_if_fail(FL_IS_OPENGL_MANAGER(self), FALSE);
170 
171  if (self->display == EGL_NO_DISPLAY) {
172  return FALSE;
173  }
174 
175  return epoxy_has_egl_extension(self->display, extension);
176 }

Referenced by fl_opengl_manager_can_fence(), and fl_opengl_manager_can_wait_sync().