opt: hardware accleration texture on Linux

This commit is contained in:
Kingtous 2023-02-16 16:30:11 +08:00
parent 91b1c10758
commit 3d549cb70b
14 changed files with 91 additions and 70 deletions

View file

@ -3,48 +3,70 @@
#include <flutter_linux/flutter_linux.h>
#include <gtk/gtk.h>
#include <epoxy/gl.h>
#include "texture_rgba_renderer_plugin.h"
struct _TextureRgbaClass {
FlPixelBufferTextureClass parent_class;
struct _TextureRgbaClass
{
FlTextureGLClass parent_class;
};
struct TextureRgbaPrivate
{
int64_t texture_id = 0;
uint8_t* buffer = nullptr;
GLuint 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)
G_DECLARE_DERIVABLE_TYPE(TextureRgba, texture_rgba, TEXTURE_RGBA_RENDERER, TEXTURE_RGBA, FlTextureGL)
/// Add private data.
G_DEFINE_TYPE_WITH_CODE(TextureRgba, texture_rgba,
fl_pixel_buffer_texture_get_type(),
fl_texture_gl_get_type(),
G_ADD_PRIVATE(TextureRgba))
static gboolean texture_rgba_populate(FlTextureGL *texture,
uint32_t *target,
uint32_t *name,
uint32_t *width,
uint32_t *height,
GError **error)
{
TextureRgbaPrivate *self = (TextureRgbaPrivate *)
texture_rgba_get_instance_private(TEXTURE_RGBA_RENDERER_TEXTURE_RGBA(texture));
if (self->texture_id == 0)
{
glGenTextures(1, &self->texture_id);
glBindTexture(GL_TEXTURE_2D, self->texture_id);
// further configuration here.
}
else
{
glBindTexture(GL_TEXTURE_2D, self->texture_id);
}
// 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_RGBA,
GL_UNSIGNED_BYTE, self->buffer);
*target = GL_TEXTURE_2D;
*name = self->texture_id;
*width = self->video_width;
*height = self->video_height;
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() {
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_class_init(TextureRgbaClass *klass)
{
FL_TEXTURE_GL_GET_CLASS(klass)->populate = texture_rgba_populate;
}
static void texture_rgba_init(TextureRgba* self) {}

View file

@ -41,15 +41,14 @@ static void texture_rgba_renderer_plugin_handle_method_call(
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_autoptr(TextureRgba) texture_rgba = texture_rgba_new();
auto texture_id = reinterpret_cast<int64_t>(FL_TEXTURE(texture_rgba));
FL_TEXTURE_GL_GET_CLASS(texture_rgba)->populate =
texture_rgba_populate;
g_renderer_map[key] = texture_rgba;
// register to
// Register to the registrar.
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)));
response = FL_METHOD_RESPONSE(fl_method_success_response_new(fl_value_new_int(texture_id)));
}
} else if (strcmp(method, "closeTexture") == 0) {
auto args = fl_method_call_get_args(method_call);