refactor: change to pixel rgba

This commit is contained in:
Kingtous 2023-02-28 17:00:48 +08:00
parent c37336cd9b
commit bcd8fad1c0
2 changed files with 99 additions and 51 deletions

View file

@ -3,22 +3,20 @@
#include <flutter_linux/flutter_linux.h> #include <flutter_linux/flutter_linux.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <epoxy/gl.h>
#include "texture_rgba_renderer_plugin.h" #include "texture_rgba_renderer_plugin.h"
#include <vector>
struct _TextureRgbaClass struct _TextureRgbaClass
{ {
FlTextureGLClass parent_class; FlPixelBufferTextureClass parent_class;
}; };
struct TextureRgbaPrivate struct TextureRgbaPrivate
{ {
FlTextureRegistrar* texture_registrar = nullptr; FlTextureRegistrar* texture_registrar = nullptr;
GLuint texture_id = 0; int64_t texture_id = 0;
const uint8_t *buffer = nullptr; uint8_t *buffer = nullptr;
uint8_t *prior_buffer = nullptr;
gboolean buffer_ready = FALSE; gboolean buffer_ready = FALSE;
gboolean terminated = FALSE; gboolean terminated = FALSE;
gint32 video_width = 0; gint32 video_width = 0;
@ -27,21 +25,28 @@ struct TextureRgbaPrivate
}; };
/// Constructor. /// Constructor.
G_DECLARE_DERIVABLE_TYPE(TextureRgba, texture_rgba, TEXTURE_RGBA_RENDERER, TEXTURE_RGBA, FlTextureGL) 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_WITH_CODE(TextureRgba, texture_rgba,
fl_texture_gl_get_type(), fl_pixel_buffer_texture_get_type(),
G_ADD_PRIVATE(TextureRgba)) G_ADD_PRIVATE(TextureRgba))
static inline void switch_rgba(uint8_t* pixels, int width, int height) {
uint8_t temp;
for (int i = 0; i < width * height; i++) {
temp = pixels[4 * i];
pixels[4 * i] = pixels[4 * i + 2];
pixels[4 * i + 2] = temp;
}
}
static void texture_rgba_terminate(TextureRgba* texture) { static void texture_rgba_terminate(TextureRgba* texture) {
TextureRgbaPrivate *self = (TextureRgbaPrivate *) TextureRgbaPrivate *self = (TextureRgbaPrivate *)
texture_rgba_get_instance_private(TEXTURE_RGBA_RENDERER_TEXTURE_RGBA(texture)); 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);
if (g_atomic_pointer_get(&self->buffer)) {
delete[] g_atomic_pointer_get(&self->buffer);
g_atomic_pointer_set(&self->buffer, nullptr);
}
g_mutex_unlock(&self->mutex); g_mutex_unlock(&self->mutex);
} }
@ -51,47 +56,86 @@ static gboolean texture_rgba_is_terminate(TextureRgba* texture) {
return g_atomic_int_get(&self->terminated); return g_atomic_int_get(&self->terminated);
} }
static gboolean texture_rgba_populate(FlTextureGL *texture, static gboolean texture_rgba_copy_pixels(FlPixelBufferTexture* texture,
uint32_t *target, const uint8_t** out_buffer,
uint32_t *name,
uint32_t* width, uint32_t* width,
uint32_t* height, uint32_t* height,
GError **error) GError** error) {
{
TextureRgbaPrivate *self = (TextureRgbaPrivate *) TextureRgbaPrivate *self = (TextureRgbaPrivate *)
texture_rgba_get_instance_private(TEXTURE_RGBA_RENDERER_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
// cross-thread operation.
g_mutex_lock(&self->mutex); g_mutex_lock(&self->mutex);
if (self->texture_id == 0) if (self->prior_buffer) {
{ delete[] self->prior_buffer;
glGenTextures(1, &self->texture_id); self->prior_buffer = nullptr;
glBindTexture(GL_TEXTURE_2D, self->texture_id);
// further configuration here.
g_atomic_pointer_set(&self->buffer, nullptr);
} }
else // @width and @height are initially stored the canvas size in Flutter.
{ // You must prepare your pixel buffer in RGBA format.
glBindTexture(GL_TEXTURE_2D, self->texture_id); // So you may do some format conversion first if your original pixel
} // buffer is not in RGBA format.
// For example, we render pixel buffer here. auto* buffer = g_atomic_pointer_get(&self->buffer);
// Note that Flutter only accepts textures in GL_RGBA8 format. // manage_your_pixel_buffer_here ();
if (g_atomic_pointer_get(&self->buffer)) { if (g_atomic_int_get(&self->buffer_ready)) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, g_atomic_int_get(&self->video_width), g_atomic_int_get(&self->video_height), 0, GL_BGRA, // Directly return pointer to your pixel buffer here.
GL_UNSIGNED_BYTE, g_atomic_pointer_get(&self->buffer)); // Flutter takes content of your pixel buffer after this function
} // is finished. So you must make the buffer live long enough until
// Now we can release the self->buffer. // next tick of Render Thread.
if (g_atomic_pointer_get(&self->buffer) != nullptr) { // If it is hard to manage lifetime of your pixel buffer, you should
delete[] self->buffer; // take look into #FlTextureGL.
g_atomic_pointer_set(&self->buffer, nullptr); *out_buffer = buffer;
}
g_atomic_int_set(&self->buffer_ready, FALSE);
*target = GL_TEXTURE_2D;
*name = self->texture_id;
*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);
self->prior_buffer = buffer;
g_atomic_int_set(&self->buffer_ready, FALSE);
g_mutex_unlock(&self->mutex); g_mutex_unlock(&self->mutex);
return TRUE; return TRUE;
} else if (g_atomic_int_get(&self->terminated)) {
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 already terminated, ignoring.");
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;
} }
}
// static gboolean texture_rgba_populate(FlTextureGL *texture,
// uint32_t *target,
// uint32_t *name,
// uint32_t *width,
// uint32_t *height,
// GError **error)
// {
// TextureRgbaPrivate *self = (TextureRgbaPrivate *)
// texture_rgba_get_instance_private(TEXTURE_RGBA_RENDERER_TEXTURE_RGBA(texture));
// g_mutex_lock(&self->mutex);
// if (self->texture_id == 0)
// {
// glGenTextures(1, &self->texture_id);
// glBindTexture(GL_TEXTURE_2D, self->texture_id);
// // further configuration here.
// g_atomic_pointer_set(&self->buffer, nullptr);
// }
// else
// {
// glBindTexture(GL_TEXTURE_2D, self->texture_id);
// }
// // For example, we render pixel buffer here.
// // Note that Flutter only accepts textures in GL_RGBA8 format.
// g_atomic_int_set(&self->buffer_ready, FALSE);
// *target = GL_TEXTURE_2D;
// *name = self->texture_id;
// *width = g_atomic_int_get(&self->video_width);
// *height = g_atomic_int_get(&self->video_height);
// g_mutex_unlock(&self->mutex);
// return TRUE;
// }
static TextureRgba *texture_rgba_new(FlTextureRegistrar* registrar) static TextureRgba *texture_rgba_new(FlTextureRegistrar* registrar)
{ {
@ -103,9 +147,12 @@ static TextureRgba *texture_rgba_new(FlTextureRegistrar* registrar)
static void texture_rgba_class_init(TextureRgbaClass *klass) static void texture_rgba_class_init(TextureRgbaClass *klass)
{ {
FL_TEXTURE_GL_GET_CLASS(klass)->populate = texture_rgba_populate; FL_PIXEL_BUFFER_TEXTURE_GET_CLASS(klass)->copy_pixels = texture_rgba_copy_pixels;
} }
static void texture_rgba_init(TextureRgba* self) {} static void texture_rgba_init(TextureRgba* self) {
FL_PIXEL_BUFFER_TEXTURE_GET_CLASS(self)->copy_pixels =
texture_rgba_copy_pixels;
}
#endif #endif

View file

@ -44,8 +44,8 @@ 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));
FL_TEXTURE_GL_GET_CLASS(texture_rgba)->populate = auto* priv = (TextureRgbaPrivate*)texture_rgba_get_instance_private(texture_rgba);
texture_rgba_populate; 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));
@ -151,6 +151,7 @@ extern "C" {
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);
// 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(&priv->buffer, copied_data);
g_atomic_int_set(&priv->video_height, height); g_atomic_int_set(&priv->video_height, height);