opt: try to fix the memory corrupt issue on Linux
This commit is contained in:
parent
c3297dfd05
commit
c37336cd9b
6 changed files with 109 additions and 64 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue