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     void ReleaseNodeBucket();
65     void ReleaseNodeMemory();
SetMainTask(gcTask hook)66     void SetMainTask(gcTask hook) {
67         mainTask_ = hook;
68     }
69 
70     void AddToOffTreeNodeBucket(const std::shared_ptr<RSBaseRenderNode>& node);
71     void ReleaseOffTreeNodeBucket();
72     void ReleaseFromTree();
73 
74     static void DrawableDestructor(DrawableV2::RSRenderNodeDrawableAdapter* ptr);
75     void DrawableDestructorInner(DrawableV2::RSRenderNodeDrawableAdapter* ptr);
76     void ReleaseDrawableBucket();
77     void ReleaseDrawableMemory();
SetRenderTask(gcTask hook)78     void SetRenderTask(gcTask hook) {
79         renderTask_ = hook;
80     }
81 
SetGCTaskEnable(bool isEnable)82     inline void SetGCTaskEnable(bool isEnable)
83     {
84         isEnable_.store(isEnable);
85     }
86 
87 private:
88     GCLevel JudgeGCLevel(uint32_t remainBucketSize);
89 
90     gcTask mainTask_ = nullptr;
91     gcTask renderTask_ = nullptr;
92 
93     std::atomic<bool> isEnable_ = true;
94     std::queue<std::vector<std::shared_ptr<RSBaseRenderNode>>> offTreeBucket_;
95     RSThresholdDetector<uint32_t> offTreeBucketThrDetector_ = RSThresholdDetector<uint32_t>(
96         OFFTREE_BUCKET_THR_LOW, OFFTREE_BUCKET_THR_HIGH);
97     std::queue<std::vector<RSRenderNode*>> nodeBucket_;
98     RSThresholdDetector<uint32_t> nodeBucketThrDetector_ = RSThresholdDetector<uint32_t>(
99         NODE_BUCKET_THR_LOW, NODE_BUCKET_THR_HIGH);
100     std::queue<std::vector<DrawableV2::RSRenderNodeDrawableAdapter*>> drawableBucket_;
101     RSThresholdDetector<uint32_t> drawableBucketThrDetector_ = RSThresholdDetector<uint32_t>(
102         DRAWABLE_BUCKET_THR_LOW, DRAWABLE_BUCKET_THR_HIGH);
103     std::mutex nodeMutex_;
104     std::mutex drawableMutex_;
105     GCLevel nodeGCLevel_ = GCLevel::IDLE;
106     GCLevel drawableGCLevel_ = GCLevel::IDLE;
107 };
108 } // namespace Rosen
109 } // namespace OHOS
110 
111 #endif // RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_RENDER_NODE_GC_H
112