2011-12-21 19:36:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "tests/Test.h"
|
2011-12-21 19:36:21 +00:00
|
|
|
|
|
|
|
#define DIMENSION 32
|
|
|
|
|
|
|
|
static void drawAndTest(skiatest::Reporter* reporter, const SkPath& path,
|
|
|
|
const SkPaint& paint, bool shouldDraw) {
|
|
|
|
SkBitmap bm;
|
2014-02-13 14:41:43 +00:00
|
|
|
bm.allocN32Pixels(DIMENSION, DIMENSION);
|
|
|
|
SkASSERT(DIMENSION*4 == bm.rowBytes()); // ensure no padding on each row
|
2012-12-06 21:47:40 +00:00
|
|
|
bm.eraseColor(SK_ColorTRANSPARENT);
|
2011-12-21 19:36:21 +00:00
|
|
|
|
|
|
|
SkCanvas canvas(bm);
|
|
|
|
SkPaint p(paint);
|
|
|
|
p.setColor(SK_ColorWHITE);
|
|
|
|
|
|
|
|
canvas.drawPath(path, p);
|
|
|
|
|
|
|
|
size_t count = DIMENSION * DIMENSION;
|
|
|
|
const SkPMColor* ptr = bm.getAddr32(0, 0);
|
|
|
|
|
2012-06-06 12:03:39 +00:00
|
|
|
SkPMColor andValue = ~0U;
|
2011-12-21 19:36:21 +00:00
|
|
|
SkPMColor orValue = 0;
|
|
|
|
for (size_t i = 0; i < count; ++i) {
|
|
|
|
SkPMColor c = ptr[i];
|
|
|
|
andValue &= c;
|
|
|
|
orValue |= c;
|
|
|
|
}
|
|
|
|
|
|
|
|
// success means we drew everywhere or nowhere (depending on shouldDraw)
|
2012-01-06 19:22:11 +00:00
|
|
|
bool success = shouldDraw ? (~0U == andValue) : (0 == orValue);
|
2011-12-21 19:36:21 +00:00
|
|
|
|
|
|
|
if (!success) {
|
2014-01-10 14:58:10 +00:00
|
|
|
const char* str;
|
2011-12-21 19:36:21 +00:00
|
|
|
if (shouldDraw) {
|
2014-01-10 14:58:10 +00:00
|
|
|
str = "Path expected to draw everywhere, but didn't. ";
|
2011-12-21 19:36:21 +00:00
|
|
|
} else {
|
2014-01-10 14:58:10 +00:00
|
|
|
str = "Path expected to draw nowhere, but did. ";
|
2011-12-21 19:36:21 +00:00
|
|
|
}
|
2014-01-10 14:58:10 +00:00
|
|
|
ERRORF(reporter, "%s style[%d] cap[%d] join[%d] antialias[%d]"
|
|
|
|
" filltype[%d] ptcount[%d]", str, paint.getStyle(),
|
|
|
|
paint.getStrokeCap(), paint.getStrokeJoin(),
|
|
|
|
paint.isAntiAlias(), path.getFillType(), path.countPoints());
|
2011-12-21 19:36:21 +00:00
|
|
|
// uncomment this if you want to step in to see the failure
|
|
|
|
// canvas.drawPath(path, p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-09 20:20:49 +00:00
|
|
|
enum DrawCaps {
|
|
|
|
kDontDrawCaps,
|
|
|
|
kDrawCaps
|
|
|
|
};
|
|
|
|
|
|
|
|
static void iter_paint(skiatest::Reporter* reporter, const SkPath& path, bool shouldDraw,
|
|
|
|
DrawCaps drawCaps) {
|
2011-12-21 19:36:21 +00:00
|
|
|
static const SkPaint::Cap gCaps[] = {
|
|
|
|
SkPaint::kButt_Cap,
|
|
|
|
SkPaint::kRound_Cap,
|
|
|
|
SkPaint::kSquare_Cap
|
|
|
|
};
|
|
|
|
static const SkPaint::Join gJoins[] = {
|
|
|
|
SkPaint::kMiter_Join,
|
|
|
|
SkPaint::kRound_Join,
|
|
|
|
SkPaint::kBevel_Join
|
|
|
|
};
|
|
|
|
static const SkPaint::Style gStyles[] = {
|
|
|
|
SkPaint::kFill_Style,
|
|
|
|
SkPaint::kStroke_Style,
|
|
|
|
SkPaint::kStrokeAndFill_Style
|
|
|
|
};
|
|
|
|
for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
|
|
|
|
for (size_t join = 0; join < SK_ARRAY_COUNT(gJoins); ++join) {
|
|
|
|
for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
|
2015-09-09 20:20:49 +00:00
|
|
|
if (drawCaps && SkPaint::kButt_Cap != gCaps[cap]
|
|
|
|
&& SkPaint::kFill_Style != gStyles[style]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-12-21 19:36:21 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setStrokeWidth(SkIntToScalar(10));
|
|
|
|
|
|
|
|
paint.setStrokeCap(gCaps[cap]);
|
|
|
|
paint.setStrokeJoin(gJoins[join]);
|
|
|
|
paint.setStyle(gStyles[style]);
|
|
|
|
|
|
|
|
paint.setAntiAlias(false);
|
|
|
|
drawAndTest(reporter, path, paint, shouldDraw);
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
drawAndTest(reporter, path, paint, shouldDraw);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CX (SkIntToScalar(DIMENSION) / 2)
|
|
|
|
#define CY (SkIntToScalar(DIMENSION) / 2)
|
|
|
|
|
2013-02-27 19:17:41 +00:00
|
|
|
static void make_empty(SkPath*) {}
|
2011-12-21 20:26:01 +00:00
|
|
|
static void make_M(SkPath* path) { path->moveTo(CX, CY); }
|
2018-08-15 14:23:39 +00:00
|
|
|
static void make_MM(SkPath* path) { path->moveTo(CX, CY).moveTo(CX, CY); }
|
|
|
|
static void make_MZM(SkPath* path) { path->moveTo(CX, CY).close().moveTo(CX, CY); }
|
|
|
|
static void make_L(SkPath* path) { path->moveTo(CX, CY).lineTo(CX, CY); }
|
|
|
|
static void make_Q(SkPath* path) { path->moveTo(CX, CY).quadTo(CX, CY, CX, CY); }
|
|
|
|
static void make_C(SkPath* path) { path->moveTo(CX, CY).cubicTo(CX, CY, CX, CY, CX, CY); }
|
2011-12-21 19:36:21 +00:00
|
|
|
|
|
|
|
/* Two invariants are tested: How does an empty/degenerate path draw?
|
|
|
|
* - if the path is drawn inverse, it should draw everywhere
|
|
|
|
* - if the path is drawn non-inverse, it should draw nowhere
|
|
|
|
*
|
|
|
|
* Things to iterate on:
|
|
|
|
* - path (empty, degenerate line/quad/cubic w/ and w/o close
|
|
|
|
* - paint style
|
|
|
|
* - path filltype
|
|
|
|
* - path stroke variants (e.g. caps, joins, width)
|
|
|
|
*/
|
|
|
|
static void test_emptydrawing(skiatest::Reporter* reporter) {
|
|
|
|
static void (*gMakeProc[])(SkPath*) = {
|
2011-12-21 20:26:01 +00:00
|
|
|
make_empty, make_M, make_MM, make_MZM, make_L, make_Q, make_C
|
2011-12-21 19:36:21 +00:00
|
|
|
};
|
2019-11-26 17:17:17 +00:00
|
|
|
static SkPathFillType gFills[] = {
|
|
|
|
SkPathFillType::kWinding,
|
|
|
|
SkPathFillType::kEvenOdd,
|
|
|
|
SkPathFillType::kInverseWinding,
|
|
|
|
SkPathFillType::kInverseEvenOdd
|
2011-12-21 19:36:21 +00:00
|
|
|
};
|
|
|
|
for (int doClose = 0; doClose < 2; ++doClose) {
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gMakeProc); ++i) {
|
|
|
|
SkPath path;
|
|
|
|
gMakeProc[i](&path);
|
|
|
|
if (doClose) {
|
|
|
|
path.close();
|
|
|
|
}
|
2015-09-09 20:20:49 +00:00
|
|
|
/* zero length segments and close following moves draw round and square caps */
|
|
|
|
bool allowCaps = make_L == gMakeProc[i] || make_Q == gMakeProc[i]
|
|
|
|
|| make_C == gMakeProc[i] || make_MZM == gMakeProc[i];
|
|
|
|
allowCaps |= SkToBool(doClose);
|
2011-12-21 19:36:21 +00:00
|
|
|
for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
|
|
|
|
path.setFillType(gFills[fill]);
|
|
|
|
bool shouldDraw = path.isInverseFillType();
|
2015-09-09 20:20:49 +00:00
|
|
|
iter_paint(reporter, path, shouldDraw, allowCaps ? kDrawCaps : kDontDrawCaps);
|
2011-12-21 19:36:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(EmptyPath, reporter) {
|
2011-12-21 19:36:21 +00:00
|
|
|
test_emptydrawing(reporter);
|
|
|
|
}
|