robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include "GrSoftwarePathRenderer.h" |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 10 | #include "GrContext.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 11 | #include "GrSWMaskHelper.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 | //////////////////////////////////////////////////////////////////////////////// |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 14 | bool GrSoftwarePathRenderer::canDrawPath(const SkPath&, |
| 15 | const SkStrokeRec&, |
| 16 | const GrDrawTarget*, |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 17 | bool antiAlias) const { |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 18 | if (!antiAlias || NULL == fContext) { |
| 19 | // TODO: We could allow the SW path to also handle non-AA paths but |
| 20 | // this would mean that GrDefaultPathRenderer would never be called |
| 21 | // (since it appears after the SW renderer in the path renderer |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 22 | // chain). Some testing would need to be done r.e. performance |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 23 | // and consistency of the resulting images before removing |
| 24 | // the "!antiAlias" clause from the above test |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 25 | return false; |
| 26 | } |
| 27 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 28 | return true; |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 29 | } |
| 30 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 31 | GrPathRenderer::StencilSupport GrSoftwarePathRenderer::onGetStencilSupport( |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame^] | 32 | const SkPath&, |
| 33 | const SkStrokeRec&, |
| 34 | const GrDrawTarget*) const { |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 35 | return GrPathRenderer::kNoSupport_StencilSupport; |
| 36 | } |
| 37 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 38 | namespace { |
| 39 | |
| 40 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 41 | // 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] | 42 | // 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] | 43 | // path bounds would be empty. |
| 44 | bool get_path_and_clip_bounds(const GrDrawTarget* target, |
| 45 | const SkPath& path, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 46 | const SkMatrix& matrix, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame^] | 47 | SkIRect* devPathBounds, |
| 48 | SkIRect* devClipBounds) { |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 49 | // compute bounds as intersection of rt size, clip, and path |
| 50 | const GrRenderTarget* rt = target->getDrawState().getRenderTarget(); |
| 51 | if (NULL == rt) { |
| 52 | return false; |
| 53 | } |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame^] | 54 | *devPathBounds = SkIRect::MakeWH(rt->width(), rt->height()); |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 55 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 56 | target->getClip()->getConservativeBounds(rt, devClipBounds); |
| 57 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 58 | // TODO: getConservativeBounds already intersects with the |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 59 | // render target's bounding box. Remove this next line |
| 60 | if (!devPathBounds->intersect(*devClipBounds)) { |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 61 | return false; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 62 | } |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 63 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 64 | if (!path.getBounds().isEmpty()) { |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame^] | 65 | SkRect pathSBounds; |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 66 | matrix.mapRect(&pathSBounds, path.getBounds()); |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame^] | 67 | SkIRect pathIBounds; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 68 | pathSBounds.roundOut(&pathIBounds); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 69 | if (!devPathBounds->intersect(pathIBounds)) { |
bsalomon@google.com | 276c1fa | 2012-06-19 13:22:45 +0000 | [diff] [blame] | 70 | // set the correct path bounds, as this would be used later. |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 71 | *devPathBounds = pathIBounds; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 72 | return false; |
| 73 | } |
| 74 | } else { |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame^] | 75 | *devPathBounds = SkIRect::EmptyIRect(); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 76 | return false; |
| 77 | } |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 82 | void draw_around_inv_path(GrDrawTarget* target, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame^] | 83 | const SkIRect& devClipBounds, |
| 84 | const SkIRect& devPathBounds) { |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 85 | GrDrawState::AutoViewMatrixRestore avmr; |
| 86 | if (!avmr.setIdentity(target->drawState())) { |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 87 | return; |
| 88 | } |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame^] | 89 | SkRect rect; |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 90 | if (devClipBounds.fTop < devPathBounds.fTop) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 91 | rect.iset(devClipBounds.fLeft, devClipBounds.fTop, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 92 | devClipBounds.fRight, devPathBounds.fTop); |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 93 | target->drawSimpleRect(rect, NULL); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 94 | } |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 95 | if (devClipBounds.fLeft < devPathBounds.fLeft) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 96 | rect.iset(devClipBounds.fLeft, devPathBounds.fTop, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 97 | devPathBounds.fLeft, devPathBounds.fBottom); |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 98 | target->drawSimpleRect(rect, NULL); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 99 | } |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 100 | if (devClipBounds.fRight > devPathBounds.fRight) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 101 | rect.iset(devPathBounds.fRight, devPathBounds.fTop, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 102 | devClipBounds.fRight, devPathBounds.fBottom); |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 103 | target->drawSimpleRect(rect, NULL); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 104 | } |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 105 | if (devClipBounds.fBottom > devPathBounds.fBottom) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 106 | rect.iset(devClipBounds.fLeft, devPathBounds.fBottom, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 107 | devClipBounds.fRight, devClipBounds.fBottom); |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 108 | target->drawSimpleRect(rect, NULL); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | } |
| 113 | |
| 114 | //////////////////////////////////////////////////////////////////////////////// |
| 115 | // return true on success; false on failure |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 116 | bool GrSoftwarePathRenderer::onDrawPath(const SkPath& path, |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 117 | const SkStrokeRec& stroke, |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 118 | GrDrawTarget* target, |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 119 | bool antiAlias) { |
| 120 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 121 | if (NULL == fContext) { |
| 122 | return false; |
| 123 | } |
| 124 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 125 | GrDrawState* drawState = target->drawState(); |
| 126 | |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 127 | SkMatrix vm = drawState->getViewMatrix(); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 128 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame^] | 129 | SkIRect devPathBounds, devClipBounds; |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 130 | if (!get_path_and_clip_bounds(target, path, vm, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 131 | &devPathBounds, &devClipBounds)) { |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 132 | if (path.isInverseFillType()) { |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 133 | draw_around_inv_path(target, devClipBounds, devPathBounds); |
bsalomon@google.com | 276c1fa | 2012-06-19 13:22:45 +0000 | [diff] [blame] | 134 | } |
| 135 | return true; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 136 | } |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 137 | |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 138 | SkAutoTUnref<GrTexture> texture( |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 139 | GrSWMaskHelper::DrawPathMaskToTexture(fContext, path, stroke, |
| 140 | devPathBounds, |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 141 | antiAlias, &vm)); |
| 142 | if (NULL == texture) { |
| 143 | return false; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 144 | } |
| 145 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 146 | GrSWMaskHelper::DrawToTargetWithPathMask(texture, target, devPathBounds); |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 147 | |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 148 | if (path.isInverseFillType()) { |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 149 | draw_around_inv_path(target, devClipBounds, devPathBounds); |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | return true; |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 153 | } |