blob: 5762dabc550aa94ef4964ddb63aa8ad7a7fabd08 [file] [log] [blame]
Ady Abrahama3b08ef2019-07-15 18:43:10 -07001/*
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 Hoisie53e19a72020-05-01 17:19:57 -040017#include <android-base/properties.h>
Ady Abrahama3b08ef2019-07-15 18:43:10 -070018#include <android-base/stringprintf.h>
Ady Abrahama3b08ef2019-07-15 18:43:10 -070019#include <gui/DebugEGLImageTracker.h>
20
21#include <cinttypes>
22#include <unordered_map>
23
Michael Hoisie53e19a72020-05-01 17:19:57 -040024using android::base::GetBoolProperty;
Ady Abrahama3b08ef2019-07-15 18:43:10 -070025using android::base::StringAppendF;
26
27std::mutex DebugEGLImageTracker::mInstanceLock;
28std::atomic<DebugEGLImageTracker *> DebugEGLImageTracker::mInstance;
29
30class DebugEGLImageTrackerNoOp : public DebugEGLImageTracker {
31public:
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
40class DebugEGLImageTrackerImpl : public DebugEGLImageTracker {
41public:
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
49private:
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
58DebugEGLImageTracker *DebugEGLImageTracker::getInstance() {
59 std::lock_guard lock(mInstanceLock);
60 if (mInstance == nullptr) {
Michael Hoisie53e19a72020-05-01 17:19:57 -040061 const bool enabled = GetBoolProperty("debug.sf.enable_egl_image_tracker", false);
Ady Abrahama3b08ef2019-07-15 18:43:10 -070062 if (enabled) {
63 mInstance = new DebugEGLImageTrackerImpl();
64 } else {
65 mInstance = new DebugEGLImageTrackerNoOp();
66 }
67 }
68
69 return mInstance;
70}
71
72void DebugEGLImageTrackerImpl::create(const char *from) {
73 std::lock_guard lock(mLock);
74 mCreateTracker[from]++;
75 mTotalCreated++;
76}
77
78void DebugEGLImageTrackerImpl::destroy(const char *from) {
79 std::lock_guard lock(mLock);
80 mDestroyTracker[from]++;
81 mTotalDestroyed++;
82}
83
84void 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}