94d7f2b7d965f26d99e63504cd68f3141a63e4f5
[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 #include <dali-toolkit/internal/visuals/visual-url.h>
29
30 namespace Dali
31 {
32 namespace Toolkit
33 {
34 namespace Internal
35 {
36 class NPatchData : public Dali::Toolkit::TextureUploadObserver
37 {
38 public:
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
41
42   /**
43    * @brief Loading State of the NPatch image.
44    */
45   enum class LoadingState
46   {
47     LOADING = 0,   ///< NPatch is on loading.
48     LOAD_COMPLETE, ///< NPatch loading is completed successfully.
49     LOAD_FAILED    ///< NPatch loading is failed.
50   };
51
52 public:
53   /**
54    * Constructor
55    */
56   NPatchData();
57
58   /**
59    * Destructor, non-virtual as not a base class
60    */
61   ~NPatchData();
62
63 public:
64   /**
65    * @brief Set cache data id.
66    *
67    * @param [in] id cache data id
68    */
69   void SetId(const NPatchDataId id);
70
71   /**
72    * @brief Retrieve cache data id
73    *
74    * @return cache data id.
75    */
76   NPatchDataId GetId() const;
77
78   /**
79    * @brief Add TextureUploadObserver that uses the image of this cache data.
80    *
81    * @param [in] textureObserver the TextureUploadObserver that uses the image of this cache data.
82    */
83   void AddObserver(TextureUploadObserver* textureObserver);
84
85   /**
86    * @brief Remove TextureUploadObserver.
87    *
88    * @param [in] textureObserver the TextureUploadObserver that will be removed in this cache data.
89    */
90   void RemoveObserver(TextureUploadObserver* textureObserver);
91
92   /**
93    * @brief Retrieve the number of observer.
94    *
95    * @return Return the number of observer.
96    */
97   uint32_t GetObserverCount() const;
98
99   /**
100    * @brief Set NPatch image url.
101    *
102    * @param [in] url NPatch image url
103    */
104   void SetUrl(const VisualUrl& url);
105
106   /**
107    * @brief Retrieve the image url.
108    *
109    * @return Return the image url.
110    */
111   VisualUrl GetUrl() const;
112
113   /**
114    * @brief Set texture set on the cache data
115    *
116    * @param [in] textureSet loaded texture set
117    */
118   void SetTextures(const TextureSet textureSet);
119
120   /**
121    * @brief Retrieve loaded texture set.
122    *
123    * @return Return loaded texture set.
124    */
125   TextureSet GetTextures() const;
126
127   /**
128    * @brief Set X directional stretchPixels
129    *
130    * @param [in] stretchPixelsX stretchPixels for X direction
131    */
132   void SetStretchPixelsX(const NPatchUtility::StretchRanges stretchPixelsX);
133
134   /**
135    * @brief Set Y directional stretchPixels
136    *
137    * @param [in] stretchPixelsY stretchPixels for Y direction
138    */
139   void SetStretchPixelsY(const NPatchUtility::StretchRanges stretchPixelsY);
140
141   /**
142    * @brief Retrieve stretchPixels for X direction.
143    *
144    * @return Return stretchPixels for X direction.
145    */
146   NPatchUtility::StretchRanges GetStretchPixelsX() const;
147
148   /**
149    * @brief Retrieve stretchPixels for Y direction.
150    *
151    * @return Return stretchPixels for Y direction.
152    */
153   NPatchUtility::StretchRanges GetStretchPixelsY() const;
154
155   /**
156    * @brief Set cache data hash.
157    *
158    * @param [in] hash cache hash
159    */
160   void SetHash(std::size_t hash);
161
162   /**
163    * @brief Retrieve hash value of the cache.
164    *
165    * @return Return hash value of the cache.
166    */
167   std::size_t GetHash() const;
168
169   /**
170    * @brief Set croppedWidth of NPatch
171    *
172    * @param [in] croppedWidth croppedWidth of NPatch
173    */
174   void SetCroppedWidth(uint32_t croppedWidth);
175
176   /**
177    * @brief Set croppedHeight of NPatch
178    *
179    * @param [in] croppedHeight croppedHeight of NPatch
180    */
181   void SetCroppedHeight(uint32_t croppedHeight);
182
183   /**
184    * @brief Retrieve croppedWidth of NPatch.
185    *
186    * @return Return croppedWidth of NPatch.
187    */
188   uint32_t GetCroppedWidth() const;
189
190   /**
191    * @brief Retrieve croppedHeight of NPatch.
192    *
193    * @return Return croppedHeight of NPatch.
194    */
195   uint32_t GetCroppedHeight() const;
196
197   /**
198    * @brief Set border of NPatch.
199    *
200    * @param [in] border border of NPatch
201    */
202   void SetBorder(const Rect<int> border);
203
204   /**
205    * @brief Retrieve border of NPatch.
206    *
207    * @return Return border of NPatch.
208    */
209   Rect<int> GetBorder() const;
210
211   /**
212    * @brief Set whether the loaded image is premultiplied or not
213    *
214    * @param [in] preMultiplyOnLoad whether the loaded image is premultiplied or not
215    */
216   void SetPreMultiplyOnLoad(bool preMultiplyOnLoad);
217
218   /**
219    * @brief Retrieve whether the loaded image is premultiplied or not.
220    *
221    * @return Return true if the image is premultiplied alpha.
222    */
223   bool IsPreMultiplied() const;
224
225   /**
226    * @brief Set current loading state.
227    *
228    * @param [in] loadingState current loading state
229    */
230   void SetLoadingState(const LoadingState loadingState);
231
232   /**
233    * @brief Retrieve current loading state.
234    *
235    * @return Return current loading state.
236    */
237   LoadingState GetLoadingState() const;
238
239   /**
240    * @brief Retrieve NPatch rendering data.
241    *
242    * @return Return NPatch rendering data.
243    */
244   void* GetRenderingMap() const;
245
246   /**
247    * @brief Set loaded pixel buffer for the cache data.
248    *
249    * @param [in] pixelBuffer loaded pixel buffer.
250    * @param [in] preMultiplied whether the loaded image is premultiplied or not
251    */
252   void SetLoadedNPatchData(Devel::PixelBuffer& pixelBuffer, bool preMultiplied);
253
254 private:
255   /**
256    * @copydoc TextureUploadObserver::LoadComplete
257    *
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.
260    */
261   void LoadComplete(bool loadSuccess, TextureInformation textureInformation) override;
262
263 private:
264   using ObserverListType = Dali::Vector<TextureUploadObserver*>;
265
266   NPatchDataId                 mId;
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
279 };
280
281 } // namespace Internal
282
283 } // namespace Toolkit
284
285 } // namespace Dali
286
287 #endif // DALI_TOOLKIT_NPATCH_DATA_H