9d216f2a1041acabf978b23558e56dd3e3a759f0
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / slp / resource-loader / resource-loader.h
1 #ifndef __DALI_SLP_PLATFORM_RESOURCE_LOADER_H__
2 #define __DALI_SLP_PLATFORM_RESOURCE_LOADER_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 #include <dali/integration-api/platform-abstraction.h>
22 #include <dali/integration-api/glyph-set.h>
23 #include <dali/integration-api/resource-cache.h>
24 #include <dali/public-api/common/dali-vector.h>
25
26 #include <string>
27 #include <ft2build.h>
28 #include FT_FREETYPE_H
29 #include FT_GLYPH_H
30
31 namespace Dali
32 {
33
34 namespace Integration
35 {
36 namespace Log
37 {
38 class Filter;
39 }
40 }
41
42 namespace Platform
43 {
44 class FontController;
45 }
46
47 namespace SlpPlatform
48 {
49
50 /**
51  * Contains information about a successfully loaded resource
52  */
53 struct LoadedResource
54 {
55   /**
56    * Constructor
57    * @param[in] loadedId        The ID of the resource
58    * @param[in] loadedType      The resource type
59    * @param[in] loadedResource  A pointer to the loaded resource data
60    */
61   LoadedResource(Integration::ResourceId      loadedId,
62                  Integration::ResourceTypeId  loadedType,
63                  Integration::ResourcePointer loadedResource)
64   : id(loadedId),
65     type(loadedType),
66     resource(loadedResource)
67   {
68   }
69
70   /// Copy constructor
71   LoadedResource(const LoadedResource& loaded)
72   : id(loaded.id),
73     type(loaded.type),
74     resource(loaded.resource)
75   {
76   }
77
78   /// Assignment operator
79   LoadedResource& operator=(const LoadedResource& rhs)
80   {
81     if( this != &rhs )
82     {
83       id = rhs.id;
84       type = rhs.type;
85       resource = rhs.resource;
86     }
87     return *this;
88   }
89
90   Integration::ResourceId      id;         ///< Integer ID
91   Integration::ResourceTypeId  type;       ///< Type enum (bitmap, shader, ...)
92   Integration::ResourcePointer resource;   ///< Reference counting pointer to the loaded / decoded representation  of the resource.
93 };
94
95 /**
96  * Contains information about a successfully saved resource
97  */
98 struct SavedResource
99 {
100   /**
101    * Constructor
102    * @param[in] savedId   The ID of the resource
103    * @param[in] savedType The resource type
104    */
105   SavedResource(Integration::ResourceId     savedId,
106                 Integration::ResourceTypeId savedType)
107   : id(savedId),
108     type(savedType)
109   {
110   }
111
112   /// Copy constructor
113   SavedResource(const LoadedResource& loaded)
114   : id(loaded.id),
115     type(loaded.type)
116   {
117   }
118
119   /// Assignment operator
120   SavedResource& operator=(const SavedResource& rhs)
121   {
122     if( this != &rhs )
123     {
124       id = rhs.id;
125       type = rhs.type;
126     }
127     return *this;
128   }
129
130   Integration::ResourceId     id;
131   Integration::ResourceTypeId type;
132 };
133
134 /**
135  * Contains information about a failed resource load/save request
136  */
137 struct FailedResource
138 {
139   FailedResource(Integration::ResourceId resourceId, Integration::ResourceFailure failure):
140     id(resourceId),
141     failureType(failure)
142   {
143   }
144
145   /// Copy constructor
146   FailedResource(const FailedResource& failed)
147   : id(failed.id),
148     failureType(failed.failureType)
149   {
150   }
151
152   /// Assignment operator
153   FailedResource& operator=(const FailedResource& rhs)
154   {
155     if( this != &rhs )
156     {
157       id = rhs.id;
158       failureType = rhs.failureType;
159     }
160     return *this;
161   }
162
163   Integration::ResourceId      id;
164   Integration::ResourceFailure failureType;
165 };
166
167 /**
168  * This implements the resource loading part of the PlatformAbstraction API.
169  * The requests for a specific resource type are farmed-out to a resource
170  * requester for that type which handles them in their own dedicated loading
171  * threads.
172  */
173 class ResourceLoader
174 {
175 public:
176
177   /**
178    * Create a resource loader.
179    * There should exactly one of these objects per Dali Core.
180    */
181   ResourceLoader();
182
183   /**
184    * Non-virtual destructor.
185    * ResourceLoader is NOT intended as a base class.
186    */
187   ~ResourceLoader();
188
189   /**
190    * Pause processing of already-queued resource requests.
191    */
192   void Pause();
193
194   /**
195    * Continue processing resource requests.
196    */
197   void Resume();
198
199   /**
200    * Check if the ResourceLoader is terminating
201    * @return true if terminating else false
202    */
203   bool IsTerminating();
204
205   /**
206    * Add a partially loaded resource to the PartiallyLoadedResource queue
207    * @param[in] resource The resource's information and data
208    */
209   void AddPartiallyLoadedResource(LoadedResource& resource);
210
211   /**
212    * Add a completely loaded resource to the LoadedResource queue
213    * @param[in] resource The resource's information and data
214    */
215   void AddLoadedResource(LoadedResource& resource);
216
217   /**
218    * Add a successfully saved resource to the SavedResource queue
219    * @param[in] resource The resource's information
220    */
221   void AddSavedResource(SavedResource& resource);
222
223   /**
224    * Add information about a failed resource load to the FailedLoads queue
225    * @param[in] resource The failed resource's information
226    */
227   void AddFailedLoad(FailedResource& resource);
228
229   /**
230    * Add information about a failed resource save to the FailedSaves queue
231    * @param[in] resource The failed resource's information
232    */
233   void AddFailedSave(FailedResource& resource);
234
235
236   // From PlatformAbstraction
237
238   /**
239    * @copydoc PlatformAbstraction::LoadResource()
240    */
241   void LoadResource(const Integration::ResourceRequest& request);
242
243   /**
244    * @copydoc PlatformAbstraction::SaveResource()
245    */
246   void SaveResource(const Integration::ResourceRequest& request);
247
248   /**
249    * @copydoc PlatformAbstraction::CancelLoad()
250    */
251   void CancelLoad(Integration::ResourceId id, Integration::ResourceTypeId typeId);
252
253   /**
254    * @copydoc PlatformAbstraction::IsLoading()
255    */
256   bool IsLoading();
257
258   /**
259    * @copydoc PlatformAbstraction::GetResources()
260    */
261   void GetResources(Integration::ResourceCache& cache);
262
263   /**
264    * Called by Font objects to synchronously query glyph data.
265    * @param[in] textRequest     resource request
266    * @param[in] freeType        handle to the FreeType library
267    * @param[in] fontFamily      name of the font's family
268    * @param[in] getBitmap       whether to load bitmaps for the symbols as well
269    * @return A GlyphSet pointer with a list of the requested glyph metrics.
270    */
271   Integration::GlyphSet* GetGlyphData ( const Integration::TextResourceType& textRequest,
272                                         FT_Library freeType,
273                                         const std::string& fontFamily,
274                                         bool getBitmap);
275
276   /**
277    * Called by Font objects to synchronously load glyph data.
278    * @param[in] textRequest     resource request
279    * @param[in] fontFamily      name of the font's family
280    * @return A GlyphSet pointer containing the requested glyph bitmaps.
281    */
282   Integration::GlyphSet* GetCachedGlyphData( const Integration::TextResourceType& textRequest,
283                                              const std::string& fontFamily );
284
285   /**
286    * Called by Font objects to synchronously query global font metrics.
287    * @param[in] freeType       handle to the FreeType library
288    * @param[in] fontFamily     name of the font's family
289    * @param[in] fontStyle      name of the font's style
290    * @param[out] globalMetrics font requested global metrics.
291    */
292   void GetGlobalMetrics( FT_Library freeType,
293                          const std::string& fontFamily,
294                          const std::string& fontStyle,
295                          Integration::GlobalMetrics& globalMetrics );
296
297   /**
298    * @copydoc PlatformAbstraction::SetDpi()
299    */
300   void SetDpi(unsigned int dpiHor, unsigned int dpiVer);
301
302   /**
303    * @copydoc PlatformAbstraction::GetFontFamilyForChars()
304    */
305   const std::string& GetFontFamilyForChars(const Integration::TextArray& charsRequested);
306
307   /**
308    * @copydoc PlatformAbstraction::AllGlyphsSupported()
309    */
310   bool AllGlyphsSupported(const std::string& fontFamily, const std::string& fontStyle, const Integration::TextArray& charsRequested);
311
312   /**
313    * @copydoc PlatformAbstraction::ValidateFontFamilyName()
314    */
315   bool ValidateFontFamilyName( const std::string& fontFamily,
316                                const std::string& fontStyle,
317                                bool& isDefaultSystemFontFamily,
318                                bool& isDefaultSystemFontStyle,
319                                std::string& closestFontFamilyMatch,
320                                std::string& closestFontStyleMatch );
321
322   /**
323    * @copydoc SlpPlatformAbstraction::GetFontLineHeightFromCapsHeight
324    */
325   PixelSize GetFontLineHeightFromCapsHeight(const std::string& fontFamily, const std::string& fontStyle, CapsHeight capsHeight, FT_Library freeType);
326
327   /**
328    * @copydoc SlpPlatformAbstraction::GetFontList
329    */
330   void GetFontList( Dali::Integration::PlatformAbstraction::FontListMode mode, std::vector<std::string>& fontList );
331
332   /**
333    * @copydoc SlpPlatformAbstraction::LoadFile()
334    */
335   bool LoadFile( const std::string& filename, std::vector< unsigned char >& buffer ) const;
336
337   /**
338    * @copydoc SlpPlatformAbstraction::LoadFile()
339    */
340   std::string LoadFile(const std::string& filename) const;
341
342   /**
343    * @copydoc SlpPlatformAbstraction::SaveFile()
344    */
345   static bool SaveFile(const std::string& filename, std::vector< unsigned char >& buffer);
346
347   /**
348    * Sets the default font family that should be used by the font resources.
349    * @param[in] fontFamily The default font family.
350    * @param[in] fontStyle The default font style.
351    */
352   void SetDefaultFontFamily( const std::string& fontFamily, const std::string& fontStyle );
353
354   /**
355    * Retrieves the glyp's image representing the given character.
356    *
357    * @param[in] freeType Handle to the FreeType library.
358    * @param[in] fontFamily The font's family name.
359    * @param[in] fontStyle The font's style.
360    * @param[in] fontSize The font's size.
361    * @param[in] character The given character.
362    *
363    * @return The bitmap image.
364    */
365   Integration::BitmapPtr GetGlyphImage( FT_Library freeType, const std::string& fontFamily, const std::string& fontStyle, float fontSize, uint32_t character );
366
367 private:
368
369   /**
370    * Find the path of a platform-specific font resource.
371    * Multi-threading note: this method will be called from the main thread only i.e. not
372    * from within the Core::Render() method.
373    * @note   Italics and boldness are applied by transforming the outline.
374    * @param[in] fontFamily The font family e.g. Arial, Courier.
375    * @param[in] fontStyle The font's style.
376    * @return The path to a font resource; the closest available match for the family parameter.
377    */
378   const std::string& GetFontPath(const std::string& fontFamily, const std::string& fontStyle); // not const because of mutex
379
380 private:
381   struct ResourceLoaderImpl;
382   ResourceLoaderImpl* mImpl;
383
384   volatile int mTerminateThread;        ///< Set to <> 0 in destructor, signals threads to exit their controlling loops
385
386 };
387
388 } // namespace SlpPlatform
389
390 } // namespace Dali
391
392 #endif // __DALI_SLPPLATFORM_RESOURCE_LOADER_H_