| Flutter Linux Embedder |

fl_renderer.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "fl_renderer.h"
6
7 #include <epoxy/egl.h>
8 #include <epoxy/gl.h>
9
10 #include "flutter/shell/platform/embedder/embedder.h"
11 #include "flutter/shell/platform/linux/fl_backing_store_provider.h"
12 #include "flutter/shell/platform/linux/fl_engine_private.h"
13 #include "flutter/shell/platform/linux/fl_view_private.h"
14
15 // Vertex shader to draw Flutter window contents.
16 static const char* vertex_shader_src =
17 "attribute vec2 position;\n"
18 "attribute vec2 in_texcoord;\n"
19 "varying vec2 texcoord;\n"
20 "\n"
21 "void main() {\n"
22 " gl_Position = vec4(position, 0, 1);\n"
23 " texcoord = in_texcoord;\n"
24 "}\n";
25
26 // Fragment shader to draw Flutter window contents.
27 static const char* fragment_shader_src =
28 "uniform sampler2D texture;\n"
29 "varying vec2 texcoord;\n"
30 "\n"
31 "void main() {\n"
32 " gl_FragColor = texture2D(texture, texcoord);\n"
33 "}\n";
34
35 G_DEFINE_QUARK(fl_renderer_error_quark, fl_renderer_error)
36
37 typedef struct {
38 FlView* view;
39
40 // target dimension for resizing
41 int target_width;
42 int target_height;
43
44 // whether the renderer waits for frame render
45 bool blocking_main_thread;
46
47 // true if frame was completed; resizing is not synchronized until first frame
48 // was rendered
49 bool had_first_frame;
50
51 // Shader program.
52 GLuint program;
53
54 // Textures to render.
55 GPtrArray* textures;
56 } FlRendererPrivate;
57
58 G_DEFINE_TYPE_WITH_PRIVATE(FlRenderer, fl_renderer, G_TYPE_OBJECT)
59
60 // Returns the log for the given OpenGL shader. Must be freed by the caller.
61 static gchar* get_shader_log(GLuint shader) {
62 int log_length;
63 gchar* log;
64
65 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_length);
66
67 log = static_cast<gchar*>(g_malloc(log_length + 1));
68 glGetShaderInfoLog(shader, log_length, nullptr, log);
69
70 return log;
71 }
72
73 // Returns the log for the given OpenGL program. Must be freed by the caller.
74 static gchar* get_program_log(GLuint program) {
75 int log_length;
76 gchar* log;
77
78 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_length);
79
80 log = static_cast<gchar*>(g_malloc(log_length + 1));
81 glGetProgramInfoLog(program, log_length, nullptr, log);
82
83 return log;
84 }
85
86 /// Converts a pixel co-ordinate from 0..pixels to OpenGL -1..1.
87 static GLfloat pixels_to_gl_coords(GLfloat position, GLfloat pixels) {
88 return (2.0 * position / pixels) - 1.0;
89 }
90
91 static void fl_renderer_unblock_main_thread(FlRenderer* self) {
92 FlRendererPrivate* priv = reinterpret_cast<FlRendererPrivate*>(
93 fl_renderer_get_instance_private(self));
94 if (priv->blocking_main_thread) {
95 priv->blocking_main_thread = false;
96
97 FlTaskRunner* runner =
98 fl_engine_get_task_runner(fl_view_get_engine(priv->view));
99 fl_task_runner_release_main_thread(runner);
100 }
101 }
102
103 static void fl_renderer_dispose(GObject* object) {
104 FlRenderer* self = FL_RENDERER(object);
105 FlRendererPrivate* priv = reinterpret_cast<FlRendererPrivate*>(
106 fl_renderer_get_instance_private(self));
107
108 fl_renderer_unblock_main_thread(self);
109
110 g_clear_pointer(&priv->textures, g_ptr_array_unref);
111
112 G_OBJECT_CLASS(fl_renderer_parent_class)->dispose(object);
113 }
114
115 static void fl_renderer_class_init(FlRendererClass* klass) {
116 G_OBJECT_CLASS(klass)->dispose = fl_renderer_dispose;
117 }
118
119 static void fl_renderer_init(FlRenderer* self) {
120 FlRendererPrivate* priv = reinterpret_cast<FlRendererPrivate*>(
121 fl_renderer_get_instance_private(self));
122 priv->textures = g_ptr_array_new_with_free_func(g_object_unref);
123 }
124
125 gboolean fl_renderer_start(FlRenderer* self, FlView* view) {
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 }
134
135 void* fl_renderer_get_proc_address(FlRenderer* self, const char* name) {
136 g_return_val_if_fail(FL_IS_RENDERER(self), NULL);
137
138 return reinterpret_cast<void*>(eglGetProcAddress(name));
139 }
140
141 void fl_renderer_make_current(FlRenderer* self) {
142 g_return_if_fail(FL_IS_RENDERER(self));
143 FL_RENDERER_GET_CLASS(self)->make_current(self);
144 }
145
146 void fl_renderer_make_resource_current(FlRenderer* self) {
147 g_return_if_fail(FL_IS_RENDERER(self));
148 FL_RENDERER_GET_CLASS(self)->make_resource_current(self);
149 }
150
151 void fl_renderer_clear_current(FlRenderer* self) {
152 g_return_if_fail(FL_IS_RENDERER(self));
153 FL_RENDERER_GET_CLASS(self)->clear_current(self);
154 }
155
156 guint32 fl_renderer_get_fbo(FlRenderer* self) {
157 g_return_val_if_fail(FL_IS_RENDERER(self), 0);
158
159 // There is only one frame buffer object - always return that.
160 return 0;
161 }
162
163 gboolean fl_renderer_create_backing_store(
164 FlRenderer* renderer,
165 const FlutterBackingStoreConfig* config,
166 FlutterBackingStore* backing_store_out) {
167 fl_renderer_make_current(renderer);
168
169 FlBackingStoreProvider* provider =
170 fl_backing_store_provider_new(config->size.width, config->size.height);
171 if (!provider) {
172 g_warning("Failed to create backing store");
173 return FALSE;
174 }
175
176 uint32_t name = fl_backing_store_provider_get_gl_framebuffer_id(provider);
177 uint32_t format = fl_backing_store_provider_get_gl_format(provider);
178
179 backing_store_out->type = kFlutterBackingStoreTypeOpenGL;
180 backing_store_out->open_gl.type = kFlutterOpenGLTargetTypeFramebuffer;
181 backing_store_out->open_gl.framebuffer.user_data = provider;
182 backing_store_out->open_gl.framebuffer.name = name;
183 backing_store_out->open_gl.framebuffer.target = format;
184 backing_store_out->open_gl.framebuffer.destruction_callback = [](void* p) {
185 // Backing store destroyed in fl_renderer_collect_backing_store(), set
186 // on FlutterCompositor.collect_backing_store_callback during engine start.
187 };
188
189 return TRUE;
190 }
191
192 gboolean fl_renderer_collect_backing_store(
193 FlRenderer* self,
194 const FlutterBackingStore* backing_store) {
195 fl_renderer_make_current(self);
196
197 // OpenGL context is required when destroying #FlBackingStoreProvider.
198 g_object_unref(backing_store->open_gl.framebuffer.user_data);
199 return TRUE;
200 }
201
202 void fl_renderer_wait_for_frame(FlRenderer* self,
203 int target_width,
204 int target_height) {
205 FlRendererPrivate* priv = reinterpret_cast<FlRendererPrivate*>(
206 fl_renderer_get_instance_private(self));
207
208 g_return_if_fail(FL_IS_RENDERER(self));
209
210 priv->target_width = target_width;
211 priv->target_height = target_height;
212
213 if (priv->had_first_frame && !priv->blocking_main_thread) {
214 priv->blocking_main_thread = true;
215 FlTaskRunner* runner =
216 fl_engine_get_task_runner(fl_view_get_engine(priv->view));
217 fl_task_runner_block_main_thread(runner);
218 }
219 }
220
221 gboolean fl_renderer_present_layers(FlRenderer* self,
222 const FlutterLayer** layers,
223 size_t layers_count) {
224 FlRendererPrivate* priv = reinterpret_cast<FlRendererPrivate*>(
225 fl_renderer_get_instance_private(self));
226
227 g_return_val_if_fail(FL_IS_RENDERER(self), FALSE);
228
229 // ignore incoming frame with wrong dimensions in trivial case with just one
230 // layer
231 if (priv->blocking_main_thread && layers_count == 1 &&
232 layers[0]->offset.x == 0 && layers[0]->offset.y == 0 &&
233 (layers[0]->size.width != priv->target_width ||
234 layers[0]->size.height != priv->target_height)) {
235 return true;
236 }
237
238 priv->had_first_frame = true;
239
240 fl_renderer_unblock_main_thread(self);
241
242 if (!priv->view) {
243 return FALSE;
244 }
245
246 g_ptr_array_set_size(priv->textures, 0);
247 for (size_t i = 0; i < layers_count; ++i) {
248 const FlutterLayer* layer = layers[i];
249 switch (layer->type) {
250 case kFlutterLayerContentTypeBackingStore: {
251 const FlutterBackingStore* backing_store = layer->backing_store;
252 auto framebuffer = &backing_store->open_gl.framebuffer;
253 FlBackingStoreProvider* provider =
254 FL_BACKING_STORE_PROVIDER(framebuffer->user_data);
255 g_ptr_array_add(priv->textures, g_object_ref(provider));
256 } break;
257 case kFlutterLayerContentTypePlatformView: {
258 // TODO(robert-ancell) Not implemented -
259 // https://github.com/flutter/flutter/issues/41724
260 } break;
261 }
262 }
263
264 fl_view_redraw(priv->view);
265
266 return TRUE;
267 }
268
269 void fl_renderer_setup(FlRenderer* self) {
270 FlRendererPrivate* priv = reinterpret_cast<FlRendererPrivate*>(
271 fl_renderer_get_instance_private(self));
272
273 g_return_if_fail(FL_IS_RENDERER(self));
274
275 GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
276 glShaderSource(vertex_shader, 1, &vertex_shader_src, nullptr);
277 glCompileShader(vertex_shader);
278 int vertex_compile_status;
279 glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &vertex_compile_status);
280 if (vertex_compile_status == GL_FALSE) {
281 g_autofree gchar* shader_log = get_shader_log(vertex_shader);
282 g_warning("Failed to compile vertex shader: %s", shader_log);
283 }
284
285 GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
286 glShaderSource(fragment_shader, 1, &fragment_shader_src, nullptr);
287 glCompileShader(fragment_shader);
288 int fragment_compile_status;
289 glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &fragment_compile_status);
290 if (fragment_compile_status == GL_FALSE) {
291 g_autofree gchar* shader_log = get_shader_log(fragment_shader);
292 g_warning("Failed to compile fragment shader: %s", shader_log);
293 }
294
295 priv->program = glCreateProgram();
296 glAttachShader(priv->program, vertex_shader);
297 glAttachShader(priv->program, fragment_shader);
298 glLinkProgram(priv->program);
299
300 int link_status;
301 glGetProgramiv(priv->program, GL_LINK_STATUS, &link_status);
302 if (link_status == GL_FALSE) {
303 g_autofree gchar* program_log = get_program_log(priv->program);
304 g_warning("Failed to link program: %s", program_log);
305 }
306
307 glDeleteShader(vertex_shader);
308 glDeleteShader(fragment_shader);
309 }
310
311 void fl_renderer_render(FlRenderer* self, int width, int height) {
312 FlRendererPrivate* priv = reinterpret_cast<FlRendererPrivate*>(
313 fl_renderer_get_instance_private(self));
314
315 g_return_if_fail(FL_IS_RENDERER(self));
316
317 glClearColor(0.0, 0.0, 0.0, 1.0);
318 glClear(GL_COLOR_BUFFER_BIT);
319
320 glUseProgram(priv->program);
321
322 for (guint i = 0; i < priv->textures->len; i++) {
323 FlBackingStoreProvider* texture =
324 FL_BACKING_STORE_PROVIDER(g_ptr_array_index(priv->textures, i));
325
326 uint32_t texture_id = fl_backing_store_provider_get_gl_texture_id(texture);
327 glBindTexture(GL_TEXTURE_2D, texture_id);
328
329 // Translate into OpenGL co-ordinates
330 GdkRectangle texture_geometry =
331 fl_backing_store_provider_get_geometry(texture);
332 GLfloat texture_x = texture_geometry.x;
333 GLfloat texture_y = texture_geometry.y;
334 GLfloat texture_width = texture_geometry.width;
335 GLfloat texture_height = texture_geometry.height;
336 GLfloat x0 = pixels_to_gl_coords(texture_x, width);
337 GLfloat y0 =
338 pixels_to_gl_coords(height - (texture_y + texture_height), height);
339 GLfloat x1 = pixels_to_gl_coords(texture_x + texture_width, width);
340 GLfloat y1 = pixels_to_gl_coords(height - texture_y, height);
341 GLfloat vertex_data[] = {x0, y0, 0, 0, x1, y1, 1, 1, x0, y1, 0, 1,
342 x0, y0, 0, 0, x1, y0, 1, 0, x1, y1, 1, 1};
343
344 GLuint vao, vertex_buffer;
345 glGenVertexArrays(1, &vao);
346 glBindVertexArray(vao);
347 glGenBuffers(1, &vertex_buffer);
348 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
349 glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data), vertex_data,
350 GL_STATIC_DRAW);
351 GLint position_index = glGetAttribLocation(priv->program, "position");
352 glEnableVertexAttribArray(position_index);
353 glVertexAttribPointer(position_index, 2, GL_FLOAT, GL_FALSE,
354 sizeof(GLfloat) * 4, 0);
355 GLint texcoord_index = glGetAttribLocation(priv->program, "in_texcoord");
356 glEnableVertexAttribArray(texcoord_index);
357 glVertexAttribPointer(texcoord_index, 2, GL_FLOAT, GL_FALSE,
358 sizeof(GLfloat) * 4, (void*)(sizeof(GLfloat) * 2));
359
360 glDrawArrays(GL_TRIANGLES, 0, 6);
361
362 glDeleteVertexArrays(1, &vao);
363 glDeleteBuffers(1, &vertex_buffer);
364 }
365
366 glFlush();
367 }
368
369 void fl_renderer_cleanup(FlRenderer* self) {
370 FlRendererPrivate* priv = reinterpret_cast<FlRendererPrivate*>(
371 fl_renderer_get_instance_private(self));
372
373 g_return_if_fail(FL_IS_RENDERER(self));
374
375 glDeleteProgram(priv->program);
376 }
fl_renderer_wait_for_frame
void fl_renderer_wait_for_frame(FlRenderer *self, int target_width, int target_height)
Definition: fl_renderer.cc:202
fl_renderer_error_quark
GQuark fl_renderer_error_quark(void) G_GNUC_CONST
G_DEFINE_TYPE_WITH_PRIVATE
G_DEFINE_TYPE_WITH_PRIVATE(FlTextInputPlugin, fl_text_input_plugin, G_TYPE_OBJECT) static gboolean finish_method(GObject *object
fl_renderer_init
static void fl_renderer_init(FlRenderer *self)
Definition: fl_renderer.cc:119
fl_backing_store_provider_get_gl_format
uint32_t fl_backing_store_provider_get_gl_format(FlBackingStoreProvider *self)
Definition: fl_backing_store_provider.cc:80
vertex_shader_src
static const char * vertex_shader_src
Definition: fl_renderer.cc:16
priv
FlPixelBufferTexturePrivate * priv
Definition: fl_pixel_buffer_texture.cc:30
FlRendererPrivate::target_width
int target_width
Definition: fl_renderer.cc:41
FlRendererPrivate::textures
GPtrArray * textures
Definition: fl_renderer.cc:55
fl_backing_store_provider_new
FlBackingStoreProvider * fl_backing_store_provider_new(int width, int height)
Definition: fl_backing_store_provider.cc:35
height
G_BEGIN_DECLS int height
Definition: fl_backing_store_provider.h:37
fl_renderer_clear_current
void fl_renderer_clear_current(FlRenderer *self)
Definition: fl_renderer.cc:151
fl_renderer_get_proc_address
void * fl_renderer_get_proc_address(FlRenderer *self, const char *name)
Definition: fl_renderer.cc:135
fl_view_private.h
fl_renderer_start
gboolean fl_renderer_start(FlRenderer *self, FlView *view)
Definition: fl_renderer.cc:125
FlRendererPrivate::blocking_main_thread
bool blocking_main_thread
Definition: fl_renderer.cc:45
G_DEFINE_QUARK
G_DEFINE_QUARK(fl_binary_messenger_codec_error_quark, fl_binary_messenger_codec_error) G_DECLARE_FINAL_TYPE(FlBinaryMessengerImpl
fl_renderer_class_init
static void fl_renderer_class_init(FlRendererClass *klass)
Definition: fl_renderer.cc:115
fl_backing_store_provider_get_gl_texture_id
uint32_t fl_backing_store_provider_get_gl_texture_id(FlBackingStoreProvider *self)
Definition: fl_backing_store_provider.cc:71
fragment_shader_src
static const char * fragment_shader_src
Definition: fl_renderer.cc:27
fl_renderer_collect_backing_store
gboolean fl_renderer_collect_backing_store(FlRenderer *self, const FlutterBackingStore *backing_store)
Definition: fl_renderer.cc:192
fl_renderer_dispose
static void fl_renderer_dispose(GObject *object)
Definition: fl_renderer.cc:103
fl_renderer_present_layers
gboolean fl_renderer_present_layers(FlRenderer *self, const FlutterLayer **layers, size_t layers_count)
Definition: fl_renderer.cc:221
FlRendererPrivate::view
FlView * view
Definition: fl_renderer.cc:38
fl_engine_private.h
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
fl_view_get_engine
G_MODULE_EXPORT FlEngine * fl_view_get_engine(FlView *self)
Definition: fl_view.cc:800
fl_renderer_create_backing_store
gboolean fl_renderer_create_backing_store(FlRenderer *renderer, const FlutterBackingStoreConfig *config, FlutterBackingStore *backing_store_out)
Definition: fl_renderer.cc:163
fl_renderer.h
fl_renderer_make_resource_current
void fl_renderer_make_resource_current(FlRenderer *self)
Definition: fl_renderer.cc:146
fl_view_redraw
void fl_view_redraw(FlView *self)
Definition: fl_view.cc:805
get_shader_log
static gchar * get_shader_log(GLuint shader)
Definition: fl_renderer.cc:61
fl_renderer_unblock_main_thread
static void fl_renderer_unblock_main_thread(FlRenderer *self)
Definition: fl_renderer.cc:91
fl_task_runner_release_main_thread
void fl_task_runner_release_main_thread(FlTaskRunner *self)
Definition: fl_task_runner.cc:205
fl_backing_store_provider_get_geometry
GdkRectangle fl_backing_store_provider_get_geometry(FlBackingStoreProvider *self)
Definition: fl_backing_store_provider.cc:105
fl_renderer_cleanup
void fl_renderer_cleanup(FlRenderer *self)
Definition: fl_renderer.cc:369
get_program_log
static gchar * get_program_log(GLuint program)
Definition: fl_renderer.cc:74
fl_renderer_make_current
void fl_renderer_make_current(FlRenderer *self)
Definition: fl_renderer.cc:141
fl_engine_get_task_runner
FlTaskRunner * fl_engine_get_task_runner(FlEngine *self)
Definition: fl_engine.cc:900
FlRendererPrivate::target_height
int target_height
Definition: fl_renderer.cc:42
fl_renderer_render
void fl_renderer_render(FlRenderer *self, int width, int height)
Definition: fl_renderer.cc:311
fl_backing_store_provider.h
pixels_to_gl_coords
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
texture_id
int64_t texture_id
Definition: texture_registrar_unittests.cc:24
width
const uint8_t uint32_t * width
Definition: fl_pixel_buffer_texture_test.cc:38
FlRendererPrivate
Definition: fl_renderer.cc:37
FlRendererPrivate::had_first_frame
bool had_first_frame
Definition: fl_renderer.cc:49
format
uint32_t uint32_t * format
Definition: fl_texture_registrar_test.cc:41
fl_renderer_get_fbo
guint32 fl_renderer_get_fbo(FlRenderer *self)
Definition: fl_renderer.cc:156
fl_task_runner_block_main_thread
void fl_task_runner_block_main_thread(FlTaskRunner *self)
Definition: fl_task_runner.cc:184
FlRendererPrivate::program
GLuint program
Definition: fl_renderer.cc:52
fl_backing_store_provider_get_gl_framebuffer_id
uint32_t fl_backing_store_provider_get_gl_framebuffer_id(FlBackingStoreProvider *self)
Definition: fl_backing_store_provider.cc:66
fl_renderer_setup
void fl_renderer_setup(FlRenderer *self)
Definition: fl_renderer.cc:269