Merge remote-tracking branch 'origin/tizen' into new_text
[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 void TizenPlatformAbstraction::GetClosestImageSize( const std::string& filename,
107                                                   const ImageAttributes& attributes,
108                                                   Vector2& closestSize )
109 {
110   closestSize = Vector2::ZERO;
111   ImageLoader::GetClosestImageSize(filename, attributes, closestSize );
112 }
113
114 void TizenPlatformAbstraction::GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
115                                                   const ImageAttributes& attributes,
116                                                   Vector2& closestSize )
117 {
118   closestSize = Vector2::ZERO;
119   ImageLoader::GetClosestImageSize(resourceBuffer, attributes, closestSize );
120 }
121
122
123 void TizenPlatformAbstraction::LoadResource(const Integration::ResourceRequest& request)
124 {
125   if (mResourceLoader)
126   {
127     mResourceLoader->LoadResource(request);
128   }
129 }
130
131 Integration::ResourcePointer TizenPlatformAbstraction::LoadResourceSynchronously(const Integration::ResourceType& resourceType, const std::string& resourcePath)
132 {
133   return ImageLoader::LoadResourceSynchronously( resourceType, resourcePath );
134 }
135
136
137 void TizenPlatformAbstraction::SaveResource(const Integration::ResourceRequest& request)
138 {
139   if (mResourceLoader)
140   {
141     if( request.GetType()->id == Integration::ResourceShader )
142     {
143 #ifdef SHADERBIN_CACHE_ENABLED
144       std::string path = mDataStoragePath;
145       path += request.GetPath();
146
147       Integration::ResourceRequest newRequest( request.GetId(), *request.GetType(), path, request.GetResource() );
148       mResourceLoader->SaveResource(newRequest);
149 #endif
150     }
151     else
152     {
153       mResourceLoader->SaveResource(request);
154     }
155   }
156 }
157
158 void TizenPlatformAbstraction::CancelLoad(Integration::ResourceId id, Integration::ResourceTypeId typeId)
159 {
160   if (mResourceLoader)
161   {
162     mResourceLoader->CancelLoad(id, typeId);
163   }
164 }
165
166 bool TizenPlatformAbstraction::IsLoading()
167 {
168   if (mResourceLoader)
169   {
170     return mResourceLoader->IsLoading();
171   }
172
173   return false;
174 }
175
176 void TizenPlatformAbstraction::GetResources(Integration::ResourceCache& cache)
177 {
178   if (mResourceLoader)
179   {
180     mResourceLoader->GetResources(cache);
181   }
182 }
183
184 void TizenPlatformAbstraction::SetDpi(unsigned int dpiHor, unsigned int dpiVer)
185 {
186   if (mResourceLoader)
187   {
188     mResourceLoader->SetDpi(dpiHor, dpiVer);
189   }
190 }
191
192 bool TizenPlatformAbstraction::LoadFile( const std::string& filename, std::vector< unsigned char >& buffer ) const
193 {
194   bool result = false;
195
196   if (mResourceLoader)
197   {
198     result = mResourceLoader->LoadFile(filename, buffer);
199   }
200
201   return result;
202 }
203
204 std::string TizenPlatformAbstraction::LoadFile( const std::string& filename )
205 {
206   std::string result;
207   if (mResourceLoader)
208   {
209     result = mResourceLoader->LoadFile(filename);
210   }
211
212   return result;
213 }
214
215 bool TizenPlatformAbstraction::SaveFile(const std::string& filename, std::vector< unsigned char >& buffer) const
216 {
217   bool result = false;
218
219   if (mResourceLoader)
220   {
221     result = mResourceLoader->SaveFile(filename, buffer);
222   }
223
224   return result;
225 }
226
227 void TizenPlatformAbstraction::JoinLoaderThreads()
228 {
229   delete mResourceLoader;
230   mResourceLoader = NULL;
231 }
232
233 Integration::DynamicsFactory* TizenPlatformAbstraction::GetDynamicsFactory()
234 {
235   if( NULL == mDynamicsFactory )
236   {
237     mDynamicsFactory = new DynamicsFactory;
238   }
239
240   return mDynamicsFactory;
241 }
242
243 bool TizenPlatformAbstraction::LoadShaderBinFile( const std::string& filename, std::vector< unsigned char >& buffer ) const
244 {
245   bool result = false;
246
247 #ifdef SHADERBIN_CACHE_ENABLED
248   std::string path;
249
250   if( mResourceLoader )
251   {
252     path = DALI_SHADERBIN_DIR;
253     path += filename;
254     result = mResourceLoader->LoadFile( path, buffer );
255   }
256
257   if( mResourceLoader && result == false )
258   {
259     path = mDataStoragePath;
260     path += filename;
261     result = mResourceLoader->LoadFile( path, buffer );
262   }
263 #endif
264
265   return result;
266 }
267
268 void TizenPlatformAbstraction::SetDataStoragePath( const std::string& path )
269 {
270   mDataStoragePath = path;
271 }
272
273 }  // namespace TizenPlatform
274
275 }  // namespace Dali