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