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