Text Shadow Implementation via copied renderer.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / rendering / atlas / atlas-manager.h
1 #ifndef __DALI_TOOLKIT_ATLAS_MANAGER_H__
2 #define __DALI_TOOLKIT_ATLAS_MANAGER_H__
3
4 /*
5  * Copyright (c) 2015 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 <stdint.h>
22 #include <dali/public-api/common/dali-vector.h>
23 #include <dali/public-api/images/buffer-image.h>
24 #include <dali/devel-api/images/atlas.h>
25 #include <dali/devel-api/rendering/material.h>
26
27 namespace Dali
28 {
29
30 namespace Toolkit
31 {
32
33 namespace Internal DALI_INTERNAL
34 {
35
36 class AtlasManager;
37
38 } // namespace Internal
39
40 class AtlasManager : public BaseHandle
41 {
42 public:
43
44   typedef uint32_t SizeType;
45   typedef SizeType AtlasId;
46   typedef SizeType ImageId;
47
48   struct AtlasSize
49   {
50     SizeType mWidth;              ///< width of the atlas in pixels
51     SizeType mHeight;             ///< height of the atlas in pixels
52     SizeType mBlockWidth;         ///< width of a block in pixels
53     SizeType mBlockHeight;        ///< height of a block in pixels
54   };
55
56   /**
57    * Metrics structures to describe Atlas Manager state
58    *
59    */
60   struct AtlasMetricsEntry
61   {
62     AtlasSize mSize;                 ///< size of atlas and blocks
63     SizeType mBlocksUsed;            ///< number of blocks used in the atlas
64     SizeType mTotalBlocks;           ///< total blocks used by atlas
65     Pixel::Format mPixelFormat;      ///< pixel format of the atlas
66   };
67
68   struct Metrics
69   {
70     Metrics()
71     : mAtlasCount( 0u ),
72       mTextureMemoryUsed( 0u )
73     {}
74
75     ~Metrics()
76     {}
77
78     SizeType mAtlasCount;                               ///< number of atlases
79     SizeType mTextureMemoryUsed;                        ///< texture memory used by atlases
80     Dali::Vector< AtlasMetricsEntry > mAtlasMetrics;    ///< container of atlas information
81   };
82
83   struct Vertex2D
84   {
85     Vector2 mPosition;        ///< Vertex posiiton
86     Vector2 mTexCoords;       ///< Vertex texture co-ordinates
87   };
88
89   struct Mesh2D
90   {
91     Vector< Vertex2D > mVertices;       ///< container of vertices
92     Vector< SizeType > mIndices;        ///< container of indices
93   };
94
95   /**
96    * Create an AtlasManager handle; this can be initialised with AtlasManager::New()
97    * Calling member functions with an uninitialised handle is not allowed.
98    */
99   AtlasManager();
100
101   /**
102    * @brief Get new instance of AtlasManager object.
103    *
104    * @return A handle to the AtlasManager control.
105    */
106   static AtlasManager New();
107
108   /**
109    * @brief Destructor
110    *
111    * This is non-virtual since derived Handle types must not contain data or virtual methods.
112    */
113   ~AtlasManager();
114
115   /**
116    * Policy on failing to add an image
117    */
118   enum AddFailPolicy
119   {
120     FAIL_ON_ADD_FAILS,
121     FAIL_ON_ADD_CREATES
122   };
123
124   /**
125    * @brief Container to hold result of placing texture into atlas
126    */
127   struct AtlasSlot
128   {
129     ImageId mImageId;                           ///< Id of stored Image
130     AtlasId mAtlasId;                           ///< Id of Atlas containing this slot
131   };
132
133   typedef Dali::Vector< AtlasManager::AtlasSlot > slotContainer;
134
135   /**
136    * @brief Create a blank atlas of specific dimensions and pixel format with a certain block size
137    *
138    * @param[in] size desired atlas dimensions
139    * @param[in] pixelformat format of a pixel in atlas
140    *
141    * @return atlas Id
142    */
143   AtlasId CreateAtlas( const AtlasSize& size, Pixel::Format pixelformat = Pixel::RGBA8888 );
144
145   /**
146    * @brief Set the policy on failure to add an image to an atlas
147    *
148    * @param policy policy to carry out if add fails
149    */
150   void SetAddPolicy( AddFailPolicy policy );
151
152   /**
153    * @brief Attempts to add an image to the most suitable atlas
154    *
155    * @details Add Policy may dictate that a new atlas is created if it can't presently be placed.
156    *          If an add is made before an atlas is created under this policy,
157    *          then a default size atlas will be created
158    *
159    * @param[in] image reference to a bitmapimage
160    * @param[out] slot result of add operation
161    * @param[in] atlas optional preferred atlas
162    *
163    * @return true if a new atlas was created
164    */
165   bool Add( const BufferImage& image,
166             AtlasSlot& slot,
167             AtlasId atlas = 0 );
168
169   /**
170    * @brief Remove previously added bitmapimage from atlas
171    *
172    * @param[in] id ImageId returned in the AtlasSlot from the add operation
173    *
174    * @return if true then image has been removed from the atlas
175    */
176   bool Remove( ImageId id );
177
178   /**
179    * @brief Generate mesh data for a previously added image
180    *
181    * @param[in] id Image Id returned in the AtlasSlot from the add operation
182    * @param[in] position position of the resulting mesh in model space
183    * @param[out] mesh Mesh Data Object to populate with mesh data
184    * @param[in] addReference Whether to increase the internal reference count for image or not
185    */
186   void GenerateMeshData( ImageId id,
187                          const Vector2& position,
188                          Mesh2D& mesh,
189                          bool addReference = true );
190
191   /**
192    * @brief Get the BufferImage containing an atlas
193    *
194    * @param[in] atlas AtlasId returned when atlas was created
195    *
196    * @return Atlas Handle
197    */
198   Dali::Atlas GetAtlasContainer( AtlasId atlas ) const;
199
200   /**
201    * @brief Get the Id of the atlas containing an image
202    *
203    * @param[in] id ImageId
204    *
205    * @return Atlas Id
206    */
207   AtlasId GetAtlas( ImageId id );
208   /**
209    * @brief Get the current size of an atlas
210    *
211    * @param[in] atlas AtlasId
212    *
213    * @return AtlasSize structure for the atlas
214    */
215   const AtlasSize& GetAtlasSize( AtlasId atlas );
216
217   /**
218    * @brief Get the number of blocks available in an atlas
219    *
220    * @param[in] atlas AtlasId
221    *
222    * @return Number of blocks free in this atlas
223    */
224   SizeType GetFreeBlocks( AtlasId atlas );
225
226   /**
227    * @brief Sets the pixel area of any new atlas and also the individual block size
228    *
229    * @param[in] size Atlas size structure
230    *
231    * @param blockSize pixel area in atlas for a block
232    */
233   void SetNewAtlasSize( const AtlasSize& size );
234
235   /**
236    * @brief Get the number of atlases created
237    *
238    * @return number of atlases
239    */
240   SizeType GetAtlasCount() const;
241
242   /**
243    * @brief Get the pixel format used by an atlas
244    *
245    * @param[in] atlas AtlasId
246    *
247    * @return Pixel format used by this atlas
248    */
249   Pixel::Format GetPixelFormat( AtlasId atlas );
250
251   /**
252    * @brief Fill in a metrics structure showing current status of this Atlas Manager
253    *
254    * @param[in] metrics metrics structure to be filled
255    */
256   void GetMetrics( Metrics& metrics );
257
258   /**
259    * @brief Get Material used by atlas
260    *
261    * @param[in] atlas AtlasId
262    *
263    * @return Material used by atlas
264    */
265   Material GetMaterial( AtlasId atlas ) const;
266
267   /**
268    * @brief Set the material used by an atlas
269    *
270    * @param[in] atlas AtlasId
271    * @param[in] material The Material to assign
272    */
273   void SetMaterial( AtlasId atlas, Material& material );
274
275 private:
276
277   explicit DALI_INTERNAL AtlasManager(Internal::AtlasManager *impl);
278
279 };
280
281 } // namespace Toolkit
282
283 } // namespace Dali
284
285 #endif // __DALI_TOOLKIT_ATLAS_MANAGER_H__