Merge "[AT-SPI] Rework children handling in ActorAccessible" into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / common / gl-window-render-thread.h
1 #ifndef DALI_INTERNAL_WINDOWSYSTEM_COMMON_GL_WINDOW_RENDER_THREAD_H
2 #define DALI_INTERNAL_WINDOWSYSTEM_COMMON_GL_WINDOW_RENDER_THREAD_H
3
4 /*
5  * Copyright (c) 2021 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/thread.h>
24
25 // INTERNAL INCLUDES
26 #include <dali/internal/graphics/gles/egl-graphics.h>
27 #include <dali/internal/window-system/common/window-base.h>
28
29 namespace Dali
30 {
31 class Adaptor;
32 class TriggerEventInterface;
33
34 namespace Internal
35 {
36 namespace Adaptor
37 {
38 class WindowBase;
39 class GlWindow;
40
41 /**
42  * @brief It is for render thread for GlWindow.
43  * User callbacks works in the thread.
44  *
45  * Key Points:
46  *  1. Two Threads:
47  *    a. Main/Event Thread.
48  *    b. Render Thread.
49  *  2. There is NO VSync thread:
50  *    a. We calculate the difference between these two times and if:
51  *      i.  The difference is less than the default frame time, we sleep.
52  *      ii. If it’s more or the same, we continue.
53  *  3. Support Rendering mode
54  *    a. CONTINUOUS mode
55  *      i. The rendering loop works continuously.
56  *    b. ON_DEMAND mode
57  *      i. The rendering works by user's request.
58  *      ii. User's request is the renderOnce()'s function calling.
59  */
60
61 class GlWindowRenderThread : public Dali::Thread
62 {
63 public:
64   /**
65    * @brief Enumeration for GlWindow Surface status type
66    * It has the status as resized, window is rotated and screen is rotated.
67    *
68    */
69   enum class SurfaceStatus
70   {
71     NO_CHANGED     = 0x00, ///< no changed,
72     RESIZED        = 0x01, ///< When surface is resized,
73     WINDOW_ROTATED = 0x02, ///< When window is rotated,
74     SCREEN_ROTATED = 0x04  ///< When screen is rotated,
75   };
76
77   /**
78    * Constructor
79    *
80    * @param[in] positionSize The position and size of the physical window
81    * @param[in] depth color depth of the physical window
82    */
83   GlWindowRenderThread(PositionSize positionSize, ColorDepth colorDepth);
84
85   /**
86    * destructor.
87    */
88   virtual ~GlWindowRenderThread();
89
90   /**
91    * Sets the GraphicsInterface instance.
92    * This graphics instance is used to create and initialize graphics resource
93    *
94    * @param[in]  graphics           The graphice instance
95    */
96   void SetGraphicsInterface(GraphicsInterface* graphics);
97
98   /**
99    * Sets the WindowBase instance
100    * This WindowBase instance is used to call wl egl window APIs.
101    *
102    * @param[in]  windowBase           The WindowBase instance
103    */
104   void SetWindowBase(WindowBase* windowBase);
105
106   /**
107    * @brief Sets graphics configuration for GlWindow
108    *
109    * @param[in] depth the flag of depth buffer. If true is set, 24bit depth buffer is enabled.
110    * @param[in] stencil the flag of stencil. it true is set, 8bit stencil buffer is enabled.
111    * @param[in] msaa the bit of msaa.
112    * @param[in] version the GLES version.
113    *
114    */
115   void SetGraphicsConfig(bool depth, bool stencil, int msaa, int version);
116
117   /**
118    * Pauses the Render Thread.
119    * It is called when GlWindow is iconified or hidden.
120    *
121    * This will lock the mutex in mRenderThreadWaitCondition.
122    */
123   void Pause();
124
125   /**
126    * Resumes the Render Thread.
127    * It is called when GlWindow is de-iconified or shown.
128    *
129    * This will lock the mutex in mRenderThreadWaitCondition.
130    */
131   void Resume();
132
133   /**
134    * Stops the Render Thread.
135    * This will lock the mutex in mRenderThreadWaitCondition.
136    *
137    * @note Should only be called in Stop as calling this will kill the render thread.
138    */
139   void Stop();
140
141   /**
142    * @copydoc Dali::GlWindow::RegisterGlCallbacks()
143    */
144   void RegisterGlCallbacks(CallbackBase* initCallback, CallbackBase* renderFrameCallback, CallbackBase* terminateCallback);
145
146   /**
147    * Enable OnDemand Rendering Mode
148    *
149    * @param[in] onDemand the flag of OnDemand Rendering Mode. If the flag is true, rendering mode is OnDemand, otherwise the flag is false, rendering mode is continuous mode.
150    */
151   void SetOnDemandRenderMode(bool onDemand);
152
153   /**
154    * @copydoc Dali::GlWindow::RenderOnce()
155    */
156   void RenderOnce();
157
158   /**
159    * Requests the window resize to GlWindow's render thread.
160    *
161    * @param[in] width new width.
162    * @param[in] height new height.
163    */
164   void RequestWindowResize(int width, int height);
165
166   /**
167    * Requests the window rotation to GlWindow's render thread.
168    *
169    * @param[in] windowAngle the window rotation's angle as 0, 90, 180 and 270.
170    */
171   void RequestWindowRotate(int windowAngle);
172
173   /**
174    * Requests the screen rotation to GlWindow's render thread.
175    *
176    * @param[in] screenAngle the screen rotation's angle as 0, 90, 180 and 270.
177    */
178   void RequestScreenRotate(int screenAngle);
179
180 protected:
181   /**
182    * The Render thread loop. This thread will be destroyed on exit from this function.
183    */
184   virtual void Run();
185
186 private:
187   /**
188    * @brief Initialize and create EGL resource
189    */
190   void InitializeGraphics(EglGraphics* eglGraphics);
191
192   /**
193    * Called by the Render Thread which ensures a wait if required.
194    *
195    * @param[out] timeToSleepUntil  The time remaining in nanoseconds to keep the thread sleeping before resuming.
196    * @return false, if the thread should stop.
197    */
198   bool RenderReady(uint64_t& timeToSleepUntil);
199
200   /**
201    * In the Tizen world, when GlWindow rotation is finished in client side,
202    * the completed message should be sent to display server.
203    * This function should be called in the event thread after buffer is committed.
204    */
205   void WindowRotationCompleted();
206
207   /**
208    * Gets window's current surface status
209    *
210    * @brief This function return window's current surface status.
211    * The status has the  the information of window resized, window rotated and screen rotated.
212    * After called, the status value is reset
213    *
214    * @[output] windowRotationAngle return current window rotation angle.
215    * @[output] screenRotationAngle return current screen rotation angle.
216    * @return the window's current surface status.
217    */
218   unsigned int GetSurfaceStatus(int& windowRotationAngle, int& screenRotationAngle);
219
220   /**
221    * Starts post rendering process
222    *
223    * @brief Starts post rendering process for window rotation
224    * It is to pause the render thread until maint thread finishes the window rotation work.
225    */
226   void PostRenderStart();
227
228   /**
229    * Finishs post rendering process
230    *
231    * @brief Finishes post rendering process for window rotation
232    * It set the resume flag for resume the render thread.
233    */
234   void PostRenderFinish();
235
236   /**
237    * Pauses the render thread unitil post rendering process
238    *
239    * @brief Pauses the render thread until main thread works window rotation.
240    */
241   void PostRenderWaitForFinished();
242
243 private:
244   GraphicsInterface*                     mGraphics; ///< Graphics interface
245   WindowBase*                            mWindowBase;
246   std::unique_ptr<TriggerEventInterface> mWindowRotationTrigger;
247
248   const Dali::LogFactoryInterface& mLogFactory;
249
250   PositionSize mPositionSize; ///< Position
251   ColorDepth   mColorDepth;
252
253   // EGL, GL Resource
254   std::unique_ptr<CallbackBase> mGLInitCallback;
255   std::unique_ptr<CallbackBase> mGLRenderFrameCallback;
256   std::unique_ptr<CallbackBase> mGLTerminateCallback;
257   EGLSurface                    mEGLSurface;
258   EGLContext                    mEGLContext;
259   bool                          mDepth : 1;
260   bool                          mStencil : 1;
261   bool                          mIsEGLInitialize : 1;
262   int                           mGLESVersion;
263   int                           mMSAA;
264   int                           mWindowRotationAngle; ///< The angle of window rotation angle
265   int                           mScreenRotationAngle; ///< The angle of screen rotation angle
266
267   // To manage the render/main thread
268   ConditionalWait       mRenderThreadWaitCondition; ///< The wait condition for the update-render-thread.
269   volatile unsigned int mDestroyRenderThread;       ///< Stop render thread. It means this rendter thread will be destoried.
270   volatile unsigned int mPauseRenderThread;         ///< Sleep render thread by pause.
271   volatile unsigned int mRenderingMode;             ///< Rendering Mode, 0: continuous, 1:OnDemad
272   volatile unsigned int mRequestRenderOnce;         ///< Request rendering once
273   volatile unsigned int mSurfaceStatus;             ///< When surface is changed as resized or rotated, this flag is set. 0: No changed, 1:resized, 2:window rotation, 4:screen rotation
274   volatile unsigned int mPostRendering;             ///< Whether post-rendering is taking place (set by the event & render threads, read by the render-thread).
275
276   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.
277
278 }; // GlWindowRenderThread
279
280 } // namespace Adaptor
281 } // namespace Internal
282 } // namespace Dali
283
284 #endif