diff --git a/engine/src/flutter/shell/platform/linux/fl_pixel_buffer_texture.cc b/engine/src/flutter/shell/platform/linux/fl_pixel_buffer_texture.cc index f0f75bef11..b284784eb5 100644 --- a/engine/src/flutter/shell/platform/linux/fl_pixel_buffer_texture.cc +++ b/engine/src/flutter/shell/platform/linux/fl_pixel_buffer_texture.cc @@ -10,14 +10,15 @@ #include "flutter/shell/platform/linux/fl_pixel_buffer_texture_private.h" typedef struct { + int64_t id; GLuint texture_id; } FlPixelBufferTexturePrivate; +static void fl_pixel_buffer_texture_iface_init(FlTextureInterface* iface); + // Added here to stop the compiler from optimising this function away. G_MODULE_EXPORT GType fl_pixel_buffer_texture_get_type(); -static void fl_pixel_buffer_texture_iface_init(FlTextureInterface* iface) {} - G_DEFINE_TYPE_WITH_CODE( FlPixelBufferTexture, fl_pixel_buffer_texture, @@ -26,6 +27,29 @@ G_DEFINE_TYPE_WITH_CODE( fl_pixel_buffer_texture_iface_init); G_ADD_PRIVATE(FlPixelBufferTexture)) +// Implements FlTexture::set_id +static void fl_pixel_buffer_texture_set_id(FlTexture* texture, int64_t id) { + FlPixelBufferTexture* self = FL_PIXEL_BUFFER_TEXTURE(texture); + FlPixelBufferTexturePrivate* priv = + reinterpret_cast( + fl_pixel_buffer_texture_get_instance_private(self)); + priv->id = id; +} + +// Implements FlTexture::set_id +static int64_t fl_pixel_buffer_texture_get_id(FlTexture* texture) { + FlPixelBufferTexture* self = FL_PIXEL_BUFFER_TEXTURE(texture); + FlPixelBufferTexturePrivate* priv = + reinterpret_cast( + fl_pixel_buffer_texture_get_instance_private(self)); + return priv->id; +} + +static void fl_pixel_buffer_texture_iface_init(FlTextureInterface* iface) { + iface->set_id = fl_pixel_buffer_texture_set_id; + iface->get_id = fl_pixel_buffer_texture_get_id; +} + static void fl_pixel_buffer_texture_dispose(GObject* object) { FlPixelBufferTexture* self = FL_PIXEL_BUFFER_TEXTURE(object); FlPixelBufferTexturePrivate* priv = diff --git a/engine/src/flutter/shell/platform/linux/fl_pixel_buffer_texture_test.cc b/engine/src/flutter/shell/platform/linux/fl_pixel_buffer_texture_test.cc index c04c4fa66e..5b32f5a58b 100644 --- a/engine/src/flutter/shell/platform/linux/fl_pixel_buffer_texture_test.cc +++ b/engine/src/flutter/shell/platform/linux/fl_pixel_buffer_texture_test.cc @@ -68,10 +68,9 @@ static FlTestPixelBufferTexture* fl_test_pixel_buffer_texture_new() { // Test that getting the texture ID works. TEST(FlPixelBufferTextureTest, TextureID) { - // Texture ID is not assigned until the pixel buffer is copied once. g_autoptr(FlTexture) texture = FL_TEXTURE(fl_test_pixel_buffer_texture_new()); - EXPECT_EQ(fl_texture_get_texture_id(texture), - reinterpret_cast(texture)); + fl_texture_set_id(texture, 42); + EXPECT_EQ(fl_texture_get_id(texture), static_cast(42)); } // Test that populating an OpenGL texture works. diff --git a/engine/src/flutter/shell/platform/linux/fl_texture.cc b/engine/src/flutter/shell/platform/linux/fl_texture.cc index ff26adc31b..8ede3bdb51 100644 --- a/engine/src/flutter/shell/platform/linux/fl_texture.cc +++ b/engine/src/flutter/shell/platform/linux/fl_texture.cc @@ -15,7 +15,12 @@ G_DEFINE_INTERFACE(FlTexture, fl_texture, G_TYPE_OBJECT) static void fl_texture_default_init(FlTextureInterface* self) {} -int64_t fl_texture_get_texture_id(FlTexture* self) { - g_return_val_if_fail(FL_IS_TEXTURE(self), -1); - return reinterpret_cast(self); +void fl_texture_set_id(FlTexture* self, int64_t id) { + g_return_if_fail(FL_IS_TEXTURE(self)); + FL_TEXTURE_GET_IFACE(self)->set_id(self, id); +} + +G_MODULE_EXPORT int64_t fl_texture_get_id(FlTexture* self) { + g_return_val_if_fail(FL_IS_TEXTURE(self), -1); + return FL_TEXTURE_GET_IFACE(self)->get_id(self); } diff --git a/engine/src/flutter/shell/platform/linux/fl_texture_gl.cc b/engine/src/flutter/shell/platform/linux/fl_texture_gl.cc index c447ccc7eb..a043c36f87 100644 --- a/engine/src/flutter/shell/platform/linux/fl_texture_gl.cc +++ b/engine/src/flutter/shell/platform/linux/fl_texture_gl.cc @@ -9,16 +9,42 @@ #include #include +typedef struct { + int64_t id; +} FlTextureGLPrivate; + +static void fl_texture_gl_texture_iface_init(FlTextureInterface* iface); + // Added here to stop the compiler from optimising this function away. G_MODULE_EXPORT GType fl_texture_gl_get_type(); -static void fl_texture_gl_texture_iface_init(FlTextureInterface* iface) {} - G_DEFINE_TYPE_WITH_CODE(FlTextureGL, fl_texture_gl, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE(fl_texture_get_type(), - fl_texture_gl_texture_iface_init)) + fl_texture_gl_texture_iface_init); + G_ADD_PRIVATE(FlTextureGL)) + +// Implements FlTexture::set_id +static void fl_texture_gl_set_id(FlTexture* texture, int64_t id) { + FlTextureGL* self = FL_TEXTURE_GL(texture); + FlTextureGLPrivate* priv = reinterpret_cast( + fl_texture_gl_get_instance_private(self)); + priv->id = id; +} + +// Implements FlTexture::set_id +static int64_t fl_texture_gl_get_id(FlTexture* texture) { + FlTextureGL* self = FL_TEXTURE_GL(texture); + FlTextureGLPrivate* priv = reinterpret_cast( + fl_texture_gl_get_instance_private(self)); + return priv->id; +} + +static void fl_texture_gl_texture_iface_init(FlTextureInterface* iface) { + iface->set_id = fl_texture_gl_set_id; + iface->get_id = fl_texture_gl_get_id; +} static void fl_texture_gl_class_init(FlTextureGLClass* klass) {} diff --git a/engine/src/flutter/shell/platform/linux/fl_texture_gl_test.cc b/engine/src/flutter/shell/platform/linux/fl_texture_gl_test.cc index 14d0b888e8..275d0cd64e 100644 --- a/engine/src/flutter/shell/platform/linux/fl_texture_gl_test.cc +++ b/engine/src/flutter/shell/platform/linux/fl_texture_gl_test.cc @@ -59,15 +59,14 @@ static FlTestTexture* fl_test_texture_new() { } // Test that getting the texture ID works. -TEST(FlTextureTest, TextureID) { - // Texture ID is not assigned until the testure is populated. +TEST(FlTextureGLTest, TextureID) { g_autoptr(FlTexture) texture = FL_TEXTURE(fl_test_texture_new()); - EXPECT_EQ(fl_texture_get_texture_id(texture), - reinterpret_cast(texture)); + fl_texture_set_id(texture, 42); + EXPECT_EQ(fl_texture_get_id(texture), static_cast(42)); } // Test that populating an OpenGL texture works. -TEST(FlTextureTest, PopulateTexture) { +TEST(FlTextureGLTest, PopulateTexture) { g_autoptr(FlTextureGL) texture = FL_TEXTURE_GL(fl_test_texture_new()); FlutterOpenGLTexture opengl_texture = {0}; g_autoptr(GError) error = nullptr; diff --git a/engine/src/flutter/shell/platform/linux/fl_texture_private.h b/engine/src/flutter/shell/platform/linux/fl_texture_private.h index 0bf304eb52..892ff7440b 100644 --- a/engine/src/flutter/shell/platform/linux/fl_texture_private.h +++ b/engine/src/flutter/shell/platform/linux/fl_texture_private.h @@ -11,14 +11,13 @@ G_BEGIN_DECLS /** - * fl_texture_get_texture_id: + * fl_texture_set_id: * @texture: an #FlTexture. + * @id: a texture ID. * - * Retrieves the unique id of this texture. - * - * Returns: the unique id of this texture. + * Set the ID for a texture. */ -int64_t fl_texture_get_texture_id(FlTexture* texture); +void fl_texture_set_id(FlTexture* texture, int64_t id); G_END_DECLS diff --git a/engine/src/flutter/shell/platform/linux/fl_texture_registrar.cc b/engine/src/flutter/shell/platform/linux/fl_texture_registrar.cc index a2a6669a5e..0851d22edd 100644 --- a/engine/src/flutter/shell/platform/linux/fl_texture_registrar.cc +++ b/engine/src/flutter/shell/platform/linux/fl_texture_registrar.cc @@ -25,6 +25,9 @@ struct _FlTextureRegistrarImpl { // Weak reference to the engine this texture registrar is created for. FlEngine* engine; + // ID to assign to the next new texture. + int64_t next_id; + // Internal record for registered textures. // // It is a map from Flutter texture ID to #FlTexture instance created by @@ -83,16 +86,19 @@ static gboolean register_texture(FlTextureRegistrar* registrar, FlTextureRegistrarImpl* self = FL_TEXTURE_REGISTRAR_IMPL(registrar); if (FL_IS_TEXTURE_GL(texture) || FL_IS_PIXEL_BUFFER_TEXTURE(texture)) { - g_hash_table_insert(self->textures, - GINT_TO_POINTER(fl_texture_get_texture_id(texture)), - g_object_ref(texture)); - if (self->engine == nullptr) { return FALSE; } - return fl_engine_register_external_texture( - self->engine, fl_texture_get_texture_id(texture)); + int64_t id = self->next_id++; + if (fl_engine_register_external_texture(self->engine, id)) { + fl_texture_set_id(texture, id); + g_hash_table_insert(self->textures, GINT_TO_POINTER(id), + g_object_ref(texture)); + return TRUE; + } else { + return FALSE; + } } else { // We currently only support #FlTextureGL and #FlPixelBufferTexture. return FALSE; @@ -114,33 +120,27 @@ static gboolean mark_texture_frame_available(FlTextureRegistrar* registrar, return FALSE; } - if (lookup_texture(registrar, fl_texture_get_texture_id(texture)) == - nullptr) { - g_warning("Unregistered texture %p", texture); - return FALSE; - } - - return fl_engine_mark_texture_frame_available( - self->engine, fl_texture_get_texture_id(texture)); + return fl_engine_mark_texture_frame_available(self->engine, + fl_texture_get_id(texture)); } static gboolean unregister_texture(FlTextureRegistrar* registrar, FlTexture* texture) { FlTextureRegistrarImpl* self = FL_TEXTURE_REGISTRAR_IMPL(registrar); - if (!g_hash_table_remove( - self->textures, - GINT_TO_POINTER(fl_texture_get_texture_id(texture)))) { - g_warning("Unregistering a non-existent texture %p", texture); - return FALSE; - } - if (self->engine == nullptr) { return FALSE; } - return fl_engine_unregister_external_texture( - self->engine, fl_texture_get_texture_id(texture)); + gboolean result = fl_engine_unregister_external_texture( + self->engine, fl_texture_get_id(texture)); + + if (!g_hash_table_remove(self->textures, + GINT_TO_POINTER(fl_texture_get_id(texture)))) { + g_warning("Unregistering a non-existent texture %p", texture); + } + + return result; } static void fl_texture_registrar_impl_iface_init( @@ -152,6 +152,7 @@ static void fl_texture_registrar_impl_iface_init( } static void fl_texture_registrar_impl_init(FlTextureRegistrarImpl* self) { + self->next_id = 1; self->textures = g_hash_table_new_full(g_direct_hash, g_direct_equal, nullptr, g_object_unref); } diff --git a/engine/src/flutter/shell/platform/linux/fl_texture_registrar_test.cc b/engine/src/flutter/shell/platform/linux/fl_texture_registrar_test.cc index 3b41d1ac1d..17bac5917d 100644 --- a/engine/src/flutter/shell/platform/linux/fl_texture_registrar_test.cc +++ b/engine/src/flutter/shell/platform/linux/fl_texture_registrar_test.cc @@ -3,7 +3,6 @@ // found in the LICENSE file. #include "flutter/shell/platform/linux/public/flutter_linux/fl_texture_registrar.h" -#include "flutter/shell/platform/linux/fl_texture_private.h" #include "flutter/shell/platform/linux/fl_texture_registrar_private.h" #include "flutter/shell/platform/linux/public/flutter_linux/fl_pixel_buffer_texture.h" #include "flutter/shell/platform/linux/public/flutter_linux/fl_texture_gl.h" @@ -74,10 +73,6 @@ TEST(FlTextureRegistrarTest, MockRegistrar) { EXPECT_TRUE(fl_texture_registrar_register_texture( FL_TEXTURE_REGISTRAR(registrar), texture)); EXPECT_EQ(fl_mock_texture_registrar_get_texture(registrar), texture); - EXPECT_EQ( - fl_texture_registrar_lookup_texture(FL_TEXTURE_REGISTRAR(registrar), - fl_texture_get_texture_id(texture)), - texture); EXPECT_TRUE(fl_texture_registrar_mark_texture_frame_available( FL_TEXTURE_REGISTRAR(registrar), texture)); EXPECT_TRUE(fl_mock_texture_registrar_get_frame_available(registrar)); diff --git a/engine/src/flutter/shell/platform/linux/public/flutter_linux/fl_texture.h b/engine/src/flutter/shell/platform/linux/public/flutter_linux/fl_texture.h index 45ceb51478..ae0372c19c 100644 --- a/engine/src/flutter/shell/platform/linux/public/flutter_linux/fl_texture.h +++ b/engine/src/flutter/shell/platform/linux/public/flutter_linux/fl_texture.h @@ -28,8 +28,23 @@ G_DECLARE_INTERFACE(FlTexture, fl_texture, FL, TEXTURE, GObject) struct _FlTextureInterface { GTypeInterface g_iface; + + void (*set_id)(FlTexture* texture, int64_t id); + + int64_t (*get_id)(FlTexture* texture); }; +/** + * fl_texture_get_id: + * @texture: a #FlTexture. + * + * Get the ID for this texture, which can be passed to Flutter code to refer to + * this texture. + * + * Returns: a texture ID. + */ +int64_t fl_texture_get_id(FlTexture* texture); + G_END_DECLS #endif // FLUTTER_SHELL_PLATFORM_LINUX_FL_TEXTURE_H_ diff --git a/engine/src/flutter/shell/platform/linux/testing/mock_engine.cc b/engine/src/flutter/shell/platform/linux/testing/mock_engine.cc index dc3772e9ba..eac0cb3809 100644 --- a/engine/src/flutter/shell/platform/linux/testing/mock_engine.cc +++ b/engine/src/flutter/shell/platform/linux/testing/mock_engine.cc @@ -505,6 +505,10 @@ FlutterEngineResult FlutterEngineMarkExternalTextureFrameAvailable( FlutterEngineResult FlutterEngineUnregisterExternalTexture( FLUTTER_API_SYMBOL(FlutterEngine) engine, int64_t texture_identifier) { + auto val = engine->textures.find(texture_identifier); + if (val == std::end(engine->textures)) { + return kInvalidArguments; + } engine->textures.erase(texture_identifier); return kSuccess; } diff --git a/engine/src/flutter/shell/platform/linux/testing/mock_texture_registrar.cc b/engine/src/flutter/shell/platform/linux/testing/mock_texture_registrar.cc index 66b5b8cfab..0a91d24fbb 100644 --- a/engine/src/flutter/shell/platform/linux/testing/mock_texture_registrar.cc +++ b/engine/src/flutter/shell/platform/linux/testing/mock_texture_registrar.cc @@ -3,7 +3,6 @@ // found in the LICENSE file. #include "flutter/shell/platform/linux/testing/mock_texture_registrar.h" -#include "flutter/shell/platform/linux/fl_texture_private.h" struct _FlMockTextureRegistrar { GObject parent_instance; @@ -35,7 +34,7 @@ static FlTexture* lookup_texture(FlTextureRegistrar* registrar, int64_t texture_id) { FlMockTextureRegistrar* self = FL_MOCK_TEXTURE_REGISTRAR(registrar); if (self->texture != nullptr && - fl_texture_get_texture_id(self->texture) == texture_id) { + fl_texture_get_id(self->texture) == texture_id) { return self->texture; } return nullptr; @@ -44,8 +43,7 @@ static FlTexture* lookup_texture(FlTextureRegistrar* registrar, static gboolean mark_texture_frame_available(FlTextureRegistrar* registrar, FlTexture* texture) { FlMockTextureRegistrar* self = FL_MOCK_TEXTURE_REGISTRAR(registrar); - if (lookup_texture(registrar, fl_texture_get_texture_id(texture)) == - nullptr) { + if (lookup_texture(registrar, fl_texture_get_id(texture)) == nullptr) { return FALSE; } self->frame_available = TRUE;