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