blob: ec0ce467c1d9f0e0f423f3f3ea7623a4f402189c [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/SkSemaphore.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/core/SkTaskGroup.h"
12#include "src/core/SkTraceEvent.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040013#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrSurfaceContextPriv.h"
Brian Salomon99a813c2020-03-02 12:50:47 -050025#include "src/gpu/SkGr.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000026#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#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
Michael Ludwig2686d692020-04-17 20:21:37 +000033 // not found and try again with the new GrStyledShape.
Robert Phillips1afd4cd2018-01-08 13:40:32 -050034 if (!args.fShape->style().applies() && SkToBool(fProxyProvider) &&
Chris Dalton6ce447a2019-06-23 18:07:38 -060035 (args.fAAType == GrAAType::kCoverage || args.fAAType == GrAAType::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////////////////////////////////////////////////////////////////////////////////
Michael Ludwig2686d692020-04-17 20:21:37 +000043static bool get_unclipped_shape_dev_bounds(const GrStyledShape& shape, const SkMatrix& matrix,
bsalomon39ef7fb2016-09-21 11:16:05 -070044 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,
Michael Ludwig2686d692020-04-17 20:21:37 +000072 const GrStyledShape& shape,
Robert Phillips20390c32018-08-17 11:01:03 -040073 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
Michael Ludwigc002d562020-05-13 14:17:57 -040078 *devClipBounds = clip.getConservativeBounds(renderTargetContext->width(),
79 renderTargetContext->height());
robertphillips@google.com7b112892012-07-31 15:18:21 +000080
bsalomon39ef7fb2016-09-21 11:16:05 -070081 if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) {
Brian Salomon44207f32020-01-06 15:20:18 -050082 *unclippedDevShapeBounds = SkIRect::MakeEmpty();
83 *clippedDevShapeBounds = SkIRect::MakeEmpty();
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000084 return false;
robertphillips@google.comed4155d2012-05-01 14:30:24 +000085 }
bsalomon39ef7fb2016-09-21 11:16:05 -070086 if (!clippedDevShapeBounds->intersect(*devClipBounds, *unclippedDevShapeBounds)) {
Brian Salomon44207f32020-01-06 15:20:18 -050087 *clippedDevShapeBounds = SkIRect::MakeEmpty();
robertphillips@google.comed4155d2012-05-01 14:30:24 +000088 return false;
89 }
90 return true;
91}
92
93////////////////////////////////////////////////////////////////////////////////
robertphillips976f5f02016-06-03 10:59:20 -070094
Brian Osman11052242016-10-27 14:47:55 -040095void GrSoftwarePathRenderer::DrawNonAARect(GrRenderTargetContext* renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -050096 GrPaint&& paint,
robertphillipsd2b6d642016-07-21 08:55:08 -070097 const GrUserStencilSettings& userStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -070098 const GrClip& clip,
robertphillips976f5f02016-06-03 10:59:20 -070099 const SkMatrix& viewMatrix,
100 const SkRect& rect,
101 const SkMatrix& localMatrix) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400102 renderTargetContext->priv().stencilRect(clip, &userStencilSettings, std::move(paint), GrAA::kNo,
103 viewMatrix, rect, &localMatrix);
robertphillips976f5f02016-06-03 10:59:20 -0700104}
105
Brian Osman11052242016-10-27 14:47:55 -0400106void GrSoftwarePathRenderer::DrawAroundInvPath(GrRenderTargetContext* renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500107 GrPaint&& paint,
robertphillipsd2b6d642016-07-21 08:55:08 -0700108 const GrUserStencilSettings& userStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -0700109 const GrClip& clip,
robertphillips976f5f02016-06-03 10:59:20 -0700110 const SkMatrix& viewMatrix,
111 const SkIRect& devClipBounds,
112 const SkIRect& devPathBounds) {
joshualittd27f73e2014-12-29 07:43:36 -0800113 SkMatrix invert;
joshualitt8059eb92014-12-29 15:10:07 -0800114 if (!viewMatrix.invert(&invert)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000115 return;
116 }
joshualittd27f73e2014-12-29 07:43:36 -0800117
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000118 SkRect rect;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000119 if (devClipBounds.fTop < devPathBounds.fTop) {
Mike Reed92b33352019-08-24 19:39:13 -0400120 rect.setLTRB(SkIntToScalar(devClipBounds.fLeft), SkIntToScalar(devClipBounds.fTop),
121 SkIntToScalar(devClipBounds.fRight), SkIntToScalar(devPathBounds.fTop));
Brian Salomonb74ef032017-08-10 12:46:01 -0400122 DrawNonAARect(renderTargetContext, GrPaint::Clone(paint), userStencilSettings, clip,
123 SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000124 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000125 if (devClipBounds.fLeft < devPathBounds.fLeft) {
Mike Reed92b33352019-08-24 19:39:13 -0400126 rect.setLTRB(SkIntToScalar(devClipBounds.fLeft), SkIntToScalar(devPathBounds.fTop),
127 SkIntToScalar(devPathBounds.fLeft), SkIntToScalar(devPathBounds.fBottom));
Brian Salomonb74ef032017-08-10 12:46:01 -0400128 DrawNonAARect(renderTargetContext, GrPaint::Clone(paint), userStencilSettings, clip,
129 SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000130 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000131 if (devClipBounds.fRight > devPathBounds.fRight) {
Mike Reed92b33352019-08-24 19:39:13 -0400132 rect.setLTRB(SkIntToScalar(devPathBounds.fRight), SkIntToScalar(devPathBounds.fTop),
133 SkIntToScalar(devClipBounds.fRight), SkIntToScalar(devPathBounds.fBottom));
Brian Salomonb74ef032017-08-10 12:46:01 -0400134 DrawNonAARect(renderTargetContext, GrPaint::Clone(paint), userStencilSettings, clip,
135 SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000136 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000137 if (devClipBounds.fBottom > devPathBounds.fBottom) {
Mike Reed92b33352019-08-24 19:39:13 -0400138 rect.setLTRB(SkIntToScalar(devClipBounds.fLeft), SkIntToScalar(devPathBounds.fBottom),
139 SkIntToScalar(devClipBounds.fRight), SkIntToScalar(devClipBounds.fBottom));
Brian Salomon82f44312017-01-11 13:42:54 -0500140 DrawNonAARect(renderTargetContext, std::move(paint), userStencilSettings, clip,
robertphillips976f5f02016-06-03 10:59:20 -0700141 SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000142 }
143}
144
Brian Osmanc7da1462017-08-17 16:14:25 -0400145void GrSoftwarePathRenderer::DrawToTargetWithShapeMask(
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500146 GrSurfaceProxyView view,
Brian Osmanc7da1462017-08-17 16:14:25 -0400147 GrRenderTargetContext* renderTargetContext,
148 GrPaint&& paint,
149 const GrUserStencilSettings& userStencilSettings,
150 const GrClip& clip,
151 const SkMatrix& viewMatrix,
152 const SkIPoint& textureOriginInDeviceSpace,
153 const SkIRect& deviceSpaceRectToDraw) {
154 SkMatrix invert;
155 if (!viewMatrix.invert(&invert)) {
156 return;
157 }
158
159 SkRect dstRect = SkRect::Make(deviceSpaceRectToDraw);
160
161 // We use device coords to compute the texture coordinates. We take the device coords and apply
162 // a translation so that the top-left of the device bounds maps to 0,0, and then a scaling
163 // matrix to normalized coords.
Mike Reed1f607332020-05-21 12:11:27 -0400164 SkMatrix maskMatrix = SkMatrix::Translate(SkIntToScalar(-textureOriginInDeviceSpace.fX),
Brian Osmanc7da1462017-08-17 16:14:25 -0400165 SkIntToScalar(-textureOriginInDeviceSpace.fY));
166 maskMatrix.preConcat(viewMatrix);
Greg Danield2ccbb52020-02-05 10:45:39 -0500167
Brian Salomonb8f098d2020-01-07 11:15:44 -0500168 paint.addCoverageFragmentProcessor(GrTextureEffect::Make(
Greg Danield2ccbb52020-02-05 10:45:39 -0500169 std::move(view), kPremul_SkAlphaType, 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
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500174static GrSurfaceProxyView make_deferred_mask_texture_view(GrRecordingContext* context,
175 SkBackingFit fit,
176 SkISize dimensions) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500177 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips0a15cc62019-07-30 12:49:10 -0400178 const GrCaps* caps = context->priv().caps();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500179
Robert Phillips0a15cc62019-07-30 12:49:10 -0400180 const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kAlpha_8,
181 GrRenderable::kNo);
Greg Daniel4065d452018-11-16 15:43:41 -0500182
Greg Daniel47c20e82020-01-21 14:29:57 -0500183 GrSwizzle swizzle = caps->getReadSwizzle(format, GrColorType::kAlpha_8);
184
Greg Daniel3a365112020-02-14 10:47:18 -0500185 auto proxy =
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400186 proxyProvider->createProxy(format, dimensions, GrRenderable::kNo, 1, GrMipMapped::kNo,
187 fit, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500188 return {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
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:
Michael Ludwig2686d692020-04-17 20:21:37 +0000200 SoftwarePathData(const SkIRect& maskBounds, const SkMatrix& viewMatrix,
201 const GrStyledShape& shape, GrAA aa)
Brian Osman5d034742017-09-11 13:38:55 -0400202 : 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; }
Michael Ludwig2686d692020-04-17 20:21:37 +0000209 const GrStyledShape& getShape() const { return fShape; }
Brian Osmanf9810662017-08-30 10:02:10 -0400210 GrAA getAA() const { return fAA; }
211
212private:
Brian Osmanf9810662017-08-30 10:02:10 -0400213 SkIRect fMaskBounds;
214 SkMatrix fViewMatrix;
Michael Ludwig2686d692020-04-17 20:21:37 +0000215 GrStyledShape fShape;
Brian Osmanf9810662017-08-30 10:02:10 -0400216 GrAA fAA;
Brian Osmanf9810662017-08-30 10:02:10 -0400217};
218
Brian Osmanc7da1462017-08-17 16:14:25 -0400219}
220
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000221////////////////////////////////////////////////////////////////////////////////
222// return true on success; false on failure
bsalomon0aff2fa2015-07-31 06:48:27 -0700223bool GrSoftwarePathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400224 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700225 "GrSoftwarePathRenderer::onDrawPath");
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500226 if (!fProxyProvider) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000227 return false;
228 }
229
caryclarkd6562002016-07-27 12:02:07 -0700230 SkASSERT(!args.fShape->style().applies());
Robert Phillips20390c32018-08-17 11:01:03 -0400231 // We really need to know if the shape will be inverse filled or not
Eric Karl5c779752017-05-08 12:02:07 -0700232 // If the path is hairline, ignore inverse fill.
Robert Phillips20390c32018-08-17 11:01:03 -0400233 bool inverseFilled = args.fShape->inverseFilled() &&
234 !IsStrokeHairlineOrEquivalent(args.fShape->style(),
235 *args.fViewMatrix, nullptr);
bsalomon8acedde2016-06-24 10:42:16 -0700236
bsalomon39ef7fb2016-09-21 11:16:05 -0700237 SkIRect unclippedDevShapeBounds, clippedDevShapeBounds, devClipBounds;
238 // To prevent overloading the cache with entries during animations we limit the cache of masks
239 // to cases where the matrix preserves axis alignment.
240 bool useCache = fAllowCaching && !inverseFilled && args.fViewMatrix->preservesAxisAlignment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -0600241 args.fShape->hasUnstyledKey() && (GrAAType::kCoverage == args.fAAType);
bsalomon39ef7fb2016-09-21 11:16:05 -0700242
Robert Phillips20390c32018-08-17 11:01:03 -0400243 if (!GetShapeAndClipBounds(args.fRenderTargetContext,
244 *args.fClip, *args.fShape,
245 *args.fViewMatrix, &unclippedDevShapeBounds,
246 &clippedDevShapeBounds,
247 &devClipBounds)) {
bsalomon8acedde2016-06-24 10:42:16 -0700248 if (inverseFilled) {
Brian Salomon82f44312017-01-11 13:42:54 -0500249 DrawAroundInvPath(args.fRenderTargetContext, std::move(args.fPaint),
250 *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix,
251 devClipBounds, unclippedDevShapeBounds);
bsalomon@google.com276c1fa2012-06-19 13:22:45 +0000252 }
253 return true;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000254 }
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000255
bsalomon39ef7fb2016-09-21 11:16:05 -0700256 const SkIRect* boundsForMask = &clippedDevShapeBounds;
257 if (useCache) {
258 // Use the cache only if >50% of the path is visible.
259 int unclippedWidth = unclippedDevShapeBounds.width();
260 int unclippedHeight = unclippedDevShapeBounds.height();
Brian Osman1a0ea732017-09-28 11:53:03 -0400261 int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight);
262 int64_t clippedArea = sk_64_mul(clippedDevShapeBounds.width(),
263 clippedDevShapeBounds.height());
Brian Osman11052242016-10-27 14:47:55 -0400264 int maxTextureSize = args.fRenderTargetContext->caps()->maxTextureSize();
bsalomon39ef7fb2016-09-21 11:16:05 -0700265 if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
266 unclippedHeight > maxTextureSize) {
267 useCache = false;
268 } else {
269 boundsForMask = &unclippedDevShapeBounds;
270 }
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000271 }
272
bsalomon39ef7fb2016-09-21 11:16:05 -0700273 GrUniqueKey maskKey;
bsalomon39ef7fb2016-09-21 11:16:05 -0700274 if (useCache) {
275 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
276 SkScalar sx = args.fViewMatrix->get(SkMatrix::kMScaleX);
277 SkScalar sy = args.fViewMatrix->get(SkMatrix::kMScaleY);
278 SkScalar kx = args.fViewMatrix->get(SkMatrix::kMSkewX);
279 SkScalar ky = args.fViewMatrix->get(SkMatrix::kMSkewY);
Stan Iliev67cd6732017-08-15 17:10:26 -0400280 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Robert Phillipsc5727d82020-05-05 10:37:20 -0400281 GrUniqueKey::Builder builder(&maskKey, kDomain, 7 + args.fShape->unstyledKeySize(),
Brian Osmana4425262018-07-26 13:37:53 -0400282 "SW Path Mask");
Robert Phillipsc5727d82020-05-05 10:37:20 -0400283 builder[0] = boundsForMask->width();
284 builder[1] = boundsForMask->height();
Robert Phillipsd58a2702020-05-01 12:27:56 -0400285
Stan Iliev67cd6732017-08-15 17:10:26 -0400286#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
287 // Fractional translate does not affect caching on Android. This is done for better cache
288 // hit ratio and speed, but it is matching HWUI behavior, which doesn't consider the matrix
289 // at all when caching paths.
Brian Osmana4425262018-07-26 13:37:53 -0400290 SkFixed fracX = 0;
291 SkFixed fracY = 0;
Stan Iliev67cd6732017-08-15 17:10:26 -0400292#else
bsalomon39ef7fb2016-09-21 11:16:05 -0700293 SkScalar tx = args.fViewMatrix->get(SkMatrix::kMTransX);
294 SkScalar ty = args.fViewMatrix->get(SkMatrix::kMTransY);
295 // Allow 8 bits each in x and y of subpixel positioning.
296 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
297 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
Stan Iliev67cd6732017-08-15 17:10:26 -0400298#endif
Robert Phillipsc5727d82020-05-05 10:37:20 -0400299 builder[2] = SkFloat2Bits(sx);
300 builder[3] = SkFloat2Bits(sy);
301 builder[4] = SkFloat2Bits(kx);
302 builder[5] = SkFloat2Bits(ky);
Brian Osmana4425262018-07-26 13:37:53 -0400303 // Distinguish between hairline and filled paths. For hairlines, we also need to include
304 // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that
305 // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers
306 // all cases we might see.
307 uint32_t styleBits = args.fShape->style().isSimpleHairline() ?
308 ((args.fShape->style().strokeRec().getCap() << 1) | 1) : 0;
Robert Phillipsc5727d82020-05-05 10:37:20 -0400309 builder[6] = fracX | (fracY >> 8) | (styleBits << 16);
310 args.fShape->writeUnstyledKey(&builder[7]);
bsalomon39ef7fb2016-09-21 11:16:05 -0700311 }
312
Robert Phillipsd3749482017-03-14 09:17:43 -0400313 sk_sp<GrTextureProxy> proxy;
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500314 GrSurfaceProxyView view;
bsalomon39ef7fb2016-09-21 11:16:05 -0700315 if (useCache) {
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400316 auto proxy = fProxyProvider->findOrCreateProxyByUniqueKey(maskKey);
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500317 if (proxy) {
318 GrSwizzle swizzle = args.fRenderTargetContext->caps()->getReadSwizzle(
319 proxy->backendFormat(), GrColorType::kAlpha_8);
320 view = {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
Robert Phillips273f1072020-05-05 13:03:07 -0400321 args.fContext->priv().stats()->incNumPathMasksCacheHits();
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500322 }
bsalomon39ef7fb2016-09-21 11:16:05 -0700323 }
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500324 if (!view) {
Robert Phillips417b7f42016-12-14 09:12:13 -0500325 SkBackingFit fit = useCache ? SkBackingFit::kExact : SkBackingFit::kApprox;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600326 GrAA aa = GrAA(GrAAType::kCoverage == args.fAAType);
Brian Osmanf9810662017-08-30 10:02:10 -0400327
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500328 SkTaskGroup* taskGroup = nullptr;
329 if (auto direct = args.fContext->priv().asDirectContext()) {
330 taskGroup = direct->priv().getTaskGroup();
331 }
332
Brian Osmanf9810662017-08-30 10:02:10 -0400333 if (taskGroup) {
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500334 view = make_deferred_mask_texture_view(args.fContext, fit, boundsForMask->size());
335 if (!view) {
Brian Osmanf9810662017-08-30 10:02:10 -0400336 return false;
337 }
338
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500339 auto uploader = std::make_unique<GrTDeferredProxyUploader<SoftwarePathData>>(
Brian Osman099fa0f2017-10-02 16:38:32 -0400340 *boundsForMask, *args.fViewMatrix, *args.fShape, aa);
341 GrTDeferredProxyUploader<SoftwarePathData>* uploaderRaw = uploader.get();
Brian Osmanf9810662017-08-30 10:02:10 -0400342
343 auto drawAndUploadMask = [uploaderRaw] {
Brian Salomon5f394272019-07-02 14:07:49 -0400344 TRACE_EVENT0("skia.gpu", "Threaded SW Mask Render");
Brian Osmanf9810662017-08-30 10:02:10 -0400345 GrSWMaskHelper helper(uploaderRaw->getPixels());
Brian Osman5d034742017-09-11 13:38:55 -0400346 if (helper.init(uploaderRaw->data().getMaskBounds())) {
347 helper.drawShape(uploaderRaw->data().getShape(),
348 *uploaderRaw->data().getViewMatrix(),
349 SkRegion::kReplace_Op, uploaderRaw->data().getAA(), 0xFF);
Brian Osmanf9810662017-08-30 10:02:10 -0400350 } else {
351 SkDEBUGFAIL("Unable to allocate SW mask.");
352 }
Brian Osman099fa0f2017-10-02 16:38:32 -0400353 uploaderRaw->signalAndFreeData();
Brian Osmanf9810662017-08-30 10:02:10 -0400354 };
355 taskGroup->add(std::move(drawAndUploadMask));
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500356 view.asTextureProxy()->texPriv().setDeferredUploader(std::move(uploader));
Brian Osmanf9810662017-08-30 10:02:10 -0400357 } else {
358 GrSWMaskHelper helper;
Brian Salomon74077562017-08-30 13:55:35 -0400359 if (!helper.init(*boundsForMask)) {
Brian Osmanf9810662017-08-30 10:02:10 -0400360 return false;
361 }
Brian Salomon74077562017-08-30 13:55:35 -0400362 helper.drawShape(*args.fShape, *args.fViewMatrix, SkRegion::kReplace_Op, aa, 0xFF);
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500363 view = helper.toTextureView(args.fContext, fit);
Brian Osmanf9810662017-08-30 10:02:10 -0400364 }
365
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500366 if (!view) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500367 return false;
368 }
369 if (useCache) {
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500370 SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon4282d292020-02-24 09:39:32 -0500371
372 // We will add an invalidator to the path so that if the path goes away we will
Brian Salomon99a813c2020-03-02 12:50:47 -0500373 // delete or recycle the mask texture.
374 auto listener = GrMakeUniqueKeyInvalidationListener(&maskKey,
375 args.fContext->priv().contextID());
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500376 fProxyProvider->assignUniqueKeyToProxy(maskKey, view.asTextureProxy());
Brian Salomon99a813c2020-03-02 12:50:47 -0500377 args.fShape->addGenIDChangeListener(std::move(listener));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500378 }
Robert Phillips273f1072020-05-05 13:03:07 -0400379
380 args.fContext->priv().stats()->incNumPathMasksGenerated();
bsalomon39ef7fb2016-09-21 11:16:05 -0700381 }
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500382 SkASSERT(view);
bsalomon8acedde2016-06-24 10:42:16 -0700383 if (inverseFilled) {
Brian Salomonb74ef032017-08-10 12:46:01 -0400384 DrawAroundInvPath(args.fRenderTargetContext, GrPaint::Clone(args.fPaint),
Brian Salomon82f44312017-01-11 13:42:54 -0500385 *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix, devClipBounds,
386 unclippedDevShapeBounds);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000387 }
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500388 DrawToTargetWithShapeMask(std::move(view), args.fRenderTargetContext, std::move(args.fPaint),
Brian Salomonfc118442019-11-22 19:09:27 -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}