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#ifndef FLUTTER_SHELL_COMMON_THREAD_HOST_H_
6#define FLUTTER_SHELL_COMMON_THREAD_HOST_H_
7
8#include <memory>
9#include <optional>
10#include <string>
11
12#include "flutter/fml/macros.h"
13#include "flutter/fml/thread.h"
14
15namespace flutter {
16
17using ThreadConfig = fml::Thread::ThreadConfig;
18using ThreadConfigSetter = fml::Thread::ThreadConfigSetter;
19
20/// The collection of all the threads used by the engine.
21struct ThreadHost {
22 enum Type {
23 Platform = 1 << 0,
24 UI = 1 << 1,
25 RASTER = 1 << 2,
26 IO = 1 << 3,
27 Profiler = 1 << 4,
28 };
29
30 /// The collection of all the thread configures, and we create custom thread
31 /// configure in engine to info the thread.
32 struct ThreadHostConfig {
33 explicit ThreadHostConfig(
34 const ThreadConfigSetter& setter = fml::Thread::SetCurrentThreadName)
35 : type_mask(0), config_setter(setter) {}
36
37 ThreadHostConfig(
38 const std::string& name_prefix,
39 uint64_t mask,
40 const ThreadConfigSetter& setter = fml::Thread::SetCurrentThreadName)
41 : type_mask(mask), name_prefix(name_prefix), config_setter(setter) {}
42
43 explicit ThreadHostConfig(
44 uint64_t mask,
45 const ThreadConfigSetter& setter = fml::Thread::SetCurrentThreadName)
46 : ThreadHostConfig("", mask, setter) {}
47
48 /// Check if need to create thread.
49 bool isThreadNeeded(Type type) const { return type_mask & type; }
50
51 /// Use the prefix and thread type to generator a thread name.
52 static std::string MakeThreadName(Type type, const std::string& prefix);
53
54 /// Specified the UI Thread Config, meanwhile set the mask.
55 void SetUIConfig(const ThreadConfig&);
56
57 /// Specified the Platform Thread Config, meanwhile set the mask.
58 void SetPlatformConfig(const ThreadConfig&);
59
60 /// Specified the IO Thread Config, meanwhile set the mask.
61 void SetRasterConfig(const ThreadConfig&);
62
63 /// Specified the IO Thread Config, meanwhile set the mask.
64 void SetIOConfig(const ThreadConfig&);
65
66 /// Specified the ProfilerThread Config, meanwhile set the mask.
67 void SetProfilerConfig(const ThreadConfig&);
68
69 uint64_t type_mask;
70
71 std::string name_prefix = "";
72
73 const ThreadConfigSetter config_setter;
74
75 std::optional<ThreadConfig> platform_config;
76 std::optional<ThreadConfig> ui_config;
77 std::optional<ThreadConfig> raster_config;
78 std::optional<ThreadConfig> io_config;
79 std::optional<ThreadConfig> profiler_config;
80 };
81
82 std::string name_prefix;
83 std::unique_ptr<fml::Thread> platform_thread;
84 std::unique_ptr<fml::Thread> ui_thread;
85 std::unique_ptr<fml::Thread> raster_thread;
86 std::unique_ptr<fml::Thread> io_thread;
87 std::unique_ptr<fml::Thread> profiler_thread;
88
89 ThreadHost();
90
91 ThreadHost(ThreadHost&&);
92
93 ThreadHost& operator=(ThreadHost&&) = default;
94
95 ThreadHost(const std::string& name_prefix, uint64_t mask);
96
97 explicit ThreadHost(const ThreadHostConfig& host_config);
98
99 ~ThreadHost();
100
101 private:
102 std::unique_ptr<fml::Thread> CreateThread(
103 Type type,
104 std::optional<ThreadConfig> thread_config,
105 const ThreadHostConfig& host_config) const;
106};
107
108} // namespace flutter
109
110#endif // FLUTTER_SHELL_COMMON_THREAD_HOST_H_
111

source code of flutter_engine/flutter/shell/common/thread_host.h