blob: 28d5a9b6b7b956faf585645ac0af0492defc7bff [file] [log] [blame]
joshualitt9ff64252015-08-10 09:03:51 -07001/*
Michael Ludwig72ab3462018-12-10 12:43:36 -05002 * Copyright 2018 Google Inc.
joshualitt9ff64252015-08-10 09:03:51 -07003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/ops/GrStrokeRectOp.h"
Michael Ludwig72ab3462018-12-10 12:43:36 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkStrokeRec.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/GrResourceKey.h"
12#include "include/utils/SkRandom.h"
Michael Ludwigfbe28592020-06-26 16:02:15 -040013#include "src/core/SkMatrixPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrCaps.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040015#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrDefaultGeoProcFactory.h"
17#include "src/gpu/GrDrawOpTest.h"
18#include "src/gpu/GrOpFlushState.h"
Robert Phillipsd2f18732020-03-04 16:12:08 -050019#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrResourceProvider.h"
21#include "src/gpu/GrVertexWriter.h"
22#include "src/gpu/ops/GrFillRectOp.h"
23#include "src/gpu/ops/GrMeshDrawOp.h"
24#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
joshualitt9ff64252015-08-10 09:03:51 -070025
Michael Ludwig72ab3462018-12-10 12:43:36 -050026namespace {
joshualitt9ff64252015-08-10 09:03:51 -070027
bsalomon8b7a9e12016-07-06 13:06:22 -070028// We support all hairlines, bevels, and miters, but not round joins. Also, check whether the miter
Michael Ludwig72ab3462018-12-10 12:43:36 -050029// limit makes a miter join effectively beveled. If the miter is effectively beveled, it is only
30// supported when using an AA stroke.
31inline static bool allowed_stroke(const SkStrokeRec& stroke, GrAA aa, bool* isMiter) {
bsalomon8b7a9e12016-07-06 13:06:22 -070032 SkASSERT(stroke.getStyle() == SkStrokeRec::kStroke_Style ||
33 stroke.getStyle() == SkStrokeRec::kHairline_Style);
34 // For hairlines, make bevel and round joins appear the same as mitered ones.
35 if (!stroke.getWidth()) {
36 *isMiter = true;
37 return true;
38 }
39 if (stroke.getJoin() == SkPaint::kBevel_Join) {
40 *isMiter = false;
Michael Ludwig72ab3462018-12-10 12:43:36 -050041 return aa == GrAA::kYes; // bevel only supported with AA
bsalomon8b7a9e12016-07-06 13:06:22 -070042 }
43 if (stroke.getJoin() == SkPaint::kMiter_Join) {
44 *isMiter = stroke.getMiter() >= SK_ScalarSqrt2;
Michael Ludwig72ab3462018-12-10 12:43:36 -050045 // Supported under non-AA only if it remains mitered
46 return aa == GrAA::kYes || *isMiter;
bsalomon8b7a9e12016-07-06 13:06:22 -070047 }
48 return false;
49}
50
Michael Ludwig72ab3462018-12-10 12:43:36 -050051
52///////////////////////////////////////////////////////////////////////////////////////////////////
53// Non-AA Stroking
54///////////////////////////////////////////////////////////////////////////////////////////////////
55
56/* create a triangle strip that strokes the specified rect. There are 8
57 unique vertices, but we repeat the last 2 to close up. Alternatively we
58 could use an indices array, and then only send 8 verts, but not sure that
59 would be faster.
60 */
61static void init_nonaa_stroke_rect_strip(SkPoint verts[10], const SkRect& rect, SkScalar width) {
62 const SkScalar rad = SkScalarHalf(width);
63
64 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
65 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
66 verts[2].set(rect.fRight - rad, rect.fTop + rad);
67 verts[3].set(rect.fRight + rad, rect.fTop - rad);
68 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
69 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
70 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
71 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
72 verts[8] = verts[0];
73 verts[9] = verts[1];
74
75 // TODO: we should be catching this higher up the call stack and just draw a single
76 // non-AA rect
77 if (2*rad >= rect.width()) {
78 verts[0].fX = verts[2].fX = verts[4].fX = verts[6].fX = verts[8].fX = rect.centerX();
79 }
80 if (2*rad >= rect.height()) {
81 verts[0].fY = verts[2].fY = verts[4].fY = verts[6].fY = verts[8].fY = rect.centerY();
82 }
83}
84
85class NonAAStrokeRectOp final : public GrMeshDrawOp {
86private:
87 using Helper = GrSimpleMeshDrawOpHelper;
88
89public:
90 DEFINE_OP_CLASS_ID
91
92 const char* name() const override { return "NonAAStrokeRectOp"; }
93
Chris Dalton1706cbf2019-05-21 19:35:29 -060094 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsd2f18732020-03-04 16:12:08 -050095 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -060096 fProgramInfo->visitFPProxies(func);
Robert Phillipsd2f18732020-03-04 16:12:08 -050097 } else {
98 fHelper.visitProxies(func);
99 }
Michael Ludwig72ab3462018-12-10 12:43:36 -0500100 }
101
John Stiles8d9bf642020-08-12 15:07:45 -0400102#if GR_TEST_UTILS
Michael Ludwig72ab3462018-12-10 12:43:36 -0500103 SkString dumpInfo() const override {
104 SkString string;
105 string.appendf(
106 "Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
107 "StrokeWidth: %.2f\n",
108 fColor.toBytes_RGBA(), fRect.fLeft, fRect.fTop, fRect.fRight, fRect.fBottom,
109 fStrokeWidth);
110 string += fHelper.dumpInfo();
111 string += INHERITED::dumpInfo();
112 return string;
113 }
114#endif
115
Robert Phillipsb97da532019-02-12 15:24:12 -0500116 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500117 GrPaint&& paint,
118 const SkMatrix& viewMatrix,
119 const SkRect& rect,
120 const SkStrokeRec& stroke,
121 GrAAType aaType) {
122 bool isMiter;
123 if (!allowed_stroke(stroke, GrAA::kNo, &isMiter)) {
124 return nullptr;
125 }
Chris Daltonbaa1b352019-04-03 12:03:00 -0600126 Helper::InputFlags inputFlags = Helper::InputFlags::kNone;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500127 // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of
128 // hairline rects. We jam all the vertices to pixel centers to avoid this, but not
129 // when MSAA is enabled because it can cause ugly artifacts.
130 if (stroke.getStyle() == SkStrokeRec::kHairline_Style && aaType != GrAAType::kMSAA) {
Chris Daltonbaa1b352019-04-03 12:03:00 -0600131 inputFlags |= Helper::InputFlags::kSnapVerticesToPixelCenters;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500132 }
Chris Daltonbaa1b352019-04-03 12:03:00 -0600133 return Helper::FactoryHelper<NonAAStrokeRectOp>(context, std::move(paint), inputFlags,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500134 viewMatrix, rect,
135 stroke, aaType);
136 }
137
138 NonAAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
Chris Daltonbaa1b352019-04-03 12:03:00 -0600139 Helper::InputFlags inputFlags, const SkMatrix& viewMatrix, const SkRect& rect,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500140 const SkStrokeRec& stroke, GrAAType aaType)
Robert Phillips4133dc42020-03-11 15:55:55 -0400141 : INHERITED(ClassID())
142 , fHelper(helperArgs, aaType, inputFlags) {
Michael Ludwig72ab3462018-12-10 12:43:36 -0500143 fColor = color;
144 fViewMatrix = viewMatrix;
145 fRect = rect;
146 // Sort the rect for hairlines
147 fRect.sort();
148 fStrokeWidth = stroke.getWidth();
149
Greg Daniel5faf4742019-10-01 15:14:44 -0400150 SkScalar rad = SkScalarHalf(fStrokeWidth);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500151 SkRect bounds = rect;
152 bounds.outset(rad, rad);
153
154 // If our caller snaps to pixel centers then we have to round out the bounds
Chris Daltonbaa1b352019-04-03 12:03:00 -0600155 if (inputFlags & Helper::InputFlags::kSnapVerticesToPixelCenters) {
Greg Daniel5faf4742019-10-01 15:14:44 -0400156 SkASSERT(!fStrokeWidth || aaType == GrAAType::kNone);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500157 viewMatrix.mapRect(&bounds);
158 // We want to be consistent with how we snap non-aa lines. To match what we do in
159 // GrGLSLVertexShaderBuilder, we first floor all the vertex values and then add half a
160 // pixel to force us to pixel centers.
Mike Reed92b33352019-08-24 19:39:13 -0400161 bounds.setLTRB(SkScalarFloorToScalar(bounds.fLeft),
162 SkScalarFloorToScalar(bounds.fTop),
163 SkScalarFloorToScalar(bounds.fRight),
164 SkScalarFloorToScalar(bounds.fBottom));
Michael Ludwig72ab3462018-12-10 12:43:36 -0500165 bounds.offset(0.5f, 0.5f);
Greg Daniel5faf4742019-10-01 15:14:44 -0400166 this->setBounds(bounds, HasAABloat::kNo, IsHairline::kNo);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500167 } else {
Greg Daniel5faf4742019-10-01 15:14:44 -0400168 HasAABloat aaBloat = (aaType == GrAAType::kNone) ? HasAABloat ::kNo : HasAABloat::kYes;
169 this->setTransformedBounds(bounds, fViewMatrix, aaBloat,
170 fStrokeWidth ? IsHairline::kNo : IsHairline::kYes);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500171 }
172 }
173
174 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
175
Chris Dalton6ce447a2019-06-23 18:07:38 -0600176 GrProcessorSet::Analysis finalize(
177 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
178 GrClampType clampType) override {
Brian Osman8fa7ab42019-03-18 10:22:42 -0400179 // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600180 return fHelper.finalizeProcessors(caps, clip, hasMixedSampledCoverage, clampType,
Brian Osman8fa7ab42019-03-18 10:22:42 -0400181 GrProcessorAnalysisCoverage::kNone, &fColor, nullptr);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500182 }
183
184private:
Robert Phillips2669a7b2020-03-12 12:07:19 -0400185 GrProgramInfo* programInfo() override { return fProgramInfo; }
186
Robert Phillips4133dc42020-03-11 15:55:55 -0400187 void onCreateProgramInfo(const GrCaps* caps,
188 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400189 const GrSurfaceProxyView* writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400190 GrAppliedClip&& clip,
191 const GrXferProcessor::DstProxyView& dstProxyView) override {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500192 GrGeometryProcessor* gp;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500193 {
194 using namespace GrDefaultGeoProcFactory;
195 Color color(fColor);
196 LocalCoords::Type localCoordsType = fHelper.usesLocalCoords()
197 ? LocalCoords::kUsePosition_Type
198 : LocalCoords::kUnused_Type;
Brian Osmanf0aee742020-03-12 09:28:44 -0400199 gp = GrDefaultGeoProcFactory::Make(arena, color, Coverage::kSolid_Type, localCoordsType,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500200 fViewMatrix);
201 }
202
Robert Phillips4133dc42020-03-11 15:55:55 -0400203 GrPrimitiveType primType = (fStrokeWidth > 0) ? GrPrimitiveType::kTriangleStrip
204 : GrPrimitiveType::kLineStrip;
Robert Phillipsd2f18732020-03-04 16:12:08 -0500205
Brian Salomon8afde5f2020-04-01 16:22:00 -0400206 fProgramInfo = fHelper.createProgramInfo(caps, arena, writeView, std::move(clip),
Robert Phillips4133dc42020-03-11 15:55:55 -0400207 dstProxyView, gp, primType);
Robert Phillipsd2f18732020-03-04 16:12:08 -0500208 }
209
Robert Phillipsd2f18732020-03-04 16:12:08 -0500210 void onPrepareDraws(Target* target) override {
211 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400212 this->createProgramInfo(target);
Robert Phillipsd2f18732020-03-04 16:12:08 -0500213 }
214
215 size_t kVertexStride = fProgramInfo->primProc().vertexStride();
Michael Ludwig72ab3462018-12-10 12:43:36 -0500216 int vertexCount = kVertsPerHairlineRect;
217 if (fStrokeWidth > 0) {
218 vertexCount = kVertsPerStrokeRect;
219 }
220
Brian Salomon12d22642019-01-29 14:38:50 -0500221 sk_sp<const GrBuffer> vertexBuffer;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500222 int firstVertex;
223
224 void* verts =
225 target->makeVertexSpace(kVertexStride, vertexCount, &vertexBuffer, &firstVertex);
226
227 if (!verts) {
228 SkDebugf("Could not allocate vertices\n");
229 return;
230 }
231
232 SkPoint* vertex = reinterpret_cast<SkPoint*>(verts);
233
Michael Ludwig72ab3462018-12-10 12:43:36 -0500234 if (fStrokeWidth > 0) {
Michael Ludwig72ab3462018-12-10 12:43:36 -0500235 init_nonaa_stroke_rect_strip(vertex, fRect, fStrokeWidth);
236 } else {
237 // hairline
Michael Ludwig72ab3462018-12-10 12:43:36 -0500238 vertex[0].set(fRect.fLeft, fRect.fTop);
239 vertex[1].set(fRect.fRight, fRect.fTop);
240 vertex[2].set(fRect.fRight, fRect.fBottom);
241 vertex[3].set(fRect.fLeft, fRect.fBottom);
242 vertex[4].set(fRect.fLeft, fRect.fTop);
243 }
244
Robert Phillipsd2f18732020-03-04 16:12:08 -0500245 fMesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600246 fMesh->set(std::move(vertexBuffer), vertexCount, firstVertex);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700247 }
248
249 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillipsd2f18732020-03-04 16:12:08 -0500250 if (!fMesh) {
251 return;
252 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500253
Chris Dalton765ed362020-03-16 17:34:44 -0600254 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
255 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
256 flushState->drawMesh(*fMesh);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500257 }
258
259 // TODO: override onCombineIfPossible
260
Robert Phillipsd2f18732020-03-04 16:12:08 -0500261 Helper fHelper;
262 SkPMColor4f fColor;
263 SkMatrix fViewMatrix;
264 SkRect fRect;
265 SkScalar fStrokeWidth;
Chris Daltoneb694b72020-03-16 09:25:50 -0600266 GrSimpleMesh* fMesh = nullptr;
Robert Phillipsd2f18732020-03-04 16:12:08 -0500267 GrProgramInfo* fProgramInfo = nullptr;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500268
269 const static int kVertsPerHairlineRect = 5;
270 const static int kVertsPerStrokeRect = 10;
271
272 typedef GrMeshDrawOp INHERITED;
273};
274
275///////////////////////////////////////////////////////////////////////////////////////////////////
276// AA Stroking
277///////////////////////////////////////////////////////////////////////////////////////////////////
278
279GR_DECLARE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
280GR_DECLARE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
281
282static void compute_aa_rects(SkRect* devOutside, SkRect* devOutsideAssist, SkRect* devInside,
283 bool* isDegenerate, const SkMatrix& viewMatrix, const SkRect& rect,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500284 SkScalar strokeWidth, bool miterStroke, SkVector* devHalfStrokeSize) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700285 SkRect devRect;
286 viewMatrix.mapRect(&devRect, rect);
287
288 SkVector devStrokeSize;
289 if (strokeWidth > 0) {
290 devStrokeSize.set(strokeWidth, strokeWidth);
291 viewMatrix.mapVectors(&devStrokeSize, 1);
292 devStrokeSize.setAbs(devStrokeSize);
293 } else {
294 devStrokeSize.set(SK_Scalar1, SK_Scalar1);
295 }
296
297 const SkScalar dx = devStrokeSize.fX;
298 const SkScalar dy = devStrokeSize.fY;
Mike Reed8be952a2017-02-13 20:44:33 -0500299 const SkScalar rx = SkScalarHalf(dx);
300 const SkScalar ry = SkScalarHalf(dy);
bsalomon8b7a9e12016-07-06 13:06:22 -0700301
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500302 devHalfStrokeSize->fX = rx;
303 devHalfStrokeSize->fY = ry;
304
bsalomon8b7a9e12016-07-06 13:06:22 -0700305 *devOutside = devRect;
306 *devOutsideAssist = devRect;
307 *devInside = devRect;
308
309 devOutside->outset(rx, ry);
310 devInside->inset(rx, ry);
311
312 // If we have a degenerate stroking rect(ie the stroke is larger than inner rect) then we
313 // make a degenerate inside rect to avoid double hitting. We will also jam all of the points
314 // together when we render these rects.
315 SkScalar spare;
316 {
317 SkScalar w = devRect.width() - dx;
318 SkScalar h = devRect.height() - dy;
Brian Osman788b9162020-02-07 10:36:46 -0500319 spare = std::min(w, h);
bsalomon8b7a9e12016-07-06 13:06:22 -0700320 }
321
322 *isDegenerate = spare <= 0;
323 if (*isDegenerate) {
324 devInside->fLeft = devInside->fRight = devRect.centerX();
325 devInside->fTop = devInside->fBottom = devRect.centerY();
326 }
327
328 // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist)
329 // to draw the outside of the octagon. Because there are 8 vertices on the outer
330 // edge, while vertex number of inner edge is 4, the same as miter-stroke.
331 if (!miterStroke) {
332 devOutside->inset(0, ry);
333 devOutsideAssist->outset(0, ry);
334 }
335}
336
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500337static GrGeometryProcessor* create_aa_stroke_rect_gp(SkArenaAlloc* arena,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500338 bool tweakAlphaForCoverage,
339 const SkMatrix& viewMatrix,
340 bool usesLocalCoords,
341 bool wideColor) {
joshualitt9ff64252015-08-10 09:03:51 -0700342 using namespace GrDefaultGeoProcFactory;
343
Brian Osman2a4c4df2018-12-20 14:06:54 -0500344 Coverage::Type coverageType =
345 tweakAlphaForCoverage ? Coverage::kSolid_Type : Coverage::kAttribute_Type;
Brian Salomon8c852be2017-01-04 10:44:42 -0500346 LocalCoords::Type localCoordsType =
Brian Osman2a4c4df2018-12-20 14:06:54 -0500347 usesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type;
348 Color::Type colorType =
349 wideColor ? Color::kPremulWideColorAttribute_Type: Color::kPremulGrColorAttribute_Type;
350
Brian Osmanf0aee742020-03-12 09:28:44 -0400351 return MakeForDeviceSpace(arena, colorType, coverageType, localCoordsType, viewMatrix);
joshualitt9ff64252015-08-10 09:03:51 -0700352}
353
Brian Salomonbaaf4392017-06-15 09:59:23 -0400354class AAStrokeRectOp final : public GrMeshDrawOp {
355private:
356 using Helper = GrSimpleMeshDrawOpHelper;
357
joshualitt3566d442015-09-18 07:12:55 -0700358public:
Brian Salomon25a88092016-12-01 09:36:50 -0500359 DEFINE_OP_CLASS_ID
joshualitt3566d442015-09-18 07:12:55 -0700360
Robert Phillipsb97da532019-02-12 15:24:12 -0500361 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400362 GrPaint&& paint,
363 const SkMatrix& viewMatrix,
364 const SkRect& devOutside,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500365 const SkRect& devInside,
366 const SkVector& devHalfStrokeSize) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400367 return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500368 devOutside, devInside, devHalfStrokeSize);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400369 }
370
Brian Osmancf860852018-10-31 14:04:39 -0400371 AAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500372 const SkMatrix& viewMatrix, const SkRect& devOutside, const SkRect& devInside,
373 const SkVector& devHalfStrokeSize)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400374 : INHERITED(ClassID())
375 , fHelper(helperArgs, GrAAType::kCoverage)
376 , fViewMatrix(viewMatrix) {
caryclarkd6562002016-07-27 12:02:07 -0700377 SkASSERT(!devOutside.isEmpty());
378 SkASSERT(!devInside.isEmpty());
joshualitt3566d442015-09-18 07:12:55 -0700379
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500380 fRects.emplace_back(RectInfo{color, devOutside, devOutside, devInside, devHalfStrokeSize, false});
Greg Daniel5faf4742019-10-01 15:14:44 -0400381 this->setBounds(devOutside, HasAABloat::kYes, IsHairline::kNo);
bsalomon8b7a9e12016-07-06 13:06:22 -0700382 fMiterStroke = true;
383 }
384
Robert Phillipsb97da532019-02-12 15:24:12 -0500385 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400386 GrPaint&& paint,
387 const SkMatrix& viewMatrix,
388 const SkRect& rect,
389 const SkStrokeRec& stroke) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700390 bool isMiter;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500391 if (!allowed_stroke(stroke, GrAA::kYes, &isMiter)) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700392 return nullptr;
393 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400394 return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix, rect,
395 stroke, isMiter);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400396 }
bsalomon8b7a9e12016-07-06 13:06:22 -0700397
Brian Osmancf860852018-10-31 14:04:39 -0400398 AAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
Brian Osman936fe7d2018-10-30 15:30:35 -0400399 const SkMatrix& viewMatrix, const SkRect& rect, const SkStrokeRec& stroke,
400 bool isMiter)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400401 : INHERITED(ClassID())
402 , fHelper(helperArgs, GrAAType::kCoverage)
403 , fViewMatrix(viewMatrix) {
404 fMiterStroke = isMiter;
405 RectInfo& info = fRects.push_back();
Michael Ludwig72ab3462018-12-10 12:43:36 -0500406 compute_aa_rects(&info.fDevOutside, &info.fDevOutsideAssist, &info.fDevInside,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500407 &info.fDegenerate, viewMatrix, rect, stroke.getWidth(), isMiter,
408 &info.fDevHalfStrokeSize);
Brian Salomon8c5bad32016-12-20 14:43:36 -0500409 info.fColor = color;
Brian Salomon510dd422017-03-16 12:15:22 -0400410 if (isMiter) {
Greg Daniel5faf4742019-10-01 15:14:44 -0400411 this->setBounds(info.fDevOutside, HasAABloat::kYes, IsHairline::kNo);
Brian Salomon510dd422017-03-16 12:15:22 -0400412 } else {
413 // The outer polygon of the bevel stroke is an octagon specified by the points of a
414 // pair of overlapping rectangles where one is wide and the other is narrow.
415 SkRect bounds = info.fDevOutside;
416 bounds.joinPossiblyEmptyRect(info.fDevOutsideAssist);
Greg Daniel5faf4742019-10-01 15:14:44 -0400417 this->setBounds(bounds, HasAABloat::kYes, IsHairline::kNo);
Brian Salomon510dd422017-03-16 12:15:22 -0400418 }
joshualitt3566d442015-09-18 07:12:55 -0700419 }
420
421 const char* name() const override { return "AAStrokeRect"; }
422
Chris Dalton1706cbf2019-05-21 19:35:29 -0600423 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsd2f18732020-03-04 16:12:08 -0500424 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600425 fProgramInfo->visitFPProxies(func);
Robert Phillipsd2f18732020-03-04 16:12:08 -0500426 } else {
427 fHelper.visitProxies(func);
428 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400429 }
430
John Stiles8d9bf642020-08-12 15:07:45 -0400431#if GR_TEST_UTILS
Brian Salomon7c3e7182016-12-01 09:35:30 -0500432 SkString dumpInfo() const override {
433 SkString string;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500434 for (const auto& info : fRects) {
Brian Salomon6a639042016-12-14 11:08:17 -0500435 string.appendf(
436 "Color: 0x%08x, ORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
437 "AssistORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
438 "IRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], Degen: %d",
Brian Osmancf860852018-10-31 14:04:39 -0400439 info.fColor.toBytes_RGBA(), info.fDevOutside.fLeft, info.fDevOutside.fTop,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500440 info.fDevOutside.fRight, info.fDevOutside.fBottom, info.fDevOutsideAssist.fLeft,
441 info.fDevOutsideAssist.fTop, info.fDevOutsideAssist.fRight,
442 info.fDevOutsideAssist.fBottom, info.fDevInside.fLeft, info.fDevInside.fTop,
443 info.fDevInside.fRight, info.fDevInside.fBottom, info.fDegenerate);
Brian Salomon7c3e7182016-12-01 09:35:30 -0500444 }
Brian Salomon82dfd3d2017-06-14 12:30:35 -0400445 string += fHelper.dumpInfo();
446 string += INHERITED::dumpInfo();
Brian Salomon7c3e7182016-12-01 09:35:30 -0500447 return string;
448 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500449#endif
Brian Salomon7c3e7182016-12-01 09:35:30 -0500450
Brian Salomonbaaf4392017-06-15 09:59:23 -0400451 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
Brian Salomona0485d92017-06-14 19:08:01 -0400452
Chris Dalton6ce447a2019-06-23 18:07:38 -0600453 GrProcessorSet::Analysis finalize(
454 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
455 GrClampType clampType) override {
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700456 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600457 caps, clip, hasMixedSampledCoverage, clampType,
458 GrProcessorAnalysisCoverage::kSingleChannel, &fRects.back().fColor, &fWideColor);
Brian Salomona0485d92017-06-14 19:08:01 -0400459 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400460
461private:
Robert Phillips2669a7b2020-03-12 12:07:19 -0400462 GrProgramInfo* programInfo() override { return fProgramInfo; }
463
Robert Phillips4133dc42020-03-11 15:55:55 -0400464 void onCreateProgramInfo(const GrCaps*,
465 SkArenaAlloc*,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400466 const GrSurfaceProxyView* writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400467 GrAppliedClip&&,
468 const GrXferProcessor::DstProxyView&) override;
Robert Phillipsd2f18732020-03-04 16:12:08 -0500469
Brian Salomon91326c32017-08-09 16:02:19 -0400470 void onPrepareDraws(Target*) override;
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700471 void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
joshualittaa37a962015-09-18 13:03:25 -0700472
joshualitt3566d442015-09-18 07:12:55 -0700473 static const int kMiterIndexCnt = 3 * 24;
474 static const int kMiterVertexCnt = 16;
475 static const int kNumMiterRectsInIndexBuffer = 256;
476
477 static const int kBevelIndexCnt = 48 + 36 + 24;
478 static const int kBevelVertexCnt = 24;
479 static const int kNumBevelRectsInIndexBuffer = 256;
480
Brian Salomondbf70722019-02-07 11:31:24 -0500481 static sk_sp<const GrGpuBuffer> GetIndexBuffer(GrResourceProvider*, bool miterStroke);
joshualitt3566d442015-09-18 07:12:55 -0700482
joshualittaa37a962015-09-18 13:03:25 -0700483 const SkMatrix& viewMatrix() const { return fViewMatrix; }
484 bool miterStroke() const { return fMiterStroke; }
joshualitt3566d442015-09-18 07:12:55 -0700485
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500486 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*, const GrCaps&) override;
joshualitt3566d442015-09-18 07:12:55 -0700487
Brian Osmancfec9d52018-11-20 11:39:15 -0500488 void generateAAStrokeRectGeometry(GrVertexWriter& vertices,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500489 const SkPMColor4f& color,
490 bool wideColor,
joshualitt3566d442015-09-18 07:12:55 -0700491 const SkRect& devOutside,
492 const SkRect& devOutsideAssist,
493 const SkRect& devInside,
494 bool miterStroke,
joshualitt11edad92015-09-22 10:32:28 -0700495 bool degenerate,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500496 bool tweakAlphaForCoverage,
497 const SkVector& devHalfStrokeSize) const;
joshualitt3566d442015-09-18 07:12:55 -0700498
bsalomon8b7a9e12016-07-06 13:06:22 -0700499 // TODO support AA rotated stroke rects by copying around view matrices
Brian Salomon8c5bad32016-12-20 14:43:36 -0500500 struct RectInfo {
Brian Osmancf860852018-10-31 14:04:39 -0400501 SkPMColor4f fColor;
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500502 SkRect fDevOutside;
503 SkRect fDevOutsideAssist;
504 SkRect fDevInside;
505 SkVector fDevHalfStrokeSize;
506 bool fDegenerate;
bsalomon8b7a9e12016-07-06 13:06:22 -0700507 };
508
Robert Phillipsd2f18732020-03-04 16:12:08 -0500509 Helper fHelper;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500510 SkSTArray<1, RectInfo, true> fRects;
Robert Phillipsd2f18732020-03-04 16:12:08 -0500511 SkMatrix fViewMatrix;
Chris Daltoneb694b72020-03-16 09:25:50 -0600512 GrSimpleMesh* fMesh = nullptr;
Robert Phillipsd2f18732020-03-04 16:12:08 -0500513 GrProgramInfo* fProgramInfo = nullptr;
514 bool fMiterStroke;
515 bool fWideColor;
joshualitt3566d442015-09-18 07:12:55 -0700516
Brian Salomonbaaf4392017-06-15 09:59:23 -0400517 typedef GrMeshDrawOp INHERITED;
joshualitt3566d442015-09-18 07:12:55 -0700518};
519
Robert Phillips4133dc42020-03-11 15:55:55 -0400520void AAStrokeRectOp::onCreateProgramInfo(const GrCaps* caps,
521 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400522 const GrSurfaceProxyView* writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400523 GrAppliedClip&& appliedClip,
524 const GrXferProcessor::DstProxyView& dstProxyView) {
Robert Phillipsd2f18732020-03-04 16:12:08 -0500525
526 GrGeometryProcessor* gp = create_aa_stroke_rect_gp(arena,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500527 fHelper.compatibleWithCoverageAsAlpha(),
528 this->viewMatrix(),
529 fHelper.usesLocalCoords(),
530 fWideColor);
joshualitt9ff64252015-08-10 09:03:51 -0700531 if (!gp) {
532 SkDebugf("Couldn't create GrGeometryProcessor\n");
Robert Phillips4133dc42020-03-11 15:55:55 -0400533 return;
Robert Phillipsd2f18732020-03-04 16:12:08 -0500534 }
535
Robert Phillips4133dc42020-03-11 15:55:55 -0400536 fProgramInfo = fHelper.createProgramInfo(caps,
537 arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400538 writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400539 std::move(appliedClip),
540 dstProxyView,
541 gp,
542 GrPrimitiveType::kTriangles);
Robert Phillipsd2f18732020-03-04 16:12:08 -0500543}
544
Robert Phillipsd2f18732020-03-04 16:12:08 -0500545void AAStrokeRectOp::onPrepareDraws(Target* target) {
546
547 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400548 this->createProgramInfo(target);
Robert Phillipsd2f18732020-03-04 16:12:08 -0500549 if (!fProgramInfo) {
550 return;
551 }
joshualitt9ff64252015-08-10 09:03:51 -0700552 }
553
joshualitt9ff64252015-08-10 09:03:51 -0700554 int innerVertexNum = 4;
555 int outerVertexNum = this->miterStroke() ? 4 : 8;
556 int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2;
557 int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500558 int instanceCount = fRects.count();
Robert Phillipsee08d522019-10-28 16:34:44 -0400559 int maxQuads = this->miterStroke() ? kNumMiterRectsInIndexBuffer : kNumBevelRectsInIndexBuffer;
joshualitt9ff64252015-08-10 09:03:51 -0700560
Brian Salomondbf70722019-02-07 11:31:24 -0500561 sk_sp<const GrGpuBuffer> indexBuffer =
Brian Salomon7eae3e02018-08-07 14:02:38 +0000562 GetIndexBuffer(target->resourceProvider(), this->miterStroke());
Brian Salomon12d22642019-01-29 14:38:50 -0500563 if (!indexBuffer) {
564 SkDebugf("Could not allocate indices\n");
565 return;
566 }
Robert Phillipsd2f18732020-03-04 16:12:08 -0500567 PatternHelper helper(target, GrPrimitiveType::kTriangles,
568 fProgramInfo->primProc().vertexStride(), std::move(indexBuffer),
569 verticesPerInstance, indicesPerInstance, instanceCount, maxQuads);
Brian Osmancfec9d52018-11-20 11:39:15 -0500570 GrVertexWriter vertices{ helper.vertices() };
Brian Salomon12d22642019-01-29 14:38:50 -0500571 if (!vertices.fPtr) {
Brian Salomon6a639042016-12-14 11:08:17 -0500572 SkDebugf("Could not allocate vertices\n");
573 return;
574 }
joshualitt9ff64252015-08-10 09:03:51 -0700575
576 for (int i = 0; i < instanceCount; i++) {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500577 const RectInfo& info = fRects[i];
joshualitt9ff64252015-08-10 09:03:51 -0700578 this->generateAAStrokeRectGeometry(vertices,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500579 info.fColor,
580 fWideColor,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500581 info.fDevOutside,
582 info.fDevOutsideAssist,
583 info.fDevInside,
joshualittaa37a962015-09-18 13:03:25 -0700584 fMiterStroke,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500585 info.fDegenerate,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500586 fHelper.compatibleWithCoverageAsAlpha(),
587 info.fDevHalfStrokeSize);
joshualitt9ff64252015-08-10 09:03:51 -0700588 }
Robert Phillipsd2f18732020-03-04 16:12:08 -0500589 fMesh = helper.mesh();
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700590}
591
592void AAStrokeRectOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillipsd2f18732020-03-04 16:12:08 -0500593 if (!fProgramInfo || !fMesh) {
594 return;
595 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500596
Chris Dalton765ed362020-03-16 17:34:44 -0600597 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
598 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
599 flushState->drawMesh(*fMesh);
joshualitt9ff64252015-08-10 09:03:51 -0700600}
601
Brian Salomondbf70722019-02-07 11:31:24 -0500602sk_sp<const GrGpuBuffer> AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider,
603 bool miterStroke) {
joshualitt9ff64252015-08-10 09:03:51 -0700604 if (miterStroke) {
Brian Salomon6a639042016-12-14 11:08:17 -0500605 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700606 static const uint16_t gMiterIndices[] = {
607 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0,
608 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0,
609 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0,
610 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0,
611
612 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4,
613 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4,
614 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4,
615 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4,
616
617 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8,
618 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8,
619 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8,
620 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8,
621 };
Brian Salomon6a639042016-12-14 11:08:17 -0500622 // clang-format on
Brian Salomon4dea72a2019-12-18 10:43:10 -0500623 static_assert(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt);
joshualitt9ff64252015-08-10 09:03:51 -0700624 GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400625 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500626 gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt,
627 gMiterIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700628 } else {
629 /**
630 * As in miter-stroke, index = a + b, and a is the current index, b is the shift
631 * from the first index. The index layout:
632 * outer AA line: 0~3, 4~7
633 * outer edge: 8~11, 12~15
634 * inner edge: 16~19
635 * inner AA line: 20~23
636 * Following comes a bevel-stroke rect and its indices:
637 *
638 * 4 7
639 * *********************************
640 * * ______________________________ *
641 * * / 12 15 \ *
642 * * / \ *
643 * 0 * |8 16_____________________19 11 | * 3
644 * * | | | | *
645 * * | | **************** | | *
646 * * | | * 20 23 * | | *
647 * * | | * * | | *
648 * * | | * 21 22 * | | *
649 * * | | **************** | | *
650 * * | |____________________| | *
651 * 1 * |9 17 18 10| * 2
652 * * \ / *
653 * * \13 __________________________14/ *
654 * * *
655 * **********************************
656 * 5 6
657 */
Brian Salomon6a639042016-12-14 11:08:17 -0500658 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700659 static const uint16_t gBevelIndices[] = {
660 // Draw outer AA, from outer AA line to outer edge, shift is 0.
661 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0,
662 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0,
663 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0,
664 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0,
665 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0,
666 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0,
667 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0,
668 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0,
669
670 // Draw the stroke, from outer edge to inner edge, shift is 8.
671 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8,
672 1 + 8, 5 + 8, 9 + 8,
673 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8,
674 6 + 8, 2 + 8, 10 + 8,
675 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8,
676 3 + 8, 7 + 8, 11 + 8,
677 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8,
678 4 + 8, 0 + 8, 8 + 8,
679
680 // Draw the inner AA, from inner edge to inner AA line, shift is 16.
681 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16,
682 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16,
683 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16,
684 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16,
685 };
Brian Salomon6a639042016-12-14 11:08:17 -0500686 // clang-format on
Brian Salomon4dea72a2019-12-18 10:43:10 -0500687 static_assert(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt);
joshualitt9ff64252015-08-10 09:03:51 -0700688
689 GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400690 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500691 gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt,
692 gBevelIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700693 }
694}
695
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500696GrOp::CombineResult AAStrokeRectOp::onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500697 const GrCaps& caps) {
Brian Salomon6a639042016-12-14 11:08:17 -0500698 AAStrokeRectOp* that = t->cast<AAStrokeRectOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700699
Brian Salomonbaaf4392017-06-15 09:59:23 -0400700 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000701 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700702 }
703
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500704 // TODO combine across miterstroke changes
joshualitt9ff64252015-08-10 09:03:51 -0700705 if (this->miterStroke() != that->miterStroke()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000706 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700707 }
708
709 // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses
Brian Salomonbaaf4392017-06-15 09:59:23 -0400710 // local coords then we won't be able to combine. TODO: Upload local coords as an attribute.
Mike Reed2c383152019-12-18 16:47:47 -0500711 if (fHelper.usesLocalCoords() &&
712 !SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix()))
713 {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000714 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700715 }
716
Brian Salomon8c5bad32016-12-20 14:43:36 -0500717 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
Brian Osman2a4c4df2018-12-20 14:06:54 -0500718 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000719 return CombineResult::kMerged;
joshualitt9ff64252015-08-10 09:03:51 -0700720}
721
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500722// Compute the coverage for the inner two rects.
723static float compute_inner_coverage(SkScalar maxDevHalfStrokeSize) {
724 if (maxDevHalfStrokeSize < SK_ScalarHalf) {
725 return 2.0f * maxDevHalfStrokeSize / (maxDevHalfStrokeSize + SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700726 }
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500727
728 return 1.0f;
joshualitt11edad92015-09-22 10:32:28 -0700729}
730
Brian Osmancfec9d52018-11-20 11:39:15 -0500731void AAStrokeRectOp::generateAAStrokeRectGeometry(GrVertexWriter& vertices,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500732 const SkPMColor4f& color,
733 bool wideColor,
Brian Salomon6a639042016-12-14 11:08:17 -0500734 const SkRect& devOutside,
735 const SkRect& devOutsideAssist,
736 const SkRect& devInside,
737 bool miterStroke,
738 bool degenerate,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500739 bool tweakAlphaForCoverage,
740 const SkVector& devHalfStrokeSize) const {
joshualitt9ff64252015-08-10 09:03:51 -0700741 // We create vertices for four nested rectangles. There are two ramps from 0 to full
742 // coverage, one on the exterior of the stroke and the other on the interior.
joshualitt9ff64252015-08-10 09:03:51 -0700743
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500744 // The following code only really works if either devStrokeSize's fX and fY are
745 // equal (in which case innerCoverage is same for all sides of the rects) or
746 // if devStrokeSize's fX and fY are both greater than 1.0 (in which case
747 // innerCoverage will always be 1). The most problematic case is when one of
748 // fX and fY is greater than 1.0 and the other is less than 1.0. In this case
749 // the smaller side should have a partial coverage but the larger side will
750 // force the coverage to be 1.0.
joshualitt9ff64252015-08-10 09:03:51 -0700751
Brian Osmancfec9d52018-11-20 11:39:15 -0500752 auto inset_fan = [](const SkRect& r, SkScalar dx, SkScalar dy) {
753 return GrVertexWriter::TriFanFromRect(r.makeInset(dx, dy));
754 };
joshualitt9ff64252015-08-10 09:03:51 -0700755
Brian Osmancfec9d52018-11-20 11:39:15 -0500756 auto maybe_coverage = [tweakAlphaForCoverage](float coverage) {
757 return GrVertexWriter::If(!tweakAlphaForCoverage, coverage);
758 };
759
Brian Osman2a4c4df2018-12-20 14:06:54 -0500760 GrVertexColor outerColor(tweakAlphaForCoverage ? SK_PMColor4fTRANSPARENT : color, wideColor);
Brian Osmancfec9d52018-11-20 11:39:15 -0500761
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500762 // For device-space stroke widths less than one we can't inset more than the original
763 // device space stroke width if we want to keep the sizing of all the rects correct.
Brian Osman788b9162020-02-07 10:36:46 -0500764 const SkScalar insetX = std::min(SK_ScalarHalf, devHalfStrokeSize.fX);
765 const SkScalar insetY = std::min(SK_ScalarHalf, devHalfStrokeSize.fY);
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500766
767 // But, correspondingly, we always want to keep the AA picture frame one pixel wide.
768 const SkScalar outsetX = SK_Scalar1 - insetX;
769 const SkScalar outsetY = SK_Scalar1 - insetY;
770
Brian Osmancfec9d52018-11-20 11:39:15 -0500771 // Outermost rect
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500772 vertices.writeQuad(inset_fan(devOutside, -outsetX, -outsetY),
Brian Osmancfec9d52018-11-20 11:39:15 -0500773 outerColor,
774 maybe_coverage(0.0f));
775
776 if (!miterStroke) {
777 // Second outermost
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500778 vertices.writeQuad(inset_fan(devOutsideAssist, -outsetX, -outsetY),
Brian Osmancfec9d52018-11-20 11:39:15 -0500779 outerColor,
780 maybe_coverage(0.0f));
joshualitt9ff64252015-08-10 09:03:51 -0700781 }
782
Brian Osman788b9162020-02-07 10:36:46 -0500783 float innerCoverage = compute_inner_coverage(std::max(devHalfStrokeSize.fX,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500784 devHalfStrokeSize.fY));
joshualitt9ff64252015-08-10 09:03:51 -0700785
Brian Osman2a4c4df2018-12-20 14:06:54 -0500786 SkPMColor4f scaledColor = color * innerCoverage;
787 GrVertexColor innerColor(tweakAlphaForCoverage ? scaledColor : color, wideColor);
joshualitt9ff64252015-08-10 09:03:51 -0700788
Brian Osmancfec9d52018-11-20 11:39:15 -0500789 // Inner rect
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500790 vertices.writeQuad(inset_fan(devOutside, insetX, insetY),
Brian Osmancfec9d52018-11-20 11:39:15 -0500791 innerColor,
792 maybe_coverage(innerCoverage));
793
794 if (!miterStroke) {
795 // Second inner
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500796 vertices.writeQuad(inset_fan(devOutsideAssist, insetX, insetY),
Brian Osmancfec9d52018-11-20 11:39:15 -0500797 innerColor,
798 maybe_coverage(innerCoverage));
joshualitt9ff64252015-08-10 09:03:51 -0700799 }
800
joshualitt11edad92015-09-22 10:32:28 -0700801 if (!degenerate) {
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500802 vertices.writeQuad(inset_fan(devInside, -insetX, -insetY),
Brian Osmancfec9d52018-11-20 11:39:15 -0500803 innerColor,
804 maybe_coverage(innerCoverage));
joshualitt11edad92015-09-22 10:32:28 -0700805
Brian Osmancfec9d52018-11-20 11:39:15 -0500806 // The innermost rect has 0 coverage...
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500807 vertices.writeQuad(inset_fan(devInside, outsetX, outsetY),
Brian Salomon747b3402019-09-16 17:25:01 -0400808 outerColor,
Brian Osmancfec9d52018-11-20 11:39:15 -0500809 maybe_coverage(0.0f));
810 } else {
811 // When the interior rect has become degenerate we smoosh to a single point
812 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
813
814 vertices.writeQuad(GrVertexWriter::TriFanFromRect(devInside),
815 innerColor,
816 maybe_coverage(innerCoverage));
817
818 // ... unless we are degenerate, in which case we must apply the scaled coverage
819 vertices.writeQuad(GrVertexWriter::TriFanFromRect(devInside),
820 innerColor,
821 maybe_coverage(innerCoverage));
joshualitt9ff64252015-08-10 09:03:51 -0700822 }
823}
824
Michael Ludwig72ab3462018-12-10 12:43:36 -0500825} // anonymous namespace
joshualitt3566d442015-09-18 07:12:55 -0700826
Michael Ludwig72ab3462018-12-10 12:43:36 -0500827namespace GrStrokeRectOp {
828
Robert Phillipsb97da532019-02-12 15:24:12 -0500829std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500830 GrPaint&& paint,
831 GrAAType aaType,
832 const SkMatrix& viewMatrix,
833 const SkRect& rect,
834 const SkStrokeRec& stroke) {
835 if (aaType == GrAAType::kCoverage) {
836 // The AA op only supports axis-aligned rectangles
837 if (!viewMatrix.rectStaysRect()) {
838 return nullptr;
839 }
840 return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke);
841 } else {
842 return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke, aaType);
843 }
844}
845
Robert Phillipsb97da532019-02-12 15:24:12 -0500846std::unique_ptr<GrDrawOp> MakeNested(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500847 GrPaint&& paint,
848 const SkMatrix& viewMatrix,
849 const SkRect rects[2]) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400850 SkASSERT(viewMatrix.rectStaysRect());
851 SkASSERT(!rects[0].isEmpty() && !rects[1].isEmpty());
852
853 SkRect devOutside, devInside;
854 viewMatrix.mapRect(&devOutside, rects[0]);
855 viewMatrix.mapRect(&devInside, rects[1]);
856 if (devInside.isEmpty()) {
857 if (devOutside.isEmpty()) {
858 return nullptr;
859 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500860 DrawQuad quad{GrQuad::MakeFromRect(rects[0], viewMatrix), GrQuad(rects[0]),
861 GrQuadAAFlags::kAll};
862 return GrFillRectOp::Make(context, std::move(paint), GrAAType::kCoverage, &quad);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400863 }
864
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500865 SkVector devHalfStrokeSize{ SkScalarHalf(devOutside.fRight - devInside.fRight),
866 SkScalarHalf(devOutside.fBottom - devInside.fBottom) };
867 return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, devOutside,
868 devInside, devHalfStrokeSize);
joshualittaa37a962015-09-18 13:03:25 -0700869}
870
Michael Ludwig72ab3462018-12-10 12:43:36 -0500871} // namespace GrStrokeRectOp
joshualitt9ff64252015-08-10 09:03:51 -0700872
Hal Canary6f6961e2017-01-31 13:50:44 -0500873#if GR_TEST_UTILS
joshualitt9ff64252015-08-10 09:03:51 -0700874
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500875#include "src/gpu/GrDrawOpTest.h"
joshualitt9ff64252015-08-10 09:03:51 -0700876
Michael Ludwig72ab3462018-12-10 12:43:36 -0500877GR_DRAW_OP_TEST_DEFINE(NonAAStrokeRectOp) {
878 SkMatrix viewMatrix = GrTest::TestMatrix(random);
879 SkRect rect = GrTest::TestRect(random);
880 SkScalar strokeWidth = random->nextBool() ? 0.0f : 2.0f;
881 SkPaint strokePaint;
882 strokePaint.setStrokeWidth(strokeWidth);
883 strokePaint.setStyle(SkPaint::kStroke_Style);
884 strokePaint.setStrokeJoin(SkPaint::kMiter_Join);
885 SkStrokeRec strokeRec(strokePaint);
886 GrAAType aaType = GrAAType::kNone;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600887 if (numSamples > 1) {
Michael Ludwig72ab3462018-12-10 12:43:36 -0500888 aaType = random->nextBool() ? GrAAType::kMSAA : GrAAType::kNone;
889 }
890 return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, strokeRec, aaType);
891}
892
Brian Salomonbaaf4392017-06-15 09:59:23 -0400893GR_DRAW_OP_TEST_DEFINE(AAStrokeRectOp) {
joshualitt9ff64252015-08-10 09:03:51 -0700894 bool miterStroke = random->nextBool();
895
bsalomon40ef4852016-05-02 13:22:13 -0700896 // Create either a empty rect or a non-empty rect.
Brian Salomon6a639042016-12-14 11:08:17 -0500897 SkRect rect =
898 random->nextBool() ? SkRect::MakeXYWH(10, 10, 50, 40) : SkRect::MakeXYWH(6, 7, 0, 0);
Brian Osman116b33e2020-02-05 13:34:09 -0500899 SkScalar minDim = std::min(rect.width(), rect.height());
bsalomon40ef4852016-05-02 13:22:13 -0700900 SkScalar strokeWidth = random->nextUScalar1() * minDim;
joshualitt9ff64252015-08-10 09:03:51 -0700901
bsalomon40ef4852016-05-02 13:22:13 -0700902 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
903 rec.setStrokeStyle(strokeWidth);
904 rec.setStrokeParams(SkPaint::kButt_Cap,
Brian Salomon6a639042016-12-14 11:08:17 -0500905 miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f);
bsalomon40ef4852016-05-02 13:22:13 -0700906 SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500907 return AAStrokeRectOp::Make(context, std::move(paint), matrix, rect, rec);
joshualitt9ff64252015-08-10 09:03:51 -0700908}
909
910#endif