Merge "Removed DepthIndex methods from public-api" into devel/new_mesh
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / renderer-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/renderer-impl.h> // Dali::Internal::Renderer
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/devel-api/rendering/renderer.h> // Dali::Renderer
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/event/common/property-input-impl.h>
27 #include <dali/internal/update/node-attachments/scene-graph-renderer-attachment.h>
28 #include <dali/internal/update/manager/update-manager.h>
29
30 namespace Dali
31 {
32 namespace Internal
33 {
34
35 namespace
36 {
37
38 /**
39  *            |name          |type     |writable|animatable|constraint-input|enum for index-checking|
40  */
41 DALI_PROPERTY_TABLE_BEGIN
42 DALI_PROPERTY( "depth-index", INTEGER, true, false, false, Dali::Renderer::Property::DEPTH_INDEX )
43 DALI_PROPERTY_TABLE_END( DEFAULT_OBJECT_PROPERTY_START_INDEX )
44
45 const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> RENDERER_IMPL = { DEFAULT_PROPERTY_DETAILS };
46
47 BaseHandle Create()
48 {
49   return Dali::BaseHandle();
50 }
51
52 TypeRegistration mType( typeid( Dali::Renderer ), typeid( Dali::Handle ), Create );
53
54 } // unnamed namespace
55
56 RendererPtr Renderer::New()
57 {
58   RendererPtr rendererPtr( new Renderer() );
59   rendererPtr->Initialize();
60   return rendererPtr;
61 }
62
63 void Renderer::SetGeometry( Geometry& geometry )
64 {
65   mGeometryConnector.Set( geometry, OnStage() );
66   const SceneGraph::Geometry* geometrySceneObject = geometry.GetGeometrySceneObject();
67
68   SetGeometryMessage( GetEventThreadServices(), *mSceneObject, *geometrySceneObject );
69 }
70
71 Geometry* Renderer::GetGeometry() const
72 {
73   return mGeometryConnector.Get().Get();
74 }
75
76 void Renderer::SetMaterial( Material& material )
77 {
78   mMaterialConnector.Set( material, OnStage() );
79   const SceneGraph::Material* materialSceneObject = material.GetMaterialSceneObject();
80   SetMaterialMessage( GetEventThreadServices(), *mSceneObject, *materialSceneObject );
81 }
82
83 Material* Renderer::GetMaterial() const
84 {
85   return mMaterialConnector.Get().Get();
86 }
87
88 void Renderer::SetDepthIndex( int depthIndex )
89 {
90   if ( mDepthIndex != depthIndex )
91   {
92     mDepthIndex = depthIndex;
93     SetDepthIndexMessage( GetEventThreadServices(), *mSceneObject, depthIndex );
94   }
95 }
96
97 int Renderer::GetDepthIndex() const
98 {
99   return mDepthIndex;
100 }
101
102 SceneGraph::RendererAttachment* Renderer::GetRendererSceneObject()
103 {
104   return mSceneObject;
105 }
106
107 unsigned int Renderer::GetDefaultPropertyCount() const
108 {
109   return RENDERER_IMPL.GetDefaultPropertyCount();
110 }
111
112 void Renderer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
113 {
114   RENDERER_IMPL.GetDefaultPropertyIndices( indices );
115 }
116
117 const char* Renderer::GetDefaultPropertyName(Property::Index index) const
118 {
119   return RENDERER_IMPL.GetDefaultPropertyName( index );
120 }
121
122 Property::Index Renderer::GetDefaultPropertyIndex( const std::string& name ) const
123 {
124   return RENDERER_IMPL.GetDefaultPropertyIndex( name );
125 }
126
127 bool Renderer::IsDefaultPropertyWritable( Property::Index index ) const
128 {
129   return RENDERER_IMPL.IsDefaultPropertyWritable( index );
130 }
131
132 bool Renderer::IsDefaultPropertyAnimatable( Property::Index index ) const
133 {
134   return RENDERER_IMPL.IsDefaultPropertyAnimatable( index );
135 }
136
137 bool Renderer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
138 {
139   return RENDERER_IMPL.IsDefaultPropertyAConstraintInput( index );
140 }
141
142 Property::Type Renderer::GetDefaultPropertyType( Property::Index index ) const
143 {
144   return RENDERER_IMPL.GetDefaultPropertyType( index );
145 }
146
147 void Renderer::SetDefaultProperty( Property::Index index,
148                                    const Property::Value& propertyValue )
149 {
150   switch( index )
151   {
152     case Dali::Renderer::Property::DEPTH_INDEX:
153     {
154       SetDepthIndex( propertyValue.Get<int>() );
155     }
156     break;
157   }
158 }
159
160 void Renderer::SetSceneGraphProperty( Property::Index index,
161                                       const PropertyMetadata& entry,
162                                       const Property::Value& value )
163 {
164   RENDERER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
165   OnPropertySet(index, value);
166 }
167
168 Property::Value Renderer::GetDefaultProperty( Property::Index index ) const
169 {
170   Property::Value value;
171   switch( index )
172   {
173     case Dali::Renderer::Property::DEPTH_INDEX:
174     {
175       value = GetDepthIndex();
176     }
177     break;
178   }
179   return value;
180 }
181
182 const SceneGraph::PropertyOwner* Renderer::GetPropertyOwner() const
183 {
184   return mSceneObject;
185 }
186
187 const SceneGraph::PropertyOwner* Renderer::GetSceneObject() const
188 {
189   return mSceneObject;
190 }
191
192 const SceneGraph::PropertyBase* Renderer::GetSceneObjectAnimatableProperty( Property::Index index ) const
193 {
194   DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
195   const SceneGraph::PropertyBase* property = NULL;
196
197   if( OnStage() )
198   {
199     property = RENDERER_IMPL.GetRegisteredSceneGraphProperty(
200       this,
201       &Renderer::FindAnimatableProperty,
202       &Renderer::FindCustomProperty,
203       index );
204   }
205
206   return property;
207 }
208
209 const PropertyInputImpl* Renderer::GetSceneObjectInputProperty( Property::Index index ) const
210 {
211   const PropertyInputImpl* property = NULL;
212
213   if( OnStage() )
214   {
215     const SceneGraph::PropertyBase* baseProperty =
216       RENDERER_IMPL.GetRegisteredSceneGraphProperty( this,
217                                                      &Renderer::FindAnimatableProperty,
218                                                      &Renderer::FindCustomProperty,
219                                                      index );
220     property = static_cast<const PropertyInputImpl*>( baseProperty );
221   }
222
223   return property;
224 }
225
226 int Renderer::GetPropertyComponentIndex( Property::Index index ) const
227 {
228   return Property::INVALID_COMPONENT_INDEX;
229 }
230
231 bool Renderer::OnStage() const
232 {
233   return mOnStage;
234 }
235
236 void Renderer::Connect()
237 {
238   // @todo: MESH_REWORK : check this
239   mGeometryConnector.OnStageConnect();
240   mMaterialConnector.OnStageConnect();
241   mOnStage = true;
242 }
243
244 void Renderer::Disconnect()
245 {
246   // @todo: MESH_REWORK : check this
247   mGeometryConnector.OnStageDisconnect();
248   mMaterialConnector.OnStageDisconnect();
249   mOnStage = false;
250 }
251
252 Renderer::Renderer()
253 : mSceneObject(NULL),
254   mDepthIndex(0),
255   mOnStage(false)
256 {
257 }
258
259 void Renderer::Initialize()
260 {
261   EventThreadServices& eventThreadServices = GetEventThreadServices();
262   SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
263
264   // Transfer object ownership of scene-object to message
265   mSceneObject = SceneGraph::RendererAttachment::New();
266
267   // Send message to update to connect to scene graph:
268   AttachToSceneGraphMessage( updateManager, mSceneObject );
269
270   eventThreadServices.RegisterObject( this );
271 }
272
273 Renderer::~Renderer()
274 {
275   if( EventThreadServices::IsCoreRunning() )
276   {
277     EventThreadServices& eventThreadServices = GetEventThreadServices();
278     SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
279     RemoveObjectMessage( updateManager, mSceneObject );
280
281     eventThreadServices.UnregisterObject( this );
282   }
283 }
284
285 } // namespace Internal
286 } // namespace Dali