Mike Reed | 22f246f | 2020-06-23 21:06:28 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 "include/core/SkPathBuilder.h" |
| 9 | #include "src/core/SkPathPriv.h" |
| 10 | #include "tests/Test.h" |
| 11 | |
| 12 | static void is_empty(skiatest::Reporter* reporter, const SkPath& p) { |
| 13 | REPORTER_ASSERT(reporter, p.getBounds().isEmpty()); |
| 14 | REPORTER_ASSERT(reporter, p.countPoints() == 0); |
| 15 | } |
| 16 | |
| 17 | DEF_TEST(pathbuilder, reporter) { |
| 18 | SkPathBuilder b; |
| 19 | |
| 20 | is_empty(reporter, b.snapshot()); |
| 21 | is_empty(reporter, b.detach()); |
| 22 | |
| 23 | b.moveTo(10, 10).lineTo(20, 20).quadTo(30, 10, 10, 20); |
| 24 | |
| 25 | SkPath p0 = b.snapshot(); |
| 26 | SkPath p1 = b.snapshot(); |
| 27 | SkPath p2 = b.detach(); |
| 28 | |
| 29 | REPORTER_ASSERT(reporter, p0.getBounds() == SkRect::MakeLTRB(10, 10, 30, 20)); |
| 30 | REPORTER_ASSERT(reporter, p0.countPoints() == 4); |
| 31 | |
| 32 | REPORTER_ASSERT(reporter, p0 == p1); |
| 33 | REPORTER_ASSERT(reporter, p0 == p2); |
| 34 | |
| 35 | is_empty(reporter, b.snapshot()); |
| 36 | is_empty(reporter, b.detach()); |
| 37 | } |
| 38 | |
| 39 | static bool check_points(const SkPath& path, const SkPoint expected[], size_t count) { |
| 40 | std::vector<SkPoint> iter_pts; |
| 41 | |
| 42 | for (auto [v, p, w] : SkPathPriv::Iterate(path)) { |
| 43 | switch (v) { |
| 44 | case SkPathVerb::kMove: |
| 45 | iter_pts.push_back(p[0]); |
| 46 | break; |
| 47 | case SkPathVerb::kLine: |
| 48 | iter_pts.push_back(p[1]); |
| 49 | break; |
| 50 | case SkPathVerb::kQuad: |
| 51 | case SkPathVerb::kConic: |
| 52 | iter_pts.push_back(p[1]); |
| 53 | iter_pts.push_back(p[2]); |
| 54 | break; |
| 55 | case SkPathVerb::kCubic: |
| 56 | iter_pts.push_back(p[1]); |
| 57 | iter_pts.push_back(p[2]); |
| 58 | iter_pts.push_back(p[3]); |
| 59 | break; |
| 60 | case SkPathVerb::kClose: |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | if (iter_pts.size() != count) { |
| 65 | return false; |
| 66 | } |
| 67 | for (size_t i = 0; i < count; ++i) { |
| 68 | if (iter_pts[i] != expected[i]) { |
| 69 | return false; |
| 70 | } |
| 71 | } |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | DEF_TEST(pathbuilder_missing_move, reporter) { |
| 76 | SkPathBuilder b; |
| 77 | |
| 78 | b.lineTo(10, 10).lineTo(20, 30); |
| 79 | const SkPoint pts0[] = { |
| 80 | {0, 0}, {10, 10}, {20, 30}, |
| 81 | }; |
| 82 | REPORTER_ASSERT(reporter, check_points(b.snapshot(), pts0, SK_ARRAY_COUNT(pts0))); |
| 83 | |
| 84 | b.reset().moveTo(20, 20).lineTo(10, 10).lineTo(20, 30).close().lineTo(60, 60); |
| 85 | const SkPoint pts1[] = { |
| 86 | {20, 20}, {10, 10}, {20, 30}, |
| 87 | {20, 20}, {60, 60}, |
| 88 | }; |
| 89 | REPORTER_ASSERT(reporter, check_points(b.snapshot(), pts1, SK_ARRAY_COUNT(pts1))); |
| 90 | } |
| 91 | |
| 92 | DEF_TEST(pathbuilder_addRect, reporter) { |
| 93 | const SkRect r = { 10, 20, 30, 40 }; |
| 94 | |
| 95 | for (int i = 0; i < 4; ++i) { |
| 96 | for (auto dir : {SkPathDirection::kCW, SkPathDirection::kCCW}) { |
| 97 | SkPathBuilder b; |
| 98 | b.addRect(r, dir, i); |
| 99 | auto bp = b.detach(); |
| 100 | |
| 101 | SkRect r2; |
| 102 | bool closed = false; |
| 103 | SkPathDirection dir2; |
| 104 | REPORTER_ASSERT(reporter, bp.isRect(&r2, &closed, &dir2)); |
| 105 | REPORTER_ASSERT(reporter, r2 == r); |
| 106 | REPORTER_ASSERT(reporter, closed); |
| 107 | REPORTER_ASSERT(reporter, dir == dir2); |
| 108 | |
| 109 | SkPath p; |
| 110 | p.addRect(r, dir, i); |
| 111 | REPORTER_ASSERT(reporter, p == bp); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | DEF_TEST(pathbuilder_addOval, reporter) { |
| 117 | const SkRect r = { 10, 20, 30, 40 }; |
| 118 | |
| 119 | for (int i = 0; i < 4; ++i) { |
| 120 | for (auto dir : {SkPathDirection::kCW, SkPathDirection::kCCW}) { |
| 121 | SkPathBuilder b; |
| 122 | b.addOval(r, dir, i); |
| 123 | auto bp = b.detach(); |
| 124 | |
| 125 | SkPath p; |
| 126 | p.addOval(r, dir, i); |
| 127 | REPORTER_ASSERT(reporter, p == bp); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | DEF_TEST(pathbuilder_addRRect, reporter) { |
| 133 | const SkRRect rr = SkRRect::MakeRectXY({ 10, 20, 30, 40 }, 5, 6); |
| 134 | |
| 135 | for (int i = 0; i < 4; ++i) { |
| 136 | for (auto dir : {SkPathDirection::kCW, SkPathDirection::kCCW}) { |
| 137 | SkPathBuilder b; |
| 138 | b.addRRect(rr, dir, i); |
| 139 | auto bp = b.detach(); |
| 140 | |
| 141 | SkPath p; |
| 142 | p.addRRect(rr, dir, i); |
| 143 | REPORTER_ASSERT(reporter, p == bp); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | #include "include/utils/SkRandom.h" |
| 149 | |
| 150 | DEF_TEST(pathbuilder_make, reporter) { |
| 151 | constexpr int N = 100; |
| 152 | uint8_t vbs[N]; |
| 153 | SkPoint pts[N]; |
| 154 | |
| 155 | SkRandom rand; |
| 156 | SkPathBuilder b; |
| 157 | b.moveTo(0, 0); |
| 158 | pts[0] = {0, 0}; vbs[0] = (uint8_t)SkPathVerb::kMove; |
| 159 | for (int i = 1; i < N; ++i) { |
| 160 | float x = rand.nextF(); |
| 161 | float y = rand.nextF(); |
| 162 | b.lineTo(x, y); |
| 163 | pts[i] = {x, y}; vbs[i] = (uint8_t)SkPathVerb::kLine; |
| 164 | } |
| 165 | auto p0 = b.detach(); |
Mike Reed | 92c33f3 | 2020-07-02 09:14:20 -0400 | [diff] [blame] | 166 | auto p1 = SkPath::Make(pts, N, vbs, N, nullptr, 0, p0.getFillType()); |
Mike Reed | 22f246f | 2020-06-23 21:06:28 -0400 | [diff] [blame] | 167 | REPORTER_ASSERT(reporter, p0 == p1); |
| 168 | } |