ShadowTest.cpp 6.3 KB

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