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