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