blob: e2aa1d117e19777f38a038dba11009740a408611 [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) {
Jim Van Verthb4366552017-03-27 14:25:29 -040017 static constexpr SkScalar kAmbientAlpha = 0.25f;
18 static constexpr SkScalar kSpotAlpha = 0.25f;
19
20 auto heightFunc = [] (SkScalar, SkScalar) { return 4; };
21
22 auto verts = SkShadowTessellator::MakeAmbient(path, ctm, heightFunc, kAmbientAlpha, true);
Brian Salomon0dda9cb2017-02-03 10:33:25 -050023 if (expectSuccess != SkToBool(verts)) {
Brian Salomonaff27a22017-02-06 15:47:44 -050024 ERRORF(reporter, "Expected shadow tessellation to %s but it did not.",
Brian Salomon0dda9cb2017-02-03 10:33:25 -050025 expectSuccess ? "succeed" : "fail");
26 }
Jim Van Verthb4366552017-03-27 14:25:29 -040027 verts = SkShadowTessellator::MakeAmbient(path, ctm, heightFunc, kAmbientAlpha, 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 }
Jim Van Verthb4366552017-03-27 14:25:29 -040032 verts = SkShadowTessellator::MakeSpot(path, ctm, heightFunc, {0, 0, 128}, 128.f,
33 kSpotAlpha, 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 }
Jim Van Verthb4366552017-03-27 14:25:29 -040038 verts = SkShadowTessellator::MakeSpot(path, ctm, heightFunc, {0, 0, 128}, 128.f,
39 kSpotAlpha, false);
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}