fix: linux read on write
This commit is contained in:
parent
69a51033bf
commit
686d6edaa5
3 changed files with 83 additions and 71 deletions
|
|
@ -7,6 +7,8 @@
|
|||
|
||||
#include "texture_rgba_renderer_plugin.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
struct _TextureRgbaClass
|
||||
{
|
||||
FlTextureGLClass parent_class;
|
||||
|
|
@ -16,10 +18,11 @@ struct TextureRgbaPrivate
|
|||
{
|
||||
FlTextureRegistrar* texture_registrar = nullptr;
|
||||
GLuint texture_id = 0;
|
||||
const uint8_t *buffer = nullptr;
|
||||
size_t buffer_length = 0;
|
||||
int32_t video_width = 0;
|
||||
int32_t video_height = 0;
|
||||
const uint8_t *buffer[2] = {nullptr, nullptr};
|
||||
size_t current_reading_index = 0;
|
||||
bool buffer_ready = false;
|
||||
int32_t video_width[2] = {0, 0};
|
||||
int32_t video_height[2] = {0, 0};
|
||||
GMutex mutex;
|
||||
};
|
||||
|
||||
|
|
@ -39,7 +42,13 @@ static gboolean texture_rgba_populate(FlTextureGL *texture,
|
|||
{
|
||||
TextureRgbaPrivate *self = (TextureRgbaPrivate *)
|
||||
texture_rgba_get_instance_private(TEXTURE_RGBA_RENDERER_TEXTURE_RGBA(texture));
|
||||
|
||||
g_mutex_lock(&self->mutex);
|
||||
if (self->buffer_ready) {
|
||||
self->current_reading_index ^= 1;
|
||||
}
|
||||
auto index = self->current_reading_index;
|
||||
|
||||
if (self->texture_id == 0)
|
||||
{
|
||||
glGenTextures(1, &self->texture_id);
|
||||
|
|
@ -52,16 +61,19 @@ static gboolean texture_rgba_populate(FlTextureGL *texture,
|
|||
}
|
||||
// For example, we render pixel buffer here.
|
||||
// Note that Flutter only accepts textures in GL_RGBA8 format.
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, self->video_width, self->video_height, 0, GL_BGRA,
|
||||
GL_UNSIGNED_BYTE, self->buffer);
|
||||
delete[] self->buffer;
|
||||
self->buffer = nullptr;
|
||||
g_mutex_unlock(&self->mutex);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, self->video_width[index], self->video_height[index], 0, GL_BGRA,
|
||||
GL_UNSIGNED_BYTE, self->buffer[index]);
|
||||
// Now we can release the self->buffer.
|
||||
if (self->buffer[index] != nullptr) {
|
||||
delete[] self->buffer[index];
|
||||
self->buffer[index] = nullptr;
|
||||
}
|
||||
self->buffer_ready = false;
|
||||
*target = GL_TEXTURE_2D;
|
||||
*name = self->texture_id;
|
||||
*width = self->video_width;
|
||||
*height = self->video_height;
|
||||
|
||||
*width = self->video_width[index];
|
||||
*height = self->video_height[index];
|
||||
g_mutex_unlock(&self->mutex);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,6 +12,8 @@
|
|||
#include <cstring>
|
||||
#include <unordered_map>
|
||||
|
||||
void FlutterRgbaRendererPluginOnRgba(void *texture_rgba_ptr, const uint8_t *buffer, int width, int height);
|
||||
|
||||
struct _TextureRgbaRendererPlugin
|
||||
{
|
||||
GObject parent_instance;
|
||||
|
|
@ -74,21 +76,14 @@ static void texture_rgba_renderer_plugin_handle_method_call(
|
|||
auto args = fl_method_call_get_args(method_call);
|
||||
auto key = fl_value_get_int(fl_value_lookup_string(args, "key"));
|
||||
auto data = fl_value_get_uint8_list(fl_value_lookup_string(args, "data"));
|
||||
auto data_length = fl_value_get_length(fl_value_lookup_string(args, "data"));
|
||||
auto buffer = new uint8_t[data_length];
|
||||
memcpy(buffer, data, data_length);
|
||||
// auto data_length = fl_value_get_length(fl_value_lookup_string(args, "data"));
|
||||
auto width = fl_value_get_int(fl_value_lookup_string(args, "width"));
|
||||
auto height = fl_value_get_int(fl_value_lookup_string(args, "height"));
|
||||
#ifdef _DEBUG
|
||||
g_assert(data_length == width * height * 4);
|
||||
#endif
|
||||
auto texture_rgba = (*self->g_renderer_map)[key];
|
||||
auto texture_rgba_private = (TextureRgbaPrivate *)texture_rgba_get_instance_private((*self->g_renderer_map)[key]);
|
||||
g_mutex_lock(&texture_rgba_private->mutex);
|
||||
if (texture_rgba_private->buffer != nullptr) {
|
||||
delete[] texture_rgba_private->buffer;
|
||||
}
|
||||
texture_rgba_private->buffer = buffer;
|
||||
texture_rgba_private->video_height = height;
|
||||
texture_rgba_private->video_width = width;
|
||||
g_mutex_unlock(&texture_rgba_private->mutex);
|
||||
FlutterRgbaRendererPluginOnRgba((void*)texture_rgba, data, width, height);
|
||||
response = FL_METHOD_RESPONSE(fl_method_success_response_new(
|
||||
fl_value_new_bool(fl_texture_registrar_mark_texture_frame_available(self->texture_registrar, FL_TEXTURE(texture_rgba)))));
|
||||
}
|
||||
|
|
@ -164,21 +159,28 @@ extern "C" {
|
|||
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) {
|
||||
// copy data to the texture.
|
||||
priv->buffer_length = 4 * width * height;
|
||||
auto copied_data = new uint8_t[priv->buffer_length];
|
||||
memcpy(copied_data, buffer, priv->buffer_length);
|
||||
if (priv->buffer != nullptr) {
|
||||
delete[] priv->buffer;
|
||||
auto buffer_length = 4 * width * height;
|
||||
auto copied_data = new uint8_t[buffer_length];
|
||||
memcpy(copied_data, buffer, buffer_length);
|
||||
// It's safe to working on a non reading index
|
||||
auto current_working_index = priv->current_reading_index ^ 1;
|
||||
// Dropped frame, let's free this.
|
||||
if (priv->buffer[current_working_index] != nullptr) {
|
||||
delete[] priv->buffer[current_working_index];
|
||||
}
|
||||
priv->buffer[current_working_index] = copied_data;
|
||||
priv->video_height[current_working_index] = height;
|
||||
priv->video_width[current_working_index] = width;
|
||||
if (!priv->buffer_ready) {
|
||||
priv->buffer_ready = true;
|
||||
fl_texture_registrar_mark_texture_frame_available(
|
||||
priv->texture_registrar,
|
||||
FL_TEXTURE(texture_rgba)
|
||||
);
|
||||
}
|
||||
priv->buffer = copied_data;
|
||||
priv->video_height = height;
|
||||
priv->video_width = width;
|
||||
fl_texture_registrar_mark_texture_frame_available(
|
||||
priv->texture_registrar,
|
||||
FL_TEXTURE(texture_rgba)
|
||||
);
|
||||
}
|
||||
g_mutex_unlock(&priv->mutex);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue