Fix issue, agif to play a number of times of loopcount.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / animated-image-visual.cpp
index d37863e..b1463ba 100755 (executable)
 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
+#include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
 #include <dali-toolkit/internal/visuals/animated-image/fixed-image-cache.h>
 #include <dali-toolkit/internal/visuals/animated-image/rolling-image-cache.h>
 #include <dali-toolkit/internal/visuals/animated-image/rolling-gif-image-cache.h>
-#include <dali-toolkit/internal/visuals/image/image-visual.h>
 #include <dali-toolkit/devel-api/image-loader/image-atlas.h>
 #include <dali-toolkit/devel-api/image-loader/texture-manager.h>
+#include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
 
 namespace Dali
 {
@@ -100,9 +101,9 @@ Debug::Filter* gAnimImgLogFilter = Debug::Filter::New(Debug::NoLogging, false, "
  *  Time
  */
 
-AnimatedImageVisualPtr AnimatedImageVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl, const Property::Map& properties )
+AnimatedImageVisualPtr AnimatedImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties )
 {
-  AnimatedImageVisualPtr visual( new AnimatedImageVisual( factoryCache ) );
+  AnimatedImageVisualPtr visual( new AnimatedImageVisual( factoryCache, shaderFactory ) );
   visual->InitializeGif( imageUrl );
   visual->SetProperties( properties );
 
@@ -114,9 +115,9 @@ AnimatedImageVisualPtr AnimatedImageVisual::New( VisualFactoryCache& factoryCach
   return visual;
 }
 
-AnimatedImageVisualPtr AnimatedImageVisual::New( VisualFactoryCache& factoryCache, const Property::Array& imageUrls, const Property::Map& properties )
+AnimatedImageVisualPtr AnimatedImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const Property::Array& imageUrls, const Property::Map& properties )
 {
-  AnimatedImageVisualPtr visual( new AnimatedImageVisual( factoryCache ) );
+  AnimatedImageVisualPtr visual( new AnimatedImageVisual( factoryCache, shaderFactory ) );
   visual->mImageUrls = new ImageCache::UrlList();
   visual->mImageUrls->reserve( imageUrls.Count() );
 
@@ -138,9 +139,9 @@ AnimatedImageVisualPtr AnimatedImageVisual::New( VisualFactoryCache& factoryCach
   return visual;
 }
 
-AnimatedImageVisualPtr AnimatedImageVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl )
+AnimatedImageVisualPtr AnimatedImageVisual::New( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl )
 {
-  AnimatedImageVisualPtr visual( new AnimatedImageVisual( factoryCache ) );
+  AnimatedImageVisualPtr visual( new AnimatedImageVisual( factoryCache, shaderFactory ) );
   visual->InitializeGif( imageUrl );
 
   if( visual->mFrameCount > 0 )
@@ -154,15 +155,16 @@ AnimatedImageVisualPtr AnimatedImageVisual::New( VisualFactoryCache& factoryCach
 void AnimatedImageVisual::InitializeGif( const VisualUrl& imageUrl )
 {
   mImageUrl = imageUrl;
-  mGifLoading = GifLoading::New( imageUrl.GetUrl() );
+  mGifLoading = GifLoading::New( imageUrl.GetUrl(), imageUrl.IsLocalResource() );
   mFrameCount = mGifLoading->GetImageCount();
   mGifLoading->LoadFrameDelays( mFrameDelayContainer );
 }
 
-AnimatedImageVisual::AnimatedImageVisual( VisualFactoryCache& factoryCache )
-: Visual::Base( factoryCache ),
+AnimatedImageVisual::AnimatedImageVisual( VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory )
+: Visual::Base( factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO ),
   mFrameDelayTimer(),
   mPlacementActor(),
+  mImageVisualShaderFactory( shaderFactory ),
   mPixelArea( FULL_TEXTURE_RECT ),
   mImageUrl(),
   mGifLoading( nullptr ),
@@ -179,6 +181,7 @@ AnimatedImageVisual::AnimatedImageVisual( VisualFactoryCache& factoryCache )
   mImageSize(),
   mWrapModeU( WrapMode::DEFAULT ),
   mWrapModeV( WrapMode::DEFAULT ),
+  mActionStatus( DevelAnimatedImageVisual::Action::PLAY ),
   mStartFirstFrame(false)
 {}
 
@@ -242,6 +245,38 @@ void AnimatedImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) cons
   // Do nothing
 }
 
