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