blob: 96679fcfd5cd558e78b17101c2f060e9056e912f [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"
commit-bot@chromium.org215a6822013-09-05 18:28:42 +000010#include "SkMatrix.h"
bungeman@google.comfab44db2013-10-11 18:50:45 +000011#include <stdio.h>
bsalomon@google.comf987d1b2011-04-04 17:13:52 +000012
bsalomon@google.com0b77d682011-08-19 13:28:54 +000013void GrGLClearErr(const GrGLInterface* gl) {
14 while (GR_GL_NO_ERROR != gl->fGetError()) {}
bsalomon@google.comf987d1b2011-04-04 17:13:52 +000015}
bsalomon@google.com27847de2011-02-22 20:59:41 +000016
bsalomon@google.com845eafd2012-06-18 12:27:29 +000017namespace {
18const char *get_error_string(uint32_t err) {
19 switch (err) {
20 case GR_GL_NO_ERROR:
21 return "";
22 case GR_GL_INVALID_ENUM:
23 return "Invalid Enum";
24 case GR_GL_INVALID_VALUE:
25 return "Invalid Value";
26 case GR_GL_INVALID_OPERATION:
27 return "Invalid Operation";
28 case GR_GL_OUT_OF_MEMORY:
29 return "Out of Memory";
30 case GR_GL_CONTEXT_LOST:
31 return "Context Lost";
32 }
33 return "Unknown";
34}
35}
36
bsalomon@google.com0b77d682011-08-19 13:28:54 +000037void GrGLCheckErr(const GrGLInterface* gl,
38 const char* location,
39 const char* call) {
40 uint32_t err = GR_GL_GET_ERROR(gl);
twiz@google.com0f31ca72011-03-18 17:38:11 +000041 if (GR_GL_NO_ERROR != err) {
bsalomon@google.com845eafd2012-06-18 12:27:29 +000042 GrPrintf("---- glGetError 0x%x(%s)", err, get_error_string(err));
bsalomon@google.com27847de2011-02-22 20:59:41 +000043 if (NULL != location) {
44 GrPrintf(" at\n\t%s", location);
45 }
46 if (NULL != call) {
47 GrPrintf("\n\t\t%s", call);
48 }
49 GrPrintf("\n");
50 }
51}
52
bsalomon@google.com960d1142013-05-29 13:11:54 +000053namespace {
54// Mesa uses a non-standard version string of format: 1.4 Mesa <mesa_major>.<mesa_minor>.
55// The mapping of from mesa version to GL version came from here: http://www.mesa3d.org/intro.html
56bool get_gl_version_for_mesa(int mesaMajorVersion, int* major, int* minor) {
57 switch (mesaMajorVersion) {
58 case 2:
59 case 3:
60 case 4:
61 case 5:
62 case 6:
63 *major = 1;
64 *minor = mesaMajorVersion - 1;
65 return true;
66 case 7:
67 *major = 2;
68 *minor = 1;
69 return true;
70 case 8:
71 *major = 3;
72 *minor = 0;
73 return true;
74 case 9:
75 *major = 3;
76 *minor = 1;
77 return true;
78 default:
79 return false;
80 }
81}
82}
83
bsalomon@google.com27847de2011-02-22 20:59:41 +000084///////////////////////////////////////////////////////////////////////////////
85
bsalomon@google.comd5d10492011-04-28 21:16:31 +000086#if GR_GL_LOG_CALLS
87 bool gLogCallsGL = !!(GR_GL_LOG_CALLS_START);
88#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +000089
bsalomon@google.comd5d10492011-04-28 21:16:31 +000090#if GR_GL_CHECK_ERROR
91 bool gCheckErrorGL = !!(GR_GL_CHECK_ERROR_START);
92#endif
93
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +000094///////////////////////////////////////////////////////////////////////////////
95
96GrGLBinding GrGLGetBindingInUseFromString(const char* versionString) {
97 if (NULL == versionString) {
mtklein@google.com330313a2013-08-22 15:37:26 +000098 SkDEBUGFAIL("NULL GL version string.");
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +000099 return kNone_GrGLBinding;
100 }
101
102 int major, minor;
103
104 // check for desktop
105 int n = sscanf(versionString, "%d.%d", &major, &minor);
106 if (2 == n) {
107 return kDesktop_GrGLBinding;
108 }
109
110 // check for ES 1
111 char profile[2];
bsalomon@google.com960d1142013-05-29 13:11:54 +0000112 n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile+1, &major, &minor);
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +0000113 if (4 == n) {
114 // we no longer support ES1.
115 return kNone_GrGLBinding;
116 }
117
118 // check for ES2
119 n = sscanf(versionString, "OpenGL ES %d.%d", &major, &minor);
120 if (2 == n) {
bsalomon@google.com791816a2013-08-15 18:54:39 +0000121 return kES_GrGLBinding;
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +0000122 }
123 return kNone_GrGLBinding;
124}
125
commit-bot@chromium.org459104c2013-06-14 14:42:56 +0000126bool GrGLIsMesaFromVersionString(const char* versionString) {
127 int major, minor, mesaMajor, mesaMinor;
128 int n = sscanf(versionString, "%d.%d Mesa %d.%d", &major, &minor, &mesaMajor, &mesaMinor);
129 return 4 == n;
130}
131
commit-bot@chromium.orgc9424b82013-10-30 20:03:16 +0000132bool GrGLIsChromiumFromRendererString(const char* rendererString) {
133 return 0 == strcmp(rendererString, "Chromium");
134}
135
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +0000136GrGLVersion GrGLGetVersionFromString(const char* versionString) {
137 if (NULL == versionString) {
mtklein@google.com330313a2013-08-22 15:37:26 +0000138 SkDEBUGFAIL("NULL GL version string.");
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +0000139 return 0;
140 }
141
142 int major, minor;
143
bsalomon@google.com960d1142013-05-29 13:11:54 +0000144 // check for mesa
145 int mesaMajor, mesaMinor;
146 int n = sscanf(versionString, "%d.%d Mesa %d.%d", &major, &minor, &mesaMajor, &mesaMinor);
147 if (4 == n) {
148 if (get_gl_version_for_mesa(mesaMajor, &major, &minor)) {
149 return GR_GL_VER(major, minor);
150 } else {
151 return 0;
152 }
153 }
154
155 n = sscanf(versionString, "%d.%d", &major, &minor);
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +0000156 if (2 == n) {
157 return GR_GL_VER(major, minor);
158 }
159
160 char profile[2];
161 n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile+1,
162 &major, &minor);
163 if (4 == n) {
164 return GR_GL_VER(major, minor);
165 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000166
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +0000167 n = sscanf(versionString, "OpenGL ES %d.%d", &major, &minor);
168 if (2 == n) {
169 return GR_GL_VER(major, minor);
170 }
171
172 return 0;
173}
174
175GrGLSLVersion GrGLGetGLSLVersionFromString(const char* versionString) {
176 if (NULL == versionString) {
mtklein@google.com330313a2013-08-22 15:37:26 +0000177 SkDEBUGFAIL("NULL GLSL version string.");
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +0000178 return 0;
179 }
180
181 int major, minor;
182
183 int n = sscanf(versionString, "%d.%d", &major, &minor);
184 if (2 == n) {
185 return GR_GLSL_VER(major, minor);
186 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000187
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +0000188 n = sscanf(versionString, "OpenGL ES GLSL ES %d.%d", &major, &minor);
189 if (2 == n) {
190 return GR_GLSL_VER(major, minor);
191 }
192
193#ifdef SK_BUILD_FOR_ANDROID
194 // android hack until the gpu vender updates their drivers
195 n = sscanf(versionString, "OpenGL ES GLSL %d.%d", &major, &minor);
196 if (2 == n) {
197 return GR_GLSL_VER(major, minor);
198 }
199#endif
200
201 return 0;
202}
203
bsalomon@google.com0b1e4812012-10-23 13:52:43 +0000204GrGLVendor GrGLGetVendorFromString(const char* vendorString) {
205 if (NULL != vendorString) {
bsalomon@google.com96966a52013-02-21 16:34:21 +0000206 if (0 == strcmp(vendorString, "ARM")) {
207 return kARM_GrGLVendor;
208 }
bsalomon@google.com3012ded2013-02-22 16:44:04 +0000209 if (0 == strcmp(vendorString, "Imagination Technologies")) {
210 return kImagination_GrGLVendor;
211 }
212 if (0 == strcmp(vendorString, "Intel")) {
213 return kIntel_GrGLVendor;
214 }
commit-bot@chromium.org7a434a22013-08-21 14:01:56 +0000215 if (0 == strcmp(vendorString, "Qualcomm")) {
216 return kQualcomm_GrGLVendor;
217 }
bsalomon@google.com0b1e4812012-10-23 13:52:43 +0000218 }
bsalomon@google.com0b1e4812012-10-23 13:52:43 +0000219 return kOther_GrGLVendor;
220}
221
commit-bot@chromium.org0694ea72013-09-18 13:00:28 +0000222GrGLRenderer GrGLGetRendererFromString(const char* rendererString) {
223 if (NULL != rendererString) {
224 if (0 == strcmp(rendererString, "NVIDIA Tegra 3")) {
225 return kTegra3_GrGLRenderer;
226 }
227 }
228 return kOther_GrGLRenderer;
229}
230
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +0000231GrGLBinding GrGLGetBindingInUse(const GrGLInterface* gl) {
232 const GrGLubyte* v;
233 GR_GL_CALL_RET(gl, v, GetString(GR_GL_VERSION));
234 return GrGLGetBindingInUseFromString((const char*) v);
235}
236
237GrGLVersion GrGLGetVersion(const GrGLInterface* gl) {
238 const GrGLubyte* v;
239 GR_GL_CALL_RET(gl, v, GetString(GR_GL_VERSION));
240 return GrGLGetVersionFromString((const char*) v);
241}
242
243GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface* gl) {
244 const GrGLubyte* v;
245 GR_GL_CALL_RET(gl, v, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
246 return GrGLGetGLSLVersionFromString((const char*) v);
247}
bsalomon@google.com0b1e4812012-10-23 13:52:43 +0000248
249GrGLVendor GrGLGetVendor(const GrGLInterface* gl) {
250 const GrGLubyte* v;
251 GR_GL_CALL_RET(gl, v, GetString(GR_GL_VENDOR));
252 return GrGLGetVendorFromString((const char*) v);
253}
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000254
commit-bot@chromium.org0694ea72013-09-18 13:00:28 +0000255GrGLRenderer GrGLGetRenderer(const GrGLInterface* gl) {
256 const GrGLubyte* v;
257 GR_GL_CALL_RET(gl, v, GetString(GR_GL_RENDERER));
258 return GrGLGetRendererFromString((const char*) v);
259}
260
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000261template<> void GrGLGetMatrix<3>(GrGLfloat* dest, const SkMatrix& src) {
262 // Col 0
263 dest[0] = SkScalarToFloat(src[SkMatrix::kMScaleX]);
264 dest[1] = SkScalarToFloat(src[SkMatrix::kMSkewY]);
265 dest[2] = SkScalarToFloat(src[SkMatrix::kMPersp0]);
266
267 // Col 1
268 dest[3] = SkScalarToFloat(src[SkMatrix::kMSkewX]);
269 dest[4] = SkScalarToFloat(src[SkMatrix::kMScaleY]);
270 dest[5] = SkScalarToFloat(src[SkMatrix::kMPersp1]);
271
272 // Col 2
273 dest[6] = SkScalarToFloat(src[SkMatrix::kMTransX]);
274 dest[7] = SkScalarToFloat(src[SkMatrix::kMTransY]);
275 dest[8] = SkScalarToFloat(src[SkMatrix::kMPersp2]);
276}
277
278template<> void GrGLGetMatrix<4>(GrGLfloat* dest, const SkMatrix& src) {
279 // Col 0
280 dest[0] = SkScalarToFloat(src[SkMatrix::kMScaleX]);
281 dest[1] = SkScalarToFloat(src[SkMatrix::kMSkewY]);
282 dest[2] = 0;
283 dest[3] = SkScalarToFloat(src[SkMatrix::kMPersp0]);
284
285 // Col 1
286 dest[4] = SkScalarToFloat(src[SkMatrix::kMSkewX]);
287 dest[5] = SkScalarToFloat(src[SkMatrix::kMScaleY]);
288 dest[6] = 0;
289 dest[7] = SkScalarToFloat(src[SkMatrix::kMPersp1]);
290
291 // Col 2
292 dest[8] = 0;
293 dest[9] = 0;
294 dest[10] = 1;
295 dest[11] = 0;
296
297 // Col 3
298 dest[12] = SkScalarToFloat(src[SkMatrix::kMTransX]);
299 dest[13] = SkScalarToFloat(src[SkMatrix::kMTransY]);
300 dest[14] = 0;
301 dest[15] = SkScalarToFloat(src[SkMatrix::kMPersp2]);
302}