+void AnimatedImageVisual::OnDoAction( const Dali::Property::Index actionId, const Dali::Property::Value& attributes )
+{
+  // Check if action is valid for this visual type and perform action if possible
+
+  switch ( actionId )
+  {
+    case DevelAnimatedImageVisual::Action::PAUSE:
+    {
+      // Pause will be executed on next timer tick
+      mActionStatus = DevelAnimatedImageVisual::Action::PAUSE;
+      break;
+    }
+    case DevelAnimatedImageVisual::Action::PLAY:
+    {
+      if( IsOnStage() && mActionStatus != DevelAnimatedImageVisual::Action::PLAY )
+      {
+        mFrameDelayTimer.Start();
+      }
+      mActionStatus = DevelAnimatedImageVisual::Action::PLAY;
+      break;
+    }
+    case DevelAnimatedImageVisual::Action::STOP:
+    {
+      // STOP reset functionality will actually be done in a future change
+      // Stop will be executed on next timer tick
+      mCurrentFrameIndex = 0;
+      mActionStatus = DevelAnimatedImageVisual::Action::STOP;
+      break;
+    }
+  }
+}
+
 void AnimatedImageVisual::DoSetProperties( const Property::Map& propertyMap )
 {
   // url[s] already passed in from constructor
@@ -299,7 +334,7 @@ void AnimatedImageVisual::DoSetProperty( Property::Index index,
     }
     case Toolkit::ImageVisual::Property::WRAP_MODE_U:
     {
-      int wrapMode;
+      int wrapMode = 0;
       if(Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode ))
       {
         mWrapModeU = Dali::WrapMode::Type(wrapMode);
@@ -312,7 +347,7 @@ void AnimatedImageVisual::DoSetProperty( Property::Index index,
     }
     case Toolkit::ImageVisual::Property::WRAP_MODE_V:
     {
-      int wrapMode;
+      int wrapMode = 0;
       if(Scripting::GetEnumerationProperty( value, WRAP_MODE_TABLE, WRAP_MODE_TABLE_COUNT, wrapMode ))
       {
         mWrapModeV = Dali::WrapMode::Type(wrapMode);
@@ -409,7 +444,7 @@ void AnimatedImageVisual::CreateRenderer()
 {
   bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE;
   bool atlasing = false;
-  Shader shader = ImageVisual::GetImageShader( mFactoryCache, atlasing, defaultWrapMode );
+  Shader shader = mImageVisualShaderFactory.GetShader( mFactoryCache, atlasing, defaultWrapMode, IsRoundedCornerRequired() );
 
   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
 
@@ -462,21 +497,25 @@ void AnimatedImageVisual::LoadFirstBatch()
   {
     mImageCache = new RollingGifImageCache( textureManager, *mGifLoading, mFrameCount, *this, cacheSize, batchSize );
   }
-  else if( batchSize > 0 && cacheSize > 0 )
+  else if( mImageUrls )
   {
-    if( cacheSize < numUrls )
+    if( batchSize > 0 && cacheSize > 0 )
     {
-      mImageCache = new RollingImageCache( textureManager, *mImageUrls, *this, cacheSize, batchSize );
+      if( cacheSize < numUrls )
+      {
+        mImageCache = new RollingImageCache( textureManager, *mImageUrls, *this, cacheSize, batchSize );
+      }
+      else
+      {
+        mImageCache = new FixedImageCache( textureManager, *mImageUrls, *this, batchSize );
+      }
     }
     else
     {
-      mImageCache = new FixedImageCache( textureManager, *mImageUrls, *this, batchSize );
+      mImageCache = new RollingImageCache( textureManager, *mImageUrls, *this, 1, 1 );
     }
   }
-  else
-  {
-    mImageCache = new RollingImageCache( textureManager, *mImageUrls, *this, 1, 1 );
-  }
+
   if (!mImageCache)
   {
     DALI_LOG_ERROR("mImageCache is null");
@@ -488,7 +527,10 @@ void AnimatedImageVisual::StartFirstFrame( TextureSet& textureSet )
   DALI_LOG_INFO(gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::StartFirstFrame()\n");
 
   mStartFirstFrame = false;
-  mImpl->mRenderer.SetTextures( textureSet );
+  if(mImpl->mRenderer)
+  {
+    mImpl->mRenderer.SetTextures( textureSet );
+  }
   Actor actor = mPlacementActor.GetHandle();
   if( actor )
   {
@@ -555,18 +597,25 @@ void AnimatedImageVisual::FrameReady( TextureSet textureSet )
   }
   else
   {
-    mImpl->mRenderer.SetTextures( textureSet );
+    if(mImpl->mRenderer)
+    {
+      mImpl->mRenderer.SetTextures( textureSet );
+    }
   }
 }
 
 bool AnimatedImageVisual::DisplayNextFrame()
 {
+  if( mActionStatus == DevelAnimatedImageVisual::Action::STOP || mActionStatus == DevelAnimatedImageVisual::Action::PAUSE )
+  {
+    return false;
+  }
   if( mFrameCount > 1 )
   {
     // Wrap the frame index
     ++mCurrentFrameIndex;
 
-    if( mLoopCount < 0 || mCurrentLoopIndex <= mLoopCount)
+    if( mLoopCount < 0 || mCurrentLoopIndex < mLoopCount)
     {
       mCurrentFrameIndex %= mFrameCount;
       if( mCurrentFrameIndex == 0 )
@@ -593,12 +642,14 @@ bool AnimatedImageVisual::DisplayNextFrame()
   }
 
   TextureSet textureSet;
-  if (mImageCache)
-    textureSet = mImageCache->NextFrame();
-  if( textureSet )
+  if( mImageCache )
   {
-    SetImageSize( textureSet );
-    mImpl->mRenderer.SetTextures( textureSet );
+    textureSet = mImageCache->NextFrame();
+    if( textureSet )
+    {
+      SetImageSize( textureSet );
+      mImpl->mRenderer.SetTextures( textureSet );
+    }
   }
 
   // Keep timer ticking