[dali_2.2.30] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / texture-manager / texture-manager-impl.h
1 #ifndef DALI_TOOLKIT_TEXTURE_MANAGER_IMPL_H
2 #define DALI_TOOLKIT_TEXTURE_MANAGER_IMPL_H
3
4 /*
5  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 // EXTERNAL INCLUDES
21 #include <dali/devel-api/adaptor-framework/animated-image-loading.h>
22 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
23 #include <dali/integration-api/processor-interface.h>
24 #include <dali/public-api/adaptor-framework/encoded-image-buffer.h>
25 #include <dali/public-api/adaptor-framework/round-robin-container-view.h>
26 #include <dali/public-api/common/dali-vector.h>
27 #include <dali/public-api/rendering/geometry.h>
28
29 // INTERNAL INCLUDES
30 #include <dali-toolkit/devel-api/image-loader/image-atlas.h>
31 #include <dali-toolkit/internal/texture-manager/texture-cache-manager.h>
32 #include <dali-toolkit/internal/texture-manager/texture-manager-type.h>
33 #include <dali-toolkit/internal/texture-manager/texture-upload-observer.h>
34 #include <dali-toolkit/internal/visuals/visual-url.h>
35
36 namespace Dali
37 {
38 namespace Toolkit
39 {
40 namespace Internal
41 {
42 class ImageAtlasManager;
43 typedef IntrusivePtr<ImageAtlasManager> ImageAtlasManagerPtr;
44 class TextureAsyncLoadingHelper;
45
46 /**
47  * The TextureManager provides a common Image loading API for Visuals.
48  *
49  * The TextureManager is responsible for providing sync, async, atlased and non-atlased
50  * CPU time alpha masking, animated image loads.
51  *
52  * Texture caching is provided and performed by TextureCacheManager.
53  * TextureUploadObserver.LoadComplete called when async load completed.
54  */
55 class TextureManager : public ConnectionTracker, public Integration::Processor
56 {
57 public:
58   // Copy enum and types and const values that TextureManager will use.
59   using TextureId         = TextureManagerType::TextureId;
60   using TextureCacheIndex = TextureManagerType::TextureCacheIndex;
61   using TextureHash       = TextureManagerType::TextureHash;
62
63   static constexpr TextureId         INVALID_TEXTURE_ID  = TextureManagerType::INVALID_TEXTURE_ID;
64   static constexpr TextureCacheIndex INVALID_CACHE_INDEX = TextureManagerType::INVALID_CACHE_INDEX;
65
66   using UseAtlas       = TextureManagerType::UseAtlas;
67   using StorageType    = TextureManagerType::StorageType;
68   using LoadType       = TextureManagerType::LoadType;
69   using LoadState      = TextureManagerType::LoadState;
70   using ReloadPolicy   = TextureManagerType::ReloadPolicy;
71   using MultiplyOnLoad = TextureManagerType::MultiplyOnLoad;
72   using TextureInfo    = TextureManagerType::TextureInfo;
73
74 public:
75   struct MaskingData
76   {
77     MaskingData();
78     ~MaskingData() = default;
79
80     VisualUrl                 mAlphaMaskUrl;
81     TextureManager::TextureId mAlphaMaskId;
82     float                     mContentScaleFactor;
83     bool                      mCropToMask;
84     bool                      mPreappliedMasking;
85     bool                      mMaskImageLoadingFailed;
86   };
87   using MaskingDataPointer = std::unique_ptr<MaskingData>;
88
89   /**
90    * Class to provide lifecycle event on destruction of texture manager.
91    */
92   struct LifecycleObserver
93   {
94     /**
95      * Called shortly before the texture manager is destroyed.
96      */
97     virtual void TextureManagerDestroyed() = 0;
98   };
99
100   /**
101    * Constructor.
102    */
103   TextureManager();
104
105   /**
106    * Destructor.
107    */
108   ~TextureManager() override;
109
110   // TextureManager Main API:
111
112   /**
113    * @brief Requests an frame of animated image load.
114    *
115    * The parameters are used to specify how the animated image is loaded.
116    * The observer has the LoadComplete method called when the load is ready.
117    *
118    * @param[in]  url                   The URL of the image to load
119    * @param[in]  animatedImageLoading  The AnimatedImageLoading that contain the animated image information
120    * @param[in]  frameIndex            The frame index to load.
121    * @param[out] textureId             The textureId of the frame
122    * @param[in, out] maskInfo          Mask info structure
123    * @param[in]  desiredSize           The size the image is likely to appear at. This can be set to 0, 0 for automatic
124    * @param[in]  fittingMode           The FittingMode to use
125    * @param[in]  samplingMode          The SamplingMode to use
126    * @param[in]  synchronousLoading    true if the frame should be loaded synchronously
127    * @param[in]  textureObserver       The client object should inherit from this and provide the "LoadCompleted" virtual.
128    *                                   This is called when an image load completes (or fails).
129    * @param[in,out] preMultiplyOnLoad  True if the image color should be multiplied by it's alpha. Set to false if the
130    *                                   image has no alpha channel
131    *
132    * @return                           The texture set containing the frame of animated image, or empty if still loading.
133    */
134   TextureSet LoadAnimatedImageTexture(const VisualUrl&                url,
135                                       Dali::AnimatedImageLoading      animatedImageLoading,
136                                       const uint32_t&                 frameIndex,
137                                       TextureManager::TextureId&      textureId,
138                                       MaskingDataPointer&             maskInfo,
139                                       const Dali::ImageDimensions&    desiredSize,
140                                       const Dali::FittingMode::Type&  fittingMode,
141                                       const Dali::SamplingMode::Type& samplingMode,
142                                       const bool&                     synchronousLoading,
143                                       TextureUploadObserver*          textureObserver,
144                                       TextureManager::MultiplyOnLoad& preMultiplyOnLoad);
145
146   /**
147    * @brief Requests an image load of the given URL to get PixelBuffer.
148    *
149    * The parameters are used to specify how the image is loaded.
150    * The observer has the LoadComplete method called when the load is ready.
151    *
152    * @param[in] url                   The URL of the image to load
153    * @param[in] desiredSize           The size the image is likely to appear at. This can be set to 0,0 for automatic
154    * @param[in] fittingMode           The FittingMode to use
155    * @param[in] samplingMode          The SamplingMode to use
156    * @param[in] synchronousLoading    true if the URL should be loaded synchronously
157    * @param[in] textureObserver       The client object should inherit from this and provide the "LoadCompleted" virtual.
158    *                                  This is called when an image load completes (or fails).
159    * @param[in] orientationCorrection Whether to rotate image to match embedded orientation data
160    * @param[in,out] preMultiplyOnLoad True if the image color should be multiplied by it's alpha. Set to false if the
161    *                                  image has no alpha channel
162    *
163    * @return                          The pixel buffer containing the image, or empty if still loading.
164    */
165   Devel::PixelBuffer LoadPixelBuffer(
166     const VisualUrl&                url,
167     const Dali::ImageDimensions&    desiredSize,
168     const Dali::FittingMode::Type&  fittingMode,
169     const Dali::SamplingMode::Type& samplingMode,
170     const bool&                     synchronousLoading,
171     TextureUploadObserver*          textureObserver,
172     const bool&                     orientationCorrection,
173     TextureManager::MultiplyOnLoad& preMultiplyOnLoad);
174
175   /**
176    * @brief Requests an image load of the given URL.
177    *
178    * The parameters are used to specify how the image is loaded.
179    * The observer has the LoadComplete method called when the load is ready.
180    *
181    * When the client has finished with the Texture, Remove() should be called.
182    *
183    * @param[in] url                   The URL of the image to load
184    * @param[in] desiredSize           The size the image is likely to appear at. This can be set to 0,0 for automatic
185    * @param[in] fittingMode           The FittingMode to use
186    * @param[in] samplingMode          The SamplingMode to use
187    * @param[in, out] maskInfo         Mask info structure
188    * @param[in] synchronousLoading    true if the URL should be loaded synchronously
189    * @param[in, out] textureId        The textureId of the URL. It is also be used to check the previous textureId
190    *                                  what requestor had. It will be used only ReloadPolicy::FORCED for now.
191    * @param[out] textureRect          The rectangle within the texture atlas that this URL occupies,
192    *                                  this is the rectangle in normalized coordinates.
193    * @param[out] textureRectSize      The rectangle within the texture atlas that this URL occupies,
194    *                                  this is the same rectangle in pixels.
195    * @param[in,out] atlasingStatus    Set to USE_ATLAS to attempt atlasing. If atlasing fails, the image will still
196    *                                  be loaded, and marked successful, but this will be set to false.
197    *                                  If atlasing succeeds, this will be set to true.
198    * @param[out] loadingStatus        The loading status of the texture
199    * @param[in] textureObserver       The client object should inherit from this and provide the "LoadCompleted" virtual.
200    *                                  This is called when an image load completes (or fails).
201    * @param[in] atlasObserver         This is used if the texture is atlased, and will be called instead of
202    *                                  textureObserver.LoadCompleted
203    * @param[in] imageAtlasManager     The atlas manager to use for atlasing textures
204    * @param[in] orientationCorrection Whether to rotate image to match embedded orientation data
205    * @param[in] reloadPolicy          Forces a reload of the texture even if already cached
206    * @param[in,out] preMultiplyOnLoad True if the image color should be multiplied by it's alpha. Set to false if the
207    *                                  image has no alpha channel
208    *
209    * @return                          The texture set containing the image, or empty if still loading.
210    */
211   TextureSet LoadTexture(
212     const VisualUrl&                    url,
213     const Dali::ImageDimensions&        desiredSize,
214     const Dali::FittingMode::Type&      fittingMode,
215     const Dali::SamplingMode::Type&     samplingMode,
216     MaskingDataPointer&                 maskInfo,
217     const bool&                         synchronousLoading,
218     TextureManager::TextureId&          textureId,
219     Dali::Vector4&                      textureRect,
220     Dali::ImageDimensions&              textureRectSize,
221     bool&                               atlasingStatus,
222     bool&                               loadingStatus,
223     TextureUploadObserver*              textureObserver,
224     AtlasUploadObserver*                atlasObserver,
225     ImageAtlasManagerPtr                imageAtlasManager,
226     const bool&                         orientationCorrection,
227     const TextureManager::ReloadPolicy& reloadPolicy,
228     TextureManager::MultiplyOnLoad&     preMultiplyOnLoad);
229
230   /**
231    * Add an observer to the object.
232    * @param[in] observer The observer to add.
233    */
234   void AddObserver(TextureManager::LifecycleObserver& observer);
235
236   /**
237    * Remove an observer from the object
238    * @pre The observer has already been added.
239    * @param[in] observer The observer to remove.
240    */
241   void RemoveObserver(TextureManager::LifecycleObserver& observer);
242
243   /**
244    * @brief Returns the geometry associated with texture.
245    * @param[in] textureId Id of the texture
246    * @param[out] frontElements number of front elements
247    * @param[out] backElements number of back elements
248    * @return Returns valid geometry object
249    */
250   Geometry GetRenderGeometry(const TextureManager::TextureId& textureId, std::uint32_t& frontElements, std::uint32_t& backElements);
251
252   /**
253    * @brief Returns the textureSet in texture manager.
254    * @param[in] textureId Id of the texture
255    * @return The textureSet in texture manager. These textures include YUV textures or images and masks.
256    */
257   TextureSet GetTextureSet(const TextureManager::TextureId& textureId);
258
259   /**
260    * @brief Returns the textureSet in texture manager.
261    * @param[in] textureInfo the information of the texture
262    * @return The textureSet in texture manager. These textures include YUV textures or images and masks.
263    */
264   TextureSet GetTextureSet(const TextureManager::TextureInfo& textureInfo);
265
266 public:
267   // API list that need to access TextureCacheManager.
268
269   /**
270    * @copydoc TextureCacheManager::GetVisualUrl
271    */
272   inline VisualUrl GetVisualUrl(const TextureManager::TextureId& textureId)
273   {
274     return mTextureCacheManager.GetVisualUrl(textureId);
275   }
276
277   /**
278    * @copydoc TextureCacheManager::GetTexture
279    */
280   inline Texture GetTexture(const TextureManager::TextureId& textureId)
281   {
282     return mTextureCacheManager.GetTexture(textureId);
283   }
284
285   /**
286    * @copydoc TextureCacheManager::RemoveExternalTexture
287    */
288   inline TextureSet RemoveExternalTexture(const std::string& url)
289   {
290     return mTextureCacheManager.RemoveExternalTexture(url);
291   }
292
293   /**
294    * @copydoc TextureCacheManager::RemoveEncodedImageBuffer
295    */
296   inline EncodedImageBuffer RemoveEncodedImageBuffer(const std::string& url)
297   {
298     return mTextureCacheManager.RemoveEncodedImageBuffer(url);
299   }
300
301   /**
302    * @copydoc TextureCacheManager::UseExternalResource
303    */
304   inline void UseExternalResource(const VisualUrl& url)
305   {
306     mTextureCacheManager.UseExternalResource(url);
307   }
308
309   /**
310    * @copydoc TextureCacheManager::GetEncodedImageBuffer
311    */
312   inline EncodedImageBuffer GetEncodedImageBuffer(const std::string& url)
313   {
314     return mTextureCacheManager.GetEncodedImageBuffer(url);
315   }
316
317   /**
318    * @copydoc TextureCacheManager::AddExternalTexture
319    */
320   inline std::string AddExternalTexture(const TextureSet& texture, bool preMultiplied = false)
321   {
322     return mTextureCacheManager.AddExternalTexture(texture, preMultiplied);
323   }
324
325   /**
326    * @copydoc TextureCacheManager::AddEncodedImageBuffer
327    */
328   inline std::string AddEncodedImageBuffer(const EncodedImageBuffer& encodedImageBuffer)
329   {
330     return mTextureCacheManager.AddEncodedImageBuffer(encodedImageBuffer);
331   }
332
333 public: // Load Request API
334   /**
335    * @brief Requests an image load of the given URL.
336    *
337    * The parameters are used to specify how the image is loaded.
338    * The observer has the LoadComplete method called when the load is ready.
339    *
340    * When the client has finished with the Texture, Remove() should be called.
341    *
342    * @param[in] url                   The URL of the image to load
343    * @param[in] desiredSize           The size the image is likely to appear at. This can be set to 0,0 for automatic
344    * @param[in] fittingMode           The FittingMode to use
345    * @param[in] samplingMode          The SamplingMode to use
346    * @param[in] useAtlasing           Set to USE_ATLAS to attempt atlasing. If atlasing fails, the image will still be loaded, and marked successful,
347    *                                  but "useAtlasing" will be set to false in the "LoadCompleted" callback from the TextureManagerUploadObserver.
348    * @param[in] observer              The client object should inherit from this and provide the "LoadCompleted" virtual.
349    *                                  This is called when an image load completes (or fails).
350    * @param[in] orientationCorrection Whether to rotate image to match embedded orientation data
351    * @param[in] reloadPolicy          Forces a reload of the texture even if already cached
352    * @param[in,out] preMultiplyOnLoad True if the image color should be multiplied by it's alpha. Set to false if the image has no alpha channel
353    * @param[in] synchronousLoading    True if the frame should be loaded synchronously. If you skip this parameter,
354    *                                  default is false.
355    * @return                          A TextureId to use as a handle to reference this Texture
356    */
357   TextureId RequestLoad(
358     const VisualUrl&                    url,
359     const ImageDimensions&              desiredSize,
360     const Dali::FittingMode::Type&      fittingMode,
361     const Dali::SamplingMode::Type&     samplingMode,
362     const TextureManager::UseAtlas&     useAtlasing,
363     TextureUploadObserver*              observer,
364     const bool&                         orientationCorrection,
365     const TextureManager::ReloadPolicy& reloadPolicy,
366     TextureManager::MultiplyOnLoad&     preMultiplyOnLoad,
367     const bool&                         synchronousLoading = false);
368
369 private: // Internal Load Request API
370   /**
371    * @brief Requests an image load of the given URL, when the texture has
372    * have loaded, it will perform a blend with the image mask, and upload
373    * the blended texture.
374    *
375    * The parameters are used to specify how the image is loaded.
376    * The observer has the LoadComplete method called when the load is ready.
377    *
378    * When the client has finished with the Texture, Remove() should be called.
379    *
380    * @param[in] url                   The URL of the image to load
381    * @param[in] maskTextureId         The texture id of an image to mask this with
382    *                                  (can be INVALID if no masking required)
383    * @param[in] previousTextureId     The texture id of an image which the requestor already has before
384    * @param[in] contentScale          The scale factor to apply to the image before masking
385    * @param[in] desiredSize           The size the image is likely to appear at. This can be set to 0,0 for automatic
386    * @param[in] fittingMode           The FittingMode to use
387    * @param[in] samplingMode          The SamplingMode to use
388    * @param[in] useAtlasing           Set to USE_ATLAS to attempt atlasing. If atlasing fails, the image will still
389    *                                  be loaded, and marked successful,
390    *                                  but "useAtlasing" will be set to false in the "LoadCompleted" callback from
391    *                                  the TextureManagerUploadObserver.
392    * @param[in] cropToMask            Only used with masking, this will crop the scaled image to the mask size.
393    *                                  If false, then the mask will be scaled to fit the image before being applied.
394    * @param[in] observer              The client object should inherit from this and provide the "LoadCompleted"
395    *                                  virtual.
396    *                                  This is called when an image load completes (or fails).
397    * @param[in] orientationCorrection Whether to rotate image to match embedded orientation data
398    * @param[in] reloadPolicy          Forces a reload of the texture even if already cached
399    * @param[in,out] preMultiplyOnLoad True if the image color should be multiplied by it's alpha. Set to false if the
400    *                                  image has no alpha channel
401    * @param[in] synchronousLoading    True if the frame should be loaded synchronously. If you skip this parameter,
402    *                                  default is false.
403    * @return                          A TextureId to use as a handle to reference this Texture
404    */
405   TextureId RequestLoad(
406     const VisualUrl&                    url,
407     const TextureManager::TextureId&    maskTextureId,
408     const TextureManager::TextureId&    previousTextureId,
409     const float&                        contentScale,
410     const ImageDimensions&              desiredSize,
411     const Dali::FittingMode::Type&      fittingMode,
412     const Dali::SamplingMode::Type&     samplingMode,
413     const TextureManager::UseAtlas&     useAtlasing,
414     const bool&                         cropToMask,
415     TextureUploadObserver*              observer,
416     const bool&                         orientationCorrection,
417     const TextureManager::ReloadPolicy& reloadPolicy,
418     TextureManager::MultiplyOnLoad&     preMultiplyOnLoad,
419     const bool&                         synchronousLoading = false);
420
421   /**
422    * @brief Requests a masking image to be loaded. This mask is not uploaded to GL,
423    * instead, it is stored in CPU memory, and can be used for CPU blending.
424    * @param[in] maskUrl            The URL of the mask image to load
425    * @param[in] storageType,       Whether the pixel data is stored in the cache or uploaded to the GPU
426    * @param[in] synchronousLoading True if the frame should be loaded synchronously. If you skip this parameter,
427    *                               default is false.
428    * @return                       A TextureId to use as a handle to reference this mask Texture
429    */
430   TextureId RequestMaskLoad(
431     const VisualUrl& maskUrl,
432     StorageType      storageType,
433     const bool&      synchronousLoading = false);
434
435   /**
436    * @brief Requests an image load of the given URL, when the texture has
437    * have loaded, if there is a valid maskTextureId, it will perform a
438    * CPU blend with the mask, and upload the blend texture.
439    *
440    * The parameters are used to specify how the image is loaded.
441    * The observer has the LoadComplete method called when the load is ready.
442    *
443    * When the client has finished with the Texture, Remove() should be called.
444    *
445    * @param[in] url                   The URL of the image to load
446    * @param[in] maskTextureId         The texture id of an image to use as a mask. If no mask is required, then set
447    *                                  to INVALID_TEXTURE_ID
448    * @param[in] previousTextureId     The texture id of an image which the requestor already has before. It will be used
449    *                                  when reloadPolicy is FORCED.
450    * @param[in] contentScale          The scaling factor to apply to the content when masking
451    * @param[in] desiredSize           The size the image is likely to appear at. This can be set to 0,0 for automatic
452    * @param[in] fittingMode           The FittingMode to use
453    * @param[in] samplingMode          The SamplingMode to use
454    * @param[in] useAtlasing           Set to USE_ATLAS to attempt atlasing. If atlasing fails, the image will still be
455    *                                  loaded, and marked successful, but "useAtlasing" will be set to false in the
456    *                                  "LoadCompleted" callback from the TextureManagerUploadObserver.
457    * @param[in] cropToMask            Whether to crop the target after masking, or scale the mask to the image before
458    *                                  masking.
459    * @param[in] storageType,          Whether the pixel data is stored in the cache or uploaded to the GPU
460    * @param[in] observer              The client object should inherit from this and provide the "LoadCompleted"
461    *                                  virtual.
462    *                                  This is called when an image load completes (or fails).
463    * @param[in] orientationCorrection Whether to rotate image to match embedded orientation data
464    * @param[in] reloadPolicy          Forces a reload of the texture even if already cached
465    * @param[in,out] preMultiplyOnLoad True if the image color should be multiplied by it's alpha. Set to false if
466    *                                  there is no alpha
467    * @param[in] animatedImageLoading  The AnimatedImageLoading to load animated image
468    * @param[in] frameIndex            The frame index of a frame to be loaded frame
469    * @param[in] synchronousLoading    True if the frame should be loaded synchronously. If you skip this parameter,
470    *                                  default is false.
471    * @return                          A TextureId to use as a handle to reference this Texture
472    */
473   TextureId RequestLoadInternal(
474     const VisualUrl&                    url,
475     const TextureManager::TextureId&    maskTextureId,
476     const TextureManager::TextureId&    previousTextureId,
477     const float&                        contentScale,
478     const Dali::ImageDimensions&        desiredSize,
479     const Dali::FittingMode::Type&      fittingMode,
480     const Dali::SamplingMode::Type&     samplingMode,
481     const TextureManager::UseAtlas&     useAtlas,
482     const bool&                         cropToMask,
483     const TextureManager::StorageType&  storageType,
484     TextureUploadObserver*              observer,
485     const bool&                         orientationCorrection,
486     const TextureManager::ReloadPolicy& reloadPolicy,
487     TextureManager::MultiplyOnLoad&     preMultiplyOnLoad,
488     Dali::AnimatedImageLoading          animatedImageLoading,
489     const std::uint32_t&                frameIndex,
490     const bool&                         synchronousLoading);
491
492   /**
493    * @brief Load a new image synchronously.
494    * @param[in] url                   The URL of the image to load
495    * @param[in] desiredSize           The size the image is likely to appear at.
496    *                                  This can be set to 0,0 for automatic
497    * @param[in] fittingMode           The FittingMode to use
498    * @param[in] samplingMode          The SamplingMode to use
499    * @param[in] orientationCorrection Whether to use image metadata to rotate or flip the image,
500    *                                  e.g., from portrait to landscape
501    * @param[in] loadYuvPlanes         True if the image should be loaded as yuv planes
502    * @param[out] pixelBuffers         The image pixelBuffer
503    * @return PixelBuffer of loaded image.
504    */
505   void LoadImageSynchronously(
506     const VisualUrl&                 url,
507     const Dali::ImageDimensions&     desiredSize,
508     const Dali::FittingMode::Type&   fittingMode,
509     const Dali::SamplingMode::Type&  samplingMode,
510     const bool&                      orientationCorrection,
511     const bool&                      loadYuvPlanes,
512     std::vector<Devel::PixelBuffer>& pixelBuffers);
513
514 public: // Remove Request API
515   /**
516    * @brief Request Remove a Texture from the TextureManager.
517    *
518    * Textures are cached and therefore only the removal of the last
519    * occurrence of a Texture will cause its removal internally.
520    *
521    * @param[in] textureId The ID of the Texture to remove.
522    * @param[in] textureObserver The texture observer.
523    */
524   void RequestRemove(const TextureManager::TextureId& textureId, TextureUploadObserver* textureObserver);
525
526 private:
527   /**
528    * @brief Remove a Texture from the TextureManager.
529    *
530    * Textures are cached and therefore only the removal of the last
531    * occurrence of a Texture will cause its removal internally.
532    *
533    * @param[in] textureId The ID of the Texture to remove.
534    */
535   void Remove(const TextureManager::TextureId& textureId);
536
537   /**
538    * @brief Initiate remove of texture queued.
539    */
540   void ProcessRemoveQueue();
541
542 private:
543   // Load and queue
544
545   /**
546    * Structure to hold info about a texture load queued during NotifyObservers
547    */
548   struct QueueElement
549   {
550     QueueElement(TextureManager::TextureId textureId, TextureUploadObserver* observer)
551     : mTextureId(textureId),
552       mObserver(observer)
553     {
554     }
555
556     TextureManager::TextureId mTextureId; ///< The texture id of the requested load.
557     TextureUploadObserver*    mObserver;  ///< Observer of texture load.
558   };
559
560   /**
561    * @brief Initiate a load or queue load if NotifyObservers is invoking callbacks
562    * @param[in] textureInfo The TextureInfo struct associated with the Texture
563    * @param[in] observer The observer wishing to observe the texture upload
564    */
565   void LoadOrQueueTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer);
566
567   /**
568    * @brief Queue a texture load to be subsequently handled by ProcessLoadQueue.
569    * @param[in] textureInfo The TextureInfo struct associated with the Texture
570    * @param[in] observer The observer wishing to observe the texture upload
571    */
572   void QueueLoadTexture(const TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer);
573
574   /**
575    * @brief Used internally to initiate a load.
576    * @param[in] textureInfo The TextureInfo struct associated with the Texture
577    * @param[in] observer The observer wishing to observe the texture upload
578    */
579   void LoadTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer);
580
581   /**
582    * @brief Initiate load of textures queued whilst NotifyObservers invoking callbacks.
583    */
584   void ProcessLoadQueue();
585
586   /**
587    * Add the observer to the observer list
588    * @param[in] textureInfo The TextureInfo struct associated with the texture
589    * @param[in] observer The observer wishing to observe the texture upload
590    */
591   void ObserveTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer);
592
593   /**
594    * @brief Performs Post-Load steps including atlasing.
595    * @param[in] textureInfo  The struct associated with this Texture
596    * @param[in] pixelBuffers The image pixelBuffer
597    * @return    True if successful
598    */
599   void PostLoad(TextureManager::TextureInfo& textureInfo, std::vector<Devel::PixelBuffer>& pixelBuffers);
600
601   /**
602    * Check if there is a texture waiting to be masked. If there
603    * is then apply this mask and upload it.
604    * @param[in] maskTextureInfo The texture info of the mask that has just loaded.
605    */
606   void CheckForWaitingTexture(TextureManager::TextureInfo& maskTextureInfo);
607
608   /**
609    * Apply the mask to the pixelBuffer.
610    * @param[in] textureInfo The information of texture to apply the mask to
611    * @param[in] maskTextureId The texture id of the mask.
612    */
613   void ApplyMask(TextureManager::TextureInfo& textureInfo, const TextureManager::TextureId& maskTextureId);
614
615   /**
616    * Upload the texture specified in pixelBuffer to the appropriate location
617    * @param[in] pixelBuffers The image data to upload
618    * @param[in] textureInfo  The texture info containing the location to store the data to.
619    */
620   void UploadTextures(std::vector<Devel::PixelBuffer>& pixelBuffers, TextureManager::TextureInfo& textureInfo);
621
622   /**
623    * Notify the current observers that the texture upload is complete,
624    * then remove the observers from the list.
625    * @param[in] textureInfo The struct associated with this Texture
626    * @param[in] success If the pixel data was retrieved successfully and uploaded to GPU
627    */
628   void NotifyObservers(TextureManager::TextureInfo& textureInfo, const bool& success);
629
630   /**
631    * Call LoadComplete to the observer.
632    * @param[in] observer    The client object should inherit from this and provide the "LoadCompleted"
633    * @param[in] textureInfo The struct associated with this Texture
634    * @param[in] success     If the pixel data was retrieved successfully and uploaded to GPU
635    */
636   void EmitLoadComplete(TextureUploadObserver* observer, TextureManager::TextureInfo& textureInfo, const bool& success);
637
638   /**
639    * @brief Remove observer in textureInfo
640    *
641    * @param textureInfo The struct associated with this Texture.
642    * @param observer The observer wishing to remove.
643    */
644   void RemoveTextureObserver(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer);
645
646 public:
647   /**
648    * @brief Common method to handle loading completion.
649    * TextureAsyncLoadingHelper will call this API After async loading finished.
650    * @param[in] textureId    The ID of the texture load complete.
651    * @param[in] pixelBuffers The loaded image data
652    */
653   void AsyncLoadComplete(const TextureManager::TextureId& textureId, std::vector<Devel::PixelBuffer>& pixelBuffers);
654
655 protected: // Implementation of Processor
656   /**
657    * @copydoc Dali::Integration::Processor::Process()
658    */
659   void Process(bool postProcessor) override;
660
661 private:
662   /**
663    * Deleted copy constructor.
664    */
665   TextureManager(const TextureManager&) = delete;
666
667   /**
668    * Deleted assignment operator.
669    */
670   TextureManager& operator=(const TextureManager& rhs) = delete;
671
672   /**
673    * This is called by the TextureManagerUploadObserver when an observer is destroyed.
674    * We use the callback to know when to remove an observer from our notify list.
675    * @param[in] observer The observer that generated the callback
676    */
677   void ObserverDestroyed(TextureUploadObserver* observer);
678
679 private:                                    // Member Variables:
680   TextureCacheManager mTextureCacheManager; ///< Manager the life-cycle and caching of Textures
681
682   std::unique_ptr<TextureAsyncLoadingHelper> mAsyncLoader;        ///< The Asynchronous image loader used to provide all local async loads
683   Dali::Vector<LifecycleObserver*>           mLifecycleObservers; ///< Lifecycle observers of texture manager
684
685   Dali::Vector<QueueElement> mLoadQueue;             ///< Queue of textures to load after NotifyObservers
686   TextureManager::TextureId  mLoadingQueueTextureId; ///< TextureId when it is loading. it causes Load Textures to be queued.
687
688   Dali::Vector<TextureManager::TextureId> mRemoveQueue; ///< Queue of textures to remove at PostProcess. It will be cleared after PostProcess.
689
690   bool mLoadYuvPlanes;             ///< A global flag to specify if the image should be loaded as yuv planes
691   bool mRemoveProcessorRegistered; ///< Flag if remove processor registered or not.
692 };
693
694 } // namespace Internal
695
696 } // namespace Toolkit
697
698 } // namespace Dali
699
700 #endif // DALI_TOOLKIT_TEXTURE_MANAGER_IMPL_H