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

Go to the source code of this file.

Functions

G_BEGIN_DECLS G_DECLARE_FINAL_TYPE (FlCompositorOpenGL, fl_compositor_opengl, FL, COMPOSITOR_OPENGL, GObject) FlCompositorOpenGL *fl_compositor_opengl_new(FlOpenGLManager *opengl_manager)
 
FlOpenGLManager * fl_compositor_opengl_get_opengl_manager (FlCompositorOpenGL *compositor)
 
gboolean fl_compositor_opengl_can_fence (FlCompositorOpenGL *compositor)
 
void fl_compositor_opengl_composite_layers (FlCompositorOpenGL *compositor, const FlutterLayer **layers, size_t layers_count)
 
GLint fl_compositor_opengl_get_frame_format (const FlutterLayer **layers, size_t layers_count)
 

Function Documentation

◆ fl_compositor_opengl_can_fence()

gboolean fl_compositor_opengl_can_fence ( FlCompositorOpenGL *  compositor)

fl_compositor_opengl_can_fence: @compositor: an #FlCompositorOpenGL.

Checks whether fences can be used to synchronize a composited frame with the context that presents it. Callers have to 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 199 of file fl_compositor_opengl.cc.

199  {
200  g_return_val_if_fail(FL_IS_COMPOSITOR_OPENGL(self), FALSE);
201  return self->can_fence;
202 }

◆ fl_compositor_opengl_composite_layers()

void fl_compositor_opengl_composite_layers ( FlCompositorOpenGL *  compositor,
const FlutterLayer **  layers,
size_t  layers_count 
)

fl_compositor_opengl_composite_layers: @compositor: an #FlCompositorOpenGL. @layers: layers to be composited. @layers_count: number of layers.

Composite @layers into the OpenGL framebuffer bound to the current OpenGL context. The caller is responsible for binding the target framebuffer before calling this function.

Definition at line 85 of file fl_compositor_opengl.cc.

87  {
88  // Save bindings that are set by this function. All bindings must be restored
89  // to their original values because Skia expects that its bindings have not
90  // been altered.
91  GLint saved_texture_binding;
92  glGetIntegerv(GL_TEXTURE_BINDING_2D, &saved_texture_binding);
93  GLint saved_vao_binding;
94  glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &saved_vao_binding);
95  GLint saved_array_buffer_binding;
96  glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &saved_array_buffer_binding);
97  GLint saved_read_framebuffer_binding;
98  glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &saved_read_framebuffer_binding);
99  GLint saved_current_program;
100  glGetIntegerv(GL_CURRENT_PROGRAM, &saved_current_program);
101  GLboolean saved_scissor_test = glIsEnabled(GL_SCISSOR_TEST);
102  GLboolean saved_blend = glIsEnabled(GL_BLEND);
103  GLint saved_src_rgb;
104  glGetIntegerv(GL_BLEND_SRC_RGB, &saved_src_rgb);
105  GLint saved_src_alpha;
106  glGetIntegerv(GL_BLEND_SRC_ALPHA, &saved_src_alpha);
107  GLint saved_dst_rgb;
108  glGetIntegerv(GL_BLEND_DST_RGB, &saved_dst_rgb);
109  GLint saved_dst_alpha;
110  glGetIntegerv(GL_BLEND_DST_ALPHA, &saved_dst_alpha);
111  GLfloat saved_clear_color[4] = {0.0, 0.0, 0.0, 0.0};
112  glGetFloatv(GL_COLOR_CLEAR_VALUE, saved_clear_color);
113 
114  size_t width = layers_count > 0 ? layers[0]->size.width : 0;
115  size_t height = layers_count > 0 ? layers[0]->size.height : 0;
116 
117  // FIXME(robert-ancell): The vertex array is the same for all views, but
118  // cannot be shared in OpenGL. Find a way to not generate this every time.
119  GLuint vao;
120  glGenVertexArrays(1, &vao);
121  glBindVertexArray(vao);
122 
123  glEnable(GL_BLEND);
124  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
125 
126  fl_compositor_opengl_shader_use(self->shader);
127 
128  // Disable the scissor test as it can affect blit operations.
129  // Prevents regressions like: https://github.com/flutter/flutter/issues/140828
130  // See OpenGL specification version 4.6, section 18.3.1.
131  glDisable(GL_SCISSOR_TEST);
132 
133  glClearColor(0.0, 0.0, 0.0, 0.0);
134  glClear(GL_COLOR_BUFFER_BIT);
135 
136  gboolean first_layer = TRUE;
137  for (size_t i = 0; i < layers_count; ++i) {
138  const FlutterLayer* layer = layers[i];
139  switch (layer->type) {
140  case kFlutterLayerContentTypeBackingStore: {
141  const FlutterBackingStore* backing_store = layer->backing_store;
142  FlFramebuffer* layer_framebuffer =
143  FL_FRAMEBUFFER(backing_store->open_gl.framebuffer.user_data);
144  // The first layer can be blitted, and following layers composited with
145  // this. If glBlitFramebuffer is unavailable, composite the first layer
146  // with the shader instead.
147  if (first_layer && self->can_blit) {
148  glBindFramebuffer(GL_READ_FRAMEBUFFER,
149  fl_framebuffer_get_id(layer_framebuffer));
150  glBlitFramebuffer(layer->offset.x, layer->offset.y, layer->size.width,
151  layer->size.height, layer->offset.x,
152  layer->offset.y, layer->size.width,
153  layer->size.height, GL_COLOR_BUFFER_BIT,
154  GL_NEAREST);
155  } else {
156  composite_layer(self, layer_framebuffer, layer->offset.x,
157  layer->offset.y, width, height);
158  }
159  first_layer = FALSE;
160  } break;
161  case kFlutterLayerContentTypePlatformView: {
162  // TODO(robert-ancell) Not implemented -
163  // https://github.com/flutter/flutter/issues/41724
164  } break;
165  }
166  }
167  glFlush();
168 
169  glDeleteVertexArrays(1, &vao);
170 
171  if (saved_blend) {
172  glEnable(GL_BLEND);
173  } else {
174  glDisable(GL_BLEND);
175  }
176  if (saved_scissor_test) {
177  glEnable(GL_SCISSOR_TEST);
178  } else {
179  glDisable(GL_SCISSOR_TEST);
180  }
181 
182  glClearColor(saved_clear_color[0], saved_clear_color[1], saved_clear_color[2],
183  saved_clear_color[3]);
184  glBindTexture(GL_TEXTURE_2D, saved_texture_binding);
185  glBindVertexArray(saved_vao_binding);
186  glBindBuffer(GL_ARRAY_BUFFER, saved_array_buffer_binding);
187  glBindFramebuffer(GL_READ_FRAMEBUFFER, saved_read_framebuffer_binding);
188  glUseProgram(saved_current_program);
189  glBlendFuncSeparate(saved_src_rgb, saved_dst_rgb, saved_src_alpha,
190  saved_dst_alpha);
191 }
static void composite_layer(FlCompositorOpenGL *self, FlFramebuffer *framebuffer, double x, double y, int width, int height)
void fl_compositor_opengl_shader_use(FlCompositorOpenGLShader *self)
GLuint fl_framebuffer_get_id(FlFramebuffer *self)
const uint8_t uint32_t uint32_t * height
const uint8_t uint32_t * width
const FlutterLayer size_t layers_count
const FlutterLayer ** layers

