Refactoring node partial update cache
[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) 2022 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/internal/update/rendering/scene-graph-renderer.h>
21 #include <dali/public-api/math/matrix.h>
22 #include <dali/public-api/math/vector3.h>
23 #include <dali/public-api/math/vector4.h>
24
25 namespace Dali::Internal::SceneGraph
26 {
27 class Node;
28 class TextureSet;
29
30 /**
31  * Structure contains partial rendering data used in order to determine
32  * whether anything has changed and node has to be updated
33  */
34 struct PartialRenderingData
35 {
36   Node*             node{nullptr};                  /// Node associated with the entry
37   Renderer*         renderer{nullptr};              /// Renderer object
38   const TextureSet* textureSet{nullptr};            /// TextureSet object
39   Matrix            matrix{};                       /// Model-view matrix
40   Vector4           color{};                        /// Color
41   Vector4           updatedPositionSize{};          /// Updated position/size (x, y, width, height)
42   Vector3           size{};                         /// Size
43   uint32_t          depthIndex{0u};                 /// Depth index
44   uint32_t          hash;                           /// Last frame's hash
45   bool              isOpaque{};                     /// Opacity state
46
47   bool mVisible{true};   /// Visible state (Not hashed)
48   bool mRendered{false}; /// Rendering state (Not hashed)
49
50   /**
51    * Calculate a hash from the cache data
52    */
53   void CalculateHash()
54   {
55     hash = Dali::INITIAL_HASH_VALUE;
56     AddToHash(hash, &node, sizeof(decltype(node)));
57     AddToHash(hash, &renderer, sizeof(decltype(renderer)));
58     AddToHash(hash, &textureSet, sizeof(decltype(textureSet)));
59     AddToHash(hash, &matrix, sizeof(decltype(matrix)));
60     AddToHash(hash, &color, sizeof(decltype(color)));
61     AddToHash(hash, &updatedPositionSize, sizeof(decltype(updatedPositionSize)));
62     AddToHash(hash, &size, sizeof(decltype(size)));
63     AddToHash(hash, &depthIndex, sizeof(decltype(depthIndex)));
64     AddToHash(hash, &isOpaque, sizeof(decltype(isOpaque)));
65   }
66
67   /**
68    * @brief Tests whether cache changed since last frame
69    * @return True if changed
70    */
71   bool IsUpdated(PartialRenderingData& frameCache)
72   {
73     frameCache.CalculateHash();
74
75     return hash != frameCache.hash ||
76            node != frameCache.node ||
77            renderer != frameCache.renderer ||
78            textureSet != frameCache.textureSet ||
79            matrix != frameCache.matrix ||
80            color != frameCache.color ||
81            updatedPositionSize != frameCache.updatedPositionSize ||
82            size != frameCache.size ||
83            depthIndex != frameCache.depthIndex ||
84            isOpaque != frameCache.isOpaque ||
85
86            !mRendered; // If everything is the same, check if we didn't render last frame.
87   }
88
89   void Update(const PartialRenderingData& frameCache)
90   {
91     node                = frameCache.node;
92     renderer            = frameCache.renderer;
93     textureSet          = frameCache.textureSet;
94     matrix              = frameCache.matrix;
95     color               = frameCache.color;
96     updatedPositionSize = frameCache.updatedPositionSize;
97     size                = frameCache.size;
98     depthIndex          = frameCache.depthIndex;
99     isOpaque            = frameCache.isOpaque;
100     hash                = frameCache.hash;
101
102     mRendered = true;
103     // Don't change mVisible.
104   }
105
106 private:
107   void AddToHash(uint32_t& aHash, void* el, size_t numBytes)
108   {
109     uint8_t* elBytes = static_cast<uint8_t*>(el);
110     for(size_t i = 0; i < numBytes; ++i)
111     {
112       aHash = aHash * 33 + *elBytes++;
113     }
114   }
115 };
116
117 } // namespace Dali::Internal::SceneGraph
118
119 #endif // DALI_INTERNAL_SCENE_GRAPH_PARTIAL_RENDERING_DATA_H