• 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/SkChecksum.h"
14 #include "include/private/base/SkTArray.h"
15 #include "include/private/gpu/ganesh/GrGLTypesPriv.h"
16 #include "src/core/SkTHash.h"
17 #include "src/gpu/Swizzle.h"
18 #include "src/gpu/ganesh/GrCaps.h"
19 #include "src/gpu/ganesh/gl/GrGLAttachment.h"
20 #include "src/gpu/ganesh/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 
65     enum BlitFramebufferFlags {
66         kNoSupport_BlitFramebufferFlag                    = 1 << 0,
67         kNoScalingOrMirroring_BlitFramebufferFlag         = 1 << 1,
68         kResolveMustBeFull_BlitFrambufferFlag             = 1 << 2,
69         kNoMSAADst_BlitFramebufferFlag                    = 1 << 3,
70         kNoFormatConversion_BlitFramebufferFlag           = 1 << 4,
71         kNoFormatConversionForMSAASrc_BlitFramebufferFlag = 1 << 5,
72         kRectsMustMatchForMSAASrc_BlitFramebufferFlag     = 1 << 6,
73     };
74 
75     enum InvalidateFBType {
76         kNone_InvalidateFBType,
77         kDiscard_InvalidateFBType,     //<! glDiscardFramebuffer()
78         kInvalidate_InvalidateFBType,  //<! glInvalidateFramebuffer()
79     };
80 
81     enum class InvalidateBufferType {
82         kNone,
83         kNullData,   // Call glBufferData with a null data pointer.
84         kInvalidate  // glInvalidateBufferData
85     };
86 
87     enum MapBufferType {
88         kNone_MapBufferType,
89         kMapBuffer_MapBufferType,       // glMapBuffer()
90         kMapBufferRange_MapBufferType,  // glMapBufferRange()
91         kChromium_MapBufferType,        // GL_CHROMIUM_map_sub
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&, GrTextureType) 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 
maxTextureMaxAnisotropy()144     float maxTextureMaxAnisotropy() const { return fMaxTextureMaxAnisotropy; }
145 
146     bool isFormatCopyable(const GrBackendFormat&) const override;
147 
148     bool canFormatBeFBOColorAttachment(GrGLFormat) const;
149 
getFormatFromColorType(GrColorType colorType)150     GrGLFormat getFormatFromColorType(GrColorType colorType) const {
151         int idx = static_cast<int>(colorType);
152         return fColorTypeToFormatTable[idx];
153     }
154 
155     /**
156      * Gets the internal format to use with glTexImage...() and glTexStorage...(). May be sized or
157      * base depending upon the GL. Not applicable to compressed textures.
158      */
getTexImageOrStorageInternalFormat(GrGLFormat format)159     GrGLenum getTexImageOrStorageInternalFormat(GrGLFormat format) const {
160         return this->getFormatInfo(format).fInternalFormatForTexImageOrStorage;
161     }
162 
163     /**
164      * Gets the external format and type to pass to glTexImage2D with nullptr to create an
165      * uninitialized texture. See getTexImageOrStorageInternalFormat() for the internal format.
166      */
167     void getTexImageExternalFormatAndType(GrGLFormat surfaceFormat, GrGLenum* externalFormat,
168                                           GrGLenum* externalType) const;
169 
170     /**
171      * Given a src data color type and a color type interpretation for a texture of a given format
172      * this provides the external GL format and type to use with glTexSubImage2d. The color types
173      * should originate from supportedWritePixelsColorType().
174      */
175     void getTexSubImageExternalFormatAndType(GrGLFormat surfaceFormat, GrColorType surfaceColorType,
176                                              GrColorType memoryColorType, GrGLenum* externalFormat,
177                                              GrGLenum* externalType) const;
178 
179     /**
180      * Gets the external format, type, and bytes per pixel to use when uploading solid color data
181      * via glTexSubImage...() to clear the texture at creation.
182      */
183     void getTexSubImageDefaultFormatTypeAndColorType(GrGLFormat format,
184                                                      GrGLenum* externalFormat,
185                                                      GrGLenum* externalType,
186                                                      GrColorType* colorType) const;
187 
188     void getReadPixelsFormat(GrGLFormat surfaceFormat, GrColorType surfaceColorType,
189                              GrColorType memoryColorType, GrGLenum* externalFormat,
190                              GrGLenum* externalType) const;
191 
192     /**
193     * Gets an array of legal stencil formats. These formats are not guaranteed
194     * to be supported by the driver but are legal GLenum names given the GL
195     * version and extensions supported.
196     */
stencilFormats()197     const SkTArray<GrGLFormat, true>& stencilFormats() const {
198         return fStencilFormats;
199     }
200 
201     bool formatSupportsTexStorage(GrGLFormat) const;
202 
203     /**
204      * Would it be useful to check GL_IMPLEMENTATION_READ_FORMAT and _TYPE for this format to
205      * detect more efficient glReadPixels arguments?
206      */
207     bool shouldQueryImplementationReadSupport(GrGLFormat format) const;
208 
209     /**
210      * Let caps know the result of GL_IMPLEMENTATION_READ_FORMAT and _TYPE query for a format
211      * to update supported glReadPixels arguments.
212      */
213     void didQueryImplementationReadSupport(GrGLFormat format,
214                                            GrGLenum readFormat,
215                                            GrGLenum readType) const;
216 
217     /**
218      * Gets the internal format to use with glRenderbufferStorageMultisample...(). May be sized or
219      * base depending upon the GL. Not applicable to compressed textures.
220      */
getRenderbufferInternalFormat(GrGLFormat format)221     GrGLenum getRenderbufferInternalFormat(GrGLFormat format) const {
222         return this->getFormatInfo(format).fInternalFormatForRenderbuffer;
223     }
224 
225     /**
226      * Gets the default external type to use with glTex[Sub]Image... when the data pointer is null.
227      */
getFormatDefaultExternalType(GrGLFormat format)228     GrGLenum getFormatDefaultExternalType(GrGLFormat format) const {
229         return this->getFormatInfo(format).fDefaultExternalType;
230     }
231 
232     /**
233      * Has a stencil format index been found for the format (or we've found that no format works).
234      */
hasStencilFormatBeenDeterminedForFormat(GrGLFormat format)235     bool hasStencilFormatBeenDeterminedForFormat(GrGLFormat format) const {
236         return this->getFormatInfo(format).fStencilFormatIndex != FormatInfo::kUnknown_StencilIndex;
237     }
238 
239     /**
240      * Gets the stencil format index for the format. This assumes
241      * hasStencilFormatBeenDeterminedForFormat has already been checked. Returns a value < 0 if
242      * no stencil format is supported with the format. Otherwise, returned index refers to the array
243      * returned by stencilFormats().
244      */
getStencilFormatIndexForFormat(GrGLFormat format)245     int getStencilFormatIndexForFormat(GrGLFormat format) const {
246         SkASSERT(this->hasStencilFormatBeenDeterminedForFormat(format));
247         return this->getFormatInfo(format).fStencilFormatIndex;
248     }
249 
250     /**
251      * If index is >= 0 this records an index into stencilFormats() as the best stencil format for
252      * the format. If < 0 it records that the format has no supported stencil format index.
253      */
254     void setStencilFormatIndexForFormat(GrGLFormat, int index);
255 
256     /**
257      * Reports the type of MSAA FBO support.
258      */
msFBOType()259     MSFBOType msFBOType() const { return fMSFBOType; }
260 
261     /**
262      * Does the preferred MSAA FBO extension have MSAA renderbuffers?
263      */
usesMSAARenderBuffers()264     bool usesMSAARenderBuffers() const {
265         return kNone_MSFBOType != fMSFBOType &&
266                kES_IMG_MsToTexture_MSFBOType != fMSFBOType &&
267                kES_EXT_MsToTexture_MSFBOType != fMSFBOType;
268     }
269 
270     /**
271      * Is it unsupported to only resolve a sub-rectangle of a framebuffer?
272      */
framebufferResolvesMustBeFullSize()273     bool framebufferResolvesMustBeFullSize() const {
274         SkASSERT(fMSFBOType != kNone_MSFBOType);
275         return fMSFBOType == kES_Apple_MSFBOType ||
276                (fBlitFramebufferFlags & kResolveMustBeFull_BlitFrambufferFlag);
277     }
278 
279     /**
280      * Can we resolve a single-sample framebuffer into an MSAA framebuffer?
281      */
canResolveSingleToMSAA()282     bool canResolveSingleToMSAA() const {
283         SkASSERT(fMSFBOType != kNone_MSFBOType);
284         return fMSFBOType != kES_Apple_MSFBOType &&
285                !(fBlitFramebufferFlags & GrGLCaps::kNoMSAADst_BlitFramebufferFlag);
286     }
287 
288     /**
289      * Is the MSAA FBO extension one where the texture is multisampled when bound to an FBO and
290      * then implicitly resolved when read.
291      */
usesImplicitMSAAResolve()292     bool usesImplicitMSAAResolve() const {
293         return kES_IMG_MsToTexture_MSFBOType == fMSFBOType ||
294                kES_EXT_MsToTexture_MSFBOType == fMSFBOType;
295     }
296 
invalidateFBType()297     InvalidateFBType invalidateFBType() const { return fInvalidateFBType; }
298 
299     /// What type of buffer mapping is supported?
mapBufferType()300     MapBufferType mapBufferType() const { return fMapBufferType; }
301 
302     /// What type of transfer buffer is supported?
transferBufferType()303     TransferBufferType transferBufferType() const { return fTransferBufferType; }
304 
305     /// How are GrFences implemented?
fenceType()306     FenceType fenceType() const { return fFenceType; }
307 
308     /// How are multi draws implemented (if at all)?
multiDrawType()309     MultiDrawType multiDrawType() const { return fMultiDrawType; }
310 
311     /// The maximum number of fragment uniform vectors (GLES has min. 16).
maxFragmentUniformVectors()312     int maxFragmentUniformVectors() const { return fMaxFragmentUniformVectors; }
313 
314     /// Is there support for GL_PACK_REVERSE_ROW_ORDER
packFlipYSupport()315     bool packFlipYSupport() const { return fPackFlipYSupport; }
316 
317     /// Is there support for texture parameter GL_TEXTURE_USAGE
textureUsageSupport()318     bool textureUsageSupport() const { return fTextureUsageSupport; }
319 
320     /// Is GL_ARB_IMAGING supported
imagingSupport()321     bool imagingSupport() const { return fImagingSupport; }
322 
323     /// Is there support for Vertex Array Objects?
vertexArrayObjectSupport()324     bool vertexArrayObjectSupport() const { return fVertexArrayObjectSupport; }
325 
326     /// Is there support for GL_KHR_debug?
debugSupport()327     bool debugSupport() const { return fDebugSupport; }
328 
329     /// Is there support for ES2 compatability?
ES2CompatibilitySupport()330     bool ES2CompatibilitySupport() const { return fES2CompatibilitySupport; }
331 
332     /// Is there support for glDrawRangeElements?
drawRangeElementsSupport()333     bool drawRangeElementsSupport() const { return fDrawRangeElementsSupport; }
334 
335     /// Are the glDraw*Base(VertexBase)Instance methods, and baseInstance fields in indirect draw
336     //commands supported?
baseVertexBaseInstanceSupport()337     bool baseVertexBaseInstanceSupport() const { return fBaseVertexBaseInstanceSupport; }
338 
339     SurfaceReadPixelsSupport surfaceSupportsReadPixels(const GrSurface*) const override;
340 
341     SupportedWrite supportedWritePixelsColorType(GrColorType surfaceColorType,
342                                                  const GrBackendFormat& surfaceFormat,
343                                                  GrColorType srcColorType) const override;
344 
isCoreProfile()345     bool isCoreProfile() const { return fIsCoreProfile; }
346 
bindFragDataLocationSupport()347     bool bindFragDataLocationSupport() const { return fBindFragDataLocationSupport; }
348 
bindUniformLocationSupport()349     bool bindUniformLocationSupport() const { return fBindUniformLocationSupport; }
350 
351     /// Are textures with GL_TEXTURE_RECTANGLE type supported.
rectangleTextureSupport()352     bool rectangleTextureSupport() const { return fRectangleTextureSupport; }
353 
354     /// Can set the BASE and MAX mip map level.
mipmapLevelControlSupport()355     bool mipmapLevelControlSupport() const { return fMipmapLevelControlSupport; }
356 
357     /// Can set the MIN/MAX LOD value.
mipmapLodControlSupport()358     bool mipmapLodControlSupport() const { return fMipmapLodControlSupport; }
359 
doManualMipmapping()360     bool doManualMipmapping() const { return fDoManualMipmapping; }
361 
362     void onDumpJSON(SkJSONWriter*) const override;
363 
invalidateBufferType()364     InvalidateBufferType invalidateBufferType() const { return fInvalidateBufferType; }
365 
366     // Certain Intel GPUs on Mac fail to clear if the glClearColor is made up of only 1s and 0s.
clearToBoundaryValuesIsBroken()367     bool clearToBoundaryValuesIsBroken() const { return fClearToBoundaryValuesIsBroken; }
368 
369     /// glClearTex(Sub)Image support
clearTextureSupport()370     bool clearTextureSupport() const { return fClearTextureSupport; }
371 
372     // Adreno/MSAA drops a draw on the imagefiltersbase GM if the base vertex param to
373     // glDrawArrays is nonzero.
374     // https://bugs.chromium.org/p/skia/issues/detail?id=6650
drawArraysBaseVertexIsBroken()375     bool drawArraysBaseVertexIsBroken() const { return fDrawArraysBaseVertexIsBroken; }
376 
377     // If true then we must use an intermediate surface to perform partial updates to unorm textures
378     // that have ever been bound to a FBO.
disallowTexSubImageForUnormConfigTexturesEverBoundToFBO()379     bool disallowTexSubImageForUnormConfigTexturesEverBoundToFBO() const {
380         return fDisallowTexSubImageForUnormConfigTexturesEverBoundToFBO;
381     }
382 
383     // Use an intermediate surface to write pixels (full or partial overwrite) to into a texture
384     // that is bound to an FBO.
useDrawInsteadOfAllRenderTargetWrites()385     bool useDrawInsteadOfAllRenderTargetWrites() const {
386         return fUseDrawInsteadOfAllRenderTargetWrites;
387     }
388 
389     // At least some Adreno 3xx drivers draw lines incorrectly after drawing non-lines. Toggling
390     // face culling on and off seems to resolve this.
requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines()391     bool requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() const {
392         return fRequiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines;
393     }
394 
395     // Older Android versions seem to have an issue with setting GL_TEXTURE_BASE_LEVEL or
396     // GL_TEXTURE_MAX_LEVEL for GL_TEXTURE_EXTERNAL_OES textures.
dontSetBaseOrMaxLevelForExternalTextures()397     bool dontSetBaseOrMaxLevelForExternalTextures() const {
398         return fDontSetBaseOrMaxLevelForExternalTextures;
399     }
400 
401     // PowerVRGX6250 drops every pixel if we modify the sample mask while color writes are disabled.
neverDisableColorWrites()402     bool neverDisableColorWrites() const { return fNeverDisableColorWrites; }
403 
404     // Texture parameters must be used to enable MIP mapping even when a sampler object is used.
mustSetAnyTexParameterToEnableMipmapping()405     bool mustSetAnyTexParameterToEnableMipmapping() const {
406         return fMustSetAnyTexParameterToEnableMipmapping;
407     }
408 
409     // Whether we must reset the blend function to not reference src2 when disabling blending after
410     // previously referencing src2.
mustResetBlendFuncBetweenDualSourceAndDisable()411     bool mustResetBlendFuncBetweenDualSourceAndDisable() const {
412         return fMustResetBlendFuncBetweenDualSourceAndDisable;
413     }
414 
415     // Before changing the sample count of a texture bound to an FBO with
416     // glFramebufferTexture2DMultisample() temporarily bind texture 0 to avoid corruption int the
417     // texture contents.
bindTexture0WhenChangingTextureFBOMultisampleCount()418     bool bindTexture0WhenChangingTextureFBOMultisampleCount() const {
419         return fBindTexture0WhenChangingTextureFBOMultisampleCount;
420     }
421 
422     // After using glCheckFramebufferStatus() bind 0 to the color attachment and then rebind the
423     // original color attachment.
rebindColorAttachmentAfterCheckFramebufferStatus()424     bool rebindColorAttachmentAfterCheckFramebufferStatus() const {
425         return fRebindColorAttachmentAfterCheckFramebufferStatus;
426     }
427 
428     // During writePixels, call glFlush() before issuing glTexSubImage2D().
flushBeforeWritePixels()429     bool flushBeforeWritePixels() const {
430         return fFlushBeforeWritePixels;
431     }
432 
433     // Returns the observed maximum number of instances the driver can handle in a single draw call
434     // without crashing, or 'pendingInstanceCount' if this workaround is not necessary.
435     // NOTE: the return value may be larger than pendingInstanceCount.
maxInstancesPerDrawWithoutCrashing(int pendingInstanceCount)436     int maxInstancesPerDrawWithoutCrashing(int pendingInstanceCount) const {
437         return (fMaxInstancesPerDrawWithoutCrashing)
438                 ? fMaxInstancesPerDrawWithoutCrashing : pendingInstanceCount;
439     }
440 
441     bool canCopyTexSubImage(GrGLFormat dstFormat, bool dstHasMSAARenderBuffer,
442                             const GrTextureType* dstTypeIfTexture,
443                             GrGLFormat srcFormat, bool srcHasMSAARenderBuffer,
444                             const GrTextureType* srcTypeIfTexture) const;
445     bool canCopyAsBlit(GrGLFormat dstFormat, int dstSampleCnt,
446                        const GrTextureType* dstTypeIfTexture,
447                        GrGLFormat srcFormat, int srcSampleCnt,
448                        const GrTextureType* srcTypeIfTexture,
449                        const SkRect& srcBounds, bool srcBoundsExact,
450                        const SkIRect& srcRect, const SkIRect& dstRect) const;
451     bool canCopyAsDraw(GrGLFormat dstFormat, bool srcIsTexturable, bool scalingCopy) const;
452 
453     DstCopyRestrictions getDstCopyRestrictions(const GrRenderTargetProxy* src,
454                                                GrColorType) const override;
455 
programBinarySupport()456     bool programBinarySupport() const { return fProgramBinarySupport; }
programParameterSupport()457     bool programParameterSupport() const { return fProgramParameterSupport; }
458     bool programBinaryFormatIsValid(GrGLenum binaryFormat) const;
459 
460     /** Are sampler objects available in this GL? */
samplerObjectSupport()461     bool samplerObjectSupport() const { return fSamplerObjectSupport; }
462 
463     /**
464      * Are we using sampler objects in favor of texture parameters? (This will only be true if
465      * samplerObjectSupport()).
466      */
useSamplerObjects()467     bool useSamplerObjects() const { return fUseSamplerObjects; }
468 
textureSwizzleSupport()469     bool textureSwizzleSupport() const { return fTextureSwizzleSupport; }
470 
tiledRenderingSupport()471     bool tiledRenderingSupport() const { return fTiledRenderingSupport; }
472 
fbFetchRequiresEnablePerSample()473     bool fbFetchRequiresEnablePerSample() const { return fFBFetchRequiresEnablePerSample; }
474 
475     /* Is there support for enabling/disabling sRGB writes for sRGB-capable color buffers? */
srgbWriteControl()476     bool srgbWriteControl() const { return fSRGBWriteControl; }
477 
478     /**
479      * Skip checks for GL errors and framebuffer completeness. Note that this does not skip
480      *  checking shader compilation and program linking status.
481      */
skipErrorChecks()482     bool skipErrorChecks() const { return fSkipErrorChecks; }
483 
supportsProtected()484     bool supportsProtected() const { return fSupportsProtected; }
485 
clientCanDisableMultisample()486     bool clientCanDisableMultisample() const { return fClientCanDisableMultisample; }
487 
488     GrBackendFormat getBackendFormatFromCompressionType(SkImage::CompressionType) const override;
489 
490     skgpu::Swizzle getWriteSwizzle(const GrBackendFormat&, GrColorType) const override;
491 
492     uint64_t computeFormatKey(const GrBackendFormat&) const override;
493 
494     GrProgramDesc makeDesc(GrRenderTarget*,
495                            const GrProgramInfo&,
496                            ProgramDescOverrideFlags) const override;
497 
498 #if GR_TEST_UTILS
standard()499     GrGLStandard standard() const { return fStandard; }
500 
501     std::vector<GrTest::TestFormatColorTypeCombination> getTestingCombinations() const override;
502 #endif
503 
504 private:
505     enum ExternalFormatUsage {
506         kTexImage_ExternalFormatUsage,
507         kReadPixels_ExternalFormatUsage,
508     };
509     void getExternalFormat(GrGLFormat surfaceFormat, GrColorType surfaceColorType,
510                            GrColorType memoryColorType, ExternalFormatUsage usage,
511                            GrGLenum* externalFormat, GrGLenum* externalType) const;
512 
513     void init(const GrContextOptions&, const GrGLContextInfo&, const GrGLInterface*);
514     void initGLSL(const GrGLContextInfo&, const GrGLInterface*);
515 
516     struct FormatWorkarounds {
517         bool fDisableSRGBRenderWithMSAAForMacAMD = false;
518         bool fDisableRGBA16FTexStorageForCrBug1008003 = false;
519         bool fDisableBGRATextureStorageForIntelWindowsES = false;
520         bool fDisableLuminance16F = false;
521         bool fDisableTexStorage = false;
522         bool fDisallowDirectRG8ReadPixels = false;
523         bool fDisallowBGRA8ReadPixels = false;
524         bool fDisallowR8ForPowerVRSGX54x = false;
525         bool fDisallowUnorm16Transfers = false;
526         bool fDisallowTextureUnorm16 = false;
527         bool fDisallowETC2Compression = false;
528     };
529 
530     void applyDriverCorrectnessWorkarounds(const GrGLContextInfo&, const GrContextOptions&,
531                                            const GrGLInterface*,
532                                            GrShaderCaps*, FormatWorkarounds*);
533 
534     void onApplyOptionsOverrides(const GrContextOptions& options) override;
535 
536     bool onIsWindowRectanglesSupportedForRT(const GrBackendRenderTarget&) const override;
537 
538     void initFSAASupport(const GrContextOptions& contextOptions, const GrGLContextInfo&,
539                          const GrGLInterface*);
540     void initBlendEqationSupport(const GrGLContextInfo&);
541     void initStencilSupport(const GrGLContextInfo&);
542     // This must be called after initFSAASupport().
543     void initFormatTable(const GrGLContextInfo&, const GrGLInterface*, const FormatWorkarounds&);
544     void setupSampleCounts(const GrGLContextInfo&, const GrGLInterface*);
545     bool onSurfaceSupportsWritePixels(const GrSurface*) const override;
546     bool onCanCopySurface(const GrSurfaceProxy* dst, const SkIRect& dstRect,
547                           const GrSurfaceProxy* src, const SkIRect& srcRect) const override;
548     GrBackendFormat onGetDefaultBackendFormat(GrColorType) const override;
549     bool onAreColorTypeAndFormatCompatible(GrColorType, const GrBackendFormat&) const override;
550 
551     SupportedRead onSupportedReadPixelsColorType(GrColorType, const GrBackendFormat&,
552                                                  GrColorType) const override;
553 
554     skgpu::Swizzle onGetReadSwizzle(const GrBackendFormat&, GrColorType) const override;
555 
556     GrDstSampleFlags onGetDstSampleFlagsForProxy(const GrRenderTargetProxy*) const override;
557 
558     bool onSupportsDynamicMSAA(const GrRenderTargetProxy*) const override;
559 
560     GrGLStandard fStandard = kNone_GrGLStandard;
561 
562     SkTArray<GrGLFormat, true> fStencilFormats;
563     SkTArray<GrGLenum, true> fProgramBinaryFormats;
564 
565     int fMaxFragmentUniformVectors = 0;
566     float fMaxTextureMaxAnisotropy = 1.f;
567 
568     MSFBOType            fMSFBOType            = kNone_MSFBOType;
569     InvalidateFBType     fInvalidateFBType     = kNone_InvalidateFBType;
570     InvalidateBufferType fInvalidateBufferType = InvalidateBufferType::kNone;
571     MapBufferType        fMapBufferType        = kNone_MapBufferType;
572     TransferBufferType   fTransferBufferType   = TransferBufferType::kNone;
573     FenceType            fFenceType            = FenceType::kNone;
574     MultiDrawType        fMultiDrawType        = MultiDrawType::kNone;
575 
576     bool fPackFlipYSupport : 1;
577     bool fTextureUsageSupport : 1;
578     bool fImagingSupport  : 1;
579     bool fVertexArrayObjectSupport : 1;
580     bool fDebugSupport : 1;
581     bool fES2CompatibilitySupport : 1;
582     bool fDrawRangeElementsSupport : 1;
583     bool fBaseVertexBaseInstanceSupport : 1;
584     bool fIsCoreProfile : 1;
585     bool fBindFragDataLocationSupport : 1;
586     bool fBindUniformLocationSupport : 1;
587     bool fRectangleTextureSupport : 1;
588     bool fMipmapLevelControlSupport : 1;
589     bool fMipmapLodControlSupport : 1;
590     bool fClearTextureSupport : 1;
591     bool fProgramBinarySupport : 1;
592     bool fProgramParameterSupport : 1;
593     bool fSamplerObjectSupport : 1;
594     bool fUseSamplerObjects : 1;
595     bool fTextureSwizzleSupport : 1;
596     bool fTiledRenderingSupport : 1;
597     bool fFBFetchRequiresEnablePerSample : 1;
598     bool fSRGBWriteControl : 1;
599     bool fSkipErrorChecks : 1;
600     bool fClientCanDisableMultisample : 1;
601     bool fSupportsProtected : 1;
602 
603     // Driver workarounds
604     bool fDoManualMipmapping : 1;
605     bool fClearToBoundaryValuesIsBroken : 1;
606     bool fDrawArraysBaseVertexIsBroken : 1;
607     bool fDisallowTexSubImageForUnormConfigTexturesEverBoundToFBO : 1;
608     bool fUseDrawInsteadOfAllRenderTargetWrites : 1;
609     bool fRequiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines : 1;
610     bool fDontSetBaseOrMaxLevelForExternalTextures : 1;
611     bool fNeverDisableColorWrites : 1;
612     bool fMustSetAnyTexParameterToEnableMipmapping : 1;
613     bool fAllowBGRA8CopyTexSubImage : 1;
614     bool fDisallowDynamicMSAA : 1;
615     bool fMustResetBlendFuncBetweenDualSourceAndDisable : 1;
616     bool fBindTexture0WhenChangingTextureFBOMultisampleCount : 1;
617     bool fRebindColorAttachmentAfterCheckFramebufferStatus : 1;
618     bool fFlushBeforeWritePixels : 1;
619     bool fDisableScalingCopyAsDraws : 1;
620     int fMaxInstancesPerDrawWithoutCrashing = 0;
621 
622     uint32_t fBlitFramebufferFlags = kNoSupport_BlitFramebufferFlag;
623 
624     struct ReadPixelsFormat {
ReadPixelsFormatReadPixelsFormat625         ReadPixelsFormat() : fFormat(0), fType(0) {}
626         GrGLenum fFormat;
627         GrGLenum fType;
628     };
629 
630     /** Number type of the components (with out considering number of bits.) */
631     enum class FormatType {
632         kUnknown,
633         kNormalizedFixedPoint,
634         kFloat,
635     };
636 
637     // ColorTypeInfo for a specific format
638     struct ColorTypeInfo {
639         GrColorType fColorType = GrColorType::kUnknown;
640         enum {
641             kUploadData_Flag = 0x1,
642             // Does Ganesh itself support rendering to this colorType & format pair. Renderability
643             // still additionally depends on if the format can be an FBO color attachment.
644             kRenderable_Flag = 0x2,
645         };
646         uint32_t fFlags = 0;
647 
648         skgpu::Swizzle fReadSwizzle;
649         skgpu::Swizzle fWriteSwizzle;
650 
651         struct ExternalIOFormats {
652             GrColorType fColorType = GrColorType::kUnknown;
653 
654             /** The external format and type are to be used when uploading/downloading data using
655                 data of fColorType and uploading to a texture of a given GrGLFormat and its
656                 intended GrColorType. The fExternalTexImageFormat is the format to use for TexImage
657                 calls. The fExternalReadFormat is used when calling ReadPixels. If either is zero
658                 that signals that either TexImage or ReadPixels is not supported for the combination
659                 of format and color types. */
660             GrGLenum fExternalType = 0;
661             GrGLenum fExternalTexImageFormat = 0;
662             GrGLenum fExternalReadFormat = 0;
663             /**
664              * Must check whether GL_IMPLEMENTATION_COLOR_READ_FORMAT and _TYPE match
665              * fExternalReadFormat and fExternalType before using with glReadPixels.
666              */
667             bool fRequiresImplementationReadQuery = false;
668         };
669 
externalFormatColorTypeInfo670         GrGLenum externalFormat(GrColorType externalColorType, ExternalFormatUsage usage,
671                                 bool haveQueriedImplementationReadFormat) const {
672             for (int i = 0; i < fExternalIOFormatCount; ++i) {
673                 if (fExternalIOFormats[i].fColorType == externalColorType) {
674                     if (usage == kTexImage_ExternalFormatUsage) {
675                         return fExternalIOFormats[i].fExternalTexImageFormat;
676                     } else {
677                         SkASSERT(usage == kReadPixels_ExternalFormatUsage);
678                         if (!haveQueriedImplementationReadFormat &&
679                             fExternalIOFormats[i].fRequiresImplementationReadQuery) {
680                             return 0;
681                         }
682                         return fExternalIOFormats[i].fExternalReadFormat;
683                     }
684                 }
685             }
686             return 0;
687         }
688 
externalTypeColorTypeInfo689         GrGLenum externalType(GrColorType externalColorType) const {
690             for (int i = 0; i < fExternalIOFormatCount; ++i) {
691                 if (fExternalIOFormats[i].fColorType == externalColorType) {
692                     return fExternalIOFormats[i].fExternalType;
693                 }
694             }
695             return 0;
696         }
697 
698         std::unique_ptr<ExternalIOFormats[]> fExternalIOFormats;
699         int fExternalIOFormatCount = 0;
700     };
701 
702     struct FormatInfo {
colorTypeFlagsFormatInfo703         uint32_t colorTypeFlags(GrColorType colorType) const {
704             for (int i = 0; i < fColorTypeInfoCount; ++i) {
705                 if (fColorTypeInfos[i].fColorType == colorType) {
706                     return fColorTypeInfos[i].fFlags;
707                 }
708             }
709             return 0;
710         }
711 
externalFormatFormatInfo712         GrGLenum externalFormat(GrColorType surfaceColorType, GrColorType externalColorType,
713                                 ExternalFormatUsage usage) const {
714             for (int i = 0; i < fColorTypeInfoCount; ++i) {
715                 if (fColorTypeInfos[i].fColorType == surfaceColorType) {
716                     return fColorTypeInfos[i].externalFormat(externalColorType, usage,
717                                                              fHaveQueriedImplementationReadSupport);
718                 }
719             }
720             return 0;
721         }
722 
externalTypeFormatInfo723         GrGLenum externalType(GrColorType surfaceColorType, GrColorType externalColorType) const {
724             for (int i = 0; i < fColorTypeInfoCount; ++i) {
725                 if (fColorTypeInfos[i].fColorType == surfaceColorType) {
726                     return fColorTypeInfos[i].externalType(externalColorType);
727                 }
728             }
729             return 0;
730         }
731 
732         enum {
733             kTexturable_Flag                 = 0x01,
734             /** kFBOColorAttachment means that even if the format cannot be a GrRenderTarget, we can
735                 still attach it to a FBO for blitting or reading pixels. */
736             kFBOColorAttachment_Flag         = 0x02,
737             kFBOColorAttachmentWithMSAA_Flag = 0x04,
738             kUseTexStorage_Flag              = 0x08,
739             /**
740              * Are pixel buffer objects supported in/out of this format? Ignored if PBOs are not
741              * supported at all.
742              */
743             kTransfers_Flag                  = 0x10,
744         };
745         uint32_t fFlags = 0;
746 
747         FormatType fFormatType = FormatType::kUnknown;
748 
749         // Not defined for uncompressed formats. Passed to glCompressedTexImage...
750         GrGLenum fCompressedInternalFormat = 0;
751 
752         // Value to uses as the "internalformat" argument to glTexImage or glTexStorage. It is
753         // initialized in coordination with the presence/absence of the kUseTexStorage flag. In
754         // other words, it is only guaranteed to be compatible with glTexImage if the flag is not
755         // set and or with glTexStorage if the flag is set.
756         GrGLenum fInternalFormatForTexImageOrStorage = 0;
757 
758         // Value to uses as the "internalformat" argument to glRenderbufferStorageMultisample...
759         GrGLenum fInternalFormatForRenderbuffer = 0;
760 
761         // Default values to use along with fInternalFormatForTexImageOrStorage for function
762         // glTexImage2D when not input providing data (passing nullptr) or when clearing it by
763         // uploading a block of solid color data. Not defined for compressed formats.
764         GrGLenum fDefaultExternalFormat = 0;
765         GrGLenum fDefaultExternalType = 0;
766         // When the above two values are used to initialize a texture by uploading cleared data to
767         // it the data should be of this color type.
768         GrColorType fDefaultColorType = GrColorType::kUnknown;
769 
770         bool fHaveQueriedImplementationReadSupport = false;
771 
772         enum {
773             // This indicates that a stencil format has not yet been determined for the config.
774             kUnknown_StencilIndex = -1,
775             // This indicates that there is no supported stencil format for the config.
776             kUnsupported_StencilFormatIndex = -2
777         };
778 
779         // Index fStencilFormats.
780         int fStencilFormatIndex = kUnknown_StencilIndex;
781 
782         SkTDArray<int> fColorSampleCounts;
783 
784         std::unique_ptr<ColorTypeInfo[]> fColorTypeInfos;
785         int fColorTypeInfoCount = 0;
786     };
787 
788     FormatInfo fFormatTable[kGrGLColorFormatCount];
789 
getFormatInfo(GrGLFormat format)790     FormatInfo& getFormatInfo(GrGLFormat format) { return fFormatTable[static_cast<int>(format)]; }
getFormatInfo(GrGLFormat format)791     const FormatInfo& getFormatInfo(GrGLFormat format) const {
792         return fFormatTable[static_cast<int>(format)];
793     }
794 
795     GrGLFormat fColorTypeToFormatTable[kGrColorTypeCnt];
796     void setColorTypeFormat(GrColorType, GrGLFormat);
797 
798     using INHERITED = GrCaps;
799 };
800 
801 #endif
802