blob: de3ca99ef14b40e7a689038233847b6803365716 [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#pragma once
17
John Reck38e0c322015-11-10 12:19:17 -080018#include <pthread.h>
19#include <ostream>
20
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070021#include <log/log.h>
22
John Reck38e0c322015-11-10 12:19:17 -080023namespace android {
24namespace uirenderer {
25
26extern pthread_t gGpuThread;
27
John Reck1bcacfd2017-11-03 10:12:19 -070028#define ASSERT_GPU_THREAD() \
29 LOG_ALWAYS_FATAL_IF(!pthread_equal(gGpuThread, pthread_self()), \
30 "Error, %p of type %d (size=%d) used on wrong thread! cur thread %lu " \
31 "!= gpu thread %lu", \
32 this, static_cast<int>(mType), mSize, pthread_self(), gGpuThread)
John Reck38e0c322015-11-10 12:19:17 -080033
34enum class GpuObjectType {
35 Texture = 0,
36 OffscreenBuffer,
37 Layer,
38
39 TypeCount,
40};
41
42class GpuMemoryTracker {
43public:
44 GpuObjectType objectType() { return mType; }
45 int objectSize() { return mSize; }
46
Greg Daniel45ec62b2017-01-04 14:27:00 -050047 static void onGpuContextCreated();
48 static void onGpuContextDestroyed();
John Reck38e0c322015-11-10 12:19:17 -080049 static void dump();
50 static void dump(std::ostream& stream);
51 static int getInstanceCount(GpuObjectType type);
52 static int getTotalSize(GpuObjectType type);
53 static void onFrameCompleted();
54
55protected:
Chih-Hung Hsieha619ec72016-08-29 14:52:43 -070056 explicit GpuMemoryTracker(GpuObjectType type) : mType(type) {
John Reck38e0c322015-11-10 12:19:17 -080057 ASSERT_GPU_THREAD();
58 startTrackingObject();
59 }
60
61 ~GpuMemoryTracker() {
62 notifySizeChanged(0);
63 stopTrackingObject();
64 }
65
66 void notifySizeChanged(int newSize);
67
68private:
69 void startTrackingObject();
70 void stopTrackingObject();
71
72 int mSize = 0;
73 GpuObjectType mType;
74};
75
John Reck1bcacfd2017-11-03 10:12:19 -070076} // namespace uirenderer
77} // namespace android;