senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
djsollen@google.com | 64a0ec3 | 2012-06-12 15:17:27 +0000 | [diff] [blame] | 8 | #include "SkBitmap.h" |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 9 | #include "SkBlurImageFilter.h" |
senorblanco@chromium.org | ae814c7 | 2011-12-20 20:02:19 +0000 | [diff] [blame] | 10 | #include "SkColorPriv.h" |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 11 | #include "SkFlattenableBuffers.h" |
robertphillips@google.com | 736dd03 | 2013-07-15 15:06:54 +0000 | [diff] [blame] | 12 | #include "SkGpuBlurUtils.h" |
senorblanco@chromium.org | 27eec46 | 2013-11-08 20:49:04 +0000 | [diff] [blame] | 13 | #include "SkBlurImage_opts.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 14 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 302cffb | 2012-08-01 20:16:34 +0000 | [diff] [blame] | 15 | #include "GrContext.h" |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 16 | #include "SkImageFilterUtils.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 17 | #endif |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 18 | |
senorblanco@chromium.org | 54e01b2 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 19 | SkBlurImageFilter::SkBlurImageFilter(SkFlattenableReadBuffer& buffer) |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 20 | : INHERITED(1, buffer) { |
senorblanco@chromium.org | 54e01b2 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 21 | fSigma.fWidth = buffer.readScalar(); |
| 22 | fSigma.fHeight = buffer.readScalar(); |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 23 | buffer.validate(SkScalarIsFinite(fSigma.fWidth) && |
| 24 | SkScalarIsFinite(fSigma.fHeight) && |
| 25 | (fSigma.fWidth >= 0) && |
| 26 | (fSigma.fHeight >= 0)); |
senorblanco@chromium.org | 54e01b2 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 27 | } |
| 28 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 29 | SkBlurImageFilter::SkBlurImageFilter(SkScalar sigmaX, |
| 30 | SkScalar sigmaY, |
| 31 | SkImageFilter* input, |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 32 | const CropRect* cropRect) |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 33 | : INHERITED(input, cropRect), fSigma(SkSize::Make(sigmaX, sigmaY)) { |
senorblanco@chromium.org | 60014ca | 2011-11-09 16:05:58 +0000 | [diff] [blame] | 34 | SkASSERT(sigmaX >= 0 && sigmaY >= 0); |
| 35 | } |
| 36 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 37 | void SkBlurImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { |
senorblanco@chromium.org | 54e01b2 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 38 | this->INHERITED::flatten(buffer); |
| 39 | buffer.writeScalar(fSigma.fWidth); |
| 40 | buffer.writeScalar(fSigma.fHeight); |
| 41 | } |
| 42 | |
senorblanco@chromium.org | 0cc00c2 | 2013-11-07 18:35:12 +0000 | [diff] [blame] | 43 | enum BlurDirection { |
| 44 | kX, kY |
| 45 | }; |
| 46 | |
| 47 | /** |
| 48 | * |
| 49 | * In order to make memory accesses cache-friendly, we reorder the passes to |
| 50 | * use contiguous memory reads wherever possible. |
| 51 | * |
| 52 | * For example, the 6 passes of the X-and-Y blur case are rewritten as |
| 53 | * follows. Instead of 3 passes in X and 3 passes in Y, we perform |
| 54 | * 2 passes in X, 1 pass in X transposed to Y on write, 2 passes in X, |
| 55 | * then 1 pass in X transposed to Y on write. |
| 56 | * |
| 57 | * +----+ +----+ +----+ +---+ +---+ +---+ +----+ |
| 58 | * + AB + ----> | AB | ----> | AB | -----> | A | ----> | A | ----> | A | -----> | AB | |
| 59 | * +----+ blurX +----+ blurX +----+ blurXY | B | blurX | B | blurX | B | blurXY +----+ |
| 60 | * +---+ +---+ +---+ |
| 61 | * |
| 62 | * In this way, two of the y-blurs become x-blurs applied to transposed |
| 63 | * images, and all memory reads are contiguous. |
| 64 | */ |
| 65 | |
| 66 | template<BlurDirection srcDirection, BlurDirection dstDirection> |
| 67 | static void boxBlur(const SkPMColor* src, int srcStride, SkPMColor* dst, int kernelSize, |
| 68 | int leftOffset, int rightOffset, int width, int height) |
senorblanco@chromium.org | ae814c7 | 2011-12-20 20:02:19 +0000 | [diff] [blame] | 69 | { |
senorblanco@chromium.org | ae814c7 | 2011-12-20 20:02:19 +0000 | [diff] [blame] | 70 | int rightBorder = SkMin32(rightOffset + 1, width); |
senorblanco@chromium.org | 0cc00c2 | 2013-11-07 18:35:12 +0000 | [diff] [blame] | 71 | int srcStrideX = srcDirection == kX ? 1 : srcStride; |
| 72 | int dstStrideX = dstDirection == kX ? 1 : height; |
| 73 | int srcStrideY = srcDirection == kX ? srcStride : 1; |
| 74 | int dstStrideY = dstDirection == kX ? width : 1; |
senorblanco@chromium.org | fe2faa8 | 2013-11-04 16:07:33 +0000 | [diff] [blame] | 75 | uint32_t scale = (1 << 24) / kernelSize; |
| 76 | uint32_t half = 1 << 23; |
senorblanco@chromium.org | ae814c7 | 2011-12-20 20:02:19 +0000 | [diff] [blame] | 77 | for (int y = 0; y < height; ++y) { |
| 78 | int sumA = 0, sumR = 0, sumG = 0, sumB = 0; |
senorblanco@chromium.org | 0cc00c2 | 2013-11-07 18:35:12 +0000 | [diff] [blame] | 79 | const SkPMColor* p = src; |
senorblanco@chromium.org | ae814c7 | 2011-12-20 20:02:19 +0000 | [diff] [blame] | 80 | for (int i = 0; i < rightBorder; ++i) { |
| 81 | sumA += SkGetPackedA32(*p); |
| 82 | sumR += SkGetPackedR32(*p); |
| 83 | sumG += SkGetPackedG32(*p); |
| 84 | sumB += SkGetPackedB32(*p); |
senorblanco@chromium.org | 0cc00c2 | 2013-11-07 18:35:12 +0000 | [diff] [blame] | 85 | p += srcStrideX; |
senorblanco@chromium.org | ae814c7 | 2011-12-20 20:02:19 +0000 | [diff] [blame] | 86 | } |
| 87 | |
senorblanco@chromium.org | 0cc00c2 | 2013-11-07 18:35:12 +0000 | [diff] [blame] | 88 | const SkPMColor* sptr = src; |
| 89 | SkColor* dptr = dst; |
senorblanco@chromium.org | ae814c7 | 2011-12-20 20:02:19 +0000 | [diff] [blame] | 90 | for (int x = 0; x < width; ++x) { |
senorblanco@chromium.org | fe2faa8 | 2013-11-04 16:07:33 +0000 | [diff] [blame] | 91 | *dptr = SkPackARGB32((sumA * scale + half) >> 24, |
| 92 | (sumR * scale + half) >> 24, |
| 93 | (sumG * scale + half) >> 24, |
| 94 | (sumB * scale + half) >> 24); |
senorblanco@chromium.org | ae814c7 | 2011-12-20 20:02:19 +0000 | [diff] [blame] | 95 | if (x >= leftOffset) { |
senorblanco@chromium.org | 0cc00c2 | 2013-11-07 18:35:12 +0000 | [diff] [blame] | 96 | SkColor l = *(sptr - leftOffset * srcStrideX); |
senorblanco@chromium.org | ae814c7 | 2011-12-20 20:02:19 +0000 | [diff] [blame] | 97 | sumA -= SkGetPackedA32(l); |
| 98 | sumR -= SkGetPackedR32(l); |
| 99 | sumG -= SkGetPackedG32(l); |
| 100 | sumB -= SkGetPackedB32(l); |
| 101 | } |
| 102 | if (x + rightOffset + 1 < width) { |
senorblanco@chromium.org | 0cc00c2 | 2013-11-07 18:35:12 +0000 | [diff] [blame] | 103 | SkColor r = *(sptr + (rightOffset + 1) * srcStrideX); |
senorblanco@chromium.org | ae814c7 | 2011-12-20 20:02:19 +0000 | [diff] [blame] | 104 | sumA += SkGetPackedA32(r); |
| 105 | sumR += SkGetPackedR32(r); |
| 106 | sumG += SkGetPackedG32(r); |
| 107 | sumB += SkGetPackedB32(r); |
| 108 | } |
senorblanco@chromium.org | 0cc00c2 | 2013-11-07 18:35:12 +0000 | [diff] [blame] | 109 | sptr += srcStrideX; |
| 110 | if (srcDirection == kY) { |
| 111 | SK_PREFETCH(sptr + (rightOffset + 1) * srcStrideX); |
| 112 | } |
| 113 | dptr += dstStrideX; |
senorblanco@chromium.org | ae814c7 | 2011-12-20 20:02:19 +0000 | [diff] [blame] | 114 | } |
senorblanco@chromium.org | 0cc00c2 | 2013-11-07 18:35:12 +0000 | [diff] [blame] | 115 | src += srcStrideY; |
| 116 | dst += dstStrideY; |
senorblanco@chromium.org | ae814c7 | 2011-12-20 20:02:19 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 120 | static void getBox3Params(SkScalar s, int *kernelSize, int* kernelSize3, int *lowOffset, |
| 121 | int *highOffset) |
senorblanco@chromium.org | ae814c7 | 2011-12-20 20:02:19 +0000 | [diff] [blame] | 122 | { |
schenney@chromium.org | 73f3ded | 2011-12-20 22:31:40 +0000 | [diff] [blame] | 123 | float pi = SkScalarToFloat(SK_ScalarPI); |
| 124 | int d = static_cast<int>(floorf(SkScalarToFloat(s) * 3.0f * sqrtf(2.0f * pi) / 4.0f + 0.5f)); |
senorblanco@chromium.org | ae814c7 | 2011-12-20 20:02:19 +0000 | [diff] [blame] | 125 | *kernelSize = d; |
| 126 | if (d % 2 == 1) { |
| 127 | *lowOffset = *highOffset = (d - 1) / 2; |
| 128 | *kernelSize3 = d; |
| 129 | } else { |
| 130 | *highOffset = d / 2; |
| 131 | *lowOffset = *highOffset - 1; |
| 132 | *kernelSize3 = d + 1; |
| 133 | } |
| 134 | } |
| 135 | |
senorblanco@chromium.org | f1369ce | 2012-08-20 14:53:21 +0000 | [diff] [blame] | 136 | bool SkBlurImageFilter::onFilterImage(Proxy* proxy, |
| 137 | const SkBitmap& source, const SkMatrix& ctm, |
| 138 | SkBitmap* dst, SkIPoint* offset) { |
senorblanco@chromium.org | 6840076 | 2013-05-24 15:04:07 +0000 | [diff] [blame] | 139 | SkBitmap src = source; |
| 140 | if (getInput(0) && !getInput(0)->filterImage(proxy, source, ctm, &src, offset)) { |
| 141 | return false; |
| 142 | } |
| 143 | |
senorblanco@chromium.org | ae814c7 | 2011-12-20 20:02:19 +0000 | [diff] [blame] | 144 | if (src.config() != SkBitmap::kARGB_8888_Config) { |
| 145 | return false; |
| 146 | } |
| 147 | |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 148 | SkAutoLockPixels alp(src); |
| 149 | if (!src.getPixels()) { |
| 150 | return false; |
| 151 | } |
| 152 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 153 | SkIRect srcBounds, dstBounds; |
| 154 | src.getBounds(&srcBounds); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 155 | if (!this->applyCropRect(&srcBounds, ctm)) { |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 156 | return false; |
| 157 | } |
| 158 | |
| 159 | dst->setConfig(src.config(), srcBounds.width(), srcBounds.height()); |
| 160 | dst->getBounds(&dstBounds); |
senorblanco@chromium.org | ae814c7 | 2011-12-20 20:02:19 +0000 | [diff] [blame] | 161 | dst->allocPixels(); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame^] | 162 | if (!dst->getPixels()) { |
| 163 | return false; |
| 164 | } |
| 165 | |
senorblanco@chromium.org | ae814c7 | 2011-12-20 20:02:19 +0000 | [diff] [blame] | 166 | int kernelSizeX, kernelSizeX3, lowOffsetX, highOffsetX; |
| 167 | int kernelSizeY, kernelSizeY3, lowOffsetY, highOffsetY; |
| 168 | getBox3Params(fSigma.width(), &kernelSizeX, &kernelSizeX3, &lowOffsetX, &highOffsetX); |
| 169 | getBox3Params(fSigma.height(), &kernelSizeY, &kernelSizeY3, &lowOffsetY, &highOffsetY); |
| 170 | |
| 171 | if (kernelSizeX < 0 || kernelSizeY < 0) { |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | if (kernelSizeX == 0 && kernelSizeY == 0) { |
| 176 | src.copyTo(dst, dst->config()); |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | SkBitmap temp; |
| 181 | temp.setConfig(dst->config(), dst->width(), dst->height()); |
| 182 | if (!temp.allocPixels()) { |
| 183 | return false; |
| 184 | } |
| 185 | |
senorblanco@chromium.org | 0cc00c2 | 2013-11-07 18:35:12 +0000 | [diff] [blame] | 186 | const SkPMColor* s = src.getAddr32(srcBounds.left(), srcBounds.top()); |
| 187 | SkPMColor* t = temp.getAddr32(0, 0); |
| 188 | SkPMColor* d = dst->getAddr32(0, 0); |
| 189 | int w = dstBounds.width(), h = dstBounds.height(); |
| 190 | int sw = src.rowBytesAsPixels(); |
senorblanco@chromium.org | 05edd02 | 2013-11-11 20:12:34 +0000 | [diff] [blame] | 191 | SkBoxBlurProc boxBlurX, boxBlurY, boxBlurXY, boxBlurYX; |
| 192 | if (!SkBoxBlurGetPlatformProcs(&boxBlurX, &boxBlurY, &boxBlurXY, &boxBlurYX)) { |
senorblanco@chromium.org | 27eec46 | 2013-11-08 20:49:04 +0000 | [diff] [blame] | 193 | boxBlurX = boxBlur<kX, kX>; |
| 194 | boxBlurY = boxBlur<kY, kY>; |
| 195 | boxBlurXY = boxBlur<kX, kY>; |
senorblanco@chromium.org | 05edd02 | 2013-11-11 20:12:34 +0000 | [diff] [blame] | 196 | boxBlurYX = boxBlur<kY, kX>; |
senorblanco@chromium.org | 27eec46 | 2013-11-08 20:49:04 +0000 | [diff] [blame] | 197 | } |
| 198 | |
senorblanco@chromium.org | ae814c7 | 2011-12-20 20:02:19 +0000 | [diff] [blame] | 199 | if (kernelSizeX > 0 && kernelSizeY > 0) { |
senorblanco@chromium.org | 0cc00c2 | 2013-11-07 18:35:12 +0000 | [diff] [blame] | 200 | boxBlurX(s, sw, t, kernelSizeX, lowOffsetX, highOffsetX, w, h); |
| 201 | boxBlurX(t, w, d, kernelSizeX, highOffsetX, lowOffsetX, w, h); |
| 202 | boxBlurXY(d, w, t, kernelSizeX3, highOffsetX, highOffsetX, w, h); |
| 203 | boxBlurX(t, h, d, kernelSizeY, lowOffsetY, highOffsetY, h, w); |
| 204 | boxBlurX(d, h, t, kernelSizeY, highOffsetY, lowOffsetY, h, w); |
| 205 | boxBlurXY(t, h, d, kernelSizeY3, highOffsetY, highOffsetY, h, w); |
senorblanco@chromium.org | ae814c7 | 2011-12-20 20:02:19 +0000 | [diff] [blame] | 206 | } else if (kernelSizeX > 0) { |
senorblanco@chromium.org | 0cc00c2 | 2013-11-07 18:35:12 +0000 | [diff] [blame] | 207 | boxBlurX(s, sw, d, kernelSizeX, lowOffsetX, highOffsetX, w, h); |
| 208 | boxBlurX(d, w, t, kernelSizeX, highOffsetX, lowOffsetX, w, h); |
| 209 | boxBlurX(t, w, d, kernelSizeX3, highOffsetX, highOffsetX, w, h); |
senorblanco@chromium.org | ae814c7 | 2011-12-20 20:02:19 +0000 | [diff] [blame] | 210 | } else if (kernelSizeY > 0) { |
senorblanco@chromium.org | 05edd02 | 2013-11-11 20:12:34 +0000 | [diff] [blame] | 211 | boxBlurYX(s, sw, d, kernelSizeY, lowOffsetY, highOffsetY, h, w); |
| 212 | boxBlurX(d, h, t, kernelSizeY, highOffsetY, lowOffsetY, h, w); |
| 213 | boxBlurXY(t, h, d, kernelSizeY3, highOffsetY, highOffsetY, h, w); |
senorblanco@chromium.org | ae814c7 | 2011-12-20 20:02:19 +0000 | [diff] [blame] | 214 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 215 | offset->fX += srcBounds.fLeft; |
| 216 | offset->fY += srcBounds.fTop; |
senorblanco@chromium.org | ae814c7 | 2011-12-20 20:02:19 +0000 | [diff] [blame] | 217 | return true; |
| 218 | } |
| 219 | |
commit-bot@chromium.org | 1aa54bf | 2013-08-05 16:53:50 +0000 | [diff] [blame] | 220 | bool SkBlurImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const SkMatrix& ctm, |
| 221 | SkBitmap* result, SkIPoint* offset) { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 222 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 223 | SkBitmap input; |
commit-bot@chromium.org | 1aa54bf | 2013-08-05 16:53:50 +0000 | [diff] [blame] | 224 | if (!SkImageFilterUtils::GetInputResultGPU(getInput(0), proxy, src, ctm, &input, offset)) { |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 225 | return false; |
| 226 | } |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 227 | GrTexture* source = input.getTexture(); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 228 | SkIRect rect; |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 229 | src.getBounds(&rect); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 230 | if (!this->applyCropRect(&rect, ctm)) { |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 231 | return false; |
| 232 | } |
skia.committer@gmail.com | 6ae6383 | 2013-07-23 07:01:05 +0000 | [diff] [blame] | 233 | SkAutoTUnref<GrTexture> tex(SkGpuBlurUtils::GaussianBlur(source->getContext(), |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 234 | source, |
| 235 | false, |
| 236 | SkRect::Make(rect), |
| 237 | true, |
| 238 | fSigma.width(), |
| 239 | fSigma.height())); |
| 240 | offset->fX += rect.fLeft; |
| 241 | offset->fY += rect.fTop; |
| 242 | return SkImageFilterUtils::WrapTexture(tex, rect.width(), rect.height(), result); |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 243 | #else |
| 244 | SkDEBUGFAIL("Should not call in GPU-less build"); |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 245 | return false; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 246 | #endif |
senorblanco@chromium.org | 302cffb | 2012-08-01 20:16:34 +0000 | [diff] [blame] | 247 | } |