627e29eeb9f3ec7abd50d8fd01f501c25b7c479f
[platform/core/uifw/dali-core.git] / dali / internal / update / rendering / scene-graph-renderer.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_RENDERER2_H
2 #define DALI_INTERNAL_SCENE_GRAPH_RENDERER2_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 #include <dali/devel-api/rendering/geometry.h>
22 #include <dali/devel-api/rendering/renderer.h> // Dali::Renderer
23 #include <dali/internal/common/blending-options.h>
24 #include <dali/internal/event/common/event-thread-services.h>
25 #include <dali/internal/update/common/property-owner.h>
26 #include <dali/internal/update/common/uniform-map.h>
27 #include <dali/internal/update/common/scene-graph-connection-change-propagator.h>
28 #include <dali/internal/render/data-providers/render-data-provider.h>
29
30 namespace Dali
31 {
32 namespace Internal
33 {
34
35 namespace Render
36 {
37 class Renderer;
38 class Geometry;
39 }
40
41 namespace SceneGraph
42 {
43 class SceneController;
44
45 class Renderer;
46 typedef Dali::Vector< Renderer* > RendererContainer;
47 typedef RendererContainer::Iterator RendererIter;
48 typedef RendererContainer::ConstIterator RendererConstIter;
49
50 class TextureSet;
51 class Geometry;
52
53 class Renderer :  public PropertyOwner,
54                   public UniformMapDataProvider,
55                   public UniformMap::Observer,
56                   public ConnectionChangePropagator::Observer
57 {
58 public:
59
60   enum Opacity
61   {
62     OPAQUE,
63     TRANSPARENT,
64     TRANSLUCENT
65   };
66
67   /**
68    * Construct a new Renderer
69    */
70   static Renderer* New();
71
72   /**
73    * Destructor
74    */
75   virtual ~Renderer();
76
77   /**
78    * Overriden delete operator
79    * Deletes the renderer from its global memory pool
80    */
81   void operator delete( void* ptr );
82
83   /**
84    * Set the texture set for the renderer
85    * @param[in] textureSet The texture set this renderer will use
86    */
87   void SetTextures( TextureSet* textureSet );
88
89
90   /**
91    * Set the shader for the renderer
92    * @param[in] shader The shader this renderer will use
93    */
94   void SetShader( Shader* shader );
95
96   /**
97    * Get the shader used by this renderer
98    * @return the shader this renderer uses
99    */
100   Shader& GetShader()
101   {
102     return *mShader;
103   }
104
105   /**
106    * Set the geometry for the renderer
107    * @param[in] geometry The geometry this renderer will use
108    */
109   void SetGeometry( Render::Geometry* geometry );
110
111   /**
112    * Set the depth index
113    * @param[in] depthIndex the new depth index to use
114    */
115   void SetDepthIndex( int depthIndex );
116
117   /**
118    * @brief Get the depth index
119    * @return The depth index
120    */
121   int GetDepthIndex() const
122   {
123     return mDepthIndex;
124   }
125
126   /**
127    * Set the face culling mode
128    * @param[in] faceCullingMode to use
129    */
130   void SetFaceCullingMode( unsigned int faceCullingMode );
131
132   /**
133    * Set the blending mode
134    * @param[in] blendingMode to use
135    */
136   void SetBlendingMode( unsigned int blendingMode );
137
138   /**
139    * Set the blending options. This should only be called from the update thread.
140    * @param[in] options A bitmask of blending options.
141    */
142   void SetBlendingOptions( unsigned int options );
143
144   /**
145    * Set the blend color for blending operation
146    * @param blendColor to pass to GL
147    */
148   void SetBlendColor( const Vector4& blendColor );
149
150   /**
151    * Set the index of first element for 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 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    * Sets the depth buffer write mode
171    * @param[in] depthWriteMode The depth buffer write mode
172    */
173   void SetDepthWriteMode( unsigned int depthWriteMode );
174
175   /**
176    * Called when an actor with this renderer is added to the stage
177    */
178   void OnStageConnect();
179
180   /*
181    * Called when an actor with this renderer is removed from the stage
182    */
183   void OnStageDisconnect();
184
185   /**
186    * Prepare the object for rendering.
187    * This is called by the UpdateManager when an object is due to be rendered in the current frame.
188    * @param[in] updateBufferIndex The current update buffer index.
189    */
190   void PrepareRender( BufferIndex updateBufferIndex );
191
192   /*
193    * Retrieve the Render thread renderer
194    * @return The associated render thread renderer
195    */
196   Render::Renderer& GetRenderer();
197
198   /**
199    * Check whether the renderer has been marked as ready to render
200    * ready means that renderer has all resources and should produce correct result
201    * complete means all resources have finished loading
202    * It's possible that renderer is complete but not ready,
203    * for example in case of resource load failed
204    * @param[out] ready TRUE if the renderer has resources to render
205    * @param[out] complete TRUE if the renderer resources are complete
206    */
207   void GetReadyAndComplete( bool& ready, bool& complete ) const;
208
209   /**
210    * Query whether the renderer is fully opaque, fully transparent or transparent.
211    * @param[in] updateBufferIndex The current update buffer index.
212    * @return OPAQUE if fully opaque, TRANSPARENT if fully transparent and TRANSLUCENT if in between
213    */
214   Opacity GetOpacity( BufferIndex updateBufferIndex, const Node& node ) const;
215
216   /**
217    * Query whether the renderer is currently in use by an actor on the stage
218    */
219   bool IsReferenced() const
220   {
221     return mReferenceCount > 0;
222   }
223
224   /**
225    * Called by the TextureSet to notify to the renderer that it has changed
226    */
227   void TextureSetChanged();
228
229 public: // Implementation of ObjectOwnerContainer template methods
230   /**
231    * Connect the object to the scene graph
232    *
233    * @param[in] sceneController The scene controller - used for sending messages to render thread
234    * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
235    */
236   void ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
237
238   /**
239    * Disconnect the object from the scene graph
240    * @param[in] sceneController The scene controller - used for sending messages to render thread
241    * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
242    */
243   void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
244
245 public: // Implementation of ConnectionChangePropagator
246   /**
247    * @copydoc ConnectionChangePropagator::AddObserver
248    */
249   void AddConnectionObserver(ConnectionChangePropagator::Observer& observer){};
250
251   /**
252    * @copydoc ConnectionChangePropagator::RemoveObserver
253    */
254   void RemoveConnectionObserver(ConnectionChangePropagator::Observer& observer){};
255
256 public:
257
258
259 public: // UniformMap::Observer
260   /**
261    * @copydoc UniformMap::Observer::UniformMappingsChanged
262    */
263   virtual void UniformMappingsChanged( const UniformMap& mappings );
264
265 public: // ConnectionChangePropagator::Observer
266
267   /**
268    * @copydoc ConnectionChangePropagator::ConnectionsChanged
269    */
270   virtual void ConnectionsChanged( PropertyOwner& owner );
271
272   /**
273    * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
274    */
275   virtual void ConnectedUniformMapChanged( );
276
277   /**
278    * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
279    */
280   virtual void ObservedObjectDestroyed(PropertyOwner& owner);
281
282 public: // PropertyOwner implementation
283   /**
284    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
285    */
286   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex ){};
287
288 public: // From UniformMapDataProvider
289
290   /**
291    * @copydoc UniformMapDataProvider::GetUniformMapChanged
292    */
293   virtual bool GetUniformMapChanged( BufferIndex bufferIndex ) const{ return mUniformMapChanged[bufferIndex];}
294
295   /**
296    * @copydoc UniformMapDataProvider::GetUniformMap
297    */
298   virtual const CollectedUniformMap& GetUniformMap( BufferIndex bufferIndex ) const;
299
300 private:
301
302   /**
303    * Protected constructor; See also Renderer::New()
304    */
305   Renderer();
306
307   /**
308    * Helper function to create a new render data provider
309    * @return the new (initialized) data provider
310    */
311   RenderDataProvider* NewRenderDataProvider();
312
313 private:
314
315   SceneController*   mSceneController;  ///< Used for initializing renderers whilst attached
316   Render::Renderer*  mRenderer;    ///< Raw pointer to the renderer (that's owned by RenderManager)
317   TextureSet*        mTextureSet;    ///< The texture set this renderer uses. (Not owned)
318   Render::Geometry*  mGeometry;    ///< The geometry this renderer uses. (Not owned)
319   Shader*            mShader;
320
321   Vector4*                        mBlendColor;      ///< The blend color for blending operation
322   unsigned int                    mBlendBitmask;    ///< The bitmask of blending options
323   Dali::Renderer::FaceCullingMode mFaceCullingMode; ///< The mode of face culling
324   BlendingMode::Type              mBlendingMode;    ///< The mode of blending
325   Dali::Renderer::DepthWriteMode  mDepthWriteMode;  ///< The depth write mode
326
327   CollectedUniformMap mCollectedUniformMap[2]; ///< Uniform maps collected by the renderer
328
329   size_t mIndexedDrawFirstElement;             ///< first element index to be drawn using indexed draw
330   size_t mIndexedDrawElementsCount;            ///< number of elements to be drawn using indexed draw
331   unsigned int mReferenceCount;                ///< Number of nodes currently using this renderer
332   unsigned int mRegenerateUniformMap;          ///< 2 if the map should be regenerated, 1 if it should be copied.
333   unsigned char mResendFlag;                   ///< Indicate whether data should be resent to the renderer
334   bool         mUniformMapChanged[2];          ///< Records if the uniform map has been altered this frame
335   bool         mResourcesReady;                ///< Set during the Update algorithm; true if the attachment has resources ready for the current frame.
336   bool         mFinishedResourceAcquisition;   ///< Set during DoPrepareResources; true if ready & all resource acquisition has finished (successfully or otherwise)
337   bool         mPremultipledAlphaEnabled : 1;  ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
338
339
340 public:
341   int mDepthIndex; ///< Used only in PrepareRenderInstructions
342 };
343
344
345 /// Messages
346 inline void SetTexturesMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const TextureSet& textureSet )
347 {
348   typedef MessageValue1< Renderer, TextureSet* > LocalType;
349
350   // Reserve some memory inside the message queue
351   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
352
353   // Construct message in the message queue memory; note that delete should not be called on the return value
354   new (slot) LocalType( &renderer, &Renderer::SetTextures, const_cast<TextureSet*>(&textureSet) );
355 }
356
357 inline void SetGeometryMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Render::Geometry& geometry )
358 {
359   typedef MessageValue1< Renderer, Render::Geometry* > LocalType;
360
361   // Reserve some memory inside the message queue
362   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
363
364   // Construct message in the message queue memory; note that delete should not be called on the return value
365   new (slot) LocalType( &renderer, &Renderer::SetGeometry, const_cast<Render::Geometry*>(&geometry) );
366 }
367
368 inline void SetShaderMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, Shader& shader )
369 {
370   typedef MessageValue1< Renderer, Shader* > LocalType;
371
372   // Reserve some memory inside the message queue
373   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
374
375   // Construct message in the message queue memory; note that delete should not be called on the return value
376   new (slot) LocalType( &renderer, &Renderer::SetShader, &shader );
377 }
378
379 inline void SetDepthIndexMessage( EventThreadServices& eventThreadServices, const Renderer& attachment, int depthIndex )
380 {
381   typedef MessageValue1< Renderer, int > LocalType;
382
383   // Reserve some memory inside the message queue
384   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
385
386   // Construct message in the message queue memory; note that delete should not be called on the return value
387   new (slot) LocalType( &attachment, &Renderer::SetDepthIndex, depthIndex );
388 }
389
390 inline void SetFaceCullingModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, Dali::Renderer::FaceCullingMode faceCullingMode )
391 {
392   typedef MessageValue1< Renderer, unsigned int > LocalType;
393
394   // Reserve some memory inside the message queue
395   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
396
397   new (slot) LocalType( &renderer, &Renderer::SetFaceCullingMode, faceCullingMode );
398 }
399
400 inline void SetBlendingModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, BlendingMode::Type blendingMode )
401 {
402   typedef MessageValue1< Renderer, unsigned int > LocalType;
403
404   // Reserve some memory inside the message queue
405   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
406
407   new (slot) LocalType( &renderer, &Renderer::SetBlendingMode, blendingMode );
408 }
409
410 inline void SetBlendingOptionsMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, unsigned int options )
411 {
412   typedef MessageValue1< Renderer, unsigned int > LocalType;
413
414   // Reserve some memory inside the message queue
415   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
416
417   new (slot) LocalType( &renderer, &Renderer::SetBlendingOptions, options );
418 }
419
420 inline void SetBlendColorMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Vector4& blendColor )
421 {
422   typedef MessageValue1< Renderer, Vector4 > LocalType;
423
424   // Reserve some memory inside the message queue
425   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
426
427   new (slot) LocalType( &renderer, &Renderer::SetBlendColor, blendColor );
428 }
429
430 inline void SetIndexedDrawFirstElementMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, size_t firstElement )
431 {
432   typedef MessageValue1< Renderer, size_t > LocalType;
433
434   // Reserve some memory inside the message queue
435   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
436
437   new (slot) LocalType( &renderer, &Renderer::SetIndexedDrawFirstElement, firstElement );
438 }
439
440 inline void SetIndexedDrawElementsCountMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, size_t elementsCount )
441 {
442   typedef MessageValue1< Renderer, size_t > LocalType;
443
444   // Reserve some memory inside the message queue
445   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
446
447   new (slot) LocalType( &renderer, &Renderer::SetIndexedDrawElementsCount, elementsCount );
448 }
449
450 inline void SetEnablePreMultipliedAlphaMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, bool preMultiplied )
451 {
452   typedef MessageValue1< Renderer, bool > LocalType;
453
454   // Reserve some memory inside the message queue
455   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
456
457   new (slot) LocalType( &renderer, &Renderer::EnablePreMultipliedAlpha, preMultiplied );
458 }
459
460 inline void SetDepthWriteModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, Dali::Renderer::DepthWriteMode depthWriteMode )
461 {
462   typedef MessageValue1< Renderer, unsigned int > LocalType;
463
464   // Reserve some memory inside the message queue
465   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
466
467   new (slot) LocalType( &renderer, &Renderer::SetDepthWriteMode, depthWriteMode );
468 }
469
470 inline void OnStageConnectMessage( EventThreadServices& eventThreadServices, const Renderer& renderer )
471 {
472   typedef Message< Renderer > LocalType;
473
474   // Reserve some memory inside the message queue
475   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
476
477   // Construct message in the message queue memory; note that delete should not be called on the return value
478   new (slot) LocalType( &renderer, &Renderer::OnStageConnect );
479 }
480
481 inline void OnStageDisconnectMessage( EventThreadServices& eventThreadServices, const Renderer& renderer )
482 {
483   typedef Message< Renderer > LocalType;
484
485   // Reserve some memory inside the message queue
486   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
487
488   // Construct message in the message queue memory; note that delete should not be called on the return value
489   new (slot) LocalType( &renderer, &Renderer::OnStageDisconnect );
490 }
491
492 } // namespace SceneGraph
493 } // namespace Internal
494 } // namespace Dali
495
496 #endif //  DALI_INTERNAL_SCENE_GRAPH_RENDERER_H