blob: 56990146f019b7320978cc20a9d8a51a9e646205 [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; };
Mike Klein98668172017-04-13 12:20:21 -040021
Jim Van Verthb4366552017-03-27 14:25:29 -040022 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);
Mike Klein98668172017-04-13 12:20:21 -040048
Brian Salomon0dda9cb2017-02-03 10:33:25 -050049 SkPath path;
50 path.cubicTo(100, 50, 20, 100, 0, 0);
Mike Klein98668172017-04-13 12:20:21 -040051 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), true);
Brian Salomon0dda9cb2017-02-03 10:33:25 -050052
53 // This line segment has no area and no shadow.
54 path.reset();
55 path.lineTo(10.f, 10.f);
Jim Van Vertha84898d2017-02-06 13:38:23 -050056 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), false);
Brian Salomon0dda9cb2017-02-03 10:33:25 -050057
58 // A series of colinear line segments
59 path.reset();
60 for (int i = 0; i < 10; ++i) {
61 path.lineTo((SkScalar)i, (SkScalar)i);
62 }
Jim Van Vertha84898d2017-02-06 13:38:23 -050063 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), false);
Brian Salomon0dda9cb2017-02-03 10:33:25 -050064}