Flutter Linux Embedder
fl_subsurface_egl.cc File Reference
#include "flutter/shell/platform/linux/fl_subsurface_egl.h"
#include <epoxy/egl.h>
#include <epoxy/gl.h>
#include <wayland-client.h>
#include <wayland-egl.h>
#include "flutter/shell/platform/linux/fl_compositor_opengl_shader.h"

Go to the source code of this file.

Classes

struct  _FlSubsurfaceEGL
 

Functions

static size_t round_to_scale (size_t size, gint scale)
 
static EGLDisplay get_display (FlSubsurfaceEGL *self)
 
static void setup (FlSubsurfaceEGL *self, size_t width, size_t height, gint scale)
 
static void fl_subsurface_egl_dispose (GObject *object)
 
static void fl_subsurface_egl_class_init (FlSubsurfaceEGLClass *klass)
 
static void fl_subsurface_egl_init (FlSubsurfaceEGL *self)
 
FlSubsurfaceEGL * fl_subsurface_egl_new (FlOpenGLManager *opengl_manager, FlSubsurface *subsurface, size_t width, size_t height, gint scale)
 
void fl_subsurface_egl_resize (FlSubsurfaceEGL *self, size_t width, size_t height)
 
void fl_subsurface_egl_present (FlSubsurfaceEGL *self, GLuint texture_id, size_t width, size_t height, FlGLFence *fence)
 

Function Documentation

◆ fl_subsurface_egl_class_init()

static void fl_subsurface_egl_class_init ( FlSubsurfaceEGLClass *  klass)
static

Definition at line 212 of file fl_subsurface_egl.cc.

212  {
213  G_OBJECT_CLASS(klass)->dispose = fl_subsurface_egl_dispose;
214 }
static void fl_subsurface_egl_dispose(GObject *object)

References fl_subsurface_egl_dispose().

◆ fl_subsurface_egl_dispose()

static void fl_subsurface_egl_dispose ( GObject *  object)
static

Definition at line 174 of file fl_subsurface_egl.cc.

174  {
175  FlSubsurfaceEGL* self = FL_SUBSURFACE_EGL(object);
176 
177  // The EGL display is owned by the engine (same Wayland display), so only the
178  // surface and context created here are destroyed; the display must not be
179  // terminated.
180  if (self->opengl_manager != nullptr) {
181  EGLDisplay egl_display = get_display(self);
182  if (self->egl_context != EGL_NO_CONTEXT) {
183  if (self->read_framebuffer != 0) {
184  eglMakeCurrent(egl_display, self->egl_surface, self->egl_surface,
185  self->egl_context);
186  glDeleteFramebuffers(1, &self->read_framebuffer);
187  self->read_framebuffer = 0;
188  eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
189  EGL_NO_CONTEXT);
190  }
191  }
192  if (self->egl_surface != EGL_NO_SURFACE) {
193  eglDestroySurface(egl_display, self->egl_surface);
194  self->egl_surface = EGL_NO_SURFACE;
195  }
196  if (self->egl_context != EGL_NO_CONTEXT) {
197  eglDestroyContext(egl_display, self->egl_context);
198  self->egl_context = EGL_NO_CONTEXT;
199  }
200  }
201  if (self->egl_window != nullptr) {
202  wl_egl_window_destroy(self->egl_window);
203  self->egl_window = nullptr;
204  }
205  g_clear_object(&self->shader);
206  g_clear_object(&self->subsurface);
207  g_clear_object(&self->opengl_manager);
208 
209  G_OBJECT_CLASS(fl_subsurface_egl_parent_class)->dispose(object);
210 }
static EGLDisplay get_display(FlSubsurfaceEGL *self)

References get_display().

Referenced by fl_subsurface_egl_class_init().

◆ fl_subsurface_egl_init()

static void fl_subsurface_egl_init ( FlSubsurfaceEGL *  self)
static

Definition at line 216 of file fl_subsurface_egl.cc.

216  {
217  self->egl_context = EGL_NO_CONTEXT;
218  self->egl_surface = EGL_NO_SURFACE;
219 }

◆ fl_subsurface_egl_new()

FlSubsurfaceEGL* fl_subsurface_egl_new ( FlOpenGLManager *  opengl_manager,
FlSubsurface *  subsurface,
size_t  width,
size_t  height,
gint  scale 
)

