blob: 68c7f482c732b333c141222f5891319b886751cf [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/private/GrAuditTrail.h"
9#include "include/private/GrOpList.h"
10#include "include/private/SkSemaphore.h"
11#include "src/core/SkMakeUnique.h"
12#include "src/core/SkTaskGroup.h"
13#include "src/core/SkTraceEvent.h"
14#include "src/gpu/GrCaps.h"
15#include "src/gpu/GrClip.h"
16#include "src/gpu/GrContextPriv.h"
17#include "src/gpu/GrDeferredProxyUploader.h"
18#include "src/gpu/GrGpuResourcePriv.h"
19#include "src/gpu/GrOpFlushState.h"
20#include "src/gpu/GrProxyProvider.h"
21#include "src/gpu/GrRecordingContextPriv.h"
Michael Ludwigaa1b6b32019-05-29 14:43:13 -040022#include "src/gpu/GrRenderTargetContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrSWMaskHelper.h"
24#include "src/gpu/GrShape.h"
25#include "src/gpu/GrSoftwarePathRenderer.h"
26#include "src/gpu/GrSurfaceContextPriv.h"
27#include "src/gpu/ops/GrDrawOp.h"
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000028
robertphillips@google.comed4155d2012-05-01 14:30:24 +000029////////////////////////////////////////////////////////////////////////////////
Chris Dalton5ed44232017-09-07 13:22:46 -060030GrPathRenderer::CanDrawPath
31GrSoftwarePathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
bsalomon8acedde2016-06-24 10:42:16 -070032 // Pass on any style that applies. The caller will apply the style if a suitable renderer is
33 // not found and try again with the new GrShape.
Robert Phillips1afd4cd2018-01-08 13:40:32 -050034 if (!args.fShape->style().applies() && SkToBool(fProxyProvider) &&
Chris Dalton09e56892019-03-13 00:22:01 -060035 ((args.fAATypeFlags & AATypeFlags::kCoverage) || args.fAATypeFlags == AATypeFlags::kNone)) {
Chris Dalton5ed44232017-09-07 13:22:46 -060036 // This is the fallback renderer for when a path is too complicated for the GPU ones.
37 return CanDrawPath::kAsBackup;
38 }
39 return CanDrawPath::kNo;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000040}
41
robertphillips@google.comed4155d2012-05-01 14:30:24 +000042////////////////////////////////////////////////////////////////////////////////
bsalomon39ef7fb2016-09-21 11:16:05 -070043static bool get_unclipped_shape_dev_bounds(const GrShape& shape, const SkMatrix& matrix,
44 SkIRect* devBounds) {
45 SkRect shapeBounds = shape.styledBounds();
46 if (shapeBounds.isEmpty()) {
47 return false;
48 }
49 SkRect shapeDevBounds;
50 matrix.mapRect(&shapeDevBounds, shapeBounds);
Brian Salomonc1c607e2016-12-20 11:41:43 -050051 // Even though these are "unclipped" bounds we still clip to the int32_t range.
52 // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints
53 // would round down to this value when cast to a float, but who really cares.
54 // INT32_MIN is exactly representable.
55 static constexpr int32_t kMaxInt = 2147483520;
56 if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) {
57 return false;
58 }
Jim Van Verthba7cf292017-11-02 20:18:56 +000059 // Make sure that the resulting SkIRect can have representable width and height
60 if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt ||
61 SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) {
62 return false;
63 }
bsalomon39ef7fb2016-09-21 11:16:05 -070064 shapeDevBounds.roundOut(devBounds);
65 return true;
66}
67
68// Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there
69// is no intersection.
Robert Phillips20390c32018-08-17 11:01:03 -040070bool GrSoftwarePathRenderer::GetShapeAndClipBounds(GrRenderTargetContext* renderTargetContext,
71 const GrClip& clip,
72 const GrShape& shape,
73 const SkMatrix& matrix,
74 SkIRect* unclippedDevShapeBounds,
75 SkIRect* clippedDevShapeBounds,
76 SkIRect* devClipBounds) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +000077 // compute bounds as intersection of rt size, clip, and path
Robert Phillips20390c32018-08-17 11:01:03 -040078 clip.getConservativeBounds(renderTargetContext->width(),
79 renderTargetContext->height(),
80 devClipBounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +000081
bsalomon39ef7fb2016-09-21 11:16:05 -070082 if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) {
83 *unclippedDevShapeBounds = SkIRect::EmptyIRect();
84 *clippedDevShapeBounds = SkIRect::EmptyIRect();
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000085 return false;
robertphillips@google.comed4155d2012-05-01 14:30:24 +000086 }
bsalomon39ef7fb2016-09-21 11:16:05 -070087 if (!clippedDevShapeBounds->intersect(*devClipBounds, *unclippedDevShapeBounds)) {
88 *clippedDevShapeBounds = SkIRect::EmptyIRect();
robertphillips@google.comed4155d2012-05-01 14:30:24 +000089 return false;
90 }
91 return true;
92}
93
94////////////////////////////////////////////////////////////////////////////////
robertphillips976f5f02016-06-03 10:59:20 -070095
Brian Osman11052242016-10-27 14:47:55 -040096void GrSoftwarePathRenderer::DrawNonAARect(GrRenderTargetContext* renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -050097 GrPaint&& paint,
robertphillipsd2b6d642016-07-21 08:55:08 -070098 const GrUserStencilSettings& userStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -070099 const GrClip& clip,
robertphillips976f5f02016-06-03 10:59:20 -0700100 const SkMatrix& viewMatrix,
101 const SkRect& rect,
102 const SkMatrix& localMatrix) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400103 renderTargetContext->priv().stencilRect(clip, &userStencilSettings, std::move(paint), GrAA::kNo,
104 viewMatrix, rect, &localMatrix);
robertphillips976f5f02016-06-03 10:59:20 -0700105}
106
Brian Osman11052242016-10-27 14:47:55 -0400107void GrSoftwarePathRenderer::DrawAroundInvPath(GrRenderTargetContext* renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500108 GrPaint&& paint,
robertphillipsd2b6d642016-07-21 08:55:08 -0700109 const GrUserStencilSettings& userStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -0700110 const GrClip& clip,
robertphillips976f5f02016-06-03 10:59:20 -0700111 const SkMatrix& viewMatrix,
112 const SkIRect& devClipBounds,
113 const SkIRect& devPathBounds) {
joshualittd27f73e2014-12-29 07:43:36 -0800114 SkMatrix invert;
joshualitt8059eb92014-12-29 15:10:07 -0800115 if (!viewMatrix.invert(&invert)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000116 return;
117 }
joshualittd27f73e2014-12-29 07:43:36 -0800118
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000119 SkRect rect;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000120 if (devClipBounds.fTop < devPathBounds.fTop) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000121 rect.iset(devClipBounds.fLeft, devClipBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000122 devClipBounds.fRight, devPathBounds.fTop);
Brian Salomonb74ef032017-08-10 12:46:01 -0400123 DrawNonAARect(renderTargetContext, GrPaint::Clone(paint), userStencilSettings, clip,
124 SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000125 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000126 if (devClipBounds.fLeft < devPathBounds.fLeft) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000127 rect.iset(devClipBounds.fLeft, devPathBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000128 devPathBounds.fLeft, devPathBounds.fBottom);
Brian Salomonb74ef032017-08-10 12:46:01 -0400129 DrawNonAARect(renderTargetContext, GrPaint::Clone(paint), userStencilSettings, clip,
130 SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000131 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000132 if (devClipBounds.fRight > devPathBounds.fRight) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000133 rect.iset(devPathBounds.fRight, devPathBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000134 devClipBounds.fRight, devPathBounds.fBottom);
Brian Salomonb74ef032017-08-10 12:46:01 -0400135 DrawNonAARect(renderTargetContext, GrPaint::Clone(paint), userStencilSettings, clip,
136 SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000137 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000138 if (devClipBounds.fBottom > devPathBounds.fBottom) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000139 rect.iset(devClipBounds.fLeft, devPathBounds.fBottom,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000140 devClipBounds.fRight, devClipBounds.fBottom);
Brian Salomon82f44312017-01-11 13:42:54 -0500141 DrawNonAARect(renderTargetContext, std::move(paint), userStencilSettings, clip,
robertphillips976f5f02016-06-03 10:59:20 -0700142 SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000143 }
144}
145
Brian Osmanc7da1462017-08-17 16:14:25 -0400146void GrSoftwarePathRenderer::DrawToTargetWithShapeMask(
147 sk_sp<GrTextureProxy> proxy,
148 GrRenderTargetContext* renderTargetContext,
149 GrPaint&& paint,
150 const GrUserStencilSettings& userStencilSettings,
151 const GrClip& clip,
152 const SkMatrix& viewMatrix,
153 const SkIPoint& textureOriginInDeviceSpace,
154 const SkIRect& deviceSpaceRectToDraw) {
155 SkMatrix invert;
156 if (!viewMatrix.invert(&invert)) {
157 return;
158 }
159
160 SkRect dstRect = SkRect::Make(deviceSpaceRectToDraw);
161
162 // We use device coords to compute the texture coordinates. We take the device coords and apply
163 // a translation so that the top-left of the device bounds maps to 0,0, and then a scaling
164 // matrix to normalized coords.
165 SkMatrix maskMatrix = SkMatrix::MakeTrans(SkIntToScalar(-textureOriginInDeviceSpace.fX),
166 SkIntToScalar(-textureOriginInDeviceSpace.fY));
167 maskMatrix.preConcat(viewMatrix);
168 paint.addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(
Brian Osman2240be92017-10-18 13:15:13 -0400169 std::move(proxy), maskMatrix, GrSamplerState::Filter::kNearest));
Brian Osmanf9810662017-08-30 10:02:10 -0400170 DrawNonAARect(renderTargetContext, std::move(paint), userStencilSettings, clip, SkMatrix::I(),
171 dstRect, invert);
172}
173
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500174static sk_sp<GrTextureProxy> make_deferred_mask_texture_proxy(GrRecordingContext* context,
175 SkBackingFit fit,
Brian Osmanf9810662017-08-30 10:02:10 -0400176 int width, int height) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500177 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500178
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
Greg Daniel4065d452018-11-16 15:43:41 -0500184 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500185 context->priv().caps()->getBackendFormatFromColorType(kAlpha_8_SkColorType);
Greg Daniel4065d452018-11-16 15:43:41 -0500186
Robert Phillips10d17212019-04-24 14:09:10 -0400187 return proxyProvider->createProxy(format, desc, kTopLeft_GrSurfaceOrigin, fit,
188 SkBudgeted::kYes);
Brian Osmanf9810662017-08-30 10:02:10 -0400189}
190
191namespace {
192
Brian Osman5d034742017-09-11 13:38:55 -0400193/**
Brian Osman099fa0f2017-10-02 16:38:32 -0400194 * Payload class for use with GrTDeferredProxyUploader. The software path renderer only draws
Brian Osman5d034742017-09-11 13:38:55 -0400195 * a single path into the mask texture. This stores all of the information needed by the worker
196 * thread's call to drawShape (see below, in onDrawPath).
197 */
198class SoftwarePathData {
Brian Osmanf9810662017-08-30 10:02:10 -0400199public:
Brian Osman5d034742017-09-11 13:38:55 -0400200 SoftwarePathData(const SkIRect& maskBounds, const SkMatrix& viewMatrix, const GrShape& shape,
201 GrAA aa)
202 : fMaskBounds(maskBounds)
Brian Osmanf9810662017-08-30 10:02:10 -0400203 , fViewMatrix(viewMatrix)
204 , fShape(shape)
Brian Osman5d034742017-09-11 13:38:55 -0400205 , fAA(aa) {}
Brian Osmanf9810662017-08-30 10:02:10 -0400206
Brian Osmanf9810662017-08-30 10:02:10 -0400207 const SkIRect& getMaskBounds() const { return fMaskBounds; }
208 const SkMatrix* getViewMatrix() const { return &fViewMatrix; }
209 const GrShape& getShape() const { return fShape; }
210 GrAA getAA() const { return fAA; }
211
212private:
Brian Osmanf9810662017-08-30 10:02:10 -0400213 SkIRect fMaskBounds;
214 SkMatrix fViewMatrix;
215 GrShape fShape;
216 GrAA fAA;
Brian Osmanf9810662017-08-30 10:02:10 -0400217};
218
Brian Osmane9242ca2017-09-26 14:05:19 -0400219// When the SkPathRef genID changes, invalidate a corresponding GrResource described by key.
220class PathInvalidator : public SkPathRef::GenIDChangeListener {
221public:
Brian Salomon238069b2018-07-11 15:58:57 -0400222 PathInvalidator(const GrUniqueKey& key, uint32_t contextUniqueID)
223 : fMsg(key, contextUniqueID) {}
224
Brian Osmane9242ca2017-09-26 14:05:19 -0400225private:
226 GrUniqueKeyInvalidatedMessage fMsg;
227
228 void onChange() override {
229 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg);
230 }
231};
232
Brian Osmanc7da1462017-08-17 16:14:25 -0400233}
234
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000235////////////////////////////////////////////////////////////////////////////////
236// return true on success; false on failure
bsalomon0aff2fa2015-07-31 06:48:27 -0700237bool GrSoftwarePathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400238 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700239 "GrSoftwarePathRenderer::onDrawPath");
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500240 if (!fProxyProvider) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000241 return false;
242 }
243
caryclarkd6562002016-07-27 12:02:07 -0700244 SkASSERT(!args.fShape->style().applies());
Robert Phillips20390c32018-08-17 11:01:03 -0400245 // We really need to know if the shape will be inverse filled or not
Eric Karl5c779752017-05-08 12:02:07 -0700246 // If the path is hairline, ignore inverse fill.
Robert Phillips20390c32018-08-17 11:01:03 -0400247 bool inverseFilled = args.fShape->inverseFilled() &&
248 !IsStrokeHairlineOrEquivalent(args.fShape->style(),
249 *args.fViewMatrix, nullptr);
bsalomon8acedde2016-06-24 10:42:16 -0700250
bsalomon39ef7fb2016-09-21 11:16:05 -0700251 SkIRect unclippedDevShapeBounds, clippedDevShapeBounds, devClipBounds;
252 // To prevent overloading the cache with entries during animations we limit the cache of masks
253 // to cases where the matrix preserves axis alignment.
254 bool useCache = fAllowCaching && !inverseFilled && args.fViewMatrix->preservesAxisAlignment() &&
Chris Dalton09e56892019-03-13 00:22:01 -0600255 args.fShape->hasUnstyledKey() && (AATypeFlags::kCoverage & args.fAATypeFlags);
bsalomon39ef7fb2016-09-21 11:16:05 -0700256
Robert Phillips20390c32018-08-17 11:01:03 -0400257 if (!GetShapeAndClipBounds(args.fRenderTargetContext,
258 *args.fClip, *args.fShape,
259 *args.fViewMatrix, &unclippedDevShapeBounds,
260 &clippedDevShapeBounds,
261 &devClipBounds)) {
bsalomon8acedde2016-06-24 10:42:16 -0700262 if (inverseFilled) {
Brian Salomon82f44312017-01-11 13:42:54 -0500263 DrawAroundInvPath(args.fRenderTargetContext, std::move(args.fPaint),
264 *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix,
265 devClipBounds, unclippedDevShapeBounds);
bsalomon@google.com276c1fa2012-06-19 13:22:45 +0000266 }
267 return true;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000268 }
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000269
bsalomon39ef7fb2016-09-21 11:16:05 -0700270 const SkIRect* boundsForMask = &clippedDevShapeBounds;
271 if (useCache) {
272 // Use the cache only if >50% of the path is visible.
273 int unclippedWidth = unclippedDevShapeBounds.width();
274 int unclippedHeight = unclippedDevShapeBounds.height();
Brian Osman1a0ea732017-09-28 11:53:03 -0400275 int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight);
276 int64_t clippedArea = sk_64_mul(clippedDevShapeBounds.width(),
277 clippedDevShapeBounds.height());
Brian Osman11052242016-10-27 14:47:55 -0400278 int maxTextureSize = args.fRenderTargetContext->caps()->maxTextureSize();
bsalomon39ef7fb2016-09-21 11:16:05 -0700279 if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
280 unclippedHeight > maxTextureSize) {
281 useCache = false;
282 } else {
283 boundsForMask = &unclippedDevShapeBounds;
284 }
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000285 }
286
bsalomon39ef7fb2016-09-21 11:16:05 -0700287 GrUniqueKey maskKey;
bsalomon39ef7fb2016-09-21 11:16:05 -0700288 if (useCache) {
289 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
290 SkScalar sx = args.fViewMatrix->get(SkMatrix::kMScaleX);
291 SkScalar sy = args.fViewMatrix->get(SkMatrix::kMScaleY);
292 SkScalar kx = args.fViewMatrix->get(SkMatrix::kMSkewX);
293 SkScalar ky = args.fViewMatrix->get(SkMatrix::kMSkewY);
Stan Iliev67cd6732017-08-15 17:10:26 -0400294 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Osmana4425262018-07-26 13:37:53 -0400295 GrUniqueKey::Builder builder(&maskKey, kDomain, 5 + args.fShape->unstyledKeySize(),
296 "SW Path Mask");
Stan Iliev67cd6732017-08-15 17:10:26 -0400297#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
298 // Fractional translate does not affect caching on Android. This is done for better cache
299 // hit ratio and speed, but it is matching HWUI behavior, which doesn't consider the matrix
300 // at all when caching paths.
Brian Osmana4425262018-07-26 13:37:53 -0400301 SkFixed fracX = 0;
302 SkFixed fracY = 0;
Stan Iliev67cd6732017-08-15 17:10:26 -0400303#else
bsalomon39ef7fb2016-09-21 11:16:05 -0700304 SkScalar tx = args.fViewMatrix->get(SkMatrix::kMTransX);
305 SkScalar ty = args.fViewMatrix->get(SkMatrix::kMTransY);
306 // Allow 8 bits each in x and y of subpixel positioning.
307 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
308 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
Stan Iliev67cd6732017-08-15 17:10:26 -0400309#endif
bsalomon39ef7fb2016-09-21 11:16:05 -0700310 builder[0] = SkFloat2Bits(sx);
311 builder[1] = SkFloat2Bits(sy);
312 builder[2] = SkFloat2Bits(kx);
313 builder[3] = SkFloat2Bits(ky);
Brian Osmana4425262018-07-26 13:37:53 -0400314 // Distinguish between hairline and filled paths. For hairlines, we also need to include
315 // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that
316 // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers
317 // all cases we might see.
318 uint32_t styleBits = args.fShape->style().isSimpleHairline() ?
319 ((args.fShape->style().strokeRec().getCap() << 1) | 1) : 0;
320 builder[4] = fracX | (fracY >> 8) | (styleBits << 16);
bsalomon39ef7fb2016-09-21 11:16:05 -0700321 args.fShape->writeUnstyledKey(&builder[5]);
322 }
323
Robert Phillipsd3749482017-03-14 09:17:43 -0400324 sk_sp<GrTextureProxy> proxy;
bsalomon39ef7fb2016-09-21 11:16:05 -0700325 if (useCache) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500326 proxy = fProxyProvider->findOrCreateProxyByUniqueKey(maskKey, kTopLeft_GrSurfaceOrigin);
bsalomon39ef7fb2016-09-21 11:16:05 -0700327 }
Robert Phillipsd3749482017-03-14 09:17:43 -0400328 if (!proxy) {
Robert Phillips417b7f42016-12-14 09:12:13 -0500329 SkBackingFit fit = useCache ? SkBackingFit::kExact : SkBackingFit::kApprox;
Chris Dalton09e56892019-03-13 00:22:01 -0600330 GrAA aa = GrAA(SkToBool(AATypeFlags::kCoverage & args.fAATypeFlags));
Brian Osmanf9810662017-08-30 10:02:10 -0400331
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500332 SkTaskGroup* taskGroup = nullptr;
333 if (auto direct = args.fContext->priv().asDirectContext()) {
334 taskGroup = direct->priv().getTaskGroup();
335 }
336
Brian Osmanf9810662017-08-30 10:02:10 -0400337 if (taskGroup) {
338 proxy = make_deferred_mask_texture_proxy(args.fContext, fit,
339 boundsForMask->width(),
340 boundsForMask->height());
341 if (!proxy) {
342 return false;
343 }
344
Brian Osman099fa0f2017-10-02 16:38:32 -0400345 auto uploader = skstd::make_unique<GrTDeferredProxyUploader<SoftwarePathData>>(
346 *boundsForMask, *args.fViewMatrix, *args.fShape, aa);
347 GrTDeferredProxyUploader<SoftwarePathData>* uploaderRaw = uploader.get();
Brian Osmanf9810662017-08-30 10:02:10 -0400348
349 auto drawAndUploadMask = [uploaderRaw] {
350 TRACE_EVENT0("skia", "Threaded SW Mask Render");
351 GrSWMaskHelper helper(uploaderRaw->getPixels());
Brian Osman5d034742017-09-11 13:38:55 -0400352 if (helper.init(uploaderRaw->data().getMaskBounds())) {
353 helper.drawShape(uploaderRaw->data().getShape(),
354 *uploaderRaw->data().getViewMatrix(),
355 SkRegion::kReplace_Op, uploaderRaw->data().getAA(), 0xFF);
Brian Osmanf9810662017-08-30 10:02:10 -0400356 } else {
357 SkDEBUGFAIL("Unable to allocate SW mask.");
358 }
Brian Osman099fa0f2017-10-02 16:38:32 -0400359 uploaderRaw->signalAndFreeData();
Brian Osmanf9810662017-08-30 10:02:10 -0400360 };
361 taskGroup->add(std::move(drawAndUploadMask));
Brian Osman099fa0f2017-10-02 16:38:32 -0400362 proxy->texPriv().setDeferredUploader(std::move(uploader));
Brian Osmanf9810662017-08-30 10:02:10 -0400363 } else {
364 GrSWMaskHelper helper;
Brian Salomon74077562017-08-30 13:55:35 -0400365 if (!helper.init(*boundsForMask)) {
Brian Osmanf9810662017-08-30 10:02:10 -0400366 return false;
367 }
Brian Salomon74077562017-08-30 13:55:35 -0400368 helper.drawShape(*args.fShape, *args.fViewMatrix, SkRegion::kReplace_Op, aa, 0xFF);
Brian Osmanf9810662017-08-30 10:02:10 -0400369 proxy = helper.toTextureProxy(args.fContext, fit);
370 }
371
Robert Phillipsd3749482017-03-14 09:17:43 -0400372 if (!proxy) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500373 return false;
374 }
375 if (useCache) {
Robert Phillipse44ef102017-07-21 15:37:19 -0400376 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500377 fProxyProvider->assignUniqueKeyToProxy(maskKey, proxy.get());
Robert Phillips869fe562018-09-17 14:55:49 -0400378 args.fShape->addGenIDChangeListener(
Robert Phillips9da87e02019-02-04 13:26:26 -0500379 sk_make_sp<PathInvalidator>(maskKey, args.fContext->priv().contextID()));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500380 }
bsalomon39ef7fb2016-09-21 11:16:05 -0700381 }
bsalomon8acedde2016-06-24 10:42:16 -0700382 if (inverseFilled) {
Brian Salomonb74ef032017-08-10 12:46:01 -0400383 DrawAroundInvPath(args.fRenderTargetContext, GrPaint::Clone(args.fPaint),
Brian Salomon82f44312017-01-11 13:42:54 -0500384 *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix, devClipBounds,
385 unclippedDevShapeBounds);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000386 }
Brian Osmanc7da1462017-08-17 16:14:25 -0400387 DrawToTargetWithShapeMask(
Robert Phillips296b1cc2017-03-15 10:42:12 -0400388 std::move(proxy), args.fRenderTargetContext, std::move(args.fPaint),
Brian Salomon82f44312017-01-11 13:42:54 -0500389 *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix,
390 SkIPoint{boundsForMask->fLeft, boundsForMask->fTop}, *boundsForMask);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000391
392 return true;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000393}