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