blob: 6a588b7b6cc7ff2af0233487a23e4436a2780adc [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"
10#include "SkRect.h"
Mike Reedf5817772018-01-09 15:36:51 -050011#include "SkRectPriv.h"
fmalita1a178ca2015-01-15 06:01:23 -080012#include "Test.h"
13
14static 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
26static 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.stojiljkovic76001832015-11-02 13:28:51 -080050static 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
fmalita1a178ca2015-01-15 06:01:23 -080088DEF_TEST(Rect, reporter) {
89 test_stroke_width_clipping(reporter);
aleksandar.stojiljkovic76001832015-11-02 13:28:51 -080090 test_skbug4406(reporter);
fmalita1a178ca2015-01-15 06:01:23 -080091}
Mike Reed185ffe92018-01-08 17:09:54 -050092
93DEF_TEST(Rect_grow, reporter) {
94 test_stroke_width_clipping(reporter);
95 test_skbug4406(reporter);
96}
Mike Reedf5817772018-01-09 15:36:51 -050097
98DEF_TEST(Rect_largest, reporter) {
Mike Reed8008df12018-01-17 12:20:04 -050099 REPORTER_ASSERT(reporter, !SkRectPriv::MakeILarge().isEmpty());
Mike Reedf5817772018-01-09 15:36:51 -0500100 REPORTER_ASSERT(reporter, SkRectPriv::MakeILargestInverted().isEmpty());
101
102 REPORTER_ASSERT(reporter, !SkRectPriv::MakeLargest().isEmpty());
Mike Reed8008df12018-01-17 12:20:04 -0500103 REPORTER_ASSERT(reporter, !SkRectPriv::MakeLargeS32().isEmpty());
Mike Reedf5817772018-01-09 15:36:51 -0500104 REPORTER_ASSERT(reporter, SkRectPriv::MakeLargestInverted().isEmpty());
105}
106