1 #ifndef DALI_INTERNAL_COMBINED_UPDATE_RENDER_CONTROLLER_H
2 #define DALI_INTERNAL_COMBINED_UPDATE_RENDER_CONTROLLER_H
5 * Copyright (c) 2024 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 <dali/devel-api/threading/conditional-wait.h>
23 #include <dali/devel-api/threading/semaphore.h>
24 #include <dali/integration-api/core.h>
26 #include <semaphore.h>
31 #include <dali/devel-api/adaptor-framework/texture-upload-manager.h>
32 #include <dali/integration-api/adaptor-framework/thread-synchronization-interface.h>
33 #include <dali/internal/adaptor/common/thread-controller-interface.h>
34 #include <dali/internal/system/common/fps-tracker.h>
35 #include <dali/internal/system/common/performance-interface.h>
36 #include <dali/internal/system/common/update-status-logger.h>
37 #include <dali/internal/window-system/common/display-connection.h>
41 class RenderSurfaceInterface;
42 class TriggerEventInterface;
48 class AdaptorInternalServices;
49 class EnvironmentOptions;
52 * @brief Two threads where events/application interaction is handled on the main/event thread and the Update & Render
53 * happen on the other thread.
57 * a. Main/Event Thread.
58 * b. Update/Render Thread.
59 * 2. There is NO VSync thread:
60 * a. We retrieve the time before Update.
61 * b. Then retrieve the time after Render.
62 * c. We calculate the difference between these two times and if:
63 * i. The difference is less than the default frame time, we sleep.
64 * ii. If it’s more or the same, we continue.
65 * 3. On the update/render thread, if we discover that we do not need to do any more updates, we use a trigger-event
66 * to inform the main/event thread. This is then processed as soon as the event thread is able to do so where it
67 * is easier to make a decision about whether we should stop the update/render thread or not (depending on any
68 * update requests etc.).
69 * 4. The main thread is blocked while the surface is being replaced.
70 * 5. When we resume from paused, elapsed time is used for the animations, i.e. the could have finished while we were paused.
71 * However, FinishedSignal emission will only happen upon resumption.
72 * 6. Elapsed time is NOT used while if we are waking up from a sleep state or doing an UpdateOnce.
74 class CombinedUpdateRenderController : public ThreadControllerInterface,
75 public ThreadSynchronizationInterface
81 CombinedUpdateRenderController(AdaptorInternalServices& adaptorInterfaces, const EnvironmentOptions& environmentOptions, ThreadMode threadMode);
84 * Non virtual destructor. Not intended as base class.
86 ~CombinedUpdateRenderController();
89 * @copydoc ThreadControllerInterface::Initialize()
91 void Initialize() override;
94 * @copydoc ThreadControllerInterface::Start()
96 void Start() override;
99 * @copydoc ThreadControllerInterface::Pause()
101 void Pause() override;
104 * @copydoc ThreadControllerInterface::Resume()
106 void Resume() override;
109 * @copydoc ThreadControllerInterface::Stop()
111 void Stop() override;
114 * @copydoc ThreadControllerInterface::RequestUpdate()
116 void RequestUpdate() override;
119 * @copydoc ThreadControllerInterface::RequestUpdateOnce()
121 void RequestUpdateOnce(UpdateMode updateMode) override;
124 * @copydoc ThreadControllerInterface::ReplaceSurface()
126 void ReplaceSurface(Dali::RenderSurfaceInterface* surface) override;
129 * @copydoc ThreadControllerInterface::DeleteSurface()
131 void DeleteSurface(Dali::RenderSurfaceInterface* surface) override;
134 * @copydoc ThreadControllerInterface::ResizeSurface()
136 void ResizeSurface() override;
139 * @copydoc ThreadControllerInterface::WaitForGraphicsInitialization()
141 void WaitForGraphicsInitialization() override;
144 * @copydoc ThreadControllerInterface::SetRenderRefreshRate()
146 void SetRenderRefreshRate(unsigned int numberOfFramesPerRender) override;
149 * @copydoc ThreadControllerInterface::SetPreRenderCallback
151 void SetPreRenderCallback(CallbackBase* callback) override;
154 * @copydoc ThreadControllerInterface::AddSurface()
156 void AddSurface(Dali::RenderSurfaceInterface* surface) override;
159 * @copydoc ThreadControllerInterface::GetThreadId()
161 int32_t GetThreadId() const override;
164 // Undefined copy constructor.
165 CombinedUpdateRenderController(const CombinedUpdateRenderController&);
167 // Undefined assignment operator.
168 CombinedUpdateRenderController& operator=(const CombinedUpdateRenderController&);
170 /////////////////////////////////////////////////////////////////////////////////////////////////
172 /////////////////////////////////////////////////////////////////////////////////////////////////
174 enum AnimationProgression
176 USE_ELAPSED_TIME, ///< Animation progression using elapsed time
177 NONE ///< No animation progression
181 * Runs the Update/Render Thread.
182 * This will lock the mutex in mUpdateRenderThreadWaitCondition.
184 * @param[in] numberOfCycles The number of times the update/render cycle should run. If -1, then it will run continuously.
185 * @param[in] animationProgression Whether to progress animation using time elapsed since the last frame.
186 * @param[in] updateMode The update mode (i.e. either update & render or skip rendering)
188 inline void RunUpdateRenderThread(int numberOfCycles, AnimationProgression animationProgression, UpdateMode updateMode);
191 * Pauses the Update/Render Thread.
192 * This will lock the mutex in mUpdateRenderThreadWaitCondition.
194 inline void PauseUpdateRenderThread();
197 * Stops the Update/Render Thread.
198 * This will lock the mutex in mUpdateRenderThreadWaitCondition.
200 * @note Should only be called in Stop as calling this will kill the update-thread.
202 inline void StopUpdateRenderThread();
205 * Checks if the the Update/Render Thread is paused.
206 * This will lock the mutex in mUpdateRenderThreadWaitCondition.
208 * @return true if paused, false otherwise
210 inline bool IsUpdateRenderThreadPaused();
213 * Used as the callback for the sleep-trigger.
215 * Will sleep when enough requests are made without any requests.
217 void ProcessSleepRequest();
219 /////////////////////////////////////////////////////////////////////////////////////////////////
220 // UpdateRenderThread
221 /////////////////////////////////////////////////////////////////////////////////////////////////
224 * The Update/Render thread loop. This thread will be destroyed on exit from this function.
226 void UpdateRenderThread();
229 * Called by the Update/Render Thread which ensures a wait if required.
231 * @param[out] useElapsedTime If true when returned, then the actual elapsed time will be used for animation.
232 * If false when returned, then there should NOT be any animation progression in the next Update.
233 * @param[in] updateRequired Whether another update is required.
234 * @param[out] timeToSleepUntil The time remaining in nanoseconds to keep the thread sleeping before resuming.
235 * @return false, if the thread should stop.
237 bool UpdateRenderReady(bool& useElapsedTime, bool updateRequired, uint64_t& timeToSleepUntil);
240 * Checks to see if the surface needs to be replaced.
241 * This will lock the mutex in mUpdateRenderThreadWaitCondition.
243 * @return Pointer to the new surface, NULL otherwise
245 Dali::RenderSurfaceInterface* ShouldSurfaceBeReplaced();
248 * Called by the Update/Render thread after a surface has been replaced.
250 * This will lock the mutex in mEventThreadWaitCondition
252 void SurfaceReplaced();
255 * Checks to see if the surface needs to be deleted.
256 * This will lock the mutex in mUpdateRenderThreadWaitCondition.
258 * @return Pointer to the deleted surface, nullptr otherwise
260 Dali::RenderSurfaceInterface* ShouldSurfaceBeDeleted();
263 * Called by the Update/Render thread after a surface has been deleted.
265 * This will lock the mutex in mEventThreadWaitCondition
267 void SurfaceDeleted();
270 * Called by the Update/Render thread after a surface has been resized.
272 * This will lock the mutex in mEventThreadWaitCondition
273 * @param[in] resizedCount The number of resized count for given surface.
275 void SurfaceResized(uint32_t resizedCount);
278 * PreCompile shaders for launching time
280 * @param[in] vertexShader vertexShader need to precompile
281 * @param[in] fragmentShader fragmentShader need to precompile
282 * @param[in] shaderName the name of precompile shader (option)
284 void PreCompileShader(std::string vertexShader, std::string fragmentShader, std::string shaderName = "");
287 * Cancel the precompile
289 void CancelPreCompile();
292 * Helper for the thread calling the entry function
293 * @param[in] This A pointer to the current object
295 static void* InternalUpdateRenderThreadEntryFunc(void* This)
297 (static_cast<CombinedUpdateRenderController*>(This))->UpdateRenderThread();
301 /////////////////////////////////////////////////////////////////////////////////////////////////
303 /////////////////////////////////////////////////////////////////////////////////////////////////
306 * Called by the update-render & v-sync threads when they up and running.
308 * This will lock the mutex in mEventThreadWaitCondition.
310 void NotifyThreadInitialised();
313 * Called by the update-render thread when graphics has been initialised.
315 void NotifyGraphicsInitialised();
318 * Helper to add a performance marker to the performance server (if it's active)
319 * @param[in] type performance marker type
321 void AddPerformanceMarker(PerformanceInterface::MarkerType type);
323 /////////////////////////////////////////////////////////////////////////////////////////////////
324 // POST RENDERING - ThreadSynchronizationInterface overrides
325 /////////////////////////////////////////////////////////////////////////////////////////////////
327 /////////////////////////////////////////////////////////////////////////////////////////////////
328 //// Called by the Event Thread if post-rendering is required
329 /////////////////////////////////////////////////////////////////////////////////////////////////
332 * @copydoc ThreadSynchronizationInterface::PostRenderComplete()
334 void PostRenderComplete() override;
336 /////////////////////////////////////////////////////////////////////////////////////////////////
337 //// Called by the Render Thread if post-rendering is required
338 /////////////////////////////////////////////////////////////////////////////////////////////////
341 * @copydoc ThreadSynchronizationInterface::PostRenderStarted()
343 void PostRenderStarted() override;
346 * @copydoc ThreadSynchronizationInterface::PostRenderStarted()
348 void PostRenderWaitForCompletion() override;
351 FpsTracker mFpsTracker; ///< Object that tracks the FPS
352 UpdateStatusLogger mUpdateStatusLogger; ///< Object that logs the update-status as required.
354 Semaphore<> mEventThreadSemaphore; ///< Used by the event thread to ensure all threads have been initialised, and when replacing the surface.
355 ConditionalWait mGraphicsInitializeWait; ///< Used by the render thread to ensure the graphics has been initialised.
356 Semaphore<> mSurfaceSemaphore; ///< Used by the event thread to ensure the surface has been deleted or replaced.
358 ConditionalWait mUpdateRenderThreadWaitCondition; ///< The wait condition for the update-render-thread.
360 AdaptorInternalServices& mAdaptorInterfaces; ///< The adaptor internal interface
361 PerformanceInterface* mPerformanceInterface; ///< The performance logging interface
362 Integration::Core& mCore; ///< Dali core reference
363 const EnvironmentOptions& mEnvironmentOptions; ///< Environment options
364 TriggerEventInterface& mNotificationTrigger; ///< Reference to notification event trigger
365 TriggerEventInterface* mSleepTrigger; ///< Used by the update-render thread to trigger the event thread when it no longer needs to do any updates
366 CallbackBase* mPreRenderCallback; ///< Used by Update/Render thread when PreRender is about to be called on graphics.
368 Dali::Devel::TextureUploadManager& mTextureUploadManager; ///< TextureUploadManager
370 pthread_t* mUpdateRenderThread; ///< The Update/Render thread.
372 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.
373 // TODO: mDefaultFrameDurationMilliseconds is defined as uint64_t, the only place where it is used, it is converted to an unsigned int!!!
374 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.
375 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.
376 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.
378 uint32_t mUpdateRequestCount; ///< Count of update-requests we have received to ensure we do not go to sleep too early.
379 uint32_t mRunning; ///< Read and set on the event-thread only to state whether we are running.
380 uint32_t mVsyncRender; ///< Whether vsync render required or not.
381 int32_t mThreadId; ///< UpdateRender thread id
383 ThreadMode mThreadMode; ///< Whether the thread runs continuously or runs when it is requested.
386 // NOTE: cannot use booleans as these are used from multiple threads, must use variable with machine word size for atomic read/write
389 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).
390 volatile unsigned int mDestroyUpdateRenderThread; ///< Whether the Update/Render thread be destroyed (set by the event-thread, read by the update-render-thread).
391 volatile unsigned int mUpdateRenderThreadCanSleep; ///< Whether the Update/Render thread can sleep (set by the event-thread, read by the update-render-thread).
392 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).
393 ///< Ensures we do not go to sleep if we have not processed the most recent update-request.
395 volatile unsigned int mUseElapsedTimeAfterWait; ///< Whether we should use the elapsed time after waiting (set by the event-thread, read by the update-render-thread).
396 volatile unsigned int mIsPreCompileCancelled; ///< Whether we need to do precompile shader.
398 Dali::RenderSurfaceInterface* volatile mNewSurface; ///< Will be set to the new-surface if requested (set by the event-thread, read & cleared by the update-render thread).
399 Dali::RenderSurfaceInterface* volatile mDeletedSurface; ///< Will be set to the deleted surface if requested (set by the event-thread, read & cleared by the update-render thread).
401 volatile unsigned int mPostRendering; ///< Whether post-rendering is taking place (set by the event & render threads, read by the render-thread).
402 volatile unsigned int mSurfaceResized; ///< Will be set to resize the surface (set by the event-thread, read & cleared by the update-render thread).
403 volatile unsigned int mForceClear; ///< Will be set to clear forcibly
405 volatile unsigned int mUploadWithoutRendering; ///< Will be set to upload the resource only (with no rendering)
407 volatile unsigned int mFirstFrameAfterResume; ///< Will be set to check the first frame after resume (for log)
409 std::vector<Rect<int>> mDamagedRects; ///< Keeps collected damaged render items rects for one render pass
412 } // namespace Adaptor
414 } // namespace Internal
418 #endif // DALI_INTERNAL_COMBINED_UPDATE_RENDER_CONTROLLER_H