2 * Copyright (c) 2023 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 <dali-toolkit/internal/image-loader/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/integration-api/trace.h>
27 #include <dali/public-api/adaptor-framework/encoded-image-buffer.h>
41 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_IMAGE_PERFORMANCE_MARKER, false);
44 LoadingTask::LoadingTask(uint32_t id, Dali::AnimatedImageLoading animatedImageLoading, uint32_t frameIndex, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad, CallbackBase* callback)
45 : AsyncTask(callback),
49 textureId(TextureManagerType::INVALID_TEXTURE_ID),
51 fittingMode(FittingMode::SCALE_TO_FILL),
52 samplingMode(SamplingMode::BOX_THEN_LINEAR),
53 preMultiplyOnLoad(preMultiplyOnLoad),
56 animatedImageLoading(animatedImageLoading),
57 frameIndex(frameIndex),
58 orientationCorrection(),
66 LoadingTask::LoadingTask(uint32_t id, Dali::AnimatedImageLoading animatedImageLoading, uint32_t frameIndex, ImageDimensions dimensions, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad, CallbackBase* callback)
67 : AsyncTask(callback),
71 textureId(TextureManagerType::INVALID_TEXTURE_ID),
72 dimensions(dimensions),
73 fittingMode(fittingMode),
74 samplingMode(samplingMode),
75 preMultiplyOnLoad(preMultiplyOnLoad),
78 animatedImageLoading(animatedImageLoading),
79 frameIndex(frameIndex),
80 orientationCorrection(),
88 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)
89 : AsyncTask(callback, url.GetProtocolType() == VisualUrl::ProtocolType::REMOTE ? AsyncTask::PriorityType::LOW : AsyncTask::PriorityType::HIGH),
93 textureId(TextureManagerType::INVALID_TEXTURE_ID),
94 dimensions(dimensions),
95 fittingMode(fittingMode),
96 samplingMode(samplingMode),
97 preMultiplyOnLoad(preMultiplyOnLoad),
100 animatedImageLoading(),
102 orientationCorrection(orientationCorrection),
105 loadPlanes(loadPlanes),
110 LoadingTask::LoadingTask(uint32_t id, const EncodedImageBuffer& encodedImageBuffer, ImageDimensions dimensions, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad, CallbackBase* callback)
111 : AsyncTask(callback),
113 encodedImageBuffer(encodedImageBuffer),
115 textureId(TextureManagerType::INVALID_TEXTURE_ID),
116 dimensions(dimensions),
117 fittingMode(fittingMode),
118 samplingMode(samplingMode),
119 preMultiplyOnLoad(preMultiplyOnLoad),
122 animatedImageLoading(),
124 orientationCorrection(orientationCorrection),
132 LoadingTask::LoadingTask(uint32_t id, Devel::PixelBuffer pixelBuffer, Devel::PixelBuffer maskPixelBuffer, float contentScale, bool cropToMask, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad, CallbackBase* callback)
133 : AsyncTask(callback),
135 encodedImageBuffer(),
137 textureId(TextureManagerType::INVALID_TEXTURE_ID),
141 preMultiplyOnLoad(preMultiplyOnLoad),
142 maskPixelBuffer(maskPixelBuffer),
143 contentScale(contentScale),
144 animatedImageLoading(),
146 orientationCorrection(),
148 cropToMask(cropToMask),
152 pixelBuffers.push_back(pixelBuffer);
155 LoadingTask::~LoadingTask()
159 void LoadingTask::Process()
162 if(gTraceFilter && gTraceFilter->IsTraceEnabled())
164 std::ostringstream oss;
165 oss << "[url:" << (!!(animatedImageLoading) ? animatedImageLoading.GetUrl() : url.GetUrl()) << "]";
166 DALI_TRACE_BEGIN(gTraceFilter, "DALI_IMAGE_LOADING_TASK");
167 DALI_LOG_RELEASE_INFO("BEGIN: DALI_IMAGE_LOADING_TASK %s", oss.str().c_str());
184 if(gTraceFilter && gTraceFilter->IsTraceEnabled())
186 std::ostringstream oss;
188 oss << "masking:" << isMaskTask << " ";
189 oss << "index:" << frameIndex << " ";
190 oss << "pixelBuffers:" << pixelBuffers.size() << " ";
191 if(!pixelBuffers.empty())
193 oss << "size:" << pixelBuffers[0].GetWidth() << "x" << pixelBuffers[0].GetHeight() << " ";
194 oss << "premult:" << pixelBuffers[0].IsAlphaPreMultiplied() << " ";
196 oss << "url:" << (!!(animatedImageLoading) ? animatedImageLoading.GetUrl() : url.GetUrl()) << "]";
197 DALI_TRACE_END(gTraceFilter, "DALI_IMAGE_LOADING_TASK");
198 DALI_LOG_RELEASE_INFO("END: DALI_IMAGE_LOADING_TASK %s", oss.str().c_str());
203 bool LoadingTask::IsReady()
208 void LoadingTask::Load()
210 Devel::PixelBuffer pixelBuffer;
211 if(animatedImageLoading)
213 pixelBuffer = animatedImageLoading.LoadFrame(frameIndex, dimensions, fittingMode, samplingMode);
215 else if(encodedImageBuffer)
217 pixelBuffer = Dali::LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), dimensions, fittingMode, samplingMode, orientationCorrection);
219 // We don't need to hold image buffer anymore.
220 encodedImageBuffer.Reset();
222 else if(url.IsValid() && url.IsLocalResource())
226 Dali::LoadImagePlanesFromFile(url.GetUrl(), pixelBuffers, dimensions, fittingMode, samplingMode, orientationCorrection);
230 pixelBuffer = Dali::LoadImageFromFile(url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection);
233 else if(url.IsValid())
235 pixelBuffer = Dali::DownloadImageSynchronously(url.GetUrl(), dimensions, fittingMode, samplingMode, orientationCorrection);
240 pixelBuffers.push_back(pixelBuffer);
243 if(pixelBuffers.empty())
245 DALI_LOG_ERROR("LoadingTask::Load: Loading is failed: %s\n", url.GetUrl().c_str());
249 void LoadingTask::ApplyMask()
251 if(!pixelBuffers.empty())
253 pixelBuffers[0].ApplyMask(maskPixelBuffer, contentScale, cropToMask);
257 void LoadingTask::MultiplyAlpha()
259 if(!pixelBuffers.empty() && Pixel::HasAlpha(pixelBuffers[0].GetPixelFormat()))
261 if(preMultiplyOnLoad == DevelAsyncImageLoader::PreMultiplyOnLoad::ON)
263 pixelBuffers[0].MultiplyColorByAlpha();
268 void LoadingTask::SetTextureId(TextureManagerType::TextureId id)
273 } // namespace Internal
275 } // namespace Toolkit