(Vector) Support asynchronous file loading
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-animation-task.h
index c13b4d3..fb4a55c 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_TOOLKIT_VECTOR_ANIMATION_TASK_H
 
 /*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 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 <memory>
+#include <dali/public-api/object/property-array.h>
 #include <chrono>
+#include <memory>
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
 
 namespace Dali
 {
-
 namespace Toolkit
 {
-
 namespace Internal
 {
-
 class VisualFactoryCache;
 class VectorAnimationThread;
 class VectorAnimationTask;
-typedef IntrusivePtr< VectorAnimationTask > VectorAnimationTaskPtr;
+typedef IntrusivePtr<VectorAnimationTask> VectorAnimationTaskPtr;
 
 /**
  * The task of the vector animation.
  */
-class VectorAnimationTask : public RefObject
+class VectorAnimationTask : public RefObject, public ConnectionTracker
 {
 public:
+  using ResourceReadySignalType = Signal<void(bool)>;
+
+  using TimePoint = std::chrono::time_point<std::chrono::steady_clock>;
 
-  using UploadCompletedSignalType = Dali::VectorAnimationRenderer::UploadCompletedSignalType;
+  /**
+   * 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_NEED_RESOURCE_READY = 1 << 7
+  };
+
+  /**
+   * @brief Structure used to pass parameters to the vector animation task
+   */
+  struct AnimationData
+  {
+    AnimationData()
+    : resendFlag(0),
+      playRange(),
+      playState(),
+      stopBehavior(DevelImageVisual::StopBehavior::CURRENT_FRAME),
+      loopingMode(DevelImageVisual::LoopingMode::RESTART),
+      currentFrame(0),
+      width(0),
+      height(0),
+      loopCount(-1)
+    {
+    }
+
+    AnimationData& operator=(const AnimationData& rhs)
+    {
+      resendFlag |= rhs.resendFlag; // OR resend flag
+      playRange    = rhs.playRange;
+      playState    = rhs.playState;
+      stopBehavior = rhs.stopBehavior;
+      loopingMode  = rhs.loopingMode;
+      currentFrame = rhs.currentFrame;
+      width        = rhs.width;
+      height       = rhs.height;
+      loopCount    = rhs.loopCount;
+      return *this;
+    }
+
+    uint32_t                             resendFlag;
+    Property::Array                      playRange;
+    DevelImageVisual::PlayState::Type    playState;
+    DevelImageVisual::StopBehavior::Type stopBehavior;
+    DevelImageVisual::LoopingMode::Type  loopingMode;
+    uint32_t                             currentFrame;
+    uint32_t                             width;
+    uint32_t                             height;
+    int32_t                              loopCount;
+  };
 
   /**
    * @brief Constructor.
    *
    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
-   * @param[in] url The url of the vector animation file
    */
-  VectorAnimationTask( VisualFactoryCache& factoryCache, const std::string& url );
+  VectorAnimationTask(VisualFactoryCache& factoryCache);
 
   /**
    * @brief Destructor.
    */
-  virtual ~VectorAnimationTask();
+  ~VectorAnimationTask() override;
+
+  /**
+   * @brief Finalizes the task.
+   */
+  void Finalize();
 
   /**
    * @brief Sets the renderer used to display the result image.
    *
    * @param[in] renderer The renderer used to display the result image
    */
-  void SetRenderer( Renderer renderer );
+  void SetRenderer(Renderer renderer);
 
   /**
-   * @brief Sets the target image size.
+   * @brief Request to load the animation file.
    *
-   * @param[in] width The target image width
-   * @param[in] height The target image height
+   * @param[in] url The url of the vector animation file
    */
-  void SetSize( uint32_t width, uint32_t height );
+  void RequestLoad(const std::string& url);
 
   /**
-   * @brief Play the vector animation.
+   * @brief Sets data to specify animation playback.
+   * @param[in] data The animation data
    */
-  void PlayAnimation();
+  void SetAnimationData(const AnimationData& data);
 
   /**
-   * @brief Stop the vector animation.
+   * @brief This callback is called after the animation is finished.
+   * @param[in] callback The animation finished callback
    */
-  void StopAnimation();
+  void SetAnimationFinishedCallback(EventThreadCallback* callback);
 
   /**
-   * @brief Pause the vector animation.
+   * @brief Gets the playing range in frame number.
+   * @param[out] startFrame The frame number to specify minimum progress.
+   * @param[out] endFrame The frame number to specify maximum progress.
    */
-  void PauseAnimation();
+  void GetPlayRange(uint32_t& startFrame, uint32_t& endFrame);
 
   /**
-   * @brief Render one frame. The current frame number will be increased.
+   * @brief Retrieves the current frame number of the animation.
+   * @return The current frame number
    */
-  void RenderFrame();
+  uint32_t GetCurrentFrameNumber() const;
 
   /**
-   * @brief This callback is called after the animation is finished.
-   * @param[in] callback The animation finished callback
+   * @brief Retrieves the total frame number of the animation.
+   * @return The total frame number
    */
-  void SetAnimationFinishedCallback( EventThreadCallback* callback );
+  uint32_t GetTotalFrameNumber() const;
 
   /**
-   * @brief Enable looping for 'count' repeats. -1 means to repeat forever.
-   * @param[in] count The number of times to loop
+   * @brief Gets the default size of the file,.
+   * @return The default size of the file
    */
-  void SetLoopCount( int32_t count );
+  void GetDefaultSize(uint32_t& width, uint32_t& height) const;
 
   /**
-   * @brief Set the playing range in frame number.
-   * @param[in] startFrame The frame number to specify minimum progress.
-   * @param[in] endFrame The frame number to specify maximum progress.
-   * The animation will play between those values.
+   * @brief Gets the layer information of all the child layers.
+   * @param[out] map The layer information
    */
-  void SetPlayRange( uint32_t startFrame, uint32_t endFrame );
+  void GetLayerInfo(Property::Map& map) const;
 
   /**
-   * @brief Gets the playing range in frame number.
-   * @param[out] startFrame The frame number to specify minimum progress.
-   * @param[out] endFrame The frame number to specify maximum progress.
+   * @brief Connect to this signal to be notified when the resource is ready.
+   * @return The signal to connect to.
    */
-  void GetPlayRange( uint32_t& startFrame, uint32_t& endFrame );
+  ResourceReadySignalType& ResourceReadySignal();
 
   /**
-   * @brief Get the play state
-   * @return The play state
+   * @brief Rasterizes the current frame.
+   * @param[out] keepAnimation true if the animation is running, false otherwise.
+   * @return true if the rasterization succeeded, false otherwise.
    */
-  DevelImageVisual::PlayState::Type GetPlayState() const;
+  bool Rasterize(bool& keepAnimation);
 
   /**
-   * @brief Sets the current frame number of the animation.
-   * @param[in] frameNumber The new frame number between [0, the maximum frame number] or between the play range if specified.
+   * @brief Calculates the time for the next frame rasterization.
+   * @return The time for the next frame rasterization.
    */
-  void SetCurrentFrameNumber( uint32_t frameNumber );
+  TimePoint CalculateNextFrameTime(bool renderNow);
 
   /**
-   * @brief Retrieves the current frame number of the animation.
-   * @return The current frame number
+   * @brief Gets the time for the next frame rasterization.
+   * @return The time for the next frame rasterization.
    */
-  uint32_t GetCurrentFrameNumber() const;
+  TimePoint GetNextFrameTime();
 
+private:
   /**
-   * @brief Retrieves the total frame number of the animation.
-   * @return The total frame number
+   * @brief Loads the animation file.
+   *
+   * @return True if loading succeeded, false otherwise.
    */
-  uint32_t GetTotalFrameNumber() const;
+  bool Load();
 
   /**
-   * @brief Gets the default size of the file,.
-   * @return The default size of the file
+   * @brief Play the vector animation.
    */
-  void GetDefaultSize( uint32_t& width, uint32_t& height ) const;
+  void PlayAnimation();
 
   /**
-   * @brief Sets the stop behavior of the animation. This is performed when the animation is stopped.
-   * @param[in] stopBehavior The stop behavior
+   * @brief Stop the vector animation.
    */
-  void SetStopBehavior( DevelImageVisual::StopBehavior::Type stopBehavior );
+  void StopAnimation();
 
   /**
-   * @brief Sets the looping mode.
-   * Animation plays forwards and then restarts from the beginning or runs backwards again.
-   * @param[in] loopingMode The looping mode
+   * @brief Pause the vector animation.
    */
-  void SetLoopingMode( DevelImageVisual::LoopingMode::Type loopingMode );
+  void PauseAnimation();
 
   /**
-   * @brief Connect to this signal to be notified when the texture upload is completed.
-   * @return The signal to connect to.
+   * @brief Sets the target image size.
+   *
+   * @param[in] width The target image width
+   * @param[in] height The target image height
    */
-  UploadCompletedSignalType& UploadCompletedSignal();
+  void SetSize(uint32_t width, uint32_t height);
 
   /**
-   * @brief Rasterizes the current frame.
-   * @return true if the animation is running, false otherwise.
+   * @brief Enable looping for 'count' repeats. -1 means to repeat forever.
+   * @param[in] count The number of times to loop
    */
-  bool Rasterize();
+  void SetLoopCount(int32_t count);
 
   /**
-   * @brief Calculates the time for the next frame rasterization.
-   * @return The time for the next frame rasterization.
+   * @brief Set the playing range in frame number.
+   * @param[in] playRange The array to specify minimum and maximum progress.
+   * The animation will play between those values.
    */
-  std::chrono::time_point< std::chrono::system_clock > CalculateNextFrameTime( bool renderNow );
+  void SetPlayRange(const Property::Array& playRange);
 
   /**
-   * @brief Gets the time for the next frame rasterization.
-   * @return The time for the next frame rasterization.
+   * @brief Sets the current frame number of the animation.
+   * @param[in] frameNumber The new frame number between [0, the maximum frame number] or between the play range if specified.
    */
-  std::chrono::time_point< std::chrono::system_clock > GetNextFrameTime();
+  void SetCurrentFrameNumber(uint32_t frameNumber);
 
-private:
+  /**
+   * @brief Sets the stop behavior of the animation. This is performed when the animation is stopped.
+   * @param[in] stopBehavior The stop behavior
+   */
+  void SetStopBehavior(DevelImageVisual::StopBehavior::Type stopBehavior);
 
   /**
-   * @brief Initializes the vector renderer.
+   * @brief Sets the looping mode.
+   * Animation plays forwards and then restarts from the beginning or runs backwards again.
+   * @param[in] loopingMode The looping mode
    */
-  void Initialize();
+  void SetLoopingMode(DevelImageVisual::LoopingMode::Type loopingMode);
 
   /**
    * @brief Gets the frame number when the animation is stopped according to the stop behavior.
    */
-  uint32_t GetStoppedFrame( uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame );
+  uint32_t GetStoppedFrame(uint32_t startFrame, uint32_t endFrame, uint32_t currentFrame);
+
+  /**
+   * @brief Applies the animation data set by the main thread.
+   */
+  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;
+  VectorAnimationTask(const VectorAnimationTask& task) = delete;
 
   // Undefined
-  VectorAnimationTask& operator=( const VectorAnimationTask& task ) = delete;
+  VectorAnimationTask& operator=(const VectorAnimationTask& task) = delete;
 
 private:
-
   enum class PlayState
   {
-    STOPPING,  ///< The animation is stopping
-    STOPPED,   ///< The animation has stopped
-    PLAYING,   ///< The animation is playing
-    PAUSED     ///< The animation is paused
+    STOPPING, ///< The animation is stopping
+    STOPPED,  ///< The animation has stopped
+    PLAYING,  ///< The animation is playing
+    PAUSED    ///< The animation is paused
   };
 
-  std::string                            mUrl;
-  VectorAnimationRenderer                mVectorRenderer;
-  VectorAnimationThread&                 mVectorAnimationThread;
-  ConditionalWait                        mConditionalWait;
-  std::unique_ptr< EventThreadCallback > mAnimationFinishedTrigger;
-  Vector2                                mPlayRange;
-  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;
-  int32_t                                mLoopCount;
-  int32_t                                mCurrentLoop;
-  bool                                   mResourceReady;
-  bool                                   mCurrentFrameUpdated;
-  bool                                   mCurrentLoopUpdated;
-  bool                                   mForward;
-  bool                                   mUpdateFrameNumber;
-  bool                                   mNeedAnimationFinishedTrigger;
-
+  std::string                          mUrl;
+  VectorAnimationRenderer              mVectorRenderer;
+  AnimationData                        mAnimationData[2];
+  VectorAnimationThread&               mVectorAnimationThread;
+  ConditionalWait                      mConditionalWait;
+  ResourceReadySignalType              mResourceReadySignal;
+  std::unique_ptr<EventThreadCallback> mAnimationFinishedTrigger;
+  std::unique_ptr<EventThreadCallback> mLoadCompletedTrigger;
+  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;
+  bool                                 mUpdateFrameNumber;
+  bool                                 mNeedAnimationFinishedTrigger;
+  bool                                 mAnimationDataUpdated;
+  bool                                 mDestroyTask;
+  bool                                 mLoadRequest;
+  bool                                 mLoadFailed;
 };
 
 } // namespace Internal