Merge branch 'devel/master(1.1.39)' into tizen
[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] depthTestMode Depth buffer test mode
87    * @param[in] depthFunction Depth function
88    */
89   static Renderer* New( SceneGraph::RenderDataProvider* dataProviders,
90                         Render::Geometry* geometry,
91                         unsigned int blendingBitmask,
92                         const Vector4* blendColor,
93                         FaceCullingMode::Type faceCullingMode,
94                         bool preMultipliedAlphaEnabled,
95                         DepthWriteMode::Type depthWriteMode,
96                         DepthTestMode::Type depthTestMode,
97                         DepthFunction::Type depthFunction );
98
99   /**
100    * Constructor.
101    * @param[in] dataProviders The data providers for the renderer
102    * @param[in] geometry The geometry for the renderer
103    * @param[in] blendingBitmask A bitmask of blending options.
104    * @param[in] blendColor The blend color to pass to GL
105    * @param[in] faceCullingMode The face-culling mode.
106    * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
107    * @param[in] depthWriteMode Depth buffer write mode
108    * @param[in] depthTestMode Depth buffer test mode
109    * @param[in] depthFunction Depth function
110    */
111   Renderer( SceneGraph::RenderDataProvider* dataProviders,
112             Render::Geometry* geometry,
113             unsigned int blendingBitmask,
114             const Vector4* blendColor,
115             FaceCullingMode::Type faceCullingMode,
116             bool preMultipliedAlphaEnabled,
117             DepthWriteMode::Type depthWriteMode,
118             DepthTestMode::Type depthTestMode,
119             DepthFunction::Type depthFunction );
120
121   /**
122    * Change the data providers of the renderer
123    * @param[in] dataProviders The data providers
124    */
125   void SetRenderDataProvider( SceneGraph::RenderDataProvider* dataProviders );
126
127   /**
128    * Change the geometry used by the renderer
129    * @param[in] geometry The new geometry
130    */
131   void SetGeometry( Render::Geometry* geometry );
132   /**
133    * Second-phase construction.
134    * This is called when the renderer is inside render thread
135    * @param[in] context to use
136    * @param[in] textureCache to use
137    * @param[in] uniformNameCache to use
138    */
139   void Initialize( Context& context, SceneGraph::TextureCache& textureCache, Render::UniformNameCache& uniformNameCache );
140
141   /**
142    * Destructor
143    */
144   ~Renderer();
145
146   /**
147    * Set the face-culling mode.
148    * @param[in] mode The face-culling mode.
149    */
150   void SetFaceCullingMode( FaceCullingMode::Type mode );
151
152   /**
153    * Set the bitmask for blending options
154    * @param[in] bitmask A bitmask of blending options.
155    */
156   void SetBlendingBitMask( unsigned int bitmask );
157
158   /**
159    * Set the blend color for blending options
160    * @param[in] blendColor The blend color to pass to GL
161    */
162   void SetBlendColor( const Vector4* color );
163
164   /**
165    * Set the first element index to draw by the indexed draw
166    * @param[in] firstElement index of first element to draw
167    */
168   void SetIndexedDrawFirstElement( size_t firstElement );
169
170   /**
171    * Set the number of elements to draw by the indexed draw
172    * @param[in] elementsCount number of elements to draw
173    */
174   void SetIndexedDrawElementsCount( size_t elementsCount );
175
176   /**
177    * @brief Set whether the Pre-multiplied Alpha Blending is required
178    *
179    * @param[in] preMultipled whether alpha is pre-multiplied.
180    */
181   void EnablePreMultipliedAlpha( bool preMultipled );
182
183   /**
184    * Sets the depth write mode
185    * @param[in] depthWriteMode The depth write mode
186    */
187   void SetDepthWriteMode( DepthWriteMode::Type depthWriteMode );
188
189   /**
190    * Query the Renderer's depth write mode
191    * @return The renderer depth write mode
192    */
193   DepthWriteMode::Type GetDepthWriteMode() const;
194
195   /**
196    * Sets the depth test mode
197    * @param[in] depthTestMode The depth test mode
198    */
199   void SetDepthTestMode( DepthTestMode::Type depthTestMode );
200
201   /**
202    * Query the Renderer's depth test mode
203    * @return The renderer depth test mode
204    */
205   DepthTestMode::Type GetDepthTestMode() const;
206
207   /**
208    * Sets the depth function
209    * @param[in] depthFunction The depth function
210    */
211   void SetDepthFunction( DepthFunction::Type depthFunction );
212
213   /**
214    * Query the Renderer's depth function
215    * @return The renderer depth function
216    */
217   DepthFunction::Type GetDepthFunction() const;
218
219   /**
220    * Called to render during RenderManager::Render().
221    * @param[in] context The context used for rendering
222    * @param[in] textureCache The texture cache used to get textures
223    * @param[in] bufferIndex The index of the previous update buffer.
224    * @param[in] node The node using this renderer
225    * @param[in] defaultShader in case there is no custom shader
226    * @param[in] modelViewMatrix The model-view matrix.
227    * @param[in] viewMatrix The view matrix.
228    * @param[in] projectionMatrix The projection matrix.
229    */
230   void Render( Context& context,
231                SceneGraph::TextureCache& textureCache,
232                BufferIndex bufferIndex,
233                const SceneGraph::NodeDataProvider& node,
234                SceneGraph::Shader& defaultShader,
235                const Matrix& modelMatrix,
236                const Matrix& modelViewMatrix,
237                const Matrix& viewMatrix,
238                const Matrix& projectionMatrix,
239                const Vector3& size,
240                bool blend);
241
242   /**
243    * Write the renderer's sort attributes to the passed in reference
244    *
245    * @param[in] bufferIndex The current update buffer index.
246    * @param[out] sortAttributes
247    */
248   void SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RendererWithSortAttributes& sortAttributes ) const;
249
250 private:
251
252   struct UniformIndexMap;
253
254   // Undefined
255   Renderer( const Renderer& );
256
257   // Undefined
258   Renderer& operator=( const Renderer& rhs );
259
260   /**
261    * Sets blending options
262    * @param context to use
263    * @param blend Wheter blending should be enabled or not
264    */
265   void SetBlending( Context& context, bool blend );
266
267   /**
268    * Set the uniforms from properties according to the uniform map
269    * @param[in] bufferIndex The index of the previous update buffer.
270    * @param[in] node The node using the renderer
271    * @param[in] size The size of the renderer
272    * @param[in] program The shader program on which to set the uniforms.
273    */
274   void SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program );
275
276   /**
277    * Set the program uniform in the map from the mapped property
278    * @param[in] bufferIndex The index of the previous update buffer.
279    * @param[in] program The shader program
280    * @param[in] map The uniform
281    */
282   void SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map );
283
284   /**
285    * Bind the textures and setup the samplers
286    * @param[in] context The GL context
287    * @param[in] textureCache The texture cache
288    * @param[in] program The shader program
289    * @return False if create or bind failed, true if success.
290    */
291   bool BindTextures( Context& context, SceneGraph::TextureCache& textureCache, Program& program );
292
293 private:
294
295   OwnerPointer< SceneGraph::RenderDataProvider > mRenderDataProvider;
296
297   Context* mContext;
298   SceneGraph::TextureCache* mTextureCache;
299   Render::UniformNameCache* mUniformNameCache;
300   Render::Geometry* mGeometry;
301
302   struct UniformIndexMap
303   {
304     unsigned int uniformIndex; // The index of the cached location in the Program
305     const PropertyInputImpl* propertyValue;
306   };
307
308   typedef Dali::Vector< UniformIndexMap > UniformIndexMappings;
309   UniformIndexMappings mUniformIndexMap;
310
311   Vector<GLint> mAttributesLocation;
312
313   BlendingOptions       mBlendingOptions; /// Blending options including blend color, blend func and blend equation
314   FaceCullingMode::Type mFaceCullingMode; /// Mode of face culling
315   DepthFunction::Type   mDepthFunction;   /// Depth function
316
317   size_t mIndexedDrawFirstElement;        /// Offset of first element to draw
318   size_t mIndexedDrawElementsCount;       /// Number of elements to draw
319
320   DepthWriteMode::Type mDepthWriteMode:2; /// Depth write mode
321   DepthTestMode::Type mDepthTestMode:2;   /// Depth test mode
322
323   bool mUpdateAttributesLocation:1;       ///< Indicates attribute locations have changed
324   bool mPremultipledAlphaEnabled:1;       ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
325 };
326
327 } // namespace SceneGraph
328
329 } // namespace Internal
330
331 } // namespace Dali
332
333 #endif // __DALI_INTERNAL_RENDER_RENDERER_H__