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 "GrGLContext.h"
12#include "SkTSearch.h"
13#include "SkTSort.h"
14
15GrGLCaps::GrGLCaps() {
16    this->reset();
17}
18
19void GrGLCaps::reset() {
20    INHERITED::reset();
21
22    fVerifiedColorConfigs.reset();
23    fStencilFormats.reset();
24    fStencilVerifiedColorConfigs.reset();
25    fMSFBOType = kNone_MSFBOType;
26    fInvalidateFBType = kNone_InvalidateFBType;
27    fLATCAlias = kLATC_LATCAlias;
28    fMapBufferType = kNone_MapBufferType;
29    fMaxFragmentUniformVectors = 0;
30    fMaxVertexAttributes = 0;
31    fMaxFragmentTextureUnits = 0;
32    fRGBA8RenderbufferSupport = false;
33    fBGRAIsInternalFormat = false;
34    fTextureSwizzleSupport = false;
35    fUnpackRowLengthSupport = false;
36    fUnpackFlipYSupport = false;
37    fPackRowLengthSupport = false;
38    fPackFlipYSupport = false;
39    fTextureUsageSupport = false;
40    fTexStorageSupport = false;
41    fTextureRedSupport = false;
42    fImagingSupport = false;
43    fTwoFormatLimit = false;
44    fFragCoordsConventionSupport = false;
45    fVertexArrayObjectSupport = false;
46    fES2CompatibilitySupport = false;
47    fUseNonVBOVertexAndIndexDynamicData = false;
48    fIsCoreProfile = false;
49    fFullClearIsFree = false;
50    fFBMixedSamplesSupport = false;
51
52    fReadPixelsSupportedCache.reset();
53
54    fShaderCaps.reset(SkNEW(GrGLSLCaps));
55
56}
57
58GrGLCaps::GrGLCaps(const GrGLCaps& caps) : GrDrawTargetCaps() {
59    *this = caps;
60}
61
62GrGLCaps& GrGLCaps::operator= (const GrGLCaps& caps) {
63    INHERITED::operator=(caps);
64    fVerifiedColorConfigs = caps.fVerifiedColorConfigs;
65    fStencilFormats = caps.fStencilFormats;
66    fStencilVerifiedColorConfigs = caps.fStencilVerifiedColorConfigs;
67    fLATCAlias = caps.fLATCAlias;
68    fMaxFragmentUniformVectors = caps.fMaxFragmentUniformVectors;
69    fMaxVertexAttributes = caps.fMaxVertexAttributes;
70    fMaxFragmentTextureUnits = caps.fMaxFragmentTextureUnits;
71    fMSFBOType = caps.fMSFBOType;
72    fInvalidateFBType = caps.fInvalidateFBType;
73    fMapBufferType = caps.fMapBufferType;
74    fRGBA8RenderbufferSupport = caps.fRGBA8RenderbufferSupport;
75    fBGRAIsInternalFormat = caps.fBGRAIsInternalFormat;
76    fTextureSwizzleSupport = caps.fTextureSwizzleSupport;
77    fUnpackRowLengthSupport = caps.fUnpackRowLengthSupport;
78    fUnpackFlipYSupport = caps.fUnpackFlipYSupport;
79    fPackRowLengthSupport = caps.fPackRowLengthSupport;
80    fPackFlipYSupport = caps.fPackFlipYSupport;
81    fTextureUsageSupport = caps.fTextureUsageSupport;
82    fTexStorageSupport = caps.fTexStorageSupport;
83    fTextureRedSupport = caps.fTextureRedSupport;
84    fImagingSupport = caps.fImagingSupport;
85    fTwoFormatLimit = caps.fTwoFormatLimit;
86    fFragCoordsConventionSupport = caps.fFragCoordsConventionSupport;
87    fVertexArrayObjectSupport = caps.fVertexArrayObjectSupport;
88    fES2CompatibilitySupport = caps.fES2CompatibilitySupport;
89    fUseNonVBOVertexAndIndexDynamicData = caps.fUseNonVBOVertexAndIndexDynamicData;
90    fIsCoreProfile = caps.fIsCoreProfile;
91    fFullClearIsFree = caps.fFullClearIsFree;
92    fFBMixedSamplesSupport = caps.fFBMixedSamplesSupport;
93
94    *(reinterpret_cast<GrGLSLCaps*>(fShaderCaps.get())) =
95                                          *(reinterpret_cast<GrGLSLCaps*>(caps.fShaderCaps.get()));
96
97    return *this;
98}
99
100bool GrGLCaps::init(const GrGLContextInfo& ctxInfo, const GrGLInterface* gli) {
101
102    this->reset();
103    if (!ctxInfo.isInitialized()) {
104        return false;
105    }
106
107    GrGLStandard standard = ctxInfo.standard();
108    GrGLVersion version = ctxInfo.version();
109
110    /**************************************************************************
111     * Caps specific to GrGLCaps
112     **************************************************************************/
113
114    if (kGLES_GrGLStandard == standard) {
115        GR_GL_GetIntegerv(gli, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
116                          &fMaxFragmentUniformVectors);
117    } else {
118        SkASSERT(kGL_GrGLStandard == standard);
119        GrGLint max;
120        GR_GL_GetIntegerv(gli, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
121        fMaxFragmentUniformVectors = max / 4;
122        if (version >= GR_GL_VER(3, 2)) {
123            GrGLint profileMask;
124            GR_GL_GetIntegerv(gli, GR_GL_CONTEXT_PROFILE_MASK, &profileMask);
125            fIsCoreProfile = SkToBool(profileMask & GR_GL_CONTEXT_CORE_PROFILE_BIT);
126        }
127    }
128    GR_GL_GetIntegerv(gli, GR_GL_MAX_VERTEX_ATTRIBS, &fMaxVertexAttributes);
129    GR_GL_GetIntegerv(gli, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &fMaxFragmentTextureUnits);
130
131    if (kGL_GrGLStandard == standard) {
132        fRGBA8RenderbufferSupport = true;
133    } else {
134        fRGBA8RenderbufferSupport = version >= GR_GL_VER(3,0) ||
135                                    ctxInfo.hasExtension("GL_OES_rgb8_rgba8") ||
136                                    ctxInfo.hasExtension("GL_ARM_rgba8");
137    }
138
139    if (kGL_GrGLStandard == standard) {
140        fTextureSwizzleSupport = version >= GR_GL_VER(3,3) ||
141                                 ctxInfo.hasExtension("GL_ARB_texture_swizzle");
142    } else {
143        fTextureSwizzleSupport = version >= GR_GL_VER(3,0);
144    }
145
146    if (kGL_GrGLStandard == standard) {
147        fUnpackRowLengthSupport = true;
148        fUnpackFlipYSupport = false;
149        fPackRowLengthSupport = true;
150        fPackFlipYSupport = false;
151    } else {
152        fUnpackRowLengthSupport = version >= GR_GL_VER(3,0) ||
153                                  ctxInfo.hasExtension("GL_EXT_unpack_subimage");
154        fUnpackFlipYSupport = ctxInfo.hasExtension("GL_CHROMIUM_flipy");
155        fPackRowLengthSupport = version >= GR_GL_VER(3,0) ||
156                                ctxInfo.hasExtension("GL_NV_pack_subimage");
157        fPackFlipYSupport =
158            ctxInfo.hasExtension("GL_ANGLE_pack_reverse_row_order");
159    }
160
161    fTextureUsageSupport = (kGLES_GrGLStandard == standard) &&
162                            ctxInfo.hasExtension("GL_ANGLE_texture_usage");
163
164    if (kGL_GrGLStandard == standard) {
165        // The EXT version can apply to either GL or GLES.
166        fTexStorageSupport = version >= GR_GL_VER(4,2) ||
167                             ctxInfo.hasExtension("GL_ARB_texture_storage") ||
168                             ctxInfo.hasExtension("GL_EXT_texture_storage");
169    } else {
170        // Qualcomm Adreno drivers appear to have issues with texture storage.
171        fTexStorageSupport = (version >= GR_GL_VER(3,0) &&
172                              kQualcomm_GrGLVendor != ctxInfo.vendor()) ||
173                             ctxInfo.hasExtension("GL_EXT_texture_storage");
174    }
175
176    if (kGL_GrGLStandard == standard) {
177        fTextureBarrierSupport = version >= GR_GL_VER(4,5) ||
178                                 ctxInfo.hasExtension("GL_ARB_texture_barrier") ||
179                                 ctxInfo.hasExtension("GL_NV_texture_barrier");
180    } else {
181        fTextureBarrierSupport = ctxInfo.hasExtension("GL_NV_texture_barrier");
182    }
183
184    // ARB_texture_rg is part of OpenGL 3.0, but mesa doesn't support GL_RED
185    // and GL_RG on FBO textures.
186    if (!ctxInfo.isMesa()) {
187        if (kGL_GrGLStandard == standard) {
188            fTextureRedSupport = version >= GR_GL_VER(3,0) ||
189                                 ctxInfo.hasExtension("GL_ARB_texture_rg");
190        } else {
191            fTextureRedSupport =  version >= GR_GL_VER(3,0) ||
192                                  ctxInfo.hasExtension("GL_EXT_texture_rg");
193        }
194    }
195    fImagingSupport = kGL_GrGLStandard == standard &&
196                      ctxInfo.hasExtension("GL_ARB_imaging");
197
198    // ES 2 only guarantees RGBA/uchar + one other format/type combo for
199    // ReadPixels. The other format has to checked at run-time since it
200    // can change based on which render target is bound
201    fTwoFormatLimit = kGLES_GrGLStandard == standard;
202
203    // Frag Coords Convention support is not part of ES
204    // Known issue on at least some Intel platforms:
205    // http://code.google.com/p/skia/issues/detail?id=946
206    if (kIntel_GrGLVendor != ctxInfo.vendor() && kGLES_GrGLStandard != standard) {
207        fFragCoordsConventionSupport = ctxInfo.glslGeneration() >= k150_GrGLSLGeneration ||
208                                       ctxInfo.hasExtension("GL_ARB_fragment_coord_conventions");
209    }
210
211    // SGX and Mali GPUs that are based on a tiled-deferred architecture that have trouble with
212    // frequently changing VBOs. We've measured a performance increase using non-VBO vertex
213    // data for dynamic content on these GPUs. Perhaps we should read the renderer string and
214    // limit this decision to specific GPU families rather than basing it on the vendor alone.
215    if (!GR_GL_MUST_USE_VBO &&
216        (kARM_GrGLVendor == ctxInfo.vendor() ||
217         kImagination_GrGLVendor == ctxInfo.vendor() ||
218         kQualcomm_GrGLVendor == ctxInfo.vendor())) {
219        fUseNonVBOVertexAndIndexDynamicData = true;
220    }
221
222    if ((kGL_GrGLStandard == standard && version >= GR_GL_VER(4,3)) ||
223        (kGLES_GrGLStandard == standard && version >= GR_GL_VER(3,0)) ||
224        ctxInfo.hasExtension("GL_ARB_invalidate_subdata")) {
225        fDiscardRenderTargetSupport = true;
226        fInvalidateFBType = kInvalidate_InvalidateFBType;
227    } else if (ctxInfo.hasExtension("GL_EXT_discard_framebuffer")) {
228        fDiscardRenderTargetSupport = true;
229        fInvalidateFBType = kDiscard_InvalidateFBType;
230    }
231
232    if (kARM_GrGLVendor == ctxInfo.vendor() || kImagination_GrGLVendor == ctxInfo.vendor()) {
233        fFullClearIsFree = true;
234    }
235
236    if (kGL_GrGLStandard == standard) {
237        fVertexArrayObjectSupport = version >= GR_GL_VER(3, 0) ||
238                                    ctxInfo.hasExtension("GL_ARB_vertex_array_object") ||
239                                    ctxInfo.hasExtension("GL_APPLE_vertex_array_object");
240    } else {
241        fVertexArrayObjectSupport = version >= GR_GL_VER(3, 0) ||
242                                    ctxInfo.hasExtension("GL_OES_vertex_array_object");
243    }
244
245    if (kGL_GrGLStandard == standard) {
246        fES2CompatibilitySupport = ctxInfo.hasExtension("GL_ARB_ES2_compatibility");
247    }
248    else {
249        fES2CompatibilitySupport = true;
250    }
251
252    this->initFSAASupport(ctxInfo, gli);
253    this->initStencilFormats(ctxInfo);
254
255    /**************************************************************************
256     * GrDrawTargetCaps fields
257     **************************************************************************/
258    if (kGL_GrGLStandard == standard) {
259        // we could also look for GL_ATI_separate_stencil extension or
260        // GL_EXT_stencil_two_side but they use different function signatures
261        // than GL2.0+ (and than each other).
262        fTwoSidedStencilSupport = (ctxInfo.version() >= GR_GL_VER(2,0));
263        // supported on GL 1.4 and higher or by extension
264        fStencilWrapOpsSupport = (ctxInfo.version() >= GR_GL_VER(1,4)) ||
265                                  ctxInfo.hasExtension("GL_EXT_stencil_wrap");
266    } else {
267        // ES 2 has two sided stencil and stencil wrap
268        fTwoSidedStencilSupport = true;
269        fStencilWrapOpsSupport = true;
270    }
271
272// Disabling advanced blend until we can resolve various bugs
273#if 0
274    if (kIntel_GrGLVendor != ctxInfo.vendor()) {
275        if (ctxInfo.hasExtension("GL_KHR_blend_equation_advanced_coherent") ||
276            ctxInfo.hasExtension("GL_NV_blend_equation_advanced_coherent")) {
277            fBlendEquationSupport = kAdvancedCoherent_BlendEquationSupport;
278        } else if (ctxInfo.hasExtension("GL_KHR_blend_equation_advanced") ||
279                   ctxInfo.hasExtension("GL_NV_blend_equation_advanced")) {
280            fBlendEquationSupport = kAdvanced_BlendEquationSupport;
281        } else {
282            fBlendEquationSupport = kBasic_BlendEquationSupport;
283        }
284    } else {
285        // On Intel platforms, KHR_blend_equation_advanced is not conformant.
286        fBlendEquationSupport = kBasic_BlendEquationSupport;
287    }
288#endif
289    if (kGL_GrGLStandard == standard) {
290        fMapBufferFlags = kCanMap_MapFlag; // we require VBO support and the desktop VBO
291                                            // extension includes glMapBuffer.
292        if (version >= GR_GL_VER(3, 0) || ctxInfo.hasExtension("GL_ARB_map_buffer_range")) {
293            fMapBufferFlags |= kSubset_MapFlag;
294            fMapBufferType = kMapBufferRange_MapBufferType;
295        } else {
296            fMapBufferType = kMapBuffer_MapBufferType;
297        }
298    } else {
299        // Unextended GLES2 doesn't have any buffer mapping.
300        fMapBufferFlags = kNone_MapBufferType;
301        if (ctxInfo.hasExtension("GL_CHROMIUM_map_sub")) {
302            fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
303            fMapBufferType = kChromium_MapBufferType;
304        } else if (version >= GR_GL_VER(3, 0) || ctxInfo.hasExtension("GL_EXT_map_buffer_range")) {
305            fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
306            fMapBufferType = kMapBufferRange_MapBufferType;
307        } else if (ctxInfo.hasExtension("GL_OES_mapbuffer")) {
308            fMapBufferFlags = kCanMap_MapFlag;
309            fMapBufferType = kMapBuffer_MapBufferType;
310        }
311    }
312
313    if (kGL_GrGLStandard == standard) {
314        SkASSERT(ctxInfo.version() >= GR_GL_VER(2,0) ||
315                 ctxInfo.hasExtension("GL_ARB_texture_non_power_of_two"));
316        fNPOTTextureTileSupport = true;
317        fMipMapSupport = true;
318    } else {
319        // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
320        // ES3 has no limitations.
321        fNPOTTextureTileSupport = ctxInfo.version() >= GR_GL_VER(3,0) ||
322                                  ctxInfo.hasExtension("GL_OES_texture_npot");
323        // ES2 supports MIP mapping for POT textures but our caps don't allow for limited MIP
324        // support. The OES extension or ES 3.0 allow for MIPS on NPOT textures. So, apparently,
325        // does the undocumented GL_IMG_texture_npot extension. This extension does not seem to
326        // to alllow arbitrary wrap modes, however.
327        fMipMapSupport = fNPOTTextureTileSupport || ctxInfo.hasExtension("GL_IMG_texture_npot");
328    }
329
330    GR_GL_GetIntegerv(gli, GR_GL_MAX_TEXTURE_SIZE, &fMaxTextureSize);
331    GR_GL_GetIntegerv(gli, GR_GL_MAX_RENDERBUFFER_SIZE, &fMaxRenderTargetSize);
332    // Our render targets are always created with textures as the color
333    // attachment, hence this min:
334    fMaxRenderTargetSize = SkTMin(fMaxTextureSize, fMaxRenderTargetSize);
335
336    fFBMixedSamplesSupport = ctxInfo.hasExtension("GL_NV_framebuffer_mixed_samples");
337
338    fGpuTracingSupport = ctxInfo.hasExtension("GL_EXT_debug_marker");
339
340    // Disable scratch texture reuse on Mali and Adreno devices
341    fReuseScratchTextures = kARM_GrGLVendor != ctxInfo.vendor() &&
342                            kQualcomm_GrGLVendor != ctxInfo.vendor();
343
344    if (GrGLCaps::kES_IMG_MsToTexture_MSFBOType == fMSFBOType) {
345        GR_GL_GetIntegerv(gli, GR_GL_MAX_SAMPLES_IMG, &fMaxSampleCount);
346    } else if (GrGLCaps::kNone_MSFBOType != fMSFBOType) {
347        GR_GL_GetIntegerv(gli, GR_GL_MAX_SAMPLES, &fMaxSampleCount);
348    }
349
350    if (kPowerVR54x_GrGLRenderer == ctxInfo.renderer() ||
351        kPowerVRRogue_GrGLRenderer == ctxInfo.renderer() ||
352        kAdreno3xx_GrGLRenderer == ctxInfo.renderer()) {
353        fUseDrawInsteadOfClear = true;
354    }
355
356    if (kGL_GrGLStandard == standard) {
357        // ARB allows mixed size FBO attachments, EXT does not.
358        if (ctxInfo.version() >= GR_GL_VER(3, 0) ||
359            ctxInfo.hasExtension("GL_ARB_framebuffer_object")) {
360            fOversizedStencilSupport = true;
361        } else {
362            SkASSERT(ctxInfo.hasExtension("GL_EXT_framebuffer_object"));
363        }
364    } else {
365        // ES 3.0 supports mixed size FBO attachments, 2.0 does not.
366        fOversizedStencilSupport = ctxInfo.version() >= GR_GL_VER(3, 0);
367    }
368
369    this->initConfigTexturableTable(ctxInfo, gli);
370    this->initConfigRenderableTable(ctxInfo);
371
372    reinterpret_cast<GrGLSLCaps*>(fShaderCaps.get())->init(ctxInfo, gli, *this);
373
374    return true;
375}
376
377void GrGLCaps::initConfigRenderableTable(const GrGLContextInfo& ctxInfo) {
378    // OpenGL < 3.0
379    //  no support for render targets unless the GL_ARB_framebuffer_object
380    //  extension is supported (in which case we get ALPHA, RED, RG, RGB,
381    //  RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
382    //  probably don't get R8 in this case.
383
384    // OpenGL 3.0
385    //  base color renderable: ALPHA, RED, RG, RGB, and RGBA
386    //  sized derivatives: ALPHA8, R8, RGBA4, RGBA8
387
388    // >= OpenGL 3.1
389    //  base color renderable: RED, RG, RGB, and RGBA
390    //  sized derivatives: R8, RGBA4, RGBA8
391    //  if the GL_ARB_compatibility extension is supported then we get back
392    //  support for GL_ALPHA and ALPHA8
393
394    // GL_EXT_bgra adds BGRA render targets to any version
395
396    // ES 2.0
397    //  color renderable: RGBA4, RGB5_A1, RGB565
398    //  GL_EXT_texture_rg adds support for R8 as a color render target
399    //  GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
400    //  GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888 added BGRA support
401
402    // ES 3.0
403    // Same as ES 2.0 except R8 and RGBA8 are supported without extensions (the functions called
404    // below already account for this).
405
406    GrGLStandard standard = ctxInfo.standard();
407
408    enum {
409        kNo_MSAA = 0,
410        kYes_MSAA = 1,
411    };
412
413    if (kGL_GrGLStandard == standard) {
414        // Post 3.0 we will get R8
415        // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
416        if (ctxInfo.version() >= GR_GL_VER(3,0) ||
417            ctxInfo.hasExtension("GL_ARB_framebuffer_object")) {
418            fConfigRenderSupport[kAlpha_8_GrPixelConfig][kNo_MSAA] = true;
419            fConfigRenderSupport[kAlpha_8_GrPixelConfig][kYes_MSAA] = true;
420        }
421    } else {
422        // On ES we can only hope for R8
423        fConfigRenderSupport[kAlpha_8_GrPixelConfig][kNo_MSAA] = fTextureRedSupport;
424        fConfigRenderSupport[kAlpha_8_GrPixelConfig][kYes_MSAA] = fTextureRedSupport;
425    }
426
427    if (kGL_GrGLStandard != standard) {
428        // only available in ES
429        fConfigRenderSupport[kRGB_565_GrPixelConfig][kNo_MSAA] = true;
430        fConfigRenderSupport[kRGB_565_GrPixelConfig][kYes_MSAA] = true;
431    }
432
433    // we no longer support 444 as a render target
434    fConfigRenderSupport[kRGBA_4444_GrPixelConfig][kNo_MSAA]  = false;
435    fConfigRenderSupport[kRGBA_4444_GrPixelConfig][kYes_MSAA]  = false;
436
437    if (this->fRGBA8RenderbufferSupport) {
438        fConfigRenderSupport[kRGBA_8888_GrPixelConfig][kNo_MSAA]  = true;
439        fConfigRenderSupport[kRGBA_8888_GrPixelConfig][kYes_MSAA]  = true;
440    }
441
442    if (this->isConfigTexturable(kBGRA_8888_GrPixelConfig)) {
443        fConfigRenderSupport[kBGRA_8888_GrPixelConfig][kNo_MSAA]  = true;
444        // The GL_EXT_texture_format_BGRA8888 extension does not add BGRA to the list of
445        // configs that are color-renderable and can be passed to glRenderBufferStorageMultisample.
446        // Chromium may have an extension to allow BGRA renderbuffers to work on desktop platforms.
447        if (ctxInfo.hasExtension("GL_CHROMIUM_renderbuffer_format_BGRA8888")) {
448            fConfigRenderSupport[kBGRA_8888_GrPixelConfig][kYes_MSAA] = true;
449        } else {
450            fConfigRenderSupport[kBGRA_8888_GrPixelConfig][kYes_MSAA] =
451                !fBGRAIsInternalFormat || !this->usesMSAARenderBuffers();
452        }
453    }
454
455    if (this->fRGBA8RenderbufferSupport && this->isConfigTexturable(kSRGBA_8888_GrPixelConfig)) {
456        if (kGL_GrGLStandard == standard) {
457            if (ctxInfo.version() >= GR_GL_VER(3,0) ||
458                ctxInfo.hasExtension("GL_ARB_framebuffer_sRGB") ||
459                ctxInfo.hasExtension("GL_EXT_framebuffer_sRGB")) {
460                fConfigRenderSupport[kSRGBA_8888_GrPixelConfig][kNo_MSAA] = true;
461                fConfigRenderSupport[kSRGBA_8888_GrPixelConfig][kYes_MSAA] = true;
462            }
463        } else {
464            if (ctxInfo.version() >= GR_GL_VER(3,0) ||
465                ctxInfo.hasExtension("GL_EXT_sRGB")) {
466                fConfigRenderSupport[kSRGBA_8888_GrPixelConfig][kNo_MSAA] = true;
467                fConfigRenderSupport[kSRGBA_8888_GrPixelConfig][kYes_MSAA] = true;
468            }
469        }
470    }
471
472    if (this->isConfigTexturable(kRGBA_float_GrPixelConfig)) {
473        if (kGL_GrGLStandard == standard) {
474            fConfigRenderSupport[kRGBA_float_GrPixelConfig][kNo_MSAA] = true;
475            fConfigRenderSupport[kRGBA_float_GrPixelConfig][kYes_MSAA] = true;
476        } else {
477            if (ctxInfo.hasExtension("GL_EXT_color_buffer_float")) {
478                fConfigRenderSupport[kRGBA_float_GrPixelConfig][kNo_MSAA] = true;
479            } else {
480                fConfigRenderSupport[kRGBA_float_GrPixelConfig][kNo_MSAA] = false;
481            }
482            // for now we don't support floating point MSAA on ES
483            fConfigRenderSupport[kAlpha_half_GrPixelConfig][kYes_MSAA] = false;
484        }
485    }
486
487    if (this->isConfigTexturable(kAlpha_half_GrPixelConfig)) {
488        if (kGL_GrGLStandard == standard) {
489            fConfigRenderSupport[kAlpha_half_GrPixelConfig][kNo_MSAA] = true;
490            fConfigRenderSupport[kAlpha_half_GrPixelConfig][kYes_MSAA] = true;
491        } else if (ctxInfo.version() >= GR_GL_VER(3,0)) {
492            fConfigRenderSupport[kAlpha_half_GrPixelConfig][kNo_MSAA] = true;
493            // for now we don't support floating point MSAA on ES
494            fConfigRenderSupport[kAlpha_half_GrPixelConfig][kYes_MSAA] = false;
495        } else {
496            if (ctxInfo.hasExtension("GL_EXT_color_buffer_half_float") && fTextureRedSupport) {
497                fConfigRenderSupport[kAlpha_half_GrPixelConfig][kNo_MSAA] = true;
498            } else {
499                fConfigRenderSupport[kAlpha_half_GrPixelConfig][kNo_MSAA] = false;
500            }
501            // for now we don't support floating point MSAA on ES
502            fConfigRenderSupport[kAlpha_half_GrPixelConfig][kYes_MSAA] = false;
503        }
504    }
505
506    // If we don't support MSAA then undo any places above where we set a config as renderable with
507    // msaa.
508    if (kNone_MSFBOType == fMSFBOType) {
509        for (int i = 0; i < kGrPixelConfigCnt; ++i) {
510            fConfigRenderSupport[i][kYes_MSAA] = false;
511        }
512    }
513}
514
515void GrGLCaps::initConfigTexturableTable(const GrGLContextInfo& ctxInfo, const GrGLInterface* gli) {
516    GrGLStandard standard = ctxInfo.standard();
517    GrGLVersion version = ctxInfo.version();
518
519    // Base texture support
520    fConfigTextureSupport[kAlpha_8_GrPixelConfig] = true;
521    fConfigTextureSupport[kRGB_565_GrPixelConfig] = true;
522    fConfigTextureSupport[kRGBA_4444_GrPixelConfig] = true;
523    fConfigTextureSupport[kRGBA_8888_GrPixelConfig] = true;
524
525    // Check for 8-bit palette..
526    GrGLint numFormats;
527    GR_GL_GetIntegerv(gli, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
528    if (numFormats) {
529        SkAutoSTMalloc<10, GrGLint> formats(numFormats);
530        GR_GL_GetIntegerv(gli, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
531        for (int i = 0; i < numFormats; ++i) {
532            if (GR_GL_PALETTE8_RGBA8 == formats[i]) {
533                fConfigTextureSupport[kIndex_8_GrPixelConfig] = true;
534                break;
535            }
536        }
537    }
538
539    // Check for BGRA
540    if (kGL_GrGLStandard == standard) {
541        fConfigTextureSupport[kBGRA_8888_GrPixelConfig] =
542            version >= GR_GL_VER(1,2) || ctxInfo.hasExtension("GL_EXT_bgra");
543    } else {
544        if (ctxInfo.hasExtension("GL_APPLE_texture_format_BGRA8888")) {
545            fConfigTextureSupport[kBGRA_8888_GrPixelConfig] = true;
546        } else if (ctxInfo.hasExtension("GL_EXT_texture_format_BGRA8888")) {
547            fConfigTextureSupport[kBGRA_8888_GrPixelConfig] = true;
548            fBGRAIsInternalFormat = true;
549        }
550        SkASSERT(fConfigTextureSupport[kBGRA_8888_GrPixelConfig] ||
551                 kSkia8888_GrPixelConfig != kBGRA_8888_GrPixelConfig);
552    }
553
554    // Check for sRGBA
555    if (kGL_GrGLStandard == standard) {
556        fConfigTextureSupport[kSRGBA_8888_GrPixelConfig] =
557            (version >= GR_GL_VER(3,0) || ctxInfo.hasExtension("GL_EXT_texture_sRGB"));
558    } else {
559        fConfigTextureSupport[kSRGBA_8888_GrPixelConfig] =
560            (version >= GR_GL_VER(3,0) || ctxInfo.hasExtension("GL_EXT_sRGB"));
561    }
562
563    // Compressed texture support
564
565    // glCompressedTexImage2D is available on all OpenGL ES devices...
566    // however, it is only available on standard OpenGL after version 1.3
567    bool hasCompressTex2D = (kGL_GrGLStandard != standard || version >= GR_GL_VER(1, 3));
568
569    fCompressedTexSubImageSupport =
570        hasCompressTex2D && (gli->fFunctions.fCompressedTexSubImage2D);
571
572    // Check for ETC1
573    bool hasETC1 = false;
574
575    // First check version for support
576    if (kGL_GrGLStandard == standard) {
577        hasETC1 = hasCompressTex2D &&
578            (version >= GR_GL_VER(4, 3) ||
579             ctxInfo.hasExtension("GL_ARB_ES3_compatibility"));
580    } else {
581        hasETC1 = hasCompressTex2D &&
582            (version >= GR_GL_VER(3, 0) ||
583             ctxInfo.hasExtension("GL_OES_compressed_ETC1_RGB8_texture") ||
584             // ETC2 is a superset of ETC1, so we can just check for that, too.
585             (ctxInfo.hasExtension("GL_OES_compressed_ETC2_RGB8_texture") &&
586              ctxInfo.hasExtension("GL_OES_compressed_ETC2_RGBA8_texture")));
587    }
588    fConfigTextureSupport[kETC1_GrPixelConfig] = hasETC1;
589
590    // Check for LATC under its various forms
591    LATCAlias alias = kLATC_LATCAlias;
592    bool hasLATC = hasCompressTex2D &&
593        (ctxInfo.hasExtension("GL_EXT_texture_compression_latc") ||
594         ctxInfo.hasExtension("GL_NV_texture_compression_latc"));
595
596    // Check for RGTC
597    if (!hasLATC) {
598        // If we're using OpenGL 3.0 or later, then we have RGTC, an identical compression format.
599        if (kGL_GrGLStandard == standard) {
600            hasLATC = version >= GR_GL_VER(3, 0);
601        }
602
603        if (!hasLATC) {
604            hasLATC =
605                ctxInfo.hasExtension("GL_EXT_texture_compression_rgtc") ||
606                ctxInfo.hasExtension("GL_ARB_texture_compression_rgtc");
607        }
608
609        if (hasLATC) {
610            alias = kRGTC_LATCAlias;
611        }
612    }
613
614    // Check for 3DC
615    if (!hasLATC) {
616        hasLATC = ctxInfo.hasExtension("GL_AMD_compressed_3DC_texture");
617        if (hasLATC) {
618            alias = k3DC_LATCAlias;
619        }
620    }
621
622    fConfigTextureSupport[kLATC_GrPixelConfig] = hasLATC;
623    fLATCAlias = alias;
624
625    // Check for R11_EAC ... We don't support R11_EAC on desktop, as most
626    // cards default to decompressing the textures in the driver, and is
627    // generally slower.
628    if (kGL_GrGLStandard != standard) {
629        fConfigTextureSupport[kR11_EAC_GrPixelConfig] = version >= GR_GL_VER(3, 0);
630    }
631
632    // Check for ASTC
633    fConfigTextureSupport[kASTC_12x12_GrPixelConfig] =
634        ctxInfo.hasExtension("GL_KHR_texture_compression_astc_hdr") ||
635        ctxInfo.hasExtension("GL_KHR_texture_compression_astc_ldr") ||
636        ctxInfo.hasExtension("GL_OES_texture_compression_astc");
637
638    // Check for floating point texture support
639    // NOTE: We disallow floating point textures on ES devices if linear
640    // filtering modes are not supported.  This is for simplicity, but a more
641    // granular approach is possible.  Coincidentally, floating point textures became part of
642    // the standard in ES3.1 / OGL 3.1, hence the shorthand
643    bool hasFPTextures = version >= GR_GL_VER(3, 1);
644    if (!hasFPTextures) {
645        hasFPTextures = ctxInfo.hasExtension("GL_ARB_texture_float") ||
646                        (ctxInfo.hasExtension("GL_OES_texture_float_linear") &&
647                         ctxInfo.hasExtension("GL_OES_texture_float"));
648    }
649    fConfigTextureSupport[kRGBA_float_GrPixelConfig] = hasFPTextures;
650
651    // Check for fp16 texture support
652    // NOTE: We disallow floating point textures on ES devices if linear
653    // filtering modes are not supported.  This is for simplicity, but a more
654    // granular approach is possible.  Coincidentally, 16-bit floating point textures became part of
655    // the standard in ES3.1 / OGL 3.1, hence the shorthand
656    bool hasHalfFPTextures = version >= GR_GL_VER(3, 1);
657    if (!hasHalfFPTextures) {
658        hasHalfFPTextures = ctxInfo.hasExtension("GL_ARB_texture_float") ||
659                            (ctxInfo.hasExtension("GL_OES_texture_half_float_linear") &&
660                             ctxInfo.hasExtension("GL_OES_texture_half_float"));
661    }
662    fConfigTextureSupport[kAlpha_half_GrPixelConfig] = hasHalfFPTextures;
663}
664
665bool GrGLCaps::doReadPixelsSupported(const GrGLInterface* intf,
666                                     GrGLenum format,
667                                     GrGLenum type) const {
668    if (GR_GL_RGBA == format && GR_GL_UNSIGNED_BYTE == type) {
669        // ES 2 guarantees this format is supported
670        return true;
671    }
672
673    if (!fTwoFormatLimit) {
674        // not limited by ES 2's constraints
675        return true;
676    }
677
678    GrGLint otherFormat = GR_GL_RGBA;
679    GrGLint otherType = GR_GL_UNSIGNED_BYTE;
680
681    // The other supported format/type combo supported for ReadPixels
682    // can change based on which render target is bound
683    GR_GL_GetIntegerv(intf,
684                      GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT,
685                      &otherFormat);
686
687    GR_GL_GetIntegerv(intf,
688                      GR_GL_IMPLEMENTATION_COLOR_READ_TYPE,
689                      &otherType);
690
691    return (GrGLenum)otherFormat == format && (GrGLenum)otherType == type;
692}
693
694bool GrGLCaps::readPixelsSupported(const GrGLInterface* intf,
695                                   GrGLenum format,
696                                   GrGLenum type,
697                                   GrGLenum currFboFormat) const {
698    ReadPixelsSupportedFormat key = {format, type, currFboFormat};
699    if (const bool* supported = fReadPixelsSupportedCache.find(key)) {
700        return *supported;
701    }
702    bool supported = this->doReadPixelsSupported(intf, format, type);
703    fReadPixelsSupportedCache.set(key, supported);
704    return supported;
705}
706
707void GrGLCaps::initFSAASupport(const GrGLContextInfo& ctxInfo, const GrGLInterface* gli) {
708
709    fMSFBOType = kNone_MSFBOType;
710    if (kGL_GrGLStandard != ctxInfo.standard()) {
711        // We prefer the EXT/IMG extension over ES3 MSAA because we've observed
712        // ES3 driver bugs on at least one device with a tiled GPU (N10).
713        if (ctxInfo.hasExtension("GL_EXT_multisampled_render_to_texture")) {
714            fMSFBOType = kES_EXT_MsToTexture_MSFBOType;
715        } else if (ctxInfo.hasExtension("GL_IMG_multisampled_render_to_texture")) {
716            fMSFBOType = kES_IMG_MsToTexture_MSFBOType;
717        } else if (ctxInfo.version() >= GR_GL_VER(3,0)) {
718            fMSFBOType = GrGLCaps::kES_3_0_MSFBOType;
719        } else if (ctxInfo.hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
720            // chrome's extension is equivalent to the EXT msaa
721            // and fbo_blit extensions.
722            fMSFBOType = kDesktop_EXT_MSFBOType;
723        } else if (ctxInfo.hasExtension("GL_APPLE_framebuffer_multisample")) {
724            fMSFBOType = kES_Apple_MSFBOType;
725        }
726    } else {
727        if ((ctxInfo.version() >= GR_GL_VER(3,0)) ||
728            ctxInfo.hasExtension("GL_ARB_framebuffer_object")) {
729            fMSFBOType = GrGLCaps::kDesktop_ARB_MSFBOType;
730        } else if (ctxInfo.hasExtension("GL_EXT_framebuffer_multisample") &&
731                   ctxInfo.hasExtension("GL_EXT_framebuffer_blit")) {
732            fMSFBOType = GrGLCaps::kDesktop_EXT_MSFBOType;
733        }
734    }
735}
736
737namespace {
738const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
739}
740
741void GrGLCaps::initStencilFormats(const GrGLContextInfo& ctxInfo) {
742
743    // Build up list of legal stencil formats (though perhaps not supported on
744    // the particular gpu/driver) from most preferred to least.
745
746    // these consts are in order of most preferred to least preferred
747    // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
748
749    static const StencilFormat
750                  // internal Format      stencil bits      total bits        packed?
751        gS8    = {GR_GL_STENCIL_INDEX8,   8,                8,                false},
752        gS16   = {GR_GL_STENCIL_INDEX16,  16,               16,               false},
753        gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8,                32,               true },
754        gS4    = {GR_GL_STENCIL_INDEX4,   4,                4,                false},
755    //  gS     = {GR_GL_STENCIL_INDEX,    kUnknownBitCount, kUnknownBitCount, false},
756        gDS    = {GR_GL_DEPTH_STENCIL,    kUnknownBitCount, kUnknownBitCount, true };
757
758    if (kGL_GrGLStandard == ctxInfo.standard()) {
759        bool supportsPackedDS =
760            ctxInfo.version() >= GR_GL_VER(3,0) ||
761            ctxInfo.hasExtension("GL_EXT_packed_depth_stencil") ||
762            ctxInfo.hasExtension("GL_ARB_framebuffer_object");
763
764        // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
765        // require FBO support we can expect these are legal formats and don't
766        // check. These also all support the unsized GL_STENCIL_INDEX.
767        fStencilFormats.push_back() = gS8;
768        fStencilFormats.push_back() = gS16;
769        if (supportsPackedDS) {
770            fStencilFormats.push_back() = gD24S8;
771        }
772        fStencilFormats.push_back() = gS4;
773        if (supportsPackedDS) {
774            fStencilFormats.push_back() = gDS;
775        }
776    } else {
777        // ES2 has STENCIL_INDEX8 without extensions but requires extensions
778        // for other formats.
779        // ES doesn't support using the unsized format.
780
781        fStencilFormats.push_back() = gS8;
782        //fStencilFormats.push_back() = gS16;
783        if (ctxInfo.version() >= GR_GL_VER(3,0) ||
784            ctxInfo.hasExtension("GL_OES_packed_depth_stencil")) {
785            fStencilFormats.push_back() = gD24S8;
786        }
787        if (ctxInfo.hasExtension("GL_OES_stencil4")) {
788            fStencilFormats.push_back() = gS4;
789        }
790    }
791    SkASSERT(0 == fStencilVerifiedColorConfigs.count());
792    fStencilVerifiedColorConfigs.push_back_n(fStencilFormats.count());
793}
794
795void GrGLCaps::markColorConfigAndStencilFormatAsVerified(
796                                    GrPixelConfig config,
797                                    const GrGLStencilAttachment::Format& format) {
798#if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
799    return;
800#endif
801    SkASSERT((unsigned)config < (unsigned)kGrPixelConfigCnt);
802    SkASSERT(fStencilFormats.count() == fStencilVerifiedColorConfigs.count());
803    int count = fStencilFormats.count();
804    // we expect a really small number of possible formats so linear search
805    // should be OK
806    SkASSERT(count < 16);
807    for (int i = 0; i < count; ++i) {
808        if (format.fInternalFormat ==
809            fStencilFormats[i].fInternalFormat) {
810            fStencilVerifiedColorConfigs[i].markVerified(config);
811            return;
812        }
813    }
814    SkFAIL("Why are we seeing a stencil format that "
815            "GrGLCaps doesn't know about.");
816}
817
818bool GrGLCaps::isColorConfigAndStencilFormatVerified(
819                                GrPixelConfig config,
820                                const GrGLStencilAttachment::Format& format) const {
821#if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
822    return false;
823#endif
824    SkASSERT((unsigned)config < (unsigned)kGrPixelConfigCnt);
825    int count = fStencilFormats.count();
826    // we expect a really small number of possible formats so linear search
827    // should be OK
828    SkASSERT(count < 16);
829    for (int i = 0; i < count; ++i) {
830        if (format.fInternalFormat ==
831            fStencilFormats[i].fInternalFormat) {
832            return fStencilVerifiedColorConfigs[i].isVerified(config);
833        }
834    }
835    SkFAIL("Why are we seeing a stencil format that "
836            "GLCaps doesn't know about.");
837    return false;
838}
839
840SkString GrGLCaps::dump() const {
841
842    SkString r = INHERITED::dump();
843
844    r.appendf("--- GL-Specific ---\n");
845    for (int i = 0; i < fStencilFormats.count(); ++i) {
846        r.appendf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
847                 i,
848                 fStencilFormats[i].fStencilBits,
849                 fStencilFormats[i].fTotalBits);
850    }
851
852    static const char* kMSFBOExtStr[] = {
853        "None",
854        "ARB",
855        "EXT",
856        "ES 3.0",
857        "Apple",
858        "IMG MS To Texture",
859        "EXT MS To Texture",
860    };
861    GR_STATIC_ASSERT(0 == kNone_MSFBOType);
862    GR_STATIC_ASSERT(1 == kDesktop_ARB_MSFBOType);
863    GR_STATIC_ASSERT(2 == kDesktop_EXT_MSFBOType);
864    GR_STATIC_ASSERT(3 == kES_3_0_MSFBOType);
865    GR_STATIC_ASSERT(4 == kES_Apple_MSFBOType);
866    GR_STATIC_ASSERT(5 == kES_IMG_MsToTexture_MSFBOType);
867    GR_STATIC_ASSERT(6 == kES_EXT_MsToTexture_MSFBOType);
868    GR_STATIC_ASSERT(SK_ARRAY_COUNT(kMSFBOExtStr) == kLast_MSFBOType + 1);
869
870    static const char* kInvalidateFBTypeStr[] = {
871        "None",
872        "Discard",
873        "Invalidate",
874    };
875    GR_STATIC_ASSERT(0 == kNone_InvalidateFBType);
876    GR_STATIC_ASSERT(1 == kDiscard_InvalidateFBType);
877    GR_STATIC_ASSERT(2 == kInvalidate_InvalidateFBType);
878    GR_STATIC_ASSERT(SK_ARRAY_COUNT(kInvalidateFBTypeStr) == kLast_InvalidateFBType + 1);
879
880    static const char* kMapBufferTypeStr[] = {
881        "None",
882        "MapBuffer",
883        "MapBufferRange",
884        "Chromium",
885    };
886    GR_STATIC_ASSERT(0 == kNone_MapBufferType);
887    GR_STATIC_ASSERT(1 == kMapBuffer_MapBufferType);
888    GR_STATIC_ASSERT(2 == kMapBufferRange_MapBufferType);
889    GR_STATIC_ASSERT(3 == kChromium_MapBufferType);
890    GR_STATIC_ASSERT(SK_ARRAY_COUNT(kMapBufferTypeStr) == kLast_MapBufferType + 1);
891
892    r.appendf("Core Profile: %s\n", (fIsCoreProfile ? "YES" : "NO"));
893    r.appendf("MSAA Type: %s\n", kMSFBOExtStr[fMSFBOType]);
894    r.appendf("Invalidate FB Type: %s\n", kInvalidateFBTypeStr[fInvalidateFBType]);
895    r.appendf("Map Buffer Type: %s\n", kMapBufferTypeStr[fMapBufferType]);
896    r.appendf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
897    r.appendf("Max FS Texture Units: %d\n", fMaxFragmentTextureUnits);
898    r.appendf("Max Vertex Attributes: %d\n", fMaxVertexAttributes);
899    r.appendf("Support RGBA8 Render Buffer: %s\n", (fRGBA8RenderbufferSupport ? "YES": "NO"));
900    r.appendf("BGRA is an internal format: %s\n", (fBGRAIsInternalFormat ? "YES": "NO"));
901    r.appendf("Support texture swizzle: %s\n", (fTextureSwizzleSupport ? "YES": "NO"));
902    r.appendf("Unpack Row length support: %s\n", (fUnpackRowLengthSupport ? "YES": "NO"));
903    r.appendf("Unpack Flip Y support: %s\n", (fUnpackFlipYSupport ? "YES": "NO"));
904    r.appendf("Pack Row length support: %s\n", (fPackRowLengthSupport ? "YES": "NO"));
905    r.appendf("Pack Flip Y support: %s\n", (fPackFlipYSupport ? "YES": "NO"));
906
907    r.appendf("Texture Usage support: %s\n", (fTextureUsageSupport ? "YES": "NO"));
908    r.appendf("Texture Storage support: %s\n", (fTexStorageSupport ? "YES": "NO"));
909    r.appendf("GL_R support: %s\n", (fTextureRedSupport ? "YES": "NO"));
910    r.appendf("GL_ARB_imaging support: %s\n", (fImagingSupport ? "YES": "NO"));
911    r.appendf("Two Format Limit: %s\n", (fTwoFormatLimit ? "YES": "NO"));
912    r.appendf("Fragment coord conventions support: %s\n",
913             (fFragCoordsConventionSupport ? "YES": "NO"));
914    r.appendf("Vertex array object support: %s\n", (fVertexArrayObjectSupport ? "YES": "NO"));
915    r.appendf("Use non-VBO for dynamic data: %s\n",
916             (fUseNonVBOVertexAndIndexDynamicData ? "YES" : "NO"));
917    r.appendf("Full screen clear is free: %s\n", (fFullClearIsFree ? "YES" : "NO"));
918    return r;
919}
920
921////////////////////////////////////////////////////////////////////////////////////////////
922
923GrGLSLCaps::GrGLSLCaps() {
924    this->reset();
925}
926
927
928void GrGLSLCaps::reset() {
929    INHERITED::reset();
930
931    fDropsTileOnZeroDivide = false;
932    fFBFetchSupport = false;
933    fFBFetchNeedsCustomOutput = false;
934    fAdvBlendEqInteraction = kNotSupported_AdvBlendEqInteraction;
935    fFBFetchColorName = NULL;
936    fFBFetchExtensionString = NULL;
937}
938
939GrGLSLCaps::GrGLSLCaps(const GrGLSLCaps& caps) : GrShaderCaps() {
940    *this = caps;
941}
942
943GrGLSLCaps& GrGLSLCaps::operator= (const GrGLSLCaps& caps) {
944    INHERITED::operator=(caps);
945    fDropsTileOnZeroDivide = caps.fDropsTileOnZeroDivide;
946    fFBFetchSupport = caps.fFBFetchSupport;
947    fFBFetchNeedsCustomOutput = caps.fFBFetchNeedsCustomOutput;
948    fAdvBlendEqInteraction = caps.fAdvBlendEqInteraction;
949    fFBFetchColorName = caps.fFBFetchColorName;
950    fFBFetchExtensionString = caps.fFBFetchExtensionString;
951
952    return *this;
953}
954
955bool GrGLSLCaps::init(const GrGLContextInfo& ctxInfo,
956                      const GrGLInterface* gli,
957                      const GrGLCaps& glCaps) {
958    this->reset();
959    if (!ctxInfo.isInitialized()) {
960        return false;
961    }
962
963    GrGLStandard standard = ctxInfo.standard();
964    GrGLVersion version = ctxInfo.version();
965
966    /**************************************************************************
967    * Caps specific to GrGLSLCaps
968    **************************************************************************/
969
970    if (kGLES_GrGLStandard == standard) {
971        if (ctxInfo.hasExtension("GL_EXT_shader_framebuffer_fetch")) {
972            fFBFetchNeedsCustomOutput = (version >= GR_GL_VER(3, 0));
973            fFBFetchSupport = true;
974            fFBFetchColorName = "gl_LastFragData[0]";
975            fFBFetchExtensionString = "GL_EXT_shader_framebuffer_fetch";
976        }
977        else if (ctxInfo.hasExtension("GL_NV_shader_framebuffer_fetch")) {
978            // Actually, we haven't seen an ES3.0 device with this extension yet, so we don't know
979            fFBFetchNeedsCustomOutput = false;
980            fFBFetchSupport = true;
981            fFBFetchColorName = "gl_LastFragData[0]";
982            fFBFetchExtensionString = "GL_NV_shader_framebuffer_fetch";
983        }
984        else if (ctxInfo.hasExtension("GL_ARM_shader_framebuffer_fetch")) {
985            // The arm extension also requires an additional flag which we will set onResetContext
986            fFBFetchNeedsCustomOutput = false;
987            fFBFetchSupport = true;
988            fFBFetchColorName = "gl_LastFragColorARM";
989            fFBFetchExtensionString = "GL_ARM_shader_framebuffer_fetch";
990        }
991    }
992
993    // Adreno GPUs have a tendency to drop tiles when there is a divide-by-zero in a shader
994    fDropsTileOnZeroDivide = kQualcomm_GrGLVendor == ctxInfo.vendor();
995
996    /**************************************************************************
997    * GrShaderCaps fields
998    **************************************************************************/
999
1000    fPathRenderingSupport = ctxInfo.hasExtension("GL_NV_path_rendering");
1001
1002    if (fPathRenderingSupport) {
1003        if (kGL_GrGLStandard == standard) {
1004            // We only support v1.3+ of GL_NV_path_rendering which allows us to
1005            // set individual fragment inputs with ProgramPathFragmentInputGen. The API
1006            // additions are detected by checking the existence of the function.
1007            fPathRenderingSupport = ctxInfo.hasExtension("GL_EXT_direct_state_access") &&
1008                ((ctxInfo.version() >= GR_GL_VER(4, 3) ||
1009                ctxInfo.hasExtension("GL_ARB_program_interface_query")) &&
1010                gli->fFunctions.fProgramPathFragmentInputGen);
1011        }
1012        else {
1013            fPathRenderingSupport = ctxInfo.version() >= GR_GL_VER(3, 1);
1014        }
1015    }
1016
1017    // For now these two are equivalent but we could have dst read in shader via some other method
1018    fDstReadInShaderSupport = fFBFetchSupport;
1019
1020    // Enable supported shader-related caps
1021    if (kGL_GrGLStandard == standard) {
1022        fDualSourceBlendingSupport = ctxInfo.version() >= GR_GL_VER(3, 3) ||
1023            ctxInfo.hasExtension("GL_ARB_blend_func_extended");
1024        fShaderDerivativeSupport = true;
1025        // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
1026        fGeometryShaderSupport = ctxInfo.version() >= GR_GL_VER(3, 2) &&
1027            ctxInfo.glslGeneration() >= k150_GrGLSLGeneration;
1028    }
1029    else {
1030        fShaderDerivativeSupport = ctxInfo.version() >= GR_GL_VER(3, 0) ||
1031            ctxInfo.hasExtension("GL_OES_standard_derivatives");
1032    }
1033
1034    if (glCaps.advancedBlendEquationSupport()) {
1035        bool coherent = glCaps.advancedCoherentBlendEquationSupport();
1036        if (ctxInfo.hasExtension(coherent ? "GL_NV_blend_equation_advanced_coherent"
1037                                          : "GL_NV_blend_equation_advanced")) {
1038            fAdvBlendEqInteraction = kAutomatic_AdvBlendEqInteraction;
1039        } else {
1040            fAdvBlendEqInteraction = kGeneralEnable_AdvBlendEqInteraction;
1041            // TODO: Use the following on any platform where "blend_support_all_equations" is slow.
1042            //fAdvBlendEqInteraction = kSpecificEnables_AdvBlendEqInteraction;
1043        }
1044    }
1045
1046    this->initShaderPrecisionTable(ctxInfo, gli);
1047
1048    return true;
1049}
1050
1051SkString GrGLSLCaps::dump() const {
1052    SkString r = INHERITED::dump();
1053
1054    static const char* kAdvBlendEqInteractionStr[] = {
1055        "Not Supported",
1056        "Automatic",
1057        "General Enable",
1058        "Specific Enables",
1059    };
1060    GR_STATIC_ASSERT(0 == kNotSupported_AdvBlendEqInteraction);
1061    GR_STATIC_ASSERT(1 == kAutomatic_AdvBlendEqInteraction);
1062    GR_STATIC_ASSERT(2 == kGeneralEnable_AdvBlendEqInteraction);
1063    GR_STATIC_ASSERT(3 == kSpecificEnables_AdvBlendEqInteraction);
1064    GR_STATIC_ASSERT(SK_ARRAY_COUNT(kAdvBlendEqInteractionStr) == kLast_AdvBlendEqInteraction + 1);
1065
1066    r.appendf("--- GLSL-Specific ---\n");
1067
1068    r.appendf("FB Fetch Support: %s\n", (fFBFetchSupport ? "YES" : "NO"));
1069    r.appendf("Drops tile on zero divide: %s\n", (fDropsTileOnZeroDivide ? "YES" : "NO"));
1070    r.appendf("Advanced blend equation interaction: %s\n",
1071              kAdvBlendEqInteractionStr[fAdvBlendEqInteraction]);
1072    return r;
1073}
1074
1075static GrGLenum precision_to_gl_float_type(GrSLPrecision p) {
1076    switch (p) {
1077    case kLow_GrSLPrecision:
1078        return GR_GL_LOW_FLOAT;
1079    case kMedium_GrSLPrecision:
1080        return GR_GL_MEDIUM_FLOAT;
1081    case kHigh_GrSLPrecision:
1082        return GR_GL_HIGH_FLOAT;
1083    }
1084    SkFAIL("Unknown precision.");
1085    return -1;
1086}
1087
1088static GrGLenum shader_type_to_gl_shader(GrShaderType type) {
1089    switch (type) {
1090    case kVertex_GrShaderType:
1091        return GR_GL_VERTEX_SHADER;
1092    case kGeometry_GrShaderType:
1093        return GR_GL_GEOMETRY_SHADER;
1094    case kFragment_GrShaderType:
1095        return GR_GL_FRAGMENT_SHADER;
1096    }
1097    SkFAIL("Unknown shader type.");
1098    return -1;
1099}
1100
1101void GrGLSLCaps::initShaderPrecisionTable(const GrGLContextInfo& ctxInfo,
1102                                          const GrGLInterface* intf) {
1103    if (kGLES_GrGLStandard == ctxInfo.standard() || ctxInfo.version() >= GR_GL_VER(4, 1) ||
1104        ctxInfo.hasExtension("GL_ARB_ES2_compatibility")) {
1105        for (int s = 0; s < kGrShaderTypeCount; ++s) {
1106            if (kGeometry_GrShaderType != s) {
1107                GrShaderType shaderType = static_cast<GrShaderType>(s);
1108                GrGLenum glShader = shader_type_to_gl_shader(shaderType);
1109                PrecisionInfo* first = NULL;
1110                fShaderPrecisionVaries = false;
1111                for (int p = 0; p < kGrSLPrecisionCount; ++p) {
1112                    GrSLPrecision precision = static_cast<GrSLPrecision>(p);
1113                    GrGLenum glPrecision = precision_to_gl_float_type(precision);
1114                    GrGLint range[2];
1115                    GrGLint bits;
1116                    GR_GL_GetShaderPrecisionFormat(intf, glShader, glPrecision, range, &bits);
1117                    if (bits) {
1118                        fFloatPrecisions[s][p].fLogRangeLow = range[0];
1119                        fFloatPrecisions[s][p].fLogRangeHigh = range[1];
1120                        fFloatPrecisions[s][p].fBits = bits;
1121                        if (!first) {
1122                            first = &fFloatPrecisions[s][p];
1123                        }
1124                        else if (!fShaderPrecisionVaries) {
1125                            fShaderPrecisionVaries = (*first != fFloatPrecisions[s][p]);
1126                        }
1127                    }
1128                }
1129            }
1130        }
1131    }
1132    else {
1133        // We're on a desktop GL that doesn't have precision info. Assume they're all 32bit float.
1134        fShaderPrecisionVaries = false;
1135        for (int s = 0; s < kGrShaderTypeCount; ++s) {
1136            if (kGeometry_GrShaderType != s) {
1137                for (int p = 0; p < kGrSLPrecisionCount; ++p) {
1138                    fFloatPrecisions[s][p].fLogRangeLow = 127;
1139                    fFloatPrecisions[s][p].fLogRangeHigh = 127;
1140                    fFloatPrecisions[s][p].fBits = 23;
1141                }
1142            }
1143        }
1144    }
1145    // GetShaderPrecisionFormat doesn't accept GL_GEOMETRY_SHADER as a shader type. Assume they're
1146    // the same as the vertex shader. Only fragment shaders were ever allowed to omit support for
1147    // highp. GS was added after GetShaderPrecisionFormat was added to the list of features that
1148    // are recommended against.
1149    if (fGeometryShaderSupport) {
1150        for (int p = 0; p < kGrSLPrecisionCount; ++p) {
1151            fFloatPrecisions[kGeometry_GrShaderType][p] = fFloatPrecisions[kVertex_GrShaderType][p];
1152        }
1153    }
1154}
1155
1156
1157
1158
1159