2 * Copyright (c) 2020 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/devel-api/adaptor-framework/thread-settings.h>
24 #include <dali/integration-api/adaptor-framework/adaptor.h>
25 #include <dali/integration-api/debug.h>
36 LoadingTask::LoadingTask( uint32_t id, Dali::AnimatedImageLoading animatedImageLoading, uint32_t frameIndex )
43 orientationCorrection(),
44 preMultiplyOnLoad( DevelAsyncImageLoader::PreMultiplyOnLoad::OFF ),
49 animatedImageLoading( animatedImageLoading ),
50 frameIndex( frameIndex )
54 LoadingTask::LoadingTask( uint32_t id, const VisualUrl& url, ImageDimensions dimensions,
55 FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad )
59 dimensions( dimensions ),
60 fittingMode( fittingMode ),
61 samplingMode( samplingMode ),
62 orientationCorrection( orientationCorrection ),
63 preMultiplyOnLoad( preMultiplyOnLoad ),
68 animatedImageLoading(),
73 LoadingTask::LoadingTask( uint32_t id, Devel::PixelBuffer pixelBuffer, Devel::PixelBuffer maskPixelBuffer, float contentScale, bool cropToMask,
74 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad )
75 : pixelBuffer( pixelBuffer ),
81 orientationCorrection(),
82 preMultiplyOnLoad( preMultiplyOnLoad ),
84 maskPixelBuffer( maskPixelBuffer ),
85 contentScale( contentScale ),
86 cropToMask( cropToMask ),
87 animatedImageLoading(),
92 void LoadingTask::Load()
94 if( animatedImageLoading )
96 pixelBuffer = animatedImageLoading.LoadFrame( frameIndex );
98 else if( url.IsLocalResource() )
100 pixelBuffer = Dali::LoadImageFromFile( url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection );
104 pixelBuffer = Dali::DownloadImageSynchronously ( url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection );
109 DALI_LOG_ERROR( "LoadingTask::Load: Loading is failed: %s\n", url.GetUrl().c_str() );
113 void LoadingTask::ApplyMask()
115 pixelBuffer.ApplyMask( maskPixelBuffer, contentScale, cropToMask );
118 void LoadingTask::MultiplyAlpha()
120 if( pixelBuffer && Pixel::HasAlpha( pixelBuffer.GetPixelFormat() ) )
122 if( preMultiplyOnLoad == DevelAsyncImageLoader::PreMultiplyOnLoad::ON )
124 pixelBuffer.MultiplyColorByAlpha();
129 ImageLoadThread::ImageLoadThread( EventThreadCallback* trigger )
130 : mTrigger( trigger ),
131 mLogFactory( Dali::Adaptor::Get().GetLogFactory() )
135 ImageLoadThread::~ImageLoadThread()
137 // add an empty task would stop the thread from conditional wait.
145 void ImageLoadThread::Run()
147 SetThreadName( "ImageLoadThread" );
148 mLogFactory.InstallLogFunction();
150 while( LoadingTask* task = NextTaskToProcess() )
152 if( !task->isMaskTask )
160 task->MultiplyAlpha();
162 AddCompletedTask( task );
166 void ImageLoadThread::AddTask( LoadingTask* task )
168 bool wasEmpty = false;
170 // Lock while adding task to the queue
171 ConditionalWait::ScopedLock lock( mConditionalWait );
172 wasEmpty = mLoadQueue.Empty();
173 mLoadQueue.PushBack( task );
178 // wake up the image loading thread
179 mConditionalWait.Notify();
183 LoadingTask* ImageLoadThread::NextCompletedTask()
185 // Lock while popping task out from the queue
186 Mutex::ScopedLock lock( mMutex );
188 if( mCompleteQueue.Empty() )
193 Vector< LoadingTask* >::Iterator next = mCompleteQueue.Begin();
194 LoadingTask* nextTask = *next;
195 mCompleteQueue.Erase( next );
200 bool ImageLoadThread::CancelTask( uint32_t loadingTaskId )
202 // Lock while remove task from the queue
203 ConditionalWait::ScopedLock lock( mConditionalWait );
205 for( Vector< LoadingTask* >::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); ++iter )
207 if( (*iter)->id == loadingTaskId )
210 mLoadQueue.Erase( iter );
219 void ImageLoadThread::CancelAll()
221 // Lock while remove task from the queue
222 ConditionalWait::ScopedLock lock( mConditionalWait );
224 for( Vector< LoadingTask* >::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); ++iter )
231 LoadingTask* ImageLoadThread::NextTaskToProcess()
233 // Lock while popping task out from the queue
234 ConditionalWait::ScopedLock lock( mConditionalWait );
236 while( mLoadQueue.Empty() )
238 mConditionalWait.Wait( lock );
241 Vector< LoadingTask* >::Iterator next = mLoadQueue.Begin();
242 LoadingTask* nextTask = *next;
243 mLoadQueue.Erase( next );
248 void ImageLoadThread::AddCompletedTask( LoadingTask* task )
250 // Lock while adding task to the queue
251 Mutex::ScopedLock lock( mMutex );
252 mCompleteQueue.PushBack( task );
254 // wake up the main thread
258 } // namespace Internal
260 } // namespace Toolkit