GrOctoBounds.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "src/gpu/ccpr/GrOctoBounds.h"
  8. #include <algorithm>
  9. bool GrOctoBounds::clip(const SkIRect& clipRect) {
  10. // Intersect dev bounds with the clip rect.
  11. float l = std::max(fBounds.left(), (float)clipRect.left());
  12. float t = std::max(fBounds.top(), (float)clipRect.top());
  13. float r = std::min(fBounds.right(), (float)clipRect.right());
  14. float b = std::min(fBounds.bottom(), (float)clipRect.bottom());
  15. float l45 = fBounds45.left();
  16. float t45 = fBounds45.top();
  17. float r45 = fBounds45.right();
  18. float b45 = fBounds45.bottom();
  19. // Check if either the bounds or 45-degree bounds are empty. We write this check as the NOT of
  20. // non-empty rects, so we will return false if any values are NaN.
  21. if (!(l < r && t < b && l45 < r45 && t45 < b45)) {
  22. return false;
  23. }
  24. // Tighten dev bounds around the new (octagonal) intersection that results after clipping. This
  25. // may be tighter now even than the clipped bounds, depending on the diagonals. Shader code that
  26. // emits octagons expects both bounding boxes to circumcribe the inner octagon, and will fail if
  27. // they do not.
  28. if (l45 > Get_x45(r,b)) {
  29. // Slide the bottom upward until it crosses the l45 diagonal at x=r.
  30. // y = x + (y0 - x0)
  31. // Substitute: l45 = x0 - y0
  32. // y = x - l45
  33. b = SkScalarPin(r - l45, t, b);
  34. } else if (r45 < Get_x45(r,b)) {
  35. // Slide the right side leftward until it crosses the r45 diagonal at y=b.
  36. // x = y + (x0 - y0)
  37. // Substitute: r45 = x0 - y0
  38. // x = y + r45
  39. r = SkScalarPin(b + r45, l, r);
  40. }
  41. if (l45 > Get_x45(l,t)) {
  42. // Slide the left side rightward until it crosses the l45 diagonal at y=t.
  43. // x = y + (x0 - y0)
  44. // Substitute: l45 = x0 - y0
  45. // x = y + l45
  46. l = SkScalarPin(t + l45, l, r);
  47. } else if (r45 < Get_x45(l,t)) {
  48. // Slide the top downward until it crosses the r45 diagonal at x=l.
  49. // y = x + (y0 - x0)
  50. // Substitute: r45 = x0 - y0
  51. // y = x - r45
  52. t = SkScalarPin(l - r45, t, b);
  53. }
  54. if (t45 > Get_y45(l,b)) {
  55. // Slide the left side rightward until it crosses the t45 diagonal at y=b.
  56. // x = -y + (x0 + y0)
  57. // Substitute: t45 = x0 + y0
  58. // x = -y + t45
  59. l = SkScalarPin(t45 - b, l, r);
  60. } else if (b45 < Get_y45(l,b)) {
  61. // Slide the bottom upward until it crosses the b45 diagonal at x=l.
  62. // y = -x + (y0 + x0)
  63. // Substitute: b45 = x0 + y0
  64. // y = -x + b45
  65. b = SkScalarPin(b45 - l, t, b);
  66. }
  67. if (t45 > Get_y45(r,t)) {
  68. // Slide the top downward until it crosses the t45 diagonal at x=r.
  69. // y = -x + (y0 + x0)
  70. // Substitute: t45 = x0 + y0
  71. // y = -x + t45
  72. t = SkScalarPin(t45 - r, t, b);
  73. } else if (b45 < Get_y45(r,t)) {
  74. // Slide the right side leftward until it crosses the b45 diagonal at y=t.
  75. // x = -y + (x0 + y0)
  76. // Substitute: b45 = x0 + y0
  77. // x = -y + b45
  78. r = SkScalarPin(b45 - t, l, r);
  79. }
  80. // Tighten the 45-degree bounding box. Since the dev bounds are now fully tightened, we only
  81. // have to clamp the diagonals to outer corners.
  82. // NOTE: This will not cause l,t,r,b to need more insetting. We only ever change a diagonal by
  83. // pinning it to a FAR corner, which, by definition, is still outside the other corners.
  84. l45 = SkScalarPin(Get_x45(l,b), l45, r45);
  85. t45 = SkScalarPin(Get_y45(l,t), t45, b45);
  86. r45 = SkScalarPin(Get_x45(r,t), l45, r45);
  87. b45 = SkScalarPin(Get_y45(r,b), t45, b45);
  88. // Make one final check for empty or NaN bounds. If the dev bounds were clipped completely
  89. // outside one of the diagonals, they will have been pinned to empty. It's also possible that
  90. // some Infs crept in and turned into NaNs.
  91. if (!(l < r && t < b && l45 < r45 && t45 < b45)) {
  92. return false;
  93. }
  94. fBounds.setLTRB(l, t, r, b);
  95. fBounds45.setLTRB(l45, t45, r45, b45);
  96. #ifdef SK_DEBUG
  97. // Verify dev bounds are inside the clip rect.
  98. SkASSERT(l >= (float)clipRect.left());
  99. SkASSERT(t >= (float)clipRect.top());
  100. SkASSERT(r <= (float)clipRect.right());
  101. SkASSERT(b <= (float)clipRect.bottom());
  102. this->validateBoundsAreTight();
  103. #endif
  104. return true;
  105. }
  106. #if defined(SK_DEBUG) || defined(GR_TEST_UTILS)
  107. void GrOctoBounds::validateBoundsAreTight() const {
  108. this->validateBoundsAreTight([](bool cond, const char* file, int line, const char* code) {
  109. SkASSERTF(cond, "%s(%d): assertion failure: \"assert(%s)\"", file, line, code);
  110. });
  111. }
  112. void GrOctoBounds::validateBoundsAreTight(const std::function<void(
  113. bool cond, const char* file, int line, const char* code)>& validateFn) const {
  114. constexpr static float epsilon = 1e-3f;
  115. float l=fBounds.left(), l45=fBounds45.left();
  116. float t=fBounds.top(), t45=fBounds45.top();
  117. float r=fBounds.right(), r45=fBounds45.right();
  118. float b=fBounds.bottom(), b45=fBounds45.bottom();
  119. #define VALIDATE(CODE) validateFn(CODE, __FILE__, __LINE__, #CODE)
  120. // Verify diagonals are inside far corners of the dev bounds.
  121. VALIDATE(l45 >= Get_x45(l,b) - epsilon);
  122. VALIDATE(t45 >= Get_y45(l,t) - epsilon);
  123. VALIDATE(r45 <= Get_x45(r,t) + epsilon);
  124. VALIDATE(b45 <= Get_y45(r,b) + epsilon);
  125. // Verify verticals and horizontals are inside far corners of the 45-degree dev bounds.
  126. VALIDATE(l >= Get_x(l45,t45) - epsilon);
  127. VALIDATE(t >= Get_y(r45,t45) - epsilon);
  128. VALIDATE(r <= Get_x(r45,b45) + epsilon);
  129. VALIDATE(b <= Get_y(l45,b45) + epsilon);
  130. // Verify diagonals are outside middle corners of the dev bounds.
  131. VALIDATE(l45 <= Get_x45(r,b) + epsilon);
  132. VALIDATE(l45 <= Get_x45(l,t) + epsilon);
  133. VALIDATE(t45 <= Get_y45(l,b) + epsilon);
  134. VALIDATE(t45 <= Get_y45(r,t) + epsilon);
  135. VALIDATE(r45 >= Get_x45(l,t) - epsilon);
  136. VALIDATE(r45 >= Get_x45(r,b) - epsilon);
  137. VALIDATE(b45 >= Get_y45(r,t) - epsilon);
  138. VALIDATE(b45 >= Get_y45(l,b) - epsilon);
  139. // Verify verticals and horizontals are outside middle corners of the 45-degree dev bounds.
  140. VALIDATE(l <= Get_x(l45,b45) + epsilon);
  141. VALIDATE(l <= Get_x(r45,t45) + epsilon);
  142. VALIDATE(t <= Get_y(r45,b45) + epsilon);
  143. VALIDATE(t <= Get_y(l45,t45) + epsilon);
  144. VALIDATE(r >= Get_x(r45,t45) - epsilon);
  145. VALIDATE(r >= Get_x(l45,b45) - epsilon);
  146. VALIDATE(b >= Get_y(l45,t45) - epsilon);
  147. VALIDATE(b >= Get_y(r45,b45) - epsilon);
  148. #undef VALIDATE
  149. }
  150. #endif