Merge changes I8783ad29,I2c860a84 into 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) 2023 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{true};   /// Visible state (Not hashed)
39   bool mRendered{false}; /// Rendering state (Not hashed)
40
41   /**
42    * Calculate a hash from the cache data
43    */
44   void CalculateHash()
45   {
46     hash = Dali::INITIAL_HASH_VALUE;
47     AddToHash(hash, &matrix, sizeof(decltype(matrix)));
48     AddToHash(hash, &color, sizeof(decltype(color)));
49     AddToHash(hash, &updatedPositionSize, sizeof(decltype(updatedPositionSize)));
50     AddToHash(hash, &size, sizeof(decltype(size)));
51   }
52
53   /**
54    * @brief Tests whether cache changed since last frame
55    * @return True if changed
56    */
57   bool IsUpdated(PartialRenderingData& frameCache)
58   {
59     frameCache.CalculateHash();
60
61     return hash != frameCache.hash ||
62            matrix != frameCache.matrix ||
63            color != frameCache.color ||
64            updatedPositionSize != frameCache.updatedPositionSize ||
65            size != frameCache.size ||
66            !mRendered; // If everything is the same, check if we didn't render last frame.
67   }
68
69   void Update(const PartialRenderingData& frameCache)
70   {
71     matrix              = frameCache.matrix;
72     color               = frameCache.color;
73     updatedPositionSize = frameCache.updatedPositionSize;
74     size                = frameCache.size;
75     hash                = frameCache.hash;
76
77     mRendered = true;
78     // Don't change mVisible.
79   }
80
81 private:
82   void AddToHash(uint32_t& aHash, void* el, size_t numBytes)
83   {
84     uint8_t* elBytes = static_cast<uint8_t*>(el);
85     for(size_t i = 0; i < numBytes; ++i)
86     {
87       aHash = aHash * 33 + *elBytes++;
88     }
89   }
90 };
91
92 } // namespace Dali::Internal::SceneGraph
93
94 #endif // DALI_INTERNAL_SCENE_GRAPH_PARTIAL_RENDERING_DATA_H