• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "SkVertices.h"
13 #include "Test.h"
14 
tessellate_shadow(skiatest::Reporter * reporter,const SkPath & path,const SkMatrix & ctm,bool expectSuccess)15 void tessellate_shadow(skiatest::Reporter* reporter, const SkPath& path, const SkMatrix& ctm,
16                        bool expectSuccess) {
17     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);
23     if (expectSuccess != SkToBool(verts)) {
24         ERRORF(reporter, "Expected shadow tessellation to %s but it did not.",
25                expectSuccess ? "succeed" : "fail");
26     }
27     verts = SkShadowTessellator::MakeAmbient(path, ctm, heightFunc, kAmbientAlpha, false);
28     if (expectSuccess != SkToBool(verts)) {
29         ERRORF(reporter, "Expected shadow tessellation to %s but it did not.",
30                expectSuccess ? "succeed" : "fail");
31     }
32     verts = SkShadowTessellator::MakeSpot(path, ctm, heightFunc, {0, 0, 128}, 128.f,
33                                           kSpotAlpha, false);
34     if (expectSuccess != SkToBool(verts)) {
35         ERRORF(reporter, "Expected shadow tessellation to %s but it did not.",
36                expectSuccess ? "succeed" : "fail");
37     }
38     verts = SkShadowTessellator::MakeSpot(path, ctm, heightFunc, {0, 0, 128}, 128.f,
39                                           kSpotAlpha, false);
40     if (expectSuccess != SkToBool(verts)) {
41         ERRORF(reporter, "Expected shadow tessellation to %s but it did not.",
42                expectSuccess ? "succeed" : "fail");
43     }
44 }
45 
DEF_TEST(ShadowUtils,reporter)46 DEF_TEST(ShadowUtils, reporter) {
47     SkCanvas canvas(100, 100);
48     // Currently SkShadowUtils doesn't really support cubics when compiled without SK_SUPPORT_GPU.
49     // However, this should now not crash.
50     SkPath path;
51     path.cubicTo(100, 50, 20, 100, 0, 0);
52     tessellate_shadow(reporter, path, canvas.getTotalMatrix(), (bool)SK_SUPPORT_GPU);
53 
54     // This line segment has no area and no shadow.
55     path.reset();
56     path.lineTo(10.f, 10.f);
57     tessellate_shadow(reporter, path, canvas.getTotalMatrix(), false);
58 
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     }
64     tessellate_shadow(reporter, path, canvas.getTotalMatrix(), false);
65 }
66