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