[dali_1.1.31] Merge branch 'devel/master'
[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    * @brief Set whether the Pre-multiplied Alpha Blending is required
160    *
161    * @param[in] preMultipled whether alpha is pre-multiplied.
162    */
163   void EnablePreMultipliedAlpha( bool preMultipled );
164
165   /**
166    * Called when an actor with this renderer is added to the stage
167    */
168   void OnStageConnect();
169
170   /*
171    * Called when an actor with this renderer is removed from the stage
172    */
173   void OnStageDisconnect();
174
175   /**
176    * Prepare the object for rendering.
177    * This is called by the UpdateManager when an object is due to be rendered in the current frame.
178    * @param[in] updateBufferIndex The current update buffer index.
179    */
180   void PrepareRender( BufferIndex updateBufferIndex );
181
182   /*
183    * Retrieve the Render thread renderer
184    * @return The associated render thread renderer
185    */
186   Render::Renderer& GetRenderer();
187
188   /**
189    * Check whether the renderer has been marked as ready to render
190    * ready means that renderer has all resources and should produce correct result
191    * complete means all resources have finished loading
192    * It's possible that renderer is complete but not ready,
193    * for example in case of resource load failed
194    * @param[out] ready TRUE if the renderer has resources to render
195    * @param[out] complete TRUE if the renderer resources are complete
196    */
197   void GetReadyAndComplete( bool& ready, bool& complete ) const;
198
199   /**
200    * Query whether the renderer is fully opaque, fully transparent or transparent.
201    * @param[in] updateBufferIndex The current update buffer index.
202    * @return OPAQUE if fully opaque, TRANSPARENT if fully transparent and TRANSLUCENT if in between
203    */
204   Opacity GetOpacity( BufferIndex updateBufferIndex, const Node& node ) const;
205
206   /**
207    * Query whether the renderer is currently in use by an actor on the stage
208    */
209   bool IsReferenced() const
210   {
211     return mReferenceCount > 0;
212   }
213
214
215 public: // Implementation of ObjectOwnerContainer template methods
216   /**
217    * Connect the object to the scene graph
218    *
219    * @param[in] sceneController The scene controller - used for sending messages to render thread
220    * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
221    */
222   void ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
223
224   /**
225    * Disconnect the object from the scene graph
226    * @param[in] sceneController The scene controller - used for sending messages to render thread
227    * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
228    */
229   void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
230
231 public: // Implementation of ConnectionChangePropagator
232   /**
233    * @copydoc ConnectionChangePropagator::AddObserver
234    */
235   void AddConnectionObserver(ConnectionChangePropagator::Observer& observer){};
236
237   /**
238    * @copydoc ConnectionChangePropagator::RemoveObserver
239    */
240   void RemoveConnectionObserver(ConnectionChangePropagator::Observer& observer){};
241
242 public:
243
244
245 public: // UniformMap::Observer
246   /**
247    * @copydoc UniformMap::Observer::UniformMappingsChanged
248    */
249   virtual void UniformMappingsChanged( const UniformMap& mappings );
250
251 public: // ConnectionChangePropagator::Observer
252
253   /**
254    * @copydoc ConnectionChangePropagator::ConnectionsChanged
255    */
256   virtual void ConnectionsChanged( PropertyOwner& owner );
257
258   /**
259    * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
260    */
261   virtual void ConnectedUniformMapChanged( );
262
263   /**
264    * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
265    */
266   virtual void ObservedObjectDestroyed(PropertyOwner& owner);
267
268 public: // PropertyOwner implementation
269   /**
270    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
271    */
272   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex ){};
273
274 public: // From UniformMapDataProvider
275
276   /**
277    * @copydoc UniformMapDataProvider::GetUniformMapChanged
278    */
279   virtual bool GetUniformMapChanged( BufferIndex bufferIndex ) const{ return mUniformMapChanged[bufferIndex];}
280
281   /**
282    * @copydoc UniformMapDataProvider::GetUniformMap
283    */
284   virtual const CollectedUniformMap& GetUniformMap( BufferIndex bufferIndex ) const;
285
286 private:
287
288   /**
289    * Protected constructor; See also Renderer::New()
290    */
291   Renderer();
292
293   /**
294    * Helper function to create a new render data provider
295    * @return the new (initialized) data provider
296    */
297   RenderDataProvider* NewRenderDataProvider();
298
299 private:
300
301   SceneController* mSceneController;  ///< Used for initializing renderers whilst attached
302   Render::Renderer*  mRenderer;    ///< Raw pointer to the new renderer (that's owned by RenderManager)
303   TextureSet*        mTextureSet;    ///< The texture set this renderer uses. (Not owned)
304   Geometry*          mGeometry;    ///< The geometry this renderer uses. (Not owned)
305   Shader*            mShader;
306
307   Vector4*                        mBlendColor;      ///< The blend color for blending operation
308   unsigned int                    mBlendBitmask;    ///< The bitmask of blending options
309   Dali::Renderer::FaceCullingMode mFaceCullingMode; ///< The mode of face culling
310   BlendingMode::Type              mBlendingMode;    ///< The mode of blending
311
312   CollectedUniformMap mCollectedUniformMap[2]; ///< Uniform maps collected by the renderer
313   unsigned int mReferenceCount;                ///< Number of nodes currently using this renderer
314   unsigned int mRegenerateUniformMap;          ///< 2 if the map should be regenerated, 1 if it should be copied.
315   unsigned char mResendFlag;                    ///< Indicate whether data should be resent to the renderer
316   bool         mUniformMapChanged[2];          ///< Records if the uniform map has been altered this frame
317   bool         mResourcesReady;                ///< Set during the Update algorithm; true if the attachment has resources ready for the current frame.
318   bool         mFinishedResourceAcquisition;   ///< Set during DoPrepareResources; true if ready & all resource acquisition has finished (successfully or otherwise)
319   bool         mPremultipledAlphaEnabled;      ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
320
321 public:
322   int mDepthIndex; ///< Used only in PrepareRenderInstructions
323 };
324
325
326 /// Messages
327 inline void SetTexturesMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const TextureSet& textureSet )
328 {
329   typedef MessageValue1< Renderer, TextureSet* > LocalType;
330
331   // Reserve some memory inside the message queue
332   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
333
334   // Construct message in the message queue memory; note that delete should not be called on the return value
335   new (slot) LocalType( &renderer, &Renderer::SetTextures, const_cast<TextureSet*>(&textureSet) );
336 }
337
338 inline void SetGeometryMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Geometry& geometry )
339 {
340   typedef MessageValue1< Renderer, Geometry* > LocalType;
341
342   // Reserve some memory inside the message queue
343   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
344
345   // Construct message in the message queue memory; note that delete should not be called on the return value
346   new (slot) LocalType( &renderer, &Renderer::SetGeometry, const_cast<Geometry*>(&geometry) );
347 }
348
349 inline void SetShaderMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, Shader& shader )
350 {
351   typedef MessageValue1< Renderer, Shader* > LocalType;
352
353   // Reserve some memory inside the message queue
354   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
355
356   // Construct message in the message queue memory; note that delete should not be called on the return value
357   new (slot) LocalType( &renderer, &Renderer::SetShader, &shader );
358 }
359
360 inline void SetDepthIndexMessage( EventThreadServices& eventThreadServices, const Renderer& attachment, int depthIndex )
361 {
362   typedef MessageValue1< Renderer, int > LocalType;
363
364   // Reserve some memory inside the message queue
365   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
366
367   // Construct message in the message queue memory; note that delete should not be called on the return value
368   new (slot) LocalType( &attachment, &Renderer::SetDepthIndex, depthIndex );
369 }
370
371 inline void SetFaceCullingModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, Dali::Renderer::FaceCullingMode faceCullingMode )
372 {
373   typedef MessageValue1< Renderer, unsigned int > LocalType;
374
375   // Reserve some memory inside the message queue
376   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
377
378   new (slot) LocalType( &renderer, &Renderer::SetFaceCullingMode, faceCullingMode );
379 }
380
381 inline void SetBlendingModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, BlendingMode::Type blendingMode )
382 {
383   typedef MessageValue1< Renderer, unsigned int > LocalType;
384
385   // Reserve some memory inside the message queue
386   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
387
388   new (slot) LocalType( &renderer, &Renderer::SetBlendingMode, blendingMode );
389 }
390
391 inline void SetBlendingOptionsMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, unsigned int options )
392 {
393   typedef MessageValue1< Renderer, unsigned int > LocalType;
394
395   // Reserve some memory inside the message queue
396   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
397
398   new (slot) LocalType( &renderer, &Renderer::SetBlendingOptions, options );
399 }
400
401 inline void SetBlendColorMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Vector4& blendColor )
402 {
403   typedef MessageValue1< Renderer, Vector4 > LocalType;
404
405   // Reserve some memory inside the message queue
406   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
407
408   new (slot) LocalType( &renderer, &Renderer::SetBlendColor, blendColor );
409 }
410
411 inline void SetEnablePreMultipliedAlphaMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, bool preMultiplied )
412 {
413   typedef MessageValue1< Renderer, bool > LocalType;
414
415   // Reserve some memory inside the message queue
416   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
417
418   new (slot) LocalType( &renderer, &Renderer::EnablePreMultipliedAlpha, preMultiplied );
419 }
420
421 inline void OnStageConnectMessage( EventThreadServices& eventThreadServices, const Renderer& renderer )
422 {
423   typedef Message< Renderer > LocalType;
424
425   // Reserve some memory inside the message queue
426   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
427
428   // Construct message in the message queue memory; note that delete should not be called on the return value
429   new (slot) LocalType( &renderer, &Renderer::OnStageConnect );
430 }
431
432 inline void OnStageDisconnectMessage( EventThreadServices& eventThreadServices, const Renderer& renderer )
433 {
434   typedef Message< Renderer > LocalType;
435
436   // Reserve some memory inside the message queue
437   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
438
439   // Construct message in the message queue memory; note that delete should not be called on the return value
440   new (slot) LocalType( &renderer, &Renderer::OnStageDisconnect );
441 }
442
443 } // namespace SceneGraph
444 } // namespace Internal
445 } // namespace Dali
446
447 #endif //  DALI_INTERNAL_SCENE_GRAPH_RENDERER_H