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 )
40 dimensions( dimensions ),
41 fittingMode( fittingMode ),
42 samplingMode( samplingMode ),
43 orientationCorrection( orientationCorrection )
47 void LoadingTask::Load()
49 if( url.IsLocalResource() )
51 pixelBuffer = Dali::LoadImageFromFile( url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection );
55 pixelBuffer = Dali::DownloadImageSynchronously ( url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection );
60 ImageLoadThread::ImageLoadThread( EventThreadCallback* trigger )
61 : mTrigger( trigger ),
62 mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
66 ImageLoadThread::~ImageLoadThread()
68 // add an empty task would stop the thread from conditional wait.
76 void ImageLoadThread::Run()
78 SetThreadName( "ImageLoadThread" );
79 mLogFactory.InstallLogFunction();
81 while( LoadingTask* task = NextTaskToProcess() )
84 AddCompletedTask( task );
88 void ImageLoadThread::AddTask( LoadingTask* task )
90 bool wasEmpty = false;
93 // Lock while adding task to the queue
94 ConditionalWait::ScopedLock lock( mConditionalWait );
95 wasEmpty = mLoadQueue.Empty();
96 mLoadQueue.PushBack( task );
101 // wake up the image loading thread
102 mConditionalWait.Notify();
106 LoadingTask* ImageLoadThread::NextCompletedTask()
108 // Lock while popping task out from the queue
109 Mutex::ScopedLock lock( mMutex );
111 if( mCompleteQueue.Empty() )
116 Vector< LoadingTask* >::Iterator next = mCompleteQueue.Begin();
117 LoadingTask* nextTask = *next;
118 mCompleteQueue.Erase( next );
123 bool ImageLoadThread::CancelTask( uint32_t loadingTaskId )
125 // Lock while remove task from the queue
126 ConditionalWait::ScopedLock lock( mConditionalWait );
128 for( Vector< LoadingTask* >::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); ++iter )
130 if( (*iter)->id == loadingTaskId )
133 mLoadQueue.Erase( iter );
142 void ImageLoadThread::CancelAll()
144 // Lock while remove task from the queue
145 ConditionalWait::ScopedLock lock( mConditionalWait );
147 for( Vector< LoadingTask* >::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); ++iter )
154 LoadingTask* ImageLoadThread::NextTaskToProcess()
156 // Lock while popping task out from the queue
157 ConditionalWait::ScopedLock lock( mConditionalWait );
159 while( mLoadQueue.Empty() )
161 mConditionalWait.Wait( lock );
164 Vector< LoadingTask* >::Iterator next = mLoadQueue.Begin();
165 LoadingTask* nextTask = *next;
166 mLoadQueue.Erase( next );
171 void ImageLoadThread::AddCompletedTask( LoadingTask* task )
173 // Lock while adding task to the queue
174 Mutex::ScopedLock lock( mMutex );
175 mCompleteQueue.PushBack( task );
177 // wake up the main thread
181 } // namespace Internal
183 } // namespace Toolkit