• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
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 "src/gpu/GrPerfMonitorReporter.h"
17 #ifdef NOT_BUILD_FOR_OHOS_SDK
18 #include <parameters.h>
19 #endif
20 #include "src/core/SkTraceEvent.h"
21 
22 #ifdef SKIA_OHOS
23 const int32_t COUNTER_SIZE = 4;
24 const int32_t CACHE_SIZE = 5;
25 #endif
26 
GetInstance()27 GrPerfMonitorReporter& GrPerfMonitorReporter::GetInstance() {
28     static GrPerfMonitorReporter instance;
29     return instance;
30 }
31 
getTextureStatsData()32 std::map<std::string, std::vector<uint16_t>> GrPerfMonitorReporter::getTextureStatsData() {
33     std::lock_guard<std::mutex> guard(mtx);
34     return fStatsTexture;
35 }
36 
getBlurStatsData()37 std::map<std::string, std::vector<uint16_t>> GrPerfMonitorReporter::getBlurStatsData() {
38     std::lock_guard<std::mutex> guard(mtx);
39     return fStatsBlur;
40 }
41 
getTexturePerfEventData()42 std::map<std::string, TextureEvent> GrPerfMonitorReporter::getTexturePerfEventData() {
43     std::lock_guard<std::mutex> guard(mtx);
44     return fTextureEvent;
45 }
46 
getBlurPerfEventData()47 std::map<std::string, BlurEvent> GrPerfMonitorReporter::getBlurPerfEventData() {
48     std::lock_guard<std::mutex> guard(mtx);
49     return fBlurEvent;
50 }
51 
getStatsCacheData()52 std::set<std::string> GrPerfMonitorReporter::getStatsCacheData() {
53     std::lock_guard<std::mutex> guard(mtx);
54     return fStatsCache;
55 }
56 
resetStatsData()57 void GrPerfMonitorReporter::resetStatsData() {
58     std::lock_guard<std::mutex> guard(mtx);
59     fStatsTexture.clear();
60     fStatsBlur.clear();
61     fStatsCache.clear();
62 }
63 
resetPerfEventData()64 void GrPerfMonitorReporter::resetPerfEventData() {
65     std::lock_guard<std::mutex> guard(mtx);
66     fTextureEvent.clear();
67     fBlurEvent.clear();
68 }
69 
recordTexturePerfEvent(const std::string & nodeName,int32_t pid,int32_t maxBytes,int32_t budgetedBytes,int64_t allocTime)70 void GrPerfMonitorReporter::recordTexturePerfEvent(const std::string& nodeName,
71                                                    int32_t pid,
72                                                    int32_t maxBytes,
73                                                    int32_t budgetedBytes,
74                                                    int64_t allocTime) {
75 #ifdef SKIA_OHOS
76     if (!isOpenPerf() || nodeName.empty() || allocTime < COUNTER_MS_THIRD_TYPE) {
77         return;
78     }
79     TextureEvent tEvent;
80     tEvent.initEvent(pid, maxBytes, budgetedBytes, allocTime);
81     tEvent.fClearCache = isClearCacheLastTime(nodeName);
82     std::lock_guard<std::mutex> guard(mtx);
83     fTextureEvent[nodeName] = tEvent;
84 #endif
85 }
86 
recordBlurPerfEvent(const std::string & nodeName,int32_t pid,uint16_t filterType,float blurRadius,int32_t width,int32_t height,int64_t blurTime)87 void GrPerfMonitorReporter::recordBlurPerfEvent(const std::string& nodeName,
88                                                 int32_t pid,
89                                                 uint16_t filterType,
90                                                 float blurRadius,
91                                                 int32_t width,
92                                                 int32_t height,
93                                                 int64_t blurTime) {
94 #ifdef SKIA_OHOS
95     if (!isOpenPerf() || nodeName.empty() || blurTime < COUNTER_MS_THIRD_TYPE) {
96         return;
97     }
98     BlurEvent bEvent;
99     bEvent.initEvent(pid, filterType, blurRadius, width, height, blurTime);
100     std::lock_guard<std::mutex> guard(mtx);
101     fBlurEvent[nodeName] = bEvent;
102 #endif
103 }
104 
recordTextureNode(const std::string & nodeName,int64_t duration)105 void GrPerfMonitorReporter::recordTextureNode(const std::string& nodeName, int64_t duration) {
106 #ifdef SKIA_OHOS
107     if (!isOpenPerf() || nodeName.empty()) {
108         return;
109     }
110     COUNTER_TYPE cType = static_cast<COUNTER_TYPE>(getSplitRange(duration));
111     if (cType <= COUNTER_INVALID_TYPE) {
112         return;
113     }
114     std::lock_guard<std::mutex> guard(mtx);
115     if (fStatsTexture.find(nodeName) == fStatsTexture.end()) {
116         fStatsTexture[nodeName] = std::vector<uint16_t>(COUNTER_SIZE, 0);
117     }
118     fStatsTexture[nodeName][cType]++;
119 #endif
120 }
121 
recordBlurNode(const std::string & nodeName,int64_t duration)122 void GrPerfMonitorReporter::recordBlurNode(const std::string& nodeName, int64_t duration) {
123 #ifdef SKIA_OHOS
124     if (!isOpenPerf() || nodeName.empty()) {
125         return;
126     }
127     COUNTER_TYPE cType = static_cast<COUNTER_TYPE>(getSplitRange(duration));
128     if (cType <= COUNTER_INVALID_TYPE) {
129         return;
130     }
131     std::lock_guard<std::mutex> guard(mtx);
132     if (fStatsBlur.find(nodeName) == fStatsBlur.end()) {
133         fStatsBlur[nodeName] = std::vector<uint16_t>(COUNTER_SIZE, 0);
134     }
135     fStatsBlur[nodeName][cType]++;
136 #endif
137 }
138 
recordTextureCache(const std::string & nodeName)139 void GrPerfMonitorReporter::recordTextureCache(const std::string& nodeName) {
140 #ifdef SKIA_OHOS
141     if (!isOpenPerf() || nodeName.empty()) {
142         return;
143     }
144     std::lock_guard<std::mutex> guard(mtx);
145     if (fStatsCache.size() >= CACHE_SIZE) {
146         fStatsCache.erase(fStatsCache.begin());
147     }
148     fStatsCache.insert(nodeName);
149 #endif
150 }
151 
getCurrentTime()152 int64_t GrPerfMonitorReporter::getCurrentTime() {
153 #ifdef SKIA_OHOS
154     if (!isOpenPerf()) {
155         return 0;
156     }
157     timespec t = {};
158     clock_gettime(CLOCK_MONOTONIC, &t);
159     return int64_t(t.tv_sec) * 1000000000LL + t.tv_nsec; // 1000000000ns == 1s
160 #else
161     return 0;
162 #endif
163 }
164 
getSplitRange(int64_t duration)165 int16_t GrPerfMonitorReporter::getSplitRange(int64_t duration) {
166     if (duration < COUNTER_MS_FIRST_TYPE) {
167         return COUNTER_INVALID_TYPE;
168     }
169     if (duration < COUNTER_MS_SECOND_TYPE) {
170         return COUNTER_FIRST_TYPE;
171     }
172     if (duration < COUNTER_MS_THIRD_TYPE) {
173         return COUNTER_SECOND_TYPE;
174     }
175     if (duration < COUNTER_MS_FORTH_TYPE) {
176         return COUNTER_THIRD_TYPE;
177     }
178     return COUNTER_FORTH_TYPE;
179 }
180 
isOpenPerf()181 bool GrPerfMonitorReporter::isOpenPerf() {
182 #ifdef NOT_BUILD_FOR_OHOS_SDK
183     static bool isOpenPerf =
184         OHOS::system::GetParameter("const.logsystem.versiontype", "beta") == "beta" &&
185         OHOS::system::GetParameter("const.product.devicetype", "phone") == "phone";
186     return isOpenPerf;
187 #else
188     return false;
189 #endif
190 }
191 
isClearCacheLastTime(const std::string & nodeName)192 bool GrPerfMonitorReporter::isClearCacheLastTime(const std::string& nodeName) {
193 #ifdef SKIA_OHOS
194     std::lock_guard<std::mutex> guard(mtx);
195     return fStatsCache.find(nodeName) != fStatsCache.end();
196 #else
197     return false;
198 #endif
199 }
200