Definition at line 221 of file fl_subsurface_egl.cc.

225  {
226  FlSubsurfaceEGL* self =
227  FL_SUBSURFACE_EGL(g_object_new(fl_subsurface_egl_get_type(), nullptr));
228  self->opengl_manager = FL_OPENGL_MANAGER(g_object_ref(opengl_manager));
229  self->subsurface = FL_SUBSURFACE(g_object_ref(subsurface));
230 
231  setup(self, width, height, scale);
232 
233  return self;
234 }
const uint8_t uint32_t uint32_t * height
const uint8_t uint32_t * width
static void setup(FlSubsurfaceEGL *self, size_t width, size_t height, gint scale)
G_BEGIN_DECLS FlSubsurface size_t size_t gint scale
G_BEGIN_DECLS FlSubsurface * subsurface

References height, scale, setup(), subsurface, and width.

Referenced by FlSubsurfaceEGLTest::CreateEGL().

◆ fl_subsurface_egl_present()

void fl_subsurface_egl_present ( FlSubsurfaceEGL *  egl,
GLuint  texture_id,
size_t  width,
size_t  height,
FlGLFence *  fence 
)

fl_subsurface_egl_present: @egl: an #FlSubsurfaceEGL. @texture_id: the OpenGL texture holding the frame to present. @width: the frame width in pixels. @height: the frame height in pixels. @fence: (allow-none): an #FlGLFence reached when @texture_id has finished rendering, or NULL if the rendering is known to have completed.

Blits @texture_id to the subsurface window surface and swaps buffers, then restores the engine's rendering context. @texture_id must have been rendered by the engine (or a context sharing resources with it).

The frame is presented using the subsurface's own context, so @fence is waited for once that context is current.

Definition at line 250 of file fl_subsurface_egl.cc.

254  {
255  g_return_if_fail(FL_IS_SUBSURFACE_EGL(self));
256 
257  // Present the composited frame to the subsurface window surface using the
258  // subsurface's own EGL context.
259  EGLDisplay egl_display = get_display(self);
260  eglMakeCurrent(egl_display, self->egl_surface, self->egl_surface,
261  self->egl_context);
262 
263  // Make this context wait for the frame to have finished rendering in the
264  // engine's context before it reads the texture below. This context is the one
265  // that reads the frame, so the waiting can be left to OpenGL and this thread
266  // doesn't have to block.
267  if (fence != nullptr) {
268  fl_gl_fence_wait(fence);
269  }
270 
271  size_t target_width = round_to_scale(width, self->scale);
272  size_t target_height = round_to_scale(height, self->scale);
273 
274  if (self->egl_window != nullptr && (self->window_width != target_width ||
275  self->window_height != target_height)) {
276  wl_egl_window_resize(self->egl_window, target_width, target_height, 0, 0);
277  self->window_width = target_width;
278  self->window_height = target_height;
279  }
280 
281  if (self->egl_window != nullptr && (self->buffer_width != target_width ||
282  self->buffer_height != target_height)) {
283  // The buffer a frame is drawn into is acquired when the previous frame is
284  // swapped, so resizing the window doesn't reach the buffer this frame
285  // would be drawn into. Swap that buffer away unused so this frame is drawn
286  // into one of the size it was rendered at. A window sized to its content
287  // only produces one frame at each size, so leaving this until the next
288  // frame would leave it showing a buffer that was never painted.
289  eglSwapBuffers(egl_display, self->egl_surface);
290  self->buffer_width = self->window_width;
291  self->buffer_height = self->window_height;
292  }
293 
294  // The frame and the buffer are both window sized, so their dimensions fit in
295  // a GLint and the arithmetic below can stay signed.
296  GLint frame_width = static_cast<GLint>(width);
297  GLint frame_height = static_cast<GLint>(height);
298 
299  // OpenGL puts the origin at the bottom left of the buffer but Wayland puts it
300  // at the top left, so the frame has to be written at the top of the buffer
301  // when the buffer is taller, which happens when the frame size was rounded up
302  // to a whole logical pixel above.
303  GLint dst_y0 = MAX(static_cast<GLint>(self->buffer_height) - frame_height, 0);
304 
305  // The subsurface context shares resources with the engine, so the engine's
306  // frame texture can be read directly without using EGLImage.
307  if (self->can_blit) {
308  // Attach the frame texture to a persistent framebuffer and blit it to the
309  // window surface. The framebuffer is created lazily on the first present
310  // and reused for subsequent frames.
311  if (self->read_framebuffer == 0) {
312  glGenFramebuffers(1, &self->read_framebuffer);
313  }
314  glBindFramebuffer(GL_READ_FRAMEBUFFER, self->read_framebuffer);
315  glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
316  GL_TEXTURE_2D, texture_id, 0);
317  glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
318  glBlitFramebuffer(0, 0, frame_width, frame_height, 0, dst_y0, frame_width,
319  dst_y0 + frame_height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
320  } else {
321  // glBlitFramebuffer is unavailable; draw the frame texture as a fullscreen
322  // quad with the shader instead.
323  glBindFramebuffer(GL_FRAMEBUFFER, 0);
324  glViewport(0, dst_y0, frame_width, frame_height);
325  fl_compositor_opengl_shader_use(self->shader);
326  fl_compositor_opengl_shader_set_offset(self->shader, 0, 0);
327  fl_compositor_opengl_shader_set_scale(self->shader, 1, 1);
328  glBindTexture(GL_TEXTURE_2D, texture_id);
329  glDrawArrays(GL_TRIANGLES, 0, 6);
330  }
331  eglSwapBuffers(egl_display, self->egl_surface);
332  self->buffer_width = self->window_width;
333  self->buffer_height = self->window_height;
334 
335  // Restore the engine's rendering context so the raster thread can continue
336  // rendering after this present.
337  fl_opengl_manager_make_current(self->opengl_manager);
338 }
void fl_compositor_opengl_shader_set_offset(FlCompositorOpenGLShader *self, double x, double y)
void fl_compositor_opengl_shader_set_scale(FlCompositorOpenGLShader *self, double x, double y)
void fl_compositor_opengl_shader_use(FlCompositorOpenGLShader *self)
void fl_gl_fence_wait(FlGLFence *self)
Definition: fl_gl_fence.cc:84
gboolean fl_opengl_manager_make_current(FlOpenGLManager *self)
static size_t round_to_scale(size_t size, gint scale)
int64_t texture_id

