Skip to content

Commit

Permalink
Revert "Revert "Migrate flutter_runner from flutter_runner::{Thread,L…
Browse files Browse the repository at this point in the history
…oop} to fml::{Thread,MessageLoop} (flutter#15118)" (flutter#15903)"

This reverts commit 69bc783.
  • Loading branch information
Hunter Freyer committed Apr 1, 2021
1 parent ced29b2 commit a21820f
Show file tree
Hide file tree
Showing 20 changed files with 114 additions and 433 deletions.
8 changes: 0 additions & 8 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -1306,8 +1306,6 @@ FILE: ../../../flutter/shell/platform/fuchsia/flutter/keyboard.cc
FILE: ../../../flutter/shell/platform/fuchsia/flutter/keyboard.h
FILE: ../../../flutter/shell/platform/fuchsia/flutter/keyboard_unittest.cc
FILE: ../../../flutter/shell/platform/fuchsia/flutter/logging.h
FILE: ../../../flutter/shell/platform/fuchsia/flutter/loop.cc
FILE: ../../../flutter/shell/platform/fuchsia/flutter/loop.h
FILE: ../../../flutter/shell/platform/fuchsia/flutter/main.cc
FILE: ../../../flutter/shell/platform/fuchsia/flutter/meta/aot_product_runtime
FILE: ../../../flutter/shell/platform/fuchsia/flutter/meta/aot_runtime
Expand All @@ -1331,12 +1329,6 @@ FILE: ../../../flutter/shell/platform/fuchsia/flutter/session_connection.cc
FILE: ../../../flutter/shell/platform/fuchsia/flutter/session_connection.h
FILE: ../../../flutter/shell/platform/fuchsia/flutter/surface.cc
FILE: ../../../flutter/shell/platform/fuchsia/flutter/surface.h
FILE: ../../../flutter/shell/platform/fuchsia/flutter/task_observers.cc
FILE: ../../../flutter/shell/platform/fuchsia/flutter/task_observers.h
FILE: ../../../flutter/shell/platform/fuchsia/flutter/task_runner_adapter.cc
FILE: ../../../flutter/shell/platform/fuchsia/flutter/task_runner_adapter.h
FILE: ../../../flutter/shell/platform/fuchsia/flutter/thread.cc
FILE: ../../../flutter/shell/platform/fuchsia/flutter/thread.h
FILE: ../../../flutter/shell/platform/fuchsia/flutter/unique_fdio_ns.h
FILE: ../../../flutter/shell/platform/fuchsia/flutter/vsync_recorder.cc
FILE: ../../../flutter/shell/platform/fuchsia/flutter/vsync_recorder.h
Expand Down
3 changes: 1 addition & 2 deletions fml/thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ void Thread::SetCurrentThreadName(const std::string& name) {
reinterpret_cast<DWORD_PTR*>(&info));
} __except (EXCEPTION_CONTINUE_EXECUTION) {
}
#elif defined(OS_FUCHSIA)
zx::thread::self()->set_property(ZX_PROP_NAME, name.c_str(), name.size());

