10 #include "flutter/shell/platform/embedder/embedder.h"
17 "attribute vec2 position;\n"
18 "attribute vec2 in_texcoord;\n"
19 "varying vec2 texcoord;\n"
22 " gl_Position = vec4(position, 0, 1);\n"
23 " texcoord = in_texcoord;\n"
29 "precision mediump float;\n"
32 "uniform sampler2D texture;\n"
33 "varying vec2 texcoord;\n"
36 " gl_FragColor = texture2D(texture, texcoord);\n"
69 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_length);
71 log =
static_cast<gchar*
>(g_malloc(log_length + 1));
72 glGetShaderInfoLog(shader, log_length,
nullptr, log);
82 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_length);
84 log =
static_cast<gchar*
>(g_malloc(log_length + 1));
85 glGetProgramInfoLog(program, log_length,
nullptr, log);
92 return (2.0 * position / pixels) - 1.0;
97 fl_renderer_get_instance_private(
self));
98 if (
priv->blocking_main_thread) {
99 priv->blocking_main_thread =
false;
101 FlTaskRunner* runner =
108 FlRenderer*
self = FL_RENDERER(
object);
110 fl_renderer_get_instance_private(
self));
114 g_clear_pointer(&
priv->textures, g_ptr_array_unref);
116 G_OBJECT_CLASS(fl_renderer_parent_class)->dispose(
object);
125 fl_renderer_get_instance_private(
self));
126 priv->textures = g_ptr_array_new_with_free_func(g_object_unref);
131 fl_renderer_get_instance_private(
self));
133 g_return_val_if_fail(FL_IS_RENDERER(
self), FALSE);
140 g_return_val_if_fail(FL_IS_RENDERER(
self), NULL);
142 return reinterpret_cast<void*
>(eglGetProcAddress(name));
146 g_return_if_fail(FL_IS_RENDERER(
self));
147 FL_RENDERER_GET_CLASS(
self)->make_current(
self);
151 g_return_if_fail(FL_IS_RENDERER(
self));
152 FL_RENDERER_GET_CLASS(
self)->make_resource_current(
self);
156 g_return_if_fail(FL_IS_RENDERER(
self));
157 FL_RENDERER_GET_CLASS(
self)->clear_current(
self);
161 g_return_val_if_fail(FL_IS_RENDERER(
self), -1.0);
162 return FL_RENDERER_GET_CLASS(
self)->get_refresh_rate(
self);
166 g_return_val_if_fail(FL_IS_RENDERER(
self), 0);
173 FlRenderer* renderer,
174 const FlutterBackingStoreConfig* config,
175 FlutterBackingStore* backing_store_out) {
178 FlBackingStoreProvider* provider =
181 g_warning(
"Failed to create backing store");
188 backing_store_out->type = kFlutterBackingStoreTypeOpenGL;
189 backing_store_out->open_gl.type = kFlutterOpenGLTargetTypeFramebuffer;
190 backing_store_out->open_gl.framebuffer.user_data = provider;
191 backing_store_out->open_gl.framebuffer.name = name;
192 backing_store_out->open_gl.framebuffer.target =
format;
193 backing_store_out->open_gl.framebuffer.destruction_callback = [](
void* p) {
203 const FlutterBackingStore* backing_store) {
207 g_object_unref(backing_store->open_gl.framebuffer.user_data);
215 fl_renderer_get_instance_private(
self));
217 g_return_if_fail(FL_IS_RENDERER(
self));
219 priv->target_width = target_width;
220 priv->target_height = target_height;
222 if (
priv->had_first_frame && !
priv->blocking_main_thread) {
223 priv->blocking_main_thread =
true;
224 FlTaskRunner* runner =
231 const FlutterLayer** layers,
232 size_t layers_count) {
234 fl_renderer_get_instance_private(
self));
236 g_return_val_if_fail(FL_IS_RENDERER(
self), FALSE);
240 if (
priv->blocking_main_thread && layers_count == 1 &&
241 layers[0]->offset.x == 0 && layers[0]->offset.y == 0 &&
242 (layers[0]->size.width !=
priv->target_width ||
243 layers[0]->size.height !=
priv->target_height)) {
247 priv->had_first_frame =
true;
255 g_ptr_array_set_size(
priv->textures, 0);
256 for (
size_t i = 0;
i < layers_count; ++
i) {
257 const FlutterLayer* layer = layers[
i];
258 switch (layer->type) {
259 case kFlutterLayerContentTypeBackingStore: {
260 const FlutterBackingStore* backing_store = layer->backing_store;
261 auto framebuffer = &backing_store->open_gl.framebuffer;
262 FlBackingStoreProvider* provider =
263 FL_BACKING_STORE_PROVIDER(framebuffer->user_data);
264 g_ptr_array_add(
priv->textures, g_object_ref(provider));
266 case kFlutterLayerContentTypePlatformView: {
280 fl_renderer_get_instance_private(
self));
282 g_return_if_fail(FL_IS_RENDERER(
self));
284 GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
286 glCompileShader(vertex_shader);
287 int vertex_compile_status;
288 glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &vertex_compile_status);
289 if (vertex_compile_status == GL_FALSE) {
291 g_warning(
"Failed to compile vertex shader: %s", shader_log);
294 GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
296 glCompileShader(fragment_shader);
297 int fragment_compile_status;
298 glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &fragment_compile_status);
299 if (fragment_compile_status == GL_FALSE) {
301 g_warning(
"Failed to compile fragment shader: %s", shader_log);
304 priv->program = glCreateProgram();
305 glAttachShader(
priv->program, vertex_shader);
306 glAttachShader(
priv->program, fragment_shader);
307 glLinkProgram(
priv->program);
310 glGetProgramiv(
priv->program, GL_LINK_STATUS, &link_status);
311 if (link_status == GL_FALSE) {
313 g_warning(
"Failed to link program: %s", program_log);
316 glDeleteShader(vertex_shader);
317 glDeleteShader(fragment_shader);
322 fl_renderer_get_instance_private(
self));
324 g_return_if_fail(FL_IS_RENDERER(
self));
329 GLint saved_texture_binding;
330 glGetIntegerv(GL_TEXTURE_BINDING_2D, &saved_texture_binding);
331 GLint saved_vao_binding;
332 glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &saved_vao_binding);
333 GLint saved_array_buffer_binding;
334 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &saved_array_buffer_binding);
336 glClearColor(0.0, 0.0, 0.0, 1.0);
337 glClear(GL_COLOR_BUFFER_BIT);
339 glUseProgram(
priv->program);
341 for (guint
i = 0;
i <
priv->textures->len;
i++) {
342 FlBackingStoreProvider* texture =
343 FL_BACKING_STORE_PROVIDER(g_ptr_array_index(
priv->textures,
i));
349 GdkRectangle texture_geometry =
351 GLfloat texture_x = texture_geometry.x;
352 GLfloat texture_y = texture_geometry.y;
353 GLfloat texture_width = texture_geometry.width;
354 GLfloat texture_height = texture_geometry.height;
360 GLfloat vertex_data[] = {x0, y0, 0, 0, x1, y1, 1, 1, x0, y1, 0, 1,
361 x0, y0, 0, 0, x1, y0, 1, 0, x1, y1, 1, 1};
363 GLuint vao, vertex_buffer;
364 glGenVertexArrays(1, &vao);
365 glBindVertexArray(vao);
366 glGenBuffers(1, &vertex_buffer);
367 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
368 glBufferData(GL_ARRAY_BUFFER,
sizeof(vertex_data), vertex_data,
370 GLint position_index = glGetAttribLocation(
priv->program,
"position");
371 glEnableVertexAttribArray(position_index);
372 glVertexAttribPointer(position_index, 2, GL_FLOAT, GL_FALSE,
373 sizeof(GLfloat) * 4, 0);
374 GLint texcoord_index = glGetAttribLocation(
priv->program,
"in_texcoord");
375 glEnableVertexAttribArray(texcoord_index);
376 glVertexAttribPointer(texcoord_index, 2, GL_FLOAT, GL_FALSE,
377 sizeof(GLfloat) * 4, (
void*)(
sizeof(GLfloat) * 2));
379 glDrawArrays(GL_TRIANGLES, 0, 6);
381 glDeleteVertexArrays(1, &vao);
382 glDeleteBuffers(1, &vertex_buffer);
387 glBindTexture(GL_TEXTURE_2D, saved_texture_binding);
388 glBindVertexArray(saved_vao_binding);
389 glBindBuffer(GL_ARRAY_BUFFER, saved_array_buffer_binding);
394 fl_renderer_get_instance_private(
self));
396 g_return_if_fail(FL_IS_RENDERER(
self));
398 glDeleteProgram(
priv->program);