feat: init repo

This commit is contained in:
Kingtous 2023-02-16 15:03:12 +08:00
commit b2bd5910e7
87 changed files with 4335 additions and 0 deletions

47
linux/CMakeLists.txt Normal file
View file

@ -0,0 +1,47 @@
# The Flutter tooling requires that developers have CMake 3.10 or later
# installed. You should not increase this version, as doing so will cause
# the plugin to fail to compile for some customers of the plugin.
cmake_minimum_required(VERSION 3.10)
# Project-level configuration.
set(PROJECT_NAME "texture_rgba_renderer")
project(${PROJECT_NAME} LANGUAGES CXX)
# This value is used when generating builds using this plugin, so it must
# not be changed.
set(PLUGIN_NAME "texture_rgba_renderer_plugin")
# Define the plugin library target. Its name must not be changed (see comment
# on PLUGIN_NAME above).
#
# Any new source files that you add to the plugin should be added here.
add_library(${PLUGIN_NAME} SHARED
"texture_rgba_renderer_plugin.cc"
)
# Apply a standard set of build settings that are configured in the
# application-level CMakeLists.txt. This can be removed for plugins that want
# full control over build settings.
apply_standard_settings(${PLUGIN_NAME})
# Symbols are hidden by default to reduce the chance of accidental conflicts
# between plugins. This should not be removed; any symbols that should be
# exported should be explicitly exported with the FLUTTER_PLUGIN_EXPORT macro.
set_target_properties(${PLUGIN_NAME} PROPERTIES
CXX_VISIBILITY_PRESET hidden)
target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL)
# Source include directories and library dependencies. Add any plugin-specific
# dependencies here.
target_include_directories(${PLUGIN_NAME} INTERFACE
"${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(${PLUGIN_NAME} PRIVATE flutter)
target_link_libraries(${PLUGIN_NAME} PRIVATE PkgConfig::GTK)
# List of absolute paths to libraries that should be bundled with the plugin.
# This list could contain prebuilt libraries, or libraries created by an
# external build triggered from this build file.
set(texture_rgba_renderer_bundled_libraries
""
PARENT_SCOPE
)

View file

@ -0,0 +1,52 @@
#ifndef TEXTURE_RGBA_RENDERER_H
#define TEXTURE_RGBA_RENDERER_H
#include <flutter_linux/flutter_linux.h>
#include <gtk/gtk.h>
#include "texture_rgba_renderer_plugin.h"
struct _TextureRgbaClass {
FlPixelBufferTextureClass parent_class;
};
struct TextureRgbaPrivate
{
int64_t texture_id = 0;
uint8_t* buffer = nullptr;
int32_t video_width = 0;
int32_t video_height = 0;
};
/// Constructor.
G_DECLARE_DERIVABLE_TYPE(TextureRgba, texture_rgba, TEXTURE_RGBA_RENDERER, TEXTURE_RGBA, FlPixelBufferTexture)
/// Add private data.
G_DEFINE_TYPE_WITH_CODE(TextureRgba, texture_rgba,
fl_pixel_buffer_texture_get_type(),
G_ADD_PRIVATE(TextureRgba))
static gboolean texture_rgba_copy_pixels(FlPixelBufferTexture* texture,
const uint8_t** out_buffer,
uint32_t* width, uint32_t* height,
GError** error) {
auto texture_rgba_private =
(TextureRgbaPrivate*)texture_rgba_get_instance_private(
TEXTURE_RGBA_RENDERER_TEXTURE_RGBA(texture));
*out_buffer = texture_rgba_private->buffer;
*width = texture_rgba_private->video_width;
*height = texture_rgba_private->video_height;
return TRUE;
}
static TextureRgba* texture_rgba_new() {
return TEXTURE_RGBA_RENDERER_TEXTURE_RGBA(g_object_new(texture_rgba_get_type(), nullptr));
}
static void texture_rgba_class_init(TextureRgbaClass* klass) {
FL_PIXEL_BUFFER_TEXTURE_CLASS(klass)->copy_pixels = texture_rgba_copy_pixels;
}
static void texture_rgba_init(TextureRgba* self) {}
#endif

View file

@ -0,0 +1,26 @@
#ifndef FLUTTER_PLUGIN_TEXTURE_RGBA_RENDERER_PLUGIN_H_
#define FLUTTER_PLUGIN_TEXTURE_RGBA_RENDERER_PLUGIN_H_
#include <flutter_linux/flutter_linux.h>
G_BEGIN_DECLS
#ifdef FLUTTER_PLUGIN_IMPL
#define FLUTTER_PLUGIN_EXPORT __attribute__((visibility("default")))
#else
#define FLUTTER_PLUGIN_EXPORT
#endif
typedef struct _TextureRgbaRendererPlugin TextureRgbaRendererPlugin;
typedef struct {
GObjectClass parent_class;
} TextureRgbaRendererPluginClass;
FLUTTER_PLUGIN_EXPORT GType texture_rgba_renderer_plugin_get_type();
FLUTTER_PLUGIN_EXPORT void texture_rgba_renderer_plugin_register_with_registrar(
FlPluginRegistrar* registrar);
G_END_DECLS
#endif // FLUTTER_PLUGIN_TEXTURE_RGBA_RENDERER_PLUGIN_H_

View file

@ -0,0 +1,117 @@
#include "include/texture_rgba_renderer/texture_rgba_renderer_plugin.h"
#include "include/texture_rgba_renderer/texture_rgba_renderer.h"
#include <flutter_linux/flutter_linux.h>
#include <gtk/gtk.h>
#include <sys/utsname.h>
#define TEXTURE_RGBA_RENDERER_PLUGIN(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), texture_rgba_renderer_plugin_get_type(), \
TextureRgbaRendererPlugin))
#include <cstring>
#include <unordered_map>
struct _TextureRgbaRendererPlugin {
GObject parent_instance;
FlTextureRegistrar* texture_registrar;
};
std::unordered_map<int64_t, TextureRgba*> g_renderer_map;
G_DEFINE_TYPE(TextureRgbaRendererPlugin, texture_rgba_renderer_plugin, g_object_get_type())
// Called when a method call is received from Flutter.
static void texture_rgba_renderer_plugin_handle_method_call(
TextureRgbaRendererPlugin* self,
FlMethodCall* method_call) {
g_autoptr(FlMethodResponse) response = nullptr;
const gchar* method = fl_method_call_get_name(method_call);
if (strcmp(method, "getPlatformVersion") == 0) {
struct utsname uname_data = {};
uname(&uname_data);
g_autofree gchar *version = g_strdup_printf("Linux %s", uname_data.version);
g_autoptr(FlValue) result = fl_value_new_string(version);
response = FL_METHOD_RESPONSE(fl_method_success_response_new(result));
} else if (strcmp(method, "createTexture") == 0) {
auto args = fl_method_call_get_args(method_call);
int64_t key = fl_value_get_int(fl_value_lookup_string(args, "key"));
if (g_renderer_map.find(key) != g_renderer_map.end()) {
response = FL_METHOD_RESPONSE(fl_method_success_response_new(fl_value_new_int(-1)));
} else {
auto texture_rgba = texture_rgba_new();
auto texture_rgba_private = (TextureRgbaPrivate*) texture_rgba_get_instance_private(texture_rgba);
texture_rgba_private->texture_id = reinterpret_cast<int64_t>(FL_TEXTURE(texture_rgba));
FL_PIXEL_BUFFER_TEXTURE_GET_CLASS(texture_rgba)->copy_pixels =
texture_rgba_copy_pixels;
g_renderer_map[key] = texture_rgba;
// register to
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_rgba_private->texture_id)));
}
} else if (strcmp(method, "closeTexture") == 0) {
auto args = fl_method_call_get_args(method_call);
int64_t key = fl_value_get_int(fl_value_lookup_string(args, "key"));
if (g_renderer_map.find(key) != g_renderer_map.end()){
fl_texture_registrar_unregister_texture(self->texture_registrar, FL_TEXTURE(g_renderer_map[key]));
}
g_renderer_map.erase(key);
response = FL_METHOD_RESPONSE(fl_method_success_response_new(fl_value_new_bool(true)));
} else if (strcmp(method, "onRgba") == 0) {
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 width = fl_value_get_int(fl_value_lookup_string(args, "width"));
auto height = fl_value_get_int(fl_value_lookup_string(args, "height"));
auto texture_rgba = g_renderer_map[key];
auto texture_rgba_private = (TextureRgbaPrivate*) texture_rgba_get_instance_private(g_renderer_map[key]);
texture_rgba_private->buffer = buffer;
texture_rgba_private->video_height = height;
texture_rgba_private->video_width = width;
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)))
));
} else {
response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
}
fl_method_call_respond(method_call, response, nullptr);
}
static void texture_rgba_renderer_plugin_dispose(GObject* object) {
G_OBJECT_CLASS(texture_rgba_renderer_plugin_parent_class)->dispose(object);
}
static void texture_rgba_renderer_plugin_class_init(TextureRgbaRendererPluginClass* klass) {
G_OBJECT_CLASS(klass)->dispose = texture_rgba_renderer_plugin_dispose;
}
static void texture_rgba_renderer_plugin_init(TextureRgbaRendererPlugin* self) {}
static void method_call_cb(FlMethodChannel* channel, FlMethodCall* method_call,
gpointer user_data) {
TextureRgbaRendererPlugin* plugin = TEXTURE_RGBA_RENDERER_PLUGIN(user_data);
texture_rgba_renderer_plugin_handle_method_call(plugin, method_call);
}
void texture_rgba_renderer_plugin_register_with_registrar(FlPluginRegistrar* registrar) {
TextureRgbaRendererPlugin* plugin = TEXTURE_RGBA_RENDERER_PLUGIN(
g_object_new(texture_rgba_renderer_plugin_get_type(), nullptr));
g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
g_autoptr(FlMethodChannel) channel =
fl_method_channel_new(fl_plugin_registrar_get_messenger(registrar),
"texture_rgba_renderer",
FL_METHOD_CODEC(codec));
plugin->texture_registrar = fl_plugin_registrar_get_texture_registrar(registrar);
fl_method_channel_set_method_call_handler(channel, method_call_cb,
g_object_ref(plugin),
g_object_unref);
g_object_unref(plugin);
}