Merge "Change "SLP" to "Tizen"" into tizen
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / tizen / tizen-platform-abstraction.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include "tizen-platform-abstraction.h"
19
20 #ifndef DALI_PROFILE_UBUNTU
21 #include <vconf.h>
22 #endif // DALI_PROFILE_UBUNTU
23 #include <dirent.h>
24
25 #include <dali/integration-api/debug.h>
26 #include <dali/integration-api/bitmap.h>
27 #include <dali/integration-api/resource-types.h>
28
29 #include "resource-loader/resource-loader.h"
30 #include "dynamics/dynamics-factory.h"
31
32 #include "tizen-font-configuration-parser.h"
33 #include "data-cache/metrics-cache.h"
34 #include "image-loaders/image-loader.h"
35
36 namespace Dali
37 {
38
39 Integration::PlatformAbstraction* CreatePlatformAbstraction()
40 {
41   return new TizenPlatform::TizenPlatformAbstraction();
42 }
43
44
45 namespace TizenPlatform
46 {
47
48 namespace
49 {
50 const std::string FONT_CONFIGURATION_FILE( FONT_CONFIGURATION_FILE_PATH ); ///< Default font configuration file
51 const std::string DEFAULT_FONT_FAMILY( "HelveticaNeue" );                  ///< Default font family when unable to retrieve from font configuration file
52 const std::string DEFAULT_FONT_STYLE( "Book" );                            ///< Default font style when unable to retrieve from font configuration file
53 const std::string NULL_FONT_FAMILY_NAME( "" );
54 const unsigned int NANOSECS_TO_MICROSECS( 1000 );                          ///< 1000 nanoseconds = 1 microsecond
55
56 /// Settings to float point conversion table
57 const float FONT_SIZE_TABLE[5] =
58 {
59   8.0f,  ///< Small
60   10.0f, ///< Normal
61   15.0f, ///< Large
62   19.0f, ///< Huge
63   25.0f  ///< Giant
64 };
65 }
66
67 TizenPlatformAbstraction::TizenPlatformAbstraction()
68 : mResourceLoader(new ResourceLoader),
69   mDynamicsFactory(NULL),
70   mDataStoragePath( "" ),
71   mDefaultFontSize(FONT_SIZE_TABLE[1])
72 {
73   int error = FT_Init_FreeType(&mFreeTypeHandle);
74   DALI_ASSERT_ALWAYS( error == 0 && "Freetype initialization failed" );
75
76   UpdateDefaultsFromDevice();
77 }
78
79 TizenPlatformAbstraction::~TizenPlatformAbstraction()
80 {
81   delete mResourceLoader;
82   delete mDynamicsFactory;
83
84   if (mFreeTypeHandle)
85   {
86     FT_Done_FreeType(mFreeTypeHandle);
87   }
88 }
89
90 void TizenPlatformAbstraction::GetTimeMicroseconds(unsigned int &seconds, unsigned int &microSeconds)
91 {
92   timespec time;
93   clock_gettime(CLOCK_MONOTONIC, &time);
94   seconds = time.tv_sec;
95   microSeconds = time.tv_nsec / NANOSECS_TO_MICROSECS;
96 }
97
98 void TizenPlatformAbstraction::Suspend()
99 {
100   if (mResourceLoader)
101   {
102     mResourceLoader->Pause();
103   }
104 }
105
106 void TizenPlatformAbstraction::Resume()
107 {
108   if (mResourceLoader)
109   {
110     mResourceLoader->Resume();
111   }
112 }
113
114 const std::string& TizenPlatformAbstraction::GetDefaultFontFamily() const
115 {
116   // VCC TODO: return default font style as well.
117   return mDefaultFontFamily;
118 }
119
120 float TizenPlatformAbstraction::GetDefaultFontSize() const
121 {
122   return mDefaultFontSize;
123 }
124
125 PixelSize TizenPlatformAbstraction::GetFontLineHeightFromCapsHeight(const std::string& fontFamily, const std::string& fontStyle, CapsHeight capsHeight) const
126 {
127   PixelSize result(0);
128
129   if (mResourceLoader)
130   {
131     result = mResourceLoader->GetFontLineHeightFromCapsHeight(fontFamily, fontStyle, capsHeight, mFreeTypeHandle);
132   }
133
134   return result;
135 }
136
137 Integration::GlyphSet* TizenPlatformAbstraction::GetGlyphData ( const Dali::Integration::TextResourceType& textRequest,
138                                                               const std::string& fontFamily,
139                                                               bool getBitmap) const
140 {
141   if (mResourceLoader)
142   {
143     return mResourceLoader->GetGlyphData(textRequest,
144                                          mFreeTypeHandle,
145                                          fontFamily,
146                                          getBitmap);
147   }
148   return NULL;
149 }
150
151 Integration::GlyphSet* TizenPlatformAbstraction::GetCachedGlyphData( const Integration::TextResourceType& textRequest,
152                                                                    const std::string& fontFamily ) const
153 {
154   if (mResourceLoader)
155   {
156     return mResourceLoader->GetCachedGlyphData( textRequest, fontFamily );
157   }
158   return NULL;
159 }
160
161
162 void TizenPlatformAbstraction::GetGlobalMetrics( const std::string& fontFamily, const std::string& fontStyle, Integration::GlobalMetrics& globalMetrics ) const
163 {
164   if( mResourceLoader )
165   {
166     mResourceLoader->GetGlobalMetrics( mFreeTypeHandle,
167                                        fontFamily,
168                                        fontStyle,
169                                        globalMetrics );
170   }
171 }
172
173 void TizenPlatformAbstraction::GetClosestImageSize( const std::string& filename,
174                                                   const ImageAttributes& attributes,
175                                                   Vector2& closestSize )
176 {
177   closestSize = Vector2::ZERO;
178   ImageLoader::GetClosestImageSize(filename, attributes, closestSize );
179 }
180
181 void TizenPlatformAbstraction::GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
182                                                   const ImageAttributes& attributes,
183                                                   Vector2& closestSize )
184 {
185   closestSize = Vector2::ZERO;
186   ImageLoader::GetClosestImageSize(resourceBuffer, attributes, closestSize );
187 }
188
189
190 void TizenPlatformAbstraction::LoadResource(const Integration::ResourceRequest& request)
191 {
192   if (mResourceLoader)
193   {
194     mResourceLoader->LoadResource(request);
195   }
196 }
197
198 Integration::ResourcePointer TizenPlatformAbstraction::LoadResourceSynchronously(const Integration::ResourceType& resourceType, const std::string& resourcePath)
199 {
200   return ImageLoader::LoadResourceSynchronously( resourceType, resourcePath );
201 }
202
203
204 void TizenPlatformAbstraction::SaveResource(const Integration::ResourceRequest& request)
205 {
206   if (mResourceLoader)
207   {
208     if( request.GetType()->id == Integration::ResourceShader )
209     {
210 #ifdef SHADERBIN_CACHE_ENABLED
211       std::string path = mDataStoragePath;
212       path += request.GetPath();
213
214       Integration::ResourceRequest newRequest( request.GetId(), *request.GetType(), path, request.GetResource() );
215       mResourceLoader->SaveResource(newRequest);
216 #endif
217     }
218     else
219     {
220       mResourceLoader->SaveResource(request);
221     }
222   }
223 }
224
225 void TizenPlatformAbstraction::CancelLoad(Integration::ResourceId id, Integration::ResourceTypeId typeId)
226 {
227   if (mResourceLoader)
228   {
229     mResourceLoader->CancelLoad(id, typeId);
230   }
231 }
232
233 bool TizenPlatformAbstraction::IsLoading()
234 {
235   if (mResourceLoader)
236   {
237     return mResourceLoader->IsLoading();
238   }
239
240   return false;
241 }
242
243 void TizenPlatformAbstraction::GetResources(Integration::ResourceCache& cache)
244 {
245   if (mResourceLoader)
246   {
247     mResourceLoader->GetResources(cache);
248   }
249 }
250
251 void TizenPlatformAbstraction::SetDpi(unsigned int dpiHor, unsigned int dpiVer)
252 {
253   if (mResourceLoader)
254   {
255     mResourceLoader->SetDpi(dpiHor, dpiVer);
256   }
257 }
258
259 const std::string& TizenPlatformAbstraction::GetFontFamilyForChars(const Integration::TextArray& charsRequested) const
260 {
261   if( mResourceLoader )
262   {
263     return mResourceLoader->GetFontFamilyForChars(charsRequested);
264   }
265
266   return NULL_FONT_FAMILY_NAME;
267 }
268
269 bool TizenPlatformAbstraction::AllGlyphsSupported(const std::string &fontFamily, const std::string& fontStyle, const Integration::TextArray& charsRequested) const
270 {
271   bool ret = false;
272   if (mResourceLoader)
273   {
274     ret = mResourceLoader->AllGlyphsSupported(fontFamily, fontStyle, charsRequested);
275   }
276   return ret;
277 }
278
279 bool TizenPlatformAbstraction::ValidateFontFamilyName(const std::string& fontFamily, const std::string& fontStyle, bool& isDefaultSystemFont, std::string& closestMatch, std::string& closestStyleMatch) const
280 {
281   bool ret = false;
282   if( mResourceLoader )
283   {
284     // TODO: Consider retrieve both isDefaultSystemFontFamily and isDefaultSystemFontStyle.
285     bool isDefaultFamily = false;
286     bool isDefaultStyle = false;
287     ret = mResourceLoader->ValidateFontFamilyName( fontFamily, fontStyle, isDefaultFamily, isDefaultStyle, closestMatch, closestStyleMatch );
288     isDefaultSystemFont = isDefaultFamily && isDefaultStyle;
289   }
290   return ret;
291 }
292
293 void TizenPlatformAbstraction::GetFontList(  Dali::Integration::PlatformAbstraction::FontListMode mode, std::vector<std::string>& fontList ) const
294 {
295   if( mResourceLoader )
296   {
297     mResourceLoader->GetFontList( mode, fontList );
298   }
299 }
300
301 bool TizenPlatformAbstraction::LoadFile( const std::string& filename, std::vector< unsigned char >& buffer ) const
302 {
303   bool result = false;
304
305   if (mResourceLoader)
306   {
307     result = mResourceLoader->LoadFile(filename, buffer);
308   }
309
310   return result;
311 }
312
313 std::string TizenPlatformAbstraction::LoadFile( const std::string& filename )
314 {
315   std::string result;
316   if (mResourceLoader)
317   {
318     result = mResourceLoader->LoadFile(filename);
319   }
320
321   return result;
322 }
323
324 bool TizenPlatformAbstraction::SaveFile(const std::string& filename, std::vector< unsigned char >& buffer) const
325 {
326   bool result = false;
327
328   if (mResourceLoader)
329   {
330     result = mResourceLoader->SaveFile(filename, buffer);
331   }
332
333   return result;
334 }
335
336 void TizenPlatformAbstraction::JoinLoaderThreads()
337 {
338   delete mResourceLoader;
339   mResourceLoader = NULL;
340 }
341
342 void TizenPlatformAbstraction::UpdateDefaultsFromDevice()
343 {
344   // FontConfigurationParser::Parse sets the default font family and the default font style.
345   // If the isn't a configuration file or is invalid, or it doesn't have any tag with the default
346   // font family nor font style then default values set by the application are used.
347   mDefaultFontFamily = DEFAULT_FONT_FAMILY;
348   mDefaultFontStyle = DEFAULT_FONT_STYLE;
349
350   // The initialized values above are not used to parse the configuration file. These values
351   // are set just in case FontConfigurationParser::Parse is not able to set default values.
352   FontConfigurationParser::Parse(FONT_CONFIGURATION_FILE, mDefaultFontFamily, mDefaultFontStyle);
353
354   if ( mResourceLoader )
355   {
356     mResourceLoader->SetDefaultFontFamily( mDefaultFontFamily, mDefaultFontStyle );
357   }
358
359   int fontSize(0);
360 #ifndef DALI_PROFILE_UBUNTU
361   vconf_get_int( VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, &fontSize );
362 #endif // DALI_PROFILE_UBUNTU
363   mDefaultFontSize = FONT_SIZE_TABLE[fontSize];
364 }
365
366 Integration::DynamicsFactory* TizenPlatformAbstraction::GetDynamicsFactory()
367 {
368   if( NULL == mDynamicsFactory )
369   {
370     mDynamicsFactory = new DynamicsFactory;
371   }
372
373   return mDynamicsFactory;
374 }
375
376 bool TizenPlatformAbstraction::ReadGlobalMetricsFromCache( const std::string& fontFamily,
377                                                          const std::string& fontStyle,
378                                                          Integration::GlobalMetrics& globalMetrics )
379 {
380   return MetricsCache::ReadGlobal( fontFamily, fontStyle, globalMetrics );
381 }
382
383 void TizenPlatformAbstraction::WriteGlobalMetricsToCache( const std::string& fontFamily,
384                                                         const std::string& fontStyle,
385                                                         const Integration::GlobalMetrics& globalMetrics )
386 {
387   MetricsCache::WriteGlobal( fontFamily, fontStyle, globalMetrics);
388 }
389
390 bool TizenPlatformAbstraction::ReadMetricsFromCache( const std::string& fontFamily,
391                                                    const std::string& fontStyle,
392                                                    std::vector<Integration::GlyphMetrics>& glyphMetricsContainer )
393 {
394   return MetricsCache::Read( fontFamily, fontStyle, glyphMetricsContainer );
395 }
396
397 void TizenPlatformAbstraction::WriteMetricsToCache( const std::string& fontFamily,
398                                                   const std::string& fontStyle,
399                                                   const Integration::GlyphSet& glyphSet )
400 {
401   MetricsCache::Write( fontFamily, fontStyle, glyphSet );
402 }
403
404 void TizenPlatformAbstraction::GetFileNamesFromDirectory( const std::string& directoryName,
405                                                         std::vector<std::string>& fileNames )
406 {
407   dirent* de = NULL;
408   DIR* dp;
409   dp = opendir( directoryName.c_str() );
410   if( dp )
411   {
412     const std::string dot( "." );
413     const std::string dotDot( ".." );
414     while( true )
415     {
416       de = readdir( dp );
417       if( de == NULL )
418       {
419         break;
420       }
421       const std::string fileName( de->d_name );
422       if( ( fileName != dot ) &&
423           ( fileName != dotDot ) )
424       {
425         fileNames.push_back( fileName );
426       }
427     }
428     closedir( dp );
429   }
430 }
431
432 Integration::BitmapPtr TizenPlatformAbstraction::GetGlyphImage( const std::string& fontFamily, const std::string& fontStyle, const float fontSize, const uint32_t character ) const
433 {
434   Integration::BitmapPtr glyphImage;
435
436   if( mResourceLoader )
437   {
438     glyphImage = mResourceLoader->GetGlyphImage( mFreeTypeHandle, fontFamily, fontStyle, fontSize, character );
439   }
440
441   return glyphImage;
442 }
443
444 bool TizenPlatformAbstraction::LoadShaderBinFile( const std::string& filename, std::vector< unsigned char >& buffer ) const
445 {
446   bool result = false;
447
448 #ifdef SHADERBIN_CACHE_ENABLED
449   std::string path;
450
451   if( mResourceLoader )
452   {
453     path = DALI_SHADERBIN_DIR;
454     path += filename;
455     result = mResourceLoader->LoadFile( path, buffer );
456   }
457
458   if( mResourceLoader && result == false )
459   {
460     path = mDataStoragePath;
461     path += filename;
462     result = mResourceLoader->LoadFile( path, buffer );
463   }
464 #endif
465
466   return result;
467 }
468
469 void TizenPlatformAbstraction::SetDataStoragePath( const std::string& path )
470 {
471   mDataStoragePath = path;
472 }
473
474 }  // namespace TizenPlatform
475
476 }  // namespace Dali