(Vector) Fix invalid callback issue
[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    * @brief 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    *
63    * @param[in] task The completed task
64    * @param[in] success true if the task succeeded, false otherwise.
65    * @param[in] keepAnimation true if the animation is running, false otherwise.
66    */
67   void OnTaskCompleted(VectorAnimationTaskPtr task, bool success, bool keepAnimation);
68
69   /**
70    * @brief Called when the sleep thread is awaken.
71    */
72   void OnAwakeFromSleep();
73
74   /**
75    * @brief Add an event trigger callback.
76    *
77    * @param callback The callback to add
78    * @note Ownership of the callback is NOT passed onto this class.
79    * @note The callback will be excuted in the main thread.
80    */
81   void AddEventTriggerCallback(CallbackBase* callback);
82
83   /**
84    * @brief Remove an event trigger callback.
85    *
86    * @param callback The callback to remove
87    */
88   void RemoveEventTriggerCallback(CallbackBase* callback);
89
90 protected:
91   /**
92    * @brief The entry function of the animation thread.
93    */
94   void Run() override;
95
96 private:
97   /**
98    * @brief Rasterizes the tasks.
99    */
100   void Rasterize();
101
102   /**
103    * @brief Called when the event callback is triggered.
104    */
105   void OnEventCallbackTriggered();
106
107   /**
108    * @brief Gets next event callback to process.
109    */
110   CallbackBase* GetNextEventCallback();
111
112   /**
113    * @brief The thread to sleep until the next frame time.
114    */
115   class SleepThread : public Thread
116   {
117   public:
118     /**
119      * @brief Constructor.
120      */
121     SleepThread(CallbackBase* callback);
122
123     /**
124      * @brief Destructor.
125      */
126     ~SleepThread() override;
127
128     /**
129      * @brief Sleeps untile the specified time point.
130      */
131     void SleepUntil(std::chrono::time_point<std::chrono::steady_clock> timeToSleepUntil);
132
133   protected:
134     /**
135      * @brief The entry function of the animation thread.
136      */
137     void Run() override;
138
139   private:
140     SleepThread(const SleepThread& thread) = delete;
141     SleepThread& operator=(const SleepThread& thread) = delete;
142
143   private:
144     ConditionalWait                                    mConditionalWait;
145     std::unique_ptr<CallbackBase>                      mAwakeCallback;
146     std::chrono::time_point<std::chrono::steady_clock> mSleepTimePoint;
147     const Dali::LogFactoryInterface&                   mLogFactory;
148     bool                                               mNeedToSleep;
149     bool                                               mDestroyThread;
150   };
151
152 private:
153   // Undefined
154   VectorAnimationThread(const VectorAnimationThread& thread) = delete;
155
156   // Undefined
157   VectorAnimationThread& operator=(const VectorAnimationThread& thread) = delete;
158
159 private:
160   std::vector<VectorAnimationTaskPtr>  mAnimationTasks;
161   std::vector<VectorAnimationTaskPtr>  mCompletedTasks;
162   std::vector<VectorAnimationTaskPtr>  mWorkingTasks;
163   std::vector<CallbackBase*>           mTriggerEventCallbacks{}; // Callbacks are not owned
164   SleepThread                          mSleepThread;
165   ConditionalWait                      mConditionalWait;
166   std::unique_ptr<EventThreadCallback> mEventTrigger{};
167   bool                                 mNeedToSleep;
168   bool                                 mDestroyThread;
169   bool                                 mEventTriggered{false};
170   const Dali::LogFactoryInterface&     mLogFactory;
171   Dali::AsyncTaskManager               mAsyncTaskManager;
172 };
173
174 } // namespace Internal
175
176 } // namespace Toolkit
177
178 } // namespace Dali
179
180 #endif // #endif // DALI_TOOLKIT_VECTOR_ANIMATION_THREAD_H