| Ady Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2019 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 |  | 
| Michael Hoisie | 53e19a7 | 2020-05-01 17:19:57 -0400 | [diff] [blame] | 17 | #include <android-base/properties.h> | 
| Ady Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame] | 18 | #include <android-base/stringprintf.h> | 
| Ady Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame] | 19 | #include <gui/DebugEGLImageTracker.h> | 
|  | 20 |  | 
|  | 21 | #include <cinttypes> | 
|  | 22 | #include <unordered_map> | 
|  | 23 |  | 
| Michael Hoisie | 53e19a7 | 2020-05-01 17:19:57 -0400 | [diff] [blame] | 24 | using android::base::GetBoolProperty; | 
| Ady Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame] | 25 | using android::base::StringAppendF; | 
|  | 26 |  | 
|  | 27 | std::mutex DebugEGLImageTracker::mInstanceLock; | 
|  | 28 | std::atomic<DebugEGLImageTracker *> DebugEGLImageTracker::mInstance; | 
|  | 29 |  | 
|  | 30 | class DebugEGLImageTrackerNoOp : public DebugEGLImageTracker { | 
|  | 31 | public: | 
|  | 32 | DebugEGLImageTrackerNoOp() = default; | 
|  | 33 | ~DebugEGLImageTrackerNoOp() override = default; | 
|  | 34 | void create(const char * /*from*/) override {} | 
|  | 35 | void destroy(const char * /*from*/) override {} | 
|  | 36 |  | 
|  | 37 | void dump(std::string & /*result*/) override {} | 
|  | 38 | }; | 
|  | 39 |  | 
|  | 40 | class DebugEGLImageTrackerImpl : public DebugEGLImageTracker { | 
|  | 41 | public: | 
|  | 42 | DebugEGLImageTrackerImpl() = default; | 
|  | 43 | ~DebugEGLImageTrackerImpl() override = default; | 
|  | 44 | void create(const char * /*from*/) override; | 
|  | 45 | void destroy(const char * /*from*/) override; | 
|  | 46 |  | 
|  | 47 | void dump(std::string & /*result*/) override; | 
|  | 48 |  | 
|  | 49 | private: | 
|  | 50 | std::mutex mLock; | 
|  | 51 | std::unordered_map<std::string, int64_t> mCreateTracker; | 
|  | 52 | std::unordered_map<std::string, int64_t> mDestroyTracker; | 
|  | 53 |  | 
|  | 54 | int64_t mTotalCreated = 0; | 
|  | 55 | int64_t mTotalDestroyed = 0; | 
|  | 56 | }; | 
|  | 57 |  | 
|  | 58 | DebugEGLImageTracker *DebugEGLImageTracker::getInstance() { | 
|  | 59 | std::lock_guard lock(mInstanceLock); | 
|  | 60 | if (mInstance == nullptr) { | 
| Michael Hoisie | 53e19a7 | 2020-05-01 17:19:57 -0400 | [diff] [blame] | 61 | const bool enabled = GetBoolProperty("debug.sf.enable_egl_image_tracker", false); | 
| Ady Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame] | 62 | if (enabled) { | 
|  | 63 | mInstance = new DebugEGLImageTrackerImpl(); | 
|  | 64 | } else { | 
|  | 65 | mInstance = new DebugEGLImageTrackerNoOp(); | 
|  | 66 | } | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | return mInstance; | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | void DebugEGLImageTrackerImpl::create(const char *from) { | 
|  | 73 | std::lock_guard lock(mLock); | 
|  | 74 | mCreateTracker[from]++; | 
|  | 75 | mTotalCreated++; | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | void DebugEGLImageTrackerImpl::destroy(const char *from) { | 
|  | 79 | std::lock_guard lock(mLock); | 
|  | 80 | mDestroyTracker[from]++; | 
|  | 81 | mTotalDestroyed++; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | void DebugEGLImageTrackerImpl::dump(std::string &result) { | 
|  | 85 | std::lock_guard lock(mLock); | 
|  | 86 | StringAppendF(&result, "Live EGL Image objects: %" PRIi64 "\n", | 
|  | 87 | mTotalCreated - mTotalDestroyed); | 
|  | 88 | StringAppendF(&result, "Total EGL Image created: %" PRIi64 "\n", mTotalCreated); | 
|  | 89 | for (const auto &[from, count] : mCreateTracker) { | 
|  | 90 | StringAppendF(&result, "\t%s: %" PRIi64 "\n", from.c_str(), count); | 
|  | 91 | } | 
|  | 92 | StringAppendF(&result, "Total EGL Image destroyed: %" PRIi64 "\n", mTotalDestroyed); | 
|  | 93 | for (const auto &[from, count] : mDestroyTracker) { | 
|  | 94 | StringAppendF(&result, "\t%s: %" PRIi64 "\n", from.c_str(), count); | 
|  | 95 | } | 
|  | 96 | } |