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