Add SSE4 version of BlurImage optimizations.

Adds an SSE4.1 version of the existing BlurImage optimizations.
Performance of blur_image_filter_* benchmarks show a 10-50%
improvement on Linux/Ubuntu Core i7.

Signed-off-by: Henrik Smiding <henrik.smiding@intel.com>

R=mtklein@google.com

Author: henrik.smiding@intel.com

Review URL: https://codereview.chromium.org/366593004
This commit is contained in:
henrik.smiding 2014-07-04 04:23:17 -07:00 committed by Commit bot
parent 3df05015ef
commit 2830632ce9
5 changed files with 175 additions and 17 deletions

View File

@ -212,22 +212,39 @@
'../src/core',
],
'conditions': [
[ 'skia_os in ["linux", "freebsd", "openbsd", "solaris", "nacl", "chromeos", "android", "mac"] \
[ 'skia_os in ["linux", "freebsd", "openbsd", "solaris", "nacl", "chromeos", "android"] \
and not skia_android_framework', {
'cflags': [
'-msse4',
],
}],
[ 'skia_arch_width == 64 and skia_arch_type == "x86"', {
[ 'skia_os == "mac"', {
'xcode_settings': {
'OTHER_CPLUSPLUSFLAGS!': [
'-mssse3',
],
'OTHER_CPLUSPLUSFLAGS': [
'-msse4',
],
},
}],
[ 'skia_arch_type == "x86"', {
'sources': [
'../src/opts/SkBlitRow_opts_SSE4_x64_asm.S',
'../src/opts/SkBlurImage_opts_SSE4.cpp',
],
'conditions': [
[ 'skia_arch_width == 64', {
'sources': [
'../src/opts/SkBlitRow_opts_SSE4_x64_asm.S',
],
}],
[ 'skia_arch_width == 32', {
'sources': [
'../src/opts/SkBlitRow_opts_SSE4_asm.S',
],
}],
],
}],
[ 'skia_arch_width == 32 and skia_arch_type == "x86"', {
'sources': [
'../src/opts/SkBlitRow_opts_SSE4_asm.S',
],
}],
],
},
# NEON code must be compiled with -mfpu=neon which also affects scalar

View File

