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" |
krajcevski | 5c2fca0 | 2014-06-10 17:25:28 -0700 | [diff] [blame] | 9 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 10 | #include "GrDrawState.h" |
robertphillips@google.com | 9528bdb | 2013-09-18 22:33:57 +0000 | [diff] [blame] | 11 | #include "GrDrawTargetCaps.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 12 | #include "GrGpu.h" |
| 13 | |
krajcevski | 5c2fca0 | 2014-06-10 17:25:28 -0700 | [diff] [blame] | 14 | #include "SkData.h" |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 15 | #include "SkStrokeRec.h" |
krajcevski | 5c2fca0 | 2014-06-10 17:25:28 -0700 | [diff] [blame] | 16 | #include "SkTextureCompressor.h" |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 17 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 18 | // TODO: try to remove this #include |
| 19 | #include "GrContext.h" |
| 20 | |
| 21 | namespace { |
krajcevski | 5c2fca0 | 2014-06-10 17:25:28 -0700 | [diff] [blame] | 22 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 23 | /* |
| 24 | * Convert a boolean operation into a transfer mode code |
| 25 | */ |
| 26 | SkXfermode::Mode op_to_mode(SkRegion::Op op) { |
| 27 | |
| 28 | static const SkXfermode::Mode modeMap[] = { |
| 29 | SkXfermode::kDstOut_Mode, // kDifference_Op |
reed@google.com | 8d3cd7a | 2013-01-30 21:36:11 +0000 | [diff] [blame] | 30 | SkXfermode::kModulate_Mode, // kIntersect_Op |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 31 | SkXfermode::kSrcOver_Mode, // kUnion_Op |
| 32 | SkXfermode::kXor_Mode, // kXOR_Op |
| 33 | SkXfermode::kClear_Mode, // kReverseDifference_Op |
| 34 | SkXfermode::kSrc_Mode, // kReplace_Op |
| 35 | }; |
| 36 | |
| 37 | return modeMap[op]; |
| 38 | } |
| 39 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Draw a single rect element of the clip stack into the accumulation bitmap |
| 44 | */ |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 45 | void GrSWMaskHelper::draw(const SkRect& rect, SkRegion::Op op, |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 46 | bool antiAlias, uint8_t alpha) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 47 | SkPaint paint; |
| 48 | |
| 49 | SkXfermode* mode = SkXfermode::Create(op_to_mode(op)); |
| 50 | |
| 51 | paint.setXfermode(mode); |
| 52 | paint.setAntiAlias(antiAlias); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 53 | paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 54 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 55 | fDraw.drawRect(rect, paint); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 56 | |
| 57 | SkSafeUnref(mode); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Draw a single path element of the clip stack into the accumulation bitmap |
| 62 | */ |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 63 | 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] | 64 | bool antiAlias, uint8_t alpha) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 65 | |
| 66 | SkPaint paint; |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 67 | if (stroke.isHairlineStyle()) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 68 | paint.setStyle(SkPaint::kStroke_Style); |
| 69 | paint.setStrokeWidth(SK_Scalar1); |
| 70 | } else { |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 71 | if (stroke.isFillStyle()) { |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 72 | paint.setStyle(SkPaint::kFill_Style); |
| 73 | } else { |
| 74 | paint.setStyle(SkPaint::kStroke_Style); |
| 75 | paint.setStrokeJoin(stroke.getJoin()); |
| 76 | paint.setStrokeCap(stroke.getCap()); |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 77 | paint.setStrokeWidth(stroke.getWidth()); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
reed@google.com | 84e922b | 2013-11-04 20:57:36 +0000 | [diff] [blame] | 80 | paint.setAntiAlias(antiAlias); |
reed@google.com | 84e922b | 2013-11-04 20:57:36 +0000 | [diff] [blame] | 81 | |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 82 | if (SkRegion::kReplace_Op == op && 0xFF == alpha) { |
| 83 | SkASSERT(0xFF == paint.getAlpha()); |
| 84 | fDraw.drawPathCoverage(path, paint); |
| 85 | } else { |
| 86 | paint.setXfermodeMode(op_to_mode(op)); |
| 87 | paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); |
| 88 | fDraw.drawPath(path, paint); |
| 89 | } |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 90 | } |
| 91 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 92 | bool GrSWMaskHelper::init(const SkIRect& resultBounds, |
reed@google.com | 84e922b | 2013-11-04 20:57:36 +0000 | [diff] [blame] | 93 | const SkMatrix* matrix) { |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 94 | if (NULL != matrix) { |
| 95 | fMatrix = *matrix; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 96 | } else { |
| 97 | fMatrix.setIdentity(); |
| 98 | } |
| 99 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 100 | // Now translate so the bound's UL corner is at the origin |
| 101 | fMatrix.postTranslate(-resultBounds.fLeft * SK_Scalar1, |
| 102 | -resultBounds.fTop * SK_Scalar1); |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 103 | SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 104 | resultBounds.height()); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 105 | |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 106 | if (!fBM.allocPixels(SkImageInfo::MakeA8(bounds.fRight, bounds.fBottom))) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 107 | return false; |
| 108 | } |
reed@google.com | 84e922b | 2013-11-04 20:57:36 +0000 | [diff] [blame] | 109 | sk_bzero(fBM.getPixels(), fBM.getSafeSize()); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 110 | |
| 111 | sk_bzero(&fDraw, sizeof(fDraw)); |
| 112 | fRasterClip.setRect(bounds); |
| 113 | fDraw.fRC = &fRasterClip; |
| 114 | fDraw.fClip = &fRasterClip.bwRgn(); |
| 115 | fDraw.fMatrix = &fMatrix; |
| 116 | fDraw.fBitmap = &fBM; |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | /** |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 121 | * Get a texture (from the texture cache) of the correct size & format. |
| 122 | * Return true on success; false on failure. |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 123 | */ |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 124 | bool GrSWMaskHelper::getTexture(GrAutoScratchTexture* texture) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 125 | GrTextureDesc desc; |
| 126 | desc.fWidth = fBM.width(); |
| 127 | desc.fHeight = fBM.height(); |
krajcevski | fb4f5cb | 2014-06-12 09:20:38 -0700 | [diff] [blame^] | 128 | |
| 129 | #if GR_COMPRESS_ALPHA_MASK |
| 130 | static const int kLATCBlockSize = 4; |
| 131 | if (desc.fWidth % kLATCBlockSize == 0 && desc.fHeight % kLATCBlockSize == 0) { |
| 132 | desc.fConfig = kLATC_GrPixelConfig; |
| 133 | } else { |
| 134 | #endif |
| 135 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 136 | #if GR_COMPRESS_ALPHA_MASK |
| 137 | } |
| 138 | #endif |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 139 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 140 | texture->set(fContext, desc); |
| 141 | return NULL != texture->texture(); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 142 | } |
| 143 | |
krajcevski | fb4f5cb | 2014-06-12 09:20:38 -0700 | [diff] [blame^] | 144 | void GrSWMaskHelper::sendTextureData(GrTexture *texture, const GrTextureDesc& desc, |
| 145 | const void *data, int rowbytes) { |
| 146 | // If we aren't reusing scratch textures we don't need to flush before |
| 147 | // writing since no one else will be using 'texture' |
| 148 | bool reuseScratch = fContext->getGpu()->caps()->reuseScratchTextures(); |
krajcevski | 5c2fca0 | 2014-06-10 17:25:28 -0700 | [diff] [blame] | 149 | |
krajcevski | fb4f5cb | 2014-06-12 09:20:38 -0700 | [diff] [blame^] | 150 | // Since we're uploading to it, and it's compressed, 'texture' shouldn't |
| 151 | // have a render target. |
| 152 | SkASSERT(NULL == texture->asRenderTarget()); |
| 153 | |
| 154 | texture->writePixels(0, 0, desc.fWidth, desc.fHeight, |
| 155 | desc.fConfig, data, rowbytes, |
| 156 | reuseScratch ? 0 : GrContext::kDontFlush_PixelOpsFlag); |
krajcevski | 5c2fca0 | 2014-06-10 17:25:28 -0700 | [diff] [blame] | 157 | } |
| 158 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 159 | /** |
| 160 | * Move the result of the software mask generation back to the gpu |
| 161 | */ |
robertphillips@google.com | d92cf2e | 2013-07-19 18:13:02 +0000 | [diff] [blame] | 162 | void GrSWMaskHelper::toTexture(GrTexture *texture) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 163 | SkAutoLockPixels alp(fBM); |
| 164 | |
krajcevski | fb4f5cb | 2014-06-12 09:20:38 -0700 | [diff] [blame^] | 165 | GrTextureDesc desc; |
| 166 | desc.fWidth = fBM.width(); |
| 167 | desc.fHeight = fBM.height(); |
| 168 | desc.fConfig = texture->config(); |
| 169 | |
| 170 | // First see if we should compress this texture before uploading. |
| 171 | if (texture->config() == kLATC_GrPixelConfig) { |
| 172 | SkTextureCompressor::Format format = SkTextureCompressor::kLATC_Format; |
| 173 | SkAutoDataUnref latcData(SkTextureCompressor::CompressBitmapToFormat(fBM, format)); |
| 174 | SkASSERT(NULL != latcData); |
robertphillips@google.com | 9528bdb | 2013-09-18 22:33:57 +0000 | [diff] [blame] | 175 | |
krajcevski | fb4f5cb | 2014-06-12 09:20:38 -0700 | [diff] [blame^] | 176 | this->sendTextureData(texture, desc, latcData->data(), 0); |
| 177 | } else { |
| 178 | // Looks like we have to send a full A8 texture. |
| 179 | this->sendTextureData(texture, desc, fBM.getPixels(), fBM.rowBytes()); |
| 180 | } |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 181 | } |
| 182 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 183 | //////////////////////////////////////////////////////////////////////////////// |
| 184 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 185 | * 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] | 186 | * and uploads the result to a scratch texture. Returns the resulting |
| 187 | * texture on success; NULL on failure. |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 188 | */ |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 189 | GrTexture* GrSWMaskHelper::DrawPathMaskToTexture(GrContext* context, |
| 190 | const SkPath& path, |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 191 | const SkStrokeRec& stroke, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 192 | const SkIRect& resultBounds, |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 193 | bool antiAlias, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 194 | SkMatrix* matrix) { |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 195 | GrSWMaskHelper helper(context); |
| 196 | |
reed@google.com | 84e922b | 2013-11-04 20:57:36 +0000 | [diff] [blame] | 197 | if (!helper.init(resultBounds, matrix)) { |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 198 | return NULL; |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 199 | } |
| 200 | |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 201 | helper.draw(path, stroke, SkRegion::kReplace_Op, antiAlias, 0xFF); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 202 | |
krajcevski | 5c2fca0 | 2014-06-10 17:25:28 -0700 | [diff] [blame] | 203 | GrAutoScratchTexture ast; |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 204 | if (!helper.getTexture(&ast)) { |
| 205 | return NULL; |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 206 | } |
| 207 | |
robertphillips@google.com | d92cf2e | 2013-07-19 18:13:02 +0000 | [diff] [blame] | 208 | helper.toTexture(ast.texture()); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 209 | |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 210 | return ast.detach(); |
| 211 | } |
| 212 | |
| 213 | void GrSWMaskHelper::DrawToTargetWithPathMask(GrTexture* texture, |
| 214 | GrDrawTarget* target, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 215 | const SkIRect& rect) { |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 216 | GrDrawState* drawState = target->drawState(); |
| 217 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 218 | GrDrawState::AutoViewMatrixRestore avmr; |
| 219 | if (!avmr.setIdentity(drawState)) { |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 220 | return; |
| 221 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 222 | GrDrawState::AutoRestoreEffects are(drawState); |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 223 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 224 | SkRect dstRect = SkRect::MakeLTRB(SK_Scalar1 * rect.fLeft, |
| 225 | SK_Scalar1 * rect.fTop, |
| 226 | SK_Scalar1 * rect.fRight, |
| 227 | SK_Scalar1 * rect.fBottom); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 228 | |
| 229 | // We want to use device coords to compute the texture coordinates. We set our matrix to be |
| 230 | // equal to the view matrix followed by a translation so that the top-left of the device bounds |
| 231 | // maps to 0,0, and then a scaling matrix to normalized coords. We apply this matrix to the |
| 232 | // vertex positions rather than local coords. |
| 233 | SkMatrix maskMatrix; |
| 234 | maskMatrix.setIDiv(texture->width(), texture->height()); |
| 235 | maskMatrix.preTranslate(SkIntToScalar(-rect.fLeft), SkIntToScalar(-rect.fTop)); |
| 236 | maskMatrix.preConcat(drawState->getViewMatrix()); |
| 237 | |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 238 | drawState->addCoverageEffect( |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 239 | GrSimpleTextureEffect::Create(texture, |
| 240 | maskMatrix, |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 241 | GrTextureParams::kNone_FilterMode, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 242 | kPosition_GrCoordSet))->unref(); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 243 | |
| 244 | target->drawSimpleRect(dstRect); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 245 | } |