TraceValue had an unowned pointer to a const char (string). But there was no guarantee that the string lived long enough for TraceValue's use. Switch to an owned string type. Use std::string, even though SkString has a little bit of use in this class. Using SkString would've resulted in a map that had both types of string, and converging on the standard seems better anyway. Update other unowned pointers which often reference TraceValues to owned strings as well. Use std::optional instead of a possible null. Leave some raw strings alone: - The ResourceMap is only used with static strings - The virtual method overrides require const char*, but they quickly convert to owned strings - The categoryKey constructor still takes a const char*, but also converts to an owned string. Based on the current use, it could have remained a raw string, but it is later used with now-owned strings. - `entry` is a short lived const char*, which obviously is safe. Bug: 305919946 Test: adb shell dumpsys gfxinfo Change-Id: I30b990771cc7f3ccf2769efc5aafb68c957eca9f
92 lines
3.1 KiB
C++
92 lines
3.1 KiB
C++
/*
|
|
* Copyright (C) 2018 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <SkTraceMemoryDump.h>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <utils/String8.h>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
namespace android {
|
|
namespace uirenderer {
|
|
namespace skiapipeline {
|
|
|
|
typedef std::pair<const char*, const char*> ResourcePair;
|
|
|
|
class SkiaMemoryTracer : public SkTraceMemoryDump {
|
|
public:
|
|
SkiaMemoryTracer(std::vector<ResourcePair> resourceMap, bool itemizeType);
|
|
SkiaMemoryTracer(const char* categoryKey, bool itemizeType);
|
|
~SkiaMemoryTracer() override {}
|
|
|
|
bool hasOutput();
|
|
void logOutput(String8& log);
|
|
void logTotals(String8& log);
|
|
size_t total();
|
|
|
|
void dumpNumericValue(const char* dumpName, const char* valueName, const char* units,
|
|
uint64_t value) override;
|
|
|
|
void dumpStringValue(const char* dumpName, const char* valueName, const char* value) override {
|
|
// for convenience we just store this in the same format as numerical values
|
|
dumpNumericValue(dumpName, valueName, value, 0);
|
|
}
|
|
|
|
LevelOfDetail getRequestedDetails() const override {
|
|
return SkTraceMemoryDump::kLight_LevelOfDetail;
|
|
}
|
|
|
|
bool shouldDumpWrappedObjects() const override { return true; }
|
|
void setMemoryBacking(const char*, const char*, const char*) override {}
|
|
void setDiscardableMemoryBacking(const char*, const SkDiscardableMemory&) override {}
|
|
|
|
private:
|
|
struct TraceValue {
|
|
TraceValue(const char* units, uint64_t value) : units(units), value(value), count(1) {}
|
|
TraceValue(const TraceValue& v) : units(v.units), value(v.value), count(v.count) {}
|
|
|
|
std::string units;
|
|
float value;
|
|
int count;
|
|
};
|
|
|
|
std::optional<std::string> mapName(const std::string& resourceName);
|
|
void processElement();
|
|
TraceValue convertUnits(const TraceValue& value);
|
|
|
|
const std::vector<ResourcePair> mResourceMap;
|
|
std::optional<std::string> mCategoryKey;
|
|
const bool mItemizeType;
|
|
|
|
// variables storing the size of all elements being dumped
|
|
TraceValue mTotalSize;
|
|
TraceValue mPurgeableSize;
|
|
|
|
// variables storing information on the current node being dumped
|
|
std::string mCurrentElement;
|
|
std::unordered_map<std::string, TraceValue> mCurrentValues;
|
|
|
|
// variable that stores the final format of the data after the individual elements are processed
|
|
std::unordered_map<std::string, std::unordered_map<std::string, TraceValue>> mResults;
|
|
};
|
|
|
|
} /* namespace skiapipeline */
|
|
} /* namespace uirenderer */
|
|
} /* namespace android */
|