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