Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright 2012 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 "SkColor.h"
     11 #include "SkDashPathEffect.h"
     12 #include "SkImageInfo.h"
     13 #include "SkMatrix.h"
     14 #include "SkPaint.h"
     15 #include "SkPath.h"
     16 #include "SkPathEffect.h"
     17 #include "SkPoint.h"
     18 #include "SkRRect.h"
     19 #include "SkRect.h"
     20 #include "SkRefCnt.h"
     21 #include "SkScalar.h"
     22 #include "SkStrokeRec.h"
     23 #include "SkSurface.h"
     24 #include "SkTypes.h"
     25 #include "Test.h"
     26 
     27 // test that we can draw an aa-rect at coordinates > 32K (bigger than fixedpoint)
     28 static void test_big_aa_rect(skiatest::Reporter* reporter) {
     29     SkBitmap output;
     30     SkPMColor pixel[1];
     31     output.installPixels(SkImageInfo::MakeN32Premul(1, 1), pixel, 4);
     32 
     33     auto surf = SkSurface::MakeRasterN32Premul(300, 33300);
     34     SkCanvas* canvas = surf->getCanvas();
     35 
     36     SkRect r = { 0, 33000, 300, 33300 };
     37     int x = SkScalarRoundToInt(r.left());
     38     int y = SkScalarRoundToInt(r.top());
     39 
     40     // check that the pixel in question starts as transparent (by the surface)
     41     if (surf->readPixels(output, x, y)) {
     42         REPORTER_ASSERT(reporter, 0 == pixel[0]);
     43     } else {
     44         REPORTER_ASSERT(reporter, false, "readPixels failed");
     45     }
     46 
     47     SkPaint paint;
     48     paint.setAntiAlias(true);
     49     paint.setColor(SK_ColorWHITE);
     50 
     51     canvas->drawRect(r, paint);
     52 
     53     // Now check that it is BLACK
     54     if (surf->readPixels(output, x, y)) {
     55         // don't know what swizzling PMColor did, but white should always
     56         // appear the same.
     57         REPORTER_ASSERT(reporter, 0xFFFFFFFF == pixel[0]);
     58     } else {
     59         REPORTER_ASSERT(reporter, false, "readPixels failed");
     60     }
     61 }
     62 
     63 ///////////////////////////////////////////////////////////////////////////////
     64 
     65 static void moveToH(SkPath* path, const uint32_t raw[]) {
     66     const float* fptr = (const float*)raw;
     67     path->moveTo(fptr[0], fptr[1]);
     68 }
     69 
     70 static void cubicToH(SkPath* path, const uint32_t raw[]) {
     71     const float* fptr = (const float*)raw;
     72     path->cubicTo(fptr[0], fptr[1], fptr[2], fptr[3], fptr[4], fptr[5]);
     73 }
     74 
     75 // This used to assert, because we performed a cast (int)(pt[0].fX * scale) to
     76 // arrive at an int (SkFDot6) rather than calling sk_float_round2int. The assert
     77 // was that the initial line-segment produced by the cubic was not monotonically
     78 // going down (i.e. the initial DY was negative). By rounding the floats, we get
     79 // the more proper result.
     80 //
     81 // http://code.google.com/p/chromium/issues/detail?id=131181
     82 //
     83 
     84 // we're not calling this test anymore; is that for a reason?
     85 
     86 static void test_crbug131181() {
     87     /*
     88      fX = 18.8943768,
     89      fY = 129.121277
     90      }, {
     91      fX = 18.8937435,
     92      fY = 129.121689
     93      }, {
     94      fX = 18.8950119,
     95      fY = 129.120422
     96      }, {
     97      fX = 18.5030727,
     98      fY = 129.13121
     99      */
    100     uint32_t data[] = {
    101         0x419727af, 0x43011f0c, 0x41972663, 0x43011f27,
    102         0x419728fc, 0x43011ed4, 0x4194064b, 0x43012197
    103     };
    104 
    105     SkPath path;
    106     moveToH(&path, &data[0]);
    107     cubicToH(&path, &data[2]);
    108 
    109     auto surface(SkSurface::MakeRasterN32Premul(640, 480));
    110 
    111     SkPaint paint;
    112     paint.setAntiAlias(true);
    113     surface->getCanvas()->drawPath(path, paint);
    114 }
    115 
    116 // This used to assert in debug builds (and crash writing bad memory in release)
    117 // because we overflowed an intermediate value (B coefficient) setting up our
    118 // stepper for the quadratic. Now we bias that value by 1/2 so we don't overflow
    119 static void test_crbug_140803() {
    120     SkBitmap bm;
    121     bm.allocN32Pixels(2700, 30*1024);
    122     SkCanvas canvas(bm);
    123 
    124     SkPaint paint;
    125     paint.setAntiAlias(true);
    126     canvas.drawPath(SkPath().moveTo(2762, 20).quadTo(11, 21702, 10, 21706), paint);
    127 }
    128 
    129 // Need to exercise drawing an inverse-path whose bounds intersect the clip,
    130 // but whose edges do not (since its a quad which draws only in the bottom half
    131 // of its bounds).
    132 // In the debug build, we used to assert in this case, until it was fixed.
    133 //
    134 static void test_inversepathwithclip() {
    135     SkPath path;
    136 
    137     path.moveTo(0, 20);
    138     path.quadTo(10, 10, 20, 20);
    139     path.toggleInverseFillType();
    140 
    141     SkPaint paint;
    142 
    143     auto surface(SkSurface::MakeRasterN32Premul(640, 480));
    144     SkCanvas* canvas = surface->getCanvas();
    145     canvas->save();
    146     canvas->clipRect(SkRect::MakeWH(19, 11));
    147 
    148     paint.setAntiAlias(false);
    149     canvas->drawPath(path, paint);
    150     paint.setAntiAlias(true);
    151     canvas->drawPath(path, paint);
    152 
    153     canvas->restore();
    154 
    155     // Now do the test again, with the path flipped, so we only draw in the
    156     // top half of our bounds, and have the clip intersect our bounds at the
    157     // bottom.
    158     path.reset();   // preserves our filltype
    159     path.moveTo(0, 10);
    160     path.quadTo(10, 20, 20, 10);
    161     canvas->clipRect(SkRect::MakeXYWH(0, 19, 19, 11));
    162 
    163     paint.setAntiAlias(false);
    164     canvas->drawPath(path, paint);
    165     paint.setAntiAlias(true);
    166     canvas->drawPath(path, paint);
    167 }
    168 
    169 static void test_bug533() {
    170     /*
    171         http://code.google.com/p/skia/issues/detail?id=533
    172         This particular test/bug only applies to the float case, where the
    173         coordinates are very large.
    174      */
    175     SkPath path;
    176     path.moveTo(64, 3);
    177     path.quadTo(-329936, -100000000, 1153, 330003);
    178 
    179     SkPaint paint;
    180     paint.setAntiAlias(true);
    181 
    182     auto surface(SkSurface::MakeRasterN32Premul(640, 480));
    183     surface->getCanvas()->drawPath(path, paint);
    184 }
    185 
    186 static void test_crbug_140642() {
    187     /*
    188      *  We used to see this construct, and due to rounding as we accumulated
    189      *  our length, the loop where we apply the phase would run off the end of
    190      *  the array, since it relied on just -= each interval value, which did not
    191      *  behave as "expected". Now the code explicitly checks for walking off the
    192      *  end of that array.
    193 
    194      *  A different (better) fix might be to rewrite dashing to do all of its
    195      *  length/phase/measure math using double, but this may need to be
    196      *  coordinated with SkPathMeasure, to be consistent between the two.
    197 
    198      <path stroke="mintcream" stroke-dasharray="27734 35660 2157846850 247"
    199            stroke-dashoffset="-248.135982067">
    200      */
    201 
    202     const SkScalar vals[] = { 27734, 35660, 2157846850.0f, 247 };
    203     auto dontAssert = SkDashPathEffect::Make(vals, 4, -248.135982067f);
    204 }
    205 
    206 static void test_crbug_124652() {
    207     /*
    208         http://code.google.com/p/chromium/issues/detail?id=124652
    209         This particular test/bug only applies to the float case, where
    210         large values can "swamp" small ones.
    211      */
    212     SkScalar intervals[2] = {837099584, 33450};
    213     auto dontAssert = SkDashPathEffect::Make(intervals, 2, -10);
    214 }
    215 
    216 static void test_bigcubic() {
    217     SkPath path;
    218     path.moveTo(64, 3);
    219     path.cubicTo(-329936, -100000000, -329936, 100000000, 1153, 330003);
    220 
    221     SkPaint paint;
    222     paint.setAntiAlias(true);
    223 
    224     auto surface(SkSurface::MakeRasterN32Premul(640, 480));
    225     surface->getCanvas()->drawPath(path, paint);
    226 }
    227 
    228 // asserts if halfway case is not handled
    229 static void test_halfway() {
    230     SkPaint paint;
    231     SkPath path;
    232     path.moveTo(16365.5f, 1394);
    233     path.lineTo(16365.5f, 1387.5f);
    234     path.quadTo(16365.5f, 1385.43f, 16367, 1383.96f);
    235     path.quadTo(16368.4f, 1382.5f, 16370.5f, 1382.5f);
    236     path.lineTo(16465.5f, 1382.5f);
    237     path.quadTo(16467.6f, 1382.5f, 16469, 1383.96f);
    238     path.quadTo(16470.5f, 1385.43f, 16470.5f, 1387.5f);
    239     path.lineTo(16470.5f, 1394);
    240     path.quadTo(16470.5f, 1396.07f, 16469, 1397.54f);
    241     path.quadTo(16467.6f, 1399, 16465.5f, 1399);
    242     path.lineTo(16370.5f, 1399);
    243     path.quadTo(16368.4f, 1399, 16367, 1397.54f);
    244     path.quadTo(16365.5f, 1396.07f, 16365.5f, 1394);
    245     path.close();
    246     SkPath p2;
    247     SkMatrix m;
    248     m.reset();
    249     m.postTranslate(0.001f, 0.001f);
    250     path.transform(m, &p2);
    251 
    252     auto surface(SkSurface::MakeRasterN32Premul(640, 480));
    253     SkCanvas* canvas = surface->getCanvas();
    254     canvas->translate(-16366, -1383);
    255     canvas->drawPath(p2, paint);
    256 
    257     m.reset();
    258     m.postTranslate(-0.001f, -0.001f);
    259     path.transform(m, &p2);
    260     canvas->drawPath(p2, paint);
    261 
    262     m.reset();
    263     path.transform(m, &p2);
    264     canvas->drawPath(p2, paint);
    265 }
    266 
    267 // we used to assert if the bounds of the device (clip) was larger than 32K
    268 // even when the path itself was smaller. We just draw and hope in the debug
    269 // version to not assert.
    270 static void test_giantaa() {
    271     const int W = 400;
    272     const int H = 400;
    273     auto surface(SkSurface::MakeRasterN32Premul(33000, 10));
    274 
    275     SkPaint paint;
    276     paint.setAntiAlias(true);
    277     SkPath path;
    278     path.addOval(SkRect::MakeXYWH(-10, -10, 20 + W, 20 + H));
    279     surface->getCanvas()->drawPath(path, paint);
    280 }
    281 
    282 // Extremely large path_length/dash_length ratios may cause infinite looping
    283 // in SkDashPathEffect::filterPath() due to single precision rounding.
    284 // The test is quite expensive, but it should get much faster after the fix
    285 // for http://crbug.com/165432 goes in.
    286 static void test_infinite_dash(skiatest::Reporter* reporter) {
    287     SkPath path;
    288     path.moveTo(0, 0);
    289     path.lineTo(5000000, 0);
    290 
    291     SkScalar intervals[] = { 0.2f, 0.2f };
    292     sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, 2, 0));
    293 
    294     SkPath filteredPath;
    295     SkPaint paint;
    296     paint.setStyle(SkPaint::kStroke_Style);
    297     paint.setPathEffect(dash);
    298 
    299     paint.getFillPath(path, &filteredPath);
    300     // If we reach this, we passed.
    301     REPORTER_ASSERT(reporter, true);
    302 }
    303 
    304 // http://crbug.com/165432
    305 // Limit extreme dash path effects to avoid exhausting the system memory.
    306 static void test_crbug_165432(skiatest::Reporter* reporter) {
    307     SkPath path;
    308     path.moveTo(0, 0);
    309     path.lineTo(10000000, 0);
    310 
    311     SkScalar intervals[] = { 0.5f, 0.5f };
    312     sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, 2, 0));
    313 
    314     SkPaint paint;
    315     paint.setStyle(SkPaint::kStroke_Style);
    316     paint.setPathEffect(dash);
    317 
    318     SkPath filteredPath;
    319     SkStrokeRec rec(paint);
    320     REPORTER_ASSERT(reporter, !dash->filterPath(&filteredPath, path, &rec, nullptr));
    321     REPORTER_ASSERT(reporter, filteredPath.isEmpty());
    322 }
    323 
    324 // http://crbug.com/472147
    325 // This is a simplified version from the bug. RRect radii not properly scaled.
    326 static void test_crbug_472147_simple(skiatest::Reporter* reporter) {
    327     auto surface(SkSurface::MakeRasterN32Premul(1000, 1000));
    328     SkCanvas* canvas = surface->getCanvas();
    329     SkPaint p;
    330     SkRect r = SkRect::MakeLTRB(-246.0f, 33.0f, 848.0f, 33554464.0f);
    331     SkVector radii[4] = {
    332         { 13.0f, 8.0f }, { 170.0f, 2.0 }, { 256.0f, 33554430.0f }, { 120.0f, 5.0f }
    333     };
    334     SkRRect rr;
    335     rr.setRectRadii(r, radii);
    336     canvas->drawRRect(rr, p);
    337 }
    338 
    339 // http://crbug.com/472147
    340 // RRect radii not properly scaled.
    341 static void test_crbug_472147_actual(skiatest::Reporter* reporter) {
    342     auto surface(SkSurface::MakeRasterN32Premul(1000, 1000));
    343     SkCanvas* canvas = surface->getCanvas();
    344     SkPaint p;
    345     SkRect r = SkRect::MakeLTRB(-246.0f, 33.0f, 848.0f, 33554464.0f);
    346     SkVector radii[4] = {
    347         { 13.0f, 8.0f }, { 170.0f, 2.0 }, { 256.0f, 33554430.0f }, { 120.0f, 5.0f }
    348     };
    349     SkRRect rr;
    350     rr.setRectRadii(r, radii);
    351     canvas->clipRRect(rr);
    352 
    353     SkRect r2 = SkRect::MakeLTRB(0, 33, 1102, 33554464);
    354     canvas->drawRect(r2, p);
    355 }
    356 
    357 DEF_TEST(DrawPath, reporter) {
    358     test_giantaa();
    359     test_bug533();
    360     test_bigcubic();
    361     test_crbug_124652();
    362     test_crbug_140642();
    363     test_crbug_140803();
    364     test_inversepathwithclip();
    365     // why?
    366     if (false) test_crbug131181();
    367     test_infinite_dash(reporter);
    368     test_crbug_165432(reporter);
    369     test_crbug_472147_simple(reporter);
    370     test_crbug_472147_actual(reporter);
    371     test_big_aa_rect(reporter);
    372     test_halfway();
    373 }
    374