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

33
.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,33 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/example/build/linux/x64/debug/bundle/texture_rgba_renderer_example",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}

View file

@ -1,3 +1,6 @@
## 0.0.9
* try fixing the memory corrupt issue on Linux.
## 0.0.7 ## 0.0.7
* add the FFI function and ptr getter for macOS. * add the FFI function and ptr getter for macOS.

View file

@ -6,7 +6,7 @@ packages:
description: description:
name: async name: async
sha256: bfe67ef28df125b7dddcea62755991f807aa39a2492a23e1550161692950bbe0 sha256: bfe67ef28df125b7dddcea62755991f807aa39a2492a23e1550161692950bbe0
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "2.10.0" version: "2.10.0"
boolean_selector: boolean_selector:
@ -14,7 +14,7 @@ packages:
description: description:
name: boolean_selector name: boolean_selector
sha256: "6cfb5af12253eaf2b368f07bacc5a80d1301a071c73360d746b7f2e32d762c66" sha256: "6cfb5af12253eaf2b368f07bacc5a80d1301a071c73360d746b7f2e32d762c66"
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "2.1.1" version: "2.1.1"
characters: characters:
@ -22,7 +22,7 @@ packages:
description: description:
name: characters name: characters
sha256: e6a326c8af69605aec75ed6c187d06b349707a27fbff8222ca9cc2cff167975c sha256: e6a326c8af69605aec75ed6c187d06b349707a27fbff8222ca9cc2cff167975c
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "1.2.1" version: "1.2.1"
clock: clock:
@ -30,7 +30,7 @@ packages:
description: description:
name: clock name: clock
sha256: cb6d7f03e1de671e34607e909a7213e31d7752be4fb66a86d29fe1eb14bfb5cf sha256: cb6d7f03e1de671e34607e909a7213e31d7752be4fb66a86d29fe1eb14bfb5cf
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "1.1.1" version: "1.1.1"
collection: collection:
@ -38,7 +38,7 @@ packages:
description: description:
name: collection name: collection
sha256: cfc915e6923fe5ce6e153b0723c753045de46de1b4d63771530504004a45fae0 sha256: cfc915e6923fe5ce6e153b0723c753045de46de1b4d63771530504004a45fae0
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "1.17.0" version: "1.17.0"
cupertino_icons: cupertino_icons:
@ -46,7 +46,7 @@ packages:
description: description:
name: cupertino_icons name: cupertino_icons
sha256: e35129dc44c9118cee2a5603506d823bab99c68393879edb440e0090d07586be sha256: e35129dc44c9118cee2a5603506d823bab99c68393879edb440e0090d07586be
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "1.0.5" version: "1.0.5"
fake_async: fake_async:
@ -54,7 +54,7 @@ packages:
description: description:
name: fake_async name: fake_async
sha256: "511392330127add0b769b75a987850d136345d9227c6b94c96a04cf4a391bf78" sha256: "511392330127add0b769b75a987850d136345d9227c6b94c96a04cf4a391bf78"
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "1.3.1" version: "1.3.1"
ffi: ffi:
@ -62,7 +62,7 @@ packages:
description: description:
name: ffi name: ffi
sha256: a38574032c5f1dd06c4aee541789906c12ccaab8ba01446e800d9c5b79c4a978 sha256: a38574032c5f1dd06c4aee541789906c12ccaab8ba01446e800d9c5b79c4a978
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "2.0.1" version: "2.0.1"
flutter: flutter:
@ -75,7 +75,7 @@ packages:
description: description:
name: flutter_lints name: flutter_lints
sha256: aeb0b80a8b3709709c9cc496cdc027c5b3216796bc0af0ce1007eaf24464fd4c sha256: aeb0b80a8b3709709c9cc496cdc027c5b3216796bc0af0ce1007eaf24464fd4c
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "2.0.1" version: "2.0.1"
flutter_test: flutter_test:
@ -88,7 +88,7 @@ packages:
description: description:
name: js name: js
sha256: "5528c2f391ededb7775ec1daa69e65a2d61276f7552de2b5f7b8d34ee9fd4ab7" sha256: "5528c2f391ededb7775ec1daa69e65a2d61276f7552de2b5f7b8d34ee9fd4ab7"
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "0.6.5" version: "0.6.5"
lints: lints:
@ -96,7 +96,7 @@ packages:
description: description:
name: lints name: lints
sha256: "5e4a9cd06d447758280a8ac2405101e0e2094d2a1dbdd3756aec3fe7775ba593" sha256: "5e4a9cd06d447758280a8ac2405101e0e2094d2a1dbdd3756aec3fe7775ba593"
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "2.0.1" version: "2.0.1"
matcher: matcher:
@ -104,7 +104,7 @@ packages:
description: description:
name: matcher name: matcher
sha256: "16db949ceee371e9b99d22f88fa3a73c4e59fd0afed0bd25fc336eb76c198b72" sha256: "16db949ceee371e9b99d22f88fa3a73c4e59fd0afed0bd25fc336eb76c198b72"
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "0.12.13" version: "0.12.13"
material_color_utilities: material_color_utilities:
@ -112,7 +112,7 @@ packages:
description: description:
name: material_color_utilities name: material_color_utilities
sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724 sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "0.2.0" version: "0.2.0"
meta: meta:
@ -120,7 +120,7 @@ packages:
description: description:
name: meta name: meta
sha256: "6c268b42ed578a53088d834796959e4a1814b5e9e164f147f580a386e5decf42" sha256: "6c268b42ed578a53088d834796959e4a1814b5e9e164f147f580a386e5decf42"
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "1.8.0" version: "1.8.0"
path: path:
@ -128,17 +128,17 @@ packages:
description: description:
name: path name: path
sha256: db9d4f58c908a4ba5953fcee2ae317c94889433e5024c27ce74a37f94267945b sha256: db9d4f58c908a4ba5953fcee2ae317c94889433e5024c27ce74a37f94267945b
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "1.8.2" version: "1.8.2"
plugin_platform_interface: plugin_platform_interface:
dependency: transitive dependency: transitive
description: description:
name: plugin_platform_interface name: plugin_platform_interface
sha256: dbf0f707c78beedc9200146ad3cb0ab4d5da13c246336987be6940f026500d3a sha256: "6a2128648c854906c53fa8e33986fc0247a1116122f9534dd20e3ab9e16a32bc"
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "2.1.3" version: "2.1.4"
sky_engine: sky_engine:
dependency: transitive dependency: transitive
description: flutter description: flutter
@ -149,7 +149,7 @@ packages:
description: description:
name: source_span name: source_span
sha256: dd904f795d4b4f3b870833847c461801f6750a9fa8e61ea5ac53f9422b31f250 sha256: dd904f795d4b4f3b870833847c461801f6750a9fa8e61ea5ac53f9422b31f250
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "1.9.1" version: "1.9.1"
stack_trace: stack_trace:
@ -157,7 +157,7 @@ packages:
description: description:
name: stack_trace name: stack_trace
sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5 sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "1.11.0" version: "1.11.0"
stream_channel: stream_channel:
@ -165,7 +165,7 @@ packages:
description: description:
name: stream_channel name: stream_channel
sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8" sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8"
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "2.1.1" version: "2.1.1"
string_scanner: string_scanner:
@ -173,7 +173,7 @@ packages:
description: description:
name: string_scanner name: string_scanner
sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde" sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde"
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "1.2.0" version: "1.2.0"
term_glyph: term_glyph:
@ -181,7 +181,7 @@ packages:
description: description:
name: term_glyph name: term_glyph
sha256: a29248a84fbb7c79282b40b8c72a1209db169a2e0542bce341da992fe1bc7e84 sha256: a29248a84fbb7c79282b40b8c72a1209db169a2e0542bce341da992fe1bc7e84
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "1.2.1" version: "1.2.1"
test_api: test_api:
@ -189,7 +189,7 @@ packages:
description: description:
name: test_api name: test_api
sha256: ad540f65f92caa91bf21dfc8ffb8c589d6e4dc0c2267818b4cc2792857706206 sha256: ad540f65f92caa91bf21dfc8ffb8c589d6e4dc0c2267818b4cc2792857706206
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "0.4.16" version: "0.4.16"
texture_rgba_renderer: texture_rgba_renderer:
@ -198,13 +198,13 @@ packages:
path: ".." path: ".."
relative: true relative: true
source: path source: path
version: "0.0.7" version: "0.0.9"
vector_math: vector_math:
dependency: transitive dependency: transitive
description: description:
name: vector_math name: vector_math
sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803" sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803"
url: "https://pub.flutter-io.cn" url: "https://pub.dev"
source: hosted source: hosted
version: "2.1.4" version: "2.1.4"
sdks: sdks:

