blob: 0b3bdb22282c77d33a0897dc4a5dc11a3e9445d0 [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"
krajcevski5c2fca02014-06-10 17:25:28 -07009
bsalomoneb1cb5c2015-05-22 08:01:09 -070010#include "GrCaps.h"
robertphillips976f5f02016-06-03 10:59:20 -070011#include "GrContext.h"
12#include "batches/GrDrawBatch.h"
13#include "GrDrawContext.h"
joshualitt04194f32016-01-13 10:08:27 -080014#include "GrPipelineBuilder.h"
bsalomon8acedde2016-06-24 10:42:16 -070015#include "GrShape.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000016
jvanverthfa38a302014-10-06 05:59:05 -070017#include "SkDistanceFieldGen.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000018
joshualitt04194f32016-01-13 10:08:27 -080019#include "batches/GrRectBatchFactory.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 */
robertphillips98377402016-05-13 05:47:23 -070024static SkXfermode::Mode op_to_mode(SkRegion::Op op) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000025
26 static const SkXfermode::Mode modeMap[] = {
27 SkXfermode::kDstOut_Mode, // kDifference_Op
reed@google.com8d3cd7a2013-01-30 21:36:11 +000028 SkXfermode::kModulate_Mode, // kIntersect_Op
robertphillips@google.com58b20212012-06-27 20:44:52 +000029 SkXfermode::kSrcOver_Mode, // kUnion_Op
30 SkXfermode::kXor_Mode, // kXOR_Op
31 SkXfermode::kClear_Mode, // kReverseDifference_Op
32 SkXfermode::kSrc_Mode, // kReplace_Op
33 };
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 */
robertphillips98377402016-05-13 05:47:23 -070041void GrSWMaskHelper::drawRect(const SkRect& rect, SkRegion::Op op,
42 bool antiAlias, uint8_t alpha) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000043 SkPaint paint;
44
reedcfb6bdf2016-03-29 11:32:50 -070045 paint.setXfermode(SkXfermode::Make(op_to_mode(op)));
robertphillips@google.com58b20212012-06-27 20:44:52 +000046 paint.setAntiAlias(antiAlias);
robertphillips@google.com366f1c62012-06-29 21:38:47 +000047 paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
robertphillips@google.com58b20212012-06-27 20:44:52 +000048
robertphillips@google.com366f1c62012-06-29 21:38:47 +000049 fDraw.drawRect(rect, paint);
robertphillips@google.com58b20212012-06-27 20:44:52 +000050}
51
52/**
53 * Draw a single path element of the clip stack into the accumulation bitmap
54 */
bsalomon8acedde2016-06-24 10:42:16 -070055void GrSWMaskHelper::drawShape(const GrShape& shape, SkRegion::Op op, bool antiAlias,
56 uint8_t alpha) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000057 SkPaint paint;
bsalomon8acedde2016-06-24 10:42:16 -070058 paint.setPathEffect(sk_ref_sp(shape.style().pathEffect()));
59 shape.style().strokeRec().applyToPaint(&paint);
reed@google.com84e922b2013-11-04 20:57:36 +000060 paint.setAntiAlias(antiAlias);
reed@google.com84e922b2013-11-04 20:57:36 +000061
bsalomon8acedde2016-06-24 10:42:16 -070062 SkPath path;
63 shape.asPath(&path);
reed@google.com126f7f52013-11-07 16:06:53 +000064 if (SkRegion::kReplace_Op == op && 0xFF == alpha) {
65 SkASSERT(0xFF == paint.getAlpha());
robertphillips98377402016-05-13 05:47:23 -070066 fDraw.drawPathCoverage(path, paint);
reed@google.com126f7f52013-11-07 16:06:53 +000067 } else {
68 paint.setXfermodeMode(op_to_mode(op));
69 paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
robertphillips98377402016-05-13 05:47:23 -070070 fDraw.drawPath(path, paint);
reed@google.com126f7f52013-11-07 16:06:53 +000071 }
robertphillips@google.com58b20212012-06-27 20:44:52 +000072}
73
robertphillips98377402016-05-13 05:47:23 -070074bool GrSWMaskHelper::init(const SkIRect& resultBounds, const SkMatrix* matrix) {
bsalomon49f085d2014-09-05 13:34:00 -070075 if (matrix) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +000076 fMatrix = *matrix;
robertphillips@google.com58b20212012-06-27 20:44:52 +000077 } else {
78 fMatrix.setIdentity();
79 }
80
robertphillips@google.com366f1c62012-06-29 21:38:47 +000081 // Now translate so the bound's UL corner is at the origin
robertphillips98377402016-05-13 05:47:23 -070082 fMatrix.postTranslate(-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop));
83 SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height());
krajcevski25a67bc2014-07-29 11:44:26 -070084
robertphillips98377402016-05-13 05:47:23 -070085 const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height());
86 if (!fPixels.tryAlloc(bmImageInfo)) {
87 return false;
krajcevskib8ccc2f2014-08-07 08:15:14 -070088 }
robertphillips98377402016-05-13 05:47:23 -070089 fPixels.erase(0);
krajcevskib8ccc2f2014-08-07 08:15:14 -070090
reed41e010c2015-06-09 12:16:53 -070091 sk_bzero(&fDraw, sizeof(fDraw));
reed41e010c2015-06-09 12:16:53 -070092 fDraw.fDst = fPixels;
robertphillips@google.com58b20212012-06-27 20:44:52 +000093 fRasterClip.setRect(bounds);
reed41e010c2015-06-09 12:16:53 -070094 fDraw.fRC = &fRasterClip;
reed41e010c2015-06-09 12:16:53 -070095 fDraw.fMatrix = &fMatrix;
robertphillips@google.com58b20212012-06-27 20:44:52 +000096 return true;
97}
98
99/**
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000100 * Get a texture (from the texture cache) of the correct size & format.
robertphillips@google.com58b20212012-06-27 20:44:52 +0000101 */
bsalomone3059732014-10-14 11:47:22 -0700102GrTexture* GrSWMaskHelper::createTexture() {
bsalomonf2703d82014-10-28 14:33:06 -0700103 GrSurfaceDesc desc;
reed41e010c2015-06-09 12:16:53 -0700104 desc.fWidth = fPixels.width();
105 desc.fHeight = fPixels.height();
krajcevskib3abe902014-07-30 13:08:11 -0700106 desc.fConfig = kAlpha_8_GrPixelConfig;
krajcevskifb4f5cb2014-06-12 09:20:38 -0700107
robertphillips0152d732016-05-20 06:38:43 -0700108 return fTexProvider->createApproxTexture(desc);
robertphillips@google.com58b20212012-06-27 20:44:52 +0000109}
110
111/**
112 * Move the result of the software mask generation back to the gpu
113 */
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +0000114void GrSWMaskHelper::toTexture(GrTexture *texture) {
robertphillips98377402016-05-13 05:47:23 -0700115 // Since we're uploading to it, and it's compressed, 'texture' shouldn't
116 // have a render target.
117 SkASSERT(!texture->asRenderTarget());
cblumeed828002016-02-16 13:00:01 -0800118
robertphillips98377402016-05-13 05:47:23 -0700119 texture->writePixels(0, 0, fPixels.width(), fPixels.height(), texture->config(),
120 fPixels.addr(), fPixels.rowBytes());
krajcevskib8ccc2f2014-08-07 08:15:14 -0700121
robertphillips@google.com58b20212012-06-27 20:44:52 +0000122}
123
jvanverthfa38a302014-10-06 05:59:05 -0700124/**
125 * Convert mask generation results to a signed distance field
126 */
127void GrSWMaskHelper::toSDF(unsigned char* sdf) {
reed41e010c2015-06-09 12:16:53 -0700128 SkGenerateDistanceFieldFromA8Image(sdf, (const unsigned char*)fPixels.addr(),
129 fPixels.width(), fPixels.height(), fPixels.rowBytes());
jvanverthfa38a302014-10-06 05:59:05 -0700130}
131
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000132////////////////////////////////////////////////////////////////////////////////
133/**
bsalomon8acedde2016-06-24 10:42:16 -0700134 * Software rasterizes shape to A8 mask and uploads the result to a scratch texture. Returns the
135 * resulting texture on success; nullptr on failure.
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000136 */
bsalomon8acedde2016-06-24 10:42:16 -0700137GrTexture* GrSWMaskHelper::DrawShapeMaskToTexture(GrTextureProvider* texProvider,
138 const GrShape& shape,
139 const SkIRect& resultBounds,
140 bool antiAlias,
141 const SkMatrix* matrix) {
robertphillips0152d732016-05-20 06:38:43 -0700142 GrSWMaskHelper helper(texProvider);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000143
reed@google.com84e922b2013-11-04 20:57:36 +0000144 if (!helper.init(resultBounds, matrix)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700145 return nullptr;
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000146 }
147
bsalomon8acedde2016-06-24 10:42:16 -0700148 helper.drawShape(shape, SkRegion::kReplace_Op, antiAlias, 0xFF);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000149
bsalomone3059732014-10-14 11:47:22 -0700150 GrTexture* texture(helper.createTexture());
151 if (!texture) {
halcanary96fcdcc2015-08-27 07:41:13 -0700152 return nullptr;
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000153 }
154
bsalomone3059732014-10-14 11:47:22 -0700155 helper.toTexture(texture);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000156
bsalomone3059732014-10-14 11:47:22 -0700157 return texture;
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000158}
159
bsalomon8acedde2016-06-24 10:42:16 -0700160void GrSWMaskHelper::DrawToTargetWithShapeMask(GrTexture* texture,
161 GrDrawContext* drawContext,
robertphillips3950f0d2016-07-07 07:33:13 -0700162 const GrPaint& paint,
bsalomon8acedde2016-06-24 10:42:16 -0700163 const GrUserStencilSettings* userStencilSettings,
164 const GrClip& clip,
bsalomon8acedde2016-06-24 10:42:16 -0700165 const SkMatrix& viewMatrix,
166 const SkIRect& rect) {
joshualittd27f73e2014-12-29 07:43:36 -0800167 SkMatrix invert;
joshualitt8059eb92014-12-29 15:10:07 -0800168 if (!viewMatrix.invert(&invert)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000169 return;
170 }
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000171
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000172 SkRect dstRect = SkRect::MakeLTRB(SK_Scalar1 * rect.fLeft,
173 SK_Scalar1 * rect.fTop,
174 SK_Scalar1 * rect.fRight,
175 SK_Scalar1 * rect.fBottom);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000176
bsalomon309d4d52014-12-18 10:17:44 -0800177 // We use device coords to compute the texture coordinates. We take the device coords and apply
178 // a translation so that the top-left of the device bounds maps to 0,0, and then a scaling
179 // matrix to normalized coords.
bsalomon@google.comc7818882013-03-20 19:19:53 +0000180 SkMatrix maskMatrix;
181 maskMatrix.setIDiv(texture->width(), texture->height());
182 maskMatrix.preTranslate(SkIntToScalar(-rect.fLeft), SkIntToScalar(-rect.fTop));
bsalomon@google.comc7818882013-03-20 19:19:53 +0000183
robertphillips3950f0d2016-07-07 07:33:13 -0700184 GrPipelineBuilder pipelineBuilder(paint, drawContext->mustUseHWAA(paint));
robertphillips976f5f02016-06-03 10:59:20 -0700185 pipelineBuilder.setUserStencil(userStencilSettings);
186
187 pipelineBuilder.addCoverageFragmentProcessor(
bungeman06ca8ec2016-06-09 08:01:03 -0700188 GrSimpleTextureEffect::Make(texture,
189 maskMatrix,
190 GrTextureParams::kNone_FilterMode,
191 kDevice_GrCoordSet));
bsalomon@google.comc7818882013-03-20 19:19:53 +0000192
robertphillips3950f0d2016-07-07 07:33:13 -0700193 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(paint.getColor(),
194 SkMatrix::I(),
joshualitt04194f32016-01-13 10:08:27 -0800195 dstRect, nullptr, &invert));
robertphillips976f5f02016-06-03 10:59:20 -0700196 drawContext->drawBatch(pipelineBuilder, clip, batch);
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000197}