Home | History | Annotate | Download | only in gm
      1 /*
      2  * Copyright 2011 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 "gm.h"
      9 #include "SkCanvas.h"
     10 #include "SkPath.h"
     11 
     12 #include "SkDashPathEffect.h"
     13 static void test_giant_dash(SkCanvas* canvas) {
     14     SkPaint paint;
     15     const SkScalar intervals[] = { SK_Scalar1, SK_Scalar1 };
     16 
     17     paint.setStrokeWidth(2);
     18     paint.setPathEffect(new SkDashPathEffect(intervals, 2, 0))->unref();
     19 
     20     SkScalar big = 500 * 1000;
     21 
     22     canvas->drawLine(10, 10, big, 10, paint);
     23     canvas->drawLine(-big, 20, 500, 20, paint);
     24     canvas->drawLine(-big, 30, big, 30, paint);
     25 
     26     const SkScalar intervals2[] = { 20, 5, 10, 5 };
     27     paint.setPathEffect(new SkDashPathEffect(intervals2, 4, 17))->unref();
     28 
     29     canvas->translate(0, 40);
     30     SkScalar x = -500;
     31     SkScalar width = 3173;
     32     for (int i = 0; i < 40; ++i) {
     33         if (i > 10)
     34         canvas->drawLine(x, 0, x + width, 0, paint);
     35         x += 1;
     36         canvas->translate(0, 4);
     37     }
     38 }
     39 
     40 // Reproduces bug found here: http://jsfiddle.net/R8Cu5/1/
     41 //
     42 #include "SkGradientShader.h"
     43 static void test_grad(SkCanvas* canvas) {
     44     SkPoint pts[] = {
     45         { 478.544067f, -84.2041016f },
     46         { 602.455933f, 625.204102f },
     47     };
     48     SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, SK_ColorRED, SK_ColorRED };
     49     SkScalar pos[] = { 0, 0.3f, 0.3f, 1.0f };
     50     SkShader* s = SkGradientShader::CreateLinear(pts, colors, pos, 4, SkShader::kClamp_TileMode);
     51     SkPaint p;
     52     p.setShader(s)->unref();
     53     canvas->drawPaint(p);
     54 }
     55 
     56 static SkCanvas* MakeCanvas(const SkIRect& bounds) {
     57     SkBitmap bm;
     58     bm.setConfig(SkBitmap::kARGB_8888_Config, bounds.width(), bounds.height());
     59     bm.allocPixels();
     60     bm.eraseColor(SK_ColorTRANSPARENT);
     61 
     62     SkCanvas* canvas = new SkCanvas(bm);
     63     canvas->translate(-SkIntToScalar(bounds.fLeft), -SkIntToScalar(bounds.fTop));
     64     return canvas;
     65 }
     66 
     67 #ifdef SK_DEBUG
     68 static void GetBitmap(const SkCanvas* canvas, SkBitmap* bm) {
     69     *bm = canvas->getDevice()->accessBitmap(false);
     70 }
     71 #endif
     72 
     73 static void compare_canvas(const SkCanvas* a, const SkCanvas* b) {
     74 #ifdef SK_DEBUG
     75     SkBitmap bma, bmb;
     76     GetBitmap(a, &bma);
     77     GetBitmap(b, &bmb);
     78 
     79     SkASSERT(bma.width() == bmb.width());
     80     SkASSERT(bma.height() == bmb.height());
     81 
     82     bma.lockPixels();
     83     bmb.lockPixels();
     84     for (int y = 0; y < bma.height(); ++y) {
     85         const SkPMColor* rowa = bma.getAddr32(0, y);
     86         const SkPMColor* rowb = bmb.getAddr32(0, y);
     87         SkASSERT(!memcmp(rowa, rowb, bma.width() << 2));
     88 
     89         for (int x = 1; x < bma.width() - 1; ++x) {
     90             SkASSERT(0xFF000000 == rowa[x]);
     91             SkASSERT(0xFF000000 == rowb[x]);
     92         }
     93     }
     94 #endif
     95 }
     96 
     97 static void drawRectAsPath(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
     98     SkPath path;
     99     path.addRect(r);
    100     canvas->drawPath(path, p);
    101 }
    102 
    103 static void test_maskFromPath(const SkPath& path) {
    104     SkIRect bounds;
    105     path.getBounds().roundOut(&bounds);
    106 
    107     SkPaint paint;
    108     paint.setAntiAlias(true);
    109 
    110     SkAutoTUnref<SkCanvas> path_canvas(MakeCanvas(bounds));
    111     path_canvas->drawPath(path, paint);
    112 
    113     SkAutoTUnref<SkCanvas> rect_canvas(MakeCanvas(bounds));
    114     drawRectAsPath(rect_canvas, path.getBounds(), paint);
    115 
    116     compare_canvas(path_canvas, rect_canvas);
    117 }
    118 
    119 static void test_mask() {
    120     for (int i = 1; i <= 20; ++i) {
    121         const SkScalar dx = SK_Scalar1 / i;
    122         const SkRect constr = SkRect::MakeWH(dx, SkIntToScalar(2));
    123         for (int n = 2; n < 20; ++n) {
    124             SkPath path;
    125             path.setFillType(SkPath::kEvenOdd_FillType);
    126             SkRect r = constr;
    127             while (r.fRight < SkIntToScalar(4)) {
    128                 path.addRect(r);
    129                 r.offset(dx, 0);
    130             }
    131             test_maskFromPath(path);
    132         }
    133     }
    134 }
    135 
    136 namespace skiagm {
    137 
    138 /** Draw a 2px border around the target, then red behind the target;
    139     set the clip to match the target, then draw >> the target in blue.
    140 */
    141 
    142 static void draw (SkCanvas* canvas, SkRect& target, int x, int y) {
    143     SkPaint borderPaint;
    144     borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
    145     borderPaint.setAntiAlias(true);
    146     SkPaint backgroundPaint;
    147     backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
    148     backgroundPaint.setAntiAlias(true);
    149     SkPaint foregroundPaint;
    150     foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
    151     foregroundPaint.setAntiAlias(true);
    152 
    153     canvas->save();
    154     canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
    155     target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
    156     canvas->drawRect(target, borderPaint);
    157     target.inset(SkIntToScalar(2), SkIntToScalar(2));
    158     canvas->drawRect(target, backgroundPaint);
    159     canvas->clipRect(target, SkRegion::kIntersect_Op, true);
    160     target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
    161     canvas->drawRect(target, foregroundPaint);
    162     canvas->restore();
    163 }
    164 
    165 static void draw_square (SkCanvas* canvas, int x, int y) {
    166     SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1));
    167     draw(canvas, target, x, y);
    168 }
    169 
    170 static void draw_column (SkCanvas* canvas, int x, int y) {
    171     SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1));
    172     draw(canvas, target, x, y);
    173 }
    174 
    175 static void draw_bar (SkCanvas* canvas, int x, int y) {
    176     SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1));
    177     draw(canvas, target, x, y);
    178 }
    179 
    180 static void draw_rect_tests (SkCanvas* canvas) {
    181     draw_square(canvas, 10, 10);
    182     draw_column(canvas, 30, 10);
    183     draw_bar(canvas, 10, 30);
    184 }
    185 
    186 /**
    187    Test a set of clipping problems discovered while writing blitAntiRect,
    188    and test all the code paths through the clipping blitters.
    189    Each region should show as a blue center surrounded by a 2px green
    190    border, with no red.
    191 */
    192 
    193 class AAClipGM : public GM {
    194 public:
    195     AAClipGM() {
    196 
    197     }
    198 
    199 protected:
    200     virtual SkString onShortName() {
    201         return SkString("aaclip");
    202     }
    203 
    204     virtual SkISize onISize() {
    205         return make_isize(640, 480);
    206     }
    207 
    208     virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
    209         if (false) {
    210             test_giant_dash(canvas); return;
    211         }
    212         if (false) {
    213             test_grad(canvas); return;
    214         }
    215         if (false) { // avoid bit rot, suppress warning
    216             test_mask();
    217         }
    218 
    219         // Initial pixel-boundary-aligned draw
    220         draw_rect_tests(canvas);
    221 
    222         // Repeat 4x with .2, .4, .6, .8 px offsets
    223         canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
    224         canvas->translate(SkIntToScalar(50), 0);
    225         draw_rect_tests(canvas);
    226 
    227         canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
    228         canvas->translate(SkIntToScalar(50), 0);
    229         draw_rect_tests(canvas);
    230 
    231         canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
    232         canvas->translate(SkIntToScalar(50), 0);
    233         draw_rect_tests(canvas);
    234 
    235         canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
    236         canvas->translate(SkIntToScalar(50), 0);
    237         draw_rect_tests(canvas);
    238     }
    239 
    240     virtual uint32_t onGetFlags() const { return kSkipPipe_Flag; }
    241 
    242 private:
    243     typedef GM INHERITED;
    244 };
    245 
    246 //////////////////////////////////////////////////////////////////////////////
    247 
    248 static GM* MyFactory(void*) { return new AAClipGM; }
    249 static GMRegistry reg(MyFactory);
    250 
    251 }
    252