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 };
41 
42 class RSB_EXPORT RSContext : public std::enable_shared_from_this<RSContext> {
43 public:
44     RSContext() = default;
45     ~RSContext() = default;
46     RSContext(const RSContext&) = delete;
47     RSContext(const RSContext&&) = delete;
48     RSContext& operator=(const RSContext&) = delete;
49     RSContext& operator=(const RSContext&&) = delete;
50 
51 #ifndef ROSEN_CROSS_PLATFORM
52     struct BufferInfo {
53         NodeId id = INVALID_NODEID;
54         sptr<SurfaceBuffer> buffer;
55         sptr<IConsumerSurface> consumer;
56         bool useFence = false;
57     };
58 #endif
59 
60     enum PurgeType {
61         NONE,
62         GENTLY,
63         STRONGLY
64     };
65 
GetMutableNodeMap()66     RSRenderNodeMap& GetMutableNodeMap()
67     {
68         return nodeMap;
69     }
70 
GetNodeMap()71     const RSRenderNodeMap& GetNodeMap() const
72     {
73         return nodeMap;
74     }
75 
GetMutableFrameRateLinkerMap()76     RSRenderFrameRateLinkerMap& GetMutableFrameRateLinkerMap()
77     {
78         return frameRateLinkerMap;
79     }
80 
GetFrameRateLinkerMap()81     const RSRenderFrameRateLinkerMap& GetFrameRateLinkerMap() const
82     {
83         return frameRateLinkerMap;
84     }
85 
GetGlobalRootRenderNode()86     const std::shared_ptr<RSBaseRenderNode>& GetGlobalRootRenderNode() const
87     {
88         return globalRootRenderNode_;
89     }
90 
GetInteractiveImplictAnimatorMap()91     RSRenderInteractiveImplictAnimatorMap& GetInteractiveImplictAnimatorMap()
92     {
93         return interactiveImplictAnimatorMap_;
94     }
95 
96     void RegisterAnimatingRenderNode(const std::shared_ptr<RSRenderNode>& nodePtr);
97     void UnregisterAnimatingRenderNode(NodeId id);
98 
GetTransactionTimestamp()99     uint64_t GetTransactionTimestamp() const
100     {
101         return transactionTimestamp_;
102     }
103 
GetCurrentTimestamp()104     uint64_t GetCurrentTimestamp() const
105     {
106         return currentTimestamp_;
107     }
108     // add node info after cmd data process
109     void AddActiveNode(const std::shared_ptr<RSRenderNode>& node);
110     bool HasActiveNode(const std::shared_ptr<RSRenderNode>& node);
111 
112     void AddPendingSyncNode(const std::shared_ptr<RSRenderNode> node);
113 
114     void MarkNeedPurge(ClearMemoryMoment moment, PurgeType purgeType);
115 
SetVsyncRequestFunc(const std::function<void ()> & taskRunner)116     void SetVsyncRequestFunc(const std::function<void()>& taskRunner)
117     {
118         vsyncRequestFunc_ = taskRunner;
119     }
120 
RequestVsync()121     void RequestVsync() const
122     {
123         if (vsyncRequestFunc_) {
124             vsyncRequestFunc_();
125         }
126     }
127 
SetTaskRunner(const std::function<void (const std::function<void ()> &,bool)> & taskRunner)128     void SetTaskRunner(const std::function<void(const std::function<void()>&, bool)>& taskRunner)
129     {
130         taskRunner_ = taskRunner;
131     }
132     void PostTask(const std::function<void()>& task, bool isSyncTask = false) const
133     {
134         if (taskRunner_) {
135             taskRunner_(task, isSyncTask);
136         }
137     }
138 
SetRTTaskRunner(const std::function<void (const std::function<void ()> &)> & taskRunner)139     void SetRTTaskRunner(const std::function<void(const std::function<void()>&)>& taskRunner)
140     {
141         rttaskRunner_ = taskRunner;
142     }
143 
PostRTTask(const std::function<void ()> & task)144     void PostRTTask(const std::function<void()>& task) const
145     {
146         if (rttaskRunner_) {
147             rttaskRunner_(task);
148         }
149     }
150 
151     void SetClearMoment(ClearMemoryMoment moment);
152     ClearMemoryMoment GetClearMoment() const;
153 
SetRequestedNextVsyncAnimate(bool requestedNextVsyncAnimate)154     void SetRequestedNextVsyncAnimate(bool requestedNextVsyncAnimate)
155     {
156         requestedNextVsyncAnimate_ = requestedNextVsyncAnimate;
157     }
158 
IsRequestedNextVsyncAnimate()159     bool IsRequestedNextVsyncAnimate() const
160     {
161         return requestedNextVsyncAnimate_;
162     }
163 
164     // save some need sync finish to client animations in list
165     void AddSyncFinishAnimationList(NodeId nodeId, AnimationId animationId);
166 
AddSubSurfaceCntUpdateInfo(SubSurfaceCntUpdateInfo info)167     void AddSubSurfaceCntUpdateInfo(SubSurfaceCntUpdateInfo info)
168     {
169         subSurfaceCntUpdateInfo_.emplace_back(info);
170     }
171 private:
172     // This function is used for initialization, should be called once after constructor.
173     void Initialize();
174     RSRenderNodeMap nodeMap;
175     RSRenderFrameRateLinkerMap frameRateLinkerMap;
176     RSRenderInteractiveImplictAnimatorMap interactiveImplictAnimatorMap_;
177     // The root of render node tree, Note: this node is not the animation fallback node.
178     std::shared_ptr<RSBaseRenderNode> globalRootRenderNode_ = std::make_shared<RSRenderNode>(0, true);
179     // The list of animating nodes in this frame.
180     std::unordered_map<NodeId, std::weak_ptr<RSRenderNode>> animatingNodeList_;
181     std::unordered_map<NodeId, std::weak_ptr<RSRenderNode>> curFrameAnimatingNodeList_;
182     // This flag indicates that a request for the next Vsync is needed when moving to the animation fallback node.
183     bool requestedNextVsyncAnimate_ = false;
184     std::vector<std::pair<NodeId, AnimationId>> needSyncFinishAnimationList_;
185     PurgeType purgeType_ = PurgeType::NONE;
186     ClearMemoryMoment clearMoment_ = ClearMemoryMoment::NO_CLEAR;
187 
188     uint64_t transactionTimestamp_ = 0;
189     uint64_t currentTimestamp_ = 0;
190     std::function<void(const std::function<void()>&, bool)> taskRunner_;
191     std::function<void(const std::function<void()>&)> rttaskRunner_;
192     std::function<void()> vsyncRequestFunc_;
193     // Collect all active Nodes sorted by root node id in this frame.
194     std::unordered_map<NodeId, std::unordered_map<NodeId, std::weak_ptr<RSRenderNode>>> activeNodesInRoot_;
195     std::mutex activeNodesInRootMutex_;
196 
197     std::unordered_map<NodeId, std::weak_ptr<RSRenderNode>> pendingSyncNodes_;
198     std::vector<SubSurfaceCntUpdateInfo> subSurfaceCntUpdateInfo_;
199 
200     friend class RSRenderThread;
201     friend class RSMainThread;
202     friend class RSDrawFrame;
203     friend class RSSurfaceCaptureTaskParallel;
204 #ifdef RS_PROFILER_ENABLED
205     friend class RSProfiler;
206 #endif
207 };
208 
209 } // namespace Rosen
210 } // namespace OHOS
211 
212 #endif // ROSEN_RENDER_SERVICE_BASE_PIPELINE_RS_CONTEXT_H
213