blob: f7d98608a28cb696b17a1615116c11a76d186dc5 [file] [log] [blame]
Michael Ludwig8f685082018-09-12 15:23:01 -04001/*
2 * Copyright 2018 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// Equivalent to SkTwoPointConicalGradient::Type
9enum class Type {
10 kRadial, kStrip, kFocal
11};
12
13in half4x4 gradientMatrix;
14
15layout(key) in Type type;
16layout(key) in bool isRadiusIncreasing;
17
18// Focal-specific optimizations
19layout(key) in bool isFocalOnCircle;
20layout(key) in bool isWellBehaved;
21layout(key) in bool isSwapped;
22layout(key) in bool isNativelyFocal;
23
24// focalParams is interpreted differently depending on if type is focal or degenerate when
25// degenerate, focalParams = (r0, r0^2), so strips will use .y and kRadial will use .x when focal,
26// focalParams = (1/r1, focalX = r0/(r0-r1)) The correct parameters are calculated once in Make for
27// each FP
28layout(tracked) in uniform half2 focalParams;
29
30@coordTransform {
31 gradientMatrix
32}
33
34void main() {
35 half2 p = sk_TransformedCoords2D[0];
36 half t = -1;
37 half v = 1; // validation flag, set to negative to discard fragment later
38
39 @switch(type) {
40 case Type::kStrip: {
41 half r0_2 = focalParams.y;
42 t = r0_2 - p.y * p.y;
43 if (t >= 0) {
44 t = p.x + sqrt(t);
45 } else {
46 v = -1;
47 }
48 }
49 break;
50 case Type::kRadial: {
51 half r0 = focalParams.x;
52 @if(isRadiusIncreasing) {
53 t = length(p) - r0;
54 } else {
55 t = -length(p) - r0;
56 }
57 }
58 break;
59 case Type::kFocal: {
60 half invR1 = focalParams.x;
61 half fx = focalParams.y;
62
63 half x_t = -1;
64 @if (isFocalOnCircle) {
65 x_t = dot(p, p) / p.x;
66 } else if (isWellBehaved) {
67 x_t = length(p) - p.x * invR1;
68 } else {
69 half temp = p.x * p.x - p.y * p.y;
70
71 // Only do sqrt if temp >= 0; this is significantly slower than checking temp >= 0
72 // in the if statement that checks r(t) >= 0. But GPU may break if we sqrt a
73 // negative float. (Although I havevn't observed that on any devices so far, and the
74 // old approach also does sqrt negative value without a check.) If the performance
75 // is really critical, maybe we should just compute the area where temp and x_t are
76 // always valid and drop all these ifs.
77 if (temp >= 0) {
78 @if(isSwapped || !isRadiusIncreasing) {
79 x_t = -sqrt(temp) - p.x * invR1;
80 } else {
81 x_t = sqrt(temp) - p.x * invR1;
82 }
83 }
84 }
85
86 // The final calculation of t from x_t has lots of static optimizations but only do them
87 // when x_t is positive (which can be assumed true if isWellBehaved is true)
88 @if (!isWellBehaved) {
89 // This will still calculate t even though it will be ignored later in the pipeline
90 // to avoid a branch
91 if (x_t <= 0.0) {
92 v = -1;
93 }
94 }
95 @if (isRadiusIncreasing) {
96 @if (isNativelyFocal) {
97 t = x_t;
98 } else {
99 t = x_t + fx;
100 }
101 } else {
102 @if (isNativelyFocal) {
103 t = -x_t;
104 } else {
105 t = -x_t + fx;
106 }
107 }
108
109 @if(isSwapped) {
110 t = 1 - t;
111 }
112 }
113 break;
114 }
115
116 sk_OutColor = half4(t, v, 0, 0);
117}
118
119//////////////////////////////////////////////////////////////////////////////
120
121@header {
122 #include "SkTwoPointConicalGradient.h"
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400123 #include "GrGradientShader.h"
Michael Ludwig8f685082018-09-12 15:23:01 -0400124}
125
Michael Ludwigb96cba32018-09-14 13:59:24 -0400126// The 2 point conical gradient can reject a pixel so it does change opacity
127// even if the input was opaque, so disable that optimization
128@optimizationFlags {
129 kNone_OptimizationFlags
130}
131
Michael Ludwig8f685082018-09-12 15:23:01 -0400132@make {
133 static std::unique_ptr<GrFragmentProcessor> Make(const SkTwoPointConicalGradient& gradient,
134 const GrFPArgs& args);
135}
136
137@cppEnd {
138 // .fp files do not let you reference outside enum definitions, so we have to explicitly map
139 // between the two compatible enum defs
140 GrTwoPointConicalGradientLayout::Type convert_type(
141 SkTwoPointConicalGradient::Type type) {
142 switch(type) {
143 case SkTwoPointConicalGradient::Type::kRadial:
144 return GrTwoPointConicalGradientLayout::Type::kRadial;
145 case SkTwoPointConicalGradient::Type::kStrip:
146 return GrTwoPointConicalGradientLayout::Type::kStrip;
147 case SkTwoPointConicalGradient::Type::kFocal:
148 return GrTwoPointConicalGradientLayout::Type::kFocal;
149 }
150 SkDEBUGFAIL("Should not be reachable");
151 return GrTwoPointConicalGradientLayout::Type::kRadial;
152 }
153
154 std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::Make(
155 const SkTwoPointConicalGradient& grad, const GrFPArgs& args) {
156 GrTwoPointConicalGradientLayout::Type grType = convert_type(grad.getType());
157
158 // The focalData struct is only valid if isFocal is true
159 const SkTwoPointConicalGradient::FocalData& focalData = grad.getFocalData();
160 bool isFocal = grType == Type::kFocal;
161
162 // Calculate optimization switches from gradient specification
163 bool isFocalOnCircle = isFocal && focalData.isFocalOnCircle();
164 bool isWellBehaved = isFocal && focalData.isWellBehaved();
165 bool isSwapped = isFocal && focalData.isSwapped();
166 bool isNativelyFocal = isFocal && focalData.isNativelyFocal();
167
168 // Type-specific calculations: isRadiusIncreasing, focalParams, and the gradient matrix.
169 // However, all types start with the total inverse local matrix calculated from the shader
170 // and args
171 bool isRadiusIncreasing;
172 SkPoint focalParams; // really just a 2D tuple
173 SkMatrix matrix;
174
175 // Initialize the base matrix
176 if (!grad.totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix)->invert(&matrix)) {
177 return nullptr;
178 }
179
180 if (isFocal) {
181 isRadiusIncreasing = (1 - focalData.fFocalX) > 0;
182
183 focalParams.set(1.0 / focalData.fR1, focalData.fFocalX);
184
185 matrix.postConcat(grad.getGradientMatrix());
186 } else if (grType == Type::kRadial) {
187 SkScalar dr = grad.getDiffRadius();
188 isRadiusIncreasing = dr >= 0;
189
190 SkScalar r0 = grad.getStartRadius() / dr;
191 focalParams.set(r0, r0 * r0);
192
193
194 // GPU radial matrix is different from the original matrix, since we map the diff radius
195 // to have |dr| = 1, so manually compute the final gradient matrix here.
196
197 // Map center to (0, 0)
198 matrix.postTranslate(-grad.getStartCenter().fX, -grad.getStartCenter().fY);
199
200 // scale |diffRadius| to 1
201 matrix.postScale(1 / dr, 1 / dr);
202 } else { // kStrip
203 isRadiusIncreasing = false; // kStrip doesn't use this flag
204
205 SkScalar r0 = grad.getStartRadius() / grad.getCenterX1();
206 focalParams.set(r0, r0 * r0);
207
208
209 matrix.postConcat(grad.getGradientMatrix());
210 }
211
212
213 return std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(
214 matrix, grType, isRadiusIncreasing, isFocalOnCircle, isWellBehaved,
215 isSwapped, isNativelyFocal, focalParams));
216 }
217}
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400218
219//////////////////////////////////////////////////////////////////////////////
220
221@test(d) {
222 SkPoint center1 = {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()};
223 SkPoint center2 = {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()};
224 SkScalar radius1 = d->fRandom->nextUScalar1();
225 SkScalar radius2 = d->fRandom->nextUScalar1();
226
227 constexpr int kTestTypeMask = (1 << 2) - 1,
228 kTestNativelyFocalBit = (1 << 2),
229 kTestFocalOnCircleBit = (1 << 3),
230 kTestSwappedBit = (1 << 4);
231 // We won't treat isWellDefined and isRadiusIncreasing specially because they
232 // should have high probability to be turned on and off as we're getting random
233 // radii and centers.
234
235 int mask = d->fRandom->nextU();
236 int type = mask & kTestTypeMask;
237 if (type == static_cast<int>(Type::kRadial)) {
238 center2 = center1;
239 // Make sure that the radii are different
240 if (SkScalarNearlyZero(radius1 - radius2)) {
241 radius2 += .1f;
242 }
243 } else if (type == static_cast<int>(Type::kStrip)) {
244 radius1 = SkTMax(radius1, .1f); // Make sure that the radius is non-zero
245 radius2 = radius1;
246 // Make sure that the centers are different
247 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
248 center2.fX += .1f;
249 }
250 } else { // kFocal_Type
251 // Make sure that the centers are different
252 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
253 center2.fX += .1f;
254 }
255
256 if (kTestNativelyFocalBit & mask) {
257 radius1 = 0;
258 }
259 if (kTestFocalOnCircleBit & mask) {
260 radius2 = radius1 + SkPoint::Distance(center1, center2);
261 }
262 if (kTestSwappedBit & mask) {
263 std::swap(radius1, radius2);
264 radius2 = 0;
265 }
266
267 // Make sure that the radii are different
268 if (SkScalarNearlyZero(radius1 - radius2)) {
269 radius2 += .1f;
270 }
271 }
272
273 if (SkScalarNearlyZero(radius1 - radius2) &&
274 SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
275 radius2 += .1f; // make sure that we're not degenerated
276 }
277
278 GrGradientShader::RandomParams params(d->fRandom);
279 auto shader = params.fUseColors4f ?
280 SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2,
281 params.fColors4f, params.fColorSpace, params.fStops,
282 params.fColorCount, params.fTileMode) :
283 SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2,
284 params.fColors, params.fStops,
285 params.fColorCount, params.fTileMode);
286 GrTest::TestAsFPArgs asFPArgs(d);
287 std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());
288
289 GrAlwaysAssert(fp);
290 return fp;
291}