/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
*/
+// CLASS HEADER
#include "tizen-platform-abstraction.h"
-#ifndef DALI_PROFILE_UBUNTU
-#include <vconf.h>
-#endif // DALI_PROFILE_UBUNTU
+// EXTERNAL INCLUDES
#include <dirent.h>
-
+#include <fstream>
#include <dali/integration-api/debug.h>
#include <dali/integration-api/bitmap.h>
#include <dali/integration-api/resource-types.h>
-#include "resource-loader/resource-loader.h"
-#include "dynamics/dynamics-factory.h"
-
-#include "tizen-font-configuration-parser.h"
-#include "data-cache/metrics-cache.h"
+// INTERNAL INCLUDES
#include "image-loaders/image-loader.h"
+#include "portable/file-reader.h"
+#include <adaptors/common/pixel-buffer-impl.h>
namespace Dali
{
-Integration::PlatformAbstraction* CreatePlatformAbstraction()
-{
- return new TizenPlatform::TizenPlatformAbstraction();
-}
-
-
namespace TizenPlatform
{
-namespace
-{
-const std::string FONT_CONFIGURATION_FILE( FONT_CONFIGURATION_FILE_PATH ); ///< Default font configuration file
-const std::string DEFAULT_FONT_FAMILY( "HelveticaNeue" ); ///< Default font family when unable to retrieve from font configuration file
-const std::string DEFAULT_FONT_STYLE( "Book" ); ///< Default font style when unable to retrieve from font configuration file
-const std::string NULL_FONT_FAMILY_NAME( "" );
-const unsigned int NANOSECS_TO_MICROSECS( 1000 ); ///< 1000 nanoseconds = 1 microsecond
-
-/// Settings to float point conversion table
-const float FONT_SIZE_TABLE[5] =
-{
- 8.0f, ///< Small
- 10.0f, ///< Normal
- 15.0f, ///< Large
- 19.0f, ///< Huge
- 25.0f ///< Giant
-};
-}
-
TizenPlatformAbstraction::TizenPlatformAbstraction()
-: mResourceLoader(new ResourceLoader),
- mDynamicsFactory(NULL),
- mDataStoragePath( "" ),
- mDefaultFontSize(FONT_SIZE_TABLE[1])
+: mDataStoragePath( "" )
{
- int error = FT_Init_FreeType(&mFreeTypeHandle);
- DALI_ASSERT_ALWAYS( error == 0 && "Freetype initialization failed" );
-
- UpdateDefaultsFromDevice();
}
TizenPlatformAbstraction::~TizenPlatformAbstraction()
{
- delete mResourceLoader;
- delete mDynamicsFactory;
-
- if (mFreeTypeHandle)
- {
- FT_Done_FreeType(mFreeTypeHandle);
- }
-}
-
-void TizenPlatformAbstraction::GetTimeMicroseconds(unsigned int &seconds, unsigned int µSeconds)
-{
- timespec time;
- clock_gettime(CLOCK_MONOTONIC, &time);
- seconds = time.tv_sec;
- microSeconds = time.tv_nsec / NANOSECS_TO_MICROSECS;
-}
-
-void TizenPlatformAbstraction::Suspend()
-{
- if (mResourceLoader)
- {
- mResourceLoader->Pause();
- }
-}
-
-void TizenPlatformAbstraction::Resume()
-{
- if (mResourceLoader)
- {
- mResourceLoader->Resume();
- }
}
-const std::string& TizenPlatformAbstraction::GetDefaultFontFamily() const
+ImageDimensions TizenPlatformAbstraction::GetClosestImageSize( const std::string& filename,
+ ImageDimensions size,
+ FittingMode::Type fittingMode,
+ SamplingMode::Type samplingMode,
+ bool orientationCorrection )
{
- // VCC TODO: return default font style as well.
- return mDefaultFontFamily;
+ return ImageLoader::GetClosestImageSize( filename, size, fittingMode, samplingMode, orientationCorrection );
}
-float TizenPlatformAbstraction::GetDefaultFontSize() const
+ImageDimensions TizenPlatformAbstraction::GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
+ ImageDimensions size,
+ FittingMode::Type fittingMode,
+ SamplingMode::Type samplingMode,
+ bool orientationCorrection )
{
- return mDefaultFontSize;
-}
-
-PixelSize TizenPlatformAbstraction::GetFontLineHeightFromCapsHeight(const std::string& fontFamily, const std::string& fontStyle, CapsHeight capsHeight) const
-{
- PixelSize result(0);
-
- if (mResourceLoader)
- {
- result = mResourceLoader->GetFontLineHeightFromCapsHeight(fontFamily, fontStyle, capsHeight, mFreeTypeHandle);
- }
-
- return result;
+ return ImageLoader::GetClosestImageSize( resourceBuffer, size, fittingMode, samplingMode, orientationCorrection );
}
-Integration::GlyphSet* TizenPlatformAbstraction::GetGlyphData ( const Dali::Integration::TextResourceType& textRequest,
- const std::string& fontFamily,
- bool getBitmap) const
+Integration::ResourcePointer TizenPlatformAbstraction::LoadImageSynchronously(const Integration::BitmapResourceType& resource, const std::string& resourcePath)
{
- if (mResourceLoader)
- {
- return mResourceLoader->GetGlyphData(textRequest,
- mFreeTypeHandle,
- fontFamily,
- getBitmap);
- }
- return NULL;
+ return ImageLoader::LoadImageSynchronously( resource, resourcePath );
}
-Integration::GlyphSet* TizenPlatformAbstraction::GetCachedGlyphData( const Integration::TextResourceType& textRequest,
- const std::string& fontFamily ) const
+Integration::BitmapPtr TizenPlatformAbstraction::DecodeBuffer( const Integration::BitmapResourceType& resource, uint8_t * buffer, size_t size )
{
- if (mResourceLoader)
- {
- return mResourceLoader->GetCachedGlyphData( textRequest, fontFamily );
- }
- return NULL;
-}
-
+ Integration::BitmapPtr resultBitmap;
+ Dali::Devel::PixelBuffer bitmap;
-void TizenPlatformAbstraction::GetGlobalMetrics( const std::string& fontFamily, const std::string& fontStyle, Integration::GlobalMetrics& globalMetrics ) const
-{
- if( mResourceLoader )
+ Dali::Internal::Platform::FileReader fileReader( buffer, size );
+ FILE * const fp = fileReader.GetFile();
+ if( fp )
{
- mResourceLoader->GetGlobalMetrics( mFreeTypeHandle,
- fontFamily,
- fontStyle,
- globalMetrics );
- }
-}
-
-void TizenPlatformAbstraction::GetClosestImageSize( const std::string& filename,
- const ImageAttributes& attributes,
- Vector2& closestSize )
-{
- closestSize = Vector2::ZERO;
- ImageLoader::GetClosestImageSize(filename, attributes, closestSize );
-}
-
-void TizenPlatformAbstraction::GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
- const ImageAttributes& attributes,
- Vector2& closestSize )
-{
- closestSize = Vector2::ZERO;
- ImageLoader::GetClosestImageSize(resourceBuffer, attributes, closestSize );
-}
-
-
-void TizenPlatformAbstraction::LoadResource(const Integration::ResourceRequest& request)
-{
- if (mResourceLoader)
- {
- mResourceLoader->LoadResource(request);
- }
-}
-
-Integration::ResourcePointer TizenPlatformAbstraction::LoadResourceSynchronously(const Integration::ResourceType& resourceType, const std::string& resourcePath)
-{
- return ImageLoader::LoadResourceSynchronously( resourceType, resourcePath );
-}
-
-
-void TizenPlatformAbstraction::SaveResource(const Integration::ResourceRequest& request)
-{
- if (mResourceLoader)
- {
- if( request.GetType()->id == Integration::ResourceShader )
+ bool result = ImageLoader::ConvertStreamToBitmap( resource, "", fp, bitmap );
+ if ( !result || !bitmap )
{
-#ifdef SHADERBIN_CACHE_ENABLED
- std::string path = mDataStoragePath;
- path += request.GetPath();
-
- Integration::ResourceRequest newRequest( request.GetId(), *request.GetType(), path, request.GetResource() );
- mResourceLoader->SaveResource(newRequest);
-#endif
+ bitmap.Reset();
+ DALI_LOG_WARNING( "Unable to decode bitmap supplied as in-memory blob.\n" );
}
else
{
- mResourceLoader->SaveResource(request);
- }
- }
-}
+ Integration::Bitmap::Profile profile{Integration::Bitmap::Profile::BITMAP_2D_PACKED_PIXELS};
-void TizenPlatformAbstraction::CancelLoad(Integration::ResourceId id, Integration::ResourceTypeId typeId)
-{
- if (mResourceLoader)
- {
- mResourceLoader->CancelLoad(id, typeId);
- }
-}
+ // For backward compatibility the Bitmap must be created
+ auto retval = Integration::Bitmap::New(profile, Dali::ResourcePolicy::OWNED_DISCARD);
-bool TizenPlatformAbstraction::IsLoading()
-{
- if (mResourceLoader)
- {
- return mResourceLoader->IsLoading();
- }
+ retval->GetPackedPixelsProfile()->ReserveBuffer(
+ bitmap.GetPixelFormat(),
+ bitmap.GetWidth(),
+ bitmap.GetHeight(),
+ bitmap.GetWidth(),
+ bitmap.GetHeight()
+ );
- return false;
-}
+ auto& impl = Dali::GetImplementation(bitmap);
-void TizenPlatformAbstraction::GetResources(Integration::ResourceCache& cache)
-{
- if (mResourceLoader)
- {
- mResourceLoader->GetResources(cache);
+ std::copy( impl.GetBuffer(), impl.GetBuffer()+impl.GetBufferSize(), retval->GetBuffer());
+ resultBitmap.Reset(retval);
+ }
}
-}
-void TizenPlatformAbstraction::SetDpi(unsigned int dpiHor, unsigned int dpiVer)
-{
- if (mResourceLoader)
- {
- mResourceLoader->SetDpi(dpiHor, dpiVer);
- }
+ return resultBitmap;
}
-const std::string& TizenPlatformAbstraction::GetFontFamilyForChars(const Integration::TextArray& charsRequested) const
+bool TizenPlatformAbstraction::LoadShaderBinaryFile( const std::string& filename, Dali::Vector< unsigned char >& buffer ) const
{
- if( mResourceLoader )
- {
- return mResourceLoader->GetFontFamilyForChars(charsRequested);
- }
+ bool result = false;
- return NULL_FONT_FAMILY_NAME;
-}
+#ifdef SHADERBIN_CACHE_ENABLED
+ std::string path;
-bool TizenPlatformAbstraction::AllGlyphsSupported(const std::string &fontFamily, const std::string& fontStyle, const Integration::TextArray& charsRequested) const
-{
- bool ret = false;
- if (mResourceLoader)
- {
- ret = mResourceLoader->AllGlyphsSupported(fontFamily, fontStyle, charsRequested);
- }
- return ret;
-}
+ // First check the system location where shaders are stored at install time:
+ path = DALI_SHADERBIN_DIR;
+ path += filename;
+ result = LoadFile( path, buffer );
-bool TizenPlatformAbstraction::ValidateFontFamilyName(const std::string& fontFamily, const std::string& fontStyle, bool& isDefaultSystemFont, std::string& closestMatch, std::string& closestStyleMatch) const
-{
- bool ret = false;
- if( mResourceLoader )
+ // Fallback to the cache of shaders stored after previous runtime compilations:
+ // On desktop this looks in the current working directory that the app was launched from.
+ if( mResourceLoader && result == false )
{
- // TODO: Consider retrieve both isDefaultSystemFontFamily and isDefaultSystemFontStyle.
- bool isDefaultFamily = false;
- bool isDefaultStyle = false;
- ret = mResourceLoader->ValidateFontFamilyName( fontFamily, fontStyle, isDefaultFamily, isDefaultStyle, closestMatch, closestStyleMatch );
- isDefaultSystemFont = isDefaultFamily && isDefaultStyle;
+ path = mDataStoragePath;
+ path += filename;
+ result = LoadFile( path, buffer );
}
- return ret;
-}
+#endif
-void TizenPlatformAbstraction::GetFontList( Dali::Integration::PlatformAbstraction::FontListMode mode, std::vector<std::string>& fontList ) const
-{
- if( mResourceLoader )
- {
- mResourceLoader->GetFontList( mode, fontList );
- }
+ return result;
}
-bool TizenPlatformAbstraction::LoadFile( const std::string& filename, std::vector< unsigned char >& buffer ) const
+bool TizenPlatformAbstraction::SaveShaderBinaryFile( const std::string& filename, const unsigned char * buffer, unsigned int numBytes ) const
{
bool result = false;
- if (mResourceLoader)
- {
- result = mResourceLoader->LoadFile(filename, buffer);
- }
+#ifdef SHADERBIN_CACHE_ENABLED
- return result;
-}
+ // Use the cache of shaders stored after previous runtime compilations:
+ // On desktop this looks in the current working directory that the app was launched from.
+ std::string path = mDataStoragePath;
+ path += filename;
+ result = SaveFile( path, buffer, numBytes );
-std::string TizenPlatformAbstraction::LoadFile( const std::string& filename )
-{
- std::string result;
- if (mResourceLoader)
- {
- result = mResourceLoader->LoadFile(filename);
- }
+#endif
return result;
}
-bool TizenPlatformAbstraction::SaveFile(const std::string& filename, std::vector< unsigned char >& buffer) const
+void TizenPlatformAbstraction::SetDataStoragePath( const std::string& path )
{
- bool result = false;
-
- if (mResourceLoader)
- {
- result = mResourceLoader->SaveFile(filename, buffer);
- }
-
- return result;
+ mDataStoragePath = path;
}
-void TizenPlatformAbstraction::JoinLoaderThreads()
+TizenPlatformAbstraction* CreatePlatformAbstraction()
{
- delete mResourceLoader;
- mResourceLoader = NULL;
+ return new TizenPlatformAbstraction();
}
-void TizenPlatformAbstraction::UpdateDefaultsFromDevice()
+bool SaveFile( const std::string& filename, const unsigned char * buffer, unsigned int numBytes )
{
- // FontConfigurationParser::Parse sets the default font family and the default font style.
- // If the isn't a configuration file or is invalid, or it doesn't have any tag with the default
- // font family nor font style then default values set by the application are used.
- mDefaultFontFamily = DEFAULT_FONT_FAMILY;
- mDefaultFontStyle = DEFAULT_FONT_STYLE;
-
- // The initialized values above are not used to parse the configuration file. These values
- // are set just in case FontConfigurationParser::Parse is not able to set default values.
- FontConfigurationParser::Parse(FONT_CONFIGURATION_FILE, mDefaultFontFamily, mDefaultFontStyle);
-
- if ( mResourceLoader )
- {
- mResourceLoader->SetDefaultFontFamily( mDefaultFontFamily, mDefaultFontStyle );
- }
+ DALI_ASSERT_DEBUG( 0 != filename.length());
- int fontSize(0);
-#ifndef DALI_PROFILE_UBUNTU
- vconf_get_int( VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, &fontSize );
-#endif // DALI_PROFILE_UBUNTU
- mDefaultFontSize = FONT_SIZE_TABLE[fontSize];
-}
+ bool result = false;
-Integration::DynamicsFactory* TizenPlatformAbstraction::GetDynamicsFactory()
-{
- if( NULL == mDynamicsFactory )
+ std::filebuf buf;
+ buf.open(filename.c_str(), std::ios::out | std::ios_base::trunc | std::ios::binary);
+ if( buf.is_open() )
{
- mDynamicsFactory = new DynamicsFactory;
- }
-
- return mDynamicsFactory;
-}
-
-bool TizenPlatformAbstraction::ReadGlobalMetricsFromCache( const std::string& fontFamily,
- const std::string& fontStyle,
- Integration::GlobalMetrics& globalMetrics )
-{
- return MetricsCache::ReadGlobal( fontFamily, fontStyle, globalMetrics );
-}
+ std::ostream stream(&buf);
-void TizenPlatformAbstraction::WriteGlobalMetricsToCache( const std::string& fontFamily,
- const std::string& fontStyle,
- const Integration::GlobalMetrics& globalMetrics )
-{
- MetricsCache::WriteGlobal( fontFamily, fontStyle, globalMetrics);
-}
+ // determine size of buffer
+ int length = static_cast<int>(numBytes);
-bool TizenPlatformAbstraction::ReadMetricsFromCache( const std::string& fontFamily,
- const std::string& fontStyle,
- std::vector<Integration::GlyphMetrics>& glyphMetricsContainer )
-{
- return MetricsCache::Read( fontFamily, fontStyle, glyphMetricsContainer );
-}
-
-void TizenPlatformAbstraction::WriteMetricsToCache( const std::string& fontFamily,
- const std::string& fontStyle,
- const Integration::GlyphSet& glyphSet )
-{
- MetricsCache::Write( fontFamily, fontStyle, glyphSet );
-}
+ // write contents of buffer to the file
+ stream.write(reinterpret_cast<const char*>(buffer), length);
-void TizenPlatformAbstraction::GetFileNamesFromDirectory( const std::string& directoryName,
- std::vector<std::string>& fileNames )
-{
- dirent* de = NULL;
- DIR* dp;
- dp = opendir( directoryName.c_str() );
- if( dp )
- {
- const std::string dot( "." );
- const std::string dotDot( ".." );
- while( true )
+ if( !stream.bad() )
{
- de = readdir( dp );
- if( de == NULL )
- {
- break;
- }
- const std::string fileName( de->d_name );
- if( ( fileName != dot ) &&
- ( fileName != dotDot ) )
- {
- fileNames.push_back( fileName );
- }
+ result = true;
}
- closedir( dp );
}
-}
-
-Integration::BitmapPtr TizenPlatformAbstraction::GetGlyphImage( const std::string& fontFamily, const std::string& fontStyle, const float fontSize, const uint32_t character ) const
-{
- Integration::BitmapPtr glyphImage;
-
- if( mResourceLoader )
- {
- glyphImage = mResourceLoader->GetGlyphImage( mFreeTypeHandle, fontFamily, fontStyle, fontSize, character );
- }
-
- return glyphImage;
-}
-
-bool TizenPlatformAbstraction::LoadShaderBinFile( const std::string& filename, std::vector< unsigned char >& buffer ) const
-{
- bool result = false;
-
-#ifdef SHADERBIN_CACHE_ENABLED
- std::string path;
-
- if( mResourceLoader )
- {
- path = DALI_SHADERBIN_DIR;
- path += filename;
- result = mResourceLoader->LoadFile( path, buffer );
- }
-
- if( mResourceLoader && result == false )
- {
- path = mDataStoragePath;
- path += filename;
- result = mResourceLoader->LoadFile( path, buffer );
- }
-#endif
return result;
}
-void TizenPlatformAbstraction::SetDataStoragePath( const std::string& path )
-{
- mDataStoragePath = path;
-}
-
} // namespace TizenPlatform
} // namespace Dali