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