1 #ifndef DALI_TOOLKIT_ASYNC_IMAGE_LOADER_H
2 #define DALI_TOOLKIT_ASYNC_IMAGE_LOADER_H
5 * Copyright (c) 2018 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.
22 #include <dali/public-api/object/base-handle.h>
23 #include <dali/public-api/images/image-operations.h>
24 #include <dali/public-api/signals/dali-signal.h>
27 #include <dali-toolkit/public-api/dali-toolkit-common.h>
36 namespace Internal DALI_INTERNAL
38 class AsyncImageLoader;
42 * @addtogroup dali_toolkit_image_loader
47 * @brief The AsyncImageLoader is used to load pixel data from a URL asynchronously.
49 * The images are loaded in a worker thread to avoid blocking the main event thread.
51 * To keep track of the loading images, each load call is assigned an ID (which is returned by the Load() call).
52 * To know when the Load has completed, connect to the ImageLoadedSignal.
53 * This signal should be connected before Load is called (in case the signal is emitted immediately).
55 * Load errors can be detected by checking the PixelData object is valid from within the signal handler.
57 * Note: The PixelData object will automatically be destroyed when it leaves its scope.
62 * class MyClass : public ConnectionTracker
66 * MyCallback( uint32_t loadedTaskId, PixelData pixelData )
68 * // First check if the image loaded correctly.
71 * if( loadedTaskId == mId1 )
73 * // use the loaded pixel data from the first image
75 * else if( loadedTaskId == mId2 )
77 * // use the loaded pixel data from the second image
87 * AsyncImageLoader imageLoader = AsyncImageLoader::New();
89 * // Connect the signal here.
90 * imageLoader.ImageLoadedSignal().Connect( &myObject, &MyClass::MyCallback );
92 * // Invoke the load calls (must do this after connecting the signal to guarantee callbacks occur).
93 * myObject.mId1 = imageLoader.Load( "first_image_url.jpg" );
94 * myObject.mId2 = imageLoader.Load( "second_image_url.jpg" );
98 class DALI_TOOLKIT_API AsyncImageLoader : public BaseHandle
102 typedef Signal< void( uint32_t, PixelData ) > ImageLoadedSignalType; ///< Image loaded signal type @SINCE_1_2_14
107 * @brief Constructor which creates an empty AsyncImageLoader handle.
110 * Use AsyncImageLoader::New() to create an initialised object.
118 * This is non-virtual since derived Handle types must not contain data or virtual methods.
123 * @brief This copy constructor is required for (smart) pointer semantics.
126 * @param[in] handle A reference to the copied handle
128 AsyncImageLoader( const AsyncImageLoader& handle );
131 * @brief This assignment operator is required for (smart) pointer semantics.
134 * @param[in] handle A reference to the copied handle
135 * @return A reference to this
137 AsyncImageLoader& operator=( const AsyncImageLoader& handle );
140 * @brief Creates a new loader to load the image asynchronously in a worker thread.
143 * @return The image loader
145 static AsyncImageLoader New();
148 * @brief Downcasts a handle to AsyncImageLoader handle.
150 * If the handle points to an AsyncImageLoader object, the downcast produces a valid handle.
151 * If not, the returned handle is left uninitialized.
154 * @param[in] handle A handle to an object
155 * @return A handle to a AsyncImageLoader object or an uninitialized handle
157 static AsyncImageLoader DownCast( BaseHandle handle );
160 * @brief Starts an image loading task.
161 * Note: When using this method, the following defaults will be used:
162 * fittingMode = FittingMode::DEFAULT
163 * samplingMode = SamplingMode::BOX_THEN_LINEAR
164 * orientationCorrection = true
169 * @param[in] url The URL of the image file to load
170 * @return The loading task id
172 uint32_t Load( const std::string& url );
175 * @brief Starts an image loading task.
176 * Note: When using this method, the following defaults will be used:
177 * fittingMode = FittingMode::DEFAULT
178 * samplingMode = SamplingMode::BOX_THEN_LINEAR
179 * orientationCorrection = true
184 * @param[in] url The URL of the image file to load
185 * @param[in] dimensions The width and height to fit the loaded image to
186 * @return The loading task id
188 uint32_t Load( const std::string& url, ImageDimensions dimensions );
191 * @brief Starts an image loading task.
195 * @param[in] url The URL of the image file to load
196 * @param[in] dimensions 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 The loading task id
202 uint32_t Load( const std::string& url,
203 ImageDimensions dimensions,
204 FittingMode::Type fittingMode,
205 SamplingMode::Type samplingMode,
206 bool orientationCorrection );
209 * @brief Cancels an image loading task if it is still queueing in the work thread.
212 * @param[in] loadingTaskId The task id returned when invoking the load call.
213 * @return If true, the loading task is removed from the queue, otherwise the loading is already implemented and unable to cancel anymore
215 bool Cancel( uint32_t loadingTaskId );
218 * @brief Cancels all the loading tasks in the queue.
224 * @brief Signal emitted for connected callback functions to get access to the loaded pixel data.
226 * A callback of the following type may be connected:
228 * void YourCallbackName( uint32_t id, PixelData pixelData );
231 * @return A reference to a signal object to Connect() with
233 ImageLoadedSignalType& ImageLoadedSignal();
235 public: // Not intended for developer use
239 * @brief Allows the creation of a AsyncImageLoader handle from an internal pointer.
241 * @note Not intended for application developers
243 * @param[in] impl A pointer to the object
245 explicit DALI_INTERNAL AsyncImageLoader( Internal::AsyncImageLoader* impl );
253 } // namespace Toolkit
257 #endif // DALI_TOOLKIT_ASYNC_IMAGE_LOADER_H