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