blob: 856076f5ce5c2a1db1779c407b507c2e845fc4f8 [file] [log] [blame]
Robert Phillipsa29a9562016-10-20 09:40:55 -04001/*
2 * Copyright 2016 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
Mike Reed80747ef2018-01-23 15:29:32 -05008#include "SkMaskFilterBase.h"
Robert Phillipsa29a9562016-10-20 09:40:55 -04009#include "SkRRectsGaussianEdgeMaskFilter.h"
10#include "SkReadBuffer.h"
Mike Reed242135a2018-02-22 13:41:39 -050011#include "SkRRectPriv.h"
Robert Phillipsa29a9562016-10-20 09:40:55 -040012#include "SkWriteBuffer.h"
13
14#if SK_SUPPORT_GPU
15#include "GrFragmentProcessor.h"
16#endif
17
18 /** \class SkRRectsGaussianEdgeMaskFilterImpl
19 * This mask filter applies a gaussian edge to the intersection of two round rects.
20 * The round rects must have the same radii at each corner and the x&y radii
21 * must also be equal.
22 */
Mike Reed80747ef2018-01-23 15:29:32 -050023class SkRRectsGaussianEdgeMaskFilterImpl : public SkMaskFilterBase {
Robert Phillipsa29a9562016-10-20 09:40:55 -040024public:
25 SkRRectsGaussianEdgeMaskFilterImpl(const SkRRect& first, const SkRRect& second,
26 SkScalar radius)
27 : fFirst(first)
28 , fSecond(second)
29 , fRadius(radius) {
30 }
31
32 SkMask::Format getFormat() const override { return SkMask::kA8_Format; }
33 bool filterMask(SkMask* dst, const SkMask& src, const SkMatrix&,
34 SkIPoint* margin) const override;
35
Robert Phillipsa29a9562016-10-20 09:40:55 -040036 SK_TO_STRING_OVERRIDE()
37 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkRRectsGaussianEdgeMaskFilterImpl)
38
39protected:
40 void flatten(SkWriteBuffer&) const override;
41
Mike Reedbfadcf02018-01-20 22:24:21 +000042#if SK_SUPPORT_GPU
43 std::unique_ptr<GrFragmentProcessor> onAsFragmentProcessor(const GrFPArgs& args) const override;
44 bool onHasFragmentProcessor() const override { return true; }
45#endif
46
Robert Phillipsa29a9562016-10-20 09:40:55 -040047private:
48 SkRRect fFirst;
49 SkRRect fSecond;
50 SkScalar fRadius;
51
52 friend class SkRRectsGaussianEdgeMaskFilter; // for serialization registration system
53
54 typedef SkMaskFilter INHERITED;
55};
56
57// x & y are in device space
58static SkScalar compute_rrect_normalized_dist(const SkRRect& rr, const SkPoint& p, SkScalar rad) {
59 SkASSERT(rr.getType() == SkRRect::kOval_Type || rr.getType() == SkRRect::kRect_Type ||
60 rr.getType() == SkRRect::kSimple_Type);
61 SkASSERT(rad > 0.0f);
62
63 SkVector delta = { SkTAbs(p.fX - rr.rect().centerX()), SkTAbs(p.fY - rr.rect().centerY()) };
64
65 SkScalar halfW = 0.5f * rr.rect().width();
66 SkScalar halfH = 0.5f * rr.rect().height();
67 SkScalar invRad = 1.0f/rad;
68
Mike Reed242135a2018-02-22 13:41:39 -050069 const SkVector& radii = SkRRectPriv::GetSimpleRadii(rr);
Robert Phillipsa29a9562016-10-20 09:40:55 -040070 SkASSERT(SkScalarNearlyEqual(radii.fX, radii.fY));
71
72 switch (rr.getType()) {
73 case SkRRect::kOval_Type: {
74 float scaledDist = delta.length() * invRad;
75 return SkTPin(halfW * invRad - scaledDist, 0.0f, 1.0f);
76 }
77 case SkRRect::kRect_Type: {
78 SkScalar xDist = (halfW - delta.fX) * invRad;
79 SkScalar yDist = (halfH - delta.fY) * invRad;
80
81 SkVector v = { 1.0f - SkTPin(xDist, 0.0f, 1.0f), 1.0f - SkTPin(yDist, 0.0f, 1.0f) };
82 return SkTPin(1.0f - v.length(), 0.0f, 1.0f);
83 }
84 case SkRRect::kSimple_Type: {
85
86 //----------------
87 // ice-cream-cone fractional distance computation
88
89 // When the blurRadius is larger than the corner radius we want to use it to
90 // compute the pointy end of the ice cream cone. If it smaller we just want to use
91 // the center of the corner's circle. When using the blurRadius the inset amount
92 // can't exceed the halfwidths of the RRect.
93 SkScalar insetDist = SkTMin(SkTMax(rad, radii.fX), SkTMin(halfW, halfH));
94
95 // "maxValue" is a correction term for if the blurRadius is larger than the
96 // size of the RRect. In that case we don't want to go all the way to black.
97 SkScalar maxValue = insetDist * invRad;
98
99 SkVector coneBottom = { halfW - insetDist, halfH - insetDist };
100 SkVector ptInConeSpace = delta - coneBottom;
101
102 SkVector cornerTop = { halfW - radii.fX - coneBottom.fX, halfH - coneBottom.fY };
103 SkVector cornerRight = { halfW - coneBottom.fX, halfH - radii.fY - coneBottom.fY };
104
105 SkScalar cross1 = ptInConeSpace.cross(cornerTop);
106 SkScalar cross2 = cornerRight.cross(ptInConeSpace);
107 bool inCone = cross1 > 0.0f && cross2 > 0.0f;
108
109 if (!inCone) {
110 SkScalar xDist = (halfW - delta.fX) * invRad;
111 SkScalar yDist = (halfH - delta.fY) * invRad;
112
113 return SkTPin(SkTMin(xDist, yDist), 0.0f, 1.0f); // perpendicular distance
114 }
115
116 SkVector cornerCenterInConeSpace = { insetDist - radii.fX, insetDist - radii.fY };
117
118 SkVector connectingVec = ptInConeSpace - cornerCenterInConeSpace;
119 float distToPtInConeSpace = SkPoint::Normalize(&ptInConeSpace);
120
121 // "a" (i.e., dot(ptInConeSpace, ptInConeSpace) should always be 1.0f since
122 // ptInConeSpace is now normalized
123 SkScalar b = 2.0f * ptInConeSpace.dot(connectingVec);
124 SkScalar c = connectingVec.dot(connectingVec) - radii.fX * radii.fY;
125
126 // lop off negative values that are outside the cone
127 SkScalar coneDist = SkTMax(0.0f, 0.5f * (-b + SkScalarSqrt(b*b - 4*c)));
128
129 // make the coneDist a fraction of how far it is from the edge to the cone's base
130 coneDist = (maxValue*coneDist) / (coneDist+distToPtInConeSpace);
131 return SkTPin(coneDist, 0.0f, 1.0f);
132 }
133 default:
134 return 0.0f;
135 }
136}
137
138bool SkRRectsGaussianEdgeMaskFilterImpl::filterMask(SkMask* dst, const SkMask& src,
139 const SkMatrix& matrix,
140 SkIPoint* margin) const {
141
142 if (src.fFormat != SkMask::kA8_Format) {
143 return false;
144 }
145
146 if (margin) {
147 margin->set(0, 0);
148 }
149
150 dst->fBounds = src.fBounds;
151 dst->fRowBytes = dst->fBounds.width();
152 dst->fFormat = SkMask::kA8_Format;
153 dst->fImage = nullptr;
154
155 if (src.fImage) {
156 size_t dstSize = dst->computeImageSize();
157 if (0 == dstSize) {
158 return false; // too big to allocate, abort
159 }
160
161 const uint8_t* srcPixels = src.fImage;
162 uint8_t* dstPixels = dst->fImage = SkMask::AllocImage(dstSize);
163
164 SkPoint basePt = { SkIntToScalar(src.fBounds.fLeft), SkIntToScalar(src.fBounds.fTop) };
Robert Phillipsa29a9562016-10-20 09:40:55 -0400165
166 for (int y = 0; y < dst->fBounds.height(); ++y) {
167 const uint8_t* srcRow = srcPixels + y * dst->fRowBytes;
168 uint8_t* dstRow = dstPixels + y*dst->fRowBytes;
169
170 for (int x = 0; x < dst->fBounds.width(); ++x) {
171 SkPoint curPt = { basePt.fX + x, basePt.fY + y };
172
173 SkVector vec;
174 vec.fX = 1.0f - compute_rrect_normalized_dist(fFirst, curPt, fRadius);
175 vec.fY = 1.0f - compute_rrect_normalized_dist(fSecond, curPt, fRadius);
176
177 SkScalar factor = SkTPin(vec.length(), 0.0f, 1.0f);
178 factor = exp(-factor * factor * 4.0f) - 0.018f;
179 SkASSERT(factor >= 0.0f && factor <= 1.0f);
180
181 dstRow[x] = (uint8_t) (factor * srcRow[x]);
182 }
183 }
184 }
185
186 return true;
187}
188
189////////////////////////////////////////////////////////////////////////////
190
191#if SK_SUPPORT_GPU
192
193#include "GrCoordTransform.h"
194#include "GrFragmentProcessor.h"
Robert Phillipsa29a9562016-10-20 09:40:55 -0400195#include "glsl/GrGLSLFragmentProcessor.h"
196#include "glsl/GrGLSLFragmentShaderBuilder.h"
197#include "glsl/GrGLSLProgramDataManager.h"
198#include "glsl/GrGLSLUniformHandler.h"
199#include "SkGr.h"
Robert Phillipsa29a9562016-10-20 09:40:55 -0400200
201class RRectsGaussianEdgeFP : public GrFragmentProcessor {
202public:
203 enum Mode {
204 kCircle_Mode,
205 kRect_Mode,
206 kSimpleCircular_Mode,
207 };
208
Brian Salomonaff329b2017-08-11 09:40:37 -0400209 static std::unique_ptr<GrFragmentProcessor> Make(const SkRRect& first, const SkRRect& second,
210 SkScalar radius) {
211 return std::unique_ptr<GrFragmentProcessor>(
212 new RRectsGaussianEdgeFP(first, second, radius));
Robert Phillipsa29a9562016-10-20 09:40:55 -0400213 }
214
Brian Salomon164f6ec2017-07-25 15:32:17 -0400215 const char* name() const override { return "RRectsGaussianEdgeFP"; }
216
Brian Salomonaff329b2017-08-11 09:40:37 -0400217 std::unique_ptr<GrFragmentProcessor> clone() const override {
218 return std::unique_ptr<GrFragmentProcessor>(new RRectsGaussianEdgeFP(*this));
Brian Salomon164f6ec2017-07-25 15:32:17 -0400219 }
220
221 const SkRRect& first() const { return fFirst; }
222 Mode firstMode() const { return fFirstMode; }
223 const SkRRect& second() const { return fSecond; }
224 Mode secondMode() const { return fSecondMode; }
225 SkScalar radius() const { return fRadius; }
226
227private:
Robert Phillipsa29a9562016-10-20 09:40:55 -0400228 class GLSLRRectsGaussianEdgeFP : public GrGLSLFragmentProcessor {
229 public:
Brian Salomon164f6ec2017-07-25 15:32:17 -0400230 GLSLRRectsGaussianEdgeFP() {}
Robert Phillipsa29a9562016-10-20 09:40:55 -0400231
232 // This method emits code so that, for each shape, the distance from the edge is returned
233 // in 'outputName' clamped to 0..1 with positive distance being towards the center of the
234 // shape. The distance will have been normalized by the radius.
235 void emitModeCode(Mode mode,
236 GrGLSLFPFragmentBuilder* fragBuilder,
237 const char* posName,
238 const char* sizesName,
239 const char* radiiName,
240 const char* radName,
241 const char* outputName,
242 const char indices[2]) { // how to access the params for the 2 rrects
243
244 // Positive distance is towards the center of the circle.
245 // Map all the cases to the lower right quadrant.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400246 fragBuilder->codeAppendf("half2 delta = abs(sk_FragCoord.xy - %s.%s);",
Ethan Nicholas38657112017-02-09 17:01:22 -0500247 posName, indices);
Robert Phillipsa29a9562016-10-20 09:40:55 -0400248
249 switch (mode) {
250 case kCircle_Mode:
251 // When a shadow circle gets large we can have some precision issues if
252 // we do "length(delta)/radius". The scaleDist temporary cuts the
253 // delta vector down a bit before invoking length.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400254 fragBuilder->codeAppendf("half scaledDist = length(delta/%s);", radName);
Robert Phillipsa29a9562016-10-20 09:40:55 -0400255 fragBuilder->codeAppendf("%s = clamp((%s.%c/%s - scaledDist), 0.0, 1.0);",
256 outputName, sizesName, indices[0], radName);
257 break;
258 case kRect_Mode:
259 fragBuilder->codeAppendf(
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400260 "half2 rectDist = half2(1.0 - clamp((%s.%c - delta.x)/%s, 0.0, 1.0),"
261 "1.0 - clamp((%s.%c - delta.y)/%s, 0.0, 1.0));",
Robert Phillipsa29a9562016-10-20 09:40:55 -0400262 sizesName, indices[0], radName,
263 sizesName, indices[1], radName);
264 fragBuilder->codeAppendf("%s = clamp(1.0 - length(rectDist), 0.0, 1.0);",
265 outputName);
266 break;
267 case kSimpleCircular_Mode:
268 // For the circular round rect we combine 2 distances:
269 // the fractional position from the corner inset point to the corner's circle
270 // the minimum perpendicular distance to the bounding rectangle
271 // The first distance is used when the pixel is inside the ice-cream-cone-shaped
272 // portion of a corner. The second is used everywhere else.
273 // This is intended to approximate the interpolation pattern if we had
274 // tessellated this geometry into a RRect outside and a rect inside.
275
276 //----------------
277 // rect distance computation
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400278 fragBuilder->codeAppendf("half xDist = (%s.%c - delta.x) / %s;",
Robert Phillipsa29a9562016-10-20 09:40:55 -0400279 sizesName, indices[0], radName);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400280 fragBuilder->codeAppendf("half yDist = (%s.%c - delta.y) / %s;",
Robert Phillipsa29a9562016-10-20 09:40:55 -0400281 sizesName, indices[1], radName);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400282 fragBuilder->codeAppend("half rectDist = clamp(min(xDist, yDist), 0.0, 1.0);");
Robert Phillipsa29a9562016-10-20 09:40:55 -0400283
284 //----------------
285 // ice-cream-cone fractional distance computation
286
287 // When the blurRadius is larger than the corner radius we want to use it to
288 // compute the pointy end of the ice cream cone. If it smaller we just want to
289 // use the center of the corner's circle. When using the blurRadius the inset
290 // amount can't exceed the halfwidths of the RRect.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400291 fragBuilder->codeAppendf("half insetDist = min(max(%s, %s.%c),"
Robert Phillipsa29a9562016-10-20 09:40:55 -0400292 "min(%s.%c, %s.%c));",
293 radName, radiiName, indices[0],
294 sizesName, indices[0], sizesName, indices[1]);
295 // "maxValue" is a correction term for if the blurRadius is larger than the
296 // size of the RRect. In that case we don't want to go all the way to black.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400297 fragBuilder->codeAppendf("half maxValue = insetDist/%s;", radName);
Robert Phillipsa29a9562016-10-20 09:40:55 -0400298
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400299 fragBuilder->codeAppendf("half2 coneBottom = half2(%s.%c - insetDist,"
300 "%s.%c - insetDist);",
Robert Phillipsa29a9562016-10-20 09:40:55 -0400301 sizesName, indices[0], sizesName, indices[1]);
302
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400303 fragBuilder->codeAppendf("half2 cornerTop = half2(%s.%c - %s.%c, %s.%c) -"
Robert Phillipsa29a9562016-10-20 09:40:55 -0400304 "coneBottom;",
305 sizesName, indices[0], radiiName, indices[0],
306 sizesName, indices[1]);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400307 fragBuilder->codeAppendf("half2 cornerRight = half2(%s.%c, %s.%c - %s.%c) -"
Robert Phillipsa29a9562016-10-20 09:40:55 -0400308 "coneBottom;",
309 sizesName, indices[0],
310 sizesName, indices[1], radiiName, indices[1]);
311
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400312 fragBuilder->codeAppend("half2 ptInConeSpace = delta - coneBottom;");
313 fragBuilder->codeAppend("half distToPtInConeSpace = length(ptInConeSpace);");
Robert Phillipsa29a9562016-10-20 09:40:55 -0400314
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400315 fragBuilder->codeAppend("half cross1 = ptInConeSpace.x * cornerTop.y -"
316 "ptInConeSpace.y * cornerTop.x;");
317 fragBuilder->codeAppend("half cross2 = -ptInConeSpace.x * cornerRight.y + "
318 "ptInConeSpace.y * cornerRight.x;");
Robert Phillipsa29a9562016-10-20 09:40:55 -0400319
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400320 fragBuilder->codeAppend("half inCone = step(0.0, cross1) *"
321 "step(0.0, cross2);");
Robert Phillipsa29a9562016-10-20 09:40:55 -0400322
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400323 fragBuilder->codeAppendf("half2 cornerCenterInConeSpace = half2(insetDist -"
Robert Phillipsa29a9562016-10-20 09:40:55 -0400324 "%s.%c);",
325 radiiName, indices[0]);
326
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400327 fragBuilder->codeAppend("half2 connectingVec = ptInConeSpace -"
Robert Phillipsa29a9562016-10-20 09:40:55 -0400328 "cornerCenterInConeSpace;");
329 fragBuilder->codeAppend("ptInConeSpace = normalize(ptInConeSpace);");
330
331 // "a" (i.e., dot(ptInConeSpace, ptInConeSpace) should always be 1.0f since
332 // ptInConeSpace is now normalized
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400333 fragBuilder->codeAppend("half b = 2.0 * dot(ptInConeSpace, connectingVec);");
334 fragBuilder->codeAppendf("half c = dot(connectingVec, connectingVec) - "
Robert Phillipsa29a9562016-10-20 09:40:55 -0400335 "%s.%c * %s.%c;",
336 radiiName, indices[0], radiiName, indices[0]);
337
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400338 fragBuilder->codeAppend("half fourAC = 4*c;");
Robert Phillipsa29a9562016-10-20 09:40:55 -0400339 // This max prevents sqrt(-1) when outside the cone
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400340 fragBuilder->codeAppend("half bSq = max(b*b, fourAC);");
Robert Phillipsa29a9562016-10-20 09:40:55 -0400341
342 // lop off negative values that are outside the cone
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400343 fragBuilder->codeAppend("half coneDist = "
Robert Phillipsa29a9562016-10-20 09:40:55 -0400344 "max(0.0, 0.5 * (-b + sqrt(bSq - fourAC)));");
345 // make the coneDist a fraction of how far it is from the edge to the
346 // cone's base
347 fragBuilder->codeAppend("coneDist = (maxValue*coneDist) /"
348 "(coneDist+distToPtInConeSpace);");
349 fragBuilder->codeAppend("coneDist = clamp(coneDist, 0.0, 1.0);");
350
351 //----------------
352 fragBuilder->codeAppendf("%s = mix(rectDist, coneDist, inCone);", outputName);
353 break;
354 }
355 }
356
357 void emitCode(EmitArgs& args) override {
358 const RRectsGaussianEdgeFP& fp = args.fFp.cast<RRectsGaussianEdgeFP>();
359 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
360 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
361
362 const char* positionsUniName = nullptr;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400363 fPositionsUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType,
Robert Phillipsa29a9562016-10-20 09:40:55 -0400364 "Positions", &positionsUniName);
365 const char* sizesUniName = nullptr;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400366 fSizesUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType,
367 kDefault_GrSLPrecision, "Sizes", &sizesUniName);
Robert Phillipsa29a9562016-10-20 09:40:55 -0400368 const char* radiiUniName = nullptr;
369 if (fp.fFirstMode == kSimpleCircular_Mode || fp.fSecondMode == kSimpleCircular_Mode) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400370 fRadiiUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType,
Robert Phillipsa29a9562016-10-20 09:40:55 -0400371 "Radii", &radiiUniName);
372 }
373 const char* radUniName = nullptr;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400374 fRadiusUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType,
Robert Phillipsa29a9562016-10-20 09:40:55 -0400375 "Radius", &radUniName);
376
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400377 fragBuilder->codeAppend("half firstDist;");
Robert Phillipsa29a9562016-10-20 09:40:55 -0400378 fragBuilder->codeAppend("{");
379 this->emitModeCode(fp.firstMode(), fragBuilder,
380 positionsUniName, sizesUniName, radiiUniName,
381 radUniName, "firstDist", "xy");
382 fragBuilder->codeAppend("}");
383
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400384 fragBuilder->codeAppend("half secondDist;");
Robert Phillipsa29a9562016-10-20 09:40:55 -0400385 fragBuilder->codeAppend("{");
386 this->emitModeCode(fp.secondMode(), fragBuilder,
387 positionsUniName, sizesUniName, radiiUniName,
388 radUniName, "secondDist", "zw");
389 fragBuilder->codeAppend("}");
390
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400391 fragBuilder->codeAppend("half2 distVec = half2(1.0 - firstDist, 1.0 - secondDist);");
Robert Phillipsa29a9562016-10-20 09:40:55 -0400392
393 // Finally use the distance to apply the Gaussian edge
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400394 fragBuilder->codeAppend("half factor = clamp(length(distVec), 0.0, 1.0);");
Robert Phillipsa29a9562016-10-20 09:40:55 -0400395 fragBuilder->codeAppend("factor = exp(-factor * factor * 4.0) - 0.018;");
396 fragBuilder->codeAppendf("%s = factor*%s;",
397 args.fOutputColor, args.fInputColor);
398 }
399
Brian Salomon94efbf52016-11-29 13:43:05 -0500400 static void GenKey(const GrProcessor& proc, const GrShaderCaps&, GrProcessorKeyBuilder* b) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400401 const RRectsGaussianEdgeFP& fp = proc.cast<RRectsGaussianEdgeFP>();
402
403 b->add32(fp.firstMode() | (fp.secondMode() << 4));
404 }
405
406 protected:
Brian Salomonab015ef2017-04-04 10:15:51 -0400407 void onSetData(const GrGLSLProgramDataManager& pdman,
408 const GrFragmentProcessor& proc) override {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400409 const RRectsGaussianEdgeFP& edgeFP = proc.cast<RRectsGaussianEdgeFP>();
410
411 const SkRRect& first = edgeFP.first();
412 const SkRRect& second = edgeFP.second();
413
414 pdman.set4f(fPositionsUni,
415 first.getBounds().centerX(),
416 first.getBounds().centerY(),
417 second.getBounds().centerX(),
418 second.getBounds().centerY());
419
Ben Wagner63fd7602017-10-09 15:45:33 -0400420 pdman.set4f(fSizesUni,
Robert Phillipsa29a9562016-10-20 09:40:55 -0400421 0.5f * first.rect().width(),
422 0.5f * first.rect().height(),
423 0.5f * second.rect().width(),
424 0.5f * second.rect().height());
425
426 if (edgeFP.firstMode() == kSimpleCircular_Mode ||
427 edgeFP.secondMode() == kSimpleCircular_Mode) {
428 // This is a bit of overkill since fX should equal fY for both round rects but it
429 // makes the shader code simpler.
Ben Wagner63fd7602017-10-09 15:45:33 -0400430 pdman.set4f(fRadiiUni,
Mike Reed242135a2018-02-22 13:41:39 -0500431 SkRRectPriv::GetSimpleRadii(first).fX,
432 SkRRectPriv::GetSimpleRadii(first).fY,
433 SkRRectPriv::GetSimpleRadii(second).fX,
434 SkRRectPriv::GetSimpleRadii(second).fY);
Robert Phillipsa29a9562016-10-20 09:40:55 -0400435 }
436
437 pdman.set1f(fRadiusUni, edgeFP.radius());
438 }
439
440 private:
441 // The centers of the two round rects (x1, y1, x2, y2)
442 GrGLSLProgramDataManager::UniformHandle fPositionsUni;
443
444 // The half widths and half heights of the two round rects (w1/2, h1/2, w2/2, h2/2)
445 // For circles we still upload both width & height to simplify things
446 GrGLSLProgramDataManager::UniformHandle fSizesUni;
447
448 // The corner radii of the two round rects (rx1, ry1, rx2, ry2)
449 // We upload both the x&y radii (although they are currently always the same) to make
450 // the indexing in the shader code simpler. In some future world we could also support
451 // non-circular corner round rects & ellipses.
452 GrGLSLProgramDataManager::UniformHandle fRadiiUni;
453
454 // The radius parameters (radius)
455 GrGLSLProgramDataManager::UniformHandle fRadiusUni;
456
457 typedef GrGLSLFragmentProcessor INHERITED;
458 };
459
Brian Salomon94efbf52016-11-29 13:43:05 -0500460 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400461 GLSLRRectsGaussianEdgeFP::GenKey(*this, caps, b);
462 }
463
Robert Phillips1c9686b2017-06-30 08:40:28 -0400464 RRectsGaussianEdgeFP(const SkRRect& first, const SkRRect& second, SkScalar radius)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400465 : INHERITED(kRRectsGaussianEdgeFP_ClassID,
466 kCompatibleWithCoverageAsAlpha_OptimizationFlag)
Robert Phillips1c9686b2017-06-30 08:40:28 -0400467 , fFirst(first)
468 , fSecond(second)
469 , fRadius(radius) {
Robert Phillips1c9686b2017-06-30 08:40:28 -0400470
471 fFirstMode = ComputeMode(fFirst);
472 fSecondMode = ComputeMode(fSecond);
473 }
Brian Salomon164f6ec2017-07-25 15:32:17 -0400474 RRectsGaussianEdgeFP(const RRectsGaussianEdgeFP& that)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400475 : INHERITED(kRRectsGaussianEdgeFP_ClassID,
476 kCompatibleWithCoverageAsAlpha_OptimizationFlag)
Brian Salomon164f6ec2017-07-25 15:32:17 -0400477 , fFirst(that.fFirst)
478 , fFirstMode(that.fFirstMode)
479 , fSecond(that.fSecond)
480 , fSecondMode(that.fSecondMode)
481 , fRadius(that.fRadius) {
Brian Salomon164f6ec2017-07-25 15:32:17 -0400482 }
Robert Phillips1c9686b2017-06-30 08:40:28 -0400483
Robert Phillipsa29a9562016-10-20 09:40:55 -0400484 static Mode ComputeMode(const SkRRect& rr) {
Mike Reed242135a2018-02-22 13:41:39 -0500485 if (SkRRectPriv::IsCircle(rr)) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400486 return kCircle_Mode;
487 } else if (rr.isRect()) {
488 return kRect_Mode;
489 } else {
Mike Reed242135a2018-02-22 13:41:39 -0500490 SkASSERT(SkRRectPriv::IsSimpleCircular(rr));
Robert Phillipsa29a9562016-10-20 09:40:55 -0400491 return kSimpleCircular_Mode;
492 }
493 }
494
495 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
496 return new GLSLRRectsGaussianEdgeFP;
497 }
498
499 bool onIsEqual(const GrFragmentProcessor& proc) const override {
500 const RRectsGaussianEdgeFP& edgeFP = proc.cast<RRectsGaussianEdgeFP>();
501 return fFirst == edgeFP.fFirst &&
Ben Wagner63fd7602017-10-09 15:45:33 -0400502 fSecond == edgeFP.fSecond &&
Robert Phillipsa29a9562016-10-20 09:40:55 -0400503 fRadius == edgeFP.fRadius;
504 }
505
506 SkRRect fFirst;
507 Mode fFirstMode;
508 SkRRect fSecond;
509 Mode fSecondMode;
510 SkScalar fRadius;
511
512 typedef GrFragmentProcessor INHERITED;
513};
514
515////////////////////////////////////////////////////////////////////////////
Robert Phillipsa29a9562016-10-20 09:40:55 -0400516
Mike Reedbfadcf02018-01-20 22:24:21 +0000517std::unique_ptr<GrFragmentProcessor>
518SkRRectsGaussianEdgeMaskFilterImpl::onAsFragmentProcessor(const GrFPArgs& args) const {
519 return RRectsGaussianEdgeFP::Make(fFirst, fSecond, fRadius);
Robert Phillipsa29a9562016-10-20 09:40:55 -0400520}
521
522#endif
523
524////////////////////////////////////////////////////////////////////////////
525
526#ifndef SK_IGNORE_TO_STRING
527void SkRRectsGaussianEdgeMaskFilterImpl::toString(SkString* str) const {
528 str->appendf("RRectsGaussianEdgeMaskFilter: ()");
529}
530#endif
531
532sk_sp<SkFlattenable> SkRRectsGaussianEdgeMaskFilterImpl::CreateProc(SkReadBuffer& buf) {
533 SkRect rect1, rect2;
534
535 buf.readRect(&rect1);
536 SkScalar xRad1 = buf.readScalar();
537 SkScalar yRad1 = buf.readScalar();
538
539 buf.readRect(&rect2);
540 SkScalar xRad2 = buf.readScalar();
541 SkScalar yRad2 = buf.readScalar();
542
543 SkScalar radius = buf.readScalar();
544
545 return sk_make_sp<SkRRectsGaussianEdgeMaskFilterImpl>(SkRRect::MakeRectXY(rect1, xRad1, yRad1),
546 SkRRect::MakeRectXY(rect2, xRad2, yRad2),
547 radius);
548}
549
550void SkRRectsGaussianEdgeMaskFilterImpl::flatten(SkWriteBuffer& buf) const {
551 INHERITED::flatten(buf);
552
Mike Reed242135a2018-02-22 13:41:39 -0500553 SkASSERT(SkRRectPriv::EqualRadii(fFirst));
Robert Phillipsa29a9562016-10-20 09:40:55 -0400554 buf.writeRect(fFirst.rect());
Mike Reed242135a2018-02-22 13:41:39 -0500555 const SkVector radii1 = SkRRectPriv::GetSimpleRadii(fFirst);
Robert Phillipsa29a9562016-10-20 09:40:55 -0400556 buf.writeScalar(radii1.fX);
557 buf.writeScalar(radii1.fY);
558
Mike Reed242135a2018-02-22 13:41:39 -0500559 SkASSERT(SkRRectPriv::EqualRadii(fSecond));
Robert Phillipsa29a9562016-10-20 09:40:55 -0400560 buf.writeRect(fSecond.rect());
Mike Reed242135a2018-02-22 13:41:39 -0500561 const SkVector radii2 = SkRRectPriv::GetSimpleRadii(fSecond);
Robert Phillipsa29a9562016-10-20 09:40:55 -0400562 buf.writeScalar(radii2.fX);
563 buf.writeScalar(radii2.fY);
564
565 buf.writeScalar(fRadius);
566}
567
568///////////////////////////////////////////////////////////////////////////////
569
570sk_sp<SkMaskFilter> SkRRectsGaussianEdgeMaskFilter::Make(const SkRRect& first,
571 const SkRRect& second,
572 SkScalar radius) {
Mike Reed242135a2018-02-22 13:41:39 -0500573 if (!SkRRectPriv::EqualRadii(first) || !SkRRectPriv::EqualRadii(second)) {
Ben Wagner63fd7602017-10-09 15:45:33 -0400574 // we only deal with the shapes where the x & y radii are equal
Robert Phillipsa29a9562016-10-20 09:40:55 -0400575 // and the same for all four corners
576 return nullptr;
577 }
578
579 return sk_make_sp<SkRRectsGaussianEdgeMaskFilterImpl>(first, second, radius);
580}
581
582///////////////////////////////////////////////////////////////////////////////
583
584SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkRRectsGaussianEdgeMaskFilter)
585SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRRectsGaussianEdgeMaskFilterImpl)
586SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
587
588///////////////////////////////////////////////////////////////////////////////