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