[dali_2.3.37] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / update / nodes / partial-rendering-data.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_PARTIAL_RENDERING_DATA_H
2 #define DALI_INTERNAL_SCENE_GRAPH_PARTIAL_RENDERING_DATA_H
3
4 /*
5  * Copyright (c) 2024 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <dali/public-api/math/matrix.h>
21 #include <dali/public-api/math/vector3.h>
22 #include <dali/public-api/math/vector4.h>
23
24 namespace Dali::Internal::SceneGraph
25 {
26 /**
27  * Structure contains partial rendering data used in order to determine
28  * whether anything has changed and node has to be updated
29  */
30 struct PartialRenderingData
31 {
32   Matrix   matrix{};              /// Model-view matrix
33   Vector4  color{};               /// Color
34   Vector4  updatedPositionSize{}; /// Updated position/size (x, y, width, height)
35   Vector3  size{};                /// Size
36   uint32_t hash{0u};              /// Last frame's hash
37
38   bool mVisible : 1; /// Visible state. It is depends on node's visibility (Not hashed)
39   bool mUpdated : 1; /// IsUpdated return true at this frame. Will be reset at UpdateNodes time. (Not hashed)
40
41   enum Decay
42   {
43     EXPIRED                = 0,
44     UPDATED_PREVIOUS_FRAME = 1,
45     UPDATED_CURRENT_FRAME  = 2
46   };
47   Decay mUpdateDecay : 3; ///< Update decay (aging, Not hashed)
48
49   PartialRenderingData()
50   : mVisible{true},
51     mUpdated{false},
52     mUpdateDecay{Decay::EXPIRED}
53   {
54   }
55
56   /**
57    * Calculate a hash from the cache data
58    */
59   void CalculateHash()
60   {
61     hash = Dali::INITIAL_HASH_VALUE;
62     AddToHash(hash, &matrix, sizeof(decltype(matrix)));
63     AddToHash(hash, &color, sizeof(decltype(color)));
64     AddToHash(hash, &updatedPositionSize, sizeof(decltype(updatedPositionSize)));
65     AddToHash(hash, &size, sizeof(decltype(size)));
66   }
67
68   /**
69    * @brief Tests whether cache changed since last frame
70    * @return True if changed.
71    */
72   bool IsUpdated(PartialRenderingData& frameCache)
73   {
74     if(mUpdateDecay == Decay::UPDATED_CURRENT_FRAME)
75     {
76       return mUpdated;
77     }
78
79     frameCache.CalculateHash();
80
81     mUpdated = hash != frameCache.hash ||
82                mUpdateDecay == Decay::EXPIRED || ///< If current value is expired, so cached data is invalid.
83                matrix != frameCache.matrix ||
84                color != frameCache.color ||
85                updatedPositionSize != frameCache.updatedPositionSize ||
86                size != frameCache.size;
87
88     mUpdateDecay = Decay::UPDATED_CURRENT_FRAME;
89
90     return mUpdated;
91   }
92
93   /**
94    * @brief Update cached value
95    */
96   void Update(const PartialRenderingData& frameCache)
97   {
98     matrix              = frameCache.matrix;
99     color               = frameCache.color;
100     updatedPositionSize = frameCache.updatedPositionSize;
101     size                = frameCache.size;
102     hash                = frameCache.hash;
103
104     // Don't change mVisible.
105   }
106
107   /**
108    * @brief Aging down for this data.
109    */
110   void Aging()
111   {
112     mUpdateDecay = static_cast<Decay>(static_cast<int>(mUpdateDecay) >> 1u);
113   }
114
115   /**
116    * @brief Make this data expired.
117    */
118   void MakeExpired()
119   {
120     mUpdateDecay = Decay::EXPIRED;
121   }
122
123 private:
124   void AddToHash(uint32_t& aHash, void* el, size_t numBytes)
125   {
126     uint8_t* elBytes = static_cast<uint8_t*>(el);
127     for(size_t i = 0; i < numBytes; ++i)
128     {
129       aHash = aHash * 33 + *elBytes++;
130     }
131   }
132 };
133
134 } // namespace Dali::Internal::SceneGraph
135
136 #endif // DALI_INTERNAL_SCENE_GRAPH_PARTIAL_RENDERING_DATA_H