Flutter Engine
The Flutter Engine
Classes | Enumerations | Functions
fl_renderer.h File Reference
#include <gtk/gtk.h>
#include "flutter/shell/platform/linux/public/flutter_linux/fl_dart_project.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_view.h"
#include "flutter/shell/platform/embedder/embedder.h"

Go to the source code of this file.

Classes

struct  _FlRendererClass
 

Enumerations

enum  FlRendererError { FL_RENDERER_ERROR_FAILED }
 

Functions

GQuark fl_renderer_error_quark (void) G_GNUC_CONST
 
gboolean fl_renderer_start (FlRenderer *renderer, FlView *view)
 
: a function name.

fl_renderer_get_proc_address: @renderer: an #FlRenderer.

Gets the rendering API function that matches the given name.

Returns: a function pointer.

void * fl_renderer_get_proc_address (FlRenderer *renderer, const char *name)
 
void fl_renderer_make_current (FlRenderer *renderer)
 
void fl_renderer_make_resource_current (FlRenderer *renderer)
 
void fl_renderer_clear_current (FlRenderer *renderer)
 
guint32 fl_renderer_get_fbo (FlRenderer *renderer)
 
gboolean fl_renderer_create_backing_store (FlRenderer *renderer, const FlutterBackingStoreConfig *config, FlutterBackingStore *backing_store_out)
 
gboolean fl_renderer_collect_backing_store (FlRenderer *renderer, const FlutterBackingStore *backing_store)
 
gboolean fl_renderer_present_layers (FlRenderer *renderer, const FlutterLayer **layers, size_t layers_count)
 
void fl_renderer_wait_for_frame (FlRenderer *renderer, int target_width, int target_height)
 
void fl_renderer_setup (FlRenderer *renderer)
 
void fl_renderer_render (FlRenderer *renderer, int width, int height)
 
void fl_renderer_cleanup (FlRenderer *renderer)
 
gdouble fl_renderer_get_refresh_rate (FlRenderer *renderer)
 

Enumeration Type Documentation

◆ FlRendererError

FlRendererError: Errors for #FlRenderer objects to set on failures.

Enumerator
FL_RENDERER_ERROR_FAILED 

Definition at line 22 of file fl_renderer.h.

