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>
25 #include <utility> // for std::pair
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>
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.
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.
48 class NPatchLoader : public Integration::Processor
57 * Destructor, non-virtual as not a base class
62 * @brief Retrieve a texture matching the n-patch url.
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.
73 NPatchData::NPatchDataId Load(TextureManager& textureManager, TextureUploadObserver* textureObserver, const VisualUrl& url, const Rect<int>& border, bool& preMultiplyOnLoad, bool synchronousLoading);
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
81 bool GetNPatchData(const NPatchData::NPatchDataId id, NPatchDataPtr& data);
84 * @brief Request remove a texture matching id.
85 * Erase the observer from the observer list of cache if we need.
87 * @param [in] id cache data id
88 * @param [in] textureObserver The NPatchVisual that requested loading.
90 void RequestRemove(NPatchData::NPatchDataId id, TextureUploadObserver* textureObserver);
92 protected: // Implementation of Processor
94 * @copydoc Dali::Integration::Processor::Process()
96 void Process(bool postProcessor) override;
99 * @copydoc Dali::Integration::Processor::GetProcessorName()
101 std::string_view GetProcessorName() const override
103 return "NPatchLoader";
107 NPatchData::NPatchDataId GenerateUniqueNPatchDataId();
109 int32_t GetCacheIndexFromId(const NPatchData::NPatchDataId id);
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.
117 * @param [in] id cache data id
118 * @param [in] textureObserver The NPatchVisual that requested loading.
120 void Remove(NPatchData::NPatchDataId id, TextureUploadObserver* textureObserver);
124 * @brief Information of NPatchData
125 * It also hold ownership of NPatchData memory.
129 NPatchInfo(NPatchDataPtr data)
137 NPatchInfo(NPatchInfo&& info) noexcept // move constructor
139 mData = std::move(info.mData);
140 mReferenceCount = info.mReferenceCount;
141 info.mReferenceCount = 0u;
143 NPatchInfo& operator=(NPatchInfo&& info) noexcept // move operator
145 mData = std::move(info.mData);
146 mReferenceCount = info.mReferenceCount;
147 info.mReferenceCount = 0u;
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
156 std::int16_t mReferenceCount; ///< The number of N-patch visuals that use this data.
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.
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.
169 NPatchDataPtr GetNPatchData(const VisualUrl& url, const Rect<int>& border, bool& preMultiplyOnLoad);
173 * Undefined copy constructor.
175 NPatchLoader(const NPatchLoader&);
178 * Undefined assignment operator.
180 NPatchLoader& operator=(const NPatchLoader& rhs);
183 NPatchData::NPatchDataId mCurrentNPatchDataId;
184 std::vector<NPatchInfo> mCache;
186 std::vector<std::pair<NPatchData::NPatchDataId, TextureUploadObserver*>> mRemoveQueue; ///< Queue of textures to remove at PostProcess. It will be cleared after PostProcess.
188 bool mRemoveProcessorRegistered : 1; ///< Flag if remove processor registered or not.
191 } // namespace Internal
193 } // namespace Toolkit
197 #endif // DALI_TOOLKIT_NPATCH_LOADER_H