1591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org/*
2591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org * Copyright 2013 Google Inc.
3591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org *
4591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org * Use of this source code is governed by a BSD-style license that can be
5591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org * found in the LICENSE file.
6591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org */
7591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
8591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org#include "gm.h"
9591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org#include "SkCanvas.h"
10591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org#include "SkPath.h"
11591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
12591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.orgnamespace skiagm {
13591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
14591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org// This GM tests a grab-bag of non-closed paths. All these paths look like
15591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org// closed rects, but they don't call path.close(). Depending on the stroke
16591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org// settings these slightly different paths give widely different results.
17591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.orgclass NonClosedPathsGM: public GM {
18591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.orgpublic:
19591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org    NonClosedPathsGM() {}
20591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
21591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org    enum ClosureType {
22591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        TotallyNonClosed,  // The last point doesn't coincide with the first one in the contour.
23591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org                           // The path looks not closed at all.
24591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
25591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        FakeCloseCorner,   // The last point coincides with the first one at a corner.
26591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org                           // The path looks closed, but final rendering has 2 ends with cap.
27591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
28591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        FakeCloseMiddle,   // The last point coincides with the first one in the middle of a line.
29591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org                           // The path looks closed, and the final rendering looks closed too.
30591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
31591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        kClosureTypeCount
32591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org    };
33591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
34591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.orgprotected:
35a90c6803865766d28e92091f56f718f5e41fe80fcommit-bot@chromium.org
3636352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    SkString onShortName() override {
37591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        return SkString("nonclosedpaths");
38591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org    }
39591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
40591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org    // 12 * 18 + 3 cases, every case is 100 * 100 pixels.
4136352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    SkISize onISize() override {
42591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        return SkISize::Make(1220, 1920);
43591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org    }
44591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
45591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org    // Use rect-like geometry for non-closed path, for right angles make it
46591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org    // easier to show the visual difference of lineCap and lineJoin.
47591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org    static void MakePath(SkPath* path, ClosureType type) {
48591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        if (FakeCloseMiddle == type) {
49591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org            path->moveTo(30, 50);
50591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org            path->lineTo(30, 30);
51591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        } else {
52591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org            path->moveTo(30, 30);
53591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        }
54591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        path->lineTo(70, 30);
55591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        path->lineTo(70, 70);
56591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        path->lineTo(30, 70);
57591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        path->lineTo(30, 50);
58591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        if (FakeCloseCorner == type) {
59591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org            path->lineTo(30, 30);
60591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        }
61591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org    }
62591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
63591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org    // Set the location for the current test on the canvas
64591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org    static void SetLocation(SkCanvas* canvas, int counter, int lineNum) {
65591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        SkScalar x = SK_Scalar1 * 100 * (counter % lineNum) + 10 + SK_Scalar1 / 4;
66591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        SkScalar y = SK_Scalar1 * 100 * (counter / lineNum) + 10 + 3 * SK_Scalar1 / 4;
67591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        canvas->translate(x, y);
68591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org    }
69591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
7036352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    void onDraw(SkCanvas* canvas) override {
71591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        // Stroke widths are:
72591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        // 0(may use hairline rendering), 10(common case for stroke-style)
73591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        // 40 and 50(>= geometry width/height, make the contour filled in fact)
74591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        static const int kStrokeWidth[] = {0, 10, 40, 50};
757fa2a65c0cfc714364490cb715171461143024e0reed@google.com        int numWidths = SK_ARRAY_COUNT(kStrokeWidth);
76591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
77591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        static const SkPaint::Style kStyle[] = {
78591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org            SkPaint::kStroke_Style, SkPaint::kStrokeAndFill_Style
79591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        };
80591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
81591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        static const SkPaint::Cap kCap[] = {
82591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org            SkPaint::kButt_Cap, SkPaint::kRound_Cap, SkPaint::kSquare_Cap
83591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        };
84591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
85591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        static const SkPaint::Join kJoin[] = {
86591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org            SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
87591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        };
88591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
89591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        static const ClosureType kType[] = {
90591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org            TotallyNonClosed, FakeCloseCorner, FakeCloseMiddle
91591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        };
92591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
93591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        int counter = 0;
94591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        SkPaint paint;
95591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        paint.setAntiAlias(true);
96591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
97591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        // For stroke style painter and fill-and-stroke style painter
98591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        for (size_t type = 0; type < kClosureTypeCount; ++type) {
99591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org            for (size_t style = 0; style < SK_ARRAY_COUNT(kStyle); ++style) {
100591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org                for (size_t cap = 0; cap < SK_ARRAY_COUNT(kCap); ++cap) {
101591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org                    for (size_t join = 0; join < SK_ARRAY_COUNT(kJoin); ++join) {
1027fa2a65c0cfc714364490cb715171461143024e0reed@google.com                        for (int width = 0; width < numWidths; ++width) {
103591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org                            canvas->save();
104591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org                            SetLocation(canvas, counter, SkPaint::kJoinCount * numWidths);
105591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
106591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org                            SkPath path;
107591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org                            MakePath(&path, kType[type]);
108591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
109591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org                            paint.setStyle(kStyle[style]);
110591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org                            paint.setStrokeCap(kCap[cap]);
111591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org                            paint.setStrokeJoin(kJoin[join]);
112591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org                            paint.setStrokeWidth(SkIntToScalar(kStrokeWidth[width]));
113591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
114591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org                            canvas->drawPath(path, paint);
115591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org                            canvas->restore();
116591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org                            ++counter;
117591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org                        }
118591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org                    }
119591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org                }
120591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org            }
121591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        }
122591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
123591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        // For fill style painter
124591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        paint.setStyle(SkPaint::kFill_Style);
125591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        for (size_t type = 0; type < kClosureTypeCount; ++type) {
126591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org            canvas->save();
127591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org            SetLocation(canvas, counter, SkPaint::kJoinCount * numWidths);
128591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
129591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org            SkPath path;
130591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org            MakePath(&path, kType[type]);
131591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
132591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org            canvas->drawPath(path, paint);
133591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org            canvas->restore();
134591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org            ++counter;
135591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org        }
136591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org    }
137591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
138591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.orgprivate:
139591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org    typedef GM INHERITED;
140591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org};
141591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
142591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org//////////////////////////////////////////////////////////////////////////////
143591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
144591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.orgDEF_GM(return new NonClosedPathsGM;)
145591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org
146591670558768f6fe97ccf74b6424c6c90f73fdc8commit-bot@chromium.org}
147