Changed passing of data providers to renderers
[platform/core/uifw/dali-core.git] / dali / internal / update / node-attachments / scene-graph-renderer-attachment.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_RENDERER_ATTACHMENT_H
2 #define DALI_INTERNAL_SCENE_GRAPH_RENDERER_ATTACHMENT_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 #include <dali/internal/event/common/event-thread-services.h>
21 #include <dali/internal/update/common/double-buffered.h>
22 #include <dali/internal/update/common/property-owner.h>
23 #include <dali/internal/update/common/animatable-property.h>
24 #include <dali/internal/update/common/scene-graph-connection-observers.h>
25 #include <dali/internal/update/controllers/render-message-dispatcher.h>
26 #include <dali/internal/update/controllers/scene-controller.h>
27 #include <dali/internal/update/node-attachments/scene-graph-renderable-attachment.h>
28 #include <dali/internal/render/data-providers/render-data-provider.h>
29
30 namespace Dali
31 {
32 namespace Internal
33 {
34 namespace SceneGraph
35 {
36 class Material;
37 class Geometry;
38 class NewRenderer;
39
40 /**
41  * The renderer attachment is the SceneGraph equivalent of Dali::Renderer. It is used to create an instance of a geometry and material for rendering, and is attached to an actor.
42  *
43  * It observes it's children (Material and Geometry) for connection change and for uniform map change, and observer's it's actor parent for uniform map change - this allows it to re-generate the uniform maps used by its RenderThread equivalent class.
44  *
45  * Lifetime and ownership
46  * It is created when a Dali::Renderer is created, and sent to UpdateManager. At this point
47  * Initialize is called on the object, but ownership is NOT taken by UpdateManager.
48
49  * When a Dali::Renderer is added to an actor, then this object becomes the node attachment
50  * for that actor, and the node takes ownership. It will create the Render::NewRenderer object
51  * in the Update thread on reciept of the connection message.
52
53  * When it's not attached to an actor, it is still possible to send messages to this
54  * object, to, e.g., set the material, or a property.
55  *
56  * @todo MESH_REWORK On merge with RenderableAttachment, change owner of all attachments to UpdateManager.
57  */
58 class RendererAttachment : public RenderableAttachment,
59                            public PropertyOwner,
60                            public UniformMapDataProvider,
61                            public UniformMap::Observer,
62                            public ConnectionObservers::Observer
63 {
64 public:
65   /**
66    * Create a new renderer attachment.
67    * @return The newly allocated RendererAttachment
68    */
69   static RendererAttachment* New();
70
71   /**
72    * Constructor
73    */
74   RendererAttachment();
75
76   /**
77    * Destructor
78    */
79   virtual ~RendererAttachment();
80
81   /**
82    * @copydoc RenderableAttachment::Initialize2().
83    */
84   virtual void Initialize2( BufferIndex updateBufferIndex );
85
86   /**
87    * @copydoc RenderableAttachment::OnDestroy2().
88    */
89   virtual void OnDestroy2();
90
91   /**
92    * @copydoc NodeAttachment::ConnectedToSceneGraph()
93    */
94   virtual void ConnectedToSceneGraph();
95
96   /**
97    * @copydoc NodeAttachment::DisconnectedFromSceneGraph()
98    */
99   virtual void DisconnectedFromSceneGraph();
100
101   /**
102    * Set the material for the renderer
103    * @param[in] bufferIndex The current frame's buffer index
104    * @param[in] material The material this renderer will use
105    */
106   void SetMaterial( BufferIndex bufferIndex, const Material* material);
107
108   /**
109    * Get the material of this renderer
110    * @return the material this renderer uses
111    */
112   const Material& GetMaterial() const;
113
114   /**
115    * Set the geometry for the renderer
116    * @param[in] bufferIndex The current frame's buffer index
117    * @param[in] geometry The geometry this renderer will use
118    */
119   void SetGeometry( BufferIndex bufferIndex, const Geometry* geometry);
120
121   /**
122    * Get the geometry of this renderer
123    * @return the geometry this renderer uses
124    */
125   const Geometry& GetGeometry() const;
126
127   /**
128    * Get the depth index
129    * @return The depth index of the renderer attachment in the current frame
130    */
131   int GetDepthIndex( BufferIndex bufferIndex ) const ;
132
133 protected: // From RenderableAttachment
134   /**
135    * @copydoc RenderableAttachment::GetRenderer().
136    */
137   virtual Renderer& GetRenderer();
138
139   /**
140    * @copydoc RenderableAttachment::GetRenderer().
141    */
142   virtual const Renderer& GetRenderer() const;
143
144   /**
145    * @copydoc RenderableAttachment::DoPrepareRender()
146    */
147   virtual void DoPrepareRender( BufferIndex updateBufferIndex );
148
149   /**
150    * @copydoc RenderableAttachment::IsFullyOpaque()
151    */
152   virtual bool IsFullyOpaque( BufferIndex updateBufferIndex );
153
154   /**
155    * @copydoc RenderableAttachment::SizeChanged()
156    */
157   virtual void SizeChanged( BufferIndex updateBufferIndex );
158
159   /**
160    * @copydoc RenderableAttachment::DoPrepareResources()
161    */
162   virtual bool DoPrepareResources( BufferIndex updateBufferIndex,
163                                    ResourceManager& resourceManager );
164
165 protected: // From ConnectionObserver
166   /**
167    * @copydoc ConnectionObservers::Observer::ConnectionsChanged
168    */
169   virtual void ConnectionsChanged(PropertyOwner& object);
170
171   /**
172    * @copydoc ConnectionObservers::Observer::ConnectedUniformMapChanged
173    */
174   virtual void ConnectedUniformMapChanged();
175
176 protected: // From UniformMap::Observer
177   /**
178    * @copydoc UniformMap::Observer::UniformMappingsChanged
179    */
180   virtual void UniformMappingsChanged( const UniformMap& mappings );
181
182 protected: // From UniformMapDataProvider
183   /**
184    * @copydoc UniformMapDataProvider::GetUniformMapChanged
185    */
186   virtual bool GetUniformMapChanged( BufferIndex bufferIndex ) const;
187
188   /**
189    * @copydoc UniformMapDataProvider::GetUniformMap
190    */
191   virtual const CollectedUniformMap& GetUniformMap( BufferIndex bufferIndex ) const;
192
193 private:
194   /**
195    * Add any new mappings from map into the current map.
196    * This doesn't override any existing mappings.
197    * @param[in] localMap The map of collected uniforms.
198    * @param[in] map The map to import into the local map
199    */
200   void AddMappings( CollectedUniformMap& localMap, const UniformMap& map );
201
202 private:
203   NewRenderer* mRenderer; ///< Raw pointer to the new renderer (that's owned by RenderManager)
204
205   Material* mMaterial; ///< The material this renderer uses. (Not owned)
206   Geometry* mGeometry; ///< The geometry this renderer uses. (Not owned)
207
208   CollectedUniformMap mCollectedUniformMap[2];
209   int mRegenerateUniformMap;  ///< 2 if the map should be regenerated, 1 if it should be copied.
210   bool mUniformMapChanged[2]; ///< Records if the uniform map has been altered this frame
211   bool mResendDataProviders;  ///< True if the data providers should be resent to the renderer
212
213 public: // Properties
214
215   AnimatableProperty<int> mDepthIndex; ///< Used only in PrepareRenderInstructions
216 };
217
218 // Messages for RendererAttachment
219
220 inline void SetMaterialMessage( EventThreadServices& eventThreadServices, const RendererAttachment& attachment, const Material& material )
221 {
222   typedef MessageDoubleBuffered1< RendererAttachment, const Material* > LocalType;
223
224   // Reserve some memory inside the message queue
225   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
226
227   // Construct message in the message queue memory; note that delete should not be called on the return value
228   new (slot) LocalType( &attachment, &RendererAttachment::SetMaterial, &material );
229 }
230
231 inline void SetGeometryMessage( EventThreadServices& eventThreadServices, const RendererAttachment& attachment, const Geometry& geometry )
232 {
233   typedef MessageDoubleBuffered1< RendererAttachment, const Geometry* > LocalType;
234
235   // Reserve some memory inside the message queue
236   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
237
238   // Construct message in the message queue memory; note that delete should not be called on the return value
239   new (slot) LocalType( &attachment, &RendererAttachment::SetGeometry, &geometry );
240 }
241
242 } // namespace SceneGraph
243 } // namespace Internal
244 } // namespace Dali
245
246
247 #endif //  DALI_INTERNAL_SCENE_GRAPH_RENDERER_ATTACHMENT_H