1 #ifndef DALI_TOOLKIT_INTERNAL_GL_VIEW_THREAD_H
2 #define DALI_TOOLKIT_INTERNAL_GL_VIEW_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/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>
38 * @brief GlViewRenderThread is a render thread for GlView.
39 * This invokes user's callbacks.
41 class GlViewRenderThread : public Dali::Thread
47 * @param[in] queue The NativeImageSourceQueue that GL renders onto
49 GlViewRenderThread(Dali::NativeImageSourceQueuePtr queue);
54 virtual ~GlViewRenderThread();
57 * @copydoc Dali::Toolkit::GlView::RegisterGlCallback()
59 void RegisterGlCallback(CallbackBase* initCallback, CallbackBase* renderFrameCallback, CallbackBase* terminateCallback);
62 * @copydoc Dali::Toolkit::GlView::SetResizeCallback()
64 void SetResizeCallback(CallbackBase* resizeCallback);
67 * @copydoc Dali::Toolkit::GlView::SetGraphicsConfig()
69 bool SetGraphicsConfig(bool depth, bool stencil, int msaa, int version);
72 * Enable OnDemand Rendering Mode
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.
77 void SetOnDemandRenderMode(bool onDemand);
80 * Sets the surface size
82 * @param[in] size the size of the NaitveImageSurface
84 void SetSurfaceSize(Dali::Vector2 size);
87 * @copydoc Dali::Toolkit::RenderOnce()
92 * Pauses the render thread.
97 * Resumes the render thread.
102 * Stops the render thread.
103 * @note Should only be called in Stop as calling this will kill the render thread.
108 * Acquires the surface resource
110 void AcquireSurface();
113 * Releases the surface resource
115 void ReleaseSurface();
119 * The routine that the thread will execute once it is started.
124 GlViewRenderThread(const GlViewRenderThread& obj) = delete;
125 GlViewRenderThread operator=(const GlViewRenderThread& obj) = delete;
128 * Called by the Render Thread which ensures a wait if required.
130 * @param[out] timeToSleepUntil The time remaining in nanoseconds to keep the thread sleeping before resuming.
131 * @return false, if the thread should stop.
133 bool RenderReady(uint64_t& timeToSleepUntil);
136 * @brief Get the monotonic time since the clock's epoch.
137 * @param[out] timeInNanoseconds The time in nanoseconds since the reference point.
139 void GetNanoSeconds(uint64_t& timeInNanoseconds);
142 * Blocks the execution of the current thread until specified sleep_time
143 * @param[in] timeInNanoseconds The time blocking for
145 void SleepUntil(uint64_t timeInNanoseconds);
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
154 std::unique_ptr<CallbackBase> mGlInitCallback;
155 std::unique_ptr<CallbackBase> mGlRenderFrameCallback;
156 std::unique_ptr<CallbackBase> mGlTerminateCallback;
157 std::unique_ptr<CallbackBase> mResizeCallback;
162 int mGraphicsApiVersion;
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.
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.
175 } // namespace Internal
176 } // namespace Toolkit
179 #endif // DALI_TOOLKIT_INTERNAL_GL_SURFACE_VIEW_THREAD_H