Skip to content

Commit 41e8ed0

Browse files
George Wrightgw280
George Wright
authored andcommitted
Migrate flutter_runner from flutter_runner::{Thread,Loop} to fml::{Thread,MessageLoop} (flutter#15118)
1 parent 8ec994b commit 41e8ed0

21 files changed

+75
-448
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,8 +1020,6 @@ FILE: ../../../flutter/shell/platform/fuchsia/flutter/kernel/extract_far.dart
10201020
FILE: ../../../flutter/shell/platform/fuchsia/flutter/kernel/framework_shim.dart
10211021
FILE: ../../../flutter/shell/platform/fuchsia/flutter/kernel/libraries.json
10221022
FILE: ../../../flutter/shell/platform/fuchsia/flutter/logging.h
1023-
FILE: ../../../flutter/shell/platform/fuchsia/flutter/loop.cc
1024-
FILE: ../../../flutter/shell/platform/fuchsia/flutter/loop.h
10251023
FILE: ../../../flutter/shell/platform/fuchsia/flutter/main.cc
10261024
FILE: ../../../flutter/shell/platform/fuchsia/flutter/meta/aot_product_runtime
10271025
FILE: ../../../flutter/shell/platform/fuchsia/flutter/meta/aot_runtime
@@ -1044,12 +1042,6 @@ FILE: ../../../flutter/shell/platform/fuchsia/flutter/session_connection.cc
10441042
FILE: ../../../flutter/shell/platform/fuchsia/flutter/session_connection.h
10451043
FILE: ../../../flutter/shell/platform/fuchsia/flutter/surface.cc
10461044
FILE: ../../../flutter/shell/platform/fuchsia/flutter/surface.h
1047-
FILE: ../../../flutter/shell/platform/fuchsia/flutter/task_observers.cc
1048-
FILE: ../../../flutter/shell/platform/fuchsia/flutter/task_observers.h
1049-
FILE: ../../../flutter/shell/platform/fuchsia/flutter/task_runner_adapter.cc
1050-
FILE: ../../../flutter/shell/platform/fuchsia/flutter/task_runner_adapter.h
1051-
FILE: ../../../flutter/shell/platform/fuchsia/flutter/thread.cc
1052-
FILE: ../../../flutter/shell/platform/fuchsia/flutter/thread.h
10531045
FILE: ../../../flutter/shell/platform/fuchsia/flutter/unique_fdio_ns.h
10541046
FILE: ../../../flutter/shell/platform/fuchsia/flutter/vsync_recorder.cc
10551047
FILE: ../../../flutter/shell/platform/fuchsia/flutter/vsync_recorder.h

fml/thread.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#if defined(OS_WIN)
1212
#include <windows.h>
13+
#elif defined(OS_FUCHSIA)
14+
#include <lib/zx/thread.h>
1315
#else
1416
#include <pthread.h>
1517
#endif
@@ -85,6 +87,8 @@ void Thread::SetCurrentThreadName(const std::string& name) {
8587
reinterpret_cast<DWORD_PTR*>(&info));
8688
} __except (EXCEPTION_CONTINUE_EXECUTION) {
8789
}
90+
#elif OS_FUCHSIA
91+
zx::thread::self()->set_property(ZX_PROP_NAME, name.c_str(), name.size());
8892
#else
8993
FML_DLOG(INFO) << "Could not set the thread name to '" << name
9094
<< "' on this platform.";

