Allow sharing RenderGeometries across multiple Renderers
[platform/core/uifw/dali-core.git] / dali / internal / update / rendering / scene-graph-geometry.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_GEOMETRY_H
2 #define DALI_INTERNAL_SCENE_GRAPH_GEOMETRY_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/devel-api/rendering/geometry.h>
21 #include <dali/internal/event/common/event-thread-services.h>
22 #include <dali/internal/update/common/animatable-property.h>
23 #include <dali/internal/update/common/double-buffered.h>
24 #include <dali/internal/update/common/double-buffered-property.h>
25 #include <dali/internal/update/common/property-owner.h>
26 #include <dali/internal/update/common/property-boolean.h>
27 #include <dali/internal/update/common/uniform-map.h>
28 #include <dali/internal/update/common/scene-graph-connection-change-propagator.h>
29 #include <dali/internal/update/common/scene-graph-property-buffer.h>
30 #include <dali/internal/render/data-providers/geometry-data-provider.h>
31 #include <dali/internal/render/data-providers/render-data-provider.h>
32
33 namespace Dali
34 {
35 namespace Internal
36 {
37 namespace SceneGraph
38 {
39 class SceneController;
40 class RenderGeometry;
41
42 /**
43  * This scene graph object is a property owner. It describes a geometry using a
44  * number of PropertyBuffers acting as Vertex buffers.
45  */
46 class Geometry : public PropertyOwner, public GeometryDataProvider, public UniformMap::Observer
47 {
48 public:
49
50   /**
51    * Constructor
52    */
53   Geometry();
54
55   /**
56    * Destructor
57    */
58   virtual ~Geometry();
59
60   /**
61    * Add a property buffer to be used as a vertex buffer
62    */
63   void AddVertexBuffer( PropertyBuffer* vertexBuffer );
64
65   /**
66    * Remove a property buffer to be used as a vertex buffer
67    * @param[in] vertexBuffer the associated vertex buffer to remove
68    */
69   void RemoveVertexBuffer( PropertyBuffer* vertexBuffer );
70
71   /**
72    * Set the buffer to be used as a source of indices for the geometry
73    * @param[in] indexBuffer the Property buffer describing the indexes for Line, Triangle tyes.
74    */
75   void SetIndexBuffer( PropertyBuffer* indexBuffer );
76
77   /**
78    * Clear the index buffer if it is no longer required, e.g. if changing geometry type
79    * to POINTS.
80    */
81   void ClearIndexBuffer();
82
83   /**
84    * Set the type of geometry to draw (Points, Lines, Triangles, etc)
85    * @param[in] bufferIndex Index for double buffered values
86    * @param[in] geometryType The geometry type
87    */
88   void SetGeometryType( BufferIndex bufferIndex, GeometryType geometryType );
89
90   /**
91    * Connect the object to the scene graph
92    *
93    * @param[in] sceneController The scene controller - used for sending messages to render thread
94    * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
95    */
96   void ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
97
98   /**
99    * Disconnect the object from the scene graph
100    * @param[in] sceneController The scene controller - used for sending messages to render thread
101    * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
102    */
103   void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
104
105   /**
106    * @copydoc ConnectionObservers::AddObserver
107    */
108   void AddConnectionObserver(ConnectionChangePropagator::Observer& observer);
109
110   /**
111    * @copydoc ConnectionObservers::RemoveObserver
112    */
113   void RemoveConnectionObserver(ConnectionChangePropagator::Observer& observer);
114
115 public: // UniformMap::Observer
116   /**
117    * @copydoc UniformMap::Observer::UniformMappingsChanged
118    */
119   virtual void UniformMappingsChanged( const UniformMap& mappings );
120
121   /**
122    * Get the vertex buffers of the geometry
123    * @return A const reference to the vertex buffers
124    */
125   Vector<PropertyBuffer*>& GetVertexBuffers();
126
127   /**
128    * Get the index buffer of the geometry
129    * @return A pointer to the index buffer if it exists, or NULL if it doesn't.
130    */
131    PropertyBuffer* GetIndexBuffer();
132
133    /**
134     * Gets the associated RenderGeometry
135     * @param[in] sceneController The scene controller
136     * @return The RenderGeometry associated to this Geometry
137     */
138    RenderGeometry* GetRenderGeometry(SceneController* sceneController);
139
140    /**
141     * Called from renderers using this geometry when they get disconnected from the scenegraph
142     */
143    void OnRendererDisconnect();
144
145
146 public: // GeometryDataProvider
147   /**
148    * Get the type of geometry to draw
149    * @param[in] bufferIndex Index for double buffered values
150    */
151   virtual GeometryType GetGeometryType( BufferIndex bufferIndex ) const;
152
153   /**
154    * Returns true if this geometry requires depth testing, e.g. if it is
155    * a set of vertices with z != 0
156    * @param[in] bufferIndex Index for double buffered values
157    */
158   virtual bool GetRequiresDepthTesting( BufferIndex bufferIndex ) const;
159
160 protected: // From PropertyOwner
161   /**
162    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
163    */
164   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
165
166 private:
167
168   /**
169    * @brief Calculate the extents of geometry contained in a vertex buffer.
170    *
171    * @param[in] vertexBuffer pointer to a vertex buffer.
172    */
173   void CalculateExtents( PropertyBuffer* vertexBuffer );
174
175   Vector<PropertyBuffer*> mVertexBuffers; ///< The vertex buffers
176   PropertyBuffer* mIndexBuffer;  ///< The index buffer if required
177   ConnectionChangePropagator mConnectionObservers;
178
179   RenderGeometry*               mRenderGeometry;
180   SceneController*              mSceneController;
181   unsigned int                  mRendererRefCount;
182
183 public: // Properties
184   AnimatableProperty<Vector3>   mCenter;
185   AnimatableProperty<Vector3>   mHalfExtents;
186   AnimatableProperty<float>     mRadius;
187   DoubleBufferedProperty<int>   mGeometryType;
188   DoubleBufferedProperty<bool>  mRequiresDepthTest;
189 };
190
191 inline void AddVertexBufferMessage( EventThreadServices& eventThreadServices , const Geometry& geometry, const PropertyBuffer& vertexBuffer )
192 {
193   typedef MessageValue1< Geometry, PropertyBuffer* > LocalType;
194
195   // Reserve some memory inside the message queue
196   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
197
198   // Construct message in the message queue memory; note that delete should not be called on the return value
199   new (slot) LocalType( &geometry, &Geometry::AddVertexBuffer, const_cast<PropertyBuffer*>(&vertexBuffer) );
200 }
201
202 inline void RemoveVertexBufferMessage( EventThreadServices& eventThreadServices, const Geometry& geometry, const PropertyBuffer& vertexBuffer )
203 {
204   typedef MessageValue1< Geometry, PropertyBuffer* > LocalType;
205
206   // Reserve some memory inside the message queue
207   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
208
209   // Construct message in the message queue memory; note that delete should not be called on the return value
210   new (slot) LocalType( &geometry, &Geometry::RemoveVertexBuffer, const_cast<PropertyBuffer*>(&vertexBuffer) );
211 }
212
213 inline void SetIndexBufferMessage( EventThreadServices& eventThreadServices, const Geometry& geometry, const PropertyBuffer& indexBuffer )
214 {
215   typedef MessageValue1< Geometry, PropertyBuffer* > LocalType;
216
217   // Reserve some memory inside the message queue
218   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
219
220   // Construct message in the message queue memory; note that delete should not be called on the return value
221   new (slot) LocalType( &geometry, &Geometry::SetIndexBuffer, const_cast<PropertyBuffer*>(&indexBuffer) );
222 }
223
224 inline void ClearIndexBufferMessage( EventThreadServices& eventThreadServices, const Geometry& geometry )
225 {
226   typedef Message< Geometry > LocalType;
227
228   // Reserve some memory inside the message queue
229   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
230
231   // Construct message in the message queue memory; note that delete should not be called on the return value
232   new (slot) LocalType( &geometry, &Geometry::ClearIndexBuffer );
233 }
234
235 } // namespace SceneGraph
236
237 // Declare enum as a message parameter type
238 template <> struct ParameterType< SceneGraph::Geometry::GeometryType > : public BasicType< SceneGraph::Geometry::GeometryType > {};
239
240 namespace SceneGraph
241 {
242
243 inline void SetGeometryTypeMessage( EventThreadServices& eventThreadServices, const Geometry& geometry, SceneGraph::Geometry::GeometryType geometryType )
244 {
245   typedef MessageDoubleBuffered1< Geometry, SceneGraph::Geometry::GeometryType > LocalType;
246
247   // Reserve some memory inside the message queue
248   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
249
250   // Construct message in the message queue memory; note that delete should not be called on the return value
251   new (slot) LocalType( &geometry, &Geometry::SetGeometryType, geometryType );
252 }
253
254 } // namespace SceneGraph
255 } // namespace Internal
256 } // namespace Dali
257
258 #endif // DALI_INTERNAL_SCENE_GRAPH_GEOMETRY_H