1 /*
2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifndef RENDER_SERVICE_CLIENT_CORE_COMMON_RS_COMMON_DEF_H
16 #define RENDER_SERVICE_CLIENT_CORE_COMMON_RS_COMMON_DEF_H
17
18 #include <cmath>
19 #include <functional>
20 #include <limits>
21 #include <memory>
22 #include <string>
23 #include <unordered_map>
24 #include <unordered_set>
25 #include <unistd.h>
26
27 #include "common/rs_macros.h"
28
29 namespace OHOS {
30 class Surface;
31
32 namespace Rosen {
33 using AnimationId = uint64_t;
34 using NodeId = uint64_t;
35 using PropertyId = uint64_t;
36 using FrameRateLinkerId = uint64_t;
37 using SurfaceId = uint64_t;
38 constexpr uint32_t UNI_MAIN_THREAD_INDEX = UINT32_MAX;
39 constexpr uint64_t INVALID_NODEID = 0;
40
41 // types in the same layer should be 0/1/2/4/8
42 // types for UINode
43 enum class RSUINodeType : uint32_t {
44 UNKNOW = 0x0000u,
45 RS_NODE = 0x0001u,
46 DISPLAY_NODE = 0x0011u,
47 SURFACE_NODE = 0x0021u,
48 PROXY_NODE = 0x0041u,
49 CANVAS_NODE = 0x0081u,
50 EFFECT_NODE = 0x0101u,
51 ROOT_NODE = 0x1081u,
52 CANVAS_DRAWING_NODE = 0x2081u,
53 };
54
55 enum class FollowType : uint8_t {
56 NONE,
57 FOLLOW_TO_PARENT,
58 FOLLOW_TO_SELF,
59 };
60
61 #define LIKELY(exp) (__builtin_expect((exp) != 0, true))
62 #define UNLIKELY(exp) (__builtin_expect((exp) != 0, false))
63
64 // types for RenderNode
65 enum class RSRenderNodeType : uint32_t {
66 UNKNOW = 0x0000u,
67 RS_NODE = 0x0001u,
68 DISPLAY_NODE = 0x0011u,
69 SURFACE_NODE = 0x0021u,
70 PROXY_NODE = 0x0041u,
71 CANVAS_NODE = 0x0081u,
72 EFFECT_NODE = 0x0101u,
73 ROOT_NODE = 0x1081u,
74 CANVAS_DRAWING_NODE = 0x2081u,
75 };
76
77 enum class CacheType : uint8_t {
78 NONE = 0,
79 CONTENT,
80 ANIMATE_PROPERTY,
81 };
82
83 enum RSDrawingCacheType : uint8_t {
84 DISABLED_CACHE = 0,
85 FORCED_CACHE, // must-to-do case
86 TARGETED_CACHE // suggested case which could be disabled by optimized strategy
87 };
88
89 // priority for node, higher number means lower priority
90 enum class NodePriorityType : uint8_t {
91 MAIN_PRIORITY = 0, // node must render in main thread
92 SUB_FOCUSNODE_PRIORITY, // node render in sub thread with the highest priority
93 SUB_HIGH_PRIORITY, // node render in sub thread with the second priority
94 SUB_LOW_PRIORITY, // node render in sub thread with low priority
95 };
96
97 enum class RSVisibleLevel : uint32_t {
98 RS_ALL_VISIBLE = 0,
99 RS_SEMI_NONDEFAULT_VISIBLE,
100 RS_SEMI_DEFAULT_VISIBLE,
101 RS_INVISIBLE,
102 RS_SYSTEM_ANIMATE_SCENE,
103 RS_UNKNOW_VISIBLE_LEVEL,
104 };
105
106 // status for sub thread node
107 enum class CacheProcessStatus : uint8_t {
108 WAITING = 0, // waiting for process
109 DOING, // processing
110 DONE, // processed
111 };
112
113 // the type of surfaceCapture
114 enum class SurfaceCaptureType : uint8_t {
115 DEFAULT_CAPTURE = 0, // displayNode capture or window capture
116 UICAPTURE,
117 };
118
119 enum class DeviceType : uint8_t {
120 PHONE,
121 PC,
122 TABLET,
123 OTHERS,
124 };
125
126 // types for PC SystemAnimatedScenes
127 enum class SystemAnimatedScenes : uint32_t {
128 ENTER_MISSION_CENTER, // Enter the mission center
129 EXIT_MISSION_CENTER, // Exit the mission center
130 ENTER_TFS_WINDOW, // Three-finger sliding window recovery
131 EXIT_TFU_WINDOW, // The three-finger up window disappears
132 ENTER_WINDOW_FULL_SCREEN, // Enter the window full screen
133 EXIT_WINDOW_FULL_SCREEN, // Exit the window full screen
134 ENTER_MAX_WINDOW, // Enter the window maximization state
135 EXIT_MAX_WINDOW, // Exit the window maximization state
136 ENTER_SPLIT_SCREEN, // Enter the split screen
137 EXIT_SPLIT_SCREEN, // Exit the split screen
138 ENTER_APP_CENTER, // Enter the app center
139 EXIT_APP_CENTER, // Exit the app center
140 APPEAR_MISSION_CENTER, // A special case scenario that displays the mission center
141 ENTER_WIND_CLEAR, // Enter win+D in clear screen mode
142 ENTER_WIND_RECOVER, // Enter win+D in recover mode
143 OTHERS, // 1.Default state 2.The state in which the animation ends
144 };
145
146 // types for RSSurfaceRenderNode
147 enum class RSSurfaceNodeType : uint8_t {
148 DEFAULT,
149 APP_WINDOW_NODE, // surfacenode created as app main window
150 ABILITY_COMPONENT_NODE, // surfacenode created as ability component
151 SELF_DRAWING_NODE, // surfacenode created by arkui component (except ability component)
152 STARTING_WINDOW_NODE, // starting window, surfacenode created by wms
153 LEASH_WINDOW_NODE, // leashwindow
154 SELF_DRAWING_WINDOW_NODE, // create by wms, such as pointer window and bootanimation
155 SURFACE_TEXTURE_NODE, // create by video
156 FOREGROUND_SURFACE,
157 SCB_SCREEN_NODE, // surfacenode created as sceneboard
158 };
159
160 enum class SelfDrawingNodeType : uint8_t {
161 DEFAULT,
162 VIDEO,
163 };
164
165 struct RSSurfaceRenderNodeConfig {
166 NodeId id = 0;
167 std::string name = "SurfaceNode";
168 std::string bundleName = "";
169 RSSurfaceNodeType nodeType = RSSurfaceNodeType::DEFAULT;
170 void* additionalData = nullptr;
171 bool isTextureExportNode = false;
172 bool isSync = false;
173 };
174
175 // types for RSSurfaceExt
176 enum class RSSurfaceExtType : uint8_t {
177 NONE,
178 SURFACE_TEXTURE,
179 };
180
181 struct RSSurfaceExtConfig {
182 RSSurfaceExtType type = RSSurfaceExtType::NONE;
183 void* additionalData = nullptr;
184 };
185 using RSSurfaceTextureConfig = RSSurfaceExtConfig;
186 using RSSurfaceTextureAttachCallBack = std::function<void(int64_t textureId, bool attach)>;
187 using RSSurfaceTextureUpdateCallBack = std::function<void(std::vector<float>&)>;
188
189 struct RSDisplayNodeConfig {
190 uint64_t screenId = 0;
191 bool isMirrored = false;
192 NodeId mirrorNodeId = 0;
193 };
194
195 constexpr int64_t NS_TO_S = 1000000000;
196 constexpr int64_t NS_PER_MS = 1000000;
197 constexpr uint32_t SIZE_UPPER_LIMIT = 1000;
198 constexpr uint32_t PARTICLE_UPPER_LIMIT = 1000000;
199
200 #if defined(M_PI)
201 constexpr float PI = M_PI;
202 #else
203 static const float PI = std::atanf(1.0) * 4;
204 #endif
205
206 template<typename T>
ROSEN_EQ(const T & x,const T & y)207 inline constexpr bool ROSEN_EQ(const T& x, const T& y)
208 {
209 if constexpr (std::is_floating_point<T>::value) {
210 return (std::abs((x) - (y)) <= (std::numeric_limits<T>::epsilon()));
211 } else {
212 return x == y;
213 }
214 }
215
216 template<typename T>
ROSEN_EQ(T x,T y,T epsilon)217 inline bool ROSEN_EQ(T x, T y, T epsilon)
218 {
219 return (std::abs((x) - (y)) <= (epsilon));
220 }
221
222 template<typename T>
ROSEN_EQ(const std::weak_ptr<T> & x,const std::weak_ptr<T> & y)223 inline bool ROSEN_EQ(const std::weak_ptr<T>& x, const std::weak_ptr<T>& y)
224 {
225 return !(x.owner_before(y) || y.owner_before(x));
226 }
227
ROSEN_LNE(float left,float right)228 inline bool ROSEN_LNE(float left, float right) //less not equal
229 {
230 constexpr float epsilon = -0.001f;
231 return (left - right) < epsilon;
232 }
233
ROSEN_GNE(float left,float right)234 inline bool ROSEN_GNE(float left, float right) //great not equal
235 {
236 constexpr float epsilon = 0.001f;
237 return (left - right) > epsilon;
238 }
239
ROSEN_GE(float left,float right)240 inline bool ROSEN_GE(float left, float right) //great or equal
241 {
242 constexpr float epsilon = -0.001f;
243 return (left - right) > epsilon;
244 }
245
ROSEN_LE(float left,float right)246 inline bool ROSEN_LE(float left, float right) //less or equal
247 {
248 constexpr float epsilon = 0.001f;
249 return (left - right) < epsilon;
250 }
251
252 class MemObject {
253 public:
MemObject(size_t size)254 explicit MemObject(size_t size) : size_(size) {}
255 virtual ~MemObject() = default;
256
257 void* operator new(size_t size);
258 void operator delete(void* ptr);
259
260 void* operator new(std::size_t size, const std::nothrow_t&) noexcept;
261 void operator delete(void* ptr, const std::nothrow_t&) noexcept;
262
263 protected:
264 size_t size_;
265 };
266
ExtractPid(uint64_t id)267 inline constexpr pid_t ExtractPid(uint64_t id)
268 {
269 // extract high 32 bits of nodeid/animationId/propertyId as pid
270 return static_cast<pid_t>(id >> 32);
271 }
272
273 template<class Container, class Predicate>
EraseIf(Container & container,Predicate pred)274 inline typename Container::size_type EraseIf(Container& container, Predicate pred)
275 {
276 // erase from container if pred returns true, backport of c++20 std::remove_if
277 typename Container::size_type oldSize = container.size();
278 const typename Container::iterator end = container.end();
279 for (typename Container::iterator iter = container.begin(); iter != end;) {
280 if (pred(*iter)) {
281 iter = container.erase(iter);
282 } else {
283 ++iter;
284 }
285 }
286 return oldSize - container.size();
287 }
288
289 } // namespace Rosen
290 } // namespace OHOS
291 #endif // RENDER_SERVICE_CLIENT_CORE_COMMON_RS_COMMON_DEF_H
292