fix: linux read on write

This commit is contained in:
Kingtous 2023-02-21 16:30:15 +08:00
parent 69a51033bf
commit 686d6edaa5
3 changed files with 83 additions and 71 deletions

View file

@ -72,7 +72,6 @@ class _MyAppState extends State<MyApp> {
setState(() { setState(() {
time = t2 - t1; time = t2 - t1;
}); });
if (!res) { if (!res) {
debugPrint("WARN: render failed"); debugPrint("WARN: render failed");
} }
@ -121,41 +120,40 @@ class _MyAppState extends State<MyApp> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
home: Scaffold( home: Scaffold(
body: Center( body: Column(
child: SingleChildScrollView( crossAxisAlignment: CrossAxisAlignment.center,
child: Column( mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center, children: [
mainAxisAlignment: MainAxisAlignment.center, Expanded(child: textureId == -1
children: [ ? const Offstage()
textureId == -1 : Padding(
? const Offstage() padding: const EdgeInsets.all(8.0),
: Container( child: Container(
alignment: Alignment.center, alignment: Alignment.center,
decoration: const BoxDecoration(color: Colors.blue), decoration: const BoxDecoration(color: Colors.blue),
// decoration: const BoxDecoration(color: Colors.black), // decoration: const BoxDecoration(color: Colors.black),
width: width.toDouble(), // width: width.toDouble(),
height: height.toDouble(), // height: height.toDouble(),
child: Texture(textureId: textureId)), child: Texture(textureId: textureId)),
Text( ),),
"texture id: $textureId, texture memory address: ${texturePtr.toRadixString(16)}"), Text(
TextButton.icon( "texture id: $textureId, texture memory address: ${texturePtr.toRadixString(16)}"),
label: const Text("play with texture (method channel API)"), TextButton.icon(
icon: const Icon(Icons.play_arrow), label: const Text("play with texture (method channel API)"),
onPressed: () => start(0), icon: const Icon(Icons.play_arrow),
), onPressed: () => start(0),
TextButton.icon(
label: const Text("play with texture (native API, faster)"),
icon: const Icon(Icons.play_arrow),
onPressed: () => start(1),
),
Text(
"Current mode: ${method == 0 ? 'Method Channel API' : 'Native API'}"),
time != 0
? Text("FPS: ${1000000 ~/ time} fps")
: const Offstage()
],
), ),
), TextButton.icon(
label: const Text("play with texture (native API, faster)"),
icon: const Icon(Icons.play_arrow),
onPressed: () => start(1),
),
Text(
"Current mode: ${method == 0 ? 'Method Channel API' : 'Native API'}"),
time != 0
? Text("FPS: ${1000000 ~/ time} fps")
: const Offstage()
],
), ),
), ),
); );

View file

@ -7,6 +7,8 @@
#include "texture_rgba_renderer_plugin.h" #include "texture_rgba_renderer_plugin.h"
#include <vector>
struct _TextureRgbaClass struct _TextureRgbaClass
{ {
FlTextureGLClass parent_class; FlTextureGLClass parent_class;
@ -16,10 +18,11 @@ struct TextureRgbaPrivate
{ {
FlTextureRegistrar* texture_registrar = nullptr; FlTextureRegistrar* texture_registrar = nullptr;
GLuint texture_id = 0; GLuint texture_id = 0;
const uint8_t *buffer = nullptr; const uint8_t *buffer[2] = {nullptr, nullptr};
size_t buffer_length = 0; size_t current_reading_index = 0;
int32_t video_width = 0; bool buffer_ready = false;
int32_t video_height = 0; int32_t video_width[2] = {0, 0};
int32_t video_height[2] = {0, 0};
GMutex mutex; GMutex mutex;
}; };
@ -39,7 +42,13 @@ static gboolean texture_rgba_populate(FlTextureGL *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);
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);
@ -52,16 +61,19 @@ 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, self->video_height, 0, GL_BGRA, glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, self->video_width[index], self->video_height[index], 0, GL_BGRA,
GL_UNSIGNED_BYTE, self->buffer); GL_UNSIGNED_BYTE, self->buffer[index]);
delete[] self->buffer; // Now we can release the self->buffer.
self->buffer = nullptr; if (self->buffer[index] != nullptr) {
g_mutex_unlock(&self->mutex); delete[] self->buffer[index];
self->buffer[index] = nullptr;
}
self->buffer_ready = false;
*target = GL_TEXTURE_2D; *target = GL_TEXTURE_2D;
*name = self->texture_id; *name = self->texture_id;
*width = self->video_width; *width = self->video_width[index];
*height = self->video_height; *height = self->video_height[index];
g_mutex_unlock(&self->mutex);
return TRUE; return TRUE;
} }

View file

@ -12,6 +12,8 @@
#include <cstring> #include <cstring>
#include <unordered_map> #include <unordered_map>
void FlutterRgbaRendererPluginOnRgba(void *texture_rgba_ptr, const uint8_t *buffer, int width, int height);
struct _TextureRgbaRendererPlugin struct _TextureRgbaRendererPlugin
{ {
GObject parent_instance; 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 args = fl_method_call_get_args(method_call);
auto key = fl_value_get_int(fl_value_lookup_string(args, "key")); 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 = 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 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 width = fl_value_get_int(fl_value_lookup_string(args, "width")); auto width = fl_value_get_int(fl_value_lookup_string(args, "width"));
auto height = fl_value_get_int(fl_value_lookup_string(args, "height")); 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 = (*self->g_renderer_map)[key];
auto texture_rgba_private = (TextureRgbaPrivate *)texture_rgba_get_instance_private((*self->g_renderer_map)[key]); FlutterRgbaRendererPluginOnRgba((void*)texture_rgba, data, width, height);
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);
response = FL_METHOD_RESPONSE(fl_method_success_response_new( 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))))); 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); 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,
if (priv->texture_id != 0) { if (priv->texture_id != 0) {
// copy data to the texture. // copy data to the texture.
priv->buffer_length = 4 * width * height; auto buffer_length = 4 * width * height;
auto copied_data = new uint8_t[priv->buffer_length]; auto copied_data = new uint8_t[buffer_length];
memcpy(copied_data, buffer, priv->buffer_length); memcpy(copied_data, buffer, buffer_length);
if (priv->buffer != nullptr) { // It's safe to working on a non reading index
delete[] priv->buffer; 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); g_mutex_unlock(&priv->mutex);
} }