blob: 6cfd49c60ba88c2af195286a1a932726292e4880 [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
166sk_sp<GrSurfaceContext> GrRecordingContext::makeDeferredSurfaceContext(
Brian Salomone7499c72019-06-24 12:12:36 -0400167 const GrBackendFormat& format,
168 const GrSurfaceDesc& dstDesc,
169 GrSurfaceOrigin origin,
170 GrMipMapped mipMapped,
171 SkBackingFit fit,
172 SkBudgeted isDstBudgeted,
Brian Salomond6287472019-06-24 15:50:07 -0400173 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -0400174 SkAlphaType alphaType,
175 sk_sp<SkColorSpace> colorSpace,
176 const SkSurfaceProps* props) {
Robert Phillips292a6b22019-02-14 14:49:02 -0500177 sk_sp<GrTextureProxy> proxy;
178 if (GrMipMapped::kNo == mipMapped) {
179 proxy = this->proxyProvider()->createProxy(format, dstDesc, origin, fit, isDstBudgeted);
180 } else {
181 SkASSERT(SkBackingFit::kExact == fit);
182 proxy = this->proxyProvider()->createMipMapProxy(format, dstDesc, origin, isDstBudgeted);
183 }
184 if (!proxy) {
185 return nullptr;
186 }
187
188 sk_sp<GrSurfaceContext> sContext = this->makeWrappedSurfaceContext(std::move(proxy),
Brian Salomond6287472019-06-24 15:50:07 -0400189 colorType,
Brian Salomone7499c72019-06-24 12:12:36 -0400190 alphaType,
Robert Phillips292a6b22019-02-14 14:49:02 -0500191 std::move(colorSpace),
192 props);
193 if (sContext && sContext->asRenderTargetContext()) {
194 sContext->asRenderTargetContext()->discard();
195 }
196
197 return sContext;
198}
199
Robert Phillipsb97da532019-02-12 15:24:12 -0500200sk_sp<GrRenderTargetContext> GrRecordingContext::makeDeferredRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -0400201 SkBackingFit fit,
202 int width,
203 int height,
Brian Salomond6287472019-06-24 15:50:07 -0400204 GrColorType colorType,
205 sk_sp<SkColorSpace> colorSpace,
206 int sampleCnt,
207 GrMipMapped mipMapped,
208 GrSurfaceOrigin origin,
209 const SkSurfaceProps* surfaceProps,
210 SkBudgeted budgeted,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400211 GrProtected isProtected) {
Robert Phillipsb97da532019-02-12 15:24:12 -0500212 SkASSERT(sampleCnt > 0);
213 if (this->abandoned()) {
214 return nullptr;
215 }
216
Brian Salomon27ae52c2019-07-03 11:27:44 -0400217 auto format = this->caps()->getBackendFormatFromGrColorType(colorType, GrSRGBEncoded::kNo);
218 if (!format.isValid()) {
219 return nullptr;
220 }
221 auto config = this->caps()->getConfigFromBackendFormat(format, colorType);
222 if (config == kUnknown_GrPixelConfig) {
223 return nullptr;
224 }
225
Robert Phillipsb97da532019-02-12 15:24:12 -0500226 GrSurfaceDesc desc;
227 desc.fFlags = kRenderTarget_GrSurfaceFlag;
228 desc.fWidth = width;
229 desc.fHeight = height;
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400230 desc.fIsProtected = isProtected;
Robert Phillipsb97da532019-02-12 15:24:12 -0500231 desc.fConfig = config;
232 desc.fSampleCnt = sampleCnt;
233
234 sk_sp<GrTextureProxy> rtp;
235 if (GrMipMapped::kNo == mipMapped) {
236 rtp = this->proxyProvider()->createProxy(format, desc, origin, fit, budgeted);
237 } else {
238 rtp = this->proxyProvider()->createMipMapProxy(format, desc, origin, budgeted);
239 }
240 if (!rtp) {
241 return nullptr;
242 }
243
Robert Phillipsb97da532019-02-12 15:24:12 -0500244 auto drawingManager = this->drawingManager();
245
Brian Salomond6287472019-06-24 15:50:07 -0400246 sk_sp<GrRenderTargetContext> renderTargetContext = drawingManager->makeRenderTargetContext(
247 std::move(rtp), colorType, std::move(colorSpace), surfaceProps);
Robert Phillipsb97da532019-02-12 15:24:12 -0500248 if (!renderTargetContext) {
249 return nullptr;
250 }
251
252 renderTargetContext->discard();
253
254 return renderTargetContext;
255}
Robert Phillipsd6841482019-02-08 10:29:20 -0500256
Brian Salomon27ae52c2019-07-03 11:27:44 -0400257static inline GrColorType color_type_fallback(GrColorType ct) {
258 switch (ct) {
259 // kRGBA_8888 is our default fallback for many color types that may not have renderable
260 // backend formats.
Brian Salomond6287472019-06-24 15:50:07 -0400261 case GrColorType::kAlpha_8:
Brian Salomond6287472019-06-24 15:50:07 -0400262 case GrColorType::kBGR_565:
Brian Salomond6287472019-06-24 15:50:07 -0400263 case GrColorType::kABGR_4444:
Brian Salomond6287472019-06-24 15:50:07 -0400264 case GrColorType::kBGRA_8888:
Brian Salomond6287472019-06-24 15:50:07 -0400265 case GrColorType::kRGBA_1010102:
Brian Salomond6287472019-06-24 15:50:07 -0400266 case GrColorType::kRGBA_F16:
Brian Salomond6287472019-06-24 15:50:07 -0400267 case GrColorType::kRGBA_F16_Clamped:
Brian Salomon27ae52c2019-07-03 11:27:44 -0400268 return GrColorType::kRGBA_8888;
Brian Salomond6287472019-06-24 15:50:07 -0400269 case GrColorType::kAlpha_F16:
Brian Salomon27ae52c2019-07-03 11:27:44 -0400270 return GrColorType::kRGBA_F16;
Brian Salomond6287472019-06-24 15:50:07 -0400271 case GrColorType::kGray_8:
Brian Salomon27ae52c2019-07-03 11:27:44 -0400272 return GrColorType::kRGB_888x;
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500273 default:
Brian Salomon27ae52c2019-07-03 11:27:44 -0400274 return GrColorType::kUnknown;
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500275 }
276}
277
278sk_sp<GrRenderTargetContext> GrRecordingContext::makeDeferredRenderTargetContextWithFallback(
Brian Salomond6287472019-06-24 15:50:07 -0400279 SkBackingFit fit,
280 int width,
281 int height,
Brian Salomond6287472019-06-24 15:50:07 -0400282 GrColorType colorType,
283 sk_sp<SkColorSpace> colorSpace,
284 int sampleCnt,
285 GrMipMapped mipMapped,
286 GrSurfaceOrigin origin,
287 const SkSurfaceProps* surfaceProps,
288 SkBudgeted budgeted,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400289 GrProtected isProtected) {
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500290 SkASSERT(sampleCnt > 0);
Brian Salomon27ae52c2019-07-03 11:27:44 -0400291 sk_sp<GrRenderTargetContext> rtc;
292 do {
293 rtc = this->makeDeferredRenderTargetContext(fit, width, height, colorType, colorSpace,
294 sampleCnt, mipMapped, origin, surfaceProps,
295 budgeted, isProtected);
296 colorType = color_type_fallback(colorType);
297 } while (!rtc && colorType != GrColorType::kUnknown);
298 return rtc;
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500299}
300
Robert Phillipsa41c6852019-02-07 10:44:10 -0500301///////////////////////////////////////////////////////////////////////////////////////////////////
302sk_sp<const GrCaps> GrRecordingContextPriv::refCaps() const {
303 return fContext->refCaps();
304}
305
306sk_sp<GrSkSLFPFactoryCache> GrRecordingContextPriv::fpFactoryCache() {
307 return fContext->fpFactoryCache();
308}
Robert Phillipsd6841482019-02-08 10:29:20 -0500309
310sk_sp<GrOpMemoryPool> GrRecordingContextPriv::refOpMemoryPool() {
311 return fContext->refOpMemoryPool();
312}
Robert Phillipsb97da532019-02-12 15:24:12 -0500313
Robert Phillipsc5058a62019-02-15 12:52:59 -0500314void GrRecordingContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
315 fContext->addOnFlushCallbackObject(onFlushCBObject);
316}
317
Robert Phillips292a6b22019-02-14 14:49:02 -0500318sk_sp<GrSurfaceContext> GrRecordingContextPriv::makeWrappedSurfaceContext(
Brian Salomone7499c72019-06-24 12:12:36 -0400319 sk_sp<GrSurfaceProxy> proxy,
Brian Salomond6287472019-06-24 15:50:07 -0400320 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -0400321 SkAlphaType alphaType,
322 sk_sp<SkColorSpace> colorSpace,
323 const SkSurfaceProps* props) {
Brian Salomond6287472019-06-24 15:50:07 -0400324 return fContext->makeWrappedSurfaceContext(std::move(proxy), colorType, alphaType,
325 std::move(colorSpace), props);
Robert Phillips292a6b22019-02-14 14:49:02 -0500326}
327
328sk_sp<GrSurfaceContext> GrRecordingContextPriv::makeDeferredSurfaceContext(
Brian Salomone7499c72019-06-24 12:12:36 -0400329 const GrBackendFormat& format,
330 const GrSurfaceDesc& dstDesc,
331 GrSurfaceOrigin origin,
332 GrMipMapped mipMapped,
333 SkBackingFit fit,
334 SkBudgeted isDstBudgeted,
Brian Salomond6287472019-06-24 15:50:07 -0400335 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -0400336 SkAlphaType alphaType,
337 sk_sp<SkColorSpace> colorSpace,
338 const SkSurfaceProps* props) {
Robert Phillips292a6b22019-02-14 14:49:02 -0500339 return fContext->makeDeferredSurfaceContext(format, dstDesc, origin, mipMapped, fit,
Brian Salomond6287472019-06-24 15:50:07 -0400340 isDstBudgeted, colorType, alphaType,
341 std::move(colorSpace), props);
Robert Phillips292a6b22019-02-14 14:49:02 -0500342}
343
Robert Phillipsb97da532019-02-12 15:24:12 -0500344sk_sp<GrRenderTargetContext> GrRecordingContextPriv::makeDeferredRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -0400345 SkBackingFit fit,
346 int width,
347 int height,
Brian Salomond6287472019-06-24 15:50:07 -0400348 GrColorType colorType,
349 sk_sp<SkColorSpace> colorSpace,
350 int sampleCnt,
351 GrMipMapped mipMapped,
352 GrSurfaceOrigin origin,
353 const SkSurfaceProps* surfaceProps,
354 SkBudgeted budgeted,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400355 GrProtected isProtected) {
Brian Salomon27ae52c2019-07-03 11:27:44 -0400356 return fContext->makeDeferredRenderTargetContext(fit, width, height, colorType,
Robert Phillipsb97da532019-02-12 15:24:12 -0500357 std::move(colorSpace), sampleCnt, mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400358 origin, surfaceProps, budgeted, isProtected);
Robert Phillipsb97da532019-02-12 15:24:12 -0500359}
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500360
361sk_sp<GrRenderTargetContext> GrRecordingContextPriv::makeDeferredRenderTargetContextWithFallback(
Brian Salomond6287472019-06-24 15:50:07 -0400362 SkBackingFit fit,
363 int width,
364 int height,
Brian Salomond6287472019-06-24 15:50:07 -0400365 GrColorType colorType,
366 sk_sp<SkColorSpace> colorSpace,
367 int sampleCnt,
368 GrMipMapped mipMapped,
369 GrSurfaceOrigin origin,
370 const SkSurfaceProps* surfaceProps,
371 SkBudgeted budgeted,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400372 GrProtected isProtected) {
Brian Salomon27ae52c2019-07-03 11:27:44 -0400373 return fContext->makeDeferredRenderTargetContextWithFallback(fit,
Brian Salomond6287472019-06-24 15:50:07 -0400374 width,
375 height,
Brian Salomond6287472019-06-24 15:50:07 -0400376 colorType,
377 std::move(colorSpace),
378 sampleCnt,
379 mipMapped,
380 origin,
381 surfaceProps,
382 budgeted,
383 isProtected);
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500384}
Robert Phillips9338c602019-02-19 12:52:29 -0500385
386GrContext* GrRecordingContextPriv::backdoor() {
387 return (GrContext*) fContext;
388}