Move MultiplyColorByAlpha() from main thread to resource thread
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / texture-manager-impl.h
old mode 100644 (file)
new mode 100755 (executable)
index b385344..2cb06bd
@@ -104,6 +104,24 @@ public:
     LOAD_FAILED      ///< Async loading failed, e.g. connection problem
   };
 
+  /**
+   * @brief Types of reloading policies
+   */
+  enum class ReloadPolicy
+  {
+    CACHED = 0,             ///< Loads cached texture if it exists.
+    FORCED                  ///< Forces reloading of texture.
+  };
+
+  /**
+   * @brief Whether to multiply alpha into color channels on load
+   */
+  enum class MultiplyOnLoad
+  {
+    LOAD_WITHOUT_MULTIPLY = 0, ///< Don't modify the image
+    MULTIPLY_ON_LOAD           ///< Multiply alpha into color channels on load
+  };
+
 public:
 
   struct MaskingData
@@ -118,6 +136,18 @@ public:
   };
   using MaskingDataPointer = std::unique_ptr<MaskingData>;
 
+
+  /**
+   * Class to provide lifecycle event on destruction of texture manager.
+   */
+  struct LifecycleObserver
+  {
+    /**
+     * Called shortly before the texture manager is destroyed.
+     */
+    virtual void TextureManagerDestroyed() = 0;
+  };
+
   /**
    * Constructor.
    */
@@ -126,20 +156,67 @@ public:
   /**
    * Destructor.
    */
-  ~TextureManager() = default;
-
+  ~TextureManager();
 
   // TextureManager Main API:
 
-  TextureSet LoadTexture(VisualUrl& url, Dali::ImageDimensions desiredSize,
-                         Dali::FittingMode::Type fittingMode, Dali::SamplingMode::Type samplingMode,
-                         const MaskingDataPointer& maskInfo, bool synchronousLoading,
-                         TextureManager::TextureId& textureId, Vector4& textureRect,
-                         bool& atlasingStatus, bool& loadingStatus, Dali::WrapMode::Type wrapModeU,
-                         Dali::WrapMode::Type wrapModeV, TextureUploadObserver* textureObserver,
-                         AtlasUploadObserver* atlasObserver,
-                         ImageAtlasManagerPtr imageAtlasManager,
-                         bool orientationCorrection );
+  /**
+   * @brief Requests an image load of the given URL.
+   *
+   * The parameters are used to specify how the image is loaded.
+   * The observer has the UploadComplete method called when the load is ready.
+   *
+   * When the client has finished with the Texture, Remove() should be called.
+   *
+   * @param[in] url                   The URL of the image to load
+   * @param[in] desiredSize           The size the image is likely to appear at. This can be set to 0,0 for automatic
+   * @param[in] fittingMode           The FittingMode to use
+   * @param[in] samplingMode          The SamplingMode to use
+   * @param[in] maskInfo              Mask info structure
+   * @param[in] synchronousLoading    true if the URL should be loaded synchronously
+   * @param[out] textureId,           The textureId of the URL
+   * @param[out] textureRect          The rectangle within the texture atlas that this URL occupies,
+   *                                  this is the rectangle in normalized coordinates.
+   * @param[out] textureRectSize      The rectangle within the texture atlas that this URL occupies,
+   *                                  this is the same rectangle in pixels.
+   * @param[in,out] atlasingStatus    Set to USE_ATLAS to attempt atlasing. If atlasing fails, the image will still
+   *                                  be loaded, and marked successful, but this will be set to false.
+   *                                  If atlasing succeeds, this will be set to true.
+   * @param[out] loadingStatus        The loading status of the texture
+   * @param[in] wrapModeU             Horizontal Wrap mode
+   * @param[in] wrapModeV             Vertical Wrap mode
+   * @param[in] textureObserver       The client object should inherit from this and provide the "UploadCompleted" virtual.
+   *                                  This is called when an image load completes (or fails).
+   * @param[in] atlasObserver         This is used if the texture is atlased, and will be called instead of
+   *                                  textureObserver.UploadCompleted
+   * @param[in] imageAtlasManager     The atlas manager to use for atlasing textures
+   * @param[in] orientationCorrection Whether to rotate image to match embedded orientation data
+   * @param[in] reloadPolicy          Forces a reload of the texture even if already cached
+   * @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
+   *
+   * @return                          The texture set containing the image, or empty if still loading.
+   */
+
+  TextureSet LoadTexture( const VisualUrl&             url,
+                          Dali::ImageDimensions        desiredSize,
+                          Dali::FittingMode::Type      fittingMode,
+                          Dali::SamplingMode::Type     samplingMode,
+                          const MaskingDataPointer&    maskInfo,
+                          bool                         synchronousLoading,
+                          TextureManager::TextureId&   textureId,
+                          Vector4&                     textureRect,
+                          Dali::ImageDimensions&       textureRectSize,
+                          bool&                        atlasingStatus,
+                          bool&                        loadingStatus,
+                          Dali::WrapMode::Type         wrapModeU,
+                          Dali::WrapMode::Type         wrapModeV,
+                          TextureUploadObserver*       textureObserver,
+                          AtlasUploadObserver*         atlasObserver,
+                          ImageAtlasManagerPtr         imageAtlasManager,
+                          bool                         orientationCorrection,
+                          TextureManager::ReloadPolicy reloadPolicy,
+                          MultiplyOnLoad&              preMultiplyOnLoad );
 
   /**
    * @brief Requests an image load of the given URL.
@@ -158,15 +235,19 @@ public:
    * @param[in] observer              The client object should inherit from this and provide the "UploadCompleted" virtual.
    *                                  This is called when an image load completes (or fails).
    * @param[in] orientationCorrection Whether to rotate image to match embedded orientation data
+   * @param[in] reloadPolicy          Forces a reload of the texture even if already cached
+   * @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
    * @return                          A TextureId to use as a handle to reference this Texture
    */
