diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/SkBitmapFilter.cpp | 378 | ||||
-rw-r--r-- | src/core/SkBitmapFilter.h | 150 | ||||
-rw-r--r-- | src/core/SkBitmapProcBicubic.cpp | 187 | ||||
-rw-r--r-- | src/core/SkBitmapProcState.cpp | 8 | ||||
-rw-r--r-- | src/core/SkBitmapProcState.h | 18 | ||||
-rw-r--r-- | src/opts/SkBitmapFilter_opts_SSE2.cpp | 189 | ||||
-rw-r--r-- | src/opts/SkBitmapFilter_opts_SSE2.h | 20 | ||||
-rw-r--r-- | src/opts/opts_check_SSE2.cpp | 13 |
8 files changed, 196 insertions, 767 deletions
diff --git a/src/core/SkBitmapFilter.cpp b/src/core/SkBitmapFilter.cpp deleted file mode 100644 index cc6ad95d1e..0000000000 --- a/src/core/SkBitmapFilter.cpp +++ /dev/null @@ -1,378 +0,0 @@ -/* - * Copyright 2013 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#include "SkBitmapProcState.h" -#include "SkBitmap.h" -#include "SkColor.h" -#include "SkColorPriv.h" -#include "SkUnPreMultiply.h" -#include "SkShader.h" -#include "SkRTConf.h" -#include "SkMath.h" - -void highQualityFilter(const SkBitmapProcState& s, int x, int y, - SkPMColor* SK_RESTRICT colors, int count) { - - const int maxX = s.fBitmap->width() - 1; - const int maxY = s.fBitmap->height() - 1; - - while (count-- > 0) { - SkPoint srcPt; - s.fInvProc(*s.fInvMatrix, SkIntToScalar(x), - SkIntToScalar(y), &srcPt); - srcPt.fX -= SK_ScalarHalf; - srcPt.fY -= SK_ScalarHalf; - SkScalar fractx = srcPt.fX - SkScalarFloorToScalar(srcPt.fX); - SkScalar fracty = srcPt.fY - SkScalarFloorToScalar(srcPt.fY); - - int sx = SkScalarFloorToInt(srcPt.fX); - int sy = SkScalarFloorToInt(srcPt.fY); - - SkFixed weight = 0; - SkFixed fr = 0, fg = 0, fb = 0, fa = 0; - - int y0 = SkClampMax(int(ceil(sy-s.getBitmapFilter()->width() + 0.5f)), maxY); - int y1 = SkClampMax(int(floor(sy+s.getBitmapFilter()->width() + 0.5f)), maxY); - int x0 = SkClampMax(int(ceil(sx-s.getBitmapFilter()->width() + 0.5f)), maxX); - int x1 = SkClampMax(int(floor(sx+s.getBitmapFilter()->width() + 0.5f)), maxX); - - for (int src_y = y0; src_y <= y1; src_y++) { - SkFixed yweight = s.getBitmapFilter()->lookup((srcPt.fY - src_y)); - - for (int src_x = x0; src_x <= x1 ; src_x++) { - SkFixed xweight = s.getBitmapFilter()->lookup((srcPt.fX - src_x)); - - SkFixed combined_weight = SkFixedMul(xweight, yweight); - - SkPMColor c = *s.fBitmap->getAddr32(src_x, src_y); - fr += combined_weight * SkGetPackedR32(c); - fg += combined_weight * SkGetPackedG32(c); - fb += combined_weight * SkGetPackedB32(c); - fa += combined_weight * SkGetPackedA32(c); - weight += combined_weight; - } - } - - fr = SkFixedDiv(fr, weight); - fg = SkFixedDiv(fg, weight); - fb = SkFixedDiv(fb, weight); - fa = SkFixedDiv(fa, weight); - - int a = SkClampMax(SkFixedRoundToInt(fa), 255); - int r = SkClampMax(SkFixedRoundToInt(fr), a); - int g = SkClampMax(SkFixedRoundToInt(fg), a); - int b = SkClampMax(SkFixedRoundToInt(fb), a); - - *colors++ = SkPackARGB32(a, r, g, b); - - x++; - } -} - -void highQualityFilter_ScaleOnly(const SkBitmapProcState &s, int x, int y, - SkPMColor *SK_RESTRICT colors, int count) { - const int maxX = s.fBitmap->width() - 1; - const int maxY = s.fBitmap->height() - 1; - - SkPoint srcPt; - - s.fInvProc(*s.fInvMatrix, SkIntToScalar(x), - SkIntToScalar(y), &srcPt); - srcPt.fY -= SK_ScalarHalf; - SkScalar fracty = srcPt.fY - SkScalarFloorToScalar(srcPt.fY); - int sy = SkScalarFloorToInt(srcPt.fY); - int y0 = SkClampMax(int(ceil(sy-s.getBitmapFilter()->width() + 0.5f)), maxY); - int y1 = SkClampMax(int(floor(sy+s.getBitmapFilter()->width() + 0.5f)), maxY); - - while (count-- > 0) { - s.fInvProc(*s.fInvMatrix, SkIntToScalar(x), - SkIntToScalar(y), &srcPt); - srcPt.fX -= SK_ScalarHalf; - srcPt.fY -= SK_ScalarHalf; - SkScalar fractx = srcPt.fX - SkScalarFloorToScalar(srcPt.fX); - - int sx = SkScalarFloorToInt(srcPt.fX); - - SkFixed weight = 0; - SkFixed fr = 0, fg = 0, fb = 0, fa = 0; - - int x0 = SkClampMax(int(ceil(sx-s.getBitmapFilter()->width() + 0.5f)), maxX); - int x1 = SkClampMax(int(floor(sx+s.getBitmapFilter()->width() + 0.5f)), maxX); - - for (int src_y = y0; src_y <= y1; src_y++) { - SkFixed yweight = s.getBitmapFilter()->lookup((srcPt.fY - src_y)); - - for (int src_x = x0; src_x <= x1 ; src_x++) { - SkFixed xweight = s.getBitmapFilter()->lookup((srcPt.fX - src_x)); - - SkFixed combined_weight = SkFixedMul(xweight, yweight); - - SkPMColor c = *s.fBitmap->getAddr32(src_x, src_y); - fr += combined_weight * SkGetPackedR32(c); - fg += combined_weight * SkGetPackedG32(c); - fb += combined_weight * SkGetPackedB32(c); - fa += combined_weight * SkGetPackedA32(c); - weight += combined_weight; - } - } - - fr = SkFixedDiv(fr, weight); - fg = SkFixedDiv(fg, weight); - fb = SkFixedDiv(fb, weight); - fa = SkFixedDiv(fa, weight); - - int a = SkClampMax(SkFixedRoundToInt(fa), 255); - int r = SkClampMax(SkFixedRoundToInt(fr), a); - int g = SkClampMax(SkFixedRoundToInt(fg), a); - int b = SkClampMax(SkFixedRoundToInt(fb), a); - - *colors++ = SkPackARGB32(a, r, g, b); - - x++; - } -} - -SK_CONF_DECLARE(const char *, c_bitmapFilter, "bitmap.filter", "mitchell", "Which bitmap filter to use [mitchell, sinc, gaussian, triangle, box]"); - -static SkBitmapFilter *allocateBitmapFilter() { - if (!strcmp(c_bitmapFilter, "mitchell")) { - return SkNEW_ARGS(SkMitchellFilter,(1.f/3.f,1.f/3.f)); - } else if (!strcmp(c_bitmapFilter, "sinc")) { - return SkNEW_ARGS(SkSincFilter,(3)); - } else if (!strcmp(c_bitmapFilter, "gaussian")) { - return SkNEW_ARGS(SkGaussianFilter,(2)); - } else if (!strcmp(c_bitmapFilter, "triangle")) { - return SkNEW(SkTriangleFilter); - } else if (!strcmp(c_bitmapFilter, "box")) { - return SkNEW(SkBoxFilter); - } else { - SkASSERT(!!!"Unknown filter type"); - } - - return NULL; -} - -SkBitmapProcState::ShaderProc32 -SkBitmapProcState::chooseBitmapFilterProc(const SkPaint& paint) { - // we need to be requested - uint32_t mask = SkPaint::kFilterBitmap_Flag - | SkPaint::kHighQualityFilterBitmap_Flag - ; - if ((paint.getFlags() & mask) != mask) { - return NULL; - } - - // TODO: consider supporting other configs (e.g. 565, A8) - if (fBitmap->config() != SkBitmap::kARGB_8888_Config) { - return NULL; - } - - // TODO: consider supporting repeat and mirror - if (SkShader::kClamp_TileMode != fTileModeX || SkShader::kClamp_TileMode != fTileModeY) { - return NULL; - } - - // TODO: support blending inside our procs - if (0xFF != paint.getAlpha()) { - return NULL; - } - - if (fInvType & (SkMatrix::kAffine_Mask | SkMatrix::kScale_Mask)) { - fBitmapFilter = allocateBitmapFilter(); - } - - if (fInvType & SkMatrix::kAffine_Mask) { - return highQualityFilter; - } else if (fInvType & SkMatrix::kScale_Mask) { - return highQualityFilter_ScaleOnly; - } else { - return NULL; - } -} - -static void divideByWeights(SkFixed *sums, SkFixed *weights, SkBitmap *dst) { - for (int y = 0 ; y < dst->height() ; y++) { - for (int x = 0 ; x < dst->width() ; x++) { - SkFixed fr = SkFixedDiv(sums[4*(y*dst->width() + x) + 0], weights[y*dst->width() + x]); - SkFixed fg = SkFixedDiv(sums[4*(y*dst->width() + x) + 1], weights[y*dst->width() + x]); - SkFixed fb = SkFixedDiv(sums[4*(y*dst->width() + x) + 2], weights[y*dst->width() + x]); - SkFixed fa = SkFixedDiv(sums[4*(y*dst->width() + x) + 3], weights[y*dst->width() + x]); - int a = SkClampMax(SkFixedRoundToInt(fa), 255); - int r = SkClampMax(SkFixedRoundToInt(fr), a); - int g = SkClampMax(SkFixedRoundToInt(fg), a); - int b = SkClampMax(SkFixedRoundToInt(fb), a); - - *dst->getAddr32(x,y) = SkPackARGB32(a, r, g, b); - } - } -} - -static void upScaleHoriz(const SkBitmap *src, SkBitmap *dst, float scale, SkBitmapFilter *filter) { - for (int y = 0 ; y < src->height() ; y++) { - for (int x = 0 ; x < dst->width() ; x++) { - float sx = x / scale - 0.5f; - int x0 = SkClampMax(int(ceil(sx-filter->width() + 0.5f)), src->width()-1); - int x1 = SkClampMax(int(floor(sx+filter->width() + 0.5f)), src->width()-1); - - SkFixed total_weight = 0; - SkFixed fr = 0, fg = 0, fb = 0, fa = 0; - - for (int src_x = x0 ; src_x <= x1 ; src_x++) { - SkFixed weight = filter->lookup(sx - src_x); - SkPMColor c = *src->getAddr32(src_x,y); - fr += weight * SkGetPackedR32(c); - fg += weight * SkGetPackedG32(c); - fb += weight * SkGetPackedB32(c); - fa += weight * SkGetPackedA32(c); - total_weight += weight; - } - fr = SkFixedDiv(fr, total_weight); - fg = SkFixedDiv(fg, total_weight); - fb = SkFixedDiv(fb, total_weight); - fa = SkFixedDiv(fa, total_weight); - - int a = SkClampMax(SkFixedRoundToInt(fa), 255); - int r = SkClampMax(SkFixedRoundToInt(fr), a); - int g = SkClampMax(SkFixedRoundToInt(fg), a); - int b = SkClampMax(SkFixedRoundToInt(fb), a); - - *dst->getAddr32(x,y) = SkPackARGB32(a, r, g, b); - } - } -} - -static void downScaleHoriz(const SkBitmap *src, SkBitmap *dst, float scale, SkBitmapFilter *filter) { - SkFixed *sums = SkNEW_ARRAY(SkFixed, dst->width() * dst->height() * 4); - SkFixed *weights = SkNEW_ARRAY(SkFixed, dst->width() * dst->height()); - - SkAutoTDeleteArray<SkFixed> ada1(sums); - SkAutoTDeleteArray<SkFixed> ada2(weights); - - memset(sums, 0, dst->width() * dst->height() * sizeof(SkFixed) * 4); - memset(weights, 0, dst->width() * dst->height() * sizeof(SkFixed)); - - for (int y = 0 ; y < src->height() ; y++) { - for (int x = 0 ; x < src->width() ; x++) { - // splat each source pixel into the destination image - float dx = (x + 0.5f) * scale; - int x0 = SkClampMax(int(ceil(dx-filter->width() + 0.5f)), dst->width()-1); - int x1 = SkClampMax(int(floor(dx+filter->width() + 0.5f)), dst->width()-1); - - SkPMColor c = *src->getAddr32(x,y); - - for (int dst_x = x0 ; dst_x <= x1 ; dst_x++) { - SkFixed weight = filter->lookup(dx - dst_x); - sums[4*(y*dst->width() + dst_x) + 0] += weight*SkGetPackedR32(c); - sums[4*(y*dst->width() + dst_x) + 1] += weight*SkGetPackedG32(c); - sums[4*(y*dst->width() + dst_x) + 2] += weight*SkGetPackedB32(c); - sums[4*(y*dst->width() + dst_x) + 3] += weight*SkGetPackedA32(c); - weights[y*dst->width() + dst_x] += weight; - } - } - } - - divideByWeights(sums, weights, dst); -} - -static void upScaleVert(const SkBitmap *src, SkBitmap *dst, float scale, SkBitmapFilter *filter) { - for (int y = 0 ; y < dst->height() ; y++) { - for (int x = 0 ; x < dst->width() ; x++) { - float sy = y / scale - 0.5f; - int y0 = SkClampMax(int(ceil(sy-filter->width() + 0.5f)), src->height()-1); - int y1 = SkClampMax(int(floor(sy+filter->width() + 0.5f)), src->height()-1); - - SkFixed total_weight = 0; - SkFixed fr = 0, fg = 0, fb = 0, fa = 0; - - for (int src_y = y0 ; src_y <= y1 ; src_y++) { - SkFixed weight = filter->lookup(sy - src_y); - SkPMColor c = *src->getAddr32(x,src_y); - fr += weight * SkGetPackedR32(c); - fg += weight * SkGetPackedG32(c); - fb += weight * SkGetPackedB32(c); - fa += weight * SkGetPackedA32(c); - total_weight += weight; - } - fr = SkFixedDiv(fr, total_weight); - fg = SkFixedDiv(fg, total_weight); - fb = SkFixedDiv(fb, total_weight); - fa = SkFixedDiv(fa, total_weight); - - int a = SkClampMax(SkFixedRoundToInt(fa), 255); - int r = SkClampMax(SkFixedRoundToInt(fr), a); - int g = SkClampMax(SkFixedRoundToInt(fg), a); - int b = SkClampMax(SkFixedRoundToInt(fb), a); - - *dst->getAddr32(x,y) = SkPackARGB32(a, r, g, b); - } - } -} - -static void downScaleVert(const SkBitmap *src, SkBitmap *dst, float scale, SkBitmapFilter *filter) { - SkFixed *sums = SkNEW_ARRAY(SkFixed, dst->width() * dst->height() * 4); - SkFixed *weights = SkNEW_ARRAY(SkFixed, dst->width() * dst->height()); - - SkAutoTDeleteArray<SkFixed> ada1(sums); - SkAutoTDeleteArray<SkFixed> ada2(weights); - - memset(sums, 0, dst->width() * dst->height() * sizeof(SkFixed) * 4); - memset(weights, 0, dst->width() * dst->height() * sizeof(SkFixed)); - - for (int y = 0 ; y < src->height() ; y++) { - for (int x = 0 ; x < src->width() ; x++) { - // splat each source pixel into the destination image - float dy = (y + 0.5f) * scale; - int y0 = SkClampMax(int(ceil(dy-filter->width() + 0.5f)), dst->height()-1); - int y1 = SkClampMax(int(floor(dy+filter->width() + 0.5f)), dst->height()-1); - - SkPMColor c = *src->getAddr32(x,y); - - for (int dst_y = y0 ; dst_y <= y1 ; dst_y++) { - SkFixed weight = filter->lookup(dy - dst_y); - sums[4*(dst_y*dst->width() + x) + 0] += weight*SkGetPackedR32(c); - sums[4*(dst_y*dst->width() + x) + 1] += weight*SkGetPackedG32(c); - sums[4*(dst_y*dst->width() + x) + 2] += weight*SkGetPackedB32(c); - sums[4*(dst_y*dst->width() + x) + 3] += weight*SkGetPackedA32(c); - weights[dst_y*dst->width() + x] += weight; - } - } - } - - divideByWeights(sums, weights, dst); -} - -void SkBitmap::scale(SkBitmap *dst) const { - - SkBitmap horiz_temp; - - horiz_temp.setConfig(SkBitmap::kARGB_8888_Config, dst->width(), height()); - horiz_temp.allocPixels(); - - SkBitmapFilter *filter = allocateBitmapFilter(); - - float horiz_scale = float(dst->width()) / width(); - - if (horiz_scale == 1) { - this->copyPixelsTo(horiz_temp.getPixels(), getSize()); - } else if (horiz_scale > 1) { - upScaleHoriz(this, &horiz_temp, horiz_scale, filter); - } else if (horiz_scale < 1) { - downScaleHoriz(this, &horiz_temp, horiz_scale, filter); - } - - float vert_scale = float(dst->height()) / height(); - - if (vert_scale == 1) { - horiz_temp.copyPixelsTo(dst->getPixels(), dst->getSize()); - } else if (vert_scale > 1) { - upScaleVert(&horiz_temp, dst, vert_scale, filter); - } else if (vert_scale < 1) { - downScaleVert(&horiz_temp, dst, vert_scale, filter); - } - - SkDELETE(filter); -} diff --git a/src/core/SkBitmapFilter.h b/src/core/SkBitmapFilter.h deleted file mode 100644 index 4e4778876c..0000000000 --- a/src/core/SkBitmapFilter.h +++ /dev/null @@ -1,150 +0,0 @@ - -/* - * Copyright 2013 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - - -#ifndef SkBitmapFilter_DEFINED -#define SkBitmapFilter_DEFINED - -#include "SkMath.h" - -// size of the precomputed bitmap filter tables for high quality filtering. -// Used to precompute the shape of the filter kernel. -// Table size chosen from experiments to see where I could start to see a difference. - -#define SKBITMAP_FILTER_TABLE_SIZE 32 - -class SkBitmapFilter { - public: - SkBitmapFilter(float width) - : fWidth(width), fInvWidth(1.f/width) { - precomputed = false; - } - - SkFixed lookup( float x ) const { - if (!precomputed) { - precomputeTable(); - } - int filter_idx = int(fabsf(x * invWidth() * SKBITMAP_FILTER_TABLE_SIZE)); - return fFilterTable[ SkTMin(filter_idx, SKBITMAP_FILTER_TABLE_SIZE-1) ]; - } - - SkFixed lookupFloat( float x ) const { - if (!precomputed) { - precomputeTable(); - } - int filter_idx = int(fabsf(x * invWidth() * SKBITMAP_FILTER_TABLE_SIZE)); - return fFilterTableFloat[ SkTMin(filter_idx, SKBITMAP_FILTER_TABLE_SIZE-1) ]; - } - - float width() const { return fWidth; } - float invWidth() const { return fInvWidth; } - virtual float evaluate(float x) const = 0; - protected: - float fWidth; - float fInvWidth; - - mutable bool precomputed; - mutable SkFixed fFilterTable[SKBITMAP_FILTER_TABLE_SIZE]; - mutable float fFilterTableFloat[SKBITMAP_FILTER_TABLE_SIZE]; - private: - void precomputeTable() const { - precomputed = true; - SkFixed *ftp = fFilterTable; - float *ftp_float = fFilterTableFloat; - for (int x = 0; x < SKBITMAP_FILTER_TABLE_SIZE; ++x) { - float fx = ((float)x + .5f) * this->width() / SKBITMAP_FILTER_TABLE_SIZE; - float filter_value = evaluate(fx); - *ftp_float++ = filter_value; - *ftp++ = SkFloatToFixed(filter_value); - } - } -}; - -class SkMitchellFilter: public SkBitmapFilter { - public: - SkMitchellFilter(float b, float c, float width=2.0f) - : SkBitmapFilter(width), B(b), C(c) { - } - - virtual float evaluate(float x) const SK_OVERRIDE { - x = fabsf(x); - float ret; - if (x > 2.f) { - return 0; - } else if (x > 1.f) { - return ((-B - 6*C) * x*x*x + (6*B + 30*C) * x*x + - (-12*B - 48*C) * x + (8*B + 24*C)) * (1.f/6.f); - } else { - return ((12 - 9*B - 6*C) * x*x*x + - (-18 + 12*B + 6*C) * x*x + - (6 - 2*B)) * (1.f/6.f); - } - } - protected: - float B, C; -}; - -class SkGaussianFilter: public SkBitmapFilter { - public: - SkGaussianFilter(float a, float width=2.0f) - : SkBitmapFilter(width), alpha(a), expWidth(expf(-alpha * width * width)) { - } - - virtual float evaluate(float x) const SK_OVERRIDE { - return SkTMax(0.f, float(expf(-alpha*x*x) - expWidth)); - } - protected: - float alpha, expWidth; -}; - -class SkTriangleFilter: public SkBitmapFilter { - public: - SkTriangleFilter(float width=1) - : SkBitmapFilter(width) { - } - - virtual float evaluate(float x) const SK_OVERRIDE { - return SkTMax(0.f, fWidth - fabsf(x)); - } - protected: -}; - -class SkBoxFilter: public SkBitmapFilter { - public: - SkBoxFilter(float width=0.5f) - : SkBitmapFilter(width) { - } - - virtual float evaluate(float x) const SK_OVERRIDE { - return 1; - } - protected: -}; - - -class SkSincFilter: public SkBitmapFilter { - public: - SkSincFilter(float t, float width=3.f) - : SkBitmapFilter(width), tau(t) { - } - - virtual float evaluate(float x) const SK_OVERRIDE { - x = sk_float_abs(x * fInvWidth); - if (x < 1e-5f) return 1.f; - if (x > 1.f) return 0.f; - x *= M_PI; - float sinc = sk_float_sin(x) / x; - float lanczos = sk_float_sin(x * tau) / (x * tau); - return sinc * lanczos; - } - protected: - float tau; -}; - - -#endif diff --git a/src/core/SkBitmapProcBicubic.cpp b/src/core/SkBitmapProcBicubic.cpp new file mode 100644 index 0000000000..f2cd0e30e7 --- /dev/null +++ b/src/core/SkBitmapProcBicubic.cpp @@ -0,0 +1,187 @@ +/* + * Copyright 2013 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkBitmapProcState.h" +#include "SkBitmap.h" +#include "SkColor.h" +#include "SkColorPriv.h" +#include "SkUnPreMultiply.h" +#include "SkRTConf.h" +#include "SkShader.h" + +#define DS(x) SkDoubleToScalar(x) + +#define MUL(a, b) ((a) * (b)) + +static inline SkPMColor cubicBlend(const SkFixed cc[4], SkPMColor c0, SkPMColor c1, SkPMColor c2, SkPMColor c3) { + SkFixed fa = MUL(cc[0], SkGetPackedA32(c0)) + MUL(cc[1], SkGetPackedA32(c1)) + MUL(cc[2], SkGetPackedA32(c2)) + MUL(cc[3], SkGetPackedA32(c3)); + SkFixed fr = MUL(cc[0], SkGetPackedR32(c0)) + MUL(cc[1], SkGetPackedR32(c1)) + MUL(cc[2], SkGetPackedR32(c2)) + MUL(cc[3], SkGetPackedR32(c3)); + SkFixed fg = MUL(cc[0], SkGetPackedG32(c0)) + MUL(cc[1], SkGetPackedG32(c1)) + MUL(cc[2], SkGetPackedG32(c2)) + MUL(cc[3], SkGetPackedG32(c3)); + SkFixed fb = MUL(cc[0], SkGetPackedB32(c0)) + MUL(cc[1], SkGetPackedB32(c1)) + MUL(cc[2], SkGetPackedB32(c2)) + MUL(cc[3], SkGetPackedB32(c3)); + + int a = SkClampMax(SkFixedRoundToInt(fa), 255); + int r = SkClampMax(SkFixedRoundToInt(fr), a); + int g = SkClampMax(SkFixedRoundToInt(fg), a); + int b = SkClampMax(SkFixedRoundToInt(fb), a); + + return SkPackARGB32(a, r, g, b); +} + +static float poly_eval(const float cc[4], float t) { + return cc[0] + t * (cc[1] + t * (cc[2] + t * cc[3])); +} + +static void build_coeff4(SkFixed dst[4], float t) { + static const SkScalar coefficients[16] = { + DS( 1.0 / 18.0), DS(-9.0 / 18.0), DS( 15.0 / 18.0), DS( -7.0 / 18.0), + DS(16.0 / 18.0), DS( 0.0 / 18.0), DS(-36.0 / 18.0), DS( 21.0 / 18.0), + DS( 1.0 / 18.0), DS( 9.0 / 18.0), DS( 27.0 / 18.0), DS(-21.0 / 18.0), + DS( 0.0 / 18.0), DS( 0.0 / 18.0), DS( -6.0 / 18.0), DS( 7.0 / 18.0), + }; + + dst[0] = SkFloatToFixed(poly_eval(&coefficients[ 0], t)); + dst[1] = SkFloatToFixed(poly_eval(&coefficients[ 4], t)); + dst[2] = SkFloatToFixed(poly_eval(&coefficients[ 8], t)); + dst[3] = SkFloatToFixed(poly_eval(&coefficients[12], t)); +} + +static SkPMColor doBicubicFilter(const SkBitmap *bm, SkFixed coeffX[4], SkFixed coeffY[4], + int x0, int x1, int x2, int x3, + int y0, int y1, int y2, int y3 ) +{ + SkPMColor s00 = *bm->getAddr32(x0, y0); + SkPMColor s10 = *bm->getAddr32(x1, y0); + SkPMColor s20 = *bm->getAddr32(x2, y0); + SkPMColor s30 = *bm->getAddr32(x3, y0); + SkPMColor s0 = cubicBlend(coeffX, s00, s10, s20, s30); + SkPMColor s01 = *bm->getAddr32(x0, y1); + SkPMColor s11 = *bm->getAddr32(x1, y1); + SkPMColor s21 = *bm->getAddr32(x2, y1); + SkPMColor s31 = *bm->getAddr32(x3, y1); + SkPMColor s1 = cubicBlend(coeffX, s01, s11, s21, s31); + SkPMColor s02 = *bm->getAddr32(x0, y2); + SkPMColor s12 = *bm->getAddr32(x1, y2); + SkPMColor s22 = *bm->getAddr32(x2, y2); + SkPMColor s32 = *bm->getAddr32(x3, y2); + SkPMColor s2 = cubicBlend(coeffX, s02, s12, s22, s32); + SkPMColor s03 = *bm->getAddr32(x0, y3); + SkPMColor s13 = *bm->getAddr32(x1, y3); + SkPMColor s23 = *bm->getAddr32(x2, y3); + SkPMColor s33 = *bm->getAddr32(x3, y3); + SkPMColor s3 = cubicBlend(coeffX, s03, s13, s23, s33); + return cubicBlend(coeffY, s0, s1, s2, s3); +} + +static void bicubicFilter(const SkBitmapProcState& s, int x, int y, + SkPMColor* SK_RESTRICT colors, int count) { + + const int maxX = s.fBitmap->width() - 1; + const int maxY = s.fBitmap->height() - 1; + + while (count-- > 0) { + SkPoint srcPt; + s.fInvProc(*s.fInvMatrix, SkIntToScalar(x), + SkIntToScalar(y), &srcPt); + srcPt.fX -= SK_ScalarHalf; + srcPt.fY -= SK_ScalarHalf; + SkScalar fractx = srcPt.fX - SkScalarFloorToScalar(srcPt.fX); + SkScalar fracty = srcPt.fY - SkScalarFloorToScalar(srcPt.fY); + + SkFixed coeffX[4], coeffY[4]; + build_coeff4(coeffX, fractx); + build_coeff4(coeffY, fracty); + + int sx = SkScalarFloorToInt(srcPt.fX); + int sy = SkScalarFloorToInt(srcPt.fY); + + // Here is where we can support other tile modes (e.g. repeat or mirror) + int x0 = SkClampMax(sx - 1, maxX); + int x1 = SkClampMax(sx , maxX); + int x2 = SkClampMax(sx + 1, maxX); + int x3 = SkClampMax(sx + 2, maxX); + int y0 = SkClampMax(sy - 1, maxY); + int y1 = SkClampMax(sy , maxY); + int y2 = SkClampMax(sy + 1, maxY); + int y3 = SkClampMax(sy + 2, maxY); + + *colors++ = doBicubicFilter( s.fBitmap, coeffX, coeffY, x0, x1, x2, x3, y0, y1, y2, y3 ); + + x++; + } +} + +static void bicubicFilter_ScaleOnly(const SkBitmapProcState &s, int x, int y, + SkPMColor *SK_RESTRICT colors, int count) { + const int maxX = s.fBitmap->width() - 1; + const int maxY = s.fBitmap->height() - 1; + + SkPoint srcPt; + s.fInvProc(*s.fInvMatrix, SkIntToScalar(x), SkIntToScalar(y), &srcPt); + srcPt.fY -= SK_ScalarHalf; + SkScalar fracty = srcPt.fY - SkScalarFloorToScalar(srcPt.fY); + SkFixed coeffX[4], coeffY[4]; + build_coeff4(coeffY, fracty); + int sy = SkScalarFloorToInt(srcPt.fY); + int y0 = SkClampMax(sy - 1, maxY); + int y1 = SkClampMax(sy , maxY); + int y2 = SkClampMax(sy + 1, maxY); + int y3 = SkClampMax(sy + 2, maxY); + + while (count-- > 0) { + s.fInvProc(*s.fInvMatrix, SkIntToScalar(x), SkIntToScalar(y), &srcPt); + srcPt.fX -= SK_ScalarHalf; + SkScalar fractx = srcPt.fX - SkScalarFloorToScalar(srcPt.fX); + + build_coeff4(coeffX, fractx); + + int sx = SkScalarFloorToInt(srcPt.fX); + + // Here is where we can support other tile modes (e.g. repeat or mirror) + int x0 = SkClampMax(sx - 1, maxX); + int x1 = SkClampMax(sx , maxX); + int x2 = SkClampMax(sx + 1, maxX); + int x3 = SkClampMax(sx + 2, maxX); + + *colors++ = doBicubicFilter( s.fBitmap, coeffX, coeffY, x0, x1, x2, x3, y0, y1, y2, y3 ); + + x++; + } +} + +SkBitmapProcState::ShaderProc32 +SkBitmapProcState::chooseBicubicFilterProc(const SkPaint& paint) { + // we need to be requested + uint32_t mask = SkPaint::kFilterBitmap_Flag + | SkPaint::kBicubicFilterBitmap_Flag + ; + if ((paint.getFlags() & mask) != mask) { + return NULL; + } + + // TODO: consider supporting other configs (e.g. 565, A8) + if (fBitmap->config() != SkBitmap::kARGB_8888_Config) { + return NULL; + } + + // TODO: consider supporting repeat and mirror + if (SkShader::kClamp_TileMode != fTileModeX || SkShader::kClamp_TileMode != fTileModeY) { + return NULL; + } + + // TODO: support blending inside our procs + if (0xFF != paint.getAlpha()) { + return NULL; + } + + if (fInvType & SkMatrix::kAffine_Mask) { + return bicubicFilter; + } else if (fInvType & SkMatrix::kScale_Mask) { + return bicubicFilter_ScaleOnly; + } else { + return NULL; + } +} diff --git a/src/core/SkBitmapProcState.cpp b/src/core/SkBitmapProcState.cpp index a2c7f990c2..2a79f2f490 100644 --- a/src/core/SkBitmapProcState.cpp +++ b/src/core/SkBitmapProcState.cpp @@ -302,13 +302,13 @@ bool SkBitmapProcState::chooseProcs(const SkMatrix& inv, const SkPaint& paint) { fShaderProc32 = this->chooseShaderProc32(); } - if (NULL == fShaderProc32) { - fShaderProc32 = this->chooseBitmapFilterProc(paint); - } - // see if our platform has any accelerated overrides this->platformProcs(); + if (NULL == fShaderProc32) { + fShaderProc32 = this->chooseBicubicFilterProc(paint); + } + return true; } diff --git a/src/core/SkBitmapProcState.h b/src/core/SkBitmapProcState.h index 69de2ca86a..7d754fe509 100644 --- a/src/core/SkBitmapProcState.h +++ b/src/core/SkBitmapProcState.h @@ -11,7 +11,6 @@ #define SkBitmapProcState_DEFINED #include "SkBitmap.h" -#include "SkBitmapFilter.h" #include "SkMatrix.h" #define FractionalInt_IS_64BIT @@ -114,8 +113,6 @@ struct SkBitmapProcState { // are ignored ShaderProc32 getShaderProc32() const { return fShaderProc32; } ShaderProc16 getShaderProc16() const { return fShaderProc16; } - - SkBitmapFilter* getBitmapFilter() const { return fBitmapFilter; } #ifdef SK_DEBUG MatrixProc getMatrixProc() const; @@ -142,11 +139,12 @@ private: MatrixProc chooseMatrixProc(bool trivial_matrix); bool chooseProcs(const SkMatrix& inv, const SkPaint&); ShaderProc32 chooseShaderProc32(); - - void buildFilterCoefficients(SkFixed dst[4], float t) const; - SkBitmapFilter *fBitmapFilter; - ShaderProc32 chooseBitmapFilterProc(const SkPaint &paint); + + /** test method for choosing a bicubic shading filter + */ + + ShaderProc32 chooseBicubicFilterProc(const SkPaint &paint); // Return false if we failed to setup for fast translate (e.g. overflow) bool setupForTranslate(); @@ -202,10 +200,4 @@ void ClampX_ClampY_nofilter_affine(const SkBitmapProcState& s, void S32_D16_filter_DX(const SkBitmapProcState& s, const uint32_t* xy, int count, uint16_t* colors); -void highQualityFilter_ScaleOnly(const SkBitmapProcState &s, int x, int y, - SkPMColor *SK_RESTRICT colors, int count); -void highQualityFilter(const SkBitmapProcState &s, int x, int y, - SkPMColor *SK_RESTRICT colors, int count); - - #endif diff --git a/src/opts/SkBitmapFilter_opts_SSE2.cpp b/src/opts/SkBitmapFilter_opts_SSE2.cpp deleted file mode 100644 index 1884f67f52..0000000000 --- a/src/opts/SkBitmapFilter_opts_SSE2.cpp +++ /dev/null @@ -1,189 +0,0 @@ -/* - * Copyright 2013 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#include "SkBitmapProcState.h" -#include "SkBitmap.h" -#include "SkColor.h" -#include "SkColorPriv.h" -#include "SkUnPreMultiply.h" -#include "SkShader.h" - -#include <emmintrin.h> - -#if 0 -static inline void print128i(__m128i value) { - int *v = (int*) &value; - printf("% .11d % .11d % .11d % .11d\n", v[0], v[1], v[2], v[3]); -} - -static inline void print128i_16(__m128i value) { - short *v = (short*) &value; - printf("% .5d % .5d % .5d % .5d % .5d % .5d % .5d % .5d\n", v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]); -} - -static inline void print128i_8(__m128i value) { - unsigned char *v = (unsigned char*) &value; - printf("%.3u %.3u %.3u %.3u %.3u %.3u %.3u %.3u %.3u %.3u %.3u %.3u %.3u %.3u %.3u %.3u\n", - v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], - v[8], v[9], v[10], v[11], v[12], v[13], v[14], v[15] - ); -} - -static inline void print128f(__m128 value) { - float *f = (float*) &value; - printf("%3.4f %3.4f %3.4f %3.4f\n", f[0], f[1], f[2], f[3]); -} -#endif - -// because the border is handled specially, this is guaranteed to have all 16 pixels -// available to it without running off the bitmap's edge. - -int debug_x = 20; -int debug_y = 255; - -void highQualityFilter_SSE2(const SkBitmapProcState& s, int x, int y, - SkPMColor* SK_RESTRICT colors, int count) { - - SkPMColor *orig_colors = colors; - - const int maxX = s.fBitmap->width() - 1; - const int maxY = s.fBitmap->height() - 1; - - while (count-- > 0) { - SkPoint srcPt; - s.fInvProc(*s.fInvMatrix, SkIntToScalar(x), - SkIntToScalar(y), &srcPt); - srcPt.fX -= SK_ScalarHalf; - srcPt.fY -= SK_ScalarHalf; - SkScalar fractx = srcPt.fX - SkScalarFloorToScalar(srcPt.fX); - SkScalar fracty = srcPt.fY - SkScalarFloorToScalar(srcPt.fY); - - int sx = SkScalarFloorToInt(srcPt.fX); - int sy = SkScalarFloorToInt(srcPt.fY); - - __m128 weight = _mm_setzero_ps(); - __m128 accum = _mm_setzero_ps(); - - int y0 = SkTMax(0, int(ceil(sy-s.getBitmapFilter()->width() + 0.5f))); - int y1 = SkTMin(maxY, int(floor(sy+s.getBitmapFilter()->width() + 0.5f))); - int x0 = SkTMax(0, int(ceil(sx-s.getBitmapFilter()->width() + 0.5f))); - int x1 = SkTMin(maxX, int(floor(sx+s.getBitmapFilter()->width() + 0.5f))); - - for (int src_y = y0; src_y <= y1; src_y++) { - float yweight = s.getBitmapFilter()->lookupFloat( (srcPt.fY - src_y) ); - - for (int src_x = x0; src_x <= x1 ; src_x++) { - float xweight = s.getBitmapFilter()->lookupFloat( (srcPt.fX - src_x) ); - - float combined_weight = xweight * yweight; - - SkPMColor color = *s.fBitmap->getAddr32(src_x, src_y); - - __m128i c = _mm_cvtsi32_si128( color ); - c = _mm_unpacklo_epi8(c, _mm_setzero_si128()); - c = _mm_unpacklo_epi16(c, _mm_setzero_si128()); - - __m128 cfloat = _mm_cvtepi32_ps( c ); - - __m128 weightVector = _mm_set1_ps(combined_weight); - - accum = _mm_add_ps(accum, _mm_mul_ps(cfloat, weightVector)); - weight = _mm_add_ps( weight, weightVector ); - } - } - - accum = _mm_div_ps(accum, weight); - accum = _mm_add_ps(accum, _mm_set1_ps(0.5f)); - - __m128i accumInt = _mm_cvtps_epi32( accum ); - - int localResult[4]; - _mm_storeu_si128((__m128i *) (localResult), accumInt); - int a = SkClampMax(localResult[0], 255); - int r = SkClampMax(localResult[1], a); - int g = SkClampMax(localResult[2], a); - int b = SkClampMax(localResult[3], a); - - *colors++ = SkPackARGB32(a, r, g, b); - - x++; - } -} - -void highQualityFilter_ScaleOnly_SSE2(const SkBitmapProcState &s, int x, int y, - SkPMColor *SK_RESTRICT colors, int count) { - SkPMColor *orig_colors = colors; - - const int maxX = s.fBitmap->width() - 1; - const int maxY = s.fBitmap->height() - 1; - - SkPoint srcPt; - s.fInvProc(*s.fInvMatrix, SkIntToScalar(x), - SkIntToScalar(y), &srcPt); - srcPt.fY -= SK_ScalarHalf; - int sy = SkScalarFloorToInt(srcPt.fY); - - SkScalar fracty = srcPt.fY - SkScalarFloorToScalar(srcPt.fY); - - int y0 = SkTMax(0, int(ceil(sy-s.getBitmapFilter()->width() + 0.5f))); - int y1 = SkTMin(maxY, int(floor(sy+s.getBitmapFilter()->width() + 0.5f))); - - while (count-- > 0) { - srcPt.fX -= SK_ScalarHalf; - srcPt.fY -= SK_ScalarHalf; - SkScalar fractx = srcPt.fX - SkScalarFloorToScalar(srcPt.fX); - - int sx = SkScalarFloorToInt(srcPt.fX); - - float weight = 0; - __m128 accum = _mm_setzero_ps(); - - int x0 = SkTMax(0, int(ceil(sx-s.getBitmapFilter()->width() + 0.5f))); - int x1 = SkTMin(maxX, int(floor(sx+s.getBitmapFilter()->width() + 0.5f))); - - for (int src_y = y0; src_y <= y1; src_y++) { - float yweight = s.getBitmapFilter()->lookupFloat( (srcPt.fY - src_y) ); - - for (int src_x = x0; src_x <= x1 ; src_x++) { - float xweight = s.getBitmapFilter()->lookupFloat( (srcPt.fX - src_x) ); - - float combined_weight = xweight * yweight; - - SkPMColor color = *s.fBitmap->getAddr32(src_x, src_y); - - __m128 c = _mm_set_ps(SkGetPackedB32(color), - SkGetPackedG32(color), - SkGetPackedR32(color), - SkGetPackedA32(color)); - - __m128 weightVector = _mm_set1_ps(combined_weight); - - accum = _mm_add_ps(accum, _mm_mul_ps(c, weightVector)); - weight += combined_weight; - } - } - - __m128 totalWeightVector = _mm_set1_ps(weight); - accum = _mm_div_ps(accum, totalWeightVector); - accum = _mm_add_ps(accum, _mm_set1_ps(0.5f)); - - float localResult[4]; - _mm_storeu_ps(localResult, accum); - int a = SkClampMax(int(localResult[0]), 255); - int r = SkClampMax(int(localResult[1]), a); - int g = SkClampMax(int(localResult[2]), a); - int b = SkClampMax(int(localResult[3]), a); - - *colors++ = SkPackARGB32(a, r, g, b); - - x++; - - s.fInvProc(*s.fInvMatrix, SkIntToScalar(x), - SkIntToScalar(y), &srcPt); - - } -} diff --git a/src/opts/SkBitmapFilter_opts_SSE2.h b/src/opts/SkBitmapFilter_opts_SSE2.h deleted file mode 100644 index 974b00896c..0000000000 --- a/src/opts/SkBitmapFilter_opts_SSE2.h +++ /dev/null @@ -1,20 +0,0 @@ - -/* - * Copyright 2013 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - - -#ifndef SkBitmapFilter_opts_sse2_DEFINED -#define SkBitmapFilter_opts_sse2_DEFINED - -#include "SkBitmapProcState.h" - -void highQualityFilter_ScaleOnly_SSE2(const SkBitmapProcState &s, int x, int y, - SkPMColor *SK_RESTRICT colors, int count); -void highQualityFilter_SSE2(const SkBitmapProcState &s, int x, int y, - SkPMColor *SK_RESTRICT colors, int count); - -#endif diff --git a/src/opts/opts_check_SSE2.cpp b/src/opts/opts_check_SSE2.cpp index 37ce9036ca..d23e3844d5 100644 --- a/src/opts/opts_check_SSE2.cpp +++ b/src/opts/opts_check_SSE2.cpp @@ -7,7 +7,6 @@ #include "SkBitmapProcState_opts_SSE2.h" #include "SkBitmapProcState_opts_SSSE3.h" -#include "SkBitmapFilter_opts_SSE2.h" #include "SkBlitMask.h" #include "SkBlitRow.h" #include "SkBlitRect_opts_SSE2.h" @@ -15,8 +14,6 @@ #include "SkUtils_opts_SSE2.h" #include "SkUtils.h" -#include "SkRTConf.h" - #if defined(_MSC_VER) && defined(_WIN64) #include <intrin.h> #endif @@ -105,8 +102,6 @@ static bool cachedHasSSSE3() { return gHasSSSE3; } -SK_CONF_DECLARE( bool, c_hqfilter_sse, "bitmap.filter.highQualitySSE", false, "Use SSE optimized version of high quality image filters"); - void SkBitmapProcState::platformProcs() { if (cachedHasSSSE3()) { #if !defined(SK_BUILD_FOR_ANDROID) @@ -147,14 +142,6 @@ void SkBitmapProcState::platformProcs() { } else if (fMatrixProc == ClampX_ClampY_nofilter_affine) { fMatrixProc = ClampX_ClampY_nofilter_affine_SSE2; } - if (c_hqfilter_sse) { - if (fShaderProc32 == highQualityFilter) { - fShaderProc32 = highQualityFilter_SSE2; - } - if (fShaderProc32 == highQualityFilter_ScaleOnly) { - fShaderProc32 = highQualityFilter_ScaleOnly_SSE2; - } - } } } |