• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 
9 #ifndef GrGLCaps_DEFINED
10 #define GrGLCaps_DEFINED
11 
12 #include <functional>
13 #include "include/private/GrGLTypesPriv.h"
14 #include "include/private/SkChecksum.h"
15 #include "include/private/SkTArray.h"
16 #include "include/private/SkTHash.h"
17 #include "src/gpu/GrCaps.h"
18 #include "src/gpu/GrSwizzle.h"
19 #include "src/gpu/gl/GrGLAttachment.h"
20 #include "src/gpu/gl/GrGLUtil.h"
21 
22 class GrGLContextInfo;
23 class GrGLRenderTarget;
24 
25 /**
26  * Stores some capabilities of a GL context. Most are determined by the GL
27  * version and the extensions string. It also tracks formats that have passed
28  * the FBO completeness test.
29  */
30 class GrGLCaps : public GrCaps {
31 public:
32     /**
33      * The type of MSAA for FBOs supported. Different extensions have different
34      * semantics of how / when a resolve is performed.
35      */
36     enum MSFBOType {
37         /**
38          * no support for MSAA FBOs
39          */
40         kNone_MSFBOType = 0,
41         /**
42          * OpenGL 3.0+, OpenGL ES 3.0+, GL_ARB_framebuffer_object,
43          * GL_CHROMIUM_framebuffer_multisample, GL_ANGLE_framebuffer_multisample,
44          * or GL_EXT_framebuffer_multisample
45          */
46         kStandard_MSFBOType,
47         /**
48          * GL_APPLE_framebuffer_multisample ES extension
49          */
50         kES_Apple_MSFBOType,
51         /**
52          * GL_IMG_multisampled_render_to_texture. This variation does not have MSAA renderbuffers.
53          * Instead the texture is multisampled when bound to the FBO and then resolved automatically
54          * when read. It also defines an alternate value for GL_MAX_SAMPLES (which we call
55          * GR_GL_MAX_SAMPLES_IMG).
56          */
57         kES_IMG_MsToTexture_MSFBOType,
58         /**
59          * GL_EXT_multisampled_render_to_texture. Same as the IMG one above but uses the standard
60          * GL_MAX_SAMPLES value.
61          */
62         kES_EXT_MsToTexture_MSFBOType,
63 
64         kLast_MSFBOType = kES_EXT_MsToTexture_MSFBOType
65     };
66 
67     enum BlitFramebufferFlags {
68         kNoSupport_BlitFramebufferFlag                    = 1 << 0,
69         kNoScalingOrMirroring_BlitFramebufferFlag         = 1 << 1,
70         kResolveMustBeFull_BlitFrambufferFlag             = 1 << 2,
71         kNoMSAADst_BlitFramebufferFlag                    = 1 << 3,
72         kNoFormatConversion_BlitFramebufferFlag           = 1 << 4,
73         kNoFormatConversionForMSAASrc_BlitFramebufferFlag = 1 << 5,
74         kRectsMustMatchForMSAASrc_BlitFramebufferFlag     = 1 << 6,
75     };
76 
77     enum InvalidateFBType {
78         kNone_InvalidateFBType,
79         kDiscard_InvalidateFBType,       //<! glDiscardFramebuffer()
80         kInvalidate_InvalidateFBType,    //<! glInvalidateFramebuffer()
81 
82         kLast_InvalidateFBType = kInvalidate_InvalidateFBType
83     };
84 
85     enum MapBufferType {
86         kNone_MapBufferType,
87         kMapBuffer_MapBufferType,         // glMapBuffer()
88         kMapBufferRange_MapBufferType,    // glMapBufferRange()
89         kChromium_MapBufferType,          // GL_CHROMIUM_map_sub
90 
91         kLast_MapBufferType = kChromium_MapBufferType,
92     };
93 
94     enum class TransferBufferType {
95         kNone,
96         kNV_PBO,    // NV__pixel_buffer_object
97         kARB_PBO,   // ARB_pixel_buffer_object
98         kChromium,  // CHROMIUM_pixel_transfer_buffer_object
99     };
100 
101     enum class FenceType {
102         kNone,
103         kSyncObject,
104         kNVFence
105     };
106 
107     enum class MultiDrawType {
108         kNone,
109         kMultiDrawIndirect,  // ARB_multi_draw_indirect, EXT_multi_draw_indirect, or GL 4.3 core.
110         kANGLEOrWebGL  // ANGLE_base_vertex_base_instance or
111                        // WEBGL_draw_instanced_base_vertex_base_instance
112     };
113 
114     /**
115      * Initializes the GrGLCaps to the set of features supported in the current
116      * OpenGL context accessible via ctxInfo.
117      */
118     GrGLCaps(const GrContextOptions& contextOptions, const GrGLContextInfo& ctxInfo,
119              const GrGLInterface* glInterface);
120 
121     bool isFormatSRGB(const GrBackendFormat&) const override;
122 
123     bool isFormatTexturable(const GrBackendFormat&) const override;
124     bool isFormatTexturable(GrGLFormat) const;
125 
126     bool isFormatAsColorTypeRenderable(GrColorType ct, const GrBackendFormat& format,
127                                        int sampleCount = 1) const override;
128     bool isFormatRenderable(const GrBackendFormat& format, int sampleCount) const override;
isFormatRenderable(GrGLFormat format,int sampleCount)129     bool isFormatRenderable(GrGLFormat format, int sampleCount) const {
130         return sampleCount <= this->maxRenderTargetSampleCount(format);
131     }
132 
getRenderTargetSampleCount(int requestedCount,const GrBackendFormat & format)133     int getRenderTargetSampleCount(int requestedCount,
134                                    const GrBackendFormat& format) const override {
135         return this->getRenderTargetSampleCount(requestedCount, format.asGLFormat());
136     }
137     int getRenderTargetSampleCount(int requestedCount, GrGLFormat) const;
138 
maxRenderTargetSampleCount(const GrBackendFormat & format)139     int maxRenderTargetSampleCount(const GrBackendFormat& format) const override {
140         return this->maxRenderTargetSampleCount(format.asGLFormat());
141     }
142     int maxRenderTargetSampleCount(GrGLFormat) const;
143 
144     bool isFormatCopyable(const GrBackendFormat&) const override;
145 
146     bool canFormatBeFBOColorAttachment(GrGLFormat) const;
147 
getFormatFromColorType(GrColorType colorType)148     GrGLFormat getFormatFromColorType(GrColorType colorType) const {
149         int idx = static_cast<int>(colorType);
150         return fColorTypeToFormatTable[idx];
151     }
152 
153     /**
154      * Gets the internal format to use with glTexImage...() and glTexStorage...(). May be sized or
155      * base depending upon the GL. Not applicable to compressed textures.
156      */
getTexImageOrStorageInternalFormat(GrGLFormat format)157     GrGLenum getTexImageOrStorageInternalFormat(GrGLFormat format) const {
158         return this->getFormatInfo(format).fInternalFormatForTexImageOrStorage;
159     }
160 
161     /**
162      * Gets the external format and type to pass to glTexImage2D with nullptr to create an
163      * uninitialized texture. See getTexImageOrStorageInternalFormat() for the internal format.
164      */
165     void getTexImageExternalFormatAndType(GrGLFormat surfaceFormat, GrGLenum* externalFormat,
166                                           GrGLenum* externalType) const;
167 
168     /**
169      * Given a src data color type and a color type interpretation for a texture of a given format
170      * this provides the external GL format and type to use with glTexSubImage2d. The color types
171      * should originate from supportedWritePixelsColorType().
172      */
173     void getTexSubImageExternalFormatAndType(GrGLFormat surfaceFormat, GrColorType surfaceColorType,
174                                              GrColorType memoryColorType, GrGLenum* externalFormat,
175                                              GrGLenum* externalType) const;
176 
177     /**
178      * Gets the external format, type, and bytes per pixel to use when uploading solid color data
179      * via glTexSubImage...() to clear the texture at creation.
180      */
181     void getTexSubImageDefaultFormatTypeAndColorType(GrGLFormat format,
182                                                      GrGLenum* externalFormat,
183                                                      GrGLenum* externalType,
184                                                      GrColorType* colorType) const;
185 
186     void getReadPixelsFormat(GrGLFormat surfaceFormat, GrColorType surfaceColorType,
187                              GrColorType memoryColorType, GrGLenum* externalFormat,
188                              GrGLenum* externalType) const;
189 
190     /**
191     * Gets an array of legal stencil formats. These formats are not guaranteed
192     * to be supported by the driver but are legal GLenum names given the GL
193     * version and extensions supported.
194     */
stencilFormats()195     const SkTArray<GrGLFormat, true>& stencilFormats() const {
196         return fStencilFormats;
197     }
198 
199     bool formatSupportsTexStorage(GrGLFormat) const;
200 
201     /**
202      * Would it be useful to check GL_IMPLEMENTATION_READ_FORMAT and _TYPE for this format to
203      * detect more efficient glReadPixels arguments?
204      */
205     bool shouldQueryImplementationReadSupport(GrGLFormat format) const;
206 
207     /**
208      * Let caps know the result of GL_IMPLEMENTATION_READ_FORMAT and _TYPE query for a format
209      * to update supported glReadPixels arguments.
210      */
211     void didQueryImplementationReadSupport(GrGLFormat format,
212                                            GrGLenum readFormat,
213                                            GrGLenum readType) const;
214 
215     /**
216      * Gets the internal format to use with glRenderbufferStorageMultisample...(). May be sized or
217      * base depending upon the GL. Not applicable to compressed textures.
218      */
getRenderbufferInternalFormat(GrGLFormat format)219     GrGLenum getRenderbufferInternalFormat(GrGLFormat format) const {
220         return this->getFormatInfo(format).fInternalFormatForRenderbuffer;
221     }
222 
223     /**
224      * Gets the default external type to use with glTex[Sub]Image... when the data pointer is null.
225      */
getFormatDefaultExternalType(GrGLFormat format)226     GrGLenum getFormatDefaultExternalType(GrGLFormat format) const {
227         return this->getFormatInfo(format).fDefaultExternalType;
228     }
229 
230     /**
231      * Has a stencil format index been found for the format (or we've found that no format works).
232      */
hasStencilFormatBeenDeterminedForFormat(GrGLFormat format)233     bool hasStencilFormatBeenDeterminedForFormat(GrGLFormat format) const {
234         return this->getFormatInfo(format).fStencilFormatIndex != FormatInfo::kUnknown_StencilIndex;
235     }
236 
237     /**
238      * Gets the stencil format index for the format. This assumes
239      * hasStencilFormatBeenDeterminedForFormat has already been checked. Returns a value < 0 if
240      * no stencil format is supported with the format. Otherwise, returned index refers to the array
241      * returned by stencilFormats().
242      */
getStencilFormatIndexForFormat(GrGLFormat format)243     int getStencilFormatIndexForFormat(GrGLFormat format) const {
244         SkASSERT(this->hasStencilFormatBeenDeterminedForFormat(format));
245         return this->getFormatInfo(format).fStencilFormatIndex;
246     }
247 
248     /**
249      * If index is >= 0 this records an index into stencilFormats() as the best stencil format for
250      * the format. If < 0 it records that the format has no supported stencil format index.
251      */
252     void setStencilFormatIndexForFormat(GrGLFormat, int index);
253 
254     /**
255      * Reports the type of MSAA FBO support.
256      */
msFBOType()257     MSFBOType msFBOType() const { return fMSFBOType; }
258 
259     /**
260      * Does the preferred MSAA FBO extension have MSAA renderbuffers?
261      */
usesMSAARenderBuffers()262     bool usesMSAARenderBuffers() const {
263         return kNone_MSFBOType != fMSFBOType &&
264                kES_IMG_MsToTexture_MSFBOType != fMSFBOType &&
265                kES_EXT_MsToTexture_MSFBOType != fMSFBOType;
266     }
267 
268     /**
269      * What functionality is supported by glBlitFramebuffer.
270      */
blitFramebufferSupportFlags()271     uint32_t blitFramebufferSupportFlags() const { return fBlitFramebufferFlags; }
272 
273     /**
274      * Is the MSAA FBO extension one where the texture is multisampled when bound to an FBO and
275      * then implicitly resolved when read.
276      */
usesImplicitMSAAResolve()277     bool usesImplicitMSAAResolve() const {
278         return kES_IMG_MsToTexture_MSFBOType == fMSFBOType ||
279                kES_EXT_MsToTexture_MSFBOType == fMSFBOType;
280     }
281 
invalidateFBType()282     InvalidateFBType invalidateFBType() const { return fInvalidateFBType; }
283 
284     /// What type of buffer mapping is supported?
mapBufferType()285     MapBufferType mapBufferType() const { return fMapBufferType; }
286 
287     /// What type of transfer buffer is supported?
transferBufferType()288     TransferBufferType transferBufferType() const { return fTransferBufferType; }
289 
290     /// How are GrFences implemented?
fenceType()291     FenceType fenceType() const { return fFenceType; }
292 
293     /// How are multi draws implemented (if at all)?
multiDrawType()294     MultiDrawType multiDrawType() const { return fMultiDrawType; }
295 
296     /// The maximum number of fragment uniform vectors (GLES has min. 16).
maxFragmentUniformVectors()297     int maxFragmentUniformVectors() const { return fMaxFragmentUniformVectors; }
298 
299     /// Is there support for GL_PACK_REVERSE_ROW_ORDER
packFlipYSupport()300     bool packFlipYSupport() const { return fPackFlipYSupport; }
301 
302     /// Is there support for texture parameter GL_TEXTURE_USAGE
textureUsageSupport()303     bool textureUsageSupport() const { return fTextureUsageSupport; }
304 
305     /// Is GL_ARB_IMAGING supported
imagingSupport()306     bool imagingSupport() const { return fImagingSupport; }
307 
308     /// Is there support for Vertex Array Objects?
vertexArrayObjectSupport()309     bool vertexArrayObjectSupport() const { return fVertexArrayObjectSupport; }
310 
311     /// Is there support for GL_KHR_debug?
debugSupport()312     bool debugSupport() const { return fDebugSupport; }
313 
314     /// Is there support for ES2 compatability?
ES2CompatibilitySupport()315     bool ES2CompatibilitySupport() const { return fES2CompatibilitySupport; }
316 
317     /// Is there support for glDrawRangeElements?
drawRangeElementsSupport()318     bool drawRangeElementsSupport() const { return fDrawRangeElementsSupport; }
319 
320     /// Are the glDraw*Base(VertexBase)Instance methods, and baseInstance fields in indirect draw
321     //commands supported?
baseVertexBaseInstanceSupport()322     bool baseVertexBaseInstanceSupport() const { return fBaseVertexBaseInstanceSupport; }
323 
324     SurfaceReadPixelsSupport surfaceSupportsReadPixels(const GrSurface*) const override;
325 
326     SupportedWrite supportedWritePixelsColorType(GrColorType surfaceColorType,
327                                                  const GrBackendFormat& surfaceFormat,
328                                                  GrColorType srcColorType) const override;
329 
isCoreProfile()330     bool isCoreProfile() const { return fIsCoreProfile; }
331 
bindFragDataLocationSupport()332     bool bindFragDataLocationSupport() const { return fBindFragDataLocationSupport; }
333 
bindUniformLocationSupport()334     bool bindUniformLocationSupport() const { return fBindUniformLocationSupport; }
335 
336     /// Are textures with GL_TEXTURE_RECTANGLE type supported.
rectangleTextureSupport()337     bool rectangleTextureSupport() const { return fRectangleTextureSupport; }
338 
339     /// Can set the BASE and MAX mip map level.
mipmapLevelControlSupport()340     bool mipmapLevelControlSupport() const { return fMipmapLevelControlSupport; }
341 
342     /// Can set the MIN/MAX LOD value.
mipmapLodControlSupport()343     bool mipmapLodControlSupport() const { return fMipmapLodControlSupport; }
344 
doManualMipmapping()345     bool doManualMipmapping() const { return fDoManualMipmapping; }
346 
347     void onDumpJSON(SkJSONWriter*) const override;
348 
useBufferDataNullHint()349     bool useBufferDataNullHint() const { return fUseBufferDataNullHint; }
350 
351     // Certain Intel GPUs on Mac fail to clear if the glClearColor is made up of only 1s and 0s.
clearToBoundaryValuesIsBroken()352     bool clearToBoundaryValuesIsBroken() const { return fClearToBoundaryValuesIsBroken; }
353 
354     /// glClearTex(Sub)Image support
clearTextureSupport()355     bool clearTextureSupport() const { return fClearTextureSupport; }
356 
357     // Adreno/MSAA drops a draw on the imagefiltersbase GM if the base vertex param to
358     // glDrawArrays is nonzero.
359     // https://bugs.chromium.org/p/skia/issues/detail?id=6650
drawArraysBaseVertexIsBroken()360     bool drawArraysBaseVertexIsBroken() const { return fDrawArraysBaseVertexIsBroken; }
361 
362     // If true then we must use an intermediate surface to perform partial updates to unorm textures
363     // that have ever been bound to a FBO.
disallowTexSubImageForUnormConfigTexturesEverBoundToFBO()364     bool disallowTexSubImageForUnormConfigTexturesEverBoundToFBO() const {
365         return fDisallowTexSubImageForUnormConfigTexturesEverBoundToFBO;
366     }
367 
368     // Use an intermediate surface to write pixels (full or partial overwrite) to into a texture
369     // that is bound to an FBO.
useDrawInsteadOfAllRenderTargetWrites()370     bool useDrawInsteadOfAllRenderTargetWrites() const {
371         return fUseDrawInsteadOfAllRenderTargetWrites;
372     }
373 
374     // At least some Adreno 3xx drivers draw lines incorrectly after drawing non-lines. Toggling
375     // face culling on and off seems to resolve this.
requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines()376     bool requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() const {
377         return fRequiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines;
378     }
379 
380     // Older Android versions seem to have an issue with setting GL_TEXTURE_BASE_LEVEL or
381     // GL_TEXTURE_MAX_LEVEL for GL_TEXTURE_EXTERNAL_OES textures.
dontSetBaseOrMaxLevelForExternalTextures()382     bool dontSetBaseOrMaxLevelForExternalTextures() const {
383         return fDontSetBaseOrMaxLevelForExternalTextures;
384     }
385 
386     // PowerVRGX6250 drops every pixel if we modify the sample mask while color writes are disabled.
neverDisableColorWrites()387     bool neverDisableColorWrites() const { return fNeverDisableColorWrites; }
388 
389     // Texture parameters must be used to enable MIP mapping even when a sampler object is used.
mustSetAnyTexParameterToEnableMipmapping()390     bool mustSetAnyTexParameterToEnableMipmapping() const {
391         return fMustSetAnyTexParameterToEnableMipmapping;
392     }
393 
394     // Returns the observed maximum number of instances the driver can handle in a single draw call
395     // without crashing, or 'pendingInstanceCount' if this workaround is not necessary.
396     // NOTE: the return value may be larger than pendingInstanceCount.
maxInstancesPerDrawWithoutCrashing(int pendingInstanceCount)397     int maxInstancesPerDrawWithoutCrashing(int pendingInstanceCount) const {
398         return (fMaxInstancesPerDrawWithoutCrashing)
399                 ? fMaxInstancesPerDrawWithoutCrashing : pendingInstanceCount;
400     }
401 
402     bool canCopyTexSubImage(GrGLFormat dstFormat, bool dstHasMSAARenderBuffer,
403                             const GrTextureType* dstTypeIfTexture,
404                             GrGLFormat srcFormat, bool srcHasMSAARenderBuffer,
405                             const GrTextureType* srcTypeIfTexture) const;
406     bool canCopyAsBlit(GrGLFormat dstFormat, int dstSampleCnt,
407                        const GrTextureType* dstTypeIfTexture,
408                        GrGLFormat srcFormat, int srcSampleCnt,
409                        const GrTextureType* srcTypeIfTexture,
410                        const SkRect& srcBounds, bool srcBoundsExact,
411                        const SkIRect& srcRect, const SkIPoint& dstPoint) const;
412     bool canCopyAsDraw(GrGLFormat dstFormat, bool srcIsTexturable) const;
413 
414     DstCopyRestrictions getDstCopyRestrictions(const GrRenderTargetProxy* src,
415                                                GrColorType) const override;
416 
programBinarySupport()417     bool programBinarySupport() const { return fProgramBinarySupport; }
programParameterSupport()418     bool programParameterSupport() const { return fProgramParameterSupport; }
419 
420     /** Are sampler objects available in this GL? */
samplerObjectSupport()421     bool samplerObjectSupport() const { return fSamplerObjectSupport; }
422 
423     /**
424      * Are we using sampler objects in favor of texture parameters? (This will only be true if
425      * samplerObjectSupport()).
426      */
useSamplerObjects()427     bool useSamplerObjects() const { return fUseSamplerObjects; }
428 
textureSwizzleSupport()429     bool textureSwizzleSupport() const { return fTextureSwizzleSupport; }
430 
tiledRenderingSupport()431     bool tiledRenderingSupport() const { return fTiledRenderingSupport; }
432 
fbFetchRequiresEnablePerSample()433     bool fbFetchRequiresEnablePerSample() const { return fFBFetchRequiresEnablePerSample; }
434 
435     /* Is there support for enabling/disabling sRGB writes for sRGB-capable color buffers? */
srgbWriteControl()436     bool srgbWriteControl() const { return fSRGBWriteControl; }
437 
438     /** Skip checks for GL errors, shader compilation success, program link success. */
skipErrorChecks()439     bool skipErrorChecks() const { return fSkipErrorChecks; }
440 
441     GrBackendFormat getBackendFormatFromCompressionType(SkImage::CompressionType) const override;
442 
443     GrSwizzle getWriteSwizzle(const GrBackendFormat&, GrColorType) const override;
444 
445     uint64_t computeFormatKey(const GrBackendFormat&) const override;
446 
447     GrProgramDesc makeDesc(GrRenderTarget*,
448                            const GrProgramInfo&,
449                            ProgramDescOverrideFlags) const override;
450 
451 #if GR_TEST_UTILS
standard()452     GrGLStandard standard() const { return fStandard; }
453 
454     std::vector<TestFormatColorTypeCombination> getTestingCombinations() const override;
455 #endif
456 
457 private:
458     enum ExternalFormatUsage {
459         kTexImage_ExternalFormatUsage,
460         kReadPixels_ExternalFormatUsage,
461     };
462     void getExternalFormat(GrGLFormat surfaceFormat, GrColorType surfaceColorType,
463                            GrColorType memoryColorType, ExternalFormatUsage usage,
464                            GrGLenum* externalFormat, GrGLenum* externalType) const;
465 
466     void init(const GrContextOptions&, const GrGLContextInfo&, const GrGLInterface*);
467     void initGLSL(const GrGLContextInfo&, const GrGLInterface*);
468 
469     struct FormatWorkarounds {
470         bool fDisableSRGBRenderWithMSAAForMacAMD = false;
471         bool fDisableRGBA16FTexStorageForCrBug1008003 = false;
472         bool fDisableBGRATextureStorageForIntelWindowsES = false;
473         bool fDisableLuminance16F = false;
474         bool fDontDisableTexStorageOnAndroid = false;
475         bool fDisallowDirectRG8ReadPixels = false;
476         bool fDisallowBGRA8ReadPixels = false;
477         bool fDisallowR8ForPowerVRSGX54x = false;
478     };
479 
480     void applyDriverCorrectnessWorkarounds(const GrGLContextInfo&, const GrContextOptions&,
481                                            const GrGLInterface*,
482                                            GrShaderCaps*, FormatWorkarounds*);
483 
484     void onApplyOptionsOverrides(const GrContextOptions& options) override;
485 
486     bool onIsWindowRectanglesSupportedForRT(const GrBackendRenderTarget&) const override;
487 
488     void initFSAASupport(const GrContextOptions& contextOptions, const GrGLContextInfo&,
489                          const GrGLInterface*);
490     void initBlendEqationSupport(const GrGLContextInfo&);
491     void initStencilSupport(const GrGLContextInfo&);
492     // This must be called after initFSAASupport().
493     void initFormatTable(const GrGLContextInfo&, const GrGLInterface*, const FormatWorkarounds&);
494     void setupSampleCounts(const GrGLContextInfo&, const GrGLInterface*);
495     bool onSurfaceSupportsWritePixels(const GrSurface*) const override;
496     bool onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
497                           const SkIRect& srcRect, const SkIPoint& dstPoint) const override;
498     GrBackendFormat onGetDefaultBackendFormat(GrColorType) const override;
499     bool onAreColorTypeAndFormatCompatible(GrColorType, const GrBackendFormat&) const override;
500 
501     SupportedRead onSupportedReadPixelsColorType(GrColorType, const GrBackendFormat&,
502                                                  GrColorType) const override;
503 
504     GrSwizzle onGetReadSwizzle(const GrBackendFormat&, GrColorType) const override;
505 
506     GrDstSampleType onGetDstSampleTypeForProxy(const GrRenderTargetProxy*) const override;
507 
onSupportsDynamicMSAA(const GrRenderTargetProxy *)508     bool onSupportsDynamicMSAA(const GrRenderTargetProxy*) const override {
509         switch (fMSFBOType) {
510             // The Apple extension doesn't support blitting from single to multisample.
511             case kES_Apple_MSFBOType:
512             case kNone_MSFBOType:
513                 return false;
514             case kStandard_MSFBOType:
515             case kES_IMG_MsToTexture_MSFBOType:
516             case kES_EXT_MsToTexture_MSFBOType:
517                 return true;
518         }
519         SkUNREACHABLE;
520     }
521 
522     GrGLStandard fStandard = kNone_GrGLStandard;
523 
524     SkTArray<GrGLFormat, true> fStencilFormats;
525 
526     int fMaxFragmentUniformVectors = 0;
527 
528     MSFBOType           fMSFBOType          = kNone_MSFBOType;
529     InvalidateFBType    fInvalidateFBType   = kNone_InvalidateFBType;
530     MapBufferType       fMapBufferType      = kNone_MapBufferType;
531     TransferBufferType  fTransferBufferType = TransferBufferType::kNone;
532     FenceType           fFenceType          = FenceType::kNone;
533     MultiDrawType       fMultiDrawType      = MultiDrawType::kNone;
534 
535     bool fPackFlipYSupport : 1;
536     bool fTextureUsageSupport : 1;
537     bool fImagingSupport  : 1;
538     bool fVertexArrayObjectSupport : 1;
539     bool fDebugSupport : 1;
540     bool fES2CompatibilitySupport : 1;
541     bool fDrawRangeElementsSupport : 1;
542     bool fBaseVertexBaseInstanceSupport : 1;
543     bool fIsCoreProfile : 1;
544     bool fBindFragDataLocationSupport : 1;
545     bool fBindUniformLocationSupport : 1;
546     bool fRectangleTextureSupport : 1;
547     bool fMipmapLevelControlSupport : 1;
548     bool fMipmapLodControlSupport : 1;
549     bool fUseBufferDataNullHint : 1;
550     bool fClearTextureSupport : 1;
551     bool fProgramBinarySupport : 1;
552     bool fProgramParameterSupport : 1;
553     bool fSamplerObjectSupport : 1;
554     bool fUseSamplerObjects : 1;
555     bool fTextureSwizzleSupport : 1;
556     bool fTiledRenderingSupport : 1;
557     bool fFBFetchRequiresEnablePerSample : 1;
558     bool fSRGBWriteControl : 1;
559     bool fSkipErrorChecks : 1;
560 
561     // Driver workarounds
562     bool fDoManualMipmapping : 1;
563     bool fClearToBoundaryValuesIsBroken : 1;
564     bool fDrawArraysBaseVertexIsBroken : 1;
565     bool fDisallowTexSubImageForUnormConfigTexturesEverBoundToFBO : 1;
566     bool fUseDrawInsteadOfAllRenderTargetWrites : 1;
567     bool fRequiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines : 1;
568     bool fDontSetBaseOrMaxLevelForExternalTextures : 1;
569     bool fNeverDisableColorWrites : 1;
570     bool fMustSetAnyTexParameterToEnableMipmapping : 1;
571     bool fAllowBGRA8CopyTexSubImage : 1;
572     int fMaxInstancesPerDrawWithoutCrashing = 0;
573 
574     uint32_t fBlitFramebufferFlags = kNoSupport_BlitFramebufferFlag;
575 
576     struct ReadPixelsFormat {
ReadPixelsFormatReadPixelsFormat577         ReadPixelsFormat() : fFormat(0), fType(0) {}
578         GrGLenum fFormat;
579         GrGLenum fType;
580     };
581 
582     /** Number type of the components (with out considering number of bits.) */
583     enum class FormatType {
584         kUnknown,
585         kNormalizedFixedPoint,
586         kFloat,
587     };
588 
589     // ColorTypeInfo for a specific format
590     struct ColorTypeInfo {
591         GrColorType fColorType = GrColorType::kUnknown;
592         enum {
593             kUploadData_Flag = 0x1,
594             // Does Ganesh itself support rendering to this colorType & format pair. Renderability
595             // still additionally depends on if the format can be an FBO color attachment.
596             kRenderable_Flag = 0x2,
597         };
598         uint32_t fFlags = 0;
599 
600         GrSwizzle fReadSwizzle;
601         GrSwizzle fWriteSwizzle;
602 
603         struct ExternalIOFormats {
604             GrColorType fColorType = GrColorType::kUnknown;
605 
606             /** The external format and type are to be used when uploading/downloading data using
607                 data of fColorType and uploading to a texture of a given GrGLFormat and its
608                 intended GrColorType. The fExternalTexImageFormat is the format to use for TexImage
609                 calls. The fExternalReadFormat is used when calling ReadPixels. If either is zero
610                 that signals that either TexImage or ReadPixels is not supported for the combination
611                 of format and color types. */
612             GrGLenum fExternalType = 0;
613             GrGLenum fExternalTexImageFormat = 0;
614             GrGLenum fExternalReadFormat = 0;
615             /**
616              * Must check whether GL_IMPLEMENTATION_COLOR_READ_FORMAT and _TYPE match
617              * fExternalReadFormat and fExternalType before using with glReadPixels.
618              */
619             bool fRequiresImplementationReadQuery = false;
620         };
621 
externalFormatColorTypeInfo622         GrGLenum externalFormat(GrColorType externalColorType, ExternalFormatUsage usage,
623                                 bool haveQueriedImplementationReadFormat) const {
624             for (int i = 0; i < fExternalIOFormatCount; ++i) {
625                 if (fExternalIOFormats[i].fColorType == externalColorType) {
626                     if (usage == kTexImage_ExternalFormatUsage) {
627                         return fExternalIOFormats[i].fExternalTexImageFormat;
628                     } else {
629                         SkASSERT(usage == kReadPixels_ExternalFormatUsage);
630                         if (!haveQueriedImplementationReadFormat &&
631                             fExternalIOFormats[i].fRequiresImplementationReadQuery) {
632                             return 0;
633                         }
634                         return fExternalIOFormats[i].fExternalReadFormat;
635                     }
636                 }
637             }
638             return 0;
639         }
640 
externalTypeColorTypeInfo641         GrGLenum externalType(GrColorType externalColorType) const {
642             for (int i = 0; i < fExternalIOFormatCount; ++i) {
643                 if (fExternalIOFormats[i].fColorType == externalColorType) {
644                     return fExternalIOFormats[i].fExternalType;
645                 }
646             }
647             return 0;
648         }
649 
650         std::unique_ptr<ExternalIOFormats[]> fExternalIOFormats;
651         int fExternalIOFormatCount = 0;
652     };
653 
654     struct FormatInfo {
colorTypeFlagsFormatInfo655         uint32_t colorTypeFlags(GrColorType colorType) const {
656             for (int i = 0; i < fColorTypeInfoCount; ++i) {
657                 if (fColorTypeInfos[i].fColorType == colorType) {
658                     return fColorTypeInfos[i].fFlags;
659                 }
660             }
661             return 0;
662         }
663 
externalFormatFormatInfo664         GrGLenum externalFormat(GrColorType surfaceColorType, GrColorType externalColorType,
665                                 ExternalFormatUsage usage) const {
666             for (int i = 0; i < fColorTypeInfoCount; ++i) {
667                 if (fColorTypeInfos[i].fColorType == surfaceColorType) {
668                     return fColorTypeInfos[i].externalFormat(externalColorType, usage,
669                                                              fHaveQueriedImplementationReadSupport);
670                 }
671             }
672             return 0;
673         }
674 
externalTypeFormatInfo675         GrGLenum externalType(GrColorType surfaceColorType, GrColorType externalColorType) const {
676             for (int i = 0; i < fColorTypeInfoCount; ++i) {
677                 if (fColorTypeInfos[i].fColorType == surfaceColorType) {
678                     return fColorTypeInfos[i].externalType(externalColorType);
679                 }
680             }
681             return 0;
682         }
683 
684         enum {
685             kTexturable_Flag                 = 0x1,
686             /** kFBOColorAttachment means that even if the format cannot be a GrRenderTarget, we can
687                 still attach it to a FBO for blitting or reading pixels. */
688             kFBOColorAttachment_Flag         = 0x2,
689             kFBOColorAttachmentWithMSAA_Flag = 0x4,
690             kUseTexStorage_Flag              = 0x8,
691         };
692         uint32_t fFlags = 0;
693 
694         FormatType fFormatType = FormatType::kUnknown;
695 
696         // Not defined for uncompressed formats. Passed to glCompressedTexImage...
697         GrGLenum fCompressedInternalFormat = 0;
698 
699         // Value to uses as the "internalformat" argument to glTexImage or glTexStorage. It is
700         // initialized in coordination with the presence/absence of the kUseTexStorage flag. In
701         // other words, it is only guaranteed to be compatible with glTexImage if the flag is not
702         // set and or with glTexStorage if the flag is set.
703         GrGLenum fInternalFormatForTexImageOrStorage = 0;
704 
705         // Value to uses as the "internalformat" argument to glRenderbufferStorageMultisample...
706         GrGLenum fInternalFormatForRenderbuffer = 0;
707 
708         // Default values to use along with fInternalFormatForTexImageOrStorage for function
709         // glTexImage2D when not input providing data (passing nullptr) or when clearing it by
710         // uploading a block of solid color data. Not defined for compressed formats.
711         GrGLenum fDefaultExternalFormat = 0;
712         GrGLenum fDefaultExternalType = 0;
713         // When the above two values are used to initialize a texture by uploading cleared data to
714         // it the data should be of this color type.
715         GrColorType fDefaultColorType = GrColorType::kUnknown;
716 
717         bool fHaveQueriedImplementationReadSupport = false;
718 
719         enum {
720             // This indicates that a stencil format has not yet been determined for the config.
721             kUnknown_StencilIndex = -1,
722             // This indicates that there is no supported stencil format for the config.
723             kUnsupported_StencilFormatIndex = -2
724         };
725 
726         // Index fStencilFormats.
727         int fStencilFormatIndex = kUnknown_StencilIndex;
728 
729         SkTDArray<int> fColorSampleCounts;
730 
731         std::unique_ptr<ColorTypeInfo[]> fColorTypeInfos;
732         int fColorTypeInfoCount = 0;
733     };
734 
735     FormatInfo fFormatTable[kGrGLColorFormatCount];
736 
getFormatInfo(GrGLFormat format)737     FormatInfo& getFormatInfo(GrGLFormat format) { return fFormatTable[static_cast<int>(format)]; }
getFormatInfo(GrGLFormat format)738     const FormatInfo& getFormatInfo(GrGLFormat format) const {
739         return fFormatTable[static_cast<int>(format)];
740     }
741 
742     GrGLFormat fColorTypeToFormatTable[kGrColorTypeCnt];
743     void setColorTypeFormat(GrColorType, GrGLFormat);
744 
745     using INHERITED = GrCaps;
746 };
747 
748 #endif
749