blob: 7f9ce91158c09c4c0acd89c4d151e5001c1788a3 [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"
Brian Salomonaff27a22017-02-06 15:47:44 -050012#include "SkVertices.h"
Brian Salomon0dda9cb2017-02-03 10:33:25 -050013#include "Test.h"
14
Jim Van Vertha84898d2017-02-06 13:38:23 -050015void tessellate_shadow(skiatest::Reporter* reporter, const SkPath& path, const SkMatrix& ctm,
16 bool expectSuccess) {
Brian Salomon0dda9cb2017-02-03 10:33:25 -050017 static constexpr SkScalar kRadius = 2.f;
18 static constexpr SkColor kUmbraColor = 0xFFFFFFFF;
19 static constexpr SkColor kPenumbraColor = 0x20202020;
Brian Salomonaff27a22017-02-06 15:47:44 -050020 auto verts =
21 SkShadowTessellator::MakeAmbient(path, ctm, kRadius, kUmbraColor, kPenumbraColor, true);
Brian Salomon0dda9cb2017-02-03 10:33:25 -050022 if (expectSuccess != SkToBool(verts)) {
Brian Salomonaff27a22017-02-06 15:47:44 -050023 ERRORF(reporter, "Expected shadow tessellation to %s but it did not.",
Brian Salomon0dda9cb2017-02-03 10:33:25 -050024 expectSuccess ? "succeed" : "fail");
25 }
Brian Salomonaff27a22017-02-06 15:47:44 -050026 verts = SkShadowTessellator::MakeAmbient(path, ctm, kRadius, kUmbraColor, kPenumbraColor,
27 false);
Brian Salomon0dda9cb2017-02-03 10:33:25 -050028 if (expectSuccess != SkToBool(verts)) {
Brian Salomonaff27a22017-02-06 15:47:44 -050029 ERRORF(reporter, "Expected shadow tessellation to %s but it did not.",
Brian Salomon0dda9cb2017-02-03 10:33:25 -050030 expectSuccess ? "succeed" : "fail");
31 }
Brian Salomonaff27a22017-02-06 15:47:44 -050032 verts = SkShadowTessellator::MakeSpot(path, ctm, 1.5f, {0, 0}, kRadius, kUmbraColor,
33 kPenumbraColor, false);
Brian Salomon0dda9cb2017-02-03 10:33:25 -050034 if (expectSuccess != SkToBool(verts)) {
Brian Salomonaff27a22017-02-06 15:47:44 -050035 ERRORF(reporter, "Expected shadow tessellation to %s but it did not.",
Brian Salomon0dda9cb2017-02-03 10:33:25 -050036 expectSuccess ? "succeed" : "fail");
37 }
Brian Salomonaff27a22017-02-06 15:47:44 -050038 verts = SkShadowTessellator::MakeSpot(path, ctm, 1.5f, {0, 0}, kRadius, kUmbraColor,
39 kPenumbraColor, true);
Brian Salomon0dda9cb2017-02-03 10:33:25 -050040 if (expectSuccess != SkToBool(verts)) {
Brian Salomonaff27a22017-02-06 15:47:44 -050041 ERRORF(reporter, "Expected shadow tessellation to %s but it did not.",
Brian Salomon0dda9cb2017-02-03 10:33:25 -050042 expectSuccess ? "succeed" : "fail");
43 }
44}
45
46DEF_TEST(ShadowUtils, reporter) {
47 SkCanvas canvas(100, 100);
Jim Van Vertha84898d2017-02-06 13:38:23 -050048 // Currently SkShadowUtils doesn't really support cubics when compiled without SK_SUPPORT_GPU.
49 // However, this should now not crash.
Brian Salomon0dda9cb2017-02-03 10:33:25 -050050 SkPath path;
51 path.cubicTo(100, 50, 20, 100, 0, 0);
Jim Van Vertha84898d2017-02-06 13:38:23 -050052 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), (bool)SK_SUPPORT_GPU);
Brian Salomon0dda9cb2017-02-03 10:33:25 -050053
54 // This line segment has no area and no shadow.
55 path.reset();
56 path.lineTo(10.f, 10.f);
Jim Van Vertha84898d2017-02-06 13:38:23 -050057 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), false);
Brian Salomon0dda9cb2017-02-03 10:33:25 -050058
59 // A series of colinear line segments
60 path.reset();
61 for (int i = 0; i < 10; ++i) {
62 path.lineTo((SkScalar)i, (SkScalar)i);
63 }
Jim Van Vertha84898d2017-02-06 13:38:23 -050064 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), false);
Brian Salomon0dda9cb2017-02-03 10:33:25 -050065}