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