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