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