GrCaps.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. #include "include/gpu/GrBackendSurface.h"
  8. #include "include/gpu/GrContextOptions.h"
  9. #include "include/gpu/GrSurface.h"
  10. #include "include/private/GrTypesPriv.h"
  11. #include "src/gpu/GrCaps.h"
  12. #include "src/gpu/GrSurfaceProxy.h"
  13. #include "src/gpu/GrWindowRectangles.h"
  14. #include "src/utils/SkJSONWriter.h"
  15. GrCaps::GrCaps(const GrContextOptions& options) {
  16. fMipMapSupport = false;
  17. fNPOTTextureTileSupport = false;
  18. fSRGBSupport = false;
  19. fSRGBWriteControl = false;
  20. fReuseScratchTextures = true;
  21. fReuseScratchBuffers = true;
  22. fGpuTracingSupport = false;
  23. fOversizedStencilSupport = false;
  24. fTextureBarrierSupport = false;
  25. fSampleLocationsSupport = false;
  26. fMultisampleDisableSupport = false;
  27. fInstanceAttribSupport = false;
  28. fMixedSamplesSupport = false;
  29. fUsePrimitiveRestart = false;
  30. fPreferClientSideDynamicBuffers = false;
  31. fPreferFullscreenClears = false;
  32. fMustClearUploadedBufferData = false;
  33. fShouldInitializeTextures = false;
  34. fSupportsAHardwareBufferImages = false;
  35. fFenceSyncSupport = false;
  36. fSemaphoreSupport = false;
  37. fCrossContextTextureSupport = false;
  38. fHalfFloatVertexAttributeSupport = false;
  39. fDynamicStateArrayGeometryProcessorTextureSupport = false;
  40. fPerformPartialClearsAsDraws = false;
  41. fPerformColorClearsAsDraws = false;
  42. fPerformStencilClearsAsDraws = false;
  43. fAllowCoverageCounting = false;
  44. fTransferBufferSupport = false;
  45. fWritePixelsRowBytesSupport = false;
  46. fReadPixelsRowBytesSupport = false;
  47. fDriverBlacklistCCPR = false;
  48. fDriverBlacklistMSAACCPR = false;
  49. fBlendEquationSupport = kBasic_BlendEquationSupport;
  50. fAdvBlendEqBlacklist = 0;
  51. fMapBufferFlags = kNone_MapFlags;
  52. fMaxVertexAttributes = 0;
  53. fMaxRenderTargetSize = 1;
  54. fMaxPreferredRenderTargetSize = 1;
  55. fMaxTextureSize = 1;
  56. fMaxWindowRectangles = 0;
  57. fInternalMultisampleCount = 0;
  58. fSuppressPrints = options.fSuppressPrints;
  59. #if GR_TEST_UTILS
  60. fWireframeMode = options.fWireframeMode;
  61. #else
  62. fWireframeMode = false;
  63. #endif
  64. fBufferMapThreshold = options.fBufferMapThreshold;
  65. fAvoidStencilBuffers = false;
  66. fAvoidWritePixelsFastPath = false;
  67. fPreferVRAMUseOverFlushes = true;
  68. fPreferTrianglesOverSampleMask = false;
  69. // Default to true, allow older versions of OpenGL to disable explicitly
  70. fClampToBorderSupport = true;
  71. fDriverBugWorkarounds = options.fDriverBugWorkarounds;
  72. }
  73. void GrCaps::applyOptionsOverrides(const GrContextOptions& options) {
  74. this->onApplyOptionsOverrides(options);
  75. if (options.fDisableDriverCorrectnessWorkarounds) {
  76. SkASSERT(!fDriverBlacklistCCPR);
  77. SkASSERT(!fDriverBlacklistMSAACCPR);
  78. SkASSERT(!fAvoidStencilBuffers);
  79. SkASSERT(!fAdvBlendEqBlacklist);
  80. SkASSERT(!fPerformColorClearsAsDraws);
  81. SkASSERT(!fPerformStencilClearsAsDraws);
  82. // Don't check the partial-clear workaround, since that is a backend limitation, not a
  83. // driver workaround (it just so happens the fallbacks are the same).
  84. }
  85. if (GrContextOptions::Enable::kNo == options.fUseDrawInsteadOfClear) {
  86. fPerformColorClearsAsDraws = false;
  87. fPerformStencilClearsAsDraws = false;
  88. } else if (GrContextOptions::Enable::kYes == options.fUseDrawInsteadOfClear) {
  89. fPerformColorClearsAsDraws = true;
  90. fPerformStencilClearsAsDraws = true;
  91. }
  92. fAllowCoverageCounting = !options.fDisableCoverageCountingPaths;
  93. fMaxTextureSize = SkTMin(fMaxTextureSize, options.fMaxTextureSizeOverride);
  94. fMaxTileSize = fMaxTextureSize;
  95. #if GR_TEST_UTILS
  96. // If the max tile override is zero, it means we should use the max texture size.
  97. if (options.fMaxTileSizeOverride && options.fMaxTileSizeOverride < fMaxTextureSize) {
  98. fMaxTileSize = options.fMaxTileSizeOverride;
  99. }
  100. if (options.fSuppressGeometryShaders) {
  101. fShaderCaps->fGeometryShaderSupport = false;
  102. }
  103. if (options.fClearAllTextures) {
  104. fShouldInitializeTextures = true;
  105. }
  106. #endif
  107. if (fMaxWindowRectangles > GrWindowRectangles::kMaxWindows) {
  108. SkDebugf("WARNING: capping window rectangles at %i. HW advertises support for %i.\n",
  109. GrWindowRectangles::kMaxWindows, fMaxWindowRectangles);
  110. fMaxWindowRectangles = GrWindowRectangles::kMaxWindows;
  111. }
  112. fInternalMultisampleCount = options.fInternalMultisampleCount;
  113. fAvoidStencilBuffers = options.fAvoidStencilBuffers;
  114. fDriverBugWorkarounds.applyOverrides(options.fDriverBugWorkarounds);
  115. }
  116. #ifdef SK_ENABLE_DUMP_GPU
  117. static const char* pixel_config_name(GrPixelConfig config) {
  118. switch (config) {
  119. case kUnknown_GrPixelConfig: return "Unknown";
  120. case kAlpha_8_GrPixelConfig: return "Alpha8";
  121. case kAlpha_8_as_Alpha_GrPixelConfig: return "Alpha8_asAlpha";
  122. case kAlpha_8_as_Red_GrPixelConfig: return "Alpha8_asRed";
  123. case kGray_8_GrPixelConfig: return "Gray8";
  124. case kGray_8_as_Lum_GrPixelConfig: return "Gray8_asLum";
  125. case kGray_8_as_Red_GrPixelConfig: return "Gray8_asRed";
  126. case kRGB_565_GrPixelConfig: return "RGB565";
  127. case kRGBA_4444_GrPixelConfig: return "RGBA444";
  128. case kRGBA_8888_GrPixelConfig: return "RGBA8888";
  129. case kRGB_888_GrPixelConfig: return "RGB888";
  130. case kRGB_888X_GrPixelConfig: return "RGB888X";
  131. case kRG_88_GrPixelConfig: return "RG88";
  132. case kBGRA_8888_GrPixelConfig: return "BGRA8888";
  133. case kSRGBA_8888_GrPixelConfig: return "SRGBA8888";
  134. case kRGBA_1010102_GrPixelConfig: return "RGBA1010102";
  135. case kRGBA_float_GrPixelConfig: return "RGBAFloat";
  136. case kAlpha_half_GrPixelConfig: return "AlphaHalf";
  137. case kAlpha_half_as_Lum_GrPixelConfig: return "AlphaHalf_asLum";
  138. case kAlpha_half_as_Red_GrPixelConfig: return "AlphaHalf_asRed";
  139. case kRGBA_half_GrPixelConfig: return "RGBAHalf";
  140. case kRGBA_half_Clamped_GrPixelConfig: return "RGBAHalfClamped";
  141. case kRGB_ETC1_GrPixelConfig: return "RGBETC1";
  142. case kR_16_GrPixelConfig: return "R16";
  143. case kRG_1616_GrPixelConfig: return "RG1616";
  144. // Experimental (for Y416 and mutant P016/P010)
  145. case kRGBA_16161616_GrPixelConfig: return "RGBA16161616";
  146. case kRG_half_GrPixelConfig: return "RGHalf";
  147. }
  148. SK_ABORT("Invalid pixel config");
  149. return "<invalid>";
  150. }
  151. static SkString map_flags_to_string(uint32_t flags) {
  152. SkString str;
  153. if (GrCaps::kNone_MapFlags == flags) {
  154. str = "none";
  155. } else {
  156. SkASSERT(GrCaps::kCanMap_MapFlag & flags);
  157. SkDEBUGCODE(flags &= ~GrCaps::kCanMap_MapFlag);
  158. str = "can_map";
  159. if (GrCaps::kSubset_MapFlag & flags) {
  160. str.append(" partial");
  161. } else {
  162. str.append(" full");
  163. }
  164. SkDEBUGCODE(flags &= ~GrCaps::kSubset_MapFlag);
  165. if (GrCaps::kAsyncRead_MapFlag & flags) {
  166. str.append(" async_read");
  167. } else {
  168. str.append(" sync_read");
  169. }
  170. SkDEBUGCODE(flags &= ~GrCaps::kAsyncRead_MapFlag);
  171. }
  172. SkASSERT(0 == flags); // Make sure we handled all the flags.
  173. return str;
  174. }
  175. void GrCaps::dumpJSON(SkJSONWriter* writer) const {
  176. writer->beginObject();
  177. writer->appendBool("MIP Map Support", fMipMapSupport);
  178. writer->appendBool("NPOT Texture Tile Support", fNPOTTextureTileSupport);
  179. writer->appendBool("sRGB Support", fSRGBSupport);
  180. writer->appendBool("sRGB Write Control", fSRGBWriteControl);
  181. writer->appendBool("Reuse Scratch Textures", fReuseScratchTextures);
  182. writer->appendBool("Reuse Scratch Buffers", fReuseScratchBuffers);
  183. writer->appendBool("Gpu Tracing Support", fGpuTracingSupport);
  184. writer->appendBool("Oversized Stencil Support", fOversizedStencilSupport);
  185. writer->appendBool("Texture Barrier Support", fTextureBarrierSupport);
  186. writer->appendBool("Sample Locations Support", fSampleLocationsSupport);
  187. writer->appendBool("Multisample disable support", fMultisampleDisableSupport);
  188. writer->appendBool("Instance Attrib Support", fInstanceAttribSupport);
  189. writer->appendBool("Mixed Samples Support", fMixedSamplesSupport);
  190. writer->appendBool("Use primitive restart", fUsePrimitiveRestart);
  191. writer->appendBool("Prefer client-side dynamic buffers", fPreferClientSideDynamicBuffers);
  192. writer->appendBool("Prefer fullscreen clears", fPreferFullscreenClears);
  193. writer->appendBool("Must clear buffer memory", fMustClearUploadedBufferData);
  194. writer->appendBool("Should initialize textures", fShouldInitializeTextures);
  195. writer->appendBool("Supports importing AHardwareBuffers", fSupportsAHardwareBufferImages);
  196. writer->appendBool("Fence sync support", fFenceSyncSupport);
  197. writer->appendBool("Semaphore support", fSemaphoreSupport);
  198. writer->appendBool("Cross context texture support", fCrossContextTextureSupport);
  199. writer->appendBool("Half float vertex attribute support", fHalfFloatVertexAttributeSupport);
  200. writer->appendBool("Specify GeometryProcessor textures as a dynamic state array",
  201. fDynamicStateArrayGeometryProcessorTextureSupport);
  202. writer->appendBool("Use draws for partial clears", fPerformPartialClearsAsDraws);
  203. writer->appendBool("Use draws for color clears", fPerformColorClearsAsDraws);
  204. writer->appendBool("Use draws for stencil clip clears", fPerformStencilClearsAsDraws);
  205. writer->appendBool("Allow coverage counting shortcuts", fAllowCoverageCounting);
  206. writer->appendBool("Supports transfer buffers", fTransferBufferSupport);
  207. writer->appendBool("Write pixels row bytes support", fWritePixelsRowBytesSupport);
  208. writer->appendBool("Read pixels row bytes support", fReadPixelsRowBytesSupport);
  209. writer->appendBool("Blacklist CCPR on current driver [workaround]", fDriverBlacklistCCPR);
  210. writer->appendBool("Blacklist MSAA version of CCPR on current driver [workaround]",
  211. fDriverBlacklistMSAACCPR);
  212. writer->appendBool("Clamp-to-border", fClampToBorderSupport);
  213. writer->appendBool("Prefer VRAM Use over flushes [workaround]", fPreferVRAMUseOverFlushes);
  214. writer->appendBool("Prefer more triangles over sample mask [MSAA only]",
  215. fPreferTrianglesOverSampleMask);
  216. writer->appendBool("Avoid stencil buffers [workaround]", fAvoidStencilBuffers);
  217. if (this->advancedBlendEquationSupport()) {
  218. writer->appendHexU32("Advanced Blend Equation Blacklist", fAdvBlendEqBlacklist);
  219. }
  220. writer->appendS32("Max Vertex Attributes", fMaxVertexAttributes);
  221. writer->appendS32("Max Texture Size", fMaxTextureSize);
  222. writer->appendS32("Max Render Target Size", fMaxRenderTargetSize);
  223. writer->appendS32("Max Preferred Render Target Size", fMaxPreferredRenderTargetSize);
  224. writer->appendS32("Max Window Rectangles", fMaxWindowRectangles);
  225. writer->appendS32("Preferred Sample Count for Internal MSAA and Mixed Samples",
  226. fInternalMultisampleCount);
  227. static const char* kBlendEquationSupportNames[] = {
  228. "Basic",
  229. "Advanced",
  230. "Advanced Coherent",
  231. };
  232. GR_STATIC_ASSERT(0 == kBasic_BlendEquationSupport);
  233. GR_STATIC_ASSERT(1 == kAdvanced_BlendEquationSupport);
  234. GR_STATIC_ASSERT(2 == kAdvancedCoherent_BlendEquationSupport);
  235. GR_STATIC_ASSERT(SK_ARRAY_COUNT(kBlendEquationSupportNames) == kLast_BlendEquationSupport + 1);
  236. writer->appendString("Blend Equation Support",
  237. kBlendEquationSupportNames[fBlendEquationSupport]);
  238. writer->appendString("Map Buffer Support", map_flags_to_string(fMapBufferFlags).c_str());
  239. SkASSERT(!this->isConfigRenderable(kUnknown_GrPixelConfig));
  240. SkASSERT(!this->isConfigTexturable(kUnknown_GrPixelConfig));
  241. writer->beginArray("configs");
  242. for (size_t i = 1; i < kGrPixelConfigCnt; ++i) {
  243. GrPixelConfig config = static_cast<GrPixelConfig>(i);
  244. writer->beginObject(nullptr, false);
  245. writer->appendString("name", pixel_config_name(config));
  246. writer->appendS32("max sample count", this->maxRenderTargetSampleCount(config));
  247. writer->appendBool("texturable", this->isConfigTexturable(config));
  248. writer->endObject();
  249. }
  250. writer->endArray();
  251. this->onDumpJSON(writer);
  252. writer->appendName("shaderCaps");
  253. this->shaderCaps()->dumpJSON(writer);
  254. writer->endObject();
  255. }
  256. #else
  257. void GrCaps::dumpJSON(SkJSONWriter* writer) const { }
  258. #endif
  259. bool GrCaps::surfaceSupportsWritePixels(const GrSurface* surface) const {
  260. return surface->readOnly() ? false : this->onSurfaceSupportsWritePixels(surface);
  261. }
  262. size_t GrCaps::transferFromOffsetAlignment(GrColorType bufferColorType) const {
  263. if (!this->transferBufferSupport()) {
  264. return 0;
  265. }
  266. size_t result = this->onTransferFromOffsetAlignment(bufferColorType);
  267. if (!result) {
  268. return 0;
  269. }
  270. // It's very convenient to access 1 byte-per-channel 32 bitvRGB/RGBA color types as uint32_t.
  271. // Make those aligned reads out of the buffer even if the underlying API doesn't require it.
  272. auto componentFlags = GrColorTypeComponentFlags(bufferColorType);
  273. if ((componentFlags == kRGBA_SkColorTypeComponentFlags ||
  274. componentFlags == kRGB_SkColorTypeComponentFlags) &&
  275. GrColorTypeBytesPerPixel(bufferColorType) == 4) {
  276. switch (result & 0b11) {
  277. // offset alignment already a multiple of 4
  278. case 0: return result;
  279. // offset alignment is a multiple of 2 but not 4.
  280. case 2: return 2 * result;
  281. // offset alignment is not a multiple of 2.
  282. default: return 4 * result;
  283. }
  284. }
  285. return result;
  286. }
  287. bool GrCaps::canCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
  288. const SkIRect& srcRect, const SkIPoint& dstPoint) const {
  289. if (dst->readOnly()) {
  290. return false;
  291. }
  292. // Currently we only ever do copies where the configs are the same. This check really should be
  293. // checking if the backend formats, color types, and swizzle are compatible. Our copy always
  294. // copies exact byte to byte from src to dst so when need to check the if we do this, the dst
  295. // has the expected values stored in the right places taking the swizzle into account. For now
  296. // we can be more restrictive and just make sure the configs are the same and if we generalize
  297. // copies and swizzles more in the future this can be updated.
  298. if (this->makeConfigSpecific(dst->config(), dst->backendFormat()) !=
  299. this->makeConfigSpecific(src->config(), src->backendFormat())) {
  300. return false;
  301. }
  302. return this->onCanCopySurface(dst, src, srcRect, dstPoint);
  303. }
  304. bool GrCaps::validateSurfaceDesc(const GrSurfaceDesc& desc, GrRenderable renderable,
  305. int renderTargetSampleCnt, GrMipMapped mipped) const {
  306. if (!this->isConfigTexturable(desc.fConfig)) {
  307. return false;
  308. }
  309. if (GrMipMapped::kYes == mipped && !this->mipMapSupport()) {
  310. return false;
  311. }
  312. if (desc.fWidth < 1 || desc.fHeight < 1) {
  313. return false;
  314. }
  315. if (renderable == GrRenderable::kYes) {
  316. if (0 == this->getRenderTargetSampleCount(renderTargetSampleCnt, desc.fConfig)) {
  317. return false;
  318. }
  319. int maxRTSize = this->maxRenderTargetSize();
  320. if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) {
  321. return false;
  322. }
  323. } else {
  324. // We currently do not support multisampled textures
  325. if (renderTargetSampleCnt != 1) {
  326. return false;
  327. }
  328. int maxSize = this->maxTextureSize();
  329. if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
  330. return false;
  331. }
  332. }
  333. return true;
  334. }
  335. GrCaps::SupportedRead GrCaps::supportedReadPixelsColorType(GrColorType srcColorType,
  336. const GrBackendFormat&,
  337. GrColorType dstColorType) const {
  338. return SupportedRead{GrSwizzle::RGBA(), srcColorType};
  339. }
  340. #ifdef SK_DEBUG
  341. bool GrCaps::AreConfigsCompatible(GrPixelConfig genericConfig, GrPixelConfig specificConfig) {
  342. bool compatible = false;
  343. switch (genericConfig) {
  344. case kAlpha_8_GrPixelConfig:
  345. compatible = kAlpha_8_GrPixelConfig == specificConfig || // here bc of the mock context
  346. kAlpha_8_as_Alpha_GrPixelConfig == specificConfig ||
  347. kAlpha_8_as_Red_GrPixelConfig == specificConfig;
  348. break;
  349. case kGray_8_GrPixelConfig:
  350. compatible = kGray_8_GrPixelConfig == specificConfig || // here bc of the mock context
  351. kGray_8_as_Lum_GrPixelConfig == specificConfig ||
  352. kGray_8_as_Red_GrPixelConfig == specificConfig;
  353. break;
  354. case kAlpha_half_GrPixelConfig:
  355. compatible = kAlpha_half_GrPixelConfig == specificConfig || // bc of the mock context
  356. kAlpha_half_as_Red_GrPixelConfig == specificConfig ||
  357. kAlpha_half_as_Lum_GrPixelConfig == specificConfig;
  358. break;
  359. case kRGB_888_GrPixelConfig:
  360. compatible = kRGB_888_GrPixelConfig == specificConfig ||
  361. kRGB_888X_GrPixelConfig == specificConfig;
  362. break;
  363. case kRGBA_8888_GrPixelConfig:
  364. compatible = kRGBA_8888_GrPixelConfig == specificConfig ||
  365. kBGRA_8888_GrPixelConfig == specificConfig;
  366. break;
  367. default:
  368. compatible = genericConfig == specificConfig;
  369. break;
  370. }
  371. if (!compatible) {
  372. SkDebugf("Configs are not compatible: %d %d\n", genericConfig, specificConfig);
  373. }
  374. return compatible;
  375. }
  376. #endif