reed | 19d8f9f | 2015-01-29 10:48:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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" |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 9 | #include "SkAnimTimer.h" |
reed | 19d8f9f | 2015-01-29 10:48:16 -0800 | [diff] [blame] | 10 | #include "SkCanvas.h" |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 11 | #include "SkPathMeasure.h" |
reed | 19d8f9f | 2015-01-29 10:48:16 -0800 | [diff] [blame] | 12 | #include "SkRandom.h" |
| 13 | |
| 14 | class AddArcGM : public skiagm::GM { |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 15 | public: |
| 16 | AddArcGM() : fRotate(0) {} |
| 17 | |
reed | 19d8f9f | 2015-01-29 10:48:16 -0800 | [diff] [blame] | 18 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 19 | SkString onShortName() override { return SkString("addarc"); } |
reed | 19d8f9f | 2015-01-29 10:48:16 -0800 | [diff] [blame] | 20 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 21 | SkISize onISize() override { return SkISize::Make(1040, 1040); } |
reed | 19d8f9f | 2015-01-29 10:48:16 -0800 | [diff] [blame] | 22 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 23 | void onDraw(SkCanvas* canvas) override { |
reed | 19d8f9f | 2015-01-29 10:48:16 -0800 | [diff] [blame] | 24 | canvas->translate(20, 20); |
| 25 | |
| 26 | SkRect r = SkRect::MakeWH(1000, 1000); |
| 27 | |
| 28 | SkPaint paint; |
| 29 | paint.setAntiAlias(true); |
| 30 | paint.setStyle(SkPaint::kStroke_Style); |
| 31 | paint.setStrokeWidth(15); |
| 32 | |
| 33 | const SkScalar inset = paint.getStrokeWidth() + 4; |
| 34 | const SkScalar sweepAngle = 345; |
| 35 | SkRandom rand; |
| 36 | |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 37 | SkScalar sign = 1; |
reed | 19d8f9f | 2015-01-29 10:48:16 -0800 | [diff] [blame] | 38 | while (r.width() > paint.getStrokeWidth() * 3) { |
| 39 | paint.setColor(rand.nextU() | (0xFF << 24)); |
| 40 | SkScalar startAngle = rand.nextUScalar1() * 360; |
| 41 | |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 42 | SkScalar speed = SkScalarSqrt(16 / r.width()) * 0.5f; |
| 43 | startAngle += fRotate * 360 * speed * sign; |
| 44 | |
reed | 19d8f9f | 2015-01-29 10:48:16 -0800 | [diff] [blame] | 45 | SkPath path; |
| 46 | path.addArc(r, startAngle, sweepAngle); |
| 47 | canvas->drawPath(path, paint); |
| 48 | |
| 49 | r.inset(inset, inset); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 50 | sign = -sign; |
reed | 19d8f9f | 2015-01-29 10:48:16 -0800 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 54 | bool onAnimate(const SkAnimTimer& timer) override { |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 55 | fRotate = timer.scaled(1, 360); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 56 | return true; |
| 57 | } |
| 58 | |
reed | 19d8f9f | 2015-01-29 10:48:16 -0800 | [diff] [blame] | 59 | private: |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 60 | SkScalar fRotate; |
reed | 19d8f9f | 2015-01-29 10:48:16 -0800 | [diff] [blame] | 61 | typedef skiagm::GM INHERITED; |
| 62 | }; |
| 63 | DEF_GM( return new AddArcGM; ) |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 64 | |
| 65 | /////////////////////////////////////////////////// |
| 66 | |
| 67 | #define R 400 |
| 68 | |
| 69 | class AddArcMeasGM : public skiagm::GM { |
| 70 | public: |
| 71 | AddArcMeasGM() {} |
| 72 | |
| 73 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 74 | SkString onShortName() override { return SkString("addarc_meas"); } |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 75 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 76 | SkISize onISize() override { return SkISize::Make(2*R + 40, 2*R + 40); } |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 77 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 78 | void onDraw(SkCanvas* canvas) override { |
reed | 61adb1b | 2015-02-09 13:01:05 -0800 | [diff] [blame] | 79 | canvas->translate(R + 20, R + 20); |
| 80 | |
| 81 | SkPaint paint; |
| 82 | paint.setAntiAlias(true); |
| 83 | paint.setStyle(SkPaint::kStroke_Style); |
| 84 | |
| 85 | SkPaint measPaint; |
| 86 | measPaint.setAntiAlias(true); |
| 87 | measPaint.setColor(SK_ColorRED); |
| 88 | |
| 89 | const SkRect oval = SkRect::MakeLTRB(-R, -R, R, R); |
| 90 | canvas->drawOval(oval, paint); |
| 91 | |
| 92 | for (SkScalar deg = 0; deg < 360; deg += 10) { |
| 93 | const SkScalar rad = SkDegreesToRadians(deg); |
| 94 | SkScalar rx = SkScalarCos(rad) * R; |
| 95 | SkScalar ry = SkScalarSin(rad) * R; |
| 96 | |
| 97 | canvas->drawLine(0, 0, rx, ry, paint); |
| 98 | |
| 99 | SkPath path; |
| 100 | path.addArc(oval, 0, deg); |
| 101 | SkPathMeasure meas(path, false); |
| 102 | SkScalar arcLen = rad * R; |
| 103 | SkPoint pos; |
| 104 | if (meas.getPosTan(arcLen, &pos, NULL)) { |
| 105 | canvas->drawLine(0, 0, pos.x(), pos.y(), measPaint); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | typedef skiagm::GM INHERITED; |
| 112 | }; |
| 113 | DEF_GM( return new AddArcMeasGM; ) |
reed | 8ed666d | 2015-02-10 17:44:26 -0800 | [diff] [blame] | 114 | |
| 115 | /////////////////////////////////////////////////// |
| 116 | |
| 117 | // Emphasize drawing a stroked oval (containing conics) and then scaling the results up, |
| 118 | // to ensure that we compute the stroke taking the CTM into account |
| 119 | // |
| 120 | class StrokeCircleGM : public skiagm::GM { |
| 121 | public: |
| 122 | StrokeCircleGM() : fRotate(0) {} |
| 123 | |
| 124 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 125 | SkString onShortName() override { return SkString("strokecircle"); } |
reed | 8ed666d | 2015-02-10 17:44:26 -0800 | [diff] [blame] | 126 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 127 | SkISize onISize() override { return SkISize::Make(520, 520); } |
reed | 8ed666d | 2015-02-10 17:44:26 -0800 | [diff] [blame] | 128 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 129 | void onDraw(SkCanvas* canvas) override { |
reed | 8ed666d | 2015-02-10 17:44:26 -0800 | [diff] [blame] | 130 | canvas->scale(20, 20); |
| 131 | canvas->translate(13, 13); |
| 132 | |
| 133 | SkPaint paint; |
| 134 | paint.setAntiAlias(true); |
| 135 | paint.setStyle(SkPaint::kStroke_Style); |
| 136 | paint.setStrokeWidth(SK_Scalar1 / 2); |
| 137 | |
| 138 | const SkScalar delta = paint.getStrokeWidth() * 3 / 2; |
| 139 | SkRect r = SkRect::MakeXYWH(-12, -12, 24, 24); |
| 140 | SkRandom rand; |
| 141 | |
| 142 | SkScalar sign = 1; |
| 143 | while (r.width() > paint.getStrokeWidth() * 2) { |
| 144 | SkAutoCanvasRestore acr(canvas, true); |
| 145 | canvas->rotate(fRotate * sign); |
| 146 | |
| 147 | paint.setColor(rand.nextU() | (0xFF << 24)); |
| 148 | canvas->drawOval(r, paint); |
| 149 | r.inset(delta, delta); |
| 150 | sign = -sign; |
| 151 | } |
| 152 | } |
| 153 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 154 | bool onAnimate(const SkAnimTimer& timer) override { |
reed | 8ed666d | 2015-02-10 17:44:26 -0800 | [diff] [blame] | 155 | fRotate = timer.scaled(60, 360); |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | private: |
| 160 | SkScalar fRotate; |
| 161 | |
| 162 | typedef skiagm::GM INHERITED; |
| 163 | }; |
| 164 | DEF_GM( return new StrokeCircleGM; ) |
reed | 9e779d4 | 2015-02-17 11:43:14 -0800 | [diff] [blame] | 165 | |
| 166 | ////////////////////// |
| 167 | |
| 168 | static void html_canvas_arc(SkPath* path, SkScalar x, SkScalar y, SkScalar r, SkScalar start, |
| 169 | SkScalar end, bool ccw) { |
| 170 | SkRect bounds = { x - r, y - r, x + r, y + r }; |
| 171 | SkScalar sweep = ccw ? end - start : start - end; |
| 172 | path->arcTo(bounds, start, sweep, false); |
| 173 | } |
| 174 | |
| 175 | // Lifted from canvas-arc-circumference-fill-diffs.html |
| 176 | class ManyArcsGM : public skiagm::GM { |
| 177 | public: |
| 178 | ManyArcsGM() {} |
| 179 | |
| 180 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 181 | SkString onShortName() override { return SkString("manyarcs"); } |
reed | 9e779d4 | 2015-02-17 11:43:14 -0800 | [diff] [blame] | 182 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 183 | SkISize onISize() override { return SkISize::Make(620, 330); } |
reed | 9e779d4 | 2015-02-17 11:43:14 -0800 | [diff] [blame] | 184 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 185 | void onDraw(SkCanvas* canvas) override { |
reed | 9e779d4 | 2015-02-17 11:43:14 -0800 | [diff] [blame] | 186 | SkPaint paint; |
| 187 | paint.setAntiAlias(true); |
| 188 | paint.setStyle(SkPaint::kStroke_Style); |
| 189 | |
| 190 | canvas->translate(10, 10); |
| 191 | |
| 192 | // 20 angles. |
| 193 | SkScalar sweepAngles[] = { |
| 194 | -123.7f, -2.3f, -2, -1, -0.3f, -0.000001f, 0, 0.000001f, 0.3f, 0.7f, |
| 195 | 1, 1.3f, 1.5f, 1.7f, 1.99999f, 2, 2.00001f, 2.3f, 4.3f, 3934723942837.3f |
| 196 | }; |
| 197 | for (size_t i = 0; i < SK_ARRAY_COUNT(sweepAngles); ++i) { |
| 198 | sweepAngles[i] *= 180; |
| 199 | } |
| 200 | |
| 201 | SkScalar startAngles[] = { -1, -0.5f, 0, 0.5f }; |
| 202 | for (size_t i = 0; i < SK_ARRAY_COUNT(startAngles); ++i) { |
| 203 | startAngles[i] *= 180; |
| 204 | } |
| 205 | |
| 206 | bool anticlockwise = false; |
| 207 | SkScalar sign = 1; |
| 208 | for (size_t i = 0; i < SK_ARRAY_COUNT(startAngles) * 2; ++i) { |
| 209 | if (i == SK_ARRAY_COUNT(startAngles)) { |
| 210 | anticlockwise = true; |
| 211 | sign = -1; |
| 212 | } |
| 213 | SkScalar startAngle = startAngles[i % SK_ARRAY_COUNT(startAngles)] * sign; |
| 214 | canvas->save(); |
| 215 | for (size_t j = 0; j < SK_ARRAY_COUNT(sweepAngles); ++j) { |
| 216 | SkPath path; |
| 217 | path.moveTo(0, 2); |
| 218 | html_canvas_arc(&path, 18, 15, 10, startAngle, startAngle + (sweepAngles[j] * sign), |
| 219 | anticlockwise); |
| 220 | path.lineTo(0, 28); |
| 221 | canvas->drawPath(path, paint); |
| 222 | canvas->translate(30, 0); |
| 223 | } |
| 224 | canvas->restore(); |
| 225 | canvas->translate(0, 40); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | private: |
| 230 | typedef skiagm::GM INHERITED; |
| 231 | }; |
| 232 | DEF_GM( return new ManyArcsGM; ) |
| 233 | |