• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 struct RSSurfaceCaptureConfig {
206     float scaleX = 1.0f;
207     float scaleY = 1.0f;
208     bool useDma = false;
209     bool useCurWindow = true;
210     SurfaceCaptureType captureType = SurfaceCaptureType::DEFAULT_CAPTURE;
211     bool isSync = false;
212 };
213 
214 struct RSSurfaceCapturePermissions {
215     bool screenCapturePermission = false;
216     bool isSystemCalling = false;
217     bool selfCapture = false;
218 };
219 
220 enum class DeviceType : uint8_t {
221     PHONE,
222     PC,
223     TABLET,
224     OTHERS,
225 };
226 
227 enum GrallocBufferAttr : uint32_t {
228     // used in set roi region to codec, must be the same as private key in codec
229     GRALLOC_BUFFER_ATTR_BUFFER_ROI_INFO = 2054,
230 };
231 
232 // types for PC SystemAnimatedScenes
233 enum class SystemAnimatedScenes : uint32_t {
234     ENTER_MISSION_CENTER, // Enter the mission center
235     EXIT_MISSION_CENTER, // Exit the mission center
236     ENTER_TFS_WINDOW, // Three-finger sliding window recovery
237     EXIT_TFU_WINDOW, // The three-finger up window disappears
238     ENTER_WINDOW_FULL_SCREEN, // Enter the window full screen
239     EXIT_WINDOW_FULL_SCREEN, // Exit the window full screen
240     ENTER_MAX_WINDOW, // Enter the window maximization state
241     EXIT_MAX_WINDOW, // Exit the window maximization state
242     ENTER_SPLIT_SCREEN, // Enter the split screen
243     EXIT_SPLIT_SCREEN, // Exit the split screen
244     ENTER_APP_CENTER, // Enter the app center
245     EXIT_APP_CENTER, // Exit the app center
246     APPEAR_MISSION_CENTER, // A special case scenario that displays the mission center
247     ENTER_WIND_CLEAR, // Enter win+D in clear screen mode
248     ENTER_WIND_RECOVER, // Enter win+D in recover mode
249     ENTER_RECENTS, // Enter recents
250     EXIT_RECENTS, // Exit recents
251     OTHERS, // 1.Default state 2.The state in which the animation ends
252 };
253 
254 // types for RSSurfaceRenderNode
255 enum class RSSurfaceNodeType : uint8_t {
256     DEFAULT,
257     APP_WINDOW_NODE,          // surfacenode created as app main window
258     STARTING_WINDOW_NODE,     // starting window, surfacenode created by wms
259     SELF_DRAWING_WINDOW_NODE, // create by wms, such as pointer window and bootanimation
260     LEASH_WINDOW_NODE,        // leashwindow
261     ABILITY_COMPONENT_NODE,   // surfacenode created as ability component
262     SELF_DRAWING_NODE,        // surfacenode created by arkui component (except ability component)
263     SURFACE_TEXTURE_NODE,      // create by video
264     FOREGROUND_SURFACE,
265     SCB_SCREEN_NODE,          // surfacenode created as sceneboard
266     UI_EXTENSION_COMMON_NODE, // uiextension node
267     UI_EXTENSION_SECURE_NODE, // uiextension node that requires info callback
268 };
269 
270 enum class MultiThreadCacheType : uint8_t {
271     NONE = 0,
272     LEASH_WINDOW,
273     ARKTS_CARD,
274     NONFOCUS_WINDOW,
275 };
276 
277 enum class SelfDrawingNodeType : uint8_t {
278     DEFAULT,
279     VIDEO,
280 };
281 
282 enum class SurfaceWindowType : uint8_t {
283     DEFAULT_WINDOW = 0,
284     SYSTEM_SCB_WINDOW = 1,
285 };
286 
287 struct RSSurfaceRenderNodeConfig {
288     NodeId id = 0;
289     std::string name = "SurfaceNode";
290     std::string bundleName = "";
291     RSSurfaceNodeType nodeType = RSSurfaceNodeType::DEFAULT;
292     void* additionalData = nullptr;
293     bool isTextureExportNode = false;
294     bool isSync = false;
295     enum SurfaceWindowType surfaceWindowType = SurfaceWindowType::DEFAULT_WINDOW;
296 };
297 
298 // types for RSSurfaceExt
299 enum class RSSurfaceExtType : uint8_t {
300     NONE,
301     SURFACE_TEXTURE,
302     SURFACE_PLATFORM_TEXTURE,
303 };
304 
305 struct RSSurfaceExtConfig {
306     RSSurfaceExtType type = RSSurfaceExtType::NONE;
307     void* additionalData = nullptr;
308 };
309 using RSSurfaceTextureConfig = RSSurfaceExtConfig;
310 using RSSurfaceTextureAttachCallBack = std::function<void(int64_t textureId, bool attach)>;
311 using RSSurfaceTextureUpdateCallBack = std::function<void(std::vector<float>&)>;
312 
313 struct RSDisplayNodeConfig {
314     uint64_t screenId = 0;
315     bool isMirrored = false;
316     NodeId mirrorNodeId = 0;
317     bool isSync = false;
318 };
319 
320 // ability state of surface node
321 enum class RSSurfaceNodeAbilityState : uint8_t {
322     BACKGROUND,
323     FOREGROUND,
324 };
325 
326 constexpr int64_t NS_TO_S = 1000000000;
327 constexpr int64_t NS_PER_MS = 1000000;
328 constexpr uint32_t SIZE_UPPER_LIMIT = 1000;
329 constexpr uint32_t PARTICLE_UPPER_LIMIT = 1000000;
330 
331 #if defined(M_PI)
332 constexpr float PI = M_PI;
333 #else
334 static const float PI = std::atanf(1.0) * 4;
335 #endif
336 
337 template<typename T>
ROSEN_EQ(const T & x,const T & y)338 inline constexpr bool ROSEN_EQ(const T& x, const T& y)
339 {
340     if constexpr (std::is_floating_point<T>::value) {
341         return (std::abs((x) - (y)) <= (std::numeric_limits<T>::epsilon()));
342     } else {
343         return x == y;
344     }
345 }
346 
347 template<typename T>
ROSEN_EQ(T x,T y,T epsilon)348 inline bool ROSEN_EQ(T x, T y, T epsilon)
349 {
350     return (std::abs((x) - (y)) <= (epsilon));
351 }
352 
353 template<typename T>
ROSEN_EQ(const std::weak_ptr<T> & x,const std::weak_ptr<T> & y)354 inline bool ROSEN_EQ(const std::weak_ptr<T>& x, const std::weak_ptr<T>& y)
355 {
356     return !(x.owner_before(y) || y.owner_before(x));
357 }
358 
ROSEN_LNE(float left,float right)359 inline bool ROSEN_LNE(float left, float right) // less not equal
360 {
361     constexpr float epsilon = -0.001f;
362     return (left - right) < epsilon;
363 }
364 
ROSEN_GNE(float left,float right)365 inline bool ROSEN_GNE(float left, float right) // great not equal
366 {
367     constexpr float epsilon = 0.001f;
368     return (left - right) > epsilon;
369 }
370 
ROSEN_GE(float left,float right)371 inline bool ROSEN_GE(float left, float right) // great or equal
372 {
373     constexpr float epsilon = -0.001f;
374     return (left - right) > epsilon;
375 }
376 
ROSEN_LE(float left,float right)377 inline bool ROSEN_LE(float left, float right) // less or equal
378 {
379     constexpr float epsilon = 0.001f;
380     return (left - right) < epsilon;
381 }
382 
383 class MemObject {
384 public:
MemObject(size_t size)385     explicit MemObject(size_t size) : size_(size) {}
386     virtual ~MemObject() = default;
387 
388     void* operator new(size_t size);
389     void operator delete(void* ptr);
390 
391     void* operator new(std::size_t size, const std::nothrow_t&) noexcept;
392     void operator delete(void* ptr, const std::nothrow_t&) noexcept;
393 
394 protected:
395     size_t size_;
396 };
397 
ExtractPid(uint64_t id)398 inline constexpr pid_t ExtractPid(uint64_t id)
399 {
400     // extract high 32 bits of nodeid/animationId/propertyId as pid
401     return static_cast<pid_t>(id >> 32);
402 }
403 
404 template<class Container, class Predicate>
EraseIf(Container & container,Predicate pred)405 inline typename Container::size_type EraseIf(Container& container, Predicate pred)
406 {
407     // erase from container if pred returns true, backport of c++20 std::remove_if
408     typename Container::size_type oldSize = container.size();
409     const typename Container::iterator end = container.end();
410     for (typename Container::iterator iter = container.begin(); iter != end;) {
411         if (pred(*iter)) {
412             iter = container.erase(iter);
413         } else {
414             ++iter;
415         }
416     }
417     return oldSize - container.size();
418 }
419 
420 enum class AncoFlags : uint32_t {
421     IS_ANCO_NODE = 0x0001
422 };
423 
424 enum class RSInterfaceErrorCode : uint32_t {
425 #undef NO_ERROR
426     NO_ERROR = 0,
427     NONSYSTEM_CALLING,
428     NOT_SELF_CALLING,
429     WRITE_PARCEL_ERROR,
430     UNKNOWN_ERROR,
431 };
432 
433 } // namespace Rosen
434 } // namespace OHOS
435 #endif // RENDER_SERVICE_CLIENT_CORE_COMMON_RS_COMMON_DEF_H
436