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