blob: 081b693f51a05d9d64833a7498f9aee819c83b96 [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
Greg Danielf41b2bd2019-08-22 16:19:24 -040069void GrRecordingContext::setupDrawingManager(bool sortOpsTasks, bool reduceOpsTaskSplitting) {
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
Greg Danielf41b2bd2019-08-22 16:19:24 -040086 // unified when the opsTasks are added back to the destination drawing manager.
Robert Phillips69893702019-02-22 11:16:30 -050087 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,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400103 sortOpsTasks,
104 reduceOpsTaskSplitting));
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
Brian Salomonbf6b9792019-08-21 09:38:10 -0400146std::unique_ptr<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
Brian Salomon9d8cac82019-07-16 15:59:31 -0400154 SkASSERT(proxy);
155
Robert Phillips292a6b22019-02-14 14:49:02 -0500156 if (proxy->asRenderTargetProxy()) {
Brian Salomone7499c72019-06-24 12:12:36 -0400157 SkASSERT(kPremul_SkAlphaType == alphaType || kOpaque_SkAlphaType == alphaType);
Brian Salomond6287472019-06-24 15:50:07 -0400158 return this->drawingManager()->makeRenderTargetContext(std::move(proxy), colorType,
Robert Phillips292a6b22019-02-14 14:49:02 -0500159 std::move(colorSpace), props);
160 } else {
161 SkASSERT(proxy->asTextureProxy());
162 SkASSERT(!props);
Brian Salomond6287472019-06-24 15:50:07 -0400163 return this->drawingManager()->makeTextureContext(std::move(proxy), colorType, alphaType,
Brian Salomone7499c72019-06-24 12:12:36 -0400164 std::move(colorSpace));
Robert Phillips292a6b22019-02-14 14:49:02 -0500165 }
166}
167
Brian Salomonbf6b9792019-08-21 09:38:10 -0400168std::unique_ptr<GrTextureContext> GrRecordingContext::makeDeferredTextureContext(
Brian Salomone7499c72019-06-24 12:12:36 -0400169 SkBackingFit fit,
Brian Salomon947efe22019-07-16 15:36:11 -0400170 int width,
171 int height,
Brian Salomond6287472019-06-24 15:50:07 -0400172 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -0400173 SkAlphaType alphaType,
174 sk_sp<SkColorSpace> colorSpace,
Brian Salomon947efe22019-07-16 15:36:11 -0400175 GrMipMapped mipMapped,
176 GrSurfaceOrigin origin,
177 SkBudgeted budgeted,
178 GrProtected isProtected) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400179 auto format = this->caps()->getDefaultBackendFormat(colorType, GrRenderable::kNo);
Brian Salomon947efe22019-07-16 15:36:11 -0400180 if (!format.isValid()) {
181 return nullptr;
Robert Phillips292a6b22019-02-14 14:49:02 -0500182 }
Brian Salomon947efe22019-07-16 15:36:11 -0400183 auto config = this->caps()->getConfigFromBackendFormat(format, colorType);
184 if (config == kUnknown_GrPixelConfig) {
Robert Phillips292a6b22019-02-14 14:49:02 -0500185 return nullptr;
186 }
187
Brian Salomon947efe22019-07-16 15:36:11 -0400188 GrSurfaceDesc desc;
189 desc.fWidth = width;
190 desc.fHeight = height;
Brian Salomon947efe22019-07-16 15:36:11 -0400191 desc.fConfig = config;
192
Brian Salomonbeb7f522019-08-30 16:19:42 -0400193 sk_sp<GrTextureProxy> texture = this->proxyProvider()->createProxy(
194 format, desc, GrRenderable::kNo, 1, origin, mipMapped, fit, budgeted, isProtected);
Brian Salomon947efe22019-07-16 15:36:11 -0400195 if (!texture) {
196 return nullptr;
Robert Phillips292a6b22019-02-14 14:49:02 -0500197 }
198
Brian Salomon947efe22019-07-16 15:36:11 -0400199 auto drawingManager = this->drawingManager();
200
201 return drawingManager->makeTextureContext(std::move(texture), colorType, alphaType,
202 std::move(colorSpace));
Robert Phillips292a6b22019-02-14 14:49:02 -0500203}
204
Brian Salomonbf6b9792019-08-21 09:38:10 -0400205std::unique_ptr<GrRenderTargetContext> GrRecordingContext::makeDeferredRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -0400206 SkBackingFit fit,
207 int width,
208 int height,
Brian Salomond6287472019-06-24 15:50:07 -0400209 GrColorType colorType,
210 sk_sp<SkColorSpace> colorSpace,
211 int sampleCnt,
212 GrMipMapped mipMapped,
213 GrSurfaceOrigin origin,
214 const SkSurfaceProps* surfaceProps,
215 SkBudgeted budgeted,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400216 GrProtected isProtected) {
Robert Phillipsb97da532019-02-12 15:24:12 -0500217 SkASSERT(sampleCnt > 0);
218 if (this->abandoned()) {
219 return nullptr;
220 }
221
Robert Phillips0a15cc62019-07-30 12:49:10 -0400222 auto format = this->caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
Brian Salomon27ae52c2019-07-03 11:27:44 -0400223 if (!format.isValid()) {
224 return nullptr;
225 }
226 auto config = this->caps()->getConfigFromBackendFormat(format, colorType);
227 if (config == kUnknown_GrPixelConfig) {
228 return nullptr;
229 }
230
Robert Phillipsb97da532019-02-12 15:24:12 -0500231 GrSurfaceDesc desc;
Robert Phillipsb97da532019-02-12 15:24:12 -0500232 desc.fWidth = width;
233 desc.fHeight = height;
234 desc.fConfig = config;
Robert Phillipsb97da532019-02-12 15:24:12 -0500235
Brian Salomonbeb7f522019-08-30 16:19:42 -0400236 sk_sp<GrTextureProxy> rtp =
237 this->proxyProvider()->createProxy(format, desc, GrRenderable::kYes, sampleCnt, origin,
238 mipMapped, fit, budgeted, isProtected);
Robert Phillipsb97da532019-02-12 15:24:12 -0500239 if (!rtp) {
240 return nullptr;
241 }
242
Robert Phillipsb97da532019-02-12 15:24:12 -0500243 auto drawingManager = this->drawingManager();
244
Brian Salomonbf6b9792019-08-21 09:38:10 -0400245 auto renderTargetContext = drawingManager->makeRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -0400246 std::move(rtp), colorType, std::move(colorSpace), surfaceProps);
Robert Phillipsb97da532019-02-12 15:24:12 -0500247 if (!renderTargetContext) {
248 return nullptr;
249 }
250
251 renderTargetContext->discard();
252
253 return renderTargetContext;
254}
Robert Phillipsd6841482019-02-08 10:29:20 -0500255
Brian Salomon27ae52c2019-07-03 11:27:44 -0400256static inline GrColorType color_type_fallback(GrColorType ct) {
257 switch (ct) {
258 // kRGBA_8888 is our default fallback for many color types that may not have renderable
259 // backend formats.
Brian Salomond6287472019-06-24 15:50:07 -0400260 case GrColorType::kAlpha_8:
Brian Salomond6287472019-06-24 15:50:07 -0400261 case GrColorType::kBGR_565:
Brian Salomond6287472019-06-24 15:50:07 -0400262 case GrColorType::kABGR_4444:
Brian Salomond6287472019-06-24 15:50:07 -0400263 case GrColorType::kBGRA_8888:
Brian Salomond6287472019-06-24 15:50:07 -0400264 case GrColorType::kRGBA_1010102:
Brian Salomond6287472019-06-24 15:50:07 -0400265 case GrColorType::kRGBA_F16:
Brian Salomond6287472019-06-24 15:50:07 -0400266 case GrColorType::kRGBA_F16_Clamped:
Brian Salomon27ae52c2019-07-03 11:27:44 -0400267 return GrColorType::kRGBA_8888;
Brian Salomond6287472019-06-24 15:50:07 -0400268 case GrColorType::kAlpha_F16:
Brian Salomon27ae52c2019-07-03 11:27:44 -0400269 return GrColorType::kRGBA_F16;
Brian Salomond6287472019-06-24 15:50:07 -0400270 case GrColorType::kGray_8:
Brian Salomon27ae52c2019-07-03 11:27:44 -0400271 return GrColorType::kRGB_888x;
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500272 default:
Brian Salomon27ae52c2019-07-03 11:27:44 -0400273 return GrColorType::kUnknown;
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500274 }
275}
276
Brian Salomonbf6b9792019-08-21 09:38:10 -0400277std::unique_ptr<GrRenderTargetContext>
278GrRecordingContext::makeDeferredRenderTargetContextWithFallback(SkBackingFit fit,
279 int width,
280 int height,
281 GrColorType colorType,
282 sk_sp<SkColorSpace> colorSpace,
283 int sampleCnt,
284 GrMipMapped mipMapped,
285 GrSurfaceOrigin origin,
286 const SkSurfaceProps* surfaceProps,
287 SkBudgeted budgeted,
288 GrProtected isProtected) {
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500289 SkASSERT(sampleCnt > 0);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400290 std::unique_ptr<GrRenderTargetContext> rtc;
Brian Salomon27ae52c2019-07-03 11:27:44 -0400291 do {
292 rtc = this->makeDeferredRenderTargetContext(fit, width, height, colorType, colorSpace,
293 sampleCnt, mipMapped, origin, surfaceProps,
294 budgeted, isProtected);
295 colorType = color_type_fallback(colorType);
296 } while (!rtc && colorType != GrColorType::kUnknown);
297 return rtc;
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500298}
299
Robert Phillipsa41c6852019-02-07 10:44:10 -0500300///////////////////////////////////////////////////////////////////////////////////////////////////
301sk_sp<const GrCaps> GrRecordingContextPriv::refCaps() const {
302 return fContext->refCaps();
303}
304
305sk_sp<GrSkSLFPFactoryCache> GrRecordingContextPriv::fpFactoryCache() {
306 return fContext->fpFactoryCache();
307}
Robert Phillipsd6841482019-02-08 10:29:20 -0500308
309sk_sp<GrOpMemoryPool> GrRecordingContextPriv::refOpMemoryPool() {
310 return fContext->refOpMemoryPool();
311}
Robert Phillipsb97da532019-02-12 15:24:12 -0500312
Robert Phillipsc5058a62019-02-15 12:52:59 -0500313void GrRecordingContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
314 fContext->addOnFlushCallbackObject(onFlushCBObject);
315}
316
Brian Salomonbf6b9792019-08-21 09:38:10 -0400317std::unique_ptr<GrSurfaceContext> GrRecordingContextPriv::makeWrappedSurfaceContext(
Brian Salomone7499c72019-06-24 12:12:36 -0400318 sk_sp<GrSurfaceProxy> proxy,
Brian Salomond6287472019-06-24 15:50:07 -0400319 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -0400320 SkAlphaType alphaType,
321 sk_sp<SkColorSpace> colorSpace,
322 const SkSurfaceProps* props) {
Brian Salomond6287472019-06-24 15:50:07 -0400323 return fContext->makeWrappedSurfaceContext(std::move(proxy), colorType, alphaType,
324 std::move(colorSpace), props);
Robert Phillips292a6b22019-02-14 14:49:02 -0500325}
326
Brian Salomonbf6b9792019-08-21 09:38:10 -0400327std::unique_ptr<GrTextureContext> GrRecordingContextPriv::makeDeferredTextureContext(
Brian Salomone7499c72019-06-24 12:12:36 -0400328 SkBackingFit fit,
Brian Salomon947efe22019-07-16 15:36:11 -0400329 int width,
330 int height,
Brian Salomond6287472019-06-24 15:50:07 -0400331 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -0400332 SkAlphaType alphaType,
333 sk_sp<SkColorSpace> colorSpace,
Brian Salomon947efe22019-07-16 15:36:11 -0400334 GrMipMapped mipMapped,
335 GrSurfaceOrigin origin,
336 SkBudgeted budgeted,
337 GrProtected isProtected) {
338 return fContext->makeDeferredTextureContext(fit, width, height, colorType, alphaType,
339 std::move(colorSpace), mipMapped, origin, budgeted,
340 isProtected);
Robert Phillips292a6b22019-02-14 14:49:02 -0500341}
342
Brian Salomonbf6b9792019-08-21 09:38:10 -0400343std::unique_ptr<GrRenderTargetContext> GrRecordingContextPriv::makeDeferredRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -0400344 SkBackingFit fit,
345 int width,
346 int height,
Brian Salomond6287472019-06-24 15:50:07 -0400347 GrColorType colorType,
348 sk_sp<SkColorSpace> colorSpace,
349 int sampleCnt,
350 GrMipMapped mipMapped,
351 GrSurfaceOrigin origin,
352 const SkSurfaceProps* surfaceProps,
353 SkBudgeted budgeted,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400354 GrProtected isProtected) {
Brian Salomon27ae52c2019-07-03 11:27:44 -0400355 return fContext->makeDeferredRenderTargetContext(fit, width, height, colorType,
Robert Phillipsb97da532019-02-12 15:24:12 -0500356 std::move(colorSpace), sampleCnt, mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400357 origin, surfaceProps, budgeted, isProtected);
Robert Phillipsb97da532019-02-12 15:24:12 -0500358}
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500359
Brian Salomonbf6b9792019-08-21 09:38:10 -0400360std::unique_ptr<GrRenderTargetContext>
361GrRecordingContextPriv::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}
Greg Daniel7bfc9132019-08-14 14:23:53 -0400389