Async image loading
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / image-loader / async-image-loader.h
1 #ifndef __DALI_TOOLKIT_ASYNC_IMAGE_LOADER_H__
2 #define __DALI_TOOLKIT_ASYNC_IMAGE_LOADER_H__
3
4 /*
5  * Copyright (c) 2016 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 // EXTERNAL INCLUDES
21 #include <string>
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>
25
26 namespace Dali
27 {
28 class PixelData;
29
30 namespace Toolkit
31 {
32
33 namespace Internal DALI_INTERNAL
34 {
35 class AsyncImageLoader;
36 }
37
38 /**
39  *@brief The AysncImageLoader is used to load pixel data from the URL asynchronously.
40  *
41  * The images are loaded in a worker thread to avoid blocking the main event thread.
42  *
43  * Each load call is assigned with an ID, connect to the ImageLoadedSignal and receive the corresponding pixel data by comparing the ID.
44  *
45  * To make sure the signal is always received, the signal should get connected before invoking the load call.
46  * @code
47  * class MyClass : public ConnectionTracker
48  * {
49  * public:
50  *
51  *   MyCallback( uint32_t id, PixelData pixelData)
52  *   {
53  *     if(id == mId1)
54  *     {
55  *       // use the loaded pixel data from the first image
56  *     }
57  *     else if( id == mId2 )
58  *     {
59  *       // use the loaded pixel data from the second image
60  *     }
61  *   }
62  *
63  *   uint32_t mId1;
64  *   uint32_t mId2;
65  * };
66  *
67  * MyClass myObject;
68  * AsyncImageLoader imageLoader = AsyncImageLoader::New();
69  * // connect the signal here
70  * imageLoader.ImageLoadedSignal().Connect( &myObject, &MyClass::MyCallback );
71  * // then invoke the load calls
72  * testCallback.mId1 = imageLoader.Load( "first_image_url.jpg" );
73  * testCallback.mId2 = imageLoader.Load( "second_image_url.jpg" );
74  *
75  * @endcode
76  */
77 class DALI_IMPORT_API AsyncImageLoader : public BaseHandle
78 {
79 public:
80
81   /**
82    * @brief Type of signal for image loading finished.
83    *
84    * The signal is emit with the load ID and its corresponding loaded pixel data
85    */
86   typedef Signal< void( uint32_t, PixelData ) > ImageLoadedSignalType;
87
88 public:
89
90   /**
91    * @brief Constructor which creates an empty AsyncImageLoader handle.
92    *
93    * Use AsyncImageLoader::New() to create an initialised object.
94    */
95   AsyncImageLoader();
96
97   /**
98    * @brief Destructor
99    *
100    * This is non-virtual since derived Handle types must not contain data or virtual methods.
101    */
102   ~AsyncImageLoader();
103
104   /**
105    * @brief This copy constructor is required for (smart) pointer semantics.
106    *
107    * @param [in] handle A reference to the copied handle
108    */
109   AsyncImageLoader( const AsyncImageLoader& handle );
110
111   /**
112    * @brief This assignment operator is required for (smart) pointer semantics.
113    *
114    * @param [in] handle  A reference to the copied handle
115    * @return A reference to this
116    */
117   AsyncImageLoader& operator=( const AsyncImageLoader& handle );
118
119  /*
120   * @brief Create a new loader to load the image asynchronously in a worker thread.
121   *
122   * @return The image loader.
123   */
124   static AsyncImageLoader New();
125
126   /**
127    * @brief Start a image loading task.
128    *
129    * @param[in] url The URL of the image file to load.
130    * @return The loading task id.
131    */
132   uint32_t Load( const std::string& url );
133   /*
134    * @brief Start a image loading task.
135    *
136    * @param[in] url The URL of the image file to load.
137    * @param[in] size The width and height to fit the loaded image to.
138    * @return The loading task id.
139    */
140   uint32_t Load( const std::string& url, ImageDimensions size );
141
142   /*
143    * @brief Start a image loading task.
144    *
145    * @param[in] url The URL of the image file to load.
146    * @param[in] size The width and height to fit the loaded image to.
147    * @param[in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter.
148    * @param[in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size.
149    * @param[in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
150    * @return The loading task id.
151    */
152   uint32_t Load( const std::string& url,
153                  ImageDimensions size,
154                  FittingMode::Type fittingMode,
155                  SamplingMode::Type samplingMode,
156                  bool orientationCorrection );
157
158   /**
159    * @brief Cancel a image loading task if it is still queuing in the work thread.
160    *
161    * @param[in] loadingTaskId The task id returned when invoking the load call.
162    * @return If true, the loading task is removed from the queue, otherwise the loading is already implemented and unable to cancel anymore
163    */
164   bool Cancel( uint32_t loadingTaskId);
165
166   /**
167    * @brief Cancel all the loading tasks in the queue
168    */
169   void CancelAll();
170
171   /**
172    * @brief Signal emit for connected callback functions to get access to the loaded pixel data.
173    *
174    * A callback of the following type may be connected:
175    * @code
176    *   void YourCallbackName( uint32_t id, PixelData pixelData );
177    * @endcode
178    *
179    * @return A signal object to Connect() with.
180    */
181   ImageLoadedSignalType& ImageLoadedSignal();
182
183 public: // Not intended for developer use
184
185   explicit DALI_INTERNAL AsyncImageLoader( Internal::AsyncImageLoader* impl );
186
187 };
188
189 } // namespace Toolkit
190
191 } // namespace Dali
192
193 #endif /* __DALI_TOOLKIT_ASYNC_IMAGE_LOADER_H__ */