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