Generate url and load texture from encoded image buffer
[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         // Insert this buffer at mTextureInfoContainer.
473         // This buffer will decode at ImageLoaderThread.
474         bool preMultiply = (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
475         mTextureInfoContainer.push_back(TextureInfo(textureId, maskTextureId, url, desiredSize, contentScale, fittingMode, samplingMode, false, cropToMask, useAtlas, textureHash, orientationCorrection, preMultiply, animatedImageLoading, frameIndex));
476         cacheIndex = mTextureInfoContainer.size() - 1u;
477
478         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);
479       }
480     }
481   }
482
483   if(textureId == INVALID_TEXTURE_ID) // There was no caching, or caching not required
484   {
485     // We need a new Texture.
486     textureId        = GenerateUniqueTextureId();
487     bool preMultiply = (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
488     mTextureInfoContainer.push_back(TextureInfo(textureId, maskTextureId, url, desiredSize, contentScale, fittingMode, samplingMode, false, cropToMask, useAtlas, textureHash, orientationCorrection, preMultiply, animatedImageLoading, frameIndex));
489     cacheIndex = mTextureInfoContainer.size() - 1u;
490
491     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);
492   }
493
494   // The below code path is common whether we are using the cache or not.
495   // The textureInfoIndex now refers to either a pre-existing cached TextureInfo,
496   // or a new TextureInfo just created.
497   TextureInfo& textureInfo(mTextureInfoContainer[cacheIndex]);
498   textureInfo.maskTextureId         = maskTextureId;
499   textureInfo.storageType           = storageType;
500   textureInfo.orientationCorrection = orientationCorrection;
501
502   DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureInfo loadState:%s\n", GET_LOAD_STATE_STRING(textureInfo.loadState));
503
504   // Force reloading of texture by setting loadState unless already loading or cancelled.
505   if(TextureManager::ReloadPolicy::FORCED == reloadPolicy &&
506      TextureManager::LoadState::LOADING != textureInfo.loadState &&
507      TextureManager::LoadState::WAITING_FOR_MASK != textureInfo.loadState &&
508      TextureManager::LoadState::MASK_APPLYING != textureInfo.loadState &&
509      TextureManager::LoadState::MASK_APPLIED != textureInfo.loadState &&
510      TextureManager::LoadState::CANCELLED != textureInfo.loadState)
511   {
512     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);
513
514     textureInfo.loadState = TextureManager::LoadState::NOT_STARTED;
515   }
516
517   // Check if we should add the observer.
518   // Only do this if we have not loaded yet and it will not have loaded by the end of this method.
519   switch(textureInfo.loadState)
520   {
521     case TextureManager::LoadState::LOAD_FAILED: // Failed notifies observer which then stops observing.
522     case TextureManager::LoadState::NOT_STARTED:
523     {
524       LoadOrQueueTexture(textureInfo, observer); // If called inside NotifyObservers, queues until afterwards
525       break;
526     }
527     case TextureManager::LoadState::LOADING:
528     case TextureManager::LoadState::WAITING_FOR_MASK:
529     case TextureManager::LoadState::MASK_APPLYING:
530     case TextureManager::LoadState::MASK_APPLIED:
531     {
532       ObserveTexture(textureInfo, observer);
533       break;
534     }
535     case TextureManager::LoadState::UPLOADED:
536     {
537       if(observer)
538       {
539         LoadOrQueueTexture(textureInfo, observer);
540       }
541       break;
542     }
543     case TextureManager::LoadState::CANCELLED:
544     {
545       // A cancelled texture hasn't finished loading yet. Treat as a loading texture
546       // (it's ref count has already been incremented, above)
547       textureInfo.loadState = TextureManager::LoadState::LOADING;
548       ObserveTexture(textureInfo, observer);
549       break;
550     }
551     case TextureManager::LoadState::LOAD_FINISHED:
552     {
553       // Loading has already completed.
554       if(observer && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
555       {
556         LoadOrQueueTexture(textureInfo, observer);
557       }
558       break;
559     }
560   }
561
562   // Return the TextureId for which this Texture can now be referenced by externally.
563   return textureId;
564 }
565
566 void TextureManager::Remove(const TextureManager::TextureId textureId, TextureUploadObserver* observer)
567 {
568   int textureInfoIndex = GetCacheIndexFromId(textureId);
569
570   if(textureInfoIndex != INVALID_INDEX)
571   {
572     TextureInfo& textureInfo(mTextureInfoContainer[textureInfoIndex]);
573
574     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);
575
576     // Decrement the reference count and check if this is the last user of this Texture.
577     if(--textureInfo.referenceCount <= 0)
578     {
579       // This is the last remove for this Texture.
580       textureInfo.referenceCount = 0;
581       bool removeTextureInfo     = false;
582
583       // If loaded, we can remove the TextureInfo and the Atlas (if atlased).
584       if(textureInfo.loadState == LoadState::UPLOADED)
585       {
586         if(textureInfo.atlas)
587         {
588           textureInfo.atlas.Remove(textureInfo.atlasRect);
589         }
590         removeTextureInfo = true;
591       }
592       else if(textureInfo.loadState == LoadState::LOADING)
593       {
594         // We mark the textureInfo for removal.
595         // Once the load has completed, this method will be called again.
596         textureInfo.loadState = LoadState::CANCELLED;
597       }
598       else
599       {
600         // In other states, we are not waiting for a load so we are safe to remove the TextureInfo data.
601         removeTextureInfo = true;
602       }
603
604       // If the state allows us to remove the TextureInfo data, we do so.
605       if(removeTextureInfo)
606       {
607         // Permanently remove the textureInfo struct.
608         mTextureInfoContainer.erase(mTextureInfoContainer.begin() + textureInfoIndex);
609       }
610     }
611
612     if(observer)
613     {
614       // Remove element from the LoadQueue
615       for(auto&& element : mLoadQueue)
616       {
617         if(element.mObserver == observer)
618         {
619           // Do not erase the item. We will clear it later in ProcessQueuedTextures().
620           element.mObserver = nullptr;
621           break;
622         }
623       }
624     }
625   }
626 }
627
628 VisualUrl TextureManager::GetVisualUrl(TextureId textureId)
629 {
630   VisualUrl visualUrl("");
631   int       cacheIndex = GetCacheIndexFromId(textureId);
632
633   if(cacheIndex != INVALID_CACHE_INDEX)
634   {
635     DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::GetVisualUrl. Using cached texture id=%d, textureId=%d\n", cacheIndex, textureId);
636
637     TextureInfo& cachedTextureInfo(mTextureInfoContainer[cacheIndex]);
638     visualUrl = cachedTextureInfo.url;
639   }
640   return visualUrl;
641 }
642
643 TextureManager::LoadState TextureManager::GetTextureState(TextureId textureId)
644 {
645   LoadState loadState = TextureManager::LoadState::NOT_STARTED;
646
647   int cacheIndex = GetCacheIndexFromId(textureId);
648   if(cacheIndex != INVALID_CACHE_INDEX)
649   {
650     TextureInfo& cachedTextureInfo(mTextureInfoContainer[cacheIndex]);
651     loadState = cachedTextureInfo.loadState;
652   }
653   else
654   {
655     for(auto&& elem : mExternalTextures)
656     {
657       if(elem.textureId == textureId)
658       {
659         loadState = LoadState::UPLOADED;
660         break;
661       }
662     }
663   }
664   return loadState;
665 }
666
667 TextureManager::LoadState TextureManager::GetTextureStateInternal(TextureId textureId)
668 {
669   LoadState loadState = TextureManager::LoadState::NOT_STARTED;
670
671   int cacheIndex = GetCacheIndexFromId(textureId);
672   if(cacheIndex != INVALID_CACHE_INDEX)
673   {
674     TextureInfo& cachedTextureInfo(mTextureInfoContainer[cacheIndex]);
675     loadState = cachedTextureInfo.loadState;
676   }
677
678   return loadState;
679 }
680
681 TextureSet TextureManager::GetTextureSet(TextureId textureId)
682 {
683   TextureSet textureSet; // empty handle
684
685   int cacheIndex = GetCacheIndexFromId(textureId);
686   if(cacheIndex != INVALID_CACHE_INDEX)
687   {
688     TextureInfo& cachedTextureInfo(mTextureInfoContainer[cacheIndex]);
689     textureSet = cachedTextureInfo.textureSet;
690   }
691   else
692   {
693     for(auto&& elem : mExternalTextures)
694     {
695       if(elem.textureId == textureId)
696       {
697         textureSet = elem.textureSet;
698         break;
699       }
700     }
701   }
702   return textureSet;
703 }
704
705 EncodedImageBuffer TextureManager::GetEncodedImageBuffer(TextureId textureId)
706 {
707   EncodedImageBuffer encodedImageBuffer; // empty handle
708   for(auto&& elem : mEncodedBufferTextures)
709   {
710     if(elem.textureId == textureId)
711     {
712       encodedImageBuffer = elem.encodedImageBuffer;
713       break;
714     }
715   }
716   return encodedImageBuffer;
717 }
718
719 EncodedImageBuffer TextureManager::GetEncodedImageBuffer(const std::string& url)
720 {
721   EncodedImageBuffer encodedImageBuffer; // empty handle
722   if(url.size() > 0 && VisualUrl::BUFFER == VisualUrl::GetProtocolType(url))
723   {
724     std::string location = VisualUrl::GetLocation(url);
725     if(location.size() > 0u)
726     {
727       TextureId targetId = std::stoi(location);
728       return GetEncodedImageBuffer(targetId);
729     }
730   }
731   return encodedImageBuffer;
732 }
733
734 std::string TextureManager::AddExternalTexture(TextureSet& textureSet)
735 {
736   TextureManager::ExternalTextureInfo info;
737   info.textureId  = GenerateUniqueTextureId();
738   info.textureSet = textureSet;
739   mExternalTextures.emplace_back(info);
740
741   return VisualUrl::CreateTextureUrl(std::to_string(info.textureId));
742 }
743
744 std::string TextureManager::AddExternalEncodedImageBuffer(const EncodedImageBuffer& encodedImageBuffer)
745 {
746   // Duplication check
747   for(auto&& elem : mEncodedBufferTextures)
748   {
749     if(elem.encodedImageBuffer == encodedImageBuffer)
750     {
751       // If same buffer added, increase reference count and return.
752       elem.referenceCount++;
753       return VisualUrl::CreateBufferUrl(std::to_string(elem.textureId));;
754     }
755   }
756   TextureManager::EncodedBufferTextureInfo info(GenerateUniqueTextureId(), encodedImageBuffer);
757   mEncodedBufferTextures.emplace_back(info);
758   return VisualUrl::CreateBufferUrl(std::to_string(info.textureId));
759 }
760
761 TextureSet TextureManager::RemoveExternalTexture(const std::string& url)
762 {
763   if(url.size() > 0u)
764   {
765     if(VisualUrl::TEXTURE == VisualUrl::GetProtocolType(url))
766     {
767       // get the location from the Url
768       std::string location = VisualUrl::GetLocation(url);
769       if(location.size() > 0u)
770       {
771         TextureId  id  = std::stoi(location);
772         const auto end = mExternalTextures.end();
773         for(auto iter = mExternalTextures.begin(); iter != end; ++iter)
774         {
775           if(iter->textureId == id)
776           {
777             auto textureSet = iter->textureSet;
778             if(--(iter->referenceCount) <= 0)
779             {
780               mExternalTextures.erase(iter);
781             }
782             return textureSet;
783           }
784         }
785       }
786     }
787   }
788   return TextureSet();
789 }
790
791 EncodedImageBuffer TextureManager::RemoveExternalEncodedImageBuffer(const std::string& url)
792 {
793   if(url.size() > 0u)
794   {
795     if(VisualUrl::BUFFER == VisualUrl::GetProtocolType(url))
796     {
797       // get the location from the Url
798       std::string location = VisualUrl::GetLocation(url);
799       if(location.size() > 0u)
800       {
801         TextureId id = std::stoi(location);
802         const auto end = mEncodedBufferTextures.end();
803         for(auto iter = mEncodedBufferTextures.begin(); iter != end; ++iter)
804         {
805           if(iter->textureId == id)
806           {
807             auto encodedImageBuffer = iter->encodedImageBuffer;
808             if(--(iter->referenceCount) <= 0)
809             {
810               mEncodedBufferTextures.erase(iter);
811             }
812             return encodedImageBuffer;
813           }
814         }
815       }
816     }
817   }
818   return EncodedImageBuffer();
819 }
820
821 void TextureManager::UseExternalResource(const VisualUrl& url)
822 {
823   if(VisualUrl::TEXTURE == url.GetProtocolType())
824   {
825     std::string location = url.GetLocation();
826     if(location.size() > 0u)
827     {
828       TextureId id = std::stoi(location);
829       for(auto&& elem : mExternalTextures)
830       {
831         if(elem.textureId == id)
832         {
833           elem.referenceCount++;
834           return;
835         }
836       }
837     }
838   }
839   else if(VisualUrl::BUFFER == url.GetProtocolType())
840   {
841     std::string location = url.GetLocation();
842     if(location.size() > 0u)
843     {
844       TextureId id = std::stoi(location);
845       for(auto&& elem : mEncodedBufferTextures)
846       {
847         if(elem.textureId == id)
848         {
849           elem.referenceCount++;
850           return;
851         }
852       }
853     }
854   }
855 }
856
857 void TextureManager::AddObserver(TextureManager::LifecycleObserver& observer)
858 {
859   // make sure an observer doesn't observe the same object twice
860   // otherwise it will get multiple calls to ObjectDestroyed()
861   DALI_ASSERT_DEBUG(mLifecycleObservers.End() == std::find(mLifecycleObservers.Begin(), mLifecycleObservers.End(), &observer));
862   mLifecycleObservers.PushBack(&observer);
863 }
864
865 void TextureManager::RemoveObserver(TextureManager::LifecycleObserver& observer)
866 {
867   // Find the observer...
868   auto endIter = mLifecycleObservers.End();
869   for(auto iter = mLifecycleObservers.Begin(); iter != endIter; ++iter)
870   {
871     if((*iter) == &observer)
872     {
873       mLifecycleObservers.Erase(iter);
874       break;
875     }
876   }
877   DALI_ASSERT_DEBUG(endIter != mLifecycleObservers.End());
878 }
879
880 void TextureManager::LoadOrQueueTexture(TextureInfo& textureInfo, TextureUploadObserver* observer)
881 {
882   switch(textureInfo.loadState)
883   {
884     case LoadState::NOT_STARTED:
885     case LoadState::LOAD_FAILED:
886     {
887       if(mQueueLoadFlag)
888       {
889         QueueLoadTexture(textureInfo, observer);
890       }
891       else
892       {
893         LoadTexture(textureInfo, observer);
894       }
895       break;
896     }
897     case LoadState::UPLOADED:
898     {
899       if(mQueueLoadFlag)
900       {
901         QueueLoadTexture(textureInfo, observer);
902       }
903       else
904       {
905         // The Texture has already loaded. The other observers have already been notified.
906         // We need to send a "late" loaded notification for this observer.
907         observer->UploadComplete(true, textureInfo.textureId, textureInfo.textureSet, textureInfo.useAtlas, textureInfo.atlasRect, textureInfo.preMultiplied);
908       }
909       break;
910     }
911     case LoadState::LOADING:
912     case LoadState::CANCELLED:
913     case LoadState::LOAD_FINISHED:
914     case LoadState::WAITING_FOR_MASK:
915     case LoadState::MASK_APPLYING:
916     case LoadState::MASK_APPLIED:
917     {
918       break;
919     }
920   }
921 }
922
923 void TextureManager::QueueLoadTexture(TextureInfo& textureInfo, TextureUploadObserver* observer)
924 {
925   auto textureId = textureInfo.textureId;
926   mLoadQueue.PushBack(LoadQueueElement(textureId, observer));
927
928   observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
929 }
930
931 void TextureManager::LoadTexture(TextureInfo& textureInfo, TextureUploadObserver* observer)
932 {
933   DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::LoadTexture(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
934
935   textureInfo.loadState = LoadState::LOADING;
936   if(!textureInfo.loadSynchronously)
937   {
938     auto& loadersContainer  = (textureInfo.url.IsLocalResource() || textureInfo.url.IsBufferResource()) ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
939     auto  loadingHelperIt   = loadersContainer.GetNext();
940     auto  premultiplyOnLoad = (textureInfo.preMultiplyOnLoad && textureInfo.maskTextureId == INVALID_TEXTURE_ID) ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
941     DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
942     if(textureInfo.animatedImageLoading)
943     {
944       loadingHelperIt->LoadAnimatedImage(textureInfo.textureId, textureInfo.animatedImageLoading, textureInfo.frameIndex);
945     }
946     else
947     {
948       loadingHelperIt->Load(textureInfo.textureId, textureInfo.url, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, textureInfo.orientationCorrection, premultiplyOnLoad);
949     }
950   }
951   ObserveTexture(textureInfo, observer);
952 }
953
954 void TextureManager::ProcessQueuedTextures()
955 {
956   for(auto&& element : mLoadQueue)
957   {
958     if(!element.mObserver)
959     {
960       continue;
961     }
962
963     int cacheIndex = GetCacheIndexFromId(element.mTextureId);
964     if(cacheIndex != INVALID_CACHE_INDEX)
965     {
966       TextureInfo& textureInfo(mTextureInfoContainer[cacheIndex]);
967       if(textureInfo.loadState == LoadState::UPLOADED)
968       {
969         element.mObserver->UploadComplete(true, textureInfo.textureId, textureInfo.textureSet, textureInfo.useAtlas, textureInfo.atlasRect, textureInfo.preMultiplied);
970       }
971       else if(textureInfo.loadState == LoadState::LOAD_FINISHED && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
972       {
973         element.mObserver->LoadComplete(true, textureInfo.pixelBuffer, textureInfo.url, textureInfo.preMultiplied);
974       }
975       else
976       {
977         LoadTexture(textureInfo, element.mObserver);
978       }
979     }
980   }
981   mLoadQueue.Clear();
982 }
983
984 void TextureManager::ObserveTexture(TextureInfo&           textureInfo,
985                                     TextureUploadObserver* observer)
986 {
987   DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ObserveTexture(): url:%s observer:%p\n", textureInfo.url.GetUrl().c_str(), observer);
988
989   if(observer)
990   {
991     textureInfo.observerList.PushBack(observer);
992     observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
993   }
994 }
995
996 void TextureManager::AsyncLoadComplete(AsyncLoadingInfoContainerType& loadingContainer, uint32_t id, Devel::PixelBuffer pixelBuffer)
997 {
998   DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::AsyncLoadComplete( id:%d )\n", id);
999
1000   if(loadingContainer.size() >= 1u)
1001   {
1002     AsyncLoadingInfo loadingInfo = loadingContainer.front();
1003
1004     if(loadingInfo.loadId == id)
1005     {
1006       int cacheIndex = GetCacheIndexFromId(loadingInfo.textureId);
1007       if(cacheIndex != INVALID_CACHE_INDEX)
1008       {
1009         TextureInfo& textureInfo(mTextureInfoContainer[cacheIndex]);
1010
1011         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);
1012
1013         if(textureInfo.loadState != LoadState::CANCELLED)
1014         {
1015           // textureInfo can be invalidated after this call (as the mTextureInfoContainer may be modified)
1016           PostLoad(textureInfo, pixelBuffer);
1017         }
1018         else
1019         {
1020           Remove(textureInfo.textureId, nullptr);
1021         }
1022       }
1023     }
1024
1025     loadingContainer.pop_front();
1026   }
1027 }
1028
1029 void TextureManager::PostLoad(TextureInfo& textureInfo, Devel::PixelBuffer& pixelBuffer)
1030 {
1031   // Was the load successful?
1032   if(pixelBuffer && (pixelBuffer.GetWidth() != 0) && (pixelBuffer.GetHeight() != 0))
1033   {
1034     // No atlas support for now
1035     textureInfo.useAtlas      = NO_ATLAS;
1036     textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
1037
1038     if(textureInfo.storageType == StorageType::UPLOAD_TO_TEXTURE)
1039     {
1040       // If there is a mask texture ID associated with this texture, then apply the mask
1041       // if it's already loaded. If it hasn't, and the mask is still loading,
1042       // wait for the mask to finish loading.
1043       if(textureInfo.maskTextureId != INVALID_TEXTURE_ID)
1044       {
1045         if(textureInfo.loadState == LoadState::MASK_APPLYING)
1046         {
1047           textureInfo.loadState = LoadState::MASK_APPLIED;
1048           UploadTexture(pixelBuffer, textureInfo);
1049           NotifyObservers(textureInfo, true);
1050         }
1051         else
1052         {
1053           LoadState maskLoadState = GetTextureStateInternal(textureInfo.maskTextureId);
1054           textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
1055           if(maskLoadState == LoadState::LOADING)
1056           {
1057             textureInfo.loadState = LoadState::WAITING_FOR_MASK;
1058           }
1059           else if(maskLoadState == LoadState::LOAD_FINISHED)
1060           {
1061             // Send New Task to Thread
1062             ApplyMask(textureInfo, textureInfo.maskTextureId);
1063           }
1064         }
1065       }
1066       else
1067       {
1068         UploadTexture(pixelBuffer, textureInfo);
1069         NotifyObservers(textureInfo, true);
1070       }
1071     }
1072     else
1073     {
1074       textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
1075       textureInfo.loadState   = LoadState::LOAD_FINISHED;
1076
1077       if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1078       {
1079         NotifyObservers(textureInfo, true);
1080       }
1081       else
1082       {
1083         // Check if there was another texture waiting for this load to complete
1084         // (e.g. if this was an image mask, and its load is on a different thread)
1085         CheckForWaitingTexture(textureInfo);
1086       }
1087     }
1088   }
1089   else
1090   {
1091     textureInfo.loadState = LoadState::LOAD_FAILED;
1092     CheckForWaitingTexture(textureInfo);
1093     NotifyObservers(textureInfo, false);
1094   }
1095 }
1096
1097 void TextureManager::CheckForWaitingTexture(TextureInfo& maskTextureInfo)
1098 {
1099   // Search the cache, checking if any texture has this texture id as a
1100   // maskTextureId:
1101   const unsigned int size = mTextureInfoContainer.size();
1102
1103   for(unsigned int cacheIndex = 0; cacheIndex < size; ++cacheIndex)
1104   {
1105     if(mTextureInfoContainer[cacheIndex].maskTextureId == maskTextureInfo.textureId &&
1106        mTextureInfoContainer[cacheIndex].loadState == LoadState::WAITING_FOR_MASK)
1107     {
1108       TextureInfo& textureInfo(mTextureInfoContainer[cacheIndex]);
1109
1110       if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED)
1111       {
1112         // Send New Task to Thread
1113         ApplyMask(textureInfo, maskTextureInfo.textureId);
1114       }
1115       else
1116       {
1117         textureInfo.pixelBuffer.Reset();
1118         textureInfo.loadState = LoadState::LOAD_FAILED;
1119         NotifyObservers(textureInfo, false);
1120       }
1121     }
1122   }
1123 }
1124
1125 void TextureManager::ApplyMask(TextureInfo& textureInfo, TextureId maskTextureId)
1126 {
1127   int maskCacheIndex = GetCacheIndexFromId(maskTextureId);
1128   if(maskCacheIndex != INVALID_CACHE_INDEX)
1129   {
1130     Devel::PixelBuffer maskPixelBuffer = mTextureInfoContainer[maskCacheIndex].pixelBuffer;
1131     Devel::PixelBuffer pixelBuffer     = textureInfo.pixelBuffer;
1132     textureInfo.pixelBuffer.Reset();
1133
1134     DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ApplyMask(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
1135
1136     textureInfo.loadState   = LoadState::MASK_APPLYING;
1137     auto& loadersContainer  = (textureInfo.url.IsLocalResource() || textureInfo.url.IsBufferResource()) ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
1138     auto  loadingHelperIt   = loadersContainer.GetNext();
1139     auto  premultiplyOnLoad = textureInfo.preMultiplyOnLoad ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
1140     DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
1141     loadingHelperIt->ApplyMask(textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask, premultiplyOnLoad);
1142   }
1143 }
1144
1145 void TextureManager::UploadTexture(Devel::PixelBuffer& pixelBuffer, TextureInfo& textureInfo)
1146 {
1147   if(textureInfo.useAtlas != USE_ATLAS)
1148   {
1149     DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "  TextureManager::UploadTexture() New Texture for textureId:%d\n", textureInfo.textureId);
1150
1151     // Check if this pixelBuffer is premultiplied
1152     textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
1153
1154     auto& renderingAddOn = RenderingAddOn::Get();
1155     if(renderingAddOn.IsValid())
1156     {
1157       renderingAddOn.CreateGeometry(textureInfo.textureId, pixelBuffer);
1158     }
1159
1160     Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight());
1161
1162     PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
1163     texture.Upload(pixelData);
1164     if(!textureInfo.textureSet)
1165     {
1166       textureInfo.textureSet = TextureSet::New();
1167     }
1168     textureInfo.textureSet.SetTexture(0u, texture);
1169   }
1170
1171   // Update the load state.
1172   // Note: This is regardless of success as we care about whether a
1173   // load attempt is in progress or not.  If unsuccessful, a broken
1174   // image is still loaded.
1175   textureInfo.loadState = LoadState::UPLOADED;
1176 }
1177
1178 void TextureManager::NotifyObservers(TextureInfo& textureInfo, bool success)
1179 {
1180   TextureId textureId = textureInfo.textureId;
1181
1182   // If there is an observer: Notify the load is complete, whether successful or not,
1183   // and erase it from the list
1184   TextureInfo* info = &textureInfo;
1185
1186   mQueueLoadFlag = true;
1187
1188   while(info->observerList.Count())
1189   {
1190     TextureUploadObserver* observer = info->observerList[0];
1191
1192     // During UploadComplete() a Control ResourceReady() signal is emitted.
1193     // During that signal the app may add remove /add Textures (e.g. via
1194     // ImageViews).
1195     // It is possible for observers to be removed from the observer list,
1196     // and it is also possible for the mTextureInfoContainer to be modified,
1197     // invalidating the reference to the textureInfo struct.
1198     // Texture load requests for the same URL are deferred until the end of this
1199     // method.
1200     DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "NotifyObservers() url:%s loadState:%s\n", textureInfo.url.GetUrl().c_str(), GET_LOAD_STATE_STRING(textureInfo.loadState));
1201
1202     // It is possible for the observer to be deleted.
1203     // Disconnect and remove the observer first.
1204     observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1205
1206     info->observerList.Erase(info->observerList.begin());
1207
1208     if(info->storageType == StorageType::RETURN_PIXEL_BUFFER)
1209     {
1210       observer->LoadComplete(success, info->pixelBuffer, info->url, info->preMultiplied);
1211     }
1212     else
1213     {
1214       observer->UploadComplete(success, info->textureId, info->textureSet, info->useAtlas, info->atlasRect, info->preMultiplied);
1215     }
1216
1217     // Get the textureInfo from the container again as it may have been invalidated.
1218     int textureInfoIndex = GetCacheIndexFromId(textureId);
1219     if(textureInfoIndex == INVALID_CACHE_INDEX)
1220     {
1221       break; // texture has been removed - can stop.
1222     }
1223     info = &mTextureInfoContainer[textureInfoIndex];
1224   }
1225
1226   mQueueLoadFlag = false;
1227   ProcessQueuedTextures();
1228
1229   if(info->storageType == StorageType::RETURN_PIXEL_BUFFER && info->observerList.Count() == 0)
1230   {
1231     Remove(info->textureId, nullptr);
1232   }
1233 }
1234
1235 TextureManager::TextureId TextureManager::GenerateUniqueTextureId()
1236 {
1237   return mCurrentTextureId++;
1238 }
1239
1240 int TextureManager::GetCacheIndexFromId(const TextureId textureId)
1241 {
1242   const unsigned int size = mTextureInfoContainer.size();
1243
1244   for(unsigned int i = 0; i < size; ++i)
1245   {
1246     if(mTextureInfoContainer[i].textureId == textureId)
1247     {
1248       return i;
1249     }
1250   }
1251
1252   return INVALID_CACHE_INDEX;
1253 }
1254
1255 TextureManager::TextureHash TextureManager::GenerateHash(
1256   const std::string&             url,
1257   const ImageDimensions          size,
1258   const FittingMode::Type        fittingMode,
1259   const Dali::SamplingMode::Type samplingMode,
1260   const UseAtlas                 useAtlas,
1261   TextureId                      maskTextureId)
1262 {
1263   std::string    hashTarget(url);
1264   const size_t   urlLength = hashTarget.length();
1265   const uint16_t width     = size.GetWidth();
1266   const uint16_t height    = size.GetWidth();
1267
1268   // If either the width or height has been specified, include the resizing options in the hash
1269   if(width != 0 || height != 0)
1270   {
1271     // We are appending 5 bytes to the URL to form the hash input.
1272     hashTarget.resize(urlLength + 5u);
1273     char* hashTargetPtr = &(hashTarget[urlLength]);
1274
1275     // Pack the width and height (4 bytes total).
1276     *hashTargetPtr++ = size.GetWidth() & 0xff;
1277     *hashTargetPtr++ = (size.GetWidth() >> 8u) & 0xff;
1278     *hashTargetPtr++ = size.GetHeight() & 0xff;
1279     *hashTargetPtr++ = (size.GetHeight() >> 8u) & 0xff;
1280
1281     // Bit-pack the FittingMode, SamplingMode and atlasing.
1282     // FittingMode=2bits, SamplingMode=3bits, useAtlas=1bit
1283     *hashTargetPtr = (fittingMode << 4u) | (samplingMode << 1) | useAtlas;
1284   }
1285   else
1286   {
1287     // We are not including sizing information, but we still need an extra byte for atlasing.
1288     hashTarget.resize(urlLength + 1u);
1289
1290     // Add the atlasing to the hash input.
1291     switch(useAtlas)
1292     {
1293       case UseAtlas::NO_ATLAS:
1294       {
1295         hashTarget[urlLength] = 'f';
1296         break;
1297       }
1298       case UseAtlas::USE_ATLAS:
1299       {
1300         hashTarget[urlLength] = 't';
1301         break;
1302       }
1303     }
1304   }
1305
1306   if(maskTextureId != INVALID_TEXTURE_ID)
1307   {
1308     auto textureIdIndex = hashTarget.length();
1309     hashTarget.resize(hashTarget.length() + sizeof(TextureId));
1310     unsigned char* hashTargetPtr = reinterpret_cast<unsigned char*>(&(hashTarget[textureIdIndex]));
1311
1312     // Append the texture id to the end of the URL byte by byte:
1313     // (to avoid SIGBUS / alignment issues)
1314     for(size_t byteIter = 0; byteIter < sizeof(TextureId); ++byteIter)
1315     {
1316       *hashTargetPtr++ = maskTextureId & 0xff;
1317       maskTextureId >>= 8u;
1318     }
1319   }
1320
1321   return Dali::CalculateHash(hashTarget);
1322 }
1323
1324 int TextureManager::FindCachedTexture(
1325   const TextureManager::TextureHash hash,
1326   const std::string&                url,
1327   const ImageDimensions             size,
1328   const FittingMode::Type           fittingMode,
1329   const Dali::SamplingMode::Type    samplingMode,
1330   const bool                        useAtlas,
1331   TextureId                         maskTextureId,
1332   TextureManager::MultiplyOnLoad    preMultiplyOnLoad)
1333 {
1334   // Default to an invalid ID, in case we do not find a match.
1335   int cacheIndex = INVALID_CACHE_INDEX;
1336
1337   // Iterate through our hashes to find a match.
1338   const unsigned int count = mTextureInfoContainer.size();
1339   for(unsigned int i = 0u; i < count; ++i)
1340   {
1341     if(mTextureInfoContainer[i].hash == hash)
1342     {
1343       // We have a match, now we check all the original parameters in case of a hash collision.
1344       TextureInfo& textureInfo(mTextureInfoContainer[i]);
1345
1346       if((url == textureInfo.url.GetUrl()) &&
1347          (useAtlas == textureInfo.useAtlas) &&
1348          (maskTextureId == textureInfo.maskTextureId) &&
1349          (size == textureInfo.desiredSize) &&
1350          ((size.GetWidth() == 0 && size.GetHeight() == 0) ||
1351           (fittingMode == textureInfo.fittingMode &&
1352            samplingMode == textureInfo.samplingMode)))
1353       {
1354         // 1. If preMultiplyOnLoad is MULTIPLY_ON_LOAD, then textureInfo.preMultiplyOnLoad should be true. The premultiplication result can be different.
1355         // 2. If preMultiplyOnLoad is LOAD_WITHOUT_MULTIPLY, then textureInfo.preMultiplied should be false.
1356         if((preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD && textureInfo.preMultiplyOnLoad) || (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY && !textureInfo.preMultiplied))
1357         {
1358           // The found Texture is a match.
1359           cacheIndex = i;
1360           break;
1361         }
1362       }
1363     }
1364   }
1365
1366   return cacheIndex;
1367 }
1368
1369 void TextureManager::ObserverDestroyed(TextureUploadObserver* observer)
1370 {
1371   const unsigned int count = mTextureInfoContainer.size();
1372   for(unsigned int i = 0; i < count; ++i)
1373   {
1374     TextureInfo& textureInfo(mTextureInfoContainer[i]);
1375     for(TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin();
1376         j != textureInfo.observerList.End();)
1377     {
1378       if(*j == observer)
1379       {
1380         j = textureInfo.observerList.Erase(j);
1381       }
1382       else
1383       {
1384         ++j;
1385       }
1386     }
1387   }
1388
1389   // Remove element from the LoadQueue
1390   for(auto&& element : mLoadQueue)
1391   {
1392     if(element.mObserver == observer)
1393     {
1394       element.mObserver = nullptr;
1395     }
1396   }
1397 }
1398
1399 TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(TextureManager& textureManager)
1400 : AsyncLoadingHelper(Toolkit::AsyncImageLoader::New(), textureManager, AsyncLoadingInfoContainerType())
1401 {
1402 }
1403
1404 void TextureManager::AsyncLoadingHelper::LoadAnimatedImage(TextureId                  textureId,
1405                                                            Dali::AnimatedImageLoading animatedImageLoading,
1406                                                            uint32_t                   frameIndex)
1407 {
1408   mLoadingInfoContainer.push_back(AsyncLoadingInfo(textureId));
1409   auto id                             = GetImplementation(mLoader).LoadAnimatedImage(animatedImageLoading, frameIndex);
1410   mLoadingInfoContainer.back().loadId = id;
1411 }
1412
1413 void TextureManager::AsyncLoadingHelper::Load(TextureId                                textureId,
1414                                               const VisualUrl&                         url,
1415                                               ImageDimensions                          desiredSize,
1416                                               FittingMode::Type                        fittingMode,
1417                                               SamplingMode::Type                       samplingMode,
1418                                               bool                                     orientationCorrection,
1419                                               DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
1420 {
1421   mLoadingInfoContainer.push_back(AsyncLoadingInfo(textureId));
1422   auto id                             = GetImplementation(mLoader).Load(url, desiredSize, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad);
1423   mLoadingInfoContainer.back().loadId = id;
1424 }
1425
1426 void TextureManager::AsyncLoadingHelper::ApplyMask(TextureId                                textureId,
1427                                                    Devel::PixelBuffer                       pixelBuffer,
1428                                                    Devel::PixelBuffer                       maskPixelBuffer,
1429                                                    float                                    contentScale,
1430                                                    bool                                     cropToMask,
1431                                                    DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
1432 {
1433   mLoadingInfoContainer.push_back(AsyncLoadingInfo(textureId));
1434   auto id                             = GetImplementation(mLoader).ApplyMask(pixelBuffer, maskPixelBuffer, contentScale, cropToMask, preMultiplyOnLoad);
1435   mLoadingInfoContainer.back().loadId = id;
1436 }
1437
1438 TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(AsyncLoadingHelper&& rhs)
1439 : AsyncLoadingHelper(rhs.mLoader, rhs.mTextureManager, std::move(rhs.mLoadingInfoContainer))
1440 {
1441 }
1442
1443 TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(
1444   Toolkit::AsyncImageLoader       loader,
1445   TextureManager&                 textureManager,
1446   AsyncLoadingInfoContainerType&& loadingInfoContainer)
1447 : mLoader(loader),
1448   mTextureManager(textureManager),
1449   mLoadingInfoContainer(std::move(loadingInfoContainer))
1450 {
1451   DevelAsyncImageLoader::PixelBufferLoadedSignal(mLoader).Connect(
1452     this, &AsyncLoadingHelper::AsyncLoadComplete);
1453 }
1454
1455 void TextureManager::AsyncLoadingHelper::AsyncLoadComplete(uint32_t           id,
1456                                                            Devel::PixelBuffer pixelBuffer)
1457 {
1458   mTextureManager.AsyncLoadComplete(mLoadingInfoContainer, id, pixelBuffer);
1459 }
1460
1461 Geometry TextureManager::GetRenderGeometry(TextureId textureId, uint32_t& frontElements, uint32_t& backElements)
1462 {
1463   return RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().GetGeometry(textureId, frontElements, backElements) : Geometry();
1464 }
1465
1466 } // namespace Internal
1467
1468 } // namespace Toolkit
1469
1470 } // namespace Dali