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