31c57c6cbb6c0d16c9c80186a4f384ecc33104f9
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / texture-manager / texture-manager-impl.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "texture-manager-impl.h"
20
21 // EXTERNAL HEADERS
22 #include <dali/devel-api/adaptor-framework/environment-variable.h>
23 #include <dali/devel-api/adaptor-framework/image-loading.h>
24 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/public-api/rendering/geometry.h>
27
28 // INTERNAL HEADERS
29 #include <dali-toolkit/internal/texture-manager/texture-async-loading-helper.h>
30 #include <dali-toolkit/internal/texture-manager/texture-cache-manager.h>
31 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
32 #include <dali-toolkit/internal/visuals/rendering-addon.h>
33
34 namespace
35 {
36 constexpr auto INITIAL_HASH_NUMBER                     = size_t{0u};
37 constexpr auto DEFAULT_NUMBER_OF_LOCAL_LOADER_THREADS  = size_t{4u};
38 constexpr auto DEFAULT_NUMBER_OF_REMOTE_LOADER_THREADS = size_t{8u};
39
40 constexpr auto NUMBER_OF_LOCAL_LOADER_THREADS_ENV  = "DALI_TEXTURE_LOCAL_THREADS";
41 constexpr auto NUMBER_OF_REMOTE_LOADER_THREADS_ENV = "DALI_TEXTURE_REMOTE_THREADS";
42
43 size_t GetNumberOfThreads(const char* environmentVariable, size_t defaultValue)
44 {
45   using Dali::EnvironmentVariable::GetEnvironmentVariable;
46   auto           numberString          = GetEnvironmentVariable(environmentVariable);
47   auto           numberOfThreads       = numberString ? std::strtoul(numberString, nullptr, 10) : 0;
48   constexpr auto MAX_NUMBER_OF_THREADS = 100u;
49   DALI_ASSERT_DEBUG(numberOfThreads < MAX_NUMBER_OF_THREADS);
50   return (numberOfThreads > 0 && numberOfThreads < MAX_NUMBER_OF_THREADS) ? numberOfThreads : defaultValue;
51 }
52
53 size_t GetNumberOfLocalLoaderThreads()
54 {
55   return GetNumberOfThreads(NUMBER_OF_LOCAL_LOADER_THREADS_ENV, DEFAULT_NUMBER_OF_LOCAL_LOADER_THREADS);
56 }
57
58 size_t GetNumberOfRemoteLoaderThreads()
59 {
60   return GetNumberOfThreads(NUMBER_OF_REMOTE_LOADER_THREADS_ENV, DEFAULT_NUMBER_OF_REMOTE_LOADER_THREADS);
61 }
62
63 } // namespace
64
65 namespace Dali
66 {
67 namespace Toolkit
68 {
69 namespace Internal
70 {
71 #ifdef DEBUG_ENABLED
72 // Define logfilter Internal namespace level so other files (e.g. texture-cache-manager.cpp) can also use this filter.
73 Debug::Filter* gTextureManagerLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TEXTURE_MANAGER");
74
75 // clang-format off
76 #define GET_LOAD_STATE_STRING(loadState) \
77   loadState == TextureManagerType::LoadState::NOT_STARTED      ? "NOT_STARTED"      : \
78   loadState == TextureManagerType::LoadState::LOADING          ? "LOADING"          : \
79   loadState == TextureManagerType::LoadState::LOAD_FINISHED    ? "LOAD_FINISHED"    : \
80   loadState == TextureManagerType::LoadState::WAITING_FOR_MASK ? "WAITING_FOR_MASK" : \
81   loadState == TextureManagerType::LoadState::MASK_APPLYING    ? "MASK_APPLYING"    : \
82   loadState == TextureManagerType::LoadState::MASK_APPLIED     ? "MASK_APPLIED"     : \
83   loadState == TextureManagerType::LoadState::UPLOADED         ? "UPLOADED"         : \
84   loadState == TextureManagerType::LoadState::CANCELLED        ? "CANCELLED"        : \
85   loadState == TextureManagerType::LoadState::LOAD_FAILED      ? "LOAD_FAILED"      : \
86                                                                  "Unknown"
87 // clang-format on
88 #endif
89
90 namespace
91 {
92 const uint32_t DEFAULT_ATLAS_SIZE(1024u);               ///< This size can fit 8 by 8 images of average size 128 * 128
93 const Vector4  FULL_ATLAS_RECT(0.0f, 0.0f, 1.0f, 1.0f); ///< UV Rectangle that covers the full Texture
94
95 void PreMultiply(Devel::PixelBuffer pixelBuffer, TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
96 {
97   if(Pixel::HasAlpha(pixelBuffer.GetPixelFormat()))
98   {
99     if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
100     {
101       pixelBuffer.MultiplyColorByAlpha();
102     }
103   }
104   else
105   {
106     preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
107   }
108 }
109
110 } // Anonymous namespace
111
112 TextureManager::MaskingData::MaskingData()
113 : mAlphaMaskUrl(),
114   mAlphaMaskId(INVALID_TEXTURE_ID),
115   mContentScaleFactor(1.0f),
116   mCropToMask(true)
117 {
118 }
119
120 TextureManager::TextureManager()
121 : mTextureCacheManager(),
122   mAsyncLocalLoaders(GetNumberOfLocalLoaderThreads(), [&]() { return TextureAsyncLoadingHelper(*this); }),
123   mAsyncRemoteLoaders(GetNumberOfRemoteLoaderThreads(), [&]() { return TextureAsyncLoadingHelper(*this); }),
124   mLifecycleObservers(),
125   mLoadQueue(),
126   mQueueLoadFlag(false)
127 {
128   // Initialize the AddOn
129   RenderingAddOn::Get();
130 }
131
132 TextureManager::~TextureManager()
133 {
134   for(auto iter = mLifecycleObservers.Begin(), endIter = mLifecycleObservers.End(); iter != endIter; ++iter)
135   {
136     (*iter)->TextureManagerDestroyed();
137   }
138 }
139
140 TextureSet TextureManager::LoadAnimatedImageTexture(
141   Dali::AnimatedImageLoading      animatedImageLoading,
142   const std::uint32_t&            frameIndex,
143   const Dali::SamplingMode::Type& samplingMode,
144   const bool&                     synchronousLoading,
145   TextureManager::TextureId&      textureId,
146   const Dali::WrapMode::Type&     wrapModeU,
147   const Dali::WrapMode::Type&     wrapModeV,
148   TextureUploadObserver*          textureObserver)
149 {
150   TextureSet textureSet;
151
152   if(synchronousLoading)
153   {
154     Devel::PixelBuffer pixelBuffer;
155     if(animatedImageLoading)
156     {
157       pixelBuffer = animatedImageLoading.LoadFrame(frameIndex);
158     }
159     if(!pixelBuffer)
160     {
161       DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous loading is failed\n");
162     }
163     else
164     {
165       PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
166       if(!textureSet)
167       {
168         Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight());
169         texture.Upload(pixelData);
170         textureSet = TextureSet::New();
171         textureSet.SetTexture(0u, texture);
172       }
173     }
174   }
175   else
176   {
177     auto preMultiply                    = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
178     textureId                           = RequestLoadInternal(animatedImageLoading.GetUrl(), INVALID_TEXTURE_ID, 1.0f, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, UseAtlas::NO_ATLAS, false, StorageType::UPLOAD_TO_TEXTURE, textureObserver, true, TextureManager::ReloadPolicy::CACHED, preMultiply, animatedImageLoading, frameIndex, false);
179     TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
180     if(loadState == TextureManager::LoadState::UPLOADED)
181     {
182       // LoadComplete has already been called - keep the same texture set
183       textureSet = GetTextureSet(textureId);
184     }
185   }
186
187   if(textureSet)
188   {
189     Sampler sampler = Sampler::New();
190     sampler.SetWrapMode(wrapModeU, wrapModeV);
191     textureSet.SetSampler(0u, sampler);
192   }
193
194   return textureSet;
195 }
196
197 Devel::PixelBuffer TextureManager::LoadPixelBuffer(
198   const VisualUrl&                url,
199   const Dali::ImageDimensions&    desiredSize,
200   const Dali::FittingMode::Type&  fittingMode,
201   const Dali::SamplingMode::Type& samplingMode,
202   const bool&                     synchronousLoading,
203   TextureUploadObserver*          textureObserver,
204   const bool&                     orientationCorrection,
205   TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
206 {
207   Devel::PixelBuffer pixelBuffer;
208   if(synchronousLoading)
209   {
210     if(url.IsValid())
211     {
212       if(url.IsBufferResource())
213       {
214         const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url.GetUrl());
215         if(encodedImageBuffer)
216         {
217           pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
218         }
219       }
220       else
221       {
222         pixelBuffer = LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
223       }
224       if(pixelBuffer && preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
225       {
226         PreMultiply(pixelBuffer, preMultiplyOnLoad);
227       }
228     }
229   }
230   else
231   {
232     RequestLoadInternal(url, INVALID_TEXTURE_ID, 1.0f, desiredSize, fittingMode, samplingMode, UseAtlas::NO_ATLAS, false, StorageType::RETURN_PIXEL_BUFFER, textureObserver, orientationCorrection, TextureManager::ReloadPolicy::FORCED, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, false);
233   }
234
235   return pixelBuffer;
236 }
237
238 TextureSet TextureManager::LoadTexture(
239   const VisualUrl&                    url,
240   const Dali::ImageDimensions&        desiredSize,
241   const Dali::FittingMode::Type&      fittingMode,
242   const Dali::SamplingMode::Type&     samplingMode,
243   MaskingDataPointer&                 maskInfo,
244   const bool&                         synchronousLoading,
245   TextureManager::TextureId&          textureId,
246   Vector4&                            textureRect,
247   Dali::ImageDimensions&              textureRectSize,
248   bool&                               atlasingStatus,
249   bool&                               loadingStatus,
250   const Dali::WrapMode::Type&         wrapModeU,
251   const Dali::WrapMode::Type&         wrapModeV,
252   TextureUploadObserver*              textureObserver,
253   AtlasUploadObserver*                atlasObserver,
254   ImageAtlasManagerPtr                imageAtlasManager,
255   const bool&                         orientationCorrection,
256   const TextureManager::ReloadPolicy& reloadPolicy,
257   TextureManager::MultiplyOnLoad&     preMultiplyOnLoad)
258 {
259   TextureSet textureSet;
260
261   loadingStatus = false;
262   textureRect   = FULL_ATLAS_RECT;
263
264   if(VisualUrl::TEXTURE == url.GetProtocolType())
265   {
266     std::string location = url.GetLocation();
267     if(location.size() > 0u)
268     {
269       TextureId id = std::stoi(location);
270       textureSet   = mTextureCacheManager.GetExternalTextureSet(id);
271       if(textureSet)
272       {
273         preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
274         textureId         = id;
275         return textureSet;
276       }
277     }
278   }
279   else
280   {
281     // For Atlas
282     if(synchronousLoading && atlasingStatus && imageAtlasManager->CheckAtlasAvailable(url, desiredSize))
283     {
284       Devel::PixelBuffer pixelBuffer = LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection);
285
286       if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
287       {
288         Devel::PixelBuffer maskPixelBuffer = LoadImageSynchronously(maskInfo->mAlphaMaskUrl.GetUrl(), ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true);
289         if(maskPixelBuffer)
290         {
291           pixelBuffer.ApplyMask(maskPixelBuffer, maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
292         }
293       }
294
295       PixelData data;
296       if(pixelBuffer)
297       {
298         PreMultiply(pixelBuffer, preMultiplyOnLoad);
299         data = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
300
301         if(data)
302         {
303           textureSet = imageAtlasManager->Add(textureRect, data);
304           if(textureSet)
305           {
306             textureRectSize.SetWidth(data.GetWidth());
307             textureRectSize.SetHeight(data.GetHeight());
308           }
309         }
310         else
311         {
312           DALI_LOG_ERROR("TextureManager::LoadTexture: Synchronous Texture loading with atlasing is failed.\n");
313         }
314       }
315       if(!textureSet)
316       {
317         atlasingStatus = false;
318       }
319     }
320
321     if(!textureSet)
322     {
323       loadingStatus = true;
324       // Atlas manager can chage desired size when it is set by 0,0.
325       // We should store into textureRectSize only if atlasing successed.
326       // So copy inputed desiredSize, and replace value into textureRectSize only if atlasing success.
327       Dali::ImageDimensions atlasDesiredSize = desiredSize;
328       if(atlasingStatus)
329       {
330         textureSet = imageAtlasManager->Add(textureRect, url.GetUrl(), atlasDesiredSize, fittingMode, true, atlasObserver);
331       }
332       if(!textureSet) // big image, no atlasing or atlasing failed
333       {
334         atlasingStatus = false;
335         if(!maskInfo || !maskInfo->mAlphaMaskUrl.IsValid())
336         {
337           textureId = RequestLoad(
338             url,
339             desiredSize,
340             fittingMode,
341             samplingMode,
342             UseAtlas::NO_ATLAS,
343             textureObserver,
344             orientationCorrection,
345             reloadPolicy,
346             preMultiplyOnLoad,
347             synchronousLoading);
348         }
349         else
350         {
351           maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, synchronousLoading);
352           textureId              = RequestLoad(
353             url,
354             maskInfo->mAlphaMaskId,
355             maskInfo->mContentScaleFactor,
356             desiredSize,
357             fittingMode,
358             samplingMode,
359             UseAtlas::NO_ATLAS,
360             maskInfo->mCropToMask,
361             textureObserver,
362             orientationCorrection,
363             reloadPolicy,
364             preMultiplyOnLoad,
365             synchronousLoading);
366         }
367
368         TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
369         if(loadState == TextureManager::LoadState::UPLOADED)
370         {
371           // LoadComplete has already been called - keep the same texture set
372           textureSet = mTextureCacheManager.GetTextureSet(textureId);
373         }
374
375         // If we are loading the texture, or waiting for the ready signal handler to complete, inform
376         // caller that they need to wait.
377         loadingStatus = (loadState == TextureManager::LoadState::LOADING ||
378                          loadState == TextureManager::LoadState::WAITING_FOR_MASK ||
379                          loadState == TextureManager::LoadState::MASK_APPLYING ||
380                          loadState == TextureManager::LoadState::MASK_APPLIED ||
381                          loadState == TextureManager::LoadState::NOT_STARTED ||
382                          mQueueLoadFlag);
383       }
384       else
385       {
386         textureRectSize = atlasDesiredSize;
387       }
388     }
389   }
390
391   if(!atlasingStatus && textureSet)
392   {
393     Sampler sampler = Sampler::New();
394     sampler.SetWrapMode(wrapModeU, wrapModeV);
395     textureSet.SetSampler(0u, sampler);
396   }
397
398   if(synchronousLoading)
399   {
400     loadingStatus = false;
401   }
402
403   return textureSet;
404 }
405
406 TextureManager::TextureId TextureManager::RequestLoad(
407   const VisualUrl&                    url,
408   const ImageDimensions&              desiredSize,
409   const Dali::FittingMode::Type&      fittingMode,
410   const Dali::SamplingMode::Type&     samplingMode,
411   const UseAtlas&                     useAtlas,
412   TextureUploadObserver*              observer,
413   const bool&                         orientationCorrection,
414   const TextureManager::ReloadPolicy& reloadPolicy,
415   TextureManager::MultiplyOnLoad&     preMultiplyOnLoad,
416   const bool&                         synchronousLoading)
417 {
418   return RequestLoadInternal(url, INVALID_TEXTURE_ID, 1.0f, desiredSize, fittingMode, samplingMode, useAtlas, false, StorageType::UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
419 }
420
421 TextureManager::TextureId TextureManager::RequestLoad(
422   const VisualUrl&                    url,
423   const TextureManager::TextureId&    maskTextureId,
424   const float&                        contentScale,
425   const Dali::ImageDimensions&        desiredSize,
426   const Dali::FittingMode::Type&      fittingMode,
427   const Dali::SamplingMode::Type&     samplingMode,
428   const TextureManager::UseAtlas&     useAtlas,
429   const bool&                         cropToMask,
430   TextureUploadObserver*              observer,
431   const bool&                         orientationCorrection,
432   const TextureManager::ReloadPolicy& reloadPolicy,
433   TextureManager::MultiplyOnLoad&     preMultiplyOnLoad,
434   const bool&                         synchronousLoading)
435 {
436   return RequestLoadInternal(url, maskTextureId, contentScale, desiredSize, fittingMode, samplingMode, useAtlas, cropToMask, StorageType::UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
437 }
438
439 TextureManager::TextureId TextureManager::RequestMaskLoad(
440   const VisualUrl& maskUrl,
441   const bool&      synchronousLoading)
442 {
443   // Use the normal load procedure to get the alpha mask.
444   auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
445   return RequestLoadInternal(maskUrl, INVALID_TEXTURE_ID, 1.0f, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, UseAtlas::NO_ATLAS, false, StorageType::KEEP_PIXEL_BUFFER, NULL, true, TextureManager::ReloadPolicy::CACHED, preMultiply, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
446 }
447
448 TextureManager::TextureId TextureManager::RequestLoadInternal(
449   const VisualUrl&                    url,
450   const TextureManager::TextureId&    maskTextureId,
451   const float&                        contentScale,
452   const Dali::ImageDimensions&        desiredSize,
453   const Dali::FittingMode::Type&      fittingMode,
454   const Dali::SamplingMode::Type&     samplingMode,
455   const TextureManager::UseAtlas&     useAtlas,
456   const bool&                         cropToMask,
457   const TextureManager::StorageType&  storageType,
458   TextureUploadObserver*              observer,
459   const bool&                         orientationCorrection,
460   const TextureManager::ReloadPolicy& reloadPolicy,
461   TextureManager::MultiplyOnLoad&     preMultiplyOnLoad,
462   Dali::AnimatedImageLoading          animatedImageLoading,
463   const std::uint32_t&                frameIndex,
464   const bool&                         synchronousLoading)
465 {
466   // First check if the requested Texture is cached.
467   bool isAnimatedImage = (animatedImageLoading) ? true : false;
468
469   TextureHash       textureHash = INITIAL_HASH_NUMBER;
470   TextureCacheIndex cacheIndex  = INVALID_CACHE_INDEX;
471   if(storageType != StorageType::RETURN_PIXEL_BUFFER && !isAnimatedImage)
472   {
473     textureHash = mTextureCacheManager.GenerateHash(url.GetUrl(), desiredSize, fittingMode, samplingMode, useAtlas, maskTextureId);
474
475     // Look up the texture by hash. Note: The extra parameters are used in case of a hash collision.
476     cacheIndex = mTextureCacheManager.FindCachedTexture(textureHash, url.GetUrl(), desiredSize, fittingMode, samplingMode, useAtlas, maskTextureId, preMultiplyOnLoad);
477   }
478
479   TextureManager::TextureId textureId = INVALID_TEXTURE_ID;
480   // Check if the requested Texture exists in the cache.
481   if(cacheIndex != INVALID_CACHE_INDEX)
482   {
483     if(TextureManager::ReloadPolicy::CACHED == reloadPolicy)
484     {
485       // Mark this texture being used by another client resource. Forced reload would replace the current texture
486       // without the need for incrementing the reference count.
487       ++(mTextureCacheManager[cacheIndex].referenceCount);
488     }
489     textureId = mTextureCacheManager[cacheIndex].textureId;
490
491     // Update preMultiplyOnLoad value. It should be changed according to preMultiplied value of the cached info.
492     preMultiplyOnLoad = mTextureCacheManager[cacheIndex].preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
493
494     DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::RequestLoad( url=%s observer=%p ) Using cached texture id@%d, textureId=%d\n", url.GetUrl().c_str(), observer, cacheIndex, textureId);
495   }
496
497   if(textureId == INVALID_TEXTURE_ID) // There was no caching, or caching not required
498   {
499     if(VisualUrl::BUFFER == url.GetProtocolType())
500     {
501       std::string location = url.GetLocation();
502       if(location.size() > 0u)
503       {
504         TextureId                 targetId           = std::stoi(location);
505         const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(targetId);
506         if(encodedImageBuffer)
507         {
508           textureId = targetId;
509
510           // Increase EncodedImageBuffer reference during it contains mTextureInfoContainer.
511           // TODO! We should change action when reload policy is FORCE.
512           // Eunki Hong will fix it after refactoring patch merged.
513           mTextureCacheManager.UseExternalResource(url.GetUrl());
514         }
515       }
516     }
517
518     if(textureId == INVALID_TEXTURE_ID)
519     {
520       textureId = mTextureCacheManager.GenerateUniqueTextureId();
521     }
522
523     bool preMultiply = (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
524
525     // Cache new texutre, and get cacheIndex.
526     cacheIndex = mTextureCacheManager.AppendCache(TextureInfo(textureId, maskTextureId, url, desiredSize, contentScale, fittingMode, samplingMode, false, cropToMask, useAtlas, textureHash, orientationCorrection, preMultiply, animatedImageLoading, frameIndex));
527
528     DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::RequestLoad( url=%s observer=%p ) New texture, cacheIndex:%d, textureId=%d\n", url.GetUrl().c_str(), observer, cacheIndex, textureId);
529   }
530
531   // The below code path is common whether we are using the cache or not.
532   // The textureInfoIndex now refers to either a pre-existing cached TextureInfo,
533   // or a new TextureInfo just created.
534   TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
535   textureInfo.maskTextureId         = maskTextureId;
536   textureInfo.storageType           = storageType;
537   textureInfo.orientationCorrection = orientationCorrection;
538
539   DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureInfo loadState:%s\n", GET_LOAD_STATE_STRING(textureInfo.loadState));
540
541   // Force reloading of texture by setting loadState unless already loading or cancelled.
542   if(TextureManager::ReloadPolicy::FORCED == reloadPolicy &&
543      TextureManager::LoadState::LOADING != textureInfo.loadState &&
544      TextureManager::LoadState::WAITING_FOR_MASK != textureInfo.loadState &&
545      TextureManager::LoadState::MASK_APPLYING != textureInfo.loadState &&
546      TextureManager::LoadState::MASK_APPLIED != textureInfo.loadState &&
547      TextureManager::LoadState::CANCELLED != textureInfo.loadState)
548   {
549     DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, "TextureManager::RequestLoad( url=%s observer=%p ) ForcedReload cacheIndex:%d, textureId=%d\n", url.GetUrl().c_str(), observer, cacheIndex, textureId);
550
551     textureInfo.loadState = TextureManager::LoadState::NOT_STARTED;
552   }
553
554   if(!synchronousLoading)
555   {
556     // Check if we should add the observer.
557     // Only do this if we have not loaded yet and it will not have loaded by the end of this method.
558     switch(textureInfo.loadState)
559     {
560       case TextureManager::LoadState::LOAD_FAILED: // Failed notifies observer which then stops observing.
561       case TextureManager::LoadState::NOT_STARTED:
562       {
563         LoadOrQueueTexture(textureInfo, observer); // If called inside NotifyObservers, queues until afterwards
564         break;
565       }
566       case TextureManager::LoadState::LOADING:
567       case TextureManager::LoadState::WAITING_FOR_MASK:
568       case TextureManager::LoadState::MASK_APPLYING:
569       case TextureManager::LoadState::MASK_APPLIED:
570       {
571         ObserveTexture(textureInfo, observer);
572         break;
573       }
574       case TextureManager::LoadState::UPLOADED:
575       {
576         if(observer)
577         {
578           LoadOrQueueTexture(textureInfo, observer);
579         }
580         break;
581       }
582       case TextureManager::LoadState::CANCELLED:
583       {
584         // A cancelled texture hasn't finished loading yet. Treat as a loading texture
585         // (it's ref count has already been incremented, above)
586         textureInfo.loadState = TextureManager::LoadState::LOADING;
587         ObserveTexture(textureInfo, observer);
588         break;
589       }
590       case TextureManager::LoadState::LOAD_FINISHED:
591       {
592         // Loading has already completed.
593         if(observer && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
594         {
595           LoadOrQueueTexture(textureInfo, observer);
596         }
597         break;
598       }
599     }
600   }
601   else
602   {
603     // If the image is already finished to load, use cached texture.
604     // We don't need to consider Observer because this is synchronous loading.
605     if(textureInfo.loadState == TextureManager::LoadState::UPLOADED ||
606        textureInfo.loadState == TextureManager::LoadState::LOAD_FINISHED)
607     {
608       return textureId;
609     }
610     else
611     {
612       Devel::PixelBuffer pixelBuffer = LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection);
613
614       if(!pixelBuffer)
615       {
616         // If pixelBuffer loading is failed in synchronously, call Remove() method.
617         Remove(textureId, nullptr);
618         return INVALID_TEXTURE_ID;
619       }
620
621       if(storageType == StorageType::KEEP_PIXEL_BUFFER) // For the mask image loading.
622       {
623         textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
624         textureInfo.loadState   = LoadState::LOAD_FINISHED;
625       }
626       else // For the image loading.
627       {
628         if(maskTextureId != INVALID_TEXTURE_ID)
629         {
630           TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
631           if(maskCacheIndex != INVALID_CACHE_INDEX)
632           {
633             Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
634             if(maskPixelBuffer)
635             {
636               pixelBuffer.ApplyMask(maskPixelBuffer, contentScale, cropToMask);
637             }
638           }
639           else
640           {
641             DALI_LOG_ERROR("Mask image is not stored in cache.\n");
642           }
643         }
644         PreMultiply(pixelBuffer, preMultiplyOnLoad);
645
646         // Upload texture
647         UploadTexture(pixelBuffer, textureInfo);
648       }
649     }
650   }
651
652   // Return the TextureId for which this Texture can now be referenced by externally.
653   return textureId;
654 }
655
656 void TextureManager::Remove(const TextureManager::TextureId& textureId, TextureUploadObserver* observer)
657 {
658   // Remove textureId in CacheManager.
659   mTextureCacheManager.RemoveCache(textureId);
660
661   if(observer)
662   {
663     // Remove element from the LoadQueue
664     for(auto&& element : mLoadQueue)
665     {
666       if(element.mObserver == observer)
667       {
668         // Do not erase the item. We will clear it later in ProcessQueuedTextures().
669         element.mObserver = nullptr;
670         break;
671       }
672     }
673   }
674 }
675
676 Devel::PixelBuffer TextureManager::LoadImageSynchronously(
677   const VisualUrl&                url,
678   const Dali::ImageDimensions&    desiredSize,
679   const Dali::FittingMode::Type&  fittingMode,
680   const Dali::SamplingMode::Type& samplingMode,
681   const bool&                     orientationCorrection)
682 {
683   Devel::PixelBuffer pixelBuffer;
684   if(url.IsBufferResource())
685   {
686     const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url.GetUrl());
687     if(encodedImageBuffer)
688     {
689       pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
690     }
691   }
692   else
693   {
694     pixelBuffer = LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
695   }
696   return pixelBuffer;
697 }
698
699 void TextureManager::AddObserver(TextureManager::LifecycleObserver& observer)
700 {
701   // make sure an observer doesn't observe the same object twice
702   // otherwise it will get multiple calls to ObjectDestroyed()
703   DALI_ASSERT_DEBUG(mLifecycleObservers.End() == std::find(mLifecycleObservers.Begin(), mLifecycleObservers.End(), &observer));
704   mLifecycleObservers.PushBack(&observer);
705 }
706
707 void TextureManager::RemoveObserver(TextureManager::LifecycleObserver& observer)
708 {
709   // Find the observer...
710   auto endIter = mLifecycleObservers.End();
711   for(auto iter = mLifecycleObservers.Begin(); iter != endIter; ++iter)
712   {
713     if((*iter) == &observer)
714     {
715       mLifecycleObservers.Erase(iter);
716       break;
717     }
718   }
719   DALI_ASSERT_DEBUG(endIter != mLifecycleObservers.End());
720 }
721
722 void TextureManager::LoadOrQueueTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
723 {
724   switch(textureInfo.loadState)
725   {
726     case LoadState::NOT_STARTED:
727     case LoadState::LOAD_FAILED:
728     {
729       if(mQueueLoadFlag)
730       {
731         QueueLoadTexture(textureInfo, observer);
732       }
733       else
734       {
735         LoadTexture(textureInfo, observer);
736       }
737       break;
738     }
739     case LoadState::UPLOADED:
740     {
741       if(mQueueLoadFlag)
742       {
743         QueueLoadTexture(textureInfo, observer);
744       }
745       else
746       {
747         // The Texture has already loaded. The other observers have already been notified.
748         // We need to send a "late" loaded notification for this observer.
749         observer->LoadComplete(true, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::TEXTURE, textureInfo.textureId, textureInfo.textureSet, (textureInfo.useAtlas == UseAtlas::USE_ATLAS) ? true : false, textureInfo.atlasRect, textureInfo.preMultiplied));
750       }
751       break;
752     }
753     case LoadState::LOADING:
754     case LoadState::CANCELLED:
755     case LoadState::LOAD_FINISHED:
756     case LoadState::WAITING_FOR_MASK:
757     case LoadState::MASK_APPLYING:
758     case LoadState::MASK_APPLIED:
759     {
760       break;
761     }
762   }
763 }
764
765 void TextureManager::QueueLoadTexture(const TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
766 {
767   const auto& textureId = textureInfo.textureId;
768   mLoadQueue.PushBack(LoadQueueElement(textureId, observer));
769
770   observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
771 }
772
773 void TextureManager::LoadTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
774 {
775   DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::LoadTexture(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
776
777   textureInfo.loadState = LoadState::LOADING;
778   if(!textureInfo.loadSynchronously)
779   {
780     auto& loadersContainer  = (textureInfo.url.IsLocalResource() || textureInfo.url.IsBufferResource()) ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
781     auto  loadingHelperIt   = loadersContainer.GetNext();
782     auto  premultiplyOnLoad = (textureInfo.preMultiplyOnLoad && textureInfo.maskTextureId == INVALID_TEXTURE_ID) ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
783     DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
784     if(textureInfo.animatedImageLoading)
785     {
786       loadingHelperIt->LoadAnimatedImage(textureInfo.textureId, textureInfo.animatedImageLoading, textureInfo.frameIndex);
787     }
788     else
789     {
790       loadingHelperIt->Load(textureInfo.textureId, textureInfo.url, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, textureInfo.orientationCorrection, premultiplyOnLoad);
791     }
792   }
793   ObserveTexture(textureInfo, observer);
794 }
795
796 void TextureManager::ProcessQueuedTextures()
797 {
798   for(auto&& element : mLoadQueue)
799   {
800     if(!element.mObserver)
801     {
802       continue;
803     }
804
805     TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(element.mTextureId);
806     if(cacheIndex != INVALID_CACHE_INDEX)
807     {
808       TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
809       if(textureInfo.loadState == LoadState::UPLOADED)
810       {
811         element.mObserver->LoadComplete(true, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::TEXTURE, textureInfo.textureId, textureInfo.textureSet, (textureInfo.useAtlas == UseAtlas::USE_ATLAS) ? true : false, textureInfo.atlasRect, textureInfo.preMultiplied));
812       }
813       else if(textureInfo.loadState == LoadState::LOAD_FINISHED && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
814       {
815         element.mObserver->LoadComplete(true, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::PIXEL_BUFFER, textureInfo.pixelBuffer, textureInfo.url.GetUrl(), textureInfo.preMultiplied));
816       }
817       else
818       {
819         LoadTexture(textureInfo, element.mObserver);
820       }
821     }
822   }
823   mLoadQueue.Clear();
824 }
825
826 void TextureManager::ObserveTexture(TextureManager::TextureInfo& textureInfo,
827                                     TextureUploadObserver*       observer)
828 {
829   DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ObserveTexture(): url:%s observer:%p\n", textureInfo.url.GetUrl().c_str(), observer);
830
831   if(observer)
832   {
833     textureInfo.observerList.PushBack(observer);
834     observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
835   }
836 }
837
838 void TextureManager::AsyncLoadComplete(const TextureManager::TextureId& textureId, Devel::PixelBuffer pixelBuffer)
839 {
840   DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::AsyncLoadComplete( textureId:%d )\n", textureId);
841   TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
842   if(cacheIndex != INVALID_CACHE_INDEX)
843   {
844     TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
845
846     DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "  textureId:%d Url:%s CacheIndex:%d LoadState: %s\n", textureInfo.textureId, textureInfo.url.GetUrl().c_str(), cacheIndex, GET_LOAD_STATE_STRING(textureInfo.loadState));
847
848     if(textureInfo.loadState != LoadState::CANCELLED)
849     {
850       // textureInfo can be invalidated after this call (as the mTextureInfoContainer may be modified)
851       PostLoad(textureInfo, pixelBuffer);
852     }
853     else
854     {
855       Remove(textureInfo.textureId, nullptr);
856     }
857   }
858 }
859
860 void TextureManager::PostLoad(TextureManager::TextureInfo& textureInfo, Devel::PixelBuffer& pixelBuffer)
861 {
862   // Was the load successful?
863   if(pixelBuffer && (pixelBuffer.GetWidth() != 0) && (pixelBuffer.GetHeight() != 0))
864   {
865     // No atlas support for now
866     textureInfo.useAtlas      = UseAtlas::NO_ATLAS;
867     textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
868
869     if(textureInfo.storageType == StorageType::UPLOAD_TO_TEXTURE)
870     {
871       // If there is a mask texture ID associated with this texture, then apply the mask
872       // if it's already loaded. If it hasn't, and the mask is still loading,
873       // wait for the mask to finish loading.
874       // note, If the texture is already uploaded synchronously during loading,
875       // we don't need to apply mask.
876       if(textureInfo.loadState != LoadState::UPLOADED &&
877          textureInfo.maskTextureId != INVALID_TEXTURE_ID)
878       {
879         if(textureInfo.loadState == LoadState::MASK_APPLYING)
880         {
881           textureInfo.loadState = LoadState::MASK_APPLIED;
882           UploadTexture(pixelBuffer, textureInfo);
883           NotifyObservers(textureInfo, true);
884         }
885         else
886         {
887           LoadState maskLoadState = mTextureCacheManager.GetTextureStateInternal(textureInfo.maskTextureId);
888           textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
889           if(maskLoadState == LoadState::LOADING)
890           {
891             textureInfo.loadState = LoadState::WAITING_FOR_MASK;
892           }
893           else if(maskLoadState == LoadState::LOAD_FINISHED)
894           {
895             // Send New Task to Thread
896             ApplyMask(textureInfo, textureInfo.maskTextureId);
897           }
898         }
899       }
900       else
901       {
902         UploadTexture(pixelBuffer, textureInfo);
903         NotifyObservers(textureInfo, true);
904       }
905     }
906     else
907     {
908       textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
909       textureInfo.loadState   = LoadState::LOAD_FINISHED;
910
911       if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
912       {
913         NotifyObservers(textureInfo, true);
914       }
915       else
916       {
917         // Check if there was another texture waiting for this load to complete
918         // (e.g. if this was an image mask, and its load is on a different thread)
919         CheckForWaitingTexture(textureInfo);
920       }
921     }
922   }
923   else
924   {
925     textureInfo.loadState = LoadState::LOAD_FAILED;
926     CheckForWaitingTexture(textureInfo);
927     NotifyObservers(textureInfo, false);
928   }
929 }
930
931 void TextureManager::CheckForWaitingTexture(TextureManager::TextureInfo& maskTextureInfo)
932 {
933   // Search the cache, checking if any texture has this texture id as a
934   // maskTextureId:
935   const TextureCacheIndex size = static_cast<TextureCacheIndex>(mTextureCacheManager.size());
936
937   for(TextureCacheIndex cacheIndex = 0; cacheIndex < size; ++cacheIndex)
938   {
939     if(mTextureCacheManager[cacheIndex].maskTextureId == maskTextureInfo.textureId &&
940        mTextureCacheManager[cacheIndex].loadState == LoadState::WAITING_FOR_MASK)
941     {
942       TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
943
944       if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED)
945       {
946         // Send New Task to Thread
947         ApplyMask(textureInfo, maskTextureInfo.textureId);
948       }
949       else
950       {
951         textureInfo.pixelBuffer.Reset();
952         textureInfo.loadState = LoadState::LOAD_FAILED;
953         NotifyObservers(textureInfo, false);
954       }
955     }
956   }
957 }
958
959 void TextureManager::ApplyMask(TextureManager::TextureInfo& textureInfo, const TextureManager::TextureId& maskTextureId)
960 {
961   TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
962   if(maskCacheIndex != INVALID_CACHE_INDEX)
963   {
964     Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
965     Devel::PixelBuffer pixelBuffer     = textureInfo.pixelBuffer;
966     textureInfo.pixelBuffer.Reset();
967
968     DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ApplyMask(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
969
970     textureInfo.loadState   = LoadState::MASK_APPLYING;
971     auto& loadersContainer  = (textureInfo.url.IsLocalResource() || textureInfo.url.IsBufferResource()) ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
972     auto  loadingHelperIt   = loadersContainer.GetNext();
973     auto  premultiplyOnLoad = textureInfo.preMultiplyOnLoad ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
974     DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
975     loadingHelperIt->ApplyMask(textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask, premultiplyOnLoad);
976   }
977 }
978
979 void TextureManager::UploadTexture(Devel::PixelBuffer& pixelBuffer, TextureManager::TextureInfo& textureInfo)
980 {
981   if(textureInfo.loadState != LoadState::UPLOADED && textureInfo.useAtlas != UseAtlas::USE_ATLAS)
982   {
983     DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "  TextureManager::UploadTexture() New Texture for textureId:%d\n", textureInfo.textureId);
984
985     // Check if this pixelBuffer is premultiplied
986     textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
987
988     auto& renderingAddOn = RenderingAddOn::Get();
989     if(renderingAddOn.IsValid())
990     {
991       renderingAddOn.CreateGeometry(textureInfo.textureId, pixelBuffer);
992     }
993
994     Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight());
995
996     PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
997     texture.Upload(pixelData);
998     if(!textureInfo.textureSet)
999     {
1000       textureInfo.textureSet = TextureSet::New();
1001     }
1002     textureInfo.textureSet.SetTexture(0u, texture);
1003   }
1004
1005   // Update the load state.
1006   // Note: This is regardless of success as we care about whether a
1007   // load attempt is in progress or not.  If unsuccessful, a broken
1008   // image is still loaded.
1009   textureInfo.loadState = LoadState::UPLOADED;
1010 }
1011
1012 void TextureManager::NotifyObservers(TextureManager::TextureInfo& textureInfo, const bool& success)
1013 {
1014   TextureId textureId = textureInfo.textureId;
1015
1016   // If there is an observer: Notify the load is complete, whether successful or not,
1017   // and erase it from the list
1018   TextureInfo* info = &textureInfo;
1019
1020   mQueueLoadFlag = true;
1021
1022   while(info->observerList.Count())
1023   {
1024     TextureUploadObserver* observer = info->observerList[0];
1025
1026     // During LoadComplete() a Control ResourceReady() signal is emitted.
1027     // During that signal the app may add remove /add Textures (e.g. via
1028     // ImageViews).
1029     // It is possible for observers to be removed from the observer list,
1030     // and it is also possible for the mTextureInfoContainer to be modified,
1031     // invalidating the reference to the textureInfo struct.
1032     // Texture load requests for the same URL are deferred until the end of this
1033     // method.
1034     DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::NotifyObservers() textureId:%d url:%s loadState:%s\n", textureId, textureInfo.url.GetUrl().c_str(), GET_LOAD_STATE_STRING(textureInfo.loadState));
1035
1036     // It is possible for the observer to be deleted.
1037     // Disconnect and remove the observer first.
1038     observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1039
1040     info->observerList.Erase(info->observerList.Begin());
1041
1042     if(info->storageType == StorageType::RETURN_PIXEL_BUFFER)
1043     {
1044       observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::PIXEL_BUFFER, info->pixelBuffer, info->url.GetUrl(), info->preMultiplied));
1045     }
1046     else
1047     {
1048       observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::TEXTURE, info->textureId, info->textureSet, (info->useAtlas == UseAtlas::USE_ATLAS) ? true : false, info->atlasRect, info->preMultiplied));
1049     }
1050
1051     // Get the textureInfo from the container again as it may have been invalidated.
1052     TextureCacheIndex textureInfoIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1053     if(textureInfoIndex == INVALID_CACHE_INDEX)
1054     {
1055       break; // texture has been removed - can stop.
1056     }
1057     info = &mTextureCacheManager[textureInfoIndex];
1058   }
1059
1060   mQueueLoadFlag = false;
1061   ProcessQueuedTextures();
1062
1063   if(info->storageType == StorageType::RETURN_PIXEL_BUFFER && info->observerList.Count() == 0)
1064   {
1065     Remove(info->textureId, nullptr);
1066   }
1067 }
1068
1069 void TextureManager::ObserverDestroyed(TextureUploadObserver* observer)
1070 {
1071   const TextureCacheIndex count = static_cast<TextureCacheIndex>(mTextureCacheManager.size());
1072   for(TextureCacheIndex i = 0; i < count; ++i)
1073   {
1074     TextureInfo& textureInfo(mTextureCacheManager[i]);
1075     for(TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin();
1076         j != textureInfo.observerList.End();)
1077     {
1078       if(*j == observer)
1079       {
1080         j = textureInfo.observerList.Erase(j);
1081       }
1082       else
1083       {
1084         ++j;
1085       }
1086     }
1087   }
1088
1089   // Remove element from the LoadQueue
1090   for(auto&& element : mLoadQueue)
1091   {
1092     if(element.mObserver == observer)
1093     {
1094       element.mObserver = nullptr;
1095     }
1096   }
1097 }
1098
1099 Dali::Geometry TextureManager::GetRenderGeometry(const TextureManager::TextureId& textureId, std::uint32_t& frontElements, std::uint32_t& backElements)
1100 {
1101   return RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().GetGeometry(textureId, frontElements, backElements) : Geometry();
1102 }
1103
1104 } // namespace Internal
1105
1106 } // namespace Toolkit
1107
1108 } // namespace Dali