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

Commit 69bc783

Browse files
author
George Wright
authored
Revert "Migrate flutter_runner from flutter_runner::{Thread,Loop} to fml::{Thread,MessageLoop} (#15118)" (#15903)
This reverts commit a50f1ef.
1 parent 973cfbc commit 69bc783

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
@@ -1020,6 +1020,8 @@ 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
10231025
FILE: ../../../flutter/shell/platform/fuchsia/flutter/main.cc
10241026
FILE: ../../../flutter/shell/platform/fuchsia/flutter/meta/aot_product_runtime
10251027
FILE: ../../../flutter/shell/platform/fuchsia/flutter/meta/aot_runtime
@@ -1042,6 +1044,12 @@ FILE: ../../../flutter/shell/platform/fuchsia/flutter/session_connection.cc
10421044
FILE: ../../../flutter/shell/platform/fuchsia/flutter/session_connection.h
10431045
FILE: ../../../flutter/shell/platform/fuchsia/flutter/surface.cc
10441046
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
10451053
FILE: ../../../flutter/shell/platform/fuchsia/flutter/unique_fdio_ns.h
10461054
FILE: ../../../flutter/shell/platform/fuchsia/flutter/vsync_recorder.cc
10471055
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>
@@ -38,6 +36,10 @@
3836
#include "runtime/dart/utils/tempfs.h"
3937
#include "runtime/dart/utils/vmo.h"
4038

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

6365
fml::AutoResetWaitableEvent latch;
64-
fml::TaskRunner::RunNowOrPostTask(thread->GetTaskRunner(), [&]() mutable {
66+
async::PostTask(thread->dispatcher(), [&]() mutable {
6567
application.reset(
6668
new Application(std::move(termination_callback), std::move(package),
6769
std::move(startup_info), runner_incoming_services,
@@ -345,12 +347,12 @@ Application::Application(
345347
settings_.disable_dart_asserts = true;
346348
#endif
347349

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-
};
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);
354356

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

379381
auto weak_application = weak_factory_.GetWeakPtr();
380-
auto platform_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
382+
auto platform_task_runner =
383+
CreateFMLTaskRunner(async_get_default_dispatcher());
381384
const std::string component_url = package.resolved_url;
382385
settings_.unhandled_exception_callback = [weak_application,
383386
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>
@@ -20,7 +18,9 @@
2018
#include "fuchsia_intl.h"
2119
#include "platform_view.h"
2220
#include "runtime/dart/utils/files.h"
21+
#include "task_runner_adapter.h"
2322
#include "third_party/skia/include/ports/SkFontMgr_fuchsia.h"
23+
#include "thread.h"
2424

2525
namespace flutter_runner {
2626

@@ -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;
@@ -165,14 +167,12 @@ Engine::Engine(Delegate& delegate,
165167

166168
// Get the task runners from the managed threads. The current thread will be
167169
// used as the "platform" thread.
168-
fml::MessageLoop::EnsureInitializedForCurrentThread();
169-
170170
const flutter::TaskRunners task_runners(
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
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
176176
);
177177

178178
// Setup the callback that will instantiate the rasterizer.
@@ -353,6 +353,12 @@ 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+
}
356362
}
357363

358364
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"
2019
#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_;
5756
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: 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

0 commit comments

Comments
 (0)