Support YUV decoding for JPEG
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / texture-manager / texture-async-loading-helper.cpp
1 /*
2  * Copyright (c) 2022 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 "texture-async-loading-helper.h"
20
21 // EXTERNAL HEADERS
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL HEADERS
25 #include <dali-toolkit/internal/image-loader/async-image-loader-impl.h>
26 #include <dali-toolkit/public-api/image-loader/sync-image-loader.h>
27
28 namespace Dali
29 {
30 namespace Toolkit
31 {
32 namespace Internal
33 {
34 #ifdef DEBUG_ENABLED
35 extern Debug::Filter* gTextureManagerLogFilter; ///< Define at texture-manager-impl.cpp
36 #endif
37
38 TextureAsyncLoadingHelper::TextureAsyncLoadingHelper(TextureManager& textureManager)
39 : TextureAsyncLoadingHelper(Toolkit::AsyncImageLoader::New(), textureManager, AsyncLoadingInfoContainerType())
40 {
41 }
42
43 void TextureAsyncLoadingHelper::LoadAnimatedImage(const TextureManager::TextureId&                textureId,
44                                                   Dali::AnimatedImageLoading                      animatedImageLoading,
45                                                   const std::uint32_t&                            frameIndex,
46                                                   const DevelAsyncImageLoader::PreMultiplyOnLoad& preMultiplyOnLoad)
47 {
48   mLoadingInfoContainer.push_back(AsyncLoadingInfo(textureId));
49   auto id                             = GetImplementation(mLoader).LoadAnimatedImage(animatedImageLoading, frameIndex, preMultiplyOnLoad);
50   mLoadingInfoContainer.back().loadId = id;
51 }
52
53 void TextureAsyncLoadingHelper::Load(const TextureManager::TextureId&                textureId,
54                                      const VisualUrl&                                url,
55                                      const Dali::ImageDimensions&                    desiredSize,
56                                      const Dali::FittingMode::Type&                  fittingMode,
57                                      const Dali::SamplingMode::Type&                 samplingMode,
58                                      const bool&                                     orientationCorrection,
59                                      const DevelAsyncImageLoader::PreMultiplyOnLoad& preMultiplyOnLoad,
60                                      const bool&                                     loadYuvPlanes)
61 {
62   mLoadingInfoContainer.push_back(AsyncLoadingInfo(textureId));
63   if(DALI_UNLIKELY(url.IsBufferResource()))
64   {
65     auto id                             = GetImplementation(mLoader).LoadEncodedImageBuffer(mTextureManager.GetEncodedImageBuffer(url.GetUrl()), desiredSize, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad);
66     mLoadingInfoContainer.back().loadId = id;
67   }
68   else
69   {
70     auto id                             = GetImplementation(mLoader).Load(url, desiredSize, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad, loadYuvPlanes);
71     mLoadingInfoContainer.back().loadId = id;
72   }
73 }
74
75 void TextureAsyncLoadingHelper::ApplyMask(const TextureManager::TextureId&                textureId,
76                                           Devel::PixelBuffer                              pixelBuffer,
77                                           Devel::PixelBuffer                              maskPixelBuffer,
78                                           const float&                                    contentScale,
79                                           const bool&                                     cropToMask,
80                                           const DevelAsyncImageLoader::PreMultiplyOnLoad& preMultiplyOnLoad)
81 {
82   mLoadingInfoContainer.push_back(AsyncLoadingInfo(textureId));
83   auto id                             = GetImplementation(mLoader).ApplyMask(pixelBuffer, maskPixelBuffer, contentScale, cropToMask, preMultiplyOnLoad);
84   mLoadingInfoContainer.back().loadId = id;
85 }
86
87 TextureAsyncLoadingHelper::TextureAsyncLoadingHelper(TextureAsyncLoadingHelper&& rhs)
88 : TextureAsyncLoadingHelper(rhs.mLoader, rhs.mTextureManager, std::move(rhs.mLoadingInfoContainer))
89 {
90 }
91
92 TextureAsyncLoadingHelper::TextureAsyncLoadingHelper(
93   Toolkit::AsyncImageLoader       loader,
94   TextureManager&                 textureManager,
95   AsyncLoadingInfoContainerType&& loadingInfoContainer)
96 : mLoader(loader),
97   mTextureManager(textureManager),
98   mLoadingInfoContainer(std::move(loadingInfoContainer))
99 {
100   DevelAsyncImageLoader::PixelBufferLoadedSignal(mLoader).Connect(
101     this, &TextureAsyncLoadingHelper::AsyncLoadComplete);
102 }
103
104 void TextureAsyncLoadingHelper::AsyncLoadComplete(uint32_t                         id,
105                                                   std::vector<Devel::PixelBuffer>& pixelBuffers)
106 {
107   DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureAsyncLoadingHelper::AsyncLoadComplete( loadId :%d )\n", id);
108   if(mLoadingInfoContainer.size() >= 1u)
109   {
110     AsyncLoadingInfo loadingInfo = mLoadingInfoContainer.front();
111
112     // We can assume that First Loading task comes First.
113     if(loadingInfo.loadId == id)
114     {
115       // Call TextureManager::AsyncLoadComplete
116       mTextureManager.AsyncLoadComplete(loadingInfo.textureId, pixelBuffers);
117     }
118
119     mLoadingInfoContainer.pop_front();
120   }
121 }
122
123 } // namespace Internal
124
125 } // namespace Toolkit
126
127 } // namespace Dali