blob: 45bb197918d80fec1e0ab79f9a5c16f9275866f1 [file] [log] [blame]
Brian Salomon0dda9cb2017-02-03 10:33:25 -05001/*
2 * Copyright 2017 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 "SkCanvas.h"
9#include "SkPath.h"
10#include "SkShadowTessellator.h"
11#include "SkShadowUtils.h"
12#include "Test.h"
13
Jim Van Vertha84898d2017-02-06 13:38:23 -050014void tessellate_shadow(skiatest::Reporter* reporter, const SkPath& path, const SkMatrix& ctm,
15 bool expectSuccess) {
Brian Salomon0dda9cb2017-02-03 10:33:25 -050016 static constexpr SkScalar kRadius = 2.f;
17 static constexpr SkColor kUmbraColor = 0xFFFFFFFF;
18 static constexpr SkColor kPenumbraColor = 0x20202020;
Jim Van Vertha84898d2017-02-06 13:38:23 -050019 auto verts = SkShadowVertices::MakeAmbient(path, ctm, kRadius, kUmbraColor, kPenumbraColor,
20 true);
Brian Salomon0dda9cb2017-02-03 10:33:25 -050021 if (expectSuccess != SkToBool(verts)) {
22 ERRORF(reporter, "Expected shadow tessellation to % but it did not.",
23 expectSuccess ? "succeed" : "fail");
24 }
Jim Van Vertha84898d2017-02-06 13:38:23 -050025 verts = SkShadowVertices::MakeAmbient(path, ctm, kRadius, kUmbraColor, kPenumbraColor, false);
Brian Salomon0dda9cb2017-02-03 10:33:25 -050026 if (expectSuccess != SkToBool(verts)) {
27 ERRORF(reporter, "Expected shadow tessellation to % but it did not.",
28 expectSuccess ? "succeed" : "fail");
29 }
Jim Van Vertha84898d2017-02-06 13:38:23 -050030 verts = SkShadowVertices::MakeSpot(path, ctm, 1.5f, {0, 0}, kRadius, kUmbraColor,
31 kPenumbraColor, false);
Brian Salomon0dda9cb2017-02-03 10:33:25 -050032 if (expectSuccess != SkToBool(verts)) {
33 ERRORF(reporter, "Expected shadow tessellation to % but it did not.",
34 expectSuccess ? "succeed" : "fail");
35 }
Jim Van Vertha84898d2017-02-06 13:38:23 -050036 verts = SkShadowVertices::MakeSpot(path, ctm, 1.5f, {0, 0}, kRadius, kUmbraColor,
37 kPenumbraColor, true);
Brian Salomon0dda9cb2017-02-03 10:33:25 -050038 if (expectSuccess != SkToBool(verts)) {
39 ERRORF(reporter, "Expected shadow tessellation to % but it did not.",
40 expectSuccess ? "succeed" : "fail");
41 }
42}
43
44DEF_TEST(ShadowUtils, reporter) {
45 SkCanvas canvas(100, 100);
Jim Van Vertha84898d2017-02-06 13:38:23 -050046 // Currently SkShadowUtils doesn't really support cubics when compiled without SK_SUPPORT_GPU.
47 // However, this should now not crash.
Brian Salomon0dda9cb2017-02-03 10:33:25 -050048 SkPath path;
49 path.cubicTo(100, 50, 20, 100, 0, 0);
Jim Van Vertha84898d2017-02-06 13:38:23 -050050 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), (bool)SK_SUPPORT_GPU);
Brian Salomon0dda9cb2017-02-03 10:33:25 -050051
52 // This line segment has no area and no shadow.
53 path.reset();
54 path.lineTo(10.f, 10.f);
Jim Van Vertha84898d2017-02-06 13:38:23 -050055 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), false);
Brian Salomon0dda9cb2017-02-03 10:33:25 -050056
57 // A series of colinear line segments
58 path.reset();
59 for (int i = 0; i < 10; ++i) {
60 path.lineTo((SkScalar)i, (SkScalar)i);
61 }
Jim Van Vertha84898d2017-02-06 13:38:23 -050062 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), false);
Brian Salomon0dda9cb2017-02-03 10:33:25 -050063}