OctoBoundsTest.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright 2019 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/utils/SkRandom.h"
  8. #include "src/gpu/ccpr/GrOctoBounds.h"
  9. #include "tests/Test.h"
  10. using namespace skiatest;
  11. constexpr static float kEpsilon = 1e-3f;
  12. static int numClipsOut = 0;
  13. static int numIntersectClips = 0;
  14. // Ensures devBounds and devBounds45 are valid. Namely, that they are both tight bounding boxes
  15. // around a valid octagon.
  16. static void validate_octo_bounds(
  17. Reporter* reporter, const SkIRect& clipRect, const GrOctoBounds& octoBounds) {
  18. // Verify dev bounds are inside the clip rect.
  19. REPORTER_ASSERT(reporter, octoBounds.left() >= (float)clipRect.left() - kEpsilon);
  20. REPORTER_ASSERT(reporter, octoBounds.top() >= (float)clipRect.top() - kEpsilon);
  21. REPORTER_ASSERT(reporter, octoBounds.right() <= (float)clipRect.right() + kEpsilon);
  22. REPORTER_ASSERT(reporter, octoBounds.bottom() <= (float)clipRect.bottom() + kEpsilon);
  23. octoBounds.validateBoundsAreTight([reporter](
  24. bool cond, const char* file, int line, const char* code) {
  25. if (!cond) {
  26. reporter->reportFailedWithContext(skiatest::Failure(file, line, code, SkString()));
  27. }
  28. });
  29. }
  30. // This is a variant of SkRandom::nextRangeU that can handle negative numbers. As currently written,
  31. // and assuming two's compliment, it would probably work to just call the existing nextRangeU
  32. // implementation with negative value(s), but we go through this method as an extra precaution.
  33. static int next_range_i(SkRandom* rand, int min, int max) {
  34. int u = rand->nextRangeU(0, max - min);
  35. return u + min;
  36. }
  37. static void test_octagon(Reporter* reporter, SkRandom* rand, float l, float t, float r, float b) {
  38. for (int i = 0; i < 20; ++i) {
  39. float minL45 = GrOctoBounds::Get_x45(l,b);
  40. float maxL45 = std::min(GrOctoBounds::Get_x45(r,b), GrOctoBounds::Get_x45(l,t));
  41. float minT45 = GrOctoBounds::Get_y45(l,t);
  42. float maxT45 = std::min(GrOctoBounds::Get_y45(l,b), GrOctoBounds::Get_y45(r,t));
  43. float minR45 = std::max(GrOctoBounds::Get_x45(l,t), GrOctoBounds::Get_x45(r,b));
  44. float maxR45 = GrOctoBounds::Get_x45(r,t);
  45. float minB45 = std::max(GrOctoBounds::Get_y45(r,t), GrOctoBounds::Get_y45(l,b));
  46. float maxB45 = GrOctoBounds::Get_y45(r,b);
  47. // Pick somewhat valid 45 degree bounds.
  48. float l45 = rand->nextRangeF(minL45, maxL45);
  49. float t45 = rand->nextRangeF(minT45, maxT45);
  50. float r45 = rand->nextRangeF(minR45, maxR45);
  51. float b45 = rand->nextRangeF(minB45, maxB45);
  52. // Grow out diagonal corners if too tight, making 45 bounds valid.
  53. std::function<void()> growOutDiagonals[4] = {
  54. [&]() { // Push top-left diagonal corner outside left edge.
  55. float miss = GrOctoBounds::Get_x(l45,t45) - l;
  56. if (miss > 0) {
  57. // x = (x45 + y45)/2
  58. l45 -= miss;
  59. if (l45 < minL45) {
  60. t45 -= minL45 - l45;
  61. l45 = minL45;
  62. }
  63. t45 -= miss;
  64. if (t45 < minT45) {
  65. l45 -= minT45 - t45;
  66. t45 = minT45;
  67. }
  68. }
  69. },
  70. [&]() { // Push top-right diagonal corner outside top edge.
  71. float miss = GrOctoBounds::Get_y(r45,t45) - t;
  72. if (miss > 0) {
  73. // y = (y45 - x45)/2
  74. r45 += miss;
  75. if (r45 > maxR45) {
  76. t45 -= r45 - maxR45;
  77. r45 = maxR45;
  78. }
  79. t45 -= miss;
  80. if (t45 < minT45) {
  81. r45 += minT45 - t45;
  82. t45 = minT45;
  83. }
  84. }
  85. },
  86. [&]() { // Push bottom-right diagonal corner outside right edge.
  87. float miss = r - GrOctoBounds::Get_x(r45,b45);
  88. if (miss > 0) {
  89. // x = (x45 + y45)/2
  90. r45 += miss;
  91. if (r45 > maxR45) {
  92. b45 += r45 - maxR45;
  93. r45 = maxR45;
  94. }
  95. b45 += miss;
  96. if (b45 > maxB45) {
  97. r45 += b45 - maxB45;
  98. b45 = maxB45;
  99. }
  100. }
  101. },
  102. [&]() { // Push bottom-left diagonal corner outside bottom edge.
  103. float miss = b - GrOctoBounds::Get_y(l45,b45);
  104. if (miss > 0) {
  105. // y = (y45 - x45)/2
  106. l45 -= miss;
  107. if (l45 < minL45) {
  108. b45 += minL45 - l45;
  109. l45 = minL45;
  110. }
  111. b45 += miss;
  112. if (b45 > maxB45) {
  113. l45 -= b45 - maxB45;
  114. b45 = maxB45;
  115. }
  116. }
  117. },
  118. };
  119. // Shuffle.
  120. for (int i = 0; i < 10; ++i) {
  121. std::swap(growOutDiagonals[rand->nextRangeU(0, 3)],
  122. growOutDiagonals[rand->nextRangeU(0, 3)]);
  123. }
  124. for (const auto& f : growOutDiagonals) {
  125. f();
  126. }
  127. GrOctoBounds octoBounds(SkRect::MakeLTRB(l,t,r,b), SkRect::MakeLTRB(l45,t45,r45,b45));
  128. SkIRect devIBounds;
  129. octoBounds.roundOut(&devIBounds);
  130. // Test a clip rect that completely encloses the octagon.
  131. bool clipSuccess = octoBounds.clip(devIBounds);
  132. REPORTER_ASSERT(reporter, clipSuccess);
  133. // Should not have clipped anything.
  134. REPORTER_ASSERT(reporter, octoBounds == GrOctoBounds({l,t,r,b}, {l45,t45,r45,b45}));
  135. validate_octo_bounds(reporter, devIBounds, octoBounds);
  136. // Test a bunch of random clip rects.
  137. for (int j = 0; j < 20; ++j) {
  138. SkIRect clipRect;
  139. do {
  140. clipRect.fLeft = next_range_i(rand, devIBounds.left(), devIBounds.right() - 1);
  141. clipRect.fTop = next_range_i(rand, devIBounds.top(), devIBounds.bottom() - 1);
  142. clipRect.fRight = next_range_i(rand, clipRect.left() + 1, devIBounds.right());
  143. clipRect.fBottom = next_range_i(rand, clipRect.top() + 1, devIBounds.bottom());
  144. } while (clipRect == devIBounds);
  145. GrOctoBounds octoBoundsClipped = octoBounds;
  146. if (!octoBoundsClipped.clip(clipRect)) {
  147. // Ensure clipRect is completely outside one of the diagonals.
  148. float il = (float)clipRect.left();
  149. float it = (float)clipRect.top();
  150. float ir = (float)clipRect.right();
  151. float ib = (float)clipRect.bottom();
  152. REPORTER_ASSERT(reporter,
  153. GrOctoBounds::Get_x45(ir,it) <= l45 + kEpsilon ||
  154. GrOctoBounds::Get_y45(ir,ib) <= t45 + kEpsilon ||
  155. GrOctoBounds::Get_x45(il,ib) >= r45 - kEpsilon ||
  156. GrOctoBounds::Get_y45(il,it) >= b45 - kEpsilon);
  157. ++numClipsOut;
  158. } else {
  159. validate_octo_bounds(reporter, clipRect, octoBoundsClipped);
  160. ++numIntersectClips;
  161. }
  162. }
  163. }
  164. }
  165. DEF_TEST(OctoBounds, reporter) {
  166. numClipsOut = 0;
  167. numIntersectClips = 0;
  168. SkRandom rand;
  169. test_octagon(reporter, &rand, 0, 0, 100, 100);
  170. test_octagon(reporter, &rand, -2, 0, 2, 100);
  171. test_octagon(reporter, &rand, 0, -10, 100, 0);
  172. // We can't test Infs or NaNs because they trigger internal asserts when setting GrOctoBounds.
  173. // Sanity check on our random clip testing.. Just make we hit both types of clip.
  174. REPORTER_ASSERT(reporter, numClipsOut > 0);
  175. REPORTER_ASSERT(reporter, numIntersectClips > 0);
  176. }