References fl_compositor_opengl_shader_set_offset(), fl_compositor_opengl_shader_set_scale(), fl_compositor_opengl_shader_use(), fl_gl_fence_wait(), fl_opengl_manager_make_current(), get_display(), height, round_to_scale(), texture_id, and width.

Referenced by TEST_F().

◆ fl_subsurface_egl_resize()

void fl_subsurface_egl_resize ( FlSubsurfaceEGL *  egl,
size_t  width,
size_t  height 
)

fl_subsurface_egl_resize: @egl: an #FlSubsurfaceEGL. @width: the new width in pixels. @height: the new height in pixels.

Resizes the native Wayland window backing the EGL surface.

Definition at line 236 of file fl_subsurface_egl.cc.

238  {
239  g_return_if_fail(FL_IS_SUBSURFACE_EGL(self));
240 
241  if (self->egl_window != nullptr) {
242  width = round_to_scale(width, self->scale);
243  height = round_to_scale(height, self->scale);
244  wl_egl_window_resize(self->egl_window, width, height, 0, 0);
245  self->window_width = width;
246  self->window_height = height;
247  }
248 }

References height, round_to_scale(), and width.

Referenced by TEST_F().

◆ get_display()

static EGLDisplay get_display ( FlSubsurfaceEGL *  self)
static

Definition at line 76 of file fl_subsurface_egl.cc.

76  {
77  return fl_opengl_manager_get_display(self->opengl_manager);
78 }
EGLDisplay fl_opengl_manager_get_display(FlOpenGLManager *self)

References fl_opengl_manager_get_display().

Referenced by fl_subsurface_egl_dispose(), fl_subsurface_egl_present(), and setup().

◆ round_to_scale()

static size_t round_to_scale ( size_t  size,
gint  scale 
)
static

Definition at line 70 of file fl_subsurface_egl.cc.

70  {
71  return ((size + scale - 1) / scale) * scale;
72 }

References scale.

Referenced by fl_subsurface_egl_present(), and fl_subsurface_egl_resize().

◆ setup()

