Revert "[Tizen] Add screen and client rotation itself function"
[platform/core/uifw/dali-adaptor.git] / dali / devel-api / adaptor-framework / gif-loading.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 3d8e785..232c555
@@ -30,6 +30,8 @@
 #include <cstring>
 #include <dali/integration-api/debug.h>
 #include <dali/public-api/images/pixel-data.h>
+#include <dali/internal/imaging/common/file-download.h>
+#include <dali/internal/system/common/file-reader.h>
 
 #define IMG_TOO_BIG( w, h )                                                        \
   ( ( static_cast<unsigned long long>(w) * static_cast<unsigned long long>(h) ) >= \
@@ -51,6 +53,7 @@ Debug::Filter *gGifLoadingLogFilter = Debug::Filter::New( Debug::NoLogging, fals
 #endif
 
 const int IMG_MAX_SIZE = 65000;
+constexpr size_t MAXIMUM_DOWNLOAD_IMAGE_SIZE  = 50 * 1024 * 1024;
 
 #if GIFLIB_MAJOR < 5
 const int DISPOSE_BACKGROUND = 2;       /* Set area too background color */
@@ -130,7 +133,8 @@ struct LoaderInfo
     : fileName( nullptr ),
       globalMap ( nullptr ),
       length( 0 ),
-      fileDescriptor( -1 )
+      fileDescriptor( -1 ),
+      isLocalResource( true )
     {
     }
 
@@ -138,6 +142,7 @@ struct LoaderInfo
     unsigned char *globalMap ;      /**< A pointer to the entire contents of the file that have been mapped with mmap(2). */
     long long length;  /**< The length of the file in bytes. */
     int fileDescriptor; /**< The file descriptor. */
+    bool isLocalResource; /**< The flag whether the file is a local resource */
   };
 
   struct FileInfo
@@ -162,6 +167,13 @@ struct LoaderInfo
 
 struct ImageProperties
 {
+  ImageProperties()
+  : w( 0 ),
+    h( 0 ),
+    alpha( 0 )
+  {
+  }
+
   unsigned int w;
   unsigned int h;
   bool alpha;
@@ -627,37 +639,74 @@ bool ReadHeader( LoaderInfo &loaderInfo,
   FrameInfo *frameInfo = NULL;
   bool full = true;
 
-  // init prop struct with some default null values
-  prop.w = 0;
-  prop.h = 0;
+  if( fileData.isLocalResource )
+  {
+    // local file
+    fileData.fileDescriptor = open( fileData.fileName, O_RDONLY );
 
-  fileData.fileDescriptor = open( fileData.fileName, O_RDONLY );
+    if( fileData.fileDescriptor == -1 )
+    {
+      return false;
+    }
 
-  if( fileData.fileDescriptor == -1 )
-  {
-    return false;
-  }
+    fileData.length = lseek( fileData.fileDescriptor, 0, SEEK_END );
+    if( fileData.length <= -1 )
+    {
+      close( fileData.fileDescriptor );
+      return false;
+    }
 
-  fileData.length = lseek( fileData.fileDescriptor, 0, SEEK_END );
-  if( fileData.length <= -1 )
-  {
-    close( fileData.fileDescriptor );
-    return false;
-  }
+    if( lseek( fileData.fileDescriptor, 0, SEEK_SET ) == -1 )
+    {
+      close( fileData.fileDescriptor );
+      return false;
+    }
+
+    // map the file and store/track info
+    fileData.globalMap  = reinterpret_cast<unsigned char *>( mmap(NULL, fileData.length, PROT_READ, MAP_SHARED, fileData.fileDescriptor, 0 ));
+    fileInfo.map = fileData.globalMap ;
 
-  if( lseek( fileData.fileDescriptor, 0, SEEK_SET ) == -1 )
+    close(fileData.fileDescriptor);
+    fileData.fileDescriptor = -1;
+  }
+  else
   {
-    close( fileData.fileDescriptor );
-    return false;
+    // remote file
+    bool succeeded;
+    Dali::Vector<uint8_t> dataBuffer;
+    size_t dataSize;
+
+    succeeded = TizenPlatform::Network::DownloadRemoteFileIntoMemory( fileData.fileName, dataBuffer, dataSize,
+                                                                      MAXIMUM_DOWNLOAD_IMAGE_SIZE );
+    if( succeeded )
+    {
+      size_t blobSize = dataBuffer.Size();
+      if( blobSize > 0U )
+      {
+        // Open a file handle on the memory buffer:
+        Dali::Internal::Platform::FileReader fileReader( dataBuffer, blobSize );
+        FILE * const fp = fileReader.GetFile();
+        if ( NULL != fp )
+        {
+          if( ( ! fseek( fp, 0, SEEK_SET ) ) )
+          {
+            fileData.globalMap = reinterpret_cast<GifByteType*>( malloc(sizeof( GifByteType ) * blobSize ) );
+            fileData.length = fread( fileData.globalMap, sizeof( GifByteType ), blobSize, fp);
+            fileInfo.map = fileData.globalMap;
+          }
+          else
+          {
+            DALI_LOG_ERROR( "Error seeking within file\n" );
+          }
+        }
+        else
+        {
+          DALI_LOG_ERROR( "Error reading file\n" );
+        }
+      }
+    }
   }
 
-  // map the file and store/track info
-  fileData.globalMap  = reinterpret_cast<unsigned char *>( mmap(NULL, fileData.length, PROT_READ, MAP_SHARED, fileData.fileDescriptor, 0 ));
-  fileInfo.map = fileData.globalMap ;
-
-  close(fileData.fileDescriptor);
-  fileData.fileDescriptor = -1;
-
   if( !fileInfo.map )
   {
     LOADERR("LOAD_ERROR_CORRUPT_FILE");
@@ -1135,21 +1184,36 @@ on_error: // jump here on any errors to clean up
 struct GifLoading::Impl
 {
 public:
-  Impl( const std::string& url)
+  Impl( const std::string& url, bool isLocalResource )
   : mUrl( url )
   {
     loaderInfo.gif = nullptr;
     int error;
     loaderInfo.fileData.fileName = mUrl.c_str();
+    loaderInfo.fileData.isLocalResource = isLocalResource;
 
     ReadHeader( loaderInfo, imageProperties, &error );
   }
 
+  // Moveable but not copyable
+
+  Impl( const Impl& ) = delete;
+  Impl& operator=( const Impl& ) = delete;
+  Impl( Impl&& ) = default;
+  Impl& operator=( Impl&& ) = default;
+
   ~Impl()
   {
     if( loaderInfo.fileData.globalMap  )
     {
-      munmap( loaderInfo.fileData.globalMap , loaderInfo.fileData.length );
+      if( loaderInfo.fileData.isLocalResource )
+      {
+        munmap( loaderInfo.fileData.globalMap , loaderInfo.fileData.length );
+      }
+      else
+      {
+        free( loaderInfo.fileData.globalMap );
+      }
       loaderInfo.fileData.globalMap  = nullptr;
     }
 
@@ -1170,16 +1234,17 @@ public:
   ImageProperties imageProperties;
 };
 
-std::unique_ptr<GifLoading> GifLoading::New( const std::string &url )
+std::unique_ptr<GifLoading> GifLoading::New( const std::string &url, bool isLocalResource )
 {
-  return std::unique_ptr<GifLoading>( new GifLoading( url ) );
+  return std::unique_ptr<GifLoading>( new GifLoading( url, isLocalResource ) );
 }
 
-GifLoading::GifLoading( const std::string &url )
-: mImpl( new GifLoading::Impl( url ) )
+GifLoading::GifLoading( const std::string &url, bool isLocalResource )
+: mImpl( new GifLoading::Impl( url, isLocalResource ) )
 {
 }
 
+
 GifLoading::~GifLoading()
 {
   delete mImpl;
@@ -1226,7 +1291,7 @@ bool GifLoading::LoadAllFrames( std::vector<Dali::PixelData> &pixelData, Dali::V
 
 ImageDimensions GifLoading::GetImageSize()
 {
-  return ImageDimensions( mImpl->imageProperties.w, mImpl->imageProperties.w );
+  return ImageDimensions( mImpl->imageProperties.w, mImpl->imageProperties.h );
 }
 
 int GifLoading::GetImageCount()