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 "Test.h"
9#include "SkPath.h"
10#include "SkCanvas.h"
11
12static void appendStr(SkString* str, const SkPaint& paint) {
13    str->appendf(" style[%d] cap[%d] join[%d] antialias[%d]",
14                 paint.getStyle(), paint.getStrokeCap(),
15                 paint.getStrokeJoin(), paint.isAntiAlias());
16}
17
18static void appendStr(SkString* str, const SkPath& path) {
19    str->appendf(" filltype[%d] ptcount[%d]",
20                 path.getFillType(), path.countPoints());
21}
22
23#define DIMENSION   32
24
25static void drawAndTest(skiatest::Reporter* reporter, const SkPath& path,
26                        const SkPaint& paint, bool shouldDraw) {
27    SkBitmap bm;
28    // explicitly specify a trim rowbytes, so we have no padding on each row
29    bm.setConfig(SkBitmap::kARGB_8888_Config, DIMENSION, DIMENSION, DIMENSION*4);
30    bm.allocPixels();
31    bm.eraseColor(SK_ColorTRANSPARENT);
32
33    SkCanvas canvas(bm);
34    SkPaint p(paint);
35    p.setColor(SK_ColorWHITE);
36
37    canvas.drawPath(path, p);
38
39    size_t count = DIMENSION * DIMENSION;
40    const SkPMColor* ptr = bm.getAddr32(0, 0);
41
42    SkPMColor andValue = ~0U;
43    SkPMColor orValue = 0;
44    for (size_t i = 0; i < count; ++i) {
45        SkPMColor c = ptr[i];
46        andValue &= c;
47        orValue |= c;
48    }
49
50    // success means we drew everywhere or nowhere (depending on shouldDraw)
51    bool success = shouldDraw ? (~0U == andValue) : (0 == orValue);
52
53    if (!success) {
54        SkString str;
55        if (shouldDraw) {
56            str.set("Path expected to draw everywhere, but didn't. ");
57        } else {
58            str.set("Path expected to draw nowhere, but did. ");
59        }
60        appendStr(&str, paint);
61        appendStr(&str, path);
62        reporter->reportFailed(str);
63
64// uncomment this if you want to step in to see the failure
65//        canvas.drawPath(path, p);
66    }
67}
68
69static void iter_paint(skiatest::Reporter* reporter, const SkPath& path, bool shouldDraw) {
70    static const SkPaint::Cap gCaps[] = {
71        SkPaint::kButt_Cap,
72        SkPaint::kRound_Cap,
73        SkPaint::kSquare_Cap
74    };
75    static const SkPaint::Join gJoins[] = {
76        SkPaint::kMiter_Join,
77        SkPaint::kRound_Join,
78        SkPaint::kBevel_Join
79    };
80    static const SkPaint::Style gStyles[] = {
81        SkPaint::kFill_Style,
82        SkPaint::kStroke_Style,
83        SkPaint::kStrokeAndFill_Style
84    };
85    for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
86        for (size_t join = 0; join < SK_ARRAY_COUNT(gJoins); ++join) {
87            for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
88                SkPaint paint;
89                paint.setStrokeWidth(SkIntToScalar(10));
90
91                paint.setStrokeCap(gCaps[cap]);
92                paint.setStrokeJoin(gJoins[join]);
93                paint.setStyle(gStyles[style]);
94
95                paint.setAntiAlias(false);
96                drawAndTest(reporter, path, paint, shouldDraw);
97                paint.setAntiAlias(true);
98                drawAndTest(reporter, path, paint, shouldDraw);
99            }
100        }
101    }
102}
103
104#define CX  (SkIntToScalar(DIMENSION) / 2)
105#define CY  (SkIntToScalar(DIMENSION) / 2)
106
107static void make_empty(SkPath*) {}
108static void make_M(SkPath* path) { path->moveTo(CX, CY); }
109static void make_MM(SkPath* path) { path->moveTo(CX, CY); path->moveTo(CX, CY); }
110static void make_MZM(SkPath* path) { path->moveTo(CX, CY); path->close(); path->moveTo(CX, CY); }
111static void make_L(SkPath* path) { path->moveTo(CX, CY); path->lineTo(CX, CY); }
112static void make_Q(SkPath* path) { path->moveTo(CX, CY); path->quadTo(CX, CY, CX, CY); }
113static void make_C(SkPath* path) { path->moveTo(CX, CY); path->cubicTo(CX, CY, CX, CY, CX, CY); }
114
115/*  Two invariants are tested: How does an empty/degenerate path draw?
116 *  - if the path is drawn inverse, it should draw everywhere
117 *  - if the path is drawn non-inverse, it should draw nowhere
118 *
119 *  Things to iterate on:
120 *  - path (empty, degenerate line/quad/cubic w/ and w/o close
121 *  - paint style
122 *  - path filltype
123 *  - path stroke variants (e.g. caps, joins, width)
124 */
125static void test_emptydrawing(skiatest::Reporter* reporter) {
126    static void (*gMakeProc[])(SkPath*) = {
127        make_empty, make_M, make_MM, make_MZM, make_L, make_Q, make_C
128    };
129    static SkPath::FillType gFills[] = {
130        SkPath::kWinding_FillType,
131        SkPath::kEvenOdd_FillType,
132        SkPath::kInverseWinding_FillType,
133        SkPath::kInverseEvenOdd_FillType
134    };
135    for (int doClose = 0; doClose < 2; ++doClose) {
136        for  (size_t i = 0; i < SK_ARRAY_COUNT(gMakeProc); ++i) {
137            SkPath path;
138            gMakeProc[i](&path);
139            if (doClose) {
140                path.close();
141            }
142            for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
143                path.setFillType(gFills[fill]);
144                bool shouldDraw = path.isInverseFillType();
145                iter_paint(reporter, path, shouldDraw);
146            }
147        }
148    }
149}
150
151static void TestEmptyPath(skiatest::Reporter* reporter) {
152    test_emptydrawing(reporter);
153}
154
155#include "TestClassDef.h"
156DEFINE_TESTCLASS("EmptyPath", TestEmptyPathClass, TestEmptyPath)
157