Flutter Engine
The Flutter Engine
Functions
fl_texture_registrar.cc File Reference
#include "flutter/shell/platform/linux/public/flutter_linux/fl_texture_registrar.h"
#include <gmodule.h>
#include "flutter/shell/platform/embedder/embedder.h"
#include "flutter/shell/platform/linux/fl_engine_private.h"
#include "flutter/shell/platform/linux/fl_pixel_buffer_texture_private.h"
#include "flutter/shell/platform/linux/fl_texture_gl_private.h"
#include "flutter/shell/platform/linux/fl_texture_private.h"
#include "flutter/shell/platform/linux/fl_texture_registrar_private.h"

Go to the source code of this file.

Functions

 G_DECLARE_FINAL_TYPE (FlTextureRegistrarImpl, fl_texture_registrar_impl, FL, TEXTURE_REGISTRAR_IMPL, GObject) struct _FlTextureRegistrarImpl
 
static void fl_texture_registrar_impl_iface_init (FlTextureRegistrarInterface *iface)
 
 G_DEFINE_TYPE_WITH_CODE (FlTextureRegistrarImpl, fl_texture_registrar_impl, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE(fl_texture_registrar_get_type(), fl_texture_registrar_impl_iface_init)) static void fl_texture_registrar_default_init(FlTextureRegistrarInterface *iface)
 
static void engine_weak_notify_cb (gpointer user_data, GObject *where_the_object_was)
 
static void fl_texture_registrar_impl_dispose (GObject *object)
 
static void fl_texture_registrar_impl_class_init (FlTextureRegistrarImplClass *klass)
 
static gboolean register_texture (FlTextureRegistrar *registrar, FlTexture *texture)
 
static FlTexture * lookup_texture (FlTextureRegistrar *registrar, int64_t texture_id)
 
static gboolean mark_texture_frame_available (FlTextureRegistrar *registrar, FlTexture *texture)
 
static gboolean unregister_texture (FlTextureRegistrar *registrar, FlTexture *texture)
 
static void fl_texture_registrar_impl_init (FlTextureRegistrarImpl *self)
 
G_MODULE_EXPORT gboolean fl_texture_registrar_register_texture (FlTextureRegistrar *self, FlTexture *texture)
 
FlTexture * fl_texture_registrar_lookup_texture (FlTextureRegistrar *self, int64_t texture_id)
 
G_MODULE_EXPORT gboolean fl_texture_registrar_mark_texture_frame_available (FlTextureRegistrar *self, FlTexture *texture)
 
G_MODULE_EXPORT gboolean fl_texture_registrar_unregister_texture (FlTextureRegistrar *self, FlTexture *texture)
 
FlTextureRegistrar * fl_texture_registrar_new (FlEngine *engine)
 

Function Documentation

◆ engine_weak_notify_cb()

static void engine_weak_notify_cb ( gpointer  user_data,
GObject *  where_the_object_was 
)
static

Definition at line 57 of file fl_texture_registrar.cc.

58 {
59 FlTextureRegistrarImpl* self = FL_TEXTURE_REGISTRAR_IMPL(user_data);
60 self->engine = nullptr;
61
62 // Unregister any textures.
63 g_mutex_lock(&self->textures_mutex);
64 g_autoptr(GHashTable) textures = self->textures;
65 self->textures = g_hash_table_new_full(g_direct_hash, g_direct_equal, nullptr,
66 g_object_unref);
67 g_hash_table_remove_all(textures);
68 g_mutex_unlock(&self->textures_mutex);
69}
void * user_data

◆ fl_texture_registrar_impl_class_init()

static void fl_texture_registrar_impl_class_init ( FlTextureRegistrarImplClass *  klass)
static

Definition at line 87 of file fl_texture_registrar.cc.

88 {
89 G_OBJECT_CLASS(klass)->dispose = fl_texture_registrar_impl_dispose;
90}
static void fl_texture_registrar_impl_dispose(GObject *object)

