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" |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 16 | #include "GrResourceProvider.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. |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 30 | if (!args.fShape->style().applies() && SkToBool(fResourceProvider) && |
| 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) { |
| 172 | GrSurfaceDesc desc; |
| 173 | desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
| 174 | desc.fWidth = width; |
| 175 | desc.fHeight = height; |
| 176 | desc.fConfig = kAlpha_8_GrPixelConfig; |
Brian Osman | d140fe9 | 2017-10-03 12:17:26 -0400 | [diff] [blame] | 177 | // MDB TODO: We're going to fill this proxy with an ASAP upload (which is out of order wrt to |
| 178 | // ops), so it can't have any pending IO. |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 179 | return GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, fit, SkBudgeted::kYes, |
| 180 | GrResourceProvider::kNoPendingIO_Flag); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | namespace { |
| 184 | |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 185 | /** |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 186 | * Payload class for use with GrTDeferredProxyUploader. The software path renderer only draws |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 187 | * a single path into the mask texture. This stores all of the information needed by the worker |
| 188 | * thread's call to drawShape (see below, in onDrawPath). |
| 189 | */ |
| 190 | class SoftwarePathData { |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 191 | public: |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 192 | SoftwarePathData(const SkIRect& maskBounds, const SkMatrix& viewMatrix, const GrShape& shape, |
| 193 | GrAA aa) |
| 194 | : fMaskBounds(maskBounds) |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 195 | , fViewMatrix(viewMatrix) |
| 196 | , fShape(shape) |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 197 | , fAA(aa) {} |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 198 | |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 199 | const SkIRect& getMaskBounds() const { return fMaskBounds; } |
| 200 | const SkMatrix* getViewMatrix() const { return &fViewMatrix; } |
| 201 | const GrShape& getShape() const { return fShape; } |
| 202 | GrAA getAA() const { return fAA; } |
| 203 | |
| 204 | private: |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 205 | SkIRect fMaskBounds; |
| 206 | SkMatrix fViewMatrix; |
| 207 | GrShape fShape; |
| 208 | GrAA fAA; |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 209 | }; |
| 210 | |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 211 | // When the SkPathRef genID changes, invalidate a corresponding GrResource described by key. |
| 212 | class PathInvalidator : public SkPathRef::GenIDChangeListener { |
| 213 | public: |
| 214 | explicit PathInvalidator(const GrUniqueKey& key) : fMsg(key) {} |
| 215 | private: |
| 216 | GrUniqueKeyInvalidatedMessage fMsg; |
| 217 | |
| 218 | void onChange() override { |
| 219 | SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); |
| 220 | } |
| 221 | }; |
| 222 | |
Brian Osman | c7da146 | 2017-08-17 16:14:25 -0400 | [diff] [blame] | 223 | } |
| 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) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 228 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 229 | "GrSoftwarePathRenderer::onDrawPath"); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 230 | if (!fResourceProvider) { |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 231 | return false; |
| 232 | } |
| 233 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 234 | // We really need to know if the shape will be inverse filled or not |
| 235 | bool inverseFilled = false; |
| 236 | SkTLazy<GrShape> tmpShape; |
caryclark | d656200 | 2016-07-27 12:02:07 -0700 | [diff] [blame] | 237 | SkASSERT(!args.fShape->style().applies()); |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 238 | // If the path is hairline, ignore inverse fill. |
| 239 | inverseFilled = args.fShape->inverseFilled() && |
| 240 | !IsStrokeHairlineOrEquivalent(args.fShape->style(), *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() && |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 246 | args.fShape->hasUnstyledKey() && GrAAType::kCoverage == args.fAAType; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 247 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 248 | if (!get_shape_and_clip_bounds(args.fRenderTargetContext->width(), |
| 249 | args.fRenderTargetContext->height(), |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 250 | *args.fClip, *args.fShape, |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 251 | *args.fViewMatrix, &unclippedDevShapeBounds, |
| 252 | &clippedDevShapeBounds, |
| 253 | &devClipBounds)) { |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 254 | if (inverseFilled) { |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 255 | DrawAroundInvPath(args.fRenderTargetContext, std::move(args.fPaint), |
| 256 | *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix, |
| 257 | devClipBounds, unclippedDevShapeBounds); |
bsalomon@google.com | 276c1fa | 2012-06-19 13:22:45 +0000 | [diff] [blame] | 258 | } |
| 259 | return true; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 260 | } |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 261 | |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 262 | const SkIRect* boundsForMask = &clippedDevShapeBounds; |
| 263 | if (useCache) { |
| 264 | // Use the cache only if >50% of the path is visible. |
| 265 | int unclippedWidth = unclippedDevShapeBounds.width(); |
| 266 | int unclippedHeight = unclippedDevShapeBounds.height(); |
Brian Osman | 1a0ea73 | 2017-09-28 11:53:03 -0400 | [diff] [blame] | 267 | int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight); |
| 268 | int64_t clippedArea = sk_64_mul(clippedDevShapeBounds.width(), |
| 269 | clippedDevShapeBounds.height()); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 270 | int maxTextureSize = args.fRenderTargetContext->caps()->maxTextureSize(); |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 271 | if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize || |
| 272 | unclippedHeight > maxTextureSize) { |
| 273 | useCache = false; |
| 274 | } else { |
| 275 | boundsForMask = &unclippedDevShapeBounds; |
| 276 | } |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 277 | } |
| 278 | |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 279 | GrUniqueKey maskKey; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 280 | if (useCache) { |
| 281 | // We require the upper left 2x2 of the matrix to match exactly for a cache hit. |
| 282 | SkScalar sx = args.fViewMatrix->get(SkMatrix::kMScaleX); |
| 283 | SkScalar sy = args.fViewMatrix->get(SkMatrix::kMScaleY); |
| 284 | SkScalar kx = args.fViewMatrix->get(SkMatrix::kMSkewX); |
| 285 | SkScalar ky = args.fViewMatrix->get(SkMatrix::kMSkewY); |
Stan Iliev | 67cd673 | 2017-08-15 17:10:26 -0400 | [diff] [blame] | 286 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 287 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 288 | // Fractional translate does not affect caching on Android. This is done for better cache |
| 289 | // hit ratio and speed, but it is matching HWUI behavior, which doesn't consider the matrix |
| 290 | // at all when caching paths. |
| 291 | GrUniqueKey::Builder builder(&maskKey, kDomain, 4 + args.fShape->unstyledKeySize()); |
| 292 | #else |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 293 | 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; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 298 | GrUniqueKey::Builder builder(&maskKey, kDomain, 5 + args.fShape->unstyledKeySize()); |
Stan Iliev | 67cd673 | 2017-08-15 17:10:26 -0400 | [diff] [blame] | 299 | #endif |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 300 | builder[0] = SkFloat2Bits(sx); |
| 301 | builder[1] = SkFloat2Bits(sy); |
| 302 | builder[2] = SkFloat2Bits(kx); |
| 303 | builder[3] = SkFloat2Bits(ky); |
Stan Iliev | 67cd673 | 2017-08-15 17:10:26 -0400 | [diff] [blame] | 304 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 305 | args.fShape->writeUnstyledKey(&builder[4]); |
| 306 | #else |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 307 | builder[4] = fracX | (fracY >> 8); |
| 308 | args.fShape->writeUnstyledKey(&builder[5]); |
Stan Iliev | 67cd673 | 2017-08-15 17:10:26 -0400 | [diff] [blame] | 309 | #endif |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 312 | sk_sp<GrTextureProxy> proxy; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 313 | if (useCache) { |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 314 | proxy = fResourceProvider->findOrCreateProxyByUniqueKey(maskKey, kTopLeft_GrSurfaceOrigin); |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 315 | } |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 316 | if (!proxy) { |
Robert Phillips | 417b7f4 | 2016-12-14 09:12:13 -0500 | [diff] [blame] | 317 | SkBackingFit fit = useCache ? SkBackingFit::kExact : SkBackingFit::kApprox; |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 318 | GrAA aa = GrAAType::kCoverage == args.fAAType ? GrAA::kYes : GrAA::kNo; |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 319 | |
| 320 | SkTaskGroup* taskGroup = args.fContext->contextPriv().getTaskGroup(); |
| 321 | if (taskGroup) { |
| 322 | proxy = make_deferred_mask_texture_proxy(args.fContext, fit, |
| 323 | boundsForMask->width(), |
| 324 | boundsForMask->height()); |
| 325 | if (!proxy) { |
| 326 | return false; |
| 327 | } |
| 328 | |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 329 | auto uploader = skstd::make_unique<GrTDeferredProxyUploader<SoftwarePathData>>( |
| 330 | *boundsForMask, *args.fViewMatrix, *args.fShape, aa); |
| 331 | GrTDeferredProxyUploader<SoftwarePathData>* uploaderRaw = uploader.get(); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 332 | |
| 333 | auto drawAndUploadMask = [uploaderRaw] { |
| 334 | TRACE_EVENT0("skia", "Threaded SW Mask Render"); |
| 335 | GrSWMaskHelper helper(uploaderRaw->getPixels()); |
Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 336 | if (helper.init(uploaderRaw->data().getMaskBounds())) { |
| 337 | helper.drawShape(uploaderRaw->data().getShape(), |
| 338 | *uploaderRaw->data().getViewMatrix(), |
| 339 | SkRegion::kReplace_Op, uploaderRaw->data().getAA(), 0xFF); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 340 | } else { |
| 341 | SkDEBUGFAIL("Unable to allocate SW mask."); |
| 342 | } |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 343 | uploaderRaw->signalAndFreeData(); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 344 | }; |
| 345 | taskGroup->add(std::move(drawAndUploadMask)); |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 346 | proxy->texPriv().setDeferredUploader(std::move(uploader)); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 347 | } else { |
| 348 | GrSWMaskHelper helper; |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 349 | if (!helper.init(*boundsForMask)) { |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 350 | return false; |
| 351 | } |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 352 | helper.drawShape(*args.fShape, *args.fViewMatrix, SkRegion::kReplace_Op, aa, 0xFF); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 353 | proxy = helper.toTextureProxy(args.fContext, fit); |
| 354 | } |
| 355 | |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 356 | if (!proxy) { |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 357 | return false; |
| 358 | } |
| 359 | if (useCache) { |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 360 | SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin); |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 361 | fResourceProvider->assignUniqueKeyToProxy(maskKey, proxy.get()); |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 362 | args.fShape->addGenIDChangeListener(new PathInvalidator(maskKey)); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 363 | } |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 364 | } |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 365 | if (inverseFilled) { |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 366 | DrawAroundInvPath(args.fRenderTargetContext, GrPaint::Clone(args.fPaint), |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 367 | *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix, devClipBounds, |
| 368 | unclippedDevShapeBounds); |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 369 | } |
Brian Osman | c7da146 | 2017-08-17 16:14:25 -0400 | [diff] [blame] | 370 | DrawToTargetWithShapeMask( |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 371 | std::move(proxy), args.fRenderTargetContext, std::move(args.fPaint), |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 372 | *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix, |
| 373 | SkIPoint{boundsForMask->fLeft, boundsForMask->fTop}, *boundsForMask); |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 374 | |
| 375 | return true; |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 376 | } |