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