◆ fl_texture_registrar_impl_dispose()

static void fl_texture_registrar_impl_dispose ( GObject *  object)
static

Definition at line 71 of file fl_texture_registrar.cc.

71 {
72 FlTextureRegistrarImpl* self = FL_TEXTURE_REGISTRAR_IMPL(object);
73
74 g_mutex_lock(&self->textures_mutex);
75 g_clear_pointer(&self->textures, g_hash_table_unref);
76 g_mutex_unlock(&self->textures_mutex);
77
78 if (self->engine != nullptr) {
79 g_object_weak_unref(G_OBJECT(self->engine), engine_weak_notify_cb, self);
80 self->engine = nullptr;
81 }
82 g_mutex_clear(&self->textures_mutex);
83
84 G_OBJECT_CLASS(fl_texture_registrar_impl_parent_class)->dispose(object);
85}
static void engine_weak_notify_cb(gpointer user_data, GObject *where_the_object_was)

◆ fl_texture_registrar_impl_iface_init()

static void fl_texture_registrar_impl_iface_init ( FlTextureRegistrarInterface *  iface)
static

Definition at line 166 of file fl_texture_registrar.cc.

167 {
168 iface->register_texture = register_texture;
169 iface->lookup_texture = lookup_texture;
170 iface->mark_texture_frame_available = mark_texture_frame_available;
171 iface->unregister_texture = unregister_texture;
172}
static gboolean register_texture(FlTextureRegistrar *registrar, FlTexture *texture)
static gboolean mark_texture_frame_available(FlTextureRegistrar *registrar, FlTexture *texture)
static FlTexture * lookup_texture(FlTextureRegistrar *registrar, int64_t texture_id)
static gboolean unregister_texture(FlTextureRegistrar *registrar, FlTexture *texture)

◆ fl_texture_registrar_impl_init()

static void fl_texture_registrar_impl_init ( FlTextureRegistrarImpl *  self)
static

Definition at line 174 of file fl_texture_registrar.cc.

174 {
175 self->next_id = 1;
176 self->textures = g_hash_table_new_full(g_direct_hash, g_direct_equal, nullptr,
177 g_object_unref);
178 // Initialize the mutex for textures.
179 g_mutex_init(&self->textures_mutex);
180}

◆ fl_texture_registrar_lookup_texture()

FlTexture * fl_texture_registrar_lookup_texture ( FlTextureRegistrar *  registrar,
int64_t  texture_id 
)

fl_texture_registrar_lookup_texture: @registrar: an #FlTextureRegistrar. @texture_id: ID of texture.

Looks for the texture with the given ID.

Returns: an #FlTexture or NULL if no texture with this ID.

Definition at line 191 of file fl_texture_registrar.cc.

192 {
193 g_return_val_if_fail(FL_IS_TEXTURE_REGISTRAR(self), NULL);
194
195 return FL_TEXTURE_REGISTRAR_GET_IFACE(self)->lookup_texture(self, texture_id);
196}
int64_t texture_id

◆ fl_texture_registrar_mark_texture_frame_available()

G_MODULE_EXPORT gboolean fl_texture_registrar_mark_texture_frame_available ( FlTextureRegistrar *  registrar,
FlTexture *  texture 
)

fl_texture_registrar_mark_texture_frame_available: @registrar: an #FlTextureRegistrar. @texture: the texture that has a frame available.

Notifies the flutter engine that the texture object has updated and needs to be rerendered.

Returns: TRUE on success.

Definition at line 198 of file fl_texture_registrar.cc.

200 {
201 g_return_val_if_fail(FL_IS_TEXTURE_REGISTRAR(self), FALSE);
202
203 return FL_TEXTURE_REGISTRAR_GET_IFACE(self)->mark_texture_frame_available(
204 self, texture);
205}
return FALSE
FlTexture * texture

◆ fl_texture_registrar_new()

FlTextureRegistrar * fl_texture_registrar_new ( FlEngine *  engine)

