1 #ifndef DALI_TOOLKIT_NPATCH_DATA_H
2 #define DALI_TOOLKIT_NPATCH_DATA_H
5 * Copyright (c) 2022 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/texture-manager/texture-manager-impl.h>
28 #include <dali-toolkit/internal/visuals/visual-url.h>
36 class NPatchData : public Dali::Toolkit::TextureUploadObserver
39 typedef int32_t NPatchDataId; ///< The NPatchDataId type. This is used as a handle to refer to a particular Npatch Data.
40 static const int INVALID_NPATCH_DATA_ID = -1; ///< Used to represent a null TextureId or error
43 * @brief Loading State of the NPatch image.
45 enum class LoadingState
47 NOT_STARTED = 0, ///< NPatch loading is not started yet.
48 LOADING, ///< NPatch is on loading.
49 LOAD_COMPLETE, ///< NPatch loading is completed successfully.
50 LOAD_FAILED ///< NPatch loading is failed.
60 * Destructor, non-virtual as not a base class
66 * @brief Set cache data id.
68 * @param [in] id cache data id
70 void SetId(const NPatchDataId id);
73 * @brief Retrieve cache data id
75 * @return cache data id.
77 NPatchDataId GetId() const;
80 * @brief Add TextureUploadObserver that uses the image of this cache data.
82 * @param [in] textureObserver the TextureUploadObserver that uses the image of this cache data.
84 void AddObserver(TextureUploadObserver* textureObserver);
87 * @brief Remove TextureUploadObserver.
89 * @param [in] textureObserver the TextureUploadObserver that will be removed in this cache data.
91 void RemoveObserver(TextureUploadObserver* textureObserver);
94 * @brief Retrieve the number of observer.
96 * @return Return the number of observer.
98 uint32_t GetObserverCount() const;
101 * @brief Set NPatch image url.
103 * @param [in] url NPatch image url
105 void SetUrl(const VisualUrl& url);
108 * @brief Retrieve the image url.
110 * @return Return the image url.
112 VisualUrl GetUrl() const;
115 * @brief Set texture set on the cache data
117 * @param [in] textureSet loaded texture set
119 void SetTextures(const TextureSet textureSet);
122 * @brief Retrieve loaded texture set.
124 * @return Return loaded texture set.
126 TextureSet GetTextures() const;
129 * @brief Set X directional stretchPixels
131 * @param [in] stretchPixelsX stretchPixels for X direction
133 void SetStretchPixelsX(const NPatchUtility::StretchRanges stretchPixelsX);
136 * @brief Set Y directional stretchPixels
138 * @param [in] stretchPixelsY stretchPixels for Y direction
140 void SetStretchPixelsY(const NPatchUtility::StretchRanges stretchPixelsY);
143 * @brief Retrieve stretchPixels for X direction.
145 * @return Return stretchPixels for X direction.
147 NPatchUtility::StretchRanges GetStretchPixelsX() const;
150 * @brief Retrieve stretchPixels for Y direction.
152 * @return Return stretchPixels for Y direction.
154 NPatchUtility::StretchRanges GetStretchPixelsY() const;
157 * @brief Set cache data hash.
159 * @param [in] hash cache hash
161 void SetHash(std::size_t hash);
164 * @brief Retrieve hash value of the cache.
166 * @return Return hash value of the cache.
168 std::size_t GetHash() const;
171 * @brief Set croppedWidth of NPatch
173 * @param [in] croppedWidth croppedWidth of NPatch
175 void SetCroppedWidth(uint32_t croppedWidth);
178 * @brief Set croppedHeight of NPatch
180 * @param [in] croppedHeight croppedHeight of NPatch
182 void SetCroppedHeight(uint32_t croppedHeight);
185 * @brief Retrieve croppedWidth of NPatch.
187 * @return Return croppedWidth of NPatch.
189 uint32_t GetCroppedWidth() const;
192 * @brief Retrieve croppedHeight of NPatch.
194 * @return Return croppedHeight of NPatch.
196 uint32_t GetCroppedHeight() const;
199 * @brief Set border of NPatch.
201 * @param [in] border border of NPatch
203 void SetBorder(const Rect<int> border);
206 * @brief Retrieve border of NPatch.
208 * @return Return border of NPatch.
210 Rect<int> GetBorder() const;
213 * @brief Set whether the loaded image is premultiplied or not
215 * @param [in] preMultiplyOnLoad whether the loaded image is premultiplied or not
217 void SetPreMultiplyOnLoad(bool preMultiplyOnLoad);
220 * @brief Retrieve whether the loaded image is premultiplied or not.
222 * @return Return true if the image is premultiplied alpha.
224 bool IsPreMultiplied() const;
227 * @brief Set current loading state.
229 * @param [in] loadingState current loading state
231 void SetLoadingState(const LoadingState loadingState);
234 * @brief Retrieve current loading state.
236 * @return Return current loading state.
238 LoadingState GetLoadingState() const;
241 * @brief Retrieve NPatch rendering data.
243 * @return Return NPatch rendering data.
245 void* GetRenderingMap() const;
248 * @brief Set loaded pixel buffer for the cache data.
250 * @param [in] pixelBuffer loaded pixel buffer.
251 * @param [in] preMultiplied whether the loaded image is premultiplied or not
253 void SetLoadedNPatchData(Devel::PixelBuffer& pixelBuffer, bool preMultiplied);
256 * @brief Send LoadComplete notify with current setuped NPatchData
258 * @param [in] observer observer who will be got LoadComplete notify
259 * @param [in] loadSuccess whether the image load success or not.
261 void NotifyObserver(TextureUploadObserver* observer, const bool& loadSuccess);
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, TextureInformation textureInformation) override;
273 using ObserverListType = Dali::Vector<TextureUploadObserver*>;
276 ObserverListType mObserverList; ///< Container used to store all observer clients of this Texture
277 VisualUrl 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