(ImageLoader) Added synchronous method to get closest image size
[platform/core/uifw/dali-core.git] / dali / integration-api / platform-abstraction.h
1 #ifndef __DALI_INTEGRATION_PLATFORM_ABSTRACTION_H__
2 #define __DALI_INTEGRATION_PLATFORM_ABSTRACTION_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
23 // INTERNAL INCLUDES
24 #include <dali/integration-api/resource-cache.h>
25 #include <dali/integration-api/glyph-set.h>
26
27 namespace Dali
28 {
29
30 typedef std::vector<uint32_t> TextArray;
31
32 namespace Integration
33 {
34
35 class Bitmap;
36 class DynamicsFactory;
37
38 /**
39  * PlatformAbstraction is an abstract interface, used by Dali to access platform specific services.
40  * A concrete implementation must be created for each platform, and provided when creating the
41  * Dali::Integration::Core object.
42  */
43 class DALI_IMPORT_API PlatformAbstraction
44 {
45 public:
46
47   // Dali Lifecycle
48
49   /**
50    * Get the monotonic time since an unspecified reference point.
51    * This is used by Dali to calculate time intervals during animations; the interval is
52    * recalculated when Dali is resumed with Dali::Integration::Core::Resume().
53    * Multi-threading note: this method may be called from either the main or rendering thread.
54    * @param[out] seconds The time in seconds since the reference point.
55    * @param[out] microSeconds The remainder in microseconds.
56    */
57   virtual void GetTimeMicroseconds(unsigned int &seconds, unsigned int &microSeconds) = 0;
58
59   /**
60    * Tell the platform abstraction that Dali is ready to pause, such as when the
61    * application enters a background state.
62    * Allows background threads to pause their work until Resume() is called.
63    * This is a good time to release recreatable data such as memory caches
64    * to cooperate with other apps and reduce the chance of this one being
65    * force-killed in a low memory situation.
66    */
67   virtual void Suspend() {} ///!ToDo: Make pure virtual once dali-adaptor patch is in place = 0;
68
69   /**
70    * Tell the platform abstraction that Dali is resuming from a pause, such as
71    * when it has transitioned from a background state to a foreground one.
72    * It is time to wake up sleeping background threads and recreate memory
73    * caches and other temporary data.
74    */
75   virtual void Resume() {} ///!ToDo: Make pure virtual once dali-adaptor patch is in place = 0;
76
77   // Resource Loading
78
79   /**
80    * Determine the size of an image the resource loaders will provide when given the same
81    * image attributes.
82    * This is a synchronous request.
83    * This function is used to determine the size of an image before it has loaded.
84    * @param[in] filename name of the image.
85    * @param[in] attributes The attributes used to load the image
86    * @param[out] closestSize Size of the image that will be loaded.
87    */
88   virtual void GetClosestImageSize( const std::string& filename,
89                                     const ImageAttributes& attributes,
90                                     Vector2& closestSize ) = 0;
91
92   /**
93    * Determine the size of an image the resource loaders will provide when given the same
94    * image attributes.
95    * This is a synchronous request.
96    * This function is used to determine the size of an image before it has loaded.
97    * @param[in] resourceBuffer A pointer to an encoded image buffer
98    * @param[in] attributes The attributes used to load the image
99    * @param[out] closestSize Size of the image that will be loaded.
100    */
101   virtual void GetClosestImageSize( ResourcePointer resourceBuffer,
102                                     const ImageAttributes& attributes,
103                                     Vector2& closestSize ) = 0;
104
105   /**
106    * Request a resource from the native filesystem. This is an asynchronous request.
107    * After this method returns, FillResourceCache() will be called to retrieve the result(s) of the
108    * resource loading operation. Loading resources in separate worker thread is recommended.
109    * Multi-threading note: this method will be called from the main thread only i.e. not
110    * from within the Core::Render() method.
111    * @param[in] request A unique resource request. This is not guaranteed to survive after LoadResource
112    * returns; the loading process should take a copy.
113    */
114   virtual void LoadResource(const ResourceRequest& request) = 0;
115
116   /**
117    * Request that a resource be saved to the native filesystem.
118    * This is an asynchronous request.
119    */
120   virtual void SaveResource(const ResourceRequest& request) = 0;
121   /**
122    * Cancel an ongoing LoadResource() request.
123    * Multi-threading note: this method will be called from the main thread only i.e. not
124    * from within the Core::Render() method.
125    * @param[in] id The ID of the resource to cancel.
126    * @param[in] typeId The ID type of the resource to cancel.
127    */
128   virtual void CancelLoad(ResourceId id, ResourceTypeId typeId) = 0;
129
130   /**
131    * Query whether any asynchronous LoadResource() requests are ongoing.
132    * Multi-threading note: this method may be called from either the main or rendering thread.
133    * @return True if resources are being loaded.
134    */
135   virtual bool IsLoading() = 0;
136
137   /**
138    * Retrieve newly loaded resources.
139    * If no resources have finished loading, then this method returns immediately.
140    * Multi-threading note: this method will be called from the update thread, from within
141    * the UpdateManager::Update() method.
142    * @param[in] cache The resource cache to fill.
143    */
144   virtual void GetResources(ResourceCache& cache) = 0;
145
146   /**
147    * Waits for the asynchronous loader threads (if any) to finish.
148    * This will be only be called before Core destruction; no resource loading requests will be
149    * made following this method.
150    */
151   virtual void JoinLoaderThreads() = 0;
152
153   // Font Queries
154
155   /**
156    * Called by Dali to retrieve the default font family for the platform.
157    * Multi-threading note: this method will be called from the main thread only i.e. not
158    * from within the Core::Render() method.
159    * @return The default font family.
160    */
161   virtual std::string GetDefaultFontFamily() const = 0;
162
163   /**
164    * Called by Dali to retrieve the default font size for the platform in points.
165    * Multi-threading note: this method will be called from the main thread only i.e. not
166    * from within the Core::Render() method.
167    * @return The default font size.
168    */
169   virtual const float GetDefaultFontSize() const = 0;
170
171   /**
172    * Gets a font line height to match a given caps-height
173    * @param[in] fontFamily The name of the font's family
174    * @param[in] fontStyle  The style of the font
175    * @param[in] capsHeight The caps-height in pixels
176    */
177   virtual const PixelSize GetFontLineHeightFromCapsHeight(const std::string fontFamily, const std::string& fontStyle, const CapsHeight& capsHeight) const = 0;
178
179   /**
180    * Called by Font objects to synchronously query glyph data.
181    * @note Font's style goes inside the textRequest parameter
182    * @param[in] textRequest  Resource request. Includes font's style.
183    * @param[in] fontFamily   The name of the font's family
184    * @param[in] getBitmap    Whether to load bitmaps for the symbols as well
185    * @return A GlyphSet pointer with a list of the requested glyph metrics.
186    */
187   virtual Integration::GlyphSet* GetGlyphData ( const Integration::TextResourceType& textRequest,
188                                                 const std::string& fontFamily,
189                                                 bool getBitmap) const = 0;
190
191   /**
192    * Called by GlyphResourceManager to synchronously load glyph data.
193    * @param[in] textRequest     resource request
194    * @param[in] fontFamily      name of the font's family
195    * @return A GlyphSet pointer containing the requested glyph bitmaps.
196    */
197   virtual Integration::GlyphSet* GetCachedGlyphData( const TextResourceType& textRequest,
198                                                      const std::string& fontFamily ) const = 0;
199
200   /**
201    * Called by Font objects to synchronously query global font metrics.
202    * @param[in] fontFamily     The name of the font's family
203    * @param[in] fontStyle      The style of the font
204    * @param[out] globalMetrics font requested global metrics.
205    */
206   virtual void GetGlobalMetrics( const std::string& fontFamily,
207                                  const std::string& fontStyle,
208                                  Integration::GlobalMetrics& globalMetrics ) const = 0;
209
210   /**
211    * Sets horizontal and vertical pixels per inch value that is used by the display
212    * @param[in] dpiHorizontal horizontal dpi value
213    * @param[in] dpiVertical   vertical dpi value
214    */
215   virtual void SetDpi (unsigned int dpiHorizontal, unsigned int dpiVertical) = 0;
216
217   /**
218    * Returns the name of the font's family for displayed text.
219    * If possible, the returned font name should be able to display all characters in text.
220    * Otherwise returns closest match.
221    * @param[in] charsRequested displayed text
222    */
223   virtual std::string GetFontFamilyForChars(const TextArray& charsRequested) const = 0;
224
225   /**
226    * Checks whether all characters of text could be displayed with specified font family.
227    * @param[in] fontFamily     The name of the font's family
228    * @param[in] fontStyle      The style of the font
229    * @param[in] text     displayed text
230    */
231   virtual bool AllGlyphsSupported(const std::string& fontFamily, const std::string& fontStyle, const TextArray& text) const = 0;
232
233   /**
234    * Checks whether fontName is a valid font family name.
235    * closestMatch is always set to the best matching font or the system default font if no near match is detected.
236    * @param[in] fontFamily     The name of the font's family
237    * @param[in] fontStyle      The style of the font
238    * @param[out] isDefaultSystemFont Whether this font has been created with a default system font.
239    * @param[out] closestFontFamilyMatch Name of the font's family found based on the user input family's name
240    * @param[out] closestFontStyleMatch  Name of the font's style found based on the user input font's style
241    * @return     true if fontName is valid, false otherwise
242    */
243   virtual bool ValidateFontFamilyName(const std::string& fontFamily, const std::string& fontStyle, bool& isDefaultSystemFont, std::string& closestFontFamilyMatch, std::string& closestFontStyleMatch) const = 0;
244
245   /**
246    * The mode for GetFontList()
247    */
248   enum FontListMode
249   {
250     LIST_SYSTEM_FONTS,
251     LIST_APPLICATION_FONTS,
252     LIST_ALL_FONTS
253   };
254
255   /**
256    * Gets a list of fonts installed on the system.
257    * @param fontListMode which fonts to include in the list
258    * @return a list of font family names
259    */
260   virtual std::vector<std::string> GetFontList( FontListMode mode ) const = 0;
261
262   /**
263    * Load a file into a buffer
264    * @param[in] filename The filename to load
265    * @param[out] buffer  A buffer to receive the file.
266    *                     The buffer is implemeneted with a std::vector which is resized to fit the file.
267    * @result             true if the file is loaded.
268    */
269   virtual bool LoadFile( const std::string& filename, std::vector< unsigned char >& buffer ) const = 0;
270
271   /**
272    * Load a file into a buffer
273    * @param[in] filename The filename to save
274    * @param[out] buffer  A buffer containing some data
275    *                     The buffer is implemeneted with a std::vector. The size() member specifies the buffer length.
276    * @result             true if the file is saved.
277    */
278   virtual bool SaveFile(const std::string& filename, std::vector< unsigned char >& buffer) const = 0;
279
280   /**
281    * This method re-loads the device defaults that Dali uses. Adaptor will call this
282    * when devices settings change.
283    */
284   virtual void UpdateDefaultsFromDevice() = 0;
285
286   /**
287    * Get a pointer to the DynamicsFactory.
288    */
289   virtual DynamicsFactory* GetDynamicsFactory() = 0;
290
291   /**
292    * Read from the metrics cache into the global metrics parameter
293    * @param[in] fontFamily The name of the font family
294    * @param[in] fontStyle The name of the font style
295    * @param[out] globalMetrics The data store to write into
296    * @return \e true if the operation succeeded
297    */
298   virtual bool ReadGlobalMetricsFromCache( const std::string& fontFamily,
299                                            const std::string& fontStyle,
300                                            Integration::GlobalMetrics& globalMetrics ) = 0;
301
302   /**
303    * Write the global metrics parameter to the metrics cache
304    * @param[in] fontFamily The name of the font family
305    * @param[in] fontStyle The name of the font style
306    * @param[out] globalMetrics The data store to write
307    */
308   virtual void WriteGlobalMetricsToCache( const std::string& fontFamily,
309                                           const std::string& fontStyle,
310                                           const Integration::GlobalMetrics& globalMetrics ) = 0;
311
312   /**
313    * Read the metrics from the cache into the supplied vector
314    * @param[in] fontFamily The name of the font family
315    * @param[in] fontStyle The name of the font style
316    * @param[out] glyphMetricsContainer The vector of metrics to write
317    * @return true if the operation succeeded
318    */
319   virtual bool ReadMetricsFromCache( const std::string& fontFamily,
320                                      const std::string& fontStyle,
321                                      std::vector<Integration::GlyphMetrics>& glyphMetricsContainer ) = 0;
322
323   /**
324    * Write the metrics to the cache
325    * @param[in] fontFamily The name of the font family
326    * @param[in] fontStyle The name of the font style
327    * @param[in] glyphSet The set of metrics to write
328    */
329   virtual void WriteMetricsToCache( const std::string& fontFamily,
330                                     const std::string& fontStyle,
331                                     const Integration::GlyphSet& glyphSet ) = 0;
332
333 }; // class PlatformAbstraction
334
335 } // namespace Integration
336
337 } // namespace Dali
338
339 #endif // __DALI_INTEGRATION_PLATFORM_ABSTRACTION_H__