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