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@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 9 | #include "GrSWMaskHelper.h" |
robertphillips | 0152d73 | 2016-05-20 06:38:43 -0700 | [diff] [blame^] | 10 | #include "GrTextureProvider.h" |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 11 | #include "batches/GrRectBatchFactory.h" |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 12 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 13 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 14 | bool GrSoftwarePathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
robertphillips | 0152d73 | 2016-05-20 06:38:43 -0700 | [diff] [blame^] | 15 | return SkToBool(fTexProvider); |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 16 | } |
| 17 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 18 | namespace { |
| 19 | |
| 20 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 21 | // gets device coord bounds of path (not considering the fill) and clip. The |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 22 | // path bounds will be a subset of the clip bounds. returns false if |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 23 | // path bounds would be empty. |
robertphillips | 0152d73 | 2016-05-20 06:38:43 -0700 | [diff] [blame^] | 24 | bool get_path_and_clip_bounds(int width, int height, |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 25 | const GrClip& clip, |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 26 | const SkPath& path, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 27 | const SkMatrix& matrix, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 28 | SkIRect* devPathBounds, |
| 29 | SkIRect* devClipBounds) { |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 30 | // compute bounds as intersection of rt size, clip, and path |
robertphillips | 0152d73 | 2016-05-20 06:38:43 -0700 | [diff] [blame^] | 31 | clip.getConservativeBounds(width, height, devClipBounds); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 32 | |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 33 | if (devClipBounds->isEmpty()) { |
robertphillips | 0152d73 | 2016-05-20 06:38:43 -0700 | [diff] [blame^] | 34 | *devPathBounds = SkIRect::MakeWH(width, height); |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 35 | return false; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 36 | } |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 37 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 38 | if (!path.getBounds().isEmpty()) { |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 39 | SkRect pathSBounds; |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 40 | matrix.mapRect(&pathSBounds, path.getBounds()); |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 41 | SkIRect pathIBounds; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 42 | pathSBounds.roundOut(&pathIBounds); |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 43 | *devPathBounds = *devClipBounds; |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 44 | if (!devPathBounds->intersect(pathIBounds)) { |
bsalomon@google.com | 276c1fa | 2012-06-19 13:22:45 +0000 | [diff] [blame] | 45 | // set the correct path bounds, as this would be used later. |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 46 | *devPathBounds = pathIBounds; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 47 | return false; |
| 48 | } |
| 49 | } else { |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 50 | *devPathBounds = SkIRect::EmptyIRect(); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 51 | return false; |
| 52 | } |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | //////////////////////////////////////////////////////////////////////////////// |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 57 | static void draw_non_aa_rect(GrDrawTarget* drawTarget, |
| 58 | const GrPipelineBuilder& pipelineBuilder, |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 59 | const GrClip& clip, |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 60 | GrColor color, |
| 61 | const SkMatrix& viewMatrix, |
| 62 | const SkRect& rect, |
| 63 | const SkMatrix& localMatrix) { |
| 64 | SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect, |
| 65 | nullptr, &localMatrix)); |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 66 | drawTarget->drawBatch(pipelineBuilder, clip, batch); |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 67 | } |
| 68 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 69 | void draw_around_inv_path(GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 70 | GrPipelineBuilder* pipelineBuilder, |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 71 | const GrClip& clip, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 72 | GrColor color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 73 | const SkMatrix& viewMatrix, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 74 | const SkIRect& devClipBounds, |
| 75 | const SkIRect& devPathBounds) { |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 76 | SkMatrix invert; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 77 | if (!viewMatrix.invert(&invert)) { |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 78 | return; |
| 79 | } |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 80 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 81 | SkRect rect; |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 82 | if (devClipBounds.fTop < devPathBounds.fTop) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 83 | rect.iset(devClipBounds.fLeft, devClipBounds.fTop, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 84 | devClipBounds.fRight, devPathBounds.fTop); |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 85 | draw_non_aa_rect(target, *pipelineBuilder, clip, color, SkMatrix::I(), rect, invert); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 86 | } |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 87 | if (devClipBounds.fLeft < devPathBounds.fLeft) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 88 | rect.iset(devClipBounds.fLeft, devPathBounds.fTop, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 89 | devPathBounds.fLeft, devPathBounds.fBottom); |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 90 | draw_non_aa_rect(target, *pipelineBuilder, clip, color, SkMatrix::I(), rect, invert); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 91 | } |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 92 | if (devClipBounds.fRight > devPathBounds.fRight) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 93 | rect.iset(devPathBounds.fRight, devPathBounds.fTop, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 94 | devClipBounds.fRight, devPathBounds.fBottom); |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 95 | draw_non_aa_rect(target, *pipelineBuilder, clip, color, SkMatrix::I(), rect, invert); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 96 | } |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 97 | if (devClipBounds.fBottom > devPathBounds.fBottom) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 98 | rect.iset(devClipBounds.fLeft, devPathBounds.fBottom, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 99 | devClipBounds.fRight, devClipBounds.fBottom); |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 100 | draw_non_aa_rect(target, *pipelineBuilder, clip, color, SkMatrix::I(), rect, invert); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | } |
| 105 | |
| 106 | //////////////////////////////////////////////////////////////////////////////// |
| 107 | // return true on success; false on failure |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 108 | bool GrSoftwarePathRenderer::onDrawPath(const DrawPathArgs& args) { |
joshualitt | de83b41 | 2016-01-14 09:58:36 -0800 | [diff] [blame] | 109 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fTarget->getAuditTrail(), "GrSoftwarePathRenderer::onDrawPath"); |
robertphillips | 0152d73 | 2016-05-20 06:38:43 -0700 | [diff] [blame^] | 110 | if (!fTexProvider || !args.fPipelineBuilder->getRenderTarget()) { |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 111 | return false; |
| 112 | } |
| 113 | |
robertphillips | 0152d73 | 2016-05-20 06:38:43 -0700 | [diff] [blame^] | 114 | const int width = args.fPipelineBuilder->getRenderTarget()->width(); |
| 115 | const int height = args.fPipelineBuilder->getRenderTarget()->height(); |
| 116 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 117 | SkIRect devPathBounds, devClipBounds; |
robertphillips | 0152d73 | 2016-05-20 06:38:43 -0700 | [diff] [blame^] | 118 | if (!get_path_and_clip_bounds(width, height, *args.fClip, *args.fPath, |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 119 | *args.fViewMatrix, &devPathBounds, &devClipBounds)) { |
| 120 | if (args.fPath->isInverseFillType()) { |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 121 | draw_around_inv_path(args.fTarget, args.fPipelineBuilder, *args.fClip, args.fColor, |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 122 | *args.fViewMatrix, devClipBounds, devPathBounds); |
bsalomon@google.com | 276c1fa | 2012-06-19 13:22:45 +0000 | [diff] [blame] | 123 | } |
| 124 | return true; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 125 | } |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 126 | |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 127 | SkAutoTUnref<GrTexture> texture( |
robertphillips | 0152d73 | 2016-05-20 06:38:43 -0700 | [diff] [blame^] | 128 | GrSWMaskHelper::DrawPathMaskToTexture(fTexProvider, *args.fPath, *args.fStyle, |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 129 | devPathBounds, |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 130 | args.fAntiAlias, args.fViewMatrix)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 131 | if (nullptr == texture) { |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 132 | return false; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 133 | } |
| 134 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 135 | GrSWMaskHelper::DrawToTargetWithPathMask(texture, args.fTarget, args.fPipelineBuilder, |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 136 | *args.fClip, args.fColor, *args.fViewMatrix, |
| 137 | devPathBounds); |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 138 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 139 | if (args.fPath->isInverseFillType()) { |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 140 | draw_around_inv_path(args.fTarget, args.fPipelineBuilder, *args.fClip, args.fColor, |
| 141 | *args.fViewMatrix, devClipBounds, devPathBounds); |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | return true; |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 145 | } |