From 54e979d903a4dce31916c41a8564c3672633760e Mon Sep 17 00:00:00 2001 From: Rob Bogie Date: Mon, 8 Apr 2019 17:55:04 +0200 Subject: [PATCH] Revert "Allow embedders to specify their own task runner interfaces. (#8273)" This reverts commit 951815161f2199c83b34218c090ce3b8536158f9. --- ci/licenses_golden/licenses_flutter | 5 - fml/task_runner.h | 4 +- shell/platform/embedder/BUILD.gn | 5 - shell/platform/embedder/embedder.cc | 63 +++--- shell/platform/embedder/embedder.h | 64 ------ shell/platform/embedder/embedder_engine.cc | 16 +- shell/platform/embedder/embedder_engine.h | 8 +- .../platform/embedder/embedder_safe_access.h | 20 -- .../platform/embedder/embedder_task_runner.cc | 72 ------- .../platform/embedder/embedder_task_runner.h | 57 ----- .../platform/embedder/embedder_thread_host.cc | 204 ------------------ .../platform/embedder/embedder_thread_host.h | 54 ----- .../embedder/fixtures/simple_main.dart | 8 - .../embedder/tests/embedder_config_builder.cc | 38 +--- .../embedder/tests/embedder_config_builder.h | 10 +- .../embedder/tests/embedder_unittests.cc | 117 +--------- 16 files changed, 40 insertions(+), 705 deletions(-) delete mode 100644 shell/platform/embedder/embedder_safe_access.h delete mode 100644 shell/platform/embedder/embedder_task_runner.cc delete mode 100644 shell/platform/embedder/embedder_task_runner.h delete mode 100644 shell/platform/embedder/embedder_thread_host.cc delete mode 100644 shell/platform/embedder/embedder_thread_host.h diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index f5259b505f734..0a30b3ca65123 100644 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -675,17 +675,12 @@ FILE: ../../../flutter/shell/platform/embedder/embedder_engine.h FILE: ../../../flutter/shell/platform/embedder/embedder_external_texture_gl.cc FILE: ../../../flutter/shell/platform/embedder/embedder_external_texture_gl.h FILE: ../../../flutter/shell/platform/embedder/embedder_include.c -FILE: ../../../flutter/shell/platform/embedder/embedder_safe_access.h FILE: ../../../flutter/shell/platform/embedder/embedder_surface.cc FILE: ../../../flutter/shell/platform/embedder/embedder_surface.h FILE: ../../../flutter/shell/platform/embedder/embedder_surface_gl.cc FILE: ../../../flutter/shell/platform/embedder/embedder_surface_gl.h FILE: ../../../flutter/shell/platform/embedder/embedder_surface_software.cc FILE: ../../../flutter/shell/platform/embedder/embedder_surface_software.h -FILE: ../../../flutter/shell/platform/embedder/embedder_task_runner.cc -FILE: ../../../flutter/shell/platform/embedder/embedder_task_runner.h -FILE: ../../../flutter/shell/platform/embedder/embedder_thread_host.cc -FILE: ../../../flutter/shell/platform/embedder/embedder_thread_host.h FILE: ../../../flutter/shell/platform/embedder/fixtures/a11y_main.dart FILE: ../../../flutter/shell/platform/embedder/fixtures/simple_main.dart FILE: ../../../flutter/shell/platform/embedder/platform_view_embedder.cc diff --git a/fml/task_runner.h b/fml/task_runner.h index 72c4219029f34..04696752c95e3 100644 --- a/fml/task_runner.h +++ b/fml/task_runner.h @@ -17,8 +17,6 @@ class MessageLoopImpl; class TaskRunner : public fml::RefCountedThreadSafe { public: - virtual ~TaskRunner(); - virtual void PostTask(fml::closure task); virtual void PostTaskForTime(fml::closure task, fml::TimePoint target_time); @@ -27,6 +25,8 @@ class TaskRunner : public fml::RefCountedThreadSafe { virtual bool RunsTasksOnCurrentThread(); + virtual ~TaskRunner(); + static void RunNowOrPostTask(fml::RefPtr runner, fml::closure task); diff --git a/shell/platform/embedder/BUILD.gn b/shell/platform/embedder/BUILD.gn index 5632f5f0fd5dd..f9659a3b5c5fb 100644 --- a/shell/platform/embedder/BUILD.gn +++ b/shell/platform/embedder/BUILD.gn @@ -22,17 +22,12 @@ source_set("embedder") { "embedder_external_texture_gl.cc", "embedder_external_texture_gl.h", "embedder_include.c", - "embedder_safe_access.h", "embedder_surface.cc", "embedder_surface.h", "embedder_surface_gl.cc", "embedder_surface_gl.h", "embedder_surface_software.cc", "embedder_surface_software.h", - "embedder_task_runner.cc", - "embedder_task_runner.h", - "embedder_thread_host.cc", - "embedder_thread_host.h", "platform_view_embedder.cc", "platform_view_embedder.h", "vsync_waiter_embedder.cc", diff --git a/shell/platform/embedder/embedder.cc b/shell/platform/embedder/embedder.cc index 93c23b12207e1..c15970de70775 100644 --- a/shell/platform/embedder/embedder.cc +++ b/shell/platform/embedder/embedder.cc @@ -21,6 +21,10 @@ extern const intptr_t kPlatformStrongDillSize; #endif // FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG } +#include "flutter/shell/platform/embedder/embedder.h" + +#include + #include "flutter/assets/directory_asset_bundle.h" #include "flutter/common/task_runners.h" #include "flutter/fml/command_line.h" @@ -34,11 +38,18 @@ extern const intptr_t kPlatformStrongDillSize; #include "flutter/shell/common/switches.h" #include "flutter/shell/platform/embedder/embedder.h" #include "flutter/shell/platform/embedder/embedder_engine.h" -#include "flutter/shell/platform/embedder/embedder_safe_access.h" -#include "flutter/shell/platform/embedder/embedder_task_runner.h" -#include "flutter/shell/platform/embedder/embedder_thread_host.h" #include "flutter/shell/platform/embedder/platform_view_embedder.h" +#define SAFE_ACCESS(pointer, member, default_value) \ + ([=]() { \ + if (offsetof(std::remove_pointer::type, member) + \ + sizeof(pointer->member) <= \ + pointer->struct_size) { \ + return pointer->member; \ + } \ + return static_castmember)>((default_value)); \ + })() + static FlutterEngineResult LogEmbedderError(FlutterEngineResult code, const char* name, const char* function, @@ -397,6 +408,20 @@ FlutterEngineResult FlutterEngineRun(size_t version, }; } + // Create a thread host with the current thread as the platform thread and all + // other threads managed. + shell::ThreadHost thread_host("io.flutter", shell::ThreadHost::Type::GPU | + shell::ThreadHost::Type::IO | + shell::ThreadHost::Type::UI); + fml::MessageLoop::EnsureInitializedForCurrentThread(); + blink::TaskRunners task_runners( + "io.flutter", + fml::MessageLoop::GetCurrent().GetTaskRunner(), // platform + thread_host.gpu_thread->GetTaskRunner(), // gpu + thread_host.ui_thread->GetTaskRunner(), // ui + thread_host.io_thread->GetTaskRunner() // io + ); + shell::PlatformViewEmbedder::UpdateSemanticsNodesCallback update_semantics_nodes_callback = nullptr; if (SAFE_ACCESS(args, update_semantics_node_callback, nullptr) != nullptr) { @@ -577,23 +602,6 @@ FlutterEngineResult FlutterEngineRun(size_t version, } } - auto thread_host = - shell::EmbedderThreadHost::CreateEmbedderOrEngineManagedThreadHost( - SAFE_ACCESS(args, custom_task_runners, nullptr)); - - if (!thread_host || !thread_host->IsValid()) { - FML_LOG(ERROR) << "Could not setup or infer thread configuration to run " - "the Flutter engine on."; - return LOG_EMBEDDER_ERROR(kInvalidArguments); - } - - auto task_runners = thread_host->GetTaskRunners(); - - if (!task_runners.IsValid()) { - FML_LOG(ERROR) << "Task runner configuration specified is invalid."; - return LOG_EMBEDDER_ERROR(kInvalidArguments); - } - // Step 1: Create the engine. auto embedder_engine = std::make_unique(std::move(thread_host), // @@ -932,18 +940,3 @@ FlutterEngineResult FlutterEnginePostRenderThreadTask(FlutterEngine engine, ? kSuccess : LOG_EMBEDDER_ERROR(kInternalInconsistency); } - -uint64_t FlutterEngineGetCurrentTime() { - return fml::TimePoint::Now().ToEpochDelta().ToNanoseconds(); -} - -FlutterEngineResult FlutterEngineRunTask(FlutterEngine engine, - const FlutterTask* task) { - if (engine == nullptr) { - return LOG_EMBEDDER_ERROR(kInvalidArguments); - } - - return reinterpret_cast(engine)->RunTask(task) - ? kSuccess - : LOG_EMBEDDER_ERROR(kInvalidArguments); -} diff --git a/shell/platform/embedder/embedder.h b/shell/platform/embedder/embedder.h index ae0348e7e0e19..442716a50114b 100644 --- a/shell/platform/embedder/embedder.h +++ b/shell/platform/embedder/embedder.h @@ -447,52 +447,6 @@ typedef void (*FlutterUpdateSemanticsCustomActionCallback)( const FlutterSemanticsCustomAction* /* semantics custom action */, void* /* user data */); -typedef struct _FlutterTaskRunner* FlutterTaskRunner; - -typedef struct { - FlutterTaskRunner runner; - uint64_t task; -} FlutterTask; - -typedef void (*FlutterTaskRunnerPostTaskCallback)( - FlutterTask /* task */, - uint64_t /* target time nanos */, - void* /* user data */); - -// An interface used by the Flutter engine to execute tasks at the target time -// on a specified thread. There should be a 1-1 relationship between a thread -// and a task runner. It is undefined behavior to run a task on a thread that is -// not associated with its task runner. -typedef struct { - // The size of this struct. Must be sizeof(FlutterTaskRunnerDescription). - size_t struct_size; - void* user_data; - // May be called from any thread. Should return true if tasks posted on the - // calling thread will be run on that same thread. - // - // This field is required. - BoolCallback runs_task_on_current_thread_callback; - // May be called from any thread. The given task should be executed by the - // embedder on the thread associated with that task runner by calling - // |FlutterEngineRunTask| at the given target time. The system monotonic clock - // should be used for the target time. The target time is the absolute time - // from epoch (NOT a delta) at which the task must be returned back to the - // engine on the correct thread. If the embedder needs to calculate a delta, - // |FlutterEngineGetCurrentTime| may be called and the difference used as the - // delta. - // - // This field is required. - FlutterTaskRunnerPostTaskCallback post_task_callback; -} FlutterTaskRunnerDescription; - -typedef struct { - // The size of this struct. Must be sizeof(FlutterCustomTaskRunners). - size_t struct_size; - // Specify the task runner for the thread on which the |FlutterEngineRun| call - // is made. - const FlutterTaskRunnerDescription* platform_task_runner; -} FlutterCustomTaskRunners; - typedef struct { // The size of this struct. Must be sizeof(FlutterProjectArgs). size_t struct_size; @@ -618,11 +572,6 @@ typedef struct { // away. Usually, this is done using the `@pragma('vm:entry-point')` // decoration. const char* custom_dart_entrypoint; - - // Typically the Flutter engine create and manages its internal threads. This - // optional argument allows for the specification of task runner interfaces to - // event loops managed by the embedder on threads it creates. - const FlutterCustomTaskRunners* custom_task_runners; } FlutterProjectArgs; FLUTTER_EXPORT @@ -766,19 +715,6 @@ FlutterEngineResult FlutterEnginePostRenderThreadTask(FlutterEngine engine, VoidCallback callback, void* callback_data); -// Get the current time in nanoseconds from the clock used by the flutter -// engine. This is the system monotonic clock. -FLUTTER_EXPORT -uint64_t FlutterEngineGetCurrentTime(); - -// Inform the engine to run the specified task. This task has been given to -// the engine via the |FlutterTaskRunnerDescription.post_task_callback|. This -// call must only be made at the target time specified in that callback. Running -// the task before that time is undefined behavior. -FLUTTER_EXPORT -FlutterEngineResult FlutterEngineRunTask(FlutterEngine engine, - const FlutterTask* task); - #if defined(__cplusplus) } // extern "C" #endif diff --git a/shell/platform/embedder/embedder_engine.cc b/shell/platform/embedder/embedder_engine.cc index 836dfc645c1a7..63083bcef1b4d 100644 --- a/shell/platform/embedder/embedder_engine.cc +++ b/shell/platform/embedder/embedder_engine.cc @@ -10,7 +10,7 @@ namespace shell { EmbedderEngine::EmbedderEngine( - std::unique_ptr thread_host, + ThreadHost thread_host, blink::TaskRunners task_runners, blink::Settings settings, Shell::CreateCallback on_create_platform_view, @@ -23,11 +23,7 @@ EmbedderEngine::EmbedderEngine( on_create_platform_view, on_create_rasterizer)), external_texture_callback_(external_texture_callback) { - if (!shell_) { - return; - } - - is_valid_ = true; + is_valid_ = shell_ != nullptr; } EmbedderEngine::~EmbedderEngine() = default; @@ -216,12 +212,4 @@ bool EmbedderEngine::PostRenderThreadTask(fml::closure task) { return true; } -bool EmbedderEngine::RunTask(const FlutterTask* task) { - if (!IsValid() || task == nullptr) { - return false; - } - return thread_host_->PostTask(reinterpret_cast(task->runner), - task->task); -} - } // namespace shell diff --git a/shell/platform/embedder/embedder_engine.h b/shell/platform/embedder/embedder_engine.h index bd2ff92d16d8b..52b528e3a2ec3 100644 --- a/shell/platform/embedder/embedder_engine.h +++ b/shell/platform/embedder/embedder_engine.h @@ -6,7 +6,6 @@ #define FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_ENGINE_H_ #include -#include #include "flutter/fml/macros.h" #include "flutter/shell/common/shell.h" @@ -14,7 +13,6 @@ #include "flutter/shell/platform/embedder/embedder.h" #include "flutter/shell/platform/embedder/embedder_engine.h" #include "flutter/shell/platform/embedder/embedder_external_texture_gl.h" -#include "flutter/shell/platform/embedder/embedder_thread_host.h" namespace shell { @@ -22,7 +20,7 @@ namespace shell { // instance of the Flutter engine. class EmbedderEngine { public: - EmbedderEngine(std::unique_ptr thread_host, + EmbedderEngine(ThreadHost thread_host, blink::TaskRunners task_runners, blink::Settings settings, Shell::CreateCallback on_create_platform_view, @@ -67,10 +65,8 @@ class EmbedderEngine { bool PostRenderThreadTask(fml::closure task); - bool RunTask(const FlutterTask* task); - private: - const std::unique_ptr thread_host_; + const ThreadHost thread_host_; std::unique_ptr shell_; const EmbedderExternalTextureGL::ExternalTextureCallback external_texture_callback_; diff --git a/shell/platform/embedder/embedder_safe_access.h b/shell/platform/embedder/embedder_safe_access.h deleted file mode 100644 index 783eb44a88bd2..0000000000000 --- a/shell/platform/embedder/embedder_safe_access.h +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_SAFE_ACCESS_H_ -#define FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_SAFE_ACCESS_H_ - -#include - -#define SAFE_ACCESS(pointer, member, default_value) \ - ([=]() { \ - if (offsetof(std::remove_pointer::type, member) + \ - sizeof(pointer->member) <= \ - pointer->struct_size) { \ - return pointer->member; \ - } \ - return static_castmember)>((default_value)); \ - })() - -#endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_SAFE_ACCESS_H_ diff --git a/shell/platform/embedder/embedder_task_runner.cc b/shell/platform/embedder/embedder_task_runner.cc deleted file mode 100644 index c0b5ab862bdf4..0000000000000 --- a/shell/platform/embedder/embedder_task_runner.cc +++ /dev/null @@ -1,72 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "flutter/shell/platform/embedder/embedder_task_runner.h" - -#include "flutter/fml/message_loop_impl.h" - -namespace shell { - -EmbedderTaskRunner::EmbedderTaskRunner(DispatchTable table) - : TaskRunner(nullptr /* loop implemenation*/), - dispatch_table_(std::move(table)) { - FML_DCHECK(dispatch_table_.post_task_callback); - FML_DCHECK(dispatch_table_.runs_task_on_current_thread_callback); -} - -EmbedderTaskRunner::~EmbedderTaskRunner() = default; - -void EmbedderTaskRunner::PostTask(fml::closure task) { - PostTaskForTime(task, fml::TimePoint::Now()); -} - -void EmbedderTaskRunner::PostTaskForTime(fml::closure task, - fml::TimePoint target_time) { - if (!task) { - return; - } - - uint64_t baton = 0; - - { - // Release the lock before the jump via the dispatch table. - std::lock_guard lock(tasks_mutex_); - baton = ++last_baton_; - pending_tasks_[baton] = task; - } - - dispatch_table_.post_task_callback(this, baton, target_time); -} - -void EmbedderTaskRunner::PostDelayedTask(fml::closure task, - fml::TimeDelta delay) { - PostTaskForTime(task, fml::TimePoint::Now() + delay); -} - -bool EmbedderTaskRunner::RunsTasksOnCurrentThread() { - return dispatch_table_.runs_task_on_current_thread_callback(); -} - -bool EmbedderTaskRunner::PostTask(uint64_t baton) { - fml::closure task; - - { - std::lock_guard lock(tasks_mutex_); - auto found = pending_tasks_.find(baton); - if (found == pending_tasks_.end()) { - FML_LOG(ERROR) << "Embedder attempted to post an unknown task."; - return false; - } - task = found->second; - pending_tasks_.erase(found); - - // Let go of the tasks mutex befor executing the task. - } - - FML_DCHECK(task); - task(); - return true; -} - -} // namespace shell diff --git a/shell/platform/embedder/embedder_task_runner.h b/shell/platform/embedder/embedder_task_runner.h deleted file mode 100644 index 084efbf148a45..0000000000000 --- a/shell/platform/embedder/embedder_task_runner.h +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_TASK_RUNNER_H_ -#define FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_TASK_RUNNER_H_ - -#include -#include - -#include "flutter/fml/macros.h" -#include "flutter/fml/synchronization/thread_annotations.h" -#include "flutter/fml/task_runner.h" - -namespace shell { - -class EmbedderTaskRunner final : public fml::TaskRunner { - public: - struct DispatchTable { - std::function - post_task_callback; - std::function runs_task_on_current_thread_callback; - }; - - EmbedderTaskRunner(DispatchTable table); - - ~EmbedderTaskRunner() override; - - bool PostTask(uint64_t baton); - - // |fml::TaskRunner| - void PostTask(fml::closure task) override; - - // |fml::TaskRunner| - void PostTaskForTime(fml::closure task, fml::TimePoint target_time) override; - - // |fml::TaskRunner| - void PostDelayedTask(fml::closure task, fml::TimeDelta delay) override; - - // |fml::TaskRunner| - bool RunsTasksOnCurrentThread() override; - - private: - DispatchTable dispatch_table_; - std::mutex tasks_mutex_; - uint64_t last_baton_ FML_GUARDED_BY(tasks_mutex_); - std::unordered_map pending_tasks_ - FML_GUARDED_BY(tasks_mutex_); - - FML_DISALLOW_COPY_AND_ASSIGN(EmbedderTaskRunner); -}; - -} // namespace shell - -#endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_TASK_RUNNER_H_ diff --git a/shell/platform/embedder/embedder_thread_host.cc b/shell/platform/embedder/embedder_thread_host.cc deleted file mode 100644 index 648025ea5a317..0000000000000 --- a/shell/platform/embedder/embedder_thread_host.cc +++ /dev/null @@ -1,204 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// This is why we can't yet export the UI thread to embedders. -#define FML_USED_ON_EMBEDDER - -#include "flutter/shell/platform/embedder/embedder_thread_host.h" - -#include "flutter/fml/message_loop.h" -#include "flutter/shell/platform/embedder/embedder_safe_access.h" - -namespace shell { - -static fml::RefPtr CreateEmbedderTaskRunner( - const FlutterTaskRunnerDescription* description) { - if (description == nullptr) { - return {}; - } - - if (SAFE_ACCESS(description, runs_task_on_current_thread_callback, nullptr) == - nullptr) { - FML_LOG(ERROR) << "FlutterTaskRunnerDescription.runs_task_on_current_" - "thread_callback was nullptr."; - return {}; - } - - if (SAFE_ACCESS(description, post_task_callback, nullptr) == nullptr) { - FML_LOG(ERROR) - << "FlutterTaskRunnerDescription.post_task_callback was nullptr."; - return {}; - } - - auto user_data = SAFE_ACCESS(description, user_data, nullptr); - - // ABI safety checks have been completed. - auto post_task_callback_c = description->post_task_callback; - auto runs_task_on_current_thread_callback_c = - description->runs_task_on_current_thread_callback; - - EmbedderTaskRunner::DispatchTable task_runner_dispatch_table = { - .post_task_callback = [post_task_callback_c, user_data]( - EmbedderTaskRunner* task_runner, - uint64_t task_baton, - fml::TimePoint target_time) -> void { - FlutterTask task = { - .runner = reinterpret_cast(task_runner), - .task = task_baton, - }; - post_task_callback_c(task, target_time.ToEpochDelta().ToNanoseconds(), - user_data); - }, - .runs_task_on_current_thread_callback = - [runs_task_on_current_thread_callback_c, user_data]() -> bool { - return runs_task_on_current_thread_callback_c(user_data); - }}; - - return fml::MakeRefCounted(task_runner_dispatch_table); -} - -std::unique_ptr -EmbedderThreadHost::CreateEmbedderOrEngineManagedThreadHost( - const FlutterCustomTaskRunners* custom_task_runners) { - { - auto host = CreateEmbedderManagedThreadHost(custom_task_runners); - if (host && host->IsValid()) { - return host; - } - } - - // Only attempt to create the engine managed host if the embedder did not - // specify a custom configuration. We don't want to fallback to the engine - // managed configuration if the embedder attempted to specify a configuration - // but messed up with an incorrect configuration. - if (custom_task_runners == nullptr) { - auto host = CreateEngineManagedThreadHost(); - if (host && host->IsValid()) { - return host; - } - } - - return nullptr; -} - -constexpr const char* kFlutterThreadName = "io.flutter"; - -// static -std::unique_ptr -EmbedderThreadHost::CreateEmbedderManagedThreadHost( - const FlutterCustomTaskRunners* custom_task_runners) { - if (custom_task_runners == nullptr) { - return nullptr; - } - - const auto platform_task_runner = CreateEmbedderTaskRunner( - SAFE_ACCESS(custom_task_runners, platform_task_runner, nullptr)); - - // TODO(chinmaygarde): Add more here as we allow more threads to be controlled - // by the embedder. Create fallbacks as necessary. - - if (!platform_task_runner) { - return nullptr; - } - - ThreadHost thread_host(kFlutterThreadName, ThreadHost::Type::GPU | - ThreadHost::Type::IO | - ThreadHost::Type::UI); - - blink::TaskRunners task_runners( - kFlutterThreadName, - platform_task_runner, // platform - thread_host.gpu_thread->GetTaskRunner(), // gpu - thread_host.ui_thread->GetTaskRunner(), // ui - thread_host.io_thread->GetTaskRunner() // io - ); - - if (!task_runners.IsValid()) { - return nullptr; - } - - std::set> embedder_task_runners; - embedder_task_runners.insert(platform_task_runner); - - auto embedder_host = std::make_unique( - std::move(thread_host), std::move(task_runners), - std::move(embedder_task_runners)); - - if (embedder_host->IsValid()) { - return embedder_host; - } - - return nullptr; -} - -// static -std::unique_ptr -EmbedderThreadHost::CreateEngineManagedThreadHost() { - // Create a thread host with the current thread as the platform thread and all - // other threads managed. - ThreadHost thread_host(kFlutterThreadName, ThreadHost::Type::GPU | - ThreadHost::Type::IO | - ThreadHost::Type::UI); - - fml::MessageLoop::EnsureInitializedForCurrentThread(); - - // For embedder platforms that don't have native message loop interop, this - // will reference a task runner that points to a null message loop - // implementation. - auto platform_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner(); - - blink::TaskRunners task_runners( - kFlutterThreadName, - platform_task_runner, // platform - thread_host.gpu_thread->GetTaskRunner(), // gpu - thread_host.ui_thread->GetTaskRunner(), // ui - thread_host.io_thread->GetTaskRunner() // io - ); - - if (!task_runners.IsValid()) { - return nullptr; - } - - std::set> empty_embedder_task_runners; - - auto embedder_host = std::make_unique( - std::move(thread_host), std::move(task_runners), - empty_embedder_task_runners); - - if (embedder_host->IsValid()) { - return embedder_host; - } - - return nullptr; -} - -EmbedderThreadHost::EmbedderThreadHost( - ThreadHost host, - blink::TaskRunners runners, - std::set> embedder_task_runners) - : host_(std::move(host)), runners_(std::move(runners)) { - for (const auto& runner : embedder_task_runners) { - runners_map_[reinterpret_cast(runner.get())] = runner; - } -} - -EmbedderThreadHost::~EmbedderThreadHost() = default; - -bool EmbedderThreadHost::IsValid() const { - return runners_.IsValid(); -} - -const blink::TaskRunners& EmbedderThreadHost::GetTaskRunners() const { - return runners_; -} - -bool EmbedderThreadHost::PostTask(int64_t runner, uint64_t task) const { - auto found = runners_map_.find(runner); - if (found == runners_map_.end()) { - return false; - } - return found->second->PostTask(task); -} - -} // namespace shell diff --git a/shell/platform/embedder/embedder_thread_host.h b/shell/platform/embedder/embedder_thread_host.h deleted file mode 100644 index 1ae9c309ed72f..0000000000000 --- a/shell/platform/embedder/embedder_thread_host.h +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_THREAD_HOST_H_ -#define FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_THREAD_HOST_H_ - -#include -#include -#include - -#include "flutter/common/task_runners.h" -#include "flutter/fml/macros.h" -#include "flutter/shell/common/thread_host.h" -#include "flutter/shell/platform/embedder/embedder.h" -#include "flutter/shell/platform/embedder/embedder_task_runner.h" - -namespace shell { - -class EmbedderThreadHost { - public: - static std::unique_ptr - CreateEmbedderOrEngineManagedThreadHost( - const FlutterCustomTaskRunners* custom_task_runners); - - EmbedderThreadHost( - ThreadHost host, - blink::TaskRunners runners, - std::set> embedder_task_runners); - - ~EmbedderThreadHost(); - - bool IsValid() const; - - const blink::TaskRunners& GetTaskRunners() const; - - bool PostTask(int64_t runner, uint64_t task) const; - - private: - ThreadHost host_; - blink::TaskRunners runners_; - std::map> runners_map_; - - static std::unique_ptr CreateEmbedderManagedThreadHost( - const FlutterCustomTaskRunners* custom_task_runners); - - static std::unique_ptr CreateEngineManagedThreadHost(); - - FML_DISALLOW_COPY_AND_ASSIGN(EmbedderThreadHost); -}; - -} // namespace shell - -#endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_THREAD_HOST_H_ diff --git a/shell/platform/embedder/fixtures/simple_main.dart b/shell/platform/embedder/fixtures/simple_main.dart index 853e136f96ed8..bd3999563e031 100644 --- a/shell/platform/embedder/fixtures/simple_main.dart +++ b/shell/platform/embedder/fixtures/simple_main.dart @@ -1,5 +1,3 @@ -import 'dart:ui'; - void main() {} @pragma('vm:entry-point') @@ -20,9 +18,3 @@ void customEntrypoint1() { void sayHiFromCustomEntrypoint1() native "SayHiFromCustomEntrypoint1"; void sayHiFromCustomEntrypoint2() native "SayHiFromCustomEntrypoint2"; void sayHiFromCustomEntrypoint3() native "SayHiFromCustomEntrypoint3"; - - -@pragma('vm:entry-point') -void invokePlatformTaskRunner() { - window.sendPlatformMessage('OhHi', null, null); -} diff --git a/shell/platform/embedder/tests/embedder_config_builder.cc b/shell/platform/embedder/tests/embedder_config_builder.cc index 546f95a78ea1a..23ef27cd3c954 100644 --- a/shell/platform/embedder/tests/embedder_config_builder.cc +++ b/shell/platform/embedder/tests/embedder_config_builder.cc @@ -13,7 +13,6 @@ EmbedderConfigBuilder::EmbedderConfigBuilder( : context_(context) { project_args_.struct_size = sizeof(project_args_); software_renderer_config_.struct_size = sizeof(FlutterSoftwareRendererConfig); - custom_task_runners_.struct_size = sizeof(FlutterCustomTaskRunners); software_renderer_config_.surface_present_callback = [](void*, const void*, size_t, size_t) { return true; }; @@ -72,43 +71,8 @@ void EmbedderConfigBuilder::SetDartEntrypoint(std::string entrypoint) { project_args_.custom_dart_entrypoint = dart_entrypoint_.c_str(); } -void EmbedderConfigBuilder::AddCommandLineArgument(std::string arg) { - if (arg.size() == 0) { - return; - } - - command_line_arguments_.emplace_back(std::move(arg)); -} - -void EmbedderConfigBuilder::SetPlatformTaskRunner( - const FlutterTaskRunnerDescription* runner) { - if (runner == nullptr) { - return; - } - custom_task_runners_.platform_task_runner = runner; - project_args_.custom_task_runners = &custom_task_runners_; -} - -UniqueEngine EmbedderConfigBuilder::LaunchEngine() { +UniqueEngine EmbedderConfigBuilder::LaunchEngine() const { FlutterEngine engine = nullptr; - - std::vector args; - args.reserve(command_line_arguments_.size()); - - for (const auto& arg : command_line_arguments_) { - args.push_back(arg.c_str()); - } - - if (args.size() > 0) { - project_args_.command_line_argv = args.data(); - project_args_.command_line_argc = args.size(); - } else { - // Clear it out in case this is not the first engine launch from the - // embedder config builder. - project_args_.command_line_argv = nullptr; - project_args_.command_line_argc = 0; - } - auto result = FlutterEngineRun(FLUTTER_ENGINE_VERSION, &renderer_config_, &project_args_, &context_, &engine); diff --git a/shell/platform/embedder/tests/embedder_config_builder.h b/shell/platform/embedder/tests/embedder_config_builder.h index 28c057d11441b..b76edb98734b9 100644 --- a/shell/platform/embedder/tests/embedder_config_builder.h +++ b/shell/platform/embedder/tests/embedder_config_builder.h @@ -19,7 +19,7 @@ struct UniqueEngineTraits { static bool IsValid(const FlutterEngine& value) { return value != nullptr; } - static void Free(FlutterEngine& engine) { + static void Free(FlutterEngine engine) { auto result = FlutterEngineShutdown(engine); FML_CHECK(result == kSuccess); } @@ -50,11 +50,7 @@ class EmbedderConfigBuilder { void SetDartEntrypoint(std::string entrypoint); - void AddCommandLineArgument(std::string arg); - - void SetPlatformTaskRunner(const FlutterTaskRunnerDescription* runner); - - UniqueEngine LaunchEngine(); + UniqueEngine LaunchEngine() const; private: EmbedderContext& context_; @@ -62,8 +58,6 @@ class EmbedderConfigBuilder { FlutterRendererConfig renderer_config_ = {}; FlutterSoftwareRendererConfig software_renderer_config_ = {}; std::string dart_entrypoint_; - FlutterCustomTaskRunners custom_task_runners_ = {}; - std::vector command_line_arguments_; FML_DISALLOW_COPY_AND_ASSIGN(EmbedderConfigBuilder); }; diff --git a/shell/platform/embedder/tests/embedder_unittests.cc b/shell/platform/embedder/tests/embedder_unittests.cc index 357a4e71114ab..6bad99a39dd0c 100644 --- a/shell/platform/embedder/tests/embedder_unittests.cc +++ b/shell/platform/embedder/tests/embedder_unittests.cc @@ -2,17 +2,11 @@ // 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 - #include "embedder.h" #include "flutter/fml/file.h" -#include "flutter/fml/make_copyable.h" #include "flutter/fml/mapping.h" -#include "flutter/fml/message_loop.h" #include "flutter/fml/synchronization/waitable_event.h" -#include "flutter/fml/thread.h" #include "flutter/shell/platform/embedder/tests/embedder_config_builder.h" #include "flutter/shell/platform/embedder/tests/embedder_test.h" #include "flutter/testing/testing.h" @@ -74,7 +68,7 @@ TEST_F(EmbedderTest, CanInvokeCustomEntrypointMacro) { // Can be defined separately. auto entry1 = [&latch1](Dart_NativeArguments args) { - FML_LOG(INFO) << "In Callback 1"; + FML_LOG(ERROR) << "In Callback 1"; latch1.Signal(); }; auto native_entry1 = CREATE_NATIVE_ENTRY(entry1); @@ -82,7 +76,7 @@ TEST_F(EmbedderTest, CanInvokeCustomEntrypointMacro) { // Can be wrapped in in the args. auto entry2 = [&latch2](Dart_NativeArguments args) { - FML_LOG(INFO) << "In Callback 2"; + FML_LOG(ERROR) << "In Callback 2"; latch2.Signal(); }; context.AddNativeCallback("SayHiFromCustomEntrypoint2", @@ -92,7 +86,7 @@ TEST_F(EmbedderTest, CanInvokeCustomEntrypointMacro) { context.AddNativeCallback( "SayHiFromCustomEntrypoint3", CREATE_NATIVE_ENTRY([&latch3](Dart_NativeArguments args) { - FML_LOG(INFO) << "In Callback 3"; + FML_LOG(ERROR) << "In Callback 3"; latch3.Signal(); })); @@ -105,110 +99,5 @@ TEST_F(EmbedderTest, CanInvokeCustomEntrypointMacro) { ASSERT_TRUE(engine.is_valid()); } -class EmbedderTestTaskRunner { - public: - EmbedderTestTaskRunner(std::function on_forward_task) - : on_forward_task_(on_forward_task) {} - - void SetForwardingTaskRunner(fml::RefPtr runner) { - forwarding_target_ = std::move(runner); - } - - FlutterTaskRunnerDescription GetEmbedderDescription() { - FlutterTaskRunnerDescription desc; - desc.struct_size = sizeof(desc); - desc.user_data = this; - desc.runs_task_on_current_thread_callback = [](void* user_data) -> bool { - return reinterpret_cast(user_data) - ->forwarding_target_->RunsTasksOnCurrentThread(); - }; - desc.post_task_callback = [](FlutterTask task, uint64_t target_time_nanos, - void* user_data) -> void { - auto runner = reinterpret_cast(user_data); - - auto target_time = fml::TimePoint::FromEpochDelta( - fml::TimeDelta::FromNanoseconds(target_time_nanos)); - - runner->forwarding_target_->PostTaskForTime( - [task, forwarder = runner->on_forward_task_]() { forwarder(task); }, - target_time); - }; - return desc; - } - - private: - fml::RefPtr forwarding_target_; - std::function on_forward_task_; - - FML_DISALLOW_COPY_AND_ASSIGN(EmbedderTestTaskRunner); -}; - -TEST_F(EmbedderTest, CanSpecifyCustomTaskRunner) { - auto& context = GetEmbedderContext(); - fml::AutoResetWaitableEvent latch; - - // Run the test on its own thread with a message loop so that it san safely - // pump its event loop while we wait for all the conditions to be checked. - fml::Thread thread; - UniqueEngine engine; - bool signalled = false; - - EmbedderTestTaskRunner runner([&](FlutterTask task) { - // There may be multiple tasks posted but we only need to check assertions - // once. - if (signalled) { - // Since we have the baton, return it back to the engine. We don't care - // about the return value because the engine could be shutting down an it - // may not actually be able to accept the same. - FlutterEngineRunTask(engine.get(), &task); - return; - } - - signalled = true; - FML_LOG(INFO) << "Checking assertions."; - ASSERT_TRUE(engine.is_valid()); - ASSERT_EQ(FlutterEngineRunTask(engine.get(), &task), kSuccess); - latch.Signal(); - }); - - thread.GetTaskRunner()->PostTask([&]() { - EmbedderConfigBuilder builder(context); - builder.AddCommandLineArgument("--verbose-logging"); - const auto task_runner_description = runner.GetEmbedderDescription(); - runner.SetForwardingTaskRunner( - fml::MessageLoop::GetCurrent().GetTaskRunner()); - builder.SetPlatformTaskRunner(&task_runner_description); - builder.SetDartEntrypoint("invokePlatformTaskRunner"); - engine = builder.LaunchEngine(); - ASSERT_TRUE(engine.is_valid()); - }); - - // Signalled when all the assertions are checked. - latch.Wait(); - FML_LOG(INFO) << "Assertions checked. Killing engine."; - ASSERT_TRUE(engine.is_valid()); - - // Since the engine was started on its own thread, it must be killed there as - // well. - fml::AutoResetWaitableEvent kill_latch; - thread.GetTaskRunner()->PostTask( - fml::MakeCopyable([&engine, &kill_latch]() mutable { - engine.reset(); - FML_LOG(INFO) << "Engine killed."; - kill_latch.Signal(); - })); - kill_latch.Wait(); - - ASSERT_TRUE(signalled); -} - -TEST(EmbedderTestNoFixture, CanGetCurrentTimeInNanoseconds) { - auto point1 = fml::TimePoint::FromEpochDelta( - fml::TimeDelta::FromNanoseconds(FlutterEngineGetCurrentTime())); - auto point2 = fml::TimePoint::Now(); - - ASSERT_LT((point2 - point1), fml::TimeDelta::FromMilliseconds(1)); -} - } // namespace testing } // namespace shell