Merge "Added Application::New(..., stylesheet)" 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 "image-loaders/image-loader.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   mDynamicsFactory(NULL),
56   mDataStoragePath( "" )
57 {
58 }
59
60 TizenPlatformAbstraction::~TizenPlatformAbstraction()
61 {
62   delete mResourceLoader;
63   delete mDynamicsFactory;
64 }
65
66 void TizenPlatformAbstraction::GetTimeMicroseconds(unsigned int &seconds, unsigned int &microSeconds)
67 {
68   timespec time;
69   clock_gettime(CLOCK_MONOTONIC, &time);
70   seconds = time.tv_sec;
71   microSeconds = time.tv_nsec / NANOSECS_TO_MICROSECS;
72 }
73
74 void TizenPlatformAbstraction::Suspend()
75 {
76   if (mResourceLoader)
77   {
78     mResourceLoader->Pause();
79   }
80 }
81
82 void TizenPlatformAbstraction::Resume()
83 {
84   if (mResourceLoader)
85   {
86     mResourceLoader->Resume();
87   }
88 }
89
90 void TizenPlatformAbstraction::GetDefaultFontDescription( std::string& fontFamily, std::string& fontStyle ) const
91 {
92   FontConfigurationParser::Parse(FONT_CONFIGURATION_FILE, fontFamily, fontStyle);
93 }
94
95 int TizenPlatformAbstraction::GetDefaultFontSize() const
96 {
97   int fontSize( -1 );
98
99 #ifndef DALI_PROFILE_UBUNTU
100   vconf_get_int( VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, &fontSize );
101 #endif // DALI_PROFILE_UBUNTU
102
103   return fontSize;
104 }
105
106 ImageDimensions TizenPlatformAbstraction::GetClosestImageSize( const std::string& filename,
107                                                                ImageDimensions size,
108                                                                FittingMode::Type fittingMode,
109                                                                SamplingMode::Type samplingMode,
110                                                                bool orientationCorrection )
111 {
112   return ImageLoader::GetClosestImageSize( filename, size, fittingMode, samplingMode, orientationCorrection );
113 }
114
115 ImageDimensions TizenPlatformAbstraction::GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
116                                                                ImageDimensions size,
117                                                                FittingMode::Type fittingMode,
118                                                                SamplingMode::Type samplingMode,
119                                                                bool orientationCorrection )
120 {
121   return ImageLoader::GetClosestImageSize( resourceBuffer, size, fittingMode, samplingMode, orientationCorrection );
122 }
123
124 void TizenPlatformAbstraction::LoadResource(const Integration::ResourceRequest& request)
125 {
126   if (mResourceLoader)
127   {
128     mResourceLoader->LoadResource(request);
129   }
130 }
131
132 Integration::ResourcePointer TizenPlatformAbstraction::LoadResourceSynchronously(const Integration::ResourceType& resourceType, const std::string& resourcePath)
133 {
134   return ImageLoader::LoadResourceSynchronously( resourceType, resourcePath );
135 }
136
137
138 void TizenPlatformAbstraction::SaveResource(const Integration::ResourceRequest& request)
139 {
140   if (mResourceLoader)
141   {
142     if( request.GetType()->id == Integration::ResourceShader )
143     {
144 #ifdef SHADERBIN_CACHE_ENABLED
145       std::string path = mDataStoragePath;
146       path += request.GetPath();
147
148       Integration::ResourceRequest newRequest( request.GetId(), *request.GetType(), path, request.GetResource() );
149       mResourceLoader->SaveResource(newRequest);
150 #endif
151     }
152     else
153     {
154       mResourceLoader->SaveResource(request);
155     }
156   }
157 }
158
159 void TizenPlatformAbstraction::CancelLoad(Integration::ResourceId id, Integration::ResourceTypeId typeId)
160 {
161   if (mResourceLoader)
162   {
163     mResourceLoader->CancelLoad(id, typeId);
164   }
165 }
166
167 bool TizenPlatformAbstraction::IsLoading()
168 {
169   if (mResourceLoader)
170   {
171     return mResourceLoader->IsLoading();
172   }
173
174   return false;
175 }
176
177 void TizenPlatformAbstraction::GetResources(Integration::ResourceCache& cache)
178 {
179   if (mResourceLoader)
180   {
181     mResourceLoader->GetResources(cache);
182   }
183 }
184
185 void TizenPlatformAbstraction::SetDpi(unsigned int dpiHor, unsigned int dpiVer)
186 {
187   if (mResourceLoader)
188   {
189     mResourceLoader->SetDpi(dpiHor, dpiVer);
190   }
191 }
192
193 bool TizenPlatformAbstraction::LoadFile( const std::string& filename, std::vector< unsigned char >& buffer ) const
194 {
195   bool result = false;
196
197   if (mResourceLoader)
198   {
199     result = mResourceLoader->LoadFile(filename, buffer);
200   }
201
202   return result;
203 }
204
205 std::string TizenPlatformAbstraction::LoadFile( const std::string& filename )
206 {
207   std::string result;
208   if (mResourceLoader)
209   {
210     result = mResourceLoader->LoadFile(filename);
211   }
212
213   return result;
214 }
215
216 bool TizenPlatformAbstraction::SaveFile(const std::string& filename, std::vector< unsigned char >& buffer) const
217 {
218   bool result = false;
219
220   if (mResourceLoader)
221   {
222     result = mResourceLoader->SaveFile(filename, buffer);
223   }
224
225   return result;
226 }
227
228 void TizenPlatformAbstraction::JoinLoaderThreads()
229 {
230   delete mResourceLoader;
231   mResourceLoader = NULL;
232 }
233
234 Integration::DynamicsFactory* TizenPlatformAbstraction::GetDynamicsFactory()
235 {
236   if( NULL == mDynamicsFactory )
237   {
238     mDynamicsFactory = new DynamicsFactory;
239   }
240
241   return mDynamicsFactory;
242 }
243
244 bool TizenPlatformAbstraction::LoadShaderBinFile( const std::string& filename, std::vector< unsigned char >& buffer ) const
245 {
246   bool result = false;
247
248 #ifdef SHADERBIN_CACHE_ENABLED
249   std::string path;
250
251   if( mResourceLoader )
252   {
253     path = DALI_SHADERBIN_DIR;
254     path += filename;
255     result = mResourceLoader->LoadFile( path, buffer );
256   }
257
258   if( mResourceLoader && result == false )
259   {
260     path = mDataStoragePath;
261     path += filename;
262     result = mResourceLoader->LoadFile( path, buffer );
263   }
264 #endif
265
266   return result;
267 }
268
269 void TizenPlatformAbstraction::SetDataStoragePath( const std::string& path )
270 {
271   mDataStoragePath = path;
272 }
273
274 }  // namespace TizenPlatform
275
276 }  // namespace Dali