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