Flutter Linux Embedder
fl_opengl_frame.h File Reference
#include <gtk/gtk.h>
#include "flutter/shell/platform/embedder/embedder.h"
#include "flutter/shell/platform/linux/fl_compositor_opengl.h"

Go to the source code of this file.

Functions

G_BEGIN_DECLS FlOpenGLFrame * fl_opengl_frame_new ()
 
void fl_opengl_frame_composite (FlOpenGLFrame *frame, FlCompositorOpenGL *compositor, const FlutterLayer **layers, size_t layers_count)
 
void fl_opengl_frame_get_size (FlOpenGLFrame *frame, size_t *width, size_t *height)
 
gboolean fl_opengl_frame_draw (FlOpenGLFrame *frame, cairo_t *cr, GdkWindow *window, gint scale_factor, size_t width, size_t height)
 

Function Documentation

◆ fl_opengl_frame_composite()

void fl_opengl_frame_composite ( FlOpenGLFrame *  frame,
FlCompositorOpenGL *  compositor,
const FlutterLayer **  layers,
size_t  layers_count 
)

fl_opengl_frame_composite: @frame: an #FlOpenGLFrame. @compositor: the #FlCompositorOpenGL to composite with. @layers: layers to composite. @layers_count: number of layers.

Composites @layers into the frame, (re)creating the underlying framebuffer to match the frame size and copying the result into CPU memory. Must be called with an OpenGL context current.

Definition at line 52 of file fl_opengl_frame.cc.

55  {
56  g_return_if_fail(FL_IS_OPENGL_FRAME(self));
57 
58  size_t width, height;
59  if (layers_count > 0) {
60  width = layers[0]->size.width;
61  height = layers[0]->size.height;
62  } else if (self->framebuffer != nullptr) {
63  // A frame with nothing to rasterize, e.g. everything painted was fully
64  // transparent, has no layers and so no size. Keep the current size so the
65  // frame still matches the window and is drawn (showing the view
66  // background) rather than being skipped - the contents are cleared when
67  // the layers are composited below.
68  width = fl_framebuffer_get_width(self->framebuffer);
69  height = fl_framebuffer_get_height(self->framebuffer);
70  } else {
71  // Nothing has been rendered yet, so there is nothing to clear.
72  return;
73  }
74 
75  if (width == 0 || height == 0) {
76  // A zero-sized layer has no content to show. Drop any existing frame so
77  // the renderer reports no frame rather than an empty framebuffer (a 0x0
78  // framebuffer would otherwise be treated as a valid frame while matching
79  // the "no frame" size of 0x0).
80  g_clear_object(&self->framebuffer);
81  g_clear_pointer(&self->pixels, g_free);
82  return;
83  }
84 
85  // Recreate the framebuffer if the frame size has changed.
86  if (self->framebuffer == nullptr ||
87  fl_framebuffer_get_width(self->framebuffer) != width ||
88  fl_framebuffer_get_height(self->framebuffer) != height) {
89  GLint general_format =
91  g_clear_object(&self->framebuffer);
92  self->framebuffer =
93  fl_framebuffer_new(general_format, width, height, FALSE);
94 
95  // Make a buffer to copy the frame pixels into.
96  self->pixels = g_renew(uint8_t, self->pixels, width * height * 4);
97  }
98 
99  // Bind the target framebuffer so the compositor draws into it, then read the
100  // frame back into CPU memory.
101  GLint saved_draw_framebuffer_binding;
102  glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &saved_draw_framebuffer_binding);
103  glBindFramebuffer(GL_DRAW_FRAMEBUFFER,
104  fl_framebuffer_get_id(self->framebuffer));
105 
107 
108  // GTK draws this frame on the main thread using a different OpenGL context
109  // to the one it was rendered with. No explicit synchronization is needed
110  // before that happens because glReadPixels() waits for the rendering to
111  // complete.
112  GLint saved_read_framebuffer_binding;
113  glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &saved_read_framebuffer_binding);
114  glBindFramebuffer(GL_READ_FRAMEBUFFER,
115  fl_framebuffer_get_id(self->framebuffer));
116  glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, self->pixels);
117  glBindFramebuffer(GL_READ_FRAMEBUFFER, saved_read_framebuffer_binding);
118 
119  glBindFramebuffer(GL_DRAW_FRAMEBUFFER, saved_draw_framebuffer_binding);
120 }
void fl_compositor_opengl_composite_layers(FlCompositorOpenGL *self, const FlutterLayer **layers, size_t layers_count)
GLint fl_compositor_opengl_get_frame_format(const FlutterLayer **layers, size_t layers_count)
size_t fl_framebuffer_get_height(FlFramebuffer *self)
GLuint fl_framebuffer_get_id(FlFramebuffer *self)
size_t fl_framebuffer_get_width(FlFramebuffer *self)
FlFramebuffer * fl_framebuffer_new(GLint format, size_t width, size_t height, gboolean depth_stencil)
const uint8_t uint32_t uint32_t * height
const uint8_t uint32_t * width
const FlutterLayer size_t layers_count
const FlutterLayer ** layers

