2017-02-03 15:33:25 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkCanvas.h"
|
2017-07-31 13:34:58 +00:00
|
|
|
#include "SkDrawShadowInfo.h"
|
2017-02-03 15:33:25 +00:00
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkShadowTessellator.h"
|
|
|
|
#include "SkShadowUtils.h"
|
2017-02-06 20:47:44 +00:00
|
|
|
#include "SkVertices.h"
|
2017-02-03 15:33:25 +00:00
|
|
|
#include "Test.h"
|
|
|
|
|
2017-02-06 18:38:23 +00:00
|
|
|
void tessellate_shadow(skiatest::Reporter* reporter, const SkPath& path, const SkMatrix& ctm,
|
|
|
|
bool expectSuccess) {
|
2017-03-27 18:25:29 +00:00
|
|
|
|
2017-05-08 18:19:30 +00:00
|
|
|
auto heightParams = SkPoint3::Make(0, 0, 4);
|
2017-04-13 16:20:21 +00:00
|
|
|
|
2017-05-08 18:19:30 +00:00
|
|
|
auto verts = SkShadowTessellator::MakeAmbient(path, ctm, heightParams, true);
|
2017-02-03 15:33:25 +00:00
|
|
|
if (expectSuccess != SkToBool(verts)) {
|
2017-02-06 20:47:44 +00:00
|
|
|
ERRORF(reporter, "Expected shadow tessellation to %s but it did not.",
|
2017-02-03 15:33:25 +00:00
|
|
|
expectSuccess ? "succeed" : "fail");
|
|
|
|
}
|
2017-05-08 18:19:30 +00:00
|
|
|
verts = SkShadowTessellator::MakeAmbient(path, ctm, heightParams, false);
|
2017-02-03 15:33:25 +00:00
|
|
|
if (expectSuccess != SkToBool(verts)) {
|
2017-02-06 20:47:44 +00:00
|
|
|
ERRORF(reporter, "Expected shadow tessellation to %s but it did not.",
|
2017-02-03 15:33:25 +00:00
|
|
|
expectSuccess ? "succeed" : "fail");
|
|
|
|
}
|
2017-05-08 18:19:30 +00:00
|
|
|
verts = SkShadowTessellator::MakeSpot(path, ctm, heightParams, {0, 0, 128}, 128.f, false);
|
2017-02-03 15:33:25 +00:00
|
|
|
if (expectSuccess != SkToBool(verts)) {
|
2017-02-06 20:47:44 +00:00
|
|
|
ERRORF(reporter, "Expected shadow tessellation to %s but it did not.",
|
2017-02-03 15:33:25 +00:00
|
|
|
expectSuccess ? "succeed" : "fail");
|
|
|
|
}
|
2017-05-08 18:19:30 +00:00
|
|
|
verts = SkShadowTessellator::MakeSpot(path, ctm, heightParams, {0, 0, 128}, 128.f, false);
|
2017-02-03 15:33:25 +00:00
|
|
|
if (expectSuccess != SkToBool(verts)) {
|
2017-02-06 20:47:44 +00:00
|
|
|
ERRORF(reporter, "Expected shadow tessellation to %s but it did not.",
|
2017-02-03 15:33:25 +00:00
|
|
|
expectSuccess ? "succeed" : "fail");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(ShadowUtils, reporter) {
|
|
|
|
SkCanvas canvas(100, 100);
|
2017-04-13 16:20:21 +00:00
|
|
|
|
2017-02-03 15:33:25 +00:00
|
|
|
SkPath path;
|
|
|
|
path.cubicTo(100, 50, 20, 100, 0, 0);
|
2017-04-13 16:20:21 +00:00
|
|
|
tessellate_shadow(reporter, path, canvas.getTotalMatrix(), true);
|
2017-02-03 15:33:25 +00:00
|
|
|
|
|
|
|
// This line segment has no area and no shadow.
|
|
|
|
path.reset();
|
|
|
|
path.lineTo(10.f, 10.f);
|
2017-02-06 18:38:23 +00:00
|
|
|
tessellate_shadow(reporter, path, canvas.getTotalMatrix(), false);
|
2017-02-03 15:33:25 +00:00
|
|
|
|
|
|
|
// A series of colinear line segments
|
|
|
|
path.reset();
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
path.lineTo((SkScalar)i, (SkScalar)i);
|
|
|
|
}
|
2017-02-06 18:38:23 +00:00
|
|
|
tessellate_shadow(reporter, path, canvas.getTotalMatrix(), false);
|
2017-02-03 15:33:25 +00:00
|
|
|
}
|
2017-07-31 13:34:58 +00:00
|
|
|
|
|
|
|
void check_xformed_bounds(skiatest::Reporter* reporter, const SkPath& path, const SkMatrix& ctm) {
|
|
|
|
const SkDrawShadowRec rec = {
|
|
|
|
SkPoint3::Make(0, 0, 4),
|
|
|
|
SkPoint3::Make(100, 0, 600),
|
|
|
|
800.f,
|
|
|
|
0.035f,
|
|
|
|
0.25f,
|
|
|
|
SK_ColorBLACK,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
SkRect bounds;
|
|
|
|
SkDrawShadowMetrics::GetLocalBounds(path, rec, ctm, &bounds);
|
|
|
|
ctm.mapRect(&bounds);
|
|
|
|
|
|
|
|
auto verts = SkShadowTessellator::MakeAmbient(path, ctm, rec.fZPlaneParams, true);
|
|
|
|
if (verts) {
|
|
|
|
REPORTER_ASSERT(reporter, bounds.contains(verts->bounds()));
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPoint mapXY = ctm.mapXY(rec.fLightPos.fX, rec.fLightPos.fY);
|
|
|
|
SkPoint3 devLightPos = SkPoint3::Make(mapXY.fX, mapXY.fY, rec.fLightPos.fZ);
|
|
|
|
verts = SkShadowTessellator::MakeSpot(path, ctm, rec.fZPlaneParams, devLightPos,
|
|
|
|
rec.fLightRadius, false);
|
|
|
|
if (verts) {
|
|
|
|
REPORTER_ASSERT(reporter, bounds.contains(verts->bounds()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_bounds(skiatest::Reporter* reporter, const SkPath& path) {
|
|
|
|
SkMatrix ctm;
|
|
|
|
ctm.setTranslate(100, 100);
|
|
|
|
check_xformed_bounds(reporter, path, ctm);
|
|
|
|
ctm.postScale(2, 2);
|
|
|
|
check_xformed_bounds(reporter, path, ctm);
|
|
|
|
ctm.preRotate(45);
|
|
|
|
check_xformed_bounds(reporter, path, ctm);
|
|
|
|
ctm.preSkew(40, -20);
|
|
|
|
check_xformed_bounds(reporter, path, ctm);
|
|
|
|
ctm[SkMatrix::kMPersp0] = 0.0001f;
|
|
|
|
ctm[SkMatrix::kMPersp1] = 12.f;
|
|
|
|
check_xformed_bounds(reporter, path, ctm);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(ShadowBounds, reporter) {
|
|
|
|
SkPath path;
|
|
|
|
path.addRRect(SkRRect::MakeRectXY(SkRect::MakeLTRB(-50, -20, 40, 30), 4, 4));
|
|
|
|
check_bounds(reporter, path);
|
|
|
|
|
|
|
|
path.reset();
|
|
|
|
path.addOval(SkRect::MakeLTRB(300, 300, 900, 900));
|
|
|
|
check_bounds(reporter, path);
|
|
|
|
|
|
|
|
path.reset();
|
|
|
|
path.cubicTo(100, 50, 20, 100, 0, 0);
|
|
|
|
check_bounds(reporter, path);
|
|
|
|
}
|