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 #ifndef RS_SKIA_MEMORY_TRACER_H
16 #define RS_SKIA_MEMORY_TRACER_H
17 #include <unordered_map>
18 #include <vector>
19 
20 #include "include/core/SkString.h"
21 #include "include/core/SkTraceMemoryDump.h"
22 #include "memory/rs_dfx_string.h"
23 
24 namespace OHOS::Rosen {
25 typedef std::pair<const char*, const char*> ResourcePair;
26 
27 class SkiaMemoryTracer : public SkTraceMemoryDump {
28 public:
29     SkiaMemoryTracer(const char* categoryKey, bool itemizeType);
30     SkiaMemoryTracer(const std::vector<ResourcePair>& resourceMap, bool itemizeType);
~SkiaMemoryTracer()31     ~SkiaMemoryTracer() override {}
32 
33     void LogOutput(DfxString& log);
34     void LogTotals(DfxString& log);
35 
36     float GetGpuMemorySizeInMB();
37     void dumpNumericValue(const char* dumpName, const char* valueName, const char* units, uint64_t value) override;
38 
dumpStringValue(const char * dumpName,const char * valueName,const char * value)39     void dumpStringValue(const char* dumpName, const char* valueName, const char* value) override
40     {
41         dumpNumericValue(dumpName, valueName, value, 0);
42     }
43 
44     float GetGLMemorySize();
45 
setMemoryBacking(const char *,const char *,const char *)46     void setMemoryBacking(const char*, const char*, const char*) override {}
setDiscardableMemoryBacking(const char *,const SkDiscardableMemory &)47     void setDiscardableMemoryBacking(const char*, const SkDiscardableMemory&) override {}
48 
getRequestedDetails()49     LevelOfDetail getRequestedDetails() const override
50     {
51         return SkTraceMemoryDump::kLight_LevelOfDetail;
52     }
53 
shouldDumpWrappedObjects()54     bool shouldDumpWrappedObjects() const override
55     {
56         return true;
57     }
58 private:
59     struct TraceValue {
TraceValueTraceValue60         TraceValue(const char* units, uint64_t value) : units(units), value(value), count(1) {}
TraceValueTraceValue61         TraceValue(const TraceValue& v) : units(v.units), value(v.value), count(v.count) {}
62         TraceValue& operator=(const TraceValue& v)
63         {
64             units = v.units;
65             value = v.value;
66             count = v.count;
67             return *this;
68         }
69         SkString units;
70         float value;
71         int count;
72     };
73 
74     const char* MapName(const char* resourceName);
75     void ProcessElement();
76     TraceValue ConvertUnits(const TraceValue& value);
77     float ConvertToMB(const TraceValue& value);
78 
79     const std::vector<ResourcePair> resourceMap_;
80     const char* categoryKey_ = nullptr;
81     const bool itemizeType_;
82 
83     TraceValue totalSize_;
84     TraceValue purgeableSize_;
85     std::string currentElement_;
86     std::unordered_map<std::string, TraceValue> currentValues_;
87     std::unordered_map<std::string, std::unordered_map<std::string, TraceValue>> results_;
88 };
89 
90 } // namespace OHOS::Rosen
91 #endif
92