• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2006 The Android Open Source Project
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "src/core/SkBlurMaskFilterImpl.h"
9 
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkBlurTypes.h"
12 #include "include/core/SkFlattenable.h"
13 #include "include/core/SkImageFilter.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkMaskFilter.h"
16 #include "include/core/SkMatrix.h"
17 #include "include/core/SkPaint.h"
18 #include "include/core/SkPath.h"
19 #include "include/core/SkPathBuilder.h"
20 #include "include/core/SkPathTypes.h"
21 #include "include/core/SkPixmap.h"
22 #include "include/core/SkPoint.h"
23 #include "include/core/SkRRect.h"
24 #include "include/core/SkRect.h"
25 #include "include/core/SkRefCnt.h"
26 #include "include/core/SkScalar.h"
27 #include "include/effects/SkImageFilters.h"
28 #include "include/private/base/SkAlign.h"
29 #include "include/private/base/SkAssert.h"
30 #include "include/private/base/SkFloatingPoint.h"
31 #include "include/private/base/SkTemplates.h"
32 #include "src/base/SkTLazy.h"
33 #include "src/core/SkBlitter_A8.h"
34 #include "src/core/SkBlurMask.h"
35 #include "src/core/SkCachedData.h"
36 #include "src/core/SkDrawBase.h"
37 #include "src/core/SkMask.h"
38 #include "src/core/SkMaskCache.h"
39 #include "src/core/SkMaskFilterBase.h"
40 #include "src/core/SkRasterClip.h"
41 #include "src/core/SkReadBuffer.h"
42 #include "src/core/SkResourceCache.h"
43 #include "src/core/SkWriteBuffer.h"
44 
45 #include <algorithm>
46 #include <cstdint>
47 #include <cstring>
48 #include <utility>
49 
SkBlurMaskFilterImpl(SkScalar sigma,SkBlurStyle style,bool respectCTM)50 SkBlurMaskFilterImpl::SkBlurMaskFilterImpl(SkScalar sigma, SkBlurStyle style, bool respectCTM)
51     : fSigma(sigma)
52     , fBlurStyle(style)
53     , fRespectCTM(respectCTM) {
54     SkASSERT(fSigma > 0);
55     SkASSERT((unsigned)style <= kLastEnum_SkBlurStyle);
56 }
57 
getFormat() const58 SkMask::Format SkBlurMaskFilterImpl::getFormat() const {
59     return SkMask::kA8_Format;
60 }
61 
asABlur(BlurRec * rec) const62 bool SkBlurMaskFilterImpl::asABlur(BlurRec* rec) const {
63     if (this->ignoreXform()) {
64         return false;
65     }
66 
67     if (rec) {
68         rec->fSigma = fSigma;
69         rec->fStyle = fBlurStyle;
70     }
71     return true;
72 }
73 
asImageFilter(const SkMatrix & ctm) const74 sk_sp<SkImageFilter> SkBlurMaskFilterImpl::asImageFilter(const SkMatrix& ctm) const {
75     float sigma = fSigma;
76     if (this->ignoreXform()) {
77         // This is analogous to computeXformedSigma(), but it might be more correct to wrap the
78         // blur image filter in a local matrix with ctm^-1, or to control the skif::Mapping when
79         // the mask filter layer is restored. This is inaccurate when 'ctm' has skew or perspective
80         const float ctmScaleFactor = fSigma / ctm.mapRadius(fSigma);
81         sigma *= ctmScaleFactor;
82     }
83 
84     // The null input image filter will be bound to the original coverage mask.
85     sk_sp<SkImageFilter> filter = SkImageFilters::Blur(sigma, sigma, nullptr);
86     // Combine the original coverage mask (src) and the blurred coverage mask (dst)
87     switch(fBlurStyle) {
88         case kInner_SkBlurStyle: //  dst = dst * src
89                                  //      = 0 * src + src * dst
90             return SkImageFilters::Blend(SkBlendMode::kDstIn, std::move(filter), nullptr);
91         case kSolid_SkBlurStyle: //  dst = src + dst - src * dst
92                                  //      = 1 * src + (1 - src) * dst
93             return SkImageFilters::Blend(SkBlendMode::kSrcOver, std::move(filter), nullptr);
94         case kOuter_SkBlurStyle: //  dst = dst * (1 - src)
95                                  //      = 0 * src + (1 - src) * dst
96             return SkImageFilters::Blend(SkBlendMode::kDstOut, std::move(filter), nullptr);
97         case kNormal_SkBlurStyle:
98             return filter;
99     }
100     SkUNREACHABLE;
101 }
102 
computeXformedSigma(const SkMatrix & ctm) const103 SkScalar SkBlurMaskFilterImpl::computeXformedSigma(const SkMatrix& ctm) const {
104     constexpr SkScalar kMaxBlurSigma = SkIntToScalar(128);
105     SkScalar xformedSigma = this->ignoreXform() ? fSigma : ctm.mapRadius(fSigma);
106     return std::min(xformedSigma, kMaxBlurSigma);
107 }
108 
filterMask(SkMaskBuilder * dst,const SkMask & src,const SkMatrix & matrix,SkIPoint * margin) const109 bool SkBlurMaskFilterImpl::filterMask(SkMaskBuilder* dst, const SkMask& src,
110                                       const SkMatrix& matrix,
111                                       SkIPoint* margin) const {
112     SkScalar sigma = this->computeXformedSigma(matrix);
113     return SkBlurMask::BoxBlur(dst, src, sigma, fBlurStyle, margin);
114 }
115 
filterRectMask(SkMaskBuilder * dst,const SkRect & r,const SkMatrix & matrix,SkIPoint * margin,SkMaskBuilder::CreateMode createMode) const116 bool SkBlurMaskFilterImpl::filterRectMask(SkMaskBuilder* dst, const SkRect& r,
117                                           const SkMatrix& matrix,
118                                           SkIPoint* margin,
119                                           SkMaskBuilder::CreateMode createMode) const {
120     SkScalar sigma = computeXformedSigma(matrix);
121 
122     return SkBlurMask::BlurRect(sigma, dst, r, fBlurStyle, margin, createMode);
123 }
124 
filterRRectMask(SkMaskBuilder * dst,const SkRRect & r,const SkMatrix & matrix,SkIPoint * margin,SkMaskBuilder::CreateMode createMode) const125 bool SkBlurMaskFilterImpl::filterRRectMask(SkMaskBuilder* dst, const SkRRect& r,
126                                            const SkMatrix& matrix,
127                                            SkIPoint* margin,
128                                            SkMaskBuilder::CreateMode createMode) const {
129     SkScalar sigma = computeXformedSigma(matrix);
130 
131     return SkBlurMask::BlurRRect(sigma, dst, r, fBlurStyle, margin, createMode);
132 }
133 
prepare_to_draw_into_mask(const SkRect & bounds,SkMaskBuilder * mask)134 static bool prepare_to_draw_into_mask(const SkRect& bounds, SkMaskBuilder* mask) {
135     SkASSERT(mask != nullptr);
136 
137     mask->bounds() = bounds.roundOut();
138     mask->rowBytes() = SkAlign4(mask->fBounds.width());
139     mask->format() = SkMask::kA8_Format;
140     const size_t size = mask->computeImageSize();
141     mask->image() = SkMaskBuilder::AllocImage(size, SkMaskBuilder::kZeroInit_Alloc);
142     if (nullptr == mask->fImage) {
143         return false;
144     }
145     return true;
146 }
147 
draw_into_mask(SkMaskBuilder * mask,const SkRect & bounds,Proc proc)148 template <typename Proc> bool draw_into_mask(SkMaskBuilder* mask, const SkRect& bounds, Proc proc) {
149     if (!prepare_to_draw_into_mask(bounds, mask)) {
150         return false;
151     }
152 
153     const int dx = mask->fBounds.fLeft;
154     const int dy = mask->fBounds.fTop;
155     SkRasterClip rclip(mask->fBounds);
156     rclip.setRect(mask->fBounds.makeOffset(-dx, -dy));
157 
158     SkASSERT(mask->fFormat == SkMask::kA8_Format);
159     auto info = SkImageInfo::MakeA8(mask->fBounds.width(), mask->fBounds.height());
160     auto pm = SkPixmap(info, mask->fImage, mask->fRowBytes);
161 
162     SkMatrix ctm = SkMatrix::Translate(-SkIntToScalar(dx), -SkIntToScalar(dy));
163 
164     SkDrawBase draw;
165     draw.fBlitterChooser = SkA8Blitter_Choose;
166     draw.fCTM = &ctm;
167     draw.fDst = pm;
168     draw.fRC  = &rclip;
169 
170     SkPaint paint;
171     paint.setAntiAlias(true);
172 
173     proc(draw, paint);
174     return true;
175 }
176 
draw_rects_into_mask(const SkRect rects[],int count,SkMaskBuilder * mask)177 static bool draw_rects_into_mask(const SkRect rects[], int count, SkMaskBuilder* mask) {
178     return draw_into_mask(mask, rects[0], [&](SkDrawBase& draw, const SkPaint& paint) {
179         if (1 == count) {
180             draw.drawRect(rects[0], paint);
181         } else {
182             // todo: do I need a fast way to do this?
183             SkPath path = SkPathBuilder().addRect(rects[0])
184                                          .addRect(rects[1])
185                                          .setFillType(SkPathFillType::kEvenOdd)
186                                          .detach();
187             draw.drawPath(path, paint);
188         }
189     });
190 }
191 
draw_rrect_into_mask(const SkRRect rrect,SkMaskBuilder * mask)192 static bool draw_rrect_into_mask(const SkRRect rrect, SkMaskBuilder* mask) {
193     return draw_into_mask(mask, rrect.rect(), [&](SkDrawBase& draw, const SkPaint& paint) {
194         draw.drawRRect(rrect, paint);
195     });
196 }
197 
rect_exceeds(const SkRect & r,SkScalar v)198 static bool rect_exceeds(const SkRect& r, SkScalar v) {
199     return r.fLeft < -v || r.fTop < -v || r.fRight > v || r.fBottom > v ||
200            r.width() > v || r.height() > v;
201 }
202 
copy_mask_to_cacheddata(SkMaskBuilder * mask)203 static SkCachedData* copy_mask_to_cacheddata(SkMaskBuilder* mask) {
204     const size_t size = mask->computeTotalImageSize();
205     SkCachedData* data = SkResourceCache::NewCachedData(size);
206     if (data) {
207         memcpy(data->writable_data(), mask->fImage, size);
208         SkMaskBuilder::FreeImage(mask->image());
209         mask->image() = (uint8_t*)data->writable_data();
210     }
211     return data;
212 }
213 
find_cached_rrect(SkTLazy<SkMask> * mask,SkScalar sigma,SkBlurStyle style,const SkRRect & rrect)214 static SkCachedData* find_cached_rrect(SkTLazy<SkMask>* mask, SkScalar sigma, SkBlurStyle style,
215                                        const SkRRect& rrect) {
216     return SkMaskCache::FindAndRef(sigma, style, rrect, mask);
217 }
218 
add_cached_rrect(SkMaskBuilder * mask,SkScalar sigma,SkBlurStyle style,const SkRRect & rrect)219 static SkCachedData* add_cached_rrect(SkMaskBuilder* mask, SkScalar sigma, SkBlurStyle style,
220                                       const SkRRect& rrect) {
221     SkCachedData* cache = copy_mask_to_cacheddata(mask);
222     if (cache) {
223         SkMaskCache::Add(sigma, style, rrect, *mask, cache);
224     }
225     return cache;
226 }
227 
find_cached_rects(SkTLazy<SkMask> * mask,SkScalar sigma,SkBlurStyle style,const SkRect rects[],int count)228 static SkCachedData* find_cached_rects(SkTLazy<SkMask>* mask, SkScalar sigma, SkBlurStyle style,
229                                        const SkRect rects[], int count) {
230     return SkMaskCache::FindAndRef(sigma, style, rects, count, mask);
231 }
232 
add_cached_rects(SkMaskBuilder * mask,SkScalar sigma,SkBlurStyle style,const SkRect rects[],int count)233 static SkCachedData* add_cached_rects(SkMaskBuilder* mask, SkScalar sigma, SkBlurStyle style,
234                                       const SkRect rects[], int count) {
235     SkCachedData* cache = copy_mask_to_cacheddata(mask);
236     if (cache) {
237         SkMaskCache::Add(sigma, style, rects, count, *mask, cache);
238     }
239     return cache;
240 }
241 
242 static const bool c_analyticBlurRRect{true};
243 
244 SkMaskFilterBase::FilterReturn
filterRRectToNine(const SkRRect & rrect,const SkMatrix & matrix,const SkIRect & clipBounds,SkTLazy<NinePatch> * patch) const245 SkBlurMaskFilterImpl::filterRRectToNine(const SkRRect& rrect, const SkMatrix& matrix,
246                                         const SkIRect& clipBounds,
247                                         SkTLazy<NinePatch>* patch) const {
248     SkASSERT(patch != nullptr);
249     switch (rrect.getType()) {
250         case SkRRect::kEmpty_Type:
251             // Nothing to draw.
252             return kFalse_FilterReturn;
253 
254         case SkRRect::kRect_Type:
255             // We should have caught this earlier.
256             SkASSERT(false);
257             [[fallthrough]];
258         case SkRRect::kOval_Type:
259             // The nine patch special case does not handle ovals, and we
260             // already have code for rectangles.
261             return kUnimplemented_FilterReturn;
262 
263         // These three can take advantage of this fast path.
264         case SkRRect::kSimple_Type:
265         case SkRRect::kNinePatch_Type:
266         case SkRRect::kComplex_Type:
267             break;
268     }
269 
270     // TODO: report correct metrics for innerstyle, where we do not grow the
271     // total bounds, but we do need an inset the size of our blur-radius
272     if (kInner_SkBlurStyle == fBlurStyle) {
273         return kUnimplemented_FilterReturn;
274     }
275 
276     // TODO: take clipBounds into account to limit our coordinates up front
277     // for now, just skip too-large src rects (to take the old code path).
278     if (rect_exceeds(rrect.rect(), SkIntToScalar(32767))) {
279         return kUnimplemented_FilterReturn;
280     }
281 
282     SkIPoint margin;
283     SkMaskBuilder srcM(nullptr, rrect.rect().roundOut(), 0, SkMask::kA8_Format), dstM;
284 
285     bool filterResult = false;
286     if (c_analyticBlurRRect) {
287         // special case for fast round rect blur
288         // don't actually do the blur the first time, just compute the correct size
289         filterResult = this->filterRRectMask(&dstM, rrect, matrix, &margin,
290                                              SkMaskBuilder::kJustComputeBounds_CreateMode);
291     }
292 
293     if (!filterResult) {
294         filterResult = this->filterMask(&dstM, srcM, matrix, &margin);
295     }
296 
297     if (!filterResult) {
298         return kFalse_FilterReturn;
299     }
300 
301     // Now figure out the appropriate width and height of the smaller round rectangle
302     // to stretch. It will take into account the larger radius per side as well as double
303     // the margin, to account for inner and outer blur.
304     const SkVector& UL = rrect.radii(SkRRect::kUpperLeft_Corner);
305     const SkVector& UR = rrect.radii(SkRRect::kUpperRight_Corner);
306     const SkVector& LR = rrect.radii(SkRRect::kLowerRight_Corner);
307     const SkVector& LL = rrect.radii(SkRRect::kLowerLeft_Corner);
308 
309     const SkScalar leftUnstretched = std::max(UL.fX, LL.fX) + SkIntToScalar(2 * margin.fX);
310     const SkScalar rightUnstretched = std::max(UR.fX, LR.fX) + SkIntToScalar(2 * margin.fX);
311 
312     // Extra space in the middle to ensure an unchanging piece for stretching. Use 3 to cover
313     // any fractional space on either side plus 1 for the part to stretch.
314     const SkScalar stretchSize = SkIntToScalar(3);
315 
316     const SkScalar totalSmallWidth = leftUnstretched + rightUnstretched + stretchSize;
317     if (totalSmallWidth >= rrect.rect().width()) {
318         // There is no valid piece to stretch.
319         return kUnimplemented_FilterReturn;
320     }
321 
322     const SkScalar topUnstretched = std::max(UL.fY, UR.fY) + SkIntToScalar(2 * margin.fY);
323     const SkScalar bottomUnstretched = std::max(LL.fY, LR.fY) + SkIntToScalar(2 * margin.fY);
324 
325     const SkScalar totalSmallHeight = topUnstretched + bottomUnstretched + stretchSize;
326     if (totalSmallHeight >= rrect.rect().height()) {
327         // There is no valid piece to stretch.
328         return kUnimplemented_FilterReturn;
329     }
330 
331     SkRect smallR = SkRect::MakeWH(totalSmallWidth, totalSmallHeight);
332 
333     SkRRect smallRR;
334     SkVector radii[4];
335     radii[SkRRect::kUpperLeft_Corner] = UL;
336     radii[SkRRect::kUpperRight_Corner] = UR;
337     radii[SkRRect::kLowerRight_Corner] = LR;
338     radii[SkRRect::kLowerLeft_Corner] = LL;
339     smallRR.setRectRadii(smallR, radii);
340 
341     const SkScalar sigma = this->computeXformedSigma(matrix);
342     SkTLazy<SkMask> cachedMask;
343     SkCachedData* cache = find_cached_rrect(&cachedMask, sigma, fBlurStyle, smallRR);
344     if (!cache) {
345         SkMaskBuilder filterM;
346         bool analyticBlurWorked = false;
347         if (c_analyticBlurRRect) {
348             analyticBlurWorked =
349                 this->filterRRectMask(&filterM, smallRR, matrix, &margin,
350                                       SkMaskBuilder::kComputeBoundsAndRenderImage_CreateMode);
351         }
352 
353         if (!analyticBlurWorked) {
354             if (!draw_rrect_into_mask(smallRR, &srcM)) {
355                 return kFalse_FilterReturn;
356             }
357             SkAutoMaskFreeImage amf(srcM.image());
358 
359             if (!this->filterMask(&filterM, srcM, matrix, &margin)) {
360                 return kFalse_FilterReturn;
361             }
362         }
363         cache = add_cached_rrect(&filterM, sigma, fBlurStyle, smallRR);
364         cachedMask.init(filterM);
365     }
366 
367     SkIRect bounds = cachedMask->fBounds;
368     bounds.offsetTo(0, 0);
369     patch->init(SkMask{cachedMask->fImage, bounds, cachedMask->fRowBytes, cachedMask->fFormat},
370                 dstM.fBounds,
371                 SkIPoint{SkScalarCeilToInt(leftUnstretched) + 1,
372                          SkScalarCeilToInt(topUnstretched) + 1},
373                 cache); // transfer ownership to patch
374     return kTrue_FilterReturn;
375 }
376 
377 // Use the faster analytic blur approach for ninepatch rects
378 static const bool c_analyticBlurNinepatch{true};
379 
380 SkMaskFilterBase::FilterReturn
filterRectsToNine(const SkRect rects[],int count,const SkMatrix & matrix,const SkIRect & clipBounds,SkTLazy<NinePatch> * patch) const381 SkBlurMaskFilterImpl::filterRectsToNine(const SkRect rects[], int count,
382                                         const SkMatrix& matrix,
383                                         const SkIRect& clipBounds,
384                                         SkTLazy<NinePatch>* patch) const {
385     if (count < 1 || count > 2) {
386         return kUnimplemented_FilterReturn;
387     }
388 
389     // TODO: report correct metrics for innerstyle, where we do not grow the
390     // total bounds, but we do need an inset the size of our blur-radius
391     if (kInner_SkBlurStyle == fBlurStyle || kOuter_SkBlurStyle == fBlurStyle) {
392         return kUnimplemented_FilterReturn;
393     }
394 
395     // TODO: take clipBounds into account to limit our coordinates up front
396     // for now, just skip too-large src rects (to take the old code path).
397     if (rect_exceeds(rects[0], SkIntToScalar(32767))) {
398         return kUnimplemented_FilterReturn;
399     }
400 
401     SkIPoint margin;
402     SkMaskBuilder srcM(nullptr, rects[0].roundOut(), 0, SkMask::kA8_Format), dstM;
403 
404     bool filterResult = false;
405     if (count == 1 && c_analyticBlurNinepatch) {
406         // special case for fast rect blur
407         // don't actually do the blur the first time, just compute the correct size
408         filterResult = this->filterRectMask(&dstM, rects[0], matrix, &margin,
409                                             SkMaskBuilder::kJustComputeBounds_CreateMode);
410     } else {
411         filterResult = this->filterMask(&dstM, srcM, matrix, &margin);
412     }
413 
414     if (!filterResult) {
415         return kFalse_FilterReturn;
416     }
417 
418     /*
419      *  smallR is the smallest version of 'rect' that will still guarantee that
420      *  we get the same blur results on all edges, plus 1 center row/col that is
421      *  representative of the extendible/stretchable edges of the ninepatch.
422      *  Since our actual edge may be fractional we inset 1 more to be sure we
423      *  don't miss any interior blur.
424      *  x is an added pixel of blur, and { and } are the (fractional) edge
425      *  pixels from the original rect.
426      *
427      *   x x { x x .... x x } x x
428      *
429      *  Thus, in this case, we inset by a total of 5 (on each side) beginning
430      *  with our outer-rect (dstM.fBounds)
431      */
432     SkRect smallR[2];
433     SkIPoint center;
434 
435     // +2 is from +1 for each edge (to account for possible fractional edges
436     int smallW = dstM.fBounds.width() - srcM.fBounds.width() + 2;
437     int smallH = dstM.fBounds.height() - srcM.fBounds.height() + 2;
438     SkIRect innerIR;
439 
440     if (1 == count) {
441         innerIR = srcM.fBounds;
442         center.set(smallW, smallH);
443     } else {
444         SkASSERT(2 == count);
445         rects[1].roundIn(&innerIR);
446         center.set(smallW + (innerIR.left() - srcM.fBounds.left()),
447                    smallH + (innerIR.top() - srcM.fBounds.top()));
448     }
449 
450     // +1 so we get a clean, stretchable, center row/col
451     smallW += 1;
452     smallH += 1;
453 
454     // we want the inset amounts to be integral, so we don't change any
455     // fractional phase on the fRight or fBottom of our smallR.
456     const SkScalar dx = SkIntToScalar(innerIR.width() - smallW);
457     const SkScalar dy = SkIntToScalar(innerIR.height() - smallH);
458     if (dx < 0 || dy < 0) {
459         // we're too small, relative to our blur, to break into nine-patch,
460         // so we ask to have our normal filterMask() be called.
461         return kUnimplemented_FilterReturn;
462     }
463 
464     smallR[0].setLTRB(rects[0].left(),       rects[0].top(),
465                       rects[0].right() - dx, rects[0].bottom() - dy);
466     if (smallR[0].width() < 2 || smallR[0].height() < 2) {
467         return kUnimplemented_FilterReturn;
468     }
469     if (2 == count) {
470         smallR[1].setLTRB(rects[1].left(), rects[1].top(),
471                           rects[1].right() - dx, rects[1].bottom() - dy);
472         SkASSERT(!smallR[1].isEmpty());
473     }
474 
475     const SkScalar sigma = this->computeXformedSigma(matrix);
476     SkTLazy<SkMask> cachedMask;
477     SkCachedData* cache = find_cached_rects(&cachedMask, sigma, fBlurStyle, smallR, count);
478     if (!cache) {
479         SkMaskBuilder filterM;
480         if (count > 1 || !c_analyticBlurNinepatch) {
481             if (!draw_rects_into_mask(smallR, count, &srcM)) {
482                 return kFalse_FilterReturn;
483             }
484 
485             SkAutoMaskFreeImage amf(srcM.image());
486 
487             if (!this->filterMask(&filterM, srcM, matrix, &margin)) {
488                 return kFalse_FilterReturn;
489             }
490         } else {
491             if (!this->filterRectMask(&filterM, smallR[0], matrix, &margin,
492                                       SkMaskBuilder::kComputeBoundsAndRenderImage_CreateMode)) {
493                 return kFalse_FilterReturn;
494             }
495         }
496         cache = add_cached_rects(&filterM, sigma, fBlurStyle, smallR, count);
497         cachedMask.init(filterM);
498     }
499     SkIRect bounds = cachedMask->fBounds;
500     bounds.offsetTo(0, 0);
501     patch->init(SkMask{cachedMask->fImage, bounds, cachedMask->fRowBytes, cachedMask->fFormat},
502                 dstM.fBounds, center, cache); // transfer ownership to patch
503     return kTrue_FilterReturn;
504 }
505 
computeFastBounds(const SkRect & src,SkRect * dst) const506 void SkBlurMaskFilterImpl::computeFastBounds(const SkRect& src,
507                                              SkRect* dst) const {
508     // TODO: if we're doing kInner blur, should we return a different outset?
509     //       i.e. pad == 0 ?
510 
511     SkScalar pad = 3.0f * fSigma;
512 
513     dst->setLTRB(src.fLeft  - pad, src.fTop    - pad,
514                  src.fRight + pad, src.fBottom + pad);
515 }
516 
CreateProc(SkReadBuffer & buffer)517 sk_sp<SkFlattenable> SkBlurMaskFilterImpl::CreateProc(SkReadBuffer& buffer) {
518     const SkScalar sigma = buffer.readScalar();
519     SkBlurStyle style = buffer.read32LE(kLastEnum_SkBlurStyle);
520 
521     uint32_t flags = buffer.read32LE(0x3);  // historically we only recorded 2 bits
522     bool respectCTM = !(flags & 1); // historically we stored ignoreCTM in low bit
523 
524     return SkMaskFilter::MakeBlur((SkBlurStyle)style, sigma, respectCTM);
525 }
526 
flatten(SkWriteBuffer & buffer) const527 void SkBlurMaskFilterImpl::flatten(SkWriteBuffer& buffer) const {
528     buffer.writeScalar(fSigma);
529     buffer.writeUInt(fBlurStyle);
530     buffer.writeUInt(!fRespectCTM); // historically we recorded ignoreCTM
531 }
532 
sk_register_blur_maskfilter_createproc()533 void sk_register_blur_maskfilter_createproc() { SK_REGISTER_FLATTENABLE(SkBlurMaskFilterImpl); }
534 
MakeBlur(SkBlurStyle style,SkScalar sigma,bool respectCTM)535 sk_sp<SkMaskFilter> SkMaskFilter::MakeBlur(SkBlurStyle style, SkScalar sigma, bool respectCTM) {
536     if (SkIsFinite(sigma) && sigma > 0) {
537         return sk_sp<SkMaskFilter>(new SkBlurMaskFilterImpl(sigma, style, respectCTM));
538     }
539     return nullptr;
540 }
541