blob: cac14923564232aabec16250bf11788bb9eb6252 [file] [log] [blame]
Robert Phillips4217ea72019-01-30 13:08:28 -05001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/private/GrRecordingContext.h"
Robert Phillips4217ea72019-01-30 13:08:28 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040011#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrCaps.h"
13#include "src/gpu/GrDrawingManager.h"
14#include "src/gpu/GrMemoryPool.h"
15#include "src/gpu/GrProxyProvider.h"
16#include "src/gpu/GrRecordingContextPriv.h"
17#include "src/gpu/GrRenderTargetContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040018#include "src/gpu/GrSkSLFPFactoryCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrTextureContext.h"
20#include "src/gpu/SkGr.h"
21#include "src/gpu/text/GrTextBlobCache.h"
Robert Phillipsa41c6852019-02-07 10:44:10 -050022
Robert Phillips292a6b22019-02-14 14:49:02 -050023#define ASSERT_SINGLE_OWNER_PRIV \
24 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
25
Robert Phillipsc1541ae2019-02-04 12:05:37 -050026GrRecordingContext::GrRecordingContext(GrBackendApi backend,
27 const GrContextOptions& options,
Robert Phillipsa41c6852019-02-07 10:44:10 -050028 uint32_t contextID)
Greg Danielf91aeb22019-06-18 09:58:02 -040029 : INHERITED(backend, options, contextID)
30 , fAuditTrail(new GrAuditTrail()) {
Robert Phillips4217ea72019-01-30 13:08:28 -050031}
32
33GrRecordingContext::~GrRecordingContext() { }
34
Robert Phillips2184fb72019-02-21 16:11:41 -050035/**
36 * TODO: move textblob draw calls below context (see comment below)
37 */
38static void textblobcache_overbudget_CB(void* data) {
39 SkASSERT(data);
40 GrRecordingContext* context = reinterpret_cast<GrRecordingContext*>(data);
41
42 GrContext* direct = context->priv().asDirectContext();
43 if (!direct) {
44 return;
45 }
46
47 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
48 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
49 // to below the GrContext level, but this is not trivial because they call drawPath on
50 // SkGpuDevice.
51 direct->flush();
52}
53
Robert Phillips292a6b22019-02-14 14:49:02 -050054bool GrRecordingContext::init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> cache) {
55
56 if (!INHERITED::init(std::move(caps), std::move(cache))) {
57 return false;
58 }
59
Herb Derbya00da612019-03-04 17:10:01 -050060 fStrikeCache.reset(new GrStrikeCache(this->caps(),
Robert Phillips2184fb72019-02-21 16:11:41 -050061 this->options().fGlyphCacheTextureMaximumBytes));
62
63 fTextBlobCache.reset(new GrTextBlobCache(textblobcache_overbudget_CB, this,
64 this->contextID()));
65
Robert Phillips56181ba2019-03-08 12:00:45 -050066 return true;
67}
68
Robert Phillips6db27c22019-05-01 10:43:56 -040069void GrRecordingContext::setupDrawingManager(bool sortOpLists, bool reduceOpListSplitting) {
Robert Phillips69893702019-02-22 11:16:30 -050070 GrPathRendererChain::Options prcOptions;
71 prcOptions.fAllowPathMaskCaching = this->options().fAllowPathMaskCaching;
72#if GR_TEST_UTILS
73 prcOptions.fGpuPathRenderers = this->options().fGpuPathRenderers;
74#endif
Chris Daltonf7748182019-03-18 16:22:30 +000075 // FIXME: Once this is removed from Chrome and Android, rename to fEnable"".
76 if (!this->options().fDisableCoverageCountingPaths) {
77 prcOptions.fGpuPathRenderers |= GpuPathRenderers::kCoverageCounting;
Robert Phillips69893702019-02-22 11:16:30 -050078 }
79 if (this->options().fDisableDistanceFieldPaths) {
80 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
81 }
82
83 if (!this->proxyProvider()->renderingDirectly()) {
84 // DDL TODO: remove this crippling of the path renderer chain
85 // Disable the small path renderer bc of the proxies in the atlas. They need to be
86 // unified when the opLists are added back to the destination drawing manager.
87 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
88 }
89
90 GrTextContext::Options textContextOptions;
91 textContextOptions.fMaxDistanceFieldFontSize = this->options().fGlyphsAsPathsFontSize;
92 textContextOptions.fMinDistanceFieldFontSize = this->options().fMinDistanceFieldFontSize;
93 textContextOptions.fDistanceFieldVerticesAlwaysHaveW = false;
94#if SK_SUPPORT_ATLAS_TEXT
95 if (GrContextOptions::Enable::kYes == this->options().fDistanceFieldGlyphVerticesAlwaysHaveW) {
96 textContextOptions.fDistanceFieldVerticesAlwaysHaveW = true;
97 }
98#endif
99
100 fDrawingManager.reset(new GrDrawingManager(this,
Robert Phillips12c46292019-04-23 07:36:17 -0400101 prcOptions,
102 textContextOptions,
103 sortOpLists,
Robert Phillips6db27c22019-05-01 10:43:56 -0400104 reduceOpListSplitting));
Robert Phillips292a6b22019-02-14 14:49:02 -0500105}
106
Robert Phillipsa9162df2019-02-11 14:12:03 -0500107void GrRecordingContext::abandonContext() {
108 INHERITED::abandonContext();
Robert Phillips2184fb72019-02-21 16:11:41 -0500109
Herb Derbya00da612019-03-04 17:10:01 -0500110 fStrikeCache->freeAll();
Robert Phillips2184fb72019-02-21 16:11:41 -0500111 fTextBlobCache->freeAll();
Robert Phillipsa9162df2019-02-11 14:12:03 -0500112}
113
Robert Phillips69893702019-02-22 11:16:30 -0500114GrDrawingManager* GrRecordingContext::drawingManager() {
115 return fDrawingManager.get();
116}
117
Robert Phillipsd6841482019-02-08 10:29:20 -0500118sk_sp<GrOpMemoryPool> GrRecordingContext::refOpMemoryPool() {
119 if (!fOpMemoryPool) {
120 // DDL TODO: should the size of the memory pool be decreased in DDL mode? CPU-side memory
121 // consumed in DDL mode vs. normal mode for a single skp might be a good metric of wasted
122 // memory.
123 fOpMemoryPool = sk_sp<GrOpMemoryPool>(new GrOpMemoryPool(16384, 16384));
124 }
125
126 SkASSERT(fOpMemoryPool);
127 return fOpMemoryPool;
128}
129
130GrOpMemoryPool* GrRecordingContext::opMemoryPool() {
131 return this->refOpMemoryPool().get();
132}
133
Robert Phillips2184fb72019-02-21 16:11:41 -0500134GrTextBlobCache* GrRecordingContext::getTextBlobCache() {
135 return fTextBlobCache.get();
136}
137
138const GrTextBlobCache* GrRecordingContext::getTextBlobCache() const {
139 return fTextBlobCache.get();
140}
141
Robert Phillipsc5058a62019-02-15 12:52:59 -0500142void GrRecordingContext::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
143 this->drawingManager()->addOnFlushCallbackObject(onFlushCBObject);
144}
145
Robert Phillips292a6b22019-02-14 14:49:02 -0500146sk_sp<GrSurfaceContext> GrRecordingContext::makeWrappedSurfaceContext(
Brian Salomone7499c72019-06-24 12:12:36 -0400147 sk_sp<GrSurfaceProxy> proxy,
Brian Salomond6287472019-06-24 15:50:07 -0400148 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -0400149 SkAlphaType alphaType,
150 sk_sp<SkColorSpace> colorSpace,
151 const SkSurfaceProps* props) {
Robert Phillips292a6b22019-02-14 14:49:02 -0500152 ASSERT_SINGLE_OWNER_PRIV
153
154 if (proxy->asRenderTargetProxy()) {
Brian Salomone7499c72019-06-24 12:12:36 -0400155 SkASSERT(kPremul_SkAlphaType == alphaType || kOpaque_SkAlphaType == alphaType);
Brian Salomond6287472019-06-24 15:50:07 -0400156 return this->drawingManager()->makeRenderTargetContext(std::move(proxy), colorType,
Robert Phillips292a6b22019-02-14 14:49:02 -0500157 std::move(colorSpace), props);
158 } else {
159 SkASSERT(proxy->asTextureProxy());
160 SkASSERT(!props);
Brian Salomond6287472019-06-24 15:50:07 -0400161 return this->drawingManager()->makeTextureContext(std::move(proxy), colorType, alphaType,
Brian Salomone7499c72019-06-24 12:12:36 -0400162 std::move(colorSpace));
Robert Phillips292a6b22019-02-14 14:49:02 -0500163 }
164}
165
Brian Salomon947efe22019-07-16 15:36:11 -0400166sk_sp<GrTextureContext> GrRecordingContext::makeDeferredTextureContext(
Brian Salomone7499c72019-06-24 12:12:36 -0400167 SkBackingFit fit,
Brian Salomon947efe22019-07-16 15:36:11 -0400168 int width,
169 int height,
Brian Salomond6287472019-06-24 15:50:07 -0400170 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -0400171 SkAlphaType alphaType,
172 sk_sp<SkColorSpace> colorSpace,
Brian Salomon947efe22019-07-16 15:36:11 -0400173 GrMipMapped mipMapped,
174 GrSurfaceOrigin origin,
175 SkBudgeted budgeted,
176 GrProtected isProtected) {
177 auto format = this->caps()->getBackendFormatFromColorType(colorType);
178 if (!format.isValid()) {
179 return nullptr;
Robert Phillips292a6b22019-02-14 14:49:02 -0500180 }
Brian Salomon947efe22019-07-16 15:36:11 -0400181 auto config = this->caps()->getConfigFromBackendFormat(format, colorType);
182 if (config == kUnknown_GrPixelConfig) {
Robert Phillips292a6b22019-02-14 14:49:02 -0500183 return nullptr;
184 }
185
Brian Salomon947efe22019-07-16 15:36:11 -0400186 GrSurfaceDesc desc;
187 desc.fWidth = width;
188 desc.fHeight = height;
189 desc.fIsProtected = isProtected;
190 desc.fConfig = config;
191
192 sk_sp<GrTextureProxy> texture;
193 if (GrMipMapped::kNo == mipMapped) {
194 texture = this->proxyProvider()->createProxy(format, desc, origin, fit, budgeted);
195 } else {
196 texture = this->proxyProvider()->createMipMapProxy(format, desc, origin, budgeted);
197 }
198 if (!texture) {
199 return nullptr;
Robert Phillips292a6b22019-02-14 14:49:02 -0500200 }
201
Brian Salomon947efe22019-07-16 15:36:11 -0400202 auto drawingManager = this->drawingManager();
203
204 return drawingManager->makeTextureContext(std::move(texture), colorType, alphaType,
205 std::move(colorSpace));
Robert Phillips292a6b22019-02-14 14:49:02 -0500206}
207
Robert Phillipsb97da532019-02-12 15:24:12 -0500208sk_sp<GrRenderTargetContext> GrRecordingContext::makeDeferredRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -0400209 SkBackingFit fit,
210 int width,
211 int height,
Brian Salomond6287472019-06-24 15:50:07 -0400212 GrColorType colorType,
213 sk_sp<SkColorSpace> colorSpace,
214 int sampleCnt,
215 GrMipMapped mipMapped,
216 GrSurfaceOrigin origin,
217 const SkSurfaceProps* surfaceProps,
218 SkBudgeted budgeted,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400219 GrProtected isProtected) {
Robert Phillipsb97da532019-02-12 15:24:12 -0500220 SkASSERT(sampleCnt > 0);
221 if (this->abandoned()) {
222 return nullptr;
223 }
224
Greg Daniele877dce2019-07-11 10:52:43 -0400225 auto format = this->caps()->getBackendFormatFromColorType(colorType);
Brian Salomon27ae52c2019-07-03 11:27:44 -0400226 if (!format.isValid()) {
227 return nullptr;
228 }
229 auto config = this->caps()->getConfigFromBackendFormat(format, colorType);
230 if (config == kUnknown_GrPixelConfig) {
231 return nullptr;
232 }
233
Robert Phillipsb97da532019-02-12 15:24:12 -0500234 GrSurfaceDesc desc;
235 desc.fFlags = kRenderTarget_GrSurfaceFlag;
236 desc.fWidth = width;
237 desc.fHeight = height;
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400238 desc.fIsProtected = isProtected;
Robert Phillipsb97da532019-02-12 15:24:12 -0500239 desc.fConfig = config;
240 desc.fSampleCnt = sampleCnt;
241
242 sk_sp<GrTextureProxy> rtp;
243 if (GrMipMapped::kNo == mipMapped) {
244 rtp = this->proxyProvider()->createProxy(format, desc, origin, fit, budgeted);
245 } else {
246 rtp = this->proxyProvider()->createMipMapProxy(format, desc, origin, budgeted);
247 }
248 if (!rtp) {
249 return nullptr;
250 }
251
Robert Phillipsb97da532019-02-12 15:24:12 -0500252 auto drawingManager = this->drawingManager();
253
Brian Salomond6287472019-06-24 15:50:07 -0400254 sk_sp<GrRenderTargetContext> renderTargetContext = drawingManager->makeRenderTargetContext(
255 std::move(rtp), colorType, std::move(colorSpace), surfaceProps);
Robert Phillipsb97da532019-02-12 15:24:12 -0500256 if (!renderTargetContext) {
257 return nullptr;
258 }
259
260 renderTargetContext->discard();
261
262 return renderTargetContext;
263}
Robert Phillipsd6841482019-02-08 10:29:20 -0500264
Brian Salomon27ae52c2019-07-03 11:27:44 -0400265static inline GrColorType color_type_fallback(GrColorType ct) {
266 switch (ct) {
267 // kRGBA_8888 is our default fallback for many color types that may not have renderable
268 // backend formats.
Brian Salomond6287472019-06-24 15:50:07 -0400269 case GrColorType::kAlpha_8:
Brian Salomond6287472019-06-24 15:50:07 -0400270 case GrColorType::kBGR_565:
Brian Salomond6287472019-06-24 15:50:07 -0400271 case GrColorType::kABGR_4444:
Brian Salomond6287472019-06-24 15:50:07 -0400272 case GrColorType::kBGRA_8888:
Brian Salomond6287472019-06-24 15:50:07 -0400273 case GrColorType::kRGBA_1010102:
Brian Salomond6287472019-06-24 15:50:07 -0400274 case GrColorType::kRGBA_F16:
Brian Salomond6287472019-06-24 15:50:07 -0400275 case GrColorType::kRGBA_F16_Clamped:
Brian Salomon27ae52c2019-07-03 11:27:44 -0400276 return GrColorType::kRGBA_8888;
Brian Salomond6287472019-06-24 15:50:07 -0400277 case GrColorType::kAlpha_F16:
Brian Salomon27ae52c2019-07-03 11:27:44 -0400278 return GrColorType::kRGBA_F16;
Brian Salomond6287472019-06-24 15:50:07 -0400279 case GrColorType::kGray_8:
Brian Salomon27ae52c2019-07-03 11:27:44 -0400280 return GrColorType::kRGB_888x;
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500281 default:
Brian Salomon27ae52c2019-07-03 11:27:44 -0400282 return GrColorType::kUnknown;
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500283 }
284}
285
286sk_sp<GrRenderTargetContext> GrRecordingContext::makeDeferredRenderTargetContextWithFallback(
Brian Salomond6287472019-06-24 15:50:07 -0400287 SkBackingFit fit,
288 int width,
289 int height,
Brian Salomond6287472019-06-24 15:50:07 -0400290 GrColorType colorType,
291 sk_sp<SkColorSpace> colorSpace,
292 int sampleCnt,
293 GrMipMapped mipMapped,
294 GrSurfaceOrigin origin,
295 const SkSurfaceProps* surfaceProps,
296 SkBudgeted budgeted,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400297 GrProtected isProtected) {
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500298 SkASSERT(sampleCnt > 0);
Brian Salomon27ae52c2019-07-03 11:27:44 -0400299 sk_sp<GrRenderTargetContext> rtc;
300 do {
301 rtc = this->makeDeferredRenderTargetContext(fit, width, height, colorType, colorSpace,
302 sampleCnt, mipMapped, origin, surfaceProps,
303 budgeted, isProtected);
304 colorType = color_type_fallback(colorType);
305 } while (!rtc && colorType != GrColorType::kUnknown);
306 return rtc;
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500307}
308
Robert Phillipsa41c6852019-02-07 10:44:10 -0500309///////////////////////////////////////////////////////////////////////////////////////////////////
310sk_sp<const GrCaps> GrRecordingContextPriv::refCaps() const {
311 return fContext->refCaps();
312}
313
314sk_sp<GrSkSLFPFactoryCache> GrRecordingContextPriv::fpFactoryCache() {
315 return fContext->fpFactoryCache();
316}
Robert Phillipsd6841482019-02-08 10:29:20 -0500317
318sk_sp<GrOpMemoryPool> GrRecordingContextPriv::refOpMemoryPool() {
319 return fContext->refOpMemoryPool();
320}
Robert Phillipsb97da532019-02-12 15:24:12 -0500321
Robert Phillipsc5058a62019-02-15 12:52:59 -0500322void GrRecordingContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
323 fContext->addOnFlushCallbackObject(onFlushCBObject);
324}
325
Robert Phillips292a6b22019-02-14 14:49:02 -0500326sk_sp<GrSurfaceContext> GrRecordingContextPriv::makeWrappedSurfaceContext(
Brian Salomone7499c72019-06-24 12:12:36 -0400327 sk_sp<GrSurfaceProxy> proxy,
Brian Salomond6287472019-06-24 15:50:07 -0400328 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -0400329 SkAlphaType alphaType,
330 sk_sp<SkColorSpace> colorSpace,
331 const SkSurfaceProps* props) {
Brian Salomond6287472019-06-24 15:50:07 -0400332 return fContext->makeWrappedSurfaceContext(std::move(proxy), colorType, alphaType,
333 std::move(colorSpace), props);
Robert Phillips292a6b22019-02-14 14:49:02 -0500334}
335
Brian Salomon947efe22019-07-16 15:36:11 -0400336sk_sp<GrTextureContext> GrRecordingContextPriv::makeDeferredTextureContext(
Brian Salomone7499c72019-06-24 12:12:36 -0400337 SkBackingFit fit,
Brian Salomon947efe22019-07-16 15:36:11 -0400338 int width,
339 int height,
Brian Salomond6287472019-06-24 15:50:07 -0400340 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -0400341 SkAlphaType alphaType,
342 sk_sp<SkColorSpace> colorSpace,
Brian Salomon947efe22019-07-16 15:36:11 -0400343 GrMipMapped mipMapped,
344 GrSurfaceOrigin origin,
345 SkBudgeted budgeted,
346 GrProtected isProtected) {
347 return fContext->makeDeferredTextureContext(fit, width, height, colorType, alphaType,
348 std::move(colorSpace), mipMapped, origin, budgeted,
349 isProtected);
Robert Phillips292a6b22019-02-14 14:49:02 -0500350}
351
Robert Phillipsb97da532019-02-12 15:24:12 -0500352sk_sp<GrRenderTargetContext> GrRecordingContextPriv::makeDeferredRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -0400353 SkBackingFit fit,
354 int width,
355 int height,
Brian Salomond6287472019-06-24 15:50:07 -0400356 GrColorType colorType,
357 sk_sp<SkColorSpace> colorSpace,
358 int sampleCnt,
359 GrMipMapped mipMapped,
360 GrSurfaceOrigin origin,
361 const SkSurfaceProps* surfaceProps,
362 SkBudgeted budgeted,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400363 GrProtected isProtected) {
Brian Salomon27ae52c2019-07-03 11:27:44 -0400364 return fContext->makeDeferredRenderTargetContext(fit, width, height, colorType,
Robert Phillipsb97da532019-02-12 15:24:12 -0500365 std::move(colorSpace), sampleCnt, mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400366 origin, surfaceProps, budgeted, isProtected);
Robert Phillipsb97da532019-02-12 15:24:12 -0500367}
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500368
369sk_sp<GrRenderTargetContext> GrRecordingContextPriv::makeDeferredRenderTargetContextWithFallback(
Brian Salomond6287472019-06-24 15:50:07 -0400370 SkBackingFit fit,
371 int width,
372 int height,
Brian Salomond6287472019-06-24 15:50:07 -0400373 GrColorType colorType,
374 sk_sp<SkColorSpace> colorSpace,
375 int sampleCnt,
376 GrMipMapped mipMapped,
377 GrSurfaceOrigin origin,
378 const SkSurfaceProps* surfaceProps,
379 SkBudgeted budgeted,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400380 GrProtected isProtected) {
Brian Salomon27ae52c2019-07-03 11:27:44 -0400381 return fContext->makeDeferredRenderTargetContextWithFallback(fit,
Brian Salomond6287472019-06-24 15:50:07 -0400382 width,
383 height,
Brian Salomond6287472019-06-24 15:50:07 -0400384 colorType,
385 std::move(colorSpace),
386 sampleCnt,
387 mipMapped,
388 origin,
389 surfaceProps,
390 budgeted,
391 isProtected);
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500392}
Robert Phillips9338c602019-02-19 12:52:29 -0500393
394GrContext* GrRecordingContextPriv::backdoor() {
395 return (GrContext*) fContext;
396}