• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include "aemu/base/GLObjectCounter.h"
15 
16 #include <array>
17 #include <atomic>
18 #include <sstream>
19 
20 namespace android {
21 namespace base {
22 
23 enum class NamedObjectType : int {
24     NULLTYPE,
25     VERTEXBUFFER,
26     TEXTURE,
27     RENDERBUFFER,
28     FRAMEBUFFER,
29     SHADER_OR_PROGRAM,
30     SAMPLER,
31     QUERY,
32     VERTEX_ARRAY_OBJECT,
33     TRANSFORM_FEEDBACK,
34     NUM_OBJECT_TYPES  // Must be last
35 };
36 
toIndex(NamedObjectType type)37 static constexpr size_t toIndex(NamedObjectType type) {
38     return static_cast<size_t>(type);
39 }
40 
41 class GLObjectCounter::Impl {
42 public:
incCount(size_t type)43     void incCount(size_t type) {
44         if (type > toIndex(NamedObjectType::NULLTYPE) &&
45             type < toIndex(NamedObjectType::NUM_OBJECT_TYPES)) {
46             mCounter[type] += 1;
47         }
48     }
49 
decCount(size_t type)50     void decCount(size_t type) {
51         if (type > toIndex(NamedObjectType::NULLTYPE) &&
52             type < toIndex(NamedObjectType::NUM_OBJECT_TYPES)) {
53             mCounter[type] -= 1;
54         }
55     }
56 
getCounts()57     std::vector<size_t> getCounts() {
58         std::vector<size_t> v;
59         for (auto& it : mCounter) {
60             v.push_back(it.load());
61         }
62         return v;
63     }
64 
printUsage()65     std::string printUsage() {
66         std::stringstream ss;
67         ss << "VertexBuffer: "
68            << mCounter[toIndex(NamedObjectType::VERTEXBUFFER)].load();
69         ss << " Texture: "
70            << mCounter[toIndex(NamedObjectType::TEXTURE)].load();
71         ss << " RenderBuffer: "
72            << mCounter[toIndex(NamedObjectType::RENDERBUFFER)].load();
73         ss << " FrameBuffer: "
74            << mCounter[toIndex(NamedObjectType::FRAMEBUFFER)].load();
75         ss << " ShaderOrProgram: "
76            << mCounter[toIndex(NamedObjectType::SHADER_OR_PROGRAM)].load();
77         ss << " Sampler: "
78            << mCounter[toIndex(NamedObjectType::SAMPLER)].load();
79         ss << " Query: " << mCounter[toIndex(NamedObjectType::QUERY)].load();
80         ss << " VertexArrayObject: "
81            << mCounter[toIndex(NamedObjectType::VERTEX_ARRAY_OBJECT)].load();
82         ss << " TransformFeedback: "
83            << mCounter[toIndex(NamedObjectType::TRANSFORM_FEEDBACK)].load()
84            << "\n";
85         return ss.str();
86     }
87 
88 private:
89     std::array<std::atomic<size_t>, toIndex(NamedObjectType::NUM_OBJECT_TYPES)>
90             mCounter = {};
91 };
92 
sGlobal()93 static GLObjectCounter* sGlobal() {
94     static GLObjectCounter* g = new GLObjectCounter;
95     return g;
96 }
97 
GLObjectCounter()98 GLObjectCounter::GLObjectCounter() : mImpl(new GLObjectCounter::Impl()) {}
99 
incCount(size_t type)100 void GLObjectCounter::incCount(size_t type) {
101     mImpl->incCount(type);
102 }
103 
decCount(size_t type)104 void GLObjectCounter::decCount(size_t type) {
105     mImpl->decCount(type);
106 }
107 
getCounts()108 std::vector<size_t> GLObjectCounter::getCounts() {
109     return mImpl->getCounts();
110 }
111 
printUsage()112 std::string GLObjectCounter::printUsage() {
113     return mImpl->printUsage();
114 }
115 
get()116 GLObjectCounter* GLObjectCounter::get() {
117     return sGlobal();
118 }
119 
120 }  // namespace base
121 }  // namespace android
122