Removing rendering backend
[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) 2018 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/public-api/common/dali-vector.h>
21 #include <dali/public-api/rendering/geometry.h>
22 #include <dali/devel-api/common/owner-container.h>
23 #include <dali/graphics-api/graphics-api-controller.h>
24 #include <dali/integration-api/graphics/graphics.h>
25 #include <dali/internal/common/message.h>
26 #include <dali/internal/common/owner-pointer.h>
27 #include <dali/internal/common/buffer-index.h>
28 #include <dali/internal/common/owner-pointer.h>
29 #include <dali/internal/event/common/event-thread-services.h>
30
31 namespace Dali
32 {
33 namespace Internal
34 {
35 namespace SceneGraph
36 {
37 class PropertyBuffer;
38
39 /**
40  * This class encapsulates vertex data and index data used to describe a mesh.
41  */
42 class Geometry
43 {
44 public:
45   typedef Dali::Geometry::Type Type;
46
47   Geometry();
48
49   /**
50    * Destructor
51    */
52   ~Geometry();
53
54   /**
55    * Initialize the geometry object with the Graphics API when added to UpdateManager
56    *
57    * @param[in] graphics The Graphics API
58    */
59   void Initialize( Integration::Graphics::Graphics& graphics );
60
61   /**
62    * Adds a property buffer to the geometry
63    * @param[in] dataProvider The PropertyBuffer data provider
64    */
65   void AddPropertyBuffer( SceneGraph::PropertyBuffer* propertyBuffer );
66
67   /**
68    * Set the data for the index buffer to be used by the geometry
69    * @param[in] indices A vector containing the indices
70    */
71   void SetIndexBuffer( Dali::Vector<unsigned short>& indices );
72
73   /**
74    * Removes a PropertyBuffer from the geometry
75    * @param[in] propertyBuffer The property buffer to be removed
76    */
77   void RemovePropertyBuffer(  const SceneGraph::PropertyBuffer* propertyBuffer );
78
79   /**
80    * Gets the attribute locations on the shader for the attributes defined in the geometry RenderBuffers
81    * @param[out] attributeLocation The vector where the attributes locations will be stored
82    * @param[in] program The program
83    * @param[in] bufferIndex The current buffer index
84    */
85
86   /**
87    * Chack if the attributes for the geometry have changed
88    * @return True if vertex buffers have been added or removed since last frame, false otherwise
89    */
90   bool AttributesChanged() const
91   {
92     return mAttributesChanged;
93   }
94
95   /**
96    * Sets the geometry type
97    * @param[in] type The new geometry type
98    */
99   void SetType( Type type )
100   {
101     mGeometryType = type;
102   }
103
104   /**
105    * Upload the geometry if it has changed, set up the attributes and perform
106    * the Draw call corresponding to the geometry type
107    * @param[in] bufferIndex The current buffer index
108    * @param[in] attributeLocation The location for the attributes in the shader
109    * @param[in] elementBufferOffset The index of first element to draw if index buffer bound
110    * @param[in] elementBufferCount Number of elements to draw if index buffer bound, uses whole buffer when 0
111    */
112   void UploadAndDraw( Graphics::API::Controller& controller,
113                       BufferIndex bufferIndex,
114                       Vector<uint32_t>& attributeLocation,
115                       size_t elementBufferOffset,
116                       size_t elementBufferCount );
117
118   /**
119    * @return
120    */
121   const Vector< SceneGraph::PropertyBuffer* >& GetVertexBuffers() const
122   {
123     return mVertexBuffers;
124   }
125
126   const Dali::Vector< unsigned short>& GetIndices() const
127   {
128     return mIndices;
129   }
130
131 private:
132   Integration::Graphics::Graphics* mGraphics; ///< Graphics interface object
133
134   // PropertyBuffers
135   Vector< SceneGraph::PropertyBuffer* > mVertexBuffers;
136   Dali::Vector< unsigned short> mIndices;
137
138   Type mGeometryType;
139
140   // Booleans
141   bool mIndicesChanged : 1;
142   bool mHasBeenUpdated : 1;
143   bool mAttributesChanged : 1;
144 };
145
146 } // SceneGraph
147
148 // Enums can be passed via message by templating a suitable ParameterType.
149 template <> struct ParameterType< Dali::Geometry::Type > : public BasicType< Dali::Geometry::Type > {};
150
151 namespace SceneGraph
152 {
153
154 // Custom message type for SetIndexBuffer() used to move data with Vector::Swap()
155 class IndexBufferMessage : public MessageBase
156 {
157 public:
158
159   /**
160    * Constructor which does a Vector::Swap()
161    */
162   IndexBufferMessage( SceneGraph::Geometry* geometry, Dali::Vector<unsigned short>& indices )
163   : MessageBase(),
164     mGeometry( geometry )
165   {
166     mIndices.Swap( indices );
167   }
168
169   /**
170    * Virtual destructor
171    */
172   virtual ~IndexBufferMessage()
173   {
174   }
175
176   /**
177    * @copydoc MessageBase::Process
178    */
179   virtual void Process( BufferIndex /*bufferIndex*/ )
180   {
181     DALI_ASSERT_DEBUG( mGeometry && "Message does not have an object" );
182     mGeometry->SetIndexBuffer( mIndices );
183   }
184
185 private:
186   SceneGraph::Geometry* mGeometry;
187   Dali::Vector<unsigned short> mIndices;
188 };
189
190 inline void SetIndexBufferMessage( EventThreadServices& eventThreadServices, SceneGraph::Geometry& geometry, Dali::Vector<unsigned short>& indices )
191 {
192   typedef IndexBufferMessage LocalType;
193
194   // Reserve some memory inside the message queue
195   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ), false );
196
197   // Construct message in the message queue memory; note that delete should not be called on the return value
198   new (slot) LocalType( &geometry, indices );
199 }
200
201 inline void AttachVertexBufferMessage( EventThreadServices& eventThreadServices, SceneGraph::Geometry& geometry, const SceneGraph::PropertyBuffer& vertexBuffer )
202 {
203   typedef MessageValue1< SceneGraph::Geometry, SceneGraph::PropertyBuffer* > LocalType;
204
205   // Reserve some memory inside the message queue
206   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ), false );
207
208   // Construct message in the message queue memory; note that delete should not be called on the return value
209   new (slot) LocalType( &geometry, &Geometry::AddPropertyBuffer, const_cast<SceneGraph::PropertyBuffer*>(&vertexBuffer) );
210 }
211
212 inline void RemoveVertexBufferMessage( EventThreadServices& eventThreadServices, SceneGraph::Geometry& geometry, const SceneGraph::PropertyBuffer& vertexBuffer )
213 {
214   typedef MessageValue1< SceneGraph::Geometry, const SceneGraph::PropertyBuffer* > LocalType;
215
216   // Reserve some memory inside the message queue
217   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ), false );
218
219   // Construct message in the message queue memory; note that delete should not be called on the return value
220   new (slot) LocalType( &geometry, &Geometry::RemovePropertyBuffer, &vertexBuffer );
221 }
222
223 inline void SetGeometryTypeMessage( EventThreadServices& eventThreadServices, SceneGraph::Geometry& geometry, Dali::Geometry::Type geometryType )
224 {
225   typedef MessageValue1< Geometry, Dali::Geometry::Type > LocalType;
226
227   // Reserve some memory inside the message queue
228   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ), false );
229
230   // Construct message in the message queue memory; note that delete should not be called on the return value
231   new (slot) LocalType( &geometry, &Geometry::SetType, geometryType );
232 }
233
234 } // namespace SceneGraph
235 } // namespace Internal
236 } // namespace Dali
237
238 #endif // DALI_INTERNAL_SCENE_GRAPH_GEOMETRY_H