Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / gpu / gl / GrGLCaps.cpp
1 /*
2  * Copyright 2012 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.
6  */
7
8
9 #include "GrGLCaps.h"
10
11 #include "GrGLContext.h"
12 #include "SkTSearch.h"
13 #include "SkTSort.h"
14
15 GrGLCaps::GrGLCaps() {
16     this->reset();
17 }
18
19 void GrGLCaps::reset() {
20     INHERITED::reset();
21
22     fVerifiedColorConfigs.reset();
23     fStencilFormats.reset();
24     fStencilVerifiedColorConfigs.reset();
25     fMSFBOType = kNone_MSFBOType;
26     fInvalidateFBType = kNone_InvalidateFBType;
27     fLATCAlias = kLATC_LATCAlias;
28     fMapBufferType = kNone_MapBufferType;
29     fMaxFragmentUniformVectors = 0;
30     fMaxVertexAttributes = 0;
31     fMaxFragmentTextureUnits = 0;
32     fMaxFixedFunctionTextureCoords = 0;
33     fRGBA8RenderbufferSupport = false;
34     fBGRAIsInternalFormat = false;
35     fTextureSwizzleSupport = false;
36     fUnpackRowLengthSupport = false;
37     fUnpackFlipYSupport = false;
38     fPackRowLengthSupport = false;
39     fPackFlipYSupport = false;
40     fTextureUsageSupport = false;
41     fTexStorageSupport = false;
42     fTextureRedSupport = false;
43     fImagingSupport = false;
44     fTwoFormatLimit = false;
45     fFragCoordsConventionSupport = false;
46     fVertexArrayObjectSupport = false;
47     fUseNonVBOVertexAndIndexDynamicData = false;
48     fIsCoreProfile = false;
49     fFullClearIsFree = false;
50     fDropsTileOnZeroDivide = false;
51     fFBFetchSupport = false;
52     fFBFetchColorName = NULL;
53     fFBFetchExtensionString = NULL;
54
55     fReadPixelsSupportedCache.reset();
56 }
57
58 GrGLCaps::GrGLCaps(const GrGLCaps& caps) : GrDrawTargetCaps() {
59     *this = caps;
60 }
61
62 GrGLCaps& GrGLCaps::operator= (const GrGLCaps& caps) {
63     INHERITED::operator=(caps);
64     fVerifiedColorConfigs = caps.fVerifiedColorConfigs;
65     fStencilFormats = caps.fStencilFormats;
66     fStencilVerifiedColorConfigs = caps.fStencilVerifiedColorConfigs;
67     fLATCAlias = caps.fLATCAlias;
68     fMaxFragmentUniformVectors = caps.fMaxFragmentUniformVectors;
69     fMaxVertexAttributes = caps.fMaxVertexAttributes;
70     fMaxFragmentTextureUnits = caps.fMaxFragmentTextureUnits;
71     fMaxFixedFunctionTextureCoords = caps.fMaxFixedFunctionTextureCoords;
72     fMSFBOType = caps.fMSFBOType;
73     fInvalidateFBType = caps.fInvalidateFBType;
74     fMapBufferType = caps.fMapBufferType;
75     fRGBA8RenderbufferSupport = caps.fRGBA8RenderbufferSupport;
76     fBGRAIsInternalFormat = caps.fBGRAIsInternalFormat;
77     fTextureSwizzleSupport = caps.fTextureSwizzleSupport;
78     fUnpackRowLengthSupport = caps.fUnpackRowLengthSupport;
79     fUnpackFlipYSupport = caps.fUnpackFlipYSupport;
80     fPackRowLengthSupport = caps.fPackRowLengthSupport;
81     fPackFlipYSupport = caps.fPackFlipYSupport;
82     fTextureUsageSupport = caps.fTextureUsageSupport;
83     fTexStorageSupport = caps.fTexStorageSupport;
84     fTextureRedSupport = caps.fTextureRedSupport;
85     fImagingSupport = caps.fImagingSupport;
86     fTwoFormatLimit = caps.fTwoFormatLimit;
87     fFragCoordsConventionSupport = caps.fFragCoordsConventionSupport;
88     fVertexArrayObjectSupport = caps.fVertexArrayObjectSupport;
89     fUseNonVBOVertexAndIndexDynamicData = caps.fUseNonVBOVertexAndIndexDynamicData;
90     fIsCoreProfile = caps.fIsCoreProfile;
91     fFullClearIsFree = caps.fFullClearIsFree;
92     fDropsTileOnZeroDivide = caps.fDropsTileOnZeroDivide;
93     fFBFetchSupport = caps.fFBFetchSupport;
94     fFBFetchColorName = caps.fFBFetchColorName;
95     fFBFetchExtensionString = caps.fFBFetchExtensionString;
96
97     return *this;
98 }
99
100 bool GrGLCaps::init(const GrGLContextInfo& ctxInfo, const GrGLInterface* gli) {
101
102     this->reset();
103     if (!ctxInfo.isInitialized()) {
104         return false;
105     }
106
107     GrGLStandard standard = ctxInfo.standard();
108     GrGLVersion version = ctxInfo.version();
109
110     /**************************************************************************
111      * Caps specific to GrGLCaps
112      **************************************************************************/
113
114     if (kGLES_GrGLStandard == standard) {
115         GR_GL_GetIntegerv(gli, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
116                           &fMaxFragmentUniformVectors);
117     } else {
118         SkASSERT(kGL_GrGLStandard == standard);
119         GrGLint max;
120         GR_GL_GetIntegerv(gli, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
121         fMaxFragmentUniformVectors = max / 4;
122         if (version >= GR_GL_VER(3, 2)) {
123             GrGLint profileMask;
124             GR_GL_GetIntegerv(gli, GR_GL_CONTEXT_PROFILE_MASK, &profileMask);
125             fIsCoreProfile = SkToBool(profileMask & GR_GL_CONTEXT_CORE_PROFILE_BIT);
126         }
127         if (!fIsCoreProfile) {
128             GR_GL_GetIntegerv(gli, GR_GL_MAX_TEXTURE_COORDS, &fMaxFixedFunctionTextureCoords);
129             // Sanity check
130             SkASSERT(fMaxFixedFunctionTextureCoords > 0 && fMaxFixedFunctionTextureCoords < 128);
131         }
132     }
133     GR_GL_GetIntegerv(gli, GR_GL_MAX_VERTEX_ATTRIBS, &fMaxVertexAttributes);
134     GR_GL_GetIntegerv(gli, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &fMaxFragmentTextureUnits);
135
136     if (kGL_GrGLStandard == standard) {
137         fRGBA8RenderbufferSupport = true;
138     } else {
139         fRGBA8RenderbufferSupport = version >= GR_GL_VER(3,0) ||
140                                     ctxInfo.hasExtension("GL_OES_rgb8_rgba8") ||
141                                     ctxInfo.hasExtension("GL_ARM_rgba8");
142     }
143
144     if (kGL_GrGLStandard == standard) {
145         fTextureSwizzleSupport = version >= GR_GL_VER(3,3) ||
146                                  ctxInfo.hasExtension("GL_ARB_texture_swizzle");
147     } else {
148         fTextureSwizzleSupport = version >= GR_GL_VER(3,0);
149     }
150
151     if (kGL_GrGLStandard == standard) {
152         fUnpackRowLengthSupport = true;
153         fUnpackFlipYSupport = false;
154         fPackRowLengthSupport = true;
155         fPackFlipYSupport = false;
156     } else {
157         fUnpackRowLengthSupport = version >= GR_GL_VER(3,0) ||
158                                   ctxInfo.hasExtension("GL_EXT_unpack_subimage");
159         fUnpackFlipYSupport = ctxInfo.hasExtension("GL_CHROMIUM_flipy");
160         fPackRowLengthSupport = version >= GR_GL_VER(3,0) ||
161                                 ctxInfo.hasExtension("GL_NV_pack_subimage");
162         fPackFlipYSupport =
163             ctxInfo.hasExtension("GL_ANGLE_pack_reverse_row_order");
164     }
165
166     fTextureUsageSupport = (kGLES_GrGLStandard == standard) &&
167                             ctxInfo.hasExtension("GL_ANGLE_texture_usage");
168
169     if (kGL_GrGLStandard == standard) {
170         // The EXT version can apply to either GL or GLES.
171         fTexStorageSupport = version >= GR_GL_VER(4,2) ||
172                              ctxInfo.hasExtension("GL_ARB_texture_storage") ||
173                              ctxInfo.hasExtension("GL_EXT_texture_storage");
174     } else {
175         // Qualcomm Adreno drivers appear to have issues with texture storage.
176         fTexStorageSupport = (version >= GR_GL_VER(3,0) &&
177                               kQualcomm_GrGLVendor != ctxInfo.vendor()) ||
178                              ctxInfo.hasExtension("GL_EXT_texture_storage");
179     }
180
181     // ARB_texture_rg is part of OpenGL 3.0, but mesa doesn't support it if
182     // it doesn't have ARB_texture_rg extension.
183     if (kGL_GrGLStandard == standard) {
184         if (ctxInfo.isMesa()) {
185             fTextureRedSupport = ctxInfo.hasExtension("GL_ARB_texture_rg");
186         } else {
187             fTextureRedSupport = version >= GR_GL_VER(3,0) ||
188                                  ctxInfo.hasExtension("GL_ARB_texture_rg");
189         }
190     } else {
191         fTextureRedSupport =  version >= GR_GL_VER(3,0) ||
192                               ctxInfo.hasExtension("GL_EXT_texture_rg");
193     }
194
195     fImagingSupport = kGL_GrGLStandard == standard &&
196                       ctxInfo.hasExtension("GL_ARB_imaging");
197
198     // ES 2 only guarantees RGBA/uchar + one other format/type combo for
199     // ReadPixels. The other format has to checked at run-time since it
200     // can change based on which render target is bound
201     fTwoFormatLimit = kGLES_GrGLStandard == standard;
202
203     // Frag Coords Convention support is not part of ES
204     // Known issue on at least some Intel platforms:
205     // http://code.google.com/p/skia/issues/detail?id=946
206     if (kIntel_GrGLVendor != ctxInfo.vendor() && kGLES_GrGLStandard != standard) {
207         fFragCoordsConventionSupport = ctxInfo.glslGeneration() >= k150_GrGLSLGeneration ||
208                                        ctxInfo.hasExtension("GL_ARB_fragment_coord_conventions");
209     }
210
211     // SGX and Mali GPUs that are based on a tiled-deferred architecture that have trouble with
212     // frequently changing VBOs. We've measured a performance increase using non-VBO vertex
213     // data for dynamic content on these GPUs. Perhaps we should read the renderer string and
214     // limit this decision to specific GPU families rather than basing it on the vendor alone.
215     if (!GR_GL_MUST_USE_VBO &&
216         (kARM_GrGLVendor == ctxInfo.vendor() ||
217          kImagination_GrGLVendor == ctxInfo.vendor() ||
218          kQualcomm_GrGLVendor == ctxInfo.vendor())) {
219         fUseNonVBOVertexAndIndexDynamicData = true;
220     }
221
222     if ((kGL_GrGLStandard == standard && version >= GR_GL_VER(4,3)) ||
223         (kGLES_GrGLStandard == standard && version >= GR_GL_VER(3,0)) ||
224         ctxInfo.hasExtension("GL_ARB_invalidate_subdata")) {
225         fDiscardRenderTargetSupport = true;
226         fInvalidateFBType = kInvalidate_InvalidateFBType;
227     } else if (ctxInfo.hasExtension("GL_EXT_discard_framebuffer")) {
228         fDiscardRenderTargetSupport = true;
229         fInvalidateFBType = kDiscard_InvalidateFBType;
230     }
231
232     if (kARM_GrGLVendor == ctxInfo.vendor() || kImagination_GrGLVendor == ctxInfo.vendor()) {
233         fFullClearIsFree = true;
234     }
235
236     if (kGL_GrGLStandard == standard) {
237         fVertexArrayObjectSupport = version >= GR_GL_VER(3, 0) ||
238                                     ctxInfo.hasExtension("GL_ARB_vertex_array_object");
239     } else {
240         fVertexArrayObjectSupport = version >= GR_GL_VER(3, 0) ||
241                                     ctxInfo.hasExtension("GL_OES_vertex_array_object");
242     }
243
244     if (kGLES_GrGLStandard == standard) {
245         if (ctxInfo.hasExtension("GL_EXT_shader_framebuffer_fetch")) {
246             fFBFetchSupport = true;
247             fFBFetchColorName = "gl_LastFragData[0]";
248             fFBFetchExtensionString = "GL_EXT_shader_framebuffer_fetch";
249         } else if (ctxInfo.hasExtension("GL_NV_shader_framebuffer_fetch")) {
250             fFBFetchSupport = true;
251             fFBFetchColorName = "gl_LastFragData[0]";
252             fFBFetchExtensionString = "GL_NV_shader_framebuffer_fetch";
253         } else if (ctxInfo.hasExtension("GL_ARM_shader_framebuffer_fetch")) {
254             // The arm extension also requires an additional flag which we will set onResetContext
255             // This is all temporary.
256             fFBFetchSupport = true;
257             fFBFetchColorName = "gl_LastFragColorARM";
258             fFBFetchExtensionString = "GL_ARM_shader_framebuffer_fetch";
259         }
260     }
261
262     // Adreno GPUs have a tendency to drop tiles when there is a divide-by-zero in a shader
263     fDropsTileOnZeroDivide = kQualcomm_GrGLVendor == ctxInfo.vendor();
264
265     this->initFSAASupport(ctxInfo, gli);
266     this->initStencilFormats(ctxInfo);
267
268     /**************************************************************************
269      * GrDrawTargetCaps fields
270      **************************************************************************/
271     if (kGL_GrGLStandard == standard) {
272         // we could also look for GL_ATI_separate_stencil extension or
273         // GL_EXT_stencil_two_side but they use different function signatures
274         // than GL2.0+ (and than each other).
275         fTwoSidedStencilSupport = (ctxInfo.version() >= GR_GL_VER(2,0));
276         // supported on GL 1.4 and higher or by extension
277         fStencilWrapOpsSupport = (ctxInfo.version() >= GR_GL_VER(1,4)) ||
278                                   ctxInfo.hasExtension("GL_EXT_stencil_wrap");
279     } else {
280         // ES 2 has two sided stencil and stencil wrap
281         fTwoSidedStencilSupport = true;
282         fStencilWrapOpsSupport = true;
283     }
284
285     if (kGL_GrGLStandard == standard) {
286         fMapBufferFlags = kCanMap_MapFlag; // we require VBO support and the desktop VBO
287                                             // extension includes glMapBuffer.
288         if (version >= GR_GL_VER(3, 0) || ctxInfo.hasExtension("GL_ARB_map_buffer_range")) {
289             fMapBufferFlags |= kSubset_MapFlag;
290             fMapBufferType = kMapBufferRange_MapBufferType;
291         } else {
292             fMapBufferType = kMapBuffer_MapBufferType;
293         }
294     } else {
295         // Unextended GLES2 doesn't have any buffer mapping.
296         fMapBufferFlags = kNone_MapBufferType;
297         if (ctxInfo.hasExtension("GL_CHROMIUM_map_sub")) {
298             fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
299             fMapBufferType = kChromium_MapBufferType;
300         } else if (version >= GR_GL_VER(3, 0) || ctxInfo.hasExtension("GL_EXT_map_buffer_range")) {
301             fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
302             fMapBufferType = kMapBufferRange_MapBufferType;
303         } else if (ctxInfo.hasExtension("GL_OES_mapbuffer")) {
304             fMapBufferFlags = kCanMap_MapFlag;
305             fMapBufferType = kMapBuffer_MapBufferType;
306         }
307     }
308
309     if (kGL_GrGLStandard == standard) {
310         SkASSERT(ctxInfo.version() >= GR_GL_VER(2,0) ||
311                  ctxInfo.hasExtension("GL_ARB_texture_non_power_of_two"));
312         fNPOTTextureTileSupport = true;
313         fMipMapSupport = true;
314     } else {
315         // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
316         // ES3 has no limitations.
317         fNPOTTextureTileSupport = ctxInfo.version() >= GR_GL_VER(3,0) ||
318                                   ctxInfo.hasExtension("GL_OES_texture_npot");
319         // ES2 supports MIP mapping for POT textures but our caps don't allow for limited MIP
320         // support. The OES extension or ES 3.0 allow for MIPS on NPOT textures. So, apparently,
321         // does the undocumented GL_IMG_texture_npot extension. This extension does not seem to
322         // to alllow arbitrary wrap modes, however.
323         fMipMapSupport = fNPOTTextureTileSupport || ctxInfo.hasExtension("GL_IMG_texture_npot");
324     }
325
326     fHWAALineSupport = (kGL_GrGLStandard == standard);
327
328     GR_GL_GetIntegerv(gli, GR_GL_MAX_TEXTURE_SIZE, &fMaxTextureSize);
329     GR_GL_GetIntegerv(gli, GR_GL_MAX_RENDERBUFFER_SIZE, &fMaxRenderTargetSize);
330     // Our render targets are always created with textures as the color
331     // attachment, hence this min:
332     fMaxRenderTargetSize = SkTMin(fMaxTextureSize, fMaxRenderTargetSize);
333
334     fPathRenderingSupport = ctxInfo.hasExtension("GL_NV_path_rendering");
335
336     if (fPathRenderingSupport) {
337         if (kGL_GrGLStandard == standard) {
338             // We need one of the two possible texturing methods: using fixed function pipeline
339             // (PathTexGen, texcoords, ...)  or using the newer NVPR API additions that support
340             // setting individual fragment inputs with ProgramPathFragmentInputGen. The API
341             // additions are detected by checking the existence of the function. Eventually we may
342             // choose to remove the fixed function codepath.
343             // Set fMaxFixedFunctionTextureCoords = 0 here if you want to force
344             // ProgramPathFragmentInputGen usage on desktop.
345             fPathRenderingSupport = ctxInfo.hasExtension("GL_EXT_direct_state_access") &&
346                 (fMaxFixedFunctionTextureCoords > 0 ||
347                  ((ctxInfo.version() >= GR_GL_VER(4,3) ||
348                    ctxInfo.hasExtension("GL_ARB_program_interface_query")) &&
349                   gli->fFunctions.fProgramPathFragmentInputGen));
350         } else {
351             fPathRenderingSupport = ctxInfo.version() >= GR_GL_VER(3,1);
352         }
353     }
354
355     fGpuTracingSupport = ctxInfo.hasExtension("GL_EXT_debug_marker");
356
357     // For now these two are equivalent but we could have dst read in shader via some other method
358     fDstReadInShaderSupport = fFBFetchSupport;
359
360     // Disable scratch texture reuse on Mali and Adreno devices
361     fReuseScratchTextures = kARM_GrGLVendor != ctxInfo.vendor() &&
362                             kQualcomm_GrGLVendor != ctxInfo.vendor();
363
364     // Enable supported shader-related caps
365     if (kGL_GrGLStandard == standard) {
366         fDualSourceBlendingSupport = ctxInfo.version() >= GR_GL_VER(3,3) ||
367                                      ctxInfo.hasExtension("GL_ARB_blend_func_extended");
368         fShaderDerivativeSupport = true;
369         // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
370         fGeometryShaderSupport = ctxInfo.version() >= GR_GL_VER(3,2) &&
371                                  ctxInfo.glslGeneration() >= k150_GrGLSLGeneration;
372     } else {
373         fShaderDerivativeSupport = ctxInfo.version() >= GR_GL_VER(3, 0) ||
374                                    ctxInfo.hasExtension("GL_OES_standard_derivatives");
375     }
376
377     if (GrGLCaps::kES_IMG_MsToTexture_MSFBOType == fMSFBOType) {
378         GR_GL_GetIntegerv(gli, GR_GL_MAX_SAMPLES_IMG, &fMaxSampleCount);
379     } else if (GrGLCaps::kNone_MSFBOType != fMSFBOType) {
380         GR_GL_GetIntegerv(gli, GR_GL_MAX_SAMPLES, &fMaxSampleCount);
381     }
382
383     if (kPowerVR54x_GrGLRenderer == ctxInfo.renderer() ||
384         kPowerVRRogue_GrGLRenderer == ctxInfo.renderer()) {
385         fUseDrawInsteadOfClear = true;
386     }
387
388     this->initConfigTexturableTable(ctxInfo, gli);
389     this->initConfigRenderableTable(ctxInfo);
390
391     return true;
392 }
393
394 void GrGLCaps::initConfigRenderableTable(const GrGLContextInfo& ctxInfo) {
395
396     // OpenGL < 3.0
397     //  no support for render targets unless the GL_ARB_framebuffer_object
398     //  extension is supported (in which case we get ALPHA, RED, RG, RGB,
399     //  RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
400     //  probably don't get R8 in this case.
401
402     // OpenGL 3.0
403     //  base color renderable: ALPHA, RED, RG, RGB, and RGBA
404     //  sized derivatives: ALPHA8, R8, RGBA4, RGBA8
405
406     // >= OpenGL 3.1
407     //  base color renderable: RED, RG, RGB, and RGBA
408     //  sized derivatives: R8, RGBA4, RGBA8
409     //  if the GL_ARB_compatibility extension is supported then we get back
410     //  support for GL_ALPHA and ALPHA8
411
412     // GL_EXT_bgra adds BGRA render targets to any version
413
414     // ES 2.0
415     //  color renderable: RGBA4, RGB5_A1, RGB565
416     //  GL_EXT_texture_rg adds support for R8 as a color render target
417     //  GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
418     //  GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888 added BGRA support
419
420     // ES 3.0
421     // Same as ES 2.0 except R8 and RGBA8 are supported without extensions (the functions called
422     // below already account for this).
423
424     GrGLStandard standard = ctxInfo.standard();
425
426     enum {
427         kNo_MSAA = 0,
428         kYes_MSAA = 1,
429     };
430
431     if (kGL_GrGLStandard == standard) {
432         // Post 3.0 we will get R8
433         // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
434         if (ctxInfo.version() >= GR_GL_VER(3,0) ||
435             ctxInfo.hasExtension("GL_ARB_framebuffer_object")) {
436             fConfigRenderSupport[kAlpha_8_GrPixelConfig][kNo_MSAA] = true;
437             fConfigRenderSupport[kAlpha_8_GrPixelConfig][kYes_MSAA] = true;
438         }
439     } else {
440         // On ES we can only hope for R8
441         fConfigRenderSupport[kAlpha_8_GrPixelConfig][kNo_MSAA] = fTextureRedSupport;
442         fConfigRenderSupport[kAlpha_8_GrPixelConfig][kYes_MSAA] = fTextureRedSupport;
443     }
444
445     if (kGL_GrGLStandard != standard) {
446         // only available in ES
447         fConfigRenderSupport[kRGB_565_GrPixelConfig][kNo_MSAA] = true;
448         fConfigRenderSupport[kRGB_565_GrPixelConfig][kYes_MSAA] = true;
449     }
450
451     // we no longer support 444 as a render target
452     fConfigRenderSupport[kRGBA_4444_GrPixelConfig][kNo_MSAA]  = false;
453     fConfigRenderSupport[kRGBA_4444_GrPixelConfig][kYes_MSAA]  = false;
454
455     if (this->fRGBA8RenderbufferSupport) {
456         fConfigRenderSupport[kRGBA_8888_GrPixelConfig][kNo_MSAA]  = true;
457         fConfigRenderSupport[kRGBA_8888_GrPixelConfig][kYes_MSAA]  = true;
458     }
459
460     if (this->isConfigTexturable(kBGRA_8888_GrPixelConfig)) {
461         fConfigRenderSupport[kBGRA_8888_GrPixelConfig][kNo_MSAA]  = true;
462         // The GL_EXT_texture_format_BGRA8888 extension does not add BGRA to the list of
463         // configs that are color-renderable and can be passed to glRenderBufferStorageMultisample.
464         // Chromium may have an extension to allow BGRA renderbuffers to work on desktop platforms.
465         if (ctxInfo.hasExtension("GL_CHROMIUM_renderbuffer_format_BGRA8888")) {
466             fConfigRenderSupport[kBGRA_8888_GrPixelConfig][kYes_MSAA] = true;
467         } else {
468             fConfigRenderSupport[kBGRA_8888_GrPixelConfig][kYes_MSAA] =
469                 !fBGRAIsInternalFormat || !this->usesMSAARenderBuffers();
470         }
471     }
472
473     if (this->isConfigTexturable(kRGBA_float_GrPixelConfig)) {
474         fConfigRenderSupport[kRGBA_float_GrPixelConfig][kNo_MSAA] = true;
475     }
476
477     // If we don't support MSAA then undo any places above where we set a config as renderable with
478     // msaa.
479     if (kNone_MSFBOType == fMSFBOType) {
480         for (int i = 0; i < kGrPixelConfigCnt; ++i) {
481             fConfigRenderSupport[i][kYes_MSAA] = false;
482         }
483     }
484 }
485
486 void GrGLCaps::initConfigTexturableTable(const GrGLContextInfo& ctxInfo, const GrGLInterface* gli) {
487     GrGLStandard standard = ctxInfo.standard();
488     GrGLVersion version = ctxInfo.version();
489
490     // Base texture support
491     fConfigTextureSupport[kAlpha_8_GrPixelConfig] = true;
492     fConfigTextureSupport[kRGB_565_GrPixelConfig] = true;
493     fConfigTextureSupport[kRGBA_4444_GrPixelConfig] = true;
494     fConfigTextureSupport[kRGBA_8888_GrPixelConfig] = true;
495
496     // Check for 8-bit palette..
497     GrGLint numFormats;
498     GR_GL_GetIntegerv(gli, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
499     if (numFormats) {
500         SkAutoSTMalloc<10, GrGLint> formats(numFormats);
501         GR_GL_GetIntegerv(gli, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
502         for (int i = 0; i < numFormats; ++i) {
503             if (GR_GL_PALETTE8_RGBA8 == formats[i]) {
504                 fConfigTextureSupport[kIndex_8_GrPixelConfig] = true;
505                 break;
506             }
507         }
508     }
509
510     // Check for BGRA
511     if (kGL_GrGLStandard == standard) {
512         fConfigTextureSupport[kBGRA_8888_GrPixelConfig] =
513             version >= GR_GL_VER(1,2) || ctxInfo.hasExtension("GL_EXT_bgra");
514     } else {
515         if (ctxInfo.hasExtension("GL_APPLE_texture_format_BGRA8888")) {
516             fConfigTextureSupport[kBGRA_8888_GrPixelConfig] = true;
517         } else if (ctxInfo.hasExtension("GL_EXT_texture_format_BGRA8888")) {
518             fConfigTextureSupport[kBGRA_8888_GrPixelConfig] = true;
519             fBGRAIsInternalFormat = true;
520         }
521         SkASSERT(fConfigTextureSupport[kBGRA_8888_GrPixelConfig] ||
522                  kSkia8888_GrPixelConfig != kBGRA_8888_GrPixelConfig);
523     }
524
525     // Compressed texture support
526
527     // glCompressedTexImage2D is available on all OpenGL ES devices...
528     // however, it is only available on standard OpenGL after version 1.3
529     bool hasCompressTex2D = (kGL_GrGLStandard != standard || version >= GR_GL_VER(1, 3));
530
531     fCompressedTexSubImageSupport =
532         hasCompressTex2D && (gli->fFunctions.fCompressedTexSubImage2D);
533
534     // Check for ETC1
535     bool hasETC1 = false;
536
537     // First check version for support
538     if (kGL_GrGLStandard == standard) {
539         hasETC1 = hasCompressTex2D &&
540             (version >= GR_GL_VER(4, 3) ||
541              ctxInfo.hasExtension("GL_ARB_ES3_compatibility"));
542     } else {
543         hasETC1 = hasCompressTex2D &&
544             (version >= GR_GL_VER(3, 0) ||
545              ctxInfo.hasExtension("GL_OES_compressed_ETC1_RGB8_texture") ||
546              // ETC2 is a superset of ETC1, so we can just check for that, too.
547              (ctxInfo.hasExtension("GL_OES_compressed_ETC2_RGB8_texture") &&
548               ctxInfo.hasExtension("GL_OES_compressed_ETC2_RGBA8_texture")));
549     }
550     fConfigTextureSupport[kETC1_GrPixelConfig] = hasETC1;
551
552     // Check for LATC under its various forms
553     LATCAlias alias = kLATC_LATCAlias;
554     bool hasLATC = hasCompressTex2D &&
555         (ctxInfo.hasExtension("GL_EXT_texture_compression_latc") ||
556          ctxInfo.hasExtension("GL_NV_texture_compression_latc"));
557
558     // Check for RGTC
559     if (!hasLATC) {
560         // If we're using OpenGL 3.0 or later, then we have RGTC, an identical compression format.
561         if (kGL_GrGLStandard == standard) {
562             hasLATC = version >= GR_GL_VER(3, 0);
563         }
564
565         if (!hasLATC) {
566             hasLATC =
567                 ctxInfo.hasExtension("GL_EXT_texture_compression_rgtc") ||
568                 ctxInfo.hasExtension("GL_ARB_texture_compression_rgtc");
569         }
570
571         if (hasLATC) {
572             alias = kRGTC_LATCAlias;
573         }
574     }
575
576     // Check for 3DC
577     if (!hasLATC) {
578         hasLATC = ctxInfo.hasExtension("GL_AMD_compressed_3DC_texture");
579         if (hasLATC) {
580             alias = k3DC_LATCAlias;
581         }
582     }
583
584     fConfigTextureSupport[kLATC_GrPixelConfig] = hasLATC;
585     fLATCAlias = alias;
586
587     // Check for R11_EAC ... We don't support R11_EAC on desktop, as most
588     // cards default to decompressing the textures in the driver, and is
589     // generally slower.
590     if (kGL_GrGLStandard != standard) {
591         fConfigTextureSupport[kR11_EAC_GrPixelConfig] = version >= GR_GL_VER(3, 0);
592     }
593
594     // Check for ASTC
595     fConfigTextureSupport[kASTC_12x12_GrPixelConfig] =
596         ctxInfo.hasExtension("GL_KHR_texture_compression_astc_hdr") ||
597         ctxInfo.hasExtension("GL_KHR_texture_compression_astc_ldr") ||
598         ctxInfo.hasExtension("GL_OES_texture_compression_astc");
599
600     // Check for floating point texture support
601     // NOTE: We disallow floating point textures on ES devices if linear
602     // filtering modes are not supported.  This is for simplicity, but a more
603     // granular approach is possible.  Coincidentally, floating point textures became part of
604     // the standard in ES3.1 / OGL 3.1, hence the shorthand
605     bool hasFPTextures = version >= GR_GL_VER(3, 1);
606     if (!hasFPTextures) {
607         hasFPTextures = ctxInfo.hasExtension("GL_ARB_texture_float") ||
608                         (ctxInfo.hasExtension("OES_texture_float_linear") &&
609                          ctxInfo.hasExtension("GL_OES_texture_float"));
610     }
611     fConfigTextureSupport[kRGBA_float_GrPixelConfig] = hasFPTextures;
612 }
613
614 bool GrGLCaps::doReadPixelsSupported(const GrGLInterface* intf,
615                                      GrGLenum format,
616                                      GrGLenum type) const {
617     if (GR_GL_RGBA == format && GR_GL_UNSIGNED_BYTE == type) {
618         // ES 2 guarantees this format is supported
619         return true;
620     }
621
622     if (!fTwoFormatLimit) {
623         // not limited by ES 2's constraints
624         return true;
625     }
626
627     GrGLint otherFormat = GR_GL_RGBA;
628     GrGLint otherType = GR_GL_UNSIGNED_BYTE;
629
630     // The other supported format/type combo supported for ReadPixels
631     // can change based on which render target is bound
632     GR_GL_GetIntegerv(intf,
633                       GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT,
634                       &otherFormat);
635
636     GR_GL_GetIntegerv(intf,
637                       GR_GL_IMPLEMENTATION_COLOR_READ_TYPE,
638                       &otherType);
639
640     return (GrGLenum)otherFormat == format && (GrGLenum)otherType == type;
641 }
642
643 bool GrGLCaps::readPixelsSupported(const GrGLInterface* intf,
644                                    GrGLenum format,
645                                    GrGLenum type,
646                                    GrGLenum currFboFormat) const {
647
648     ReadPixelsSupportedFormats::Key key = {format, type, currFboFormat};
649
650     ReadPixelsSupportedFormats* cachedValue = fReadPixelsSupportedCache.find(key);
651
652     if (NULL == cachedValue) {
653         bool value = doReadPixelsSupported(intf, format, type);
654         ReadPixelsSupportedFormats newValue(key, value);
655         fReadPixelsSupportedCache.add(newValue);
656
657         return newValue.value();
658     }
659
660     return cachedValue->value();
661 }
662
663 void GrGLCaps::initFSAASupport(const GrGLContextInfo& ctxInfo, const GrGLInterface* gli) {
664
665     fMSFBOType = kNone_MSFBOType;
666     if (kGL_GrGLStandard != ctxInfo.standard()) {
667         // We prefer the EXT/IMG extension over ES3 MSAA because we've observed
668         // ES3 driver bugs on at least one device with a tiled GPU (N10).
669         if (ctxInfo.hasExtension("GL_EXT_multisampled_render_to_texture")) {
670             fMSFBOType = kES_EXT_MsToTexture_MSFBOType;
671         } else if (ctxInfo.hasExtension("GL_IMG_multisampled_render_to_texture")) {
672             fMSFBOType = kES_IMG_MsToTexture_MSFBOType;
673         } else if (ctxInfo.version() >= GR_GL_VER(3,0)) {
674             fMSFBOType = GrGLCaps::kES_3_0_MSFBOType;
675         } else if (ctxInfo.hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
676             // chrome's extension is equivalent to the EXT msaa
677             // and fbo_blit extensions.
678             fMSFBOType = kDesktop_EXT_MSFBOType;
679         } else if (ctxInfo.hasExtension("GL_APPLE_framebuffer_multisample")) {
680             fMSFBOType = kES_Apple_MSFBOType;
681         }
682     } else {
683         if ((ctxInfo.version() >= GR_GL_VER(3,0)) ||
684             ctxInfo.hasExtension("GL_ARB_framebuffer_object")) {
685             fMSFBOType = GrGLCaps::kDesktop_ARB_MSFBOType;
686         } else if (ctxInfo.hasExtension("GL_EXT_framebuffer_multisample") &&
687                    ctxInfo.hasExtension("GL_EXT_framebuffer_blit")) {
688             fMSFBOType = GrGLCaps::kDesktop_EXT_MSFBOType;
689         }
690     }
691 }
692
693 namespace {
694 const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
695 }
696
697 void GrGLCaps::initStencilFormats(const GrGLContextInfo& ctxInfo) {
698
699     // Build up list of legal stencil formats (though perhaps not supported on
700     // the particular gpu/driver) from most preferred to least.
701
702     // these consts are in order of most preferred to least preferred
703     // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
704
705     static const StencilFormat
706                   // internal Format      stencil bits      total bits        packed?
707         gS8    = {GR_GL_STENCIL_INDEX8,   8,                8,                false},
708         gS16   = {GR_GL_STENCIL_INDEX16,  16,               16,               false},
709         gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8,                32,               true },
710         gS4    = {GR_GL_STENCIL_INDEX4,   4,                4,                false},
711     //  gS     = {GR_GL_STENCIL_INDEX,    kUnknownBitCount, kUnknownBitCount, false},
712         gDS    = {GR_GL_DEPTH_STENCIL,    kUnknownBitCount, kUnknownBitCount, true };
713
714     if (kGL_GrGLStandard == ctxInfo.standard()) {
715         bool supportsPackedDS =
716             ctxInfo.version() >= GR_GL_VER(3,0) ||
717             ctxInfo.hasExtension("GL_EXT_packed_depth_stencil") ||
718             ctxInfo.hasExtension("GL_ARB_framebuffer_object");
719
720         // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
721         // require FBO support we can expect these are legal formats and don't
722         // check. These also all support the unsized GL_STENCIL_INDEX.
723         fStencilFormats.push_back() = gS8;
724         fStencilFormats.push_back() = gS16;
725         if (supportsPackedDS) {
726             fStencilFormats.push_back() = gD24S8;
727         }
728         fStencilFormats.push_back() = gS4;
729         if (supportsPackedDS) {
730             fStencilFormats.push_back() = gDS;
731         }
732     } else {
733         // ES2 has STENCIL_INDEX8 without extensions but requires extensions
734         // for other formats.
735         // ES doesn't support using the unsized format.
736
737         fStencilFormats.push_back() = gS8;
738         //fStencilFormats.push_back() = gS16;
739         if (ctxInfo.version() >= GR_GL_VER(3,0) ||
740             ctxInfo.hasExtension("GL_OES_packed_depth_stencil")) {
741             fStencilFormats.push_back() = gD24S8;
742         }
743         if (ctxInfo.hasExtension("GL_OES_stencil4")) {
744             fStencilFormats.push_back() = gS4;
745         }
746     }
747     SkASSERT(0 == fStencilVerifiedColorConfigs.count());
748     fStencilVerifiedColorConfigs.push_back_n(fStencilFormats.count());
749 }
750
751 void GrGLCaps::markColorConfigAndStencilFormatAsVerified(
752                                     GrPixelConfig config,
753                                     const GrGLStencilBuffer::Format& format) {
754 #if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
755     return;
756 #endif
757     SkASSERT((unsigned)config < (unsigned)kGrPixelConfigCnt);
758     SkASSERT(fStencilFormats.count() == fStencilVerifiedColorConfigs.count());
759     int count = fStencilFormats.count();
760     // we expect a really small number of possible formats so linear search
761     // should be OK
762     SkASSERT(count < 16);
763     for (int i = 0; i < count; ++i) {
764         if (format.fInternalFormat ==
765             fStencilFormats[i].fInternalFormat) {
766             fStencilVerifiedColorConfigs[i].markVerified(config);
767             return;
768         }
769     }
770     SkFAIL("Why are we seeing a stencil format that "
771             "GrGLCaps doesn't know about.");
772 }
773
774 bool GrGLCaps::isColorConfigAndStencilFormatVerified(
775                                 GrPixelConfig config,
776                                 const GrGLStencilBuffer::Format& format) const {
777 #if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
778     return false;
779 #endif
780     SkASSERT((unsigned)config < (unsigned)kGrPixelConfigCnt);
781     int count = fStencilFormats.count();
782     // we expect a really small number of possible formats so linear search
783     // should be OK
784     SkASSERT(count < 16);
785     for (int i = 0; i < count; ++i) {
786         if (format.fInternalFormat ==
787             fStencilFormats[i].fInternalFormat) {
788             return fStencilVerifiedColorConfigs[i].isVerified(config);
789         }
790     }
791     SkFAIL("Why are we seeing a stencil format that "
792             "GLCaps doesn't know about.");
793     return false;
794 }
795
796 SkString GrGLCaps::dump() const {
797
798     SkString r = INHERITED::dump();
799
800     r.appendf("--- GL-Specific ---\n");
801     for (int i = 0; i < fStencilFormats.count(); ++i) {
802         r.appendf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
803                  i,
804                  fStencilFormats[i].fStencilBits,
805                  fStencilFormats[i].fTotalBits);
806     }
807
808     static const char* kMSFBOExtStr[] = {
809         "None",
810         "ARB",
811         "EXT",
812         "ES 3.0",
813         "Apple",
814         "IMG MS To Texture",
815         "EXT MS To Texture",
816     };
817     GR_STATIC_ASSERT(0 == kNone_MSFBOType);
818     GR_STATIC_ASSERT(1 == kDesktop_ARB_MSFBOType);
819     GR_STATIC_ASSERT(2 == kDesktop_EXT_MSFBOType);
820     GR_STATIC_ASSERT(3 == kES_3_0_MSFBOType);
821     GR_STATIC_ASSERT(4 == kES_Apple_MSFBOType);
822     GR_STATIC_ASSERT(5 == kES_IMG_MsToTexture_MSFBOType);
823     GR_STATIC_ASSERT(6 == kES_EXT_MsToTexture_MSFBOType);
824     GR_STATIC_ASSERT(SK_ARRAY_COUNT(kMSFBOExtStr) == kLast_MSFBOType + 1);
825
826     static const char* kInvalidateFBTypeStr[] = {
827         "None",
828         "Discard",
829         "Invalidate",
830     };
831     GR_STATIC_ASSERT(0 == kNone_InvalidateFBType);
832     GR_STATIC_ASSERT(1 == kDiscard_InvalidateFBType);
833     GR_STATIC_ASSERT(2 == kInvalidate_InvalidateFBType);
834     GR_STATIC_ASSERT(SK_ARRAY_COUNT(kInvalidateFBTypeStr) == kLast_InvalidateFBType + 1);
835
836     static const char* kMapBufferTypeStr[] = {
837         "None",
838         "MapBuffer",
839         "MapBufferRange",
840         "Chromium",
841     };
842     GR_STATIC_ASSERT(0 == kNone_MapBufferType);
843     GR_STATIC_ASSERT(1 == kMapBuffer_MapBufferType);
844     GR_STATIC_ASSERT(2 == kMapBufferRange_MapBufferType);
845     GR_STATIC_ASSERT(3 == kChromium_MapBufferType);
846     GR_STATIC_ASSERT(SK_ARRAY_COUNT(kMapBufferTypeStr) == kLast_MapBufferType + 1);
847
848     r.appendf("Core Profile: %s\n", (fIsCoreProfile ? "YES" : "NO"));
849     r.appendf("MSAA Type: %s\n", kMSFBOExtStr[fMSFBOType]);
850     r.appendf("FB Fetch Support: %s\n", (fFBFetchSupport ? "YES" : "NO"));
851     r.appendf("Invalidate FB Type: %s\n", kInvalidateFBTypeStr[fInvalidateFBType]);
852     r.appendf("Map Buffer Type: %s\n", kMapBufferTypeStr[fMapBufferType]);
853     r.appendf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
854     r.appendf("Max FS Texture Units: %d\n", fMaxFragmentTextureUnits);
855     if (!fIsCoreProfile) {
856         r.appendf("Max Fixed Function Texture Coords: %d\n", fMaxFixedFunctionTextureCoords);
857     }
858     r.appendf("Max Vertex Attributes: %d\n", fMaxVertexAttributes);
859     r.appendf("Support RGBA8 Render Buffer: %s\n", (fRGBA8RenderbufferSupport ? "YES": "NO"));
860     r.appendf("BGRA is an internal format: %s\n", (fBGRAIsInternalFormat ? "YES": "NO"));
861     r.appendf("Support texture swizzle: %s\n", (fTextureSwizzleSupport ? "YES": "NO"));
862     r.appendf("Unpack Row length support: %s\n", (fUnpackRowLengthSupport ? "YES": "NO"));
863     r.appendf("Unpack Flip Y support: %s\n", (fUnpackFlipYSupport ? "YES": "NO"));
864     r.appendf("Pack Row length support: %s\n", (fPackRowLengthSupport ? "YES": "NO"));
865     r.appendf("Pack Flip Y support: %s\n", (fPackFlipYSupport ? "YES": "NO"));
866
867     r.appendf("Texture Usage support: %s\n", (fTextureUsageSupport ? "YES": "NO"));
868     r.appendf("Texture Storage support: %s\n", (fTexStorageSupport ? "YES": "NO"));
869     r.appendf("GL_R support: %s\n", (fTextureRedSupport ? "YES": "NO"));
870     r.appendf("GL_ARB_imaging support: %s\n", (fImagingSupport ? "YES": "NO"));
871     r.appendf("Two Format Limit: %s\n", (fTwoFormatLimit ? "YES": "NO"));
872     r.appendf("Fragment coord conventions support: %s\n",
873              (fFragCoordsConventionSupport ? "YES": "NO"));
874     r.appendf("Vertex array object support: %s\n", (fVertexArrayObjectSupport ? "YES": "NO"));
875     r.appendf("Use non-VBO for dynamic data: %s\n",
876              (fUseNonVBOVertexAndIndexDynamicData ? "YES" : "NO"));
877     r.appendf("Full screen clear is free: %s\n", (fFullClearIsFree ? "YES" : "NO"));
878     r.appendf("Drops tile on zero divide: %s\n", (fDropsTileOnZeroDivide ? "YES" : "NO"));
879     return r;
880 }