-  TextureId RequestLoad( const VisualUrl&         url,
-                         const ImageDimensions    desiredSize,
-                         FittingMode::Type        fittingMode,
-                         Dali::SamplingMode::Type samplingMode,
-                         const UseAtlas           useAtlasing,
-                         TextureUploadObserver*   observer,
-                         bool                     orientationCorrection );
+  TextureId RequestLoad( const VisualUrl&                   url,
+                         const ImageDimensions              desiredSize,
+                         FittingMode::Type                  fittingMode,
+                         Dali::SamplingMode::Type           samplingMode,
+                         const UseAtlas                     useAtlasing,
+                         TextureUploadObserver*             observer,
+                         bool                               orientationCorrection,
+                         TextureManager::ReloadPolicy       reloadPolicy,
+                         MultiplyOnLoad&                    preMultiplyOnLoad );
 
   /**
    * @brief Requests an image load of the given URL, when the texture has
@@ -179,29 +260,39 @@ public:
    * When the client has finished with the Texture, Remove() should be called.
    *
    * @param[in] url                   The URL of the image to load
-   * @param[in] maskTextureId         The texture id of an image to mask this with (can be INVALID if no masking required)
+   * @param[in] maskTextureId         The texture id of an image to mask this with
+   *                                  (can be INVALID if no masking required)
    * @param[in] contentScale          The scale factor to apply to the image before masking
    * @param[in] desiredSize           The size the image is likely to appear at. This can be set to 0,0 for automatic
    * @param[in] fittingMode           The FittingMode to use
    * @param[in] samplingMode          The SamplingMode to use
-   * @param[in] useAtlasing           Set to USE_ATLAS to attempt atlasing. If atlasing fails, the image will still be loaded, and marked successful,
-   *                                  but "useAtlasing" will be set to false in the "UploadCompleted" callback from the TextureManagerUploadObserver.
-   * @param[in] cropToMask            Only used with masking, this will crop the scaled image to the mask size. If false, then the mask will be scaled to fit the image before being applied.
-   * @param[in] observer              The client object should inherit from this and provide the "UploadCompleted" virtual.
+   * @param[in] useAtlasing           Set to USE_ATLAS to attempt atlasing. If atlasing fails, the image will still
+   *                                  be loaded, and marked successful,
+   *                                  but "useAtlasing" will be set to false in the "UploadCompleted" callback from
+   *                                  the TextureManagerUploadObserver.
+   * @param[in] cropToMask            Only used with masking, this will crop the scaled image to the mask size.
+   *                                  If false, then the mask will be scaled to fit the image before being applied.
+   * @param[in] observer              The client object should inherit from this and provide the "UploadCompleted"
+   *                                  virtual.
    *                                  This is called when an image load completes (or fails).
    * @param[in] orientationCorrection Whether to rotate image to match embedded orientation data
+   * @param[in] reloadPolicy          Forces a reload of the texture even if already cached
+   * @param[in] preMultiplyOnLoad     True if the image color should be multiplied by it's alpha. Set to false if the
+   *                                  image has no alpha channel
    * @return                          A TextureId to use as a handle to reference this Texture
    */
