senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkMatrixConvolutionImageFilter.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkColorPriv.h" |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 11 | #include "SkDevice.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 12 | #include "SkReadBuffer.h" |
| 13 | #include "SkWriteBuffer.h" |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 14 | #include "SkRect.h" |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 15 | #include "SkUnPreMultiply.h" |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 16 | |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 17 | #if SK_SUPPORT_GPU |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 18 | #include "effects/GrMatrixConvolutionEffect.h" |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 19 | #endif |
| 20 | |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 21 | // We need to be able to read at most SK_MaxS32 bytes, so divide that |
| 22 | // by the size of a scalar to know how many scalars we can read. |
| 23 | static const int32_t gMaxKernelSize = SK_MaxS32 / sizeof(SkScalar); |
| 24 | |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 25 | SkMatrixConvolutionImageFilter::SkMatrixConvolutionImageFilter( |
| 26 | const SkISize& kernelSize, |
| 27 | const SkScalar* kernel, |
| 28 | SkScalar gain, |
| 29 | SkScalar bias, |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 30 | const SkIPoint& kernelOffset, |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 31 | TileMode tileMode, |
| 32 | bool convolveAlpha, |
| 33 | SkImageFilter* input, |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 34 | const CropRect* cropRect) |
| 35 | : INHERITED(1, &input, cropRect), |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 36 | fKernelSize(kernelSize), |
| 37 | fGain(gain), |
| 38 | fBias(bias), |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 39 | fKernelOffset(kernelOffset), |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 40 | fTileMode(tileMode), |
| 41 | fConvolveAlpha(convolveAlpha) { |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 42 | size_t size = (size_t) sk_64_mul(fKernelSize.width(), fKernelSize.height()); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 43 | fKernel = new SkScalar[size]; |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 44 | memcpy(fKernel, kernel, size * sizeof(SkScalar)); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 45 | SkASSERT(kernelSize.fWidth >= 1 && kernelSize.fHeight >= 1); |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 46 | SkASSERT(kernelOffset.fX >= 0 && kernelOffset.fX < kernelSize.fWidth); |
| 47 | SkASSERT(kernelOffset.fY >= 0 && kernelOffset.fY < kernelSize.fHeight); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 48 | } |
| 49 | |
reed | 5e1ddb1 | 2015-12-21 08:52:45 -0800 | [diff] [blame] | 50 | SkImageFilter* SkMatrixConvolutionImageFilter::Create( |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 51 | const SkISize& kernelSize, |
| 52 | const SkScalar* kernel, |
| 53 | SkScalar gain, |
| 54 | SkScalar bias, |
| 55 | const SkIPoint& kernelOffset, |
| 56 | TileMode tileMode, |
| 57 | bool convolveAlpha, |
| 58 | SkImageFilter* input, |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 59 | const CropRect* cropRect) { |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 60 | if (kernelSize.width() < 1 || kernelSize.height() < 1) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 61 | return nullptr; |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 62 | } |
| 63 | if (gMaxKernelSize / kernelSize.fWidth < kernelSize.fHeight) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 64 | return nullptr; |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 65 | } |
| 66 | if (!kernel) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 67 | return nullptr; |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 68 | } |
sugoi | 1379b87 | 2014-11-17 05:45:55 -0800 | [diff] [blame] | 69 | if ((kernelOffset.fX < 0) || (kernelOffset.fX >= kernelSize.fWidth) || |
| 70 | (kernelOffset.fY < 0) || (kernelOffset.fY >= kernelSize.fHeight)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 71 | return nullptr; |
sugoi | 1379b87 | 2014-11-17 05:45:55 -0800 | [diff] [blame] | 72 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 73 | return new SkMatrixConvolutionImageFilter(kernelSize, kernel, gain, bias, kernelOffset, |
| 74 | tileMode, convolveAlpha, input, cropRect); |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 75 | } |
| 76 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 77 | SkFlattenable* SkMatrixConvolutionImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 78 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 79 | SkISize kernelSize; |
| 80 | kernelSize.fWidth = buffer.readInt(); |
| 81 | kernelSize.fHeight = buffer.readInt(); |
| 82 | const int count = buffer.getArrayCount(); |
| 83 | |
| 84 | const int64_t kernelArea = sk_64_mul(kernelSize.width(), kernelSize.height()); |
| 85 | if (!buffer.validate(kernelArea == count)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 86 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 87 | } |
| 88 | SkAutoSTArray<16, SkScalar> kernel(count); |
| 89 | if (!buffer.readScalarArray(kernel.get(), count)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 90 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 91 | } |
| 92 | SkScalar gain = buffer.readScalar(); |
| 93 | SkScalar bias = buffer.readScalar(); |
| 94 | SkIPoint kernelOffset; |
| 95 | kernelOffset.fX = buffer.readInt(); |
| 96 | kernelOffset.fY = buffer.readInt(); |
| 97 | TileMode tileMode = (TileMode)buffer.readInt(); |
| 98 | bool convolveAlpha = buffer.readBool(); |
| 99 | return Create(kernelSize, kernel.get(), gain, bias, kernelOffset, tileMode, convolveAlpha, |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 100 | common.getInput(0), &common.cropRect()); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 101 | } |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 102 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 103 | void SkMatrixConvolutionImageFilter::flatten(SkWriteBuffer& buffer) const { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 104 | this->INHERITED::flatten(buffer); |
| 105 | buffer.writeInt(fKernelSize.fWidth); |
| 106 | buffer.writeInt(fKernelSize.fHeight); |
| 107 | buffer.writeScalarArray(fKernel, fKernelSize.fWidth * fKernelSize.fHeight); |
| 108 | buffer.writeScalar(fGain); |
| 109 | buffer.writeScalar(fBias); |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 110 | buffer.writeInt(fKernelOffset.fX); |
| 111 | buffer.writeInt(fKernelOffset.fY); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 112 | buffer.writeInt((int) fTileMode); |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 113 | buffer.writeBool(fConvolveAlpha); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | SkMatrixConvolutionImageFilter::~SkMatrixConvolutionImageFilter() { |
| 117 | delete[] fKernel; |
| 118 | } |
| 119 | |
| 120 | class UncheckedPixelFetcher { |
| 121 | public: |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 122 | static inline SkPMColor fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 123 | return *src.getAddr32(x, y); |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | class ClampPixelFetcher { |
| 128 | public: |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 129 | static inline SkPMColor fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) { |
bungeman | 62ce030 | 2015-08-28 09:09:32 -0700 | [diff] [blame] | 130 | x = SkTPin(x, bounds.fLeft, bounds.fRight - 1); |
| 131 | y = SkTPin(y, bounds.fTop, bounds.fBottom - 1); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 132 | return *src.getAddr32(x, y); |
| 133 | } |
| 134 | }; |
| 135 | |
| 136 | class RepeatPixelFetcher { |
| 137 | public: |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 138 | static inline SkPMColor fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) { |
| 139 | x = (x - bounds.left()) % bounds.width() + bounds.left(); |
| 140 | y = (y - bounds.top()) % bounds.height() + bounds.top(); |
| 141 | if (x < bounds.left()) { |
| 142 | x += bounds.width(); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 143 | } |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 144 | if (y < bounds.top()) { |
| 145 | y += bounds.height(); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 146 | } |
| 147 | return *src.getAddr32(x, y); |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | class ClampToBlackPixelFetcher { |
| 152 | public: |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 153 | static inline SkPMColor fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) { |
| 154 | if (x < bounds.fLeft || x >= bounds.fRight || y < bounds.fTop || y >= bounds.fBottom) { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 155 | return 0; |
| 156 | } else { |
| 157 | return *src.getAddr32(x, y); |
| 158 | } |
| 159 | } |
| 160 | }; |
| 161 | |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 162 | template<class PixelFetcher, bool convolveAlpha> |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 163 | void SkMatrixConvolutionImageFilter::filterPixels(const SkBitmap& src, |
| 164 | SkBitmap* result, |
senorblanco@chromium.org | 8c7372b | 2014-05-02 19:13:11 +0000 | [diff] [blame] | 165 | const SkIRect& r, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 166 | const SkIRect& bounds) const { |
senorblanco@chromium.org | 8c7372b | 2014-05-02 19:13:11 +0000 | [diff] [blame] | 167 | SkIRect rect(r); |
| 168 | if (!rect.intersect(bounds)) { |
senorblanco@chromium.org | 9195743 | 2014-05-01 14:03:41 +0000 | [diff] [blame] | 169 | return; |
| 170 | } |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 171 | for (int y = rect.fTop; y < rect.fBottom; ++y) { |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 172 | SkPMColor* dptr = result->getAddr32(rect.fLeft - bounds.fLeft, y - bounds.fTop); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 173 | for (int x = rect.fLeft; x < rect.fRight; ++x) { |
| 174 | SkScalar sumA = 0, sumR = 0, sumG = 0, sumB = 0; |
| 175 | for (int cy = 0; cy < fKernelSize.fHeight; cy++) { |
| 176 | for (int cx = 0; cx < fKernelSize.fWidth; cx++) { |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 177 | SkPMColor s = PixelFetcher::fetch(src, |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 178 | x + cx - fKernelOffset.fX, |
| 179 | y + cy - fKernelOffset.fY, |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 180 | bounds); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 181 | SkScalar k = fKernel[cy * fKernelSize.fWidth + cx]; |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 182 | if (convolveAlpha) { |
| 183 | sumA += SkScalarMul(SkIntToScalar(SkGetPackedA32(s)), k); |
| 184 | } |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 185 | sumR += SkScalarMul(SkIntToScalar(SkGetPackedR32(s)), k); |
| 186 | sumG += SkScalarMul(SkIntToScalar(SkGetPackedG32(s)), k); |
| 187 | sumB += SkScalarMul(SkIntToScalar(SkGetPackedB32(s)), k); |
| 188 | } |
| 189 | } |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 190 | int a = convolveAlpha |
| 191 | ? SkClampMax(SkScalarFloorToInt(SkScalarMul(sumA, fGain) + fBias), 255) |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 192 | : 255; |
senorblanco@chromium.org | cc9471c | 2012-09-20 17:59:49 +0000 | [diff] [blame] | 193 | int r = SkClampMax(SkScalarFloorToInt(SkScalarMul(sumR, fGain) + fBias), a); |
| 194 | int g = SkClampMax(SkScalarFloorToInt(SkScalarMul(sumG, fGain) + fBias), a); |
| 195 | int b = SkClampMax(SkScalarFloorToInt(SkScalarMul(sumB, fGain) + fBias), a); |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 196 | if (!convolveAlpha) { |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 197 | a = SkGetPackedA32(PixelFetcher::fetch(src, x, y, bounds)); |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 198 | *dptr++ = SkPreMultiplyARGB(a, r, g, b); |
| 199 | } else { |
| 200 | *dptr++ = SkPackARGB32(a, r, g, b); |
| 201 | } |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 206 | template<class PixelFetcher> |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 207 | void SkMatrixConvolutionImageFilter::filterPixels(const SkBitmap& src, |
| 208 | SkBitmap* result, |
| 209 | const SkIRect& rect, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 210 | const SkIRect& bounds) const { |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 211 | if (fConvolveAlpha) { |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 212 | filterPixels<PixelFetcher, true>(src, result, rect, bounds); |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 213 | } else { |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 214 | filterPixels<PixelFetcher, false>(src, result, rect, bounds); |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 218 | void SkMatrixConvolutionImageFilter::filterInteriorPixels(const SkBitmap& src, |
| 219 | SkBitmap* result, |
| 220 | const SkIRect& rect, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 221 | const SkIRect& bounds) const { |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 222 | filterPixels<UncheckedPixelFetcher>(src, result, rect, bounds); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 223 | } |
| 224 | |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 225 | void SkMatrixConvolutionImageFilter::filterBorderPixels(const SkBitmap& src, |
| 226 | SkBitmap* result, |
| 227 | const SkIRect& rect, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 228 | const SkIRect& bounds) const { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 229 | switch (fTileMode) { |
| 230 | case kClamp_TileMode: |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 231 | filterPixels<ClampPixelFetcher>(src, result, rect, bounds); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 232 | break; |
| 233 | case kRepeat_TileMode: |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 234 | filterPixels<RepeatPixelFetcher>(src, result, rect, bounds); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 235 | break; |
| 236 | case kClampToBlack_TileMode: |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 237 | filterPixels<ClampToBlackPixelFetcher>(src, result, rect, bounds); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 238 | break; |
| 239 | } |
| 240 | } |
| 241 | |
senorblanco@chromium.org | 377c14a | 2013-02-04 22:57:21 +0000 | [diff] [blame] | 242 | // FIXME: This should be refactored to SkImageFilterUtils for |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 243 | // use by other filters. For now, we assume the input is always |
| 244 | // premultiplied and unpremultiply it |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 245 | static SkBitmap unpremultiplyBitmap(SkImageFilter::Proxy* proxy, const SkBitmap& src) |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 246 | { |
| 247 | SkAutoLockPixels alp(src); |
| 248 | if (!src.getPixels()) { |
| 249 | return SkBitmap(); |
| 250 | } |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 251 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(src.width(), src.height())); |
| 252 | if (!device) { |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 253 | return SkBitmap(); |
| 254 | } |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 255 | SkBitmap result = device->accessBitmap(false); |
| 256 | SkAutoLockPixels alp_result(result); |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 257 | for (int y = 0; y < src.height(); ++y) { |
| 258 | const uint32_t* srcRow = src.getAddr32(0, y); |
| 259 | uint32_t* dstRow = result.getAddr32(0, y); |
| 260 | for (int x = 0; x < src.width(); ++x) { |
| 261 | dstRow[x] = SkUnPreMultiply::PMColorToColor(srcRow[x]); |
| 262 | } |
| 263 | } |
| 264 | return result; |
| 265 | } |
| 266 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 267 | bool SkMatrixConvolutionImageFilter::onFilterImageDeprecated(Proxy* proxy, |
| 268 | const SkBitmap& source, |
| 269 | const Context& ctx, |
| 270 | SkBitmap* result, |
| 271 | SkIPoint* offset) const { |
senorblanco@chromium.org | 6840076 | 2013-05-24 15:04:07 +0000 | [diff] [blame] | 272 | SkBitmap src = source; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 273 | SkIPoint srcOffset = SkIPoint::Make(0, 0); |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 274 | if (!this->filterInputDeprecated(0, proxy, source, ctx, &src, &srcOffset)) { |
senorblanco@chromium.org | 6840076 | 2013-05-24 15:04:07 +0000 | [diff] [blame] | 275 | return false; |
| 276 | } |
| 277 | |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 278 | if (src.colorType() != kN32_SkColorType) { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 279 | return false; |
| 280 | } |
| 281 | |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 282 | SkIRect bounds; |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 283 | if (!this->applyCropRectDeprecated(this->mapContext(ctx), proxy, src, &srcOffset, |
| 284 | &bounds, &src)) { |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 285 | return false; |
| 286 | } |
| 287 | |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 288 | if (!fConvolveAlpha && !src.isOpaque()) { |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 289 | src = unpremultiplyBitmap(proxy, src); |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 290 | } |
| 291 | |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 292 | SkAutoLockPixels alp(src); |
| 293 | if (!src.getPixels()) { |
| 294 | return false; |
| 295 | } |
| 296 | |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 297 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height())); |
| 298 | if (!device) { |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 299 | return false; |
| 300 | } |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 301 | *result = device->accessBitmap(false); |
| 302 | SkAutoLockPixels alp_result(*result); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 303 | |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 304 | offset->fX = bounds.fLeft; |
| 305 | offset->fY = bounds.fTop; |
| 306 | bounds.offset(-srcOffset); |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 307 | SkIRect interior = SkIRect::MakeXYWH(bounds.left() + fKernelOffset.fX, |
| 308 | bounds.top() + fKernelOffset.fY, |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 309 | bounds.width() - fKernelSize.fWidth + 1, |
| 310 | bounds.height() - fKernelSize.fHeight + 1); |
| 311 | SkIRect top = SkIRect::MakeLTRB(bounds.left(), bounds.top(), bounds.right(), interior.top()); |
| 312 | SkIRect bottom = SkIRect::MakeLTRB(bounds.left(), interior.bottom(), |
| 313 | bounds.right(), bounds.bottom()); |
| 314 | SkIRect left = SkIRect::MakeLTRB(bounds.left(), interior.top(), |
| 315 | interior.left(), interior.bottom()); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 316 | SkIRect right = SkIRect::MakeLTRB(interior.right(), interior.top(), |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 317 | bounds.right(), interior.bottom()); |
| 318 | filterBorderPixels(src, result, top, bounds); |
| 319 | filterBorderPixels(src, result, left, bounds); |
| 320 | filterInteriorPixels(src, result, interior, bounds); |
| 321 | filterBorderPixels(src, result, right, bounds); |
| 322 | filterBorderPixels(src, result, bottom, bounds); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 323 | return true; |
| 324 | } |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 325 | |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 326 | void SkMatrixConvolutionImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm, |
| 327 | SkIRect* dst, MapDirection direction) const { |
| 328 | *dst = src; |
| 329 | int w = fKernelSize.width() - 1, h = fKernelSize.height() - 1; |
| 330 | dst->fRight += w; |
| 331 | dst->fBottom += h; |
| 332 | if (kReverse_MapDirection == direction) { |
| 333 | dst->offset(-fKernelOffset); |
| 334 | } else { |
| 335 | dst->offset(fKernelOffset - SkIPoint::Make(w, h)); |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 336 | } |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 337 | } |
| 338 | |
senorblanco | a544eda | 2015-12-07 07:48:34 -0800 | [diff] [blame] | 339 | bool SkMatrixConvolutionImageFilter::canComputeFastBounds() const { |
| 340 | // Because the kernel is applied in device-space, we have no idea what |
| 341 | // pixels it will affect in object-space. |
| 342 | return false; |
| 343 | } |
| 344 | |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 345 | #if SK_SUPPORT_GPU |
| 346 | |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 347 | static GrTextureDomain::Mode convert_tilemodes( |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 348 | SkMatrixConvolutionImageFilter::TileMode tileMode) { |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 349 | switch (tileMode) { |
| 350 | case SkMatrixConvolutionImageFilter::kClamp_TileMode: |
| 351 | return GrTextureDomain::kClamp_Mode; |
| 352 | case SkMatrixConvolutionImageFilter::kRepeat_TileMode: |
| 353 | return GrTextureDomain::kRepeat_Mode; |
| 354 | case SkMatrixConvolutionImageFilter::kClampToBlack_TileMode: |
| 355 | return GrTextureDomain::kDecal_Mode; |
| 356 | default: |
| 357 | SkASSERT(false); |
| 358 | } |
| 359 | return GrTextureDomain::kIgnore_Mode; |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 360 | } |
| 361 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 362 | bool SkMatrixConvolutionImageFilter::asFragmentProcessor(GrFragmentProcessor** fp, |
| 363 | GrTexture* texture, |
| 364 | const SkMatrix&, |
| 365 | const SkIRect& bounds) const { |
| 366 | if (!fp) { |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 367 | return fKernelSize.width() * fKernelSize.height() <= MAX_KERNEL_SIZE; |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 368 | } |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 369 | SkASSERT(fKernelSize.width() * fKernelSize.height() <= MAX_KERNEL_SIZE); |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 370 | *fp = GrMatrixConvolutionEffect::Create(texture, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 371 | bounds, |
| 372 | fKernelSize, |
| 373 | fKernel, |
| 374 | fGain, |
| 375 | fBias, |
| 376 | fKernelOffset, |
| 377 | convert_tilemodes(fTileMode), |
| 378 | fConvolveAlpha); |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 379 | return true; |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 380 | } |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 381 | #endif |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 382 | |
| 383 | #ifndef SK_IGNORE_TO_STRING |
| 384 | void SkMatrixConvolutionImageFilter::toString(SkString* str) const { |
| 385 | str->appendf("SkMatrixConvolutionImageFilter: ("); |
| 386 | str->appendf("size: (%d,%d) kernel: (", fKernelSize.width(), fKernelSize.height()); |
| 387 | for (int y = 0; y < fKernelSize.height(); y++) { |
| 388 | for (int x = 0; x < fKernelSize.width(); x++) { |
| 389 | str->appendf("%f ", fKernel[y * fKernelSize.width() + x]); |
| 390 | } |
| 391 | } |
| 392 | str->appendf(")"); |
| 393 | str->appendf("gain: %f bias: %f ", fGain, fBias); |
| 394 | str->appendf("offset: (%d, %d) ", fKernelOffset.fX, fKernelOffset.fY); |
| 395 | str->appendf("convolveAlpha: %s", fConvolveAlpha ? "true" : "false"); |
| 396 | str->append(")"); |
| 397 | } |
| 398 | #endif |