fmalita | 1a178ca | 2015-01-15 06:01:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 "SkBitmap.h" |
| 9 | #include "SkCanvas.h" |
Mike Reed | 926e193 | 2018-01-29 15:56:11 -0500 | [diff] [blame] | 10 | #include "SkPath.h" |
fmalita | 1a178ca | 2015-01-15 06:01:23 -0800 | [diff] [blame] | 11 | #include "SkRect.h" |
Mike Reed | f581777 | 2018-01-09 15:36:51 -0500 | [diff] [blame] | 12 | #include "SkRectPriv.h" |
fmalita | 1a178ca | 2015-01-15 06:01:23 -0800 | [diff] [blame] | 13 | #include "Test.h" |
| 14 | |
| 15 | static bool has_green_pixels(const SkBitmap& bm) { |
| 16 | for (int j = 0; j < bm.height(); ++j) { |
| 17 | for (int i = 0; i < bm.width(); ++i) { |
| 18 | if (SkColorGetG(bm.getColor(i, j))) { |
| 19 | return true; |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | return false; |
| 25 | } |
| 26 | |
| 27 | static void test_stroke_width_clipping(skiatest::Reporter* reporter) { |
| 28 | SkBitmap bm; |
| 29 | bm.allocN32Pixels(100, 10); |
| 30 | bm.eraseColor(SK_ColorTRANSPARENT); |
| 31 | |
| 32 | SkCanvas canvas(bm); |
| 33 | SkPaint paint; |
| 34 | paint.setStyle(SkPaint::kStroke_Style); |
| 35 | paint.setStrokeWidth(10); |
| 36 | paint.setColor(0xff00ff00); |
| 37 | |
| 38 | // clip out the left half of our canvas |
| 39 | canvas.clipRect(SkRect::MakeXYWH(51, 0, 49, 100)); |
| 40 | |
| 41 | // no stroke bleed should be visible |
| 42 | canvas.drawRect(SkRect::MakeWH(44, 100), paint); |
| 43 | REPORTER_ASSERT(reporter, !has_green_pixels(bm)); |
| 44 | |
| 45 | // right stroke edge should bleed into the visible area |
| 46 | canvas.scale(2, 2); |
| 47 | canvas.drawRect(SkRect::MakeWH(22, 50), paint); |
| 48 | REPORTER_ASSERT(reporter, has_green_pixels(bm)); |
| 49 | } |
| 50 | |
aleksandar.stojiljkovic | 7600183 | 2015-11-02 13:28:51 -0800 | [diff] [blame] | 51 | static void test_skbug4406(skiatest::Reporter* reporter) { |
| 52 | SkBitmap bm; |
| 53 | bm.allocN32Pixels(10, 10); |
| 54 | bm.eraseColor(SK_ColorTRANSPARENT); |
| 55 | |
| 56 | SkCanvas canvas(bm); |
| 57 | const SkRect r = { 1.5f, 1, 3.5f, 3 }; |
| 58 | // draw filled green rect first |
| 59 | SkPaint paint; |
| 60 | paint.setStyle(SkPaint::kFill_Style); |
| 61 | paint.setColor(0xff00ff00); |
| 62 | paint.setStrokeWidth(1); |
| 63 | paint.setAntiAlias(true); |
| 64 | canvas.drawRect(r, paint); |
| 65 | |
| 66 | // paint black with stroke rect (that asserts in bug 4406) |
| 67 | // over the filled rect, it should cover it |
| 68 | paint.setStyle(SkPaint::kStroke_Style); |
| 69 | paint.setColor(0xff000000); |
| 70 | paint.setStrokeWidth(1); |
| 71 | canvas.drawRect(r, paint); |
| 72 | REPORTER_ASSERT(reporter, !has_green_pixels(bm)); |
| 73 | |
| 74 | // do it again with thinner stroke |
| 75 | paint.setStyle(SkPaint::kFill_Style); |
| 76 | paint.setColor(0xff00ff00); |
| 77 | paint.setStrokeWidth(1); |
| 78 | paint.setAntiAlias(true); |
| 79 | canvas.drawRect(r, paint); |
| 80 | // paint black with stroke rect (that asserts in bug 4406) |
| 81 | // over the filled rect, it doesnt cover it completelly with thinner stroke |
| 82 | paint.setStyle(SkPaint::kStroke_Style); |
| 83 | paint.setColor(0xff000000); |
| 84 | paint.setStrokeWidth(0.99f); |
| 85 | canvas.drawRect(r, paint); |
| 86 | REPORTER_ASSERT(reporter, has_green_pixels(bm)); |
| 87 | } |
| 88 | |
fmalita | 1a178ca | 2015-01-15 06:01:23 -0800 | [diff] [blame] | 89 | DEF_TEST(Rect, reporter) { |
| 90 | test_stroke_width_clipping(reporter); |
aleksandar.stojiljkovic | 7600183 | 2015-11-02 13:28:51 -0800 | [diff] [blame] | 91 | test_skbug4406(reporter); |
fmalita | 1a178ca | 2015-01-15 06:01:23 -0800 | [diff] [blame] | 92 | } |
Mike Reed | 185ffe9 | 2018-01-08 17:09:54 -0500 | [diff] [blame] | 93 | |
| 94 | DEF_TEST(Rect_grow, reporter) { |
| 95 | test_stroke_width_clipping(reporter); |
| 96 | test_skbug4406(reporter); |
| 97 | } |
Mike Reed | f581777 | 2018-01-09 15:36:51 -0500 | [diff] [blame] | 98 | |
Mike Reed | 926e193 | 2018-01-29 15:56:11 -0500 | [diff] [blame] | 99 | DEF_TEST(Rect_path_nan, reporter) { |
| 100 | SkRect r = { 0, 0, SK_ScalarNaN, 100 }; |
| 101 | SkPath p; |
| 102 | p.addRect(r); |
| 103 | // path normally just jams its bounds to be r, but it must notice that r is non-finite |
| 104 | REPORTER_ASSERT(reporter, !p.isFinite()); |
| 105 | } |
| 106 | |
Mike Reed | f581777 | 2018-01-09 15:36:51 -0500 | [diff] [blame] | 107 | DEF_TEST(Rect_largest, reporter) { |
Mike Reed | 8008df1 | 2018-01-17 12:20:04 -0500 | [diff] [blame] | 108 | REPORTER_ASSERT(reporter, !SkRectPriv::MakeILarge().isEmpty()); |
Mike Reed | f581777 | 2018-01-09 15:36:51 -0500 | [diff] [blame] | 109 | REPORTER_ASSERT(reporter, SkRectPriv::MakeILargestInverted().isEmpty()); |
| 110 | |
| 111 | REPORTER_ASSERT(reporter, !SkRectPriv::MakeLargest().isEmpty()); |
Mike Reed | 8008df1 | 2018-01-17 12:20:04 -0500 | [diff] [blame] | 112 | REPORTER_ASSERT(reporter, !SkRectPriv::MakeLargeS32().isEmpty()); |
Mike Reed | f581777 | 2018-01-09 15:36:51 -0500 | [diff] [blame] | 113 | REPORTER_ASSERT(reporter, SkRectPriv::MakeLargestInverted().isEmpty()); |
| 114 | } |
| 115 | |
Cary Clark | c06754b | 2018-05-16 21:28:55 -0400 | [diff] [blame^] | 116 | /* |
| 117 | * Test the setBounds always handles non-finite values correctly: |
| 118 | * - setBoundsCheck should return false, and set the rect to all zeros |
| 119 | * - setBoundsNoCheck should ensure that rect.isFinite() is false (definitely NOT all zeros) |
| 120 | */ |
| 121 | DEF_TEST(Rect_setbounds, reporter) { |
| 122 | const SkPoint p0[] = { { SK_ScalarInfinity, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 } }; |
| 123 | const SkPoint p1[] = { { 0, SK_ScalarInfinity }, { 1, 1 }, { 2, 2 }, { 3, 3 } }; |
| 124 | const SkPoint p2[] = { { SK_ScalarNaN, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 } }; |
| 125 | const SkPoint p3[] = { { 0, SK_ScalarNaN }, { 1, 1 }, { 2, 2 }, { 3, 3 } }; |
| 126 | |
| 127 | SkRect r; |
| 128 | const SkRect zeror = { 0, 0, 0, 0 }; |
| 129 | for (const SkPoint* pts : { p0, p1, p2, p3 }) { |
| 130 | for (int n = 1; n <= 4; ++n) { |
| 131 | bool isfinite = r.setBoundsCheck(pts, n); |
| 132 | REPORTER_ASSERT(reporter, !isfinite); |
| 133 | REPORTER_ASSERT(reporter, r == zeror); |
| 134 | |
| 135 | r.setBoundsNoCheck(pts, n); |
| 136 | if (r.isFinite()) |
| 137 | r.setBoundsNoCheck(pts, n); |
| 138 | REPORTER_ASSERT(reporter, !r.isFinite()); |
| 139 | } |
| 140 | } |
| 141 | } |