[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / npatch-loader.h
1 #ifndef DALI_TOOLKIT_NPATCH_LOADER_H
2 #define DALI_TOOLKIT_NPATCH_LOADER_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/pixel-buffer.h>
22 #include <dali/integration-api/processor-interface.h>
23 #include <dali/public-api/rendering/texture-set.h>
24 #include <string>
25 #include <utility> // for std::pair
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/devel-api/utility/npatch-utilities.h>
29 #include <dali-toolkit/internal/texture-manager/texture-manager-impl.h>
30 #include <dali-toolkit/internal/visuals/npatch-data.h>
31 #include <dali-toolkit/internal/visuals/visual-url.h>
32
33 namespace Dali
34 {
35 namespace Toolkit
36 {
37 namespace Internal
38 {
39 /**
40  * The manager for loading Npatch textures.
41  * It caches them internally for better performance; i.e. to avoid loading and
42  * parsing the files over and over.
43  *
44  * Cache is not cleaned during app lifecycle as N patches take considerably
45  * small space and there's not usually a lot of them. Usually N patches are specified in
46  * toolkit default style and there is 1-2 per control that are shared across the whole application.
47  */
48 class NPatchLoader : public Integration::Processor
49 {
50 public:
51   /**
52    * Constructor
53    */
54   NPatchLoader();
55
56   /**
57    * Destructor, non-virtual as not a base class
58    */
59   ~NPatchLoader();
60
61   /**
62    * @brief Retrieve a texture matching the n-patch url.
63    *
64    * @param [in] textureManager that will be used to loading image
65    * @param [in] textureObserver The NPatchVisual that requested loading.
66    * @param [in] url to retrieve
67    * @param [in] border The border size of the image
68    * @param [in,out] preMultiplyOnLoad True if the image color should be multiplied by it's alpha. Set to false if the
69    *                                   image has no alpha channel
70    * @param [in] synchronousLoading True if the image will be loaded in synchronous time.
71    * @return id of the texture.
72    */
73   NPatchData::NPatchDataId Load(TextureManager& textureManager, TextureUploadObserver* textureObserver, const VisualUrl& url, const Rect<int>& border, bool& preMultiplyOnLoad, bool synchronousLoading);
74
75   /**
76    * @brief Retrieve N patch data matching to an id
77    * @param [in] id of data
78    * @param [out] data const pointer to the NPatchData
79    * @return true if data matching to id was really found
80    */
81   bool GetNPatchData(const NPatchData::NPatchDataId id, NPatchDataPtr& data);
82
83   /**
84    * @brief Request remove a texture matching id.
85    * Erase the observer from the observer list of cache if we need.
86    *
87    * @param [in] id cache data id
88    * @param [in] textureObserver The NPatchVisual that requested loading.
89    */
90   void RequestRemove(NPatchData::NPatchDataId id, TextureUploadObserver* textureObserver);
91
92 protected: // Implementation of Processor
93   /**
94    * @copydoc Dali::Integration::Processor::Process()
95    */
96   void Process(bool postProcessor) override;
97
98   /**
99    * @copydoc Dali::Integration::Processor::GetProcessorName()
100    */
101   std::string_view GetProcessorName() const override
102   {
103     return "NPatchLoader";
104   }
105
106 private:
107   NPatchData::NPatchDataId GenerateUniqueNPatchDataId();
108
109   int32_t GetCacheIndexFromId(const NPatchData::NPatchDataId id);
110
111   /**
112    * @brief Remove a texture matching id.
113    * Erase the observer from the observer list of cache if we need.
114    * This API decrease cached NPatchInfo reference.
115    * If the NPatchInfo reference become 0, the textureSet will be reset.
116    *
117    * @param [in] id cache data id
118    * @param [in] textureObserver The NPatchVisual that requested loading.
119    */
120   void Remove(NPatchData::NPatchDataId id, TextureUploadObserver* textureObserver);
121
122 private:
123   /**
124    * @brief Information of NPatchData
125    * It also hold ownership of NPatchData memory.
126    */
127   struct NPatchInfo
128   {
129     NPatchInfo(NPatchDataPtr data)
130     : mData(data),
131       mReferenceCount(1u)
132     {
133     }
134     ~NPatchInfo()
135     {
136     }
137     NPatchInfo(NPatchInfo&& info) noexcept // move constructor
138     {
139       mData                = std::move(info.mData);
140       mReferenceCount      = info.mReferenceCount;
141       info.mReferenceCount = 0u;
142     }
143     NPatchInfo& operator=(NPatchInfo&& info) noexcept // move operator
144     {
145       mData                = std::move(info.mData);
146       mReferenceCount      = info.mReferenceCount;
147       info.mReferenceCount = 0u;
148       return *this;
149     }
150
151     NPatchInfo()                       = delete;            // Do not use default constructor
152     NPatchInfo(const NPatchInfo& info) = delete;            // Do not use copy constructor
153     NPatchInfo& operator=(const NPatchInfo& info) = delete; // Do not use copy assign
154
155     NPatchDataPtr mData;
156     std::int16_t  mReferenceCount; ///< The number of N-patch visuals that use this data.
157   };
158
159   /**
160    * @brief Get cached NPatchData by inputed url and border. If there is no cached data, create new one.
161    * @note This API increase cached NPatchInfo reference.
162    *
163    * @param [in] url to retrieve
164    * @param [in] border The border size of the image
165    * @param [in,out] preMultiplyOnLoad True if the image color should be multiplied by it's alpha. Set to false if the
166    *                                   image has no alpha channel
167    * @return NPatchData pointer that Load function will used.
168    */
169   NPatchDataPtr GetNPatchData(const VisualUrl& url, const Rect<int>& border, bool& preMultiplyOnLoad);
170
171 protected:
172   /**
173    * Undefined copy constructor.
174    */
175   NPatchLoader(const NPatchLoader&);
176
177   /**
178    * Undefined assignment operator.
179    */
180   NPatchLoader& operator=(const NPatchLoader& rhs);
181
182 private:
183   NPatchData::NPatchDataId mCurrentNPatchDataId;
184   std::vector<NPatchInfo>  mCache;
185
186   std::vector<std::pair<NPatchData::NPatchDataId, TextureUploadObserver*>> mRemoveQueue; ///< Queue of textures to remove at PostProcess. It will be cleared after PostProcess.
187
188   bool mRemoveProcessorRegistered : 1; ///< Flag if remove processor registered or not.
189 };
190
191 } // namespace Internal
192
193 } // namespace Toolkit
194
195 } // namespace Dali
196
197 #endif // DALI_TOOLKIT_NPATCH_LOADER_H