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