1 #ifndef DALI_TOOLKIT_NPATCH_DATA_H
2 #define DALI_TOOLKIT_NPATCH_DATA_H
5 * Copyright (c) 2021 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/public-api/rendering/texture-set.h>
26 #include <dali-toolkit/devel-api/utility/npatch-utilities.h>
27 #include <dali-toolkit/internal/visuals/texture-manager-impl.h>
35 class NPatchData : public Dali::Toolkit::TextureUploadObserver
38 typedef int32_t NPatchDataId; ///< The NPatchDataId type. This is used as a handle to refer to a particular Npatch Data.
39 static const int INVALID_NPATCH_DATA_ID = -1; ///< Used to represent a null TextureId or error
42 * @brief Loading State of the NPatch image.
44 enum class LoadingState
46 LOADING = 0, ///< NPatch is on loading.
47 LOAD_COMPLETE, ///< NPatch loading is completed successfully.
48 LOAD_FAILED ///< NPatch loading is failed.
58 * Destructor, non-virtual as not a base class
64 * @brief Set cache data id.
66 * @param [in] id cache data id
68 void SetId(const NPatchDataId id);
71 * @brief Retrieve cache data id
73 * @return cache data id.
75 NPatchDataId GetId() const;
78 * @brief Add TextureUploadObserver that uses the image of this cache data.
80 * @param [in] textureObserver the TextureUploadObserver that uses the image of this cache data.
82 void AddObserver(TextureUploadObserver* textureObserver);
85 * @brief Remove TextureUploadObserver.
87 * @param [in] textureObserver the TextureUploadObserver that will be removed in this cache data.
89 void RemoveObserver(TextureUploadObserver* textureObserver);
92 * @brief Retrieve the number of observer.
94 * @return Return the number of observer.
96 uint32_t GetObserverCount() const;
99 * @brief Set NPatch image url.
101 * @param [in] url NPatch image url
103 void SetUrl(const std::string url);
106 * @brief Retrieve the image url.
108 * @return Return the image url.
110 std::string GetUrl() const;
113 * @brief Set texture set on the cache data
115 * @param [in] textureSet loaded texture set
117 void SetTextures(const TextureSet textureSet);
120 * @brief Retrieve loaded texture set.
122 * @return Return loaded texture set.
124 TextureSet GetTextures() const;
127 * @brief Set X directional stretchPixels
129 * @param [in] stretchPixelsX stretchPixels for X direction
131 void SetStretchPixelsX(const NPatchUtility::StretchRanges stretchPixelsX);
134 * @brief Set Y directional stretchPixels
136 * @param [in] stretchPixelsY stretchPixels for Y direction
138 void SetStretchPixelsY(const NPatchUtility::StretchRanges stretchPixelsY);
141 * @brief Retrieve stretchPixels for X direction.
143 * @return Return stretchPixels for X direction.
145 NPatchUtility::StretchRanges GetStretchPixelsX() const;
148 * @brief Retrieve stretchPixels for Y direction.
150 * @return Return stretchPixels for Y direction.
152 NPatchUtility::StretchRanges GetStretchPixelsY() const;
155 * @brief Set cache data hash.
157 * @param [in] hash cache hash
159 void SetHash(std::size_t hash);
162 * @brief Retrieve hash value of the cache.
164 * @return Return hash value of the cache.
166 std::size_t GetHash() const;
169 * @brief Set croppedWidth of NPatch
171 * @param [in] croppedWidth croppedWidth of NPatch
173 void SetCroppedWidth(uint32_t croppedWidth);
176 * @brief Set croppedHeight of NPatch
178 * @param [in] croppedHeight croppedHeight of NPatch
180 void SetCroppedHeight(uint32_t croppedHeight);
183 * @brief Retrieve croppedWidth of NPatch.
185 * @return Return croppedWidth of NPatch.
187 uint32_t GetCroppedWidth() const;
190 * @brief Retrieve croppedHeight of NPatch.
192 * @return Return croppedHeight of NPatch.
194 uint32_t GetCroppedHeight() const;
197 * @brief Set border of NPatch.
199 * @param [in] border border of NPatch
201 void SetBorder(const Rect<int> border);
204 * @brief Retrieve border of NPatch.
206 * @return Return border of NPatch.
208 Rect<int> GetBorder() const;
211 * @brief Set whether the loaded image is premultiplied or not
213 * @param [in] preMultiplyOnLoad whether the loaded image is premultiplied or not
215 void SetPreMultiplyOnLoad(bool preMultiplyOnLoad);
218 * @brief Retrieve whether the loaded image is premultiplied or not.
220 * @return Return true if the image is premultiplied alpha.
222 bool IsPreMultiplied() const;
225 * @brief Set current loading state.
227 * @param [in] loadingState current loading state
229 void SetLoadingState(const LoadingState loadingState);
232 * @brief Retrieve current loading state.
234 * @return Return current loading state.
236 LoadingState GetLoadingState() const;
239 * @brief Retrieve NPatch rendering data.
241 * @return Return NPatch rendering data.
243 void* GetRenderingMap() const;
246 * @brief Set loaded pixel buffer for the cache data.
248 * @param [in] pixelBuffer loaded pixel buffer.
249 * @param [in] preMultiplied whether the loaded image is premultiplied or not
251 void SetLoadedNPatchData(Devel::PixelBuffer& pixelBuffer, bool preMultiplied);
255 * @copydoc TextureUploadObserver::UploadCompleted
257 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
258 * This callback is the place to add the renderer as it would be called once the loading is finished.
260 void UploadComplete(bool loadSuccess, int32_t textureId, TextureSet textureSet, bool useAtlasing, const Vector4& atlasRect, bool preMultiplied) override
265 * @copydoc TextureUploadObserver::LoadComplete
267 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
268 * This callback is the place to add the renderer as it would be called once the loading is finished.
270 void LoadComplete(bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied) override;
273 using ObserverListType = Dali::Vector<TextureUploadObserver*>;
276 ObserverListType mObserverList; ///< Container used to store all observer clients of this Texture
277 std::string mUrl; ///< Url of the N-Patch
278 TextureSet mTextureSet; ///< Texture containing the cropped image
279 NPatchUtility::StretchRanges mStretchPixelsX; ///< X stretch pixels
280 NPatchUtility::StretchRanges mStretchPixelsY; ///< Y stretch pixels
281 std::size_t mHash; ///< Hash code for the Url
282 uint32_t mCroppedWidth; ///< Width of the cropped middle part of N-patch
283 uint32_t mCroppedHeight; ///< Height of the cropped middle part of N-patch
284 Rect<int> mBorder; ///< The size of the border
285 LoadingState mLoadingState; ///< True if the data loading is completed
286 bool mPreMultiplyOnLoad; ///< Whether to multiply alpha into color channels on load
287 void* mRenderingMap; ///< NPatch rendering data
290 } // namespace Internal
292 } // namespace Toolkit
296 #endif // DALI_TOOLKIT_NPATCH_DATA_H