From 8208587b23dd04781d22f4ddc16db74ed40280ed Mon Sep 17 00:00:00 2001 From: Kingtous Date: Tue, 28 Feb 2023 19:23:08 +0800 Subject: [PATCH] refactor: use instance instead --- .../texture_rgba_renderer.h | 61 ++++++++++--------- linux/texture_rgba_renderer_plugin.cc | 26 ++++---- 2 files changed, 44 insertions(+), 43 deletions(-) diff --git a/linux/include/texture_rgba_renderer/texture_rgba_renderer.h b/linux/include/texture_rgba_renderer/texture_rgba_renderer.h index e6da256..5974038 100644 --- a/linux/include/texture_rgba_renderer/texture_rgba_renderer.h +++ b/linux/include/texture_rgba_renderer/texture_rgba_renderer.h @@ -6,16 +6,19 @@ #include "texture_rgba_renderer_plugin.h" -struct _TextureRgbaClass -{ - FlPixelBufferTextureClass parent_class; -}; +G_BEGIN_DECLS -struct TextureRgbaPrivate +typedef struct { + FlPixelBufferTextureClass parent_class; +} TextureRgbaClass; + +struct _TextureRgba { + FlPixelBufferTexture parent_instance; + + uint8_t *buffer = nullptr; FlTextureRegistrar* texture_registrar = nullptr; int64_t texture_id = 0; - uint8_t *buffer = nullptr; uint8_t *prior_buffer = nullptr; gboolean buffer_ready = FALSE; gboolean terminated = FALSE; @@ -24,12 +27,17 @@ struct TextureRgbaPrivate GMutex mutex; }; +typedef struct _TextureRgba TextureRgba; + /// Constructor. -G_DECLARE_DERIVABLE_TYPE(TextureRgba, texture_rgba, TEXTURE_RGBA_RENDERER, TEXTURE_RGBA, FlPixelBufferTextureClass) +// G_DECLARE_DERIVABLE_TYPE(TextureRgba, texture_rgba, TEXTURE_RGBA_RENDERER, TEXTURE_RGBA, FlPixelBufferTextureClass) /// Add private data. -G_DEFINE_TYPE_WITH_CODE(TextureRgba, texture_rgba, - fl_pixel_buffer_texture_get_type(), - G_ADD_PRIVATE(TextureRgba)) +G_DEFINE_TYPE(TextureRgba, texture_rgba, + fl_pixel_buffer_texture_get_type()) + +#define TEXTURE_RGBA(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), texture_rgba_get_type(), \ + TextureRgba)) static inline void switch_rgba(uint8_t* pixels, int width, int height) { uint8_t temp; @@ -42,17 +50,13 @@ static inline void switch_rgba(uint8_t* pixels, int width, int height) { } -static void texture_rgba_terminate(TextureRgba* texture) { - TextureRgbaPrivate *self = (TextureRgbaPrivate *) - texture_rgba_get_instance_private(TEXTURE_RGBA_RENDERER_TEXTURE_RGBA(texture)); +static void texture_rgba_terminate(TextureRgba* self) { g_mutex_lock(&self->mutex); g_atomic_int_set(&self->terminated, TRUE); g_mutex_unlock(&self->mutex); } -static gboolean texture_rgba_is_terminate(TextureRgba* texture) { - TextureRgbaPrivate *self = (TextureRgbaPrivate *) - texture_rgba_get_instance_private(TEXTURE_RGBA_RENDERER_TEXTURE_RGBA(texture)); +static gboolean texture_rgba_is_terminate(TextureRgba* self) { return g_atomic_int_get(&self->terminated); } @@ -61,20 +65,15 @@ static gboolean texture_rgba_copy_pixels(FlPixelBufferTexture* texture, uint32_t* width, uint32_t* height, GError** error) { - TextureRgbaPrivate *self = (TextureRgbaPrivate *) - texture_rgba_get_instance_private(TEXTURE_RGBA_RENDERER_TEXTURE_RGBA(texture)); + TextureRgba *self = TEXTURE_RGBA(texture); // This method is called on Render Thread. Be careful with your // cross-thread operation. g_mutex_lock(&self->mutex); - if (self->prior_buffer) { - delete[] self->prior_buffer; - self->prior_buffer = nullptr; - } // @width and @height are initially stored the canvas size in Flutter. // You must prepare your pixel buffer in RGBA format. // So you may do some format conversion first if your original pixel // buffer is not in RGBA format. - auto* buffer = g_atomic_pointer_get(&self->buffer); + uint8_t* buffer = (uint8_t*)g_atomic_pointer_get(&self->buffer); // manage_your_pixel_buffer_here (); if (g_atomic_int_get(&self->buffer_ready)) { // Directly return pointer to your pixel buffer here. @@ -83,6 +82,10 @@ static gboolean texture_rgba_copy_pixels(FlPixelBufferTexture* texture, // next tick of Render Thread. // If it is hard to manage lifetime of your pixel buffer, you should // take look into #FlTextureGL. + if (self->prior_buffer) { + delete[] self->prior_buffer; + self->prior_buffer = nullptr; + } *out_buffer = buffer; *width = g_atomic_int_get(&self->video_width); *height = g_atomic_int_get(&self->video_height); @@ -97,9 +100,8 @@ static gboolean texture_rgba_copy_pixels(FlPixelBufferTexture* texture, return FALSE; } else { g_mutex_unlock(&self->mutex); - // set @error to report failure. - *error = g_error_new(g_quark_from_static_string("TextureRgba Renderer"), 1, "the texture is waiting for incoming images."); - return FALSE; + // *error = g_error_new(g_quark_from_static_string("TextureRgba Renderer"), 1, "waiting for new incoming images."); + return TRUE; } } @@ -139,9 +141,9 @@ static gboolean texture_rgba_copy_pixels(FlPixelBufferTexture* texture, static TextureRgba *texture_rgba_new(FlTextureRegistrar* registrar) { - auto texture = TEXTURE_RGBA_RENDERER_TEXTURE_RGBA(g_object_new(texture_rgba_get_type(), nullptr)); - ((TextureRgbaPrivate*)texture_rgba_get_instance_private(texture))->texture_registrar = registrar; - g_mutex_init(&((TextureRgbaPrivate*)texture_rgba_get_instance_private(texture))->mutex); + auto texture = TEXTURE_RGBA(g_object_new(texture_rgba_get_type(), nullptr)); + texture->texture_registrar = registrar; + g_mutex_init(&texture->mutex); return texture; } @@ -155,4 +157,5 @@ static void texture_rgba_init(TextureRgba* self) { texture_rgba_copy_pixels; } +G_END_DECLS #endif \ No newline at end of file diff --git a/linux/texture_rgba_renderer_plugin.cc b/linux/texture_rgba_renderer_plugin.cc index 55cf898..0915dbf 100644 --- a/linux/texture_rgba_renderer_plugin.cc +++ b/linux/texture_rgba_renderer_plugin.cc @@ -44,8 +44,7 @@ static void texture_rgba_renderer_plugin_handle_method_call( { TextureRgba* texture_rgba = texture_rgba_new(self->texture_registrar); auto texture_id = reinterpret_cast(FL_TEXTURE(texture_rgba)); - auto* priv = (TextureRgbaPrivate*)texture_rgba_get_instance_private(texture_rgba); - priv->texture_id = texture_id; + texture_rgba->texture_id = texture_id; g_renderer_map.insert(std::make_pair(key, texture_rgba)); // Register to the registrar. fl_texture_registrar_register_texture(self->texture_registrar, FL_TEXTURE(texture_rgba)); @@ -142,26 +141,25 @@ void texture_rgba_renderer_plugin_register_with_registrar(FlPluginRegistrar *reg extern "C" { void FlutterRgbaRendererPluginOnRgba(void *texture_rgba_ptr, const uint8_t *buffer, int width, int height) { - TextureRgba* texture_rgba = TEXTURE_RGBA_RENDERER_TEXTURE_RGBA(texture_rgba_ptr); - auto priv = (TextureRgbaPrivate *)texture_rgba_get_instance_private(texture_rgba); - g_mutex_lock(&priv->mutex); - // private has registered a texture_id, - if (priv->texture_id != 0 && !g_atomic_int_get(&priv->buffer_ready) && !texture_rgba_is_terminate(texture_rgba)) { + TextureRgba* self = TEXTURE_RGBA(texture_rgba_ptr); + g_mutex_lock(&self->mutex); + // selfate has registered a texture_id, + if (self->texture_id != 0 && !g_atomic_int_get(&self->buffer_ready) && !texture_rgba_is_terminate(self)) { // copy data to the texture. auto buffer_length = 4 * width * height; auto copied_data = new uint8_t[buffer_length]; memcpy(copied_data, buffer, buffer_length); switch_rgba(copied_data, width, height); // It's safe to working on a non reading index - g_atomic_pointer_set(&priv->buffer, copied_data); - g_atomic_int_set(&priv->video_height, height); - g_atomic_int_set(&priv->video_width, width); - g_atomic_int_set(&priv->buffer_ready, TRUE); + g_atomic_pointer_set(&self->buffer, copied_data); + g_atomic_int_set(&self->video_height, height); + g_atomic_int_set(&self->video_width, width); + g_atomic_int_set(&self->buffer_ready, TRUE); fl_texture_registrar_mark_texture_frame_available( - priv->texture_registrar, - FL_TEXTURE(texture_rgba) + self->texture_registrar, + FL_TEXTURE(self) ); } - g_mutex_unlock(&priv->mutex); + g_mutex_unlock(&self->mutex); } };