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