Merge remote-tracking branch 'origin/tizen' into new_text
[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
29 namespace Dali
30 {
31 struct Vector2;
32 class ImageAttributes;
33
34 namespace Internal DALI_INTERNAL
35 {
36 class ResourceImage;
37 }
38
39 /**
40  * @brief ResourceImage is an image loaded using a URL
41  *
42  * <h3>ResourceImage Loading</h3>
43  *
44  * When the ResourceImage is created, resource loading will be attempted unless
45  * the ResourceImage is created with IMMEDIATE loading policy or a compatible resource is found in cache.
46  * In case of loading images ON_DEMAND, resource loading will only be attempted if the associated ImageActor
47  * is put on Stage.
48  * Custom loading requests can be made by providing an ImageAttributes object 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, orientation compensation can be set when requesting an image.
59  * See ImageAttributes for more details.
60  *
61  * <i>Compatible resources</i>
62  *
63  * Before loading a new ResourceImage the internal image resource cache is checked by dali.
64  * If there is an image already loaded in memory and is deemed "compatible" with the requested image,
65  * that resource is reused.
66  * This happens for example if a loaded image exists with the same URL, and the difference between both
67  * of the dimensions is less than 50%.
68  *
69  * <i>Reloading images</i>
70  *
71  * The same request used on creating the ResourceImage is re-issued when reloading images.
72  * If the file changed since the last load operation, this might result in a different resource.
73  * Reload only takes effect if both of these conditions apply:
74  * - The ResourceImage has already finished loading
75  * - The ResourceImage is either on Stage or using IMMEDIATE load policy
76  *
77  * Signals
78  * | %Signal Name           | Method                       |
79  * |------------------------|------------------------------|
80  * | image-loading-finished | @ref LoadingFinishedSignal() |
81  */
82 class DALI_IMPORT_API ResourceImage : public Image
83 {
84 public:
85   /**
86    * @brief Resource management options.
87    */
88
89   /**
90    * @brief LoadPolicy controls the way images are loaded into memory.
91    */
92   enum LoadPolicy
93   {
94     IMMEDIATE, ///< load image once it is created (default)
95     ON_DEMAND  ///< delay loading until the image is being used (a related actor is added to Stage)
96   };
97
98   /**
99    * @brief Type of signal for LoadingFinished and Uploaded.
100    */
101   typedef Signal< void (ResourceImage) > ResourceImageSignal;
102
103 public:
104
105   /**
106    * @brief Get the size of an image from disk.
107    *
108    * This function will read the header info from file on disk and is
109    * synchronous, so it should not be used repeatedly or in tight
110    * loops.
111    *
112    * @param [in] url The URL of the image file.
113    * @return The width and height in pixels of the image.
114    */
115   static Vector2 GetImageSize( const std::string& url );
116
117   /**
118    * @brief Constructor which creates an empty ResourceImage object.
119    *
120    * Use ResourceImage::New(...) to create an initialised object.
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    */
129   ~ResourceImage();
130
131   /**
132    * @brief This copy constructor is required for (smart) pointer semantics.
133    *
134    * @param [in] handle A reference to the copied handle
135    */
136   ResourceImage( const ResourceImage& handle );
137
138   /**
139    * @brief This assignment operator is required for (smart) pointer semantics.
140    *
141    * @param [in] rhs  A reference to the copied handle
142    * @return A reference to this
143    */
144   ResourceImage& operator=( const ResourceImage& rhs );
145
146   /**
147    * @brief Create an initialised ResourceImage object.
148    *
149    * @param [in] url The URL of the image file to use.
150    * @return A handle to a newly allocated object
151    */
152   static ResourceImage New( const std::string& url );
153
154   /**
155    * @brief Create an initialised ResourceImage object.
156    *
157    * @param [in] url The URL of the image file to use.
158    * @param [in] loadPol    The LoadPolicy to apply when loading the image resource.
159    * @param [in] releasePol The ReleasePolicy to apply to Image.
160    * @return A handle to a newly allocated object
161    */
162   static ResourceImage New( const std::string& url, LoadPolicy loadPol, ReleasePolicy releasePol );
163
164   /**
165    * @brief Create an initialised ResourceImage object.
166    *
167    * @param [in] url The URL of the image file to use.
168    * @param [in] attributes Requested parameters for loading (size, scaling etc.).
169    * @return A handle to a newly allocated object
170    */
171   static ResourceImage New( const std::string& url, const ImageAttributes& attributes );
172
173   /**
174    * @brief Create an initialised ResourceImage object.
175    *
176    * @param [in] url The URL of the image file to use.
177    * @param [in] attributes Requested parameters for loading (size, scaling etc.).
178    * @param [in] loadPol    The LoadPolicy to apply when loading the image resource.
179    * @param [in] releasePol The ReleasePolicy to apply to Image.
180    * @return A handle to a newly allocated object
181    */
182   static ResourceImage New( const std::string& url, const ImageAttributes& attributes, LoadPolicy loadPol, ReleasePolicy releasePol );
183
184   /**
185    * @brief Downcast an Object handle to ResourceImage handle.
186    *
187    * If handle points to a ResourceImage object the
188    * downcast produces valid handle. If not the returned handle is left uninitialized.
189    * @param[in] handle to An object
190    * @return handle to a Image object or an uninitialized handle
191    */
192   static ResourceImage DownCast( BaseHandle handle );
193
194   /**
195    * @brief Return load policy.
196    *
197    * @return resource load policy
198    */
199   LoadPolicy GetLoadPolicy() const;
200
201   /**
202    * @brief Query whether the image data has loaded.
203    *
204    * The asynchronous loading begins when the Image object is created.
205    * After the Image object is discarded, the image data will be released from memory.
206    * @return The loading state, either Loading, Success or Failed.
207    */
208   LoadingState GetLoadingState() const;
209
210   /**
211    * @brief Returns the URL of the image.
212    *
213    * @return The URL of the image file.
214    */
215   std::string GetUrl() const;
216
217   /**
218    * @brief Reload image from filesystem.
219    *
220    * The set ImageAttributes are used when requesting the image again.
221    * @note if Image is offstage and OnDemand policy is set, reload request is ignored.
222    */
223   void Reload();
224
225   /**
226    * @brief Get the attributes of an image.
227    *
228    * Only to be used after the image has finished loading.
229    * (Ticket's LoadingSucceeded callback was called)
230    * The returned value will reflect the true image dimensions once the asynchronous loading has finished.
231    * Connect to SignalLoadingFinished or use GetLoadingState to make sure this value is actual.
232    * @pre image should be loaded
233    * @return a copy of the attributes
234    */
235   ImageAttributes GetAttributes() const;
236
237 public: // Signals
238
239   /**
240    * @brief Emitted when the image data loads successfully, or when the loading fails.
241    *
242    * @return A signal object to Connect() with.
243    */
244   ResourceImageSignal& LoadingFinishedSignal();
245
246 public: // Not intended for application developers
247
248   explicit DALI_INTERNAL ResourceImage( Internal::ResourceImage* );
249 };
250
251 } // namespace Dali
252
253 #endif // __DALI_RESOURCE_IMAGE_H__