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" |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 12 | #include "GrShape.h" |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 13 | #include "GrSurfaceContext.h" |
| 14 | #include "GrTextureProxy.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 15 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 16 | /* |
| 17 | * Convert a boolean operation into a transfer mode code |
| 18 | */ |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 19 | static SkBlendMode op_to_mode(SkRegion::Op op) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 20 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 21 | static const SkBlendMode modeMap[] = { |
| 22 | SkBlendMode::kDstOut, // kDifference_Op |
| 23 | SkBlendMode::kModulate, // kIntersect_Op |
| 24 | SkBlendMode::kSrcOver, // kUnion_Op |
| 25 | SkBlendMode::kXor, // kXOR_Op |
| 26 | SkBlendMode::kClear, // kReverseDifference_Op |
| 27 | SkBlendMode::kSrc, // kReplace_Op |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | return modeMap[op]; |
| 31 | } |
| 32 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 33 | /** |
| 34 | * Draw a single rect element of the clip stack into the accumulation bitmap |
| 35 | */ |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 36 | void GrSWMaskHelper::drawRect(const SkRect& rect, const SkMatrix& matrix, SkRegion::Op op, GrAA aa, |
| 37 | uint8_t alpha) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 38 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 39 | paint.setBlendMode(op_to_mode(op)); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 40 | paint.setAntiAlias(GrAA::kYes == aa); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 41 | paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 42 | |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 43 | SkMatrix translatedMatrix = matrix; |
| 44 | translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY); |
| 45 | fDraw.fMatrix = &translatedMatrix; |
| 46 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 47 | fDraw.drawRect(rect, paint); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Draw a single path element of the clip stack into the accumulation bitmap |
| 52 | */ |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 53 | void GrSWMaskHelper::drawShape(const GrShape& shape, const SkMatrix& matrix, SkRegion::Op op, |
| 54 | GrAA aa, uint8_t alpha) { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 55 | SkPaint paint; |
Robert Phillips | f809c1e | 2017-01-13 11:02:42 -0500 | [diff] [blame] | 56 | paint.setPathEffect(shape.style().refPathEffect()); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 57 | shape.style().strokeRec().applyToPaint(&paint); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 58 | paint.setAntiAlias(GrAA::kYes == aa); |
reed@google.com | 84e922b | 2013-11-04 20:57:36 +0000 | [diff] [blame] | 59 | |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 60 | SkMatrix translatedMatrix = matrix; |
| 61 | translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY); |
| 62 | fDraw.fMatrix = &translatedMatrix; |
| 63 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 64 | SkPath path; |
| 65 | shape.asPath(&path); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 66 | if (SkRegion::kReplace_Op == op && 0xFF == alpha) { |
| 67 | SkASSERT(0xFF == paint.getAlpha()); |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 68 | fDraw.drawPathCoverage(path, paint); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 69 | } else { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 70 | paint.setBlendMode(op_to_mode(op)); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 71 | paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 72 | fDraw.drawPath(path, paint); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 73 | } |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 74 | }; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 75 | |
Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 76 | bool GrSWMaskHelper::init(const SkIRect& resultBounds) { |
| 77 | // We will need to translate draws so the bound's UL corner is at the origin |
| 78 | fTranslate = {-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop)}; |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 79 | SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height()); |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 80 | |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 81 | const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height()); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 82 | if (!fPixels->tryAlloc(bmImageInfo)) { |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 83 | return false; |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 84 | } |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 85 | fPixels->erase(0); |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 86 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 87 | sk_bzero(&fDraw, sizeof(fDraw)); |
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) { |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 95 | GrSurfaceDesc desc; |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 96 | desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 97 | desc.fWidth = fPixels->width(); |
| 98 | desc.fHeight = fPixels->height(); |
krajcevski | b3abe90 | 2014-07-30 13:08:11 -0700 | [diff] [blame] | 99 | desc.fConfig = kAlpha_8_GrPixelConfig; |
krajcevski | fb4f5cb | 2014-06-12 09:20:38 -0700 | [diff] [blame] | 100 | |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 101 | sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeDeferredSurfaceContext( |
| 102 | desc, |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 103 | GrMipMapped::kNo, |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 104 | fit, |
| 105 | SkBudgeted::kYes); |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 106 | if (!sContext || !sContext->asTextureProxy()) { |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 107 | return nullptr; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 108 | } |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 109 | |
Robert Phillips | c949ce9 | 2017-01-19 16:59:04 -0500 | [diff] [blame] | 110 | SkImageInfo ii = SkImageInfo::MakeA8(desc.fWidth, desc.fHeight); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 111 | if (!sContext->writePixels(ii, fPixels->addr(), fPixels->rowBytes(), 0, 0)) { |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 112 | return nullptr; |
| 113 | } |
cblume | ed82800 | 2016-02-16 13:00:01 -0800 | [diff] [blame] | 114 | |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 115 | return sContext->asTextureProxyRef(); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 116 | } |