Add support of writing DALi cache file for Android. 56/224456/4
authorAnton Obzhirov <a.obzhirov@samsung.com>
Mon, 10 Feb 2020 17:51:54 +0000 (17:51 +0000)
committerAnton Obzhirov <a.obzhirov@samsung.com>
Thu, 13 Feb 2020 16:43:23 +0000 (16:43 +0000)
Change-Id: I8c52ec58c7009ad336047dce69c023dc5c93a3e7

build/tizen/system-cache-path.in
dali/integration-api/adaptor-framework/android/android-framework.cpp
dali/integration-api/adaptor-framework/android/android-framework.h
dali/internal/adaptor-framework/android/file-stream-impl-android.cpp
dali/internal/adaptor/android/android-framework-impl.cpp
dali/internal/adaptor/android/android-framework-impl.h
dali/internal/system/common/configuration-manager.cpp
dali/internal/system/common/configuration-manager.h

index 816be33..b1bf5d9 100644 (file)
 #include <string>
 #include <cstdlib>
 
+#if ANDROID
+#include <dali/integration-api/adaptor-framework/android/android-framework.h>
+#endif
+
 std::string GetSystemCachePath()
 {
+#if ANDROID
+  return Dali::Integration::AndroidFramework::Get().GetInternalDataPath() + "/dali_common_caches/";
+#else
   return std::string( "@cachePath@/.cache/dali_common_caches/" );
-}
\ No newline at end of file
+#endif
+}
index 859f7c6..3138264 100644 (file)
@@ -70,6 +70,16 @@ AAssetManager* AndroidFramework::GetApplicationAssets() const
   return mImpl->GetApplicationAssets();
 }
 
+void AndroidFramework::SetInternalDataPath( const std::string& path )
+{
+  mImpl->SetInternalDataPath( path );
+}
+
+std::string AndroidFramework::GetInternalDataPath() const
+{
+  return mImpl->GetInternalDataPath();
+}
+
 void AndroidFramework::SetApplicationConfiguration( AConfiguration* configuration )
 {
   mImpl->SetApplicationConfiguration( configuration );
index de49091..e7e755f 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_INTEGRATION_ANDROID_FRAMEWORK_H
 
 /*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 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.
@@ -19,6 +19,8 @@
  */
 
 // EXTERNAL INCLUDES
+#include <string>
+
 #include <jni.h>
 #include <android/asset_manager.h>
 #include <android/asset_manager_jni.h>
@@ -108,6 +110,18 @@ public:
   AAssetManager* GetApplicationAssets() const;
 
   /**
+   *  Sets the Android application internal data path.
+   *  @param[in] path A path to the application data path
+   */
+  void SetInternalDataPath( const std::string& path );
+
+  /**
+   *  Gets the Android application internal data path.
+   *  @return The application data path
+   */
+  std::string GetInternalDataPath() const;
+
+  /**
    * @brief Sets the Android application configuration
    * @param[in] configuration A pointer to Android application configuration
    */
index b6eab47..3c85454 100644 (file)
@@ -109,31 +109,38 @@ std::iostream& FileStream::Impl::GetStream()
     return mBufferStream;
   }
 
-  std::ios_base::openmode openMode = std::ios::ate;
-  if( mMode & Dali::FileStream::BINARY )
+  int openMode = 0;
+
+  if( mMode & Dali::FileStream::APPEND )
   {
-    openMode |= std::ios::binary;
+    openMode |= ( std::ios::out | std::ios::app );
   }
-
-  if( mMode & Dali::FileStream::WRITE )
+  else if( mMode & Dali::FileStream::WRITE )
   {
-    openMode |= std::ios::out;
+    openMode |= ( std::ios::out | std::ios::ate );
   }
-  else
+
+  if( mMode & Dali::FileStream::READ )
   {
     openMode |= std::ios::in;
   }
 
