2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/update/queue/update-message-queue.h>
22 #include <dali/public-api/common/vector-wrapper.h>
23 #include <dali/devel-api/threading/mutex.h>
24 #include <dali/integration-api/render-controller.h>
25 #include <dali/internal/common/message.h>
26 #include <dali/internal/common/message-buffer.h>
27 #include <dali/internal/render/common/performance-monitor.h>
31 using Dali::Integration::RenderController;
32 using Dali::Internal::SceneGraph::SceneGraphBuffers;
40 namespace // unnamed namespace
43 // A message to set Actor::SIZE is 72 bytes on 32bit device
44 // A buffer of size 32768 would store (32768 - 4) / (72 + 4) = 431 of those messages
45 static const std::size_t INITIAL_BUFFER_SIZE = 32768;
46 static const std::size_t MAX_BUFFER_CAPACITY = 73728; // Avoid keeping buffers which exceed this
47 static const std::size_t MAX_FREE_BUFFER_COUNT = 3; // Allow this number of buffers to be recycled
49 // A queue of message buffers
50 typedef vector< MessageBuffer* > MessageBufferQueue;
51 typedef MessageBufferQueue::iterator MessageBufferIter;
53 typedef Dali::Mutex MessageQueueMutex;
55 } // unnamed namespace
61 * Private MessageQueue data
63 struct MessageQueue::Impl
65 Impl( RenderController& controller, const SceneGraphBuffers& buffers )
66 : renderController(controller),
67 sceneGraphBuffers(buffers),
68 processingEvents(false),
70 sceneUpdateFlag( false ),
72 currentMessageBuffer(NULL)
78 // Delete the current buffer
79 if( currentMessageBuffer )
81 DeleteBufferContents( currentMessageBuffer );
82 delete currentMessageBuffer;
85 // Delete the unprocessed buffers
86 const MessageBufferIter processQueueEndIter = processQueue.end();
87 for ( MessageBufferIter iter = processQueue.begin(); iter != processQueueEndIter; ++iter )
89 MessageBuffer* unprocessedBuffer = *iter;
90 DeleteBufferContents( unprocessedBuffer );
91 delete unprocessedBuffer;
94 // Delete the recycled buffers
95 const MessageBufferIter recycleQueueEndIter = recycleQueue.end();
96 for ( MessageBufferIter iter = recycleQueue.begin(); iter != recycleQueueEndIter; ++iter )
98 MessageBuffer* recycledBuffer = *iter;
99 DeleteBufferContents( recycledBuffer );
100 delete recycledBuffer;
103 const MessageBufferIter freeQueueEndIter = freeQueue.end();
104 for ( MessageBufferIter iter = freeQueue.begin(); iter != freeQueueEndIter; ++iter )
106 MessageBuffer* freeBuffer = *iter;
107 DeleteBufferContents( freeBuffer );
112 void DeleteBufferContents( MessageBuffer* buffer )
114 for( MessageBuffer::Iterator iter = buffer->Begin(); iter.IsValid(); iter.Next() )
116 MessageBase* message = reinterpret_cast< MessageBase* >( iter.Get() );
118 // Call virtual destructor explictly; since delete will not be called after placement new
119 message->~MessageBase();
123 RenderController& renderController; ///< render controller
124 const SceneGraphBuffers& sceneGraphBuffers; ///< Used to keep track of which buffers are being written or read.
126 bool processingEvents; ///< Whether messages queued will be flushed by core
127 bool queueWasEmpty; ///< Flag whether the queue was empty during the Update()
128 bool sceneUpdateFlag; ///< true when there is a new message that requires a scene-graph node tree update
129 int sceneUpdate; ///< Non zero when there is a message in the queue requiring a scene-graph node tree update
131 MessageQueueMutex queueMutex; ///< queueMutex must be locked whilst accessing processQueue or recycleQueue
132 MessageBufferQueue processQueue; ///< to process in the next update
133 MessageBufferQueue recycleQueue; ///< to recycle MessageBuffers after the messages have been processed
135 MessageBuffer* currentMessageBuffer; ///< can be used without locking
136 MessageBufferQueue freeQueue; ///< buffers from the recycleQueue; can be used without locking
139 MessageQueue::MessageQueue( Integration::RenderController& controller, const SceneGraph::SceneGraphBuffers& buffers )
142 mImpl = new Impl( controller, buffers );
145 MessageQueue::~MessageQueue()
150 void MessageQueue::EventProcessingStarted()
152 mImpl->processingEvents = true;
155 unsigned int* MessageQueue::ReserveMessageSlot( unsigned int requestedSize, bool updateScene )
157 DALI_ASSERT_DEBUG( 0 != requestedSize );
161 mImpl->sceneUpdateFlag = true;
164 if ( !mImpl->currentMessageBuffer )
166 const MessageBufferIter endIter = mImpl->freeQueue.end();
168 // Find the largest recycled buffer from freeQueue
169 MessageBufferIter nextBuffer = endIter;
170 for ( MessageBufferIter iter = mImpl->freeQueue.begin(); iter != endIter; ++iter )
172 if ( endIter == nextBuffer ||
173 (*nextBuffer)->GetCapacity() < (*iter)->GetCapacity() )
179 if ( endIter != nextBuffer )
181 // Reuse a recycled buffer from freeQueue
182 mImpl->currentMessageBuffer = *nextBuffer;
183 mImpl->freeQueue.erase( nextBuffer );
187 mImpl->currentMessageBuffer = new MessageBuffer( INITIAL_BUFFER_SIZE );
191 // If we are inside Core::ProcessEvents(), core will automatically flush the queue.
192 // If we are outside, then we have to request a call to Core::ProcessEvents() on idle.
193 if ( false == mImpl->processingEvents )
195 mImpl->renderController.RequestProcessEventsOnIdle();
198 return mImpl->currentMessageBuffer->ReserveMessageSlot( requestedSize );
201 bool MessageQueue::FlushQueue()
203 const bool messagesToProcess = ( NULL != mImpl->currentMessageBuffer );
205 // If there're messages to flush
206 if ( messagesToProcess )
208 // queueMutex must be locked whilst accessing processQueue or recycleQueue
209 MessageQueueMutex::ScopedLock lock( mImpl->queueMutex );
211 mImpl->processQueue.push_back( mImpl->currentMessageBuffer );
212 mImpl->currentMessageBuffer = NULL;
214 // Grab any recycled MessageBuffers
215 while ( !mImpl->recycleQueue.empty() )
217 MessageBuffer* recycled = mImpl->recycleQueue.back();
218 mImpl->recycleQueue.pop_back();
220 // Guard against excessive message buffer growth
221 if ( MAX_FREE_BUFFER_COUNT < mImpl->freeQueue.size() ||
222 MAX_BUFFER_CAPACITY < recycled->GetCapacity() )
228 mImpl->freeQueue.push_back( recycled );
232 if( mImpl->sceneUpdateFlag )
234 mImpl->sceneUpdate |= 2;
235 mImpl->sceneUpdateFlag = false;
239 mImpl->processingEvents = false;
241 return messagesToProcess;
244 void MessageQueue::ProcessMessages( BufferIndex updateBufferIndex )
246 PERF_MONITOR_START(PerformanceMonitor::PROCESS_MESSAGES);
248 // queueMutex must be locked whilst accessing queue
249 MessageQueueMutex::ScopedLock lock( mImpl->queueMutex );
251 const MessageBufferIter processQueueEndIter = mImpl->processQueue.end();
252 for ( MessageBufferIter iter = mImpl->processQueue.begin(); iter != processQueueEndIter ; ++iter )
254 MessageBuffer* buffer = *iter;
256 for( MessageBuffer::Iterator iter = buffer->Begin(); iter.IsValid(); iter.Next() )
258 MessageBase* message = reinterpret_cast< MessageBase* >( iter.Get() );
260 message->Process( updateBufferIndex );
262 // Call virtual destructor explictly; since delete will not be called after placement new
263 message->~MessageBase();
267 // Pass back for use in the event-thread
268 mImpl->recycleQueue.push_back( buffer );
271 mImpl->sceneUpdate >>= 1;
273 mImpl->queueWasEmpty = mImpl->processQueue.empty(); // Flag whether we processed anything
275 mImpl->processQueue.clear();
277 PERF_MONITOR_END(PerformanceMonitor::PROCESS_MESSAGES);
280 bool MessageQueue::WasEmpty() const
282 return mImpl->queueWasEmpty;
285 bool MessageQueue::IsSceneUpdateRequired() const
287 return mImpl->sceneUpdate;
290 } // namespace Update
292 } // namespace Internal