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