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 "GrPaint.h" |
| 11 | #include "SkPaint.h" |
| 12 | #include "GrRenderTarget.h" |
| 13 | #include "GrContext.h" |
| 14 | #include "SkDraw.h" |
| 15 | #include "SkRasterClip.h" |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 16 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 17 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 18 | bool GrSoftwarePathRenderer::canDrawPath(const SkPath& path, |
| 19 | GrPathFill fill, |
| 20 | const GrDrawTarget* target, |
| 21 | bool antiAlias) const { |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 22 | if (!antiAlias || NULL == fContext) { |
| 23 | // TODO: We could allow the SW path to also handle non-AA paths but |
| 24 | // this would mean that GrDefaultPathRenderer would never be called |
| 25 | // (since it appears after the SW renderer in the path renderer |
| 26 | // chain). Some testing would need to be done r.e. performance |
| 27 | // and consistency of the resulting images before removing |
| 28 | // the "!antiAlias" clause from the above test |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 29 | return false; |
| 30 | } |
| 31 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 32 | return true; |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 33 | } |
| 34 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 35 | namespace { |
| 36 | |
| 37 | //////////////////////////////////////////////////////////////////////////////// |
| 38 | SkPath::FillType gr_fill_to_sk_fill(GrPathFill fill) { |
| 39 | switch (fill) { |
| 40 | case kWinding_PathFill: |
| 41 | return SkPath::kWinding_FillType; |
| 42 | case kEvenOdd_PathFill: |
| 43 | return SkPath::kEvenOdd_FillType; |
| 44 | case kInverseWinding_PathFill: |
| 45 | return SkPath::kInverseWinding_FillType; |
| 46 | case kInverseEvenOdd_PathFill: |
| 47 | return SkPath::kInverseEvenOdd_FillType; |
| 48 | default: |
| 49 | GrCrash("Unexpected fill."); |
| 50 | return SkPath::kWinding_FillType; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | //////////////////////////////////////////////////////////////////////////////// |
| 55 | // gets device coord bounds of path (not considering the fill) and clip. The |
| 56 | // path bounds will be a subset of the clip bounds. returns false if |
| 57 | // path bounds would be empty. |
| 58 | bool get_path_and_clip_bounds(const GrDrawTarget* target, |
| 59 | const SkPath& path, |
| 60 | const GrVec* translate, |
| 61 | GrIRect* pathBounds, |
| 62 | GrIRect* clipBounds) { |
| 63 | // compute bounds as intersection of rt size, clip, and path |
| 64 | const GrRenderTarget* rt = target->getDrawState().getRenderTarget(); |
| 65 | if (NULL == rt) { |
| 66 | return false; |
| 67 | } |
| 68 | *pathBounds = GrIRect::MakeWH(rt->width(), rt->height()); |
| 69 | const GrClip& clip = target->getClip(); |
| 70 | if (clip.hasConservativeBounds()) { |
| 71 | clip.getConservativeBounds().roundOut(clipBounds); |
| 72 | if (!pathBounds->intersect(*clipBounds)) { |
| 73 | return false; |
| 74 | } |
| 75 | } else { |
| 76 | // pathBounds is currently the rt extent, set clip bounds to that rect. |
| 77 | *clipBounds = *pathBounds; |
| 78 | } |
| 79 | GrRect pathSBounds = path.getBounds(); |
| 80 | if (!pathSBounds.isEmpty()) { |
| 81 | if (NULL != translate) { |
| 82 | pathSBounds.offset(*translate); |
| 83 | } |
| 84 | target->getDrawState().getViewMatrix().mapRect(&pathSBounds, |
| 85 | pathSBounds); |
| 86 | GrIRect pathIBounds; |
| 87 | pathSBounds.roundOut(&pathIBounds); |
| 88 | if (!pathBounds->intersect(pathIBounds)) { |
| 89 | return false; |
| 90 | } |
| 91 | } else { |
| 92 | return false; |
| 93 | } |
| 94 | return true; |
| 95 | } |
| 96 | |
robertphillips@google.com | 6f31a3b | 2012-05-14 17:37:05 +0000 | [diff] [blame] | 97 | /** |
| 98 | * The GrSWMaskHelper helps generate clip masks using the software rendering |
| 99 | * path. |
| 100 | */ |
| 101 | class GrSWMaskHelper : public GrNoncopyable { |
| 102 | public: |
| 103 | GrSWMaskHelper(GrContext* context) |
| 104 | : fContext(context) { |
| 105 | |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Draw a single element of the clip stack into the accumulation bitmap |
| 110 | */ |
| 111 | void draw(const SkPath& clientPath, GrPathFill fill, bool antiAlias) { |
| 112 | SkPaint paint; |
| 113 | SkPath tmpPath; |
| 114 | const SkPath* pathToDraw = &clientPath; |
| 115 | if (kHairLine_PathFill == fill) { |
| 116 | paint.setStyle(SkPaint::kStroke_Style); |
| 117 | paint.setStrokeWidth(SK_Scalar1); |
| 118 | } else { |
| 119 | paint.setStyle(SkPaint::kFill_Style); |
| 120 | SkPath::FillType skfill = gr_fill_to_sk_fill(fill); |
| 121 | if (skfill != pathToDraw->getFillType()) { |
| 122 | tmpPath = *pathToDraw; |
| 123 | tmpPath.setFillType(skfill); |
| 124 | pathToDraw = &tmpPath; |
| 125 | } |
| 126 | } |
| 127 | paint.setAntiAlias(antiAlias); |
| 128 | paint.setColor(SK_ColorWHITE); |
| 129 | |
| 130 | fDraw.drawPath(*pathToDraw, paint); |
| 131 | } |
| 132 | |
| 133 | bool init(const GrIRect& pathDevBounds, const GrPoint* translate) { |
| 134 | fMatrix = fContext->getMatrix(); |
| 135 | if (NULL != translate) { |
| 136 | fMatrix.postTranslate(translate->fX, translate->fY); |
| 137 | } |
| 138 | |
| 139 | fMatrix.postTranslate(-pathDevBounds.fLeft * SK_Scalar1, |
| 140 | -pathDevBounds.fTop * SK_Scalar1); |
| 141 | GrIRect bounds = GrIRect::MakeWH(pathDevBounds.width(), |
| 142 | pathDevBounds.height()); |
| 143 | |
| 144 | fBM.setConfig(SkBitmap::kA8_Config, bounds.fRight, bounds.fBottom); |
| 145 | if (!fBM.allocPixels()) { |
| 146 | return false; |
| 147 | } |
| 148 | sk_bzero(fBM.getPixels(), fBM.getSafeSize()); |
| 149 | |
| 150 | sk_bzero(&fDraw, sizeof(fDraw)); |
| 151 | fRasterClip.setRect(bounds); |
| 152 | fDraw.fRC = &fRasterClip; |
| 153 | fDraw.fClip = &fRasterClip.bwRgn(); |
| 154 | fDraw.fMatrix = &fMatrix; |
| 155 | fDraw.fBitmap = &fBM; |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Move the result of the software mask generation back to the gpu |
| 161 | */ |
| 162 | bool toTexture(GrAutoScratchTexture* tex) { |
| 163 | const GrTextureDesc desc = { |
| 164 | kNone_GrTextureFlags, |
| 165 | fBM.width(), |
| 166 | fBM.height(), |
| 167 | kAlpha_8_GrPixelConfig, |
| 168 | 0 // samples |
| 169 | }; |
| 170 | |
| 171 | tex->set(fContext, desc); |
| 172 | GrTexture* texture = tex->texture(); |
| 173 | |
| 174 | if (NULL == texture) { |
| 175 | return false; |
| 176 | } |
| 177 | SkAutoLockPixels alp(fBM); |
| 178 | texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 179 | fBM.getPixels(), fBM.rowBytes()); |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | protected: |
| 184 | private: |
| 185 | GrContext* fContext; |
| 186 | GrMatrix fMatrix; |
| 187 | SkBitmap fBM; |
| 188 | SkDraw fDraw; |
| 189 | SkRasterClip fRasterClip; |
| 190 | |
| 191 | typedef GrPathRenderer INHERITED; |
| 192 | }; |
| 193 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 194 | //////////////////////////////////////////////////////////////////////////////// |
| 195 | /** |
| 196 | * sw rasterizes path to A8 mask using the context's matrix and uploads to a |
| 197 | * scratch texture. |
| 198 | */ |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 199 | bool sw_draw_path_to_mask_texture(const SkPath& clientPath, |
| 200 | const GrIRect& pathDevBounds, |
| 201 | GrPathFill fill, |
| 202 | GrContext* context, |
| 203 | const GrPoint* translate, |
| 204 | GrAutoScratchTexture* tex, |
| 205 | bool antiAlias) { |
robertphillips@google.com | 6f31a3b | 2012-05-14 17:37:05 +0000 | [diff] [blame] | 206 | GrSWMaskHelper helper(context); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 207 | |
robertphillips@google.com | 6f31a3b | 2012-05-14 17:37:05 +0000 | [diff] [blame] | 208 | if (!helper.init(pathDevBounds, translate)) { |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 209 | return false; |
| 210 | } |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 211 | |
robertphillips@google.com | 6f31a3b | 2012-05-14 17:37:05 +0000 | [diff] [blame] | 212 | helper.draw(clientPath, fill, antiAlias); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 213 | |
robertphillips@google.com | 6f31a3b | 2012-05-14 17:37:05 +0000 | [diff] [blame] | 214 | if (!helper.toTexture(tex)) { |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 215 | return false; |
| 216 | } |
robertphillips@google.com | 6f31a3b | 2012-05-14 17:37:05 +0000 | [diff] [blame] | 217 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 218 | return true; |
| 219 | } |
| 220 | |
| 221 | //////////////////////////////////////////////////////////////////////////////// |
| 222 | void draw_around_inv_path(GrDrawTarget* target, |
| 223 | GrDrawState::StageMask stageMask, |
| 224 | const GrIRect& clipBounds, |
| 225 | const GrIRect& pathBounds) { |
| 226 | GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask); |
| 227 | GrRect rect; |
| 228 | if (clipBounds.fTop < pathBounds.fTop) { |
| 229 | rect.iset(clipBounds.fLeft, clipBounds.fTop, |
| 230 | clipBounds.fRight, pathBounds.fTop); |
| 231 | target->drawSimpleRect(rect, NULL, stageMask); |
| 232 | } |
| 233 | if (clipBounds.fLeft < pathBounds.fLeft) { |
| 234 | rect.iset(clipBounds.fLeft, pathBounds.fTop, |
| 235 | pathBounds.fLeft, pathBounds.fBottom); |
| 236 | target->drawSimpleRect(rect, NULL, stageMask); |
| 237 | } |
| 238 | if (clipBounds.fRight > pathBounds.fRight) { |
| 239 | rect.iset(pathBounds.fRight, pathBounds.fTop, |
| 240 | clipBounds.fRight, pathBounds.fBottom); |
| 241 | target->drawSimpleRect(rect, NULL, stageMask); |
| 242 | } |
| 243 | if (clipBounds.fBottom > pathBounds.fBottom) { |
| 244 | rect.iset(clipBounds.fLeft, pathBounds.fBottom, |
| 245 | clipBounds.fRight, clipBounds.fBottom); |
| 246 | target->drawSimpleRect(rect, NULL, stageMask); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | } |
| 251 | |
| 252 | //////////////////////////////////////////////////////////////////////////////// |
| 253 | // return true on success; false on failure |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 254 | bool GrSoftwarePathRenderer::onDrawPath(const SkPath& path, |
| 255 | GrPathFill fill, |
| 256 | const GrVec* translate, |
| 257 | GrDrawTarget* target, |
| 258 | GrDrawState::StageMask stageMask, |
| 259 | bool antiAlias) { |
| 260 | |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 261 | if (NULL == fContext) { |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | GrAutoScratchTexture ast; |
| 266 | GrIRect pathBounds, clipBounds; |
| 267 | if (!get_path_and_clip_bounds(target, path, translate, |
| 268 | &pathBounds, &clipBounds)) { |
| 269 | return true; // path is empty so there is nothing to do |
| 270 | } |
| 271 | if (sw_draw_path_to_mask_texture(path, pathBounds, |
| 272 | fill, fContext, |
| 273 | translate, &ast, antiAlias)) { |
| 274 | GrTexture* texture = ast.texture(); |
| 275 | GrAssert(NULL != texture); |
| 276 | GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask); |
| 277 | enum { |
robertphillips@google.com | 7ff2e37 | 2012-05-02 19:27:44 +0000 | [diff] [blame] | 278 | // the SW path renderer shares this stage with glyph |
| 279 | // rendering (kGlyphMaskStage in GrBatchedTextContext) |
| 280 | kPathMaskStage = GrPaint::kTotalStages, |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 281 | }; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 282 | GrAssert(NULL == target->drawState()->getTexture(kPathMaskStage)); |
robertphillips@google.com | ed4155d | 2012-05-01 14:30:24 +0000 | [diff] [blame] | 283 | target->drawState()->setTexture(kPathMaskStage, texture); |
| 284 | target->drawState()->sampler(kPathMaskStage)->reset(); |
| 285 | GrScalar w = GrIntToScalar(pathBounds.width()); |
| 286 | GrScalar h = GrIntToScalar(pathBounds.height()); |
| 287 | GrRect maskRect = GrRect::MakeWH(w / texture->width(), |
| 288 | h / texture->height()); |
| 289 | const GrRect* srcRects[GrDrawState::kNumStages] = {NULL}; |
| 290 | srcRects[kPathMaskStage] = &maskRect; |
| 291 | stageMask |= 1 << kPathMaskStage; |
| 292 | GrRect dstRect = GrRect::MakeLTRB( |
| 293 | SK_Scalar1* pathBounds.fLeft, |
| 294 | SK_Scalar1* pathBounds.fTop, |
| 295 | SK_Scalar1* pathBounds.fRight, |
| 296 | SK_Scalar1* pathBounds.fBottom); |
| 297 | target->drawRect(dstRect, NULL, stageMask, srcRects, NULL); |
| 298 | target->drawState()->setTexture(kPathMaskStage, NULL); |
| 299 | if (GrIsFillInverted(fill)) { |
| 300 | draw_around_inv_path(target, stageMask, |
| 301 | clipBounds, pathBounds); |
| 302 | } |
| 303 | return true; |
| 304 | } |
| 305 | |
robertphillips@google.com | f4c2c52 | 2012-04-27 12:08:47 +0000 | [diff] [blame] | 306 | return false; |
| 307 | } |
| 308 | |