Skip to content

Commit a99f373

Browse files
committed
Make an interface for FlTextureRegistrar
1 parent 3510312 commit a99f373

File tree

5 files changed

+261
-113
lines changed

5 files changed

+261
-113
lines changed

shell/platform/linux/fl_engine.cc

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
#include "flutter/shell/platform/linux/fl_binary_messenger_private.h"
1313
#include "flutter/shell/platform/linux/fl_dart_project_private.h"
1414
#include "flutter/shell/platform/linux/fl_engine_private.h"
15+
#include "flutter/shell/platform/linux/fl_pixel_buffer_texture_private.h"
1516
#include "flutter/shell/platform/linux/fl_plugin_registrar_private.h"
1617
#include "flutter/shell/platform/linux/fl_renderer.h"
1718
#include "flutter/shell/platform/linux/fl_renderer_headless.h"
1819
#include "flutter/shell/platform/linux/fl_settings_plugin.h"
20+
#include "flutter/shell/platform/linux/fl_texture_gl_private.h"
1921
#include "flutter/shell/platform/linux/fl_texture_registrar_private.h"
2022
#include "flutter/shell/platform/linux/public/flutter_linux/fl_plugin_registry.h"
2123

@@ -224,18 +226,39 @@ static bool fl_engine_gl_external_texture_frame_callback(
224226
int64_t texture_id,
225227
size_t width,
226228
size_t height,
227-
FlutterOpenGLTexture* texture) {
229+
FlutterOpenGLTexture* opengl_texture) {
228230
FlEngine* self = static_cast<FlEngine*>(user_data);
229231
if (!self->texture_registrar) {
230232
return false;
231233
}
234+
235+
FlTexture* texture =
236+
fl_texture_registrar_lookup_texture(self->texture_registrar, texture_id);
237+
if (texture == nullptr) {
238+
g_warning("Unable to find texture %" G_GINT64_FORMAT, texture_id);
239+
return false;
240+
}
241+
242+
gboolean result;
232243
g_autoptr(GError) error = nullptr;
233-
gboolean result = fl_texture_registrar_populate_gl_external_texture(
234-
self->texture_registrar, texture_id, width, height, texture, &error);
244+
if (FL_IS_TEXTURE_GL(texture)) {
245+
result = fl_texture_gl_populate(FL_TEXTURE_GL(texture), width, height,
246+
opengl_texture, &error);
247+
} else if (FL_IS_PIXEL_BUFFER_TEXTURE(texture)) {
248+
result =
249+
fl_pixel_buffer_texture_populate(FL_PIXEL_BUFFER_TEXTURE(texture),
250+
width, height, opengl_texture, &error);
251+
} else {
252+
g_warning("Unsupported texture type %" G_GINT64_FORMAT, texture_id);
253+
return false;
254+
}
255+
235256
if (!result) {
236257
g_warning("%s", error->message);
258+
return false;
237259
}
238-
return result;
260+
261+
return true;
239262
}
240263

241264
// Called by the engine to determine if it is on the GTK thread.

shell/platform/linux/fl_texture_registrar.cc

Lines changed: 97 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313
#include "flutter/shell/platform/linux/fl_texture_private.h"
1414
#include "flutter/shell/platform/linux/fl_texture_registrar_private.h"
1515

