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