[dali_2.3.23] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / render / common / render-item.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/render/common/render-item.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/common/math.h>
23 #include <dali/internal/common/memory-pool-object-allocator.h>
24 #include <dali/internal/render/renderers/render-renderer.h>
25
26 namespace
27 {
28 //Memory pool used to allocate new RenderItems. Memory used by this pool will be released when shutting down DALi
29 Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::RenderItem>& GetRenderItemPool()
30 {
31   static Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::RenderItem> gRenderItemPool;
32   return gRenderItemPool;
33 }
34 } // namespace
35
36 namespace Dali
37 {
38 namespace Internal
39 {
40 namespace SceneGraph
41 {
42 RenderItem* RenderItem::New()
43 {
44   return new(GetRenderItemPool().AllocateRaw()) RenderItem();
45 }
46
47 RenderItemKey RenderItem::NewKey()
48 {
49   void* ptr = GetRenderItemPool().AllocateRaw();
50   auto  key = GetRenderItemPool().GetKeyFromPtr(static_cast<RenderItem*>(ptr));
51   new(ptr) RenderItem();
52   return RenderItemKey(key);
53 }
54
55 RenderItem::RenderItem()
56 : mModelMatrix(false),
57   mModelViewMatrix(false),
58   mScale(),
59   mSize(),
60   mRenderer{},
61   mNode(nullptr),
62   mTextureSet(nullptr),
63   mDepthIndex(0),
64   mIsOpaque(true),
65   mIsUpdated(false)
66 {
67 }
68
69 RenderItem::~RenderItem() = default;
70
71 RenderItem* RenderItem::Get(RenderItemKey::KeyType key)
72 {
73   return GetRenderItemPool().GetPtrFromKey(key);
74 }
75
76 RenderItemKey RenderItem::GetKey(const RenderItem& renderItem)
77 {
78   return RenderItemKey(GetRenderItemPool().GetKeyFromPtr(const_cast<RenderItem*>(&renderItem)));
79 }
80
81 RenderItemKey RenderItem::GetKey(RenderItem* renderItem)
82 {
83   return RenderItemKey(GetRenderItemPool().GetKeyFromPtr(renderItem));
84 }
85
86 ClippingBox RenderItem::CalculateTransformSpaceAABB(const Matrix& transformMatrix, const Vector3& position, const Vector3& size)
87 {
88   // Calculate extent vector of the AABB:
89   const float halfActorX = size.x * 0.5f;
90   const float halfActorY = size.y * 0.5f;
91
92   // To transform the actor bounds to the transformed space, We do a fast, 2D version of a matrix multiply optimized for 2D quads.
93   // This reduces float multiplications from 64 (16 * 4) to 12 (4 * 3).
94   // We create an array of 4 corners and directly initialize the first 3 with the matrix multiplication result of the respective corner.
95   // This causes the construction of the vector arrays contents in-place for optimization.
96   // We place the coords into the array in clockwise order, so we know opposite corners are always i + 2 from corner i.
97   // We skip the 4th corner here as we can calculate that from the other 3, bypassing matrix multiplication.
98   // Note: The below transform methods use a fast (2D) matrix multiply (only 4 multiplications are done).
99   Vector2 corners[4]{Transform2D(transformMatrix, -halfActorX + position.x, -halfActorY + position.y),
100                      Transform2D(transformMatrix, halfActorX + position.x, -halfActorY + position.y),
101                      Transform2D(transformMatrix, halfActorX + position.x, halfActorY + position.y)};
102
103   // As we are dealing with a rectangle, we can do a fast calculation to get the 4th corner from knowing the other 3 (even if rotated).
104   corners[3] = Vector2(corners[0] + (corners[2] - corners[1]));
105
106   // Calculate the AABB:
107   // We use knowledge that opposite corners will be the max/min of each other. Doing this reduces the normal 12 branching comparisons to 3.
108   // The standard equivalent min/max code of the below would be:
109   //       Vector2 AABBmax( std::max( corners[0].x, std::max( corners[1].x, std::max( corners[3].x, corners[2].x ) ) ),
110   //                        std::max( corners[0].y, std::max( corners[1].y, std::max( corners[3].y, corners[2].y ) ) ) );
111   //       Vector2 AABBmin( std::min( corners[0].x, std::min( corners[1].x, std::min( corners[3].x, corners[2].x ) ) ),
112   //                        std::min( corners[0].y, std::min( corners[1].y, std::min( corners[3].y, corners[2].y ) ) ) );
113   unsigned int smallestX = 0u;
114   // Loop 3 times to find the index of the smallest X value.
115   // Note: We deliberately do NOT unroll the code here as this hampers the compilers output.
116   for(unsigned int i = 1u; i < 4u; ++i)
117   {
118     if(corners[i].x < corners[smallestX].x)
119     {
120       smallestX = i;
121     }
122   }
123
124   // As we are dealing with a rectangle, we can assume opposite corners are the largest.
125   // So without doing min/max branching, we can fetch the min/max values of all the remaining X/Y coords from this one index.
126   Vector4 aabb(corners[smallestX].x, corners[(smallestX + 3u) % 4].y, corners[(smallestX + 2u) % 4].x, corners[(smallestX + 1u) % 4].y);
127
128   // Round outwards from center
129   int x = static_cast<int>(floor(aabb.x));
130   int y = static_cast<int>(floor(aabb.y));
131   int z = static_cast<int>(ceilf(aabb.z));
132   int w = static_cast<int>(ceilf(aabb.w));
133
134   return ClippingBox(x, y, z - x, fabsf(w - y));
135 }
136
137 ClippingBox RenderItem::CalculateViewportSpaceAABB(const Matrix& modelViewMatrix, const Vector3& position, const Vector3& size, const int viewportWidth, const int viewportHeight)
138 {
139   // Calculate extent vector of the AABB:
140   const float halfActorX = size.x * 0.5f;
141   const float halfActorY = size.y * 0.5f;
142
143   // To transform the actor bounds to screen-space, We do a fast, 2D version of a matrix multiply optimized for 2D quads.
144   // This reduces float multiplications from 64 (16 * 4) to 12 (4 * 3).
145   // We create an array of 4 corners and directly initialize the first 3 with the matrix multiplication result of the respective corner.
146   // This causes the construction of the vector arrays contents in-place for optimization.
147   // We place the coords into the array in clockwise order, so we know opposite corners are always i + 2 from corner i.
148   // We skip the 4th corner here as we can calculate that from the other 3, bypassing matrix multiplication.
149   // Note: The below transform methods use a fast (2D) matrix multiply (only 4 multiplications are done).
150   Vector2 corners[4]{Transform2D(modelViewMatrix, -halfActorX + position.x, -halfActorY + position.y),
151                      Transform2D(modelViewMatrix, halfActorX + position.x, -halfActorY + position.y),
152                      Transform2D(modelViewMatrix, halfActorX + position.x, halfActorY + position.y)};
153
154   // As we are dealing with a rectangle, we can do a fast calculation to get the 4th corner from knowing the other 3 (even if rotated).
155   corners[3] = Vector2(corners[0] + (corners[2] - corners[1]));
156
157   // Calculate the AABB:
158   // We use knowledge that opposite corners will be the max/min of each other. Doing this reduces the normal 12 branching comparisons to 3.
159   // The standard equivalent min/max code of the below would be:
160   //       Vector2 AABBmax( std::max( corners[0].x, std::max( corners[1].x, std::max( corners[3].x, corners[2].x ) ) ),
161   //                        std::max( corners[0].y, std::max( corners[1].y, std::max( corners[3].y, corners[2].y ) ) ) );
162   //       Vector2 AABBmin( std::min( corners[0].x, std::min( corners[1].x, std::min( corners[3].x, corners[2].x ) ) ),
163   //                        std::min( corners[0].y, std::min( corners[1].y, std::min( corners[3].y, corners[2].y ) ) ) );
164   unsigned int smallestX = 0u;
165   // Loop 3 times to find the index of the smallest X value.
166   // Note: We deliberately do NOT unroll the code here as this hampers the compilers output.
167   for(unsigned int i = 1u; i < 4u; ++i)
168   {
169     if(corners[i].x < corners[smallestX].x)
170     {
171       smallestX = i;
172     }
173   }
174
175   // As we are dealing with a rectangle, we can assume opposite corners are the largest.
176   // So without doing min/max branching, we can fetch the min/max values of all the remaining X/Y coords from this one index.
177   Vector4 aabb(corners[smallestX].x, corners[(smallestX + 3u) % 4].y, corners[(smallestX + 2u) % 4].x, corners[(smallestX + 1u) % 4].y);
178
179   // Return the AABB in screen-space pixels (x, y, width, height).
180   // Note: This is a algebraic simplification of: ( viewport.x - aabb.width ) / 2 - ( ( aabb.width / 2 ) + aabb.x ) per axis.
181   Vector4 aabbInScreen(static_cast<float>(viewportWidth) * 0.5f - aabb.z,
182                        static_cast<float>(viewportHeight) * 0.5f - aabb.w,
183                        static_cast<float>(viewportWidth) * 0.5f - aabb.x,
184                        static_cast<float>(viewportHeight) * 0.5f - aabb.y);
185
186   int x = static_cast<int>(floor(aabbInScreen.x));
187   int y = static_cast<int>(floor(aabbInScreen.y));
188   int z = static_cast<int>(roundf(aabbInScreen.z));
189   int w = static_cast<int>(roundf(aabbInScreen.w));
190
191   return ClippingBox(x, y, z - x, w - y);
192 }
193
194 void RenderItem::operator delete(void* ptr)
195 {
196   GetRenderItemPool().Free(static_cast<RenderItem*>(ptr));
197 }
198
199 uint32_t RenderItem::GetMemoryPoolCapacity()
200 {
201   return GetRenderItemPool().GetCapacity();
202 }
203
204 } // namespace SceneGraph
205
206 } // namespace Internal
207
208 } // namespace Dali