blob: c4438efc1be95e4a772f91dc6756ee9cca29efea [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,
148 SkAlphaType alphaType,
149 sk_sp<SkColorSpace> colorSpace,
150 const SkSurfaceProps* props) {
Robert Phillips292a6b22019-02-14 14:49:02 -0500151 ASSERT_SINGLE_OWNER_PRIV
152
153 if (proxy->asRenderTargetProxy()) {
Brian Salomone7499c72019-06-24 12:12:36 -0400154 SkASSERT(kPremul_SkAlphaType == alphaType || kOpaque_SkAlphaType == alphaType);
Robert Phillips292a6b22019-02-14 14:49:02 -0500155 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
156 std::move(colorSpace), props);
157 } else {
158 SkASSERT(proxy->asTextureProxy());
159 SkASSERT(!props);
Brian Salomone7499c72019-06-24 12:12:36 -0400160 return this->drawingManager()->makeTextureContext(std::move(proxy), alphaType,
161 std::move(colorSpace));
Robert Phillips292a6b22019-02-14 14:49:02 -0500162 }
163}
164
165sk_sp<GrSurfaceContext> GrRecordingContext::makeDeferredSurfaceContext(
Brian Salomone7499c72019-06-24 12:12:36 -0400166 const GrBackendFormat& format,
167 const GrSurfaceDesc& dstDesc,
168 GrSurfaceOrigin origin,
169 GrMipMapped mipMapped,
170 SkBackingFit fit,
171 SkBudgeted isDstBudgeted,
172 SkAlphaType alphaType,
173 sk_sp<SkColorSpace> colorSpace,
174 const SkSurfaceProps* props) {
Robert Phillips292a6b22019-02-14 14:49:02 -0500175 sk_sp<GrTextureProxy> proxy;
176 if (GrMipMapped::kNo == mipMapped) {
177 proxy = this->proxyProvider()->createProxy(format, dstDesc, origin, fit, isDstBudgeted);
178 } else {
179 SkASSERT(SkBackingFit::kExact == fit);
180 proxy = this->proxyProvider()->createMipMapProxy(format, dstDesc, origin, isDstBudgeted);
181 }
182 if (!proxy) {
183 return nullptr;
184 }
185
186 sk_sp<GrSurfaceContext> sContext = this->makeWrappedSurfaceContext(std::move(proxy),
Brian Salomone7499c72019-06-24 12:12:36 -0400187 alphaType,
Robert Phillips292a6b22019-02-14 14:49:02 -0500188 std::move(colorSpace),
189 props);
190 if (sContext && sContext->asRenderTargetContext()) {
191 sContext->asRenderTargetContext()->discard();
192 }
193
194 return sContext;
195}
196
Robert Phillipsb97da532019-02-12 15:24:12 -0500197sk_sp<GrRenderTargetContext> GrRecordingContext::makeDeferredRenderTargetContext(
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400198 const GrBackendFormat& format, SkBackingFit fit, int width, int height,
199 GrPixelConfig config, sk_sp<SkColorSpace> colorSpace, int sampleCnt, GrMipMapped mipMapped,
200 GrSurfaceOrigin origin, const SkSurfaceProps* surfaceProps, SkBudgeted budgeted,
201 GrProtected isProtected) {
Robert Phillipsb97da532019-02-12 15:24:12 -0500202 SkASSERT(sampleCnt > 0);
203 if (this->abandoned()) {
204 return nullptr;
205 }
206
207 GrSurfaceDesc desc;
208 desc.fFlags = kRenderTarget_GrSurfaceFlag;
209 desc.fWidth = width;
210 desc.fHeight = height;
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400211 desc.fIsProtected = isProtected;
Robert Phillipsb97da532019-02-12 15:24:12 -0500212 desc.fConfig = config;
213 desc.fSampleCnt = sampleCnt;
214
215 sk_sp<GrTextureProxy> rtp;
216 if (GrMipMapped::kNo == mipMapped) {
217 rtp = this->proxyProvider()->createProxy(format, desc, origin, fit, budgeted);
218 } else {
219 rtp = this->proxyProvider()->createMipMapProxy(format, desc, origin, budgeted);
220 }
221 if (!rtp) {
222 return nullptr;
223 }
224
Robert Phillipsb97da532019-02-12 15:24:12 -0500225 auto drawingManager = this->drawingManager();
226
227 sk_sp<GrRenderTargetContext> renderTargetContext =
228 drawingManager->makeRenderTargetContext(std::move(rtp), std::move(colorSpace),
229 surfaceProps);
230 if (!renderTargetContext) {
231 return nullptr;
232 }
233
234 renderTargetContext->discard();
235
236 return renderTargetContext;
237}
Robert Phillipsd6841482019-02-08 10:29:20 -0500238
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500239static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
240 switch (config) {
241 case kAlpha_8_GrPixelConfig:
242 case kAlpha_8_as_Alpha_GrPixelConfig:
243 case kAlpha_8_as_Red_GrPixelConfig:
244 case kRGB_565_GrPixelConfig:
245 case kRGBA_4444_GrPixelConfig:
246 case kBGRA_8888_GrPixelConfig:
247 case kRGBA_1010102_GrPixelConfig:
248 case kRGBA_half_GrPixelConfig:
Brian Osmand0626aa2019-03-11 15:28:06 -0400249 case kRGBA_half_Clamped_GrPixelConfig:
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500250 return kRGBA_8888_GrPixelConfig;
251 case kSBGRA_8888_GrPixelConfig:
252 return kSRGBA_8888_GrPixelConfig;
253 case kAlpha_half_GrPixelConfig:
254 case kAlpha_half_as_Red_GrPixelConfig:
255 return kRGBA_half_GrPixelConfig;
256 case kGray_8_GrPixelConfig:
257 case kGray_8_as_Lum_GrPixelConfig:
258 case kGray_8_as_Red_GrPixelConfig:
259 return kRGB_888_GrPixelConfig;
260 default:
261 return kUnknown_GrPixelConfig;
262 }
263}
264
265sk_sp<GrRenderTargetContext> GrRecordingContext::makeDeferredRenderTargetContextWithFallback(
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400266 const GrBackendFormat& format, SkBackingFit fit, int width, int height,
267 GrPixelConfig config, sk_sp<SkColorSpace> colorSpace, int sampleCnt, GrMipMapped mipMapped,
268 GrSurfaceOrigin origin, const SkSurfaceProps* surfaceProps, SkBudgeted budgeted,
269 GrProtected isProtected) {
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500270 GrBackendFormat localFormat = format;
271 SkASSERT(sampleCnt > 0);
272 if (0 == this->caps()->getRenderTargetSampleCount(sampleCnt, config)) {
273 config = GrPixelConfigFallback(config);
274 // TODO: First we should be checking the getRenderTargetSampleCount from the GrBackendFormat
275 // and not GrPixelConfig. Besides that, we should implement the fallback in the caps, but
276 // for now we just convert the fallback pixel config to an SkColorType and then get the
277 // GrBackendFormat from that.
278 SkColorType colorType;
279 if (!GrPixelConfigToColorType(config, &colorType)) {
280 return nullptr;
281 }
282 localFormat = this->caps()->getBackendFormatFromColorType(colorType);
283 }
284
285 return this->makeDeferredRenderTargetContext(localFormat, fit, width, height, config,
286 std::move(colorSpace), sampleCnt, mipMapped,
287 origin, surfaceProps, budgeted);
288}
289
Robert Phillipsa41c6852019-02-07 10:44:10 -0500290///////////////////////////////////////////////////////////////////////////////////////////////////
291sk_sp<const GrCaps> GrRecordingContextPriv::refCaps() const {
292 return fContext->refCaps();
293}
294
295sk_sp<GrSkSLFPFactoryCache> GrRecordingContextPriv::fpFactoryCache() {
296 return fContext->fpFactoryCache();
297}
Robert Phillipsd6841482019-02-08 10:29:20 -0500298
299sk_sp<GrOpMemoryPool> GrRecordingContextPriv::refOpMemoryPool() {
300 return fContext->refOpMemoryPool();
301}
Robert Phillipsb97da532019-02-12 15:24:12 -0500302
Robert Phillipsc5058a62019-02-15 12:52:59 -0500303void GrRecordingContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
304 fContext->addOnFlushCallbackObject(onFlushCBObject);
305}
306
Robert Phillips292a6b22019-02-14 14:49:02 -0500307sk_sp<GrSurfaceContext> GrRecordingContextPriv::makeWrappedSurfaceContext(
Brian Salomone7499c72019-06-24 12:12:36 -0400308 sk_sp<GrSurfaceProxy> proxy,
309 SkAlphaType alphaType,
310 sk_sp<SkColorSpace> colorSpace,
311 const SkSurfaceProps* props) {
312 return fContext->makeWrappedSurfaceContext(std::move(proxy), alphaType, std::move(colorSpace),
313 props);
Robert Phillips292a6b22019-02-14 14:49:02 -0500314}
315
316sk_sp<GrSurfaceContext> GrRecordingContextPriv::makeDeferredSurfaceContext(
Brian Salomone7499c72019-06-24 12:12:36 -0400317 const GrBackendFormat& format,
318 const GrSurfaceDesc& dstDesc,
319 GrSurfaceOrigin origin,
320 GrMipMapped mipMapped,
321 SkBackingFit fit,
322 SkBudgeted isDstBudgeted,
323 SkAlphaType alphaType,
324 sk_sp<SkColorSpace> colorSpace,
325 const SkSurfaceProps* props) {
Robert Phillips292a6b22019-02-14 14:49:02 -0500326 return fContext->makeDeferredSurfaceContext(format, dstDesc, origin, mipMapped, fit,
Brian Salomone7499c72019-06-24 12:12:36 -0400327 isDstBudgeted, alphaType, std::move(colorSpace),
328 props);
Robert Phillips292a6b22019-02-14 14:49:02 -0500329}
330
Robert Phillipsb97da532019-02-12 15:24:12 -0500331sk_sp<GrRenderTargetContext> GrRecordingContextPriv::makeDeferredRenderTargetContext(
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400332 const GrBackendFormat& format, SkBackingFit fit, int width, int height,
333 GrPixelConfig config, sk_sp<SkColorSpace> colorSpace, int sampleCnt, GrMipMapped mipMapped,
334 GrSurfaceOrigin origin, const SkSurfaceProps* surfaceProps, SkBudgeted budgeted,
335 GrProtected isProtected) {
Robert Phillipsb97da532019-02-12 15:24:12 -0500336 return fContext->makeDeferredRenderTargetContext(format, fit, width, height, config,
337 std::move(colorSpace), sampleCnt, mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400338 origin, surfaceProps, budgeted, isProtected);
Robert Phillipsb97da532019-02-12 15:24:12 -0500339}
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500340
341sk_sp<GrRenderTargetContext> GrRecordingContextPriv::makeDeferredRenderTargetContextWithFallback(
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400342 const GrBackendFormat& format, SkBackingFit fit, int width, int height,
343 GrPixelConfig config, sk_sp<SkColorSpace> colorSpace, int sampleCnt, GrMipMapped mipMapped,
344 GrSurfaceOrigin origin, const SkSurfaceProps* surfaceProps, SkBudgeted budgeted,
345 GrProtected isProtected) {
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500346 return fContext->makeDeferredRenderTargetContextWithFallback(format, fit, width, height, config,
347 std::move(colorSpace), sampleCnt,
348 mipMapped, origin, surfaceProps,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400349 budgeted, isProtected);
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500350}
Robert Phillips9338c602019-02-19 12:52:29 -0500351
352GrContext* GrRecordingContextPriv::backdoor() {
353 return (GrContext*) fContext;
354}