[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / gl-view / gl-view-render-thread.h
1 #ifndef DALI_TOOLKIT_INTERNAL_GL_VIEW_THREAD_H
2 #define DALI_TOOLKIT_INTERNAL_GL_VIEW_THREAD_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/adaptor-framework/native-image-source-queue.h>
23 #include <dali/devel-api/threading/conditional-wait.h>
24 #include <dali/devel-api/threading/semaphore.h>
25 #include <dali/devel-api/threading/thread.h>
26 #include <dali/integration-api/adaptor-framework/log-factory-interface.h>
27 #include <dali/integration-api/adaptor-framework/native-image-surface.h>
28 #include <dali/integration-api/adaptor-framework/trace-factory-interface.h>
29 #include <dali/public-api/math/vector2.h>
30 #include <dali/public-api/signals/callback.h>
31
32 namespace Dali
33 {
34 namespace Toolkit
35 {
36 namespace Internal
37 {
38 /**
39  * @brief GlViewRenderThread is a render thread for GlView.
40  * This invokes user's callbacks.
41  */
42 class GlViewRenderThread : public Dali::Thread
43 {
44 public:
45   /**
46    * Constructor
47    *
48    * @param[in] queue The NativeImageSourceQueue that GL renders onto
49    */
50   GlViewRenderThread(Dali::NativeImageSourceQueuePtr queue);
51
52   /**
53    * destructor.
54    */
55   virtual ~GlViewRenderThread();
56
57   /**
58    * @copydoc Dali::Toolkit::GlView::RegisterGlCallbacks()
59    */
60   void RegisterGlCallbacks(CallbackBase* initCallback, CallbackBase* renderFrameCallback, CallbackBase* terminateCallback);
61
62   /**
63    * @copydoc Dali::Toolkit::GlView::SetResizeCallback()
64    */
65   void SetResizeCallback(CallbackBase* resizeCallback);
66
67   /**
68    * @copydoc Dali::Toolkit::GlView::SetGraphicsConfig()
69    */
70   bool SetGraphicsConfig(bool depth, bool stencil, int msaa, int version);
71
72   /**
73    * Enable OnDemand Rendering Mode
74    *
75    * @param[in] onDemand the flag of OnDemand Rendering Mode. If the flag is true, the rendering mode is set OnDemand,
76    * otherwise the flag is false, rendering mode is set continuous mode.
77    */
78   void SetOnDemandRenderMode(bool onDemand);
79
80   /**
81    * Sets the surface size
82    *
83    * @param[in] size the size of the NaitveImageSurface
84    */
85   void SetSurfaceSize(Dali::Vector2 size);
86
87   /**
88    * @copydoc Dali::Toolkit::RenderOnce()
89    */
90   void RenderOnce();
91
92   /**
93    * Pauses the render thread.
94    */
95   void Pause();
96
97   /**
98    * Resumes the render thread.
99    */
100   void Resume();
101
102   /**
103    * Stops the render thread.
104    * @note Should only be called in Stop as calling this will kill the render thread.
105    */
106   void Stop();
107
108   /**
109    * Acquires the surface resource
110    */
111   void AcquireSurface();
112
113   /**
114    * Releases the surface resource
115    */
116   void ReleaseSurface();
117
118 protected:
119   /**
120    * The routine that the thread will execute once it is started.
121    */
122   void Run() override;
123
124 private:
125   GlViewRenderThread(const GlViewRenderThread& obj) = delete;
126   GlViewRenderThread operator=(const GlViewRenderThread& obj) = delete;
127
128   /**
129    * Called by the Render Thread which ensures a wait if required.
130    *
131    * @param[out] timeToSleepUntil  The time remaining in nanoseconds to keep the thread sleeping before resuming.
132    * @return false, if the thread should stop.
133    */
134   bool RenderReady(uint64_t& timeToSleepUntil);
135
136   /**
137    * @brief Get the monotonic time since the clock's epoch.
138    * @param[out]  timeInNanoseconds  The time in nanoseconds since the reference point.
139    */
140   void GetNanoSeconds(uint64_t& timeInNanoseconds);
141
142   /**
143    * Blocks the execution of the current thread until specified sleep_time
144    * @param[in] timeInNanoseconds  The time blocking for
145    */
146   void SleepUntil(uint64_t timeInNanoseconds);
147
148 private:
149   const Dali::LogFactoryInterface&   mLogFactory;
150   const Dali::TraceFactoryInterface& mTraceFactory;
151
152   Dali::Vector2                   mSurfaceSize; ///< The size of mNativeImageQueue
153   Dali::NativeImageSurfacePtr     mNativeImageSurface;
154   Dali::NativeImageSourceQueuePtr mNativeImageQueue;
155   Semaphore<>                     mSurfaceSemaphore; ///< The semaphore to avoid race condition to the render target
156
157   std::unique_ptr<CallbackBase> mGlInitCallback;
158   std::unique_ptr<CallbackBase> mGlRenderFrameCallback;
159   std::unique_ptr<CallbackBase> mGlTerminateCallback;
160   std::unique_ptr<CallbackBase> mResizeCallback;
161
162   bool mDepth : 1;
163   bool mStencil : 1;
164   int  mMSAA;
165   int  mGraphicsApiVersion;
166
167   Dali::ConditionalWait mConditionalWait;
168   volatile unsigned int mIsThreadStarted;   ///< Whether this thread has been started.
169   volatile unsigned int mIsThreadStopped;   ///< Stop render thread. It means this render thread will be destroyed.
170   volatile unsigned int mIsThreadPaused;    ///< Sleep render thread by pause.
171   volatile unsigned int mIsRenderRequested; ///< Request rendering once
172   volatile unsigned int mRenderingMode;     ///< Rendering Mode, 0: Continuous, 1:OnDemand
173   volatile unsigned int mIsSurfaceResized;  ///< Invoke ResizeCallback when NativeImageSurface is resized.
174
175   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.
176 };
177
178 } // namespace Internal
179 } // namespace Toolkit
180 } // namespace Dali
181
182 #endif // DALI_TOOLKIT_INTERNAL_GL_SURFACE_VIEW_THREAD_H