[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) 2021 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/rendering/texture-set.h>
23 #include <string>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/devel-api/utility/npatch-utilities.h>
27 #include <dali-toolkit/internal/visuals/texture-manager-impl.h>
28
29 namespace Dali
30 {
31 namespace Toolkit
32 {
33 namespace Internal
34 {
35 class NPatchData : public Dali::Toolkit::TextureUploadObserver
36 {
37 public:
38   typedef int32_t  NPatchDataId;                ///< The NPatchDataId type. This is used as a handle to refer to a particular Npatch Data.
39   static const int INVALID_NPATCH_DATA_ID = -1; ///< Used to represent a null TextureId or error
40
41   /**
42    * @brief Loading State of the NPatch image.
43    */
44   enum class LoadingState
45   {
46     LOADING = 0,   ///< NPatch is on loading.
47     LOAD_COMPLETE, ///< NPatch loading is completed successfully.
48     LOAD_FAILED    ///< NPatch loading is failed.
49   };
50
51 public:
52   /**
53    * Constructor
54    */
55   NPatchData();
56
57   /**
58    * Destructor, non-virtual as not a base class
59    */
60   ~NPatchData();
61
62 public:
63   /**
64    * @brief Set cache data id.
65    *
66    * @param [in] id cache data id
67    */
68   void SetId(const NPatchDataId id);
69
70   /**
71    * @brief Retrieve cache data id
72    *
73    * @return cache data id.
74    */
75   NPatchDataId GetId() const;
76
77   /**
78    * @brief Add TextureUploadObserver that uses the image of this cache data.
79    *
80    * @param [in] textureObserver the TextureUploadObserver that uses the image of this cache data.
81    */
82   void AddObserver(TextureUploadObserver* textureObserver);
83
84   /**
85    * @brief Remove TextureUploadObserver.
86    *
87    * @param [in] textureObserver the TextureUploadObserver that will be removed in this cache data.
88    */
89   void RemoveObserver(TextureUploadObserver* textureObserver);
90
91   /**
92    * @brief Retrieve the number of observer.
93    *
94    * @return Return the number of observer.
95    */
96   uint32_t GetObserverCount() const;
97
98   /**
99    * @brief Set NPatch image url.
100    *
101    * @param [in] url NPatch image url
102    */
103   void SetUrl(const std::string url);
104
105   /**
106    * @brief Retrieve the image url.
107    *
108    * @return Return the image url.
109    */
110   std::string GetUrl() const;
111
112   /**
113    * @brief Set texture set on the cache data
114    *
115    * @param [in] textureSet loaded texture set
116    */
117   void SetTextures(const TextureSet textureSet);
118
119   /**
120    * @brief Retrieve loaded texture set.
121    *
122    * @return Return loaded texture set.
123    */
124   TextureSet GetTextures() const;
125
126   /**
127    * @brief Set X directional stretchPixels
128    *
129    * @param [in] stretchPixelsX stretchPixels for X direction
130    */
131   void SetStretchPixelsX(const NPatchUtility::StretchRanges stretchPixelsX);
132
133   /**
134    * @brief Set Y directional stretchPixels
135    *
136    * @param [in] stretchPixelsY stretchPixels for Y direction
137    */
138   void SetStretchPixelsY(const NPatchUtility::StretchRanges stretchPixelsY);
139
140   /**
141    * @brief Retrieve stretchPixels for X direction.
142    *
143    * @return Return stretchPixels for X direction.
144    */
145   NPatchUtility::StretchRanges GetStretchPixelsX() const;
146
147   /**
148    * @brief Retrieve stretchPixels for Y direction.
149    *
150    * @return Return stretchPixels for Y direction.
151    */
152   NPatchUtility::StretchRanges GetStretchPixelsY() const;
153
154   /**
155    * @brief Set cache data hash.
156    *
157    * @param [in] hash cache hash
158    */
159   void SetHash(std::size_t hash);
160
161   /**
162    * @brief Retrieve hash value of the cache.
163    *
164    * @return Return hash value of the cache.
165    */
166   std::size_t GetHash() const;
167
168   /**
169    * @brief Set croppedWidth of NPatch
170    *
171    * @param [in] croppedWidth croppedWidth of NPatch
172    */
173   void SetCroppedWidth(uint32_t croppedWidth);
174
175   /**
176    * @brief Set croppedHeight of NPatch
177    *
178    * @param [in] croppedHeight croppedHeight of NPatch
179    */
180   void SetCroppedHeight(uint32_t croppedHeight);
181
182   /**
183    * @brief Retrieve croppedWidth of NPatch.
184    *
185    * @return Return croppedWidth of NPatch.
186    */
187   uint32_t GetCroppedWidth() const;
188
189   /**
190    * @brief Retrieve croppedHeight of NPatch.
191    *
192    * @return Return croppedHeight of NPatch.
193    */
194   uint32_t GetCroppedHeight() const;
195
196   /**
197    * @brief Set border of NPatch.
198    *
199    * @param [in] border border of NPatch
200    */
201   void SetBorder(const Rect<int> border);
202
203   /**
204    * @brief Retrieve border of NPatch.
205    *
206    * @return Return border of NPatch.
207    */
208   Rect<int> GetBorder() const;
209
210   /**
211    * @brief Set whether the loaded image is premultiplied or not
212    *
213    * @param [in] preMultiplyOnLoad whether the loaded image is premultiplied or not
214    */
215   void SetPreMultiplyOnLoad(bool preMultiplyOnLoad);
216
217   /**
218    * @brief Retrieve whether the loaded image is premultiplied or not.
219    *
220    * @return Return true if the image is premultiplied alpha.
221    */
222   bool IsPreMultiplied() const;
223
224   /**
225    * @brief Set current loading state.
226    *
227    * @param [in] loadingState current loading state
228    */
229   void SetLoadingState(const LoadingState loadingState);
230
231   /**
232    * @brief Retrieve current loading state.
233    *
234    * @return Return current loading state.
235    */
236   LoadingState GetLoadingState() const;
237
238   /**
239    * @brief Retrieve NPatch rendering data.
240    *
241    * @return Return NPatch rendering data.
242    */
243   void* GetRenderingMap() const;
244
245   /**
246    * @brief Set loaded pixel buffer for the cache data.
247    *
248    * @param [in] pixelBuffer loaded pixel buffer.
249    * @param [in] preMultiplied whether the loaded image is premultiplied or not
250    */
251   void SetLoadedNPatchData(Devel::PixelBuffer& pixelBuffer, bool preMultiplied);
252
253 private:
254   /**
255    * @copydoc TextureUploadObserver::UploadCompleted
256    *
257    * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
258    * This callback is the place to add the renderer as it would be called once the loading is finished.
259    */
260   void UploadComplete(bool loadSuccess, int32_t textureId, TextureSet textureSet, bool useAtlasing, const Vector4& atlasRect, bool preMultiplied) override
261   {
262   }
263
264   /**
265    * @copydoc TextureUploadObserver::LoadComplete
266    *
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.
269    */
270   void LoadComplete(bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied) override;
271
272 private:
273   using ObserverListType = Dali::Vector<TextureUploadObserver*>;
274
275   NPatchDataId                 mId;
276   ObserverListType             mObserverList;      ///< Container used to store all observer clients of this Texture
277   std::string                  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
288 };
289
290 } // namespace Internal
291
292 } // namespace Toolkit
293
294 } // namespace Dali
295
296 #endif // DALI_TOOLKIT_NPATCH_DATA_H