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 #include "modifier/rs_modifier_type.h"
29
30 namespace OHOS {
31 class Surface;
32
33 namespace Rosen {
34 using AnimationId = uint64_t;
35 using NodeId = uint64_t;
36 using PropertyId = uint64_t;
37 constexpr uint32_t UNI_MAIN_THREAD_INDEX = UINT32_MAX;
38 constexpr uint64_t INVALID_NODEID = UINT64_MAX;
39
40 // types in the same layer should be 0/1/2/4/8
41 // types for UINode
42 enum class RSUINodeType : uint32_t {
43 UNKNOW = 0x0000u,
44 RS_NODE = 0x0001u,
45 DISPLAY_NODE = 0x0011u,
46 SURFACE_NODE = 0x0021u,
47 PROXY_NODE = 0x0041u,
48 CANVAS_NODE = 0x0081u,
49 EFFECT_NODE = 0x0101u,
50 ROOT_NODE = 0x1081u,
51 CANVAS_DRAWING_NODE = 0x2081u,
52 };
53
54 enum class FollowType : uint8_t {
55 NONE,
56 FOLLOW_TO_PARENT,
57 FOLLOW_TO_SELF,
58 };
59
60 static inline const std::unordered_map<RSUINodeType, std::string> RSUINodeTypeStrs = {
61 {RSUINodeType::UNKNOW, "UNKNOW"},
62 {RSUINodeType::DISPLAY_NODE, "DisplayNode"},
63 {RSUINodeType::RS_NODE, "RsNode"},
64 {RSUINodeType::SURFACE_NODE, "SurfaceNode"},
65 {RSUINodeType::PROXY_NODE, "ProxyNode"},
66 {RSUINodeType::CANVAS_NODE, "CanvasNode"},
67 {RSUINodeType::ROOT_NODE, "RootNode"},
68 {RSUINodeType::EFFECT_NODE, "EffectNode"},
69 {RSUINodeType::CANVAS_DRAWING_NODE, "CanvasDrawingNode"},
70 };
71
72 // types for RenderNode
73 enum class RSRenderNodeType : uint32_t {
74 UNKNOW = 0x0000u,
75 RS_NODE = 0x0001u,
76 DISPLAY_NODE = 0x0011u,
77 SURFACE_NODE = 0x0021u,
78 PROXY_NODE = 0x0041u,
79 CANVAS_NODE = 0x0081u,
80 EFFECT_NODE = 0x0101u,
81 ROOT_NODE = 0x1081u,
82 CANVAS_DRAWING_NODE = 0x2081u,
83 };
84
85 enum class CacheType : uint8_t {
86 NONE = 0,
87 CONTENT,
88 ANIMATE_PROPERTY,
89 };
90
91 enum RSDrawingCacheType : uint8_t {
92 DISABLED_CACHE = 0,
93 FORCED_CACHE, // must-to-do case
94 TARGETED_CACHE // suggested case which could be disabled by optimized strategy
95 };
96
97 // priority for node, higher number means lower priority
98 enum class NodePriorityType : uint8_t {
99 MAIN_PRIORITY = 0, // node must render in main thread
100 SUB_HIGH_PRIORITY, // node render in sub thread with high priority
101 SUB_LOW_PRIORITY, // node render in sub thread with low priority
102 };
103
104 // status for sub thread node
105 enum class CacheProcessStatus : uint8_t {
106 WAITING = 0, // waiting for process
107 DOING, // processing
108 DONE, // processed
109 };
110
111 enum class DeviceType : uint8_t {
112 PHONE,
113 PC,
114 TABLET,
115 OTHERS,
116 };
117
118 // types for RSSurfaceRenderNode
119 enum class RSSurfaceNodeType : uint8_t {
120 DEFAULT,
121 APP_WINDOW_NODE, // surfacenode created as app main window
122 ABILITY_COMPONENT_NODE, // surfacenode created as ability component
123 SELF_DRAWING_NODE, // surfacenode created by arkui component (except ability component)
124 STARTING_WINDOW_NODE, // starting window, surfacenode created by wms
125 LEASH_WINDOW_NODE, // leashwindow
126 SELF_DRAWING_WINDOW_NODE, // create by wms, such as pointer window and bootanimation
127 };
128
129 struct RSSurfaceRenderNodeConfig {
130 NodeId id = 0;
131 std::string name = "SurfaceNode";
132 std::string bundleName = "";
133 RSSurfaceNodeType nodeType = RSSurfaceNodeType::DEFAULT;
134 void* additionalData = nullptr;
135 };
136
137 struct RSDisplayNodeConfig {
138 uint64_t screenId = 0;
139 bool isMirrored = false;
140 NodeId mirrorNodeId = 0;
141 };
142
143 constexpr int64_t NS_TO_S = 1000000000;
144 constexpr int64_t NS_PER_MS = 1000000;
145
146 #if defined(M_PI)
147 constexpr float PI = M_PI;
148 #else
149 static const float PI = std::atanf(1.0) * 4;
150 #endif
151
152 template<typename T>
ROSEN_EQ(const T & x,const T & y)153 inline constexpr bool ROSEN_EQ(const T& x, const T& y)
154 {
155 if constexpr (std::is_floating_point<T>::value) {
156 return (std::abs((x) - (y)) <= (std::numeric_limits<T>::epsilon()));
157 } else {
158 return x == y;
159 }
160 }
161
162 template<typename T>
ROSEN_EQ(T x,T y,T epsilon)163 inline bool ROSEN_EQ(T x, T y, T epsilon)
164 {
165 return (std::abs((x) - (y)) <= (epsilon));
166 }
167
168 template<typename T>
ROSEN_EQ(const std::weak_ptr<T> & x,const std::weak_ptr<T> & y)169 inline bool ROSEN_EQ(const std::weak_ptr<T>& x, const std::weak_ptr<T>& y)
170 {
171 return !(x.owner_before(y) || y.owner_before(x));
172 }
173
ROSEN_LNE(float left,float right)174 inline bool ROSEN_LNE(float left, float right) //less not equal
175 {
176 constexpr float epsilon = -0.001f;
177 return (left - right) < epsilon;
178 }
179
ROSEN_GNE(float left,float right)180 inline bool ROSEN_GNE(float left, float right) //great not equal
181 {
182 constexpr float epsilon = 0.001f;
183 return (left - right) > epsilon;
184 }
185
ROSEN_GE(float left,float right)186 inline bool ROSEN_GE(float left, float right) //great or equal
187 {
188 constexpr float epsilon = -0.001f;
189 return (left - right) > epsilon;
190 }
191
ROSEN_LE(float left,float right)192 inline bool ROSEN_LE(float left, float right) //less or equal
193 {
194 constexpr float epsilon = 0.001f;
195 return (left - right) < epsilon;
196 }
197
198 class MemObject {
199 public:
MemObject(size_t size)200 explicit MemObject(size_t size) : size_(size) {}
201 virtual ~MemObject() = default;
202
203 void* operator new(size_t size);
204 void operator delete(void* ptr);
205
206 void* operator new(std::size_t size, const std::nothrow_t&) noexcept;
207 void operator delete(void* ptr, const std::nothrow_t&) noexcept;
208
209 protected:
210 size_t size_;
211 };
212
ExtractPid(uint64_t id)213 inline constexpr pid_t ExtractPid(uint64_t id)
214 {
215 // extract high 32 bits of nodeid/animationId/propertyId as pid
216 return static_cast<pid_t>(id >> 32);
217 }
218
219 template<class Container, class Predicate>
EraseIf(Container & container,Predicate pred)220 inline typename Container::size_type EraseIf(Container& container, Predicate pred)
221 {
222 // erase from container if pred returns true, backport of c++20 std::remove_if
223 typename Container::size_type oldSize = container.size();
224 const typename Container::iterator end = container.end();
225 for (typename Container::iterator iter = container.begin(); iter != end;) {
226 if (pred(*iter)) {
227 iter = container.erase(iter);
228 } else {
229 ++iter;
230 }
231 }
232 return oldSize - container.size();
233 }
234
235 } // namespace Rosen
236 } // namespace OHOS
237 #endif // RENDER_SERVICE_CLIENT_CORE_COMMON_RS_COMMON_DEF_H
238