15faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org/*
25faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org * Copyright 2012 The Android Open Source Project
35faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org *
45faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org * Use of this source code is governed by a BSD-style license that can be
55faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org * found in the LICENSE file.
65faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org */
75faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
85faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org#include "SkMatrixConvolutionImageFilter.h"
95faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org#include "SkBitmap.h"
105faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org#include "SkColorPriv.h"
118b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.org#include "SkReadBuffer.h"
128b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.org#include "SkWriteBuffer.h"
135faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org#include "SkRect.h"
148640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org#include "SkUnPreMultiply.h"
155faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
163bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org#if SK_SUPPORT_GPU
17d698f77c13d97c61109b861eac4d25b14a5de935bsalomon@google.com#include "gl/GrGLEffect.h"
18dbe49f735484f8862e378b63d0a074a301093dd0bsalomon@google.com#include "effects/GrSingleTextureEffect.h"
192eaaefd7e6a58339b3f93333f1e9cc92252cc303bsalomon@google.com#include "GrTBackendEffectFactory.h"
2017fc651dbe2e0624f6c85fb6e081d28a87d5a08bbsalomon@google.com#include "GrTexture.h"
2117fc651dbe2e0624f6c85fb6e081d28a87d5a08bbsalomon@google.com#include "SkMatrix.h"
223bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org#endif
233bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
24c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.orgstatic bool tile_mode_is_valid(SkMatrixConvolutionImageFilter::TileMode tileMode) {
25c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org    switch (tileMode) {
26c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org    case SkMatrixConvolutionImageFilter::kClamp_TileMode:
27c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org    case SkMatrixConvolutionImageFilter::kRepeat_TileMode:
28c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org    case SkMatrixConvolutionImageFilter::kClampToBlack_TileMode:
29c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org        return true;
30c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org    default:
31c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org        break;
32c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org    }
33c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org    return false;
34c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org}
35c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org
367938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.orgSkMatrixConvolutionImageFilter::SkMatrixConvolutionImageFilter(
377938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    const SkISize& kernelSize,
387938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    const SkScalar* kernel,
397938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    SkScalar gain,
407938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    SkScalar bias,
41cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org    const SkIPoint& kernelOffset,
427938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    TileMode tileMode,
437938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    bool convolveAlpha,
447938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    SkImageFilter* input,
457938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    const CropRect* cropRect)
467938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org  : INHERITED(input, cropRect),
475faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    fKernelSize(kernelSize),
485faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    fGain(gain),
495faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    fBias(bias),
50cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org    fKernelOffset(kernelOffset),
518640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org    fTileMode(tileMode),
528640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org    fConvolveAlpha(convolveAlpha) {
535faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    uint32_t size = fKernelSize.fWidth * fKernelSize.fHeight;
545faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    fKernel = SkNEW_ARRAY(SkScalar, size);
555faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    memcpy(fKernel, kernel, size * sizeof(SkScalar));
563bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    SkASSERT(kernelSize.fWidth >= 1 && kernelSize.fHeight >= 1);
57cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org    SkASSERT(kernelOffset.fX >= 0 && kernelOffset.fX < kernelSize.fWidth);
58cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org    SkASSERT(kernelOffset.fY >= 0 && kernelOffset.fY < kernelSize.fHeight);
595faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org}
605faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
618b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.orgSkMatrixConvolutionImageFilter::SkMatrixConvolutionImageFilter(SkReadBuffer& buffer)
62ce33d60187718e7bb01944ee130c9f5d9fb335eccommit-bot@chromium.org    : INHERITED(1, buffer) {
63025128811219dc45fd99b6c4d1d14f833cf7a26ecommit-bot@chromium.org    // We need to be able to read at most SK_MaxS32 bytes, so divide that
64025128811219dc45fd99b6c4d1d14f833cf7a26ecommit-bot@chromium.org    // by the size of a scalar to know how many scalars we can read.
65025128811219dc45fd99b6c4d1d14f833cf7a26ecommit-bot@chromium.org    static const int32_t kMaxSize = SK_MaxS32 / sizeof(SkScalar);
665faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    fKernelSize.fWidth = buffer.readInt();
675faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    fKernelSize.fHeight = buffer.readInt();
68d594dbec0407343b7ac13af9c4580ec5933ab060commit-bot@chromium.org    if ((fKernelSize.fWidth >= 1) && (fKernelSize.fHeight >= 1) &&
69d594dbec0407343b7ac13af9c4580ec5933ab060commit-bot@chromium.org        // Make sure size won't be larger than a signed int,
70d594dbec0407343b7ac13af9c4580ec5933ab060commit-bot@chromium.org        // which would still be extremely large for a kernel,
71d594dbec0407343b7ac13af9c4580ec5933ab060commit-bot@chromium.org        // but we don't impose a hard limit for kernel size
72025128811219dc45fd99b6c4d1d14f833cf7a26ecommit-bot@chromium.org        (kMaxSize / fKernelSize.fWidth >= fKernelSize.fHeight)) {
73025128811219dc45fd99b6c4d1d14f833cf7a26ecommit-bot@chromium.org        size_t size = fKernelSize.fWidth * fKernelSize.fHeight;
74d594dbec0407343b7ac13af9c4580ec5933ab060commit-bot@chromium.org        fKernel = SkNEW_ARRAY(SkScalar, size);
75025128811219dc45fd99b6c4d1d14f833cf7a26ecommit-bot@chromium.org        SkDEBUGCODE(bool success =) buffer.readScalarArray(fKernel, size);
76025128811219dc45fd99b6c4d1d14f833cf7a26ecommit-bot@chromium.org        SkASSERT(success);
77d594dbec0407343b7ac13af9c4580ec5933ab060commit-bot@chromium.org    } else {
78d594dbec0407343b7ac13af9c4580ec5933ab060commit-bot@chromium.org        fKernel = 0;
79d594dbec0407343b7ac13af9c4580ec5933ab060commit-bot@chromium.org    }
805faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    fGain = buffer.readScalar();
815faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    fBias = buffer.readScalar();
82cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org    fKernelOffset.fX = buffer.readInt();
83cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org    fKernelOffset.fY = buffer.readInt();
845faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    fTileMode = (TileMode) buffer.readInt();
858640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org    fConvolveAlpha = buffer.readBool();
86d594dbec0407343b7ac13af9c4580ec5933ab060commit-bot@chromium.org    buffer.validate((fKernel != 0) &&
87d594dbec0407343b7ac13af9c4580ec5933ab060commit-bot@chromium.org                    SkScalarIsFinite(fGain) &&
88c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org                    SkScalarIsFinite(fBias) &&
8956d135c03e1d38f0b9e17d79a3cbf8db2a915086sugoi                    tile_mode_is_valid(fTileMode) &&
9056d135c03e1d38f0b9e17d79a3cbf8db2a915086sugoi                    (fKernelOffset.fX >= 0) && (fKernelOffset.fX < fKernelSize.fWidth) &&
9156d135c03e1d38f0b9e17d79a3cbf8db2a915086sugoi                    (fKernelOffset.fY >= 0) && (fKernelOffset.fY < fKernelSize.fHeight));
925faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org}
935faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
948b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.orgvoid SkMatrixConvolutionImageFilter::flatten(SkWriteBuffer& buffer) const {
955faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    this->INHERITED::flatten(buffer);
965faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    buffer.writeInt(fKernelSize.fWidth);
975faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    buffer.writeInt(fKernelSize.fHeight);
985faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    buffer.writeScalarArray(fKernel, fKernelSize.fWidth * fKernelSize.fHeight);
995faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    buffer.writeScalar(fGain);
1005faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    buffer.writeScalar(fBias);
101cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org    buffer.writeInt(fKernelOffset.fX);
102cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org    buffer.writeInt(fKernelOffset.fY);
1035faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    buffer.writeInt((int) fTileMode);
1048640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org    buffer.writeBool(fConvolveAlpha);
1055faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org}
1065faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
1075faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.orgSkMatrixConvolutionImageFilter::~SkMatrixConvolutionImageFilter() {
1085faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    delete[] fKernel;
1095faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org}
1105faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
1115faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.orgclass UncheckedPixelFetcher {
1125faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.orgpublic:
1137938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    static inline SkPMColor fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) {
1145faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org        return *src.getAddr32(x, y);
1155faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    }
1165faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org};
1175faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
1185faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.orgclass ClampPixelFetcher {
1195faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.orgpublic:
1207938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    static inline SkPMColor fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) {
1217938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org        x = SkPin32(x, bounds.fLeft, bounds.fRight - 1);
1227938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org        y = SkPin32(y, bounds.fTop, bounds.fBottom - 1);
1235faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org        return *src.getAddr32(x, y);
1245faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    }
1255faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org};
1265faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
1275faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.orgclass RepeatPixelFetcher {
1285faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.orgpublic:
1297938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    static inline SkPMColor fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) {
1307938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org        x = (x - bounds.left()) % bounds.width() + bounds.left();
1317938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org        y = (y - bounds.top()) % bounds.height() + bounds.top();
1327938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org        if (x < bounds.left()) {
1337938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org            x += bounds.width();
1345faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org        }
1357938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org        if (y < bounds.top()) {
1367938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org            y += bounds.height();
1375faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org        }
1385faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org        return *src.getAddr32(x, y);
1395faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    }
1405faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org};
1415faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
1425faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.orgclass ClampToBlackPixelFetcher {
1435faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.orgpublic:
1447938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    static inline SkPMColor fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) {
1457938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org        if (x < bounds.fLeft || x >= bounds.fRight || y < bounds.fTop || y >= bounds.fBottom) {
1465faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org            return 0;
1475faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org        } else {
1485faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org            return *src.getAddr32(x, y);
1495faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org        }
1505faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    }
1515faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org};
1525faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
1538640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.orgtemplate<class PixelFetcher, bool convolveAlpha>
1547938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.orgvoid SkMatrixConvolutionImageFilter::filterPixels(const SkBitmap& src,
1557938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                                                  SkBitmap* result,
1568c7372bbe8949f2864bd3d9df00d85c5669a74b1senorblanco@chromium.org                                                  const SkIRect& r,
157ae761f7545d8ebf181d220169afac2056b057b8ccommit-bot@chromium.org                                                  const SkIRect& bounds) const {
1588c7372bbe8949f2864bd3d9df00d85c5669a74b1senorblanco@chromium.org    SkIRect rect(r);
1598c7372bbe8949f2864bd3d9df00d85c5669a74b1senorblanco@chromium.org    if (!rect.intersect(bounds)) {
1609195743aac79a4fa82059ab614b9795f215475f7senorblanco@chromium.org        return;
1619195743aac79a4fa82059ab614b9795f215475f7senorblanco@chromium.org    }
1625faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    for (int y = rect.fTop; y < rect.fBottom; ++y) {
1637938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org        SkPMColor* dptr = result->getAddr32(rect.fLeft - bounds.fLeft, y - bounds.fTop);
1645faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org        for (int x = rect.fLeft; x < rect.fRight; ++x) {
1655faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org            SkScalar sumA = 0, sumR = 0, sumG = 0, sumB = 0;
1665faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org            for (int cy = 0; cy < fKernelSize.fHeight; cy++) {
1675faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org                for (int cx = 0; cx < fKernelSize.fWidth; cx++) {
1687938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                    SkPMColor s = PixelFetcher::fetch(src,
169cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                                                      x + cx - fKernelOffset.fX,
170cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                                                      y + cy - fKernelOffset.fY,
1717938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                                                      bounds);
1725faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org                    SkScalar k = fKernel[cy * fKernelSize.fWidth + cx];
1738640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org                    if (convolveAlpha) {
1748640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org                        sumA += SkScalarMul(SkIntToScalar(SkGetPackedA32(s)), k);
1758640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org                    }
1765faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org                    sumR += SkScalarMul(SkIntToScalar(SkGetPackedR32(s)), k);
1775faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org                    sumG += SkScalarMul(SkIntToScalar(SkGetPackedG32(s)), k);
1785faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org                    sumB += SkScalarMul(SkIntToScalar(SkGetPackedB32(s)), k);
1795faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org                }
1805faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org            }
1818640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org            int a = convolveAlpha
1828640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org                  ? SkClampMax(SkScalarFloorToInt(SkScalarMul(sumA, fGain) + fBias), 255)
1833bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org                  : 255;
184cc9471c36d3967270f7eb26f8b53ce0f17bc9fbbsenorblanco@chromium.org            int r = SkClampMax(SkScalarFloorToInt(SkScalarMul(sumR, fGain) + fBias), a);
185cc9471c36d3967270f7eb26f8b53ce0f17bc9fbbsenorblanco@chromium.org            int g = SkClampMax(SkScalarFloorToInt(SkScalarMul(sumG, fGain) + fBias), a);
186cc9471c36d3967270f7eb26f8b53ce0f17bc9fbbsenorblanco@chromium.org            int b = SkClampMax(SkScalarFloorToInt(SkScalarMul(sumB, fGain) + fBias), a);
1878640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org            if (!convolveAlpha) {
1887938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                a = SkGetPackedA32(PixelFetcher::fetch(src, x, y, bounds));
1898640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org                *dptr++ = SkPreMultiplyARGB(a, r, g, b);
1908640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org            } else {
1918640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org                *dptr++ = SkPackARGB32(a, r, g, b);
1928640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org            }
1935faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org        }
1945faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    }
1955faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org}
1965faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
1978640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.orgtemplate<class PixelFetcher>
1987938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.orgvoid SkMatrixConvolutionImageFilter::filterPixels(const SkBitmap& src,
1997938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                                                  SkBitmap* result,
2007938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                                                  const SkIRect& rect,
201ae761f7545d8ebf181d220169afac2056b057b8ccommit-bot@chromium.org                                                  const SkIRect& bounds) const {
2028640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org    if (fConvolveAlpha) {
2037938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org        filterPixels<PixelFetcher, true>(src, result, rect, bounds);
2048640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org    } else {
2057938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org        filterPixels<PixelFetcher, false>(src, result, rect, bounds);
2068640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org    }
2078640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org}
2088640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org
2097938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.orgvoid SkMatrixConvolutionImageFilter::filterInteriorPixels(const SkBitmap& src,
2107938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                                                          SkBitmap* result,
2117938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                                                          const SkIRect& rect,
212ae761f7545d8ebf181d220169afac2056b057b8ccommit-bot@chromium.org                                                          const SkIRect& bounds) const {
2137938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    filterPixels<UncheckedPixelFetcher>(src, result, rect, bounds);
2145faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org}
2155faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
2167938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.orgvoid SkMatrixConvolutionImageFilter::filterBorderPixels(const SkBitmap& src,
2177938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                                                        SkBitmap* result,
2187938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                                                        const SkIRect& rect,
219ae761f7545d8ebf181d220169afac2056b057b8ccommit-bot@chromium.org                                                        const SkIRect& bounds) const {
2205faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    switch (fTileMode) {
2215faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org        case kClamp_TileMode:
2227938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org            filterPixels<ClampPixelFetcher>(src, result, rect, bounds);
2235faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org            break;
2245faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org        case kRepeat_TileMode:
2257938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org            filterPixels<RepeatPixelFetcher>(src, result, rect, bounds);
2265faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org            break;
2275faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org        case kClampToBlack_TileMode:
2287938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org            filterPixels<ClampToBlackPixelFetcher>(src, result, rect, bounds);
2295faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org            break;
2305faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    }
2315faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org}
2325faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
233377c14a1e648f4427bd11474fad8ac264d98aff2senorblanco@chromium.org// FIXME:  This should be refactored to SkImageFilterUtils for
2348640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org// use by other filters.  For now, we assume the input is always
2358640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org// premultiplied and unpremultiply it
2368640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.orgstatic SkBitmap unpremultiplyBitmap(const SkBitmap& src)
2378640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org{
2388640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org    SkAutoLockPixels alp(src);
2398640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org    if (!src.getPixels()) {
2408640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org        return SkBitmap();
2418640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org    }
2428640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org    SkBitmap result;
243c77392ed58ec78ab19fa0e3ff99fb8110854fba2reed    if (!result.allocPixels(src.info())) {
2448640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org        return SkBitmap();
2458640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org    }
2468640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org    for (int y = 0; y < src.height(); ++y) {
2478640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org        const uint32_t* srcRow = src.getAddr32(0, y);
2488640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org        uint32_t* dstRow = result.getAddr32(0, y);
2498640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org        for (int x = 0; x < src.width(); ++x) {
2508640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org            dstRow[x] = SkUnPreMultiply::PMColorToColor(srcRow[x]);
2518640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org        }
2528640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org    }
2538640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org    return result;
2548640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org}
2558640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org
2565faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.orgbool SkMatrixConvolutionImageFilter::onFilterImage(Proxy* proxy,
2575faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org                                                   const SkBitmap& source,
2584cb543d6057b692e1099e9f115155f0bf323a0c8senorblanco@chromium.org                                                   const Context& ctx,
2595faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org                                                   SkBitmap* result,
260ae761f7545d8ebf181d220169afac2056b057b8ccommit-bot@chromium.org                                                   SkIPoint* offset) const {
26168400767be5f72e4b9750ccc8bcf0078d42869a7senorblanco@chromium.org    SkBitmap src = source;
2626776b82d466fa93ccffd251fdf556fe058395444senorblanco@chromium.org    SkIPoint srcOffset = SkIPoint::Make(0, 0);
2634cb543d6057b692e1099e9f115155f0bf323a0c8senorblanco@chromium.org    if (getInput(0) && !getInput(0)->filterImage(proxy, source, ctx, &src, &srcOffset)) {
26468400767be5f72e4b9750ccc8bcf0078d42869a7senorblanco@chromium.org        return false;
26568400767be5f72e4b9750ccc8bcf0078d42869a7senorblanco@chromium.org    }
26668400767be5f72e4b9750ccc8bcf0078d42869a7senorblanco@chromium.org
26728fcae2ec77eb16a79e155f8d788b20457f1c951commit-bot@chromium.org    if (src.colorType() != kN32_SkColorType) {
2685faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org        return false;
2695faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    }
2705faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
2717938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    SkIRect bounds;
272118252962f89a80db661a0544f1bd61cbaab6321senorblanco@chromium.org    if (!this->applyCropRect(ctx, proxy, src, &srcOffset, &bounds, &src)) {
2737938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org        return false;
2747938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    }
2757938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org
2768640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org    if (!fConvolveAlpha && !src.isOpaque()) {
2778640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org        src = unpremultiplyBitmap(src);
2788640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org    }
2798640d5024d57da5508bdf7585849e3b1f1cb365bsenorblanco@chromium.org
2805faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    SkAutoLockPixels alp(src);
2815faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    if (!src.getPixels()) {
2825faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org        return false;
2835faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    }
2845faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
2856c22573edb234ad14df947278cfed010669a39a7reed    if (!result->allocPixels(src.info().makeWH(bounds.width(), bounds.height()))) {
286cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org        return false;
287cd3b15ca6364a04b0eeeb4f89c7daa8aefe854c8commit-bot@chromium.org    }
2885faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org
2896776b82d466fa93ccffd251fdf556fe058395444senorblanco@chromium.org    offset->fX = bounds.fLeft;
2906776b82d466fa93ccffd251fdf556fe058395444senorblanco@chromium.org    offset->fY = bounds.fTop;
2916776b82d466fa93ccffd251fdf556fe058395444senorblanco@chromium.org    bounds.offset(-srcOffset);
292cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org    SkIRect interior = SkIRect::MakeXYWH(bounds.left() + fKernelOffset.fX,
293cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                                         bounds.top() + fKernelOffset.fY,
2947938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                                         bounds.width() - fKernelSize.fWidth + 1,
2957938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                                         bounds.height() - fKernelSize.fHeight + 1);
2967938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    SkIRect top = SkIRect::MakeLTRB(bounds.left(), bounds.top(), bounds.right(), interior.top());
2977938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    SkIRect bottom = SkIRect::MakeLTRB(bounds.left(), interior.bottom(),
2987938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                                       bounds.right(), bounds.bottom());
2997938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    SkIRect left = SkIRect::MakeLTRB(bounds.left(), interior.top(),
3007938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                                     interior.left(), interior.bottom());
3015faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    SkIRect right = SkIRect::MakeLTRB(interior.right(), interior.top(),
3027938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                                      bounds.right(), interior.bottom());
3037938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    filterBorderPixels(src, result, top, bounds);
3047938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    filterBorderPixels(src, result, left, bounds);
3057938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    filterInteriorPixels(src, result, interior, bounds);
3067938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    filterBorderPixels(src, result, right, bounds);
3077938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    filterBorderPixels(src, result, bottom, bounds);
3085faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org    return true;
3095faa2dc266ec933b3961f985e5718236f1ecbe47senorblanco@chromium.org}
3103bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
3110a5c233e3b911232c0d6f9a88ded99ecf88b8a97senorblanco@chromium.orgbool SkMatrixConvolutionImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& ctm,
3120a5c233e3b911232c0d6f9a88ded99ecf88b8a97senorblanco@chromium.org                                                    SkIRect* dst) const {
3130a5c233e3b911232c0d6f9a88ded99ecf88b8a97senorblanco@chromium.org    SkIRect bounds = src;
3140a5c233e3b911232c0d6f9a88ded99ecf88b8a97senorblanco@chromium.org    bounds.fRight += fKernelSize.width() - 1;
3150a5c233e3b911232c0d6f9a88ded99ecf88b8a97senorblanco@chromium.org    bounds.fBottom += fKernelSize.height() - 1;
3160a5c233e3b911232c0d6f9a88ded99ecf88b8a97senorblanco@chromium.org    bounds.offset(-fKernelOffset);
3170a5c233e3b911232c0d6f9a88ded99ecf88b8a97senorblanco@chromium.org    if (getInput(0) && !getInput(0)->filterBounds(bounds, ctm, &bounds)) {
3180a5c233e3b911232c0d6f9a88ded99ecf88b8a97senorblanco@chromium.org        return false;
3190a5c233e3b911232c0d6f9a88ded99ecf88b8a97senorblanco@chromium.org    }
3200a5c233e3b911232c0d6f9a88ded99ecf88b8a97senorblanco@chromium.org    *dst = bounds;
3210a5c233e3b911232c0d6f9a88ded99ecf88b8a97senorblanco@chromium.org    return true;
3220a5c233e3b911232c0d6f9a88ded99ecf88b8a97senorblanco@chromium.org}
3230a5c233e3b911232c0d6f9a88ded99ecf88b8a97senorblanco@chromium.org
3243bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org#if SK_SUPPORT_GPU
3253bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
3263bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org///////////////////////////////////////////////////////////////////////////////
3273bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
3283bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.orgclass GrGLMatrixConvolutionEffect;
3293bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
3303bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.orgclass GrMatrixConvolutionEffect : public GrSingleTextureEffect {
3313bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.orgpublic:
3323bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    typedef SkMatrixConvolutionImageFilter::TileMode TileMode;
3330ac6af49975c54c2debf41e9200af416ecd2d973bsalomon@google.com    static GrEffectRef* Create(GrTexture* texture,
3347938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                               const SkIRect& bounds,
3350ac6af49975c54c2debf41e9200af416ecd2d973bsalomon@google.com                               const SkISize& kernelSize,
3360ac6af49975c54c2debf41e9200af416ecd2d973bsalomon@google.com                               const SkScalar* kernel,
3370ac6af49975c54c2debf41e9200af416ecd2d973bsalomon@google.com                               SkScalar gain,
3380ac6af49975c54c2debf41e9200af416ecd2d973bsalomon@google.com                               SkScalar bias,
339cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                               const SkIPoint& kernelOffset,
3400ac6af49975c54c2debf41e9200af416ecd2d973bsalomon@google.com                               TileMode tileMode,
3410ac6af49975c54c2debf41e9200af416ecd2d973bsalomon@google.com                               bool convolveAlpha) {
3426340a41108633ac1ce5941e5cd30538630c4c55bbsalomon@google.com        AutoEffectUnref effect(SkNEW_ARGS(GrMatrixConvolutionEffect, (texture,
3437938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                                                                      bounds,
3446340a41108633ac1ce5941e5cd30538630c4c55bbsalomon@google.com                                                                      kernelSize,
3456340a41108633ac1ce5941e5cd30538630c4c55bbsalomon@google.com                                                                      kernel,
3466340a41108633ac1ce5941e5cd30538630c4c55bbsalomon@google.com                                                                      gain,
3476340a41108633ac1ce5941e5cd30538630c4c55bbsalomon@google.com                                                                      bias,
348cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                                                                      kernelOffset,
3496340a41108633ac1ce5941e5cd30538630c4c55bbsalomon@google.com                                                                      tileMode,
3506340a41108633ac1ce5941e5cd30538630c4c55bbsalomon@google.com                                                                      convolveAlpha)));
351a1ebbe447d5eab098111eb83580e55f2f5f6facabsalomon@google.com        return CreateEffectRef(effect);
3520ac6af49975c54c2debf41e9200af416ecd2d973bsalomon@google.com    }
3533bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    virtual ~GrMatrixConvolutionEffect();
3543bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
3550263fcd72ee81ccff012bff85dbee89cd524a930bsalomon@google.com    virtual void getConstantColorComponents(GrColor* color,
3560263fcd72ee81ccff012bff85dbee89cd524a930bsalomon@google.com                                            uint32_t* validFlags) const SK_OVERRIDE {
3570263fcd72ee81ccff012bff85dbee89cd524a930bsalomon@google.com        // TODO: Try to do better?
3580263fcd72ee81ccff012bff85dbee89cd524a930bsalomon@google.com        *validFlags = 0;
3590263fcd72ee81ccff012bff85dbee89cd524a930bsalomon@google.com    }
3600263fcd72ee81ccff012bff85dbee89cd524a930bsalomon@google.com
3613bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    static const char* Name() { return "MatrixConvolution"; }
3627938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    const SkIRect& bounds() const { return fBounds; }
3633bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    const SkISize& kernelSize() const { return fKernelSize; }
364cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org    const float* kernelOffset() const { return fKernelOffset; }
3653bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    const float* kernel() const { return fKernel; }
3663bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    float gain() const { return fGain; }
3673bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    float bias() const { return fBias; }
3683bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    TileMode tileMode() const { return fTileMode; }
3693bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    bool convolveAlpha() const { return fConvolveAlpha; }
3703bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
371422e81aeb1f4078367c85efe591c7df8c33874ecbsalomon@google.com    typedef GrGLMatrixConvolutionEffect GLEffect;
3723bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
373396e61fe440590744345e0c56970b26ab464591dbsalomon@google.com    virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
3743bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
3753bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.orgprivate:
3760ac6af49975c54c2debf41e9200af416ecd2d973bsalomon@google.com    GrMatrixConvolutionEffect(GrTexture*,
3777938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                              const SkIRect& bounds,
3780ac6af49975c54c2debf41e9200af416ecd2d973bsalomon@google.com                              const SkISize& kernelSize,
3790ac6af49975c54c2debf41e9200af416ecd2d973bsalomon@google.com                              const SkScalar* kernel,
3800ac6af49975c54c2debf41e9200af416ecd2d973bsalomon@google.com                              SkScalar gain,
3810ac6af49975c54c2debf41e9200af416ecd2d973bsalomon@google.com                              SkScalar bias,
382cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                              const SkIPoint& kernelOffset,
3830ac6af49975c54c2debf41e9200af416ecd2d973bsalomon@google.com                              TileMode tileMode,
3840ac6af49975c54c2debf41e9200af416ecd2d973bsalomon@google.com                              bool convolveAlpha);
3850ac6af49975c54c2debf41e9200af416ecd2d973bsalomon@google.com
3868a252f79629b189a03de22cd8ff0312c5bccedd1bsalomon@google.com    virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE;
38768b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
3887938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    SkIRect  fBounds;
3893bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    SkISize  fKernelSize;
3903bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    float   *fKernel;
3913bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    float    fGain;
3923bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    float    fBias;
393cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org    float    fKernelOffset[2];
3943bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    TileMode fTileMode;
3953bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    bool     fConvolveAlpha;
3963bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
397f271cc7183fe48ac64d2d9a454eb013c91b42d53bsalomon@google.com    GR_DECLARE_EFFECT_TEST;
3983bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
3993bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    typedef GrSingleTextureEffect INHERITED;
4003bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org};
4013bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
40222a800a2578564a8b66bd4d9903ef4186c37f35cbsalomon@google.comclass GrGLMatrixConvolutionEffect : public GrGLEffect {
4033bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.orgpublic:
404396e61fe440590744345e0c56970b26ab464591dbsalomon@google.com    GrGLMatrixConvolutionEffect(const GrBackendEffectFactory& factory,
405c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                                const GrDrawEffect& effect);
40622a800a2578564a8b66bd4d9903ef4186c37f35cbsalomon@google.com    virtual void emitCode(GrGLShaderBuilder*,
407c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                          const GrDrawEffect&,
40822a800a2578564a8b66bd4d9903ef4186c37f35cbsalomon@google.com                          EffectKey,
40922a800a2578564a8b66bd4d9903ef4186c37f35cbsalomon@google.com                          const char* outputColor,
41022a800a2578564a8b66bd4d9903ef4186c37f35cbsalomon@google.com                          const char* inputColor,
41177af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com                          const TransformedCoordsArray&,
41222a800a2578564a8b66bd4d9903ef4186c37f35cbsalomon@google.com                          const TextureSamplerArray&) SK_OVERRIDE;
4133bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
414c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&);
4153bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
416c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
4173bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
4183bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.orgprivate:
4193bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    typedef GrGLUniformManager::UniformHandle        UniformHandle;
4203bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    typedef SkMatrixConvolutionImageFilter::TileMode TileMode;
42117fc651dbe2e0624f6c85fb6e081d28a87d5a08bbsalomon@google.com    SkISize             fKernelSize;
42217fc651dbe2e0624f6c85fb6e081d28a87d5a08bbsalomon@google.com    TileMode            fTileMode;
42317fc651dbe2e0624f6c85fb6e081d28a87d5a08bbsalomon@google.com    bool                fConvolveAlpha;
42417fc651dbe2e0624f6c85fb6e081d28a87d5a08bbsalomon@google.com
4257938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    UniformHandle       fBoundsUni;
42617fc651dbe2e0624f6c85fb6e081d28a87d5a08bbsalomon@google.com    UniformHandle       fKernelUni;
42717fc651dbe2e0624f6c85fb6e081d28a87d5a08bbsalomon@google.com    UniformHandle       fImageIncrementUni;
42884cd099704b3896ca66081a96508572a924f850ccommit-bot@chromium.org    UniformHandle       fKernelOffsetUni;
42917fc651dbe2e0624f6c85fb6e081d28a87d5a08bbsalomon@google.com    UniformHandle       fGainUni;
43017fc651dbe2e0624f6c85fb6e081d28a87d5a08bbsalomon@google.com    UniformHandle       fBiasUni;
4313bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
43222a800a2578564a8b66bd4d9903ef4186c37f35cbsalomon@google.com    typedef GrGLEffect INHERITED;
4333bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org};
4343bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
435396e61fe440590744345e0c56970b26ab464591dbsalomon@google.comGrGLMatrixConvolutionEffect::GrGLMatrixConvolutionEffect(const GrBackendEffectFactory& factory,
436c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                                                         const GrDrawEffect& drawEffect)
43777af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com    : INHERITED(factory) {
438c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvolutionEffect>();
4393bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    fKernelSize = m.kernelSize();
4403bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    fTileMode = m.tileMode();
4413bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    fConvolveAlpha = m.convolveAlpha();
4423bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org}
4433bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
4443bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.orgstatic void appendTextureLookup(GrGLShaderBuilder* builder,
4453bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org                                const GrGLShaderBuilder::TextureSampler& sampler,
4463bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org                                const char* coord,
4477938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                                const char* bounds,
4483bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org                                SkMatrixConvolutionImageFilter::TileMode tileMode) {
4493bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    SkString clampedCoord;
4503bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    switch (tileMode) {
4513bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org        case SkMatrixConvolutionImageFilter::kClamp_TileMode:
4527938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org            clampedCoord.printf("clamp(%s, %s.xy, %s.zw)", coord, bounds, bounds);
4533bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org            coord = clampedCoord.c_str();
4543bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org            break;
4553bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org        case SkMatrixConvolutionImageFilter::kRepeat_TileMode:
4567938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org            clampedCoord.printf("mod(%s - %s.xy, %s.zw - %s.xy) + %s.xy", coord, bounds, bounds, bounds, bounds);
4573bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org            coord = clampedCoord.c_str();
4583bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org            break;
4593bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org        case SkMatrixConvolutionImageFilter::kClampToBlack_TileMode:
4607938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org            builder->fsCodeAppendf("clamp(%s, %s.xy, %s.zw) != %s ? vec4(0, 0, 0, 0) : ", coord, bounds, bounds, coord);
4613bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org            break;
4623bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    }
46374a3a2135ca82ab9324b7e499caa3280348a4fdacommit-bot@chromium.org    builder->fsAppendTextureLookup(sampler, coord);
4643bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org}
4653bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
46622a800a2578564a8b66bd4d9903ef4186c37f35cbsalomon@google.comvoid GrGLMatrixConvolutionEffect::emitCode(GrGLShaderBuilder* builder,
467c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                                           const GrDrawEffect&,
46817fc651dbe2e0624f6c85fb6e081d28a87d5a08bbsalomon@google.com                                           EffectKey key,
46922a800a2578564a8b66bd4d9903ef4186c37f35cbsalomon@google.com                                           const char* outputColor,
47022a800a2578564a8b66bd4d9903ef4186c37f35cbsalomon@google.com                                           const char* inputColor,
47177af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com                                           const TransformedCoordsArray& coords,
47222a800a2578564a8b66bd4d9903ef4186c37f35cbsalomon@google.com                                           const TextureSamplerArray& samplers) {
473a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    sk_ignore_unused_variable(inputColor);
47477af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com    SkString coords2D = builder->ensureFSCoords2D(coords, 0);
4757938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    fBoundsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
4767938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                                     kVec4f_GrSLType, "Bounds");
47774a3a2135ca82ab9324b7e499caa3280348a4fdacommit-bot@chromium.org    fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
47822a800a2578564a8b66bd4d9903ef4186c37f35cbsalomon@google.com                                             kVec2f_GrSLType, "ImageIncrement");
47974a3a2135ca82ab9324b7e499caa3280348a4fdacommit-bot@chromium.org    fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_Visibility,
4807938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                                             kFloat_GrSLType,
4817938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                                             "Kernel",
4827938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org                                             fKernelSize.width() * fKernelSize.height());
48384cd099704b3896ca66081a96508572a924f850ccommit-bot@chromium.org    fKernelOffsetUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
48484cd099704b3896ca66081a96508572a924f850ccommit-bot@chromium.org                                             kVec2f_GrSLType, "KernelOffset");
48574a3a2135ca82ab9324b7e499caa3280348a4fdacommit-bot@chromium.org    fGainUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
48622a800a2578564a8b66bd4d9903ef4186c37f35cbsalomon@google.com                                   kFloat_GrSLType, "Gain");
48774a3a2135ca82ab9324b7e499caa3280348a4fdacommit-bot@chromium.org    fBiasUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
48822a800a2578564a8b66bd4d9903ef4186c37f35cbsalomon@google.com                                   kFloat_GrSLType, "Bias");
48922a800a2578564a8b66bd4d9903ef4186c37f35cbsalomon@google.com
4907938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    const char* bounds = builder->getUniformCStr(fBoundsUni);
49184cd099704b3896ca66081a96508572a924f850ccommit-bot@chromium.org    const char* kernelOffset = builder->getUniformCStr(fKernelOffsetUni);
4923bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
4933bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    const char* kernel = builder->getUniformCStr(fKernelUni);
4943bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    const char* gain = builder->getUniformCStr(fGainUni);
4953bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    const char* bias = builder->getUniformCStr(fBiasUni);
4963bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    int kWidth = fKernelSize.width();
4973bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    int kHeight = fKernelSize.height();
4983bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
499f910d3b23bcf590ee937628dbab8e39a98ee5860bsalomon@google.com    builder->fsCodeAppend("\t\tvec4 sum = vec4(0, 0, 0, 0);\n");
500cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org    builder->fsCodeAppendf("\t\tvec2 coord = %s - %s * %s;\n", coords2D.c_str(), kernelOffset, imgInc);
501f910d3b23bcf590ee937628dbab8e39a98ee5860bsalomon@google.com    builder->fsCodeAppendf("\t\tfor (int y = 0; y < %d; y++) {\n", kHeight);
502f910d3b23bcf590ee937628dbab8e39a98ee5860bsalomon@google.com    builder->fsCodeAppendf("\t\t\tfor (int x = 0; x < %d; x++) {\n", kWidth);
503f910d3b23bcf590ee937628dbab8e39a98ee5860bsalomon@google.com    builder->fsCodeAppendf("\t\t\t\tfloat k = %s[y * %d + x];\n", kernel, kWidth);
504f910d3b23bcf590ee937628dbab8e39a98ee5860bsalomon@google.com    builder->fsCodeAppendf("\t\t\t\tvec2 coord2 = coord + vec2(x, y) * %s;\n", imgInc);
505f910d3b23bcf590ee937628dbab8e39a98ee5860bsalomon@google.com    builder->fsCodeAppend("\t\t\t\tvec4 c = ");
5067938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org    appendTextureLookup(builder, samplers[0], "coord2", bounds, fTileMode);
507f910d3b23bcf590ee937628dbab8e39a98ee5860bsalomon@google.com    builder->fsCodeAppend(";\n");
5083bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    if (!fConvolveAlpha) {
509f910d3b23bcf590ee937628dbab8e39a98ee5860bsalomon@google.com        builder->fsCodeAppend("\t\t\t\tc.rgb /= c.a;\n");
5103bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    }
511f910d3b23bcf590ee937628dbab8e39a98ee5860bsalomon@google.com    builder->fsCodeAppend("\t\t\t\tsum += c * k;\n");
512f910d3b23bcf590ee937628dbab8e39a98ee5860bsalomon@google.com    builder->fsCodeAppend("\t\t\t}\n");
513f910d3b23bcf590ee937628dbab8e39a98ee5860bsalomon@google.com    builder->fsCodeAppend("\t\t}\n");
5143bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    if (fConvolveAlpha) {
515f910d3b23bcf590ee937628dbab8e39a98ee5860bsalomon@google.com        builder->fsCodeAppendf("\t\t%s = sum * %s + %s;\n", outputColor, gain, bias);
5167938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org        builder->fsCodeAppendf("\t\t%s.rgb = clamp(%s.rgb, 0.0, %s.a);\n",
5177938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org            outputColor, outputColor, outputColor);
5183bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    } else {
519f910d3b23bcf590ee937628dbab8e39a98ee5860bsalomon@google.com        builder->fsCodeAppend("\t\tvec4 c = ");
5207938bae14af94c1d48d122a2d686e123b66411a7senorblanco@chromium.org        appendTextureLookup(builder, samplers[0], coords2D.c_str(), bounds, fTileMode);
521f910d3b23bcf590ee937628dbab8e39a98ee5860bsalomon@google.com        builder->fsCodeAppend(";\n");
522f910d3b23bcf590ee937628dbab8e39a98ee5860bsalomon@google.com        builder->fsCodeAppendf("\t\t%s.a = c.a;\n", outputColor);
523f910d3b23bcf590ee937628dbab8e39a98ee5860bsalomon@google.com        builder->fsCodeAppendf("\t\t%s.rgb = sum.rgb * %s + %s;\n", outputColor, gain, bias);
524f910d3b23bcf590ee937628dbab8e39a98ee5860bsalomon@google.com        builder->fsCodeAppendf("\t\t%s.rgb *= %s.a;\n", outputColor, outputColor);
5253bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    }
5263bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org}
5273bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
5283bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.orgnamespace {
5293bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
5303bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.orgint encodeXY(int x, int y) {
5313bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    SkASSERT(x >= 1 && y >= 1 && x * y <= 32);
5323bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org    if (y < x)
5333bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org        return 0x40 | encodeXY(y, x);
534dda28c9bde2a34b98e1383532c210270dacb7ca8skia.committer@gmail.com    else
5353bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org        return (0x40 >> x) | (y - x);
5363bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org}
5373bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
5383bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org};
5393bc16c8bc1ecb9ac4450f58093cc9e3edb8a50b8senorblanco@chromium.org
540c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.comGrGLEffect::EffectKey GrGLMatrixConvolutionEffect::GenKey(const GrDrawEffect& drawEffect,
541