Merge "Remove Atlas parameter for TextureManager cache system" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-animation-task.h
index 03b8a89..dcd66fc 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H
 
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 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.
 #include <dali/devel-api/adaptor-framework/event-thread-callback.h>
 #include <dali/devel-api/adaptor-framework/vector-animation-renderer.h>
 #include <dali/devel-api/threading/conditional-wait.h>
+#include <dali/public-api/adaptor-framework/async-task-manager.h>
+#include <dali/public-api/adaptor-framework/encoded-image-buffer.h>
+#include <dali/public-api/common/vector-wrapper.h>
 #include <dali/public-api/object/property-array.h>
 #include <chrono>
 #include <memory>
 
 // INTERNAL INCLUDES
+#include <dali-toolkit/devel-api/visuals/animated-vector-image-visual-actions-devel.h>
 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
+#include <dali-toolkit/internal/visuals/visual-url.h>
 
 namespace Dali
 {
@@ -42,23 +47,35 @@ typedef IntrusivePtr<VectorAnimationTask> VectorAnimationTaskPtr;
 /**
  * The task of the vector animation.
  */
-class VectorAnimationTask : public RefObject
+class VectorAnimationTask : public AsyncTask, public ConnectionTracker
 {
 public:
-  using UploadCompletedSignalType = Dali::VectorAnimationRenderer::UploadCompletedSignalType;
+  enum class ResourceStatus
+  {
+    LOADED, /// Resource is loaded
+    READY,  /// Resource is ready
+    FAILED  /// Resource is fail to load
+  };
+
+  using ResourceReadySignalType = Signal<void(ResourceStatus)>;
+
+  using TimePoint           = std::chrono::time_point<std::chrono::steady_clock>;
+  using DynamicPropertyType = std::vector<DevelAnimatedVectorImageVisual::DynamicPropertyInfo>;
 
   /**
    * Flags for re-sending data to the vector animation thread
    */
   enum ResendFlags
   {
-    RESEND_PLAY_RANGE    = 1 << 0,
-    RESEND_LOOP_COUNT    = 1 << 1,
-    RESEND_STOP_BEHAVIOR = 1 << 2,
-    RESEND_LOOPING_MODE  = 1 << 3,
-    RESEND_CURRENT_FRAME = 1 << 4,
-    RESEND_SIZE          = 1 << 5,
-    RESEND_PLAY_STATE    = 1 << 6
+    RESEND_PLAY_RANGE          = 1 << 0,
+    RESEND_LOOP_COUNT          = 1 << 1,
+    RESEND_STOP_BEHAVIOR       = 1 << 2,
+    RESEND_LOOPING_MODE        = 1 << 3,
+    RESEND_CURRENT_FRAME       = 1 << 4,
+    RESEND_SIZE                = 1 << 5,
+    RESEND_PLAY_STATE          = 1 << 6,
+    RESEND_NEED_RESOURCE_READY = 1 << 7,
+    RESEND_DYNAMIC_PROPERTY    = 1 << 8
   };
 
   /**
@@ -69,6 +86,7 @@ public:
     AnimationData()
     : resendFlag(0),
       playRange(),
+      dynamicProperties(),
       playState(),
       stopBehavior(DevelImageVisual::StopBehavior::CURRENT_FRAME),
       loopingMode(DevelImageVisual::LoopingMode::RESTART),
@@ -90,11 +108,13 @@ public:
       width        = rhs.width;
       height       = rhs.height;
       loopCount    = rhs.loopCount;
+      dynamicProperties.insert(dynamicProperties.end(), rhs.dynamicProperties.begin(), rhs.dynamicProperties.end());
       return *this;
     }
 
     uint32_t                             resendFlag;
     Property::Array                      playRange;
+    DynamicPropertyType                  dynamicProperties;
     DevelImageVisual::PlayState::Type    playState;
     DevelImageVisual::StopBehavior::Type stopBehavior;
     DevelImageVisual::LoopingMode::Type  loopingMode;
@@ -117,17 +137,20 @@ public:
   ~VectorAnimationTask() override;
 
   /**
-   * @brief Finalizes the task.
+   * Process the task accodring to the type
    */
-  void Finalize();
+  void Process() override;
 
   /**
-   * @brief Loads the animation file.
-   *
-   * @param[in] url The url of the vector animation file
-   * @return True if loading success, false otherwise.
+   * Whether the task is ready to process.
+   * @return True if the task is ready to process.
+   */
+  bool IsReady() override;
+
+  /**
+   * @brief Finalizes the task.
    */
-  bool Load(const std::string& url);
+  void Finalize();
 
   /**
    * @brief Sets the renderer used to display the result image.
@@ -137,6 +160,21 @@ public:
   void SetRenderer(Renderer renderer);
 
   /**
+   * @brief Requests to load the animation file.
+   *
+   * @param[in] url The url of the vector animation file
+   * @param[in] encodedImageBuffer The resource buffer if required.
+   * @param[in] synchronousLoading True if the url should be loaded synchronously
+   */
+  void RequestLoad(const VisualUrl& url, EncodedImageBuffer encodedImageBuffer, bool synchronousLoading);
+
+  /**
+   * @brief Queries whether loading is requested.
+   * @return True if loading is requested.
+   */
+  bool IsLoadRequested() const;
+
+  /**
    * @brief Sets data to specify animation playback.
    * @param[in] data The animation data
    */
@@ -146,7 +184,7 @@ public:
    * @brief This callback is called after the animation is finished.
    * @param[in] callback The animation finished callback
    */
-  void SetAnimationFinishedCallback(EventThreadCallback* callback);
+  void SetAnimationFinishedCallback(CallbackBase* callback);
 
   /**
    * @brief Gets the playing range in frame number.
@@ -180,14 +218,20 @@ public:
   void GetLayerInfo(Property::Map& map) const;
 
   /**
-   * @brief Connect to this signal to be notified when the texture upload is completed.
+   * @brief Gets the all marker information.
+   * @param[out] map The marker information
+   */
+  void GetMarkerInfo(Property::Map& map) const;
+
+  /**
+   * @brief Connect to this signal to be notified when the resource is ready.
    * @return The signal to connect to.
    */
-  UploadCompletedSignalType& UploadCompletedSignal();
+  ResourceReadySignalType& ResourceReadySignal();
 
   /**
    * @brief Rasterizes the current frame.
-   * @return true if the animation is running, false otherwise.
+   * @return true if the rasterization succeeded, false otherwise.
    */
   bool Rasterize();
 
@@ -195,16 +239,42 @@ public:
    * @brief Calculates the time for the next frame rasterization.
    * @return The time for the next frame rasterization.
    */
-  std::chrono::time_point<std::chrono::system_clock> CalculateNextFrameTime(bool renderNow);
+  TimePoint CalculateNextFrameTime(bool renderNow);
 
   /**
    * @brief Gets the time for the next frame rasterization.
    * @return The time for the next frame rasterization.
    */
-  std::chrono::time_point<std::chrono::system_clock> GetNextFrameTime();
+  TimePoint GetNextFrameTime();
+
+  /**
+   * @brief Called when the rasterization is completed from the asyncTaskManager
+   * @param[in] task The completed task
+   */
+  void TaskCompleted(VectorAnimationTaskPtr task);
+
+  /**
+   * @brief Check the rasterization succeeded
+   * @return true if the rasterization succeeded, false otherwise.
+   */
+  bool IsRasterized();
+
+  /**
+   * @brief Check the animation is running
+   * @return true if the animation is running, false otherwise.
+   */
+  bool IsAnimating();
 
 private:
   /**
+   * @brief Loads the animation file.
+   *
+   * @param[in] synchronousLoading True if loading is requested synchronously
+   * @return True if loading succeeded, false otherwise.
+   */
+  bool Load(bool synchronousLoading);
+
+  /**
    * @brief Play the vector animation.
    */
   void PlayAnimation();
@@ -269,6 +339,16 @@ private:
    */
   void ApplyAnimationData();
 
+  /**
+   * @brief Called when the texture upload is completed.
+   */
+  void OnUploadCompleted();
+
+  /**
+   * @brief Event callback from rasterize thread. This is called when the file loading is completed.
+   */
+  void OnLoadCompleted();
+
   // Undefined
   VectorAnimationTask(const VectorAnimationTask& task) = delete;
 
@@ -284,32 +364,44 @@ private:
     PAUSED    ///< The animation is paused
   };
 
-  std::string                                        mUrl;
-  VectorAnimationRenderer                            mVectorRenderer;
-  AnimationData                                      mAnimationData[2];
-  VectorAnimationThread&                             mVectorAnimationThread;
-  ConditionalWait                                    mConditionalWait;
-  std::unique_ptr<EventThreadCallback>               mAnimationFinishedTrigger;
-  PlayState                                          mPlayState;
-  DevelImageVisual::StopBehavior::Type               mStopBehavior;
-  DevelImageVisual::LoopingMode::Type                mLoopingMode;
-  std::chrono::time_point<std::chrono::system_clock> mNextFrameStartTime;
-  int64_t                                            mFrameDurationNanoSeconds;
-  float                                              mFrameRate;
-  uint32_t                                           mCurrentFrame;
-  uint32_t                                           mTotalFrame;
-  uint32_t                                           mStartFrame;
-  uint32_t                                           mEndFrame;
-  uint32_t                                           mWidth;
-  uint32_t                                           mHeight;
-  uint32_t                                           mAnimationDataIndex;
-  int32_t                                            mLoopCount;
-  int32_t                                            mCurrentLoop;
-  bool                                               mForward;
-  bool                                               mUpdateFrameNumber;
-  bool                                               mNeedAnimationFinishedTrigger;
-  bool                                               mAnimationDataUpdated;
-  bool                                               mDestroyTask;
+  VisualUrl                            mImageUrl;
+  EncodedImageBuffer                   mEncodedImageBuffer;
+  VectorAnimationRenderer              mVectorRenderer;
+  std::vector<AnimationData>           mAnimationData[2];
+  VectorAnimationThread&               mVectorAnimationThread;
+  ConditionalWait                      mConditionalWait;
+  ResourceReadySignalType              mResourceReadySignal;
+  std::unique_ptr<CallbackBase>        mAnimationFinishedCallback{};
+  std::unique_ptr<CallbackBase>        mLoadCompletedCallback{};
+  mutable Property::Map                mCachedLayerInfo;
+  mutable Property::Map                mCachedMarkerInfo;
+  PlayState                            mPlayState;
+  DevelImageVisual::StopBehavior::Type mStopBehavior;
+  DevelImageVisual::LoopingMode::Type  mLoopingMode;
+  TimePoint                            mNextFrameStartTime;
+  int64_t                              mFrameDurationMicroSeconds;
+  float                                mFrameRate;
+  uint32_t                             mCurrentFrame;
+  uint32_t                             mTotalFrame;
+  uint32_t                             mStartFrame;
+  uint32_t                             mEndFrame;
+  uint32_t                             mDroppedFrames;
+  uint32_t                             mWidth;
+  uint32_t                             mHeight;
+  uint32_t                             mAnimationDataIndex;
+  int32_t                              mLoopCount;
+  int32_t                              mCurrentLoop;
+  bool                                 mForward : 1;
+  bool                                 mUpdateFrameNumber : 1;
+  bool                                 mNeedAnimationFinishedTrigger : 1;
+  bool                                 mAnimationDataUpdated : 1;
+  bool                                 mDestroyTask : 1;
+  bool                                 mLoadRequest : 1;
+  bool                                 mLoadFailed : 1;
+  bool                                 mRasterized : 1;
+  bool                                 mKeepAnimation : 1;
+  mutable bool                         mLayerInfoCached : 1;
+  mutable bool                         mMarkerInfoCached : 1;
 };
 
 } // namespace Internal