blob: 880991327f187f900e35afeded37fef824d9e54b [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"
bsalomonbb243832016-07-22 07:10:19 -070012#include "GrPipelineBuilder.h"
Brian Salomon89527432016-12-16 09:52:16 -050013#include "GrRenderTargetContext.h"
bsalomon8acedde2016-06-24 10:42:16 -070014#include "GrShape.h"
Robert Phillipse305cc1f2016-12-14 12:19:05 -050015#include "GrSurfaceContext.h"
16#include "GrTextureProxy.h"
Brian Salomona0485d92017-06-14 19:08:01 -040017#include "SkDistanceFieldGen.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040018#include "ops/GrDrawOp.h"
19#include "ops/GrRectOpFactory.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000020
robertphillips@google.com58b20212012-06-27 20:44:52 +000021/*
22 * Convert a boolean operation into a transfer mode code
23 */
reed374772b2016-10-05 17:33:02 -070024static SkBlendMode op_to_mode(SkRegion::Op op) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000025
reed374772b2016-10-05 17:33:02 -070026 static const SkBlendMode modeMap[] = {
27 SkBlendMode::kDstOut, // kDifference_Op
28 SkBlendMode::kModulate, // kIntersect_Op
29 SkBlendMode::kSrcOver, // kUnion_Op
30 SkBlendMode::kXor, // kXOR_Op
31 SkBlendMode::kClear, // kReverseDifference_Op
32 SkBlendMode::kSrc, // kReplace_Op
robertphillips@google.com58b20212012-06-27 20:44:52 +000033 };
34
35 return modeMap[op];
36}
37
robertphillips@google.com58b20212012-06-27 20:44:52 +000038/**
39 * Draw a single rect element of the clip stack into the accumulation bitmap
40 */
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050041void GrSWMaskHelper::drawRect(const SkRect& rect, SkRegion::Op op, GrAA aa, uint8_t alpha) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000042 SkPaint paint;
43
reed374772b2016-10-05 17:33:02 -070044 paint.setBlendMode(op_to_mode(op));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050045 paint.setAntiAlias(GrAA::kYes == aa);
robertphillips@google.com366f1c62012-06-29 21:38:47 +000046 paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
robertphillips@google.com58b20212012-06-27 20:44:52 +000047
robertphillips@google.com366f1c62012-06-29 21:38:47 +000048 fDraw.drawRect(rect, paint);
robertphillips@google.com58b20212012-06-27 20:44:52 +000049}
50
51/**
52 * Draw a single path element of the clip stack into the accumulation bitmap
53 */
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050054void GrSWMaskHelper::drawShape(const GrShape& shape, SkRegion::Op op, GrAA aa, uint8_t alpha) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000055 SkPaint paint;
Robert Phillipsf809c1e2017-01-13 11:02:42 -050056 paint.setPathEffect(shape.style().refPathEffect());
bsalomon8acedde2016-06-24 10:42:16 -070057 shape.style().strokeRec().applyToPaint(&paint);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050058 paint.setAntiAlias(GrAA::kYes == aa);
reed@google.com84e922b2013-11-04 20:57:36 +000059
bsalomon8acedde2016-06-24 10:42:16 -070060 SkPath path;
61 shape.asPath(&path);
reed@google.com126f7f52013-11-07 16:06:53 +000062 if (SkRegion::kReplace_Op == op && 0xFF == alpha) {
63 SkASSERT(0xFF == paint.getAlpha());
robertphillips98377402016-05-13 05:47:23 -070064 fDraw.drawPathCoverage(path, paint);
reed@google.com126f7f52013-11-07 16:06:53 +000065 } else {
reed374772b2016-10-05 17:33:02 -070066 paint.setBlendMode(op_to_mode(op));
reed@google.com126f7f52013-11-07 16:06:53 +000067 paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
robertphillips98377402016-05-13 05:47:23 -070068 fDraw.drawPath(path, paint);
reed@google.com126f7f52013-11-07 16:06:53 +000069 }
robertphillips@google.com58b20212012-06-27 20:44:52 +000070}
71
robertphillips98377402016-05-13 05:47:23 -070072bool GrSWMaskHelper::init(const SkIRect& resultBounds, const SkMatrix* matrix) {
bsalomon49f085d2014-09-05 13:34:00 -070073 if (matrix) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +000074 fMatrix = *matrix;
robertphillips@google.com58b20212012-06-27 20:44:52 +000075 } else {
76 fMatrix.setIdentity();
77 }
78
robertphillips@google.com366f1c62012-06-29 21:38:47 +000079 // Now translate so the bound's UL corner is at the origin
robertphillips98377402016-05-13 05:47:23 -070080 fMatrix.postTranslate(-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop));
81 SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height());
krajcevski25a67bc2014-07-29 11:44:26 -070082
robertphillips98377402016-05-13 05:47:23 -070083 const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height());
84 if (!fPixels.tryAlloc(bmImageInfo)) {
85 return false;
krajcevskib8ccc2f2014-08-07 08:15:14 -070086 }
robertphillips98377402016-05-13 05:47:23 -070087 fPixels.erase(0);
krajcevskib8ccc2f2014-08-07 08:15:14 -070088
reed41e010c2015-06-09 12:16:53 -070089 sk_bzero(&fDraw, sizeof(fDraw));
reed41e010c2015-06-09 12:16:53 -070090 fDraw.fDst = fPixels;
robertphillips@google.com58b20212012-06-27 20:44:52 +000091 fRasterClip.setRect(bounds);
reed41e010c2015-06-09 12:16:53 -070092 fDraw.fRC = &fRasterClip;
reed41e010c2015-06-09 12:16:53 -070093 fDraw.fMatrix = &fMatrix;
robertphillips@google.com58b20212012-06-27 20:44:52 +000094 return true;
95}
96
Robert Phillipsd3749482017-03-14 09:17:43 -040097sk_sp<GrTextureProxy> GrSWMaskHelper::toTextureProxy(GrContext* context, SkBackingFit fit) {
bsalomonf2703d82014-10-28 14:33:06 -070098 GrSurfaceDesc desc;
Robert Phillipsd3749482017-03-14 09:17:43 -040099 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
reed41e010c2015-06-09 12:16:53 -0700100 desc.fWidth = fPixels.width();
101 desc.fHeight = fPixels.height();
krajcevskib3abe902014-07-30 13:08:11 -0700102 desc.fConfig = kAlpha_8_GrPixelConfig;
krajcevskifb4f5cb2014-06-12 09:20:38 -0700103
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500104 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeDeferredSurfaceContext(
105 desc,
106 fit,
107 SkBudgeted::kYes);
Robert Phillipsf200a902017-01-30 13:27:37 -0500108 if (!sContext || !sContext->asTextureProxy()) {
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500109 return nullptr;
bsalomon39ef7fb2016-09-21 11:16:05 -0700110 }
robertphillips@google.com58b20212012-06-27 20:44:52 +0000111
Robert Phillipsc949ce92017-01-19 16:59:04 -0500112 SkImageInfo ii = SkImageInfo::MakeA8(desc.fWidth, desc.fHeight);
113 if (!sContext->writePixels(ii, fPixels.addr(), fPixels.rowBytes(), 0, 0)) {
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500114 return nullptr;
115 }
cblumeed828002016-02-16 13:00:01 -0800116
Robert Phillipsf200a902017-01-30 13:27:37 -0500117 return sContext->asTextureProxyRef();
robertphillips@google.com58b20212012-06-27 20:44:52 +0000118}
119
jvanverthfa38a302014-10-06 05:59:05 -0700120/**
121 * Convert mask generation results to a signed distance field
122 */
123void GrSWMaskHelper::toSDF(unsigned char* sdf) {
reed41e010c2015-06-09 12:16:53 -0700124 SkGenerateDistanceFieldFromA8Image(sdf, (const unsigned char*)fPixels.addr(),
125 fPixels.width(), fPixels.height(), fPixels.rowBytes());
jvanverthfa38a302014-10-06 05:59:05 -0700126}
127
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000128////////////////////////////////////////////////////////////////////////////////
129/**
bsalomon8acedde2016-06-24 10:42:16 -0700130 * Software rasterizes shape to A8 mask and uploads the result to a scratch texture. Returns the
131 * resulting texture on success; nullptr on failure.
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000132 */
Robert Phillipsd3749482017-03-14 09:17:43 -0400133sk_sp<GrTextureProxy> GrSWMaskHelper::DrawShapeMaskToTexture(GrContext* context,
134 const GrShape& shape,
135 const SkIRect& resultBounds,
136 GrAA aa,
137 SkBackingFit fit,
138 const SkMatrix* matrix) {
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500139 GrSWMaskHelper helper;
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000140
reed@google.com84e922b2013-11-04 20:57:36 +0000141 if (!helper.init(resultBounds, matrix)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700142 return nullptr;
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000143 }
144
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500145 helper.drawShape(shape, SkRegion::kReplace_Op, aa, 0xFF);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000146
Robert Phillipsd3749482017-03-14 09:17:43 -0400147 return helper.toTextureProxy(context, fit);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000148}
149
Robert Phillips296b1cc2017-03-15 10:42:12 -0400150void GrSWMaskHelper::DrawToTargetWithShapeMask(sk_sp<GrTextureProxy> proxy,
Brian Osman11052242016-10-27 14:47:55 -0400151 GrRenderTargetContext* renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500152 GrPaint&& paint,
robertphillipsd2b6d642016-07-21 08:55:08 -0700153 const GrUserStencilSettings& userStencilSettings,
bsalomon8acedde2016-06-24 10:42:16 -0700154 const GrClip& clip,
bsalomon8acedde2016-06-24 10:42:16 -0700155 const SkMatrix& viewMatrix,
bsalomon39ef7fb2016-09-21 11:16:05 -0700156 const SkIPoint& textureOriginInDeviceSpace,
157 const SkIRect& deviceSpaceRectToDraw) {
joshualittd27f73e2014-12-29 07:43:36 -0800158 SkMatrix invert;
joshualitt8059eb92014-12-29 15:10:07 -0800159 if (!viewMatrix.invert(&invert)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000160 return;
161 }
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000162
bsalomon39ef7fb2016-09-21 11:16:05 -0700163 SkRect dstRect = SkRect::Make(deviceSpaceRectToDraw);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000164
bsalomon309d4d52014-12-18 10:17:44 -0800165 // We use device coords to compute the texture coordinates. We take the device coords and apply
166 // a translation so that the top-left of the device bounds maps to 0,0, and then a scaling
167 // matrix to normalized coords.
Robert Phillips67c18d62017-01-20 12:44:06 -0500168 SkMatrix maskMatrix = SkMatrix::MakeTrans(SkIntToScalar(-textureOriginInDeviceSpace.fX),
169 SkIntToScalar(-textureOriginInDeviceSpace.fY));
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400170 maskMatrix.preConcat(viewMatrix);
Brian Salomond4652ca2017-01-13 12:11:36 -0500171 paint.addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400172 std::move(proxy), nullptr, maskMatrix,
Robert Phillips296b1cc2017-03-15 10:42:12 -0400173 GrSamplerParams::kNone_FilterMode));
Brian Salomonbaaf4392017-06-15 09:59:23 -0400174 renderTargetContext->addDrawOp(clip,
175 GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
176 std::move(paint), SkMatrix::I(), invert, dstRect,
177 GrAAType::kNone, &userStencilSettings));
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000178}