Messages and ownership of update objects
[platform/core/uifw/dali-core.git] / dali / internal / update / geometry / 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/public-api/geometry/geometry.h>
21 #include <dali/internal/common/event-to-update.h>
22 #include <dali/internal/update/common/double-buffered.h>
23 #include <dali/internal/update/common/property-owner.h>
24 #include <dali/internal/update/common/scene-graph-property-buffer.h>
25
26 namespace Dali
27 {
28 namespace Internal
29 {
30 namespace SceneGraph
31 {
32
33 /**
34  * This scene graph object is a property owner. It describes a geometry using a
35  * number of PropertyBuffers acting as Vertex buffers.
36  */
37 class Geometry : public PropertyOwner
38 {
39 public:
40   typedef Dali::Geometry::GeometryType GeometryType;
41
42   typedef OwnerContainer< PropertyBuffer* > VertexBuffers;
43
44   /**
45    * Constructor
46    */
47   Geometry();
48
49   /**
50    * Destructor
51    */
52   ~Geometry();
53
54   /**
55    * Add a property buffer to be used as a vertex buffer
56    */
57   void AddVertexBuffer( PropertyBuffer* vertexBuffer );
58
59   /**
60    * Remove a property buffer to be used as a vertex buffer
61    * @param[in] vertexBuffer the associated vertex buffer to remove
62    */
63   void RemoveVertexBuffer( PropertyBuffer* vertexBuffer );
64
65   /**
66    * Set the buffer to be used as a source of indices for the geometry
67    * @param[in] indexBuffer the Property buffer describing the indexes for Line, Triangle tyes.
68    */
69   void SetIndexBuffer( PropertyBuffer* indexBuffer );
70
71   /**
72    * Clear the index buffer if it is no longer required, e.g. if changing geometry type
73    * to POINTS.
74    */
75   void ClearIndexBuffer();
76
77   /**
78    * Set the type of geometry to draw (Points, Lines, Triangles, etc)
79    * @param[in] geometryType The geometry type
80    */
81   void SetGeometryType( GeometryType geometryType );
82
83 public: // GeometryDataProvider
84   /**
85    * Get the vertex buffers of the geometry
86    * @return A const reference to the vertex buffers
87    */
88   virtual const VertexBuffers& GetVertexBuffers();
89
90   /**
91    * Get the index buffer of the geometry
92    * @return A const reference to the index buffer
93    */
94   virtual const PropertyBuffer& GetIndexBuffer();
95
96   /**
97    * Get the type of geometry to draw
98    */
99   virtual GeometryType GetGeometryType( );
100
101 private:
102   VertexBuffers mVertexBuffers; ///< The vertex buffers
103   OwnerPointer<PropertyBuffer> mIndexBuffer;  ///< The index buffer if required
104   GeometryType mGeometryType;   ///< The type of geometry (tris/lines etc)
105 };
106
107 inline void AddVertexBufferMessage( EventToUpdate& eventToUpdate, const Geometry& geometry, PropertyBuffer& vertexBuffer )
108 {
109   typedef MessageValue1< Geometry, OwnerPointer<PropertyBuffer> > LocalType;
110
111   // Reserve some memory inside the message queue
112   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
113
114   // Construct message in the message queue memory; note that delete should not be called on the return value
115   new (slot) LocalType( &geometry, &Geometry::AddVertexBuffer, &vertexBuffer );
116 }
117
118 inline void RemoveVertexBufferMessage( EventToUpdate& eventToUpdate, const Geometry& geometry, PropertyBuffer& vertexBuffer )
119 {
120   typedef MessageValue1< Geometry, PropertyBuffer* > LocalType;
121
122   // Reserve some memory inside the message queue
123   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
124
125   // Construct message in the message queue memory; note that delete should not be called on the return value
126   new (slot) LocalType( &geometry, &Geometry::RemoveVertexBuffer, &vertexBuffer );
127 }
128
129 inline void SetIndexBufferMessage( EventToUpdate& eventToUpdate, const Geometry& geometry, PropertyBuffer& indexBuffer )
130 {
131   typedef MessageValue1< Geometry, OwnerPointer< PropertyBuffer > > LocalType;
132
133   // Reserve some memory inside the message queue
134   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
135
136   // Construct message in the message queue memory; note that delete should not be called on the return value
137   new (slot) LocalType( &geometry, &Geometry::SetIndexBuffer, &indexBuffer );
138 }
139
140 inline void ClearIndexBufferMessage( EventToUpdate& eventToUpdate, const Geometry& geometry )
141 {
142   typedef Message< Geometry > LocalType;
143
144   // Reserve some memory inside the message queue
145   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
146
147   // Construct message in the message queue memory; note that delete should not be called on the return value
148   new (slot) LocalType( &geometry, &Geometry::ClearIndexBuffer );
149 }
150
151 } // namespace SceneGraph
152
153 // Declare enum as a message parameter type
154 template <> struct ParameterType< SceneGraph::Geometry::GeometryType > : public BasicType< SceneGraph::Geometry::GeometryType > {};
155
156 namespace SceneGraph
157 {
158
159 inline void SetGeometryTypeMessage( EventToUpdate& eventToUpdate, const Geometry& geometry, SceneGraph::Geometry::GeometryType geometryType )
160 {
161   typedef MessageValue1< Geometry, SceneGraph::Geometry::GeometryType > LocalType;
162
163   // Reserve some memory inside the message queue
164   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
165
166   // Construct message in the message queue memory; note that delete should not be called on the return value
167   new (slot) LocalType( &geometry, &Geometry::SetGeometryType, geometryType );
168 }
169
170 } // namespace SceneGraph
171 } // namespace Internal
172 } // namespace Dali
173
174 #endif // DALI_INTERNAL_SCENE_GRAPH_GEOMETRY_H