1 /*
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 
8 #include "SkRRectsGaussianEdgeMaskFilter.h"
9 #include "SkReadBuffer.h"
10 #include "SkRRect.h"
11 #include "SkWriteBuffer.h"
12 
13 #if SK_SUPPORT_GPU
14 #include "GrFragmentProcessor.h"
15 #endif
16 
17  /** \class SkRRectsGaussianEdgeMaskFilterImpl
18   * This mask filter applies a gaussian edge to the intersection of two round rects.
19   * The round rects must have the same radii at each corner and the x&y radii
20   * must also be equal.
21   */
22 class SkRRectsGaussianEdgeMaskFilterImpl : public SkMaskFilter {
23 public:
SkRRectsGaussianEdgeMaskFilterImpl(const SkRRect & first,const SkRRect & second,SkScalar radius)24     SkRRectsGaussianEdgeMaskFilterImpl(const SkRRect& first, const SkRRect& second,
25                                        SkScalar radius)
26         : fFirst(first)
27         , fSecond(second)
28         , fRadius(radius) {
29     }
30 
getFormat() const31     SkMask::Format getFormat() const override { return SkMask::kA8_Format; }
32     bool filterMask(SkMask* dst, const SkMask& src, const SkMatrix&,
33                     SkIPoint* margin) const override;
34 
35 #if SK_SUPPORT_GPU
36     bool asFragmentProcessor(GrFragmentProcessor**, GrTexture*, const SkMatrix& ctm) const override;
37 #endif
38 
39     SK_TO_STRING_OVERRIDE()
40     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkRRectsGaussianEdgeMaskFilterImpl)
41 
42 protected:
43     void flatten(SkWriteBuffer&) const override;
44 
45 private:
46     SkRRect  fFirst;
47     SkRRect  fSecond;
48     SkScalar fRadius;
49 
50     friend class SkRRectsGaussianEdgeMaskFilter; // for serialization registration system
51 
52     typedef SkMaskFilter INHERITED;
53 };
54 
55 // x & y are in device space
compute_rrect_normalized_dist(const SkRRect & rr,const SkPoint & p,SkScalar rad)56 static SkScalar compute_rrect_normalized_dist(const SkRRect& rr, const SkPoint& p, SkScalar rad) {
57     SkASSERT(rr.getType() == SkRRect::kOval_Type || rr.getType() == SkRRect::kRect_Type ||
58              rr.getType() == SkRRect::kSimple_Type);
59     SkASSERT(rad > 0.0f);
60 
61     SkVector delta = { SkTAbs(p.fX - rr.rect().centerX()), SkTAbs(p.fY - rr.rect().centerY()) };
62 
63     SkScalar halfW = 0.5f * rr.rect().width();
64     SkScalar halfH = 0.5f * rr.rect().height();
65     SkScalar invRad = 1.0f/rad;
66 
67     const SkVector& radii = rr.getSimpleRadii();
68     SkASSERT(SkScalarNearlyEqual(radii.fX, radii.fY));
69 
70     switch (rr.getType()) {
71         case SkRRect::kOval_Type: {
72             float scaledDist = delta.length() * invRad;
73             return SkTPin(halfW * invRad - scaledDist, 0.0f, 1.0f);
74         }
75         case SkRRect::kRect_Type: {
76             SkScalar xDist = (halfW - delta.fX) * invRad;
77             SkScalar yDist = (halfH - delta.fY) * invRad;
78 
79             SkVector v = { 1.0f - SkTPin(xDist, 0.0f, 1.0f), 1.0f - SkTPin(yDist, 0.0f, 1.0f) };
80             return SkTPin(1.0f - v.length(), 0.0f, 1.0f);
81         }
82         case SkRRect::kSimple_Type: {
83 
84             //----------------
85             // ice-cream-cone fractional distance computation
86 
87             // When the blurRadius is larger than the corner radius we want to use it to
88             // compute the pointy end of the ice cream cone. If it smaller we just want to use
89             // the center of the corner's circle. When using the blurRadius the inset amount
90             // can't exceed the halfwidths of the RRect.
91             SkScalar insetDist = SkTMin(SkTMax(rad, radii.fX), SkTMin(halfW, halfH));
92 
93             // "maxValue" is a correction term for if the blurRadius is larger than the
94             // size of the RRect. In that case we don't want to go all the way to black.
95             SkScalar maxValue = insetDist * invRad;
96 
97             SkVector coneBottom = { halfW - insetDist, halfH - insetDist };
98             SkVector ptInConeSpace = delta - coneBottom;
99 
100             SkVector cornerTop = { halfW - radii.fX - coneBottom.fX, halfH - coneBottom.fY };
101             SkVector cornerRight = { halfW - coneBottom.fX, halfH - radii.fY - coneBottom.fY };
102 
103             SkScalar cross1 = ptInConeSpace.cross(cornerTop);
104             SkScalar cross2 = cornerRight.cross(ptInConeSpace);
105             bool inCone = cross1 > 0.0f && cross2 > 0.0f;
106 
107             if (!inCone) {
108                 SkScalar xDist = (halfW - delta.fX) * invRad;
109                 SkScalar yDist = (halfH - delta.fY) * invRad;
110 
111                 return SkTPin(SkTMin(xDist, yDist), 0.0f, 1.0f); // perpendicular distance
112             }
113 
114             SkVector cornerCenterInConeSpace = { insetDist - radii.fX, insetDist - radii.fY };
115 
116             SkVector connectingVec = ptInConeSpace - cornerCenterInConeSpace;
117             float distToPtInConeSpace = SkPoint::Normalize(&ptInConeSpace);
118 
119             // "a" (i.e., dot(ptInConeSpace, ptInConeSpace) should always be 1.0f since
120             // ptInConeSpace is now normalized
121             SkScalar b = 2.0f * ptInConeSpace.dot(connectingVec);
122             SkScalar c = connectingVec.dot(connectingVec) - radii.fX * radii.fY;
123 
124             // lop off negative values that are outside the cone
125             SkScalar coneDist = SkTMax(0.0f, 0.5f * (-b + SkScalarSqrt(b*b - 4*c)));
126 
127             // make the coneDist a fraction of how far it is from the edge to the cone's base
128             coneDist = (maxValue*coneDist) / (coneDist+distToPtInConeSpace);
129             return SkTPin(coneDist, 0.0f, 1.0f);
130         }
131         default:
132             return 0.0f;
133     }
134 }
135 
filterMask(SkMask * dst,const SkMask & src,const SkMatrix & matrix,SkIPoint * margin) const136 bool SkRRectsGaussianEdgeMaskFilterImpl::filterMask(SkMask* dst, const SkMask& src,
137                                                     const SkMatrix& matrix,
138                                                     SkIPoint* margin) const {
139 
140     if (src.fFormat != SkMask::kA8_Format) {
141         return false;
142     }
143 
144     if (margin) {
145         margin->set(0, 0);
146     }
147 
148     dst->fBounds = src.fBounds;
149     dst->fRowBytes = dst->fBounds.width();
150     dst->fFormat = SkMask::kA8_Format;
151     dst->fImage = nullptr;
152 
153     if (src.fImage) {
154         size_t dstSize = dst->computeImageSize();
155         if (0 == dstSize) {
156             return false;   // too big to allocate, abort
157         }
158 
159         const uint8_t* srcPixels = src.fImage;
160         uint8_t* dstPixels = dst->fImage = SkMask::AllocImage(dstSize);
161 
162         SkPoint basePt = { SkIntToScalar(src.fBounds.fLeft), SkIntToScalar(src.fBounds.fTop) };
163 
164         for (int y = 0; y < dst->fBounds.height(); ++y) {
165             const uint8_t* srcRow = srcPixels + y * dst->fRowBytes;
166             uint8_t* dstRow = dstPixels + y*dst->fRowBytes;
167 
168             for (int x = 0; x < dst->fBounds.width(); ++x) {
169                 SkPoint curPt = { basePt.fX + x, basePt.fY + y };
170 
171                 SkVector vec;
172                 vec.fX = 1.0f - compute_rrect_normalized_dist(fFirst, curPt, fRadius);
173                 vec.fY = 1.0f - compute_rrect_normalized_dist(fSecond, curPt, fRadius);
174 
175                 SkScalar factor = SkTPin(vec.length(), 0.0f, 1.0f);
176                 factor = exp(-factor * factor * 4.0f) - 0.018f;
177                 SkASSERT(factor >= 0.0f && factor <= 1.0f);
178 
179                 dstRow[x] = (uint8_t) (factor * srcRow[x]);
180             }
181         }
182     }
183 
184     return true;
185 }
186 
187 ////////////////////////////////////////////////////////////////////////////
188 
189 #if SK_SUPPORT_GPU
190 
191 #include "GrCoordTransform.h"
192 #include "GrFragmentProcessor.h"
193 #include "glsl/GrGLSLFragmentProcessor.h"
194 #include "glsl/GrGLSLFragmentShaderBuilder.h"
195 #include "glsl/GrGLSLProgramDataManager.h"
196 #include "glsl/GrGLSLUniformHandler.h"
197 #include "SkGr.h"
198 
199 class RRectsGaussianEdgeFP : public GrFragmentProcessor {
200 public:
201     enum Mode {
202         kCircle_Mode,
203         kRect_Mode,
204         kSimpleCircular_Mode,
205     };
206 
RRectsGaussianEdgeFP(const SkRRect & first,const SkRRect & second,SkScalar radius)207     RRectsGaussianEdgeFP(const SkRRect& first, const SkRRect& second, SkScalar radius)
208             : INHERITED(kCompatibleWithCoverageAsAlpha_OptimizationFlag)
209             , fFirst(first)
210             , fSecond(second)
211             , fRadius(radius) {
212         this->initClassID<RRectsGaussianEdgeFP>();
213 
214         fFirstMode = ComputeMode(fFirst);
215         fSecondMode = ComputeMode(fSecond);
216     }
217 
218     class GLSLRRectsGaussianEdgeFP : public GrGLSLFragmentProcessor {
219     public:
GLSLRRectsGaussianEdgeFP()220         GLSLRRectsGaussianEdgeFP() { }
221 
222         // This method emits code so that, for each shape, the distance from the edge is returned
223         // in 'outputName' clamped to 0..1 with positive distance being towards the center of the
224         // shape. The distance will have been normalized by the radius.
emitModeCode(Mode mode,GrGLSLFPFragmentBuilder * fragBuilder,const char * posName,const char * sizesName,const char * radiiName,const char * radName,const char * outputName,const char indices[2])225         void emitModeCode(Mode mode,
226                           GrGLSLFPFragmentBuilder* fragBuilder,
227                           const char* posName,
228                           const char* sizesName,
229                           const char* radiiName,
230                           const char* radName,
231                           const char* outputName,
232                           const char  indices[2]) { // how to access the params for the 2 rrects
233 
234             // Positive distance is towards the center of the circle.
235             // Map all the cases to the lower right quadrant.
236             fragBuilder->codeAppendf("vec2 delta = abs(sk_FragCoord.xy - %s.%s);",
237                                      posName, indices);
238 
239             switch (mode) {
240                 case kCircle_Mode:
241                     // When a shadow circle gets large we can have some precision issues if
242                     // we do "length(delta)/radius". The scaleDist temporary cuts the
243                     // delta vector down a bit before invoking length.
244                     fragBuilder->codeAppendf("float scaledDist = length(delta/%s);", radName);
245                     fragBuilder->codeAppendf("%s = clamp((%s.%c/%s - scaledDist), 0.0, 1.0);",
246                                              outputName, sizesName, indices[0], radName);
247                     break;
248                 case kRect_Mode:
249                     fragBuilder->codeAppendf(
250                         "vec2 rectDist = vec2(1.0 - clamp((%s.%c - delta.x)/%s, 0.0, 1.0),"
251                                              "1.0 - clamp((%s.%c - delta.y)/%s, 0.0, 1.0));",
252                         sizesName, indices[0], radName,
253                         sizesName, indices[1], radName);
254                     fragBuilder->codeAppendf("%s = clamp(1.0 - length(rectDist), 0.0, 1.0);",
255                                              outputName);
256                     break;
257                 case kSimpleCircular_Mode:
258                     // For the circular round rect we combine 2 distances:
259                     //    the fractional position from the corner inset point to the corner's circle
260                     //    the minimum perpendicular distance to the bounding rectangle
261                     // The first distance is used when the pixel is inside the ice-cream-cone-shaped
262                     // portion of a corner. The second is used everywhere else.
263                     // This is intended to approximate the interpolation pattern if we had
264                     // tessellated this geometry into a RRect outside and a rect inside.
265 
266                     //----------------
267                     // rect distance computation
268                     fragBuilder->codeAppendf("float xDist = (%s.%c - delta.x) / %s;",
269                                              sizesName, indices[0], radName);
270                     fragBuilder->codeAppendf("float yDist = (%s.%c - delta.y) / %s;",
271                                              sizesName, indices[1], radName);
272                     fragBuilder->codeAppend("float rectDist = clamp(min(xDist, yDist), 0.0, 1.0);");
273 
274                     //----------------
275                     // ice-cream-cone fractional distance computation
276 
277                     // When the blurRadius is larger than the corner radius we want to use it to
278                     // compute the pointy end of the ice cream cone. If it smaller we just want to
279                     // use the center of the corner's circle. When using the blurRadius the inset
280                     // amount can't exceed the halfwidths of the RRect.
281                     fragBuilder->codeAppendf("float insetDist = min(max(%s, %s.%c),"
282                                                                    "min(%s.%c, %s.%c));",
283                                              radName, radiiName, indices[0],
284                                              sizesName, indices[0], sizesName, indices[1]);
285                     // "maxValue" is a correction term for if the blurRadius is larger than the
286                     // size of the RRect. In that case we don't want to go all the way to black.
287                     fragBuilder->codeAppendf("float maxValue = insetDist/%s;", radName);
288 
289                     fragBuilder->codeAppendf("vec2 coneBottom = vec2(%s.%c - insetDist,"
290                                                                     "%s.%c - insetDist);",
291                                              sizesName, indices[0], sizesName, indices[1]);
292 
293                     fragBuilder->codeAppendf("vec2 cornerTop = vec2(%s.%c - %s.%c, %s.%c) -"
294                                                                         "coneBottom;",
295                                              sizesName, indices[0], radiiName, indices[0],
296                                              sizesName, indices[1]);
297                     fragBuilder->codeAppendf("vec2 cornerRight = vec2(%s.%c, %s.%c - %s.%c) -"
298                                                                         "coneBottom;",
299                                              sizesName, indices[0],
300                                              sizesName, indices[1], radiiName, indices[1]);
301 
302                     fragBuilder->codeAppend("vec2 ptInConeSpace = delta - coneBottom;");
303                     fragBuilder->codeAppend("float distToPtInConeSpace = length(ptInConeSpace);");
304 
305                     fragBuilder->codeAppend("float cross1 =  ptInConeSpace.x * cornerTop.y -"
306                                                             "ptInConeSpace.y * cornerTop.x;");
307                     fragBuilder->codeAppend("float cross2 = -ptInConeSpace.x * cornerRight.y + "
308                                                             "ptInConeSpace.y * cornerRight.x;");
309 
310                     fragBuilder->codeAppend("float inCone = step(0.0, cross1) *"
311                                                            "step(0.0, cross2);");
312 
313                     fragBuilder->codeAppendf("vec2 cornerCenterInConeSpace = vec2(insetDist -"
314                                                                                  "%s.%c);",
315                                              radiiName, indices[0]);
316 
317                     fragBuilder->codeAppend("vec2 connectingVec = ptInConeSpace -"
318                                                                         "cornerCenterInConeSpace;");
319                     fragBuilder->codeAppend("ptInConeSpace = normalize(ptInConeSpace);");
320 
321                     // "a" (i.e., dot(ptInConeSpace, ptInConeSpace) should always be 1.0f since
322                     // ptInConeSpace is now normalized
323                     fragBuilder->codeAppend("float b = 2.0 * dot(ptInConeSpace, connectingVec);");
324                     fragBuilder->codeAppendf("float c = dot(connectingVec, connectingVec) - "
325                                                                                    "%s.%c * %s.%c;",
326                                              radiiName, indices[0], radiiName, indices[0]);
327 
328                     fragBuilder->codeAppend("float fourAC = 4*c;");
329                     // This max prevents sqrt(-1) when outside the cone
330                     fragBuilder->codeAppend("float bSq = max(b*b, fourAC);");
331 
332                     // lop off negative values that are outside the cone
333                     fragBuilder->codeAppend("float coneDist = "
334                                                     "max(0.0, 0.5 * (-b + sqrt(bSq - fourAC)));");
335                     // make the coneDist a fraction of how far it is from the edge to the
336                     // cone's base
337                     fragBuilder->codeAppend("coneDist = (maxValue*coneDist) /"
338                                                                 "(coneDist+distToPtInConeSpace);");
339                     fragBuilder->codeAppend("coneDist = clamp(coneDist, 0.0, 1.0);");
340 
341                     //----------------
342                     fragBuilder->codeAppendf("%s = mix(rectDist, coneDist, inCone);", outputName);
343                     break;
344                 }
345         }
346 
emitCode(EmitArgs & args)347         void emitCode(EmitArgs& args) override {
348             const RRectsGaussianEdgeFP& fp = args.fFp.cast<RRectsGaussianEdgeFP>();
349             GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
350             GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
351 
352             const char* positionsUniName = nullptr;
353             fPositionsUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
354                                                        kVec4f_GrSLType, kDefault_GrSLPrecision,
355                                                        "Positions", &positionsUniName);
356             const char* sizesUniName = nullptr;
357             fSizesUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
358                                                    kVec4f_GrSLType, kDefault_GrSLPrecision,
359                                                    "Sizes", &sizesUniName);
360             const char* radiiUniName = nullptr;
361             if (fp.fFirstMode == kSimpleCircular_Mode || fp.fSecondMode == kSimpleCircular_Mode) {
362                 fRadiiUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
363                                                        kVec4f_GrSLType, kDefault_GrSLPrecision,
364                                                        "Radii", &radiiUniName);
365             }
366             const char* radUniName = nullptr;
367             fRadiusUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
368                                                     kFloat_GrSLType, kDefault_GrSLPrecision,
369                                                     "Radius", &radUniName);
370 
371             fragBuilder->codeAppend("float firstDist;");
372             fragBuilder->codeAppend("{");
373             this->emitModeCode(fp.firstMode(), fragBuilder,
374                                positionsUniName, sizesUniName, radiiUniName,
375                                radUniName, "firstDist", "xy");
376             fragBuilder->codeAppend("}");
377 
378             fragBuilder->codeAppend("float secondDist;");
379             fragBuilder->codeAppend("{");
380             this->emitModeCode(fp.secondMode(), fragBuilder,
381                                positionsUniName, sizesUniName, radiiUniName,
382                                radUniName, "secondDist", "zw");
383             fragBuilder->codeAppend("}");
384 
385             fragBuilder->codeAppend("vec2 distVec = vec2(1.0 - firstDist, 1.0 - secondDist);");
386 
387             // Finally use the distance to apply the Gaussian edge
388             fragBuilder->codeAppend("float factor = clamp(length(distVec), 0.0, 1.0);");
389             fragBuilder->codeAppend("factor = exp(-factor * factor * 4.0) - 0.018;");
390             fragBuilder->codeAppendf("%s = factor*%s;",
391                                      args.fOutputColor, args.fInputColor);
392         }
393 
GenKey(const GrProcessor & proc,const GrShaderCaps &,GrProcessorKeyBuilder * b)394         static void GenKey(const GrProcessor& proc, const GrShaderCaps&, GrProcessorKeyBuilder* b) {
395             const RRectsGaussianEdgeFP& fp = proc.cast<RRectsGaussianEdgeFP>();
396 
397             b->add32(fp.firstMode() | (fp.secondMode() << 4));
398         }
399 
400     protected:
onSetData(const GrGLSLProgramDataManager & pdman,const GrProcessor & proc)401         void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override {
402             const RRectsGaussianEdgeFP& edgeFP = proc.cast<RRectsGaussianEdgeFP>();
403 
404             const SkRRect& first = edgeFP.first();
405             const SkRRect& second = edgeFP.second();
406 
407             pdman.set4f(fPositionsUni,
408                         first.getBounds().centerX(),
409                         first.getBounds().centerY(),
410                         second.getBounds().centerX(),
411                         second.getBounds().centerY());
412 
413             pdman.set4f(fSizesUni,
414                         0.5f * first.rect().width(),
415                         0.5f * first.rect().height(),
416                         0.5f * second.rect().width(),
417                         0.5f * second.rect().height());
418 
419             if (edgeFP.firstMode() == kSimpleCircular_Mode ||
420                 edgeFP.secondMode() == kSimpleCircular_Mode) {
421                 // This is a bit of overkill since fX should equal fY for both round rects but it
422                 // makes the shader code simpler.
423                 pdman.set4f(fRadiiUni,
424                             first.getSimpleRadii().fX,  first.getSimpleRadii().fY,
425                             second.getSimpleRadii().fX, second.getSimpleRadii().fY);
426             }
427 
428             pdman.set1f(fRadiusUni, edgeFP.radius());
429         }
430 
431     private:
432         // The centers of the two round rects (x1, y1, x2, y2)
433         GrGLSLProgramDataManager::UniformHandle fPositionsUni;
434 
435         // The half widths and half heights of the two round rects (w1/2, h1/2, w2/2, h2/2)
436         // For circles we still upload both width & height to simplify things
437         GrGLSLProgramDataManager::UniformHandle fSizesUni;
438 
439         // The corner radii of the two round rects (rx1, ry1, rx2, ry2)
440         // We upload both the x&y radii (although they are currently always the same) to make
441         // the indexing in the shader code simpler. In some future world we could also support
442         // non-circular corner round rects & ellipses.
443         GrGLSLProgramDataManager::UniformHandle fRadiiUni;
444 
445         // The radius parameters (radius)
446         GrGLSLProgramDataManager::UniformHandle fRadiusUni;
447 
448         typedef GrGLSLFragmentProcessor INHERITED;
449     };
450 
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const451     void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override {
452         GLSLRRectsGaussianEdgeFP::GenKey(*this, caps, b);
453     }
454 
name() const455     const char* name() const override { return "RRectsGaussianEdgeFP"; }
456 
first() const457     const SkRRect& first() const { return fFirst; }
firstMode() const458     Mode firstMode() const { return fFirstMode; }
second() const459     const SkRRect& second() const { return fSecond; }
secondMode() const460     Mode secondMode() const { return fSecondMode; }
radius() const461     SkScalar radius() const { return fRadius; }
462 
463 private:
ComputeMode(const SkRRect & rr)464     static Mode ComputeMode(const SkRRect& rr) {
465         if (rr.isCircle()) {
466             return kCircle_Mode;
467         } else if (rr.isRect()) {
468             return kRect_Mode;
469         } else {
470             SkASSERT(rr.isSimpleCircular());
471             return kSimpleCircular_Mode;
472         }
473     }
474 
onCreateGLSLInstance() const475     GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
476         return new GLSLRRectsGaussianEdgeFP;
477     }
478 
onIsEqual(const GrFragmentProcessor & proc) const479     bool onIsEqual(const GrFragmentProcessor& proc) const override {
480         const RRectsGaussianEdgeFP& edgeFP = proc.cast<RRectsGaussianEdgeFP>();
481         return fFirst  == edgeFP.fFirst &&
482                fSecond == edgeFP.fSecond &&
483                fRadius == edgeFP.fRadius;
484     }
485 
486     SkRRect  fFirst;
487     Mode     fFirstMode;
488     SkRRect  fSecond;
489     Mode     fSecondMode;
490     SkScalar fRadius;
491 
492     typedef GrFragmentProcessor INHERITED;
493 };
494 
495 ////////////////////////////////////////////////////////////////////////////
asFragmentProcessor(GrFragmentProcessor ** fp,GrTexture *,const SkMatrix & ctm) const496 bool SkRRectsGaussianEdgeMaskFilterImpl::asFragmentProcessor(GrFragmentProcessor** fp,
497                                                              GrTexture*, const
498                                                              SkMatrix& ctm) const {
499     if (fp) {
500         *fp = new RRectsGaussianEdgeFP(fFirst, fSecond, fRadius);
501     }
502 
503     return true;
504 }
505 
506 #endif
507 
508 ////////////////////////////////////////////////////////////////////////////
509 
510 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const511 void SkRRectsGaussianEdgeMaskFilterImpl::toString(SkString* str) const {
512     str->appendf("RRectsGaussianEdgeMaskFilter: ()");
513 }
514 #endif
515 
CreateProc(SkReadBuffer & buf)516 sk_sp<SkFlattenable> SkRRectsGaussianEdgeMaskFilterImpl::CreateProc(SkReadBuffer& buf) {
517     SkRect rect1, rect2;
518 
519     buf.readRect(&rect1);
520     SkScalar xRad1 = buf.readScalar();
521     SkScalar yRad1 = buf.readScalar();
522 
523     buf.readRect(&rect2);
524     SkScalar xRad2 = buf.readScalar();
525     SkScalar yRad2 = buf.readScalar();
526 
527     SkScalar radius = buf.readScalar();
528 
529     return sk_make_sp<SkRRectsGaussianEdgeMaskFilterImpl>(SkRRect::MakeRectXY(rect1, xRad1, yRad1),
530                                                           SkRRect::MakeRectXY(rect2, xRad2, yRad2),
531                                                           radius);
532 }
533 
flatten(SkWriteBuffer & buf) const534 void SkRRectsGaussianEdgeMaskFilterImpl::flatten(SkWriteBuffer& buf) const {
535     INHERITED::flatten(buf);
536 
537     SkASSERT(fFirst.isRect() || fFirst.isCircle() || fFirst.isSimpleCircular());
538     buf.writeRect(fFirst.rect());
539     const SkVector& radii1 = fFirst.getSimpleRadii();
540     buf.writeScalar(radii1.fX);
541     buf.writeScalar(radii1.fY);
542 
543     SkASSERT(fSecond.isRect() || fSecond.isCircle() || fSecond.isSimpleCircular());
544     buf.writeRect(fSecond.rect());
545     const SkVector& radii2 = fSecond.getSimpleRadii();
546     buf.writeScalar(radii2.fX);
547     buf.writeScalar(radii2.fY);
548 
549     buf.writeScalar(fRadius);
550 }
551 
552 ///////////////////////////////////////////////////////////////////////////////
553 
Make(const SkRRect & first,const SkRRect & second,SkScalar radius)554 sk_sp<SkMaskFilter> SkRRectsGaussianEdgeMaskFilter::Make(const SkRRect& first,
555                                                          const SkRRect& second,
556                                                          SkScalar radius) {
557     if ((!first.isRect()  && !first.isCircle()  && !first.isSimpleCircular()) ||
558         (!second.isRect() && !second.isCircle() && !second.isSimpleCircular())) {
559         // we only deal with the shapes where the x & y radii are equal
560         // and the same for all four corners
561         return nullptr;
562     }
563 
564     return sk_make_sp<SkRRectsGaussianEdgeMaskFilterImpl>(first, second, radius);
565 }
566 
567 ///////////////////////////////////////////////////////////////////////////////
568 
569 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkRRectsGaussianEdgeMaskFilter)
570 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRRectsGaussianEdgeMaskFilterImpl)
571 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
572 
573 ///////////////////////////////////////////////////////////////////////////////
574