-  TextureId RequestLoad( const VisualUrl&         url,
-                         TextureId                maskTextureId,
-                         float                    contentScale,
-                         const ImageDimensions    desiredSize,
-                         FittingMode::Type        fittingMode,
-                         Dali::SamplingMode::Type samplingMode,
-                         const UseAtlas           useAtlasing,
-                         bool                     cropToMask,
-                         TextureUploadObserver*   observer,
-                         bool                     orientationCorrection );
+  TextureId RequestLoad( const VisualUrl&                   url,
+                         TextureId                          maskTextureId,
+                         float                              contentScale,
+                         const ImageDimensions              desiredSize,
+                         FittingMode::Type                  fittingMode,
+                         Dali::SamplingMode::Type           samplingMode,
+                         const UseAtlas                     useAtlasing,
+                         bool                               cropToMask,
+                         TextureUploadObserver*             observer,
+                         bool                               orientationCorrection,
+                         TextureManager::ReloadPolicy       reloadPolicy,
+                         MultiplyOnLoad&                    preMultiplyOnLoad );
 
   /**
    * Requests a masking image to be loaded. This mask is not uploaded to GL,
@@ -220,9 +311,11 @@ public:
   void Remove( const TextureManager::TextureId textureId );
 
   /**
-   * Get the visualUrl associated with the texture id
+   * @brief Get the visualUrl associated with the texture id.
+   * @param[in] textureId The texture Id to get
+   * @return The visual Url associated with the texture id.
    */
-  const VisualUrl& GetVisualUrl( TextureId textureId );
+  VisualUrl GetVisualUrl( TextureId textureId );
 
   /**
    * @brief Get the current state of a texture
@@ -253,6 +346,25 @@ public:
    */
   TextureSet RemoveExternalTexture( const std::string& url );
 
+  /**
+   * Add an observer to the object.
+   * @param[in] observer The observer to add.
+   */
+  void AddObserver( TextureManager::LifecycleObserver& observer );
+
+  /**
+   * Remove an observer from the object
+   * @pre The observer has already been added.
+   * @param[in] observer The observer to remove.
+   */
+  void RemoveObserver( TextureManager::LifecycleObserver& observer );
+
+  /**
+   * @brief Set an image to be used when a visual has failed to correctly render
+   * @param[in] brokenImageUrl The broken image url.
+   */
+  void SetBrokenImageUrl(const std::string& brokenImageUrl);
+
 private:
 
   /**
@@ -266,36 +378,54 @@ private:
    * When the client has finished with the Texture, Remove() should be called.
    *
    * @param[in] url                   The URL of the image to load
-   * @param[in] maskTextureId         The texture id of an image to use as a mask. If no mask is required, then set to INVALID_TEXTURE_ID
+   * @param[in] maskTextureId         The texture id of an image to use as a mask. If no mask is required, then set
+   *                                  to INVALID_TEXTURE_ID
    * @param[in] contentScale          The scaling factor to apply to the content when masking
    * @param[in] desiredSize           The size the image is likely to appear at. This can be set to 0,0 for automatic
    * @param[in] fittingMode           The FittingMode to use
    * @param[in] samplingMode          The SamplingMode to use
-   * @param[in] useAtlasing           Set to USE_ATLAS to attempt atlasing. If atlasing fails, the image will still be loaded, and marked successful,
-   *                                  but "useAtlasing" will be set to false in the "UploadCompleted" callback from the TextureManagerUploadObserver.
-   * @param[in] cropToMask            Whether to crop the target after masking, or scale the mask to the image before masking.
+   * @param[in] useAtlasing           Set to USE_ATLAS to attempt atlasing. If atlasing fails, the image will still be
+   *                                  loaded, and marked successful, but "useAtlasing" will be set to false in the
+   *                                  "UploadCompleted" callback from the TextureManagerUploadObserver.
+   * @param[in] cropToMask            Whether to crop the target after masking, or scale the mask to the image before
+   *                                  masking.
    * @param[in] storageType,          Whether the pixel data is stored in the cache or uploaded to the GPU
-   * @param[in] observer              The client object should inherit from this and provide the "UploadCompleted" virtual.
+   * @param[in] observer              The client object should inherit from this and provide the "UploadCompleted"
+   *                                  virtual.
    *                                  This is called when an image load completes (or fails).
    * @param[in] orientationCorrection Whether to rotate image to match embedded orientation data
+   * @param[in] reloadPolicy          Forces a reload of the texture even if already cached
+   * @param[in] preMultiplyOnLoad     True if the image color should be multiplied by it's alpha. Set to false if
+   *                                  there is no alpha
    * @return                          A TextureId to use as a handle to reference this Texture
    */
   TextureId RequestLoadInternal(
-    const VisualUrl&         url,
-    TextureId                maskTextureId,
-    float                    contentScale,
-    const ImageDimensions    desiredSize,
-    FittingMode::Type        fittingMode,
-    Dali::SamplingMode::Type samplingMode,
-    UseAtlas                 useAtlas,
-    bool                     cropToMask,
-    StorageType              storageType,
-    TextureUploadObserver*   observer,
-    bool                     orientationCorrection );
+    const VisualUrl&                    url,
+    TextureId                           maskTextureId,
+    float                               contentScale,
+    const ImageDimensions               desiredSize,
+    FittingMode::Type                   fittingMode,
+    Dali::SamplingMode::Type            samplingMode,
+    UseAtlas                            useAtlas,
+    bool                                cropToMask,
+    StorageType                         storageType,
+    TextureUploadObserver*              observer,
+    bool                                orientationCorrection,
+    TextureManager::ReloadPolicy        reloadPolicy,
+    MultiplyOnLoad&                     preMultiplyOnLoad );
 
