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