View file

@ -18,11 +18,11 @@ struct TextureRgbaPrivate
{ {
FlTextureRegistrar* texture_registrar = nullptr; FlTextureRegistrar* texture_registrar = nullptr;
GLuint texture_id = 0; GLuint texture_id = 0;
const uint8_t *buffer[2] = {nullptr, nullptr}; const uint8_t *buffer = nullptr;
size_t current_reading_index = 0; gboolean buffer_ready = FALSE;
bool buffer_ready = false; gboolean terminated = FALSE;
int32_t video_width[2] = {0, 0}; gint32 video_width = 0;
int32_t video_height[2] = {0, 0}; gint32 video_height = 0;
GMutex mutex; GMutex mutex;
}; };
@ -33,6 +33,24 @@ G_DEFINE_TYPE_WITH_CODE(TextureRgba, texture_rgba,
fl_texture_gl_get_type(), fl_texture_gl_get_type(),
G_ADD_PRIVATE(TextureRgba)) 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, static gboolean texture_rgba_populate(FlTextureGL *texture,
uint32_t *target, uint32_t *target,
uint32_t *name, 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)); texture_rgba_get_instance_private(TEXTURE_RGBA_RENDERER_TEXTURE_RGBA(texture));
g_mutex_lock(&self->mutex); 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) if (self->texture_id == 0)
{ {
glGenTextures(1, &self->texture_id); glGenTextures(1, &self->texture_id);
glBindTexture(GL_TEXTURE_2D, self->texture_id); glBindTexture(GL_TEXTURE_2D, self->texture_id);
// further configuration here. // further configuration here.
g_atomic_pointer_set(&self->buffer, nullptr);
} }
else else
{ {
@ -61,18 +75,20 @@ static gboolean texture_rgba_populate(FlTextureGL *texture,
} }
// For example, we render pixel buffer here. // For example, we render pixel buffer here.
// Note that Flutter only accepts textures in GL_RGBA8 format. // 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, if (g_atomic_pointer_get(&self->buffer)) {
GL_UNSIGNED_BYTE, self->buffer[index]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, g_atomic_int_get(&self->video_width), g_atomic_int_get(&self->video_height), 0, GL_BGRA,
// Now we can release the self->buffer. GL_UNSIGNED_BYTE, g_atomic_pointer_get(&self->buffer));
if (self->buffer[index] != nullptr) {
delete[] self->buffer[index];
self->buffer[index] = nullptr;
} }
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; *target = GL_TEXTURE_2D;
*name = self->texture_id; *name = self->texture_id;
*width = self->video_width[index]; *width = g_atomic_int_get(&self->video_width);
*height = self->video_height[index]; *height = g_atomic_int_get(&self->video_height);
g_mutex_unlock(&self->mutex); g_mutex_unlock(&self->mutex);
return TRUE; return TRUE;
} }