fl_texture_registrar_new: @engine: an #FlEngine.

Creates a new #FlTextureRegistrar.

Returns: a new #FlTextureRegistrar.

Definition at line 216 of file fl_texture_registrar.cc.

216 {
217 FlTextureRegistrarImpl* self = FL_TEXTURE_REGISTRAR_IMPL(
218 g_object_new(fl_texture_registrar_impl_get_type(), nullptr));
219
220 // Added to stop compiler complaining about an unused function.
221 FL_IS_TEXTURE_REGISTRAR_IMPL(self);
222
223 self->engine = engine;
224 g_object_weak_ref(G_OBJECT(engine), engine_weak_notify_cb, self);
225
226 return FL_TEXTURE_REGISTRAR(self);
227}
FlutterEngine engine
Definition: main.cc:68

◆ fl_texture_registrar_register_texture()

G_MODULE_EXPORT gboolean fl_texture_registrar_register_texture ( FlTextureRegistrar *  registrar,
FlTexture *  texture 
)

FlTextureRegistrar:

#FlTextureRegistrar is used when registering textures.

Flutter Framework accesses your texture by the related unique texture ID. To draw your texture in Dart, you should add Texture widget in your widget tree with the same texture ID. Use platform channels to send this unique texture ID to the Dart side. fl_texture_registrar_register_texture: @registrar: an #FlTextureRegistrar. @texture: an #FlTexture for registration.

Registers a texture.

Returns: TRUE on success.

Definition at line 182 of file fl_texture_registrar.cc.

184 {
185 g_return_val_if_fail(FL_IS_TEXTURE_REGISTRAR(self), FALSE);
186 g_return_val_if_fail(FL_IS_TEXTURE(texture), FALSE);
187
188 return FL_TEXTURE_REGISTRAR_GET_IFACE(self)->register_texture(self, texture);
189}

◆ fl_texture_registrar_unregister_texture()

G_MODULE_EXPORT gboolean fl_texture_registrar_unregister_texture ( FlTextureRegistrar *  registrar,
FlTexture *  texture 
)

fl_texture_registrar_unregister_texture: @registrar: an #FlTextureRegistrar. @texture: the texture being unregistered.

Unregisters an existing texture object.

Returns: TRUE on success.

Definition at line 207 of file fl_texture_registrar.cc.

209 {
210 g_return_val_if_fail(FL_IS_TEXTURE_REGISTRAR(self), FALSE);
211
212 return FL_TEXTURE_REGISTRAR_GET_IFACE(self)->unregister_texture(self,
213 texture);
214}

◆ G_DECLARE_FINAL_TYPE()

G_DECLARE_FINAL_TYPE ( FlTextureRegistrarImpl  ,
fl_texture_registrar_impl  ,
FL  ,
TEXTURE_REGISTRAR_IMPL  ,
GObject   
)

Definition at line 16 of file fl_texture_registrar.cc.

22 {
23 GObject parent_instance;
24
25 // Weak reference to the engine this texture registrar is created for.
26 FlEngine* engine;
27
28 // ID to assign to the next new texture.
29 int64_t next_id;
30
31 // Internal record for registered textures.
32 //
33 // It is a map from Flutter texture ID to #FlTexture instance created by
34 // plugins. The keys are directly stored int64s. The values are stored
35 // pointer to #FlTexture. This table is freed by the responder.
36 GHashTable* textures;
37
38 // The mutex guard to make `textures` thread-safe.
39 GMutex textures_mutex;
40};
static uint32_t next_id()
Definition: SkTextBlob.cpp:146

◆ G_DEFINE_TYPE_WITH_CODE()

