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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrSWMaskHelper.h" |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 9 | |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 10 | #include "include/gpu/GrRecordingContext.h" |
Brian Osman | 9aaec36 | 2020-05-08 14:54:37 -0400 | [diff] [blame] | 11 | #include "src/core/SkMatrixProvider.h" |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrBitmapTextureMaker.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrCaps.h" |
| 14 | #include "src/gpu/GrProxyProvider.h" |
| 15 | #include "src/gpu/GrRecordingContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/GrSurfaceContext.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 17 | #include "src/gpu/GrTextureProxy.h" |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 18 | #include "src/gpu/geometry/GrStyledShape.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 19 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 20 | /* |
| 21 | * Convert a boolean operation into a transfer mode code |
| 22 | */ |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 23 | static SkBlendMode op_to_mode(SkRegion::Op op) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 24 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 25 | static const SkBlendMode modeMap[] = { |
| 26 | SkBlendMode::kDstOut, // kDifference_Op |
| 27 | SkBlendMode::kModulate, // kIntersect_Op |
| 28 | SkBlendMode::kSrcOver, // kUnion_Op |
| 29 | SkBlendMode::kXor, // kXOR_Op |
| 30 | SkBlendMode::kClear, // kReverseDifference_Op |
| 31 | SkBlendMode::kSrc, // kReplace_Op |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | return modeMap[op]; |
| 35 | } |
| 36 | |
Michael Ludwig | 4fc7c5f | 2020-05-04 14:51:43 -0400 | [diff] [blame] | 37 | static SkPaint get_paint(SkRegion::Op op, GrAA aa, uint8_t alpha) { |
| 38 | SkPaint paint; |
| 39 | paint.setBlendMode(op_to_mode(op)); |
| 40 | paint.setAntiAlias(GrAA::kYes == aa); |
| 41 | // SkPaint's color is unpremul so this will produce alpha in every channel. |
| 42 | paint.setColor(SkColorSetARGB(alpha, 255, 255, 255)); |
| 43 | return paint; |
| 44 | } |
| 45 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 46 | /** |
| 47 | * Draw a single rect element of the clip stack into the accumulation bitmap |
| 48 | */ |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 49 | void GrSWMaskHelper::drawRect(const SkRect& rect, const SkMatrix& matrix, SkRegion::Op op, GrAA aa, |
| 50 | uint8_t alpha) { |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 51 | SkMatrix translatedMatrix = matrix; |
| 52 | translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY); |
Brian Osman | 9aaec36 | 2020-05-08 14:54:37 -0400 | [diff] [blame] | 53 | SkSimpleMatrixProvider matrixProvider(translatedMatrix); |
| 54 | fDraw.fMatrixProvider = &matrixProvider; |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 55 | |
Michael Ludwig | 4fc7c5f | 2020-05-04 14:51:43 -0400 | [diff] [blame] | 56 | fDraw.drawRect(rect, get_paint(op, aa, alpha)); |
| 57 | } |
| 58 | |
| 59 | void GrSWMaskHelper::drawRRect(const SkRRect& rrect, const SkMatrix& matrix, SkRegion::Op op, |
| 60 | GrAA aa, uint8_t alpha) { |
| 61 | SkMatrix translatedMatrix = matrix; |
| 62 | translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY); |
Brian Osman | 9aaec36 | 2020-05-08 14:54:37 -0400 | [diff] [blame] | 63 | SkSimpleMatrixProvider matrixProvider(translatedMatrix); |
| 64 | fDraw.fMatrixProvider = &matrixProvider; |
Michael Ludwig | 4fc7c5f | 2020-05-04 14:51:43 -0400 | [diff] [blame] | 65 | |
| 66 | fDraw.drawRRect(rrect, get_paint(op, aa, alpha)); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Draw a single path element of the clip stack into the accumulation bitmap |
| 71 | */ |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 72 | void GrSWMaskHelper::drawShape(const GrStyledShape& shape, const SkMatrix& matrix, SkRegion::Op op, |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 73 | GrAA aa, uint8_t alpha) { |
Michael Ludwig | 4fc7c5f | 2020-05-04 14:51:43 -0400 | [diff] [blame] | 74 | SkPaint paint = get_paint(op, aa, alpha); |
Robert Phillips | f809c1e | 2017-01-13 11:02:42 -0500 | [diff] [blame] | 75 | paint.setPathEffect(shape.style().refPathEffect()); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 76 | shape.style().strokeRec().applyToPaint(&paint); |
reed@google.com | 84e922b | 2013-11-04 20:57:36 +0000 | [diff] [blame] | 77 | |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 78 | SkMatrix translatedMatrix = matrix; |
| 79 | translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY); |
Brian Osman | 9aaec36 | 2020-05-08 14:54:37 -0400 | [diff] [blame] | 80 | SkSimpleMatrixProvider matrixProvider(translatedMatrix); |
| 81 | fDraw.fMatrixProvider = &matrixProvider; |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 82 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 83 | SkPath path; |
| 84 | shape.asPath(&path); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 85 | if (SkRegion::kReplace_Op == op && 0xFF == alpha) { |
| 86 | SkASSERT(0xFF == paint.getAlpha()); |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 87 | fDraw.drawPathCoverage(path, paint); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 88 | } else { |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 89 | fDraw.drawPath(path, paint); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 90 | } |
Michael Ludwig | 4fc7c5f | 2020-05-04 14:51:43 -0400 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void GrSWMaskHelper::drawShape(const GrShape& shape, const SkMatrix& matrix, SkRegion::Op op, |
| 94 | GrAA aa, uint8_t alpha) { |
| 95 | SkPaint paint = get_paint(op, aa, alpha); |
| 96 | |
| 97 | SkMatrix translatedMatrix = matrix; |
| 98 | translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY); |
Brian Osman | 9aaec36 | 2020-05-08 14:54:37 -0400 | [diff] [blame] | 99 | SkSimpleMatrixProvider matrixProvider(translatedMatrix); |
| 100 | fDraw.fMatrixProvider = &matrixProvider; |
Michael Ludwig | 4fc7c5f | 2020-05-04 14:51:43 -0400 | [diff] [blame] | 101 | |
| 102 | if (shape.inverted()) { |
| 103 | if (shape.isEmpty() || shape.isLine() || shape.isPoint()) { |
| 104 | // These shapes are empty for simple fills, so when inverted, cover everything |
| 105 | fDraw.drawPaint(paint); |
| 106 | return; |
| 107 | } |
| 108 | // Else fall through to the draw method using asPath(), which will toggle fill type properly |
| 109 | } else if (shape.isEmpty() || shape.isLine() || shape.isPoint()) { |
| 110 | // Do nothing, these shapes do not cover any pixels for simple fills |
| 111 | return; |
| 112 | } else if (shape.isRect()) { |
| 113 | fDraw.drawRect(shape.rect(), paint); |
| 114 | return; |
| 115 | } else if (shape.isRRect()) { |
| 116 | fDraw.drawRRect(shape.rrect(), paint); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | // A complex, or inverse-filled shape, so go through drawPath. |
| 121 | SkPath path; |
| 122 | shape.asPath(&path); |
| 123 | if (SkRegion::kReplace_Op == op && 0xFF == alpha) { |
| 124 | SkASSERT(0xFF == paint.getAlpha()); |
| 125 | fDraw.drawPathCoverage(path, paint); |
| 126 | } else { |
| 127 | fDraw.drawPath(path, paint); |
| 128 | } |
| 129 | } |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 130 | |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 131 | bool GrSWMaskHelper::init(const SkIRect& resultBounds) { |
| 132 | // We will need to translate draws so the bound's UL corner is at the origin |
| 133 | fTranslate = {-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop)}; |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 134 | SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height()); |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 135 | |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 136 | const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height()); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 137 | if (!fPixels->tryAlloc(bmImageInfo)) { |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 138 | return false; |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 139 | } |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 140 | fPixels->erase(0); |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 141 | |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 142 | fDraw.fDst = *fPixels; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 143 | fRasterClip.setRect(bounds); |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 144 | fDraw.fRC = &fRasterClip; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 145 | return true; |
| 146 | } |
| 147 | |
Greg Daniel | 9f0dfbd | 2020-02-10 11:47:11 -0500 | [diff] [blame] | 148 | GrSurfaceProxyView GrSWMaskHelper::toTextureView(GrRecordingContext* context, SkBackingFit fit) { |
Robert Phillips | cb7b831 | 2018-04-20 11:49:51 -0400 | [diff] [blame] | 149 | SkImageInfo ii = SkImageInfo::MakeA8(fPixels->width(), fPixels->height()); |
| 150 | size_t rowBytes = fPixels->rowBytes(); |
krajcevski | fb4f5cb | 2014-06-12 09:20:38 -0700 | [diff] [blame] | 151 | |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 152 | SkBitmap bitmap; |
| 153 | SkAssertResult(bitmap.installPixels(ii, fPixels->detachPixels(), rowBytes, |
| 154 | [](void* addr, void* context) { sk_free(addr); }, |
| 155 | nullptr)); |
| 156 | bitmap.setImmutable(); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 157 | |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 158 | GrBitmapTextureMaker maker(context, bitmap, fit); |
Brian Salomon | ecbb0fb | 2020-02-28 18:07:32 -0500 | [diff] [blame] | 159 | return maker.view(GrMipMapped::kNo); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 160 | } |