robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrSWMaskHelper.h" |
| 9 | #include "GrDrawState.h" |
| 10 | #include "GrGpu.h" |
| 11 | |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 12 | #include "SkStrokeRec.h" |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 13 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 14 | // TODO: try to remove this #include |
| 15 | #include "GrContext.h" |
| 16 | |
| 17 | namespace { |
| 18 | /* |
| 19 | * Convert a boolean operation into a transfer mode code |
| 20 | */ |
| 21 | SkXfermode::Mode op_to_mode(SkRegion::Op op) { |
| 22 | |
| 23 | static const SkXfermode::Mode modeMap[] = { |
| 24 | SkXfermode::kDstOut_Mode, // kDifference_Op |
reed@google.com | 8d3cd7a | 2013-01-30 21:36:11 +0000 | [diff] [blame] | 25 | SkXfermode::kModulate_Mode, // kIntersect_Op |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 26 | SkXfermode::kSrcOver_Mode, // kUnion_Op |
| 27 | SkXfermode::kXor_Mode, // kXOR_Op |
| 28 | SkXfermode::kClear_Mode, // kReverseDifference_Op |
| 29 | SkXfermode::kSrc_Mode, // kReplace_Op |
| 30 | }; |
| 31 | |
| 32 | return modeMap[op]; |
| 33 | } |
| 34 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Draw a single rect element of the clip stack into the accumulation bitmap |
| 39 | */ |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 40 | void GrSWMaskHelper::draw(const GrRect& rect, SkRegion::Op op, |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 41 | bool antiAlias, uint8_t alpha) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 42 | SkPaint paint; |
| 43 | |
| 44 | SkXfermode* mode = SkXfermode::Create(op_to_mode(op)); |
| 45 | |
| 46 | paint.setXfermode(mode); |
| 47 | paint.setAntiAlias(antiAlias); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 48 | paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 49 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 50 | fDraw.drawRect(rect, paint); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 51 | |
| 52 | SkSafeUnref(mode); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Draw a single path element of the clip stack into the accumulation bitmap |
| 57 | */ |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 58 | void GrSWMaskHelper::draw(const SkPath& path, const SkStrokeRec& stroke, SkRegion::Op op, |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 59 | bool antiAlias, uint8_t alpha) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 60 | |
| 61 | SkPaint paint; |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 62 | if (stroke.isHairlineStyle()) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 63 | paint.setStyle(SkPaint::kStroke_Style); |
| 64 | paint.setStrokeWidth(SK_Scalar1); |
| 65 | } else { |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 66 | if (stroke.isFillStyle()) { |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 67 | paint.setStyle(SkPaint::kFill_Style); |
| 68 | } else { |
| 69 | paint.setStyle(SkPaint::kStroke_Style); |
| 70 | paint.setStrokeJoin(stroke.getJoin()); |
| 71 | paint.setStrokeCap(stroke.getCap()); |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 72 | paint.setStrokeWidth(stroke.getWidth()); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 75 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 76 | SkXfermode* mode = SkXfermode::Create(op_to_mode(op)); |
| 77 | |
| 78 | paint.setXfermode(mode); |
| 79 | paint.setAntiAlias(antiAlias); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 80 | paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 81 | |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 82 | fDraw.drawPath(path, paint); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 83 | |
| 84 | SkSafeUnref(mode); |
| 85 | } |
| 86 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 87 | bool GrSWMaskHelper::init(const GrIRect& resultBounds, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 88 | const SkMatrix* matrix) { |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 89 | if (NULL != matrix) { |
| 90 | fMatrix = *matrix; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 91 | } else { |
| 92 | fMatrix.setIdentity(); |
| 93 | } |
| 94 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 95 | // Now translate so the bound's UL corner is at the origin |
| 96 | fMatrix.postTranslate(-resultBounds.fLeft * SK_Scalar1, |
| 97 | -resultBounds.fTop * SK_Scalar1); |
| 98 | GrIRect bounds = GrIRect::MakeWH(resultBounds.width(), |
| 99 | resultBounds.height()); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 100 | |
| 101 | fBM.setConfig(SkBitmap::kA8_Config, bounds.fRight, bounds.fBottom); |
| 102 | if (!fBM.allocPixels()) { |
| 103 | return false; |
| 104 | } |
| 105 | sk_bzero(fBM.getPixels(), fBM.getSafeSize()); |
| 106 | |
| 107 | sk_bzero(&fDraw, sizeof(fDraw)); |
| 108 | fRasterClip.setRect(bounds); |
| 109 | fDraw.fRC = &fRasterClip; |
| 110 | fDraw.fClip = &fRasterClip.bwRgn(); |
| 111 | fDraw.fMatrix = &fMatrix; |
| 112 | fDraw.fBitmap = &fBM; |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | /** |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 117 | * Get a texture (from the texture cache) of the correct size & format. |
| 118 | * Return true on success; false on failure. |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 119 | */ |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 120 | bool GrSWMaskHelper::getTexture(GrAutoScratchTexture* texture) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 121 | GrTextureDesc desc; |
| 122 | desc.fWidth = fBM.width(); |
| 123 | desc.fHeight = fBM.height(); |
| 124 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 125 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 126 | texture->set(fContext, desc); |
| 127 | return NULL != texture->texture(); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Move the result of the software mask generation back to the gpu |
| 132 | */ |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 133 | void GrSWMaskHelper::toTexture(GrTexture *texture, uint8_t alpha) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 134 | SkAutoLockPixels alp(fBM); |
| 135 | |
| 136 | // The destination texture is almost always larger than "fBM". Clear |
| 137 | // it appropriately so we don't get mask artifacts outside of the path's |
| 138 | // bounding box |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 139 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 140 | // "texture" needs to be installed as the render target for the clear |
| 141 | // and the texture upload but cannot remain the render target upon |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 142 | // return. Callers typically use it as a texture and it would then |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 143 | // be both source and dest. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 144 | GrDrawState::AutoRenderTargetRestore artr(fContext->getGpu()->drawState(), |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 145 | texture->asRenderTarget()); |
| 146 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 147 | fContext->getGpu()->clear(NULL, GrColorPackRGBA(alpha, alpha, alpha, alpha)); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 148 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 149 | texture->writePixels(0, 0, fBM.width(), fBM.height(), |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 150 | kAlpha_8_GrPixelConfig, |
| 151 | fBM.getPixels(), fBM.rowBytes()); |
| 152 | } |
| 153 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 154 | //////////////////////////////////////////////////////////////////////////////// |
| 155 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 156 | * Software rasterizes path to A8 mask (possibly using the context's matrix) |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 157 | * and uploads the result to a scratch texture. Returns the resulting |
| 158 | * texture on success; NULL on failure. |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 159 | */ |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 160 | GrTexture* GrSWMaskHelper::DrawPathMaskToTexture(GrContext* context, |
| 161 | const SkPath& path, |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 162 | const SkStrokeRec& stroke, |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 163 | const GrIRect& resultBounds, |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 164 | bool antiAlias, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 165 | SkMatrix* matrix) { |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 166 | GrAutoScratchTexture ast; |
| 167 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 168 | GrSWMaskHelper helper(context); |
| 169 | |
| 170 | if (!helper.init(resultBounds, matrix)) { |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 171 | return NULL; |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 172 | } |
| 173 | |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 174 | helper.draw(path, stroke, SkRegion::kReplace_Op, antiAlias, 0xFF); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 175 | |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 176 | if (!helper.getTexture(&ast)) { |
| 177 | return NULL; |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 178 | } |
| 179 | |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 180 | helper.toTexture(ast.texture(), 0x00); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 181 | |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 182 | return ast.detach(); |
| 183 | } |
| 184 | |
| 185 | void GrSWMaskHelper::DrawToTargetWithPathMask(GrTexture* texture, |
| 186 | GrDrawTarget* target, |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 187 | const GrIRect& rect) { |
| 188 | GrDrawState* drawState = target->drawState(); |
| 189 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame^] | 190 | GrDrawState::AutoViewMatrixRestore avmr; |
| 191 | if (!avmr.setIdentity(drawState)) { |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 192 | return; |
| 193 | } |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 194 | enum { |
| 195 | // the SW path renderer shares this stage with glyph |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 196 | // rendering (kGlyphMaskStage in GrTextContext) |
| 197 | // && edge rendering (kEdgeEffectStage in GrContext) |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 198 | kPathMaskStage = GrPaint::kTotalStages, |
| 199 | }; |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 200 | |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 201 | GrRect dstRect = GrRect::MakeLTRB( |
| 202 | SK_Scalar1 * rect.fLeft, |
| 203 | SK_Scalar1 * rect.fTop, |
| 204 | SK_Scalar1 * rect.fRight, |
| 205 | SK_Scalar1 * rect.fBottom); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 206 | |
| 207 | // We want to use device coords to compute the texture coordinates. We set our matrix to be |
| 208 | // equal to the view matrix followed by a translation so that the top-left of the device bounds |
| 209 | // maps to 0,0, and then a scaling matrix to normalized coords. We apply this matrix to the |
| 210 | // vertex positions rather than local coords. |
| 211 | SkMatrix maskMatrix; |
| 212 | maskMatrix.setIDiv(texture->width(), texture->height()); |
| 213 | maskMatrix.preTranslate(SkIntToScalar(-rect.fLeft), SkIntToScalar(-rect.fTop)); |
| 214 | maskMatrix.preConcat(drawState->getViewMatrix()); |
| 215 | |
| 216 | GrAssert(!drawState->isStageEnabled(kPathMaskStage)); |
| 217 | drawState->setEffect(kPathMaskStage, |
| 218 | GrSimpleTextureEffect::Create(texture, |
| 219 | maskMatrix, |
| 220 | false, |
| 221 | GrEffect::kPosition_CoordsType))->unref(); |
| 222 | |
| 223 | target->drawSimpleRect(dstRect); |
tomhudson@google.com | 676e660 | 2012-07-10 17:21:48 +0000 | [diff] [blame] | 224 | drawState->disableStage(kPathMaskStage); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 225 | } |