[Tizen] FastTrackUpload task implement
[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   mLoadYuvImage(false),
51   mPremultiplied(false)
52 {
53   mCallback = std::unique_ptr<CallbackBase>(callback);
54   PrepareTexture();
55 }
56
57 FastTrackLoadingTask::~FastTrackLoadingTask()
58 {
59 }
60
61 void FastTrackLoadingTask::PrepareTexture()
62 {
63   mTexture    = mTextureUploadManager.GenerateTexture2D();
64   mResourceId = Integration::GetTextureResourceId(mTexture);
65 }
66
67 void FastTrackLoadingTask::OnComplete(AsyncTaskPtr task)
68 {
69   if(mLoadSuccess)
70   {
71     Dali::Integration::SetTextureSize(mTexture, Dali::ImageDimensions(mImageWidth, mImageHeight));
72     Dali::Integration::SetTexturePixelFormat(mTexture, mImageFormat);
73   }
74   if(mCallback)
75   {
76     CallbackBase::Execute(*mCallback, FastTrackLoadingTaskPtr(reinterpret_cast<FastTrackLoadingTask*>(task.Get())));
77   }
78 }
79
80 // Called by worker thread
81
82 void FastTrackLoadingTask::Process()
83 {
84   Load();
85   UploadToTexture();
86 }
87
88 bool FastTrackLoadingTask::IsReady()
89 {
90   return true;
91 }
92
93 void FastTrackLoadingTask::Load()
94 {
95   Devel::PixelBuffer              pixelBuffer;
96   std::vector<Devel::PixelBuffer> pixelBuffers;
97
98   if(mUrl.IsValid() && mUrl.IsLocalResource())
99   {
100     // TODO : We need to consider YUV case in future.
101     //Dali::LoadImagePlanesFromFile(mUrl.GetUrl(), pixelBuffers, mDimensions, mFittingMode, mSamplingMode, mOrientationCorrection);
102
103     pixelBuffer = Dali::LoadImageFromFile(mUrl.GetUrl(), mDimensions, mFittingMode, mSamplingMode, mOrientationCorrection);
104   }
105   else if(mUrl.IsValid())
106   {
107     pixelBuffer = Dali::DownloadImageSynchronously(mUrl.GetUrl(), mDimensions, mFittingMode, mSamplingMode, mOrientationCorrection);
108   }
109
110   if(pixelBuffer)
111   {
112     pixelBuffers.push_back(pixelBuffer);
113   }
114
115   if(pixelBuffers.empty())
116   {
117     DALI_LOG_ERROR("FastTrackLoadingTask::Load: Loading is failed: ResourceId : %d, url : [%s]\n", mResourceId, mUrl.GetUrl().c_str());
118   }
119   else
120   {
121     if(pixelBuffers.size() == 1u)
122     {
123       mLoadSuccess = true;
124       MultiplyAlpha(pixelBuffers[0]);
125       mPixelData = Dali::Devel::PixelBuffer::Convert(pixelBuffers[0]);
126     }
127     else
128     {
129       DALI_LOG_ERROR("FastTrackLoadingTask::Load: ??? Undefined case. PixelBuffers.size() : %zu : ResourceId : %d, url : [%s]\n", pixelBuffers.size(), mResourceId, mUrl.GetUrl().c_str());
130     }
131   }
132 }
133
134 void FastTrackLoadingTask::MultiplyAlpha(Dali::Devel::PixelBuffer pixelBuffer)
135 {
136   if(mPreMultiplyOnLoad == DevelAsyncImageLoader::PreMultiplyOnLoad::ON && Pixel::HasAlpha(pixelBuffer.GetPixelFormat()))
137   {
138     pixelBuffer.MultiplyColorByAlpha();
139     mPremultiplied = pixelBuffer.IsAlphaPreMultiplied();
140   }
141 }
142
143 void FastTrackLoadingTask::UploadToTexture()
144 {
145   if(mLoadSuccess)
146   {
147     mImageWidth  = mPixelData.GetWidth();
148     mImageHeight = mPixelData.GetHeight();
149     mImageFormat = mPixelData.GetPixelFormat();
150
151     mTextureUploadManager.RequestUpload(mResourceId, mPixelData);
152   }
153
154   mPixelData.Reset();
155 }
156
157 } // namespace Internal
158
159 } // namespace Toolkit
160
161 } // namespace Dali