Merge "Test harness sync" into 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) 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/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    * @note Ownership of the callback is NOT passed onto this class.
81    * @note The callback will be excuted in the main thread.
82    */
83   void AddEventTriggerCallback(CallbackBase* callback);
84
85   /**
86    * @brief Remove event trigger callbacks what we added before.
87    *
88    * @param callback The callback to remove
89    */
90   void RemoveEventTriggerCallbacks(CallbackBase* callback);
91
92 protected:
93   /**
94    * @brief The entry function of the animation thread.
95    */
96   void Run() override;
97
98 private:
99   /**
100    * @brief Rasterizes the tasks.
101    */
102   void Rasterize();
103
104   /**
105    * @brief Called when the event callback is triggered.
106    */
107   void OnEventCallbackTriggered();
108
109   /**
110    * @brief Gets next event callback to process.
111    */
112   CallbackBase* GetNextEventCallback();
113
114   /**
115    * @brief The thread to sleep until the next frame time.
116    */
117   class SleepThread : public Thread
118   {
119   public:
120     /**
121      * @brief Constructor.
122      */
123     SleepThread(CallbackBase* callback);
124
125     /**
126      * @brief Destructor.
127      */
128     ~SleepThread() override;
129
130     /**
131      * @brief Sleeps untile the specified time point.
132      */
133     void SleepUntil(std::chrono::time_point<std::chrono::steady_clock> timeToSleepUntil);
134
135   protected:
136     /**
137      * @brief The entry function of the animation thread.
138      */
139     void Run() override;
140
141   private:
142     SleepThread(const SleepThread& thread) = delete;
143     SleepThread& operator=(const SleepThread& thread) = delete;
144
145   private:
146     ConditionalWait                                    mConditionalWait;
147     std::unique_ptr<CallbackBase>                      mAwakeCallback;
148     std::chrono::time_point<std::chrono::steady_clock> mSleepTimePoint;
149     const Dali::LogFactoryInterface&                   mLogFactory;
150     const Dali::TraceFactoryInterface&                 mTraceFactory;
151     bool                                               mNeedToSleep;
152     bool                                               mDestroyThread;
153   };
154
155 private:
156   // Undefined
157   VectorAnimationThread(const VectorAnimationThread& thread) = delete;
158
159   // Undefined
160   VectorAnimationThread& operator=(const VectorAnimationThread& thread) = delete;
161
162 private:
163   std::vector<VectorAnimationTaskPtr>  mAnimationTasks;
164   std::vector<VectorAnimationTaskPtr>  mCompletedTasks;
165   std::vector<VectorAnimationTaskPtr>  mWorkingTasks;
166   std::vector<CallbackBase*>           mTriggerEventCallbacks{}; // Callbacks are not owned
167   SleepThread                          mSleepThread;
168   ConditionalWait                      mConditionalWait;
169   Mutex                                mEventTriggerMutex;
170   std::unique_ptr<EventThreadCallback> mEventTrigger{};
171   bool                                 mNeedToSleep;
172   bool                                 mDestroyThread;
173   bool                                 mEventTriggered{false};
174   const Dali::LogFactoryInterface&     mLogFactory;
175   const Dali::TraceFactoryInterface&   mTraceFactory;
176   Dali::AsyncTaskManager               mAsyncTaskManager;
177 };
178
179 } // namespace Internal
180
181 } // namespace Toolkit
182
183 } // namespace Dali
184
185 #endif // #endif // DALI_TOOLKIT_VECTOR_ANIMATION_THREAD_H