Home | History | Annotate | Download | only in gl
      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 #include "GrGLCaps.h"
      9 #include "GrContextOptions.h"
     10 #include "GrGLContext.h"
     11 #include "GrGLRenderTarget.h"
     12 #include "GrGLTexture.h"
     13 #include "GrShaderCaps.h"
     14 #include "GrSurfaceProxyPriv.h"
     15 #include "SkJSONWriter.h"
     16 #include "SkTSearch.h"
     17 #include "SkTSort.h"
     18 
     19 GrGLCaps::GrGLCaps(const GrContextOptions& contextOptions,
     20                    const GrGLContextInfo& ctxInfo,
     21                    const GrGLInterface* glInterface) : INHERITED(contextOptions) {
     22     fStandard = ctxInfo.standard();
     23 
     24     fStencilFormats.reset();
     25     fMSFBOType = kNone_MSFBOType;
     26     fInvalidateFBType = kNone_InvalidateFBType;
     27     fMapBufferType = kNone_MapBufferType;
     28     fTransferBufferType = kNone_TransferBufferType;
     29     fMaxFragmentUniformVectors = 0;
     30     fUnpackRowLengthSupport = false;
     31     fUnpackFlipYSupport = false;
     32     fPackRowLengthSupport = false;
     33     fPackFlipYSupport = false;
     34     fTextureUsageSupport = false;
     35     fAlpha8IsRenderable = false;
     36     fImagingSupport = false;
     37     fVertexArrayObjectSupport = false;
     38     fDirectStateAccessSupport = false;
     39     fDebugSupport = false;
     40     fES2CompatibilitySupport = false;
     41     fDrawIndirectSupport = false;
     42     fMultiDrawIndirectSupport = false;
     43     fBaseInstanceSupport = false;
     44     fIsCoreProfile = false;
     45     fBindFragDataLocationSupport = false;
     46     fRectangleTextureSupport = false;
     47     fTextureSwizzleSupport = false;
     48     fRGBA8888PixelsOpsAreSlow = false;
     49     fPartialFBOReadIsSlow = false;
     50     fMipMapLevelAndLodControlSupport = false;
     51     fRGBAToBGRAReadbackConversionsAreSlow = false;
     52     fDoManualMipmapping = false;
     53     fSRGBDecodeDisableAffectsMipmaps = false;
     54     fClearToBoundaryValuesIsBroken = false;
     55     fClearTextureSupport = false;
     56     fDrawArraysBaseVertexIsBroken = false;
     57     fUseDrawToClearColor = false;
     58     fUseDrawToClearStencilClip = false;
     59     fDisallowTexSubImageForUnormConfigTexturesEverBoundToFBO = false;
     60     fUseDrawInsteadOfAllRenderTargetWrites = false;
     61     fRequiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines = false;
     62     fProgramBinarySupport = false;
     63 
     64     fBlitFramebufferFlags = kNoSupport_BlitFramebufferFlag;
     65     fMaxInstancesPerDrawArraysWithoutCrashing = 0;
     66 
     67     fShaderCaps.reset(new GrShaderCaps(contextOptions));
     68 
     69     this->init(contextOptions, ctxInfo, glInterface);
     70 }
     71 
     72 void GrGLCaps::init(const GrContextOptions& contextOptions,
     73                     const GrGLContextInfo& ctxInfo,
     74                     const GrGLInterface* gli) {
     75     GrGLStandard standard = ctxInfo.standard();
     76     GrGLVersion version = ctxInfo.version();
     77 
     78     if (kGLES_GrGLStandard == standard) {
     79         GR_GL_GetIntegerv(gli, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
     80                           &fMaxFragmentUniformVectors);
     81     } else {
     82         SkASSERT(kGL_GrGLStandard == standard);
     83         GrGLint max;
     84         GR_GL_GetIntegerv(gli, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
     85         fMaxFragmentUniformVectors = max / 4;
     86         if (version >= GR_GL_VER(3, 2)) {
     87             GrGLint profileMask;
     88             GR_GL_GetIntegerv(gli, GR_GL_CONTEXT_PROFILE_MASK, &profileMask);
     89             fIsCoreProfile = SkToBool(profileMask & GR_GL_CONTEXT_CORE_PROFILE_BIT);
     90         }
     91     }
     92     GR_GL_GetIntegerv(gli, GR_GL_MAX_VERTEX_ATTRIBS, &fMaxVertexAttributes);
     93 
     94     if (kGL_GrGLStandard == standard) {
     95         fUnpackRowLengthSupport = true;
     96         fUnpackFlipYSupport = false;
     97         fPackRowLengthSupport = true;
     98         fPackFlipYSupport = false;
     99     } else {
    100         fUnpackRowLengthSupport = version >= GR_GL_VER(3,0) ||
    101                                   ctxInfo.hasExtension("GL_EXT_unpack_subimage");
    102         fUnpackFlipYSupport = ctxInfo.hasExtension("GL_CHROMIUM_flipy");
    103         fPackRowLengthSupport = version >= GR_GL_VER(3,0) ||
    104                                 ctxInfo.hasExtension("GL_NV_pack_subimage");
    105         fPackFlipYSupport =
    106             ctxInfo.hasExtension("GL_ANGLE_pack_reverse_row_order");
    107     }
    108 
    109     fTextureUsageSupport = (kGLES_GrGLStandard == standard) &&
    110                             ctxInfo.hasExtension("GL_ANGLE_texture_usage");
    111 
    112     if (kGL_GrGLStandard == standard) {
    113         fTextureBarrierSupport = version >= GR_GL_VER(4,5) ||
    114                                  ctxInfo.hasExtension("GL_ARB_texture_barrier") ||
    115                                  ctxInfo.hasExtension("GL_NV_texture_barrier");
    116     } else {
    117         fTextureBarrierSupport = ctxInfo.hasExtension("GL_NV_texture_barrier");
    118     }
    119 
    120     if (kGL_GrGLStandard == standard) {
    121         fSampleLocationsSupport = version >= GR_GL_VER(3,2) ||
    122                                   ctxInfo.hasExtension("GL_ARB_texture_multisample");
    123     } else {
    124         fSampleLocationsSupport = version >= GR_GL_VER(3,1);
    125     }
    126 
    127     fImagingSupport = kGL_GrGLStandard == standard &&
    128                       ctxInfo.hasExtension("GL_ARB_imaging");
    129 
    130     if (((kGL_GrGLStandard == standard && version >= GR_GL_VER(4,3)) ||
    131          (kGLES_GrGLStandard == standard && version >= GR_GL_VER(3,0)) ||
    132          ctxInfo.hasExtension("GL_ARB_invalidate_subdata"))) {
    133         fDiscardRenderTargetSupport = true;
    134         fInvalidateFBType = kInvalidate_InvalidateFBType;
    135     } else if (ctxInfo.hasExtension("GL_EXT_discard_framebuffer")) {
    136         fDiscardRenderTargetSupport = true;
    137         fInvalidateFBType = kDiscard_InvalidateFBType;
    138     }
    139 
    140     // For future reference on Desktop GL, GL_PRIMITIVE_RESTART_FIXED_INDEX appears in 4.3, and
    141     // GL_PRIMITIVE_RESTART (where the client must call glPrimitiveRestartIndex) appears in 3.1.
    142     if (kGLES_GrGLStandard == standard) {
    143         // Primitive restart can cause a 3x slowdown on Adreno. Enable conservatively.
    144         // TODO: Evaluate on PowerVR.
    145         // FIXME: Primitive restart would likely be a win on iOS if we had an enum value for it.
    146         if (kARM_GrGLVendor == ctxInfo.vendor()) {
    147             fUsePrimitiveRestart = version >= GR_GL_VER(3,0);
    148         }
    149     }
    150 
    151     if (kARM_GrGLVendor == ctxInfo.vendor() ||
    152         kImagination_GrGLVendor == ctxInfo.vendor() ||
    153         kQualcomm_GrGLVendor == ctxInfo.vendor() ) {
    154         fPreferFullscreenClears = true;
    155     }
    156 
    157     if (kGL_GrGLStandard == standard) {
    158         fVertexArrayObjectSupport = version >= GR_GL_VER(3, 0) ||
    159                                     ctxInfo.hasExtension("GL_ARB_vertex_array_object") ||
    160                                     ctxInfo.hasExtension("GL_APPLE_vertex_array_object");
    161     } else {
    162         fVertexArrayObjectSupport = version >= GR_GL_VER(3, 0) ||
    163                                     ctxInfo.hasExtension("GL_OES_vertex_array_object");
    164     }
    165 
    166     if (kGL_GrGLStandard == standard) {
    167         fDirectStateAccessSupport = ctxInfo.hasExtension("GL_EXT_direct_state_access");
    168     } else {
    169         fDirectStateAccessSupport = false;
    170     }
    171 
    172     if (kGL_GrGLStandard == standard && version >= GR_GL_VER(4,3)) {
    173         fDebugSupport = true;
    174     } else {
    175         fDebugSupport = ctxInfo.hasExtension("GL_KHR_debug");
    176     }
    177 
    178     if (kGL_GrGLStandard == standard) {
    179         fES2CompatibilitySupport = ctxInfo.hasExtension("GL_ARB_ES2_compatibility");
    180     }
    181     else {
    182         fES2CompatibilitySupport = true;
    183     }
    184 
    185     if (kGL_GrGLStandard == standard) {
    186         fMultisampleDisableSupport = true;
    187     } else {
    188         fMultisampleDisableSupport = ctxInfo.hasExtension("GL_EXT_multisample_compatibility");
    189     }
    190 
    191     if (kGL_GrGLStandard == standard) {
    192         // 3.1 has draw_instanced but not instanced_arrays, for the time being we only care about
    193         // instanced arrays, but we could make this more granular if we wanted
    194         fInstanceAttribSupport =
    195                 version >= GR_GL_VER(3, 2) ||
    196                 (ctxInfo.hasExtension("GL_ARB_draw_instanced") &&
    197                  ctxInfo.hasExtension("GL_ARB_instanced_arrays"));
    198     } else {
    199         fInstanceAttribSupport =
    200                 version >= GR_GL_VER(3, 0) ||
    201                 (ctxInfo.hasExtension("GL_EXT_draw_instanced") &&
    202                  ctxInfo.hasExtension("GL_EXT_instanced_arrays"));
    203     }
    204 
    205     if (kGL_GrGLStandard == standard) {
    206         if (version >= GR_GL_VER(3, 0)) {
    207             fBindFragDataLocationSupport = true;
    208         }
    209     } else {
    210         if (version >= GR_GL_VER(3, 0) && ctxInfo.hasExtension("GL_EXT_blend_func_extended")) {
    211             fBindFragDataLocationSupport = true;
    212         }
    213     }
    214 
    215     fBindUniformLocationSupport = ctxInfo.hasExtension("GL_CHROMIUM_bind_uniform_location");
    216 
    217     if (kGL_GrGLStandard == standard) {
    218         if (version >= GR_GL_VER(3, 1) || ctxInfo.hasExtension("GL_ARB_texture_rectangle")) {
    219             // We also require textureSize() support for rectangle 2D samplers which was added in
    220             // GLSL 1.40.
    221             if (ctxInfo.glslGeneration() >= k140_GrGLSLGeneration) {
    222                 fRectangleTextureSupport = true;
    223             }
    224         }
    225     } else {
    226         // Command buffer exposes this in GL ES context for Chromium reasons,
    227         // but it should not be used. Also, at the time of writing command buffer
    228         // lacks TexImage2D support and ANGLE lacks GL ES 3.0 support.
    229     }
    230 
    231     if (kGL_GrGLStandard == standard) {
    232         if (version >= GR_GL_VER(3,3) || ctxInfo.hasExtension("GL_ARB_texture_swizzle")) {
    233             fTextureSwizzleSupport = true;
    234         }
    235     } else {
    236         if (version >= GR_GL_VER(3,0)) {
    237             fTextureSwizzleSupport = true;
    238         }
    239     }
    240 
    241     if (kGL_GrGLStandard == standard) {
    242         fMipMapLevelAndLodControlSupport = true;
    243     } else if (kGLES_GrGLStandard == standard) {
    244         if (version >= GR_GL_VER(3,0)) {
    245             fMipMapLevelAndLodControlSupport = true;
    246         }
    247     }
    248 
    249 #ifdef SK_BUILD_FOR_WIN
    250     // We're assuming that on Windows Chromium we're using ANGLE.
    251     bool isANGLE = kANGLE_GrGLDriver == ctxInfo.driver() ||
    252                    kChromium_GrGLDriver == ctxInfo.driver();
    253     // Angle has slow read/write pixel paths for 32bit RGBA (but fast for BGRA).
    254     fRGBA8888PixelsOpsAreSlow = isANGLE;
    255     // On DX9 ANGLE reading a partial FBO is slow. TODO: Check whether this is still true and
    256     // check DX11 ANGLE.
    257     fPartialFBOReadIsSlow = isANGLE;
    258 #endif
    259 
    260     bool isMESA = kMesa_GrGLDriver == ctxInfo.driver();
    261     bool isMAC = false;
    262 #ifdef SK_BUILD_FOR_MAC
    263     isMAC = true;
    264 #endif
    265 
    266     // Both mesa and mac have reduced performance if reading back an RGBA framebuffer as BGRA or
    267     // vis-versa.
    268     fRGBAToBGRAReadbackConversionsAreSlow = isMESA || isMAC;
    269 
    270     if (kGL_GrGLStandard == standard) {
    271         if (version >= GR_GL_VER(4,4) || ctxInfo.hasExtension("GL_ARB_clear_texture")) {
    272             fClearTextureSupport = true;
    273         }
    274     } else if (ctxInfo.hasExtension("GL_EXT_clear_texture")) {
    275         fClearTextureSupport = true;
    276     }
    277 
    278     /**************************************************************************
    279     * GrShaderCaps fields
    280     **************************************************************************/
    281 
    282     // This must be called after fCoreProfile is set on the GrGLCaps
    283     this->initGLSL(ctxInfo, gli);
    284     GrShaderCaps* shaderCaps = fShaderCaps.get();
    285 
    286     shaderCaps->fPathRenderingSupport = this->hasPathRenderingSupport(ctxInfo, gli);
    287 #if GR_TEST_UTILS
    288     if (contextOptions.fSuppressPathRendering) {
    289         shaderCaps->fPathRenderingSupport = false;
    290     }
    291 #endif
    292 
    293     // Enable supported shader-related caps
    294     if (kGL_GrGLStandard == standard) {
    295         shaderCaps->fDualSourceBlendingSupport = (ctxInfo.version() >= GR_GL_VER(3, 3) ||
    296             ctxInfo.hasExtension("GL_ARB_blend_func_extended")) &&
    297             GrGLSLSupportsNamedFragmentShaderOutputs(ctxInfo.glslGeneration());
    298 
    299         shaderCaps->fShaderDerivativeSupport = true;
    300 
    301         // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
    302         shaderCaps->fGeometryShaderSupport = ctxInfo.version() >= GR_GL_VER(3, 2) &&
    303             ctxInfo.glslGeneration() >= k150_GrGLSLGeneration;
    304         if (shaderCaps->fGeometryShaderSupport) {
    305             if (ctxInfo.glslGeneration() >= k400_GrGLSLGeneration) {
    306                 shaderCaps->fGSInvocationsSupport = true;
    307             } else if (ctxInfo.hasExtension("GL_ARB_gpu_shader5")) {
    308                 shaderCaps->fGSInvocationsSupport = true;
    309                 shaderCaps->fGSInvocationsExtensionString = "GL_ARB_gpu_shader5";
    310             }
    311         }
    312 
    313         shaderCaps->fIntegerSupport = ctxInfo.version() >= GR_GL_VER(3, 0) &&
    314             ctxInfo.glslGeneration() >= k130_GrGLSLGeneration;
    315     } else {
    316         shaderCaps->fDualSourceBlendingSupport = ctxInfo.hasExtension("GL_EXT_blend_func_extended");
    317 
    318         shaderCaps->fShaderDerivativeSupport = ctxInfo.version() >= GR_GL_VER(3, 0) ||
    319             ctxInfo.hasExtension("GL_OES_standard_derivatives");
    320 
    321         // Mali has support for geometry shaders, but in practice with ccpr they are slower than the
    322         // backup impl that only uses vertex shaders.
    323         if (kARM_GrGLVendor != ctxInfo.vendor()) {
    324             if (ctxInfo.version() >= GR_GL_VER(3,2)) {
    325                 shaderCaps->fGeometryShaderSupport = true;
    326             } else if (ctxInfo.hasExtension("GL_EXT_geometry_shader")) {
    327                 shaderCaps->fGeometryShaderSupport = true;
    328                 shaderCaps->fGeometryShaderExtensionString = "GL_EXT_geometry_shader";
    329             }
    330             shaderCaps->fGSInvocationsSupport = shaderCaps->fGeometryShaderSupport;
    331         }
    332 
    333         shaderCaps->fIntegerSupport = ctxInfo.version() >= GR_GL_VER(3, 0) &&
    334             ctxInfo.glslGeneration() >= k330_GrGLSLGeneration; // We use this value for GLSL ES 3.0.
    335     }
    336 
    337     // Protect ourselves against tracking huge amounts of texture state.
    338     static const uint8_t kMaxSaneSamplers = 32;
    339     GrGLint maxSamplers;
    340     GR_GL_GetIntegerv(gli, GR_GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &maxSamplers);
    341     shaderCaps->fMaxVertexSamplers = SkTMin<GrGLint>(kMaxSaneSamplers, maxSamplers);
    342     if (shaderCaps->fGeometryShaderSupport) {
    343         GR_GL_GetIntegerv(gli, GR_GL_MAX_GEOMETRY_TEXTURE_IMAGE_UNITS, &maxSamplers);
    344         shaderCaps->fMaxGeometrySamplers = SkTMin<GrGLint>(kMaxSaneSamplers, maxSamplers);
    345     }
    346     GR_GL_GetIntegerv(gli, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxSamplers);
    347     shaderCaps->fMaxFragmentSamplers = SkTMin<GrGLint>(kMaxSaneSamplers, maxSamplers);
    348     GR_GL_GetIntegerv(gli, GR_GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxSamplers);
    349     shaderCaps->fMaxCombinedSamplers = SkTMin<GrGLint>(kMaxSaneSamplers, maxSamplers);
    350 
    351     // This is all *very* approximate.
    352     switch (ctxInfo.vendor()) {
    353         case kNVIDIA_GrGLVendor:
    354             // We've seen a range from 100 x 100 (TegraK1, GTX660) up to 300 x 300 (GTX 1070)
    355             // but it doesn't clearly align with Pascal vs Maxwell vs Kepler.
    356             fShaderCaps->fDisableImageMultitexturingDstRectAreaThreshold = 150 * 150;
    357             break;
    358         case kImagination_GrGLVendor:
    359             // Two PowerVR Rogues, Nexus Player and Chromebook Cb5-312T (PowerVR GX6250), show that
    360             // it is always a win to use multitexturing.
    361             if (kPowerVRRogue_GrGLRenderer == ctxInfo.renderer()) {
    362                 fShaderCaps->fDisableImageMultitexturingDstRectAreaThreshold =
    363                         std::numeric_limits<size_t>::max();
    364             }
    365             break;
    366         case kATI_GrGLVendor:
    367             // So far no AMD GPU shows a performance difference. A tie goes to disabling
    368             // multitexturing for simplicity's sake.
    369             fShaderCaps->fDisableImageMultitexturingDstRectAreaThreshold = 0;
    370             break;
    371         default:
    372             break;
    373     }
    374 
    375     // SGX and Mali GPUs that are based on a tiled-deferred architecture that have trouble with
    376     // frequently changing VBOs. We've measured a performance increase using non-VBO vertex
    377     // data for dynamic content on these GPUs. Perhaps we should read the renderer string and
    378     // limit this decision to specific GPU families rather than basing it on the vendor alone.
    379     if (!GR_GL_MUST_USE_VBO &&
    380         !fIsCoreProfile &&
    381         (kARM_GrGLVendor == ctxInfo.vendor() ||
    382          kImagination_GrGLVendor == ctxInfo.vendor() ||
    383          kQualcomm_GrGLVendor == ctxInfo.vendor())) {
    384         fPreferClientSideDynamicBuffers = true;
    385     }
    386 
    387     if (!contextOptions.fAvoidStencilBuffers) {
    388         // To reduce surface area, if we avoid stencil buffers, we also disable MSAA.
    389         this->initFSAASupport(contextOptions, ctxInfo, gli);
    390         this->initStencilSupport(ctxInfo);
    391     }
    392 
    393     // Setup blit framebuffer
    394     if (kGL_GrGLStandard != ctxInfo.standard()) {
    395         if (ctxInfo.version() >= GR_GL_VER(3, 0)) {
    396             fBlitFramebufferFlags = kNoFormatConversionForMSAASrc_BlitFramebufferFlag |
    397                                     kNoMSAADst_BlitFramebufferFlag |
    398                                     kRectsMustMatchForMSAASrc_BlitFramebufferFlag;
    399         } else if (ctxInfo.hasExtension("GL_CHROMIUM_framebuffer_multisample") ||
    400                    ctxInfo.hasExtension("GL_ANGLE_framebuffer_blit")) {
    401             // The CHROMIUM extension uses the ANGLE version of glBlitFramebuffer and includes its
    402             // limitations.
    403             fBlitFramebufferFlags = kNoScalingOrMirroring_BlitFramebufferFlag |
    404                                     kResolveMustBeFull_BlitFrambufferFlag |
    405                                     kNoMSAADst_BlitFramebufferFlag |
    406                                     kNoFormatConversion_BlitFramebufferFlag |
    407                                     kRectsMustMatchForMSAASrc_BlitFramebufferFlag;
    408         }
    409     } else {
    410         if (fUsesMixedSamples ||
    411             ctxInfo.version() >= GR_GL_VER(3,0) ||
    412             ctxInfo.hasExtension("GL_ARB_framebuffer_object") ||
    413             ctxInfo.hasExtension("GL_EXT_framebuffer_blit")) {
    414             fBlitFramebufferFlags = 0;
    415         }
    416     }
    417 
    418     this->initBlendEqationSupport(ctxInfo);
    419 
    420     if (kGL_GrGLStandard == standard) {
    421         fMapBufferFlags = kCanMap_MapFlag; // we require VBO support and the desktop VBO
    422                                             // extension includes glMapBuffer.
    423         if (version >= GR_GL_VER(3, 0) || ctxInfo.hasExtension("GL_ARB_map_buffer_range")) {
    424             fMapBufferFlags |= kSubset_MapFlag;
    425             fMapBufferType = kMapBufferRange_MapBufferType;
    426         } else {
    427             fMapBufferType = kMapBuffer_MapBufferType;
    428         }
    429     } else {
    430         // Unextended GLES2 doesn't have any buffer mapping.
    431         fMapBufferFlags = kNone_MapBufferType;
    432         if (ctxInfo.hasExtension("GL_CHROMIUM_map_sub")) {
    433             fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
    434             fMapBufferType = kChromium_MapBufferType;
    435         } else if (version >= GR_GL_VER(3, 0) || ctxInfo.hasExtension("GL_EXT_map_buffer_range")) {
    436             fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
    437             fMapBufferType = kMapBufferRange_MapBufferType;
    438         } else if (ctxInfo.hasExtension("GL_OES_mapbuffer")) {
    439             fMapBufferFlags = kCanMap_MapFlag;
    440             fMapBufferType = kMapBuffer_MapBufferType;
    441         }
    442     }
    443 
    444     if (kGL_GrGLStandard == standard) {
    445         if (version >= GR_GL_VER(3, 0) || ctxInfo.hasExtension("GL_ARB_pixel_buffer_object")) {
    446             fTransferBufferType = kPBO_TransferBufferType;
    447         }
    448     } else {
    449         if (version >= GR_GL_VER(3, 0) ||
    450             (ctxInfo.hasExtension("GL_NV_pixel_buffer_object") &&
    451              // GL_EXT_unpack_subimage needed to support subtexture rectangles
    452              ctxInfo.hasExtension("GL_EXT_unpack_subimage"))) {
    453             fTransferBufferType = kPBO_TransferBufferType;
    454 // TODO: get transfer buffers working in Chrome
    455 //        } else if (ctxInfo.hasExtension("GL_CHROMIUM_pixel_transfer_buffer_object")) {
    456 //            fTransferBufferType = kChromium_TransferBufferType;
    457         }
    458     }
    459 
    460     // On many GPUs, map memory is very expensive, so we effectively disable it here by setting the
    461     // threshold to the maximum unless the client gives us a hint that map memory is cheap.
    462     if (fBufferMapThreshold < 0) {
    463 #if 0
    464         // We think mapping on Chromium will be cheaper once we know ahead of time how much space
    465         // we will use for all GrMeshDrawOps. Right now we might wind up mapping a large buffer and
    466         // using a small subset.
    467         fBufferMapThreshold = kChromium_GrGLDriver == ctxInfo.driver() ? 0 : SK_MaxS32;
    468 #else
    469         fBufferMapThreshold = SK_MaxS32;
    470 #endif
    471     }
    472 
    473     if (kGL_GrGLStandard == standard) {
    474         fNPOTTextureTileSupport = true;
    475         fMipMapSupport = true;
    476     } else {
    477         // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
    478         // ES3 has no limitations.
    479         fNPOTTextureTileSupport = ctxInfo.version() >= GR_GL_VER(3,0) ||
    480                                   ctxInfo.hasExtension("GL_OES_texture_npot");
    481         // ES2 supports MIP mapping for POT textures but our caps don't allow for limited MIP
    482         // support. The OES extension or ES 3.0 allow for MIPS on NPOT textures. So, apparently,
    483         // does the undocumented GL_IMG_texture_npot extension. This extension does not seem to
    484         // to alllow arbitrary wrap modes, however.
    485         fMipMapSupport = fNPOTTextureTileSupport || ctxInfo.hasExtension("GL_IMG_texture_npot");
    486     }
    487 
    488     GR_GL_GetIntegerv(gli, GR_GL_MAX_TEXTURE_SIZE, &fMaxTextureSize);
    489     GR_GL_GetIntegerv(gli, GR_GL_MAX_RENDERBUFFER_SIZE, &fMaxRenderTargetSize);
    490     // Our render targets are always created with textures as the color
    491     // attachment, hence this min:
    492     fMaxRenderTargetSize = SkTMin(fMaxTextureSize, fMaxRenderTargetSize);
    493 
    494     fGpuTracingSupport = ctxInfo.hasExtension("GL_EXT_debug_marker");
    495 
    496     // Disable scratch texture reuse on Mali and Adreno devices
    497     fReuseScratchTextures = kARM_GrGLVendor != ctxInfo.vendor();
    498 
    499 #if 0
    500     fReuseScratchBuffers = kARM_GrGLVendor != ctxInfo.vendor() &&
    501                            kQualcomm_GrGLVendor != ctxInfo.vendor();
    502 #endif
    503 
    504     if (ctxInfo.hasExtension("GL_EXT_window_rectangles")) {
    505         GR_GL_GetIntegerv(gli, GR_GL_MAX_WINDOW_RECTANGLES, &fMaxWindowRectangles);
    506     }
    507 
    508 #ifdef SK_BUILD_FOR_WIN
    509     // On ANGLE deferring flushes can lead to GPU starvation
    510     fPreferVRAMUseOverFlushes = !isANGLE;
    511 #endif
    512 
    513     if (kChromium_GrGLDriver == ctxInfo.driver()) {
    514         fMustClearUploadedBufferData = true;
    515     }
    516 
    517     if (kGL_GrGLStandard == standard) {
    518         // ARB allows mixed size FBO attachments, EXT does not.
    519         if (ctxInfo.version() >= GR_GL_VER(3, 0) ||
    520             ctxInfo.hasExtension("GL_ARB_framebuffer_object")) {
    521             fOversizedStencilSupport = true;
    522         } else {
    523             SkASSERT(ctxInfo.hasExtension("GL_EXT_framebuffer_object"));
    524         }
    525     } else {
    526         // ES 3.0 supports mixed size FBO attachments, 2.0 does not.
    527         fOversizedStencilSupport = ctxInfo.version() >= GR_GL_VER(3, 0);
    528     }
    529 
    530     if (kGL_GrGLStandard == standard) {
    531         fDrawIndirectSupport = version >= GR_GL_VER(4,0) ||
    532                                ctxInfo.hasExtension("GL_ARB_draw_indirect");
    533         fBaseInstanceSupport = version >= GR_GL_VER(4,2);
    534         fMultiDrawIndirectSupport = version >= GR_GL_VER(4,3) ||
    535                                     (fDrawIndirectSupport &&
    536                                      !fBaseInstanceSupport && // The ARB extension has no base inst.
    537                                      ctxInfo.hasExtension("GL_ARB_multi_draw_indirect"));
    538         fDrawRangeElementsSupport = version >= GR_GL_VER(2,0);
    539     } else {
    540         fDrawIndirectSupport = version >= GR_GL_VER(3,1);
    541         fMultiDrawIndirectSupport = fDrawIndirectSupport &&
    542                                     ctxInfo.hasExtension("GL_EXT_multi_draw_indirect");
    543         fBaseInstanceSupport = fDrawIndirectSupport &&
    544                                ctxInfo.hasExtension("GL_EXT_base_instance");
    545         fDrawRangeElementsSupport = version >= GR_GL_VER(3,0);
    546     }
    547 
    548     if (kGL_GrGLStandard == standard) {
    549         if ((version >= GR_GL_VER(4, 0) || ctxInfo.hasExtension("GL_ARB_sample_shading"))) {
    550             fSampleShadingSupport = true;
    551         }
    552     } else if (ctxInfo.hasExtension("GL_OES_sample_shading")) {
    553         fSampleShadingSupport = true;
    554     }
    555 
    556     // TODO: support CHROMIUM_sync_point and maybe KHR_fence_sync
    557     if (kGL_GrGLStandard == standard) {
    558         if (version >= GR_GL_VER(3, 2) || ctxInfo.hasExtension("GL_ARB_sync")) {
    559             fFenceSyncSupport = true;
    560         }
    561     } else if (version >= GR_GL_VER(3, 0) || ctxInfo.hasExtension("GL_APPLE_sync")) {
    562         fFenceSyncSupport = true;
    563     }
    564 
    565     // Safely moving textures between contexts requires fences.
    566     fCrossContextTextureSupport = fFenceSyncSupport;
    567 
    568     fSRGBDecodeDisableSupport = ctxInfo.hasExtension("GL_EXT_texture_sRGB_decode");
    569 
    570     fSRGBDecodeDisableAffectsMipmaps = fSRGBDecodeDisableSupport &&
    571         kChromium_GrGLDriver != ctxInfo.driver();
    572 
    573     if (kGL_GrGLStandard == standard) {
    574         if (version >= GR_GL_VER(4, 1)) {
    575             fProgramBinarySupport = true;
    576         }
    577     } else if (version >= GR_GL_VER(3, 0)) {
    578         fProgramBinarySupport = true;
    579     }
    580 
    581     // Requires fTextureRedSupport, fTextureSwizzleSupport, msaa support, ES compatibility have
    582     // already been detected.
    583     this->initConfigTable(contextOptions, ctxInfo, gli, shaderCaps);
    584 
    585     if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
    586         this->applyDriverCorrectnessWorkarounds(ctxInfo, contextOptions, shaderCaps);
    587     }
    588 
    589     this->applyOptionsOverrides(contextOptions);
    590     shaderCaps->applyOptionsOverrides(contextOptions);
    591 
    592     // For now these two are equivalent but we could have dst read in shader via some other method.
    593     shaderCaps->fDstReadInShaderSupport = shaderCaps->fFBFetchSupport;
    594 }
    595 
    596 const char* get_glsl_version_decl_string(GrGLStandard standard, GrGLSLGeneration generation,
    597                                          bool isCoreProfile) {
    598     switch (generation) {
    599         case k110_GrGLSLGeneration:
    600             if (kGLES_GrGLStandard == standard) {
    601                 // ES2s shader language is based on version 1.20 but is version
    602                 // 1.00 of the ES language.
    603                 return "#version 100\n";
    604             } else {
    605                 SkASSERT(kGL_GrGLStandard == standard);
    606                 return "#version 110\n";
    607             }
    608         case k130_GrGLSLGeneration:
    609             SkASSERT(kGL_GrGLStandard == standard);
    610             return "#version 130\n";
    611         case k140_GrGLSLGeneration:
    612             SkASSERT(kGL_GrGLStandard == standard);
    613             return "#version 140\n";
    614         case k150_GrGLSLGeneration:
    615             SkASSERT(kGL_GrGLStandard == standard);
    616             if (isCoreProfile) {
    617                 return "#version 150\n";
    618             } else {
    619                 return "#version 150 compatibility\n";
    620             }
    621         case k330_GrGLSLGeneration:
    622             if (kGLES_GrGLStandard == standard) {
    623                 return "#version 300 es\n";
    624             } else {
    625                 SkASSERT(kGL_GrGLStandard == standard);
    626                 if (isCoreProfile) {
    627                     return "#version 330\n";
    628                 } else {
    629                     return "#version 330 compatibility\n";
    630                 }
    631             }
    632         case k400_GrGLSLGeneration:
    633             SkASSERT(kGL_GrGLStandard == standard);
    634             if (isCoreProfile) {
    635                 return "#version 400\n";
    636             } else {
    637                 return "#version 400 compatibility\n";
    638             }
    639         case k420_GrGLSLGeneration:
    640             SkASSERT(kGL_GrGLStandard == standard);
    641             if (isCoreProfile) {
    642                 return "#version 420\n";
    643             }
    644             else {
    645                 return "#version 420 compatibility\n";
    646             }
    647         case k310es_GrGLSLGeneration:
    648             SkASSERT(kGLES_GrGLStandard == standard);
    649             return "#version 310 es\n";
    650         case k320es_GrGLSLGeneration:
    651             SkASSERT(kGLES_GrGLStandard == standard);
    652             return "#version 320 es\n";
    653     }
    654     return "<no version>";
    655 }
    656 
    657 bool is_float_fp32(const GrGLContextInfo& ctxInfo, const GrGLInterface* gli, GrGLenum precision) {
    658     if (kGLES_GrGLStandard != ctxInfo.standard() &&
    659         ctxInfo.version() < GR_GL_VER(4,1) &&
    660         !ctxInfo.hasExtension("GL_ARB_ES2_compatibility")) {
    661         // We're on a desktop GL that doesn't have precision info. Assume they're all 32bit float.
    662         return true;
    663     }
    664     // glGetShaderPrecisionFormat doesn't accept GL_GEOMETRY_SHADER as a shader type. Hopefully the
    665     // geometry shaders don't have lower precision than vertex and fragment.
    666     for (GrGLenum shader : {GR_GL_FRAGMENT_SHADER, GR_GL_VERTEX_SHADER}) {
    667         GrGLint range[2];
    668         GrGLint bits;
    669         GR_GL_GetShaderPrecisionFormat(gli, shader, precision, range, &bits);
    670         if (range[0] < 127 || range[1] < 127 || bits < 23) {
    671             return false;
    672         }
    673     }
    674     return true;
    675 }
    676 
    677 void GrGLCaps::initGLSL(const GrGLContextInfo& ctxInfo, const GrGLInterface* gli) {
    678     GrGLStandard standard = ctxInfo.standard();
    679     GrGLVersion version = ctxInfo.version();
    680 
    681     /**************************************************************************
    682     * Caps specific to GrShaderCaps
    683     **************************************************************************/
    684 
    685     GrShaderCaps* shaderCaps = fShaderCaps.get();
    686     shaderCaps->fGLSLGeneration = ctxInfo.glslGeneration();
    687     if (kGLES_GrGLStandard == standard) {
    688         if (ctxInfo.hasExtension("GL_EXT_shader_framebuffer_fetch")) {
    689             shaderCaps->fFBFetchNeedsCustomOutput = (version >= GR_GL_VER(3, 0));
    690             shaderCaps->fFBFetchSupport = true;
    691             shaderCaps->fFBFetchColorName = "gl_LastFragData[0]";
    692             shaderCaps->fFBFetchExtensionString = "GL_EXT_shader_framebuffer_fetch";
    693         }
    694         else if (ctxInfo.hasExtension("GL_NV_shader_framebuffer_fetch")) {
    695             // Actually, we haven't seen an ES3.0 device with this extension yet, so we don't know
    696             shaderCaps->fFBFetchNeedsCustomOutput = false;
    697             shaderCaps->fFBFetchSupport = true;
    698             shaderCaps->fFBFetchColorName = "gl_LastFragData[0]";
    699             shaderCaps->fFBFetchExtensionString = "GL_NV_shader_framebuffer_fetch";
    700         }
    701         else if (ctxInfo.hasExtension("GL_ARM_shader_framebuffer_fetch")) {
    702             // The arm extension also requires an additional flag which we will set onResetContext
    703             shaderCaps->fFBFetchNeedsCustomOutput = false;
    704             shaderCaps->fFBFetchSupport = true;
    705             shaderCaps->fFBFetchColorName = "gl_LastFragColorARM";
    706             shaderCaps->fFBFetchExtensionString = "GL_ARM_shader_framebuffer_fetch";
    707         }
    708         shaderCaps->fUsesPrecisionModifiers = true;
    709     }
    710 
    711     shaderCaps->fBindlessTextureSupport = ctxInfo.hasExtension("GL_NV_bindless_texture");
    712 
    713     if (kGL_GrGLStandard == standard) {
    714         shaderCaps->fFlatInterpolationSupport = ctxInfo.glslGeneration() >= k130_GrGLSLGeneration;
    715     } else {
    716         shaderCaps->fFlatInterpolationSupport =
    717             ctxInfo.glslGeneration() >= k330_GrGLSLGeneration; // This is the value for GLSL ES 3.0.
    718     }
    719     // Flat interpolation appears to be slow on Qualcomm GPUs (tested Adreno 405 and 530).
    720     shaderCaps->fPreferFlatInterpolation = shaderCaps->fFlatInterpolationSupport &&
    721                                            kQualcomm_GrGLVendor != ctxInfo.vendor();
    722     if (kGL_GrGLStandard == standard) {
    723         shaderCaps->fNoPerspectiveInterpolationSupport =
    724             ctxInfo.glslGeneration() >= k130_GrGLSLGeneration;
    725     } else {
    726         if (ctxInfo.hasExtension("GL_NV_shader_noperspective_interpolation")) {
    727             shaderCaps->fNoPerspectiveInterpolationSupport = true;
    728             shaderCaps->fNoPerspectiveInterpolationExtensionString =
    729                 "GL_NV_shader_noperspective_interpolation";
    730         }
    731     }
    732 
    733     if (kGL_GrGLStandard == standard) {
    734         shaderCaps->fMultisampleInterpolationSupport =
    735                 ctxInfo.glslGeneration() >= k400_GrGLSLGeneration;
    736     } else {
    737         if (ctxInfo.glslGeneration() >= k320es_GrGLSLGeneration) {
    738             shaderCaps->fMultisampleInterpolationSupport = true;
    739         } else if (ctxInfo.hasExtension("GL_OES_shader_multisample_interpolation")) {
    740             shaderCaps->fMultisampleInterpolationSupport = true;
    741             shaderCaps->fMultisampleInterpolationExtensionString =
    742                 "GL_OES_shader_multisample_interpolation";
    743         }
    744     }
    745 
    746     if (kGL_GrGLStandard == standard) {
    747         shaderCaps->fSampleVariablesSupport = ctxInfo.glslGeneration() >= k400_GrGLSLGeneration;
    748     } else {
    749         if (ctxInfo.glslGeneration() >= k320es_GrGLSLGeneration) {
    750             shaderCaps->fSampleVariablesSupport = true;
    751         } else if (ctxInfo.hasExtension("GL_OES_sample_variables")) {
    752             shaderCaps->fSampleVariablesSupport = true;
    753             shaderCaps->fSampleVariablesExtensionString = "GL_OES_sample_variables";
    754         }
    755     }
    756 
    757     if (shaderCaps->fSampleVariablesSupport &&
    758         ctxInfo.hasExtension("GL_NV_sample_mask_override_coverage")) {
    759         shaderCaps->fSampleMaskOverrideCoverageSupport = true;
    760     }
    761 
    762     shaderCaps->fVersionDeclString = get_glsl_version_decl_string(standard,
    763                                                                   shaderCaps->fGLSLGeneration,
    764                                                                   fIsCoreProfile);
    765 
    766     if (kGLES_GrGLStandard == standard && k110_GrGLSLGeneration == shaderCaps->fGLSLGeneration) {
    767         shaderCaps->fShaderDerivativeExtensionString = "GL_OES_standard_derivatives";
    768     }
    769 
    770     // Frag Coords Convention support is not part of ES
    771     if (kGLES_GrGLStandard != standard &&
    772         (ctxInfo.glslGeneration() >= k150_GrGLSLGeneration ||
    773          ctxInfo.hasExtension("GL_ARB_fragment_coord_conventions"))) {
    774         shaderCaps->fFragCoordConventionsExtensionString = "GL_ARB_fragment_coord_conventions";
    775     }
    776 
    777     if (kGLES_GrGLStandard == standard) {
    778         shaderCaps->fSecondaryOutputExtensionString = "GL_EXT_blend_func_extended";
    779     }
    780 
    781     if (ctxInfo.hasExtension("GL_OES_EGL_image_external")) {
    782         if (ctxInfo.glslGeneration() == k110_GrGLSLGeneration) {
    783             shaderCaps->fExternalTextureSupport = true;
    784         } else if (ctxInfo.hasExtension("GL_OES_EGL_image_external_essl3") ||
    785                    ctxInfo.hasExtension("OES_EGL_image_external_essl3")) {
    786             // At least one driver has been found that has this extension without the "GL_" prefix.
    787             shaderCaps->fExternalTextureSupport = true;
    788         }
    789     }
    790 
    791     if (shaderCaps->fExternalTextureSupport) {
    792         if (ctxInfo.glslGeneration() == k110_GrGLSLGeneration) {
    793             shaderCaps->fExternalTextureExtensionString = "GL_OES_EGL_image_external";
    794         } else {
    795             shaderCaps->fExternalTextureExtensionString = "GL_OES_EGL_image_external_essl3";
    796         }
    797     }
    798 
    799     if (kGL_GrGLStandard == standard) {
    800         shaderCaps->fTexelFetchSupport = ctxInfo.glslGeneration() >= k130_GrGLSLGeneration;
    801     } else {
    802         shaderCaps->fTexelFetchSupport =
    803             ctxInfo.glslGeneration() >= k330_GrGLSLGeneration; // We use this value for GLSL ES 3.0.
    804     }
    805 
    806     if (shaderCaps->fTexelFetchSupport) {
    807         if (kGL_GrGLStandard == standard) {
    808             shaderCaps->fTexelBufferSupport = ctxInfo.version() >= GR_GL_VER(3, 1) &&
    809                                             ctxInfo.glslGeneration() >= k330_GrGLSLGeneration;
    810         } else {
    811             if (ctxInfo.version() >= GR_GL_VER(3, 2) &&
    812                 ctxInfo.glslGeneration() >= k320es_GrGLSLGeneration) {
    813                 shaderCaps->fTexelBufferSupport = true;
    814             } else if (ctxInfo.hasExtension("GL_OES_texture_buffer")) {
    815                 shaderCaps->fTexelBufferSupport = true;
    816                 shaderCaps->fTexelBufferExtensionString = "GL_OES_texture_buffer";
    817             } else if (ctxInfo.hasExtension("GL_EXT_texture_buffer")) {
    818                 shaderCaps->fTexelBufferSupport = true;
    819                 shaderCaps->fTexelBufferExtensionString = "GL_EXT_texture_buffer";
    820             }
    821         }
    822     }
    823 
    824     if (kGL_GrGLStandard == standard) {
    825         shaderCaps->fVertexIDSupport = true;
    826     } else {
    827         // Desktop GLSL 3.30 == ES GLSL 3.00.
    828         shaderCaps->fVertexIDSupport = ctxInfo.glslGeneration() >= k330_GrGLSLGeneration;
    829     }
    830 
    831     shaderCaps->fFloatIs32Bits = is_float_fp32(ctxInfo, gli, GR_GL_HIGH_FLOAT);
    832     shaderCaps->fHalfIs32Bits = is_float_fp32(ctxInfo, gli, GR_GL_MEDIUM_FLOAT);
    833 }
    834 
    835 bool GrGLCaps::hasPathRenderingSupport(const GrGLContextInfo& ctxInfo, const GrGLInterface* gli) {
    836     bool hasChromiumPathRendering = ctxInfo.hasExtension("GL_CHROMIUM_path_rendering");
    837 
    838     if (!(ctxInfo.hasExtension("GL_NV_path_rendering") || hasChromiumPathRendering)) {
    839         return false;
    840     }
    841 
    842     if (kGL_GrGLStandard == ctxInfo.standard()) {
    843         if (ctxInfo.version() < GR_GL_VER(4, 3) &&
    844             !ctxInfo.hasExtension("GL_ARB_program_interface_query")) {
    845             return false;
    846         }
    847     } else {
    848         if (!hasChromiumPathRendering &&
    849             ctxInfo.version() < GR_GL_VER(3, 1)) {
    850             return false;
    851         }
    852     }
    853     // We only support v1.3+ of GL_NV_path_rendering which allows us to
    854     // set individual fragment inputs with ProgramPathFragmentInputGen. The API
    855     // additions are detected by checking the existence of the function.
    856     // We also use *Then* functions that not all drivers might have. Check
    857     // them for consistency.
    858     if (!gli->fFunctions.fStencilThenCoverFillPath ||
    859         !gli->fFunctions.fStencilThenCoverStrokePath ||
    860         !gli->fFunctions.fStencilThenCoverFillPathInstanced ||
    861         !gli->fFunctions.fStencilThenCoverStrokePathInstanced ||
    862         !gli->fFunctions.fProgramPathFragmentInputGen) {
    863         return false;
    864     }
    865     return true;
    866 }
    867 
    868 bool GrGLCaps::readPixelsSupported(GrPixelConfig surfaceConfig,
    869                                    GrPixelConfig readConfig,
    870                                    std::function<void (GrGLenum, GrGLint*)> getIntegerv,
    871                                    std::function<bool ()> bindRenderTarget,
    872                                    std::function<void ()> unbindRenderTarget) const {
    873     // If it's not possible to even have a color attachment of surfaceConfig then read pixels is
    874     // not supported regardless of readConfig.
    875     if (!this->canConfigBeFBOColorAttachment(surfaceConfig)) {
    876         return false;
    877     }
    878 
    879     if (GrPixelConfigIsSint(surfaceConfig) != GrPixelConfigIsSint(readConfig)) {
    880         return false;
    881     }
    882 
    883     GrGLenum readFormat;
    884     GrGLenum readType;
    885     if (!this->getReadPixelsFormat(surfaceConfig, readConfig, &readFormat, &readType)) {
    886         return false;
    887     }
    888 
    889     if (kGL_GrGLStandard == fStandard) {
    890         // Some OpenGL implementations allow GL_ALPHA as a format to glReadPixels. However,
    891         // the manual (https://www.opengl.org/sdk/docs/man/) says only these formats are allowed:
    892         // GL_STENCIL_INDEX, GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL, GL_RED, GL_GREEN, GL_BLUE,
    893         // GL_RGB, GL_BGR, GL_RGBA, and GL_BGRA. We check for the subset that we would use.
    894         // The manual does not seem to fully match the spec as the spec allows integer formats
    895         // when the bound color buffer is an integer buffer. It doesn't specify which integer
    896         // formats are allowed, so perhaps all of them are. We only use GL_RGBA_INTEGER currently.
    897         if (readFormat != GR_GL_RED && readFormat != GR_GL_RG && readFormat != GR_GL_RGB &&
    898             readFormat != GR_GL_RGBA && readFormat != GR_GL_BGRA &&
    899             readFormat != GR_GL_RGBA_INTEGER) {
    900             return false;
    901         }
    902         // There is also a set of allowed types, but all the types we use are in the set:
    903         // GL_UNSIGNED_BYTE, GL_BYTE, GL_UNSIGNED_SHORT, GL_SHORT, GL_UNSIGNED_INT, GL_INT,
    904         // GL_HALF_FLOAT, GL_FLOAT, GL_UNSIGNED_BYTE_3_3_2, GL_UNSIGNED_BYTE_2_3_3_REV,
    905         // GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_5_6_5_REV, GL_UNSIGNED_SHORT_4_4_4_4,
    906         // GL_UNSIGNED_SHORT_4_4_4_4_REV, GL_UNSIGNED_SHORT_5_5_5_1, GL_UNSIGNED_SHORT_1_5_5_5_REV,
    907         // GL_UNSIGNED_INT_8_8_8_8, GL_UNSIGNED_INT_8_8_8_8_REV,GL_UNSIGNED_INT_10_10_10_2,
    908         // GL_UNSIGNED_INT_2_10_10_10_REV, GL_UNSIGNED_INT_24_8, GL_UNSIGNED_INT_10F_11F_11F_REV,
    909         // GL_UNSIGNED_INT_5_9_9_9_REV, or GL_FLOAT_32_UNSIGNED_INT_24_8_REV.
    910         return true;
    911     }
    912 
    913     // See Section 16.1.2 in the ES 3.2 specification.
    914     switch (fConfigTable[surfaceConfig].fFormatType) {
    915         case kNormalizedFixedPoint_FormatType:
    916             if (GR_GL_RGBA == readFormat && GR_GL_UNSIGNED_BYTE == readType) {
    917                 return true;
    918             }
    919             break;
    920         case kInteger_FormatType:
    921             if (GR_GL_RGBA_INTEGER == readFormat && GR_GL_INT == readType) {
    922                 return true;
    923             }
    924             break;
    925         case kFloat_FormatType:
    926             if (GR_GL_RGBA == readFormat && GR_GL_FLOAT == readType) {
    927                 return true;
    928             }
    929             break;
    930     }
    931 
    932     if (0 == fConfigTable[surfaceConfig].fSecondReadPixelsFormat.fFormat) {
    933         ReadPixelsFormat* rpFormat =
    934             const_cast<ReadPixelsFormat*>(&fConfigTable[surfaceConfig].fSecondReadPixelsFormat);
    935         GrGLint format = 0, type = 0;
    936         if (!bindRenderTarget()) {
    937             return false;
    938         }
    939         getIntegerv(GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format);
    940         getIntegerv(GR_GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
    941         rpFormat->fFormat = format;
    942         rpFormat->fType = type;
    943         unbindRenderTarget();
    944     }
    945 
    946     return fConfigTable[surfaceConfig].fSecondReadPixelsFormat.fFormat == readFormat &&
    947            fConfigTable[surfaceConfig].fSecondReadPixelsFormat.fType == readType;
    948 }
    949 
    950 void GrGLCaps::initFSAASupport(const GrContextOptions& contextOptions, const GrGLContextInfo& ctxInfo,
    951                                const GrGLInterface* gli) {
    952     // We need dual source blending and the ability to disable multisample in order to support mixed
    953     // samples in every corner case. We only use mixed samples if the stencil-and-cover path
    954     // renderer is available and enabled; no other path renderers support this feature.
    955     if (fMultisampleDisableSupport &&
    956         this->shaderCaps()->dualSourceBlendingSupport() &&
    957         this->shaderCaps()->pathRenderingSupport()
    958 #if GR_TEST_UTILS
    959         && (contextOptions.fGpuPathRenderers & GpuPathRenderers::kStencilAndCover)
    960 #endif
    961         ) {
    962         fUsesMixedSamples = ctxInfo.hasExtension("GL_NV_framebuffer_mixed_samples") ||
    963                             ctxInfo.hasExtension("GL_CHROMIUM_framebuffer_mixed_samples");
    964     }
    965 
    966     if (kGL_GrGLStandard != ctxInfo.standard()) {
    967         if (ctxInfo.version() >= GR_GL_VER(3,0) &&
    968             ctxInfo.renderer() != kGalliumLLVM_GrGLRenderer) {
    969             // The gallium llvmpipe renderer for es3.0 does not have textureRed support even though
    970             // it is part of the spec. Thus alpha8 will not be renderable for those devices.
    971             fAlpha8IsRenderable = true;
    972         }
    973         // We prefer the EXT/IMG extension over ES3 MSAA because we've observed
    974         // ES3 driver bugs on at least one device with a tiled GPU (N10).
    975         if (ctxInfo.hasExtension("GL_EXT_multisampled_render_to_texture")) {
    976             fMSFBOType = kES_EXT_MsToTexture_MSFBOType;
    977         } else if (ctxInfo.hasExtension("GL_IMG_multisampled_render_to_texture")) {
    978             fMSFBOType = kES_IMG_MsToTexture_MSFBOType;
    979         } else if (fUsesMixedSamples) {
    980             fMSFBOType = kMixedSamples_MSFBOType;
    981         } else if (ctxInfo.version() >= GR_GL_VER(3,0)) {
    982             fMSFBOType = kStandard_MSFBOType;
    983         } else if (ctxInfo.hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
    984             fMSFBOType = kStandard_MSFBOType;
    985         } else if (ctxInfo.hasExtension("GL_ANGLE_framebuffer_multisample")) {
    986             fMSFBOType = kStandard_MSFBOType;
    987         } else if (ctxInfo.hasExtension("GL_APPLE_framebuffer_multisample")) {
    988             fMSFBOType = kES_Apple_MSFBOType;
    989         }
    990     } else {
    991         if (fUsesMixedSamples) {
    992             fMSFBOType = kMixedSamples_MSFBOType;
    993         } else if (ctxInfo.version() >= GR_GL_VER(3,0) ||
    994                    ctxInfo.hasExtension("GL_ARB_framebuffer_object")) {
    995 
    996             fMSFBOType = kStandard_MSFBOType;
    997             if (!fIsCoreProfile && ctxInfo.renderer() != kOSMesa_GrGLRenderer) {
    998                 // Core profile removes ALPHA8 support.
    999                 // OpenGL 3.0+ (and GL_ARB_framebuffer_object) supports ALPHA8 as renderable.
   1000                 // However, osmesa fails if it is used even when GL_ARB_framebuffer_object is
   1001                 // present.
   1002                 fAlpha8IsRenderable = true;
   1003             }
   1004         } else if (ctxInfo.hasExtension("GL_EXT_framebuffer_multisample") &&
   1005                    ctxInfo.hasExtension("GL_EXT_framebuffer_blit")) {
   1006             fMSFBOType = kStandard_MSFBOType;
   1007         }
   1008     }
   1009 
   1010     // We disable MSAA across the board for Intel GPUs for performance reasons.
   1011     if (kIntel_GrGLVendor == ctxInfo.vendor()) {
   1012         fMSFBOType = kNone_MSFBOType;
   1013     }
   1014 
   1015     // We only have a use for raster multisample if there is coverage modulation from mixed samples.
   1016     if (fUsesMixedSamples && ctxInfo.hasExtension("GL_EXT_raster_multisample")) {
   1017         GR_GL_GetIntegerv(gli, GR_GL_MAX_RASTER_SAMPLES, &fMaxRasterSamples);
   1018     }
   1019 }
   1020 
   1021 void GrGLCaps::initBlendEqationSupport(const GrGLContextInfo& ctxInfo) {
   1022     GrShaderCaps* shaderCaps = static_cast<GrShaderCaps*>(fShaderCaps.get());
   1023 
   1024     bool layoutQualifierSupport = false;
   1025     if ((kGL_GrGLStandard == fStandard && shaderCaps->generation() >= k140_GrGLSLGeneration)  ||
   1026         (kGLES_GrGLStandard == fStandard && shaderCaps->generation() >= k330_GrGLSLGeneration)) {
   1027         layoutQualifierSupport = true;
   1028     }
   1029 
   1030     if (ctxInfo.hasExtension("GL_NV_blend_equation_advanced_coherent")) {
   1031         fBlendEquationSupport = kAdvancedCoherent_BlendEquationSupport;
   1032         shaderCaps->fAdvBlendEqInteraction = GrShaderCaps::kAutomatic_AdvBlendEqInteraction;
   1033     } else if (ctxInfo.hasExtension("GL_KHR_blend_equation_advanced_coherent") &&
   1034                layoutQualifierSupport) {
   1035         fBlendEquationSupport = kAdvancedCoherent_BlendEquationSupport;
   1036         shaderCaps->fAdvBlendEqInteraction = GrShaderCaps::kGeneralEnable_AdvBlendEqInteraction;
   1037     } else if (ctxInfo.hasExtension("GL_NV_blend_equation_advanced")) {
   1038         fBlendEquationSupport = kAdvanced_BlendEquationSupport;
   1039         shaderCaps->fAdvBlendEqInteraction = GrShaderCaps::kAutomatic_AdvBlendEqInteraction;
   1040     } else if (ctxInfo.hasExtension("GL_KHR_blend_equation_advanced") && layoutQualifierSupport) {
   1041         fBlendEquationSupport = kAdvanced_BlendEquationSupport;
   1042         shaderCaps->fAdvBlendEqInteraction = GrShaderCaps::kGeneralEnable_AdvBlendEqInteraction;
   1043         // TODO: Use kSpecificEnables_AdvBlendEqInteraction if "blend_support_all_equations" is
   1044         // slow on a particular platform.
   1045     }
   1046 }
   1047 
   1048 namespace {
   1049 const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
   1050 }
   1051 
   1052 void GrGLCaps::initStencilSupport(const GrGLContextInfo& ctxInfo) {
   1053 
   1054     // Build up list of legal stencil formats (though perhaps not supported on
   1055     // the particular gpu/driver) from most preferred to least.
   1056 
   1057     // these consts are in order of most preferred to least preferred
   1058     // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
   1059 
   1060     static const StencilFormat
   1061                   // internal Format      stencil bits      total bits        packed?
   1062         gS8    = {GR_GL_STENCIL_INDEX8,   8,                8,                false},
   1063         gS16   = {GR_GL_STENCIL_INDEX16,  16,               16,               false},
   1064         gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8,                32,               true },
   1065         gS4    = {GR_GL_STENCIL_INDEX4,   4,                4,                false},
   1066     //  gS     = {GR_GL_STENCIL_INDEX,    kUnknownBitCount, kUnknownBitCount, false},
   1067         gDS    = {GR_GL_DEPTH_STENCIL,    kUnknownBitCount, kUnknownBitCount, true };
   1068 
   1069     if (kGL_GrGLStandard == ctxInfo.standard()) {
   1070         bool supportsPackedDS =
   1071             ctxInfo.version() >= GR_GL_VER(3,0) ||
   1072             ctxInfo.hasExtension("GL_EXT_packed_depth_stencil") ||
   1073             ctxInfo.hasExtension("GL_ARB_framebuffer_object");
   1074 
   1075         // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
   1076         // require FBO support we can expect these are legal formats and don't
   1077         // check. These also all support the unsized GL_STENCIL_INDEX.
   1078         fStencilFormats.push_back() = gS8;
   1079         fStencilFormats.push_back() = gS16;
   1080         if (supportsPackedDS) {
   1081             fStencilFormats.push_back() = gD24S8;
   1082         }
   1083         fStencilFormats.push_back() = gS4;
   1084         if (supportsPackedDS) {
   1085             fStencilFormats.push_back() = gDS;
   1086         }
   1087     } else {
   1088         // ES2 has STENCIL_INDEX8 without extensions but requires extensions
   1089         // for other formats.
   1090         // ES doesn't support using the unsized format.
   1091 
   1092         fStencilFormats.push_back() = gS8;
   1093         //fStencilFormats.push_back() = gS16;
   1094         if (ctxInfo.version() >= GR_GL_VER(3,0) ||
   1095             ctxInfo.hasExtension("GL_OES_packed_depth_stencil")) {
   1096             fStencilFormats.push_back() = gD24S8;
   1097         }
   1098         if (ctxInfo.hasExtension("GL_OES_stencil4")) {
   1099             fStencilFormats.push_back() = gS4;
   1100         }
   1101     }
   1102 }
   1103 
   1104 void GrGLCaps::onDumpJSON(SkJSONWriter* writer) const {
   1105 
   1106     // We are called by the base class, which has already called beginObject(). We choose to nest
   1107     // all of our caps information in a named sub-object.
   1108     writer->beginObject("GL caps");
   1109 
   1110     writer->beginArray("Stencil Formats");
   1111 
   1112     for (int i = 0; i < fStencilFormats.count(); ++i) {
   1113         writer->beginObject(nullptr, false);
   1114         writer->appendS32("stencil bits", fStencilFormats[i].fStencilBits);
   1115         writer->appendS32("total bits", fStencilFormats[i].fTotalBits);
   1116         writer->endObject();
   1117     }
   1118 
   1119     writer->endArray();
   1120 
   1121     static const char* kMSFBOExtStr[] = {
   1122         "None",
   1123         "Standard",
   1124         "Apple",
   1125         "IMG MS To Texture",
   1126         "EXT MS To Texture",
   1127         "MixedSamples",
   1128     };
   1129     GR_STATIC_ASSERT(0 == kNone_MSFBOType);
   1130     GR_STATIC_ASSERT(1 == kStandard_MSFBOType);
   1131     GR_STATIC_ASSERT(2 == kES_Apple_MSFBOType);
   1132     GR_STATIC_ASSERT(3 == kES_IMG_MsToTexture_MSFBOType);
   1133     GR_STATIC_ASSERT(4 == kES_EXT_MsToTexture_MSFBOType);
   1134     GR_STATIC_ASSERT(5 == kMixedSamples_MSFBOType);
   1135     GR_STATIC_ASSERT(SK_ARRAY_COUNT(kMSFBOExtStr) == kLast_MSFBOType + 1);
   1136 
   1137     static const char* kInvalidateFBTypeStr[] = {
   1138         "None",
   1139         "Discard",
   1140         "Invalidate",
   1141     };
   1142     GR_STATIC_ASSERT(0 == kNone_InvalidateFBType);
   1143     GR_STATIC_ASSERT(1 == kDiscard_InvalidateFBType);
   1144     GR_STATIC_ASSERT(2 == kInvalidate_InvalidateFBType);
   1145     GR_STATIC_ASSERT(SK_ARRAY_COUNT(kInvalidateFBTypeStr) == kLast_InvalidateFBType + 1);
   1146 
   1147     static const char* kMapBufferTypeStr[] = {
   1148         "None",
   1149         "MapBuffer",
   1150         "MapBufferRange",
   1151         "Chromium",
   1152     };
   1153     GR_STATIC_ASSERT(0 == kNone_MapBufferType);
   1154     GR_STATIC_ASSERT(1 == kMapBuffer_MapBufferType);
   1155     GR_STATIC_ASSERT(2 == kMapBufferRange_MapBufferType);
   1156     GR_STATIC_ASSERT(3 == kChromium_MapBufferType);
   1157     GR_STATIC_ASSERT(SK_ARRAY_COUNT(kMapBufferTypeStr) == kLast_MapBufferType + 1);
   1158 
   1159     writer->appendBool("Core Profile", fIsCoreProfile);
   1160     writer->appendString("MSAA Type", kMSFBOExtStr[fMSFBOType]);
   1161     writer->appendString("Invalidate FB Type", kInvalidateFBTypeStr[fInvalidateFBType]);
   1162     writer->appendString("Map Buffer Type", kMapBufferTypeStr[fMapBufferType]);
   1163     writer->appendS32("Max FS Uniform Vectors", fMaxFragmentUniformVectors);
   1164     writer->appendBool("Unpack Row length support", fUnpackRowLengthSupport);
   1165     writer->appendBool("Unpack Flip Y support", fUnpackFlipYSupport);
   1166     writer->appendBool("Pack Row length support", fPackRowLengthSupport);
   1167     writer->appendBool("Pack Flip Y support", fPackFlipYSupport);
   1168 
   1169     writer->appendBool("Texture Usage support", fTextureUsageSupport);
   1170     writer->appendBool("Alpha8 is renderable", fAlpha8IsRenderable);
   1171     writer->appendBool("GL_ARB_imaging support", fImagingSupport);
   1172     writer->appendBool("Vertex array object support", fVertexArrayObjectSupport);
   1173     writer->appendBool("Direct state access support", fDirectStateAccessSupport);
   1174     writer->appendBool("Debug support", fDebugSupport);
   1175     writer->appendBool("Draw indirect support", fDrawIndirectSupport);
   1176     writer->appendBool("Multi draw indirect support", fMultiDrawIndirectSupport);
   1177     writer->appendBool("Base instance support", fBaseInstanceSupport);
   1178     writer->appendBool("RGBA 8888 pixel ops are slow", fRGBA8888PixelsOpsAreSlow);
   1179     writer->appendBool("Partial FBO read is slow", fPartialFBOReadIsSlow);
   1180     writer->appendBool("Bind uniform location support", fBindUniformLocationSupport);
   1181     writer->appendBool("Rectangle texture support", fRectangleTextureSupport);
   1182     writer->appendBool("Texture swizzle support", fTextureSwizzleSupport);
   1183     writer->appendBool("BGRA to RGBA readback conversions are slow",
   1184                        fRGBAToBGRAReadbackConversionsAreSlow);
   1185     writer->appendBool("Draw To clear color", fUseDrawToClearColor);
   1186     writer->appendBool("Draw To clear stencil clip", fUseDrawToClearStencilClip);
   1187     writer->appendBool("Intermediate texture for partial updates of unorm textures ever bound to FBOs",
   1188                        fDisallowTexSubImageForUnormConfigTexturesEverBoundToFBO);
   1189     writer->appendBool("Intermediate texture for all updates of textures bound to FBOs",
   1190                        fUseDrawInsteadOfAllRenderTargetWrites);
   1191     writer->appendBool("Max instances per glDrawArraysInstanced without crashing (or zero)",
   1192                        fMaxInstancesPerDrawArraysWithoutCrashing);
   1193 
   1194     writer->beginArray("configs");
   1195 
   1196     for (int i = 0; i < kGrPixelConfigCnt; ++i) {
   1197         writer->beginObject(nullptr, false);
   1198         writer->appendHexU32("flags", fConfigTable[i].fFlags);
   1199         writer->appendHexU32("b_internal", fConfigTable[i].fFormats.fBaseInternalFormat);
   1200         writer->appendHexU32("s_internal", fConfigTable[i].fFormats.fSizedInternalFormat);
   1201         writer->appendHexU32("e_format",
   1202                              fConfigTable[i].fFormats.fExternalFormat[kOther_ExternalFormatUsage]);
   1203         writer->appendHexU32(
   1204                 "e_format_teximage",
   1205                 fConfigTable[i].fFormats.fExternalFormat[kTexImage_ExternalFormatUsage]);
   1206         writer->appendHexU32("e_type", fConfigTable[i].fFormats.fExternalType);
   1207         writer->appendHexU32("i_for_teximage", fConfigTable[i].fFormats.fInternalFormatTexImage);
   1208         writer->appendHexU32("i_for_renderbuffer",
   1209                              fConfigTable[i].fFormats.fInternalFormatRenderbuffer);
   1210         writer->endObject();
   1211     }
   1212 
   1213     writer->endArray();
   1214     writer->endObject();
   1215 }
   1216 
   1217 bool GrGLCaps::bgraIsInternalFormat() const {
   1218     return fConfigTable[kBGRA_8888_GrPixelConfig].fFormats.fBaseInternalFormat == GR_GL_BGRA;
   1219 }
   1220 
   1221 bool GrGLCaps::getTexImageFormats(GrPixelConfig surfaceConfig, GrPixelConfig externalConfig,
   1222                                   GrGLenum* internalFormat, GrGLenum* externalFormat,
   1223                                   GrGLenum* externalType) const {
   1224     if (!this->getExternalFormat(surfaceConfig, externalConfig, kTexImage_ExternalFormatUsage,
   1225                                  externalFormat, externalType)) {
   1226         return false;
   1227     }
   1228     *internalFormat = fConfigTable[surfaceConfig].fFormats.fInternalFormatTexImage;
   1229     return true;
   1230 }
   1231 
   1232 bool GrGLCaps::getReadPixelsFormat(GrPixelConfig surfaceConfig, GrPixelConfig externalConfig,
   1233                                    GrGLenum* externalFormat, GrGLenum* externalType) const {
   1234     if (!this->getExternalFormat(surfaceConfig, externalConfig, kOther_ExternalFormatUsage,
   1235                                  externalFormat, externalType)) {
   1236         return false;
   1237     }
   1238     return true;
   1239 }
   1240 
   1241 bool GrGLCaps::getRenderbufferFormat(GrPixelConfig config, GrGLenum* internalFormat) const {
   1242     *internalFormat = fConfigTable[config].fFormats.fInternalFormatRenderbuffer;
   1243     return true;
   1244 }
   1245 
   1246 bool GrGLCaps::getExternalFormat(GrPixelConfig surfaceConfig, GrPixelConfig memoryConfig,
   1247                                  ExternalFormatUsage usage, GrGLenum* externalFormat,
   1248                                  GrGLenum* externalType) const {
   1249     SkASSERT(externalFormat && externalType);
   1250 
   1251     bool surfaceIsAlphaOnly = GrPixelConfigIsAlphaOnly(surfaceConfig);
   1252     bool memoryIsAlphaOnly = GrPixelConfigIsAlphaOnly(memoryConfig);
   1253 
   1254     // We don't currently support moving RGBA data into and out of ALPHA surfaces. It could be
   1255     // made to work in many cases using glPixelStore and what not but is not needed currently.
   1256     if (surfaceIsAlphaOnly && !memoryIsAlphaOnly) {
   1257         return false;
   1258     }
   1259 
   1260     *externalFormat = fConfigTable[memoryConfig].fFormats.fExternalFormat[usage];
   1261     *externalType = fConfigTable[memoryConfig].fFormats.fExternalType;
   1262 
   1263     // When GL_RED is supported as a texture format, our alpha-only textures are stored using
   1264     // GL_RED and we swizzle in order to map all components to 'r'. However, in this case the
   1265     // surface is not alpha-only and we want alpha to really mean the alpha component of the
   1266     // texture, not the red component.
   1267     if (memoryIsAlphaOnly && !surfaceIsAlphaOnly) {
   1268         if (GR_GL_RED == *externalFormat) {
   1269             *externalFormat = GR_GL_ALPHA;
   1270         }
   1271     }
   1272 
   1273     return true;
   1274 }
   1275 
   1276 void GrGLCaps::initConfigTable(const GrContextOptions& contextOptions,
   1277                                const GrGLContextInfo& ctxInfo, const GrGLInterface* gli,
   1278                                GrShaderCaps* shaderCaps) {
   1279     /*
   1280         Comments on renderability of configs on various GL versions.
   1281           OpenGL < 3.0:
   1282             no built in support for render targets.
   1283             GL_EXT_framebuffer_object adds possible support for any sized format with base internal
   1284               format RGB, RGBA and NV float formats we don't use.
   1285               This is the following:
   1286                 R3_G3_B2, RGB4, RGB5, RGB8, RGB10, RGB12, RGB16, RGBA2, RGBA4, RGB5_A1, RGBA8
   1287                 RGB10_A2, RGBA12,RGBA16
   1288               Though, it is hard to believe the more obscure formats such as RGBA12 would work
   1289               since they aren't required by later standards and the driver can simply return
   1290               FRAMEBUFFER_UNSUPPORTED for anything it doesn't allow.
   1291             GL_ARB_framebuffer_object adds everything added by the EXT extension and additionally
   1292               any sized internal format with a base internal format of ALPHA, LUMINANCE,
   1293               LUMINANCE_ALPHA, INTENSITY, RED, and RG.
   1294               This adds a lot of additional renderable sized formats, including ALPHA8.
   1295               The GL_ARB_texture_rg brings in the RED and RG formats (8, 8I, 8UI, 16, 16I, 16UI,
   1296               16F, 32I, 32UI, and 32F variants).
   1297               Again, the driver has an escape hatch via FRAMEBUFFER_UNSUPPORTED.
   1298 
   1299             For both the above extensions we limit ourselves to those that are also required by
   1300             OpenGL 3.0.
   1301 
   1302           OpenGL 3.0:
   1303             Any format with base internal format ALPHA, RED, RG, RGB or RGBA is "color-renderable"
   1304             but are not required to be supported as renderable textures/renderbuffer.
   1305             Required renderable color formats:
   1306                 - RGBA32F, RGBA32I, RGBA32UI, RGBA16, RGBA16F, RGBA16I,
   1307                   RGBA16UI, RGBA8, RGBA8I, RGBA8UI, SRGB8_ALPHA8, and
   1308                   RGB10_A2.
   1309                 - R11F_G11F_B10F.
   1310                 - RG32F, RG32I, RG32UI, RG16, RG16F, RG16I, RG16UI, RG8, RG8I,
   1311                   and RG8UI.
   1312                 - R32F, R32I, R32UI, R16F, R16I, R16UI, R16, R8, R8I, and R8UI.
   1313                 - ALPHA8
   1314 
   1315           OpenGL 3.1, 3.2, 3.3
   1316             Same as 3.0 except ALPHA8 requires GL_ARB_compatibility/compatibility profile.
   1317           OpengGL 3.3, 4.0, 4.1
   1318             Adds RGB10_A2UI.
   1319           OpengGL 4.2
   1320             Adds
   1321                 - RGB5_A1, RGBA4
   1322                 - RGB565
   1323           OpenGL 4.4
   1324             Does away with the separate list and adds a column to the sized internal color format
   1325             table. However, no new formats become required color renderable.
   1326 
   1327           ES 2.0
   1328             color renderable: RGBA4, RGB5_A1, RGB565
   1329             GL_EXT_texture_rg adds support for R8, RG5 as a color render target
   1330             GL_OES_rgb8_rgba8 adds support for RGB8 and RGBA8
   1331             GL_ARM_rgba8 adds support for RGBA8 (but not RGB8)
   1332             GL_EXT_texture_format_BGRA8888 does not add renderbuffer support
   1333             GL_CHROMIUM_renderbuffer_format_BGRA8888 adds BGRA8 as color-renderable
   1334             GL_APPLE_texture_format_BGRA8888 does not add renderbuffer support
   1335 
   1336           ES 3.0
   1337                 - RGBA32I, RGBA32UI, RGBA16I, RGBA16UI, RGBA8, RGBA8I,
   1338                   RGBA8UI, SRGB8_ALPHA8, RGB10_A2, RGB10_A2UI, RGBA4, and
   1339                   RGB5_A1.
   1340                 - RGB8 and RGB565.
   1341                 - RG32I, RG32UI, RG16I, RG16UI, RG8, RG8I, and RG8UI.
   1342                 - R32I, R32UI, R16I, R16UI, R8, R8I, and R8UI
   1343           ES 3.1
   1344             Adds RGB10_A2, RGB10_A2UI,
   1345           ES 3.2
   1346             Adds R16F, RG16F, RGBA16F, R32F, RG32F, RGBA32F, R11F_G11F_B10F.
   1347     */
   1348 
   1349     // Correctness workarounds.
   1350     bool disableTextureRedForMesa = false;
   1351     bool disableSRGBForX86PowerVR = false;
   1352     bool disableSRGBWriteControlForAdreno4xx = false;
   1353     bool disableR8TexStorageForANGLEGL = false;
   1354     bool disableSRGBRenderWithMSAAForMacAMD = false;
   1355 
   1356     if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
   1357         // ARB_texture_rg is part of OpenGL 3.0, but osmesa doesn't support GL_RED
   1358         // and GL_RG on FBO textures.
   1359         disableTextureRedForMesa = kOSMesa_GrGLRenderer == ctxInfo.renderer();
   1360 
   1361         bool isX86PowerVR = false;
   1362 #if defined(SK_CPU_X86)
   1363         if (kPowerVRRogue_GrGLRenderer == ctxInfo.renderer()) {
   1364             isX86PowerVR = true;
   1365         }
   1366 #endif
   1367         // NexusPlayer has strange bugs with sRGB (skbug.com/4148). This is a targeted fix to
   1368         // blacklist that device (and any others that might be sharing the same driver).
   1369         disableSRGBForX86PowerVR = isX86PowerVR;
   1370         disableSRGBWriteControlForAdreno4xx = kAdreno4xx_GrGLRenderer == ctxInfo.renderer();
   1371 
   1372         // Angle with es2->GL has a bug where it will hang trying to call TexSubImage on GL_R8
   1373         // formats on miplevels > 0. We already disable texturing on gles > 2.0 so just need to
   1374         // check that we are not going to OpenGL.
   1375         disableR8TexStorageForANGLEGL = GrGLANGLEBackend::kOpenGL == ctxInfo.angleBackend();
   1376 
   1377         // MacPro devices with AMD cards fail to create MSAA sRGB render buffers.
   1378 #if defined(SK_BUILD_FOR_MAC)
   1379         disableSRGBRenderWithMSAAForMacAMD = kATI_GrGLVendor == ctxInfo.vendor();
   1380 #endif
   1381     }
   1382 
   1383     uint32_t nonMSAARenderFlags = ConfigInfo::kRenderable_Flag |
   1384                                   ConfigInfo::kFBOColorAttachment_Flag;
   1385     uint32_t allRenderFlags = nonMSAARenderFlags;
   1386     if (kNone_MSFBOType != fMSFBOType) {
   1387         allRenderFlags |= ConfigInfo::kRenderableWithMSAA_Flag;
   1388     }
   1389     GrGLStandard standard = ctxInfo.standard();
   1390     GrGLVersion version = ctxInfo.version();
   1391 
   1392     bool texStorageSupported = false;
   1393     if (kGL_GrGLStandard == standard) {
   1394         // The EXT version can apply to either GL or GLES.
   1395         texStorageSupported = version >= GR_GL_VER(4,2) ||
   1396                               ctxInfo.hasExtension("GL_ARB_texture_storage") ||
   1397                               ctxInfo.hasExtension("GL_EXT_texture_storage");
   1398     } else {
   1399         texStorageSupported = version >= GR_GL_VER(3,0) ||
   1400                               ctxInfo.hasExtension("GL_EXT_texture_storage");
   1401     }
   1402 
   1403     bool texelBufferSupport = this->shaderCaps()->texelBufferSupport();
   1404 
   1405     bool textureRedSupport = false;
   1406 
   1407     if (!disableTextureRedForMesa) {
   1408         if (kGL_GrGLStandard == standard) {
   1409             textureRedSupport =
   1410                     version >= GR_GL_VER(3, 0) || ctxInfo.hasExtension("GL_ARB_texture_rg");
   1411         } else {
   1412             textureRedSupport =
   1413                     version >= GR_GL_VER(3, 0) || ctxInfo.hasExtension("GL_EXT_texture_rg");
   1414         }
   1415     }
   1416 
   1417     fConfigTable[kUnknown_GrPixelConfig].fFormats.fBaseInternalFormat = 0;
   1418     fConfigTable[kUnknown_GrPixelConfig].fFormats.fSizedInternalFormat = 0;
   1419     fConfigTable[kUnknown_GrPixelConfig].fFormats.fExternalFormat[kOther_ExternalFormatUsage] = 0;
   1420     fConfigTable[kUnknown_GrPixelConfig].fFormats.fExternalType = 0;
   1421     fConfigTable[kUnknown_GrPixelConfig].fFormatType = kNormalizedFixedPoint_FormatType;
   1422     fConfigTable[kUnknown_GrPixelConfig].fSwizzle = GrSwizzle::RGBA();
   1423 
   1424     fConfigTable[kRGBA_8888_GrPixelConfig].fFormats.fBaseInternalFormat = GR_GL_RGBA;
   1425     fConfigTable[kRGBA_8888_GrPixelConfig].fFormats.fSizedInternalFormat = GR_GL_RGBA8;
   1426     fConfigTable[kRGBA_8888_GrPixelConfig].fFormats.fExternalFormat[kOther_ExternalFormatUsage] =
   1427         GR_GL_RGBA;
   1428     fConfigTable[kRGBA_8888_GrPixelConfig].fFormats.fExternalType = GR_GL_UNSIGNED_BYTE;
   1429     fConfigTable[kRGBA_8888_GrPixelConfig].fFormatType = kNormalizedFixedPoint_FormatType;
   1430     fConfigTable[kRGBA_8888_GrPixelConfig].fFlags = ConfigInfo::kTextureable_Flag;
   1431     if (kGL_GrGLStandard == standard) {
   1432         // We require some form of FBO support and all GLs with FBO support can render to RGBA8
   1433         fConfigTable[kRGBA_8888_GrPixelConfig].fFlags |= allRenderFlags;
   1434     } else {
   1435         if (version >= GR_GL_VER(3,0) || ctxInfo.hasExtension("GL_OES_rgb8_rgba8") ||
   1436             ctxInfo.hasExtension("GL_ARM_rgba8")) {
   1437             fConfigTable[kRGBA_8888_GrPixelConfig].fFlags |= allRenderFlags;
   1438         }
   1439     }
   1440     if (texStorageSupported) {
   1441         fConfigTable[kRGBA_8888_GrPixelConfig].fFlags |= ConfigInfo::kCanUseTexStorage_Flag;
   1442     }
   1443     if (texelBufferSupport) {
   1444         fConfigTable[kRGBA_8888_GrPixelConfig].fFlags |= ConfigInfo::kCanUseWithTexelBuffer_Flag;
   1445     }
   1446     fConfigTable[kRGBA_8888_GrPixelConfig].fSwizzle = GrSwizzle::RGBA();
   1447 
   1448     fConfigTable[kBGRA_8888_GrPixelConfig].fFormats.fExternalFormat[kOther_ExternalFormatUsage] =
   1449         GR_GL_BGRA;
   1450     fConfigTable[kBGRA_8888_GrPixelConfig].fFormats.fExternalType  = GR_GL_UNSIGNED_BYTE;
   1451     fConfigTable[kBGRA_8888_GrPixelConfig].fFormatType = kNormalizedFixedPoint_FormatType;
   1452 
   1453    // TexStorage requires using a sized internal format and BGRA8 is only supported if we have the
   1454    // GL_APPLE_texture_format_BGRA8888 extension or if we have GL_EXT_texutre_storage and
   1455    // GL_EXT_texture_format_BGRA8888.
   1456     bool supportsBGRATexStorage = false;
   1457 
   1458     if (kGL_GrGLStandard == standard) {
   1459         fConfigTable[kBGRA_8888_GrPixelConfig].fFormats.fBaseInternalFormat = GR_GL_RGBA;
   1460         fConfigTable[kBGRA_8888_GrPixelConfig].fFormats.fSizedInternalFormat = GR_GL_RGBA8;
   1461         if (version >= GR_GL_VER(1, 2) || ctxInfo.hasExtension("GL_EXT_bgra")) {
   1462             // Since the internal format is RGBA8, it is also renderable.
   1463             fConfigTable[kBGRA_8888_GrPixelConfig].fFlags = ConfigInfo::kTextureable_Flag |
   1464                                                             allRenderFlags;
   1465         }
   1466         // Since we are using RGBA8 we can use tex storage.
   1467         supportsBGRATexStorage = true;
   1468     } else {
   1469         fConfigTable[kBGRA_8888_GrPixelConfig].fFormats.fBaseInternalFormat = GR_GL_BGRA;
   1470         fConfigTable[kBGRA_8888_GrPixelConfig].fFormats.fSizedInternalFormat = GR_GL_BGRA8;
   1471         if (ctxInfo.hasExtension("GL_EXT_texture_format_BGRA8888")) {
   1472             fConfigTable[kBGRA_8888_GrPixelConfig].fFlags = ConfigInfo::kTextureable_Flag |
   1473                                                             nonMSAARenderFlags;
   1474 
   1475             if (ctxInfo.hasExtension("GL_EXT_texture_storage")) {
   1476                 supportsBGRATexStorage = true;
   1477             }
   1478             if (ctxInfo.hasExtension("GL_CHROMIUM_renderbuffer_format_BGRA8888") &&
   1479                 (this->usesMSAARenderBuffers() || this->fMSFBOType == kMixedSamples_MSFBOType)) {
   1480                 fConfigTable[kBGRA_8888_GrPixelConfig].fFlags |=
   1481                     ConfigInfo::kRenderableWithMSAA_Flag;
   1482             }
   1483         } else if (ctxInfo.hasExtension("GL_APPLE_texture_format_BGRA8888")) {
   1484             // This APPLE extension introduces complexity on ES2. It leaves the internal format
   1485             // as RGBA, but allows BGRA as the external format. From testing, it appears that the
   1486             // driver remembers the external format when the texture is created (with TexImage).
   1487             // If you then try to upload data in the other swizzle (with TexSubImage), it fails.
   1488             // We could work around this, but it adds even more state tracking to code that is
   1489             // already too tricky. Instead, we opt not to support BGRA on ES2 with this extension.
   1490             // This also side-steps some ambiguous interactions with the texture storage extension.
   1491             if (version >= GR_GL_VER(3,0)) {
   1492                 // The APPLE extension doesn't make this renderable.
   1493                 fConfigTable[kBGRA_8888_GrPixelConfig].fFlags = ConfigInfo::kTextureable_Flag;
   1494                 supportsBGRATexStorage = true;
   1495             }
   1496         }
   1497     }
   1498 
   1499     if (texStorageSupported && supportsBGRATexStorage) {
   1500         fConfigTable[kBGRA_8888_GrPixelConfig].fFlags |= ConfigInfo::kCanUseTexStorage_Flag;
   1501     }
   1502     fConfigTable[kBGRA_8888_GrPixelConfig].fSwizzle = GrSwizzle::RGBA();
   1503 
   1504     // We only enable srgb support if both textures and FBOs support srgb,
   1505     // *and* we can disable sRGB decode-on-read, to support "legacy" mode.
   1506     if (kGL_GrGLStandard == standard) {
   1507         if (ctxInfo.version() >= GR_GL_VER(3,0)) {
   1508             fSRGBSupport = true;
   1509         } else if (ctxInfo.hasExtension("GL_EXT_texture_sRGB")) {
   1510             if (ctxInfo.hasExtension("GL_ARB_framebuffer_sRGB") ||
   1511                 ctxInfo.hasExtension("GL_EXT_framebuffer_sRGB")) {
   1512                 fSRGBSupport = true;
   1513             }
   1514         }
   1515         // All the above srgb extensions support toggling srgb writes
   1516         if (fSRGBSupport) {
   1517             fSRGBWriteControl = true;
   1518         }
   1519     } else {
   1520         fSRGBSupport = ctxInfo.version() >= GR_GL_VER(3,0) || ctxInfo.hasExtension("GL_EXT_sRGB");
   1521         if (disableSRGBForX86PowerVR) {
   1522             fSRGBSupport = false;
   1523         }
   1524         // ES through 3.1 requires EXT_srgb_write_control to support toggling
   1525         // sRGB writing for destinations.
   1526         // See https://bug.skia.org/5329 for Adreno4xx issue.
   1527         fSRGBWriteControl = !disableSRGBWriteControlForAdreno4xx &&
   1528             ctxInfo.hasExtension("GL_EXT_sRGB_write_control");
   1529     }
   1530     if (contextOptions.fRequireDecodeDisableForSRGB && !fSRGBDecodeDisableSupport) {
   1531         // To support "legacy" L32 mode, we require the ability to turn off sRGB decode. Clients
   1532         // can opt-out of that requirement, if they intend to always do linear blending.
   1533         fSRGBSupport = false;
   1534     }
   1535 
   1536     // This is very conservative, if we're on a platform where N32 is BGRA, and using ES, disable
   1537     // all sRGB support. Too much code relies on creating surfaces with N32 + sRGB colorspace,
   1538     // and sBGRA is basically impossible to support on any version of ES (with our current code).
   1539     // In particular, ES2 doesn't support sBGRA at all, and even in ES3, there is no valid pair
   1540     // of formats that can be used for TexImage calls to upload BGRA data to sRGBA (which is what
   1541     // we *have* to use as the internal format, because sBGRA doesn't exist). This primarily
   1542     // affects Windows.
   1543     if (kSkia8888_GrPixelConfig == kBGRA_8888_GrPixelConfig && kGLES_GrGLStandard == standard) {
   1544         fSRGBSupport = false;
   1545     }
   1546 
   1547     // ES2 Command Buffer has several TexStorage restrictions. It appears to fail for any format
   1548     // not explicitly allowed by GL_EXT_texture_storage, particularly those from other extensions.
   1549     bool isCommandBufferES2 = kChromium_GrGLDriver == ctxInfo.driver() && version < GR_GL_VER(3, 0);
   1550 
   1551     uint32_t srgbRenderFlags = allRenderFlags;
   1552     if (disableSRGBRenderWithMSAAForMacAMD) {
   1553         srgbRenderFlags &= ~ConfigInfo::kRenderableWithMSAA_Flag;
   1554     }
   1555 
   1556     fConfigTable[kSRGBA_8888_GrPixelConfig].fFormats.fBaseInternalFormat = GR_GL_SRGB_ALPHA;
   1557     fConfigTable[kSRGBA_8888_GrPixelConfig].fFormats.fSizedInternalFormat = GR_GL_SRGB8_ALPHA8;
   1558     // GL does not do srgb<->rgb conversions when transferring between cpu and gpu. Thus, the
   1559     // external format is GL_RGBA. See below for note about ES2.0 and glTex[Sub]Image.
   1560     fConfigTable[kSRGBA_8888_GrPixelConfig].fFormats.fExternalFormat[kOther_ExternalFormatUsage] =
   1561         GR_GL_RGBA;
   1562     fConfigTable[kSRGBA_8888_GrPixelConfig].fFormats.fExternalType = GR_GL_UNSIGNED_BYTE;
   1563     fConfigTable[kSRGBA_8888_GrPixelConfig].fFormatType = kNormalizedFixedPoint_FormatType;
   1564     if (fSRGBSupport) {
   1565         fConfigTable[kSRGBA_8888_GrPixelConfig].fFlags = ConfigInfo::kTextureable_Flag |
   1566                                                          srgbRenderFlags;
   1567     }
   1568     // ES2 Command Buffer does not allow TexStorage with SRGB8_ALPHA8_EXT
   1569     if (texStorageSupported && !isCommandBufferES2) {
   1570         fConfigTable[kSRGBA_8888_GrPixelConfig].fFlags |= ConfigInfo::kCanUseTexStorage_Flag;
   1571     }
   1572     fConfigTable[kSRGBA_8888_GrPixelConfig].fSwizzle = GrSwizzle::RGBA();
   1573     // sBGRA is not a "real" thing in OpenGL, but GPUs support it, and on platforms where
   1574     // kN32 == BGRA, we need some way to work with it. (The default framebuffer on Windows
   1575     // is in this format, for example).
   1576     fConfigTable[kSBGRA_8888_GrPixelConfig].fFormats.fBaseInternalFormat = GR_GL_SRGB_ALPHA;
   1577     fConfigTable[kSBGRA_8888_GrPixelConfig].fFormats.fSizedInternalFormat = GR_GL_SRGB8_ALPHA8;
   1578     // GL does not do srgb<->rgb conversions when transferring between cpu and gpu. Thus, the
   1579     // external format is GL_BGRA.
   1580     fConfigTable[kSBGRA_8888_GrPixelConfig].fFormats.fExternalFormat[kOther_ExternalFormatUsage] =
   1581         GR_GL_BGRA;
   1582     fConfigTable[kSBGRA_8888_GrPixelConfig].fFormats.fExternalType = GR_GL_UNSIGNED_BYTE;
   1583     fConfigTable[kSBGRA_8888_GrPixelConfig].fFormatType = kNormalizedFixedPoint_FormatType;
   1584     if (fSRGBSupport && kGL_GrGLStandard == standard) {
   1585         fConfigTable[kSBGRA_8888_GrPixelConfig].fFlags = ConfigInfo::kTextureable_Flag |
   1586                                                          srgbRenderFlags;
   1587     }
   1588 
   1589     if (texStorageSupported) {
   1590         fConfigTable[kSBGRA_8888_GrPixelConfig].fFlags |= ConfigInfo::kCanUseTexStorage_Flag;
   1591     }
   1592     fConfigTable[kSBGRA_8888_GrPixelConfig].fSwizzle = GrSwizzle::RGBA();
   1593 
   1594     bool hasIntegerTextures;
   1595     if (standard == kGL_GrGLStandard) {
   1596         hasIntegerTextures = version >= GR_GL_VER(3, 0) ||
   1597                              ctxInfo.hasExtension("GL_EXT_texture_integer");
   1598     } else {
   1599         hasIntegerTextures = (version >= GR_GL_VER(3, 0));
   1600     }
   1601     // We may have limited GLSL to an earlier version that doesn't have integer sampler types.
   1602     if (ctxInfo.glslGeneration() == k110_GrGLSLGeneration) {
   1603         hasIntegerTextures = false;
   1604     }
   1605     fConfigTable[kRGBA_8888_sint_GrPixelConfig].fFormats.fBaseInternalFormat  = GR_GL_RGBA_INTEGER;
   1606     fConfigTable[kRGBA_8888_sint_GrPixelConfig].fFormats.fSizedInternalFormat = GR_GL_RGBA8I;
   1607     fConfigTable[kRGBA_8888_sint_GrPixelConfig].fFormats.fExternalFormat[kOther_ExternalFormatUsage] = GR_GL_RGBA_INTEGER;
   1608     fConfigTable[kRGBA_8888_sint_GrPixelConfig].fFormats.fExternalType = GR_GL_BYTE;
   1609     fConfigTable[kRGBA_8888_sint_GrPixelConfig].fFormatType = kInteger_FormatType;
   1610     // We currently only support using integer textures as srcs, not for rendering (even though GL
   1611     // allows it).
   1612     if (hasIntegerTextures) {
   1613         fConfigTable[kRGBA_8888_sint_GrPixelConfig].fFlags = ConfigInfo::kTextureable_Flag |
   1614                                                              ConfigInfo::kFBOColorAttachment_Flag;
   1615         if (texStorageSupported) {
   1616             fConfigTable[kRGBA_8888_sint_GrPixelConfig].fFlags |=
   1617                 ConfigInfo::kCanUseTexStorage_Flag;
   1618         }
   1619     }
   1620 
   1621     fConfigTable[kRGB_565_GrPixelConfig].fFormats.fBaseInternalFormat = GR_GL_RGB;
   1622     if (this->ES2CompatibilitySupport()) {
   1623         fConfigTable[kRGB_565_GrPixelConfig].fFormats.fSizedInternalFormat = GR_GL_RGB565;
   1624     } else {
   1625         fConfigTable[kRGB_565_GrPixelConfig].fFormats.fSizedInternalFormat = GR_GL_RGB5;
   1626     }
   1627     fConfigTable[kRGB_565_GrPixelConfig].fFormats.fExternalFormat[kOther_ExternalFormatUsage] =
   1628         GR_GL_RGB;
   1629     fConfigTable[kRGB_565_GrPixelConfig].fFormats.fExternalType = GR_GL_UNSIGNED_SHORT_5_6_5;
   1630     fConfigTable[kRGB_565_GrPixelConfig].fFormatType = kNormalizedFixedPoint_FormatType;
   1631     fConfigTable[kRGB_565_GrPixelConfig].fFlags = ConfigInfo::kTextureable_Flag;
   1632     if (kGL_GrGLStandard == standard) {
   1633         if (version >= GR_GL_VER(4, 2) || ctxInfo.hasExtension("GL_ARB_ES2_compatibility")) {
   1634             fConfigTable[kRGB_565_GrPixelConfig].fFlags |= allRenderFlags;
   1635         }
   1636     } else {
   1637         fConfigTable[kRGB_565_GrPixelConfig].fFlags |= allRenderFlags;
   1638     }
   1639     // 565 is not a sized internal format on desktop GL. So on desktop with
   1640     // 565 we always use an unsized internal format to let the system pick
   1641     // the best sized format to convert the 565 data to. Since TexStorage
   1642     // only allows sized internal formats we disallow it.
   1643     //
   1644     // TODO: As of 4.2, regular GL supports 565. This logic is due for an
   1645     // update.
   1646     if (texStorageSupported && kGL_GrGLStandard != standard) {
   1647         fConfigTable[kRGB_565_GrPixelConfig].fFlags |= ConfigInfo::kCanUseTexStorage_Flag;
   1648     }
   1649     fConfigTable[kRGB_565_GrPixelConfig].fSwizzle = GrSwizzle::RGBA();
   1650 
   1651     fConfigTable[kRGBA_4444_GrPixelConfig].fFormats.fBaseInternalFormat = GR_GL_RGBA;
   1652     fConfigTable[kRGBA_4444_GrPixelConfig].fFormats.fSizedInternalFormat = GR_GL_RGBA4;
   1653     fConfigTable[kRGBA_4444_GrPixelConfig].fFormats.fExternalFormat[kOther_ExternalFormatUsage] =
   1654         GR_GL_RGBA;
   1655     fConfigTable[kRGBA_4444_GrPixelConfig].fFormats.fExternalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
   1656     fConfigTable[kRGBA_4444_GrPixelConfig].fFormatType = kNormalizedFixedPoint_FormatType;
   1657     fConfigTable[kRGBA_4444_GrPixelConfig].fFlags = ConfigInfo::kTextureable_Flag;
   1658     if (kGL_GrGLStandard == standard) {
   1659         if (version >= GR_GL_VER(4, 2)) {
   1660             fConfigTable[kRGBA_4444_GrPixelConfig].fFlags |= allRenderFlags;
   1661         }
   1662     } else {
   1663         fConfigTable[kRGBA_4444_GrPixelConfig].fFlags |= allRenderFlags;
   1664     }
   1665     if (texStorageSupported) {
   1666         fConfigTable[kRGBA_4444_GrPixelConfig].fFlags |= ConfigInfo::kCanUseTexStorage_Flag;
   1667     }
   1668     fConfigTable[kRGBA_4444_GrPixelConfig].fSwizzle = GrSwizzle::RGBA();
   1669 
   1670     bool alpha8IsValidForGL = kGL_GrGLStandard == standard &&
   1671             (!fIsCoreProfile || version <= GR_GL_VER(3, 0));
   1672 
   1673     ConfigInfo& alphaInfo = fConfigTable[kAlpha_8_as_Alpha_GrPixelConfig];
   1674     alphaInfo.fFormats.fExternalType = GR_GL_UNSIGNED_BYTE;
   1675     alphaInfo.fFormatType = kNormalizedFixedPoint_FormatType;
   1676     if (alpha8IsValidForGL || (kGL_GrGLStandard != standard && version < GR_GL_VER(3, 0))) {
   1677         alphaInfo.fFlags = ConfigInfo::kTextureable_Flag;
   1678     }
   1679     alphaInfo.fFormats.fBaseInternalFormat = GR_GL_ALPHA;
   1680     alphaInfo.fFormats.fSizedInternalFormat = GR_GL_ALPHA8;
   1681     alphaInfo.fFormats.fExternalFormat[kOther_ExternalFormatUsage] = GR_GL_ALPHA;
   1682     alphaInfo.fSwizzle = GrSwizzle::AAAA();
   1683     if (fAlpha8IsRenderable && alpha8IsValidForGL) {
   1684         alphaInfo.fFlags |= allRenderFlags;
   1685     }
   1686 
   1687     ConfigInfo& redInfo = fConfigTable[kAlpha_8_as_Red_GrPixelConfig];
   1688     redInfo.fFormats.fExternalType = GR_GL_UNSIGNED_BYTE;
   1689     redInfo.fFormatType = kNormalizedFixedPoint_FormatType;
   1690     redInfo.fFormats.fBaseInternalFormat = GR_GL_RED;
   1691     redInfo.fFormats.fSizedInternalFormat = GR_GL_R8;
   1692     redInfo.fFormats.fExternalFormat[kOther_ExternalFormatUsage] = GR_GL_RED;
   1693     redInfo.fSwizzle = GrSwizzle::RRRR();
   1694 
   1695     // ES2 Command Buffer does not allow TexStorage with R8_EXT (so Alpha_8 and Gray_8)
   1696     if (texStorageSupported && !isCommandBufferES2) {
   1697         if (!disableR8TexStorageForANGLEGL) {
   1698             alphaInfo.fFlags |= ConfigInfo::kCanUseTexStorage_Flag;
   1699         }
   1700         redInfo.fFlags |= ConfigInfo::kCanUseTexStorage_Flag;
   1701     }
   1702 
   1703     if (textureRedSupport) {
   1704         redInfo.fFlags |= ConfigInfo::kTextureable_Flag | allRenderFlags;
   1705         if (texelBufferSupport) {
   1706             redInfo.fFlags |= ConfigInfo::kCanUseWithTexelBuffer_Flag;
   1707         }
   1708 
   1709         fConfigTable[kAlpha_8_GrPixelConfig] = redInfo;
   1710     } else {
   1711         redInfo.fFlags = 0;
   1712 
   1713         fConfigTable[kAlpha_8_GrPixelConfig] = alphaInfo;
   1714     }
   1715 
   1716     ConfigInfo& grayLumInfo = fConfigTable[kGray_8_as_Lum_GrPixelConfig];
   1717     grayLumInfo.fFormats.fExternalType = GR_GL_UNSIGNED_BYTE;
   1718     grayLumInfo.fFormatType = kNormalizedFixedPoint_FormatType;
   1719     grayLumInfo.fFormats.fBaseInternalFormat = GR_GL_LUMINANCE;
   1720     grayLumInfo.fFormats.fSizedInternalFormat = GR_GL_LUMINANCE8;
   1721     grayLumInfo.fFormats.fExternalFormat[kOther_ExternalFormatUsage] = GR_GL_LUMINANCE;
   1722     grayLumInfo.fSwizzle = GrSwizzle::RGBA();
   1723     if ((standard == kGL_GrGLStandard && version <= GR_GL_VER(3, 0)) ||
   1724         (standard == kGLES_GrGLStandard && version < GR_GL_VER(3, 0))) {
   1725         grayLumInfo.fFlags = ConfigInfo::kTextureable_Flag;
   1726     }
   1727 
   1728     ConfigInfo& grayRedInfo = fConfigTable[kGray_8_as_Red_GrPixelConfig];
   1729     grayRedInfo.fFormats.fExternalType = GR_GL_UNSIGNED_BYTE;
   1730     grayRedInfo.fFormatType = kNormalizedFixedPoint_FormatType;
   1731     grayRedInfo.fFormats.fBaseInternalFormat = GR_GL_RED;
   1732     grayRedInfo.fFormats.fSizedInternalFormat = GR_GL_R8;
   1733     grayRedInfo.fFormats.fExternalFormat[kOther_ExternalFormatUsage] = GR_GL_RED;
   1734     grayRedInfo.fSwizzle = GrSwizzle::RRRA();
   1735     grayRedInfo.fFlags = ConfigInfo::kTextureable_Flag;
   1736 
   1737 #if 0 // Leaving Gray8 as non-renderable, to keep things simple and match raster. Needs to be
   1738       // updated to support Gray8_as_Lum and Gray8_as_red if this is ever enabled.
   1739     if (this->textureRedSupport() ||
   1740         (kDesktop_ARB_MSFBOType == this->msFBOType() &&
   1741          ctxInfo.renderer() != kOSMesa_GrGLRenderer)) {
   1742         // desktop ARB extension/3.0+ supports LUMINANCE8 as renderable.
   1743         // However, osmesa fails if it used even when GL_ARB_framebuffer_object is present.
   1744         // Core profile removes LUMINANCE8 support, but we should have chosen R8 in that case.
   1745         fConfigTable[kGray_8_GrPixelConfig].fFlags |= allRenderFlags;
   1746     }
   1747 #endif
   1748     if (texStorageSupported && !isCommandBufferES2) {
   1749         if (!disableR8TexStorageForANGLEGL) {
   1750             grayLumInfo.fFlags |= ConfigInfo::kCanUseTexStorage_Flag;
   1751         }
   1752         grayRedInfo.fFlags |= ConfigInfo::kCanUseTexStorage_Flag;
   1753     }
   1754 
   1755     if (textureRedSupport) {
   1756         if (texelBufferSupport) {
   1757             grayRedInfo.fFlags |= ConfigInfo::kCanUseWithTexelBuffer_Flag;
   1758         }
   1759         fConfigTable[kGray_8_GrPixelConfig] = grayRedInfo;
   1760     } else {
   1761         grayRedInfo.fFlags = 0;
   1762         fConfigTable[kGray_8_GrPixelConfig] = grayLumInfo;
   1763     }
   1764 
   1765     // Check for [half] floating point texture support
   1766     // NOTE: We disallow floating point textures on ES devices if linear filtering modes are not
   1767     // supported. This is for simplicity, but a more granular approach is possible. Coincidentally,
   1768     // [half] floating point textures became part of the standard in ES3.1 / OGL 3.0.
   1769     bool hasFPTextures = false;
   1770     bool hasHalfFPTextures = false;
   1771     bool rgIsTexturable = false;
   1772     // for now we don't support floating point MSAA on ES
   1773     uint32_t fpRenderFlags = (kGL_GrGLStandard == standard) ? allRenderFlags : nonMSAARenderFlags;
   1774 
   1775     if (kGL_GrGLStandard == standard) {
   1776         if (version >= GR_GL_VER(3, 0)) {
   1777             hasFPTextures = true;
   1778             hasHalfFPTextures = true;
   1779             rgIsTexturable = true;
   1780         }
   1781     } else {
   1782         if (version >= GR_GL_VER(3, 0)) {
   1783             hasFPTextures = true;
   1784             hasHalfFPTextures = true;
   1785             rgIsTexturable = true;
   1786         } else {
   1787             if (ctxInfo.hasExtension("GL_OES_texture_float_linear") &&
   1788                 ctxInfo.hasExtension("GL_OES_texture_float")) {
   1789                 hasFPTextures = true;
   1790             }
   1791             if (ctxInfo.hasExtension("GL_OES_texture_half_float_linear") &&
   1792                 ctxInfo.hasExtension("GL_OES_texture_half_float")) {
   1793                 hasHalfFPTextures = true;
   1794             }
   1795         }
   1796     }
   1797 
   1798     for (auto fpconfig : {kRGBA_float_GrPixelConfig, kRG_float_GrPixelConfig}) {
   1799         const GrGLenum format = kRGBA_float_GrPixelConfig == fpconfig ? GR_GL_RGBA : GR_GL_RG;
   1800         fConfigTable[fpconfig].fFormats.fBaseInternalFormat = format;
   1801         fConfigTable[fpconfig].fFormats.fSizedInternalFormat =
   1802             kRGBA_float_GrPixelConfig == fpconfig ? GR_GL_RGBA32F : GR_GL_RG32F;
   1803         fConfigTable[fpconfig].fFormats.fExternalFormat[kOther_ExternalFormatUsage] = format;
   1804         fConfigTable[fpconfig].fFormats.fExternalType = GR_GL_FLOAT;
   1805         fConfigTable[fpconfig].fFormatType = kFloat_FormatType;
   1806         if (hasFPTextures) {
   1807             fConfigTable[fpconfig].fFlags = rgIsTexturable ? ConfigInfo::kTextureable_Flag : 0;
   1808             // For now we only enable rendering to float on desktop, because on ES we'd have to
   1809             // solve many precision issues and no clients actually want this yet.
   1810             if (kGL_GrGLStandard == standard /* || version >= GR_GL_VER(3,2) ||
   1811                 ctxInfo.hasExtension("GL_EXT_color_buffer_float")*/) {
   1812                 fConfigTable[fpconfig].fFlags |= fpRenderFlags;
   1813             }
   1814         }
   1815         if (texStorageSupported) {
   1816             fConfigTable[fpconfig].fFlags |= ConfigInfo::kCanUseTexStorage_Flag;
   1817         }
   1818         if (texelBufferSupport) {
   1819             fConfigTable[fpconfig].fFlags |= ConfigInfo::kCanUseWithTexelBuffer_Flag;
   1820         }
   1821         fConfigTable[fpconfig].fSwizzle = GrSwizzle::RGBA();
   1822     }
   1823 
   1824     GrGLenum redHalfExternalType;
   1825     if (kGL_GrGLStandard == ctxInfo.standard() || ctxInfo.version() >= GR_GL_VER(3, 0)) {
   1826         redHalfExternalType = GR_GL_HALF_FLOAT;
   1827     } else {
   1828         redHalfExternalType = GR_GL_HALF_FLOAT_OES;
   1829     }
   1830     ConfigInfo& redHalf = fConfigTable[kAlpha_half_as_Red_GrPixelConfig];
   1831     redHalf.fFormats.fExternalType = redHalfExternalType;
   1832     redHalf.fFormatType = kFloat_FormatType;
   1833     redHalf.fFormats.fBaseInternalFormat = GR_GL_RED;
   1834     redHalf.fFormats.fSizedInternalFormat = GR_GL_R16F;
   1835     redHalf.fFormats.fExternalFormat[kOther_ExternalFormatUsage] = GR_GL_RED;
   1836     redHalf.fSwizzle = GrSwizzle::RRRR();
   1837     if (textureRedSupport && hasHalfFPTextures) {
   1838         redHalf.fFlags = ConfigInfo::kTextureable_Flag;
   1839 
   1840         if (kGL_GrGLStandard == standard || version >= GR_GL_VER(3, 2) ||
   1841             (textureRedSupport && ctxInfo.hasExtension("GL_EXT_color_buffer_half_float"))) {
   1842             redHalf.fFlags |= fpRenderFlags;
   1843         }
   1844 
   1845         if (texStorageSupported && !isCommandBufferES2) {
   1846             redHalf.fFlags |= ConfigInfo::kCanUseTexStorage_Flag;
   1847         }
   1848 
   1849         if (texelBufferSupport) {
   1850             redHalf.fFlags |= ConfigInfo::kCanUseWithTexelBuffer_Flag;
   1851         }
   1852     }
   1853     fConfigTable[kAlpha_half_GrPixelConfig] = redHalf;
   1854 
   1855     fConfigTable[kRGBA_half_GrPixelConfig].fFormats.fBaseInternalFormat = GR_GL_RGBA;
   1856     fConfigTable[kRGBA_half_GrPixelConfig].fFormats.fSizedInternalFormat = GR_GL_RGBA16F;
   1857     fConfigTable[kRGBA_half_GrPixelConfig].fFormats.fExternalFormat[kOther_ExternalFormatUsage] =
   1858         GR_GL_RGBA;
   1859     if (kGL_GrGLStandard == ctxInfo.standard() || ctxInfo.version() >= GR_GL_VER(3, 0)) {
   1860         fConfigTable[kRGBA_half_GrPixelConfig].fFormats.fExternalType = GR_GL_HALF_FLOAT;
   1861     } else {
   1862         fConfigTable[kRGBA_half_GrPixelConfig].fFormats.fExternalType = GR_GL_HALF_FLOAT_OES;
   1863     }
   1864     fConfigTable[kRGBA_half_GrPixelConfig].fFormatType = kFloat_FormatType;
   1865     if (hasHalfFPTextures) {
   1866         fConfigTable[kRGBA_half_GrPixelConfig].fFlags = ConfigInfo::kTextureable_Flag;
   1867         // ES requires 3.2 or EXT_color_buffer_half_float.
   1868         if (kGL_GrGLStandard == standard || version >= GR_GL_VER(3,2) ||
   1869              ctxInfo.hasExtension("GL_EXT_color_buffer_half_float")) {
   1870             fConfigTable[kRGBA_half_GrPixelConfig].fFlags |= fpRenderFlags;
   1871         }
   1872     }
   1873     if (texStorageSupported) {
   1874         fConfigTable[kRGBA_half_GrPixelConfig].fFlags |= ConfigInfo::kCanUseTexStorage_Flag;
   1875     }
   1876     if (texelBufferSupport) {
   1877         fConfigTable[kRGBA_half_GrPixelConfig].fFlags |= ConfigInfo::kCanUseWithTexelBuffer_Flag;
   1878     }
   1879     fConfigTable[kRGBA_half_GrPixelConfig].fSwizzle = GrSwizzle::RGBA();
   1880 
   1881     // Bulk populate the texture internal/external formats here and then deal with exceptions below.
   1882 
   1883     // ES 2.0 requires that the internal/external formats match.
   1884     bool useSizedTexFormats = (kGL_GrGLStandard == ctxInfo.standard() ||
   1885                                ctxInfo.version() >= GR_GL_VER(3,0));
   1886     // All ES versions (thus far) require sized internal formats for render buffers.
   1887     // TODO: Always use sized internal format?
   1888     bool useSizedRbFormats = kGLES_GrGLStandard == ctxInfo.standard();
   1889 
   1890     for (int i = 0; i < kGrPixelConfigCnt; ++i) {
   1891         // Almost always we want to pass fExternalFormat[kOther_ExternalFormatUsage] as the <format>
   1892         // param to glTex[Sub]Image.
   1893         fConfigTable[i].fFormats.fExternalFormat[kTexImage_ExternalFormatUsage] =
   1894             fConfigTable[i].fFormats.fExternalFormat[kOther_ExternalFormatUsage];
   1895         fConfigTable[i].fFormats.fInternalFormatTexImage = useSizedTexFormats ?
   1896             fConfigTable[i].fFormats.fSizedInternalFormat :
   1897             fConfigTable[i].fFormats.fBaseInternalFormat;
   1898         fConfigTable[i].fFormats.fInternalFormatRenderbuffer = useSizedRbFormats ?
   1899             fConfigTable[i].fFormats.fSizedInternalFormat :
   1900             fConfigTable[i].fFormats.fBaseInternalFormat;
   1901     }
   1902     // If we're on ES 3.0+ but because of a driver workaround selected GL_ALPHA to implement the
   1903     // kAlpha_8_GrPixelConfig then we actually have to use a base internal format rather than a
   1904     // sized internal format. This is because there is no valid 8 bit alpha sized internal format
   1905     // in ES.
   1906     if (useSizedTexFormats && kGLES_GrGLStandard == ctxInfo.standard() && !textureRedSupport) {
   1907         SkASSERT(fConfigTable[kAlpha_8_GrPixelConfig].fFormats.fBaseInternalFormat == GR_GL_ALPHA8);
   1908         SkASSERT(fConfigTable[kAlpha_8_as_Alpha_GrPixelConfig].fFormats.fBaseInternalFormat ==
   1909                      GR_GL_ALPHA8);
   1910         fConfigTable[kAlpha_8_GrPixelConfig].fFormats.fInternalFormatTexImage =
   1911             fConfigTable[kAlpha_8_GrPixelConfig].fFormats.fBaseInternalFormat;
   1912         fConfigTable[kAlpha_8_as_Alpha_GrPixelConfig].fFormats.fInternalFormatTexImage =
   1913             fConfigTable[kAlpha_8_as_Alpha_GrPixelConfig].fFormats.fBaseInternalFormat;
   1914     }
   1915 
   1916     // OpenGL ES 2.0 + GL_EXT_sRGB allows GL_SRGB_ALPHA to be specified as the <format>
   1917     // param to Tex(Sub)Image. ES 2.0 requires the <internalFormat> and <format> params to match.
   1918     // Thus, on ES 2.0 we will use GL_SRGB_ALPHA as the <format> param.
   1919     // On OpenGL and ES 3.0+ GL_SRGB_ALPHA does not work for the <format> param to glTexImage.
   1920     if (ctxInfo.standard() == kGLES_GrGLStandard && ctxInfo.version() == GR_GL_VER(2,0)) {
   1921         fConfigTable[kSRGBA_8888_GrPixelConfig].fFormats.fExternalFormat[kTexImage_ExternalFormatUsage] =
   1922             GR_GL_SRGB_ALPHA;
   1923 
   1924         // Additionally, because we had to "invent" sBGRA, there is no way to make it work
   1925         // in ES 2.0, because there is no <internalFormat> we can use. So just make that format
   1926         // unsupported. (If we have no sRGB support at all, this will get overwritten below).
   1927         fConfigTable[kSBGRA_8888_GrPixelConfig].fFlags = 0;
   1928     }
   1929 
   1930     // If BGRA is supported as an internal format it must always be specified to glTex[Sub]Image
   1931     // as a base format.
   1932     // GL_EXT_texture_format_BGRA8888:
   1933     //      This extension GL_BGRA as an unsized internal format. However, it is written against ES
   1934     //      2.0 and therefore doesn't define a value for GL_BGRA8 as ES 2.0 uses unsized internal
   1935     //      formats.
   1936     // GL_APPLE_texture_format_BGRA8888:
   1937     //     ES 2.0: the extension makes BGRA an external format but not an internal format.
   1938     //     ES 3.0: the extension explicitly states GL_BGRA8 is not a valid internal format for
   1939     //             glTexImage (just for glTexStorage).
   1940     if (useSizedTexFormats && this->bgraIsInternalFormat()) {
   1941         fConfigTable[kBGRA_8888_GrPixelConfig].fFormats.fInternalFormatTexImage = GR_GL_BGRA;
   1942     }
   1943 
   1944     // If we don't have texture swizzle support then the shader generator must insert the
   1945     // swizzle into shader code.
   1946     if (!this->textureSwizzleSupport()) {
   1947         for (int i = 0; i < kGrPixelConfigCnt; ++i) {
   1948             shaderCaps->fConfigTextureSwizzle[i] = fConfigTable[i].fSwizzle;
   1949         }
   1950     }
   1951 
   1952     // Shader output swizzles will default to RGBA. When we've use GL_RED instead of GL_ALPHA to
   1953     // implement kAlpha_8_GrPixelConfig we need to swizzle the shader outputs so the alpha channel
   1954     // gets written to the single component.
   1955     if (textureRedSupport) {
   1956         for (int i = 0; i < kGrPixelConfigCnt; ++i) {
   1957             GrPixelConfig config = static_cast<GrPixelConfig>(i);
   1958             if (GrPixelConfigIsAlphaOnly(config) &&
   1959                 fConfigTable[i].fFormats.fBaseInternalFormat == GR_GL_RED) {
   1960                 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA();
   1961             }
   1962         }
   1963     }
   1964 
   1965     for (int i = 0; i < kGrPixelConfigCnt; ++i) {
   1966         if (ConfigInfo::kRenderableWithMSAA_Flag & fConfigTable[i].fFlags) {
   1967             // We assume that MSAA rendering is supported only if we support non-MSAA rendering.
   1968             SkASSERT(ConfigInfo::kRenderable_Flag & fConfigTable[i].fFlags);
   1969             if ((kGL_GrGLStandard == ctxInfo.standard() &&
   1970                  (ctxInfo.version() >= GR_GL_VER(4,2) ||
   1971                   ctxInfo.hasExtension("GL_ARB_internalformat_query"))) ||
   1972                 (kGLES_GrGLStandard == ctxInfo.standard() && ctxInfo.version() >= GR_GL_VER(3,0))) {
   1973                 int count;
   1974                 GrGLenum format = fConfigTable[i].fFormats.fInternalFormatRenderbuffer;
   1975                 GR_GL_GetInternalformativ(gli, GR_GL_RENDERBUFFER, format, GR_GL_NUM_SAMPLE_COUNTS,
   1976                                           1, &count);
   1977                 if (count) {
   1978                     int* temp = new int[count];
   1979                     GR_GL_GetInternalformativ(gli, GR_GL_RENDERBUFFER, format, GR_GL_SAMPLES, count,
   1980                                               temp);
   1981                     // GL has a concept of MSAA rasterization with a single sample but we do not.
   1982                     if (count && temp[count - 1] == 1) {
   1983                         --count;
   1984                         SkASSERT(!count || temp[count -1] > 1);
   1985                     }
   1986                     fConfigTable[i].fColorSampleCounts.setCount(count+1);
   1987                     // We initialize our supported values with 1 (no msaa) and reverse the order
   1988                     // returned by GL so that the array is ascending.
   1989                     fConfigTable[i].fColorSampleCounts[0] = 1;
   1990                     for (int j = 0; j < count; ++j) {
   1991                         fConfigTable[i].fColorSampleCounts[j+1] = temp[count - j - 1];
   1992                     }
   1993                     delete[] temp;
   1994                 }
   1995             } else {
   1996                 // Fake out the table using some semi-standard counts up to the max allowed sample
   1997                 // count.
   1998                 int maxSampleCnt = 1;
   1999                 if (GrGLCaps::kES_IMG_MsToTexture_MSFBOType == fMSFBOType) {
   2000                     GR_GL_GetIntegerv(gli, GR_GL_MAX_SAMPLES_IMG, &maxSampleCnt);
   2001                 } else if (GrGLCaps::kNone_MSFBOType != fMSFBOType) {
   2002                     GR_GL_GetIntegerv(gli, GR_GL_MAX_SAMPLES, &maxSampleCnt);
   2003                 }
   2004                 // Chrome has a mock GL implementation that returns 0.
   2005                 maxSampleCnt = SkTMax(1, maxSampleCnt);
   2006 
   2007                 static constexpr int kDefaultSamples[] = {1, 2, 4, 8};
   2008                 int count = SK_ARRAY_COUNT(kDefaultSamples);
   2009                 for (; count > 0; --count) {
   2010                     if (kDefaultSamples[count - 1] <= maxSampleCnt) {
   2011                         break;
   2012                     }
   2013                 }
   2014                 if (count > 0) {
   2015                     fConfigTable[i].fColorSampleCounts.append(count, kDefaultSamples);
   2016                 }
   2017             }
   2018         } else if (ConfigInfo::kRenderable_Flag & fConfigTable[i].fFlags) {
   2019             fConfigTable[i].fColorSampleCounts.setCount(1);
   2020             fConfigTable[i].fColorSampleCounts[0] = 1;
   2021         }
   2022     }
   2023 
   2024 #ifdef SK_DEBUG
   2025     // Make sure we initialized everything.
   2026     ConfigInfo defaultEntry;
   2027     for (int i = 0; i < kGrPixelConfigCnt; ++i) {
   2028         // Make sure we didn't set renderable and not blittable or renderable with msaa and not
   2029         // renderable.
   2030         SkASSERT(!((ConfigInfo::kRenderable_Flag) && !(ConfigInfo::kFBOColorAttachment_Flag)));
   2031         SkASSERT(!((ConfigInfo::kRenderableWithMSAA_Flag) && !(ConfigInfo::kRenderable_Flag)));
   2032         SkASSERT(defaultEntry.fFormats.fBaseInternalFormat !=
   2033                  fConfigTable[i].fFormats.fBaseInternalFormat);
   2034         SkASSERT(defaultEntry.fFormats.fSizedInternalFormat !=
   2035                  fConfigTable[i].fFormats.fSizedInternalFormat);
   2036         for (int j = 0; j < kExternalFormatUsageCnt; ++j) {
   2037             SkASSERT(defaultEntry.fFormats.fExternalFormat[j] !=
   2038                      fConfigTable[i].fFormats.fExternalFormat[j]);
   2039         }
   2040         SkASSERT(defaultEntry.fFormats.fExternalType != fConfigTable[i].fFormats.fExternalType);
   2041     }
   2042 #endif
   2043 }
   2044 
   2045 bool GrGLCaps::initDescForDstCopy(const GrRenderTargetProxy* src, GrSurfaceDesc* desc,
   2046                                   bool* rectsMustMatch, bool* disallowSubrect) const {
   2047     // By default, we don't require rects to match.
   2048     *rectsMustMatch = false;
   2049 
   2050     // By default, we allow subrects.
   2051     *disallowSubrect = false;
   2052 
   2053     // If the src is a texture, we can implement the blit as a draw assuming the config is
   2054     // renderable.
   2055     if (src->asTextureProxy() && this->isConfigRenderable(src->config(), false)) {
   2056         desc->fOrigin = kBottomLeft_GrSurfaceOrigin;
   2057         desc->fFlags = kRenderTarget_GrSurfaceFlag;
   2058         desc->fConfig = src->config();
   2059         return true;
   2060     }
   2061 
   2062     {
   2063         // The only way we could see a non-GR_GL_TEXTURE_2D texture would be if it were
   2064         // wrapped. In that case the proxy would already be instantiated.
   2065         const GrTexture* srcTexture = src->priv().peekTexture();
   2066         const GrGLTexture* glSrcTexture = static_cast<const GrGLTexture*>(srcTexture);
   2067         if (glSrcTexture && glSrcTexture->target() != GR_GL_TEXTURE_2D) {
   2068             // Not supported for FBO blit or CopyTexSubImage
   2069             return false;
   2070         }
   2071     }
   2072 
   2073     // We look for opportunities to use CopyTexSubImage, or fbo blit. If neither are
   2074     // possible and we return false to fallback to creating a render target dst for render-to-
   2075     // texture. This code prefers CopyTexSubImage to fbo blit and avoids triggering temporary fbo
   2076     // creation. It isn't clear that avoiding temporary fbo creation is actually optimal.
   2077     GrSurfaceOrigin originForBlitFramebuffer = kTopLeft_GrSurfaceOrigin;
   2078     bool rectsMustMatchForBlitFramebuffer = false;
   2079     bool disallowSubrectForBlitFramebuffer = false;
   2080     if (src->numColorSamples() &&
   2081         (this->blitFramebufferSupportFlags() & kResolveMustBeFull_BlitFrambufferFlag)) {
   2082         rectsMustMatchForBlitFramebuffer = true;
   2083         disallowSubrectForBlitFramebuffer = true;
   2084         // Mirroring causes rects to mismatch later, don't allow it.
   2085         originForBlitFramebuffer = src->origin();
   2086     } else if (src->numColorSamples() && (this->blitFramebufferSupportFlags() &
   2087                                           kRectsMustMatchForMSAASrc_BlitFramebufferFlag)) {
   2088         rectsMustMatchForBlitFramebuffer = true;
   2089         // Mirroring causes rects to mismatch later, don't allow it.
   2090         originForBlitFramebuffer = src->origin();
   2091     } else if (this->blitFramebufferSupportFlags() & kNoScalingOrMirroring_BlitFramebufferFlag) {
   2092         originForBlitFramebuffer = src->origin();
   2093     }
   2094 
   2095     // Check for format issues with glCopyTexSubImage2D
   2096     if (this->bgraIsInternalFormat() && kBGRA_8888_GrPixelConfig == src->config()) {
   2097         // glCopyTexSubImage2D doesn't work with this config. If the bgra can be used with fbo blit
   2098         // then we set up for that, otherwise fail.
   2099         if (this->canConfigBeFBOColorAttachment(kBGRA_8888_GrPixelConfig)) {
   2100             desc->fOrigin = originForBlitFramebuffer;
   2101             desc->fConfig = kBGRA_8888_GrPixelConfig;
   2102             *rectsMustMatch = rectsMustMatchForBlitFramebuffer;
   2103             *disallowSubrect = disallowSubrectForBlitFramebuffer;
   2104             return true;
   2105         }
   2106         return false;
   2107     }
   2108 
   2109     {
   2110         bool srcIsMSAARenderbuffer = GrFSAAType::kUnifiedMSAA == src->fsaaType() &&
   2111                                      this->usesMSAARenderBuffers();
   2112         if (srcIsMSAARenderbuffer) {
   2113             // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer. Set up for FBO
   2114             // blit or fail.
   2115             if (this->canConfigBeFBOColorAttachment(src->config())) {
   2116                 desc->fOrigin = originForBlitFramebuffer;
   2117                 desc->fConfig = src->config();
   2118                 *rectsMustMatch = rectsMustMatchForBlitFramebuffer;
   2119                 *disallowSubrect = disallowSubrectForBlitFramebuffer;
   2120                 return true;
   2121             }
   2122             return false;
   2123         }
   2124     }
   2125 
   2126     // We'll do a CopyTexSubImage. Make the dst a plain old texture.
   2127     desc->fConfig = src->config();
   2128     desc->fOrigin = src->origin();
   2129     desc->fFlags = kNone_GrSurfaceFlags;
   2130     return true;
   2131 }
   2132 
   2133 void GrGLCaps::applyDriverCorrectnessWorkarounds(const GrGLContextInfo& ctxInfo,
   2134                                                  const GrContextOptions& contextOptions,
   2135                                                  GrShaderCaps* shaderCaps) {
   2136     // A driver but on the nexus 6 causes incorrect dst copies when invalidate is called beforehand.
   2137     // Thus we are blacklisting this extension for now on Adreno4xx devices.
   2138     if (kAdreno4xx_GrGLRenderer == ctxInfo.renderer()) {
   2139         fDiscardRenderTargetSupport = false;
   2140         fInvalidateFBType = kNone_InvalidateFBType;
   2141     }
   2142 
   2143     // glClearTexImage seems to have a bug in NVIDIA drivers that was fixed sometime between
   2144     // 340.96 and 367.57.
   2145     if (kGL_GrGLStandard == ctxInfo.standard() &&
   2146         ctxInfo.driver() == kNVIDIA_GrGLDriver &&
   2147         ctxInfo.driverVersion() < GR_GL_DRIVER_VER(367, 57)) {
   2148         fClearTextureSupport = false;
   2149     }
   2150 
   2151     // Calling glClearTexImage crashes on the NexusPlayer.
   2152     if (kPowerVRRogue_GrGLRenderer == ctxInfo.renderer()) {
   2153         fClearTextureSupport = false;
   2154     }
   2155 
   2156     // On at least some MacBooks, GLSL 4.0 geometry shaders break if we use invocations.
   2157 #ifdef SK_BUILD_FOR_MAC
   2158     if (shaderCaps->fGeometryShaderSupport) {
   2159         shaderCaps->fGSInvocationsSupport = false;
   2160     }
   2161 #endif
   2162 
   2163     // Qualcomm driver @103.0 has been observed to crash compiling ccpr geometry
   2164     // shaders. @127.0 is the earliest verified driver to not crash.
   2165     if (kQualcomm_GrGLDriver == ctxInfo.driver() &&
   2166         ctxInfo.driverVersion() < GR_GL_DRIVER_VER(127,0)) {
   2167         shaderCaps->fGeometryShaderSupport = false;
   2168     }
   2169 
   2170 #if defined(__has_feature)
   2171 #if defined(SK_BUILD_FOR_MAC) && __has_feature(thread_sanitizer)
   2172     // See skbug.com/7058
   2173     fMapBufferType = kNone_MapBufferType;
   2174     fMapBufferFlags = kNone_MapFlags;
   2175 #endif
   2176 #endif
   2177 
   2178     // We found that the Galaxy J5 with an Adreno 306 running 6.0.1 has a bug where
   2179     // GL_INVALID_OPERATION thrown by glDrawArrays when using a buffer that was mapped. The same bug
   2180     // did not reproduce on a Nexus7 2013 with a 320 running Android M with driver 127.0. It's
   2181     // unclear whether this really affects a wide range of devices.
   2182     if (ctxInfo.renderer() == kAdreno3xx_GrGLRenderer &&
   2183         ctxInfo.driverVersion() > GR_GL_DRIVER_VER(127, 0)) {
   2184         fMapBufferType = kNone_MapBufferType;
   2185         fMapBufferFlags = kNone_MapFlags;
   2186     }
   2187 
   2188     // TODO: re-enable for ANGLE
   2189     if (kANGLE_GrGLDriver == ctxInfo.driver()) {
   2190         fTransferBufferType = kNone_TransferBufferType;
   2191     }
   2192 
   2193     // Using MIPs on this GPU seems to be a source of trouble.
   2194     if (kPowerVR54x_GrGLRenderer == ctxInfo.renderer()) {
   2195         fMipMapSupport = false;
   2196     }
   2197 
   2198     if (kPowerVRRogue_GrGLRenderer == ctxInfo.renderer()) {
   2199         // Temporarily disabling clip analytic fragments processors on Nexus player while we work
   2200         // around a driver bug related to gl_FragCoord.
   2201         // https://bugs.chromium.org/p/skia/issues/detail?id=7286
   2202         fMaxClipAnalyticFPs = 0;
   2203     }
   2204 
   2205 #ifndef SK_BUILD_FOR_IOS
   2206     if (kPowerVR54x_GrGLRenderer == ctxInfo.renderer() ||
   2207         kPowerVRRogue_GrGLRenderer == ctxInfo.renderer() ||
   2208         (kAdreno3xx_GrGLRenderer == ctxInfo.renderer() &&
   2209          ctxInfo.driver() != kChromium_GrGLDriver)) {
   2210         fUseDrawToClearColor = true;
   2211     }
   2212 #endif
   2213 
   2214     // A lot of GPUs have trouble with full screen clears (skbug.com/7195)
   2215     if (kAMDRadeonHD7xxx_GrGLRenderer == ctxInfo.renderer() ||
   2216         kAMDRadeonR9M4xx_GrGLRenderer == ctxInfo.renderer()) {
   2217         fUseDrawToClearColor = true;
   2218     }
   2219 
   2220 #ifdef SK_BUILD_FOR_MAC
   2221     // crbug.com/768134 - On MacBook Pros, the Intel Iris Pro doesn't always perform
   2222     // full screen clears
   2223     // crbug.com/773107 - On MacBook Pros, a wide range of Intel GPUs don't always
   2224     // perform full screen clears.
   2225     if (kIntel_GrGLVendor == ctxInfo.vendor()) {
   2226         fUseDrawToClearColor = true;
   2227     }
   2228 #endif
   2229 
   2230     // See crbug.com/755871. This could probably be narrowed to just partial clears as the driver
   2231     // bugs seems to involve clearing too much and not skipping the clear.
   2232     // See crbug.com/768134. This is also needed for full clears and was seen on an nVidia K620
   2233     // but only for D3D11 ANGLE.
   2234     if (GrGLANGLEBackend::kD3D11 == ctxInfo.angleBackend()) {
   2235         fUseDrawToClearColor = true;
   2236     }
   2237 
   2238     if (kAdreno4xx_GrGLRenderer == ctxInfo.renderer()) {
   2239         // This is known to be fixed sometime between driver 145.0 and 219.0
   2240         if (ctxInfo.driverVersion() <= GR_GL_DRIVER_VER(219, 0)) {
   2241             fUseDrawToClearStencilClip = true;
   2242         }
   2243         fDisallowTexSubImageForUnormConfigTexturesEverBoundToFBO = true;
   2244     }
   2245 
   2246     // This was reproduced on the following configurations:
   2247     // - A Galaxy J5 (Adreno 306) running Android 6 with driver 140.0
   2248     // - A Nexus 7 2013 (Adreno 320) running Android 5 with driver 104.0
   2249     // - A Nexus 7 2013 (Adreno 320) running Android 6 with driver 127.0
   2250     // - A Nexus 5 (Adreno 330) running Android 6 with driver 127.0
   2251     // and not produced on:
   2252     // - A Nexus 7 2013 (Adreno 320) running Android 4 with driver 53.0
   2253     // The particular lines that get dropped from test images varies across different devices.
   2254     if (kAdreno3xx_GrGLRenderer == ctxInfo.renderer() &&
   2255         ctxInfo.driverVersion() > GR_GL_DRIVER_VER(53, 0)) {
   2256         fRequiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines = true;
   2257     }
   2258 
   2259     // Our Chromebook with kPowerVRRogue_GrGLRenderer seems to crash when glDrawArraysInstanced is
   2260     // given 1 << 15 or more instances.
   2261     if (kPowerVRRogue_GrGLRenderer == ctxInfo.renderer()) {
   2262         fMaxInstancesPerDrawArraysWithoutCrashing = 0x7fff;
   2263     }
   2264 
   2265     // Texture uploads sometimes seem to be ignored to textures bound to FBOS on Tegra3.
   2266     if (kTegra3_GrGLRenderer == ctxInfo.renderer()) {
   2267         fDisallowTexSubImageForUnormConfigTexturesEverBoundToFBO = true;
   2268         fUseDrawInsteadOfAllRenderTargetWrites = true;
   2269     }
   2270 
   2271     if (kGL_GrGLStandard == ctxInfo.standard() && kIntel_GrGLVendor == ctxInfo.vendor() ) {
   2272         fSampleShadingSupport = false;
   2273     }
   2274 
   2275 #ifdef SK_BUILD_FOR_MAC
   2276     static constexpr bool isMAC = true;
   2277 #else
   2278     static constexpr bool isMAC = false;
   2279 #endif
   2280 
   2281     // We support manual mip-map generation (via iterative downsampling draw calls). This fixes
   2282     // bugs on some cards/drivers that produce incorrect mip-maps for sRGB textures when using
   2283     // glGenerateMipmap. Our implementation requires mip-level sampling control. Additionally,
   2284     // it can be much slower (especially on mobile GPUs), so we opt-in only when necessary:
   2285     if (fMipMapLevelAndLodControlSupport &&
   2286         (contextOptions.fDoManualMipmapping ||
   2287          (kIntel_GrGLVendor == ctxInfo.vendor()) ||
   2288          (kNVIDIA_GrGLDriver == ctxInfo.driver() && isMAC) ||
   2289          (kATI_GrGLVendor == ctxInfo.vendor()))) {
   2290         fDoManualMipmapping = true;
   2291     }
   2292 
   2293     // See http://crbug.com/710443
   2294 #ifdef SK_BUILD_FOR_MAC
   2295     if (kIntel6xxx_GrGLRenderer == ctxInfo.renderer()) {
   2296         fClearToBoundaryValuesIsBroken = true;
   2297     }
   2298 #endif
   2299     if (kQualcomm_GrGLVendor == ctxInfo.vendor()) {
   2300         fDrawArraysBaseVertexIsBroken = true;
   2301     }
   2302 
   2303     // The ccpr vertex-shader implementation does not work on this platform. Only allow CCPR with
   2304     // GS.
   2305 
   2306     if (kANGLE_GrGLRenderer == ctxInfo.renderer() &&
   2307         GrGLANGLERenderer::kSkylake == ctxInfo.angleRenderer()) {
   2308         bool gsSupport = fShaderCaps->geometryShaderSupport();
   2309 #if GR_TEST_UTILS
   2310         gsSupport &= !contextOptions.fSuppressGeometryShaders;
   2311 #endif
   2312         fBlacklistCoverageCounting = !gsSupport;
   2313     }
   2314     // Currently the extension is advertised but fb fetch is broken on 500 series Adrenos like the
   2315     // Galaxy S7.
   2316     // TODO: Once this is fixed we can update the check here to look at a driver version number too.
   2317     if (kAdreno5xx_GrGLRenderer == ctxInfo.renderer()) {
   2318         shaderCaps->fFBFetchSupport = false;
   2319     }
   2320 
   2321     // Pre-361 NVIDIA has a bug with NV_sample_mask_override_coverage.
   2322     if (kNVIDIA_GrGLDriver == ctxInfo.driver() &&
   2323         ctxInfo.driverVersion() < GR_GL_DRIVER_VER(361,00)) {
   2324         shaderCaps->fSampleMaskOverrideCoverageSupport = false;
   2325     }
   2326 
   2327     // Adreno GPUs have a tendency to drop tiles when there is a divide-by-zero in a shader
   2328     shaderCaps->fDropsTileOnZeroDivide = kQualcomm_GrGLVendor == ctxInfo.vendor();
   2329 
   2330     // On the NexusS and GalaxyNexus, the use of 'any' causes the compilation error "Calls to any
   2331     // function that may require a gradient calculation inside a conditional block may return
   2332     // undefined results". This appears to be an issue with the 'any' call since even the simple
   2333     // "result=black; if (any()) result=white;" code fails to compile. This issue comes into play
   2334     // from our GrTextureDomain processor.
   2335     shaderCaps->fCanUseAnyFunctionInShader = kImagination_GrGLVendor != ctxInfo.vendor();
   2336 
   2337     // Known issue on at least some Intel platforms:
   2338     // http://code.google.com/p/skia/issues/detail?id=946
   2339     if (kIntel_GrGLVendor == ctxInfo.vendor()) {
   2340         shaderCaps->fFragCoordConventionsExtensionString = nullptr;
   2341     }
   2342 
   2343     if (kTegra3_GrGLRenderer == ctxInfo.renderer()) {
   2344         // The Tegra3 compiler will sometimes never return if we have min(abs(x), 1.0),
   2345         // so we must do the abs first in a separate expression.
   2346         shaderCaps->fCanUseMinAndAbsTogether = false;
   2347 
   2348         // Tegra3 fract() seems to trigger undefined behavior for negative values, so we
   2349         // must avoid this condition.
   2350         shaderCaps->fCanUseFractForNegativeValues = false;
   2351     }
   2352 
   2353     // On Intel GPU there is an issue where it reads the second argument to atan "- %s.x" as an int
   2354     // thus must us -1.0 * %s.x to work correctly
   2355     if (kIntel_GrGLVendor == ctxInfo.vendor()) {
   2356         shaderCaps->fMustForceNegatedAtanParamToFloat = true;
   2357     }
   2358 
   2359     // On some Intel GPUs there is an issue where the driver outputs bogus values in the shader
   2360     // when floor and abs are called on the same line. Thus we must execute an Op between them to
   2361     // make sure the compiler doesn't re-inline them even if we break the calls apart.
   2362     if (kIntel_GrGLVendor == ctxInfo.vendor()) {
   2363         shaderCaps->fMustDoOpBetweenFloorAndAbs = true;
   2364     }
   2365 
   2366     // On Adreno devices with framebuffer fetch support, there is a bug where they always return
   2367     // the original dst color when reading the outColor even after being written to. By using a
   2368     // local outColor we can work around this bug.
   2369     if (shaderCaps->fFBFetchSupport && kQualcomm_GrGLVendor == ctxInfo.vendor()) {
   2370         shaderCaps->fRequiresLocalOutputColorForFBFetch = true;
   2371     }
   2372 
   2373     // Newer Mali GPUs do incorrect static analysis in specific situations: If there is uniform
   2374     // color, and that uniform contains an opaque color, and the output of the shader is only based
   2375     // on that uniform plus soemthing un-trackable (like a texture read), the compiler will deduce
   2376     // that the shader always outputs opaque values. In that case, it appears to remove the shader
   2377     // based blending code it normally injects, turning SrcOver into Src. To fix this, we always
   2378     // insert an extra bit of math on the uniform that confuses the compiler just enough...
   2379     if (kMaliT_GrGLRenderer == ctxInfo.renderer()) {
   2380         shaderCaps->fMustObfuscateUniformColor = true;
   2381     }
   2382 #ifdef SK_BUILD_FOR_WIN
   2383     // Check for ANGLE on Windows, so we can workaround a bug in D3D itself (anglebug.com/2098).
   2384     //
   2385     // Basically, if a shader has a construct like:
   2386     //
   2387     // float x = someCondition ? someValue : 0;
   2388     // float2 result = (0 == x) ? float2(x, x)
   2389     //                          : float2(2 * x / x, 0);
   2390     //
   2391     // ... the compiler will produce an error 'NaN and infinity literals not allowed', even though
   2392     // we've explicitly guarded the division with a check against zero. This manifests in much
   2393     // more complex ways in some of our shaders, so we use this caps bit to add an epsilon value
   2394     // to the denominator of divisions, even when we've added checks that the denominator isn't 0.
   2395     if (kANGLE_GrGLDriver == ctxInfo.driver() || kChromium_GrGLDriver == ctxInfo.driver()) {
   2396         shaderCaps->fMustGuardDivisionEvenAfterExplicitZeroCheck = true;
   2397     }
   2398 #endif
   2399 
   2400     // We've seen Adreno 3xx devices produce incorrect (flipped) values for gl_FragCoord, in some
   2401     // (rare) situations. It's sporadic, and mostly on older drivers. It also seems to be the case
   2402     // that the interpolation of vertex shader outputs is quite inaccurate.
   2403     if (kAdreno3xx_GrGLRenderer == ctxInfo.renderer()) {
   2404         shaderCaps->fCanUseFragCoord = false;
   2405         shaderCaps->fInterpolantsAreInaccurate = true;
   2406     }
   2407 
   2408     // Disabling advanced blend on various platforms with major known issues. We also block Chrome
   2409     // for now until its own blacklists can be updated.
   2410     if (kAdreno4xx_GrGLRenderer == ctxInfo.renderer() ||
   2411         kAdreno5xx_GrGLRenderer == ctxInfo.renderer() ||
   2412         kIntel_GrGLDriver == ctxInfo.driver() ||
   2413         kChromium_GrGLDriver == ctxInfo.driver()) {
   2414         fBlendEquationSupport = kBasic_BlendEquationSupport;
   2415         shaderCaps->fAdvBlendEqInteraction = GrShaderCaps::kNotSupported_AdvBlendEqInteraction;
   2416     }
   2417 
   2418     // Non-coherent advanced blend has an issue on NVIDIA pre 337.00.
   2419     if (kNVIDIA_GrGLDriver == ctxInfo.driver() &&
   2420         ctxInfo.driverVersion() < GR_GL_DRIVER_VER(337,00) &&
   2421         kAdvanced_BlendEquationSupport == fBlendEquationSupport) {
   2422         fBlendEquationSupport = kBasic_BlendEquationSupport;
   2423         shaderCaps->fAdvBlendEqInteraction = GrShaderCaps::kNotSupported_AdvBlendEqInteraction;
   2424     }
   2425 
   2426     if (this->advancedBlendEquationSupport()) {
   2427         if (kNVIDIA_GrGLDriver == ctxInfo.driver() &&
   2428             ctxInfo.driverVersion() < GR_GL_DRIVER_VER(355,00)) {
   2429             // Blacklist color-dodge and color-burn on pre-355.00 NVIDIA.
   2430             fAdvBlendEqBlacklist |= (1 << kColorDodge_GrBlendEquation) |
   2431                                     (1 << kColorBurn_GrBlendEquation);
   2432         }
   2433         if (kARM_GrGLVendor == ctxInfo.vendor()) {
   2434             // Blacklist color-burn on ARM until the fix is released.
   2435             fAdvBlendEqBlacklist |= (1 << kColorBurn_GrBlendEquation);
   2436         }
   2437     }
   2438 
   2439     // Workaround NVIDIA bug related to glInvalidateFramebuffer and mixed samples.
   2440     if (fMultisampleDisableSupport &&
   2441         this->shaderCaps()->dualSourceBlendingSupport() &&
   2442         this->shaderCaps()->pathRenderingSupport() &&
   2443         fUsesMixedSamples &&
   2444 #if GR_TEST_UTILS
   2445         (contextOptions.fGpuPathRenderers & GpuPathRenderers::kStencilAndCover) &&
   2446 #endif
   2447         (kNVIDIA_GrGLDriver == ctxInfo.driver() ||
   2448          kChromium_GrGLDriver == ctxInfo.driver())) {
   2449             fDiscardRenderTargetSupport = false;
   2450             fInvalidateFBType = kNone_InvalidateFBType;
   2451     }
   2452 }
   2453 
   2454 void GrGLCaps::onApplyOptionsOverrides(const GrContextOptions& options) {
   2455     if (options.fDisableDriverCorrectnessWorkarounds) {
   2456         SkASSERT(!fDoManualMipmapping);
   2457         SkASSERT(!fClearToBoundaryValuesIsBroken);
   2458         SkASSERT(0 == fMaxInstancesPerDrawArraysWithoutCrashing);
   2459         SkASSERT(!fDrawArraysBaseVertexIsBroken);
   2460         SkASSERT(!fUseDrawToClearColor);
   2461         SkASSERT(!fUseDrawToClearStencilClip);
   2462         SkASSERT(!fDisallowTexSubImageForUnormConfigTexturesEverBoundToFBO);
   2463         SkASSERT(!fUseDrawInsteadOfAllRenderTargetWrites);
   2464         SkASSERT(!fRequiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines);
   2465     }
   2466     if (options.fUseDrawInsteadOfPartialRenderTargetWrite) {
   2467         fUseDrawInsteadOfAllRenderTargetWrites = true;
   2468     }
   2469     if (GrContextOptions::Enable::kNo == options.fUseDrawInsteadOfGLClear) {
   2470         fUseDrawToClearColor = false;
   2471     } else if (GrContextOptions::Enable::kYes == options.fUseDrawInsteadOfGLClear) {
   2472         fUseDrawToClearColor = true;
   2473     }
   2474     if (options.fDoManualMipmapping) {
   2475         fDoManualMipmapping = true;
   2476     }
   2477 }
   2478 
   2479 int GrGLCaps::getSampleCount(int requestedCount, GrPixelConfig config) const {
   2480     requestedCount = SkTMax(1, requestedCount);
   2481     int count = fConfigTable[config].fColorSampleCounts.count();
   2482     if (!count || !this->isConfigRenderable(config, requestedCount > 1)) {
   2483         return 0;
   2484     }
   2485 
   2486     if (1 == requestedCount) {
   2487         return fConfigTable[config].fColorSampleCounts[0] == 1 ? 1 : 0;
   2488     }
   2489 
   2490     for (int i = 0; i < count; ++i) {
   2491         if (fConfigTable[config].fColorSampleCounts[i] >= requestedCount) {
   2492             return fConfigTable[config].fColorSampleCounts[i];
   2493         }
   2494     }
   2495     return 0;
   2496 }
   2497 
   2498 bool validate_sized_format(GrGLenum format, SkColorType ct, GrPixelConfig* config,
   2499                            GrGLStandard standard) {
   2500     *config = kUnknown_GrPixelConfig;
   2501 
   2502     switch (ct) {
   2503         case kUnknown_SkColorType:
   2504             return false;
   2505         case kAlpha_8_SkColorType:
   2506             if (GR_GL_ALPHA8 == format) {
   2507                 *config = kAlpha_8_as_Alpha_GrPixelConfig;
   2508             } else if (GR_GL_R8 == format) {
   2509                 *config = kAlpha_8_as_Red_GrPixelConfig;
   2510             }
   2511             break;
   2512         case kRGB_565_SkColorType:
   2513             if (GR_GL_RGB565 == format) {
   2514                 *config = kRGB_565_GrPixelConfig;
   2515             }
   2516             break;
   2517         case kARGB_4444_SkColorType:
   2518             if (GR_GL_RGBA4 == format) {
   2519                 *config = kRGBA_4444_GrPixelConfig;
   2520             }
   2521             break;
   2522         case kRGBA_8888_SkColorType:
   2523             if (GR_GL_RGBA8 == format) {
   2524                 *config = kRGBA_8888_GrPixelConfig;
   2525             } else if (GR_GL_SRGB8_ALPHA8 == format) {
   2526                 *config = kSRGBA_8888_GrPixelConfig;
   2527             }
   2528             break;
   2529         case kRGB_888x_SkColorType:
   2530             return false;
   2531         case kBGRA_8888_SkColorType:
   2532             if (GR_GL_RGBA8 == format) {
   2533                 if (kGL_GrGLStandard == standard) {
   2534                     *config = kBGRA_8888_GrPixelConfig;
   2535                 }
   2536             } else if (GR_GL_BGRA8 == format) {
   2537                 if (kGLES_GrGLStandard == standard) {
   2538                     *config = kBGRA_8888_GrPixelConfig;
   2539                 }
   2540             } else if (GR_GL_SRGB8_ALPHA8 == format) {
   2541                 *config = kSBGRA_8888_GrPixelConfig;
   2542             }
   2543             break;
   2544         case kRGBA_1010102_SkColorType:
   2545             return false;
   2546         case kRGB_101010x_SkColorType:
   2547             return false;
   2548         case kGray_8_SkColorType:
   2549             if (GR_GL_LUMINANCE8 == format) {
   2550                 *config = kGray_8_as_Lum_GrPixelConfig;
   2551             } else if (GR_GL_R8 == format) {
   2552                 *config = kGray_8_as_Red_GrPixelConfig;
   2553             }
   2554             break;
   2555         case kRGBA_F16_SkColorType:
   2556             if (GR_GL_RGBA16F == format) {
   2557                 *config = kRGBA_half_GrPixelConfig;
   2558             }
   2559             break;
   2560     }
   2561 
   2562     return kUnknown_GrPixelConfig != *config;
   2563 }
   2564 
   2565 bool GrGLCaps::validateBackendTexture(const GrBackendTexture& tex, SkColorType ct,
   2566                                       GrPixelConfig* config) const {
   2567     const GrGLTextureInfo* texInfo = tex.getGLTextureInfo();
   2568     if (!texInfo) {
   2569         return false;
   2570     }
   2571     return validate_sized_format(texInfo->fFormat, ct, config, fStandard);
   2572 }
   2573 
   2574 bool GrGLCaps::validateBackendRenderTarget(const GrBackendRenderTarget& rt, SkColorType ct,
   2575                                            GrPixelConfig* config) const {
   2576     const GrGLFramebufferInfo* fbInfo = rt.getGLFramebufferInfo();
   2577     if (!fbInfo) {
   2578         return false;
   2579     }
   2580     return validate_sized_format(fbInfo->fFormat, ct, config, fStandard);
   2581 }
   2582 
   2583