5#define FML_USED_ON_EMBEDDER
7#include "flutter/shell/platform/embedder/embedder_thread_host.h"
11#include "flutter/fml/message_loop.h"
12#include "flutter/shell/platform/embedder/embedder_struct_macros.h"
29static std::pair<bool, fml::RefPtr<EmbedderTaskRunner>>
31 if (description ==
nullptr) {
38 if (
SAFE_ACCESS(description, runs_task_on_current_thread_callback,
nullptr) ==
40 FML_LOG(
ERROR) <<
"FlutterTaskRunnerDescription.runs_task_on_current_"
41 "thread_callback was nullptr.";
45 if (
SAFE_ACCESS(description, post_task_callback,
nullptr) ==
nullptr) {
47 <<
"FlutterTaskRunnerDescription.post_task_callback was nullptr.";
55 auto runs_task_on_current_thread_callback_c =
69 post_task_callback_c(task, target_time.ToEpochDelta().ToNanoseconds(),
73 [runs_task_on_current_thread_callback_c,
user_data]() ->
bool {
74 return runs_task_on_current_thread_callback_c(
user_data);
77 return {
true, fml::MakeRefCounted<EmbedderTaskRunner>(
78 task_runner_dispatch_table,
82std::unique_ptr<EmbedderThreadHost>
88 CreateEmbedderManagedThreadHost(custom_task_runners, config_setter);
98 if (custom_task_runners ==
nullptr) {
99 auto host = CreateEngineManagedThreadHost(config_setter);
125std::unique_ptr<EmbedderThreadHost>
126EmbedderThreadHost::CreateEmbedderManagedThreadHost(
129 if (custom_task_runners ==
nullptr) {
133 auto thread_host_config = ThreadHost::ThreadHostConfig(config_setter);
145 SAFE_ACCESS(custom_task_runners, platform_task_runner,
nullptr));
147 SAFE_ACCESS(custom_task_runners, render_task_runner,
nullptr));
149 if (!platform_task_runner_pair.first || !render_task_runner_pair.first) {
158 if (!render_task_runner_pair.second) {
165 if (platform_task_runner_pair.second && render_task_runner_pair.second) {
166 if (platform_task_runner_pair.second->GetEmbedderIdentifier() ==
167 render_task_runner_pair.second->GetEmbedderIdentifier()) {
168 render_task_runner_pair.second = platform_task_runner_pair.second;
174 ThreadHost thread_host(thread_host_config);
178 auto platform_task_runner = platform_task_runner_pair.second
180 platform_task_runner_pair.second)
185 auto render_task_runner = render_task_runner_pair.second
187 render_task_runner_pair.second)
188 : thread_host.raster_thread->GetTaskRunner();
192 platform_task_runner,
194 thread_host.ui_thread->GetTaskRunner(),
195 thread_host.io_thread->GetTaskRunner()
198 if (!task_runners.IsValid()) {
202 std::set<fml::RefPtr<EmbedderTaskRunner>> embedder_task_runners;
204 if (platform_task_runner_pair.second) {
205 embedder_task_runners.insert(platform_task_runner_pair.second);
208 if (render_task_runner_pair.second) {
209 embedder_task_runners.insert(render_task_runner_pair.second);
212 auto embedder_host = std::make_unique<EmbedderThreadHost>(
213 std::move(thread_host), std::move(task_runners),
214 std::move(embedder_task_runners));
216 if (embedder_host->IsValid()) {
217 return embedder_host;
224std::unique_ptr<EmbedderThreadHost>
225EmbedderThreadHost::CreateEngineManagedThreadHost(
228 auto thread_host_config = ThreadHost::ThreadHostConfig(config_setter);
238 ThreadHost thread_host(thread_host_config);
247 platform_task_runner,
248 thread_host.raster_thread->GetTaskRunner(),
249 thread_host.ui_thread->GetTaskRunner(),
250 thread_host.io_thread->GetTaskRunner()
253 if (!task_runners.IsValid()) {
257 std::set<fml::RefPtr<EmbedderTaskRunner>> empty_embedder_task_runners;
259 auto embedder_host = std::make_unique<EmbedderThreadHost>(
260 std::move(thread_host), std::move(task_runners),
261 empty_embedder_task_runners);
263 if (embedder_host->IsValid()) {
264 return embedder_host;
274 : host_(
std::move(
host)), runners_(runners) {
275 for (
const auto& runner : embedder_task_runners) {
276 runners_map_[
reinterpret_cast<int64_t
>(runner.get())] = runner;
291 auto found = runners_map_.find(runner);
292 if (found == runners_map_.end()) {
295 return found->second->PostTask(task);
bool PostTask(int64_t runner, uint64_t task) const
const flutter::TaskRunners & GetTaskRunners() const
static std::unique_ptr< EmbedderThreadHost > CreateEmbedderOrEngineManagedThreadHost(const FlutterCustomTaskRunners *custom_task_runners, const flutter::ThreadConfigSetter &config_setter=fml::Thread::SetCurrentThreadName)
EmbedderThreadHost(ThreadHost host, const flutter::TaskRunners &runners, const std::set< fml::RefPtr< EmbedderTaskRunner > > &embedder_task_runners)
static void EnsureInitializedForCurrentThread()
fml::RefPtr< fml::TaskRunner > GetTaskRunner() const
static FML_EMBEDDER_ONLY MessageLoop & GetCurrent()
ThreadPriority
Valid values for priority of Thread.
@ kRaster
Suitable for thread which raster data.
@ kBackground
Suitable for threads that shouldn't disrupt high priority work.
@ kDisplay
Suitable for threads which generate data for the display.
static SkString identifier(const FontFamilyDesc &family, const FontDesc &font)
@ kRaster
Suitable for thread which raster data.
#define SAFE_ACCESS(pointer, member, default_value)
#define FML_LOG(severity)
fml::Thread::ThreadConfig MakeThreadConfig(flutter::ThreadHost::Type type, fml::Thread::ThreadPriority priority)
static fml::RefPtr< fml::TaskRunner > GetCurrentThreadTaskRunner()
constexpr const char * kFlutterThreadName
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service host
fml::Thread::ThreadConfig ThreadConfig
fml::Thread::ThreadConfigSetter ThreadConfigSetter
static std::pair< bool, fml::RefPtr< EmbedderTaskRunner > > CreateEmbedderTaskRunner(const FlutterTaskRunnerDescription *description)
Attempts to create a task runner from an embedder task runner description. The first boolean in the p...
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not set
BoolCallback runs_task_on_current_thread_callback
FlutterTaskRunnerPostTaskCallback post_task_callback
static std::string MakeThreadName(Type type, const std::string &prefix)
Use the prefix and thread type to generator a thread name.
The collection of all the threads used by the engine.
The ThreadConfig is the thread info include thread name, thread priority.