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