blob: cfad2bda46f54dc579cdd25431eba2f24faaf778 [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
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 Dalton6ce447a2019-06-23 18:07:38 -0600162 GrProcessorSet::Analysis finalize(
163 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
164 GrClampType clampType) override {
Brian Osman8fa7ab42019-03-18 10:22:42 -0400165 // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600166 return fHelper.finalizeProcessors(caps, clip, hasMixedSampledCoverage, clampType,
Brian Osman8fa7ab42019-03-18 10:22:42 -0400167 GrProcessorAnalysisCoverage::kNone, &fColor, nullptr);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500168 }
169
170private:
Robert Phillips2669a7b2020-03-12 12:07:19 -0400171 GrProgramInfo* programInfo() override { return fProgramInfo; }
172
Robert Phillips4133dc42020-03-11 15:55:55 -0400173 void onCreateProgramInfo(const GrCaps* caps,
174 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500175 const GrSurfaceProxyView& writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400176 GrAppliedClip&& clip,
Greg Danield358cbe2020-09-11 09:33:54 -0400177 const GrXferProcessor::DstProxyView& 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 Phillipsd2f18732020-03-04 16:12:08 -0500199 void onPrepareDraws(Target* target) override {
200 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
280static void compute_aa_rects(SkRect* devOutside, SkRect* devOutsideAssist, SkRect* devInside,
281 bool* isDegenerate, const SkMatrix& viewMatrix, const SkRect& rect,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500282 SkScalar strokeWidth, bool miterStroke, SkVector* devHalfStrokeSize) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700283 SkRect devRect;
284 viewMatrix.mapRect(&devRect, rect);
285
286 SkVector devStrokeSize;
287 if (strokeWidth > 0) {
288 devStrokeSize.set(strokeWidth, strokeWidth);
289 viewMatrix.mapVectors(&devStrokeSize, 1);
290 devStrokeSize.setAbs(devStrokeSize);
291 } else {
292 devStrokeSize.set(SK_Scalar1, SK_Scalar1);
293 }
294
295 const SkScalar dx = devStrokeSize.fX;
296 const SkScalar dy = devStrokeSize.fY;
Mike Reed8be952a2017-02-13 20:44:33 -0500297 const SkScalar rx = SkScalarHalf(dx);
298 const SkScalar ry = SkScalarHalf(dy);
bsalomon8b7a9e12016-07-06 13:06:22 -0700299
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500300 devHalfStrokeSize->fX = rx;
301 devHalfStrokeSize->fY = ry;
302
bsalomon8b7a9e12016-07-06 13:06:22 -0700303 *devOutside = devRect;
304 *devOutsideAssist = devRect;
305 *devInside = devRect;
306
307 devOutside->outset(rx, ry);
308 devInside->inset(rx, ry);
309
310 // If we have a degenerate stroking rect(ie the stroke is larger than inner rect) then we
311 // make a degenerate inside rect to avoid double hitting. We will also jam all of the points
312 // together when we render these rects.
313 SkScalar spare;
314 {
315 SkScalar w = devRect.width() - dx;
316 SkScalar h = devRect.height() - dy;
Brian Osman788b9162020-02-07 10:36:46 -0500317 spare = std::min(w, h);
bsalomon8b7a9e12016-07-06 13:06:22 -0700318 }
319
320 *isDegenerate = spare <= 0;
321 if (*isDegenerate) {
322 devInside->fLeft = devInside->fRight = devRect.centerX();
323 devInside->fTop = devInside->fBottom = devRect.centerY();
324 }
325
326 // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist)
327 // to draw the outside of the octagon. Because there are 8 vertices on the outer
328 // edge, while vertex number of inner edge is 4, the same as miter-stroke.
329 if (!miterStroke) {
330 devOutside->inset(0, ry);
331 devOutsideAssist->outset(0, ry);
332 }
333}
334
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500335static GrGeometryProcessor* create_aa_stroke_rect_gp(SkArenaAlloc* arena,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500336 bool tweakAlphaForCoverage,
337 const SkMatrix& viewMatrix,
338 bool usesLocalCoords,
339 bool wideColor) {
joshualitt9ff64252015-08-10 09:03:51 -0700340 using namespace GrDefaultGeoProcFactory;
341
Brian Osman2a4c4df2018-12-20 14:06:54 -0500342 Coverage::Type coverageType =
343 tweakAlphaForCoverage ? Coverage::kSolid_Type : Coverage::kAttribute_Type;
Brian Salomon8c852be2017-01-04 10:44:42 -0500344 LocalCoords::Type localCoordsType =
Brian Osman2a4c4df2018-12-20 14:06:54 -0500345 usesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type;
346 Color::Type colorType =
347 wideColor ? Color::kPremulWideColorAttribute_Type: Color::kPremulGrColorAttribute_Type;
348
Brian Osmanf0aee742020-03-12 09:28:44 -0400349 return MakeForDeviceSpace(arena, colorType, coverageType, localCoordsType, viewMatrix);
joshualitt9ff64252015-08-10 09:03:51 -0700350}
351
Brian Salomonbaaf4392017-06-15 09:59:23 -0400352class AAStrokeRectOp final : public GrMeshDrawOp {
353private:
354 using Helper = GrSimpleMeshDrawOpHelper;
355
joshualitt3566d442015-09-18 07:12:55 -0700356public:
Brian Salomon25a88092016-12-01 09:36:50 -0500357 DEFINE_OP_CLASS_ID
joshualitt3566d442015-09-18 07:12:55 -0700358
Herb Derbyc76d4092020-10-07 16:46:15 -0400359 static GrOp::Owner Make(GrRecordingContext* context,
360 GrPaint&& paint,
361 const SkMatrix& viewMatrix,
362 const SkRect& devOutside,
363 const SkRect& devInside,
364 const SkVector& devHalfStrokeSize) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400365 return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500366 devOutside, devInside, devHalfStrokeSize);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400367 }
368
Herb Derbyc76d4092020-10-07 16:46:15 -0400369 AAStrokeRectOp(GrProcessorSet* processorSet, const SkPMColor4f& color,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500370 const SkMatrix& viewMatrix, const SkRect& devOutside, const SkRect& devInside,
371 const SkVector& devHalfStrokeSize)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400372 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -0400373 , fHelper(processorSet, GrAAType::kCoverage)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400374 , fViewMatrix(viewMatrix) {
caryclarkd6562002016-07-27 12:02:07 -0700375 SkASSERT(!devOutside.isEmpty());
376 SkASSERT(!devInside.isEmpty());
joshualitt3566d442015-09-18 07:12:55 -0700377
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500378 fRects.emplace_back(RectInfo{color, devOutside, devOutside, devInside, devHalfStrokeSize, false});
Greg Daniel5faf4742019-10-01 15:14:44 -0400379 this->setBounds(devOutside, HasAABloat::kYes, IsHairline::kNo);
bsalomon8b7a9e12016-07-06 13:06:22 -0700380 fMiterStroke = true;
381 }
382
Herb Derbyc76d4092020-10-07 16:46:15 -0400383 static GrOp::Owner Make(GrRecordingContext* context,
384 GrPaint&& paint,
385 const SkMatrix& viewMatrix,
386 const SkRect& rect,
387 const SkStrokeRec& stroke) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700388 bool isMiter;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500389 if (!allowed_stroke(stroke, GrAA::kYes, &isMiter)) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700390 return nullptr;
391 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400392 return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix, rect,
393 stroke, isMiter);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400394 }
bsalomon8b7a9e12016-07-06 13:06:22 -0700395
Herb Derbyc76d4092020-10-07 16:46:15 -0400396 AAStrokeRectOp(GrProcessorSet* processorSet, const SkPMColor4f& color,
Brian Osman936fe7d2018-10-30 15:30:35 -0400397 const SkMatrix& viewMatrix, const SkRect& rect, const SkStrokeRec& stroke,
398 bool isMiter)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400399 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -0400400 , fHelper(processorSet, GrAAType::kCoverage)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400401 , fViewMatrix(viewMatrix) {
402 fMiterStroke = isMiter;
403 RectInfo& info = fRects.push_back();
Michael Ludwig72ab3462018-12-10 12:43:36 -0500404 compute_aa_rects(&info.fDevOutside, &info.fDevOutsideAssist, &info.fDevInside,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500405 &info.fDegenerate, viewMatrix, rect, stroke.getWidth(), isMiter,
406 &info.fDevHalfStrokeSize);
Brian Salomon8c5bad32016-12-20 14:43:36 -0500407 info.fColor = color;
Brian Salomon510dd422017-03-16 12:15:22 -0400408 if (isMiter) {
Greg Daniel5faf4742019-10-01 15:14:44 -0400409 this->setBounds(info.fDevOutside, HasAABloat::kYes, IsHairline::kNo);
Brian Salomon510dd422017-03-16 12:15:22 -0400410 } else {
411 // The outer polygon of the bevel stroke is an octagon specified by the points of a
412 // pair of overlapping rectangles where one is wide and the other is narrow.
413 SkRect bounds = info.fDevOutside;
414 bounds.joinPossiblyEmptyRect(info.fDevOutsideAssist);
Greg Daniel5faf4742019-10-01 15:14:44 -0400415 this->setBounds(bounds, HasAABloat::kYes, IsHairline::kNo);
Brian Salomon510dd422017-03-16 12:15:22 -0400416 }
joshualitt3566d442015-09-18 07:12:55 -0700417 }
418
419 const char* name() const override { return "AAStrokeRect"; }
420
Chris Dalton1706cbf2019-05-21 19:35:29 -0600421 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsd2f18732020-03-04 16:12:08 -0500422 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600423 fProgramInfo->visitFPProxies(func);
Robert Phillipsd2f18732020-03-04 16:12:08 -0500424 } else {
425 fHelper.visitProxies(func);
426 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400427 }
428
Brian Salomonbaaf4392017-06-15 09:59:23 -0400429 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
Brian Salomona0485d92017-06-14 19:08:01 -0400430
Chris Dalton6ce447a2019-06-23 18:07:38 -0600431 GrProcessorSet::Analysis finalize(
432 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
433 GrClampType clampType) override {
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700434 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600435 caps, clip, hasMixedSampledCoverage, clampType,
436 GrProcessorAnalysisCoverage::kSingleChannel, &fRects.back().fColor, &fWideColor);
Brian Salomona0485d92017-06-14 19:08:01 -0400437 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400438
439private:
Robert Phillips2669a7b2020-03-12 12:07:19 -0400440 GrProgramInfo* programInfo() override { return fProgramInfo; }
441
Robert Phillips4133dc42020-03-11 15:55:55 -0400442 void onCreateProgramInfo(const GrCaps*,
443 SkArenaAlloc*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500444 const GrSurfaceProxyView& writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400445 GrAppliedClip&&,
Greg Danield358cbe2020-09-11 09:33:54 -0400446 const GrXferProcessor::DstProxyView&,
Greg Daniel42dbca52020-11-20 10:22:43 -0500447 GrXferBarrierFlags renderPassXferBarriers,
448 GrLoadOp colorLoadOp) override;
Robert Phillipsd2f18732020-03-04 16:12:08 -0500449
Brian Salomon91326c32017-08-09 16:02:19 -0400450 void onPrepareDraws(Target*) override;
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700451 void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
joshualittaa37a962015-09-18 13:03:25 -0700452
John Stilesaf366522020-08-13 09:57:34 -0400453#if GR_TEST_UTILS
454 SkString onDumpInfo() const override {
455 SkString string;
456 for (const auto& info : fRects) {
457 string.appendf(
458 "Color: 0x%08x, ORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
459 "AssistORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
460 "IRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], Degen: %d",
461 info.fColor.toBytes_RGBA(), info.fDevOutside.fLeft, info.fDevOutside.fTop,
462 info.fDevOutside.fRight, info.fDevOutside.fBottom, info.fDevOutsideAssist.fLeft,
463 info.fDevOutsideAssist.fTop, info.fDevOutsideAssist.fRight,
464 info.fDevOutsideAssist.fBottom, info.fDevInside.fLeft, info.fDevInside.fTop,
465 info.fDevInside.fRight, info.fDevInside.fBottom, info.fDegenerate);
466 }
467 string += fHelper.dumpInfo();
468 return string;
469 }
470#endif
471
joshualitt3566d442015-09-18 07:12:55 -0700472 static const int kMiterIndexCnt = 3 * 24;
473 static const int kMiterVertexCnt = 16;
474 static const int kNumMiterRectsInIndexBuffer = 256;
475
476 static const int kBevelIndexCnt = 48 + 36 + 24;
477 static const int kBevelVertexCnt = 24;
478 static const int kNumBevelRectsInIndexBuffer = 256;
479
Brian Salomondbf70722019-02-07 11:31:24 -0500480 static sk_sp<const GrGpuBuffer> GetIndexBuffer(GrResourceProvider*, bool miterStroke);
joshualitt3566d442015-09-18 07:12:55 -0700481
joshualittaa37a962015-09-18 13:03:25 -0700482 const SkMatrix& viewMatrix() const { return fViewMatrix; }
483 bool miterStroke() const { return fMiterStroke; }
joshualitt3566d442015-09-18 07:12:55 -0700484
Herb Derbye25c3002020-10-27 15:57:27 -0400485 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps&) override;
joshualitt3566d442015-09-18 07:12:55 -0700486
Brian Osmancfec9d52018-11-20 11:39:15 -0500487 void generateAAStrokeRectGeometry(GrVertexWriter& vertices,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500488 const SkPMColor4f& color,
489 bool wideColor,
joshualitt3566d442015-09-18 07:12:55 -0700490 const SkRect& devOutside,
491 const SkRect& devOutsideAssist,
492 const SkRect& devInside,
493 bool miterStroke,
joshualitt11edad92015-09-22 10:32:28 -0700494 bool degenerate,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500495 bool tweakAlphaForCoverage,
496 const SkVector& devHalfStrokeSize) const;
joshualitt3566d442015-09-18 07:12:55 -0700497
bsalomon8b7a9e12016-07-06 13:06:22 -0700498 // TODO support AA rotated stroke rects by copying around view matrices
Brian Salomon8c5bad32016-12-20 14:43:36 -0500499 struct RectInfo {
Brian Osmancf860852018-10-31 14:04:39 -0400500 SkPMColor4f fColor;
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500501 SkRect fDevOutside;
502 SkRect fDevOutsideAssist;
503 SkRect fDevInside;
504 SkVector fDevHalfStrokeSize;
505 bool fDegenerate;
bsalomon8b7a9e12016-07-06 13:06:22 -0700506 };
507
Robert Phillipsd2f18732020-03-04 16:12:08 -0500508 Helper fHelper;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500509 SkSTArray<1, RectInfo, true> fRects;
Robert Phillipsd2f18732020-03-04 16:12:08 -0500510 SkMatrix fViewMatrix;
Chris Daltoneb694b72020-03-16 09:25:50 -0600511 GrSimpleMesh* fMesh = nullptr;
Robert Phillipsd2f18732020-03-04 16:12:08 -0500512 GrProgramInfo* fProgramInfo = nullptr;
513 bool fMiterStroke;
514 bool fWideColor;
joshualitt3566d442015-09-18 07:12:55 -0700515
John Stiles7571f9e2020-09-02 22:42:33 -0400516 using INHERITED = GrMeshDrawOp;
joshualitt3566d442015-09-18 07:12:55 -0700517};
518
Robert Phillips4133dc42020-03-11 15:55:55 -0400519void AAStrokeRectOp::onCreateProgramInfo(const GrCaps* caps,
520 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500521 const GrSurfaceProxyView& writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400522 GrAppliedClip&& appliedClip,
Greg Danield358cbe2020-09-11 09:33:54 -0400523 const GrXferProcessor::DstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500524 GrXferBarrierFlags renderPassXferBarriers,
525 GrLoadOp colorLoadOp) {
Robert Phillipsd2f18732020-03-04 16:12:08 -0500526
527 GrGeometryProcessor* gp = create_aa_stroke_rect_gp(arena,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500528 fHelper.compatibleWithCoverageAsAlpha(),
529 this->viewMatrix(),
530 fHelper.usesLocalCoords(),
531 fWideColor);
joshualitt9ff64252015-08-10 09:03:51 -0700532 if (!gp) {
533 SkDebugf("Couldn't create GrGeometryProcessor\n");
Robert Phillips4133dc42020-03-11 15:55:55 -0400534 return;
Robert Phillipsd2f18732020-03-04 16:12:08 -0500535 }
536
Robert Phillips4133dc42020-03-11 15:55:55 -0400537 fProgramInfo = fHelper.createProgramInfo(caps,
538 arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400539 writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400540 std::move(appliedClip),
541 dstProxyView,
542 gp,
Greg Danield358cbe2020-09-11 09:33:54 -0400543 GrPrimitiveType::kTriangles,
Greg Daniel42dbca52020-11-20 10:22:43 -0500544 renderPassXferBarriers,
545 colorLoadOp);
Robert Phillipsd2f18732020-03-04 16:12:08 -0500546}
547
Robert Phillipsd2f18732020-03-04 16:12:08 -0500548void AAStrokeRectOp::onPrepareDraws(Target* target) {
549
550 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400551 this->createProgramInfo(target);
Robert Phillipsd2f18732020-03-04 16:12:08 -0500552 if (!fProgramInfo) {
553 return;
554 }
joshualitt9ff64252015-08-10 09:03:51 -0700555 }
556
joshualitt9ff64252015-08-10 09:03:51 -0700557 int innerVertexNum = 4;
558 int outerVertexNum = this->miterStroke() ? 4 : 8;
559 int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2;
560 int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500561 int instanceCount = fRects.count();
Robert Phillipsee08d522019-10-28 16:34:44 -0400562 int maxQuads = this->miterStroke() ? kNumMiterRectsInIndexBuffer : kNumBevelRectsInIndexBuffer;
joshualitt9ff64252015-08-10 09:03:51 -0700563
Brian Salomondbf70722019-02-07 11:31:24 -0500564 sk_sp<const GrGpuBuffer> indexBuffer =
Brian Salomon7eae3e02018-08-07 14:02:38 +0000565 GetIndexBuffer(target->resourceProvider(), this->miterStroke());
Brian Salomon12d22642019-01-29 14:38:50 -0500566 if (!indexBuffer) {
567 SkDebugf("Could not allocate indices\n");
568 return;
569 }
Robert Phillipsd2f18732020-03-04 16:12:08 -0500570 PatternHelper helper(target, GrPrimitiveType::kTriangles,
Robert Phillips787fd9d2021-03-22 14:48:09 -0400571 fProgramInfo->geomProc().vertexStride(), std::move(indexBuffer),
Robert Phillipsd2f18732020-03-04 16:12:08 -0500572 verticesPerInstance, indicesPerInstance, instanceCount, maxQuads);
Brian Osmancfec9d52018-11-20 11:39:15 -0500573 GrVertexWriter vertices{ helper.vertices() };
Brian Salomon12d22642019-01-29 14:38:50 -0500574 if (!vertices.fPtr) {
Brian Salomon6a639042016-12-14 11:08:17 -0500575 SkDebugf("Could not allocate vertices\n");
576 return;
577 }
joshualitt9ff64252015-08-10 09:03:51 -0700578
579 for (int i = 0; i < instanceCount; i++) {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500580 const RectInfo& info = fRects[i];
joshualitt9ff64252015-08-10 09:03:51 -0700581 this->generateAAStrokeRectGeometry(vertices,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500582 info.fColor,
583 fWideColor,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500584 info.fDevOutside,
585 info.fDevOutsideAssist,
586 info.fDevInside,
joshualittaa37a962015-09-18 13:03:25 -0700587 fMiterStroke,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500588 info.fDegenerate,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500589 fHelper.compatibleWithCoverageAsAlpha(),
590 info.fDevHalfStrokeSize);
joshualitt9ff64252015-08-10 09:03:51 -0700591 }
Robert Phillipsd2f18732020-03-04 16:12:08 -0500592 fMesh = helper.mesh();
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700593}
594
595void AAStrokeRectOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillipsd2f18732020-03-04 16:12:08 -0500596 if (!fProgramInfo || !fMesh) {
597 return;
598 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500599
Chris Dalton765ed362020-03-16 17:34:44 -0600600 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
Robert Phillips787fd9d2021-03-22 14:48:09 -0400601 flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
Chris Dalton765ed362020-03-16 17:34:44 -0600602 flushState->drawMesh(*fMesh);
joshualitt9ff64252015-08-10 09:03:51 -0700603}
604
Brian Salomondbf70722019-02-07 11:31:24 -0500605sk_sp<const GrGpuBuffer> AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider,
606 bool miterStroke) {
joshualitt9ff64252015-08-10 09:03:51 -0700607 if (miterStroke) {
Brian Salomon6a639042016-12-14 11:08:17 -0500608 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700609 static const uint16_t gMiterIndices[] = {
610 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0,
611 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0,
612 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0,
613 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0,
614
615 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4,
616 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4,
617 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4,
618 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4,
619
620 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8,
621 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8,
622 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8,
623 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8,
624 };
Brian Salomon6a639042016-12-14 11:08:17 -0500625 // clang-format on
Brian Salomon4dea72a2019-12-18 10:43:10 -0500626 static_assert(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt);
joshualitt9ff64252015-08-10 09:03:51 -0700627 GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400628 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500629 gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt,
630 gMiterIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700631 } else {
632 /**
633 * As in miter-stroke, index = a + b, and a is the current index, b is the shift
634 * from the first index. The index layout:
635 * outer AA line: 0~3, 4~7
636 * outer edge: 8~11, 12~15
637 * inner edge: 16~19
638 * inner AA line: 20~23
639 * Following comes a bevel-stroke rect and its indices:
640 *
641 * 4 7
642 * *********************************
643 * * ______________________________ *
644 * * / 12 15 \ *
645 * * / \ *
646 * 0 * |8 16_____________________19 11 | * 3
647 * * | | | | *
648 * * | | **************** | | *
649 * * | | * 20 23 * | | *
650 * * | | * * | | *
651 * * | | * 21 22 * | | *
652 * * | | **************** | | *
653 * * | |____________________| | *
654 * 1 * |9 17 18 10| * 2
655 * * \ / *
656 * * \13 __________________________14/ *
657 * * *
658 * **********************************
659 * 5 6
660 */
Brian Salomon6a639042016-12-14 11:08:17 -0500661 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700662 static const uint16_t gBevelIndices[] = {
663 // Draw outer AA, from outer AA line to outer edge, shift is 0.
664 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0,
665 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0,
666 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0,
667 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0,
668 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0,
669 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0,
670 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0,
671 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0,
672
673 // Draw the stroke, from outer edge to inner edge, shift is 8.
674 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8,
675 1 + 8, 5 + 8, 9 + 8,
676 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8,
677 6 + 8, 2 + 8, 10 + 8,
678 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8,
679 3 + 8, 7 + 8, 11 + 8,
680 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8,
681 4 + 8, 0 + 8, 8 + 8,
682
683 // Draw the inner AA, from inner edge to inner AA line, shift is 16.
684 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16,
685 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16,
686 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16,
687 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16,
688 };
Brian Salomon6a639042016-12-14 11:08:17 -0500689 // clang-format on
Brian Salomon4dea72a2019-12-18 10:43:10 -0500690 static_assert(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt);
joshualitt9ff64252015-08-10 09:03:51 -0700691
692 GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400693 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500694 gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt,
695 gBevelIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700696 }
697}
698
Herb Derbye25c3002020-10-27 15:57:27 -0400699GrOp::CombineResult AAStrokeRectOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps)
700{
Brian Salomon6a639042016-12-14 11:08:17 -0500701 AAStrokeRectOp* that = t->cast<AAStrokeRectOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700702
Brian Salomonbaaf4392017-06-15 09:59:23 -0400703 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000704 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700705 }
706
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500707 // TODO combine across miterstroke changes
joshualitt9ff64252015-08-10 09:03:51 -0700708 if (this->miterStroke() != that->miterStroke()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000709 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700710 }
711
712 // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses
Brian Salomonbaaf4392017-06-15 09:59:23 -0400713 // local coords then we won't be able to combine. TODO: Upload local coords as an attribute.
Mike Reed2c383152019-12-18 16:47:47 -0500714 if (fHelper.usesLocalCoords() &&
715 !SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix()))
716 {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000717 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700718 }
719
Brian Salomon8c5bad32016-12-20 14:43:36 -0500720 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
Brian Osman2a4c4df2018-12-20 14:06:54 -0500721 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000722 return CombineResult::kMerged;
joshualitt9ff64252015-08-10 09:03:51 -0700723}
724
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500725// Compute the coverage for the inner two rects.
726static float compute_inner_coverage(SkScalar maxDevHalfStrokeSize) {
727 if (maxDevHalfStrokeSize < SK_ScalarHalf) {
728 return 2.0f * maxDevHalfStrokeSize / (maxDevHalfStrokeSize + SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700729 }
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500730
731 return 1.0f;
joshualitt11edad92015-09-22 10:32:28 -0700732}
733
Brian Osmancfec9d52018-11-20 11:39:15 -0500734void AAStrokeRectOp::generateAAStrokeRectGeometry(GrVertexWriter& vertices,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500735 const SkPMColor4f& color,
736 bool wideColor,
Brian Salomon6a639042016-12-14 11:08:17 -0500737 const SkRect& devOutside,
738 const SkRect& devOutsideAssist,
739 const SkRect& devInside,
740 bool miterStroke,
741 bool degenerate,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500742 bool tweakAlphaForCoverage,
743 const SkVector& devHalfStrokeSize) const {
joshualitt9ff64252015-08-10 09:03:51 -0700744 // We create vertices for four nested rectangles. There are two ramps from 0 to full
745 // coverage, one on the exterior of the stroke and the other on the interior.
joshualitt9ff64252015-08-10 09:03:51 -0700746
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500747 // The following code only really works if either devStrokeSize's fX and fY are
748 // equal (in which case innerCoverage is same for all sides of the rects) or
749 // if devStrokeSize's fX and fY are both greater than 1.0 (in which case
750 // innerCoverage will always be 1). The most problematic case is when one of
751 // fX and fY is greater than 1.0 and the other is less than 1.0. In this case
752 // the smaller side should have a partial coverage but the larger side will
753 // force the coverage to be 1.0.
joshualitt9ff64252015-08-10 09:03:51 -0700754
Brian Osmancfec9d52018-11-20 11:39:15 -0500755 auto inset_fan = [](const SkRect& r, SkScalar dx, SkScalar dy) {
756 return GrVertexWriter::TriFanFromRect(r.makeInset(dx, dy));
757 };
joshualitt9ff64252015-08-10 09:03:51 -0700758
Brian Osmancfec9d52018-11-20 11:39:15 -0500759 auto maybe_coverage = [tweakAlphaForCoverage](float coverage) {
760 return GrVertexWriter::If(!tweakAlphaForCoverage, coverage);
761 };
762
Brian Osman2a4c4df2018-12-20 14:06:54 -0500763 GrVertexColor outerColor(tweakAlphaForCoverage ? SK_PMColor4fTRANSPARENT : color, wideColor);
Brian Osmancfec9d52018-11-20 11:39:15 -0500764
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500765 // For device-space stroke widths less than one we can't inset more than the original
766 // device space stroke width if we want to keep the sizing of all the rects correct.
Brian Osman788b9162020-02-07 10:36:46 -0500767 const SkScalar insetX = std::min(SK_ScalarHalf, devHalfStrokeSize.fX);
768 const SkScalar insetY = std::min(SK_ScalarHalf, devHalfStrokeSize.fY);
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500769
770 // But, correspondingly, we always want to keep the AA picture frame one pixel wide.
771 const SkScalar outsetX = SK_Scalar1 - insetX;
772 const SkScalar outsetY = SK_Scalar1 - insetY;
773
Brian Osmancfec9d52018-11-20 11:39:15 -0500774 // Outermost rect
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500775 vertices.writeQuad(inset_fan(devOutside, -outsetX, -outsetY),
Brian Osmancfec9d52018-11-20 11:39:15 -0500776 outerColor,
777 maybe_coverage(0.0f));
778
779 if (!miterStroke) {
780 // Second outermost
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500781 vertices.writeQuad(inset_fan(devOutsideAssist, -outsetX, -outsetY),
Brian Osmancfec9d52018-11-20 11:39:15 -0500782 outerColor,
783 maybe_coverage(0.0f));
joshualitt9ff64252015-08-10 09:03:51 -0700784 }
785
Brian Osman788b9162020-02-07 10:36:46 -0500786 float innerCoverage = compute_inner_coverage(std::max(devHalfStrokeSize.fX,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500787 devHalfStrokeSize.fY));
joshualitt9ff64252015-08-10 09:03:51 -0700788
Brian Osman2a4c4df2018-12-20 14:06:54 -0500789 SkPMColor4f scaledColor = color * innerCoverage;
790 GrVertexColor innerColor(tweakAlphaForCoverage ? scaledColor : color, wideColor);
joshualitt9ff64252015-08-10 09:03:51 -0700791
Brian Osmancfec9d52018-11-20 11:39:15 -0500792 // Inner rect
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500793 vertices.writeQuad(inset_fan(devOutside, insetX, insetY),
Brian Osmancfec9d52018-11-20 11:39:15 -0500794 innerColor,
795 maybe_coverage(innerCoverage));
796
797 if (!miterStroke) {
798 // Second inner
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500799 vertices.writeQuad(inset_fan(devOutsideAssist, insetX, insetY),
Brian Osmancfec9d52018-11-20 11:39:15 -0500800 innerColor,
801 maybe_coverage(innerCoverage));
joshualitt9ff64252015-08-10 09:03:51 -0700802 }
803
joshualitt11edad92015-09-22 10:32:28 -0700804 if (!degenerate) {
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500805 vertices.writeQuad(inset_fan(devInside, -insetX, -insetY),
Brian Osmancfec9d52018-11-20 11:39:15 -0500806 innerColor,
807 maybe_coverage(innerCoverage));
joshualitt11edad92015-09-22 10:32:28 -0700808
Brian Osmancfec9d52018-11-20 11:39:15 -0500809 // The innermost rect has 0 coverage...
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500810 vertices.writeQuad(inset_fan(devInside, outsetX, outsetY),
Brian Salomon747b3402019-09-16 17:25:01 -0400811 outerColor,
Brian Osmancfec9d52018-11-20 11:39:15 -0500812 maybe_coverage(0.0f));
813 } else {
814 // When the interior rect has become degenerate we smoosh to a single point
815 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
816
817 vertices.writeQuad(GrVertexWriter::TriFanFromRect(devInside),
818 innerColor,
819 maybe_coverage(innerCoverage));
820
821 // ... unless we are degenerate, in which case we must apply the scaled coverage
822 vertices.writeQuad(GrVertexWriter::TriFanFromRect(devInside),
823 innerColor,
824 maybe_coverage(innerCoverage));
joshualitt9ff64252015-08-10 09:03:51 -0700825 }
826}
827
Michael Ludwig72ab3462018-12-10 12:43:36 -0500828} // anonymous namespace
joshualitt3566d442015-09-18 07:12:55 -0700829
Michael Ludwig72ab3462018-12-10 12:43:36 -0500830namespace GrStrokeRectOp {
831
Herb Derbyc76d4092020-10-07 16:46:15 -0400832GrOp::Owner Make(GrRecordingContext* context,
833 GrPaint&& paint,
834 GrAAType aaType,
835 const SkMatrix& viewMatrix,
836 const SkRect& rect,
837 const SkStrokeRec& stroke) {
Michael Ludwig72ab3462018-12-10 12:43:36 -0500838 if (aaType == GrAAType::kCoverage) {
839 // The AA op only supports axis-aligned rectangles
840 if (!viewMatrix.rectStaysRect()) {
841 return nullptr;
842 }
843 return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke);
844 } else {
845 return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke, aaType);
846 }
847}
848
Herb Derbyc76d4092020-10-07 16:46:15 -0400849GrOp::Owner MakeNested(GrRecordingContext* context,
850 GrPaint&& paint,
851 const SkMatrix& viewMatrix,
852 const SkRect rects[2]) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400853 SkASSERT(viewMatrix.rectStaysRect());
854 SkASSERT(!rects[0].isEmpty() && !rects[1].isEmpty());
855
856 SkRect devOutside, devInside;
857 viewMatrix.mapRect(&devOutside, rects[0]);
858 viewMatrix.mapRect(&devInside, rects[1]);
859 if (devInside.isEmpty()) {
860 if (devOutside.isEmpty()) {
861 return nullptr;
862 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500863 DrawQuad quad{GrQuad::MakeFromRect(rects[0], viewMatrix), GrQuad(rects[0]),
864 GrQuadAAFlags::kAll};
865 return GrFillRectOp::Make(context, std::move(paint), GrAAType::kCoverage, &quad);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400866 }
867
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500868 SkVector devHalfStrokeSize{ SkScalarHalf(devOutside.fRight - devInside.fRight),
869 SkScalarHalf(devOutside.fBottom - devInside.fBottom) };
870 return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, devOutside,
871 devInside, devHalfStrokeSize);
joshualittaa37a962015-09-18 13:03:25 -0700872}
873
Michael Ludwig72ab3462018-12-10 12:43:36 -0500874} // namespace GrStrokeRectOp
joshualitt9ff64252015-08-10 09:03:51 -0700875
Hal Canary6f6961e2017-01-31 13:50:44 -0500876#if GR_TEST_UTILS
joshualitt9ff64252015-08-10 09:03:51 -0700877
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500878#include "src/gpu/GrDrawOpTest.h"
joshualitt9ff64252015-08-10 09:03:51 -0700879
Michael Ludwig72ab3462018-12-10 12:43:36 -0500880GR_DRAW_OP_TEST_DEFINE(NonAAStrokeRectOp) {
881 SkMatrix viewMatrix = GrTest::TestMatrix(random);
882 SkRect rect = GrTest::TestRect(random);
883 SkScalar strokeWidth = random->nextBool() ? 0.0f : 2.0f;
884 SkPaint strokePaint;
885 strokePaint.setStrokeWidth(strokeWidth);
886 strokePaint.setStyle(SkPaint::kStroke_Style);
887 strokePaint.setStrokeJoin(SkPaint::kMiter_Join);
888 SkStrokeRec strokeRec(strokePaint);
889 GrAAType aaType = GrAAType::kNone;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600890 if (numSamples > 1) {
Michael Ludwig72ab3462018-12-10 12:43:36 -0500891 aaType = random->nextBool() ? GrAAType::kMSAA : GrAAType::kNone;
892 }
893 return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, strokeRec, aaType);
894}
895
Brian Salomonbaaf4392017-06-15 09:59:23 -0400896GR_DRAW_OP_TEST_DEFINE(AAStrokeRectOp) {
joshualitt9ff64252015-08-10 09:03:51 -0700897 bool miterStroke = random->nextBool();
898
bsalomon40ef4852016-05-02 13:22:13 -0700899 // Create either a empty rect or a non-empty rect.
Brian Salomon6a639042016-12-14 11:08:17 -0500900 SkRect rect =
901 random->nextBool() ? SkRect::MakeXYWH(10, 10, 50, 40) : SkRect::MakeXYWH(6, 7, 0, 0);
Brian Osman116b33e2020-02-05 13:34:09 -0500902 SkScalar minDim = std::min(rect.width(), rect.height());
bsalomon40ef4852016-05-02 13:22:13 -0700903 SkScalar strokeWidth = random->nextUScalar1() * minDim;
joshualitt9ff64252015-08-10 09:03:51 -0700904
bsalomon40ef4852016-05-02 13:22:13 -0700905 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
906 rec.setStrokeStyle(strokeWidth);
907 rec.setStrokeParams(SkPaint::kButt_Cap,
Brian Salomon6a639042016-12-14 11:08:17 -0500908 miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f);
bsalomon40ef4852016-05-02 13:22:13 -0700909 SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500910 return AAStrokeRectOp::Make(context, std::move(paint), matrix, rect, rec);
joshualitt9ff64252015-08-10 09:03:51 -0700911}
912
913#endif