View file

@ -42,7 +42,7 @@ static void texture_rgba_renderer_plugin_handle_method_call(
} }
else 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)); auto texture_id = reinterpret_cast<int64_t>(FL_TEXTURE(texture_rgba));
FL_TEXTURE_GL_GET_CLASS(texture_rgba)->populate = FL_TEXTURE_GL_GET_CLASS(texture_rgba)->populate =
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()) if (g_renderer_map.find(key) != g_renderer_map.end())
{ {
fl_texture_registrar_unregister_texture(self->texture_registrar, FL_TEXTURE(g_renderer_map[key])); 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); g_renderer_map.erase(key);
} }
response = FL_METHOD_RESPONSE(fl_method_success_response_new(fl_value_new_bool(true))); 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" { 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) {
fprintf(stderr, "onRgba called\n");
TextureRgba* texture_rgba = TEXTURE_RGBA_RENDERER_TEXTURE_RGBA(texture_rgba_ptr); TextureRgba* texture_rgba = TEXTURE_RGBA_RENDERER_TEXTURE_RGBA(texture_rgba_ptr);
auto priv = (TextureRgbaPrivate *)texture_rgba_get_instance_private(texture_rgba); auto priv = (TextureRgbaPrivate *)texture_rgba_get_instance_private(texture_rgba);
g_mutex_lock(&priv->mutex); g_mutex_lock(&priv->mutex);
// private has registered a texture_id, // 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. // 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);
// It's safe to working on a non reading index // It's safe to working on a non reading index
auto current_working_index = priv->current_reading_index ^ 1; g_atomic_pointer_set(&priv->buffer, copied_data);
// Dropped frame, let's free this. g_atomic_int_set(&priv->video_height, height);
if (priv->buffer[current_working_index] != nullptr) { g_atomic_int_set(&priv->video_width, width);
delete[] priv->buffer[current_working_index]; g_atomic_int_set(&priv->buffer_ready, TRUE);
} fl_texture_registrar_mark_texture_frame_available(
priv->buffer[current_working_index] = copied_data; priv->texture_registrar,
priv->video_height[current_working_index] = height; FL_TEXTURE(texture_rgba)
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_mutex_unlock(&priv->mutex); g_mutex_unlock(&priv->mutex);
} }

View file

@ -1,6 +1,6 @@
name: texture_rgba_renderer name: texture_rgba_renderer
description: A texture helper which brings a high level api to handle BGRA data. description: A texture helper which brings a high level api to handle BGRA data.
version: 0.0.7 version: 0.0.9
homepage: https://github.com/Kingtous/flutter_texture_rgba_renderer homepage: https://github.com/Kingtous/flutter_texture_rgba_renderer
environment: environment: