blob: 39732dbb683dcc37571ecd8f334dedc42e9a043c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
twiz@google.com59a190b2011-03-14 21:23:01 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
twiz@google.com59a190b2011-03-14 21:23:01 +00007 */
8
9
twiz@google.com59a190b2011-03-14 21:23:01 +000010#include "GrTypes.h"
tomhudson@google.com6bf38b52012-02-14 15:11:59 +000011#include "gl/GrGLInterface.h"
12#include "gl/GrGLDefines.h"
twiz@google.com59a190b2011-03-14 21:23:01 +000013
14#include <stdio.h>
15
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000016#if GR_GL_PER_GL_FUNC_CALLBACK
17namespace {
18void GrGLDefaultInterfaceCallback(const GrGLInterface*) {}
19}
20#endif
21
bsalomon@google.com89ec61e2012-02-10 20:05:18 +000022GrGLBinding GrGLGetBindingInUseFromString(const char* versionString) {
23 if (NULL == versionString) {
24 GrAssert(!"NULL GL version string.");
25 return kNone_GrGLBinding;
26 }
27
28 int major, minor;
29
30 // check for desktop
31 int n = sscanf(versionString, "%d.%d", &major, &minor);
32 if (2 == n) {
33 return kDesktop_GrGLBinding;
34 }
35
36 // check for ES 1
37 char profile[2];
38 n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile+1,
39 &major, &minor);
40 if (4 == n) {
41 // we no longer support ES1.
42 return kNone_GrGLBinding;
43 }
44
45 // check for ES2
46 n = sscanf(versionString, "OpenGL ES %d.%d", &major, &minor);
47 if (2 == n) {
48 return kES2_GrGLBinding;
49 }
50 return kNone_GrGLBinding;
51}
52
bsalomon@google.comc82b8892011-09-22 14:10:33 +000053GrGLVersion GrGLGetVersionFromString(const char* versionString) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000054 if (NULL == versionString) {
55 GrAssert(!"NULL GL version string.");
56 return 0;
57 }
58
bsalomon@google.comc82b8892011-09-22 14:10:33 +000059 int major, minor;
twiz@google.com0f31ca72011-03-18 17:38:11 +000060
bsalomon@google.comc82b8892011-09-22 14:10:33 +000061 int n = sscanf(versionString, "%d.%d", &major, &minor);
twiz@google.com0f31ca72011-03-18 17:38:11 +000062 if (2 == n) {
bsalomon@google.comc82b8892011-09-22 14:10:33 +000063 return GR_GL_VER(major, minor);
twiz@google.com59a190b2011-03-14 21:23:01 +000064 }
twiz@google.com0f31ca72011-03-18 17:38:11 +000065
twiz@google.com59a190b2011-03-14 21:23:01 +000066 char profile[2];
twiz@google.com0f31ca72011-03-18 17:38:11 +000067 n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile+1,
bsalomon@google.comc82b8892011-09-22 14:10:33 +000068 &major, &minor);
69 if (4 == n) {
70 return GR_GL_VER(major, minor);
71 }
72
73 n = sscanf(versionString, "OpenGL ES %d.%d", &major, &minor);
74 if (2 == n) {
75 return GR_GL_VER(major, minor);
twiz@google.com59a190b2011-03-14 21:23:01 +000076 }
twiz@google.com0f31ca72011-03-18 17:38:11 +000077
bsalomon@google.comc82b8892011-09-22 14:10:33 +000078 return 0;
twiz@google.com59a190b2011-03-14 21:23:01 +000079}
80
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000081GrGLSLVersion GrGLGetGLSLVersionFromString(const char* versionString) {
82 if (NULL == versionString) {
83 GrAssert(!"NULL GLSL version string.");
84 return 0;
85 }
86
87 int major, minor;
88
89 int n = sscanf(versionString, "%d.%d", &major, &minor);
90 if (2 == n) {
91 return GR_GLSL_VER(major, minor);
92 }
93
94 n = sscanf(versionString, "OpenGL ES GLSL ES %d.%d", &major, &minor);
95 if (2 == n) {
96 return GR_GLSL_VER(major, minor);
97 }
djsollen@google.com55e713c2012-03-20 15:28:14 +000098
99#ifdef SK_BUILD_FOR_ANDROID
100 // android hack until the gpu vender updates their drivers
101 n = sscanf(versionString, "OpenGL ES GLSL %d.%d", &major, &minor);
102 if (2 == n) {
103 return GR_GLSL_VER(major, minor);
104 }
105#endif
106
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000107 return 0;
108}
109
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000110bool GrGLHasExtensionFromString(const char* ext, const char* extensionString) {
twiz@google.com59a190b2011-03-14 21:23:01 +0000111 int extLength = strlen(ext);
112
113 while (true) {
114 int n = strcspn(extensionString, " ");
115 if (n == extLength && 0 == strncmp(ext, extensionString, n)) {
116 return true;
117 }
118 if (0 == extensionString[n]) {
119 return false;
120 }
121 extensionString += n+1;
122 }
123
124 return false;
125}
126
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000127bool GrGLHasExtension(const GrGLInterface* gl, const char* ext) {
bsalomon@google.comdca4aab2011-09-06 19:05:24 +0000128 const GrGLubyte* glstr;
129 GR_GL_CALL_RET(gl, glstr, GetString(GR_GL_EXTENSIONS));
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000130 return GrGLHasExtensionFromString(ext, (const char*) glstr);
twiz@google.com59a190b2011-03-14 21:23:01 +0000131}
132
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000133GrGLBinding GrGLGetBindingInUse(const GrGLInterface* gl) {
134 const GrGLubyte* v;
135 GR_GL_CALL_RET(gl, v, GetString(GR_GL_VERSION));
136 return GrGLGetBindingInUseFromString((const char*) v);
137}
138
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000139GrGLVersion GrGLGetVersion(const GrGLInterface* gl) {
bsalomon@google.comdca4aab2011-09-06 19:05:24 +0000140 const GrGLubyte* v;
141 GR_GL_CALL_RET(gl, v, GetString(GR_GL_VERSION));
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000142 return GrGLGetVersionFromString((const char*) v);
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000143}
144
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000145GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface* gl) {
146 const GrGLubyte* v;
147 GR_GL_CALL_RET(gl, v, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
148 return GrGLGetGLSLVersionFromString((const char*) v);
149}
150
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000151GrGLInterface::GrGLInterface() {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000152 fBindingsExported = kNone_GrGLBinding;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000153
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000154#if GR_GL_PER_GL_FUNC_CALLBACK
155 fCallback = GrGLDefaultInterfaceCallback;
156 fCallbackData = 0;
157#endif
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000158}
159
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000160bool GrGLInterface::validate(GrGLBinding binding) const {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000161
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000162 // kNone must be 0 so that the check we're about to do can never succeed if
163 // binding == kNone.
164 GR_STATIC_ASSERT(kNone_GrGLBinding == 0);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000165
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000166 if (0 == (binding & fBindingsExported)) {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000167 return false;
168 }
169
170 // functions that are always required
171 if (NULL == fActiveTexture ||
172 NULL == fAttachShader ||
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000173 NULL == fBindAttribLocation ||
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000174 NULL == fBindBuffer ||
175 NULL == fBindTexture ||
176 NULL == fBlendFunc ||
177 NULL == fBufferData ||
178 NULL == fBufferSubData ||
179 NULL == fClear ||
180 NULL == fClearColor ||
181 NULL == fClearStencil ||
182 NULL == fColorMask ||
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000183 NULL == fCompileShader ||
184 NULL == fCreateProgram ||
185 NULL == fCreateShader ||
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000186 NULL == fCullFace ||
187 NULL == fDeleteBuffers ||
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000188 NULL == fDeleteProgram ||
189 NULL == fDeleteShader ||
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000190 NULL == fDeleteTextures ||
191 NULL == fDepthMask ||
192 NULL == fDisable ||
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000193 NULL == fDisableVertexAttribArray ||
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000194 NULL == fDrawArrays ||
195 NULL == fDrawElements ||
196 NULL == fEnable ||
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000197 NULL == fEnableVertexAttribArray ||
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000198 NULL == fFrontFace ||
199 NULL == fGenBuffers ||
200 NULL == fGenTextures ||
201 NULL == fGetBufferParameteriv ||
202 NULL == fGetError ||
203 NULL == fGetIntegerv ||
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000204 NULL == fGetProgramInfoLog ||
205 NULL == fGetProgramiv ||
206 NULL == fGetShaderInfoLog ||
207 NULL == fGetShaderiv ||
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000208 NULL == fGetString ||
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000209 NULL == fGetUniformLocation ||
210 NULL == fLinkProgram ||
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000211 NULL == fPixelStorei ||
212 NULL == fReadPixels ||
213 NULL == fScissor ||
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000214 NULL == fShaderSource ||
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000215 NULL == fStencilFunc ||
216 NULL == fStencilMask ||
217 NULL == fStencilOp ||
218 NULL == fTexImage2D ||
219 NULL == fTexParameteri ||
220 NULL == fTexSubImage2D ||
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000221 NULL == fUniform1f ||
222 NULL == fUniform1i ||
223 NULL == fUniform1fv ||
224 NULL == fUniform1iv ||
225 NULL == fUniform2f ||
226 NULL == fUniform2i ||
227 NULL == fUniform2fv ||
228 NULL == fUniform2iv ||
229 NULL == fUniform3f ||
230 NULL == fUniform3i ||
231 NULL == fUniform3fv ||
232 NULL == fUniform3iv ||
233 NULL == fUniform4f ||
234 NULL == fUniform4i ||
235 NULL == fUniform4fv ||
236 NULL == fUniform4iv ||
237 NULL == fUniformMatrix2fv ||
238 NULL == fUniformMatrix3fv ||
239 NULL == fUniformMatrix4fv ||
240 NULL == fUseProgram ||
241 NULL == fVertexAttrib4fv ||
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000242 NULL == fVertexAttribPointer ||
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000243 NULL == fViewport ||
244 NULL == fBindFramebuffer ||
245 NULL == fBindRenderbuffer ||
246 NULL == fCheckFramebufferStatus ||
247 NULL == fDeleteFramebuffers ||
248 NULL == fDeleteRenderbuffers ||
bsalomon@google.com373a6632011-10-19 20:43:20 +0000249 NULL == fFinish ||
250 NULL == fFlush ||
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000251 NULL == fFramebufferRenderbuffer ||
252 NULL == fFramebufferTexture2D ||
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000253 NULL == fGetFramebufferAttachmentParameteriv ||
254 NULL == fGetRenderbufferParameteriv ||
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000255 NULL == fGenFramebuffers ||
256 NULL == fGenRenderbuffers ||
257 NULL == fRenderbufferStorage) {
258 return false;
259 }
260
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000261 const char* ext;
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000262 GrGLVersion glVer = GrGLGetVersion(this);
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000263 ext = (const char*)fGetString(GR_GL_EXTENSIONS);
264
265 // Now check that baseline ES/Desktop fns not covered above are present
266 // and that we have fn pointers for any advertised extensions that we will
267 // try to use.
268
269 // these functions are part of ES2, we assume they are available
270 // On the desktop we assume they are available if the extension
271 // is present or GL version is high enough.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000272 if (kES2_GrGLBinding == binding) {
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000273 if (NULL == fBlendColor ||
274 NULL == fStencilFuncSeparate ||
275 NULL == fStencilMaskSeparate ||
276 NULL == fStencilOpSeparate) {
277 return false;
278 }
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000279 } else if (kDesktop_GrGLBinding == binding) {
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000280 if (glVer >= GR_GL_VER(2,0)) {
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000281 if (NULL == fStencilFuncSeparate ||
282 NULL == fStencilMaskSeparate ||
283 NULL == fStencilOpSeparate) {
284 return false;
285 }
286 }
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000287 if (glVer >= GR_GL_VER(3,0) && NULL == fBindFragDataLocation) {
bsalomon@google.combc5cf512011-09-21 16:21:07 +0000288 return false;
289 }
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000290 if (glVer >= GR_GL_VER(2,0) ||
291 GrGLHasExtensionFromString("GL_ARB_draw_buffers", ext)) {
bsalomon@google.comd32c5f52011-08-02 19:29:03 +0000292 if (NULL == fDrawBuffers) {
293 return false;
294 }
295 }
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000296 if (glVer >= GR_GL_VER(1,4) ||
297 GrGLHasExtensionFromString("GL_EXT_blend_color", ext)) {
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000298 if (NULL == fBlendColor) {
299 return false;
300 }
301 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000302 if (glVer >= GR_GL_VER(1,5) ||
303 GrGLHasExtensionFromString("GL_ARB_occlusion_query", ext)) {
304 if (NULL == fGenQueries ||
305 NULL == fDeleteQueries ||
306 NULL == fBeginQuery ||
307 NULL == fEndQuery ||
308 NULL == fGetQueryiv ||
309 NULL == fGetQueryObjectiv ||
310 NULL == fGetQueryObjectuiv) {
311 return false;
312 }
313 }
314 if (glVer >= GR_GL_VER(3,3) ||
315 GrGLHasExtensionFromString("GL_ARB_timer_query", ext) ||
316 GrGLHasExtensionFromString("GL_EXT_timer_query", ext)) {
317 if (NULL == fGetQueryObjecti64v ||
318 NULL == fGetQueryObjectui64v) {
319 return false;
320 }
321 }
322 if (glVer >= GR_GL_VER(3,3) ||
323 GrGLHasExtensionFromString("GL_ARB_timer_query", ext)) {
324 if (NULL == fQueryCounter) {
325 return false;
326 }
327 }
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000328 }
329
330 // optional function on desktop before 1.3
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000331 if (kDesktop_GrGLBinding != binding ||
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000332 (glVer >= GR_GL_VER(1,3) ||
333 GrGLHasExtensionFromString("GL_ARB_texture_compression", ext))) {
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000334 if (NULL == fCompressedTexImage2D) {
335 return false;
336 }
337 }
338
bsalomon@google.comd32c5f52011-08-02 19:29:03 +0000339 // part of desktop GL, but not ES
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000340 if (kDesktop_GrGLBinding == binding &&
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000341 (NULL == fLineWidth ||
bsalomon@google.comd32c5f52011-08-02 19:29:03 +0000342 NULL == fGetTexLevelParameteriv ||
bsalomon@google.comc49d66b2011-08-03 14:22:30 +0000343 NULL == fDrawBuffer ||
344 NULL == fReadBuffer)) {
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000345 return false;
346 }
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000347
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000348 // GL_EXT_texture_storage is part of desktop 4.2
349 // There is a desktop ARB extension and an ES+desktop EXT extension
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000350 if (kDesktop_GrGLBinding == binding) {
bsalomon@google.combaa9ea12012-01-06 19:05:43 +0000351 if (glVer >= GR_GL_VER(4,2) ||
352 GrGLHasExtensionFromString("GL_ARB_texture_storage", ext) ||
353 GrGLHasExtensionFromString("GL_EXT_texture_storage", ext)) {
354 if (NULL == fTexStorage2D) {
355 return false;
356 }
357 }
358 } else if (GrGLHasExtensionFromString("GL_EXT_texture_storage", ext)) {
359 if (NULL == fTexStorage2D) {
360 return false;
361 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000362 }
363
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000364 // FBO MSAA
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000365 if (kDesktop_GrGLBinding == binding) {
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000366 // GL 3.0 and the ARB extension have multisample + blit
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000367 if (glVer >= GR_GL_VER(3,0) || GrGLHasExtensionFromString("GL_ARB_framebuffer_object", ext)) {
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000368 if (NULL == fRenderbufferStorageMultisample ||
369 NULL == fBlitFramebuffer) {
370 return false;
371 }
372 } else {
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000373 if (GrGLHasExtensionFromString("GL_EXT_framebuffer_blit", ext) &&
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000374 NULL == fBlitFramebuffer) {
375 return false;
376 }
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000377 if (GrGLHasExtensionFromString("GL_EXT_framebuffer_multisample", ext) &&
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000378 NULL == fRenderbufferStorageMultisample) {
379 return false;
380 }
381 }
382 } else {
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000383 if (GrGLHasExtensionFromString("GL_CHROMIUM_framebuffer_multisample", ext)) {
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000384 if (NULL == fRenderbufferStorageMultisample ||
385 NULL == fBlitFramebuffer) {
386 return false;
387 }
388 }
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000389 if (GrGLHasExtensionFromString("GL_APPLE_framebuffer_multisample", ext)) {
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000390 if (NULL == fRenderbufferStorageMultisample ||
391 NULL == fResolveMultisampleFramebuffer) {
392 return false;
393 }
394 }
395 }
396
397 // On ES buffer mapping is an extension. On Desktop
398 // buffer mapping was part of original VBO extension
399 // which we require.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000400 if (kDesktop_GrGLBinding == binding ||
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000401 GrGLHasExtensionFromString("GL_OES_mapbuffer", ext)) {
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000402 if (NULL == fMapBuffer ||
403 NULL == fUnmapBuffer) {
404 return false;
405 }
406 }
407
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000408 // Dual source blending
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000409 if (kDesktop_GrGLBinding == binding &&
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000410 (glVer >= GR_GL_VER(3,3) ||
411 GrGLHasExtensionFromString("GL_ARB_blend_func_extended", ext))) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000412 if (NULL == fBindFragDataLocationIndexed) {
413 return false;
414 }
415 }
416
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000417 return true;
418}
419