blob: 3e0a79f75bc2c8dec0016994e2b8bda2ccf4e569 [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 Holler9e2c50e2021-02-09 14:41:52 -050017#include "src/gpu/GrDeferredProxyUploader.h"
Adlai Hollera0693042020-10-14 11:23:11 -040018#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrGpuResourcePriv.h"
20#include "src/gpu/GrOpFlushState.h"
21#include "src/gpu/GrProxyProvider.h"
22#include "src/gpu/GrRecordingContextPriv.h"
23#include "src/gpu/GrSWMaskHelper.h"
Robert Phillips62214f72021-06-15 10:12:51 -040024#include "src/gpu/GrUtil.h"
Brian Salomon99a813c2020-03-02 12:50:47 -050025#include "src/gpu/SkGr.h"
Robert Phillips550de7f2021-07-06 16:28:52 -040026#include "src/gpu/effects/GrTextureEffect.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000027#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/ops/GrDrawOp.h"
Robert Phillips4dca8312021-07-28 15:13:20 -040029#include "src/gpu/v1/SurfaceDrawContext_v1.h"
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000030
robertphillips@google.comed4155d2012-05-01 14:30:24 +000031////////////////////////////////////////////////////////////////////////////////
Chris Dalton5ed44232017-09-07 13:22:46 -060032GrPathRenderer::CanDrawPath
33GrSoftwarePathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
bsalomon8acedde2016-06-24 10:42:16 -070034 // Pass on any style that applies. The caller will apply the style if a suitable renderer is
Michael Ludwig2686d692020-04-17 20:21:37 +000035 // not found and try again with the new GrStyledShape.
Robert Phillips1afd4cd2018-01-08 13:40:32 -050036 if (!args.fShape->style().applies() && SkToBool(fProxyProvider) &&
Chris Dalton6ce447a2019-06-23 18:07:38 -060037 (args.fAAType == GrAAType::kCoverage || args.fAAType == GrAAType::kNone)) {
Chris Dalton5ed44232017-09-07 13:22:46 -060038 // This is the fallback renderer for when a path is too complicated for the GPU ones.
39 return CanDrawPath::kAsBackup;
40 }
41 return CanDrawPath::kNo;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000042}
43
robertphillips@google.comed4155d2012-05-01 14:30:24 +000044////////////////////////////////////////////////////////////////////////////////
Michael Ludwig2686d692020-04-17 20:21:37 +000045static bool get_unclipped_shape_dev_bounds(const GrStyledShape& shape, const SkMatrix& matrix,
bsalomon39ef7fb2016-09-21 11:16:05 -070046 SkIRect* devBounds) {
47 SkRect shapeBounds = shape.styledBounds();
48 if (shapeBounds.isEmpty()) {
49 return false;
50 }
51 SkRect shapeDevBounds;
52 matrix.mapRect(&shapeDevBounds, shapeBounds);
Brian Salomonc1c607e2016-12-20 11:41:43 -050053 // Even though these are "unclipped" bounds we still clip to the int32_t range.
54 // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints
55 // would round down to this value when cast to a float, but who really cares.
56 // INT32_MIN is exactly representable.
57 static constexpr int32_t kMaxInt = 2147483520;
58 if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) {
59 return false;
60 }
Jim Van Verthba7cf292017-11-02 20:18:56 +000061 // Make sure that the resulting SkIRect can have representable width and height
62 if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt ||
63 SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) {
64 return false;
65 }
bsalomon39ef7fb2016-09-21 11:16:05 -070066 shapeDevBounds.roundOut(devBounds);
67 return true;
68}
69
70// Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there
71// is no intersection.
Robert Phillips4dca8312021-07-28 15:13:20 -040072bool GrSoftwarePathRenderer::GetShapeAndClipBounds(skgpu::v1::SurfaceDrawContext* sdc,
Michael Ludwig7c12e282020-05-29 09:54:07 -040073 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +000074 const GrStyledShape& shape,
Robert Phillips20390c32018-08-17 11:01:03 -040075 const SkMatrix& matrix,
76 SkIRect* unclippedDevShapeBounds,
77 SkIRect* clippedDevShapeBounds,
78 SkIRect* devClipBounds) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +000079 // compute bounds as intersection of rt size, clip, and path
Michael Ludwige06a8972020-06-11 10:29:00 -040080 *devClipBounds = clip ? clip->getConservativeBounds()
Robert Phillips4dca8312021-07-28 15:13:20 -040081 : SkIRect::MakeWH(sdc->width(), sdc->height());
robertphillips@google.com7b112892012-07-31 15:18:21 +000082
bsalomon39ef7fb2016-09-21 11:16:05 -070083 if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) {
Brian Salomon44207f32020-01-06 15:20:18 -050084 *unclippedDevShapeBounds = SkIRect::MakeEmpty();
85 *clippedDevShapeBounds = SkIRect::MakeEmpty();
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000086 return false;
robertphillips@google.comed4155d2012-05-01 14:30:24 +000087 }
bsalomon39ef7fb2016-09-21 11:16:05 -070088 if (!clippedDevShapeBounds->intersect(*devClipBounds, *unclippedDevShapeBounds)) {
Brian Salomon44207f32020-01-06 15:20:18 -050089 *clippedDevShapeBounds = SkIRect::MakeEmpty();
robertphillips@google.comed4155d2012-05-01 14:30:24 +000090 return false;
91 }
92 return true;
93}
94
95////////////////////////////////////////////////////////////////////////////////
robertphillips976f5f02016-06-03 10:59:20 -070096
Robert Phillips4dca8312021-07-28 15:13:20 -040097void GrSoftwarePathRenderer::DrawNonAARect(skgpu::v1::SurfaceDrawContext* sdc,
Brian Salomon82f44312017-01-11 13:42:54 -050098 GrPaint&& paint,
robertphillipsd2b6d642016-07-21 08:55:08 -070099 const GrUserStencilSettings& userStencilSettings,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400100 const GrClip* clip,
robertphillips976f5f02016-06-03 10:59:20 -0700101 const SkMatrix& viewMatrix,
102 const SkRect& rect,
103 const SkMatrix& localMatrix) {
Robert Phillips4dca8312021-07-28 15:13:20 -0400104 sdc->stencilRect(clip, &userStencilSettings, std::move(paint), GrAA::kNo,
105 viewMatrix, rect, &localMatrix);
robertphillips976f5f02016-06-03 10:59:20 -0700106}
107
Robert Phillips4dca8312021-07-28 15:13:20 -0400108void GrSoftwarePathRenderer::DrawAroundInvPath(skgpu::v1::SurfaceDrawContext* sdc,
Brian Salomon82f44312017-01-11 13:42:54 -0500109 GrPaint&& paint,
robertphillipsd2b6d642016-07-21 08:55:08 -0700110 const GrUserStencilSettings& userStencilSettings,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400111 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) {
Mike Reed92b33352019-08-24 19:39:13 -0400122 rect.setLTRB(SkIntToScalar(devClipBounds.fLeft), SkIntToScalar(devClipBounds.fTop),
123 SkIntToScalar(devClipBounds.fRight), SkIntToScalar(devPathBounds.fTop));
Robert Phillips4dca8312021-07-28 15:13:20 -0400124 DrawNonAARect(sdc, GrPaint::Clone(paint), userStencilSettings, clip,
Brian Salomonb74ef032017-08-10 12:46:01 -0400125 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) {
Mike Reed92b33352019-08-24 19:39:13 -0400128 rect.setLTRB(SkIntToScalar(devClipBounds.fLeft), SkIntToScalar(devPathBounds.fTop),
129 SkIntToScalar(devPathBounds.fLeft), SkIntToScalar(devPathBounds.fBottom));
Robert Phillips4dca8312021-07-28 15:13:20 -0400130 DrawNonAARect(sdc, GrPaint::Clone(paint), userStencilSettings, clip,
Brian Salomonb74ef032017-08-10 12:46:01 -0400131 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) {
Mike Reed92b33352019-08-24 19:39:13 -0400134 rect.setLTRB(SkIntToScalar(devPathBounds.fRight), SkIntToScalar(devPathBounds.fTop),
135 SkIntToScalar(devClipBounds.fRight), SkIntToScalar(devPathBounds.fBottom));
Robert Phillips4dca8312021-07-28 15:13:20 -0400136 DrawNonAARect(sdc, GrPaint::Clone(paint), userStencilSettings, clip,
Brian Salomonb74ef032017-08-10 12:46:01 -0400137 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) {
Mike Reed92b33352019-08-24 19:39:13 -0400140 rect.setLTRB(SkIntToScalar(devClipBounds.fLeft), SkIntToScalar(devPathBounds.fBottom),
141 SkIntToScalar(devClipBounds.fRight), SkIntToScalar(devClipBounds.fBottom));
Robert Phillips4dca8312021-07-28 15:13:20 -0400142 DrawNonAARect(sdc, 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(
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500148 GrSurfaceProxyView view,
Robert Phillips4dca8312021-07-28 15:13:20 -0400149 skgpu::v1::SurfaceDrawContext* sdc,
Brian Osmanc7da1462017-08-17 16:14:25 -0400150 GrPaint&& paint,
151 const GrUserStencilSettings& userStencilSettings,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400152 const GrClip* clip,
Brian Osmanc7da1462017-08-17 16:14:25 -0400153 const SkMatrix& viewMatrix,
154 const SkIPoint& textureOriginInDeviceSpace,
155 const SkIRect& deviceSpaceRectToDraw) {
156 SkMatrix invert;
157 if (!viewMatrix.invert(&invert)) {
158 return;
159 }
160
Brian Salomonb43d6992021-01-05 14:37:40 -0500161 view.concatSwizzle(GrSwizzle("aaaa"));
162
Brian Osmanc7da1462017-08-17 16:14:25 -0400163 SkRect dstRect = SkRect::Make(deviceSpaceRectToDraw);
164
165 // We use device coords to compute the texture coordinates. We take the device coords and apply
166 // a translation so that the top-left of the device bounds maps to 0,0, and then a scaling
167 // matrix to normalized coords.
Mike Reed1f607332020-05-21 12:11:27 -0400168 SkMatrix maskMatrix = SkMatrix::Translate(SkIntToScalar(-textureOriginInDeviceSpace.fX),
Brian Osmanc7da1462017-08-17 16:14:25 -0400169 SkIntToScalar(-textureOriginInDeviceSpace.fY));
170 maskMatrix.preConcat(viewMatrix);
Greg Danield2ccbb52020-02-05 10:45:39 -0500171
John Stiles41d91b62020-07-21 14:39:40 -0400172 paint.setCoverageFragmentProcessor(GrTextureEffect::Make(
Greg Danield2ccbb52020-02-05 10:45:39 -0500173 std::move(view), kPremul_SkAlphaType, maskMatrix, GrSamplerState::Filter::kNearest));
Robert Phillips4dca8312021-07-28 15:13:20 -0400174 DrawNonAARect(sdc, std::move(paint), userStencilSettings, clip, SkMatrix::I(),
Brian Osmanf9810662017-08-30 10:02:10 -0400175 dstRect, invert);
176}
177
Robert Phillips643f4812021-08-11 09:31:00 -0400178static GrSurfaceProxyView make_deferred_mask_texture_view(GrRecordingContext* rContext,
Adlai Hollercc25d532021-02-10 13:58:34 +0000179 SkBackingFit fit,
180 SkISize dimensions) {
Robert Phillips643f4812021-08-11 09:31:00 -0400181 GrProxyProvider* proxyProvider = rContext->priv().proxyProvider();
182 const GrCaps* caps = rContext->priv().caps();
Adlai Hollercc25d532021-02-10 13:58:34 +0000183
184 const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kAlpha_8,
185 GrRenderable::kNo);
186
187 GrSwizzle swizzle = caps->getReadSwizzle(format, GrColorType::kAlpha_8);
188
189 auto proxy =
190 proxyProvider->createProxy(format, dimensions, GrRenderable::kNo, 1, GrMipmapped::kNo,
191 fit, SkBudgeted::kYes, GrProtected::kNo);
192 return {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
193}
194
Brian Osmanf9810662017-08-30 10:02:10 -0400195namespace {
196
Brian Osman5d034742017-09-11 13:38:55 -0400197/**
Adlai Hollercc25d532021-02-10 13:58:34 +0000198 * Payload class for use with GrTDeferredProxyUploader. The software path renderer only draws
Brian Osman5d034742017-09-11 13:38:55 -0400199 * a single path into the mask texture. This stores all of the information needed by the worker
200 * thread's call to drawShape (see below, in onDrawPath).
201 */
202class SoftwarePathData {
Brian Osmanf9810662017-08-30 10:02:10 -0400203public:
Michael Ludwig2686d692020-04-17 20:21:37 +0000204 SoftwarePathData(const SkIRect& maskBounds, const SkMatrix& viewMatrix,
205 const GrStyledShape& shape, GrAA aa)
Brian Osman5d034742017-09-11 13:38:55 -0400206 : fMaskBounds(maskBounds)
Brian Osmanf9810662017-08-30 10:02:10 -0400207 , fViewMatrix(viewMatrix)
208 , fShape(shape)
Brian Osman5d034742017-09-11 13:38:55 -0400209 , fAA(aa) {}
Brian Osmanf9810662017-08-30 10:02:10 -0400210
Brian Osmanf9810662017-08-30 10:02:10 -0400211 const SkIRect& getMaskBounds() const { return fMaskBounds; }
Adlai Hollercc25d532021-02-10 13:58:34 +0000212 const SkMatrix* getViewMatrix() const { return &fViewMatrix; }
Michael Ludwig2686d692020-04-17 20:21:37 +0000213 const GrStyledShape& getShape() const { return fShape; }
Brian Osmanf9810662017-08-30 10:02:10 -0400214 GrAA getAA() const { return fAA; }
215
216private:
Brian Osmanf9810662017-08-30 10:02:10 -0400217 SkIRect fMaskBounds;
218 SkMatrix fViewMatrix;
Michael Ludwig2686d692020-04-17 20:21:37 +0000219 GrStyledShape fShape;
Brian Osmanf9810662017-08-30 10:02:10 -0400220 GrAA fAA;
Brian Osmanf9810662017-08-30 10:02:10 -0400221};
222
John Stilesa6841be2020-08-06 14:11:56 -0400223} // namespace
Brian Osmanc7da1462017-08-17 16:14:25 -0400224
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000225////////////////////////////////////////////////////////////////////////////////
226// return true on success; false on failure
bsalomon0aff2fa2015-07-31 06:48:27 -0700227bool GrSoftwarePathRenderer::onDrawPath(const DrawPathArgs& args) {
Robert Phillipsa92913e2021-07-12 16:31:52 -0400228 GR_AUDIT_TRAIL_AUTO_FRAME(args.fContext->priv().auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700229 "GrSoftwarePathRenderer::onDrawPath");
Robert Phillipsa92913e2021-07-12 16:31:52 -0400230
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500231 if (!fProxyProvider) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000232 return false;
233 }
234
caryclarkd6562002016-07-27 12:02:07 -0700235 SkASSERT(!args.fShape->style().applies());
Robert Phillips20390c32018-08-17 11:01:03 -0400236 // We really need to know if the shape will be inverse filled or not
Eric Karl5c779752017-05-08 12:02:07 -0700237 // If the path is hairline, ignore inverse fill.
Robert Phillips20390c32018-08-17 11:01:03 -0400238 bool inverseFilled = args.fShape->inverseFilled() &&
Robert Phillips62214f72021-06-15 10:12:51 -0400239 !GrIsStrokeHairlineOrEquivalent(args.fShape->style(),
240 *args.fViewMatrix, nullptr);
bsalomon8acedde2016-06-24 10:42:16 -0700241
bsalomon39ef7fb2016-09-21 11:16:05 -0700242 SkIRect unclippedDevShapeBounds, clippedDevShapeBounds, devClipBounds;
243 // To prevent overloading the cache with entries during animations we limit the cache of masks
244 // to cases where the matrix preserves axis alignment.
245 bool useCache = fAllowCaching && !inverseFilled && args.fViewMatrix->preservesAxisAlignment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -0600246 args.fShape->hasUnstyledKey() && (GrAAType::kCoverage == args.fAAType);
bsalomon39ef7fb2016-09-21 11:16:05 -0700247
John Stiles0fbc6a32021-06-04 14:40:57 -0400248 if (!GetShapeAndClipBounds(args.fSurfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400249 args.fClip, *args.fShape,
Robert Phillips20390c32018-08-17 11:01:03 -0400250 *args.fViewMatrix, &unclippedDevShapeBounds,
251 &clippedDevShapeBounds,
252 &devClipBounds)) {
bsalomon8acedde2016-06-24 10:42:16 -0700253 if (inverseFilled) {
John Stiles0fbc6a32021-06-04 14:40:57 -0400254 DrawAroundInvPath(args.fSurfaceDrawContext, std::move(args.fPaint),
Michael Ludwig7c12e282020-05-29 09:54:07 -0400255 *args.fUserStencilSettings, args.fClip, *args.fViewMatrix,
Brian Salomon82f44312017-01-11 13:42:54 -0500256 devClipBounds, unclippedDevShapeBounds);
bsalomon@google.com276c1fa2012-06-19 13:22:45 +0000257 }
258 return true;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000259 }
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000260
bsalomon39ef7fb2016-09-21 11:16:05 -0700261 const SkIRect* boundsForMask = &clippedDevShapeBounds;
262 if (useCache) {
263 // Use the cache only if >50% of the path is visible.
264 int unclippedWidth = unclippedDevShapeBounds.width();
265 int unclippedHeight = unclippedDevShapeBounds.height();
Brian Osman1a0ea732017-09-28 11:53:03 -0400266 int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight);
267 int64_t clippedArea = sk_64_mul(clippedDevShapeBounds.width(),
268 clippedDevShapeBounds.height());
John Stiles0fbc6a32021-06-04 14:40:57 -0400269 int maxTextureSize = args.fSurfaceDrawContext->caps()->maxTextureSize();
bsalomon39ef7fb2016-09-21 11:16:05 -0700270 if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
271 unclippedHeight > maxTextureSize) {
272 useCache = false;
273 } else {
274 boundsForMask = &unclippedDevShapeBounds;
275 }
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000276 }
277
bsalomon39ef7fb2016-09-21 11:16:05 -0700278 GrUniqueKey maskKey;
bsalomon39ef7fb2016-09-21 11:16:05 -0700279 if (useCache) {
280 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
281 SkScalar sx = args.fViewMatrix->get(SkMatrix::kMScaleX);
282 SkScalar sy = args.fViewMatrix->get(SkMatrix::kMScaleY);
283 SkScalar kx = args.fViewMatrix->get(SkMatrix::kMSkewX);
284 SkScalar ky = args.fViewMatrix->get(SkMatrix::kMSkewY);
Stan Iliev67cd6732017-08-15 17:10:26 -0400285 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Robert Phillipsc5727d82020-05-05 10:37:20 -0400286 GrUniqueKey::Builder builder(&maskKey, kDomain, 7 + args.fShape->unstyledKeySize(),
Brian Osmana4425262018-07-26 13:37:53 -0400287 "SW Path Mask");
Robert Phillipsc5727d82020-05-05 10:37:20 -0400288 builder[0] = boundsForMask->width();
289 builder[1] = boundsForMask->height();
Robert Phillipsd58a2702020-05-01 12:27:56 -0400290
Stan Iliev67cd6732017-08-15 17:10:26 -0400291#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
292 // Fractional translate does not affect caching on Android. This is done for better cache
293 // hit ratio and speed, but it is matching HWUI behavior, which doesn't consider the matrix
294 // at all when caching paths.
Brian Osmana4425262018-07-26 13:37:53 -0400295 SkFixed fracX = 0;
296 SkFixed fracY = 0;
Stan Iliev67cd6732017-08-15 17:10:26 -0400297#else
bsalomon39ef7fb2016-09-21 11:16:05 -0700298 SkScalar tx = args.fViewMatrix->get(SkMatrix::kMTransX);
299 SkScalar ty = args.fViewMatrix->get(SkMatrix::kMTransY);
300 // Allow 8 bits each in x and y of subpixel positioning.
301 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
302 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
Stan Iliev67cd6732017-08-15 17:10:26 -0400303#endif
Robert Phillipsc5727d82020-05-05 10:37:20 -0400304 builder[2] = SkFloat2Bits(sx);
305 builder[3] = SkFloat2Bits(sy);
306 builder[4] = SkFloat2Bits(kx);
307 builder[5] = SkFloat2Bits(ky);
Brian Osmana4425262018-07-26 13:37:53 -0400308 // Distinguish between hairline and filled paths. For hairlines, we also need to include
309 // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that
310 // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers
311 // all cases we might see.
312 uint32_t styleBits = args.fShape->style().isSimpleHairline() ?
313 ((args.fShape->style().strokeRec().getCap() << 1) | 1) : 0;
Robert Phillipsc5727d82020-05-05 10:37:20 -0400314 builder[6] = fracX | (fracY >> 8) | (styleBits << 16);
315 args.fShape->writeUnstyledKey(&builder[7]);
bsalomon39ef7fb2016-09-21 11:16:05 -0700316 }
317
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500318 GrSurfaceProxyView view;
bsalomon39ef7fb2016-09-21 11:16:05 -0700319 if (useCache) {
John Stiles7c3fa4e2021-08-10 17:00:56 -0400320 sk_sp<GrTextureProxy> proxy = fProxyProvider->findOrCreateProxyByUniqueKey(maskKey);
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500321 if (proxy) {
John Stiles0fbc6a32021-06-04 14:40:57 -0400322 GrSwizzle swizzle = args.fSurfaceDrawContext->caps()->getReadSwizzle(
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500323 proxy->backendFormat(), GrColorType::kAlpha_8);
324 view = {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
Robert Phillips273f1072020-05-05 13:03:07 -0400325 args.fContext->priv().stats()->incNumPathMasksCacheHits();
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500326 }
bsalomon39ef7fb2016-09-21 11:16:05 -0700327 }
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500328 if (!view) {
Robert Phillips417b7f42016-12-14 09:12:13 -0500329 SkBackingFit fit = useCache ? SkBackingFit::kExact : SkBackingFit::kApprox;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600330 GrAA aa = GrAA(GrAAType::kCoverage == args.fAAType);
Adlai Hollercc25d532021-02-10 13:58:34 +0000331
332 SkTaskGroup* taskGroup = nullptr;
333 if (auto direct = args.fContext->asDirectContext()) {
334 taskGroup = direct->priv().getTaskGroup();
335 }
336
337 if (taskGroup) {
338 view = make_deferred_mask_texture_view(args.fContext, fit, boundsForMask->size());
339 if (!view) {
340 return false;
341 }
342
343 auto uploader = std::make_unique<GrTDeferredProxyUploader<SoftwarePathData>>(
344 *boundsForMask, *args.fViewMatrix, *args.fShape, aa);
345 GrTDeferredProxyUploader<SoftwarePathData>* uploaderRaw = uploader.get();
346
347 auto drawAndUploadMask = [uploaderRaw] {
348 TRACE_EVENT0("skia.gpu", "Threaded SW Mask Render");
349 GrSWMaskHelper helper(uploaderRaw->getPixels());
350 if (helper.init(uploaderRaw->data().getMaskBounds())) {
351 helper.drawShape(uploaderRaw->data().getShape(),
352 *uploaderRaw->data().getViewMatrix(),
353 SkRegion::kReplace_Op, uploaderRaw->data().getAA(), 0xFF);
354 } else {
355 SkDEBUGFAIL("Unable to allocate SW mask.");
356 }
357 uploaderRaw->signalAndFreeData();
358 };
359 taskGroup->add(std::move(drawAndUploadMask));
360 view.asTextureProxy()->texPriv().setDeferredUploader(std::move(uploader));
361 } else {
362 GrSWMaskHelper helper;
363 if (!helper.init(*boundsForMask)) {
364 return false;
365 }
366 helper.drawShape(*args.fShape, *args.fViewMatrix, SkRegion::kReplace_Op, aa, 0xFF);
367 view = helper.toTextureView(args.fContext, fit);
368 }
Brian Osmanf9810662017-08-30 10:02:10 -0400369
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500370 if (!view) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500371 return false;
372 }
373 if (useCache) {
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500374 SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon4282d292020-02-24 09:39:32 -0500375
376 // We will add an invalidator to the path so that if the path goes away we will
Brian Salomon99a813c2020-03-02 12:50:47 -0500377 // delete or recycle the mask texture.
378 auto listener = GrMakeUniqueKeyInvalidationListener(&maskKey,
379 args.fContext->priv().contextID());
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500380 fProxyProvider->assignUniqueKeyToProxy(maskKey, view.asTextureProxy());
Brian Salomon99a813c2020-03-02 12:50:47 -0500381 args.fShape->addGenIDChangeListener(std::move(listener));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500382 }
Robert Phillips273f1072020-05-05 13:03:07 -0400383
384 args.fContext->priv().stats()->incNumPathMasksGenerated();
bsalomon39ef7fb2016-09-21 11:16:05 -0700385 }
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500386 SkASSERT(view);
bsalomon8acedde2016-06-24 10:42:16 -0700387 if (inverseFilled) {
John Stiles0fbc6a32021-06-04 14:40:57 -0400388 DrawAroundInvPath(args.fSurfaceDrawContext, GrPaint::Clone(args.fPaint),
Michael Ludwig7c12e282020-05-29 09:54:07 -0400389 *args.fUserStencilSettings, args.fClip, *args.fViewMatrix, devClipBounds,
Brian Salomon82f44312017-01-11 13:42:54 -0500390 unclippedDevShapeBounds);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000391 }
John Stiles0fbc6a32021-06-04 14:40:57 -0400392 DrawToTargetWithShapeMask(std::move(view), args.fSurfaceDrawContext, std::move(args.fPaint),
Michael Ludwig7c12e282020-05-29 09:54:07 -0400393 *args.fUserStencilSettings, args.fClip, *args.fViewMatrix,
Brian Salomonfc118442019-11-22 19:09:27 -0500394 SkIPoint{boundsForMask->fLeft, boundsForMask->fTop}, *boundsForMask);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000395
396 return true;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000397}