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     Material mMaterial;                                                 // material used for atlas texture
69     SizeType mNextFreeBlock;                                            // next free block will be placed here ( actually +1 )
70     Dali::Vector< SizeType > mFreeBlocksList;                           // unless there are any previously freed blocks
71   };
72
73   struct AtlasSlotDescriptor
74   {
75     SizeType mCount;                                                    // Reference count for this slot
76     SizeType mImageWidth;                                               // Width of image stored
77     SizeType mImageHeight;                                              // Height of image stored
78     AtlasId mAtlasId;                                                   // Image is stored in this Atlas
79     Dali::Vector< SizeType > mBlocksList;                               // List of blocks within atlas used for image
80   };
81
82   AtlasManager();
83
84   /**
85    * Create a new AtlasManager
86    */
87   static AtlasManagerPtr New();
88
89   virtual ~AtlasManager();
90
91   /**
92    * @copydoc: Toolkit::AtlasManager::CreateAtlas
93    */
94   AtlasId CreateAtlas( SizeType width,
95                        SizeType height,
96                        SizeType blockWidth,
97                        SizeType blockHeight,
98                        Pixel::Format pixelformat );
99
100   /**
101    * @copydoc Toolkit::AtlasManager::SetAddPolicy
102    */
103   void SetAddPolicy( Toolkit::AtlasManager::AddFailPolicy policy );
104
105   /**
106    * @copydoc Toolkit::AtlasManager::Add
107    */
108   void Add( const BufferImage& image,
109             Toolkit::AtlasManager::AtlasSlot& slot,
110             Toolkit::AtlasManager::AtlasId atlas );
111
112   /**
113    * @copydoc Toolkit::AtlasManager::GenerateMeshData
114    */
115   void GenerateMeshData( ImageId id,
116                          const Vector2& position,
117                          MeshData& mesh );
118
119   /**
120    * @copydoc Toolkit::AtlasManager::StitchMesh
121    */
122   void StitchMesh( MeshData& first,
123                    const MeshData& second,
124                    bool optimize );
125
126   /**
127    * @copydoc Toolkit::AtlasManager::StitchMesh
128    */
129   void StitchMesh( const MeshData& first,
130                    const MeshData& second,
131                    MeshData& out, bool optimize );
132
133   /**
134    * @copydoc Toolkit::AtlasManager::Remove
135    */
136   bool Remove( ImageId id );
137
138   /**
139    * @copydoc Toolkit::AtlasManager::GetAtlasContainer
140    */
141   Dali::Atlas GetAtlasContainer( AtlasId atlas ) const;
142
143   /**
144    * @copydoc Toolkit::AtlasManager::GetAtlas
145    */
146   AtlasId GetAtlas( ImageId id ) const;
147
148   /**
149    * @copydoc Toolkit::AtlasManager::SetAtlasSize
150    */
151   void SetAtlasSize( const Vector2& size,
152                      const Vector2& blockSize );
153
154   /**
155    * @copydoc Toolkit::AtlasManager::GetBlockSize
156    */
157   Vector2 GetBlockSize( AtlasId atlas );
158
159   /**
160    * @copydoc Toolkit::AtlasManager::GetFreeBlocks
161    */
162   SizeType GetFreeBlocks( AtlasId atlas ) const;
163
164   /*
165    * @copydoc Toolkit::AtlasManager::GetAtlasCount
166    */
167   SizeType GetAtlasCount() const;
168
169   /**
170    * @copydoc Toolkit::AtlasManager::GetPixelFormat
171    */
172   Pixel::Format GetPixelFormat( AtlasId atlas );
173
174 private:
175
176   std::vector< AtlasDescriptor > mAtlasList;        // List of atlases created
177   std::vector< AtlasSlotDescriptor > mImageList;  // List of bitmaps store in atlases
178
179   SizeType CheckAtlas( SizeType atlas,
180                        SizeType width,
181                        SizeType height,
182                        Pixel::Format pixelFormat,
183                        SizeType& blockArea,
184                        SizeType& totalBlocks );
185
186   void CreateMesh( SizeType atlas,
187                    SizeType imageWidth,
188                    SizeType imageHeight,
189                    const Vector2& position,
190                    SizeType widthInBlocks,
191                    SizeType heightInBlocks,
192                    Dali::MeshData& meshData,
193                    AtlasSlotDescriptor& desc );
194
195   void OptimizeVertices( const MeshData::VertexContainer& in,
196                          MeshData::FaceIndices& faces,
197                          MeshData::VertexContainer& out );
198
199   void UploadImage( const BufferImage& image,
200                     const AtlasSlotDescriptor& desc );
201
202   void PrintMeshData( const MeshData& meshData );
203
204   Vector2 mNewAtlasSize;
205   Vector2 mNewBlockSize;
206   Toolkit::AtlasManager::AddFailPolicy mAddFailPolicy;
207 };
208
209 } // namespace Internal
210
211 inline const Internal::AtlasManager& GetImplementation(const Toolkit::AtlasManager& manager)
212 {
213   DALI_ASSERT_ALWAYS( manager && "AtlasManager handle is empty" );
214
215   const BaseObject& handle = manager.GetBaseObject();
216
217   return static_cast<const Internal::AtlasManager&>(handle);
218 }
219
220 inline Internal::AtlasManager& GetImplementation(Toolkit::AtlasManager& manager)
221 {
222   DALI_ASSERT_ALWAYS( manager && "AtlasManager handle is empty" );
223
224   BaseObject& handle = manager.GetBaseObject();
225
226   return static_cast<Internal::AtlasManager&>(handle);
227 }
228
229 } // namespace Toolkit
230
231 } // namespace Dali
232
233
234  #endif // __DALI_TOOLKIT_ATLAS_MANAGER_IMPL_H__