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>
34 LoadingTask::LoadingTask( uint32_t id, const VisualUrl& url, ImageDimensions dimensions,
35 FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection )
39 dimensions( dimensions ),
40 fittingMode( fittingMode ),
41 samplingMode( samplingMode ),
42 orientationCorrection( orientationCorrection )
46 void LoadingTask::Load()
48 if( url.IsLocalResource() )
50 pixelBuffer = Dali::LoadImageFromFile( url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection );
54 pixelBuffer = Dali::DownloadImageSynchronously ( url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection );
59 ImageLoadThread::ImageLoadThread( EventThreadCallback* trigger )
60 : mTrigger( trigger ),
61 mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
65 ImageLoadThread::~ImageLoadThread()
67 // add an empty task would stop the thread from conditional wait.
75 void ImageLoadThread::Run()
77 mLogFactory.InstallLogFunction();
79 while( LoadingTask* task = NextTaskToProcess() )
82 AddCompletedTask( task );
86 void ImageLoadThread::AddTask( LoadingTask* task )
88 bool wasEmpty = false;
91 // Lock while adding task to the queue
92 ConditionalWait::ScopedLock lock( mConditionalWait );
93 wasEmpty = mLoadQueue.Empty();
94 mLoadQueue.PushBack( task );
99 // wake up the image loading thread
100 mConditionalWait.Notify();
104 LoadingTask* ImageLoadThread::NextCompletedTask()
106 // Lock while popping task out from the queue
107 Mutex::ScopedLock lock( mMutex );
109 if( mCompleteQueue.Empty() )
114 Vector< LoadingTask* >::Iterator next = mCompleteQueue.Begin();
115 LoadingTask* nextTask = *next;
116 mCompleteQueue.Erase( next );
121 bool ImageLoadThread::CancelTask( uint32_t loadingTaskId )
123 // Lock while remove task from the queue
124 ConditionalWait::ScopedLock lock( mConditionalWait );
126 for( Vector< LoadingTask* >::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); ++iter )
128 if( (*iter)->id == loadingTaskId )
131 mLoadQueue.Erase( iter );
140 void ImageLoadThread::CancelAll()
142 // Lock while remove task from the queue
143 ConditionalWait::ScopedLock lock( mConditionalWait );
145 for( Vector< LoadingTask* >::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); ++iter )
152 LoadingTask* ImageLoadThread::NextTaskToProcess()
154 // Lock while popping task out from the queue
155 ConditionalWait::ScopedLock lock( mConditionalWait );
157 while( mLoadQueue.Empty() )
159 mConditionalWait.Wait( lock );
162 Vector< LoadingTask* >::Iterator next = mLoadQueue.Begin();
163 LoadingTask* nextTask = *next;
164 mLoadQueue.Erase( next );
169 void ImageLoadThread::AddCompletedTask( LoadingTask* task )
171 // Lock while adding task to the queue
172 Mutex::ScopedLock lock( mMutex );
173 mCompleteQueue.PushBack( task );
175 // wake up the main thread
179 } // namespace Internal
181 } // namespace Toolkit