Allow worker thread trace + Trace during image works
[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 #include <dali/integration-api/trace.h>
27
28 #ifdef TRACE_ENABLED
29 #include <sstream>
30 #endif
31
32 namespace Dali
33 {
34 namespace Toolkit
35 {
36 namespace Internal
37 {
38 namespace
39 {
40 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_IMAGE_PERFORMANCE_MARKER, false);
41 }
42
43 FastTrackLoadingTask::FastTrackLoadingTask(const VisualUrl& url, ImageDimensions dimensions, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection, DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad, CallbackBase* callback)
44 : AsyncTask(MakeCallback(this, &FastTrackLoadingTask::OnComplete), url.GetProtocolType() == VisualUrl::ProtocolType::REMOTE ? AsyncTask::PriorityType::LOW : AsyncTask::PriorityType::HIGH),
45   mUrl(url),
46   mTexture(),
47   mDimensions(dimensions),
48   mFittingMode(fittingMode),
49   mSamplingMode(samplingMode),
50   mPreMultiplyOnLoad(preMultiplyOnLoad),
51   mCallback(),
52   mTextureUploadManager(Dali::Devel::TextureUploadManager::Get()),
53   mImageWidth(0u),
54   mImageHeight(0u),
55   mImageFormat(Pixel::INVALID),
56   mPixelData(),
57   mResourceId(0u),
58   mOrientationCorrection(orientationCorrection),
59   mLoadSuccess(false),
60   mPremultiplied(false)
61 {
62   mCallback = std::unique_ptr<CallbackBase>(callback);
63   PrepareTexture();
64 }
65
66 FastTrackLoadingTask::~FastTrackLoadingTask()
67 {
68 }
69
70 void FastTrackLoadingTask::PrepareTexture()
71 {
72   mTexture    = mTextureUploadManager.GenerateTexture2D();
73   mResourceId = Integration::GetTextureResourceId(mTexture);
74 }
75
76 void FastTrackLoadingTask::OnComplete(AsyncTaskPtr task)
77 {
78   if(mLoadSuccess)
79   {
80     Dali::Integration::SetTextureSize(mTexture, Dali::ImageDimensions(mImageWidth, mImageHeight));
81     Dali::Integration::SetTexturePixelFormat(mTexture, mImageFormat);
82   }
83   if(mCallback)
84   {
85     CallbackBase::Execute(*mCallback, FastTrackLoadingTaskPtr(reinterpret_cast<FastTrackLoadingTask*>(task.Get())));
86   }
87 }
88
89 // Called by worker thread
90
91 void FastTrackLoadingTask::Process()
92 {
93   Load();
94   UploadToTexture();
95 }
96
97 bool FastTrackLoadingTask::IsReady()
98 {
99   return true;
100 }
101
102 void FastTrackLoadingTask::Load()
103 {
104 #ifdef TRACE_ENABLED
105   if(gTraceFilter && gTraceFilter->IsTraceEnabled())
106   {
107     std::ostringstream oss;
108     oss << "[url:" << mUrl.GetUrl() << "]";
109     DALI_TRACE_BEGIN_WITH_MESSAGE(gTraceFilter, "DALI_IMAGE_FAST_TRACK_UPLOADING_TASK", oss.str().c_str());
110   }
111 #endif
112
113   Devel::PixelBuffer              pixelBuffer;
114   std::vector<Devel::PixelBuffer> pixelBuffers;
115
116   if(mUrl.IsValid() && mUrl.IsLocalResource())
117   {
118     // TODO : We need to consider YUV case in future.
119     //Dali::LoadImagePlanesFromFile(mUrl.GetUrl(), pixelBuffers, mDimensions, mFittingMode, mSamplingMode, mOrientationCorrection);
120
121     pixelBuffer = Dali::LoadImageFromFile(mUrl.GetUrl(), mDimensions, mFittingMode, mSamplingMode, mOrientationCorrection);
122   }
123   else if(mUrl.IsValid())
124   {
125     pixelBuffer = Dali::DownloadImageSynchronously(mUrl.GetUrl(), mDimensions, mFittingMode, mSamplingMode, mOrientationCorrection);
126   }
127
128   if(pixelBuffer)
129   {
130     pixelBuffers.push_back(pixelBuffer);
131   }
132
133   if(pixelBuffers.empty())
134   {
135     DALI_LOG_ERROR("FastTrackLoadingTask::Load: Loading is failed: ResourceId : %d, url : [%s]\n", mResourceId, mUrl.GetUrl().c_str());
136   }
137   else
138   {
139     if(pixelBuffers.size() == 1u)
140     {
141       mLoadSuccess = true;
142       MultiplyAlpha(pixelBuffers[0]);
143       mPixelData = Dali::Devel::PixelBuffer::Convert(pixelBuffers[0]);
144     }
145     else
146     {
147       DALI_LOG_ERROR("FastTrackLoadingTask::Load: ??? Undefined case. PixelBuffers.size() : %zu : ResourceId : %d, url : [%s]\n", pixelBuffers.size(), mResourceId, mUrl.GetUrl().c_str());
148     }
149   }
150
151 #ifdef TRACE_ENABLED
152   if(gTraceFilter && gTraceFilter->IsTraceEnabled())
153   {
154     std::ostringstream oss;
155     oss << "[";
156     oss << "pixelBuffers: " << pixelBuffers.size() << " ";
157     if(!pixelBuffers.empty())
158     {
159       oss << "premult:" << mPremultiplied << " ";
160     }
161     oss << "url:" << mUrl.GetUrl() << "]";
162     DALI_TRACE_END_WITH_MESSAGE(gTraceFilter, "DALI_IMAGE_FAST_TRACK_UPLOADING_TASK", oss.str().c_str());
163   }
164 #endif
165 }
166
167 void FastTrackLoadingTask::MultiplyAlpha(Dali::Devel::PixelBuffer pixelBuffer)
168 {
169   if(mPreMultiplyOnLoad == DevelAsyncImageLoader::PreMultiplyOnLoad::ON && Pixel::HasAlpha(pixelBuffer.GetPixelFormat()))
170   {
171     pixelBuffer.MultiplyColorByAlpha();
172     mPremultiplied = pixelBuffer.IsAlphaPreMultiplied();
173   }
174 }
175
176 void FastTrackLoadingTask::UploadToTexture()
177 {
178   if(mLoadSuccess)
179   {
180     mImageWidth  = mPixelData.GetWidth();
181     mImageHeight = mPixelData.GetHeight();
182     mImageFormat = mPixelData.GetPixelFormat();
183
184     mTextureUploadManager.RequestUpload(mResourceId, mPixelData);
185   }
186
187   mPixelData.Reset();
188 }
189
190 } // namespace Internal
191
192 } // namespace Toolkit
193
194 } // namespace Dali