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>
25 #include <dali/integration-api/debug.h>
36 LoadingTask::LoadingTask( uint32_t id, const VisualUrl& url, ImageDimensions dimensions,
37 FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad )
41 dimensions( dimensions ),
42 fittingMode( fittingMode ),
43 samplingMode( samplingMode ),
44 orientationCorrection( orientationCorrection ),
45 preMultiplyOnLoad( preMultiplyOnLoad ),
53 LoadingTask::LoadingTask( uint32_t id, Devel::PixelBuffer pixelBuffer, Devel::PixelBuffer maskPixelBuffer, float contentScale, bool cropToMask,
54 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad )
55 : pixelBuffer( pixelBuffer ),
61 orientationCorrection(),
62 preMultiplyOnLoad( preMultiplyOnLoad ),
64 maskPixelBuffer( maskPixelBuffer ),
65 contentScale( contentScale ),
66 cropToMask( cropToMask )
70 void LoadingTask::Load()
72 if( url.IsLocalResource() )
74 pixelBuffer = Dali::LoadImageFromFile( url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection );
78 pixelBuffer = Dali::DownloadImageSynchronously ( url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection );
83 DALI_LOG_ERROR( "LoadingTask::Load: Loading is failed: %s\n", url.GetUrl().c_str() );
87 void LoadingTask::ApplyMask()
89 pixelBuffer.ApplyMask( maskPixelBuffer, contentScale, cropToMask );
92 void LoadingTask::MultiplyAlpha()
94 if( pixelBuffer && Pixel::HasAlpha( pixelBuffer.GetPixelFormat() ) )
96 if( preMultiplyOnLoad == DevelAsyncImageLoader::PreMultiplyOnLoad::ON )
98 pixelBuffer.MultiplyColorByAlpha();
103 ImageLoadThread::ImageLoadThread( EventThreadCallback* trigger )
104 : mTrigger( trigger ),
105 mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
109 ImageLoadThread::~ImageLoadThread()
111 // add an empty task would stop the thread from conditional wait.
119 void ImageLoadThread::Run()
121 SetThreadName( "ImageLoadThread" );
122 mLogFactory.InstallLogFunction();
124 while( LoadingTask* task = NextTaskToProcess() )
126 if( !task->isMaskTask )
134 task->MultiplyAlpha();
136 AddCompletedTask( task );
140 void ImageLoadThread::AddTask( LoadingTask* task )
142 bool wasEmpty = false;
145 // Lock while adding task to the queue
146 ConditionalWait::ScopedLock lock( mConditionalWait );
147 wasEmpty = mLoadQueue.Empty();
148 mLoadQueue.PushBack( task );
153 // wake up the image loading thread
154 mConditionalWait.Notify();
158 LoadingTask* ImageLoadThread::NextCompletedTask()
160 // Lock while popping task out from the queue
161 Mutex::ScopedLock lock( mMutex );
163 if( mCompleteQueue.Empty() )
168 Vector< LoadingTask* >::Iterator next = mCompleteQueue.Begin();
169 LoadingTask* nextTask = *next;
170 mCompleteQueue.Erase( next );
175 bool ImageLoadThread::CancelTask( uint32_t loadingTaskId )
177 // Lock while remove task from the queue
178 ConditionalWait::ScopedLock lock( mConditionalWait );
180 for( Vector< LoadingTask* >::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); ++iter )
182 if( (*iter)->id == loadingTaskId )
185 mLoadQueue.Erase( iter );
194 void ImageLoadThread::CancelAll()
196 // Lock while remove task from the queue
197 ConditionalWait::ScopedLock lock( mConditionalWait );
199 for( Vector< LoadingTask* >::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); ++iter )
206 LoadingTask* ImageLoadThread::NextTaskToProcess()
208 // Lock while popping task out from the queue
209 ConditionalWait::ScopedLock lock( mConditionalWait );
211 while( mLoadQueue.Empty() )
213 mConditionalWait.Wait( lock );
216 Vector< LoadingTask* >::Iterator next = mLoadQueue.Begin();
217 LoadingTask* nextTask = *next;
218 mLoadQueue.Erase( next );
223 void ImageLoadThread::AddCompletedTask( LoadingTask* task )
225 // Lock while adding task to the queue
226 Mutex::ScopedLock lock( mMutex );
227 mCompleteQueue.PushBack( task );
229 // wake up the main thread
233 } // namespace Internal
235 } // namespace Toolkit