Merge branch 'devel/master' into tizen
[platform/core/uifw/dali-core.git] / dali / public-api / signals / render-callback.h
1 #ifndef DALI_RENDER_CALLBACK_H
2 #define DALI_RENDER_CALLBACK_H
3
4 /*
5  * Copyright (c) 2022 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 // INTERNAL INCLUDES
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>
27
28 // EXTERNAL INCLUDES
29 #include <any>
30 #include <memory>
31 #include <utility>
32 #include <vector>
33
34 namespace Dali
35 {
36 /**
37  * @class RenderCallbackInput
38  *
39  * This structure contains data to be passed into the RenderCallback
40  * functor.
41  *
42  * RenderCallbackInput inherits from Graphics::NativeDrawInput
43  *
44  * @SINCE_2_1.30
45  */
46 struct DALI_CORE_API RenderCallbackInput
47 {
48   Matrix                mvp;
49   Matrix                projection;
50   Size                  size;
51   Rect<int32_t>         clippingBox;
52   std::vector<uint32_t> textureBindings;
53
54   std::any eglContext; ///< Storage for EGL Context
55 };
56
57 /**
58  * @class RenderCallback
59  *
60  * RenderCallback is used by the direct rendering feature and allows injecting
61  * custom render code (using native API such as GL directly) into the DALi
62  * rendering stream. The RenderCallback will be executed within own context (unless
63  * native API is context-less) to maintain state separation from DALi render state.
64  *
65  * The class wraps CallbackBase object ensuring its type-safe assignment
66  *
67  * @SINCE_2_1.14
68  */
69 class DALI_CORE_API RenderCallback
70 {
71 public:
72   /**
73    * Templated member function type
74    */
75   template<class T>
76   using FuncType = bool (T::*)(const RenderCallbackInput&);
77
78   /**
79    * @brief Constructor of RenderCallback
80    *
81    * @param[in] object Object to invoke
82    * @param[in] func Member function to invoke
83    *
84    * @SINCE_2_1.14
85    */
86   template<class T>
87   RenderCallback(T* object, FuncType<T> func)
88   : mCallback(MakeCallback(object, func))
89   {
90   }
91
92   /**
93    * @brief Creates new instance of RenderCallback
94    *
95    * @SINCE_2_1.14
96    * @param[in] object Object to invoke
97    * @param[in] func Member function to invoke
98    * @return Unique pointer to the RenderCallback instance
99    */
100   template<class T>
101   static std::unique_ptr<RenderCallback> New(T* object, FuncType<T> func)
102   {
103     return std::make_unique<RenderCallback>(object, func);
104   }
105
106   /**
107    * @brief Explicit cast operator
108    *
109    * @SINCE_2_1.14
110    * @return casts RenderCallback to CallbackBase object
111    */
112   explicit operator CallbackBase*()
113   {
114     return mCallback.get();
115   }
116
117   /**
118    * @brief Binds DALi textures to the callback
119    *
120    * The textures that are bound to the callback will be passed upon
121    * callback execution providing native handles (like GL name) so they
122    * can be used alongside with custom GL code.
123    *
124    * Binding texture does not affect lifecycle and it's up to the client-side
125    * to make sure the resource is alive when used inside the callback.
126    *
127    * @param[in] textures List of DALi textures to be bound to the callback
128    * @SINCE_2_1.30
129    */
130   void BindTextureResources(std::vector<Dali::Texture> textures)
131   {
132     mTextureResources = std::move(textures);
133   }
134
135   /**
136    * @brief Returns list of DALi textures bound to the callback
137    *
138    * @return list of textures
139    */
140   [[nodiscard]] const std::vector<Dali::Texture>& GetTextureResources() const
141   {
142     return mTextureResources;
143   }
144   /**
145    * @brief Explicit cast operator
146    *
147    * @SINCE_2_1.14
148    * @return casts RenderCallback to CallbackBase object
149    */
150   explicit operator CallbackBase&()
151   {
152     return *mCallback;
153   }
154
155   /**
156    * @brief Returns input data passed to the callback upon execution
157    *
158    * The input data will be filled by the DALi and Graphics backend
159    * providing DALi rendering related data (such as clipping box) as well as
160    * Graphics native API related data (like EGLContext for GL).
161    *
162    * @SINCE_2_1.30
163    * @return Valid RenderCallbackInput structure
164    */
165   RenderCallbackInput& GetRenderCallbackInput()
166   {
167     return mRenderCallbackInput;
168   }
169
170 private:
171   std::unique_ptr<CallbackBase> mCallback; //< Callback base object
172   RenderCallbackInput           mRenderCallbackInput;
173
174   std::vector<Dali::Texture> mTextureResources{};
175 };
176 } // namespace Dali
177
178 #endif // DALI_RENDER_CALLBACK_H