(Renderer) Fix memory leak when using blend-color
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-renderer.h
1 #ifndef DALI_INTERNAL_RENDER_RENDERER_H
2 #define DALI_INTERNAL_RENDER_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/math/matrix.h>
23 #include <dali/public-api/math/vector4.h>
24 #include <dali/public-api/rendering/texture-set.h>
25 #include <dali/internal/common/blending-options.h>
26 #include <dali/internal/common/message.h>
27 #include <dali/internal/common/type-abstraction-enums.h>
28 #include <dali/internal/event/common/property-input-impl.h>
29 #include <dali/internal/render/data-providers/render-data-provider.h>
30 #include <dali/internal/render/gl-resources/gl-resource-owner.h>
31 #include <dali/internal/render/renderers/render-geometry.h>
32 #include <dali/internal/update/manager/render-instruction-processor.h>
33 #include <dali/integration-api/debug.h>
34
35 namespace Dali
36 {
37
38 namespace Internal
39 {
40 class Context;
41 class Texture;
42 class Program;
43
44 namespace SceneGraph
45 {
46 class SceneController;
47 class Shader;
48 class NodeDataProvider;
49 }
50
51 namespace Render
52 {
53
54 /**
55  * Renderers are used to render meshes
56  * These objects are used during RenderManager::Render(), so properties modified during
57  * the Update must either be double-buffered, or set via a message added to the RenderQueue.
58  */
59 class Renderer : public GlResourceOwner
60 {
61 public:
62
63   /**
64    * @brief Struct to encapsulate stencil parameters required for control of the stencil buffer.
65    */
66   struct StencilParameters
67   {
68     StencilParameters( RenderMode::Type renderMode, StencilFunction::Type stencilFunction, int stencilFunctionMask,
69                        int stencilFunctionReference, int stencilMask, StencilOperation::Type stencilOperationOnFail,
70                        StencilOperation::Type stencilOperationOnZFail, StencilOperation::Type stencilOperationOnZPass )
71     : stencilFunctionMask      ( stencilFunctionMask      ),
72       stencilFunctionReference ( stencilFunctionReference ),
73       stencilMask              ( stencilMask              ),
74       renderMode               ( renderMode               ),
75       stencilFunction          ( stencilFunction          ),
76       stencilOperationOnFail   ( stencilOperationOnFail   ),
77       stencilOperationOnZFail  ( stencilOperationOnZFail  ),
78       stencilOperationOnZPass  ( stencilOperationOnZPass  )
79     {
80     }
81
82     int stencilFunctionMask;                          ///< The stencil function mask
83     int stencilFunctionReference;                     ///< The stencil function reference
84     int stencilMask;                                  ///< The stencil mask
85     RenderMode::Type       renderMode:3;              ///< The render mode
86     StencilFunction::Type  stencilFunction:3;         ///< The stencil function
87     StencilOperation::Type stencilOperationOnFail:3;  ///< The stencil operation for stencil test fail
88     StencilOperation::Type stencilOperationOnZFail:3; ///< The stencil operation for depth test fail
89     StencilOperation::Type stencilOperationOnZPass:3; ///< The stencil operation for depth test pass
90   };
91
92   /**
93    * @copydoc Dali::Internal::GlResourceOwner::GlContextDestroyed()
94    */
95   void GlContextDestroyed();
96
97   /**
98    * @copydoc Dali::Internal::GlResourceOwner::GlCleanup()
99    */
100   void GlCleanup();
101
102   /**
103    * Create a new renderer instance
104    * @param[in] dataProviders The data providers for the renderer
105    * @param[in] geometry The geometry for the renderer
106    * @param[in] blendingBitmask A bitmask of blending options.
107    * @param[in] blendColor The blend color to pass to GL
108    * @param[in] faceCullingMode The face-culling mode.
109    * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
110    * @param[in] depthWriteMode Depth buffer write mode
111    * @param[in] depthTestMode Depth buffer test mode
112    * @param[in] depthFunction Depth function
113    * @param[in] stencilParameters Struct containing all stencil related options
114    */
115   static Renderer* New( SceneGraph::RenderDataProvider* dataProviders,
116                         Render::Geometry* geometry,
117                         unsigned int blendingBitmask,
118                         const Vector4& blendColor,
119                         FaceCullingMode::Type faceCullingMode,
120                         bool preMultipliedAlphaEnabled,
121                         DepthWriteMode::Type depthWriteMode,
122                         DepthTestMode::Type depthTestMode,
123                         DepthFunction::Type depthFunction,
124                         StencilParameters& stencilParameters );
125
126   /**
127    * Constructor.
128    * @param[in] dataProviders The data providers for the renderer
129    * @param[in] geometry The geometry for the renderer
130    * @param[in] blendingBitmask A bitmask of blending options.
131    * @param[in] blendColor The blend color to pass to GL
132    * @param[in] faceCullingMode The face-culling mode.
133    * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
134    * @param[in] depthWriteMode Depth buffer write mode
135    * @param[in] depthTestMode Depth buffer test mode
136    * @param[in] depthFunction Depth function
137    * @param[in] stencilParameters Struct containing all stencil related options
138    */
139   Renderer( SceneGraph::RenderDataProvider* dataProviders,
140             Render::Geometry* geometry,
141             unsigned int blendingBitmask,
142             const Vector4& blendColor,
143             FaceCullingMode::Type faceCullingMode,
144             bool preMultipliedAlphaEnabled,
145             DepthWriteMode::Type depthWriteMode,
146             DepthTestMode::Type depthTestMode,
147             DepthFunction::Type depthFunction,
148             StencilParameters& stencilParameters );
149
150   /**
151    * Change the data providers of the renderer
152    * @param[in] dataProviders The data providers
153    */
154   void SetRenderDataProvider( SceneGraph::RenderDataProvider* dataProviders );
155
156   /**
157    * Change the geometry used by the renderer
158    * @param[in] geometry The new geometry
159    */
160   void SetGeometry( Render::Geometry* geometry );
161
162   /**
163    * Second-phase construction.
164    * This is called when the renderer is inside render thread
165    * @param[in] context Context used by the renderer
166    */
167   void Initialize( Context& context );
168
169   /**
170    * Destructor
171    */
172   ~Renderer();
173
174   /**
175    * Set the face-culling mode.
176    * @param[in] mode The face-culling mode.
177    */
178   void SetFaceCullingMode( FaceCullingMode::Type mode );
179
180   /**
181    * Set the bitmask for blending options
182    * @param[in] bitmask A bitmask of blending options.
183    */
184   void SetBlendingBitMask( unsigned int bitmask );
185
186   /**
187    * Set the blend color for blending options
188    * @param[in] blendColor The blend color to pass to GL
189    */
190   void SetBlendColor( const Vector4& color );
191
192   /**
193    * Set the first element index to draw by the indexed draw
194    * @param[in] firstElement index of first element to draw
195    */
196   void SetIndexedDrawFirstElement( size_t firstElement );
197
198   /**
199    * Set the number of elements to draw by the indexed draw
200    * @param[in] elementsCount number of elements to draw
201    */
202   void SetIndexedDrawElementsCount( size_t elementsCount );
203
204   /**
205    * @brief Set whether the Pre-multiplied Alpha Blending is required
206    *
207    * @param[in] preMultipled whether alpha is pre-multiplied.
208    */
209   void EnablePreMultipliedAlpha( bool preMultipled );
210
211   /**
212    * Sets the depth write mode
213    * @param[in] depthWriteMode The depth write mode
214    */
215   void SetDepthWriteMode( DepthWriteMode::Type depthWriteMode );
216
217   /**
218    * Query the Renderer's depth write mode
219    * @return The renderer depth write mode
220    */
221   DepthWriteMode::Type GetDepthWriteMode() const;
222
223   /**
224    * Sets the depth test mode
225    * @param[in] depthTestMode The depth test mode
226    */
227   void SetDepthTestMode( DepthTestMode::Type depthTestMode );
228
229   /**
230    * Query the Renderer's depth test mode
231    * @return The renderer depth test mode
232    */
233   DepthTestMode::Type GetDepthTestMode() const;
234
235   /**
236    * Sets the depth function
237    * @param[in] depthFunction The depth function
238    */
239   void SetDepthFunction( DepthFunction::Type depthFunction );
240
241   /**
242    * Query the Renderer's depth function
243    * @return The renderer depth function
244    */
245   DepthFunction::Type GetDepthFunction() const;
246
247   /**
248    * Sets the render mode
249    * @param[in] renderMode The render mode
250    */
251   void SetRenderMode( RenderMode::Type mode );
252
253   /**
254    * Gets the render mode
255    * @return The render mode
256    */
257   RenderMode::Type GetRenderMode() const;
258
259   /**
260    * Sets the stencil function
261    * @param[in] stencilFunction The stencil function
262    */
263   void SetStencilFunction( StencilFunction::Type stencilFunction );
264
265   /**
266    * Gets the stencil function
267    * @return The stencil function
268    */
269   StencilFunction::Type GetStencilFunction() const;
270
271   /**
272    * Sets the stencil function mask
273    * @param[in] stencilFunctionMask The stencil function mask
274    */
275   void SetStencilFunctionMask( int stencilFunctionMask );
276
277   /**
278    * Gets the stencil function mask
279    * @return The stencil function mask
280    */
281   int GetStencilFunctionMask() const;
282
283   /**
284    * Sets the stencil function reference
285    * @param[in] stencilFunctionReference The stencil function reference
286    */
287   void SetStencilFunctionReference( int stencilFunctionReference );
288
289   /**
290    * Gets the stencil function reference
291    * @return The stencil function reference
292    */
293   int GetStencilFunctionReference() const;
294
295   /**
296    * Sets the stencil mask
297    * @param[in] stencilMask The stencil mask
298    */
299   void SetStencilMask( int stencilMask );
300
301   /**
302    * Gets the stencil mask
303    * @return The stencil mask
304    */
305   int GetStencilMask() const;
306
307   /**
308    * Sets the stencil operation for when the stencil test fails
309    * @param[in] stencilOperationOnFail The stencil operation
310    */
311   void SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail );
312
313   /**
314    * Gets the stencil operation for when the stencil test fails
315    * @return The stencil operation
316    */
317   StencilOperation::Type GetStencilOperationOnFail() const;
318
319   /**
320    * Sets the stencil operation for when the depth test fails
321    * @param[in] stencilOperationOnZFail The stencil operation
322    */
323   void SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail );
324
325   /**
326    * Gets the stencil operation for when the depth test fails
327    * @return The stencil operation
328    */
329   StencilOperation::Type GetStencilOperationOnZFail() const;
330
331   /**
332    * Sets the stencil operation for when the depth test passes
333    * @param[in] stencilOperationOnZPass The stencil operation
334    */
335   void SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass );
336
337   /**
338    * Gets the stencil operation for when the depth test passes
339    * @return The stencil operation
340    */
341   StencilOperation::Type GetStencilOperationOnZPass() const;
342
343   /**
344    * Called to render during RenderManager::Render().
345    * @param[in] context The context used for rendering
346    * @param[in] bufferIndex The index of the previous update buffer.
347    * @param[in] node The node using this renderer
348    * @param[in] defaultShader in case there is no custom shader
349    * @param[in] modelViewMatrix The model-view matrix.
350    * @param[in] viewMatrix The view matrix.
351    * @param[in] projectionMatrix The projection matrix.
352    * @param[in] size Size of the render item
353    * @param[in] blend If true, blending is enabled
354    */
355   void Render( Context& context,
356                BufferIndex bufferIndex,
357                const SceneGraph::NodeDataProvider& node,
358                SceneGraph::Shader& defaultShader,
359                const Matrix& modelMatrix,
360                const Matrix& modelViewMatrix,
361                const Matrix& viewMatrix,
362                const Matrix& projectionMatrix,
363                const Vector3& size,
364                bool blend );
365
366   /**
367    * Write the renderer's sort attributes to the passed in reference
368    *
369    * @param[in] bufferIndex The current update buffer index.
370    * @param[out] sortAttributes
371    */
372   void SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes ) const;
373
374 private:
375
376   struct UniformIndexMap;
377
378   // Undefined
379   Renderer( const Renderer& );
380
381   // Undefined
382   Renderer& operator=( const Renderer& rhs );
383
384   /**
385    * Sets blending options
386    * @param context to use
387    * @param blend Wheter blending should be enabled or not
388    */
389   void SetBlending( Context& context, bool blend );
390
391   /**
392    * Set the uniforms from properties according to the uniform map
393    * @param[in] bufferIndex The index of the previous update buffer.
394    * @param[in] node The node using the renderer
395    * @param[in] size The size of the renderer
396    * @param[in] program The shader program on which to set the uniforms.
397    */
398   void SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program );
399
400   /**
401    * Set the program uniform in the map from the mapped property
402    * @param[in] bufferIndex The index of the previous update buffer.
403    * @param[in] program The shader program
404    * @param[in] map The uniform
405    */
406   void SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map );
407
408   /**
409    * Bind the textures and setup the samplers
410    * @param[in] context The GL context
411    * @param[in] program The shader program
412    * @return False if create or bind failed, true if success.
413    */
414   bool BindTextures( Context& context, Program& program );
415
416 private:
417
418   OwnerPointer< SceneGraph::RenderDataProvider > mRenderDataProvider;
419
420   Context*                     mContext;
421   Render::Geometry*            mGeometry;
422
423   struct UniformIndexMap
424   {
425     unsigned int               uniformIndex;                ///< The index of the cached location in the Program
426     const PropertyInputImpl*   propertyValue;
427   };
428
429   typedef Dali::Vector< UniformIndexMap > UniformIndexMappings;
430
431   UniformIndexMappings         mUniformIndexMap;
432   Vector<GLint>                mAttributesLocation;
433
434   StencilParameters            mStencilParameters;          ///< Struct containing all stencil related options
435   BlendingOptions              mBlendingOptions;            ///< Blending options including blend color, blend func and blend equation
436
437   size_t                       mIndexedDrawFirstElement;    ///< Offset of first element to draw
438   size_t                       mIndexedDrawElementsCount;   ///< Number of elements to draw
439
440   DepthFunction::Type          mDepthFunction:3;            ///< The depth function
441   FaceCullingMode::Type        mFaceCullingMode:2;          ///< The mode of face culling
442   DepthWriteMode::Type         mDepthWriteMode:2;           ///< The depth write mode
443   DepthTestMode::Type          mDepthTestMode:2;            ///< The depth test mode
444   bool                         mUpdateAttributesLocation:1; ///< Indicates attribute locations have changed
445   bool                         mPremultipledAlphaEnabled:1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
446
447 };
448
449 } // namespace SceneGraph
450
451 } // namespace Internal
452
453 } // namespace Dali
454
455 #endif // DALI_INTERNAL_RENDER_RENDERER_H