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 "GrCaps.h"
9
10 #include "GrBackendSurface.h"
11 #include "GrContextOptions.h"
12 #include "GrWindowRectangles.h"
13 #include "SkJSONWriter.h"
14
pixel_config_name(GrPixelConfig config)15 static const char* pixel_config_name(GrPixelConfig config) {
16 switch (config) {
17 case kUnknown_GrPixelConfig: return "Unknown";
18 case kAlpha_8_GrPixelConfig: return "Alpha8";
19 case kAlpha_8_as_Alpha_GrPixelConfig: return "Alpha8_asAlpha";
20 case kAlpha_8_as_Red_GrPixelConfig: return "Alpha8_asRed";
21 case kGray_8_GrPixelConfig: return "Gray8";
22 case kGray_8_as_Lum_GrPixelConfig: return "Gray8_asLum";
23 case kGray_8_as_Red_GrPixelConfig: return "Gray8_asRed";
24 case kRGB_565_GrPixelConfig: return "RGB565";
25 case kRGBA_4444_GrPixelConfig: return "RGBA444";
26 case kRGBA_8888_GrPixelConfig: return "RGBA8888";
27 case kBGRA_8888_GrPixelConfig: return "BGRA8888";
28 case kSRGBA_8888_GrPixelConfig: return "SRGBA8888";
29 case kSBGRA_8888_GrPixelConfig: return "SBGRA8888";
30 case kRGBA_1010102_GrPixelConfig: return "RGBA1010102";
31 case kRGBA_float_GrPixelConfig: return "RGBAFloat";
32 case kRG_float_GrPixelConfig: return "RGFloat";
33 case kAlpha_half_GrPixelConfig: return "AlphaHalf";
34 case kAlpha_half_as_Red_GrPixelConfig: return "AlphaHalf_asRed";
35 case kRGBA_half_GrPixelConfig: return "RGBAHalf";
36 }
37 SK_ABORT("Invalid pixel config");
38 return "<invalid>";
39 }
40
GrCaps(const GrContextOptions & options)41 GrCaps::GrCaps(const GrContextOptions& options) {
42 fMipMapSupport = false;
43 fNPOTTextureTileSupport = false;
44 fSRGBSupport = false;
45 fSRGBWriteControl = false;
46 fSRGBDecodeDisableSupport = false;
47 fDiscardRenderTargetSupport = false;
48 fReuseScratchTextures = true;
49 fReuseScratchBuffers = true;
50 fGpuTracingSupport = false;
51 fOversizedStencilSupport = false;
52 fTextureBarrierSupport = false;
53 fSampleLocationsSupport = false;
54 fMultisampleDisableSupport = false;
55 fInstanceAttribSupport = false;
56 fUsesMixedSamples = false;
57 fUsePrimitiveRestart = false;
58 fPreferClientSideDynamicBuffers = false;
59 fPreferFullscreenClears = false;
60 fMustClearUploadedBufferData = false;
61 fSampleShadingSupport = false;
62 fFenceSyncSupport = false;
63 fCrossContextTextureSupport = false;
64
65 fBlendEquationSupport = kBasic_BlendEquationSupport;
66 fAdvBlendEqBlacklist = 0;
67
68 fMapBufferFlags = kNone_MapFlags;
69
70 fMaxVertexAttributes = 0;
71 fMaxRenderTargetSize = 1;
72 fMaxPreferredRenderTargetSize = 1;
73 fMaxTextureSize = 1;
74 fMaxRasterSamples = 0;
75 fMaxWindowRectangles = 0;
76
77 // An default count of 4 was chosen because of the common pattern in Blink of:
78 // isect RR
79 // diff RR
80 // isect convex_poly
81 // isect convex_poly
82 // when drawing rounded div borders.
83 fMaxClipAnalyticFPs = 4;
84
85 fSuppressPrints = options.fSuppressPrints;
86 #if GR_TEST_UTILS
87 fWireframeMode = options.fWireframeMode;
88 #else
89 fWireframeMode = false;
90 #endif
91 fBufferMapThreshold = options.fBufferMapThreshold;
92 fBlacklistCoverageCounting = false;
93 fAvoidStencilBuffers = false;
94
95 fPreferVRAMUseOverFlushes = true;
96 }
97
applyOptionsOverrides(const GrContextOptions & options)98 void GrCaps::applyOptionsOverrides(const GrContextOptions& options) {
99 this->onApplyOptionsOverrides(options);
100 if (options.fDisableDriverCorrectnessWorkarounds) {
101 // We always blacklist coverage counting on Vulkan currently. TODO: Either stop doing that
102 // or disambiguate blacklisting from incomplete implementation.
103 // SkASSERT(!fBlacklistCoverageCounting);
104 SkASSERT(!fAvoidStencilBuffers);
105 SkASSERT(!fAdvBlendEqBlacklist);
106 }
107
108 fMaxTextureSize = SkTMin(fMaxTextureSize, options.fMaxTextureSizeOverride);
109 fMaxTileSize = fMaxTextureSize;
110 #if GR_TEST_UTILS
111 // If the max tile override is zero, it means we should use the max texture size.
112 if (options.fMaxTileSizeOverride && options.fMaxTileSizeOverride < fMaxTextureSize) {
113 fMaxTileSize = options.fMaxTileSizeOverride;
114 }
115 if (options.fSuppressGeometryShaders) {
116 fShaderCaps->fGeometryShaderSupport = false;
117 }
118 #endif
119 if (fMaxWindowRectangles > GrWindowRectangles::kMaxWindows) {
120 SkDebugf("WARNING: capping window rectangles at %i. HW advertises support for %i.\n",
121 GrWindowRectangles::kMaxWindows, fMaxWindowRectangles);
122 fMaxWindowRectangles = GrWindowRectangles::kMaxWindows;
123 }
124 fAvoidStencilBuffers = options.fAvoidStencilBuffers;
125 }
126
map_flags_to_string(uint32_t flags)127 static SkString map_flags_to_string(uint32_t flags) {
128 SkString str;
129 if (GrCaps::kNone_MapFlags == flags) {
130 str = "none";
131 } else {
132 SkASSERT(GrCaps::kCanMap_MapFlag & flags);
133 SkDEBUGCODE(flags &= ~GrCaps::kCanMap_MapFlag);
134 str = "can_map";
135
136 if (GrCaps::kSubset_MapFlag & flags) {
137 str.append(" partial");
138 } else {
139 str.append(" full");
140 }
141 SkDEBUGCODE(flags &= ~GrCaps::kSubset_MapFlag);
142 }
143 SkASSERT(0 == flags); // Make sure we handled all the flags.
144 return str;
145 }
146
dumpJSON(SkJSONWriter * writer) const147 void GrCaps::dumpJSON(SkJSONWriter* writer) const {
148 writer->beginObject();
149
150 writer->appendBool("MIP Map Support", fMipMapSupport);
151 writer->appendBool("NPOT Texture Tile Support", fNPOTTextureTileSupport);
152 writer->appendBool("sRGB Support", fSRGBSupport);
153 writer->appendBool("sRGB Write Control", fSRGBWriteControl);
154 writer->appendBool("sRGB Decode Disable", fSRGBDecodeDisableSupport);
155 writer->appendBool("Discard Render Target Support", fDiscardRenderTargetSupport);
156 writer->appendBool("Reuse Scratch Textures", fReuseScratchTextures);
157 writer->appendBool("Reuse Scratch Buffers", fReuseScratchBuffers);
158 writer->appendBool("Gpu Tracing Support", fGpuTracingSupport);
159 writer->appendBool("Oversized Stencil Support", fOversizedStencilSupport);
160 writer->appendBool("Texture Barrier Support", fTextureBarrierSupport);
161 writer->appendBool("Sample Locations Support", fSampleLocationsSupport);
162 writer->appendBool("Multisample disable support", fMultisampleDisableSupport);
163 writer->appendBool("Instance Attrib Support", fInstanceAttribSupport);
164 writer->appendBool("Uses Mixed Samples", fUsesMixedSamples);
165 writer->appendBool("Use primitive restart", fUsePrimitiveRestart);
166 writer->appendBool("Prefer client-side dynamic buffers", fPreferClientSideDynamicBuffers);
167 writer->appendBool("Prefer fullscreen clears", fPreferFullscreenClears);
168 writer->appendBool("Must clear buffer memory", fMustClearUploadedBufferData);
169 writer->appendBool("Sample shading support", fSampleShadingSupport);
170 writer->appendBool("Fence sync support", fFenceSyncSupport);
171 writer->appendBool("Cross context texture support", fCrossContextTextureSupport);
172
173 writer->appendBool("Blacklist Coverage Counting Path Renderer [workaround]",
174 fBlacklistCoverageCounting);
175 writer->appendBool("Prefer VRAM Use over flushes [workaround]", fPreferVRAMUseOverFlushes);
176 writer->appendBool("Avoid stencil buffers [workaround]", fAvoidStencilBuffers);
177
178 if (this->advancedBlendEquationSupport()) {
179 writer->appendHexU32("Advanced Blend Equation Blacklist", fAdvBlendEqBlacklist);
180 }
181
182 writer->appendS32("Max Vertex Attributes", fMaxVertexAttributes);
183 writer->appendS32("Max Texture Size", fMaxTextureSize);
184 writer->appendS32("Max Render Target Size", fMaxRenderTargetSize);
185 writer->appendS32("Max Preferred Render Target Size", fMaxPreferredRenderTargetSize);
186 writer->appendS32("Max Raster Samples", fMaxRasterSamples);
187 writer->appendS32("Max Window Rectangles", fMaxWindowRectangles);
188 writer->appendS32("Max Clip Analytic Fragment Processors", fMaxClipAnalyticFPs);
189
190 static const char* kBlendEquationSupportNames[] = {
191 "Basic",
192 "Advanced",
193 "Advanced Coherent",
194 };
195 GR_STATIC_ASSERT(0 == kBasic_BlendEquationSupport);
196 GR_STATIC_ASSERT(1 == kAdvanced_BlendEquationSupport);
197 GR_STATIC_ASSERT(2 == kAdvancedCoherent_BlendEquationSupport);
198 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kBlendEquationSupportNames) == kLast_BlendEquationSupport + 1);
199
200 writer->appendString("Blend Equation Support",
201 kBlendEquationSupportNames[fBlendEquationSupport]);
202 writer->appendString("Map Buffer Support", map_flags_to_string(fMapBufferFlags).c_str());
203
204 SkASSERT(!this->isConfigRenderable(kUnknown_GrPixelConfig));
205 SkASSERT(!this->isConfigTexturable(kUnknown_GrPixelConfig));
206
207 writer->beginArray("configs");
208
209 for (size_t i = 1; i < kGrPixelConfigCnt; ++i) {
210 GrPixelConfig config = static_cast<GrPixelConfig>(i);
211 writer->beginObject(nullptr, false);
212 writer->appendString("name", pixel_config_name(config));
213 writer->appendS32("max sample count", this->maxRenderTargetSampleCount(config));
214 writer->appendBool("texturable", this->isConfigTexturable(config));
215 writer->endObject();
216 }
217
218 writer->endArray();
219
220 this->onDumpJSON(writer);
221
222 writer->appendName("shaderCaps");
223 this->shaderCaps()->dumpJSON(writer);
224
225 writer->endObject();
226 }
227
validateSurfaceDesc(const GrSurfaceDesc & desc,GrMipMapped mipped) const228 bool GrCaps::validateSurfaceDesc(const GrSurfaceDesc& desc, GrMipMapped mipped) const {
229 if (!this->isConfigTexturable(desc.fConfig)) {
230 return false;
231 }
232
233 if (GrMipMapped::kYes == mipped && !this->mipMapSupport()) {
234 return false;
235 }
236
237 if (desc.fWidth < 1 || desc.fHeight < 1) {
238 return false;
239 }
240
241 if (SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag)) {
242 if (0 == this->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig)) {
243 return false;
244 }
245 int maxRTSize = this->maxRenderTargetSize();
246 if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) {
247 return false;
248 }
249 } else {
250 // We currently do not support multisampled textures
251 if (desc.fSampleCnt > 1) {
252 return false;
253 }
254 int maxSize = this->maxTextureSize();
255 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
256 return false;
257 }
258 }
259
260 return true;
261 }
262