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