Merge "DALi Version 1.9.30" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / image-loader / async-image-loader-impl.cpp
1 /*
2  * Copyright (c) 2020 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::LoadAnimatedImage( Dali::AnimatedImageLoading animatedImageLoading,
53                                               uint32_t frameIndex )
54 {
55   if( !mIsLoadThreadStarted )
56   {
57     mLoadThread.Start();
58     mIsLoadThreadStarted = true;
59   }
60   mLoadThread.AddTask( new LoadingTask( ++mLoadTaskId, animatedImageLoading, frameIndex ) );
61
62   return mLoadTaskId;
63 }
64
65 uint32_t AsyncImageLoader::Load( const VisualUrl& url,
66                                  ImageDimensions dimensions,
67                                  FittingMode::Type fittingMode,
68                                  SamplingMode::Type samplingMode,
69                                  bool orientationCorrection,
70                                  DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
71 {
72   if( !mIsLoadThreadStarted )
73   {
74     mLoadThread.Start();
75     mIsLoadThreadStarted = true;
76   }
77   mLoadThread.AddTask( new LoadingTask( ++mLoadTaskId, url, dimensions, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad ) );
78
79   return mLoadTaskId;
80 }
81
82 uint32_t AsyncImageLoader::ApplyMask( Devel::PixelBuffer pixelBuffer,
83                                       Devel::PixelBuffer maskPixelBuffer,
84                                       float contentScale,
85                                       bool cropToMask,
86                                       DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
87 {
88   if( !mIsLoadThreadStarted )
89   {
90     mLoadThread.Start();
91     mIsLoadThreadStarted = true;
92   }
93   mLoadThread.AddTask( new LoadingTask( ++mLoadTaskId, pixelBuffer, maskPixelBuffer, contentScale, cropToMask, preMultiplyOnLoad ) );
94
95   return mLoadTaskId;
96 }
97
98 Toolkit::AsyncImageLoader::ImageLoadedSignalType& AsyncImageLoader::ImageLoadedSignal()
99 {
100   return mLoadedSignal;
101 }
102
103 Toolkit::DevelAsyncImageLoader::PixelBufferLoadedSignalType& AsyncImageLoader::PixelBufferLoadedSignal()
104 {
105   return mPixelBufferLoadedSignal;
106 }
107
108 bool AsyncImageLoader::Cancel( uint32_t loadingTaskId )
109 {
110   return mLoadThread.CancelTask( loadingTaskId );
111 }
112
113 void AsyncImageLoader::CancelAll()
114 {
115   mLoadThread.CancelAll();
116 }
117
118 void AsyncImageLoader::ProcessLoadedImage()
119 {
120   while( LoadingTask *next = mLoadThread.NextCompletedTask() )
121   {
122     if( mPixelBufferLoadedSignal.GetConnectionCount() > 0 )
123     {
124       mPixelBufferLoadedSignal.Emit( next->id, next->pixelBuffer );
125     }
126     else if( mLoadedSignal.GetConnectionCount() > 0 )
127     {
128       PixelData pixelData;
129       if( next->pixelBuffer )
130       {
131         pixelData = Devel::PixelBuffer::Convert( next->pixelBuffer );
132       }
133       mLoadedSignal.Emit( next->id, pixelData );
134     }
135
136     delete next;
137   }
138 }
139
140 } // namespace Internal
141
142 } // namespace Toolkit
143
144 } // namespace Dali