1 /*
2 * Copyright (C) 2016 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 "GLcommon/NamedObject.h"
18
19 #include "GLcommon/GLEScontext.h"
20 #include "GLcommon/ObjectNameSpace.h"
21
22 #include "aemu/base/GLObjectCounter.h"
23 #include "aemu/base/synchronization/Lock.h"
24
toIndex(NamedObjectType type)25 static constexpr int toIndex(NamedObjectType type) {
26 return static_cast<int>(type);
27 }
28
NamedObject(GenNameInfo genNameInfo,GlobalNameSpace * globalNameSpace)29 NamedObject::NamedObject(GenNameInfo genNameInfo,
30 GlobalNameSpace *globalNameSpace) {
31 // We need a global mutex from globalNameSpace
32 // And we can't change the mutex to a static variable because it would be
33 // passed around different shared libraries.
34 m_globalNameSpace = globalNameSpace;
35 m_type = genNameInfo.m_type;
36 if (genNameInfo.m_existingGlobal) {
37 fprintf(stderr, "%s: global name %u exists already\n", __func__, genNameInfo.m_existingGlobal);
38 m_globalName = genNameInfo.m_existingGlobal;
39 } else {
40 android::base::AutoLock _lock(m_globalNameSpace->m_lock);
41 switch (genNameInfo.m_type) {
42 case NamedObjectType::VERTEXBUFFER:
43 GLEScontext::dispatcher().glGenBuffers(1,&m_globalName);
44 break;
45 case NamedObjectType::TEXTURE:
46 GLEScontext::dispatcher().glGenTextures(1,&m_globalName);
47 break;
48 case NamedObjectType::RENDERBUFFER:
49 GLEScontext::dispatcher().glGenRenderbuffers(1, &m_globalName);
50 break;
51 case NamedObjectType::FRAMEBUFFER:
52 GLEScontext::dispatcher().glGenFramebuffers(1,&m_globalName);
53 break;
54 case NamedObjectType::SHADER_OR_PROGRAM:
55 switch (genNameInfo.m_shaderProgramType) {
56 case ShaderProgramType::PROGRAM:
57 m_globalName = GLEScontext::dispatcher().glCreateProgram();
58 break;
59 case ShaderProgramType::VERTEX_SHADER:
60 m_globalName = GLEScontext::dispatcher().glCreateShader(
61 GL_VERTEX_SHADER);
62 break;
63 case ShaderProgramType::FRAGMENT_SHADER:
64 m_globalName = GLEScontext::dispatcher().glCreateShader(
65 GL_FRAGMENT_SHADER);
66 break;
67 case ShaderProgramType::COMPUTE_SHADER:
68 m_globalName = GLEScontext::dispatcher().glCreateShader(
69 GL_COMPUTE_SHADER);
70 break;
71 }
72 break;
73 case NamedObjectType::SAMPLER:
74 GLEScontext::dispatcher().glGenSamplers(1, &m_globalName);
75 break;
76 case NamedObjectType::QUERY:
77 GLEScontext::dispatcher().glGenQueries(1, &m_globalName);
78 break;
79 case NamedObjectType::VERTEX_ARRAY_OBJECT:
80 GLEScontext::dispatcher().glGenVertexArrays(1, &m_globalName);
81 break;
82 case NamedObjectType::TRANSFORM_FEEDBACK:
83 GLEScontext::dispatcher().glGenTransformFeedbacks(
84 1, &m_globalName);
85 break;
86 default:
87 m_globalName = 0;
88 }
89 android::base::GLObjectCounter::get()->incCount(toIndex(genNameInfo.m_type));
90 }
91 }
92
~NamedObject()93 NamedObject::~NamedObject() {
94 android::base::AutoLock _lock(m_globalNameSpace->m_lock);
95 assert(GLEScontext::dispatcher().isInitialized());
96 switch (m_type) {
97 case NamedObjectType::VERTEXBUFFER:
98 GLEScontext::dispatcher().glDeleteBuffers(1, &m_globalName);
99 break;
100 case NamedObjectType::TEXTURE:
101 GLEScontext::dispatcher().glDeleteTextures(1, &m_globalName);
102 break;
103 case NamedObjectType::RENDERBUFFER:
104 GLEScontext::dispatcher().glDeleteRenderbuffers(1, &m_globalName);
105 break;
106 case NamedObjectType::FRAMEBUFFER:
107 GLEScontext::dispatcher().glDeleteFramebuffers(1, &m_globalName);
108 break;
109 case NamedObjectType::SHADER_OR_PROGRAM:
110 if (GLEScontext::dispatcher().glIsProgram(m_globalName)) {
111 GLEScontext::dispatcher().glDeleteProgram(m_globalName);
112 } else {
113 GLEScontext::dispatcher().glDeleteShader(m_globalName);
114 }
115 break;
116 case NamedObjectType::SAMPLER:
117 GLEScontext::dispatcher().glDeleteSamplers(1, &m_globalName);
118 break;
119 case NamedObjectType::QUERY:
120 GLEScontext::dispatcher().glDeleteQueries(1, &m_globalName);
121 break;
122 case NamedObjectType::VERTEX_ARRAY_OBJECT:
123 GLEScontext::dispatcher().glDeleteVertexArrays(1, &m_globalName);
124 break;
125 case NamedObjectType::TRANSFORM_FEEDBACK:
126 GLEScontext::dispatcher().glDeleteTransformFeedbacks(1, &m_globalName);
127 break;
128 default:
129 break;
130 }
131 android::base::GLObjectCounter::get()->decCount(toIndex(m_type));
132 }
133
134