Uniform map handling
[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 void Renderer::SetMaterial( Material& material )
64 {
65   mMaterialConnector.Set( material, OnStage() );
66   const SceneGraph::Material* materialSceneObject = material.GetMaterialSceneObject();
67   SetMaterialMessage( GetEventThreadServices(), *mSceneObject, *materialSceneObject );
68 }
69
70 void Renderer::SetDepthIndex( int depthIndex )
71 {
72   SceneGraph::PropertyMessage<int>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mDepthIndex, &SceneGraph::AnimatableProperty<int>::Bake, depthIndex );
73 }
74
75 int Renderer::GetCurrentDepthIndex() const
76 {
77   int depthIndex = 0;
78   if( mSceneObject )
79   {
80     BufferIndex bufferIndex = GetEventThreadServices().GetEventBufferIndex();
81     depthIndex = mSceneObject->mDepthIndex[bufferIndex];
82   }
83   return depthIndex;
84 }
85
86 SceneGraph::RendererAttachment* Renderer::GetRendererSceneObject()
87 {
88   return mSceneObject;
89 }
90
91 unsigned int Renderer::GetDefaultPropertyCount() const
92 {
93   return RENDERER_IMPL.GetDefaultPropertyCount();
94 }
95
96 void Renderer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
97 {
98   RENDERER_IMPL.GetDefaultPropertyIndices( indices );
99 }
100
101 const char* Renderer::GetDefaultPropertyName(Property::Index index) const
102 {
103   return RENDERER_IMPL.GetDefaultPropertyName( index );
104 }
105
106 Property::Index Renderer::GetDefaultPropertyIndex( const std::string& name ) const
107 {
108   return RENDERER_IMPL.GetDefaultPropertyIndex( name );
109 }
110
111 bool Renderer::IsDefaultPropertyWritable( Property::Index index ) const
112 {
113   return RENDERER_IMPL.IsDefaultPropertyWritable( index );
114 }
115
116 bool Renderer::IsDefaultPropertyAnimatable( Property::Index index ) const
117 {
118   return RENDERER_IMPL.IsDefaultPropertyAnimatable( index );
119 }
120
121 bool Renderer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
122 {
123   return RENDERER_IMPL.IsDefaultPropertyAConstraintInput( index );
124 }
125
126 Property::Type Renderer::GetDefaultPropertyType( Property::Index index ) const
127 {
128   return RENDERER_IMPL.GetDefaultPropertyType( index );
129 }
130
131 void Renderer::SetDefaultProperty( Property::Index index,
132                                    const Property::Value& propertyValue )
133 {
134   switch( index )
135   {
136     case Dali::Renderer::Property::DEPTH_INDEX:
137     {
138       SetDepthIndex( propertyValue.Get<int>() );
139     }
140     break;
141   }
142 }
143
144 void Renderer::SetSceneGraphProperty( Property::Index index,
145                                       const PropertyMetadata& entry,
146                                       const Property::Value& value )
147 {
148   RENDERER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
149   OnPropertySet(index, value);
150 }
151
152 Property::Value Renderer::GetDefaultProperty( Property::Index index ) const
153 {
154   Property::Value value;
155   switch( index )
156   {
157     case Dali::Renderer::Property::DEPTH_INDEX:
158     {
159       value = GetCurrentDepthIndex();
160     }
161     break;
162   }
163   return value;
164 }
165
166 const SceneGraph::PropertyOwner* Renderer::GetPropertyOwner() const
167 {
168   return mSceneObject;
169 }
170
171 const SceneGraph::PropertyOwner* Renderer::GetSceneObject() const
172 {
173   return mSceneObject;
174 }
175
176 const SceneGraph::PropertyBase* Renderer::GetSceneObjectAnimatableProperty( Property::Index index ) const
177 {
178   DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
179   const SceneGraph::PropertyBase* property = NULL;
180
181   if( OnStage() )
182   {
183     property = RENDERER_IMPL.GetRegisteredSceneGraphProperty(
184       this,
185       &Renderer::FindAnimatableProperty,
186       &Renderer::FindCustomProperty,
187       index );
188
189     if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
190     {
191       switch(index)
192       {
193         case Dali::Renderer::Property::DEPTH_INDEX:
194         {
195           property = &mSceneObject->mDepthIndex;
196         }
197         break;
198       }
199     }
200   }
201
202   return property;
203 }
204
205 const PropertyInputImpl* Renderer::GetSceneObjectInputProperty( Property::Index index ) const
206 {
207   const PropertyInputImpl* property = NULL;
208
209   if( OnStage() )
210   {
211     const SceneGraph::PropertyBase* baseProperty =
212       RENDERER_IMPL.GetRegisteredSceneGraphProperty( this,
213                                                      &Renderer::FindAnimatableProperty,
214                                                      &Renderer::FindCustomProperty,
215                                                      index );
216     property = static_cast<const PropertyInputImpl*>( baseProperty );
217
218     if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
219     {
220       switch(index)
221       {
222         case Dali::Renderer::Property::DEPTH_INDEX:
223         {
224           property = &mSceneObject->mDepthIndex;
225         }
226         break;
227       }
228     }
229   }
230
231   return property;
232 }
233
234 int Renderer::GetPropertyComponentIndex( Property::Index index ) const
235 {
236   return Property::INVALID_COMPONENT_INDEX;
237 }
238
239 bool Renderer::OnStage() const
240 {
241   return mOnStage;
242 }
243
244 void Renderer::Connect()
245 {
246   // @todo: MESH_REWORK : check this
247   mGeometryConnector.OnStageConnect();
248   mMaterialConnector.OnStageConnect();
249   mOnStage = true;
250 }
251
252 void Renderer::Disconnect()
253 {
254   // @todo: MESH_REWORK : check this
255   mGeometryConnector.OnStageDisconnect();
256   mMaterialConnector.OnStageDisconnect();
257   mOnStage = false;
258 }
259
260 Renderer::Renderer()
261 : mSceneObject(NULL),
262   mOnStage(false)
263 {
264 }
265
266 void Renderer::Initialize()
267 {
268   DALI_ASSERT_ALWAYS( EventThreadServices::IsCoreRunning() && "Core is not running" );
269   EventThreadServices& eventThreadServices = GetEventThreadServices();
270   SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
271
272   // Transfer object ownership of scene-object to message
273   mSceneObject = SceneGraph::RendererAttachment::New();
274
275   // Send message to update to connect to scene graph:
276   AttachToSceneGraphMessage( updateManager, mSceneObject );
277 }
278
279 Renderer::~Renderer()
280 {
281 }
282
283 } // namespace Internal
284 } // namespace Dali