• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "src/gpu/ops/StrokeRectOp.h"
9 
10 #include "include/core/SkStrokeRec.h"
11 #include "include/private/GrResourceKey.h"
12 #include "include/utils/SkRandom.h"
13 #include "src/core/SkMatrixPriv.h"
14 #include "src/gpu/BufferWriter.h"
15 #include "src/gpu/GrCaps.h"
16 #include "src/gpu/GrColor.h"
17 #include "src/gpu/GrDefaultGeoProcFactory.h"
18 #include "src/gpu/GrDrawOpTest.h"
19 #include "src/gpu/GrOpFlushState.h"
20 #include "src/gpu/GrProgramInfo.h"
21 #include "src/gpu/GrResourceProvider.h"
22 #include "src/gpu/geometry/GrQuad.h"
23 #include "src/gpu/ops/FillRectOp.h"
24 #include "src/gpu/ops/GrMeshDrawOp.h"
25 #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
26 
27 namespace skgpu::v1::StrokeRectOp {
28 
29 namespace {
30 
31 // We support all hairlines, bevels, and miters, but not round joins. Also, check whether the miter
32 // limit makes a miter join effectively beveled. If the miter is effectively beveled, it is only
33 // supported when using an AA stroke.
allowed_stroke(const SkStrokeRec & stroke,GrAA aa,bool * isMiter)34 inline bool allowed_stroke(const SkStrokeRec& stroke, GrAA aa, bool* isMiter) {
35     SkASSERT(stroke.getStyle() == SkStrokeRec::kStroke_Style ||
36              stroke.getStyle() == SkStrokeRec::kHairline_Style);
37     // For hairlines, make bevel and round joins appear the same as mitered ones.
38     if (!stroke.getWidth()) {
39         *isMiter = true;
40         return true;
41     }
42     if (stroke.getJoin() == SkPaint::kBevel_Join) {
43         *isMiter = false;
44         return aa == GrAA::kYes; // bevel only supported with AA
45     }
46     if (stroke.getJoin() == SkPaint::kMiter_Join) {
47         *isMiter = stroke.getMiter() >= SK_ScalarSqrt2;
48         // Supported under non-AA only if it remains mitered
49         return aa == GrAA::kYes || *isMiter;
50     }
51     return false;
52 }
53 
54 
55 ///////////////////////////////////////////////////////////////////////////////////////////////////
56 // Non-AA Stroking
57 ///////////////////////////////////////////////////////////////////////////////////////////////////
58 
59 /*  create a triangle strip that strokes the specified rect. There are 8
60     unique vertices, but we repeat the last 2 to close up. Alternatively we
61     could use an indices array, and then only send 8 verts, but not sure that
62     would be faster.
63     */
init_nonaa_stroke_rect_strip(SkPoint verts[10],const SkRect & rect,SkScalar width)64 void init_nonaa_stroke_rect_strip(SkPoint verts[10], const SkRect& rect, SkScalar width) {
65     const SkScalar rad = SkScalarHalf(width);
66 
67     verts[0].set(rect.fLeft + rad, rect.fTop + rad);
68     verts[1].set(rect.fLeft - rad, rect.fTop - rad);
69     verts[2].set(rect.fRight - rad, rect.fTop + rad);
70     verts[3].set(rect.fRight + rad, rect.fTop - rad);
71     verts[4].set(rect.fRight - rad, rect.fBottom - rad);
72     verts[5].set(rect.fRight + rad, rect.fBottom + rad);
73     verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
74     verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
75     verts[8] = verts[0];
76     verts[9] = verts[1];
77 
78     // TODO: we should be catching this higher up the call stack and just draw a single
79     // non-AA rect
80     if (2*rad >= rect.width()) {
81         verts[0].fX = verts[2].fX = verts[4].fX = verts[6].fX = verts[8].fX = rect.centerX();
82     }
83     if (2*rad >= rect.height()) {
84         verts[0].fY = verts[2].fY = verts[4].fY = verts[6].fY = verts[8].fY = rect.centerY();
85     }
86 }
87 
88 class NonAAStrokeRectOp final : public GrMeshDrawOp {
89 private:
90     using Helper = GrSimpleMeshDrawOpHelper;
91 
92 public:
93     DEFINE_OP_CLASS_ID
94 
name() const95     const char* name() const override { return "NonAAStrokeRectOp"; }
96 
visitProxies(const GrVisitProxyFunc & func) const97     void visitProxies(const GrVisitProxyFunc& func) const override {
98         if (fProgramInfo) {
99             fProgramInfo->visitFPProxies(func);
100         } else {
101             fHelper.visitProxies(func);
102         }
103     }
104 
Make(GrRecordingContext * context,GrPaint && paint,const SkMatrix & viewMatrix,const SkRect & rect,const SkStrokeRec & stroke,GrAAType aaType)105     static GrOp::Owner Make(GrRecordingContext* context,
106                             GrPaint&& paint,
107                             const SkMatrix& viewMatrix,
108                             const SkRect& rect,
109                             const SkStrokeRec& stroke,
110                             GrAAType aaType) {
111         bool isMiter;
112         if (!allowed_stroke(stroke, GrAA::kNo, &isMiter)) {
113             return nullptr;
114         }
115         Helper::InputFlags inputFlags = Helper::InputFlags::kNone;
116         // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of
117         // hairline rects. We jam all the vertices to pixel centers to avoid this, but not
118         // when MSAA is enabled because it can cause ugly artifacts.
119         if (stroke.getStyle() == SkStrokeRec::kHairline_Style && aaType != GrAAType::kMSAA) {
120             inputFlags |= Helper::InputFlags::kSnapVerticesToPixelCenters;
121         }
122         return Helper::FactoryHelper<NonAAStrokeRectOp>(context, std::move(paint), inputFlags,
123                                                         viewMatrix, rect,
124                                                         stroke, aaType);
125     }
126 
NonAAStrokeRectOp(GrProcessorSet * processorSet,const SkPMColor4f & color,Helper::InputFlags inputFlags,const SkMatrix & viewMatrix,const SkRect & rect,const SkStrokeRec & stroke,GrAAType aaType)127     NonAAStrokeRectOp(GrProcessorSet* processorSet, const SkPMColor4f& color,
128                       Helper::InputFlags inputFlags, const SkMatrix& viewMatrix, const SkRect& rect,
129                       const SkStrokeRec& stroke, GrAAType aaType)
130             : INHERITED(ClassID())
131             , fHelper(processorSet, aaType, inputFlags) {
132         fColor = color;
133         fViewMatrix = viewMatrix;
134         fRect = rect;
135         // Sort the rect for hairlines
136         fRect.sort();
137         fStrokeWidth = stroke.getWidth();
138 
139         SkScalar rad = SkScalarHalf(fStrokeWidth);
140         SkRect bounds = rect;
141         bounds.outset(rad, rad);
142 
143         // If our caller snaps to pixel centers then we have to round out the bounds
144         if (inputFlags & Helper::InputFlags::kSnapVerticesToPixelCenters) {
145             SkASSERT(!fStrokeWidth || aaType == GrAAType::kNone);
146             viewMatrix.mapRect(&bounds);
147             // We want to be consistent with how we snap non-aa lines. To match what we do in
148             // GrGLSLVertexShaderBuilder, we first floor all the vertex values and then add half a
149             // pixel to force us to pixel centers.
150             bounds.setLTRB(SkScalarFloorToScalar(bounds.fLeft),
151                            SkScalarFloorToScalar(bounds.fTop),
152                            SkScalarFloorToScalar(bounds.fRight),
153                            SkScalarFloorToScalar(bounds.fBottom));
154             bounds.offset(0.5f, 0.5f);
155             this->setBounds(bounds, HasAABloat::kNo, IsHairline::kNo);
156         } else {
157             HasAABloat aaBloat = (aaType == GrAAType::kNone) ? HasAABloat ::kNo : HasAABloat::kYes;
158             this->setTransformedBounds(bounds, fViewMatrix, aaBloat,
159                                        fStrokeWidth ? IsHairline::kNo : IsHairline::kYes);
160         }
161     }
162 
fixedFunctionFlags() const163     FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
164 
finalize(const GrCaps & caps,const GrAppliedClip * clip,GrClampType clampType)165     GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
166                                       GrClampType clampType) override {
167         // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
168         return fHelper.finalizeProcessors(caps, clip, clampType, GrProcessorAnalysisCoverage::kNone,
169                                           &fColor, nullptr);
170     }
171 
172 private:
programInfo()173     GrProgramInfo* programInfo() override { return fProgramInfo; }
174 
onCreateProgramInfo(const GrCaps * caps,SkArenaAlloc * arena,const GrSurfaceProxyView & writeView,bool usesMSAASurface,GrAppliedClip && clip,const GrDstProxyView & dstProxyView,GrXferBarrierFlags renderPassXferBarriers,GrLoadOp colorLoadOp)175     void onCreateProgramInfo(const GrCaps* caps,
176                              SkArenaAlloc* arena,
177                              const GrSurfaceProxyView& writeView,
178                              bool usesMSAASurface,
179                              GrAppliedClip&& clip,
180                              const GrDstProxyView& dstProxyView,
181                              GrXferBarrierFlags renderPassXferBarriers,
182                              GrLoadOp colorLoadOp) override {
183         GrGeometryProcessor* gp;
184         {
185             using namespace GrDefaultGeoProcFactory;
186             Color color(fColor);
187             LocalCoords::Type localCoordsType = fHelper.usesLocalCoords()
188                                                         ? LocalCoords::kUsePosition_Type
189                                                         : LocalCoords::kUnused_Type;
190             gp = GrDefaultGeoProcFactory::Make(arena, color, Coverage::kSolid_Type, localCoordsType,
191                                                fViewMatrix);
192         }
193 
194         GrPrimitiveType primType = (fStrokeWidth > 0) ? GrPrimitiveType::kTriangleStrip
195                                                       : GrPrimitiveType::kLineStrip;
196 
197         fProgramInfo = fHelper.createProgramInfo(caps, arena, writeView, usesMSAASurface,
198                                                  std::move(clip), dstProxyView, gp, primType,
199                                                  renderPassXferBarriers, colorLoadOp);
200     }
201 
onPrepareDraws(GrMeshDrawTarget * target)202     void onPrepareDraws(GrMeshDrawTarget* target) override {
203         if (!fProgramInfo) {
204             this->createProgramInfo(target);
205         }
206 
207         size_t kVertexStride = fProgramInfo->geomProc().vertexStride();
208         int vertexCount = kVertsPerHairlineRect;
209         if (fStrokeWidth > 0) {
210             vertexCount = kVertsPerStrokeRect;
211         }
212 
213         sk_sp<const GrBuffer> vertexBuffer;
214         int firstVertex;
215 
216         void* verts =
217                 target->makeVertexSpace(kVertexStride, vertexCount, &vertexBuffer, &firstVertex);
218 
219         if (!verts) {
220             SkDebugf("Could not allocate vertices\n");
221             return;
222         }
223 
224         SkPoint* vertex = reinterpret_cast<SkPoint*>(verts);
225 
226         if (fStrokeWidth > 0) {
227             init_nonaa_stroke_rect_strip(vertex, fRect, fStrokeWidth);
228         } else {
229             // hairline
230             vertex[0].set(fRect.fLeft, fRect.fTop);
231             vertex[1].set(fRect.fRight, fRect.fTop);
232             vertex[2].set(fRect.fRight, fRect.fBottom);
233             vertex[3].set(fRect.fLeft, fRect.fBottom);
234             vertex[4].set(fRect.fLeft, fRect.fTop);
235         }
236 
237         fMesh = target->allocMesh();
238         fMesh->set(std::move(vertexBuffer), vertexCount, firstVertex);
239     }
240 
onExecute(GrOpFlushState * flushState,const SkRect & chainBounds)241     void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
242         if (!fMesh) {
243             return;
244         }
245 
246         flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
247         flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
248         flushState->drawMesh(*fMesh);
249     }
250 
251 #if GR_TEST_UTILS
onDumpInfo() const252     SkString onDumpInfo() const override {
253         return SkStringPrintf("Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
254                               "StrokeWidth: %.2f\n%s",
255                               fColor.toBytes_RGBA(), fRect.fLeft, fRect.fTop, fRect.fRight,
256                               fRect.fBottom, fStrokeWidth, fHelper.dumpInfo().c_str());
257     }
258 #endif
259 
260     // TODO: override onCombineIfPossible
261 
262     Helper         fHelper;
263     SkPMColor4f    fColor;
264     SkMatrix       fViewMatrix;
265     SkRect         fRect;
266     SkScalar       fStrokeWidth;
267     GrSimpleMesh*  fMesh = nullptr;
268     GrProgramInfo* fProgramInfo = nullptr;
269 
270     const static int kVertsPerHairlineRect = 5;
271     const static int kVertsPerStrokeRect = 10;
272 
273     using INHERITED = GrMeshDrawOp;
274 };
275 
276 ///////////////////////////////////////////////////////////////////////////////////////////////////
277 // AA Stroking
278 ///////////////////////////////////////////////////////////////////////////////////////////////////
279 
280 GR_DECLARE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
281 GR_DECLARE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
282 
stroke_dev_half_size_supported(SkVector devHalfStrokeSize)283 bool stroke_dev_half_size_supported(SkVector devHalfStrokeSize) {
284     // Since the horizontal and vertical strokes share internal corners, the coverage value at that
285     // corner needs to be equal for the horizontal and vertical strokes both.
286     //
287     // The inner coverage values will be equal if the horizontal and vertical stroke widths are
288     // equal (in which case innerCoverage is same for all sides of the rects) or if the horizontal
289     // and vertical stroke widths are both greater than 1 (in which case innerCoverage will always
290     // be 1). In actuality we allow them to be nearly-equal since differing by < 1/1000 will not be
291     // visually detectable when the shape is already less than 1px in thickness.
292     return SkScalarNearlyEqual(devHalfStrokeSize.fX, devHalfStrokeSize.fY) ||
293            std::min(devHalfStrokeSize.fX, devHalfStrokeSize.fY) >= .5f;
294 }
295 
compute_aa_rects(const GrCaps & caps,SkRect * devOutside,SkRect * devOutsideAssist,SkRect * devInside,bool * isDegenerate,const SkMatrix & viewMatrix,const SkRect & rect,SkScalar strokeWidth,bool miterStroke,SkVector * devHalfStrokeSize)296 bool compute_aa_rects(const GrCaps& caps,
297                       SkRect* devOutside,
298                       SkRect* devOutsideAssist,
299                       SkRect* devInside,
300                       bool* isDegenerate,
301                       const SkMatrix& viewMatrix,
302                       const SkRect& rect,
303                       SkScalar strokeWidth,
304                       bool miterStroke,
305                       SkVector* devHalfStrokeSize) {
306     SkVector devStrokeSize;
307     if (strokeWidth > 0) {
308         devStrokeSize.set(strokeWidth, strokeWidth);
309         viewMatrix.mapVectors(&devStrokeSize, 1);
310         devStrokeSize.setAbs(devStrokeSize);
311     } else {
312         devStrokeSize.set(SK_Scalar1, SK_Scalar1);
313     }
314 
315     const SkScalar dx = devStrokeSize.fX;
316     const SkScalar dy = devStrokeSize.fY;
317     const SkScalar rx = SkScalarHalf(dx);
318     const SkScalar ry = SkScalarHalf(dy);
319 
320     devHalfStrokeSize->fX = rx;
321     devHalfStrokeSize->fY = ry;
322 
323     SkRect devRect;
324     viewMatrix.mapRect(&devRect, rect);
325 
326     // Clip our draw rect 1 full stroke width plus bloat outside the viewport. This avoids
327     // interpolation precision issues with very large coordinates.
328     const float m = caps.maxRenderTargetSize();
329     const SkRect visibilityBounds = SkRect::MakeWH(m, m).makeOutset(dx + 1, dy + 1);
330     if (!devRect.intersect(visibilityBounds)) {
331         return false;
332     }
333 
334     *devOutside = devRect;
335     *devOutsideAssist = devRect;
336     *devInside = devRect;
337 
338     devOutside->outset(rx, ry);
339     devInside->inset(rx, ry);
340 
341     // If we have a degenerate stroking rect(ie the stroke is larger than inner rect) then we
342     // make a degenerate inside rect to avoid double hitting.  We will also jam all of the points
343     // together when we render these rects.
344     SkScalar spare;
345     {
346         SkScalar w = devRect.width() - dx;
347         SkScalar h = devRect.height() - dy;
348         spare = std::min(w, h);
349     }
350 
351     *isDegenerate = spare <= 0;
352     if (*isDegenerate) {
353         devInside->fLeft = devInside->fRight = devRect.centerX();
354         devInside->fTop = devInside->fBottom = devRect.centerY();
355     }
356 
357     // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist)
358     // to draw the outside of the octagon. Because there are 8 vertices on the outer
359     // edge, while vertex number of inner edge is 4, the same as miter-stroke.
360     if (!miterStroke) {
361         devOutside->inset(0, ry);
362         devOutsideAssist->outset(0, ry);
363     }
364 
365     return true;
366 }
367 
create_aa_stroke_rect_gp(SkArenaAlloc * arena,bool usesMSAASurface,bool tweakAlphaForCoverage,const SkMatrix & viewMatrix,bool usesLocalCoords,bool wideColor)368 GrGeometryProcessor* create_aa_stroke_rect_gp(SkArenaAlloc* arena,
369                                               bool usesMSAASurface,
370                                               bool tweakAlphaForCoverage,
371                                               const SkMatrix& viewMatrix,
372                                               bool usesLocalCoords,
373                                               bool wideColor) {
374     using namespace GrDefaultGeoProcFactory;
375 
376     // When MSAA is enabled, we have to extend our AA bloats and interpolate coverage values outside
377     // 0..1. We tell the gp in this case that coverage is an unclamped attribute so it will call
378     // saturate(coverage) in the fragment shader.
379     Coverage::Type coverageType = usesMSAASurface ? Coverage::kAttributeUnclamped_Type
380                         : (!tweakAlphaForCoverage ? Coverage::kAttribute_Type
381                                                   : Coverage::kSolid_Type);
382     LocalCoords::Type localCoordsType =
383         usesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type;
384     Color::Type colorType =
385         wideColor ? Color::kPremulWideColorAttribute_Type: Color::kPremulGrColorAttribute_Type;
386 
387     return MakeForDeviceSpace(arena, colorType, coverageType, localCoordsType, viewMatrix);
388 }
389 
390 class AAStrokeRectOp final : public GrMeshDrawOp {
391 private:
392     using Helper = GrSimpleMeshDrawOpHelper;
393 
394 public:
395     DEFINE_OP_CLASS_ID
396 
397     // TODO support AA rotated stroke rects by copying around view matrices
398     struct RectInfo {
399         SkPMColor4f fColor;
400         SkRect      fDevOutside;
401         SkRect      fDevOutsideAssist;
402         SkRect      fDevInside;
403         SkVector    fDevHalfStrokeSize;
404         bool        fDegenerate;
405     };
406 
Make(GrRecordingContext * context,GrPaint && paint,const SkMatrix & viewMatrix,const SkRect & devOutside,const SkRect & devInside,const SkVector & devHalfStrokeSize)407     static GrOp::Owner Make(GrRecordingContext* context,
408                             GrPaint&& paint,
409                             const SkMatrix& viewMatrix,
410                             const SkRect& devOutside,
411                             const SkRect& devInside,
412                             const SkVector& devHalfStrokeSize) {
413         if (!viewMatrix.rectStaysRect()) {
414             // The AA op only supports axis-aligned rectangles
415             return nullptr;
416         }
417         if (!stroke_dev_half_size_supported(devHalfStrokeSize)) {
418             return nullptr;
419         }
420         return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix,
421                                                      devOutside, devInside, devHalfStrokeSize);
422     }
423 
AAStrokeRectOp(GrProcessorSet * processorSet,const SkPMColor4f & color,const SkMatrix & viewMatrix,const SkRect & devOutside,const SkRect & devInside,const SkVector & devHalfStrokeSize)424     AAStrokeRectOp(GrProcessorSet* processorSet, const SkPMColor4f& color,
425                    const SkMatrix& viewMatrix, const SkRect& devOutside, const SkRect& devInside,
426                    const SkVector& devHalfStrokeSize)
427             : INHERITED(ClassID())
428             , fHelper(processorSet, GrAAType::kCoverage)
429             , fViewMatrix(viewMatrix) {
430         SkASSERT(!devOutside.isEmpty());
431         SkASSERT(!devInside.isEmpty());
432 
433         fRects.emplace_back(RectInfo{color, devOutside, devOutside, devInside, devHalfStrokeSize, false});
434         this->setBounds(devOutside, HasAABloat::kYes, IsHairline::kNo);
435         fMiterStroke = true;
436     }
437 
Make(GrRecordingContext * context,GrPaint && paint,const SkMatrix & viewMatrix,const SkRect & rect,const SkStrokeRec & stroke)438     static GrOp::Owner Make(GrRecordingContext* context,
439                             GrPaint&& paint,
440                             const SkMatrix& viewMatrix,
441                             const SkRect& rect,
442                             const SkStrokeRec& stroke) {
443         if (!viewMatrix.rectStaysRect()) {
444             // The AA op only supports axis-aligned rectangles
445             return nullptr;
446         }
447         bool isMiter;
448         if (!allowed_stroke(stroke, GrAA::kYes, &isMiter)) {
449             return nullptr;
450         }
451         RectInfo info;
452         if (!compute_aa_rects(*context->priv().caps(),
453                               &info.fDevOutside,
454                               &info.fDevOutsideAssist,
455                               &info.fDevInside,
456                               &info.fDegenerate,
457                               viewMatrix,
458                               rect,
459                               stroke.getWidth(),
460                               isMiter,
461                               &info.fDevHalfStrokeSize)) {
462             return nullptr;
463         }
464         if (!stroke_dev_half_size_supported(info.fDevHalfStrokeSize)) {
465             return nullptr;
466         }
467         return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix, info,
468                                                      isMiter);
469     }
470 
AAStrokeRectOp(GrProcessorSet * processorSet,const SkPMColor4f & color,const SkMatrix & viewMatrix,const RectInfo & infoExceptColor,bool isMiter)471     AAStrokeRectOp(GrProcessorSet* processorSet, const SkPMColor4f& color,
472                    const SkMatrix& viewMatrix, const RectInfo& infoExceptColor, bool isMiter)
473             : INHERITED(ClassID())
474             , fHelper(processorSet, GrAAType::kCoverage)
475             , fViewMatrix(viewMatrix) {
476         fMiterStroke = isMiter;
477         RectInfo& info = fRects.push_back(infoExceptColor);
478         info.fColor = color;
479         if (isMiter) {
480             this->setBounds(info.fDevOutside, HasAABloat::kYes, IsHairline::kNo);
481         } else {
482             // The outer polygon of the bevel stroke is an octagon specified by the points of a
483             // pair of overlapping rectangles where one is wide and the other is narrow.
484             SkRect bounds = info.fDevOutside;
485             bounds.joinPossiblyEmptyRect(info.fDevOutsideAssist);
486             this->setBounds(bounds, HasAABloat::kYes, IsHairline::kNo);
487         }
488     }
489 
name() const490     const char* name() const override { return "AAStrokeRect"; }
491 
visitProxies(const GrVisitProxyFunc & func) const492     void visitProxies(const GrVisitProxyFunc& func) const override {
493         if (fProgramInfo) {
494             fProgramInfo->visitFPProxies(func);
495         } else {
496             fHelper.visitProxies(func);
497         }
498     }
499 
fixedFunctionFlags() const500     FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
501 
finalize(const GrCaps & caps,const GrAppliedClip * clip,GrClampType clampType)502     GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
503                                       GrClampType clampType) override {
504         return fHelper.finalizeProcessors(caps, clip, clampType,
505                                           GrProcessorAnalysisCoverage::kSingleChannel,
506                                           &fRects.back().fColor, &fWideColor);
507     }
508 
509 private:
programInfo()510     GrProgramInfo* programInfo() override { return fProgramInfo; }
511 
compatibleWithCoverageAsAlpha(bool usesMSAASurface) const512     bool compatibleWithCoverageAsAlpha(bool usesMSAASurface) const {
513         // When MSAA is enabled, we have to extend our AA bloats and interpolate coverage values
514         // outside 0..1. This makes us incompatible with coverage as alpha.
515         return !usesMSAASurface && fHelper.compatibleWithCoverageAsAlpha();
516     }
517 
518     void onCreateProgramInfo(const GrCaps*,
519                              SkArenaAlloc*,
520                              const GrSurfaceProxyView& writeView,
521                              bool usesMSAASurface,
522                              GrAppliedClip&&,
523                              const GrDstProxyView&,
524                              GrXferBarrierFlags renderPassXferBarriers,
525                              GrLoadOp colorLoadOp) override;
526 
527     void onPrepareDraws(GrMeshDrawTarget*) override;
528     void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
529 
530 #if GR_TEST_UTILS
onDumpInfo() const531     SkString onDumpInfo() const override {
532         SkString string;
533         for (const auto& info : fRects) {
534             string.appendf(
535                     "Color: 0x%08x, ORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
536                     "AssistORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
537                     "IRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], Degen: %d",
538                     info.fColor.toBytes_RGBA(), info.fDevOutside.fLeft, info.fDevOutside.fTop,
539                     info.fDevOutside.fRight, info.fDevOutside.fBottom, info.fDevOutsideAssist.fLeft,
540                     info.fDevOutsideAssist.fTop, info.fDevOutsideAssist.fRight,
541                     info.fDevOutsideAssist.fBottom, info.fDevInside.fLeft, info.fDevInside.fTop,
542                     info.fDevInside.fRight, info.fDevInside.fBottom, info.fDegenerate);
543         }
544         string += fHelper.dumpInfo();
545         return string;
546     }
547 #endif
548 
549     static const int kMiterIndexCnt = 3 * 24;
550     static const int kMiterVertexCnt = 16;
551     static const int kNumMiterRectsInIndexBuffer = 256;
552 
553     static const int kBevelIndexCnt = 48 + 36 + 24;
554     static const int kBevelVertexCnt = 24;
555     static const int kNumBevelRectsInIndexBuffer = 256;
556 
557     static sk_sp<const GrGpuBuffer> GetIndexBuffer(GrResourceProvider*, bool miterStroke);
558 
viewMatrix() const559     const SkMatrix& viewMatrix() const { return fViewMatrix; }
miterStroke() const560     bool miterStroke() const { return fMiterStroke; }
561 
562     CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps&) override;
563 
564     void generateAAStrokeRectGeometry(VertexWriter& vertices,
565                                       const SkPMColor4f& color,
566                                       bool wideColor,
567                                       const SkRect& devOutside,
568                                       const SkRect& devOutsideAssist,
569                                       const SkRect& devInside,
570                                       bool miterStroke,
571                                       bool degenerate,
572                                       const SkVector& devHalfStrokeSize,
573                                       bool usesMSAASurface) const;
574 
575     Helper         fHelper;
576     SkSTArray<1, RectInfo, true> fRects;
577     SkMatrix       fViewMatrix;
578     GrSimpleMesh*  fMesh = nullptr;
579     GrProgramInfo* fProgramInfo = nullptr;
580     bool           fMiterStroke;
581     bool           fWideColor;
582 
583     using INHERITED = GrMeshDrawOp;
584 };
585 
onCreateProgramInfo(const GrCaps * caps,SkArenaAlloc * arena,const GrSurfaceProxyView & writeView,bool usesMSAASurface,GrAppliedClip && appliedClip,const GrDstProxyView & dstProxyView,GrXferBarrierFlags renderPassXferBarriers,GrLoadOp colorLoadOp)586 void AAStrokeRectOp::onCreateProgramInfo(const GrCaps* caps,
587                                          SkArenaAlloc* arena,
588                                          const GrSurfaceProxyView& writeView,
589                                          bool usesMSAASurface,
590                                          GrAppliedClip&& appliedClip,
591                                          const GrDstProxyView& dstProxyView,
592                                          GrXferBarrierFlags renderPassXferBarriers,
593                                          GrLoadOp colorLoadOp) {
594 
595     GrGeometryProcessor* gp = create_aa_stroke_rect_gp(
596             arena,
597             usesMSAASurface,
598             this->compatibleWithCoverageAsAlpha(usesMSAASurface),
599             this->viewMatrix(),
600             fHelper.usesLocalCoords(),
601             fWideColor);
602     if (!gp) {
603         SkDebugf("Couldn't create GrGeometryProcessor\n");
604         return;
605     }
606 
607     fProgramInfo = fHelper.createProgramInfo(caps,
608                                              arena,
609                                              writeView,
610                                              usesMSAASurface,
611                                              std::move(appliedClip),
612                                              dstProxyView,
613                                              gp,
614                                              GrPrimitiveType::kTriangles,
615                                              renderPassXferBarriers,
616                                              colorLoadOp);
617 }
618 
onPrepareDraws(GrMeshDrawTarget * target)619 void AAStrokeRectOp::onPrepareDraws(GrMeshDrawTarget* target) {
620 
621     if (!fProgramInfo) {
622         this->createProgramInfo(target);
623         if (!fProgramInfo) {
624             return;
625         }
626     }
627 
628     int innerVertexNum = 4;
629     int outerVertexNum = this->miterStroke() ? 4 : 8;
630     int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2;
631     int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt;
632     int instanceCount = fRects.count();
633     int maxQuads = this->miterStroke() ? kNumMiterRectsInIndexBuffer : kNumBevelRectsInIndexBuffer;
634 
635     sk_sp<const GrGpuBuffer> indexBuffer =
636             GetIndexBuffer(target->resourceProvider(), this->miterStroke());
637     if (!indexBuffer) {
638         SkDebugf("Could not allocate indices\n");
639         return;
640     }
641     PatternHelper helper(target, GrPrimitiveType::kTriangles,
642                          fProgramInfo->geomProc().vertexStride(), std::move(indexBuffer),
643                          verticesPerInstance, indicesPerInstance, instanceCount, maxQuads);
644     VertexWriter vertices{ helper.vertices() };
645     if (!vertices) {
646         SkDebugf("Could not allocate vertices\n");
647         return;
648     }
649 
650     for (int i = 0; i < instanceCount; i++) {
651         const RectInfo& info = fRects[i];
652         this->generateAAStrokeRectGeometry(vertices,
653                                            info.fColor,
654                                            fWideColor,
655                                            info.fDevOutside,
656                                            info.fDevOutsideAssist,
657                                            info.fDevInside,
658                                            fMiterStroke,
659                                            info.fDegenerate,
660                                            info.fDevHalfStrokeSize,
661                                            target->usesMSAASurface());
662     }
663     fMesh = helper.mesh();
664 }
665 
onExecute(GrOpFlushState * flushState,const SkRect & chainBounds)666 void AAStrokeRectOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
667     if (!fProgramInfo || !fMesh) {
668         return;
669     }
670 
671     flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
672     flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
673     flushState->drawMesh(*fMesh);
674 }
675 
GetIndexBuffer(GrResourceProvider * resourceProvider,bool miterStroke)676 sk_sp<const GrGpuBuffer> AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider,
677                                                         bool miterStroke) {
678     if (miterStroke) {
679         // clang-format off
680         static const uint16_t gMiterIndices[] = {
681             0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0,
682             1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0,
683             2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0,
684             3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0,
685 
686             0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4,
687             1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4,
688             2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4,
689             3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4,
690 
691             0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8,
692             1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8,
693             2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8,
694             3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8,
695         };
696         // clang-format on
697         static_assert(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt);
698         GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
699         return resourceProvider->findOrCreatePatternedIndexBuffer(
700                 gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt,
701                 gMiterIndexBufferKey);
702     } else {
703         /**
704          * As in miter-stroke, index = a + b, and a is the current index, b is the shift
705          * from the first index. The index layout:
706          * outer AA line: 0~3, 4~7
707          * outer edge:    8~11, 12~15
708          * inner edge:    16~19
709          * inner AA line: 20~23
710          * Following comes a bevel-stroke rect and its indices:
711          *
712          *           4                                 7
713          *            *********************************
714          *          *   ______________________________  *
715          *         *  / 12                          15 \  *
716          *        *  /                                  \  *
717          *     0 *  |8     16_____________________19  11 |  * 3
718          *       *  |       |                    |       |  *
719          *       *  |       |  ****************  |       |  *
720          *       *  |       |  * 20        23 *  |       |  *
721          *       *  |       |  *              *  |       |  *
722          *       *  |       |  * 21        22 *  |       |  *
723          *       *  |       |  ****************  |       |  *
724          *       *  |       |____________________|       |  *
725          *     1 *  |9    17                      18   10|  * 2
726          *        *  \                                  /  *
727          *         *  \13 __________________________14/  *
728          *          *                                   *
729          *           **********************************
730          *          5                                  6
731          */
732         // clang-format off
733         static const uint16_t gBevelIndices[] = {
734             // Draw outer AA, from outer AA line to outer edge, shift is 0.
735             0 + 0, 1 + 0,  9 + 0,  9 + 0,  8 + 0, 0 + 0,
736             1 + 0, 5 + 0, 13 + 0, 13 + 0,  9 + 0, 1 + 0,
737             5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0,
738             6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0,
739             2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0,
740             3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0,
741             7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0,
742             4 + 0, 0 + 0,  8 + 0,  8 + 0, 12 + 0, 4 + 0,
743 
744             // Draw the stroke, from outer edge to inner edge, shift is 8.
745             0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8,
746             1 + 8, 5 + 8, 9 + 8,
747             5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8,
748             6 + 8, 2 + 8, 10 + 8,
749             2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8,
750             3 + 8, 7 + 8, 11 + 8,
751             7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8,
752             4 + 8, 0 + 8, 8 + 8,
753 
754             // Draw the inner AA, from inner edge to inner AA line, shift is 16.
755             0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16,
756             1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16,
757             2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16,
758             3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16,
759         };
760         // clang-format on
761         static_assert(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt);
762 
763         GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
764         return resourceProvider->findOrCreatePatternedIndexBuffer(
765                 gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt,
766                 gBevelIndexBufferKey);
767     }
768 }
769 
onCombineIfPossible(GrOp * t,SkArenaAlloc *,const GrCaps & caps)770 GrOp::CombineResult AAStrokeRectOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps)
771 {
772     AAStrokeRectOp* that = t->cast<AAStrokeRectOp>();
773 
774     if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
775         return CombineResult::kCannotCombine;
776     }
777 
778     // TODO combine across miterstroke changes
779     if (this->miterStroke() != that->miterStroke()) {
780         return CombineResult::kCannotCombine;
781     }
782 
783     // We apply the viewmatrix to the rect points on the cpu.  However, if the pipeline uses
784     // local coords then we won't be able to combine. TODO: Upload local coords as an attribute.
785     if (fHelper.usesLocalCoords() &&
786         !SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix()))
787     {
788         return CombineResult::kCannotCombine;
789     }
790 
791     fRects.push_back_n(that->fRects.count(), that->fRects.begin());
792     fWideColor |= that->fWideColor;
793     return CombineResult::kMerged;
794 }
795 
generateAAStrokeRectGeometry(VertexWriter & vertices,const SkPMColor4f & color,bool wideColor,const SkRect & devOutside,const SkRect & devOutsideAssist,const SkRect & devInside,bool miterStroke,bool degenerate,const SkVector & devHalfStrokeSize,bool usesMSAASurface) const796 void AAStrokeRectOp::generateAAStrokeRectGeometry(VertexWriter& vertices,
797                                                   const SkPMColor4f& color,
798                                                   bool wideColor,
799                                                   const SkRect& devOutside,
800                                                   const SkRect& devOutsideAssist,
801                                                   const SkRect& devInside,
802                                                   bool miterStroke,
803                                                   bool degenerate,
804                                                   const SkVector& devHalfStrokeSize,
805                                                   bool usesMSAASurface) const {
806     // We create vertices for four nested rectangles. There are two ramps from 0 to full
807     // coverage, one on the exterior of the stroke and the other on the interior.
808 
809     // The following code only works if either devStrokeSize's fX and fY are
810     // equal (in which case innerCoverage is same for all sides of the rects) or
811     // if devStrokeSize's fX and fY are both greater than 1.0 (in which case
812     // innerCoverage will always be 1).
813     SkASSERT(stroke_dev_half_size_supported(devHalfStrokeSize));
814 
815     auto inset_fan = [](const SkRect& r, SkScalar dx, SkScalar dy) {
816         return VertexWriter::TriFanFromRect(r.makeInset(dx, dy));
817     };
818 
819     bool tweakAlphaForCoverage = this->compatibleWithCoverageAsAlpha(usesMSAASurface);
820 
821     auto maybe_coverage = [tweakAlphaForCoverage](float coverage) {
822         return VertexWriter::If(!tweakAlphaForCoverage, coverage);
823     };
824 
825     // How much do we inset toward the inside of the strokes?
826     float inset = std::min(0.5f, std::min(devHalfStrokeSize.fX, devHalfStrokeSize.fY));
827     float innerCoverage = 1;
828     if (inset < 0.5f) {
829         // Stroke is subpixel, so reduce the coverage to simulate the narrower strokes.
830         innerCoverage = 2 * inset / (inset + .5f);
831     }
832 
833     // How much do we outset away from the outside of the strokes?
834     // We always want to keep the AA picture frame one pixel wide.
835     float outset = 1 - inset;
836     float outerCoverage = 0;
837 
838     // How much do we outset away from the interior side of the stroke (toward the center)?
839     float interiorOutset = outset;
840     float interiorCoverage = outerCoverage;
841 
842     if (usesMSAASurface) {
843         // Since we're using MSAA, extend our outsets to ensure any pixel with partial coverage has
844         // a full sample mask.
845         constexpr float msaaExtraBloat = SK_ScalarSqrt2 - .5f;
846         outset += msaaExtraBloat;
847         outerCoverage -= msaaExtraBloat;
848 
849         float insetExtraBloat =
850                 std::min(inset + msaaExtraBloat,
851                          std::min(devHalfStrokeSize.fX, devHalfStrokeSize.fY)) - inset;
852         inset += insetExtraBloat;
853         innerCoverage += insetExtraBloat;
854 
855         float interiorExtraBloat =
856                 std::min(interiorOutset + msaaExtraBloat,
857                          std::min(devInside.width(), devInside.height()) / 2) - interiorOutset;
858         interiorOutset += interiorExtraBloat;
859         interiorCoverage -= interiorExtraBloat;
860     }
861 
862     GrVertexColor innerColor(tweakAlphaForCoverage ? color * innerCoverage : color, wideColor);
863     GrVertexColor outerColor(tweakAlphaForCoverage ? SK_PMColor4fTRANSPARENT : color, wideColor);
864 
865     // Exterior outset rect (away from stroke).
866     vertices.writeQuad(inset_fan(devOutside, -outset, -outset),
867                        outerColor,
868                        maybe_coverage(outerCoverage));
869 
870     if (!miterStroke) {
871         // Second exterior outset.
872         vertices.writeQuad(inset_fan(devOutsideAssist, -outset, -outset),
873                            outerColor,
874                            maybe_coverage(outerCoverage));
875     }
876 
877     // Exterior inset rect (toward stroke).
878     vertices.writeQuad(inset_fan(devOutside, inset, inset),
879                        innerColor,
880                        maybe_coverage(innerCoverage));
881 
882     if (!miterStroke) {
883         // Second exterior inset.
884         vertices.writeQuad(inset_fan(devOutsideAssist, inset, inset),
885                            innerColor,
886                            maybe_coverage(innerCoverage));
887     }
888 
889     if (!degenerate) {
890         // Interior inset rect (toward stroke).
891         vertices.writeQuad(inset_fan(devInside, -inset, -inset),
892                            innerColor,
893                            maybe_coverage(innerCoverage));
894 
895         // Interior outset rect (away from stroke, toward center of rect).
896         SkRect interiorAABoundary = devInside.makeInset(interiorOutset, interiorOutset);
897         float coverageBackset = 0;  // Adds back coverage when the interior AA edges cross.
898         if (interiorAABoundary.fLeft > interiorAABoundary.fRight) {
899             coverageBackset =
900                     (interiorAABoundary.fLeft - interiorAABoundary.fRight) / (interiorOutset * 2);
901             interiorAABoundary.fLeft = interiorAABoundary.fRight = interiorAABoundary.centerX();
902         }
903         if (interiorAABoundary.fTop > interiorAABoundary.fBottom) {
904             coverageBackset = std::max(
905                     (interiorAABoundary.fTop - interiorAABoundary.fBottom) / (interiorOutset * 2),
906                     coverageBackset);
907             interiorAABoundary.fTop = interiorAABoundary.fBottom = interiorAABoundary.centerY();
908         }
909         if (coverageBackset > 0) {
910             // The interior edges crossed. Lerp back toward innerCoverage, which is what this op
911             // will draw in the degenerate case. This gives a smooth transition into the degenerate
912             // case.
913             interiorCoverage += interiorCoverage * (1 - coverageBackset) +
914                                 innerCoverage * coverageBackset;
915         }
916         GrVertexColor interiorColor(tweakAlphaForCoverage ? color * interiorCoverage : color,
917                                     wideColor);
918         vertices.writeQuad(VertexWriter::TriFanFromRect(interiorAABoundary),
919                            interiorColor,
920                            maybe_coverage(interiorCoverage));
921     } else {
922         // When the interior rect has become degenerate we smoosh to a single point
923         SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
924 
925         vertices.writeQuad(VertexWriter::TriFanFromRect(devInside),
926                            innerColor,
927                            maybe_coverage(innerCoverage));
928 
929         // ... unless we are degenerate, in which case we must apply the scaled coverage
930         vertices.writeQuad(VertexWriter::TriFanFromRect(devInside),
931                            innerColor,
932                            maybe_coverage(innerCoverage));
933     }
934 }
935 
936 }  // anonymous namespace
937 
Make(GrRecordingContext * context,GrPaint && paint,GrAAType aaType,const SkMatrix & viewMatrix,const SkRect & rect,const SkStrokeRec & stroke)938 GrOp::Owner Make(GrRecordingContext* context,
939                  GrPaint&& paint,
940                  GrAAType aaType,
941                  const SkMatrix& viewMatrix,
942                  const SkRect& rect,
943                  const SkStrokeRec& stroke) {
944     SkASSERT(!context->priv().caps()->reducedShaderMode());
945     if (aaType == GrAAType::kCoverage) {
946         return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke);
947     } else {
948         return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke, aaType);
949     }
950 }
951 
MakeNested(GrRecordingContext * context,GrPaint && paint,const SkMatrix & viewMatrix,const SkRect rects[2])952 GrOp::Owner MakeNested(GrRecordingContext* context,
953                        GrPaint&& paint,
954                        const SkMatrix& viewMatrix,
955                        const SkRect rects[2]) {
956     SkASSERT(viewMatrix.rectStaysRect());
957     SkASSERT(!rects[0].isEmpty() && !rects[1].isEmpty());
958 
959     SkRect devOutside = viewMatrix.mapRect(rects[0]);
960     SkRect devInside = viewMatrix.mapRect(rects[1]);
961     float dx = devOutside.fRight - devInside.fRight;
962     float dy = devOutside.fBottom - devInside.fBottom;
963 
964     // Clips our draw rects 1 full pixel outside the viewport. This avoids interpolation precision
965     // issues with very large coordinates.
966     const float m = context->priv().caps()->maxRenderTargetSize();
967     const SkRect visibilityBounds = SkRect::MakeWH(m, m).makeOutset(1, 1);
968 
969     if (!devOutside.intersect(visibilityBounds.makeOutset(dx, dy))) {
970         return nullptr;
971     }
972 
973     if (devInside.isEmpty() || !devInside.intersect(visibilityBounds)) {
974         if (devOutside.isEmpty()) {
975             return nullptr;
976         }
977         DrawQuad quad{GrQuad::MakeFromRect(rects[0], viewMatrix), GrQuad(rects[0]),
978                       GrQuadAAFlags::kAll};
979         return FillRectOp::Make(context, std::move(paint), GrAAType::kCoverage, &quad);
980     }
981 
982     return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, devOutside,
983                                 devInside, SkVector{dx, dy} * .5f);
984 }
985 
986 } // namespace skgpu::v1::StrokeRectOp
987 
988 #if GR_TEST_UTILS
989 
990 #include "src/gpu/GrDrawOpTest.h"
991 
GR_DRAW_OP_TEST_DEFINE(NonAAStrokeRectOp)992 GR_DRAW_OP_TEST_DEFINE(NonAAStrokeRectOp) {
993     SkMatrix viewMatrix = GrTest::TestMatrix(random);
994     SkRect rect = GrTest::TestRect(random);
995     SkScalar strokeWidth = random->nextBool() ? 0.0f : 2.0f;
996     SkPaint strokePaint;
997     strokePaint.setStrokeWidth(strokeWidth);
998     strokePaint.setStyle(SkPaint::kStroke_Style);
999     strokePaint.setStrokeJoin(SkPaint::kMiter_Join);
1000     SkStrokeRec strokeRec(strokePaint);
1001     GrAAType aaType = GrAAType::kNone;
1002     if (numSamples > 1) {
1003         aaType = random->nextBool() ? GrAAType::kMSAA : GrAAType::kNone;
1004     }
1005     return skgpu::v1::StrokeRectOp::NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix,
1006                                                             rect, strokeRec, aaType);
1007 }
1008 
GR_DRAW_OP_TEST_DEFINE(AAStrokeRectOp)1009 GR_DRAW_OP_TEST_DEFINE(AAStrokeRectOp) {
1010     bool miterStroke = random->nextBool();
1011 
1012     // Create either a empty rect or a non-empty rect.
1013     SkRect rect =
1014             random->nextBool() ? SkRect::MakeXYWH(10, 10, 50, 40) : SkRect::MakeXYWH(6, 7, 0, 0);
1015     SkScalar minDim = std::min(rect.width(), rect.height());
1016     SkScalar strokeWidth = random->nextUScalar1() * minDim;
1017 
1018     SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
1019     rec.setStrokeStyle(strokeWidth);
1020     rec.setStrokeParams(SkPaint::kButt_Cap,
1021                         miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f);
1022     SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random);
1023     return skgpu::v1::StrokeRectOp::AAStrokeRectOp::Make(context, std::move(paint), matrix, rect,
1024                                                          rec);
1025 }
1026 
1027 #endif
1028