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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/private/GrRecordingContext.h" |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrBitmapTextureMaker.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrCaps.h" |
| 13 | #include "src/gpu/GrProxyProvider.h" |
| 14 | #include "src/gpu/GrRecordingContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/GrSurfaceContext.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrTextureProxy.h" |
Michael Ludwig | 663afe5 | 2019-06-03 16:46:19 -0400 | [diff] [blame] | 17 | #include "src/gpu/geometry/GrShape.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 18 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 19 | /* |
| 20 | * Convert a boolean operation into a transfer mode code |
| 21 | */ |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 22 | static SkBlendMode op_to_mode(SkRegion::Op op) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 23 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 24 | static const SkBlendMode modeMap[] = { |
| 25 | SkBlendMode::kDstOut, // kDifference_Op |
| 26 | SkBlendMode::kModulate, // kIntersect_Op |
| 27 | SkBlendMode::kSrcOver, // kUnion_Op |
| 28 | SkBlendMode::kXor, // kXOR_Op |
| 29 | SkBlendMode::kClear, // kReverseDifference_Op |
| 30 | SkBlendMode::kSrc, // kReplace_Op |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | return modeMap[op]; |
| 34 | } |
| 35 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 36 | /** |
| 37 | * Draw a single rect element of the clip stack into the accumulation bitmap |
| 38 | */ |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 39 | void GrSWMaskHelper::drawRect(const SkRect& rect, const SkMatrix& matrix, SkRegion::Op op, GrAA aa, |
| 40 | uint8_t alpha) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 41 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 42 | paint.setBlendMode(op_to_mode(op)); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 43 | paint.setAntiAlias(GrAA::kYes == aa); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 44 | paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 45 | |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 46 | SkMatrix translatedMatrix = matrix; |
| 47 | translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY); |
| 48 | fDraw.fMatrix = &translatedMatrix; |
| 49 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 50 | fDraw.drawRect(rect, paint); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Draw a single path element of the clip stack into the accumulation bitmap |
| 55 | */ |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 56 | void GrSWMaskHelper::drawShape(const GrShape& shape, const SkMatrix& matrix, SkRegion::Op op, |
| 57 | 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 | |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 63 | SkMatrix translatedMatrix = matrix; |
| 64 | translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY); |
| 65 | fDraw.fMatrix = &translatedMatrix; |
| 66 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 67 | SkPath path; |
| 68 | shape.asPath(&path); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 69 | if (SkRegion::kReplace_Op == op && 0xFF == alpha) { |
| 70 | SkASSERT(0xFF == paint.getAlpha()); |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 71 | fDraw.drawPathCoverage(path, paint); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 72 | } else { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 73 | paint.setBlendMode(op_to_mode(op)); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 74 | paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 75 | fDraw.drawPath(path, paint); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 76 | } |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 77 | }; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 78 | |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 79 | bool GrSWMaskHelper::init(const SkIRect& resultBounds) { |
| 80 | // We will need to translate draws so the bound's UL corner is at the origin |
| 81 | fTranslate = {-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop)}; |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 82 | SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height()); |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 83 | |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 84 | const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height()); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 85 | if (!fPixels->tryAlloc(bmImageInfo)) { |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 86 | return false; |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 87 | } |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 88 | fPixels->erase(0); |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 89 | |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 90 | fDraw.fDst = *fPixels; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 91 | fRasterClip.setRect(bounds); |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 92 | fDraw.fRC = &fRasterClip; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | |
Greg Daniel | 9f0dfbd | 2020-02-10 11:47:11 -0500 | [diff] [blame^] | 96 | GrSurfaceProxyView GrSWMaskHelper::toTextureView(GrRecordingContext* context, SkBackingFit fit) { |
Robert Phillips | cb7b831 | 2018-04-20 11:49:51 -0400 | [diff] [blame] | 97 | SkImageInfo ii = SkImageInfo::MakeA8(fPixels->width(), fPixels->height()); |
| 98 | size_t rowBytes = fPixels->rowBytes(); |
krajcevski | fb4f5cb | 2014-06-12 09:20:38 -0700 | [diff] [blame] | 99 | |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 100 | SkBitmap bitmap; |
| 101 | SkAssertResult(bitmap.installPixels(ii, fPixels->detachPixels(), rowBytes, |
| 102 | [](void* addr, void* context) { sk_free(addr); }, |
| 103 | nullptr)); |
| 104 | bitmap.setImmutable(); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 105 | |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 106 | GrBitmapTextureMaker maker(context, bitmap, GrBitmapTextureMaker::Cached::kNo, fit); |
Greg Daniel | c61d7e3 | 2020-02-04 14:27:45 -0500 | [diff] [blame] | 107 | auto[textureView, ct] = maker.view(GrMipMapped::kNo); |
Greg Daniel | 9f0dfbd | 2020-02-10 11:47:11 -0500 | [diff] [blame^] | 108 | return textureView; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 109 | } |