diff --git a/example/lib/main.dart b/example/lib/main.dart index 130c01d..161adaf 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -72,7 +72,6 @@ class _MyAppState extends State { setState(() { time = t2 - t1; }); - if (!res) { debugPrint("WARN: render failed"); } @@ -121,41 +120,40 @@ class _MyAppState extends State { Widget build(BuildContext context) { return MaterialApp( home: Scaffold( - body: Center( - child: SingleChildScrollView( - child: Column( - crossAxisAlignment: CrossAxisAlignment.center, - mainAxisAlignment: MainAxisAlignment.center, - children: [ - textureId == -1 - ? const Offstage() - : Container( - alignment: Alignment.center, - decoration: const BoxDecoration(color: Colors.blue), - // decoration: const BoxDecoration(color: Colors.black), - width: width.toDouble(), - height: height.toDouble(), - child: Texture(textureId: textureId)), - Text( - "texture id: $textureId, texture memory address: ${texturePtr.toRadixString(16)}"), - TextButton.icon( - label: const Text("play with texture (method channel API)"), - 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() - ], + body: Column( + crossAxisAlignment: CrossAxisAlignment.center, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Expanded(child: textureId == -1 + ? const Offstage() + : Padding( + padding: const EdgeInsets.all(8.0), + child: Container( + alignment: Alignment.center, + decoration: const BoxDecoration(color: Colors.blue), + // decoration: const BoxDecoration(color: Colors.black), + // width: width.toDouble(), + // height: height.toDouble(), + child: Texture(textureId: textureId)), + ),), + Text( + "texture id: $textureId, texture memory address: ${texturePtr.toRadixString(16)}"), + TextButton.icon( + label: const Text("play with texture (method channel API)"), + 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() + ], ), ), ); diff --git a/linux/include/texture_rgba_renderer/texture_rgba_renderer.h b/linux/include/texture_rgba_renderer/texture_rgba_renderer.h index 078da03..e5f61b6 100644 --- a/linux/include/texture_rgba_renderer/texture_rgba_renderer.h +++ b/linux/include/texture_rgba_renderer/texture_rgba_renderer.h @@ -7,6 +7,8 @@ #include "texture_rgba_renderer_plugin.h" +#include + 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; } diff --git a/linux/texture_rgba_renderer_plugin.cc b/linux/texture_rgba_renderer_plugin.cc index 6a36343..657afc8 100644 --- a/linux/texture_rgba_renderer_plugin.cc +++ b/linux/texture_rgba_renderer_plugin.cc @@ -12,6 +12,8 @@ #include #include +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); }