[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
1 #ifndef DALI_TOOLKIT_VECTOR_ANIMATION_THREAD_H
2 #define DALI_TOOLKIT_VECTOR_ANIMATION_THREAD_H
3
4 /*
5  * Copyright (c) 2024 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/mutex.h>
23 #include <dali/devel-api/threading/thread.h>
24 #include <dali/integration-api/adaptor-framework/log-factory-interface.h>
25 #include <dali/integration-api/adaptor-framework/trace-factory-interface.h>
26 #include <dali/public-api/adaptor-framework/round-robin-container-view.h>
27 #include <dali/public-api/signals/connection-tracker.h>
28 #include <memory>
29
30 // INTERNAL INCLUDES
31 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-task.h>
32
33 namespace Dali
34 {
35 namespace Toolkit
36 {
37 namespace Internal
38 {
39 /**
40  * The main animation thread for vector animations
41  */
42 class VectorAnimationThread : public Thread
43 {
44 public:
45   /**
46    * @brief Constructor.
47    */
48   VectorAnimationThread();
49
50   /**
51    * @brief Destructor.
52    */
53   ~VectorAnimationThread() override;
54
55   /**
56    * @brief Add a animation task into the vector animation thread, called by main thread.
57    *
58    * @param[in] task The task added to the thread.
59    */
60   void AddTask(VectorAnimationTaskPtr task);
61
62   /**
63    * @brief Called when the rasterization is completed from the rasterize thread.
64    *
65    * @param[in] task The completed task
66    * @param[in] success true if the task succeeded, false otherwise.
67    * @param[in] keepAnimation true if the animation is running, false otherwise.
68    */
69   void OnTaskCompleted(VectorAnimationTaskPtr task, bool success, bool keepAnimation);
70
71   /**
72    * @brief Called when the sleep thread is awaken.
73    */
74   void OnAwakeFromSleep();
75
76   /**
77    * @brief Add an event trigger callback.
78    *
79    * @param callback The callback to add
80    * @param argument The argument to pass to the callback
81    * @note Ownership of the callback is NOT passed onto this class.
82    * @note The callback will be excuted in the main thread.
83    */
84   void AddEventTriggerCallback(CallbackBase* callback, uint32_t argument);
85
86   /**
87    * @brief Remove event trigger callbacks what we added before.
88    *
89    * @param callback The callback to remove
90    */
91   void RemoveEventTriggerCallbacks(CallbackBase* callback);
92
93 protected:
94   /**
95    * @brief The entry function of the animation thread.
96    */
97   void Run() override;
98
99 private:
100   /**
101    * @brief Rasterizes the tasks.
102    */
103   void Rasterize();
104
105   /**
106    * @brief Called when the event callback is triggered.
107    */
108   void OnEventCallbackTriggered();
109
110   /**
111    * @brief Gets next event callback to process.
112    */
113   std::pair<CallbackBase*, uint32_t> GetNextEventCallback();
114
115   /**
116    * @brief The thread to sleep until the next frame time.
117    */
118   class SleepThread : public Thread
119   {
120   public:
121     /**
122      * @brief Constructor.
123      */
124     SleepThread(CallbackBase* callback);
125
126     /**
127      * @brief Destructor.
128      */
129     ~SleepThread() override;
130
131     /**
132      * @brief Sleeps untile the specified time point.
133      */
134     void SleepUntil(std::chrono::time_point<std::chrono::steady_clock> timeToSleepUntil);
135
136   protected:
137     /**
138      * @brief The entry function of the animation thread.
139      */
140     void Run() override;
141
142   private:
143     SleepThread(const SleepThread& thread) = delete;
144     SleepThread& operator=(const SleepThread& thread) = delete;
145
146   private:
147     ConditionalWait                                    mConditionalWait;
148     std::unique_ptr<CallbackBase>                      mAwakeCallback;
149     std::chrono::time_point<std::chrono::steady_clock> mSleepTimePoint;
150     const Dali::LogFactoryInterface&                   mLogFactory;
151     const Dali::TraceFactoryInterface&                 mTraceFactory;
152     bool                                               mNeedToSleep;
153     bool                                               mDestroyThread;
154   };
155
156 private:
157   // Undefined
158   VectorAnimationThread(const VectorAnimationThread& thread) = delete;
159
160   // Undefined
161   VectorAnimationThread& operator=(const VectorAnimationThread& thread) = delete;
162
163 private:
164   std::vector<VectorAnimationTaskPtr>             mAnimationTasks;
165   std::vector<VectorAnimationTaskPtr>             mCompletedTasks;
166   std::vector<VectorAnimationTaskPtr>             mWorkingTasks;
167   std::vector<std::pair<CallbackBase*, uint32_t>> mTriggerEventCallbacks{}; // Callbacks are not owned
168   SleepThread                                     mSleepThread;
169   ConditionalWait                                 mConditionalWait;
170   Mutex                                           mEventTriggerMutex;
171   std::unique_ptr<EventThreadCallback>            mEventTrigger{};
172   bool                                            mNeedToSleep;
173   bool                                            mDestroyThread;
174   bool                                            mEventTriggered{false};
175   const Dali::LogFactoryInterface&                mLogFactory;
176   const Dali::TraceFactoryInterface&              mTraceFactory;
177   Dali::AsyncTaskManager                          mAsyncTaskManager;
178 };
179
180 } // namespace Internal
181
182 } // namespace Toolkit
183
184 } // namespace Dali
185
186 #endif // #endif // DALI_TOOLKIT_VECTOR_ANIMATION_THREAD_H