Flutter Engine
The Flutter Engine
thread_host.cc
Go to the documentation of this file.
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
13namespace flutter {
14
16 Type type,
17 const std::string& prefix) {
18 switch (type) {
19 case Type::kPlatform:
20 return prefix + ".platform";
21 case Type::kUi:
22 return prefix + ".ui";
23 case Type::kIo:
24 return prefix + ".io";
25 case Type::kRaster:
26 return prefix + ".raster";
27 case Type::kProfiler:
28 return prefix + ".profiler";
29 }
30}
31
33 type_mask |= ThreadHost::Type::kIo;
34 io_config = config;
35}
36
38 type_mask |= ThreadHost::Type::kUi;
39 ui_config = config;
40}
41
43 const ThreadConfig& config) {
44 type_mask |= ThreadHost::Type::kPlatform;
45 platform_config = config;
46}
47
49 type_mask |= ThreadHost::Type::kRaster;
50 raster_config = config;
51}
52
54 const ThreadConfig& config) {
55 type_mask |= ThreadHost::Type::kProfiler;
56 profiler_config = config;
57}
58
59std::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(
67 }
68 return std::make_unique<fml::Thread>(host_config.config_setter,
69 thread_config.value());
70}
71
72ThreadHost::ThreadHost() = default;
73
75
76ThreadHost::ThreadHost(const std::string& name_prefix, uint64_t mask)
78
80 : name_prefix(host_config.name_prefix) {
81 if (host_config.isThreadNeeded(ThreadHost::Type::kPlatform)) {
83 CreateThread(Type::kPlatform, host_config.platform_config, host_config);
84 }
85
86 if (host_config.isThreadNeeded(ThreadHost::Type::kUi)) {
87 ui_thread = CreateThread(Type::kUi, host_config.ui_config, host_config);
88 }
89
92 CreateThread(Type::kRaster, host_config.raster_config, host_config);
93 }
94
95 if (host_config.isThreadNeeded(ThreadHost::Type::kIo)) {
96 io_thread = CreateThread(Type::kIo, host_config.io_config, host_config);
97 }
98
99 if (host_config.isThreadNeeded(ThreadHost::Type::kProfiler)) {
101 CreateThread(Type::kProfiler, host_config.profiler_config, host_config);
102 }
103}
104
105ThreadHost::~ThreadHost() = default;
106
107} // namespace flutter
GLenum type
@ kRaster
Suitable for thread which raster data.
Definition: embedder.h:266
fml::Thread::ThreadConfig ThreadConfig
Definition: thread_host.h:17
std::optional< ThreadConfig > platform_config
Definition: thread_host.h:75
std::optional< ThreadConfig > io_config
Definition: thread_host.h:78
void SetUIConfig(const ThreadConfig &)
Specified the UI Thread Config, meanwhile set the mask.
Definition: thread_host.cc:37
const ThreadConfigSetter config_setter
Definition: thread_host.h:73
void SetProfilerConfig(const ThreadConfig &)
Specified the ProfilerThread Config, meanwhile set the mask.
Definition: thread_host.cc:53
std::optional< ThreadConfig > ui_config
Definition: thread_host.h:76
std::optional< ThreadConfig > raster_config
Definition: thread_host.h:77
static std::string MakeThreadName(Type type, const std::string &prefix)
Use the prefix and thread type to generator a thread name.
Definition: thread_host.cc:15
void SetIOConfig(const ThreadConfig &)
Specified the IO Thread Config, meanwhile set the mask.
Definition: thread_host.cc:32
void SetRasterConfig(const ThreadConfig &)
Specified the IO Thread Config, meanwhile set the mask.
Definition: thread_host.cc:48
bool isThreadNeeded(Type type) const
Check if need to create thread.
Definition: thread_host.h:49
std::optional< ThreadConfig > profiler_config
Definition: thread_host.h:79
void SetPlatformConfig(const ThreadConfig &)
Specified the Platform Thread Config, meanwhile set the mask.
Definition: thread_host.cc:42
The collection of all the threads used by the engine.
Definition: thread_host.h:21
std::unique_ptr< fml::Thread > io_thread
Definition: thread_host.h:86
std::unique_ptr< fml::Thread > platform_thread
Definition: thread_host.h:83
std::string name_prefix
Definition: thread_host.h:82
std::unique_ptr< fml::Thread > profiler_thread
Definition: thread_host.h:87
std::unique_ptr< fml::Thread > raster_thread
Definition: thread_host.h:85
std::unique_ptr< fml::Thread > ui_thread
Definition: thread_host.h:84
The ThreadConfig is the thread info include thread name, thread priority.
Definition: thread.h:35