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 using InteractiveImplictAnimatorId = uint64_t;
39 using LeashPersistentId = uint64_t;
40 constexpr uint32_t UNI_MAIN_THREAD_INDEX = UINT32_MAX;
41 constexpr uint32_t UNI_RENDER_THREAD_INDEX = UNI_MAIN_THREAD_INDEX - 1;
42 constexpr uint64_t INVALID_NODEID = 0;
43 constexpr int32_t INSTANCE_ID_UNDEFINED = -1;
44 constexpr uint32_t RGBA_MAX = 255;
45 constexpr uint64_t INVALID_LEASH_PERSISTENTID = 0;
46
47 // types in the same layer should be 0/1/2/4/8
48 // types for UINode
49 enum class RSUINodeType : uint32_t {
50 UNKNOW = 0x0000u,
51 RS_NODE = 0x0001u,
52 DISPLAY_NODE = 0x0011u,
53 SURFACE_NODE = 0x0021u,
54 PROXY_NODE = 0x0041u,
55 CANVAS_NODE = 0x0081u,
56 EFFECT_NODE = 0x0101u,
57 ROOT_NODE = 0x1081u,
58 CANVAS_DRAWING_NODE = 0x2081u,
59 };
60
61 enum class FollowType : uint8_t {
62 NONE,
63 FOLLOW_TO_PARENT,
64 FOLLOW_TO_SELF,
65 };
66
67 #define LIKELY(exp) (__builtin_expect((exp) != 0, true))
68 #define UNLIKELY(exp) (__builtin_expect((exp) != 0, false))
69
70 // types for RenderNode
71 enum class RSRenderNodeType : uint32_t {
72 UNKNOW = 0x0000u,
73 RS_NODE = 0x0001u,
74 DISPLAY_NODE = 0x0011u,
75 SURFACE_NODE = 0x0021u,
76 PROXY_NODE = 0x0041u,
77 CANVAS_NODE = 0x0081u,
78 EFFECT_NODE = 0x0101u,
79 ROOT_NODE = 0x1081u,
80 CANVAS_DRAWING_NODE = 0x2081u,
81 };
82
83 // types for Processor
84 enum class RSProcessorType : uint32_t {
85 UNKNOW = 0x0000u,
86 RS_PROCESSOR = 0x0001u,
87 PHYSICAL_SCREEN_PROCESSOR = 0x0011u,
88 VIRTUAL_SCREEN_PROCESSOR = 0x0021u,
89 UNIRENDER_PROCESSOR = 0x0041u,
90 UNIRENDER_VIRTUAL_PROCESSOR = 0x0081u,
91 };
92
93 enum RSRenderParamsDirtyType {
94 NO_DIRTY = 0,
95 MATRIX_DIRTY,
96 LAYER_INFO_DIRTY,
97 BUFFER_INFO_DIRTY,
98 DRAWING_CACHE_TYPE_DIRTY,
99 MAX_DIRTY_TYPE,
100 };
101
102 enum class NodeDirtyType : uint32_t {
103 NOT_DIRTY = 0x0000u,
104 GEOMETRY = 0x0001u,
105 BACKGROUND = 0x0002u,
106 CONTENT = 0x0004u,
107 FOREGROUND = 0x0008u,
108 OVERLAY = 0x0010u,
109 APPEARANCE = 0x0020u,
110 };
111
112 enum class CacheType : uint8_t {
113 NONE = 0,
114 CONTENT,
115 ANIMATE_PROPERTY,
116 };
117
118 enum class DrawableCacheType : uint8_t {
119 NONE = 0,
120 CONTENT,
121 };
122
123 enum RSDrawingCacheType : uint8_t {
124 DISABLED_CACHE = 0,
125 FORCED_CACHE, // must-to-do case
126 TARGETED_CACHE, // suggested case which could be disabled by optimized strategy
127 FOREGROUND_FILTER_CACHE // using cache to draw foreground filter
128 };
129
130 enum class FilterCacheType : uint8_t {
131 NONE = 0,
132 SNAPSHOT = 1,
133 FILTERED_SNAPSHOT = 2,
134 BOTH = SNAPSHOT | FILTERED_SNAPSHOT,
135 };
136
137 // opinc state
138 enum NodeCacheState : uint8_t {
139 STATE_INIT = 0,
140 STATE_CHANGE,
141 STATE_UNCHANGE,
142 STATE_DISABLE,
143 };
144
145 enum NodeChangeType : uint8_t {
146 KEEP_UNCHANGE = 0,
147 SELF_DIRTY,
148 };
149
150 // opinc cache state
151 enum NodeStrategyType : uint8_t {
152 CACHE_NONE = 0,
153 DDGR_OPINC_DYNAMIC,
154 OPINC_AUTOCACHE,
155 NODE_GROUP,
156 CACHE_DISABLE,
157 };
158
159 enum NodeRecordState : uint8_t {
160 RECORD_NONE = 0,
161 RECORD_CALCULATE,
162 RECORD_CACHING,
163 RECORD_CACHED,
164 RECORD_DISABLE,
165 };
166
167 enum DrawAreaEnableState : uint8_t {
168 DRAW_AREA_INIT = 0,
169 DRAW_AREA_ENABLE,
170 DRAW_AREA_DISABLE,
171 };
172
173 // priority for node, higher number means lower priority
174 enum class NodePriorityType : uint8_t {
175 MAIN_PRIORITY = 0, // node must render in main thread
176 SUB_FOCUSNODE_PRIORITY, // node render in sub thread with the highest priority
177 SUB_HIGH_PRIORITY, // node render in sub thread with the second priority
178 SUB_LOW_PRIORITY, // node render in sub thread with low priority
179 };
180
181 enum class RSVisibleLevel : uint32_t {
182 RS_ALL_VISIBLE = 0,
183 RS_SEMI_NONDEFAULT_VISIBLE,
184 RS_SEMI_DEFAULT_VISIBLE,
185 RS_INVISIBLE,
186 RS_SYSTEM_ANIMATE_SCENE,
187 RS_UNKNOW_VISIBLE_LEVEL,
188 };
189
190 // status for sub thread node
191 enum class CacheProcessStatus : uint8_t {
192 WAITING = 0, // waiting for process
193 DOING, // processing
194 DONE, // processed
195 SKIPPED, // skip cur process and wait for next new data to process
196 UNKNOWN,
197 };
198
199 // the type of surfaceCapture
200 enum class SurfaceCaptureType : uint8_t {
201 DEFAULT_CAPTURE = 0, // displayNode capture or window capture
202 UICAPTURE,
203 };
204
205 #ifdef TP_FEATURE_ENABLE
206 // the type of TpFeatureConfig
207 enum class TpFeatureConfigType : uint8_t {
208 DEFAULT_TP_FEATURE = 0,
209 AFT_TP_FEATURE,
210 };
211 #endif
212
213 struct RSSurfaceCaptureConfig {
214 float scaleX = 1.0f;
215 float scaleY = 1.0f;
216 bool useDma = false;
217 bool useCurWindow = true;
218 SurfaceCaptureType captureType = SurfaceCaptureType::DEFAULT_CAPTURE;
219 bool isSync = false;
220 float screenLeft = 0.0f;
221 float screenTop = 0.0f;
222 float screenWidth = 0.0f;
223 float screenHeight = 0.0f;
224 };
225
226 struct RSSurfaceCapturePermissions {
227 bool screenCapturePermission = false;
228 bool isSystemCalling = false;
229 bool selfCapture = false;
230 };
231
232 enum class DeviceType : uint8_t {
233 PHONE,
234 PC,
235 TABLET,
236 OTHERS,
237 };
238
239 enum GrallocBufferAttr : uint32_t {
240 // used in set roi region to codec, must be the same as private key in codec
241 GRALLOC_BUFFER_ATTR_BUFFER_ROI_INFO = 2054,
242 };
243
244 // types for PC SystemAnimatedScenes
245 enum class SystemAnimatedScenes : uint32_t {
246 ENTER_MISSION_CENTER, // Enter the mission center
247 EXIT_MISSION_CENTER, // Exit the mission center
248 ENTER_TFS_WINDOW, // Three-finger sliding window recovery
249 EXIT_TFU_WINDOW, // The three-finger up window disappears
250 ENTER_WINDOW_FULL_SCREEN, // Enter the window full screen
251 EXIT_WINDOW_FULL_SCREEN, // Exit the window full screen
252 ENTER_MAX_WINDOW, // Enter the window maximization state
253 EXIT_MAX_WINDOW, // Exit the window maximization state
254 ENTER_SPLIT_SCREEN, // Enter the split screen
255 EXIT_SPLIT_SCREEN, // Exit the split screen
256 ENTER_APP_CENTER, // Enter the app center
257 EXIT_APP_CENTER, // Exit the app center
258 APPEAR_MISSION_CENTER, // A special case scenario that displays the mission center
259 ENTER_WIND_CLEAR, // Enter win+D in clear screen mode
260 ENTER_WIND_RECOVER, // Enter win+D in recover mode
261 ENTER_RECENTS, // Enter recents
262 EXIT_RECENTS, // Exit recents
263 LOCKSCREEN_TO_LAUNCHER, // Enter unlock screen for pc scene
264 OTHERS, // 1.Default state 2.The state in which the animation ends
265 };
266
267 // types for RSSurfaceRenderNode
268 enum class RSSurfaceNodeType : uint8_t {
269 DEFAULT,
270 APP_WINDOW_NODE, // surfacenode created as app main window
271 STARTING_WINDOW_NODE, // starting window, surfacenode created by wms
272 SELF_DRAWING_WINDOW_NODE, // create by wms, such as pointer window and bootanimation
273 LEASH_WINDOW_NODE, // leashwindow
274 ABILITY_COMPONENT_NODE, // surfacenode created as ability component
275 SELF_DRAWING_NODE, // surfacenode created by arkui component (except ability component)
276 SURFACE_TEXTURE_NODE, // create by video
277 FOREGROUND_SURFACE,
278 SCB_SCREEN_NODE, // surfacenode created as sceneboard
279 UI_EXTENSION_COMMON_NODE, // uiextension node
280 UI_EXTENSION_SECURE_NODE, // uiextension node that requires info callback
281 };
282
283 enum class MultiThreadCacheType : uint8_t {
284 NONE = 0,
285 LEASH_WINDOW,
286 ARKTS_CARD,
287 NONFOCUS_WINDOW,
288 };
289
290 enum class SelfDrawingNodeType : uint8_t {
291 DEFAULT,
292 VIDEO,
293 XCOM,
294 };
295
296 enum class RSUIFirstSwitch {
297 NONE, // follow RS rules
298 MODAL_WINDOW_CLOSE, // open app with modal window animation, close uifirst
299 FORCE_DISABLE, // force close uifirst
300 FORCE_ENABLE, // force open uifirst
301 FORCE_ENABLE_LIMIT, // force open uifirst, but is limited by system specification(filter, rotation).
302 FORCE_DISABLE_NONFOCUS, // force close uifirst when only in nonfocus window
303 };
304
305 enum class SurfaceWindowType : uint8_t {
306 DEFAULT_WINDOW = 0,
307 SYSTEM_SCB_WINDOW = 1,
308 };
309
310 struct RSSurfaceRenderNodeConfig {
311 NodeId id = 0;
312 std::string name = "SurfaceNode";
313 std::string bundleName = "";
314 RSSurfaceNodeType nodeType = RSSurfaceNodeType::DEFAULT;
315 void* additionalData = nullptr;
316 bool isTextureExportNode = false;
317 bool isSync = false;
318 enum SurfaceWindowType surfaceWindowType = SurfaceWindowType::DEFAULT_WINDOW;
319 };
320
321 // codes for arkui-x start
322 // types for RSSurfaceExt
323 enum class RSSurfaceExtType : uint8_t {
324 NONE,
325 SURFACE_TEXTURE,
326 SURFACE_PLATFORM_TEXTURE,
327 };
328
329 struct RSSurfaceExtConfig {
330 RSSurfaceExtType type = RSSurfaceExtType::NONE;
331 void* additionalData = nullptr;
332 };
333 using RSSurfaceTextureConfig = RSSurfaceExtConfig;
334 using RSSurfaceTextureAttachCallBack = std::function<void(int64_t textureId, bool attach)>;
335 using RSSurfaceTextureUpdateCallBack = std::function<void(std::vector<float>&)>;
336 using RSSurfaceTextureInitTypeCallBack = std::function<void(int32_t&)>;
337 // codes for arkui-x end
338
339 struct RSDisplayNodeConfig {
340 uint64_t screenId = 0;
341 bool isMirrored = false;
342 NodeId mirrorNodeId = 0;
343 bool isSync = false;
344 };
345
346 // ability state of surface node
347 enum class RSSurfaceNodeAbilityState : uint8_t {
348 BACKGROUND,
349 FOREGROUND,
350 };
351
352 struct SubSurfaceCntUpdateInfo {
353 int updateCnt_ = 0;
354 NodeId preParentId_ = INVALID_NODEID;
355 NodeId curParentId_ = INVALID_NODEID;
356 };
357
358 constexpr int64_t NS_TO_S = 1000000000;
359 constexpr int64_t NS_PER_MS = 1000000;
360 constexpr uint32_t SIZE_UPPER_LIMIT = 1000;
361 constexpr uint32_t PARTICLE_EMMITER_UPPER_LIMIT = 2000;
362 constexpr uint32_t PARTICLE_UPPER_LIMIT = 1000000;
363
364 #if defined(M_PI)
365 constexpr float PI = M_PI;
366 #else
367 static const float PI = std::atanf(1.0) * 4;
368 #endif
369
370 template<typename T>
ROSEN_EQ(const T & x,const T & y)371 inline constexpr bool ROSEN_EQ(const T& x, const T& y)
372 {
373 if constexpr (std::is_floating_point<T>::value) {
374 return (std::abs((x) - (y)) <= (std::numeric_limits<T>::epsilon()));
375 } else {
376 return x == y;
377 }
378 }
379
380 template<typename T>
ROSEN_EQ(T x,T y,T epsilon)381 inline bool ROSEN_EQ(T x, T y, T epsilon)
382 {
383 return (std::abs((x) - (y)) <= (epsilon));
384 }
385
386 template<typename T>
ROSEN_EQ(const std::weak_ptr<T> & x,const std::weak_ptr<T> & y)387 inline bool ROSEN_EQ(const std::weak_ptr<T>& x, const std::weak_ptr<T>& y)
388 {
389 return !(x.owner_before(y) || y.owner_before(x));
390 }
391
ROSEN_LNE(float left,float right)392 inline bool ROSEN_LNE(float left, float right) // less not equal
393 {
394 constexpr float epsilon = -0.001f;
395 return (left - right) < epsilon;
396 }
397
ROSEN_GNE(float left,float right)398 inline bool ROSEN_GNE(float left, float right) // great not equal
399 {
400 constexpr float epsilon = 0.001f;
401 return (left - right) > epsilon;
402 }
403
ROSEN_GE(float left,float right)404 inline bool ROSEN_GE(float left, float right) // great or equal
405 {
406 constexpr float epsilon = -0.001f;
407 return (left - right) > epsilon;
408 }
409
ROSEN_LE(float left,float right)410 inline bool ROSEN_LE(float left, float right) // less or equal
411 {
412 constexpr float epsilon = 0.001f;
413 return (left - right) < epsilon;
414 }
415
416 class MemObject {
417 public:
MemObject(size_t size)418 explicit MemObject(size_t size) : size_(size) {}
419 virtual ~MemObject() = default;
420
421 void* operator new(size_t size);
422 void operator delete(void* ptr);
423
424 void* operator new(std::size_t size, const std::nothrow_t&) noexcept;
425 void operator delete(void* ptr, const std::nothrow_t&) noexcept;
426
427 protected:
428 size_t size_;
429 };
430
ExtractPid(uint64_t id)431 inline constexpr pid_t ExtractPid(uint64_t id)
432 {
433 // extract high 32 bits of nodeid/animationId/propertyId as pid
434 return static_cast<pid_t>(id >> 32);
435 }
436
437 template<class Container, class Predicate>
EraseIf(Container & container,Predicate pred)438 inline typename Container::size_type EraseIf(Container& container, Predicate pred)
439 {
440 // erase from container if pred returns true, backport of c++20 std::remove_if
441 typename Container::size_type oldSize = container.size();
442 const typename Container::iterator end = container.end();
443 for (typename Container::iterator iter = container.begin(); iter != end;) {
444 if (pred(*iter)) {
445 iter = container.erase(iter);
446 } else {
447 ++iter;
448 }
449 }
450 return oldSize - container.size();
451 }
452
453 enum class AncoFlags : uint32_t {
454 IS_ANCO_NODE = 0x0001
455 };
456
457 enum class RSInterfaceErrorCode : uint32_t {
458 #undef NO_ERROR
459 NO_ERROR = 0,
460 NONSYSTEM_CALLING,
461 NOT_SELF_CALLING,
462 WRITE_PARCEL_ERROR,
463 UNKNOWN_ERROR,
464 };
465
466 } // namespace Rosen
467 } // namespace OHOS
468 #endif // RENDER_SERVICE_CLIENT_CORE_COMMON_RS_COMMON_DEF_H
469