Merge remote-tracking branch 'origin/tizen' into devel/new_mesh
[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     Toolkit::AtlasManager::AtlasSize mSize;                             // size of atlas
64     Pixel::Format mPixelFormat;                                         // pixel format used by atlas
65     BufferImage mHorizontalStrip;                                       // Image used to pad upload
66     BufferImage mVerticalStrip;                                         // Image used to pad upload
67     BufferImage mFilledPixelImage;                                      // Image used by atlas for operations such as underline
68     PixelBuffer* mStripBuffer;                                          // Blank image buffer used to pad upload
69     Material mMaterial;                                                 // material used for atlas texture
70     Sampler mSampler;                                                   // sampler 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( const Toolkit::AtlasManager::AtlasSize& size, Pixel::Format pixelformat );
97
98   /**
99    * @copydoc Toolkit::AtlasManager::SetAddPolicy
100    */
101   void SetAddPolicy( Toolkit::AtlasManager::AddFailPolicy policy );
102
103   /**
104    * @copydoc Toolkit::AtlasManager::Add
105    */
106   void Add( const BufferImage& image,
107             Toolkit::AtlasManager::AtlasSlot& slot,
108             Toolkit::AtlasManager::AtlasId atlas );
109
110   /**
111    * @copydoc Toolkit::AtlasManager::GenerateMeshData
112    */
113   void GenerateMeshData( ImageId id,
114                          const Vector2& position,
115                          Toolkit::AtlasManager::Mesh2D& mesh );
116
117   /**
118    * @copydoc Toolkit::AtlasManager::StitchMesh
119    */
120   void StitchMesh( Toolkit::AtlasManager::Mesh2D& first,
121                    const Toolkit::AtlasManager::Mesh2D& second,
122                    bool optimize );
123
124   /**
125    * @copydoc Toolkit::AtlasManager::StitchMesh
126    */
127   void StitchMesh(  const Toolkit::AtlasManager::Mesh2D& first,
128                     const Toolkit::AtlasManager::Mesh2D& second,
129                     Toolkit::AtlasManager::Mesh2D& out,
130                     bool optimize );
131
132   /**
133    * @copydoc Toolkit::AtlasManager::Remove
134    */
135   bool Remove( ImageId id );
136
137   /**
138    * @copydoc Toolkit::AtlasManager::GetAtlasContainer
139    */
140   Dali::Atlas GetAtlasContainer( AtlasId atlas ) const;
141
142   /**
143    * @copydoc Toolkit::AtlasManager::GetAtlas
144    */
145   AtlasId GetAtlas( ImageId id ) const;
146
147   /**
148    * @copydoc Toolkit::AtlasManager::SetNewAtlasSize
149    */
150   void SetNewAtlasSize( const Toolkit::AtlasManager::AtlasSize& size );
151
152   /**
153    * @copydoc Toolkit::AtlasManager::GetAtlasSize
154    */
155   const Toolkit::AtlasManager::AtlasSize& GetAtlasSize( AtlasId atlas );
156
157   /**
158    * @copydoc Toolkit::AtlasManager::GetBlockSize
159    */
160   Vector2 GetBlockSize( AtlasId atlas );
161
162   /**
163    * @copydoc Toolkit::AtlasManager::GetFreeBlocks
164    */
165   SizeType GetFreeBlocks( AtlasId atlas ) const;
166
167   /*
168    * @copydoc Toolkit::AtlasManager::GetAtlasCount
169    */
170   SizeType GetAtlasCount() const;
171
172   /**
173    * @copydoc Toolkit::AtlasManager::GetPixelFormat
174    */
175   Pixel::Format GetPixelFormat( AtlasId atlas );
176
177   /**
178    * @copydoc Toolkit::AtlasManager::GetMetrics
179    */
180   void GetMetrics( Toolkit::AtlasManager::Metrics& metrics );
181
182   /**
183    * @copydoc Toolkit::AtlasManager::GetMaterial
184    */
185   Material GetMaterial( AtlasId atlas ) const;
186
187 /**
188    * @copydoc Toolkit::AtlasManager::GetSampler
189    */
190   Sampler GetSampler( AtlasId atlas ) const;
191
192 private:
193
194   std::vector< AtlasDescriptor > mAtlasList;        // List of atlases created
195   std::vector< AtlasSlotDescriptor > mImageList;  // List of bitmaps store in atlases
196
197   SizeType CheckAtlas( SizeType atlas,
198                        SizeType width,
199                        SizeType height,
200                        Pixel::Format pixelFormat,
201                        SizeType& blockArea,
202                        SizeType& totalBlocks );
203
204   void CreateMesh( SizeType atlas,
205                    SizeType imageWidth,
206                    SizeType imageHeight,
207                    const Vector2& position,
208                    SizeType widthInBlocks,
209                    SizeType heightInBlocks,
210                    Toolkit::AtlasManager::Mesh2D& mesh,
211                    AtlasSlotDescriptor& desc );
212
213   void OptimizeMesh( const Toolkit::AtlasManager::Mesh2D& in,
214                      Toolkit::AtlasManager::Mesh2D& out );
215
216   void UploadImage( const BufferImage& image,
217                     const AtlasSlotDescriptor& desc );
218
219   void PrintMeshData( const Toolkit::AtlasManager::Mesh2D& mesh );
220
221   Toolkit::AtlasManager::AtlasSize mNewAtlasSize;
222   Toolkit::AtlasManager::AddFailPolicy mAddFailPolicy;
223   uint32_t mFilledPixel;
224 };
225
226 } // namespace Internal
227
228 inline const Internal::AtlasManager& GetImplementation(const Toolkit::AtlasManager& manager)
229 {
230   DALI_ASSERT_ALWAYS( manager && "AtlasManager handle is empty" );
231
232   const BaseObject& handle = manager.GetBaseObject();
233
234   return static_cast<const Internal::AtlasManager&>(handle);
235 }
236
237 inline Internal::AtlasManager& GetImplementation(Toolkit::AtlasManager& manager)
238 {
239   DALI_ASSERT_ALWAYS( manager && "AtlasManager handle is empty" );
240
241   BaseObject& handle = manager.GetBaseObject();
242
243   return static_cast<Internal::AtlasManager&>(handle);
244 }
245
246 } // namespace Toolkit
247
248 } // namespace Dali
249
250
251  #endif // __DALI_TOOLKIT_ATLAS_MANAGER_IMPL_H__