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/integration-api/adaptors/adaptor.h>
24 #include <dali/devel-api/adaptor-framework/thread-settings.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 : pixelBuffer( pixelBuffer ),
59 orientationCorrection(),
62 maskPixelBuffer( maskPixelBuffer ),
63 contentScale( contentScale ),
64 cropToMask( cropToMask )
68 void LoadingTask::Load()
70 if( url.IsLocalResource() )
72 pixelBuffer = Dali::LoadImageFromFile( url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection );
76 pixelBuffer = Dali::DownloadImageSynchronously ( url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection );
79 if( pixelBuffer && Pixel::HasAlpha( pixelBuffer.GetPixelFormat() ) )
81 if( preMultiplyOnLoad == DevelAsyncImageLoader::PreMultiplyOnLoad::ON )
83 pixelBuffer.MultiplyColorByAlpha();
88 void LoadingTask::ApplyMask()
90 pixelBuffer.ApplyMask( maskPixelBuffer, contentScale, cropToMask );
93 ImageLoadThread::ImageLoadThread( EventThreadCallback* trigger )
94 : mTrigger( trigger ),
95 mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
99 ImageLoadThread::~ImageLoadThread()
101 // add an empty task would stop the thread from conditional wait.
109 void ImageLoadThread::Run()
111 SetThreadName( "ImageLoadThread" );
112 mLogFactory.InstallLogFunction();
114 while( LoadingTask* task = NextTaskToProcess() )
116 if( !task->isMaskTask )
125 AddCompletedTask( task );
129 void ImageLoadThread::AddTask( LoadingTask* task )
131 bool wasEmpty = false;
134 // Lock while adding task to the queue
135 ConditionalWait::ScopedLock lock( mConditionalWait );
136 wasEmpty = mLoadQueue.Empty();
137 mLoadQueue.PushBack( task );
142 // wake up the image loading thread
143 mConditionalWait.Notify();
147 LoadingTask* ImageLoadThread::NextCompletedTask()
149 // Lock while popping task out from the queue
150 Mutex::ScopedLock lock( mMutex );
152 if( mCompleteQueue.Empty() )
157 Vector< LoadingTask* >::Iterator next = mCompleteQueue.Begin();
158 LoadingTask* nextTask = *next;
159 mCompleteQueue.Erase( next );
164 bool ImageLoadThread::CancelTask( uint32_t loadingTaskId )
166 // Lock while remove task from the queue
167 ConditionalWait::ScopedLock lock( mConditionalWait );
169 for( Vector< LoadingTask* >::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); ++iter )
171 if( (*iter)->id == loadingTaskId )
174 mLoadQueue.Erase( iter );
183 void ImageLoadThread::CancelAll()
185 // Lock while remove task from the queue
186 ConditionalWait::ScopedLock lock( mConditionalWait );
188 for( Vector< LoadingTask* >::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); ++iter )
195 LoadingTask* ImageLoadThread::NextTaskToProcess()
197 // Lock while popping task out from the queue
198 ConditionalWait::ScopedLock lock( mConditionalWait );
200 while( mLoadQueue.Empty() )
202 mConditionalWait.Wait( lock );
205 Vector< LoadingTask* >::Iterator next = mLoadQueue.Begin();
206 LoadingTask* nextTask = *next;
207 mLoadQueue.Erase( next );
212 void ImageLoadThread::AddCompletedTask( LoadingTask* task )
214 // Lock while adding task to the queue
215 Mutex::ScopedLock lock( mMutex );
216 mCompleteQueue.PushBack( task );
218 // wake up the main thread
222 } // namespace Internal
224 } // namespace Toolkit