| Flutter Linux Embedder |

fl_backing_store_provider.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_backing_store_provider.h"
6
7 #include <epoxy/gl.h>
8
9 struct _FlBackingStoreProvider {
10 GObject parent_instance;
11
12 uint32_t framebuffer_id;
13 uint32_t texture_id;
14 GdkRectangle geometry;
15 };
16
17 G_DEFINE_TYPE(FlBackingStoreProvider, fl_backing_store_provider, G_TYPE_OBJECT)
18
19 static void fl_backing_store_provider_dispose(GObject* object) {
20 FlBackingStoreProvider* self = FL_BACKING_STORE_PROVIDER(object);
21
22 glDeleteFramebuffers(1, &self->framebuffer_id);
23 glDeleteTextures(1, &self->texture_id);
24
25 G_OBJECT_CLASS(fl_backing_store_provider_parent_class)->dispose(object);
26 }
27
28 static void fl_backing_store_provider_class_init(
29 FlBackingStoreProviderClass* klass) {
30 G_OBJECT_CLASS(klass)->dispose = fl_backing_store_provider_dispose;
31 }
32
33 static void fl_backing_store_provider_init(FlBackingStoreProvider* self) {}
34
35 FlBackingStoreProvider* fl_backing_store_provider_new(int width, int height) {
36 FlBackingStoreProvider* provider = FL_BACKING_STORE_PROVIDER(
37 g_object_new(fl_backing_store_provider_get_type(), nullptr));
38
39 provider->geometry = {
40 .x = 0,
41 .y = 0,
42 .width = width,
43 .height = height,
44 };
45
46 glGenTextures(1, &provider->texture_id);
47 glGenFramebuffers(1, &provider->framebuffer_id);
48
49 glBindFramebuffer(GL_FRAMEBUFFER, provider->framebuffer_id);
50
51 glBindTexture(GL_TEXTURE_2D, provider->texture_id);
52 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
53 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
54 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
55 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
56 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA,
57 GL_UNSIGNED_BYTE, NULL);
58 glBindTexture(GL_TEXTURE_2D, 0);
59
60 glFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
61 GL_TEXTURE_2D, provider->texture_id, 0);
62
63 return provider;
64 }
65
66 uint32_t fl_backing_store_provider_get_gl_framebuffer_id(
67 FlBackingStoreProvider* self) {
68 return self->framebuffer_id;
69 }
70
71 uint32_t fl_backing_store_provider_get_gl_texture_id(
72 FlBackingStoreProvider* self) {
73 return self->texture_id;
74 }
75
76 uint32_t fl_backing_store_provider_get_gl_target(FlBackingStoreProvider* self) {
77 return GL_TEXTURE_2D;
78 }
79
80 uint32_t fl_backing_store_provider_get_gl_format(FlBackingStoreProvider* self) {
81 // Flutter defines SK_R32_SHIFT=16, so SK_PMCOLOR_BYTE_ORDER should be BGRA.
82 // In Linux kN32_SkColorType is assumed to be kBGRA_8888_SkColorType.
83 // So we must choose a valid gl format to be compatible with surface format
84 // BGRA8.
85 // Following logic is copied from Skia GrGLCaps.cpp:
86 // https://github.com/google/skia/blob/4738ed711e03212aceec3cd502a4adb545f38e63/src/gpu/ganesh/gl/GrGLCaps.cpp#L1963-L2116
87
88 if (epoxy_is_desktop_gl()) {
89 // For OpenGL.
90 if (epoxy_gl_version() >= 12 || epoxy_has_gl_extension("GL_EXT_bgra")) {
91 return GL_RGBA8;
92 }
93 } else {
94 // For OpenGL ES.
95 if (epoxy_has_gl_extension("GL_EXT_texture_format_BGRA8888") ||
96 (epoxy_has_gl_extension("GL_APPLE_texture_format_BGRA8888") &&
97 epoxy_gl_version() >= 30)) {
98 return GL_BGRA8_EXT;
99 }
100 }
101 g_critical("Failed to determine valid GL format for Flutter rendering");
102 return GL_RGBA8;
103 }
104
105 GdkRectangle fl_backing_store_provider_get_geometry(
106 FlBackingStoreProvider* self) {
107 return self->geometry;
108 }
_FlBackingStoreProvider::geometry
GdkRectangle geometry
Definition: fl_backing_store_provider.cc:14
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_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_backing_store_provider_class_init
static void fl_backing_store_provider_class_init(FlBackingStoreProviderClass *klass)
Definition: fl_backing_store_provider.cc:28
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
_FlBackingStoreProvider::texture_id
uint32_t texture_id
Definition: fl_backing_store_provider.cc:13
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
_FlBackingStoreProvider::parent_instance
GObject parent_instance
Definition: fl_backing_store_provider.cc:10
fl_backing_store_provider_dispose
static void fl_backing_store_provider_dispose(GObject *object)
Definition: fl_backing_store_provider.cc:19
fl_backing_store_provider_get_geometry
GdkRectangle fl_backing_store_provider_get_geometry(FlBackingStoreProvider *self)
Definition: fl_backing_store_provider.cc:105
fl_backing_store_provider_get_gl_target
uint32_t fl_backing_store_provider_get_gl_target(FlBackingStoreProvider *self)
Definition: fl_backing_store_provider.cc:76
_FlBackingStoreProvider
Definition: fl_backing_store_provider.cc:9
fl_backing_store_provider.h
width
const uint8_t uint32_t * width
Definition: fl_pixel_buffer_texture_test.cc:38
_FlBackingStoreProvider::framebuffer_id
uint32_t framebuffer_id
Definition: fl_backing_store_provider.cc:12
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_backing_store_provider_init
static void fl_backing_store_provider_init(FlBackingStoreProvider *self)
Definition: fl_backing_store_provider.cc:33