Support YUV decoding for JPEG
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / image-loader / async-image-loader-impl.cpp
1 /*
2  * Copyright (c) 2022 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                                 bool                                     loadPlanes)
70 {
71   if(!mIsLoadThreadStarted)
72   {
73     mLoadThread.Start();
74     mIsLoadThreadStarted = true;
75   }
76   mLoadThread.AddTask(new LoadingTask(++mLoadTaskId, url, dimensions, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad, loadPlanes));
77
78   return mLoadTaskId;
79 }
80
81 uint32_t AsyncImageLoader::LoadEncodedImageBuffer(const EncodedImageBuffer&                encodedImageBuffer,
82                                                   ImageDimensions                          dimensions,
83                                                   FittingMode::Type                        fittingMode,
84                                                   SamplingMode::Type                       samplingMode,
85                                                   bool                                     orientationCorrection,
86                                                   DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
87 {
88   if(!mIsLoadThreadStarted)
89   {
90     mLoadThread.Start();
91     mIsLoadThreadStarted = true;
92   }
93   mLoadThread.AddTask(new LoadingTask(++mLoadTaskId, encodedImageBuffer, dimensions, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad));
94
95   return mLoadTaskId;
96 }
97
98 uint32_t AsyncImageLoader::ApplyMask(Devel::PixelBuffer                       pixelBuffer,
99                                      Devel::PixelBuffer                       maskPixelBuffer,
100                                      float                                    contentScale,
101                                      bool                                     cropToMask,
102                                      DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
103 {
104   if(!mIsLoadThreadStarted)
105   {
106     mLoadThread.Start();
107     mIsLoadThreadStarted = true;
108   }
109   mLoadThread.AddTask(new LoadingTask(++mLoadTaskId, pixelBuffer, maskPixelBuffer, contentScale, cropToMask, preMultiplyOnLoad));
110
111   return mLoadTaskId;
112 }
113
114 Toolkit::AsyncImageLoader::ImageLoadedSignalType& AsyncImageLoader::ImageLoadedSignal()
115 {
116   return mLoadedSignal;
117 }
118
119 Toolkit::DevelAsyncImageLoader::PixelBufferLoadedSignalType& AsyncImageLoader::PixelBufferLoadedSignal()
120 {
121   return mPixelBufferLoadedSignal;
122 }
123
124 bool AsyncImageLoader::Cancel(uint32_t loadingTaskId)
125 {
126   return mLoadThread.CancelTask(loadingTaskId);
127 }
128
129 void AsyncImageLoader::CancelAll()
130 {
131   mLoadThread.CancelAll();
132 }
133
134 void AsyncImageLoader::ProcessLoadedImage()
135 {
136   while(LoadingTask* next = mLoadThread.NextCompletedTask())
137   {
138     if(mPixelBufferLoadedSignal.GetConnectionCount() > 0)
139     {
140       mPixelBufferLoadedSignal.Emit(next->id, next->pixelBuffers);
141     }
142     else if(mLoadedSignal.GetConnectionCount() > 0)
143     {
144       PixelData pixelData;
145       if(!next->pixelBuffers.empty())
146       {
147         pixelData = Devel::PixelBuffer::Convert(next->pixelBuffers[0]);
148       }
149       mLoadedSignal.Emit(next->id, pixelData);
150     }
151
152     delete next;
153   }
154 }
155
156 } // namespace Internal
157
158 } // namespace Toolkit
159
160 } // namespace Dali