8111d69a2626f3ff05a631c54ffbc12c8745bac3
[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) 2020 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
33 namespace Internal
34 {
35 namespace Adaptor
36 {
37 class WindowBase;
38 class GlWindow;
39
40 /**
41  * @brief It is for render thread for GlWindow.
42  * User callbacks works in the thread.
43  *
44  * Key Points:
45  *  1. Two Threads:
46  *    a. Main/Event Thread.
47  *    b. Render Thread.
48  *  2. There is NO VSync thread:
49  *    a. We calculate the difference between these two times and if:
50  *      i.  The difference is less than the default frame time, we sleep.
51  *      ii. If it’s more or the same, we continue.
52  *  3. Support Rendering mode
53  *    a. CONTINUOUS mode
54  *      i. The rendering loop works continuously.
55  *    b. ON_DEMAND mode
56  *      i. The rendering works by user's request.
57  *      ii. User's request is the renderOnce()'s function calling.
58  */
59
60 class GlWindowRenderThread : public Dali::Thread
61 {
62 public:
63   /**
64    * Constructor
65    *
66    * @param[in] positionSize The position and size of the physical window
67    * @param[in] depth color depth of the physical window
68    */
69   GlWindowRenderThread(PositionSize positionSize, ColorDepth colorDepth);
70
71   /**
72    * destructor.
73    */
74   virtual ~GlWindowRenderThread();
75
76   /**
77    * Sets the GraphicsInterface instance.
78    * This graphics instance is used to create and initialize graphics resource
79    *
80    * @param[in]  graphics           The graphice instance
81    */
82   void SetGraphicsInterface(GraphicsInterface* graphics);
83
84   /**
85    * Sets the WindowBase instance
86    * This WindowBase instance is used to call wl egl window APIs.
87    *
88    * @param[in]  windowBase           The WindowBase instance
89    */
90   void SetWindowBase(WindowBase* windowBase);
91
92   /**
93    * @brief Sets egl configuration for GlWindow
94    *
95    * @param[in] depth the flag of depth buffer. If true is set, 24bit depth buffer is enabled.
96    * @param[in] stencil the flag of stencil. it true is set, 8bit stencil buffer is enabled.
97    * @param[in] msaa the bit of msaa.
98    * @param[in] version the GLES version.
99    *
100    */
101   void SetEglConfig(bool depth, bool stencil, int msaa, int version);
102
103   /**
104    * Pauses the Render Thread.
105    * It is called when GlWindow is iconified or hidden.
106    *
107    * This will lock the mutex in mRenderThreadWaitCondition.
108    */
109   void Pause();
110
111   /**
112    * Resumes the Render Thread.
113    * It is called when GlWindow is de-iconified or shown.
114    *
115    * This will lock the mutex in mRenderThreadWaitCondition.
116    */
117   void Resume();
118
119   /**
120    * Stops the Render Thread.
121    * This will lock the mutex in mRenderThreadWaitCondition.
122    *
123    * @note Should only be called in Stop as calling this will kill the render thread.
124    */
125   void Stop();
126
127   /**
128    * @copydoc Dali::GlWindow::RegisterGlCallback()
129    */
130   void RegisterGlCallback(CallbackBase* initCallback, CallbackBase* renderFrameCallback, CallbackBase* terminateCallback);
131
132   /**
133    * Enable OnDemand Rendering Mode
134    *
135    * @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.
136    */
137   void SetOnDemandRenderMode(bool onDemand);
138
139   /**
140    * @copydoc Dali::GlWindow::RenderOnce()
141    */
142   void RenderOnce();
143
144 protected:
145   /**
146    * The Render thread loop. This thread will be destroyed on exit from this function.
147    */
148   virtual void Run();
149
150 private:
151   /**
152    * @brief Initialize and create EGL resource
153    */
154   void InitializeGraphics(EglGraphics* eglGraphics);
155
156   /**
157    * Called by the Render Thread which ensures a wait if required.
158    *
159    * @param[out] timeToSleepUntil  The time remaining in nanoseconds to keep the thread sleeping before resuming.
160    * @return false, if the thread should stop.
161    */
162   bool RenderReady(uint64_t& timeToSleepUntil);
163
164 private:
165   GraphicsInterface* mGraphics; ///< Graphics interface
166   WindowBase*        mWindowBase;
167
168   const Dali::LogFactoryInterface& mLogFactory;
169
170   PositionSize mPositionSize; ///< Position
171   ColorDepth   mColorDepth;
172
173   // EGL, GL Resource
174   std::unique_ptr<CallbackBase> mGLInitCallback;
175   std::unique_ptr<CallbackBase> mGLRenderFrameCallback;
176   std::unique_ptr<CallbackBase> mGLTerminateCallback;
177   EGLSurface                    mEGLSurface;
178   EGLContext                    mEGLContext;
179   bool                          mDepth : 1;
180   bool                          mStencil : 1;
181   bool                          mIsEGLInitialize : 1;
182   int                           mGLESVersion;
183   int                           mMSAA;
184
185   // To manage the render/main thread
186   ConditionalWait       mRenderThreadWaitCondition; ///< The wait condition for the update-render-thread.
187   volatile unsigned int mDestroyRenderThread;       ///< Stop render thread. It means this rendter thread will be destoried.
188   volatile unsigned int mPauseRenderThread;         ///< Sleep render thread by pause.
189   volatile unsigned int mRenderingMode;             ///< Rendering Mode, 0: continuous, 1:OnDemad
190   volatile unsigned int mRequestRenderOnce;         ///< Request rendering once
191
192   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.
193
194 }; // GlWindowRenderThread
195
196 } // namespace Adaptor
197 } // namespace Internal
198 } // namespace Dali
199
200 #endif