liyuqian | e60d855 | 2016-10-03 13:49:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 "SkPath.h" |
| 10 | #include "SkScan.h" |
| 11 | |
| 12 | #define W 800 |
| 13 | #define H 800 |
| 14 | |
| 15 | class AnalyticAntiAliasConvexGM : public skiagm::GM { |
| 16 | public: |
| 17 | AnalyticAntiAliasConvexGM() {} |
| 18 | |
| 19 | protected: |
| 20 | |
| 21 | SkString onShortName() override { |
| 22 | return SkString("analytic_antialias_convex"); |
| 23 | } |
| 24 | |
| 25 | SkISize onISize() override { |
| 26 | return SkISize::Make(W, H); |
| 27 | } |
| 28 | |
| 29 | void onDraw(SkCanvas* canvas) override { |
| 30 | SkPaint p; |
| 31 | p.setColor(SK_ColorRED); |
| 32 | p.setAntiAlias(true); |
| 33 | |
| 34 | canvas->clear(0xFFFFFFFF); |
| 35 | |
| 36 | canvas->save(); |
| 37 | |
| 38 | SkScalar y = 0; |
| 39 | |
| 40 | canvas->translate(0, y); |
| 41 | canvas->rotate(1); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 42 | canvas->drawRect({ 20, 20, 200, 200 }, p); |
liyuqian | e60d855 | 2016-10-03 13:49:37 -0700 | [diff] [blame] | 43 | canvas->restore(); |
| 44 | |
| 45 | y += 200; |
| 46 | |
Yuqian Li | 63f69cd | 2018-04-18 15:09:44 -0400 | [diff] [blame] | 47 | canvas->save(); |
liyuqian | e60d855 | 2016-10-03 13:49:37 -0700 | [diff] [blame] | 48 | canvas->translate(0, y); |
| 49 | canvas->rotate(1); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 50 | canvas->drawRect({ 20, 20, 20.2f, 200 }, p); |
| 51 | canvas->drawRect({ 20, 200, 200, 200.1f }, p); |
liyuqian | e60d855 | 2016-10-03 13:49:37 -0700 | [diff] [blame] | 52 | canvas->drawCircle(100, 100, 30, p); |
| 53 | canvas->restore(); |
liyuqian | 451ceba | 2016-11-07 08:10:44 -0800 | [diff] [blame] | 54 | |
| 55 | // The following path is empty but it'll reveal bug chrome:662914 |
| 56 | SkPath path; |
| 57 | path.moveTo(SkBits2Float(0x429b9d5c), SkBits2Float(0x4367a041)); // 77.8073f, 231.626f |
| 58 | // 77.8075f, 231.626f, 77.8074f, 231.625f, 77.8073f, 231.625f |
| 59 | path.cubicTo(SkBits2Float(0x429b9d71), SkBits2Float(0x4367a022), |
| 60 | SkBits2Float(0x429b9d64), SkBits2Float(0x4367a009), |
| 61 | SkBits2Float(0x429b9d50), SkBits2Float(0x43679ff2)); |
| 62 | path.lineTo(SkBits2Float(0x429b9d5c), SkBits2Float(0x4367a041)); // 77.8073f, 231.626f |
| 63 | path.close(); |
| 64 | canvas->drawPath(path, p); |
Yuqian Li | 0038b7f | 2016-11-14 11:51:41 -0500 | [diff] [blame] | 65 | |
| 66 | // The following path reveals a subtle SkAnalyticQuadraticEdge::updateQuadratic bug: |
| 67 | // we should not use any snapped y for the intermediate values whose error may accumulate; |
| 68 | // snapping should only be allowed once before updateLine. |
| 69 | path.reset(); |
| 70 | path.moveTo(SkBits2Float(0x434ba71e), SkBits2Float(0x438a06d0)); // 203.653f, 276.053f |
| 71 | path.lineTo(SkBits2Float(0x43492a74), SkBits2Float(0x4396d70d)); // 201.166f, 301.68f |
| 72 | // 200.921f, 304.207f, 196.939f, 303.82f, 0.707107f |
| 73 | path.conicTo(SkBits2Float(0x4348ebaf), SkBits2Float(0x43981a75), |
| 74 | SkBits2Float(0x4344f079), SkBits2Float(0x4397e900), SkBits2Float(0x3f3504f3)); |
| 75 | path.close(); |
| 76 | // Manually setting convexity is required. Otherwise, this path will be considered concave. |
| 77 | path.setConvexity(SkPath::kConvex_Convexity); |
| 78 | canvas->drawPath(path, p); |
Yuqian Li | 4a657ad | 2018-02-07 19:04:34 +0800 | [diff] [blame] | 79 | |
| 80 | // skbug.com/7573 |
| 81 | y += 200; |
Yuqian Li | 63f69cd | 2018-04-18 15:09:44 -0400 | [diff] [blame] | 82 | canvas->save(); |
Yuqian Li | 4a657ad | 2018-02-07 19:04:34 +0800 | [diff] [blame] | 83 | canvas->translate(0, y); |
| 84 | p.setAntiAlias(true); |
| 85 | path.reset(); |
| 86 | path.moveTo(1.98009784f, 9.0162744f); |
| 87 | path.lineTo(47.843992f, 10.1922744f); |
| 88 | path.lineTo(47.804008f, 11.7597256f); |
| 89 | path.lineTo(1.93990216f, 10.5837256f); |
| 90 | canvas->drawPath(path, p); |
| 91 | canvas->restore(); |
Yuqian Li | 63f69cd | 2018-04-18 15:09:44 -0400 | [diff] [blame] | 92 | |
| 93 | // skbug.com/7813 |
| 94 | // t8888 splits the 800-high canvas into 3 pieces; the boundary is close to 266 and 534 |
| 95 | path.reset(); |
| 96 | path.moveTo(700, 266); |
| 97 | path.lineTo(710, 266); |
| 98 | path.lineTo(710, 534); |
| 99 | path.lineTo(700, 534); |
| 100 | canvas->drawPath(path, p); |
liyuqian | e60d855 | 2016-10-03 13:49:37 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | private: |
| 104 | typedef skiagm::GM INHERITED; |
| 105 | }; |
| 106 | |
Yuqian Li | 131c1fb | 2016-12-12 16:24:47 -0500 | [diff] [blame] | 107 | class AnalyticAntiAliasGeneralGM : public skiagm::GM { |
| 108 | public: |
| 109 | AnalyticAntiAliasGeneralGM() {} |
| 110 | |
| 111 | protected: |
| 112 | |
| 113 | SkString onShortName() override { |
| 114 | return SkString("analytic_antialias_general"); |
| 115 | } |
| 116 | |
| 117 | SkISize onISize() override { |
| 118 | return SkISize::Make(W, H); |
| 119 | } |
| 120 | |
| 121 | void onDraw(SkCanvas* canvas) override { |
| 122 | SkPaint p; |
| 123 | p.setColor(SK_ColorRED); |
| 124 | p.setAntiAlias(true); |
| 125 | |
| 126 | canvas->clear(0xFFFFFFFF); |
| 127 | |
| 128 | canvas->save(); |
| 129 | canvas->rotate(1); |
| 130 | const SkScalar R = 115.2f, C = 128.0f; |
| 131 | SkPath path; |
| 132 | path.moveTo(C + R, C); |
| 133 | for (int i = 1; i < 8; ++i) { |
| 134 | SkScalar a = 2.6927937f * i; |
| 135 | SkScalar cosine; |
| 136 | SkScalar sine = SkScalarSinCos(a, &cosine); |
| 137 | path.lineTo(C + R * cosine, C + R * sine); |
| 138 | } |
| 139 | canvas->drawPath(path, p); |
| 140 | canvas->restore(); |
| 141 | |
| 142 | canvas->save(); |
| 143 | canvas->translate(200, 0); |
| 144 | canvas->rotate(1); |
| 145 | p.setStyle(SkPaint::kStroke_Style); |
| 146 | p.setStrokeWidth(5); |
| 147 | canvas->drawPath(path, p); |
| 148 | canvas->restore(); |
Yuqian Li | fbe0793 | 2017-11-07 11:19:52 -0500 | [diff] [blame] | 149 | |
| 150 | |
| 151 | // The following two paths test if we correctly cumulates the alpha on the middle pixel |
| 152 | // column where the left rect and the right rect abut. |
| 153 | p.setStyle(SkPaint::kFill_Style); |
| 154 | canvas->translate(0, 300); |
| 155 | path.reset(); |
| 156 | path.addRect({20, 20, 100.4999f, 100}); |
| 157 | path.addRect({100.5001f, 20, 200, 100}); |
| 158 | canvas->drawPath(path, p); |
| 159 | |
| 160 | canvas->translate(300, 0); |
| 161 | path.reset(); |
| 162 | path.addRect({20, 20, 100.1f, 100}); |
| 163 | path.addRect({100.9f, 20, 200, 100}); |
| 164 | canvas->drawPath(path, p); |
Yuqian Li | 131c1fb | 2016-12-12 16:24:47 -0500 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | private: |
| 168 | typedef skiagm::GM INHERITED; |
| 169 | }; |
| 170 | |
| 171 | class AnalyticAntiAliasInverseGM : public skiagm::GM { |
| 172 | public: |
| 173 | AnalyticAntiAliasInverseGM() {} |
| 174 | |
| 175 | protected: |
| 176 | |
| 177 | SkString onShortName() override { |
| 178 | return SkString("analytic_antialias_inverse"); |
| 179 | } |
| 180 | |
| 181 | SkISize onISize() override { |
| 182 | return SkISize::Make(W, H); |
| 183 | } |
| 184 | |
| 185 | void onDraw(SkCanvas* canvas) override { |
| 186 | SkPaint p; |
| 187 | p.setColor(SK_ColorRED); |
| 188 | p.setAntiAlias(true); |
| 189 | |
| 190 | canvas->save(); |
| 191 | |
| 192 | SkPath path; |
| 193 | path.addCircle(100, 100, 30); |
| 194 | path.setFillType(SkPath::kInverseWinding_FillType); |
| 195 | canvas->drawPath(path, p); |
| 196 | canvas->restore(); |
| 197 | } |
| 198 | |
| 199 | private: |
| 200 | typedef skiagm::GM INHERITED; |
| 201 | }; |
| 202 | |
liyuqian | e60d855 | 2016-10-03 13:49:37 -0700 | [diff] [blame] | 203 | DEF_GM( return new AnalyticAntiAliasConvexGM; ) |
Yuqian Li | 131c1fb | 2016-12-12 16:24:47 -0500 | [diff] [blame] | 204 | DEF_GM( return new AnalyticAntiAliasGeneralGM; ) |
| 205 | DEF_GM( return new AnalyticAntiAliasInverseGM; ) |