Add async task manager
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / image-loader / loading-task.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 "loading-task.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/adaptor-framework/image-loading.h>
23 #include <dali/devel-api/adaptor-framework/thread-settings.h>
24 #include <dali/integration-api/adaptor-framework/adaptor.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/public-api/adaptor-framework/encoded-image-buffer.h>
27
28 namespace Dali
29 {
30 namespace Toolkit
31 {
32 namespace Internal
33 {
34 LoadingTask::LoadingTask(uint32_t id, Dali::AnimatedImageLoading animatedImageLoading, uint32_t frameIndex, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad, CallbackBase* callback)
35 : AsyncTask(callback),
36   url(),
37   encodedImageBuffer(),
38   id(id),
39   textureId(TextureManagerType::INVALID_TEXTURE_ID),
40   dimensions(),
41   fittingMode(),
42   samplingMode(),
43   preMultiplyOnLoad(preMultiplyOnLoad),
44   maskPixelBuffer(),
45   contentScale(1.0f),
46   animatedImageLoading(animatedImageLoading),
47   frameIndex(frameIndex),
48   orientationCorrection(),
49   isMaskTask(false),
50   cropToMask(false),
51   loadPlanes(false),
52   isReady(true)
53 {
54 }
55
56 LoadingTask::LoadingTask(uint32_t id, const VisualUrl& url, ImageDimensions dimensions, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad, bool loadPlanes, CallbackBase* callback)
57 : AsyncTask(callback),
58   url(url),
59   encodedImageBuffer(),
60   id(id),
61   textureId(TextureManagerType::INVALID_TEXTURE_ID),
62   dimensions(dimensions),
63   fittingMode(fittingMode),
64   samplingMode(samplingMode),
65   preMultiplyOnLoad(preMultiplyOnLoad),
66   maskPixelBuffer(),
67   contentScale(1.0f),
68   animatedImageLoading(),
69   frameIndex(0u),
70   orientationCorrection(orientationCorrection),
71   isMaskTask(false),
72   cropToMask(false),
73   loadPlanes(loadPlanes),
74   isReady(true)
75 {
76 }
77
78 LoadingTask::LoadingTask(uint32_t id, const EncodedImageBuffer& encodedImageBuffer, ImageDimensions dimensions, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad, CallbackBase* callback)
79 : AsyncTask(callback),
80   url(),
81   encodedImageBuffer(encodedImageBuffer),
82   id(id),
83   textureId(TextureManagerType::INVALID_TEXTURE_ID),
84   dimensions(dimensions),
85   fittingMode(fittingMode),
86   samplingMode(samplingMode),
87   preMultiplyOnLoad(preMultiplyOnLoad),
88   maskPixelBuffer(),
89   contentScale(1.0f),
90   animatedImageLoading(),
91   frameIndex(0u),
92   orientationCorrection(orientationCorrection),
93   isMaskTask(false),
94   cropToMask(false),
95   loadPlanes(false),
96   isReady(true)
97 {
98 }
99
100 LoadingTask::LoadingTask(uint32_t id, Devel::PixelBuffer pixelBuffer, Devel::PixelBuffer maskPixelBuffer, float contentScale, bool cropToMask, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad, CallbackBase* callback)
101 : AsyncTask(callback),
102   url(""),
103   encodedImageBuffer(),
104   id(id),
105   textureId(TextureManagerType::INVALID_TEXTURE_ID),
106   dimensions(),
107   fittingMode(),
108   samplingMode(),
109   preMultiplyOnLoad(preMultiplyOnLoad),
110   maskPixelBuffer(maskPixelBuffer),
111   contentScale(contentScale),
112   animatedImageLoading(),
113   frameIndex(0u),
114   orientationCorrection(),
115   isMaskTask(true),
116   cropToMask(cropToMask),
117   loadPlanes(false),
118   isReady(true)
119 {
120   pixelBuffers.push_back(pixelBuffer);
121 }
122
123 LoadingTask::~LoadingTask()
124 {
125 }
126
127 void LoadingTask::Process()
128 {
129   isReady = false;
130   if(!isMaskTask)
131   {
132     Load();
133   }
134   else
135   {
136     ApplyMask();
137   }
138   MultiplyAlpha();
139   isReady = true;
140 }
141
142 bool LoadingTask::IsReady()
143 {
144   return isReady;
145 }
146
147 void LoadingTask::Load()
148 {
149   Devel::PixelBuffer pixelBuffer;
150   if(animatedImageLoading)
151   {
152     pixelBuffer = animatedImageLoading.LoadFrame(frameIndex);
153   }
154   else if(encodedImageBuffer)
155   {
156     pixelBuffer = Dali::LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), dimensions, fittingMode, samplingMode, orientationCorrection);
157   }
158   else if(url.IsValid() && url.IsLocalResource())
159   {
160     if(loadPlanes)
161     {
162       Dali::LoadImagePlanesFromFile(url.GetUrl(), pixelBuffers, dimensions, fittingMode, samplingMode, orientationCorrection);
163     }
164     else
165     {
166       pixelBuffer = Dali::LoadImageFromFile(url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection);
167     }
168   }
169   else if(url.IsValid())
170   {
171     pixelBuffer = Dali::DownloadImageSynchronously(url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection);
172   }
173
174   if(pixelBuffer)
175   {
176     pixelBuffers.push_back(pixelBuffer);
177   }
178
179   if(pixelBuffers.empty())
180   {
181     DALI_LOG_ERROR("LoadingTask::Load: Loading is failed: %s\n", url.GetUrl().c_str());
182   }
183 }
184
185 void LoadingTask::ApplyMask()
186 {
187   if(!pixelBuffers.empty())
188   {
189     pixelBuffers[0].ApplyMask(maskPixelBuffer, contentScale, cropToMask);
190   }
191 }
192
193 void LoadingTask::MultiplyAlpha()
194 {
195   if(!pixelBuffers.empty() && Pixel::HasAlpha(pixelBuffers[0].GetPixelFormat()))
196   {
197     if(preMultiplyOnLoad == DevelAsyncImageLoader::PreMultiplyOnLoad::ON)
198     {
199       pixelBuffers[0].MultiplyColorByAlpha();
200     }
201   }
202 }
203
204 void LoadingTask::SetTextureId(TextureManagerType::TextureId id)
205 {
206   textureId = id;
207 }
208
209 } // namespace Internal
210
211 } // namespace Toolkit
212
213 } // namespace Dali