1 #ifndef __DALI_INTERNAL_UPDATE_RENDER_SYNCHRONIZATION_H__
2 #define __DALI_INTERNAL_UPDATE_RENDER_SYNCHRONIZATION_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <base/interfaces/performance-interface.h>
26 #include <boost/thread.hpp>
27 #include <boost/thread/condition_variable.hpp>
36 class PlatformAbstraction;
38 } // namespace Integration
46 class AdaptorInternalServices;
49 * This object is used to synchronize the update, render and vsync threads.
50 * The Core::GetMaximumUpdateCount() method determines how many frames may be prepared, ahead of the rendering.
51 * For example if the maximum update count is 2, then Core::Update() for frame N+1 may be processed whilst frame N is being rendered.
52 * However the Core::Update() for frame N+2 may not be called, until the Core::Render() method for frame N has returned.
54 class UpdateRenderSynchronization
59 * Create an update/render synchronization object.
60 * @param[in] adaptorInterfaces base adaptor interface
62 UpdateRenderSynchronization( AdaptorInternalServices& adaptorInterfaces );
65 * Non virtual destructor. Not inteded as base class.
67 ~UpdateRenderSynchronization();
80 * Pause the controller (and threads)
85 * Resume the controller (and threads)
90 * Wake update thread if sleeping. If the update thread is not sleeping
91 * this becomes a noop.
92 * Called when an update is requested by Core.
93 * i.e. when a batch of messages have been queued for the next update.
95 void UpdateRequested();
98 * Update once (even if paused)
100 void UpdateWhilePaused();
103 * Called by Update thread before it runs the update. This is the point where we can pause
105 void UpdateReadyToRun();
108 * Called after an update has completed, to inform render-thread a buffer is ready to render.
109 * The function also waits for a free buffer to become available before returning.
110 * @pre Called by update thread only.
111 * @param[out] renderNeedsUpdate Whether the render task requires another update.
112 * @return True if updating should continue, false if the update-thread should quit.
114 bool UpdateSyncWithRender( bool& renderNeedsUpdate );
117 * Called by update thread to wait for all rendering to finish.
118 * Used by update to check the status of the final render before pausing.
119 * @pre Called by update thread only.
121 void UpdateWaitForAllRenderingToFinish();
124 * Try block the update-thread when there's nothing to update.
125 * @return True if updating should continue, false if the update-thread should quit.
127 bool UpdateTryToSleep();
130 * Called by the render thread after it renders a frame.
131 * Used to notify the update-thread that a frame has been rendered.
132 * @pre Called by render thread only.
133 * @param updateRequired Whether a further update is required.
135 void RenderFinished( bool updateRequired );
138 * Called by the render-thread to wait for a buffer to read from and then render.
139 * @pre Called by render thread only.
140 * @return True if rendering should continue, false if the render-thread should quit.
142 bool RenderSyncWithUpdate();
145 * Called by the render/update threads to wait for a VSync.
150 * Called by the VSync notifier thread so it can sleep if Update/Render threads are sleeping/paused
151 * @return true if VSync monitoring/notifications should continue.
153 bool VSyncNotifierSyncWithUpdateAndRender( unsigned int frameNumber, unsigned int seconds, unsigned int microseconds );
156 * Retrieves the last VSync frame number
157 * @return The VSync frame number.
159 unsigned int GetFrameNumber() const;
162 * Retrieves the time (in microseconds) at the last VSync
163 * @return The VSync timestamp in microseconds.
165 uint64_t GetTimeMicroseconds();
169 // Undefined copy constructor.
170 UpdateRenderSynchronization( const UpdateRenderSynchronization& );
172 // Undefined assignment operator.
173 UpdateRenderSynchronization& operator=( const UpdateRenderSynchronization& );
176 * Helper to add a performance marker to the performance server (if its active)
177 * @param type performance marker type
179 void AddPerformanceMarker( PerformanceMarker::MarkerType type );
183 const unsigned int mMaximumUpdateCount; ///< How many frames may be prepared, ahead of the rendering.
184 volatile unsigned int mUpdateReadyCount; ///< Incremented after each update, decremented after each render (protected by mMutex)
185 // ARM CPUs perform aligned 32 bit read/writes atomically, so the following variables do not require mutex protection on modification
186 volatile int mRunning; ///< Used during UpdateThread::Stop() to exit the update & render loops
187 volatile int mUpdateRequired; ///< Used to inform the update thread, that render requires another update
188 volatile int mPaused; ///< The paused flag
189 volatile int mUpdateRequested; ///< An update has been requested
190 volatile int mAllowUpdateWhilePaused; ///< whether to allow (one) update while paused
191 volatile int mVSyncSleep; ///< Set true when the VSync thread should sleep
192 volatile unsigned int mVSyncFrameNumber; ///< Frame number of latest VSync
193 volatile unsigned int mVSyncSeconds; ///< Timestamp (seconds) of latest VSync
194 volatile unsigned int mVSyncMicroseconds; ///< Timestamp (microseconds) of latest VSync
196 boost::mutex mMutex; ///< This mutex must be locked before reading/writing mUpdateReadyCount
197 boost::condition_variable mUpdateFinishedCondition; ///< The render thread waits for this condition
198 boost::condition_variable mUpdateSleepCondition; ///< The update thread waits for this condition when sleeping
199 boost::condition_variable mRenderFinishedCondition; ///< The update thread waits for this condition
200 boost::condition_variable mVSyncReceivedCondition; ///< The render thread waits on this condition
201 boost::condition_variable mVSyncSleepCondition; ///< The vsync thread waits for this condition
202 boost::condition_variable mPausedCondition; ///< The controller waits for this condition while paused
204 Dali::Integration::Core& mCore; ///< Dali core reference
205 PerformanceInterface* mPerformanceInterface;///< The performance logging interface
207 }; // class UpdateRenderSynchronization
209 } // namespace Adaptor
211 } // namespace Internal
215 #endif // __DALI_INTERNAL_UPDATE_RENDER_SYNCHRONIZATION_H__