John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 1 | /* |
| 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 Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 18 | #include <pthread.h> |
| 19 | #include <ostream> |
| 20 | |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 21 | #include <log/log.h> |
| 22 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 23 | namespace android { |
| 24 | namespace uirenderer { |
| 25 | |
| 26 | extern pthread_t gGpuThread; |
| 27 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 28 | #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 Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 33 | |
| 34 | enum class GpuObjectType { |
| 35 | Texture = 0, |
| 36 | OffscreenBuffer, |
| 37 | Layer, |
| 38 | |
| 39 | TypeCount, |
| 40 | }; |
| 41 | |
| 42 | class GpuMemoryTracker { |
| 43 | public: |
| 44 | GpuObjectType objectType() { return mType; } |
| 45 | int objectSize() { return mSize; } |
| 46 | |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 47 | static void onGpuContextCreated(); |
| 48 | static void onGpuContextDestroyed(); |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 49 | 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 | |
| 55 | protected: |
Chih-Hung Hsieh | a619ec7 | 2016-08-29 14:52:43 -0700 | [diff] [blame] | 56 | explicit GpuMemoryTracker(GpuObjectType type) : mType(type) { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 57 | ASSERT_GPU_THREAD(); |
| 58 | startTrackingObject(); |
| 59 | } |
| 60 | |
| 61 | ~GpuMemoryTracker() { |
| 62 | notifySizeChanged(0); |
| 63 | stopTrackingObject(); |
| 64 | } |
| 65 | |
| 66 | void notifySizeChanged(int newSize); |
| 67 | |
| 68 | private: |
| 69 | void startTrackingObject(); |
| 70 | void stopTrackingObject(); |
| 71 | |
| 72 | int mSize = 0; |
| 73 | GpuObjectType mType; |
| 74 | }; |
| 75 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 76 | } // namespace uirenderer |
| 77 | } // namespace android; |