DALi Version 2.2.11
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-animation-thread.h
1 #ifndef DALI_TOOLKIT_VECTOR_ANIMATION_THREAD_H
2 #define DALI_TOOLKIT_VECTOR_ANIMATION_THREAD_H
3
4 /*
5  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 // EXTERNAL INCLUDES
21 #include <dali/devel-api/threading/conditional-wait.h>
22 #include <dali/devel-api/threading/thread.h>
23 #include <dali/integration-api/adaptor-framework/log-factory-interface.h>
24 #include <dali/public-api/adaptor-framework/round-robin-container-view.h>
25 #include <dali/public-api/signals/connection-tracker.h>
26 #include <memory>
27
28 // INTERNAL INCLUDES
29 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-task.h>
30
31 namespace Dali
32 {
33 namespace Toolkit
34 {
35 namespace Internal
36 {
37 /**
38  * The main animation thread for vector animations
39  */
40 class VectorAnimationThread : public Thread
41 {
42 public:
43   /**
44    * @brief Constructor.
45    */
46   VectorAnimationThread();
47
48   /**
49    * @brief Destructor.
50    */
51   ~VectorAnimationThread() override;
52
53   /**
54    * Add a animation task into the vector animation thread, called by main thread.
55    *
56    * @param[in] task The task added to the thread.
57    */
58   void AddTask(VectorAnimationTaskPtr task);
59
60   /**
61    * @brief Called when the rasterization is completed from the rasterize thread.
62    * @param[in] task The completed task
63    * @param[in] success true if the task succeeded, false otherwise.
64    * @param[in] keepAnimation true if the animation is running, false otherwise.
65    */
66   void OnTaskCompleted(VectorAnimationTaskPtr task, bool success, bool keepAnimation);
67
68   /**
69    * @brief Called when the sleep thread is awaken.
70    */
71   void OnAwakeFromSleep();
72
73 protected:
74   /**
75    * @brief The entry function of the animation thread.
76    */
77   void Run() override;
78
79 private:
80   /**
81    * Rasterizes the tasks.
82    */
83   void Rasterize();
84
85   /**
86    * @brief The thread to sleep until the next frame time.
87    */
88   class SleepThread : public Thread
89   {
90   public:
91     /**
92      * @brief Constructor.
93      */
94     SleepThread(CallbackBase* callback);
95
96     /**
97      * @brief Destructor.
98      */
99     ~SleepThread() override;
100
101     /**
102      * @brief Sleeps untile the specified time point.
103      */
104     void SleepUntil(std::chrono::time_point<std::chrono::steady_clock> timeToSleepUntil);
105
106   protected:
107     /**
108      * @brief The entry function of the animation thread.
109      */
110     void Run() override;
111
112   private:
113     SleepThread(const SleepThread& thread) = delete;
114     SleepThread& operator=(const SleepThread& thread) = delete;
115
116   private:
117     ConditionalWait                                    mConditionalWait;
118     std::unique_ptr<CallbackBase>                      mAwakeCallback;
119     std::chrono::time_point<std::chrono::steady_clock> mSleepTimePoint;
120     const Dali::LogFactoryInterface&                   mLogFactory;
121     bool                                               mNeedToSleep;
122     bool                                               mDestroyThread;
123   };
124
125 private:
126   // Undefined
127   VectorAnimationThread(const VectorAnimationThread& thread) = delete;
128
129   // Undefined
130   VectorAnimationThread& operator=(const VectorAnimationThread& thread) = delete;
131
132 private:
133   std::vector<VectorAnimationTaskPtr> mAnimationTasks;
134   std::vector<VectorAnimationTaskPtr> mCompletedTasks;
135   std::vector<VectorAnimationTaskPtr> mWorkingTasks;
136   SleepThread                         mSleepThread;
137   ConditionalWait                     mConditionalWait;
138   bool                                mNeedToSleep;
139   bool                                mDestroyThread;
140   const Dali::LogFactoryInterface&    mLogFactory;
141   Dali::AsyncTaskManager              mAsyncTaskManager;
142 };
143
144 } // namespace Internal
145
146 } // namespace Toolkit
147
148 } // namespace Dali
149
150 #endif // #endif // DALI_TOOLKIT_VECTOR_ANIMATION_THREAD_H