[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-animation-thread.h
old mode 100755 (executable)
new mode 100644 (file)
index 964f662..8b820cc
@@ -2,7 +2,7 @@
 #define DALI_TOOLKIT_VECTOR_ANIMATION_THREAD_H
 
 /*
- * Copyright (c) 2019 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.
  */
 
 // EXTERNAL INCLUDES
-#include <memory>
-#include <dali/public-api/signals/connection-tracker.h>
 #include <dali/devel-api/threading/conditional-wait.h>
+#include <dali/devel-api/threading/mutex.h>
 #include <dali/devel-api/threading/thread.h>
 #include <dali/integration-api/adaptor-framework/log-factory-interface.h>
+#include <dali/integration-api/adaptor-framework/trace-factory-interface.h>
+#include <dali/public-api/adaptor-framework/round-robin-container-view.h>
+#include <dali/public-api/signals/connection-tracker.h>
+#include <memory>
 
 // INTERNAL INCLUDES
-#include <dali-toolkit/internal/helpers/round-robin-container-view.h>
 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-task.h>
-#include <dali-toolkit/internal/visuals/animated-vector-image/vector-rasterize-thread.h>
 
 namespace Dali
 {
-
 namespace Toolkit
 {
-
 namespace Internal
 {
-
 /**
  * The main animation thread for vector animations
  */
 class VectorAnimationThread : public Thread
 {
 public:
-
   /**
    * @brief Constructor.
    */
@@ -53,80 +50,72 @@ public:
   /**
    * @brief Destructor.
    */
-  virtual ~VectorAnimationThread();
+  ~VectorAnimationThread() override;
 
   /**
-   * Add a animation task into the vector animation thread, called by main thread.
+   * @brief Add a animation task into the vector animation thread, called by main thread.
    *
    * @param[in] task The task added to the thread.
    */
-  void AddTask( VectorAnimationTaskPtr task );
+  void AddTask(VectorAnimationTaskPtr task);
 
   /**
    * @brief Called when the rasterization is completed from the rasterize thread.
-   * @param task The completed task
+   *
+   * @param[in] task The completed task
+   * @param[in] success true if the task succeeded, false otherwise.
+   * @param[in] keepAnimation true if the animation is running, false otherwise.
    */
-  void OnTaskCompleted( VectorAnimationTaskPtr task, bool stopped );
+  void OnTaskCompleted(VectorAnimationTaskPtr task, bool success, bool keepAnimation);
 
   /**
    * @brief Called when the sleep thread is awaken.
    */
   void OnAwakeFromSleep();
 
-protected:
+  /**
+   * @brief Add an event trigger callback.
+   *
+   * @param callback The callback to add
+   * @param argument The argument to pass to the callback
+   * @note Ownership of the callback is NOT passed onto this class.
+   * @note The callback will be excuted in the main thread.
+   */
+  void AddEventTriggerCallback(CallbackBase* callback, uint32_t argument);
 
   /**
+   * @brief Remove event trigger callbacks what we added before.
+   *
+   * @param callback The callback to remove
+   */
+  void RemoveEventTriggerCallbacks(CallbackBase* callback);
+
+  /**
+   * @brief Request to event callback from rasterize thread. This is called when we want to ensure rendering next frame.
+   */
+  void RequestForceRenderOnce();
+
+protected:
+  /**
    * @brief The entry function of the animation thread.
    */
   void Run() override;
 
 private:
-
   /**
-   * Rasterizes the tasks.
+   * @brief Rasterizes the tasks.
    */
   void Rasterize();
 
-private:
-
   /**
-   * @brief Helper class to keep the relation between VectorRasterizeThread and corresponding container
+   * @brief Called when the event callback is triggered.
    */
-  class RasterizeHelper : public ConnectionTracker
-  {
-  public:
-    /**
-     * @brief Create an RasterizeHelper.
-     *
-     * @param[in] animationThread Reference to the VectorAnimationThread
-     */
-    RasterizeHelper( VectorAnimationThread& animationThread );
-
-    /**
-     * @brief Rasterizes the task.
-     *
-     * @param[in] task The task to rasterize.
-     */
-    void Rasterize( VectorAnimationTaskPtr task );
-
-  public:
-    RasterizeHelper( const RasterizeHelper& ) = delete;
-    RasterizeHelper& operator=( const RasterizeHelper& ) = delete;
-
-    RasterizeHelper( RasterizeHelper&& rhs );
-    RasterizeHelper& operator=( RasterizeHelper&& rhs ) = delete;
-
-  private:
-
-    /**
-     * @brief Main constructor that used by all other constructors
-     */
-    RasterizeHelper( std::unique_ptr< VectorRasterizeThread > rasterizer, VectorAnimationThread& animationThread );
+  void OnEventCallbackTriggered();
 
-  private:
-    std::unique_ptr< VectorRasterizeThread > mRasterizer;
-    VectorAnimationThread&                   mAnimationThread;
-  };
+  /**
+   * @brief Gets next event callback to process.
+   */
+  std::pair<CallbackBase*, uint32_t> GetNextEventCallback();
 
   /**
    * @brief The thread to sleep until the next frame time.
@@ -134,63 +123,66 @@ private:
   class SleepThread : public Thread
   {
   public:
-
     /**
      * @brief Constructor.
      */
-    SleepThread( CallbackBase* callback );
+    SleepThread(CallbackBase* callback);
 
     /**
      * @brief Destructor.
      */
-    virtual ~SleepThread();
+    ~SleepThread() override;
 
     /**
      * @brief Sleeps untile the specified time point.
      */
-    void SleepUntil( std::chrono::time_point< std::chrono::system_clock > timeToSleepUntil );
+    void SleepUntil(std::chrono::time_point<std::chrono::steady_clock> timeToSleepUntil);
 
   protected:
-
     /**
      * @brief The entry function of the animation thread.
      */
     void Run() override;
 
   private:
-
-    SleepThread( const SleepThread& thread ) = delete;
-    SleepThread& operator=( const SleepThread& thread ) = delete;
+    SleepThread(const SleepThread& thread) = delete;
+    SleepThread& operator=(const SleepThread& thread) = delete;
 
   private:
-    ConditionalWait                  mConditionalWait;
-    std::unique_ptr< CallbackBase >  mAwakeCallback;
-    std::chrono::time_point< std::chrono::system_clock > mSleepTimePoint;
-    const Dali::LogFactoryInterface& mLogFactory;
-    bool                             mNeedToSleep;
-    bool                             mDestroyThread;
+    ConditionalWait                                    mConditionalWait;
+    std::unique_ptr<CallbackBase>                      mAwakeCallback;
+    std::chrono::time_point<std::chrono::steady_clock> mSleepTimePoint;
+    const Dali::LogFactoryInterface&                   mLogFactory;
+    const Dali::TraceFactoryInterface&                 mTraceFactory;
+
+    bool mNeedToSleep : 1;
+    bool mDestroyThread : 1;
   };
 
 private:
-
   // Undefined
-  VectorAnimationThread( const VectorAnimationThread& thread ) = delete;
+  VectorAnimationThread(const VectorAnimationThread& thread) = delete;
 
   // Undefined
-  VectorAnimationThread& operator=( const VectorAnimationThread& thread ) = delete;
+  VectorAnimationThread& operator=(const VectorAnimationThread& thread) = delete;
 
 private:
-
-  std::vector< VectorAnimationTaskPtr >      mAnimationTasks;
-  std::vector< VectorAnimationTaskPtr >      mCompletedTasks;
-  std::vector< VectorAnimationTaskPtr >      mWorkingTasks;
-  RoundRobinContainerView< RasterizeHelper > mRasterizers;
-  SleepThread                                mSleepThread;
-  ConditionalWait                            mConditionalWait;
-  bool                                       mNeedToSleep;
-  bool                                       mDestroyThread;
-  const Dali::LogFactoryInterface&           mLogFactory;
-
+  std::vector<VectorAnimationTaskPtr>             mAnimationTasks;
+  std::vector<VectorAnimationTaskPtr>             mCompletedTasks;
+  std::vector<VectorAnimationTaskPtr>             mWorkingTasks;
+  std::vector<std::pair<CallbackBase*, uint32_t>> mTriggerEventCallbacks{}; // Callbacks are not owned
+  SleepThread                                     mSleepThread;
+  ConditionalWait                                 mConditionalWait;
+  Mutex                                           mEventTriggerMutex;
+  std::unique_ptr<EventThreadCallback>            mEventTrigger{};
+  const Dali::LogFactoryInterface&                mLogFactory;
+  const Dali::TraceFactoryInterface&              mTraceFactory;
+  Dali::AsyncTaskManager                          mAsyncTaskManager;
+
+  bool mNeedToSleep : 1;
+  bool mDestroyThread : 1;
+  bool mEventTriggered : 1;
+  bool mForceRenderOnce : 1;
 };
 
 } // namespace Internal