6fe8eba5cda26f487120493e861510ff72da19d6
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / image-loader / async-image-loader-impl.cpp
1 /*
2  * Copyright (c) 2019 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/integration-api/adaptor-framework/adaptor.h>
23
24 namespace Dali
25 {
26
27 namespace Toolkit
28 {
29
30 namespace Internal
31 {
32
33 AsyncImageLoader::AsyncImageLoader()
34 : mLoadedSignal(),
35   mLoadThread( new EventThreadCallback( MakeCallback( this, &AsyncImageLoader::ProcessLoadedImage ) ) ),
36   mLoadTaskId( 0u ),
37   mIsLoadThreadStarted( false )
38 {
39 }
40
41 AsyncImageLoader::~AsyncImageLoader()
42 {
43   mLoadThread.CancelAll();
44 }
45
46 IntrusivePtr<AsyncImageLoader> AsyncImageLoader::New()
47 {
48   IntrusivePtr<AsyncImageLoader> internal = new AsyncImageLoader();
49   return internal;
50 }
51
52 uint32_t AsyncImageLoader::Load( const VisualUrl& url,
53                                  ImageDimensions dimensions,
54                                  FittingMode::Type fittingMode,
55                                  SamplingMode::Type samplingMode,
56                                  bool orientationCorrection,
57                                  DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
58 {
59   if( !mIsLoadThreadStarted )
60   {
61     mLoadThread.Start();
62     mIsLoadThreadStarted = true;
63   }
64   mLoadThread.AddTask( new LoadingTask( ++mLoadTaskId, url, dimensions, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad ) );
65
66   return mLoadTaskId;
67 }
68
69 uint32_t AsyncImageLoader::ApplyMask( Devel::PixelBuffer pixelBuffer,
70                                       Devel::PixelBuffer maskPixelBuffer,
71                                       float contentScale,
72                                       bool cropToMask,
73                                       DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
74 {
75   if( !mIsLoadThreadStarted )
76   {
77     mLoadThread.Start();
78     mIsLoadThreadStarted = true;
79   }
80   mLoadThread.AddTask( new LoadingTask( ++mLoadTaskId, pixelBuffer, maskPixelBuffer, contentScale, cropToMask, preMultiplyOnLoad ) );
81
82   return mLoadTaskId;
83 }
84
85 Toolkit::AsyncImageLoader::ImageLoadedSignalType& AsyncImageLoader::ImageLoadedSignal()
86 {
87   return mLoadedSignal;
88 }
89
90 Toolkit::DevelAsyncImageLoader::PixelBufferLoadedSignalType& AsyncImageLoader::PixelBufferLoadedSignal()
91 {
92   return mPixelBufferLoadedSignal;
93 }
94
95 bool AsyncImageLoader::Cancel( uint32_t loadingTaskId )
96 {
97   return mLoadThread.CancelTask( loadingTaskId );
98 }
99
100 void AsyncImageLoader::CancelAll()
101 {
102   mLoadThread.CancelAll();
103 }
104
105 void AsyncImageLoader::ProcessLoadedImage()
106 {
107   while( LoadingTask *next = mLoadThread.NextCompletedTask() )
108   {
109     if( mPixelBufferLoadedSignal.GetConnectionCount() > 0 )
110     {
111       mPixelBufferLoadedSignal.Emit( next->id, next->pixelBuffer );
112     }
113     else if( mLoadedSignal.GetConnectionCount() > 0 )
114     {
115       PixelData pixelData;
116       if( next->pixelBuffer )
117       {
118         pixelData = Devel::PixelBuffer::Convert( next->pixelBuffer );
119       }
120       mLoadedSignal.Emit( next->id, pixelData );
121     }
122
123     delete next;
124   }
125 }
126
127 } // namespace Internal
128
129 } // namespace Toolkit
130
131 } // namespace Dali