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" |
| 11 | #include "SkFlattenableBuffers.h" |
| 12 | #include "SkRect.h" |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 13 | #include "SkUnPreMultiply.h" |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 14 | |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 15 | #if SK_SUPPORT_GPU |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 16 | #include "gl/GrGLEffect.h" |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 17 | #include "effects/GrSingleTextureEffect.h" |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 18 | #include "GrTBackendEffectFactory.h" |
bsalomon@google.com | 17fc651 | 2012-11-02 21:45:01 +0000 | [diff] [blame] | 19 | #include "GrTexture.h" |
| 20 | #include "SkMatrix.h" |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 21 | #endif |
| 22 | |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 23 | SkMatrixConvolutionImageFilter::SkMatrixConvolutionImageFilter( |
| 24 | const SkISize& kernelSize, |
| 25 | const SkScalar* kernel, |
| 26 | SkScalar gain, |
| 27 | SkScalar bias, |
| 28 | const SkIPoint& target, |
| 29 | TileMode tileMode, |
| 30 | bool convolveAlpha, |
| 31 | SkImageFilter* input, |
| 32 | const CropRect* cropRect) |
| 33 | : INHERITED(input, cropRect), |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 34 | fKernelSize(kernelSize), |
| 35 | fGain(gain), |
| 36 | fBias(bias), |
| 37 | fTarget(target), |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 38 | fTileMode(tileMode), |
| 39 | fConvolveAlpha(convolveAlpha) { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 40 | uint32_t size = fKernelSize.fWidth * fKernelSize.fHeight; |
| 41 | fKernel = SkNEW_ARRAY(SkScalar, size); |
| 42 | memcpy(fKernel, kernel, size * sizeof(SkScalar)); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 43 | SkASSERT(kernelSize.fWidth >= 1 && kernelSize.fHeight >= 1); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 44 | SkASSERT(target.fX >= 0 && target.fX < kernelSize.fWidth); |
| 45 | SkASSERT(target.fY >= 0 && target.fY < kernelSize.fHeight); |
| 46 | } |
| 47 | |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 48 | SkMatrixConvolutionImageFilter::SkMatrixConvolutionImageFilter(SkFlattenableReadBuffer& buffer) |
| 49 | : INHERITED(buffer) { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 50 | fKernelSize.fWidth = buffer.readInt(); |
| 51 | fKernelSize.fHeight = buffer.readInt(); |
| 52 | uint32_t size = fKernelSize.fWidth * fKernelSize.fHeight; |
| 53 | fKernel = SkNEW_ARRAY(SkScalar, size); |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 54 | SkDEBUGCODE(uint32_t readSize = )buffer.readScalarArray(fKernel); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 55 | SkASSERT(readSize == size); |
| 56 | fGain = buffer.readScalar(); |
| 57 | fBias = buffer.readScalar(); |
senorblanco@chromium.org | 528952b | 2012-11-29 22:39:10 +0000 | [diff] [blame] | 58 | fTarget.fX = buffer.readInt(); |
| 59 | fTarget.fY = buffer.readInt(); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 60 | fTileMode = (TileMode) buffer.readInt(); |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 61 | fConvolveAlpha = buffer.readBool(); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void SkMatrixConvolutionImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { |
| 65 | this->INHERITED::flatten(buffer); |
| 66 | buffer.writeInt(fKernelSize.fWidth); |
| 67 | buffer.writeInt(fKernelSize.fHeight); |
| 68 | buffer.writeScalarArray(fKernel, fKernelSize.fWidth * fKernelSize.fHeight); |
| 69 | buffer.writeScalar(fGain); |
| 70 | buffer.writeScalar(fBias); |
senorblanco@chromium.org | 528952b | 2012-11-29 22:39:10 +0000 | [diff] [blame] | 71 | buffer.writeInt(fTarget.fX); |
| 72 | buffer.writeInt(fTarget.fY); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 73 | buffer.writeInt((int) fTileMode); |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 74 | buffer.writeBool(fConvolveAlpha); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | SkMatrixConvolutionImageFilter::~SkMatrixConvolutionImageFilter() { |
| 78 | delete[] fKernel; |
| 79 | } |
| 80 | |
| 81 | class UncheckedPixelFetcher { |
| 82 | public: |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 83 | 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] | 84 | return *src.getAddr32(x, y); |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | class ClampPixelFetcher { |
| 89 | public: |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 90 | static inline SkPMColor fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) { |
| 91 | x = SkPin32(x, bounds.fLeft, bounds.fRight - 1); |
| 92 | y = SkPin32(y, bounds.fTop, bounds.fBottom - 1); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 93 | return *src.getAddr32(x, y); |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | class RepeatPixelFetcher { |
| 98 | public: |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 99 | static inline SkPMColor fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) { |
| 100 | x = (x - bounds.left()) % bounds.width() + bounds.left(); |
| 101 | y = (y - bounds.top()) % bounds.height() + bounds.top(); |
| 102 | if (x < bounds.left()) { |
| 103 | x += bounds.width(); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 104 | } |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 105 | if (y < bounds.top()) { |
| 106 | y += bounds.height(); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 107 | } |
| 108 | return *src.getAddr32(x, y); |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | class ClampToBlackPixelFetcher { |
| 113 | public: |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 114 | static inline SkPMColor fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) { |
| 115 | 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] | 116 | return 0; |
| 117 | } else { |
| 118 | return *src.getAddr32(x, y); |
| 119 | } |
| 120 | } |
| 121 | }; |
| 122 | |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 123 | template<class PixelFetcher, bool convolveAlpha> |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 124 | void SkMatrixConvolutionImageFilter::filterPixels(const SkBitmap& src, |
| 125 | SkBitmap* result, |
| 126 | const SkIRect& rect, |
| 127 | const SkIRect& bounds) { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 128 | for (int y = rect.fTop; y < rect.fBottom; ++y) { |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 129 | SkPMColor* dptr = result->getAddr32(rect.fLeft - bounds.fLeft, y - bounds.fTop); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 130 | for (int x = rect.fLeft; x < rect.fRight; ++x) { |
| 131 | SkScalar sumA = 0, sumR = 0, sumG = 0, sumB = 0; |
| 132 | for (int cy = 0; cy < fKernelSize.fHeight; cy++) { |
| 133 | for (int cx = 0; cx < fKernelSize.fWidth; cx++) { |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 134 | SkPMColor s = PixelFetcher::fetch(src, |
| 135 | x + cx - fTarget.fX, |
| 136 | y + cy - fTarget.fY, |
| 137 | bounds); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 138 | SkScalar k = fKernel[cy * fKernelSize.fWidth + cx]; |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 139 | if (convolveAlpha) { |
| 140 | sumA += SkScalarMul(SkIntToScalar(SkGetPackedA32(s)), k); |
| 141 | } |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 142 | sumR += SkScalarMul(SkIntToScalar(SkGetPackedR32(s)), k); |
| 143 | sumG += SkScalarMul(SkIntToScalar(SkGetPackedG32(s)), k); |
| 144 | sumB += SkScalarMul(SkIntToScalar(SkGetPackedB32(s)), k); |
| 145 | } |
| 146 | } |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 147 | int a = convolveAlpha |
| 148 | ? SkClampMax(SkScalarFloorToInt(SkScalarMul(sumA, fGain) + fBias), 255) |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 149 | : 255; |
senorblanco@chromium.org | cc9471c | 2012-09-20 17:59:49 +0000 | [diff] [blame] | 150 | int r = SkClampMax(SkScalarFloorToInt(SkScalarMul(sumR, fGain) + fBias), a); |
| 151 | int g = SkClampMax(SkScalarFloorToInt(SkScalarMul(sumG, fGain) + fBias), a); |
| 152 | int b = SkClampMax(SkScalarFloorToInt(SkScalarMul(sumB, fGain) + fBias), a); |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 153 | if (!convolveAlpha) { |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 154 | a = SkGetPackedA32(PixelFetcher::fetch(src, x, y, bounds)); |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 155 | *dptr++ = SkPreMultiplyARGB(a, r, g, b); |
| 156 | } else { |
| 157 | *dptr++ = SkPackARGB32(a, r, g, b); |
| 158 | } |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 163 | template<class PixelFetcher> |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 164 | void SkMatrixConvolutionImageFilter::filterPixels(const SkBitmap& src, |
| 165 | SkBitmap* result, |
| 166 | const SkIRect& rect, |
| 167 | const SkIRect& bounds) { |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 168 | if (fConvolveAlpha) { |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 169 | filterPixels<PixelFetcher, true>(src, result, rect, bounds); |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 170 | } else { |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 171 | filterPixels<PixelFetcher, false>(src, result, rect, bounds); |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 175 | void SkMatrixConvolutionImageFilter::filterInteriorPixels(const SkBitmap& src, |
| 176 | SkBitmap* result, |
| 177 | const SkIRect& rect, |
| 178 | const SkIRect& bounds) { |
| 179 | filterPixels<UncheckedPixelFetcher>(src, result, rect, bounds); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 180 | } |
| 181 | |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 182 | void SkMatrixConvolutionImageFilter::filterBorderPixels(const SkBitmap& src, |
| 183 | SkBitmap* result, |
| 184 | const SkIRect& rect, |
| 185 | const SkIRect& bounds) { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 186 | switch (fTileMode) { |
| 187 | case kClamp_TileMode: |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 188 | filterPixels<ClampPixelFetcher>(src, result, rect, bounds); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 189 | break; |
| 190 | case kRepeat_TileMode: |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 191 | filterPixels<RepeatPixelFetcher>(src, result, rect, bounds); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 192 | break; |
| 193 | case kClampToBlack_TileMode: |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 194 | filterPixels<ClampToBlackPixelFetcher>(src, result, rect, bounds); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 195 | break; |
| 196 | } |
| 197 | } |
| 198 | |
senorblanco@chromium.org | 377c14a | 2013-02-04 22:57:21 +0000 | [diff] [blame] | 199 | // FIXME: This should be refactored to SkImageFilterUtils for |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 200 | // use by other filters. For now, we assume the input is always |
| 201 | // premultiplied and unpremultiply it |
| 202 | static SkBitmap unpremultiplyBitmap(const SkBitmap& src) |
| 203 | { |
| 204 | SkAutoLockPixels alp(src); |
| 205 | if (!src.getPixels()) { |
| 206 | return SkBitmap(); |
| 207 | } |
| 208 | SkBitmap result; |
| 209 | result.setConfig(src.config(), src.width(), src.height()); |
| 210 | result.allocPixels(); |
| 211 | if (!result.getPixels()) { |
| 212 | return SkBitmap(); |
| 213 | } |
| 214 | for (int y = 0; y < src.height(); ++y) { |
| 215 | const uint32_t* srcRow = src.getAddr32(0, y); |
| 216 | uint32_t* dstRow = result.getAddr32(0, y); |
| 217 | for (int x = 0; x < src.width(); ++x) { |
| 218 | dstRow[x] = SkUnPreMultiply::PMColorToColor(srcRow[x]); |
| 219 | } |
| 220 | } |
| 221 | return result; |
| 222 | } |
| 223 | |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 224 | bool SkMatrixConvolutionImageFilter::onFilterImage(Proxy* proxy, |
| 225 | const SkBitmap& source, |
| 226 | const SkMatrix& matrix, |
| 227 | SkBitmap* result, |
| 228 | SkIPoint* loc) { |
senorblanco@chromium.org | 6840076 | 2013-05-24 15:04:07 +0000 | [diff] [blame] | 229 | SkBitmap src = source; |
| 230 | if (getInput(0) && !getInput(0)->filterImage(proxy, source, matrix, &src, loc)) { |
| 231 | return false; |
| 232 | } |
| 233 | |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 234 | if (src.config() != SkBitmap::kARGB_8888_Config) { |
| 235 | return false; |
| 236 | } |
| 237 | |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 238 | SkIRect bounds; |
| 239 | src.getBounds(&bounds); |
| 240 | if (!this->applyCropRect(&bounds, matrix)) { |
| 241 | return false; |
| 242 | } |
| 243 | |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 244 | if (!fConvolveAlpha && !src.isOpaque()) { |
| 245 | src = unpremultiplyBitmap(src); |
| 246 | } |
| 247 | |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 248 | SkAutoLockPixels alp(src); |
| 249 | if (!src.getPixels()) { |
| 250 | return false; |
| 251 | } |
| 252 | |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 253 | result->setConfig(src.config(), bounds.width(), bounds.height()); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 254 | result->allocPixels(); |
| 255 | |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 256 | SkIRect interior = SkIRect::MakeXYWH(bounds.left() + fTarget.fX, |
| 257 | bounds.top() + fTarget.fY, |
| 258 | bounds.width() - fKernelSize.fWidth + 1, |
| 259 | bounds.height() - fKernelSize.fHeight + 1); |
| 260 | SkIRect top = SkIRect::MakeLTRB(bounds.left(), bounds.top(), bounds.right(), interior.top()); |
| 261 | SkIRect bottom = SkIRect::MakeLTRB(bounds.left(), interior.bottom(), |
| 262 | bounds.right(), bounds.bottom()); |
| 263 | SkIRect left = SkIRect::MakeLTRB(bounds.left(), interior.top(), |
| 264 | interior.left(), interior.bottom()); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 265 | SkIRect right = SkIRect::MakeLTRB(interior.right(), interior.top(), |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 266 | bounds.right(), interior.bottom()); |
| 267 | filterBorderPixels(src, result, top, bounds); |
| 268 | filterBorderPixels(src, result, left, bounds); |
| 269 | filterInteriorPixels(src, result, interior, bounds); |
| 270 | filterBorderPixels(src, result, right, bounds); |
| 271 | filterBorderPixels(src, result, bottom, bounds); |
| 272 | loc->fX += bounds.fLeft; |
| 273 | loc->fY += bounds.fTop; |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 274 | return true; |
| 275 | } |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 276 | |
| 277 | #if SK_SUPPORT_GPU |
| 278 | |
| 279 | /////////////////////////////////////////////////////////////////////////////// |
| 280 | |
| 281 | class GrGLMatrixConvolutionEffect; |
| 282 | |
| 283 | class GrMatrixConvolutionEffect : public GrSingleTextureEffect { |
| 284 | public: |
| 285 | typedef SkMatrixConvolutionImageFilter::TileMode TileMode; |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 286 | static GrEffectRef* Create(GrTexture* texture, |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 287 | const SkIRect& bounds, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 288 | const SkISize& kernelSize, |
| 289 | const SkScalar* kernel, |
| 290 | SkScalar gain, |
| 291 | SkScalar bias, |
| 292 | const SkIPoint& target, |
| 293 | TileMode tileMode, |
| 294 | bool convolveAlpha) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 295 | AutoEffectUnref effect(SkNEW_ARGS(GrMatrixConvolutionEffect, (texture, |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 296 | bounds, |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 297 | kernelSize, |
| 298 | kernel, |
| 299 | gain, |
| 300 | bias, |
| 301 | target, |
| 302 | tileMode, |
| 303 | convolveAlpha))); |
bsalomon@google.com | a1ebbe4 | 2013-01-16 15:51:47 +0000 | [diff] [blame] | 304 | return CreateEffectRef(effect); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 305 | } |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 306 | virtual ~GrMatrixConvolutionEffect(); |
| 307 | |
bsalomon@google.com | 0263fcd | 2013-01-11 22:24:04 +0000 | [diff] [blame] | 308 | virtual void getConstantColorComponents(GrColor* color, |
| 309 | uint32_t* validFlags) const SK_OVERRIDE { |
| 310 | // TODO: Try to do better? |
| 311 | *validFlags = 0; |
| 312 | } |
| 313 | |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 314 | static const char* Name() { return "MatrixConvolution"; } |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 315 | const SkIRect& bounds() const { return fBounds; } |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 316 | const SkISize& kernelSize() const { return fKernelSize; } |
| 317 | const float* target() const { return fTarget; } |
| 318 | const float* kernel() const { return fKernel; } |
| 319 | float gain() const { return fGain; } |
| 320 | float bias() const { return fBias; } |
| 321 | TileMode tileMode() const { return fTileMode; } |
| 322 | bool convolveAlpha() const { return fConvolveAlpha; } |
| 323 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 324 | typedef GrGLMatrixConvolutionEffect GLEffect; |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 325 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 326 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 327 | |
| 328 | private: |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 329 | GrMatrixConvolutionEffect(GrTexture*, |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 330 | const SkIRect& bounds, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 331 | const SkISize& kernelSize, |
| 332 | const SkScalar* kernel, |
| 333 | SkScalar gain, |
| 334 | SkScalar bias, |
| 335 | const SkIPoint& target, |
| 336 | TileMode tileMode, |
| 337 | bool convolveAlpha); |
| 338 | |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 339 | virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 340 | |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 341 | SkIRect fBounds; |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 342 | SkISize fKernelSize; |
| 343 | float *fKernel; |
| 344 | float fGain; |
| 345 | float fBias; |
| 346 | float fTarget[2]; |
| 347 | TileMode fTileMode; |
| 348 | bool fConvolveAlpha; |
| 349 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 350 | GR_DECLARE_EFFECT_TEST; |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 351 | |
| 352 | typedef GrSingleTextureEffect INHERITED; |
| 353 | }; |
| 354 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 355 | class GrGLMatrixConvolutionEffect : public GrGLEffect { |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 356 | public: |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 357 | GrGLMatrixConvolutionEffect(const GrBackendEffectFactory& factory, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 358 | const GrDrawEffect& effect); |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 359 | virtual void emitCode(GrGLShaderBuilder*, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 360 | const GrDrawEffect&, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 361 | EffectKey, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 362 | const char* outputColor, |
| 363 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 364 | const TransformedCoordsArray&, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 365 | const TextureSamplerArray&) SK_OVERRIDE; |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 366 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 367 | static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 368 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 369 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 370 | |
| 371 | private: |
| 372 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
| 373 | typedef SkMatrixConvolutionImageFilter::TileMode TileMode; |
bsalomon@google.com | 17fc651 | 2012-11-02 21:45:01 +0000 | [diff] [blame] | 374 | SkISize fKernelSize; |
| 375 | TileMode fTileMode; |
| 376 | bool fConvolveAlpha; |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 377 | |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 378 | UniformHandle fBoundsUni; |
bsalomon@google.com | 17fc651 | 2012-11-02 21:45:01 +0000 | [diff] [blame] | 379 | UniformHandle fKernelUni; |
| 380 | UniformHandle fImageIncrementUni; |
| 381 | UniformHandle fTargetUni; |
| 382 | UniformHandle fGainUni; |
| 383 | UniformHandle fBiasUni; |
| 384 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 385 | typedef GrGLEffect INHERITED; |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 386 | }; |
| 387 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 388 | GrGLMatrixConvolutionEffect::GrGLMatrixConvolutionEffect(const GrBackendEffectFactory& factory, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 389 | const GrDrawEffect& drawEffect) |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 390 | : INHERITED(factory) { |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 391 | const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvolutionEffect>(); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 392 | fKernelSize = m.kernelSize(); |
| 393 | fTileMode = m.tileMode(); |
| 394 | fConvolveAlpha = m.convolveAlpha(); |
| 395 | } |
| 396 | |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 397 | static void appendTextureLookup(GrGLShaderBuilder* builder, |
| 398 | const GrGLShaderBuilder::TextureSampler& sampler, |
| 399 | const char* coord, |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 400 | const char* bounds, |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 401 | SkMatrixConvolutionImageFilter::TileMode tileMode) { |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 402 | SkString clampedCoord; |
| 403 | switch (tileMode) { |
| 404 | case SkMatrixConvolutionImageFilter::kClamp_TileMode: |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 405 | clampedCoord.printf("clamp(%s, %s.xy, %s.zw)", coord, bounds, bounds); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 406 | coord = clampedCoord.c_str(); |
| 407 | break; |
| 408 | case SkMatrixConvolutionImageFilter::kRepeat_TileMode: |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 409 | clampedCoord.printf("mod(%s - %s.xy, %s.zw - %s.xy) + %s.xy", coord, bounds, bounds, bounds, bounds); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 410 | coord = clampedCoord.c_str(); |
| 411 | break; |
| 412 | case SkMatrixConvolutionImageFilter::kClampToBlack_TileMode: |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 413 | builder->fsCodeAppendf("clamp(%s, %s.xy, %s.zw) != %s ? vec4(0, 0, 0, 0) : ", coord, bounds, bounds, coord); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 414 | break; |
| 415 | } |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 416 | builder->fsAppendTextureLookup(sampler, coord); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 417 | } |
| 418 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 419 | void GrGLMatrixConvolutionEffect::emitCode(GrGLShaderBuilder* builder, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 420 | const GrDrawEffect&, |
bsalomon@google.com | 17fc651 | 2012-11-02 21:45:01 +0000 | [diff] [blame] | 421 | EffectKey key, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 422 | const char* outputColor, |
| 423 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 424 | const TransformedCoordsArray& coords, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 425 | const TextureSamplerArray& samplers) { |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 426 | SkString coords2D = builder->ensureFSCoords2D(coords, 0); |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 427 | fBoundsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 428 | kVec4f_GrSLType, "Bounds"); |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 429 | fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 430 | kVec2f_GrSLType, "ImageIncrement"); |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 431 | fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_Visibility, |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 432 | kFloat_GrSLType, |
| 433 | "Kernel", |
| 434 | fKernelSize.width() * fKernelSize.height()); |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 435 | fTargetUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 436 | kVec2f_GrSLType, "Target"); |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 437 | fGainUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 438 | kFloat_GrSLType, "Gain"); |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 439 | fBiasUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 440 | kFloat_GrSLType, "Bias"); |
| 441 | |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 442 | const char* bounds = builder->getUniformCStr(fBoundsUni); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 443 | const char* target = builder->getUniformCStr(fTargetUni); |
| 444 | const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
| 445 | const char* kernel = builder->getUniformCStr(fKernelUni); |
| 446 | const char* gain = builder->getUniformCStr(fGainUni); |
| 447 | const char* bias = builder->getUniformCStr(fBiasUni); |
| 448 | int kWidth = fKernelSize.width(); |
| 449 | int kHeight = fKernelSize.height(); |
| 450 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 451 | builder->fsCodeAppend("\t\tvec4 sum = vec4(0, 0, 0, 0);\n"); |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 452 | builder->fsCodeAppendf("\t\tvec2 coord = %s - %s * %s;\n", coords2D.c_str(), target, imgInc); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 453 | builder->fsCodeAppendf("\t\tfor (int y = 0; y < %d; y++) {\n", kHeight); |
| 454 | builder->fsCodeAppendf("\t\t\tfor (int x = 0; x < %d; x++) {\n", kWidth); |
| 455 | builder->fsCodeAppendf("\t\t\t\tfloat k = %s[y * %d + x];\n", kernel, kWidth); |
| 456 | builder->fsCodeAppendf("\t\t\t\tvec2 coord2 = coord + vec2(x, y) * %s;\n", imgInc); |
| 457 | builder->fsCodeAppend("\t\t\t\tvec4 c = "); |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 458 | appendTextureLookup(builder, samplers[0], "coord2", bounds, fTileMode); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 459 | builder->fsCodeAppend(";\n"); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 460 | if (!fConvolveAlpha) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 461 | builder->fsCodeAppend("\t\t\t\tc.rgb /= c.a;\n"); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 462 | } |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 463 | builder->fsCodeAppend("\t\t\t\tsum += c * k;\n"); |
| 464 | builder->fsCodeAppend("\t\t\t}\n"); |
| 465 | builder->fsCodeAppend("\t\t}\n"); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 466 | if (fConvolveAlpha) { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 467 | builder->fsCodeAppendf("\t\t%s = sum * %s + %s;\n", outputColor, gain, bias); |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 468 | builder->fsCodeAppendf("\t\t%s.rgb = clamp(%s.rgb, 0.0, %s.a);\n", |
| 469 | outputColor, outputColor, outputColor); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 470 | } else { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 471 | builder->fsCodeAppend("\t\tvec4 c = "); |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 472 | appendTextureLookup(builder, samplers[0], coords2D.c_str(), bounds, fTileMode); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 473 | builder->fsCodeAppend(";\n"); |
| 474 | builder->fsCodeAppendf("\t\t%s.a = c.a;\n", outputColor); |
| 475 | builder->fsCodeAppendf("\t\t%s.rgb = sum.rgb * %s + %s;\n", outputColor, gain, bias); |
| 476 | builder->fsCodeAppendf("\t\t%s.rgb *= %s.a;\n", outputColor, outputColor); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
| 480 | namespace { |
| 481 | |
| 482 | int encodeXY(int x, int y) { |
| 483 | SkASSERT(x >= 1 && y >= 1 && x * y <= 32); |
| 484 | if (y < x) |
| 485 | return 0x40 | encodeXY(y, x); |
skia.committer@gmail.com | dda28c9 | 2012-10-05 02:01:40 +0000 | [diff] [blame] | 486 | else |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 487 | return (0x40 >> x) | (y - x); |
| 488 | } |
| 489 | |
| 490 | }; |
| 491 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 492 | GrGLEffect::EffectKey GrGLMatrixConvolutionEffect::GenKey(const GrDrawEffect& drawEffect, |
| 493 | const GrGLCaps&) { |
| 494 | const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvolutionEffect>(); |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 495 | EffectKey key = encodeXY(m.kernelSize().width(), m.kernelSize().height()); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 496 | key |= m.tileMode() << 7; |
| 497 | key |= m.convolveAlpha() ? 1 << 9 : 0; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 498 | return key; |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | void GrGLMatrixConvolutionEffect::setData(const GrGLUniformManager& uman, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 502 | const GrDrawEffect& drawEffect) { |
| 503 | const GrMatrixConvolutionEffect& conv = drawEffect.castEffect<GrMatrixConvolutionEffect>(); |
| 504 | GrTexture& texture = *conv.texture(0); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 505 | // the code we generated was for a specific kernel size |
commit-bot@chromium.org | 96ae688 | 2013-08-14 12:09:00 +0000 | [diff] [blame] | 506 | SkASSERT(conv.kernelSize() == fKernelSize); |
| 507 | SkASSERT(conv.tileMode() == fTileMode); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 508 | float imageIncrement[2]; |
senorblanco@chromium.org | ef5dbe1 | 2013-01-28 16:42:38 +0000 | [diff] [blame] | 509 | float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 510 | imageIncrement[0] = 1.0f / texture.width(); |
senorblanco@chromium.org | 027c102 | 2013-01-14 21:53:45 +0000 | [diff] [blame] | 511 | imageIncrement[1] = ySign / texture.height(); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 512 | uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 513 | uman.set2fv(fTargetUni, 0, 1, conv.target()); |
| 514 | uman.set1fv(fKernelUni, 0, fKernelSize.width() * fKernelSize.height(), conv.kernel()); |
| 515 | uman.set1f(fGainUni, conv.gain()); |
| 516 | uman.set1f(fBiasUni, conv.bias()); |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 517 | const SkIRect& bounds = conv.bounds(); |
| 518 | uman.set4f(fBoundsUni, |
| 519 | (float) bounds.left() / texture.width(), |
| 520 | (float) bounds.top() / texture.height(), |
| 521 | (float) bounds.right() / texture.width(), |
| 522 | (float) bounds.bottom() / texture.height()); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrTexture* texture, |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 526 | const SkIRect& bounds, |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 527 | const SkISize& kernelSize, |
| 528 | const SkScalar* kernel, |
| 529 | SkScalar gain, |
| 530 | SkScalar bias, |
| 531 | const SkIPoint& target, |
| 532 | TileMode tileMode, |
| 533 | bool convolveAlpha) |
bsalomon@google.com | 17fc651 | 2012-11-02 21:45:01 +0000 | [diff] [blame] | 534 | : INHERITED(texture, MakeDivByTextureWHMatrix(texture)), |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 535 | fBounds(bounds), |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 536 | fKernelSize(kernelSize), |
| 537 | fGain(SkScalarToFloat(gain)), |
| 538 | fBias(SkScalarToFloat(bias) / 255.0f), |
| 539 | fTileMode(tileMode), |
| 540 | fConvolveAlpha(convolveAlpha) { |
| 541 | fKernel = new float[kernelSize.width() * kernelSize.height()]; |
| 542 | for (int i = 0; i < kernelSize.width() * kernelSize.height(); i++) { |
| 543 | fKernel[i] = SkScalarToFloat(kernel[i]); |
| 544 | } |
senorblanco@chromium.org | 528952b | 2012-11-29 22:39:10 +0000 | [diff] [blame] | 545 | fTarget[0] = static_cast<float>(target.x()); |
| 546 | fTarget[1] = static_cast<float>(target.y()); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | GrMatrixConvolutionEffect::~GrMatrixConvolutionEffect() { |
| 550 | delete[] fKernel; |
| 551 | } |
| 552 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 553 | const GrBackendEffectFactory& GrMatrixConvolutionEffect::getFactory() const { |
| 554 | return GrTBackendEffectFactory<GrMatrixConvolutionEffect>::getInstance(); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 555 | } |
| 556 | |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 557 | bool GrMatrixConvolutionEffect::onIsEqual(const GrEffect& sBase) const { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 558 | const GrMatrixConvolutionEffect& s = CastEffect<GrMatrixConvolutionEffect>(sBase); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 559 | return this->texture(0) == s.texture(0) && |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 560 | fKernelSize == s.kernelSize() && |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 561 | !memcmp(fKernel, s.kernel(), |
| 562 | fKernelSize.width() * fKernelSize.height() * sizeof(float)) && |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 563 | fGain == s.gain() && |
| 564 | fBias == s.bias() && |
| 565 | fTarget == s.target() && |
| 566 | fTileMode == s.tileMode() && |
| 567 | fConvolveAlpha == s.convolveAlpha(); |
| 568 | } |
| 569 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 570 | GR_DEFINE_EFFECT_TEST(GrMatrixConvolutionEffect); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 571 | |
skia.committer@gmail.com | dda28c9 | 2012-10-05 02:01:40 +0000 | [diff] [blame] | 572 | // A little bit less than the minimum # uniforms required by DX9SM2 (32). |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 573 | // Allows for a 5x5 kernel (or 25x1, for that matter). |
| 574 | #define MAX_KERNEL_SIZE 25 |
| 575 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 576 | GrEffectRef* GrMatrixConvolutionEffect::TestCreate(SkRandom* random, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 577 | GrContext* context, |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 578 | const GrDrawTargetCaps&, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 579 | GrTexture* textures[]) { |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 580 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 581 | GrEffectUnitTest::kAlphaTextureIdx; |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 582 | int width = random->nextRangeU(1, MAX_KERNEL_SIZE); |
| 583 | int height = random->nextRangeU(1, MAX_KERNEL_SIZE / width); |
| 584 | SkISize kernelSize = SkISize::Make(width, height); |
robertphillips@google.com | 35300c4 | 2013-03-21 17:38:49 +0000 | [diff] [blame] | 585 | SkAutoTDeleteArray<SkScalar> kernel(new SkScalar[width * height]); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 586 | for (int i = 0; i < width * height; i++) { |
robertphillips@google.com | 35300c4 | 2013-03-21 17:38:49 +0000 | [diff] [blame] | 587 | kernel.get()[i] = random->nextSScalar1(); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 588 | } |
| 589 | SkScalar gain = random->nextSScalar1(); |
| 590 | SkScalar bias = random->nextSScalar1(); |
| 591 | SkIPoint target = SkIPoint::Make(random->nextRangeU(0, kernelSize.width()), |
| 592 | random->nextRangeU(0, kernelSize.height())); |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 593 | SkIRect bounds = SkIRect::MakeXYWH(random->nextRangeU(0, textures[texIdx]->width()), |
| 594 | random->nextRangeU(0, textures[texIdx]->height()), |
| 595 | random->nextRangeU(0, textures[texIdx]->width()), |
| 596 | random->nextRangeU(0, textures[texIdx]->height())); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 597 | TileMode tileMode = static_cast<TileMode>(random->nextRangeU(0, 2)); |
| 598 | bool convolveAlpha = random->nextBool(); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 599 | return GrMatrixConvolutionEffect::Create(textures[texIdx], |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 600 | bounds, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 601 | kernelSize, |
robertphillips@google.com | 35300c4 | 2013-03-21 17:38:49 +0000 | [diff] [blame] | 602 | kernel.get(), |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 603 | gain, |
| 604 | bias, |
| 605 | target, |
| 606 | tileMode, |
| 607 | convolveAlpha); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 608 | } |
| 609 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 610 | bool SkMatrixConvolutionImageFilter::asNewEffect(GrEffectRef** effect, |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 611 | GrTexture* texture, |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 612 | const SkMatrix&, |
| 613 | const SkIRect& bounds |
| 614 | ) const { |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 615 | if (!effect) { |
| 616 | return fKernelSize.width() * fKernelSize.height() <= MAX_KERNEL_SIZE; |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 617 | } |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 618 | SkASSERT(fKernelSize.width() * fKernelSize.height() <= MAX_KERNEL_SIZE); |
| 619 | *effect = GrMatrixConvolutionEffect::Create(texture, |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame^] | 620 | bounds, |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 621 | fKernelSize, |
| 622 | fKernel, |
| 623 | fGain, |
| 624 | fBias, |
| 625 | fTarget, |
| 626 | fTileMode, |
| 627 | fConvolveAlpha); |
| 628 | return true; |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /////////////////////////////////////////////////////////////////////////////// |
| 632 | |
| 633 | #endif |