| 1 |
/* |
| 2 |
* Copyright 2013 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 "gm/gm.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkColor.h" |
| 11 |
#include "include/core/SkPaint.h" |
| 12 |
#include "include/core/SkPathBuilder.h" |
| 13 |
#include "include/core/SkPoint.h" |
| 14 |
#include "include/core/SkRect.h" |
| 15 |
#include "include/core/SkScalar.h" |
| 16 |
#include "include/core/SkSize.h" |
| 17 |
#include "include/core/SkString.h" |
| 18 |
#include "include/core/SkSurface.h" |
| 19 |
#include "include/core/SkTypes.h" |
| 20 |
#include "include/private/base/SkTArray.h" |
| 21 |
|
| 22 |
using namespace skia_private; |
| 23 |
|
| 24 |
namespace skiagm { |
| 25 |
|
| 26 |
class HairlinesGM : public GM { |
| 27 |
protected: |
| 28 |
SkString getName() const override { return SkString("hairlines"); } |
| 29 |
|
| 30 |
SkISize getISize() override { return SkISize::Make(1250, 1250); } |
| 31 |
|
| 32 |
void onOnceBeforeDraw() override { |
| 33 |
{ |
| 34 |
SkPathBuilder lineAngles; |
| 35 |
constexpr int kNumAngles = 15; |
| 36 |
constexpr int kRadius = 40; |
| 37 |
|
| 38 |
for (int i = 0; i < kNumAngles; ++i) { |
| 39 |
SkScalar angle = SK_ScalarPI * SkIntToScalar(i) / kNumAngles; |
| 40 |
SkScalar x = kRadius * SkScalarCos(angle); |
| 41 |
SkScalar y = kRadius * SkScalarSin(angle); |
| 42 |
lineAngles.moveTo(x, y).lineTo(-x, -y); |
| 43 |
} |
| 44 |
fPaths.push_back(lineAngles.detach()); |
| 45 |
} |
| 46 |
|
| 47 |
fPaths.push_back(SkPathBuilder().moveTo(0, -10) |
| 48 |
.quadTo(100, 100, -10, 0) |
| 49 |
.detach()); |
| 50 |
|
| 51 |
fPaths.push_back(SkPathBuilder().moveTo(0, -5) |
| 52 |
.quadTo(100, 100, -5, 0) |
| 53 |
.detach()); |
| 54 |
|
| 55 |
fPaths.push_back(SkPathBuilder().moveTo(0, -2) |
| 56 |
.quadTo(100, 100, -2, 0) |
| 57 |
.detach()); |
| 58 |
|
| 59 |
fPaths.push_back(SkPathBuilder().moveTo(0, -1) |
| 60 |
.quadTo(100, 100, -2 + 306.0f / 4, 75) |
| 61 |
.detach()); |
| 62 |
|
| 63 |
fPaths.push_back(SkPathBuilder().moveTo(0, -1) |
| 64 |
.quadTo(100, 100, -1, 0) |
| 65 |
.detach()); |
| 66 |
|
| 67 |
fPaths.push_back(SkPathBuilder().moveTo(0, -0) |
| 68 |
.quadTo(100, 100, 0, 0) |
| 69 |
.detach()); |
| 70 |
|
| 71 |
fPaths.push_back(SkPathBuilder().moveTo(0, -0) |
| 72 |
.quadTo(100, 100, 75, 75) |
| 73 |
.detach()); |
| 74 |
|
| 75 |
// Two problem cases for gpu hairline renderer found by shapeops testing. These used |
| 76 |
// to assert that the computed bounding box didn't contain all the vertices. |
| 77 |
|
| 78 |
fPaths.push_back(SkPathBuilder().moveTo(4, 6) |
| 79 |
.cubicTo(5, 6, 5, 4, 4, 0) |
| 80 |
.close() |
| 81 |
.detach()); |
| 82 |
|
| 83 |
fPaths.push_back(SkPathBuilder().moveTo(5, 1) |
| 84 |
.lineTo( 4.32787323f, 1.67212653f) |
| 85 |
.cubicTo(2.75223875f, 3.24776125f, |
| 86 |
3.00581908f, 4.51236057f, |
| 87 |
3.7580452f, 4.37367964f) |
| 88 |
.cubicTo(4.66472578f, 3.888381f, |
| 89 |
5.f, 2.875f, |
| 90 |
5.f, 1.f) |
| 91 |
.close() |
| 92 |
.detach()); |
| 93 |
|
| 94 |
// Three paths that show the same bug (missing end caps) |
| 95 |
|
| 96 |
fPaths.push_back(SkPathBuilder().moveTo(6.5f,5.5f) |
| 97 |
.lineTo(3.5f,0.5f) |
| 98 |
.moveTo(0.5f,5.5f) |
| 99 |
.lineTo(3.5f,0.5f) |
| 100 |
.detach()); |
| 101 |
|
| 102 |
// An X (crbug.com/137317) |
| 103 |
fPaths.push_back(SkPathBuilder().moveTo(1, 1) |
| 104 |
.lineTo(6, 6) |
| 105 |
.moveTo(1, 6) |
| 106 |
.lineTo(6, 1) |
| 107 |
.detach()); |
| 108 |
|
| 109 |
// A right angle (crbug.com/137465 and crbug.com/256776) |
| 110 |
fPaths.push_back(SkPathBuilder().moveTo(5.5f, 5.5f) |
| 111 |
.lineTo(5.5f, 0.5f) |
| 112 |
.lineTo(0.5f, 0.5f) |
| 113 |
.detach()); |
| 114 |
|
| 115 |
{ |
| 116 |
// Arc example to test imperfect truncation bug (crbug.com/295626) |
| 117 |
constexpr SkScalar kRad = SkIntToScalar(2000); |
| 118 |
constexpr SkScalar kStartAngle = 262.59717f; |
| 119 |
constexpr SkScalar kSweepAngle = SkScalarHalf(17.188717f); |
| 120 |
|
| 121 |
SkPathBuilder bug; |
| 122 |
|
| 123 |
// Add a circular arc |
| 124 |
SkRect circle = SkRect::MakeLTRB(-kRad, -kRad, kRad, kRad); |
| 125 |
bug.addArc(circle, kStartAngle, kSweepAngle); |
| 126 |
|
| 127 |
// Now add the chord that should cap the circular arc |
| 128 |
SkPoint p0 = { kRad * SkScalarCos(SkDegreesToRadians(kStartAngle)), |
| 129 |
kRad * SkScalarSin(SkDegreesToRadians(kStartAngle)) }; |
| 130 |
|
| 131 |
SkPoint p1 = { kRad * SkScalarCos(SkDegreesToRadians(kStartAngle + kSweepAngle)), |
| 132 |
kRad * SkScalarSin(SkDegreesToRadians(kStartAngle + kSweepAngle)) }; |
| 133 |
|
| 134 |
bug.moveTo(p0); |
| 135 |
bug.lineTo(p1); |
| 136 |
fPaths.push_back(bug.detach()); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
void onDraw(SkCanvas* canvas) override { |
| 141 |
constexpr SkAlpha kAlphaValue[] = { 0xFF, 0x40 }; |
| 142 |
constexpr SkScalar kWidths[] = { 0, 0.5f, 1.5f }; |
| 143 |
|
| 144 |
enum { |
| 145 |
kMargin = 5, |
| 146 |
}; |
| 147 |
int wrapX = 1250 - kMargin; |
| 148 |
|
| 149 |
SkScalar maxH = 0; |
| 150 |
canvas->translate(SkIntToScalar(kMargin), SkIntToScalar(kMargin)); |
| 151 |
canvas->save(); |
| 152 |
|
| 153 |
SkScalar x = SkIntToScalar(kMargin); |
| 154 |
for (int p = 0; p < fPaths.size(); ++p) { |
| 155 |
for (size_t a = 0; a < std::size(kAlphaValue); ++a) { |
| 156 |
for (int aa = 0; aa < 2; ++aa) { |
| 157 |
for (size_t w = 0; w < std::size(kWidths); w++) { |
| 158 |
const SkRect& bounds = fPaths[p].getBounds(); |
| 159 |
|
| 160 |
if (x + bounds.width() > wrapX) { |
| 161 |
canvas->restore(); |
| 162 |
canvas->translate(0, maxH + SkIntToScalar(kMargin)); |
| 163 |
canvas->save(); |
| 164 |
maxH = 0; |
| 165 |
x = SkIntToScalar(kMargin); |
| 166 |
} |
| 167 |
|
| 168 |
SkPaint paint; |
| 169 |
paint.setARGB(kAlphaValue[a], 0, 0, 0); |
| 170 |
paint.setAntiAlias(SkToBool(aa)); |
| 171 |
paint.setStyle(SkPaint::kStroke_Style); |
| 172 |
paint.setStrokeWidth(kWidths[w]); |
| 173 |
|
| 174 |
canvas->save(); |
| 175 |
canvas->translate(-bounds.fLeft, -bounds.fTop); |
| 176 |
canvas->drawPath(fPaths[p], paint); |
| 177 |
canvas->restore(); |
| 178 |
|
| 179 |
maxH = std::max(maxH, bounds.height()); |
| 180 |
|
| 181 |
SkScalar dx = bounds.width() + SkIntToScalar(kMargin); |
| 182 |
x += dx; |
| 183 |
canvas->translate(dx, 0); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
canvas->restore(); |
| 189 |
} |
| 190 |
|
| 191 |
private: |
| 192 |
TArray<SkPath> fPaths; |
| 193 |
using INHERITED = GM; |
| 194 |
}; |
| 195 |
|
| 196 |
static void draw_squarehair_tests(SkCanvas* canvas, SkPaint paint) { |
| 197 |
paint.setStyle(SkPaint::kStroke_Style); |
| 198 |
canvas->drawLine(10, 10, 20, 10, paint); |
| 199 |
// degenerate move, line, close to make sure we still draw the cap on both ends. |
| 200 |
SkPath p = SkPathBuilder().moveTo(10, 15).lineTo(20, 15).close().detach(); |
| 201 |
canvas->drawPath(p, paint); |
| 202 |
canvas->drawLine(10, 20.5, 20, 20.5, paint); |
| 203 |
canvas->drawLine(30, 10, 30, 20, paint); |
| 204 |
canvas->drawLine(35.5, 10, 35.5, 20, paint); |
| 205 |
canvas->drawLine(40, 10, 50, 20, paint); |
| 206 |
SkPathBuilder path; |
| 207 |
path.moveTo(60, 10); |
| 208 |
path.quadTo(60, 20, 70, 20); |
| 209 |
path.conicTo(70, 10, 80, 10, 0.707f); |
| 210 |
canvas->drawPath(path.detach(), paint); |
| 211 |
|
| 212 |
path.moveTo(90, 10); |
| 213 |
path.cubicTo(90, 20, 100, 20, 100, 10); |
| 214 |
path.lineTo(110, 10); |
| 215 |
canvas->drawPath(path.detach(), paint); |
| 216 |
} |
| 217 |
|
| 218 |
DEF_SIMPLE_GM(squarehair, canvas, 240, 360) { |
| 219 |
const bool aliases[] = { false, true }; |
| 220 |
const SkScalar widths[] = { 0, 0.999f, 1, 1.001f }; |
| 221 |
const SkPaint::Cap caps[] = { SkPaint::kButt_Cap, SkPaint::kSquare_Cap, SkPaint::kRound_Cap }; |
| 222 |
// draw aliased on left side, and anti-aliased on right side |
| 223 |
for (auto alias : aliases) { |
| 224 |
canvas->save(); |
| 225 |
for (auto width : widths) { |
| 226 |
for (auto cap : caps) { |
| 227 |
SkPaint paint; |
| 228 |
paint.setStrokeCap(cap); |
| 229 |
paint.setStrokeWidth(width); |
| 230 |
paint.setAntiAlias(alias); |
| 231 |
paint.setColor(SK_ColorBLACK); |
| 232 |
draw_squarehair_tests(canvas, paint); |
| 233 |
canvas->translate(0, 30); |
| 234 |
} |
| 235 |
} |
| 236 |
canvas->restore(); |
| 237 |
canvas->translate(120, 0); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
DEF_SIMPLE_GM_CAN_FAIL(squarehair_diffs, canvas, errorMsg, 600, 720) { |
| 242 |
const bool aliases[] = { false, true }; |
| 243 |
const SkScalar widths[] = { 0, 1, 1.001f }; |
| 244 |
// Draws each of the three caps in a different color so we can overlay the three channels |
| 245 |
// and zoom in to see the differences in the cap algorithms. |
| 246 |
const SkPaint::Cap caps[] = { SkPaint::kButt_Cap, SkPaint::kSquare_Cap, SkPaint::kRound_Cap }; |
| 247 |
const SkColor colors[] = {SkColorSetRGB(255, 0, 0), SkColorSetRGB(0, 255, 0), SkColorSetRGB(0, 0, 255)}; |
| 248 |
for (auto alias : aliases) { |
| 249 |
for (auto width : widths) { |
| 250 |
SkPaint backdrop; |
| 251 |
backdrop.setColor(SK_ColorBLACK); |
| 252 |
backdrop.setStyle(SkPaint::Style::kFill_Style); |
| 253 |
canvas->drawRect({120, 0, 600, 100}, backdrop); |
| 254 |
for (int i = 0; i < 3; i++) { |
| 255 |
auto surface = canvas->makeSurface(canvas->imageInfo().makeWH(120, 25)); |
| 256 |
if (!surface) { |
| 257 |
*errorMsg = "Could not make subsurface"; |
| 258 |
return DrawResult::kSkip; |
| 259 |
} |
| 260 |
SkPaint paint; |
| 261 |
paint.setAntiAlias(alias); |
| 262 |
paint.setStrokeWidth(width); |
| 263 |
paint.setStrokeCap(caps[i]); |
| 264 |
paint.setColor(colors[i]); |
| 265 |
draw_squarehair_tests(surface->getCanvas(), paint); |
| 266 |
auto img = surface->makeImageSnapshot(); |
| 267 |
canvas->drawImage(img, 0, 30*i); |
| 268 |
{ |
| 269 |
SkAutoCanvasRestore acr(canvas, true); |
| 270 |
canvas->scale(4, 4); |
| 271 |
SkPaint plus; |
| 272 |
plus.setBlendMode(SkBlendMode::kPlus); |
| 273 |
canvas->drawImage(img, 30, 0, {}, &plus); |
| 274 |
} |
| 275 |
} |
| 276 |
canvas->translate(0, 120); |
| 277 |
} |
| 278 |
canvas->translate(0, 20); |
| 279 |
} |
| 280 |
return skiagm::DrawResult::kOk; |
| 281 |
} |
| 282 |
|
| 283 |
// GM to test subdivision of hairlines |
| 284 |
static void draw_subdivided_quad(SkCanvas* canvas, int x0, int y0, int x1, int y1, SkColor color) { |
| 285 |
SkPaint paint; |
| 286 |
paint.setStrokeWidth(1); |
| 287 |
paint.setAntiAlias(true); |
| 288 |
paint.setStyle(SkPaint::kStroke_Style); |
| 289 |
paint.setColor(color); |
| 290 |
|
| 291 |
canvas->drawPath(SkPathBuilder().moveTo(0,0) |
| 292 |
.quadTo(SkIntToScalar(x0), SkIntToScalar(y0), |
| 293 |
SkIntToScalar(x1), SkIntToScalar(y1)) |
| 294 |
.detach(), |
| 295 |
paint); |
| 296 |
} |
| 297 |
|
| 298 |
DEF_SIMPLE_GM(hairline_subdiv, canvas, 512, 256) { |
| 299 |
// no subdivisions |
| 300 |
canvas->translate(45, -25); |
| 301 |
draw_subdivided_quad(canvas, 334, 334, 467, 267, SK_ColorBLACK); |
| 302 |
|
| 303 |
// one subdivision |
| 304 |
canvas->translate(-185, -150); |
| 305 |
draw_subdivided_quad(canvas, 472, 472, 660, 378, SK_ColorRED); |
| 306 |
|
| 307 |
// two subdivisions |
| 308 |
canvas->translate(-275, -200); |
| 309 |
draw_subdivided_quad(canvas, 668, 668, 934, 535, SK_ColorGREEN); |
| 310 |
|
| 311 |
// three subdivisions |
| 312 |
canvas->translate(-385, -260); |
| 313 |
draw_subdivided_quad(canvas, 944, 944, 1320, 756, SK_ColorBLUE); |
| 314 |
} |
| 315 |
|
| 316 |
////////////////////////////////////////////////////////////////////////////// |
| 317 |
|
| 318 |
DEF_GM( return new HairlinesGM; ) |
| 319 |
|
| 320 |
} // namespace skiagm |