Added memory pool logging
[platform/core/uifw/dali-core.git] / dali / internal / render / queue / render-queue.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/queue/render-queue.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/common/message.h>
23
24 namespace Dali
25 {
26 namespace Internal
27 {
28 namespace // unnamed namespace
29 {
30 static const std::size_t INITIAL_BUFFER_SIZE = 32768;
31 static const std::size_t MAX_BUFFER_SIZE     = 32768;
32
33 } // unnamed namespace
34
35 namespace SceneGraph
36 {
37 RenderQueue::RenderQueue()
38 : container0(nullptr),
39   container1(nullptr)
40 {
41   container0 = new MessageBuffer(INITIAL_BUFFER_SIZE);
42   container1 = new MessageBuffer(INITIAL_BUFFER_SIZE);
43 }
44
45 RenderQueue::~RenderQueue()
46 {
47   if(container0)
48   {
49     for(MessageBuffer::Iterator iter = container0->Begin(); iter.IsValid(); iter.Next())
50     {
51       MessageBase* message = reinterpret_cast<MessageBase*>(iter.Get());
52
53       // Call virtual destructor explicitly; since delete will not be called after placement new
54       message->~MessageBase();
55     }
56
57     delete container0;
58   }
59
60   if(container1)
61   {
62     for(MessageBuffer::Iterator iter = container1->Begin(); iter.IsValid(); iter.Next())
63     {
64       MessageBase* message = reinterpret_cast<MessageBase*>(iter.Get());
65
66       // Call virtual destructor explicitly; since delete will not be called after placement new
67       message->~MessageBase();
68     }
69
70     delete container1;
71   }
72 }
73
74 uint32_t* RenderQueue::ReserveMessageSlot(BufferIndex updateBufferIndex, std::size_t size)
75 {
76   MessageBuffer* container = GetCurrentContainer(updateBufferIndex);
77
78   return container->ReserveMessageSlot(size);
79 }
80
81 void RenderQueue::ProcessMessages(BufferIndex bufferIndex)
82 {
83   std::size_t capacity = container0->GetCapacity() + container1->GetCapacity();
84   mCapacity            = capacity; // write is atomic.
85
86   MessageBuffer* container = GetCurrentContainer(bufferIndex);
87
88   for(MessageBuffer::Iterator iter = container->Begin(); iter.IsValid(); iter.Next())
89   {
90     MessageBase* message = reinterpret_cast<MessageBase*>(iter.Get());
91
92     message->Process(bufferIndex);
93
94     // Call virtual destructor explictly; since delete will not be called after placement new
95     message->~MessageBase();
96   }
97
98   container->Reset();
99
100   LimitBufferCapacity(bufferIndex);
101 }
102
103 MessageBuffer* RenderQueue::GetCurrentContainer(BufferIndex bufferIndex)
104 {
105   MessageBuffer* container(nullptr);
106
107   /**
108    * The update-thread queues messages with one container,
109    * whilst the render-thread is processing the other.
110    */
111   if(!bufferIndex)
112   {
113     container = container0;
114   }
115   else
116   {
117     container = container1;
118   }
119
120   return container;
121 }
122
123 void RenderQueue::LimitBufferCapacity(BufferIndex bufferIndex)
124 {
125   if(!bufferIndex)
126   {
127     if(MAX_BUFFER_SIZE < container0->GetCapacity())
128     {
129       delete container0;
130       container0 = nullptr;
131       container0 = new MessageBuffer(INITIAL_BUFFER_SIZE);
132     }
133   }
134   else
135   {
136     if(MAX_BUFFER_SIZE < container1->GetCapacity())
137     {
138       delete container1;
139       container1 = nullptr;
140       container1 = new MessageBuffer(INITIAL_BUFFER_SIZE);
141     }
142   }
143 }
144
145 } // namespace SceneGraph
146
147 } // namespace Internal
148
149 } // namespace Dali