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