1a8a876c8b13e54878a5c926ce430037ea973c04
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / tizen / tizen-platform-abstraction.cpp
1 /*
2  * Copyright (c) 2017 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 // CLASS HEADER
19 #include "tizen-platform-abstraction.h"
20
21 // EXTERNAL INCLUDES
22 #include <dirent.h>
23 #include <fstream>
24 #include <dali/integration-api/debug.h>
25 #include <dali/integration-api/bitmap.h>
26 #include <dali/integration-api/resource-types.h>
27
28 // INTERNAL INCLUDES
29 #include "image-loaders/image-loader.h"
30 #include "portable/file-reader.h"
31
32 namespace Dali
33 {
34
35 namespace TizenPlatform
36 {
37
38 TizenPlatformAbstraction::TizenPlatformAbstraction()
39 : mDataStoragePath( "" )
40 {
41 }
42
43 TizenPlatformAbstraction::~TizenPlatformAbstraction()
44 {
45 }
46
47 ImageDimensions TizenPlatformAbstraction::GetClosestImageSize( const std::string& filename,
48                                                                ImageDimensions size,
49                                                                FittingMode::Type fittingMode,
50                                                                SamplingMode::Type samplingMode,
51                                                                bool orientationCorrection )
52 {
53   return ImageLoader::GetClosestImageSize( filename, size, fittingMode, samplingMode, orientationCorrection );
54 }
55
56 ImageDimensions TizenPlatformAbstraction::GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
57                                                                ImageDimensions size,
58                                                                FittingMode::Type fittingMode,
59                                                                SamplingMode::Type samplingMode,
60                                                                bool orientationCorrection )
61 {
62   return ImageLoader::GetClosestImageSize( resourceBuffer, size, fittingMode, samplingMode, orientationCorrection );
63 }
64
65 Integration::ResourcePointer TizenPlatformAbstraction::LoadImageSynchronously(const Integration::BitmapResourceType& resource, const std::string& resourcePath)
66 {
67   return ImageLoader::LoadImageSynchronously( resource, resourcePath );
68 }
69
70 Integration::BitmapPtr TizenPlatformAbstraction::DecodeBuffer( const Integration::BitmapResourceType& resource, uint8_t * buffer, size_t size )
71 {
72   Integration::BitmapPtr bitmap = 0;
73
74   Dali::Internal::Platform::FileReader fileReader( buffer, size );
75   FILE * const fp = fileReader.GetFile();
76   if( fp )
77   {
78     bool result = ImageLoader::ConvertStreamToBitmap( resource, "", fp, bitmap );
79     if ( !result || !bitmap )
80     {
81       bitmap.Reset();
82       DALI_LOG_WARNING( "Unable to decode bitmap supplied as in-memory blob.\n" );
83     }
84   }
85
86   return bitmap;
87 }
88
89 bool TizenPlatformAbstraction::LoadShaderBinaryFile( const std::string& filename, Dali::Vector< unsigned char >& buffer ) const
90 {
91   bool result = false;
92
93 #ifdef SHADERBIN_CACHE_ENABLED
94   std::string path;
95
96   // First check the system location where shaders are stored at install time:
97   path = DALI_SHADERBIN_DIR;
98   path += filename;
99   result = LoadFile( path, buffer );
100
101   // Fallback to the cache of shaders stored after previous runtime compilations:
102   // On desktop this looks in the current working directory that the app was launched from.
103   if( mResourceLoader && result == false )
104   {
105     path = mDataStoragePath;
106     path += filename;
107     result = LoadFile( path, buffer );
108   }
109 #endif
110
111   return result;
112 }
113
114 bool TizenPlatformAbstraction::SaveShaderBinaryFile( const std::string& filename, const unsigned char * buffer, unsigned int numBytes ) const
115 {
116   bool result = false;
117
118 #ifdef SHADERBIN_CACHE_ENABLED
119
120   // Use the cache of shaders stored after previous runtime compilations:
121   // On desktop this looks in the current working directory that the app was launched from.
122   std::string path = mDataStoragePath;
123   path += filename;
124   result = SaveFile( path, buffer, numBytes );
125
126 #endif
127
128   return result;
129 }
130
131 void TizenPlatformAbstraction::SetDataStoragePath( const std::string& path )
132 {
133   mDataStoragePath = path;
134 }
135
136 TizenPlatformAbstraction* CreatePlatformAbstraction()
137 {
138   return new TizenPlatformAbstraction();
139 }
140
141 bool SaveFile( const std::string& filename, const unsigned char * buffer, unsigned int numBytes )
142 {
143   DALI_ASSERT_DEBUG( 0 != filename.length());
144
145   bool result = false;
146
147   std::filebuf buf;
148   buf.open(filename.c_str(), std::ios::out | std::ios_base::trunc | std::ios::binary);
149   if( buf.is_open() )
150   {
151     std::ostream stream(&buf);
152
153     // determine size of buffer
154     int length = static_cast<int>(numBytes);
155
156     // write contents of buffer to the file
157     stream.write(reinterpret_cast<const char*>(buffer), length);
158
159     if( !stream.bad() )
160     {
161       result = true;
162     }
163   }
164
165   return result;
166 }
167
168 }  // namespace TizenPlatform
169
170 }  // namespace Dali