2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "gl/GrGLInterface.h"
10 #include "gl/GrGLExtensions.h"
11 #include "gl/GrGLUtil.h"
15 #if GR_GL_PER_GL_FUNC_CALLBACK
17 void GrGLDefaultInterfaceCallback(const GrGLInterface*) {}
21 const GrGLInterface* GrGLInterfaceAddTestDebugMarker(const GrGLInterface* interface,
22 GrGLInsertEventMarkerProc insertEventMarkerFn,
23 GrGLPushGroupMarkerProc pushGroupMarkerFn,
24 GrGLPopGroupMarkerProc popGroupMarkerFn) {
25 GrGLInterface* newInterface = GrGLInterface::NewClone(interface);
27 if (!newInterface->fExtensions.has("GL_EXT_debug_marker")) {
28 newInterface->fExtensions.add("GL_EXT_debug_marker");
31 newInterface->fFunctions.fInsertEventMarker = insertEventMarkerFn;
32 newInterface->fFunctions.fPushGroupMarker = pushGroupMarkerFn;
33 newInterface->fFunctions.fPopGroupMarker = popGroupMarkerFn;
38 const GrGLInterface* GrGLInterfaceRemoveNVPR(const GrGLInterface* interface) {
39 GrGLInterface* newInterface = GrGLInterface::NewClone(interface);
41 newInterface->fExtensions.remove("GL_NV_path_rendering");
42 newInterface->fFunctions.fPathCommands = NULL;
43 newInterface->fFunctions.fPathCoords = NULL;
44 newInterface->fFunctions.fPathParameteri = NULL;
45 newInterface->fFunctions.fPathParameterf = NULL;
46 newInterface->fFunctions.fGenPaths = NULL;
47 newInterface->fFunctions.fDeletePaths = NULL;
48 newInterface->fFunctions.fIsPath = NULL;
49 newInterface->fFunctions.fPathStencilFunc = NULL;
50 newInterface->fFunctions.fStencilFillPath = NULL;
51 newInterface->fFunctions.fStencilStrokePath = NULL;
52 newInterface->fFunctions.fStencilFillPathInstanced = NULL;
53 newInterface->fFunctions.fStencilStrokePathInstanced = NULL;
54 newInterface->fFunctions.fPathTexGen = NULL;
55 newInterface->fFunctions.fCoverFillPath = NULL;
56 newInterface->fFunctions.fCoverStrokePath = NULL;
57 newInterface->fFunctions.fCoverFillPathInstanced = NULL;
58 newInterface->fFunctions.fCoverStrokePathInstanced = NULL;
59 newInterface->fFunctions.fStencilThenCoverFillPath = NULL;
60 newInterface->fFunctions.fStencilThenCoverStrokePath = NULL;
61 newInterface->fFunctions.fStencilThenCoverFillPathInstanced = NULL;
62 newInterface->fFunctions.fStencilThenCoverStrokePathInstanced = NULL;
63 newInterface->fFunctions.fProgramPathFragmentInputGen = NULL;
64 newInterface->fFunctions.fPathMemoryGlyphIndexArray = NULL;
68 GrGLInterface::GrGLInterface() {
69 fStandard = kNone_GrGLStandard;
71 #if GR_GL_PER_GL_FUNC_CALLBACK
72 fCallback = GrGLDefaultInterfaceCallback;
77 GrGLInterface* GrGLInterface::NewClone(const GrGLInterface* interface) {
80 GrGLInterface* clone = SkNEW(GrGLInterface);
81 clone->fStandard = interface->fStandard;
82 clone->fExtensions = interface->fExtensions;
83 clone->fFunctions = interface->fFunctions;
84 #if GR_GL_PER_GL_FUNC_CALLBACK
85 clone->fCallback = interface->fCallback;
86 clone->fCallbackData = interface->fCallbackData;
92 static int kIsDebug = 1;
94 static int kIsDebug = 0;
97 #define RETURN_FALSE_INTERFACE \
98 if (kIsDebug) { SkDebugf("%s:%d GrGLInterface::validate() failed.\n", __FILE__, __LINE__); } \
101 bool GrGLInterface::validate() const {
103 if (kNone_GrGLStandard == fStandard) {
104 RETURN_FALSE_INTERFACE
107 if (!fExtensions.isInitialized()) {
108 RETURN_FALSE_INTERFACE
111 // functions that are always required
112 if (NULL == fFunctions.fActiveTexture ||
113 NULL == fFunctions.fAttachShader ||
114 NULL == fFunctions.fBindAttribLocation ||
115 NULL == fFunctions.fBindBuffer ||
116 NULL == fFunctions.fBindTexture ||
117 NULL == fFunctions.fBlendFunc ||
118 NULL == fFunctions.fBlendColor || // -> GL >= 1.4, ES >= 2.0 or extension
119 NULL == fFunctions.fBufferData ||
120 NULL == fFunctions.fBufferSubData ||
121 NULL == fFunctions.fClear ||
122 NULL == fFunctions.fClearColor ||
123 NULL == fFunctions.fClearStencil ||
124 NULL == fFunctions.fColorMask ||
125 NULL == fFunctions.fCompileShader ||
126 NULL == fFunctions.fCopyTexSubImage2D ||
127 NULL == fFunctions.fCreateProgram ||
128 NULL == fFunctions.fCreateShader ||
129 NULL == fFunctions.fCullFace ||
130 NULL == fFunctions.fDeleteBuffers ||
131 NULL == fFunctions.fDeleteProgram ||
132 NULL == fFunctions.fDeleteShader ||
133 NULL == fFunctions.fDeleteTextures ||
134 NULL == fFunctions.fDepthMask ||
135 NULL == fFunctions.fDisable ||
136 NULL == fFunctions.fDisableVertexAttribArray ||
137 NULL == fFunctions.fDrawArrays ||
138 NULL == fFunctions.fDrawElements ||
139 NULL == fFunctions.fEnable ||
140 NULL == fFunctions.fEnableVertexAttribArray ||
141 NULL == fFunctions.fFrontFace ||
142 NULL == fFunctions.fGenBuffers ||
143 NULL == fFunctions.fGenTextures ||
144 NULL == fFunctions.fGetBufferParameteriv ||
145 NULL == fFunctions.fGenerateMipmap ||
146 NULL == fFunctions.fGetError ||
147 NULL == fFunctions.fGetIntegerv ||
148 NULL == fFunctions.fGetProgramInfoLog ||
149 NULL == fFunctions.fGetProgramiv ||
150 NULL == fFunctions.fGetShaderInfoLog ||
151 NULL == fFunctions.fGetShaderiv ||
152 NULL == fFunctions.fGetString ||
153 NULL == fFunctions.fGetUniformLocation ||
154 NULL == fFunctions.fLinkProgram ||
155 NULL == fFunctions.fLineWidth ||
156 NULL == fFunctions.fPixelStorei ||
157 NULL == fFunctions.fReadPixels ||
158 NULL == fFunctions.fScissor ||
159 NULL == fFunctions.fShaderSource ||
160 NULL == fFunctions.fStencilFunc ||
161 NULL == fFunctions.fStencilMask ||
162 NULL == fFunctions.fStencilOp ||
163 NULL == fFunctions.fTexImage2D ||
164 NULL == fFunctions.fTexParameteri ||
165 NULL == fFunctions.fTexParameteriv ||
166 NULL == fFunctions.fTexSubImage2D ||
167 NULL == fFunctions.fUniform1f ||
168 NULL == fFunctions.fUniform1i ||
169 NULL == fFunctions.fUniform1fv ||
170 NULL == fFunctions.fUniform1iv ||
171 NULL == fFunctions.fUniform2f ||
172 NULL == fFunctions.fUniform2i ||
173 NULL == fFunctions.fUniform2fv ||
174 NULL == fFunctions.fUniform2iv ||
175 NULL == fFunctions.fUniform3f ||
176 NULL == fFunctions.fUniform3i ||
177 NULL == fFunctions.fUniform3fv ||
178 NULL == fFunctions.fUniform3iv ||
179 NULL == fFunctions.fUniform4f ||
180 NULL == fFunctions.fUniform4i ||
181 NULL == fFunctions.fUniform4fv ||
182 NULL == fFunctions.fUniform4iv ||
183 NULL == fFunctions.fUniformMatrix2fv ||
184 NULL == fFunctions.fUniformMatrix3fv ||
185 NULL == fFunctions.fUniformMatrix4fv ||
186 NULL == fFunctions.fUseProgram ||
188 NULL == fFunctions.fVertexAttrib1f ||
189 NULL == fFunctions.fVertexAttrib2fv ||
190 NULL == fFunctions.fVertexAttrib3fv ||
192 NULL == fFunctions.fVertexAttrib4fv ||
193 NULL == fFunctions.fVertexAttribPointer ||
194 NULL == fFunctions.fViewport ||
195 NULL == fFunctions.fBindFramebuffer ||
196 NULL == fFunctions.fBindRenderbuffer ||
197 NULL == fFunctions.fCheckFramebufferStatus ||
198 NULL == fFunctions.fDeleteFramebuffers ||
199 NULL == fFunctions.fDeleteRenderbuffers ||
200 NULL == fFunctions.fFinish ||
201 NULL == fFunctions.fFlush ||
202 NULL == fFunctions.fFramebufferRenderbuffer ||
203 NULL == fFunctions.fFramebufferTexture2D ||
204 NULL == fFunctions.fGetFramebufferAttachmentParameteriv ||
205 NULL == fFunctions.fGetRenderbufferParameteriv ||
206 NULL == fFunctions.fGenFramebuffers ||
207 NULL == fFunctions.fGenRenderbuffers ||
208 NULL == fFunctions.fRenderbufferStorage) {
209 RETURN_FALSE_INTERFACE
212 GrGLVersion glVer = GrGLGetVersion(this);
213 if (GR_GL_INVALID_VER == glVer) {
214 RETURN_FALSE_INTERFACE
217 // Now check that baseline ES/Desktop fns not covered above are present
218 // and that we have fn pointers for any advertised fExtensions that we will
221 // these functions are part of ES2, we assume they are available
222 // On the desktop we assume they are available if the extension
223 // is present or GL version is high enough.
224 if (kGLES_GrGLStandard == fStandard) {
225 if (NULL == fFunctions.fStencilFuncSeparate ||
226 NULL == fFunctions.fStencilMaskSeparate ||
227 NULL == fFunctions.fStencilOpSeparate) {
228 RETURN_FALSE_INTERFACE
230 } else if (kGL_GrGLStandard == fStandard) {
232 if (glVer >= GR_GL_VER(2,0)) {
233 if (NULL == fFunctions.fStencilFuncSeparate ||
234 NULL == fFunctions.fStencilMaskSeparate ||
235 NULL == fFunctions.fStencilOpSeparate) {
236 RETURN_FALSE_INTERFACE
239 if (glVer >= GR_GL_VER(3,0) && NULL == fFunctions.fBindFragDataLocation) {
240 RETURN_FALSE_INTERFACE
242 if (glVer >= GR_GL_VER(2,0) || fExtensions.has("GL_ARB_draw_buffers")) {
243 if (NULL == fFunctions.fDrawBuffers) {
244 RETURN_FALSE_INTERFACE
248 if (glVer >= GR_GL_VER(1,5) || fExtensions.has("GL_ARB_occlusion_query")) {
249 if (NULL == fFunctions.fGenQueries ||
250 NULL == fFunctions.fDeleteQueries ||
251 NULL == fFunctions.fBeginQuery ||
252 NULL == fFunctions.fEndQuery ||
253 NULL == fFunctions.fGetQueryiv ||
254 NULL == fFunctions.fGetQueryObjectiv ||
255 NULL == fFunctions.fGetQueryObjectuiv) {
256 RETURN_FALSE_INTERFACE
259 if (glVer >= GR_GL_VER(3,3) ||
260 fExtensions.has("GL_ARB_timer_query") ||
261 fExtensions.has("GL_EXT_timer_query")) {
262 if (NULL == fFunctions.fGetQueryObjecti64v ||
263 NULL == fFunctions.fGetQueryObjectui64v) {
264 RETURN_FALSE_INTERFACE
267 if (glVer >= GR_GL_VER(3,3) || fExtensions.has("GL_ARB_timer_query")) {
268 if (NULL == fFunctions.fQueryCounter) {
269 RETURN_FALSE_INTERFACE
274 // optional function on desktop before 1.3
275 if (kGL_GrGLStandard != fStandard ||
276 (glVer >= GR_GL_VER(1,3)) ||
277 fExtensions.has("GL_ARB_texture_compression")) {
278 if (NULL == fFunctions.fCompressedTexImage2D
280 || NULL == fFunctions.fCompressedTexSubImage2D
283 RETURN_FALSE_INTERFACE
287 // part of desktop GL, but not ES
288 if (kGL_GrGLStandard == fStandard &&
289 (NULL == fFunctions.fGetTexLevelParameteriv ||
290 NULL == fFunctions.fDrawBuffer ||
291 NULL == fFunctions.fReadBuffer)) {
292 RETURN_FALSE_INTERFACE
295 // GL_EXT_texture_storage is part of desktop 4.2
296 // There is a desktop ARB extension and an ES+desktop EXT extension
297 if (kGL_GrGLStandard == fStandard) {
298 if (glVer >= GR_GL_VER(4,2) ||
299 fExtensions.has("GL_ARB_texture_storage") ||
300 fExtensions.has("GL_EXT_texture_storage")) {
301 if (NULL == fFunctions.fTexStorage2D) {
302 RETURN_FALSE_INTERFACE
305 } else if (glVer >= GR_GL_VER(3,0) || fExtensions.has("GL_EXT_texture_storage")) {
306 if (NULL == fFunctions.fTexStorage2D) {
307 RETURN_FALSE_INTERFACE
311 if (fExtensions.has("GL_EXT_discard_framebuffer")) {
312 // FIXME: Remove this once Chromium is updated to provide this function
314 if (NULL == fFunctions.fDiscardFramebuffer) {
315 RETURN_FALSE_INTERFACE
321 if (kGL_GrGLStandard == fStandard) {
322 // GL 3.0 and the ARB extension have multisample + blit
323 if (glVer >= GR_GL_VER(3,0) || fExtensions.has("GL_ARB_framebuffer_object")) {
324 if (NULL == fFunctions.fRenderbufferStorageMultisample ||
325 NULL == fFunctions.fBlitFramebuffer) {
326 RETURN_FALSE_INTERFACE
329 if (fExtensions.has("GL_EXT_framebuffer_blit") &&
330 NULL == fFunctions.fBlitFramebuffer) {
331 RETURN_FALSE_INTERFACE
333 if (fExtensions.has("GL_EXT_framebuffer_multisample") &&
334 NULL == fFunctions.fRenderbufferStorageMultisample) {
335 RETURN_FALSE_INTERFACE
339 if (glVer >= GR_GL_VER(3,0) || fExtensions.has("GL_CHROMIUM_framebuffer_multisample")) {
340 if (NULL == fFunctions.fRenderbufferStorageMultisample ||
341 NULL == fFunctions.fBlitFramebuffer) {
342 RETURN_FALSE_INTERFACE
345 if (fExtensions.has("GL_APPLE_framebuffer_multisample")) {
346 if (NULL == fFunctions.fRenderbufferStorageMultisampleES2APPLE ||
347 NULL == fFunctions.fResolveMultisampleFramebuffer) {
348 RETURN_FALSE_INTERFACE
351 if (fExtensions.has("GL_IMG_multisampled_render_to_texture") ||
352 fExtensions.has("GL_EXT_multisampled_render_to_texture")) {
353 if (NULL == fFunctions.fRenderbufferStorageMultisampleES2EXT ||
354 NULL == fFunctions.fFramebufferTexture2DMultisample) {
355 RETURN_FALSE_INTERFACE
360 // On ES buffer mapping is an extension. On Desktop
361 // buffer mapping was part of original VBO extension
363 if (kGL_GrGLStandard == fStandard || fExtensions.has("GL_OES_mapbuffer")) {
364 if (NULL == fFunctions.fMapBuffer ||
365 NULL == fFunctions.fUnmapBuffer) {
366 RETURN_FALSE_INTERFACE
370 // Dual source blending
371 if (kGL_GrGLStandard == fStandard &&
372 (glVer >= GR_GL_VER(3,3) || fExtensions.has("GL_ARB_blend_func_extended"))) {
373 if (NULL == fFunctions.fBindFragDataLocationIndexed) {
374 RETURN_FALSE_INTERFACE
378 // glGetStringi was added in version 3.0 of both desktop and ES.
379 if (glVer >= GR_GL_VER(3, 0)) {
380 if (NULL == fFunctions.fGetStringi) {
381 RETURN_FALSE_INTERFACE
385 if (kGL_GrGLStandard == fStandard) {
386 if (glVer >= GR_GL_VER(3, 0) || fExtensions.has("GL_ARB_vertex_array_object")) {
387 if (NULL == fFunctions.fBindVertexArray ||
388 NULL == fFunctions.fDeleteVertexArrays ||
389 NULL == fFunctions.fGenVertexArrays) {
390 RETURN_FALSE_INTERFACE
394 if (glVer >= GR_GL_VER(3,0) || fExtensions.has("GL_OES_vertex_array_object")) {
395 if (NULL == fFunctions.fBindVertexArray ||
396 NULL == fFunctions.fDeleteVertexArrays ||
397 NULL == fFunctions.fGenVertexArrays) {
398 RETURN_FALSE_INTERFACE
403 if (fExtensions.has("GL_EXT_debug_marker")) {
404 if (NULL == fFunctions.fInsertEventMarker ||
405 NULL == fFunctions.fPushGroupMarker ||
406 NULL == fFunctions.fPopGroupMarker) {
407 RETURN_FALSE_INTERFACE
411 if ((kGL_GrGLStandard == fStandard && glVer >= GR_GL_VER(4,3)) ||
412 fExtensions.has("GL_ARB_invalidate_subdata")) {
413 if (NULL == fFunctions.fInvalidateBufferData ||
414 NULL == fFunctions.fInvalidateBufferSubData ||
415 NULL == fFunctions.fInvalidateFramebuffer ||
416 NULL == fFunctions.fInvalidateSubFramebuffer ||
417 NULL == fFunctions.fInvalidateTexImage ||
418 NULL == fFunctions.fInvalidateTexSubImage) {
419 RETURN_FALSE_INTERFACE;
421 } else if (kGLES_GrGLStandard == fStandard && glVer >= GR_GL_VER(3,0)) {
422 // ES 3.0 adds the framebuffer functions but not the others.
423 if (NULL == fFunctions.fInvalidateFramebuffer ||
424 NULL == fFunctions.fInvalidateSubFramebuffer) {
425 RETURN_FALSE_INTERFACE;
429 if (kGLES_GrGLStandard == fStandard && fExtensions.has("GL_CHROMIUM_map_sub")) {
430 if (NULL == fFunctions.fMapBufferSubData ||
431 NULL == fFunctions.fMapTexSubImage2D ||
432 NULL == fFunctions.fUnmapBufferSubData ||
433 NULL == fFunctions.fUnmapTexSubImage2D) {
434 RETURN_FALSE_INTERFACE;
438 // These functions are added to the 3.0 version of both GLES and GL.
439 if (glVer >= GR_GL_VER(3,0) ||
440 (kGLES_GrGLStandard == fStandard && fExtensions.has("GL_EXT_map_buffer_range")) ||
441 (kGL_GrGLStandard == fStandard && fExtensions.has("GL_ARB_map_buffer_range"))) {
442 if (NULL == fFunctions.fMapBufferRange ||
443 NULL == fFunctions.fFlushMappedBufferRange) {
444 RETURN_FALSE_INTERFACE;
448 if ((kGL_GrGLStandard == fStandard && fExtensions.has("GL_EXT_direct_state_access")) ||
449 (kGLES_GrGLStandard == fStandard && fExtensions.has("GL_NV_path_rendering"))) {
450 if (NULL == fFunctions.fMatrixLoadf ||
451 NULL == fFunctions.fMatrixLoadIdentity) {
452 RETURN_FALSE_INTERFACE
456 if ((kGL_GrGLStandard == fStandard &&
457 (glVer >= GR_GL_VER(4,3) || fExtensions.has("GL_ARB_program_interface_query"))) ||
458 (kGLES_GrGLStandard == fStandard && glVer >= GR_GL_VER(3,1))) {
459 if (NULL == fFunctions.fGetProgramResourceLocation) {
460 RETURN_FALSE_INTERFACE
464 if (fExtensions.has("GL_NV_path_rendering")) {
465 if (NULL == fFunctions.fPathCommands ||
466 NULL == fFunctions.fPathCoords ||
467 NULL == fFunctions.fPathParameteri ||
468 NULL == fFunctions.fPathParameterf ||
469 NULL == fFunctions.fGenPaths ||
470 NULL == fFunctions.fDeletePaths ||
471 NULL == fFunctions.fIsPath ||
472 NULL == fFunctions.fPathStencilFunc ||
473 NULL == fFunctions.fStencilFillPath ||
474 NULL == fFunctions.fStencilStrokePath ||
475 NULL == fFunctions.fStencilFillPathInstanced ||
476 NULL == fFunctions.fStencilStrokePathInstanced ||
477 NULL == fFunctions.fCoverFillPath ||
478 NULL == fFunctions.fCoverStrokePath ||
479 NULL == fFunctions.fCoverFillPathInstanced ||
480 NULL == fFunctions.fCoverStrokePathInstanced) {
481 RETURN_FALSE_INTERFACE
483 if (kGL_GrGLStandard == fStandard) {
484 // Some methods only exist on desktop
485 if (NULL == fFunctions.fPathTexGen) {
486 RETURN_FALSE_INTERFACE
489 // All additions through v1.3 exist on GLES
490 if (NULL == fFunctions.fStencilThenCoverFillPath ||
491 NULL == fFunctions.fStencilThenCoverStrokePath ||
492 NULL == fFunctions.fStencilThenCoverFillPathInstanced ||
493 NULL == fFunctions.fStencilThenCoverStrokePathInstanced ||
494 NULL == fFunctions.fProgramPathFragmentInputGen ||
495 NULL == fFunctions.fPathMemoryGlyphIndexArray) {
496 RETURN_FALSE_INTERFACE