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