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