Apply premultiply in animated image visual
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / texture-manager / texture-async-loading-helper.h
1 #ifndef DALI_TOOLKIT_TEXTURE_ASYNC_LOADING_HELPER_H
2 #define DALI_TOOLKIT_TEXTURE_ASYNC_LOADING_HELPER_H
3
4 /*
5  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 // EXTERNAL INCLUDES
21 #include <dali/public-api/signals/connection-tracker.h>
22 #include <deque>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/internal/image-loader/async-image-loader-impl.h>
26 #include <dali-toolkit/internal/texture-manager/texture-manager-impl.h>
27
28 namespace Dali
29 {
30 namespace Toolkit
31 {
32 namespace Internal
33 {
34 /**
35  * @brief Helper class to keep the relation between AsyncImageLoader and corresponding LoadingInfo container
36  */
37 class TextureAsyncLoadingHelper : public ConnectionTracker
38 {
39   /**
40    * Struct to hold information about a requested Async load.
41    * This is used to look up a TextureManager::TextureId from the returned AsyncLoad Id.
42    */
43   struct AsyncLoadingInfo
44   {
45     AsyncLoadingInfo(TextureManager::TextureId textureId)
46     : textureId(textureId),
47       loadId(0)
48     {
49     }
50
51     TextureManager::TextureId textureId; ///< The external Texture Id assigned to this load
52     std::uint32_t             loadId;    ///< The load Id used by the async loader to reference this load
53   };
54
55 public:
56   /**
57    * @brief Create an TextureAsyncLoadingHelper.
58    * @param[in] textureManager Reference to the texture manager
59    */
60   TextureAsyncLoadingHelper(TextureManager& textureManager);
61
62   /**
63    * @brief Load a new frame of animated image
64    * @param[in] textureId             TextureId to reference the texture that will be loaded
65    * @param[in] animatedImageLoading  The AnimatedImageLoading to load animated image
66    * @param[in] frameIndex            The frame index of a frame to be loaded frame
67    * @param[in] preMultiplyOnLoad     if the image's color should be multiplied by it's alpha. Set to OFF if there is no alpha or if the image need to be applied alpha mask.
68    */
69   void LoadAnimatedImage(const TextureManager::TextureId&                textureId,
70                          Dali::AnimatedImageLoading                      animatedImageLoading,
71                          const std::uint32_t&                            frameIndex,
72                          const DevelAsyncImageLoader::PreMultiplyOnLoad& preMultiplyOnLoad);
73
74   /**
75    * @brief Load a new texture.
76    * @param[in] textureId             TextureId to reference the texture that will be loaded
77    * @param[in] url                   The URL of the image to load
78    * @param[in] desiredSize           The size the image is likely to appear at.
79    *                                  This can be set to 0,0 for automatic
80    * @param[in] fittingMode           The FittingMode to use
81    * @param[in] samplingMode          The SamplingMode to use
82    * @param[in] orientationCorrection Whether to use image metadata to rotate or flip the image,
83    *                                  e.g., from portrait to landscape
84    * @param[in] preMultiplyOnLoad     if the image's color should be multiplied by it's alpha. Set to OFF if there is no alpha or if the image need to be applied alpha mask.
85    */
86   void Load(const TextureManager::TextureId&                textureId,
87             const VisualUrl&                                url,
88             const Dali::ImageDimensions&                    desiredSize,
89             const Dali::FittingMode::Type&                  fittingMode,
90             const Dali::SamplingMode::Type&                 samplingMode,
91             const bool&                                     orientationCorrection,
92             const DevelAsyncImageLoader::PreMultiplyOnLoad& preMultiplyOnLoad);
93
94   /**
95    * @brief Apply mask
96    * @param [in] textureId of the texture
97    * @param [in] pixelBuffer of the to be masked image
98    * @param [in] maskPixelBuffer of the mask image
99    * @param [in] contentScale The factor to scale the content
100    * @param [in] cropToMask Whether to crop the content to the mask size
101    * @param [in] preMultiplyOnLoad if the image's color should be multiplied by it's alpha. Set to OFF if there is no alpha.
102    */
103   void ApplyMask(const TextureManager::TextureId&                textureId,
104                  Devel::PixelBuffer                              pixelBuffer,
105                  Devel::PixelBuffer                              maskPixelBuffer,
106                  const float&                                    contentScale,
107                  const bool&                                     cropToMask,
108                  const DevelAsyncImageLoader::PreMultiplyOnLoad& preMultiplyOnLoad);
109
110 public:
111   TextureAsyncLoadingHelper(const TextureAsyncLoadingHelper&) = delete;
112   TextureAsyncLoadingHelper& operator=(const TextureAsyncLoadingHelper&) = delete;
113
114   TextureAsyncLoadingHelper(TextureAsyncLoadingHelper&& rhs);
115   TextureAsyncLoadingHelper& operator=(TextureAsyncLoadingHelper&& rhs) = delete;
116
117 private:                                                              // Private typedefs:
118   typedef std::deque<AsyncLoadingInfo> AsyncLoadingInfoContainerType; ///< The container type used to manage Asynchronous loads in progress
119
120 private:
121   /**
122    * @brief Main constructor that used by all other constructors
123    */
124   TextureAsyncLoadingHelper(Toolkit::AsyncImageLoader       loader,
125                             TextureManager&                 textureManager,
126                             AsyncLoadingInfoContainerType&& loadingInfoContainer);
127
128   /**
129    * @brief Callback to be called when texture loading is complete, it passes the pixel buffer on to texture manager.
130    * @param[in] id          Loader id
131    * @param[in] pixelBuffer Image data
132    */
133   void AsyncLoadComplete(std::uint32_t id, Devel::PixelBuffer pixelBuffer);
134
135 private: // Member Variables:
136   Toolkit::AsyncImageLoader     mLoader;
137   TextureManager&               mTextureManager;
138   AsyncLoadingInfoContainerType mLoadingInfoContainer;
139 };
140
141 } // namespace Internal
142
143 } // namespace Toolkit
144
145 } // namespace Dali
146
147 #endif // DALI_TOOLKIT_TEXTURE_ASYNC_LOADING_HELPER_H