Add GetCallbackInvocationThread() for AsyncTaskMananager
[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    * Constructor
58    * @SINCE_2_2.3
59    * @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.
60    * @param[in] threadType The thread type of invocation callback.
61    */
62   AsyncTask(CallbackBase* callback, ThreadType threadType = AsyncTask::ThreadType::MAIN_THREAD);
63
64   /**
65    * Get the complated callback
66    * @SINCE_2_2.3
67    */
68   CallbackBase* GetCompletedCallback();
69
70   /**
71    * Get the thread of the invocation callback
72    * @SINCE_2_2.9
73    * @return the type of invocation callback.
74    */
75   ThreadType GetCallbackInvocationThread();
76
77   /**
78    * Destructor.
79    * @SINCE_2_2.3
80    */
81   virtual ~AsyncTask() = default;
82
83   /**
84    * Process the task
85    * @SINCE_2_2.3
86    */
87   virtual void Process() = 0;
88
89   /**
90    * Whether the task is ready to process.
91    * @SINCE_2_2.3
92    * @return True if the task is ready to process.
93    */
94   virtual bool IsReady() = 0;
95
96 private:
97   std::unique_ptr<CallbackBase> mCompletedCallback;
98   ThreadType                    mThreadType;
99
100   // Undefined
101   AsyncTask(const AsyncTask& task) = delete;
102
103   // Undefined
104   AsyncTask& operator=(const AsyncTask& task) = delete;
105 };
106
107 /**
108  * The manager for async task
109  * @SINCE_2_2.3
110  */
111 class DALI_ADAPTOR_API AsyncTaskManager : public BaseHandle
112 {
113 public:
114   /**
115    * Constructor.
116    * @SINCE_2_2.3
117    */
118   AsyncTaskManager();
119
120   /**
121    * Destructor.
122    * @SINCE_2_2.3
123    */
124   ~AsyncTaskManager();
125
126   /**
127    * @brief Gets the singleton of AsyncTaskManager object.
128    *
129    * @SINCE_2_2.3
130    * @return A handle to the AsyncTaskManager
131    */
132   static AsyncTaskManager Get();
133
134   /**
135    * @brief Add the async task into the waiting queue, called by main thread.
136    *
137    * @SINCE_2_2.3
138    * @param[in] task The task added to the queue.
139    */
140   void AddTask(AsyncTaskPtr task);
141
142   /**
143    * @brief Remove the task from the waiting queue, called by main thread.
144    *
145    * @SINCE_2_2.3
146    * @param[in] task The task pointer.
147    */
148   void RemoveTask(AsyncTaskPtr task);
149
150 public:
151   /// @cond internal
152   /**
153    * @brief Allows the creation of a AsyncTaskManager handle from an internal pointer.
154    *
155    * @note Not intended for application developers
156    * @SINCE_2_2.3
157    * @param[in] impl A pointer to the object
158    */
159   explicit DALI_INTERNAL AsyncTaskManager(Internal::Adaptor::AsyncTaskManager* impl);
160   /// @endcond
161 };
162
163 } // namespace Dali
164
165 #endif