Flutter Linux Embedder
fl_subsurface_egl.h File Reference

Go to the source code of this file.

Functions

G_BEGIN_DECLS G_DECLARE_FINAL_TYPE (FlSubsurfaceEGL, fl_subsurface_egl, FL, SUBSURFACE_EGL, GObject) FlSubsurfaceEGL *fl_subsurface_egl_new(FlOpenGLManager *opengl_manager
 
void fl_subsurface_egl_resize (FlSubsurfaceEGL *egl, size_t width, size_t height)
 
void fl_subsurface_egl_present (FlSubsurfaceEGL *egl, GLuint texture_id, size_t width, size_t height, FlGLFence *fence)
 

Variables

G_BEGIN_DECLS FlSubsurface * subsurface
 
G_BEGIN_DECLS FlSubsurface size_t width
 
G_BEGIN_DECLS FlSubsurface size_t size_t height
 
G_BEGIN_DECLS FlSubsurface size_t size_t gint scale
 

Function Documentation

◆ 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)
const uint8_t uint32_t uint32_t * height
const uint8_t uint32_t * width
static EGLDisplay get_display(FlSubsurfaceEGL *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().

◆ G_DECLARE_FINAL_TYPE()

G_BEGIN_DECLS G_DECLARE_FINAL_TYPE ( FlSubsurfaceEGL  ,
fl_subsurface_egl  ,
FL  ,
SUBSURFACE_EGL  ,
GObject   
)

FlSubsurfaceEGL:

#FlSubsurfaceEGL manages the EGL context and window surface used to present engine frames onto a Wayland subsurface. Its context shares resources with the engine's render context, so the engine's frame texture can be blitted to the subsurface directly, without using EGLImage. fl_subsurface_egl_new: @opengl_manager: the #FlOpenGLManager providing the engine's EGL display and share context. @subsurface: the #FlSubsurface to present onto. @width: the surface width in logical pixels. @height: the surface height in logical pixels. @scale: the surface buffer scale.

Creates the EGL context and window surface for presenting frames onto @subsurface. A reference is kept on @subsurface, so its Wayland surface is guaranteed to outlive the EGL surface created here. If this fails a warning is printed and the returned object will fail to present frames, generating the usual EGL/OpenGL errors.

Returns: a new #FlSubsurfaceEGL.

Variable Documentation

◆ height

G_BEGIN_DECLS FlSubsurface size_t size_t height

Definition at line 52 of file fl_subsurface_egl.h.

◆ scale

◆ subsurface

G_BEGIN_DECLS FlSubsurface* subsurface

Definition at line 50 of file fl_subsurface_egl.h.

Referenced by fl_subsurface_egl_new(), and TEST_F().

◆ width

G_BEGIN_DECLS FlSubsurface size_t width

Definition at line 51 of file fl_subsurface_egl.h.