| Flutter Linux Embedder |

fl_renderer_gl.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 "flutter/shell/platform/linux/fl_renderer_gl.h"
6
7 #include "flutter/shell/platform/linux/fl_backing_store_provider.h"
8 #include "flutter/shell/platform/linux/fl_view_private.h"
9
10 struct _FlRendererGL {
11 FlRenderer parent_instance;
12 };
13
14 G_DEFINE_TYPE(FlRendererGL, fl_renderer_gl, fl_renderer_get_type())
15
16 // Implements FlRenderer::create_contexts.
17 static gboolean fl_renderer_gl_create_contexts(FlRenderer* renderer,
18 GtkWidget* widget,
19 GdkGLContext** visible,
20 GdkGLContext** resource,
21 GError** error) {
22 GdkWindow* window = gtk_widget_get_parent_window(widget);
23
24 *visible = gdk_window_create_gl_context(window, error);
25
26 if (*error != nullptr) {
27 return FALSE;
28 }
29
30 *resource = gdk_window_create_gl_context(window, error);
31
32 if (*error != nullptr) {
33 return FALSE;
34 }
35 return TRUE;
36 }
37
38 // Implements FlRenderer::create_backing_store.
39 static gboolean fl_renderer_gl_create_backing_store(
40 FlRenderer* renderer,
41 const FlutterBackingStoreConfig* config,
42 FlutterBackingStore* backing_store_out) {
43 g_autoptr(GError) error = nullptr;
44 gboolean result = fl_renderer_make_current(renderer, &error);
45 if (!result) {
46 g_warning("Failed to make renderer current when creating backing store: %s",
47 error->message);
48 return FALSE;
49 }
50
51 FlBackingStoreProvider* provider =
52 fl_backing_store_provider_new(config->size.width, config->size.height);
53 if (!provider) {
54 g_warning("Failed to create backing store");
55 return FALSE;
56 }
57
58 uint32_t name = fl_backing_store_provider_get_gl_framebuffer_id(provider);
59 uint32_t format = fl_backing_store_provider_get_gl_format(provider);
60
61 backing_store_out->type = kFlutterBackingStoreTypeOpenGL;
62 backing_store_out->open_gl.type = kFlutterOpenGLTargetTypeFramebuffer;
63 backing_store_out->open_gl.framebuffer.user_data = provider;
64 backing_store_out->open_gl.framebuffer.name = name;
65 backing_store_out->open_gl.framebuffer.target = format;
66 backing_store_out->open_gl.framebuffer.destruction_callback = [](void* p) {
67 // Backing store destroyed in fl_renderer_gl_collect_backing_store(), set
68 // on FlutterCompositor.collect_backing_store_callback during engine start.
69 };
70
71 return TRUE;
72 }
73
74 // Implements FlRenderer::collect_backing_store.
75 static gboolean fl_renderer_gl_collect_backing_store(
76 FlRenderer* renderer,
77 const FlutterBackingStore* backing_store) {
78 g_autoptr(GError) error = nullptr;
79 gboolean result = fl_renderer_make_current(renderer, &error);
80 if (!result) {
81 g_warning(
82 "Failed to make renderer current when collecting backing store: %s",
83 error->message);
84 return FALSE;
85 }
86
87 // OpenGL context is required when destroying #FlBackingStoreProvider.
88 g_object_unref(backing_store->open_gl.framebuffer.user_data);
89 return TRUE;
90 }
91
92 // Implements FlRenderer::present_layers.
93 static gboolean fl_renderer_gl_present_layers(FlRenderer* renderer,
94 const FlutterLayer** layers,
95 size_t layers_count) {
96 FlView* view = fl_renderer_get_view(renderer);
97 GdkGLContext* context = fl_renderer_get_context(renderer);
98 if (!view || !context) {
99 return FALSE;
100 }
101
102 g_autoptr(GPtrArray) textures = g_ptr_array_new();
103 for (size_t i = 0; i < layers_count; ++i) {
104 const FlutterLayer* layer = layers[i];
105 switch (layer->type) {
106 case kFlutterLayerContentTypeBackingStore: {
107 const FlutterBackingStore* backing_store = layer->backing_store;
108 auto framebuffer = &backing_store->open_gl.framebuffer;
109 g_ptr_array_add(textures, reinterpret_cast<FlBackingStoreProvider*>(
110 framebuffer->user_data));
111 } break;
112 case kFlutterLayerContentTypePlatformView: {
113 // Currently unsupported.
114 } break;
115 }
116 }
117
118 fl_view_set_textures(view, context, textures);
119
120 return TRUE;
121 }
122
123 static void fl_renderer_gl_class_init(FlRendererGLClass* klass) {
124 FL_RENDERER_CLASS(klass)->create_contexts = fl_renderer_gl_create_contexts;
125 FL_RENDERER_CLASS(klass)->create_backing_store =
126 fl_renderer_gl_create_backing_store;
127 FL_RENDERER_CLASS(klass)->collect_backing_store =
128 fl_renderer_gl_collect_backing_store;
129 FL_RENDERER_CLASS(klass)->present_layers = fl_renderer_gl_present_layers;
130 }
131
132 static void fl_renderer_gl_init(FlRendererGL* self) {}
133
134 FlRendererGL* fl_renderer_gl_new() {
135 return FL_RENDERER_GL(g_object_new(fl_renderer_gl_get_type(), nullptr));
136 }
fl_renderer_gl_init
static void fl_renderer_gl_init(FlRendererGL *self)
Definition: fl_renderer_gl.cc:132
fl_renderer_get_view
FlView * fl_renderer_get_view(FlRenderer *self)
Definition: fl_renderer.cc:79
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
fl_renderer_gl.h
fl_backing_store_provider_new
FlBackingStoreProvider * fl_backing_store_provider_new(int width, int height)
Definition: fl_backing_store_provider.cc:35
fl_renderer_gl_create_contexts
static gboolean fl_renderer_gl_create_contexts(FlRenderer *renderer, GtkWidget *widget, GdkGLContext **visible, GdkGLContext **resource, GError **error)
Definition: fl_renderer_gl.cc:17
fl_view_private.h
_FlRendererGL::parent_instance
FlRenderer parent_instance
Definition: fl_renderer_gl.cc:11
_FlRendererGL
Definition: fl_renderer_gl.cc:10
fl_renderer_gl_create_backing_store
static gboolean fl_renderer_gl_create_backing_store(FlRenderer *renderer, const FlutterBackingStoreConfig *config, FlutterBackingStore *backing_store_out)
Definition: fl_renderer_gl.cc:39
G_DEFINE_TYPE
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
Definition: fl_basic_message_channel.cc:37
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
fl_renderer_gl_present_layers
static gboolean fl_renderer_gl_present_layers(FlRenderer *renderer, const FlutterLayer **layers, size_t layers_count)
Definition: fl_renderer_gl.cc:93
result
GAsyncResult * result
Definition: fl_text_input_plugin.cc:106
fl_renderer_gl_new
FlRendererGL * fl_renderer_gl_new()
Definition: fl_renderer_gl.cc:134
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
fl_renderer_gl_collect_backing_store
static gboolean fl_renderer_gl_collect_backing_store(FlRenderer *renderer, const FlutterBackingStore *backing_store)
Definition: fl_renderer_gl.cc:75
fl_view_set_textures
void fl_view_set_textures(FlView *self, GdkGLContext *context, GPtrArray *textures)
Definition: fl_view.cc:775
fl_backing_store_provider.h
fl_renderer_make_current
gboolean fl_renderer_make_current(FlRenderer *self, GError **error)
Definition: fl_renderer.cc:95
format
uint32_t uint32_t * format
Definition: fl_texture_registrar_test.cc:41
fl_renderer_get_context
GdkGLContext * fl_renderer_get_context(FlRenderer *self)
Definition: fl_renderer.cc:85
fl_renderer_gl_class_init
static void fl_renderer_gl_class_init(FlRendererGLClass *klass)
Definition: fl_renderer_gl.cc:123
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