Merge "(AutomatedTests) Synchronise TestSingletonService with Adaptor" into devel...
[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, public ConnectionTracker
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 mTotalBlocks;                                              // total number of blocks in atlas
72     SizeType mAvailableBlocks;                                          // number of blocks available in atlas
73     Dali::Vector< SizeType > mFreeBlocksList;                           // unless there are any previously freed blocks
74   };
75
76   struct AtlasSlotDescriptor
77   {
78     SizeType mCount;                                                    // Reference count for this slot
79     SizeType mImageWidth;                                               // Width of image stored
80     SizeType mImageHeight;                                              // Height of image stored
81     AtlasId mAtlasId;                                                   // Image is stored in this Atlas
82     Dali::Vector< SizeType > mBlocksList;                               // List of blocks within atlas used for image
83   };
84
85   AtlasManager();
86
87   /**
88    * Create a new AtlasManager
89    */
90   static AtlasManagerPtr New();
91
92   virtual ~AtlasManager();
93
94   /**
95    * @copydoc: Toolkit::AtlasManager::CreateAtlas
96    */
97   AtlasId CreateAtlas( const Toolkit::AtlasManager::AtlasSize& size, Pixel::Format pixelformat );
98
99   /**
100    * @copydoc Toolkit::AtlasManager::SetAddPolicy
101    */
102   void SetAddPolicy( Toolkit::AtlasManager::AddFailPolicy policy );
103
104   /**
105    * @copydoc Toolkit::AtlasManager::Add
106    */
107   void Add( const BufferImage& image,
108             Toolkit::AtlasManager::AtlasSlot& slot,
109             Toolkit::AtlasManager::AtlasId atlas );
110
111   /**
112    * @copydoc Toolkit::AtlasManager::GenerateMeshData
113    */
114   void GenerateMeshData( ImageId id,
115                          const Vector2& position,
116                          Toolkit::AtlasManager::Mesh2D& mesh,
117                          bool addReference );
118
119   /**
120    * @copydoc Toolkit::AtlasManager::StitchMesh
121    */
122   void StitchMesh( Toolkit::AtlasManager::Mesh2D& first,
123                    const Toolkit::AtlasManager::Mesh2D& second,
124                    bool optimize );
125
126   /**
127    * @copydoc Toolkit::AtlasManager::StitchMesh
128    */
129   void StitchMesh(  const Toolkit::AtlasManager::Mesh2D& first,
130                     const Toolkit::AtlasManager::Mesh2D& second,
131                     Toolkit::AtlasManager::Mesh2D& out,
132                     bool optimize );
133
134   /**
135    * @copydoc Toolkit::AtlasManager::Remove
136    */
137   bool Remove( ImageId id );
138
139   /**
140    * @copydoc Toolkit::AtlasManager::GetAtlasContainer
141    */
142   Dali::Atlas GetAtlasContainer( AtlasId atlas ) const;
143
144   /**
145    * @copydoc Toolkit::AtlasManager::GetAtlas
146    */
147   AtlasId GetAtlas( ImageId id ) const;
148
149   /**
150    * @copydoc Toolkit::AtlasManager::SetNewAtlasSize
151    */
152   void SetNewAtlasSize( const Toolkit::AtlasManager::AtlasSize& size );
153
154   /**
155    * @copydoc Toolkit::AtlasManager::GetAtlasSize
156    */
157   const Toolkit::AtlasManager::AtlasSize& GetAtlasSize( AtlasId atlas );
158
159   /**
160    * @copydoc Toolkit::AtlasManager::GetBlockSize
161    */
162   Vector2 GetBlockSize( AtlasId atlas );
163
164   /**
165    * @copydoc Toolkit::AtlasManager::GetFreeBlocks
166    */
167   SizeType GetFreeBlocks( AtlasId atlas ) const;
168
169   /*
170    * @copydoc Toolkit::AtlasManager::GetAtlasCount
171    */
172   SizeType GetAtlasCount() const;
173
174   /**
175    * @copydoc Toolkit::AtlasManager::GetPixelFormat
176    */
177   Pixel::Format GetPixelFormat( AtlasId atlas );
178
179   /**
180    * @copydoc Toolkit::AtlasManager::GetMetrics
181    */
182   void GetMetrics( Toolkit::AtlasManager::Metrics& metrics );
183
184   /**
185    * @copydoc Toolkit::AtlasManager::GetMaterial
186    */
187   Material GetMaterial( AtlasId atlas ) const;
188
189 /**
190    * @copydoc Toolkit::AtlasManager::GetSampler
191    */
192   Sampler GetSampler( AtlasId atlas ) const;
193
194 private:
195
196   std::vector< AtlasDescriptor > mAtlasList;            // List of atlases created
197   std::vector< AtlasSlotDescriptor > mImageList;        // List of bitmaps store in atlases
198   Vector< PixelBuffer* > mUploadedImages;               // List of PixelBuffers passed to UploadedSignal
199   Toolkit::AtlasManager::AtlasSize mNewAtlasSize;       // Atlas size to use in next creation
200   Toolkit::AtlasManager::AddFailPolicy mAddFailPolicy;  // Policy for faling to add an Image
201   SizeType mFilledPixel;                                // 32Bit pixel image for underlining
202
203   SizeType CheckAtlas( SizeType atlas,
204                        SizeType width,
205                        SizeType height,
206                        Pixel::Format pixelFormat,
207                        SizeType& blockArea );
208
209   void CreateMesh( SizeType atlas,
210                    SizeType imageWidth,
211                    SizeType imageHeight,
212                    const Vector2& position,
213                    SizeType widthInBlocks,
214                    SizeType heightInBlocks,
215                    Toolkit::AtlasManager::Mesh2D& mesh,
216                    AtlasSlotDescriptor& desc );
217
218   void OptimizeMesh( const Toolkit::AtlasManager::Mesh2D& in,
219                      Toolkit::AtlasManager::Mesh2D& out );
220
221   void UploadImage( const BufferImage& image,
222                     const AtlasSlotDescriptor& desc );
223
224   void PrintMeshData( const Toolkit::AtlasManager::Mesh2D& mesh );
225
226   void OnUpload( Image image );
227
228   Shader mShaderL8;
229   Shader mShaderRgba;
230
231 };
232
233 } // namespace Internal
234
235 inline const Internal::AtlasManager& GetImplementation(const Toolkit::AtlasManager& manager)
236 {
237   DALI_ASSERT_ALWAYS( manager && "AtlasManager handle is empty" );
238
239   const BaseObject& handle = manager.GetBaseObject();
240
241   return static_cast<const Internal::AtlasManager&>(handle);
242 }
243
244 inline Internal::AtlasManager& GetImplementation(Toolkit::AtlasManager& manager)
245 {
246   DALI_ASSERT_ALWAYS( manager && "AtlasManager handle is empty" );
247
248   BaseObject& handle = manager.GetBaseObject();
249
250   return static_cast<Internal::AtlasManager&>(handle);
251 }
252
253 } // namespace Toolkit
254
255 } // namespace Dali
256
257
258  #endif // __DALI_TOOLKIT_ATLAS_MANAGER_IMPL_H__