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