22 {
23 // NOLINTBEGIN(readability-identifier-naming)
25 // NOLINTEND(readability-identifier-naming)
FlRendererError
Definition: fl_renderer.h:22
@ FL_RENDERER_ERROR_FAILED
Definition: fl_renderer.h:24

Function Documentation

◆ fl_renderer_cleanup()

void fl_renderer_cleanup ( FlRenderer *  renderer)

fl_renderer_cleanup:

Removes OpenGL resources used for rendering. Requires an active OpenGL context.

Definition at line 388 of file fl_renderer.cc.

388 {
389 FlRendererPrivate* priv = reinterpret_cast<FlRendererPrivate*>(
390 fl_renderer_get_instance_private(self));
391
392 g_return_if_fail(FL_IS_RENDERER(self));
393
394 glDeleteProgram(priv->program);
395}
FlPixelBufferTexturePrivate * priv

◆ fl_renderer_clear_current()

void fl_renderer_clear_current ( FlRenderer *  renderer)

fl_renderer_clear_current: @renderer: an #FlRenderer.

Clears the current rendering context.

Definition at line 151 of file fl_renderer.cc.

151 {
152 g_return_if_fail(FL_IS_RENDERER(self));
153 FL_RENDERER_GET_CLASS(self)->clear_current(self);
154}

◆ fl_renderer_collect_backing_store()

gboolean fl_renderer_collect_backing_store ( FlRenderer *  renderer,
const FlutterBackingStore backing_store 
)

fl_renderer_collect_backing_store: @renderer: an #FlRenderer. @backing_store: backing store to be released.

A callback invoked by the engine to release the backing store. The embedder may collect any resources associated with the backing store.

Returns TRUE if successful.

Definition at line 197 of file fl_renderer.cc.

199 {
201
202 // OpenGL context is required when destroying #FlBackingStoreProvider.
203 g_object_unref(backing_store->open_gl.framebuffer.user_data);
204 return TRUE;
205}
void fl_renderer_make_current(FlRenderer *self)
Definition: fl_renderer.cc:141
FlutterOpenGLBackingStore open_gl
The description of the OpenGL backing store.
Definition: embedder.h:1768
FlutterOpenGLFramebuffer framebuffer
Definition: embedder.h:1616
void * user_data
User data to be returned on the invocation of the destruction callback.
Definition: embedder.h:399

◆ fl_renderer_create_backing_store()

gboolean fl_renderer_create_backing_store ( FlRenderer *  renderer,
const FlutterBackingStoreConfig config,
FlutterBackingStore backing_store_out 
)

fl_renderer_create_backing_store: @renderer: an #FlRenderer. @config: backing store config. @backing_store_out: saves created backing store.

Obtain a backing store for a specific FlutterLayer.

Returns TRUE if successful.

Definition at line 168 of file fl_renderer.cc.

171 {
173
174 FlBackingStoreProvider* provider =
176 if (!provider) {
177 g_warning("Failed to create backing store");
178 return FALSE;
179 }
180
183
184 backing_store_out->type = kFlutterBackingStoreTypeOpenGL;
186 backing_store_out->open_gl.framebuffer.user_data = provider;
187 backing_store_out->open_gl.framebuffer.name = name;
188 backing_store_out->open_gl.framebuffer.target = format;
189 backing_store_out->open_gl.framebuffer.destruction_callback = [](void* p) {
190 // Backing store destroyed in fl_renderer_collect_backing_store(), set
191 // on FlutterCompositor.collect_backing_store_callback during engine start.
192 };
193
194 return TRUE;
195}
@ kFlutterOpenGLTargetTypeFramebuffer
Definition: embedder.h:306
@ kFlutterBackingStoreTypeOpenGL
Definition: embedder.h:1742
uint32_t fl_backing_store_provider_get_gl_framebuffer_id(FlBackingStoreProvider *self)
uint32_t fl_backing_store_provider_get_gl_format(FlBackingStoreProvider *self)
FlBackingStoreProvider * fl_backing_store_provider_new(int width, int height)
uint32_t uint32_t * format
return FALSE
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
FlutterSize size
The size of the render target the engine expects to render into.
Definition: embedder.h:1784
FlutterBackingStoreType type
Specifies the type of backing store.
Definition: embedder.h:1762
FlutterOpenGLTargetType type
Definition: embedder.h:1610
uint32_t name
The name of the framebuffer.
Definition: embedder.h:396
VoidCallback destruction_callback
Definition: embedder.h:403
double height
Definition: embedder.h:425
double width
Definition: embedder.h:424

◆ fl_renderer_error_quark()

GQuark fl_renderer_error_quark ( void  )

◆ fl_renderer_get_fbo()

guint32 fl_renderer_get_fbo ( FlRenderer *  renderer)

fl_renderer_get_fbo: @renderer: an #FlRenderer.

Gets the frame buffer object to render to.

Returns: a frame buffer object index.

Definition at line 161 of file fl_renderer.cc.

161 {
162 g_return_val_if_fail(FL_IS_RENDERER(self), 0);
163
164 // There is only one frame buffer object - always return that.
165 return 0;
166}

◆ fl_renderer_get_proc_address()

void * fl_renderer_get_proc_address ( FlRenderer *  renderer,
const char *  name 
)

Definition at line 135 of file fl_renderer.cc.

135 {
136 g_return_val_if_fail(FL_IS_RENDERER(self), NULL);
137
138 return reinterpret_cast<void*>(eglGetProcAddress(name));
139}

◆ fl_renderer_get_refresh_rate()

gdouble fl_renderer_get_refresh_rate ( FlRenderer *  renderer)

fl_renderer_get_refresh_rate: @renderer: an #FlRenderer.

Returns: The refresh rate of the display in Hz. If the refresh rate is not available, returns -1.0.

Definition at line 156 of file fl_renderer.cc.

156 {
157 g_return_val_if_fail(FL_IS_RENDERER(self), -1.0);
158 return FL_RENDERER_GET_CLASS(self)->get_refresh_rate(self);
159}

◆ fl_renderer_make_current()

void fl_renderer_make_current ( FlRenderer *  renderer)

fl_renderer_make_current: @renderer: an #FlRenderer.

Makes the rendering context current.

Definition at line 141 of file fl_renderer.cc.

141 {
142 g_return_if_fail(FL_IS_RENDERER(self));
143 FL_RENDERER_GET_CLASS(self)->make_current(self);
144}

◆ fl_renderer_make_resource_current()

void fl_renderer_make_resource_current ( FlRenderer *  renderer)

fl_renderer_make_resource_current: @renderer: an #FlRenderer.

Makes the resource rendering context current.

Definition at line 146 of file fl_renderer.cc.

146 {
147 g_return_if_fail(FL_IS_RENDERER(self));
148 FL_RENDERER_GET_CLASS(self)->make_resource_current(self);
149}

◆ fl_renderer_present_layers()

gboolean fl_renderer_present_layers ( FlRenderer *  renderer,
const FlutterLayer **  layers,
size_t  layers_count 
)

fl_renderer_present_layers: @renderer: an #FlRenderer. @layers: layers to be composited. @layers_count: number of layers.

Callback invoked by the engine to composite the contents of each layer onto the screen.

Returns TRUE if successful.

Definition at line 226 of file fl_renderer.cc.

228 {
229 FlRendererPrivate* priv = reinterpret_cast<FlRendererPrivate*>(
230 fl_renderer_get_instance_private(self));
231
232 g_return_val_if_fail(FL_IS_RENDERER(self), FALSE);
233
234 // ignore incoming frame with wrong dimensions in trivial case with just one
235 // layer
236 if (priv->blocking_main_thread && layers_count == 1 &&
237 layers[0]->offset.x == 0 && layers[0]->offset.y == 0 &&
238 (layers[0]->size.width != priv->target_width ||
239 layers[0]->size.height != priv->target_height)) {
240 return true;
241 }
242
243 priv->had_first_frame = true;
244
246
247 if (!priv->view) {
248 return FALSE;
249 }
250
251 g_ptr_array_set_size(priv->textures, 0);
252 for (size_t i = 0; i < layers_count; ++i) {
253 const FlutterLayer* layer = layers[i];
254 switch (layer->type) {
256 const FlutterBackingStore* backing_store = layer->backing_store;
257 auto framebuffer = &backing_store->open_gl.framebuffer;
258 FlBackingStoreProvider* provider =
259 FL_BACKING_STORE_PROVIDER(framebuffer->user_data);
260 g_ptr_array_add(priv->textures, g_object_ref(provider));
261 } break;
263 // TODO(robert-ancell) Not implemented -
264 // https://github.com/flutter/flutter/issues/41724
265 } break;
266 }
267 }
268
269 fl_view_redraw(priv->view);
270
271 return TRUE;
272}
@ kFlutterLayerContentTypePlatformView
Indicates that the contents of this layer are determined by the embedder.
Definition: embedder.h:1795
@ kFlutterLayerContentTypeBackingStore
Definition: embedder.h:1793
static void fl_renderer_unblock_main_thread(FlRenderer *self)
Definition: fl_renderer.cc:91
void fl_view_redraw(FlView *self)
Definition: fl_view.cc:840
FlutterLayerContentType type
Definition: embedder.h:1824
const FlutterBackingStore * backing_store
Definition: embedder.h:1828

