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