DALi Version 2.2.11
[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(FittingMode::SCALE_TO_FILL),
42   samplingMode(SamplingMode::BOX_THEN_LINEAR),
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, Dali::AnimatedImageLoading animatedImageLoading, uint32_t frameIndex, ImageDimensions dimensions, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad, CallbackBase* callback)
57 : AsyncTask(callback),
58   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(animatedImageLoading),
69   frameIndex(frameIndex),
70   orientationCorrection(),
71   isMaskTask(false),
72   cropToMask(false),
73   loadPlanes(false),
74   isReady(true)
75 {
76 }
77
78 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)
79 : AsyncTask(callback),
80   url(url),
81   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(loadPlanes),
96   isReady(true)
97 {
98 }
99
100 LoadingTask::LoadingTask(uint32_t id, const EncodedImageBuffer& encodedImageBuffer, ImageDimensions dimensions, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad, CallbackBase* callback)
101 : AsyncTask(callback),
102   url(),
103   encodedImageBuffer(encodedImageBuffer),
104   id(id),
105   textureId(TextureManagerType::INVALID_TEXTURE_ID),
106   dimensions(dimensions),
107   fittingMode(fittingMode),
108   samplingMode(samplingMode),
109   preMultiplyOnLoad(preMultiplyOnLoad),
110   maskPixelBuffer(),
111   contentScale(1.0f),
112   animatedImageLoading(),
113   frameIndex(0u),
114   orientationCorrection(orientationCorrection),
115   isMaskTask(false),
116   cropToMask(false),
117   loadPlanes(false),
118   isReady(true)
119 {
120 }
121
122 LoadingTask::LoadingTask(uint32_t id, Devel::PixelBuffer pixelBuffer, Devel::PixelBuffer maskPixelBuffer, float contentScale, bool cropToMask, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad, CallbackBase* callback)
123 : AsyncTask(callback),
124   url(""),
125   encodedImageBuffer(),
126   id(id),
127   textureId(TextureManagerType::INVALID_TEXTURE_ID),
128   dimensions(),
129   fittingMode(),
130   samplingMode(),
131   preMultiplyOnLoad(preMultiplyOnLoad),
132   maskPixelBuffer(maskPixelBuffer),
133   contentScale(contentScale),
134   animatedImageLoading(),
135   frameIndex(0u),
136   orientationCorrection(),
137   isMaskTask(true),
138   cropToMask(cropToMask),
139   loadPlanes(false),
140   isReady(true)
141 {
142   pixelBuffers.push_back(pixelBuffer);
143 }
144
145 LoadingTask::~LoadingTask()
146 {
147 }
148
149 void LoadingTask::Process()
150 {
151   isReady = false;
152   if(!isMaskTask)
153   {
154     Load();
155   }
156   else
157   {
158     ApplyMask();
159   }
160   MultiplyAlpha();
161   isReady = true;
162 }
163
164 bool LoadingTask::IsReady()
165 {
166   return isReady;
167 }
168
169 void LoadingTask::Load()
170 {
171   Devel::PixelBuffer pixelBuffer;
172   if(animatedImageLoading)
173   {
174     pixelBuffer = animatedImageLoading.LoadFrame(frameIndex, dimensions, fittingMode, samplingMode);
175   }
176   else if(encodedImageBuffer)
177   {
178     pixelBuffer = Dali::LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), dimensions, fittingMode, samplingMode, orientationCorrection);
179   }
180   else if(url.IsValid() && url.IsLocalResource())
181   {
182     if(loadPlanes)
183     {
184       Dali::LoadImagePlanesFromFile(url.GetUrl(), pixelBuffers, dimensions, fittingMode, samplingMode, orientationCorrection);
185     }
186     else
187     {
188       pixelBuffer = Dali::LoadImageFromFile(url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection);
189     }
190   }
191   else if(url.IsValid())
192   {
193     pixelBuffer = Dali::DownloadImageSynchronously(url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection);
194   }
195
196   if(pixelBuffer)
197   {
198     pixelBuffers.push_back(pixelBuffer);
199   }
200
201   if(pixelBuffers.empty())
202   {
203     DALI_LOG_ERROR("LoadingTask::Load: Loading is failed: %s\n", url.GetUrl().c_str());
204   }
205 }
206
207 void LoadingTask::ApplyMask()
208 {
209   if(!pixelBuffers.empty())
210   {
211     pixelBuffers[0].ApplyMask(maskPixelBuffer, contentScale, cropToMask);
212   }
213 }
214
215 void LoadingTask::MultiplyAlpha()
216 {
217   if(!pixelBuffers.empty() && Pixel::HasAlpha(pixelBuffers[0].GetPixelFormat()))
218   {
219     if(preMultiplyOnLoad == DevelAsyncImageLoader::PreMultiplyOnLoad::ON)
220     {
221       pixelBuffers[0].MultiplyColorByAlpha();
222     }
223   }
224 }
225
226 void LoadingTask::SetTextureId(TextureManagerType::TextureId id)
227 {
228   textureId = id;
229 }
230
231 } // namespace Internal
232
233 } // namespace Toolkit
234
235 } // namespace Dali