Skip to content

Commit e27569b

Browse files
committed
Refactor using FlutterProjectBundle
1 parent 044ba9f commit e27569b

6 files changed

+199
-68
lines changed

shell/platform/tizen/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ template("embedder_for_profile") {
9797
"channels/text_input_channel.cc",
9898
"external_texture_pixel_gl.cc",
9999
"external_texture_surface_gl.cc",
100+
"flutter_project_bundle.cc",
100101
"flutter_tizen.cc",
101102
"flutter_tizen_engine.cc",
102103
"flutter_tizen_texture_registrar.cc",
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved.
2+
// Copyright 2013 The Flutter Authors. All rights reserved.
3+
// Use of this source code is governed by a BSD-style license that can be
4+
// found in the LICENSE file.
5+
6+
#include "flutter_project_bundle.h"
7+
8+
#include <filesystem>
9+
10+
#include "flutter/shell/platform/common/path_utils.h"
11+
#include "flutter/shell/platform/tizen/tizen_log.h"
12+
13+
namespace flutter {
14+
15+
FlutterProjectBundle::FlutterProjectBundle(
16+
const FlutterDesktopEngineProperties& properties)
17+
: assets_path_(properties.assets_path),
18+
icu_path_(properties.icu_data_path) {
19+
if (properties.aot_library_path != nullptr) {
20+
aot_library_path_ = std::filesystem::path(properties.aot_library_path);
21+
}
22+
23+
// Resolve any relative paths.
24+
if (assets_path_.is_relative() || icu_path_.is_relative() ||
25+
(!aot_library_path_.empty() && aot_library_path_.is_relative())) {
26+
std::filesystem::path executable_location = GetExecutableDirectory();
27+
if (executable_location.empty()) {
28+
FT_LOGE("Unable to find executable location to resolve resource paths.");
29+
assets_path_ = std::filesystem::path();
30+
icu_path_ = std::filesystem::path();
31+
} else {
32+
assets_path_ = std::filesystem::path(executable_location) / assets_path_;
33+
icu_path_ = std::filesystem::path(executable_location) / icu_path_;
34+
if (!aot_library_path_.empty()) {
35+
aot_library_path_ =
36+
std::filesystem::path(executable_location) / aot_library_path_;
37+
}
38+
}
39+
}
40+
41+
switches_.insert(switches_.end(), properties.switches,
42+
properties.switches + properties.switches_count);
43+
}
44+
45+
bool FlutterProjectBundle::HasValidPaths() {
46+
return !assets_path_.empty() && !icu_path_.empty();
47+
}
48+
49+
// Attempts to load AOT data from the given path, which must be absolute and
50+
// non-empty. Logs and returns nullptr on failure.
51+
UniqueAotDataPtr FlutterProjectBundle::LoadAotData(
52+
const FlutterEngineProcTable& engine_procs) {
53+
if (aot_library_path_.empty()) {
54+
FT_LOGE(
55+
"Attempted to load AOT data, but no aot_library_path was provided.");
56+
return UniqueAotDataPtr(nullptr, nullptr);
57+
}
58+
if (!std::filesystem::exists(aot_library_path_)) {
59+
FT_LOGE("Can't load AOT data from %s; no such file.",
60+
aot_library_path_.u8string().c_str());
61+
return UniqueAotDataPtr(nullptr, nullptr);
62+
}
63+
std::string path_string = aot_library_path_.u8string();
64+
FlutterEngineAOTDataSource source = {};
65+
source.type = kFlutterEngineAOTDataSourceTypeElfPath;
66+
source.elf_path = path_string.c_str();
67+
FlutterEngineAOTData data = nullptr;
68+
auto result = engine_procs.CreateAOTData(&source, &data);
69+
if (result != kSuccess) {
70+
FT_LOGE("Failed to load AOT data from: %s", path_string.c_str());
71+
return UniqueAotDataPtr(nullptr, nullptr);
72+
}
73+
return UniqueAotDataPtr(data, engine_procs.CollectAOTData);
74+
}
75+
76+
FlutterProjectBundle::~FlutterProjectBundle() {}
77+
78+
} // namespace flutter
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved.
2+
// Copyright 2013 The Flutter Authors. All rights reserved.
3+
// Use of this source code is governed by a BSD-style license that can be
4+
// found in the LICENSE file.
5+
6+
#ifndef EMBEDDER_FLUTTER_PROJECT_BUNDLE_H_
7+
#define EMBEDDER_FLUTTER_PROJECT_BUNDLE_H_
8+
9+
#include <filesystem>
10+
#include <string>
11+
#include <vector>
12+
13+
#include "flutter/shell/platform/embedder/embedder.h"
14+
#include "flutter/shell/platform/tizen/public/flutter_tizen.h"
15+
16+
namespace flutter {
17+
18+
using UniqueAotDataPtr =
19+
std::unique_ptr<_FlutterEngineAOTData, FlutterEngineCollectAOTDataFnPtr>;
20+
21+
// The data associated with a Flutter project needed to run it in an engine.
22+
class FlutterProjectBundle {
23+
public:
24+
// Creates a new project bundle from the given properties.
25+
//
26+
// Treats any relative paths as relative to the directory of this executable.
27+
explicit FlutterProjectBundle(
28+
const FlutterDesktopEngineProperties& properties);
29+
30+
~FlutterProjectBundle();
31+
32+
// Whether or not the bundle is valid. This does not check that the paths
33+
// exist, or contain valid data, just that paths were able to be constructed.
34+
bool HasValidPaths();
35+
36+
// Returns the path to the assets directory.
37+
const std::filesystem::path& assets_path() { return assets_path_; }
38+
39+
// Returns the path to the ICU data file.
40+
const std::filesystem::path& icu_path() { return icu_path_; }
41+
42+
// Returns any switches that should be passed to the engine.
43+
const std::vector<std::string> switches() { return switches_; }
44+
45+
// Attempts to load AOT data for this bundle. The returned data must be
46+
// retained until any engine instance it is passed to has been shut down.
47+
//
48+
// Logs and returns nullptr on failure.
49+
UniqueAotDataPtr LoadAotData(const FlutterEngineProcTable& engine_procs);
50+
51+
private:
52+
std::filesystem::path assets_path_;
53+
std::filesystem::path icu_path_;
54+
std::vector<std::string> switches_;
55+
56+
// Path to the AOT library file, if any.
57+
std::filesystem::path aot_library_path_;
58+
};
59+
60+
} // namespace flutter
61+
62+
#endif // EMBEDDER_FLUTTER_PROJECT_BUNDLE_H_

shell/platform/tizen/flutter_tizen.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "flutter/shell/platform/common/client_wrapper/include/flutter/plugin_registrar.h"
99
#include "flutter/shell/platform/common/client_wrapper/include/flutter/standard_message_codec.h"
1010
#include "flutter/shell/platform/common/incoming_message_dispatcher.h"
11+
#include "flutter/shell/platform/tizen/flutter_project_bundle.h"
1112
#include "flutter/shell/platform/tizen/flutter_tizen_engine.h"
1213
#include "flutter/shell/platform/tizen/public/flutter_platform_view.h"
1314
#include "flutter/shell/platform/tizen/tizen_log.h"
@@ -29,8 +30,9 @@ FlutterDesktopEngineRef FlutterDesktopRunEngine(
2930
bool headed) {
3031
flutter::StartLogging();
3132

32-
auto engine = std::make_unique<flutter::FlutterTizenEngine>(headed);
33-
if (!engine->RunEngine(engine_properties)) {
33+
flutter::FlutterProjectBundle project(engine_properties);
34+
auto engine = std::make_unique<flutter::FlutterTizenEngine>(project, headed);
35+
if (!engine->RunEngine()) {
3436
FT_LOGE("Failed to run the Flutter engine.");
3537
return nullptr;
3638
}

shell/platform/tizen/flutter_tizen_engine.cc

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include "flutter_tizen_engine.h"
77

8-
#include <filesystem>
8+
#include <algorithm>
99
#include <string>
1010
#include <vector>
1111

@@ -33,7 +33,10 @@ constexpr double kProfileFactor = 1.0;
3333

3434
} // namespace
3535

36-
FlutterTizenEngine::FlutterTizenEngine(bool headed) {
36+
FlutterTizenEngine::FlutterTizenEngine(const FlutterProjectBundle& project,
37+
bool headed)
38+
: project_(std::make_unique<FlutterProjectBundle>(project)),
39+
aot_data_(nullptr, nullptr) {
3740
embedder_api_.struct_size = sizeof(FlutterEngineProcTable);
3841
FlutterEngineGetProcAddresses(&embedder_api_);
3942

@@ -88,52 +91,38 @@ void FlutterTizenEngine::NotifyLowMemoryWarning() {
8891
embedder_api_.NotifyLowMemoryWarning(engine_);
8992
}
9093

91-
// Attempts to load AOT data from the given path, which must be absolute and
92-
// non-empty. Logs and returns nullptr on failure.
93-
UniqueAotDataPtr FlutterTizenEngine::LoadAotData(std::string aot_data_path) {
94-
if (aot_data_path.empty()) {
95-
FT_LOGE(
96-
"Attempted to load AOT data, but no aot_library_path was provided.");
97-
return nullptr;
98-
}
99-
if (!std::filesystem::exists(aot_data_path)) {
100-
FT_LOGE("Can't load AOT data from %s; no such file.",
101-
aot_data_path.c_str());
102-
return nullptr;
103-
}
104-
FlutterEngineAOTDataSource source = {};
105-
source.type = kFlutterEngineAOTDataSourceTypeElfPath;
106-
source.elf_path = aot_data_path.c_str();
107-
FlutterEngineAOTData data = nullptr;
108-
auto result = embedder_api_.CreateAOTData(&source, &data);
109-
if (result != kSuccess) {
110-
FT_LOGE("Failed to load AOT data from: %s", aot_data_path.c_str());
111-
return nullptr;
112-
}
113-
return UniqueAotDataPtr(data);
114-
}
115-
116-
bool FlutterTizenEngine::RunEngine(
117-
const FlutterDesktopEngineProperties& engine_properties) {
94+
bool FlutterTizenEngine::RunEngine() {
11895
if (IsHeaded() && !renderer->IsValid()) {
11996
FT_LOGE("The display was not valid.");
12097
return false;
12198
}
12299

100+
if (!project_->HasValidPaths()) {
101+
FT_LOGE("Missing or unresolvable paths to assets.");
102+
return false;
103+
}
104+
std::string assets_path_string = project_->assets_path().u8string();
105+
std::string icu_path_string = project_->icu_path().u8string();
106+
if (embedder_api_.RunsAOTCompiledDartCode()) {
107+
aot_data_ = project_->LoadAotData(embedder_api_);
108+
if (!aot_data_) {
109+
FT_LOGE("Unable to start engine without AOT data.");
110+
return false;
111+
}
112+
}
113+
123114
// FlutterProjectArgs is expecting a full argv, so when processing it for
124115
// flags the first item is treated as the executable and ignored. Add a dummy
125116
// value so that all provided arguments are used.
117+
std::vector<std::string> switches = project_->switches();
126118
std::vector<const char*> argv = {"placeholder"};
127-
if (engine_properties.switches_count > 0) {
128-
argv.insert(argv.end(), &engine_properties.switches[0],
129-
&engine_properties.switches[engine_properties.switches_count]);
130-
}
119+
std::transform(
120+
switches.begin(), switches.end(), std::back_inserter(argv),
121+
[](const std::string& arg) -> const char* { return arg.c_str(); });
131122

132-
for (size_t i = 0; i < engine_properties.switches_count; ++i) {
133-
auto str = std::string{engine_properties.switches[i]};
134-
if (str.find("verbose-logging") != std::string::npos) {
135-
SetMinLoggingLevel(DLOG_INFO);
136-
}
123+
if (std::find(switches.begin(), switches.end(), "verbose-logging") !=
124+
switches.end()) {
125+
SetMinLoggingLevel(DLOG_INFO);
137126
}
138127

139128
// Configure task runners.
@@ -173,10 +162,10 @@ bool FlutterTizenEngine::RunEngine(
173162

174163
FlutterProjectArgs args = {};
175164
args.struct_size = sizeof(FlutterProjectArgs);
176-
args.assets_path = engine_properties.assets_path;
177-
args.icu_data_path = engine_properties.icu_data_path;
165+
args.assets_path = assets_path_string.c_str();
166+
args.icu_data_path = icu_path_string.c_str();
178167
args.command_line_argc = static_cast<int>(argv.size());
179-
args.command_line_argv = &argv[0];
168+
args.command_line_argv = argv.size() > 0 ? argv.data() : nullptr;
180169
args.platform_message_callback =
181170
[](const FlutterPlatformMessage* engine_message, void* user_data) {
182171
if (engine_message->struct_size != sizeof(FlutterPlatformMessage)) {
@@ -198,13 +187,7 @@ bool FlutterTizenEngine::RunEngine(
198187
};
199188
}
200189
#endif
201-
202-
if (embedder_api_.RunsAOTCompiledDartCode()) {
203-
aot_data_ = LoadAotData(engine_properties.aot_library_path);
204-
if (!aot_data_) {
205-
FT_LOGE("Unable to start engine without AOT data.");
206-
return false;
207-
}
190+
if (aot_data_) {
208191
args.aot_data = aot_data_.get();
209192
}
210193

shell/platform/tizen/flutter_tizen_engine.h

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#ifndef EMBEDDER_FLUTTER_TIZEN_ENGINE_H_
77
#define EMBEDDER_FLUTTER_TIZEN_ENGINE_H_
88

9-
#include <map>
109
#include <memory>
1110

1211
#include "flutter/shell/platform/common/client_wrapper/include/flutter/plugin_registrar.h"
@@ -20,6 +19,7 @@
2019
#include "flutter/shell/platform/tizen/channels/platform_view_channel.h"
2120
#include "flutter/shell/platform/tizen/channels/settings_channel.h"
2221
#include "flutter/shell/platform/tizen/channels/text_input_channel.h"
22+
#include "flutter/shell/platform/tizen/flutter_project_bundle.h"
2323
#include "flutter/shell/platform/tizen/flutter_tizen_texture_registrar.h"
2424
#include "flutter/shell/platform/tizen/key_event_handler.h"
2525
#include "flutter/shell/platform/tizen/public/flutter_tizen.h"
@@ -47,27 +47,25 @@ struct FlutterDesktopMessenger {
4747

4848
namespace flutter {
4949

50-
// Custom deleter for FlutterEngineAOTData.
51-
struct AOTDataDeleter {
52-
void operator()(FlutterEngineAOTData aot_data) {
53-
FlutterEngineCollectAOTData(aot_data);
54-
}
55-
};
56-
57-
using UniqueAotDataPtr = std::unique_ptr<_FlutterEngineAOTData, AOTDataDeleter>;
58-
5950
// Manages state associated with the underlying FlutterEngine.
6051
class FlutterTizenEngine : public TizenRenderer::Delegate {
6152
public:
62-
explicit FlutterTizenEngine(bool headed);
53+
// Creates a new Flutter engine object configured to run |project|.
54+
explicit FlutterTizenEngine(const FlutterProjectBundle& project, bool headed);
55+
6356
virtual ~FlutterTizenEngine();
6457

6558
// Prevent copying.
6659
FlutterTizenEngine(FlutterTizenEngine const&) = delete;
6760
FlutterTizenEngine& operator=(FlutterTizenEngine const&) = delete;
6861

62+
// Sets up an instance of TizenRenderer.
6963
void InitializeRenderer();
70-
bool RunEngine(const FlutterDesktopEngineProperties& engine_properties);
64+
65+
// Starts running the engine.
66+
bool RunEngine();
67+
68+
// Stops the engine.
7169
bool StopEngine();
7270

7371
// Returns the currently configured Plugin Registrar.
@@ -140,15 +138,25 @@ class FlutterTizenEngine : public TizenRenderer::Delegate {
140138

141139
private:
142140
bool IsHeaded() { return renderer != nullptr; }
143-
UniqueAotDataPtr LoadAotData(std::string aot_data_path);
141+
144142
FlutterDesktopMessage ConvertToDesktopMessage(
145143
const FlutterPlatformMessage& engine_message);
144+
145+
// Creates and returns a FlutterRendererConfig depending on the current
146+
// display mode (headed or headless).
147+
// The user_data received by the render callbacks refers to the
148+
// FlutterTizenEngine.
146149
FlutterRendererConfig GetRendererConfig();
147150

151+
// The handle to the embedder.h engine instance.
152+
FLUTTER_API_SYMBOL(FlutterEngine) engine_ = nullptr;
153+
148154
FlutterEngineProcTable embedder_api_ = {};
149155

150-
// The Flutter engine instance.
151-
FLUTTER_API_SYMBOL(FlutterEngine) engine_;
156+
std::unique_ptr<FlutterProjectBundle> project_;
157+
158+
// AOT data for this engine instance, if applicable.
159+
UniqueAotDataPtr aot_data_;
152160

153161
// The handlers listening to platform events.
154162
std::unique_ptr<KeyEventHandler> key_event_handler_;
@@ -180,9 +188,6 @@ class FlutterTizenEngine : public TizenRenderer::Delegate {
180188
std::unique_ptr<TizenVsyncWaiter> tizen_vsync_waiter_;
181189
#endif
182190

183-
// AOT data for this engine instance, if applicable.
184-
UniqueAotDataPtr aot_data_;
185-
186191
// The current renderer transformation.
187192
FlutterTransformation transformation_;
188193
};

0 commit comments

Comments
 (0)