Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit f5de1ac

Browse files
author
George Wright
authored
Revert "Migrate flutter_runner from flutter_runner::{Thread,Loop} to fml::{Thread,MessageLoop} (#15118)" (#16277)
This reverts commit 41e8ed0.
1 parent 4641a09 commit f5de1ac

21 files changed

+448
-75
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,8 @@ FILE: ../../../flutter/shell/platform/fuchsia/flutter/kernel/extract_far.dart
10251025
FILE: ../../../flutter/shell/platform/fuchsia/flutter/kernel/framework_shim.dart
10261026
FILE: ../../../flutter/shell/platform/fuchsia/flutter/kernel/libraries.json
10271027
FILE: ../../../flutter/shell/platform/fuchsia/flutter/logging.h
1028+
FILE: ../../../flutter/shell/platform/fuchsia/flutter/loop.cc
1029+
FILE: ../../../flutter/shell/platform/fuchsia/flutter/loop.h
10281030
FILE: ../../../flutter/shell/platform/fuchsia/flutter/main.cc
10291031
FILE: ../../../flutter/shell/platform/fuchsia/flutter/meta/aot_product_runtime
10301032
FILE: ../../../flutter/shell/platform/fuchsia/flutter/meta/aot_runtime
@@ -1047,6 +1049,12 @@ FILE: ../../../flutter/shell/platform/fuchsia/flutter/session_connection.cc
10471049
FILE: ../../../flutter/shell/platform/fuchsia/flutter/session_connection.h
10481050
FILE: ../../../flutter/shell/platform/fuchsia/flutter/surface.cc
10491051
FILE: ../../../flutter/shell/platform/fuchsia/flutter/surface.h
1052+
FILE: ../../../flutter/shell/platform/fuchsia/flutter/task_observers.cc
1053+
FILE: ../../../flutter/shell/platform/fuchsia/flutter/task_observers.h
1054+
FILE: ../../../flutter/shell/platform/fuchsia/flutter/task_runner_adapter.cc
1055+
FILE: ../../../flutter/shell/platform/fuchsia/flutter/task_runner_adapter.h
1056+
FILE: ../../../flutter/shell/platform/fuchsia/flutter/thread.cc
1057+
FILE: ../../../flutter/shell/platform/fuchsia/flutter/thread.h
10501058
FILE: ../../../flutter/shell/platform/fuchsia/flutter/unique_fdio_ns.h
10511059
FILE: ../../../flutter/shell/platform/fuchsia/flutter/vsync_recorder.cc
10521060
FILE: ../../../flutter/shell/platform/fuchsia/flutter/vsync_recorder.h

fml/thread.cc

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

1111
#if defined(OS_WIN)
1212
#include <windows.h>
13-
#elif defined(OS_FUCHSIA)
14-
#include <lib/zx/thread.h>
1513
#else
1614
#include <pthread.h>
1715
#endif
@@ -87,8 +85,6 @@ void Thread::SetCurrentThreadName(const std::string& name) {
8785
reinterpret_cast<DWORD_PTR*>(&info));
8886
} __except (EXCEPTION_CONTINUE_EXECUTION) {
8987
}
90-
#elif OS_FUCHSIA
91-
zx::thread::self()->set_property(ZX_PROP_NAME, name.c_str(), name.size());
9288
#else
9389
FML_DLOG(INFO) << "Could not set the thread name to '" << name
9490
<< "' on this platform.";

