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