[dali_2.2.48] 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 private:
99   NPatchData::NPatchDataId GenerateUniqueNPatchDataId();
100
101   int32_t GetCacheIndexFromId(const NPatchData::NPatchDataId id);
102
103   /**
104    * @brief Remove a texture matching id.
105    * Erase the observer from the observer list of cache if we need.
106    * This API decrease cached NPatchInfo reference.
107    * If the NPatchInfo reference become 0, the textureSet will be reset.
108    *
109    * @param [in] id cache data id
110    * @param [in] textureObserver The NPatchVisual that requested loading.
111    */
112   void Remove(NPatchData::NPatchDataId id, TextureUploadObserver* textureObserver);
113
114 private:
115   /**
116    * @brief Information of NPatchData
117    * It also hold ownership of NPatchData memory.
118    */
119   struct NPatchInfo
120   {
121     NPatchInfo(NPatchDataPtr data)
122     : mData(data),
123       mReferenceCount(1u)
124     {
125     }
126     ~NPatchInfo()
127     {
128     }
129     NPatchInfo(NPatchInfo&& info) noexcept // move constructor
130     {
131       mData                = std::move(info.mData);
132       mReferenceCount      = info.mReferenceCount;
133       info.mReferenceCount = 0u;
134     }
135     NPatchInfo& operator=(NPatchInfo&& info) noexcept // move operator
136     {
137       mData                = std::move(info.mData);
138       mReferenceCount      = info.mReferenceCount;
139       info.mReferenceCount = 0u;
140       return *this;
141     }
142
143     NPatchInfo()                       = delete;            // Do not use default constructor
144     NPatchInfo(const NPatchInfo& info) = delete;            // Do not use copy constructor
145     NPatchInfo& operator=(const NPatchInfo& info) = delete; // Do not use copy assign
146
147     NPatchDataPtr mData;
148     std::int16_t  mReferenceCount; ///< The number of N-patch visuals that use this data.
149   };
150
151   /**
152    * @brief Get cached NPatchData by inputed url and border. If there is no cached data, create new one.
153    * @note This API increase cached NPatchInfo reference.
154    *
155    * @param [in] url to retrieve
156    * @param [in] border The border size of the image
157    * @param [in,out] preMultiplyOnLoad True if the image color should be multiplied by it's alpha. Set to false if the
158    *                                   image has no alpha channel
159    * @return NPatchData pointer that Load function will used.
160    */
161   NPatchDataPtr GetNPatchData(const VisualUrl& url, const Rect<int>& border, bool& preMultiplyOnLoad);
162
163 protected:
164   /**
165    * Undefined copy constructor.
166    */
167   NPatchLoader(const NPatchLoader&);
168
169   /**
170    * Undefined assignment operator.
171    */
172   NPatchLoader& operator=(const NPatchLoader& rhs);
173
174 private:
175   NPatchData::NPatchDataId mCurrentNPatchDataId;
176   std::vector<NPatchInfo>  mCache;
177
178   std::vector<std::pair<NPatchData::NPatchDataId, TextureUploadObserver*>> mRemoveQueue; ///< Queue of textures to remove at PostProcess. It will be cleared after PostProcess.
179
180   bool mRemoveProcessorRegistered : 1; ///< Flag if remove processor registered or not.
181 };
182
183 } // namespace Internal
184
185 } // namespace Toolkit
186
187 } // namespace Dali
188
189 #endif // DALI_TOOLKIT_NPATCH_LOADER_H