Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions external_texture/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:external_texture/external_texture.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
runApp(MyApp());
Expand All @@ -15,11 +15,17 @@ class MyApp extends StatefulWidget {

class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
int? textureID;

@override
void initState() {
super.initState();
initPlatformState();
ExternalTexture.registerTexture().then((id) {
setState(() {
textureID = id;
});
});
}

// Platform messages are asynchronous, so we initialize in an async method.
Expand All @@ -28,8 +34,7 @@ class _MyAppState extends State<MyApp> {
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion =
await ExternalTexture.platformVersion ?? 'Unknown platform version';
platformVersion = await ExternalTexture.platformVersion ?? 'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
Expand All @@ -52,7 +57,7 @@ class _MyAppState extends State<MyApp> {
title: const Text('Plugin example app'),
),
body: Center(
child: Text('Running on: $_platformVersion\n'),
child: Column(children: [textureID == null ? const SizedBox() : Expanded(child: Texture(textureId: textureID!)), Text('Running on: $_platformVersion\n')]),
),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Generated file. Do not edit.
//

// clang-format off

#include "generated_plugin_registrant.h"

#include <external_texture/external_texture_plugin.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Generated file. Do not edit.
//

// clang-format off

#ifndef GENERATED_PLUGIN_REGISTRANT_
#define GENERATED_PLUGIN_REGISTRANT_

Expand Down
2 changes: 2 additions & 0 deletions external_texture/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ 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)
find_package( OpenGL REQUIRED )
target_link_libraries(${PLUGIN_NAME} PRIVATE OpenGL::GL)

# List of absolute paths to libraries that should be bundled with the plugin
set(external_texture_bundled_libraries
Expand Down
52 changes: 30 additions & 22 deletions external_texture/linux/external_texture_plugin.cc
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
#include "include/external_texture/external_texture_plugin.h"
#include "include/external_texture/fl_my_texture_gl.h"

#include <flutter_linux/flutter_linux.h>
#include <gtk/gtk.h>
#include <sys/utsname.h>
#include <iostream>

#include <cstring>
#include "include/external_texture/opengl_renderer.h"
#include <iostream>
#include <memory>

#include "include/external_texture/fl_my_texture_gl.h"
#include "include/external_texture/opengl_renderer.h"
std::unique_ptr<OpenGLRenderer> openglRenderer;

#define EXTERNAL_TEXTURE_PLUGIN(obj) \
#define EXTERNAL_TEXTURE_PLUGIN(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), external_texture_plugin_get_type(), \
ExternalTexturePlugin))



struct _ExternalTexturePlugin {
GObject parent_instance;
FlTextureRegistrar* texture_registrar;
FlView* fl_view;
};

G_DEFINE_TYPE(ExternalTexturePlugin, external_texture_plugin, g_object_get_type())
G_DEFINE_TYPE(ExternalTexturePlugin,
external_texture_plugin,
g_object_get_type())

// Called when a method call is received from Flutter.
static void external_texture_plugin_handle_method_call(
Expand All @@ -34,7 +37,7 @@ static void external_texture_plugin_handle_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_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, "registerTexture") == 0) {
Expand All @@ -46,12 +49,15 @@ static void external_texture_plugin_handle_method_call(
int width = 1280;
int height = 720;
int texture_name = openglRenderer->genTexture(width, height);
FlMyTextureGL* t = fl_my_texture_gl_new(GL_TEXTURE_2D, texture_name, width, height);
g_autoptr(FlTexture) texture = FL_TEXTURE(t);
FlMyTextureGL* t =
fl_my_texture_gl_new(GL_TEXTURE_2D, texture_name, width, height);
g_autoptr(FlTexture) texture = FL_TEXTURE(t);
FlTextureRegistrar* texture_registrar = self->texture_registrar;
int64_t texture_id = fl_texture_registrar_register_texture(texture_registrar, texture);
fl_texture_registrar_mark_texture_frame_available(texture_registrar, texture_id);
g_autoptr(FlValue) result = fl_value_new_int(texture_id);
fl_texture_registrar_register_texture(texture_registrar, texture);
fl_texture_registrar_mark_texture_frame_available(texture_registrar,
texture);
g_autoptr(FlValue) result =
fl_value_new_int(reinterpret_cast<int64_t>(texture));
response = FL_METHOD_RESPONSE(fl_method_success_response_new(result));
} else {
response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
Expand All @@ -64,32 +70,34 @@ static void external_texture_plugin_dispose(GObject* object) {
G_OBJECT_CLASS(external_texture_plugin_parent_class)->dispose(object);
}

static void external_texture_plugin_class_init(ExternalTexturePluginClass* klass) {
static void external_texture_plugin_class_init(
ExternalTexturePluginClass* klass) {
G_OBJECT_CLASS(klass)->dispose = external_texture_plugin_dispose;
}

static void external_texture_plugin_init(ExternalTexturePlugin* self) {}

static void method_call_cb(FlMethodChannel* channel, FlMethodCall* method_call,
static void method_call_cb(FlMethodChannel* channel,
FlMethodCall* method_call,
gpointer user_data) {
ExternalTexturePlugin* plugin = EXTERNAL_TEXTURE_PLUGIN(user_data);
external_texture_plugin_handle_method_call(plugin, method_call);
}

void external_texture_plugin_register_with_registrar(FlPluginRegistrar* registrar) {
void external_texture_plugin_register_with_registrar(
FlPluginRegistrar* registrar) {
ExternalTexturePlugin* plugin = EXTERNAL_TEXTURE_PLUGIN(
g_object_new(external_texture_plugin_get_type(), nullptr));
FlView* fl_view = fl_plugin_registrar_get_view(registrar);
plugin->fl_view = fl_view;
plugin->texture_registrar = fl_plugin_registrar_get_texture_registrar(registrar);
plugin->texture_registrar =
fl_plugin_registrar_get_texture_registrar(registrar);
g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
g_autoptr(FlMethodChannel) channel =
fl_method_channel_new(fl_plugin_registrar_get_messenger(registrar),
"external_texture",
FL_METHOD_CODEC(codec));
fl_method_channel_set_method_call_handler(channel, method_call_cb,
g_object_ref(plugin),
g_object_unref);
"external_texture", FL_METHOD_CODEC(codec));
fl_method_channel_set_method_call_handler(
channel, method_call_cb, g_object_ref(plugin), g_object_unref);

g_object_unref(plugin);
}