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