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