Merge branch 'devel/master (1.1.16)' into tizen
[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/internal/event/common/event-thread-services.h>
23 #include <dali/internal/update/common/property-owner.h>
24 #include <dali/internal/update/common/uniform-map.h>
25 #include <dali/internal/update/common/scene-graph-connection-change-propagator.h>
26 #include <dali/internal/render/data-providers/render-data-provider.h>
27
28 namespace Dali
29 {
30 namespace Internal
31 {
32
33 namespace Render
34 {
35 class Renderer;
36 }
37
38 namespace SceneGraph
39 {
40 class SceneController;
41
42 class Renderer;
43 typedef Dali::Vector< Renderer* > RendererContainer;
44 typedef RendererContainer::Iterator RendererIter;
45 typedef RendererContainer::ConstIterator RendererConstIter;
46
47 class Material;
48 class Geometry;
49
50 class Renderer :  public PropertyOwner,
51                   public UniformMapDataProvider,
52                   public UniformMap::Observer,
53                   public ConnectionChangePropagator::Observer
54 {
55 public:
56
57   enum Opacity
58   {
59     OPAQUE,
60     TRANSPARENT,
61     TRANSLUCENT
62   };
63
64   /**
65    * Construct a new Renderer
66    */
67   static Renderer* New();
68
69   /**
70    * Destructor
71    */
72   virtual ~Renderer();
73
74   /**
75    * Overriden delete operator
76    * Deletes the renderer from its global memory pool
77    */
78   void operator delete( void* ptr );
79
80   /**
81    * Set the material for the renderer
82    * @param[in] bufferIndex The current frame's buffer index
83    * @param[in] material The material this renderer will use
84    */
85   void SetMaterial( BufferIndex bufferIndex, Material* material);
86
87   /**
88    * Get the material of this renderer
89    * @return the material this renderer uses
90    */
91   Material& GetMaterial()
92   {
93     return *mMaterial;
94   }
95
96   /**
97    * Set the geometry for the renderer
98    * @param[in] bufferIndex The current frame's buffer index
99    * @param[in] geometry The geometry this renderer will use
100    */
101   void SetGeometry( BufferIndex bufferIndex, Geometry* material);
102
103   /**
104    * Get the geometry of this renderer
105    * @return the geometry this renderer uses
106    */
107   Geometry& GetGeometry()
108   {
109     return *mGeometry;
110   }
111
112   /**
113    * Set the depth index
114    * @param[in] depthIndex the new depth index to use
115    */
116   void SetDepthIndex( int depthIndex );
117
118   /**
119    * @brief Get the depth index
120    * @return The depth index
121    */
122   int GetDepthIndex() const
123   {
124     return mDepthIndex;
125   }
126
127   /**
128    * Called when an actor with this renderer is added to the stage
129    */
130   void OnStageConnect();
131
132   /*
133    * Called when an actor with this renderer is removed from the stage
134    */
135   void OnStageDisconnect();
136
137   /**
138    * Prepare the object for rendering.
139    * This is called by the UpdateManager when an object is due to be rendered in the current frame.
140    * @param[in] updateBufferIndex The current update buffer index.
141    */
142   void PrepareRender( BufferIndex updateBufferIndex );
143
144   /*
145    * Retrieve the Render thread renderer
146    * @return The associated render thread renderer
147    */
148   Render::Renderer& GetRenderer();
149
150   /**
151    * Check whether the renderer has been marked as ready to render
152    * ready means that renderer has all resources and should produce correct result
153    * complete means all resources have finished loading
154    * It's possible that renderer is complete but not ready,
155    * for example in case of resource load failed
156    * @param[out] ready TRUE if the renderer has resources to render
157    * @param[out] complete TRUE if the renderer resources are complete
158    */
159   void GetReadyAndComplete( bool& ready, bool& complete ) const;
160
161   /**
162    * Query whether the renderer is fully opaque, fully transparent or transparent.
163    * @param[in] updateBufferIndex The current update buffer index.
164    * @return OPAQUE if fully opaque, TRANSPARENT if fully transparent and TRANSLUCENT if in between
165    */
166   Opacity GetOpacity( BufferIndex updateBufferIndex, const Node& node ) const;
167
168   /**
169    * Query whether the renderer is currently in use by an actor on the stage
170    */
171   bool IsReferenced() const
172   {
173     return mReferenceCount > 0;
174   }
175
176
177 public: // Implementation of ObjectOwnerContainer template methods
178   /**
179    * Connect the object to the scene graph
180    *
181    * @param[in] sceneController The scene controller - used for sending messages to render thread
182    * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
183    */
184   void ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
185
186   /**
187    * Disconnect the object from the scene graph
188    * @param[in] sceneController The scene controller - used for sending messages to render thread
189    * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
190    */
191   void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
192
193 public: // Implementation of ConnectionChangePropagator
194   /**
195    * @copydoc ConnectionChangePropagator::AddObserver
196    */
197   void AddConnectionObserver(ConnectionChangePropagator::Observer& observer){};
198
199   /**
200    * @copydoc ConnectionChangePropagator::RemoveObserver
201    */
202   void RemoveConnectionObserver(ConnectionChangePropagator::Observer& observer){};
203
204 public:
205
206
207 public: // UniformMap::Observer
208   /**
209    * @copydoc UniformMap::Observer::UniformMappingsChanged
210    */
211   virtual void UniformMappingsChanged( const UniformMap& mappings );
212
213 public: // ConnectionChangePropagator::Observer
214
215   /**
216    * @copydoc ConnectionChangePropagator::ConnectionsChanged
217    */
218   virtual void ConnectionsChanged( PropertyOwner& owner );
219
220   /**
221    * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
222    */
223   virtual void ConnectedUniformMapChanged( );
224
225   /**
226    * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
227    */
228   virtual void ObservedObjectDestroyed(PropertyOwner& owner);
229
230 public: // PropertyOwner implementation
231   /**
232    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
233    */
234   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex ){};
235
236 public: // From UniformMapDataProvider
237
238   /**
239    * @copydoc UniformMapDataProvider::GetUniformMapChanged
240    */
241   virtual bool GetUniformMapChanged( BufferIndex bufferIndex ) const{ return mUniformMapChanged[bufferIndex];}
242
243   /**
244    * @copydoc UniformMapDataProvider::GetUniformMap
245    */
246   virtual const CollectedUniformMap& GetUniformMap( BufferIndex bufferIndex ) const;
247
248 private:
249
250   /**
251    * Protected constructor; See also Renderer::New()
252    */
253   Renderer();
254
255   /**
256    * Helper function to create a new render data provider
257    * @return the new (initialized) data provider
258    */
259   RenderDataProvider* NewRenderDataProvider();
260
261   SceneController* mSceneController;  ///< Used for initializing renderers whilst attached
262   Render::Renderer*  mRenderer;    ///< Raw pointer to the new renderer (that's owned by RenderManager)
263   Material*             mMaterial;    ///< The material this renderer uses. (Not owned)
264   Geometry*             mGeometry;    ///< The geometry this renderer uses. (Not owned)
265
266   CollectedUniformMap mCollectedUniformMap[2]; ///< Uniform maps collected by the renderer
267   unsigned int mReferenceCount;                ///< Number of nodes currently using this renderer
268   unsigned int mRegenerateUniformMap;          ///< 2 if the map should be regenerated, 1 if it should be copied.
269   bool         mUniformMapChanged[2];          ///< Records if the uniform map has been altered this frame
270   bool         mResendDataProviders;           ///< True if the data providers should be resent to the renderer
271   bool         mResendGeometry;                ///< True if geometry should be resent to the renderer
272   bool         mResourcesReady;                ///< Set during the Update algorithm; true if the attachment has resources ready for the current frame.
273   bool         mFinishedResourceAcquisition;   ///< Set during DoPrepareResources; true if ready & all resource acquisition has finished (successfully or otherwise)
274
275 public:
276   int mDepthIndex; ///< Used only in PrepareRenderInstructions
277 };
278
279
280 /// Messages
281 inline void SetMaterialMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Material& material )
282 {
283   typedef MessageDoubleBuffered1< Renderer, Material* > LocalType;
284
285   // Reserve some memory inside the message queue
286   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
287
288   // Construct message in the message queue memory; note that delete should not be called on the return value
289   new (slot) LocalType( &renderer, &Renderer::SetMaterial, const_cast<Material*>(&material) );
290 }
291
292 inline void SetGeometryMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Geometry& geometry )
293 {
294   typedef MessageDoubleBuffered1< Renderer, Geometry* > LocalType;
295
296   // Reserve some memory inside the message queue
297   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
298
299   // Construct message in the message queue memory; note that delete should not be called on the return value
300   new (slot) LocalType( &renderer, &Renderer::SetGeometry, const_cast<Geometry*>(&geometry) );
301 }
302
303 inline void SetDepthIndexMessage( EventThreadServices& eventThreadServices, const Renderer& attachment, int depthIndex )
304 {
305   typedef MessageValue1< Renderer, int > LocalType;
306
307   // Reserve some memory inside the message queue
308   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
309
310   // Construct message in the message queue memory; note that delete should not be called on the return value
311   new (slot) LocalType( &attachment, &Renderer::SetDepthIndex, depthIndex );
312 }
313
314 inline void OnStageConnectMessage( EventThreadServices& eventThreadServices, const Renderer& renderer )
315 {
316   typedef Message< Renderer > LocalType;
317
318   // Reserve some memory inside the message queue
319   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
320
321   // Construct message in the message queue memory; note that delete should not be called on the return value
322   new (slot) LocalType( &renderer, &Renderer::OnStageConnect );
323 }
324
325 inline void OnStageDisconnectMessage( EventThreadServices& eventThreadServices, const Renderer& renderer )
326 {
327   typedef Message< Renderer > LocalType;
328
329   // Reserve some memory inside the message queue
330   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
331
332   // Construct message in the message queue memory; note that delete should not be called on the return value
333   new (slot) LocalType( &renderer, &Renderer::OnStageDisconnect );
334 }
335
336 } // namespace SceneGraph
337 } // namespace Internal
338 } // namespace Dali
339
340 #endif //  DALI_INTERNAL_SCENE_GRAPH_RENDERER_H