Check renderer when call DisplayNextFrame()
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / animated-image-visual.cpp
index 6b7adf3..5a83d92 100755 (executable)
@@ -82,7 +82,7 @@ Debug::Filter* gAnimImgLogFilter = Debug::Filter::New(Debug::NoLogging, false, "
  *   |     new cache
  *   |       cache->LoadBatch()
  *   |
- *   | DoSetOnStage()
+ *   | DoSetOnScene()
  *   |   PrepareTextureSet()
  *   |     cache->FirstFrame()
  *   |   CreateRenderer()    (Doesn't become ready until first frame loads)
@@ -174,7 +174,7 @@ AnimatedImageVisual::AnimatedImageVisual( VisualFactoryCache& factoryCache, Imag
   mPixelArea( FULL_TEXTURE_RECT ),
   mImageUrl(),
   mAnimatedImageLoading(),
-  mCurrentFrameIndex( 0 ),
+  mFrameIndexForJumpTo( 0 ),
   mImageUrls( NULL ),
   mImageCache( NULL ),
   mCacheSize( 2 ),
@@ -272,7 +272,7 @@ void AnimatedImageVisual::OnDoAction( const Dali::Property::Index actionId, cons
     }
     case DevelAnimatedImageVisual::Action::PLAY:
     {
-      if( mFrameDelayTimer && IsOnStage() && mActionStatus != DevelAnimatedImageVisual::Action::PLAY )
+      if( mFrameDelayTimer && IsOnScene() && mActionStatus != DevelAnimatedImageVisual::Action::PLAY )
       {
         mFrameDelayTimer.Start();
       }
@@ -284,7 +284,7 @@ void AnimatedImageVisual::OnDoAction( const Dali::Property::Index actionId, cons
       // STOP reset functionality will actually be done in a future change
       // Stop will be executed on next timer tick
       mActionStatus = DevelAnimatedImageVisual::Action::STOP;
-      if( IsOnStage() )
+      if( IsOnScene() )
       {
         DisplayNextFrame();
       }
@@ -302,8 +302,8 @@ void AnimatedImageVisual::OnDoAction( const Dali::Property::Index actionId, cons
         else
         {
           mIsJumpTo = true;
-          mCurrentFrameIndex = frameNumber;
-          if( IsOnStage() )
+          mFrameIndexForJumpTo = frameNumber;
+          if( IsOnScene() )
           {
             DisplayNextFrame();
           }
@@ -481,7 +481,7 @@ void AnimatedImageVisual::DoSetProperty( Property::Index index,
   }
 }
 
-void AnimatedImageVisual::DoSetOnStage( Actor& actor )
+void AnimatedImageVisual::DoSetOnScene( Actor& actor )
 {
   mPlacementActor = actor;
   TextureSet textureSet = PrepareTextureSet();
@@ -497,7 +497,7 @@ void AnimatedImageVisual::DoSetOnStage( Actor& actor )
   }
 }
 
-void AnimatedImageVisual::DoSetOffStage( Actor& actor )
+void AnimatedImageVisual::DoSetOffScene( Actor& actor )
 {
   DALI_ASSERT_DEBUG( (bool)mImpl->mRenderer && "There should always be a renderer whilst on stage");
 
@@ -510,6 +510,7 @@ void AnimatedImageVisual::DoSetOffStage( Actor& actor )
   actor.RemoveRenderer( mImpl->mRenderer );
   mImpl->mRenderer.Reset();
   mPlacementActor.Reset();
+  mStartFirstFrame = false;
 }
 
 void AnimatedImageVisual::OnSetTransform()
@@ -544,8 +545,6 @@ void AnimatedImageVisual::CreateRenderer()
   {
     mImpl->mRenderer.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, mPixelArea );
   }
-
-  mCurrentFrameIndex = 0;
 }
 
 void AnimatedImageVisual::LoadFirstBatch()
@@ -610,22 +609,21 @@ void AnimatedImageVisual::StartFirstFrame( TextureSet& textureSet )
   if(mImpl->mRenderer)
   {
     mImpl->mRenderer.SetTextures( textureSet );
-  }
-  Actor actor = mPlacementActor.GetHandle();
-  if( actor )
-  {
-    actor.AddRenderer( mImpl->mRenderer );
-    mPlacementActor.Reset();
-  }
 
-  mCurrentFrameIndex = 0;
+    Actor actor = mPlacementActor.GetHandle();
+    if( actor )
+    {
+      actor.AddRenderer( mImpl->mRenderer );
+      mPlacementActor.Reset();
+    }
+  }
 
   if( mFrameCount > 1 )
   {
-    int frameDelay = mFrameDelay; // from URL array
-    if( mAnimatedImageLoading && mImageCache )
+    int frameDelay = mImageCache->GetFrameInterval( 0 );
+    if( frameDelay == 0u )
     {
-      frameDelay = mImageCache->GetFrameInterval( 0 );
+      frameDelay = mFrameDelay; // from URL array
     }
     mFrameDelayTimer = Timer::New( frameDelay );
     mFrameDelayTimer.TickSignal().Connect( this, &AnimatedImageVisual::DisplayNextFrame );
@@ -642,15 +640,11 @@ TextureSet AnimatedImageVisual::PrepareTextureSet()
   {
     textureSet = mImageCache->FirstFrame();
   }
+
   if( textureSet )
   {
     SetImageSize( textureSet );
   }
-  else
-  {
-    DALI_LOG_INFO( gAnimImgLogFilter, Debug::Concise, "ResourceReady(ResourceStatus::FAILED)\n" );
-    ResourceReady( Toolkit::Visual::ResourceStatus::FAILED );
-  }
 
   return textureSet;
 }
@@ -670,99 +664,118 @@ void AnimatedImageVisual::SetImageSize( TextureSet& textureSet )
 
 void AnimatedImageVisual::FrameReady( TextureSet textureSet )
 {
-  SetImageSize( textureSet );
-
-  if( mStartFirstFrame )
+  if(textureSet)
   {
-    StartFirstFrame( textureSet );
+    SetImageSize(textureSet);
+
+    if(mStartFirstFrame)
+    {
+      StartFirstFrame(textureSet);
+    }
+    else
+    {
+      if(mImpl->mRenderer)
+      {
+        mImpl->mRenderer.SetTextures(textureSet);
+      }
+    }
   }
   else
   {
-    if( mImpl->mRenderer )
-    {
-      mImpl->mRenderer.SetTextures( textureSet );
-    }
+    DALI_LOG_INFO( gAnimImgLogFilter, Debug::Concise, "ResourceReady(ResourceStatus::FAILED)\n" );
+    ResourceReady( Toolkit::Visual::ResourceStatus::FAILED );
   }
 }
 
 bool AnimatedImageVisual::DisplayNextFrame()
 {
-  if( mIsJumpTo )
-  {
-    mIsJumpTo = false;
-  }
-  else if( mActionStatus == DevelAnimatedImageVisual::Action::PAUSE )
-  {
-    return false;
-  }
-  else if( mActionStatus == DevelAnimatedImageVisual::Action::STOP )
+  bool continueTimer = false;
+
+  if(mImageCache)
   {
-    mCurrentLoopIndex = 0;
-    if( mStopBehavior == DevelImageVisual::StopBehavior::FIRST_FRAME )
+    bool nextFrame = false;
+    uint32_t frameIndex = mImageCache->GetCurrentFrameIndex();
+
+    if( mIsJumpTo )
     {
-      mCurrentFrameIndex = 0;
+      mIsJumpTo = false;
+      frameIndex = mFrameIndexForJumpTo;
     }
-    else if( mStopBehavior == DevelImageVisual::StopBehavior::LAST_FRAME )
+    else if( mActionStatus == DevelAnimatedImageVisual::Action::PAUSE )
     {
-      mCurrentFrameIndex = mFrameCount - 1;
+      return false;
     }
-    else
+    else if( mActionStatus == DevelAnimatedImageVisual::Action::STOP )
     {
-      return false; // Do not draw already rendered scene twice.
-    }
-  }
-  else
-  {
-    if( mFrameCount > 1 )
-    {
-      // Wrap the frame index
-      bool finished = false;
-      ++mCurrentFrameIndex;
-      if( mCurrentFrameIndex >= mFrameCount )
+      frameIndex = 0;
+      if( mStopBehavior == DevelImageVisual::StopBehavior::FIRST_FRAME )
       {
-        ++mCurrentLoopIndex;
-        finished = true;
+        frameIndex = 0;
       }
-
-      if( mLoopCount < 0 || mCurrentLoopIndex < mLoopCount)
+      else if( mStopBehavior == DevelImageVisual::StopBehavior::LAST_FRAME )
       {
-        if( finished )
-        {
-          mCurrentFrameIndex = 0; // Back to the first frame
-        }
+        frameIndex = mFrameCount - 1;
       }
       else
       {
-        // This will stop timer
-        mActionStatus = DevelAnimatedImageVisual::Action::STOP;
-        return DisplayNextFrame();
+        return false; // Do not draw already rendered scene twice.
       }
     }
-    // TODO : newly added one.
-    if( mAnimatedImageLoading && mImageCache )
+    else
     {
-      unsigned int delay = mImageCache->GetFrameInterval( mCurrentFrameIndex );
-      if( mFrameDelayTimer.GetInterval() != delay )
+      if( mFrameCount > 1 )
       {
-        mFrameDelayTimer.SetInterval( delay );
+        nextFrame = true;
+        frameIndex++;
+        if( frameIndex >= mFrameCount )
+        {
+          frameIndex %= mFrameCount;
+          ++mCurrentLoopIndex;
+        }
+
+        if(mLoopCount >= 0 && mCurrentLoopIndex >= mLoopCount)
+        {
+          // This will stop timer
+          mActionStatus = DevelAnimatedImageVisual::Action::STOP;
+          return DisplayNextFrame();
+        }
+      }
+
+      unsigned int delay = mImageCache->GetFrameInterval( frameIndex );
+      if( delay > 0u )
+      {
+        if( mFrameDelayTimer.GetInterval() != delay )
+        {
+          mFrameDelayTimer.SetInterval( delay );
+        }
       }
     }
-  }
 
-  DALI_LOG_INFO( gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::DisplayNextFrame(this:%p) CurrentFrameIndex:%d\n", this, mCurrentFrameIndex);
+    DALI_LOG_INFO( gAnimImgLogFilter,Debug::Concise,"AnimatedImageVisual::DisplayNextFrame(this:%p) CurrentFrameIndex:%d\n", this, frameIndex);
+
+    TextureSet textureSet;
+    if(nextFrame)
+    {
+      textureSet = mImageCache->NextFrame();
+    }
+    else
+    {
+      textureSet = mImageCache->Frame( frameIndex );
+    }
 
-  TextureSet textureSet;
-  if( mImageCache )
-  {
-    textureSet = mImageCache->Frame( mCurrentFrameIndex );
     if( textureSet )
     {
       SetImageSize( textureSet );
-      mImpl->mRenderer.SetTextures( textureSet );
+      if( mImpl->mRenderer )
+      {
+        mImpl->mRenderer.SetTextures( textureSet );
+      }
     }
+
+    continueTimer = ( mActionStatus == DevelAnimatedImageVisual::Action::PLAY ) ? true : false;
   }
 
-  return ( mActionStatus == DevelAnimatedImageVisual::Action::PLAY ) ? true : false;
+  return continueTimer;
 }