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 | |
| 8 | #include "GrSoftwarePathRenderer.h" |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 9 | #include "GrAuditTrail.h" |
| 10 | #include "GrClip.h" |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 11 | #include "GrContextPriv.h" |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 12 | #include "GrDeferredProxyUploader.h" |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 13 | #include "GrGpuResourcePriv.h" |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 14 | #include "GrOpFlushState.h" |
| 15 | #include "GrOpList.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 16 | #include "GrProxyProvider.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 17 | #include "GrSWMaskHelper.h" |
Brian Salomon | 653f42f | 2018-07-10 10:07:31 -0400 | [diff] [blame] | 18 | #include "GrShape.h" |
| 19 | #include "GrSurfaceContextPriv.h" |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 20 | #include "SkMakeUnique.h" |
| 21 | #include "SkSemaphore.h" |
| 22 | #include "SkTaskGroup.h" |
| 23 | #include "SkTraceEvent.h" |
Robert Phillips | 009e9af | 2017-06-15 14:01:04 -0400 | [diff] [blame] | 24 | #include "ops/GrDrawOp.h" |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 25 | #include "ops/GrRectOpFactory.h" |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 26 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 27 | //////////////////////////////////////////////////////////////////////////////// |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 28 | GrPathRenderer::CanDrawPath |
| 29 | GrSoftwarePathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 30 | // Pass on any style that applies. The caller will apply the style if a suitable renderer is |
| 31 | // not found and try again with the new GrShape. |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 32 | if (!args.fShape->style().applies() && SkToBool(fProxyProvider) && |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 33 | (args.fAAType == GrAAType::kCoverage || args.fAAType == GrAAType::kNone)) { |
| 34 | // 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.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 38 | } |
| 39 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 40 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 41 | static bool get_unclipped_shape_dev_bounds(const GrShape& shape, const SkMatrix& matrix, |
| 42 | SkIRect* devBounds) { |
| 43 | SkRect shapeBounds = shape.styledBounds(); |
| 44 | if (shapeBounds.isEmpty()) { |
| 45 | return false; |
| 46 | } |
| 47 | SkRect shapeDevBounds; |
| 48 | matrix.mapRect(&shapeDevBounds, shapeBounds); |
Brian Salomon | c1c607e | 2016-12-20 11:41:43 -0500 | [diff] [blame] | 49 | // 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 Verth | ba7cf29 | 2017-11-02 20:18:56 +0000 | [diff] [blame] | 57 | // 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 | } |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 62 | shapeDevBounds.roundOut(devBounds); |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | // Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there |
| 67 | // is no intersection. |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 68 | bool GrSoftwarePathRenderer::GetShapeAndClipBounds(GrRenderTargetContext* renderTargetContext, |
| 69 | const GrClip& clip, |
| 70 | const GrShape& shape, |
| 71 | const SkMatrix& matrix, |
| 72 | SkIRect* unclippedDevShapeBounds, |
| 73 | SkIRect* clippedDevShapeBounds, |
| 74 | SkIRect* devClipBounds) { |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 75 | // compute bounds as intersection of rt size, clip, and path |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 76 | clip.getConservativeBounds(renderTargetContext->width(), |
| 77 | renderTargetContext->height(), |
| 78 | devClipBounds); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 79 | |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 80 | if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) { |
| 81 | *unclippedDevShapeBounds = SkIRect::EmptyIRect(); |
| 82 | *clippedDevShapeBounds = SkIRect::EmptyIRect(); |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 83 | return false; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 84 | } |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 85 | if (!clippedDevShapeBounds->intersect(*devClipBounds, *unclippedDevShapeBounds)) { |
| 86 | *clippedDevShapeBounds = SkIRect::EmptyIRect(); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 87 | return false; |
| 88 | } |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 93 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 94 | void GrSoftwarePathRenderer::DrawNonAARect(GrRenderTargetContext* renderTargetContext, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 95 | GrPaint&& paint, |
robertphillips | d2b6d64 | 2016-07-21 08:55:08 -0700 | [diff] [blame] | 96 | const GrUserStencilSettings& userStencilSettings, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 97 | const GrClip& clip, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 98 | const SkMatrix& viewMatrix, |
| 99 | const SkRect& rect, |
| 100 | const SkMatrix& localMatrix) { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 101 | GrContext* context = renderTargetContext->surfPriv().getContext(); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 102 | renderTargetContext->addDrawOp(clip, |
| 103 | GrRectOpFactory::MakeNonAAFillWithLocalMatrix( |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 104 | context, std::move(paint), viewMatrix, localMatrix, rect, |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 105 | GrAAType::kNone, &userStencilSettings)); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 108 | void GrSoftwarePathRenderer::DrawAroundInvPath(GrRenderTargetContext* renderTargetContext, |
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, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [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) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 122 | rect.iset(devClipBounds.fLeft, devClipBounds.fTop, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 123 | devClipBounds.fRight, devPathBounds.fTop); |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 124 | DrawNonAARect(renderTargetContext, GrPaint::Clone(paint), userStencilSettings, clip, |
| 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) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 128 | rect.iset(devClipBounds.fLeft, devPathBounds.fTop, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 129 | devPathBounds.fLeft, devPathBounds.fBottom); |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 130 | DrawNonAARect(renderTargetContext, GrPaint::Clone(paint), userStencilSettings, clip, |
| 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) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 134 | rect.iset(devPathBounds.fRight, devPathBounds.fTop, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 135 | devClipBounds.fRight, devPathBounds.fBottom); |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 136 | DrawNonAARect(renderTargetContext, GrPaint::Clone(paint), userStencilSettings, clip, |
| 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) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 140 | rect.iset(devClipBounds.fLeft, devPathBounds.fBottom, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 141 | devClipBounds.fRight, devClipBounds.fBottom); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 142 | DrawNonAARect(renderTargetContext, 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( |
| 148 | sk_sp<GrTextureProxy> proxy, |
| 149 | GrRenderTargetContext* renderTargetContext, |
| 150 | GrPaint&& paint, |
| 151 | const GrUserStencilSettings& userStencilSettings, |
| 152 | const GrClip& clip, |
| 153 | const SkMatrix& viewMatrix, |
| 154 | const SkIPoint& textureOriginInDeviceSpace, |
| 155 | const SkIRect& deviceSpaceRectToDraw) { |
| 156 | SkMatrix invert; |
| 157 | if (!viewMatrix.invert(&invert)) { |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | SkRect dstRect = SkRect::Make(deviceSpaceRectToDraw); |
| 162 | |
| 163 | // We use device coords to compute the texture coordinates. We take the device coords and apply |
| 164 | // a translation so that the top-left of the device bounds maps to 0,0, and then a scaling |
| 165 | // matrix to normalized coords. |
| 166 | SkMatrix maskMatrix = SkMatrix::MakeTrans(SkIntToScalar(-textureOriginInDeviceSpace.fX), |
| 167 | SkIntToScalar(-textureOriginInDeviceSpace.fY)); |
| 168 | maskMatrix.preConcat(viewMatrix); |
| 169 | paint.addCoverageFragmentProcessor(GrSimpleTextureEffect::Make( |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 170 | std::move(proxy), maskMatrix, GrSamplerState::Filter::kNearest)); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 171 | DrawNonAARect(renderTargetContext, std::move(paint), userStencilSettings, clip, SkMatrix::I(), |
| 172 | dstRect, invert); |
| 173 | } |
| 174 | |
| 175 | static sk_sp<GrTextureProxy> make_deferred_mask_texture_proxy(GrContext* context, SkBackingFit fit, |
| 176 | int width, int height) { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 177 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
| 178 | |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 179 | GrSurfaceDesc desc; |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 180 | desc.fWidth = width; |
| 181 | desc.fHeight = height; |
| 182 | desc.fConfig = kAlpha_8_GrPixelConfig; |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 183 | |
Brian Osman | d140fe9 | 2017-10-03 12:17:26 -0400 | [diff] [blame] | 184 | // MDB TODO: We're going to fill this proxy with an ASAP upload (which is out of order wrt to |
| 185 | // ops), so it can't have any pending IO. |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 186 | return proxyProvider->createProxy(desc, kTopLeft_GrSurfaceOrigin, fit, SkBudgeted::kYes, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 187 | GrInternalSurfaceFlags::kNoPendingIO); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | namespace { |
| 191 | |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 192 | /** |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 193 | * Payload class for use with GrTDeferredProxyUploader. The software path renderer only draws |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 194 | * a single path into the mask texture. This stores all of the information needed by the worker |
| 195 | * thread's call to drawShape (see below, in onDrawPath). |
| 196 | */ |
| 197 | class SoftwarePathData { |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 198 | public: |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 199 | SoftwarePathData(const SkIRect& maskBounds, const SkMatrix& viewMatrix, const GrShape& shape, |
| 200 | GrAA aa) |
| 201 | : fMaskBounds(maskBounds) |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 202 | , fViewMatrix(viewMatrix) |
| 203 | , fShape(shape) |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 204 | , fAA(aa) {} |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 205 | |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 206 | const SkIRect& getMaskBounds() const { return fMaskBounds; } |
| 207 | const SkMatrix* getViewMatrix() const { return &fViewMatrix; } |
| 208 | const GrShape& getShape() const { return fShape; } |
| 209 | GrAA getAA() const { return fAA; } |
| 210 | |
| 211 | private: |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 212 | SkIRect fMaskBounds; |
| 213 | SkMatrix fViewMatrix; |
| 214 | GrShape fShape; |
| 215 | GrAA fAA; |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 216 | }; |
| 217 | |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 218 | // When the SkPathRef genID changes, invalidate a corresponding GrResource described by key. |
| 219 | class PathInvalidator : public SkPathRef::GenIDChangeListener { |
| 220 | public: |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 221 | PathInvalidator(const GrUniqueKey& key, uint32_t contextUniqueID) |
| 222 | : fMsg(key, contextUniqueID) {} |
| 223 | |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 224 | private: |
| 225 | GrUniqueKeyInvalidatedMessage fMsg; |
| 226 | |
| 227 | void onChange() override { |
| 228 | SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); |
| 229 | } |
| 230 | }; |
| 231 | |
Brian Osman | c7da146 | 2017-08-17 16:14:25 -0400 | [diff] [blame] | 232 | } |
| 233 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 234 | //////////////////////////////////////////////////////////////////////////////// |
| 235 | // return true on success; false on failure |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 236 | bool GrSoftwarePathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 237 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 238 | "GrSoftwarePathRenderer::onDrawPath"); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 239 | if (!fProxyProvider) { |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 240 | return false; |
| 241 | } |
| 242 | |
caryclark | d656200 | 2016-07-27 12:02:07 -0700 | [diff] [blame] | 243 | SkASSERT(!args.fShape->style().applies()); |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 244 | // 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] | 245 | // If the path is hairline, ignore inverse fill. |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 246 | bool inverseFilled = args.fShape->inverseFilled() && |
| 247 | !IsStrokeHairlineOrEquivalent(args.fShape->style(), |
| 248 | *args.fViewMatrix, nullptr); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 249 | |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 250 | SkIRect unclippedDevShapeBounds, clippedDevShapeBounds, devClipBounds; |
| 251 | // To prevent overloading the cache with entries during animations we limit the cache of masks |
| 252 | // to cases where the matrix preserves axis alignment. |
| 253 | bool useCache = fAllowCaching && !inverseFilled && args.fViewMatrix->preservesAxisAlignment() && |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 254 | args.fShape->hasUnstyledKey() && GrAAType::kCoverage == args.fAAType; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 255 | |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 256 | if (!GetShapeAndClipBounds(args.fRenderTargetContext, |
| 257 | *args.fClip, *args.fShape, |
| 258 | *args.fViewMatrix, &unclippedDevShapeBounds, |
| 259 | &clippedDevShapeBounds, |
| 260 | &devClipBounds)) { |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 261 | if (inverseFilled) { |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 262 | DrawAroundInvPath(args.fRenderTargetContext, std::move(args.fPaint), |
| 263 | *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix, |
| 264 | devClipBounds, unclippedDevShapeBounds); |
bsalomon@google.com | 276c1fa | 2012-06-19 13:22:45 +0000 | [diff] [blame] | 265 | } |
| 266 | return true; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 267 | } |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 268 | |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 269 | const SkIRect* boundsForMask = &clippedDevShapeBounds; |
| 270 | if (useCache) { |
| 271 | // Use the cache only if >50% of the path is visible. |
| 272 | int unclippedWidth = unclippedDevShapeBounds.width(); |
| 273 | int unclippedHeight = unclippedDevShapeBounds.height(); |
Brian Osman | 1a0ea73 | 2017-09-28 11:53:03 -0400 | [diff] [blame] | 274 | int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight); |
| 275 | int64_t clippedArea = sk_64_mul(clippedDevShapeBounds.width(), |
| 276 | clippedDevShapeBounds.height()); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 277 | int maxTextureSize = args.fRenderTargetContext->caps()->maxTextureSize(); |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 278 | if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize || |
| 279 | unclippedHeight > maxTextureSize) { |
| 280 | useCache = false; |
| 281 | } else { |
| 282 | boundsForMask = &unclippedDevShapeBounds; |
| 283 | } |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 284 | } |
| 285 | |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 286 | GrUniqueKey maskKey; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 287 | if (useCache) { |
| 288 | // We require the upper left 2x2 of the matrix to match exactly for a cache hit. |
| 289 | SkScalar sx = args.fViewMatrix->get(SkMatrix::kMScaleX); |
| 290 | SkScalar sy = args.fViewMatrix->get(SkMatrix::kMScaleY); |
| 291 | SkScalar kx = args.fViewMatrix->get(SkMatrix::kMSkewX); |
| 292 | SkScalar ky = args.fViewMatrix->get(SkMatrix::kMSkewY); |
Stan Iliev | 67cd673 | 2017-08-15 17:10:26 -0400 | [diff] [blame] | 293 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
Brian Osman | a442526 | 2018-07-26 13:37:53 -0400 | [diff] [blame] | 294 | GrUniqueKey::Builder builder(&maskKey, kDomain, 5 + args.fShape->unstyledKeySize(), |
| 295 | "SW Path Mask"); |
Stan Iliev | 67cd673 | 2017-08-15 17:10:26 -0400 | [diff] [blame] | 296 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 297 | // Fractional translate does not affect caching on Android. This is done for better cache |
| 298 | // hit ratio and speed, but it is matching HWUI behavior, which doesn't consider the matrix |
| 299 | // at all when caching paths. |
Brian Osman | a442526 | 2018-07-26 13:37:53 -0400 | [diff] [blame] | 300 | SkFixed fracX = 0; |
| 301 | SkFixed fracY = 0; |
Stan Iliev | 67cd673 | 2017-08-15 17:10:26 -0400 | [diff] [blame] | 302 | #else |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 303 | SkScalar tx = args.fViewMatrix->get(SkMatrix::kMTransX); |
| 304 | SkScalar ty = args.fViewMatrix->get(SkMatrix::kMTransY); |
| 305 | // Allow 8 bits each in x and y of subpixel positioning. |
| 306 | SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00; |
| 307 | SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00; |
Stan Iliev | 67cd673 | 2017-08-15 17:10:26 -0400 | [diff] [blame] | 308 | #endif |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 309 | builder[0] = SkFloat2Bits(sx); |
| 310 | builder[1] = SkFloat2Bits(sy); |
| 311 | builder[2] = SkFloat2Bits(kx); |
| 312 | builder[3] = SkFloat2Bits(ky); |
Brian Osman | a442526 | 2018-07-26 13:37:53 -0400 | [diff] [blame] | 313 | // Distinguish between hairline and filled paths. For hairlines, we also need to include |
| 314 | // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that |
| 315 | // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers |
| 316 | // all cases we might see. |
| 317 | uint32_t styleBits = args.fShape->style().isSimpleHairline() ? |
| 318 | ((args.fShape->style().strokeRec().getCap() << 1) | 1) : 0; |
| 319 | builder[4] = fracX | (fracY >> 8) | (styleBits << 16); |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 320 | args.fShape->writeUnstyledKey(&builder[5]); |
| 321 | } |
| 322 | |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 323 | sk_sp<GrTextureProxy> proxy; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 324 | if (useCache) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 325 | proxy = fProxyProvider->findOrCreateProxyByUniqueKey(maskKey, kTopLeft_GrSurfaceOrigin); |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 326 | } |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 327 | if (!proxy) { |
Robert Phillips | 417b7f4 | 2016-12-14 09:12:13 -0500 | [diff] [blame] | 328 | SkBackingFit fit = useCache ? SkBackingFit::kExact : SkBackingFit::kApprox; |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 329 | GrAA aa = GrAAType::kCoverage == args.fAAType ? GrAA::kYes : GrAA::kNo; |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 330 | |
| 331 | SkTaskGroup* taskGroup = args.fContext->contextPriv().getTaskGroup(); |
| 332 | if (taskGroup) { |
| 333 | proxy = make_deferred_mask_texture_proxy(args.fContext, fit, |
| 334 | boundsForMask->width(), |
| 335 | boundsForMask->height()); |
| 336 | if (!proxy) { |
| 337 | return false; |
| 338 | } |
| 339 | |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 340 | auto uploader = skstd::make_unique<GrTDeferredProxyUploader<SoftwarePathData>>( |
| 341 | *boundsForMask, *args.fViewMatrix, *args.fShape, aa); |
| 342 | GrTDeferredProxyUploader<SoftwarePathData>* uploaderRaw = uploader.get(); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 343 | |
| 344 | auto drawAndUploadMask = [uploaderRaw] { |
| 345 | TRACE_EVENT0("skia", "Threaded SW Mask Render"); |
| 346 | GrSWMaskHelper helper(uploaderRaw->getPixels()); |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 347 | if (helper.init(uploaderRaw->data().getMaskBounds())) { |
| 348 | helper.drawShape(uploaderRaw->data().getShape(), |
| 349 | *uploaderRaw->data().getViewMatrix(), |
| 350 | SkRegion::kReplace_Op, uploaderRaw->data().getAA(), 0xFF); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 351 | } else { |
| 352 | SkDEBUGFAIL("Unable to allocate SW mask."); |
| 353 | } |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 354 | uploaderRaw->signalAndFreeData(); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 355 | }; |
| 356 | taskGroup->add(std::move(drawAndUploadMask)); |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 357 | proxy->texPriv().setDeferredUploader(std::move(uploader)); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 358 | } else { |
| 359 | GrSWMaskHelper helper; |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 360 | if (!helper.init(*boundsForMask)) { |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 361 | return false; |
| 362 | } |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 363 | helper.drawShape(*args.fShape, *args.fViewMatrix, SkRegion::kReplace_Op, aa, 0xFF); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 364 | proxy = helper.toTextureProxy(args.fContext, fit); |
| 365 | } |
| 366 | |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 367 | if (!proxy) { |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 368 | return false; |
| 369 | } |
| 370 | if (useCache) { |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 371 | SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 372 | fProxyProvider->assignUniqueKeyToProxy(maskKey, proxy.get()); |
Robert Phillips | 869fe56 | 2018-09-17 14:55:49 -0400 | [diff] [blame^] | 373 | args.fShape->addGenIDChangeListener( |
| 374 | sk_make_sp<PathInvalidator>(maskKey, args.fContext->uniqueID())); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 375 | } |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 376 | } |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 377 | if (inverseFilled) { |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 378 | DrawAroundInvPath(args.fRenderTargetContext, GrPaint::Clone(args.fPaint), |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 379 | *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix, devClipBounds, |
| 380 | unclippedDevShapeBounds); |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 381 | } |
Brian Osman | c7da146 | 2017-08-17 16:14:25 -0400 | [diff] [blame] | 382 | DrawToTargetWithShapeMask( |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 383 | std::move(proxy), args.fRenderTargetContext, std::move(args.fPaint), |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 384 | *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix, |
| 385 | SkIPoint{boundsForMask->fLeft, boundsForMask->fTop}, *boundsForMask); |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 386 | |
| 387 | return true; |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 388 | } |