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" |
Brian Salomon | a0485d9 | 2017-06-14 19:08:01 -0400 | [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 | 1ec03f3 | 2017-06-14 09:49:26 -0400 | [diff] [blame] | 18 | #include "ops/GrDrawOp.h" |
Brian Salomon | a0485d9 | 2017-06-14 19:08:01 -0400 | [diff] [blame] | 19 | |
| 20 | #include "SkDistanceFieldGen.h" |
| 21 | |
| 22 | #include "ops/GrNonAAFillRectOp.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 | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 100 | sk_sp<GrTextureProxy> GrSWMaskHelper::toTextureProxy(GrContext* context, SkBackingFit fit) { |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 101 | GrSurfaceDesc desc; |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 102 | desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 103 | desc.fWidth = fPixels.width(); |
| 104 | desc.fHeight = fPixels.height(); |
krajcevski | b3abe90 | 2014-07-30 13:08:11 -0700 | [diff] [blame] | 105 | desc.fConfig = kAlpha_8_GrPixelConfig; |
krajcevski | fb4f5cb | 2014-06-12 09:20:38 -0700 | [diff] [blame] | 106 | |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 107 | sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeDeferredSurfaceContext( |
| 108 | desc, |
| 109 | fit, |
| 110 | SkBudgeted::kYes); |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 111 | if (!sContext || !sContext->asTextureProxy()) { |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 112 | return nullptr; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 113 | } |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 114 | |
Robert Phillips | c949ce9 | 2017-01-19 16:59:04 -0500 | [diff] [blame] | 115 | SkImageInfo ii = SkImageInfo::MakeA8(desc.fWidth, desc.fHeight); |
| 116 | if (!sContext->writePixels(ii, fPixels.addr(), fPixels.rowBytes(), 0, 0)) { |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 117 | return nullptr; |
| 118 | } |
cblume | ed82800 | 2016-02-16 13:00:01 -0800 | [diff] [blame] | 119 | |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 120 | return sContext->asTextureProxyRef(); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 121 | } |
| 122 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 123 | /** |
| 124 | * Convert mask generation results to a signed distance field |
| 125 | */ |
| 126 | void GrSWMaskHelper::toSDF(unsigned char* sdf) { |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 127 | SkGenerateDistanceFieldFromA8Image(sdf, (const unsigned char*)fPixels.addr(), |
| 128 | fPixels.width(), fPixels.height(), fPixels.rowBytes()); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 129 | } |
| 130 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 131 | //////////////////////////////////////////////////////////////////////////////// |
| 132 | /** |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 133 | * Software rasterizes shape to A8 mask and uploads the result to a scratch texture. Returns the |
| 134 | * resulting texture on success; nullptr on failure. |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 135 | */ |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 136 | sk_sp<GrTextureProxy> GrSWMaskHelper::DrawShapeMaskToTexture(GrContext* context, |
| 137 | const GrShape& shape, |
| 138 | const SkIRect& resultBounds, |
| 139 | GrAA aa, |
| 140 | SkBackingFit fit, |
| 141 | const SkMatrix* matrix) { |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 142 | GrSWMaskHelper helper; |
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 | |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 148 | helper.drawShape(shape, SkRegion::kReplace_Op, aa, 0xFF); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 149 | |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 150 | return helper.toTextureProxy(context, fit); |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 153 | void GrSWMaskHelper::DrawToTargetWithShapeMask(sk_sp<GrTextureProxy> proxy, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 154 | GrRenderTargetContext* renderTargetContext, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 155 | GrPaint&& paint, |
robertphillips | d2b6d64 | 2016-07-21 08:55:08 -0700 | [diff] [blame] | 156 | const GrUserStencilSettings& userStencilSettings, |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 157 | const GrClip& clip, |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 158 | const SkMatrix& viewMatrix, |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 159 | const SkIPoint& textureOriginInDeviceSpace, |
| 160 | const SkIRect& deviceSpaceRectToDraw) { |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 161 | SkMatrix invert; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 162 | if (!viewMatrix.invert(&invert)) { |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 163 | return; |
| 164 | } |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 165 | |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 166 | GrResourceProvider* resourceProvider = renderTargetContext->resourceProvider(); |
| 167 | |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 168 | SkRect dstRect = SkRect::Make(deviceSpaceRectToDraw); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 169 | |
bsalomon | 309d4d5 | 2014-12-18 10:17:44 -0800 | [diff] [blame] | 170 | // We use device coords to compute the texture coordinates. We take the device coords and apply |
| 171 | // a translation so that the top-left of the device bounds maps to 0,0, and then a scaling |
| 172 | // matrix to normalized coords. |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 173 | SkMatrix maskMatrix = SkMatrix::MakeTrans(SkIntToScalar(-textureOriginInDeviceSpace.fX), |
| 174 | SkIntToScalar(-textureOriginInDeviceSpace.fY)); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 175 | maskMatrix.preConcat(viewMatrix); |
Brian Salomon | d4652ca | 2017-01-13 12:11:36 -0500 | [diff] [blame] | 176 | paint.addCoverageFragmentProcessor(GrSimpleTextureEffect::Make( |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 177 | resourceProvider, std::move(proxy), nullptr, maskMatrix, |
| 178 | GrSamplerParams::kNone_FilterMode)); |
Brian Salomon | a0485d9 | 2017-06-14 19:08:01 -0400 | [diff] [blame] | 179 | renderTargetContext->addDrawOp( |
| 180 | clip, GrNonAAFillRectOp::Make(std::move(paint), SkMatrix::I(), dstRect, nullptr, |
| 181 | &invert, GrAAType::kNone, &userStencilSettings)); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 182 | } |