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 "SkMaskFilterBase.h"
9#include "SkRRectsGaussianEdgeMaskFilter.h"
10#include "SkReadBuffer.h"
11#include "SkRRect.h"
12#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  */
23class SkRRectsGaussianEdgeMaskFilterImpl : public SkMaskFilterBase {
24public:
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
36    SK_TO_STRING_OVERRIDE()
37    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkRRectsGaussianEdgeMaskFilterImpl)
38
39protected:
40    void flatten(SkWriteBuffer&) const override;
41
42#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
47private:
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
69    const SkVector& radii = rr.getSimpleRadii();
70    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) };
165
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"
195#include "glsl/GrGLSLFragmentProcessor.h"
196#include "glsl/GrGLSLFragmentShaderBuilder.h"
197#include "glsl/GrGLSLProgramDataManager.h"
198#include "glsl/GrGLSLUniformHandler.h"
199#include "SkGr.h"
200
201class RRectsGaussianEdgeFP : public GrFragmentProcessor {
202public:
203    enum Mode {
204        kCircle_Mode,
205        kRect_Mode,
206        kSimpleCircular_Mode,
207    };
208
209    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));
213    }
214
215    const char* name() const override { return "RRectsGaussianEdgeFP"; }
216
217    std::unique_ptr<GrFragmentProcessor> clone() const override {
218        return std::unique_ptr<GrFragmentProcessor>(new RRectsGaussianEdgeFP(*this));
219    }
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:
228    class GLSLRRectsGaussianEdgeFP : public GrGLSLFragmentProcessor {
229    public:
230        GLSLRRectsGaussianEdgeFP() {}
231
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.
246            fragBuilder->codeAppendf("half2 delta = abs(sk_FragCoord.xy - %s.%s);",
247                                     posName, indices);
248
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.
254                    fragBuilder->codeAppendf("half scaledDist = length(delta/%s);", radName);
255                    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(
260                        "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));",
262                        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
278                    fragBuilder->codeAppendf("half xDist = (%s.%c - delta.x) / %s;",
279                                             sizesName, indices[0], radName);
280                    fragBuilder->codeAppendf("half yDist = (%s.%c - delta.y) / %s;",
281                                             sizesName, indices[1], radName);
282                    fragBuilder->codeAppend("half rectDist = clamp(min(xDist, yDist), 0.0, 1.0);");
283
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.
291                    fragBuilder->codeAppendf("half insetDist = min(max(%s, %s.%c),"
292                                                                   "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.
297                    fragBuilder->codeAppendf("half maxValue = insetDist/%s;", radName);
298
299                    fragBuilder->codeAppendf("half2 coneBottom = half2(%s.%c - insetDist,"
300                                                                       "%s.%c - insetDist);",
301                                             sizesName, indices[0], sizesName, indices[1]);
302
303                    fragBuilder->codeAppendf("half2 cornerTop = half2(%s.%c - %s.%c, %s.%c) -"
304                                                                        "coneBottom;",
305                                             sizesName, indices[0], radiiName, indices[0],
306                                             sizesName, indices[1]);
307                    fragBuilder->codeAppendf("half2 cornerRight = half2(%s.%c, %s.%c - %s.%c) -"
308                                                                        "coneBottom;",
309                                             sizesName, indices[0],
310                                             sizesName, indices[1], radiiName, indices[1]);
311
312                    fragBuilder->codeAppend("half2 ptInConeSpace = delta - coneBottom;");
313                    fragBuilder->codeAppend("half distToPtInConeSpace = length(ptInConeSpace);");
314
315                    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;");
319
320                    fragBuilder->codeAppend("half inCone = step(0.0, cross1) *"
321                                                          "step(0.0, cross2);");
322
323                    fragBuilder->codeAppendf("half2 cornerCenterInConeSpace = half2(insetDist -"
324                                                                                 "%s.%c);",
325                                             radiiName, indices[0]);
326
327                    fragBuilder->codeAppend("half2 connectingVec = ptInConeSpace -"
328                                                                        "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
333                    fragBuilder->codeAppend("half b = 2.0 * dot(ptInConeSpace, connectingVec);");
334                    fragBuilder->codeAppendf("half c = dot(connectingVec, connectingVec) - "
335                                                                                   "%s.%c * %s.%c;",
336                                             radiiName, indices[0], radiiName, indices[0]);
337
338                    fragBuilder->codeAppend("half fourAC = 4*c;");
339                    // This max prevents sqrt(-1) when outside the cone
340                    fragBuilder->codeAppend("half bSq = max(b*b, fourAC);");
341
342                    // lop off negative values that are outside the cone
343                    fragBuilder->codeAppend("half coneDist = "
344                                                    "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;
363            fPositionsUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType,
364                                                       "Positions", &positionsUniName);
365            const char* sizesUniName = nullptr;
366            fSizesUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType,
367                                                   kDefault_GrSLPrecision, "Sizes", &sizesUniName);
368            const char* radiiUniName = nullptr;
369            if (fp.fFirstMode == kSimpleCircular_Mode || fp.fSecondMode == kSimpleCircular_Mode) {
370                fRadiiUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType,
371                                                       "Radii", &radiiUniName);
372            }
373            const char* radUniName = nullptr;
374            fRadiusUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType,
375                                                    "Radius", &radUniName);
376
377            fragBuilder->codeAppend("half firstDist;");
378            fragBuilder->codeAppend("{");
379            this->emitModeCode(fp.firstMode(), fragBuilder,
380                               positionsUniName, sizesUniName, radiiUniName,
381                               radUniName, "firstDist", "xy");
382            fragBuilder->codeAppend("}");
383
384            fragBuilder->codeAppend("half secondDist;");
385            fragBuilder->codeAppend("{");
386            this->emitModeCode(fp.secondMode(), fragBuilder,
387                               positionsUniName, sizesUniName, radiiUniName,
388                               radUniName, "secondDist", "zw");
389            fragBuilder->codeAppend("}");
390
391            fragBuilder->codeAppend("half2 distVec = half2(1.0 - firstDist, 1.0 - secondDist);");
392
393            // Finally use the distance to apply the Gaussian edge
394            fragBuilder->codeAppend("half factor = clamp(length(distVec), 0.0, 1.0);");
395            fragBuilder->codeAppend("factor = exp(-factor * factor * 4.0) - 0.018;");
396            fragBuilder->codeAppendf("%s = factor*%s;",
397                                     args.fOutputColor, args.fInputColor);
398        }
399
400        static void GenKey(const GrProcessor& proc, const GrShaderCaps&, GrProcessorKeyBuilder* b) {
401            const RRectsGaussianEdgeFP& fp = proc.cast<RRectsGaussianEdgeFP>();
402
403            b->add32(fp.firstMode() | (fp.secondMode() << 4));
404        }
405
406    protected:
407        void onSetData(const GrGLSLProgramDataManager& pdman,
408                       const GrFragmentProcessor& proc) override {
409            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
420            pdman.set4f(fSizesUni,
421                        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.
430                pdman.set4f(fRadiiUni,
431                            first.getSimpleRadii().fX,  first.getSimpleRadii().fY,
432                            second.getSimpleRadii().fX, second.getSimpleRadii().fY);
433            }
434
435            pdman.set1f(fRadiusUni, edgeFP.radius());
436        }
437
438    private:
439        // The centers of the two round rects (x1, y1, x2, y2)
440        GrGLSLProgramDataManager::UniformHandle fPositionsUni;
441
442        // The half widths and half heights of the two round rects (w1/2, h1/2, w2/2, h2/2)
443        // For circles we still upload both width & height to simplify things
444        GrGLSLProgramDataManager::UniformHandle fSizesUni;
445
446        // The corner radii of the two round rects (rx1, ry1, rx2, ry2)
447        // We upload both the x&y radii (although they are currently always the same) to make
448        // the indexing in the shader code simpler. In some future world we could also support
449        // non-circular corner round rects & ellipses.
450        GrGLSLProgramDataManager::UniformHandle fRadiiUni;
451
452        // The radius parameters (radius)
453        GrGLSLProgramDataManager::UniformHandle fRadiusUni;
454
455        typedef GrGLSLFragmentProcessor INHERITED;
456    };
457
458    void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override {
459        GLSLRRectsGaussianEdgeFP::GenKey(*this, caps, b);
460    }
461
462    RRectsGaussianEdgeFP(const SkRRect& first, const SkRRect& second, SkScalar radius)
463            : INHERITED(kRRectsGaussianEdgeFP_ClassID,
464                        kCompatibleWithCoverageAsAlpha_OptimizationFlag)
465            , fFirst(first)
466            , fSecond(second)
467            , fRadius(radius) {
468
469        fFirstMode = ComputeMode(fFirst);
470        fSecondMode = ComputeMode(fSecond);
471    }
472    RRectsGaussianEdgeFP(const RRectsGaussianEdgeFP& that)
473            : INHERITED(kRRectsGaussianEdgeFP_ClassID,
474                        kCompatibleWithCoverageAsAlpha_OptimizationFlag)
475            , fFirst(that.fFirst)
476            , fFirstMode(that.fFirstMode)
477            , fSecond(that.fSecond)
478            , fSecondMode(that.fSecondMode)
479            , fRadius(that.fRadius) {
480    }
481
482    static Mode ComputeMode(const SkRRect& rr) {
483        if (rr.isCircle()) {
484            return kCircle_Mode;
485        } else if (rr.isRect()) {
486            return kRect_Mode;
487        } else {
488            SkASSERT(rr.isSimpleCircular());
489            return kSimpleCircular_Mode;
490        }
491    }
492
493    GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
494        return new GLSLRRectsGaussianEdgeFP;
495    }
496
497    bool onIsEqual(const GrFragmentProcessor& proc) const override {
498        const RRectsGaussianEdgeFP& edgeFP = proc.cast<RRectsGaussianEdgeFP>();
499        return fFirst  == edgeFP.fFirst &&
500               fSecond == edgeFP.fSecond &&
501               fRadius == edgeFP.fRadius;
502    }
503
504    SkRRect  fFirst;
505    Mode     fFirstMode;
506    SkRRect  fSecond;
507    Mode     fSecondMode;
508    SkScalar fRadius;
509
510    typedef GrFragmentProcessor INHERITED;
511};
512
513////////////////////////////////////////////////////////////////////////////
514
515std::unique_ptr<GrFragmentProcessor>
516SkRRectsGaussianEdgeMaskFilterImpl::onAsFragmentProcessor(const GrFPArgs& args) const {
517    return RRectsGaussianEdgeFP::Make(fFirst, fSecond, fRadius);
518}
519
520#endif
521
522////////////////////////////////////////////////////////////////////////////
523
524#ifndef SK_IGNORE_TO_STRING
525void SkRRectsGaussianEdgeMaskFilterImpl::toString(SkString* str) const {
526    str->appendf("RRectsGaussianEdgeMaskFilter: ()");
527}
528#endif
529
530sk_sp<SkFlattenable> SkRRectsGaussianEdgeMaskFilterImpl::CreateProc(SkReadBuffer& buf) {
531    SkRect rect1, rect2;
532
533    buf.readRect(&rect1);
534    SkScalar xRad1 = buf.readScalar();
535    SkScalar yRad1 = buf.readScalar();
536
537    buf.readRect(&rect2);
538    SkScalar xRad2 = buf.readScalar();
539    SkScalar yRad2 = buf.readScalar();
540
541    SkScalar radius = buf.readScalar();
542
543    return sk_make_sp<SkRRectsGaussianEdgeMaskFilterImpl>(SkRRect::MakeRectXY(rect1, xRad1, yRad1),
544                                                          SkRRect::MakeRectXY(rect2, xRad2, yRad2),
545                                                          radius);
546}
547
548void SkRRectsGaussianEdgeMaskFilterImpl::flatten(SkWriteBuffer& buf) const {
549    INHERITED::flatten(buf);
550
551    SkASSERT(fFirst.isRect() || fFirst.isCircle() || fFirst.isSimpleCircular());
552    buf.writeRect(fFirst.rect());
553    const SkVector& radii1 = fFirst.getSimpleRadii();
554    buf.writeScalar(radii1.fX);
555    buf.writeScalar(radii1.fY);
556
557    SkASSERT(fSecond.isRect() || fSecond.isCircle() || fSecond.isSimpleCircular());
558    buf.writeRect(fSecond.rect());
559    const SkVector& radii2 = fSecond.getSimpleRadii();
560    buf.writeScalar(radii2.fX);
561    buf.writeScalar(radii2.fY);
562
563    buf.writeScalar(fRadius);
564}
565
566///////////////////////////////////////////////////////////////////////////////
567
568sk_sp<SkMaskFilter> SkRRectsGaussianEdgeMaskFilter::Make(const SkRRect& first,
569                                                         const SkRRect& second,
570                                                         SkScalar radius) {
571    if ((!first.isRect()  && !first.isCircle()  && !first.isSimpleCircular()) ||
572        (!second.isRect() && !second.isCircle() && !second.isSimpleCircular())) {
573        // we only deal with the shapes where the x & y radii are equal
574        // and the same for all four corners
575        return nullptr;
576    }
577
578    return sk_make_sp<SkRRectsGaussianEdgeMaskFilterImpl>(first, second, radius);
579}
580
581///////////////////////////////////////////////////////////////////////////////
582
583SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkRRectsGaussianEdgeMaskFilter)
584SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRRectsGaussianEdgeMaskFilterImpl)
585SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
586
587///////////////////////////////////////////////////////////////////////////////
588