blob: 7b1a1c03c88ca8437aead9437cda29437c812814 [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
8#include "GrRecordingContext.h"
9
Robert Phillipsa41c6852019-02-07 10:44:10 -050010#include "GrCaps.h"
Robert Phillips0d075de2019-03-04 11:08:13 -050011#include "GrContext.h"
Robert Phillipsb97da532019-02-12 15:24:12 -050012#include "GrDrawingManager.h"
Robert Phillipsd6841482019-02-08 10:29:20 -050013#include "GrMemoryPool.h"
Robert Phillipsb97da532019-02-12 15:24:12 -050014#include "GrProxyProvider.h"
Robert Phillipsa41c6852019-02-07 10:44:10 -050015#include "GrRecordingContextPriv.h"
Robert Phillipsb97da532019-02-12 15:24:12 -050016#include "GrRenderTargetContext.h"
Robert Phillipsa41c6852019-02-07 10:44:10 -050017#include "GrSkSLFPFactoryCache.h"
Robert Phillips292a6b22019-02-14 14:49:02 -050018#include "GrTextureContext.h"
Robert Phillips6f0e02f2019-02-13 11:02:28 -050019#include "SkGr.h"
Robert Phillips2184fb72019-02-21 16:11:41 -050020#include "text/GrTextBlobCache.h"
Robert Phillipsa41c6852019-02-07 10:44:10 -050021
Robert Phillips292a6b22019-02-14 14:49:02 -050022#define ASSERT_SINGLE_OWNER_PRIV \
23 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
24
Robert Phillipsc1541ae2019-02-04 12:05:37 -050025GrRecordingContext::GrRecordingContext(GrBackendApi backend,
26 const GrContextOptions& options,
Robert Phillipsa41c6852019-02-07 10:44:10 -050027 uint32_t contextID)
28 : INHERITED(backend, options, contextID) {
Robert Phillips4217ea72019-01-30 13:08:28 -050029}
30
31GrRecordingContext::~GrRecordingContext() { }
32
Robert Phillips2184fb72019-02-21 16:11:41 -050033/**
34 * TODO: move textblob draw calls below context (see comment below)
35 */
36static void textblobcache_overbudget_CB(void* data) {
37 SkASSERT(data);
38 GrRecordingContext* context = reinterpret_cast<GrRecordingContext*>(data);
39
40 GrContext* direct = context->priv().asDirectContext();
41 if (!direct) {
42 return;
43 }
44
45 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
46 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
47 // to below the GrContext level, but this is not trivial because they call drawPath on
48 // SkGpuDevice.
49 direct->flush();
50}
51
Robert Phillips292a6b22019-02-14 14:49:02 -050052bool GrRecordingContext::init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> cache) {
53
54 if (!INHERITED::init(std::move(caps), std::move(cache))) {
55 return false;
56 }
57
Herb Derbya00da612019-03-04 17:10:01 -050058 fStrikeCache.reset(new GrStrikeCache(this->caps(),
Robert Phillips2184fb72019-02-21 16:11:41 -050059 this->options().fGlyphCacheTextureMaximumBytes));
60
61 fTextBlobCache.reset(new GrTextBlobCache(textblobcache_overbudget_CB, this,
62 this->contextID()));
63
Robert Phillips56181ba2019-03-08 12:00:45 -050064 return true;
65}
66
Robert Phillips12c46292019-04-23 07:36:17 -040067void GrRecordingContext::setupDrawingManager(bool sortOpLists) {
Robert Phillips69893702019-02-22 11:16:30 -050068 GrPathRendererChain::Options prcOptions;
69 prcOptions.fAllowPathMaskCaching = this->options().fAllowPathMaskCaching;
70#if GR_TEST_UTILS
71 prcOptions.fGpuPathRenderers = this->options().fGpuPathRenderers;
72#endif
Chris Daltonf7748182019-03-18 16:22:30 +000073 // FIXME: Once this is removed from Chrome and Android, rename to fEnable"".
74 if (!this->options().fDisableCoverageCountingPaths) {
75 prcOptions.fGpuPathRenderers |= GpuPathRenderers::kCoverageCounting;
Robert Phillips69893702019-02-22 11:16:30 -050076 }
77 if (this->options().fDisableDistanceFieldPaths) {
78 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
79 }
80
81 if (!this->proxyProvider()->renderingDirectly()) {
82 // DDL TODO: remove this crippling of the path renderer chain
83 // Disable the small path renderer bc of the proxies in the atlas. They need to be
84 // unified when the opLists are added back to the destination drawing manager.
85 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
86 }
87
88 GrTextContext::Options textContextOptions;
89 textContextOptions.fMaxDistanceFieldFontSize = this->options().fGlyphsAsPathsFontSize;
90 textContextOptions.fMinDistanceFieldFontSize = this->options().fMinDistanceFieldFontSize;
91 textContextOptions.fDistanceFieldVerticesAlwaysHaveW = false;
92#if SK_SUPPORT_ATLAS_TEXT
93 if (GrContextOptions::Enable::kYes == this->options().fDistanceFieldGlyphVerticesAlwaysHaveW) {
94 textContextOptions.fDistanceFieldVerticesAlwaysHaveW = true;
95 }
96#endif
97
Robert Phillips60dd62b2019-03-12 16:28:59 +000098 // SHORT TERM TODO: until intermediate flushes at allocation time are added we need to obey the
99 // reduceOpListSplitting flag. Once that lands we should always reduce opList splitting in
100 // DDL contexts/drawing managers. We should still obey the options for non-DDL drawing managers
101 // until predictive intermediate flushes are added (i.e., we can't reorder forever).
Robert Phillips69893702019-02-22 11:16:30 -0500102 fDrawingManager.reset(new GrDrawingManager(this,
Robert Phillips12c46292019-04-23 07:36:17 -0400103 prcOptions,
104 textContextOptions,
105 sortOpLists,
106 this->options().fReduceOpListSplitting));
Robert Phillips292a6b22019-02-14 14:49:02 -0500107}
108
Robert Phillipsa9162df2019-02-11 14:12:03 -0500109void GrRecordingContext::abandonContext() {
110 INHERITED::abandonContext();
Robert Phillips2184fb72019-02-21 16:11:41 -0500111
Herb Derbya00da612019-03-04 17:10:01 -0500112 fStrikeCache->freeAll();
Robert Phillips2184fb72019-02-21 16:11:41 -0500113 fTextBlobCache->freeAll();
Robert Phillipsa9162df2019-02-11 14:12:03 -0500114}
115
Robert Phillips69893702019-02-22 11:16:30 -0500116GrDrawingManager* GrRecordingContext::drawingManager() {
117 return fDrawingManager.get();
118}
119
Robert Phillipsd6841482019-02-08 10:29:20 -0500120sk_sp<GrOpMemoryPool> GrRecordingContext::refOpMemoryPool() {
121 if (!fOpMemoryPool) {
122 // DDL TODO: should the size of the memory pool be decreased in DDL mode? CPU-side memory
123 // consumed in DDL mode vs. normal mode for a single skp might be a good metric of wasted
124 // memory.
125 fOpMemoryPool = sk_sp<GrOpMemoryPool>(new GrOpMemoryPool(16384, 16384));
126 }
127
128 SkASSERT(fOpMemoryPool);
129 return fOpMemoryPool;
130}
131
132GrOpMemoryPool* GrRecordingContext::opMemoryPool() {
133 return this->refOpMemoryPool().get();
134}
135
Robert Phillips2184fb72019-02-21 16:11:41 -0500136GrTextBlobCache* GrRecordingContext::getTextBlobCache() {
137 return fTextBlobCache.get();
138}
139
140const GrTextBlobCache* GrRecordingContext::getTextBlobCache() const {
141 return fTextBlobCache.get();
142}
143
Robert Phillipsc5058a62019-02-15 12:52:59 -0500144void GrRecordingContext::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
145 this->drawingManager()->addOnFlushCallbackObject(onFlushCBObject);
146}
147
Robert Phillips292a6b22019-02-14 14:49:02 -0500148sk_sp<GrSurfaceContext> GrRecordingContext::makeWrappedSurfaceContext(
149 sk_sp<GrSurfaceProxy> proxy,
150 sk_sp<SkColorSpace> colorSpace,
151 const SkSurfaceProps* props) {
152 ASSERT_SINGLE_OWNER_PRIV
153
154 if (proxy->asRenderTargetProxy()) {
155 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
156 std::move(colorSpace), props);
157 } else {
158 SkASSERT(proxy->asTextureProxy());
159 SkASSERT(!props);
160 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
161 }
162}
163
164sk_sp<GrSurfaceContext> GrRecordingContext::makeDeferredSurfaceContext(
165 const GrBackendFormat& format,
166 const GrSurfaceDesc& dstDesc,
167 GrSurfaceOrigin origin,
168 GrMipMapped mipMapped,
169 SkBackingFit fit,
170 SkBudgeted isDstBudgeted,
171 sk_sp<SkColorSpace> colorSpace,
172 const SkSurfaceProps* props) {
173 sk_sp<GrTextureProxy> proxy;
174 if (GrMipMapped::kNo == mipMapped) {
175 proxy = this->proxyProvider()->createProxy(format, dstDesc, origin, fit, isDstBudgeted);
176 } else {
177 SkASSERT(SkBackingFit::kExact == fit);
178 proxy = this->proxyProvider()->createMipMapProxy(format, dstDesc, origin, isDstBudgeted);
179 }
180 if (!proxy) {
181 return nullptr;
182 }
183
184 sk_sp<GrSurfaceContext> sContext = this->makeWrappedSurfaceContext(std::move(proxy),
185 std::move(colorSpace),
186 props);
187 if (sContext && sContext->asRenderTargetContext()) {
188 sContext->asRenderTargetContext()->discard();
189 }
190
191 return sContext;
192}
193
Robert Phillipsb97da532019-02-12 15:24:12 -0500194sk_sp<GrRenderTargetContext> GrRecordingContext::makeDeferredRenderTargetContext(
195 const GrBackendFormat& format,
196 SkBackingFit fit,
197 int width, int height,
198 GrPixelConfig config,
199 sk_sp<SkColorSpace> colorSpace,
200 int sampleCnt,
201 GrMipMapped mipMapped,
202 GrSurfaceOrigin origin,
203 const SkSurfaceProps* surfaceProps,
204 SkBudgeted budgeted) {
205 SkASSERT(sampleCnt > 0);
206 if (this->abandoned()) {
207 return nullptr;
208 }
209
210 GrSurfaceDesc desc;
211 desc.fFlags = kRenderTarget_GrSurfaceFlag;
212 desc.fWidth = width;
213 desc.fHeight = height;
214 desc.fConfig = config;
215 desc.fSampleCnt = sampleCnt;
216
217 sk_sp<GrTextureProxy> rtp;
218 if (GrMipMapped::kNo == mipMapped) {
219 rtp = this->proxyProvider()->createProxy(format, desc, origin, fit, budgeted);
220 } else {
221 rtp = this->proxyProvider()->createMipMapProxy(format, desc, origin, budgeted);
222 }
223 if (!rtp) {
224 return nullptr;
225 }
226
Robert Phillipsb97da532019-02-12 15:24:12 -0500227 auto drawingManager = this->drawingManager();
228
229 sk_sp<GrRenderTargetContext> renderTargetContext =
230 drawingManager->makeRenderTargetContext(std::move(rtp), std::move(colorSpace),
231 surfaceProps);
232 if (!renderTargetContext) {
233 return nullptr;
234 }
235
236 renderTargetContext->discard();
237
238 return renderTargetContext;
239}
Robert Phillipsd6841482019-02-08 10:29:20 -0500240
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500241static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
242 switch (config) {
243 case kAlpha_8_GrPixelConfig:
244 case kAlpha_8_as_Alpha_GrPixelConfig:
245 case kAlpha_8_as_Red_GrPixelConfig:
246 case kRGB_565_GrPixelConfig:
247 case kRGBA_4444_GrPixelConfig:
248 case kBGRA_8888_GrPixelConfig:
249 case kRGBA_1010102_GrPixelConfig:
250 case kRGBA_half_GrPixelConfig:
Brian Osmand0626aa2019-03-11 15:28:06 -0400251 case kRGBA_half_Clamped_GrPixelConfig:
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500252 return kRGBA_8888_GrPixelConfig;
253 case kSBGRA_8888_GrPixelConfig:
254 return kSRGBA_8888_GrPixelConfig;
255 case kAlpha_half_GrPixelConfig:
256 case kAlpha_half_as_Red_GrPixelConfig:
257 return kRGBA_half_GrPixelConfig;
258 case kGray_8_GrPixelConfig:
259 case kGray_8_as_Lum_GrPixelConfig:
260 case kGray_8_as_Red_GrPixelConfig:
261 return kRGB_888_GrPixelConfig;
262 default:
263 return kUnknown_GrPixelConfig;
264 }
265}
266
267sk_sp<GrRenderTargetContext> GrRecordingContext::makeDeferredRenderTargetContextWithFallback(
268 const GrBackendFormat& format,
269 SkBackingFit fit,
270 int width, int height,
271 GrPixelConfig config,
272 sk_sp<SkColorSpace> colorSpace,
273 int sampleCnt,
274 GrMipMapped mipMapped,
275 GrSurfaceOrigin origin,
276 const SkSurfaceProps* surfaceProps,
277 SkBudgeted budgeted) {
278 GrBackendFormat localFormat = format;
279 SkASSERT(sampleCnt > 0);
280 if (0 == this->caps()->getRenderTargetSampleCount(sampleCnt, config)) {
281 config = GrPixelConfigFallback(config);
282 // TODO: First we should be checking the getRenderTargetSampleCount from the GrBackendFormat
283 // and not GrPixelConfig. Besides that, we should implement the fallback in the caps, but
284 // for now we just convert the fallback pixel config to an SkColorType and then get the
285 // GrBackendFormat from that.
286 SkColorType colorType;
287 if (!GrPixelConfigToColorType(config, &colorType)) {
288 return nullptr;
289 }
290 localFormat = this->caps()->getBackendFormatFromColorType(colorType);
291 }
292
293 return this->makeDeferredRenderTargetContext(localFormat, fit, width, height, config,
294 std::move(colorSpace), sampleCnt, mipMapped,
295 origin, surfaceProps, budgeted);
296}
297
Robert Phillipsa41c6852019-02-07 10:44:10 -0500298///////////////////////////////////////////////////////////////////////////////////////////////////
299sk_sp<const GrCaps> GrRecordingContextPriv::refCaps() const {
300 return fContext->refCaps();
301}
302
303sk_sp<GrSkSLFPFactoryCache> GrRecordingContextPriv::fpFactoryCache() {
304 return fContext->fpFactoryCache();
305}
Robert Phillipsd6841482019-02-08 10:29:20 -0500306
307sk_sp<GrOpMemoryPool> GrRecordingContextPriv::refOpMemoryPool() {
308 return fContext->refOpMemoryPool();
309}
Robert Phillipsb97da532019-02-12 15:24:12 -0500310
Robert Phillipsc5058a62019-02-15 12:52:59 -0500311void GrRecordingContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
312 fContext->addOnFlushCallbackObject(onFlushCBObject);
313}
314
Robert Phillips292a6b22019-02-14 14:49:02 -0500315sk_sp<GrSurfaceContext> GrRecordingContextPriv::makeWrappedSurfaceContext(
316 sk_sp<GrSurfaceProxy> proxy,
317 sk_sp<SkColorSpace> colorSpace,
318 const SkSurfaceProps* props) {
319 return fContext->makeWrappedSurfaceContext(std::move(proxy), std::move(colorSpace), props);
320}
321
322sk_sp<GrSurfaceContext> GrRecordingContextPriv::makeDeferredSurfaceContext(
323 const GrBackendFormat& format,
324 const GrSurfaceDesc& dstDesc,
325 GrSurfaceOrigin origin,
326 GrMipMapped mipMapped,
327 SkBackingFit fit,
328 SkBudgeted isDstBudgeted,
329 sk_sp<SkColorSpace> colorSpace,
330 const SkSurfaceProps* props) {
331 return fContext->makeDeferredSurfaceContext(format, dstDesc, origin, mipMapped, fit,
332 isDstBudgeted, std::move(colorSpace), props);
333}
334
Robert Phillipsb97da532019-02-12 15:24:12 -0500335sk_sp<GrRenderTargetContext> GrRecordingContextPriv::makeDeferredRenderTargetContext(
336 const GrBackendFormat& format,
337 SkBackingFit fit,
338 int width, int height,
339 GrPixelConfig config,
340 sk_sp<SkColorSpace> colorSpace,
341 int sampleCnt,
342 GrMipMapped mipMapped,
343 GrSurfaceOrigin origin,
344 const SkSurfaceProps* surfaceProps,
345 SkBudgeted budgeted) {
346 return fContext->makeDeferredRenderTargetContext(format, fit, width, height, config,
347 std::move(colorSpace), sampleCnt, mipMapped,
348 origin, surfaceProps, budgeted);
349}
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500350
351sk_sp<GrRenderTargetContext> GrRecordingContextPriv::makeDeferredRenderTargetContextWithFallback(
352 const GrBackendFormat& format,
353 SkBackingFit fit,
354 int width, int height,
355 GrPixelConfig config,
356 sk_sp<SkColorSpace> colorSpace,
357 int sampleCnt,
358 GrMipMapped mipMapped,
359 GrSurfaceOrigin origin,
360 const SkSurfaceProps* surfaceProps,
361 SkBudgeted budgeted) {
362 return fContext->makeDeferredRenderTargetContextWithFallback(format, fit, width, height, config,
363 std::move(colorSpace), sampleCnt,
364 mipMapped, origin, surfaceProps,
365 budgeted);
366}
Robert Phillips9338c602019-02-19 12:52:29 -0500367
368GrContext* GrRecordingContextPriv::backdoor() {
369 return (GrContext*) fContext;
370}