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