blob: 5d247fa2b73720dff1c9c47ae2cdd76c00db3bf2 [file] [log] [blame]
robertphillips@google.com58b20212012-06-27 20:44:52 +00001/*
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"
bsalomoneb1cb5c2015-05-22 08:01:09 -07009#include "GrCaps.h"
robertphillips976f5f02016-06-03 10:59:20 -070010#include "GrContext.h"
Robert Phillipse305cc1f2016-12-14 12:19:05 -050011#include "GrContextPriv.h"
Brian Salomon89527432016-12-16 09:52:16 -050012#include "GrRenderTargetContext.h"
bsalomon8acedde2016-06-24 10:42:16 -070013#include "GrShape.h"
Robert Phillipse305cc1f2016-12-14 12:19:05 -050014#include "GrSurfaceContext.h"
15#include "GrTextureProxy.h"
Brian Salomona0485d92017-06-14 19:08:01 -040016#include "SkDistanceFieldGen.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040017#include "ops/GrDrawOp.h"
18#include "ops/GrRectOpFactory.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000019
robertphillips@google.com58b20212012-06-27 20:44:52 +000020/*
21 * Convert a boolean operation into a transfer mode code
22 */
reed374772b2016-10-05 17:33:02 -070023static SkBlendMode op_to_mode(SkRegion::Op op) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000024
reed374772b2016-10-05 17:33:02 -070025 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.com58b20212012-06-27 20:44:52 +000032 };
33
34 return modeMap[op];
35}
36
robertphillips@google.com58b20212012-06-27 20:44:52 +000037/**
38 * Draw a single rect element of the clip stack into the accumulation bitmap
39 */
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050040void GrSWMaskHelper::drawRect(const SkRect& rect, SkRegion::Op op, GrAA aa, uint8_t alpha) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000041 SkPaint paint;
42
reed374772b2016-10-05 17:33:02 -070043 paint.setBlendMode(op_to_mode(op));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050044 paint.setAntiAlias(GrAA::kYes == aa);
robertphillips@google.com366f1c62012-06-29 21:38:47 +000045 paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
robertphillips@google.com58b20212012-06-27 20:44:52 +000046
robertphillips@google.com366f1c62012-06-29 21:38:47 +000047 fDraw.drawRect(rect, paint);
robertphillips@google.com58b20212012-06-27 20:44:52 +000048}
49
50/**
51 * Draw a single path element of the clip stack into the accumulation bitmap
52 */
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050053void GrSWMaskHelper::drawShape(const GrShape& shape, SkRegion::Op op, GrAA aa, uint8_t alpha) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000054 SkPaint paint;
Robert Phillipsf809c1e2017-01-13 11:02:42 -050055 paint.setPathEffect(shape.style().refPathEffect());
bsalomon8acedde2016-06-24 10:42:16 -070056 shape.style().strokeRec().applyToPaint(&paint);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050057 paint.setAntiAlias(GrAA::kYes == aa);
reed@google.com84e922b2013-11-04 20:57:36 +000058
bsalomon8acedde2016-06-24 10:42:16 -070059 SkPath path;
60 shape.asPath(&path);
reed@google.com126f7f52013-11-07 16:06:53 +000061 if (SkRegion::kReplace_Op == op && 0xFF == alpha) {
62 SkASSERT(0xFF == paint.getAlpha());
robertphillips98377402016-05-13 05:47:23 -070063 fDraw.drawPathCoverage(path, paint);
reed@google.com126f7f52013-11-07 16:06:53 +000064 } else {
reed374772b2016-10-05 17:33:02 -070065 paint.setBlendMode(op_to_mode(op));
reed@google.com126f7f52013-11-07 16:06:53 +000066 paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
robertphillips98377402016-05-13 05:47:23 -070067 fDraw.drawPath(path, paint);
reed@google.com126f7f52013-11-07 16:06:53 +000068 }
robertphillips@google.com58b20212012-06-27 20:44:52 +000069}
70
robertphillips98377402016-05-13 05:47:23 -070071bool GrSWMaskHelper::init(const SkIRect& resultBounds, const SkMatrix* matrix) {
bsalomon49f085d2014-09-05 13:34:00 -070072 if (matrix) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +000073 fMatrix = *matrix;
robertphillips@google.com58b20212012-06-27 20:44:52 +000074 } else {
75 fMatrix.setIdentity();
76 }
77
robertphillips@google.com366f1c62012-06-29 21:38:47 +000078 // Now translate so the bound's UL corner is at the origin
robertphillips98377402016-05-13 05:47:23 -070079 fMatrix.postTranslate(-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop));
80 SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height());
krajcevski25a67bc2014-07-29 11:44:26 -070081
robertphillips98377402016-05-13 05:47:23 -070082 const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height());
83 if (!fPixels.tryAlloc(bmImageInfo)) {
84 return false;
krajcevskib8ccc2f2014-08-07 08:15:14 -070085 }
robertphillips98377402016-05-13 05:47:23 -070086 fPixels.erase(0);
krajcevskib8ccc2f2014-08-07 08:15:14 -070087
reed41e010c2015-06-09 12:16:53 -070088 sk_bzero(&fDraw, sizeof(fDraw));
reed41e010c2015-06-09 12:16:53 -070089 fDraw.fDst = fPixels;
robertphillips@google.com58b20212012-06-27 20:44:52 +000090 fRasterClip.setRect(bounds);
reed41e010c2015-06-09 12:16:53 -070091 fDraw.fRC = &fRasterClip;
reed41e010c2015-06-09 12:16:53 -070092 fDraw.fMatrix = &fMatrix;
robertphillips@google.com58b20212012-06-27 20:44:52 +000093 return true;
94}
95
Robert Phillipsd3749482017-03-14 09:17:43 -040096sk_sp<GrTextureProxy> GrSWMaskHelper::toTextureProxy(GrContext* context, SkBackingFit fit) {
bsalomonf2703d82014-10-28 14:33:06 -070097 GrSurfaceDesc desc;
Robert Phillipsd3749482017-03-14 09:17:43 -040098 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
reed41e010c2015-06-09 12:16:53 -070099 desc.fWidth = fPixels.width();
100 desc.fHeight = fPixels.height();
krajcevskib3abe902014-07-30 13:08:11 -0700101 desc.fConfig = kAlpha_8_GrPixelConfig;
krajcevskifb4f5cb2014-06-12 09:20:38 -0700102
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500103 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeDeferredSurfaceContext(
104 desc,
105 fit,
106 SkBudgeted::kYes);
Robert Phillipsf200a902017-01-30 13:27:37 -0500107 if (!sContext || !sContext->asTextureProxy()) {
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500108 return nullptr;
bsalomon39ef7fb2016-09-21 11:16:05 -0700109 }
robertphillips@google.com58b20212012-06-27 20:44:52 +0000110
Robert Phillipsc949ce92017-01-19 16:59:04 -0500111 SkImageInfo ii = SkImageInfo::MakeA8(desc.fWidth, desc.fHeight);
112 if (!sContext->writePixels(ii, fPixels.addr(), fPixels.rowBytes(), 0, 0)) {
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500113 return nullptr;
114 }
cblumeed828002016-02-16 13:00:01 -0800115
Robert Phillipsf200a902017-01-30 13:27:37 -0500116 return sContext->asTextureProxyRef();
robertphillips@google.com58b20212012-06-27 20:44:52 +0000117}
118
jvanverthfa38a302014-10-06 05:59:05 -0700119/**
120 * Convert mask generation results to a signed distance field
121 */
122void GrSWMaskHelper::toSDF(unsigned char* sdf) {
reed41e010c2015-06-09 12:16:53 -0700123 SkGenerateDistanceFieldFromA8Image(sdf, (const unsigned char*)fPixels.addr(),
124 fPixels.width(), fPixels.height(), fPixels.rowBytes());
jvanverthfa38a302014-10-06 05:59:05 -0700125}
126
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000127////////////////////////////////////////////////////////////////////////////////
128/**
bsalomon8acedde2016-06-24 10:42:16 -0700129 * Software rasterizes shape to A8 mask and uploads the result to a scratch texture. Returns the
130 * resulting texture on success; nullptr on failure.
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000131 */
Robert Phillipsd3749482017-03-14 09:17:43 -0400132sk_sp<GrTextureProxy> GrSWMaskHelper::DrawShapeMaskToTexture(GrContext* context,
133 const GrShape& shape,
134 const SkIRect& resultBounds,
135 GrAA aa,
136 SkBackingFit fit,
137 const SkMatrix* matrix) {
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500138 GrSWMaskHelper helper;
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000139
reed@google.com84e922b2013-11-04 20:57:36 +0000140 if (!helper.init(resultBounds, matrix)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700141 return nullptr;
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000142 }
143
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500144 helper.drawShape(shape, SkRegion::kReplace_Op, aa, 0xFF);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000145
Robert Phillipsd3749482017-03-14 09:17:43 -0400146 return helper.toTextureProxy(context, fit);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000147}
148
Robert Phillips296b1cc2017-03-15 10:42:12 -0400149void GrSWMaskHelper::DrawToTargetWithShapeMask(sk_sp<GrTextureProxy> proxy,
Brian Osman11052242016-10-27 14:47:55 -0400150 GrRenderTargetContext* renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500151 GrPaint&& paint,
robertphillipsd2b6d642016-07-21 08:55:08 -0700152 const GrUserStencilSettings& userStencilSettings,
bsalomon8acedde2016-06-24 10:42:16 -0700153 const GrClip& clip,
bsalomon8acedde2016-06-24 10:42:16 -0700154 const SkMatrix& viewMatrix,
bsalomon39ef7fb2016-09-21 11:16:05 -0700155 const SkIPoint& textureOriginInDeviceSpace,
156 const SkIRect& deviceSpaceRectToDraw) {
joshualittd27f73e2014-12-29 07:43:36 -0800157 SkMatrix invert;
joshualitt8059eb92014-12-29 15:10:07 -0800158 if (!viewMatrix.invert(&invert)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000159 return;
160 }
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000161
bsalomon39ef7fb2016-09-21 11:16:05 -0700162 SkRect dstRect = SkRect::Make(deviceSpaceRectToDraw);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000163
bsalomon309d4d52014-12-18 10:17:44 -0800164 // We use device coords to compute the texture coordinates. We take the device coords and apply
165 // a translation so that the top-left of the device bounds maps to 0,0, and then a scaling
166 // matrix to normalized coords.
Robert Phillips67c18d62017-01-20 12:44:06 -0500167 SkMatrix maskMatrix = SkMatrix::MakeTrans(SkIntToScalar(-textureOriginInDeviceSpace.fX),
168 SkIntToScalar(-textureOriginInDeviceSpace.fY));
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400169 maskMatrix.preConcat(viewMatrix);
Brian Salomond4652ca2017-01-13 12:11:36 -0500170 paint.addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400171 std::move(proxy), nullptr, maskMatrix,
Robert Phillips296b1cc2017-03-15 10:42:12 -0400172 GrSamplerParams::kNone_FilterMode));
Brian Salomonbaaf4392017-06-15 09:59:23 -0400173 renderTargetContext->addDrawOp(clip,
174 GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
175 std::move(paint), SkMatrix::I(), invert, dstRect,
176 GrAAType::kNone, &userStencilSettings));
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000177}