1 /*
2  * Copyright (c) 2023 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 "memory/rs_tag_tracker.h"
17 
18 #include "platform/common/rs_log.h"
19 
20 namespace OHOS::Rosen {
21 namespace {
22 static std::atomic<bool> g_releaseResourceEnabled_ = true;
23 }
RSTagTracker(Drawing::GPUContext * gpuContext,RSTagTracker::TAGTYPE tagType)24 RSTagTracker::RSTagTracker(Drawing::GPUContext* gpuContext, RSTagTracker::TAGTYPE tagType) : gpuContext_(gpuContext)
25 {
26     if (!gpuContext_) {
27         return;
28     }
29     if (!g_releaseResourceEnabled_) {
30         return;
31     }
32 #if defined (RS_ENABLE_GL) || defined (RS_ENABLE_VK)
33     Drawing::GPUResourceTag tag(0, 0, 0, tagType, TagType2String(tagType));
34     gpuContext_->SetCurrentGpuResourceTag(tag);
35 #endif
36 }
37 
UpdateReleaseResourceEnabled(bool releaseResEnabled)38 void RSTagTracker::UpdateReleaseResourceEnabled(bool releaseResEnabled)
39 {
40     g_releaseResourceEnabled_ = releaseResEnabled;
41 }
42 
TagType2String(TAGTYPE type)43 std::string RSTagTracker::TagType2String(TAGTYPE type)
44 {
45     std::string tagType;
46     switch (type) {
47         case TAG_SAVELAYER_DRAW_NODE :
48             tagType = "savelayer_draw_node";
49             break;
50         case TAG_RESTORELAYER_DRAW_NODE :
51             tagType = "restorelayer_draw_node";
52             break;
53         case TAG_SAVELAYER_COLOR_FILTER :
54             tagType = "savelayer_color_filter";
55             break;
56         case TAG_FILTER :
57             tagType = "filter";
58             break;
59         case TAG_CAPTURE :
60             tagType = "capture";
61             break;
62         case TAG_SUB_THREAD :
63             tagType = "sub_thread";
64             break;
65         case TAG_ACQUIRE_SURFACE :
66             tagType = "acquire_surface";
67             break;
68         case TAG_RENDER_FRAME :
69             tagType = "render_frame";
70             break;
71         case TAG_DRAW_SURFACENODE :
72             tagType = "draw_surface_node";
73             break;
74         case TAG_UNTAGGED :
75             tagType = "untagged";
76             break;
77         default :
78             tagType = "";
79             break;
80     }
81     return tagType;
82 }
83 
RSTagTracker(Drawing::GPUContext * gpuContext,NodeId nodeId,RSTagTracker::TAGTYPE tagType,const std::string & name)84 RSTagTracker::RSTagTracker(Drawing::GPUContext* gpuContext, NodeId nodeId,
85     RSTagTracker::TAGTYPE tagType, const std::string& name)
86     : gpuContext_(gpuContext)
87 {
88     if (!gpuContext_) {
89         return;
90     }
91     if (!g_releaseResourceEnabled_) {
92         return;
93     }
94 #if defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK)
95     Drawing::GPUResourceTag tag(ExtractPid(nodeId), 0, nodeId, tagType, name);
96     gpuContext_->SetCurrentGpuResourceTag(tag);
97 #endif
98 }
99 
RSTagTracker(Drawing::GPUContext * gpuContext,Drawing::GPUResourceTag & tag)100 RSTagTracker::RSTagTracker(Drawing::GPUContext* gpuContext, Drawing::GPUResourceTag& tag) : gpuContext_(gpuContext)
101 {
102     if (!gpuContext_) {
103         return;
104     }
105     if (!g_releaseResourceEnabled_) {
106         return;
107     }
108 #if defined (RS_ENABLE_GL) || defined (RS_ENABLE_VK)
109     gpuContext_->SetCurrentGpuResourceTag(tag);
110 #endif
111 }
112 
SetTagEnd()113 void RSTagTracker::SetTagEnd()
114 {
115     if (!gpuContext_) {
116         return;
117     }
118     if (!g_releaseResourceEnabled_) {
119         return;
120     }
121     isSetTagEnd_ = true;
122 #if defined (RS_ENABLE_GL) || defined (RS_ENABLE_VK)
123     Drawing::GPUResourceTag tagEnd(0, 0, 0, 0, "SetTagEnd");
124     gpuContext_->SetCurrentGpuResourceTag(tagEnd);
125 #endif
126 }
127 
~RSTagTracker()128 RSTagTracker::~RSTagTracker()
129 {
130     if (!g_releaseResourceEnabled_) {
131         return;
132     }
133 #if defined (RS_ENABLE_GL) || defined (RS_ENABLE_VK)
134     // Set empty tag to notify skia that the tag is complete
135     if (!isSetTagEnd_ && gpuContext_) {
136         Drawing::GPUResourceTag tagEnd(0, 0, 0, 0, "~RSTagTracker");
137         gpuContext_->SetCurrentGpuResourceTag(tagEnd);
138     }
139 #endif
140 }
141 } // namespace OHOS::Rosen