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