1 /*
2  * Copyright (C) 2020 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 <GrDirectContext.h>
20 #include <SkString.h>
21 #include <SkTraceMemoryDump.h>
22 
23 #include <string>
24 #include <unordered_map>
25 #include <utility>
26 
27 namespace android {
28 namespace uirenderer {
29 namespace skiapipeline {
30 
31 class ATraceMemoryDump : public SkTraceMemoryDump {
32 public:
33     ATraceMemoryDump();
~ATraceMemoryDump()34     ~ATraceMemoryDump() override {}
35 
36     void dumpNumericValue(const char* dumpName, const char* valueName, const char* units,
37                           uint64_t value) override;
38 
39     void dumpStringValue(const char* dumpName, const char* valueName, const char* value) override;
40 
getRequestedDetails()41     LevelOfDetail getRequestedDetails() const override {
42         return SkTraceMemoryDump::kLight_LevelOfDetail;
43     }
44 
shouldDumpWrappedObjects()45     bool shouldDumpWrappedObjects() const override { return false; }
46 
47     void setMemoryBacking(const char* dumpName, const char* backingType,
48                           const char* backingObjectId) override;
49 
setDiscardableMemoryBacking(const char *,const SkDiscardableMemory &)50     void setDiscardableMemoryBacking(const char*, const SkDiscardableMemory&) override {}
51 
52     void startFrame();
53 
54     void logTraces(bool gpuMemoryIsAlreadyInDump, GrDirectContext* grContext);
55 
56 private:
57     std::string mLastDumpName;
58 
59     uint64_t mLastDumpValue;
60 
61     uint64_t mLastPurgeableDumpValue;
62 
63     std::string mCategory;
64 
65     struct TraceValue {
66         uint64_t memory;
67         uint64_t purgeableMemory;
68     };
69 
70     // keys are define in sResourceMap
71     std::unordered_map<std::string, TraceValue> mCurrentValues;
72 
73     void recordAndResetCountersIfNeeded(const char* dumpName);
74 
75     void resetCurrentCounter(const char* dumpName);
76 };
77 
78 } /* namespace skiapipeline */
79 } /* namespace uirenderer */
80 } /* namespace android */