blob: f4c5da67d57b266bcbcd94ba974164d6db695ea6 [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
51///////////////////////////////////////////////////////////////////////////////
52
bsalomon@google.comd5d10492011-04-28 21:16:31 +000053#if GR_GL_LOG_CALLS
54 bool gLogCallsGL = !!(GR_GL_LOG_CALLS_START);
55#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +000056
bsalomon@google.comd5d10492011-04-28 21:16:31 +000057#if GR_GL_CHECK_ERROR
58 bool gCheckErrorGL = !!(GR_GL_CHECK_ERROR_START);
59#endif
60
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +000061///////////////////////////////////////////////////////////////////////////////
62
63GrGLBinding GrGLGetBindingInUseFromString(const char* versionString) {
64 if (NULL == versionString) {
65 GrAssert(!"NULL GL version string.");
66 return kNone_GrGLBinding;
67 }
68
69 int major, minor;
70
71 // check for desktop
72 int n = sscanf(versionString, "%d.%d", &major, &minor);
73 if (2 == n) {
74 return kDesktop_GrGLBinding;
75 }
76
77 // check for ES 1
78 char profile[2];
79 n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile+1,
80 &major, &minor);
81 if (4 == n) {
82 // we no longer support ES1.
83 return kNone_GrGLBinding;
84 }
85
86 // check for ES2
87 n = sscanf(versionString, "OpenGL ES %d.%d", &major, &minor);
88 if (2 == n) {
89 return kES2_GrGLBinding;
90 }
91 return kNone_GrGLBinding;
92}
93
94GrGLVersion GrGLGetVersionFromString(const char* versionString) {
95 if (NULL == versionString) {
96 GrAssert(!"NULL GL version string.");
97 return 0;
98 }
99
100 int major, minor;
101
102 int n = sscanf(versionString, "%d.%d", &major, &minor);
103 if (2 == n) {
104 return GR_GL_VER(major, minor);
105 }
106
107 char profile[2];
108 n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile+1,
109 &major, &minor);
110 if (4 == n) {
111 return GR_GL_VER(major, minor);
112 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000113
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +0000114 n = sscanf(versionString, "OpenGL ES %d.%d", &major, &minor);
115 if (2 == n) {
116 return GR_GL_VER(major, minor);
117 }
118
119 return 0;
120}
121
122GrGLSLVersion GrGLGetGLSLVersionFromString(const char* versionString) {
123 if (NULL == versionString) {
124 GrAssert(!"NULL GLSL version string.");
125 return 0;
126 }
127
128 int major, minor;
129
130 int n = sscanf(versionString, "%d.%d", &major, &minor);
131 if (2 == n) {
132 return GR_GLSL_VER(major, minor);
133 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000134
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +0000135 n = sscanf(versionString, "OpenGL ES GLSL ES %d.%d", &major, &minor);
136 if (2 == n) {
137 return GR_GLSL_VER(major, minor);
138 }
139
140#ifdef SK_BUILD_FOR_ANDROID
141 // android hack until the gpu vender updates their drivers
142 n = sscanf(versionString, "OpenGL ES GLSL %d.%d", &major, &minor);
143 if (2 == n) {
144 return GR_GLSL_VER(major, minor);
145 }
146#endif
147
148 return 0;
149}
150
bsalomon@google.com0b1e4812012-10-23 13:52:43 +0000151GrGLVendor GrGLGetVendorFromString(const char* vendorString) {
152 if (NULL != vendorString) {
bsalomon@google.com96966a52013-02-21 16:34:21 +0000153 if (0 == strcmp(vendorString, "ARM")) {
154 return kARM_GrGLVendor;
155 }
bsalomon@google.com3012ded2013-02-22 16:44:04 +0000156 if (0 == strcmp(vendorString, "Imagination Technologies")) {
157 return kImagination_GrGLVendor;
158 }
159 if (0 == strcmp(vendorString, "Intel")) {
160 return kIntel_GrGLVendor;
161 }
bsalomon@google.com0b1e4812012-10-23 13:52:43 +0000162 }
bsalomon@google.com0b1e4812012-10-23 13:52:43 +0000163 return kOther_GrGLVendor;
164}
165
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +0000166GrGLBinding GrGLGetBindingInUse(const GrGLInterface* gl) {
167 const GrGLubyte* v;
168 GR_GL_CALL_RET(gl, v, GetString(GR_GL_VERSION));
169 return GrGLGetBindingInUseFromString((const char*) v);
170}
171
172GrGLVersion GrGLGetVersion(const GrGLInterface* gl) {
173 const GrGLubyte* v;
174 GR_GL_CALL_RET(gl, v, GetString(GR_GL_VERSION));
175 return GrGLGetVersionFromString((const char*) v);
176}
177
178GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface* gl) {
179 const GrGLubyte* v;
180 GR_GL_CALL_RET(gl, v, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
181 return GrGLGetGLSLVersionFromString((const char*) v);
182}
bsalomon@google.com0b1e4812012-10-23 13:52:43 +0000183
184GrGLVendor GrGLGetVendor(const GrGLInterface* gl) {
185 const GrGLubyte* v;
186 GR_GL_CALL_RET(gl, v, GetString(GR_GL_VENDOR));
187 return GrGLGetVendorFromString((const char*) v);
188}