1 #ifndef __DALI_INTERNAL_COMBINED_UPDATE_RENDER_CONTROLLER_H__
2 #define __DALI_INTERNAL_COMBINED_UPDATE_RENDER_CONTROLLER_H__
5 * Copyright (c) 2015 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 <semaphore.h>
24 #include <dali/integration-api/core.h>
25 #include <dali/devel-api/threading/conditional-wait.h>
28 #include <integration-api/thread-synchronization-interface.h>
29 #include <base/interfaces/performance-interface.h>
30 #include <base/fps-tracker.h>
31 #include <base/render-helper.h>
32 #include <base/thread-controller-interface.h>
33 #include <base/update-status-logger.h>
39 class TriggerEventInterface;
47 class AdaptorInternalServices;
48 class EnvironmentOptions;
51 * @brief Two threads where events/application interaction is handled on the main/event thread and the Update & Render
52 * happen on the other thread.
56 * a. Main/Event Thread.
57 * b. Update/Render Thread.
58 * 2. There is NO VSync thread:
59 * a. We retrieve the time before Update.
60 * b. Then retrieve the time after Render.
61 * c. We calculate the difference between these two times and if:
62 * i. The difference is less than the default frame time, we sleep.
63 * ii. If it’s more or the same, we continue.
64 * 3. On the update/render thread, if we discover that we do not need to do any more updates, we use a trigger-event
65 * to inform the main/event thread. This is then processed as soon as the event thread is able to do so where it
66 * is easier to make a decision about whether we should stop the update/render thread or not (depending on any
67 * update requests etc.).
68 * 4. The main thread is blocked while the surface is being replaced.
69 * 5. When we resume from paused, elapsed time is used for the animations, i.e. the could have finished while we were paused.
70 * However, FinishedSignal emission will only happen upon resumption.
71 * 6. Elapsed time is NOT used while if we are waking up from a sleep state or doing an UpdateOnce.
73 class CombinedUpdateRenderController : public ThreadControllerInterface,
74 public ThreadSynchronizationInterface
81 CombinedUpdateRenderController( AdaptorInternalServices& adaptorInterfaces, const EnvironmentOptions& environmentOptions );
84 * Non virtual destructor. Not intended as base class.
86 ~CombinedUpdateRenderController();
89 * @copydoc ThreadControllerInterface::Initialize()
91 virtual void Initialize();
94 * @copydoc ThreadControllerInterface::Start()
99 * @copydoc ThreadControllerInterface::Pause()
101 virtual void Pause();
104 * @copydoc ThreadControllerInterface::Resume()
106 virtual void Resume();
109 * @copydoc ThreadControllerInterface::Stop()
114 * @copydoc ThreadControllerInterface::RequestUpdate()
116 virtual void RequestUpdate();
119 * @copydoc ThreadControllerInterface::RequestUpdateOnce()
121 virtual void RequestUpdateOnce();
124 * @copydoc ThreadControllerInterface::ReplaceSurface()
126 virtual void ReplaceSurface( RenderSurface* surface );
129 * @copydoc ThreadControllerInterface::SetRenderRefreshRate()
131 virtual void SetRenderRefreshRate( unsigned int numberOfFramesPerRender );
135 // Undefined copy constructor.
136 CombinedUpdateRenderController( const CombinedUpdateRenderController& );
138 // Undefined assignment operator.
139 CombinedUpdateRenderController& operator=( const CombinedUpdateRenderController& );
141 /////////////////////////////////////////////////////////////////////////////////////////////////
143 /////////////////////////////////////////////////////////////////////////////////////////////////
146 * Runs the Update/Render Thread.
147 * This will lock the mutex in mUpdateRenderThreadWaitCondition.
149 * @param[in] numberOfCycles The number of times the update/render cycle should run. If -1, then it will run continuously.
150 * @param[in] useElapsedTimeAfterWait If true, then the elapsed time during wait is used for animations, otherwise no animation progression is made.
152 inline void RunUpdateRenderThread( int numberOfCycles, bool useElapsedTimeAfterWait );
155 * Pauses the Update/Render Thread.
156 * This will lock the mutex in mUpdateRenderThreadWaitCondition.
158 inline void PauseUpdateRenderThread();
161 * Stops the Update/Render Thread.
162 * This will lock the mutex in mUpdateRenderThreadWaitCondition.
164 * @note Should only be called in Stop as calling this will kill the update-thread.
166 inline void StopUpdateRenderThread();
169 * Checks if the the Update/Render Thread is paused.
170 * This will lock the mutex in mUpdateRenderThreadWaitCondition.
172 * @return true if paused, false otherwise
174 inline bool IsUpdateRenderThreadPaused();
177 * Used as the callback for the sleep-trigger.
179 * Will sleep when enough requests are made without any requests.
181 void ProcessSleepRequest();
183 /////////////////////////////////////////////////////////////////////////////////////////////////
184 // UpdateRenderThread
185 /////////////////////////////////////////////////////////////////////////////////////////////////
188 * The Update/Render thread loop. This thread will be destroyed on exit from this function.
190 void UpdateRenderThread();
193 * Called by the Update/Render Thread which ensures a wait if required.
195 * @param[out] useElapsedTime If true when returned, then the actual elapsed time will be used for animation.
196 * If false when returned, then there should NOT be any animation progression in the next Update.
197 * @param[in] updateRequired Whether another update is required.
198 * @return false, if the thread should stop.
200 bool UpdateRenderReady( bool& useElapsedTime, bool updateRequired );
203 * Checks to see if the surface needs to be replaced.
204 * This will lock the mutex in mUpdateRenderThreadWaitCondition.
206 * @return Pointer to the new surface, NULL otherwise
208 RenderSurface* ShouldSurfaceBeReplaced();
211 * Called by the Update/Render thread after a surface has been replaced.
213 * This will lock the mutex in mEventThreadWaitCondition
215 void SurfaceReplaced();
218 * Helper for the thread calling the entry function
219 * @param[in] This A pointer to the current object
221 static void* InternalUpdateRenderThreadEntryFunc( void* This )
223 ( static_cast<CombinedUpdateRenderController*>( This ) )->UpdateRenderThread();
227 /////////////////////////////////////////////////////////////////////////////////////////////////
229 /////////////////////////////////////////////////////////////////////////////////////////////////
232 * Called by the update-render & v-sync threads when they up and running.
234 * This will lock the mutex in mEventThreadWaitCondition.
236 void NotifyThreadInitialised();
239 * Helper to add a performance marker to the performance server (if it's active)
240 * @param[in] type performance marker type
242 void AddPerformanceMarker( PerformanceInterface::MarkerType type );
244 /////////////////////////////////////////////////////////////////////////////////////////////////
245 // POST RENDERING - ThreadSynchronizationInterface overrides
246 /////////////////////////////////////////////////////////////////////////////////////////////////
248 /////////////////////////////////////////////////////////////////////////////////////////////////
249 //// Called by the Event Thread if post-rendering is required
250 /////////////////////////////////////////////////////////////////////////////////////////////////
253 * @copydoc ThreadSynchronizationInterface::PostRenderComplete()
255 virtual void PostRenderComplete();
257 /////////////////////////////////////////////////////////////////////////////////////////////////
258 //// Called by the Render Thread if post-rendering is required
259 /////////////////////////////////////////////////////////////////////////////////////////////////
262 * @copydoc ThreadSynchronizationInterface::PostRenderStarted()
264 virtual void PostRenderStarted();
267 * @copydoc ThreadSynchronizationInterface::PostRenderStarted()
269 virtual void PostRenderWaitForCompletion();
273 FpsTracker mFpsTracker; ///< Object that tracks the FPS
274 UpdateStatusLogger mUpdateStatusLogger; ///< Object that logs the update-status as required.
276 RenderHelper mRenderHelper; ///< Helper class for EGL, pre & post rendering
278 sem_t mEventThreadSemaphore; ///< Used by the event thread to ensure all threads have been initialised, and when replacing the surface.
280 ConditionalWait mUpdateRenderThreadWaitCondition; ///< The wait condition for the update-render-thread.
282 AdaptorInternalServices& mAdaptorInterfaces; ///< The adaptor internal interface
283 PerformanceInterface* mPerformanceInterface; ///< The performance logging interface
284 Integration::Core& mCore; ///< Dali core reference
285 const EnvironmentOptions& mEnvironmentOptions; ///< Environment options
286 TriggerEventInterface& mNotificationTrigger; ///< Reference to notification event trigger
287 TriggerEventInterface* mSleepTrigger; ///< Used by the update-render thread to trigger the event thread when it no longer needs to do any updates
289 pthread_t* mUpdateRenderThread; ///< The Update/Render thread.
291 float mDefaultFrameDelta; ///< Default time delta between each frame (used for animations). Not protected by lock, but written to rarely so not worth adding a lock when reading.
292 uint64_t mDefaultFrameDurationMilliseconds; ///< Default duration of a frame (used for predicting the time of the next frame). Not protected by lock, but written to rarely so not worth adding a lock when reading.
293 uint64_t mDefaultFrameDurationNanoseconds; ///< Default duration of a frame (used for sleeping if not enough time elapsed). Not protected by lock, but written to rarely so not worth adding a lock when reading.
294 uint64_t mDefaultHalfFrameNanoseconds; ///< Is half of mDefaultFrameDurationNanoseconds. Using a member variable avoids having to do the calculation every frame. Not protected by lock, but written to rarely so not worth adding a lock when reading.
296 unsigned int mUpdateRequestCount; ///< Count of update-requests we have received to ensure we do not go to sleep too early.
297 unsigned int mRunning; ///< Read and set on the event-thread only to state whether we are running.
300 // NOTE: cannot use booleans as these are used from multiple threads, must use variable with machine word size for atomic read/write
303 volatile int mUpdateRenderRunCount; ///< The number of times Update/Render cycle should run. If -1, then will run continuously (set by the event-thread, read by v-sync-thread).
304 volatile unsigned int mDestroyUpdateRenderThread; ///< Whether the Update/Render thread be destroyed (set by the event-thread, read by the update-render-thread).
305 volatile unsigned int mUpdateRenderThreadCanSleep; ///< Whether the Update/Render thread can sleep (set by the event-thread, read by the update-render-thread).
306 volatile unsigned int mPendingRequestUpdate; ///< Is set as soon as an RequestUpdate is made and unset when the next update happens (set by the event-thread and update-render thread, read by the update-render-thread).
307 ///< Ensures we do not go to sleep if we have not processed the most recent update-request.
309 volatile unsigned int mUseElapsedTimeAfterWait; ///< Whether we should use the elapsed time after waiting (set by the event-thread, read by the update-render-thread).
311 RenderSurface* volatile mNewSurface; ///< Will be set to the new-surface if requested (set by the event-thread, read & cleared by the update-render thread).
313 volatile unsigned int mPostRendering; ///< Whether post-rendering is taking place (set by the event & render threads, read by the render-thread).
316 } // namespace Adaptor
318 } // namespace Internal
322 #endif // __DALI_INTERNAL_COMBINED_UPDATE_RENDER_CONTROLLER_H__