1 /*
2 * Copyright (c) 2025 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 #include "feature/memory_info_manager/rs_memory_info_manager.h"
17
18 #include "memory/rs_memory_track.h"
19 #ifdef ROSEN_OHOS
20 #include "mem_mgr_client.h"
21 #endif
22 #include "platform/common/rs_system_properties.h"
23
24 namespace OHOS {
25 namespace Rosen {
SetSurfaceMemoryInfo(bool onTree,const std::shared_ptr<RSSurfaceHandler> handler)26 void RSMemoryInfoManager::SetSurfaceMemoryInfo(bool onTree, const std::shared_ptr<RSSurfaceHandler> handler)
27 {
28 if (!RSSystemProperties::GetRSMemoryInfoManagerParam()) {
29 return;
30 }
31 if (!handler || !handler->GetBuffer()) {
32 return;
33 }
34 auto buffer = handler->GetBuffer();
35 int fd = buffer->GetFileDescriptor();
36 if (!onTree && MemoryTrack::Instance().GetGlobalRootNodeStatusChangeFlag()) {
37 Memory::MemMgrClient::GetInstance().SetDmabufInfo(fd,
38 Memory::DmabufRsInfo::SURFACE_OFF_TREE_IN_ROOT);
39 } else if (!onTree) {
40 Memory::MemMgrClient::GetInstance().SetDmabufInfo(fd,
41 Memory::DmabufRsInfo::SURFACE_OFF_TREE);
42 } else if (onTree && MemoryTrack::Instance().GetGlobalRootNodeStatusChangeFlag()) {
43 Memory::MemMgrClient::GetInstance().SetDmabufInfo(fd,
44 Memory::DmabufRsInfo::SURFACE_ON_TREE_IN_ROOT);
45 } else {
46 Memory::MemMgrClient::GetInstance().SetDmabufInfo(fd,
47 Memory::DmabufRsInfo::SURFACE_ON_TREE);
48 }
49 }
50
SetImageMemoryInfo(const std::shared_ptr<Media::PixelMap> pixelMap)51 void RSMemoryInfoManager::SetImageMemoryInfo(const std::shared_ptr<Media::PixelMap> pixelMap)
52 {
53 if (!RSSystemProperties::GetRSMemoryInfoManagerParam()) {
54 return;
55 }
56 if (!pixelMap || pixelMap->GetAllocatorType() != Media::AllocatorType::DMA_ALLOC) {
57 return;
58 }
59 SurfaceBuffer *sbBuffer = reinterpret_cast<SurfaceBuffer*>(pixelMap->GetFd());
60 if (!sbBuffer) {
61 return;
62 }
63 NODE_ON_TREE_STATUS res = NODE_ON_TREE_STATUS::STATUS_INVALID;
64 if (RSSystemProperties::GetClosePixelMapFdEnabled()) {
65 res = MemoryTrack::Instance().GetNodeOnTreeStatus(pixelMap->GetPixels());
66 } else {
67 res = MemoryTrack::Instance().GetNodeOnTreeStatus(pixelMap->GetFd());
68 }
69 if (res == NODE_ON_TREE_STATUS::STATUS_OFF_TREE) {
70 Memory::MemMgrClient::GetInstance().SetDmabufInfo(sbBuffer->GetFileDescriptor(),
71 Memory::DmabufRsInfo::IMAGE_OFF_TREE);
72 } else if (res == NODE_ON_TREE_STATUS::STATUS_OFF_TREE_IN_ROOT) {
73 Memory::MemMgrClient::GetInstance().SetDmabufInfo(sbBuffer->GetFileDescriptor(),
74 Memory::DmabufRsInfo::IMAGE_OFF_TREE_IN_ROOT);
75 } else if (res == NODE_ON_TREE_STATUS::STATUS_ON_TREE) {
76 Memory::MemMgrClient::GetInstance().SetDmabufInfo(sbBuffer->GetFileDescriptor(),
77 Memory::DmabufRsInfo::IMAGE_ON_TREE);
78 } else if (res == NODE_ON_TREE_STATUS::STATUS_ON_TREE_IN_ROOT) {
79 Memory::MemMgrClient::GetInstance().SetDmabufInfo(sbBuffer->GetFileDescriptor(),
80 Memory::DmabufRsInfo::IMAGE_ON_TREE_IN_ROOT);
81 }
82 }
83
RecordNodeOnTreeStatus(bool flag,NodeId nodeId,NodeId instanceRootNodeId)84 void RSMemoryInfoManager::RecordNodeOnTreeStatus(bool flag, NodeId nodeId, NodeId instanceRootNodeId)
85 {
86 if (!RSSystemProperties::GetRSMemoryInfoManagerParam()) {
87 return;
88 }
89 if (nodeId == instanceRootNodeId) {
90 MemoryTrack::Instance().SetGlobalRootNodeStatusChangeFlag(true);
91 }
92 MemoryTrack::Instance().SetNodeOnTreeStatus(nodeId,
93 MemoryTrack::Instance().GetGlobalRootNodeStatusChangeFlag(), flag);
94 }
95
ResetRootNodeStatusChangeFlag(NodeId nodeId,NodeId instanceRootNodeId)96 void RSMemoryInfoManager::ResetRootNodeStatusChangeFlag(NodeId nodeId, NodeId instanceRootNodeId)
97 {
98 if (!RSSystemProperties::GetRSMemoryInfoManagerParam()) {
99 return;
100 }
101 if (nodeId != instanceRootNodeId) {
102 return;
103 }
104 MemoryTrack::Instance().SetGlobalRootNodeStatusChangeFlag(false);
105 }
106 }
107 }