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