[dali_2.3.19] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / npatch-loader.h
index 58bd73f..fb66fcc 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_TOOLKIT_NPATCH_LOADER_H
 
 /*
- * Copyright (c) 2020 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  */
 
 // EXTERNAL INCLUDES
-#include <string>
-#include <dali/public-api/rendering/texture-set.h>
-#include <dali/devel-api/common/owner-container.h>
 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
+#include <dali/integration-api/processor-interface.h>
+#include <dali/public-api/rendering/texture-set.h>
+#include <string>
+#include <utility> // for std::pair
 
 // INTERNAL INCLUDES
-#include <dali-toolkit/internal/visuals/texture-manager-impl.h>
 #include <dali-toolkit/devel-api/utility/npatch-utilities.h>
+#include <dali-toolkit/internal/texture-manager/texture-manager-impl.h>
+#include <dali-toolkit/internal/visuals/npatch-data.h>
+#include <dali-toolkit/internal/visuals/visual-url.h>
 
 namespace Dali
 {
-
 namespace Toolkit
 {
-
 namespace Internal
 {
-
 /**
  * The manager for loading Npatch textures.
  * It caches them internally for better performance; i.e. to avoid loading and
@@ -45,40 +45,9 @@ namespace Internal
  * small space and there's not usually a lot of them. Usually N patches are specified in
  * toolkit default style and there is 1-2 per control that are shared across the whole application.
  */
-class NPatchLoader
+class NPatchLoader : public Integration::Processor
 {
 public:
-
-  enum
-  {
-    UNINITIALIZED_ID = 0 ///< uninitialised id, use to initialize ids
-  };
-
-  struct Data
-  {
-    Data()
-    : url(),
-      textureSet(),
-      hash( 0 ),
-      croppedWidth( 0 ),
-      croppedHeight( 0 ),
-      border( 0, 0, 0, 0 ),
-      loadCompleted( false )
-    {}
-
-    std::string url;                              ///< Url of the N-Patch
-    TextureSet textureSet;                        ///< Texture containing the cropped image
-    NPatchUtility::StretchRanges stretchPixelsX;  ///< X stretch pixels
-    NPatchUtility::StretchRanges stretchPixelsY;  ///< Y stretch pixels
-    std::size_t hash;                             ///< Hash code for the Url
-    uint32_t croppedWidth;                        ///< Width of the cropped middle part of N-patch
-    uint32_t croppedHeight;                       ///< Height of the cropped middle part of N-patch
-    Rect< int > border;                           ///< The size of the border
-    bool loadCompleted;                           ///< True if the data loading is completed
-  };
-
-public:
-
   /**
    * Constructor
    */
@@ -101,26 +70,105 @@ public:
    * @param [in] synchronousLoading True if the image will be loaded in synchronous time.
    * @return id of the texture.
    */
-  std::size_t Load( TextureManager& textureManager, TextureUploadObserver* textureObserver, const std::string& url, const Rect< int >& border, bool& preMultiplyOnLoad, bool synchronousLoading );
+  NPatchData::NPatchDataId Load(TextureManager& textureManager, TextureUploadObserver* textureObserver, const VisualUrl& url, const Rect<int>& border, bool& preMultiplyOnLoad, bool synchronousLoading);
 
   /**
-   * @brief Set loaded PixelBuffer and its information
+   * @brief Retrieve N patch data matching to an id
+   * @param [in] id of data
+   * @param [out] data const pointer to the NPatchData
+   * @return true if data matching to id was really found
+   */
+  bool GetNPatchData(const NPatchData::NPatchDataId id, NPatchDataPtr& data);
+
+  /**
+   * @brief Request remove a texture matching id.
+   * Erase the observer from the observer list of cache if we need.
    *
    * @param [in] id cache data id
-   * @param [in] pixelBuffer of loaded image
+   * @param [in] textureObserver The NPatchVisual that requested loading.
    */
-  void SetNPatchData( std::size_t id, Devel::PixelBuffer& pixelBuffer );
+  void RequestRemove(NPatchData::NPatchDataId id, TextureUploadObserver* textureObserver);
 
+protected: // Implementation of Processor
   /**
-   * @brief Retrieve N patch data matching to an id
-   * @param [in] id of data
-   * @param [out] data const pointer to the data
-   * @return true if data matching to id was really found
+   * @copydoc Dali::Integration::Processor::Process()
    */
-  bool GetNPatchData( std::size_t id, const Data*& data );
+  void Process(bool postProcessor) override;
 
-protected:
+  /**
+   * @copydoc Dali::Integration::Processor::GetProcessorName()
+   */
+  std::string_view GetProcessorName() const override
+  {
+    return "NPatchLoader";
+  }
+
+private:
+  NPatchData::NPatchDataId GenerateUniqueNPatchDataId();
+
+  int32_t GetCacheIndexFromId(const NPatchData::NPatchDataId id);
+
+  /**
+   * @brief Remove a texture matching id.
+   * Erase the observer from the observer list of cache if we need.
+   * This API decrease cached NPatchInfo reference.
+   * If the NPatchInfo reference become 0, the textureSet will be reset.
+   *
+   * @param [in] id cache data id
+   * @param [in] textureObserver The NPatchVisual that requested loading.
+   */
+  void Remove(NPatchData::NPatchDataId id, TextureUploadObserver* textureObserver);
 
+private:
+  /**
+   * @brief Information of NPatchData
+   * It also hold ownership of NPatchData memory.
+   */
+  struct NPatchInfo
+  {
+    NPatchInfo(NPatchDataPtr data)
+    : mData(data),
+      mReferenceCount(1u)
+    {
+    }
+    ~NPatchInfo()
+    {
+    }
+    NPatchInfo(NPatchInfo&& info) noexcept // move constructor
+    {
+      mData                = std::move(info.mData);
+      mReferenceCount      = info.mReferenceCount;
+      info.mReferenceCount = 0u;
+    }
+    NPatchInfo& operator=(NPatchInfo&& info) noexcept // move operator
+    {
+      mData                = std::move(info.mData);
+      mReferenceCount      = info.mReferenceCount;
+      info.mReferenceCount = 0u;
+      return *this;
+    }
+
+    NPatchInfo()                       = delete;            // Do not use default constructor
+    NPatchInfo(const NPatchInfo& info) = delete;            // Do not use copy constructor
+    NPatchInfo& operator=(const NPatchInfo& info) = delete; // Do not use copy assign
+
+    NPatchDataPtr mData;
+    std::int16_t  mReferenceCount; ///< The number of N-patch visuals that use this data.
+  };
+
+  /**
+   * @brief Get cached NPatchData by inputed url and border. If there is no cached data, create new one.
+   * @note This API increase cached NPatchInfo reference.
+   *
+   * @param [in] url to retrieve
+   * @param [in] border The border size of the image
+   * @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 NPatchData pointer that Load function will used.
+   */
+  NPatchDataPtr GetNPatchData(const VisualUrl& url, const Rect<int>& border, bool& preMultiplyOnLoad);
+
+protected:
   /**
    * Undefined copy constructor.
    */
@@ -132,12 +180,15 @@ protected:
   NPatchLoader& operator=(const NPatchLoader& rhs);
 
 private:
+  NPatchData::NPatchDataId mCurrentNPatchDataId;
+  std::vector<NPatchInfo>  mCache;
 
-  OwnerContainer< Data* > mCache;
+  std::vector<std::pair<NPatchData::NPatchDataId, TextureUploadObserver*>> mRemoveQueue; ///< Queue of textures to remove at PostProcess. It will be cleared after PostProcess.
 
+  bool mRemoveProcessorRegistered : 1; ///< Flag if remove processor registered or not.
 };
 
-} // name Internal
+} // namespace Internal
 
 } // namespace Toolkit