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 "drawable/rs_render_node_drawable_adapter.h" 27 #include "pipeline/rs_render_node.h" 28 29 namespace OHOS { 30 namespace Rosen { 31 namespace { 32 const int BUCKET_MAX_SIZE = 50; 33 const int OFF_TREE_BUCKET_MAX_SIZE = 500; 34 const char* OFF_TREE_TASK = "ReleaseNodeFromTree"; 35 const char* DELETE_NODE_TASK = "ReleaseNodeMemory"; 36 const char* DELETE_DRAWABLE_TASK = "ReleaseDrawableMemory"; 37 } 38 class RSB_EXPORT RSRenderNodeGC { 39 public: 40 typedef void (*gcTask)(RSTaskMessage::RSTask, const std::string&, int64_t, 41 AppExecFwk::EventQueue::Priority); 42 43 static RSRenderNodeGC& Instance(); 44 45 static void NodeDestructor(RSRenderNode* ptr); 46 void NodeDestructorInner(RSRenderNode* ptr); 47 void ReleaseNodeBucket(); 48 void ReleaseNodeMemory(); SetMainTask(gcTask hook)49 void SetMainTask(gcTask hook) { 50 mainTask_ = hook; 51 } 52 53 void AddToOffTreeNodeBucket(const std::shared_ptr<RSBaseRenderNode>& node); 54 void ReleaseOffTreeNodeBucket(); 55 void ReleaseFromTree(); 56 57 static void DrawableDestructor(DrawableV2::RSRenderNodeDrawableAdapter* ptr); 58 void DrawableDestructorInner(DrawableV2::RSRenderNodeDrawableAdapter* ptr); 59 void ReleaseDrawableBucket(); 60 void ReleaseDrawableMemory(); SetRenderTask(gcTask hook)61 void SetRenderTask(gcTask hook) { 62 renderTask_ = hook; 63 } 64 SetGCTaskEnable(bool isEnable)65 inline void SetGCTaskEnable(bool isEnable) 66 { 67 isEnable_.store(isEnable); 68 } 69 70 private: 71 gcTask mainTask_ = nullptr; 72 gcTask renderTask_ = nullptr; 73 74 std::atomic<bool> isEnable_ = true; 75 std::queue<std::vector<std::shared_ptr<RSBaseRenderNode>>> offTreeBucket_; 76 std::queue<std::vector<RSRenderNode*>> nodeBucket_; 77 std::queue<std::vector<DrawableV2::RSRenderNodeDrawableAdapter*>> drawableBucket_; 78 std::mutex nodeMutex_; 79 std::mutex drawableMutex_; 80 }; 81 } // namespace Rosen 82 } // namespace OHOS 83 84 #endif // RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_RENDER_NODE_GC_H 85