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 std::string& 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()
47 pixelData = Dali::LoadImageFromFile( url, dimensions, fittingMode, samplingMode, orientationCorrection );
51 ImageLoadThread::ImageLoadThread( EventThreadCallback* trigger )
56 ImageLoadThread::~ImageLoadThread()
58 // add an empty task would stop the thread from conditional wait.
66 void ImageLoadThread::Run()
68 while( LoadingTask* task = NextTaskToProcess() )
71 AddCompletedTask( task );
75 void ImageLoadThread::AddTask( LoadingTask* task )
77 bool wasEmpty = false;
80 // Lock while adding task to the queue
81 ConditionalWait::ScopedLock lock( mConditionalWait );
82 wasEmpty = mLoadQueue.Empty();
83 mLoadQueue.PushBack( task );
88 // wake up the image loading thread
89 mConditionalWait.Notify();
93 LoadingTask* ImageLoadThread::NextCompletedTask()
95 // Lock while popping task out from the queue
96 Mutex::ScopedLock lock( mMutex );
98 if( mCompleteQueue.Empty() )
103 Vector< LoadingTask* >::Iterator next = mCompleteQueue.Begin();
104 LoadingTask* nextTask = *next;
105 mCompleteQueue.Erase( next );
110 bool ImageLoadThread::CancelTask( uint32_t loadingTaskId )
112 // Lock while remove task from the queue
113 ConditionalWait::ScopedLock lock( mConditionalWait );
115 for( Vector< LoadingTask* >::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); ++iter )
117 if( (*iter)->id == loadingTaskId )
120 mLoadQueue.Erase( iter );
129 void ImageLoadThread::CancelAll()
131 // Lock while remove task from the queue
132 ConditionalWait::ScopedLock lock( mConditionalWait );
134 for( Vector< LoadingTask* >::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); ++iter )
141 LoadingTask* ImageLoadThread::NextTaskToProcess()
143 // Lock while popping task out from the queue
144 ConditionalWait::ScopedLock lock( mConditionalWait );
146 while( mLoadQueue.Empty() )
148 mConditionalWait.Wait( lock );
151 Vector< LoadingTask* >::Iterator next = mLoadQueue.Begin();
152 LoadingTask* nextTask = *next;
153 mLoadQueue.Erase( next );
158 void ImageLoadThread::AddCompletedTask( LoadingTask* task )
160 // Lock while adding task to the queue
161 Mutex::ScopedLock lock( mMutex );
162 mCompleteQueue.PushBack( task );
164 // wake up the main thread
168 } // namespace Internal
170 } // namespace Toolkit