static void setup ( FlSubsurfaceEGL *  self,
size_t  width,
size_t  height,
gint  scale 
)
static

Definition at line 83 of file fl_subsurface_egl.cc.

86  {
87  // The native window is created in device pixels below.
88  self->scale = scale;
89  self->window_width = width * scale;
90  self->window_height = height * scale;
91  self->buffer_width = self->window_width;
92  self->buffer_height = self->window_height;
93 
94  // Share the engine's EGL display and render context so the engine's frame
95  // texture can be accessed directly, without using EGLImage.
96  EGLDisplay egl_display = get_display(self);
97  if (egl_display == EGL_NO_DISPLAY) {
98  g_warning("Failed to get EGL display for subsurface");
99  return;
100  }
101 
102  static const EGLint config_attributes[] = {EGL_SURFACE_TYPE,
103  EGL_WINDOW_BIT,
104  EGL_RENDERABLE_TYPE,
105  EGL_OPENGL_ES2_BIT,
106  EGL_RED_SIZE,
107  8,
108  EGL_GREEN_SIZE,
109  8,
110  EGL_BLUE_SIZE,
111  8,
112  EGL_ALPHA_SIZE,
113  8,
114  EGL_NONE};
115  EGLConfig egl_config;
116  EGLint num_config;
117  if (!eglChooseConfig(egl_display, config_attributes, &egl_config, 1,
118  &num_config) ||
119  num_config == 0) {
120  g_warning("Failed to choose EGL config for subsurface");
121  return;
122  }
123 
124  eglBindAPI(EGL_OPENGL_ES_API);
125 
126  static const EGLint context_attributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2,
127  EGL_NONE};
128  EGLContext share_context =
129  fl_opengl_manager_get_context(self->opengl_manager);
130  self->egl_context = eglCreateContext(egl_display, egl_config, share_context,
131  context_attributes);
132  if (self->egl_context == EGL_NO_CONTEXT) {
133  g_warning("Failed to create EGL context for subsurface");
134  return;
135  }
136 
137  struct wl_surface* surface = fl_subsurface_get_surface(self->subsurface);
138  self->egl_window =
139  wl_egl_window_create(surface, width * scale, height * scale);
140  if (self->egl_window == nullptr) {
141  g_warning("Failed to create wl_egl_window for subsurface");
142  return;
143  }
144 
145  self->egl_surface = eglCreateWindowSurface(
146  egl_display, egl_config,
147  reinterpret_cast<EGLNativeWindowType>(self->egl_window), nullptr);
148  if (self->egl_surface == EGL_NO_SURFACE) {
149  g_warning("Failed to create EGL window surface for subsurface");
150  return;
151  }
152 
153  wl_surface_set_buffer_scale(surface, scale);
154 
155  eglMakeCurrent(egl_display, self->egl_surface, self->egl_surface,
156  self->egl_context);
157  eglSwapInterval(egl_display, 0);
158 
159  // Determine whether this context can use glBlitFramebuffer to copy the engine
160  // frame to the window surface; if not, fall back to compositing the frame
161  // with a shader, matching the OpenGL renderer.
162  self->can_blit = fl_opengl_manager_can_blit(self->opengl_manager);
163  eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
164 
165  if (!self->can_blit) {
166  // The shader is created in the engine's context, which the subsurface
167  // context shares, so its program and buffers are usable when the subsurface
168  // context is current.
169  self->shader = fl_compositor_opengl_shader_new(self->opengl_manager);
170  fl_opengl_manager_clear_current(self->opengl_manager);
171  }
172 }
FlCompositorOpenGLShader * fl_compositor_opengl_shader_new(FlOpenGLManager *opengl_manager)
gboolean fl_opengl_manager_clear_current(FlOpenGLManager *self)
gboolean fl_opengl_manager_can_blit(FlOpenGLManager *self)
EGLContext fl_opengl_manager_get_context(FlOpenGLManager *self)
struct wl_surface * fl_subsurface_get_surface(FlSubsurface *self)

References fl_compositor_opengl_shader_new(), fl_opengl_manager_can_blit(), fl_opengl_manager_clear_current(), fl_opengl_manager_get_context(), fl_subsurface_get_surface(), get_display(), height, scale, and width.

Referenced by fl_subsurface_egl_new().