• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
clip(const SkIRect & clipRect)11 bool 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 = SkScalarPin(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 = SkScalarPin(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 = SkScalarPin(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 = SkScalarPin(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 = SkScalarPin(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 = SkScalarPin(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 = SkScalarPin(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 = SkScalarPin(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 = SkScalarPin(Get_x45(l,b), l45, r45);
91     t45 = SkScalarPin(Get_y45(l,t), t45, b45);
92     r45 = SkScalarPin(Get_x45(r,t), l45, r45);
93     b45 = SkScalarPin(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)
validateBoundsAreTight() const118 void 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 
validateBoundsAreTight(const std::function<void (bool cond,const char * file,int line,const char * code)> & validateFn) const124 void GrOctoBounds::validateBoundsAreTight(const std::function<void(
125         bool cond, const char* file, int line, const char* code)>& validateFn) const {
126     constexpr static float epsilon = 1e-3f;
127 
128     float l=fBounds.left(), l45=fBounds45.left();
129     float t=fBounds.top(), t45=fBounds45.top();
130     float r=fBounds.right(), r45=fBounds45.right();
131     float b=fBounds.bottom(), b45=fBounds45.bottom();
132 
133 #define VALIDATE(CODE) validateFn(CODE, __FILE__, __LINE__, #CODE)
134     // Verify diagonals are inside far corners of the dev bounds.
135     VALIDATE(l45 >= Get_x45(l,b) - epsilon);
136     VALIDATE(t45 >= Get_y45(l,t) - epsilon);
137     VALIDATE(r45 <= Get_x45(r,t) + epsilon);
138     VALIDATE(b45 <= Get_y45(r,b) + epsilon);
139     // Verify verticals and horizontals are inside far corners of the 45-degree dev bounds.
140     VALIDATE(l >= Get_x(l45,t45) - epsilon);
141     VALIDATE(t >= Get_y(r45,t45) - epsilon);
142     VALIDATE(r <= Get_x(r45,b45) + epsilon);
143     VALIDATE(b <= Get_y(l45,b45) + epsilon);
144     // Verify diagonals are outside middle corners of the dev bounds.
145     VALIDATE(l45 <= Get_x45(r,b) + epsilon);
146     VALIDATE(l45 <= Get_x45(l,t) + epsilon);
147     VALIDATE(t45 <= Get_y45(l,b) + epsilon);
148     VALIDATE(t45 <= Get_y45(r,t) + epsilon);
149     VALIDATE(r45 >= Get_x45(l,t) - epsilon);
150     VALIDATE(r45 >= Get_x45(r,b) - epsilon);
151     VALIDATE(b45 >= Get_y45(r,t) - epsilon);
152     VALIDATE(b45 >= Get_y45(l,b) - epsilon);
153     // Verify verticals and horizontals are outside middle corners of the 45-degree dev bounds.
154     VALIDATE(l <= Get_x(l45,b45) + epsilon);
155     VALIDATE(l <= Get_x(r45,t45) + epsilon);
156     VALIDATE(t <= Get_y(r45,b45) + epsilon);
157     VALIDATE(t <= Get_y(l45,t45) + epsilon);
158     VALIDATE(r >= Get_x(r45,t45) - epsilon);
159     VALIDATE(r >= Get_x(l45,b45) - epsilon);
160     VALIDATE(b >= Get_y(l45,t45) - epsilon);
161     VALIDATE(b >= Get_y(r45,b45) - epsilon);
162 #undef VALIDATE
163 }
164 #endif
165