1 #ifndef DALI_RENDER_CALLBACK_H
2 #define DALI_RENDER_CALLBACK_H
5 * Copyright (c) 2024 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/public-api/math/matrix.h>
23 #include <dali/public-api/math/rect.h>
24 #include <dali/public-api/math/vector2.h>
25 #include <dali/public-api/rendering/texture.h>
26 #include <dali/public-api/signals/callback.h>
37 * @class RenderCallbackInput
39 * This structure contains data to be passed into the RenderCallback
44 struct DALI_CORE_API RenderCallbackInput
49 Rect<int32_t> clippingBox;
50 std::vector<uint32_t> textureBindings;
52 std::any eglContext; ///< Storage for EGL Context
53 bool usingOwnEglContext; ///< Uses own EGL context (owns GL state), custom code should be aware of it
55 Matrix view; // Added at end to avoid abi break.
59 * @class RenderCallback
61 * RenderCallback is used by the direct rendering feature and allows injecting
62 * custom render code (using native API such as GL directly) into the DALi
63 * rendering stream. The RenderCallback will be executed within own context (unless
64 * native API is context-less) to maintain state separation from DALi render state.
66 * The class wraps CallbackBase object ensuring its type-safe assignment
70 class DALI_CORE_API RenderCallback
75 * @brief Mode of execution of custom rendering code into the pipeline
77 * ISOLATED mode will try to isolate custom rendering so it will start with
78 * clean state of graphics API and won't affect DALi rendering pipeline. This
79 * mode is considered as safe.
81 * UNSAFE mode will inject custom rendering code into DALi rendering
82 * pipeline as is and won't isolate graphics native API state. This mode
83 * should be used with caution and is considered unsafe.
85 * The default mode is ISOLATED.
89 enum class ExecutionMode
92 * @brief Native rendering commands will be isolated from DALi graphics pipline state
93 * @details This mode is default and provides safest way of executing custom graphics API commands.
99 * @brief Native rendering will be injected directly into DALi graphics pipeline
100 * @details This mode is considered unsafe as it's directly injected into DALi rendering pipeline.
101 * It inherits current graphics API state and may alter it.
107 * @brief Default mode is ISOLATED
114 * Templated member function type
117 using FuncType = bool (T::*)(const RenderCallbackInput&);
120 * @brief Constructor of RenderCallback
122 * @param[in] object Object to invoke
123 * @param[in] func Member function to invoke
124 * @param[in] executionMode execution mode of custom code
129 RenderCallback(T* object, FuncType<T> func, ExecutionMode executionMode)
130 : mCallback(MakeCallback(object, func)),
131 mExecutionMode(executionMode)
136 * @brief Creates new instance of RenderCallback
139 * @param[in] object Object to invoke
140 * @param[in] func Member function to invoke
141 * @return Unique pointer to the RenderCallback instance
144 static std::unique_ptr<RenderCallback> New(T* object, FuncType<T> func)
146 return std::make_unique<RenderCallback>(object, func, ExecutionMode::DEFAULT);
150 * @brief Creates new instance of RenderCallback
153 * @param[in] object Object to invoke
154 * @param[in] func Member function to invoke
155 * @param[in] executionMode Execution mode of custom code
156 * @return Unique pointer to the RenderCallback instance
159 static std::unique_ptr<RenderCallback> New(T* object, FuncType<T> func, ExecutionMode executionMode)
161 return std::make_unique<RenderCallback>(object, func, executionMode);
165 * @brief Explicit cast operator
168 * @return casts RenderCallback to CallbackBase object
170 explicit operator CallbackBase*()
172 return mCallback.get();
176 * @brief Binds DALi textures to the callback
178 * The textures that are bound to the callback will be passed upon
179 * callback execution providing native handles (like GL name) so they
180 * can be used alongside with custom GL code.
182 * Binding texture does not affect lifecycle and it's up to the client-side
183 * to make sure the resource is alive when used inside the callback.
185 * @param[in] textures List of DALi textures to be bound to the callback
188 void BindTextureResources(std::vector<Dali::Texture> textures)
190 mTextureResources = std::move(textures);
194 * @brief Returns list of DALi textures bound to the callback
196 * @return list of textures
198 [[nodiscard]] const std::vector<Dali::Texture>& GetTextureResources() const
200 return mTextureResources;
203 * @brief Explicit cast operator
206 * @return casts RenderCallback to CallbackBase object
208 explicit operator CallbackBase&()
214 * @brief Returns input data passed to the callback upon execution
216 * The input data will be filled by the DALi and Graphics backend
217 * providing DALi rendering related data (such as clipping box) as well as
218 * Graphics native API related data (like EGLContext for GL).
221 * @return Valid RenderCallbackInput structure
223 RenderCallbackInput& GetRenderCallbackInput()
225 return mRenderCallbackInput;
229 * @brief Returns execution mode of the callback
232 * @return Valid execution mode
234 [[nodiscard]] ExecutionMode GetExecutionMode() const
236 return mExecutionMode;
240 std::unique_ptr<CallbackBase> mCallback; //< Callback base object
241 RenderCallbackInput mRenderCallbackInput;
242 ExecutionMode mExecutionMode{ExecutionMode::DEFAULT};
243 std::vector<Dali::Texture> mTextureResources{};
247 #endif // DALI_RENDER_CALLBACK_H