[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / npatch-data.h
1 #ifndef DALI_TOOLKIT_NPATCH_DATA_H
2 #define DALI_TOOLKIT_NPATCH_DATA_H
3
4 /*
5  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 // EXTERNAL INCLUDES
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>
24 #include <string>
25
26 // INTERNAL INCLUDES
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>
30
31 namespace Dali
32 {
33 namespace Toolkit
34 {
35 namespace Internal
36 {
37 class NPatchData;
38 typedef IntrusivePtr<NPatchData> NPatchDataPtr;
39
40 class NPatchData : public ConnectionTracker, public Dali::Toolkit::TextureUploadObserver, public Dali::RefObject
41 {
42 public:
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
45
46   /**
47    * @brief Loading State of the NPatch image.
48    */
49   enum class LoadingState
50   {
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.
55   };
56
57 public:
58   /**
59    * Constructor
60    */
61   NPatchData();
62
63   /**
64    * Destructor, non-virtual as not a base class
65    */
66   ~NPatchData();
67
68 public:
69   /**
70    * @brief Set cache data id.
71    *
72    * @param [in] id cache data id
73    */
74   void SetId(const NPatchDataId id);
75
76   /**
77    * @brief Retrieve cache data id
78    *
79    * @return cache data id.
80    */
81   NPatchDataId GetId() const;
82
83   /**
84    * @brief Add TextureUploadObserver that uses the image of this cache data.
85    *
86    * @param [in] textureObserver the TextureUploadObserver that uses the image of this cache data.
87    */
88   void AddObserver(TextureUploadObserver* textureObserver);
89
90   /**
91    * @brief Remove TextureUploadObserver.
92    *
93    * @param [in] textureObserver the TextureUploadObserver that will be removed in this cache data.
94    */
95   void RemoveObserver(TextureUploadObserver* textureObserver);
96
97   /**
98    * @brief Retrieve the number of observer.
99    *
100    * @return Return the number of observer.
101    */
102   uint32_t GetObserverCount() const;
103
104   /**
105    * @brief Set NPatch image url.
106    *
107    * @param [in] url NPatch image url
108    */
109   void SetUrl(const VisualUrl& url);
110
111   /**
112    * @brief Retrieve the image url.
113    *
114    * @return Return the image url.
115    */
116   VisualUrl GetUrl() const;
117
118   /**
119    * @brief Set texture set on the cache data
120    *
121    * @param [in] textureSet loaded texture set
122    */
123   void SetTextures(const TextureSet textureSet);
124
125   /**
126    * @brief Retrieve loaded texture set.
127    *
128    * @return Return loaded texture set.
129    */
130   TextureSet GetTextures() const;
131
132   /**
133    * @brief Set X directional stretchPixels
134    *
135    * @param [in] stretchPixelsX stretchPixels for X direction
136    */
137   void SetStretchPixelsX(const NPatchUtility::StretchRanges stretchPixelsX);
138
139   /**
140    * @brief Set Y directional stretchPixels
141    *
142    * @param [in] stretchPixelsY stretchPixels for Y direction
143    */
144   void SetStretchPixelsY(const NPatchUtility::StretchRanges stretchPixelsY);
145
146   /**
147    * @brief Retrieve stretchPixels for X direction.
148    *
149    * @return Return stretchPixels for X direction.
150    */
151   NPatchUtility::StretchRanges GetStretchPixelsX() const;
152
153   /**
154    * @brief Retrieve stretchPixels for Y direction.
155    *
156    * @return Return stretchPixels for Y direction.
157    */
158   NPatchUtility::StretchRanges GetStretchPixelsY() const;
159
160   /**
161    * @brief Set cache data hash.
162    *
163    * @param [in] hash cache hash
164    */
165   void SetHash(std::size_t hash);
166
167   /**
168    * @brief Retrieve hash value of the cache.
169    *
170    * @return Return hash value of the cache.
171    */
172   std::size_t GetHash() const;
173
174   /**
175    * @brief Set croppedWidth of NPatch
176    *
177    * @param [in] croppedWidth croppedWidth of NPatch
178    */
179   void SetCroppedWidth(uint32_t croppedWidth);
180
181   /**
182    * @brief Set croppedHeight of NPatch
183    *
184    * @param [in] croppedHeight croppedHeight of NPatch
185    */
186   void SetCroppedHeight(uint32_t croppedHeight);
187
188   /**
189    * @brief Retrieve croppedWidth of NPatch.
190    *
191    * @return Return croppedWidth of NPatch.
192    */
193   uint32_t GetCroppedWidth() const;
194
195   /**
196    * @brief Retrieve croppedHeight of NPatch.
197    *
198    * @return Return croppedHeight of NPatch.
199    */
200   uint32_t GetCroppedHeight() const;
201
202   /**
203    * @brief Set border of NPatch.
204    *
205    * @param [in] border border of NPatch
206    */
207   void SetBorder(const Rect<int> border);
208
209   /**
210    * @brief Retrieve border of NPatch.
211    *
212    * @return Return border of NPatch.
213    */
214   Rect<int> GetBorder() const;
215
216   /**
217    * @brief Set whether the loaded image is premultiplied or not
218    *
219    * @param [in] preMultiplyOnLoad whether the loaded image is premultiplied or not
220    */
221   void SetPreMultiplyOnLoad(bool preMultiplyOnLoad);
222
223   /**
224    * @brief Retrieve whether the loaded image is premultiplied or not.
225    *
226    * @return Return true if the image is premultiplied alpha.
227    */
228   bool IsPreMultiplied() const;
229
230   /**
231    * @brief Set current loading state.
232    *
233    * @param [in] loadingState current loading state
234    */
235   void SetLoadingState(const LoadingState loadingState);
236
237   /**
238    * @brief Retrieve current loading state.
239    *
240    * @return Return current loading state.
241    */
242   LoadingState GetLoadingState() const;
243
244   /**
245    * @brief Retrieve NPatch rendering data.
246    *
247    * @return Return NPatch rendering data.
248    */
249   void* GetRenderingMap() const;
250
251   /**
252    * @brief Set loaded pixel buffer for the cache data.
253    *
254    * @param [in] pixelBuffer loaded pixel buffer.
255    * @param [in] preMultiplied whether the loaded image is premultiplied or not
256    */
257   void SetLoadedNPatchData(Devel::PixelBuffer& pixelBuffer, bool preMultiplied);
258
259   /**
260    * @brief Send LoadComplete notify with current setuped NPatchData
261    *
262    * @param [in] observer observer who will be got LoadComplete notify
263    * @param [in] loadSuccess whether the image load success or not.
264    */
265   void NotifyObserver(TextureUploadObserver* observer, const bool& loadSuccess);
266
267 private:
268   /**
269    * @copydoc TextureUploadObserver::LoadComplete
270    *
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.
273    */
274   void LoadComplete(bool loadSuccess, TextureInformation textureInformation) override;
275
276   /**
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
280    */
281   void ObserverDestroyed(TextureUploadObserver* observer);
282
283 private:
284   using ObserverListType = Dali::Vector<TextureUploadObserver*>;
285
286   NPatchDataId     mId;
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
289
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
300
301   bool mPreMultiplyOnLoad : 1; ///< Whether to multiply alpha into color channels on load
302   bool mObserverNotifying : 1; ///< Whether this NPatchData notifying observers or not.
303 };
304
305 } // namespace Internal
306
307 } // namespace Toolkit
308
309 } // namespace Dali
310
311 #endif // DALI_TOOLKIT_NPATCH_DATA_H