Merge "Change BuildRequires of dali2-toolkit to Requires" into 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::ApplyMask(Devel::PixelBuffer                       pixelBuffer,
80                                      Devel::PixelBuffer                       maskPixelBuffer,
81                                      float                                    contentScale,
82                                      bool                                     cropToMask,
83                                      DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
84 {
85   if(!mIsLoadThreadStarted)
86   {
87     mLoadThread.Start();
88     mIsLoadThreadStarted = true;
89   }
90   mLoadThread.AddTask(new LoadingTask(++mLoadTaskId, pixelBuffer, maskPixelBuffer, contentScale, cropToMask, preMultiplyOnLoad));
91
92   return mLoadTaskId;
93 }
94
95 Toolkit::AsyncImageLoader::ImageLoadedSignalType& AsyncImageLoader::ImageLoadedSignal()
96 {
97   return mLoadedSignal;
98 }
99
100 Toolkit::DevelAsyncImageLoader::PixelBufferLoadedSignalType& AsyncImageLoader::PixelBufferLoadedSignal()
101 {
102   return mPixelBufferLoadedSignal;
103 }
104
105 bool AsyncImageLoader::Cancel(uint32_t loadingTaskId)
106 {
107   return mLoadThread.CancelTask(loadingTaskId);
108 }
109
110 void AsyncImageLoader::CancelAll()
111 {
112   mLoadThread.CancelAll();
113 }
114
115 void AsyncImageLoader::ProcessLoadedImage()
116 {
117   while(LoadingTask* next = mLoadThread.NextCompletedTask())
118   {
119     if(mPixelBufferLoadedSignal.GetConnectionCount() > 0)
120     {
121       mPixelBufferLoadedSignal.Emit(next->id, next->pixelBuffer);
122     }
123     else if(mLoadedSignal.GetConnectionCount() > 0)
124     {
125       PixelData pixelData;
126       if(next->pixelBuffer)
127       {
128         pixelData = Devel::PixelBuffer::Convert(next->pixelBuffer);
129       }
130       mLoadedSignal.Emit(next->id, pixelData);
131     }
132
133     delete next;
134   }
135 }
136
137 } // namespace Internal
138
139 } // namespace Toolkit
140
141 } // namespace Dali