1 #ifndef DALI_INTERNAL_SCENE_GRAPH_GEOMETRY_H
2 #define DALI_INTERNAL_SCENE_GRAPH_GEOMETRY_H
5 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
40 * This class encapsulates vertex data and index data used to describe a mesh.
45 typedef Dali::Geometry::Type Type;
55 * Initialize the geometry object with the Graphics API when added to UpdateManager
57 * @param[in] graphics The Graphics API
59 void Initialize( Integration::Graphics::Graphics& graphics );
62 * Adds a property buffer to the geometry
63 * @param[in] dataProvider The PropertyBuffer data provider
65 void AddPropertyBuffer( SceneGraph::PropertyBuffer* propertyBuffer );
68 * Set the data for the index buffer to be used by the geometry
69 * @param[in] indices A vector containing the indices
71 void SetIndexBuffer( Dali::Vector<unsigned short>& indices );
74 * Removes a PropertyBuffer from the geometry
75 * @param[in] propertyBuffer The property buffer to be removed
77 void RemovePropertyBuffer( const SceneGraph::PropertyBuffer* propertyBuffer );
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
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
90 bool AttributesChanged() const
92 return mAttributesChanged;
96 * Sets the geometry type
97 * @param[in] type The new geometry type
99 void SetType( Type type )
101 mGeometryType = type;
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
112 void UploadAndDraw( Graphics::API::Controller& controller,
113 BufferIndex bufferIndex,
114 Vector<uint32_t>& attributeLocation,
115 size_t elementBufferOffset,
116 size_t elementBufferCount );
121 const Vector< SceneGraph::PropertyBuffer* >& GetVertexBuffers() const
123 return mVertexBuffers;
126 const Dali::Vector< unsigned short>& GetIndices() const
132 Integration::Graphics::Graphics* mGraphics; ///< Graphics interface object
135 Vector< SceneGraph::PropertyBuffer* > mVertexBuffers;
136 Dali::Vector< unsigned short> mIndices;
141 bool mIndicesChanged : 1;
142 bool mHasBeenUpdated : 1;
143 bool mAttributesChanged : 1;
148 // Enums can be passed via message by templating a suitable ParameterType.
149 template <> struct ParameterType< Dali::Geometry::Type > : public BasicType< Dali::Geometry::Type > {};
154 // Custom message type for SetIndexBuffer() used to move data with Vector::Swap()
155 class IndexBufferMessage : public MessageBase
160 * Constructor which does a Vector::Swap()
162 IndexBufferMessage( SceneGraph::Geometry* geometry, Dali::Vector<unsigned short>& indices )
164 mGeometry( geometry )
166 mIndices.Swap( indices );
172 virtual ~IndexBufferMessage()
177 * @copydoc MessageBase::Process
179 virtual void Process( BufferIndex /*bufferIndex*/ )
181 DALI_ASSERT_DEBUG( mGeometry && "Message does not have an object" );
182 mGeometry->SetIndexBuffer( mIndices );
186 SceneGraph::Geometry* mGeometry;
187 Dali::Vector<unsigned short> mIndices;
190 inline void SetIndexBufferMessage( EventThreadServices& eventThreadServices, SceneGraph::Geometry& geometry, Dali::Vector<unsigned short>& indices )
192 typedef IndexBufferMessage LocalType;
194 // Reserve some memory inside the message queue
195 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ), false );
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 );
201 inline void AttachVertexBufferMessage( EventThreadServices& eventThreadServices, SceneGraph::Geometry& geometry, const SceneGraph::PropertyBuffer& vertexBuffer )
203 typedef MessageValue1< SceneGraph::Geometry, SceneGraph::PropertyBuffer* > LocalType;
205 // Reserve some memory inside the message queue
206 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ), false );
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) );
212 inline void RemoveVertexBufferMessage( EventThreadServices& eventThreadServices, SceneGraph::Geometry& geometry, const SceneGraph::PropertyBuffer& vertexBuffer )
214 typedef MessageValue1< SceneGraph::Geometry, const SceneGraph::PropertyBuffer* > LocalType;
216 // Reserve some memory inside the message queue
217 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ), false );
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 );
223 inline void SetGeometryTypeMessage( EventThreadServices& eventThreadServices, SceneGraph::Geometry& geometry, Dali::Geometry::Type geometryType )
225 typedef MessageValue1< Geometry, Dali::Geometry::Type > LocalType;
227 // Reserve some memory inside the message queue
228 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ), false );
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 );
234 } // namespace SceneGraph
235 } // namespace Internal
238 #endif // DALI_INTERNAL_SCENE_GRAPH_GEOMETRY_H