blob: fbdcb7a57523d5e1a06ebc6a61814ceb015eba77 [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
Robert Phillipsb97da532019-02-12 15:24:12 -0500102 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500103 GrPaint&& paint,
104 const SkMatrix& viewMatrix,
105 const SkRect& rect,
106 const SkStrokeRec& stroke,
107 GrAAType aaType) {
108 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
124 NonAAStrokeRectOp(const Helper::MakeArgs& helperArgs, 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())
128 , fHelper(helperArgs, 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,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400175 const GrSurfaceProxyView* writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400176 GrAppliedClip&& clip,
177 const GrXferProcessor::DstProxyView& dstProxyView) override {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500178 GrGeometryProcessor* gp;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500179 {
180 using namespace GrDefaultGeoProcFactory;
181 Color color(fColor);
182 LocalCoords::Type localCoordsType = fHelper.usesLocalCoords()
183 ? LocalCoords::kUsePosition_Type
184 : LocalCoords::kUnused_Type;
Brian Osmanf0aee742020-03-12 09:28:44 -0400185 gp = GrDefaultGeoProcFactory::Make(arena, color, Coverage::kSolid_Type, localCoordsType,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500186 fViewMatrix);
187 }
188
Robert Phillips4133dc42020-03-11 15:55:55 -0400189 GrPrimitiveType primType = (fStrokeWidth > 0) ? GrPrimitiveType::kTriangleStrip
190 : GrPrimitiveType::kLineStrip;
Robert Phillipsd2f18732020-03-04 16:12:08 -0500191
Brian Salomon8afde5f2020-04-01 16:22:00 -0400192 fProgramInfo = fHelper.createProgramInfo(caps, arena, writeView, std::move(clip),
Robert Phillips4133dc42020-03-11 15:55:55 -0400193 dstProxyView, gp, primType);
Robert Phillipsd2f18732020-03-04 16:12:08 -0500194 }
195
Robert Phillipsd2f18732020-03-04 16:12:08 -0500196 void onPrepareDraws(Target* target) override {
197 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400198 this->createProgramInfo(target);
Robert Phillipsd2f18732020-03-04 16:12:08 -0500199 }
200
201 size_t kVertexStride = fProgramInfo->primProc().vertexStride();
Michael Ludwig72ab3462018-12-10 12:43:36 -0500202 int vertexCount = kVertsPerHairlineRect;
203 if (fStrokeWidth > 0) {
204 vertexCount = kVertsPerStrokeRect;
205 }
206
Brian Salomon12d22642019-01-29 14:38:50 -0500207 sk_sp<const GrBuffer> vertexBuffer;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500208 int firstVertex;
209
210 void* verts =
211 target->makeVertexSpace(kVertexStride, vertexCount, &vertexBuffer, &firstVertex);
212
213 if (!verts) {
214 SkDebugf("Could not allocate vertices\n");
215 return;
216 }
217
218 SkPoint* vertex = reinterpret_cast<SkPoint*>(verts);
219
Michael Ludwig72ab3462018-12-10 12:43:36 -0500220 if (fStrokeWidth > 0) {
Michael Ludwig72ab3462018-12-10 12:43:36 -0500221 init_nonaa_stroke_rect_strip(vertex, fRect, fStrokeWidth);
222 } else {
223 // hairline
Michael Ludwig72ab3462018-12-10 12:43:36 -0500224 vertex[0].set(fRect.fLeft, fRect.fTop);
225 vertex[1].set(fRect.fRight, fRect.fTop);
226 vertex[2].set(fRect.fRight, fRect.fBottom);
227 vertex[3].set(fRect.fLeft, fRect.fBottom);
228 vertex[4].set(fRect.fLeft, fRect.fTop);
229 }
230
Robert Phillipsd2f18732020-03-04 16:12:08 -0500231 fMesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600232 fMesh->set(std::move(vertexBuffer), vertexCount, firstVertex);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700233 }
234
235 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillipsd2f18732020-03-04 16:12:08 -0500236 if (!fMesh) {
237 return;
238 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500239
Chris Dalton765ed362020-03-16 17:34:44 -0600240 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
241 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
242 flushState->drawMesh(*fMesh);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500243 }
244
John Stilesaf366522020-08-13 09:57:34 -0400245#if GR_TEST_UTILS
246 SkString onDumpInfo() const override {
247 return SkStringPrintf("Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
248 "StrokeWidth: %.2f\n%s",
249 fColor.toBytes_RGBA(), fRect.fLeft, fRect.fTop, fRect.fRight,
250 fRect.fBottom, fStrokeWidth, fHelper.dumpInfo().c_str());
251 }
252#endif
253
Michael Ludwig72ab3462018-12-10 12:43:36 -0500254 // TODO: override onCombineIfPossible
255
Robert Phillipsd2f18732020-03-04 16:12:08 -0500256 Helper fHelper;
257 SkPMColor4f fColor;
258 SkMatrix fViewMatrix;
259 SkRect fRect;
260 SkScalar fStrokeWidth;
Chris Daltoneb694b72020-03-16 09:25:50 -0600261 GrSimpleMesh* fMesh = nullptr;
Robert Phillipsd2f18732020-03-04 16:12:08 -0500262 GrProgramInfo* fProgramInfo = nullptr;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500263
264 const static int kVertsPerHairlineRect = 5;
265 const static int kVertsPerStrokeRect = 10;
266
John Stiles7571f9e2020-09-02 22:42:33 -0400267 using INHERITED = GrMeshDrawOp;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500268};
269
270///////////////////////////////////////////////////////////////////////////////////////////////////
271// AA Stroking
272///////////////////////////////////////////////////////////////////////////////////////////////////
273
274GR_DECLARE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
275GR_DECLARE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
276
277static void compute_aa_rects(SkRect* devOutside, SkRect* devOutsideAssist, SkRect* devInside,
278 bool* isDegenerate, const SkMatrix& viewMatrix, const SkRect& rect,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500279 SkScalar strokeWidth, bool miterStroke, SkVector* devHalfStrokeSize) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700280 SkRect devRect;
281 viewMatrix.mapRect(&devRect, rect);
282
283 SkVector devStrokeSize;
284 if (strokeWidth > 0) {
285 devStrokeSize.set(strokeWidth, strokeWidth);
286 viewMatrix.mapVectors(&devStrokeSize, 1);
287 devStrokeSize.setAbs(devStrokeSize);
288 } else {
289 devStrokeSize.set(SK_Scalar1, SK_Scalar1);
290 }
291
292 const SkScalar dx = devStrokeSize.fX;
293 const SkScalar dy = devStrokeSize.fY;
Mike Reed8be952a2017-02-13 20:44:33 -0500294 const SkScalar rx = SkScalarHalf(dx);
295 const SkScalar ry = SkScalarHalf(dy);
bsalomon8b7a9e12016-07-06 13:06:22 -0700296
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500297 devHalfStrokeSize->fX = rx;
298 devHalfStrokeSize->fY = ry;
299
bsalomon8b7a9e12016-07-06 13:06:22 -0700300 *devOutside = devRect;
301 *devOutsideAssist = devRect;
302 *devInside = devRect;
303
304 devOutside->outset(rx, ry);
305 devInside->inset(rx, ry);
306
307 // If we have a degenerate stroking rect(ie the stroke is larger than inner rect) then we
308 // make a degenerate inside rect to avoid double hitting. We will also jam all of the points
309 // together when we render these rects.
310 SkScalar spare;
311 {
312 SkScalar w = devRect.width() - dx;
313 SkScalar h = devRect.height() - dy;
Brian Osman788b9162020-02-07 10:36:46 -0500314 spare = std::min(w, h);
bsalomon8b7a9e12016-07-06 13:06:22 -0700315 }
316
317 *isDegenerate = spare <= 0;
318 if (*isDegenerate) {
319 devInside->fLeft = devInside->fRight = devRect.centerX();
320 devInside->fTop = devInside->fBottom = devRect.centerY();
321 }
322
323 // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist)
324 // to draw the outside of the octagon. Because there are 8 vertices on the outer
325 // edge, while vertex number of inner edge is 4, the same as miter-stroke.
326 if (!miterStroke) {
327 devOutside->inset(0, ry);
328 devOutsideAssist->outset(0, ry);
329 }
330}
331
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500332static GrGeometryProcessor* create_aa_stroke_rect_gp(SkArenaAlloc* arena,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500333 bool tweakAlphaForCoverage,
334 const SkMatrix& viewMatrix,
335 bool usesLocalCoords,
336 bool wideColor) {
joshualitt9ff64252015-08-10 09:03:51 -0700337 using namespace GrDefaultGeoProcFactory;
338
Brian Osman2a4c4df2018-12-20 14:06:54 -0500339 Coverage::Type coverageType =
340 tweakAlphaForCoverage ? Coverage::kSolid_Type : Coverage::kAttribute_Type;
Brian Salomon8c852be2017-01-04 10:44:42 -0500341 LocalCoords::Type localCoordsType =
Brian Osman2a4c4df2018-12-20 14:06:54 -0500342 usesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type;
343 Color::Type colorType =
344 wideColor ? Color::kPremulWideColorAttribute_Type: Color::kPremulGrColorAttribute_Type;
345
Brian Osmanf0aee742020-03-12 09:28:44 -0400346 return MakeForDeviceSpace(arena, colorType, coverageType, localCoordsType, viewMatrix);
joshualitt9ff64252015-08-10 09:03:51 -0700347}
348
Brian Salomonbaaf4392017-06-15 09:59:23 -0400349class AAStrokeRectOp final : public GrMeshDrawOp {
350private:
351 using Helper = GrSimpleMeshDrawOpHelper;
352
joshualitt3566d442015-09-18 07:12:55 -0700353public:
Brian Salomon25a88092016-12-01 09:36:50 -0500354 DEFINE_OP_CLASS_ID
joshualitt3566d442015-09-18 07:12:55 -0700355
Robert Phillipsb97da532019-02-12 15:24:12 -0500356 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400357 GrPaint&& paint,
358 const SkMatrix& viewMatrix,
359 const SkRect& devOutside,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500360 const SkRect& devInside,
361 const SkVector& devHalfStrokeSize) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400362 return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500363 devOutside, devInside, devHalfStrokeSize);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400364 }
365
Brian Osmancf860852018-10-31 14:04:39 -0400366 AAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500367 const SkMatrix& viewMatrix, const SkRect& devOutside, const SkRect& devInside,
368 const SkVector& devHalfStrokeSize)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400369 : INHERITED(ClassID())
370 , fHelper(helperArgs, GrAAType::kCoverage)
371 , fViewMatrix(viewMatrix) {
caryclarkd6562002016-07-27 12:02:07 -0700372 SkASSERT(!devOutside.isEmpty());
373 SkASSERT(!devInside.isEmpty());
joshualitt3566d442015-09-18 07:12:55 -0700374
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500375 fRects.emplace_back(RectInfo{color, devOutside, devOutside, devInside, devHalfStrokeSize, false});
Greg Daniel5faf4742019-10-01 15:14:44 -0400376 this->setBounds(devOutside, HasAABloat::kYes, IsHairline::kNo);
bsalomon8b7a9e12016-07-06 13:06:22 -0700377 fMiterStroke = true;
378 }
379
Robert Phillipsb97da532019-02-12 15:24:12 -0500380 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400381 GrPaint&& paint,
382 const SkMatrix& viewMatrix,
383 const SkRect& rect,
384 const SkStrokeRec& stroke) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700385 bool isMiter;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500386 if (!allowed_stroke(stroke, GrAA::kYes, &isMiter)) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700387 return nullptr;
388 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400389 return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix, rect,
390 stroke, isMiter);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400391 }
bsalomon8b7a9e12016-07-06 13:06:22 -0700392
Brian Osmancf860852018-10-31 14:04:39 -0400393 AAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
Brian Osman936fe7d2018-10-30 15:30:35 -0400394 const SkMatrix& viewMatrix, const SkRect& rect, const SkStrokeRec& stroke,
395 bool isMiter)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400396 : INHERITED(ClassID())
397 , fHelper(helperArgs, GrAAType::kCoverage)
398 , fViewMatrix(viewMatrix) {
399 fMiterStroke = isMiter;
400 RectInfo& info = fRects.push_back();
Michael Ludwig72ab3462018-12-10 12:43:36 -0500401 compute_aa_rects(&info.fDevOutside, &info.fDevOutsideAssist, &info.fDevInside,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500402 &info.fDegenerate, viewMatrix, rect, stroke.getWidth(), isMiter,
403 &info.fDevHalfStrokeSize);
Brian Salomon8c5bad32016-12-20 14:43:36 -0500404 info.fColor = color;
Brian Salomon510dd422017-03-16 12:15:22 -0400405 if (isMiter) {
Greg Daniel5faf4742019-10-01 15:14:44 -0400406 this->setBounds(info.fDevOutside, HasAABloat::kYes, IsHairline::kNo);
Brian Salomon510dd422017-03-16 12:15:22 -0400407 } else {
408 // The outer polygon of the bevel stroke is an octagon specified by the points of a
409 // pair of overlapping rectangles where one is wide and the other is narrow.
410 SkRect bounds = info.fDevOutside;
411 bounds.joinPossiblyEmptyRect(info.fDevOutsideAssist);
Greg Daniel5faf4742019-10-01 15:14:44 -0400412 this->setBounds(bounds, HasAABloat::kYes, IsHairline::kNo);
Brian Salomon510dd422017-03-16 12:15:22 -0400413 }
joshualitt3566d442015-09-18 07:12:55 -0700414 }
415
416 const char* name() const override { return "AAStrokeRect"; }
417
Chris Dalton1706cbf2019-05-21 19:35:29 -0600418 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsd2f18732020-03-04 16:12:08 -0500419 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600420 fProgramInfo->visitFPProxies(func);
Robert Phillipsd2f18732020-03-04 16:12:08 -0500421 } else {
422 fHelper.visitProxies(func);
423 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400424 }
425
Brian Salomonbaaf4392017-06-15 09:59:23 -0400426 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
Brian Salomona0485d92017-06-14 19:08:01 -0400427
Chris Dalton6ce447a2019-06-23 18:07:38 -0600428 GrProcessorSet::Analysis finalize(
429 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
430 GrClampType clampType) override {
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700431 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600432 caps, clip, hasMixedSampledCoverage, clampType,
433 GrProcessorAnalysisCoverage::kSingleChannel, &fRects.back().fColor, &fWideColor);
Brian Salomona0485d92017-06-14 19:08:01 -0400434 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400435
436private:
Robert Phillips2669a7b2020-03-12 12:07:19 -0400437 GrProgramInfo* programInfo() override { return fProgramInfo; }
438
Robert Phillips4133dc42020-03-11 15:55:55 -0400439 void onCreateProgramInfo(const GrCaps*,
440 SkArenaAlloc*,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400441 const GrSurfaceProxyView* writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400442 GrAppliedClip&&,
443 const GrXferProcessor::DstProxyView&) override;
Robert Phillipsd2f18732020-03-04 16:12:08 -0500444
Brian Salomon91326c32017-08-09 16:02:19 -0400445 void onPrepareDraws(Target*) override;
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700446 void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
joshualittaa37a962015-09-18 13:03:25 -0700447
John Stilesaf366522020-08-13 09:57:34 -0400448#if GR_TEST_UTILS
449 SkString onDumpInfo() const override {
450 SkString string;
451 for (const auto& info : fRects) {
452 string.appendf(
453 "Color: 0x%08x, ORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
454 "AssistORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
455 "IRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], Degen: %d",
456 info.fColor.toBytes_RGBA(), info.fDevOutside.fLeft, info.fDevOutside.fTop,
457 info.fDevOutside.fRight, info.fDevOutside.fBottom, info.fDevOutsideAssist.fLeft,
458 info.fDevOutsideAssist.fTop, info.fDevOutsideAssist.fRight,
459 info.fDevOutsideAssist.fBottom, info.fDevInside.fLeft, info.fDevInside.fTop,
460 info.fDevInside.fRight, info.fDevInside.fBottom, info.fDegenerate);
461 }
462 string += fHelper.dumpInfo();
463 return string;
464 }
465#endif
466
joshualitt3566d442015-09-18 07:12:55 -0700467 static const int kMiterIndexCnt = 3 * 24;
468 static const int kMiterVertexCnt = 16;
469 static const int kNumMiterRectsInIndexBuffer = 256;
470
471 static const int kBevelIndexCnt = 48 + 36 + 24;
472 static const int kBevelVertexCnt = 24;
473 static const int kNumBevelRectsInIndexBuffer = 256;
474
Brian Salomondbf70722019-02-07 11:31:24 -0500475 static sk_sp<const GrGpuBuffer> GetIndexBuffer(GrResourceProvider*, bool miterStroke);
joshualitt3566d442015-09-18 07:12:55 -0700476
joshualittaa37a962015-09-18 13:03:25 -0700477 const SkMatrix& viewMatrix() const { return fViewMatrix; }
478 bool miterStroke() const { return fMiterStroke; }
joshualitt3566d442015-09-18 07:12:55 -0700479
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500480 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*, const GrCaps&) override;
joshualitt3566d442015-09-18 07:12:55 -0700481
Brian Osmancfec9d52018-11-20 11:39:15 -0500482 void generateAAStrokeRectGeometry(GrVertexWriter& vertices,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500483 const SkPMColor4f& color,
484 bool wideColor,
joshualitt3566d442015-09-18 07:12:55 -0700485 const SkRect& devOutside,
486 const SkRect& devOutsideAssist,
487 const SkRect& devInside,
488 bool miterStroke,
joshualitt11edad92015-09-22 10:32:28 -0700489 bool degenerate,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500490 bool tweakAlphaForCoverage,
491 const SkVector& devHalfStrokeSize) const;
joshualitt3566d442015-09-18 07:12:55 -0700492
bsalomon8b7a9e12016-07-06 13:06:22 -0700493 // TODO support AA rotated stroke rects by copying around view matrices
Brian Salomon8c5bad32016-12-20 14:43:36 -0500494 struct RectInfo {
Brian Osmancf860852018-10-31 14:04:39 -0400495 SkPMColor4f fColor;
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500496 SkRect fDevOutside;
497 SkRect fDevOutsideAssist;
498 SkRect fDevInside;
499 SkVector fDevHalfStrokeSize;
500 bool fDegenerate;
bsalomon8b7a9e12016-07-06 13:06:22 -0700501 };
502
Robert Phillipsd2f18732020-03-04 16:12:08 -0500503 Helper fHelper;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500504 SkSTArray<1, RectInfo, true> fRects;
Robert Phillipsd2f18732020-03-04 16:12:08 -0500505 SkMatrix fViewMatrix;
Chris Daltoneb694b72020-03-16 09:25:50 -0600506 GrSimpleMesh* fMesh = nullptr;
Robert Phillipsd2f18732020-03-04 16:12:08 -0500507 GrProgramInfo* fProgramInfo = nullptr;
508 bool fMiterStroke;
509 bool fWideColor;
joshualitt3566d442015-09-18 07:12:55 -0700510
John Stiles7571f9e2020-09-02 22:42:33 -0400511 using INHERITED = GrMeshDrawOp;
joshualitt3566d442015-09-18 07:12:55 -0700512};
513
Robert Phillips4133dc42020-03-11 15:55:55 -0400514void AAStrokeRectOp::onCreateProgramInfo(const GrCaps* caps,
515 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400516 const GrSurfaceProxyView* writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400517 GrAppliedClip&& appliedClip,
518 const GrXferProcessor::DstProxyView& dstProxyView) {
Robert Phillipsd2f18732020-03-04 16:12:08 -0500519
520 GrGeometryProcessor* gp = create_aa_stroke_rect_gp(arena,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500521 fHelper.compatibleWithCoverageAsAlpha(),
522 this->viewMatrix(),
523 fHelper.usesLocalCoords(),
524 fWideColor);
joshualitt9ff64252015-08-10 09:03:51 -0700525 if (!gp) {
526 SkDebugf("Couldn't create GrGeometryProcessor\n");
Robert Phillips4133dc42020-03-11 15:55:55 -0400527 return;
Robert Phillipsd2f18732020-03-04 16:12:08 -0500528 }
529
Robert Phillips4133dc42020-03-11 15:55:55 -0400530 fProgramInfo = fHelper.createProgramInfo(caps,
531 arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400532 writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400533 std::move(appliedClip),
534 dstProxyView,
535 gp,
536 GrPrimitiveType::kTriangles);
Robert Phillipsd2f18732020-03-04 16:12:08 -0500537}
538
Robert Phillipsd2f18732020-03-04 16:12:08 -0500539void AAStrokeRectOp::onPrepareDraws(Target* target) {
540
541 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400542 this->createProgramInfo(target);
Robert Phillipsd2f18732020-03-04 16:12:08 -0500543 if (!fProgramInfo) {
544 return;
545 }
joshualitt9ff64252015-08-10 09:03:51 -0700546 }
547
joshualitt9ff64252015-08-10 09:03:51 -0700548 int innerVertexNum = 4;
549 int outerVertexNum = this->miterStroke() ? 4 : 8;
550 int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2;
551 int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500552 int instanceCount = fRects.count();
Robert Phillipsee08d522019-10-28 16:34:44 -0400553 int maxQuads = this->miterStroke() ? kNumMiterRectsInIndexBuffer : kNumBevelRectsInIndexBuffer;
joshualitt9ff64252015-08-10 09:03:51 -0700554
Brian Salomondbf70722019-02-07 11:31:24 -0500555 sk_sp<const GrGpuBuffer> indexBuffer =
Brian Salomon7eae3e02018-08-07 14:02:38 +0000556 GetIndexBuffer(target->resourceProvider(), this->miterStroke());
Brian Salomon12d22642019-01-29 14:38:50 -0500557 if (!indexBuffer) {
558 SkDebugf("Could not allocate indices\n");
559 return;
560 }
Robert Phillipsd2f18732020-03-04 16:12:08 -0500561 PatternHelper helper(target, GrPrimitiveType::kTriangles,
562 fProgramInfo->primProc().vertexStride(), std::move(indexBuffer),
563 verticesPerInstance, indicesPerInstance, instanceCount, maxQuads);
Brian Osmancfec9d52018-11-20 11:39:15 -0500564 GrVertexWriter vertices{ helper.vertices() };
Brian Salomon12d22642019-01-29 14:38:50 -0500565 if (!vertices.fPtr) {
Brian Salomon6a639042016-12-14 11:08:17 -0500566 SkDebugf("Could not allocate vertices\n");
567 return;
568 }
joshualitt9ff64252015-08-10 09:03:51 -0700569
570 for (int i = 0; i < instanceCount; i++) {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500571 const RectInfo& info = fRects[i];
joshualitt9ff64252015-08-10 09:03:51 -0700572 this->generateAAStrokeRectGeometry(vertices,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500573 info.fColor,
574 fWideColor,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500575 info.fDevOutside,
576 info.fDevOutsideAssist,
577 info.fDevInside,
joshualittaa37a962015-09-18 13:03:25 -0700578 fMiterStroke,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500579 info.fDegenerate,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500580 fHelper.compatibleWithCoverageAsAlpha(),
581 info.fDevHalfStrokeSize);
joshualitt9ff64252015-08-10 09:03:51 -0700582 }
Robert Phillipsd2f18732020-03-04 16:12:08 -0500583 fMesh = helper.mesh();
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700584}
585
586void AAStrokeRectOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillipsd2f18732020-03-04 16:12:08 -0500587 if (!fProgramInfo || !fMesh) {
588 return;
589 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500590
Chris Dalton765ed362020-03-16 17:34:44 -0600591 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
592 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
593 flushState->drawMesh(*fMesh);
joshualitt9ff64252015-08-10 09:03:51 -0700594}
595
Brian Salomondbf70722019-02-07 11:31:24 -0500596sk_sp<const GrGpuBuffer> AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider,
597 bool miterStroke) {
joshualitt9ff64252015-08-10 09:03:51 -0700598 if (miterStroke) {
Brian Salomon6a639042016-12-14 11:08:17 -0500599 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700600 static const uint16_t gMiterIndices[] = {
601 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0,
602 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0,
603 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0,
604 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0,
605
606 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4,
607 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4,
608 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4,
609 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4,
610
611 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8,
612 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8,
613 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8,
614 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8,
615 };
Brian Salomon6a639042016-12-14 11:08:17 -0500616 // clang-format on
Brian Salomon4dea72a2019-12-18 10:43:10 -0500617 static_assert(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt);
joshualitt9ff64252015-08-10 09:03:51 -0700618 GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400619 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500620 gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt,
621 gMiterIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700622 } else {
623 /**
624 * As in miter-stroke, index = a + b, and a is the current index, b is the shift
625 * from the first index. The index layout:
626 * outer AA line: 0~3, 4~7
627 * outer edge: 8~11, 12~15
628 * inner edge: 16~19
629 * inner AA line: 20~23
630 * Following comes a bevel-stroke rect and its indices:
631 *
632 * 4 7
633 * *********************************
634 * * ______________________________ *
635 * * / 12 15 \ *
636 * * / \ *
637 * 0 * |8 16_____________________19 11 | * 3
638 * * | | | | *
639 * * | | **************** | | *
640 * * | | * 20 23 * | | *
641 * * | | * * | | *
642 * * | | * 21 22 * | | *
643 * * | | **************** | | *
644 * * | |____________________| | *
645 * 1 * |9 17 18 10| * 2
646 * * \ / *
647 * * \13 __________________________14/ *
648 * * *
649 * **********************************
650 * 5 6
651 */
Brian Salomon6a639042016-12-14 11:08:17 -0500652 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700653 static const uint16_t gBevelIndices[] = {
654 // Draw outer AA, from outer AA line to outer edge, shift is 0.
655 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0,
656 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0,
657 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0,
658 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0,
659 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0,
660 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0,
661 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0,
662 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0,
663
664 // Draw the stroke, from outer edge to inner edge, shift is 8.
665 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8,
666 1 + 8, 5 + 8, 9 + 8,
667 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8,
668 6 + 8, 2 + 8, 10 + 8,
669 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8,
670 3 + 8, 7 + 8, 11 + 8,
671 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8,
672 4 + 8, 0 + 8, 8 + 8,
673
674 // Draw the inner AA, from inner edge to inner AA line, shift is 16.
675 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16,
676 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16,
677 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16,
678 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16,
679 };
Brian Salomon6a639042016-12-14 11:08:17 -0500680 // clang-format on
Brian Salomon4dea72a2019-12-18 10:43:10 -0500681 static_assert(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt);
joshualitt9ff64252015-08-10 09:03:51 -0700682
683 GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400684 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500685 gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt,
686 gBevelIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700687 }
688}
689
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500690GrOp::CombineResult AAStrokeRectOp::onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500691 const GrCaps& caps) {
Brian Salomon6a639042016-12-14 11:08:17 -0500692 AAStrokeRectOp* that = t->cast<AAStrokeRectOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700693
Brian Salomonbaaf4392017-06-15 09:59:23 -0400694 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000695 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700696 }
697
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500698 // TODO combine across miterstroke changes
joshualitt9ff64252015-08-10 09:03:51 -0700699 if (this->miterStroke() != that->miterStroke()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000700 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700701 }
702
703 // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses
Brian Salomonbaaf4392017-06-15 09:59:23 -0400704 // local coords then we won't be able to combine. TODO: Upload local coords as an attribute.
Mike Reed2c383152019-12-18 16:47:47 -0500705 if (fHelper.usesLocalCoords() &&
706 !SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix()))
707 {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000708 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700709 }
710
Brian Salomon8c5bad32016-12-20 14:43:36 -0500711 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
Brian Osman2a4c4df2018-12-20 14:06:54 -0500712 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000713 return CombineResult::kMerged;
joshualitt9ff64252015-08-10 09:03:51 -0700714}
715
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500716// Compute the coverage for the inner two rects.
717static float compute_inner_coverage(SkScalar maxDevHalfStrokeSize) {
718 if (maxDevHalfStrokeSize < SK_ScalarHalf) {
719 return 2.0f * maxDevHalfStrokeSize / (maxDevHalfStrokeSize + SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700720 }
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500721
722 return 1.0f;
joshualitt11edad92015-09-22 10:32:28 -0700723}
724
Brian Osmancfec9d52018-11-20 11:39:15 -0500725void AAStrokeRectOp::generateAAStrokeRectGeometry(GrVertexWriter& vertices,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500726 const SkPMColor4f& color,
727 bool wideColor,
Brian Salomon6a639042016-12-14 11:08:17 -0500728 const SkRect& devOutside,
729 const SkRect& devOutsideAssist,
730 const SkRect& devInside,
731 bool miterStroke,
732 bool degenerate,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500733 bool tweakAlphaForCoverage,
734 const SkVector& devHalfStrokeSize) const {
joshualitt9ff64252015-08-10 09:03:51 -0700735 // We create vertices for four nested rectangles. There are two ramps from 0 to full
736 // coverage, one on the exterior of the stroke and the other on the interior.
joshualitt9ff64252015-08-10 09:03:51 -0700737
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500738 // The following code only really works if either devStrokeSize's fX and fY are
739 // equal (in which case innerCoverage is same for all sides of the rects) or
740 // if devStrokeSize's fX and fY are both greater than 1.0 (in which case
741 // innerCoverage will always be 1). The most problematic case is when one of
742 // fX and fY is greater than 1.0 and the other is less than 1.0. In this case
743 // the smaller side should have a partial coverage but the larger side will
744 // force the coverage to be 1.0.
joshualitt9ff64252015-08-10 09:03:51 -0700745
Brian Osmancfec9d52018-11-20 11:39:15 -0500746 auto inset_fan = [](const SkRect& r, SkScalar dx, SkScalar dy) {
747 return GrVertexWriter::TriFanFromRect(r.makeInset(dx, dy));
748 };
joshualitt9ff64252015-08-10 09:03:51 -0700749
Brian Osmancfec9d52018-11-20 11:39:15 -0500750 auto maybe_coverage = [tweakAlphaForCoverage](float coverage) {
751 return GrVertexWriter::If(!tweakAlphaForCoverage, coverage);
752 };
753
Brian Osman2a4c4df2018-12-20 14:06:54 -0500754 GrVertexColor outerColor(tweakAlphaForCoverage ? SK_PMColor4fTRANSPARENT : color, wideColor);
Brian Osmancfec9d52018-11-20 11:39:15 -0500755
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500756 // For device-space stroke widths less than one we can't inset more than the original
757 // device space stroke width if we want to keep the sizing of all the rects correct.
Brian Osman788b9162020-02-07 10:36:46 -0500758 const SkScalar insetX = std::min(SK_ScalarHalf, devHalfStrokeSize.fX);
759 const SkScalar insetY = std::min(SK_ScalarHalf, devHalfStrokeSize.fY);
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500760
761 // But, correspondingly, we always want to keep the AA picture frame one pixel wide.
762 const SkScalar outsetX = SK_Scalar1 - insetX;
763 const SkScalar outsetY = SK_Scalar1 - insetY;
764
Brian Osmancfec9d52018-11-20 11:39:15 -0500765 // Outermost rect
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500766 vertices.writeQuad(inset_fan(devOutside, -outsetX, -outsetY),
Brian Osmancfec9d52018-11-20 11:39:15 -0500767 outerColor,
768 maybe_coverage(0.0f));
769
770 if (!miterStroke) {
771 // Second outermost
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500772 vertices.writeQuad(inset_fan(devOutsideAssist, -outsetX, -outsetY),
Brian Osmancfec9d52018-11-20 11:39:15 -0500773 outerColor,
774 maybe_coverage(0.0f));
joshualitt9ff64252015-08-10 09:03:51 -0700775 }
776
Brian Osman788b9162020-02-07 10:36:46 -0500777 float innerCoverage = compute_inner_coverage(std::max(devHalfStrokeSize.fX,
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500778 devHalfStrokeSize.fY));
joshualitt9ff64252015-08-10 09:03:51 -0700779
Brian Osman2a4c4df2018-12-20 14:06:54 -0500780 SkPMColor4f scaledColor = color * innerCoverage;
781 GrVertexColor innerColor(tweakAlphaForCoverage ? scaledColor : color, wideColor);
joshualitt9ff64252015-08-10 09:03:51 -0700782
Brian Osmancfec9d52018-11-20 11:39:15 -0500783 // Inner rect
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500784 vertices.writeQuad(inset_fan(devOutside, insetX, insetY),
Brian Osmancfec9d52018-11-20 11:39:15 -0500785 innerColor,
786 maybe_coverage(innerCoverage));
787
788 if (!miterStroke) {
789 // Second inner
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500790 vertices.writeQuad(inset_fan(devOutsideAssist, insetX, insetY),
Brian Osmancfec9d52018-11-20 11:39:15 -0500791 innerColor,
792 maybe_coverage(innerCoverage));
joshualitt9ff64252015-08-10 09:03:51 -0700793 }
794
joshualitt11edad92015-09-22 10:32:28 -0700795 if (!degenerate) {
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500796 vertices.writeQuad(inset_fan(devInside, -insetX, -insetY),
Brian Osmancfec9d52018-11-20 11:39:15 -0500797 innerColor,
798 maybe_coverage(innerCoverage));
joshualitt11edad92015-09-22 10:32:28 -0700799
Brian Osmancfec9d52018-11-20 11:39:15 -0500800 // The innermost rect has 0 coverage...
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500801 vertices.writeQuad(inset_fan(devInside, outsetX, outsetY),
Brian Salomon747b3402019-09-16 17:25:01 -0400802 outerColor,
Brian Osmancfec9d52018-11-20 11:39:15 -0500803 maybe_coverage(0.0f));
804 } else {
805 // When the interior rect has become degenerate we smoosh to a single point
806 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
807
808 vertices.writeQuad(GrVertexWriter::TriFanFromRect(devInside),
809 innerColor,
810 maybe_coverage(innerCoverage));
811
812 // ... unless we are degenerate, in which case we must apply the scaled coverage
813 vertices.writeQuad(GrVertexWriter::TriFanFromRect(devInside),
814 innerColor,
815 maybe_coverage(innerCoverage));
joshualitt9ff64252015-08-10 09:03:51 -0700816 }
817}
818
Michael Ludwig72ab3462018-12-10 12:43:36 -0500819} // anonymous namespace
joshualitt3566d442015-09-18 07:12:55 -0700820
Michael Ludwig72ab3462018-12-10 12:43:36 -0500821namespace GrStrokeRectOp {
822
Robert Phillipsb97da532019-02-12 15:24:12 -0500823std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500824 GrPaint&& paint,
825 GrAAType aaType,
826 const SkMatrix& viewMatrix,
827 const SkRect& rect,
828 const SkStrokeRec& stroke) {
829 if (aaType == GrAAType::kCoverage) {
830 // The AA op only supports axis-aligned rectangles
831 if (!viewMatrix.rectStaysRect()) {
832 return nullptr;
833 }
834 return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke);
835 } else {
836 return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke, aaType);
837 }
838}
839
Robert Phillipsb97da532019-02-12 15:24:12 -0500840std::unique_ptr<GrDrawOp> MakeNested(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500841 GrPaint&& paint,
842 const SkMatrix& viewMatrix,
843 const SkRect rects[2]) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400844 SkASSERT(viewMatrix.rectStaysRect());
845 SkASSERT(!rects[0].isEmpty() && !rects[1].isEmpty());
846
847 SkRect devOutside, devInside;
848 viewMatrix.mapRect(&devOutside, rects[0]);
849 viewMatrix.mapRect(&devInside, rects[1]);
850 if (devInside.isEmpty()) {
851 if (devOutside.isEmpty()) {
852 return nullptr;
853 }
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500854 DrawQuad quad{GrQuad::MakeFromRect(rects[0], viewMatrix), GrQuad(rects[0]),
855 GrQuadAAFlags::kAll};
856 return GrFillRectOp::Make(context, std::move(paint), GrAAType::kCoverage, &quad);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400857 }
858
Robert Phillipsd5caeb82020-01-08 16:27:59 -0500859 SkVector devHalfStrokeSize{ SkScalarHalf(devOutside.fRight - devInside.fRight),
860 SkScalarHalf(devOutside.fBottom - devInside.fBottom) };
861 return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, devOutside,
862 devInside, devHalfStrokeSize);
joshualittaa37a962015-09-18 13:03:25 -0700863}
864
Michael Ludwig72ab3462018-12-10 12:43:36 -0500865} // namespace GrStrokeRectOp
joshualitt9ff64252015-08-10 09:03:51 -0700866
Hal Canary6f6961e2017-01-31 13:50:44 -0500867#if GR_TEST_UTILS
joshualitt9ff64252015-08-10 09:03:51 -0700868
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500869#include "src/gpu/GrDrawOpTest.h"
joshualitt9ff64252015-08-10 09:03:51 -0700870
Michael Ludwig72ab3462018-12-10 12:43:36 -0500871GR_DRAW_OP_TEST_DEFINE(NonAAStrokeRectOp) {
872 SkMatrix viewMatrix = GrTest::TestMatrix(random);
873 SkRect rect = GrTest::TestRect(random);
874 SkScalar strokeWidth = random->nextBool() ? 0.0f : 2.0f;
875 SkPaint strokePaint;
876 strokePaint.setStrokeWidth(strokeWidth);
877 strokePaint.setStyle(SkPaint::kStroke_Style);
878 strokePaint.setStrokeJoin(SkPaint::kMiter_Join);
879 SkStrokeRec strokeRec(strokePaint);
880 GrAAType aaType = GrAAType::kNone;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600881 if (numSamples > 1) {
Michael Ludwig72ab3462018-12-10 12:43:36 -0500882 aaType = random->nextBool() ? GrAAType::kMSAA : GrAAType::kNone;
883 }
884 return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, strokeRec, aaType);
885}
886
Brian Salomonbaaf4392017-06-15 09:59:23 -0400887GR_DRAW_OP_TEST_DEFINE(AAStrokeRectOp) {
joshualitt9ff64252015-08-10 09:03:51 -0700888 bool miterStroke = random->nextBool();
889
bsalomon40ef4852016-05-02 13:22:13 -0700890 // Create either a empty rect or a non-empty rect.
Brian Salomon6a639042016-12-14 11:08:17 -0500891 SkRect rect =
892 random->nextBool() ? SkRect::MakeXYWH(10, 10, 50, 40) : SkRect::MakeXYWH(6, 7, 0, 0);
Brian Osman116b33e2020-02-05 13:34:09 -0500893 SkScalar minDim = std::min(rect.width(), rect.height());
bsalomon40ef4852016-05-02 13:22:13 -0700894 SkScalar strokeWidth = random->nextUScalar1() * minDim;
joshualitt9ff64252015-08-10 09:03:51 -0700895
bsalomon40ef4852016-05-02 13:22:13 -0700896 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
897 rec.setStrokeStyle(strokeWidth);
898 rec.setStrokeParams(SkPaint::kButt_Cap,
Brian Salomon6a639042016-12-14 11:08:17 -0500899 miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f);
bsalomon40ef4852016-05-02 13:22:13 -0700900 SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500901 return AAStrokeRectOp::Make(context, std::move(paint), matrix, rect, rec);
joshualitt9ff64252015-08-10 09:03:51 -0700902}
903
904#endif