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" |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 12 | #include "GrContextPriv.h" |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 13 | #include "GrPipelineBuilder.h" |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 14 | #include "GrRenderTargetContext.h" |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 15 | #include "GrShape.h" |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 16 | #include "GrSurfaceContext.h" |
| 17 | #include "GrTextureProxy.h" |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 18 | #include "ops/GrDrawOp.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 19 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 20 | #include "SkDistanceFieldGen.h" |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 21 | |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 22 | #include "ops/GrRectOpFactory.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 23 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 24 | /* |
| 25 | * Convert a boolean operation into a transfer mode code |
| 26 | */ |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 27 | static SkBlendMode op_to_mode(SkRegion::Op op) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 28 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 29 | static const SkBlendMode modeMap[] = { |
| 30 | SkBlendMode::kDstOut, // kDifference_Op |
| 31 | SkBlendMode::kModulate, // kIntersect_Op |
| 32 | SkBlendMode::kSrcOver, // kUnion_Op |
| 33 | SkBlendMode::kXor, // kXOR_Op |
| 34 | SkBlendMode::kClear, // kReverseDifference_Op |
| 35 | SkBlendMode::kSrc, // kReplace_Op |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | return modeMap[op]; |
| 39 | } |
| 40 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 41 | /** |
| 42 | * Draw a single rect element of the clip stack into the accumulation bitmap |
| 43 | */ |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 44 | void GrSWMaskHelper::drawRect(const SkRect& rect, SkRegion::Op op, GrAA aa, uint8_t alpha) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 45 | SkPaint paint; |
| 46 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 47 | paint.setBlendMode(op_to_mode(op)); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 48 | paint.setAntiAlias(GrAA::kYes == aa); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 49 | paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 50 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 51 | fDraw.drawRect(rect, paint); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Draw a single path element of the clip stack into the accumulation bitmap |
| 56 | */ |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 57 | void GrSWMaskHelper::drawShape(const GrShape& shape, SkRegion::Op op, GrAA aa, uint8_t alpha) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 58 | SkPaint paint; |
Robert Phillips | f809c1e | 2017-01-13 11:02:42 -0500 | [diff] [blame] | 59 | paint.setPathEffect(shape.style().refPathEffect()); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 60 | shape.style().strokeRec().applyToPaint(&paint); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 61 | paint.setAntiAlias(GrAA::kYes == aa); |
reed@google.com | 84e922b | 2013-11-04 20:57:36 +0000 | [diff] [blame] | 62 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 63 | SkPath path; |
| 64 | shape.asPath(&path); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 65 | if (SkRegion::kReplace_Op == op && 0xFF == alpha) { |
| 66 | SkASSERT(0xFF == paint.getAlpha()); |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 67 | fDraw.drawPathCoverage(path, paint); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 68 | } else { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 69 | paint.setBlendMode(op_to_mode(op)); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 70 | paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 71 | fDraw.drawPath(path, paint); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 72 | } |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 73 | } |
| 74 | |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 75 | bool GrSWMaskHelper::init(const SkIRect& resultBounds, const SkMatrix* matrix) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 76 | if (matrix) { |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 77 | fMatrix = *matrix; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 78 | } else { |
| 79 | fMatrix.setIdentity(); |
| 80 | } |
| 81 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 82 | // Now translate so the bound's UL corner is at the origin |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 83 | fMatrix.postTranslate(-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop)); |
| 84 | SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height()); |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 85 | |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 86 | const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height()); |
| 87 | if (!fPixels.tryAlloc(bmImageInfo)) { |
| 88 | return false; |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 89 | } |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 90 | fPixels.erase(0); |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 91 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 92 | sk_bzero(&fDraw, sizeof(fDraw)); |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 93 | fDraw.fDst = fPixels; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 94 | fRasterClip.setRect(bounds); |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 95 | fDraw.fRC = &fRasterClip; |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 96 | fDraw.fMatrix = &fMatrix; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 97 | return true; |
| 98 | } |
| 99 | |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 100 | sk_sp<GrTextureProxy> GrSWMaskHelper::toTexture(GrContext* context, SkBackingFit fit) { |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 101 | GrSurfaceDesc desc; |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 102 | desc.fWidth = fPixels.width(); |
| 103 | desc.fHeight = fPixels.height(); |
krajcevski | b3abe90 | 2014-07-30 13:08:11 -0700 | [diff] [blame] | 104 | desc.fConfig = kAlpha_8_GrPixelConfig; |
krajcevski | fb4f5cb | 2014-06-12 09:20:38 -0700 | [diff] [blame] | 105 | |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 106 | sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeDeferredSurfaceContext( |
| 107 | desc, |
| 108 | fit, |
| 109 | SkBudgeted::kYes); |
| 110 | if (!sContext || !sContext->asDeferredTexture()) { |
| 111 | return nullptr; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 112 | } |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 113 | |
Robert Phillips | c949ce9 | 2017-01-19 16:59:04 -0500 | [diff] [blame] | 114 | SkImageInfo ii = SkImageInfo::MakeA8(desc.fWidth, desc.fHeight); |
| 115 | if (!sContext->writePixels(ii, fPixels.addr(), fPixels.rowBytes(), 0, 0)) { |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 116 | return nullptr; |
| 117 | } |
cblume | ed82800 | 2016-02-16 13:00:01 -0800 | [diff] [blame] | 118 | |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 119 | return sk_ref_sp(sContext->asDeferredTexture()); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 120 | } |
| 121 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 122 | /** |
| 123 | * Convert mask generation results to a signed distance field |
| 124 | */ |
| 125 | void GrSWMaskHelper::toSDF(unsigned char* sdf) { |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 126 | SkGenerateDistanceFieldFromA8Image(sdf, (const unsigned char*)fPixels.addr(), |
| 127 | fPixels.width(), fPixels.height(), fPixels.rowBytes()); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 128 | } |
| 129 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 130 | //////////////////////////////////////////////////////////////////////////////// |
| 131 | /** |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 132 | * Software rasterizes shape to A8 mask and uploads the result to a scratch texture. Returns the |
| 133 | * resulting texture on success; nullptr on failure. |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 134 | */ |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 135 | sk_sp<GrTexture> GrSWMaskHelper::DrawShapeMaskToTexture(GrContext* context, |
| 136 | const GrShape& shape, |
| 137 | const SkIRect& resultBounds, |
| 138 | GrAA aa, |
| 139 | SkBackingFit fit, |
| 140 | const SkMatrix* matrix) { |
| 141 | GrSWMaskHelper helper; |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 142 | |
reed@google.com | 84e922b | 2013-11-04 20:57:36 +0000 | [diff] [blame] | 143 | if (!helper.init(resultBounds, matrix)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 144 | return nullptr; |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 147 | helper.drawShape(shape, SkRegion::kReplace_Op, aa, 0xFF); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 148 | |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 149 | sk_sp<GrTextureProxy> tProxy = helper.toTexture(context, fit); |
| 150 | if (!tProxy) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 151 | return nullptr; |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 154 | return sk_ref_sp(tProxy->instantiate(context->textureProvider())); |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 155 | } |
| 156 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 157 | void GrSWMaskHelper::DrawToTargetWithShapeMask(GrTexture* texture, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 158 | GrRenderTargetContext* renderTargetContext, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 159 | GrPaint&& paint, |
robertphillips | d2b6d64 | 2016-07-21 08:55:08 -0700 | [diff] [blame] | 160 | const GrUserStencilSettings& userStencilSettings, |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 161 | const GrClip& clip, |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 162 | const SkMatrix& viewMatrix, |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 163 | const SkIPoint& textureOriginInDeviceSpace, |
| 164 | const SkIRect& deviceSpaceRectToDraw) { |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 165 | SkMatrix invert; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 166 | if (!viewMatrix.invert(&invert)) { |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 167 | return; |
| 168 | } |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 169 | |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 170 | SkRect dstRect = SkRect::Make(deviceSpaceRectToDraw); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 171 | |
bsalomon | 309d4d5 | 2014-12-18 10:17:44 -0800 | [diff] [blame] | 172 | // We use device coords to compute the texture coordinates. We take the device coords and apply |
| 173 | // a translation so that the top-left of the device bounds maps to 0,0, and then a scaling |
| 174 | // matrix to normalized coords. |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 175 | SkMatrix maskMatrix = SkMatrix::MakeTrans(SkIntToScalar(-textureOriginInDeviceSpace.fX), |
| 176 | SkIntToScalar(-textureOriginInDeviceSpace.fY)); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 177 | maskMatrix.preConcat(viewMatrix); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 178 | std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFill(paint.getColor(), SkMatrix::I(), |
| 179 | dstRect, nullptr, &invert); |
Brian Salomon | d4652ca | 2017-01-13 12:11:36 -0500 | [diff] [blame] | 180 | paint.addCoverageFragmentProcessor(GrSimpleTextureEffect::Make( |
| 181 | texture, nullptr, maskMatrix, GrSamplerParams::kNone_FilterMode)); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 182 | GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone); |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 183 | pipelineBuilder.setUserStencil(&userStencilSettings); |
Brian Salomon | 24f1978 | 2016-12-13 15:10:11 -0500 | [diff] [blame] | 184 | renderTargetContext->addDrawOp(pipelineBuilder, clip, std::move(op)); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 185 | } |