Merge "supports remote URL gif image" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / 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) 2017 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 <deque>
22 #include <functional>
23 #include <string>
24 #include <memory>
25 #include <dali/public-api/common/dali-vector.h>
26 #include <dali/public-api/object/ref-object.h>
27 #include <dali/public-api/rendering/texture-set.h>
28 #include <dali/devel-api/common/owner-container.h>
29 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
30
31 // INTERNAL INCLUDES
32 #include <dali-toolkit/devel-api/image-loader/async-image-loader-devel.h>
33 #include <dali-toolkit/devel-api/image-loader/image-atlas.h>
34 #include <dali-toolkit/public-api/image-loader/async-image-loader.h>
35 #include <dali-toolkit/internal/visuals/texture-upload-observer.h>
36 #include <dali-toolkit/internal/visuals/visual-url.h>
37 #include <dali-toolkit/internal/helpers/round-robin-container-view.h>
38 #include <dali-toolkit/internal/image-loader/async-image-loader-impl.h>
39
40
41 namespace Dali
42 {
43
44 namespace Toolkit
45 {
46
47 namespace Internal
48 {
49 class ImageAtlasManager;
50 typedef IntrusivePtr<ImageAtlasManager> ImageAtlasManagerPtr;
51
52 /**
53  * The TextureManager provides a common Image loading API for Visuals.
54  *
55  * The TextureManager is responsible for providing sync, async, atlased and non-atlased loads.
56  * Texture caching is provided and performed when possible.
57  * Broken Images are automatically provided on load failure.
58  */
59 class TextureManager : public ConnectionTracker
60 {
61 public:
62
63   typedef int32_t TextureId;       ///< The TextureId type. This is used as a handle to refer to a particular Texture.
64   static const int INVALID_TEXTURE_ID = -1; ///< Used to represent a null TextureId or error
65
66   /**
67    * Whether the texture should be atlased or uploaded into it's own GPU texture
68    */
69   enum UseAtlas
70   {
71     NO_ATLAS,
72     USE_ATLAS
73   };
74
75   /**
76    * Whether the pixel data should be kept in TextureManager, or uploaded for rendering
77    */
78   enum StorageType
79   {
80     KEEP_PIXEL_BUFFER,
81     UPLOAD_TO_TEXTURE
82   };
83
84   /**
85    * Whether the texture should be loaded synchronously or asynchronously.
86    */
87   enum LoadType
88   {
89     LOAD_ASYNCHRONOUSLY,
90     LOAD_SYNCHRONOUSLY
91   };
92
93   /**
94    * @brief The LoadState Enumeration represents the current state of a particular Texture's life-cycle.
95    */
96   enum LoadState
97   {
98     NOT_STARTED,     ///< Default
99     LOADING,         ///< Loading has been started, but not finished.
100     LOAD_FINISHED,   ///< Loading has finished. (for CPU storage only)
101     WAITING_FOR_MASK,///< Loading has finished, but waiting for mask image
102     UPLOADED,        ///< Uploaded and ready. (For GPU upload only)
103     CANCELLED,       ///< Removed before loading completed
104     LOAD_FAILED      ///< Async loading failed, e.g. connection problem
105   };
106
107   /**
108    * @brief Types of reloading policies
109    */
110   enum class ReloadPolicy
111   {
112     CACHED = 0,             ///< Loads cached texture if it exists.
113     FORCED                  ///< Forces reloading of texture.
114   };
115
116   /**
117    * @brief Whether to multiply alpha into color channels on load
118    */
119   enum class MultiplyOnLoad
120   {
121     LOAD_WITHOUT_MULTIPLY = 0, ///< Don't modify the image
122     MULTIPLY_ON_LOAD           ///< Multiply alpha into color channels on load
123   };
124
125 public:
126
127   struct MaskingData
128   {
129     MaskingData();
130     ~MaskingData() = default;
131
132     VisualUrl mAlphaMaskUrl;
133     TextureManager::TextureId mAlphaMaskId;
134     float mContentScaleFactor;
135     bool mCropToMask;
136   };
137   using MaskingDataPointer = std::unique_ptr<MaskingData>;
138
139
140   /**
141    * Class to provide lifecycle event on destruction of texture manager.
142    */
143   struct LifecycleObserver
144   {
145     /**
146      * Called shortly before the texture manager is destroyed.
147      */
148     virtual void TextureManagerDestroyed() = 0;
149   };
150
151   /**
152    * Constructor.
153    */
154   TextureManager();
155
156   /**
157    * Destructor.
158    */
159   ~TextureManager();
160
161   // TextureManager Main API:
162
163   /**
164    * @brief Requests an image load of the given URL.
165    *
166    * The parameters are used to specify how the image is loaded.
167    * The observer has the UploadComplete method called when the load is ready.
168    *
169    * When the client has finished with the Texture, Remove() should be called.
170    *
171    * @param[in] url                   The URL of the image to load
172    * @param[in] desiredSize           The size the image is likely to appear at. This can be set to 0,0 for automatic
173    * @param[in] fittingMode           The FittingMode to use
174    * @param[in] samplingMode          The SamplingMode to use
175    * @param[in] maskInfo              Mask info structure
176    * @param[in] synchronousLoading    true if the URL should be loaded synchronously
177    * @param[out] textureId,           The textureId of the URL
178    * @param[out] textureRect          The rectangle within the texture atlas that this URL occupies
179    * @param[in,out] atlasingStatus    Set to USE_ATLAS to attempt atlasing. If atlasing fails, the image will still
180    *                                  be loaded, and marked successful, but this will be set to false.
181    *                                  If atlasing succeeds, this will be set to true.
182    * @param[out] loadingStatus        The loading status of the texture
183    * @param[in] wrapModeU             Horizontal Wrap mode
184    * @param[in] wrapModeV             Vertical Wrap mode
185    * @param[in] textureObserver       The client object should inherit from this and provide the "UploadCompleted" virtual.
186    *                                  This is called when an image load completes (or fails).
187    * @param[in] atlasObserver         This is used if the texture is atlased, and will be called instead of
188    *                                  textureObserver.UploadCompleted
189    * @param[in] imageAtlasManager     The atlas manager to use for atlasing textures
190    * @param[in] orientationCorrection Whether to rotate image to match embedded orientation data
191    * @param[in] reloadPolicy          Forces a reload of the texture even if already cached
192    * @param[in,out] preMultiplyOnLoad True if the image color should be multiplied by it's alpha. Set to false if the
193    *                                  image has no alpha channel
194    *
195    * @return                          The texture set containing the image, or empty if still loading.
196    */
197
198   TextureSet LoadTexture( const VisualUrl&             url,
199                           Dali::ImageDimensions        desiredSize,
200                           Dali::FittingMode::Type      fittingMode,
201                           Dali::SamplingMode::Type     samplingMode,
202                           const MaskingDataPointer&    maskInfo,
203                           bool                         synchronousLoading,
204                           TextureManager::TextureId&   textureId,
205                           Vector4&                     textureRect,
206                           bool&                        atlasingStatus,
207                           bool&                        loadingStatus,
208                           Dali::WrapMode::Type         wrapModeU,
209                           Dali::WrapMode::Type         wrapModeV,
210                           TextureUploadObserver*       textureObserver,
211                           AtlasUploadObserver*         atlasObserver,
212                           ImageAtlasManagerPtr         imageAtlasManager,
213                           bool                         orientationCorrection,
214                           TextureManager::ReloadPolicy reloadPolicy,
215                           MultiplyOnLoad&              preMultiplyOnLoad );
216
217   /**
218    * @brief Requests an image load of the given URL.
219    *
220    * The parameters are used to specify how the image is loaded.
221    * The observer has the UploadComplete method called when the load is ready.
222    *
223    * When the client has finished with the Texture, Remove() should be called.
224    *
225    * @param[in] url                   The URL of the image to load
226    * @param[in] desiredSize           The size the image is likely to appear at. This can be set to 0,0 for automatic
227    * @param[in] fittingMode           The FittingMode to use
228    * @param[in] samplingMode          The SamplingMode to use
229    * @param[in] useAtlasing           Set to USE_ATLAS to attempt atlasing. If atlasing fails, the image will still be loaded, and marked successful,
230    *                                  but "useAtlasing" will be set to false in the "UploadCompleted" callback from the TextureManagerUploadObserver.
231    * @param[in] observer              The client object should inherit from this and provide the "UploadCompleted" virtual.
232    *                                  This is called when an image load completes (or fails).
233    * @param[in] orientationCorrection Whether to rotate image to match embedded orientation data
234    * @param[in] reloadPolicy          Forces a reload of the texture even if already cached
235    * @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
236    * @return                          A TextureId to use as a handle to reference this Texture
237    */
238   TextureId RequestLoad( const VisualUrl&                   url,
239                          const ImageDimensions              desiredSize,
240                          FittingMode::Type                  fittingMode,
241                          Dali::SamplingMode::Type           samplingMode,
242                          const UseAtlas                     useAtlasing,
243                          TextureUploadObserver*             observer,
244                          bool                               orientationCorrection,
245                          TextureManager::ReloadPolicy       reloadPolicy,
246                          MultiplyOnLoad&                    preMultiplyOnLoad );
247
248   /**
249    * @brief Requests an image load of the given URL, when the texture has
250    * have loaded, it will perform a blend with the image mask, and upload
251    * the blended texture.
252    *
253    * The parameters are used to specify how the image is loaded.
254    * The observer has the UploadComplete method called when the load is ready.
255    *
256    * When the client has finished with the Texture, Remove() should be called.
257    *
258    * @param[in] url                   The URL of the image to load
259    * @param[in] maskTextureId         The texture id of an image to mask this with
260    *                                  (can be INVALID if no masking required)
261    * @param[in] contentScale          The scale factor to apply to the image before masking
262    * @param[in] desiredSize           The size the image is likely to appear at. This can be set to 0,0 for automatic
263    * @param[in] fittingMode           The FittingMode to use
264    * @param[in] samplingMode          The SamplingMode to use
265    * @param[in] useAtlasing           Set to USE_ATLAS to attempt atlasing. If atlasing fails, the image will still
266    *                                  be loaded, and marked successful,
267    *                                  but "useAtlasing" will be set to false in the "UploadCompleted" callback from
268    *                                  the TextureManagerUploadObserver.
269    * @param[in] cropToMask            Only used with masking, this will crop the scaled image to the mask size.
270    *                                  If false, then the mask will be scaled to fit the image before being applied.
271    * @param[in] observer              The client object should inherit from this and provide the "UploadCompleted"
272    *                                  virtual.
273    *                                  This is called when an image load completes (or fails).
274    * @param[in] orientationCorrection Whether to rotate image to match embedded orientation data
275    * @param[in] reloadPolicy          Forces a reload of the texture even if already cached
276    * @param[in] preMultiplyOnLoad     True if the image color should be multiplied by it's alpha. Set to false if the
277    *                                  image has no alpha channel
278    * @return                          A TextureId to use as a handle to reference this Texture
279    */
280   TextureId RequestLoad( const VisualUrl&                   url,
281                          TextureId                          maskTextureId,
282                          float                              contentScale,
283                          const ImageDimensions              desiredSize,
284                          FittingMode::Type                  fittingMode,
285                          Dali::SamplingMode::Type           samplingMode,
286                          const UseAtlas                     useAtlasing,
287                          bool                               cropToMask,
288                          TextureUploadObserver*             observer,
289                          bool                               orientationCorrection,
290                          TextureManager::ReloadPolicy       reloadPolicy,
291                          MultiplyOnLoad&                    preMultiplyOnLoad );
292
293   /**
294    * Requests a masking image to be loaded. This mask is not uploaded to GL,
295    * instead, it is stored in CPU memory, and can be used for CPU blending.
296    */
297   TextureId RequestMaskLoad( const VisualUrl& maskUrl );
298
299   /**
300    * @brief Remove a Texture from the TextureManager.
301    *
302    * Textures are cached and therefore only the removal of the last
303    * occurrence of a Texture will cause its removal internally.
304    *
305    * @param[in] textureId The ID of the Texture to remove.
306    */
307   void Remove( const TextureManager::TextureId textureId );
308
309   /**
310    * @brief Get the visualUrl associated with the texture id.
311    * @param[in] textureId The texture Id to get
312    * @return The visual Url associated with the texture id.
313    */
314   VisualUrl GetVisualUrl( TextureId textureId );
315
316   /**
317    * @brief Get the current state of a texture
318    * @param[in] textureId The texture id to query
319    * @return The loading state if the texture is valid, or NOT_STARTED if the textureId
320    * is not valid.
321    */
322   LoadState GetTextureState( TextureId textureId );
323
324   /**
325    * @brief Get the associated texture set if the texture id is valid
326    * @param[in] textureId The texture Id to look up
327    * @return the associated texture set, or an empty handle if textureId is not valid
328    */
329   TextureSet GetTextureSet( TextureId textureId );
330
331   /**
332    * Adds an external texture to the texture manager
333    * @param[in] texture The texture to add
334    * @return string containing the URL for the texture
335    */
336   std::string AddExternalTexture( TextureSet& texture );
337
338   /**
339    * Removes an external texture from texture manager
340    * @param[in] url The string containing the texture to remove
341    * @return handle to the texture
342    */
343   TextureSet RemoveExternalTexture( const std::string& url );
344
345   /**
346    * Add an observer to the object.
347    * @param[in] observer The observer to add.
348    */
349   void AddObserver( TextureManager::LifecycleObserver& observer );
350
351   /**
352    * Remove an observer from the object
353    * @pre The observer has already been added.
354    * @param[in] observer The observer to remove.
355    */
356   void RemoveObserver( TextureManager::LifecycleObserver& observer );
357
358 private:
359
360   /**
361    * @brief Requests an image load of the given URL, when the texture has
362    * have loaded, if there is a valid maskTextureId, it will perform a
363    * CPU blend with the mask, and upload the blend texture.
364    *
365    * The parameters are used to specify how the image is loaded.
366    * The observer has the UploadComplete method called when the load is ready.
367    *
368    * When the client has finished with the Texture, Remove() should be called.
369    *
370    * @param[in] url                   The URL of the image to load
371    * @param[in] maskTextureId         The texture id of an image to use as a mask. If no mask is required, then set
372    *                                  to INVALID_TEXTURE_ID
373    * @param[in] contentScale          The scaling factor to apply to the content when masking
374    * @param[in] desiredSize           The size the image is likely to appear at. This can be set to 0,0 for automatic
375    * @param[in] fittingMode           The FittingMode to use
376    * @param[in] samplingMode          The SamplingMode to use
377    * @param[in] useAtlasing           Set to USE_ATLAS to attempt atlasing. If atlasing fails, the image will still be
378    *                                  loaded, and marked successful, but "useAtlasing" will be set to false in the
379    *                                  "UploadCompleted" callback from the TextureManagerUploadObserver.
380    * @param[in] cropToMask            Whether to crop the target after masking, or scale the mask to the image before
381    *                                  masking.
382    * @param[in] storageType,          Whether the pixel data is stored in the cache or uploaded to the GPU
383    * @param[in] observer              The client object should inherit from this and provide the "UploadCompleted"
384    *                                  virtual.
385    *                                  This is called when an image load completes (or fails).
386    * @param[in] orientationCorrection Whether to rotate image to match embedded orientation data
387    * @param[in] reloadPolicy          Forces a reload of the texture even if already cached
388    * @param[in] preMultiplyOnLoad     True if the image color should be multiplied by it's alpha. Set to false if
389    *                                  there is no alpha
390    * @return                          A TextureId to use as a handle to reference this Texture
391    */
392   TextureId RequestLoadInternal(
393     const VisualUrl&                    url,
394     TextureId                           maskTextureId,
395     float                               contentScale,
396     const ImageDimensions               desiredSize,
397     FittingMode::Type                   fittingMode,
398     Dali::SamplingMode::Type            samplingMode,
399     UseAtlas                            useAtlas,
400     bool                                cropToMask,
401     StorageType                         storageType,
402     TextureUploadObserver*              observer,
403     bool                                orientationCorrection,
404     TextureManager::ReloadPolicy        reloadPolicy,
405     MultiplyOnLoad&                     preMultiplyOnLoad );
406
407   /**
408    * @brief Get the current state of a texture
409    * @param[in] textureId The texture id to query
410    * @return The loading state if the texture is valid, or NOT_STARTED if the textureId
411    * is not valid.
412    */
413   LoadState GetTextureStateInternal( TextureId textureId );
414
415   typedef size_t TextureHash; ///< The type used to store the hash used for Texture caching.
416
417   /**
418    * @brief This struct is used to manage the life-cycle of Texture loading and caching.
419    */
420   struct TextureInfo
421   {
422     TextureInfo( TextureId textureId,
423                  TextureId maskTextureId,
424                  const VisualUrl& url,
425                  ImageDimensions desiredSize,
426                  float scaleFactor,
427                  FittingMode::Type fittingMode,
428                  Dali::SamplingMode::Type samplingMode,
429                  bool loadSynchronously,
430                  bool cropToMask,
431                  UseAtlas useAtlas,
432                  TextureManager::TextureHash hash,
433                  bool orientationCorrection,
434                  bool preMultiplyOnLoad )
435     : url( url ),
436       desiredSize( desiredSize ),
437       useSize( desiredSize ),
438       atlasRect( 0.0f, 0.0f, 1.0f, 1.0f ), // Full atlas rectangle
439       textureId( textureId ),
440       maskTextureId( maskTextureId ),
441       hash( hash ),
442       scaleFactor( scaleFactor ),
443       referenceCount( 1u ),
444       loadState( NOT_STARTED ),
445       fittingMode( fittingMode ),
446       samplingMode( samplingMode ),
447       storageType( UPLOAD_TO_TEXTURE ),
448       loadSynchronously( loadSynchronously ),
449       useAtlas( useAtlas ),
450       cropToMask( cropToMask ),
451       orientationCorrection( true ),
452       preMultiplyOnLoad( preMultiplyOnLoad ),
453       preMultiplied( false )
454     {
455     }
456
457     /**
458      * Container type used to store all observer clients of this Texture
459      */
460     typedef Dali::Vector< TextureUploadObserver* > ObserverListType;
461
462     ObserverListType observerList; ///< Container used to store all observer clients of this Texture
463     Toolkit::ImageAtlas atlas;     ///< The atlas this Texture lays within (if any)
464     Devel::PixelBuffer pixelBuffer;///< The PixelBuffer holding the image data (May be empty after upload)
465     TextureSet textureSet;         ///< The TextureSet holding the Texture
466     VisualUrl url;                 ///< The URL of the image
467     ImageDimensions desiredSize;   ///< The size requested
468     ImageDimensions useSize;       ///< The size used
469     Vector4 atlasRect;             ///< The atlas rect used if atlased
470     TextureId textureId;           ///< The TextureId associated with this Texture
471     TextureId maskTextureId;       ///< The mask TextureId to be applied on load
472     TextureManager::TextureHash hash; ///< The hash used to cache this Texture
473     float scaleFactor;             ///< The scale factor to apply to the Texture when masking
474     int16_t referenceCount;        ///< The reference count of clients using this Texture
475     LoadState loadState:3;         ///< The load state showing the load progress of the Texture
476     FittingMode::Type fittingMode:2; ///< The requested FittingMode
477     Dali::SamplingMode::Type samplingMode:3; ///< The requested SamplingMode
478     StorageType storageType:1;     ///< CPU storage / GPU upload;
479     bool loadSynchronously:1;      ///< True if synchronous loading was requested
480     UseAtlas useAtlas:1;           ///< USE_ATLAS if an atlas was requested.
481                                    ///< This is updated to false if atlas is not used
482     bool cropToMask:1;             ///< true if the image should be cropped to the mask size.
483     bool orientationCorrection:1;  ///< true if the image should be rotated to match exif orientation data
484     bool preMultiplyOnLoad:1;      ///< true if the image's color should be multiplied by it's alpha
485     bool preMultiplied:1;          ///< true if the image's color was multiplied by it's alpha
486   };
487
488   // Structs:
489
490   /**
491    * Struct to hold information about a requested Async load.
492    * This is used to look up a TextureManager::TextureId from the returned AsyncLoad Id.
493    */
494   struct AsyncLoadingInfo
495   {
496     AsyncLoadingInfo( TextureId textureId )
497     : textureId( textureId ),
498       loadId( 0 )
499     {
500     }
501
502     TextureId           textureId;   ///< The external Texture Id assigned to this load
503     uint32_t            loadId;      ///< The load Id used by the async loader to reference this load
504   };
505
506   // Private typedefs:
507
508   typedef std::deque<AsyncLoadingInfo>  AsyncLoadingInfoContainerType;  ///< The container type used to manage Asynchronous loads in progress
509   typedef std::vector<TextureInfo>      TextureInfoContainerType;       ///< The container type used to manage the life-cycle and caching of Textures
510
511   /**
512    * @brief Used internally to initiate a load.
513    * @param[in] textureInfo The TextureInfo struct associated with the Texture
514    * @return                True if the load was initiated
515    */
516   bool LoadTexture( TextureInfo& textureInfo );
517
518   /**
519    * Add the observer to the observer list
520    * @param[in] textureInfo The TextureInfo struct associated with the texture
521    * observer The observer wishing to observe the texture upload
522    */
523   void ObserveTexture( TextureInfo & textureInfo, TextureUploadObserver* observer );
524
525   /**
526    * @brief This signal handler is called when the async local loader finishes loading.
527    * @param[in] id        This is the async image loaders Id
528    * @param[in] pixelBuffer The loaded image data
529    */
530   void AsyncLocalLoadComplete( uint32_t id, Devel::PixelBuffer pixelBuffer );
531
532   /**
533    * @brief This signal handler is called when the async local loader finishes loading.
534    * @param[in] id        This is the async image loaders Id
535    * @param[in] pixelBuffer The loaded image data
536    */
537   void AsyncRemoteLoadComplete( uint32_t id, Devel::PixelBuffer pixelBuffer );
538
539   /**
540    * Common method to handle loading completion
541    * @param[in] container The Async loading container
542    * @param[in] id        This is the async image loaders Id
543    * @param[in] pixelBuffer The loaded image data
544    */
545   void AsyncLoadComplete( AsyncLoadingInfoContainerType& container, uint32_t id, Devel::PixelBuffer pixelBuffer );
546
547   /**
548    * @brief Performs Post-Load steps including atlasing.
549    * @param[in] textureInfo The struct associated with this Texture
550    * @param[in] pixelBuffer The image pixelBuffer
551    * @return    True if successful
552    */
553   void PostLoad( TextureManager::TextureInfo& textureInfo, Devel::PixelBuffer& pixelBuffer );
554
555   /**
556    * Check if there is a texture waiting to be masked. If there
557    * is then apply this mask and upload it.
558    * @param[in] maskTextureInfo The texture info of the mask that has just loaded.
559    */
560   void CheckForWaitingTexture( TextureInfo& maskTextureInfo );
561
562   /**
563    * Apply the mask to the pixelBuffer.
564    * @param[in] pixelBuffer The pixelBuffer to apply the mask to
565    * @param[in] maskTextureId The texture id of the mask.
566    * @param[in] contentScale The factor to scale the content
567    * @param[in] cropToMask Whether to crop the content to the mask size
568    */
569   void ApplyMask( Devel::PixelBuffer& pixelBuffer, TextureId maskTextureId,
570                   float contentScale, bool cropToMask );
571
572   /**
573    * Upload the texture specified in pixelBuffer to the appropriate location
574    * @param[in] pixelBuffer The image data to upload
575    * @param[in] textureInfo The texture info containing the location to
576    * store the data to.
577    */
578   void UploadTexture( Devel::PixelBuffer& pixelBuffer, TextureInfo& textureInfo );
579
580   /**
581    * Mark the texture as complete, and inform observers
582    * @param[in] textureInfo The struct associated with this Texture
583    */
584   void UploadComplete( TextureInfo& textureInfo );
585
586   /**
587    * Notify the current observers that the texture upload is complete,
588    * then remove the observers from the list.
589    * @param[in] textureInfo The struct associated with this Texture
590    * @param[in] success If the pixel data was retrieved successfully and uploaded to GPU
591    */
592   void NotifyObservers( TextureInfo& textureInfo, bool success );
593
594   /**
595    * @brief Generates a new, unique TextureId
596    * @return A unique TextureId
597    */
598   TextureManager::TextureId GenerateUniqueTextureId();
599
600   /**
601    * @brief Used to lookup an index into the TextureInfoContainer from a TextureId
602    * @param[in] textureId The TextureId to look up
603    * @return              The cache index
604    */
605   int GetCacheIndexFromId( TextureId textureId );
606
607
608   /**
609    * @brief Generates a hash for caching based on the input parameters.
610    * Only applies size, fitting mode andsampling mode if the size is specified.
611    * Only applies maskTextureId if it isn't INVALID_TEXTURE_ID
612    * Always applies useAtlas.
613    * @param[in] url          The URL of the image to load
614    * @param[in] size         The image size
615    * @param[in] fittingMode  The FittingMode to use
616    * @param[in] samplingMode The SamplingMode to use
617    * @param[in] useAtlas     True if atlased
618    * @param[in] maskTextureId The masking texture id (or INVALID_TEXTURE_ID)
619    * @return                 A hash of the provided data for caching.
620    */
621   TextureHash GenerateHash( const std::string& url, const ImageDimensions size,
622                             const FittingMode::Type fittingMode,
623                             const Dali::SamplingMode::Type samplingMode, const UseAtlas useAtlas,
624                             TextureId maskTextureId,
625                             MultiplyOnLoad preMultiplyOnLoad);
626   /**
627    * @brief Looks up a cached texture by its hash.
628    * If found, the given parameters are used to check there is no hash-collision.
629    * @param[in] hash          The hash to look up
630    * @param[in] url           The URL of the image to load
631    * @param[in] size          The image size
632    * @param[in] fittingMode   The FittingMode to use
633    * @param[in] samplingMode  The SamplingMode to use
634    * @param[in] useAtlas      True if atlased
635    * @param[in] maskTextureId Optional texture ID to use to mask this image
636    * @return A TextureId of a cached Texture if found. Or INVALID_TEXTURE_ID if not found.
637    */
638   TextureManager::TextureId FindCachedTexture(
639     const TextureManager::TextureHash hash,
640     const std::string& url,
641     const ImageDimensions size,
642     const FittingMode::Type fittingMode,
643     const Dali::SamplingMode::Type samplingMode,
644     const bool useAtlas,
645     TextureId maskTextureId,
646     MultiplyOnLoad preMultiplyOnLoad);
647
648 private:
649
650   /**
651    * @brief Helper class to keep the relation between AsyncImageLoader and corresponding LoadingInfo container
652    */
653   class AsyncLoadingHelper : public ConnectionTracker
654   {
655   public:
656     /**
657      * @brief Create an AsyncLoadingHelper.
658      * @param[in] textureManager Reference to the texture manager
659      */
660     AsyncLoadingHelper(TextureManager& textureManager);
661
662     /**
663      * @brief Load a new texture.
664      * @param[in] textureId             TextureId to reference the texture that will be loaded
665      * @param[in] url                   The URL of the image to load
666      * @param[in] desiredSize           The size the image is likely to appear at.
667      *                                  This can be set to 0,0 for automatic
668      * @param[in] fittingMode           The FittingMode to use
669      * @param[in] samplingMode          The SamplingMode to use
670      * @param[in] orientationCorrection Whether to use image metadata to rotate or flip the image,
671      *                                  e.g., from portrait to landscape
672      */
673     void Load(TextureId textureId,
674               const VisualUrl& url,
675               ImageDimensions desiredSize,
676               FittingMode::Type fittingMode,
677               SamplingMode::Type samplingMode,
678               bool orientationCorrection);
679
680   public:
681     AsyncLoadingHelper(const AsyncLoadingHelper&) = delete;
682     AsyncLoadingHelper& operator=(const AsyncLoadingHelper&) = delete;
683
684     AsyncLoadingHelper(AsyncLoadingHelper&& rhs);
685     AsyncLoadingHelper& operator=(AsyncLoadingHelper&&rhs) = delete;
686
687   private:
688     /**
689      * @brief Main constructor that used by all other constructors
690      */
691     AsyncLoadingHelper(Toolkit::AsyncImageLoader loader,
692                        TextureManager& textureManager,
693                        AsyncLoadingInfoContainerType&& loadingInfoContainer);
694
695     /**
696      * @brief Callback to be called when texture loading is complete, it passes the pixel buffer on to texture manager.
697      * @param[in] id          Loader id
698      * @param[in] pixelBuffer Image data
699      */
700     void AsyncLoadComplete(uint32_t id, Devel::PixelBuffer pixelBuffer);
701
702   private:
703     Toolkit::AsyncImageLoader     mLoader;
704     TextureManager&               mTextureManager;
705     AsyncLoadingInfoContainerType mLoadingInfoContainer;
706   };
707
708   struct ExternalTextureInfo
709   {
710     TextureId textureId;
711     TextureSet textureSet;
712   };
713
714 private:
715
716   /**
717    * Deleted copy constructor.
718    */
719   TextureManager( const TextureManager& ) = delete;
720
721   /**
722    * Deleted assignment operator.
723    */
724   TextureManager& operator=( const TextureManager& rhs ) = delete;
725
726   /**
727    * This is called by the TextureManagerUploadObserver when an observer is destroyed.
728    * We use the callback to know when to remove an observer from our notify list.
729    * @param[in] observer The observer that generated the callback
730    */
731   void ObserverDestroyed( TextureUploadObserver* observer );
732
733 private:  // Member Variables:
734
735   TextureInfoContainerType                      mTextureInfoContainer; ///< Used to manage the life-cycle and caching of Textures
736   RoundRobinContainerView< AsyncLoadingHelper > mAsyncLocalLoaders;    ///< The Asynchronous image loaders used to provide all local async loads
737   RoundRobinContainerView< AsyncLoadingHelper > mAsyncRemoteLoaders;   ///< The Asynchronous image loaders used to provide all remote async loads
738   std::vector< ExternalTextureInfo >            mExternalTextures;     ///< Externally provided textures
739   Dali::Vector<LifecycleObserver*>              mLifecycleObservers;   ///< Lifecycle observers of texture manager
740   TextureId                                     mCurrentTextureId;     ///< The current value used for the unique Texture Id generation
741 };
742
743
744 } // name Internal
745
746 } // namespace Toolkit
747
748 } // namespace Dali
749
750 #endif // DALI_TOOLKIT_TEXTURE_MANAGER_IMPL_H