(cleanup) Stop including std::vector directly inside dali
[platform/core/uifw/dali-core.git] / dali / integration-api / resource-types.h
1 #ifndef __DALI_INTEGRATION_RESOURCE_TYPES_H__
2 #define __DALI_INTEGRATION_RESOURCE_TYPES_H__
3
4 //
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // EXTERNAL INCLUDES
21 #include <stdint.h>
22
23 // INTERNAL INCLUDES
24 #include <dali/public-api/common/vector-wrapper.h>
25 #include <dali/public-api/images/image-attributes.h>
26 #include <dali/integration-api/resource-declarations.h>
27
28 namespace Dali
29 {
30
31 namespace Integration
32 {
33
34 // Resource Types
35
36 /**
37  * Extendable set of resource types
38  */
39 enum ResourceTypeId
40 {
41   ResourceBitmap,
42   ResourceNativeImage,
43   ResourceTargetImage,
44   ResourceShader,
45   ResourceModel,
46   ResourceMesh,
47   ResourceText
48 };
49
50 /**
51  * The abstract base class for resource types.
52  */
53 struct DALI_IMPORT_API ResourceType
54 {
55   /**
56    * Constructor.
57    * @param[in] typeId resource type id
58    */
59   ResourceType(ResourceTypeId typeId)
60   : id(typeId) {}
61
62   /**
63    * Destructor.
64    */
65   virtual ~ResourceType() {}
66
67   /**
68    * Create a copy of the resource type with the same attributes.
69    * @return pointer to the new ResourceType.
70    */
71   virtual ResourceType* Clone() const = 0;
72
73   const ResourceTypeId id;
74
75 private:
76
77   // Undefined copy constructor.
78   ResourceType(const ResourceType& typePath);
79
80   // Undefined assignment operator.
81   ResourceType& operator=(const ResourceType& rhs);
82 };
83
84 /**
85  * BitmapResourceType describes a bitmap resource, which can be requested
86  * from ResourceLoader::LoadResource() or AllocateBitmapImage.
87  */
88 struct DALI_IMPORT_API BitmapResourceType : public ResourceType
89 {
90   /**
91    * Constructor.
92    * @param[in] attribs parameters for image loading request
93    */
94   BitmapResourceType(const ImageAttributes& attribs)
95   : ResourceType(ResourceBitmap),
96     imageAttributes(attribs) {}
97
98   /**
99    * Destructor.
100    */
101   virtual ~BitmapResourceType() {}
102
103   /**
104    * @copydoc ResourceType::Clone
105    */
106   virtual ResourceType* Clone() const
107   {
108     return new BitmapResourceType(imageAttributes);
109   }
110
111   /**
112    * Attributes are copied from the request.
113    */
114   ImageAttributes imageAttributes;
115
116 private:
117
118   // Undefined copy constructor.
119   BitmapResourceType(const BitmapResourceType& typePath);
120
121   // Undefined assignment operator.
122   BitmapResourceType& operator=(const BitmapResourceType& rhs);
123 };
124
125 /**
126  * NativeImageResourceType describes a native image resource, which can be injected
127  * through ResourceManager::AddNativeImage() or requested through ResourceLoader::LoadResource().
128  * If the adaptor does not support NativeImages, it can fall back to Bitmap type.
129  */
130 struct DALI_IMPORT_API NativeImageResourceType : public ResourceType
131 {
132   /**
133    * Constructor.
134    */
135   NativeImageResourceType()
136   : ResourceType(ResourceNativeImage) {}
137
138   /**
139    * Constructor.
140    * @param[in] attribs parameters for image loading request
141    */
142   NativeImageResourceType(const ImageAttributes& attribs)
143   : ResourceType(ResourceNativeImage),
144     imageAttributes(attribs) {}
145
146   /**
147    * Destructor.
148    */
149   virtual ~NativeImageResourceType() {}
150
151  /**
152   * @copydoc ResourceType::Clone
153   */
154   virtual ResourceType* Clone() const
155   {
156     return new NativeImageResourceType(imageAttributes);
157   }
158
159   /**
160    * Attributes are copied from the request (if supplied).
161    */
162   ImageAttributes imageAttributes;
163
164 private:
165
166   // Undefined copy constructor.
167   NativeImageResourceType(const NativeImageResourceType& typePath);
168
169   // Undefined assignment operator.
170   NativeImageResourceType& operator=(const NativeImageResourceType& rhs);
171 };
172
173 /**
174  * RenderTargetResourceType describes a bitmap resource, which can injected
175  * through ResourceManager::AddTargetImage()
176  */
177 struct DALI_IMPORT_API RenderTargetResourceType : public ResourceType
178 {
179   /**
180    * Constructor.
181    */
182   RenderTargetResourceType()
183   : ResourceType(ResourceTargetImage) {}
184
185   /**
186    * Constructor.
187    * @param[in] attribs parameters for image loading request
188    */
189   RenderTargetResourceType(const ImageAttributes& attribs)
190   : ResourceType(ResourceTargetImage),
191     imageAttributes(attribs) {}
192
193   /**
194    * Destructor.
195    */
196   virtual ~RenderTargetResourceType() {}
197
198   /**
199    * @copydoc ResourceType::Clone
200    */
201   virtual ResourceType* Clone() const
202   {
203     return new RenderTargetResourceType(imageAttributes);
204   }
205
206   /**
207    * Attributes are copied from the request.
208    */
209   ImageAttributes imageAttributes;
210
211 private:
212
213   // Undefined copy constructor.
214   RenderTargetResourceType(const RenderTargetResourceType& typePath);
215
216   // Undefined assignment operator.
217   RenderTargetResourceType& operator=(const RenderTargetResourceType& rhs);
218 };
219
220 /**
221  * ShaderResourceType describes a shader program resource, which can be requested
222  * from PlatformAbstraction::LoadResource()
223  */
224 struct DALI_IMPORT_API ShaderResourceType : public ResourceType
225 {
226   /**
227    * Constructor.
228    */
229   ShaderResourceType(size_t shaderHash, const std::string& vertexSource, const std::string& fragmentSource)
230   : ResourceType(ResourceShader),
231     hash(shaderHash),
232     vertexShader(vertexSource),
233     fragmentShader(fragmentSource)
234   {
235   }
236
237   /**
238    * Destructor.
239    */
240   virtual ~ShaderResourceType()
241   {
242   }
243
244   /**
245    * @copydoc ResourceType::Clone
246    */
247   virtual ResourceType* Clone() const
248   {
249     return new ShaderResourceType(hash, vertexShader, fragmentShader);
250   }
251
252 public: // Attributes
253   size_t            hash;              ///< Hash of the vertex/fragment sources
254   const std::string vertexShader;      ///< source code for vertex program
255   const std::string fragmentShader;    ///< source code for fragment program
256
257 private:
258
259   // Undefined copy constructor.
260   ShaderResourceType(const ShaderResourceType& typePath);
261
262   // Undefined assignment operator.
263   ShaderResourceType& operator=(const ShaderResourceType& rhs);
264 };
265
266 /**
267  * TextResourceType describes a font resource, which can be requested.
268  * from PlatformAbstraction::LoadResource()  No font atlas is created.
269  */
270 struct DALI_IMPORT_API TextResourceType : public ResourceType
271 {
272   /**
273    *  Text quality enum
274    */
275   enum TextQuality
276   {
277     TextQualityLow,       ///< Request lower quality text
278     TextQualityHigh       ///< Request higher quality text
279   };
280
281   /**
282    * Structure for requesting character to be loaded from file with atlas position
283    * for automatic texture upload
284    */
285   struct GlyphPosition
286   {
287     GlyphPosition(unsigned int chr, unsigned int xPos, unsigned int yPos)
288     : character(chr),
289       quality(0),
290       loaded(0),
291       xPosition(xPos),
292       yPosition(yPos)
293     {
294     }
295
296     /** \addtogroup GlyphPositionPackedWord
297      * We have 32 bits available for this data because of the alignment restrictions
298      * on the 32 bit words that follow so rather than using the minimum number of
299      * bits for each, we give "loaded" a whole 8 bits and push it to a byte-aligned
300      * address to make access possible via a plain byte load instead of a load,
301      * mask, shift sequence. The naive bitwidths before this modification are as follows:
302      *    character:21;
303      *    quality:1;
304      *    loaded:1;
305      *  @{
306      */
307     uint32_t character:21;       ///< character code (UTF-32), max value of 0x10ffff (21 bits)
308     uint32_t quality:3;          ///< Loaded quality 0 = low quality, 1 = high quality
309     uint32_t loaded:8;           ///< true if Loaded
310     /** @}*/
311
312     uint32_t xPosition;      ///< X Position in atlas
313     uint32_t yPosition;      ///< Y Position in atlas
314
315     /**
316      * Used by ResourceTypeCompare
317      */
318     friend bool operator==(const GlyphPosition& lhs, const GlyphPosition& rhs);
319   };
320
321   typedef std::vector< GlyphPosition > CharacterList;      ///< List of glyphs requested
322
323   enum GlyphCacheMode
324   {
325     GLYPH_CACHE_READ,    ///< Doesn't cache glyphs.
326     GLYPH_CACHE_WRITE,   ///< Caches glyphs.
327   };
328
329   /**
330    * Text resource type constructor
331    * @param [in] hash           The resourceHash for the FontAtlas and FontMetrics
332    * @param [in] style          The font style
333    * @param [in] characterList  The requested text as a vector or UTF-32 codes
334    * @param [in] textureAtlasId The resource ID of the texture atlas
335    * @param [in] quality        A boolean, set to true to request high quality glyph bitmaps.
336    * @param [in] maxGlyphSize   The size of the largest glyph in the font.
337    * @param [in] cache          Whether text glyph should be cached or not.
338    */
339   TextResourceType( const size_t hash,
340                     const std::string& style,
341                     const CharacterList& characterList,
342                     ResourceId textureAtlasId,
343                     TextQuality quality = TextQualityLow,
344                     Vector2 maxGlyphSize = Vector2::ONE,
345                     GlyphCacheMode cache = GLYPH_CACHE_READ )
346   : ResourceType(ResourceText),
347     mFontHash(hash),
348     mStyle(style),
349     mCharacterList(characterList),
350     mTextureAtlasId(textureAtlasId),
351     mQuality(quality),
352     mMaxGlyphSize(maxGlyphSize),
353     mCache( cache )
354   {
355   }
356
357   /**
358    * virtual destructor
359    */
360   virtual ~TextResourceType()
361   {
362   }
363
364   /**
365    * @copydoc ResourceType::Clone
366    */
367   virtual ResourceType* Clone() const
368   {
369     return new TextResourceType(mFontHash, mStyle, mCharacterList, mTextureAtlasId, mQuality, mMaxGlyphSize, mCache);
370   }
371
372   /**
373    * Font resource hash.
374    */
375   const size_t mFontHash;
376
377   /**
378    * Font style.
379    */
380   const std::string mStyle;
381
382   /**
383    * Displayed text (UTF-32 codes)
384    */
385
386   CharacterList mCharacterList; ///< List of characters
387
388   ResourceId mTextureAtlasId; ///< Resource ID of the texture atlas this request is for
389
390   TextQuality mQuality;  ///< Text quality setting
391
392   Vector2 mMaxGlyphSize;  ///< Max glyph size for font
393
394   GlyphCacheMode mCache; ///< Whether text glyphs should be cached.
395
396 private:
397
398   // Undefined copy constructor.
399   TextResourceType(const TextResourceType& typePath);
400
401   // Undefined copy constructor.
402   TextResourceType& operator=(const TextResourceType& rhs);
403 };
404
405 /**
406  * ModelResourceType describes a model resource, which can be requested
407  * from PlatformAbstraction::LoadResource()
408  */
409 struct DALI_IMPORT_API ModelResourceType : public ResourceType
410 {
411   /**
412    * Constructor.
413    */
414   ModelResourceType()
415     : ResourceType(ResourceModel)
416   {
417   }
418
419   /**
420    * Destructor.
421    */
422   virtual ~ModelResourceType()
423   {
424   }
425
426   /**
427    * @copydoc ResourceType::Clone
428    */
429   virtual ResourceType* Clone() const
430   {
431     return new ModelResourceType();
432   }
433
434 private:
435
436   // Undefined copy constructor.
437   ModelResourceType(const ModelResourceType& typePath);
438
439   // Undefined assignment operator.
440   ModelResourceType& operator=(const ModelResourceType& rhs);
441 };
442
443
444 /**
445  * MeshResourceType describes a mesh program resource, which can be created
446  * using ResourceManager::AllocateMesh.
447  */
448 struct DALI_IMPORT_API MeshResourceType : public ResourceType
449 {
450   /**
451    * Constructor.
452    */
453   MeshResourceType()
454   : ResourceType(ResourceMesh) {}
455
456   /**
457    * Destructor.
458    */
459   virtual ~MeshResourceType() {}
460
461   /**
462    * @copydoc ResourceType::Clone
463    */
464   virtual ResourceType* Clone() const
465   {
466     return new MeshResourceType();
467   }
468
469 private:
470
471   // Undefined copy constructor.
472   MeshResourceType(const MeshResourceType& typePath);
473
474   // Undefined assignment operator.
475   MeshResourceType& operator=(const MeshResourceType& rhs);
476 };
477
478 inline bool operator==(const TextResourceType::GlyphPosition& lhs, const TextResourceType::GlyphPosition& rhs)
479 {
480   return lhs.character == rhs.character && lhs.xPosition == rhs.xPosition && lhs.yPosition == rhs.yPosition && lhs.quality == rhs.quality;
481 }
482
483 } // namespace Integration
484
485 } // namespace Dali
486
487 #endif // __DALI_INTEGRATION_RESOURCE_TYPES_H__
488