shell/platform/fuchsia/flutter/BUILD.gn

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ executable("flutter_runner_unittests") {
303303
"fuchsia_intl.h",
304304
"fuchsia_intl_unittest.cc",
305305
"logging.h",
306+
"loop.cc",
307+
"loop.h",
306308
"platform_view.cc",
307309
"platform_view.h",
308310
"platform_view_unittest.cc",
@@ -311,6 +313,12 @@ executable("flutter_runner_unittests") {
311313
"runner_unittest.cc",
312314
"surface.cc",
313315
"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",
314322
"vsync_recorder.cc",
315323
"vsync_recorder.h",
316324
"vsync_waiter.cc",

shell/platform/fuchsia/flutter/component.cc

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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-
75
#include "component.h"
86

97
#include <dlfcn.h>
@@ -37,6 +35,10 @@
3735
#include "runtime/dart/utils/tempfs.h"
3836
#include "runtime/dart/utils/vmo.h"
3937

38+
#include "task_observers.h"
39+
#include "task_runner_adapter.h"
40+
#include "thread.h"
41+
4042
// TODO(kaushikiska): Use these constants from ::llcpp::fuchsia::io
4143
// Can read from target object.
4244
constexpr uint32_t OPEN_RIGHT_READABLE = 1u;
@@ -56,11 +58,11 @@ ActiveApplication Application::Create(
5658
fuchsia::sys::StartupInfo startup_info,
5759
std::shared_ptr<sys::ServiceDirectory> runner_incoming_services,
5860
fidl::InterfaceRequest<fuchsia::sys::ComponentController> controller) {
59-
std::unique_ptr<fml::Thread> thread = std::make_unique<fml::Thread>();
61+
std::unique_ptr<Thread> thread = std::make_unique<Thread>();
6062
std::unique_ptr<Application> application;
6163

6264
fml::AutoResetWaitableEvent latch;
63-
fml::TaskRunner::RunNowOrPostTask(thread->GetTaskRunner(), [&]() mutable {
65+
async::PostTask(thread->dispatcher(), [&]() mutable {
6466
application.reset(
6567
new Application(std::move(termination_callback), std::move(package),
6668
std::move(startup_info), runner_incoming_services,
@@ -344,12 +346,12 @@ Application::Application(
344346
settings_.disable_dart_asserts = true;
345347
#endif
346348

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

354356
// TODO(FL-117): Re-enable causal async stack traces when this issue is
355357
// addressed.
@@ -376,7 +378,8 @@ Application::Application(
376378
#endif // defined(__aarch64__)
377379

378380
auto weak_application = weak_factory_.GetWeakPtr();
379-
auto platform_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
381+
auto platform_task_runner =
382+
CreateFMLTaskRunner(async_get_default_dispatcher());
380383
const std::string component_url = package.resolved_url;
381384
settings_.unhandled_exception_callback = [weak_application,
382385
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"
2827

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<fml::Thread> thread;
36+
std::unique_ptr<Thread> thread;
3737
std::unique_ptr<Application> application;
3838

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

shell/platform/fuchsia/flutter/engine.cc

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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-
75
#include "engine.h"
86

97
#include <lib/async/cpp/task.h>
@@ -21,7 +19,9 @@
2119
#include "fuchsia_intl.h"
2220
#include "platform_view.h"
2321
#include "runtime/dart/utils/files.h"
22+
#include "task_runner_adapter.h"
2423
#include "third_party/skia/include/ports/SkFontMgr_fuchsia.h"
24+
#include "thread.h"
2525

2626
namespace flutter_runner {
2727

@@ -60,17 +60,19 @@ 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),
6763
settings_(std::move(settings)),
6864
weak_factory_(this) {
6965
if (zx::event::create(0, &vsync_event_) != ZX_OK) {
7066
FML_DLOG(ERROR) << "Could not create the vsync event.";
7167
return;
7268
}
7369

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+
7476
// Set up the session connection.
7577
auto scenic = svc->Connect<fuchsia::ui::scenic::Scenic>();
7678
fidl::InterfaceHandle<fuchsia::ui::scenic::Session> session;
@@ -170,14 +172,12 @@ Engine::Engine(Delegate& delegate,
170172

171173
// Get the task runners from the managed threads. The current thread will be
172174
// used as the "platform" thread.
173-
fml::MessageLoop::EnsureInitializedForCurrentThread();
174-
175175
const flutter::TaskRunners task_runners(
176-
thread_label_, // Dart thread labels
177-
fml::MessageLoop::GetCurrent().GetTaskRunner(), // platform
178-
thread_host_.gpu_thread->GetTaskRunner(), // gpu
179-
thread_host_.ui_thread->GetTaskRunner(), // ui
180-
thread_host_.io_thread->GetTaskRunner() // io
176+
thread_label_, // Dart thread labels
177+
CreateFMLTaskRunner(async_get_default_dispatcher()), // platform
178+
CreateFMLTaskRunner(threads_[0]->dispatcher()), // gpu
179+
CreateFMLTaskRunner(threads_[1]->dispatcher()), // ui
180+
CreateFMLTaskRunner(threads_[2]->dispatcher()) // io
181181
);
182182

183183
// Setup the callback that will instantiate the rasterizer.
@@ -359,6 +359,12 @@ Engine::Engine(Delegate& delegate,
359359

360360
Engine::~Engine() {
361361
shell_.reset();
362+
for (const auto& thread : threads_) {
363+
thread->Quit();
364+
}
365+
for (const auto& thread : threads_) {
366+
thread->Join();
367+
}
362368
}
363369

364370
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
@@ -15,8 +15,8 @@
1515

1616
#include "flutter/fml/macros.h"
1717
#include "flutter/shell/common/shell.h"
18-
#include "flutter/shell/common/thread_host.h"
1918
#include "isolate_configurator.h"
19+
#include "thread.h"
2020

2121
namespace flutter_runner {
2222

@@ -51,8 +51,8 @@ class Engine final {
5151
private:
5252
Delegate& delegate_;
5353
const std::string thread_label_;
54-
flutter::ThreadHost thread_host_;
5554
flutter::Settings settings_;
55+
std::array<std::unique_ptr<Thread>, 3> threads_;
5656
std::unique_ptr<IsolateConfigurator> isolate_configurator_;
5757
std::unique_ptr<flutter::Shell> shell_;
5858
zx::event vsync_event_;

shell/platform/fuchsia/flutter/engine_flutter_runner.gni

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ template("flutter_runner") {
5555
"isolate_configurator.cc",
5656
"isolate_configurator.h",
5757
"logging.h",
58+
"loop.cc",
59+
"loop.h",
5860
"main.cc",
5961
"platform_view.cc",
6062
"platform_view.h",
@@ -64,6 +66,12 @@ template("flutter_runner") {
6466
"session_connection.h",
6567
"surface.cc",
6668
"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",
6775
"unique_fdio_ns.h",
6876
"vsync_recorder.cc",
6977
"vsync_recorder.h",

shell/platform/fuchsia/flutter/fuchsia_intl.cc

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

11+
#include "loop.h"
1112
#include "rapidjson/document.h"
1213
#include "rapidjson/stringbuffer.h"
1314
#include "rapidjson/writer.h"
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "loop.h"
6+
7+
#include <lib/async-loop/loop.h>
8+
#include <lib/async/default.h>
9+
10+
#include "task_observers.h"
11+
12+
namespace flutter_runner {
13+
14+
namespace {
15+
16+
static void LoopEpilogue(async_loop_t*, void*) {
17+
ExecuteAfterTaskObservers();
18+
}
19+
20+
constexpr async_loop_config_t kAttachedLoopConfig = {
21+
.default_accessors =
22+
{
23+
.getter = async_get_default_dispatcher,
24+
.setter = async_set_default_dispatcher,
25+
},
26+
.make_default_for_current_thread = true,
27+
.epilogue = &LoopEpilogue,
28+
};
29+
30+
constexpr async_loop_config_t kDetachedLoopConfig = {
31+
.default_accessors =
32+
{
33+
.getter = async_get_default_dispatcher,
34+
.setter = async_set_default_dispatcher,
35+
},
36+
.make_default_for_current_thread = false,
37+
.epilogue = &LoopEpilogue,
38+
};
39+
40+
} // namespace
41+
42+
async::Loop* MakeObservableLoop(bool attachToThread) {
43+
return new async::Loop(
44+
&(attachToThread ? kAttachedLoopConfig : kDetachedLoopConfig));
45+
}
46+
47+
} // namespace flutter_runner

shell/platform/fuchsia/flutter/loop.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_LOOP_H_
6+
#define FLUTTER_SHELL_PLATFORM_FUCHSIA_LOOP_H_
7+
8+
#include <lib/async-loop/cpp/loop.h>
9+
10+
namespace flutter_runner {
11+
12+
// Creates a loop which allows task observers to be attached to it.
13+
async::Loop* MakeObservableLoop(bool attachToThread);
14+
15+
} // namespace flutter_runner
16+
17+
#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_LOOP_H_

shell/platform/fuchsia/flutter/main.cc

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,36 @@
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-
75
#include <lib/async-loop/cpp/loop.h>
8-
#include <lib/async-loop/default.h>
96
#include <lib/trace-provider/provider.h>
107
#include <lib/trace/event.h>
118

129
#include <cstdlib>
1310

11+
#include "loop.h"
1412
#include "runner.h"
1513
#include "runtime/dart/utils/tempfs.h"
1614

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

2118
std::unique_ptr<trace::TraceProviderWithFdio> provider;
2219
{
2320
TRACE_DURATION("flutter", "CreateTraceProvider");
2421
bool already_started;
2522
// Use CreateSynchronously to prevent loss of early events.
2623
trace::TraceProviderWithFdio::CreateSynchronously(
27-
async_get_default_dispatcher(), "flutter_runner", &provider,
28-
&already_started);
24+
loop->dispatcher(), "flutter_runner", &provider, &already_started);
2925
}
3026

3127
// Set up the process-wide /tmp memfs.
3228
dart_utils::RunnerTemp runner_temp;
3329

3430
FML_DLOG(INFO) << "Flutter application services initialized.";
3531

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

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

4036
FML_DLOG(INFO) << "Flutter application services terminated.";
4137

0 commit comments

Comments
 (0)