blob: 7be4423483c903d70e26339877eab15cc6d3ace7 [file] [log] [blame]
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +00001/*
2 * Copyright 2013 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 "SkTArray.h"
11
12namespace skiagm {
13
14class HairlinesGM : public GM {
15protected:
16
17 virtual SkString onShortName() SK_OVERRIDE {
18 return SkString("hairlines");
19 }
20
21 virtual SkISize onISize() { return make_isize(800, 600); }
22
23 virtual void onOnceBeforeDraw() SK_OVERRIDE {
24 {
25 SkPath* lineAnglesPath = &fPaths.push_back();
26 enum {
27 kNumAngles = 15,
28 kRadius = 40,
29 };
30 for (int i = 0; i < kNumAngles; ++i) {
31 SkScalar angle = SK_ScalarPI * SkIntToScalar(i) / kNumAngles;
32 SkScalar x = kRadius * SkScalarCos(angle);
33 SkScalar y = kRadius * SkScalarSin(angle);
34 lineAnglesPath->moveTo(x, y);
35 lineAnglesPath->lineTo(-x, -y);
36 }
37 }
38
39 {
40 SkPath* kindaTightQuad = &fPaths.push_back();
41 kindaTightQuad->moveTo(0, -10 * SK_Scalar1);
42 kindaTightQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), -10 * SK_Scalar1, 0);
43 }
44
45 {
46 SkPath* tightQuad = &fPaths.push_back();
47 tightQuad->moveTo(0, -5 * SK_Scalar1);
48 tightQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), -5 * SK_Scalar1, 0);
49 }
50
51 {
52 SkPath* tighterQuad = &fPaths.push_back();
53 tighterQuad->moveTo(0, -2 * SK_Scalar1);
54 tighterQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), -2 * SK_Scalar1, 0);
55 }
56
57 {
58 SkPath* unevenTighterQuad = &fPaths.push_back();
59 unevenTighterQuad->moveTo(0, -1 * SK_Scalar1);
60 SkPoint p;
61 p.set(-2 * SK_Scalar1 + 3 * SkIntToScalar(102) / 4, SkIntToScalar(75));
62 unevenTighterQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), p.fX, p.fY);
63 }
64
65 {
66 SkPath* reallyTightQuad = &fPaths.push_back();
67 reallyTightQuad->moveTo(0, -1 * SK_Scalar1);
68 reallyTightQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), -1 * SK_Scalar1, 0);
69 }
70
71 {
72 SkPath* closedQuad = &fPaths.push_back();
73 closedQuad->moveTo(0, -0);
74 closedQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), 0, 0);
75 }
76
77 {
78 SkPath* unevenClosedQuad = &fPaths.push_back();
79 unevenClosedQuad->moveTo(0, -0);
80 unevenClosedQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100),
81 SkIntToScalar(75), SkIntToScalar(75));
82 }
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +000083
84 // Two problem cases for gpu hairline renderer found by shapeops testing. These used
85 // to assert that the computed bounding box didn't contain all the vertices.
86 {
87 SkPath* problem1 = &fPaths.push_back();
88 problem1->moveTo(SkIntToScalar(4), SkIntToScalar(6));
89 problem1->cubicTo(SkIntToScalar(5), SkIntToScalar(6),
90 SkIntToScalar(5), SkIntToScalar(4),
91 SkIntToScalar(4), SkIntToScalar(0));
92 problem1->close();
93 }
94
95 {
96 SkPath* problem2 = &fPaths.push_back();
97 problem2->moveTo(SkIntToScalar(5), SkIntToScalar(1));
98 problem2->lineTo(SkFloatToScalar(4.32787323f), SkFloatToScalar(1.67212653f));
99 problem2->cubicTo(SkFloatToScalar(2.75223875f), SkFloatToScalar(3.24776125f),
100 SkFloatToScalar(3.00581908f), SkFloatToScalar(4.51236057f),
101 SkFloatToScalar(3.7580452f), SkFloatToScalar(4.37367964f));
102 problem2->cubicTo(SkFloatToScalar(4.66472578f), SkFloatToScalar(3.888381f),
103 SkFloatToScalar(5.f), SkFloatToScalar(2.875f),
104 SkFloatToScalar(5.f), SkFloatToScalar(1.f));
105 problem2->close();
106 }
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000107 }
108
109 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
110 static const SkAlpha kAlphaValue[] = { 0xFF, 0x40 };
111
112 enum {
113 kMargin = 5,
114 };
115 int wrapX = canvas->getDeviceSize().fWidth - kMargin;
116
117 SkScalar maxH = 0;
118 canvas->translate(SkIntToScalar(kMargin), SkIntToScalar(kMargin));
119 canvas->save();
120
121 SkScalar x = SkIntToScalar(kMargin);
122 for (int p = 0; p < fPaths.count(); ++p) {
123 for (size_t a = 0; a < SK_ARRAY_COUNT(kAlphaValue); ++a) {
124 for (int aa = 0; aa < 2; ++aa) {
125 const SkRect& bounds = fPaths[p].getBounds();
126
127 if (x + bounds.width() > wrapX) {
128 canvas->restore();
129 canvas->translate(0, maxH + SkIntToScalar(kMargin));
130 canvas->save();
131 maxH = 0;
132 x = SkIntToScalar(kMargin);
133 }
134
135 SkPaint paint;
136 paint.setARGB(kAlphaValue[a], 0, 0, 0);
137 paint.setAntiAlias(SkToBool(aa));
138 paint.setStyle(SkPaint::kStroke_Style);
139 paint.setStrokeWidth(0);
140
141 canvas->save();
142 canvas->translate(-bounds.fLeft, -bounds.fTop);
143 canvas->drawPath(fPaths[p], paint);
144 canvas->restore();
145
146 maxH = SkMaxScalar(maxH, bounds.height());
147
148 SkScalar dx = bounds.width() + SkIntToScalar(kMargin);
149 x += dx;
150 canvas->translate(dx, 0);
151 }
152 }
153 }
154 canvas->restore();
155 }
156
157private:
158 SkTArray<SkPath> fPaths;
159 typedef GM INHERITED;
160};
161
162//////////////////////////////////////////////////////////////////////////////
163
164static GM* MyFactory(void*) { return new HairlinesGM; }
165static GMRegistry reg(MyFactory);
166
167}