1 #ifndef DALI_TOOLKIT_ATLAS_MANAGER_H
2 #define DALI_TOOLKIT_ATLAS_MANAGER_H
5 * Copyright (c) 2020 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.
22 #include <dali/public-api/common/dali-vector.h>
23 #include <dali/public-api/rendering/texture-set.h>
31 namespace Internal DALI_INTERNAL
36 } // namespace Internal
38 class AtlasManager : public BaseHandle
42 typedef uint32_t SizeType;
43 typedef SizeType AtlasId;
44 typedef SizeType ImageId;
48 SizeType mWidth; ///< width of the atlas in pixels
49 SizeType mHeight; ///< height of the atlas in pixels
50 SizeType mBlockWidth; ///< width of a block in pixels
51 SizeType mBlockHeight; ///< height of a block in pixels
55 * Metrics structures to describe Atlas Manager state
58 struct AtlasMetricsEntry
60 AtlasSize mSize; ///< size of atlas and blocks
61 SizeType mBlocksUsed; ///< number of blocks used in the atlas
62 SizeType mTotalBlocks; ///< total blocks used by atlas
63 Pixel::Format mPixelFormat; ///< pixel format of the atlas
70 mTextureMemoryUsed( 0u )
76 SizeType mAtlasCount; ///< number of atlases
77 SizeType mTextureMemoryUsed; ///< texture memory used by atlases
78 Dali::Vector< AtlasMetricsEntry > mAtlasMetrics; ///< container of atlas information
83 Vector2 mPosition; ///< Vertex posiiton
84 Vector2 mTexCoords; ///< Vertex texture co-ordinates
85 Vector4 mColor; ///< Vertex color
90 Vector< Vertex2D > mVertices; ///< container of vertices
91 Vector< unsigned short > mIndices; ///< container of indices
95 * Create an AtlasManager handle; this can be initialised with AtlasManager::New()
96 * Calling member functions with an uninitialised handle is not allowed.
101 * @brief Get new instance of AtlasManager object.
103 * @return A handle to the AtlasManager control.
105 static AtlasManager New();
110 * This is non-virtual since derived Handle types must not contain data or virtual methods.
115 * Policy on failing to add an image
124 * @brief Container to hold result of placing texture into atlas
128 ImageId mImageId; ///< Id of stored Image
129 AtlasId mAtlasId; ///< Id of Atlas containing this slot
132 typedef Dali::Vector< AtlasManager::AtlasSlot > slotContainer;
135 * @brief Create a blank atlas of specific dimensions and pixel format with a certain block size
137 * @param[in] size desired atlas dimensions
138 * @param[in] pixelformat format of a pixel in atlas
142 AtlasId CreateAtlas( const AtlasSize& size, Pixel::Format pixelformat = Pixel::RGBA8888 );
145 * @brief Set the policy on failure to add an image to an atlas
147 * @param policy policy to carry out if add fails
149 void SetAddPolicy( AddFailPolicy policy );
152 * @brief Attempts to add an image to the most suitable atlas
154 * @details Add Policy may dictate that a new atlas is created if it can't presently be placed.
155 * If an add is made before an atlas is created under this policy,
156 * then a default size atlas will be created
158 * @param[in] image PixelData object containing the image data
159 * @param[out] slot result of add operation
160 * @param[in] atlas optional preferred atlas
162 * @return true if a new atlas was created
164 bool Add( const PixelData& image,
169 * @brief Remove previously added bitmapimage from atlas
171 * @param[in] id ImageId returned in the AtlasSlot from the add operation
173 * @return if true then image has been removed from the atlas
175 bool Remove( ImageId id );
178 * @brief Generate mesh data for a previously added image
180 * @param[in] id Image Id returned in the AtlasSlot from the add operation
181 * @param[in] position position of the resulting mesh in model space
182 * @param[out] mesh Mesh Data Object to populate with mesh data
183 * @param[in] addReference Whether to increase the internal reference count for image or not
185 void GenerateMeshData( ImageId id,
186 const Vector2& position,
188 bool addReference = true );
191 * @brief Get the Texture containing an atlas
193 * @param[in] atlas AtlasId returned when atlas was created
195 * @return Atlas Handle
197 Dali::Texture GetAtlasContainer( AtlasId atlas ) const;
200 * @brief Get the Id of the atlas containing an image
202 * @param[in] id ImageId
206 AtlasId GetAtlas( ImageId id );
208 * @brief Get the current size of an atlas
210 * @param[in] atlas AtlasId
212 * @return AtlasSize structure for the atlas
214 const AtlasSize& GetAtlasSize( AtlasId atlas );
217 * @brief Get the number of blocks available in an atlas
219 * @param[in] atlas AtlasId
221 * @return Number of blocks free in this atlas
223 SizeType GetFreeBlocks( AtlasId atlas );
226 * @brief Sets the pixel area of any new atlas and also the individual block size
228 * @param[in] size Atlas size structure
230 * @param blockSize pixel area in atlas for a block
232 void SetNewAtlasSize( const AtlasSize& size );
235 * @brief Get the number of atlases created
237 * @return number of atlases
239 SizeType GetAtlasCount() const;
242 * @brief Get the pixel format used by an atlas
244 * @param[in] atlas AtlasId
246 * @return Pixel format used by this atlas
248 Pixel::Format GetPixelFormat( AtlasId atlas ) const;
251 * @brief Fill in a metrics structure showing current status of this Atlas Manager
253 * @param[in] metrics metrics structure to be filled
255 void GetMetrics( Metrics& metrics );
258 * @brief Get TextureSet used by atlas
260 * @param[in] atlas AtlasId
262 * @return TextureSet used by atlas
264 TextureSet GetTextures( AtlasId atlas ) const;
267 * @brief Set the texture set used by an atlas
269 * @param[in] atlas AtlasId
270 * @param[in] textureSet The texture set to assign
272 void SetTextures( AtlasId atlas, TextureSet& textureSet );
276 explicit DALI_INTERNAL AtlasManager(Internal::AtlasManager *impl);
280 } // namespace Toolkit
284 #endif // DALI_TOOLKIT_ATLAS_MANAGER_H