1 #ifndef DALI_TOOLKIT_NPATCH_LOADER_H
2 #define DALI_TOOLKIT_NPATCH_LOADER_H
5 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 #include <utility> // for std::pair
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>
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.
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.
49 class NPatchLoader : public Integration::Processor
58 * Destructor, non-virtual as not a base class
63 * @brief Retrieve a texture matching the n-patch url.
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.
74 NPatchData::NPatchDataId Load(TextureManager& textureManager, TextureUploadObserver* textureObserver, const VisualUrl& url, const Rect<int>& border, bool& preMultiplyOnLoad, bool synchronousLoading);
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
82 bool GetNPatchData(const NPatchData::NPatchDataId id, std::shared_ptr<const NPatchData>& data);
85 * @brief Request remove a texture matching id.
86 * Erase the observer from the observer list of cache if we need.
88 * @param [in] id cache data id
89 * @param [in] textureObserver The NPatchVisual that requested loading.
91 void RequestRemove(NPatchData::NPatchDataId id, TextureUploadObserver* textureObserver);
93 protected: // Implementation of Processor
95 * @copydoc Dali::Integration::Processor::Process()
97 void Process(bool postProcessor) override;
100 NPatchData::NPatchDataId GenerateUniqueNPatchDataId();
102 int32_t GetCacheIndexFromId(const NPatchData::NPatchDataId id);
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.
110 * @param [in] id cache data id
111 * @param [in] textureObserver The NPatchVisual that requested loading.
113 void Remove(NPatchData::NPatchDataId id, TextureUploadObserver* textureObserver);
117 * @brief Information of NPatchData
118 * It also hold ownership of NPatchData memory.
122 NPatchInfo(std::shared_ptr<NPatchData> data)
130 NPatchInfo(NPatchInfo&& info) noexcept // move constructor
132 mData = std::move(info.mData);
133 mReferenceCount = info.mReferenceCount;
134 info.mReferenceCount = 0u;
136 NPatchInfo& operator=(NPatchInfo&& info) noexcept // move operator
138 mData = std::move(info.mData);
139 mReferenceCount = info.mReferenceCount;
140 info.mReferenceCount = 0u;
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
148 std::shared_ptr<NPatchData> mData;
149 std::int16_t mReferenceCount; ///< The number of N-patch visuals that use this data.
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.
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.
162 std::shared_ptr<NPatchData> GetNPatchData(const VisualUrl& url, const Rect<int>& border, bool& preMultiplyOnLoad);
166 * Undefined copy constructor.
168 NPatchLoader(const NPatchLoader&);
171 * Undefined assignment operator.
173 NPatchLoader& operator=(const NPatchLoader& rhs);
176 NPatchData::NPatchDataId mCurrentNPatchDataId;
177 std::vector<NPatchInfo> mCache;
179 std::vector<std::pair<NPatchData::NPatchDataId, TextureUploadObserver*>> mRemoveQueue; ///< Queue of textures to remove at PostProcess. It will be cleared after PostProcess.
181 bool mRemoveProcessorRegistered : 1; ///< Flag if remove processor registered or not.
184 } // namespace Internal
186 } // namespace Toolkit
190 #endif // DALI_TOOLKIT_NPATCH_LOADER_H