Tizen 2.4.0 rev3 SDK Public Release
[framework/graphics/dali.git] / dali / public-api / images / resource-image.h
1 #ifndef __DALI_RESOURCE_IMAGE_H__
2 #define __DALI_RESOURCE_IMAGE_H__
3
4 /*
5  * Copyright (c) 2015 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
21 // EXTERNAL INCLUDES
22 #include <string>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/common/loading-state.h>
26 #include <dali/public-api/images/image.h>
27 #include <dali/public-api/signals/dali-signal.h>
28 #include <dali/public-api/images/image-operations.h>
29
30 namespace Dali
31 {
32 /**
33  * @addtogroup dali_core_images
34  * @{
35  */
36
37 namespace Internal DALI_INTERNAL
38 {
39 class ResourceImage;
40 }
41
42 /**
43  * @brief ResourceImage is an image loaded using a URL
44  *
45  * <h3>ResourceImage Loading</h3>
46  *
47  * When the ResourceImage is created, resource loading will be attempted unless
48  * the ResourceImage is created with ResourceImage::IMMEDIATE loading policy or a compatible resource is found in cache.
49  * In case of loading images ResourceImage::ON_DEMAND, resource loading will only be attempted if the associated Dali::Toolkit::ImageView
50  * is put on Stage.
51  * Scaling of images to a desired smaller size can be requested by providing desired dimensions,
52  * scaling mode and filter mode to to ResourceImage::New().
53  *
54  * <i>Custom load requests</i>
55  *
56  * Size, scaling mode, filter mode, and orientation compensation can be set when requesting an image.
57  *
58  * <i>Compatible resources</i>
59  *
60  * Before loading a new ResourceImage the internal image resource cache is checked by dali.
61  * If there is an image already loaded in memory and is deemed "compatible" with the requested image,
62  * that resource is reused.
63  * This happens for example if a loaded image exists with the same URL, scaling and filtering modes,
64  * and the difference between both of the dimensions is less than a few pixels.
65  *
66  * <i>Reloading images</i>
67  *
68  * The same request used on creating the ResourceImage is re-issued when reloading images.
69  * If the file changed since the last load operation, this might result in a different resource.
70  * Reload only takes effect if both of these conditions apply:
71  * - The ResourceImage has already finished loading
72  * - The ResourceImage is either on Stage or using ResourceImage::IMMEDIATE load policy
73  *
74  * @since_tizen 2.4
75  */
76 class DALI_IMPORT_API ResourceImage : public Image
77 {
78 public:
79   /**
80    * @brief Resource management options.
81    */
82
83   /**
84    * @brief LoadPolicy controls the way images are loaded into memory.
85    * @since_tizen 2.4
86    * @remarks This is an experimental feature and might not be supported in the next release.
87    * We do recommend not to use it.
88    */
89   enum LoadPolicy
90   {
91     IMMEDIATE, ///< Load image once it is created (default) @since_tizen 2.4
92     ON_DEMAND  ///< Delay loading until the image is being used (a related actor is added to Stage) @since_tizen 2.4
93   };
94
95   /**
96    * @brief Type of signal for LoadingFinished and Uploaded.
97    * @since_tizen 2.4
98    */
99   typedef Signal< void (ResourceImage) > ResourceImageSignal;
100
101 public:
102
103   /**
104    * @brief Get the size of an image from disk.
105    *
106    * This function will read the header info from file on disk and is
107    * synchronous, so it should not be used repeatedly or in tight
108    * loops.
109    *
110    * @since_tizen 2.4
111    * @param [in] url The URL of the image file.
112    * @return The width and height in pixels of the image.
113    */
114   static ImageDimensions GetImageSize( const std::string& url );
115
116   /**
117    * @brief Constructor which creates an empty ResourceImage object.
118    *
119    * Use ResourceImage::New(...) to create an initialised object.
120    * @since_tizen 2.4
121    */
122   ResourceImage();
123
124   /**
125    * @brief Destructor
126    *
127    * This is non-virtual since derived Handle types must not contain data or virtual methods.
128    * @since_tizen 2.4
129    */
130   ~ResourceImage();
131
132   /**
133    * @brief This copy constructor is required for (smart) pointer semantics.
134    *
135    * @since_tizen 2.4
136    * @param [in] handle A reference to the copied handle
137    */
138   ResourceImage( const ResourceImage& handle );
139
140   /**
141    * @brief This assignment operator is required for (smart) pointer semantics.
142    *
143    * @since_tizen 2.4
144    * @param [in] rhs  A reference to the copied handle
145    * @return A reference to this
146    */
147   ResourceImage& operator=( const ResourceImage& rhs );
148
149   /**
150    * @name ResourceImageFactoryFunctions
151    * Create ResourceImage object instances using these functions.
152    */
153   ///@{
154
155   /**
156    * @brief Create an initialised ResourceImage object.
157    *
158    * Uses defaults for all options.
159    *
160    * @sa Dali::FittingMode::Type Dali::SamplingMode::Type
161    * @since_tizen 2.4
162    * @privlevel public
163    * @privilege %http://tizen.org/privilege/internet
164    * @remarks %http://tizen.org/privilege/internet is needed if @a url is a http or https address.
165    * @param [in] url The URL of the image file to use (can be a local file path, http or https address).
166    * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
167    * @return A handle to a newly allocated object
168    */
169   static ResourceImage New( const std::string& url, bool orientationCorrection = true );
170
171   /**
172    * @brief Create an initialised ResourceImage object.
173    *
174    * @since_tizen 2.4
175    * @privlevel public
176    * @privilege %http://tizen.org/privilege/internet
177    * @remarks ResourceImage::LoadPolicy and Image::ReleasePolicy are experimental features and might not be supported in the next release.
178    * We do recommend not to use this method.
179    * Please refer the Remarks of ResourceImage::LoadPolicy and Image::ReleasePolicy for more information.
180    * @remarks %http://tizen.org/privilege/internet is needed if @a url is a http or https address.
181    * @param [in] url The URL of the image file to use (can be a local file path, http or https address).
182    * @param [in] loadPol    The LoadPolicy to apply when loading the image resource.
183    * @param [in] releasePol The ReleasePolicy to apply to Image.
184    * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
185    * @return A handle to a newly allocated object
186    */
187   static ResourceImage New( const std::string& url, LoadPolicy loadPol, ReleasePolicy releasePol, bool orientationCorrection = true );
188
189   /**
190    * @brief Create an initialised ResourceImage object.
191    *
192    * @since_tizen 2.4
193    * @privlevel public
194    * @privilege %http://tizen.org/privilege/internet
195    * @remarks %http://tizen.org/privilege/internet is needed if @a url is a http or https address.
196    * @param [in] url The URL of the image file to use (can be a local file path, http or https address).
197    * @param [in] size The width and height to fit the loaded image to.
198    * @param [in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter.
199    * @param [in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size.
200    * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
201    * @return A handle to a newly allocated object
202    */
203   static ResourceImage New( const std::string& url,
204                             ImageDimensions size,
205                             FittingMode::Type fittingMode = FittingMode::DEFAULT,
206                             SamplingMode::Type samplingMode = SamplingMode::DEFAULT,
207                             bool orientationCorrection = true );
208
209   /**
210    * @brief Create an initialised ResourceImage object.
211    *
212    * @since_tizen 2.4
213    * @privlevel public
214    * @privilege %http://tizen.org/privilege/internet
215    * @remarks LoadPolicy and ReleasePolicy are experimental features and might not be supported in the next release.
216    * We do recommend not to use this method.
217    * Please refer the Remarks of LoadPolicy and ReleasePolicy for more information.
218    * @remarks %http://tizen.org/privilege/internet is needed if @a url is a http or https address.
219    * @param [in] url The URL of the image file to use (can be a local file path, http or https address).
220    * @param [in] loadPol    The LoadPolicy to apply when loading the image resource.
221    * @param [in] releasePol The ReleasePolicy to apply to Image.
222    * @param [in] size The width and height to fit the loaded image to.
223    * @param [in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter.
224    * @param [in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size.
225    * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
226    * @return A handle to a newly allocated object
227    */
228   static ResourceImage New( const std::string& url,
229                             LoadPolicy loadPol,
230                             ReleasePolicy releasePol,
231                             ImageDimensions size,
232                             FittingMode::Type fittingMode = FittingMode::DEFAULT,
233                             SamplingMode::Type samplingMode = SamplingMode::DEFAULT,
234                             bool orientationCorrection = true );
235
236   ///@}
237
238   /**
239    * @brief Downcast a handle to ResourceImage handle.
240    *
241    * If handle points to a ResourceImage object the
242    * downcast produces valid handle. If not the returned handle is left uninitialized.
243    * @since_tizen 2.4
244    * @param[in] handle Handle to an object
245    * @return Handle to a Image object or an uninitialized handle
246    */
247   static ResourceImage DownCast( BaseHandle handle );
248
249   /**
250    * @brief Return load policy.
251    *
252    * @since_tizen 2.4
253    * @remarks This is an experimental feature and might not be supported in the next release.
254    * We do recommend not to use it.
255    * @return Resource load policy
256    */
257   LoadPolicy GetLoadPolicy() const;
258
259   /**
260    * @brief Query whether the image data has loaded.
261    *
262    * The asynchronous loading begins when the Image object is created.
263    * After the Image object is discarded, the image data will be released from memory.
264    * @since_tizen 2.4
265    * @return The loading state, either Loading, Success or Failed.
266    */
267   LoadingState GetLoadingState() const;
268
269   /**
270    * @brief Returns the URL of the image.
271    *
272    * @since_tizen 2.4
273    * @return The URL of the image file.
274    */
275   std::string GetUrl() const;
276
277   /**
278    * @brief Reload image from filesystem.
279    *
280    * The original set of image loading attributes (requested dimensions, scaling
281    * mode and filter mode) are used when requesting the image again.
282    * @since_tizen 2.4
283    * @note If image is offstage and OnDemand policy is set, the reload request is
284    * ignored.
285    */
286   void Reload();
287
288 public: // Signals
289
290   /**
291    * @brief Emitted when the image data loads successfully, or when the loading fails.
292    *
293    * @since_tizen 2.4
294    * @return A signal object to Connect() with.
295    */
296   ResourceImageSignal& LoadingFinishedSignal();
297
298 public: // Not intended for application developers
299
300   explicit DALI_INTERNAL ResourceImage( Internal::ResourceImage* );
301 };
302
303 /**
304  * @}
305  */
306 } // namespace Dali
307
308 #endif // __DALI_RESOURCE_IMAGE_H__