blob: d3138f1d44bdb507046a65d2126ea1528205b879 [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
Brian Salomon99a813c2020-03-02 12:50:47 -05008#include "src/gpu/GrSoftwarePathRenderer.h"
9
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040010#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/SkSemaphore.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/core/SkTaskGroup.h"
13#include "src/core/SkTraceEvent.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040014#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrCaps.h"
16#include "src/gpu/GrClip.h"
Adlai Hollera0693042020-10-14 11:23:11 -040017#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrGpuResourcePriv.h"
19#include "src/gpu/GrOpFlushState.h"
20#include "src/gpu/GrProxyProvider.h"
21#include "src/gpu/GrRecordingContextPriv.h"
22#include "src/gpu/GrSWMaskHelper.h"
Brian Salomon99a813c2020-03-02 12:50:47 -050023#include "src/gpu/SkGr.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000024#include "src/gpu/geometry/GrStyledShape.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
Michael Ludwig2686d692020-04-17 20:21:37 +000031 // not found and try again with the new GrStyledShape.
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////////////////////////////////////////////////////////////////////////////////
Michael Ludwig2686d692020-04-17 20:21:37 +000041static bool get_unclipped_shape_dev_bounds(const GrStyledShape& shape, const SkMatrix& matrix,
bsalomon39ef7fb2016-09-21 11:16:05 -070042 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.
Brian Salomon1aa1f5f2020-12-11 17:25:17 -050068bool GrSoftwarePathRenderer::GetShapeAndClipBounds(GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -040069 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +000070 const GrStyledShape& shape,
Robert Phillips20390c32018-08-17 11:01:03 -040071 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
Michael Ludwige06a8972020-06-11 10:29:00 -040076 *devClipBounds = clip ? clip->getConservativeBounds()
Brian Salomon1aa1f5f2020-12-11 17:25:17 -050077 : SkIRect::MakeWH(surfaceDrawContext->width(),
78 surfaceDrawContext->height());
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 Salomon1aa1f5f2020-12-11 17:25:17 -050094void GrSoftwarePathRenderer::DrawNonAARect(GrSurfaceDrawContext* surfaceDrawContext,
Brian Salomon82f44312017-01-11 13:42:54 -050095 GrPaint&& paint,
robertphillipsd2b6d642016-07-21 08:55:08 -070096 const GrUserStencilSettings& userStencilSettings,
Michael Ludwig7c12e282020-05-29 09:54:07 -040097 const GrClip* clip,
robertphillips976f5f02016-06-03 10:59:20 -070098 const SkMatrix& viewMatrix,
99 const SkRect& rect,
100 const SkMatrix& localMatrix) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500101 surfaceDrawContext->stencilRect(clip, &userStencilSettings, std::move(paint), GrAA::kNo,
102 viewMatrix, rect, &localMatrix);
robertphillips976f5f02016-06-03 10:59:20 -0700103}
104
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500105void GrSoftwarePathRenderer::DrawAroundInvPath(GrSurfaceDrawContext* surfaceDrawContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500106 GrPaint&& paint,
robertphillipsd2b6d642016-07-21 08:55:08 -0700107 const GrUserStencilSettings& userStencilSettings,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400108 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 Salomon1aa1f5f2020-12-11 17:25:17 -0500121 DrawNonAARect(surfaceDrawContext, GrPaint::Clone(paint), userStencilSettings, clip,
Brian Salomonb74ef032017-08-10 12:46:01 -0400122 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 Salomon1aa1f5f2020-12-11 17:25:17 -0500127 DrawNonAARect(surfaceDrawContext, GrPaint::Clone(paint), userStencilSettings, clip,
Brian Salomonb74ef032017-08-10 12:46:01 -0400128 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 Salomon1aa1f5f2020-12-11 17:25:17 -0500133 DrawNonAARect(surfaceDrawContext, GrPaint::Clone(paint), userStencilSettings, clip,
Brian Salomonb74ef032017-08-10 12:46:01 -0400134 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 Salomon1aa1f5f2020-12-11 17:25:17 -0500139 DrawNonAARect(surfaceDrawContext, 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(
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500145 GrSurfaceProxyView view,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500146 GrSurfaceDrawContext* surfaceDrawContext,
Brian Osmanc7da1462017-08-17 16:14:25 -0400147 GrPaint&& paint,
148 const GrUserStencilSettings& userStencilSettings,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400149 const GrClip* clip,
Brian Osmanc7da1462017-08-17 16:14:25 -0400150 const SkMatrix& viewMatrix,
151 const SkIPoint& textureOriginInDeviceSpace,
152 const SkIRect& deviceSpaceRectToDraw) {
153 SkMatrix invert;
154 if (!viewMatrix.invert(&invert)) {
155 return;
156 }
157
Brian Salomonb43d6992021-01-05 14:37:40 -0500158 view.concatSwizzle(GrSwizzle("aaaa"));
159
Brian Osmanc7da1462017-08-17 16:14:25 -0400160 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.
Mike Reed1f607332020-05-21 12:11:27 -0400165 SkMatrix maskMatrix = SkMatrix::Translate(SkIntToScalar(-textureOriginInDeviceSpace.fX),
Brian Osmanc7da1462017-08-17 16:14:25 -0400166 SkIntToScalar(-textureOriginInDeviceSpace.fY));
167 maskMatrix.preConcat(viewMatrix);
Greg Danield2ccbb52020-02-05 10:45:39 -0500168
John Stiles41d91b62020-07-21 14:39:40 -0400169 paint.setCoverageFragmentProcessor(GrTextureEffect::Make(
Greg Danield2ccbb52020-02-05 10:45:39 -0500170 std::move(view), kPremul_SkAlphaType, maskMatrix, GrSamplerState::Filter::kNearest));
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500171 DrawNonAARect(surfaceDrawContext, std::move(paint), userStencilSettings, clip, SkMatrix::I(),
Brian Osmanf9810662017-08-30 10:02:10 -0400172 dstRect, invert);
173}
174
Brian Osmanf9810662017-08-30 10:02:10 -0400175namespace {
176
Brian Osman5d034742017-09-11 13:38:55 -0400177/**
Adlai Holler990a0d82021-02-05 13:40:51 -0500178 * Payload class for use with GrSWMaskHelper. The software path renderer only draws
Brian Osman5d034742017-09-11 13:38:55 -0400179 * a single path into the mask texture. This stores all of the information needed by the worker
180 * thread's call to drawShape (see below, in onDrawPath).
181 */
182class SoftwarePathData {
Brian Osmanf9810662017-08-30 10:02:10 -0400183public:
Michael Ludwig2686d692020-04-17 20:21:37 +0000184 SoftwarePathData(const SkIRect& maskBounds, const SkMatrix& viewMatrix,
185 const GrStyledShape& shape, GrAA aa)
Brian Osman5d034742017-09-11 13:38:55 -0400186 : fMaskBounds(maskBounds)
Brian Osmanf9810662017-08-30 10:02:10 -0400187 , fViewMatrix(viewMatrix)
188 , fShape(shape)
Brian Osman5d034742017-09-11 13:38:55 -0400189 , fAA(aa) {}
Brian Osmanf9810662017-08-30 10:02:10 -0400190
Brian Osmanf9810662017-08-30 10:02:10 -0400191 const SkIRect& getMaskBounds() const { return fMaskBounds; }
Adlai Holler990a0d82021-02-05 13:40:51 -0500192 const SkMatrix& getViewMatrix() const { return fViewMatrix; }
Michael Ludwig2686d692020-04-17 20:21:37 +0000193 const GrStyledShape& getShape() const { return fShape; }
Brian Osmanf9810662017-08-30 10:02:10 -0400194 GrAA getAA() const { return fAA; }
195
196private:
Brian Osmanf9810662017-08-30 10:02:10 -0400197 SkIRect fMaskBounds;
198 SkMatrix fViewMatrix;
Michael Ludwig2686d692020-04-17 20:21:37 +0000199 GrStyledShape fShape;
Brian Osmanf9810662017-08-30 10:02:10 -0400200 GrAA fAA;
Brian Osmanf9810662017-08-30 10:02:10 -0400201};
202
John Stilesa6841be2020-08-06 14:11:56 -0400203} // namespace
Brian Osmanc7da1462017-08-17 16:14:25 -0400204
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000205////////////////////////////////////////////////////////////////////////////////
206// return true on success; false on failure
bsalomon0aff2fa2015-07-31 06:48:27 -0700207bool GrSoftwarePathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400208 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700209 "GrSoftwarePathRenderer::onDrawPath");
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500210 if (!fProxyProvider) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000211 return false;
212 }
213
caryclarkd6562002016-07-27 12:02:07 -0700214 SkASSERT(!args.fShape->style().applies());
Robert Phillips20390c32018-08-17 11:01:03 -0400215 // We really need to know if the shape will be inverse filled or not
Eric Karl5c779752017-05-08 12:02:07 -0700216 // If the path is hairline, ignore inverse fill.
Robert Phillips20390c32018-08-17 11:01:03 -0400217 bool inverseFilled = args.fShape->inverseFilled() &&
218 !IsStrokeHairlineOrEquivalent(args.fShape->style(),
219 *args.fViewMatrix, nullptr);
bsalomon8acedde2016-06-24 10:42:16 -0700220
bsalomon39ef7fb2016-09-21 11:16:05 -0700221 SkIRect unclippedDevShapeBounds, clippedDevShapeBounds, devClipBounds;
222 // To prevent overloading the cache with entries during animations we limit the cache of masks
223 // to cases where the matrix preserves axis alignment.
224 bool useCache = fAllowCaching && !inverseFilled && args.fViewMatrix->preservesAxisAlignment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -0600225 args.fShape->hasUnstyledKey() && (GrAAType::kCoverage == args.fAAType);
bsalomon39ef7fb2016-09-21 11:16:05 -0700226
Robert Phillips20390c32018-08-17 11:01:03 -0400227 if (!GetShapeAndClipBounds(args.fRenderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400228 args.fClip, *args.fShape,
Robert Phillips20390c32018-08-17 11:01:03 -0400229 *args.fViewMatrix, &unclippedDevShapeBounds,
230 &clippedDevShapeBounds,
231 &devClipBounds)) {
bsalomon8acedde2016-06-24 10:42:16 -0700232 if (inverseFilled) {
Brian Salomon82f44312017-01-11 13:42:54 -0500233 DrawAroundInvPath(args.fRenderTargetContext, std::move(args.fPaint),
Michael Ludwig7c12e282020-05-29 09:54:07 -0400234 *args.fUserStencilSettings, args.fClip, *args.fViewMatrix,
Brian Salomon82f44312017-01-11 13:42:54 -0500235 devClipBounds, unclippedDevShapeBounds);
bsalomon@google.com276c1fa2012-06-19 13:22:45 +0000236 }
237 return true;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000238 }
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000239
bsalomon39ef7fb2016-09-21 11:16:05 -0700240 const SkIRect* boundsForMask = &clippedDevShapeBounds;
241 if (useCache) {
242 // Use the cache only if >50% of the path is visible.
243 int unclippedWidth = unclippedDevShapeBounds.width();
244 int unclippedHeight = unclippedDevShapeBounds.height();
Brian Osman1a0ea732017-09-28 11:53:03 -0400245 int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight);
246 int64_t clippedArea = sk_64_mul(clippedDevShapeBounds.width(),
247 clippedDevShapeBounds.height());
Brian Osman11052242016-10-27 14:47:55 -0400248 int maxTextureSize = args.fRenderTargetContext->caps()->maxTextureSize();
bsalomon39ef7fb2016-09-21 11:16:05 -0700249 if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
250 unclippedHeight > maxTextureSize) {
251 useCache = false;
252 } else {
253 boundsForMask = &unclippedDevShapeBounds;
254 }
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000255 }
256
bsalomon39ef7fb2016-09-21 11:16:05 -0700257 GrUniqueKey maskKey;
bsalomon39ef7fb2016-09-21 11:16:05 -0700258 if (useCache) {
259 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
260 SkScalar sx = args.fViewMatrix->get(SkMatrix::kMScaleX);
261 SkScalar sy = args.fViewMatrix->get(SkMatrix::kMScaleY);
262 SkScalar kx = args.fViewMatrix->get(SkMatrix::kMSkewX);
263 SkScalar ky = args.fViewMatrix->get(SkMatrix::kMSkewY);
Stan Iliev67cd6732017-08-15 17:10:26 -0400264 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Robert Phillipsc5727d82020-05-05 10:37:20 -0400265 GrUniqueKey::Builder builder(&maskKey, kDomain, 7 + args.fShape->unstyledKeySize(),
Brian Osmana4425262018-07-26 13:37:53 -0400266 "SW Path Mask");
Robert Phillipsc5727d82020-05-05 10:37:20 -0400267 builder[0] = boundsForMask->width();
268 builder[1] = boundsForMask->height();
Robert Phillipsd58a2702020-05-01 12:27:56 -0400269
Stan Iliev67cd6732017-08-15 17:10:26 -0400270#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
271 // Fractional translate does not affect caching on Android. This is done for better cache
272 // hit ratio and speed, but it is matching HWUI behavior, which doesn't consider the matrix
273 // at all when caching paths.
Brian Osmana4425262018-07-26 13:37:53 -0400274 SkFixed fracX = 0;
275 SkFixed fracY = 0;
Stan Iliev67cd6732017-08-15 17:10:26 -0400276#else
bsalomon39ef7fb2016-09-21 11:16:05 -0700277 SkScalar tx = args.fViewMatrix->get(SkMatrix::kMTransX);
278 SkScalar ty = args.fViewMatrix->get(SkMatrix::kMTransY);
279 // Allow 8 bits each in x and y of subpixel positioning.
280 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
281 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
Stan Iliev67cd6732017-08-15 17:10:26 -0400282#endif
Robert Phillipsc5727d82020-05-05 10:37:20 -0400283 builder[2] = SkFloat2Bits(sx);
284 builder[3] = SkFloat2Bits(sy);
285 builder[4] = SkFloat2Bits(kx);
286 builder[5] = SkFloat2Bits(ky);
Brian Osmana4425262018-07-26 13:37:53 -0400287 // Distinguish between hairline and filled paths. For hairlines, we also need to include
288 // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that
289 // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers
290 // all cases we might see.
291 uint32_t styleBits = args.fShape->style().isSimpleHairline() ?
292 ((args.fShape->style().strokeRec().getCap() << 1) | 1) : 0;
Robert Phillipsc5727d82020-05-05 10:37:20 -0400293 builder[6] = fracX | (fracY >> 8) | (styleBits << 16);
294 args.fShape->writeUnstyledKey(&builder[7]);
bsalomon39ef7fb2016-09-21 11:16:05 -0700295 }
296
Robert Phillipsd3749482017-03-14 09:17:43 -0400297 sk_sp<GrTextureProxy> proxy;
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500298 GrSurfaceProxyView view;
bsalomon39ef7fb2016-09-21 11:16:05 -0700299 if (useCache) {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400300 auto proxy = fProxyProvider->findOrCreateProxyByUniqueKey(maskKey);
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500301 if (proxy) {
302 GrSwizzle swizzle = args.fRenderTargetContext->caps()->getReadSwizzle(
303 proxy->backendFormat(), GrColorType::kAlpha_8);
304 view = {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
Robert Phillips273f1072020-05-05 13:03:07 -0400305 args.fContext->priv().stats()->incNumPathMasksCacheHits();
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500306 }
bsalomon39ef7fb2016-09-21 11:16:05 -0700307 }
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500308 if (!view) {
Robert Phillips417b7f42016-12-14 09:12:13 -0500309 SkBackingFit fit = useCache ? SkBackingFit::kExact : SkBackingFit::kApprox;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600310 GrAA aa = GrAA(GrAAType::kCoverage == args.fAAType);
Adlai Holler990a0d82021-02-05 13:40:51 -0500311 SoftwarePathData data(*boundsForMask, *args.fViewMatrix, *args.fShape, aa);
312 view = GrSWMaskHelper::MakeTexture(data.getMaskBounds(),
313 args.fContext,
314 fit,
315 [data{std::move(data)}](GrSWMaskHelper* helper) {
316 TRACE_EVENT0("skia.gpu", "SW Mask Render");
317 helper->drawShape(data.getShape(), data.getViewMatrix(), SkRegion::kReplace_Op,
318 data.getAA(), 0xFF);
319 });
Brian Osmanf9810662017-08-30 10:02:10 -0400320
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500321 if (!view) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500322 return false;
323 }
324 if (useCache) {
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500325 SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon4282d292020-02-24 09:39:32 -0500326
327 // We will add an invalidator to the path so that if the path goes away we will
Brian Salomon99a813c2020-03-02 12:50:47 -0500328 // delete or recycle the mask texture.
329 auto listener = GrMakeUniqueKeyInvalidationListener(&maskKey,
330 args.fContext->priv().contextID());
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500331 fProxyProvider->assignUniqueKeyToProxy(maskKey, view.asTextureProxy());
Brian Salomon99a813c2020-03-02 12:50:47 -0500332 args.fShape->addGenIDChangeListener(std::move(listener));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500333 }
Robert Phillips273f1072020-05-05 13:03:07 -0400334
335 args.fContext->priv().stats()->incNumPathMasksGenerated();
bsalomon39ef7fb2016-09-21 11:16:05 -0700336 }
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500337 SkASSERT(view);
bsalomon8acedde2016-06-24 10:42:16 -0700338 if (inverseFilled) {
Brian Salomonb74ef032017-08-10 12:46:01 -0400339 DrawAroundInvPath(args.fRenderTargetContext, GrPaint::Clone(args.fPaint),
Michael Ludwig7c12e282020-05-29 09:54:07 -0400340 *args.fUserStencilSettings, args.fClip, *args.fViewMatrix, devClipBounds,
Brian Salomon82f44312017-01-11 13:42:54 -0500341 unclippedDevShapeBounds);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000342 }
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500343 DrawToTargetWithShapeMask(std::move(view), args.fRenderTargetContext, std::move(args.fPaint),
Michael Ludwig7c12e282020-05-29 09:54:07 -0400344 *args.fUserStencilSettings, args.fClip, *args.fViewMatrix,
Brian Salomonfc118442019-11-22 19:09:27 -0500345 SkIPoint{boundsForMask->fLeft, boundsForMask->fTop}, *boundsForMask);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000346
347 return true;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000348}