2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "image-load-thread.h"
22 #include <dali/devel-api/adaptor-framework/image-loading.h>
23 #include <dali/devel-api/adaptor-framework/thread-settings.h>
24 #include <dali/integration-api/adaptor-framework/adaptor.h>
25 #include <dali/integration-api/debug.h>
36 LoadingTask::LoadingTask( uint32_t id, Dali::AnimatedImageLoading animatedImageLoading, uint32_t frameIndex )
43 orientationCorrection(),
44 preMultiplyOnLoad( DevelAsyncImageLoader::PreMultiplyOnLoad::OFF ),
49 animatedImageLoading( animatedImageLoading ),
50 frameIndex( frameIndex )
54 LoadingTask::LoadingTask( uint32_t id, const VisualUrl& url, ImageDimensions dimensions,
55 FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad )
59 dimensions( dimensions ),
60 fittingMode( fittingMode ),
61 samplingMode( samplingMode ),
62 orientationCorrection( orientationCorrection ),
63 preMultiplyOnLoad( preMultiplyOnLoad ),
68 animatedImageLoading(),
73 LoadingTask::LoadingTask( uint32_t id, Devel::PixelBuffer pixelBuffer, Devel::PixelBuffer maskPixelBuffer, float contentScale, bool cropToMask,
74 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad )
75 : pixelBuffer( pixelBuffer ),
81 orientationCorrection(),
82 preMultiplyOnLoad( preMultiplyOnLoad ),
84 maskPixelBuffer( maskPixelBuffer ),
85 contentScale( contentScale ),
86 cropToMask( cropToMask ),
87 animatedImageLoading(),
92 void LoadingTask::Load()
94 if( animatedImageLoading )
96 pixelBuffer = animatedImageLoading.LoadFrame( frameIndex );
98 else if( url.IsLocalResource() )
100 pixelBuffer = Dali::LoadImageFromFile( url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection );
104 pixelBuffer = Dali::DownloadImageSynchronously ( url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection );
109 DALI_LOG_ERROR( "LoadingTask::Load: Loading is failed: %s\n", url.GetUrl().c_str() );
113 void LoadingTask::ApplyMask()
115 pixelBuffer.ApplyMask( maskPixelBuffer, contentScale, cropToMask );
118 void LoadingTask::MultiplyAlpha()
120 if( pixelBuffer && Pixel::HasAlpha( pixelBuffer.GetPixelFormat() ) )
122 if( preMultiplyOnLoad == DevelAsyncImageLoader::PreMultiplyOnLoad::ON )
124 pixelBuffer.MultiplyColorByAlpha();
129 ImageLoadThread::ImageLoadThread( EventThreadCallback* trigger )
130 : mTrigger( trigger ),
131 mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
135 ImageLoadThread::~ImageLoadThread()
137 // add an empty task would stop the thread from conditional wait.
144 for( auto&& iter : mLoadQueue )
150 for( auto&& iter : mCompleteQueue )
154 mCompleteQueue.Clear();
157 void ImageLoadThread::Run()
159 SetThreadName( "ImageLoadThread" );
160 mLogFactory.InstallLogFunction();
162 while( LoadingTask* task = NextTaskToProcess() )
164 if( !task->isMaskTask )
172 task->MultiplyAlpha();
174 AddCompletedTask( task );
178 void ImageLoadThread::AddTask( LoadingTask* task )
180 bool wasEmpty = false;
182 // Lock while adding task to the queue
183 ConditionalWait::ScopedLock lock( mConditionalWait );
184 wasEmpty = mLoadQueue.Empty();
185 mLoadQueue.PushBack( task );
190 // wake up the image loading thread
191 mConditionalWait.Notify();
195 LoadingTask* ImageLoadThread::NextCompletedTask()
197 // Lock while popping task out from the queue
198 Mutex::ScopedLock lock( mMutex );
200 if( mCompleteQueue.Empty() )
205 Vector< LoadingTask* >::Iterator next = mCompleteQueue.Begin();
206 LoadingTask* nextTask = *next;
207 mCompleteQueue.Erase( next );
212 bool ImageLoadThread::CancelTask( uint32_t loadingTaskId )
214 // Lock while remove task from the queue
215 ConditionalWait::ScopedLock lock( mConditionalWait );
217 for( Vector< LoadingTask* >::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); ++iter )
219 if( (*iter)->id == loadingTaskId )
222 mLoadQueue.Erase( iter );
231 void ImageLoadThread::CancelAll()
233 // Lock while remove task from the queue
234 ConditionalWait::ScopedLock lock( mConditionalWait );
236 for( Vector< LoadingTask* >::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); ++iter )
243 LoadingTask* ImageLoadThread::NextTaskToProcess()
245 // Lock while popping task out from the queue
246 ConditionalWait::ScopedLock lock( mConditionalWait );
248 while( mLoadQueue.Empty() )
250 mConditionalWait.Wait( lock );
253 Vector< LoadingTask* >::Iterator next = mLoadQueue.Begin();
254 LoadingTask* nextTask = *next;
255 mLoadQueue.Erase( next );
260 void ImageLoadThread::AddCompletedTask( LoadingTask* task )
262 // Lock while adding task to the queue
263 Mutex::ScopedLock lock( mMutex );
264 mCompleteQueue.PushBack( task );
266 // wake up the main thread
270 } // namespace Internal
272 } // namespace Toolkit