Bitmap core patch 2 of 4 - Replace all uses of the Bitmap class with new simpler...
[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 #include <vector>
23
24 // INTERNAL INCLUDES
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 type identifiers used by ResourceType subclasses.
38  */
39 enum ResourceTypeId
40 {
41   ResourceImageData,   ///< A buffer of image data with dimension and type metadata.
42   ResourceNativeImage,
43   ResourceTargetImage,
44   ResourceShader,
45   ResourceModel,
46   ResourceMesh,
47   ResourceText,
48   ResourceTexture,     ///< Used to pass through a request for a GLES texture to be allocated on the render thread.
49   ResourceAppBitmap    ///< Used in Core to tag Bitmaps that belong to BitmapImages.
50 };
51
52 /**
53  * @brief A ResourceType-derived class is just a data bucket.
54  *
55  * Each ResourceType derived class carries a bundle of data specific to one or
56  * more resource request types that are unambiguously specified by the
57  * <code>id</code> member of this base class.
58  * Dispatch on that ResourceTypeId to know exactly which kind of resource
59  * request is associated with a particular instance.
60  */
61 struct DALI_IMPORT_API ResourceType
62 {
63   /**
64    * Constructor.
65    * @param[in] typeId resource type id
66    */
67   ResourceType(ResourceTypeId typeId)
68   : id(typeId) {}
69
70   virtual ~ResourceType() {}
71
72   /**
73    * Create a copy of the resource type with the same attributes.
74    * @return pointer to the new ResourceType.
75    */
76   virtual ResourceType* Clone() const = 0;
77
78   const ResourceTypeId id;
79
80 private:
81
82   // Undefined copy constructor.
83   ResourceType( const ResourceType& type );
84
85   // Undefined assignment operator.
86   ResourceType& operator=( const ResourceType& rhs );
87 };
88
89 /**
90  * ImageResourceType can be used to identify a a request as pertaining
91  * to images and carries some image attributes to modify the request.
92  * @todo Replace all uses of other image-related ResourceTypes with
93  * this one as they are duplicates.
94  */
95 struct DALI_IMPORT_API ImageResourceType : public ResourceType
96 {
97   /**
98    * Constructor.
99    * @param[in] typeId  One of the image ResourceTypeIds.
100    * @param[in] attribs parameters for image loading request
101    */
102   ImageResourceType(const ResourceTypeId typeId, const ImageAttributes& attribs)
103   : ResourceType(typeId),
104     imageAttributes(attribs)
105   {
106     DALI_ASSERT_DEBUG( typeId == ResourceTexture || typeId == ResourceImageData || typeId == ResourceNativeImage || typeId == ResourceTargetImage || typeId == ResourceAppBitmap );
107   }
108
109   /**
110    * @copydoc ResourceType::Clone
111    */
112   virtual ResourceType* Clone() const
113   {
114     return new ImageResourceType( id, imageAttributes );
115   }
116
117   /**
118    * Attributes are copied from the request.
119    */
120   ImageAttributes imageAttributes;
121 };
122
123 /**
124  * NativeImageResourceType describes a native image resource, which can be injected
125  * through ResourceManager::AddNativeImage() or requested through ResourceLoader::LoadResource().
126  * If the adaptor does not support NativeImages, it can fall back to Bitmap type.
127  */
128 struct DALI_IMPORT_API NativeImageResourceType : public ResourceType
129 {
130   /**
131    * Default constructor initialises the base class with the correct ResourceTypeId.
132    */
133   NativeImageResourceType()
134   : ResourceType(ResourceNativeImage) {}
135
136   /**
137    * Constructor.
138    * @param[in] attribs parameters for image loading request
139    */
140   NativeImageResourceType(const ImageAttributes& attribs)
141   : ResourceType(ResourceNativeImage),
142     imageAttributes(attribs) {}
143
144  /**
145   * @copydoc ResourceType::Clone
146   */
147   virtual ResourceType* Clone() const
148   {
149     return new NativeImageResourceType(imageAttributes);
150   }
151
152   /**
153    * Attributes are copied from the request (if supplied).
154    */
155   ImageAttributes imageAttributes;
156 };
157
158 /**
159  * RenderTargetResourceType describes a bitmap resource, which can injected
160  * through ResourceManager::AddTargetImage()
161  */
162 struct DALI_IMPORT_API RenderTargetResourceType : public ResourceType
163 {
164   /**
165    * Default constructor initialises the base class with the correct ResourceTypeId.
166    */
167   RenderTargetResourceType()
168   : ResourceType(ResourceTargetImage) {}
169
170   /**
171    * Constructor.
172    * @param[in] attribs parameters for image loading request
173    */
174   RenderTargetResourceType(const ImageAttributes& attribs)
175   : ResourceType(ResourceTargetImage),
176     imageAttributes(attribs) {}
177
178   /**
179    * @copydoc ResourceType::Clone
180    */
181   virtual ResourceType* Clone() const
182   {
183     return new RenderTargetResourceType(imageAttributes);
184   }
185
186   /**
187    * Attributes are copied from the request.
188    */
189   ImageAttributes imageAttributes;
190 };
191
192 /**
193  * ShaderResourceType describes a shader program resource, which can be requested
194  * from PlatformAbstraction::LoadResource()
195  */
196 struct DALI_IMPORT_API ShaderResourceType : public ResourceType
197 {
198   ShaderResourceType(size_t shaderHash, const std::string& vertexSource, const std::string& fragmentSource)
199   : ResourceType(ResourceShader),
200     hash(shaderHash),
201     vertexShader(vertexSource),
202     fragmentShader(fragmentSource)
203   {
204   }
205
206   /**
207    * @copydoc ResourceType::Clone
208    */
209   virtual ResourceType* Clone() const
210   {
211     return new ShaderResourceType(hash, vertexShader, fragmentShader);
212   }
213
214 public: // Attributes
215   size_t            hash;              ///< Hash of the vertex/fragment sources
216   const std::string vertexShader;      ///< source code for vertex program
217   const std::string fragmentShader;    ///< source code for fragment program
218 };
219
220 /**
221  * TextResourceType describes a font resource, which can be requested.
222  * from PlatformAbstraction::LoadResource()  No font atlas is created.
223  */
224 struct DALI_IMPORT_API TextResourceType : public ResourceType
225 {
226   /**
227    *  Text quality enum
228    */
229   enum TextQuality
230   {
231     TextQualityLow,       ///< Request lower quality text
232     TextQualityHigh       ///< Request higher quality text
233   };
234
235   /**
236    * Structure for requesting character to be loaded from file with atlas position
237    * for automatic texture upload
238    */
239   struct GlyphPosition
240   {
241     GlyphPosition(unsigned int chr, unsigned int xPos, unsigned int yPos)
242     : character(chr),
243       quality(0),
244       loaded(0),
245       xPosition(xPos),
246       yPosition(yPos)
247     {
248     }
249
250     /** \addtogroup GlyphPositionPackedWord
251      * We have 32 bits available for this data because of the alignment restrictions
252      * on the 32 bit words that follow so rather than using the minimum number of
253      * bits for each, we give "loaded" a whole 8 bits and push it to a byte-aligned
254      * address to make access possible via a plain byte load instead of a load,
255      * mask, shift sequence. The naive bitwidths before this modification are as follows:<code>
256      *    character:21;
257      *    quality:1;
258      *    loaded:1;</code>
259      *  @{
260      */
261     uint32_t character:21;       ///< character code (UTF-32), max value of 0x10ffff (21 bits)
262     uint32_t quality:3;          ///< Loaded quality 0 = low quality, 1 = high quality
263     uint32_t loaded:8;           ///< true if Loaded
264     /** @}*/
265
266     uint32_t xPosition;      ///< X Position in atlas
267     uint32_t yPosition;      ///< Y Position in atlas
268
269     /**
270      * Used by ResourceTypeCompare
271      */
272     friend bool operator==(const GlyphPosition& lhs, const GlyphPosition& rhs);
273   };
274
275   typedef std::vector< GlyphPosition > CharacterList;      ///< List of glyphs requested
276
277   enum GlyphCacheMode
278   {
279     GLYPH_CACHE_READ,    ///< Doesn't cache glyphs.
280     GLYPH_CACHE_WRITE,   ///< Caches glyphs.
281   };
282
283   /**
284    * Text resource type constructor
285    * @param [in] hash           The resourceHash for the FontAtlas and FontMetrics
286    * @param [in] style          The font style
287    * @param [in] characterList  The requested text as a vector or UTF-32 codes
288    * @param [in] textureAtlasId The resource ID of the texture atlas
289    * @param [in] quality        A boolean, set to true to request high quality glyph bitmaps.
290    * @param [in] maxGlyphSize   The size of the largest glyph in the font.
291    * @param [in] cache          Whether text glyph should be cached or not.
292    */
293   TextResourceType( const size_t hash,
294                     const std::string& style,
295                     const CharacterList& characterList,
296                     ResourceId textureAtlasId,
297                     TextQuality quality = TextQualityLow,
298                     Vector2 maxGlyphSize = Vector2::ONE,
299                     GlyphCacheMode cache = GLYPH_CACHE_READ )
300   : ResourceType(ResourceText),
301     mFontHash(hash),
302     mStyle(style),
303     mCharacterList(characterList),
304     mTextureAtlasId(textureAtlasId),
305     mQuality(quality),
306     mMaxGlyphSize(maxGlyphSize),
307     mCache( cache )
308   {
309   }
310
311   /**
312    * @copydoc ResourceType::Clone
313    */
314   virtual ResourceType* Clone() const
315   {
316     return new TextResourceType(mFontHash, mStyle, mCharacterList, mTextureAtlasId, mQuality, mMaxGlyphSize, mCache);
317   }
318
319   /**
320    * Font resource hash.
321    */
322   const size_t mFontHash;
323
324   /**
325    * Font style.
326    */
327   const std::string mStyle;
328
329   /**
330    * Displayed text (UTF-32 codes)
331    */
332
333   CharacterList mCharacterList; ///< List of characters
334
335   ResourceId mTextureAtlasId; ///< Resource ID of the texture atlas this request is for
336
337   TextQuality mQuality;  ///< Text quality setting
338
339   Vector2 mMaxGlyphSize;  ///< Max glyph size for font
340
341   GlyphCacheMode mCache; ///< Whether text glyphs should be cached.
342 };
343
344 /**
345  * ModelResourceType describes a model resource, which can be requested
346  * from PlatformAbstraction::LoadResource()
347  */
348 struct DALI_IMPORT_API ModelResourceType : public ResourceType
349 {
350   /**
351    * Default constructor initialises the base class with the correct ResourceTypeId.
352    */
353   ModelResourceType()
354     : ResourceType(ResourceModel)
355   {
356   }
357
358   /**
359    * @copydoc ResourceType::Clone
360    */
361   virtual ResourceType* Clone() const
362   {
363     return new ModelResourceType();
364   }
365 };
366
367
368 /**
369  * MeshResourceType describes a mesh program resource, which can be created
370  * using ResourceManager::AllocateMesh.
371  */
372 struct DALI_IMPORT_API MeshResourceType : public ResourceType
373 {
374   /**
375    * Default constructor initialises the base class with the correct ResourceTypeId.
376    */
377   MeshResourceType()
378   : ResourceType(ResourceMesh) {}
379
380   /**
381    * @copydoc ResourceType::Clone
382    */
383   virtual ResourceType* Clone() const
384   {
385     return new MeshResourceType();
386   }
387 };
388
389 inline bool operator==(const TextResourceType::GlyphPosition& lhs, const TextResourceType::GlyphPosition& rhs)
390 {
391   return lhs.character == rhs.character && lhs.xPosition == rhs.xPosition && lhs.yPosition == rhs.yPosition && lhs.quality == rhs.quality;
392 }
393
394 } // namespace Integration
395
396 } // namespace Dali
397
398 #endif // __DALI_INTEGRATION_RESOURCE_TYPES_H__
399