0c10ea24c83d75627384c09ccf483d5bf938ce93
[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 // INTERNAL INCLUDES
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/internal/event/common/object-impl-helper.h> // Dali::Internal::ObjectHelper
24 #include <dali/internal/update/manager/update-manager.h>
25 #include <dali/internal/update/rendering/scene-graph-texture-set.h>
26
27 namespace Dali
28 {
29 namespace Internal
30 {
31
32 TextureSetPtr TextureSet::New()
33 {
34   TextureSetPtr textureSet( new TextureSet() );
35   textureSet->Initialize();
36   return textureSet;
37 }
38
39 void TextureSet::SetImage( size_t index, ImagePtr image )
40 {
41   if( !mNewTextures.empty() )
42   {
43     DALI_LOG_ERROR( "Error: Cannot mix images and textures in the same TextureSet");
44     return;
45   }
46
47   size_t textureCount( mImages.size() );
48   if( index < textureCount )
49   {
50     if( mImages[index] && mOnStage )
51     {
52       mImages[index]->Disconnect();
53     }
54   }
55   else
56   {
57     mImages.resize(index + 1);
58     mSamplers.resize(index + 1);
59     for( size_t i(textureCount); i<=index; ++i )
60     {
61       mImages[i] = NULL;
62       mSamplers[i] = NULL;
63     }
64   }
65   mImages[index] = image;
66
67
68   if( image )
69   {
70     if( mOnStage )
71     {
72       image->Connect();
73     }
74     SceneGraph::SetImageMessage( mEventThreadServices, *mSceneObject, index, image->GetResourceId() );
75   }
76   else
77   {
78     SceneGraph::SetImageMessage( mEventThreadServices, *mSceneObject, index, Integration::InvalidResourceId );
79   }
80 }
81
82 void TextureSet::SetTexture( size_t index, NewTexturePtr texture )
83 {
84   if( !mImages.empty() )
85   {
86     DALI_LOG_ERROR( "Error: Cannot mix images and textures in the same texture set");
87     return;
88   }
89
90   size_t textureCount( mNewTextures.size() );
91   if( index >= textureCount )
92   {
93     mNewTextures.resize(index + 1);
94     mSamplers.resize(index + 1);
95     for( size_t i(textureCount); i<=index; ++i )
96     {
97       mNewTextures[i] = NULL;
98       mSamplers[i] = NULL;
99     }
100   }
101
102   mNewTextures[index]= texture;
103
104   Render::NewTexture* renderTexture(0);
105   if( texture )
106   {
107     renderTexture = texture->GetRenderObject();
108   }
109
110   SceneGraph::SetTextureMessage( mEventThreadServices, *mSceneObject, index, renderTexture );
111 }
112
113 Image* TextureSet::GetImage( size_t index ) const
114 {
115   Image* result(0);
116   if( index < mImages.size() )
117   {
118     result = mImages[index].Get();
119   }
120   else
121   {
122     DALI_LOG_ERROR( "Error: Invalid index to TextureSet::GetImage");
123   }
124
125   return result;
126 }
127
128 NewTexture* TextureSet::GetTexture( size_t index ) const
129 {
130   NewTexture* result(0);
131   if( index < mNewTextures.size() )
132   {
133     result = mNewTextures[index].Get();
134   }
135   else
136   {
137     DALI_LOG_ERROR( "Error: Invalid index to TextureSet::GetTexture");
138   }
139
140   return result;
141 }
142
143 void TextureSet::SetSampler( size_t index, SamplerPtr sampler )
144 {
145   size_t samplerCount( mSamplers.size() );
146   if( samplerCount < index + 1  )
147   {
148     mSamplers.resize( index + 1 );
149     mNewTextures.resize( index + 1 );
150     mImages.resize( index + 1 );
151     for( size_t i(samplerCount); i<=index; ++i )
152     {
153       mImages[i] = NULL;
154       mSamplers[i] = NULL;
155       mNewTextures[i] = NULL;
156     }
157   }
158
159   mSamplers[index] = sampler;
160
161   Render::Sampler* renderSampler(0);
162   if( sampler )
163   {
164     renderSampler = sampler->GetSamplerRenderObject();
165   }
166
167   SceneGraph::SetSamplerMessage( mEventThreadServices, *mSceneObject, index, renderSampler );
168 }
169
170 Sampler* TextureSet::GetSampler( size_t index ) const
171 {
172   Sampler* result(0);
173   if( index < mSamplers.size() )
174   {
175     result = mSamplers[index].Get();
176   }
177   else
178   {
179     DALI_LOG_ERROR( "Error: Invalid index to TextureSet::GetSampler");
180   }
181
182   return result;
183 }
184
185 size_t TextureSet::GetTextureCount() const
186 {
187   return mImages.size();
188 }
189
190 const SceneGraph::TextureSet* TextureSet::GetTextureSetSceneObject() const
191 {
192   return mSceneObject;
193 }
194
195 bool TextureSet::OnStage() const
196 {
197   return mOnStage;
198 }
199
200 void TextureSet::Connect()
201 {
202   mOnStage = true;
203
204   for( size_t i(0); i<mImages.size(); ++i )
205   {
206     if( mImages[i] )
207     {
208       mImages[i]->Connect();
209       SceneGraph::SetImageMessage( mEventThreadServices, *mSceneObject, i, mImages[i]->GetResourceId() );
210     }
211     else
212     {
213       SceneGraph::SetImageMessage( mEventThreadServices, *mSceneObject, i, Integration::InvalidResourceId );
214     }
215   }
216 }
217
218 void TextureSet::Disconnect()
219 {
220   for( size_t i(0); i<mImages.size(); ++i )
221   {
222     if( mImages[i] )
223     {
224       mImages[i]->Disconnect();
225     }
226   }
227
228   mOnStage = false;
229 }
230
231 TextureSet::TextureSet()
232 :mEventThreadServices( *Stage::GetCurrent() ),
233  mSceneObject( NULL ),
234  mOnStage( false )
235 {
236 }
237
238 void TextureSet::Initialize()
239 {
240   SceneGraph::UpdateManager& updateManager = mEventThreadServices.GetUpdateManager();
241
242   mSceneObject = SceneGraph::TextureSet::New();
243   AddTextureSetMessage( updateManager, *mSceneObject );
244 }
245
246 TextureSet::~TextureSet()
247 {
248   if( EventThreadServices::IsCoreRunning() )
249   {
250     SceneGraph::UpdateManager& updateManager = mEventThreadServices.GetUpdateManager();
251     RemoveTextureSetMessage( updateManager, *mSceneObject );
252   }
253 }
254
255 } // namespace Internal
256 } // namespace Dali