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