1 #ifndef DALI_TOOLKIT_NPATCH_DATA_H
2 #define DALI_TOOLKIT_NPATCH_DATA_H
5 * Copyright (c) 2020 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.
22 #include <dali/public-api/rendering/texture-set.h>
23 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
26 #include <dali-toolkit/internal/visuals/texture-manager-impl.h>
27 #include <dali-toolkit/devel-api/utility/npatch-utilities.h>
38 class NPatchData : public Dali::Toolkit::TextureUploadObserver
41 typedef int32_t NPatchDataId; ///< The NPatchDataId type. This is used as a handle to refer to a particular Npatch Data.
42 static const int INVALID_NPATCH_DATA_ID = -1; ///< Used to represent a null TextureId or error
45 * @brief Loading State of the NPatch image.
47 enum class LoadingState
49 LOADING = 0, ///< NPatch is on loading.
50 LOAD_COMPLETE, ///< NPatch loading is completed successfully.
51 LOAD_FAILED ///< NPatch loading is failed.
62 * Destructor, non-virtual as not a base class
69 * @brief Set cache data id.
71 * @param [in] id cache data id
73 void SetId(const NPatchDataId id);
76 * @brief Retrieve cache data id
78 * @return cache data id.
80 NPatchDataId GetId() const;
83 * @brief Add TextureUploadObserver that uses the image of this cache data.
85 * @param [in] textureObserver the TextureUploadObserver that uses the image of this cache data.
87 void AddObserver(TextureUploadObserver* textureObserver);
90 * @brief Remove TextureUploadObserver.
92 * @param [in] textureObserver the TextureUploadObserver that will be removed in this cache data.
94 void RemoveObserver(TextureUploadObserver* textureObserver);
97 * @brief Retrieve the number of observer.
99 * @return Return the number of observer.
101 uint32_t GetObserverCount() const;
104 * @brief Set NPatch image url.
106 * @param [in] url NPatch image url
108 void SetUrl(const std::string url);
111 * @brief Retrieve the image url.
113 * @return Return the image url.
115 std::string GetUrl() const;
118 * @brief Set texture set on the cache data
120 * @param [in] textureSet loaded texture set
122 void SetTextures(const TextureSet textureSet);
125 * @brief Retrieve loaded texture set.
127 * @return Return loaded texture set.
129 TextureSet GetTextures() const;
132 * @brief Set X directional stretchPixels
134 * @param [in] stretchPixelsX stretchPixels for X direction
136 void SetStretchPixelsX(const NPatchUtility::StretchRanges stretchPixelsX);
139 * @brief Set Y directional stretchPixels
141 * @param [in] stretchPixelsY stretchPixels for Y direction
143 void SetStretchPixelsY(const NPatchUtility::StretchRanges stretchPixelsY);
146 * @brief Retrieve stretchPixels for X direction.
148 * @return Return stretchPixels for X direction.
150 NPatchUtility::StretchRanges GetStretchPixelsX() const;
153 * @brief Retrieve stretchPixels for Y direction.
155 * @return Return stretchPixels for Y direction.
157 NPatchUtility::StretchRanges GetStretchPixelsY() const;
160 * @brief Set cache data hash.
162 * @param [in] hash cache hash
164 void SetHash(std::size_t hash);
167 * @brief Retrieve hash value of the cache.
169 * @return Return hash value of the cache.
171 std::size_t GetHash() const;
174 * @brief Set croppedWidth of NPatch
176 * @param [in] croppedWidth croppedWidth of NPatch
178 void SetCroppedWidth(uint32_t croppedWidth);
181 * @brief Set croppedHeight of NPatch
183 * @param [in] croppedHeight croppedHeight of NPatch
185 void SetCroppedHeight(uint32_t croppedHeight);
188 * @brief Retrieve croppedWidth of NPatch.
190 * @return Return croppedWidth of NPatch.
192 uint32_t GetCroppedWidth() const;
195 * @brief Retrieve croppedHeight of NPatch.
197 * @return Return croppedHeight of NPatch.
199 uint32_t GetCroppedHeight() const;
202 * @brief Set border of NPatch.
204 * @param [in] border border of NPatch
206 void SetBorder(const Rect<int> border);
209 * @brief Retrieve border of NPatch.
211 * @return Return border of NPatch.
213 Rect<int> GetBorder() const;
216 * @brief Set whether the loaded image is premultiplied or not
218 * @param [in] preMultiplyOnLoad whether the loaded image is premultiplied or not
220 void SetPreMultiplyOnLoad(bool preMultiplyOnLoad);
223 * @brief Retrieve whether the loaded image is premultiplied or not.
225 * @return Return true if the image is premultiplied alpha.
227 bool IsPreMultiplied() const;
230 * @brief Set current loading state.
232 * @param [in] loadingState current loading state
234 void SetLoadingState(const LoadingState loadingState);
237 * @brief Retrieve current loading state.
239 * @return Return current loading state.
241 LoadingState GetLoadingState() const;
245 * @brief Retrieve NPatch rendering data.
247 * @return Return NPatch rendering data.
249 void* GetRenderingMap() const;
252 * @brief Set loaded pixel buffer for the cache data.
254 * @param [in] pixelBuffer loaded pixel buffer.
255 * @param [in] preMultiplied whether the loaded image is premultiplied or not
257 void SetLoadedNPatchData( Devel::PixelBuffer& pixelBuffer, bool preMultiplied );
262 * @copydoc TextureUploadObserver::UploadCompleted
264 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
265 * This callback is the place to add the renderer as it would be called once the loading is finished.
267 void UploadComplete( bool loadSuccess, int32_t textureId, TextureSet textureSet, bool useAtlasing, const Vector4& atlasRect, bool preMultiplied ) override {}
270 * @copydoc TextureUploadObserver::LoadComplete
272 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
273 * This callback is the place to add the renderer as it would be called once the loading is finished.
275 void LoadComplete( bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied ) override;
280 using ObserverListType = Dali::Vector<TextureUploadObserver*>;
283 ObserverListType mObserverList; ///< Container used to store all observer clients of this Texture
284 std::string mUrl; ///< Url of the N-Patch
285 TextureSet mTextureSet; ///< Texture containing the cropped image
286 NPatchUtility::StretchRanges mStretchPixelsX; ///< X stretch pixels
287 NPatchUtility::StretchRanges mStretchPixelsY; ///< Y stretch pixels
288 std::size_t mHash; ///< Hash code for the Url
289 uint32_t mCroppedWidth; ///< Width of the cropped middle part of N-patch
290 uint32_t mCroppedHeight; ///< Height of the cropped middle part of N-patch
291 Rect<int> mBorder; ///< The size of the border
292 LoadingState mLoadingState; ///< True if the data loading is completed
293 bool mPreMultiplyOnLoad; ///< Whether to multiply alpha into color channels on load
294 void* mRenderingMap; ///< NPatch rendering data
297 } // namespace Internal
299 } // namespace Toolkit
303 #endif // DALI_TOOLKIT_NPATCH_DATA_H