1 #ifndef DALI_TOOLKIT_IMAGE_LOAD_THREAD_H
2 #define DALI_TOOLKIT_IMAGE_LOAD_THREAD_H
5 * Copyright (c) 2021 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.
21 #include <dali-toolkit/devel-api/image-loader/async-image-loader-devel.h>
22 #include <dali-toolkit/internal/visuals/visual-url.h>
23 #include <dali/devel-api/adaptor-framework/event-thread-callback.h>
24 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
25 #include <dali/devel-api/threading/conditional-wait.h>
26 #include <dali/devel-api/threading/mutex.h>
27 #include <dali/devel-api/threading/thread.h>
28 #include <dali/integration-api/adaptor-framework/log-factory-interface.h>
29 #include <dali/public-api/common/dali-vector.h>
30 #include <dali/public-api/images/image-operations.h>
31 #include <dali/public-api/object/ref-object.h>
32 #include <dali/public-api/adaptor-framework/encoded-image-buffer.h>
41 * The task of loading and packing an image into the atlas.
47 * @param [in] id of the task
48 * @param [in] animatedImageLoading The AnimatedImageLoading to load animated image
49 * @param [in] frameIndex The frame index of a frame to be loaded frame
50 * @param [in] preMultiplyOnLoad ON if the image's color should be multiplied by it's alpha. Set to OFF if there is no alpha or if the image need to be applied alpha mask.
52 LoadingTask(uint32_t id,
53 Dali::AnimatedImageLoading animatedImageLoading,
55 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad);
59 * @param [in] id of the task
60 * @param [in] url The URL of the image file to load.
61 * @param [in] size The width and height to fit the loaded image to, 0.0 means whole image
62 * @param [in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter.
63 * @param [in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size.
64 * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
65 * @param [in] preMultiplyOnLoad ON if the image's color should be multiplied by it's alpha. Set to OFF if there is no alpha or if the image need to be applied alpha mask.
67 LoadingTask(uint32_t id,
69 ImageDimensions dimensions,
70 FittingMode::Type fittingMode,
71 SamplingMode::Type samplingMode,
72 bool orientationCorrection,
73 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad);
77 * @param [in] id of the task
78 * @param [in] encodedImageBuffer The encoded buffer of the image to load.
79 * @param [in] size The width and height to fit the loaded image to, 0.0 means whole image
80 * @param [in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter.
81 * @param [in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size.
82 * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
83 * @param [in] preMultiplyOnLoad ON if the image's color should be multiplied by it's alpha. Set to OFF if there is no alpha or if the image need to be applied alpha mask.
85 LoadingTask(uint32_t id,
86 const EncodedImageBuffer& encodedImageBuffer,
87 ImageDimensions dimensions,
88 FittingMode::Type fittingMode,
89 SamplingMode::Type samplingMode,
90 bool orientationCorrection,
91 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad);
95 * @param [in] id of the task
96 * @param [in] pixelBuffer of the to be masked image
97 * @param [in] maskPixelBuffer of the mask image
98 * @param [in] contentScale The factor to scale the content
99 * @param [in] cropToMask Whether to crop the content to the mask size
100 * @param [in] preMultiplyOnLoad ON if the image's color should be multiplied by it's alpha. Set to OFF if there is no alpha.
102 LoadingTask(uint32_t id,
103 Devel::PixelBuffer pixelBuffer,
104 Devel::PixelBuffer maskPixelBuffer,
107 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad);
122 void MultiplyAlpha();
126 LoadingTask(const LoadingTask& queue);
129 LoadingTask& operator=(const LoadingTask& queue);
132 Devel::PixelBuffer pixelBuffer; ///< pixelBuffer handle after successful load
133 ///< or pixelBuffer to be masked image in the mask task
134 VisualUrl url; ///< url of the image to load
135 EncodedImageBuffer encodedImageBuffer; ///< encoded buffer of the image to load
136 uint32_t id; ///< The unique id associated with this task.
137 ImageDimensions dimensions; ///< dimensions to load
138 FittingMode::Type fittingMode; ///< fitting options
139 SamplingMode::Type samplingMode; ///< sampling options
140 bool orientationCorrection : 1; ///< if orientation correction is needed
141 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad; //< if the image's color should be multiplied by it's alpha
143 bool isMaskTask; ///< whether this task is for mask or not
144 Devel::PixelBuffer maskPixelBuffer; ///< pixelBuffer of mask image
145 float contentScale; ///< The factor to scale the content
146 bool cropToMask; ///< Whether to crop the content to the mask size
147 Dali::AnimatedImageLoading animatedImageLoading;
152 * The worker thread for image loading.
154 class ImageLoadThread : public Thread
160 * @param[in] mTrigger The trigger to wake up the main thread.
162 ImageLoadThread(EventThreadCallback* mTrigger);
167 ~ImageLoadThread() override;
170 * Add a task in to the loading queue
172 * @param[in] task The task added to the queue.
174 * @note This class takes ownership of the task object
176 void AddTask(LoadingTask* task);
179 * Pop the next task out from the completed queue.
181 * @return The next task to be processed.
183 LoadingTask* NextCompletedTask();
186 * Remove the loading task from the waiting queue.
188 bool CancelTask(uint32_t loadingTaskId);
191 * Remove all the loading tasks in the waiting queue.
197 * Pop the next loading task out from the queue to process.
199 * @return The next task to be processed.
201 LoadingTask* NextTaskToProcess();
204 * Add a task in to the loading queue
206 * @param[in] task The task added to the queue.
208 void AddCompletedTask(LoadingTask* task);
212 * The entry function of the worker thread.
213 * It fetches loading task from the loadQueue, loads the image and adds to the completeQueue.
219 ImageLoadThread(const ImageLoadThread& thread);
222 ImageLoadThread& operator=(const ImageLoadThread& thread);
225 Vector<LoadingTask*> mLoadQueue; ///<The task queue with images for loading.
226 Vector<LoadingTask*> mCompleteQueue; ///<The task queue with images loaded.
227 EventThreadCallback* mTrigger;
228 const Dali::LogFactoryInterface& mLogFactory; ///< The log factory
230 ConditionalWait mConditionalWait;
234 } // namespace Internal
236 } // namespace Toolkit
240 #endif // DALI_TOOLKIT_IMAGE_LOAD_THREAD_H