References composite_layer(), fl_compositor_opengl_shader_use(), fl_framebuffer_get_id(), height, i, layers, layers_count, TRUE, and width.

Referenced by fl_opengl_frame_composite(), and TEST_F().

◆ fl_compositor_opengl_get_frame_format()

GLint fl_compositor_opengl_get_frame_format ( const FlutterLayer **  layers,
size_t  layers_count 
)

fl_compositor_opengl_get_frame_format: @layers: layers the frame is composited from. @layers_count: number of layers.

Gets the texture format to composite @layers into. This matches the format the engine rendered them with, so the composited frame can be read back correctly. Compositing into a mismatched format produces an empty frame.

Returns: a texture format, e.g. GL_RGBA or GL_BGRA_EXT.

Definition at line 204 of file fl_compositor_opengl.cc.

205  {
206  // Every backing store in a frame is created by the same engine code from
207  // context-wide capabilities, so they all share a format and the first one
208  // describes the frame. Layers that aren't OpenGL framebuffer backing stores,
209  // e.g. platform views, don't have a format to match; fl_engine.cc only
210  // creates framebuffers, so there is nothing else to read a format from.
211  for (size_t i = 0; i < layers_count; i++) {
212  const FlutterLayer* layer = layers[i];
213  if (layer == nullptr ||
214  layer->type != kFlutterLayerContentTypeBackingStore ||
215  layer->backing_store == nullptr ||
216  layer->backing_store->type != kFlutterBackingStoreTypeOpenGL ||
217  layer->backing_store->open_gl.type !=
218  kFlutterOpenGLTargetTypeFramebuffer) {
219  continue;
220  }
221 
222  return layer->backing_store->open_gl.framebuffer.target == GL_BGRA8_EXT
223  ? GL_BGRA_EXT
224  : GL_RGBA;
225  }
226 
227  return GL_RGBA;
228 }

References i, layers, and layers_count.

Referenced by fl_opengl_frame_composite(), and TEST_F().

◆ fl_compositor_opengl_get_opengl_manager()

FlOpenGLManager* fl_compositor_opengl_get_opengl_manager ( FlCompositorOpenGL *  compositor)

fl_compositor_opengl_get_opengl_manager: @compositor: an #FlCompositorOpenGL.

Gets the OpenGL manager the frames are composited with.

Returns: an #FlOpenGLManager.

Definition at line 193 of file fl_compositor_opengl.cc.

194  {
195  g_return_val_if_fail(FL_IS_COMPOSITOR_OPENGL(self), nullptr);
196  return self->opengl_manager;
197 }

◆ G_DECLARE_FINAL_TYPE()

G_BEGIN_DECLS G_DECLARE_FINAL_TYPE ( FlCompositorOpenGL  ,
fl_compositor_opengl  ,
FL  ,
COMPOSITOR_OPENGL  ,
GObject   
)

FlCompositorOpenGL:

#FlCompositorOpenGL is a class that implements compositing using OpenGL.

Layers are composited into the OpenGL framebuffer bound to the current OpenGL context. The caller is responsible for binding the target framebuffer and for reading the composited frame back if required. fl_compositor_opengl_new: @opengl_manager: an #FlOpenGLManager

Creates a new OpenGL compositor.

Returns: a new #FlCompositorOpenGL.