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