| 1 |
/* |
| 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 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkColor.h" |
| 11 |
#include "include/core/SkImage.h" |
| 12 |
#include "include/core/SkImageInfo.h" |
| 13 |
#include "include/core/SkPaint.h" |
| 14 |
#include "include/core/SkPath.h" |
| 15 |
#include "include/core/SkRect.h" |
| 16 |
#include "include/core/SkRefCnt.h" |
| 17 |
#include "include/core/SkScalar.h" |
| 18 |
#include "include/core/SkString.h" |
| 19 |
#include "include/core/SkSurface.h" |
| 20 |
#include "include/core/SkTypes.h" |
| 21 |
#include "include/utils/SkParsePath.h" |
| 22 |
#include "src/core/SkAutoPixmapStorage.h" |
| 23 |
#include "src/core/SkColorPriv.h" |
| 24 |
|
| 25 |
#if defined(SK_GANESH) |
| 26 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 27 |
#endif |
| 28 |
|
| 29 |
// GM to test combinations of stroking zero length paths with different caps and other settings |
| 30 |
// Variables: |
| 31 |
// * Antialiasing: On, Off |
| 32 |
// * Caps: Butt, Round, Square |
| 33 |
// * Stroke width: 0, 0.9, 1, 1.1, 15, 25 |
| 34 |
// * Path form: M, ML, MLZ, MZ |
| 35 |
// * Path contours: 1 or 2 |
| 36 |
// * Path verbs: Line, Quad, Cubic, Conic |
| 37 |
// |
| 38 |
// Each test is drawn to a 50x20 offscreen surface, and expected to produce some number (0 - 2) of |
| 39 |
// visible pieces of cap geometry. These are counted by scanning horizontally for peaks (blobs). |
| 40 |
|
| 41 |
static void draw_path_cell(SkCanvas* canvas, |
| 42 |
SkSurface* surface, |
| 43 |
int expectedCaps) { |
| 44 |
static const SkColor kFailureRed = 0x7F7F0000; |
| 45 |
static const SkColor kFailureYellow = 0x7F7F7F00; |
| 46 |
static const SkColor kSuccessGreen = 0x7F007f00; |
| 47 |
|
| 48 |
int w = surface->width(), h = surface->height(); |
| 49 |
|
| 50 |
// Read the pixels back |
| 51 |
SkImageInfo info = SkImageInfo::MakeN32Premul(w, h); |
| 52 |
SkAutoPixmapStorage pmap; |
| 53 |
pmap.alloc(info); |
| 54 |
if (!surface->readPixels(pmap, 0, 0)) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
// To account for rasterization differences, we scan the middle two rows [y, y+1] of the image |
| 59 |
SkASSERT(h % 2 == 0); |
| 60 |
int y = (h - 1) / 2; |
| 61 |
|
| 62 |
bool inBlob = false; |
| 63 |
int numBlobs = 0; |
| 64 |
for (int x = 0; x < w; ++x) { |
| 65 |
// We drew white-on-black. We can look for any non-zero value. Just check red. |
| 66 |
// And we care if either row is non-zero, so just add them to simplify everything. |
| 67 |
uint32_t v = SkGetPackedR32(*pmap.addr32(x, y)) + SkGetPackedR32(*pmap.addr32(x, y + 1)); |
| 68 |
|
| 69 |
if (!inBlob && v) { |
| 70 |
++numBlobs; |
| 71 |
} |
| 72 |
inBlob = SkToBool(v); |
| 73 |
} |
| 74 |
|
| 75 |
SkPaint result; |
| 76 |
|
| 77 |
if (numBlobs == expectedCaps) { |
| 78 |
result.setColor(kSuccessGreen); // Green |
| 79 |
} else if (numBlobs > expectedCaps) { |
| 80 |
result.setColor(kFailureYellow); // Yellow -- more geometry than expected |
| 81 |
} else { |
| 82 |
result.setColor(kFailureRed); // Red -- missing some geometry |
| 83 |
} |
| 84 |
|
| 85 |
auto img = surface->makeImageSnapshot(); |
| 86 |
canvas->drawImage(img, 0, 0); |
| 87 |
canvas->drawRect(SkRect::MakeWH(w, h), result); |
| 88 |
} |
| 89 |
|
| 90 |
static const SkPaint::Cap kCaps[] = { |
| 91 |
SkPaint::kButt_Cap, |
| 92 |
SkPaint::kRound_Cap, |
| 93 |
SkPaint::kSquare_Cap |
| 94 |
}; |
| 95 |
|
| 96 |
static const SkScalar kWidths[] = { 0.0f, 0.9f, 1.0f, 1.1f, 15.0f, 25.0f }; |
| 97 |
|
| 98 |
// Full set of path structures for single contour case (each primitive with and without a close) |
| 99 |
static const char* kAllVerbs[] = { |
| 100 |
nullptr, |
| 101 |
"z ", |
| 102 |
"l 0 0 ", |
| 103 |
"l 0 0 z ", |
| 104 |
"q 0 0 0 0 ", |
| 105 |
"q 0 0 0 0 z ", |
| 106 |
"c 0 0 0 0 0 0 ", |
| 107 |
"c 0 0 0 0 0 0 z ", |
| 108 |
"a 0 0 0 0 0 0 0 ", |
| 109 |
"a 0 0 0 0 0 0 0 z " |
| 110 |
}; |
| 111 |
|
| 112 |
// Reduced set of path structures for double contour case, to keep total number of cases down |
| 113 |
static const char* kSomeVerbs[] = { |
| 114 |
nullptr, |
| 115 |
"z ", |
| 116 |
"l 0 0 ", |
| 117 |
"l 0 0 z ", |
| 118 |
"q 0 0 0 0 ", |
| 119 |
"q 0 0 0 0 z ", |
| 120 |
}; |
| 121 |
|
| 122 |
static const int kCellWidth = 50; |
| 123 |
static const int kCellHeight = 20; |
| 124 |
static const int kCellPad = 2; |
| 125 |
|
| 126 |
static const int kNumRows = std::size(kCaps) * std::size(kWidths); |
| 127 |
static const int kNumColumns = std::size(kAllVerbs); |
| 128 |
static const int kTotalWidth = kNumColumns * (kCellWidth + kCellPad) + kCellPad; |
| 129 |
static const int kTotalHeight = kNumRows * (kCellHeight + kCellPad) + kCellPad; |
| 130 |
|
| 131 |
static const int kDblContourNumColums = std::size(kSomeVerbs) * std::size(kSomeVerbs); |
| 132 |
static const int kDblContourTotalWidth = kDblContourNumColums * (kCellWidth + kCellPad) + kCellPad; |
| 133 |
|
| 134 |
static skiagm::DrawResult draw_zero_length_capped_paths(SkCanvas* canvas, bool aa, |
| 135 |
SkString* errorMsg) { |
| 136 |
canvas->translate(kCellPad, kCellPad); |
| 137 |
|
| 138 |
SkImageInfo info = canvas->imageInfo().makeWH(kCellWidth, kCellHeight); |
| 139 |
auto surface = canvas->makeSurface(info); |
| 140 |
if (!surface) { |
| 141 |
surface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(kCellWidth, kCellHeight)); |
| 142 |
} |
| 143 |
|
| 144 |
SkPaint paint; |
| 145 |
paint.setColor(SK_ColorWHITE); |
| 146 |
paint.setAntiAlias(aa); |
| 147 |
paint.setStyle(SkPaint::kStroke_Style); |
| 148 |
|
| 149 |
for (auto cap : kCaps) { |
| 150 |
for (auto width : kWidths) { |
| 151 |
paint.setStrokeCap(cap); |
| 152 |
paint.setStrokeWidth(width); |
| 153 |
canvas->save(); |
| 154 |
|
| 155 |
for (auto verb : kAllVerbs) { |
| 156 |
SkString pathStr; |
| 157 |
pathStr.appendf("M %f %f ", (kCellWidth - 1) * 0.5f, (kCellHeight - 1) * 0.5f); |
| 158 |
if (verb) { |
| 159 |
pathStr.append(verb); |
| 160 |
} |
| 161 |
|
| 162 |
SkPath path = SkParsePath::FromSVGString(pathStr.c_str()).value_or(SkPath()); |
| 163 |
|
| 164 |
surface->getCanvas()->clear(SK_ColorTRANSPARENT); |
| 165 |
surface->getCanvas()->drawPath(path, paint); |
| 166 |
|
| 167 |
// All cases should draw one cap, except for butt capped, and dangling moves |
| 168 |
// (without a verb or close), which shouldn't draw anything. |
| 169 |
int expectedCaps = ((SkPaint::kButt_Cap == cap) || !verb) ? 0 : 1; |
| 170 |
|
| 171 |
draw_path_cell(canvas, surface.get(), expectedCaps); |
| 172 |
canvas->translate(kCellWidth + kCellPad, 0); |
| 173 |
} |
| 174 |
canvas->restore(); |
| 175 |
canvas->translate(0, kCellHeight + kCellPad); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
return skiagm::DrawResult::kOk; |
| 180 |
} |
| 181 |
|
| 182 |
DEF_SIMPLE_GM_BG_CAN_FAIL(zero_length_paths_aa, canvas, errorMsg, |
| 183 |
kTotalWidth, kTotalHeight, SK_ColorBLACK) { |
| 184 |
return draw_zero_length_capped_paths(canvas, true, errorMsg); |
| 185 |
} |
| 186 |
|
| 187 |
DEF_SIMPLE_GM_BG_CAN_FAIL(zero_length_paths_bw, canvas, errorMsg, |
| 188 |
kTotalWidth, kTotalHeight, SK_ColorBLACK) { |
| 189 |
return draw_zero_length_capped_paths(canvas, false, errorMsg); |
| 190 |
} |
| 191 |
|
| 192 |
static skiagm::DrawResult draw_zero_length_capped_paths_dbl_contour(SkCanvas* canvas, bool aa, |
| 193 |
SkString* errorMsg) { |
| 194 |
#if defined(SK_GANESH) |
| 195 |
auto rContext = canvas->recordingContext(); |
| 196 |
auto dContext = GrAsDirectContext(rContext); |
| 197 |
if (!dContext && rContext) { |
| 198 |
*errorMsg = "Not supported in DDL mode"; |
| 199 |
return skiagm::DrawResult::kSkip; |
| 200 |
} |
| 201 |
#endif |
| 202 |
|
| 203 |
canvas->translate(kCellPad, kCellPad); |
| 204 |
|
| 205 |
SkImageInfo info = canvas->imageInfo().makeWH(kCellWidth, kCellHeight); |
| 206 |
auto surface = canvas->makeSurface(info); |
| 207 |
if (!surface) { |
| 208 |
surface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(kCellWidth, kCellHeight)); |
| 209 |
} |
| 210 |
|
| 211 |
SkPaint paint; |
| 212 |
paint.setColor(SK_ColorWHITE); |
| 213 |
paint.setAntiAlias(aa); |
| 214 |
paint.setStyle(SkPaint::kStroke_Style); |
| 215 |
|
| 216 |
for (auto cap : kCaps) { |
| 217 |
for (auto width : kWidths) { |
| 218 |
paint.setStrokeCap(cap); |
| 219 |
paint.setStrokeWidth(width); |
| 220 |
canvas->save(); |
| 221 |
|
| 222 |
for (auto firstVerb : kSomeVerbs) { |
| 223 |
for (auto secondVerb : kSomeVerbs) { |
| 224 |
int expectedCaps = 0; |
| 225 |
|
| 226 |
SkString pathStr; |
| 227 |
pathStr.append("M 9.5 9.5 "); |
| 228 |
if (firstVerb) { |
| 229 |
pathStr.append(firstVerb); |
| 230 |
++expectedCaps; |
| 231 |
} |
| 232 |
pathStr.append("M 40.5 9.5 "); |
| 233 |
if (secondVerb) { |
| 234 |
pathStr.append(secondVerb); |
| 235 |
++expectedCaps; |
| 236 |
} |
| 237 |
|
| 238 |
SkPath path = SkParsePath::FromSVGString(pathStr.c_str()).value_or(SkPath()); |
| 239 |
|
| 240 |
surface->getCanvas()->clear(SK_ColorTRANSPARENT); |
| 241 |
surface->getCanvas()->drawPath(path, paint); |
| 242 |
|
| 243 |
if (SkPaint::kButt_Cap == cap) { |
| 244 |
expectedCaps = 0; |
| 245 |
} |
| 246 |
|
| 247 |
draw_path_cell(canvas, surface.get(), expectedCaps); |
| 248 |
canvas->translate(kCellWidth + kCellPad, 0); |
| 249 |
} |
| 250 |
} |
| 251 |
canvas->restore(); |
| 252 |
canvas->translate(0, kCellHeight + kCellPad); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
return skiagm::DrawResult::kOk; |
| 257 |
} |
| 258 |
|
| 259 |
DEF_SIMPLE_GM_BG_CAN_FAIL(zero_length_paths_dbl_aa, canvas, errorMsg, |
| 260 |
kDblContourTotalWidth, kTotalHeight, SK_ColorBLACK) { |
| 261 |
return draw_zero_length_capped_paths_dbl_contour(canvas, true, errorMsg); |
| 262 |
} |
| 263 |
|
| 264 |
DEF_SIMPLE_GM_BG_CAN_FAIL(zero_length_paths_dbl_bw, canvas, errorMsg, |
| 265 |
kDblContourTotalWidth, kTotalHeight, SK_ColorBLACK) { |
| 266 |
return draw_zero_length_capped_paths_dbl_contour(canvas, false, errorMsg); |
| 267 |
} |