Skip to content

Commit 5a49ee4

Browse files
author
Hunter Freyer
committed
Revert "Revert "Migrate flutter_runner from flutter_runner::{Thread,Loop} to fml::{Thread,MessageLoop} (flutter#15118)" (flutter#15903)"
This reverts commit 69bc783.
1 parent ced29b2 commit 5a49ee4

20 files changed

+114
-433
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,8 +1306,6 @@ FILE: ../../../flutter/shell/platform/fuchsia/flutter/keyboard.cc
13061306
FILE: ../../../flutter/shell/platform/fuchsia/flutter/keyboard.h
13071307
FILE: ../../../flutter/shell/platform/fuchsia/flutter/keyboard_unittest.cc
13081308
FILE: ../../../flutter/shell/platform/fuchsia/flutter/logging.h
1309-
FILE: ../../../flutter/shell/platform/fuchsia/flutter/loop.cc
1310-
FILE: ../../../flutter/shell/platform/fuchsia/flutter/loop.h
13111309
FILE: ../../../flutter/shell/platform/fuchsia/flutter/main.cc
13121310
FILE: ../../../flutter/shell/platform/fuchsia/flutter/meta/aot_product_runtime
13131311
FILE: ../../../flutter/shell/platform/fuchsia/flutter/meta/aot_runtime
@@ -1331,12 +1329,6 @@ FILE: ../../../flutter/shell/platform/fuchsia/flutter/session_connection.cc
13311329
FILE: ../../../flutter/shell/platform/fuchsia/flutter/session_connection.h
13321330
FILE: ../../../flutter/shell/platform/fuchsia/flutter/surface.cc
13331331
FILE: ../../../flutter/shell/platform/fuchsia/flutter/surface.h
1334-
FILE: ../../../flutter/shell/platform/fuchsia/flutter/task_observers.cc
1335-
FILE: ../../../flutter/shell/platform/fuchsia/flutter/task_observers.h
1336-
FILE: ../../../flutter/shell/platform/fuchsia/flutter/task_runner_adapter.cc
1337-
FILE: ../../../flutter/shell/platform/fuchsia/flutter/task_runner_adapter.h
1338-
FILE: ../../../flutter/shell/platform/fuchsia/flutter/thread.cc
1339-
FILE: ../../../flutter/shell/platform/fuchsia/flutter/thread.h
13401332
FILE: ../../../flutter/shell/platform/fuchsia/flutter/unique_fdio_ns.h
13411333
FILE: ../../../flutter/shell/platform/fuchsia/flutter/vsync_recorder.cc
13421334
FILE: ../../../flutter/shell/platform/fuchsia/flutter/vsync_recorder.h

fml/thread.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ void Thread::SetCurrentThreadName(const std::string& name) {
8686
reinterpret_cast<DWORD_PTR*>(&info));
8787
} __except (EXCEPTION_CONTINUE_EXECUTION) {
8888
}
89-
#elif defined(OS_FUCHSIA)
90-
zx::thread::self()->set_property(ZX_PROP_NAME, name.c_str(), name.size());
89+
9190
#else
9291
FML_DLOG(INFO) << "Could not set the thread name to '" << name
9392
<< "' on this platform.";

