0ca22e4ddcf6edd75bc786c3db2568c1f240b27a
[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 int TizenPlatformAbstraction::GetDefaultFontSize() const
58 {
59   int fontSize( -1 );
60
61 #ifndef DALI_PROFILE_UBUNTU
62   vconf_get_int( VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, &fontSize );
63 #endif // DALI_PROFILE_UBUNTU
64
65   return fontSize;
66 }
67
68 ImageDimensions TizenPlatformAbstraction::GetClosestImageSize( const std::string& filename,
69                                                                ImageDimensions size,
70                                                                FittingMode::Type fittingMode,
71                                                                SamplingMode::Type samplingMode,
72                                                                bool orientationCorrection )
73 {
74   return ImageLoader::GetClosestImageSize( filename, size, fittingMode, samplingMode, orientationCorrection );
75 }
76
77 ImageDimensions TizenPlatformAbstraction::GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
78                                                                ImageDimensions size,
79                                                                FittingMode::Type fittingMode,
80                                                                SamplingMode::Type samplingMode,
81                                                                bool orientationCorrection )
82 {
83   return ImageLoader::GetClosestImageSize( resourceBuffer, size, fittingMode, samplingMode, orientationCorrection );
84 }
85
86 Integration::ResourcePointer TizenPlatformAbstraction::LoadResourceSynchronously(const Integration::ResourceType& resourceType, const std::string& resourcePath)
87 {
88   return ImageLoader::LoadResourceSynchronously( resourceType, resourcePath );
89 }
90
91 Integration::BitmapPtr TizenPlatformAbstraction::DecodeBuffer( const Integration::ResourceType& resourceType, uint8_t * buffer, size_t size )
92 {
93   Integration::BitmapPtr bitmap = 0;
94
95   Dali::Internal::Platform::FileCloser fileCloser( buffer, size, "rb" );
96   FILE * const fp = fileCloser.GetFile();
97   if( fp )
98   {
99     bool result = ImageLoader::ConvertStreamToBitmap( resourceType, "", fp, StubbedResourceLoadingClient(), bitmap );
100     if ( !result || !bitmap )
101     {
102       bitmap.Reset();
103       DALI_LOG_WARNING( "Unable to decode bitmap supplied as in-memory blob.\n" );
104     }
105   }
106
107   return bitmap;
108 }
109
110 bool TizenPlatformAbstraction::LoadShaderBinaryFile( const std::string& filename, Dali::Vector< unsigned char >& buffer ) const
111 {
112   bool result = false;
113
114 #ifdef SHADERBIN_CACHE_ENABLED
115   std::string path;
116
117   // First check the system location where shaders are stored at install time:
118   if( mResourceLoader )
119   {
120     path = DALI_SHADERBIN_DIR;
121     path += filename;
122     result = mResourceLoader->LoadFile( path, buffer );
123   }
124
125   // Fallback to the cache of shaders stored after previous runtime compilations:
126   // On desktop this looks in the current working directory that the app was launched from.
127   if( mResourceLoader && result == false )
128   {
129     path = mDataStoragePath;
130     path += filename;
131     result = mResourceLoader->LoadFile( path, buffer );
132   }
133 #endif
134
135   return result;
136 }
137
138 bool TizenPlatformAbstraction::SaveShaderBinaryFile( const std::string& filename, const unsigned char * buffer, unsigned int numBytes ) const
139 {
140   bool result = false;
141
142 #ifdef SHADERBIN_CACHE_ENABLED
143
144     // Fallback to the cache of shaders stored after previous runtime compilations:
145     // On desktop this looks in the current working directory that the app was launched from.
146     if( mResourceLoader )
147     {
148       std::string path = mDataStoragePath;
149       path += filename;
150       result = mResourceLoader->SaveFile( path, buffer, numBytes );
151     }
152 #endif
153
154   return result;
155 }
156
157 void TizenPlatformAbstraction::SetDataStoragePath( const std::string& path )
158 {
159   mDataStoragePath = path;
160 }
161
162 }  // namespace TizenPlatform
163
164 }  // namespace Dali