G_DEFINE_TYPE_WITH_CODE ( FlTextureRegistrarImpl  ,
fl_texture_registrar_impl  ,
G_TYPE_OBJECT  ,
G_IMPLEMENT_INTERFACE(fl_texture_registrar_get_type(), fl_texture_registrar_impl_iface_init  
)

Definition at line 47 of file fl_texture_registrar.cc.

55 {}

◆ lookup_texture()

static FlTexture * lookup_texture ( FlTextureRegistrar *  registrar,
int64_t  texture_id 
)
static

Definition at line 123 of file fl_texture_registrar.cc.

124 {
125 FlTextureRegistrarImpl* self = FL_TEXTURE_REGISTRAR_IMPL(registrar);
126 g_mutex_lock(&self->textures_mutex);
127 FlTexture* texture = reinterpret_cast<FlTexture*>(
128 g_hash_table_lookup(self->textures, GINT_TO_POINTER(texture_id)));
129 g_mutex_unlock(&self->textures_mutex);
130 return texture;
131}

◆ mark_texture_frame_available()

static gboolean mark_texture_frame_available ( FlTextureRegistrar *  registrar,
FlTexture *  texture 
)
static

Definition at line 133 of file fl_texture_registrar.cc.

134 {
135 FlTextureRegistrarImpl* self = FL_TEXTURE_REGISTRAR_IMPL(registrar);
136
137 if (self->engine == nullptr) {
138 return FALSE;
139 }
140
143}
gboolean fl_engine_mark_texture_frame_available(FlEngine *self, int64_t texture_id)
Definition: fl_engine.cc:894
G_MODULE_EXPORT int64_t fl_texture_get_id(FlTexture *self)
Definition: fl_texture.cc:20

◆ register_texture()

static gboolean register_texture ( FlTextureRegistrar *  registrar,
FlTexture *  texture 
)
static

Definition at line 92 of file fl_texture_registrar.cc.

93 {
94 FlTextureRegistrarImpl* self = FL_TEXTURE_REGISTRAR_IMPL(registrar);
95
96 if (FL_IS_TEXTURE_GL(texture) || FL_IS_PIXEL_BUFFER_TEXTURE(texture)) {
97 if (self->engine == nullptr) {
98 return FALSE;
99 }
100
101 // We ideally would use numeric IDs, but for backwards compatibility with
102 // existing code use the address of the texture. Once all code uses
103 // fl_texture_get_id we can re-enable this method. See
104 // https://github.com/flutter/flutter/issues/124009 int64_t id =
105 // self->next_id++;
106 int64_t id = reinterpret_cast<int64_t>(texture);
107 if (fl_engine_register_external_texture(self->engine, id)) {
109 g_mutex_lock(&self->textures_mutex);
110 g_hash_table_insert(self->textures, GINT_TO_POINTER(id),
111 g_object_ref(texture));
112 g_mutex_unlock(&self->textures_mutex);
113 return TRUE;
114 } else {
115 return FALSE;
116 }
117 } else {
118 // We currently only support #FlTextureGL and #FlPixelBufferTexture.
119 return FALSE;
120 }
121}
gboolean fl_engine_register_external_texture(FlEngine *self, int64_t texture_id)
Definition: fl_engine.cc:901
void fl_texture_set_id(FlTexture *self, int64_t id)
Definition: fl_texture.cc:15

◆ unregister_texture()

static gboolean unregister_texture ( FlTextureRegistrar *  registrar,
FlTexture *  texture 
)
static

Definition at line 145 of file fl_texture_registrar.cc.

146 {
147 FlTextureRegistrarImpl* self = FL_TEXTURE_REGISTRAR_IMPL(registrar);
148
149 if (self->engine == nullptr) {
150 return FALSE;
151 }
152
154 self->engine, fl_texture_get_id(texture));
155
156 g_mutex_lock(&self->textures_mutex);
157 if (!g_hash_table_remove(self->textures,
158 GINT_TO_POINTER(fl_texture_get_id(texture)))) {
159 g_warning("Unregistering a non-existent texture %p", texture);
160 }
161 g_mutex_unlock(&self->textures_mutex);
162
163 return result;
164}
gboolean fl_engine_unregister_external_texture(FlEngine *self, int64_t texture_id)
Definition: fl_engine.cc:908
GAsyncResult * result