blob: 0c1434807b6878da91c16885e8629272a421e004 [file] [log] [blame]
bsalomon@google.com27847de2011-02-22 20:59:41 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.
bsalomon@google.com27847de2011-02-22 20:59:41 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +00009#include "GrGLUtil.h"
bsalomon@google.comf987d1b2011-04-04 17:13:52 +000010
bsalomon@google.com0b77d682011-08-19 13:28:54 +000011void GrGLClearErr(const GrGLInterface* gl) {
12 while (GR_GL_NO_ERROR != gl->fGetError()) {}
bsalomon@google.comf987d1b2011-04-04 17:13:52 +000013}
bsalomon@google.com27847de2011-02-22 20:59:41 +000014
bsalomon@google.com845eafd2012-06-18 12:27:29 +000015namespace {
16const char *get_error_string(uint32_t err) {
17 switch (err) {
18 case GR_GL_NO_ERROR:
19 return "";
20 case GR_GL_INVALID_ENUM:
21 return "Invalid Enum";
22 case GR_GL_INVALID_VALUE:
23 return "Invalid Value";
24 case GR_GL_INVALID_OPERATION:
25 return "Invalid Operation";
26 case GR_GL_OUT_OF_MEMORY:
27 return "Out of Memory";
28 case GR_GL_CONTEXT_LOST:
29 return "Context Lost";
30 }
31 return "Unknown";
32}
33}
34
bsalomon@google.com0b77d682011-08-19 13:28:54 +000035void GrGLCheckErr(const GrGLInterface* gl,
36 const char* location,
37 const char* call) {
38 uint32_t err = GR_GL_GET_ERROR(gl);
twiz@google.com0f31ca72011-03-18 17:38:11 +000039 if (GR_GL_NO_ERROR != err) {
bsalomon@google.com845eafd2012-06-18 12:27:29 +000040 GrPrintf("---- glGetError 0x%x(%s)", err, get_error_string(err));
bsalomon@google.com27847de2011-02-22 20:59:41 +000041 if (NULL != location) {
42 GrPrintf(" at\n\t%s", location);
43 }
44 if (NULL != call) {
45 GrPrintf("\n\t\t%s", call);
46 }
47 GrPrintf("\n");
48 }
49}
50
bsalomon@google.com960d1142013-05-29 13:11:54 +000051namespace {
52// Mesa uses a non-standard version string of format: 1.4 Mesa <mesa_major>.<mesa_minor>.
53// The mapping of from mesa version to GL version came from here: http://www.mesa3d.org/intro.html
54bool get_gl_version_for_mesa(int mesaMajorVersion, int* major, int* minor) {
55 switch (mesaMajorVersion) {
56 case 2:
57 case 3:
58 case 4:
59 case 5:
60 case 6:
61 *major = 1;
62 *minor = mesaMajorVersion - 1;
63 return true;
64 case 7:
65 *major = 2;
66 *minor = 1;
67 return true;
68 case 8:
69 *major = 3;
70 *minor = 0;
71 return true;
72 case 9:
73 *major = 3;
74 *minor = 1;
75 return true;
76 default:
77 return false;
78 }
79}
80}
81
bsalomon@google.com27847de2011-02-22 20:59:41 +000082///////////////////////////////////////////////////////////////////////////////
83
bsalomon@google.comd5d10492011-04-28 21:16:31 +000084#if GR_GL_LOG_CALLS
85 bool gLogCallsGL = !!(GR_GL_LOG_CALLS_START);
86#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +000087
bsalomon@google.comd5d10492011-04-28 21:16:31 +000088#if GR_GL_CHECK_ERROR
89 bool gCheckErrorGL = !!(GR_GL_CHECK_ERROR_START);
90#endif
91
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +000092///////////////////////////////////////////////////////////////////////////////
93
94GrGLBinding GrGLGetBindingInUseFromString(const char* versionString) {
95 if (NULL == versionString) {
96 GrAssert(!"NULL GL version string.");
97 return kNone_GrGLBinding;
98 }
99
100 int major, minor;
101
102 // check for desktop
103 int n = sscanf(versionString, "%d.%d", &major, &minor);
104 if (2 == n) {
105 return kDesktop_GrGLBinding;
106 }
107
108 // check for ES 1
109 char profile[2];
bsalomon@google.com960d1142013-05-29 13:11:54 +0000110 n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile+1, &major, &minor);
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +0000111 if (4 == n) {
112 // we no longer support ES1.
113 return kNone_GrGLBinding;
114 }
115
116 // check for ES2
117 n = sscanf(versionString, "OpenGL ES %d.%d", &major, &minor);
118 if (2 == n) {
bsalomon@google.com791816a2013-08-15 18:54:39 +0000119 return kES_GrGLBinding;
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +0000120 }
121 return kNone_GrGLBinding;
122}
123
commit-bot@chromium.org459104c2013-06-14 14:42:56 +0000124bool GrGLIsMesaFromVersionString(const char* versionString) {
125 int major, minor, mesaMajor, mesaMinor;
126 int n = sscanf(versionString, "%d.%d Mesa %d.%d", &major, &minor, &mesaMajor, &mesaMinor);
127 return 4 == n;
128}
129
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +0000130GrGLVersion GrGLGetVersionFromString(const char* versionString) {
131 if (NULL == versionString) {
132 GrAssert(!"NULL GL version string.");
133 return 0;
134 }
135
136 int major, minor;
137
bsalomon@google.com960d1142013-05-29 13:11:54 +0000138 // check for mesa
139 int mesaMajor, mesaMinor;
140 int n = sscanf(versionString, "%d.%d Mesa %d.%d", &major, &minor, &mesaMajor, &mesaMinor);
141 if (4 == n) {
142 if (get_gl_version_for_mesa(mesaMajor, &major, &minor)) {
143 return GR_GL_VER(major, minor);
144 } else {
145 return 0;
146 }
147 }
148
149 n = sscanf(versionString, "%d.%d", &major, &minor);
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +0000150 if (2 == n) {
151 return GR_GL_VER(major, minor);
152 }
153
154 char profile[2];
155 n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile+1,
156 &major, &minor);
157 if (4 == n) {
158 return GR_GL_VER(major, minor);
159 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000160
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +0000161 n = sscanf(versionString, "OpenGL ES %d.%d", &major, &minor);
162 if (2 == n) {
163 return GR_GL_VER(major, minor);
164 }
165
166 return 0;
167}
168
169GrGLSLVersion GrGLGetGLSLVersionFromString(const char* versionString) {
170 if (NULL == versionString) {
171 GrAssert(!"NULL GLSL version string.");
172 return 0;
173 }
174
175 int major, minor;
176
177 int n = sscanf(versionString, "%d.%d", &major, &minor);
178 if (2 == n) {
179 return GR_GLSL_VER(major, minor);
180 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000181
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +0000182 n = sscanf(versionString, "OpenGL ES GLSL ES %d.%d", &major, &minor);
183 if (2 == n) {
184 return GR_GLSL_VER(major, minor);
185 }
186
187#ifdef SK_BUILD_FOR_ANDROID
188 // android hack until the gpu vender updates their drivers
189 n = sscanf(versionString, "OpenGL ES GLSL %d.%d", &major, &minor);
190 if (2 == n) {
191 return GR_GLSL_VER(major, minor);
192 }
193#endif
194
195 return 0;
196}
197
bsalomon@google.com0b1e4812012-10-23 13:52:43 +0000198GrGLVendor GrGLGetVendorFromString(const char* vendorString) {
199 if (NULL != vendorString) {
bsalomon@google.com96966a52013-02-21 16:34:21 +0000200 if (0 == strcmp(vendorString, "ARM")) {
201 return kARM_GrGLVendor;
202 }
bsalomon@google.com3012ded2013-02-22 16:44:04 +0000203 if (0 == strcmp(vendorString, "Imagination Technologies")) {
204 return kImagination_GrGLVendor;
205 }
206 if (0 == strcmp(vendorString, "Intel")) {
207 return kIntel_GrGLVendor;
208 }
bsalomon@google.com0b1e4812012-10-23 13:52:43 +0000209 }
bsalomon@google.com0b1e4812012-10-23 13:52:43 +0000210 return kOther_GrGLVendor;
211}
212
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +0000213GrGLBinding GrGLGetBindingInUse(const GrGLInterface* gl) {
214 const GrGLubyte* v;
215 GR_GL_CALL_RET(gl, v, GetString(GR_GL_VERSION));
216 return GrGLGetBindingInUseFromString((const char*) v);
217}
218
219GrGLVersion GrGLGetVersion(const GrGLInterface* gl) {
220 const GrGLubyte* v;
221 GR_GL_CALL_RET(gl, v, GetString(GR_GL_VERSION));
222 return GrGLGetVersionFromString((const char*) v);
223}
224
225GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface* gl) {
226 const GrGLubyte* v;
227 GR_GL_CALL_RET(gl, v, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
228 return GrGLGetGLSLVersionFromString((const char*) v);
229}
bsalomon@google.com0b1e4812012-10-23 13:52:43 +0000230
231GrGLVendor GrGLGetVendor(const GrGLInterface* gl) {
232 const GrGLubyte* v;
233 GR_GL_CALL_RET(gl, v, GetString(GR_GL_VENDOR));
234 return GrGLGetVendorFromString((const char*) v);
235}