2 * Copyright (c) 2017 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 )
48 void LoadingTask::Load()
50 if( url.IsLocalResource() )
52 pixelBuffer = Dali::LoadImageFromFile( url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection );
56 pixelBuffer = Dali::DownloadImageSynchronously ( url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection );
59 if( pixelBuffer && Pixel::HasAlpha( pixelBuffer.GetPixelFormat() ) )
61 if( preMultiplyOnLoad == DevelAsyncImageLoader::PreMultiplyOnLoad::ON )
63 pixelBuffer.MultiplyColorByAlpha();
69 ImageLoadThread::ImageLoadThread( EventThreadCallback* trigger )
70 : mTrigger( trigger ),
71 mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
75 ImageLoadThread::~ImageLoadThread()
77 // add an empty task would stop the thread from conditional wait.
85 void ImageLoadThread::Run()
87 SetThreadName( "ImageLoadThread" );
88 mLogFactory.InstallLogFunction();
90 while( LoadingTask* task = NextTaskToProcess() )
93 AddCompletedTask( task );
97 void ImageLoadThread::AddTask( LoadingTask* task )
99 bool wasEmpty = false;
102 // Lock while adding task to the queue
103 ConditionalWait::ScopedLock lock( mConditionalWait );
104 wasEmpty = mLoadQueue.Empty();
105 mLoadQueue.PushBack( task );
110 // wake up the image loading thread
111 mConditionalWait.Notify();
115 LoadingTask* ImageLoadThread::NextCompletedTask()
117 // Lock while popping task out from the queue
118 Mutex::ScopedLock lock( mMutex );
120 if( mCompleteQueue.Empty() )
125 Vector< LoadingTask* >::Iterator next = mCompleteQueue.Begin();
126 LoadingTask* nextTask = *next;
127 mCompleteQueue.Erase( next );
132 bool ImageLoadThread::CancelTask( uint32_t loadingTaskId )
134 // Lock while remove task from the queue
135 ConditionalWait::ScopedLock lock( mConditionalWait );
137 for( Vector< LoadingTask* >::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); ++iter )
139 if( (*iter)->id == loadingTaskId )
142 mLoadQueue.Erase( iter );
151 void ImageLoadThread::CancelAll()
153 // Lock while remove task from the queue
154 ConditionalWait::ScopedLock lock( mConditionalWait );
156 for( Vector< LoadingTask* >::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); ++iter )
163 LoadingTask* ImageLoadThread::NextTaskToProcess()
165 // Lock while popping task out from the queue
166 ConditionalWait::ScopedLock lock( mConditionalWait );
168 while( mLoadQueue.Empty() )
170 mConditionalWait.Wait( lock );
173 Vector< LoadingTask* >::Iterator next = mLoadQueue.Begin();
174 LoadingTask* nextTask = *next;
175 mLoadQueue.Erase( next );
180 void ImageLoadThread::AddCompletedTask( LoadingTask* task )
182 // Lock while adding task to the queue
183 Mutex::ScopedLock lock( mMutex );
184 mCompleteQueue.PushBack( task );
186 // wake up the main thread
190 } // namespace Internal
192 } // namespace Toolkit