@ -55,17 +55,13 @@ void SkBoxBlur_SSE2(const SkPMColor* src, int srcStride, SkPMColor* dst, int ker
const SkPMColor* sptr = src;
SkColor* dptr = dst;
for (int x = 0; x < width; ++x) {
#if 0
// In SSE4.1, this would be
__m128i result = _mm_mullo_epi32(sum, scale);
#else
// But SSE2 has no PMULLUD, so we must do AG and RB separately.
// SSE2 has no PMULLUD, so we must do AG and RB separately.
__m128i tmp1 = _mm_mul_epu32(sum, scale);
__m128i tmp2 = _mm_mul_epu32(_mm_srli_si128(sum, 4),
_mm_srli_si128(scale, 4));
__m128i result = _mm_unpacklo_epi32(_mm_shuffle_epi32(tmp1, _MM_SHUFFLE(0,0,2,0)),
_mm_shuffle_epi32(tmp2, _MM_SHUFFLE(0,0,2,0)));
#endif
// sumA*scale+.5 sumB*scale+.5 sumG*scale+.5 sumB*scale+.5
result = _mm_add_epi32(result, half);

View File

@ -0,0 +1,123 @@
/*
* Copyright 2014 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 "SkBitmap.h"
#include "SkBlurImage_opts_SSE4.h"
#include "SkColorPriv.h"
#include "SkRect.h"
/* With the exception of the Android framework we always build the SSE4 functions
* and enable the caller to determine SSE4 support. However, for the Android framework,
* if the device does not support SSE4x then the compiler will not supply the required
* -msse4* option needed to build this file, so instead we provide a stub implementation.
*/
#if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) || SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
#include <smmintrin.h>
namespace {
enum BlurDirection {
kX, kY
};
/* Helper function to spread the components of a 32-bit integer into the
* lower 8 bits of each 32-bit element of an SSE register.
*/
inline __m128i expand(int a) {
const __m128i zero = _mm_setzero_si128();
// 0 0 0 0 0 0 0 0 0 0 0 0 A R G B
__m128i result = _mm_cvtsi32_si128(a);
// 0 0 0 0 0 0 0 0 0 A 0 R 0 G 0 B
result = _mm_unpacklo_epi8(result, zero);
// 0 0 0 A 0 0 0 R 0 0 0 G 0 0 0 B
return _mm_unpacklo_epi16(result, zero);
}
template<BlurDirection srcDirection, BlurDirection dstDirection>
void SkBoxBlur_SSE4(const SkPMColor* src, int srcStride, SkPMColor* dst, int kernelSize,
int leftOffset, int rightOffset, int width, int height)
{
const int rightBorder = SkMin32(rightOffset + 1, width);
const int srcStrideX = srcDirection == kX ? 1 : srcStride;
const int dstStrideX = dstDirection == kX ? 1 : height;
const int srcStrideY = srcDirection == kX ? srcStride : 1;
const int dstStrideY = dstDirection == kX ? width : 1;
const __m128i scale = _mm_set1_epi32((1 << 24) / kernelSize);
const __m128i half = _mm_set1_epi32(1 << 23);
const __m128i zero = _mm_setzero_si128();
for (int y = 0; y < height; ++y) {
__m128i sum = zero;
const SkPMColor* p = src;
for (int i = 0; i < rightBorder; ++i) {
sum = _mm_add_epi32(sum, expand(*p));
p += srcStrideX;
}
const SkPMColor* sptr = src;
SkColor* dptr = dst;
for (int x = 0; x < width; ++x) {
__m128i result = _mm_mullo_epi32(sum, scale);
// sumA*scale+.5 sumB*scale+.5 sumG*scale+.5 sumB*scale+.5
result = _mm_add_epi32(result, half);
// 0 0 0 A 0 0 0 R 0 0 0 G 0 0 0 B
result = _mm_srli_epi32(result, 24);
// 0 0 0 0 0 0 0 0 0 A 0 R 0 G 0 B
result = _mm_packs_epi32(result, zero);
// 0 0 0 0 0 0 0 0 0 0 0 0 A R G B
result = _mm_packus_epi16(result, zero);
*dptr = _mm_cvtsi128_si32(result);
if (x >= leftOffset) {
SkColor l = *(sptr - leftOffset * srcStrideX);
sum = _mm_sub_epi32(sum, expand(l));
}
if (x + rightOffset + 1 < width) {
SkColor r = *(sptr + (rightOffset + 1) * srcStrideX);
sum = _mm_add_epi32(sum, expand(r));
}
sptr += srcStrideX;
if (srcDirection == kY) {
_mm_prefetch(reinterpret_cast<const char*>(sptr + (rightOffset + 1) * srcStrideX),
_MM_HINT_T0);
}
dptr += dstStrideX;
}
src += srcStrideY;
dst += dstStrideY;
}
}
} // namespace
bool SkBoxBlurGetPlatformProcs_SSE4(SkBoxBlurProc* boxBlurX,
SkBoxBlurProc* boxBlurY,
SkBoxBlurProc* boxBlurXY,
SkBoxBlurProc* boxBlurYX) {
*boxBlurX = SkBoxBlur_SSE4<kX, kX>;
*boxBlurY = SkBoxBlur_SSE4<kY, kY>;
*boxBlurXY = SkBoxBlur_SSE4<kX, kY>;
*boxBlurYX = SkBoxBlur_SSE4<kY, kX>;
return true;
}
#else // !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) || SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
bool SkBoxBlurGetPlatformProcs_SSE4(SkBoxBlurProc* boxBlurX,
SkBoxBlurProc* boxBlurY,
SkBoxBlurProc* boxBlurXY,
SkBoxBlurProc* boxBlurYX) {
sk_throw();
}
#endif

View File

@ -0,0 +1,18 @@
/*
* Copyright 2014 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.
*/
#ifndef SkBlurImage_opts_SSE4_DEFINED
#define SkBlurImage_opts_SSE4_DEFINED
#include "SkBlurImage_opts.h"
bool SkBoxBlurGetPlatformProcs_SSE4(SkBoxBlurProc* boxBlurX,
SkBoxBlurProc* boxBlurY,
SkBoxBlurProc* boxBlurXY,
SkBoxBlurProc* boxBlurYX);
#endif

View File

@ -15,6 +15,7 @@
#include "SkBlitRow_opts_SSE2.h"
#include "SkBlitRow_opts_SSE4.h"
#include "SkBlurImage_opts_SSE2.h"
#include "SkBlurImage_opts_SSE4.h"
#include "SkMorphology_opts.h"
#include "SkMorphology_opts_SSE2.h"
#include "SkRTConf.h"
@ -358,10 +359,13 @@ bool SkBoxBlurGetPlatformProcs(SkBoxBlurProc* boxBlurX,
#ifdef SK_DISABLE_BLUR_DIVISION_OPTIMIZATION
return false;
#else
if (!supports_simd(SK_CPU_SSE_LEVEL_SSE2)) {
return false;
if (supports_simd(SK_CPU_SSE_LEVEL_SSE41)) {
return SkBoxBlurGetPlatformProcs_SSE4(boxBlurX, boxBlurY, boxBlurXY, boxBlurYX);
}
return SkBoxBlurGetPlatformProcs_SSE2(boxBlurX, boxBlurY, boxBlurXY, boxBlurYX);
else if (supports_simd(SK_CPU_SSE_LEVEL_SSE2)) {
return SkBoxBlurGetPlatformProcs_SSE2(boxBlurX, boxBlurY, boxBlurXY, boxBlurYX);
}
return false;
#endif
}