16-
struct _FlTextureRegistrar {
16+
G_DECLARE_FINAL_TYPE(FlTextureRegistrarImpl,
17+
fl_texture_registrar_impl,
18+
FL,
19+
TEXTURE_REGISTRAR_IMPL,
20+
GObject)
21+
22+
struct _FlTextureRegistrarImpl {
1723
GObject parent_instance;
1824

1925
// Weak reference to the engine this texture registrar is created for.
@@ -27,11 +33,24 @@ struct _FlTextureRegistrar {
2733
GHashTable* textures;
2834
};
2935

30-
G_DEFINE_TYPE(FlTextureRegistrar, fl_texture_registrar, G_TYPE_OBJECT)
36+
static void fl_texture_registrar_impl_iface_init(
37+
FlTextureRegistrarInterface* iface);
38+
39+
G_DEFINE_INTERFACE(FlTextureRegistrar, fl_texture_registrar, G_TYPE_OBJECT)
40+
41+
G_DEFINE_TYPE_WITH_CODE(
42+
FlTextureRegistrarImpl,
43+
fl_texture_registrar_impl,
44+
G_TYPE_OBJECT,
45+
G_IMPLEMENT_INTERFACE(fl_texture_registrar_get_type(),
46+
fl_texture_registrar_impl_iface_init))
47+
48+
static void fl_texture_registrar_default_init(
49+
FlTextureRegistrarInterface* iface) {}
3150

3251
static void engine_weak_notify_cb(gpointer user_data,
3352
GObject* where_the_object_was) {
34-
FlTextureRegistrar* self = FL_TEXTURE_REGISTRAR(user_data);
53+
FlTextureRegistrarImpl* self = FL_TEXTURE_REGISTRAR_IMPL(user_data);
3554
self->engine = nullptr;
3655

3756
// Unregister any textures.
@@ -41,8 +60,8 @@ static void engine_weak_notify_cb(gpointer user_data,
4160
g_hash_table_remove_all(textures);
4261
}
4362

44-
static void fl_texture_registrar_dispose(GObject* object) {
45-
FlTextureRegistrar* self = FL_TEXTURE_REGISTRAR(object);
63+
static void fl_texture_registrar_impl_dispose(GObject* object) {
64+
FlTextureRegistrarImpl* self = FL_TEXTURE_REGISTRAR_IMPL(object);
4665

4766
g_clear_pointer(&self->textures, g_hash_table_unref);
4867

@@ -51,23 +70,17 @@ static void fl_texture_registrar_dispose(GObject* object) {
5170
self->engine = nullptr;
5271
}
5372

54-
G_OBJECT_CLASS(fl_texture_registrar_parent_class)->dispose(object);
73+
G_OBJECT_CLASS(fl_texture_registrar_impl_parent_class)->dispose(object);
5574
}
5675

57-
static void fl_texture_registrar_class_init(FlTextureRegistrarClass* klass) {
58-
G_OBJECT_CLASS(klass)->dispose = fl_texture_registrar_dispose;
59-
}
60-
61-
static void fl_texture_registrar_init(FlTextureRegistrar* self) {
62-
self->textures = g_hash_table_new_full(g_direct_hash, g_direct_equal, nullptr,
63-
g_object_unref);
76+
static void fl_texture_registrar_impl_class_init(
77+
FlTextureRegistrarImplClass* klass) {
78+
G_OBJECT_CLASS(klass)->dispose = fl_texture_registrar_impl_dispose;
6479
}
6580

66-
G_MODULE_EXPORT gboolean
67-
fl_texture_registrar_register_texture(FlTextureRegistrar* self,
68-
FlTexture* texture) {
69-
g_return_val_if_fail(FL_IS_TEXTURE_REGISTRAR(self), FALSE);
70-
g_return_val_if_fail(FL_IS_TEXTURE(texture), FALSE);
81+
static gboolean register_texture(FlTextureRegistrar* registrar,
82+
FlTexture* texture) {
83+
FlTextureRegistrarImpl* self = FL_TEXTURE_REGISTRAR_IMPL(registrar);
7184

7285
if (FL_IS_TEXTURE_GL(texture) || FL_IS_PIXEL_BUFFER_TEXTURE(texture)) {
7386
g_hash_table_insert(self->textures,
@@ -86,17 +99,23 @@ fl_texture_registrar_register_texture(FlTextureRegistrar* self,
8699
}
87100
}
88101

89-
G_MODULE_EXPORT gboolean
90-
fl_texture_registrar_mark_texture_frame_available(FlTextureRegistrar* self,
91-
FlTexture* texture) {
92-
g_return_val_if_fail(FL_IS_TEXTURE_REGISTRAR(self), FALSE);
102+
static FlTexture* lookup_texture(FlTextureRegistrar* registrar,
103+
int64_t texture_id) {
104+
FlTextureRegistrarImpl* self = FL_TEXTURE_REGISTRAR_IMPL(registrar);
105+
return reinterpret_cast<FlTexture*>(
106+
g_hash_table_lookup(self->textures, GINT_TO_POINTER(texture_id)));
107+
}
108+
109+
static gboolean mark_texture_frame_available(FlTextureRegistrar* registrar,
110+
FlTexture* texture) {
111+
FlTextureRegistrarImpl* self = FL_TEXTURE_REGISTRAR_IMPL(registrar);
93112

94113
if (self->engine == nullptr) {
95114
return FALSE;
96115
}
97116

98-
if (fl_texture_registrar_get_texture(
99-
self, fl_texture_get_texture_id(texture)) == nullptr) {
117+
if (lookup_texture(registrar, fl_texture_get_texture_id(texture)) ==
118+
nullptr) {
100119
g_warning("Unregistered texture %p", texture);
101120
return FALSE;
102121
}
@@ -105,36 +124,9 @@ fl_texture_registrar_mark_texture_frame_available(FlTextureRegistrar* self,
105124
self->engine, fl_texture_get_texture_id(texture));
106125
}
107126

108-
gboolean fl_texture_registrar_populate_gl_external_texture(
109-
FlTextureRegistrar* self,
110-
int64_t texture_id,
111-
uint32_t width,
112-
uint32_t height,
113-
FlutterOpenGLTexture* opengl_texture,
114-
GError** error) {
115-
FlTexture* texture = fl_texture_registrar_get_texture(self, texture_id);
116-
if (texture == nullptr) {
117-
g_set_error(error, fl_engine_error_quark(), FL_ENGINE_ERROR_FAILED,
118-
"Unable to find texture %" G_GINT64_FORMAT, texture_id);
119-
return FALSE;
120-
}
121-
if (FL_IS_TEXTURE_GL(texture)) {
122-
return fl_texture_gl_populate(FL_TEXTURE_GL(texture), width, height,
123-
opengl_texture, error);
124-
} else if (FL_IS_PIXEL_BUFFER_TEXTURE(texture)) {
125-
return fl_pixel_buffer_texture_populate(
126-
FL_PIXEL_BUFFER_TEXTURE(texture), width, height, opengl_texture, error);
127-
} else {
128-
g_set_error(error, fl_engine_error_quark(), FL_ENGINE_ERROR_FAILED,
129-
"Unsupported texture type %" G_GINT64_FORMAT, texture_id);
130-
return FALSE;
131-
}
132-
}
133-
134-
G_MODULE_EXPORT gboolean
135-
fl_texture_registrar_unregister_texture(FlTextureRegistrar* self,
136-
FlTexture* texture) {
137-
g_return_val_if_fail(FL_IS_TEXTURE_REGISTRAR(self), FALSE);
127+
static gboolean unregister_texture(FlTextureRegistrar* registrar,
128+
FlTexture* texture) {
129+
FlTextureRegistrarImpl* self = FL_TEXTURE_REGISTRAR_IMPL(registrar);
138130

139131
if (!g_hash_table_remove(
140132
self->textures,
@@ -151,18 +143,62 @@ fl_texture_registrar_unregister_texture(FlTextureRegistrar* self,
151143
self->engine, fl_texture_get_texture_id(texture));
152144
}
153145

154-
FlTexture* fl_texture_registrar_get_texture(FlTextureRegistrar* registrar,
155-
int64_t texture_id) {
156-
return reinterpret_cast<FlTexture*>(
157-
g_hash_table_lookup(registrar->textures, GINT_TO_POINTER(texture_id)));
146+
static void fl_texture_registrar_impl_iface_init(
147+
FlTextureRegistrarInterface* iface) {
148+
iface->register_texture = register_texture;
149+
iface->lookup_texture = lookup_texture;
150+
iface->mark_texture_frame_available = mark_texture_frame_available;
151+
iface->unregister_texture = unregister_texture;
152+
}
153+
154+
static void fl_texture_registrar_impl_init(FlTextureRegistrarImpl* self) {
155+
self->textures = g_hash_table_new_full(g_direct_hash, g_direct_equal, nullptr,
156+
g_object_unref);
157+
}
158+
159+
G_MODULE_EXPORT gboolean
160+
fl_texture_registrar_register_texture(FlTextureRegistrar* self,
161+
FlTexture* texture) {
162+
g_return_val_if_fail(FL_IS_TEXTURE_REGISTRAR(self), FALSE);
163+
g_return_val_if_fail(FL_IS_TEXTURE(texture), FALSE);
164+
165+
return FL_TEXTURE_REGISTRAR_GET_IFACE(self)->register_texture(self, texture);
166+
}
167+
168+
FlTexture* fl_texture_registrar_lookup_texture(FlTextureRegistrar* self,
169+
int64_t texture_id) {
170+
g_return_val_if_fail(FL_IS_TEXTURE_REGISTRAR(self), NULL);
171+
172+
return FL_TEXTURE_REGISTRAR_GET_IFACE(self)->lookup_texture(self, texture_id);
173+
}
174+
175+
G_MODULE_EXPORT gboolean
176+
fl_texture_registrar_mark_texture_frame_available(FlTextureRegistrar* self,
177+
FlTexture* texture) {
178+
g_return_val_if_fail(FL_IS_TEXTURE_REGISTRAR(self), FALSE);
179+
180+
return FL_TEXTURE_REGISTRAR_GET_IFACE(self)->mark_texture_frame_available(
181+
self, texture);
182+
}
183+
184+
G_MODULE_EXPORT gboolean
185+
fl_texture_registrar_unregister_texture(FlTextureRegistrar* self,
186+
FlTexture* texture) {
187+
g_return_val_if_fail(FL_IS_TEXTURE_REGISTRAR(self), FALSE);
188+
189+
return FL_TEXTURE_REGISTRAR_GET_IFACE(self)->unregister_texture(self,
190+
texture);
158191
}
159192

160193
FlTextureRegistrar* fl_texture_registrar_new(FlEngine* engine) {
161-
FlTextureRegistrar* self = FL_TEXTURE_REGISTRAR(
162-
g_object_new(fl_texture_registrar_get_type(), nullptr));
194+
FlTextureRegistrarImpl* self = FL_TEXTURE_REGISTRAR_IMPL(
195+
g_object_new(fl_texture_registrar_impl_get_type(), nullptr));
196+
197+
// Added to stop compiler complaining about an unused function.
198+
FL_IS_TEXTURE_REGISTRAR_IMPL(self);
163199

164200
self->engine = engine;
165201
g_object_weak_ref(G_OBJECT(engine), engine_weak_notify_cb, self);
166202

167-
return self;
203+
return FL_TEXTURE_REGISTRAR(self);
168204
}

shell/platform/linux/fl_texture_registrar_private.h

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,16 @@ G_BEGIN_DECLS
2222
FlTextureRegistrar* fl_texture_registrar_new(FlEngine* engine);
2323

2424
/**
25-
* fl_texture_registrar_populate_gl_external_texture:
25+
* fl_texture_registrar_lookup_texture:
2626
* @registrar: an #FlTextureRegistrar.
2727
* @texture_id: ID of texture.
28-
* @width: width of the texture.
29-
* @height: height of the texture.
30-
* @opengl_texture: (out): return an #FlutterOpenGLTexture.
31-
* @error: (allow-none): #GError location to store the error occurring, or
32-
* %NULL to ignore.
3328
*
34-
* Attempts to populate the given @texture_id.
29+
* Looks for the texture with the given ID.
3530
*
36-
* Returns: %TRUE on success.
31+
* Returns: an #FlTexture or %NULL if no texture with this ID.
3732
*/
38-
gboolean fl_texture_registrar_populate_gl_external_texture(
39-
FlTextureRegistrar* registrar,
40-
int64_t texture_id,
41-
uint32_t width,
42-
uint32_t height,
43-
FlutterOpenGLTexture* opengl_texture,
44-
GError** error);
45-
46-
/**
47-
* fl_texture_registrar_get_texture:
48-
* @registrar: an #FlTextureRegistrar.
49-
* @texture_id: ID of texture.
50-
*
51-
* Gets a registered texture by @texture_id.
52-
*
53-
* Returns: an #FlTexture, or %NULL if not found.
54-
*/
55-
FlTexture* fl_texture_registrar_get_texture(FlTextureRegistrar* registrar,
56-
int64_t texture_id);
33+
FlTexture* fl_texture_registrar_lookup_texture(FlTextureRegistrar* registrar,
34+
int64_t texture_id);
5735

5836
G_END_DECLS
5937

0 commit comments

Comments
 (0)