(Vector) Ensure not to add duplicated task
[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) 2019 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 <memory>
22 #include <dali/public-api/signals/connection-tracker.h>
23 #include <dali/devel-api/threading/conditional-wait.h>
24 #include <dali/devel-api/threading/thread.h>
25 #include <dali/integration-api/adaptor-framework/log-factory-interface.h>
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
35 namespace Toolkit
36 {
37
38 namespace Internal
39 {
40
41 /**
42  * The main animation thread for vector animations
43  */
44 class VectorAnimationThread : public Thread
45 {
46 public:
47
48   /**
49    * @brief Constructor.
50    */
51   VectorAnimationThread();
52
53   /**
54    * @brief Destructor.
55    */
56   virtual ~VectorAnimationThread();
57
58   /**
59    * Add a animation task into the vector animation thread, called by main thread.
60    *
61    * @param[in] task The task added to the thread.
62    */
63   void AddTask( VectorAnimationTaskPtr task );
64
65   /**
66    * @brief Called when the rasterization is completed from the rasterize thread.
67    * @param task The completed task
68    */
69   void OnTaskCompleted( VectorAnimationTaskPtr task, bool stopped );
70
71   /**
72    * @brief Called when the sleep thread is awaken.
73    */
74   void OnAwakeFromSleep();
75
76 protected:
77
78   /**
79    * @brief The entry function of the animation thread.
80    */
81   void Run() override;
82
83 private:
84
85   /**
86    * Rasterizes the tasks.
87    */
88   void Rasterize();
89
90 private:
91
92   /**
93    * @brief Helper class to keep the relation between VectorRasterizeThread and corresponding container
94    */
95   class RasterizeHelper : public ConnectionTracker
96   {
97   public:
98     /**
99      * @brief Create an RasterizeHelper.
100      *
101      * @param[in] animationThread Reference to the VectorAnimationThread
102      */
103     RasterizeHelper( VectorAnimationThread& animationThread );
104
105     /**
106      * @brief Rasterizes the task.
107      *
108      * @param[in] task The task to rasterize.
109      */
110     void Rasterize( VectorAnimationTaskPtr task );
111
112   public:
113     RasterizeHelper( const RasterizeHelper& ) = delete;
114     RasterizeHelper& operator=( const RasterizeHelper& ) = delete;
115
116     RasterizeHelper( RasterizeHelper&& rhs );
117     RasterizeHelper& operator=( RasterizeHelper&& rhs ) = delete;
118
119   private:
120
121     /**
122      * @brief Main constructor that used by all other constructors
123      */
124     RasterizeHelper( std::unique_ptr< VectorRasterizeThread > rasterizer, VectorAnimationThread& animationThread );
125
126   private:
127     std::unique_ptr< VectorRasterizeThread > mRasterizer;
128     VectorAnimationThread&                   mAnimationThread;
129   };
130
131   /**
132    * @brief The thread to sleep until the next frame time.
133    */
134   class SleepThread : public Thread
135   {
136   public:
137
138     /**
139      * @brief Constructor.
140      */
141     SleepThread( CallbackBase* callback );
142
143     /**
144      * @brief Destructor.
145      */
146     virtual ~SleepThread();
147
148     /**
149      * @brief Sleeps untile the specified time point.
150      */
151     void SleepUntil( std::chrono::time_point< std::chrono::system_clock > timeToSleepUntil );
152
153   protected:
154
155     /**
156      * @brief The entry function of the animation thread.
157      */
158     void Run() override;
159
160   private:
161
162     SleepThread( const SleepThread& thread ) = delete;
163     SleepThread& operator=( const SleepThread& thread ) = delete;
164
165   private:
166     ConditionalWait                  mConditionalWait;
167     std::unique_ptr< CallbackBase >  mAwakeCallback;
168     std::chrono::time_point< std::chrono::system_clock > mSleepTimePoint;
169     const Dali::LogFactoryInterface& mLogFactory;
170     bool                             mNeedToSleep;
171     bool                             mDestroyThread;
172   };
173
174 private:
175
176   // Undefined
177   VectorAnimationThread( const VectorAnimationThread& thread ) = delete;
178
179   // Undefined
180   VectorAnimationThread& operator=( const VectorAnimationThread& thread ) = delete;
181
182 private:
183
184   std::vector< VectorAnimationTaskPtr >      mAnimationTasks;
185   std::vector< VectorAnimationTaskPtr >      mCompletedTasks;
186   std::vector< VectorAnimationTaskPtr >      mWorkingTasks;
187   RoundRobinContainerView< RasterizeHelper > mRasterizers;
188   SleepThread                                mSleepThread;
189   ConditionalWait                            mConditionalWait;
190   bool                                       mNeedToSleep;
191   bool                                       mDestroyThread;
192   const Dali::LogFactoryInterface&           mLogFactory;
193
194 };
195
196 } // namespace Internal
197
198 } // namespace Toolkit
199
200 } // namespace Dali
201
202 #endif // #endif // DALI_TOOLKIT_VECTOR_ANIMATION_THREAD_H