17818ab90fba7c4e1b44f381697c7e0d3830eed6
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / renderer-impl.h
1 #ifndef DALI_INTERNAL_RENDERER_H
2 #define DALI_INTERNAL_RENDERER_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/devel-api/rendering/renderer-devel.h>
23 #include <dali/internal/common/blending-options.h>
24 #include <dali/internal/event/common/object-connector.h>    // Dali::Internal::ObjectConnector
25 #include <dali/internal/event/common/object-impl.h>         // Dali::Internal::Object
26 #include <dali/internal/event/rendering/geometry-impl.h>    // Dali::Internal::Geometry
27 #include <dali/internal/event/rendering/texture-set-impl.h> // Dali::Internal::TextureSet
28 #include <dali/internal/render/renderers/render-renderer.h> // Dali::Render::Renderer::StencilParameters
29 #include <dali/public-api/common/dali-common.h>             // DALI_ASSERT_ALWAYS
30 #include <dali/public-api/common/intrusive-ptr.h>           // Dali::IntrusivePtr
31 #include <dali/public-api/rendering/renderer.h>             // Dali::Renderer
32
33 namespace Dali
34 {
35 namespace Internal
36 {
37 namespace SceneGraph
38 {
39 class Renderer;
40 }
41
42 class Renderer;
43 using RendererPtr = IntrusivePtr<Renderer>;
44
45 /**
46  * Renderer is an object that can be used to show content by combining a Geometry with a shader and textures.
47  */
48 class Renderer : public Object
49 {
50 public:
51   /**
52    * Create a new Renderer.
53    * @return A smart-pointer to the newly allocated Renderer.
54    */
55   static RendererPtr New();
56
57   /**
58    * @copydoc Dali::Renderer::SetGeometry()
59    */
60   void SetGeometry(Geometry& geometry);
61
62   /**
63    * @copydoc Dali::Renderer::GetGeometry()
64    */
65   GeometryPtr GetGeometry() const;
66
67   /**
68    * @copydoc Dali::Renderer::SetTextures()
69    */
70   void SetTextures(TextureSet& textureSet);
71
72   /**
73    * @copydoc Dali::Renderer::GetTextures()
74    */
75   TextureSetPtr GetTextures() const;
76
77   /**
78    * @copydoc Dali::Renderer::SetShader()
79    */
80   void SetShader(Shader& shader);
81
82   /**
83    * @copydoc Dali::Renderer::GetShader()
84    */
85   ShaderPtr GetShader() const;
86
87   /**
88    * @copydoc Dali::Renderer::SetDepthIndex()
89    */
90   void SetDepthIndex(int32_t depthIndex);
91
92   /**
93    * @copydoc Dali::Renderer::GetDepthIndex()
94    */
95   int32_t GetDepthIndex() const;
96
97   /**
98    * @copydoc Dali::Renderer::SetBlendMode()
99    */
100   void SetBlendMode(BlendMode::Type mode);
101
102   /**
103    * @copydoc Dali::Renderer::GetBlendMode()
104    */
105   BlendMode::Type GetBlendMode() const;
106
107   /**
108    * @copydoc Dali::Renderer::SetBlendFunc()
109    */
110   void SetBlendFunc(BlendFactor::Type srcFactorRgba, BlendFactor::Type destFactorRgba);
111
112   /**
113    * @copydoc Dali::Renderer::SetBlendFunc()
114    */
115   void SetBlendFunc(BlendFactor::Type srcFactorRgb, BlendFactor::Type destFactorRgb, BlendFactor::Type srcFactorAlpha, BlendFactor::Type destFactorAlpha);
116
117   /**
118    * @copydoc Dali::Renderer::GetBlendFunc()
119    */
120   void GetBlendFunc(BlendFactor::Type& srcFactorRgb, BlendFactor::Type& destFactorRgb, BlendFactor::Type& srcFactorAlpha, BlendFactor::Type& destFactorAlpha) const;
121
122   /**
123    * @brief Set same Blend Equation for the RGB and alpha
124    */
125   void SetBlendEquation(DevelBlendEquation::Type equationRgba);
126
127   /**
128    * @brief Set Blend Equation separately for the RGB and alpha
129    */
130   void SetBlendEquation(DevelBlendEquation::Type equationRgb, DevelBlendEquation::Type equationAlpha);
131
132   /**
133    * @brief Get Blend Equation of rgb and alpha
134    */
135   void GetBlendEquation(DevelBlendEquation::Type& equationRgb, DevelBlendEquation::Type& equationAlpha) const;
136
137   /**
138    * @copydoc Dali::Renderer::SetIndexedDrawFirstElement
139    */
140   void SetIndexedDrawFirstElement(uint32_t firstElement);
141
142   /**
143    * @copydoc Dali::Renderer::SetIndexedDrawElementsCount
144    */
145   void SetIndexedDrawElementsCount(uint32_t elementsCount);
146
147   /**
148    * @brief Set whether the Pre-multiplied Alpha Blending is required
149    *
150    * @param[in] preMultipled whether alpha is pre-multiplied.
151    */
152   void EnablePreMultipliedAlpha(bool preMultipled);
153
154   /**
155    * @brief Query whether alpha is pre-multiplied.
156    *
157    * @return True is alpha is pre-multiplied, false otherwise.
158    */
159   bool IsPreMultipliedAlphaEnabled() const;
160
161   /**
162    * @brief Query whether Blend Equation Advanced is used in this renderer
163    *
164    * @return True is Blend Equation Advanced is used, false otherwise.
165    */
166   bool IsAdvancedBlendEquationApplied() const;
167
168   /**
169    * @brief Get the scene graph object
170    *
171    * @return the scene object
172    */
173   const SceneGraph::Renderer& GetRendererSceneObject() const;
174
175 public: // Default property extensions from Object
176   /**
177    * @copydoc Dali::Internal::Object::SetDefaultProperty()
178    */
179   void SetDefaultProperty(Property::Index index, const Property::Value& propertyValue) override;
180
181   /**
182    * @copydoc Dali::Internal::Object::GetDefaultProperty()
183    */
184   Property::Value GetDefaultProperty(Property::Index index) const override;
185
186   /**
187    * @copydoc Dali::Internal::Object::GetDefaultPropertyCurrentValue()
188    */
189   Property::Value GetDefaultPropertyCurrentValue(Property::Index index) const override;
190
191   /**
192     * @copydoc Dali::Internal::Object::OnNotifyDefaultPropertyAnimation()
193     */
194   void OnNotifyDefaultPropertyAnimation(Animation& animation, Property::Index index, const Property::Value& value, Animation::Type animationType) override;
195
196   /**
197    * @copydoc Dali::Internal::Object::GetSceneObjectAnimatableProperty()
198    */
199   const SceneGraph::PropertyBase* GetSceneObjectAnimatableProperty(Property::Index index) const override;
200
201   /**
202    * @copydoc Dali::Internal::Object::GetSceneObjectInputProperty()
203    */
204   const PropertyInputImpl* GetSceneObjectInputProperty(Property::Index index) const override;
205
206   /**
207    * @brief Adds a draw command to the Renderer.
208    * DrawCommands override Renderer's default behaviour.
209    *
210    * @param[in] command Valid reference to a DrawCommand objects
211    */
212   void AddDrawCommand(const Dali::DevelRenderer::DrawCommand& command);
213
214 protected: // implementation
215   /**
216    * @brief Constructor.
217    *
218    * @param sceneObject the scene graph renderer
219    */
220   Renderer(const SceneGraph::Renderer* sceneObject);
221
222   /**
223    * A reference counted object may only be deleted by calling Unreference()
224    */
225   ~Renderer() override;
226
227 private:
228   Renderer(const Renderer&) = delete;            ///< Deleted copy constructor
229   Renderer& operator=(const Renderer&) = delete; ///< Deleted assignment operator
230
231   /**
232    * @brief Retrieves the cached event side value of a default property.
233    * @param[in]  index  The index of the property
234    * @param[out] value  Is set with the cached value of the property if found.
235    * @return True if value set, false otherwise.
236    */
237   bool GetCachedPropertyValue(Property::Index index, Property::Value& value) const;
238
239   /**
240    * @brief Retrieves the current value of a default property from the scene-graph.
241    * @param[in]  index  The index of the property
242    * @param[out] value  Is set with the current scene-graph value of the property
243    * @return True if value set, false otherwise.
244    */
245   bool GetCurrentPropertyValue(Property::Index index, Property::Value& value) const;
246
247   /**
248    * @brief Sets the blend color.
249    * @param[in] blendColor The blend color to set.
250    */
251   void SetBlendColor(const Vector4& blendColor);
252
253   /**
254    * @brief Retrieves the blend-color.
255    * @return A const reference to the blend-color
256    */
257   const Vector4& GetBlendColor() const;
258
259 protected:
260   GeometryPtr   mGeometry;   ///< Intrusive pointer to the geometry used by this renderer
261   TextureSetPtr mTextureSet; ///< Intrusive pointer to the texture set used by this renderer
262   ShaderPtr     mShader;     ///< Intrusive pointer to the shader used by this renderer
263
264   int32_t mDepthIndex;
265
266   uint32_t mIndexedDrawFirstElement; ///< Offset of first element to draw from bound index buffer
267   uint32_t mIndexedDrawElementCount; ///< Number of elements to draw
268
269   Render::Renderer::StencilParameters mStencilParameters; ///< Struct containing all stencil related options
270   BlendingOptions                     mBlendingOptions;   ///< Local copy of blending options bitmask
271
272   float                          mOpacity;                      ///< Local copy of the opacity
273   DepthFunction::Type            mDepthFunction : 4;            ///< Local copy of the depth function
274   FaceCullingMode::Type          mFaceCullingMode : 3;          ///< Local copy of the mode of face culling
275   BlendMode::Type                mBlendMode : 3;                ///< Local copy of the mode of blending
276   DepthWriteMode::Type           mDepthWriteMode : 3;           ///< Local copy of the depth write mode
277   DepthTestMode::Type            mDepthTestMode : 3;            ///< Local copy of the depth test mode
278   DevelRenderer::Rendering::Type mRenderingBehavior : 2;        ///< The rendering behavior
279   bool                           mPremultipledAlphaEnabled : 1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
280
281 private:
282   std::vector<Dali::DevelRenderer::DrawCommand> mDrawCommands; ///< list of draw commands
283 };
284
285 } // namespace Internal
286
287 // Helpers for public-api forwarding methods
288 inline Internal::Renderer& GetImplementation(Dali::Renderer& handle)
289 {
290   DALI_ASSERT_ALWAYS(handle && "Renderer handle is empty");
291
292   BaseObject& object = handle.GetBaseObject();
293
294   return static_cast<Internal::Renderer&>(object);
295 }
296
297 inline const Internal::Renderer& GetImplementation(const Dali::Renderer& handle)
298 {
299   DALI_ASSERT_ALWAYS(handle && "Renderer handle is empty");
300
301   const BaseObject& object = handle.GetBaseObject();
302
303   return static_cast<const Internal::Renderer&>(object);
304 }
305
306 } // namespace Dali
307
308 #endif // DALI_INTERNAL_RENDERER_H