shell/platform/fuchsia/flutter/BUILD.gn

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,6 @@ executable("flutter_runner_unittests") {
303303
"fuchsia_intl.h",
304304
"fuchsia_intl_unittest.cc",
305305
"logging.h",
306-
"loop.cc",
307-
"loop.h",
308306
"platform_view.cc",
309307
"platform_view.h",
310308
"platform_view_unittest.cc",
@@ -313,12 +311,6 @@ executable("flutter_runner_unittests") {
313311
"runner_unittest.cc",
314312
"surface.cc",
315313
"surface.h",
316-
"task_observers.cc",
317-
"task_observers.h",
318-
"task_runner_adapter.cc",
319-
"task_runner_adapter.h",
320-
"thread.cc",
321-
"thread.h",
322314
"vsync_recorder.cc",
323315
"vsync_recorder.h",
324316
"vsync_waiter.cc",

shell/platform/fuchsia/flutter/component.cc

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
#define FML_USED_ON_EMBEDDER
6+
57
#include "component.h"
68

79
#include <dlfcn.h>
@@ -36,10 +38,6 @@
3638
#include "runtime/dart/utils/tempfs.h"
3739
#include "runtime/dart/utils/vmo.h"
3840

39-
#include "task_observers.h"
40-
#include "task_runner_adapter.h"
41-
#include "thread.h"
42-
4341
// TODO(kaushikiska): Use these constants from ::llcpp::fuchsia::io
4442
// Can read from target object.
4543
constexpr uint32_t OPEN_RIGHT_READABLE = 1u;
@@ -59,11 +57,11 @@ ActiveApplication Application::Create(
5957
fuchsia::sys::StartupInfo startup_info,
6058
std::shared_ptr<sys::ServiceDirectory> runner_incoming_services,
6159
fidl::InterfaceRequest<fuchsia::sys::ComponentController> controller) {
62-
std::unique_ptr<Thread> thread = std::make_unique<Thread>();
60+
std::unique_ptr<fml::Thread> thread = std::make_unique<fml::Thread>();
6361
std::unique_ptr<Application> application;
6462

6563
fml::AutoResetWaitableEvent latch;
66-
async::PostTask(thread->dispatcher(), [&]() mutable {
64+
fml::TaskRunner::RunNowOrPostTask(thread->GetTaskRunner(), [&]() mutable {
6765
application.reset(
6866
new Application(std::move(termination_callback), std::move(package),
6967
std::move(startup_info), runner_incoming_services,
@@ -347,12 +345,12 @@ Application::Application(
347345
settings_.disable_dart_asserts = true;
348346
#endif
349347

350-
settings_.task_observer_add =
351-
std::bind(&CurrentMessageLoopAddAfterTaskObserver, std::placeholders::_1,
352-
std::placeholders::_2);
353-
354-
settings_.task_observer_remove = std::bind(
355-
&CurrentMessageLoopRemoveAfterTaskObserver, std::placeholders::_1);
348+
settings_.task_observer_add = [](intptr_t key, fml::closure callback) {
349+
fml::MessageLoop::GetCurrent().AddTaskObserver(key, std::move(callback));
350+
};
351+
settings_.task_observer_remove = [](intptr_t key) {
352+
fml::MessageLoop::GetCurrent().RemoveTaskObserver(key);
353+
};
356354

357355
// TODO(FL-117): Re-enable causal async stack traces when this issue is
358356
// addressed.
@@ -379,8 +377,7 @@ Application::Application(
379377
#endif // defined(__aarch64__)
380378

381379
auto weak_application = weak_factory_.GetWeakPtr();
382-
auto platform_task_runner =
383-
CreateFMLTaskRunner(async_get_default_dispatcher());
380+
auto platform_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
384381
const std::string component_url = package.resolved_url;
385382
settings_.unhandled_exception_callback = [weak_application,
386383
platform_task_runner,

shell/platform/fuchsia/flutter/component.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@
2424
#include "engine.h"
2525
#include "flutter/common/settings.h"
2626
#include "flutter/fml/macros.h"
27+
#include "flutter/fml/thread.h"
2728

28-
#include "thread.h"
2929
#include "unique_fdio_ns.h"
3030

3131
namespace flutter_runner {
3232

3333
class Application;
3434

3535
struct ActiveApplication {
36-
std::unique_ptr<Thread> thread;
36+
std::unique_ptr<fml::Thread> thread;
3737
std::unique_ptr<Application> application;
3838

3939
ActiveApplication& operator=(ActiveApplication&& other) noexcept {

shell/platform/fuchsia/flutter/engine.cc

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
#define FML_USED_ON_EMBEDDER
6+
57
#include "engine.h"
68

79
#include <lib/async/cpp/task.h>
@@ -18,9 +20,7 @@
1820
#include "fuchsia_intl.h"
1921
#include "platform_view.h"
2022
#include "runtime/dart/utils/files.h"
21-
#include "task_runner_adapter.h"
2223
#include "third_party/skia/include/ports/SkFontMgr_fuchsia.h"
23-
#include "thread.h"
2424

2525
namespace flutter_runner {
2626

@@ -60,19 +60,17 @@ Engine::Engine(Delegate& delegate,
6060
fidl::InterfaceRequest<fuchsia::io::Directory> directory_request)
6161
: delegate_(delegate),
6262
thread_label_(std::move(thread_label)),
63+
thread_host_(thread_label_ + ".",
64+
flutter::ThreadHost::Type::IO |
65+
flutter::ThreadHost::Type::UI |
66+
flutter::ThreadHost::Type::GPU),
6367
settings_(std::move(settings)),
6468
weak_factory_(this) {
6569
if (zx::event::create(0, &vsync_event_) != ZX_OK) {
6670
FML_DLOG(ERROR) << "Could not create the vsync event.";
6771
return;
6872
}
6973

70-
// Launch the threads that will be used to run the shell. These threads will
71-
// be joined in the destructor.
72-
for (auto& thread : threads_) {
73-
thread.reset(new Thread());
74-
}
75-
7674
// Set up the session connection.
7775
auto scenic = svc->Connect<fuchsia::ui::scenic::Scenic>();
7876
fidl::InterfaceHandle<fuchsia::ui::scenic::Session> session;
@@ -167,12 +165,14 @@ Engine::Engine(Delegate& delegate,
167165

168166
// Get the task runners from the managed threads. The current thread will be
169167
// used as the "platform" thread.
168+
fml::MessageLoop::EnsureInitializedForCurrentThread();
169+
170170
const flutter::TaskRunners task_runners(
171-
thread_label_, // Dart thread labels
172-
CreateFMLTaskRunner(async_get_default_dispatcher()), // platform
173-
CreateFMLTaskRunner(threads_[0]->dispatcher()), // gpu
174-
CreateFMLTaskRunner(threads_[1]->dispatcher()), // ui
175-
CreateFMLTaskRunner(threads_[2]->dispatcher()) // io
171+
thread_label_, // Dart thread labels
172+
fml::MessageLoop::GetCurrent().GetTaskRunner(), // platform
173+
thread_host_.gpu_thread->GetTaskRunner(), // gpu
174+
thread_host_.ui_thread->GetTaskRunner(), // ui
175+
thread_host_.io_thread->GetTaskRunner() // io
176176
);
177177

178178
// Setup the callback that will instantiate the rasterizer.
@@ -353,12 +353,6 @@ Engine::Engine(Delegate& delegate,
353353

354354
Engine::~Engine() {
355355
shell_.reset();
356-
for (const auto& thread : threads_) {
357-
thread->Quit();
358-
}
359-
for (const auto& thread : threads_) {
360-
thread->Join();
361-
}
362356
}
363357

364358
std::pair<bool, uint32_t> Engine::GetEngineReturnCode() const {

shell/platform/fuchsia/flutter/engine.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
#include "flutter/fml/macros.h"
1818
#include "flutter/shell/common/shell.h"
19+
#include "flutter/shell/common/thread_host.h"
1920
#include "isolate_configurator.h"
20-
#include "thread.h"
2121

2222
namespace flutter_runner {
2323

@@ -53,8 +53,8 @@ class Engine final {
5353
private:
5454
Delegate& delegate_;
5555
const std::string thread_label_;
56+
flutter::ThreadHost thread_host_;
5657
flutter::Settings settings_;
57-
std::array<std::unique_ptr<Thread>, 3> threads_;
5858
std::unique_ptr<IsolateConfigurator> isolate_configurator_;
5959
std::unique_ptr<flutter::Shell> shell_;
6060
zx::event vsync_event_;

shell/platform/fuchsia/flutter/engine_flutter_runner.gni

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ template("flutter_runner") {
5555
"isolate_configurator.cc",
5656
"isolate_configurator.h",
5757
"logging.h",
58-
"loop.cc",
59-
"loop.h",
6058
"main.cc",
6159
"platform_view.cc",
6260
"platform_view.h",
@@ -66,12 +64,6 @@ template("flutter_runner") {
6664
"session_connection.h",
6765
"surface.cc",
6866
"surface.h",
69-
"task_observers.cc",
70-
"task_observers.h",
71-
"task_runner_adapter.cc",
72-
"task_runner_adapter.h",
73-
"thread.cc",
74-
"thread.h",
7567
"unique_fdio_ns.h",
7668
"vsync_recorder.cc",
7769
"vsync_recorder.h",

shell/platform/fuchsia/flutter/fuchsia_intl.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <string>
99
#include <vector>
1010

11-
#include "loop.h"
1211
#include "rapidjson/document.h"
1312
#include "rapidjson/stringbuffer.h"
1413
#include "rapidjson/writer.h"

shell/platform/fuchsia/flutter/loop.cc

Lines changed: 0 additions & 47 deletions
This file was deleted.

shell/platform/fuchsia/flutter/loop.h

Lines changed: 0 additions & 17 deletions
This file was deleted.

shell/platform/fuchsia/flutter/main.cc

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,40 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
#define FML_USED_ON_EMBEDDER
6+
57
#include <lib/async-loop/cpp/loop.h>
8+
#include <lib/async-loop/default.h>
69
#include <lib/trace-provider/provider.h>
710
#include <lib/trace/event.h>
811

912
#include <cstdlib>
1013

11-
#include "loop.h"
1214
#include "runner.h"
1315
#include "runtime/dart/utils/tempfs.h"
1416

1517
int main(int argc, char const* argv[]) {
16-
std::unique_ptr<async::Loop> loop(flutter_runner::MakeObservableLoop(true));
18+
fml::MessageLoop::EnsureInitializedForCurrentThread();
19+
auto& message_loop = fml::MessageLoop::GetCurrent();
1720

1821
std::unique_ptr<trace::TraceProviderWithFdio> provider;
1922
{
2023
TRACE_DURATION("flutter", "CreateTraceProvider");
2124
bool already_started;
2225
// Use CreateSynchronously to prevent loss of early events.
2326
trace::TraceProviderWithFdio::CreateSynchronously(
24-
loop->dispatcher(), "flutter_runner", &provider, &already_started);
27+
async_get_default_dispatcher(), "flutter_runner", &provider,
28+
&already_started);
2529
}
2630

2731
// Set up the process-wide /tmp memfs.
2832
dart_utils::RunnerTemp runner_temp;
2933

3034
FML_DLOG(INFO) << "Flutter application services initialized.";
3135

32-
flutter_runner::Runner runner(loop.get());
36+
flutter_runner::Runner runner(message_loop);
3337

34-
loop->Run();
38+
message_loop.Run();
3539

3640
FML_DLOG(INFO) << "Flutter application services terminated.";
3741

0 commit comments

Comments
 (0)