1 #ifndef DALI_TOOLKIT_INTERNAL_GL_VIEW_THREAD_H
2 #define DALI_TOOLKIT_INTERNAL_GL_VIEW_THREAD_H
5 * Copyright (c) 2023 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/integration-api/adaptor-framework/trace-factory-interface.h>
29 #include <dali/public-api/math/vector2.h>
30 #include <dali/public-api/signals/callback.h>
39 * @brief GlViewRenderThread is a render thread for GlView.
40 * This invokes user's callbacks.
42 class GlViewRenderThread : public Dali::Thread
48 * @param[in] queue The NativeImageSourceQueue that GL renders onto
50 GlViewRenderThread(Dali::NativeImageSourceQueuePtr queue);
55 virtual ~GlViewRenderThread();
58 * @copydoc Dali::Toolkit::GlView::RegisterGlCallbacks()
60 void RegisterGlCallbacks(CallbackBase* initCallback, CallbackBase* renderFrameCallback, CallbackBase* terminateCallback);
63 * @copydoc Dali::Toolkit::GlView::SetResizeCallback()
65 void SetResizeCallback(CallbackBase* resizeCallback);
68 * @copydoc Dali::Toolkit::GlView::SetGraphicsConfig()
70 bool SetGraphicsConfig(bool depth, bool stencil, int msaa, int version);
73 * Enable OnDemand Rendering Mode
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.
78 void SetOnDemandRenderMode(bool onDemand);
81 * Sets the surface size
83 * @param[in] size the size of the NaitveImageSurface
85 void SetSurfaceSize(Dali::Vector2 size);
88 * @copydoc Dali::Toolkit::RenderOnce()
93 * Pauses the render thread.
98 * Resumes the render thread.
103 * Stops the render thread.
104 * @note Should only be called in Stop as calling this will kill the render thread.
109 * Acquires the surface resource
111 void AcquireSurface();
114 * Releases the surface resource
116 void ReleaseSurface();
120 * The routine that the thread will execute once it is started.
125 GlViewRenderThread(const GlViewRenderThread& obj) = delete;
126 GlViewRenderThread operator=(const GlViewRenderThread& obj) = delete;
129 * Called by the Render Thread which ensures a wait if required.
131 * @param[out] timeToSleepUntil The time remaining in nanoseconds to keep the thread sleeping before resuming.
132 * @return false, if the thread should stop.
134 bool RenderReady(uint64_t& timeToSleepUntil);
137 * @brief Get the monotonic time since the clock's epoch.
138 * @param[out] timeInNanoseconds The time in nanoseconds since the reference point.
140 void GetNanoSeconds(uint64_t& timeInNanoseconds);
143 * Blocks the execution of the current thread until specified sleep_time
144 * @param[in] timeInNanoseconds The time blocking for
146 void SleepUntil(uint64_t timeInNanoseconds);
149 const Dali::LogFactoryInterface& mLogFactory;
150 const Dali::TraceFactoryInterface& mTraceFactory;
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
157 std::unique_ptr<CallbackBase> mGlInitCallback;
158 std::unique_ptr<CallbackBase> mGlRenderFrameCallback;
159 std::unique_ptr<CallbackBase> mGlTerminateCallback;
160 std::unique_ptr<CallbackBase> mResizeCallback;
165 int mGraphicsApiVersion;
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.
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.
178 } // namespace Internal
179 } // namespace Toolkit
182 #endif // DALI_TOOLKIT_INTERNAL_GL_SURFACE_VIEW_THREAD_H