Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [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 | |
| 8 | #include "SkCanvas.h" |
| 9 | #include "SkPath.h" |
| 10 | #include "SkShadowTessellator.h" |
| 11 | #include "SkShadowUtils.h" |
| 12 | #include "Test.h" |
| 13 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame^] | 14 | void tessellate_shadow(skiatest::Reporter* reporter, const SkPath& path, const SkMatrix& ctm, |
| 15 | bool expectSuccess) { |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 16 | static constexpr SkScalar kRadius = 2.f; |
| 17 | static constexpr SkColor kUmbraColor = 0xFFFFFFFF; |
| 18 | static constexpr SkColor kPenumbraColor = 0x20202020; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame^] | 19 | auto verts = SkShadowVertices::MakeAmbient(path, ctm, kRadius, kUmbraColor, kPenumbraColor, |
| 20 | true); |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 21 | if (expectSuccess != SkToBool(verts)) { |
| 22 | ERRORF(reporter, "Expected shadow tessellation to % but it did not.", |
| 23 | expectSuccess ? "succeed" : "fail"); |
| 24 | } |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame^] | 25 | verts = SkShadowVertices::MakeAmbient(path, ctm, kRadius, kUmbraColor, kPenumbraColor, false); |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 26 | if (expectSuccess != SkToBool(verts)) { |
| 27 | ERRORF(reporter, "Expected shadow tessellation to % but it did not.", |
| 28 | expectSuccess ? "succeed" : "fail"); |
| 29 | } |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame^] | 30 | verts = SkShadowVertices::MakeSpot(path, ctm, 1.5f, {0, 0}, kRadius, kUmbraColor, |
| 31 | kPenumbraColor, false); |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 32 | if (expectSuccess != SkToBool(verts)) { |
| 33 | ERRORF(reporter, "Expected shadow tessellation to % but it did not.", |
| 34 | expectSuccess ? "succeed" : "fail"); |
| 35 | } |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame^] | 36 | verts = SkShadowVertices::MakeSpot(path, ctm, 1.5f, {0, 0}, kRadius, kUmbraColor, |
| 37 | kPenumbraColor, true); |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 38 | if (expectSuccess != SkToBool(verts)) { |
| 39 | ERRORF(reporter, "Expected shadow tessellation to % but it did not.", |
| 40 | expectSuccess ? "succeed" : "fail"); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | DEF_TEST(ShadowUtils, reporter) { |
| 45 | SkCanvas canvas(100, 100); |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame^] | 46 | // Currently SkShadowUtils doesn't really support cubics when compiled without SK_SUPPORT_GPU. |
| 47 | // However, this should now not crash. |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 48 | SkPath path; |
| 49 | path.cubicTo(100, 50, 20, 100, 0, 0); |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame^] | 50 | tessellate_shadow(reporter, path, canvas.getTotalMatrix(), (bool)SK_SUPPORT_GPU); |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 51 | |
| 52 | // This line segment has no area and no shadow. |
| 53 | path.reset(); |
| 54 | path.lineTo(10.f, 10.f); |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame^] | 55 | tessellate_shadow(reporter, path, canvas.getTotalMatrix(), false); |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 56 | |
| 57 | // A series of colinear line segments |
| 58 | path.reset(); |
| 59 | for (int i = 0; i < 10; ++i) { |
| 60 | path.lineTo((SkScalar)i, (SkScalar)i); |
| 61 | } |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame^] | 62 | tessellate_shadow(reporter, path, canvas.getTotalMatrix(), false); |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 63 | } |