blob: a9a7af8f22f3a9b0b24b0f01bf1ca6d50dbb8615 [file] [log] [blame]
John Reck38e0c322015-11-10 12:19:17 -08001/*
2 * Copyright (C) 2016 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
John Reck1bcacfd2017-11-03 10:12:19 -070017#include "utils/StringUtils.h"
John Reck38e0c322015-11-10 12:19:17 -080018
John Reck38e0c322015-11-10 12:19:17 -080019#include <GpuMemoryTracker.h>
John Reck1bcacfd2017-11-03 10:12:19 -070020#include <cutils/compiler.h>
John Reck38e0c322015-11-10 12:19:17 -080021#include <utils/Trace.h>
22#include <array>
23#include <sstream>
24#include <unordered_set>
25#include <vector>
26
27namespace android {
28namespace uirenderer {
29
30pthread_t gGpuThread = 0;
31
32#define NUM_TYPES static_cast<int>(GpuObjectType::TypeCount)
33
34const char* TYPE_NAMES[] = {
John Reck1bcacfd2017-11-03 10:12:19 -070035 "Texture", "OffscreenBuffer", "Layer",
John Reck38e0c322015-11-10 12:19:17 -080036};
37
38struct TypeStats {
39 int totalSize = 0;
40 int count = 0;
41};
42
43static std::array<TypeStats, NUM_TYPES> gObjectStats;
44static std::unordered_set<GpuMemoryTracker*> gObjectSet;
45
46void GpuMemoryTracker::notifySizeChanged(int newSize) {
47 int delta = newSize - mSize;
48 mSize = newSize;
49 gObjectStats[static_cast<int>(mType)].totalSize += delta;
50}
51
52void GpuMemoryTracker::startTrackingObject() {
53 auto result = gObjectSet.insert(this);
54 LOG_ALWAYS_FATAL_IF(!result.second,
John Reck1bcacfd2017-11-03 10:12:19 -070055 "startTrackingObject() on %p failed, already being tracked!", this);
John Reck38e0c322015-11-10 12:19:17 -080056 gObjectStats[static_cast<int>(mType)].count++;
57}
58
59void GpuMemoryTracker::stopTrackingObject() {
60 size_t removed = gObjectSet.erase(this);
John Reck1bcacfd2017-11-03 10:12:19 -070061 LOG_ALWAYS_FATAL_IF(removed != 1, "stopTrackingObject removed %zd, is %p not being tracked?",
62 removed, this);
John Reck38e0c322015-11-10 12:19:17 -080063 gObjectStats[static_cast<int>(mType)].count--;
64}
65
Greg Daniel45ec62b2017-01-04 14:27:00 -050066void GpuMemoryTracker::onGpuContextCreated() {
John Reck1bcacfd2017-11-03 10:12:19 -070067 LOG_ALWAYS_FATAL_IF(gGpuThread != 0,
68 "We already have a gpu thread? "
69 "current = %lu, gpu thread = %lu",
70 pthread_self(), gGpuThread);
John Reck38e0c322015-11-10 12:19:17 -080071 gGpuThread = pthread_self();
72}
73
Greg Daniel45ec62b2017-01-04 14:27:00 -050074void GpuMemoryTracker::onGpuContextDestroyed() {
John Reck38e0c322015-11-10 12:19:17 -080075 gGpuThread = 0;
76 if (CC_UNLIKELY(gObjectSet.size() > 0)) {
77 std::stringstream os;
78 dump(os);
79 ALOGE("%s", os.str().c_str());
80 LOG_ALWAYS_FATAL("Leaked %zd GPU objects!", gObjectSet.size());
81 }
82}
83
84void GpuMemoryTracker::dump() {
85 std::stringstream strout;
86 dump(strout);
87 ALOGD("%s", strout.str().c_str());
88}
89
90void GpuMemoryTracker::dump(std::ostream& stream) {
91 for (int type = 0; type < NUM_TYPES; type++) {
92 const TypeStats& stats = gObjectStats[type];
93 stream << TYPE_NAMES[type];
94 stream << " is using " << SizePrinter{stats.totalSize};
95 stream << ", count = " << stats.count;
96 stream << std::endl;
97 }
98}
99
100int GpuMemoryTracker::getInstanceCount(GpuObjectType type) {
101 return gObjectStats[static_cast<int>(type)].count;
102}
103
104int GpuMemoryTracker::getTotalSize(GpuObjectType type) {
105 return gObjectStats[static_cast<int>(type)].totalSize;
106}
107
108void GpuMemoryTracker::onFrameCompleted() {
109 if (ATRACE_ENABLED()) {
110 char buf[128];
111 for (int type = 0; type < NUM_TYPES; type++) {
112 snprintf(buf, 128, "hwui_%s", TYPE_NAMES[type]);
113 const TypeStats& stats = gObjectStats[type];
114 ATRACE_INT(buf, stats.totalSize);
115 snprintf(buf, 128, "hwui_%s_count", TYPE_NAMES[type]);
116 ATRACE_INT(buf, stats.count);
117 }
118 }
John Reck38e0c322015-11-10 12:19:17 -0800119}
120
John Reck1bcacfd2017-11-03 10:12:19 -0700121} // namespace uirenderer
122} // namespace android;