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