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