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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | d1701ba | 2019-04-30 13:44:26 -0400 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkPath.h" |
Ben Wagner | d1701ba | 2019-04-30 13:44:26 -0400 | [diff] [blame] | 13 | #include "include/core/SkScalar.h" |
| 14 | #include "include/private/SkFloatBits.h" |
liyuqian | e60d855 | 2016-10-03 13:49:37 -0700 | [diff] [blame] | 15 | |
| 16 | #define W 800 |
| 17 | #define H 800 |
| 18 | |
Hal Canary | 607c44f | 2019-01-23 10:40:02 -0500 | [diff] [blame] | 19 | DEF_SIMPLE_GM(analytic_antialias_convex, canvas, W, H) { |
liyuqian | e60d855 | 2016-10-03 13:49:37 -0700 | [diff] [blame] | 20 | SkPaint p; |
| 21 | p.setColor(SK_ColorRED); |
| 22 | p.setAntiAlias(true); |
| 23 | |
| 24 | canvas->clear(0xFFFFFFFF); |
| 25 | |
| 26 | canvas->save(); |
| 27 | |
| 28 | SkScalar y = 0; |
| 29 | |
| 30 | canvas->translate(0, y); |
| 31 | canvas->rotate(1); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 32 | canvas->drawRect({ 20, 20, 200, 200 }, p); |
liyuqian | e60d855 | 2016-10-03 13:49:37 -0700 | [diff] [blame] | 33 | canvas->restore(); |
| 34 | |
| 35 | y += 200; |
| 36 | |
Yuqian Li | 63f69cd | 2018-04-18 15:09:44 -0400 | [diff] [blame] | 37 | canvas->save(); |
liyuqian | e60d855 | 2016-10-03 13:49:37 -0700 | [diff] [blame] | 38 | canvas->translate(0, y); |
| 39 | canvas->rotate(1); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 40 | canvas->drawRect({ 20, 20, 20.2f, 200 }, p); |
| 41 | canvas->drawRect({ 20, 200, 200, 200.1f }, p); |
liyuqian | e60d855 | 2016-10-03 13:49:37 -0700 | [diff] [blame] | 42 | canvas->drawCircle(100, 100, 30, p); |
| 43 | canvas->restore(); |
liyuqian | 451ceba | 2016-11-07 08:10:44 -0800 | [diff] [blame] | 44 | |
| 45 | // The following path is empty but it'll reveal bug chrome:662914 |
| 46 | SkPath path; |
| 47 | path.moveTo(SkBits2Float(0x429b9d5c), SkBits2Float(0x4367a041)); // 77.8073f, 231.626f |
| 48 | // 77.8075f, 231.626f, 77.8074f, 231.625f, 77.8073f, 231.625f |
| 49 | path.cubicTo(SkBits2Float(0x429b9d71), SkBits2Float(0x4367a022), |
| 50 | SkBits2Float(0x429b9d64), SkBits2Float(0x4367a009), |
| 51 | SkBits2Float(0x429b9d50), SkBits2Float(0x43679ff2)); |
| 52 | path.lineTo(SkBits2Float(0x429b9d5c), SkBits2Float(0x4367a041)); // 77.8073f, 231.626f |
| 53 | path.close(); |
| 54 | canvas->drawPath(path, p); |
Yuqian Li | 0038b7f | 2016-11-14 11:51:41 -0500 | [diff] [blame] | 55 | |
| 56 | // The following path reveals a subtle SkAnalyticQuadraticEdge::updateQuadratic bug: |
| 57 | // we should not use any snapped y for the intermediate values whose error may accumulate; |
| 58 | // snapping should only be allowed once before updateLine. |
| 59 | path.reset(); |
| 60 | path.moveTo(SkBits2Float(0x434ba71e), SkBits2Float(0x438a06d0)); // 203.653f, 276.053f |
| 61 | path.lineTo(SkBits2Float(0x43492a74), SkBits2Float(0x4396d70d)); // 201.166f, 301.68f |
| 62 | // 200.921f, 304.207f, 196.939f, 303.82f, 0.707107f |
| 63 | path.conicTo(SkBits2Float(0x4348ebaf), SkBits2Float(0x43981a75), |
| 64 | SkBits2Float(0x4344f079), SkBits2Float(0x4397e900), SkBits2Float(0x3f3504f3)); |
| 65 | path.close(); |
| 66 | // Manually setting convexity is required. Otherwise, this path will be considered concave. |
| 67 | path.setConvexity(SkPath::kConvex_Convexity); |
| 68 | canvas->drawPath(path, p); |
Yuqian Li | 4a657ad | 2018-02-07 19:04:34 +0800 | [diff] [blame] | 69 | |
| 70 | // skbug.com/7573 |
| 71 | y += 200; |
Yuqian Li | 63f69cd | 2018-04-18 15:09:44 -0400 | [diff] [blame] | 72 | canvas->save(); |
Yuqian Li | 4a657ad | 2018-02-07 19:04:34 +0800 | [diff] [blame] | 73 | canvas->translate(0, y); |
| 74 | p.setAntiAlias(true); |
| 75 | path.reset(); |
| 76 | path.moveTo(1.98009784f, 9.0162744f); |
| 77 | path.lineTo(47.843992f, 10.1922744f); |
| 78 | path.lineTo(47.804008f, 11.7597256f); |
| 79 | path.lineTo(1.93990216f, 10.5837256f); |
| 80 | canvas->drawPath(path, p); |
| 81 | canvas->restore(); |
Yuqian Li | 63f69cd | 2018-04-18 15:09:44 -0400 | [diff] [blame] | 82 | |
| 83 | // skbug.com/7813 |
| 84 | // t8888 splits the 800-high canvas into 3 pieces; the boundary is close to 266 and 534 |
| 85 | path.reset(); |
| 86 | path.moveTo(700, 266); |
| 87 | path.lineTo(710, 266); |
| 88 | path.lineTo(710, 534); |
| 89 | path.lineTo(700, 534); |
| 90 | canvas->drawPath(path, p); |
Hal Canary | 607c44f | 2019-01-23 10:40:02 -0500 | [diff] [blame] | 91 | } |
liyuqian | e60d855 | 2016-10-03 13:49:37 -0700 | [diff] [blame] | 92 | |
Hal Canary | 607c44f | 2019-01-23 10:40:02 -0500 | [diff] [blame] | 93 | DEF_SIMPLE_GM(analytic_antialias_general, canvas, W, H) { |
Yuqian Li | 131c1fb | 2016-12-12 16:24:47 -0500 | [diff] [blame] | 94 | SkPaint p; |
| 95 | p.setColor(SK_ColorRED); |
| 96 | p.setAntiAlias(true); |
| 97 | |
| 98 | canvas->clear(0xFFFFFFFF); |
| 99 | |
| 100 | canvas->save(); |
| 101 | canvas->rotate(1); |
| 102 | const SkScalar R = 115.2f, C = 128.0f; |
| 103 | SkPath path; |
| 104 | path.moveTo(C + R, C); |
| 105 | for (int i = 1; i < 8; ++i) { |
| 106 | SkScalar a = 2.6927937f * i; |
Brian Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 107 | path.lineTo(C + R * SkScalarCos(a), C + R * SkScalarSin(a)); |
Yuqian Li | 131c1fb | 2016-12-12 16:24:47 -0500 | [diff] [blame] | 108 | } |
| 109 | canvas->drawPath(path, p); |
| 110 | canvas->restore(); |
| 111 | |
| 112 | canvas->save(); |
| 113 | canvas->translate(200, 0); |
| 114 | canvas->rotate(1); |
| 115 | p.setStyle(SkPaint::kStroke_Style); |
| 116 | p.setStrokeWidth(5); |
| 117 | canvas->drawPath(path, p); |
| 118 | canvas->restore(); |
Yuqian Li | fbe0793 | 2017-11-07 11:19:52 -0500 | [diff] [blame] | 119 | |
| 120 | |
| 121 | // The following two paths test if we correctly cumulates the alpha on the middle pixel |
| 122 | // column where the left rect and the right rect abut. |
| 123 | p.setStyle(SkPaint::kFill_Style); |
| 124 | canvas->translate(0, 300); |
| 125 | path.reset(); |
| 126 | path.addRect({20, 20, 100.4999f, 100}); |
| 127 | path.addRect({100.5001f, 20, 200, 100}); |
| 128 | canvas->drawPath(path, p); |
| 129 | |
| 130 | canvas->translate(300, 0); |
| 131 | path.reset(); |
| 132 | path.addRect({20, 20, 100.1f, 100}); |
| 133 | path.addRect({100.9f, 20, 200, 100}); |
| 134 | canvas->drawPath(path, p); |
Hal Canary | 607c44f | 2019-01-23 10:40:02 -0500 | [diff] [blame] | 135 | } |
Yuqian Li | 131c1fb | 2016-12-12 16:24:47 -0500 | [diff] [blame] | 136 | |
Hal Canary | 607c44f | 2019-01-23 10:40:02 -0500 | [diff] [blame] | 137 | DEF_SIMPLE_GM(analytic_antialias_inverse, canvas, W, H) { |
Yuqian Li | 131c1fb | 2016-12-12 16:24:47 -0500 | [diff] [blame] | 138 | SkPaint p; |
| 139 | p.setColor(SK_ColorRED); |
| 140 | p.setAntiAlias(true); |
| 141 | |
| 142 | canvas->save(); |
| 143 | |
| 144 | SkPath path; |
| 145 | path.addCircle(100, 100, 30); |
| 146 | path.setFillType(SkPath::kInverseWinding_FillType); |
| 147 | canvas->drawPath(path, p); |
| 148 | canvas->restore(); |
Hal Canary | 607c44f | 2019-01-23 10:40:02 -0500 | [diff] [blame] | 149 | } |