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