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 | |
bsalomon | eb1cb5c | 2015-05-22 08:01:09 -0700 | [diff] [blame] | 10 | #include "GrCaps.h" |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 11 | #include "GrContext.h" |
| 12 | #include "batches/GrDrawBatch.h" |
| 13 | #include "GrDrawContext.h" |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 14 | #include "GrPipelineBuilder.h" |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 15 | #include "GrShape.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 16 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 17 | #include "SkDistanceFieldGen.h" |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 18 | |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 19 | #include "batches/GrRectBatchFactory.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 20 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 21 | /* |
| 22 | * Convert a boolean operation into a transfer mode code |
| 23 | */ |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 24 | static SkXfermode::Mode op_to_mode(SkRegion::Op op) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 25 | |
| 26 | static const SkXfermode::Mode modeMap[] = { |
| 27 | SkXfermode::kDstOut_Mode, // kDifference_Op |
reed@google.com | 8d3cd7a | 2013-01-30 21:36:11 +0000 | [diff] [blame] | 28 | SkXfermode::kModulate_Mode, // kIntersect_Op |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 29 | SkXfermode::kSrcOver_Mode, // kUnion_Op |
| 30 | SkXfermode::kXor_Mode, // kXOR_Op |
| 31 | SkXfermode::kClear_Mode, // kReverseDifference_Op |
| 32 | SkXfermode::kSrc_Mode, // kReplace_Op |
| 33 | }; |
| 34 | |
| 35 | return modeMap[op]; |
| 36 | } |
| 37 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 38 | /** |
| 39 | * Draw a single rect element of the clip stack into the accumulation bitmap |
| 40 | */ |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 41 | void GrSWMaskHelper::drawRect(const SkRect& rect, SkRegion::Op op, |
| 42 | bool antiAlias, uint8_t alpha) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 43 | SkPaint paint; |
| 44 | |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 45 | paint.setXfermode(SkXfermode::Make(op_to_mode(op))); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 46 | paint.setAntiAlias(antiAlias); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 47 | paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 48 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 49 | fDraw.drawRect(rect, paint); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Draw a single path element of the clip stack into the accumulation bitmap |
| 54 | */ |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 55 | void GrSWMaskHelper::drawShape(const GrShape& shape, SkRegion::Op op, bool antiAlias, |
| 56 | uint8_t alpha) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 57 | SkPaint paint; |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 58 | paint.setPathEffect(sk_ref_sp(shape.style().pathEffect())); |
| 59 | shape.style().strokeRec().applyToPaint(&paint); |
reed@google.com | 84e922b | 2013-11-04 20:57:36 +0000 | [diff] [blame] | 60 | paint.setAntiAlias(antiAlias); |
reed@google.com | 84e922b | 2013-11-04 20:57:36 +0000 | [diff] [blame] | 61 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 62 | SkPath path; |
| 63 | shape.asPath(&path); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 64 | if (SkRegion::kReplace_Op == op && 0xFF == alpha) { |
| 65 | SkASSERT(0xFF == paint.getAlpha()); |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 66 | fDraw.drawPathCoverage(path, paint); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 67 | } else { |
| 68 | paint.setXfermodeMode(op_to_mode(op)); |
| 69 | paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 70 | fDraw.drawPath(path, paint); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 71 | } |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 72 | } |
| 73 | |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 74 | bool GrSWMaskHelper::init(const SkIRect& resultBounds, const SkMatrix* matrix) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 75 | if (matrix) { |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 76 | fMatrix = *matrix; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 77 | } else { |
| 78 | fMatrix.setIdentity(); |
| 79 | } |
| 80 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 81 | // Now translate so the bound's UL corner is at the origin |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 82 | fMatrix.postTranslate(-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop)); |
| 83 | SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height()); |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 84 | |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 85 | const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height()); |
| 86 | if (!fPixels.tryAlloc(bmImageInfo)) { |
| 87 | return false; |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 88 | } |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 89 | fPixels.erase(0); |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 90 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 91 | sk_bzero(&fDraw, sizeof(fDraw)); |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 92 | fDraw.fDst = fPixels; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 93 | fRasterClip.setRect(bounds); |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 94 | fDraw.fRC = &fRasterClip; |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 95 | fDraw.fMatrix = &fMatrix; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 96 | return true; |
| 97 | } |
| 98 | |
| 99 | /** |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 100 | * Get a texture (from the texture cache) of the correct size & format. |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 101 | */ |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 102 | GrTexture* GrSWMaskHelper::createTexture() { |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 103 | GrSurfaceDesc desc; |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 104 | desc.fWidth = fPixels.width(); |
| 105 | desc.fHeight = fPixels.height(); |
krajcevski | b3abe90 | 2014-07-30 13:08:11 -0700 | [diff] [blame] | 106 | desc.fConfig = kAlpha_8_GrPixelConfig; |
krajcevski | fb4f5cb | 2014-06-12 09:20:38 -0700 | [diff] [blame] | 107 | |
robertphillips | 0152d73 | 2016-05-20 06:38:43 -0700 | [diff] [blame] | 108 | return fTexProvider->createApproxTexture(desc); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Move the result of the software mask generation back to the gpu |
| 113 | */ |
robertphillips@google.com | d92cf2e | 2013-07-19 18:13:02 +0000 | [diff] [blame] | 114 | void GrSWMaskHelper::toTexture(GrTexture *texture) { |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 115 | // Since we're uploading to it, and it's compressed, 'texture' shouldn't |
| 116 | // have a render target. |
| 117 | SkASSERT(!texture->asRenderTarget()); |
cblume | ed82800 | 2016-02-16 13:00:01 -0800 | [diff] [blame] | 118 | |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 119 | texture->writePixels(0, 0, fPixels.width(), fPixels.height(), texture->config(), |
| 120 | fPixels.addr(), fPixels.rowBytes()); |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 121 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 122 | } |
| 123 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 124 | /** |
| 125 | * Convert mask generation results to a signed distance field |
| 126 | */ |
| 127 | void GrSWMaskHelper::toSDF(unsigned char* sdf) { |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 128 | SkGenerateDistanceFieldFromA8Image(sdf, (const unsigned char*)fPixels.addr(), |
| 129 | fPixels.width(), fPixels.height(), fPixels.rowBytes()); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 130 | } |
| 131 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 132 | //////////////////////////////////////////////////////////////////////////////// |
| 133 | /** |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 134 | * Software rasterizes shape to A8 mask and uploads the result to a scratch texture. Returns the |
| 135 | * resulting texture on success; nullptr on failure. |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 136 | */ |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 137 | GrTexture* GrSWMaskHelper::DrawShapeMaskToTexture(GrTextureProvider* texProvider, |
| 138 | const GrShape& shape, |
| 139 | const SkIRect& resultBounds, |
| 140 | bool antiAlias, |
| 141 | const SkMatrix* matrix) { |
robertphillips | 0152d73 | 2016-05-20 06:38:43 -0700 | [diff] [blame] | 142 | GrSWMaskHelper helper(texProvider); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 143 | |
reed@google.com | 84e922b | 2013-11-04 20:57:36 +0000 | [diff] [blame] | 144 | if (!helper.init(resultBounds, matrix)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 145 | return nullptr; |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 146 | } |
| 147 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 148 | helper.drawShape(shape, SkRegion::kReplace_Op, antiAlias, 0xFF); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 149 | |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 150 | GrTexture* texture(helper.createTexture()); |
| 151 | if (!texture) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 152 | return nullptr; |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 153 | } |
| 154 | |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 155 | helper.toTexture(texture); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 156 | |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 157 | return texture; |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 158 | } |
| 159 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 160 | void GrSWMaskHelper::DrawToTargetWithShapeMask(GrTexture* texture, |
| 161 | GrDrawContext* drawContext, |
robertphillips | 3950f0d | 2016-07-07 07:33:13 -0700 | [diff] [blame] | 162 | const GrPaint& paint, |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 163 | const GrUserStencilSettings* userStencilSettings, |
| 164 | const GrClip& clip, |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 165 | const SkMatrix& viewMatrix, |
| 166 | const SkIRect& rect) { |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 167 | SkMatrix invert; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 168 | if (!viewMatrix.invert(&invert)) { |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 169 | return; |
| 170 | } |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 171 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 172 | SkRect dstRect = SkRect::MakeLTRB(SK_Scalar1 * rect.fLeft, |
| 173 | SK_Scalar1 * rect.fTop, |
| 174 | SK_Scalar1 * rect.fRight, |
| 175 | SK_Scalar1 * rect.fBottom); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 176 | |
bsalomon | 309d4d5 | 2014-12-18 10:17:44 -0800 | [diff] [blame] | 177 | // We use device coords to compute the texture coordinates. We take the device coords and apply |
| 178 | // a translation so that the top-left of the device bounds maps to 0,0, and then a scaling |
| 179 | // matrix to normalized coords. |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 180 | SkMatrix maskMatrix; |
| 181 | maskMatrix.setIDiv(texture->width(), texture->height()); |
| 182 | maskMatrix.preTranslate(SkIntToScalar(-rect.fLeft), SkIntToScalar(-rect.fTop)); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 183 | |
robertphillips | 3950f0d | 2016-07-07 07:33:13 -0700 | [diff] [blame] | 184 | GrPipelineBuilder pipelineBuilder(paint, drawContext->mustUseHWAA(paint)); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 185 | pipelineBuilder.setUserStencil(userStencilSettings); |
| 186 | |
| 187 | pipelineBuilder.addCoverageFragmentProcessor( |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 188 | GrSimpleTextureEffect::Make(texture, |
| 189 | maskMatrix, |
| 190 | GrTextureParams::kNone_FilterMode, |
| 191 | kDevice_GrCoordSet)); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 192 | |
robertphillips | 3950f0d | 2016-07-07 07:33:13 -0700 | [diff] [blame] | 193 | SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(paint.getColor(), |
| 194 | SkMatrix::I(), |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 195 | dstRect, nullptr, &invert)); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 196 | drawContext->drawBatch(pipelineBuilder, clip, batch); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 197 | } |