[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / image-atlas-manager.h
1 #ifndef DALI_TOOLKIT_IMAGE_ATLAS_MANAGER_H
2 #define DALI_TOOLKIT_IMAGE_ATLAS_MANAGER_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/adaptor-framework/encoded-image-buffer.h>
22 #include <dali/public-api/common/vector-wrapper.h>
23 #include <dali/public-api/object/ref-object.h>
24 #include <dali/public-api/rendering/texture-set.h>
25 #include <string>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/devel-api/image-loader/image-atlas.h>
29 #include <dali-toolkit/internal/visuals/visual-url.h>
30
31 namespace Dali
32 {
33 namespace Toolkit
34 {
35 class AtlasUploadObserver;
36
37 namespace Internal
38 {
39 /**
40  * The manager for automatic image atlasing. Owned by VisualFactory
41  */
42 class ImageAtlasManager : public RefObject
43 {
44 public:
45   typedef std::vector<Toolkit::ImageAtlas> AtlasContainer;
46   typedef std::vector<TextureSet>          TextureSetContainer;
47
48 public:
49   /**
50    * Construtor
51    *
52    */
53   ImageAtlasManager();
54
55   /**
56    * @brief Check whether the image of url could be Atlas or not.
57    *
58    * @param [in] url The URL of the resource image file to use.
59    * @param [in] size The width and height to fit the loaded image to.
60    * @return True if the image could be Atlas.
61    */
62   bool CheckAtlasAvailable(const VisualUrl& url, const ImageDimensions& size) const;
63
64   /**
65    * @brief Add an image to the atlas.
66    *
67    * @note To make the atlasing efficient, an valid size should be provided.
68    *       If size is not provided, then the image file will be opened to read the actual size for loading.
69    *
70    * SamplingMode::BOX_THEN_LINEAR is used to sampling pixels from the input image while fitting it to desired size.
71    *
72    * @param [out] textureRect The texture area of the resource image in the atlas.
73    * @param [in] url The URL of the resource image file to use.
74    * @param [in, out] size The width and height to fit the loaded image to.
75    * @param [in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter.
76    * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
77    * @param [in] atlasUploadObserver The object to observe the uploading state inside ImageAtlas.
78    * @return The texture set containing the image.
79    */
80   TextureSet Add(Vector4&             textureRect,
81                  const VisualUrl&     url,
82                  ImageDimensions&     size,
83                  FittingMode::Type    fittingMode           = FittingMode::DEFAULT,
84                  bool                 orientationCorrection = true,
85                  AtlasUploadObserver* atlasUploadObserver   = NULL);
86
87   /**
88    * @brief Add an image to the atlas.
89    *
90    * @note To make the atlasing efficient, an valid size should be provided.
91    *
92    * SamplingMode::BOX_THEN_LINEAR is used to sampling pixels from the input image while fitting it to desired size.
93    *
94    * @param [out] textureRect The texture area of the resource image in the atlas.
95    * @param [in] encodedImageBuffer The encoded buffer of the resource image file to use.
96    * @param [in] size The width and height to fit the loaded image to.
97    * @param [in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter.
98    * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
99    * @param [in] atlasUploadObserver The object to observe the uploading state inside ImageAtlas.
100    * @return The texture set containing the image.
101    */
102   TextureSet Add(Vector4&                  textureRect,
103                  const EncodedImageBuffer& encodedImageBuffer,
104                  const ImageDimensions&    size,
105                  FittingMode::Type         fittingMode           = FittingMode::DEFAULT,
106                  bool                      orientationCorrection = true,
107                  AtlasUploadObserver*      atlasUploadObserver   = NULL);
108
109   /**
110    * @brief Add a pixel buffer to the atlas
111    *
112    * @param [out] textureRect The texture area of the resource image in the atlas.
113    * @param [in] pixelData The pixel data.
114    * @return The texture set containing the image.
115    */
116   TextureSet Add(Vector4&  textureRect,
117                  PixelData pixelData);
118
119   /**
120    * Remove the image at the given rectangle from the texture set.
121    *
122    * @param [in] textureSet The texture set containing the atlas image.
123    * @param [in] textureRect The texture area to be removed.
124    */
125   void Remove(TextureSet textureSet, const Vector4& textureRect);
126
127   /**
128    * @brief Set the broken image which is used to replace the image if loading fails.
129    *
130    * @param[in] brokenImageUrl The url of the broken image.
131    */
132   void SetBrokenImage(const std::string& brokenImageUrl);
133
134   /**
135    * @brief Get shader
136    */
137   Shader GetShader() const;
138
139 private:
140   /**
141    * @brief Create a new atlas.
142    *
143    * This method is called when the newly added image or pixel buffer cannot fit into the current atlas list.
144    */
145   void CreateNewAtlas();
146
147 protected:
148   /**
149    * Destructor
150    */
151   ~ImageAtlasManager() override;
152
153   /**
154    * Undefined copy constructor.
155    */
156   ImageAtlasManager(const ImageAtlasManager&);
157
158   /**
159    * Undefined assignment operator.
160    */
161   ImageAtlasManager& operator=(const ImageAtlasManager& rhs);
162
163 private:
164   AtlasContainer      mAtlasList;
165   TextureSetContainer mTextureSetList;
166   std::string         mBrokenImageUrl;
167 };
168
169 } // namespace Internal
170
171 } // namespace Toolkit
172
173 } // namespace Dali
174
175 #endif // DALI_TOOLKIT_ATLAS_MANAGER_H