blob: 05b27957a4144baeb60ad8d0bacd799806796bdd [file] [log] [blame]
robertphillips@google.comf4c2c522012-04-27 12:08:47 +00001/*
2 * Copyright 2012 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 "GrSoftwarePathRenderer.h"
robertphillips976f5f02016-06-03 10:59:20 -07009#include "GrAuditTrail.h"
10#include "GrClip.h"
Brian Osmanf9810662017-08-30 10:02:10 -040011#include "GrContextPriv.h"
Brian Osman099fa0f2017-10-02 16:38:32 -040012#include "GrDeferredProxyUploader.h"
bsalomon39ef7fb2016-09-21 11:16:05 -070013#include "GrGpuResourcePriv.h"
Brian Osmanf9810662017-08-30 10:02:10 -040014#include "GrOpFlushState.h"
15#include "GrOpList.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050016#include "GrProxyProvider.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000017#include "GrSWMaskHelper.h"
Brian Salomon653f42f2018-07-10 10:07:31 -040018#include "GrShape.h"
19#include "GrSurfaceContextPriv.h"
Brian Osmanf9810662017-08-30 10:02:10 -040020#include "SkMakeUnique.h"
21#include "SkSemaphore.h"
22#include "SkTaskGroup.h"
23#include "SkTraceEvent.h"
Robert Phillips009e9af2017-06-15 14:01:04 -040024#include "ops/GrDrawOp.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040025#include "ops/GrRectOpFactory.h"
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000026
robertphillips@google.comed4155d2012-05-01 14:30:24 +000027////////////////////////////////////////////////////////////////////////////////
Chris Dalton5ed44232017-09-07 13:22:46 -060028GrPathRenderer::CanDrawPath
29GrSoftwarePathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
bsalomon8acedde2016-06-24 10:42:16 -070030 // Pass on any style that applies. The caller will apply the style if a suitable renderer is
31 // not found and try again with the new GrShape.
Robert Phillips1afd4cd2018-01-08 13:40:32 -050032 if (!args.fShape->style().applies() && SkToBool(fProxyProvider) &&
Chris Dalton5ed44232017-09-07 13:22:46 -060033 (args.fAAType == GrAAType::kCoverage || args.fAAType == GrAAType::kNone)) {
34 // This is the fallback renderer for when a path is too complicated for the GPU ones.
35 return CanDrawPath::kAsBackup;
36 }
37 return CanDrawPath::kNo;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000038}
39
robertphillips@google.comed4155d2012-05-01 14:30:24 +000040////////////////////////////////////////////////////////////////////////////////
bsalomon39ef7fb2016-09-21 11:16:05 -070041static bool get_unclipped_shape_dev_bounds(const GrShape& shape, const SkMatrix& matrix,
42 SkIRect* devBounds) {
43 SkRect shapeBounds = shape.styledBounds();
44 if (shapeBounds.isEmpty()) {
45 return false;
46 }
47 SkRect shapeDevBounds;
48 matrix.mapRect(&shapeDevBounds, shapeBounds);
Brian Salomonc1c607e2016-12-20 11:41:43 -050049 // Even though these are "unclipped" bounds we still clip to the int32_t range.
50 // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints
51 // would round down to this value when cast to a float, but who really cares.
52 // INT32_MIN is exactly representable.
53 static constexpr int32_t kMaxInt = 2147483520;
54 if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) {
55 return false;
56 }
Jim Van Verthba7cf292017-11-02 20:18:56 +000057 // Make sure that the resulting SkIRect can have representable width and height
58 if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt ||
59 SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) {
60 return false;
61 }
bsalomon39ef7fb2016-09-21 11:16:05 -070062 shapeDevBounds.roundOut(devBounds);
63 return true;
64}
65
66// Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there
67// is no intersection.
Robert Phillips20390c32018-08-17 11:01:03 -040068bool GrSoftwarePathRenderer::GetShapeAndClipBounds(GrRenderTargetContext* renderTargetContext,
69 const GrClip& clip,
70 const GrShape& shape,
71 const SkMatrix& matrix,
72 SkIRect* unclippedDevShapeBounds,
73 SkIRect* clippedDevShapeBounds,
74 SkIRect* devClipBounds) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +000075 // compute bounds as intersection of rt size, clip, and path
Robert Phillips20390c32018-08-17 11:01:03 -040076 clip.getConservativeBounds(renderTargetContext->width(),
77 renderTargetContext->height(),
78 devClipBounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +000079
bsalomon39ef7fb2016-09-21 11:16:05 -070080 if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) {
81 *unclippedDevShapeBounds = SkIRect::EmptyIRect();
82 *clippedDevShapeBounds = SkIRect::EmptyIRect();
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000083 return false;
robertphillips@google.comed4155d2012-05-01 14:30:24 +000084 }
bsalomon39ef7fb2016-09-21 11:16:05 -070085 if (!clippedDevShapeBounds->intersect(*devClipBounds, *unclippedDevShapeBounds)) {
86 *clippedDevShapeBounds = SkIRect::EmptyIRect();
robertphillips@google.comed4155d2012-05-01 14:30:24 +000087 return false;
88 }
89 return true;
90}
91
92////////////////////////////////////////////////////////////////////////////////
robertphillips976f5f02016-06-03 10:59:20 -070093
Brian Osman11052242016-10-27 14:47:55 -040094void GrSoftwarePathRenderer::DrawNonAARect(GrRenderTargetContext* renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -050095 GrPaint&& paint,
robertphillipsd2b6d642016-07-21 08:55:08 -070096 const GrUserStencilSettings& userStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -070097 const GrClip& clip,
robertphillips976f5f02016-06-03 10:59:20 -070098 const SkMatrix& viewMatrix,
99 const SkRect& rect,
100 const SkMatrix& localMatrix) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400101 GrContext* context = renderTargetContext->surfPriv().getContext();
Brian Salomonbaaf4392017-06-15 09:59:23 -0400102 renderTargetContext->addDrawOp(clip,
103 GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
Robert Phillips7c525e62018-06-12 10:11:12 -0400104 context, std::move(paint), viewMatrix, localMatrix, rect,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400105 GrAAType::kNone, &userStencilSettings));
robertphillips976f5f02016-06-03 10:59:20 -0700106}
107
Brian Osman11052242016-10-27 14:47:55 -0400108void GrSoftwarePathRenderer::DrawAroundInvPath(GrRenderTargetContext* renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500109 GrPaint&& paint,
robertphillipsd2b6d642016-07-21 08:55:08 -0700110 const GrUserStencilSettings& userStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -0700111 const GrClip& clip,
robertphillips976f5f02016-06-03 10:59:20 -0700112 const SkMatrix& viewMatrix,
113 const SkIRect& devClipBounds,
114 const SkIRect& devPathBounds) {
joshualittd27f73e2014-12-29 07:43:36 -0800115 SkMatrix invert;
joshualitt8059eb92014-12-29 15:10:07 -0800116 if (!viewMatrix.invert(&invert)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000117 return;
118 }
joshualittd27f73e2014-12-29 07:43:36 -0800119
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000120 SkRect rect;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000121 if (devClipBounds.fTop < devPathBounds.fTop) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000122 rect.iset(devClipBounds.fLeft, devClipBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000123 devClipBounds.fRight, devPathBounds.fTop);
Brian Salomonb74ef032017-08-10 12:46:01 -0400124 DrawNonAARect(renderTargetContext, GrPaint::Clone(paint), userStencilSettings, clip,
125 SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000126 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000127 if (devClipBounds.fLeft < devPathBounds.fLeft) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000128 rect.iset(devClipBounds.fLeft, devPathBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000129 devPathBounds.fLeft, devPathBounds.fBottom);
Brian Salomonb74ef032017-08-10 12:46:01 -0400130 DrawNonAARect(renderTargetContext, GrPaint::Clone(paint), userStencilSettings, clip,
131 SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000132 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000133 if (devClipBounds.fRight > devPathBounds.fRight) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000134 rect.iset(devPathBounds.fRight, devPathBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000135 devClipBounds.fRight, devPathBounds.fBottom);
Brian Salomonb74ef032017-08-10 12:46:01 -0400136 DrawNonAARect(renderTargetContext, GrPaint::Clone(paint), userStencilSettings, clip,
137 SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000138 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000139 if (devClipBounds.fBottom > devPathBounds.fBottom) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000140 rect.iset(devClipBounds.fLeft, devPathBounds.fBottom,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000141 devClipBounds.fRight, devClipBounds.fBottom);
Brian Salomon82f44312017-01-11 13:42:54 -0500142 DrawNonAARect(renderTargetContext, std::move(paint), userStencilSettings, clip,
robertphillips976f5f02016-06-03 10:59:20 -0700143 SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000144 }
145}
146
Brian Osmanc7da1462017-08-17 16:14:25 -0400147void GrSoftwarePathRenderer::DrawToTargetWithShapeMask(
148 sk_sp<GrTextureProxy> proxy,
149 GrRenderTargetContext* renderTargetContext,
150 GrPaint&& paint,
151 const GrUserStencilSettings& userStencilSettings,
152 const GrClip& clip,
153 const SkMatrix& viewMatrix,
154 const SkIPoint& textureOriginInDeviceSpace,
155 const SkIRect& deviceSpaceRectToDraw) {
156 SkMatrix invert;
157 if (!viewMatrix.invert(&invert)) {
158 return;
159 }
160
161 SkRect dstRect = SkRect::Make(deviceSpaceRectToDraw);
162
163 // We use device coords to compute the texture coordinates. We take the device coords and apply
164 // a translation so that the top-left of the device bounds maps to 0,0, and then a scaling
165 // matrix to normalized coords.
166 SkMatrix maskMatrix = SkMatrix::MakeTrans(SkIntToScalar(-textureOriginInDeviceSpace.fX),
167 SkIntToScalar(-textureOriginInDeviceSpace.fY));
168 maskMatrix.preConcat(viewMatrix);
169 paint.addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(
Brian Osman2240be92017-10-18 13:15:13 -0400170 std::move(proxy), maskMatrix, GrSamplerState::Filter::kNearest));
Brian Osmanf9810662017-08-30 10:02:10 -0400171 DrawNonAARect(renderTargetContext, std::move(paint), userStencilSettings, clip, SkMatrix::I(),
172 dstRect, invert);
173}
174
175static sk_sp<GrTextureProxy> make_deferred_mask_texture_proxy(GrContext* context, SkBackingFit fit,
176 int width, int height) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500177 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
178
Brian Osmanf9810662017-08-30 10:02:10 -0400179 GrSurfaceDesc desc;
Brian Osmanf9810662017-08-30 10:02:10 -0400180 desc.fWidth = width;
181 desc.fHeight = height;
182 desc.fConfig = kAlpha_8_GrPixelConfig;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500183
Brian Osmand140fe92017-10-03 12:17:26 -0400184 // MDB TODO: We're going to fill this proxy with an ASAP upload (which is out of order wrt to
185 // ops), so it can't have any pending IO.
Brian Salomon2a4f9832018-03-03 22:43:43 -0500186 return proxyProvider->createProxy(desc, kTopLeft_GrSurfaceOrigin, fit, SkBudgeted::kYes,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400187 GrInternalSurfaceFlags::kNoPendingIO);
Brian Osmanf9810662017-08-30 10:02:10 -0400188}
189
190namespace {
191
Brian Osman5d034742017-09-11 13:38:55 -0400192/**
Brian Osman099fa0f2017-10-02 16:38:32 -0400193 * Payload class for use with GrTDeferredProxyUploader. The software path renderer only draws
Brian Osman5d034742017-09-11 13:38:55 -0400194 * a single path into the mask texture. This stores all of the information needed by the worker
195 * thread's call to drawShape (see below, in onDrawPath).
196 */
197class SoftwarePathData {
Brian Osmanf9810662017-08-30 10:02:10 -0400198public:
Brian Osman5d034742017-09-11 13:38:55 -0400199 SoftwarePathData(const SkIRect& maskBounds, const SkMatrix& viewMatrix, const GrShape& shape,
200 GrAA aa)
201 : fMaskBounds(maskBounds)
Brian Osmanf9810662017-08-30 10:02:10 -0400202 , fViewMatrix(viewMatrix)
203 , fShape(shape)
Brian Osman5d034742017-09-11 13:38:55 -0400204 , fAA(aa) {}
Brian Osmanf9810662017-08-30 10:02:10 -0400205
Brian Osmanf9810662017-08-30 10:02:10 -0400206 const SkIRect& getMaskBounds() const { return fMaskBounds; }
207 const SkMatrix* getViewMatrix() const { return &fViewMatrix; }
208 const GrShape& getShape() const { return fShape; }
209 GrAA getAA() const { return fAA; }
210
211private:
Brian Osmanf9810662017-08-30 10:02:10 -0400212 SkIRect fMaskBounds;
213 SkMatrix fViewMatrix;
214 GrShape fShape;
215 GrAA fAA;
Brian Osmanf9810662017-08-30 10:02:10 -0400216};
217
Brian Osmane9242ca2017-09-26 14:05:19 -0400218// When the SkPathRef genID changes, invalidate a corresponding GrResource described by key.
219class PathInvalidator : public SkPathRef::GenIDChangeListener {
220public:
Brian Salomon238069b2018-07-11 15:58:57 -0400221 PathInvalidator(const GrUniqueKey& key, uint32_t contextUniqueID)
222 : fMsg(key, contextUniqueID) {}
223
Brian Osmane9242ca2017-09-26 14:05:19 -0400224private:
225 GrUniqueKeyInvalidatedMessage fMsg;
226
227 void onChange() override {
228 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg);
229 }
230};
231
Brian Osmanc7da1462017-08-17 16:14:25 -0400232}
233
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000234////////////////////////////////////////////////////////////////////////////////
235// return true on success; false on failure
bsalomon0aff2fa2015-07-31 06:48:27 -0700236bool GrSoftwarePathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400237 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700238 "GrSoftwarePathRenderer::onDrawPath");
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500239 if (!fProxyProvider) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000240 return false;
241 }
242
caryclarkd6562002016-07-27 12:02:07 -0700243 SkASSERT(!args.fShape->style().applies());
Robert Phillips20390c32018-08-17 11:01:03 -0400244 // We really need to know if the shape will be inverse filled or not
Eric Karl5c779752017-05-08 12:02:07 -0700245 // If the path is hairline, ignore inverse fill.
Robert Phillips20390c32018-08-17 11:01:03 -0400246 bool inverseFilled = args.fShape->inverseFilled() &&
247 !IsStrokeHairlineOrEquivalent(args.fShape->style(),
248 *args.fViewMatrix, nullptr);
bsalomon8acedde2016-06-24 10:42:16 -0700249
bsalomon39ef7fb2016-09-21 11:16:05 -0700250 SkIRect unclippedDevShapeBounds, clippedDevShapeBounds, devClipBounds;
251 // To prevent overloading the cache with entries during animations we limit the cache of masks
252 // to cases where the matrix preserves axis alignment.
253 bool useCache = fAllowCaching && !inverseFilled && args.fViewMatrix->preservesAxisAlignment() &&
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500254 args.fShape->hasUnstyledKey() && GrAAType::kCoverage == args.fAAType;
bsalomon39ef7fb2016-09-21 11:16:05 -0700255
Robert Phillips20390c32018-08-17 11:01:03 -0400256 if (!GetShapeAndClipBounds(args.fRenderTargetContext,
257 *args.fClip, *args.fShape,
258 *args.fViewMatrix, &unclippedDevShapeBounds,
259 &clippedDevShapeBounds,
260 &devClipBounds)) {
bsalomon8acedde2016-06-24 10:42:16 -0700261 if (inverseFilled) {
Brian Salomon82f44312017-01-11 13:42:54 -0500262 DrawAroundInvPath(args.fRenderTargetContext, std::move(args.fPaint),
263 *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix,
264 devClipBounds, unclippedDevShapeBounds);
bsalomon@google.com276c1fa2012-06-19 13:22:45 +0000265 }
266 return true;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000267 }
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000268
bsalomon39ef7fb2016-09-21 11:16:05 -0700269 const SkIRect* boundsForMask = &clippedDevShapeBounds;
270 if (useCache) {
271 // Use the cache only if >50% of the path is visible.
272 int unclippedWidth = unclippedDevShapeBounds.width();
273 int unclippedHeight = unclippedDevShapeBounds.height();
Brian Osman1a0ea732017-09-28 11:53:03 -0400274 int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight);
275 int64_t clippedArea = sk_64_mul(clippedDevShapeBounds.width(),
276 clippedDevShapeBounds.height());
Brian Osman11052242016-10-27 14:47:55 -0400277 int maxTextureSize = args.fRenderTargetContext->caps()->maxTextureSize();
bsalomon39ef7fb2016-09-21 11:16:05 -0700278 if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
279 unclippedHeight > maxTextureSize) {
280 useCache = false;
281 } else {
282 boundsForMask = &unclippedDevShapeBounds;
283 }
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000284 }
285
bsalomon39ef7fb2016-09-21 11:16:05 -0700286 GrUniqueKey maskKey;
bsalomon39ef7fb2016-09-21 11:16:05 -0700287 if (useCache) {
288 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
289 SkScalar sx = args.fViewMatrix->get(SkMatrix::kMScaleX);
290 SkScalar sy = args.fViewMatrix->get(SkMatrix::kMScaleY);
291 SkScalar kx = args.fViewMatrix->get(SkMatrix::kMSkewX);
292 SkScalar ky = args.fViewMatrix->get(SkMatrix::kMSkewY);
Stan Iliev67cd6732017-08-15 17:10:26 -0400293 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Osmana4425262018-07-26 13:37:53 -0400294 GrUniqueKey::Builder builder(&maskKey, kDomain, 5 + args.fShape->unstyledKeySize(),
295 "SW Path Mask");
Stan Iliev67cd6732017-08-15 17:10:26 -0400296#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
297 // Fractional translate does not affect caching on Android. This is done for better cache
298 // hit ratio and speed, but it is matching HWUI behavior, which doesn't consider the matrix
299 // at all when caching paths.
Brian Osmana4425262018-07-26 13:37:53 -0400300 SkFixed fracX = 0;
301 SkFixed fracY = 0;
Stan Iliev67cd6732017-08-15 17:10:26 -0400302#else
bsalomon39ef7fb2016-09-21 11:16:05 -0700303 SkScalar tx = args.fViewMatrix->get(SkMatrix::kMTransX);
304 SkScalar ty = args.fViewMatrix->get(SkMatrix::kMTransY);
305 // Allow 8 bits each in x and y of subpixel positioning.
306 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
307 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
Stan Iliev67cd6732017-08-15 17:10:26 -0400308#endif
bsalomon39ef7fb2016-09-21 11:16:05 -0700309 builder[0] = SkFloat2Bits(sx);
310 builder[1] = SkFloat2Bits(sy);
311 builder[2] = SkFloat2Bits(kx);
312 builder[3] = SkFloat2Bits(ky);
Brian Osmana4425262018-07-26 13:37:53 -0400313 // Distinguish between hairline and filled paths. For hairlines, we also need to include
314 // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that
315 // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers
316 // all cases we might see.
317 uint32_t styleBits = args.fShape->style().isSimpleHairline() ?
318 ((args.fShape->style().strokeRec().getCap() << 1) | 1) : 0;
319 builder[4] = fracX | (fracY >> 8) | (styleBits << 16);
bsalomon39ef7fb2016-09-21 11:16:05 -0700320 args.fShape->writeUnstyledKey(&builder[5]);
321 }
322
Robert Phillipsd3749482017-03-14 09:17:43 -0400323 sk_sp<GrTextureProxy> proxy;
bsalomon39ef7fb2016-09-21 11:16:05 -0700324 if (useCache) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500325 proxy = fProxyProvider->findOrCreateProxyByUniqueKey(maskKey, kTopLeft_GrSurfaceOrigin);
bsalomon39ef7fb2016-09-21 11:16:05 -0700326 }
Robert Phillipsd3749482017-03-14 09:17:43 -0400327 if (!proxy) {
Robert Phillips417b7f42016-12-14 09:12:13 -0500328 SkBackingFit fit = useCache ? SkBackingFit::kExact : SkBackingFit::kApprox;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500329 GrAA aa = GrAAType::kCoverage == args.fAAType ? GrAA::kYes : GrAA::kNo;
Brian Osmanf9810662017-08-30 10:02:10 -0400330
331 SkTaskGroup* taskGroup = args.fContext->contextPriv().getTaskGroup();
332 if (taskGroup) {
333 proxy = make_deferred_mask_texture_proxy(args.fContext, fit,
334 boundsForMask->width(),
335 boundsForMask->height());
336 if (!proxy) {
337 return false;
338 }
339
Brian Osman099fa0f2017-10-02 16:38:32 -0400340 auto uploader = skstd::make_unique<GrTDeferredProxyUploader<SoftwarePathData>>(
341 *boundsForMask, *args.fViewMatrix, *args.fShape, aa);
342 GrTDeferredProxyUploader<SoftwarePathData>* uploaderRaw = uploader.get();
Brian Osmanf9810662017-08-30 10:02:10 -0400343
344 auto drawAndUploadMask = [uploaderRaw] {
345 TRACE_EVENT0("skia", "Threaded SW Mask Render");
346 GrSWMaskHelper helper(uploaderRaw->getPixels());
Brian Osman5d034742017-09-11 13:38:55 -0400347 if (helper.init(uploaderRaw->data().getMaskBounds())) {
348 helper.drawShape(uploaderRaw->data().getShape(),
349 *uploaderRaw->data().getViewMatrix(),
350 SkRegion::kReplace_Op, uploaderRaw->data().getAA(), 0xFF);
Brian Osmanf9810662017-08-30 10:02:10 -0400351 } else {
352 SkDEBUGFAIL("Unable to allocate SW mask.");
353 }
Brian Osman099fa0f2017-10-02 16:38:32 -0400354 uploaderRaw->signalAndFreeData();
Brian Osmanf9810662017-08-30 10:02:10 -0400355 };
356 taskGroup->add(std::move(drawAndUploadMask));
Brian Osman099fa0f2017-10-02 16:38:32 -0400357 proxy->texPriv().setDeferredUploader(std::move(uploader));
Brian Osmanf9810662017-08-30 10:02:10 -0400358 } else {
359 GrSWMaskHelper helper;
Brian Salomon74077562017-08-30 13:55:35 -0400360 if (!helper.init(*boundsForMask)) {
Brian Osmanf9810662017-08-30 10:02:10 -0400361 return false;
362 }
Brian Salomon74077562017-08-30 13:55:35 -0400363 helper.drawShape(*args.fShape, *args.fViewMatrix, SkRegion::kReplace_Op, aa, 0xFF);
Brian Osmanf9810662017-08-30 10:02:10 -0400364 proxy = helper.toTextureProxy(args.fContext, fit);
365 }
366
Robert Phillipsd3749482017-03-14 09:17:43 -0400367 if (!proxy) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500368 return false;
369 }
370 if (useCache) {
Robert Phillipse44ef102017-07-21 15:37:19 -0400371 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500372 fProxyProvider->assignUniqueKeyToProxy(maskKey, proxy.get());
Robert Phillips869fe562018-09-17 14:55:49 -0400373 args.fShape->addGenIDChangeListener(
374 sk_make_sp<PathInvalidator>(maskKey, args.fContext->uniqueID()));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500375 }
bsalomon39ef7fb2016-09-21 11:16:05 -0700376 }
bsalomon8acedde2016-06-24 10:42:16 -0700377 if (inverseFilled) {
Brian Salomonb74ef032017-08-10 12:46:01 -0400378 DrawAroundInvPath(args.fRenderTargetContext, GrPaint::Clone(args.fPaint),
Brian Salomon82f44312017-01-11 13:42:54 -0500379 *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix, devClipBounds,
380 unclippedDevShapeBounds);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000381 }
Brian Osmanc7da1462017-08-17 16:14:25 -0400382 DrawToTargetWithShapeMask(
Robert Phillips296b1cc2017-03-15 10:42:12 -0400383 std::move(proxy), args.fRenderTargetContext, std::move(args.fPaint),
Brian Salomon82f44312017-01-11 13:42:54 -0500384 *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix,
385 SkIPoint{boundsForMask->fLeft, boundsForMask->fTop}, *boundsForMask);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000386
387 return true;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000388}