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