da899a94d985d482de5c9965a60d405970944a65
[platform/core/uifw/dali-core.git] / dali / internal / render / common / render-instruction-container.cpp
1 /*
2  * Copyright (c) 2018 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-instruction-container.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/render/common/render-instruction.h>
23
24 namespace Dali
25 {
26
27 namespace Internal
28 {
29
30 namespace SceneGraph
31 {
32
33 RenderInstructionContainer::RenderInstructionContainer()
34 {
35   // array initialisation in ctor initializer list not supported until C++ 11
36   mIndex[ 0 ] = 0u;
37   mIndex[ 1 ] = 0u;
38 }
39
40 RenderInstructionContainer::~RenderInstructionContainer()
41 {
42   // OwnerContainer deletes the instructions
43 }
44
45 void RenderInstructionContainer::ResetAndReserve( BufferIndex bufferIndex, uint32_t capacityRequired )
46 {
47   mIndex[ bufferIndex ] = 0u;
48   uint32_t oldcapacity = static_cast<uint32_t>( mInstructions[ bufferIndex ].Capacity() ); // uint32_t is large enough in practice
49   if( oldcapacity < capacityRequired )
50   {
51     mInstructions[ bufferIndex ].Reserve( capacityRequired );
52     // add N new elements
53     for( ; oldcapacity < capacityRequired; ++oldcapacity )
54     {
55       mInstructions[ bufferIndex ].PushBack( new RenderInstruction );
56     }
57   }
58   // Note that we may have spare elements in the list, we don't remove them as that would
59   // decrease the capacity of our container and lead to possibly reallocating, which we hate
60   // RenderInstruction holds a lot of data so we keep them and recycle instead of new & delete
61 }
62
63 uint32_t RenderInstructionContainer::Count( BufferIndex bufferIndex )
64 {
65   // mIndex contains the number of instructions that have been really prepared and updated
66   // (from UpdateManager through GetNextInstruction)
67   return mIndex[ bufferIndex ];
68 }
69
70 RenderInstruction& RenderInstructionContainer::GetNextInstruction( BufferIndex bufferIndex )
71 {
72   // At protects against running out of space
73   return At( bufferIndex, mIndex[ bufferIndex ]++ );
74 }
75
76 RenderInstruction& RenderInstructionContainer::At( BufferIndex bufferIndex, uint32_t index )
77 {
78   DALI_ASSERT_DEBUG( index < mInstructions[ bufferIndex ].Count() );
79
80   return *mInstructions[ bufferIndex ][ index ];
81 }
82
83 void RenderInstructionContainer::DiscardCurrentInstruction( BufferIndex bufferIndex )
84 {
85   mIndex[ bufferIndex ]--;
86 }
87
88 } // namespace SceneGraph
89
90 } // namespace Internal
91
92 } // namespace Dali