◆ fl_renderer_render()

void fl_renderer_render ( FlRenderer *  renderer,
int  width,
int  height 
)

fl_renderer_render: @renderer: an #FlRenderer. @width: width of the window in pixels. @height: height of the window in pixels.

Performs OpenGL commands to render current Flutter view.

Definition at line 316 of file fl_renderer.cc.

316 {
317 FlRendererPrivate* priv = reinterpret_cast<FlRendererPrivate*>(
318 fl_renderer_get_instance_private(self));
319
320 g_return_if_fail(FL_IS_RENDERER(self));
321
322 // Save bindings that are set by this function. All bindings must be restored
323 // to their original values because Skia expects that its bindings have not
324 // been altered.
325 GLint saved_texture_binding;
326 glGetIntegerv(GL_TEXTURE_BINDING_2D, &saved_texture_binding);
327 GLint saved_vao_binding;
328 glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &saved_vao_binding);
329 GLint saved_array_buffer_binding;
330 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &saved_array_buffer_binding);
331
332 glClearColor(0.0, 0.0, 0.0, 1.0);
333 glClear(GL_COLOR_BUFFER_BIT);
334
335 glUseProgram(priv->program);
336
337 for (guint i = 0; i < priv->textures->len; i++) {
338 FlBackingStoreProvider* texture =
339 FL_BACKING_STORE_PROVIDER(g_ptr_array_index(priv->textures, i));
340
342 glBindTexture(GL_TEXTURE_2D, texture_id);
343
344 // Translate into OpenGL co-ordinates
345 GdkRectangle texture_geometry =
347 GLfloat texture_x = texture_geometry.x;
348 GLfloat texture_y = texture_geometry.y;
349 GLfloat texture_width = texture_geometry.width;
350 GLfloat texture_height = texture_geometry.height;
351 GLfloat x0 = pixels_to_gl_coords(texture_x, width);
352 GLfloat y0 =
353 pixels_to_gl_coords(height - (texture_y + texture_height), height);
354 GLfloat x1 = pixels_to_gl_coords(texture_x + texture_width, width);
355 GLfloat y1 = pixels_to_gl_coords(height - texture_y, height);
356 GLfloat vertex_data[] = {x0, y0, 0, 0, x1, y1, 1, 1, x0, y1, 0, 1,
357 x0, y0, 0, 0, x1, y0, 1, 0, x1, y1, 1, 1};
358
359 GLuint vao, vertex_buffer;
360 glGenVertexArrays(1, &vao);
361 glBindVertexArray(vao);
362 glGenBuffers(1, &vertex_buffer);
363 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
364 glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data), vertex_data,
365 GL_STATIC_DRAW);
366 GLint position_index = glGetAttribLocation(priv->program, "position");
367 glEnableVertexAttribArray(position_index);
368 glVertexAttribPointer(position_index, 2, GL_FLOAT, GL_FALSE,
369 sizeof(GLfloat) * 4, 0);
370 GLint texcoord_index = glGetAttribLocation(priv->program, "in_texcoord");
371 glEnableVertexAttribArray(texcoord_index);
372 glVertexAttribPointer(texcoord_index, 2, GL_FLOAT, GL_FALSE,
373 sizeof(GLfloat) * 4, (void*)(sizeof(GLfloat) * 2));
374
375 glDrawArrays(GL_TRIANGLES, 0, 6);
376
377 glDeleteVertexArrays(1, &vao);
378 glDeleteBuffers(1, &vertex_buffer);
379 }
380
381 glFlush();
382
383 glBindTexture(GL_TEXTURE_2D, saved_texture_binding);
384 glBindVertexArray(saved_vao_binding);
385 glBindBuffer(GL_ARRAY_BUFFER, saved_array_buffer_binding);
386}
uint32_t fl_backing_store_provider_get_gl_texture_id(FlBackingStoreProvider *self)
GdkRectangle fl_backing_store_provider_get_geometry(FlBackingStoreProvider *self)
static GLfloat pixels_to_gl_coords(GLfloat position, GLfloat pixels)
Converts a pixel co-ordinate from 0..pixels to OpenGL -1..1.
Definition: fl_renderer.cc:87
FlTexture * texture
int32_t height
int32_t width
int64_t texture_id

