1 #ifndef DALI_TOOLKIT_NPATCH_DATA_H
2 #define DALI_TOOLKIT_NPATCH_DATA_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/public-api/object/ref-object.h>
23 #include <dali/public-api/rendering/texture-set.h>
27 #include <dali-toolkit/devel-api/utility/npatch-utilities.h>
28 #include <dali-toolkit/internal/texture-manager/texture-manager-impl.h>
29 #include <dali-toolkit/internal/visuals/visual-url.h>
38 typedef IntrusivePtr<NPatchData> NPatchDataPtr;
40 class NPatchData : public ConnectionTracker, public Dali::Toolkit::TextureUploadObserver, public Dali::RefObject
43 typedef int32_t NPatchDataId; ///< The NPatchDataId type. This is used as a handle to refer to a particular Npatch Data.
44 static const NPatchDataId INVALID_NPATCH_DATA_ID = -1; ///< Used to represent a null TextureId or error
47 * @brief Loading State of the NPatch image.
49 enum class LoadingState
51 NOT_STARTED = 0, ///< NPatch loading is not started yet.
52 LOADING, ///< NPatch is on loading.
53 LOAD_COMPLETE, ///< NPatch loading is completed successfully.
54 LOAD_FAILED ///< NPatch loading is failed.
64 * Destructor, non-virtual as not a base class
70 * @brief Set cache data id.
72 * @param [in] id cache data id
74 void SetId(const NPatchDataId id);
77 * @brief Retrieve cache data id
79 * @return cache data id.
81 NPatchDataId GetId() const;
84 * @brief Add TextureUploadObserver that uses the image of this cache data.
86 * @param [in] textureObserver the TextureUploadObserver that uses the image of this cache data.
88 void AddObserver(TextureUploadObserver* textureObserver);
91 * @brief Remove TextureUploadObserver.
93 * @param [in] textureObserver the TextureUploadObserver that will be removed in this cache data.
95 void RemoveObserver(TextureUploadObserver* textureObserver);
98 * @brief Retrieve the number of observer.
100 * @return Return the number of observer.
102 uint32_t GetObserverCount() const;
105 * @brief Set NPatch image url.
107 * @param [in] url NPatch image url
109 void SetUrl(const VisualUrl& url);
112 * @brief Retrieve the image url.
114 * @return Return the image url.
116 VisualUrl GetUrl() const;
119 * @brief Set texture set on the cache data
121 * @param [in] textureSet loaded texture set
123 void SetTextures(const TextureSet textureSet);
126 * @brief Retrieve loaded texture set.
128 * @return Return loaded texture set.
130 TextureSet GetTextures() const;
133 * @brief Set X directional stretchPixels
135 * @param [in] stretchPixelsX stretchPixels for X direction
137 void SetStretchPixelsX(const NPatchUtility::StretchRanges stretchPixelsX);
140 * @brief Set Y directional stretchPixels
142 * @param [in] stretchPixelsY stretchPixels for Y direction
144 void SetStretchPixelsY(const NPatchUtility::StretchRanges stretchPixelsY);
147 * @brief Retrieve stretchPixels for X direction.
149 * @return Return stretchPixels for X direction.
151 NPatchUtility::StretchRanges GetStretchPixelsX() const;
154 * @brief Retrieve stretchPixels for Y direction.
156 * @return Return stretchPixels for Y direction.
158 NPatchUtility::StretchRanges GetStretchPixelsY() const;
161 * @brief Set cache data hash.
163 * @param [in] hash cache hash
165 void SetHash(std::size_t hash);
168 * @brief Retrieve hash value of the cache.
170 * @return Return hash value of the cache.
172 std::size_t GetHash() const;
175 * @brief Set croppedWidth of NPatch
177 * @param [in] croppedWidth croppedWidth of NPatch
179 void SetCroppedWidth(uint32_t croppedWidth);
182 * @brief Set croppedHeight of NPatch
184 * @param [in] croppedHeight croppedHeight of NPatch
186 void SetCroppedHeight(uint32_t croppedHeight);
189 * @brief Retrieve croppedWidth of NPatch.
191 * @return Return croppedWidth of NPatch.
193 uint32_t GetCroppedWidth() const;
196 * @brief Retrieve croppedHeight of NPatch.
198 * @return Return croppedHeight of NPatch.
200 uint32_t GetCroppedHeight() const;
203 * @brief Set border of NPatch.
205 * @param [in] border border of NPatch
207 void SetBorder(const Rect<int> border);
210 * @brief Retrieve border of NPatch.
212 * @return Return border of NPatch.
214 Rect<int> GetBorder() const;
217 * @brief Set whether the loaded image is premultiplied or not
219 * @param [in] preMultiplyOnLoad whether the loaded image is premultiplied or not
221 void SetPreMultiplyOnLoad(bool preMultiplyOnLoad);
224 * @brief Retrieve whether the loaded image is premultiplied or not.
226 * @return Return true if the image is premultiplied alpha.
228 bool IsPreMultiplied() const;
231 * @brief Set current loading state.
233 * @param [in] loadingState current loading state
235 void SetLoadingState(const LoadingState loadingState);
238 * @brief Retrieve current loading state.
240 * @return Return current loading state.
242 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);
260 * @brief Send LoadComplete notify with current setuped NPatchData
262 * @param [in] observer observer who will be got LoadComplete notify
263 * @param [in] loadSuccess whether the image load success or not.
265 void NotifyObserver(TextureUploadObserver* observer, const bool& loadSuccess);
269 * @copydoc TextureUploadObserver::LoadComplete
271 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
272 * This callback is the place to add the renderer as it would be called once the loading is finished.
274 void LoadComplete(bool loadSuccess, TextureInformation textureInformation) override;
277 * This is called by the TextureUploadObserver when an observer is destroyed.
278 * We use the callback to know when to remove an observer from our notify list.
279 * @param[in] observer The observer that generated the callback
281 void ObserverDestroyed(TextureUploadObserver* observer);
284 using ObserverListType = Dali::Vector<TextureUploadObserver*>;
287 ObserverListType mObserverList; ///< Container used to store all observer clients of this Texture
288 ObserverListType mQueuedObservers; ///< Container observers when user try to add during notify observers
290 VisualUrl mUrl; ///< Url of the N-Patch
291 TextureSet mTextureSet; ///< Texture containing the cropped image
292 NPatchUtility::StretchRanges mStretchPixelsX; ///< X stretch pixels
293 NPatchUtility::StretchRanges mStretchPixelsY; ///< Y stretch pixels
294 std::size_t mHash; ///< Hash code for the Url
295 uint32_t mCroppedWidth; ///< Width of the cropped middle part of N-patch
296 uint32_t mCroppedHeight; ///< Height of the cropped middle part of N-patch
297 Rect<int> mBorder; ///< The size of the border
298 LoadingState mLoadingState; ///< True if the data loading is completed
299 void* mRenderingMap; ///< NPatch rendering data
301 bool mPreMultiplyOnLoad : 1; ///< Whether to multiply alpha into color channels on load
302 bool mObserverNotifying : 1; ///< Whether this NPatchData notifying observers or not.
305 } // namespace Internal
307 } // namespace Toolkit
311 #endif // DALI_TOOLKIT_NPATCH_DATA_H