(Partial Update) Change UPDATE_SIZE_HINT to UPDATE_AREA_HINT
[platform/core/uifw/dali-core.git] / dali / internal / render / common / render-item.cpp
1 /*
2  * Copyright (c) 2022 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> gRenderItemPool;
30 } // namespace
31 namespace Dali
32 {
33 namespace Internal
34 {
35 namespace SceneGraph
36 {
37 RenderItem* RenderItem::New()
38 {
39   return new(gRenderItemPool.AllocateRaw()) RenderItem();
40 }
41
42 RenderItem::RenderItem()
43 : mModelMatrix(false),
44   mModelViewMatrix(false),
45   mColor(Vector4::ZERO),
46   mSize(),
47   mRenderer(nullptr),
48   mNode(nullptr),
49   mTextureSet(nullptr),
50   mDepthIndex(0),
51   mIsOpaque(true),
52   mIsUpdated(false)
53 {
54 }
55
56 RenderItem::~RenderItem() = default;
57
58 ClippingBox RenderItem::CalculateTransformSpaceAABB(const Matrix& transformMatrix, const Vector3& position, const Vector3& size)
59 {
60   // Calculate extent vector of the AABB:
61   const float halfActorX = size.x * 0.5f;
62   const float halfActorY = size.y * 0.5f;
63
64   // To transform the actor bounds to the transformed space, We do a fast, 2D version of a matrix multiply optimized for 2D quads.
65   // This reduces float multiplications from 64 (16 * 4) to 12 (4 * 3).
66   // We create an array of 4 corners and directly initialize the first 3 with the matrix multiplication result of the respective corner.
67   // This causes the construction of the vector arrays contents in-place for optimization.
68   // We place the coords into the array in clockwise order, so we know opposite corners are always i + 2 from corner i.
69   // We skip the 4th corner here as we can calculate that from the other 3, bypassing matrix multiplication.
70   // Note: The below transform methods use a fast (2D) matrix multiply (only 4 multiplications are done).
71   Vector2 corners[4]{Transform2D(transformMatrix, -halfActorX + position.x, -halfActorY + position.y),
72                      Transform2D(transformMatrix, halfActorX + position.x, -halfActorY + position.y),
73                      Transform2D(transformMatrix, halfActorX + position.x, halfActorY + position.y)};
74
75   // 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).
76   corners[3] = Vector2(corners[0] + (corners[2] - corners[1]));
77
78   // Calculate the AABB:
79   // We use knowledge that opposite corners will be the max/min of each other. Doing this reduces the normal 12 branching comparisons to 3.
80   // The standard equivalent min/max code of the below would be:
81   //       Vector2 AABBmax( std::max( corners[0].x, std::max( corners[1].x, std::max( corners[3].x, corners[2].x ) ) ),
82   //                        std::max( corners[0].y, std::max( corners[1].y, std::max( corners[3].y, corners[2].y ) ) ) );
83   //       Vector2 AABBmin( std::min( corners[0].x, std::min( corners[1].x, std::min( corners[3].x, corners[2].x ) ) ),
84   //                        std::min( corners[0].y, std::min( corners[1].y, std::min( corners[3].y, corners[2].y ) ) ) );
85   unsigned int smallestX = 0u;
86   // Loop 3 times to find the index of the smallest X value.
87   // Note: We deliberately do NOT unroll the code here as this hampers the compilers output.
88   for(unsigned int i = 1u; i < 4u; ++i)
89   {
90     if(corners[i].x < corners[smallestX].x)
91     {
92       smallestX = i;
93     }
94   }
95
96   // As we are dealing with a rectangle, we can assume opposite corners are the largest.
97   // So without doing min/max branching, we can fetch the min/max values of all the remaining X/Y coords from this one index.
98   Vector4 aabb(corners[smallestX].x, corners[(smallestX + 3u) % 4].y, corners[(smallestX + 2u) % 4].x, corners[(smallestX + 1u) % 4].y);
99
100   // Round outwards from center
101   int x = static_cast<int>(floor(aabb.x));
102   int y = static_cast<int>(floor(aabb.y));
103   int z = static_cast<int>(ceilf(aabb.z));
104   int w = static_cast<int>(ceilf(aabb.w));
105
106   return ClippingBox(x, y, z - x, fabsf(w - y));
107 }
108
109 ClippingBox RenderItem::CalculateViewportSpaceAABB(const Matrix& modelViewMatrix, const Vector3& position, const Vector3& size, const int viewportWidth, const int viewportHeight)
110 {
111   // Calculate extent vector of the AABB:
112   const float halfActorX = size.x * 0.5f;
113   const float halfActorY = size.y * 0.5f;
114
115   // To transform the actor bounds to screen-space, We do a fast, 2D version of a matrix multiply optimized for 2D quads.
116   // This reduces float multiplications from 64 (16 * 4) to 12 (4 * 3).
117   // We create an array of 4 corners and directly initialize the first 3 with the matrix multiplication result of the respective corner.
118   // This causes the construction of the vector arrays contents in-place for optimization.
119   // We place the coords into the array in clockwise order, so we know opposite corners are always i + 2 from corner i.
120   // We skip the 4th corner here as we can calculate that from the other 3, bypassing matrix multiplication.
121   // Note: The below transform methods use a fast (2D) matrix multiply (only 4 multiplications are done).
122   Vector2 corners[4]{Transform2D(modelViewMatrix, -halfActorX + position.x, -halfActorY + position.y),
123                      Transform2D(modelViewMatrix, halfActorX + position.x, -halfActorY + position.y),
124                      Transform2D(modelViewMatrix, halfActorX + position.x, halfActorY + position.y)};
125
126   // 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).
127   corners[3] = Vector2(corners[0] + (corners[2] - corners[1]));
128
129   // Calculate the AABB:
130   // We use knowledge that opposite corners will be the max/min of each other. Doing this reduces the normal 12 branching comparisons to 3.
131   // The standard equivalent min/max code of the below would be:
132   //       Vector2 AABBmax( std::max( corners[0].x, std::max( corners[1].x, std::max( corners[3].x, corners[2].x ) ) ),
133   //                        std::max( corners[0].y, std::max( corners[1].y, std::max( corners[3].y, corners[2].y ) ) ) );
134   //       Vector2 AABBmin( std::min( corners[0].x, std::min( corners[1].x, std::min( corners[3].x, corners[2].x ) ) ),
135   //                        std::min( corners[0].y, std::min( corners[1].y, std::min( corners[3].y, corners[2].y ) ) ) );
136   unsigned int smallestX = 0u;
137   // Loop 3 times to find the index of the smallest X value.
138   // Note: We deliberately do NOT unroll the code here as this hampers the compilers output.
139   for(unsigned int i = 1u; i < 4u; ++i)
140   {
141     if(corners[i].x < corners[smallestX].x)
142     {
143       smallestX = i;
144     }
145   }
146
147   // As we are dealing with a rectangle, we can assume opposite corners are the largest.
148   // So without doing min/max branching, we can fetch the min/max values of all the remaining X/Y coords from this one index.
149   Vector4 aabb(corners[smallestX].x, corners[(smallestX + 3u) % 4].y, corners[(smallestX + 2u) % 4].x, corners[(smallestX + 1u) % 4].y);
150
151   // Return the AABB in screen-space pixels (x, y, width, height).
152   // Note: This is a algebraic simplification of: ( viewport.x - aabb.width ) / 2 - ( ( aabb.width / 2 ) + aabb.x ) per axis.
153   Vector4 aabbInScreen(static_cast<float>(viewportWidth) * 0.5f - aabb.z,
154                        static_cast<float>(viewportHeight) * 0.5f - aabb.w,
155                        static_cast<float>(viewportWidth) * 0.5f - aabb.x,
156                        static_cast<float>(viewportHeight) * 0.5f - aabb.y);
157
158   int x = static_cast<int>(floor(aabbInScreen.x));
159   int y = static_cast<int>(floor(aabbInScreen.y));
160   int z = static_cast<int>(roundf(aabbInScreen.z));
161   int w = static_cast<int>(roundf(aabbInScreen.w));
162
163   return ClippingBox(x, y, z - x, w - y);
164 }
165
166 void RenderItem::operator delete(void* ptr)
167 {
168   gRenderItemPool.Free(static_cast<RenderItem*>(ptr));
169 }
170
171 } // namespace SceneGraph
172
173 } // namespace Internal
174
175 } // namespace Dali