◆ fl_renderer_setup()

void fl_renderer_setup ( FlRenderer *  renderer)

fl_renderer_setup: @renderer: an #FlRenderer.

Creates OpenGL resources required before rendering. Requires an active OpenGL context.

Definition at line 274 of file fl_renderer.cc.

274 {
275 FlRendererPrivate* priv = reinterpret_cast<FlRendererPrivate*>(
276 fl_renderer_get_instance_private(self));
277
278 g_return_if_fail(FL_IS_RENDERER(self));
279
280 GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
281 glShaderSource(vertex_shader, 1, &vertex_shader_src, nullptr);
282 glCompileShader(vertex_shader);
283 int vertex_compile_status;
284 glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &vertex_compile_status);
285 if (vertex_compile_status == GL_FALSE) {
286 g_autofree gchar* shader_log = get_shader_log(vertex_shader);
287 g_warning("Failed to compile vertex shader: %s", shader_log);
288 }
289
290 GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
291 glShaderSource(fragment_shader, 1, &fragment_shader_src, nullptr);
292 glCompileShader(fragment_shader);
293 int fragment_compile_status;
294 glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &fragment_compile_status);
295 if (fragment_compile_status == GL_FALSE) {
296 g_autofree gchar* shader_log = get_shader_log(fragment_shader);
297 g_warning("Failed to compile fragment shader: %s", shader_log);
298 }
299
300 priv->program = glCreateProgram();
301 glAttachShader(priv->program, vertex_shader);
302 glAttachShader(priv->program, fragment_shader);
303 glLinkProgram(priv->program);
304
305 int link_status;
306 glGetProgramiv(priv->program, GL_LINK_STATUS, &link_status);
307 if (link_status == GL_FALSE) {
308 g_autofree gchar* program_log = get_program_log(priv->program);
309 g_warning("Failed to link program: %s", program_log);
310 }
311
312 glDeleteShader(vertex_shader);
313 glDeleteShader(fragment_shader);
314}
static gchar * get_shader_log(GLuint shader)
Definition: fl_renderer.cc:61
static const char * fragment_shader_src
Definition: fl_renderer.cc:27
static gchar * get_program_log(GLuint program)
Definition: fl_renderer.cc:74
static const char * vertex_shader_src
Definition: fl_renderer.cc:16

