Force call KeepRendering when lottie animation stopped, or frame changed
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / animated-vector-image-visual.cpp
index 5d8a355..b23f066 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 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.
@@ -97,11 +97,13 @@ AnimatedVectorImageVisual::AnimatedVectorImageVisual(VisualFactoryCache& factory
   mPlacementActor(),
   mPlayState(DevelImageVisual::PlayState::STOPPED),
   mEventCallback(nullptr),
+  mLastSentPlayStateId(0u),
   mLoadFailed(false),
   mRendererAdded(false),
   mCoreShutdown(false),
   mRedrawInScalingDown(true),
-  mEnableFrameCache(false)
+  mEnableFrameCache(false),
+  mUseNativeImage(false)
 {
   // the rasterized image is with pre-multiplied alpha format
   mImpl->mFlags |= Visual::Base::Impl::IS_PREMULTIPLIED_ALPHA;
@@ -409,6 +411,7 @@ void AnimatedVectorImageVisual::OnInitialize(void)
 {
   mVectorAnimationTask->ResourceReadySignal().Connect(this, &AnimatedVectorImageVisual::OnResourceReady);
   mVectorAnimationTask->SetAnimationFinishedCallback(MakeCallback(this, &AnimatedVectorImageVisual::OnAnimationFinished));
+  mVectorAnimationTask->SetForceRenderOnceCallback(MakeCallback(this, &AnimatedVectorImageVisual::OnForceRendering));
 
   EncodedImageBuffer encodedImageBuffer;
 
@@ -457,6 +460,7 @@ void AnimatedVectorImageVisual::DoSetOnScene(Actor& actor)
     Vector2 imageSize = actor.GetProperty(Actor::Property::SIZE).Get<Vector2>();
     mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize, false);
     actor.AddRenderer(mImpl->mRenderer);
+    mRendererAdded = true;
     ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
   }
   else
@@ -563,11 +567,9 @@ void AnimatedVectorImageVisual::OnDoAction(const Property::Index actionId, const
     {
       if(IsOnScene() && mVisualSize != Vector2::ZERO)
       {
-        if(mAnimationData.playState != DevelImageVisual::PlayState::PLAYING)
-        {
-          mAnimationData.playState = DevelImageVisual::PlayState::PLAYING;
-          mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
-        }
+        // Always resend Playing state. If task is already playing, it will be ignored at Rasterize time.
+        mAnimationData.playState = DevelImageVisual::PlayState::PLAYING;
+        mAnimationData.resendFlag |= VectorAnimationTask::RESEND_PLAY_STATE;
       }
       mPlayState = DevelImageVisual::PlayState::PLAYING;
       break;
@@ -645,6 +647,29 @@ void AnimatedVectorImageVisual::OnResourceReady(VectorAnimationTask::ResourceSta
   else
   {
     mLoadFailed = status == VectorAnimationTask::ResourceStatus::FAILED ? true : false;
+    if(status == VectorAnimationTask::ResourceStatus::READY)
+    {
+      // Texture was ready. Change the shader if we need.
+      bool useNativeImage = false;
+      if(mImpl->mRenderer)
+      {
+        auto textureSet = mImpl->mRenderer.GetTextures();
+        if(textureSet && textureSet.GetTextureCount() > 0)
+        {
+          auto texture = textureSet.GetTexture(0u);
+          if(texture)
+          {
+            useNativeImage = DevelTexture::IsNative(texture);
+
+            if(mUseNativeImage != useNativeImage)
+            {
+              mUseNativeImage = useNativeImage;
+              UpdateShader();
+            }
+          }
+        }
+      }
+    }
 
     // If weak handle is holding a placement actor, it is the time to add the renderer to actor.
     Actor actor = mPlacementActor.GetHandle();
@@ -670,8 +695,14 @@ void AnimatedVectorImageVisual::OnResourceReady(VectorAnimationTask::ResourceSta
   DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "status = %d [%p]\n", status, this);
 }
 
-void AnimatedVectorImageVisual::OnAnimationFinished()
+void AnimatedVectorImageVisual::OnAnimationFinished(uint32_t playStateId)
 {
+  // Only send event when animation is finished by the last Play/Pause/Stop request.
+  if(mLastSentPlayStateId != playStateId)
+  {
+    return;
+  }
+
   AnimatedVectorImageVisualPtr self = this; // Keep reference until this API finished
 
   DALI_LOG_INFO(gVectorAnimationLogFilter, Debug::Verbose, "AnimatedVectorImageVisual::OnAnimationFinished: action state = %d [%p]\n", mPlayState, this);
@@ -694,10 +725,25 @@ void AnimatedVectorImageVisual::OnAnimationFinished()
   }
 }
 
+void AnimatedVectorImageVisual::OnForceRendering(uint32_t playStateId)
+{
+  if(!mCoreShutdown)
+  {
+    Stage::GetCurrent().KeepRendering(0.0f); // Trigger event processing
+  }
+}
+
 void AnimatedVectorImageVisual::SendAnimationData()
 {
   if(mAnimationData.resendFlag)
   {
+    if(mAnimationData.resendFlag & VectorAnimationTask::RESEND_PLAY_STATE)
+    {
+      // Keep last sent playId. It will be used when we try to emit AnimationFinished signal.
+      // The OnAnimationFinished signal what before Play/Pause/Stop action send could be come after action sent.
+      // To ensure the OnAnimationFinished signal comes belong to what we sent, we need to keep last sent playId.
+      mAnimationData.playStateId = ++mLastSentPlayStateId;
+    }
     mVectorAnimationTask->SetAnimationData(mAnimationData);
 
     if(mImpl->mRenderer)
@@ -850,7 +896,8 @@ Shader AnimatedVectorImageVisual::GenerateShader() const
       mFactoryCache,
       ImageVisualShaderFeatureBuilder()
         .EnableRoundedCorner(IsRoundedCornerRequired())
-        .EnableBorderline(IsBorderlineRequired()));
+        .EnableBorderline(IsBorderlineRequired())
+        .SetTextureForFragmentShaderCheck(mUseNativeImage ? mImpl->mRenderer.GetTextures().GetTexture(0) : Dali::Texture()));
   }
   return shader;
 }