Extract code for safely creating SkMasks.

This is in preparation for adding a faster small radii blur.

Change-Id: I8afd4642ebafa63a45031a7a0bd7a720951dc102
Reviewed-on: https://skia-review.googlesource.com/70040
Reviewed-by: Florin Malita <fmalita@chromium.org>
Commit-Queue: Herb Derby <herb@google.com>
diff --git a/src/core/SkMaskBlurFilter.cpp b/src/core/SkMaskBlurFilter.cpp
index 282f395..604a44b 100644
--- a/src/core/SkMaskBlurFilter.cpp
+++ b/src/core/SkMaskBlurFilter.cpp
@@ -542,6 +542,33 @@
     return (3 * fSigmaW <= 1) && (3 * fSigmaH <= 1);
 }
 
+static SkMask prepare_destination(int radiusX, int radiusY, const SkMask& src) {
+    SkSafeMath safe;
+
+    SkMask dst;
+    // dstW = srcW + 2 * radiusX;
+    int dstW = safe.add(src.fBounds.width(), safe.add(radiusX, radiusX));
+    // dstH = srcH + 2 * radiusY;
+    int dstH = safe.add(src.fBounds.height(), safe.add(radiusY, radiusY));
+
+    dst.fBounds.set(0, 0, dstW, dstH);
+    dst.fBounds.offset(src.fBounds.x(), src.fBounds.y());
+    dst.fBounds.offset(-SkTo<int32_t>(radiusX), -SkTo<int32_t>(radiusY));
+
+    dst.fImage = nullptr;
+    dst.fRowBytes = SkTo<uint32_t>(dstW);
+    dst.fFormat = SkMask::kA8_Format;
+
+    size_t toAlloc = safe.mul(dstW, dstH);
+
+    if (safe) {
+        dst.fImage = SkMask::AllocImage(toAlloc);
+    } else{
+        dst.fBounds = SkIRect::MakeEmpty();
+    }
+    return dst;
+}
+
 SkIPoint SkMaskBlurFilter::blur(const SkMask& src, SkMask* dst) const {
 
     // 1024 is a place holder guess until more analysis can be done.
@@ -550,38 +577,23 @@
     PlanningInterface* planW = make_plan(&alloc, fSigmaW);
     PlanningInterface* planH = make_plan(&alloc, fSigmaH);
 
-    size_t borderW = planW->border();
-    size_t borderH = planH->border();
-
-    auto srcW = SkTo<size_t>(src.fBounds.width());
-    auto srcH = SkTo<size_t>(src.fBounds.height());
-
-    SkSafeMath safe;
-
-    // size_t dstW = srcW + 2 * borderW;
-    size_t dstW = safe.add(srcW, safe.add(borderW, borderW));
-    //size_t dstH = srcH + 2 * borderH;
-    size_t dstH = safe.add(srcH, safe.add(borderH, borderH));
-
-    dst->fBounds.set(0, 0, dstW, dstH);
-    dst->fBounds.offset(src.fBounds.x(), src.fBounds.y());
-    dst->fBounds.offset(-SkTo<int32_t>(borderW), -SkTo<int32_t>(borderH));
-
-    dst->fImage = nullptr;
-    dst->fRowBytes = SkTo<uint32_t>(dstW);
-    dst->fFormat = SkMask::kA8_Format;
+    size_t borderW = planW->border(),
+           borderH = planH->border();
 
     if (src.fImage == nullptr) {
         return {SkTo<int32_t>(borderW), SkTo<int32_t>(borderH)};
     }
 
-    size_t toAlloc = safe.mul(dstW, dstH);
-    if (!safe) {
-        dst->fBounds = SkIRect::MakeEmpty();
-        // There is no border offset because we are not drawing.
+    *dst = prepare_destination(borderW, borderH, src);
+    if (dst->fImage == nullptr) {
         return {0, 0};
     }
-    dst->fImage = SkMask::AllocImage(toAlloc);
+
+    auto srcW = SkTo<size_t>(src.fBounds.width());
+    auto srcH = SkTo<size_t>(src.fBounds.height());
+
+    size_t dstW = dst->fBounds.width(),
+           dstH = dst->fBounds.height();
 
     auto bufferSize = std::max(planW->bufferSize(), planH->bufferSize());
     auto buffer = alloc.makeArrayDefault<uint32_t>(bufferSize);