References fl_compositor_opengl_composite_layers(), fl_compositor_opengl_get_frame_format(), fl_framebuffer_get_height(), fl_framebuffer_get_id(), fl_framebuffer_get_width(), fl_framebuffer_new(), height, layers, layers_count, and width.

Referenced by TEST_F().

◆ fl_opengl_frame_draw()

gboolean fl_opengl_frame_draw ( FlOpenGLFrame *  frame,
cairo_t *  cr,
GdkWindow *  window,
gint  scale_factor,
size_t  width,
size_t  height 
)

fl_opengl_frame_draw: @frame: an #FlOpenGLFrame. @cr: the Cairo context to draw into. @window: the #GdkWindow being drawn. @scale_factor: the window scale factor. @width: the width to draw in pixels. @height: the height to draw in pixels.

Draws the current frame into @cr. Must be called with an OpenGL context current.

Returns: TRUE if a frame was drawn.

Definition at line 136 of file fl_opengl_frame.cc.

141  {
142  g_return_val_if_fail(FL_IS_OPENGL_FRAME(self), FALSE);
143 
144  if (self->framebuffer == nullptr) {
145  return FALSE;
146  }
147 
148  GLint saved_texture_binding;
149  glGetIntegerv(GL_TEXTURE_BINDING_2D, &saved_texture_binding);
150 
151  GLuint texture_id;
152  glGenTextures(1, &texture_id);
153  glBindTexture(GL_TEXTURE_2D, texture_id);
154  GLsizei fb_width =
155  static_cast<GLsizei>(fl_framebuffer_get_width(self->framebuffer));
156  GLsizei fb_height =
157  static_cast<GLsizei>(fl_framebuffer_get_height(self->framebuffer));
158  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fb_width, fb_height, 0, GL_RGBA,
159  GL_UNSIGNED_BYTE, self->pixels);
160 
161  gdk_cairo_draw_from_gl(cr, window, texture_id, GL_TEXTURE, scale_factor, 0, 0,
162  width, height);
163 
164  glDeleteTextures(1, &texture_id);
165 
166  glBindTexture(GL_TEXTURE_2D, saved_texture_binding);
167 
168  glFlush();
169 
170  return TRUE;
171 }
int64_t texture_id

References fl_framebuffer_get_height(), fl_framebuffer_get_width(), height, texture_id, TRUE, and width.

Referenced by TEST_F().

◆ fl_opengl_frame_get_size()

void fl_opengl_frame_get_size ( FlOpenGLFrame *  frame,
size_t *  width,
size_t *  height 
)

fl_opengl_frame_get_size: @frame: an #FlOpenGLFrame. @width: (out): location for the width in pixels. @height: (out): location for the height in pixels.

Gets the size of the current frame in pixels. The size is zero if no frame has been composited yet.

Definition at line 122 of file fl_opengl_frame.cc.

124  {
125  g_return_if_fail(FL_IS_OPENGL_FRAME(self));
126 
127  if (self->framebuffer != nullptr) {
128  *width = fl_framebuffer_get_width(self->framebuffer);
129  *height = fl_framebuffer_get_height(self->framebuffer);
130  } else {
131  *width = 0;
132  *height = 0;
133  }
134 }

References fl_framebuffer_get_height(), fl_framebuffer_get_width(), height, and width.

Referenced by TEST_F().

◆ fl_opengl_frame_new()

G_BEGIN_DECLS FlOpenGLFrame* fl_opengl_frame_new ( )

FlOpenGLFrame:

#FlOpenGLFrame manages the OpenGL framebuffer a rendered frame is composited into and presented from.

When the frame can be shared between OpenGL contexts (EGL) the framebuffer is presented directly. Otherwise (GLX) the composited frame is copied into a CPU buffer so it can be re-uploaded in the presenting context. fl_opengl_frame_new:

Creates a new object that manages the framebuffer and CPU pixel copy a rendered frame is composited into.

Returns: a new #FlOpenGLFrame.

Definition at line 48 of file fl_opengl_frame.cc.

48  {
49  return FL_OPENGL_FRAME(g_object_new(fl_opengl_frame_get_type(), nullptr));
50 }

Referenced by TEST_F().