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 16 #include <unordered_map> 17 #include <vector> 18 19 #include "include/core/SkString.h" 20 #include "include/core/SkTraceMemoryDump.h" 21 #include "memory/rs_dfx_string.h" 22 23 namespace OHOS::Rosen { 24 typedef std::pair<const char*, const char*> ResourcePair; 25 26 class SkiaMemoryTracer : public SkTraceMemoryDump { 27 public: 28 SkiaMemoryTracer(const char* categoryKey, bool itemizeType); 29 SkiaMemoryTracer(std::vector<ResourcePair> resourceMap, bool itemizeType); ~SkiaMemoryTracer()30 ~SkiaMemoryTracer() override {} 31 32 void LogOutput(DfxString& log); 33 void LogTotals(DfxString& log); 34 35 void dumpNumericValue(const char* dumpName, const char* valueName, const char* units, uint64_t value) override; 36 dumpStringValue(const char * dumpName,const char * valueName,const char * value)37 void dumpStringValue(const char* dumpName, const char* valueName, const char* value) override 38 { 39 dumpNumericValue(dumpName, valueName, value, 0); 40 } 41 42 float GetGLMemorySize(); 43 setMemoryBacking(const char *,const char *,const char *)44 void setMemoryBacking(const char*, const char*, const char*) override {} setDiscardableMemoryBacking(const char *,const SkDiscardableMemory &)45 void setDiscardableMemoryBacking(const char*, const SkDiscardableMemory&) override {} 46 getRequestedDetails()47 LevelOfDetail getRequestedDetails() const override 48 { 49 return SkTraceMemoryDump::kLight_LevelOfDetail; 50 } 51 shouldDumpWrappedObjects()52 bool shouldDumpWrappedObjects() const override 53 { 54 return true; 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 TraceValue& operator=(const TraceValue& v) 61 { 62 units = v.units; 63 value = v.value; 64 count = v.count; 65 return *this; 66 } 67 const char* units; 68 float value; 69 int count; 70 }; 71 72 const char* MapName(const char* resourceName); 73 void ProcessElement(); 74 TraceValue ConvertUnits(const TraceValue& value); 75 76 const std::vector<ResourcePair> resourceMap_; 77 const char* categoryKey_ = nullptr; 78 const bool itemizeType_; 79 80 TraceValue totalSize_; 81 TraceValue purgeableSize_; 82 std::string currentElement_; 83 std::unordered_map<std::string, TraceValue> currentValues_; 84 std::unordered_map<std::string, std::unordered_map<std::string, TraceValue>> results_; 85 }; 86 87 } // namespace OHOS::Rosen 88