+  /**
+   * @brief Get the current state of a texture
+   * @param[in] textureId The texture id to query
+   * @return The loading state if the texture is valid, or NOT_STARTED if the textureId
+   * is not valid.
+   */
+  LoadState GetTextureStateInternal( TextureId textureId );
 
   typedef size_t TextureHash; ///< The type used to store the hash used for Texture caching.
 
+  // Structs:
+
   /**
    * @brief This struct is used to manage the life-cycle of Texture loading and caching.
    */
@@ -312,7 +442,8 @@ private:
                  bool cropToMask,
                  UseAtlas useAtlas,
                  TextureManager::TextureHash hash,
-                 bool orientationCorrection )
+                 bool orientationCorrection,
+                 bool preMultiplyOnLoad )
     : url( url ),
       desiredSize( desiredSize ),
       useSize( desiredSize ),
@@ -329,7 +460,9 @@ private:
       loadSynchronously( loadSynchronously ),
       useAtlas( useAtlas ),
       cropToMask( cropToMask ),
-      orientationCorrection( true )
+      orientationCorrection( true ),
+      preMultiplyOnLoad( preMultiplyOnLoad ),
+      preMultiplied( false )
     {
     }
 
@@ -351,17 +484,33 @@ private:
     TextureManager::TextureHash hash; ///< The hash used to cache this Texture
     float scaleFactor;             ///< The scale factor to apply to the Texture when masking
     int16_t referenceCount;        ///< The reference count of clients using this Texture
-    LoadState loadState:3;         ///< The load state showing the load progress of the Texture
+    LoadState loadState:4;         ///< The load state showing the load progress of the Texture
     FittingMode::Type fittingMode:2; ///< The requested FittingMode
     Dali::SamplingMode::Type samplingMode:3; ///< The requested SamplingMode
-    StorageType storageType:1;     ///< CPU storage / GPU upload;
+    StorageType storageType:2;     ///< CPU storage / GPU upload;
     bool loadSynchronously:1;      ///< True if synchronous loading was requested
-    UseAtlas useAtlas:1;           ///< USE_ATLAS if an atlas was requested. This is updated to false if atlas is not used
+    UseAtlas useAtlas:2;           ///< USE_ATLAS if an atlas was requested.
+                                   ///< This is updated to false if atlas is not used
     bool cropToMask:1;             ///< true if the image should be cropped to the mask size.
     bool orientationCorrection:1;  ///< true if the image should be rotated to match exif orientation data
+    bool preMultiplyOnLoad:1;      ///< true if the image's color should be multiplied by it's alpha
+    bool preMultiplied:1;          ///< true if the image's color was multiplied by it's alpha
   };
 
