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 #include "pipeline/rs_render_node_map.h" 22 #include "pipeline/rs_render_frame_rate_linker_map.h" 23 24 namespace OHOS { 25 namespace Rosen { 26 enum ClearMemoryMoment : uint32_t { 27 FILTER_INVALID = 0, 28 PROCESS_EXIT, 29 COMMON_SURFACE_NODE_HIDE, 30 SCENEBOARD_SURFACE_NODE_HIDE, 31 LOW_MEMORY, 32 NO_CLEAR, 33 }; 34 35 class RSB_EXPORT RSContext : public std::enable_shared_from_this<RSContext> { 36 public: 37 RSContext() = default; 38 ~RSContext() = default; 39 RSContext(const RSContext&) = delete; 40 RSContext(const RSContext&&) = delete; 41 RSContext& operator=(const RSContext&) = delete; 42 RSContext& operator=(const RSContext&&) = delete; 43 44 enum PurgeType { 45 NONE, 46 GENTLY, 47 STRONGLY 48 }; 49 GetMutableNodeMap()50 RSRenderNodeMap& GetMutableNodeMap() 51 { 52 return nodeMap; 53 } 54 GetNodeMap()55 const RSRenderNodeMap& GetNodeMap() const 56 { 57 return nodeMap; 58 } 59 GetMutableFrameRateLinkerMap()60 RSRenderFrameRateLinkerMap& GetMutableFrameRateLinkerMap() 61 { 62 return frameRateLinkerMap; 63 } 64 GetFrameRateLinkerMap()65 const RSRenderFrameRateLinkerMap& GetFrameRateLinkerMap() const 66 { 67 return frameRateLinkerMap; 68 } 69 GetGlobalRootRenderNode()70 const std::shared_ptr<RSBaseRenderNode>& GetGlobalRootRenderNode() const 71 { 72 return globalRootRenderNode_; 73 } 74 75 void RegisterAnimatingRenderNode(const std::shared_ptr<RSRenderNode>& nodePtr); 76 void UnregisterAnimatingRenderNode(NodeId id); 77 GetTransactionTimestamp()78 uint64_t GetTransactionTimestamp() const 79 { 80 return transactionTimestamp_; 81 } 82 GetCurrentTimestamp()83 uint64_t GetCurrentTimestamp() const 84 { 85 return currentTimestamp_; 86 } 87 // add node info after cmd data process 88 void AddActiveNode(const std::shared_ptr<RSRenderNode>& node); 89 bool HasActiveNode(const std::shared_ptr<RSRenderNode>& node); 90 91 void MarkNeedPurge(ClearMemoryMoment moment, PurgeType purgeType); 92 SetVsyncRequestFunc(const std::function<void ()> & taskRunner)93 void SetVsyncRequestFunc(const std::function<void()>& taskRunner) 94 { 95 vsyncRequestFunc_ = taskRunner; 96 } 97 RequestVsync()98 void RequestVsync() const 99 { 100 vsyncRequestFunc_(); 101 } 102 SetTaskRunner(const std::function<void (const std::function<void ()> &,bool)> & taskRunner)103 void SetTaskRunner(const std::function<void(const std::function<void()>&, bool)>& taskRunner) 104 { 105 taskRunner_ = taskRunner; 106 } 107 void PostTask(const std::function<void()>& task, bool isSyncTask = false) const 108 { 109 if (taskRunner_) { 110 taskRunner_(task, isSyncTask); 111 } 112 } 113 114 private: 115 // This function is used for initialization, should be called once after constructor. 116 void Initialize(); 117 RSRenderNodeMap nodeMap; 118 RSRenderFrameRateLinkerMap frameRateLinkerMap; 119 // The root of render node tree, Note: this node is not the animation fallback node. 120 std::shared_ptr<RSBaseRenderNode> globalRootRenderNode_ = std::make_shared<RSRenderNode>(0, true); 121 // The list of animating nodes in this frame. 122 std::unordered_map<NodeId, std::weak_ptr<RSRenderNode>> animatingNodeList_; 123 PurgeType purgeType_ = PurgeType::NONE; 124 ClearMemoryMoment clearMoment_ = ClearMemoryMoment::NO_CLEAR; 125 126 uint64_t transactionTimestamp_ = 0; 127 uint64_t currentTimestamp_ = 0; 128 std::function<void(const std::function<void()>&, bool)> taskRunner_; 129 std::function<void()> vsyncRequestFunc_; 130 // Collect all active Nodes sorted by root node id in this frame. 131 std::unordered_map<NodeId, std::unordered_map<NodeId, std::weak_ptr<RSRenderNode>>> activeNodesInRoot_; 132 std::mutex activeNodesInRootMutex_; 133 134 friend class RSRenderThread; 135 friend class RSMainThread; 136 }; 137 138 } // namespace Rosen 139 } // namespace OHOS 140 141 #endif // ROSEN_RENDER_SERVICE_BASE_PIPELINE_RS_CONTEXT_H 142