• 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 <map>
23 #include <set>
24 
25 #include "base/log/frame_info.h"
26 #include "base/memory/referenced.h"
27 #include "base/utils/macros.h"
28 
29 namespace OHOS::Ace::NG {
30 
31 class CustomNode;
32 class FrameNode;
33 
34 using TaskThread = uint32_t;
35 constexpr TaskThread PLATFORM_TASK = 0;
36 constexpr TaskThread MAIN_TASK = 1;
37 constexpr TaskThread BACKGROUND_TASK = 1 << 1;
38 constexpr TaskThread UNDEFINED_TASK = 1 << 2;
39 
40 class UITask {
41 public:
UITask(std::function<void ()> && task)42     explicit UITask(std::function<void()>&& task) : task_(std::move(task)) {}
43 
UITask(std::function<void ()> && task,TaskThread taskThread)44     UITask(std::function<void()>&& task, TaskThread taskThread) : task_(std::move(task)), taskThread_(taskThread) {}
45 
46     ~UITask() = default;
47 
SetTaskThreadType(TaskThread taskThread)48     void SetTaskThreadType(TaskThread taskThread)
49     {
50         taskThread_ = taskThread;
51     }
52 
GetTaskThreadType()53     TaskThread GetTaskThreadType() const
54     {
55         return taskThread_;
56     }
57 
operator()58     void operator()() const
59     {
60         if (task_) {
61             task_();
62         }
63     }
64 
65 private:
66     std::function<void()> task_;
67     TaskThread taskThread_ = MAIN_TASK;
68 };
69 
70 class ACE_EXPORT UITaskScheduler final {
71 public:
72     using PredictTask = std::function<void(int64_t, bool)>;
73     UITaskScheduler();
74     ~UITaskScheduler();
75 
76     // Called on Main Thread.
77     void AddDirtyLayoutNode(const RefPtr<FrameNode>& dirty);
78     void AddDirtyRenderNode(const RefPtr<FrameNode>& dirty);
79     void AddPredictTask(PredictTask&& task);
80     void AddAfterLayoutTask(std::function<void()>&& task);
81     void AddAfterRenderTask(std::function<void()>&& task);
82     void AddPersistAfterLayoutTask(std::function<void()>&& task);
83 
84     void FlushLayoutTask(bool forceUseMainThread = false);
85     void FlushRenderTask(bool forceUseMainThread = false);
86     void FlushTask();
87     void FlushPredictTask(int64_t deadline, bool canUseLongPredictTask = false);
88     void FlushAfterLayoutTask();
89     void FlushAfterRenderTask();
90     void FlushPersistAfterLayoutTask();
91     void RestoreGeoState();
92     void ExpandSafeArea();
93 
94     void FlushDelayJsActive();
95 
UpdateCurrentPageId(uint32_t id)96     void UpdateCurrentPageId(uint32_t id)
97     {
98         currentPageId_ = id;
99     }
100 
101     void CleanUp();
102 
103     bool isEmpty();
104 
StartRecordFrameInfo(FrameInfo * info)105     void StartRecordFrameInfo(FrameInfo* info)
106     {
107         frameInfo_ = info;
108     }
109 
FinishRecordFrameInfo()110     void FinishRecordFrameInfo()
111     {
112         frameInfo_ = nullptr;
113     }
114 
GetFrameId()115     static uint64_t GetFrameId()
116     {
117         return frameId_;
118     }
119 
IsLayouting()120     bool IsLayouting() const
121     {
122         return isLayouting_;
123     }
124 
125     void SetJSViewActive(bool active, WeakPtr<CustomNode> custom);
126 
IsDirtyLayoutNodesEmpty()127     bool IsDirtyLayoutNodesEmpty()
128     {
129         return dirtyLayoutNodes_.empty();
130     }
131 
AddSyncGeometryNodeTask(std::function<void ()> && task)132     void AddSyncGeometryNodeTask(std::function<void()>&& task)
133     {
134         syncGeometryNodeTasks_.emplace_back(task);
135     }
136 
137     void FlushSyncGeometryNodeTasks();
138 
139 private:
140     bool NeedAdditionalLayout();
141 
142     template<typename T>
143     struct NodeCompare {
operatorNodeCompare144         bool operator()(const T& nodeLeft, const T& nodeRight) const
145         {
146             if (!nodeLeft || !nodeRight) {
147                 return false;
148             }
149             if (nodeLeft->GetLayoutPriority() != nodeRight->GetLayoutPriority()) {
150                 return nodeLeft->GetLayoutPriority() > nodeRight->GetLayoutPriority();
151             }
152             if (nodeLeft->GetPageId() != nodeRight->GetPageId()) {
153                 return nodeLeft->GetPageId() < nodeRight->GetPageId();
154             }
155             if (nodeLeft->GetDepth() != nodeRight->GetDepth()) {
156                 return nodeLeft->GetDepth() < nodeRight->GetDepth();
157             }
158             return nodeLeft < nodeRight;
159         }
160     };
161 
162     using PageDirtySet = std::set<RefPtr<FrameNode>, NodeCompare<RefPtr<FrameNode>>>;
163     using RootDirtyMap = std::map<uint32_t, PageDirtySet>;
164 
165     std::list<RefPtr<FrameNode>> dirtyLayoutNodes_;
166     RootDirtyMap dirtyRenderNodes_;
167     std::list<PredictTask> predictTask_;
168     std::list<std::function<void()>> afterLayoutTasks_;
169     std::list<std::function<void()>> afterRenderTasks_;
170     std::list<std::function<void()>> persistAfterLayoutTasks_;
171     std::list<std::function<void()>> syncGeometryNodeTasks_;
172 
173     uint32_t currentPageId_ = 0;
174     bool is64BitSystem_ = false;
175     bool isLayouting_ = false;
176 
177     FrameInfo* frameInfo_ = nullptr;
178 
179     std::map<WeakPtr<CustomNode>, bool> delayJsActiveNodes_;
180 
181     static uint64_t frameId_;
182 
183     ACE_DISALLOW_COPY_AND_MOVE(UITaskScheduler);
184 };
185 
186 } // namespace OHOS::Ace::NG
187 
188 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_PIPELINE_NG_UI_TASK_SCHEDULER_H
189