epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2006 The Android Open Source Project |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #include "SkBlurMaskFilter.h" |
| 10 | #include "SkBlurMask.h" |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 11 | #include "SkFlattenableBuffers.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkMaskFilter.h" |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 13 | #include "SkRTConf.h" |
robertphillips@google.com | 0bd80fa | 2013-03-18 17:53:38 +0000 | [diff] [blame] | 14 | #include "SkStringUtils.h" |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame^] | 15 | #include "SkStrokeRec.h" |
| 16 | |
| 17 | #if SK_SUPPORT_GPU |
| 18 | #include "GrContext.h" |
| 19 | #include "GrTexture.h" |
| 20 | #include "effects/GrSimpleTextureEffect.h" |
| 21 | #include "SkGrPixelRef.h" |
| 22 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | |
| 24 | class SkBlurMaskFilterImpl : public SkMaskFilter { |
| 25 | public: |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 26 | SkBlurMaskFilterImpl(SkScalar radius, SkBlurMaskFilter::BlurStyle, |
| 27 | uint32_t flags); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | |
| 29 | // overrides from SkMaskFilter |
reed@google.com | 30711b7 | 2012-12-18 19:18:39 +0000 | [diff] [blame] | 30 | virtual SkMask::Format getFormat() const SK_OVERRIDE; |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 31 | virtual bool filterMask(SkMask* dst, const SkMask& src, const SkMatrix&, |
reed@google.com | 30711b7 | 2012-12-18 19:18:39 +0000 | [diff] [blame] | 32 | SkIPoint* margin) const SK_OVERRIDE; |
skia.committer@gmail.com | 2e71f16 | 2013-03-12 07:12:32 +0000 | [diff] [blame] | 33 | |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame^] | 34 | #if SK_SUPPORT_GPU |
| 35 | virtual bool canFilterMaskGPU(const SkRect& devBounds, |
| 36 | const SkIRect& clipBounds, |
| 37 | const SkMatrix& ctm, |
| 38 | SkRect* maskRect) const SK_OVERRIDE; |
| 39 | virtual bool filterMaskGPU(GrTexture* src, |
| 40 | const SkRect& maskRect, |
| 41 | GrTexture** result, |
| 42 | bool canOverwriteSrc) const; |
| 43 | #endif |
| 44 | |
reed@google.com | 30711b7 | 2012-12-18 19:18:39 +0000 | [diff] [blame] | 45 | virtual void computeFastBounds(const SkRect&, SkRect*) const SK_OVERRIDE; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | |
robertphillips@google.com | 0bd80fa | 2013-03-18 17:53:38 +0000 | [diff] [blame] | 47 | SkDEVCODE(virtual void toString(SkString* str) const SK_OVERRIDE;) |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 48 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkBlurMaskFilterImpl) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 50 | protected: |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 51 | virtual FilterReturn filterRectsToNine(const SkRect[], int count, const SkMatrix&, |
| 52 | const SkIRect& clipBounds, |
reed@google.com | 30711b7 | 2012-12-18 19:18:39 +0000 | [diff] [blame] | 53 | NinePatch*) const SK_OVERRIDE; |
skia.committer@gmail.com | 2e71f16 | 2013-03-12 07:12:32 +0000 | [diff] [blame] | 54 | |
| 55 | bool filterRectMask(SkMask* dstM, const SkRect& r, const SkMatrix& matrix, |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 56 | SkIPoint* margin, SkMask::CreateMode createMode) const; |
skia.committer@gmail.com | 453995e | 2012-11-10 02:01:26 +0000 | [diff] [blame] | 57 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | private: |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame^] | 59 | // To avoid unseemly allocation requests (esp. for finite platforms like |
| 60 | // handset) we limit the radius so something manageable. (as opposed to |
| 61 | // a request like 10,000) |
| 62 | static const SkScalar kMAX_RADIUS; |
| 63 | // This constant approximates the scaling done in the software path's |
| 64 | // "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)). |
| 65 | // IMHO, it actually should be 1: we blur "less" than we should do |
| 66 | // according to the CSS and canvas specs, simply because Safari does the same. |
| 67 | // Firefox used to do the same too, until 4.0 where they fixed it. So at some |
| 68 | // point we should probably get rid of these scaling constants and rebaseline |
| 69 | // all the blur tests. |
| 70 | static const SkScalar kBLUR_SIGMA_SCALE; |
| 71 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | SkScalar fRadius; |
| 73 | SkBlurMaskFilter::BlurStyle fBlurStyle; |
senorblanco@chromium.org | 038aff6 | 2010-12-06 23:45:58 +0000 | [diff] [blame] | 74 | uint32_t fBlurFlags; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | |
| 76 | SkBlurMaskFilterImpl(SkFlattenableReadBuffer&); |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 77 | virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame^] | 78 | #if SK_SUPPORT_GPU |
| 79 | SkScalar computeXformedRadius(const SkMatrix& ctm) const { |
| 80 | bool ignoreTransform = SkToBool(fBlurFlags & SkBlurMaskFilter::kIgnoreTransform_BlurFlag); |
| 81 | |
| 82 | SkScalar xformedRadius = ignoreTransform ? fRadius |
| 83 | : ctm.mapRadius(fRadius); |
| 84 | return SkMinScalar(xformedRadius, kMAX_RADIUS); |
| 85 | } |
| 86 | #endif |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 87 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | typedef SkMaskFilter INHERITED; |
| 89 | }; |
| 90 | |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame^] | 91 | const SkScalar SkBlurMaskFilterImpl::kMAX_RADIUS = SkIntToScalar(128); |
| 92 | const SkScalar SkBlurMaskFilterImpl::kBLUR_SIGMA_SCALE = 0.6f; |
| 93 | |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 94 | SkMaskFilter* SkBlurMaskFilter::Create(SkScalar radius, |
| 95 | SkBlurMaskFilter::BlurStyle style, |
| 96 | uint32_t flags) { |
reed@google.com | e582869 | 2012-01-11 14:07:27 +0000 | [diff] [blame] | 97 | // use !(radius > 0) instead of radius <= 0 to reject NaN values |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 98 | if (!(radius > 0) || (unsigned)style >= SkBlurMaskFilter::kBlurStyleCount |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 99 | || flags > SkBlurMaskFilter::kAll_BlurFlag) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | return NULL; |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 101 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | |
senorblanco@chromium.org | 038aff6 | 2010-12-06 23:45:58 +0000 | [diff] [blame] | 103 | return SkNEW_ARGS(SkBlurMaskFilterImpl, (radius, style, flags)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | } |
| 105 | |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 106 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 108 | SkBlurMaskFilterImpl::SkBlurMaskFilterImpl(SkScalar radius, |
| 109 | SkBlurMaskFilter::BlurStyle style, |
senorblanco@chromium.org | 038aff6 | 2010-12-06 23:45:58 +0000 | [diff] [blame] | 110 | uint32_t flags) |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 111 | : fRadius(radius), fBlurStyle(style), fBlurFlags(flags) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 | #if 0 |
| 113 | fGamma = NULL; |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 114 | if (gammaScale) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | fGamma = new U8[256]; |
| 116 | if (gammaScale > 0) |
| 117 | SkBlurMask::BuildSqrGamma(fGamma, gammaScale); |
| 118 | else |
| 119 | SkBlurMask::BuildSqrtGamma(fGamma, -gammaScale); |
| 120 | } |
| 121 | #endif |
| 122 | SkASSERT(radius >= 0); |
| 123 | SkASSERT((unsigned)style < SkBlurMaskFilter::kBlurStyleCount); |
senorblanco@chromium.org | 038aff6 | 2010-12-06 23:45:58 +0000 | [diff] [blame] | 124 | SkASSERT(flags <= SkBlurMaskFilter::kAll_BlurFlag); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 125 | } |
| 126 | |
reed@google.com | 30711b7 | 2012-12-18 19:18:39 +0000 | [diff] [blame] | 127 | SkMask::Format SkBlurMaskFilterImpl::getFormat() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | return SkMask::kA8_Format; |
| 129 | } |
| 130 | |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 131 | bool SkBlurMaskFilterImpl::filterMask(SkMask* dst, const SkMask& src, |
reed@google.com | 30711b7 | 2012-12-18 19:18:39 +0000 | [diff] [blame] | 132 | const SkMatrix& matrix, |
| 133 | SkIPoint* margin) const{ |
senorblanco@chromium.org | 038aff6 | 2010-12-06 23:45:58 +0000 | [diff] [blame] | 134 | SkScalar radius; |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 135 | if (fBlurFlags & SkBlurMaskFilter::kIgnoreTransform_BlurFlag) { |
senorblanco@chromium.org | 038aff6 | 2010-12-06 23:45:58 +0000 | [diff] [blame] | 136 | radius = fRadius; |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 137 | } else { |
senorblanco@chromium.org | 038aff6 | 2010-12-06 23:45:58 +0000 | [diff] [blame] | 138 | radius = matrix.mapRadius(fRadius); |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 139 | } |
senorblanco@chromium.org | 038aff6 | 2010-12-06 23:45:58 +0000 | [diff] [blame] | 140 | |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame^] | 141 | radius = SkMinScalar(radius, kMAX_RADIUS); |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 142 | SkBlurMask::Quality blurQuality = |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 143 | (fBlurFlags & SkBlurMaskFilter::kHighQuality_BlurFlag) ? |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 144 | SkBlurMask::kHigh_Quality : SkBlurMask::kLow_Quality; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 145 | |
senorblanco@chromium.org | 91f489a | 2012-11-29 17:09:27 +0000 | [diff] [blame] | 146 | return SkBlurMask::Blur(dst, src, radius, (SkBlurMask::Style)fBlurStyle, |
| 147 | blurQuality, margin); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | } |
| 149 | |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 150 | bool SkBlurMaskFilterImpl::filterRectMask(SkMask* dst, const SkRect& r, |
| 151 | const SkMatrix& matrix, |
| 152 | SkIPoint* margin, SkMask::CreateMode createMode) const{ |
| 153 | SkScalar radius; |
| 154 | if (fBlurFlags & SkBlurMaskFilter::kIgnoreTransform_BlurFlag) { |
| 155 | radius = fRadius; |
| 156 | } else { |
| 157 | radius = matrix.mapRadius(fRadius); |
| 158 | } |
| 159 | |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame^] | 160 | radius = SkMinScalar(radius, kMAX_RADIUS); |
skia.committer@gmail.com | 2e71f16 | 2013-03-12 07:12:32 +0000 | [diff] [blame] | 161 | |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 162 | return SkBlurMask::BlurRect(dst, r, radius, (SkBlurMask::Style)fBlurStyle, |
| 163 | margin, createMode); |
| 164 | } |
| 165 | |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 166 | #include "SkCanvas.h" |
| 167 | |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 168 | static bool drawRectsIntoMask(const SkRect rects[], int count, SkMask* mask) { |
| 169 | rects[0].roundOut(&mask->fBounds); |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 170 | mask->fRowBytes = SkAlign4(mask->fBounds.width()); |
| 171 | mask->fFormat = SkMask::kA8_Format; |
| 172 | size_t size = mask->computeImageSize(); |
| 173 | mask->fImage = SkMask::AllocImage(size); |
| 174 | if (NULL == mask->fImage) { |
| 175 | return false; |
| 176 | } |
| 177 | sk_bzero(mask->fImage, size); |
| 178 | |
| 179 | SkBitmap bitmap; |
| 180 | bitmap.setConfig(SkBitmap::kA8_Config, |
| 181 | mask->fBounds.width(), mask->fBounds.height(), |
| 182 | mask->fRowBytes); |
| 183 | bitmap.setPixels(mask->fImage); |
| 184 | |
| 185 | SkCanvas canvas(bitmap); |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 186 | canvas.translate(-SkIntToScalar(mask->fBounds.left()), |
| 187 | -SkIntToScalar(mask->fBounds.top())); |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 188 | |
| 189 | SkPaint paint; |
| 190 | paint.setAntiAlias(true); |
| 191 | |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 192 | if (1 == count) { |
| 193 | canvas.drawRect(rects[0], paint); |
| 194 | } else { |
| 195 | // todo: do I need a fast way to do this? |
| 196 | SkPath path; |
| 197 | path.addRect(rects[0]); |
| 198 | path.addRect(rects[1]); |
| 199 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 200 | canvas.drawPath(path, paint); |
| 201 | } |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 202 | return true; |
| 203 | } |
| 204 | |
reed@google.com | f276fa7 | 2012-11-21 14:14:10 +0000 | [diff] [blame] | 205 | static bool rect_exceeds(const SkRect& r, SkScalar v) { |
| 206 | return r.fLeft < -v || r.fTop < -v || r.fRight > v || r.fBottom > v || |
| 207 | r.width() > v || r.height() > v; |
reed@google.com | 07784a0 | 2012-11-19 21:09:14 +0000 | [diff] [blame] | 208 | } |
| 209 | |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 210 | #ifdef SK_IGNORE_FAST_RECT_BLUR |
humper@google.com | e86af1f | 2013-03-11 21:23:36 +0000 | [diff] [blame] | 211 | SK_CONF_DECLARE( bool, c_analyticBlurNinepatch, "mask.filter.analyticNinePatch", false, "Use the faster analytic blur approach for ninepatch rects" ); |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 212 | #else |
humper@google.com | e86af1f | 2013-03-11 21:23:36 +0000 | [diff] [blame] | 213 | SK_CONF_DECLARE( bool, c_analyticBlurNinepatch, "mask.filter.analyticNinePatch", true, "Use the faster analytic blur approach for ninepatch rects" ); |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 214 | #endif |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 215 | |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 216 | SkMaskFilter::FilterReturn |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 217 | SkBlurMaskFilterImpl::filterRectsToNine(const SkRect rects[], int count, |
| 218 | const SkMatrix& matrix, |
| 219 | const SkIRect& clipBounds, |
reed@google.com | 30711b7 | 2012-12-18 19:18:39 +0000 | [diff] [blame] | 220 | NinePatch* patch) const { |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 221 | if (count < 1 || count > 2) { |
| 222 | return kUnimplemented_FilterReturn; |
| 223 | } |
skia.committer@gmail.com | 3458716 | 2012-11-20 02:01:23 +0000 | [diff] [blame] | 224 | |
reed@google.com | 57850b9 | 2012-12-17 21:20:53 +0000 | [diff] [blame] | 225 | // TODO: report correct metrics for innerstyle, where we do not grow the |
| 226 | // total bounds, but we do need an inset the size of our blur-radius |
| 227 | if (SkBlurMaskFilter::kInner_BlurStyle == fBlurStyle) { |
| 228 | return kUnimplemented_FilterReturn; |
| 229 | } |
| 230 | |
reed@google.com | 07784a0 | 2012-11-19 21:09:14 +0000 | [diff] [blame] | 231 | // TODO: take clipBounds into account to limit our coordinates up front |
| 232 | // for now, just skip too-large src rects (to take the old code path). |
reed@google.com | f276fa7 | 2012-11-21 14:14:10 +0000 | [diff] [blame] | 233 | if (rect_exceeds(rects[0], SkIntToScalar(32767))) { |
reed@google.com | 07784a0 | 2012-11-19 21:09:14 +0000 | [diff] [blame] | 234 | return kUnimplemented_FilterReturn; |
| 235 | } |
skia.committer@gmail.com | 3458716 | 2012-11-20 02:01:23 +0000 | [diff] [blame] | 236 | |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 237 | SkIPoint margin; |
| 238 | SkMask srcM, dstM; |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 239 | rects[0].roundOut(&srcM.fBounds); |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 240 | srcM.fImage = NULL; |
| 241 | srcM.fFormat = SkMask::kA8_Format; |
| 242 | srcM.fRowBytes = 0; |
skia.committer@gmail.com | 2e71f16 | 2013-03-12 07:12:32 +0000 | [diff] [blame] | 243 | |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 244 | bool filterResult = false; |
| 245 | if (count == 1 && c_analyticBlurNinepatch) { |
| 246 | // special case for fast rect blur |
| 247 | // don't actually do the blur the first time, just compute the correct size |
skia.committer@gmail.com | 2e71f16 | 2013-03-12 07:12:32 +0000 | [diff] [blame] | 248 | filterResult = this->filterRectMask(&dstM, rects[0], matrix, &margin, |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 249 | SkMask::kJustComputeBounds_CreateMode); |
| 250 | } else { |
| 251 | filterResult = this->filterMask(&dstM, srcM, matrix, &margin); |
| 252 | } |
skia.committer@gmail.com | 2e71f16 | 2013-03-12 07:12:32 +0000 | [diff] [blame] | 253 | |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 254 | if (!filterResult) { |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 255 | return kFalse_FilterReturn; |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * smallR is the smallest version of 'rect' that will still guarantee that |
| 260 | * we get the same blur results on all edges, plus 1 center row/col that is |
| 261 | * representative of the extendible/stretchable edges of the ninepatch. |
| 262 | * Since our actual edge may be fractional we inset 1 more to be sure we |
| 263 | * don't miss any interior blur. |
| 264 | * x is an added pixel of blur, and { and } are the (fractional) edge |
| 265 | * pixels from the original rect. |
| 266 | * |
| 267 | * x x { x x .... x x } x x |
| 268 | * |
| 269 | * Thus, in this case, we inset by a total of 5 (on each side) beginning |
| 270 | * with our outer-rect (dstM.fBounds) |
| 271 | */ |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 272 | SkRect smallR[2]; |
| 273 | SkIPoint center; |
| 274 | |
| 275 | // +2 is from +1 for each edge (to account for possible fractional edges |
| 276 | int smallW = dstM.fBounds.width() - srcM.fBounds.width() + 2; |
| 277 | int smallH = dstM.fBounds.height() - srcM.fBounds.height() + 2; |
| 278 | SkIRect innerIR; |
| 279 | |
| 280 | if (1 == count) { |
| 281 | innerIR = srcM.fBounds; |
| 282 | center.set(smallW, smallH); |
| 283 | } else { |
| 284 | SkASSERT(2 == count); |
| 285 | rects[1].roundIn(&innerIR); |
| 286 | center.set(smallW + (innerIR.left() - srcM.fBounds.left()), |
| 287 | smallH + (innerIR.top() - srcM.fBounds.top())); |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 288 | } |
| 289 | |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 290 | // +1 so we get a clean, stretchable, center row/col |
| 291 | smallW += 1; |
| 292 | smallH += 1; |
| 293 | |
| 294 | // we want the inset amounts to be integral, so we don't change any |
| 295 | // fractional phase on the fRight or fBottom of our smallR. |
| 296 | const SkScalar dx = SkIntToScalar(innerIR.width() - smallW); |
| 297 | const SkScalar dy = SkIntToScalar(innerIR.height() - smallH); |
| 298 | if (dx < 0 || dy < 0) { |
| 299 | // we're too small, relative to our blur, to break into nine-patch, |
| 300 | // so we ask to have our normal filterMask() be called. |
| 301 | return kUnimplemented_FilterReturn; |
| 302 | } |
| 303 | |
| 304 | smallR[0].set(rects[0].left(), rects[0].top(), rects[0].right() - dx, rects[0].bottom() - dy); |
| 305 | SkASSERT(!smallR[0].isEmpty()); |
| 306 | if (2 == count) { |
| 307 | smallR[1].set(rects[1].left(), rects[1].top(), |
| 308 | rects[1].right() - dx, rects[1].bottom() - dy); |
| 309 | SkASSERT(!smallR[1].isEmpty()); |
| 310 | } |
| 311 | |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 312 | if (count > 1 || !c_analyticBlurNinepatch) { |
| 313 | if (!drawRectsIntoMask(smallR, count, &srcM)) { |
| 314 | return kFalse_FilterReturn; |
| 315 | } |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 316 | |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 317 | SkAutoMaskFreeImage amf(srcM.fImage); |
reed@google.com | d198a5b | 2012-11-27 15:18:04 +0000 | [diff] [blame] | 318 | |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 319 | if (!this->filterMask(&patch->fMask, srcM, matrix, &margin)) { |
| 320 | return kFalse_FilterReturn; |
| 321 | } |
| 322 | } else { |
skia.committer@gmail.com | 2e71f16 | 2013-03-12 07:12:32 +0000 | [diff] [blame] | 323 | if (!this->filterRectMask(&patch->fMask, smallR[0], matrix, &margin, |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 324 | SkMask::kComputeBoundsAndRenderImage_CreateMode)) { |
| 325 | return kFalse_FilterReturn; |
| 326 | } |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 327 | } |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 328 | patch->fMask.fBounds.offsetTo(0, 0); |
| 329 | patch->fOuterRect = dstM.fBounds; |
| 330 | patch->fCenter = center; |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 331 | return kTrue_FilterReturn; |
| 332 | } |
| 333 | |
reed@google.com | 30711b7 | 2012-12-18 19:18:39 +0000 | [diff] [blame] | 334 | void SkBlurMaskFilterImpl::computeFastBounds(const SkRect& src, |
| 335 | SkRect* dst) const { |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 336 | dst->set(src.fLeft - fRadius, src.fTop - fRadius, |
| 337 | src.fRight + fRadius, src.fBottom + fRadius); |
| 338 | } |
| 339 | |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 340 | SkBlurMaskFilterImpl::SkBlurMaskFilterImpl(SkFlattenableReadBuffer& buffer) |
| 341 | : SkMaskFilter(buffer) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 342 | fRadius = buffer.readScalar(); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 343 | fBlurStyle = (SkBlurMaskFilter::BlurStyle)buffer.readInt(); |
| 344 | fBlurFlags = buffer.readUInt() & SkBlurMaskFilter::kAll_BlurFlag; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 345 | SkASSERT(fRadius >= 0); |
| 346 | SkASSERT((unsigned)fBlurStyle < SkBlurMaskFilter::kBlurStyleCount); |
| 347 | } |
| 348 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 349 | void SkBlurMaskFilterImpl::flatten(SkFlattenableWriteBuffer& buffer) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 350 | this->INHERITED::flatten(buffer); |
| 351 | buffer.writeScalar(fRadius); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 352 | buffer.writeInt(fBlurStyle); |
| 353 | buffer.writeUInt(fBlurFlags); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 354 | } |
| 355 | |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame^] | 356 | #if SK_SUPPORT_GPU |
reed@google.com | 2b75f42 | 2011-07-07 13:43:38 +0000 | [diff] [blame] | 357 | |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame^] | 358 | bool SkBlurMaskFilterImpl::canFilterMaskGPU(const SkRect& srcBounds, |
| 359 | const SkIRect& clipBounds, |
| 360 | const SkMatrix& ctm, |
| 361 | SkRect* maskRect) const { |
| 362 | SkScalar xformedRadius = this->computeXformedRadius(ctm); |
| 363 | if (xformedRadius <= 0) { |
| 364 | return false; |
reed@google.com | 2b75f42 | 2011-07-07 13:43:38 +0000 | [diff] [blame] | 365 | } |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame^] | 366 | |
| 367 | static const SkScalar kMIN_GPU_BLUR_SIZE = SkIntToScalar(64); |
| 368 | static const SkScalar kMIN_GPU_BLUR_RADIUS = SkIntToScalar(32); |
| 369 | |
| 370 | if (srcBounds.width() <= kMIN_GPU_BLUR_SIZE && |
| 371 | srcBounds.height() <= kMIN_GPU_BLUR_SIZE && |
| 372 | xformedRadius <= kMIN_GPU_BLUR_RADIUS) { |
| 373 | // We prefer to blur small rect with small radius via CPU. |
| 374 | return false; |
| 375 | } |
| 376 | |
| 377 | if (NULL == maskRect) { |
| 378 | // don't need to compute maskRect |
| 379 | return true; |
| 380 | } |
| 381 | |
| 382 | float sigma3 = 3 * SkScalarToFloat(xformedRadius) * kBLUR_SIGMA_SCALE; |
| 383 | |
| 384 | SkRect clipRect = SkRect::MakeFromIRect(clipBounds); |
| 385 | SkRect srcRect(srcBounds); |
| 386 | |
| 387 | // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area. |
| 388 | srcRect.outset(SkFloatToScalar(sigma3), SkFloatToScalar(sigma3)); |
| 389 | clipRect.outset(SkFloatToScalar(sigma3), SkFloatToScalar(sigma3)); |
| 390 | srcRect.intersect(clipRect); |
| 391 | *maskRect = srcRect; |
| 392 | return true; |
reed@google.com | 2b75f42 | 2011-07-07 13:43:38 +0000 | [diff] [blame] | 393 | } |
| 394 | |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame^] | 395 | bool SkBlurMaskFilterImpl::filterMaskGPU(GrTexture* src, |
| 396 | const SkRect& maskRect, |
| 397 | GrTexture** result, |
| 398 | bool canOverwriteSrc) const { |
| 399 | SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height()); |
| 400 | |
| 401 | GrContext* context = src->getContext(); |
| 402 | |
| 403 | GrContext::AutoWideOpenIdentityDraw awo(context, NULL); |
| 404 | |
| 405 | SkScalar xformedRadius = this->computeXformedRadius(context->getMatrix()); |
| 406 | SkASSERT(xformedRadius > 0); |
| 407 | |
| 408 | float sigma = SkScalarToFloat(xformedRadius) * kBLUR_SIGMA_SCALE; |
| 409 | |
| 410 | // If we're doing a normal blur, we can clobber the pathTexture in the |
| 411 | // gaussianBlur. Otherwise, we need to save it for later compositing. |
| 412 | bool isNormalBlur = (SkBlurMaskFilter::kNormal_BlurStyle == fBlurStyle); |
| 413 | *result = context->gaussianBlur(src, isNormalBlur && canOverwriteSrc, |
| 414 | clipRect, sigma, sigma); |
| 415 | if (NULL == result) { |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | if (!isNormalBlur) { |
| 420 | context->setIdentityMatrix(); |
| 421 | GrPaint paint; |
| 422 | SkMatrix matrix; |
| 423 | matrix.setIDiv(src->width(), src->height()); |
| 424 | // Blend pathTexture over blurTexture. |
| 425 | GrContext::AutoRenderTarget art(context, (*result)->asRenderTarget()); |
| 426 | paint.colorStage(0)->setEffect( |
| 427 | GrSimpleTextureEffect::Create(src, matrix))->unref(); |
| 428 | if (SkBlurMaskFilter::kInner_BlurStyle == fBlurStyle) { |
| 429 | // inner: dst = dst * src |
| 430 | paint.setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff); |
| 431 | } else if (SkBlurMaskFilter::kSolid_BlurStyle == fBlurStyle) { |
| 432 | // solid: dst = src + dst - src * dst |
| 433 | // = (1 - dst) * src + 1 * dst |
| 434 | paint.setBlendFunc(kIDC_GrBlendCoeff, kOne_GrBlendCoeff); |
| 435 | } else if (SkBlurMaskFilter::kOuter_BlurStyle == fBlurStyle) { |
| 436 | // outer: dst = dst * (1 - src) |
| 437 | // = 0 * src + (1 - src) * dst |
| 438 | paint.setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff); |
| 439 | } |
| 440 | context->drawRect(paint, clipRect); |
| 441 | } |
| 442 | |
| 443 | return true; |
| 444 | } |
| 445 | |
| 446 | #endif // SK_SUPPORT_GPU |
| 447 | |
| 448 | |
robertphillips@google.com | 0bd80fa | 2013-03-18 17:53:38 +0000 | [diff] [blame] | 449 | #ifdef SK_DEVELOPER |
| 450 | void SkBlurMaskFilterImpl::toString(SkString* str) const { |
| 451 | str->append("SkBlurMaskFilterImpl: ("); |
| 452 | |
| 453 | str->append("radius: "); |
| 454 | str->appendScalar(fRadius); |
| 455 | str->append(" "); |
| 456 | |
| 457 | static const char* gStyleName[SkBlurMaskFilter::kBlurStyleCount] = { |
| 458 | "normal", "solid", "outer", "inner" |
| 459 | }; |
| 460 | |
| 461 | str->appendf("style: %s ", gStyleName[fBlurStyle]); |
| 462 | str->append("flags: ("); |
| 463 | if (fBlurFlags) { |
| 464 | bool needSeparator = false; |
skia.committer@gmail.com | 8eaddb0 | 2013-03-19 07:15:10 +0000 | [diff] [blame] | 465 | SkAddFlagToString(str, |
| 466 | SkToBool(fBlurFlags & SkBlurMaskFilter::kIgnoreTransform_BlurFlag), |
robertphillips@google.com | 0bd80fa | 2013-03-18 17:53:38 +0000 | [diff] [blame] | 467 | "IgnoreXform", &needSeparator); |
skia.committer@gmail.com | 8eaddb0 | 2013-03-19 07:15:10 +0000 | [diff] [blame] | 468 | SkAddFlagToString(str, |
| 469 | SkToBool(fBlurFlags & SkBlurMaskFilter::kHighQuality_BlurFlag), |
robertphillips@google.com | 0bd80fa | 2013-03-18 17:53:38 +0000 | [diff] [blame] | 470 | "HighQuality", &needSeparator); |
| 471 | } else { |
| 472 | str->append("None"); |
| 473 | } |
| 474 | str->append("))"); |
| 475 | } |
| 476 | #endif |
| 477 | |
caryclark@google.com | d26147a | 2011-12-15 14:16:43 +0000 | [diff] [blame] | 478 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkBlurMaskFilter) |
| 479 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkBlurMaskFilterImpl) |
| 480 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |