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>
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 LOADING = 0, ///< NPatch is on loading.
48 LOAD_COMPLETE, ///< NPatch loading is completed successfully.
49 LOAD_FAILED ///< NPatch loading is failed.
59 * Destructor, non-virtual as not a base class
65 * @brief Set cache data id.
67 * @param [in] id cache data id
69 void SetId(const NPatchDataId id);
72 * @brief Retrieve cache data id
74 * @return cache data id.
76 NPatchDataId GetId() const;
79 * @brief Add TextureUploadObserver that uses the image of this cache data.
81 * @param [in] textureObserver the TextureUploadObserver that uses the image of this cache data.
83 void AddObserver(TextureUploadObserver* textureObserver);
86 * @brief Remove TextureUploadObserver.
88 * @param [in] textureObserver the TextureUploadObserver that will be removed in this cache data.
90 void RemoveObserver(TextureUploadObserver* textureObserver);
93 * @brief Retrieve the number of observer.
95 * @return Return the number of observer.
97 uint32_t GetObserverCount() const;
100 * @brief Set NPatch image url.
102 * @param [in] url NPatch image url
104 void SetUrl(const VisualUrl& url);
107 * @brief Retrieve the image url.
109 * @return Return the image url.
111 VisualUrl GetUrl() const;
114 * @brief Set texture set on the cache data
116 * @param [in] textureSet loaded texture set
118 void SetTextures(const TextureSet textureSet);
121 * @brief Retrieve loaded texture set.
123 * @return Return loaded texture set.
125 TextureSet GetTextures() const;
128 * @brief Set X directional stretchPixels
130 * @param [in] stretchPixelsX stretchPixels for X direction
132 void SetStretchPixelsX(const NPatchUtility::StretchRanges stretchPixelsX);
135 * @brief Set Y directional stretchPixels
137 * @param [in] stretchPixelsY stretchPixels for Y direction
139 void SetStretchPixelsY(const NPatchUtility::StretchRanges stretchPixelsY);
142 * @brief Retrieve stretchPixels for X direction.
144 * @return Return stretchPixels for X direction.
146 NPatchUtility::StretchRanges GetStretchPixelsX() const;
149 * @brief Retrieve stretchPixels for Y direction.
151 * @return Return stretchPixels for Y direction.
153 NPatchUtility::StretchRanges GetStretchPixelsY() const;
156 * @brief Set cache data hash.
158 * @param [in] hash cache hash
160 void SetHash(std::size_t hash);
163 * @brief Retrieve hash value of the cache.
165 * @return Return hash value of the cache.
167 std::size_t GetHash() const;
170 * @brief Set croppedWidth of NPatch
172 * @param [in] croppedWidth croppedWidth of NPatch
174 void SetCroppedWidth(uint32_t croppedWidth);
177 * @brief Set croppedHeight of NPatch
179 * @param [in] croppedHeight croppedHeight of NPatch
181 void SetCroppedHeight(uint32_t croppedHeight);
184 * @brief Retrieve croppedWidth of NPatch.
186 * @return Return croppedWidth of NPatch.
188 uint32_t GetCroppedWidth() const;
191 * @brief Retrieve croppedHeight of NPatch.
193 * @return Return croppedHeight of NPatch.
195 uint32_t GetCroppedHeight() const;
198 * @brief Set border of NPatch.
200 * @param [in] border border of NPatch
202 void SetBorder(const Rect<int> border);
205 * @brief Retrieve border of NPatch.
207 * @return Return border of NPatch.
209 Rect<int> GetBorder() const;
212 * @brief Set whether the loaded image is premultiplied or not
214 * @param [in] preMultiplyOnLoad whether the loaded image is premultiplied or not
216 void SetPreMultiplyOnLoad(bool preMultiplyOnLoad);
219 * @brief Retrieve whether the loaded image is premultiplied or not.
221 * @return Return true if the image is premultiplied alpha.
223 bool IsPreMultiplied() const;
226 * @brief Set current loading state.
228 * @param [in] loadingState current loading state
230 void SetLoadingState(const LoadingState loadingState);
233 * @brief Retrieve current loading state.
235 * @return Return current loading state.
237 LoadingState GetLoadingState() const;
240 * @brief Retrieve NPatch rendering data.
242 * @return Return NPatch rendering data.
244 void* GetRenderingMap() const;
247 * @brief Set loaded pixel buffer for the cache data.
249 * @param [in] pixelBuffer loaded pixel buffer.
250 * @param [in] preMultiplied whether the loaded image is premultiplied or not
252 void SetLoadedNPatchData(Devel::PixelBuffer& pixelBuffer, bool preMultiplied);
256 * @copydoc TextureUploadObserver::LoadComplete
258 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
259 * This callback is the place to add the renderer as it would be called once the loading is finished.
261 void LoadComplete(bool loadSuccess, TextureInformation textureInformation) override;
264 using ObserverListType = Dali::Vector<TextureUploadObserver*>;
267 ObserverListType mObserverList; ///< Container used to store all observer clients of this Texture
268 VisualUrl mUrl; ///< Url of the N-Patch
269 TextureSet mTextureSet; ///< Texture containing the cropped image
270 NPatchUtility::StretchRanges mStretchPixelsX; ///< X stretch pixels
271 NPatchUtility::StretchRanges mStretchPixelsY; ///< Y stretch pixels
272 std::size_t mHash; ///< Hash code for the Url
273 uint32_t mCroppedWidth; ///< Width of the cropped middle part of N-patch
274 uint32_t mCroppedHeight; ///< Height of the cropped middle part of N-patch
275 Rect<int> mBorder; ///< The size of the border
276 LoadingState mLoadingState; ///< True if the data loading is completed
277 bool mPreMultiplyOnLoad; ///< Whether to multiply alpha into color channels on load
278 void* mRenderingMap; ///< NPatch rendering data
281 } // namespace Internal
283 } // namespace Toolkit
287 #endif // DALI_TOOLKIT_NPATCH_DATA_H