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