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>
25 #include <boost/thread.hpp>
26 #include <boost/thread/condition_variable.hpp>
35 class PlatformAbstraction;
37 } // namespace Integration
45 class AdaptorInternalServices;
48 * This object is used to synchronize the update, render and vsync threads.
49 * The Core::GetMaximumUpdateCount() method determines how many frames may be prepared, ahead of the rendering.
50 * 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.
51 * However the Core::Update() for frame N+2 may not be called, until the Core::Render() method for frame N has returned.
53 class UpdateRenderSynchronization
58 * Create an update/render synchronization object.
59 * @param[in] adaptorInterfaces base adaptor interface
61 UpdateRenderSynchronization( AdaptorInternalServices& adaptorInterfaces );
64 * Non virtual destructor. Not inteded as base class.
66 ~UpdateRenderSynchronization();
79 * Pause the controller (and threads)
84 * Resume the controller (and threads)
89 * Wake update thread if sleeping. If the update thread is not sleeping
90 * this becomes a noop.
91 * Called when an update is requested by Core.
92 * i.e. when a batch of messages have been queued for the next update.
94 void UpdateRequested();
97 * Update once (even if paused)
99 void UpdateWhilePaused();
102 * Called by Update thread before it runs the update. This is the point where we can pause
104 void UpdateReadyToRun();
107 * Called after an update has completed, to inform render-thread a buffer is ready to render.
108 * The function also waits for a free buffer to become available before returning.
109 * @pre Called by update thread only.
110 * @param[out] renderNeedsUpdate Whether the render task requires another update.
111 * @return True if updating should continue, false if the update-thread should quit.
113 bool UpdateSyncWithRender( bool& renderNeedsUpdate );
116 * Called by update thread to wait for all rendering to finish.
117 * Used by update to check the status of the final render before pausing.
118 * @pre Called by update thread only.
120 void UpdateWaitForAllRenderingToFinish();
123 * Try block the update-thread when there's nothing to update.
124 * @return True if updating should continue, false if the update-thread should quit.
126 bool UpdateTryToSleep();
129 * Called by the render thread after it renders a frame.
130 * Used to notify the update-thread that a frame has been rendered.
131 * @pre Called by render thread only.
132 * @param updateRequired Whether a further update is required.
134 void RenderFinished( bool updateRequired );
137 * Called by the render-thread to wait for a buffer to read from and then render.
138 * @pre Called by render thread only.
139 * @return True if rendering should continue, false if the render-thread should quit.
141 bool RenderSyncWithUpdate();
144 * Called by the render/update threads to wait for a VSync.
149 * Called by the VSync notifier thread so it can sleep if Update/Render threads are sleeping/paused
150 * @return true if VSync monitoring/notifications should continue.
152 bool VSyncNotifierSyncWithUpdateAndRender( unsigned int frameNumber, unsigned int seconds, unsigned int microseconds );
155 * Retrieves the last VSync frame number
156 * @return The VSync frame number.
158 unsigned int GetFrameNumber() const;
161 * Retrieves the time (in microseconds) at the last VSync
162 * @return The VSync timestamp in microseconds.
164 uint64_t GetTimeMicroseconds();
168 // Undefined copy constructor.
169 UpdateRenderSynchronization( const UpdateRenderSynchronization& );
171 // Undefined assignment operator.
172 UpdateRenderSynchronization& operator=( const UpdateRenderSynchronization& );
175 * Helper to add a performance marker to the performance server (if its active)
176 * @param type performance marker type
178 void AddPerformanceMarker( PerformanceMarker::MarkerType type );
182 const unsigned int mMaximumUpdateCount; ///< How many frames may be prepared, ahead of the rendering.
183 volatile unsigned int mUpdateReadyCount; ///< Incremented after each update, decremented after each render (protected by mMutex)
184 // ARM CPUs perform aligned 32 bit read/writes atomically, so the following variables do not require mutex protection on modification
185 volatile int mRunning; ///< Used during UpdateThread::Stop() to exit the update & render loops
186 volatile int mUpdateRequired; ///< Used to inform the update thread, that render requires another update
187 volatile int mPaused; ///< The paused flag
188 volatile int mUpdateRequested; ///< An update has been requested
189 volatile int mAllowUpdateWhilePaused; ///< whether to allow (one) update while paused
190 volatile int mVSyncSleep; ///< Set true when the VSync thread should sleep
191 volatile unsigned int mVSyncFrameNumber; ///< Frame number of latest VSync
192 volatile unsigned int mVSyncSeconds; ///< Timestamp (seconds) of latest VSync
193 volatile unsigned int mVSyncMicroseconds; ///< Timestamp (microseconds) of latest VSync
195 boost::mutex mMutex; ///< This mutex must be locked before reading/writing mUpdateReadyCount
196 boost::condition_variable mUpdateFinishedCondition; ///< The render thread waits for this condition
197 boost::condition_variable mUpdateSleepCondition; ///< The update thread waits for this condition when sleeping
198 boost::condition_variable mRenderFinishedCondition; ///< The update thread waits for this condition
199 boost::condition_variable mVSyncReceivedCondition; ///< The render thread waits on this condition
200 boost::condition_variable mVSyncSleepCondition; ///< The vsync thread waits for this condition
201 boost::condition_variable mPausedCondition; ///< The controller waits for this condition while paused
203 Dali::Integration::Core& mCore; ///< Dali core reference
204 PerformanceInterface* mPerformanceInterface;///< The performance logging interface
206 }; // class UpdateRenderSynchronization
208 } // namespace Adaptor
210 } // namespace Internal
214 #endif // __DALI_INTERNAL_UPDATE_RENDER_SYNCHRONIZATION_H__