1 #ifndef __DALI_RESOURCE_IMAGE_H__
2 #define __DALI_RESOURCE_IMAGE_H__
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
32 class ImageAttributes;
34 namespace Internal DALI_INTERNAL
40 * @brief ResourceImage is an image loaded using a URL
42 * <h3>ResourceImage Loading</h3>
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
48 * Custom loading requests can be made by providing an ImageAttributes object to ResourceImage::New().
51 * - IMMEDIATE: acquire image resource when creating ResourceImage.
52 * - ON_DEMAND: only load in case the associated ImageActor is put on Stage
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".
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.
61 * <i>Compatible resources</i>
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%.
69 * <i>Reloading images</i>
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
77 class DALI_IMPORT_API ResourceImage : public Image
81 * @brief Resource management options.
85 * @brief LoadPolicy controls the way images are loaded into memory.
89 IMMEDIATE, ///< load image once it is created (default)
90 ON_DEMAND ///< delay loading until the image is being used (a related actor is added to Stage)
94 * @brief Type of signal for LoadingFinished and Uploaded.
96 typedef Signal< void (ResourceImage) > ResourceImageSignal;
99 static const char* const SIGNAL_IMAGE_LOADING_FINISHED; ///< Name of LoadingFinished signal
104 * @brief Get the size of an image from disk.
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
110 * @param [in] url The URL of the image file.
111 * @return The width and height in pixels of the image.
113 static Vector2 GetImageSize( const std::string& url );
116 * @brief Constructor which creates an empty ResourceImage object.
118 * Use ResourceImage::New(...) to create an initialised object.
125 * This is non-virtual since derived Handle types must not contain data or virtual methods.
130 * @brief This copy constructor is required for (smart) pointer semantics.
132 * @param [in] handle A reference to the copied handle
134 ResourceImage( const ResourceImage& handle );
137 * @brief This assignment operator is required for (smart) pointer semantics.
139 * @param [in] rhs A reference to the copied handle
140 * @return A reference to this
142 ResourceImage& operator=( const ResourceImage& rhs );
145 * @brief Create an initialised ResourceImage object.
147 * @param [in] url The URL of the image file to use.
148 * @return A handle to a newly allocated object
150 static ResourceImage New( const std::string& url );
153 * @brief Create an initialised ResourceImage object.
155 * @param [in] url The URL of the image file to use.
156 * @param [in] loadPol The LoadPolicy to apply when loading the image resource.
157 * @param [in] releasePol The ReleasePolicy to apply to Image.
158 * @return A handle to a newly allocated object
160 static ResourceImage New( const std::string& url, LoadPolicy loadPol, ReleasePolicy releasePol );
163 * @brief Create an initialised ResourceImage object.
165 * @param [in] url The URL of the image file to use.
166 * @param [in] attributes Requested parameters for loading (size, scaling etc.).
167 * @return A handle to a newly allocated object
169 static ResourceImage New( const std::string& url, const ImageAttributes& attributes );
172 * @brief Create an initialised ResourceImage object.
174 * @param [in] url The URL of the image file to use.
175 * @param [in] attributes Requested parameters for loading (size, scaling etc.).
176 * @param [in] loadPol The LoadPolicy to apply when loading the image resource.
177 * @param [in] releasePol The ReleasePolicy to apply to Image.
178 * @return A handle to a newly allocated object
180 static ResourceImage New( const std::string& url, const ImageAttributes& attributes, LoadPolicy loadPol, ReleasePolicy releasePol );
183 * @brief Downcast an Object handle to ResourceImage handle.
185 * If handle points to a ResourceImage object the
186 * downcast produces valid handle. If not the returned handle is left uninitialized.
187 * @param[in] handle to An object
188 * @return handle to a Image object or an uninitialized handle
190 static ResourceImage DownCast( BaseHandle handle );
193 * @brief Return load policy.
195 * @return resource load policy
197 LoadPolicy GetLoadPolicy() const;
200 * @brief Query whether the image data has loaded.
202 * The asynchronous loading begins when the Image object is created.
203 * After the Image object is discarded, the image data will be released from memory.
204 * @return The loading state, either Loading, Success or Failed.
206 LoadingState GetLoadingState() const;
209 * @brief Returns the URL of the image.
211 * @return The URL of the image file.
213 std::string GetUrl() const;
216 * @brief Reload image from filesystem.
218 * The set ImageAttributes are used when requesting the image again.
219 * @note if Image is offstage and OnDemand policy is set, reload request is ignored.
224 * @brief Get the attributes of an image.
226 * Only to be used after the image has finished loading.
227 * (Ticket's LoadingSucceeded callback was called)
228 * The returned value will reflect the true image dimensions once the asynchronous loading has finished.
229 * Connect to SignalLoadingFinished or use GetLoadingState to make sure this value is actual.
230 * @pre image should be loaded
231 * @return a copy of the attributes
233 ImageAttributes GetAttributes() const;
238 * @brief Emitted when the image data loads successfully, or when the loading fails.
240 * @return A signal object to Connect() with.
242 ResourceImageSignal& LoadingFinishedSignal();
244 public: // Not intended for application developers
246 explicit DALI_INTERNAL ResourceImage( Internal::ResourceImage* );
251 #endif // __DALI_RESOURCE_IMAGE_H__