-  // Structs:
+  /**
+   * Structure to hold info about a texture load queued during NotifyObservers
+   */
+  struct LoadQueueElement
+  {
+    LoadQueueElement( TextureId textureId, TextureUploadObserver* observer )
+    : mTextureId( textureId ),
+      mObserver( observer )
+    {
+    }
+
+    TextureId mTextureId; ///< The texture id of the requested load.
+    TextureUploadObserver* mObserver; ///< Observer of texture load.
+  };
 
   /**
    * Struct to hold information about a requested Async load.
@@ -385,16 +534,35 @@ private:
   typedef std::vector<TextureInfo>      TextureInfoContainerType;       ///< The container type used to manage the life-cycle and caching of Textures
 
   /**
+   * @brief Initiate a load or queue load if NotifyObservers is invoking callbacks
+   * @param[in] textureInfo The TextureInfo struct associated with the Texture
+   * @param[in] observer The observer wishing to observe the texture upload
+   */
+  void LoadOrQueueTexture( TextureInfo& textureInfo, TextureUploadObserver* observer );
+
+  /**
+   * @brief Queue a texture load to be subsequently handled by ProcessQueuedTextures.
+   * @param[in] textureInfo The TextureInfo struct associated with the Texture
+   * @param[in] observer The observer wishing to observe the texture upload
+   */
+  void QueueLoadTexture( TextureInfo& textureInfo, TextureUploadObserver* observer );
+
+  /**
    * @brief Used internally to initiate a load.
    * @param[in] textureInfo The TextureInfo struct associated with the Texture
-   * @return                True if the load was initiated
+   * @param[in] observer The observer wishing to observe the texture upload
    */
-  bool LoadTexture( TextureInfo& textureInfo );
+  void LoadTexture( TextureInfo& textureInfo, TextureUploadObserver* observer );
+
+  /**
+   * @brief Initiate load of textures queued whilst NotifyObservers invoking callbacks.
+   */
+  void ProcessQueuedTextures();
 
   /**
    * Add the observer to the observer list
    * @param[in] textureInfo The TextureInfo struct associated with the texture
-   * observer The observer wishing to observe the texture upload
+   * @param[in] observer The observer wishing to observe the texture upload
    */
   void ObserveTexture( TextureInfo & textureInfo, TextureUploadObserver* observer );
 
@@ -498,6 +666,7 @@ private:
                             const FittingMode::Type fittingMode,
                             const Dali::SamplingMode::Type samplingMode, const UseAtlas useAtlas,
                             TextureId maskTextureId );
+
   /**
    * @brief Looks up a cached texture by its hash.
    * If found, the given parameters are used to check there is no hash-collision.
@@ -517,7 +686,8 @@ private:
     const FittingMode::Type fittingMode,
     const Dali::SamplingMode::Type samplingMode,
     const bool useAtlas,
-    TextureId maskTextureId );
+    TextureId maskTextureId,
+    MultiplyOnLoad preMultiplyOnLoad);
 
 private:
 
@@ -537,17 +707,21 @@ private:
      * @brief Load a new texture.
      * @param[in] textureId             TextureId to reference the texture that will be loaded
      * @param[in] url                   The URL of the image to load
-     * @param[in] desiredSize           The size the image is likely to appear at. This can be set to 0,0 for automatic
+     * @param[in] desiredSize           The size the image is likely to appear at.
+     *                                  This can be set to 0,0 for automatic
      * @param[in] fittingMode           The FittingMode to use
      * @param[in] samplingMode          The SamplingMode to use
-     * @param[in] orientationCorrection Whether to use image metadata to rotate or flip the image, e.g., from portrait to landscape
+     * @param[in] orientationCorrection Whether to use image metadata to rotate or flip the image,
+     *                                  e.g., from portrait to landscape
+     * @param[in] preMultiplyOnLoad     if the image's color should be multiplied by it's alpha.
      */
     void Load(TextureId textureId,
               const VisualUrl& url,
               ImageDimensions desiredSize,
               FittingMode::Type fittingMode,
               SamplingMode::Type samplingMode,
-              bool orientationCorrection);
+              bool orientationCorrection,
+              DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad);
 
   public:
     AsyncLoadingHelper(const AsyncLoadingHelper&) = delete;
@@ -608,8 +782,11 @@ private:  // Member Variables:
   RoundRobinContainerView< AsyncLoadingHelper > mAsyncLocalLoaders;    ///< The Asynchronous image loaders used to provide all local async loads
   RoundRobinContainerView< AsyncLoadingHelper > mAsyncRemoteLoaders;   ///< The Asynchronous image loaders used to provide all remote async loads
   std::vector< ExternalTextureInfo >            mExternalTextures;     ///< Externally provided textures
+  Dali::Vector<LifecycleObserver*>              mLifecycleObservers;   ///< Lifecycle observers of texture manager
+  Dali::Vector<LoadQueueElement>                mLoadQueue;            ///< Queue of textures to load after NotifyObservers
+  std::string                                   mBrokenImageUrl;       ///< Broken image url
   TextureId                                     mCurrentTextureId;     ///< The current value used for the unique Texture Id generation
-
+  bool                                          mQueueLoadFlag;        ///< Flag that causes Load Textures to be queued.
 };