blob: 57f28c5a3be6ec8db860f0b0e3e908160c541195 [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
Robert Phillips294723d2021-06-17 09:23:58 -040094 void visitProxies(const GrVisitProxyFunc& 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
Herb Derbyc76d4092020-10-07 16:46:15 -0400102 static GrOp::Owner Make(GrRecordingContext* context,
103 GrPaint&& paint,
104 const SkMatrix& viewMatrix,
105 const SkRect& rect,
106 const SkStrokeRec& stroke,
107 GrAAType aaType) {
Michael Ludwig72ab3462018-12-10 12:43:36 -0500108 bool isMiter;
109 if (!allowed_stroke(stroke, GrAA::kNo, &isMiter)) {
110 return nullptr;
111 }
Chris Daltonbaa1b352019-04-03 12:03:00 -0600112 Helper::InputFlags inputFlags = Helper::InputFlags::kNone;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500113 // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of
114 // hairline rects. We jam all the vertices to pixel centers to avoid this, but not
115 // when MSAA is enabled because it can cause ugly artifacts.
116 if (stroke.getStyle() == SkStrokeRec::kHairline_Style && aaType != GrAAType::kMSAA) {
Chris Daltonbaa1b352019-04-03 12:03:00 -0600117 inputFlags |= Helper::InputFlags::kSnapVerticesToPixelCenters;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500118 }
Chris Daltonbaa1b352019-04-03 12:03:00 -0600119 return Helper::FactoryHelper<NonAAStrokeRectOp>(context, std::move(paint), inputFlags,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500120 viewMatrix, rect,
121 stroke, aaType);
122 }
123
Herb Derbyc76d4092020-10-07 16:46:15 -0400124 NonAAStrokeRectOp(GrProcessorSet* processorSet, const SkPMColor4f& color,
Chris Daltonbaa1b352019-04-03 12:03:00 -0600125 Helper::InputFlags inputFlags, const SkMatrix& viewMatrix, const SkRect& rect,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500126 const SkStrokeRec& stroke, GrAAType aaType)
Robert Phillips4133dc42020-03-11 15:55:55 -0400127 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -0400128 , fHelper(processorSet, aaType, inputFlags) {
Michael Ludwig72ab3462018-12-10 12:43:36 -0500129 fColor = color;
130 fViewMatrix = viewMatrix;
131 fRect = rect;
132 // Sort the rect for hairlines
133 fRect.sort();
134 fStrokeWidth = stroke.getWidth();
135
Greg Daniel5faf4742019-10-01 15:14:44 -0400136 SkScalar rad = SkScalarHalf(fStrokeWidth);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500137 SkRect bounds = rect;
138 bounds.outset(rad, rad);
139
140 // If our caller snaps to pixel centers then we have to round out the bounds
Chris Daltonbaa1b352019-04-03 12:03:00 -0600141 if (inputFlags & Helper::InputFlags::kSnapVerticesToPixelCenters) {
Greg Daniel5faf4742019-10-01 15:14:44 -0400142 SkASSERT(!fStrokeWidth || aaType == GrAAType::kNone);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500143 viewMatrix.mapRect(&bounds);
144 // We want to be consistent with how we snap non-aa lines. To match what we do in
145 // GrGLSLVertexShaderBuilder, we first floor all the vertex values and then add half a
146 // pixel to force us to pixel centers.
Mike Reed92b33352019-08-24 19:39:13 -0400147 bounds.setLTRB(SkScalarFloorToScalar(bounds.fLeft),
148 SkScalarFloorToScalar(bounds.fTop),
149 SkScalarFloorToScalar(bounds.fRight),
150 SkScalarFloorToScalar(bounds.fBottom));
Michael Ludwig72ab3462018-12-10 12:43:36 -0500151 bounds.offset(0.5f, 0.5f);
Greg Daniel5faf4742019-10-01 15:14:44 -0400152 this->setBounds(bounds, HasAABloat::kNo, IsHairline::kNo);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500153 } else {
Greg Daniel5faf4742019-10-01 15:14:44 -0400154 HasAABloat aaBloat = (aaType == GrAAType::kNone) ? HasAABloat ::kNo : HasAABloat::kYes;
155 this->setTransformedBounds(bounds, fViewMatrix, aaBloat,
156 fStrokeWidth ? IsHairline::kNo : IsHairline::kYes);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500157 }
158 }
159
160 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
161
Chris Dalton57ab06c2021-04-22 12:57:28 -0600162 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
163 GrClampType clampType) override {
Brian Osman8fa7ab42019-03-18 10:22:42 -0400164 // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
Chris Dalton57ab06c2021-04-22 12:57:28 -0600165 return fHelper.finalizeProcessors(caps, clip, clampType, GrProcessorAnalysisCoverage::kNone,
166 &fColor, nullptr);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500167 }
168
169private:
Robert Phillips2669a7b2020-03-12 12:07:19 -0400170 GrProgramInfo* programInfo() override { return fProgramInfo; }
171
Robert Phillips4133dc42020-03-11 15:55:55 -0400172 void onCreateProgramInfo(const GrCaps* caps,
173 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500174 const GrSurfaceProxyView& writeView,
Chris Dalton6aaf00f2021-07-13 13:26:39 -0600175 bool usesMSAASurface,
Robert Phillips4133dc42020-03-11 15:55:55 -0400176 GrAppliedClip&& clip,
John Stiles52cb1d02021-06-02 11:58:05 -0400177 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500178 GrXferBarrierFlags renderPassXferBarriers,
179 GrLoadOp colorLoadOp) override {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500180 GrGeometryProcessor* gp;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500181 {
182 using namespace GrDefaultGeoProcFactory;
183 Color color(fColor);
184 LocalCoords::Type localCoordsType = fHelper.usesLocalCoords()
185 ? LocalCoords::kUsePosition_Type
186 : LocalCoords::kUnused_Type;
Brian Osmanf0aee742020-03-12 09:28:44 -0400187 gp = GrDefaultGeoProcFactory::Make(arena, color, Coverage::kSolid_Type, localCoordsType,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500188 fViewMatrix);
189 }
190
Robert Phillips4133dc42020-03-11 15:55:55 -0400191 GrPrimitiveType primType = (fStrokeWidth > 0) ? GrPrimitiveType::kTriangleStrip
192 : GrPrimitiveType::kLineStrip;
Robert Phillipsd2f18732020-03-04 16:12:08 -0500193
Brian Salomon8afde5f2020-04-01 16:22:00 -0400194 fProgramInfo = fHelper.createProgramInfo(caps, arena, writeView, std::move(clip),
Greg Danield358cbe2020-09-11 09:33:54 -0400195 dstProxyView, gp, primType,
Greg Daniel42dbca52020-11-20 10:22:43 -0500196 renderPassXferBarriers, colorLoadOp);
Robert Phillipsd2f18732020-03-04 16:12:08 -0500197 }
198
Robert Phillips71143952021-06-17 14:55:07 -0400199 void onPrepareDraws(GrMeshDrawTarget* target) override {
Robert Phillipsd2f18732020-03-04 16:12:08 -0500200 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400201 this->createProgramInfo(target);
Robert Phillipsd2f18732020-03-04 16:12:08 -0500202 }
203
Robert Phillips787fd9d2021-03-22 14:48:09 -0400204 size_t kVertexStride = fProgramInfo->geomProc().vertexStride();
Michael Ludwig72ab3462018-12-10 12:43:36 -0500205 int vertexCount = kVertsPerHairlineRect;
206 if (fStrokeWidth > 0) {
207 vertexCount = kVertsPerStrokeRect;
208 }
209
Brian Salomon12d22642019-01-29 14:38:50 -0500210 sk_sp<const GrBuffer> vertexBuffer;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500211 int firstVertex;
212
213 void* verts =
214 target->makeVertexSpace(kVertexStride, vertexCount, &vertexBuffer, &firstVertex);
215
216 if (!verts) {
217 SkDebugf("Could not allocate vertices\n");
218 return;
219 }
220
221 SkPoint* vertex = reinterpret_cast<SkPoint*>(verts);
222
Michael Ludwig72ab3462018-12-10 12:43:36 -0500223 if (fStrokeWidth > 0) {
Michael Ludwig72ab3462018-12-10 12:43:36 -0500224 init_nonaa_stroke_rect_strip(vertex, fRect, fStrokeWidth);
225 } else {
226 // hairline
Michael Ludwig72ab3462018-12-10 12:43:36 -0500227 vertex[0].set(fRect.fLeft, fRect.fTop);
228 vertex[1].set(fRect.fRight, fRect.fTop);
229 vertex[2].set(fRect.fRight, fRect.fBottom);
230 vertex[3].set(fRect.fLeft, fRect.fBottom);
231 vertex[4].set(fRect.fLeft, fRect.fTop);
232 }
233
Robert Phillipsd2f18732020-03-04 16:12:08 -0500234 fMesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600235 fMesh->set(std::move(vertexBuffer), vertexCount, firstVertex);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700236 }
237
238 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillipsd2f18732020-03-04 16:12:08 -0500239 if (!fMesh) {
240 return;
241 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500242
Chris Dalton765ed362020-03-16 17:34:44 -0600243 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
Robert Phillips787fd9d2021-03-22 14:48:09 -0400244 flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
Chris Dalton765ed362020-03-16 17:34:44 -0600245 flushState->drawMesh(*fMesh);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500246 }
247
John Stilesaf366522020-08-13 09:57:34 -0400248#if GR_TEST_UTILS
249 SkString onDumpInfo() const override {
250 return SkStringPrintf("Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
251 "StrokeWidth: %.2f\n%s",
252 fColor.toBytes_RGBA(), fRect.fLeft, fRect.fTop, fRect.fRight,
253 fRect.fBottom, fStrokeWidth, fHelper.dumpInfo().c_str());
254 }
255#endif
256
Michael Ludwig72ab3462018-12-10 12:43:36 -0500257 // TODO: override onCombineIfPossible
258
Robert Phillipsd2f18732020-03-04 16:12:08 -0500259 Helper fHelper;
260 SkPMColor4f fColor;
261 SkMatrix fViewMatrix;
262 SkRect fRect;
263 SkScalar fStrokeWidth;
Chris Daltoneb694b72020-03-16 09:25:50 -0600264 GrSimpleMesh* fMesh = nullptr;
Robert Phillipsd2f18732020-03-04 16:12:08 -0500265 GrProgramInfo* fProgramInfo = nullptr;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500266
267 const static int kVertsPerHairlineRect = 5;
268 const static int kVertsPerStrokeRect = 10;
269
John Stiles7571f9e2020-09-02 22:42:33 -0400270 using INHERITED = GrMeshDrawOp;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500271};
272
273///////////////////////////////////////////////////////////////////////////////////////////////////
274// AA Stroking
275///////////////////////////////////////////////////////////////////////////////////////////////////
276
277GR_DECLARE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
278GR_DECLARE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
279
Chris Dalton3ee0dc62021-07-14 01:36:49 -0600280static bool stroke_dev_half_size_supported(SkVector devHalfStrokeSize) {
281 // Since the horizontal and vertical strokes share internal corners, the coverage value at that
282 // corner needs to be equal for the horizontal and vertical strokes both.
283 //
284 // The inner coverage values will be equal if the horizontal and vertical stroke widths are
285 // equal (in which case innerCoverage is same for all sides of the rects) or if the horizontal
286 // and vertical stroke widths are both greater than 1 (in which case innerCoverage will always
287 // be 1).
288 return devHalfStrokeSize.fX == devHalfStrokeSize.fY ||
289 std::min(devHalfStrokeSize.fX, devHalfStrokeSize.fY) >= .5f;
290}
291
Chris Dalton4f489102021-07-14 14:44:28 -0600292static bool compute_aa_rects(const GrCaps& caps,
293 SkRect* devOutside,
294 SkRect* devOutsideAssist,
295 SkRect* devInside,
296 bool* isDegenerate,
297 const SkMatrix& viewMatrix,
298 const SkRect& rect,
299 SkScalar strokeWidth,
300 bool miterStroke,
301 SkVector* devHalfStrokeSize) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700302 SkVector devStrokeSize;
303 if (strokeWidth > 0) {
304 devStrokeSize.set(strokeWidth, strokeWidth);
305 viewMatrix.mapVectors(&devStrokeSize, 1);
306 devStrokeSize.setAbs(devStrokeSize);
307 } else {
308 devStrokeSize.set(SK_Scalar1, SK_Scalar1);
309 }
310
311 const SkScalar dx = devStrokeSize.fX;
312 const SkScalar dy = devStrokeSize.fY;
Mike Reed8be952a2017-02-13 20:44:33 -0500313 const SkScalar rx = SkScalarHalf(dx);
314 const SkScalar ry = SkScalarHalf(dy);
bsalomon8b7a9e12016-07-06 13:06:22 -0700315
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500316 devHalfStrokeSize->fX = rx;
317 devHalfStrokeSize->fY = ry;
318
Chris Dalton4f489102021-07-14 14:44:28 -0600319 SkRect devRect;
320 viewMatrix.mapRect(&devRect, rect);
321
322 // Clip our draw rect 1 full stroke width plus bloat outside the viewport. This avoids
323 // interpolation precision issues with very large coordinates.
324 const int m = caps.maxRenderTargetSize();
325 const SkRect visibilityBounds = SkRect::MakeWH(m, m).makeOutset(dx + 1, dy + 1);
326 if (!devRect.intersect(visibilityBounds)) {
327 return false;
328 }
329
bsalomon8b7a9e12016-07-06 13:06:22 -0700330 *devOutside = devRect;
331 *devOutsideAssist = devRect;
332 *devInside = devRect;
333
334 devOutside->outset(rx, ry);
335 devInside->inset(rx, ry);
336
337 // If we have a degenerate stroking rect(ie the stroke is larger than inner rect) then we
338 // make a degenerate inside rect to avoid double hitting. We will also jam all of the points
339 // together when we render these rects.
340 SkScalar spare;
341 {
342 SkScalar w = devRect.width() - dx;
343 SkScalar h = devRect.height() - dy;
Brian Osman788b9162020-02-07 10:36:46 -0500344 spare = std::min(w, h);
bsalomon8b7a9e12016-07-06 13:06:22 -0700345 }
346
347 *isDegenerate = spare <= 0;
348 if (*isDegenerate) {
349 devInside->fLeft = devInside->fRight = devRect.centerX();
350 devInside->fTop = devInside->fBottom = devRect.centerY();
351 }
352
353 // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist)
354 // to draw the outside of the octagon. Because there are 8 vertices on the outer
355 // edge, while vertex number of inner edge is 4, the same as miter-stroke.
356 if (!miterStroke) {
357 devOutside->inset(0, ry);
358 devOutsideAssist->outset(0, ry);
359 }
Chris Dalton4f489102021-07-14 14:44:28 -0600360
361 return true;
bsalomon8b7a9e12016-07-06 13:06:22 -0700362}
363
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500364static GrGeometryProcessor* create_aa_stroke_rect_gp(SkArenaAlloc* arena,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500365 bool tweakAlphaForCoverage,
366 const SkMatrix& viewMatrix,
367 bool usesLocalCoords,
368 bool wideColor) {
joshualitt9ff64252015-08-10 09:03:51 -0700369 using namespace GrDefaultGeoProcFactory;
370
Brian Osman2a4c4df2018-12-20 14:06:54 -0500371 Coverage::Type coverageType =
372 tweakAlphaForCoverage ? Coverage::kSolid_Type : Coverage::kAttribute_Type;
Brian Salomon8c852be2017-01-04 10:44:42 -0500373 LocalCoords::Type localCoordsType =
Brian Osman2a4c4df2018-12-20 14:06:54 -0500374 usesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type;
375 Color::Type colorType =
376 wideColor ? Color::kPremulWideColorAttribute_Type: Color::kPremulGrColorAttribute_Type;
377
Brian Osmanf0aee742020-03-12 09:28:44 -0400378 return MakeForDeviceSpace(arena, colorType, coverageType, localCoordsType, viewMatrix);
joshualitt9ff64252015-08-10 09:03:51 -0700379}
380
Brian Salomonbaaf4392017-06-15 09:59:23 -0400381class AAStrokeRectOp final : public GrMeshDrawOp {
382private:
383 using Helper = GrSimpleMeshDrawOpHelper;
384
joshualitt3566d442015-09-18 07:12:55 -0700385public:
Brian Salomon25a88092016-12-01 09:36:50 -0500386 DEFINE_OP_CLASS_ID
joshualitt3566d442015-09-18 07:12:55 -0700387
Chris Dalton3ee0dc62021-07-14 01:36:49 -0600388 // TODO support AA rotated stroke rects by copying around view matrices
389 struct RectInfo {
390 SkPMColor4f fColor;
391 SkRect fDevOutside;
392 SkRect fDevOutsideAssist;
393 SkRect fDevInside;
394 SkVector fDevHalfStrokeSize;
395 bool fDegenerate;
396 };
397
Herb Derbyc76d4092020-10-07 16:46:15 -0400398 static GrOp::Owner Make(GrRecordingContext* context,
399 GrPaint&& paint,
400 const SkMatrix& viewMatrix,
401 const SkRect& devOutside,
402 const SkRect& devInside,
403 const SkVector& devHalfStrokeSize) {
Chris Dalton3ee0dc62021-07-14 01:36:49 -0600404 if (!viewMatrix.rectStaysRect()) {
405 // The AA op only supports axis-aligned rectangles
406 return nullptr;
407 }
408 if (!stroke_dev_half_size_supported(devHalfStrokeSize)) {
409 return nullptr;
410 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400411 return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500412 devOutside, devInside, devHalfStrokeSize);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400413 }
414
Herb Derbyc76d4092020-10-07 16:46:15 -0400415 AAStrokeRectOp(GrProcessorSet* processorSet, const SkPMColor4f& color,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500416 const SkMatrix& viewMatrix, const SkRect& devOutside, const SkRect& devInside,
417 const SkVector& devHalfStrokeSize)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400418 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -0400419 , fHelper(processorSet, GrAAType::kCoverage)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400420 , fViewMatrix(viewMatrix) {
caryclarkd6562002016-07-27 12:02:07 -0700421 SkASSERT(!devOutside.isEmpty());
422 SkASSERT(!devInside.isEmpty());
joshualitt3566d442015-09-18 07:12:55 -0700423
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500424 fRects.emplace_back(RectInfo{color, devOutside, devOutside, devInside, devHalfStrokeSize, false});
Greg Daniel5faf4742019-10-01 15:14:44 -0400425 this->setBounds(devOutside, HasAABloat::kYes, IsHairline::kNo);
bsalomon8b7a9e12016-07-06 13:06:22 -0700426 fMiterStroke = true;
427 }
428
Herb Derbyc76d4092020-10-07 16:46:15 -0400429 static GrOp::Owner Make(GrRecordingContext* context,
430 GrPaint&& paint,
431 const SkMatrix& viewMatrix,
432 const SkRect& rect,
433 const SkStrokeRec& stroke) {
Chris Dalton3ee0dc62021-07-14 01:36:49 -0600434 if (!viewMatrix.rectStaysRect()) {
435 // The AA op only supports axis-aligned rectangles
436 return nullptr;
437 }
bsalomon8b7a9e12016-07-06 13:06:22 -0700438 bool isMiter;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500439 if (!allowed_stroke(stroke, GrAA::kYes, &isMiter)) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700440 return nullptr;
441 }
Chris Dalton3ee0dc62021-07-14 01:36:49 -0600442 RectInfo info;
Chris Dalton4f489102021-07-14 14:44:28 -0600443 if (!compute_aa_rects(*context->priv().caps(),
444 &info.fDevOutside,
445 &info.fDevOutsideAssist,
446 &info.fDevInside,
447 &info.fDegenerate,
448 viewMatrix,
449 rect,
450 stroke.getWidth(),
451 isMiter,
452 &info.fDevHalfStrokeSize)) {
453 return nullptr;
454 }
Chris Dalton3ee0dc62021-07-14 01:36:49 -0600455 if (!stroke_dev_half_size_supported(info.fDevHalfStrokeSize)) {
456 return nullptr;
457 }
458 return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix, info,
459 isMiter);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400460 }
bsalomon8b7a9e12016-07-06 13:06:22 -0700461
Herb Derbyc76d4092020-10-07 16:46:15 -0400462 AAStrokeRectOp(GrProcessorSet* processorSet, const SkPMColor4f& color,
Chris Dalton3ee0dc62021-07-14 01:36:49 -0600463 const SkMatrix& viewMatrix, const RectInfo& infoExceptColor, bool isMiter)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400464 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -0400465 , fHelper(processorSet, GrAAType::kCoverage)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400466 , fViewMatrix(viewMatrix) {
467 fMiterStroke = isMiter;
Chris Dalton3ee0dc62021-07-14 01:36:49 -0600468 RectInfo& info = fRects.push_back(infoExceptColor);
Brian Salomon8c5bad32016-12-20 14:43:36 -0500469 info.fColor = color;
Brian Salomon510dd422017-03-16 12:15:22 -0400470 if (isMiter) {
Greg Daniel5faf4742019-10-01 15:14:44 -0400471 this->setBounds(info.fDevOutside, HasAABloat::kYes, IsHairline::kNo);
Brian Salomon510dd422017-03-16 12:15:22 -0400472 } else {
473 // The outer polygon of the bevel stroke is an octagon specified by the points of a
474 // pair of overlapping rectangles where one is wide and the other is narrow.
475 SkRect bounds = info.fDevOutside;
476 bounds.joinPossiblyEmptyRect(info.fDevOutsideAssist);
Greg Daniel5faf4742019-10-01 15:14:44 -0400477 this->setBounds(bounds, HasAABloat::kYes, IsHairline::kNo);
Brian Salomon510dd422017-03-16 12:15:22 -0400478 }
joshualitt3566d442015-09-18 07:12:55 -0700479 }
480
481 const char* name() const override { return "AAStrokeRect"; }
482
Robert Phillips294723d2021-06-17 09:23:58 -0400483 void visitProxies(const GrVisitProxyFunc& func) const override {
Robert Phillipsd2f18732020-03-04 16:12:08 -0500484 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600485 fProgramInfo->visitFPProxies(func);
Robert Phillipsd2f18732020-03-04 16:12:08 -0500486 } else {
487 fHelper.visitProxies(func);
488 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400489 }
490
Brian Salomonbaaf4392017-06-15 09:59:23 -0400491 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
Brian Salomona0485d92017-06-14 19:08:01 -0400492
Chris Dalton57ab06c2021-04-22 12:57:28 -0600493 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
494 GrClampType clampType) override {
495 return fHelper.finalizeProcessors(caps, clip, clampType,
496 GrProcessorAnalysisCoverage::kSingleChannel,
497 &fRects.back().fColor, &fWideColor);
Brian Salomona0485d92017-06-14 19:08:01 -0400498 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400499
500private:
Robert Phillips2669a7b2020-03-12 12:07:19 -0400501 GrProgramInfo* programInfo() override { return fProgramInfo; }
502
Robert Phillips4133dc42020-03-11 15:55:55 -0400503 void onCreateProgramInfo(const GrCaps*,
504 SkArenaAlloc*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500505 const GrSurfaceProxyView& writeView,
Chris Dalton6aaf00f2021-07-13 13:26:39 -0600506 bool usesMSAASurface,
Robert Phillips4133dc42020-03-11 15:55:55 -0400507 GrAppliedClip&&,
John Stiles52cb1d02021-06-02 11:58:05 -0400508 const GrDstProxyView&,
Greg Daniel42dbca52020-11-20 10:22:43 -0500509 GrXferBarrierFlags renderPassXferBarriers,
510 GrLoadOp colorLoadOp) override;
Robert Phillipsd2f18732020-03-04 16:12:08 -0500511
Robert Phillips71143952021-06-17 14:55:07 -0400512 void onPrepareDraws(GrMeshDrawTarget*) override;
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700513 void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
joshualittaa37a962015-09-18 13:03:25 -0700514
John Stilesaf366522020-08-13 09:57:34 -0400515#if GR_TEST_UTILS
516 SkString onDumpInfo() const override {
517 SkString string;
518 for (const auto& info : fRects) {
519 string.appendf(
520 "Color: 0x%08x, ORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
521 "AssistORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
522 "IRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], Degen: %d",
523 info.fColor.toBytes_RGBA(), info.fDevOutside.fLeft, info.fDevOutside.fTop,
524 info.fDevOutside.fRight, info.fDevOutside.fBottom, info.fDevOutsideAssist.fLeft,
525 info.fDevOutsideAssist.fTop, info.fDevOutsideAssist.fRight,
526 info.fDevOutsideAssist.fBottom, info.fDevInside.fLeft, info.fDevInside.fTop,
527 info.fDevInside.fRight, info.fDevInside.fBottom, info.fDegenerate);
528 }
529 string += fHelper.dumpInfo();
530 return string;
531 }
532#endif
533
joshualitt3566d442015-09-18 07:12:55 -0700534 static const int kMiterIndexCnt = 3 * 24;
535 static const int kMiterVertexCnt = 16;
536 static const int kNumMiterRectsInIndexBuffer = 256;
537
538 static const int kBevelIndexCnt = 48 + 36 + 24;
539 static const int kBevelVertexCnt = 24;
540 static const int kNumBevelRectsInIndexBuffer = 256;
541
Brian Salomondbf70722019-02-07 11:31:24 -0500542 static sk_sp<const GrGpuBuffer> GetIndexBuffer(GrResourceProvider*, bool miterStroke);
joshualitt3566d442015-09-18 07:12:55 -0700543
joshualittaa37a962015-09-18 13:03:25 -0700544 const SkMatrix& viewMatrix() const { return fViewMatrix; }
545 bool miterStroke() const { return fMiterStroke; }
joshualitt3566d442015-09-18 07:12:55 -0700546
Herb Derbye25c3002020-10-27 15:57:27 -0400547 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps&) override;
joshualitt3566d442015-09-18 07:12:55 -0700548
Brian Osmancfec9d52018-11-20 11:39:15 -0500549 void generateAAStrokeRectGeometry(GrVertexWriter& vertices,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500550 const SkPMColor4f& color,
551 bool wideColor,
joshualitt3566d442015-09-18 07:12:55 -0700552 const SkRect& devOutside,
553 const SkRect& devOutsideAssist,
554 const SkRect& devInside,
555 bool miterStroke,
joshualitt11edad92015-09-22 10:32:28 -0700556 bool degenerate,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500557 bool tweakAlphaForCoverage,
558 const SkVector& devHalfStrokeSize) const;
joshualitt3566d442015-09-18 07:12:55 -0700559
Robert Phillipsd2f18732020-03-04 16:12:08 -0500560 Helper fHelper;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500561 SkSTArray<1, RectInfo, true> fRects;
Robert Phillipsd2f18732020-03-04 16:12:08 -0500562 SkMatrix fViewMatrix;
Chris Daltoneb694b72020-03-16 09:25:50 -0600563 GrSimpleMesh* fMesh = nullptr;
Robert Phillipsd2f18732020-03-04 16:12:08 -0500564 GrProgramInfo* fProgramInfo = nullptr;
565 bool fMiterStroke;
566 bool fWideColor;
joshualitt3566d442015-09-18 07:12:55 -0700567
John Stiles7571f9e2020-09-02 22:42:33 -0400568 using INHERITED = GrMeshDrawOp;
joshualitt3566d442015-09-18 07:12:55 -0700569};
570
Robert Phillips4133dc42020-03-11 15:55:55 -0400571void AAStrokeRectOp::onCreateProgramInfo(const GrCaps* caps,
572 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500573 const GrSurfaceProxyView& writeView,
Chris Dalton6aaf00f2021-07-13 13:26:39 -0600574 bool usesMSAASurface,
Robert Phillips4133dc42020-03-11 15:55:55 -0400575 GrAppliedClip&& appliedClip,
John Stiles52cb1d02021-06-02 11:58:05 -0400576 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500577 GrXferBarrierFlags renderPassXferBarriers,
578 GrLoadOp colorLoadOp) {
Robert Phillipsd2f18732020-03-04 16:12:08 -0500579
580 GrGeometryProcessor* gp = create_aa_stroke_rect_gp(arena,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500581 fHelper.compatibleWithCoverageAsAlpha(),
582 this->viewMatrix(),
583 fHelper.usesLocalCoords(),
584 fWideColor);
joshualitt9ff64252015-08-10 09:03:51 -0700585 if (!gp) {
586 SkDebugf("Couldn't create GrGeometryProcessor\n");
Robert Phillips4133dc42020-03-11 15:55:55 -0400587 return;
Robert Phillipsd2f18732020-03-04 16:12:08 -0500588 }
589
Robert Phillips4133dc42020-03-11 15:55:55 -0400590 fProgramInfo = fHelper.createProgramInfo(caps,
591 arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400592 writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400593 std::move(appliedClip),
594 dstProxyView,
595 gp,
Greg Danield358cbe2020-09-11 09:33:54 -0400596 GrPrimitiveType::kTriangles,
Greg Daniel42dbca52020-11-20 10:22:43 -0500597 renderPassXferBarriers,
598 colorLoadOp);
Robert Phillipsd2f18732020-03-04 16:12:08 -0500599}
600
Robert Phillips71143952021-06-17 14:55:07 -0400601void AAStrokeRectOp::onPrepareDraws(GrMeshDrawTarget* target) {
Robert Phillipsd2f18732020-03-04 16:12:08 -0500602
603 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400604 this->createProgramInfo(target);
Robert Phillipsd2f18732020-03-04 16:12:08 -0500605 if (!fProgramInfo) {
606 return;
607 }
joshualitt9ff64252015-08-10 09:03:51 -0700608 }
609
joshualitt9ff64252015-08-10 09:03:51 -0700610 int innerVertexNum = 4;
611 int outerVertexNum = this->miterStroke() ? 4 : 8;
612 int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2;
613 int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500614 int instanceCount = fRects.count();
Robert Phillipsee08d522019-10-28 16:34:44 -0400615 int maxQuads = this->miterStroke() ? kNumMiterRectsInIndexBuffer : kNumBevelRectsInIndexBuffer;
joshualitt9ff64252015-08-10 09:03:51 -0700616
Brian Salomondbf70722019-02-07 11:31:24 -0500617 sk_sp<const GrGpuBuffer> indexBuffer =
Brian Salomon7eae3e02018-08-07 14:02:38 +0000618 GetIndexBuffer(target->resourceProvider(), this->miterStroke());
Brian Salomon12d22642019-01-29 14:38:50 -0500619 if (!indexBuffer) {
620 SkDebugf("Could not allocate indices\n");
621 return;
622 }
Robert Phillipsd2f18732020-03-04 16:12:08 -0500623 PatternHelper helper(target, GrPrimitiveType::kTriangles,
Robert Phillips787fd9d2021-03-22 14:48:09 -0400624 fProgramInfo->geomProc().vertexStride(), std::move(indexBuffer),
Robert Phillipsd2f18732020-03-04 16:12:08 -0500625 verticesPerInstance, indicesPerInstance, instanceCount, maxQuads);
Brian Osmancfec9d52018-11-20 11:39:15 -0500626 GrVertexWriter vertices{ helper.vertices() };
Brian Salomon12d22642019-01-29 14:38:50 -0500627 if (!vertices.fPtr) {
Brian Salomon6a639042016-12-14 11:08:17 -0500628 SkDebugf("Could not allocate vertices\n");
629 return;
630 }
joshualitt9ff64252015-08-10 09:03:51 -0700631
632 for (int i = 0; i < instanceCount; i++) {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500633 const RectInfo& info = fRects[i];
joshualitt9ff64252015-08-10 09:03:51 -0700634 this->generateAAStrokeRectGeometry(vertices,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500635 info.fColor,
636 fWideColor,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500637 info.fDevOutside,
638 info.fDevOutsideAssist,
639 info.fDevInside,
joshualittaa37a962015-09-18 13:03:25 -0700640 fMiterStroke,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500641 info.fDegenerate,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500642 fHelper.compatibleWithCoverageAsAlpha(),
643 info.fDevHalfStrokeSize);
joshualitt9ff64252015-08-10 09:03:51 -0700644 }
Robert Phillipsd2f18732020-03-04 16:12:08 -0500645 fMesh = helper.mesh();
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700646}
647
648void AAStrokeRectOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillipsd2f18732020-03-04 16:12:08 -0500649 if (!fProgramInfo || !fMesh) {
650 return;
651 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500652
Chris Dalton765ed362020-03-16 17:34:44 -0600653 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
Robert Phillips787fd9d2021-03-22 14:48:09 -0400654 flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
Chris Dalton765ed362020-03-16 17:34:44 -0600655 flushState->drawMesh(*fMesh);
joshualitt9ff64252015-08-10 09:03:51 -0700656}
657
Brian Salomondbf70722019-02-07 11:31:24 -0500658sk_sp<const GrGpuBuffer> AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider,
659 bool miterStroke) {
joshualitt9ff64252015-08-10 09:03:51 -0700660 if (miterStroke) {
Brian Salomon6a639042016-12-14 11:08:17 -0500661 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700662 static const uint16_t gMiterIndices[] = {
663 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0,
664 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0,
665 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0,
666 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0,
667
668 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4,
669 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4,
670 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4,
671 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4,
672
673 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8,
674 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8,
675 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8,
676 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8,
677 };
Brian Salomon6a639042016-12-14 11:08:17 -0500678 // clang-format on
Brian Salomon4dea72a2019-12-18 10:43:10 -0500679 static_assert(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt);
joshualitt9ff64252015-08-10 09:03:51 -0700680 GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400681 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500682 gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt,
683 gMiterIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700684 } else {
685 /**
686 * As in miter-stroke, index = a + b, and a is the current index, b is the shift
687 * from the first index. The index layout:
688 * outer AA line: 0~3, 4~7
689 * outer edge: 8~11, 12~15
690 * inner edge: 16~19
691 * inner AA line: 20~23
692 * Following comes a bevel-stroke rect and its indices:
693 *
694 * 4 7
695 * *********************************
696 * * ______________________________ *
697 * * / 12 15 \ *
698 * * / \ *
699 * 0 * |8 16_____________________19 11 | * 3
700 * * | | | | *
701 * * | | **************** | | *
702 * * | | * 20 23 * | | *
703 * * | | * * | | *
704 * * | | * 21 22 * | | *
705 * * | | **************** | | *
706 * * | |____________________| | *
707 * 1 * |9 17 18 10| * 2
708 * * \ / *
709 * * \13 __________________________14/ *
710 * * *
711 * **********************************
712 * 5 6
713 */
Brian Salomon6a639042016-12-14 11:08:17 -0500714 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700715 static const uint16_t gBevelIndices[] = {
716 // Draw outer AA, from outer AA line to outer edge, shift is 0.
717 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0,
718 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0,
719 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0,
720 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0,
721 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0,
722 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0,
723 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0,
724 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0,
725
726 // Draw the stroke, from outer edge to inner edge, shift is 8.
727 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8,
728 1 + 8, 5 + 8, 9 + 8,
729 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8,
730 6 + 8, 2 + 8, 10 + 8,
731 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8,
732 3 + 8, 7 + 8, 11 + 8,
733 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8,
734 4 + 8, 0 + 8, 8 + 8,
735
736 // Draw the inner AA, from inner edge to inner AA line, shift is 16.
737 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16,
738 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16,
739 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16,
740 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16,
741 };
Brian Salomon6a639042016-12-14 11:08:17 -0500742 // clang-format on
Brian Salomon4dea72a2019-12-18 10:43:10 -0500743 static_assert(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt);
joshualitt9ff64252015-08-10 09:03:51 -0700744
745 GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400746 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500747 gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt,
748 gBevelIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700749 }
750}
751
Herb Derbye25c3002020-10-27 15:57:27 -0400752GrOp::CombineResult AAStrokeRectOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps)
753{
Brian Salomon6a639042016-12-14 11:08:17 -0500754 AAStrokeRectOp* that = t->cast<AAStrokeRectOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700755
Brian Salomonbaaf4392017-06-15 09:59:23 -0400756 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000757 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700758 }
759
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500760 // TODO combine across miterstroke changes
joshualitt9ff64252015-08-10 09:03:51 -0700761 if (this->miterStroke() != that->miterStroke()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000762 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700763 }
764
765 // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses
Brian Salomonbaaf4392017-06-15 09:59:23 -0400766 // local coords then we won't be able to combine. TODO: Upload local coords as an attribute.
Mike Reed2c383152019-12-18 16:47:47 -0500767 if (fHelper.usesLocalCoords() &&
768 !SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix()))
769 {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000770 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700771 }
772
Brian Salomon8c5bad32016-12-20 14:43:36 -0500773 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
Brian Osman2a4c4df2018-12-20 14:06:54 -0500774 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000775 return CombineResult::kMerged;
joshualitt9ff64252015-08-10 09:03:51 -0700776}
777
Brian Osmancfec9d52018-11-20 11:39:15 -0500778void AAStrokeRectOp::generateAAStrokeRectGeometry(GrVertexWriter& vertices,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500779 const SkPMColor4f& color,
780 bool wideColor,
Brian Salomon6a639042016-12-14 11:08:17 -0500781 const SkRect& devOutside,
782 const SkRect& devOutsideAssist,
783 const SkRect& devInside,
784 bool miterStroke,
785 bool degenerate,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500786 bool tweakAlphaForCoverage,
787 const SkVector& devHalfStrokeSize) const {
joshualitt9ff64252015-08-10 09:03:51 -0700788 // We create vertices for four nested rectangles. There are two ramps from 0 to full
789 // coverage, one on the exterior of the stroke and the other on the interior.
joshualitt9ff64252015-08-10 09:03:51 -0700790
Chris Dalton3ee0dc62021-07-14 01:36:49 -0600791 // The following code only works if either devStrokeSize's fX and fY are
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500792 // equal (in which case innerCoverage is same for all sides of the rects) or
793 // if devStrokeSize's fX and fY are both greater than 1.0 (in which case
Chris Dalton3ee0dc62021-07-14 01:36:49 -0600794 // innerCoverage will always be 1).
795 SkASSERT(stroke_dev_half_size_supported(devHalfStrokeSize));
joshualitt9ff64252015-08-10 09:03:51 -0700796
Brian Osmancfec9d52018-11-20 11:39:15 -0500797 auto inset_fan = [](const SkRect& r, SkScalar dx, SkScalar dy) {
798 return GrVertexWriter::TriFanFromRect(r.makeInset(dx, dy));
799 };
joshualitt9ff64252015-08-10 09:03:51 -0700800
Brian Osmancfec9d52018-11-20 11:39:15 -0500801 auto maybe_coverage = [tweakAlphaForCoverage](float coverage) {
802 return GrVertexWriter::If(!tweakAlphaForCoverage, coverage);
803 };
804
Chris Dalton3ee0dc62021-07-14 01:36:49 -0600805 // How much do we inset toward the inside of the strokes?
806 float inset = .5f;
807 float innerCoverage = 1;
808 if (inset > devHalfStrokeSize.fX) {
809 SkASSERT(devHalfStrokeSize.fX == devHalfStrokeSize.fY);
810 inset = devHalfStrokeSize.fX;
811 innerCoverage = 2 * inset / (inset + .5f);
812 }
813 GrVertexColor innerColor(tweakAlphaForCoverage ? color * innerCoverage : color, wideColor);
814
815 // How much do we outset away from the outside of the strokes?
816 // We always want to keep the AA picture frame one pixel wide.
817 float outset = 1 - inset;
818 float outerCoverage = 0;
Brian Osman2a4c4df2018-12-20 14:06:54 -0500819 GrVertexColor outerColor(tweakAlphaForCoverage ? SK_PMColor4fTRANSPARENT : color, wideColor);
Brian Osmancfec9d52018-11-20 11:39:15 -0500820
Chris Dalton3ee0dc62021-07-14 01:36:49 -0600821 // Exterior outset rect (away from stroke).
822 vertices.writeQuad(inset_fan(devOutside, -outset, -outset),
Brian Osmancfec9d52018-11-20 11:39:15 -0500823 outerColor,
Chris Dalton3ee0dc62021-07-14 01:36:49 -0600824 maybe_coverage(outerCoverage));
Brian Osmancfec9d52018-11-20 11:39:15 -0500825
826 if (!miterStroke) {
Chris Dalton3ee0dc62021-07-14 01:36:49 -0600827 // Second exterior outset.
828 vertices.writeQuad(inset_fan(devOutsideAssist, -outset, -outset),
Brian Osmancfec9d52018-11-20 11:39:15 -0500829 outerColor,
Chris Dalton3ee0dc62021-07-14 01:36:49 -0600830 maybe_coverage(outerCoverage));
joshualitt9ff64252015-08-10 09:03:51 -0700831 }
832
Chris Dalton3ee0dc62021-07-14 01:36:49 -0600833 // Exterior inset rect (toward stroke).
834 vertices.writeQuad(inset_fan(devOutside, inset, inset),
Brian Osmancfec9d52018-11-20 11:39:15 -0500835 innerColor,
836 maybe_coverage(innerCoverage));
837
838 if (!miterStroke) {
Chris Dalton3ee0dc62021-07-14 01:36:49 -0600839 // Second exterior inset.
840 vertices.writeQuad(inset_fan(devOutsideAssist, inset, inset),
Brian Osmancfec9d52018-11-20 11:39:15 -0500841 innerColor,
842 maybe_coverage(innerCoverage));
joshualitt9ff64252015-08-10 09:03:51 -0700843 }
844
joshualitt11edad92015-09-22 10:32:28 -0700845 if (!degenerate) {
Chris Dalton3ee0dc62021-07-14 01:36:49 -0600846 // Interior inset rect (toward stroke).
847 vertices.writeQuad(inset_fan(devInside, -inset, -inset),
Brian Osmancfec9d52018-11-20 11:39:15 -0500848 innerColor,
849 maybe_coverage(innerCoverage));
joshualitt11edad92015-09-22 10:32:28 -0700850
Chris Dalton3ee0dc62021-07-14 01:36:49 -0600851 // Interior outset rect (away from stroke, toward center of rect).
Chris Daltonbdc2cd02021-07-14 01:54:38 -0600852 SkRect interiorAABoundary = devInside.makeInset(outset, outset);
853 float interiorCoverage = outerCoverage;
854 float coverageBackset = 0; // Adds back coverage when the interior AA edges cross.
855 if (interiorAABoundary.fLeft > interiorAABoundary.fRight) {
856 coverageBackset = (interiorAABoundary.fLeft - interiorAABoundary.fRight) / (outset * 2);
857 interiorAABoundary.fLeft = interiorAABoundary.fRight = interiorAABoundary.centerX();
858 }
859 if (interiorAABoundary.fTop > interiorAABoundary.fBottom) {
860 coverageBackset = std::max(
861 (interiorAABoundary.fTop - interiorAABoundary.fBottom) / (outset * 2),
862 coverageBackset);
863 interiorAABoundary.fTop = interiorAABoundary.fBottom = interiorAABoundary.centerY();
864 }
865 if (coverageBackset > 0) {
866 // The interior edges crossed. Lerp back toward innerCoverage, which is what this op
867 // will draw in the degenerate case. This gives a smooth transition into the degenerate
868 // case.
869 interiorCoverage += interiorCoverage * (1 - coverageBackset) +
870 innerCoverage * coverageBackset;
871 }
872 GrVertexColor interiorColor(tweakAlphaForCoverage ? color * interiorCoverage : color,
873 wideColor);
874 vertices.writeQuad(GrVertexWriter::TriFanFromRect(interiorAABoundary),
875 interiorColor,
876 maybe_coverage(interiorCoverage));
Brian Osmancfec9d52018-11-20 11:39:15 -0500877 } else {
878 // When the interior rect has become degenerate we smoosh to a single point
879 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
880
881 vertices.writeQuad(GrVertexWriter::TriFanFromRect(devInside),
882 innerColor,
883 maybe_coverage(innerCoverage));
884
885 // ... unless we are degenerate, in which case we must apply the scaled coverage
886 vertices.writeQuad(GrVertexWriter::TriFanFromRect(devInside),
887 innerColor,
888 maybe_coverage(innerCoverage));
joshualitt9ff64252015-08-10 09:03:51 -0700889 }
890}
891
Michael Ludwig72ab3462018-12-10 12:43:36 -0500892} // anonymous namespace
joshualitt3566d442015-09-18 07:12:55 -0700893
Michael Ludwig72ab3462018-12-10 12:43:36 -0500894namespace GrStrokeRectOp {
895
Herb Derbyc76d4092020-10-07 16:46:15 -0400896GrOp::Owner Make(GrRecordingContext* context,
897 GrPaint&& paint,
898 GrAAType aaType,
899 const SkMatrix& viewMatrix,
900 const SkRect& rect,
901 const SkStrokeRec& stroke) {
Brian Salomonb18a9472021-06-23 10:03:07 -0400902 SkASSERT(!context->priv().caps()->reducedShaderMode());
Michael Ludwig72ab3462018-12-10 12:43:36 -0500903 if (aaType == GrAAType::kCoverage) {
Michael Ludwig72ab3462018-12-10 12:43:36 -0500904 return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke);
905 } else {
906 return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke, aaType);
907 }
908}
909
Herb Derbyc76d4092020-10-07 16:46:15 -0400910GrOp::Owner MakeNested(GrRecordingContext* context,
911 GrPaint&& paint,
912 const SkMatrix& viewMatrix,
913 const SkRect rects[2]) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400914 SkASSERT(viewMatrix.rectStaysRect());
915 SkASSERT(!rects[0].isEmpty() && !rects[1].isEmpty());
916
Chris Dalton4f489102021-07-14 14:44:28 -0600917 // Clips our draw rects 1 full pixel outside the viewport. This avoids interpolation precision
918 // issues with very large coordinates.
919 const int m = context->priv().caps()->maxRenderTargetSize();
920 const SkRect visibilityBounds = SkRect::MakeWH(m, m).makeOutset(1, 1);
921
922 SkRect devOutside;
Brian Salomonbaaf4392017-06-15 09:59:23 -0400923 viewMatrix.mapRect(&devOutside, rects[0]);
Chris Dalton4f489102021-07-14 14:44:28 -0600924 if (!devOutside.intersect(visibilityBounds)) {
925 return nullptr;
926 }
927
928 SkRect devInside;
Brian Salomonbaaf4392017-06-15 09:59:23 -0400929 viewMatrix.mapRect(&devInside, rects[1]);
Chris Dalton4f489102021-07-14 14:44:28 -0600930 if (devInside.isEmpty() || !devInside.intersect(visibilityBounds)) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400931 if (devOutside.isEmpty()) {
932 return nullptr;
933 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500934 DrawQuad quad{GrQuad::MakeFromRect(rects[0], viewMatrix), GrQuad(rects[0]),
935 GrQuadAAFlags::kAll};
936 return GrFillRectOp::Make(context, std::move(paint), GrAAType::kCoverage, &quad);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400937 }
938
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500939 SkVector devHalfStrokeSize{ SkScalarHalf(devOutside.fRight - devInside.fRight),
940 SkScalarHalf(devOutside.fBottom - devInside.fBottom) };
941 return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, devOutside,
942 devInside, devHalfStrokeSize);
joshualittaa37a962015-09-18 13:03:25 -0700943}
944
Michael Ludwig72ab3462018-12-10 12:43:36 -0500945} // namespace GrStrokeRectOp
joshualitt9ff64252015-08-10 09:03:51 -0700946
Hal Canary6f6961e2017-01-31 13:50:44 -0500947#if GR_TEST_UTILS
joshualitt9ff64252015-08-10 09:03:51 -0700948
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500949#include "src/gpu/GrDrawOpTest.h"
joshualitt9ff64252015-08-10 09:03:51 -0700950
Michael Ludwig72ab3462018-12-10 12:43:36 -0500951GR_DRAW_OP_TEST_DEFINE(NonAAStrokeRectOp) {
952 SkMatrix viewMatrix = GrTest::TestMatrix(random);
953 SkRect rect = GrTest::TestRect(random);
954 SkScalar strokeWidth = random->nextBool() ? 0.0f : 2.0f;
955 SkPaint strokePaint;
956 strokePaint.setStrokeWidth(strokeWidth);
957 strokePaint.setStyle(SkPaint::kStroke_Style);
958 strokePaint.setStrokeJoin(SkPaint::kMiter_Join);
959 SkStrokeRec strokeRec(strokePaint);
960 GrAAType aaType = GrAAType::kNone;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600961 if (numSamples > 1) {
Michael Ludwig72ab3462018-12-10 12:43:36 -0500962 aaType = random->nextBool() ? GrAAType::kMSAA : GrAAType::kNone;
963 }
964 return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, strokeRec, aaType);
965}
966
Brian Salomonbaaf4392017-06-15 09:59:23 -0400967GR_DRAW_OP_TEST_DEFINE(AAStrokeRectOp) {
joshualitt9ff64252015-08-10 09:03:51 -0700968 bool miterStroke = random->nextBool();
969
bsalomon40ef4852016-05-02 13:22:13 -0700970 // Create either a empty rect or a non-empty rect.
Brian Salomon6a639042016-12-14 11:08:17 -0500971 SkRect rect =
972 random->nextBool() ? SkRect::MakeXYWH(10, 10, 50, 40) : SkRect::MakeXYWH(6, 7, 0, 0);
Brian Osman116b33e2020-02-05 13:34:09 -0500973 SkScalar minDim = std::min(rect.width(), rect.height());
bsalomon40ef4852016-05-02 13:22:13 -0700974 SkScalar strokeWidth = random->nextUScalar1() * minDim;
joshualitt9ff64252015-08-10 09:03:51 -0700975
bsalomon40ef4852016-05-02 13:22:13 -0700976 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
977 rec.setStrokeStyle(strokeWidth);
978 rec.setStrokeParams(SkPaint::kButt_Cap,
Brian Salomon6a639042016-12-14 11:08:17 -0500979 miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f);
bsalomon40ef4852016-05-02 13:22:13 -0700980 SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500981 return AAStrokeRectOp::Make(context, std::move(paint), matrix, rect, rec);
joshualitt9ff64252015-08-10 09:03:51 -0700982}
983
984#endif