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 <SkString.h>
20 #include <SkTraceMemoryDump.h>
21 #include <utils/String8.h>
22 #include <unordered_map>
23 #include <vector>
24 
25 namespace android {
26 namespace uirenderer {
27 namespace skiapipeline {
28 
29 typedef std::pair<const char*, const char*> ResourcePair;
30 
31 class SkiaMemoryTracer : public SkTraceMemoryDump {
32 public:
33     SkiaMemoryTracer(std::vector<ResourcePair> resourceMap, bool itemizeType);
34     SkiaMemoryTracer(const char* categoryKey, bool itemizeType);
~SkiaMemoryTracer()35     ~SkiaMemoryTracer() override {}
36 
37     void logOutput(String8& log);
38     void logTotals(String8& log);
39 
40     void dumpNumericValue(const char* dumpName, const char* valueName, const char* units,
41                           uint64_t value) override;
42 
dumpStringValue(const char * dumpName,const char * valueName,const char * value)43     void dumpStringValue(const char* dumpName, const char* valueName, const char* value) override {
44         // for convenience we just store this in the same format as numerical values
45         dumpNumericValue(dumpName, valueName, value, 0);
46     }
47 
getRequestedDetails()48     LevelOfDetail getRequestedDetails() const override {
49         return SkTraceMemoryDump::kLight_LevelOfDetail;
50     }
51 
shouldDumpWrappedObjects()52     bool shouldDumpWrappedObjects() const override { return true; }
setMemoryBacking(const char *,const char *,const char *)53     void setMemoryBacking(const char*, const char*, const char*) override {}
setDiscardableMemoryBacking(const char *,const SkDiscardableMemory &)54     void setDiscardableMemoryBacking(const char*, const SkDiscardableMemory&) override {}
55 
56 private:
57     struct TraceValue {
TraceValueTraceValue58         TraceValue(const char* units, uint64_t value) : units(units), value(value), count(1) {}
TraceValueTraceValue59         TraceValue(const TraceValue& v) : units(v.units), value(v.value), count(v.count) {}
60 
61         const char* units;
62         float value;
63         int count;
64     };
65 
66     const char* mapName(const char* resourceName);
67     void processElement();
68     TraceValue convertUnits(const TraceValue& value);
69 
70     const std::vector<ResourcePair> mResourceMap;
71     const char* mCategoryKey = nullptr;
72     const bool mItemizeType;
73 
74     // variables storing the size of all elements being dumped
75     TraceValue mTotalSize;
76     TraceValue mPurgeableSize;
77 
78     // variables storing information on the current node being dumped
79     std::string mCurrentElement;
80     std::unordered_map<const char*, TraceValue> mCurrentValues;
81 
82     // variable that stores the final format of the data after the individual elements are processed
83     std::unordered_map<std::string, std::unordered_map<const char*, TraceValue>> mResults;
84 };
85 
86 } /* namespace skiapipeline */
87 } /* namespace uirenderer */
88 } /* namespace android */