• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_PIPELINE_NG_UI_TASK_SCHEDULER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_PIPELINE_NG_UI_TASK_SCHEDULER_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <list>
22 #include <set>
23 #include <unordered_map>
24 
25 #include "base/memory/referenced.h"
26 #include "base/utils/macros.h"
27 
28 namespace OHOS::Ace::NG {
29 
30 class FrameNode;
31 
32 using TaskThread = uint32_t;
33 constexpr TaskThread PLATFORM_TASK = 0;
34 constexpr TaskThread MAIN_TASK = 1;
35 constexpr TaskThread BACKGROUND_TASK = 1 << 1;
36 constexpr TaskThread UNDEFINED_TASK = 1 << 2;
37 
38 class UITask {
39 public:
UITask(std::function<void ()> && task)40     explicit UITask(std::function<void()>&& task) : task_(std::move(task)) {}
41 
UITask(std::function<void ()> && task,TaskThread taskThread)42     UITask(std::function<void()>&& task, TaskThread taskThread) : task_(std::move(task)), taskThread_(taskThread) {}
43 
44     ~UITask() = default;
45 
SetTaskThreadType(TaskThread taskThread)46     void SetTaskThreadType(TaskThread taskThread)
47     {
48         taskThread_ = taskThread;
49     }
50 
GetTaskThreadType()51     TaskThread GetTaskThreadType() const
52     {
53         return taskThread_;
54     }
55 
operator()56     void operator()() const
57     {
58         if (task_) {
59             task_();
60         }
61     }
62 
63 private:
64     std::function<void()> task_;
65     TaskThread taskThread_ = MAIN_TASK;
66 };
67 
68 class ACE_EXPORT UITaskScheduler final {
69 public:
70     using PredictTask = std::function<void(int64_t)>;
71     UITaskScheduler() = default;
72     ~UITaskScheduler();
73 
74     // Called on Main Thread.
75     void AddDirtyLayoutNode(const RefPtr<FrameNode>& dirty);
76     void AddDirtyRenderNode(const RefPtr<FrameNode>& dirty);
77     void AddPredictTask(PredictTask&& task);
78     void AddAfterLayoutTask(std::function<void()>&& task);
79 
80     void FlushLayoutTask(bool forceUseMainThread = false);
81     void FlushRenderTask(bool forceUseMainThread = false);
82     void FlushTask();
83     void FlushPredictTask(int64_t deadline);
84     void FlushAfterLayoutTask();
85 
UpdateCurrentPageId(uint32_t id)86     void UpdateCurrentPageId(uint32_t id)
87     {
88         currentPageId_ = id;
89     }
90 
91     void CleanUp();
92 
93     bool isEmpty();
94 
95 private:
96     template<typename T>
97     struct NodeCompare {
operatorNodeCompare98         bool operator()(const T& nodeLeft, const T& nodeRight) const
99         {
100             if (!nodeLeft || !nodeRight) {
101                 return false;
102             }
103             if (nodeLeft->GetDepth() < nodeRight->GetDepth()) {
104                 return true;
105             }
106             if (nodeLeft->GetDepth() == nodeRight->GetDepth()) {
107                 return nodeLeft < nodeRight;
108             }
109             return false;
110         }
111     };
112 
113     using PageDirtySet = std::set<RefPtr<FrameNode>, NodeCompare<RefPtr<FrameNode>>>;
114     using RootDirtyMap = std::unordered_map<uint32_t, PageDirtySet>;
115 
116     RootDirtyMap dirtyLayoutNodes_;
117     RootDirtyMap dirtyRenderNodes_;
118     std::list<PredictTask> predictTask_;
119     std::list<std::function<void()>> afterLayoutTasks_;
120 
121     uint32_t currentPageId_ = 0;
122 
123     ACE_DISALLOW_COPY_AND_MOVE(UITaskScheduler);
124 };
125 
126 } // namespace OHOS::Ace::NG
127 
128 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_PIPELINE_NG_UI_TASK_SCHEDULER_H
129