Debian Codegraph @

src/gpu/ganesh/GrCaps.cpp

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