Apply premultiply in animated image visual
[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                                              DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
52 {
53   if(!mIsLoadThreadStarted)
54   {
55     mLoadThread.Start();
56     mIsLoadThreadStarted = true;
57   }
58   mLoadThread.AddTask(new LoadingTask(++mLoadTaskId, animatedImageLoading, frameIndex, preMultiplyOnLoad));
59
60   return mLoadTaskId;
61 }
62
63 uint32_t AsyncImageLoader::Load(const VisualUrl&                         url,
64                                 ImageDimensions                          dimensions,
65                                 FittingMode::Type                        fittingMode,
66                                 SamplingMode::Type                       samplingMode,
67                                 bool                                     orientationCorrection,
68                                 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
69 {
70   if(!mIsLoadThreadStarted)
71   {
72     mLoadThread.Start();
73     mIsLoadThreadStarted = true;
74   }
75   mLoadThread.AddTask(new LoadingTask(++mLoadTaskId, url, dimensions, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad));
76
77   return mLoadTaskId;
78 }
79
80 uint32_t AsyncImageLoader::LoadEncodedImageBuffer(const EncodedImageBuffer&                encodedImageBuffer,
81                                                   ImageDimensions                          dimensions,
82                                                   FittingMode::Type                        fittingMode,
83                                                   SamplingMode::Type                       samplingMode,
84                                                   bool                                     orientationCorrection,
85                                                   DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
86 {
87   if(!mIsLoadThreadStarted)
88   {
89     mLoadThread.Start();
90     mIsLoadThreadStarted = true;
91   }
92   mLoadThread.AddTask(new LoadingTask(++mLoadTaskId, encodedImageBuffer, dimensions, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad));
93
94   return mLoadTaskId;
95 }
96
97 uint32_t AsyncImageLoader::ApplyMask(Devel::PixelBuffer                       pixelBuffer,
98                                      Devel::PixelBuffer                       maskPixelBuffer,
99                                      float                                    contentScale,
100                                      bool                                     cropToMask,
101                                      DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
102 {
103   if(!mIsLoadThreadStarted)
104   {
105     mLoadThread.Start();
106     mIsLoadThreadStarted = true;
107   }
108   mLoadThread.AddTask(new LoadingTask(++mLoadTaskId, pixelBuffer, maskPixelBuffer, contentScale, cropToMask, preMultiplyOnLoad));
109
110   return mLoadTaskId;
111 }
112
113 Toolkit::AsyncImageLoader::ImageLoadedSignalType& AsyncImageLoader::ImageLoadedSignal()
114 {
115   return mLoadedSignal;
116 }
117
118 Toolkit::DevelAsyncImageLoader::PixelBufferLoadedSignalType& AsyncImageLoader::PixelBufferLoadedSignal()
119 {
120   return mPixelBufferLoadedSignal;
121 }
122
123 bool AsyncImageLoader::Cancel(uint32_t loadingTaskId)
124 {
125   return mLoadThread.CancelTask(loadingTaskId);
126 }
127
128 void AsyncImageLoader::CancelAll()
129 {
130   mLoadThread.CancelAll();
131 }
132
133 void AsyncImageLoader::ProcessLoadedImage()
134 {
135   while(LoadingTask* next = mLoadThread.NextCompletedTask())
136   {
137     if(mPixelBufferLoadedSignal.GetConnectionCount() > 0)
138     {
139       mPixelBufferLoadedSignal.Emit(next->id, next->pixelBuffer);
140     }
141     else if(mLoadedSignal.GetConnectionCount() > 0)
142     {
143       PixelData pixelData;
144       if(next->pixelBuffer)
145       {
146         pixelData = Devel::PixelBuffer::Convert(next->pixelBuffer);
147       }
148       mLoadedSignal.Emit(next->id, pixelData);
149     }
150
151     delete next;
152   }
153 }
154
155 } // namespace Internal
156
157 } // namespace Toolkit
158
159 } // namespace Dali