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 bool hasOutput(); 38 void logOutput(String8& log); 39 void logTotals(String8& log); 40 size_t total(); 41 42 void dumpNumericValue(const char* dumpName, const char* valueName, const char* units, 43 uint64_t value) override; 44 dumpStringValue(const char * dumpName,const char * valueName,const char * value)45 void dumpStringValue(const char* dumpName, const char* valueName, const char* value) override { 46 // for convenience we just store this in the same format as numerical values 47 dumpNumericValue(dumpName, valueName, value, 0); 48 } 49 getRequestedDetails()50 LevelOfDetail getRequestedDetails() const override { 51 return SkTraceMemoryDump::kLight_LevelOfDetail; 52 } 53 shouldDumpWrappedObjects()54 bool shouldDumpWrappedObjects() const override { return true; } setMemoryBacking(const char *,const char *,const char *)55 void setMemoryBacking(const char*, const char*, const char*) override {} setDiscardableMemoryBacking(const char *,const SkDiscardableMemory &)56 void setDiscardableMemoryBacking(const char*, const SkDiscardableMemory&) override {} 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 63 const char* units; 64 float value; 65 int count; 66 }; 67 68 const char* mapName(const char* resourceName); 69 void processElement(); 70 TraceValue convertUnits(const TraceValue& value); 71 72 const std::vector<ResourcePair> mResourceMap; 73 const char* mCategoryKey = nullptr; 74 const bool mItemizeType; 75 76 // variables storing the size of all elements being dumped 77 TraceValue mTotalSize; 78 TraceValue mPurgeableSize; 79 80 // variables storing information on the current node being dumped 81 std::string mCurrentElement; 82 std::unordered_map<const char*, TraceValue> mCurrentValues; 83 84 // variable that stores the final format of the data after the individual elements are processed 85 std::unordered_map<std::string, std::unordered_map<const char*, TraceValue>> mResults; 86 }; 87 88 } /* namespace skiapipeline */ 89 } /* namespace uirenderer */ 90 } /* namespace android */