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" |
| 10 | #include "SkRect.h" |
Mike Reed | f581777 | 2018-01-09 15:36:51 -0500 | [diff] [blame] | 11 | #include "SkRectPriv.h" |
fmalita | 1a178ca | 2015-01-15 06:01:23 -0800 | [diff] [blame] | 12 | #include "Test.h" |
| 13 | |
| 14 | static bool has_green_pixels(const SkBitmap& bm) { |
| 15 | for (int j = 0; j < bm.height(); ++j) { |
| 16 | for (int i = 0; i < bm.width(); ++i) { |
| 17 | if (SkColorGetG(bm.getColor(i, j))) { |
| 18 | return true; |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | static void test_stroke_width_clipping(skiatest::Reporter* reporter) { |
| 27 | SkBitmap bm; |
| 28 | bm.allocN32Pixels(100, 10); |
| 29 | bm.eraseColor(SK_ColorTRANSPARENT); |
| 30 | |
| 31 | SkCanvas canvas(bm); |
| 32 | SkPaint paint; |
| 33 | paint.setStyle(SkPaint::kStroke_Style); |
| 34 | paint.setStrokeWidth(10); |
| 35 | paint.setColor(0xff00ff00); |
| 36 | |
| 37 | // clip out the left half of our canvas |
| 38 | canvas.clipRect(SkRect::MakeXYWH(51, 0, 49, 100)); |
| 39 | |
| 40 | // no stroke bleed should be visible |
| 41 | canvas.drawRect(SkRect::MakeWH(44, 100), paint); |
| 42 | REPORTER_ASSERT(reporter, !has_green_pixels(bm)); |
| 43 | |
| 44 | // right stroke edge should bleed into the visible area |
| 45 | canvas.scale(2, 2); |
| 46 | canvas.drawRect(SkRect::MakeWH(22, 50), paint); |
| 47 | REPORTER_ASSERT(reporter, has_green_pixels(bm)); |
| 48 | } |
| 49 | |
aleksandar.stojiljkovic | 7600183 | 2015-11-02 13:28:51 -0800 | [diff] [blame] | 50 | static void test_skbug4406(skiatest::Reporter* reporter) { |
| 51 | SkBitmap bm; |
| 52 | bm.allocN32Pixels(10, 10); |
| 53 | bm.eraseColor(SK_ColorTRANSPARENT); |
| 54 | |
| 55 | SkCanvas canvas(bm); |
| 56 | const SkRect r = { 1.5f, 1, 3.5f, 3 }; |
| 57 | // draw filled green rect first |
| 58 | SkPaint paint; |
| 59 | paint.setStyle(SkPaint::kFill_Style); |
| 60 | paint.setColor(0xff00ff00); |
| 61 | paint.setStrokeWidth(1); |
| 62 | paint.setAntiAlias(true); |
| 63 | canvas.drawRect(r, paint); |
| 64 | |
| 65 | // paint black with stroke rect (that asserts in bug 4406) |
| 66 | // over the filled rect, it should cover it |
| 67 | paint.setStyle(SkPaint::kStroke_Style); |
| 68 | paint.setColor(0xff000000); |
| 69 | paint.setStrokeWidth(1); |
| 70 | canvas.drawRect(r, paint); |
| 71 | REPORTER_ASSERT(reporter, !has_green_pixels(bm)); |
| 72 | |
| 73 | // do it again with thinner stroke |
| 74 | paint.setStyle(SkPaint::kFill_Style); |
| 75 | paint.setColor(0xff00ff00); |
| 76 | paint.setStrokeWidth(1); |
| 77 | paint.setAntiAlias(true); |
| 78 | canvas.drawRect(r, paint); |
| 79 | // paint black with stroke rect (that asserts in bug 4406) |
| 80 | // over the filled rect, it doesnt cover it completelly with thinner stroke |
| 81 | paint.setStyle(SkPaint::kStroke_Style); |
| 82 | paint.setColor(0xff000000); |
| 83 | paint.setStrokeWidth(0.99f); |
| 84 | canvas.drawRect(r, paint); |
| 85 | REPORTER_ASSERT(reporter, has_green_pixels(bm)); |
| 86 | } |
| 87 | |
fmalita | 1a178ca | 2015-01-15 06:01:23 -0800 | [diff] [blame] | 88 | DEF_TEST(Rect, reporter) { |
| 89 | test_stroke_width_clipping(reporter); |
aleksandar.stojiljkovic | 7600183 | 2015-11-02 13:28:51 -0800 | [diff] [blame] | 90 | test_skbug4406(reporter); |
fmalita | 1a178ca | 2015-01-15 06:01:23 -0800 | [diff] [blame] | 91 | } |
Mike Reed | 185ffe9 | 2018-01-08 17:09:54 -0500 | [diff] [blame] | 92 | |
| 93 | DEF_TEST(Rect_grow, reporter) { |
| 94 | test_stroke_width_clipping(reporter); |
| 95 | test_skbug4406(reporter); |
| 96 | } |
Mike Reed | f581777 | 2018-01-09 15:36:51 -0500 | [diff] [blame] | 97 | |
| 98 | DEF_TEST(Rect_largest, reporter) { |
| 99 | REPORTER_ASSERT(reporter, !SkRectPriv::MakeILargest().isEmpty()); |
| 100 | REPORTER_ASSERT(reporter, SkRectPriv::MakeILargestInverted().isEmpty()); |
| 101 | |
| 102 | REPORTER_ASSERT(reporter, !SkRectPriv::MakeLargest().isEmpty()); |
| 103 | REPORTER_ASSERT(reporter, !SkRectPriv::MakeLargestS32().isEmpty()); |
| 104 | REPORTER_ASSERT(reporter, SkRectPriv::MakeLargestInverted().isEmpty()); |
| 105 | } |
| 106 | |