Merge "DALi Version 2.2.38" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / image-loader / fast-track-loading-task.cpp
1 /*
2  * Copyright (c) 2023 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 <dali-toolkit/internal/image-loader/fast-track-loading-task.h>
20
21 // EXTERNAL INCLUDES
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>
26
27 namespace Dali
28 {
29 namespace Toolkit
30 {
31 namespace Internal
32 {
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),
35   mUrl(url),
36   mTexture(),
37   mDimensions(dimensions),
38   mFittingMode(fittingMode),
39   mSamplingMode(samplingMode),
40   mPreMultiplyOnLoad(preMultiplyOnLoad),
41   mCallback(),
42   mTextureUploadManager(Dali::Devel::TextureUploadManager::Get()),
43   mImageWidth(0u),
44   mImageHeight(0u),
45   mImageFormat(Pixel::INVALID),
46   mPixelData(),
47   mResourceId(0u),
48   mOrientationCorrection(orientationCorrection),
49   mLoadSuccess(false),
50   mPremultiplied(false)
51 {
52   mCallback = std::unique_ptr<CallbackBase>(callback);
53   PrepareTexture();
54 }
55
56 FastTrackLoadingTask::~FastTrackLoadingTask()
57 {
58 }
59
60 void FastTrackLoadingTask::PrepareTexture()
61 {
62   mTexture    = mTextureUploadManager.GenerateTexture2D();
63   mResourceId = Integration::GetTextureResourceId(mTexture);
64 }
65
66 void FastTrackLoadingTask::OnComplete(AsyncTaskPtr task)
67 {
68   if(mLoadSuccess)
69   {
70     Dali::Integration::SetTextureSize(mTexture, Dali::ImageDimensions(mImageWidth, mImageHeight));
71     Dali::Integration::SetTexturePixelFormat(mTexture, mImageFormat);
72   }
73   if(mCallback)
74   {
75     CallbackBase::Execute(*mCallback, FastTrackLoadingTaskPtr(reinterpret_cast<FastTrackLoadingTask*>(task.Get())));
76   }
77 }
78
79 // Called by worker thread
80
81 void FastTrackLoadingTask::Process()
82 {
83   Load();
84   UploadToTexture();
85 }
86
87 bool FastTrackLoadingTask::IsReady()
88 {
89   return true;
90 }
91
92 void FastTrackLoadingTask::Load()
93 {
94   Devel::PixelBuffer              pixelBuffer;
95   std::vector<Devel::PixelBuffer> pixelBuffers;
96
97   if(mUrl.IsValid() && mUrl.IsLocalResource())
98   {
99     // TODO : We need to consider YUV case in future.
100     //Dali::LoadImagePlanesFromFile(mUrl.GetUrl(), pixelBuffers, mDimensions, mFittingMode, mSamplingMode, mOrientationCorrection);
101
102     pixelBuffer = Dali::LoadImageFromFile(mUrl.GetUrl(), mDimensions, mFittingMode, mSamplingMode, mOrientationCorrection);
103   }
104   else if(mUrl.IsValid())
105   {
106     pixelBuffer = Dali::DownloadImageSynchronously(mUrl.GetUrl(), mDimensions, mFittingMode, mSamplingMode, mOrientationCorrection);
107   }
108
109   if(pixelBuffer)
110   {
111     pixelBuffers.push_back(pixelBuffer);
112   }
113
114   if(pixelBuffers.empty())
115   {
116     DALI_LOG_ERROR("FastTrackLoadingTask::Load: Loading is failed: ResourceId : %d, url : [%s]\n", mResourceId, mUrl.GetUrl().c_str());
117   }
118   else
119   {
120     if(pixelBuffers.size() == 1u)
121     {
122       mLoadSuccess = true;
123       MultiplyAlpha(pixelBuffers[0]);
124       mPixelData = Dali::Devel::PixelBuffer::Convert(pixelBuffers[0]);
125     }
126     else
127     {
128       DALI_LOG_ERROR("FastTrackLoadingTask::Load: ??? Undefined case. PixelBuffers.size() : %zu : ResourceId : %d, url : [%s]\n", pixelBuffers.size(), mResourceId, mUrl.GetUrl().c_str());
129     }
130   }
131 }
132
133 void FastTrackLoadingTask::MultiplyAlpha(Dali::Devel::PixelBuffer pixelBuffer)
134 {
135   if(mPreMultiplyOnLoad == DevelAsyncImageLoader::PreMultiplyOnLoad::ON && Pixel::HasAlpha(pixelBuffer.GetPixelFormat()))
136   {
137     pixelBuffer.MultiplyColorByAlpha();
138     mPremultiplied = pixelBuffer.IsAlphaPreMultiplied();
139   }
140 }
141
142 void FastTrackLoadingTask::UploadToTexture()
143 {
144   if(mLoadSuccess)
145   {
146     mImageWidth  = mPixelData.GetWidth();
147     mImageHeight = mPixelData.GetHeight();
148     mImageFormat = mPixelData.GetPixelFormat();
149
150     mTextureUploadManager.RequestUpload(mResourceId, mPixelData);
151   }
152
153   mPixelData.Reset();
154 }
155
156 } // namespace Internal
157
158 } // namespace Toolkit
159
160 } // namespace Dali