#else
FML_DLOG(INFO) << "Could not set the thread name to '" << name
<< "' on this platform.";
Expand Down
8 changes: 0 additions & 8 deletions shell/platform/fuchsia/flutter/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ template("runner_sources") {
"keyboard.cc",
"keyboard.h",
"logging.h",
"loop.cc",
"loop.h",
"platform_view.cc",
"platform_view.h",
"runner.cc",
Expand All @@ -76,12 +74,6 @@ template("runner_sources") {
"session_connection.h",
"surface.cc",
"surface.h",
"task_observers.cc",
"task_observers.h",
"task_runner_adapter.cc",
"task_runner_adapter.h",
"thread.cc",
"thread.h",
"unique_fdio_ns.h",
"vsync_recorder.cc",
"vsync_recorder.h",
Expand Down
24 changes: 11 additions & 13 deletions shell/platform/fuchsia/flutter/component.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#define FML_USED_ON_EMBEDDER

#include "component.h"

#include <dlfcn.h>
Expand Down Expand Up @@ -37,9 +39,6 @@
#include "runtime/dart/utils/tempfs.h"
#include "runtime/dart/utils/vmo.h"

#include "task_observers.h"
#include "task_runner_adapter.h"

// TODO(kaushikiska): Use these constants from ::llcpp::fuchsia::io
// Can read from target object.
constexpr uint32_t OPEN_RIGHT_READABLE = 1u;
Expand Down Expand Up @@ -186,11 +185,11 @@ ActiveApplication Application::Create(
fuchsia::sys::StartupInfo startup_info,
std::shared_ptr<sys::ServiceDirectory> runner_incoming_services,
fidl::InterfaceRequest<fuchsia::sys::ComponentController> controller) {
std::unique_ptr<Thread> thread = std::make_unique<Thread>();
std::unique_ptr<fml::Thread> thread = std::make_unique<fml::Thread>();
std::unique_ptr<Application> application;

fml::AutoResetWaitableEvent latch;
async::PostTask(thread->dispatcher(), [&]() mutable {
fml::TaskRunner::RunNowOrPostTask(thread->GetTaskRunner(), [&]() mutable {
application.reset(
new Application(std::move(termination_callback), std::move(package),
std::move(startup_info), runner_incoming_services,
Expand Down Expand Up @@ -490,12 +489,12 @@ Application::Application(
// terminates.
settings_.leak_vm = false;

settings_.task_observer_add =
std::bind(&CurrentMessageLoopAddAfterTaskObserver, std::placeholders::_1,
std::placeholders::_2);

settings_.task_observer_remove = std::bind(
&CurrentMessageLoopRemoveAfterTaskObserver, std::placeholders::_1);
settings_.task_observer_add = [](intptr_t key, fml::closure callback) {
fml::MessageLoop::GetCurrent().AddTaskObserver(key, std::move(callback));
};
settings_.task_observer_remove = [](intptr_t key) {
fml::MessageLoop::GetCurrent().RemoveTaskObserver(key);
};

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

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

auto weak_application = weak_factory_.GetWeakPtr();
auto platform_task_runner =
CreateFMLTaskRunner(async_get_default_dispatcher());
auto platform_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
const std::string component_url = package.resolved_url;
settings_.unhandled_exception_callback = [weak_application,
platform_task_runner,
Expand Down
4 changes: 2 additions & 2 deletions shell/platform/fuchsia/flutter/component.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@

#include "flutter/common/settings.h"
#include "flutter/fml/macros.h"
#include "flutter/fml/thread.h"

#include "engine.h"
#include "flutter_runner_product_configuration.h"
#include "thread.h"
#include "unique_fdio_ns.h"

namespace flutter_runner {

class Application;

struct ActiveApplication {
std::unique_ptr<Thread> thread;
std::unique_ptr<fml::Thread> thread;
std::unique_ptr<Application> application;

ActiveApplication& operator=(ActiveApplication&& other) noexcept {
Expand Down
58 changes: 35 additions & 23 deletions shell/platform/fuchsia/flutter/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#define FML_USED_ON_EMBEDDER

#include "engine.h"

#include <lib/async/cpp/task.h>
Expand All @@ -24,7 +26,6 @@
#include "fuchsia_intl.h"
#include "platform_view.h"
#include "surface.h"
#include "task_runner_adapter.h"

#if defined(LEGACY_FUCHSIA_EMBEDDER)
#include "compositor_context.h" // nogncheck
Expand Down Expand Up @@ -71,28 +72,17 @@ Engine::Engine(Delegate& delegate,
FlutterRunnerProductConfiguration product_config)
: delegate_(delegate),
thread_label_(std::move(thread_label)),
#if defined(LEGACY_FUCHSIA_EMBEDDER)
use_legacy_renderer_(product_config.use_legacy_renderer()),
#endif
intercept_all_input_(product_config.get_intercept_all_input()),
thread_host_(thread_label_ + ".",
flutter::ThreadHost::Type::IO |
flutter::ThreadHost::Type::UI |
flutter::ThreadHost::Type::GPU),
weak_factory_(this) {
if (zx::event::create(0, &vsync_event_) != ZX_OK) {
FML_DLOG(ERROR) << "Could not create the vsync event.";
return;
}

// Get the task runners from the managed threads. The current thread will be
// used as the "platform" thread.
const flutter::TaskRunners task_runners(
thread_label_, // Dart thread labels
CreateFMLTaskRunner(async_get_default_dispatcher()), // platform
CreateFMLTaskRunner(threads_[0].dispatcher()), // raster
CreateFMLTaskRunner(threads_[1].dispatcher()), // ui
CreateFMLTaskRunner(threads_[2].dispatcher()) // io
);
UpdateNativeThreadLabelNames(thread_label_, task_runners);

// Connect to Scenic.
// Set up the session connection.
auto scenic = svc->Connect<fuchsia::ui::scenic::Scenic>();
fidl::InterfaceHandle<fuchsia::ui::scenic::Session> session;
fidl::InterfaceHandle<fuchsia::ui::scenic::SessionListener> session_listener;
Expand Down Expand Up @@ -274,6 +264,34 @@ Engine::Engine(Delegate& delegate,
vsync_handle);
});

// Session can be terminated on the GPU thread, but we must terminate
// ourselves on the platform thread.
//
// This handles the fidl error callback when the Session connection is
// broken. The SessionListener interface also has an OnError method, which is
// invoked on the platform thread (in PlatformView).
fml::closure on_session_error_callback =
[dispatcher = async_get_default_dispatcher(),
weak = weak_factory_.GetWeakPtr()]() {
async::PostTask(dispatcher, [weak]() {
if (weak) {
weak->Terminate();
}
});
};

// Get the task runners from the managed threads. The current thread will be
// used as the "platform" thread.
fml::MessageLoop::EnsureInitializedForCurrentThread();

const flutter::TaskRunners task_runners(
thread_label_, // Dart thread labels
fml::MessageLoop::GetCurrent().GetTaskRunner(), // platform
thread_host_.gpu_thread->GetTaskRunner(), // gpu
thread_host_.ui_thread->GetTaskRunner(), // ui
thread_host_.io_thread->GetTaskRunner() // io
);

// Setup the callback that will instantiate the rasterizer.
flutter::Shell::CreateCallback<flutter::Rasterizer> on_create_rasterizer;
#if defined(LEGACY_FUCHSIA_EMBEDDER)
Expand Down Expand Up @@ -454,12 +472,6 @@ Engine::Engine(Delegate& delegate,

Engine::~Engine() {
shell_.reset();
for (auto& thread : threads_) {
thread.Quit();
}
for (auto& thread : threads_) {
thread.Join();
}
}

std::optional<uint32_t> Engine::GetEngineReturnCode() const {
Expand Down
6 changes: 3 additions & 3 deletions shell/platform/fuchsia/flutter/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

#include "flutter_runner_product_configuration.h"
#include "fuchsia_external_view_embedder.h"
#include "flutter/shell/common/thread_host.h"
#include "isolate_configurator.h"
#include "session_connection.h"
#include "thread.h"
#include "vulkan_surface_producer.h"

#if defined(LEGACY_FUCHSIA_EMBEDDER)
Expand Down Expand Up @@ -71,8 +71,8 @@ class Engine final {
Delegate& delegate_;

const std::string thread_label_;
std::array<Thread, 3> threads_;

flutter::ThreadHost thread_host_;
std::optional<SessionConnection> session_connection_;
std::optional<VulkanSurfaceProducer> surface_producer_;
std::shared_ptr<FuchsiaExternalViewEmbedder> external_view_embedder_;
Expand Down
1 change: 0 additions & 1 deletion shell/platform/fuchsia/flutter/fuchsia_intl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <string>
#include <vector>

#include "loop.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
Expand Down
47 changes: 0 additions & 47 deletions shell/platform/fuchsia/flutter/loop.cc

This file was deleted.

17 changes: 0 additions & 17 deletions shell/platform/fuchsia/flutter/loop.h

This file was deleted.

15 changes: 10 additions & 5 deletions shell/platform/fuchsia/flutter/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,41 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#define FML_USED_ON_EMBEDDER

#include <lib/async-loop/cpp/loop.h>
#include <lib/async-loop/default.h>
#include <lib/trace-provider/provider.h>
#include <lib/trace/event.h>

#include <cstdlib>

#include "loop.h"
#include "platform/utils.h"
#include "runner.h"
#include "runtime/dart/utils/tempfs.h"

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

std::unique_ptr<trace::TraceProviderWithFdio> provider;
{
bool already_started;
// Use CreateSynchronously to prevent loss of early events.
trace::TraceProviderWithFdio::CreateSynchronously(
loop->dispatcher(), "flutter_runner", &provider, &already_started);
async_get_default_dispatcher(), "flutter_runner", &provider,
&already_started);
}

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

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

flutter_runner::Runner runner(loop.get(), dart::ComponentContext());
flutter_runner::Runner runner(message_loop);

message_loop.Run();

loop->Run();
FML_DLOG(INFO) << "Flutter application services terminated.";

return EXIT_SUCCESS;
Expand Down
Loading

0 comments on commit a21820f

Please sign in to comment.