diff options
author | 2013-10-30 21:57:04 +0000 | |
---|---|---|
committer | 2013-10-30 21:57:04 +0000 | |
commit | 7a47ad3bacb36ca7609490bd7cbd85d2c49a0042 (patch) | |
tree | 375a48616970b64e1818808ff449c543af8aae84 | |
parent | 2d2e5c4e2425a1cb64d31d7d10832f4a3a9137a4 (diff) |
Implement SSE2-based implementations of the morphology filters (dilate & erode). This gives a 3-5X speedup over the naive implementation, and also mitigates a timing-based security attack in Chrome (https://code.google.com/p/chromium/issues/detail?id=251711).
NOTE: this will require a corresponding GYP change on the Skia roll into Chrome: https://codereview.chromium.org/52453004/
R=mtklein@google.com, reed@google.com
Review URL: https://codereview.chromium.org/52603004
git-svn-id: http://skia.googlecode.com/svn/trunk@12038 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r-- | gyp/effects.gyp | 1 | ||||
-rw-r--r-- | gyp/opts.gyp | 2 | ||||
-rw-r--r-- | src/effects/SkMorphologyImageFilter.cpp | 61 | ||||
-rw-r--r-- | src/opts/SkMorphology_opts.h | 27 | ||||
-rw-r--r-- | src/opts/SkMorphology_opts_SSE2.cpp | 78 | ||||
-rw-r--r-- | src/opts/SkMorphology_opts_SSE2.h | 15 | ||||
-rw-r--r-- | src/opts/SkMorphology_opts_none.cpp | 12 | ||||
-rw-r--r-- | src/opts/opts_check_SSE2.cpp | 20 |
8 files changed, 200 insertions, 16 deletions
diff --git a/gyp/effects.gyp b/gyp/effects.gyp index d22debb757..7b256d31eb 100644 --- a/gyp/effects.gyp +++ b/gyp/effects.gyp @@ -17,6 +17,7 @@ 'include_dirs': [ '../include/effects', '../src/effects', + '../src/opts', '../src/core', ], 'direct_dependent_settings': { diff --git a/gyp/opts.gyp b/gyp/opts.gyp index d9cd6f29ba..383c3c9644 100644 --- a/gyp/opts.gyp +++ b/gyp/opts.gyp @@ -51,6 +51,7 @@ '../src/opts/SkBitmapFilter_opts_SSE2.cpp', '../src/opts/SkBlitRow_opts_SSE2.cpp', '../src/opts/SkBlitRect_opts_SSE2.cpp', + '../src/opts/SkMorphology_opts_SSE2.cpp', '../src/opts/SkUtils_opts_SSE2.cpp', '../src/opts/SkXfermode_opts_none.cpp', ], @@ -100,6 +101,7 @@ '../src/opts/SkBitmapProcState_opts_none.cpp', '../src/opts/SkBlitMask_opts_none.cpp', '../src/opts/SkBlitRow_opts_none.cpp', + '../src/opts/SkMorphology_opts_none.cpp', '../src/opts/SkUtils_opts_none.cpp', '../src/opts/SkXfermode_opts_none.cpp', ], diff --git a/src/effects/SkMorphologyImageFilter.cpp b/src/effects/SkMorphologyImageFilter.cpp index 8705caa7d2..52c01fe0d6 100644 --- a/src/effects/SkMorphologyImageFilter.cpp +++ b/src/effects/SkMorphologyImageFilter.cpp @@ -10,6 +10,7 @@ #include "SkColorPriv.h" #include "SkFlattenableBuffers.h" #include "SkRect.h" +#include "SkMorphology_opts.h" #if SK_SUPPORT_GPU #include "GrContext.h" #include "GrTexture.h" @@ -38,11 +39,19 @@ void SkMorphologyImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { buffer.writeInt(fRadius.fHeight); } +enum MorphDirection { + kX, kY +}; + +template<MorphDirection direction> static void erode(const SkPMColor* src, SkPMColor* dst, int radius, int width, int height, - int srcStrideX, int srcStrideY, - int dstStrideX, int dstStrideY) + int srcStride, int dstStride) { + const int srcStrideX = direction == kX ? 1 : srcStride; + const int dstStrideX = direction == kX ? 1 : dstStride; + const int srcStrideY = direction == kX ? srcStride : 1; + const int dstStrideY = direction == kX ? dstStride : 1; radius = SkMin32(radius, width - 1); const SkPMColor* upperSrc = src + radius * srcStrideX; for (int x = 0; x < width; ++x) { @@ -74,23 +83,35 @@ static void erode(const SkPMColor* src, SkPMColor* dst, static void erodeX(const SkBitmap& src, SkBitmap* dst, int radiusX, const SkIRect& bounds) { - erode(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0), - radiusX, bounds.width(), bounds.height(), - 1, src.rowBytesAsPixels(), 1, dst->rowBytesAsPixels()); + SkMorphologyProc erodeXProc = SkMorphologyGetPlatformProc(kErodeX_SkMorphologyProcType); + if (!erodeXProc) { + erodeXProc = erode<kX>; + } + erodeXProc(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0), + radiusX, bounds.width(), bounds.height(), + src.rowBytesAsPixels(), dst->rowBytesAsPixels()); } static void erodeY(const SkBitmap& src, SkBitmap* dst, int radiusY, const SkIRect& bounds) { - erode(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0), - radiusY, bounds.height(), bounds.width(), - src.rowBytesAsPixels(), 1, dst->rowBytesAsPixels(), 1); + SkMorphologyProc erodeYProc = SkMorphologyGetPlatformProc(kErodeY_SkMorphologyProcType); + if (!erodeYProc) { + erodeYProc = erode<kY>; + } + erodeYProc(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0), + radiusY, bounds.height(), bounds.width(), + src.rowBytesAsPixels(), dst->rowBytesAsPixels()); } +template<MorphDirection direction> static void dilate(const SkPMColor* src, SkPMColor* dst, int radius, int width, int height, - int srcStrideX, int srcStrideY, - int dstStrideX, int dstStrideY) + int srcStride, int dstStride) { + const int srcStrideX = direction == kX ? 1 : srcStride; + const int dstStrideX = direction == kX ? 1 : dstStride; + const int srcStrideY = direction == kX ? srcStride : 1; + const int dstStrideY = direction == kX ? dstStride : 1; radius = SkMin32(radius, width - 1); const SkPMColor* upperSrc = src + radius * srcStrideX; for (int x = 0; x < width; ++x) { @@ -122,16 +143,24 @@ static void dilate(const SkPMColor* src, SkPMColor* dst, static void dilateX(const SkBitmap& src, SkBitmap* dst, int radiusX, const SkIRect& bounds) { - dilate(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0), - radiusX, bounds.width(), bounds.height(), - 1, src.rowBytesAsPixels(), 1, dst->rowBytesAsPixels()); + SkMorphologyProc dilateXProc = SkMorphologyGetPlatformProc(kDilateX_SkMorphologyProcType); + if (!dilateXProc) { + dilateXProc = dilate<kX>; + } + dilateXProc(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0), + radiusX, bounds.width(), bounds.height(), + src.rowBytesAsPixels(), dst->rowBytesAsPixels()); } static void dilateY(const SkBitmap& src, SkBitmap* dst, int radiusY, const SkIRect& bounds) { - dilate(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0), - radiusY, bounds.height(), bounds.width(), - src.rowBytesAsPixels(), 1, dst->rowBytesAsPixels(), 1); + SkMorphologyProc dilateYProc = SkMorphologyGetPlatformProc(kDilateY_SkMorphologyProcType); + if (!dilateYProc) { + dilateYProc = dilate<kY>; + } + dilateYProc(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0), + radiusY, bounds.height(), bounds.width(), + src.rowBytesAsPixels(), dst->rowBytesAsPixels()); } bool SkErodeImageFilter::onFilterImage(Proxy* proxy, diff --git a/src/opts/SkMorphology_opts.h b/src/opts/SkMorphology_opts.h new file mode 100644 index 0000000000..e3ad853cf6 --- /dev/null +++ b/src/opts/SkMorphology_opts.h @@ -0,0 +1,27 @@ +/* + * Copyright 2013 The Android Open Source Project + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <SkColor.h> + +/** + * All morphology procs have the same signature: src is the source buffer, dst the + * destination buffer, radius is the morphology radius, width and height are the bounds + * of the destination buffer (in pixels), and srcStride and dstStride are the + * number of pixels per row in each buffer. All buffers are 8888. + */ + +typedef void (*SkMorphologyProc)(const SkPMColor* src, SkPMColor* dst, int radius, + int width, int height, int srcStride, int dstStride); + +enum SkMorphologyProcType { + kDilateX_SkMorphologyProcType, + kDilateY_SkMorphologyProcType, + kErodeX_SkMorphologyProcType, + kErodeY_SkMorphologyProcType +}; + +SkMorphologyProc SkMorphologyGetPlatformProc(SkMorphologyProcType type); diff --git a/src/opts/SkMorphology_opts_SSE2.cpp b/src/opts/SkMorphology_opts_SSE2.cpp new file mode 100644 index 0000000000..6314335653 --- /dev/null +++ b/src/opts/SkMorphology_opts_SSE2.cpp @@ -0,0 +1,78 @@ +/* + * Copyright 2013 The Android Open Source Project + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + + +#include "SkColorPriv.h" + +#include <emmintrin.h> + +/* SSE2 version of dilateX, dilateY, erodeX, erodeY. + * portable versions are in src/effects/SkMorphologyImageFilter.cpp. + */ + +enum MorphType { + kDilate, kErode +}; + +enum MorphDirection { + kX, kY +}; + +template<MorphType type, MorphDirection direction> +static void SkMorph_SSE2(const SkPMColor* src, SkPMColor* dst, int radius, + int width, int height, int srcStride, int dstStride) +{ + const int srcStrideX = direction == kX ? 1 : srcStride; + const int dstStrideX = direction == kX ? 1 : dstStride; + const int srcStrideY = direction == kX ? srcStride : 1; + const int dstStrideY = direction == kX ? dstStride : 1; + radius = SkMin32(radius, width - 1); + const SkPMColor* upperSrc = src + radius * srcStrideX; + for (int x = 0; x < width; ++x) { + const SkPMColor* lp = src; + const SkPMColor* up = upperSrc; + SkPMColor* dptr = dst; + for (int y = 0; y < height; ++y) { + __m128i max = type == kDilate ? _mm_setzero_si128() : _mm_set1_epi32(0xFFFFFFFF); + for (const SkPMColor* p = lp; p <= up; p += srcStrideX) { + __m128i src_pixel = _mm_cvtsi32_si128(*p); + max = type == kDilate ? _mm_max_epu8(src_pixel, max) : _mm_min_epu8(src_pixel, max); + } + *dptr = _mm_cvtsi128_si32(max); + dptr += dstStrideY; + lp += srcStrideY; + up += srcStrideY; + } + if (x >= radius) src += srcStrideX; + if (x + radius < width - 1) upperSrc += srcStrideX; + dst += dstStrideX; + } +} + +void SkDilateX_SSE2(const SkPMColor* src, SkPMColor* dst, int radius, + int width, int height, int srcStride, int dstStride) +{ + SkMorph_SSE2<kDilate, kX>(src, dst, radius, width, height, srcStride, dstStride); +} + +void SkErodeX_SSE2(const SkPMColor* src, SkPMColor* dst, int radius, + int width, int height, int srcStride, int dstStride) +{ + SkMorph_SSE2<kErode, kX>(src, dst, radius, width, height, srcStride, dstStride); +} + +void SkDilateY_SSE2(const SkPMColor* src, SkPMColor* dst, int radius, + int width, int height, int srcStride, int dstStride) +{ + SkMorph_SSE2<kDilate, kY>(src, dst, radius, width, height, srcStride, dstStride); +} + +void SkErodeY_SSE2(const SkPMColor* src, SkPMColor* dst, int radius, + int width, int height, int srcStride, int dstStride) +{ + SkMorph_SSE2<kErode, kY>(src, dst, radius, width, height, srcStride, dstStride); +} diff --git a/src/opts/SkMorphology_opts_SSE2.h b/src/opts/SkMorphology_opts_SSE2.h new file mode 100644 index 0000000000..bd103e6eba --- /dev/null +++ b/src/opts/SkMorphology_opts_SSE2.h @@ -0,0 +1,15 @@ +/* + * Copyright 2013 The Android Open Source Project + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +void SkDilateX_SSE2(const SkPMColor* src, SkPMColor* dst, int radius, + int width, int height, int srcStride, int dstStride); +void SkDilateY_SSE2(const SkPMColor* src, SkPMColor* dst, int radius, + int width, int height, int srcStride, int dstStride); +void SkErodeX_SSE2(const SkPMColor* src, SkPMColor* dst, int radius, + int width, int height, int srcStride, int dstStride); +void SkErodeY_SSE2(const SkPMColor* src, SkPMColor* dst, int radius, + int width, int height, int srcStride, int dstStride); diff --git a/src/opts/SkMorphology_opts_none.cpp b/src/opts/SkMorphology_opts_none.cpp new file mode 100644 index 0000000000..66d58ba571 --- /dev/null +++ b/src/opts/SkMorphology_opts_none.cpp @@ -0,0 +1,12 @@ +/* + * Copyright 2013 The Android Open Source Project + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkMorphology_opts.h" + +SkMorphologyProc SkMorphologyGetPlatformProc(SkMorphologyProcType) { + return NULL; +} diff --git a/src/opts/opts_check_SSE2.cpp b/src/opts/opts_check_SSE2.cpp index 8f0bdac8fd..b40ca9d4b8 100644 --- a/src/opts/opts_check_SSE2.cpp +++ b/src/opts/opts_check_SSE2.cpp @@ -14,6 +14,8 @@ #include "SkBlitRow_opts_SSE2.h" #include "SkUtils_opts_SSE2.h" #include "SkUtils.h" +#include "SkMorphology_opts.h" +#include "SkMorphology_opts_SSE2.h" #include "SkRTConf.h" @@ -248,6 +250,24 @@ SkMemset32Proc SkMemset32GetPlatformProc() { } } +SkMorphologyProc SkMorphologyGetPlatformProc(SkMorphologyProcType type) { + if (!cachedHasSSE2()) { + return NULL; + } + switch (type) { + case kDilateX_SkMorphologyProcType: + return SkDilateX_SSE2; + case kDilateY_SkMorphologyProcType: + return SkDilateY_SSE2; + case kErodeX_SkMorphologyProcType: + return SkErodeX_SSE2; + case kErodeY_SkMorphologyProcType: + return SkErodeY_SSE2; + default: + return NULL; + } +} + SkBlitRow::ColorRectProc PlatformColorRectProcFactory(); // suppress warning SkBlitRow::ColorRectProc PlatformColorRectProcFactory() { |