01104d331abbf530d27a1128f0538bf49bd05014
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / texture-set-impl.cpp
1 /*
2  * Copyright (c) 2016 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/texture-set-impl.h> // Dali::Internal::TextureSet
20
21 //EXTERNAL INCLUDES
22 #include <string>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/object/type-registry.h>
26 #include <dali/devel-api/rendering/texture-set.h> // Dali::Internal::Render::TextureSet
27 #include <dali/internal/event/common/object-impl-helper.h> // Dali::Internal::ObjectHelper
28 #include <dali/internal/event/common/property-helper.h> // DALI_PROPERTY_TABLE_BEGIN, DALI_PROPERTY, DALI_PROPERTY_TABLE_END
29 #include <dali/internal/update/manager/update-manager.h>
30 #include <dali/internal/update/rendering/scene-graph-texture-set.h>
31
32 namespace Dali
33 {
34 namespace Internal
35 {
36
37 namespace
38 {
39
40 const ObjectImplHelper<0> TEXTURE_SET_IMPL = { NULL };
41
42 BaseHandle Create()
43 {
44   return Dali::BaseHandle();
45 }
46
47 TypeRegistration mType( typeid( Dali::TextureSet ), typeid( Dali::Handle ), Create );
48
49 } // unnamed namespace
50
51 TextureSetPtr TextureSet::New()
52 {
53   TextureSetPtr textureSet( new TextureSet() );
54   textureSet->Initialize();
55   return textureSet;
56 }
57
58 void TextureSet::SetImage( size_t index, ImagePtr image )
59 {
60   size_t textureCount( mTextures.size() );
61   if( index < textureCount )
62   {
63     if( mTextures[index].image && mOnStage )
64     {
65       mTextures[index].image->Disconnect();
66     }
67   }
68   else
69   {
70     mTextures.resize(index + 1);
71     for( size_t i(textureCount); i<=index; ++i )
72     {
73       mTextures[i].image = NULL;
74       mTextures[i].sampler = NULL;
75     }
76   }
77   mTextures[index].image = image;
78
79
80   if( image )
81   {
82     if( mOnStage )
83     {
84       image->Connect();
85     }
86     SceneGraph::SetImageMessage( GetEventThreadServices(), *mSceneObject, index, image->GetResourceId() );
87   }
88   else
89   {
90     SceneGraph::SetImageMessage( GetEventThreadServices(), *mSceneObject, index, Integration::InvalidResourceId );
91   }
92 }
93
94 Image* TextureSet::GetImage( size_t index ) const
95 {
96   Image* result(0);
97   if( index < mTextures.size() )
98   {
99     result = mTextures[index].image.Get();
100   }
101   else
102   {
103     DALI_LOG_ERROR( "Error: Invalid index to TextureSet::GetImage");
104   }
105
106   return result;
107 }
108
109 void TextureSet::SetSampler( size_t index, SamplerPtr sampler )
110 {
111   size_t textureCount( mTextures.size() );
112   if( textureCount < index + 1  )
113   {
114     mTextures.resize(index + 1);
115     for( size_t i(textureCount); i<=index; ++i )
116     {
117       mTextures[i].image = NULL;
118       mTextures[i].sampler = NULL;
119     }
120   }
121
122   mTextures[index].sampler = sampler;
123
124   Render::Sampler* renderSampler(0);
125   if( sampler )
126   {
127     renderSampler = sampler->GetSamplerRenderObject();
128   }
129
130   SceneGraph::SetSamplerMessage( GetEventThreadServices(), *mSceneObject, index, renderSampler );
131 }
132
133 Sampler* TextureSet::GetSampler( size_t index ) const
134 {
135   Sampler* result(0);
136   if( index < mTextures.size() )
137   {
138     result = mTextures[index].sampler.Get();
139   }
140   else
141   {
142     DALI_LOG_ERROR( "Error: Invalid index to TextureSet::GetSampler");
143   }
144
145   return result;
146 }
147
148 size_t TextureSet::GetTextureCount() const
149 {
150   return mTextures.size();
151 }
152
153 const SceneGraph::TextureSet* TextureSet::GetTextureSetSceneObject() const
154 {
155   return mSceneObject;
156 }
157
158 unsigned int TextureSet::GetDefaultPropertyCount() const
159 {
160   return TEXTURE_SET_IMPL.GetDefaultPropertyCount();
161 }
162
163 void TextureSet::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
164 {
165   TEXTURE_SET_IMPL.GetDefaultPropertyIndices( indices );
166 }
167
168 const char* TextureSet::GetDefaultPropertyName(Property::Index index) const
169 {
170   return TEXTURE_SET_IMPL.GetDefaultPropertyName( index );
171 }
172
173 Property::Index TextureSet::GetDefaultPropertyIndex( const std::string& name ) const
174 {
175   return TEXTURE_SET_IMPL.GetDefaultPropertyIndex( name );
176 }
177
178 bool TextureSet::IsDefaultPropertyWritable( Property::Index index ) const
179 {
180   return TEXTURE_SET_IMPL.IsDefaultPropertyWritable( index );
181 }
182
183 bool TextureSet::IsDefaultPropertyAnimatable( Property::Index index ) const
184 {
185   return TEXTURE_SET_IMPL.IsDefaultPropertyAnimatable( index );
186 }
187
188 bool TextureSet::IsDefaultPropertyAConstraintInput( Property::Index index ) const
189 {
190   return TEXTURE_SET_IMPL.IsDefaultPropertyAConstraintInput( index );
191 }
192
193 Property::Type TextureSet::GetDefaultPropertyType( Property::Index index ) const
194 {
195   return TEXTURE_SET_IMPL.GetDefaultPropertyType( index );
196 }
197
198 void TextureSet::SetDefaultProperty( Property::Index index,
199                                    const Property::Value& propertyValue )
200 {
201 }
202
203 void TextureSet::SetSceneGraphProperty( Property::Index index,
204                                       const PropertyMetadata& entry,
205                                       const Property::Value& value )
206 {
207   TEXTURE_SET_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
208   OnPropertySet(index, value);
209 }
210
211 Property::Value TextureSet::GetDefaultProperty( Property::Index index ) const
212 {
213   Property::Value value;
214   return value;
215 }
216
217 const SceneGraph::PropertyOwner* TextureSet::GetPropertyOwner() const
218 {
219   return mSceneObject;
220 }
221
222 const SceneGraph::PropertyOwner* TextureSet::GetSceneObject() const
223 {
224   return mSceneObject;
225 }
226
227 const SceneGraph::PropertyBase* TextureSet::GetSceneObjectAnimatableProperty( Property::Index index ) const
228 {
229   PropertyMetadata* property = index >= PROPERTY_CUSTOM_START_INDEX ? static_cast<PropertyMetadata*>(FindCustomProperty( index )) : static_cast<PropertyMetadata*>(FindAnimatableProperty( index ));
230   DALI_ASSERT_ALWAYS( property && "Property index is invalid" );
231   return property->GetSceneGraphProperty();
232 }
233
234 const PropertyInputImpl* TextureSet::GetSceneObjectInputProperty( Property::Index index ) const
235 {
236   return GetSceneObjectAnimatableProperty( index );
237 }
238
239 int TextureSet::GetPropertyComponentIndex( Property::Index index ) const
240 {
241   return Property::INVALID_COMPONENT_INDEX;
242 }
243
244 bool TextureSet::OnStage() const
245 {
246   return mOnStage;
247 }
248
249 void TextureSet::Connect()
250 {
251   mOnStage = true;
252
253   for( size_t i(0); i<mTextures.size(); ++i )
254   {
255     if( mTextures[i].image )
256     {
257       mTextures[i].image->Connect();
258       SceneGraph::SetImageMessage( GetEventThreadServices(), *mSceneObject, i, mTextures[i].image->GetResourceId() );
259     }
260     else
261     {
262       SceneGraph::SetImageMessage( GetEventThreadServices(), *mSceneObject, i, Integration::InvalidResourceId );
263     }
264   }
265 }
266
267 void TextureSet::Disconnect()
268 {
269   for( size_t i(0); i<mTextures.size(); ++i )
270   {
271     if( mTextures[i].image )
272     {
273       mTextures[i].image->Disconnect();
274     }
275   }
276
277   mOnStage = false;
278 }
279
280 TextureSet::TextureSet()
281 : mSceneObject( NULL ),
282   mTextures(),
283   mOnStage( false )
284 {
285 }
286
287 void TextureSet::Initialize()
288 {
289   EventThreadServices& eventThreadServices = GetEventThreadServices();
290   SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
291
292   mSceneObject = SceneGraph::TextureSet::New();
293   AddMessage( updateManager, updateManager.GetTexturesOwner(), *mSceneObject );
294
295   eventThreadServices.RegisterObject( this );
296 }
297
298 TextureSet::~TextureSet()
299 {
300   if( EventThreadServices::IsCoreRunning() )
301   {
302     EventThreadServices& eventThreadServices = GetEventThreadServices();
303     SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
304     RemoveMessage( updateManager, updateManager.GetTexturesOwner(), *mSceneObject );
305
306     eventThreadServices.UnregisterObject( this );
307   }
308 }
309
310 } // namespace Internal
311 } // namespace Dali