robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 1 | /* |
| 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 Salomon | 99a813c | 2020-03-02 12:50:47 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrSoftwarePathRenderer.h" |
| 9 | |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 10 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/private/SkSemaphore.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/core/SkTaskGroup.h" |
| 13 | #include "src/core/SkTraceEvent.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrAuditTrail.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/GrCaps.h" |
| 16 | #include "src/gpu/GrClip.h" |
Adlai Holler | 9e2c50e | 2021-02-09 14:41:52 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrDeferredProxyUploader.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #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 Phillips | 62214f7 | 2021-06-15 10:12:51 -0400 | [diff] [blame] | 24 | #include "src/gpu/GrUtil.h" |
Brian Salomon | 99a813c | 2020-03-02 12:50:47 -0500 | [diff] [blame] | 25 | #include "src/gpu/SkGr.h" |
Robert Phillips | 550de7f | 2021-07-06 16:28:52 -0400 | [diff] [blame] | 26 | #include "src/gpu/effects/GrTextureEffect.h" |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 27 | #include "src/gpu/geometry/GrStyledShape.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "src/gpu/ops/GrDrawOp.h" |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 29 | #include "src/gpu/v1/SurfaceDrawContext_v1.h" |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 30 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 31 | //////////////////////////////////////////////////////////////////////////////// |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 32 | GrPathRenderer::CanDrawPath |
| 33 | GrSoftwarePathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 34 | // Pass on any style that applies. The caller will apply the style if a suitable renderer is |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 35 | // not found and try again with the new GrStyledShape. |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 36 | if (!args.fShape->style().applies() && SkToBool(fProxyProvider) && |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 37 | (args.fAAType == GrAAType::kCoverage || args.fAAType == GrAAType::kNone)) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 38 | // 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.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 42 | } |
| 43 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 44 | //////////////////////////////////////////////////////////////////////////////// |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 45 | static bool get_unclipped_shape_dev_bounds(const GrStyledShape& shape, const SkMatrix& matrix, |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 46 | SkIRect* devBounds) { |
| 47 | SkRect shapeBounds = shape.styledBounds(); |
| 48 | if (shapeBounds.isEmpty()) { |
| 49 | return false; |
| 50 | } |
| 51 | SkRect shapeDevBounds; |
| 52 | matrix.mapRect(&shapeDevBounds, shapeBounds); |
Brian Salomon | c1c607e | 2016-12-20 11:41:43 -0500 | [diff] [blame] | 53 | // 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 Verth | ba7cf29 | 2017-11-02 20:18:56 +0000 | [diff] [blame] | 61 | // 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 | } |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 66 | 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 Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 72 | bool GrSoftwarePathRenderer::GetShapeAndClipBounds(skgpu::v1::SurfaceDrawContext* sdc, |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 73 | const GrClip* clip, |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 74 | const GrStyledShape& shape, |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 75 | const SkMatrix& matrix, |
| 76 | SkIRect* unclippedDevShapeBounds, |
| 77 | SkIRect* clippedDevShapeBounds, |
| 78 | SkIRect* devClipBounds) { |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 79 | // compute bounds as intersection of rt size, clip, and path |
Michael Ludwig | e06a897 | 2020-06-11 10:29:00 -0400 | [diff] [blame] | 80 | *devClipBounds = clip ? clip->getConservativeBounds() |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 81 | : SkIRect::MakeWH(sdc->width(), sdc->height()); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 82 | |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 83 | if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) { |
Brian Salomon | 44207f3 | 2020-01-06 15:20:18 -0500 | [diff] [blame] | 84 | *unclippedDevShapeBounds = SkIRect::MakeEmpty(); |
| 85 | *clippedDevShapeBounds = SkIRect::MakeEmpty(); |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 86 | return false; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 87 | } |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 88 | if (!clippedDevShapeBounds->intersect(*devClipBounds, *unclippedDevShapeBounds)) { |
Brian Salomon | 44207f3 | 2020-01-06 15:20:18 -0500 | [diff] [blame] | 89 | *clippedDevShapeBounds = SkIRect::MakeEmpty(); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 90 | return false; |
| 91 | } |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 96 | |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 97 | void GrSoftwarePathRenderer::DrawNonAARect(skgpu::v1::SurfaceDrawContext* sdc, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 98 | GrPaint&& paint, |
robertphillips | d2b6d64 | 2016-07-21 08:55:08 -0700 | [diff] [blame] | 99 | const GrUserStencilSettings& userStencilSettings, |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 100 | const GrClip* clip, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 101 | const SkMatrix& viewMatrix, |
| 102 | const SkRect& rect, |
| 103 | const SkMatrix& localMatrix) { |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 104 | sdc->stencilRect(clip, &userStencilSettings, std::move(paint), GrAA::kNo, |
| 105 | viewMatrix, rect, &localMatrix); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 108 | void GrSoftwarePathRenderer::DrawAroundInvPath(skgpu::v1::SurfaceDrawContext* sdc, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 109 | GrPaint&& paint, |
robertphillips | d2b6d64 | 2016-07-21 08:55:08 -0700 | [diff] [blame] | 110 | const GrUserStencilSettings& userStencilSettings, |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 111 | const GrClip* clip, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 112 | const SkMatrix& viewMatrix, |
| 113 | const SkIRect& devClipBounds, |
| 114 | const SkIRect& devPathBounds) { |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 115 | SkMatrix invert; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 116 | if (!viewMatrix.invert(&invert)) { |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 117 | return; |
| 118 | } |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 119 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 120 | SkRect rect; |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 121 | if (devClipBounds.fTop < devPathBounds.fTop) { |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 122 | rect.setLTRB(SkIntToScalar(devClipBounds.fLeft), SkIntToScalar(devClipBounds.fTop), |
| 123 | SkIntToScalar(devClipBounds.fRight), SkIntToScalar(devPathBounds.fTop)); |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 124 | DrawNonAARect(sdc, GrPaint::Clone(paint), userStencilSettings, clip, |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 125 | SkMatrix::I(), rect, invert); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 126 | } |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 127 | if (devClipBounds.fLeft < devPathBounds.fLeft) { |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 128 | rect.setLTRB(SkIntToScalar(devClipBounds.fLeft), SkIntToScalar(devPathBounds.fTop), |
| 129 | SkIntToScalar(devPathBounds.fLeft), SkIntToScalar(devPathBounds.fBottom)); |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 130 | DrawNonAARect(sdc, GrPaint::Clone(paint), userStencilSettings, clip, |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 131 | SkMatrix::I(), rect, invert); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 132 | } |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 133 | if (devClipBounds.fRight > devPathBounds.fRight) { |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 134 | rect.setLTRB(SkIntToScalar(devPathBounds.fRight), SkIntToScalar(devPathBounds.fTop), |
| 135 | SkIntToScalar(devClipBounds.fRight), SkIntToScalar(devPathBounds.fBottom)); |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 136 | DrawNonAARect(sdc, GrPaint::Clone(paint), userStencilSettings, clip, |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 137 | SkMatrix::I(), rect, invert); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 138 | } |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 139 | if (devClipBounds.fBottom > devPathBounds.fBottom) { |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 140 | rect.setLTRB(SkIntToScalar(devClipBounds.fLeft), SkIntToScalar(devPathBounds.fBottom), |
| 141 | SkIntToScalar(devClipBounds.fRight), SkIntToScalar(devClipBounds.fBottom)); |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 142 | DrawNonAARect(sdc, std::move(paint), userStencilSettings, clip, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 143 | SkMatrix::I(), rect, invert); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Brian Osman | c7da146 | 2017-08-17 16:14:25 -0400 | [diff] [blame] | 147 | void GrSoftwarePathRenderer::DrawToTargetWithShapeMask( |
Greg Daniel | 9f0dfbd | 2020-02-10 11:47:11 -0500 | [diff] [blame] | 148 | GrSurfaceProxyView view, |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 149 | skgpu::v1::SurfaceDrawContext* sdc, |
Brian Osman | c7da146 | 2017-08-17 16:14:25 -0400 | [diff] [blame] | 150 | GrPaint&& paint, |
| 151 | const GrUserStencilSettings& userStencilSettings, |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 152 | const GrClip* clip, |
Brian Osman | c7da146 | 2017-08-17 16:14:25 -0400 | [diff] [blame] | 153 | const SkMatrix& viewMatrix, |
| 154 | const SkIPoint& textureOriginInDeviceSpace, |
| 155 | const SkIRect& deviceSpaceRectToDraw) { |
| 156 | SkMatrix invert; |
| 157 | if (!viewMatrix.invert(&invert)) { |
| 158 | return; |
| 159 | } |
| 160 | |
Brian Salomon | b43d699 | 2021-01-05 14:37:40 -0500 | [diff] [blame] | 161 | view.concatSwizzle(GrSwizzle("aaaa")); |
| 162 | |
Brian Osman | c7da146 | 2017-08-17 16:14:25 -0400 | [diff] [blame] | 163 | 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 Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 168 | SkMatrix maskMatrix = SkMatrix::Translate(SkIntToScalar(-textureOriginInDeviceSpace.fX), |
Brian Osman | c7da146 | 2017-08-17 16:14:25 -0400 | [diff] [blame] | 169 | SkIntToScalar(-textureOriginInDeviceSpace.fY)); |
| 170 | maskMatrix.preConcat(viewMatrix); |
Greg Daniel | d2ccbb5 | 2020-02-05 10:45:39 -0500 | [diff] [blame] | 171 | |
John Stiles | 41d91b6 | 2020-07-21 14:39:40 -0400 | [diff] [blame] | 172 | paint.setCoverageFragmentProcessor(GrTextureEffect::Make( |
Greg Daniel | d2ccbb5 | 2020-02-05 10:45:39 -0500 | [diff] [blame] | 173 | std::move(view), kPremul_SkAlphaType, maskMatrix, GrSamplerState::Filter::kNearest)); |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 174 | DrawNonAARect(sdc, std::move(paint), userStencilSettings, clip, SkMatrix::I(), |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 175 | dstRect, invert); |
| 176 | } |
| 177 | |
Robert Phillips | 643f481 | 2021-08-11 09:31:00 -0400 | [diff] [blame] | 178 | static GrSurfaceProxyView make_deferred_mask_texture_view(GrRecordingContext* rContext, |
Adlai Holler | cc25d53 | 2021-02-10 13:58:34 +0000 | [diff] [blame] | 179 | SkBackingFit fit, |
| 180 | SkISize dimensions) { |
Robert Phillips | 643f481 | 2021-08-11 09:31:00 -0400 | [diff] [blame] | 181 | GrProxyProvider* proxyProvider = rContext->priv().proxyProvider(); |
| 182 | const GrCaps* caps = rContext->priv().caps(); |
Adlai Holler | cc25d53 | 2021-02-10 13:58:34 +0000 | [diff] [blame] | 183 | |
| 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 Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 195 | namespace { |
| 196 | |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 197 | /** |
Adlai Holler | cc25d53 | 2021-02-10 13:58:34 +0000 | [diff] [blame] | 198 | * Payload class for use with GrTDeferredProxyUploader. The software path renderer only draws |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 199 | * 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 | */ |
| 202 | class SoftwarePathData { |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 203 | public: |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 204 | SoftwarePathData(const SkIRect& maskBounds, const SkMatrix& viewMatrix, |
| 205 | const GrStyledShape& shape, GrAA aa) |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 206 | : fMaskBounds(maskBounds) |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 207 | , fViewMatrix(viewMatrix) |
| 208 | , fShape(shape) |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 209 | , fAA(aa) {} |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 210 | |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 211 | const SkIRect& getMaskBounds() const { return fMaskBounds; } |
Adlai Holler | cc25d53 | 2021-02-10 13:58:34 +0000 | [diff] [blame] | 212 | const SkMatrix* getViewMatrix() const { return &fViewMatrix; } |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 213 | const GrStyledShape& getShape() const { return fShape; } |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 214 | GrAA getAA() const { return fAA; } |
| 215 | |
| 216 | private: |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 217 | SkIRect fMaskBounds; |
| 218 | SkMatrix fViewMatrix; |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 219 | GrStyledShape fShape; |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 220 | GrAA fAA; |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 221 | }; |
| 222 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 223 | } // namespace |
Brian Osman | c7da146 | 2017-08-17 16:14:25 -0400 | [diff] [blame] | 224 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 225 | //////////////////////////////////////////////////////////////////////////////// |
| 226 | // return true on success; false on failure |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 227 | bool GrSoftwarePathRenderer::onDrawPath(const DrawPathArgs& args) { |
Robert Phillips | a92913e | 2021-07-12 16:31:52 -0400 | [diff] [blame] | 228 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fContext->priv().auditTrail(), |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 229 | "GrSoftwarePathRenderer::onDrawPath"); |
Robert Phillips | a92913e | 2021-07-12 16:31:52 -0400 | [diff] [blame] | 230 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 231 | if (!fProxyProvider) { |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 232 | return false; |
| 233 | } |
| 234 | |
caryclark | d656200 | 2016-07-27 12:02:07 -0700 | [diff] [blame] | 235 | SkASSERT(!args.fShape->style().applies()); |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 236 | // We really need to know if the shape will be inverse filled or not |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 237 | // If the path is hairline, ignore inverse fill. |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 238 | bool inverseFilled = args.fShape->inverseFilled() && |
Robert Phillips | 62214f7 | 2021-06-15 10:12:51 -0400 | [diff] [blame] | 239 | !GrIsStrokeHairlineOrEquivalent(args.fShape->style(), |
| 240 | *args.fViewMatrix, nullptr); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 241 | |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 242 | 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 Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 246 | args.fShape->hasUnstyledKey() && (GrAAType::kCoverage == args.fAAType); |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 247 | |
John Stiles | 0fbc6a3 | 2021-06-04 14:40:57 -0400 | [diff] [blame] | 248 | if (!GetShapeAndClipBounds(args.fSurfaceDrawContext, |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 249 | args.fClip, *args.fShape, |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 250 | *args.fViewMatrix, &unclippedDevShapeBounds, |
| 251 | &clippedDevShapeBounds, |
| 252 | &devClipBounds)) { |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 253 | if (inverseFilled) { |
John Stiles | 0fbc6a3 | 2021-06-04 14:40:57 -0400 | [diff] [blame] | 254 | DrawAroundInvPath(args.fSurfaceDrawContext, std::move(args.fPaint), |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 255 | *args.fUserStencilSettings, args.fClip, *args.fViewMatrix, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 256 | devClipBounds, unclippedDevShapeBounds); |
bsalomon@google.com | 276c1fa | 2012-06-19 13:22:45 +0000 | [diff] [blame] | 257 | } |
| 258 | return true; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 259 | } |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 260 | |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 261 | 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 Osman | 1a0ea73 | 2017-09-28 11:53:03 -0400 | [diff] [blame] | 266 | int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight); |
| 267 | int64_t clippedArea = sk_64_mul(clippedDevShapeBounds.width(), |
| 268 | clippedDevShapeBounds.height()); |
John Stiles | 0fbc6a3 | 2021-06-04 14:40:57 -0400 | [diff] [blame] | 269 | int maxTextureSize = args.fSurfaceDrawContext->caps()->maxTextureSize(); |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 270 | if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize || |
| 271 | unclippedHeight > maxTextureSize) { |
| 272 | useCache = false; |
| 273 | } else { |
| 274 | boundsForMask = &unclippedDevShapeBounds; |
| 275 | } |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 276 | } |
| 277 | |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 278 | GrUniqueKey maskKey; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 279 | 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 Iliev | 67cd673 | 2017-08-15 17:10:26 -0400 | [diff] [blame] | 285 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
Robert Phillips | c5727d8 | 2020-05-05 10:37:20 -0400 | [diff] [blame] | 286 | GrUniqueKey::Builder builder(&maskKey, kDomain, 7 + args.fShape->unstyledKeySize(), |
Brian Osman | a442526 | 2018-07-26 13:37:53 -0400 | [diff] [blame] | 287 | "SW Path Mask"); |
Robert Phillips | c5727d8 | 2020-05-05 10:37:20 -0400 | [diff] [blame] | 288 | builder[0] = boundsForMask->width(); |
| 289 | builder[1] = boundsForMask->height(); |
Robert Phillips | d58a270 | 2020-05-01 12:27:56 -0400 | [diff] [blame] | 290 | |
Stan Iliev | 67cd673 | 2017-08-15 17:10:26 -0400 | [diff] [blame] | 291 | #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 Osman | a442526 | 2018-07-26 13:37:53 -0400 | [diff] [blame] | 295 | SkFixed fracX = 0; |
| 296 | SkFixed fracY = 0; |
Stan Iliev | 67cd673 | 2017-08-15 17:10:26 -0400 | [diff] [blame] | 297 | #else |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 298 | 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 Iliev | 67cd673 | 2017-08-15 17:10:26 -0400 | [diff] [blame] | 303 | #endif |
Robert Phillips | c5727d8 | 2020-05-05 10:37:20 -0400 | [diff] [blame] | 304 | builder[2] = SkFloat2Bits(sx); |
| 305 | builder[3] = SkFloat2Bits(sy); |
| 306 | builder[4] = SkFloat2Bits(kx); |
| 307 | builder[5] = SkFloat2Bits(ky); |
Brian Osman | a442526 | 2018-07-26 13:37:53 -0400 | [diff] [blame] | 308 | // 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 Phillips | c5727d8 | 2020-05-05 10:37:20 -0400 | [diff] [blame] | 314 | builder[6] = fracX | (fracY >> 8) | (styleBits << 16); |
| 315 | args.fShape->writeUnstyledKey(&builder[7]); |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Greg Daniel | 9f0dfbd | 2020-02-10 11:47:11 -0500 | [diff] [blame] | 318 | GrSurfaceProxyView view; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 319 | if (useCache) { |
John Stiles | 7c3fa4e | 2021-08-10 17:00:56 -0400 | [diff] [blame] | 320 | sk_sp<GrTextureProxy> proxy = fProxyProvider->findOrCreateProxyByUniqueKey(maskKey); |
Greg Daniel | 9f0dfbd | 2020-02-10 11:47:11 -0500 | [diff] [blame] | 321 | if (proxy) { |
John Stiles | 0fbc6a3 | 2021-06-04 14:40:57 -0400 | [diff] [blame] | 322 | GrSwizzle swizzle = args.fSurfaceDrawContext->caps()->getReadSwizzle( |
Greg Daniel | 9f0dfbd | 2020-02-10 11:47:11 -0500 | [diff] [blame] | 323 | proxy->backendFormat(), GrColorType::kAlpha_8); |
| 324 | view = {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle}; |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 325 | args.fContext->priv().stats()->incNumPathMasksCacheHits(); |
Greg Daniel | 9f0dfbd | 2020-02-10 11:47:11 -0500 | [diff] [blame] | 326 | } |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 327 | } |
Greg Daniel | 9f0dfbd | 2020-02-10 11:47:11 -0500 | [diff] [blame] | 328 | if (!view) { |
Robert Phillips | 417b7f4 | 2016-12-14 09:12:13 -0500 | [diff] [blame] | 329 | SkBackingFit fit = useCache ? SkBackingFit::kExact : SkBackingFit::kApprox; |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 330 | GrAA aa = GrAA(GrAAType::kCoverage == args.fAAType); |
Adlai Holler | cc25d53 | 2021-02-10 13:58:34 +0000 | [diff] [blame] | 331 | |
| 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 Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 369 | |
Greg Daniel | 9f0dfbd | 2020-02-10 11:47:11 -0500 | [diff] [blame] | 370 | if (!view) { |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 371 | return false; |
| 372 | } |
| 373 | if (useCache) { |
Greg Daniel | 9f0dfbd | 2020-02-10 11:47:11 -0500 | [diff] [blame] | 374 | SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin); |
Brian Salomon | 4282d29 | 2020-02-24 09:39:32 -0500 | [diff] [blame] | 375 | |
| 376 | // We will add an invalidator to the path so that if the path goes away we will |
Brian Salomon | 99a813c | 2020-03-02 12:50:47 -0500 | [diff] [blame] | 377 | // delete or recycle the mask texture. |
| 378 | auto listener = GrMakeUniqueKeyInvalidationListener(&maskKey, |
| 379 | args.fContext->priv().contextID()); |
Greg Daniel | 9f0dfbd | 2020-02-10 11:47:11 -0500 | [diff] [blame] | 380 | fProxyProvider->assignUniqueKeyToProxy(maskKey, view.asTextureProxy()); |
Brian Salomon | 99a813c | 2020-03-02 12:50:47 -0500 | [diff] [blame] | 381 | args.fShape->addGenIDChangeListener(std::move(listener)); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 382 | } |
Robert Phillips | 273f107 | 2020-05-05 13:03:07 -0400 | [diff] [blame] | 383 | |
| 384 | args.fContext->priv().stats()->incNumPathMasksGenerated(); |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 385 | } |
Greg Daniel | 9f0dfbd | 2020-02-10 11:47:11 -0500 | [diff] [blame] | 386 | SkASSERT(view); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 387 | if (inverseFilled) { |
John Stiles | 0fbc6a3 | 2021-06-04 14:40:57 -0400 | [diff] [blame] | 388 | DrawAroundInvPath(args.fSurfaceDrawContext, GrPaint::Clone(args.fPaint), |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 389 | *args.fUserStencilSettings, args.fClip, *args.fViewMatrix, devClipBounds, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 390 | unclippedDevShapeBounds); |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 391 | } |
John Stiles | 0fbc6a3 | 2021-06-04 14:40:57 -0400 | [diff] [blame] | 392 | DrawToTargetWithShapeMask(std::move(view), args.fSurfaceDrawContext, std::move(args.fPaint), |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 393 | *args.fUserStencilSettings, args.fClip, *args.fViewMatrix, |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 394 | SkIPoint{boundsForMask->fLeft, boundsForMask->fTop}, *boundsForMask); |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 395 | |
| 396 | return true; |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 397 | } |