• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 ROSEN_RENDER_SERVICE_BASE_PIPELINE_RS_CONTEXT_H
17 #define ROSEN_RENDER_SERVICE_BASE_PIPELINE_RS_CONTEXT_H
18 
19 #include <cstdint>
20 #include "common/rs_macros.h"
21 #ifndef ROSEN_CROSS_PLATFORM
22 #include "iconsumer_surface.h"
23 #include "surface_buffer.h"
24 #include "sync_fence.h"
25 #endif
26 #include "animation/rs_render_interactive_implict_animator_map.h"
27 #include "feature/capture/rs_ui_capture_helper.h"
28 #include "pipeline/rs_render_node_map.h"
29 #include "pipeline/rs_render_frame_rate_linker_map.h"
30 
31 namespace OHOS {
32 namespace Rosen {
33 enum ClearMemoryMoment : uint32_t {
34     FILTER_INVALID = 0,
35     PROCESS_EXIT,
36     COMMON_SURFACE_NODE_HIDE,
37     SCENEBOARD_SURFACE_NODE_HIDE,
38     LOW_MEMORY,
39     NO_CLEAR,
40     DEFAULT_CLEAN,
41     RECLAIM_CLEAN,
42 };
43 
44 class RSB_EXPORT RSContext : public std::enable_shared_from_this<RSContext> {
45 public:
46     RSContext() = default;
47     ~RSContext() = default;
48     RSContext(const RSContext&) = delete;
49     RSContext(const RSContext&&) = delete;
50     RSContext& operator=(const RSContext&) = delete;
51     RSContext& operator=(const RSContext&&) = delete;
52 
53 #ifndef ROSEN_CROSS_PLATFORM
54     struct BufferInfo {
55         NodeId id = INVALID_NODEID;
56         sptr<SurfaceBuffer> buffer;
57         sptr<IConsumerSurface> consumer;
58         bool useFence = false;
59     };
60 #endif
61 
62     enum PurgeType {
63         NONE,
64         GENTLY,
65         STRONGLY
66     };
67 
GetMutableNodeMap()68     RSRenderNodeMap& GetMutableNodeMap()
69     {
70         return nodeMap;
71     }
72 
GetNodeMap()73     const RSRenderNodeMap& GetNodeMap() const
74     {
75         return nodeMap;
76     }
77 
GetAnimatingNodeList()78     const std::unordered_map<NodeId, std::weak_ptr<RSRenderNode>>& GetAnimatingNodeList() const
79     {
80         return animatingNodeList_;
81     }
82 
GetMutableFrameRateLinkerMap()83     RSRenderFrameRateLinkerMap& GetMutableFrameRateLinkerMap()
84     {
85         return frameRateLinkerMap;
86     }
87 
GetFrameRateLinkerMap()88     const RSRenderFrameRateLinkerMap& GetFrameRateLinkerMap() const
89     {
90         return frameRateLinkerMap;
91     }
92 
GetGlobalRootRenderNode()93     const std::shared_ptr<RSBaseRenderNode>& GetGlobalRootRenderNode() const
94     {
95         return globalRootRenderNode_;
96     }
97 
GetInteractiveImplictAnimatorMap()98     RSRenderInteractiveImplictAnimatorMap& GetInteractiveImplictAnimatorMap()
99     {
100         return interactiveImplictAnimatorMap_;
101     }
102 
103     void RegisterAnimatingRenderNode(const std::shared_ptr<RSRenderNode>& nodePtr);
104     void UnregisterAnimatingRenderNode(NodeId id);
105 
GetTransactionTimestamp()106     uint64_t GetTransactionTimestamp() const
107     {
108         return transactionTimestamp_;
109     }
110 
GetCurrentTimestamp()111     uint64_t GetCurrentTimestamp() const
112     {
113         return currentTimestamp_;
114     }
115     // add node info after cmd data process
116     void AddActiveNode(const std::shared_ptr<RSRenderNode>& node);
117     bool HasActiveNode(const std::shared_ptr<RSRenderNode>& node);
118 
119     void AddPendingSyncNode(const std::shared_ptr<RSRenderNode> node);
120 
121     void MarkNeedPurge(ClearMemoryMoment moment, PurgeType purgeType);
122 
SetVsyncRequestFunc(const std::function<void ()> & taskRunner)123     void SetVsyncRequestFunc(const std::function<void()>& taskRunner)
124     {
125         vsyncRequestFunc_ = taskRunner;
126     }
127 
RequestVsync()128     void RequestVsync() const
129     {
130         if (vsyncRequestFunc_) {
131             vsyncRequestFunc_();
132         }
133     }
134 
SetTaskRunner(const std::function<void (const std::function<void ()> &,bool)> & taskRunner)135     void SetTaskRunner(const std::function<void(const std::function<void()>&, bool)>& taskRunner)
136     {
137         taskRunner_ = taskRunner;
138     }
139     void PostTask(const std::function<void()>& task, bool isSyncTask = false) const
140     {
141         if (taskRunner_) {
142             taskRunner_(task, isSyncTask);
143         }
144     }
145 
SetRTTaskRunner(const std::function<void (const std::function<void ()> &)> & taskRunner)146     void SetRTTaskRunner(const std::function<void(const std::function<void()>&)>& taskRunner)
147     {
148         rttaskRunner_ = taskRunner;
149     }
150 
PostRTTask(const std::function<void ()> & task)151     void PostRTTask(const std::function<void()>& task) const
152     {
153         if (rttaskRunner_) {
154             rttaskRunner_(task);
155         }
156     }
157 
158     void SetClearMoment(ClearMemoryMoment moment);
159     ClearMemoryMoment GetClearMoment() const;
160 
161     // For LTPO: Transmit data in uiFrameworkTypeTable and uiFrameworkDirtyNodes
162     // between RSRenderNode and HGM model by RSContext.
SetUiFrameworkTypeTable(const std::vector<std::string> & table)163     void SetUiFrameworkTypeTable(const std::vector<std::string>& table)
164     {
165         uiFrameworkTypeTable_ = table;
166     }
167 
GetUiFrameworkTypeTable()168     const std::vector<std::string>& GetUiFrameworkTypeTable() const
169     {
170         return uiFrameworkTypeTable_;
171     }
172 
UpdateUIFrameworkDirtyNodes(std::weak_ptr<RSRenderNode> uiFwkDirtyNode)173     void UpdateUIFrameworkDirtyNodes(std::weak_ptr<RSRenderNode> uiFwkDirtyNode)
174     {
175         uiFrameworkDirtyNodes_.emplace_back(uiFwkDirtyNode);
176     }
177 
GetUiFrameworkDirtyNodes()178     std::vector<std::weak_ptr<RSRenderNode>>& GetUiFrameworkDirtyNodes()
179     {
180         return uiFrameworkDirtyNodes_;
181     }
182 
SetRequestedNextVsyncAnimate(bool requestedNextVsyncAnimate)183     void SetRequestedNextVsyncAnimate(bool requestedNextVsyncAnimate)
184     {
185         requestedNextVsyncAnimate_ = requestedNextVsyncAnimate;
186     }
187 
IsRequestedNextVsyncAnimate()188     bool IsRequestedNextVsyncAnimate() const
189     {
190         return requestedNextVsyncAnimate_;
191     }
192 
193     // save some need sync finish to client animations in list
194     void AddSyncFinishAnimationList(NodeId nodeId, AnimationId animationId, uint64_t token);
195 
AddSubSurfaceCntUpdateInfo(SubSurfaceCntUpdateInfo info)196     void AddSubSurfaceCntUpdateInfo(SubSurfaceCntUpdateInfo info)
197     {
198         subSurfaceCntUpdateInfo_.emplace_back(info);
199     }
200 
GetUiCaptureHelper()201     RSUiCaptureHelper& GetUiCaptureHelper()
202     {
203         if (!uiCaptureHelper_) {
204             uiCaptureHelper_ = std::make_unique<RSUiCaptureHelper>();
205         }
206         return *uiCaptureHelper_;
207     }
208 
GetUnirenderVisibleLeashWindowCount()209     uint32_t GetUnirenderVisibleLeashWindowCount()
210     {
211         return visibleLeashWindowCount_.load();
212     }
213 
SetUnirenderVisibleLeashWindowCount(uint32_t count)214     void SetUnirenderVisibleLeashWindowCount(uint32_t count)
215     {
216         visibleLeashWindowCount_.store(count);
217     }
218 private:
219     // This function is used for initialization, should be called once after constructor.
220     void Initialize();
221     // This flag indicates that a request for the next Vsync is needed when moving to the animation fallback node.
222     bool requestedNextVsyncAnimate_ = false;
223     PurgeType purgeType_ = PurgeType::NONE;
224     ClearMemoryMoment clearMoment_ = ClearMemoryMoment::NO_CLEAR;
225     uint64_t transactionTimestamp_ = 0;
226     uint64_t currentTimestamp_ = 0;
227     // The root of render node tree, Note: this node is not the animation fallback node.
228     std::shared_ptr<RSBaseRenderNode> globalRootRenderNode_ = std::make_shared<RSRenderNode>(0, true);
229     RSRenderNodeMap nodeMap;
230     std::vector<std::string> uiFrameworkTypeTable_;
231     std::vector<std::weak_ptr<RSRenderNode>> uiFrameworkDirtyNodes_;
232     RSRenderFrameRateLinkerMap frameRateLinkerMap;
233     RSRenderInteractiveImplictAnimatorMap interactiveImplictAnimatorMap_;
234     // The list of animating nodes in this frame.
235     std::unordered_map<NodeId, std::weak_ptr<RSRenderNode>> animatingNodeList_;
236     std::unordered_map<NodeId, std::weak_ptr<RSRenderNode>> curFrameAnimatingNodeList_;
237     std::vector<std::tuple<NodeId, AnimationId, uint64_t>> needSyncFinishAnimationList_;
238 
239     std::function<void(const std::function<void()>&, bool)> taskRunner_;
240     std::function<void(const std::function<void()>&)> rttaskRunner_;
241     std::function<void()> vsyncRequestFunc_;
242     // Collect all active Nodes sorted by root node id in this frame.
243     std::unordered_map<NodeId, std::unordered_map<NodeId, std::weak_ptr<RSRenderNode>>> activeNodesInRoot_;
244     std::mutex activeNodesInRootMutex_;
245 
246     std::unordered_map<NodeId, std::weak_ptr<RSRenderNode>> pendingSyncNodes_;
247     std::vector<SubSurfaceCntUpdateInfo> subSurfaceCntUpdateInfo_;
248 
249     std::unique_ptr<RSUiCaptureHelper> uiCaptureHelper_;
250     std::atomic<uint32_t> visibleLeashWindowCount_ = 0;
251 
252     friend class RSRenderThread;
253     friend class RSMainThread;
254 #ifdef RS_ENABLE_GPU
255     friend class RSDrawFrame;
256 #endif
257     friend class RSSurfaceCaptureTaskParallel;
258 #ifdef RS_PROFILER_ENABLED
259     friend class RSProfiler;
260 #endif
261 };
262 
263 } // namespace Rosen
264 } // namespace OHOS
265 
266 #endif // ROSEN_RENDER_SERVICE_BASE_PIPELINE_RS_CONTEXT_H
267