2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "loading-task.h"
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>
34 LoadingTask::LoadingTask(uint32_t id, Dali::AnimatedImageLoading animatedImageLoading, uint32_t frameIndex, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad, CallbackBase* callback)
35 : AsyncTask(callback),
39 textureId(TextureManagerType::INVALID_TEXTURE_ID),
41 fittingMode(FittingMode::SCALE_TO_FILL),
42 samplingMode(SamplingMode::BOX_THEN_LINEAR),
43 preMultiplyOnLoad(preMultiplyOnLoad),
46 animatedImageLoading(animatedImageLoading),
47 frameIndex(frameIndex),
48 orientationCorrection(),
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),
61 textureId(TextureManagerType::INVALID_TEXTURE_ID),
62 dimensions(dimensions),
63 fittingMode(fittingMode),
64 samplingMode(samplingMode),
65 preMultiplyOnLoad(preMultiplyOnLoad),
68 animatedImageLoading(animatedImageLoading),
69 frameIndex(frameIndex),
70 orientationCorrection(),
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),
83 textureId(TextureManagerType::INVALID_TEXTURE_ID),
84 dimensions(dimensions),
85 fittingMode(fittingMode),
86 samplingMode(samplingMode),
87 preMultiplyOnLoad(preMultiplyOnLoad),
90 animatedImageLoading(),
92 orientationCorrection(orientationCorrection),
95 loadPlanes(loadPlanes),
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),
103 encodedImageBuffer(encodedImageBuffer),
105 textureId(TextureManagerType::INVALID_TEXTURE_ID),
106 dimensions(dimensions),
107 fittingMode(fittingMode),
108 samplingMode(samplingMode),
109 preMultiplyOnLoad(preMultiplyOnLoad),
112 animatedImageLoading(),
114 orientationCorrection(orientationCorrection),
122 LoadingTask::LoadingTask(uint32_t id, Devel::PixelBuffer pixelBuffer, Devel::PixelBuffer maskPixelBuffer, float contentScale, bool cropToMask, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad, CallbackBase* callback)
123 : AsyncTask(callback),
125 encodedImageBuffer(),
127 textureId(TextureManagerType::INVALID_TEXTURE_ID),
131 preMultiplyOnLoad(preMultiplyOnLoad),
132 maskPixelBuffer(maskPixelBuffer),
133 contentScale(contentScale),
134 animatedImageLoading(),
136 orientationCorrection(),
138 cropToMask(cropToMask),
142 pixelBuffers.push_back(pixelBuffer);
145 LoadingTask::~LoadingTask()
149 void LoadingTask::Process()
164 bool LoadingTask::IsReady()
169 void LoadingTask::Load()
171 Devel::PixelBuffer pixelBuffer;
172 if(animatedImageLoading)
174 pixelBuffer = animatedImageLoading.LoadFrame(frameIndex, dimensions, fittingMode, samplingMode);
176 else if(encodedImageBuffer)
178 pixelBuffer = Dali::LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), dimensions, fittingMode, samplingMode, orientationCorrection);
180 else if(url.IsValid() && url.IsLocalResource())
184 Dali::LoadImagePlanesFromFile(url.GetUrl(), pixelBuffers, dimensions, fittingMode, samplingMode, orientationCorrection);
188 pixelBuffer = Dali::LoadImageFromFile(url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection);
191 else if(url.IsValid())
193 pixelBuffer = Dali::DownloadImageSynchronously(url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection);
198 pixelBuffers.push_back(pixelBuffer);
201 if(pixelBuffers.empty())
203 DALI_LOG_ERROR("LoadingTask::Load: Loading is failed: %s\n", url.GetUrl().c_str());
207 void LoadingTask::ApplyMask()
209 if(!pixelBuffers.empty())
211 pixelBuffers[0].ApplyMask(maskPixelBuffer, contentScale, cropToMask);
215 void LoadingTask::MultiplyAlpha()
217 if(!pixelBuffers.empty() && Pixel::HasAlpha(pixelBuffers[0].GetPixelFormat()))
219 if(preMultiplyOnLoad == DevelAsyncImageLoader::PreMultiplyOnLoad::ON)
221 pixelBuffers[0].MultiplyColorByAlpha();
226 void LoadingTask::SetTextureId(TextureManagerType::TextureId id)
231 } // namespace Internal
233 } // namespace Toolkit