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