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