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