1 #ifndef DALI_TOOLKIT_ATLAS_MANAGER_H
2 #define DALI_TOOLKIT_ATLAS_MANAGER_H
5 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <dali/public-api/common/dali-vector.h>
22 #include <dali/public-api/rendering/texture-set.h>
29 namespace Internal DALI_INTERNAL
33 } // namespace DALI_INTERNAL
35 class AtlasManager : public BaseHandle
38 typedef uint32_t SizeType;
39 typedef SizeType AtlasId;
40 typedef SizeType ImageId;
44 SizeType mWidth; ///< width of the atlas in pixels
45 SizeType mHeight; ///< height of the atlas in pixels
46 SizeType mBlockWidth; ///< width of a block in pixels
47 SizeType mBlockHeight; ///< height of a block in pixels
51 * Metrics structures to describe Atlas Manager state
54 struct AtlasMetricsEntry
56 AtlasSize mSize; ///< size of atlas and blocks
57 SizeType mBlocksUsed; ///< number of blocks used in the atlas
58 SizeType mTotalBlocks; ///< total blocks used by atlas
59 Pixel::Format mPixelFormat; ///< pixel format of the atlas
66 mTextureMemoryUsed(0u)
74 SizeType mAtlasCount; ///< number of atlases
75 SizeType mTextureMemoryUsed; ///< texture memory used by atlases
76 Dali::Vector<AtlasMetricsEntry> mAtlasMetrics; ///< container of atlas information
81 Vector2 mPosition; ///< Vertex posiiton
82 Vector2 mTexCoords; ///< Vertex texture co-ordinates
83 Vector4 mColor; ///< Vertex color
88 Vector<Vertex2D> mVertices; ///< container of vertices
89 Vector<unsigned short> mIndices; ///< container of indices
93 * Create an AtlasManager handle; this can be initialised with AtlasManager::New()
94 * Calling member functions with an uninitialised handle is not allowed.
99 * @brief Get new instance of AtlasManager object.
101 * @return A handle to the AtlasManager control.
103 static AtlasManager New();
108 * This is non-virtual since derived Handle types must not contain data or virtual methods.
113 * Policy on failing to add an image
122 * @brief Container to hold result of placing texture into atlas
126 ImageId mImageId; ///< Id of stored Image
127 AtlasId mAtlasId; ///< Id of Atlas containing this slot
130 typedef Dali::Vector<AtlasManager::AtlasSlot> slotContainer;
133 * @brief Create a blank atlas of specific dimensions and pixel format with a certain block size
135 * @param[in] size desired atlas dimensions
136 * @param[in] pixelformat format of a pixel in atlas
140 AtlasId CreateAtlas(const AtlasSize& size, Pixel::Format pixelformat = Pixel::RGBA8888);
143 * @brief Set the policy on failure to add an image to an atlas
145 * @param policy policy to carry out if add fails
147 void SetAddPolicy(AddFailPolicy policy);
150 * @brief Attempts to add an image to the most suitable atlas
152 * @details Add Policy may dictate that a new atlas is created if it can't presently be placed.
153 * If an add is made before an atlas is created under this policy,
154 * then a default size atlas will be created
156 * @param[in] image PixelData object containing the image data
157 * @param[out] slot result of add operation
158 * @param[in] atlas optional preferred atlas
160 * @return true if a new atlas was created
162 bool Add(const PixelData& image,
167 * @brief Remove previously added bitmapimage from atlas
169 * @param[in] id ImageId returned in the AtlasSlot from the add operation
171 * @return if true then image has been removed from the atlas
173 bool Remove(ImageId id);
176 * @brief Generate mesh data for a previously added image
178 * @param[in] id Image Id returned in the AtlasSlot from the add operation
179 * @param[in] position position of the resulting mesh in model space
180 * @param[out] mesh Mesh Data Object to populate with mesh data
181 * @param[in] addReference Whether to increase the internal reference count for image or not
183 void GenerateMeshData(ImageId id,
184 const Vector2& position,
186 bool addReference = true);
189 * @brief Get the Texture containing an atlas
191 * @param[in] atlas AtlasId returned when atlas was created
193 * @return Atlas Handle
195 Dali::Texture GetAtlasContainer(AtlasId atlas) const;
198 * @brief Get the Id of the atlas containing an image
200 * @param[in] id ImageId
204 AtlasId GetAtlas(ImageId id);
206 * @brief Get the current size of an atlas
208 * @param[in] atlas AtlasId
210 * @return AtlasSize structure for the atlas
212 const AtlasSize& GetAtlasSize(AtlasId atlas);
215 * @brief Get the number of blocks available in an atlas
217 * @param[in] atlas AtlasId
219 * @return Number of blocks free in this atlas
221 SizeType GetFreeBlocks(AtlasId atlas);
224 * @brief Sets the pixel area of any new atlas and also the individual block size
226 * @param[in] size Atlas size structure
228 * @param blockSize pixel area in atlas for a block
230 void SetNewAtlasSize(const AtlasSize& size);
233 * @brief Get the number of atlases created
235 * @return number of atlases
237 SizeType GetAtlasCount() const;
240 * @brief Get the pixel format used by an atlas
242 * @param[in] atlas AtlasId
244 * @return Pixel format used by this atlas
246 Pixel::Format GetPixelFormat(AtlasId atlas) const;
249 * @brief Fill in a metrics structure showing current status of this Atlas Manager
251 * @param[in] metrics metrics structure to be filled
253 void GetMetrics(Metrics& metrics);
256 * @brief Get TextureSet used by atlas
258 * @param[in] atlas AtlasId
260 * @return TextureSet used by atlas
262 TextureSet GetTextures(AtlasId atlas) const;
265 * @brief Set the texture set used by an atlas
267 * @param[in] atlas AtlasId
268 * @param[in] textureSet The texture set to assign
270 void SetTextures(AtlasId atlas, TextureSet& textureSet);
273 explicit DALI_INTERNAL AtlasManager(Internal::AtlasManager* impl);
276 } // namespace Toolkit
280 #endif // DALI_TOOLKIT_ATLAS_MANAGER_H