1 /*
2 * Copyright 2015 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 #include "src/gpu/GrCaps.h"
9
10 #include "include/gpu/GrBackendSurface.h"
11 #include "include/gpu/GrContextOptions.h"
12 #include "include/private/GrTypesPriv.h"
13 #include "src/gpu/GrBackendUtils.h"
14 #include "src/gpu/GrRenderTargetProxy.h"
15 #include "src/gpu/GrSurface.h"
16 #include "src/gpu/GrSurfaceProxy.h"
17 #include "src/gpu/GrWindowRectangles.h"
18
GrCaps(const GrContextOptions & options)19 GrCaps::GrCaps(const GrContextOptions& options) {
20 fMipmapSupport = false;
21 fNPOTTextureTileSupport = false;
22 fReuseScratchTextures = true;
23 fReuseScratchBuffers = true;
24 fGpuTracingSupport = false;
25 fOversizedStencilSupport = false;
26 fTextureBarrierSupport = false;
27 fSampleLocationsSupport = false;
28 fDrawInstancedSupport = false;
29 fNativeDrawIndirectSupport = false;
30 fUseClientSideIndirectBuffers = false;
31 fConservativeRasterSupport = false;
32 fWireframeSupport = false;
33 fMSAAResolvesAutomatically = false;
34 fPreferDiscardableMSAAAttachment = false;
35 fUsePrimitiveRestart = false;
36 fPreferClientSideDynamicBuffers = false;
37 fPreferFullscreenClears = false;
38 fTwoSidedStencilRefsAndMasksMustMatch = false;
39 fMustClearUploadedBufferData = false;
40 fShouldInitializeTextures = false;
41 fSupportsAHardwareBufferImages = false;
42 fFenceSyncSupport = false;
43 fSemaphoreSupport = false;
44 fCrossContextTextureSupport = false;
45 fHalfFloatVertexAttributeSupport = false;
46 fDynamicStateArrayGeometryProcessorTextureSupport = false;
47 fPerformPartialClearsAsDraws = false;
48 fPerformColorClearsAsDraws = false;
49 fAvoidLargeIndexBufferDraws = false;
50 fPerformStencilClearsAsDraws = false;
51 fTransferFromBufferToTextureSupport = false;
52 fTransferFromSurfaceToBufferSupport = false;
53 fWritePixelsRowBytesSupport = false;
54 fTransferPixelsToRowBytesSupport = false;
55 fReadPixelsRowBytesSupport = false;
56 fShouldCollapseSrcOverToSrcWhenAble = false;
57 fMustSyncGpuDuringAbandon = true;
58 fDriverDisableMSAAClipAtlas = false;
59 fDisableTessellationPathRenderer = false;
60
61 fBlendEquationSupport = kBasic_BlendEquationSupport;
62 fAdvBlendEqDisableFlags = 0;
63
64 fMapBufferFlags = kNone_MapFlags;
65
66 fMaxVertexAttributes = 0;
67 fMaxRenderTargetSize = 1;
68 fMaxPreferredRenderTargetSize = 1;
69 fMaxTextureSize = 1;
70 fMaxWindowRectangles = 0;
71 fInternalMultisampleCount = 0;
72
73 fSuppressPrints = options.fSuppressPrints;
74 #if GR_TEST_UTILS
75 fWireframeMode = options.fWireframeMode;
76 #else
77 fWireframeMode = false;
78 #endif
79 fBufferMapThreshold = options.fBufferMapThreshold;
80 fAvoidStencilBuffers = false;
81 fAvoidWritePixelsFastPath = false;
82 fRequiresManualFBBarrierAfterTessellatedStencilDraw = false;
83 fNativeDrawIndexedIndirectIsBroken = false;
84 fAvoidReorderingRenderTasks = false;
85 fAvoidDithering = false;
86
87 fPreferVRAMUseOverFlushes = true;
88
89 // Default to true, allow older versions of OpenGL to disable explicitly
90 fClampToBorderSupport = true;
91
92 fDriverBugWorkarounds = options.fDriverBugWorkarounds;
93 }
94
finishInitialization(const GrContextOptions & options)95 void GrCaps::finishInitialization(const GrContextOptions& options) {
96 if (!fNativeDrawIndirectSupport) {
97 // We will implement indirect draws with a polyfill, so the commands need to reside in CPU
98 // memory.
99 fUseClientSideIndirectBuffers = true;
100 }
101
102 this->applyOptionsOverrides(options);
103
104 // Our render targets are always created with textures as the color attachment, hence this min:
105 fMaxRenderTargetSize = std::min(fMaxRenderTargetSize, fMaxTextureSize);
106 fMaxPreferredRenderTargetSize = std::min(fMaxPreferredRenderTargetSize, fMaxRenderTargetSize);
107 }
108
applyOptionsOverrides(const GrContextOptions & options)109 void GrCaps::applyOptionsOverrides(const GrContextOptions& options) {
110 fShaderCaps->applyOptionsOverrides(options);
111 this->onApplyOptionsOverrides(options);
112 if (options.fDisableDriverCorrectnessWorkarounds) {
113 SkASSERT(!fDriverDisableMSAAClipAtlas);
114 SkASSERT(!fDisableTessellationPathRenderer);
115 SkASSERT(!fAvoidStencilBuffers);
116 SkASSERT(!fAvoidWritePixelsFastPath);
117 SkASSERT(!fRequiresManualFBBarrierAfterTessellatedStencilDraw);
118 SkASSERT(!fNativeDrawIndexedIndirectIsBroken);
119 SkASSERT(!fAdvBlendEqDisableFlags);
120 SkASSERT(!fPerformColorClearsAsDraws);
121 SkASSERT(!fPerformStencilClearsAsDraws);
122 // Don't check the partial-clear workaround, since that is a backend limitation, not a
123 // driver workaround (it just so happens the fallbacks are the same).
124 }
125 if (GrContextOptions::Enable::kNo == options.fUseDrawInsteadOfClear) {
126 fPerformColorClearsAsDraws = false;
127 fPerformStencilClearsAsDraws = false;
128 } else if (GrContextOptions::Enable::kYes == options.fUseDrawInsteadOfClear) {
129 fPerformColorClearsAsDraws = true;
130 fPerformStencilClearsAsDraws = true;
131 }
132
133 fMaxTextureSize = std::min(fMaxTextureSize, options.fMaxTextureSizeOverride);
134 #if GR_TEST_UTILS
135 if (options.fSuppressAdvancedBlendEquations) {
136 fBlendEquationSupport = kBasic_BlendEquationSupport;
137 }
138 if (options.fClearAllTextures) {
139 fShouldInitializeTextures = true;
140 }
141 if (options.fDisallowWriteAndTransferPixelRowBytes) {
142 fWritePixelsRowBytesSupport = false;
143 fTransferPixelsToRowBytesSupport = false;
144 }
145 if (options.fAlwaysPreferHardwareTessellation) {
146 fMinPathVerbsForHwTessellation = fMinStrokeVerbsForHwTessellation = 0;
147 }
148 #endif
149 if (options.fSuppressMipmapSupport) {
150 fMipmapSupport = false;
151 }
152
153 if (fMaxWindowRectangles > GrWindowRectangles::kMaxWindows) {
154 SkDebugf("WARNING: capping window rectangles at %i. HW advertises support for %i.\n",
155 GrWindowRectangles::kMaxWindows, fMaxWindowRectangles);
156 fMaxWindowRectangles = GrWindowRectangles::kMaxWindows;
157 }
158
159 fInternalMultisampleCount = options.fInternalMultisampleCount;
160
161 fAvoidStencilBuffers = options.fAvoidStencilBuffers;
162
163 fDriverBugWorkarounds.applyOverrides(options.fDriverBugWorkarounds);
164 }
165
166
167 #ifdef SK_ENABLE_DUMP_GPU
168 #include "src/gpu/GrTestUtils.h"
169 #include "src/utils/SkJSONWriter.h"
170
map_flags_to_string(uint32_t flags)171 static SkString map_flags_to_string(uint32_t flags) {
172 SkString str;
173 if (GrCaps::kNone_MapFlags == flags) {
174 str = "none";
175 } else {
176 SkASSERT(GrCaps::kCanMap_MapFlag & flags);
177 SkDEBUGCODE(flags &= ~GrCaps::kCanMap_MapFlag);
178 str = "can_map";
179
180 if (GrCaps::kSubset_MapFlag & flags) {
181 str.append(" partial");
182 } else {
183 str.append(" full");
184 }
185 SkDEBUGCODE(flags &= ~GrCaps::kSubset_MapFlag);
186 if (GrCaps::kAsyncRead_MapFlag & flags) {
187 str.append(" async_read");
188 } else {
189 str.append(" sync_read");
190 }
191 SkDEBUGCODE(flags &= ~GrCaps::kAsyncRead_MapFlag);
192 }
193 SkASSERT(0 == flags); // Make sure we handled all the flags.
194 return str;
195 }
196
dumpJSON(SkJSONWriter * writer) const197 void GrCaps::dumpJSON(SkJSONWriter* writer) const {
198 writer->beginObject();
199
200 writer->appendBool("MIP Map Support", fMipmapSupport);
201 writer->appendBool("NPOT Texture Tile Support", fNPOTTextureTileSupport);
202 writer->appendBool("Reuse Scratch Textures", fReuseScratchTextures);
203 writer->appendBool("Reuse Scratch Buffers", fReuseScratchBuffers);
204 writer->appendBool("Gpu Tracing Support", fGpuTracingSupport);
205 writer->appendBool("Oversized Stencil Support", fOversizedStencilSupport);
206 writer->appendBool("Texture Barrier Support", fTextureBarrierSupport);
207 writer->appendBool("Sample Locations Support", fSampleLocationsSupport);
208 writer->appendBool("Draw Instanced Support", fDrawInstancedSupport);
209 writer->appendBool("Native Draw Indirect Support", fNativeDrawIndirectSupport);
210 writer->appendBool("Use client side indirect buffers", fUseClientSideIndirectBuffers);
211 writer->appendBool("Conservative Raster Support", fConservativeRasterSupport);
212 writer->appendBool("Wireframe Support", fWireframeSupport);
213 writer->appendBool("MSAA Resolves Automatically", fMSAAResolvesAutomatically);
214 writer->appendBool("Use primitive restart", fUsePrimitiveRestart);
215 writer->appendBool("Prefer client-side dynamic buffers", fPreferClientSideDynamicBuffers);
216 writer->appendBool("Prefer fullscreen clears (and stencil discard)", fPreferFullscreenClears);
217 writer->appendBool("Two-sided Stencil Refs And Masks Must Match",
218 fTwoSidedStencilRefsAndMasksMustMatch);
219 writer->appendBool("Must clear buffer memory", fMustClearUploadedBufferData);
220 writer->appendBool("Should initialize textures", fShouldInitializeTextures);
221 writer->appendBool("Supports importing AHardwareBuffers", fSupportsAHardwareBufferImages);
222 writer->appendBool("Fence sync support", fFenceSyncSupport);
223 writer->appendBool("Semaphore support", fSemaphoreSupport);
224 writer->appendBool("Cross context texture support", fCrossContextTextureSupport);
225 writer->appendBool("Half float vertex attribute support", fHalfFloatVertexAttributeSupport);
226 writer->appendBool("Specify GeometryProcessor textures as a dynamic state array",
227 fDynamicStateArrayGeometryProcessorTextureSupport);
228 writer->appendBool("Use draws for partial clears", fPerformPartialClearsAsDraws);
229 writer->appendBool("Use draws for color clears", fPerformColorClearsAsDraws);
230 writer->appendBool("Avoid Large IndexBuffer Draws", fAvoidLargeIndexBufferDraws);
231 writer->appendBool("Use draws for stencil clip clears", fPerformStencilClearsAsDraws);
232 writer->appendBool("Supports transfers from buffers to textures",
233 fTransferFromBufferToTextureSupport);
234 writer->appendBool("Supports transfers from textures to buffers",
235 fTransferFromSurfaceToBufferSupport);
236 writer->appendBool("Write pixels row bytes support", fWritePixelsRowBytesSupport);
237 writer->appendBool("Transfer pixels to row bytes support", fTransferPixelsToRowBytesSupport);
238 writer->appendBool("Read pixels row bytes support", fReadPixelsRowBytesSupport);
239 writer->appendBool("Disable msaa clip mask atlas on current driver [workaround]",
240 fDriverDisableMSAAClipAtlas);
241 writer->appendBool("Disable TessellationPathRenderer current driver [workaround]",
242 fDisableTessellationPathRenderer);
243 writer->appendBool("Clamp-to-border", fClampToBorderSupport);
244
245 writer->appendBool("Prefer VRAM Use over flushes [workaround]", fPreferVRAMUseOverFlushes);
246 writer->appendBool("Avoid stencil buffers [workaround]", fAvoidStencilBuffers);
247 writer->appendBool("Avoid writePixels fast path [workaround]", fAvoidWritePixelsFastPath);
248 writer->appendBool("Requires manual FB barrier after tessellated stencilDraw [workaround]",
249 fRequiresManualFBBarrierAfterTessellatedStencilDraw);
250 writer->appendBool("Native draw indexed indirect is broken [workaround]",
251 fNativeDrawIndexedIndirectIsBroken);
252 writer->appendBool("Avoid DAG reordering [workaround]", fAvoidReorderingRenderTasks);
253 writer->appendBool("Avoid Dithering [workaround]", fAvoidDithering);
254
255 if (this->advancedBlendEquationSupport()) {
256 writer->appendHexU32("Advanced Blend Equation Disable Flags", fAdvBlendEqDisableFlags);
257 }
258
259 writer->appendS32("Max Vertex Attributes", fMaxVertexAttributes);
260 writer->appendS32("Max Texture Size", fMaxTextureSize);
261 writer->appendS32("Max Render Target Size", fMaxRenderTargetSize);
262 writer->appendS32("Max Preferred Render Target Size", fMaxPreferredRenderTargetSize);
263 writer->appendS32("Max Window Rectangles", fMaxWindowRectangles);
264 writer->appendS32("Sample Count for Internal MSAA", fInternalMultisampleCount);
265 writer->appendS32("Min Path Verbs for HW Tessellation", fMinPathVerbsForHwTessellation);
266 writer->appendS32("Min Stroke Verbs for HW Tessellation", fMinStrokeVerbsForHwTessellation);
267
268 static const char* kBlendEquationSupportNames[] = {
269 "Basic",
270 "Advanced",
271 "Advanced Coherent",
272 };
273 static_assert(0 == kBasic_BlendEquationSupport);
274 static_assert(1 == kAdvanced_BlendEquationSupport);
275 static_assert(2 == kAdvancedCoherent_BlendEquationSupport);
276 static_assert(SK_ARRAY_COUNT(kBlendEquationSupportNames) == kLast_BlendEquationSupport + 1);
277
278 writer->appendString("Blend Equation Support",
279 kBlendEquationSupportNames[fBlendEquationSupport]);
280 writer->appendString("Map Buffer Support", map_flags_to_string(fMapBufferFlags).c_str());
281
282 this->onDumpJSON(writer);
283
284 writer->appendName("shaderCaps");
285 this->shaderCaps()->dumpJSON(writer);
286
287 writer->endObject();
288 }
289 #else
dumpJSON(SkJSONWriter * writer) const290 void GrCaps::dumpJSON(SkJSONWriter* writer) const { }
291 #endif
292
surfaceSupportsWritePixels(const GrSurface * surface) const293 bool GrCaps::surfaceSupportsWritePixels(const GrSurface* surface) const {
294 return surface->readOnly() ? false : this->onSurfaceSupportsWritePixels(surface);
295 }
296
canCopySurface(const GrSurfaceProxy * dst,const GrSurfaceProxy * src,const SkIRect & srcRect,const SkIPoint & dstPoint) const297 bool GrCaps::canCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
298 const SkIRect& srcRect, const SkIPoint& dstPoint) const {
299 if (dst->readOnly()) {
300 return false;
301 }
302
303 if (dst->backendFormat() != src->backendFormat()) {
304 return false;
305 }
306 return this->onCanCopySurface(dst, src, srcRect, dstPoint);
307 }
308
validateSurfaceParams(const SkISize & dimensions,const GrBackendFormat & format,GrRenderable renderable,int renderTargetSampleCnt,GrMipmapped mipped,GrTextureType textureType) const309 bool GrCaps::validateSurfaceParams(const SkISize& dimensions, const GrBackendFormat& format,
310 GrRenderable renderable, int renderTargetSampleCnt,
311 GrMipmapped mipped, GrTextureType textureType) const {
312 if (textureType != GrTextureType::kNone) {
313 if (!this->isFormatTexturable(format, textureType)) {
314 return false;
315 }
316 }
317
318 if (GrMipmapped::kYes == mipped && !this->mipmapSupport()) {
319 return false;
320 }
321
322 if (dimensions.width() < 1 || dimensions.height() < 1) {
323 return false;
324 }
325
326 if (renderable == GrRenderable::kYes) {
327 if (!this->isFormatRenderable(format, renderTargetSampleCnt)) {
328 return false;
329 }
330 int maxRTSize = this->maxRenderTargetSize();
331 if (dimensions.width() > maxRTSize || dimensions.height() > maxRTSize) {
332 return false;
333 }
334 } else {
335 // We currently do not support multisampled textures
336 if (renderTargetSampleCnt != 1) {
337 return false;
338 }
339 int maxSize = this->maxTextureSize();
340 if (dimensions.width() > maxSize || dimensions.height() > maxSize) {
341 return false;
342 }
343 }
344
345 return true;
346 }
347
supportedReadPixelsColorType(GrColorType srcColorType,const GrBackendFormat & srcFormat,GrColorType dstColorType) const348 GrCaps::SupportedRead GrCaps::supportedReadPixelsColorType(GrColorType srcColorType,
349 const GrBackendFormat& srcFormat,
350 GrColorType dstColorType) const {
351 SupportedRead read = this->onSupportedReadPixelsColorType(srcColorType, srcFormat,
352 dstColorType);
353
354 // There are known problems with 24 vs 32 bit BPP with this color type. Just fail for now if
355 // using a transfer buffer.
356 if (GrColorType::kRGB_888x == read.fColorType) {
357 read.fOffsetAlignmentForTransferBuffer = 0;
358 }
359 // It's very convenient to access 1 byte-per-channel 32 bit color types as uint32_t on the CPU.
360 // Make those aligned reads out of the buffer even if the underlying API doesn't require it.
361 auto channelFlags = GrColorTypeChannelFlags(read.fColorType);
362 if ((channelFlags == kRGBA_SkColorChannelFlags || channelFlags == kRGB_SkColorChannelFlags ||
363 channelFlags == kAlpha_SkColorChannelFlag || channelFlags == kGray_SkColorChannelFlag) &&
364 GrColorTypeBytesPerPixel(read.fColorType) == 4) {
365 switch (read.fOffsetAlignmentForTransferBuffer & 0b11) {
366 // offset alignment already a multiple of 4
367 case 0:
368 break;
369 // offset alignment is a multiple of 2 but not 4.
370 case 2:
371 read.fOffsetAlignmentForTransferBuffer *= 2;
372 break;
373 // offset alignment is not a multiple of 2.
374 default:
375 read.fOffsetAlignmentForTransferBuffer *= 4;
376 break;
377 }
378 }
379 return read;
380 }
381
getDefaultBackendFormat(GrColorType colorType,GrRenderable renderable) const382 GrBackendFormat GrCaps::getDefaultBackendFormat(GrColorType colorType,
383 GrRenderable renderable) const {
384 // Unknown color types are always an invalid format, so early out before calling virtual.
385 if (colorType == GrColorType::kUnknown) {
386 return {};
387 }
388
389 auto format = this->onGetDefaultBackendFormat(colorType);
390 if (!this->isFormatTexturable(format, GrTextureType::k2D)) {
391 return {};
392 }
393 if (!this->areColorTypeAndFormatCompatible(colorType, format)) {
394 return {};
395 }
396 // Currently we require that it be possible to write pixels into the "default" format. Perhaps,
397 // that could be a separate requirement from the caller. It seems less necessary if
398 // renderability was requested.
399 if (this->supportedWritePixelsColorType(colorType, format, colorType).fColorType ==
400 GrColorType::kUnknown) {
401 return {};
402 }
403 if (renderable == GrRenderable::kYes &&
404 !this->isFormatAsColorTypeRenderable(colorType, format)) {
405 return {};
406 }
407 return format;
408 }
409
areColorTypeAndFormatCompatible(GrColorType grCT,const GrBackendFormat & format) const410 bool GrCaps::areColorTypeAndFormatCompatible(GrColorType grCT,
411 const GrBackendFormat& format) const {
412 if (GrColorType::kUnknown == grCT) {
413 return false;
414 }
415
416 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
417 if (compression != SkImage::CompressionType::kNone) {
418 return grCT == (SkCompressionTypeIsOpaque(compression) ? GrColorType::kRGB_888x
419 : GrColorType::kRGBA_8888);
420 }
421
422 return this->onAreColorTypeAndFormatCompatible(grCT, format);
423 }
424
getReadSwizzle(const GrBackendFormat & format,GrColorType colorType) const425 skgpu::Swizzle GrCaps::getReadSwizzle(const GrBackendFormat& format, GrColorType colorType) const {
426 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
427 if (compression != SkImage::CompressionType::kNone) {
428 if (colorType == GrColorType::kRGB_888x || colorType == GrColorType::kRGBA_8888) {
429 return skgpu::Swizzle::RGBA();
430 }
431 SkDEBUGFAILF("Illegal color type (%d) and compressed format (%d) combination.",
432 (int)colorType, (int)compression);
433 return {};
434 }
435
436 return this->onGetReadSwizzle(format, colorType);
437 }
438
isFormatCompressed(const GrBackendFormat & format) const439 bool GrCaps::isFormatCompressed(const GrBackendFormat& format) const {
440 return GrBackendFormatToCompressionType(format) != SkImage::CompressionType::kNone;
441 }
442
getDstSampleFlagsForProxy(const GrRenderTargetProxy * rt,bool drawUsesMSAA) const443 GrDstSampleFlags GrCaps::getDstSampleFlagsForProxy(const GrRenderTargetProxy* rt,
444 bool drawUsesMSAA) const {
445 SkASSERT(rt);
446 if (this->textureBarrierSupport() && (!drawUsesMSAA || this->msaaResolvesAutomatically())) {
447 return this->onGetDstSampleFlagsForProxy(rt);
448 }
449 return GrDstSampleFlags::kNone;
450 }
451
supportsDynamicMSAA(const GrRenderTargetProxy * rtProxy) const452 bool GrCaps::supportsDynamicMSAA(const GrRenderTargetProxy* rtProxy) const {
453 return rtProxy->numSamples() == 1 &&
454 this->internalMultisampleCount(rtProxy->backendFormat()) > 1 &&
455 this->onSupportsDynamicMSAA(rtProxy);
456 }
457
color_type_fallback(GrColorType ct)458 static inline GrColorType color_type_fallback(GrColorType ct) {
459 switch (ct) {
460 // kRGBA_8888 is our default fallback for many color types that may not have renderable
461 // backend formats.
462 case GrColorType::kAlpha_8:
463 case GrColorType::kBGR_565:
464 case GrColorType::kABGR_4444:
465 case GrColorType::kBGRA_8888:
466 case GrColorType::kRGBA_1010102:
467 case GrColorType::kBGRA_1010102:
468 case GrColorType::kRGBA_F16:
469 case GrColorType::kRGBA_F16_Clamped:
470 return GrColorType::kRGBA_8888;
471 case GrColorType::kAlpha_F16:
472 return GrColorType::kRGBA_F16;
473 case GrColorType::kGray_8:
474 return GrColorType::kRGB_888x;
475 default:
476 return GrColorType::kUnknown;
477 }
478 }
479
getFallbackColorTypeAndFormat(GrColorType ct,int sampleCnt) const480 std::tuple<GrColorType, GrBackendFormat> GrCaps::getFallbackColorTypeAndFormat(
481 GrColorType ct,
482 int sampleCnt) const {
483 do {
484 auto format = this->getDefaultBackendFormat(ct, GrRenderable::kYes);
485 // We continue to the fallback color type if there no default renderable format or we
486 // requested msaa and the format doesn't support msaa.
487 if (format.isValid() && this->isFormatRenderable(format, sampleCnt)) {
488 return {ct, format};
489 }
490 ct = color_type_fallback(ct);
491 } while (ct != GrColorType::kUnknown);
492 return {GrColorType::kUnknown, {}};
493 }
494