(Renderer) Fix memory leak when using blend-color
[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) 2017 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/common/dali-common.h> // DALI_ASSERT_ALWAYS
23 #include <dali/public-api/common/intrusive-ptr.h> // Dali::IntrusivePtr
24 #include <dali/public-api/rendering/renderer.h> // Dali::Renderer
25 #include <dali/internal/common/blending-options.h>
26 #include <dali/internal/event/common/object-connector.h> // Dali::Internal::ObjectConnector
27 #include <dali/internal/event/common/object-impl.h> // Dali::Internal::Object
28 #include <dali/internal/event/rendering/texture-set-impl.h> // Dali::Internal::TextureSet
29 #include <dali/internal/event/rendering/geometry-impl.h> // Dali::Internal::Geometry
30 #include <dali/internal/render/renderers/render-renderer.h> // Dali::Render::Renderer::StencilParameters
31
32 namespace Dali
33 {
34 namespace Internal
35 {
36 namespace SceneGraph
37 {
38 class Renderer;
39 }
40
41 class Renderer;
42 typedef IntrusivePtr<Renderer> RendererPtr;
43
44 /**
45  * Renderer is an object that can be used to show content by combining a Geometry with a shader and textures.
46  */
47 class Renderer : public Object
48 {
49 public:
50
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   Geometry* GetGeometry() const;
66
67   /**
68    * @copydoc Dali::Renderer::SetTextures()
69    */
70   void SetTextures( TextureSet& textureSet );
71
72   /**
73    * @copydoc Dali::Renderer::GetTextures()
74    */
75   TextureSet* GetTextures() const;
76
77   /**
78    * @copydoc Dali::Renderer::SetShader()
79    */
80   void SetShader( Shader& shader );
81
82   /**
83    * @copydoc Dali::Renderer::GetShader()
84    */
85   Shader* GetShader() const;
86
87   /**
88    * @copydoc Dali::Renderer::SetDepthIndex()
89    */
90   void SetDepthIndex( int depthIndex );
91
92   /**
93    * @copydoc Dali::Renderer::GetDepthIndex()
94    */
95   int 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,
116                       BlendFactor::Type srcFactorAlpha, BlendFactor::Type destFactorAlpha );
117
118    /**
119     * @copydoc Dali::Renderer::GetBlendFunc()
120     */
121    void GetBlendFunc( BlendFactor::Type& srcFactorRgb,   BlendFactor::Type& destFactorRgb,
122                       BlendFactor::Type& srcFactorAlpha, BlendFactor::Type& destFactorAlpha ) const;
123
124    /**
125     * @copydoc Dali::Renderer::SetBlendEquation()
126     */
127    void SetBlendEquation( BlendEquation::Type equationRgba );
128
129    /**
130     * @copydoc Dali::Renderer::SetBlendEquation()
131     */
132    void SetBlendEquation( BlendEquation::Type equationRgb, BlendEquation::Type equationAlpha );
133
134    /**
135     * @copydoc Dali::Renderer::GetBlendEquation()
136     */
137    void GetBlendEquation( BlendEquation::Type& equationRgb, BlendEquation::Type& equationAlpha ) const;
138
139    /**
140     * @copydoc Dali::Renderer::SetIndexedDrawFirstElement
141     */
142    void SetIndexedDrawFirstElement( size_t firstElement );
143
144    /**
145     * @copydoc Dali::Renderer::SetIndexedDrawElementsCount
146     */
147    void SetIndexedDrawElementsCount( size_t elementsCount );
148
149    /**
150     * @brief Set whether the Pre-multiplied Alpha Blending is required
151     *
152     * @param[in] preMultipled whether alpha is pre-multiplied.
153     */
154    void EnablePreMultipliedAlpha( bool preMultipled );
155
156    /**
157     * @brief Query whether alpha is pre-multiplied.
158     *
159     * @return True is alpha is pre-multiplied, false otherwise.
160     */
161    bool IsPreMultipliedAlphaEnabled() const;
162
163    /**
164     * @brief Get the scene graph object
165     *
166     * @return the scene object
167     */
168    SceneGraph::Renderer* GetRendererSceneObject();
169
170 public: // Default property extensions from Object
171
172   /**
173    * @copydoc Dali::Internal::Object::GetDefaultPropertyCount()
174    */
175   virtual unsigned int GetDefaultPropertyCount() const;
176
177   /**
178    * @copydoc Dali::Internal::Object::GetDefaultPropertyIndices()
179    */
180   virtual void GetDefaultPropertyIndices( Property::IndexContainer& indices ) const;
181
182   /**
183    * @copydoc Dali::Internal::Object::GetDefaultPropertyName()
184    */
185   virtual const char* GetDefaultPropertyName(Property::Index index) const;
186
187   /**
188    * @copydoc Dali::Internal::Object::GetDefaultPropertyIndex()
189    */
190   virtual Property::Index GetDefaultPropertyIndex(const std::string& name) const;
191
192   /**
193    * @copydoc Dali::Internal::Object::IsDefaultPropertyWritable()
194    */
195   virtual bool IsDefaultPropertyWritable(Property::Index index) const;
196
197   /**
198    * @copydoc Dali::Internal::Object::IsDefaultPropertyAnimatable()
199    */
200   virtual bool IsDefaultPropertyAnimatable(Property::Index index) const;
201
202   /**
203    * @copydoc Dali::Internal::Object::IsDefaultPropertyAConstraintInput()
204    */
205   virtual bool IsDefaultPropertyAConstraintInput( Property::Index index ) const;
206
207   /**
208    * @copydoc Dali::Internal::Object::GetDefaultPropertyType()
209    */
210   virtual Property::Type GetDefaultPropertyType(Property::Index index) const;
211
212   /**
213    * @copydoc Dali::Internal::Object::SetDefaultProperty()
214    */
215   virtual void SetDefaultProperty(Property::Index index, const Property::Value& propertyValue);
216
217   /**
218    * @copydoc Dali::Internal::Object::SetSceneGraphProperty()
219    */
220   virtual void SetSceneGraphProperty( Property::Index index, const PropertyMetadata& entry, const Property::Value& value );
221
222   /**
223    * @copydoc Dali::Internal::Object::GetDefaultProperty()
224    */
225   virtual Property::Value GetDefaultProperty( Property::Index index ) const;
226
227   /**
228    * @copydoc Dali::Internal::Object::GetPropertyOwner()
229    */
230   virtual const SceneGraph::PropertyOwner* GetPropertyOwner() const;
231
232   /**
233    * @copydoc Dali::Internal::Object::GetSceneObject()
234    */
235   virtual const SceneGraph::PropertyOwner* GetSceneObject() const;
236
237   /**
238    * @copydoc Dali::Internal::Object::GetSceneObjectAnimatableProperty()
239    */
240   virtual const SceneGraph::PropertyBase* GetSceneObjectAnimatableProperty( Property::Index index ) const;
241
242   /**
243    * @copydoc Dali::Internal::Object::GetSceneObjectInputProperty()
244    */
245   virtual const PropertyInputImpl* GetSceneObjectInputProperty( Property::Index index ) const;
246
247   /**
248    * @copydoc Dali::Internal::Object::GetPropertyComponentIndex()
249    */
250   virtual int GetPropertyComponentIndex( Property::Index index ) const;
251
252 private: // implementation
253
254   /**
255    * @brief Default constructor.
256    */
257   Renderer();
258
259   /**
260    * @brief Initializes the Renderer.
261    */
262   void Initialize();
263
264   /**
265    * @brief Sets the blend color.
266    * @param[in] blendColor The blend color to set.
267    */
268   void SetBlendColor( const Vector4& blendColor );
269
270   /**
271    * @brief Retrieves the blend-color.
272    * @return A const reference to the blend-color
273    */
274   const Vector4& GetBlendColor() const;
275
276 protected:
277   /**
278    * A reference counted object may only be deleted by calling Unreference()
279    */
280   virtual ~Renderer();
281
282 private: // unimplemented methods
283   Renderer( const Renderer& );
284   Renderer& operator=( const Renderer& );
285
286 private: // data
287   SceneGraph::Renderer*               mSceneObject;
288   GeometryPtr                         mGeometry;                     ///< Connector that holds the geometry used by this renderer
289   TextureSetPtr                       mTextureSet;                   ///< Connector that holds the texture set used by this renderer
290   ShaderPtr                           mShader;                       ///< Connector that holds the shader used by this renderer
291
292   int                                 mDepthIndex;
293
294   size_t                              mIndexedDrawFirstElement;      ///< Offset of first element to draw from bound index buffer
295   size_t                              mIndexedDrawElementCount;      ///< Number of elements to draw
296
297   Render::Renderer::StencilParameters mStencilParameters;            ///< Struct containing all stencil related options
298   BlendingOptions                     mBlendingOptions;              ///< Local copy of blending options bitmask
299
300   DepthFunction::Type                 mDepthFunction:3;              ///< Local copy of the depth function
301   FaceCullingMode::Type               mFaceCullingMode:2;            ///< Local copy of the mode of face culling
302   BlendMode::Type                     mBlendMode:2;                  ///< Local copy of the mode of blending
303   DepthWriteMode::Type                mDepthWriteMode:2;             ///< Local copy of the depth write mode
304   DepthTestMode::Type                 mDepthTestMode:2;              ///< Local copy of the depth test mode
305   bool                                mPremultipledAlphaEnabled:1;   ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
306 };
307
308 } // namespace Internal
309
310 // Helpers for public-api forwarding methods
311 inline Internal::Renderer& GetImplementation( Dali::Renderer& handle )
312 {
313   DALI_ASSERT_ALWAYS(handle && "Renderer handle is empty");
314
315   BaseObject& object = handle.GetBaseObject();
316
317   return static_cast<Internal::Renderer&>(object);
318 }
319
320 inline const Internal::Renderer& GetImplementation( const Dali::Renderer& handle )
321 {
322   DALI_ASSERT_ALWAYS(handle && "Renderer handle is empty");
323
324   const BaseObject& object = handle.GetBaseObject();
325
326   return static_cast<const Internal::Renderer&>(object);
327 }
328
329 } // namespace Dali
330
331 #endif // DALI_INTERNAL_RENDERER_H