[graphite] Check miter limit for stroked rects

I had a flawed understanding of the miter limit and thought it's min
allowed value was sqrt(2) so 90 degree corners would never be a bevel.
That's not the case so this checks the miter limit before uploading the
final join style to the instance data.

I also noticed that the stroke_rect_shader GM was setting the miter
limit on the paint and then drawing a second row, so on the second
time through the original full-miter draw was still seeing the very
small miter limit and was becoming beveled. This is fixed so the
miter column matches across AA and non-AA.

I confirmed that in Graphite, the beveled-miter column now draws with
bevels instead of mitered.

Bug: b/266451994
Change-Id: Ia8f3a4d7c9ddb9b9b34f933d0787b967181e07c2
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/645040
Commit-Queue: Jim Van Verth <jvanverth@google.com>
Reviewed-by: Jim Van Verth <jvanverth@google.com>
Commit-Queue: Michael Ludwig <michaelludwig@google.com>
Auto-Submit: Michael Ludwig <michaelludwig@google.com>
diff --git a/gm/stroke_rect_shader.cpp b/gm/stroke_rect_shader.cpp
index 1e97014..6a57030 100644
--- a/gm/stroke_rect_shader.cpp
+++ b/gm/stroke_rect_shader.cpp
@@ -29,17 +29,19 @@
     constexpr SkRect kRect {0, 0, 100, 100};
     constexpr SkPoint kPts[] {{kRect.fLeft, kRect.fTop}, {kRect.fRight, kRect.fBottom}};
     constexpr SkColor kColors[] {SK_ColorRED, SK_ColorBLUE};
-    SkPaint paint;
     sk_sp<SkShader> shader = SkGradientShader::MakeLinear(kPts, kColors, nullptr, 2,
                                                           SkTileMode::kClamp);
-    paint.setShader(std::move(shader));
-    paint.setStyle(SkPaint::kStroke_Style);
+
     // Do a large initial translate so that local coords disagree with device coords significantly
     // for the first rect drawn.
     canvas->translate(kRect.centerX(), kRect.centerY());
     constexpr SkScalar kPad = 20;
     for (auto aa : {false, true}) {
+        SkPaint paint;
+        paint.setShader(shader);
+        paint.setStyle(SkPaint::kStroke_Style);
         paint.setAntiAlias(aa);
+
         canvas->save();
 
         constexpr SkScalar kStrokeWidth = 10;
diff --git a/src/gpu/graphite/render/AnalyticRRectRenderStep.cpp b/src/gpu/graphite/render/AnalyticRRectRenderStep.cpp
index 515ec81..217821d 100644
--- a/src/gpu/graphite/render/AnalyticRRectRenderStep.cpp
+++ b/src/gpu/graphite/render/AnalyticRRectRenderStep.cpp
@@ -762,9 +762,14 @@
         if (params.strokeStyle().halfWidth() > 0.f) {
             float joinStyle = params.strokeStyle().joinLimit();
             if (params.strokeStyle().isMiterJoin()) {
-                // Discard actual miter limit because a 90-degree corner never exceeds that, and
-                // set either +1 for per-corner mitering or +2 for if all corners are mitered.
-                joinStyle = all(xRadii == skvx::float4(0.f)) ? 2.f : 1.f;
+                // All corners are 90-degrees so become beveled if the miter limit is < sqrt(2).
+                if (params.strokeStyle().miterLimit() < SK_ScalarSqrt2) {
+                    joinStyle = 0.f; // == bevel
+                } else {
+                    // Discard actual miter limit because a 90-degree corner never exceeds that, and
+                    // set either +1 for per-corner mitering or +2 for if all corners are mitered.
+                    joinStyle = all(xRadii == skvx::float4(0.f)) ? 2.f : 1.f;
+                }
             }
             // Write a negative value outside [-1, 0] to signal a stroked shape, then the style
             // params, followed by corner radii and bounds.