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