Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkPath.h" |
| 10 | #include "include/core/SkVertices.h" |
| 11 | #include "include/utils/SkShadowUtils.h" |
| 12 | #include "src/core/SkDrawShadowInfo.h" |
Mike Reed | ba96256 | 2020-03-12 20:33:21 -0400 | [diff] [blame] | 13 | #include "src/core/SkVerticesPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/utils/SkShadowTessellator.h" |
| 15 | #include "tests/Test.h" |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 16 | |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 17 | enum ExpectVerts { |
| 18 | kDont_ExpectVerts, |
| 19 | kDo_ExpectVerts |
| 20 | }; |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 21 | |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 22 | void check_result(skiatest::Reporter* reporter, sk_sp<SkVertices> verts, |
| 23 | ExpectVerts expectVerts, bool expectSuccess) { |
| 24 | if (expectSuccess != SkToBool(verts)) { |
| 25 | ERRORF(reporter, "Expected shadow tessellation to %s but it did not.", |
| 26 | expectSuccess ? "succeed" : "fail"); |
| 27 | } |
| 28 | if (SkToBool(verts)) { |
Brian Osman | 8cbedf9 | 2020-03-31 10:38:31 -0400 | [diff] [blame] | 29 | if (kDont_ExpectVerts == expectVerts && verts->priv().vertexCount()) { |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 30 | ERRORF(reporter, "Expected shadow tessellation to generate no vertices but it did."); |
Brian Osman | 8cbedf9 | 2020-03-31 10:38:31 -0400 | [diff] [blame] | 31 | } else if (kDo_ExpectVerts == expectVerts && !verts->priv().vertexCount()) { |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 32 | ERRORF(reporter, "Expected shadow tessellation to generate vertices but it didn't."); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | void tessellate_shadow(skiatest::Reporter* reporter, const SkPath& path, const SkMatrix& ctm, |
| 38 | const SkPoint3& heightParams, ExpectVerts expectVerts, bool expectSuccess) { |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 39 | |
| 40 | auto verts = SkShadowTessellator::MakeAmbient(path, ctm, heightParams, true); |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 41 | check_result(reporter, verts, expectVerts, expectSuccess); |
| 42 | |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 43 | verts = SkShadowTessellator::MakeAmbient(path, ctm, heightParams, false); |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 44 | check_result(reporter, verts, expectVerts, expectSuccess); |
| 45 | |
Jim Van Verth | a5ef397 | 2019-05-01 13:28:07 -0400 | [diff] [blame] | 46 | verts = SkShadowTessellator::MakeSpot(path, ctm, heightParams, {0, 0, 128}, 128.f, true); |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 47 | check_result(reporter, verts, expectVerts, expectSuccess); |
| 48 | |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 49 | verts = SkShadowTessellator::MakeSpot(path, ctm, heightParams, {0, 0, 128}, 128.f, false); |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 50 | check_result(reporter, verts, expectVerts, expectSuccess); |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | DEF_TEST(ShadowUtils, reporter) { |
| 54 | SkCanvas canvas(100, 100); |
| 55 | |
| 56 | SkPath path; |
| 57 | path.cubicTo(100, 50, 20, 100, 0, 0); |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 58 | tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 4}, kDo_ExpectVerts, true); |
| 59 | // super high path |
| 60 | tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 4.0e+37f}, |
| 61 | kDo_ExpectVerts, true); |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 62 | |
| 63 | // This line segment has no area and no shadow. |
| 64 | path.reset(); |
| 65 | path.lineTo(10.f, 10.f); |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 66 | tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 4}, kDont_ExpectVerts, true); |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 67 | |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 68 | // A series of collinear line segments |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 69 | path.reset(); |
| 70 | for (int i = 0; i < 10; ++i) { |
| 71 | path.lineTo((SkScalar)i, (SkScalar)i); |
| 72 | } |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 73 | tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 4}, kDont_ExpectVerts, true); |
| 74 | |
| 75 | // ugly degenerate path |
| 76 | path.reset(); |
| 77 | path.moveTo(-134217728, 2.22265153e+21f); |
| 78 | path.cubicTo(-2.33326106e+21f, 7.36298265e-41f, 3.72237738e-22f, 5.99502692e-36f, |
| 79 | 1.13631943e+22f, 2.0890786e+33f); |
| 80 | path.cubicTo(1.03397626e-25f, 5.99502692e-36f, 9.18354962e-41f, 0, 4.6142745e-37f, -213558848); |
| 81 | path.lineTo(-134217728, 2.2226515e+21f); |
| 82 | tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 9}, kDont_ExpectVerts, true); |
| 83 | |
| 84 | // simple concave path (star of David) |
| 85 | path.reset(); |
| 86 | path.moveTo(0.0f, -50.0f); |
| 87 | path.lineTo(14.43f, -25.0f); |
| 88 | path.lineTo(43.30f, -25.0f); |
| 89 | path.lineTo(28.86f, 0.0f); |
| 90 | path.lineTo(43.30f, 25.0f); |
| 91 | path.lineTo(14.43f, 25.0f); |
| 92 | path.lineTo(0.0f, 50.0f); |
| 93 | path.lineTo(-14.43f, 25.0f); |
| 94 | path.lineTo(-43.30f, 25.0f); |
| 95 | path.lineTo(-28.86f, 0.0f); |
| 96 | path.lineTo(-43.30f, -25.0f); |
| 97 | path.lineTo(-14.43f, -25.0f); |
| 98 | // uncomment when transparent concave shadows are working |
| 99 | // tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 9}, kDo_ExpectVerts, true); |
| 100 | |
| 101 | // complex concave path (bowtie) |
| 102 | path.reset(); |
| 103 | path.moveTo(-50, -50); |
| 104 | path.lineTo(-50, 50); |
| 105 | path.lineTo(50, -50); |
| 106 | path.lineTo(50, 50); |
| 107 | path.lineTo(-50, -50); |
| 108 | tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 9}, kDont_ExpectVerts, false); |
| 109 | |
| 110 | // multiple contour path |
| 111 | path.close(); |
| 112 | path.moveTo(0, 0); |
| 113 | path.lineTo(1, 0); |
| 114 | path.lineTo(0, 1); |
| 115 | tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 9}, kDont_ExpectVerts, false); |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void check_xformed_bounds(skiatest::Reporter* reporter, const SkPath& path, const SkMatrix& ctm) { |
| 119 | const SkDrawShadowRec rec = { |
| 120 | SkPoint3::Make(0, 0, 4), |
| 121 | SkPoint3::Make(100, 0, 600), |
| 122 | 800.f, |
Jim Van Verth | b1b80f7 | 2018-01-18 15:19:13 -0500 | [diff] [blame] | 123 | 0x08000000, |
| 124 | 0x40000000, |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 125 | 0 |
| 126 | }; |
| 127 | SkRect bounds; |
| 128 | SkDrawShadowMetrics::GetLocalBounds(path, rec, ctm, &bounds); |
| 129 | ctm.mapRect(&bounds); |
| 130 | |
| 131 | auto verts = SkShadowTessellator::MakeAmbient(path, ctm, rec.fZPlaneParams, true); |
| 132 | if (verts) { |
| 133 | REPORTER_ASSERT(reporter, bounds.contains(verts->bounds())); |
| 134 | } |
| 135 | |
| 136 | SkPoint mapXY = ctm.mapXY(rec.fLightPos.fX, rec.fLightPos.fY); |
| 137 | SkPoint3 devLightPos = SkPoint3::Make(mapXY.fX, mapXY.fY, rec.fLightPos.fZ); |
| 138 | verts = SkShadowTessellator::MakeSpot(path, ctm, rec.fZPlaneParams, devLightPos, |
| 139 | rec.fLightRadius, false); |
| 140 | if (verts) { |
| 141 | REPORTER_ASSERT(reporter, bounds.contains(verts->bounds())); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void check_bounds(skiatest::Reporter* reporter, const SkPath& path) { |
Mike Reed | bb59dfa | 2019-12-12 14:48:28 -0500 | [diff] [blame] | 146 | const bool fixed_shadows_in_perspective = false; // skbug.com/9698 |
| 147 | |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 148 | SkMatrix ctm; |
| 149 | ctm.setTranslate(100, 100); |
| 150 | check_xformed_bounds(reporter, path, ctm); |
| 151 | ctm.postScale(2, 2); |
| 152 | check_xformed_bounds(reporter, path, ctm); |
| 153 | ctm.preRotate(45); |
| 154 | check_xformed_bounds(reporter, path, ctm); |
| 155 | ctm.preSkew(40, -20); |
| 156 | check_xformed_bounds(reporter, path, ctm); |
Mike Reed | bb59dfa | 2019-12-12 14:48:28 -0500 | [diff] [blame] | 157 | if (fixed_shadows_in_perspective) { |
| 158 | ctm[SkMatrix::kMPersp0] = 0.0001f; |
| 159 | ctm[SkMatrix::kMPersp1] = 12.f; |
| 160 | check_xformed_bounds(reporter, path, ctm); |
| 161 | ctm[SkMatrix::kMPersp0] = 0.0001f; |
| 162 | ctm[SkMatrix::kMPersp1] = -12.f; |
| 163 | check_xformed_bounds(reporter, path, ctm); |
| 164 | ctm[SkMatrix::kMPersp0] = 12.f; |
| 165 | ctm[SkMatrix::kMPersp1] = 0.0001f; |
| 166 | check_xformed_bounds(reporter, path, ctm); |
| 167 | } |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | DEF_TEST(ShadowBounds, reporter) { |
| 171 | SkPath path; |
| 172 | path.addRRect(SkRRect::MakeRectXY(SkRect::MakeLTRB(-50, -20, 40, 30), 4, 4)); |
| 173 | check_bounds(reporter, path); |
| 174 | |
| 175 | path.reset(); |
| 176 | path.addOval(SkRect::MakeLTRB(300, 300, 900, 900)); |
| 177 | check_bounds(reporter, path); |
| 178 | |
| 179 | path.reset(); |
| 180 | path.cubicTo(100, 50, 20, 100, 0, 0); |
| 181 | check_bounds(reporter, path); |
| 182 | } |