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