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
17 #include <android-base/properties.h>
18 #include <android-base/stringprintf.h>
19 #include <gui/DebugEGLImageTracker.h>
20
21 #include <cinttypes>
22 #include <unordered_map>
23
24 using android::base::GetBoolProperty;
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;
create(const char *)34 void create(const char * /*from*/) override {}
destroy(const char *)35 void destroy(const char * /*from*/) override {}
36
dump(std::string &)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
getInstance()58 DebugEGLImageTracker *DebugEGLImageTracker::getInstance() {
59 std::lock_guard lock(mInstanceLock);
60 if (mInstance == nullptr) {
61 const bool enabled = GetBoolProperty("debug.sf.enable_egl_image_tracker", false);
62 if (enabled) {
63 mInstance = new DebugEGLImageTrackerImpl();
64 } else {
65 mInstance = new DebugEGLImageTrackerNoOp();
66 }
67 }
68
69 return mInstance;
70 }
71
create(const char * from)72 void DebugEGLImageTrackerImpl::create(const char *from) {
73 std::lock_guard lock(mLock);
74 mCreateTracker[from]++;
75 mTotalCreated++;
76 }
77
destroy(const char * from)78 void DebugEGLImageTrackerImpl::destroy(const char *from) {
79 std::lock_guard lock(mLock);
80 mDestroyTracker[from]++;
81 mTotalDestroyed++;
82 }
83
dump(std::string & result)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 }
97