opt: try to fix the memory corrupt issue on Linux

This commit is contained in:
Kingtous 2023-02-28 15:37:20 +08:00
parent c3297dfd05
commit c37336cd9b
6 changed files with 109 additions and 64 deletions

View file

@ -18,11 +18,11 @@ struct TextureRgbaPrivate
{
FlTextureRegistrar* texture_registrar = nullptr;
GLuint texture_id = 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};
const uint8_t *buffer = nullptr;
gboolean buffer_ready = FALSE;
gboolean terminated = FALSE;
gint32 video_width = 0;
gint32 video_height = 0;
GMutex mutex;
};
@ -33,6 +33,24 @@ G_DEFINE_TYPE_WITH_CODE(TextureRgba, texture_rgba,
fl_texture_gl_get_type(),
G_ADD_PRIVATE(TextureRgba))
static void texture_rgba_terminate(TextureRgba* texture) {
TextureRgbaPrivate *self = (TextureRgbaPrivate *)
texture_rgba_get_instance_private(TEXTURE_RGBA_RENDERER_TEXTURE_RGBA(texture));
g_mutex_lock(&self->mutex);
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);
}
static gboolean texture_rgba_is_terminate(TextureRgba* texture) {
TextureRgbaPrivate *self = (TextureRgbaPrivate *)
texture_rgba_get_instance_private(TEXTURE_RGBA_RENDERER_TEXTURE_RGBA(texture));
return g_atomic_int_get(&self->terminated);
}
static gboolean texture_rgba_populate(FlTextureGL *texture,
uint32_t *target,
uint32_t *name,
@ -44,16 +62,12 @@ static gboolean texture_rgba_populate(FlTextureGL *texture,
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);
glBindTexture(GL_TEXTURE_2D, self->texture_id);
// further configuration here.
g_atomic_pointer_set(&self->buffer, nullptr);
}
else
{
@ -61,18 +75,20 @@ 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[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;
if (g_atomic_pointer_get(&self->buffer)) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, g_atomic_int_get(&self->video_width), g_atomic_int_get(&self->video_height), 0, GL_BGRA,
GL_UNSIGNED_BYTE, g_atomic_pointer_get(&self->buffer));
}
self->buffer_ready = false;
// Now we can release the self->buffer.
if (g_atomic_pointer_get(&self->buffer) != nullptr) {
delete[] self->buffer;
g_atomic_pointer_set(&self->buffer, nullptr);
}
g_atomic_int_set(&self->buffer_ready, FALSE);
*target = GL_TEXTURE_2D;
*name = self->texture_id;
*width = self->video_width[index];
*height = self->video_height[index];
*width = g_atomic_int_get(&self->video_width);
*height = g_atomic_int_get(&self->video_height);
g_mutex_unlock(&self->mutex);
return TRUE;
}

View file

@ -42,7 +42,7 @@ static void texture_rgba_renderer_plugin_handle_method_call(
}
else
{
g_autoptr(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));
FL_TEXTURE_GL_GET_CLASS(texture_rgba)->populate =
texture_rgba_populate;
@ -59,6 +59,7 @@ static void texture_rgba_renderer_plugin_handle_method_call(
if (g_renderer_map.find(key) != g_renderer_map.end())
{
fl_texture_registrar_unregister_texture(self->texture_registrar, FL_TEXTURE(g_renderer_map[key]));
texture_rgba_terminate(g_renderer_map[key]);
g_renderer_map.erase(key);
}
response = FL_METHOD_RESPONSE(fl_method_success_response_new(fl_value_new_bool(true)));
@ -141,32 +142,24 @@ 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) {
fprintf(stderr, "onRgba called\n");
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) {
if (priv->texture_id != 0 && !g_atomic_int_get(&priv->buffer_ready) && !texture_rgba_is_terminate(texture_rgba)) {
// 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);
// 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)
);
}
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);
fl_texture_registrar_mark_texture_frame_available(
priv->texture_registrar,
FL_TEXTURE(texture_rgba)
);
}
g_mutex_unlock(&priv->mutex);
}