Add fl_texture_get_id, so textures can be passed to Flutter (flutter/engine#40290)

Add fl_texture_get_id, so textures can be passed to Flutter
This commit is contained in:
Robert Ancell
2023-03-16 14:10:07 +13:00
committed by GitHub
parent 116271dad3
commit c411d37046
11 changed files with 118 additions and 53 deletions

View File

@@ -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<FlPixelBufferTexturePrivate*>(
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<FlPixelBufferTexturePrivate*>(
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 =

View File

@@ -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<int64_t>(texture));
fl_texture_set_id(texture, 42);
EXPECT_EQ(fl_texture_get_id(texture), static_cast<int64_t>(42));
}
// Test that populating an OpenGL texture works.

View File

@@ -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<int64_t>(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);
}

View File

@@ -9,16 +9,42 @@
#include <gmodule.h>
#include <cstdio>
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<FlTextureGLPrivate*>(
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<FlTextureGLPrivate*>(
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) {}

View File

@@ -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<int64_t>(texture));
fl_texture_set_id(texture, 42);
EXPECT_EQ(fl_texture_get_id(texture), static_cast<int64_t>(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;

View File

@@ -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

View File

@@ -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);
}

View File

@@ -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));

View File

@@ -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_

View File

@@ -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;
}

View File

@@ -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;