Merge "Add KEYBOARD_FOCUSABLE_CHILDREN property. Whether the children of this actor...
[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) 2021 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/signals/connection-tracker.h>
25 #include <memory>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/internal/helpers/round-robin-container-view.h>
29 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-task.h>
30 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-rasterize-thread.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    * 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    * @param task The completed task
64    */
65   void OnTaskCompleted(VectorAnimationTaskPtr task, bool stopped);
66
67   /**
68    * @brief Called when the sleep thread is awaken.
69    */
70   void OnAwakeFromSleep();
71
72 protected:
73   /**
74    * @brief The entry function of the animation thread.
75    */
76   void Run() override;
77
78 private:
79   /**
80    * Rasterizes the tasks.
81    */
82   void Rasterize();
83
84 private:
85   /**
86    * @brief Helper class to keep the relation between VectorRasterizeThread and corresponding container
87    */
88   class RasterizeHelper : public ConnectionTracker
89   {
90   public:
91     /**
92      * @brief Create an RasterizeHelper.
93      *
94      * @param[in] animationThread Reference to the VectorAnimationThread
95      */
96     RasterizeHelper(VectorAnimationThread& animationThread);
97
98     /**
99      * @brief Rasterizes the task.
100      *
101      * @param[in] task The task to rasterize.
102      */
103     void Rasterize(VectorAnimationTaskPtr task);
104
105   public:
106     RasterizeHelper(const RasterizeHelper&) = delete;
107     RasterizeHelper& operator=(const RasterizeHelper&) = delete;
108
109     RasterizeHelper(RasterizeHelper&& rhs);
110     RasterizeHelper& operator=(RasterizeHelper&& rhs) = delete;
111
112   private:
113     /**
114      * @brief Main constructor that used by all other constructors
115      */
116     RasterizeHelper(std::unique_ptr<VectorRasterizeThread> rasterizer, VectorAnimationThread& animationThread);
117
118   private:
119     std::unique_ptr<VectorRasterizeThread> mRasterizer;
120     VectorAnimationThread&                 mAnimationThread;
121   };
122
123   /**
124    * @brief The thread to sleep until the next frame time.
125    */
126   class SleepThread : public Thread
127   {
128   public:
129     /**
130      * @brief Constructor.
131      */
132     SleepThread(CallbackBase* callback);
133
134     /**
135      * @brief Destructor.
136      */
137     ~SleepThread() override;
138
139     /**
140      * @brief Sleeps untile the specified time point.
141      */
142     void SleepUntil(std::chrono::time_point<std::chrono::steady_clock> timeToSleepUntil);
143
144   protected:
145     /**
146      * @brief The entry function of the animation thread.
147      */
148     void Run() override;
149
150   private:
151     SleepThread(const SleepThread& thread) = delete;
152     SleepThread& operator=(const SleepThread& thread) = delete;
153
154   private:
155     ConditionalWait                                    mConditionalWait;
156     std::unique_ptr<CallbackBase>                      mAwakeCallback;
157     std::chrono::time_point<std::chrono::steady_clock> mSleepTimePoint;
158     const Dali::LogFactoryInterface&                   mLogFactory;
159     bool                                               mNeedToSleep;
160     bool                                               mDestroyThread;
161   };
162
163 private:
164   // Undefined
165   VectorAnimationThread(const VectorAnimationThread& thread) = delete;
166
167   // Undefined
168   VectorAnimationThread& operator=(const VectorAnimationThread& thread) = delete;
169
170 private:
171   std::vector<VectorAnimationTaskPtr>      mAnimationTasks;
172   std::vector<VectorAnimationTaskPtr>      mCompletedTasks;
173   std::vector<VectorAnimationTaskPtr>      mWorkingTasks;
174   RoundRobinContainerView<RasterizeHelper> mRasterizers;
175   SleepThread                              mSleepThread;
176   ConditionalWait                          mConditionalWait;
177   bool                                     mNeedToSleep;
178   bool                                     mDestroyThread;
179   const Dali::LogFactoryInterface&         mLogFactory;
180 };
181
182 } // namespace Internal
183
184 } // namespace Toolkit
185
186 } // namespace Dali
187
188 #endif // #endif // DALI_TOOLKIT_VECTOR_ANIMATION_THREAD_H