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