+  if( mMode & Dali::FileStream::BINARY )
+  {
+    openMode |= std::ios::binary;
+  }
+
   if( !mFileName.empty() )
   {
-    if ( !(mMode & Dali::FileStream::WRITE) )
+    // TODO: it works only with text files, we need custom stream buffer implementation for binary and to avoid buffer copy
+    if( !( mMode & Dali::FileStream::WRITE ) && !( mMode & Dali::FileStream::APPEND ) && !( mMode & Dali::FileStream::BINARY ) )
     {
       std::streampos fileSize;
-      if ( ReadFile( mFileName, fileSize, mFileBuffer, Dali::FileLoader::BINARY ) )
+      if( ReadFile( mFileName, fileSize, mFileBuffer, Dali::FileLoader::TEXT ) )
       {
         mBuffer = reinterpret_cast<uint8_t*>( &mFileBuffer[0] );
         mDataSize = fileSize;
-        mBufferStream.rdbuf()->pubsetbuf( reinterpret_cast<char*>( mBuffer ), mDataSize );
+        mBufferStream.str( std::string ( &mFileBuffer[0], fileSize ) );
         if( !mBufferStream.rdbuf()->in_avail() )
         {
           DALI_LOG_ERROR( "File open failed for memory buffer at location: \"%p\", of size: \"%u\", in mode: \"%d\".\n",
@@ -148,7 +155,7 @@ std::iostream& FileStream::Impl::GetStream()
     }
     else
     {
-      mFileStream.open( mFileName, openMode );
+      mFileStream.open( mFileName, static_cast<std::ios_base::openmode>( openMode ) );
       if( !mFileStream.is_open() )
       {
         DALI_LOG_ERROR( "stream open failed for: \"%s\", in mode: \"%d\".\n", mFileName.c_str(), static_cast<int>( openMode ) );
@@ -185,7 +192,11 @@ FILE* FileStream::Impl::GetFile()
   char openMode[16] = { 0 };
   int i = 0;
 
-  if( mMode & Dali::FileStream::WRITE )
+  if( mMode & Dali::FileStream::APPEND )
+  {
+    openMode[i++] = 'a';
+  }
+  else if( mMode & Dali::FileStream::WRITE )
   {
     openMode[i++] = 'w';
   }
@@ -203,10 +214,10 @@ FILE* FileStream::Impl::GetFile()
 
   if( !mFileName.empty() )
   {
-    if ( !( mMode & Dali::FileStream::WRITE ) )
+    if ( !( mMode & Dali::FileStream::WRITE ) && !( mMode & Dali::FileStream::APPEND ) )
     {
       std::streampos fileSize;
-      if ( ReadFile( mFileName, fileSize, mFileBuffer, Dali::FileLoader::BINARY ) )
+      if ( ReadFile( mFileName, fileSize, mFileBuffer, ( mMode & Dali::FileStream::BINARY ) ? Dali::FileLoader::BINARY : Dali::FileLoader::TEXT ) )
       {
         mBuffer = reinterpret_cast<uint8_t*>( &mFileBuffer[0] );
         mDataSize = fileSize;
index 025cdf3..c83265e 100644 (file)
@@ -78,6 +78,16 @@ AAssetManager* AndroidFramework::GetApplicationAssets() const
   return mAssets;
 }
 
+void AndroidFramework::SetInternalDataPath( const std::string& path )
+{
+  mInternalDataPath = path;
+}
+
+std::string AndroidFramework::GetInternalDataPath() const
+{
+  return mInternalDataPath;
+}
+
 void AndroidFramework::SetApplicationConfiguration( AConfiguration* configuration )
 {
   mConfiguration = configuration;
index f3727b5..9bec4ce 100644 (file)
@@ -95,6 +95,16 @@ public:
   AAssetManager* GetApplicationAssets() const;
 
   /**
+   *  copydoc Dali::Integration::AndroidFramework::SetInternalDataPath()
+   */
+  void SetInternalDataPath( const std::string& path );
+
+  /**
+   *  copydoc Dali::Integration::AndroidFramework::GetInternalDataPath()
+   */
+  std::string GetInternalDataPath() const;
+
+  /**
    * @copydoc Dali::Integration::AndroidFramework::SetApplicationConfiguration()
    */
   void SetApplicationConfiguration( AConfiguration* configuration );
@@ -175,6 +185,7 @@ private:
   android_app* mNativeApplication;
   ANativeWindow* mWindow;
   AAssetManager* mAssets;
+  std::string mInternalDataPath;
   AConfiguration* mConfiguration;
   JavaVM* mJVM;
 
index 975749e..63b62dc 100644 (file)
@@ -44,7 +44,7 @@ namespace
 const std::string SYSTEM_CACHE_FILE = "gpu-environment.conf";
 const std::string DALI_ENV_MULTIPLE_WINDOW_SUPPORT = "DALI_ENV_MULTIPLE_WINDOW_SUPPORT";
 
-bool RetrieveKeyFromFile( std::fstream& stream, std::string key, std::string& value )
+bool RetrieveKeyFromConfigFile( std::iostream& stream, const std::string& key, std::string& value )
 {
   bool keyFound = false;
 
@@ -77,7 +77,6 @@ bool RetrieveKeyFromFile( std::fstream& stream, std::string key, std::string& va
 
 ConfigurationManager::ConfigurationManager( std::string systemCachePath, EglGraphics* eglGraphics, ThreadController* threadController )
 : mSystemCacheFilePath( systemCachePath + SYSTEM_CACHE_FILE ),
-  mFileStream( new Dali::FileStream( mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT ) ),
   mEglGraphics( eglGraphics ),
   mThreadController( threadController ),
   mMaxTextureSize( 0u ),
@@ -91,37 +90,51 @@ ConfigurationManager::~ConfigurationManager()
 {
 }
 
-unsigned int ConfigurationManager::GetMaxTextureSize()
+void ConfigurationManager::RetrieveKeysFromConfigFile( const std::string& configFilePath )
 {
-  if ( !mMaxTextureSizeCached )
+  Dali::FileStream configFile( configFilePath, Dali::FileStream::READ | Dali::FileStream::TEXT );
+  std::iostream& stream = configFile.GetStream();
+  if( stream.rdbuf()->in_avail() )
   {
-    std::fstream& configFile = dynamic_cast<std::fstream&>( mFileStream->GetStream() );
-    if( configFile.is_open() )
+    std::string value;
+    if( !mMaxTextureSizeCached &&
+        RetrieveKeyFromConfigFile( stream, DALI_ENV_MAX_TEXTURE_SIZE, value ) )
     {
-      std::string environmentVariableValue;
-      if( RetrieveKeyFromFile( configFile, DALI_ENV_MAX_TEXTURE_SIZE, environmentVariableValue ) )
-      {
-        mMaxTextureSize = std::atoi( environmentVariableValue.c_str() );
-      }
-      else
-      {
-        GlImplementation& mGLES = mEglGraphics->GetGlesInterface();
-        mMaxTextureSize = mGLES.GetMaxTextureSize();
+      mMaxTextureSize = std::atoi( value.c_str() );
+      mMaxTextureSizeCached = true;
+    }
 
-        configFile.clear();
-        configFile << DALI_ENV_MAX_TEXTURE_SIZE << " " << mMaxTextureSize << std::endl;
-      }
+    if( !mIsMultipleWindowSupportedCached &&
+        RetrieveKeyFromConfigFile( stream, DALI_ENV_MULTIPLE_WINDOW_SUPPORT, value ) )
+    {
+      mIsMultipleWindowSupported = std::atoi( value.c_str() );
+      mIsMultipleWindowSupportedCached = true;
+    }
+  }
+}
+
+unsigned int ConfigurationManager::GetMaxTextureSize()
+{
+  if( !mMaxTextureSizeCached )
+  {
+    RetrieveKeysFromConfigFile( mSystemCacheFilePath );
 
+    if( !mMaxTextureSizeCached )
+    {
+      GlImplementation& mGLES = mEglGraphics->GetGlesInterface();
+      mMaxTextureSize = mGLES.GetMaxTextureSize();
       mMaxTextureSizeCached = true;
 
-      if ( mIsMultipleWindowSupportedCached )
+      Dali::FileStream configFile( mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT );
+      std::fstream& stream = dynamic_cast<std::fstream&>( configFile.GetStream() );
+      if( stream.is_open() )
       {
-        configFile.close();
+        stream << DALI_ENV_MAX_TEXTURE_SIZE << " " << mMaxTextureSize << std::endl;
+      }
+      else
+      {
+        DALI_LOG_ERROR( "Fail to open file : %s\n", mSystemCacheFilePath.c_str() );
       }
-    }
-    else
-    {
-      DALI_LOG_ERROR( "Fail to open file : %s\n", mSystemCacheFilePath.c_str() );
     }
   }
 
@@ -132,41 +145,32 @@ bool ConfigurationManager::IsMultipleWindowSupported()
 {
   if ( !mIsMultipleWindowSupportedCached )
   {
-    std::fstream& configFile = dynamic_cast<std::fstream&>( mFileStream->GetStream() );
-    if( configFile.is_open() )
+    RetrieveKeysFromConfigFile( mSystemCacheFilePath );
+
+    if ( !mIsMultipleWindowSupportedCached )
     {
-      std::string environmentVariableValue;
-      if( RetrieveKeyFromFile( configFile, DALI_ENV_MULTIPLE_WINDOW_SUPPORT, environmentVariableValue ) )
+      EglImplementation& eglImpl = mEglGraphics->GetEglImplementation();
+      if ( !eglImpl.IsGlesInitialized() )
       {
-        mIsMultipleWindowSupported = std::atoi( environmentVariableValue.c_str() );
-      }
-      else
-      {
-        EglImplementation& eglImpl = mEglGraphics->GetEglImplementation();
-        if ( !eglImpl.IsGlesInitialized() )
-        {
-          // Wait until GLES is initialised, but this will happen once.
-          // This method blocks until the render thread has initialised the graphics.
-          mThreadController->WaitForGraphicsInitialization();
-        }
-
-        // Query from GLES and save the cache
-        mIsMultipleWindowSupported = eglImpl.IsSurfacelessContextSupported();
-
-        configFile.clear();
-        configFile << DALI_ENV_MULTIPLE_WINDOW_SUPPORT << " " << mIsMultipleWindowSupported << std::endl;
+        // Wait until GLES is initialised, but this will happen once.
+        // This method blocks until the render thread has initialised the graphics.
+        mThreadController->WaitForGraphicsInitialization();
       }
 
+      // Query from GLES and save the cache
+      mIsMultipleWindowSupported = eglImpl.IsSurfacelessContextSupported();
       mIsMultipleWindowSupportedCached = true;
 
-      if ( mMaxTextureSizeCached )
+      Dali::FileStream configFile( mSystemCacheFilePath, Dali::FileStream::READ | Dali::FileStream::APPEND | Dali::FileStream::TEXT );
+      std::fstream& stream = dynamic_cast<std::fstream&>( configFile.GetStream() );
+      if ( stream.is_open() )
       {
-        configFile.close();
+        stream << DALI_ENV_MULTIPLE_WINDOW_SUPPORT << " " << mIsMultipleWindowSupported << std::endl;
+      }
+      else
+      {
+        DALI_LOG_ERROR( "Fail to open file : %s\n", mSystemCacheFilePath.c_str() );
       }
-    }
-    else
-    {
-      DALI_LOG_ERROR( "Fail to open file : %s\n", mSystemCacheFilePath.c_str() );
     }
   }
 
index 105a5b4..a6721ee 100644 (file)
@@ -54,6 +54,11 @@ public:
   virtual ~ConfigurationManager();
 
   /**
+   * @brief Retrieve all keys from the config file if the file exists.
+   */
+  void RetrieveKeysFromConfigFile( const std::string& configFilePath );
+
+  /**
    * @brief Get the maximum texture size.
    * @return The maximum texture size
    */
@@ -80,7 +85,6 @@ public:
 private: // Data
 
   std::string mSystemCacheFilePath;              ///< The path of system cache file
-  std::unique_ptr<FileStream> mFileStream;       ///< The file stream to access the system cache
   EglGraphics* mEglGraphics;                     ///< EGL graphics
   ThreadController* mThreadController;           ///< The thread controller
   unsigned int mMaxTextureSize;                  ///< The largest texture that the GL can handle