1 #ifndef DALI_INTERNAL_WINDOWSYSTEM_COMMON_GL_WINDOW_RENDER_THREAD_H
2 #define DALI_INTERNAL_WINDOWSYSTEM_COMMON_GL_WINDOW_RENDER_THREAD_H
5 * Copyright (c) 2021 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/thread.h>
26 #include <dali/internal/graphics/gles/egl-graphics.h>
27 #include <dali/internal/window-system/common/window-base.h>
32 class TriggerEventInterface;
42 * @brief It is for render thread for GlWindow.
43 * User callbacks works in the thread.
47 * a. Main/Event 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
55 * i. The rendering loop works continuously.
57 * i. The rendering works by user's request.
58 * ii. User's request is the renderOnce()'s function calling.
61 class GlWindowRenderThread : public Dali::Thread
65 * @brief Enumeration for GlWindow Surface status type
66 * It has the status as resized, window is rotated and screen is rotated.
69 enum class SurfaceStatus
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,
80 * @param[in] positionSize The position and size of the physical window
81 * @param[in] depth color depth of the physical window
83 GlWindowRenderThread(PositionSize positionSize, ColorDepth colorDepth);
88 virtual ~GlWindowRenderThread();
91 * Sets the GraphicsInterface instance.
92 * This graphics instance is used to create and initialize graphics resource
94 * @param[in] graphics The graphice instance
96 void SetGraphicsInterface(GraphicsInterface* graphics);
99 * Sets the WindowBase instance
100 * This WindowBase instance is used to call wl egl window APIs.
102 * @param[in] windowBase The WindowBase instance
104 void SetWindowBase(WindowBase* windowBase);
107 * @brief Sets graphics configuration for GlWindow
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.
115 void SetGraphicsConfig(bool depth, bool stencil, int msaa, int version);
118 * Pauses the Render Thread.
119 * It is called when GlWindow is iconified or hidden.
121 * This will lock the mutex in mRenderThreadWaitCondition.
126 * Resumes the Render Thread.
127 * It is called when GlWindow is de-iconified or shown.
129 * This will lock the mutex in mRenderThreadWaitCondition.
134 * Stops the Render Thread.
135 * This will lock the mutex in mRenderThreadWaitCondition.
137 * @note Should only be called in Stop as calling this will kill the render thread.
142 * @copydoc Dali::GlWindow::RegisterGlCallbacks()
144 void RegisterGlCallbacks(CallbackBase* initCallback, CallbackBase* renderFrameCallback, CallbackBase* terminateCallback);
147 * Enable OnDemand Rendering Mode
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.
151 void SetOnDemandRenderMode(bool onDemand);
154 * @copydoc Dali::GlWindow::RenderOnce()
159 * Requests the window resize to GlWindow's render thread.
161 * @param[in] width new width.
162 * @param[in] height new height.
164 void RequestWindowResize(int width, int height);
167 * Requests the window rotation to GlWindow's render thread.
169 * @param[in] windowAngle the window rotation's angle as 0, 90, 180 and 270.
171 void RequestWindowRotate(int windowAngle);
174 * Requests the screen rotation to GlWindow's render thread.
176 * @param[in] screenAngle the screen rotation's angle as 0, 90, 180 and 270.
178 void RequestScreenRotate(int screenAngle);
182 * The Render thread loop. This thread will be destroyed on exit from this function.
188 * @brief Initialize and create EGL resource
190 void InitializeGraphics(EglGraphics* eglGraphics);
193 * Called by the Render Thread which ensures a wait if required.
195 * @param[out] timeToSleepUntil The time remaining in nanoseconds to keep the thread sleeping before resuming.
196 * @return false, if the thread should stop.
198 bool RenderReady(uint64_t& timeToSleepUntil);
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.
205 void WindowRotationCompleted();
208 * Gets window's current surface status
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
214 * @[output] windowRotationAngle return current window rotation angle.
215 * @[output] screenRotationAngle return current screen rotation angle.
216 * @return the window's current surface status.
218 unsigned int GetSurfaceStatus(int& windowRotationAngle, int& screenRotationAngle);
221 * Starts post rendering process
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.
226 void PostRenderStart();
229 * Finishs post rendering process
231 * @brief Finishes post rendering process for window rotation
232 * It set the resume flag for resume the render thread.
234 void PostRenderFinish();
237 * Pauses the render thread unitil post rendering process
239 * @brief Pauses the render thread until main thread works window rotation.
241 void PostRenderWaitForFinished();
244 GraphicsInterface* mGraphics; ///< Graphics interface
245 WindowBase* mWindowBase;
246 std::unique_ptr<TriggerEventInterface> mWindowRotationTrigger;
248 const Dali::LogFactoryInterface& mLogFactory;
250 PositionSize mPositionSize; ///< Position
251 ColorDepth mColorDepth;
254 std::unique_ptr<CallbackBase> mGLInitCallback;
255 std::unique_ptr<CallbackBase> mGLRenderFrameCallback;
256 std::unique_ptr<CallbackBase> mGLTerminateCallback;
257 EGLSurface mEGLSurface;
258 EGLContext mEGLContext;
261 bool mIsEGLInitialize : 1;
264 int mWindowRotationAngle; ///< The angle of window rotation angle
265 int mScreenRotationAngle; ///< The angle of screen rotation angle
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).
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.
278 }; // GlWindowRenderThread
280 } // namespace Adaptor
281 } // namespace Internal