blob: a4bc3a2f56ba6ff15da18ff36ffa355e77d4676b [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"
Robert Phillipsedcd4312021-06-03 10:14:16 -040020#include "include/gpu/GrDirectContext.h"
Kevin Lubick0d4d1142023-02-13 09:13:10 -050021#include "src/base/SkRandom.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/core/SkGeometry.h"
Greg Daniel719239c2022-04-07 11:20:24 -040023#include "src/gpu/ganesh/GrCaps.h"
24#include "src/gpu/ganesh/GrDirectContextPriv.h"
25#include "src/gpu/ganesh/GrDrawingManager.h"
26#include "src/gpu/ganesh/GrRecordingContextPriv.h"
Chris Dalton09a7bb22018-08-31 19:53:15 +080027
Chris Dalton71e21262020-08-20 09:42:12 -060028static constexpr float kStrokeWidth = 30;
Chris Dalton09a7bb22018-08-31 19:53:15 +080029static constexpr int kCellSize = 200;
Chris Daltonc2ae19c2020-09-09 08:35:13 -060030static constexpr int kNumCols = 5;
Chris Dalton7b807262020-12-10 10:22:50 -070031static constexpr int kNumRows = 5;
Chris Dalton31634282020-09-17 12:16:54 -060032static constexpr int kTestWidth = kNumCols * kCellSize;
33static constexpr int kTestHeight = kNumRows * kCellSize;
Chris Dalton09a7bb22018-08-31 19:53:15 +080034
Chris Dalton71e21262020-08-20 09:42:12 -060035enum class CellFillMode {
36 kStretch,
37 kCenter
38};
39
40struct TrickyCubic {
41 SkPoint fPoints[4];
Chris Dalton5c3e1a92020-09-15 20:18:21 -060042 int fNumPts;
Chris Dalton71e21262020-08-20 09:42:12 -060043 CellFillMode fFillMode;
44 float fScale = 1;
45};
46
Chris Dalton31634282020-09-17 12:16:54 -060047// This is a compilation of cubics that have given strokers grief. Feel free to add more.
Chris Dalton71e21262020-08-20 09:42:12 -060048static const TrickyCubic kTrickyCubics[] = {
Chris Dalton5c3e1a92020-09-15 20:18:21 -060049 {{{122, 737}, {348, 553}, {403, 761}, {400, 760}}, 4, CellFillMode::kStretch},
50 {{{244, 520}, {244, 518}, {1141, 634}, {394, 688}}, 4, CellFillMode::kStretch},
51 {{{550, 194}, {138, 130}, {1035, 246}, {288, 300}}, 4, CellFillMode::kStretch},
52 {{{226, 733}, {556, 779}, {-43, 471}, {348, 683}}, 4, CellFillMode::kStretch},
53 {{{268, 204}, {492, 304}, {352, 23}, {433, 412}}, 4, CellFillMode::kStretch},
54 {{{172, 480}, {396, 580}, {256, 299}, {338, 677}}, 4, CellFillMode::kStretch},
55 {{{731, 340}, {318, 252}, {1026, -64}, {367, 265}}, 4, CellFillMode::kStretch},
56 {{{475, 708}, {62, 620}, {770, 304}, {220, 659}}, 4, CellFillMode::kStretch},
57 {{{0, 0}, {128, 128}, {128, 0}, {0, 128}}, 4, CellFillMode::kCenter}, // Perfect cusp
58 {{{0,.01f}, {128,127.999f}, {128,.01f}, {0,127.99f}}, 4, CellFillMode::kCenter}, // Near-cusp
59 {{{0,-.01f}, {128,128.001f}, {128,-.01f}, {0,128.001f}}, 4, CellFillMode::kCenter}, // Near-cusp
60 {{{0,0}, {0,-10}, {0,-10}, {0,10}}, 4, CellFillMode::kCenter, 1.098283f}, // Flat line with 180
61 {{{10,0}, {0,0}, {20,0}, {10,0}}, 4, CellFillMode::kStretch}, // Flat line with 2 180s
62 {{{39,-39}, {40,-40}, {40,-40}, {0,0}}, 4, CellFillMode::kStretch}, // Flat diagonal with 180
63 {{{40, 40}, {0, 0}, {200, 200}, {0, 0}}, 4, CellFillMode::kStretch}, // Diag w/ an internal 180
64 {{{0,0}, {1e-2f,0}, {-1e-2f,0}, {0,0}}, 4, CellFillMode::kCenter}, // Circle
65 {{{400.75f,100.05f}, {400.75f,100.05f}, {100.05f,300.95f}, {100.05f,300.95f}}, 4,
Chris Daltonc2ae19c2020-09-09 08:35:13 -060066 CellFillMode::kStretch}, // Flat line with no turns
Chris Dalton5c3e1a92020-09-15 20:18:21 -060067 {{{0.5f,0}, {0,0}, {20,0}, {10,0}}, 4, CellFillMode::kStretch}, // Flat line with 2 180s
68 {{{10,0}, {0,0}, {10,0}, {10,0}}, 4, CellFillMode::kStretch}, // Flat line with a 180
Chris Dalton7b807262020-12-10 10:22:50 -070069 {{{1,1}, {2,1}, {1,1}, {1, std::numeric_limits<float>::quiet_NaN()}}, 3,
70 CellFillMode::kStretch}, // Flat QUAD with a cusp
71 {{{1,1}, {100,1}, {25,1}, {.3f, std::numeric_limits<float>::quiet_NaN()}}, 3,
72 CellFillMode::kStretch}, // Flat CONIC with a cusp
73 {{{1,1}, {100,1}, {25,1}, {1.5f, std::numeric_limits<float>::quiet_NaN()}}, 3,
74 CellFillMode::kStretch}, // Flat CONIC with a cusp
Chris Dalton09a7bb22018-08-31 19:53:15 +080075};
76
77static SkRect calc_tight_cubic_bounds(const SkPoint P[4], int depth=5) {
78 if (0 == depth) {
79 SkRect bounds;
Brian Osman788b9162020-02-07 10:36:46 -050080 bounds.fLeft = std::min(std::min(P[0].x(), P[1].x()), std::min(P[2].x(), P[3].x()));
81 bounds.fTop = std::min(std::min(P[0].y(), P[1].y()), std::min(P[2].y(), P[3].y()));
82 bounds.fRight = std::max(std::max(P[0].x(), P[1].x()), std::max(P[2].x(), P[3].x()));
83 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 +080084 return bounds;
85 }
86
87 SkPoint chopped[7];
88 SkChopCubicAt(P, chopped, .5f);
89 SkRect bounds = calc_tight_cubic_bounds(chopped, depth - 1);
90 bounds.join(calc_tight_cubic_bounds(chopped+3, depth - 1));
91 return bounds;
92}
93
Chris Dalton5c3e1a92020-09-15 20:18:21 -060094static SkPoint lerp(const SkPoint& a, const SkPoint& b, float T) {
95 SkASSERT(1 != T); // The below does not guarantee lerp(a, b, 1) === b.
96 return (b - a) * T + a;
97}
98
Chris Dalton71e21262020-08-20 09:42:12 -060099enum class FillMode {
100 kCenter,
101 kScale
102};
103
Chris Dalton7d979912021-05-11 12:36:51 -0600104static void draw_test(SkCanvas* canvas, SkPaint::Cap cap, SkPaint::Join join) {
105 SkRandom rand;
106
Michael Ludwigde436b32022-05-04 10:38:04 -0400107 canvas->clear(SK_ColorBLACK);
Chris Dalton7d979912021-05-11 12:36:51 -0600108
Chris Dalton31634282020-09-17 12:16:54 -0600109 SkPaint strokePaint;
110 strokePaint.setAntiAlias(true);
111 strokePaint.setStrokeWidth(kStrokeWidth);
Chris Dalton31634282020-09-17 12:16:54 -0600112 strokePaint.setStyle(SkPaint::kStroke_Style);
Chris Dalton7d979912021-05-11 12:36:51 -0600113 strokePaint.setStrokeCap(cap);
114 strokePaint.setStrokeJoin(join);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800115
Herb Derbyc37b3862022-06-21 09:49:17 -0400116 for (size_t i = 0; i < std::size(kTrickyCubics); ++i) {
Chris Dalton31634282020-09-17 12:16:54 -0600117 auto [originalPts, numPts, fillMode, scale] = kTrickyCubics[i];
Chris Dalton09a7bb22018-08-31 19:53:15 +0800118
Chris Dalton31634282020-09-17 12:16:54 -0600119 SkASSERT(numPts <= 4);
120 SkPoint p[4];
121 memcpy(p, originalPts, sizeof(SkPoint) * numPts);
122 for (int j = 0; j < numPts; ++j) {
123 p[j] *= scale;
Chris Dalton09a7bb22018-08-31 19:53:15 +0800124 }
Chris Dalton7b807262020-12-10 10:22:50 -0700125 float w = originalPts[3].fX;
Chris Dalton09a7bb22018-08-31 19:53:15 +0800126
Chris Dalton31634282020-09-17 12:16:54 -0600127 auto cellRect = SkRect::MakeXYWH((i % kNumCols) * kCellSize, (i / kNumCols) * kCellSize,
Chris Dalton71e21262020-08-20 09:42:12 -0600128 kCellSize, kCellSize);
129
Chris Dalton5c3e1a92020-09-15 20:18:21 -0600130 SkRect strokeBounds;
131 if (numPts == 4) {
132 strokeBounds = calc_tight_cubic_bounds(p);
133 } else {
134 SkASSERT(numPts == 3);
135 SkPoint asCubic[4] = {p[0], lerp(p[0], p[1], 2/3.f), lerp(p[1], p[2], 1/3.f), p[2]};
136 strokeBounds = calc_tight_cubic_bounds(asCubic);
137 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800138 strokeBounds.outset(kStrokeWidth, kStrokeWidth);
139
140 SkMatrix matrix;
Chris Dalton71e21262020-08-20 09:42:12 -0600141 if (fillMode == CellFillMode::kStretch) {
Mike Reed2ac6ce82021-01-15 12:26:22 -0500142 matrix = SkMatrix::RectToRect(strokeBounds, cellRect, SkMatrix::kCenter_ScaleToFit);
Chris Dalton71e21262020-08-20 09:42:12 -0600143 } else {
144 matrix.setTranslate(cellRect.x() + kStrokeWidth +
145 (cellRect.width() - strokeBounds.width()) / 2,
146 cellRect.y() + kStrokeWidth +
147 (cellRect.height() - strokeBounds.height()) / 2);
148 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800149
150 SkAutoCanvasRestore acr(canvas, true);
151 canvas->concat(matrix);
Chris Dalton31634282020-09-17 12:16:54 -0600152 strokePaint.setStrokeWidth(kStrokeWidth / matrix.getMaxScale());
Chris Dalton7d979912021-05-11 12:36:51 -0600153 strokePaint.setColor(rand.nextU() | 0xff808080);
Chris Dalton5c3e1a92020-09-15 20:18:21 -0600154 SkPath path = SkPath().moveTo(p[0]);
155 if (numPts == 4) {
156 path.cubicTo(p[1], p[2], p[3]);
Chris Dalton7b807262020-12-10 10:22:50 -0700157 } else if (w == 1) {
Chris Dalton5c3e1a92020-09-15 20:18:21 -0600158 SkASSERT(numPts == 3);
159 path.quadTo(p[1], p[2]);
Chris Dalton7b807262020-12-10 10:22:50 -0700160 } else {
161 SkASSERT(numPts == 3);
162 path.conicTo(p[1], p[2], w);
Chris Dalton5c3e1a92020-09-15 20:18:21 -0600163 }
Chris Dalton31634282020-09-17 12:16:54 -0600164 canvas->drawPath(path, strokePaint);
165 }
166}
167
168DEF_SIMPLE_GM(trickycubicstrokes, canvas, kTestWidth, kTestHeight) {
Chris Dalton7d979912021-05-11 12:36:51 -0600169 draw_test(canvas, SkPaint::kButt_Cap, SkPaint::kMiter_Join);
170}
171
172DEF_SIMPLE_GM(trickycubicstrokes_roundcaps, canvas, kTestWidth, kTestHeight) {
173 draw_test(canvas, SkPaint::kRound_Cap, SkPaint::kRound_Join);
Chris Dalton31634282020-09-17 12:16:54 -0600174}