refactor: use instance instead
This commit is contained in:
parent
8682d1d564
commit
8208587b23
2 changed files with 44 additions and 43 deletions
|
|
@ -6,16 +6,19 @@
|
||||||
|
|
||||||
#include "texture_rgba_renderer_plugin.h"
|
#include "texture_rgba_renderer_plugin.h"
|
||||||
|
|
||||||
struct _TextureRgbaClass
|
G_BEGIN_DECLS
|
||||||
{
|
|
||||||
FlPixelBufferTextureClass parent_class;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct TextureRgbaPrivate
|
typedef struct {
|
||||||
|
FlPixelBufferTextureClass parent_class;
|
||||||
|
} TextureRgbaClass;
|
||||||
|
|
||||||
|
struct _TextureRgba
|
||||||
{
|
{
|
||||||
|
FlPixelBufferTexture parent_instance;
|
||||||
|
|
||||||
|
uint8_t *buffer = nullptr;
|
||||||
FlTextureRegistrar* texture_registrar = nullptr;
|
FlTextureRegistrar* texture_registrar = nullptr;
|
||||||
int64_t texture_id = 0;
|
int64_t texture_id = 0;
|
||||||
uint8_t *buffer = nullptr;
|
|
||||||
uint8_t *prior_buffer = nullptr;
|
uint8_t *prior_buffer = nullptr;
|
||||||
gboolean buffer_ready = FALSE;
|
gboolean buffer_ready = FALSE;
|
||||||
gboolean terminated = FALSE;
|
gboolean terminated = FALSE;
|
||||||
|
|
@ -24,12 +27,17 @@ struct TextureRgbaPrivate
|
||||||
GMutex mutex;
|
GMutex mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef struct _TextureRgba TextureRgba;
|
||||||
|
|
||||||
/// Constructor.
|
/// 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.
|
/// Add private data.
|
||||||
G_DEFINE_TYPE_WITH_CODE(TextureRgba, texture_rgba,
|
G_DEFINE_TYPE(TextureRgba, texture_rgba,
|
||||||
fl_pixel_buffer_texture_get_type(),
|
fl_pixel_buffer_texture_get_type())
|
||||||
G_ADD_PRIVATE(TextureRgba))
|
|
||||||
|
#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) {
|
static inline void switch_rgba(uint8_t* pixels, int width, int height) {
|
||||||
uint8_t temp;
|
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) {
|
static void texture_rgba_terminate(TextureRgba* self) {
|
||||||
TextureRgbaPrivate *self = (TextureRgbaPrivate *)
|
|
||||||
texture_rgba_get_instance_private(TEXTURE_RGBA_RENDERER_TEXTURE_RGBA(texture));
|
|
||||||
g_mutex_lock(&self->mutex);
|
g_mutex_lock(&self->mutex);
|
||||||
g_atomic_int_set(&self->terminated, TRUE);
|
g_atomic_int_set(&self->terminated, TRUE);
|
||||||
g_mutex_unlock(&self->mutex);
|
g_mutex_unlock(&self->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean texture_rgba_is_terminate(TextureRgba* texture) {
|
static gboolean texture_rgba_is_terminate(TextureRgba* self) {
|
||||||
TextureRgbaPrivate *self = (TextureRgbaPrivate *)
|
|
||||||
texture_rgba_get_instance_private(TEXTURE_RGBA_RENDERER_TEXTURE_RGBA(texture));
|
|
||||||
return g_atomic_int_get(&self->terminated);
|
return g_atomic_int_get(&self->terminated);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -61,20 +65,15 @@ static gboolean texture_rgba_copy_pixels(FlPixelBufferTexture* texture,
|
||||||
uint32_t* width,
|
uint32_t* width,
|
||||||
uint32_t* height,
|
uint32_t* height,
|
||||||
GError** error) {
|
GError** error) {
|
||||||
TextureRgbaPrivate *self = (TextureRgbaPrivate *)
|
TextureRgba *self = TEXTURE_RGBA(texture);
|
||||||
texture_rgba_get_instance_private(TEXTURE_RGBA_RENDERER_TEXTURE_RGBA(texture));
|
|
||||||
// This method is called on Render Thread. Be careful with your
|
// This method is called on Render Thread. Be careful with your
|
||||||
// cross-thread operation.
|
// cross-thread operation.
|
||||||
g_mutex_lock(&self->mutex);
|
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.
|
// @width and @height are initially stored the canvas size in Flutter.
|
||||||
// You must prepare your pixel buffer in RGBA format.
|
// You must prepare your pixel buffer in RGBA format.
|
||||||
// So you may do some format conversion first if your original pixel
|
// So you may do some format conversion first if your original pixel
|
||||||
// buffer is not in RGBA format.
|
// 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 ();
|
// manage_your_pixel_buffer_here ();
|
||||||
if (g_atomic_int_get(&self->buffer_ready)) {
|
if (g_atomic_int_get(&self->buffer_ready)) {
|
||||||
// Directly return pointer to your pixel buffer here.
|
// 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.
|
// next tick of Render Thread.
|
||||||
// If it is hard to manage lifetime of your pixel buffer, you should
|
// If it is hard to manage lifetime of your pixel buffer, you should
|
||||||
// take look into #FlTextureGL.
|
// take look into #FlTextureGL.
|
||||||
|
if (self->prior_buffer) {
|
||||||
|
delete[] self->prior_buffer;
|
||||||
|
self->prior_buffer = nullptr;
|
||||||
|
}
|
||||||
*out_buffer = buffer;
|
*out_buffer = buffer;
|
||||||
*width = g_atomic_int_get(&self->video_width);
|
*width = g_atomic_int_get(&self->video_width);
|
||||||
*height = g_atomic_int_get(&self->video_height);
|
*height = g_atomic_int_get(&self->video_height);
|
||||||
|
|
@ -97,9 +100,8 @@ static gboolean texture_rgba_copy_pixels(FlPixelBufferTexture* texture,
|
||||||
return FALSE;
|
return FALSE;
|
||||||
} else {
|
} else {
|
||||||
g_mutex_unlock(&self->mutex);
|
g_mutex_unlock(&self->mutex);
|
||||||
// set @error to report failure.
|
// *error = g_error_new(g_quark_from_static_string("TextureRgba Renderer"), 1, "waiting for new incoming images.");
|
||||||
*error = g_error_new(g_quark_from_static_string("TextureRgba Renderer"), 1, "the texture is waiting for incoming images.");
|
return TRUE;
|
||||||
return FALSE;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -139,9 +141,9 @@ static gboolean texture_rgba_copy_pixels(FlPixelBufferTexture* texture,
|
||||||
|
|
||||||
static TextureRgba *texture_rgba_new(FlTextureRegistrar* registrar)
|
static TextureRgba *texture_rgba_new(FlTextureRegistrar* registrar)
|
||||||
{
|
{
|
||||||
auto texture = TEXTURE_RGBA_RENDERER_TEXTURE_RGBA(g_object_new(texture_rgba_get_type(), nullptr));
|
auto texture = TEXTURE_RGBA(g_object_new(texture_rgba_get_type(), nullptr));
|
||||||
((TextureRgbaPrivate*)texture_rgba_get_instance_private(texture))->texture_registrar = registrar;
|
texture->texture_registrar = registrar;
|
||||||
g_mutex_init(&((TextureRgbaPrivate*)texture_rgba_get_instance_private(texture))->mutex);
|
g_mutex_init(&texture->mutex);
|
||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -155,4 +157,5 @@ static void texture_rgba_init(TextureRgba* self) {
|
||||||
texture_rgba_copy_pixels;
|
texture_rgba_copy_pixels;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
G_END_DECLS
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -44,8 +44,7 @@ static void texture_rgba_renderer_plugin_handle_method_call(
|
||||||
{
|
{
|
||||||
TextureRgba* texture_rgba = texture_rgba_new(self->texture_registrar);
|
TextureRgba* texture_rgba = texture_rgba_new(self->texture_registrar);
|
||||||
auto texture_id = reinterpret_cast<int64_t>(FL_TEXTURE(texture_rgba));
|
auto texture_id = reinterpret_cast<int64_t>(FL_TEXTURE(texture_rgba));
|
||||||
auto* priv = (TextureRgbaPrivate*)texture_rgba_get_instance_private(texture_rgba);
|
texture_rgba->texture_id = texture_id;
|
||||||
priv->texture_id = texture_id;
|
|
||||||
g_renderer_map.insert(std::make_pair(key, texture_rgba));
|
g_renderer_map.insert(std::make_pair(key, texture_rgba));
|
||||||
// Register to the registrar.
|
// Register to the registrar.
|
||||||
fl_texture_registrar_register_texture(self->texture_registrar, FL_TEXTURE(texture_rgba));
|
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" {
|
extern "C" {
|
||||||
void FlutterRgbaRendererPluginOnRgba(void *texture_rgba_ptr, const uint8_t *buffer, int width, int height) {
|
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);
|
TextureRgba* self = TEXTURE_RGBA(texture_rgba_ptr);
|
||||||
auto priv = (TextureRgbaPrivate *)texture_rgba_get_instance_private(texture_rgba);
|
g_mutex_lock(&self->mutex);
|
||||||
g_mutex_lock(&priv->mutex);
|
// selfate has registered a texture_id,
|
||||||
// private has registered a texture_id,
|
if (self->texture_id != 0 && !g_atomic_int_get(&self->buffer_ready) && !texture_rgba_is_terminate(self)) {
|
||||||
if (priv->texture_id != 0 && !g_atomic_int_get(&priv->buffer_ready) && !texture_rgba_is_terminate(texture_rgba)) {
|
|
||||||
// copy data to the texture.
|
// copy data to the texture.
|
||||||
auto buffer_length = 4 * width * height;
|
auto buffer_length = 4 * width * height;
|
||||||
auto copied_data = new uint8_t[buffer_length];
|
auto copied_data = new uint8_t[buffer_length];
|
||||||
memcpy(copied_data, buffer, buffer_length);
|
memcpy(copied_data, buffer, buffer_length);
|
||||||
switch_rgba(copied_data, width, height);
|
switch_rgba(copied_data, width, height);
|
||||||
// It's safe to working on a non reading index
|
// It's safe to working on a non reading index
|
||||||
g_atomic_pointer_set(&priv->buffer, copied_data);
|
g_atomic_pointer_set(&self->buffer, copied_data);
|
||||||
g_atomic_int_set(&priv->video_height, height);
|
g_atomic_int_set(&self->video_height, height);
|
||||||
g_atomic_int_set(&priv->video_width, width);
|
g_atomic_int_set(&self->video_width, width);
|
||||||
g_atomic_int_set(&priv->buffer_ready, TRUE);
|
g_atomic_int_set(&self->buffer_ready, TRUE);
|
||||||
fl_texture_registrar_mark_texture_frame_available(
|
fl_texture_registrar_mark_texture_frame_available(
|
||||||
priv->texture_registrar,
|
self->texture_registrar,
|
||||||
FL_TEXTURE(texture_rgba)
|
FL_TEXTURE(self)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
g_mutex_unlock(&priv->mutex);
|
g_mutex_unlock(&self->mutex);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue