blob: 5a2cd55182bcdf6448db28e4b22d55782a8b5ac7 [file] [log] [blame]
Chris Dalton09a7bb22018-08-31 19:53:15 +08001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkMatrix.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPaint.h"
13#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkPoint.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkSize.h"
17#include "include/core/SkString.h"
18#include "include/core/SkTypes.h"
Chris Dalton31634282020-09-17 12:16:54 -060019#include "include/gpu/GrContextOptions.h"
20#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/core/SkGeometry.h"
Chris Dalton31634282020-09-17 12:16:54 -060022#include "src/gpu/GrDrawingManager.h"
23#include "src/gpu/GrRecordingContextPriv.h"
24#include "src/gpu/tessellate/GrTessellationPathRenderer.h"
Chris Dalton09a7bb22018-08-31 19:53:15 +080025
Chris Dalton71e21262020-08-20 09:42:12 -060026static constexpr float kStrokeWidth = 30;
Chris Dalton09a7bb22018-08-31 19:53:15 +080027static constexpr int kCellSize = 200;
Chris Daltonc2ae19c2020-09-09 08:35:13 -060028static constexpr int kNumCols = 5;
Chris Dalton7b807262020-12-10 10:22:50 -070029static constexpr int kNumRows = 5;
Chris Dalton31634282020-09-17 12:16:54 -060030static constexpr int kTestWidth = kNumCols * kCellSize;
31static constexpr int kTestHeight = kNumRows * kCellSize;
Chris Dalton09a7bb22018-08-31 19:53:15 +080032
Chris Dalton71e21262020-08-20 09:42:12 -060033enum class CellFillMode {
34 kStretch,
35 kCenter
36};
37
38struct TrickyCubic {
39 SkPoint fPoints[4];
Chris Dalton5c3e1a92020-09-15 20:18:21 -060040 int fNumPts;
Chris Dalton71e21262020-08-20 09:42:12 -060041 CellFillMode fFillMode;
42 float fScale = 1;
43};
44
Chris Dalton31634282020-09-17 12:16:54 -060045// This is a compilation of cubics that have given strokers grief. Feel free to add more.
Chris Dalton71e21262020-08-20 09:42:12 -060046static const TrickyCubic kTrickyCubics[] = {
Chris Dalton5c3e1a92020-09-15 20:18:21 -060047 {{{122, 737}, {348, 553}, {403, 761}, {400, 760}}, 4, CellFillMode::kStretch},
48 {{{244, 520}, {244, 518}, {1141, 634}, {394, 688}}, 4, CellFillMode::kStretch},
49 {{{550, 194}, {138, 130}, {1035, 246}, {288, 300}}, 4, CellFillMode::kStretch},
50 {{{226, 733}, {556, 779}, {-43, 471}, {348, 683}}, 4, CellFillMode::kStretch},
51 {{{268, 204}, {492, 304}, {352, 23}, {433, 412}}, 4, CellFillMode::kStretch},
52 {{{172, 480}, {396, 580}, {256, 299}, {338, 677}}, 4, CellFillMode::kStretch},
53 {{{731, 340}, {318, 252}, {1026, -64}, {367, 265}}, 4, CellFillMode::kStretch},
54 {{{475, 708}, {62, 620}, {770, 304}, {220, 659}}, 4, CellFillMode::kStretch},
55 {{{0, 0}, {128, 128}, {128, 0}, {0, 128}}, 4, CellFillMode::kCenter}, // Perfect cusp
56 {{{0,.01f}, {128,127.999f}, {128,.01f}, {0,127.99f}}, 4, CellFillMode::kCenter}, // Near-cusp
57 {{{0,-.01f}, {128,128.001f}, {128,-.01f}, {0,128.001f}}, 4, CellFillMode::kCenter}, // Near-cusp
58 {{{0,0}, {0,-10}, {0,-10}, {0,10}}, 4, CellFillMode::kCenter, 1.098283f}, // Flat line with 180
59 {{{10,0}, {0,0}, {20,0}, {10,0}}, 4, CellFillMode::kStretch}, // Flat line with 2 180s
60 {{{39,-39}, {40,-40}, {40,-40}, {0,0}}, 4, CellFillMode::kStretch}, // Flat diagonal with 180
61 {{{40, 40}, {0, 0}, {200, 200}, {0, 0}}, 4, CellFillMode::kStretch}, // Diag w/ an internal 180
62 {{{0,0}, {1e-2f,0}, {-1e-2f,0}, {0,0}}, 4, CellFillMode::kCenter}, // Circle
63 {{{400.75f,100.05f}, {400.75f,100.05f}, {100.05f,300.95f}, {100.05f,300.95f}}, 4,
Chris Daltonc2ae19c2020-09-09 08:35:13 -060064 CellFillMode::kStretch}, // Flat line with no turns
Chris Dalton5c3e1a92020-09-15 20:18:21 -060065 {{{0.5f,0}, {0,0}, {20,0}, {10,0}}, 4, CellFillMode::kStretch}, // Flat line with 2 180s
66 {{{10,0}, {0,0}, {10,0}, {10,0}}, 4, CellFillMode::kStretch}, // Flat line with a 180
Chris Dalton7b807262020-12-10 10:22:50 -070067 {{{1,1}, {2,1}, {1,1}, {1, std::numeric_limits<float>::quiet_NaN()}}, 3,
68 CellFillMode::kStretch}, // Flat QUAD with a cusp
69 {{{1,1}, {100,1}, {25,1}, {.3f, std::numeric_limits<float>::quiet_NaN()}}, 3,
70 CellFillMode::kStretch}, // Flat CONIC with a cusp
71 {{{1,1}, {100,1}, {25,1}, {1.5f, std::numeric_limits<float>::quiet_NaN()}}, 3,
72 CellFillMode::kStretch}, // Flat CONIC with a cusp
Chris Dalton09a7bb22018-08-31 19:53:15 +080073};
74
75static SkRect calc_tight_cubic_bounds(const SkPoint P[4], int depth=5) {
76 if (0 == depth) {
77 SkRect bounds;
Brian Osman788b9162020-02-07 10:36:46 -050078 bounds.fLeft = std::min(std::min(P[0].x(), P[1].x()), std::min(P[2].x(), P[3].x()));
79 bounds.fTop = std::min(std::min(P[0].y(), P[1].y()), std::min(P[2].y(), P[3].y()));
80 bounds.fRight = std::max(std::max(P[0].x(), P[1].x()), std::max(P[2].x(), P[3].x()));
81 bounds.fBottom = std::max(std::max(P[0].y(), P[1].y()), std::max(P[2].y(), P[3].y()));
Chris Dalton09a7bb22018-08-31 19:53:15 +080082 return bounds;
83 }
84
85 SkPoint chopped[7];
86 SkChopCubicAt(P, chopped, .5f);
87 SkRect bounds = calc_tight_cubic_bounds(chopped, depth - 1);
88 bounds.join(calc_tight_cubic_bounds(chopped+3, depth - 1));
89 return bounds;
90}
91
Chris Dalton5c3e1a92020-09-15 20:18:21 -060092static SkPoint lerp(const SkPoint& a, const SkPoint& b, float T) {
93 SkASSERT(1 != T); // The below does not guarantee lerp(a, b, 1) === b.
94 return (b - a) * T + a;
95}
96
Chris Dalton71e21262020-08-20 09:42:12 -060097enum class FillMode {
98 kCenter,
99 kScale
100};
101
Chris Dalton31634282020-09-17 12:16:54 -0600102static void draw_test(SkCanvas* canvas, const SkColor strokeColor) {
103 SkPaint strokePaint;
104 strokePaint.setAntiAlias(true);
105 strokePaint.setStrokeWidth(kStrokeWidth);
106 strokePaint.setColor(strokeColor);
107 strokePaint.setStyle(SkPaint::kStroke_Style);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800108
Chris Dalton31634282020-09-17 12:16:54 -0600109 canvas->clear(SK_ColorBLACK);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800110
Chris Dalton31634282020-09-17 12:16:54 -0600111 for (size_t i = 0; i < SK_ARRAY_COUNT(kTrickyCubics); ++i) {
112 auto [originalPts, numPts, fillMode, scale] = kTrickyCubics[i];
Chris Dalton09a7bb22018-08-31 19:53:15 +0800113
Chris Dalton31634282020-09-17 12:16:54 -0600114 SkASSERT(numPts <= 4);
115 SkPoint p[4];
116 memcpy(p, originalPts, sizeof(SkPoint) * numPts);
117 for (int j = 0; j < numPts; ++j) {
118 p[j] *= scale;
Chris Dalton09a7bb22018-08-31 19:53:15 +0800119 }
Chris Dalton7b807262020-12-10 10:22:50 -0700120 float w = originalPts[3].fX;
Chris Dalton09a7bb22018-08-31 19:53:15 +0800121
Chris Dalton31634282020-09-17 12:16:54 -0600122 auto cellRect = SkRect::MakeXYWH((i % kNumCols) * kCellSize, (i / kNumCols) * kCellSize,
Chris Dalton71e21262020-08-20 09:42:12 -0600123 kCellSize, kCellSize);
124
Chris Dalton5c3e1a92020-09-15 20:18:21 -0600125 SkRect strokeBounds;
126 if (numPts == 4) {
127 strokeBounds = calc_tight_cubic_bounds(p);
128 } else {
129 SkASSERT(numPts == 3);
130 SkPoint asCubic[4] = {p[0], lerp(p[0], p[1], 2/3.f), lerp(p[1], p[2], 1/3.f), p[2]};
131 strokeBounds = calc_tight_cubic_bounds(asCubic);
132 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800133 strokeBounds.outset(kStrokeWidth, kStrokeWidth);
134
135 SkMatrix matrix;
Chris Dalton71e21262020-08-20 09:42:12 -0600136 if (fillMode == CellFillMode::kStretch) {
Mike Reed2ac6ce82021-01-15 12:26:22 -0500137 matrix = SkMatrix::RectToRect(strokeBounds, cellRect, SkMatrix::kCenter_ScaleToFit);
Chris Dalton71e21262020-08-20 09:42:12 -0600138 } else {
139 matrix.setTranslate(cellRect.x() + kStrokeWidth +
140 (cellRect.width() - strokeBounds.width()) / 2,
141 cellRect.y() + kStrokeWidth +
142 (cellRect.height() - strokeBounds.height()) / 2);
143 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800144
145 SkAutoCanvasRestore acr(canvas, true);
146 canvas->concat(matrix);
Chris Dalton31634282020-09-17 12:16:54 -0600147 strokePaint.setStrokeWidth(kStrokeWidth / matrix.getMaxScale());
Chris Dalton5c3e1a92020-09-15 20:18:21 -0600148 SkPath path = SkPath().moveTo(p[0]);
149 if (numPts == 4) {
150 path.cubicTo(p[1], p[2], p[3]);
Chris Dalton7b807262020-12-10 10:22:50 -0700151 } else if (w == 1) {
Chris Dalton5c3e1a92020-09-15 20:18:21 -0600152 SkASSERT(numPts == 3);
153 path.quadTo(p[1], p[2]);
Chris Dalton7b807262020-12-10 10:22:50 -0700154 } else {
155 SkASSERT(numPts == 3);
156 path.conicTo(p[1], p[2], w);
Chris Dalton5c3e1a92020-09-15 20:18:21 -0600157 }
Chris Dalton31634282020-09-17 12:16:54 -0600158 canvas->drawPath(path, strokePaint);
159 }
160}
161
162DEF_SIMPLE_GM(trickycubicstrokes, canvas, kTestWidth, kTestHeight) {
163 draw_test(canvas, SK_ColorGREEN);
164}
165
166class TrickyCubicStrokes_tess_segs_5 : public skiagm::GpuGM {
167 SkString onShortName() override {
168 return SkString("trickycubicstrokes_tess_segs_5");
Chris Dalton09a7bb22018-08-31 19:53:15 +0800169 }
170
Chris Dalton31634282020-09-17 12:16:54 -0600171 SkISize onISize() override {
172 return SkISize::Make(kTestWidth, kTestHeight);
173 }
174
Chris Dalton6756afa2020-09-22 16:47:52 -0600175 // Pick a very small, odd (and better yet, prime) number of segments.
176 //
177 // - Odd because it makes the tessellation strip asymmetric, which will be important to test for
178 // future plans that involve drawing in reverse order.
179 //
180 // - >=4 because the tessellator code will just assume we have enough to combine a miter join
181 // and line in a single patch. (Requires 4 segments. Spec required minimum is 64.)
182 static constexpr int kMaxTessellationSegmentsOverride = 5;
183
Chris Dalton31634282020-09-17 12:16:54 -0600184 void modifyGrContextOptions(GrContextOptions* options) override {
Chris Dalton6756afa2020-09-22 16:47:52 -0600185 options->fMaxTessellationSegmentsOverride = kMaxTessellationSegmentsOverride;
Chris Dalton31634282020-09-17 12:16:54 -0600186 // Only allow the tessellation path renderer.
187 options->fGpuPathRenderers = (GpuPathRenderers)((int)options->fGpuPathRenderers &
188 (int)GpuPathRenderers::kTessellation);
189 }
190
Brian Salomoneebe7352020-12-09 16:37:04 -0500191 DrawResult onDraw(GrRecordingContext* context, GrSurfaceDrawContext*, SkCanvas* canvas,
Chris Dalton31634282020-09-17 12:16:54 -0600192 SkString* errorMsg) override {
193 if (!context->priv().caps()->shaderCaps()->tessellationSupport() ||
194 !GrTessellationPathRenderer::IsSupported(*context->priv().caps())) {
195 errorMsg->set("Tessellation not supported.");
196 return DrawResult::kSkip;
197 }
198 auto opts = context->priv().drawingManager()->testingOnly_getOptionsForPathRendererChain();
199 if (!(opts.fGpuPathRenderers & GpuPathRenderers::kTessellation)) {
200 errorMsg->set("GrTessellationPathRenderer disabled.");
201 return DrawResult::kSkip;
202 }
Chris Dalton6756afa2020-09-22 16:47:52 -0600203 if (context->priv().caps()->shaderCaps()->maxTessellationSegments() !=
204 kMaxTessellationSegmentsOverride) {
205 errorMsg->set("modifyGrContextOptions did not affect maxTessellationSegments. "
206 "(Are you running viewer? If so use '--maxTessellationSegments 5'.)");
207 return DrawResult::kFail;
208 }
Chris Dalton31634282020-09-17 12:16:54 -0600209 // Suppress a tessellator warning message that caps.maxTessellationSegments is too small.
210 GrRecordingContextPriv::AutoSuppressWarningMessages aswm(context);
211 draw_test(canvas, SK_ColorRED);
212 return DrawResult::kOk;
213 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800214};
215
Chris Dalton31634282020-09-17 12:16:54 -0600216DEF_GM( return new TrickyCubicStrokes_tess_segs_5; )