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