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 "SkPath.h" 11 #include "SkRect.h" 12 #include "SkRectPriv.h" 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 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 89 DEF_TEST(Rect, reporter) { 90 test_stroke_width_clipping(reporter); 91 test_skbug4406(reporter); 92 } 93 94 DEF_TEST(Rect_grow, reporter) { 95 test_stroke_width_clipping(reporter); 96 test_skbug4406(reporter); 97 } 98 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 107 DEF_TEST(Rect_largest, reporter) { 108 REPORTER_ASSERT(reporter, !SkRectPriv::MakeILarge().isEmpty()); 109 REPORTER_ASSERT(reporter, SkRectPriv::MakeILargestInverted().isEmpty()); 110 111 REPORTER_ASSERT(reporter, !SkRectPriv::MakeLargest().isEmpty()); 112 REPORTER_ASSERT(reporter, !SkRectPriv::MakeLargeS32().isEmpty()); 113 REPORTER_ASSERT(reporter, SkRectPriv::MakeLargestInverted().isEmpty()); 114 } 115 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 } 142 143 static float make_big_value(skiatest::Reporter* reporter) { 144 // need to make a big value, one that will cause rect.width() to overflow to inf. 145 // however, the windows compiler wants about this if it can see the big value inlined. 146 // hence, this stupid trick to try to fool their compiler. 147 SkASSERT(reporter); 148 return reporter ? SK_ScalarMax * 0.75f : 0; 149 } 150 151 DEF_TEST(Rect_center, reporter) { 152 // ensure we can compute center even when the width/height might overflow 153 const SkScalar big = make_big_value(reporter); 154 const SkRect r = { -big, -big, big, big }; 155 156 REPORTER_ASSERT(reporter, r.isFinite()); 157 REPORTER_ASSERT(reporter, SkScalarIsFinite(r.centerX())); 158 REPORTER_ASSERT(reporter, SkScalarIsFinite(r.centerY())); 159 REPORTER_ASSERT(reporter, !SkScalarIsFinite(r.width())); 160 REPORTER_ASSERT(reporter, !SkScalarIsFinite(r.height())); 161 } 162 163 #include "SkSurface.h" 164 165 // Before the fix, this sequence would trigger a release_assert in the Tiler 166 // in SkBitmapDevice.cpp 167 DEF_TEST(big_tiled_rect_crbug_927075, reporter) { 168 // since part of the regression test allocates a huge buffer, don't bother trying on 169 // 32-bit devices (e.g. chromecast) so we avoid them failing to allocated. 170 171 if (sizeof(void*) == 8) { 172 const int w = 67108863; 173 const int h = 1; 174 const auto info = SkImageInfo::MakeN32Premul(w, h); 175 176 auto surf = SkSurface::MakeRaster(info); 177 auto canvas = surf->getCanvas(); 178 179 const SkRect r = { 257, 213, 67109120, 214 }; 180 SkPaint paint; 181 paint.setAntiAlias(true); 182 183 canvas->translate(-r.fLeft, -r.fTop); 184 canvas->drawRect(r, paint); 185 } 186 } 187