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" |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 11 | #include "GrPipelineBuilder.h" |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 12 | #include "GrGpuResourcePriv.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 13 | #include "GrSWMaskHelper.h" |
robertphillips | 0152d73 | 2016-05-20 06:38:43 -0700 | [diff] [blame] | 14 | #include "GrTextureProvider.h" |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 15 | #include "batches/GrRectBatchFactory.h" |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 16 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 17 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 18 | bool GrSoftwarePathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 19 | // Pass on any style that applies. The caller will apply the style if a suitable renderer is |
| 20 | // not found and try again with the new GrShape. |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 21 | return !args.fShape->style().applies() && SkToBool(fTexProvider) && |
| 22 | (args.fAAType == GrAAType::kCoverage || args.fAAType == GrAAType::kNone); |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 23 | } |
| 24 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 25 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 26 | static bool get_unclipped_shape_dev_bounds(const GrShape& shape, const SkMatrix& matrix, |
| 27 | SkIRect* devBounds) { |
| 28 | SkRect shapeBounds = shape.styledBounds(); |
| 29 | if (shapeBounds.isEmpty()) { |
| 30 | return false; |
| 31 | } |
| 32 | SkRect shapeDevBounds; |
| 33 | matrix.mapRect(&shapeDevBounds, shapeBounds); |
| 34 | shapeDevBounds.roundOut(devBounds); |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | // Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there |
| 39 | // is no intersection. |
| 40 | static bool get_shape_and_clip_bounds(int width, int height, |
| 41 | const GrClip& clip, |
| 42 | const GrShape& shape, |
| 43 | const SkMatrix& matrix, |
| 44 | SkIRect* unclippedDevShapeBounds, |
| 45 | SkIRect* clippedDevShapeBounds, |
| 46 | SkIRect* devClipBounds) { |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 47 | // compute bounds as intersection of rt size, clip, and path |
robertphillips | 0152d73 | 2016-05-20 06:38:43 -0700 | [diff] [blame] | 48 | clip.getConservativeBounds(width, height, devClipBounds); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 49 | |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 50 | if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) { |
| 51 | *unclippedDevShapeBounds = SkIRect::EmptyIRect(); |
| 52 | *clippedDevShapeBounds = SkIRect::EmptyIRect(); |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 53 | return false; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 54 | } |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 55 | if (!clippedDevShapeBounds->intersect(*devClipBounds, *unclippedDevShapeBounds)) { |
| 56 | *clippedDevShapeBounds = SkIRect::EmptyIRect(); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 57 | return false; |
| 58 | } |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 63 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 64 | void GrSoftwarePathRenderer::DrawNonAARect(GrRenderTargetContext* renderTargetContext, |
robertphillips | 3950f0d | 2016-07-07 07:33:13 -0700 | [diff] [blame] | 65 | const GrPaint& paint, |
robertphillips | d2b6d64 | 2016-07-21 08:55:08 -0700 | [diff] [blame] | 66 | const GrUserStencilSettings& userStencilSettings, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 67 | const GrClip& clip, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 68 | const SkMatrix& viewMatrix, |
| 69 | const SkRect& rect, |
| 70 | const SkMatrix& localMatrix) { |
Brian Salomon | 24f1978 | 2016-12-13 15:10:11 -0500 | [diff] [blame] | 71 | sk_sp<GrDrawOp> op(GrRectBatchFactory::CreateNonAAFill(paint.getColor(), viewMatrix, rect, |
| 72 | nullptr, &localMatrix)); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 73 | |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 74 | GrPipelineBuilder pipelineBuilder(paint, GrAAType::kNone); |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 75 | pipelineBuilder.setUserStencil(&userStencilSettings); |
Brian Salomon | 24f1978 | 2016-12-13 15:10:11 -0500 | [diff] [blame] | 76 | renderTargetContext->addDrawOp(pipelineBuilder, clip, std::move(op)); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 79 | void GrSoftwarePathRenderer::DrawAroundInvPath(GrRenderTargetContext* renderTargetContext, |
robertphillips | 3950f0d | 2016-07-07 07:33:13 -0700 | [diff] [blame] | 80 | const GrPaint& paint, |
robertphillips | d2b6d64 | 2016-07-21 08:55:08 -0700 | [diff] [blame] | 81 | const GrUserStencilSettings& userStencilSettings, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 82 | const GrClip& clip, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 83 | const SkMatrix& viewMatrix, |
| 84 | const SkIRect& devClipBounds, |
| 85 | const SkIRect& devPathBounds) { |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 86 | SkMatrix invert; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 87 | if (!viewMatrix.invert(&invert)) { |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 88 | return; |
| 89 | } |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 90 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 91 | SkRect rect; |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 92 | if (devClipBounds.fTop < devPathBounds.fTop) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 93 | rect.iset(devClipBounds.fLeft, devClipBounds.fTop, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 94 | devClipBounds.fRight, devPathBounds.fTop); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 95 | DrawNonAARect(renderTargetContext, paint, userStencilSettings, clip, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 96 | SkMatrix::I(), rect, invert); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 97 | } |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 98 | if (devClipBounds.fLeft < devPathBounds.fLeft) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 99 | rect.iset(devClipBounds.fLeft, devPathBounds.fTop, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 100 | devPathBounds.fLeft, devPathBounds.fBottom); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 101 | DrawNonAARect(renderTargetContext, paint, userStencilSettings, clip, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 102 | SkMatrix::I(), rect, invert); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 103 | } |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 104 | if (devClipBounds.fRight > devPathBounds.fRight) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 105 | rect.iset(devPathBounds.fRight, devPathBounds.fTop, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 106 | devClipBounds.fRight, devPathBounds.fBottom); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 107 | DrawNonAARect(renderTargetContext, paint, userStencilSettings, clip, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 108 | SkMatrix::I(), rect, invert); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 109 | } |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 110 | if (devClipBounds.fBottom > devPathBounds.fBottom) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 111 | rect.iset(devClipBounds.fLeft, devPathBounds.fBottom, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 112 | devClipBounds.fRight, devClipBounds.fBottom); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 113 | DrawNonAARect(renderTargetContext, paint, userStencilSettings, clip, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 114 | SkMatrix::I(), rect, invert); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 118 | //////////////////////////////////////////////////////////////////////////////// |
| 119 | // return true on success; false on failure |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 120 | bool GrSoftwarePathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 121 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 122 | "GrSoftwarePathRenderer::onDrawPath"); |
| 123 | if (!fTexProvider) { |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 127 | // We really need to know if the shape will be inverse filled or not |
| 128 | bool inverseFilled = false; |
| 129 | SkTLazy<GrShape> tmpShape; |
caryclark | d656200 | 2016-07-27 12:02:07 -0700 | [diff] [blame] | 130 | SkASSERT(!args.fShape->style().applies()); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 131 | inverseFilled = args.fShape->inverseFilled(); |
| 132 | |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 133 | SkIRect unclippedDevShapeBounds, clippedDevShapeBounds, devClipBounds; |
| 134 | // To prevent overloading the cache with entries during animations we limit the cache of masks |
| 135 | // to cases where the matrix preserves axis alignment. |
| 136 | bool useCache = fAllowCaching && !inverseFilled && args.fViewMatrix->preservesAxisAlignment() && |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 137 | args.fShape->hasUnstyledKey() && GrAAType::kCoverage == args.fAAType; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 138 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 139 | if (!get_shape_and_clip_bounds(args.fRenderTargetContext->width(), |
| 140 | args.fRenderTargetContext->height(), |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 141 | *args.fClip, *args.fShape, |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 142 | *args.fViewMatrix, &unclippedDevShapeBounds, |
| 143 | &clippedDevShapeBounds, |
| 144 | &devClipBounds)) { |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 145 | if (inverseFilled) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 146 | DrawAroundInvPath(args.fRenderTargetContext, *args.fPaint, *args.fUserStencilSettings, |
robertphillips | 3950f0d | 2016-07-07 07:33:13 -0700 | [diff] [blame] | 147 | *args.fClip, |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 148 | *args.fViewMatrix, devClipBounds, unclippedDevShapeBounds); |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 149 | |
bsalomon@google.com | 276c1fa | 2012-06-19 13:22:45 +0000 | [diff] [blame] | 150 | } |
| 151 | return true; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 152 | } |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 153 | |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 154 | const SkIRect* boundsForMask = &clippedDevShapeBounds; |
| 155 | if (useCache) { |
| 156 | // Use the cache only if >50% of the path is visible. |
| 157 | int unclippedWidth = unclippedDevShapeBounds.width(); |
| 158 | int unclippedHeight = unclippedDevShapeBounds.height(); |
| 159 | int unclippedArea = unclippedWidth * unclippedHeight; |
| 160 | int clippedArea = clippedDevShapeBounds.width() * clippedDevShapeBounds.height(); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 161 | int maxTextureSize = args.fRenderTargetContext->caps()->maxTextureSize(); |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 162 | if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize || |
| 163 | unclippedHeight > maxTextureSize) { |
| 164 | useCache = false; |
| 165 | } else { |
| 166 | boundsForMask = &unclippedDevShapeBounds; |
| 167 | } |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 168 | } |
| 169 | |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 170 | GrUniqueKey maskKey; |
| 171 | struct KeyData { |
| 172 | SkScalar fFractionalTranslateX; |
| 173 | SkScalar fFractionalTranslateY; |
| 174 | }; |
| 175 | |
| 176 | if (useCache) { |
| 177 | // We require the upper left 2x2 of the matrix to match exactly for a cache hit. |
| 178 | SkScalar sx = args.fViewMatrix->get(SkMatrix::kMScaleX); |
| 179 | SkScalar sy = args.fViewMatrix->get(SkMatrix::kMScaleY); |
| 180 | SkScalar kx = args.fViewMatrix->get(SkMatrix::kMSkewX); |
| 181 | SkScalar ky = args.fViewMatrix->get(SkMatrix::kMSkewY); |
| 182 | SkScalar tx = args.fViewMatrix->get(SkMatrix::kMTransX); |
| 183 | SkScalar ty = args.fViewMatrix->get(SkMatrix::kMTransY); |
| 184 | // Allow 8 bits each in x and y of subpixel positioning. |
| 185 | SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00; |
| 186 | SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00; |
| 187 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 188 | GrUniqueKey::Builder builder(&maskKey, kDomain, 5 + args.fShape->unstyledKeySize()); |
| 189 | builder[0] = SkFloat2Bits(sx); |
| 190 | builder[1] = SkFloat2Bits(sy); |
| 191 | builder[2] = SkFloat2Bits(kx); |
| 192 | builder[3] = SkFloat2Bits(ky); |
| 193 | builder[4] = fracX | (fracY >> 8); |
| 194 | args.fShape->writeUnstyledKey(&builder[5]); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 195 | // FIXME: Doesn't the key need to consider whether we're using AA or not? In practice that |
| 196 | // should always be true, though. |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | sk_sp<GrTexture> texture; |
| 200 | if (useCache) { |
| 201 | texture.reset(args.fResourceProvider->findAndRefTextureByUniqueKey(maskKey)); |
| 202 | } |
| 203 | if (!texture) { |
Robert Phillips | 417b7f4 | 2016-12-14 09:12:13 -0500 | [diff] [blame^] | 204 | SkBackingFit fit = useCache ? SkBackingFit::kExact : SkBackingFit::kApprox; |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 205 | GrAA aa = GrAAType::kCoverage == args.fAAType ? GrAA::kYes : GrAA::kNo; |
| 206 | texture.reset(GrSWMaskHelper::DrawShapeMaskToTexture(fTexProvider, *args.fShape, |
| 207 | *boundsForMask, aa, |
Robert Phillips | 417b7f4 | 2016-12-14 09:12:13 -0500 | [diff] [blame^] | 208 | fit, args.fViewMatrix)); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 209 | if (!texture) { |
| 210 | return false; |
| 211 | } |
| 212 | if (useCache) { |
| 213 | texture->resourcePriv().setUniqueKey(maskKey); |
| 214 | } |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 215 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 216 | GrSWMaskHelper::DrawToTargetWithShapeMask(texture.get(), args.fRenderTargetContext, |
| 217 | *args.fPaint, *args.fUserStencilSettings, |
robertphillips | 3950f0d | 2016-07-07 07:33:13 -0700 | [diff] [blame] | 218 | *args.fClip, *args.fViewMatrix, |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 219 | SkIPoint {boundsForMask->fLeft, boundsForMask->fTop}, |
| 220 | *boundsForMask); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 221 | if (inverseFilled) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 222 | DrawAroundInvPath(args.fRenderTargetContext, *args.fPaint, *args.fUserStencilSettings, |
robertphillips | 3950f0d | 2016-07-07 07:33:13 -0700 | [diff] [blame] | 223 | *args.fClip, |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 224 | *args.fViewMatrix, devClipBounds, unclippedDevShapeBounds); |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | return true; |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 228 | } |