DebugEGLImageTracker.cpp 3.2 KB

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