Merge "Removed redundant type field" into devel/master
[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( "requires-depth-test",   BOOLEAN,  true, false, true, Dali::Geometry::Property::REQUIRES_DEPTH_TEST )
43 DALI_PROPERTY_TABLE_END( DEFAULT_ACTOR_PROPERTY_START_INDEX )
44
45 const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> GEOMETRY_IMPL = { DEFAULT_PROPERTY_DETAILS };
46
47 BaseHandle Create()
48 {
49   return Dali::Geometry::New();
50 }
51
52 TypeRegistration mType( typeid( Dali::Geometry ), typeid( Dali::Handle ), Create );
53
54 } // unnamed namespace
55
56 GeometryPtr Geometry::New()
57 {
58   GeometryPtr geometry( new Geometry() );
59   geometry->Initialize();
60   return geometry;
61 }
62
63 std::size_t Geometry::AddVertexBuffer( PropertyBuffer& vertexBuffer )
64 {
65   mVertexBuffers.push_back( &vertexBuffer );
66   SceneGraph::AddVertexBufferMessage( GetEventThreadServices(), *mSceneObject, *vertexBuffer.GetRenderObject() );
67   return mVertexBuffers.size() - 1u;
68 }
69
70 std::size_t Geometry::GetNumberOfVertexBuffers() const
71 {
72   return mVertexBuffers.size();
73 }
74
75 void Geometry::RemoveVertexBuffer( std::size_t index )
76 {
77   const Render::PropertyBuffer& renderPropertyBuffer = static_cast<const Render::PropertyBuffer&>( *(mVertexBuffers[index]->GetRenderObject()) );
78   SceneGraph::RemoveVertexBufferMessage( GetEventThreadServices(), *mSceneObject, renderPropertyBuffer );
79
80   mVertexBuffers.erase( mVertexBuffers.begin() + index );
81 }
82
83 void Geometry::SetIndexBuffer( PropertyBuffer& indexBuffer )
84 {
85   mIndexBuffer = &indexBuffer;
86   SceneGraph::SetIndexBufferMessage( GetEventThreadServices(), *mSceneObject, *indexBuffer.GetRenderObject() );
87 }
88
89 void Geometry::SetGeometryType( Dali::Geometry::GeometryType geometryType )
90 {
91   if( geometryType != mGeometryType )
92   {
93     SceneGraph::SetGeometryTypeMessage(GetEventThreadServices(),
94                                        *mSceneObject,
95                                        geometryType );
96
97     mGeometryType = geometryType;
98   }
99 }
100
101 Dali::Geometry::GeometryType Geometry::GetGeometryType() const
102 {
103   return mGeometryType;
104 }
105
106 void Geometry::SetRequiresDepthTesting( bool requiresDepthTest )
107 {
108   if( requiresDepthTest != mRequiresDepthTest )
109   {
110     SceneGraph::SetGeometryRequiresDepthTestMessage(GetEventThreadServices(),
111                                                     *mSceneObject,
112                                                     requiresDepthTest );
113
114     mRequiresDepthTest = requiresDepthTest;
115   }
116 }
117
118 bool Geometry::GetRequiresDepthTesting() const
119 {
120   return mRequiresDepthTest;
121 }
122
123 const SceneGraph::Geometry* Geometry::GetGeometrySceneObject() const
124 {
125   return mSceneObject;
126 }
127
128 unsigned int Geometry::GetDefaultPropertyCount() const
129 {
130   return GEOMETRY_IMPL.GetDefaultPropertyCount();
131 }
132
133 void Geometry::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
134 {
135   GEOMETRY_IMPL.GetDefaultPropertyIndices( indices );
136 }
137
138 const char* Geometry::GetDefaultPropertyName(Property::Index index) const
139 {
140   return GEOMETRY_IMPL.GetDefaultPropertyName( index );
141 }
142
143 Property::Index Geometry::GetDefaultPropertyIndex( const std::string& name ) const
144 {
145   return GEOMETRY_IMPL.GetDefaultPropertyIndex( name );
146 }
147
148 bool Geometry::IsDefaultPropertyWritable( Property::Index index ) const
149 {
150   return GEOMETRY_IMPL.IsDefaultPropertyWritable( index );
151 }
152
153 bool Geometry::IsDefaultPropertyAnimatable( Property::Index index ) const
154 {
155   return GEOMETRY_IMPL.IsDefaultPropertyAnimatable( index );
156 }
157
158 bool Geometry::IsDefaultPropertyAConstraintInput( Property::Index index ) const
159 {
160   return GEOMETRY_IMPL.IsDefaultPropertyAConstraintInput( index );
161 }
162
163 Property::Type Geometry::GetDefaultPropertyType( Property::Index index ) const
164 {
165   return GEOMETRY_IMPL.GetDefaultPropertyType( index );
166 }
167
168 void Geometry::SetDefaultProperty( Property::Index index,
169                                    const Property::Value& propertyValue )
170 {
171   switch( index )
172   {
173     case Dali::Geometry::Property::GEOMETRY_TYPE :
174     {
175       Dali::Geometry::GeometryType geometryType = static_cast<Dali::Geometry::GeometryType>(propertyValue.Get<int>());
176       if( geometryType != mGeometryType )
177       {
178         SceneGraph::SetGeometryTypeMessage(GetEventThreadServices(), *mSceneObject, geometryType );
179         mGeometryType = geometryType;
180       }
181       break;
182     }
183     case Dali::Geometry::Property::REQUIRES_DEPTH_TEST :
184     {
185       bool requiresDepthTest = propertyValue.Get<bool>();
186       if( requiresDepthTest != mRequiresDepthTest )
187       {
188         SceneGraph::SetGeometryRequiresDepthTestMessage(GetEventThreadServices(), *mSceneObject, requiresDepthTest);
189         mRequiresDepthTest = requiresDepthTest;
190       }
191       break;
192     }
193   }
194 }
195
196 void Geometry::SetSceneGraphProperty( Property::Index index,
197                                       const PropertyMetadata& entry,
198                                       const Property::Value& value )
199 {
200   GEOMETRY_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
201 }
202
203 Property::Value Geometry::GetDefaultProperty( Property::Index index ) const
204 {
205   Property::Value value;
206
207   switch( index )
208   {
209     case Dali::Geometry::Property::GEOMETRY_TYPE :
210     {
211       if( mSceneObject )
212       {
213         value = mGeometryType;
214       }
215       break;
216     }
217     case Dali::Geometry::Property::REQUIRES_DEPTH_TEST :
218     {
219       if( mSceneObject )
220       {
221         value = mRequiresDepthTest;
222       }
223       break;
224     }
225   }
226
227   return value;
228 }
229
230 const SceneGraph::PropertyOwner* Geometry::GetPropertyOwner() const
231 {
232   return mSceneObject;
233 }
234
235 const SceneGraph::PropertyOwner* Geometry::GetSceneObject() const
236 {
237   return mSceneObject;
238 }
239
240 const SceneGraph::PropertyBase* Geometry::GetSceneObjectAnimatableProperty( Property::Index index ) const
241 {
242   const SceneGraph::PropertyBase* property = NULL;
243   if( OnStage() )
244   {
245     property = GEOMETRY_IMPL.GetRegisteredSceneGraphProperty ( this,
246                                                                &Geometry::FindAnimatableProperty,
247                                                                &Geometry::FindCustomProperty,
248                                                                index );
249   }
250
251   return property;
252 }
253
254 const PropertyInputImpl* Geometry::GetSceneObjectInputProperty( Property::Index index ) const
255 {
256   const PropertyInputImpl* property = NULL;
257
258   if( OnStage() )
259   {
260     const SceneGraph::PropertyBase* baseProperty =
261         GEOMETRY_IMPL.GetRegisteredSceneGraphProperty ( this,
262                                                         &Geometry::FindAnimatableProperty,
263                                                         &Geometry::FindCustomProperty,
264                                                         index );
265
266     property = static_cast<const PropertyInputImpl*>( baseProperty );
267   }
268
269   return property;
270 }
271
272 bool Geometry::OnStage() const
273 {
274   return mOnStage;
275 }
276
277 void Geometry::Connect()
278 {
279   mOnStage = true;
280 }
281
282 void Geometry::Disconnect()
283 {
284   mOnStage = false;
285 }
286
287 Geometry::Geometry()
288 : mSceneObject( NULL ),
289   mIndexBuffer( NULL ),
290   mGeometryType(Dali::Geometry::TRIANGLES),
291   mRequiresDepthTest(false),
292   mOnStage( false )
293 {
294 }
295
296 void Geometry::Initialize()
297 {
298   EventThreadServices& eventThreadServices = GetEventThreadServices();
299   SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
300
301   mSceneObject = new SceneGraph::Geometry();
302   AddMessage( updateManager, updateManager.GetGeometryOwner(), *mSceneObject );
303
304   eventThreadServices.RegisterObject( this );
305 }
306
307 Geometry::~Geometry()
308 {
309   if( EventThreadServices::IsCoreRunning() )
310   {
311     EventThreadServices& eventThreadServices = GetEventThreadServices();
312     SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
313     RemoveMessage( updateManager, updateManager.GetGeometryOwner(), *mSceneObject );
314
315     eventThreadServices.UnregisterObject( this );
316   }
317 }
318
319
320 } // namespace Internal
321 } // namespace Dali