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