blob: 63ffe951c642af41db34e389497ff9bd0be5a867 [file] [log] [blame]
halcanary8b2bc252015-10-06 09:41:47 -07001/*
2 * Copyright 2015 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
Brian Osmanee6aa802017-07-12 17:26:34 -04008#include "SkAutoPixmapStorage.h"
9#include "SkColorPriv.h"
10#include "SkImage.h"
11#include "SkParsePath.h"
halcanary8b2bc252015-10-06 09:41:47 -070012#include "SkPath.h"
Brian Osmanee6aa802017-07-12 17:26:34 -040013#include "SkSurface.h"
halcanary8b2bc252015-10-06 09:41:47 -070014#include "gm.h"
15
Brian Osmanee6aa802017-07-12 17:26:34 -040016// GM to test combinations of stroking zero length paths with different caps and other settings
17// Variables:
18// * Antialiasing: On, Off
19// * Caps: Butt, Round, Square
20// * Stroke width: 0, 0.9, 1, 1.1, 15, 25
21// * Path form: M, ML, MLZ, MZ
22// * Path contours: 1 or 2
23// * Path verbs: Line, Quad, Cubic, Conic
24//
25// Each test is drawn to a 50x20 offscreen surface, and expected to produce some number (0 - 2) of
26// visible pieces of cap geometry. These are counted by scanning horizontally for peaks (blobs).
caryclark1f17ab52015-12-16 08:53:41 -080027
Brian Osmanee6aa802017-07-12 17:26:34 -040028static bool draw_path_cell(SkCanvas* canvas, SkImage* img, int expectedCaps) {
29 // Draw the image
30 canvas->drawImage(img, 0, 0);
halcanary8b2bc252015-10-06 09:41:47 -070031
Brian Osmanee6aa802017-07-12 17:26:34 -040032 int w = img->width(), h = img->height();
halcanary8b2bc252015-10-06 09:41:47 -070033
Brian Osmanee6aa802017-07-12 17:26:34 -040034 // Read the pixels back
35 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
36 SkAutoPixmapStorage pmap;
37 pmap.alloc(info);
38 SkAssertResult(img->readPixels(pmap, 0, 0));
halcanary8b2bc252015-10-06 09:41:47 -070039
Brian Osmanee6aa802017-07-12 17:26:34 -040040 // To account for rasterization differences, we scan the middle two rows [y, y+1] of the image
41 SkASSERT(h % 2 == 0);
42 int y = (h - 1) / 2;
halcanary8b2bc252015-10-06 09:41:47 -070043
Brian Osmanee6aa802017-07-12 17:26:34 -040044 bool inBlob = false;
45 int numBlobs = 0;
46 for (int x = 0; x < w; ++x) {
47 // We drew white-on-black. We can look for any non-zero value. Just check red.
48 // And we care if either row is non-zero, so just add them to simplify everything.
49 uint32_t v = SkGetPackedR32(*pmap.addr32(x, y)) + SkGetPackedR32(*pmap.addr32(x, y + 1));
halcanary8b2bc252015-10-06 09:41:47 -070050
Brian Osmanee6aa802017-07-12 17:26:34 -040051 if (!inBlob && v) {
52 ++numBlobs;
53 }
54 inBlob = SkToBool(v);
caryclark1f17ab52015-12-16 08:53:41 -080055 }
56
Brian Osmanee6aa802017-07-12 17:26:34 -040057 SkPaint outline;
58 outline.setStyle(SkPaint::kStroke_Style);
59 if (numBlobs == expectedCaps) {
60 outline.setColor(0xFF007F00); // Green
61 } else if (numBlobs > expectedCaps) {
62 outline.setColor(0xFF7F7F00); // Yellow -- more geometry than expected
63 } else {
64 outline.setColor(0xFF7F0000); // Red -- missing some geometry
caryclark1f17ab52015-12-16 08:53:41 -080065 }
66
Brian Osmanee6aa802017-07-12 17:26:34 -040067 canvas->drawRect(SkRect::MakeWH(w, h), outline);
68 return numBlobs == expectedCaps;
69}
caryclark1f17ab52015-12-16 08:53:41 -080070
Brian Osmanee6aa802017-07-12 17:26:34 -040071static const SkPaint::Cap kCaps[] = {
72 SkPaint::kButt_Cap,
73 SkPaint::kRound_Cap,
74 SkPaint::kSquare_Cap
caryclark1f17ab52015-12-16 08:53:41 -080075};
76
Brian Osmanee6aa802017-07-12 17:26:34 -040077static const SkScalar kWidths[] = { 0.0f, 0.9f, 1.0f, 1.1f, 15.0f, 25.0f };
caryclark1f17ab52015-12-16 08:53:41 -080078
Brian Osmanee6aa802017-07-12 17:26:34 -040079// Full set of path structures for single contour case (each primitive with and without a close)
80static const char* kAllVerbs[] = {
81 nullptr,
82 "z ",
83 "l 0 0 ",
84 "l 0 0 z ",
85 "q 0 0 0 0 ",
86 "q 0 0 0 0 z ",
87 "c 0 0 0 0 0 0 ",
88 "c 0 0 0 0 0 0 z ",
89 "a 0 0 0 0 0 0 0 ",
90 "a 0 0 0 0 0 0 0 z "
91};
92
93// Reduced set of path structures for double contour case, to keep total number of cases down
94static const char* kSomeVerbs[] = {
95 nullptr,
96 "z ",
97 "l 0 0 ",
98 "l 0 0 z ",
99 "q 0 0 0 0 ",
100 "q 0 0 0 0 z ",
101};
102
103static const int kCellWidth = 50;
104static const int kCellHeight = 20;
105static const int kCellPad = 2;
106
107static const int kNumRows = SK_ARRAY_COUNT(kCaps) * SK_ARRAY_COUNT(kWidths);
108static const int kNumColumns = SK_ARRAY_COUNT(kAllVerbs);
109static const int kTotalWidth = kNumColumns * (kCellWidth + kCellPad) + kCellPad;
110static const int kTotalHeight = kNumRows * (kCellHeight + kCellPad) + kCellPad;
111
112static const int kDblContourNumColums = SK_ARRAY_COUNT(kSomeVerbs) * SK_ARRAY_COUNT(kSomeVerbs);
113static const int kDblContourTotalWidth = kDblContourNumColums * (kCellWidth + kCellPad) + kCellPad;
114
115// 50% transparent versions of the colors used for positive/negative triage icons on gold.skia.org
116static const SkColor kFailureRed = 0x7FE7298A;
117static const SkColor kSuccessGreen = 0x7F1B9E77;
118
119static void draw_zero_length_capped_paths(SkCanvas* canvas, bool aa) {
120 canvas->translate(kCellPad, kCellPad);
121
122 SkImageInfo info = canvas->imageInfo().makeWH(kCellWidth, kCellHeight);
123 auto surface = canvas->makeSurface(info);
124 if (!surface) {
125 surface = SkSurface::MakeRasterN32Premul(kCellWidth, kCellHeight);
126 }
127
128 SkPaint paint;
129 paint.setColor(SK_ColorWHITE);
130 paint.setAntiAlias(aa);
131 paint.setStyle(SkPaint::kStroke_Style);
132
133 int numFailedTests = 0;
134 for (auto cap : kCaps) {
135 for (auto width : kWidths) {
136 paint.setStrokeCap(cap);
137 paint.setStrokeWidth(width);
138 canvas->save();
139
140 for (auto verb : kAllVerbs) {
141 SkString pathStr;
142 pathStr.appendf("M %f %f ", (kCellWidth - 1) * 0.5f, (kCellHeight - 1) * 0.5f);
143 if (verb) {
144 pathStr.append(verb);
145 }
146
147 SkPath path;
148 SkParsePath::FromSVGString(pathStr.c_str(), &path);
149
150 surface->getCanvas()->clear(SK_ColorTRANSPARENT);
151 surface->getCanvas()->drawPath(path, paint);
152 auto img = surface->makeImageSnapshot();
153
154 // All cases should draw one cap, except for butt capped, and dangling moves
155 // (without a verb or close), which shouldn't draw anything.
156 int expectedCaps = ((SkPaint::kButt_Cap == cap) || !verb) ? 0 : 1;
157
158 if (!draw_path_cell(canvas, img.get(), expectedCaps)) {
159 ++numFailedTests;
160 }
161 canvas->translate(kCellWidth + kCellPad, 0);
162 }
163 canvas->restore();
164 canvas->translate(0, kCellHeight + kCellPad);
165 }
166 }
167
168 canvas->drawColor(numFailedTests > 0 ? kFailureRed : kSuccessGreen);
169}
170
171DEF_SIMPLE_GM_BG(zero_length_paths_aa, canvas, kTotalWidth, kTotalHeight, SK_ColorBLACK) {
172 draw_zero_length_capped_paths(canvas, true);
173}
174
175DEF_SIMPLE_GM_BG(zero_length_paths_bw, canvas, kTotalWidth, kTotalHeight, SK_ColorBLACK) {
176 draw_zero_length_capped_paths(canvas, false);
177}
178
179static void draw_zero_length_capped_paths_dbl_contour(SkCanvas* canvas, bool aa) {
180 canvas->translate(kCellPad, kCellPad);
181
182 SkImageInfo info = canvas->imageInfo().makeWH(kCellWidth, kCellHeight);
183 auto surface = canvas->makeSurface(info);
184 if (!surface) {
185 surface = SkSurface::MakeRasterN32Premul(kCellWidth, kCellHeight);
186 }
187
188 SkPaint paint;
189 paint.setColor(SK_ColorWHITE);
190 paint.setAntiAlias(aa);
191 paint.setStyle(SkPaint::kStroke_Style);
192
193 int numFailedTests = 0;
194 for (auto cap : kCaps) {
195 for (auto width : kWidths) {
196 paint.setStrokeCap(cap);
197 paint.setStrokeWidth(width);
198 canvas->save();
199
200 for (auto firstVerb : kSomeVerbs) {
201 for (auto secondVerb : kSomeVerbs) {
202 int expectedCaps = 0;
203
204 SkString pathStr;
205 pathStr.append("M 9.5 9.5 ");
206 if (firstVerb) {
207 pathStr.append(firstVerb);
208 ++expectedCaps;
209 }
210 pathStr.append("M 40.5 9.5 ");
211 if (secondVerb) {
212 pathStr.append(secondVerb);
213 ++expectedCaps;
214 }
215
216 SkPath path;
217 SkParsePath::FromSVGString(pathStr.c_str(), &path);
218
219 surface->getCanvas()->clear(SK_ColorTRANSPARENT);
220 surface->getCanvas()->drawPath(path, paint);
221 auto img = surface->makeImageSnapshot();
222
223 if (SkPaint::kButt_Cap == cap) {
224 expectedCaps = 0;
225 }
226
227 if (!draw_path_cell(canvas, img.get(), expectedCaps)) {
228 ++numFailedTests;
229 }
230 canvas->translate(kCellWidth + kCellPad, 0);
231 }
232 }
233 canvas->restore();
234 canvas->translate(0, kCellHeight + kCellPad);
235 }
236 }
237
238 canvas->drawColor(numFailedTests > 0 ? kFailureRed : kSuccessGreen);
239}
240
241DEF_SIMPLE_GM_BG(zero_length_paths_dbl_aa, canvas, kDblContourTotalWidth, kTotalHeight,
242 SK_ColorBLACK) {
243 draw_zero_length_capped_paths_dbl_contour(canvas, true);
244}
245
246DEF_SIMPLE_GM_BG(zero_length_paths_dbl_bw, canvas, kDblContourTotalWidth, kTotalHeight,
247 SK_ColorBLACK) {
248 draw_zero_length_capped_paths_dbl_contour(canvas, false);
249}