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