Merge "DALi Version 1.1.37" into devel/master
[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) 2015 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/devel-api/rendering/texture-set.h>
25 #include <dali/internal/common/blending-options.h>
26 #include <dali/internal/common/message.h>
27 #include <dali/internal/event/common/property-input-impl.h>
28 #include <dali/internal/event/effects/shader-declarations.h>
29 #include <dali/internal/render/gl-resources/gl-resource-owner.h>
30 #include <dali/integration-api/debug.h>
31 #include <dali/internal/common/type-abstraction-enums.h>
32 #include <dali/internal/update/manager/prepare-render-instructions.h>
33 #include <dali/internal/render/data-providers/render-data-provider.h>
34 #include <dali/internal/render/renderers/render-geometry.h>
35
36 namespace Dali
37 {
38
39 namespace Internal
40 {
41 class Context;
42 class Texture;
43 class Program;
44
45 namespace SceneGraph
46 {
47 class SceneController;
48 class Shader;
49 class TextureCache;
50 class NodeDataProvider;
51 }
52
53
54 namespace Render
55 {
56 class UniformNameCache;
57
58 /**
59  * Renderers are used to render meshes
60  * These objects are used during RenderManager::Render(), so properties modified during
61  * the Update must either be double-buffered, or set via a message added to the RenderQueue.
62  */
63 class Renderer : public GlResourceOwner
64 {
65 public:
66
67   /**
68    * @copydoc Dali::Internal::GlResourceOwner::GlContextDestroyed()
69    */
70   void GlContextDestroyed();
71
72   /**
73    * @copydoc Dali::Internal::GlResourceOwner::GlCleanup()
74    */
75   void GlCleanup();
76
77   /**
78    * Create a new renderer instance
79    * @param[in] dataProviders The data providers for the renderer
80    * @param[in] geometry The geometry for the renderer
81    * @param[in] blendingBitmask A bitmask of blending options.
82    * @param[in] blendColor The blend color to pass to GL
83    * @param[in] faceCullingMode The face-culling mode.
84    * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
85    * @param[in] depthWriteMode Depth buffer write mode
86    * @param[in] depthFunction Depth function
87    */
88   static Renderer* New( SceneGraph::RenderDataProvider* dataProviders,
89                         Render::Geometry* geometry,
90                         unsigned int blendingBitmask,
91                         const Vector4* blendColor,
92                         FaceCullingMode::Type faceCullingMode,
93                         bool preMultipliedAlphaEnabled,
94                         DepthWriteMode::Type depthWriteMode,
95                         DepthFunction::Type depthFunction );
96
97   /**
98    * Constructor.
99    * @param[in] dataProviders The data providers for the renderer
100    * @param[in] geometry The geometry for the renderer
101    * @param[in] blendingBitmask A bitmask of blending options.
102    * @param[in] blendColor The blend color to pass to GL
103    * @param[in] faceCullingMode The face-culling mode.
104    * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
105    * @param[in] depthWriteMode Depth buffer write mode
106    * @param[in] depthFunction Depth function
107    */
108   Renderer( SceneGraph::RenderDataProvider* dataProviders,
109             Render::Geometry* geometry,
110             unsigned int blendingBitmask,
111             const Vector4* blendColor,
112             FaceCullingMode::Type faceCullingMode,
113             bool preMultipliedAlphaEnabled,
114             DepthWriteMode::Type depthWriteMode,
115             DepthFunction::Type depthFunction );
116
117   /**
118    * Change the data providers of the renderer
119    * @param[in] dataProviders The data providers
120    */
121   void SetRenderDataProvider( SceneGraph::RenderDataProvider* dataProviders );
122
123   /**
124    * Change the geometry used by the renderer
125    * @param[in] geometry The new geometry
126    */
127   void SetGeometry( Render::Geometry* geometry );
128   /**
129    * Second-phase construction.
130    * This is called when the renderer is inside render thread
131    * @param[in] context to use
132    * @param[in] textureCache to use
133    * @param[in] uniformNameCache to use
134    */
135   void Initialize( Context& context, SceneGraph::TextureCache& textureCache, Render::UniformNameCache& uniformNameCache );
136
137   /**
138    * Destructor
139    */
140   ~Renderer();
141
142   /**
143    * Set the face-culling mode.
144    * @param[in] mode The face-culling mode.
145    */
146   void SetFaceCullingMode( FaceCullingMode::Type mode );
147
148   /**
149    * Set the bitmask for blending options
150    * @param[in] bitmask A bitmask of blending options.
151    */
152   void SetBlendingBitMask( unsigned int bitmask );
153
154   /**
155    * Set the blend color for blending options
156    * @param[in] blendColor The blend color to pass to GL
157    */
158   void SetBlendColor( const Vector4* color );
159
160   /**
161    * Set the first element index to draw by the indexed draw
162    * @param[in] firstElement index of first element to draw
163    */
164   void SetIndexedDrawFirstElement( size_t firstElement );
165
166   /**
167    * Set the number of elements to draw by the indexed draw
168    * @param[in] elementsCount number of elements to draw
169    */
170   void SetIndexedDrawElementsCount( size_t elementsCount );
171
172   /**
173    * @brief Set whether the Pre-multiplied Alpha Blending is required
174    *
175    * @param[in] preMultipled whether alpha is pre-multiplied.
176    */
177   void EnablePreMultipliedAlpha( bool preMultipled );
178
179   /**
180    * Sets the depth write mode
181    * @param[in] depthWriteMode The depth write mode
182    */
183   void SetDepthWriteMode( DepthWriteMode::Type depthWriteMode );
184
185   /**
186    * Query the Renderer's depth write mode
187    * @return The renderer depth write mode
188    */
189   DepthWriteMode::Type GetDepthWriteMode() const;
190
191   /**
192    * Sets the depth function
193    * @param[in] depthFunction The depth function
194    */
195   void SetDepthFunction( DepthFunction::Type depthFunction );
196
197   /**
198    * Query the Renderer's depth function
199    * @return The renderer depth function
200    */
201   DepthFunction::Type GetDepthFunction() const;
202
203   /**
204    * Called to render during RenderManager::Render().
205    * @param[in] context The context used for rendering
206    * @param[in] textureCache The texture cache used to get textures
207    * @param[in] bufferIndex The index of the previous update buffer.
208    * @param[in] node The node using this renderer
209    * @param[in] defaultShader in case there is no custom shader
210    * @param[in] modelViewMatrix The model-view matrix.
211    * @param[in] viewMatrix The view matrix.
212    * @param[in] projectionMatrix The projection matrix.
213    */
214   void Render( Context& context,
215                SceneGraph::TextureCache& textureCache,
216                BufferIndex bufferIndex,
217                const SceneGraph::NodeDataProvider& node,
218                SceneGraph::Shader& defaultShader,
219                const Matrix& modelMatrix,
220                const Matrix& modelViewMatrix,
221                const Matrix& viewMatrix,
222                const Matrix& projectionMatrix,
223                const Vector3& size,
224                bool blend);
225
226   /**
227    * Write the renderer's sort attributes to the passed in reference
228    *
229    * @param[in] bufferIndex The current update buffer index.
230    * @param[out] sortAttributes
231    */
232   void SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RendererWithSortAttributes& sortAttributes ) const;
233
234 private:
235
236   struct UniformIndexMap;
237
238   // Undefined
239   Renderer( const Renderer& );
240
241   // Undefined
242   Renderer& operator=( const Renderer& rhs );
243
244   /**
245    * Sets blending options
246    * @param context to use
247    * @param blend Wheter blending should be enabled or not
248    */
249   void SetBlending( Context& context, bool blend );
250
251   /**
252    * Set the uniforms from properties according to the uniform map
253    * @param[in] bufferIndex The index of the previous update buffer.
254    * @param[in] node The node using the renderer
255    * @param[in] size The size of the renderer
256    * @param[in] program The shader program on which to set the uniforms.
257    */
258   void SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program );
259
260   /**
261    * Set the program uniform in the map from the mapped property
262    * @param[in] bufferIndex The index of the previous update buffer.
263    * @param[in] program The shader program
264    * @param[in] map The uniform
265    */
266   void SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map );
267
268   /**
269    * Bind the textures and setup the samplers
270    * @param[in] context The GL context
271    * @param[in] textureCache The texture cache
272    * @param[in] program The shader program
273    * @return False if create or bind failed, true if success.
274    */
275   bool BindTextures( Context& context, SceneGraph::TextureCache& textureCache, Program& program );
276
277 private:
278
279   OwnerPointer< SceneGraph::RenderDataProvider > mRenderDataProvider;
280
281   Context* mContext;
282   SceneGraph::TextureCache* mTextureCache;
283   Render::UniformNameCache* mUniformNameCache;
284   Render::Geometry* mGeometry;
285
286   struct UniformIndexMap
287   {
288     unsigned int uniformIndex; // The index of the cached location in the Program
289     const PropertyInputImpl* propertyValue;
290   };
291
292   typedef Dali::Vector< UniformIndexMap > UniformIndexMappings;
293   UniformIndexMappings mUniformIndexMap;
294
295   Vector<GLint> mAttributesLocation;
296
297   BlendingOptions       mBlendingOptions; /// Blending options including blend color, blend func and blend equation
298   FaceCullingMode::Type mFaceCullingMode; /// Mode of face culling
299   DepthWriteMode::Type  mDepthWriteMode;  /// Depth write mode
300   DepthFunction::Type   mDepthFunction;   /// Depth function
301
302   size_t mIndexedDrawFirstElement;                  /// Offset of first element to draw
303   size_t mIndexedDrawElementsCount;                 /// Number of elements to draw
304
305   bool mUpdateAttributesLocation:1;                 ///< Indicates attribute locations have changed
306   bool mPremultipledAlphaEnabled:1;                 ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
307 };
308
309 } // namespace SceneGraph
310
311 } // namespace Internal
312
313 } // namespace Dali
314
315 #endif // __DALI_INTERNAL_RENDER_RENDERER_H__