bbed300d6c7c447109792a5d1459eb842011c246
[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( "geometry-half-extents", VECTOR3,  true, true,   true, Dali::Geometry::Property::GEOMETRY_HALF_EXTENTS )
44 DALI_PROPERTY( "requires-depth-test",   BOOLEAN,  true, false,  true, Dali::Geometry::Property::REQUIRES_DEPTH_TEST )
45 DALI_PROPERTY_TABLE_END( DEFAULT_ACTOR_PROPERTY_START_INDEX )
46
47 const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> GEOMETRY_IMPL = { DEFAULT_PROPERTY_DETAILS };
48
49 BaseHandle Create()
50 {
51   return Dali::Geometry::New();
52 }
53
54 TypeRegistration mType( typeid( Dali::Geometry ), typeid( Dali::Handle ), Create );
55
56 } // unnamed namespace
57
58 GeometryPtr Geometry::New()
59 {
60   GeometryPtr geometry( new Geometry() );
61   geometry->Initialize();
62   return geometry;
63 }
64
65 std::size_t Geometry::AddVertexBuffer( PropertyBuffer& vertexBuffer )
66 {
67   PropertyBufferConnector connector;
68   connector.Set( vertexBuffer, OnStage() );
69   mVertexBufferConnectors.push_back( connector );
70
71   const SceneGraph::PropertyBuffer& sceneGraphPropertyBuffer = dynamic_cast<const SceneGraph::PropertyBuffer&>( *vertexBuffer.GetSceneObject() );
72
73   SceneGraph::AddVertexBufferMessage( GetEventThreadServices(), *mSceneObject, sceneGraphPropertyBuffer );
74
75   return mVertexBufferConnectors.size() - 1u;
76 }
77
78 std::size_t Geometry::GetNumberOfVertexBuffers() const
79 {
80   return mVertexBufferConnectors.size();
81 }
82
83 void Geometry::RemoveVertexBuffer( std::size_t index )
84 {
85   mVertexBufferConnectors.erase( mVertexBufferConnectors.begin() + index );
86 }
87
88 void Geometry::SetIndexBuffer( PropertyBuffer& indexBuffer )
89 {
90   mIndexBufferConnector.Set( indexBuffer, OnStage() );
91
92   const SceneGraph::PropertyBuffer& sceneGraphPropertyBuffer = dynamic_cast<const SceneGraph::PropertyBuffer&>( *indexBuffer.GetSceneObject() );
93
94   SceneGraph::SetIndexBufferMessage( GetEventThreadServices(), *mSceneObject, sceneGraphPropertyBuffer );
95 }
96
97 void Geometry::SetGeometryType( Dali::Geometry::GeometryType geometryType )
98 {
99   if( NULL != mSceneObject )
100   {
101     SceneGraph::DoubleBufferedPropertyMessage<int>::Send(
102       GetEventThreadServices(),
103       mSceneObject,
104       &mSceneObject->mGeometryType,
105       &SceneGraph::DoubleBufferedProperty<int>::Set,
106       static_cast<int>(geometryType) );
107   }
108 }
109
110 Dali::Geometry::GeometryType Geometry::GetGeometryType() const
111 {
112   return mSceneObject->GetGeometryType(GetEventThreadServices().GetEventBufferIndex());
113 }
114
115 void Geometry::SetRequiresDepthTesting( bool requiresDepthTest )
116 {
117   if( NULL != mSceneObject )
118   {
119     SceneGraph::DoubleBufferedPropertyMessage<bool>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mRequiresDepthTest, &SceneGraph::DoubleBufferedProperty<bool>::Set, static_cast<int>(requiresDepthTest) );
120   }
121 }
122
123 bool Geometry::GetRequiresDepthTesting() const
124 {
125   if( mSceneObject )
126   {
127     // mSceneObject is being used in a separate thread; copy the value from the previous update
128     return mSceneObject->GetRequiresDepthTesting(GetEventThreadServices().GetEventBufferIndex());
129   }
130   return false;
131 }
132
133 const SceneGraph::Geometry* Geometry::GetGeometrySceneObject() const
134 {
135   return mSceneObject;
136 }
137
138 unsigned int Geometry::GetDefaultPropertyCount() const
139 {
140   return GEOMETRY_IMPL.GetDefaultPropertyCount();
141 }
142
143 void Geometry::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
144 {
145   GEOMETRY_IMPL.GetDefaultPropertyIndices( indices );
146 }
147
148 const char* Geometry::GetDefaultPropertyName(Property::Index index) const
149 {
150   return GEOMETRY_IMPL.GetDefaultPropertyName( index );
151 }
152
153 Property::Index Geometry::GetDefaultPropertyIndex( const std::string& name ) const
154 {
155   return GEOMETRY_IMPL.GetDefaultPropertyIndex( name );
156 }
157
158 bool Geometry::IsDefaultPropertyWritable( Property::Index index ) const
159 {
160   return GEOMETRY_IMPL.IsDefaultPropertyWritable( index );
161 }
162
163 bool Geometry::IsDefaultPropertyAnimatable( Property::Index index ) const
164 {
165   return GEOMETRY_IMPL.IsDefaultPropertyAnimatable( index );
166 }
167
168 bool Geometry::IsDefaultPropertyAConstraintInput( Property::Index index ) const
169 {
170   return GEOMETRY_IMPL.IsDefaultPropertyAConstraintInput( index );
171 }
172
173 Property::Type Geometry::GetDefaultPropertyType( Property::Index index ) const
174 {
175   return GEOMETRY_IMPL.GetDefaultPropertyType( index );
176 }
177
178 void Geometry::SetDefaultProperty( Property::Index index,
179                                    const Property::Value& propertyValue )
180 {
181   switch( index )
182   {
183     case Dali::Geometry::Property::GEOMETRY_TYPE :
184     {
185       SceneGraph::DoubleBufferedPropertyMessage<int>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mGeometryType, &SceneGraph::DoubleBufferedProperty<int>::Set, propertyValue.Get<int>() );
186       break;
187     }
188     case Dali::Geometry::Property::GEOMETRY_CENTER :
189     {
190       SceneGraph::AnimatablePropertyMessage<Vector3>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mCenter, &SceneGraph::AnimatableProperty<Vector3>::Bake, propertyValue.Get<Vector3>() );
191       break;
192     }
193
194     case Dali::Geometry::Property::GEOMETRY_HALF_EXTENTS :
195     {
196       SceneGraph::AnimatablePropertyMessage<Vector3>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mHalfExtents, &SceneGraph::AnimatableProperty<Vector3>::Bake, propertyValue.Get<Vector3>() );
197       break;
198     }
199
200     case Dali::Geometry::Property::REQUIRES_DEPTH_TEST :
201     {
202       SceneGraph::DoubleBufferedPropertyMessage<bool>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mRequiresDepthTest, &SceneGraph::DoubleBufferedProperty<bool>::Set, propertyValue.Get<bool>() );
203       break;
204     }
205   }
206 }
207
208 void Geometry::SetSceneGraphProperty( Property::Index index,
209                                       const PropertyMetadata& entry,
210                                       const Property::Value& value )
211 {
212   GEOMETRY_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
213 }
214
215 Property::Value Geometry::GetDefaultProperty( Property::Index index ) const
216 {
217   BufferIndex bufferIndex = GetEventThreadServices().GetEventBufferIndex();
218   Property::Value value;
219
220   switch( index )
221   {
222     case Dali::Geometry::Property::GEOMETRY_TYPE :
223     {
224       if( mSceneObject )
225       {
226         value = mSceneObject->mGeometryType[bufferIndex];
227       }
228       break;
229     }
230     case Dali::Geometry::Property::GEOMETRY_CENTER :
231     {
232       if( mSceneObject )
233       {
234         value = mSceneObject->mCenter[bufferIndex];
235       }
236       break;
237     }
238
239     case Dali::Geometry::Property::GEOMETRY_HALF_EXTENTS :
240     {
241       if( mSceneObject )
242       {
243         value = mSceneObject->mHalfExtents[bufferIndex];
244       }
245       break;
246     }
247
248     case Dali::Geometry::Property::REQUIRES_DEPTH_TEST :
249     {
250       if( mSceneObject )
251       {
252         value = mSceneObject->mRequiresDepthTest[bufferIndex];
253       }
254       break;
255     }
256   }
257
258   return value;
259 }
260
261 const SceneGraph::PropertyOwner* Geometry::GetPropertyOwner() const
262 {
263   return mSceneObject;
264 }
265
266 const SceneGraph::PropertyOwner* Geometry::GetSceneObject() const
267 {
268   return mSceneObject;
269 }
270
271 const SceneGraph::PropertyBase* Geometry::GetSceneObjectAnimatableProperty( Property::Index index ) const
272 {
273   const SceneGraph::PropertyBase* property = NULL;
274
275   if( OnStage() )
276   {
277     property = GEOMETRY_IMPL.GetRegisteredSceneGraphProperty ( this,
278                                                                &Geometry::FindAnimatableProperty,
279                                                                &Geometry::FindCustomProperty,
280                                                                index );
281
282     if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
283     {
284       switch(index)
285       {
286         case Dali::Geometry::Property::GEOMETRY_CENTER :
287         {
288           property = &mSceneObject->mCenter;
289           break;
290         }
291         case Dali::Geometry::Property::GEOMETRY_HALF_EXTENTS :
292         {
293           property = &mSceneObject->mHalfExtents;
294           break;
295         }
296         default:
297         {
298           DALI_ASSERT_ALWAYS( 0 && "Property is not animatable" );
299           break;
300         }
301       }
302     }
303   }
304   return property;
305 }
306
307 const PropertyInputImpl* Geometry::GetSceneObjectInputProperty( Property::Index index ) const
308 {
309   const PropertyInputImpl* property = NULL;
310
311   if( OnStage() )
312   {
313     const SceneGraph::PropertyBase* baseProperty =
314       GEOMETRY_IMPL.GetRegisteredSceneGraphProperty ( this,
315                                                       &Geometry::FindAnimatableProperty,
316                                                       &Geometry::FindCustomProperty,
317                                                       index );
318
319     property = static_cast<const PropertyInputImpl*>( baseProperty );
320
321     if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
322     {
323       switch(index)
324       {
325         case Dali::Geometry::Property::GEOMETRY_TYPE :
326         {
327           property = &mSceneObject->mGeometryType;
328           break;
329         }
330         case Dali::Geometry::Property::GEOMETRY_CENTER :
331         {
332           property = &mSceneObject->mCenter;
333           break;
334         }
335         case Dali::Geometry::Property::GEOMETRY_HALF_EXTENTS :
336         {
337           property = &mSceneObject->mHalfExtents;
338           break;
339         }
340         case Dali::Geometry::Property::REQUIRES_DEPTH_TEST :
341         {
342           property = &mSceneObject->mRequiresDepthTest;
343           break;
344         }
345         default:
346         {
347           DALI_ASSERT_ALWAYS( 0 && "Property cannot be a constraint input");
348           break;
349         }
350       }
351     }
352   }
353
354   return property;
355 }
356
357 int Geometry::GetPropertyComponentIndex( Property::Index index ) const
358 {
359   // @todo MESH_REWORK - Change this if component properties are added for center/half-extent
360   return Property::INVALID_COMPONENT_INDEX;
361 }
362
363 bool Geometry::OnStage() const
364 {
365   return mOnStage;
366 }
367
368 void Geometry::Connect()
369 {
370   mOnStage = true;
371
372   PropertyBufferConnectorContainer::const_iterator end = mVertexBufferConnectors.end();
373   for( PropertyBufferConnectorContainer::iterator it = mVertexBufferConnectors.begin();
374        it < end;
375        ++it )
376   {
377     it->OnStageConnect();
378   }
379   mIndexBufferConnector.OnStageConnect();
380 }
381
382 void Geometry::Disconnect()
383 {
384   mOnStage = false;
385
386   PropertyBufferConnectorContainer::const_iterator end = mVertexBufferConnectors.end();
387   for( PropertyBufferConnectorContainer::iterator it = mVertexBufferConnectors.begin();
388        it < end;
389        ++it )
390   {
391     it->OnStageDisconnect();
392   }
393   mIndexBufferConnector.OnStageDisconnect();
394 }
395
396 Geometry::Geometry()
397 : mSceneObject( NULL ),
398   mOnStage( false )
399 {
400 }
401
402 void Geometry::Initialize()
403 {
404   EventThreadServices& eventThreadServices = GetEventThreadServices();
405   SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
406
407   mSceneObject = new SceneGraph::Geometry();
408   AddMessage( updateManager, updateManager.GetGeometryOwner(), *mSceneObject );
409
410   eventThreadServices.RegisterObject( this );
411 }
412
413 Geometry::~Geometry()
414 {
415   if( EventThreadServices::IsCoreRunning() )
416   {
417     EventThreadServices& eventThreadServices = GetEventThreadServices();
418     SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
419     RemoveMessage( updateManager, updateManager.GetGeometryOwner(), *mSceneObject );
420
421     eventThreadServices.UnregisterObject( this );
422   }
423 }
424
425
426 } // namespace Internal
427 } // namespace Dali