Added GetUniformName to Sampler.
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / sampler-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/sampler-impl.h> // Dali::Internal::Sampler
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/devel-api/rendering/sampler.h> // Dali::Internal::Sampler
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/rendering/scene-graph-sampler.h> // Dali::Internal::SceneGraph::Sampler
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( "minification-filter",   INTEGER,   true, false,  true, Dali::Sampler::Property::MINIFICATION_FILTER )
42 DALI_PROPERTY( "magnification-filter",  INTEGER,   true, false,  true, Dali::Sampler::Property::MAGNIGICATION_FILTER )
43 DALI_PROPERTY( "u-wrap",                INTEGER,   true, false,  true, Dali::Sampler::Property::U_WRAP )
44 DALI_PROPERTY( "v-wrap",                INTEGER,   true, false,  true, Dali::Sampler::Property::V_WRAP )
45 DALI_PROPERTY( "affects-transparency",  BOOLEAN,  true, false,  true, Dali::Sampler::Property::AFFECTS_TRANSPARENCY )
46 DALI_PROPERTY_TABLE_END( DEFAULT_ACTOR_PROPERTY_START_INDEX )
47
48 const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> SAMPLER_IMPL = { DEFAULT_PROPERTY_DETAILS };
49
50 BaseHandle Create()
51 {
52   return Dali::BaseHandle();
53 }
54
55 TypeRegistration mType( typeid( Dali::Sampler ), typeid( Dali::Handle ), Create );
56
57 } // unnamed namespace
58
59 SamplerPtr Sampler::New( const std::string& textureUnitUniformName )
60 {
61   SamplerPtr sampler( new Sampler() );
62   sampler->Initialize( textureUnitUniformName );
63   return sampler;
64 }
65
66 void Sampler::SetTextureUnitUniformName( const std::string& name )
67 {
68   SetTextureUnitUniformNameMessage( GetEventThreadServices(), *mSceneObject, name);
69 }
70
71 const std::string& Sampler::GetTextureUnitUniformName() const
72 {
73   return mSceneObject->GetTextureUnitUniformName();
74 }
75
76 void Sampler::SetImage( ImagePtr& image )
77 {
78   // Keep a reference to the image object
79   mImageConnector.Set( image, OnStage() );
80
81   // sceneObject is being used in a separate thread; queue a message to set
82   if( mOnStage )
83   {
84     unsigned int resourceId = image->GetResourceId();
85     if( resourceId != 0 )
86     {
87       SetTextureMessage( GetEventThreadServices(), *mSceneObject, resourceId );
88     }
89   }
90 }
91
92 ImagePtr Sampler::GetImage() const
93 {
94   return mImageConnector.Get();
95 }
96
97 void Sampler::SetFilterMode( Dali::Sampler::FilterMode minFilter, Dali::Sampler::FilterMode magFilter )
98 {
99   if( NULL != mSceneObject )
100   {
101     SetFilterModeMessage( GetEventThreadServices(), *mSceneObject, minFilter, magFilter );
102   }
103 }
104
105 void Sampler::SetWrapMode( Dali::Sampler::WrapMode uWrap, Dali::Sampler::WrapMode vWrap )
106 {
107   if( NULL != mSceneObject )
108   {
109     SetWrapModeMessage( GetEventThreadServices(), *mSceneObject, uWrap, vWrap );
110   }
111 }
112
113 void Sampler::SetAffectsTransparency( bool affectsTransparency )
114 {
115   if( NULL != mSceneObject )
116   {
117     SceneGraph::DoubleBufferedPropertyMessage<bool>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mAffectsTransparency, &SceneGraph::DoubleBufferedProperty<bool>::Set, affectsTransparency );
118   }
119 }
120
121 const SceneGraph::Sampler* Sampler::GetSamplerSceneObject() const
122 {
123   return mSceneObject;
124 }
125
126 unsigned int Sampler::GetDefaultPropertyCount() const
127 {
128   return SAMPLER_IMPL.GetDefaultPropertyCount();
129 }
130
131 void Sampler::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
132 {
133   SAMPLER_IMPL.GetDefaultPropertyIndices( indices );
134 }
135
136 const char* Sampler::GetDefaultPropertyName(Property::Index index) const
137 {
138   return SAMPLER_IMPL.GetDefaultPropertyName( index );
139 }
140
141 Property::Index Sampler::GetDefaultPropertyIndex( const std::string& name ) const
142 {
143   return SAMPLER_IMPL.GetDefaultPropertyIndex( name );
144 }
145
146 bool Sampler::IsDefaultPropertyWritable( Property::Index index ) const
147 {
148   return SAMPLER_IMPL.IsDefaultPropertyWritable( index );
149 }
150
151 bool Sampler::IsDefaultPropertyAnimatable( Property::Index index ) const
152 {
153   return SAMPLER_IMPL.IsDefaultPropertyAnimatable( index );
154 }
155
156 bool Sampler::IsDefaultPropertyAConstraintInput( Property::Index index ) const
157 {
158   return SAMPLER_IMPL.IsDefaultPropertyAConstraintInput( index );
159 }
160
161 Property::Type Sampler::GetDefaultPropertyType( Property::Index index ) const
162 {
163   return SAMPLER_IMPL.GetDefaultPropertyType( index );
164 }
165
166 void Sampler::SetDefaultProperty( Property::Index index,
167                                   const Property::Value& propertyValue )
168 {
169   switch( index )
170   {
171     case Dali::Sampler::Property::MINIFICATION_FILTER:
172     {
173       SceneGraph::DoubleBufferedPropertyMessage<int>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mMinFilter, &SceneGraph::DoubleBufferedProperty<int>::Set, propertyValue.Get<int>() );
174       break;
175     }
176     case Dali::Sampler::Property::MAGNIGICATION_FILTER:
177     {
178       SceneGraph::DoubleBufferedPropertyMessage<int>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mMagFilter, &SceneGraph::DoubleBufferedProperty<int>::Set, propertyValue.Get<int>() );
179       break;
180     }
181     case Dali::Sampler::Property::U_WRAP:
182     {
183       SceneGraph::DoubleBufferedPropertyMessage<int>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mUWrapMode, &SceneGraph::DoubleBufferedProperty<int>::Set, propertyValue.Get<int>() );
184       break;
185     }
186     case Dali::Sampler::Property::V_WRAP:
187     {
188       SceneGraph::DoubleBufferedPropertyMessage<int>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mVWrapMode, &SceneGraph::DoubleBufferedProperty<int>::Set, propertyValue.Get<int>() );
189       break;
190     }
191     case Dali::Sampler::Property::AFFECTS_TRANSPARENCY:
192     {
193       SceneGraph::DoubleBufferedPropertyMessage<bool>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mAffectsTransparency, &SceneGraph::DoubleBufferedProperty<bool>::Set, propertyValue.Get<bool>() );
194       break;
195     }
196   }
197 }
198
199 void Sampler::SetSceneGraphProperty( Property::Index index,
200                                      const PropertyMetadata& entry,
201                                      const Property::Value& value )
202 {
203   SAMPLER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
204   OnPropertySet(index, value);
205 }
206
207 Property::Value Sampler::GetDefaultProperty( Property::Index index ) const
208 {
209   BufferIndex bufferIndex = GetEventThreadServices().GetEventBufferIndex();
210   Property::Value value;
211
212   switch( index )
213   {
214     case Dali::Sampler::Property::MINIFICATION_FILTER:
215     {
216       value = mSceneObject->mMinFilter[bufferIndex];
217       break;
218     }
219     case Dali::Sampler::Property::MAGNIGICATION_FILTER:
220     {
221       value = mSceneObject->mMagFilter[bufferIndex];
222       break;
223     }
224     case Dali::Sampler::Property::U_WRAP:
225     {
226       value = mSceneObject->mUWrapMode[bufferIndex];
227       break;
228     }
229     case Dali::Sampler::Property::V_WRAP:
230     {
231       value = mSceneObject->mVWrapMode[bufferIndex];
232       break;
233     }
234     case Dali::Sampler::Property::AFFECTS_TRANSPARENCY:
235     {
236       value = mSceneObject->mAffectsTransparency[bufferIndex];
237       break;
238     }
239   }
240   return value;
241 }
242
243 const SceneGraph::PropertyOwner* Sampler::GetPropertyOwner() const
244 {
245   return mSceneObject;
246 }
247
248 const SceneGraph::PropertyOwner* Sampler::GetSceneObject() const
249 {
250   return mSceneObject;
251 }
252
253 const SceneGraph::PropertyBase* Sampler::GetSceneObjectAnimatableProperty( Property::Index index ) const
254 {
255   DALI_ASSERT_ALWAYS( IsPropertyAnimatable( index ) && "Property is not animatable" );
256
257   const SceneGraph::PropertyBase* property = NULL;
258
259   if( OnStage() )
260   {
261     property = SAMPLER_IMPL.GetRegisteredSceneGraphProperty( this,
262                                                              &Sampler::FindAnimatableProperty,
263                                                              &Sampler::FindCustomProperty,
264                                                              index );
265
266     if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
267     {
268       // No animatable default props
269       DALI_ASSERT_ALWAYS( 0 && "Property is not animatable" );
270     }
271   }
272
273   return property;
274 }
275
276 const PropertyInputImpl* Sampler::GetSceneObjectInputProperty( Property::Index index ) const
277 {
278   const PropertyInputImpl* property = NULL;
279
280   if( OnStage() )
281   {
282     const SceneGraph::PropertyBase* baseProperty =
283       SAMPLER_IMPL.GetRegisteredSceneGraphProperty( this,
284                                                     &Sampler::FindAnimatableProperty,
285                                                     &Sampler::FindCustomProperty,
286                                                     index );
287     property = static_cast<const PropertyInputImpl*>( baseProperty );
288
289     if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
290     {
291       switch( index )
292       {
293         case Dali::Sampler::Property::MINIFICATION_FILTER:
294         {
295           property = &mSceneObject->mMinFilter;
296           break;
297         }
298         case Dali::Sampler::Property::MAGNIGICATION_FILTER:
299         {
300           property = &mSceneObject->mMagFilter;
301           break;
302         }
303         case Dali::Sampler::Property::U_WRAP:
304         {
305           property = &mSceneObject->mUWrapMode;
306           break;
307         }
308         case Dali::Sampler::Property::V_WRAP:
309         {
310           property = &mSceneObject->mVWrapMode;
311           break;
312         }
313         case Dali::Sampler::Property::AFFECTS_TRANSPARENCY:
314         {
315           property = &mSceneObject->mAffectsTransparency;
316           break;
317         }
318       }
319     }
320   }
321
322   return property;
323 }
324
325 int Sampler::GetPropertyComponentIndex( Property::Index index ) const
326 {
327   return Property::INVALID_COMPONENT_INDEX;
328 }
329
330 bool Sampler::OnStage() const
331 {
332   return mOnStage;
333 }
334
335 void Sampler::Connect()
336 {
337   mOnStage = true;
338
339   mImageConnector.OnStageConnect();
340
341   // sceneObject is being used in a separate thread; queue a message to set
342   unsigned int resourceId = mImageConnector.Get()->GetResourceId();
343   SetTextureMessage( GetEventThreadServices(), *mSceneObject, resourceId );
344 }
345
346 void Sampler::Disconnect()
347 {
348   mOnStage = false;
349
350   mImageConnector.OnStageDisconnect();
351 }
352
353 Sampler::Sampler()
354 : mSceneObject( NULL ),
355   mOnStage( false )
356 {
357 }
358
359 void Sampler::Initialize( const std::string& textureUnitUniformName )
360 {
361   EventThreadServices& eventThreadServices = GetEventThreadServices();
362   SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
363
364   mSceneObject = new SceneGraph::Sampler( textureUnitUniformName );
365   AddMessage( updateManager, updateManager.GetSamplerOwner(), *mSceneObject );
366
367   eventThreadServices.RegisterObject( this );
368 }
369
370 Sampler::~Sampler()
371 {
372   if( EventThreadServices::IsCoreRunning() )
373   {
374     EventThreadServices& eventThreadServices = GetEventThreadServices();
375     SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
376     RemoveMessage( updateManager, updateManager.GetSamplerOwner(), *mSceneObject );
377
378     eventThreadServices.UnregisterObject( this );
379   }
380 }
381
382 } // namespace Internal
383 } // namespace Dali