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/event/common/notification-manager.h>
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/public-api/common/mutex.h>
24 #include <dali/internal/common/owner-container.h>
25 #include <dali/internal/common/message.h>
26 #include <dali/internal/event/common/property-notification-impl.h>
27 #include <dali/internal/event/common/complete-notification-interface.h>
37 typedef Dali::Vector< CompleteNotificationInterface* > InterfaceContainer;
40 * helper to move elements from one container to another
41 * @param from where to move
42 * @param to move target
44 void MoveElements( InterfaceContainer& from, InterfaceContainer& to )
46 // check if there's something in from
47 const InterfaceContainer::SizeType fromCount = from.Count();
50 // check if to has some elements
51 const InterfaceContainer::SizeType toCount = to.Count();
54 // to is empty so we can swap with from
59 to.Reserve( toCount + fromCount );
60 for( InterfaceContainer::SizeType i = 0; i < fromCount; ++i )
62 to.PushBack( from[ i ] );
70 typedef Dali::Mutex MessageQueueMutex;
71 typedef OwnerContainer< MessageBase* > MessageContainer;
73 struct NotificationManager::Impl
77 // reserve space on the vectors to avoid reallocs
78 // applications typically have up-to 20-30 notifications at startup
79 updateCompletedMessageQueue.Reserve( 32 );
80 updateWorkingMessageQueue.Reserve( 32 );
81 eventMessageQueue.Reserve( 32 );
83 // only a few manager objects get complete notifications (animation, render list, property notifications, ...)
84 updateCompletedInterfaceQueue.Reserve( 4 );
85 updateWorkingInterfaceQueue.Reserve( 4 );
86 eventInterfaceQueue.Reserve( 4 );
93 // queueMutex must be locked whilst accessing queue
94 MessageQueueMutex queueMutex;
95 // three queues for objects owned by notification manager
96 MessageContainer updateCompletedMessageQueue;
97 MessageContainer updateWorkingMessageQueue;
98 MessageContainer eventMessageQueue;
99 // three queues for objects referenced by notification manager
100 InterfaceContainer updateCompletedInterfaceQueue;
101 InterfaceContainer updateWorkingInterfaceQueue;
102 InterfaceContainer eventInterfaceQueue;
105 NotificationManager::NotificationManager()
110 NotificationManager::~NotificationManager()
115 void NotificationManager::QueueCompleteNotification( CompleteNotificationInterface* instance )
117 // queueMutex must be locked whilst accessing queues
118 MessageQueueMutex::ScopedLock lock( mImpl->queueMutex );
120 mImpl->updateWorkingInterfaceQueue.PushBack( instance );
123 void NotificationManager::QueueMessage( MessageBase* message )
125 DALI_ASSERT_DEBUG( NULL != message );
127 // queueMutex must be locked whilst accessing queues
128 MessageQueueMutex::ScopedLock lock( mImpl->queueMutex );
130 mImpl->updateWorkingMessageQueue.PushBack( message );
133 void NotificationManager::UpdateCompleted()
135 // queueMutex must be locked whilst accessing queues
136 MessageQueueMutex::ScopedLock lock( mImpl->queueMutex );
137 // Move messages from update working queue to completed queue
138 // note that in theory its possible for update completed to have last frames
139 // events as well still hanging around. we need to keep them as well
140 mImpl->updateCompletedMessageQueue.MoveFrom( mImpl->updateWorkingMessageQueue );
141 // move pointers from interface queue
142 MoveElements( mImpl->updateWorkingInterfaceQueue, mImpl->updateCompletedInterfaceQueue );
143 // finally the lock is released
146 bool NotificationManager::MessagesToProcess()
148 // queueMutex must be locked whilst accessing queues
149 MessageQueueMutex::ScopedLock lock( mImpl->queueMutex );
151 return ( 0u < mImpl->updateCompletedMessageQueue.Count() ||
152 ( 0u < mImpl->updateCompletedInterfaceQueue.Count() ) );
155 void NotificationManager::ProcessMessages()
157 // queueMutex must be locked whilst accessing queues
159 MessageQueueMutex::ScopedLock lock( mImpl->queueMutex );
161 // Move messages from update completed queue to event queue
162 // note that in theory its possible for event queue to have
163 // last frames events as well still hanging around so need to keep them
164 mImpl->eventMessageQueue.MoveFrom( mImpl->updateCompletedMessageQueue );
165 MoveElements( mImpl->updateCompletedInterfaceQueue, mImpl->eventInterfaceQueue );
167 // end of scope, lock is released
169 MessageContainer::Iterator iter = mImpl->eventMessageQueue.Begin();
170 const MessageContainer::Iterator end = mImpl->eventMessageQueue.End();
171 for( ; iter != end; ++iter )
173 (*iter)->Process( 0u/*ignored*/ );
175 // release the processed messages from event side queue
176 mImpl->eventMessageQueue.Clear();
178 InterfaceContainer::Iterator iter2 = mImpl->eventInterfaceQueue.Begin();
179 const InterfaceContainer::Iterator end2 = mImpl->eventInterfaceQueue.End();
180 for( ; iter2 != end2; ++iter2 )
182 CompleteNotificationInterface* interface = *iter2;
185 interface->NotifyCompleted();
188 // just clear the container, we dont own the objects
189 mImpl->eventInterfaceQueue.Clear();
192 } // namespace Internal