blob: d34214d6d3b465bf3edde7abeec3445a0d5e79ec [file] [log] [blame]
fmalita1a178ca2015-01-15 06:01:23 -08001/*
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 Reed926e1932018-01-29 15:56:11 -050010#include "SkPath.h"
fmalita1a178ca2015-01-15 06:01:23 -080011#include "SkRect.h"
Mike Reedf5817772018-01-09 15:36:51 -050012#include "SkRectPriv.h"
fmalita1a178ca2015-01-15 06:01:23 -080013#include "Test.h"
14
15static 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
27static 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.stojiljkovic76001832015-11-02 13:28:51 -080051static 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
fmalita1a178ca2015-01-15 06:01:23 -080089DEF_TEST(Rect, reporter) {
90 test_stroke_width_clipping(reporter);
aleksandar.stojiljkovic76001832015-11-02 13:28:51 -080091 test_skbug4406(reporter);
fmalita1a178ca2015-01-15 06:01:23 -080092}
Mike Reed185ffe92018-01-08 17:09:54 -050093
94DEF_TEST(Rect_grow, reporter) {
95 test_stroke_width_clipping(reporter);
96 test_skbug4406(reporter);
97}
Mike Reedf5817772018-01-09 15:36:51 -050098
Mike Reed926e1932018-01-29 15:56:11 -050099DEF_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 Reedf5817772018-01-09 15:36:51 -0500107DEF_TEST(Rect_largest, reporter) {
Mike Reed8008df12018-01-17 12:20:04 -0500108 REPORTER_ASSERT(reporter, !SkRectPriv::MakeILarge().isEmpty());
Mike Reedf5817772018-01-09 15:36:51 -0500109 REPORTER_ASSERT(reporter, SkRectPriv::MakeILargestInverted().isEmpty());
110
111 REPORTER_ASSERT(reporter, !SkRectPriv::MakeLargest().isEmpty());
Mike Reed8008df12018-01-17 12:20:04 -0500112 REPORTER_ASSERT(reporter, !SkRectPriv::MakeLargeS32().isEmpty());
Mike Reedf5817772018-01-09 15:36:51 -0500113 REPORTER_ASSERT(reporter, SkRectPriv::MakeLargestInverted().isEmpty());
114}
115