2 * Copyright (c) 2015 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>
33 LoadingTask::LoadingTask( uint32_t id, const VisualUrl& url, ImageDimensions dimensions,
34 FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection )
38 dimensions( dimensions ),
39 fittingMode( fittingMode ),
40 samplingMode( samplingMode ),
41 orientationCorrection( orientationCorrection )
45 void LoadingTask::Load()
49 pixelData = Dali::LoadImageFromFile( url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection );
53 pixelData = Dali::DownloadImageSynchronously ( url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection );
58 ImageLoadThread::ImageLoadThread( EventThreadCallback* trigger )
63 ImageLoadThread::~ImageLoadThread()
65 // add an empty task would stop the thread from conditional wait.
73 void ImageLoadThread::Run()
75 while( LoadingTask* task = NextTaskToProcess() )
78 AddCompletedTask( task );
82 void ImageLoadThread::AddTask( LoadingTask* task )
84 bool wasEmpty = false;
87 // Lock while adding task to the queue
88 ConditionalWait::ScopedLock lock( mConditionalWait );
89 wasEmpty = mLoadQueue.Empty();
90 mLoadQueue.PushBack( task );
95 // wake up the image loading thread
96 mConditionalWait.Notify();
100 LoadingTask* ImageLoadThread::NextCompletedTask()
102 // Lock while popping task out from the queue
103 Mutex::ScopedLock lock( mMutex );
105 if( mCompleteQueue.Empty() )
110 Vector< LoadingTask* >::Iterator next = mCompleteQueue.Begin();
111 LoadingTask* nextTask = *next;
112 mCompleteQueue.Erase( next );
117 bool ImageLoadThread::CancelTask( uint32_t loadingTaskId )
119 // Lock while remove task from the queue
120 ConditionalWait::ScopedLock lock( mConditionalWait );
122 for( Vector< LoadingTask* >::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); ++iter )
124 if( (*iter)->id == loadingTaskId )
127 mLoadQueue.Erase( iter );
136 void ImageLoadThread::CancelAll()
138 // Lock while remove task from the queue
139 ConditionalWait::ScopedLock lock( mConditionalWait );
141 for( Vector< LoadingTask* >::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); ++iter )
148 LoadingTask* ImageLoadThread::NextTaskToProcess()
150 // Lock while popping task out from the queue
151 ConditionalWait::ScopedLock lock( mConditionalWait );
153 while( mLoadQueue.Empty() )
155 mConditionalWait.Wait( lock );
158 Vector< LoadingTask* >::Iterator next = mLoadQueue.Begin();
159 LoadingTask* nextTask = *next;
160 mLoadQueue.Erase( next );
165 void ImageLoadThread::AddCompletedTask( LoadingTask* task )
167 // Lock while adding task to the queue
168 Mutex::ScopedLock lock( mMutex );
169 mCompleteQueue.PushBack( task );
171 // wake up the main thread
175 } // namespace Internal
177 } // namespace Toolkit