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 | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 36 | 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] | 37 | SkPaint paint; |
| 38 | |
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 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 43 | fDraw.drawRect(rect, paint); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Draw a single path element of the clip stack into the accumulation bitmap |
| 48 | */ |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 49 | 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] | 50 | SkPaint paint; |
Robert Phillips | f809c1e | 2017-01-13 11:02:42 -0500 | [diff] [blame] | 51 | paint.setPathEffect(shape.style().refPathEffect()); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 52 | shape.style().strokeRec().applyToPaint(&paint); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 53 | paint.setAntiAlias(GrAA::kYes == aa); |
reed@google.com | 84e922b | 2013-11-04 20:57:36 +0000 | [diff] [blame] | 54 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 55 | SkPath path; |
| 56 | shape.asPath(&path); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 57 | if (SkRegion::kReplace_Op == op && 0xFF == alpha) { |
| 58 | SkASSERT(0xFF == paint.getAlpha()); |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 59 | fDraw.drawPathCoverage(path, paint); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 60 | } else { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 61 | paint.setBlendMode(op_to_mode(op)); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 62 | paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 63 | fDraw.drawPath(path, paint); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 64 | } |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 65 | } |
| 66 | |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 67 | bool GrSWMaskHelper::init(const SkIRect& resultBounds, const SkMatrix* matrix) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 68 | if (matrix) { |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 69 | fMatrix = *matrix; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 70 | } else { |
| 71 | fMatrix.setIdentity(); |
| 72 | } |
| 73 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 74 | // Now translate so the bound's UL corner is at the origin |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 75 | fMatrix.postTranslate(-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop)); |
| 76 | SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height()); |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 77 | |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 78 | const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height()); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 79 | if (!fPixels->tryAlloc(bmImageInfo)) { |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 80 | return false; |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 81 | } |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 82 | fPixels->erase(0); |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 83 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 84 | sk_bzero(&fDraw, sizeof(fDraw)); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 85 | fDraw.fDst = *fPixels; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 86 | fRasterClip.setRect(bounds); |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 87 | fDraw.fRC = &fRasterClip; |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 88 | fDraw.fMatrix = &fMatrix; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 89 | return true; |
| 90 | } |
| 91 | |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 92 | sk_sp<GrTextureProxy> GrSWMaskHelper::toTextureProxy(GrContext* context, SkBackingFit fit) { |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 93 | GrSurfaceDesc desc; |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 94 | desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 95 | desc.fWidth = fPixels->width(); |
| 96 | desc.fHeight = fPixels->height(); |
krajcevski | b3abe90 | 2014-07-30 13:08:11 -0700 | [diff] [blame] | 97 | desc.fConfig = kAlpha_8_GrPixelConfig; |
krajcevski | fb4f5cb | 2014-06-12 09:20:38 -0700 | [diff] [blame] | 98 | |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 99 | sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeDeferredSurfaceContext( |
| 100 | desc, |
| 101 | fit, |
| 102 | SkBudgeted::kYes); |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 103 | if (!sContext || !sContext->asTextureProxy()) { |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 104 | return nullptr; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 105 | } |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 106 | |
Robert Phillips | c949ce9 | 2017-01-19 16:59:04 -0500 | [diff] [blame] | 107 | SkImageInfo ii = SkImageInfo::MakeA8(desc.fWidth, desc.fHeight); |
Brian Osman | f981066 | 2017-08-30 10:02:10 -0400 | [diff] [blame] | 108 | if (!sContext->writePixels(ii, fPixels->addr(), fPixels->rowBytes(), 0, 0)) { |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 109 | return nullptr; |
| 110 | } |
cblume | ed82800 | 2016-02-16 13:00:01 -0800 | [diff] [blame] | 111 | |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 112 | return sContext->asTextureProxyRef(); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 113 | } |