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