2 * Copyright (c) 2019 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>
35 LoadingTask::LoadingTask( uint32_t id, const VisualUrl& url, ImageDimensions dimensions,
36 FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad )
40 dimensions( dimensions ),
41 fittingMode( fittingMode ),
42 samplingMode( samplingMode ),
43 orientationCorrection( orientationCorrection ),
44 preMultiplyOnLoad( preMultiplyOnLoad ),
52 LoadingTask::LoadingTask( uint32_t id, Devel::PixelBuffer pixelBuffer, Devel::PixelBuffer maskPixelBuffer, float contentScale, bool cropToMask,
53 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad )
54 : pixelBuffer( pixelBuffer ),
60 orientationCorrection(),
61 preMultiplyOnLoad( preMultiplyOnLoad ),
63 maskPixelBuffer( maskPixelBuffer ),
64 contentScale( contentScale ),
65 cropToMask( cropToMask )
69 void LoadingTask::Load()
71 if( url.IsLocalResource() )
73 pixelBuffer = Dali::LoadImageFromFile( url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection );
77 pixelBuffer = Dali::DownloadImageSynchronously ( url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection );
81 void LoadingTask::ApplyMask()
83 pixelBuffer.ApplyMask( maskPixelBuffer, contentScale, cropToMask );
86 void LoadingTask::MultiplyAlpha()
88 if( pixelBuffer && Pixel::HasAlpha( pixelBuffer.GetPixelFormat() ) )
90 if( preMultiplyOnLoad == DevelAsyncImageLoader::PreMultiplyOnLoad::ON )
92 pixelBuffer.MultiplyColorByAlpha();
97 ImageLoadThread::ImageLoadThread( EventThreadCallback* trigger )
98 : mTrigger( trigger ),
99 mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
103 ImageLoadThread::~ImageLoadThread()
105 // add an empty task would stop the thread from conditional wait.
113 void ImageLoadThread::Run()
115 SetThreadName( "ImageLoadThread" );
116 mLogFactory.InstallLogFunction();
118 while( LoadingTask* task = NextTaskToProcess() )
120 if( !task->isMaskTask )
128 task->MultiplyAlpha();
130 AddCompletedTask( task );
134 void ImageLoadThread::AddTask( LoadingTask* task )
136 bool wasEmpty = false;
139 // Lock while adding task to the queue
140 ConditionalWait::ScopedLock lock( mConditionalWait );
141 wasEmpty = mLoadQueue.Empty();
142 mLoadQueue.PushBack( task );
147 // wake up the image loading thread
148 mConditionalWait.Notify();
152 LoadingTask* ImageLoadThread::NextCompletedTask()
154 // Lock while popping task out from the queue
155 Mutex::ScopedLock lock( mMutex );
157 if( mCompleteQueue.Empty() )
162 Vector< LoadingTask* >::Iterator next = mCompleteQueue.Begin();
163 LoadingTask* nextTask = *next;
164 mCompleteQueue.Erase( next );
169 bool ImageLoadThread::CancelTask( uint32_t loadingTaskId )
171 // Lock while remove task from the queue
172 ConditionalWait::ScopedLock lock( mConditionalWait );
174 for( Vector< LoadingTask* >::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); ++iter )
176 if( (*iter)->id == loadingTaskId )
179 mLoadQueue.Erase( iter );
188 void ImageLoadThread::CancelAll()
190 // Lock while remove task from the queue
191 ConditionalWait::ScopedLock lock( mConditionalWait );
193 for( Vector< LoadingTask* >::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); ++iter )
200 LoadingTask* ImageLoadThread::NextTaskToProcess()
202 // Lock while popping task out from the queue
203 ConditionalWait::ScopedLock lock( mConditionalWait );
205 while( mLoadQueue.Empty() )
207 mConditionalWait.Wait( lock );
210 Vector< LoadingTask* >::Iterator next = mLoadQueue.Begin();
211 LoadingTask* nextTask = *next;
212 mLoadQueue.Erase( next );
217 void ImageLoadThread::AddCompletedTask( LoadingTask* task )
219 // Lock while adding task to the queue
220 Mutex::ScopedLock lock( mMutex );
221 mCompleteQueue.PushBack( task );
223 // wake up the main thread
227 } // namespace Internal
229 } // namespace Toolkit