◆ fl_renderer_start()

gboolean fl_renderer_start ( FlRenderer *  renderer,
FlView *  view 
)

fl_renderer_start: @renderer: an #FlRenderer. @view: the view Flutter is renderering to.

Start the renderer.

Returns: TRUE if successfully started.

Definition at line 125 of file fl_renderer.cc.

125 {
126 FlRendererPrivate* priv = reinterpret_cast<FlRendererPrivate*>(
127 fl_renderer_get_instance_private(self));
128
129 g_return_val_if_fail(FL_IS_RENDERER(self), FALSE);
130
131 priv->view = view;
132 return TRUE;
133}

◆ fl_renderer_wait_for_frame()

void fl_renderer_wait_for_frame ( FlRenderer *  renderer,
int  target_width,
int  target_height 
)

fl_renderer_wait_for_frame: @renderer: an #FlRenderer. @target_width: width of frame being waited for @target_height: height of frame being waited for

Holds the thread until frame with requested dimensions is presented. While waiting for frame Flutter platform and raster tasks are being processed.

Definition at line 207 of file fl_renderer.cc.

209 {
210 FlRendererPrivate* priv = reinterpret_cast<FlRendererPrivate*>(
211 fl_renderer_get_instance_private(self));
212
213 g_return_if_fail(FL_IS_RENDERER(self));
214
215 priv->target_width = target_width;
216 priv->target_height = target_height;
217
218 if (priv->had_first_frame && !priv->blocking_main_thread) {
219 priv->blocking_main_thread = true;
220 FlTaskRunner* runner =
223 }
224}
FlTaskRunner * fl_engine_get_task_runner(FlEngine *self)
Definition: fl_engine.cc:921
void fl_task_runner_block_main_thread(FlTaskRunner *self)
G_MODULE_EXPORT FlEngine * fl_view_get_engine(FlView *self)
Definition: fl_view.cc:835