Upstream version 7.36.149.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 #include "GrGLContext.h"
11 #include "SkTSearch.h"
12 #include "SkTSort.h"
13
14 GrGLCaps::GrGLCaps() {
15     this->reset();
16 }
17
18 void GrGLCaps::reset() {
19     INHERITED::reset();
20
21     fVerifiedColorConfigs.reset();
22     fStencilFormats.reset();
23     fStencilVerifiedColorConfigs.reset();
24     fMSFBOType = kNone_MSFBOType;
25     fFBFetchType = kNone_FBFetchType;
26     fInvalidateFBType = kNone_InvalidateFBType;
27     fMapBufferType = kNone_MapBufferType;
28     fMaxFragmentUniformVectors = 0;
29     fMaxVertexAttributes = 0;
30     fMaxFragmentTextureUnits = 0;
31     fMaxFixedFunctionTextureCoords = 0;
32     fRGBA8RenderbufferSupport = false;
33     fBGRAFormatSupport = 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 }
52
53 GrGLCaps::GrGLCaps(const GrGLCaps& caps) : GrDrawTargetCaps() {
54     *this = caps;
55 }
56
57 GrGLCaps& GrGLCaps::operator= (const GrGLCaps& caps) {
58     INHERITED::operator=(caps);
59     fVerifiedColorConfigs = caps.fVerifiedColorConfigs;
60     fStencilFormats = caps.fStencilFormats;
61     fStencilVerifiedColorConfigs = caps.fStencilVerifiedColorConfigs;
62     fMaxFragmentUniformVectors = caps.fMaxFragmentUniformVectors;
63     fMaxVertexAttributes = caps.fMaxVertexAttributes;
64     fMaxFragmentTextureUnits = caps.fMaxFragmentTextureUnits;
65     fMaxFixedFunctionTextureCoords = caps.fMaxFixedFunctionTextureCoords;
66     fMSFBOType = caps.fMSFBOType;
67     fFBFetchType = caps.fFBFetchType;
68     fInvalidateFBType = caps.fInvalidateFBType;
69     fMapBufferType = caps.fMapBufferType;
70     fRGBA8RenderbufferSupport = caps.fRGBA8RenderbufferSupport;
71     fBGRAFormatSupport = caps.fBGRAFormatSupport;
72     fBGRAIsInternalFormat = caps.fBGRAIsInternalFormat;
73     fTextureSwizzleSupport = caps.fTextureSwizzleSupport;
74     fUnpackRowLengthSupport = caps.fUnpackRowLengthSupport;
75     fUnpackFlipYSupport = caps.fUnpackFlipYSupport;
76     fPackRowLengthSupport = caps.fPackRowLengthSupport;
77     fPackFlipYSupport = caps.fPackFlipYSupport;
78     fTextureUsageSupport = caps.fTextureUsageSupport;
79     fTexStorageSupport = caps.fTexStorageSupport;
80     fTextureRedSupport = caps.fTextureRedSupport;
81     fImagingSupport = caps.fImagingSupport;
82     fTwoFormatLimit = caps.fTwoFormatLimit;
83     fFragCoordsConventionSupport = caps.fFragCoordsConventionSupport;
84     fVertexArrayObjectSupport = caps.fVertexArrayObjectSupport;
85     fUseNonVBOVertexAndIndexDynamicData = caps.fUseNonVBOVertexAndIndexDynamicData;
86     fIsCoreProfile = caps.fIsCoreProfile;
87     fFullClearIsFree = caps.fFullClearIsFree;
88     fDropsTileOnZeroDivide = caps.fDropsTileOnZeroDivide;
89
90     return *this;
91 }
92
93 bool GrGLCaps::init(const GrGLContextInfo& ctxInfo, const GrGLInterface* gli) {
94
95     this->reset();
96     if (!ctxInfo.isInitialized()) {
97         return false;
98     }
99
100     GrGLStandard standard = ctxInfo.standard();
101     GrGLVersion version = ctxInfo.version();
102
103     /**************************************************************************
104      * Caps specific to GrGLCaps
105      **************************************************************************/
106
107     if (kGLES_GrGLStandard == standard) {
108         GR_GL_GetIntegerv(gli, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
109                           &fMaxFragmentUniformVectors);
110     } else {
111         SkASSERT(kGL_GrGLStandard == standard);
112         GrGLint max;
113         GR_GL_GetIntegerv(gli, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
114         fMaxFragmentUniformVectors = max / 4;
115         if (version >= GR_GL_VER(3, 2)) {
116             GrGLint profileMask;
117             GR_GL_GetIntegerv(gli, GR_GL_CONTEXT_PROFILE_MASK, &profileMask);
118             fIsCoreProfile = SkToBool(profileMask & GR_GL_CONTEXT_CORE_PROFILE_BIT);
119         }
120         if (!fIsCoreProfile) {
121             GR_GL_GetIntegerv(gli, GR_GL_MAX_TEXTURE_COORDS, &fMaxFixedFunctionTextureCoords);
122             // Sanity check
123             SkASSERT(fMaxFixedFunctionTextureCoords > 0 && fMaxFixedFunctionTextureCoords < 128);
124         }
125     }
126     GR_GL_GetIntegerv(gli, GR_GL_MAX_VERTEX_ATTRIBS, &fMaxVertexAttributes);
127     GR_GL_GetIntegerv(gli, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &fMaxFragmentTextureUnits);
128
129     if (kGL_GrGLStandard == standard) {
130         fRGBA8RenderbufferSupport = true;
131     } else {
132         fRGBA8RenderbufferSupport = version >= GR_GL_VER(3,0) ||
133                                     ctxInfo.hasExtension("GL_OES_rgb8_rgba8") ||
134                                     ctxInfo.hasExtension("GL_ARM_rgba8");
135     }
136
137     if (kGL_GrGLStandard == standard) {
138         fBGRAFormatSupport = version >= GR_GL_VER(1,2) ||
139                              ctxInfo.hasExtension("GL_EXT_bgra");
140     } else {
141         if (ctxInfo.hasExtension("GL_APPLE_texture_format_BGRA8888")) {
142             fBGRAFormatSupport = true;
143         } else if (ctxInfo.hasExtension("GL_EXT_texture_format_BGRA8888")) {
144             fBGRAFormatSupport = true;
145             fBGRAIsInternalFormat = true;
146         }
147         SkASSERT(fBGRAFormatSupport ||
148                  kSkia8888_GrPixelConfig != kBGRA_8888_GrPixelConfig);
149     }
150
151     if (kGL_GrGLStandard == standard) {
152         fTextureSwizzleSupport = version >= GR_GL_VER(3,3) ||
153                                  ctxInfo.hasExtension("GL_ARB_texture_swizzle");
154     } else {
155         fTextureSwizzleSupport = version >= GR_GL_VER(3,0);
156     }
157
158     if (kGL_GrGLStandard == standard) {
159         fUnpackRowLengthSupport = true;
160         fUnpackFlipYSupport = false;
161         fPackRowLengthSupport = true;
162         fPackFlipYSupport = false;
163     } else {
164         fUnpackRowLengthSupport = version >= GR_GL_VER(3,0) ||
165                                   ctxInfo.hasExtension("GL_EXT_unpack_subimage");
166         fUnpackFlipYSupport = ctxInfo.hasExtension("GL_CHROMIUM_flipy");
167         fPackRowLengthSupport = version >= GR_GL_VER(3,0) ||
168                                 ctxInfo.hasExtension("GL_NV_pack_subimage");
169         fPackFlipYSupport =
170             ctxInfo.hasExtension("GL_ANGLE_pack_reverse_row_order");
171     }
172
173     fTextureUsageSupport = (kGLES_GrGLStandard == standard) &&
174                             ctxInfo.hasExtension("GL_ANGLE_texture_usage");
175
176     if (kGL_GrGLStandard == standard) {
177         // The EXT version can apply to either GL or GLES.
178         fTexStorageSupport = version >= GR_GL_VER(4,2) ||
179                              ctxInfo.hasExtension("GL_ARB_texture_storage") ||
180                              ctxInfo.hasExtension("GL_EXT_texture_storage");
181     } else {
182         // Qualcomm Adreno drivers appear to have issues with texture storage.
183         fTexStorageSupport = (version >= GR_GL_VER(3,0) &&
184                               kQualcomm_GrGLVendor != ctxInfo.vendor()) ||
185                              ctxInfo.hasExtension("GL_EXT_texture_storage");
186     }
187
188     // ARB_texture_rg is part of OpenGL 3.0, but mesa doesn't support it if
189     // it doesn't have ARB_texture_rg extension.
190     if (kGL_GrGLStandard == standard) {
191         if (ctxInfo.isMesa()) {
192             fTextureRedSupport = ctxInfo.hasExtension("GL_ARB_texture_rg");
193         } else {
194             fTextureRedSupport = version >= GR_GL_VER(3,0) ||
195                                  ctxInfo.hasExtension("GL_ARB_texture_rg");
196         }
197     } else {
198         fTextureRedSupport =  version >= GR_GL_VER(3,0) ||
199                               ctxInfo.hasExtension("GL_EXT_texture_rg");
200     }
201
202     fImagingSupport = kGL_GrGLStandard == standard &&
203                       ctxInfo.hasExtension("GL_ARB_imaging");
204
205     // ES 2 only guarantees RGBA/uchar + one other format/type combo for
206     // ReadPixels. The other format has to checked at run-time since it
207     // can change based on which render target is bound
208     fTwoFormatLimit = kGLES_GrGLStandard == standard;
209
210     // Known issue on at least some Intel platforms:
211     // http://code.google.com/p/skia/issues/detail?id=946
212     if (kIntel_GrGLVendor != ctxInfo.vendor()) {
213         fFragCoordsConventionSupport = ctxInfo.glslGeneration() >= k150_GrGLSLGeneration ||
214                                        ctxInfo.hasExtension("GL_ARB_fragment_coord_conventions");
215     }
216
217     // SGX and Mali GPUs that are based on a tiled-deferred architecture that have trouble with
218     // frequently changing VBOs. We've measured a performance increase using non-VBO vertex
219     // data for dynamic content on these GPUs. Perhaps we should read the renderer string and
220     // limit this decision to specific GPU families rather than basing it on the vendor alone.
221     if (!GR_GL_MUST_USE_VBO &&
222         (kARM_GrGLVendor == ctxInfo.vendor() || kImagination_GrGLVendor == ctxInfo.vendor())) {
223         fUseNonVBOVertexAndIndexDynamicData = true;
224     }
225
226     if ((kGL_GrGLStandard == standard && version >= GR_GL_VER(4,3)) ||
227         (kGLES_GrGLStandard == standard && version >= GR_GL_VER(3,0)) ||
228         ctxInfo.hasExtension("GL_ARB_invalidate_subdata")) {
229         fDiscardRenderTargetSupport = true;
230         fInvalidateFBType = kInvalidate_InvalidateFBType;
231     } else if (ctxInfo.hasExtension("GL_EXT_discard_framebuffer")) {
232         fDiscardRenderTargetSupport = true;
233         fInvalidateFBType = kDiscard_InvalidateFBType;
234     }
235
236     if (kARM_GrGLVendor == ctxInfo.vendor() || kImagination_GrGLVendor == ctxInfo.vendor()) {
237         fFullClearIsFree = true;
238     }
239
240     if (kGL_GrGLStandard == standard) {
241         fVertexArrayObjectSupport = version >= GR_GL_VER(3, 0) ||
242                                     ctxInfo.hasExtension("GL_ARB_vertex_array_object");
243     } else {
244         fVertexArrayObjectSupport = version >= GR_GL_VER(3, 0) ||
245                                     ctxInfo.hasExtension("GL_OES_vertex_array_object");
246     }
247
248     if (kGLES_GrGLStandard == standard) {
249         if (ctxInfo.hasExtension("GL_EXT_shader_framebuffer_fetch")) {
250             fFBFetchType = kEXT_FBFetchType;
251         } else if (ctxInfo.hasExtension("GL_NV_shader_framebuffer_fetch")) {
252             fFBFetchType = kNV_FBFetchType;
253         }
254     }
255
256     // Adreno GPUs have a tendency to drop tiles when there is a divide-by-zero in a shader
257     fDropsTileOnZeroDivide = kQualcomm_GrGLVendor == ctxInfo.vendor();
258
259     this->initFSAASupport(ctxInfo, gli);
260     this->initStencilFormats(ctxInfo);
261
262     /**************************************************************************
263      * GrDrawTargetCaps fields
264      **************************************************************************/
265     GrGLint numFormats;
266     GR_GL_GetIntegerv(gli, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
267     if (numFormats) {
268         SkAutoSTMalloc<10, GrGLint> formats(numFormats);
269         GR_GL_GetIntegerv(gli, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
270         for (int i = 0; i < numFormats; ++i) {
271             if (formats[i] == GR_GL_PALETTE8_RGBA8) {
272                 f8BitPaletteSupport = true;
273                 break;
274             }
275         }
276     }
277
278     if (kGL_GrGLStandard == standard) {
279         // we could also look for GL_ATI_separate_stencil extension or
280         // GL_EXT_stencil_two_side but they use different function signatures
281         // than GL2.0+ (and than each other).
282         fTwoSidedStencilSupport = (ctxInfo.version() >= GR_GL_VER(2,0));
283         // supported on GL 1.4 and higher or by extension
284         fStencilWrapOpsSupport = (ctxInfo.version() >= GR_GL_VER(1,4)) ||
285                                   ctxInfo.hasExtension("GL_EXT_stencil_wrap");
286     } else {
287         // ES 2 has two sided stencil and stencil wrap
288         fTwoSidedStencilSupport = true;
289         fStencilWrapOpsSupport = true;
290     }
291
292     if (kGL_GrGLStandard == standard) {
293         fMapBufferFlags = kCanMap_MapFlag; // we require VBO support and the desktop VBO
294                                             // extension includes glMapBuffer.
295         if (version >= GR_GL_VER(3, 0) || ctxInfo.hasExtension("GL_ARB_map_buffer_range")) {
296             fMapBufferFlags |= kSubset_MapFlag;
297             fMapBufferType = kMapBufferRange_MapBufferType;
298         } else {
299             fMapBufferType = kMapBuffer_MapBufferType;
300         }
301     } else {
302         // Unextended GLES2 doesn't have any buffer mapping.
303         fMapBufferFlags = kNone_MapBufferType;
304         if (ctxInfo.hasExtension("GL_CHROMIUM_map_sub")) {
305             fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
306             fMapBufferType = kChromium_MapBufferType;
307         } else if (version >= GR_GL_VER(3, 0) || ctxInfo.hasExtension("GL_EXT_map_buffer_range")) {
308             fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
309             fMapBufferType = kMapBufferRange_MapBufferType;
310         } else if (ctxInfo.hasExtension("GL_OES_mapbuffer")) {
311             fMapBufferFlags = kCanMap_MapFlag;
312             fMapBufferType = kMapBuffer_MapBufferType;
313         }
314     }
315
316     if (kGL_GrGLStandard == standard) {
317         SkASSERT(ctxInfo.version() >= GR_GL_VER(2,0) ||
318                  ctxInfo.hasExtension("GL_ARB_texture_non_power_of_two"));
319         fNPOTTextureTileSupport = true;
320         fMipMapSupport = true;
321     } else {
322         // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
323         // ES3 has no limitations.
324         fNPOTTextureTileSupport = ctxInfo.version() >= GR_GL_VER(3,0) ||
325                                   ctxInfo.hasExtension("GL_OES_texture_npot");
326         // ES2 supports MIP mapping for POT textures but our caps don't allow for limited MIP
327         // support. The OES extension or ES 3.0 allow for MIPS on NPOT textures. So, apparently,
328         // does the undocumented GL_IMG_texture_npot extension. This extension does not seem to
329         // to alllow arbitrary wrap modes, however.
330         fMipMapSupport = fNPOTTextureTileSupport || ctxInfo.hasExtension("GL_IMG_texture_npot");
331     }
332
333     fHWAALineSupport = (kGL_GrGLStandard == standard);
334
335     GR_GL_GetIntegerv(gli, GR_GL_MAX_TEXTURE_SIZE, &fMaxTextureSize);
336     GR_GL_GetIntegerv(gli, GR_GL_MAX_RENDERBUFFER_SIZE, &fMaxRenderTargetSize);
337     // Our render targets are always created with textures as the color
338     // attachment, hence this min:
339     fMaxRenderTargetSize = SkTMin(fMaxTextureSize, fMaxRenderTargetSize);
340
341     fPathRenderingSupport = ctxInfo.hasExtension("GL_NV_path_rendering") &&
342         ctxInfo.hasExtension("GL_EXT_direct_state_access");
343
344     fGpuTracingSupport = ctxInfo.hasExtension("GL_EXT_debug_marker");
345
346     fDstReadInShaderSupport = kNone_FBFetchType != fFBFetchType;
347
348     // Disable scratch texture reuse on Mali and Adreno devices
349     fReuseScratchTextures = kARM_GrGLVendor != ctxInfo.vendor() &&
350                             kQualcomm_GrGLVendor != ctxInfo.vendor();
351
352     // Enable supported shader-related caps
353     if (kGL_GrGLStandard == standard) {
354         fDualSourceBlendingSupport = ctxInfo.version() >= GR_GL_VER(3,3) ||
355                                      ctxInfo.hasExtension("GL_ARB_blend_func_extended");
356         fShaderDerivativeSupport = true;
357         // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
358         fGeometryShaderSupport = ctxInfo.version() >= GR_GL_VER(3,2) &&
359                                  ctxInfo.glslGeneration() >= k150_GrGLSLGeneration;
360     } else {
361         fShaderDerivativeSupport = ctxInfo.hasExtension("GL_OES_standard_derivatives");
362     }
363
364     if (GrGLCaps::kES_IMG_MsToTexture_MSFBOType == fMSFBOType) {
365         GR_GL_GetIntegerv(gli, GR_GL_MAX_SAMPLES_IMG, &fMaxSampleCount);
366     } else if (GrGLCaps::kNone_MSFBOType != fMSFBOType) {
367         GR_GL_GetIntegerv(gli, GR_GL_MAX_SAMPLES, &fMaxSampleCount);
368     }
369
370     this->initConfigRenderableTable(ctxInfo);
371
372     return true;
373 }
374
375 void GrGLCaps::initConfigRenderableTable(const GrGLContextInfo& ctxInfo) {
376
377     // OpenGL < 3.0
378     //  no support for render targets unless the GL_ARB_framebuffer_object
379     //  extension is supported (in which case we get ALPHA, RED, RG, RGB,
380     //  RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
381     //  probably don't get R8 in this case.
382
383     // OpenGL 3.0
384     //  base color renderable: ALPHA, RED, RG, RGB, and RGBA
385     //  sized derivatives: ALPHA8, R8, RGBA4, RGBA8
386
387     // >= OpenGL 3.1
388     //  base color renderable: RED, RG, RGB, and RGBA
389     //  sized derivatives: R8, RGBA4, RGBA8
390     //  if the GL_ARB_compatibility extension is supported then we get back
391     //  support for GL_ALPHA and ALPHA8
392
393     // GL_EXT_bgra adds BGRA render targets to any version
394
395     // ES 2.0
396     //  color renderable: RGBA4, RGB5_A1, RGB565
397     //  GL_EXT_texture_rg adds support for R8 as a color render target
398     //  GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
399     //  GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888 added BGRA support
400
401     // ES 3.0
402     // Same as ES 2.0 except R8 and RGBA8 are supported without extensions (the functions called
403     // below already account for this).
404
405     enum {
406         kNo_MSAA = 0,
407         kYes_MSAA = 1,
408     };
409
410     if (kGL_GrGLStandard == ctxInfo.standard()) {
411         // Post 3.0 we will get R8
412         // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
413         if (ctxInfo.version() >= GR_GL_VER(3,0) ||
414             ctxInfo.hasExtension("GL_ARB_framebuffer_object")) {
415             fConfigRenderSupport[kAlpha_8_GrPixelConfig][kNo_MSAA] = true;
416             fConfigRenderSupport[kAlpha_8_GrPixelConfig][kYes_MSAA] = true;
417         }
418     } else {
419         // On ES we can only hope for R8
420         fConfigRenderSupport[kAlpha_8_GrPixelConfig][kNo_MSAA] = fTextureRedSupport;
421         fConfigRenderSupport[kAlpha_8_GrPixelConfig][kYes_MSAA] = fTextureRedSupport;
422     }
423
424     if (kGL_GrGLStandard != ctxInfo.standard()) {
425         // only available in ES
426         fConfigRenderSupport[kRGB_565_GrPixelConfig][kNo_MSAA] = true;
427         fConfigRenderSupport[kRGB_565_GrPixelConfig][kYes_MSAA] = true;
428     }
429
430     // we no longer support 444 as a render target
431     fConfigRenderSupport[kRGBA_4444_GrPixelConfig][kNo_MSAA]  = false;
432     fConfigRenderSupport[kRGBA_4444_GrPixelConfig][kYes_MSAA]  = false;
433
434     if (this->fRGBA8RenderbufferSupport) {
435         fConfigRenderSupport[kRGBA_8888_GrPixelConfig][kNo_MSAA]  = true;
436         fConfigRenderSupport[kRGBA_8888_GrPixelConfig][kYes_MSAA]  = true;
437     }
438
439     if (this->fBGRAFormatSupport) {
440         fConfigRenderSupport[kBGRA_8888_GrPixelConfig][kNo_MSAA]  = true;
441         // The GL_EXT_texture_format_BGRA8888 extension does not add BGRA to the list of
442         // configs that are color-renderable and can be passed to glRenderBufferStorageMultisample.
443         // Chromium may have an extension to allow BGRA renderbuffers to work on desktop platforms.
444         if (ctxInfo.hasExtension("GL_CHROMIUM_renderbuffer_format_BGRA8888")) {
445             fConfigRenderSupport[kBGRA_8888_GrPixelConfig][kYes_MSAA] = true;
446         } else {
447             fConfigRenderSupport[kBGRA_8888_GrPixelConfig][kYes_MSAA] =
448                 !fBGRAIsInternalFormat || !this->usesMSAARenderBuffers();
449         }
450     }
451
452     // If we don't support MSAA then undo any places above where we set a config as renderable with
453     // msaa.
454     if (kNone_MSFBOType == fMSFBOType) {
455         for (int i = 0; i < kGrPixelConfigCnt; ++i) {
456             fConfigRenderSupport[i][kYes_MSAA] = false;
457         }
458     }
459 }
460
461 bool GrGLCaps::readPixelsSupported(const GrGLInterface* intf,
462                                    GrGLenum format,
463                                    GrGLenum type) const {
464     if (GR_GL_RGBA == format && GR_GL_UNSIGNED_BYTE == type) {
465         // ES 2 guarantees this format is supported
466         return true;
467     }
468
469     if (!fTwoFormatLimit) {
470         // not limited by ES 2's constraints
471         return true;
472     }
473
474     GrGLint otherFormat = GR_GL_RGBA;
475     GrGLint otherType = GR_GL_UNSIGNED_BYTE;
476
477     // The other supported format/type combo supported for ReadPixels
478     // can change based on which render target is bound
479     GR_GL_GetIntegerv(intf,
480                       GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT,
481                       &otherFormat);
482
483     GR_GL_GetIntegerv(intf,
484                       GR_GL_IMPLEMENTATION_COLOR_READ_TYPE,
485                       &otherType);
486
487     return (GrGLenum)otherFormat == format && (GrGLenum)otherType == type;
488 }
489
490 void GrGLCaps::initFSAASupport(const GrGLContextInfo& ctxInfo, const GrGLInterface* gli) {
491
492     fMSFBOType = kNone_MSFBOType;
493     if (kGL_GrGLStandard != ctxInfo.standard()) {
494         // We prefer the EXT/IMG extension over ES3 MSAA because we've observed
495         // ES3 driver bugs on at least one device with a tiled GPU (N10).
496         if (ctxInfo.hasExtension("GL_EXT_multisampled_render_to_texture")) {
497             fMSFBOType = kES_EXT_MsToTexture_MSFBOType;
498         } else if (ctxInfo.hasExtension("GL_IMG_multisampled_render_to_texture")) {
499             fMSFBOType = kES_IMG_MsToTexture_MSFBOType;
500         } else if (ctxInfo.version() >= GR_GL_VER(3,0)) {
501             fMSFBOType = GrGLCaps::kES_3_0_MSFBOType;
502         } else if (ctxInfo.hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
503             // chrome's extension is equivalent to the EXT msaa
504             // and fbo_blit extensions.
505             fMSFBOType = kDesktop_EXT_MSFBOType;
506         } else if (ctxInfo.hasExtension("GL_APPLE_framebuffer_multisample")) {
507             fMSFBOType = kES_Apple_MSFBOType;
508         }
509     } else {
510         if ((ctxInfo.version() >= GR_GL_VER(3,0)) ||
511             ctxInfo.hasExtension("GL_ARB_framebuffer_object")) {
512             fMSFBOType = GrGLCaps::kDesktop_ARB_MSFBOType;
513         } else if (ctxInfo.hasExtension("GL_EXT_framebuffer_multisample") &&
514                    ctxInfo.hasExtension("GL_EXT_framebuffer_blit")) {
515             fMSFBOType = GrGLCaps::kDesktop_EXT_MSFBOType;
516         }
517     }
518 }
519
520 namespace {
521 const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
522 }
523
524 void GrGLCaps::initStencilFormats(const GrGLContextInfo& ctxInfo) {
525
526     // Build up list of legal stencil formats (though perhaps not supported on
527     // the particular gpu/driver) from most preferred to least.
528
529     // these consts are in order of most preferred to least preferred
530     // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
531
532     static const StencilFormat
533                   // internal Format      stencil bits      total bits        packed?
534         gS8    = {GR_GL_STENCIL_INDEX8,   8,                8,                false},
535         gS16   = {GR_GL_STENCIL_INDEX16,  16,               16,               false},
536         gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8,                32,               true },
537         gS4    = {GR_GL_STENCIL_INDEX4,   4,                4,                false},
538     //  gS     = {GR_GL_STENCIL_INDEX,    kUnknownBitCount, kUnknownBitCount, false},
539         gDS    = {GR_GL_DEPTH_STENCIL,    kUnknownBitCount, kUnknownBitCount, true };
540
541     if (kGL_GrGLStandard == ctxInfo.standard()) {
542         bool supportsPackedDS =
543             ctxInfo.version() >= GR_GL_VER(3,0) ||
544             ctxInfo.hasExtension("GL_EXT_packed_depth_stencil") ||
545             ctxInfo.hasExtension("GL_ARB_framebuffer_object");
546
547         // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
548         // require FBO support we can expect these are legal formats and don't
549         // check. These also all support the unsized GL_STENCIL_INDEX.
550         fStencilFormats.push_back() = gS8;
551         fStencilFormats.push_back() = gS16;
552         if (supportsPackedDS) {
553             fStencilFormats.push_back() = gD24S8;
554         }
555         fStencilFormats.push_back() = gS4;
556         if (supportsPackedDS) {
557             fStencilFormats.push_back() = gDS;
558         }
559     } else {
560         // ES2 has STENCIL_INDEX8 without extensions but requires extensions
561         // for other formats.
562         // ES doesn't support using the unsized format.
563
564         fStencilFormats.push_back() = gS8;
565         //fStencilFormats.push_back() = gS16;
566         if (ctxInfo.version() >= GR_GL_VER(3,0) ||
567             ctxInfo.hasExtension("GL_OES_packed_depth_stencil")) {
568             fStencilFormats.push_back() = gD24S8;
569         }
570         if (ctxInfo.hasExtension("GL_OES_stencil4")) {
571             fStencilFormats.push_back() = gS4;
572         }
573     }
574     SkASSERT(0 == fStencilVerifiedColorConfigs.count());
575     fStencilVerifiedColorConfigs.push_back_n(fStencilFormats.count());
576 }
577
578 void GrGLCaps::markColorConfigAndStencilFormatAsVerified(
579                                     GrPixelConfig config,
580                                     const GrGLStencilBuffer::Format& format) {
581 #if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
582     return;
583 #endif
584     SkASSERT((unsigned)config < (unsigned)kGrPixelConfigCnt);
585     SkASSERT(fStencilFormats.count() == fStencilVerifiedColorConfigs.count());
586     int count = fStencilFormats.count();
587     // we expect a really small number of possible formats so linear search
588     // should be OK
589     SkASSERT(count < 16);
590     for (int i = 0; i < count; ++i) {
591         if (format.fInternalFormat ==
592             fStencilFormats[i].fInternalFormat) {
593             fStencilVerifiedColorConfigs[i].markVerified(config);
594             return;
595         }
596     }
597     SkFAIL("Why are we seeing a stencil format that "
598             "GrGLCaps doesn't know about.");
599 }
600
601 bool GrGLCaps::isColorConfigAndStencilFormatVerified(
602                                 GrPixelConfig config,
603                                 const GrGLStencilBuffer::Format& format) const {
604 #if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
605     return false;
606 #endif
607     SkASSERT((unsigned)config < (unsigned)kGrPixelConfigCnt);
608     int count = fStencilFormats.count();
609     // we expect a really small number of possible formats so linear search
610     // should be OK
611     SkASSERT(count < 16);
612     for (int i = 0; i < count; ++i) {
613         if (format.fInternalFormat ==
614             fStencilFormats[i].fInternalFormat) {
615             return fStencilVerifiedColorConfigs[i].isVerified(config);
616         }
617     }
618     SkFAIL("Why are we seeing a stencil format that "
619             "GLCaps doesn't know about.");
620     return false;
621 }
622
623 SkString GrGLCaps::dump() const {
624
625     SkString r = INHERITED::dump();
626
627     r.appendf("--- GL-Specific ---\n");
628     for (int i = 0; i < fStencilFormats.count(); ++i) {
629         r.appendf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
630                  i,
631                  fStencilFormats[i].fStencilBits,
632                  fStencilFormats[i].fTotalBits);
633     }
634
635     static const char* kMSFBOExtStr[] = {
636         "None",
637         "ARB",
638         "EXT",
639         "ES 3.0",
640         "Apple",
641         "IMG MS To Texture",
642         "EXT MS To Texture",
643     };
644     GR_STATIC_ASSERT(0 == kNone_MSFBOType);
645     GR_STATIC_ASSERT(1 == kDesktop_ARB_MSFBOType);
646     GR_STATIC_ASSERT(2 == kDesktop_EXT_MSFBOType);
647     GR_STATIC_ASSERT(3 == kES_3_0_MSFBOType);
648     GR_STATIC_ASSERT(4 == kES_Apple_MSFBOType);
649     GR_STATIC_ASSERT(5 == kES_IMG_MsToTexture_MSFBOType);
650     GR_STATIC_ASSERT(6 == kES_EXT_MsToTexture_MSFBOType);
651     GR_STATIC_ASSERT(SK_ARRAY_COUNT(kMSFBOExtStr) == kLast_MSFBOType + 1);
652
653     static const char* kFBFetchTypeStr[] = {
654         "None",
655         "EXT",
656         "NV",
657     };
658     GR_STATIC_ASSERT(0 == kNone_FBFetchType);
659     GR_STATIC_ASSERT(1 == kEXT_FBFetchType);
660     GR_STATIC_ASSERT(2 == kNV_FBFetchType);
661     GR_STATIC_ASSERT(SK_ARRAY_COUNT(kFBFetchTypeStr) == kLast_FBFetchType + 1);
662
663     static const char* kInvalidateFBTypeStr[] = {
664         "None",
665         "Discard",
666         "Invalidate",
667     };
668     GR_STATIC_ASSERT(0 == kNone_InvalidateFBType);
669     GR_STATIC_ASSERT(1 == kDiscard_InvalidateFBType);
670     GR_STATIC_ASSERT(2 == kInvalidate_InvalidateFBType);
671     GR_STATIC_ASSERT(SK_ARRAY_COUNT(kInvalidateFBTypeStr) == kLast_InvalidateFBType + 1);
672
673     static const char* kMapBufferTypeStr[] = {
674         "None",
675         "MapBuffer",
676         "MapBufferRange",
677         "Chromium",
678     };
679     GR_STATIC_ASSERT(0 == kNone_MapBufferType);
680     GR_STATIC_ASSERT(1 == kMapBuffer_MapBufferType);
681     GR_STATIC_ASSERT(2 == kMapBufferRange_MapBufferType);
682     GR_STATIC_ASSERT(3 == kChromium_MapBufferType);
683     GR_STATIC_ASSERT(SK_ARRAY_COUNT(kMapBufferTypeStr) == kLast_MapBufferType + 1);
684
685     r.appendf("Core Profile: %s\n", (fIsCoreProfile ? "YES" : "NO"));
686     r.appendf("MSAA Type: %s\n", kMSFBOExtStr[fMSFBOType]);
687     r.appendf("FB Fetch Type: %s\n", kFBFetchTypeStr[fFBFetchType]);
688     r.appendf("Invalidate FB Type: %s\n", kInvalidateFBTypeStr[fInvalidateFBType]);
689     r.appendf("Map Buffer Type: %s\n", kMapBufferTypeStr[fMapBufferType]);
690     r.appendf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
691     r.appendf("Max FS Texture Units: %d\n", fMaxFragmentTextureUnits);
692     if (!fIsCoreProfile) {
693         r.appendf("Max Fixed Function Texture Coords: %d\n", fMaxFixedFunctionTextureCoords);
694     }
695     r.appendf("Max Vertex Attributes: %d\n", fMaxVertexAttributes);
696     r.appendf("Support RGBA8 Render Buffer: %s\n", (fRGBA8RenderbufferSupport ? "YES": "NO"));
697     r.appendf("BGRA support: %s\n", (fBGRAFormatSupport ? "YES": "NO"));
698     r.appendf("BGRA is an internal format: %s\n", (fBGRAIsInternalFormat ? "YES": "NO"));
699     r.appendf("Support texture swizzle: %s\n", (fTextureSwizzleSupport ? "YES": "NO"));
700     r.appendf("Unpack Row length support: %s\n", (fUnpackRowLengthSupport ? "YES": "NO"));
701     r.appendf("Unpack Flip Y support: %s\n", (fUnpackFlipYSupport ? "YES": "NO"));
702     r.appendf("Pack Row length support: %s\n", (fPackRowLengthSupport ? "YES": "NO"));
703     r.appendf("Pack Flip Y support: %s\n", (fPackFlipYSupport ? "YES": "NO"));
704
705     r.appendf("Texture Usage support: %s\n", (fTextureUsageSupport ? "YES": "NO"));
706     r.appendf("Texture Storage support: %s\n", (fTexStorageSupport ? "YES": "NO"));
707     r.appendf("GL_R support: %s\n", (fTextureRedSupport ? "YES": "NO"));
708     r.appendf("GL_ARB_imaging support: %s\n", (fImagingSupport ? "YES": "NO"));
709     r.appendf("Two Format Limit: %s\n", (fTwoFormatLimit ? "YES": "NO"));
710     r.appendf("Fragment coord conventions support: %s\n",
711              (fFragCoordsConventionSupport ? "YES": "NO"));
712     r.appendf("Vertex array object support: %s\n", (fVertexArrayObjectSupport ? "YES": "NO"));
713     r.appendf("Use non-VBO for dynamic data: %s\n",
714              (fUseNonVBOVertexAndIndexDynamicData ? "YES" : "NO"));
715     r.appendf("Full screen clear is free: %s\n", (fFullClearIsFree ? "YES" : "NO"));
716     r.appendf("Drops tile on zero divide: %s\n", (fDropsTileOnZeroDivide ? "YES" : "NO"));
717     return r;
718 }