1 #ifndef DALI_TOOLKIT_GL_VIEW_H
2 #define DALI_TOOLKIT_GL_VIEW_H
4 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #include <dali/public-api/common/vector-wrapper.h>
22 #include <dali/public-api/rendering/texture.h>
25 #include <dali-toolkit/public-api/controls/control.h>
31 namespace Internal DALI_INTERNAL
37 * @brief GlView is a class for rendering with GL
39 * GlView allows drawing with OpenGL.
40 * GlView creates a GL context, a GL surface and a render thread.
41 * The render thread invokes user's callbacks.
45 class DALI_TOOLKIT_API GlView : public Dali::Toolkit::Control
49 * @brief Implementation backend mode
53 enum class BackendMode
56 * DIRECT_RENDERING mode executes GL code within DALi graphics
57 * pipeline. When Renderer is about to be drawn, the callback
58 * will be executed and the custom code "injected" into the pipeline.
59 * This allows rendering directly to the surface rather than offscreen.
66 * DIRECT_RENDERING_THREADED mode executes GL code on separate thread
67 * and then blits the result within DALi graphics commands stream.
68 * The mode is logically compatible with the EGL_IMAGE_OFFSCREEN_RENDERING.
72 DIRECT_RENDERING_THREADED,
75 * EGL_IMAGE_OFFSCREEN_RENDERING mode executes GL code in own thread
76 * and renders to the offscreen NativeImage (EGL) buffer. This backend
77 * will render in parallel but has higher memory footprint and may suffer
78 * performance issues due to using EGL image.
82 EGL_IMAGE_OFFSCREEN_RENDERING,
85 * The default mode is set to EGL_IMAGE_OFFSCREEN_RENDERING for backwards
90 DEFAULT = EGL_IMAGE_OFFSCREEN_RENDERING
94 * @brief Enumeration for rendering mode
96 * This Enumeration is used to choose the rendering mode.
98 * One of them is continuous mode. It is rendered continuously.
99 * The other is on demand mode. It is rendered by application.
103 enum class RenderingMode
105 CONTINUOUS, ///< continuous mode
106 ON_DEMAND ///< on demand by application
110 * @brief Enumeration for Graphics API version
112 * This Enumeration is used to set a GLES version for EGL configuration.
116 enum class GraphicsApiVersion
118 GLES_VERSION_2_0 = 0, ///< GLES version 2.0
119 GLES_VERSION_3_0, ///< GLES version 3.0
123 * @brief Enumeration for color buffer format
125 * This Enumeration is used to set a color buffer format of GlView
129 enum class ColorFormat
131 RGB888, ///< 8 red bits, 8 green bits, 8 blue bits
132 RGBA8888 ///< 8 red bits, 8 green bits, 8 blue bits, alpha 8 bits
136 * @brief Creates a GlView control.
138 * @note This function always creates the GlView with NativeImage backend.
140 * @param[in] colorFormat the format of the color buffer.
141 * @return A handle to a GlView control
145 static GlView New(ColorFormat colorFormat);
148 * @brief Creates a GlView control.
150 * The new GlView will be created with specified backend.
151 * The colorFormat is ignored for DIRECT_RENDERING backend.
153 * @param[in] colorFormat the format of the color buffer.
154 * @param[in] backendMode the backend used by the GlView
155 * @return A handle to a GlView control
159 static GlView New(BackendMode backendMode, ColorFormat colorFormat);
162 * @brief Creates an uninitialized GlView.
171 * This is non-virtual since derived Handle types must not contain data or virtual methods.
178 * @brief Copy constructor.
180 * @param[in] GlView GlView to copy. The copied GlView will point at the same implementation
184 GlView(const GlView& GlView);
187 * @brief Move constructor
189 * @param[in] rhs A reference to the moved handle
193 GlView(GlView&& rhs) noexcept;
196 * @brief Assignment operator.
198 * @param[in] GlView The GlView to assign from
199 * @return A reference to this
203 GlView& operator=(const GlView& GlView);
206 * @brief Move assignment
208 * @param[in] rhs A reference to the moved handle
209 * @return A reference to this
213 GlView& operator=(GlView&& rhs) noexcept;
216 * @brief Downcasts a handle to GlView handle.
218 * If handle points to a GlView, the downcast produces valid handle.
219 * If not, the returned handle is left uninitialized.
221 * @param[in] handle Handle to an object
222 * @return Handle to a GlView or an uninitialized handle
226 static GlView DownCast(BaseHandle handle);
229 * @brief Registers GL callback functions for GlView.
231 * @param[in] initCallback the callback function to create GL resources.
232 * @param[in] renderFrameCallback the callback function to render for the frame.
233 * @param[in] terminateCallback the callback function to clean-up GL resources.
235 * A initCallback of the following type have to be used:
237 * void intializeGL();
239 * This callback will be called before renderFrame callback is called once.
241 * A renderFrameCallback of the following type have to be used:
243 * int renderFrameGL();
245 * If the return value of this callback is not 0, the eglSwapBuffers() will be called.
247 * A terminateCallback of the following type have to be used:
249 * void terminateGL();
251 * This callback is called when GlView is deleted.
253 * @note Ownership of the callbacks is passed onto this class.
254 * <b>You can't call Dali APIs in your callbacks because it is invoked in GlView's own render thread.</b>
255 * And this must be called before adding GlView to the scene.
259 void RegisterGlCallbacks(CallbackBase* initCallback, CallbackBase* renderFrameCallback, CallbackBase* terminateCallback);
262 * @brief Sets the ResizeCallback of the GlView.
263 * When GlView is resized, ResizeCallback would be invoked.
264 * You can get the resized width and height of the GlView.
266 * @param[in] resizeCallback The ResizeCallback function
268 * A resizeCallback of the following type have to be used:
270 * void resizeCallback(int width, int height);
273 * @note Ownership of the callback is passed onto this class.
274 * <b>You can't call Dali APIs in your callback because it is invoked in GlView's own render thread.</b>
275 * And this must be called before adding GlView to the scene.
279 void SetResizeCallback(CallbackBase* resizeCallback);
282 * @brief Sets the rendering mode.
284 * @param[in] mode the rendering mode for GlView
286 * @note The default Rendering mode is CONTINUOUS.
287 * If ON_DEMAND mode is set, it is rendered by RenderOnce()
291 void SetRenderingMode(RenderingMode mode);
294 * @brief Gets the rendering mode.
298 [[nodiscard]] RenderingMode GetRenderingMode() const;
301 * @brief Gets the backend mode.
305 [[nodiscard]] BackendMode GetBackendMode() const;
308 * @brief Sets egl configuration for GlView
310 * @param[in] depth the flag of depth buffer. If the value is true, 24bit depth buffer is enabled.
311 * @param[in] stencil the flag of stencil. If the value is true, 8bit stencil buffer is enabled.
312 * @param[in] msaa the expected sampling number per pixel.
313 * @param[in] version the graphics API version
314 * @return True if the config exists, false otherwise.
318 bool SetGraphicsConfig(bool depth, bool stencil, int msaa, GraphicsApiVersion version);
321 * @brief Renders once more even if GL render functions are not added to idler.
322 * @note Will not work if the window is hidden or GL render functions are added to idler
329 * @brief Binds DALi textures to the callback
331 * The textures that are bound to the callback will be passed upon
332 * callback execution providing native handles (like GL name) so they
333 * can be used alongside with custom GL code.
335 * Binding texture does not affect lifecycle and it's up to the client-side
336 * to make sure the resource is alive when used inside the callback.
338 * @param[in] textures List of DALi textures to be bound to the callback
340 * @note It only supported only in the GlView::BackendMode::DIRECT_RENDERING.
344 void BindTextureResources(std::vector<Dali::Texture> textures);
346 public: // Not intended for application developers
349 * @brief Creates a handle using the Toolkit::Internal implementation.
350 * @param[in] implementation The GlView implementation
354 DALI_INTERNAL GlView(Internal::GlViewImpl& implementation);
357 * @brief Allows the creation of this GlView from an Internal::CustomActor pointer.
358 * @param[in] internal A pointer to the internal CustomActor
362 DALI_INTERNAL GlView(Dali::Internal::CustomActor* internal);
366 } // namespace Toolkit
370 #endif // DALI_TOOLKIT_GL_VIEW_H