1eee5bcd2becbf67599a1c9f35adccc8d04fe22b
[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 {
47   mLoadingInfoContainer.push_back(AsyncLoadingInfo(textureId));
48   auto id                             = GetImplementation(mLoader).LoadAnimatedImage(animatedImageLoading, frameIndex);
49   mLoadingInfoContainer.back().loadId = id;
50 }
51
52 void TextureAsyncLoadingHelper::Load(const TextureManager::TextureId&                textureId,
53                                      const VisualUrl&                                url,
54                                      const Dali::ImageDimensions&                    desiredSize,
55                                      const Dali::FittingMode::Type&                  fittingMode,
56                                      const Dali::SamplingMode::Type&                 samplingMode,
57                                      const bool&                                     orientationCorrection,
58                                      const DevelAsyncImageLoader::PreMultiplyOnLoad& preMultiplyOnLoad)
59 {
60   mLoadingInfoContainer.push_back(AsyncLoadingInfo(textureId));
61   if(DALI_UNLIKELY(url.IsBufferResource()))
62   {
63     auto id                             = GetImplementation(mLoader).LoadEncodedImageBuffer(mTextureManager.GetEncodedImageBuffer(url.GetUrl()), desiredSize, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad);
64     mLoadingInfoContainer.back().loadId = id;
65   }
66   else
67   {
68     auto id                             = GetImplementation(mLoader).Load(url, desiredSize, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad);
69     mLoadingInfoContainer.back().loadId = id;
70   }
71 }
72
73 void TextureAsyncLoadingHelper::ApplyMask(const TextureManager::TextureId&                textureId,
74                                           Devel::PixelBuffer                              pixelBuffer,
75                                           Devel::PixelBuffer                              maskPixelBuffer,
76                                           const float&                                    contentScale,
77                                           const bool&                                     cropToMask,
78                                           const DevelAsyncImageLoader::PreMultiplyOnLoad& preMultiplyOnLoad)
79 {
80   mLoadingInfoContainer.push_back(AsyncLoadingInfo(textureId));
81   auto id                             = GetImplementation(mLoader).ApplyMask(pixelBuffer, maskPixelBuffer, contentScale, cropToMask, preMultiplyOnLoad);
82   mLoadingInfoContainer.back().loadId = id;
83 }
84
85 TextureAsyncLoadingHelper::TextureAsyncLoadingHelper(TextureAsyncLoadingHelper&& rhs)
86 : TextureAsyncLoadingHelper(rhs.mLoader, rhs.mTextureManager, std::move(rhs.mLoadingInfoContainer))
87 {
88 }
89
90 TextureAsyncLoadingHelper::TextureAsyncLoadingHelper(
91   Toolkit::AsyncImageLoader       loader,
92   TextureManager&                 textureManager,
93   AsyncLoadingInfoContainerType&& loadingInfoContainer)
94 : mLoader(loader),
95   mTextureManager(textureManager),
96   mLoadingInfoContainer(std::move(loadingInfoContainer))
97 {
98   DevelAsyncImageLoader::PixelBufferLoadedSignal(mLoader).Connect(
99     this, &TextureAsyncLoadingHelper::AsyncLoadComplete);
100 }
101
102 void TextureAsyncLoadingHelper::AsyncLoadComplete(uint32_t           id,
103                                                   Devel::PixelBuffer pixelBuffer)
104 {
105   DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureAsyncLoadingHelper::AsyncLoadComplete( loadId :%d )\n", id);
106   if(mLoadingInfoContainer.size() >= 1u)
107   {
108     AsyncLoadingInfo loadingInfo = mLoadingInfoContainer.front();
109
110     // We can assume that First Loading task comes First.
111     if(loadingInfo.loadId == id)
112     {
113       // Call TextureManager::AsyncLoadComplete
114       mTextureManager.AsyncLoadComplete(loadingInfo.textureId, pixelBuffer);
115     }
116
117     mLoadingInfoContainer.pop_front();
118   }
119 }
120
121 } // namespace Internal
122
123 } // namespace Toolkit
124
125 } // namespace Dali