GrRecordingContext.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright 2019 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/private/GrRecordingContext.h"
  8. #include "include/gpu/GrContext.h"
  9. #include "src/gpu/GrAuditTrail.h"
  10. #include "src/gpu/GrCaps.h"
  11. #include "src/gpu/GrDrawingManager.h"
  12. #include "src/gpu/GrMemoryPool.h"
  13. #include "src/gpu/GrProxyProvider.h"
  14. #include "src/gpu/GrRecordingContextPriv.h"
  15. #include "src/gpu/GrRenderTargetContext.h"
  16. #include "src/gpu/GrSkSLFPFactoryCache.h"
  17. #include "src/gpu/GrTextureContext.h"
  18. #include "src/gpu/SkGr.h"
  19. #include "src/gpu/text/GrTextBlobCache.h"
  20. #define ASSERT_SINGLE_OWNER_PRIV \
  21. SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
  22. GrRecordingContext::GrRecordingContext(GrBackendApi backend,
  23. const GrContextOptions& options,
  24. uint32_t contextID)
  25. : INHERITED(backend, options, contextID)
  26. , fAuditTrail(new GrAuditTrail()) {
  27. }
  28. GrRecordingContext::~GrRecordingContext() { }
  29. /**
  30. * TODO: move textblob draw calls below context (see comment below)
  31. */
  32. static void textblobcache_overbudget_CB(void* data) {
  33. SkASSERT(data);
  34. GrRecordingContext* context = reinterpret_cast<GrRecordingContext*>(data);
  35. GrContext* direct = context->priv().asDirectContext();
  36. if (!direct) {
  37. return;
  38. }
  39. // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
  40. // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
  41. // to below the GrContext level, but this is not trivial because they call drawPath on
  42. // SkGpuDevice.
  43. direct->flush();
  44. }
  45. bool GrRecordingContext::init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> cache) {
  46. if (!INHERITED::init(std::move(caps), std::move(cache))) {
  47. return false;
  48. }
  49. fStrikeCache.reset(new GrStrikeCache(this->caps(),
  50. this->options().fGlyphCacheTextureMaximumBytes));
  51. fTextBlobCache.reset(new GrTextBlobCache(textblobcache_overbudget_CB, this,
  52. this->contextID()));
  53. return true;
  54. }
  55. void GrRecordingContext::setupDrawingManager(bool sortOpLists, bool reduceOpListSplitting) {
  56. GrPathRendererChain::Options prcOptions;
  57. prcOptions.fAllowPathMaskCaching = this->options().fAllowPathMaskCaching;
  58. #if GR_TEST_UTILS
  59. prcOptions.fGpuPathRenderers = this->options().fGpuPathRenderers;
  60. #endif
  61. // FIXME: Once this is removed from Chrome and Android, rename to fEnable"".
  62. if (!this->options().fDisableCoverageCountingPaths) {
  63. prcOptions.fGpuPathRenderers |= GpuPathRenderers::kCoverageCounting;
  64. }
  65. if (this->options().fDisableDistanceFieldPaths) {
  66. prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
  67. }
  68. if (!this->proxyProvider()->renderingDirectly()) {
  69. // DDL TODO: remove this crippling of the path renderer chain
  70. // Disable the small path renderer bc of the proxies in the atlas. They need to be
  71. // unified when the opLists are added back to the destination drawing manager.
  72. prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
  73. }
  74. GrTextContext::Options textContextOptions;
  75. textContextOptions.fMaxDistanceFieldFontSize = this->options().fGlyphsAsPathsFontSize;
  76. textContextOptions.fMinDistanceFieldFontSize = this->options().fMinDistanceFieldFontSize;
  77. textContextOptions.fDistanceFieldVerticesAlwaysHaveW = false;
  78. #if SK_SUPPORT_ATLAS_TEXT
  79. if (GrContextOptions::Enable::kYes == this->options().fDistanceFieldGlyphVerticesAlwaysHaveW) {
  80. textContextOptions.fDistanceFieldVerticesAlwaysHaveW = true;
  81. }
  82. #endif
  83. fDrawingManager.reset(new GrDrawingManager(this,
  84. prcOptions,
  85. textContextOptions,
  86. sortOpLists,
  87. reduceOpListSplitting));
  88. }
  89. void GrRecordingContext::abandonContext() {
  90. INHERITED::abandonContext();
  91. fStrikeCache->freeAll();
  92. fTextBlobCache->freeAll();
  93. }
  94. GrDrawingManager* GrRecordingContext::drawingManager() {
  95. return fDrawingManager.get();
  96. }
  97. sk_sp<GrOpMemoryPool> GrRecordingContext::refOpMemoryPool() {
  98. if (!fOpMemoryPool) {
  99. // DDL TODO: should the size of the memory pool be decreased in DDL mode? CPU-side memory
  100. // consumed in DDL mode vs. normal mode for a single skp might be a good metric of wasted
  101. // memory.
  102. fOpMemoryPool = sk_sp<GrOpMemoryPool>(new GrOpMemoryPool(16384, 16384));
  103. }
  104. SkASSERT(fOpMemoryPool);
  105. return fOpMemoryPool;
  106. }
  107. GrOpMemoryPool* GrRecordingContext::opMemoryPool() {
  108. return this->refOpMemoryPool().get();
  109. }
  110. GrTextBlobCache* GrRecordingContext::getTextBlobCache() {
  111. return fTextBlobCache.get();
  112. }
  113. const GrTextBlobCache* GrRecordingContext::getTextBlobCache() const {
  114. return fTextBlobCache.get();
  115. }
  116. void GrRecordingContext::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
  117. this->drawingManager()->addOnFlushCallbackObject(onFlushCBObject);
  118. }
  119. sk_sp<GrSurfaceContext> GrRecordingContext::makeWrappedSurfaceContext(
  120. sk_sp<GrSurfaceProxy> proxy,
  121. GrColorType colorType,
  122. SkAlphaType alphaType,
  123. sk_sp<SkColorSpace> colorSpace,
  124. const SkSurfaceProps* props) {
  125. ASSERT_SINGLE_OWNER_PRIV
  126. SkASSERT(proxy);
  127. if (proxy->asRenderTargetProxy()) {
  128. SkASSERT(kPremul_SkAlphaType == alphaType || kOpaque_SkAlphaType == alphaType);
  129. return this->drawingManager()->makeRenderTargetContext(std::move(proxy), colorType,
  130. std::move(colorSpace), props);
  131. } else {
  132. SkASSERT(proxy->asTextureProxy());
  133. SkASSERT(!props);
  134. return this->drawingManager()->makeTextureContext(std::move(proxy), colorType, alphaType,
  135. std::move(colorSpace));
  136. }
  137. }
  138. sk_sp<GrTextureContext> GrRecordingContext::makeDeferredTextureContext(
  139. SkBackingFit fit,
  140. int width,
  141. int height,
  142. GrColorType colorType,
  143. SkAlphaType alphaType,
  144. sk_sp<SkColorSpace> colorSpace,
  145. GrMipMapped mipMapped,
  146. GrSurfaceOrigin origin,
  147. SkBudgeted budgeted,
  148. GrProtected isProtected) {
  149. auto format = this->caps()->getBackendFormatFromColorType(colorType);
  150. if (!format.isValid()) {
  151. return nullptr;
  152. }
  153. auto config = this->caps()->getConfigFromBackendFormat(format, colorType);
  154. if (config == kUnknown_GrPixelConfig) {
  155. return nullptr;
  156. }
  157. GrSurfaceDesc desc;
  158. desc.fWidth = width;
  159. desc.fHeight = height;
  160. desc.fConfig = config;
  161. sk_sp<GrTextureProxy> texture;
  162. if (GrMipMapped::kNo == mipMapped) {
  163. texture = this->proxyProvider()->createProxy(format, desc, GrRenderable::kNo, 1, origin,
  164. fit, budgeted, isProtected);
  165. } else {
  166. texture = this->proxyProvider()->createMipMapProxy(format, desc, GrRenderable::kNo, 1,
  167. origin, budgeted, isProtected);
  168. }
  169. if (!texture) {
  170. return nullptr;
  171. }
  172. auto drawingManager = this->drawingManager();
  173. return drawingManager->makeTextureContext(std::move(texture), colorType, alphaType,
  174. std::move(colorSpace));
  175. }
  176. sk_sp<GrRenderTargetContext> GrRecordingContext::makeDeferredRenderTargetContext(
  177. SkBackingFit fit,
  178. int width,
  179. int height,
  180. GrColorType colorType,
  181. sk_sp<SkColorSpace> colorSpace,
  182. int sampleCnt,
  183. GrMipMapped mipMapped,
  184. GrSurfaceOrigin origin,
  185. const SkSurfaceProps* surfaceProps,
  186. SkBudgeted budgeted,
  187. GrProtected isProtected) {
  188. SkASSERT(sampleCnt > 0);
  189. if (this->abandoned()) {
  190. return nullptr;
  191. }
  192. auto format = this->caps()->getBackendFormatFromColorType(colorType);
  193. if (!format.isValid()) {
  194. return nullptr;
  195. }
  196. auto config = this->caps()->getConfigFromBackendFormat(format, colorType);
  197. if (config == kUnknown_GrPixelConfig) {
  198. return nullptr;
  199. }
  200. GrSurfaceDesc desc;
  201. desc.fWidth = width;
  202. desc.fHeight = height;
  203. desc.fConfig = config;
  204. sk_sp<GrTextureProxy> rtp;
  205. if (GrMipMapped::kNo == mipMapped) {
  206. rtp = this->proxyProvider()->createProxy(format, desc, GrRenderable::kYes, sampleCnt,
  207. origin, fit, budgeted, isProtected);
  208. } else {
  209. rtp = this->proxyProvider()->createMipMapProxy(format, desc, GrRenderable::kYes, sampleCnt,
  210. origin, budgeted, isProtected);
  211. }
  212. if (!rtp) {
  213. return nullptr;
  214. }
  215. auto drawingManager = this->drawingManager();
  216. sk_sp<GrRenderTargetContext> renderTargetContext = drawingManager->makeRenderTargetContext(
  217. std::move(rtp), colorType, std::move(colorSpace), surfaceProps);
  218. if (!renderTargetContext) {
  219. return nullptr;
  220. }
  221. renderTargetContext->discard();
  222. return renderTargetContext;
  223. }
  224. static inline GrColorType color_type_fallback(GrColorType ct) {
  225. switch (ct) {
  226. // kRGBA_8888 is our default fallback for many color types that may not have renderable
  227. // backend formats.
  228. case GrColorType::kAlpha_8:
  229. case GrColorType::kBGR_565:
  230. case GrColorType::kABGR_4444:
  231. case GrColorType::kBGRA_8888:
  232. case GrColorType::kRGBA_1010102:
  233. case GrColorType::kRGBA_F16:
  234. case GrColorType::kRGBA_F16_Clamped:
  235. return GrColorType::kRGBA_8888;
  236. case GrColorType::kAlpha_F16:
  237. return GrColorType::kRGBA_F16;
  238. case GrColorType::kGray_8:
  239. return GrColorType::kRGB_888x;
  240. default:
  241. return GrColorType::kUnknown;
  242. }
  243. }
  244. sk_sp<GrRenderTargetContext> GrRecordingContext::makeDeferredRenderTargetContextWithFallback(
  245. SkBackingFit fit,
  246. int width,
  247. int height,
  248. GrColorType colorType,
  249. sk_sp<SkColorSpace> colorSpace,
  250. int sampleCnt,
  251. GrMipMapped mipMapped,
  252. GrSurfaceOrigin origin,
  253. const SkSurfaceProps* surfaceProps,
  254. SkBudgeted budgeted,
  255. GrProtected isProtected) {
  256. SkASSERT(sampleCnt > 0);
  257. sk_sp<GrRenderTargetContext> rtc;
  258. do {
  259. rtc = this->makeDeferredRenderTargetContext(fit, width, height, colorType, colorSpace,
  260. sampleCnt, mipMapped, origin, surfaceProps,
  261. budgeted, isProtected);
  262. colorType = color_type_fallback(colorType);
  263. } while (!rtc && colorType != GrColorType::kUnknown);
  264. return rtc;
  265. }
  266. ///////////////////////////////////////////////////////////////////////////////////////////////////
  267. sk_sp<const GrCaps> GrRecordingContextPriv::refCaps() const {
  268. return fContext->refCaps();
  269. }
  270. sk_sp<GrSkSLFPFactoryCache> GrRecordingContextPriv::fpFactoryCache() {
  271. return fContext->fpFactoryCache();
  272. }
  273. sk_sp<GrOpMemoryPool> GrRecordingContextPriv::refOpMemoryPool() {
  274. return fContext->refOpMemoryPool();
  275. }
  276. void GrRecordingContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
  277. fContext->addOnFlushCallbackObject(onFlushCBObject);
  278. }
  279. sk_sp<GrSurfaceContext> GrRecordingContextPriv::makeWrappedSurfaceContext(
  280. sk_sp<GrSurfaceProxy> proxy,
  281. GrColorType colorType,
  282. SkAlphaType alphaType,
  283. sk_sp<SkColorSpace> colorSpace,
  284. const SkSurfaceProps* props) {
  285. return fContext->makeWrappedSurfaceContext(std::move(proxy), colorType, alphaType,
  286. std::move(colorSpace), props);
  287. }
  288. sk_sp<GrTextureContext> GrRecordingContextPriv::makeDeferredTextureContext(
  289. SkBackingFit fit,
  290. int width,
  291. int height,
  292. GrColorType colorType,
  293. SkAlphaType alphaType,
  294. sk_sp<SkColorSpace> colorSpace,
  295. GrMipMapped mipMapped,
  296. GrSurfaceOrigin origin,
  297. SkBudgeted budgeted,
  298. GrProtected isProtected) {
  299. return fContext->makeDeferredTextureContext(fit, width, height, colorType, alphaType,
  300. std::move(colorSpace), mipMapped, origin, budgeted,
  301. isProtected);
  302. }
  303. sk_sp<GrRenderTargetContext> GrRecordingContextPriv::makeDeferredRenderTargetContext(
  304. SkBackingFit fit,
  305. int width,
  306. int height,
  307. GrColorType colorType,
  308. sk_sp<SkColorSpace> colorSpace,
  309. int sampleCnt,
  310. GrMipMapped mipMapped,
  311. GrSurfaceOrigin origin,
  312. const SkSurfaceProps* surfaceProps,
  313. SkBudgeted budgeted,
  314. GrProtected isProtected) {
  315. return fContext->makeDeferredRenderTargetContext(fit, width, height, colorType,
  316. std::move(colorSpace), sampleCnt, mipMapped,
  317. origin, surfaceProps, budgeted, isProtected);
  318. }
  319. sk_sp<GrRenderTargetContext> GrRecordingContextPriv::makeDeferredRenderTargetContextWithFallback(
  320. SkBackingFit fit,
  321. int width,
  322. int height,
  323. GrColorType colorType,
  324. sk_sp<SkColorSpace> colorSpace,
  325. int sampleCnt,
  326. GrMipMapped mipMapped,
  327. GrSurfaceOrigin origin,
  328. const SkSurfaceProps* surfaceProps,
  329. SkBudgeted budgeted,
  330. GrProtected isProtected) {
  331. return fContext->makeDeferredRenderTargetContextWithFallback(fit,
  332. width,
  333. height,
  334. colorType,
  335. std::move(colorSpace),
  336. sampleCnt,
  337. mipMapped,
  338. origin,
  339. surfaceProps,
  340. budgeted,
  341. isProtected);
  342. }
  343. GrContext* GrRecordingContextPriv::backdoor() {
  344. return (GrContext*) fContext;
  345. }