commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 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.h" |
| 9 | #include "SkCanvas.h" |
bungeman | 7403d87 | 2015-08-04 14:56:53 -0700 | [diff] [blame^] | 10 | #include "SkPath.h" |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 11 | #include "SkTArray.h" |
| 12 | |
| 13 | namespace skiagm { |
| 14 | |
| 15 | class HairlinesGM : public GM { |
| 16 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 17 | |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 18 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 19 | SkString onShortName() override { |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 20 | return SkString("hairlines"); |
| 21 | } |
| 22 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 23 | SkISize onISize() override { return SkISize::Make(1250, 1250); } |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 24 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 25 | void onOnceBeforeDraw() override { |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 26 | { |
| 27 | SkPath* lineAnglesPath = &fPaths.push_back(); |
| 28 | enum { |
| 29 | kNumAngles = 15, |
| 30 | kRadius = 40, |
| 31 | }; |
| 32 | for (int i = 0; i < kNumAngles; ++i) { |
| 33 | SkScalar angle = SK_ScalarPI * SkIntToScalar(i) / kNumAngles; |
| 34 | SkScalar x = kRadius * SkScalarCos(angle); |
| 35 | SkScalar y = kRadius * SkScalarSin(angle); |
| 36 | lineAnglesPath->moveTo(x, y); |
| 37 | lineAnglesPath->lineTo(-x, -y); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | { |
| 42 | SkPath* kindaTightQuad = &fPaths.push_back(); |
| 43 | kindaTightQuad->moveTo(0, -10 * SK_Scalar1); |
| 44 | kindaTightQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), -10 * SK_Scalar1, 0); |
| 45 | } |
| 46 | |
| 47 | { |
| 48 | SkPath* tightQuad = &fPaths.push_back(); |
| 49 | tightQuad->moveTo(0, -5 * SK_Scalar1); |
| 50 | tightQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), -5 * SK_Scalar1, 0); |
| 51 | } |
| 52 | |
| 53 | { |
| 54 | SkPath* tighterQuad = &fPaths.push_back(); |
| 55 | tighterQuad->moveTo(0, -2 * SK_Scalar1); |
| 56 | tighterQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), -2 * SK_Scalar1, 0); |
| 57 | } |
| 58 | |
| 59 | { |
| 60 | SkPath* unevenTighterQuad = &fPaths.push_back(); |
| 61 | unevenTighterQuad->moveTo(0, -1 * SK_Scalar1); |
| 62 | SkPoint p; |
| 63 | p.set(-2 * SK_Scalar1 + 3 * SkIntToScalar(102) / 4, SkIntToScalar(75)); |
| 64 | unevenTighterQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), p.fX, p.fY); |
| 65 | } |
| 66 | |
| 67 | { |
| 68 | SkPath* reallyTightQuad = &fPaths.push_back(); |
| 69 | reallyTightQuad->moveTo(0, -1 * SK_Scalar1); |
| 70 | reallyTightQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), -1 * SK_Scalar1, 0); |
| 71 | } |
| 72 | |
| 73 | { |
| 74 | SkPath* closedQuad = &fPaths.push_back(); |
| 75 | closedQuad->moveTo(0, -0); |
| 76 | closedQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), 0, 0); |
| 77 | } |
| 78 | |
| 79 | { |
| 80 | SkPath* unevenClosedQuad = &fPaths.push_back(); |
| 81 | unevenClosedQuad->moveTo(0, -0); |
| 82 | unevenClosedQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), |
| 83 | SkIntToScalar(75), SkIntToScalar(75)); |
| 84 | } |
commit-bot@chromium.org | b8bd6cb | 2013-09-03 14:56:17 +0000 | [diff] [blame] | 85 | |
| 86 | // Two problem cases for gpu hairline renderer found by shapeops testing. These used |
| 87 | // to assert that the computed bounding box didn't contain all the vertices. |
| 88 | { |
| 89 | SkPath* problem1 = &fPaths.push_back(); |
| 90 | problem1->moveTo(SkIntToScalar(4), SkIntToScalar(6)); |
| 91 | problem1->cubicTo(SkIntToScalar(5), SkIntToScalar(6), |
| 92 | SkIntToScalar(5), SkIntToScalar(4), |
| 93 | SkIntToScalar(4), SkIntToScalar(0)); |
| 94 | problem1->close(); |
| 95 | } |
| 96 | |
| 97 | { |
| 98 | SkPath* problem2 = &fPaths.push_back(); |
| 99 | problem2->moveTo(SkIntToScalar(5), SkIntToScalar(1)); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 100 | problem2->lineTo(4.32787323f, 1.67212653f); |
| 101 | problem2->cubicTo(2.75223875f, 3.24776125f, |
| 102 | 3.00581908f, 4.51236057f, |
| 103 | 3.7580452f, 4.37367964f); |
| 104 | problem2->cubicTo(4.66472578f, 3.888381f, |
| 105 | 5.f, 2.875f, |
| 106 | 5.f, 1.f); |
commit-bot@chromium.org | b8bd6cb | 2013-09-03 14:56:17 +0000 | [diff] [blame] | 107 | problem2->close(); |
| 108 | } |
robertphillips@google.com | ada90da | 2013-09-18 22:14:49 +0000 | [diff] [blame] | 109 | |
| 110 | // Three paths that show the same bug (missing end caps) |
| 111 | { |
| 112 | // A caret (crbug.com/131770) |
| 113 | SkPath* bug0 = &fPaths.push_back(); |
| 114 | bug0->moveTo(6.5f,5.5f); |
| 115 | bug0->lineTo(3.5f,0.5f); |
| 116 | bug0->moveTo(0.5f,5.5f); |
| 117 | bug0->lineTo(3.5f,0.5f); |
| 118 | } |
| 119 | |
| 120 | { |
| 121 | // An X (crbug.com/137317) |
| 122 | SkPath* bug1 = &fPaths.push_back(); |
| 123 | |
| 124 | bug1->moveTo(1, 1); |
| 125 | bug1->lineTo(6, 6); |
| 126 | bug1->moveTo(1, 6); |
| 127 | bug1->lineTo(6, 1); |
| 128 | } |
| 129 | |
| 130 | { |
| 131 | // A right angle (crbug.com/137465 and crbug.com/256776) |
| 132 | SkPath* bug2 = &fPaths.push_back(); |
| 133 | |
| 134 | bug2->moveTo(5.5f, 5.5f); |
| 135 | bug2->lineTo(5.5f, 0.5f); |
| 136 | bug2->lineTo(0.5f, 0.5f); |
| 137 | } |
robertphillips@google.com | 6c00032 | 2013-09-26 12:05:32 +0000 | [diff] [blame] | 138 | |
| 139 | { |
skia.committer@gmail.com | 65caeaf | 2013-09-27 07:01:29 +0000 | [diff] [blame] | 140 | // Arc example to test imperfect truncation bug (crbug.com/295626) |
robertphillips@google.com | 6c00032 | 2013-09-26 12:05:32 +0000 | [diff] [blame] | 141 | static const SkScalar kRad = SkIntToScalar(2000); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 142 | static const SkScalar kStartAngle = 262.59717f; |
| 143 | static const SkScalar kSweepAngle = SkScalarHalf(17.188717f); |
robertphillips@google.com | 6c00032 | 2013-09-26 12:05:32 +0000 | [diff] [blame] | 144 | |
| 145 | SkPath* bug = &fPaths.push_back(); |
| 146 | |
| 147 | // Add a circular arc |
| 148 | SkRect circle = SkRect::MakeLTRB(-kRad, -kRad, kRad, kRad); |
| 149 | bug->addArc(circle, kStartAngle, kSweepAngle); |
| 150 | |
| 151 | // Now add the chord that should cap the circular arc |
| 152 | SkScalar cosV, sinV = SkScalarSinCos(SkDegreesToRadians(kStartAngle), &cosV); |
| 153 | |
| 154 | SkPoint p0 = SkPoint::Make(kRad * cosV, kRad * sinV); |
| 155 | |
| 156 | sinV = SkScalarSinCos(SkDegreesToRadians(kStartAngle + kSweepAngle), &cosV); |
| 157 | |
| 158 | SkPoint p1 = SkPoint::Make(kRad * cosV, kRad * sinV); |
| 159 | |
| 160 | bug->moveTo(p0); |
| 161 | bug->lineTo(p1); |
| 162 | } |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 163 | } |
| 164 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 165 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 166 | static const SkAlpha kAlphaValue[] = { 0xFF, 0x40 }; |
joshualitt | bd528cd | 2014-12-10 14:23:40 -0800 | [diff] [blame] | 167 | static const SkScalar kWidths[] = { 0, 0.5f, 1.5f }; |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 168 | |
| 169 | enum { |
| 170 | kMargin = 5, |
| 171 | }; |
reed | 9aeb2a1 | 2015-04-11 19:29:50 -0700 | [diff] [blame] | 172 | int wrapX = 1250 - kMargin; |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 173 | |
| 174 | SkScalar maxH = 0; |
| 175 | canvas->translate(SkIntToScalar(kMargin), SkIntToScalar(kMargin)); |
| 176 | canvas->save(); |
| 177 | |
| 178 | SkScalar x = SkIntToScalar(kMargin); |
| 179 | for (int p = 0; p < fPaths.count(); ++p) { |
| 180 | for (size_t a = 0; a < SK_ARRAY_COUNT(kAlphaValue); ++a) { |
| 181 | for (int aa = 0; aa < 2; ++aa) { |
joshualitt | bd528cd | 2014-12-10 14:23:40 -0800 | [diff] [blame] | 182 | for (size_t w = 0; w < SK_ARRAY_COUNT(kWidths); w++) { |
| 183 | const SkRect& bounds = fPaths[p].getBounds(); |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 184 | |
joshualitt | bd528cd | 2014-12-10 14:23:40 -0800 | [diff] [blame] | 185 | if (x + bounds.width() > wrapX) { |
| 186 | canvas->restore(); |
| 187 | canvas->translate(0, maxH + SkIntToScalar(kMargin)); |
| 188 | canvas->save(); |
| 189 | maxH = 0; |
| 190 | x = SkIntToScalar(kMargin); |
| 191 | } |
| 192 | |
| 193 | SkPaint paint; |
| 194 | paint.setARGB(kAlphaValue[a], 0, 0, 0); |
| 195 | paint.setAntiAlias(SkToBool(aa)); |
| 196 | paint.setStyle(SkPaint::kStroke_Style); |
| 197 | paint.setStrokeWidth(kWidths[w]); |
| 198 | |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 199 | canvas->save(); |
joshualitt | bd528cd | 2014-12-10 14:23:40 -0800 | [diff] [blame] | 200 | canvas->translate(-bounds.fLeft, -bounds.fTop); |
| 201 | canvas->drawPath(fPaths[p], paint); |
| 202 | canvas->restore(); |
| 203 | |
| 204 | maxH = SkMaxScalar(maxH, bounds.height()); |
| 205 | |
| 206 | SkScalar dx = bounds.width() + SkIntToScalar(kMargin); |
| 207 | x += dx; |
| 208 | canvas->translate(dx, 0); |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 209 | } |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | } |
| 213 | canvas->restore(); |
| 214 | } |
| 215 | |
| 216 | private: |
| 217 | SkTArray<SkPath> fPaths; |
| 218 | typedef GM INHERITED; |
| 219 | }; |
| 220 | |
| 221 | ////////////////////////////////////////////////////////////////////////////// |
| 222 | |
| 223 | static GM* MyFactory(void*) { return new HairlinesGM; } |
| 224 | static GMRegistry reg(MyFactory); |
| 225 | |
| 226 | } |