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 Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 9 | |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 10 | #include "GrContext.h" |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 11 | #include "GrContextPriv.h" |
Robert Phillips | cb7b831 | 2018-04-20 11:49:51 -0400 | [diff] [blame] | 12 | #include "GrProxyProvider.h" |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 13 | #include "GrShape.h" |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 14 | #include "GrSurfaceContext.h" |
| 15 | #include "GrTextureProxy.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 16 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 17 | /* |
| 18 | * Convert a boolean operation into a transfer mode code |
| 19 | */ |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 20 | static SkBlendMode op_to_mode(SkRegion::Op op) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 21 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 22 | static const SkBlendMode modeMap[] = { |
| 23 | SkBlendMode::kDstOut, // kDifference_Op |
| 24 | SkBlendMode::kModulate, // kIntersect_Op |
| 25 | SkBlendMode::kSrcOver, // kUnion_Op |
| 26 | SkBlendMode::kXor, // kXOR_Op |
| 27 | SkBlendMode::kClear, // kReverseDifference_Op |
| 28 | SkBlendMode::kSrc, // kReplace_Op |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | return modeMap[op]; |
| 32 | } |
| 33 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 34 | /** |
| 35 | * Draw a single rect element of the clip stack into the accumulation bitmap |
| 36 | */ |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 37 | void GrSWMaskHelper::drawRect(const SkRect& rect, const SkMatrix& matrix, SkRegion::Op op, GrAA aa, |
| 38 | uint8_t alpha) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 39 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 40 | paint.setBlendMode(op_to_mode(op)); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 41 | paint.setAntiAlias(GrAA::kYes == aa); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 42 | paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 43 | |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 44 | SkMatrix translatedMatrix = matrix; |
| 45 | translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY); |
| 46 | fDraw.fMatrix = &translatedMatrix; |
| 47 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 48 | fDraw.drawRect(rect, paint); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Draw a single path element of the clip stack into the accumulation bitmap |
| 53 | */ |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 54 | void GrSWMaskHelper::drawShape(const GrShape& shape, const SkMatrix& matrix, SkRegion::Op op, |
| 55 | GrAA aa, uint8_t alpha) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 56 | SkPaint paint; |
Robert Phillips | f809c1e | 2017-01-13 11:02:42 -0500 | [diff] [blame] | 57 | paint.setPathEffect(shape.style().refPathEffect()); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 58 | shape.style().strokeRec().applyToPaint(&paint); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 59 | paint.setAntiAlias(GrAA::kYes == aa); |
reed@google.com | 84e922b | 2013-11-04 20:57:36 +0000 | [diff] [blame] | 60 | |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 61 | SkMatrix translatedMatrix = matrix; |
| 62 | translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY); |
| 63 | fDraw.fMatrix = &translatedMatrix; |
| 64 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 65 | SkPath path; |
| 66 | shape.asPath(&path); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 67 | if (SkRegion::kReplace_Op == op && 0xFF == alpha) { |
| 68 | SkASSERT(0xFF == paint.getAlpha()); |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 69 | fDraw.drawPathCoverage(path, paint); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 70 | } else { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 71 | paint.setBlendMode(op_to_mode(op)); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 72 | paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 73 | fDraw.drawPath(path, paint); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 74 | } |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 75 | }; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 76 | |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 77 | bool GrSWMaskHelper::init(const SkIRect& resultBounds) { |
| 78 | // We will need to translate draws so the bound's UL corner is at the origin |
| 79 | fTranslate = {-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop)}; |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 80 | SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height()); |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 81 | |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 82 | const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height()); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 83 | if (!fPixels->tryAlloc(bmImageInfo)) { |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 84 | return false; |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 85 | } |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 86 | fPixels->erase(0); |
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 | fDraw.fDst = *fPixels; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 89 | fRasterClip.setRect(bounds); |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 90 | fDraw.fRC = &fRasterClip; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 91 | return true; |
| 92 | } |
| 93 | |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 94 | sk_sp<GrTextureProxy> GrSWMaskHelper::toTextureProxy(GrContext* context, SkBackingFit fit) { |
Robert Phillips | cb7b831 | 2018-04-20 11:49:51 -0400 | [diff] [blame] | 95 | SkImageInfo ii = SkImageInfo::MakeA8(fPixels->width(), fPixels->height()); |
| 96 | size_t rowBytes = fPixels->rowBytes(); |
krajcevski | fb4f5cb | 2014-06-12 09:20:38 -0700 | [diff] [blame] | 97 | |
Robert Phillips | cb7b831 | 2018-04-20 11:49:51 -0400 | [diff] [blame] | 98 | sk_sp<SkData> data = fPixels->detachPixelsAsData(); |
| 99 | if (!data) { |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 100 | return nullptr; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 101 | } |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 102 | |
Robert Phillips | cb7b831 | 2018-04-20 11:49:51 -0400 | [diff] [blame] | 103 | sk_sp<SkImage> img = SkImage::MakeRasterData(ii, std::move(data), rowBytes); |
| 104 | if (!img) { |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 105 | return nullptr; |
| 106 | } |
cblume | ed82800 | 2016-02-16 13:00:01 -0800 | [diff] [blame] | 107 | |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 108 | // TODO: http://skbug.com/8422: Although this fixes http://skbug.com/8351, it seems like these |
| 109 | // should just participate in the normal allocation process and not need the pending IO flag. |
| 110 | auto surfaceFlags = GrInternalSurfaceFlags::kNone; |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame^] | 111 | if (!context->priv().resourceProvider()) { |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 112 | // In DDL mode, this texture proxy will be instantiated at flush time, therfore it cannot |
| 113 | // have pending IO. |
| 114 | surfaceFlags |= GrInternalSurfaceFlags::kNoPendingIO; |
| 115 | } |
Kevin Lubick | a96ec09 | 2018-12-17 11:08:36 -0500 | [diff] [blame] | 116 | auto clearFlag = kNone_GrSurfaceFlags; |
| 117 | // In a WASM build on Firefox, we see warnings like |
| 118 | // WebGL warning: texSubImage2D: This operation requires zeroing texture data. This is slow. |
| 119 | // WebGL warning: texSubImage2D: Texture has not been initialized prior to a partial upload, |
| 120 | // forcing the browser to clear it. This may be slow. |
| 121 | // Setting the initial clear seems to make those warnings go away and offers a substantial |
| 122 | // boost in performance in Firefox. Chrome sees a more modest increase. |
| 123 | #if IS_WEBGL==1 |
| 124 | clearFlag = kPerformInitialClear_GrSurfaceFlag; |
| 125 | #endif |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame^] | 126 | return context->priv().proxyProvider()->createTextureProxy( |
Kevin Lubick | a96ec09 | 2018-12-17 11:08:36 -0500 | [diff] [blame] | 127 | std::move(img), clearFlag, 1, SkBudgeted::kYes, fit, surfaceFlags); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 128 | } |