2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "tizen-platform-abstraction.h"
24 #include <dali/integration-api/debug.h>
25 #include <dali/integration-api/bitmap.h>
26 #include <dali/integration-api/resource-types.h>
29 #include "image-loaders/image-loader.h"
30 #include "portable/file-reader.h"
31 #include <adaptors/common/pixel-buffer-impl.h>
36 namespace TizenPlatform
39 TizenPlatformAbstraction::TizenPlatformAbstraction()
40 : mDataStoragePath( "" )
44 TizenPlatformAbstraction::~TizenPlatformAbstraction()
48 ImageDimensions TizenPlatformAbstraction::GetClosestImageSize( const std::string& filename,
50 FittingMode::Type fittingMode,
51 SamplingMode::Type samplingMode,
52 bool orientationCorrection )
54 return ImageLoader::GetClosestImageSize( filename, size, fittingMode, samplingMode, orientationCorrection );
57 ImageDimensions TizenPlatformAbstraction::GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
59 FittingMode::Type fittingMode,
60 SamplingMode::Type samplingMode,
61 bool orientationCorrection )
63 return ImageLoader::GetClosestImageSize( resourceBuffer, size, fittingMode, samplingMode, orientationCorrection );
66 Integration::ResourcePointer TizenPlatformAbstraction::LoadImageSynchronously(const Integration::BitmapResourceType& resource, const std::string& resourcePath)
68 return ImageLoader::LoadImageSynchronously( resource, resourcePath );
71 Integration::BitmapPtr TizenPlatformAbstraction::DecodeBuffer( const Integration::BitmapResourceType& resource, uint8_t * buffer, size_t size )
73 Integration::BitmapPtr resultBitmap;
74 Dali::Devel::PixelBuffer bitmap;
76 Dali::Internal::Platform::FileReader fileReader( buffer, size );
77 FILE * const fp = fileReader.GetFile();
80 bool result = ImageLoader::ConvertStreamToBitmap( resource, "", fp, bitmap );
81 if ( !result || !bitmap )
84 DALI_LOG_WARNING( "Unable to decode bitmap supplied as in-memory blob.\n" );
88 Integration::Bitmap::Profile profile{Integration::Bitmap::Profile::BITMAP_2D_PACKED_PIXELS};
90 // For backward compatibility the Bitmap must be created
91 auto retval = Integration::Bitmap::New(profile, Dali::ResourcePolicy::OWNED_DISCARD);
93 retval->GetPackedPixelsProfile()->ReserveBuffer(
94 bitmap.GetPixelFormat(),
101 auto& impl = Dali::GetImplementation(bitmap);
103 std::copy( impl.GetBuffer(), impl.GetBuffer()+impl.GetBufferSize(), retval->GetBuffer());
104 resultBitmap.Reset(retval);
111 bool TizenPlatformAbstraction::LoadShaderBinaryFile( const std::string& filename, Dali::Vector< unsigned char >& buffer ) const
115 #ifdef SHADERBIN_CACHE_ENABLED
118 // First check the system location where shaders are stored at install time:
119 path = DALI_SHADERBIN_DIR;
121 result = LoadFile( path, buffer );
123 // Fallback to the cache of shaders stored after previous runtime compilations:
124 // On desktop this looks in the current working directory that the app was launched from.
125 if( mResourceLoader && result == false )
127 path = mDataStoragePath;
129 result = LoadFile( path, buffer );
136 bool TizenPlatformAbstraction::SaveShaderBinaryFile( const std::string& filename, const unsigned char * buffer, unsigned int numBytes ) const
140 #ifdef SHADERBIN_CACHE_ENABLED
142 // Use the cache of shaders stored after previous runtime compilations:
143 // On desktop this looks in the current working directory that the app was launched from.
144 std::string path = mDataStoragePath;
146 result = SaveFile( path, buffer, numBytes );
153 void TizenPlatformAbstraction::SetDataStoragePath( const std::string& path )
155 mDataStoragePath = path;
158 TizenPlatformAbstraction* CreatePlatformAbstraction()
160 return new TizenPlatformAbstraction();
163 bool SaveFile( const std::string& filename, const unsigned char * buffer, unsigned int numBytes )
165 DALI_ASSERT_DEBUG( 0 != filename.length());
170 buf.open(filename.c_str(), std::ios::out | std::ios_base::trunc | std::ios::binary);
173 std::ostream stream(&buf);
175 // determine size of buffer
176 int length = static_cast<int>(numBytes);
178 // write contents of buffer to the file
179 stream.write(reinterpret_cast<const char*>(buffer), length);
190 } // namespace TizenPlatform