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 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 14 | bool GrSoftwarePathRenderer::canDrawPath(const SkPath& path, |
| 15 | GrPathFill fill, |
| 16 | const GrDrawTarget* target, |
| 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 |
| 22 | // chain). Some testing would need to be done r.e. performance |
| 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 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 34 | // gets device coord bounds of path (not considering the fill) and clip. The |
| 35 | // path bounds will be a subset of the clip bounds. returns false if |
| 36 | // path bounds would be empty. |
| 37 | bool get_path_and_clip_bounds(const GrDrawTarget* target, |
| 38 | const SkPath& path, |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 39 | const GrMatrix& matrix, |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 40 | GrIRect* pathBounds, |
| 41 | GrIRect* clipBounds) { |
| 42 | // compute bounds as intersection of rt size, clip, and path |
| 43 | const GrRenderTarget* rt = target->getDrawState().getRenderTarget(); |
| 44 | if (NULL == rt) { |
| 45 | return false; |
| 46 | } |
| 47 | *pathBounds = GrIRect::MakeWH(rt->width(), rt->height()); |
| 48 | const GrClip& clip = target->getClip(); |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 49 | |
| 50 | clip.getConservativeBounds().roundOut(clipBounds); |
| 51 | if (!pathBounds->intersect(*clipBounds)) { |
| 52 | return false; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 53 | } |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 54 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 55 | if (!path.getBounds().isEmpty()) { |
| 56 | GrRect pathSBounds; |
| 57 | matrix.mapRect(&pathSBounds, path.getBounds()); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 58 | GrIRect pathIBounds; |
| 59 | pathSBounds.roundOut(&pathIBounds); |
| 60 | if (!pathBounds->intersect(pathIBounds)) { |
bsalomon@google.com | 276c1fa | 2012-06-19 13:22:45 +0000 | [diff] [blame] | 61 | // set the correct path bounds, as this would be used later. |
| 62 | *pathBounds = pathIBounds; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 63 | return false; |
| 64 | } |
| 65 | } else { |
bsalomon@google.com | 276c1fa | 2012-06-19 13:22:45 +0000 | [diff] [blame] | 66 | *pathBounds = GrIRect::EmptyIRect(); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 73 | void draw_around_inv_path(GrDrawTarget* target, |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 74 | const GrIRect& clipBounds, |
| 75 | const GrIRect& pathBounds) { |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame^] | 76 | GrDrawTarget::AutoDeviceCoordDraw adcd(target); |
| 77 | if (!adcd.succeeded()) { |
| 78 | return; |
| 79 | } |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 80 | GrRect rect; |
| 81 | if (clipBounds.fTop < pathBounds.fTop) { |
| 82 | rect.iset(clipBounds.fLeft, clipBounds.fTop, |
| 83 | clipBounds.fRight, pathBounds.fTop); |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame^] | 84 | target->drawSimpleRect(rect, NULL); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 85 | } |
| 86 | if (clipBounds.fLeft < pathBounds.fLeft) { |
| 87 | rect.iset(clipBounds.fLeft, pathBounds.fTop, |
| 88 | pathBounds.fLeft, pathBounds.fBottom); |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame^] | 89 | target->drawSimpleRect(rect, NULL); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 90 | } |
| 91 | if (clipBounds.fRight > pathBounds.fRight) { |
| 92 | rect.iset(pathBounds.fRight, pathBounds.fTop, |
| 93 | clipBounds.fRight, pathBounds.fBottom); |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame^] | 94 | target->drawSimpleRect(rect, NULL); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 95 | } |
| 96 | if (clipBounds.fBottom > pathBounds.fBottom) { |
| 97 | rect.iset(clipBounds.fLeft, pathBounds.fBottom, |
| 98 | clipBounds.fRight, clipBounds.fBottom); |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame^] | 99 | target->drawSimpleRect(rect, NULL); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | } |
| 104 | |
| 105 | //////////////////////////////////////////////////////////////////////////////// |
| 106 | // return true on success; false on failure |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 107 | bool GrSoftwarePathRenderer::onDrawPath(const SkPath& path, |
| 108 | GrPathFill fill, |
| 109 | const GrVec* translate, |
| 110 | GrDrawTarget* target, |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 111 | bool antiAlias) { |
| 112 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 113 | if (NULL == fContext) { |
| 114 | return false; |
| 115 | } |
| 116 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 117 | GrDrawState* drawState = target->drawState(); |
| 118 | |
| 119 | GrMatrix vm = drawState->getViewMatrix(); |
| 120 | if (NULL != translate) { |
| 121 | vm.postTranslate(translate->fX, translate->fY); |
| 122 | } |
| 123 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 124 | GrIRect pathBounds, clipBounds; |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 125 | if (!get_path_and_clip_bounds(target, path, vm, |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 126 | &pathBounds, &clipBounds)) { |
bsalomon@google.com | 276c1fa | 2012-06-19 13:22:45 +0000 | [diff] [blame] | 127 | if (GrIsFillInverted(fill)) { |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame^] | 128 | draw_around_inv_path(target, clipBounds, pathBounds); |
bsalomon@google.com | 276c1fa | 2012-06-19 13:22:45 +0000 | [diff] [blame] | 129 | } |
| 130 | return true; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 131 | } |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 132 | |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 133 | SkAutoTUnref<GrTexture> texture( |
| 134 | GrSWMaskHelper::DrawPathMaskToTexture(fContext, path, |
| 135 | pathBounds, fill, |
| 136 | antiAlias, &vm)); |
| 137 | if (NULL == texture) { |
| 138 | return false; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 139 | } |
| 140 | |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame^] | 141 | GrSWMaskHelper::DrawToTargetWithPathMask(texture, target, pathBounds); |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 142 | |
| 143 | if (GrIsFillInverted(fill)) { |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame^] | 144 | draw_around_inv_path(target, clipBounds, pathBounds); |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | return true; |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 148 | } |