• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_RENDER_NODE_GC_H
17 #define RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_RENDER_NODE_GC_H
18 
19 #include <cstdint>
20 #include <event_handler.h>
21 #include <mutex>
22 #include <vector>
23 #include <queue>
24 
25 #include "common/rs_thread_handler.h"
26 #include "common/rs_threshold_detector.h"
27 #include "drawable/rs_render_node_drawable_adapter.h"
28 #include "pipeline/rs_render_node.h"
29 
30 namespace OHOS {
31 namespace Rosen {
32 namespace {
33     const int BUCKET_MAX_SIZE = 50;
34     const int OFF_TREE_BUCKET_MAX_SIZE = 500;
35     const char* OFF_TREE_TASK = "ReleaseNodeFromTree";
36     const char* DELETE_NODE_TASK = "ReleaseNodeMemory";
37     const char* DELETE_DRAWABLE_TASK = "ReleaseDrawableMemory";
38     const uint32_t NODE_BUCKET_THR_LOW = 4;
39     const uint32_t NODE_BUCKET_THR_HIGH = 100;
40     const uint32_t DRAWABLE_BUCKET_THR_LOW = 4;
41     const uint32_t DRAWABLE_BUCKET_THR_HIGH = 100;
42     const uint32_t OFFTREE_BUCKET_THR_LOW = 4;
43     const uint32_t OFFTREE_BUCKET_THR_HIGH = 20;
44     const uint32_t GC_LEVEL_THR_IMMEDIATE = 1000;
45     const uint32_t GC_LEVEL_THR_HIGH = 500;
46     const uint32_t GC_LEVEL_THR_LOW = 50;
47 }
48 
49 enum class GCLevel : uint32_t {
50     IMMEDIATE = 0,
51     HIGH,
52     LOW,
53     IDLE,
54 };
55 class RSB_EXPORT RSRenderNodeGC {
56 public:
57     typedef void (*gcTask)(RSTaskMessage::RSTask, const std::string&, int64_t,
58         AppExecFwk::EventQueue::Priority);
59 
60     static RSRenderNodeGC& Instance();
61 
62     static void NodeDestructor(RSRenderNode* ptr);
63     void NodeDestructorInner(RSRenderNode* ptr);
64     bool IsBucketQueueEmpty();
65     void ReleaseNodeBucket();
66     void ReleaseNodeMemory();
SetMainTask(gcTask hook)67     void SetMainTask(gcTask hook) {
68         mainTask_ = hook;
69     }
70 
71     void AddToOffTreeNodeBucket(const std::shared_ptr<RSBaseRenderNode>& node);
72     void AddToOffTreeNodeBucket(pid_t pid,
73         std::unordered_map<NodeId, std::shared_ptr<RSBaseRenderNode>>& renderNodeMap);
74     void ReleaseOffTreeNodeBucket();
75     void ReleaseFromTree();
76 
77     static void DrawableDestructor(DrawableV2::RSRenderNodeDrawableAdapter* ptr);
78     void DrawableDestructorInner(DrawableV2::RSRenderNodeDrawableAdapter* ptr);
79     void ReleaseDrawableBucket();
80     void ReleaseDrawableMemory();
SetRenderTask(gcTask hook)81     void SetRenderTask(gcTask hook) {
82         renderTask_ = hook;
83     }
84 
SetGCTaskEnable(bool isEnable)85     inline void SetGCTaskEnable(bool isEnable)
86     {
87         isEnable_.store(isEnable);
88     }
89 
90 private:
91     GCLevel JudgeGCLevel(uint32_t remainBucketSize);
92     void ReleaseOffTreeNodeForBucket(const RSThresholdDetector<uint32_t>::DetectCallBack &callBack);
93     void ReleaseOffTreeNodeForBucketMap(const RSThresholdDetector<uint32_t>::DetectCallBack &callBack);
94 
95     std::atomic<bool> isEnable_ = true;
96     GCLevel nodeGCLevel_ = GCLevel::IDLE;
97     GCLevel drawableGCLevel_ = GCLevel::IDLE;
98     gcTask mainTask_ = nullptr;
99     gcTask renderTask_ = nullptr;
100 
101     std::queue<std::vector<std::shared_ptr<RSBaseRenderNode>>> offTreeBucket_;
102     std::queue<std::pair<pid_t, std::unordered_map<NodeId, std::shared_ptr<RSBaseRenderNode>>>> offTreeBucketMap_;
103     uint64_t renderNodeNumsInBucketMap_ = 0;
104 
105     RSThresholdDetector<uint32_t> offTreeBucketThrDetector_ = RSThresholdDetector<uint32_t>(
106         OFFTREE_BUCKET_THR_LOW, OFFTREE_BUCKET_THR_HIGH);
107     std::queue<std::vector<RSRenderNode*>> nodeBucket_;
108     RSThresholdDetector<uint32_t> nodeBucketThrDetector_ = RSThresholdDetector<uint32_t>(
109         NODE_BUCKET_THR_LOW, NODE_BUCKET_THR_HIGH);
110     std::queue<std::vector<DrawableV2::RSRenderNodeDrawableAdapter*>> drawableBucket_;
111     RSThresholdDetector<uint32_t> drawableBucketThrDetector_ = RSThresholdDetector<uint32_t>(
112         DRAWABLE_BUCKET_THR_LOW, DRAWABLE_BUCKET_THR_HIGH);
113     std::mutex nodeMutex_;
114     std::mutex drawableMutex_;
115 };
116 } // namespace Rosen
117 } // namespace OHOS
118 
119 #endif // RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_RENDER_NODE_GC_H
120