Support int uniform in shader
[platform/core/uifw/dali-adaptor.git] / dali / public-api / adaptor-framework / async-task-manager.h
1 #ifndef DALI_ASYNC_TASK_MANAGER_H
2 #define DALI_ASYNC_TASK_MANAGER_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/public-api/common/intrusive-ptr.h>
22 #include <dali/public-api/object/base-handle.h>
23 #include <dali/public-api/signals/callback.h>
24 #include <memory>
25
26 // INTERNAL INCLUDES
27 #include <dali/public-api/dali-adaptor-common.h>
28
29 namespace Dali
30 {
31 namespace Internal DALI_INTERNAL
32 {
33 namespace Adaptor
34 {
35 class AsyncTaskManager;
36 }
37 } // namespace DALI_INTERNAL
38
39 class AsyncTask;
40 using AsyncTaskPtr = IntrusivePtr<AsyncTask>;
41
42 /**
43  * The async tasks to be processed in the worker thread.
44  * @SINCE_2_2.3
45  */
46 class DALI_ADAPTOR_API AsyncTask : public RefObject
47 {
48 public:
49   // The Type of invocation thread
50   enum class ThreadType
51   {
52     MAIN_THREAD,
53     WORKER_THREAD
54   };
55
56   /**
57    * @brief The priority of this task what user think.
58    * To avoid long term tasks (like remote image download) block whole threads,
59    * Let user set the priority type of this task.
60    *
61    * Low priority task means it doesn't need to process by FIFO logic.
62    * So we make that Low priority don't take whole threads.
63    *
64    * Task selection algorithm defined internally.
65    *
66    * @note Task cannot change the priority type after create.
67    *
68    * @SINCE_2_2.17
69    */
70   enum class PriorityType
71   {
72     HIGH = 0, ///< Highest priority to process task. @SINCE_2_2.17
73     LOW  = 1, ///< Lowest priority to process task. @SINCE_2_2.17
74
75     PRIORITY_COUNT, ///< The number of priority type. @SINCE_2_2.17
76
77     DEFAULT = HIGH, ///< Default priority value if nothing defined. @SINCE_2_2.17
78   };
79
80   /**
81    * Constructor
82    * @SINCE_2_2.3
83    * @param[in] callback The callback to invoke on task completion, either on the main thread on the worker thread. The ownership of callback is taken by this class.
84    * @param[in] priority The proirity type of this task.
85    * @param[in] threadType The thread type of invocation callback.
86    */
87   AsyncTask(CallbackBase* callback, PriorityType priority = PriorityType::DEFAULT, ThreadType threadType = AsyncTask::ThreadType::MAIN_THREAD);
88
89   /**
90    * Get the complated callback
91    * @SINCE_2_2.3
92    */
93   CallbackBase* GetCompletedCallback();
94
95   /**
96    * Get the thread of the invocation callback
97    * @SINCE_2_2.9
98    * @return the type of invocation callback.
99    */
100   ThreadType GetCallbackInvocationThread();
101
102   /**
103    * Get the priority of this task
104    * @SINCE_2_2.17
105    * @return the type of priority.
106    */
107   PriorityType GetPriorityType() const;
108
109   /**
110    * Destructor.
111    * @SINCE_2_2.3
112    */
113   virtual ~AsyncTask() = default;
114
115   /**
116    * Process the task
117    * @SINCE_2_2.3
118    */
119   virtual void Process() = 0;
120
121   /**
122    * Whether the task is ready to process.
123    * @SINCE_2_2.3
124    * @return True if the task is ready to process.
125    */
126   virtual bool IsReady() = 0;
127
128 private:
129   std::unique_ptr<CallbackBase> mCompletedCallback;
130   const PriorityType            mPriorityType;
131   ThreadType                    mThreadType;
132
133   // Undefined
134   AsyncTask(const AsyncTask& task) = delete;
135
136   // Undefined
137   AsyncTask& operator=(const AsyncTask& task) = delete;
138 };
139
140 /**
141  * The manager for async task
142  * @SINCE_2_2.3
143  */
144 class DALI_ADAPTOR_API AsyncTaskManager : public BaseHandle
145 {
146 public:
147   /**
148    * Constructor.
149    * @SINCE_2_2.3
150    */
151   AsyncTaskManager();
152
153   /**
154    * Destructor.
155    * @SINCE_2_2.3
156    */
157   ~AsyncTaskManager();
158
159   /**
160    * @brief Gets the singleton of AsyncTaskManager object.
161    *
162    * @SINCE_2_2.3
163    * @return A handle to the AsyncTaskManager
164    */
165   static AsyncTaskManager Get();
166
167   /**
168    * @brief Add the async task into the waiting queue, called by main thread.
169    *
170    * @SINCE_2_2.3
171    * @param[in] task The task added to the queue.
172    */
173   void AddTask(AsyncTaskPtr task);
174
175   /**
176    * @brief Remove the task from the waiting queue, called by main thread.
177    *
178    * @SINCE_2_2.3
179    * @param[in] task The task pointer.
180    */
181   void RemoveTask(AsyncTaskPtr task);
182
183 public:
184   /// @cond internal
185   /**
186    * @brief Allows the creation of a AsyncTaskManager handle from an internal pointer.
187    *
188    * @note Not intended for application developers
189    * @SINCE_2_2.3
190    * @param[in] impl A pointer to the object
191    */
192   explicit DALI_INTERNAL AsyncTaskManager(Internal::Adaptor::AsyncTaskManager* impl);
193   /// @endcond
194 };
195
196 } // namespace Dali
197
198 #endif