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 NPatchData::NPatchDataId GenerateUniqueNPatchDataId();
101 int32_t GetCacheIndexFromId(const NPatchData::NPatchDataId id);
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.
109 * @param [in] id cache data id
110 * @param [in] textureObserver The NPatchVisual that requested loading.
112 void Remove(NPatchData::NPatchDataId id, TextureUploadObserver* textureObserver);
116 * @brief Information of NPatchData
117 * It also hold ownership of NPatchData memory.
121 NPatchInfo(NPatchDataPtr data)
129 NPatchInfo(NPatchInfo&& info) noexcept // move constructor
131 mData = std::move(info.mData);
132 mReferenceCount = info.mReferenceCount;
133 info.mReferenceCount = 0u;
135 NPatchInfo& operator=(NPatchInfo&& info) noexcept // move operator
137 mData = std::move(info.mData);
138 mReferenceCount = info.mReferenceCount;
139 info.mReferenceCount = 0u;
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
148 std::int16_t mReferenceCount; ///< The number of N-patch visuals that use this data.
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.
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.
161 NPatchDataPtr GetNPatchData(const VisualUrl& url, const Rect<int>& border, bool& preMultiplyOnLoad);
165 * Undefined copy constructor.
167 NPatchLoader(const NPatchLoader&);
170 * Undefined assignment operator.
172 NPatchLoader& operator=(const NPatchLoader& rhs);
175 NPatchData::NPatchDataId mCurrentNPatchDataId;
176 std::vector<NPatchInfo> mCache;
178 std::vector<std::pair<NPatchData::NPatchDataId, TextureUploadObserver*>> mRemoveQueue; ///< Queue of textures to remove at PostProcess. It will be cleared after PostProcess.
180 bool mRemoveProcessorRegistered : 1; ///< Flag if remove processor registered or not.
183 } // namespace Internal
185 } // namespace Toolkit
189 #endif // DALI_TOOLKIT_NPATCH_LOADER_H