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