Merge branch 'devel/master(1.1.1)' into tizen
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / tizen / tizen-platform-abstraction.cpp
1 /*
2  * Copyright (c) 2015 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 // INTERNAL INCLUDES
30 #include "resource-loader/resource-loader.h"
31 #include "tizen-font-configuration-parser.h"
32 #include "image-loaders/image-loader.h"
33 #include "portable/file-closer.h"
34
35 namespace Dali
36 {
37
38 Integration::PlatformAbstraction* CreatePlatformAbstraction()
39 {
40   return new TizenPlatform::TizenPlatformAbstraction();
41 }
42
43
44 namespace TizenPlatform
45 {
46
47 namespace
48 {
49 const std::string FONT_CONFIGURATION_FILE( FONT_CONFIGURATION_FILE_PATH ); ///< Default font configuration file
50 const unsigned int NANOSECS_TO_MICROSECS( 1000 );                          ///< 1000 nanoseconds = 1 microsecond
51 }
52
53 TizenPlatformAbstraction::TizenPlatformAbstraction()
54 : mResourceLoader(new ResourceLoader),
55   mDataStoragePath( "" )
56 {
57 }
58
59 TizenPlatformAbstraction::~TizenPlatformAbstraction()
60 {
61   delete mResourceLoader;
62 }
63
64 void TizenPlatformAbstraction::GetTimeMicroseconds(unsigned int &seconds, unsigned int &microSeconds)
65 {
66   timespec time;
67   clock_gettime(CLOCK_MONOTONIC, &time);
68   seconds = time.tv_sec;
69   microSeconds = time.tv_nsec / NANOSECS_TO_MICROSECS;
70 }
71
72 void TizenPlatformAbstraction::Suspend()
73 {
74   if (mResourceLoader)
75   {
76     mResourceLoader->Pause();
77   }
78 }
79
80 void TizenPlatformAbstraction::Resume()
81 {
82   if (mResourceLoader)
83   {
84     mResourceLoader->Resume();
85   }
86 }
87
88 void TizenPlatformAbstraction::GetDefaultFontDescription( std::string& fontFamily, std::string& fontStyle ) const
89 {
90   FontConfigurationParser::Parse(FONT_CONFIGURATION_FILE, fontFamily, fontStyle);
91 }
92
93 int TizenPlatformAbstraction::GetDefaultFontSize() const
94 {
95   int fontSize( -1 );
96
97 #ifndef DALI_PROFILE_UBUNTU
98   vconf_get_int( VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, &fontSize );
99 #endif // DALI_PROFILE_UBUNTU
100
101   return fontSize;
102 }
103
104 ImageDimensions TizenPlatformAbstraction::GetClosestImageSize( const std::string& filename,
105                                                                ImageDimensions size,
106                                                                FittingMode::Type fittingMode,
107                                                                SamplingMode::Type samplingMode,
108                                                                bool orientationCorrection )
109 {
110   return ImageLoader::GetClosestImageSize( filename, size, fittingMode, samplingMode, orientationCorrection );
111 }
112
113 ImageDimensions TizenPlatformAbstraction::GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
114                                                                ImageDimensions size,
115                                                                FittingMode::Type fittingMode,
116                                                                SamplingMode::Type samplingMode,
117                                                                bool orientationCorrection )
118 {
119   return ImageLoader::GetClosestImageSize( resourceBuffer, size, fittingMode, samplingMode, orientationCorrection );
120 }
121
122 void TizenPlatformAbstraction::LoadResource(const Integration::ResourceRequest& request)
123 {
124   if (mResourceLoader)
125   {
126     mResourceLoader->LoadResource(request);
127   }
128 }
129
130 Integration::ResourcePointer TizenPlatformAbstraction::LoadResourceSynchronously(const Integration::ResourceType& resourceType, const std::string& resourcePath)
131 {
132   return ImageLoader::LoadResourceSynchronously( resourceType, resourcePath );
133 }
134
135 Integration::BitmapPtr TizenPlatformAbstraction::DecodeBuffer( const Integration::ResourceType& resourceType, uint8_t * buffer, size_t size )
136 {
137   Integration::BitmapPtr bitmap = 0;
138
139   Dali::Internal::Platform::FileCloser fileCloser( buffer, size, "rb" );
140   FILE * const fp = fileCloser.GetFile();
141   if( fp )
142   {
143     bool result = ImageLoader::ConvertStreamToBitmap( resourceType, "", fp, StubbedResourceLoadingClient(), bitmap );
144     if ( !result || !bitmap )
145     {
146       bitmap.Reset();
147       DALI_LOG_WARNING( "Unable to decode bitmap supplied as in-memory blob.\n" );
148     }
149   }
150
151   return bitmap;
152 }
153
154 void TizenPlatformAbstraction::CancelLoad(Integration::ResourceId id, Integration::ResourceTypeId typeId)
155 {
156   if (mResourceLoader)
157   {
158     mResourceLoader->CancelLoad(id, typeId);
159   }
160 }
161
162 void TizenPlatformAbstraction::GetResources(Integration::ResourceCache& cache)
163 {
164   if (mResourceLoader)
165   {
166     mResourceLoader->GetResources(cache);
167   }
168 }
169
170 bool TizenPlatformAbstraction::LoadFile( const std::string& filename, Dali::Vector< unsigned char >& buffer ) const
171 {
172   bool result = false;
173
174   if( mResourceLoader )
175   {
176     result = mResourceLoader->LoadFile( filename, buffer );
177   }
178
179   return result;
180 }
181
182 std::string TizenPlatformAbstraction::LoadFile( const std::string& filename )
183 {
184   std::string result;
185   if (mResourceLoader)
186   {
187     result = mResourceLoader->LoadFile(filename);
188   }
189
190   return result;
191 }
192
193 void TizenPlatformAbstraction::JoinLoaderThreads()
194 {
195   delete mResourceLoader;
196   mResourceLoader = NULL;
197 }
198
199 bool TizenPlatformAbstraction::LoadShaderBinaryFile( const std::string& filename, Dali::Vector< unsigned char >& buffer ) const
200 {
201   bool result = false;
202
203 #ifdef SHADERBIN_CACHE_ENABLED
204   std::string path;
205
206   // First check the system location where shaders are stored at install time:
207   if( mResourceLoader )
208   {
209     path = DALI_SHADERBIN_DIR;
210     path += filename;
211     result = mResourceLoader->LoadFile( path, buffer );
212   }
213
214   // Fallback to the cache of shaders stored after previous runtime compilations:
215   // On desktop this looks in the current working directory that the app was launched from.
216   if( mResourceLoader && result == false )
217   {
218     path = mDataStoragePath;
219     path += filename;
220     result = mResourceLoader->LoadFile( path, buffer );
221   }
222 #endif
223
224   return result;
225 }
226
227 bool TizenPlatformAbstraction::SaveShaderBinaryFile( const std::string& filename, const unsigned char * buffer, unsigned int numBytes ) const
228 {
229   bool result = false;
230
231 #ifdef SHADERBIN_CACHE_ENABLED
232
233     // Fallback to the cache of shaders stored after previous runtime compilations:
234     // On desktop this looks in the current working directory that the app was launched from.
235     if( mResourceLoader )
236     {
237       std::string path = mDataStoragePath;
238       path += filename;
239       result = mResourceLoader->SaveFile( path, buffer, numBytes );
240     }
241 #endif
242
243   return result;
244 }
245
246 void TizenPlatformAbstraction::SetDataStoragePath( const std::string& path )
247 {
248   mDataStoragePath = path;
249 }
250
251 }  // namespace TizenPlatform
252
253 }  // namespace Dali