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