blob: a3d1ecda1283022dd1cc19c0d6f2a37b84f667be [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/SkSemaphore.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/core/SkTaskGroup.h"
10#include "src/core/SkTraceEvent.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/GrClip.h"
14#include "src/gpu/GrContextPriv.h"
15#include "src/gpu/GrDeferredProxyUploader.h"
16#include "src/gpu/GrGpuResourcePriv.h"
17#include "src/gpu/GrOpFlushState.h"
18#include "src/gpu/GrProxyProvider.h"
19#include "src/gpu/GrRecordingContextPriv.h"
Michael Ludwigaa1b6b32019-05-29 14:43:13 -040020#include "src/gpu/GrRenderTargetContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrSWMaskHelper.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrSoftwarePathRenderer.h"
23#include "src/gpu/GrSurfaceContextPriv.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040024#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/ops/GrDrawOp.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 Dalton6ce447a2019-06-23 18:07:38 -060033 (args.fAAType == GrAAType::kCoverage || args.fAAType == GrAAType::kNone)) {
Chris Dalton5ed44232017-09-07 13:22:46 -060034 // 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)) {
Brian Salomon44207f32020-01-06 15:20:18 -050081 *unclippedDevShapeBounds = SkIRect::MakeEmpty();
82 *clippedDevShapeBounds = SkIRect::MakeEmpty();
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)) {
Brian Salomon44207f32020-01-06 15:20:18 -050086 *clippedDevShapeBounds = SkIRect::MakeEmpty();
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) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400101 renderTargetContext->priv().stencilRect(clip, &userStencilSettings, std::move(paint), GrAA::kNo,
102 viewMatrix, rect, &localMatrix);
robertphillips976f5f02016-06-03 10:59:20 -0700103}
104
Brian Osman11052242016-10-27 14:47:55 -0400105void GrSoftwarePathRenderer::DrawAroundInvPath(GrRenderTargetContext* renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500106 GrPaint&& paint,
robertphillipsd2b6d642016-07-21 08:55:08 -0700107 const GrUserStencilSettings& userStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -0700108 const GrClip& clip,
robertphillips976f5f02016-06-03 10:59:20 -0700109 const SkMatrix& viewMatrix,
110 const SkIRect& devClipBounds,
111 const SkIRect& devPathBounds) {
joshualittd27f73e2014-12-29 07:43:36 -0800112 SkMatrix invert;
joshualitt8059eb92014-12-29 15:10:07 -0800113 if (!viewMatrix.invert(&invert)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000114 return;
115 }
joshualittd27f73e2014-12-29 07:43:36 -0800116
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000117 SkRect rect;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000118 if (devClipBounds.fTop < devPathBounds.fTop) {
Mike Reed92b33352019-08-24 19:39:13 -0400119 rect.setLTRB(SkIntToScalar(devClipBounds.fLeft), SkIntToScalar(devClipBounds.fTop),
120 SkIntToScalar(devClipBounds.fRight), SkIntToScalar(devPathBounds.fTop));
Brian Salomonb74ef032017-08-10 12:46:01 -0400121 DrawNonAARect(renderTargetContext, GrPaint::Clone(paint), userStencilSettings, clip,
122 SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000123 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000124 if (devClipBounds.fLeft < devPathBounds.fLeft) {
Mike Reed92b33352019-08-24 19:39:13 -0400125 rect.setLTRB(SkIntToScalar(devClipBounds.fLeft), SkIntToScalar(devPathBounds.fTop),
126 SkIntToScalar(devPathBounds.fLeft), SkIntToScalar(devPathBounds.fBottom));
Brian Salomonb74ef032017-08-10 12:46:01 -0400127 DrawNonAARect(renderTargetContext, GrPaint::Clone(paint), userStencilSettings, clip,
128 SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000129 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000130 if (devClipBounds.fRight > devPathBounds.fRight) {
Mike Reed92b33352019-08-24 19:39:13 -0400131 rect.setLTRB(SkIntToScalar(devPathBounds.fRight), SkIntToScalar(devPathBounds.fTop),
132 SkIntToScalar(devClipBounds.fRight), SkIntToScalar(devPathBounds.fBottom));
Brian Salomonb74ef032017-08-10 12:46:01 -0400133 DrawNonAARect(renderTargetContext, GrPaint::Clone(paint), userStencilSettings, clip,
134 SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000135 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000136 if (devClipBounds.fBottom > devPathBounds.fBottom) {
Mike Reed92b33352019-08-24 19:39:13 -0400137 rect.setLTRB(SkIntToScalar(devClipBounds.fLeft), SkIntToScalar(devPathBounds.fBottom),
138 SkIntToScalar(devClipBounds.fRight), SkIntToScalar(devClipBounds.fBottom));
Brian Salomon82f44312017-01-11 13:42:54 -0500139 DrawNonAARect(renderTargetContext, std::move(paint), userStencilSettings, clip,
robertphillips976f5f02016-06-03 10:59:20 -0700140 SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000141 }
142}
143
Brian Osmanc7da1462017-08-17 16:14:25 -0400144void GrSoftwarePathRenderer::DrawToTargetWithShapeMask(
145 sk_sp<GrTextureProxy> proxy,
146 GrRenderTargetContext* renderTargetContext,
147 GrPaint&& paint,
148 const GrUserStencilSettings& userStencilSettings,
149 const GrClip& clip,
150 const SkMatrix& viewMatrix,
151 const SkIPoint& textureOriginInDeviceSpace,
152 const SkIRect& deviceSpaceRectToDraw) {
153 SkMatrix invert;
154 if (!viewMatrix.invert(&invert)) {
155 return;
156 }
157
158 SkRect dstRect = SkRect::Make(deviceSpaceRectToDraw);
159
160 // We use device coords to compute the texture coordinates. We take the device coords and apply
161 // a translation so that the top-left of the device bounds maps to 0,0, and then a scaling
162 // matrix to normalized coords.
163 SkMatrix maskMatrix = SkMatrix::MakeTrans(SkIntToScalar(-textureOriginInDeviceSpace.fX),
164 SkIntToScalar(-textureOriginInDeviceSpace.fY));
165 maskMatrix.preConcat(viewMatrix);
Brian Salomonb8f098d2020-01-07 11:15:44 -0500166 paint.addCoverageFragmentProcessor(GrTextureEffect::Make(
Brian Salomonfc118442019-11-22 19:09:27 -0500167 std::move(proxy), kPremul_SkAlphaType, maskMatrix, GrSamplerState::Filter::kNearest));
Brian Osmanf9810662017-08-30 10:02:10 -0400168 DrawNonAARect(renderTargetContext, std::move(paint), userStencilSettings, clip, SkMatrix::I(),
169 dstRect, invert);
170}
171
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500172static sk_sp<GrTextureProxy> make_deferred_mask_texture_proxy(GrRecordingContext* context,
173 SkBackingFit fit,
Brian Osmanf9810662017-08-30 10:02:10 -0400174 int width, int height) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500175 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips0a15cc62019-07-30 12:49:10 -0400176 const GrCaps* caps = context->priv().caps();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500177
Brian Osmanf9810662017-08-30 10:02:10 -0400178 GrSurfaceDesc desc;
Brian Osmanf9810662017-08-30 10:02:10 -0400179 desc.fWidth = width;
180 desc.fHeight = height;
181 desc.fConfig = kAlpha_8_GrPixelConfig;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500182
Robert Phillips0a15cc62019-07-30 12:49:10 -0400183 const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kAlpha_8,
184 GrRenderable::kNo);
Greg Daniel4065d452018-11-16 15:43:41 -0500185
Greg Daniel47c20e82020-01-21 14:29:57 -0500186 GrSwizzle swizzle = caps->getReadSwizzle(format, GrColorType::kAlpha_8);
187
188 return proxyProvider->createProxy(format, desc, swizzle, GrRenderable::kNo, 1,
189 kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, fit,
190 SkBudgeted::kYes, GrProtected::kNo);
Brian Osmanf9810662017-08-30 10:02:10 -0400191}
192
193namespace {
194
Brian Osman5d034742017-09-11 13:38:55 -0400195/**
Brian Osman099fa0f2017-10-02 16:38:32 -0400196 * Payload class for use with GrTDeferredProxyUploader. The software path renderer only draws
Brian Osman5d034742017-09-11 13:38:55 -0400197 * a single path into the mask texture. This stores all of the information needed by the worker
198 * thread's call to drawShape (see below, in onDrawPath).
199 */
200class SoftwarePathData {
Brian Osmanf9810662017-08-30 10:02:10 -0400201public:
Brian Osman5d034742017-09-11 13:38:55 -0400202 SoftwarePathData(const SkIRect& maskBounds, const SkMatrix& viewMatrix, const GrShape& shape,
203 GrAA aa)
204 : fMaskBounds(maskBounds)
Brian Osmanf9810662017-08-30 10:02:10 -0400205 , fViewMatrix(viewMatrix)
206 , fShape(shape)
Brian Osman5d034742017-09-11 13:38:55 -0400207 , fAA(aa) {}
Brian Osmanf9810662017-08-30 10:02:10 -0400208
Brian Osmanf9810662017-08-30 10:02:10 -0400209 const SkIRect& getMaskBounds() const { return fMaskBounds; }
210 const SkMatrix* getViewMatrix() const { return &fViewMatrix; }
211 const GrShape& getShape() const { return fShape; }
212 GrAA getAA() const { return fAA; }
213
214private:
Brian Osmanf9810662017-08-30 10:02:10 -0400215 SkIRect fMaskBounds;
216 SkMatrix fViewMatrix;
217 GrShape fShape;
218 GrAA fAA;
Brian Osmanf9810662017-08-30 10:02:10 -0400219};
220
Brian Osmane9242ca2017-09-26 14:05:19 -0400221// When the SkPathRef genID changes, invalidate a corresponding GrResource described by key.
222class PathInvalidator : public SkPathRef::GenIDChangeListener {
223public:
Brian Salomon238069b2018-07-11 15:58:57 -0400224 PathInvalidator(const GrUniqueKey& key, uint32_t contextUniqueID)
225 : fMsg(key, contextUniqueID) {}
226
Brian Osmane9242ca2017-09-26 14:05:19 -0400227private:
228 GrUniqueKeyInvalidatedMessage fMsg;
229
230 void onChange() override {
231 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg);
232 }
233};
234
Brian Osmanc7da1462017-08-17 16:14:25 -0400235}
236
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000237////////////////////////////////////////////////////////////////////////////////
238// return true on success; false on failure
bsalomon0aff2fa2015-07-31 06:48:27 -0700239bool GrSoftwarePathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400240 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700241 "GrSoftwarePathRenderer::onDrawPath");
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500242 if (!fProxyProvider) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000243 return false;
244 }
245
caryclarkd6562002016-07-27 12:02:07 -0700246 SkASSERT(!args.fShape->style().applies());
Robert Phillips20390c32018-08-17 11:01:03 -0400247 // We really need to know if the shape will be inverse filled or not
Eric Karl5c779752017-05-08 12:02:07 -0700248 // If the path is hairline, ignore inverse fill.
Robert Phillips20390c32018-08-17 11:01:03 -0400249 bool inverseFilled = args.fShape->inverseFilled() &&
250 !IsStrokeHairlineOrEquivalent(args.fShape->style(),
251 *args.fViewMatrix, nullptr);
bsalomon8acedde2016-06-24 10:42:16 -0700252
bsalomon39ef7fb2016-09-21 11:16:05 -0700253 SkIRect unclippedDevShapeBounds, clippedDevShapeBounds, devClipBounds;
254 // To prevent overloading the cache with entries during animations we limit the cache of masks
255 // to cases where the matrix preserves axis alignment.
256 bool useCache = fAllowCaching && !inverseFilled && args.fViewMatrix->preservesAxisAlignment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -0600257 args.fShape->hasUnstyledKey() && (GrAAType::kCoverage == args.fAAType);
bsalomon39ef7fb2016-09-21 11:16:05 -0700258
Robert Phillips20390c32018-08-17 11:01:03 -0400259 if (!GetShapeAndClipBounds(args.fRenderTargetContext,
260 *args.fClip, *args.fShape,
261 *args.fViewMatrix, &unclippedDevShapeBounds,
262 &clippedDevShapeBounds,
263 &devClipBounds)) {
bsalomon8acedde2016-06-24 10:42:16 -0700264 if (inverseFilled) {
Brian Salomon82f44312017-01-11 13:42:54 -0500265 DrawAroundInvPath(args.fRenderTargetContext, std::move(args.fPaint),
266 *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix,
267 devClipBounds, unclippedDevShapeBounds);
bsalomon@google.com276c1fa2012-06-19 13:22:45 +0000268 }
269 return true;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000270 }
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000271
bsalomon39ef7fb2016-09-21 11:16:05 -0700272 const SkIRect* boundsForMask = &clippedDevShapeBounds;
273 if (useCache) {
274 // Use the cache only if >50% of the path is visible.
275 int unclippedWidth = unclippedDevShapeBounds.width();
276 int unclippedHeight = unclippedDevShapeBounds.height();
Brian Osman1a0ea732017-09-28 11:53:03 -0400277 int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight);
278 int64_t clippedArea = sk_64_mul(clippedDevShapeBounds.width(),
279 clippedDevShapeBounds.height());
Brian Osman11052242016-10-27 14:47:55 -0400280 int maxTextureSize = args.fRenderTargetContext->caps()->maxTextureSize();
bsalomon39ef7fb2016-09-21 11:16:05 -0700281 if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
282 unclippedHeight > maxTextureSize) {
283 useCache = false;
284 } else {
285 boundsForMask = &unclippedDevShapeBounds;
286 }
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000287 }
288
bsalomon39ef7fb2016-09-21 11:16:05 -0700289 GrUniqueKey maskKey;
bsalomon39ef7fb2016-09-21 11:16:05 -0700290 if (useCache) {
291 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
292 SkScalar sx = args.fViewMatrix->get(SkMatrix::kMScaleX);
293 SkScalar sy = args.fViewMatrix->get(SkMatrix::kMScaleY);
294 SkScalar kx = args.fViewMatrix->get(SkMatrix::kMSkewX);
295 SkScalar ky = args.fViewMatrix->get(SkMatrix::kMSkewY);
Stan Iliev67cd6732017-08-15 17:10:26 -0400296 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Brian Osmana4425262018-07-26 13:37:53 -0400297 GrUniqueKey::Builder builder(&maskKey, kDomain, 5 + args.fShape->unstyledKeySize(),
298 "SW Path Mask");
Stan Iliev67cd6732017-08-15 17:10:26 -0400299#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
300 // Fractional translate does not affect caching on Android. This is done for better cache
301 // hit ratio and speed, but it is matching HWUI behavior, which doesn't consider the matrix
302 // at all when caching paths.
Brian Osmana4425262018-07-26 13:37:53 -0400303 SkFixed fracX = 0;
304 SkFixed fracY = 0;
Stan Iliev67cd6732017-08-15 17:10:26 -0400305#else
bsalomon39ef7fb2016-09-21 11:16:05 -0700306 SkScalar tx = args.fViewMatrix->get(SkMatrix::kMTransX);
307 SkScalar ty = args.fViewMatrix->get(SkMatrix::kMTransY);
308 // Allow 8 bits each in x and y of subpixel positioning.
309 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
310 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
Stan Iliev67cd6732017-08-15 17:10:26 -0400311#endif
bsalomon39ef7fb2016-09-21 11:16:05 -0700312 builder[0] = SkFloat2Bits(sx);
313 builder[1] = SkFloat2Bits(sy);
314 builder[2] = SkFloat2Bits(kx);
315 builder[3] = SkFloat2Bits(ky);
Brian Osmana4425262018-07-26 13:37:53 -0400316 // Distinguish between hairline and filled paths. For hairlines, we also need to include
317 // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that
318 // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers
319 // all cases we might see.
320 uint32_t styleBits = args.fShape->style().isSimpleHairline() ?
321 ((args.fShape->style().strokeRec().getCap() << 1) | 1) : 0;
322 builder[4] = fracX | (fracY >> 8) | (styleBits << 16);
bsalomon39ef7fb2016-09-21 11:16:05 -0700323 args.fShape->writeUnstyledKey(&builder[5]);
324 }
325
Robert Phillipsd3749482017-03-14 09:17:43 -0400326 sk_sp<GrTextureProxy> proxy;
bsalomon39ef7fb2016-09-21 11:16:05 -0700327 if (useCache) {
Brian Salomon2af3e702019-08-11 19:10:31 -0400328 proxy = fProxyProvider->findOrCreateProxyByUniqueKey(maskKey, GrColorType::kAlpha_8,
329 kTopLeft_GrSurfaceOrigin);
bsalomon39ef7fb2016-09-21 11:16:05 -0700330 }
Robert Phillipsd3749482017-03-14 09:17:43 -0400331 if (!proxy) {
Robert Phillips417b7f42016-12-14 09:12:13 -0500332 SkBackingFit fit = useCache ? SkBackingFit::kExact : SkBackingFit::kApprox;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600333 GrAA aa = GrAA(GrAAType::kCoverage == args.fAAType);
Brian Osmanf9810662017-08-30 10:02:10 -0400334
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500335 SkTaskGroup* taskGroup = nullptr;
336 if (auto direct = args.fContext->priv().asDirectContext()) {
337 taskGroup = direct->priv().getTaskGroup();
338 }
339
Brian Osmanf9810662017-08-30 10:02:10 -0400340 if (taskGroup) {
341 proxy = make_deferred_mask_texture_proxy(args.fContext, fit,
342 boundsForMask->width(),
343 boundsForMask->height());
344 if (!proxy) {
345 return false;
346 }
347
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500348 auto uploader = std::make_unique<GrTDeferredProxyUploader<SoftwarePathData>>(
Brian Osman099fa0f2017-10-02 16:38:32 -0400349 *boundsForMask, *args.fViewMatrix, *args.fShape, aa);
350 GrTDeferredProxyUploader<SoftwarePathData>* uploaderRaw = uploader.get();
Brian Osmanf9810662017-08-30 10:02:10 -0400351
352 auto drawAndUploadMask = [uploaderRaw] {
Brian Salomon5f394272019-07-02 14:07:49 -0400353 TRACE_EVENT0("skia.gpu", "Threaded SW Mask Render");
Brian Osmanf9810662017-08-30 10:02:10 -0400354 GrSWMaskHelper helper(uploaderRaw->getPixels());
Brian Osman5d034742017-09-11 13:38:55 -0400355 if (helper.init(uploaderRaw->data().getMaskBounds())) {
356 helper.drawShape(uploaderRaw->data().getShape(),
357 *uploaderRaw->data().getViewMatrix(),
358 SkRegion::kReplace_Op, uploaderRaw->data().getAA(), 0xFF);
Brian Osmanf9810662017-08-30 10:02:10 -0400359 } else {
360 SkDEBUGFAIL("Unable to allocate SW mask.");
361 }
Brian Osman099fa0f2017-10-02 16:38:32 -0400362 uploaderRaw->signalAndFreeData();
Brian Osmanf9810662017-08-30 10:02:10 -0400363 };
364 taskGroup->add(std::move(drawAndUploadMask));
Brian Osman099fa0f2017-10-02 16:38:32 -0400365 proxy->texPriv().setDeferredUploader(std::move(uploader));
Brian Osmanf9810662017-08-30 10:02:10 -0400366 } else {
367 GrSWMaskHelper helper;
Brian Salomon74077562017-08-30 13:55:35 -0400368 if (!helper.init(*boundsForMask)) {
Brian Osmanf9810662017-08-30 10:02:10 -0400369 return false;
370 }
Brian Salomon74077562017-08-30 13:55:35 -0400371 helper.drawShape(*args.fShape, *args.fViewMatrix, SkRegion::kReplace_Op, aa, 0xFF);
Brian Osmanf9810662017-08-30 10:02:10 -0400372 proxy = helper.toTextureProxy(args.fContext, fit);
373 }
374
Robert Phillipsd3749482017-03-14 09:17:43 -0400375 if (!proxy) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500376 return false;
377 }
378 if (useCache) {
Robert Phillipse44ef102017-07-21 15:37:19 -0400379 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500380 fProxyProvider->assignUniqueKeyToProxy(maskKey, proxy.get());
Robert Phillips869fe562018-09-17 14:55:49 -0400381 args.fShape->addGenIDChangeListener(
Robert Phillips9da87e02019-02-04 13:26:26 -0500382 sk_make_sp<PathInvalidator>(maskKey, args.fContext->priv().contextID()));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500383 }
bsalomon39ef7fb2016-09-21 11:16:05 -0700384 }
bsalomon8acedde2016-06-24 10:42:16 -0700385 if (inverseFilled) {
Brian Salomonb74ef032017-08-10 12:46:01 -0400386 DrawAroundInvPath(args.fRenderTargetContext, GrPaint::Clone(args.fPaint),
Brian Salomon82f44312017-01-11 13:42:54 -0500387 *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix, devClipBounds,
388 unclippedDevShapeBounds);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000389 }
Brian Salomonfc118442019-11-22 19:09:27 -0500390 DrawToTargetWithShapeMask(std::move(proxy), args.fRenderTargetContext, std::move(args.fPaint),
391 *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix,
392 SkIPoint{boundsForMask->fLeft, boundsForMask->fTop}, *boundsForMask);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000393
394 return true;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000395}