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/fast-track-loading-task.h>
22 #include <dali/devel-api/adaptor-framework/image-loading.h>
23 #include <dali/devel-api/adaptor-framework/texture-upload-manager.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/integration-api/texture-integ.h>
33 FastTrackLoadingTask::FastTrackLoadingTask(const VisualUrl& url, ImageDimensions dimensions, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad, CallbackBase* callback)
34 : AsyncTask(MakeCallback(this, &FastTrackLoadingTask::OnComplete), url.GetProtocolType() == VisualUrl::ProtocolType::REMOTE ? AsyncTask::PriorityType::LOW : AsyncTask::PriorityType::HIGH),
37 mDimensions(dimensions),
38 mFittingMode(fittingMode),
39 mSamplingMode(samplingMode),
40 mPreMultiplyOnLoad(preMultiplyOnLoad),
42 mTextureUploadManager(Dali::Devel::TextureUploadManager::Get()),
45 mImageFormat(Pixel::INVALID),
48 mOrientationCorrection(orientationCorrection),
53 mCallback = std::unique_ptr<CallbackBase>(callback);
57 FastTrackLoadingTask::~FastTrackLoadingTask()
61 void FastTrackLoadingTask::PrepareTexture()
63 mTexture = mTextureUploadManager.GenerateTexture2D();
64 mResourceId = Integration::GetTextureResourceId(mTexture);
67 void FastTrackLoadingTask::OnComplete(AsyncTaskPtr task)
71 Dali::Integration::SetTextureSize(mTexture, Dali::ImageDimensions(mImageWidth, mImageHeight));
72 Dali::Integration::SetTexturePixelFormat(mTexture, mImageFormat);
76 CallbackBase::Execute(*mCallback, FastTrackLoadingTaskPtr(reinterpret_cast<FastTrackLoadingTask*>(task.Get())));
80 // Called by worker thread
82 void FastTrackLoadingTask::Process()
88 bool FastTrackLoadingTask::IsReady()
93 void FastTrackLoadingTask::Load()
95 Devel::PixelBuffer pixelBuffer;
96 std::vector<Devel::PixelBuffer> pixelBuffers;
98 if(mUrl.IsValid() && mUrl.IsLocalResource())
100 // TODO : We need to consider YUV case in future.
101 //Dali::LoadImagePlanesFromFile(mUrl.GetUrl(), pixelBuffers, mDimensions, mFittingMode, mSamplingMode, mOrientationCorrection);
103 pixelBuffer = Dali::LoadImageFromFile(mUrl.GetUrl(), mDimensions, mFittingMode, mSamplingMode, mOrientationCorrection);
105 else if(mUrl.IsValid())
107 pixelBuffer = Dali::DownloadImageSynchronously(mUrl.GetUrl(), mDimensions, mFittingMode, mSamplingMode, mOrientationCorrection);
112 pixelBuffers.push_back(pixelBuffer);
115 if(pixelBuffers.empty())
117 DALI_LOG_ERROR("FastTrackLoadingTask::Load: Loading is failed: ResourceId : %d, url : [%s]\n", mResourceId, mUrl.GetUrl().c_str());
121 if(pixelBuffers.size() == 1u)
124 MultiplyAlpha(pixelBuffers[0]);
125 mPixelData = Dali::Devel::PixelBuffer::Convert(pixelBuffers[0]);
129 DALI_LOG_ERROR("FastTrackLoadingTask::Load: ??? Undefined case. PixelBuffers.size() : %zu : ResourceId : %d, url : [%s]\n", pixelBuffers.size(), mResourceId, mUrl.GetUrl().c_str());
134 void FastTrackLoadingTask::MultiplyAlpha(Dali::Devel::PixelBuffer pixelBuffer)
136 if(mPreMultiplyOnLoad == DevelAsyncImageLoader::PreMultiplyOnLoad::ON && Pixel::HasAlpha(pixelBuffer.GetPixelFormat()))
138 pixelBuffer.MultiplyColorByAlpha();
139 mPremultiplied = pixelBuffer.IsAlphaPreMultiplied();
143 void FastTrackLoadingTask::UploadToTexture()
147 mImageWidth = mPixelData.GetWidth();
148 mImageHeight = mPixelData.GetHeight();
149 mImageFormat = mPixelData.GetPixelFormat();
151 mTextureUploadManager.RequestUpload(mResourceId, mPixelData);
157 } // namespace Internal
159 } // namespace Toolkit