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