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