fix: linux syn

This commit is contained in:
Kingtous 2023-02-18 19:10:08 +08:00
parent 0f59fef831
commit 3e42db31d1
5 changed files with 111 additions and 37 deletions

View file

@ -36,6 +36,7 @@ class _MyAppState extends State<MyApp> {
if (textureId != -1) { if (textureId != -1) {
debugPrint("Texture register success, textureId=$textureId"); debugPrint("Texture register success, textureId=$textureId");
_textureRgbaRendererPlugin.getTexturePtr(key).then((value) { _textureRgbaRendererPlugin.getTexturePtr(key).then((value) {
debugPrint("texture ptr: $value");
setState(() { setState(() {
texturePtr = value; texturePtr = value;
}); });

View file

@ -41,8 +41,7 @@ class MethodChannelTextureRgbaRenderer extends TextureRgbaRendererPlatform {
@override @override
Future<int> getTexturePtr(int key) async { Future<int> getTexturePtr(int key) async {
// TODO: currently only supports Windows. if (Platform.isMacOS) {
if (!Platform.isWindows) {
return -1; return -1;
} }
final ptr = final ptr =

View file

@ -14,10 +14,13 @@ struct _TextureRgbaClass
struct TextureRgbaPrivate struct TextureRgbaPrivate
{ {
FlTextureRegistrar* texture_registrar = nullptr;
GLuint texture_id = 0; GLuint texture_id = 0;
uint8_t *buffer = nullptr; const uint8_t *buffer = nullptr;
size_t buffer_length = 0;
int32_t video_width = 0; int32_t video_width = 0;
int32_t video_height = 0; int32_t video_height = 0;
GMutex mutex;
}; };
/// Constructor. /// Constructor.
@ -36,6 +39,7 @@ 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);
if (self->texture_id == 0) if (self->texture_id == 0)
{ {
glGenTextures(1, &self->texture_id); glGenTextures(1, &self->texture_id);
@ -50,6 +54,7 @@ static gboolean texture_rgba_populate(FlTextureGL *texture,
// 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_RGBA, glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, self->video_width, self->video_height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, self->buffer); GL_UNSIGNED_BYTE, self->buffer);
g_mutex_unlock(&self->mutex);
*target = GL_TEXTURE_2D; *target = GL_TEXTURE_2D;
*name = self->texture_id; *name = self->texture_id;
@ -59,9 +64,12 @@ static gboolean texture_rgba_populate(FlTextureGL *texture,
return TRUE; return TRUE;
} }
static TextureRgba *texture_rgba_new() static TextureRgba *texture_rgba_new(FlTextureRegistrar* registrar)
{ {
return TEXTURE_RGBA_RENDERER_TEXTURE_RGBA(g_object_new(texture_rgba_get_type(), nullptr)); auto texture = TEXTURE_RGBA_RENDERER_TEXTURE_RGBA(g_object_new(texture_rgba_get_type(), nullptr));
((TextureRgbaPrivate*)texture_rgba_get_instance_private(texture))->texture_registrar = registrar;
g_mutex_init(&((TextureRgbaPrivate*)texture_rgba_get_instance_private(texture))->mutex);
return texture;
} }
static void texture_rgba_class_init(TextureRgbaClass *klass) static void texture_rgba_class_init(TextureRgbaClass *klass)

View file

@ -21,6 +21,9 @@ FLUTTER_PLUGIN_EXPORT GType texture_rgba_renderer_plugin_get_type();
FLUTTER_PLUGIN_EXPORT void texture_rgba_renderer_plugin_register_with_registrar( FLUTTER_PLUGIN_EXPORT void texture_rgba_renderer_plugin_register_with_registrar(
FlPluginRegistrar* registrar); FlPluginRegistrar* registrar);
// Keep the same symbol name with Windows.
FLUTTER_PLUGIN_EXPORT void FlutterRgbaRendererPluginOnRgba(void *texture_rgba, const uint8_t *buffer, int width, int height);
G_END_DECLS G_END_DECLS
#endif // FLUTTER_PLUGIN_TEXTURE_RGBA_RENDERER_PLUGIN_H_ #endif // FLUTTER_PLUGIN_TEXTURE_RGBA_RENDERER_PLUGIN_H_

View file

@ -12,53 +12,65 @@
#include <cstring> #include <cstring>
#include <unordered_map> #include <unordered_map>
struct _TextureRgbaRendererPlugin { struct _TextureRgbaRendererPlugin
{
GObject parent_instance; GObject parent_instance;
FlTextureRegistrar* texture_registrar; FlTextureRegistrar *texture_registrar;
std::unordered_map<int64_t, TextureRgba *> *g_renderer_map;
}; };
std::unordered_map<int64_t, TextureRgba*> g_renderer_map;
G_DEFINE_TYPE(TextureRgbaRendererPlugin, texture_rgba_renderer_plugin, g_object_get_type()) G_DEFINE_TYPE(TextureRgbaRendererPlugin, texture_rgba_renderer_plugin, g_object_get_type())
// Called when a method call is received from Flutter. // Called when a method call is received from Flutter.
static void texture_rgba_renderer_plugin_handle_method_call( static void texture_rgba_renderer_plugin_handle_method_call(
TextureRgbaRendererPlugin* self, TextureRgbaRendererPlugin *self,
FlMethodCall* method_call) { FlMethodCall *method_call)
{
g_autoptr(FlMethodResponse) response = nullptr; g_autoptr(FlMethodResponse) response = nullptr;
const gchar* method = fl_method_call_get_name(method_call); const gchar *method = fl_method_call_get_name(method_call);
if (strcmp(method, "getPlatformVersion") == 0) { if (strcmp(method, "getPlatformVersion") == 0)
{
struct utsname uname_data = {}; struct utsname uname_data = {};
uname(&uname_data); uname(&uname_data);
g_autofree gchar *version = g_strdup_printf("Linux %s", uname_data.version); g_autofree gchar *version = g_strdup_printf("Linux %s", uname_data.version);
g_autoptr(FlValue) result = fl_value_new_string(version); g_autoptr(FlValue) result = fl_value_new_string(version);
response = FL_METHOD_RESPONSE(fl_method_success_response_new(result)); response = FL_METHOD_RESPONSE(fl_method_success_response_new(result));
} else if (strcmp(method, "createTexture") == 0) { }
else if (strcmp(method, "createTexture") == 0)
{
auto args = fl_method_call_get_args(method_call); auto args = fl_method_call_get_args(method_call);
int64_t key = fl_value_get_int(fl_value_lookup_string(args, "key")); int64_t key = fl_value_get_int(fl_value_lookup_string(args, "key"));
if (g_renderer_map.find(key) != g_renderer_map.end()) { if (self->g_renderer_map->find(key) != self->g_renderer_map->end())
{
response = FL_METHOD_RESPONSE(fl_method_success_response_new(fl_value_new_int(-1))); response = FL_METHOD_RESPONSE(fl_method_success_response_new(fl_value_new_int(-1)));
} else { }
g_autoptr(TextureRgba) texture_rgba = texture_rgba_new(); else
{
g_autoptr(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;
g_renderer_map[key] = texture_rgba; self->g_renderer_map->insert(std::make_pair(key, texture_rgba));
// Register to the registrar. // Register to the registrar.
fl_texture_registrar_register_texture(self->texture_registrar, FL_TEXTURE(texture_rgba)); fl_texture_registrar_register_texture(self->texture_registrar, FL_TEXTURE(texture_rgba));
response = FL_METHOD_RESPONSE(fl_method_success_response_new(fl_value_new_int(texture_id))); response = FL_METHOD_RESPONSE(fl_method_success_response_new(fl_value_new_int(texture_id)));
} }
} else if (strcmp(method, "closeTexture") == 0) { }
else if (strcmp(method, "closeTexture") == 0)
{
auto args = fl_method_call_get_args(method_call); auto args = fl_method_call_get_args(method_call);
int64_t key = fl_value_get_int(fl_value_lookup_string(args, "key")); int64_t key = fl_value_get_int(fl_value_lookup_string(args, "key"));
if (g_renderer_map.find(key) != g_renderer_map.end()){ if (self->g_renderer_map->find(key) != self->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((*self->g_renderer_map)[key]));
} }
g_renderer_map.erase(key); self->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)));
} else if (strcmp(method, "onRgba") == 0) { }
else if (strcmp(method, "onRgba") == 0)
{
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"));
@ -67,39 +79,67 @@ static void texture_rgba_renderer_plugin_handle_method_call(
memcpy(buffer, data, 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"));
auto texture_rgba = g_renderer_map[key]; auto texture_rgba = (*self->g_renderer_map)[key];
auto texture_rgba_private = (TextureRgbaPrivate*) texture_rgba_get_instance_private(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);
texture_rgba_private->buffer = buffer; texture_rgba_private->buffer = buffer;
texture_rgba_private->video_height = height; texture_rgba_private->video_height = height;
texture_rgba_private->video_width = width; 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)))));
)); }
} else { else if (strcmp(method, "getTexturePtr") == 0)
{
auto args = fl_method_call_get_args(method_call);
auto key = fl_value_get_int(fl_value_lookup_string(args, "key"));
if (self->g_renderer_map->find(key) == self->g_renderer_map->end())
{
response = FL_METHOD_RESPONSE(fl_method_success_response_new(
fl_value_new_int(0)));
}
else
{
// Return an address.
size_t rgba = reinterpret_cast<size_t>((void *)(*self->g_renderer_map)[key]);
response = FL_METHOD_RESPONSE(fl_method_success_response_new(
fl_value_new_int(rgba)));
}
}
else
{
response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new()); response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
} }
fl_method_call_respond(method_call, response, nullptr); fl_method_call_respond(method_call, response, nullptr);
} }
static void texture_rgba_renderer_plugin_dispose(GObject* object) { static void texture_rgba_renderer_plugin_dispose(GObject *object)
{
TEXTURE_RGBA_RENDERER_PLUGIN(object)->g_renderer_map->clear();
G_OBJECT_CLASS(texture_rgba_renderer_plugin_parent_class)->dispose(object); G_OBJECT_CLASS(texture_rgba_renderer_plugin_parent_class)->dispose(object);
} }
static void texture_rgba_renderer_plugin_class_init(TextureRgbaRendererPluginClass* klass) { static void texture_rgba_renderer_plugin_class_init(TextureRgbaRendererPluginClass *klass)
{
G_OBJECT_CLASS(klass)->dispose = texture_rgba_renderer_plugin_dispose; G_OBJECT_CLASS(klass)->dispose = texture_rgba_renderer_plugin_dispose;
} }
static void texture_rgba_renderer_plugin_init(TextureRgbaRendererPlugin* self) {} static void texture_rgba_renderer_plugin_init(TextureRgbaRendererPlugin *self)
{
self->g_renderer_map = new std::unordered_map<int64_t, TextureRgba *>();
}
static void method_call_cb(FlMethodChannel* channel, FlMethodCall* method_call, static void method_call_cb(FlMethodChannel *channel, FlMethodCall *method_call,
gpointer user_data) { gpointer user_data)
TextureRgbaRendererPlugin* plugin = TEXTURE_RGBA_RENDERER_PLUGIN(user_data); {
TextureRgbaRendererPlugin *plugin = TEXTURE_RGBA_RENDERER_PLUGIN(user_data);
texture_rgba_renderer_plugin_handle_method_call(plugin, method_call); texture_rgba_renderer_plugin_handle_method_call(plugin, method_call);
} }
void texture_rgba_renderer_plugin_register_with_registrar(FlPluginRegistrar* registrar) { void texture_rgba_renderer_plugin_register_with_registrar(FlPluginRegistrar *registrar)
TextureRgbaRendererPlugin* plugin = TEXTURE_RGBA_RENDERER_PLUGIN( {
TextureRgbaRendererPlugin *plugin = TEXTURE_RGBA_RENDERER_PLUGIN(
g_object_new(texture_rgba_renderer_plugin_get_type(), nullptr)); g_object_new(texture_rgba_renderer_plugin_get_type(), nullptr));
g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new(); g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
@ -114,3 +154,26 @@ void texture_rgba_renderer_plugin_register_with_registrar(FlPluginRegistrar* reg
g_object_unref(plugin); g_object_unref(plugin);
} }
extern "C" {
void FlutterRgbaRendererPluginOnRgba(void *texture_rgba_ptr, const uint8_t *buffer, int width, int height) {
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);
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);
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);
}
};