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, |
| 39 | const GrVec* translate, |
| 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(); |
| 49 | if (clip.hasConservativeBounds()) { |
| 50 | clip.getConservativeBounds().roundOut(clipBounds); |
| 51 | if (!pathBounds->intersect(*clipBounds)) { |
| 52 | return false; |
| 53 | } |
| 54 | } else { |
| 55 | // pathBounds is currently the rt extent, set clip bounds to that rect. |
| 56 | *clipBounds = *pathBounds; |
| 57 | } |
| 58 | GrRect pathSBounds = path.getBounds(); |
| 59 | if (!pathSBounds.isEmpty()) { |
| 60 | if (NULL != translate) { |
| 61 | pathSBounds.offset(*translate); |
| 62 | } |
| 63 | target->getDrawState().getViewMatrix().mapRect(&pathSBounds, |
| 64 | pathSBounds); |
| 65 | GrIRect pathIBounds; |
| 66 | pathSBounds.roundOut(&pathIBounds); |
| 67 | if (!pathBounds->intersect(pathIBounds)) { |
bsalomon@google.com | 276c1fa | 2012-06-19 13:22:45 +0000 | [diff] [blame] | 68 | // set the correct path bounds, as this would be used later. |
| 69 | *pathBounds = pathIBounds; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 70 | return false; |
| 71 | } |
| 72 | } else { |
bsalomon@google.com | 276c1fa | 2012-06-19 13:22:45 +0000 | [diff] [blame] | 73 | *pathBounds = GrIRect::EmptyIRect(); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 74 | return false; |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | //////////////////////////////////////////////////////////////////////////////// |
| 80 | /** |
| 81 | * sw rasterizes path to A8 mask using the context's matrix and uploads to a |
| 82 | * scratch texture. |
| 83 | */ |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 84 | bool sw_draw_path_to_mask_texture(const SkPath& clientPath, |
| 85 | const GrIRect& pathDevBounds, |
| 86 | GrPathFill fill, |
| 87 | GrContext* context, |
| 88 | const GrPoint* translate, |
| 89 | GrAutoScratchTexture* tex, |
| 90 | bool antiAlias) { |
robertphillips@google.com | 6f31a3b | 2012-05-14 17:37:05 +0000 | [diff] [blame] | 91 | GrSWMaskHelper helper(context); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 92 | |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 93 | if (!helper.init(pathDevBounds, translate, true)) { |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 94 | return false; |
| 95 | } |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 96 | |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 97 | helper.draw(clientPath, SkRegion::kReplace_Op, |
| 98 | fill, antiAlias, SK_ColorWHITE); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 99 | |
robertphillips@google.com | b4f06d7 | 2012-05-15 12:10:05 +0000 | [diff] [blame] | 100 | if (!helper.getTexture(tex)) { |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 101 | return false; |
| 102 | } |
robertphillips@google.com | 6f31a3b | 2012-05-14 17:37:05 +0000 | [diff] [blame] | 103 | |
robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 104 | helper.toTexture(tex->texture(), false); |
robertphillips@google.com | b4f06d7 | 2012-05-15 12:10:05 +0000 | [diff] [blame] | 105 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 106 | return true; |
| 107 | } |
| 108 | |
| 109 | //////////////////////////////////////////////////////////////////////////////// |
| 110 | void draw_around_inv_path(GrDrawTarget* target, |
| 111 | GrDrawState::StageMask stageMask, |
| 112 | const GrIRect& clipBounds, |
| 113 | const GrIRect& pathBounds) { |
| 114 | GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask); |
| 115 | GrRect rect; |
| 116 | if (clipBounds.fTop < pathBounds.fTop) { |
| 117 | rect.iset(clipBounds.fLeft, clipBounds.fTop, |
| 118 | clipBounds.fRight, pathBounds.fTop); |
| 119 | target->drawSimpleRect(rect, NULL, stageMask); |
| 120 | } |
| 121 | if (clipBounds.fLeft < pathBounds.fLeft) { |
| 122 | rect.iset(clipBounds.fLeft, pathBounds.fTop, |
| 123 | pathBounds.fLeft, pathBounds.fBottom); |
| 124 | target->drawSimpleRect(rect, NULL, stageMask); |
| 125 | } |
| 126 | if (clipBounds.fRight > pathBounds.fRight) { |
| 127 | rect.iset(pathBounds.fRight, pathBounds.fTop, |
| 128 | clipBounds.fRight, pathBounds.fBottom); |
| 129 | target->drawSimpleRect(rect, NULL, stageMask); |
| 130 | } |
| 131 | if (clipBounds.fBottom > pathBounds.fBottom) { |
| 132 | rect.iset(clipBounds.fLeft, pathBounds.fBottom, |
| 133 | clipBounds.fRight, clipBounds.fBottom); |
| 134 | target->drawSimpleRect(rect, NULL, stageMask); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | } |
| 139 | |
| 140 | //////////////////////////////////////////////////////////////////////////////// |
| 141 | // return true on success; false on failure |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 142 | bool GrSoftwarePathRenderer::onDrawPath(const SkPath& path, |
| 143 | GrPathFill fill, |
| 144 | const GrVec* translate, |
| 145 | GrDrawTarget* target, |
| 146 | GrDrawState::StageMask stageMask, |
| 147 | bool antiAlias) { |
| 148 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 149 | if (NULL == fContext) { |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | GrAutoScratchTexture ast; |
| 154 | GrIRect pathBounds, clipBounds; |
| 155 | if (!get_path_and_clip_bounds(target, path, translate, |
| 156 | &pathBounds, &clipBounds)) { |
bsalomon@google.com | 276c1fa | 2012-06-19 13:22:45 +0000 | [diff] [blame] | 157 | if (GrIsFillInverted(fill)) { |
| 158 | draw_around_inv_path(target, stageMask, |
| 159 | clipBounds, pathBounds); |
| 160 | } |
| 161 | return true; |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 162 | } |
| 163 | if (sw_draw_path_to_mask_texture(path, pathBounds, |
| 164 | fill, fContext, |
| 165 | translate, &ast, antiAlias)) { |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 166 | SkAutoTUnref<GrTexture> texture(ast.detach()); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 167 | GrAssert(NULL != texture); |
| 168 | GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask); |
| 169 | enum { |
robertphillips@google.com | 7ff2e37 | 2012-05-02 19:27:44 +0000 | [diff] [blame] | 170 | // the SW path renderer shares this stage with glyph |
| 171 | // rendering (kGlyphMaskStage in GrBatchedTextContext) |
| 172 | kPathMaskStage = GrPaint::kTotalStages, |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 173 | }; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 174 | GrAssert(NULL == target->drawState()->getTexture(kPathMaskStage)); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 175 | target->drawState()->setTexture(kPathMaskStage, texture); |
| 176 | target->drawState()->sampler(kPathMaskStage)->reset(); |
| 177 | GrScalar w = GrIntToScalar(pathBounds.width()); |
| 178 | GrScalar h = GrIntToScalar(pathBounds.height()); |
| 179 | GrRect maskRect = GrRect::MakeWH(w / texture->width(), |
| 180 | h / texture->height()); |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 181 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 182 | const GrRect* srcRects[GrDrawState::kNumStages] = {NULL}; |
| 183 | srcRects[kPathMaskStage] = &maskRect; |
| 184 | stageMask |= 1 << kPathMaskStage; |
| 185 | GrRect dstRect = GrRect::MakeLTRB( |
| 186 | SK_Scalar1* pathBounds.fLeft, |
| 187 | SK_Scalar1* pathBounds.fTop, |
| 188 | SK_Scalar1* pathBounds.fRight, |
| 189 | SK_Scalar1* pathBounds.fBottom); |
| 190 | target->drawRect(dstRect, NULL, stageMask, srcRects, NULL); |
| 191 | target->drawState()->setTexture(kPathMaskStage, NULL); |
| 192 | if (GrIsFillInverted(fill)) { |
| 193 | draw_around_inv_path(target, stageMask, |
| 194 | clipBounds, pathBounds); |
| 195 | } |
| 196 | return true; |
| 197 | } |
| 198 | |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 199 | return false; |
| 200 | } |