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),
52 mCallback = std::unique_ptr<CallbackBase>(callback);
56 FastTrackLoadingTask::~FastTrackLoadingTask()
60 void FastTrackLoadingTask::PrepareTexture()
62 mTexture = mTextureUploadManager.GenerateTexture2D();
63 mResourceId = Integration::GetTextureResourceId(mTexture);
66 void FastTrackLoadingTask::OnComplete(AsyncTaskPtr task)
70 Dali::Integration::SetTextureSize(mTexture, Dali::ImageDimensions(mImageWidth, mImageHeight));
71 Dali::Integration::SetTexturePixelFormat(mTexture, mImageFormat);
75 CallbackBase::Execute(*mCallback, FastTrackLoadingTaskPtr(reinterpret_cast<FastTrackLoadingTask*>(task.Get())));
79 // Called by worker thread
81 void FastTrackLoadingTask::Process()
87 bool FastTrackLoadingTask::IsReady()
92 void FastTrackLoadingTask::Load()
94 Devel::PixelBuffer pixelBuffer;
95 std::vector<Devel::PixelBuffer> pixelBuffers;
97 if(mUrl.IsValid() && mUrl.IsLocalResource())
99 // TODO : We need to consider YUV case in future.
100 //Dali::LoadImagePlanesFromFile(mUrl.GetUrl(), pixelBuffers, mDimensions, mFittingMode, mSamplingMode, mOrientationCorrection);
102 pixelBuffer = Dali::LoadImageFromFile(mUrl.GetUrl(), mDimensions, mFittingMode, mSamplingMode, mOrientationCorrection);
104 else if(mUrl.IsValid())
106 pixelBuffer = Dali::DownloadImageSynchronously(mUrl.GetUrl(), mDimensions, mFittingMode, mSamplingMode, mOrientationCorrection);
111 pixelBuffers.push_back(pixelBuffer);
114 if(pixelBuffers.empty())
116 DALI_LOG_ERROR("FastTrackLoadingTask::Load: Loading is failed: ResourceId : %d, url : [%s]\n", mResourceId, mUrl.GetUrl().c_str());
120 if(pixelBuffers.size() == 1u)
123 MultiplyAlpha(pixelBuffers[0]);
124 mPixelData = Dali::Devel::PixelBuffer::Convert(pixelBuffers[0]);
128 DALI_LOG_ERROR("FastTrackLoadingTask::Load: ??? Undefined case. PixelBuffers.size() : %zu : ResourceId : %d, url : [%s]\n", pixelBuffers.size(), mResourceId, mUrl.GetUrl().c_str());
133 void FastTrackLoadingTask::MultiplyAlpha(Dali::Devel::PixelBuffer pixelBuffer)
135 if(mPreMultiplyOnLoad == DevelAsyncImageLoader::PreMultiplyOnLoad::ON && Pixel::HasAlpha(pixelBuffer.GetPixelFormat()))
137 pixelBuffer.MultiplyColorByAlpha();
138 mPremultiplied = pixelBuffer.IsAlphaPreMultiplied();
142 void FastTrackLoadingTask::UploadToTexture()
146 mImageWidth = mPixelData.GetWidth();
147 mImageHeight = mPixelData.GetHeight();
148 mImageFormat = mPixelData.GetPixelFormat();
150 mTextureUploadManager.RequestUpload(mResourceId, mPixelData);
156 } // namespace Internal
158 } // namespace Toolkit