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