[Tizen] Use broken image when animated image loading is failed
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / common / gif-loading.cpp
index 69d5b7a..1b195b9 100644 (file)
@@ -29,6 +29,7 @@
 #include <cstring>
 #include <memory>
 
+#include <dali/devel-api/threading/mutex.h>
 #include <dali/integration-api/debug.h>
 #include <dali/internal/imaging/common/file-download.h>
 #include <dali/internal/system/common/file-reader.h>
@@ -218,7 +219,7 @@ struct GifAccessor
 
     if(!gif)
     {
-      DALI_LOG_ERROR("LOAD_ERROR_UNKNOWN_FORMAT");
+      DALI_LOG_ERROR("LOAD_ERROR_UNKNOWN_FORMAT\n");
     }
   }
 
@@ -635,6 +636,7 @@ bool DecodeImage(GifFileType* gif, uint32_t* data, int rowpix, int xin, int yin,
 
   if((gifW < w) || (gifH < h))
   {
+    DALI_LOG_ERROR("gifW : %d, w : %d, gifH : %d, h : %d\n", gifW, w, gifH, h);
     DALI_ASSERT_DEBUG(false && "Dimensions are bigger than the Gif image size");
     goto on_error;
   }
@@ -784,7 +786,7 @@ bool ReadHeader(LoaderInfo&      loaderInfo,
   if(!success || !fileData.globalMap)
   {
     success = false;
-    DALI_LOG_ERROR("LOAD_ERROR_CORRUPT_FILE");
+    DALI_LOG_ERROR("LOAD_ERROR_CORRUPT_FILE\n");
   }
   else
   {
@@ -830,7 +832,7 @@ bool ReadHeader(LoaderInfo&      loaderInfo,
             else
             {
               success = false;
-              DALI_LOG_ERROR("LOAD_ERROR_UNKNOWN_FORMAT");
+              DALI_LOG_ERROR("LOAD_ERROR_UNKNOWN_FORMAT\n");
             }
             break;
           }
@@ -845,14 +847,14 @@ bool ReadHeader(LoaderInfo&      loaderInfo,
             if(DGifGetImageDesc(gifAccessor.gif) == GIF_ERROR)
             {
               success = false;
-              DALI_LOG_ERROR("LOAD_ERROR_UNKNOWN_FORMAT");
+              DALI_LOG_ERROR("LOAD_ERROR_UNKNOWN_FORMAT\n");
               break;
             }
             // skip decoding and just walk image to next
             if(DGifGetCode(gifAccessor.gif, &img_code, &img) == GIF_ERROR)
             {
               success = false;
-              DALI_LOG_ERROR("LOAD_ERROR_UNKNOWN_FORMAT");
+              DALI_LOG_ERROR("LOAD_ERROR_UNKNOWN_FORMAT\n");
               break;
             }
             // skip till next...
@@ -999,7 +1001,7 @@ bool ReadNextFrame(LoaderInfo& loaderInfo, ImageProperties& prop, //  use for w
   frame = FindFrame(animated, index);
   if(!frame)
   {
-    DALI_LOG_ERROR("LOAD_ERROR_CORRUPT_FILE");
+    DALI_LOG_ERROR("LOAD_ERROR_CORRUPT_FILE\n");
     return false;
   }
   else if(!(frame->loaded) || !(frame->data))
@@ -1024,13 +1026,13 @@ bool ReadNextFrame(LoaderInfo& loaderInfo, ImageProperties& prop, //  use for w
       loaderInfo.fileInfo.position = 0;
       if(!loaderInfo.fileInfo.map)
       {
-        DALI_LOG_ERROR("LOAD_ERROR_CORRUPT_FILE");
+        DALI_LOG_ERROR("LOAD_ERROR_CORRUPT_FILE\n");
         return false;
       }
       std::unique_ptr<GifAccessor> gifAccessor = std::make_unique<GifAccessor>(loaderInfo.fileInfo);
       if(!gifAccessor->gif)
       {
-        DALI_LOG_ERROR("LOAD_ERROR_UNKNOWN_FORMAT");
+        DALI_LOG_ERROR("LOAD_ERROR_UNKNOWN_FORMAT\n");
         return false;
       }
       loaderInfo.gifAccessor = std::move(gifAccessor);
@@ -1045,7 +1047,7 @@ bool ReadNextFrame(LoaderInfo& loaderInfo, ImageProperties& prop, //  use for w
     {
       if(DGifGetRecordType(loaderInfo.gifAccessor->gif, &rec) == GIF_ERROR)
       {
-        DALI_LOG_ERROR("LOAD_ERROR_UNKNOWN_FORMAT");
+        DALI_LOG_ERROR("LOAD_ERROR_UNKNOWN_FORMAT\n");
         return false;
       }
 
@@ -1073,7 +1075,7 @@ bool ReadNextFrame(LoaderInfo& loaderInfo, ImageProperties& prop, //  use for w
         // get image desc
         if(DGifGetImageDesc(loaderInfo.gifAccessor->gif) == GIF_ERROR)
         {
-          DALI_LOG_ERROR("LOAD_ERROR_UNKNOWN_FORMAT");
+          DALI_LOG_ERROR("LOAD_ERROR_UNKNOWN_FORMAT\n");
           return false;
         }
 
@@ -1147,7 +1149,7 @@ bool ReadNextFrame(LoaderInfo& loaderInfo, ImageProperties& prop, //  use for w
           ClipCoordinates(prop.w, prop.h, &xin, &yin, frameInfo->x, frameInfo->y, frameInfo->w, frameInfo->h, &x, &y, &w, &h);
           if(!DecodeImage(loaderInfo.gifAccessor->gif, thisFrame->data, prop.w, xin, yin, frameInfo->transparent, x, y, w, h, first))
           {
-            DALI_LOG_ERROR("LOAD_ERROR_CORRUPT_FILE");
+            DALI_LOG_ERROR("LOAD_ERROR_CORRUPT_FILE\n");
             return false;
           }
 
@@ -1173,7 +1175,7 @@ bool ReadNextFrame(LoaderInfo& loaderInfo, ImageProperties& prop, //  use for w
             // and decode the gif with overwriting
             if(!DecodeImage(loaderInfo.gifAccessor->gif, reinterpret_cast<uint32_t*>(pixels), prop.w, xin, yin, frameInfo->transparent, x, y, w, h, true))
             {
-              DALI_LOG_ERROR("LOAD_ERROR_CORRUPT_FILE");
+              DALI_LOG_ERROR("LOAD_ERROR_CORRUPT_FILE\n");
               return false;
             }
 
@@ -1187,7 +1189,7 @@ bool ReadNextFrame(LoaderInfo& loaderInfo, ImageProperties& prop, //  use for w
           // skip decoding and just walk image to next
           if(DGifGetCode(loaderInfo.gifAccessor->gif, &img_code, &img) == GIF_ERROR)
           {
-            DALI_LOG_ERROR("LOAD_ERROR_UNKNOWN_FORMAT");
+            DALI_LOG_ERROR("LOAD_ERROR_UNKNOWN_FORMAT\n");
             return false;
           }
 
@@ -1236,14 +1238,16 @@ struct GifLoading::Impl
 {
 public:
   Impl(const std::string& url, bool isLocalResource)
-  : mUrl(url)
+  : mUrl(url),
+    mLoadSucceeded(true),
+    mMutex()
   {
     loaderInfo.gifAccessor = nullptr;
     int error;
     loaderInfo.fileData.fileName        = mUrl.c_str();
     loaderInfo.fileData.isLocalResource = isLocalResource;
 
-    ReadHeader(loaderInfo, imageProperties, &error);
+    mLoadSucceeded = ReadHeader(loaderInfo, imageProperties, &error);
   }
 
   // Moveable but not copyable
@@ -1255,6 +1259,8 @@ public:
   std::string     mUrl;
   LoaderInfo      loaderInfo;
   ImageProperties imageProperties;
+  bool            mLoadSucceeded;
+  Mutex           mMutex;
 };
 
 AnimatedImageLoadingPtr GifLoading::New(const std::string& url, bool isLocalResource)
@@ -1276,6 +1282,16 @@ bool GifLoading::LoadNextNFrames(uint32_t frameStartIndex, int count, std::vecto
 {
   int  error;
   bool ret = false;
+  if(!mImpl->mLoadSucceeded)
+  {
+    return false;
+  }
+
+  Mutex::ScopedLock lock(mImpl->mMutex);
+  if(!mImpl->mLoadSucceeded)
+  {
+    return false;
+  }
 
   const int bufferSize = mImpl->imageProperties.w * mImpl->imageProperties.h * sizeof(uint32_t);
 
@@ -1304,7 +1320,12 @@ Dali::Devel::PixelBuffer GifLoading::LoadFrame(uint32_t frameIndex)
 {
   int                      error;
   Dali::Devel::PixelBuffer pixelBuffer;
+  if(!mImpl->mLoadSucceeded)
+  {
+    return pixelBuffer;
+  }
 
+  Mutex::ScopedLock lock(mImpl->mMutex);
   DALI_LOG_INFO(gGifLoadingLogFilter, Debug::Concise, "LoadFrame( frameIndex:%d )\n", frameIndex);
 
   pixelBuffer = Dali::Devel::PixelBuffer::New(mImpl->imageProperties.w, mImpl->imageProperties.h, Dali::Pixel::RGBA8888);
@@ -1339,6 +1360,11 @@ std::string GifLoading::GetUrl() const
   return mImpl->mUrl;
 }
 
+bool GifLoading::HasLoadingSucceeded() const
+{
+  return mImpl->mLoadSucceeded;
+}
+
 } // namespace Adaptor
 
 } // namespace Internal