Skip to content

Commit

Permalink
Implement FlutterDesktopPluginRegistrarSetDestructionHandler (flutter…
Browse files Browse the repository at this point in the history
…-tizen#29)

* Implement FlutterDesktopPluginRegistrarSetDestructionHandler

- Add implementation for
  FlutterDesktopPluginRegistrarSetDestructionHandler
- Update renamed API FlutterDesktopRegistrarEnableInputBlocking ->
  FlutterDesktopPluginRegistrarEnableInputBlocking
- Some refactoring
- Remove logs which are too verbose

* Remove flutter namespace from ExternalTextureGL
  • Loading branch information
swift-kim authored and GitHub Enterprise committed Nov 5, 2020
1 parent 8cc935b commit 77cc712
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 103 deletions.
9 changes: 2 additions & 7 deletions shell/platform/tizen/external_texture_gl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,20 @@
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <GLES3/gl32.h>
#include <atomic>

#include <atomic>
#include <iostream>

namespace flutter {

struct ExternalTextureGLState {
GLuint gl_texture;
};

static std::atomic_long nextTextureId = {1};


ExternalTextureGL::ExternalTextureGL()
: state_(std::make_unique<ExternalTextureGLState>()),
texture_tbm_surface_(NULL),
texture_id_(nextTextureId++){}
texture_id_(nextTextureId++) {}

ExternalTextureGL::~ExternalTextureGL() {
mutex_.lock();
Expand Down Expand Up @@ -152,5 +149,3 @@ void ExternalTextureGL::destructionCallback(void* user_data) {
reinterpret_cast<ExternalTextureGL*>(user_data);
externalTextureGL->DestructionTbmSurface();
}

} // namespace flutter
4 changes: 0 additions & 4 deletions shell/platform/tizen/external_texture_gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
#include "flutter/shell/platform/embedder/embedder.h"
#include "logger.h"

namespace flutter {

typedef struct ExternalTextureGLState ExternalTextureGLState;

// An adaptation class of flutter engine and external texture interface.
Expand Down Expand Up @@ -67,6 +65,4 @@ class ExternalTextureGL {
const long texture_id_;
};

} // namespace flutter

#endif // FLUTTER_SHELL_PLATFORM_TIZEN_EXTERNAL_TEXTURE_GL_H_
57 changes: 30 additions & 27 deletions shell/platform/tizen/flutter_tizen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,34 +85,30 @@ FlutterWindowControllerRef FlutterCreateWindow(
InitializeLogging();

auto state = std::make_unique<FlutterWindowControllerState>();
state->engine = std::make_unique<TizenEmbedderEngine>();
state->engine = std::make_unique<TizenEmbedderEngine>(window_properties);

if (!state->engine->CreateWindow(window_properties)) {
LoggerE("Failed to create window");
return nullptr;
}

if (!state->engine->RunFlutterEngine(engine_properties)) {
LoggerE("Failed to run flutter engine");
if (!state->engine->RunEngine(engine_properties)) {
LoggerE("Failed to run the Flutter engine.");
return nullptr;
}

return state.release();
}

void FlutterDestoryWindow(FlutterWindowControllerRef controller) {
LoggerD("FlutterDestoryWindow");
controller->engine->DestroyWindow();
if (controller->engine) {
controller->engine->StopEngine();
}
delete controller;
}

bool FlutterRunsPrecompiledCode() {
return FlutterEngineRunsAOTCompiledDartCode();
}

void FlutterDesktopRegistrarEnableInputBlocking(
void FlutterDesktopPluginRegistrarEnableInputBlocking(
FlutterDesktopPluginRegistrarRef registrar, const char* channel) {
registrar->messenger->dispatcher->EnableInputBlockingForChannel(channel);
registrar->engine->message_dispatcher->EnableInputBlockingForChannel(channel);
}

FlutterDesktopPluginRegistrarRef FlutterDesktopGetPluginRegistrar(
Expand All @@ -125,7 +121,13 @@ FlutterDesktopPluginRegistrarRef FlutterDesktopGetPluginRegistrar(

FlutterDesktopMessengerRef FlutterDesktopPluginRegistrarGetMessenger(
FlutterDesktopPluginRegistrarRef registrar) {
return registrar->messenger.get();
return registrar->engine->messenger.get();
}

void FlutterDesktopPluginRegistrarSetDestructionHandler(
FlutterDesktopPluginRegistrarRef registrar,
FlutterDesktopOnPluginRegistrarDestroyed callback) {
registrar->engine->SetPluginRegistrarDestructionCallback(callback);
}

FlutterTextureRegistrarRef FlutterPluginRegistrarGetTexture(
Expand All @@ -149,7 +151,7 @@ bool FlutterDesktopMessengerSendWithReply(FlutterDesktopMessengerRef messenger,
FlutterPlatformMessageResponseHandle* response_handle = nullptr;
if (reply != nullptr && user_data != nullptr) {
FlutterEngineResult result = FlutterPlatformMessageCreateResponseHandle(
messenger->engine, reply, user_data, &response_handle);
messenger->engine->flutter_engine, reply, user_data, &response_handle);
if (result != kSuccess) {
LoggerE("Failed to create response handle");
return false;
Expand All @@ -162,11 +164,11 @@ bool FlutterDesktopMessengerSendWithReply(FlutterDesktopMessengerRef messenger,
message_size,
response_handle,
};
FlutterEngineResult message_result =
FlutterEngineSendPlatformMessage(messenger->engine, &platform_message);
FlutterEngineResult message_result = FlutterEngineSendPlatformMessage(
messenger->engine->flutter_engine, &platform_message);
if (response_handle != nullptr) {
FlutterPlatformMessageReleaseResponseHandle(messenger->engine,
response_handle);
FlutterPlatformMessageReleaseResponseHandle(
messenger->engine->flutter_engine, response_handle);
}
return message_result == kSuccess;
}
Expand All @@ -175,15 +177,16 @@ void FlutterDesktopMessengerSendResponse(
FlutterDesktopMessengerRef messenger,
const FlutterDesktopMessageResponseHandle* handle, const uint8_t* data,
size_t data_length) {
FlutterEngineSendPlatformMessageResponse(messenger->engine, handle, data,
data_length);
FlutterEngineSendPlatformMessageResponse(messenger->engine->flutter_engine,
handle, data, data_length);
}

void FlutterDesktopMessengerSetCallback(FlutterDesktopMessengerRef messenger,
const char* channel,
FlutterDesktopMessageCallback callback,
void* user_data) {
messenger->dispatcher->SetMessageCallback(channel, callback, user_data);
messenger->engine->message_dispatcher->SetMessageCallback(channel, callback,
user_data);
}

void FlutterNotifyLocaleChange(FlutterWindowControllerRef controller) {
Expand Down Expand Up @@ -231,11 +234,11 @@ void FlutterRotateWindow(FlutterWindowControllerRef controller,

int64_t FlutterRegisterExternalTexture(
FlutterTextureRegistrarRef texture_registrar) {
LoggerI("FlutterDesktopRegisterExternalTexture");
auto texture_gl = std::make_unique<flutter::ExternalTextureGL>();
LoggerD("FlutterDesktopRegisterExternalTexture");
auto texture_gl = std::make_unique<ExternalTextureGL>();
int64_t texture_id = texture_gl->texture_id();
texture_registrar->textures[texture_id] = std::move(texture_gl);
if (FlutterEngineRegisterExternalTexture(texture_registrar->engine,
if (FlutterEngineRegisterExternalTexture(texture_registrar->flutter_engine,
texture_id) == kSuccess) {
return texture_id;
}
Expand All @@ -247,8 +250,8 @@ bool FlutterUnregisterExternalTexture(
auto it = texture_registrar->textures.find(texture_id);
if (it != texture_registrar->textures.end())
texture_registrar->textures.erase(it);
return (FlutterEngineUnregisterExternalTexture(texture_registrar->engine,
texture_id) == kSuccess);
return (FlutterEngineUnregisterExternalTexture(
texture_registrar->flutter_engine, texture_id) == kSuccess);
}

bool FlutterMarkExternalTextureFrameAvailable(
Expand All @@ -265,5 +268,5 @@ bool FlutterMarkExternalTextureFrameAvailable(
return false;
}
return (FlutterEngineMarkExternalTextureFrameAvailable(
texture_registrar->engine, texture_id) == kSuccess);
texture_registrar->flutter_engine, texture_id) == kSuccess);
}
91 changes: 48 additions & 43 deletions shell/platform/tizen/tizen_embedder_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,33 @@
// Unique number associated with platform tasks.
static constexpr size_t kPlatformTaskRunnerIdentifier = 1;

TizenEmbedderEngine::TizenEmbedderEngine() { LoggerD("Flutter constructor"); }

TizenEmbedderEngine::~TizenEmbedderEngine() {}

bool TizenEmbedderEngine::CreateWindow(
TizenEmbedderEngine::TizenEmbedderEngine(
const FlutterWindowProperties& window_properties) {
tizen_surface = std::make_unique<TizenSurfaceGL>(
window_properties.x, window_properties.y, window_properties.width,
window_properties.height);
return tizen_surface->IsValid();
}

void TizenEmbedderEngine::DestroyWindow() {
if (flutter_engine) {
FlutterEngineShutdown(flutter_engine);
flutter_engine = nullptr;
}
// Run flutter task on Tizen main loop.
// Tizen engine has four threads (GPU thread, UI thread, IO thread, platform
// thread). UI threads need to send flutter task to platform thread.
event_loop_ = std::make_unique<TizenEventLoop>(
std::this_thread::get_id(), // main thread
[this](const auto* task) {
if (FlutterEngineRunTask(this->flutter_engine, task) != kSuccess) {
LoggerE("Could not post an engine task.");
}
});

messenger = std::make_unique<FlutterDesktopMessenger>();
messenger->engine = this;
message_dispatcher =
std::make_unique<flutter::IncomingMessageDispatcher>(messenger.get());

tizen_vsync_waiter_ = std::make_unique<TizenVsyncWaiter>();
}

TizenEmbedderEngine::~TizenEmbedderEngine() {}

// Attempts to load AOT data from the given path, which must be absolute and
// non-empty. Logs and returns nullptr on failure.
UniqueAotDataPtr LoadAotData(std::string aot_data_path) {
Expand All @@ -71,10 +79,10 @@ UniqueAotDataPtr LoadAotData(std::string aot_data_path) {
return UniqueAotDataPtr(data);
}

bool TizenEmbedderEngine::RunFlutterEngine(
bool TizenEmbedderEngine::RunEngine(
const FlutterEngineProperties& engine_properties) {
if (!tizen_surface->IsValid()) {
LoggerE("display was not valid.");
LoggerE("The display was not valid.");
return false;
}

Expand All @@ -87,39 +95,24 @@ bool TizenEmbedderEngine::RunFlutterEngine(
&engine_properties.switches[engine_properties.switches_count]);
}

//Run flutter task on Tizen main loop.
//Tizen engine have four threads(GPU thread,UI thread,IO thread,Platform thread),
//UI threads need send flutter task to platform thead.
event_loop_ = std::make_unique<TizenEventLoop>(
std::this_thread::get_id(), // main thread
[this](const auto* task) {
if (FlutterEngineRunTask(this->flutter_engine, task) != kSuccess) {
LoggerE("Could not post an engine task.");
}
});

// Configure task runner interop.
FlutterTaskRunnerDescription platform_task_runner = {};
platform_task_runner.struct_size = sizeof(FlutterTaskRunnerDescription);
platform_task_runner.user_data = this;
platform_task_runner.user_data = event_loop_.get();
platform_task_runner.runs_task_on_current_thread_callback =
[](void* data) -> bool {
return reinterpret_cast<TizenEmbedderEngine*>(data)
->event_loop_->RunsTasksOnCurrentThread();
return static_cast<TizenEventLoop*>(data)->RunsTasksOnCurrentThread();
};
platform_task_runner.post_task_callback =
[](FlutterTask task, uint64_t target_time_nanos, void* data) -> void {
reinterpret_cast<TizenEmbedderEngine*>(data)->event_loop_->PostTask(
task, target_time_nanos);
static_cast<TizenEventLoop*>(data)->PostTask(task, target_time_nanos);
};
platform_task_runner.identifier = kPlatformTaskRunnerIdentifier;

FlutterCustomTaskRunners custom_task_runners = {};
custom_task_runners.struct_size = sizeof(FlutterCustomTaskRunners);
custom_task_runners.platform_task_runner = &platform_task_runner;

tizen_vsync_waiter_ = std::make_unique<TizenVsyncWaiter>();

FlutterRendererConfig config = {};
config.type = kOpenGL;
config.open_gl.struct_size = sizeof(config.open_gl);
Expand Down Expand Up @@ -160,24 +153,18 @@ bool TizenEmbedderEngine::RunFlutterEngine(
return false;
}

auto messenger = std::make_unique<FlutterDesktopMessenger>();
message_dispatcher_ =
std::make_unique<flutter::IncomingMessageDispatcher>(messenger.get());
messenger->engine = flutter_engine;
messenger->dispatcher = message_dispatcher_.get();
tizen_vsync_waiter_->AsyncWaitForRunEngineSuccess(flutter_engine);

std::unique_ptr<FlutterTextureRegistrar> textures =
std::make_unique<FlutterTextureRegistrar>();
textures->engine = flutter_engine;
textures->flutter_engine = flutter_engine;
plugin_registrar_ = std::make_unique<FlutterDesktopPluginRegistrar>();
plugin_registrar_->messenger = std::move(messenger);
plugin_registrar_->engine = this;
plugin_registrar_->texture_registrar = std::move(textures);

internal_plugin_registrar_ =
std::make_unique<flutter::PluginRegistrar>(plugin_registrar_.get());

tizen_vsync_waiter_->AsyncWaitForRunEngineSuccess(flutter_engine);

key_event_channel = std::make_unique<KeyEventChannel>(
internal_plugin_registrar_->messenger());
navigation_channel = std::make_unique<NavigationChannel>(
Expand All @@ -194,16 +181,32 @@ bool TizenEmbedderEngine::RunFlutterEngine(
key_event_handler_ = std::make_unique<KeyEventHandler>(this);
touch_event_handler_ = std::make_unique<TouchEventHandler>(this);

// This must be called to initialize the value of transformation_.
SetWindowOrientation(0);

return true;
}

bool TizenEmbedderEngine::StopEngine() {
if (flutter_engine) {
if (plugin_registrar_destruction_callback_) {
plugin_registrar_destruction_callback_(plugin_registrar_.get());
}
FlutterEngineResult result = FlutterEngineShutdown(flutter_engine);
flutter_engine = nullptr;
return (result == kSuccess);
}
return false;
}

FlutterDesktopPluginRegistrarRef TizenEmbedderEngine::GetPluginRegistrar() {
return plugin_registrar_.get();
}

void TizenEmbedderEngine::SetPluginRegistrarDestructionCallback(
FlutterDesktopOnPluginRegistrarDestroyed callback) {
plugin_registrar_destruction_callback_ = callback;
}

bool TizenEmbedderEngine::OnAcquireExternalTexture(
void* user_data, int64_t texture_id, size_t width, size_t height,
FlutterOpenGLTexture* texture) {
Expand Down Expand Up @@ -258,6 +261,8 @@ void TizenEmbedderEngine::SendWindowMetrics(int32_t width, int32_t height,
FlutterEngineSendWindowMetricsEvent(flutter_engine, &event);
}

// This must be called at least once in order to initialize the value of
// transformation_.
void TizenEmbedderEngine::SetWindowOrientation(int32_t degree) {
if (!tizen_surface) {
return;
Expand Down Expand Up @@ -316,7 +321,7 @@ void TizenEmbedderEngine::OnFlutterPlatformMessage(
reinterpret_cast<TizenEmbedderEngine*>(user_data);
auto message =
tizen_embedder_engine->ConvertToDesktopMessage(*engine_message);
tizen_embedder_engine->message_dispatcher_->HandleMessage(message);
tizen_embedder_engine->message_dispatcher->HandleMessage(message);
}

void TizenEmbedderEngine::OnVsyncCallback(void* user_data, intptr_t baton) {
Expand Down
Loading

0 comments on commit 77cc712

Please sign in to comment.