1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <SkTraceMemoryDump.h>
20 #include <optional>
21 #include <string>
22 #include <utils/String8.h>
23 #include <unordered_map>
24 #include <vector>
25 
26 namespace android {
27 namespace uirenderer {
28 namespace skiapipeline {
29 
30 typedef std::pair<const char*, const char*> ResourcePair;
31 
32 class SkiaMemoryTracer : public SkTraceMemoryDump {
33 public:
34     SkiaMemoryTracer(std::vector<ResourcePair> resourceMap, bool itemizeType);
35     SkiaMemoryTracer(const char* categoryKey, bool itemizeType);
~SkiaMemoryTracer()36     ~SkiaMemoryTracer() override {}
37 
38     bool hasOutput();
39     void logOutput(String8& log);
40     void logTotals(String8& log);
41     size_t total();
42 
43     void dumpNumericValue(const char* dumpName, const char* valueName, const char* units,
44                           uint64_t value) override;
45 
dumpStringValue(const char * dumpName,const char * valueName,const char * value)46     void dumpStringValue(const char* dumpName, const char* valueName, const char* value) override {
47         // for convenience we just store this in the same format as numerical values
48         dumpNumericValue(dumpName, valueName, value, 0);
49     }
50 
getRequestedDetails()51     LevelOfDetail getRequestedDetails() const override {
52         return SkTraceMemoryDump::kLight_LevelOfDetail;
53     }
54 
shouldDumpWrappedObjects()55     bool shouldDumpWrappedObjects() const override { return true; }
setMemoryBacking(const char *,const char *,const char *)56     void setMemoryBacking(const char*, const char*, const char*) override {}
setDiscardableMemoryBacking(const char *,const SkDiscardableMemory &)57     void setDiscardableMemoryBacking(const char*, const SkDiscardableMemory&) override {}
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 
64         std::string units;
65         float value;
66         int count;
67     };
68 
69     std::optional<std::string> mapName(const std::string& resourceName);
70     void processElement();
71     TraceValue convertUnits(const TraceValue& value);
72 
73     const std::vector<ResourcePair> mResourceMap;
74     std::optional<std::string> mCategoryKey;
75     const bool mItemizeType;
76 
77     // variables storing the size of all elements being dumped
78     TraceValue mTotalSize;
79     TraceValue mPurgeableSize;
80 
81     // variables storing information on the current node being dumped
82     std::string mCurrentElement;
83     std::unordered_map<std::string, TraceValue> mCurrentValues;
84 
85     // variable that stores the final format of the data after the individual elements are processed
86     std::unordered_map<std::string, std::unordered_map<std::string, TraceValue>> mResults;
87 };
88 
89 } /* namespace skiapipeline */
90 } /* namespace uirenderer */
91 } /* namespace android */
92