Move more public-api headers to devel-api. PART 2
[platform/core/uifw/dali-core.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/devel-api/images/image-operations.h>
29
30 namespace Dali
31 {
32
33 namespace Internal DALI_INTERNAL
34 {
35 class ResourceImage;
36 }
37
38 /**
39  * @brief ResourceImage is an image loaded using a URL
40  *
41  * <h3>ResourceImage Loading</h3>
42  *
43  * When the ResourceImage is created, resource loading will be attempted unless
44  * the ResourceImage is created with IMMEDIATE loading policy or a compatible resource is found in cache.
45  * In case of loading images ON_DEMAND, resource loading will only be attempted if the associated ImageActor
46  * is put on Stage.
47  * Scaling of images to a desired smaller size can be requested by providing desired dimensions,
48  * scaling mode and filter mode to to ResourceImage::New().
49  *
50  * <i>LoadPolicies</i>
51  * - IMMEDIATE: acquire image resource when creating ResourceImage.
52  * - ON_DEMAND: only load in case the associated ImageActor is put on Stage
53  *
54  * <i>Resolution of conflicting policies</i>
55  * If the same image is created more than once with conflicting policies, LoadPolicy "IMMEDIATE" overrides "ON_DEMAND".
56  *
57  * <i>Custom load requests</i>
58  * Size, scaling mode, filter mode, and orientation compensation can be set when requesting an image.
59  *
60  * <i>Compatible resources</i>
61  *
62  * Before loading a new ResourceImage the internal image resource cache is checked by dali.
63  * If there is an image already loaded in memory and is deemed "compatible" with the requested image,
64  * that resource is reused.
65  * This happens for example if a loaded image exists with the same URL, scaling and filtering modes,
66  * and the difference between both of the dimensions is less than a few pixels.
67  *
68  * <i>Reloading images</i>
69  *
70  * The same request used on creating the ResourceImage is re-issued when reloading images.
71  * If the file changed since the last load operation, this might result in a different resource.
72  * Reload only takes effect if both of these conditions apply:
73  * - The ResourceImage has already finished loading
74  * - The ResourceImage is either on Stage or using IMMEDIATE load policy
75  *
76  * Signals
77  * | %Signal Name           | Method                       |
78  * |------------------------|------------------------------|
79  * | image-loading-finished | @ref LoadingFinishedSignal() |
80  */
81 class DALI_IMPORT_API ResourceImage : public Image
82 {
83 public:
84   /**
85    * @brief Resource management options.
86    */
87
88   /**
89    * @brief LoadPolicy controls the way images are loaded into memory.
90    */
91   enum LoadPolicy
92   {
93     IMMEDIATE, ///< load image once it is created (default)
94     ON_DEMAND  ///< delay loading until the image is being used (a related actor is added to Stage)
95   };
96
97   /**
98    * @brief Type of signal for LoadingFinished and Uploaded.
99    */
100   typedef Signal< void (ResourceImage) > ResourceImageSignal;
101
102 public:
103
104   /**
105    * @brief Get the size of an image from disk.
106    *
107    * This function will read the header info from file on disk and is
108    * synchronous, so it should not be used repeatedly or in tight
109    * loops.
110    *
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    */
121   ResourceImage();
122
123   /**
124    * @brief Destructor
125    *
126    * This is non-virtual since derived Handle types must not contain data or virtual methods.
127    */
128   ~ResourceImage();
129
130   /**
131    * @brief This copy constructor is required for (smart) pointer semantics.
132    *
133    * @param [in] handle A reference to the copied handle
134    */
135   ResourceImage( const ResourceImage& handle );
136
137   /**
138    * @brief This assignment operator is required for (smart) pointer semantics.
139    *
140    * @param [in] rhs  A reference to the copied handle
141    * @return A reference to this
142    */
143   ResourceImage& operator=( const ResourceImage& rhs );
144
145   /**
146    * @name ResourceImageFactoryFunctions
147    * Create ResourceImage object instances using these functions.
148    */
149   ///@{
150
151   /**
152    * @brief Create an initialised ResourceImage object.
153    *
154    * Uses defaults for all options.
155    *
156    * @sa Dali::FittingMode::Type Dali::SamplingMode::Type
157    * @param [in] url The URL of the image file to use.
158    * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
159    * @return A handle to a newly allocated object
160    */
161   static ResourceImage New( const std::string& url, bool orientationCorrection = true );
162
163   /**
164    * @brief Create an initialised ResourceImage object.
165    *
166    * @param [in] url The URL of the image file to use.
167    * @param [in] loadPol    The LoadPolicy to apply when loading the image resource.
168    * @param [in] releasePol The ReleasePolicy to apply to Image.
169    * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
170    * @return A handle to a newly allocated object
171    */
172   static ResourceImage New( const std::string& url, LoadPolicy loadPol, ReleasePolicy releasePol, bool orientationCorrection = true );
173
174   /**
175    * @brief Create an initialised ResourceImage object.
176    *
177    * @param [in] url The URL of the image file to use.
178    * @param [in] size The width and height to fit the loaded image to.
179    * @param [in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter.
180    * @param [in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size.
181    * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
182    * @return A handle to a newly allocated object
183    */
184   static ResourceImage New( const std::string& url,
185                             ImageDimensions size,
186                             FittingMode::Type fittingMode = FittingMode::DEFAULT,
187                             SamplingMode::Type samplingMode = SamplingMode::DEFAULT,
188                             bool orientationCorrection = true );
189
190   /**
191    * @brief Create an initialised ResourceImage object.
192    *
193    * @param [in] url The URL of the image file to use.
194    * @param [in] loadPol    The LoadPolicy to apply when loading the image resource.
195    * @param [in] releasePol The ReleasePolicy to apply to Image.
196    * @param [in] size The width and height to fit the loaded image to.
197    * @param [in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter.
198    * @param [in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size.
199    * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
200    * @return A handle to a newly allocated object
201    */
202   static ResourceImage New( const std::string& url,
203                             LoadPolicy loadPol,
204                             ReleasePolicy releasePol,
205                             ImageDimensions size,
206                             FittingMode::Type fittingMode = FittingMode::DEFAULT,
207                             SamplingMode::Type samplingMode = SamplingMode::DEFAULT,
208                             bool orientationCorrection = true );
209
210   ///@}
211
212   /**
213    * @brief Downcast an Object handle to ResourceImage handle.
214    *
215    * If handle points to a ResourceImage object the
216    * downcast produces valid handle. If not the returned handle is left uninitialized.
217    * @param[in] handle to An object
218    * @return handle to a Image object or an uninitialized handle
219    */
220   static ResourceImage DownCast( BaseHandle handle );
221
222   /**
223    * @brief Return load policy.
224    *
225    * @return resource load policy
226    */
227   LoadPolicy GetLoadPolicy() const;
228
229   /**
230    * @brief Query whether the image data has loaded.
231    *
232    * The asynchronous loading begins when the Image object is created.
233    * After the Image object is discarded, the image data will be released from memory.
234    * @return The loading state, either Loading, Success or Failed.
235    */
236   LoadingState GetLoadingState() const;
237
238   /**
239    * @brief Returns the URL of the image.
240    *
241    * @return The URL of the image file.
242    */
243   std::string GetUrl() const;
244
245   /**
246    * @brief Reload image from filesystem.
247    *
248    * The original set of image loading attributes (requested dimensions, scaling
249    * mode and filter mode) are used when requesting the image again.
250    * @note If image is offstage and OnDemand policy is set, the reload request is
251    * ignored.
252    */
253   void Reload();
254
255 public: // Signals
256
257   /**
258    * @brief Emitted when the image data loads successfully, or when the loading fails.
259    *
260    * @return A signal object to Connect() with.
261    */
262   ResourceImageSignal& LoadingFinishedSignal();
263
264 public: // Not intended for application developers
265
266   explicit DALI_INTERNAL ResourceImage( Internal::ResourceImage* );
267 };
268
269 } // namespace Dali
270
271 #endif // __DALI_RESOURCE_IMAGE_H__