Merge "Updating non-ecore X window-system with new API" into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / internal / adaptor / common / combined-update-render-controller.h
1 #ifndef DALI_INTERNAL_COMBINED_UPDATE_RENDER_CONTROLLER_H
2 #define DALI_INTERNAL_COMBINED_UPDATE_RENDER_CONTROLLER_H
3
4 /*
5  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/threading/conditional-wait.h>
23 #include <dali/devel-api/threading/semaphore.h>
24 #include <dali/integration-api/core.h>
25 #include <pthread.h>
26 #include <semaphore.h>
27 #include <stdint.h>
28 #include <atomic>
29
30 // INTERNAL INCLUDES
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>
38
39 namespace Dali
40 {
41 class RenderSurfaceInterface;
42 class TriggerEventInterface;
43
44 namespace Internal
45 {
46
47 namespace Adaptor
48 {
49 class AdaptorInternalServices;
50 class EnvironmentOptions;
51
52 /**
53  * @brief Two threads where events/application interaction is handled on the main/event thread and the Update & Render
54  * happen on the other thread.
55  *
56  * Key Points:
57  *  1. Two Threads:
58  *    a. Main/Event Thread.
59  *    b. Update/Render Thread.
60  *  2. There is NO VSync thread:
61  *    a. We retrieve the time before Update.
62  *    b. Then retrieve the time after Render.
63  *    c. We calculate the difference between these two times and if:
64  *      i.  The difference is less than the default frame time, we sleep.
65  *      ii. If it’s more or the same, we continue.
66  *  3. On the update/render thread, if we discover that we do not need to do any more updates, we use a trigger-event
67  *     to inform the main/event thread. This is then processed as soon as the event thread is able to do so where it
68  *     is easier to make a decision about whether we should stop the update/render thread or not (depending on any
69  *     update requests etc.).
70  *  4. The main thread is blocked while the surface is being replaced.
71  *  5. When we resume from paused, elapsed time is used for the animations, i.e. the could have finished while we were paused.
72  *     However, FinishedSignal emission will only happen upon resumption.
73  *  6. Elapsed time is NOT used while if we are waking up from a sleep state or doing an UpdateOnce.
74  */
75 class CombinedUpdateRenderController : public ThreadControllerInterface,
76                                        public ThreadSynchronizationInterface
77 {
78 public:
79   /**
80    * Constructor
81    */
82   CombinedUpdateRenderController(AdaptorInternalServices& adaptorInterfaces, const EnvironmentOptions& environmentOptions, ThreadMode threadMode);
83
84   /**
85    * Non virtual destructor. Not intended as base class.
86    */
87   ~CombinedUpdateRenderController();
88
89   /**
90    * @copydoc ThreadControllerInterface::Initialize()
91    */
92   void Initialize() override;
93
94   /**
95    * @copydoc ThreadControllerInterface::Start()
96    */
97   void Start() override;
98
99   /**
100    * @copydoc ThreadControllerInterface::Pause()
101    */
102   void Pause() override;
103
104   /**
105    * @copydoc ThreadControllerInterface::Resume()
106    */
107   void Resume() override;
108
109   /**
110    * @copydoc ThreadControllerInterface::Stop()
111    */
112   void Stop() override;
113
114   /**
115    * @copydoc ThreadControllerInterface::RequestUpdate()
116    */
117   void RequestUpdate() override;
118
119   /**
120    * @copydoc ThreadControllerInterface::RequestUpdateOnce()
121    */
122   void RequestUpdateOnce(UpdateMode updateMode) override;
123
124   /**
125    * @copydoc ThreadControllerInterface::ReplaceSurface()
126    */
127   void ReplaceSurface(Dali::RenderSurfaceInterface* surface) override;
128
129   /**
130    * @copydoc ThreadControllerInterface::DeleteSurface()
131    */
132   void DeleteSurface(Dali::RenderSurfaceInterface* surface) override;
133
134   /**
135    * @copydoc ThreadControllerInterface::ResizeSurface()
136    */
137   void ResizeSurface() override;
138
139   /**
140    * @copydoc ThreadControllerInterface::WaitForGraphicsInitialization()
141    */
142   void WaitForGraphicsInitialization() override;
143
144   /**
145    * @copydoc ThreadControllerInterface::SetRenderRefreshRate()
146    */
147   void SetRenderRefreshRate(unsigned int numberOfFramesPerRender) override;
148
149   /**
150    * @copydoc ThreadControllerInterface::SetPreRenderCallback
151    */
152   void SetPreRenderCallback(CallbackBase* callback) override;
153
154   /**
155    * @copydoc ThreadControllerInterface::AddSurface()
156    */
157   void AddSurface(Dali::RenderSurfaceInterface* surface) override;
158
159   /**
160    * @copydoc ThreadControllerInterface::GetThreadId()
161    */
162   int32_t GetThreadId() const override;
163
164 private:
165   // Undefined copy constructor.
166   CombinedUpdateRenderController(const CombinedUpdateRenderController&);
167
168   // Undefined assignment operator.
169   CombinedUpdateRenderController& operator=(const CombinedUpdateRenderController&);
170
171   /////////////////////////////////////////////////////////////////////////////////////////////////
172   // EventThread
173   /////////////////////////////////////////////////////////////////////////////////////////////////
174
175   enum AnimationProgression
176   {
177     USE_ELAPSED_TIME, ///< Animation progression using elapsed time
178     NONE              ///< No animation progression
179   };
180
181   /**
182    * Runs the Update/Render Thread.
183    * This will lock the mutex in mUpdateRenderThreadWaitCondition.
184    *
185    * @param[in]  numberOfCycles           The number of times the update/render cycle should run. If -1, then it will run continuously.
186    * @param[in]  animationProgression     Whether to progress animation using time elapsed since the last frame.
187    * @param[in]  updateMode               The update mode (i.e. either update & render or skip rendering)
188    */
189   inline void RunUpdateRenderThread(int numberOfCycles, AnimationProgression animationProgression, UpdateMode updateMode);
190
191   /**
192    * Pauses the Update/Render Thread.
193    * This will lock the mutex in mUpdateRenderThreadWaitCondition.
194    */
195   inline void PauseUpdateRenderThread();
196
197   /**
198    * Stops the Update/Render Thread.
199    * This will lock the mutex in mUpdateRenderThreadWaitCondition.
200    *
201    * @note Should only be called in Stop as calling this will kill the update-thread.
202    */
203   inline void StopUpdateRenderThread();
204
205   /**
206    * Checks if the the Update/Render Thread is paused.
207    * This will lock the mutex in mUpdateRenderThreadWaitCondition.
208    *
209    * @return true if paused, false otherwise
210    */
211   inline bool IsUpdateRenderThreadPaused();
212
213   /**
214    * Used as the callback for the sleep-trigger.
215    *
216    * Will sleep when enough requests are made without any requests.
217    */
218   void ProcessSleepRequest();
219
220   /////////////////////////////////////////////////////////////////////////////////////////////////
221   // UpdateRenderThread
222   /////////////////////////////////////////////////////////////////////////////////////////////////
223
224   /**
225    * The Update/Render thread loop. This thread will be destroyed on exit from this function.
226    */
227   void UpdateRenderThread();
228
229   /**
230    * Called by the Update/Render Thread which ensures a wait if required.
231    *
232    * @param[out] useElapsedTime    If true when returned, then the actual elapsed time will be used for animation.
233    *                               If false when returned, then there should NOT be any animation progression in the next Update.
234    * @param[in]  updateRequired    Whether another update is required.
235    * @param[out] timeToSleepUntil  The time remaining in nanoseconds to keep the thread sleeping before resuming.
236    * @return false, if the thread should stop.
237    */
238   bool UpdateRenderReady(bool& useElapsedTime, bool updateRequired, uint64_t& timeToSleepUntil);
239
240   /**
241    * Checks to see if the surface needs to be replaced.
242    * This will lock the mutex in mUpdateRenderThreadWaitCondition.
243    *
244    * @return Pointer to the new surface, NULL otherwise
245    */
246   Dali::RenderSurfaceInterface* ShouldSurfaceBeReplaced();
247
248   /**
249    * Called by the Update/Render thread after a surface has been replaced.
250    *
251    * This will lock the mutex in mEventThreadWaitCondition
252    */
253   void SurfaceReplaced();
254
255   /**
256    * Checks to see if the surface needs to be deleted.
257    * This will lock the mutex in mUpdateRenderThreadWaitCondition.
258    *
259    * @return Pointer to the deleted surface, nullptr otherwise
260    */
261   Dali::RenderSurfaceInterface* ShouldSurfaceBeDeleted();
262
263   /**
264    * Called by the Update/Render thread after a surface has been deleted.
265    *
266    * This will lock the mutex in mEventThreadWaitCondition
267    */
268   void SurfaceDeleted();
269
270   /**
271    * Called by the Update/Render thread after a surface has been resized.
272    *
273    * This will lock the mutex in mEventThreadWaitCondition
274    */
275   void SurfaceResized();
276
277   /**
278    * PreCompile shaders for launching time
279    *
280    * @param[in] vertexShader vertexShader need to precompile
281    * @param[in] fragmentShader fragmentShader need to precompile
282   */
283   void PreCompileShader(std::string vertexShader, std::string fragmentShader);
284
285   /**
286    * Cancel the precompile
287   */
288   void CancelPreCompile();
289
290   /**
291    * Helper for the thread calling the entry function
292    * @param[in] This A pointer to the current object
293    */
294   static void* InternalUpdateRenderThreadEntryFunc(void* This)
295   {
296     (static_cast<CombinedUpdateRenderController*>(This))->UpdateRenderThread();
297     return NULL;
298   }
299
300   /////////////////////////////////////////////////////////////////////////////////////////////////
301   // ALL Threads
302   /////////////////////////////////////////////////////////////////////////////////////////////////
303
304   /**
305    * Called by the update-render & v-sync threads when they up and running.
306    *
307    * This will lock the mutex in mEventThreadWaitCondition.
308    */
309   void NotifyThreadInitialised();
310
311   /**
312    * Called by the update-render thread when graphics has been initialised.
313    */
314   void NotifyGraphicsInitialised();
315
316   /**
317    * Helper to add a performance marker to the performance server (if it's active)
318    * @param[in]  type  performance marker type
319    */
320   void AddPerformanceMarker(PerformanceInterface::MarkerType type);
321
322   /////////////////////////////////////////////////////////////////////////////////////////////////
323   // POST RENDERING - ThreadSynchronizationInterface overrides
324   /////////////////////////////////////////////////////////////////////////////////////////////////
325
326   /////////////////////////////////////////////////////////////////////////////////////////////////
327   //// Called by the Event Thread if post-rendering is required
328   /////////////////////////////////////////////////////////////////////////////////////////////////
329
330   /**
331    * @copydoc ThreadSynchronizationInterface::PostRenderComplete()
332    */
333   void PostRenderComplete() override;
334
335   /////////////////////////////////////////////////////////////////////////////////////////////////
336   //// Called by the Render Thread if post-rendering is required
337   /////////////////////////////////////////////////////////////////////////////////////////////////
338
339   /**
340    * @copydoc ThreadSynchronizationInterface::PostRenderStarted()
341    */
342   void PostRenderStarted() override;
343
344   /**
345    * @copydoc ThreadSynchronizationInterface::PostRenderStarted()
346    */
347   void PostRenderWaitForCompletion() override;
348
349 private:
350   FpsTracker         mFpsTracker;         ///< Object that tracks the FPS
351   UpdateStatusLogger mUpdateStatusLogger; ///< Object that logs the update-status as required.
352
353   Semaphore<>     mEventThreadSemaphore;   ///< Used by the event thread to ensure all threads have been initialised, and when replacing the surface.
354   ConditionalWait mGraphicsInitializeWait; ///< Used by the render thread to ensure the graphics has been initialised.
355   Semaphore<>     mSurfaceSemaphore;       ///< Used by the event thread to ensure the surface has been deleted or replaced.
356
357   ConditionalWait mUpdateRenderThreadWaitCondition; ///< The wait condition for the update-render-thread.
358
359   AdaptorInternalServices&  mAdaptorInterfaces;    ///< The adaptor internal interface
360   PerformanceInterface*     mPerformanceInterface; ///< The performance logging interface
361   Integration::Core&        mCore;                 ///< Dali core reference
362   const EnvironmentOptions& mEnvironmentOptions;   ///< Environment options
363   TriggerEventInterface&    mNotificationTrigger;  ///< Reference to notification event trigger
364   TriggerEventInterface*    mSleepTrigger;         ///< Used by the update-render thread to trigger the event thread when it no longer needs to do any updates
365   CallbackBase*             mPreRenderCallback;    ///< Used by Update/Render thread when PreRender is about to be called on graphics.
366
367   Dali::Devel::TextureUploadManager& mTextureUploadManager; ///< TextureUploadManager
368
369   pthread_t* mUpdateRenderThread; ///< The Update/Render thread.
370
371   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.
372   // TODO: mDefaultFrameDurationMilliseconds is defined as uint64_t, the only place where it is used, it is converted to an unsigned int!!!
373   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.
374   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.
375   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.
376
377   uint32_t mUpdateRequestCount; ///< Count of update-requests we have received to ensure we do not go to sleep too early.
378   uint32_t mRunning;            ///< Read and set on the event-thread only to state whether we are running.
379   int32_t  mThreadId;           ///< UpdateRender thread id
380
381   ThreadMode mThreadMode; ///< Whether the thread runs continuously or runs when it is requested.
382
383   //
384   // NOTE: cannot use booleans as these are used from multiple threads, must use variable with machine word size for atomic read/write
385   //
386
387   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).
388   volatile unsigned int mDestroyUpdateRenderThread;  ///< Whether the Update/Render thread be destroyed (set by the event-thread, read by the update-render-thread).
389   volatile unsigned int mUpdateRenderThreadCanSleep; ///< Whether the Update/Render thread can sleep (set by the event-thread, read by the update-render-thread).
390   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).
391                                                      ///< Ensures we do not go to sleep if we have not processed the most recent update-request.
392
393   volatile unsigned int mUseElapsedTimeAfterWait; ///< Whether we should use the elapsed time after waiting (set by the event-thread, read by the update-render-thread).
394   volatile unsigned int mIsPreCompileCancelled;      ///< Whether we need to do precompile shader.
395
396   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).
397   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).
398
399   volatile unsigned int mPostRendering;  ///< Whether post-rendering is taking place (set by the event & render threads, read by the render-thread).
400   volatile unsigned int mSurfaceResized; ///< Will be set to resize the surface (set by the event-thread, read & cleared by the update-render thread).
401   volatile unsigned int mForceClear;     ///< Will be set to clear forcibly
402
403   volatile unsigned int mUploadWithoutRendering; ///< Will be set to upload the resource only (with no rendering)
404
405   volatile unsigned int mFirstFrameAfterResume; ///< Will be set to check the first frame after resume (for log)
406
407   std::vector<Rect<int>> mDamagedRects; ///< Keeps collected damaged render items rects for one render pass
408 };
409
410 } // namespace Adaptor
411
412 } // namespace Internal
413
414 } // namespace Dali
415
416 #endif // DALI_INTERNAL_COMBINED_UPDATE_RENDER_CONTROLLER_H