caryclark | a8d2ffb | 2014-06-24 07:55:11 -0700 | [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 | */ |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 7 | #include "include/core/SkCanvas.h" |
| 8 | #include "include/utils/SkRandom.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "tests/PathOpsExtendedTest.h" |
| 10 | #include "tests/PathOpsThreadedCommon.h" |
| 11 | #include "tests/Test.h" |
caryclark | a8d2ffb | 2014-06-24 07:55:11 -0700 | [diff] [blame] | 12 | |
| 13 | static void testTightBoundsLines(PathOpsThreadState* data) { |
| 14 | SkRandom ran; |
| 15 | for (int index = 0; index < 1000; ++index) { |
| 16 | SkPath path; |
| 17 | int contourCount = ran.nextRangeU(1, 10); |
| 18 | for (int cIndex = 0; cIndex < contourCount; ++cIndex) { |
| 19 | int lineCount = ran.nextRangeU(1, 10); |
| 20 | path.moveTo(ran.nextRangeF(-1000, 1000), ran.nextRangeF(-1000, 1000)); |
| 21 | for (int lIndex = 0; lIndex < lineCount; ++lIndex) { |
| 22 | path.lineTo(ran.nextRangeF(-1000, 1000), ran.nextRangeF(-1000, 1000)); |
| 23 | } |
| 24 | if (ran.nextBool()) { |
| 25 | path.close(); |
| 26 | } |
| 27 | } |
| 28 | SkRect classicBounds = path.getBounds(); |
| 29 | SkRect tightBounds; |
| 30 | REPORTER_ASSERT(data->fReporter, TightBounds(path, &tightBounds)); |
| 31 | REPORTER_ASSERT(data->fReporter, classicBounds == tightBounds); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | DEF_TEST(PathOpsTightBoundsLines, reporter) { |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 36 | initializeTests(reporter, "tightBoundsLines"); |
| 37 | PathOpsThreadedTestRunner testRunner(reporter); |
caryclark | a8d2ffb | 2014-06-24 07:55:11 -0700 | [diff] [blame] | 38 | int outerCount = reporter->allowExtendedTest() ? 100 : 1; |
| 39 | for (int index = 0; index < outerCount; ++index) { |
| 40 | for (int idx2 = 0; idx2 < 10; ++idx2) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 41 | *testRunner.fRunnables.append() = |
| 42 | new PathOpsThreadedRunnable(&testTightBoundsLines, 0, 0, 0, 0, &testRunner); |
caryclark | a8d2ffb | 2014-06-24 07:55:11 -0700 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | testRunner.render(); |
| 46 | } |
| 47 | |
| 48 | static void testTightBoundsQuads(PathOpsThreadState* data) { |
| 49 | SkRandom ran; |
| 50 | const int bitWidth = 32; |
| 51 | const int bitHeight = 32; |
| 52 | const float pathMin = 1; |
| 53 | const float pathMax = (float) (bitHeight - 2); |
| 54 | SkBitmap& bits = *data->fBitmap; |
| 55 | if (bits.width() == 0) { |
| 56 | bits.allocN32Pixels(bitWidth, bitHeight); |
| 57 | } |
| 58 | SkCanvas canvas(bits); |
| 59 | SkPaint paint; |
| 60 | for (int index = 0; index < 100; ++index) { |
| 61 | SkPath path; |
| 62 | int contourCount = ran.nextRangeU(1, 10); |
| 63 | for (int cIndex = 0; cIndex < contourCount; ++cIndex) { |
| 64 | int lineCount = ran.nextRangeU(1, 10); |
| 65 | path.moveTo(ran.nextRangeF(1, pathMax), ran.nextRangeF(pathMin, pathMax)); |
| 66 | for (int lIndex = 0; lIndex < lineCount; ++lIndex) { |
| 67 | if (ran.nextBool()) { |
| 68 | path.lineTo(ran.nextRangeF(pathMin, pathMax), ran.nextRangeF(pathMin, pathMax)); |
| 69 | } else { |
| 70 | path.quadTo(ran.nextRangeF(pathMin, pathMax), ran.nextRangeF(pathMin, pathMax), |
| 71 | ran.nextRangeF(pathMin, pathMax), ran.nextRangeF(pathMin, pathMax)); |
| 72 | } |
| 73 | } |
| 74 | if (ran.nextBool()) { |
| 75 | path.close(); |
| 76 | } |
| 77 | } |
| 78 | SkRect classicBounds = path.getBounds(); |
| 79 | SkRect tightBounds; |
| 80 | REPORTER_ASSERT(data->fReporter, TightBounds(path, &tightBounds)); |
| 81 | REPORTER_ASSERT(data->fReporter, classicBounds.contains(tightBounds)); |
| 82 | canvas.drawColor(SK_ColorWHITE); |
| 83 | canvas.drawPath(path, paint); |
| 84 | SkIRect bitsWritten = {31, 31, 0, 0}; |
| 85 | for (int y = 0; y < bitHeight; ++y) { |
| 86 | uint32_t* addr1 = data->fBitmap->getAddr32(0, y); |
| 87 | bool lineWritten = false; |
| 88 | for (int x = 0; x < bitWidth; ++x) { |
| 89 | if (addr1[x] == (uint32_t) -1) { |
| 90 | continue; |
| 91 | } |
| 92 | lineWritten = true; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 93 | bitsWritten.fLeft = std::min(bitsWritten.fLeft, x); |
| 94 | bitsWritten.fRight = std::max(bitsWritten.fRight, x); |
caryclark | a8d2ffb | 2014-06-24 07:55:11 -0700 | [diff] [blame] | 95 | } |
| 96 | if (!lineWritten) { |
| 97 | continue; |
| 98 | } |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 99 | bitsWritten.fTop = std::min(bitsWritten.fTop, y); |
| 100 | bitsWritten.fBottom = std::max(bitsWritten.fBottom, y); |
caryclark | a8d2ffb | 2014-06-24 07:55:11 -0700 | [diff] [blame] | 101 | } |
| 102 | if (!bitsWritten.isEmpty()) { |
| 103 | SkIRect tightOut; |
| 104 | tightBounds.roundOut(&tightOut); |
| 105 | REPORTER_ASSERT(data->fReporter, tightOut.contains(bitsWritten)); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | DEF_TEST(PathOpsTightBoundsQuads, reporter) { |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 111 | initializeTests(reporter, "tightBoundsQuads"); |
| 112 | PathOpsThreadedTestRunner testRunner(reporter); |
caryclark | a8d2ffb | 2014-06-24 07:55:11 -0700 | [diff] [blame] | 113 | int outerCount = reporter->allowExtendedTest() ? 100 : 1; |
| 114 | for (int index = 0; index < outerCount; ++index) { |
| 115 | for (int idx2 = 0; idx2 < 10; ++idx2) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 116 | *testRunner.fRunnables.append() = |
| 117 | new PathOpsThreadedRunnable(&testTightBoundsQuads, 0, 0, 0, 0, &testRunner); |
caryclark | a8d2ffb | 2014-06-24 07:55:11 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | testRunner.render(); |
| 121 | } |
caryclark | 61c21cd | 2016-10-05 13:23:00 -0700 | [diff] [blame] | 122 | |
| 123 | DEF_TEST(PathOpsTightBoundsMove, reporter) { |
| 124 | SkPath path; |
| 125 | path.moveTo(10, 10); |
| 126 | path.close(); |
| 127 | path.moveTo(20, 20); |
| 128 | path.lineTo(20, 20); |
| 129 | path.close(); |
| 130 | path.moveTo(15, 15); |
| 131 | path.lineTo(15, 15); |
| 132 | path.close(); |
| 133 | const SkRect& bounds = path.getBounds(); |
| 134 | SkRect tight; |
| 135 | REPORTER_ASSERT(reporter, TightBounds(path, &tight)); |
| 136 | REPORTER_ASSERT(reporter, bounds == tight); |
| 137 | } |
| 138 | |
| 139 | DEF_TEST(PathOpsTightBoundsMoveOne, reporter) { |
| 140 | SkPath path; |
| 141 | path.moveTo(20, 20); |
| 142 | const SkRect& bounds = path.getBounds(); |
| 143 | SkRect tight; |
| 144 | REPORTER_ASSERT(reporter, TightBounds(path, &tight)); |
| 145 | REPORTER_ASSERT(reporter, bounds == tight); |
| 146 | } |
| 147 | |
| 148 | DEF_TEST(PathOpsTightBoundsMoveTwo, reporter) { |
| 149 | SkPath path; |
| 150 | path.moveTo(20, 20); |
| 151 | path.moveTo(40, 40); |
| 152 | const SkRect& bounds = path.getBounds(); |
| 153 | SkRect tight; |
| 154 | REPORTER_ASSERT(reporter, TightBounds(path, &tight)); |
| 155 | REPORTER_ASSERT(reporter, bounds == tight); |
| 156 | } |
| 157 | |
| 158 | DEF_TEST(PathOpsTightBoundsTiny, reporter) { |
| 159 | SkPath path; |
| 160 | path.moveTo(1, 1); |
| 161 | path.quadTo(1.000001f, 1, 1, 1); |
| 162 | const SkRect& bounds = path.getBounds(); |
| 163 | SkRect tight; |
| 164 | REPORTER_ASSERT(reporter, TightBounds(path, &tight)); |
| 165 | SkRect moveBounds = {1, 1, 1, 1}; |
| 166 | REPORTER_ASSERT(reporter, bounds != tight); |
| 167 | REPORTER_ASSERT(reporter, moveBounds == tight); |
| 168 | } |
| 169 | |
| 170 | DEF_TEST(PathOpsTightBoundsWellBehaved, reporter) { |
| 171 | SkPath path; |
| 172 | path.moveTo(1, 1); |
| 173 | path.quadTo(2, 3, 4, 5); |
| 174 | const SkRect& bounds = path.getBounds(); |
| 175 | SkRect tight; |
| 176 | REPORTER_ASSERT(reporter, TightBounds(path, &tight)); |
| 177 | REPORTER_ASSERT(reporter, bounds == tight); |
| 178 | } |
| 179 | |
| 180 | DEF_TEST(PathOpsTightBoundsIllBehaved, reporter) { |
| 181 | SkPath path; |
| 182 | path.moveTo(1, 1); |
| 183 | path.quadTo(4, 3, 2, 2); |
| 184 | const SkRect& bounds = path.getBounds(); |
| 185 | SkRect tight; |
| 186 | REPORTER_ASSERT(reporter, TightBounds(path, &tight)); |
| 187 | REPORTER_ASSERT(reporter, bounds != tight); |
| 188 | } |
| 189 | |
Fredrik Söderquist | b60e864 | 2017-01-04 13:31:47 +0100 | [diff] [blame] | 190 | DEF_TEST(PathOpsTightBoundsIllBehavedScaled, reporter) { |
| 191 | SkPath path; |
| 192 | path.moveTo(0, 0); |
| 193 | path.quadTo(1048578, 1048577, 1048576, 1048576); |
| 194 | const SkRect& bounds = path.getBounds(); |
| 195 | SkRect tight; |
| 196 | REPORTER_ASSERT(reporter, TightBounds(path, &tight)); |
| 197 | REPORTER_ASSERT(reporter, bounds != tight); |
| 198 | REPORTER_ASSERT(reporter, tight.right() == 1048576); |
| 199 | REPORTER_ASSERT(reporter, tight.bottom() == 1048576); |
| 200 | } |