1 /*
2  * Copyright (C) 2021 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 "DebugGl.h"
18 
19 #include <cstdarg>
20 
21 #include "OpenGLESDispatch/DispatchTables.h"
22 
23 namespace gfxstream {
24 namespace gl {
25 
formatString(const char * format,...)26 std::string formatString(const char* format, ...) {
27     char buf[1024];
28     va_list args;
29     va_start(args, format);
30     vsnprintf(buf, 1024, format, args);
31     std::string ret(buf);
32     va_end(args);
33     return ret;
34 }
35 
ScopedDebugGroup(const std::string & message)36 ScopedDebugGroup::ScopedDebugGroup(const std::string& message) {
37     s_gles2.glGetError();
38 
39     bool groupPushed = false;
40     if (s_gles2.glPushDebugGroupKHR) {
41         s_gles2.glPushDebugGroupKHR(GL_DEBUG_SOURCE_APPLICATION_KHR, 0, message.size(),
42                                     message.c_str());
43         groupPushed = s_gles2.glGetError() == GL_NO_ERROR;
44     }
45     if (s_gles2.glPushDebugGroup && !groupPushed) {
46         s_gles2.glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, message.size(), message.c_str());
47         groupPushed = s_gles2.glGetError() == GL_NO_ERROR;
48     }
49 }
50 
~ScopedDebugGroup()51 ScopedDebugGroup::~ScopedDebugGroup() {
52     s_gles2.glGetError();
53 
54     bool groupPopped = false;
55     if (s_gles2.glPopDebugGroupKHR) {
56         s_gles2.glPopDebugGroupKHR();
57         groupPopped = s_gles2.glGetError() == GL_NO_ERROR;
58     }
59     if (s_gles2.glPopDebugGroup && !groupPopped) {
60         s_gles2.glPopDebugGroup();
61         groupPopped = s_gles2.glGetError() == GL_NO_ERROR;
62     }
63 }
64 
65 }  // namespace gl
66 }  // namespace gfxstream