Remove scene graph property buffer
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / geometry-impl.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/rendering/geometry-impl.h> // Dali::Internal::Geometry
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/object/type-registry.h>
23
24 #include <dali/internal/event/common/object-impl-helper.h> // Dali::Internal::ObjectHelper
25 #include <dali/internal/event/common/property-helper.h> // DALI_PROPERTY_TABLE_BEGIN, DALI_PROPERTY, DALI_PROPERTY_TABLE_END
26 #include <dali/internal/update/common/double-buffered-property.h>
27 #include <dali/internal/update/manager/update-manager.h>
28
29 namespace Dali
30 {
31 namespace Internal
32 {
33
34 namespace
35 {
36
37 /**
38  *            |name                    |type     |writable|animatable|constraint-input|enum for index-checking|
39  */
40 DALI_PROPERTY_TABLE_BEGIN
41 DALI_PROPERTY( "geometry-type",         STRING,   true, false,  true, Dali::Geometry::Property::GEOMETRY_TYPE )
42 DALI_PROPERTY( "geometry-center",       VECTOR3,  true, true,   true, Dali::Geometry::Property::GEOMETRY_CENTER )
43 DALI_PROPERTY( "requires-depth-test",   BOOLEAN,  true, false,  true, Dali::Geometry::Property::REQUIRES_DEPTH_TEST )
44 DALI_PROPERTY_TABLE_END( DEFAULT_ACTOR_PROPERTY_START_INDEX )
45
46 const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> GEOMETRY_IMPL = { DEFAULT_PROPERTY_DETAILS };
47
48 BaseHandle Create()
49 {
50   return Dali::Geometry::New();
51 }
52
53 TypeRegistration mType( typeid( Dali::Geometry ), typeid( Dali::Handle ), Create );
54
55 } // unnamed namespace
56
57 GeometryPtr Geometry::New()
58 {
59   GeometryPtr geometry( new Geometry() );
60   geometry->Initialize();
61   return geometry;
62 }
63
64 std::size_t Geometry::AddVertexBuffer( PropertyBuffer& vertexBuffer )
65 {
66   mVertexBuffers.push_back( &vertexBuffer );
67   SceneGraph::AddVertexBufferMessage( GetEventThreadServices(), *mSceneObject, *vertexBuffer.GetRenderObject() );
68   return mVertexBuffers.size() - 1u;
69 }
70
71 std::size_t Geometry::GetNumberOfVertexBuffers() const
72 {
73   return mVertexBuffers.size();
74 }
75
76 void Geometry::RemoveVertexBuffer( std::size_t index )
77 {
78   const Render::PropertyBuffer& renderPropertyBuffer = static_cast<const Render::PropertyBuffer&>( *(mVertexBuffers[index]->GetRenderObject()) );
79   SceneGraph::RemoveVertexBufferMessage( GetEventThreadServices(), *mSceneObject, renderPropertyBuffer );
80
81   mVertexBuffers.erase( mVertexBuffers.begin() + index );
82 }
83
84 void Geometry::SetIndexBuffer( PropertyBuffer& indexBuffer )
85 {
86   mIndexBuffer = &indexBuffer;
87   SceneGraph::SetIndexBufferMessage( GetEventThreadServices(), *mSceneObject, *indexBuffer.GetRenderObject() );
88 }
89
90 void Geometry::SetGeometryType( Dali::Geometry::GeometryType geometryType )
91 {
92   if( NULL != mSceneObject )
93   {
94     SceneGraph::DoubleBufferedPropertyMessage<int>::Send(
95       GetEventThreadServices(),
96       mSceneObject,
97       &mSceneObject->mGeometryType,
98       &SceneGraph::DoubleBufferedProperty<int>::Set,
99       static_cast<int>(geometryType) );
100   }
101 }
102
103 Dali::Geometry::GeometryType Geometry::GetGeometryType() const
104 {
105   return mSceneObject->GetGeometryType(GetEventThreadServices().GetEventBufferIndex());
106 }
107
108 void Geometry::SetRequiresDepthTesting( bool requiresDepthTest )
109 {
110   if( NULL != mSceneObject )
111   {
112     SceneGraph::DoubleBufferedPropertyMessage<bool>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mRequiresDepthTest, &SceneGraph::DoubleBufferedProperty<bool>::Set, static_cast<int>(requiresDepthTest) );
113   }
114 }
115
116 bool Geometry::GetRequiresDepthTesting() const
117 {
118   if( mSceneObject )
119   {
120     // mSceneObject is being used in a separate thread; copy the value from the previous update
121     return mSceneObject->GetRequiresDepthTesting(GetEventThreadServices().GetEventBufferIndex());
122   }
123   return false;
124 }
125
126 const SceneGraph::Geometry* Geometry::GetGeometrySceneObject() const
127 {
128   return mSceneObject;
129 }
130
131 unsigned int Geometry::GetDefaultPropertyCount() const
132 {
133   return GEOMETRY_IMPL.GetDefaultPropertyCount();
134 }
135
136 void Geometry::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
137 {
138   GEOMETRY_IMPL.GetDefaultPropertyIndices( indices );
139 }
140
141 const char* Geometry::GetDefaultPropertyName(Property::Index index) const
142 {
143   return GEOMETRY_IMPL.GetDefaultPropertyName( index );
144 }
145
146 Property::Index Geometry::GetDefaultPropertyIndex( const std::string& name ) const
147 {
148   return GEOMETRY_IMPL.GetDefaultPropertyIndex( name );
149 }
150
151 bool Geometry::IsDefaultPropertyWritable( Property::Index index ) const
152 {
153   return GEOMETRY_IMPL.IsDefaultPropertyWritable( index );
154 }
155
156 bool Geometry::IsDefaultPropertyAnimatable( Property::Index index ) const
157 {
158   return GEOMETRY_IMPL.IsDefaultPropertyAnimatable( index );
159 }
160
161 bool Geometry::IsDefaultPropertyAConstraintInput( Property::Index index ) const
162 {
163   return GEOMETRY_IMPL.IsDefaultPropertyAConstraintInput( index );
164 }
165
166 Property::Type Geometry::GetDefaultPropertyType( Property::Index index ) const
167 {
168   return GEOMETRY_IMPL.GetDefaultPropertyType( index );
169 }
170
171 void Geometry::SetDefaultProperty( Property::Index index,
172                                    const Property::Value& propertyValue )
173 {
174   switch( index )
175   {
176     case Dali::Geometry::Property::GEOMETRY_TYPE :
177     {
178       SceneGraph::DoubleBufferedPropertyMessage<int>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mGeometryType, &SceneGraph::DoubleBufferedProperty<int>::Set, propertyValue.Get<int>() );
179       break;
180     }
181     case Dali::Geometry::Property::GEOMETRY_CENTER :
182     {
183       SceneGraph::AnimatablePropertyMessage<Vector3>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mCenter, &SceneGraph::AnimatableProperty<Vector3>::Bake, propertyValue.Get<Vector3>() );
184       break;
185     }
186     case Dali::Geometry::Property::REQUIRES_DEPTH_TEST :
187     {
188       SceneGraph::DoubleBufferedPropertyMessage<bool>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mRequiresDepthTest, &SceneGraph::DoubleBufferedProperty<bool>::Set, propertyValue.Get<bool>() );
189       break;
190     }
191   }
192 }
193
194 void Geometry::SetSceneGraphProperty( Property::Index index,
195                                       const PropertyMetadata& entry,
196                                       const Property::Value& value )
197 {
198   GEOMETRY_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
199 }
200
201 Property::Value Geometry::GetDefaultProperty( Property::Index index ) const
202 {
203   BufferIndex bufferIndex = GetEventThreadServices().GetEventBufferIndex();
204   Property::Value value;
205
206   switch( index )
207   {
208     case Dali::Geometry::Property::GEOMETRY_TYPE :
209     {
210       if( mSceneObject )
211       {
212         value = mSceneObject->mGeometryType[bufferIndex];
213       }
214       break;
215     }
216     case Dali::Geometry::Property::GEOMETRY_CENTER :
217     {
218       if( mSceneObject )
219       {
220         value = mSceneObject->mCenter[bufferIndex];
221       }
222       break;
223     }
224
225     case Dali::Geometry::Property::REQUIRES_DEPTH_TEST :
226     {
227       if( mSceneObject )
228       {
229         value = mSceneObject->mRequiresDepthTest[bufferIndex];
230       }
231       break;
232     }
233   }
234
235   return value;
236 }
237
238 const SceneGraph::PropertyOwner* Geometry::GetPropertyOwner() const
239 {
240   return mSceneObject;
241 }
242
243 const SceneGraph::PropertyOwner* Geometry::GetSceneObject() const
244 {
245   return mSceneObject;
246 }
247
248 const SceneGraph::PropertyBase* Geometry::GetSceneObjectAnimatableProperty( Property::Index index ) const
249 {
250   const SceneGraph::PropertyBase* property = NULL;
251
252   if( OnStage() )
253   {
254     property = GEOMETRY_IMPL.GetRegisteredSceneGraphProperty ( this,
255                                                                &Geometry::FindAnimatableProperty,
256                                                                &Geometry::FindCustomProperty,
257                                                                index );
258
259     if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
260     {
261       switch(index)
262       {
263         case Dali::Geometry::Property::GEOMETRY_CENTER :
264         {
265           property = &mSceneObject->mCenter;
266           break;
267         }
268         default:
269         {
270           DALI_ASSERT_ALWAYS( 0 && "Property is not animatable" );
271           break;
272         }
273       }
274     }
275   }
276   return property;
277 }
278
279 const PropertyInputImpl* Geometry::GetSceneObjectInputProperty( Property::Index index ) const
280 {
281   const PropertyInputImpl* property = NULL;
282
283   if( OnStage() )
284   {
285     const SceneGraph::PropertyBase* baseProperty =
286       GEOMETRY_IMPL.GetRegisteredSceneGraphProperty ( this,
287                                                       &Geometry::FindAnimatableProperty,
288                                                       &Geometry::FindCustomProperty,
289                                                       index );
290
291     property = static_cast<const PropertyInputImpl*>( baseProperty );
292
293     if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
294     {
295       switch(index)
296       {
297         case Dali::Geometry::Property::GEOMETRY_TYPE :
298         {
299           property = &mSceneObject->mGeometryType;
300           break;
301         }
302         case Dali::Geometry::Property::GEOMETRY_CENTER :
303         {
304           property = &mSceneObject->mCenter;
305           break;
306         }
307         case Dali::Geometry::Property::REQUIRES_DEPTH_TEST :
308         {
309           property = &mSceneObject->mRequiresDepthTest;
310           break;
311         }
312         default:
313         {
314           DALI_ASSERT_ALWAYS( 0 && "Property cannot be a constraint input");
315           break;
316         }
317       }
318     }
319   }
320
321   return property;
322 }
323
324 int Geometry::GetPropertyComponentIndex( Property::Index index ) const
325 {
326   // @todo MESH_REWORK - Change this if component properties are added for center/half-extent
327   return Property::INVALID_COMPONENT_INDEX;
328 }
329
330 bool Geometry::OnStage() const
331 {
332   return mOnStage;
333 }
334
335 void Geometry::Connect()
336 {
337   mOnStage = true;
338 }
339
340 void Geometry::Disconnect()
341 {
342   mOnStage = false;
343 }
344
345 Geometry::Geometry()
346 : mIndexBuffer( NULL ),
347   mSceneObject( NULL ),
348   mOnStage( false )
349 {
350 }
351
352 void Geometry::Initialize()
353 {
354   EventThreadServices& eventThreadServices = GetEventThreadServices();
355   SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
356
357   mSceneObject = new SceneGraph::Geometry();
358   AddMessage( updateManager, updateManager.GetGeometryOwner(), *mSceneObject );
359
360   eventThreadServices.RegisterObject( this );
361 }
362
363 Geometry::~Geometry()
364 {
365   if( EventThreadServices::IsCoreRunning() )
366   {
367     EventThreadServices& eventThreadServices = GetEventThreadServices();
368     SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
369     RemoveMessage( updateManager, updateManager.GetGeometryOwner(), *mSceneObject );
370
371     eventThreadServices.UnregisterObject( this );
372   }
373 }
374
375
376 } // namespace Internal
377 } // namespace Dali