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