blob: 0e9e21fb2f9d33c8facae2ca5962308a1a030edc [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.com845eafd2012-06-18 12:27:29 +000011
bsalomon@google.com0b77d682011-08-19 13:28:54 +000012void GrGLClearErr(const GrGLInterface* gl) {
13 while (GR_GL_NO_ERROR != gl->fGetError()) {}
bsalomon@google.comf987d1b2011-04-04 17:13:52 +000014}
bsalomon@google.com27847de2011-02-22 20:59:41 +000015
bsalomon@google.com845eafd2012-06-18 12:27:29 +000016namespace {
17const 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
bsalomon@google.com0b77d682011-08-19 13:28:54 +000036void GrGLCheckErr(const GrGLInterface* gl,
37 const char* location,
38 const char* call) {
39 uint32_t err = GR_GL_GET_ERROR(gl);
twiz@google.com0f31ca72011-03-18 17:38:11 +000040 if (GR_GL_NO_ERROR != err) {
bsalomon@google.com845eafd2012-06-18 12:27:29 +000041 GrPrintf("---- glGetError 0x%x(%s)", err, get_error_string(err));
bsalomon@google.com27847de2011-02-22 20:59:41 +000042 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
bsalomon@google.comd5d10492011-04-28 21:16:31 +000054#if GR_GL_LOG_CALLS
55 bool gLogCallsGL = !!(GR_GL_LOG_CALLS_START);
56#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +000057
bsalomon@google.comd5d10492011-04-28 21:16:31 +000058#if GR_GL_CHECK_ERROR
59 bool gCheckErrorGL = !!(GR_GL_CHECK_ERROR_START);
60#endif
61
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +000062///////////////////////////////////////////////////////////////////////////////
63
64GrGLBinding 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
95GrGLVersion 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 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000114
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +0000115 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
123GrGLSLVersion 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 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000135
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +0000136 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
152bool 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
169bool GrGLHasExtension(const GrGLInterface* gl, const char* ext) {
170 const GrGLubyte* glstr;
171 GR_GL_CALL_RET(gl, glstr, GetString(GR_GL_EXTENSIONS));
172 return GrGLHasExtensionFromString(ext, (const char*) glstr);
173}
174
175GrGLBinding GrGLGetBindingInUse(const GrGLInterface* gl) {
176 const GrGLubyte* v;
177 GR_GL_CALL_RET(gl, v, GetString(GR_GL_VERSION));
178 return GrGLGetBindingInUseFromString((const char*) v);
179}
180
181GrGLVersion GrGLGetVersion(const GrGLInterface* gl) {
182 const GrGLubyte* v;
183 GR_GL_CALL_RET(gl, v, GetString(GR_GL_VERSION));
184 return GrGLGetVersionFromString((const char*) v);
185}
186
187GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface* gl) {
188 const GrGLubyte* v;
189 GR_GL_CALL_RET(gl, v, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
190 return GrGLGetGLSLVersionFromString((const char*) v);
191}