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