d25ea5cf7f163a1fca6b285ff1b56dcc0505832a
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / image-loader / async-image-loader-impl.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include "async-image-loader-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/adaptor-framework/bitmap-loader.h>
23 #include <dali/integration-api/adaptors/adaptor.h>
24
25 namespace Dali
26 {
27
28 namespace Toolkit
29 {
30
31 namespace Internal
32 {
33
34 AsyncImageLoader::AsyncImageLoader()
35 : mLoadedSignal(),
36   mLoadThread( new EventThreadCallback( MakeCallback( this, &AsyncImageLoader::ProcessLoadedImage ) ) ),
37   mLoadTaskId( 0 ),
38   mIsLoadThreadStarted( false )
39 {
40 }
41
42 AsyncImageLoader::~AsyncImageLoader()
43 {
44   mLoadThread.CancelAll();
45 }
46
47 IntrusivePtr<AsyncImageLoader> AsyncImageLoader::New()
48 {
49   IntrusivePtr<AsyncImageLoader> internal = new AsyncImageLoader();
50   return internal;
51 }
52
53 uint32_t AsyncImageLoader::Load( const std::string& url,
54                                  ImageDimensions size,
55                                  FittingMode::Type fittingMode,
56                                  SamplingMode::Type samplingMode,
57                                  bool orientationCorrection )
58 {
59   if( !mIsLoadThreadStarted )
60   {
61     mLoadThread.Start();
62     mIsLoadThreadStarted = true;
63   }
64
65   BitmapLoader loader = BitmapLoader::New( url, size, fittingMode, samplingMode, orientationCorrection );
66
67   mLoadThread.AddTask( new LoadingTask( ++mLoadTaskId, loader ) );
68
69   return mLoadTaskId;
70 }
71
72 Toolkit::AsyncImageLoader::ImageLoadedSignalType& AsyncImageLoader::ImageLoadedSignal()
73 {
74   return mLoadedSignal;
75 }
76
77 bool AsyncImageLoader::Cancel( uint32_t loadingTaskId )
78 {
79   return mLoadThread.CancelTask( loadingTaskId );
80 }
81
82 void AsyncImageLoader::CancelAll()
83 {
84   mLoadThread.CancelAll();
85 }
86
87 void AsyncImageLoader::ProcessLoadedImage()
88 {
89   while( LoadingTask *next =  mLoadThread.NextCompletedTask() )
90   {
91     mLoadedSignal.Emit( next->id, next->loader.GetPixelData() );
92     delete next;
93   }
94 }
95
96 } // namespace Internal
97
98 } // namespace Toolkit
99
100 } // namespace Dali