8912c17fd0df44c14fd0b0b58963bc9f6d5d890f
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / texture-manager-impl.cpp
1  /*
2  * Copyright (c) 2020 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-toolkit/internal/visuals/texture-manager-impl.h>
20
21 // EXTERNAL HEADERS
22 #include <cstdlib>
23 #include <string>
24 #include <dali/public-api/math/vector4.h>
25 #include <dali/devel-api/adaptor-framework/environment-variable.h>
26 #include <dali/devel-api/adaptor-framework/image-loading.h>
27 #include <dali/devel-api/common/hash.h>
28 #include <dali/devel-api/images/texture-set-image.h>
29 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
30 #include <dali/integration-api/debug.h>
31
32 // INTERNAL HEADERS
33 #include <dali-toolkit/internal/image-loader/image-atlas-impl.h>
34 #include <dali-toolkit/public-api/image-loader/sync-image-loader.h>
35 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
36
37 namespace
38 {
39
40 constexpr auto DEFAULT_NUMBER_OF_LOCAL_LOADER_THREADS = size_t{4u};
41 constexpr auto DEFAULT_NUMBER_OF_REMOTE_LOADER_THREADS = size_t{8u};
42
43 constexpr auto NUMBER_OF_LOCAL_LOADER_THREADS_ENV = "DALI_TEXTURE_LOCAL_THREADS";
44 constexpr auto NUMBER_OF_REMOTE_LOADER_THREADS_ENV = "DALI_TEXTURE_REMOTE_THREADS";
45
46 size_t GetNumberOfThreads(const char* environmentVariable, size_t defaultValue)
47 {
48   using Dali::EnvironmentVariable::GetEnvironmentVariable;
49   auto numberString = GetEnvironmentVariable(environmentVariable);
50   auto numberOfThreads = numberString ? std::strtoul(numberString, nullptr, 10) : 0;
51   constexpr auto MAX_NUMBER_OF_THREADS = 100u;
52   DALI_ASSERT_DEBUG( numberOfThreads < MAX_NUMBER_OF_THREADS );
53   return ( numberOfThreads > 0 && numberOfThreads < MAX_NUMBER_OF_THREADS ) ? numberOfThreads : defaultValue;
54 }
55
56 size_t GetNumberOfLocalLoaderThreads()
57 {
58   return GetNumberOfThreads(NUMBER_OF_LOCAL_LOADER_THREADS_ENV, DEFAULT_NUMBER_OF_LOCAL_LOADER_THREADS);
59 }
60
61 size_t GetNumberOfRemoteLoaderThreads()
62 {
63   return GetNumberOfThreads(NUMBER_OF_REMOTE_LOADER_THREADS_ENV, DEFAULT_NUMBER_OF_REMOTE_LOADER_THREADS);
64 }
65
66 } // namespace
67
68 namespace Dali
69 {
70
71 namespace Toolkit
72 {
73
74 namespace Internal
75 {
76
77 namespace
78 {
79
80 #ifdef DEBUG_ENABLED
81 Debug::Filter* gTextureManagerLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_TEXTURE_MANAGER" );
82
83 #define GET_LOAD_STATE_STRING( loadState ) \
84   loadState == TextureManager::NOT_STARTED ? "NOT_STARTED" :             \
85     loadState == TextureManager::LOADING ? "LOADING" :                   \
86     loadState == TextureManager::LOAD_FINISHED ? "LOAD_FINISHED" :       \
87     loadState == TextureManager::WAITING_FOR_MASK ? "WAITING_FOR_MASK" : \
88     loadState == TextureManager::MASK_APPLYING ? "MASK_APPLYING" :         \
89     loadState == TextureManager::MASK_APPLIED ? "MASK_APPLIED" :         \
90     loadState == TextureManager::UPLOADED ? "UPLOADED" :                 \
91     loadState == TextureManager::CANCELLED ? "CANCELLED" :               \
92     loadState == TextureManager::LOAD_FAILED ? "LOAD_FAILED" : "Unknown"
93
94 #endif
95
96 const uint32_t      DEFAULT_ATLAS_SIZE( 1024u );                     ///< This size can fit 8 by 8 images of average size 128 * 128
97 const Vector4       FULL_ATLAS_RECT( 0.0f, 0.0f, 1.0f, 1.0f );       ///< UV Rectangle that covers the full Texture
98 const int           INVALID_INDEX( -1 );                             ///< Invalid index used to represent a non-existant TextureInfo struct
99 const int           INVALID_CACHE_INDEX( -1 ); ///< Invalid Cache index
100
101
102 void PreMultiply( Devel::PixelBuffer pixelBuffer, TextureManager::MultiplyOnLoad& preMultiplyOnLoad )
103 {
104   if( Pixel::HasAlpha( pixelBuffer.GetPixelFormat() ) )
105   {
106     if( preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD )
107     {
108       pixelBuffer.MultiplyColorByAlpha();
109     }
110   }
111   else
112   {
113     preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
114   }
115 }
116
117 } // Anonymous namespace
118
119 TextureManager::MaskingData::MaskingData()
120 : mAlphaMaskUrl(),
121   mAlphaMaskId( INVALID_TEXTURE_ID ),
122   mContentScaleFactor( 1.0f ),
123   mCropToMask( true )
124 {
125 }
126
127 TextureManager::TextureManager()
128 : mAsyncLocalLoaders( GetNumberOfLocalLoaderThreads(), [&]() { return AsyncLoadingHelper(*this); } ),
129   mAsyncRemoteLoaders( GetNumberOfRemoteLoaderThreads(), [&]() { return AsyncLoadingHelper(*this); } ),
130   mExternalTextures(),
131   mLifecycleObservers(),
132   mLoadQueue(),
133   mBrokenImageUrl(""),
134   mCurrentTextureId( 0 ),
135   mQueueLoadFlag(false)
136 {
137 }
138
139 TextureManager::~TextureManager()
140 {
141   for( auto iter = mLifecycleObservers.Begin(), endIter = mLifecycleObservers.End(); iter != endIter; ++iter)
142   {
143     (*iter)->TextureManagerDestroyed();
144   }
145 }
146
147 Devel::PixelBuffer TextureManager::LoadPixelBuffer(
148   const VisualUrl& url, Dali::ImageDimensions desiredSize, Dali::FittingMode::Type fittingMode, Dali::SamplingMode::Type samplingMode, bool synchronousLoading, TextureUploadObserver* textureObserver, bool orientationCorrection, TextureManager::MultiplyOnLoad& preMultiplyOnLoad )
149 {
150   Devel::PixelBuffer pixelBuffer;
151   if( synchronousLoading )
152   {
153     if( url.IsValid() )
154     {
155       pixelBuffer = LoadImageFromFile( url.GetUrl(), desiredSize, fittingMode, samplingMode,
156                                        orientationCorrection  );
157       if( pixelBuffer && preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD )
158       {
159         PreMultiply( pixelBuffer, preMultiplyOnLoad );
160       }
161     }
162   }
163   else
164   {
165     RequestLoadInternal( url, INVALID_TEXTURE_ID, 1.0f, desiredSize, fittingMode, samplingMode, TextureManager::NO_ATLAS,
166                          false, RETURN_PIXEL_BUFFER, textureObserver, orientationCorrection, TextureManager::ReloadPolicy::FORCED,
167                          preMultiplyOnLoad );
168   }
169
170   return pixelBuffer;
171 }
172
173 TextureSet TextureManager::LoadTexture(
174   const VisualUrl& url, Dali::ImageDimensions desiredSize, Dali::FittingMode::Type fittingMode,
175   Dali::SamplingMode::Type samplingMode, MaskingDataPointer& maskInfo,
176   bool synchronousLoading, TextureManager::TextureId& textureId, Vector4& textureRect,
177   Dali::ImageDimensions& textureRectSize, bool& atlasingStatus, bool& loadingStatus,
178   Dali::WrapMode::Type wrapModeU, Dali::WrapMode::Type wrapModeV, TextureUploadObserver* textureObserver,
179   AtlasUploadObserver* atlasObserver, ImageAtlasManagerPtr imageAtlasManager, bool orientationCorrection,
180   TextureManager::ReloadPolicy reloadPolicy, TextureManager::MultiplyOnLoad& preMultiplyOnLoad )
181 {
182   TextureSet textureSet;
183
184   loadingStatus = false;
185   textureRect = FULL_ATLAS_RECT;
186
187   if( VisualUrl::TEXTURE == url.GetProtocolType())
188   {
189     std::string location = url.GetLocation();
190     if( location.size() > 0u )
191     {
192       TextureId id = std::stoi( location );
193       for( auto&& elem : mExternalTextures )
194       {
195         if( elem.textureId == id )
196         {
197           preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
198           textureId = elem.textureId;
199           return elem.textureSet;
200         }
201       }
202     }
203   }
204   else if( synchronousLoading )
205   {
206     PixelData data;
207     if( url.IsValid() )
208     {
209       Devel::PixelBuffer pixelBuffer = LoadImageFromFile( url.GetUrl(), desiredSize, fittingMode, samplingMode,
210                                        orientationCorrection  );
211       if( maskInfo )
212       {
213         Devel::PixelBuffer maskPixelBuffer = LoadImageFromFile( maskInfo->mAlphaMaskUrl.GetUrl(), ImageDimensions(),
214                                              FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true  );
215         if( maskPixelBuffer )
216         {
217           pixelBuffer.ApplyMask( maskPixelBuffer, maskInfo->mContentScaleFactor, maskInfo->mCropToMask );
218         }
219       }
220       if( pixelBuffer )
221       {
222         PreMultiply( pixelBuffer, preMultiplyOnLoad );
223         data = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
224       }
225     }
226     if( !data )
227     {
228       // use broken image
229       Devel::PixelBuffer pixelBuffer = LoadImageFromFile( mBrokenImageUrl );
230       if( pixelBuffer )
231       {
232         PreMultiply( pixelBuffer, preMultiplyOnLoad );
233         data = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
234       }
235       Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, data.GetPixelFormat(),
236                                       data.GetWidth(), data.GetHeight() );
237       texture.Upload( data );
238       textureSet = TextureSet::New();
239       textureSet.SetTexture( 0u, texture );
240     }
241     else
242     {
243       if( atlasingStatus ) // attempt atlasing
244       {
245         textureSet = imageAtlasManager->Add( textureRect, data );
246       }
247       if( !textureSet ) // big image, no atlasing or atlasing failed
248       {
249         atlasingStatus = false;
250         Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, data.GetPixelFormat(),
251                                         data.GetWidth(), data.GetHeight() );
252         texture.Upload( data );
253         textureSet = TextureSet::New();
254         textureSet.SetTexture( 0u, texture );
255       }
256       else
257       {
258         textureRectSize.SetWidth(data.GetWidth());
259         textureRectSize.SetHeight(data.GetHeight());
260       }
261     }
262   }
263   else
264   {
265     loadingStatus = true;
266     if( atlasingStatus )
267     {
268       textureSet = imageAtlasManager->Add( textureRect, url.GetUrl(), desiredSize, fittingMode, true, atlasObserver);
269     }
270     if( !textureSet ) // big image, no atlasing or atlasing failed
271     {
272       atlasingStatus = false;
273       if( !maskInfo )
274       {
275         textureId = RequestLoad( url, desiredSize, fittingMode, samplingMode, TextureManager::NO_ATLAS,
276                                  textureObserver, orientationCorrection, reloadPolicy, preMultiplyOnLoad );
277       }
278       else
279       {
280         maskInfo->mAlphaMaskId = RequestMaskLoad( maskInfo->mAlphaMaskUrl );
281         textureId = RequestLoad( url,
282                                  maskInfo->mAlphaMaskId,
283                                  maskInfo->mContentScaleFactor,
284                                  desiredSize,
285                                  fittingMode, samplingMode,
286                                  TextureManager::NO_ATLAS,
287                                  maskInfo->mCropToMask,
288                                  textureObserver,
289                                  orientationCorrection,
290                                  reloadPolicy, preMultiplyOnLoad );
291       }
292
293       TextureManager::LoadState loadState = GetTextureStateInternal( textureId );
294       if( loadState == TextureManager::UPLOADED )
295       {
296         // UploadComplete has already been called - keep the same texture set
297         textureSet = GetTextureSet( textureId );
298       }
299
300       // If we are loading the texture, or waiting for the ready signal handler to complete, inform
301       // caller that they need to wait.
302       loadingStatus = ( loadState == TextureManager::LOADING ||
303                         loadState == TextureManager::WAITING_FOR_MASK ||
304                         loadState == TextureManager::MASK_APPLYING ||
305                         loadState == TextureManager::MASK_APPLIED ||
306                         loadState == TextureManager::NOT_STARTED ||
307                         mQueueLoadFlag );
308
309     }
310     else
311     {
312       textureRectSize = desiredSize;
313     }
314   }
315
316   if( ! atlasingStatus && textureSet )
317   {
318     Sampler sampler = Sampler::New();
319     sampler.SetWrapMode(  wrapModeU, wrapModeV  );
320     textureSet.SetSampler( 0u, sampler );
321   }
322
323   return textureSet;
324 }
325
326 TextureManager::TextureId TextureManager::RequestLoad(
327   const VisualUrl&                url,
328   const ImageDimensions           desiredSize,
329   FittingMode::Type               fittingMode,
330   Dali::SamplingMode::Type        samplingMode,
331   const UseAtlas                  useAtlas,
332   TextureUploadObserver*          observer,
333   bool                            orientationCorrection,
334   TextureManager::ReloadPolicy    reloadPolicy,
335   TextureManager::MultiplyOnLoad& preMultiplyOnLoad )
336 {
337   return RequestLoadInternal( url, INVALID_TEXTURE_ID, 1.0f, desiredSize, fittingMode, samplingMode, useAtlas,
338                               false, UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy,
339                               preMultiplyOnLoad );
340 }
341
342 TextureManager::TextureId TextureManager::RequestLoad(
343   const VisualUrl&                url,
344   TextureId                       maskTextureId,
345   float                           contentScale,
346   const ImageDimensions           desiredSize,
347   FittingMode::Type               fittingMode,
348   Dali::SamplingMode::Type        samplingMode,
349   const UseAtlas                  useAtlas,
350   bool                            cropToMask,
351   TextureUploadObserver*          observer,
352   bool                            orientationCorrection,
353   TextureManager::ReloadPolicy    reloadPolicy,
354   TextureManager::MultiplyOnLoad& preMultiplyOnLoad )
355 {
356   return RequestLoadInternal( url, maskTextureId, contentScale, desiredSize, fittingMode, samplingMode, useAtlas,
357                               cropToMask, UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy,
358                               preMultiplyOnLoad );
359 }
360
361 TextureManager::TextureId TextureManager::RequestMaskLoad( const VisualUrl& maskUrl )
362 {
363   // Use the normal load procedure to get the alpha mask.
364   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
365   return RequestLoadInternal( maskUrl, INVALID_TEXTURE_ID, 1.0f, ImageDimensions(), FittingMode::SCALE_TO_FILL,
366                               SamplingMode::NO_FILTER, NO_ATLAS, false, KEEP_PIXEL_BUFFER, NULL, true,
367                               TextureManager::ReloadPolicy::CACHED, preMultiply );
368 }
369
370 TextureManager::TextureId TextureManager::RequestLoadInternal(
371   const VisualUrl&                url,
372   TextureId                       maskTextureId,
373   float                           contentScale,
374   const ImageDimensions           desiredSize,
375   FittingMode::Type               fittingMode,
376   Dali::SamplingMode::Type        samplingMode,
377   UseAtlas                        useAtlas,
378   bool                            cropToMask,
379   StorageType                     storageType,
380   TextureUploadObserver*          observer,
381   bool                            orientationCorrection,
382   TextureManager::ReloadPolicy    reloadPolicy,
383   TextureManager::MultiplyOnLoad& preMultiplyOnLoad )
384 {
385   // First check if the requested Texture is cached.
386   const TextureHash textureHash = GenerateHash( url.GetUrl(), desiredSize, fittingMode, samplingMode, useAtlas,
387                                                 maskTextureId );
388
389   TextureManager::TextureId textureId = INVALID_TEXTURE_ID;
390   // Look up the texture by hash. Note: The extra parameters are used in case of a hash collision.
391   int cacheIndex = FindCachedTexture( textureHash, url.GetUrl(), desiredSize, fittingMode, samplingMode, useAtlas,
392                                       maskTextureId, preMultiplyOnLoad, storageType );
393
394   // Check if the requested Texture exists in the cache.
395   if( cacheIndex != INVALID_CACHE_INDEX )
396   {
397     if ( TextureManager::ReloadPolicy::CACHED == reloadPolicy )
398     {
399       // Mark this texture being used by another client resource. Forced reload would replace the current texture
400       // without the need for incrementing the reference count.
401       ++( mTextureInfoContainer[ cacheIndex ].referenceCount );
402     }
403     textureId = mTextureInfoContainer[ cacheIndex ].textureId;
404
405     // Update preMultiplyOnLoad value. It should be changed according to preMultiplied value of the cached info.
406     preMultiplyOnLoad = mTextureInfoContainer[ cacheIndex ].preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
407
408     DALI_LOG_INFO( gTextureManagerLogFilter, Debug::General, "TextureManager::RequestLoad( url=%s observer=%p ) Using cached texture id@%d, textureId=%d\n",
409                    url.GetUrl().c_str(), observer, cacheIndex, textureId );
410   }
411
412   if( textureId == INVALID_TEXTURE_ID ) // There was no caching, or caching not required
413   {
414     // We need a new Texture.
415     textureId = GenerateUniqueTextureId();
416     bool preMultiply = ( preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD );
417     mTextureInfoContainer.push_back( TextureInfo( textureId, maskTextureId, url.GetUrl(),
418                                                   desiredSize, contentScale, fittingMode, samplingMode,
419                                                   false, cropToMask, useAtlas, textureHash, orientationCorrection,
420                                                   preMultiply ) );
421     cacheIndex = mTextureInfoContainer.size() - 1u;
422
423     DALI_LOG_INFO( gTextureManagerLogFilter, Debug::General, "TextureManager::RequestLoad( url=%s observer=%p ) New texture, cacheIndex:%d, textureId=%d\n",
424                    url.GetUrl().c_str(), observer, cacheIndex, textureId );
425   }
426
427   // The below code path is common whether we are using the cache or not.
428   // The textureInfoIndex now refers to either a pre-existing cached TextureInfo,
429   // or a new TextureInfo just created.
430   TextureInfo& textureInfo( mTextureInfoContainer[ cacheIndex ] );
431   textureInfo.maskTextureId = maskTextureId;
432   textureInfo.storageType = storageType;
433   textureInfo.orientationCorrection = orientationCorrection;
434
435   DALI_LOG_INFO( gTextureManagerLogFilter, Debug::General, "TextureInfo loadState:%s\n",
436                  GET_LOAD_STATE_STRING(textureInfo.loadState ) );
437
438   // Force reloading of texture by setting loadState unless already loading or cancelled.
439   if ( TextureManager::ReloadPolicy::FORCED == reloadPolicy &&
440        TextureManager::LOADING != textureInfo.loadState &&
441        TextureManager::WAITING_FOR_MASK != textureInfo.loadState &&
442        TextureManager::MASK_APPLYING != textureInfo.loadState &&
443        TextureManager::MASK_APPLIED != textureInfo.loadState &&
444        TextureManager::CANCELLED != textureInfo.loadState )
445   {
446     DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Verbose, "TextureManager::RequestLoad( url=%s observer=%p ) ForcedReload cacheIndex:%d, textureId=%d\n",
447                    url.GetUrl().c_str(), observer, cacheIndex, textureId );
448
449     textureInfo.loadState = TextureManager::NOT_STARTED;
450   }
451
452   // Check if we should add the observer.
453   // Only do this if we have not loaded yet and it will not have loaded by the end of this method.
454   switch( textureInfo.loadState )
455   {
456     case TextureManager::LOAD_FAILED: // Failed notifies observer which then stops observing.
457     case TextureManager::NOT_STARTED:
458     {
459       LoadOrQueueTexture( textureInfo, observer ); // If called inside NotifyObservers, queues until afterwards
460       break;
461     }
462     case TextureManager::LOADING:
463     case TextureManager::WAITING_FOR_MASK:
464     case TextureManager::MASK_APPLYING:
465     case TextureManager::MASK_APPLIED:
466     {
467       ObserveTexture( textureInfo, observer );
468       break;
469     }
470     case TextureManager::UPLOADED:
471     {
472       if( observer )
473       {
474         LoadOrQueueTexture( textureInfo, observer );
475       }
476       break;
477     }
478     case TextureManager::CANCELLED:
479     {
480       // A cancelled texture hasn't finished loading yet. Treat as a loading texture
481       // (it's ref count has already been incremented, above)
482       textureInfo.loadState = TextureManager::LOADING;
483       ObserveTexture( textureInfo, observer );
484       break;
485     }
486     case TextureManager::LOAD_FINISHED:
487     {
488       // Loading has already completed.
489       if( observer && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER )
490       {
491         LoadOrQueueTexture( textureInfo, observer );
492       }
493       break;
494     }
495   }
496
497   // Return the TextureId for which this Texture can now be referenced by externally.
498   return textureId;
499 }
500
501 void TextureManager::Remove( const TextureManager::TextureId textureId, TextureUploadObserver* observer )
502 {
503   int textureInfoIndex = GetCacheIndexFromId( textureId );
504   if( textureInfoIndex != INVALID_INDEX )
505   {
506     TextureInfo& textureInfo( mTextureInfoContainer[ textureInfoIndex ] );
507
508     DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise,
509                    "TextureManager::Remove(%d) url:%s\n  cacheIdx:%d loadState:%s reference count = %d\n",
510                    textureId, textureInfo.url.GetUrl().c_str(),
511                    textureInfoIndex, GET_LOAD_STATE_STRING( textureInfo.loadState ), textureInfo.referenceCount );
512
513     // Decrement the reference count and check if this is the last user of this Texture.
514     if( --textureInfo.referenceCount <= 0 )
515     {
516       // This is the last remove for this Texture.
517       textureInfo.referenceCount = 0;
518       bool removeTextureInfo = false;
519
520       // If loaded, we can remove the TextureInfo and the Atlas (if atlased).
521       if( textureInfo.loadState == UPLOADED )
522       {
523         if( textureInfo.atlas )
524         {
525           textureInfo.atlas.Remove( textureInfo.atlasRect );
526         }
527         removeTextureInfo = true;
528       }
529       else if( textureInfo.loadState == LOADING )
530       {
531         // We mark the textureInfo for removal.
532         // Once the load has completed, this method will be called again.
533         textureInfo.loadState = CANCELLED;
534       }
535       else
536       {
537         // In other states, we are not waiting for a load so we are safe to remove the TextureInfo data.
538         removeTextureInfo = true;
539       }
540
541       // If the state allows us to remove the TextureInfo data, we do so.
542       if( removeTextureInfo )
543       {
544         // Permanently remove the textureInfo struct.
545         mTextureInfoContainer.erase( mTextureInfoContainer.begin() + textureInfoIndex );
546       }
547     }
548
549     if( observer )
550     {
551       // Remove element from the LoadQueue
552       for( auto&& element : mLoadQueue )
553       {
554         if( element.mObserver == observer )
555         {
556           mLoadQueue.Erase( &element );
557           break;
558         }
559       }
560     }
561   }
562 }
563
564 VisualUrl TextureManager::GetVisualUrl( TextureId textureId )
565 {
566   VisualUrl visualUrl("");
567   int cacheIndex = GetCacheIndexFromId( textureId );
568
569   if( cacheIndex != INVALID_CACHE_INDEX )
570   {
571     DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::GetVisualUrl. Using cached texture id=%d, textureId=%d\n",
572                    cacheIndex, textureId );
573
574     TextureInfo& cachedTextureInfo( mTextureInfoContainer[ cacheIndex ] );
575     visualUrl = cachedTextureInfo.url;
576   }
577   return visualUrl;
578 }
579
580 TextureManager::LoadState TextureManager::GetTextureState( TextureId textureId )
581 {
582   LoadState loadState = TextureManager::NOT_STARTED;
583
584   int cacheIndex = GetCacheIndexFromId( textureId );
585   if( cacheIndex != INVALID_CACHE_INDEX )
586   {
587     TextureInfo& cachedTextureInfo( mTextureInfoContainer[ cacheIndex ] );
588     loadState = cachedTextureInfo.loadState;
589   }
590   else
591   {
592     for( auto&& elem : mExternalTextures )
593     {
594       if( elem.textureId == textureId )
595       {
596         loadState = LoadState::UPLOADED;
597         break;
598       }
599     }
600   }
601   return loadState;
602 }
603
604 TextureManager::LoadState TextureManager::GetTextureStateInternal( TextureId textureId )
605 {
606   LoadState loadState = TextureManager::NOT_STARTED;
607
608   int cacheIndex = GetCacheIndexFromId( textureId );
609   if( cacheIndex != INVALID_CACHE_INDEX )
610   {
611     TextureInfo& cachedTextureInfo( mTextureInfoContainer[ cacheIndex ] );
612     loadState = cachedTextureInfo.loadState;
613   }
614
615   return loadState;
616 }
617
618 TextureSet TextureManager::GetTextureSet( TextureId textureId )
619 {
620   TextureSet textureSet;// empty handle
621
622   int cacheIndex = GetCacheIndexFromId( textureId );
623   if( cacheIndex != INVALID_CACHE_INDEX )
624   {
625     TextureInfo& cachedTextureInfo( mTextureInfoContainer[ cacheIndex ] );
626     textureSet = cachedTextureInfo.textureSet;
627   }
628   else
629   {
630     for( auto&& elem : mExternalTextures )
631     {
632       if( elem.textureId == textureId )
633       {
634         textureSet = elem.textureSet;
635         break;
636       }
637     }
638   }
639   return textureSet;
640 }
641
642 std::string TextureManager::AddExternalTexture( TextureSet& textureSet )
643 {
644   TextureManager::ExternalTextureInfo info;
645   info.textureId = GenerateUniqueTextureId();
646   info.textureSet = textureSet;
647   mExternalTextures.emplace_back( info );
648   return VisualUrl::CreateTextureUrl( std::to_string( info.textureId ) );
649 }
650
651 TextureSet TextureManager::RemoveExternalTexture( const std::string& url )
652 {
653   if( url.size() > 0u )
654   {
655     // get the location from the Url
656     VisualUrl parseUrl( url );
657     if( VisualUrl::TEXTURE == parseUrl.GetProtocolType() )
658     {
659       std::string location = parseUrl.GetLocation();
660       if( location.size() > 0u )
661       {
662         TextureId id = std::stoi( location );
663         const auto end = mExternalTextures.end();
664         for( auto iter = mExternalTextures.begin(); iter != end; ++iter )
665         {
666           if( iter->textureId == id )
667           {
668             auto textureSet = iter->textureSet;
669             mExternalTextures.erase( iter );
670             return textureSet;
671           }
672         }
673       }
674     }
675   }
676   return TextureSet();
677 }
678
679
680 void TextureManager::AddObserver( TextureManager::LifecycleObserver& observer )
681 {
682   // make sure an observer doesn't observe the same object twice
683   // otherwise it will get multiple calls to ObjectDestroyed()
684   DALI_ASSERT_DEBUG( mLifecycleObservers.End() == std::find( mLifecycleObservers.Begin(), mLifecycleObservers.End(), &observer));
685   mLifecycleObservers.PushBack( &observer );
686 }
687
688 void TextureManager::RemoveObserver( TextureManager::LifecycleObserver& observer)
689 {
690   // Find the observer...
691   auto endIter =  mLifecycleObservers.End();
692   for( auto iter = mLifecycleObservers.Begin(); iter != endIter; ++iter)
693   {
694     if( (*iter) == &observer)
695     {
696       mLifecycleObservers.Erase( iter );
697       break;
698     }
699   }
700   DALI_ASSERT_DEBUG(endIter != mLifecycleObservers.End());
701 }
702
703 void TextureManager::LoadOrQueueTexture( TextureInfo& textureInfo, TextureUploadObserver* observer )
704 {
705   switch( textureInfo.loadState )
706   {
707     case NOT_STARTED:
708     case LOAD_FAILED:
709     {
710       if( mQueueLoadFlag )
711       {
712         QueueLoadTexture( textureInfo, observer );
713       }
714       else
715       {
716         LoadTexture( textureInfo, observer );
717       }
718       break;
719     }
720     case UPLOADED:
721     {
722       if( mQueueLoadFlag )
723       {
724         QueueLoadTexture( textureInfo, observer );
725       }
726       else
727       {
728         // The Texture has already loaded. The other observers have already been notified.
729         // We need to send a "late" loaded notification for this observer.
730         observer->UploadComplete( true, textureInfo.textureId, textureInfo.textureSet,
731                                   textureInfo.useAtlas, textureInfo.atlasRect,
732                                   textureInfo.preMultiplied );
733       }
734       break;
735     }
736     case LOADING:
737     case CANCELLED:
738     case LOAD_FINISHED:
739     case WAITING_FOR_MASK:
740     case MASK_APPLYING:
741     case MASK_APPLIED:
742     {
743       break;
744     }
745   }
746 }
747
748 void TextureManager::QueueLoadTexture( TextureInfo& textureInfo, TextureUploadObserver* observer )
749 {
750   auto textureId = textureInfo.textureId;
751   mLoadQueue.PushBack( LoadQueueElement( textureId, observer) );
752 }
753
754 void TextureManager::LoadTexture( TextureInfo& textureInfo, TextureUploadObserver* observer )
755 {
756   DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::LoadTexture(): url:%s sync:%s\n",
757                  textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously?"T":"F" );
758
759   textureInfo.loadState = LOADING;
760   if( !textureInfo.loadSynchronously )
761   {
762     auto& loadersContainer = textureInfo.url.IsLocalResource() ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
763     auto loadingHelperIt = loadersContainer.GetNext();
764     auto premultiplyOnLoad = ( textureInfo.preMultiplyOnLoad && textureInfo.maskTextureId == INVALID_TEXTURE_ID ) ?
765                                DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
766     DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
767     loadingHelperIt->Load(textureInfo.textureId, textureInfo.url,
768                           textureInfo.desiredSize, textureInfo.fittingMode,
769                           textureInfo.samplingMode, textureInfo.orientationCorrection,
770                           premultiplyOnLoad );
771   }
772   ObserveTexture( textureInfo, observer );
773 }
774
775 void TextureManager::ProcessQueuedTextures()
776 {
777   for( auto&& element : mLoadQueue )
778   {
779     int cacheIndex = GetCacheIndexFromId( element.mTextureId );
780     if( cacheIndex != INVALID_CACHE_INDEX )
781     {
782       TextureInfo& textureInfo( mTextureInfoContainer[cacheIndex] );
783       if( textureInfo.loadState == UPLOADED )
784       {
785         element.mObserver->UploadComplete( true, textureInfo.textureId, textureInfo.textureSet,
786                                            textureInfo.useAtlas, textureInfo.atlasRect,
787                                            textureInfo.preMultiplied );
788       }
789       else if ( textureInfo.loadState == LOAD_FINISHED && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER )
790       {
791         element.mObserver->LoadComplete( true, textureInfo.pixelBuffer, textureInfo.url, textureInfo.preMultiplied );
792       }
793       else
794       {
795         LoadTexture( textureInfo, element.mObserver );
796       }
797     }
798   }
799   mLoadQueue.Clear();
800 }
801
802 void TextureManager::ObserveTexture( TextureInfo& textureInfo,
803                                      TextureUploadObserver* observer )
804 {
805   DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::ObserveTexture(): url:%s observer:%p\n",
806                  textureInfo.url.GetUrl().c_str(), observer );
807
808   if( observer )
809   {
810     textureInfo.observerList.PushBack( observer );
811     observer->DestructionSignal().Connect( this, &TextureManager::ObserverDestroyed );
812   }
813 }
814
815 void TextureManager::AsyncLoadComplete( AsyncLoadingInfoContainerType& loadingContainer, uint32_t id,
816                                         Devel::PixelBuffer pixelBuffer )
817 {
818   DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::AsyncLoadComplete( id:%d )\n", id );
819
820   if( loadingContainer.size() >= 1u )
821   {
822     AsyncLoadingInfo loadingInfo = loadingContainer.front();
823
824     if( loadingInfo.loadId == id )
825     {
826       int cacheIndex = GetCacheIndexFromId( loadingInfo.textureId );
827       if( cacheIndex != INVALID_CACHE_INDEX )
828       {
829         TextureInfo& textureInfo( mTextureInfoContainer[cacheIndex] );
830
831         DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise,
832                        "  textureId:%d Url:%s CacheIndex:%d LoadState: %d\n",
833                        textureInfo.textureId, textureInfo.url.GetUrl().c_str(), cacheIndex, textureInfo.loadState );
834
835         if( textureInfo.loadState != CANCELLED )
836         {
837           // textureInfo can be invalidated after this call (as the mTextureInfoContainer may be modified)
838           PostLoad( textureInfo, pixelBuffer );
839         }
840         else
841         {
842           Remove( textureInfo.textureId, nullptr );
843         }
844       }
845     }
846
847     loadingContainer.pop_front();
848   }
849 }
850
851 void TextureManager::PostLoad( TextureInfo& textureInfo, Devel::PixelBuffer& pixelBuffer )
852 {
853   // Was the load successful?
854   if( pixelBuffer && ( pixelBuffer.GetWidth() != 0 ) && ( pixelBuffer.GetHeight() != 0 ) )
855   {
856     // No atlas support for now
857     textureInfo.useAtlas = NO_ATLAS;
858     textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
859
860     if( textureInfo.storageType == UPLOAD_TO_TEXTURE )
861     {
862       // If there is a mask texture ID associated with this texture, then apply the mask
863       // if it's already loaded. If it hasn't, and the mask is still loading,
864       // wait for the mask to finish loading.
865       if( textureInfo.maskTextureId != INVALID_TEXTURE_ID )
866       {
867         if( textureInfo.loadState == MASK_APPLYING )
868         {
869           textureInfo.loadState = MASK_APPLIED;
870           UploadTexture( pixelBuffer, textureInfo );
871           NotifyObservers( textureInfo, true );
872         }
873         else
874         {
875           LoadState maskLoadState = GetTextureStateInternal( textureInfo.maskTextureId );
876           textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
877           if( maskLoadState == LOADING )
878           {
879             textureInfo.loadState = WAITING_FOR_MASK;
880           }
881           else if( maskLoadState == LOAD_FINISHED )
882           {
883             // Send New Task to Thread
884             ApplyMask( textureInfo, textureInfo.maskTextureId );
885           }
886         }
887       }
888       else
889       {
890         UploadTexture( pixelBuffer, textureInfo );
891         NotifyObservers( textureInfo, true );
892       }
893     }
894     else
895     {
896       textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
897       textureInfo.loadState = LOAD_FINISHED;
898
899       if( textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER )
900       {
901         NotifyObservers( textureInfo, true );
902       }
903       else
904       {
905         // Check if there was another texture waiting for this load to complete
906         // (e.g. if this was an image mask, and its load is on a different thread)
907         CheckForWaitingTexture( textureInfo );
908       }
909     }
910   }
911   else
912   {
913     // @todo If the load was unsuccessful, upload the broken image.
914     textureInfo.loadState = LOAD_FAILED;
915     CheckForWaitingTexture( textureInfo );
916     NotifyObservers( textureInfo, false );
917   }
918 }
919
920 void TextureManager::CheckForWaitingTexture( TextureInfo& maskTextureInfo )
921 {
922   // Search the cache, checking if any texture has this texture id as a
923   // maskTextureId:
924   const unsigned int size = mTextureInfoContainer.size();
925
926   for( unsigned int cacheIndex = 0; cacheIndex < size; ++cacheIndex )
927   {
928     if( mTextureInfoContainer[cacheIndex].maskTextureId == maskTextureInfo.textureId &&
929         mTextureInfoContainer[cacheIndex].loadState == WAITING_FOR_MASK )
930     {
931       TextureInfo& textureInfo( mTextureInfoContainer[cacheIndex] );
932
933       if( maskTextureInfo.loadState == LOAD_FINISHED )
934       {
935         // Send New Task to Thread
936         ApplyMask( textureInfo, maskTextureInfo.textureId );
937       }
938       else
939       {
940         textureInfo.pixelBuffer.Reset();
941         textureInfo.loadState = LOAD_FAILED;
942         NotifyObservers( textureInfo, false );
943       }
944     }
945   }
946 }
947
948 void TextureManager::ApplyMask( TextureInfo& textureInfo, TextureId maskTextureId )
949 {
950   int maskCacheIndex = GetCacheIndexFromId( maskTextureId );
951   if( maskCacheIndex != INVALID_CACHE_INDEX )
952   {
953     Devel::PixelBuffer maskPixelBuffer = mTextureInfoContainer[maskCacheIndex].pixelBuffer;
954     Devel::PixelBuffer pixelBuffer = textureInfo.pixelBuffer;
955     textureInfo.pixelBuffer.Reset();
956
957     DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::ApplyMask(): url:%s sync:%s\n",
958                    textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously?"T":"F" );
959
960     textureInfo.loadState = MASK_APPLYING;
961     auto& loadersContainer = textureInfo.url.IsLocalResource() ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
962     auto loadingHelperIt = loadersContainer.GetNext();
963     auto premultiplyOnLoad = textureInfo.preMultiplyOnLoad ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
964     DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
965     loadingHelperIt->ApplyMask( textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask, premultiplyOnLoad );
966   }
967 }
968
969 void TextureManager::UploadTexture( Devel::PixelBuffer& pixelBuffer, TextureInfo& textureInfo )
970 {
971   if( textureInfo.useAtlas != USE_ATLAS )
972   {
973     DALI_LOG_INFO( gTextureManagerLogFilter, Debug::General, "  TextureManager::UploadTexture() New Texture for textureId:%d\n", textureInfo.textureId );
974
975     // Check if this pixelBuffer is premultiplied
976     textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
977
978     Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(),
979                                     pixelBuffer.GetWidth(), pixelBuffer.GetHeight() );
980
981     PixelData pixelData = Devel::PixelBuffer::Convert( pixelBuffer );
982     texture.Upload( pixelData );
983     if ( ! textureInfo.textureSet )
984     {
985       textureInfo.textureSet = TextureSet::New();
986     }
987     textureInfo.textureSet.SetTexture( 0u, texture );
988   }
989
990   // Update the load state.
991   // Note: This is regardless of success as we care about whether a
992   // load attempt is in progress or not.  If unsuccessful, a broken
993   // image is still loaded.
994   textureInfo.loadState = UPLOADED;
995 }
996
997 void TextureManager::NotifyObservers( TextureInfo& textureInfo, bool success )
998 {
999   TextureId textureId = textureInfo.textureId;
1000
1001   // If there is an observer: Notify the load is complete, whether successful or not,
1002   // and erase it from the list
1003   TextureInfo* info = &textureInfo;
1004
1005   mQueueLoadFlag = true;
1006
1007   while( info->observerList.Count() )
1008   {
1009     TextureUploadObserver* observer = info->observerList[0];
1010
1011     // During UploadComplete() a Control ResourceReady() signal is emitted.
1012     // During that signal the app may add remove /add Textures (e.g. via
1013     // ImageViews).
1014     // It is possible for observers to be removed from the observer list,
1015     // and it is also possible for the mTextureInfoContainer to be modified,
1016     // invalidating the reference to the textureInfo struct.
1017     // Texture load requests for the same URL are deferred until the end of this
1018     // method.
1019     DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "NotifyObservers() url:%s loadState:%s\n",
1020                    textureInfo.url.GetUrl().c_str(), GET_LOAD_STATE_STRING(textureInfo.loadState ) );
1021
1022     // It is possible for the observer to be deleted.
1023     // Disconnect and remove the observer first.
1024     observer->DestructionSignal().Disconnect( this, &TextureManager::ObserverDestroyed );
1025
1026     info->observerList.Erase( info->observerList.begin() );
1027
1028     if( info->storageType == StorageType::RETURN_PIXEL_BUFFER )
1029     {
1030       observer->LoadComplete( success, info->pixelBuffer, info->url, info->preMultiplied );
1031     }
1032     else
1033     {
1034       observer->UploadComplete( success, info->textureId, info->textureSet, info->useAtlas, info->atlasRect,
1035                                 info->preMultiplied );
1036     }
1037
1038     // Get the textureInfo from the container again as it may have been invalidated.
1039     int textureInfoIndex = GetCacheIndexFromId( textureId );
1040     if( textureInfoIndex == INVALID_CACHE_INDEX)
1041     {
1042       break; // texture has been removed - can stop.
1043     }
1044     info = &mTextureInfoContainer[ textureInfoIndex ];
1045   }
1046
1047   mQueueLoadFlag = false;
1048   ProcessQueuedTextures();
1049
1050   if( info->storageType == StorageType::RETURN_PIXEL_BUFFER && info->observerList.Count() == 0 )
1051   {
1052     Remove( info->textureId, nullptr );
1053   }
1054 }
1055
1056 TextureManager::TextureId TextureManager::GenerateUniqueTextureId()
1057 {
1058   return mCurrentTextureId++;
1059 }
1060
1061 int TextureManager::GetCacheIndexFromId( const TextureId textureId )
1062 {
1063   const unsigned int size = mTextureInfoContainer.size();
1064
1065   for( unsigned int i = 0; i < size; ++i )
1066   {
1067     if( mTextureInfoContainer[i].textureId == textureId )
1068     {
1069       return i;
1070     }
1071   }
1072
1073   return INVALID_CACHE_INDEX;
1074 }
1075
1076 TextureManager::TextureHash TextureManager::GenerateHash(
1077   const std::string&             url,
1078   const ImageDimensions          size,
1079   const FittingMode::Type        fittingMode,
1080   const Dali::SamplingMode::Type samplingMode,
1081   const UseAtlas                 useAtlas,
1082   TextureId                      maskTextureId )
1083 {
1084   std::string hashTarget( url );
1085   const size_t urlLength = hashTarget.length();
1086   const uint16_t width = size.GetWidth();
1087   const uint16_t height = size.GetWidth();
1088
1089   // If either the width or height has been specified, include the resizing options in the hash
1090   if( width != 0 || height != 0 )
1091   {
1092     // We are appending 5 bytes to the URL to form the hash input.
1093     hashTarget.resize( urlLength + 5u );
1094     char* hashTargetPtr = &( hashTarget[ urlLength ] );
1095
1096     // Pack the width and height (4 bytes total).
1097     *hashTargetPtr++ = size.GetWidth() & 0xff;
1098     *hashTargetPtr++ = ( size.GetWidth() >> 8u ) & 0xff;
1099     *hashTargetPtr++ = size.GetHeight() & 0xff;
1100     *hashTargetPtr++ = ( size.GetHeight() >> 8u ) & 0xff;
1101
1102     // Bit-pack the FittingMode, SamplingMode and atlasing.
1103     // FittingMode=2bits, SamplingMode=3bits, useAtlas=1bit
1104     *hashTargetPtr   = ( fittingMode << 4u ) | ( samplingMode << 1 ) | useAtlas;
1105   }
1106   else
1107   {
1108     // We are not including sizing information, but we still need an extra byte for atlasing.
1109     hashTarget.resize( urlLength + 1u );
1110
1111     // Add the atlasing to the hash input.
1112     switch( useAtlas )
1113     {
1114       case UseAtlas::NO_ATLAS:
1115       {
1116         hashTarget[ urlLength ] = 'f';
1117         break;
1118       }
1119       case UseAtlas::USE_ATLAS:
1120       {
1121         hashTarget[ urlLength ] = 't';
1122         break;
1123       }
1124     }
1125   }
1126
1127   if( maskTextureId != INVALID_TEXTURE_ID )
1128   {
1129     auto textureIdIndex = hashTarget.length();
1130     hashTarget.resize( hashTarget.length() + sizeof( TextureId ) );
1131     unsigned char* hashTargetPtr = reinterpret_cast<unsigned char*>(&( hashTarget[ textureIdIndex ] ));
1132
1133     // Append the texture id to the end of the URL byte by byte:
1134     // (to avoid SIGBUS / alignment issues)
1135     for( size_t byteIter = 0; byteIter < sizeof( TextureId ); ++byteIter )
1136     {
1137       *hashTargetPtr++ = maskTextureId & 0xff;
1138       maskTextureId >>= 8u;
1139     }
1140   }
1141
1142   return Dali::CalculateHash( hashTarget );
1143 }
1144
1145 int TextureManager::FindCachedTexture(
1146   const TextureManager::TextureHash hash,
1147   const std::string&                url,
1148   const ImageDimensions             size,
1149   const FittingMode::Type           fittingMode,
1150   const Dali::SamplingMode::Type    samplingMode,
1151   const bool                        useAtlas,
1152   TextureId                         maskTextureId,
1153   TextureManager::MultiplyOnLoad    preMultiplyOnLoad,
1154   StorageType                       storageType )
1155 {
1156   // Default to an invalid ID, in case we do not find a match.
1157   int cacheIndex = INVALID_CACHE_INDEX;
1158
1159   // Iterate through our hashes to find a match.
1160   const unsigned int count = mTextureInfoContainer.size();
1161   for( unsigned int i = 0u; i < count; ++i )
1162   {
1163     if( mTextureInfoContainer[i].hash == hash )
1164     {
1165       // We have a match, now we check all the original parameters in case of a hash collision.
1166       TextureInfo& textureInfo( mTextureInfoContainer[i] );
1167
1168       if( ( url == textureInfo.url.GetUrl() ) &&
1169           ( useAtlas == textureInfo.useAtlas ) &&
1170           ( maskTextureId == textureInfo.maskTextureId ) &&
1171           ( size == textureInfo.desiredSize ) &&
1172           ( ( size.GetWidth() == 0 && size.GetHeight() == 0 ) ||
1173             ( fittingMode == textureInfo.fittingMode &&
1174               samplingMode == textureInfo.samplingMode ) ) &&
1175           ( storageType == textureInfo.storageType ) )
1176       {
1177         // 1. If preMultiplyOnLoad is MULTIPLY_ON_LOAD, then textureInfo.preMultiplyOnLoad should be true. The premultiplication result can be different.
1178         // 2. If preMultiplyOnLoad is LOAD_WITHOUT_MULTIPLY, then textureInfo.preMultiplied should be false.
1179         if( ( preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD && textureInfo.preMultiplyOnLoad )
1180             || ( preMultiplyOnLoad == TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY && !textureInfo.preMultiplied ) )
1181         {
1182           // The found Texture is a match.
1183           cacheIndex = i;
1184           break;
1185         }
1186       }
1187     }
1188   }
1189
1190   return cacheIndex;
1191 }
1192
1193 void TextureManager::ObserverDestroyed( TextureUploadObserver* observer )
1194 {
1195   const unsigned int count = mTextureInfoContainer.size();
1196   for( unsigned int i = 0; i < count; ++i )
1197   {
1198     TextureInfo& textureInfo( mTextureInfoContainer[i] );
1199     for( TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin();
1200          j != textureInfo.observerList.End(); )
1201     {
1202       if( *j == observer )
1203       {
1204         j = textureInfo.observerList.Erase( j );
1205       }
1206       else
1207       {
1208         ++j;
1209       }
1210     }
1211   }
1212 }
1213
1214
1215 TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(TextureManager& textureManager)
1216 : AsyncLoadingHelper(Toolkit::AsyncImageLoader::New(), textureManager,
1217                      AsyncLoadingInfoContainerType())
1218 {
1219 }
1220
1221 void TextureManager::AsyncLoadingHelper::Load(TextureId          textureId,
1222                                               const VisualUrl&   url,
1223                                               ImageDimensions    desiredSize,
1224                                               FittingMode::Type  fittingMode,
1225                                               SamplingMode::Type samplingMode,
1226                                               bool               orientationCorrection,
1227                                               DevelAsyncImageLoader::PreMultiplyOnLoad  preMultiplyOnLoad)
1228 {
1229   mLoadingInfoContainer.push_back(AsyncLoadingInfo(textureId));
1230   auto id = DevelAsyncImageLoader::Load( mLoader, url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad );
1231   mLoadingInfoContainer.back().loadId = id;
1232 }
1233
1234 void TextureManager::AsyncLoadingHelper::ApplyMask( TextureId textureId,
1235                                                     Devel::PixelBuffer pixelBuffer,
1236                                                     Devel::PixelBuffer maskPixelBuffer,
1237                                                     float contentScale,
1238                                                     bool cropToMask,
1239                                                     DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad )
1240 {
1241   mLoadingInfoContainer.push_back(AsyncLoadingInfo(textureId));
1242   auto id = DevelAsyncImageLoader::ApplyMask( mLoader, pixelBuffer, maskPixelBuffer, contentScale, cropToMask, preMultiplyOnLoad );
1243   mLoadingInfoContainer.back().loadId = id;
1244 }
1245
1246 TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(AsyncLoadingHelper&& rhs)
1247 : AsyncLoadingHelper(rhs.mLoader, rhs.mTextureManager, std::move(rhs.mLoadingInfoContainer))
1248 {
1249 }
1250
1251 TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(
1252     Toolkit::AsyncImageLoader loader,
1253     TextureManager& textureManager,
1254     AsyncLoadingInfoContainerType&& loadingInfoContainer)
1255 : mLoader(loader),
1256   mTextureManager(textureManager),
1257   mLoadingInfoContainer(std::move(loadingInfoContainer))
1258 {
1259   DevelAsyncImageLoader::PixelBufferLoadedSignal(mLoader).Connect(
1260       this, &AsyncLoadingHelper::AsyncLoadComplete);
1261 }
1262
1263 void TextureManager::AsyncLoadingHelper::AsyncLoadComplete(uint32_t           id,
1264                                                            Devel::PixelBuffer pixelBuffer )
1265 {
1266   mTextureManager.AsyncLoadComplete( mLoadingInfoContainer, id, pixelBuffer );
1267 }
1268
1269 void TextureManager::SetBrokenImageUrl(const std::string& brokenImageUrl)
1270 {
1271   mBrokenImageUrl = brokenImageUrl;
1272 }
1273
1274 } // namespace Internal
1275
1276 } // namespace Toolkit
1277
1278 } // namespace Dali