[4.0] Exposing Exif Image metadata
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / tizen / tizen-platform-abstraction.cpp
index e7d2bb0..2bf6e6f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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"
+// 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 unsigned int NANOSECS_TO_MICROSECS( 1000 );                          ///< 1000 nanoseconds = 1 microsecond
-}
-
 TizenPlatformAbstraction::TizenPlatformAbstraction()
-: mResourceLoader(new ResourceLoader),
-  mDynamicsFactory(NULL),
-  mDataStoragePath( "" )
+: mDataStoragePath( "" )
 {
 }
 
 TizenPlatformAbstraction::~TizenPlatformAbstraction()
 {
-  delete mResourceLoader;
-  delete mDynamicsFactory;
-}
-
-void TizenPlatformAbstraction::GetTimeMicroseconds(unsigned int &seconds, unsigned int &microSeconds)
-{
-  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();
-  }
-}
-
-void TizenPlatformAbstraction::GetDefaultFontDescription( std::string& fontFamily, std::string& fontStyle ) const
-{
-  FontConfigurationParser::Parse(FONT_CONFIGURATION_FILE, fontFamily, fontStyle);
 }
 
-int TizenPlatformAbstraction::GetDefaultFontSize() const
+ImageDimensions TizenPlatformAbstraction::GetClosestImageSize( const std::string& filename,
+                                                               ImageDimensions size,
+                                                               FittingMode::Type fittingMode,
+                                                               SamplingMode::Type samplingMode,
+                                                               bool orientationCorrection )
 {
-  int fontSize( -1 );
-
-#ifndef DALI_PROFILE_UBUNTU
-  vconf_get_int( VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, &fontSize );
-#endif // DALI_PROFILE_UBUNTU
-
-  return fontSize;
+  return ImageLoader::GetClosestImageSize( filename, size, fittingMode, samplingMode, orientationCorrection );
 }
 
-void TizenPlatformAbstraction::GetClosestImageSize( const std::string& filename,
-                                                  const ImageAttributes& attributes,
-                                                  Vector2& closestSize )
+ImageDimensions TizenPlatformAbstraction::GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
+                                                               ImageDimensions size,
+                                                               FittingMode::Type fittingMode,
+                                                               SamplingMode::Type samplingMode,
+                                                               bool orientationCorrection )
 {
-  closestSize = Vector2::ZERO;
-  ImageLoader::GetClosestImageSize(filename, attributes, closestSize );
+  return ImageLoader::GetClosestImageSize( resourceBuffer, size, fittingMode, samplingMode, orientationCorrection );
 }
 
-void TizenPlatformAbstraction::GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
-                                                  const ImageAttributes& attributes,
-                                                  Vector2& closestSize )
+Integration::ResourcePointer TizenPlatformAbstraction::LoadImageSynchronously(const Integration::BitmapResourceType& resource, const std::string& resourcePath)
 {
-  closestSize = Vector2::ZERO;
-  ImageLoader::GetClosestImageSize(resourceBuffer, attributes, closestSize );
+  return ImageLoader::LoadImageSynchronously( resource, resourcePath );
 }
 
-
-void TizenPlatformAbstraction::LoadResource(const Integration::ResourceRequest& request)
+Integration::BitmapPtr TizenPlatformAbstraction::DecodeBuffer( const Integration::BitmapResourceType& resource, uint8_t * buffer, size_t size )
 {
-  if (mResourceLoader)
-  {
-    mResourceLoader->LoadResource(request);
-  }
-}
+  Integration::BitmapPtr resultBitmap;
+  Dali::Devel::PixelBuffer bitmap;
 
-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)
+  Dali::Internal::Platform::FileReader fileReader( buffer, size );
+  FILE * const fp = fileReader.GetFile();
+  if( fp )
   {
-    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;
 }
 
-bool TizenPlatformAbstraction::LoadFile( const std::string& filename, std::vector< unsigned char >& buffer ) const
+bool TizenPlatformAbstraction::LoadShaderBinaryFile( const std::string& filename, Dali::Vector< unsigned char >& buffer ) const
 {
   bool result = false;
 
-  if (mResourceLoader)
-  {
-    result = mResourceLoader->LoadFile(filename, buffer);
-  }
+#ifdef SHADERBIN_CACHE_ENABLED
+  std::string path;
 
-  return result;
-}
+  // First check the system location where shaders are stored at install time:
+  path = DALI_SHADERBIN_DIR;
+  path += filename;
+  result = LoadFile( path, buffer );
 
-std::string TizenPlatformAbstraction::LoadFile( const std::string& filename )
-{
-  std::string result;
-  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 )
   {
-    result = mResourceLoader->LoadFile(filename);
+    path = mDataStoragePath;
+    path += filename;
+    result = LoadFile( path, buffer );
   }
+#endif
 
   return result;
 }
 
-bool TizenPlatformAbstraction::SaveFile(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->SaveFile(filename, buffer);
-  }
+#ifdef SHADERBIN_CACHE_ENABLED
+
+  // 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 );
+
+#endif
 
   return result;
 }
 
-void TizenPlatformAbstraction::JoinLoaderThreads()
+void TizenPlatformAbstraction::SetDataStoragePath( const std::string& path )
 {
-  delete mResourceLoader;
-  mResourceLoader = NULL;
+  mDataStoragePath = path;
 }
 
-Integration::DynamicsFactory* TizenPlatformAbstraction::GetDynamicsFactory()
+TizenPlatformAbstraction* CreatePlatformAbstraction()
 {
-  if( NULL == mDynamicsFactory )
-  {
-    mDynamicsFactory = new DynamicsFactory;
-  }
-
-  return mDynamicsFactory;
+  return new TizenPlatformAbstraction();
 }
 
-bool TizenPlatformAbstraction::LoadShaderBinFile( const std::string& filename, std::vector< unsigned char >& buffer ) const
+bool SaveFile( const std::string& filename, const unsigned char * buffer, unsigned int numBytes )
 {
-  bool result = false;
+  DALI_ASSERT_DEBUG( 0 != filename.length());
 
-#ifdef SHADERBIN_CACHE_ENABLED
-  std::string path;
+  bool result = false;
 
-  if( mResourceLoader )
+  std::filebuf buf;
+  buf.open(filename.c_str(), std::ios::out | std::ios_base::trunc | std::ios::binary);
+  if( buf.is_open() )
   {
-    path = DALI_SHADERBIN_DIR;
-    path += filename;
-    result = mResourceLoader->LoadFile( path, buffer );
-  }
+    std::ostream stream(&buf);
 
-  if( mResourceLoader && result == false )
-  {
-    path = mDataStoragePath;
-    path += filename;
-    result = mResourceLoader->LoadFile( path, buffer );
+    // determine size of buffer
+    int length = static_cast<int>(numBytes);
+
+    // write contents of buffer to the file
+    stream.write(reinterpret_cast<const char*>(buffer), length);
+
+    if( !stream.bad() )
+    {
+      result = true;
+    }
   }
-#endif
 
   return result;
 }
 
-void TizenPlatformAbstraction::SetDataStoragePath( const std::string& path )
-{
-  mDataStoragePath = path;
-}
-
 }  // namespace TizenPlatform
 
 }  // namespace Dali