Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / atlas-manager / atlas-manager-impl.h
1 #ifndef __DALI_TOOLKIT_ATLAS_MANAGER_IMPL_H__
2 #define __DALI_TOOLKIT_ATLAS_MANAGER_IMPL_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
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/common/vector-wrapper.h>
23 #include <dali/public-api/object/base-object.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/internal/atlas-manager/atlas-manager.h>
27
28 namespace Dali
29 {
30
31 namespace Toolkit
32 {
33
34 class AtlasManager;
35
36 } // namespace Toolkit
37
38 namespace Toolkit
39 {
40
41 namespace Internal
42 {
43
44 typedef Dali::Vector< Toolkit::AtlasManager::AtlasSlot > slotContainer;
45
46 class AtlasManager;
47 typedef IntrusivePtr<AtlasManager> AtlasManagerPtr;
48
49 class AtlasManager : public Dali::BaseObject
50 {
51 public:
52
53   typedef uint32_t SizeType;
54   typedef SizeType AtlasId;
55   typedef SizeType ImageId;
56
57   /**
58    * @brief Internal storage of atlas attributes and image upload results
59    */
60   struct AtlasDescriptor
61   {
62     Dali::Atlas mAtlas;                                                 // atlas image
63     SizeType mWidth;                                                    // width of atlas
64     SizeType mHeight;                                                   // height of atlas
65     SizeType mBlockWidth;                                               // width of a block in atlas
66     SizeType mBlockHeight;                                              // height of a block in atlas
67     Pixel::Format mPixelFormat;                                         // pixel format used by atlas
68     BufferImage mEdgeX;                                                 // Image used to pad upload
69     BufferImage mEdgeY;                                                 // Image used to pad upload
70     Material mMaterial;                                                 // material used for atlas texture
71     SizeType mNextFreeBlock;                                            // next free block will be placed here ( actually +1 )
72     Dali::Vector< SizeType > mFreeBlocksList;                           // unless there are any previously freed blocks
73   };
74
75   struct AtlasSlotDescriptor
76   {
77     SizeType mCount;                                                    // Reference count for this slot
78     SizeType mImageWidth;                                               // Width of image stored
79     SizeType mImageHeight;                                              // Height of image stored
80     AtlasId mAtlasId;                                                   // Image is stored in this Atlas
81     Dali::Vector< SizeType > mBlocksList;                               // List of blocks within atlas used for image
82   };
83
84   AtlasManager();
85
86   /**
87    * Create a new AtlasManager
88    */
89   static AtlasManagerPtr New();
90
91   virtual ~AtlasManager();
92
93   /**
94    * @copydoc: Toolkit::AtlasManager::CreateAtlas
95    */
96   AtlasId CreateAtlas( SizeType width,
97                        SizeType height,
98                        SizeType blockWidth,
99                        SizeType blockHeight,
100                        Pixel::Format pixelformat );
101
102   /**
103    * @copydoc Toolkit::AtlasManager::SetAddPolicy
104    */
105   void SetAddPolicy( Toolkit::AtlasManager::AddFailPolicy policy );
106
107   /**
108    * @copydoc Toolkit::AtlasManager::Add
109    */
110   void Add( const BufferImage& image,
111             Toolkit::AtlasManager::AtlasSlot& slot,
112             Toolkit::AtlasManager::AtlasId atlas );
113
114   /**
115    * @copydoc Toolkit::AtlasManager::GenerateMeshData
116    */
117   void GenerateMeshData( ImageId id,
118                          const Vector2& position,
119                          MeshData& mesh );
120
121   /**
122    * @copydoc Toolkit::AtlasManager::StitchMesh
123    */
124   void StitchMesh( MeshData& first,
125                    const MeshData& second,
126                    bool optimize );
127
128   /**
129    * @copydoc Toolkit::AtlasManager::StitchMesh
130    */
131   void StitchMesh( const MeshData& first,
132                    const MeshData& second,
133                    MeshData& out, bool optimize );
134
135   /**
136    * @copydoc Toolkit::AtlasManager::Remove
137    */
138   bool Remove( ImageId id );
139
140   /**
141    * @copydoc Toolkit::AtlasManager::GetAtlasContainer
142    */
143   Dali::Atlas GetAtlasContainer( AtlasId atlas ) const;
144
145   /**
146    * @copydoc Toolkit::AtlasManager::GetAtlas
147    */
148   AtlasId GetAtlas( ImageId id ) const;
149
150   /**
151    * @copydoc Toolkit::AtlasManager::SetAtlasSize
152    */
153   void SetAtlasSize( const Vector2& size,
154                      const Vector2& blockSize );
155
156   /**
157    * @copydoc Toolkit::AtlasManager::GetBlockSize
158    */
159   Vector2 GetBlockSize( AtlasId atlas );
160
161   /**
162    * @copydoc Toolkit::AtlasManager::GetFreeBlocks
163    */
164   SizeType GetFreeBlocks( AtlasId atlas ) const;
165
166   /*
167    * @copydoc Toolkit::AtlasManager::GetAtlasCount
168    */
169   SizeType GetAtlasCount() const;
170
171   /**
172    * @copydoc Toolkit::AtlasManager::GetPixelFormat
173    */
174   Pixel::Format GetPixelFormat( AtlasId atlas );
175
176 private:
177
178   std::vector< AtlasDescriptor > mAtlasList;        // List of atlases created
179   std::vector< AtlasSlotDescriptor > mImageList;  // List of bitmaps store in atlases
180
181   SizeType CheckAtlas( SizeType atlas,
182                        SizeType width,
183                        SizeType height,
184                        Pixel::Format pixelFormat,
185                        SizeType& blockArea,
186                        SizeType& totalBlocks );
187
188   void CreateMesh( SizeType atlas,
189                    SizeType imageWidth,
190                    SizeType imageHeight,
191                    const Vector2& position,
192                    SizeType widthInBlocks,
193                    SizeType heightInBlocks,
194                    Dali::MeshData& meshData,
195                    AtlasSlotDescriptor& desc );
196
197   void OptimizeVertices( const MeshData::VertexContainer& in,
198                          MeshData::FaceIndices& faces,
199                          MeshData::VertexContainer& out );
200
201   void UploadImage( const BufferImage& image,
202                     const AtlasSlotDescriptor& desc );
203
204   void PrintMeshData( const MeshData& meshData );
205
206   Vector2 mNewAtlasSize;
207   Vector2 mNewBlockSize;
208   Toolkit::AtlasManager::AddFailPolicy mAddFailPolicy;
209   PixelBuffer* mEdgeBuffer;
210   uint32_t mEdgeBufferSize;
211 };
212
213 } // namespace Internal
214
215 inline const Internal::AtlasManager& GetImplementation(const Toolkit::AtlasManager& manager)
216 {
217   DALI_ASSERT_ALWAYS( manager && "AtlasManager handle is empty" );
218
219   const BaseObject& handle = manager.GetBaseObject();
220
221   return static_cast<const Internal::AtlasManager&>(handle);
222 }
223
224 inline Internal::AtlasManager& GetImplementation(Toolkit::AtlasManager& manager)
225 {
226   DALI_ASSERT_ALWAYS( manager && "AtlasManager handle is empty" );
227
228   BaseObject& handle = manager.GetBaseObject();
229
230   return static_cast<Internal::AtlasManager&>(handle);
231 }
232
233 } // namespace Toolkit
234
235 } // namespace Dali
236
237
238  #endif // __DALI_TOOLKIT_ATLAS_MANAGER_IMPL_H__