1 /*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8
9 #include "GrGLUtil.h"
10
11
GrGLClearErr(const GrGLInterface * gl)12 void GrGLClearErr(const GrGLInterface* gl) {
13 while (GR_GL_NO_ERROR != gl->fGetError()) {}
14 }
15
16 namespace {
get_error_string(uint32_t err)17 const char *get_error_string(uint32_t err) {
18 switch (err) {
19 case GR_GL_NO_ERROR:
20 return "";
21 case GR_GL_INVALID_ENUM:
22 return "Invalid Enum";
23 case GR_GL_INVALID_VALUE:
24 return "Invalid Value";
25 case GR_GL_INVALID_OPERATION:
26 return "Invalid Operation";
27 case GR_GL_OUT_OF_MEMORY:
28 return "Out of Memory";
29 case GR_GL_CONTEXT_LOST:
30 return "Context Lost";
31 }
32 return "Unknown";
33 }
34 }
35
GrGLCheckErr(const GrGLInterface * gl,const char * location,const char * call)36 void GrGLCheckErr(const GrGLInterface* gl,
37 const char* location,
38 const char* call) {
39 uint32_t err = GR_GL_GET_ERROR(gl);
40 if (GR_GL_NO_ERROR != err) {
41 GrPrintf("---- glGetError 0x%x(%s)", err, get_error_string(err));
42 if (NULL != location) {
43 GrPrintf(" at\n\t%s", location);
44 }
45 if (NULL != call) {
46 GrPrintf("\n\t\t%s", call);
47 }
48 GrPrintf("\n");
49 }
50 }
51
52 ///////////////////////////////////////////////////////////////////////////////
53
54 #if GR_GL_LOG_CALLS
55 bool gLogCallsGL = !!(GR_GL_LOG_CALLS_START);
56 #endif
57
58 #if GR_GL_CHECK_ERROR
59 bool gCheckErrorGL = !!(GR_GL_CHECK_ERROR_START);
60 #endif
61
62 ///////////////////////////////////////////////////////////////////////////////
63
GrGLGetBindingInUseFromString(const char * versionString)64 GrGLBinding GrGLGetBindingInUseFromString(const char* versionString) {
65 if (NULL == versionString) {
66 GrAssert(!"NULL GL version string.");
67 return kNone_GrGLBinding;
68 }
69
70 int major, minor;
71
72 // check for desktop
73 int n = sscanf(versionString, "%d.%d", &major, &minor);
74 if (2 == n) {
75 return kDesktop_GrGLBinding;
76 }
77
78 // check for ES 1
79 char profile[2];
80 n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile+1,
81 &major, &minor);
82 if (4 == n) {
83 // we no longer support ES1.
84 return kNone_GrGLBinding;
85 }
86
87 // check for ES2
88 n = sscanf(versionString, "OpenGL ES %d.%d", &major, &minor);
89 if (2 == n) {
90 return kES2_GrGLBinding;
91 }
92 return kNone_GrGLBinding;
93 }
94
GrGLGetVersionFromString(const char * versionString)95 GrGLVersion GrGLGetVersionFromString(const char* versionString) {
96 if (NULL == versionString) {
97 GrAssert(!"NULL GL version string.");
98 return 0;
99 }
100
101 int major, minor;
102
103 int n = sscanf(versionString, "%d.%d", &major, &minor);
104 if (2 == n) {
105 return GR_GL_VER(major, minor);
106 }
107
108 char profile[2];
109 n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile+1,
110 &major, &minor);
111 if (4 == n) {
112 return GR_GL_VER(major, minor);
113 }
114
115 n = sscanf(versionString, "OpenGL ES %d.%d", &major, &minor);
116 if (2 == n) {
117 return GR_GL_VER(major, minor);
118 }
119
120 return 0;
121 }
122
GrGLGetGLSLVersionFromString(const char * versionString)123 GrGLSLVersion GrGLGetGLSLVersionFromString(const char* versionString) {
124 if (NULL == versionString) {
125 GrAssert(!"NULL GLSL version string.");
126 return 0;
127 }
128
129 int major, minor;
130
131 int n = sscanf(versionString, "%d.%d", &major, &minor);
132 if (2 == n) {
133 return GR_GLSL_VER(major, minor);
134 }
135
136 n = sscanf(versionString, "OpenGL ES GLSL ES %d.%d", &major, &minor);
137 if (2 == n) {
138 return GR_GLSL_VER(major, minor);
139 }
140
141 #ifdef SK_BUILD_FOR_ANDROID
142 // android hack until the gpu vender updates their drivers
143 n = sscanf(versionString, "OpenGL ES GLSL %d.%d", &major, &minor);
144 if (2 == n) {
145 return GR_GLSL_VER(major, minor);
146 }
147 #endif
148
149 return 0;
150 }
151
GrGLHasExtensionFromString(const char * ext,const char * extensionString)152 bool GrGLHasExtensionFromString(const char* ext, const char* extensionString) {
153 int extLength = strlen(ext);
154
155 while (true) {
156 int n = strcspn(extensionString, " ");
157 if (n == extLength && 0 == strncmp(ext, extensionString, n)) {
158 return true;
159 }
160 if (0 == extensionString[n]) {
161 return false;
162 }
163 extensionString += n+1;
164 }
165
166 return false;
167 }
168
GrGLGetVendorFromString(const char * vendorString)169 GrGLVendor GrGLGetVendorFromString(const char* vendorString) {
170 if (NULL != vendorString) {
171 if (0 == strcmp(vendorString, "Intel")) {
172 return kIntel_GrGLVendor;
173 }
174 }
175
176 return kOther_GrGLVendor;
177 }
178
GrGLHasExtension(const GrGLInterface * gl,const char * ext)179 bool GrGLHasExtension(const GrGLInterface* gl, const char* ext) {
180 const GrGLubyte* glstr;
181 GR_GL_CALL_RET(gl, glstr, GetString(GR_GL_EXTENSIONS));
182 return GrGLHasExtensionFromString(ext, (const char*) glstr);
183 }
184
GrGLGetBindingInUse(const GrGLInterface * gl)185 GrGLBinding GrGLGetBindingInUse(const GrGLInterface* gl) {
186 const GrGLubyte* v;
187 GR_GL_CALL_RET(gl, v, GetString(GR_GL_VERSION));
188 return GrGLGetBindingInUseFromString((const char*) v);
189 }
190
GrGLGetVersion(const GrGLInterface * gl)191 GrGLVersion GrGLGetVersion(const GrGLInterface* gl) {
192 const GrGLubyte* v;
193 GR_GL_CALL_RET(gl, v, GetString(GR_GL_VERSION));
194 return GrGLGetVersionFromString((const char*) v);
195 }
196
GrGLGetGLSLVersion(const GrGLInterface * gl)197 GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface* gl) {
198 const GrGLubyte* v;
199 GR_GL_CALL_RET(gl, v, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
200 return GrGLGetGLSLVersionFromString((const char*) v);
201 }
202
GrGLGetVendor(const GrGLInterface * gl)203 GrGLVendor GrGLGetVendor(const GrGLInterface* gl) {
204 const GrGLubyte* v;
205 GR_GL_CALL_RET(gl, v, GetString(GR_GL_VENDOR));
206 return GrGLGetVendorFromString((const char*) v);
207 }
208