shell/platform/fuchsia/flutter/BUILD.gn

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ template("runner_sources") {
6666
"keyboard.cc",
6767
"keyboard.h",
6868
"logging.h",
69-
"loop.cc",
70-
"loop.h",
7169
"platform_view.cc",
7270
"platform_view.h",
7371
"runner.cc",
@@ -76,12 +74,6 @@ template("runner_sources") {
7674
"session_connection.h",
7775
"surface.cc",
7876
"surface.h",
79-
"task_observers.cc",
80-
"task_observers.h",
81-
"task_runner_adapter.cc",
82-
"task_runner_adapter.h",
83-
"thread.cc",
84-
"thread.h",
8577
"unique_fdio_ns.h",
8678
"vsync_recorder.cc",
8779
"vsync_recorder.h",

shell/platform/fuchsia/flutter/component.cc

Lines changed: 11 additions & 13 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>
@@ -37,9 +39,6 @@
3739
#include "runtime/dart/utils/tempfs.h"
3840
#include "runtime/dart/utils/vmo.h"
3941

40-
#include "task_observers.h"
41-
#include "task_runner_adapter.h"
42-
4342
// TODO(kaushikiska): Use these constants from ::llcpp::fuchsia::io
4443
// Can read from target object.
4544
constexpr uint32_t OPEN_RIGHT_READABLE = 1u;
@@ -186,11 +185,11 @@ ActiveApplication Application::Create(
186185
fuchsia::sys::StartupInfo startup_info,
187186
std::shared_ptr<sys::ServiceDirectory> runner_incoming_services,
188187
fidl::InterfaceRequest<fuchsia::sys::ComponentController> controller) {
189-
std::unique_ptr<Thread> thread = std::make_unique<Thread>();
188+
std::unique_ptr<fml::Thread> thread = std::make_unique<fml::Thread>();
190189
std::unique_ptr<Application> application;
191190

192191
fml::AutoResetWaitableEvent latch;
193-
async::PostTask(thread->dispatcher(), [&]() mutable {
192+
fml::TaskRunner::RunNowOrPostTask(thread->GetTaskRunner(), [&]() mutable {
194193
application.reset(
195194
new Application(std::move(termination_callback), std::move(package),
196195
std::move(startup_info), runner_incoming_services,
@@ -490,12 +489,12 @@ Application::Application(
490489
// terminates.
491490
settings_.leak_vm = false;
492491

493-
settings_.task_observer_add =
494-
std::bind(&CurrentMessageLoopAddAfterTaskObserver, std::placeholders::_1,
495-
std::placeholders::_2);
496-
497-
settings_.task_observer_remove = std::bind(
498-
&CurrentMessageLoopRemoveAfterTaskObserver, std::placeholders::_1);
492+
settings_.task_observer_add = [](intptr_t key, fml::closure callback) {
493+
fml::MessageLoop::GetCurrent().AddTaskObserver(key, std::move(callback));
494+
};
495+
settings_.task_observer_remove = [](intptr_t key) {
496+
fml::MessageLoop::GetCurrent().RemoveTaskObserver(key);
497+
};
499498

500499
settings_.dart_flags = {"--no_causal_async_stacks", "--lazy_async_stacks"};
501500

@@ -509,8 +508,7 @@ Application::Application(
509508
#endif // defined(__aarch64__)
510509

511510
auto weak_application = weak_factory_.GetWeakPtr();
512-
auto platform_task_runner =
513-
CreateFMLTaskRunner(async_get_default_dispatcher());
511+
auto platform_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
514512
const std::string component_url = package.resolved_url;
515513
settings_.unhandled_exception_callback = [weak_application,
516514
platform_task_runner,

shell/platform/fuchsia/flutter/component.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@
2323

2424
#include "flutter/common/settings.h"
2525
#include "flutter/fml/macros.h"
26+
#include "flutter/fml/thread.h"
2627

2728
#include "engine.h"
2829
#include "flutter_runner_product_configuration.h"
29-
#include "thread.h"
3030
#include "unique_fdio_ns.h"
3131

3232
namespace flutter_runner {
3333

3434
class Application;
3535

3636
struct ActiveApplication {
37-
std::unique_ptr<Thread> thread;
37+
std::unique_ptr<fml::Thread> thread;
3838
std::unique_ptr<Application> application;
3939

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

shell/platform/fuchsia/flutter/engine.cc

Lines changed: 35 additions & 23 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>
@@ -24,7 +26,6 @@
2426
#include "fuchsia_intl.h"
2527
#include "platform_view.h"
2628
#include "surface.h"
27-
#include "task_runner_adapter.h"
2829

2930
#if defined(LEGACY_FUCHSIA_EMBEDDER)
3031
#include "compositor_context.h" // nogncheck
@@ -71,28 +72,17 @@ Engine::Engine(Delegate& delegate,
7172
FlutterRunnerProductConfiguration product_config)
7273
: delegate_(delegate),
7374
thread_label_(std::move(thread_label)),
74-
#if defined(LEGACY_FUCHSIA_EMBEDDER)
75-
use_legacy_renderer_(product_config.use_legacy_renderer()),
76-
#endif
77-
intercept_all_input_(product_config.get_intercept_all_input()),
75+
thread_host_(thread_label_ + ".",
76+
flutter::ThreadHost::Type::IO |
77+
flutter::ThreadHost::Type::UI |
78+
flutter::ThreadHost::Type::GPU),
7879
weak_factory_(this) {
7980
if (zx::event::create(0, &vsync_event_) != ZX_OK) {
8081
FML_DLOG(ERROR) << "Could not create the vsync event.";
8182
return;
8283
}
8384

84-
// Get the task runners from the managed threads. The current thread will be
85-
// used as the "platform" thread.
86-
const flutter::TaskRunners task_runners(
87-
thread_label_, // Dart thread labels
88-
CreateFMLTaskRunner(async_get_default_dispatcher()), // platform
89-
CreateFMLTaskRunner(threads_[0].dispatcher()), // raster
90-
CreateFMLTaskRunner(threads_[1].dispatcher()), // ui
91-
CreateFMLTaskRunner(threads_[2].dispatcher()) // io
92-
);
93-
UpdateNativeThreadLabelNames(thread_label_, task_runners);
94-
95-
// Connect to Scenic.
85+
// Set up the session connection.
9686
auto scenic = svc->Connect<fuchsia::ui::scenic::Scenic>();
9787
fidl::InterfaceHandle<fuchsia::ui::scenic::Session> session;
9888
fidl::InterfaceHandle<fuchsia::ui::scenic::SessionListener> session_listener;
@@ -274,6 +264,34 @@ Engine::Engine(Delegate& delegate,
274264
vsync_handle);
275265
});
276266

267+
// Session can be terminated on the GPU thread, but we must terminate
268+
// ourselves on the platform thread.
269+
//
270+
// This handles the fidl error callback when the Session connection is
271+
// broken. The SessionListener interface also has an OnError method, which is
272+
// invoked on the platform thread (in PlatformView).
273+
fml::closure on_session_error_callback =
274+
[dispatcher = async_get_default_dispatcher(),
275+
weak = weak_factory_.GetWeakPtr()]() {
276+
async::PostTask(dispatcher, [weak]() {
277+
if (weak) {
278+
weak->Terminate();
279+
}
280+
});
281+
};
282+
283+
// Get the task runners from the managed threads. The current thread will be
284+
// used as the "platform" thread.
285+
fml::MessageLoop::EnsureInitializedForCurrentThread();
286+
287+
const flutter::TaskRunners task_runners(
288+
thread_label_, // Dart thread labels
289+
fml::MessageLoop::GetCurrent().GetTaskRunner(), // platform
290+
thread_host_.gpu_thread->GetTaskRunner(), // gpu
291+
thread_host_.ui_thread->GetTaskRunner(), // ui
292+
thread_host_.io_thread->GetTaskRunner() // io
293+
);
294+
277295
// Setup the callback that will instantiate the rasterizer.
278296
flutter::Shell::CreateCallback<flutter::Rasterizer> on_create_rasterizer;
279297
#if defined(LEGACY_FUCHSIA_EMBEDDER)
@@ -454,12 +472,6 @@ Engine::Engine(Delegate& delegate,
454472

455473
Engine::~Engine() {
456474
shell_.reset();
457-
for (auto& thread : threads_) {
458-
thread.Quit();
459-
}
460-
for (auto& thread : threads_) {
461-
thread.Join();
462-
}
463475
}
464476

465477
std::optional<uint32_t> Engine::GetEngineReturnCode() const {

shell/platform/fuchsia/flutter/engine.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323

2424
#include "flutter_runner_product_configuration.h"
2525
#include "fuchsia_external_view_embedder.h"
26+
#include "flutter/shell/common/thread_host.h"
2627
#include "isolate_configurator.h"
2728
#include "session_connection.h"
28-
#include "thread.h"
2929
#include "vulkan_surface_producer.h"
3030

3131
#if defined(LEGACY_FUCHSIA_EMBEDDER)
@@ -71,8 +71,8 @@ class Engine final {
7171
Delegate& delegate_;
7272

7373
const std::string thread_label_;
74-
std::array<Thread, 3> threads_;
75-
74+
flutter::ThreadHost thread_host_;
75+
7676
std::optional<SessionConnection> session_connection_;
7777
std::optional<VulkanSurfaceProducer> surface_producer_;
7878
std::shared_ptr<FuchsiaExternalViewEmbedder> external_view_embedder_;

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.

0 commit comments

Comments
 (0)