| 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 "flutter/shell/common/thread_host.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | #include <memory> |
| 9 | #include <optional> |
| 10 | #include <string> |
| 11 | #include <utility> |
| 12 | |
| 13 | namespace flutter { |
| 14 | |
| 15 | std::string ThreadHost::ThreadHostConfig::MakeThreadName( |
| 16 | Type type, |
| 17 | const std::string& prefix) { |
| 18 | switch (type) { |
| 19 | case Type::Platform: |
| 20 | return prefix + ".platform" ; |
| 21 | case Type::UI: |
| 22 | return prefix + ".ui" ; |
| 23 | case Type::IO: |
| 24 | return prefix + ".io" ; |
| 25 | case Type::RASTER: |
| 26 | return prefix + ".raster" ; |
| 27 | case Type::Profiler: |
| 28 | return prefix + ".profiler" ; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | void ThreadHost::ThreadHostConfig::SetIOConfig(const ThreadConfig& config) { |
| 33 | type_mask |= ThreadHost::Type::IO; |
| 34 | io_config = config; |
| 35 | } |
| 36 | |
| 37 | void ThreadHost::ThreadHostConfig::SetUIConfig(const ThreadConfig& config) { |
| 38 | type_mask |= ThreadHost::Type::UI; |
| 39 | ui_config = config; |
| 40 | } |
| 41 | |
| 42 | void ThreadHost::ThreadHostConfig::SetPlatformConfig( |
| 43 | const ThreadConfig& config) { |
| 44 | type_mask |= ThreadHost::Type::Platform; |
| 45 | platform_config = config; |
| 46 | } |
| 47 | |
| 48 | void ThreadHost::ThreadHostConfig::SetRasterConfig(const ThreadConfig& config) { |
| 49 | type_mask |= ThreadHost::Type::RASTER; |
| 50 | raster_config = config; |
| 51 | } |
| 52 | |
| 53 | void ThreadHost::ThreadHostConfig::SetProfilerConfig( |
| 54 | const ThreadConfig& config) { |
| 55 | type_mask |= ThreadHost::Type::Profiler; |
| 56 | profiler_config = config; |
| 57 | } |
| 58 | |
| 59 | std::unique_ptr<fml::Thread> ThreadHost::CreateThread( |
| 60 | Type type, |
| 61 | std::optional<ThreadConfig> thread_config, |
| 62 | const ThreadHostConfig& host_config) const { |
| 63 | /// if not specified ThreadConfig, create a ThreadConfig. |
| 64 | if (!thread_config.has_value()) { |
| 65 | thread_config = ThreadConfig( |
| 66 | ThreadHostConfig::MakeThreadName(type, prefix: host_config.name_prefix)); |
| 67 | } |
| 68 | return std::make_unique<fml::Thread>(args: host_config.config_setter, |
| 69 | args&: thread_config.value()); |
| 70 | } |
| 71 | |
| 72 | ThreadHost::ThreadHost() = default; |
| 73 | |
| 74 | ThreadHost::ThreadHost(ThreadHost&&) = default; |
| 75 | |
| 76 | ThreadHost::ThreadHost(const std::string& name_prefix, uint64_t mask) |
| 77 | : ThreadHost(ThreadHostConfig(name_prefix, mask)) {} |
| 78 | |
| 79 | ThreadHost::ThreadHost(const ThreadHostConfig& host_config) |
| 80 | : name_prefix(host_config.name_prefix) { |
| 81 | if (host_config.isThreadNeeded(type: ThreadHost::Type::Platform)) { |
| 82 | platform_thread = |
| 83 | CreateThread(type: Type::Platform, thread_config: host_config.platform_config, host_config); |
| 84 | } |
| 85 | |
| 86 | if (host_config.isThreadNeeded(type: ThreadHost::Type::UI)) { |
| 87 | ui_thread = CreateThread(type: Type::UI, thread_config: host_config.ui_config, host_config); |
| 88 | } |
| 89 | |
| 90 | if (host_config.isThreadNeeded(type: ThreadHost::Type::RASTER)) { |
| 91 | raster_thread = |
| 92 | CreateThread(type: Type::RASTER, thread_config: host_config.raster_config, host_config); |
| 93 | } |
| 94 | |
| 95 | if (host_config.isThreadNeeded(type: ThreadHost::Type::IO)) { |
| 96 | io_thread = CreateThread(type: Type::IO, thread_config: host_config.io_config, host_config); |
| 97 | } |
| 98 | |
| 99 | if (host_config.isThreadNeeded(type: ThreadHost::Type::Profiler)) { |
| 100 | profiler_thread = |
| 101 | CreateThread(type: Type::Profiler, thread_config: host_config.profiler_config, host_config); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | ThreadHost::~ThreadHost() = default; |
| 106 | |
| 107 | } // namespace flutter |
| 108 | |