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" |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 11 | #include "SkGpuBlurUtils.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 12 | #include "SkReadBuffer.h" |
| 13 | #include "SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkMaskFilter.h" |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 15 | #include "SkRRect.h" |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 16 | #include "SkRTConf.h" |
robertphillips@google.com | 0bd80fa | 2013-03-18 17:53:38 +0000 | [diff] [blame] | 17 | #include "SkStringUtils.h" |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 18 | #include "SkStrokeRec.h" |
| 19 | |
| 20 | #if SK_SUPPORT_GPU |
| 21 | #include "GrContext.h" |
| 22 | #include "GrTexture.h" |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 23 | #include "GrEffect.h" |
| 24 | #include "gl/GrGLEffect.h" |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 25 | #include "effects/GrSimpleTextureEffect.h" |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 26 | #include "GrTBackendEffectFactory.h" |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 27 | #include "SkGrPixelRef.h" |
| 28 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | |
| 30 | class SkBlurMaskFilterImpl : public SkMaskFilter { |
| 31 | public: |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 32 | SkBlurMaskFilterImpl(SkScalar sigma, SkBlurMaskFilter::BlurStyle, uint32_t flags); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | |
| 34 | // overrides from SkMaskFilter |
reed@google.com | 30711b7 | 2012-12-18 19:18:39 +0000 | [diff] [blame] | 35 | virtual SkMask::Format getFormat() const SK_OVERRIDE; |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 36 | virtual bool filterMask(SkMask* dst, const SkMask& src, const SkMatrix&, |
reed@google.com | 30711b7 | 2012-12-18 19:18:39 +0000 | [diff] [blame] | 37 | SkIPoint* margin) const SK_OVERRIDE; |
skia.committer@gmail.com | 2e71f16 | 2013-03-12 07:12:32 +0000 | [diff] [blame] | 38 | |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 39 | #if SK_SUPPORT_GPU |
skia.committer@gmail.com | 1842adf | 2013-07-04 07:01:07 +0000 | [diff] [blame] | 40 | virtual bool canFilterMaskGPU(const SkRect& devBounds, |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 41 | const SkIRect& clipBounds, |
| 42 | const SkMatrix& ctm, |
| 43 | SkRect* maskRect) const SK_OVERRIDE; |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 44 | virtual bool directFilterMaskGPU(GrContext* context, |
| 45 | GrPaint* grp, |
| 46 | const SkStrokeRec& strokeRec, |
| 47 | const SkPath& path) const SK_OVERRIDE; |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 48 | virtual bool directFilterRRectMaskGPU(GrContext* context, |
| 49 | GrPaint* grp, |
| 50 | const SkStrokeRec& strokeRec, |
| 51 | const SkRRect& rrect) const SK_OVERRIDE; |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 52 | |
skia.committer@gmail.com | 1842adf | 2013-07-04 07:01:07 +0000 | [diff] [blame] | 53 | virtual bool filterMaskGPU(GrTexture* src, |
commit-bot@chromium.org | 41bf930 | 2014-01-08 22:25:53 +0000 | [diff] [blame] | 54 | const SkMatrix& ctm, |
skia.committer@gmail.com | 1842adf | 2013-07-04 07:01:07 +0000 | [diff] [blame] | 55 | const SkRect& maskRect, |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 56 | GrTexture** result, |
commit-bot@chromium.org | 41bf930 | 2014-01-08 22:25:53 +0000 | [diff] [blame] | 57 | bool canOverwriteSrc) const SK_OVERRIDE; |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 58 | #endif |
| 59 | |
reed@google.com | 30711b7 | 2012-12-18 19:18:39 +0000 | [diff] [blame] | 60 | virtual void computeFastBounds(const SkRect&, SkRect*) const SK_OVERRIDE; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 62 | SK_TO_STRING_OVERRIDE() |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 63 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkBlurMaskFilterImpl) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 65 | protected: |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 66 | virtual FilterReturn filterRectsToNine(const SkRect[], int count, const SkMatrix&, |
| 67 | const SkIRect& clipBounds, |
reed@google.com | 30711b7 | 2012-12-18 19:18:39 +0000 | [diff] [blame] | 68 | NinePatch*) const SK_OVERRIDE; |
skia.committer@gmail.com | 2e71f16 | 2013-03-12 07:12:32 +0000 | [diff] [blame] | 69 | |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 70 | virtual FilterReturn filterRRectToNine(const SkRRect&, const SkMatrix&, |
| 71 | const SkIRect& clipBounds, |
| 72 | NinePatch*) const SK_OVERRIDE; |
| 73 | |
skia.committer@gmail.com | 2e71f16 | 2013-03-12 07:12:32 +0000 | [diff] [blame] | 74 | bool filterRectMask(SkMask* dstM, const SkRect& r, const SkMatrix& matrix, |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 75 | SkIPoint* margin, SkMask::CreateMode createMode) const; |
commit-bot@chromium.org | a477154 | 2014-03-10 21:42:06 +0000 | [diff] [blame] | 76 | bool filterRRectMask(SkMask* dstM, const SkRRect& r, const SkMatrix& matrix, |
| 77 | SkIPoint* margin, SkMask::CreateMode createMode) const; |
skia.committer@gmail.com | 453995e | 2012-11-10 02:01:26 +0000 | [diff] [blame] | 78 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | private: |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 80 | // To avoid unseemly allocation requests (esp. for finite platforms like |
| 81 | // handset) we limit the radius so something manageable. (as opposed to |
| 82 | // a request like 10,000) |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 83 | static const SkScalar kMAX_BLUR_SIGMA; |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 84 | |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 85 | SkScalar fSigma; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | SkBlurMaskFilter::BlurStyle fBlurStyle; |
senorblanco@chromium.org | 038aff6 | 2010-12-06 23:45:58 +0000 | [diff] [blame] | 87 | uint32_t fBlurFlags; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 89 | SkBlurMaskFilterImpl(SkReadBuffer&); |
| 90 | virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 91 | |
| 92 | SkScalar computeXformedSigma(const SkMatrix& ctm) const { |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 93 | bool ignoreTransform = SkToBool(fBlurFlags & SkBlurMaskFilter::kIgnoreTransform_BlurFlag); |
| 94 | |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 95 | SkScalar xformedSigma = ignoreTransform ? fSigma : ctm.mapRadius(fSigma); |
| 96 | return SkMinScalar(xformedSigma, kMAX_BLUR_SIGMA); |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 97 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 98 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | typedef SkMaskFilter INHERITED; |
| 100 | }; |
| 101 | |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 102 | const SkScalar SkBlurMaskFilterImpl::kMAX_BLUR_SIGMA = SkIntToScalar(128); |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 103 | |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 104 | SkMaskFilter* SkBlurMaskFilter::Create(SkScalar radius, |
| 105 | SkBlurMaskFilter::BlurStyle style, |
| 106 | uint32_t flags) { |
reed@google.com | e582869 | 2012-01-11 14:07:27 +0000 | [diff] [blame] | 107 | // use !(radius > 0) instead of radius <= 0 to reject NaN values |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 108 | if (!(radius > 0) || (unsigned)style >= SkBlurMaskFilter::kBlurStyleCount |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 109 | || flags > SkBlurMaskFilter::kAll_BlurFlag) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 110 | return NULL; |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 111 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 | |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 113 | SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(radius); |
| 114 | |
| 115 | return SkNEW_ARGS(SkBlurMaskFilterImpl, (sigma, style, flags)); |
| 116 | } |
| 117 | |
| 118 | SkMaskFilter* SkBlurMaskFilter::Create(SkBlurMaskFilter::BlurStyle style, |
| 119 | SkScalar sigma, |
| 120 | uint32_t flags) { |
| 121 | // use !(sigma > 0) instead of sigma <= 0 to reject NaN values |
| 122 | if (!(sigma > 0) || (unsigned)style >= SkBlurMaskFilter::kBlurStyleCount |
| 123 | || flags > SkBlurMaskFilter::kAll_BlurFlag) { |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | return SkNEW_ARGS(SkBlurMaskFilterImpl, (sigma, style, flags)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | } |
| 129 | |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 130 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 132 | SkBlurMaskFilterImpl::SkBlurMaskFilterImpl(SkScalar sigma, |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 133 | SkBlurMaskFilter::BlurStyle style, |
senorblanco@chromium.org | 038aff6 | 2010-12-06 23:45:58 +0000 | [diff] [blame] | 134 | uint32_t flags) |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 135 | : fSigma(sigma), fBlurStyle(style), fBlurFlags(flags) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 136 | #if 0 |
| 137 | fGamma = NULL; |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 138 | if (gammaScale) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | fGamma = new U8[256]; |
| 140 | if (gammaScale > 0) |
| 141 | SkBlurMask::BuildSqrGamma(fGamma, gammaScale); |
| 142 | else |
| 143 | SkBlurMask::BuildSqrtGamma(fGamma, -gammaScale); |
| 144 | } |
| 145 | #endif |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 146 | SkASSERT(fSigma >= 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 147 | SkASSERT((unsigned)style < SkBlurMaskFilter::kBlurStyleCount); |
senorblanco@chromium.org | 038aff6 | 2010-12-06 23:45:58 +0000 | [diff] [blame] | 148 | SkASSERT(flags <= SkBlurMaskFilter::kAll_BlurFlag); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | } |
| 150 | |
reed@google.com | 30711b7 | 2012-12-18 19:18:39 +0000 | [diff] [blame] | 151 | SkMask::Format SkBlurMaskFilterImpl::getFormat() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | return SkMask::kA8_Format; |
| 153 | } |
| 154 | |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 155 | bool SkBlurMaskFilterImpl::filterMask(SkMask* dst, const SkMask& src, |
reed@google.com | 30711b7 | 2012-12-18 19:18:39 +0000 | [diff] [blame] | 156 | const SkMatrix& matrix, |
| 157 | SkIPoint* margin) const{ |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 158 | SkScalar sigma = this->computeXformedSigma(matrix); |
senorblanco@chromium.org | 038aff6 | 2010-12-06 23:45:58 +0000 | [diff] [blame] | 159 | |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 160 | SkBlurMask::Quality blurQuality = |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 161 | (fBlurFlags & SkBlurMaskFilter::kHighQuality_BlurFlag) ? |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 162 | SkBlurMask::kHigh_Quality : SkBlurMask::kLow_Quality; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 163 | |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 164 | return SkBlurMask::BoxBlur(dst, src, sigma, (SkBlurMask::Style)fBlurStyle, |
| 165 | blurQuality, margin); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | } |
| 167 | |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 168 | bool SkBlurMaskFilterImpl::filterRectMask(SkMask* dst, const SkRect& r, |
| 169 | const SkMatrix& matrix, |
| 170 | SkIPoint* margin, SkMask::CreateMode createMode) const{ |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 171 | SkScalar sigma = computeXformedSigma(matrix); |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 172 | |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 173 | return SkBlurMask::BlurRect(sigma, dst, r, (SkBlurMask::Style)fBlurStyle, |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 174 | margin, createMode); |
| 175 | } |
| 176 | |
commit-bot@chromium.org | a477154 | 2014-03-10 21:42:06 +0000 | [diff] [blame] | 177 | bool SkBlurMaskFilterImpl::filterRRectMask(SkMask* dst, const SkRRect& r, |
| 178 | const SkMatrix& matrix, |
| 179 | SkIPoint* margin, SkMask::CreateMode createMode) const{ |
| 180 | SkScalar sigma = computeXformedSigma(matrix); |
| 181 | |
| 182 | return SkBlurMask::BlurRRect(sigma, dst, r, (SkBlurMask::Style)fBlurStyle, |
| 183 | margin, createMode); |
| 184 | } |
| 185 | |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 186 | #include "SkCanvas.h" |
| 187 | |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 188 | static bool prepare_to_draw_into_mask(const SkRect& bounds, SkMask* mask) { |
| 189 | SkASSERT(mask != NULL); |
| 190 | |
| 191 | bounds.roundOut(&mask->fBounds); |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 192 | mask->fRowBytes = SkAlign4(mask->fBounds.width()); |
| 193 | mask->fFormat = SkMask::kA8_Format; |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 194 | const size_t size = mask->computeImageSize(); |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 195 | mask->fImage = SkMask::AllocImage(size); |
| 196 | if (NULL == mask->fImage) { |
| 197 | return false; |
| 198 | } |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 199 | |
| 200 | // FIXME: use sk_calloc in AllocImage? |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 201 | sk_bzero(mask->fImage, size); |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 202 | return true; |
| 203 | } |
| 204 | |
| 205 | static bool draw_rrect_into_mask(const SkRRect rrect, SkMask* mask) { |
| 206 | if (!prepare_to_draw_into_mask(rrect.rect(), mask)) { |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | // FIXME: This code duplicates code in draw_rects_into_mask, below. Is there a |
| 211 | // clean way to share more code? |
| 212 | SkBitmap bitmap; |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 213 | bitmap.installMaskPixels(*mask); |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 214 | |
| 215 | SkCanvas canvas(bitmap); |
| 216 | canvas.translate(-SkIntToScalar(mask->fBounds.left()), |
| 217 | -SkIntToScalar(mask->fBounds.top())); |
| 218 | |
| 219 | SkPaint paint; |
| 220 | paint.setAntiAlias(true); |
| 221 | canvas.drawRRect(rrect, paint); |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | static bool draw_rects_into_mask(const SkRect rects[], int count, SkMask* mask) { |
| 226 | if (!prepare_to_draw_into_mask(rects[0], mask)) { |
| 227 | return false; |
| 228 | } |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 229 | |
| 230 | SkBitmap bitmap; |
commit-bot@chromium.org | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 231 | bitmap.installPixels(SkImageInfo::Make(mask->fBounds.width(), |
| 232 | mask->fBounds.height(), |
| 233 | kAlpha_8_SkColorType, |
| 234 | kPremul_SkAlphaType), |
| 235 | mask->fImage, mask->fRowBytes, NULL, NULL); |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 236 | |
| 237 | SkCanvas canvas(bitmap); |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 238 | canvas.translate(-SkIntToScalar(mask->fBounds.left()), |
| 239 | -SkIntToScalar(mask->fBounds.top())); |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 240 | |
| 241 | SkPaint paint; |
| 242 | paint.setAntiAlias(true); |
| 243 | |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 244 | if (1 == count) { |
| 245 | canvas.drawRect(rects[0], paint); |
| 246 | } else { |
| 247 | // todo: do I need a fast way to do this? |
| 248 | SkPath path; |
| 249 | path.addRect(rects[0]); |
| 250 | path.addRect(rects[1]); |
| 251 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 252 | canvas.drawPath(path, paint); |
| 253 | } |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 254 | return true; |
| 255 | } |
| 256 | |
reed@google.com | f276fa7 | 2012-11-21 14:14:10 +0000 | [diff] [blame] | 257 | static bool rect_exceeds(const SkRect& r, SkScalar v) { |
| 258 | return r.fLeft < -v || r.fTop < -v || r.fRight > v || r.fBottom > v || |
| 259 | r.width() > v || r.height() > v; |
reed@google.com | 07784a0 | 2012-11-19 21:09:14 +0000 | [diff] [blame] | 260 | } |
| 261 | |
commit-bot@chromium.org | a477154 | 2014-03-10 21:42:06 +0000 | [diff] [blame] | 262 | #ifdef SK_IGNORE_FAST_RRECT_BLUR |
| 263 | SK_CONF_DECLARE( bool, c_analyticBlurRRect, "mask.filter.blur.analyticRRect", false, "Use the faster analytic blur approach for ninepatch rects" ); |
| 264 | #else |
| 265 | SK_CONF_DECLARE( bool, c_analyticBlurRRect, "mask.filter.blur.analyticRRect", true, "Use the faster analytic blur approach for ninepatch round rects" ); |
| 266 | #endif |
| 267 | |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 268 | SkMaskFilter::FilterReturn |
| 269 | SkBlurMaskFilterImpl::filterRRectToNine(const SkRRect& rrect, const SkMatrix& matrix, |
| 270 | const SkIRect& clipBounds, |
| 271 | NinePatch* patch) const { |
| 272 | SkASSERT(patch != NULL); |
| 273 | switch (rrect.getType()) { |
| 274 | case SkRRect::kUnknown_Type: |
| 275 | // Unknown should never be returned. |
| 276 | SkASSERT(false); |
| 277 | // Fall through. |
| 278 | case SkRRect::kEmpty_Type: |
| 279 | // Nothing to draw. |
| 280 | return kFalse_FilterReturn; |
| 281 | |
| 282 | case SkRRect::kRect_Type: |
| 283 | // We should have caught this earlier. |
| 284 | SkASSERT(false); |
| 285 | // Fall through. |
| 286 | case SkRRect::kOval_Type: |
| 287 | // The nine patch special case does not handle ovals, and we |
| 288 | // already have code for rectangles. |
| 289 | return kUnimplemented_FilterReturn; |
| 290 | |
| 291 | case SkRRect::kSimple_Type: |
commit-bot@chromium.org | bab3fc4 | 2014-03-17 19:47:40 +0000 | [diff] [blame^] | 292 | // Fall through. |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 293 | case SkRRect::kComplex_Type: |
commit-bot@chromium.org | bab3fc4 | 2014-03-17 19:47:40 +0000 | [diff] [blame^] | 294 | // These can take advantage of this fast path. |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 295 | break; |
| 296 | } |
| 297 | |
| 298 | // TODO: report correct metrics for innerstyle, where we do not grow the |
| 299 | // total bounds, but we do need an inset the size of our blur-radius |
| 300 | if (SkBlurMaskFilter::kInner_BlurStyle == fBlurStyle) { |
| 301 | return kUnimplemented_FilterReturn; |
| 302 | } |
| 303 | |
| 304 | // TODO: take clipBounds into account to limit our coordinates up front |
| 305 | // for now, just skip too-large src rects (to take the old code path). |
| 306 | if (rect_exceeds(rrect.rect(), SkIntToScalar(32767))) { |
| 307 | return kUnimplemented_FilterReturn; |
| 308 | } |
| 309 | |
| 310 | SkIPoint margin; |
| 311 | SkMask srcM, dstM; |
| 312 | rrect.rect().roundOut(&srcM.fBounds); |
| 313 | srcM.fImage = NULL; |
| 314 | srcM.fFormat = SkMask::kA8_Format; |
| 315 | srcM.fRowBytes = 0; |
| 316 | |
commit-bot@chromium.org | a477154 | 2014-03-10 21:42:06 +0000 | [diff] [blame] | 317 | bool filterResult = false; |
| 318 | if (c_analyticBlurRRect) { |
| 319 | // special case for fast round rect blur |
| 320 | // don't actually do the blur the first time, just compute the correct size |
| 321 | filterResult = this->filterRRectMask(&dstM, rrect, matrix, &margin, |
| 322 | SkMask::kJustComputeBounds_CreateMode); |
| 323 | } |
| 324 | |
| 325 | if (!filterResult) { |
| 326 | filterResult = this->filterMask(&dstM, srcM, matrix, &margin); |
| 327 | } |
| 328 | |
| 329 | if (!filterResult) { |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 330 | return kFalse_FilterReturn; |
| 331 | } |
| 332 | |
| 333 | // Now figure out the appropriate width and height of the smaller round rectangle |
| 334 | // to stretch. It will take into account the larger radius per side as well as double |
| 335 | // the margin, to account for inner and outer blur. |
| 336 | const SkVector& UL = rrect.radii(SkRRect::kUpperLeft_Corner); |
| 337 | const SkVector& UR = rrect.radii(SkRRect::kUpperRight_Corner); |
| 338 | const SkVector& LR = rrect.radii(SkRRect::kLowerRight_Corner); |
| 339 | const SkVector& LL = rrect.radii(SkRRect::kLowerLeft_Corner); |
| 340 | |
| 341 | const SkScalar leftUnstretched = SkTMax(UL.fX, LL.fX) + SkIntToScalar(2 * margin.fX); |
| 342 | const SkScalar rightUnstretched = SkTMax(UR.fX, LR.fX) + SkIntToScalar(2 * margin.fX); |
| 343 | |
| 344 | // Extra space in the middle to ensure an unchanging piece for stretching. Use 3 to cover |
| 345 | // any fractional space on either side plus 1 for the part to stretch. |
| 346 | const SkScalar stretchSize = SkIntToScalar(3); |
| 347 | |
| 348 | const SkScalar totalSmallWidth = leftUnstretched + rightUnstretched + stretchSize; |
| 349 | if (totalSmallWidth >= rrect.rect().width()) { |
| 350 | // There is no valid piece to stretch. |
| 351 | return kUnimplemented_FilterReturn; |
| 352 | } |
| 353 | |
| 354 | const SkScalar topUnstretched = SkTMax(UL.fY, UR.fY) + SkIntToScalar(2 * margin.fY); |
| 355 | const SkScalar bottomUnstretched = SkTMax(LL.fY, LR.fY) + SkIntToScalar(2 * margin.fY); |
| 356 | |
| 357 | const SkScalar totalSmallHeight = topUnstretched + bottomUnstretched + stretchSize; |
| 358 | if (totalSmallHeight >= rrect.rect().height()) { |
| 359 | // There is no valid piece to stretch. |
| 360 | return kUnimplemented_FilterReturn; |
| 361 | } |
| 362 | |
| 363 | SkRect smallR = SkRect::MakeWH(totalSmallWidth, totalSmallHeight); |
| 364 | |
| 365 | SkRRect smallRR; |
| 366 | SkVector radii[4]; |
| 367 | radii[SkRRect::kUpperLeft_Corner] = UL; |
| 368 | radii[SkRRect::kUpperRight_Corner] = UR; |
| 369 | radii[SkRRect::kLowerRight_Corner] = LR; |
| 370 | radii[SkRRect::kLowerLeft_Corner] = LL; |
| 371 | smallRR.setRectRadii(smallR, radii); |
| 372 | |
commit-bot@chromium.org | a477154 | 2014-03-10 21:42:06 +0000 | [diff] [blame] | 373 | bool analyticBlurWorked = false; |
| 374 | if (c_analyticBlurRRect) { |
| 375 | analyticBlurWorked = |
| 376 | this->filterRRectMask(&patch->fMask, smallRR, matrix, &margin, |
| 377 | SkMask::kComputeBoundsAndRenderImage_CreateMode); |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 378 | } |
| 379 | |
commit-bot@chromium.org | a477154 | 2014-03-10 21:42:06 +0000 | [diff] [blame] | 380 | if (!analyticBlurWorked) { |
| 381 | if (!draw_rrect_into_mask(smallRR, &srcM)) { |
| 382 | return kFalse_FilterReturn; |
| 383 | } |
robertphillips@google.com | ad99358 | 2013-11-11 18:45:18 +0000 | [diff] [blame] | 384 | |
commit-bot@chromium.org | a477154 | 2014-03-10 21:42:06 +0000 | [diff] [blame] | 385 | SkAutoMaskFreeImage amf(srcM.fImage); |
| 386 | |
| 387 | if (!this->filterMask(&patch->fMask, srcM, matrix, &margin)) { |
| 388 | return kFalse_FilterReturn; |
| 389 | } |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | patch->fMask.fBounds.offsetTo(0, 0); |
| 393 | patch->fOuterRect = dstM.fBounds; |
| 394 | patch->fCenter.fX = SkScalarCeilToInt(leftUnstretched) + 1; |
| 395 | patch->fCenter.fY = SkScalarCeilToInt(topUnstretched) + 1; |
| 396 | return kTrue_FilterReturn; |
| 397 | } |
| 398 | |
humper@google.com | e86af1f | 2013-03-11 21:23:36 +0000 | [diff] [blame] | 399 | 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] | 400 | |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 401 | SkMaskFilter::FilterReturn |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 402 | SkBlurMaskFilterImpl::filterRectsToNine(const SkRect rects[], int count, |
| 403 | const SkMatrix& matrix, |
| 404 | const SkIRect& clipBounds, |
reed@google.com | 30711b7 | 2012-12-18 19:18:39 +0000 | [diff] [blame] | 405 | NinePatch* patch) const { |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 406 | if (count < 1 || count > 2) { |
| 407 | return kUnimplemented_FilterReturn; |
| 408 | } |
skia.committer@gmail.com | 3458716 | 2012-11-20 02:01:23 +0000 | [diff] [blame] | 409 | |
reed@google.com | 57850b9 | 2012-12-17 21:20:53 +0000 | [diff] [blame] | 410 | // TODO: report correct metrics for innerstyle, where we do not grow the |
| 411 | // total bounds, but we do need an inset the size of our blur-radius |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 412 | if (SkBlurMaskFilter::kInner_BlurStyle == fBlurStyle || |
| 413 | SkBlurMaskFilter::kOuter_BlurStyle == fBlurStyle) { |
reed@google.com | 57850b9 | 2012-12-17 21:20:53 +0000 | [diff] [blame] | 414 | return kUnimplemented_FilterReturn; |
| 415 | } |
| 416 | |
reed@google.com | 07784a0 | 2012-11-19 21:09:14 +0000 | [diff] [blame] | 417 | // TODO: take clipBounds into account to limit our coordinates up front |
| 418 | // 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] | 419 | if (rect_exceeds(rects[0], SkIntToScalar(32767))) { |
reed@google.com | 07784a0 | 2012-11-19 21:09:14 +0000 | [diff] [blame] | 420 | return kUnimplemented_FilterReturn; |
| 421 | } |
skia.committer@gmail.com | 3458716 | 2012-11-20 02:01:23 +0000 | [diff] [blame] | 422 | |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 423 | SkIPoint margin; |
| 424 | SkMask srcM, dstM; |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 425 | rects[0].roundOut(&srcM.fBounds); |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 426 | srcM.fImage = NULL; |
| 427 | srcM.fFormat = SkMask::kA8_Format; |
| 428 | srcM.fRowBytes = 0; |
skia.committer@gmail.com | 2e71f16 | 2013-03-12 07:12:32 +0000 | [diff] [blame] | 429 | |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 430 | bool filterResult = false; |
| 431 | if (count == 1 && c_analyticBlurNinepatch) { |
| 432 | // special case for fast rect blur |
| 433 | // 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] | 434 | filterResult = this->filterRectMask(&dstM, rects[0], matrix, &margin, |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 435 | SkMask::kJustComputeBounds_CreateMode); |
| 436 | } else { |
| 437 | filterResult = this->filterMask(&dstM, srcM, matrix, &margin); |
| 438 | } |
skia.committer@gmail.com | 2e71f16 | 2013-03-12 07:12:32 +0000 | [diff] [blame] | 439 | |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 440 | if (!filterResult) { |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 441 | return kFalse_FilterReturn; |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | * smallR is the smallest version of 'rect' that will still guarantee that |
| 446 | * we get the same blur results on all edges, plus 1 center row/col that is |
| 447 | * representative of the extendible/stretchable edges of the ninepatch. |
| 448 | * Since our actual edge may be fractional we inset 1 more to be sure we |
| 449 | * don't miss any interior blur. |
| 450 | * x is an added pixel of blur, and { and } are the (fractional) edge |
| 451 | * pixels from the original rect. |
| 452 | * |
| 453 | * x x { x x .... x x } x x |
| 454 | * |
| 455 | * Thus, in this case, we inset by a total of 5 (on each side) beginning |
| 456 | * with our outer-rect (dstM.fBounds) |
| 457 | */ |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 458 | SkRect smallR[2]; |
| 459 | SkIPoint center; |
| 460 | |
| 461 | // +2 is from +1 for each edge (to account for possible fractional edges |
| 462 | int smallW = dstM.fBounds.width() - srcM.fBounds.width() + 2; |
| 463 | int smallH = dstM.fBounds.height() - srcM.fBounds.height() + 2; |
| 464 | SkIRect innerIR; |
| 465 | |
| 466 | if (1 == count) { |
| 467 | innerIR = srcM.fBounds; |
| 468 | center.set(smallW, smallH); |
| 469 | } else { |
| 470 | SkASSERT(2 == count); |
| 471 | rects[1].roundIn(&innerIR); |
| 472 | center.set(smallW + (innerIR.left() - srcM.fBounds.left()), |
| 473 | smallH + (innerIR.top() - srcM.fBounds.top())); |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 474 | } |
| 475 | |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 476 | // +1 so we get a clean, stretchable, center row/col |
| 477 | smallW += 1; |
| 478 | smallH += 1; |
| 479 | |
| 480 | // we want the inset amounts to be integral, so we don't change any |
| 481 | // fractional phase on the fRight or fBottom of our smallR. |
| 482 | const SkScalar dx = SkIntToScalar(innerIR.width() - smallW); |
| 483 | const SkScalar dy = SkIntToScalar(innerIR.height() - smallH); |
| 484 | if (dx < 0 || dy < 0) { |
| 485 | // we're too small, relative to our blur, to break into nine-patch, |
| 486 | // so we ask to have our normal filterMask() be called. |
| 487 | return kUnimplemented_FilterReturn; |
| 488 | } |
| 489 | |
| 490 | smallR[0].set(rects[0].left(), rects[0].top(), rects[0].right() - dx, rects[0].bottom() - dy); |
robertphillips@google.com | 112a23e | 2013-09-03 17:17:43 +0000 | [diff] [blame] | 491 | if (smallR[0].width() < 2 || smallR[0].height() < 2) { |
| 492 | return kUnimplemented_FilterReturn; |
| 493 | } |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 494 | if (2 == count) { |
| 495 | smallR[1].set(rects[1].left(), rects[1].top(), |
| 496 | rects[1].right() - dx, rects[1].bottom() - dy); |
| 497 | SkASSERT(!smallR[1].isEmpty()); |
| 498 | } |
| 499 | |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 500 | if (count > 1 || !c_analyticBlurNinepatch) { |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 501 | if (!draw_rects_into_mask(smallR, count, &srcM)) { |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 502 | return kFalse_FilterReturn; |
| 503 | } |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 504 | |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 505 | SkAutoMaskFreeImage amf(srcM.fImage); |
reed@google.com | d198a5b | 2012-11-27 15:18:04 +0000 | [diff] [blame] | 506 | |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 507 | if (!this->filterMask(&patch->fMask, srcM, matrix, &margin)) { |
| 508 | return kFalse_FilterReturn; |
| 509 | } |
| 510 | } else { |
skia.committer@gmail.com | 2e71f16 | 2013-03-12 07:12:32 +0000 | [diff] [blame] | 511 | if (!this->filterRectMask(&patch->fMask, smallR[0], matrix, &margin, |
humper@google.com | 7c5d7b7 | 2013-03-11 20:16:28 +0000 | [diff] [blame] | 512 | SkMask::kComputeBoundsAndRenderImage_CreateMode)) { |
| 513 | return kFalse_FilterReturn; |
| 514 | } |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 515 | } |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 516 | patch->fMask.fBounds.offsetTo(0, 0); |
| 517 | patch->fOuterRect = dstM.fBounds; |
| 518 | patch->fCenter = center; |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 519 | return kTrue_FilterReturn; |
| 520 | } |
| 521 | |
reed@google.com | 30711b7 | 2012-12-18 19:18:39 +0000 | [diff] [blame] | 522 | void SkBlurMaskFilterImpl::computeFastBounds(const SkRect& src, |
| 523 | SkRect* dst) const { |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 524 | SkScalar pad = 3.0f * fSigma; |
robertphillips@google.com | 17ad2bd | 2013-07-30 12:15:19 +0000 | [diff] [blame] | 525 | |
| 526 | dst->set(src.fLeft - pad, src.fTop - pad, |
| 527 | src.fRight + pad, src.fBottom + pad); |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 528 | } |
| 529 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 530 | SkBlurMaskFilterImpl::SkBlurMaskFilterImpl(SkReadBuffer& buffer) |
reed@google.com | 03016a3 | 2011-08-12 14:59:59 +0000 | [diff] [blame] | 531 | : SkMaskFilter(buffer) { |
commit-bot@chromium.org | fed2ab6 | 2014-01-23 15:16:05 +0000 | [diff] [blame] | 532 | fSigma = buffer.readScalar(); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 533 | fBlurStyle = (SkBlurMaskFilter::BlurStyle)buffer.readInt(); |
| 534 | fBlurFlags = buffer.readUInt() & SkBlurMaskFilter::kAll_BlurFlag; |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 535 | SkASSERT(fSigma >= 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 536 | SkASSERT((unsigned)fBlurStyle < SkBlurMaskFilter::kBlurStyleCount); |
| 537 | } |
| 538 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 539 | void SkBlurMaskFilterImpl::flatten(SkWriteBuffer& buffer) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 540 | this->INHERITED::flatten(buffer); |
robertphillips@google.com | 11e0555 | 2013-12-03 19:46:58 +0000 | [diff] [blame] | 541 | buffer.writeScalar(fSigma); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 542 | buffer.writeInt(fBlurStyle); |
| 543 | buffer.writeUInt(fBlurFlags); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 544 | } |
| 545 | |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 546 | #if SK_SUPPORT_GPU |
reed@google.com | 2b75f42 | 2011-07-07 13:43:38 +0000 | [diff] [blame] | 547 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 548 | class GrGLRectBlurEffect; |
| 549 | |
| 550 | class GrRectBlurEffect : public GrEffect { |
| 551 | public: |
| 552 | virtual ~GrRectBlurEffect(); |
| 553 | |
| 554 | static const char* Name() { return "RectBlur"; } |
| 555 | |
| 556 | typedef GrGLRectBlurEffect GLEffect; |
| 557 | |
| 558 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
| 559 | virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; |
| 560 | |
| 561 | /** |
| 562 | * Create a simple filter effect with custom bicubic coefficients. |
| 563 | */ |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 564 | static GrEffectRef* Create(GrContext *context, const SkRect& rect, |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 565 | float sigma) { |
commit-bot@chromium.org | a29105a | 2014-01-31 14:00:58 +0000 | [diff] [blame] | 566 | GrTexture *horizontalScanline = NULL, *verticalScanline = NULL; |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 567 | bool createdScanlines = CreateScanlineTextures(context, sigma, |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 568 | SkScalarCeilToInt(rect.width()), |
| 569 | SkScalarCeilToInt(rect.height()), |
| 570 | &horizontalScanline, &verticalScanline); |
commit-bot@chromium.org | a29105a | 2014-01-31 14:00:58 +0000 | [diff] [blame] | 571 | SkAutoTUnref<GrTexture> hunref(horizontalScanline), vunref(verticalScanline); |
| 572 | if (!createdScanlines) { |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 573 | return NULL; |
| 574 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 575 | AutoEffectUnref effect(SkNEW_ARGS(GrRectBlurEffect, (rect, sigma, |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 576 | horizontalScanline, verticalScanline))); |
skia.committer@gmail.com | ba12448 | 2014-02-01 03:01:57 +0000 | [diff] [blame] | 577 | return CreateEffectRef(effect); |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 578 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 579 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 580 | unsigned int getWidth() const { return fWidth; } |
| 581 | unsigned int getHeight() const { return fHeight; } |
| 582 | float getSigma() const { return fSigma; } |
| 583 | const GrCoordTransform& getTransform() const { return fTransform; } |
| 584 | |
| 585 | private: |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 586 | GrRectBlurEffect(const SkRect& rect, float sigma, |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 587 | GrTexture *horizontal_scanline, GrTexture *vertical_scanline); |
| 588 | virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 589 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 590 | static bool CreateScanlineTextures(GrContext *context, float sigma, |
| 591 | unsigned int width, unsigned int height, |
| 592 | GrTexture **horizontalScanline, |
| 593 | GrTexture **verticalScanline); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 594 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 595 | unsigned int fWidth, fHeight; |
| 596 | float fSigma; |
| 597 | GrTextureAccess fHorizontalScanlineAccess; |
| 598 | GrTextureAccess fVerticalScanlineAccess; |
| 599 | GrCoordTransform fTransform; |
| 600 | |
| 601 | GR_DECLARE_EFFECT_TEST; |
| 602 | |
| 603 | typedef GrEffect INHERITED; |
| 604 | }; |
| 605 | |
| 606 | class GrGLRectBlurEffect : public GrGLEffect { |
| 607 | public: |
| 608 | GrGLRectBlurEffect(const GrBackendEffectFactory& factory, |
| 609 | const GrDrawEffect&); |
| 610 | virtual void emitCode(GrGLShaderBuilder*, |
| 611 | const GrDrawEffect&, |
| 612 | EffectKey, |
| 613 | const char* outputColor, |
| 614 | const char* inputColor, |
| 615 | const TransformedCoordsArray&, |
| 616 | const TextureSamplerArray&) SK_OVERRIDE; |
| 617 | |
| 618 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; |
| 619 | |
| 620 | private: |
| 621 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
| 622 | |
| 623 | UniformHandle fWidthUni; |
| 624 | UniformHandle fHeightUni; |
| 625 | |
| 626 | typedef GrGLEffect INHERITED; |
| 627 | }; |
| 628 | |
| 629 | GrGLRectBlurEffect::GrGLRectBlurEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
| 630 | : INHERITED(factory) { |
| 631 | } |
| 632 | |
| 633 | void GrGLRectBlurEffect::emitCode(GrGLShaderBuilder* builder, |
| 634 | const GrDrawEffect&, |
| 635 | EffectKey key, |
| 636 | const char* outputColor, |
| 637 | const char* inputColor, |
| 638 | const TransformedCoordsArray& coords, |
| 639 | const TextureSamplerArray& samplers) { |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 640 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 641 | SkString texture_coords = builder->ensureFSCoords2D(coords, 0); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 642 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 643 | if (inputColor) { |
| 644 | builder->fsCodeAppendf("\tvec4 src=%s;\n", inputColor); |
| 645 | } else { |
| 646 | builder->fsCodeAppendf("\tvec4 src=vec4(1)\n;"); |
| 647 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 648 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 649 | builder->fsCodeAppendf("\tvec4 horiz = "); |
| 650 | builder->fsAppendTextureLookup( samplers[0], texture_coords.c_str() ); |
| 651 | builder->fsCodeAppendf(";\n"); |
| 652 | builder->fsCodeAppendf("\tvec4 vert = "); |
| 653 | builder->fsAppendTextureLookup( samplers[1], texture_coords.c_str() ); |
| 654 | builder->fsCodeAppendf(";\n"); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 655 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 656 | builder->fsCodeAppendf("\tfloat final = (horiz*vert).r;\n"); |
| 657 | builder->fsCodeAppendf("\t%s = final*src;\n", outputColor); |
| 658 | } |
| 659 | |
| 660 | void GrGLRectBlurEffect::setData(const GrGLUniformManager& uman, |
| 661 | const GrDrawEffect& drawEffect) { |
| 662 | } |
| 663 | |
| 664 | bool GrRectBlurEffect::CreateScanlineTextures(GrContext *context, float sigma, |
| 665 | unsigned int width, unsigned int height, |
| 666 | GrTexture **horizontalScanline, |
| 667 | GrTexture **verticalScanline) { |
| 668 | GrTextureParams params; |
| 669 | GrTextureDesc texDesc; |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 670 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 671 | unsigned int profile_size = SkScalarFloorToInt(6*sigma); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 672 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 673 | texDesc.fWidth = width; |
| 674 | texDesc.fHeight = 1; |
| 675 | texDesc.fConfig = kAlpha_8_GrPixelConfig; |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 676 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 677 | static const GrCacheID::Domain gBlurProfileDomain = GrCacheID::GenerateDomain(); |
| 678 | GrCacheID::Key key; |
| 679 | memset(&key, 0, sizeof(key)); |
| 680 | key.fData32[0] = profile_size; |
| 681 | key.fData32[1] = width; |
| 682 | key.fData32[2] = 1; |
| 683 | GrCacheID horizontalCacheID(gBlurProfileDomain, key); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 684 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 685 | uint8_t *profile = NULL; |
commit-bot@chromium.org | cc787fa | 2014-01-31 01:00:49 +0000 | [diff] [blame] | 686 | SkAutoTDeleteArray<uint8_t> ada(NULL); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 687 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 688 | *horizontalScanline = context->findAndRefTexture(texDesc, horizontalCacheID, ¶ms); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 689 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 690 | if (NULL == *horizontalScanline) { |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 691 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 692 | SkBlurMask::ComputeBlurProfile(sigma, &profile); |
commit-bot@chromium.org | cc787fa | 2014-01-31 01:00:49 +0000 | [diff] [blame] | 693 | ada.reset(profile); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 694 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 695 | SkAutoTMalloc<uint8_t> horizontalPixels(width); |
| 696 | SkBlurMask::ComputeBlurredScanline(horizontalPixels, profile, width, sigma); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 697 | |
| 698 | *horizontalScanline = context->createTexture(¶ms, texDesc, horizontalCacheID, |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 699 | horizontalPixels, 0); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 700 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 701 | if (NULL == *horizontalScanline) { |
| 702 | return false; |
| 703 | } |
| 704 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 705 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 706 | texDesc.fWidth = 1; |
| 707 | texDesc.fHeight = height; |
| 708 | key.fData32[1] = 1; |
| 709 | key.fData32[2] = height; |
| 710 | GrCacheID verticalCacheID(gBlurProfileDomain, key); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 711 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 712 | *verticalScanline = context->findAndRefTexture(texDesc, verticalCacheID, ¶ms); |
| 713 | if (NULL == *verticalScanline) { |
| 714 | if (NULL == profile) { |
| 715 | SkBlurMask::ComputeBlurProfile(sigma, &profile); |
commit-bot@chromium.org | cc787fa | 2014-01-31 01:00:49 +0000 | [diff] [blame] | 716 | ada.reset(profile); |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 717 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 718 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 719 | SkAutoTMalloc<uint8_t> verticalPixels(height); |
| 720 | SkBlurMask::ComputeBlurredScanline(verticalPixels, profile, height, sigma); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 721 | |
| 722 | *verticalScanline = context->createTexture(¶ms, texDesc, verticalCacheID, |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 723 | verticalPixels, 0); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 724 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 725 | if (NULL == *verticalScanline) { |
commit-bot@chromium.org | 85b45af | 2014-01-31 14:47:57 +0000 | [diff] [blame] | 726 | SkSafeSetNull(*horizontalScanline); |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 727 | return false; |
| 728 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 729 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 730 | } |
| 731 | return true; |
| 732 | } |
| 733 | |
| 734 | GrRectBlurEffect::GrRectBlurEffect(const SkRect& rect, float sigma, |
| 735 | GrTexture *horizontal_scanline, GrTexture *vertical_scanline) |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 736 | : INHERITED(), |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 737 | fWidth(horizontal_scanline->width()), |
| 738 | fHeight(vertical_scanline->width()), |
| 739 | fSigma(sigma), |
| 740 | fHorizontalScanlineAccess(horizontal_scanline), |
| 741 | fVerticalScanlineAccess(vertical_scanline) { |
| 742 | SkMatrix mat; |
| 743 | mat.setRectToRect(rect, SkRect::MakeWH(1,1), SkMatrix::kFill_ScaleToFit); |
| 744 | fTransform.reset(kLocal_GrCoordSet, mat); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 745 | this->addTextureAccess(&fHorizontalScanlineAccess); |
| 746 | this->addTextureAccess(&fVerticalScanlineAccess); |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 747 | this->addCoordTransform(&fTransform); |
| 748 | } |
| 749 | |
| 750 | GrRectBlurEffect::~GrRectBlurEffect() { |
| 751 | } |
| 752 | |
| 753 | const GrBackendEffectFactory& GrRectBlurEffect::getFactory() const { |
| 754 | return GrTBackendEffectFactory<GrRectBlurEffect>::getInstance(); |
| 755 | } |
| 756 | |
| 757 | bool GrRectBlurEffect::onIsEqual(const GrEffect& sBase) const { |
| 758 | const GrRectBlurEffect& s = CastEffect<GrRectBlurEffect>(sBase); |
| 759 | return this->getWidth() == s.getWidth() && |
| 760 | this->getHeight() == s.getHeight() && |
| 761 | this->getSigma() == s.getSigma() && |
| 762 | this->getTransform() == s.getTransform(); |
| 763 | } |
| 764 | |
| 765 | void GrRectBlurEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
| 766 | *validFlags = 0; |
| 767 | return; |
| 768 | } |
| 769 | |
| 770 | GR_DEFINE_EFFECT_TEST(GrRectBlurEffect); |
| 771 | |
| 772 | GrEffectRef* GrRectBlurEffect::TestCreate(SkRandom* random, |
| 773 | GrContext* context, |
| 774 | const GrDrawTargetCaps&, |
| 775 | GrTexture**) { |
| 776 | float sigma = random->nextRangeF(3,8); |
| 777 | float width = random->nextRangeF(200,300); |
| 778 | float height = random->nextRangeF(200,300); |
| 779 | return GrRectBlurEffect::Create(context, SkRect::MakeWH(width, height), sigma); |
| 780 | } |
| 781 | |
| 782 | |
| 783 | bool SkBlurMaskFilterImpl::directFilterMaskGPU(GrContext* context, |
| 784 | GrPaint* grp, |
| 785 | const SkStrokeRec& strokeRec, |
| 786 | const SkPath& path) const { |
| 787 | if (fBlurStyle != SkBlurMaskFilter::kNormal_BlurStyle) { |
| 788 | return false; |
| 789 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 790 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 791 | SkRect rect; |
| 792 | if (!path.isRect(&rect)) { |
| 793 | return false; |
| 794 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 795 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 796 | if (!strokeRec.isFillStyle()) { |
| 797 | return false; |
| 798 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 799 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 800 | SkMatrix ctm = context->getMatrix(); |
| 801 | SkScalar xformedSigma = this->computeXformedSigma(ctm); |
| 802 | rect.outset(3*xformedSigma, 3*xformedSigma); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 803 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 804 | SkAutoTUnref<GrEffectRef> effect(GrRectBlurEffect::Create( |
| 805 | context, rect, xformedSigma)); |
| 806 | if (!effect) { |
| 807 | return false; |
| 808 | } |
| 809 | |
| 810 | GrContext::AutoMatrix am; |
| 811 | if (!am.setIdentity(context, grp)) { |
| 812 | return false; |
| 813 | } |
| 814 | |
| 815 | |
| 816 | grp->addCoverageEffect(effect); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 817 | |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 818 | context->drawRect(*grp, rect); |
| 819 | return true; |
| 820 | } |
| 821 | |
commit-bot@chromium.org | 8213970 | 2014-03-10 22:53:20 +0000 | [diff] [blame] | 822 | bool SkBlurMaskFilterImpl::directFilterRRectMaskGPU(GrContext* context, |
| 823 | GrPaint* grp, |
| 824 | const SkStrokeRec& strokeRec, |
| 825 | const SkRRect& rrect) const { |
| 826 | return false; |
| 827 | } |
| 828 | |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 829 | bool SkBlurMaskFilterImpl::canFilterMaskGPU(const SkRect& srcBounds, |
| 830 | const SkIRect& clipBounds, |
| 831 | const SkMatrix& ctm, |
| 832 | SkRect* maskRect) const { |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 833 | SkScalar xformedSigma = this->computeXformedSigma(ctm); |
| 834 | if (xformedSigma <= 0) { |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 835 | return false; |
reed@google.com | 2b75f42 | 2011-07-07 13:43:38 +0000 | [diff] [blame] | 836 | } |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 837 | |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 838 | static const SkScalar kMIN_GPU_BLUR_SIZE = SkIntToScalar(64); |
| 839 | static const SkScalar kMIN_GPU_BLUR_SIGMA = SkIntToScalar(32); |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 840 | |
| 841 | if (srcBounds.width() <= kMIN_GPU_BLUR_SIZE && |
| 842 | srcBounds.height() <= kMIN_GPU_BLUR_SIZE && |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 843 | xformedSigma <= kMIN_GPU_BLUR_SIGMA) { |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 844 | // We prefer to blur small rect with small radius via CPU. |
| 845 | return false; |
| 846 | } |
| 847 | |
| 848 | if (NULL == maskRect) { |
| 849 | // don't need to compute maskRect |
| 850 | return true; |
| 851 | } |
| 852 | |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 853 | float sigma3 = 3 * SkScalarToFloat(xformedSigma); |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 854 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 855 | SkRect clipRect = SkRect::Make(clipBounds); |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 856 | SkRect srcRect(srcBounds); |
| 857 | |
| 858 | // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area. |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 859 | srcRect.outset(sigma3, sigma3); |
| 860 | clipRect.outset(sigma3, sigma3); |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 861 | srcRect.intersect(clipRect); |
| 862 | *maskRect = srcRect; |
skia.committer@gmail.com | 1842adf | 2013-07-04 07:01:07 +0000 | [diff] [blame] | 863 | return true; |
reed@google.com | 2b75f42 | 2011-07-07 13:43:38 +0000 | [diff] [blame] | 864 | } |
| 865 | |
skia.committer@gmail.com | 1842adf | 2013-07-04 07:01:07 +0000 | [diff] [blame] | 866 | bool SkBlurMaskFilterImpl::filterMaskGPU(GrTexture* src, |
commit-bot@chromium.org | 439ff1b | 2014-01-13 16:39:39 +0000 | [diff] [blame] | 867 | const SkMatrix& ctm, |
skia.committer@gmail.com | 1842adf | 2013-07-04 07:01:07 +0000 | [diff] [blame] | 868 | const SkRect& maskRect, |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 869 | GrTexture** result, |
| 870 | bool canOverwriteSrc) const { |
| 871 | SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height()); |
| 872 | |
| 873 | GrContext* context = src->getContext(); |
| 874 | |
| 875 | GrContext::AutoWideOpenIdentityDraw awo(context, NULL); |
| 876 | |
commit-bot@chromium.org | 439ff1b | 2014-01-13 16:39:39 +0000 | [diff] [blame] | 877 | SkScalar xformedSigma = this->computeXformedSigma(ctm); |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 878 | SkASSERT(xformedSigma > 0); |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 879 | |
| 880 | // If we're doing a normal blur, we can clobber the pathTexture in the |
| 881 | // gaussianBlur. Otherwise, we need to save it for later compositing. |
| 882 | bool isNormalBlur = (SkBlurMaskFilter::kNormal_BlurStyle == fBlurStyle); |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 883 | *result = SkGpuBlurUtils::GaussianBlur(context, src, isNormalBlur && canOverwriteSrc, |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 884 | clipRect, false, xformedSigma, xformedSigma); |
robertphillips@google.com | 3cc820c | 2013-07-03 19:36:55 +0000 | [diff] [blame] | 885 | if (NULL == *result) { |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 886 | return false; |
| 887 | } |
| 888 | |
| 889 | if (!isNormalBlur) { |
| 890 | context->setIdentityMatrix(); |
| 891 | GrPaint paint; |
| 892 | SkMatrix matrix; |
| 893 | matrix.setIDiv(src->width(), src->height()); |
| 894 | // Blend pathTexture over blurTexture. |
| 895 | GrContext::AutoRenderTarget art(context, (*result)->asRenderTarget()); |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 896 | paint.addColorEffect(GrSimpleTextureEffect::Create(src, matrix))->unref(); |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 897 | if (SkBlurMaskFilter::kInner_BlurStyle == fBlurStyle) { |
| 898 | // inner: dst = dst * src |
| 899 | paint.setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff); |
| 900 | } else if (SkBlurMaskFilter::kSolid_BlurStyle == fBlurStyle) { |
| 901 | // solid: dst = src + dst - src * dst |
| 902 | // = (1 - dst) * src + 1 * dst |
| 903 | paint.setBlendFunc(kIDC_GrBlendCoeff, kOne_GrBlendCoeff); |
| 904 | } else if (SkBlurMaskFilter::kOuter_BlurStyle == fBlurStyle) { |
| 905 | // outer: dst = dst * (1 - src) |
| 906 | // = 0 * src + (1 - src) * dst |
| 907 | paint.setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff); |
| 908 | } |
| 909 | context->drawRect(paint, clipRect); |
| 910 | } |
| 911 | |
| 912 | return true; |
| 913 | } |
| 914 | |
| 915 | #endif // SK_SUPPORT_GPU |
| 916 | |
| 917 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 918 | #ifndef SK_IGNORE_TO_STRING |
robertphillips@google.com | 0bd80fa | 2013-03-18 17:53:38 +0000 | [diff] [blame] | 919 | void SkBlurMaskFilterImpl::toString(SkString* str) const { |
| 920 | str->append("SkBlurMaskFilterImpl: ("); |
| 921 | |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 922 | str->append("sigma: "); |
| 923 | str->appendScalar(fSigma); |
robertphillips@google.com | 0bd80fa | 2013-03-18 17:53:38 +0000 | [diff] [blame] | 924 | str->append(" "); |
| 925 | |
| 926 | static const char* gStyleName[SkBlurMaskFilter::kBlurStyleCount] = { |
| 927 | "normal", "solid", "outer", "inner" |
| 928 | }; |
| 929 | |
| 930 | str->appendf("style: %s ", gStyleName[fBlurStyle]); |
| 931 | str->append("flags: ("); |
| 932 | if (fBlurFlags) { |
| 933 | bool needSeparator = false; |
skia.committer@gmail.com | 8eaddb0 | 2013-03-19 07:15:10 +0000 | [diff] [blame] | 934 | SkAddFlagToString(str, |
| 935 | SkToBool(fBlurFlags & SkBlurMaskFilter::kIgnoreTransform_BlurFlag), |
robertphillips@google.com | 0bd80fa | 2013-03-18 17:53:38 +0000 | [diff] [blame] | 936 | "IgnoreXform", &needSeparator); |
skia.committer@gmail.com | 8eaddb0 | 2013-03-19 07:15:10 +0000 | [diff] [blame] | 937 | SkAddFlagToString(str, |
| 938 | SkToBool(fBlurFlags & SkBlurMaskFilter::kHighQuality_BlurFlag), |
robertphillips@google.com | 0bd80fa | 2013-03-18 17:53:38 +0000 | [diff] [blame] | 939 | "HighQuality", &needSeparator); |
| 940 | } else { |
| 941 | str->append("None"); |
| 942 | } |
| 943 | str->append("))"); |
| 944 | } |
| 945 | #endif |
| 946 | |
caryclark@google.com | d26147a | 2011-12-15 14:16:43 +0000 | [diff] [blame] | 947 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkBlurMaskFilter) |
| 948 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkBlurMaskFilterImpl) |
| 949 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |