Skip to content

Commit 3b2ee1d

Browse files
committed
Add support for running flutter engine without UI
Signed-off-by: Rafal Walczyna <[email protected]>
1 parent 94bd946 commit 3b2ee1d

File tree

4 files changed

+101
-58
lines changed

4 files changed

+101
-58
lines changed

shell/platform/tizen/flutter_tizen.cc

+14
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ FlutterWindowControllerRef FlutterCreateWindow(
2626

2727
auto state = std::make_unique<FlutterWindowControllerState>();
2828
state->engine = std::make_unique<TizenEmbedderEngine>();
29+
state->engine->InitializeTizenRenderer();
2930

3031
if (!state->engine->RunEngine(engine_properties)) {
3132
FT_LOGE("Failed to run the Flutter engine.");
@@ -35,6 +36,19 @@ FlutterWindowControllerRef FlutterCreateWindow(
3536
return state.release();
3637
}
3738

39+
FlutterWindowControllerRef FlutterRunEngine(
40+
const FlutterEngineProperties& engine_properties) {
41+
StartLogging();
42+
auto state = std::make_unique<FlutterWindowControllerState>();
43+
state->engine = std::make_unique<TizenEmbedderEngine>();
44+
45+
if (!state->engine->RunEngine(engine_properties)) {
46+
FT_LOGE("Failed to run the Flutter engine.");
47+
return nullptr;
48+
}
49+
return state.release();
50+
}
51+
3852
void FlutterDestroyWindow(FlutterWindowControllerRef controller) {
3953
if (controller->engine) {
4054
controller->engine->StopEngine();

shell/platform/tizen/public/flutter_tizen.h

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ typedef struct {
4040
FLUTTER_EXPORT FlutterWindowControllerRef
4141
FlutterCreateWindow(const FlutterEngineProperties& engine_properties);
4242

43+
FLUTTER_EXPORT FlutterWindowControllerRef
44+
FlutterRunEngine(const FlutterEngineProperties& engine_properties);
45+
4346
// Returns the plugin registrar handle for the plugin with the given name.
4447
//
4548
// The name must be unique across the application.

shell/platform/tizen/tizen_embedder_engine.cc

+80-58
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ static DeviceProfile GetDeviceProfile() {
4141

4242
TizenEmbedderEngine::TizenEmbedderEngine()
4343
: device_profile(GetDeviceProfile()) {
44-
#ifdef TIZEN_RENDERER_EVAS_GL
45-
tizen_renderer = std::make_unique<TizenRendererEvasGL>(*this);
46-
#else
47-
tizen_renderer = std::make_unique<TizenRendererEcoreWl2>(*this);
48-
#endif
49-
5044
// Run flutter task on Tizen main loop.
5145
// Tizen engine has four threads (GPU thread, UI thread, IO thread, platform
5246
// thread). UI threads need to send flutter task to platform thread.
@@ -58,7 +52,16 @@ TizenEmbedderEngine::TizenEmbedderEngine()
5852
}
5953
});
6054

55+
messenger = std::make_unique<FlutterDesktopMessenger>();
56+
messenger->engine = this;
57+
message_dispatcher =
58+
std::make_unique<flutter::IncomingMessageDispatcher>(messenger.get());
59+
}
60+
61+
void TizenEmbedderEngine::InitializeTizenRenderer() {
6162
#ifdef TIZEN_RENDERER_EVAS_GL
63+
tizen_renderer = std::make_unique<TizenRendererEvasGL>(*this);
64+
6265
render_loop_ = std::make_unique<TizenRenderEventLoop>(
6366
std::this_thread::get_id(), // main thread
6467
[this](const auto* task) {
@@ -67,14 +70,8 @@ TizenEmbedderEngine::TizenEmbedderEngine()
6770
}
6871
},
6972
tizen_renderer.get());
70-
#endif
71-
72-
messenger = std::make_unique<FlutterDesktopMessenger>();
73-
messenger->engine = this;
74-
message_dispatcher =
75-
std::make_unique<flutter::IncomingMessageDispatcher>(messenger.get());
76-
77-
#ifndef TIZEN_RENDERER_EVAS_GL
73+
#else
74+
tizen_renderer = std::make_unique<TizenRendererEcoreWl2>(*this);
7875
tizen_vsync_waiter_ = std::make_unique<TizenVsyncWaiter>(this);
7976
#endif
8077
}
@@ -111,7 +108,7 @@ UniqueAotDataPtr LoadAotData(std::string aot_data_path) {
111108

112109
bool TizenEmbedderEngine::RunEngine(
113110
const FlutterEngineProperties& engine_properties) {
114-
if (!tizen_renderer->IsValid()) {
111+
if (HasTizenRenderer() && !tizen_renderer->IsValid()) {
115112
FT_LOGE("The display was not valid.");
116113
return false;
117114
}
@@ -138,40 +135,46 @@ bool TizenEmbedderEngine::RunEngine(
138135
static_cast<TizenEventLoop*>(data)->PostTask(task, target_time_nanos);
139136
};
140137
platform_task_runner.identifier = kPlatformTaskRunnerIdentifier;
141-
142-
#ifdef TIZEN_RENDERER_EVAS_GL
143-
FlutterTaskRunnerDescription render_task_runner = {};
144-
render_task_runner.struct_size = sizeof(FlutterTaskRunnerDescription);
145-
render_task_runner.user_data = render_loop_.get();
146-
render_task_runner.runs_task_on_current_thread_callback =
147-
[](void* data) -> bool {
148-
return static_cast<TizenEventLoop*>(data)->RunsTasksOnCurrentThread();
149-
};
150-
render_task_runner.post_task_callback =
151-
[](FlutterTask task, uint64_t target_time_nanos, void* data) -> void {
152-
static_cast<TizenEventLoop*>(data)->PostTask(task, target_time_nanos);
153-
};
154-
render_task_runner.identifier = kRenderTaskRunnerIdentifier;
155-
#endif
156-
157138
FlutterCustomTaskRunners custom_task_runners = {};
158139
custom_task_runners.struct_size = sizeof(FlutterCustomTaskRunners);
159140
custom_task_runners.platform_task_runner = &platform_task_runner;
141+
160142
#ifdef TIZEN_RENDERER_EVAS_GL
161-
custom_task_runners.render_task_runner = &render_task_runner;
143+
if (HasTizenRenderer()) {
144+
FlutterTaskRunnerDescription render_task_runner = {};
145+
render_task_runner.struct_size = sizeof(FlutterTaskRunnerDescription);
146+
render_task_runner.user_data = render_loop_.get();
147+
render_task_runner.runs_task_on_current_thread_callback =
148+
[](void* data) -> bool {
149+
return static_cast<TizenEventLoop*>(data)->RunsTasksOnCurrentThread();
150+
};
151+
render_task_runner.post_task_callback =
152+
[](FlutterTask task, uint64_t target_time_nanos, void* data) -> void {
153+
static_cast<TizenEventLoop*>(data)->PostTask(task, target_time_nanos);
154+
};
155+
render_task_runner.identifier = kRenderTaskRunnerIdentifier;
156+
custom_task_runners.render_task_runner = &render_task_runner;
157+
}
162158
#endif
163159

164160
FlutterRendererConfig config = {};
165-
config.type = kOpenGL;
166-
config.open_gl.struct_size = sizeof(config.open_gl);
167-
config.open_gl.make_current = MakeContextCurrent;
168-
config.open_gl.make_resource_current = MakeResourceCurrent;
169-
config.open_gl.clear_current = ClearContext;
170-
config.open_gl.present = Present;
171-
config.open_gl.fbo_callback = GetActiveFbo;
172-
config.open_gl.surface_transformation = Transformation;
173-
config.open_gl.gl_proc_resolver = GlProcResolver;
174-
config.open_gl.gl_external_texture_frame_callback = OnAcquireExternalTexture;
161+
if (HasTizenRenderer()) {
162+
config.type = kOpenGL;
163+
config.open_gl.struct_size = sizeof(config.open_gl);
164+
config.open_gl.make_current = MakeContextCurrent;
165+
config.open_gl.make_resource_current = MakeResourceCurrent;
166+
config.open_gl.clear_current = ClearContext;
167+
config.open_gl.present = Present;
168+
config.open_gl.fbo_callback = GetActiveFbo;
169+
config.open_gl.surface_transformation = Transformation;
170+
config.open_gl.gl_proc_resolver = GlProcResolver;
171+
config.open_gl.gl_external_texture_frame_callback =
172+
OnAcquireExternalTexture;
173+
} else {
174+
config.type = kSoftware;
175+
config.software.struct_size = sizeof(config.software);
176+
config.software.surface_present_callback = SurfacePresentCallback;
177+
}
175178

176179
FlutterProjectArgs args = {};
177180
args.struct_size = sizeof(FlutterProjectArgs);
@@ -181,8 +184,11 @@ bool TizenEmbedderEngine::RunEngine(
181184
args.command_line_argv = &argv[0];
182185
args.platform_message_callback = OnFlutterPlatformMessage;
183186
args.custom_task_runners = &custom_task_runners;
187+
184188
#ifndef TIZEN_RENDERER_EVAS_GL
185-
args.vsync_callback = OnVsyncCallback;
189+
if (HasTizenRenderer()) {
190+
args.vsync_callback = OnVsyncCallback;
191+
}
186192
#endif
187193

188194
if (FlutterEngineRunsAOTCompiledDartCode()) {
@@ -203,36 +209,40 @@ bool TizenEmbedderEngine::RunEngine(
203209
return false;
204210
}
205211

206-
std::unique_ptr<FlutterTextureRegistrar> textures =
207-
std::make_unique<FlutterTextureRegistrar>();
208-
textures->flutter_engine = flutter_engine;
209212
plugin_registrar_ = std::make_unique<FlutterDesktopPluginRegistrar>();
210213
plugin_registrar_->engine = this;
211-
plugin_registrar_->texture_registrar = std::move(textures);
212214

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

216-
key_event_channel = std::make_unique<KeyEventChannel>(
217-
internal_plugin_registrar_->messenger());
218-
navigation_channel = std::make_unique<NavigationChannel>(
219-
internal_plugin_registrar_->messenger());
220218
platform_channel = std::make_unique<PlatformChannel>(
221219
internal_plugin_registrar_->messenger());
222220
settings_channel = std::make_unique<SettingsChannel>(
223221
internal_plugin_registrar_->messenger());
224-
text_input_channel = std::make_unique<TextInputChannel>(
225-
internal_plugin_registrar_->messenger(), this);
226222
localization_channel = std::make_unique<LocalizationChannel>(flutter_engine);
227223
localization_channel->SendLocales();
228224
lifecycle_channel = std::make_unique<LifecycleChannel>(flutter_engine);
229-
platform_view_channel = std::make_unique<PlatformViewChannel>(
230-
internal_plugin_registrar_->messenger(), this);
231225

232-
key_event_handler_ = std::make_unique<KeyEventHandler>(this);
233-
touch_event_handler_ = std::make_unique<TouchEventHandler>(this);
226+
if (HasTizenRenderer()) {
227+
std::unique_ptr<FlutterTextureRegistrar> textures =
228+
std::make_unique<FlutterTextureRegistrar>();
229+
textures->flutter_engine = flutter_engine;
230+
plugin_registrar_->texture_registrar = std::move(textures);
231+
232+
key_event_channel = std::make_unique<KeyEventChannel>(
233+
internal_plugin_registrar_->messenger());
234+
navigation_channel = std::make_unique<NavigationChannel>(
235+
internal_plugin_registrar_->messenger());
236+
text_input_channel = std::make_unique<TextInputChannel>(
237+
internal_plugin_registrar_->messenger(), this);
238+
platform_view_channel = std::make_unique<PlatformViewChannel>(
239+
internal_plugin_registrar_->messenger(), this);
240+
key_event_handler_ = std::make_unique<KeyEventHandler>(this);
241+
touch_event_handler_ = std::make_unique<TouchEventHandler>(this);
242+
243+
SetWindowOrientation(0);
244+
}
234245

235-
SetWindowOrientation(0);
236246
return true;
237247
}
238248

@@ -394,6 +404,14 @@ FlutterDesktopMessage TizenEmbedderEngine::ConvertToDesktopMessage(
394404
return message;
395405
}
396406

407+
bool TizenEmbedderEngine::SurfacePresentCallback(void* user_data,
408+
const void* allocation,
409+
size_t row_bytes,
410+
size_t height) {
411+
FT_LOGD("SurfacePresentCallback");
412+
return true;
413+
}
414+
397415
bool TizenEmbedderEngine::MakeContextCurrent(void* user_data) {
398416
return reinterpret_cast<TizenEmbedderEngine*>(user_data)
399417
->tizen_renderer->OnMakeCurrent();
@@ -427,3 +445,7 @@ void* TizenEmbedderEngine::GlProcResolver(void* user_data, const char* name) {
427445
return reinterpret_cast<TizenEmbedderEngine*>(user_data)
428446
->tizen_renderer->OnProcResolver(name);
429447
}
448+
449+
bool TizenEmbedderEngine::HasTizenRenderer() {
450+
return tizen_renderer != nullptr;
451+
}

shell/platform/tizen/tizen_embedder_engine.h

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class TizenEmbedderEngine : public TizenRenderer::Delegate {
7171
public:
7272
explicit TizenEmbedderEngine();
7373
virtual ~TizenEmbedderEngine();
74+
void InitializeTizenRenderer();
7475
bool RunEngine(const FlutterEngineProperties& engine_properties);
7576
bool StopEngine();
7677

@@ -115,6 +116,7 @@ class TizenEmbedderEngine : public TizenRenderer::Delegate {
115116
const DeviceProfile device_profile;
116117

117118
private:
119+
static bool SurfacePresentCallback(void*, const void*, size_t, size_t);
118120
static bool MakeContextCurrent(void* user_data);
119121
static bool ClearContext(void* user_data);
120122
static bool Present(void* user_data);
@@ -134,6 +136,8 @@ class TizenEmbedderEngine : public TizenRenderer::Delegate {
134136
size_t width, size_t height,
135137
FlutterOpenGLTexture* texture);
136138

139+
bool HasTizenRenderer();
140+
137141
// The handlers listening to platform events.
138142
std::unique_ptr<KeyEventHandler> key_event_handler_;
139143
std::unique_ptr<TouchEventHandler> touch_event_handler_;

0 commit comments

Comments
 (0)