michael@0: michael@0: /* michael@0: * Copyright 2009 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #include michael@0: #include "SkBitmapProcState_opts_SSE2.h" michael@0: #include "SkPaint.h" michael@0: #include "SkUtils.h" michael@0: michael@0: void S32_opaque_D32_filter_DX_SSE2(const SkBitmapProcState& s, michael@0: const uint32_t* xy, michael@0: int count, uint32_t* colors) { michael@0: SkASSERT(count > 0 && colors != NULL); michael@0: SkASSERT(s.fFilterLevel != SkPaint::kNone_FilterLevel); michael@0: SkASSERT(s.fBitmap->config() == SkBitmap::kARGB_8888_Config); michael@0: SkASSERT(s.fAlphaScale == 256); michael@0: michael@0: const char* srcAddr = static_cast(s.fBitmap->getPixels()); michael@0: size_t rb = s.fBitmap->rowBytes(); michael@0: uint32_t XY = *xy++; michael@0: unsigned y0 = XY >> 14; michael@0: const uint32_t* row0 = reinterpret_cast(srcAddr + (y0 >> 4) * rb); michael@0: const uint32_t* row1 = reinterpret_cast(srcAddr + (XY & 0x3FFF) * rb); michael@0: unsigned subY = y0 & 0xF; michael@0: michael@0: // ( 0, 0, 0, 0, 0, 0, 0, 16) michael@0: __m128i sixteen = _mm_cvtsi32_si128(16); michael@0: michael@0: // ( 0, 0, 0, 0, 16, 16, 16, 16) michael@0: sixteen = _mm_shufflelo_epi16(sixteen, 0); michael@0: michael@0: // ( 0, 0, 0, 0, 0, 0, 0, y) michael@0: __m128i allY = _mm_cvtsi32_si128(subY); michael@0: michael@0: // ( 0, 0, 0, 0, y, y, y, y) michael@0: allY = _mm_shufflelo_epi16(allY, 0); michael@0: michael@0: // ( 0, 0, 0, 0, 16-y, 16-y, 16-y, 16-y) michael@0: __m128i negY = _mm_sub_epi16(sixteen, allY); michael@0: michael@0: // (16-y, 16-y, 16-y, 16-y, y, y, y, y) michael@0: allY = _mm_unpacklo_epi64(allY, negY); michael@0: michael@0: // (16, 16, 16, 16, 16, 16, 16, 16 ) michael@0: sixteen = _mm_shuffle_epi32(sixteen, 0); michael@0: michael@0: // ( 0, 0, 0, 0, 0, 0, 0, 0) michael@0: __m128i zero = _mm_setzero_si128(); michael@0: do { michael@0: uint32_t XX = *xy++; // x0:14 | 4 | x1:14 michael@0: unsigned x0 = XX >> 18; michael@0: unsigned x1 = XX & 0x3FFF; michael@0: michael@0: // (0, 0, 0, 0, 0, 0, 0, x) michael@0: __m128i allX = _mm_cvtsi32_si128((XX >> 14) & 0x0F); michael@0: michael@0: // (0, 0, 0, 0, x, x, x, x) michael@0: allX = _mm_shufflelo_epi16(allX, 0); michael@0: michael@0: // (x, x, x, x, x, x, x, x) michael@0: allX = _mm_shuffle_epi32(allX, 0); michael@0: michael@0: // (16-x, 16-x, 16-x, 16-x, 16-x, 16-x, 16-x) michael@0: __m128i negX = _mm_sub_epi16(sixteen, allX); michael@0: michael@0: // Load 4 samples (pixels). michael@0: __m128i a00 = _mm_cvtsi32_si128(row0[x0]); michael@0: __m128i a01 = _mm_cvtsi32_si128(row0[x1]); michael@0: __m128i a10 = _mm_cvtsi32_si128(row1[x0]); michael@0: __m128i a11 = _mm_cvtsi32_si128(row1[x1]); michael@0: michael@0: // (0, 0, a00, a10) michael@0: __m128i a00a10 = _mm_unpacklo_epi32(a10, a00); michael@0: michael@0: // Expand to 16 bits per component. michael@0: a00a10 = _mm_unpacklo_epi8(a00a10, zero); michael@0: michael@0: // ((a00 * (16-y)), (a10 * y)). michael@0: a00a10 = _mm_mullo_epi16(a00a10, allY); michael@0: michael@0: // (a00 * (16-y) * (16-x), a10 * y * (16-x)). michael@0: a00a10 = _mm_mullo_epi16(a00a10, negX); michael@0: michael@0: // (0, 0, a01, a10) michael@0: __m128i a01a11 = _mm_unpacklo_epi32(a11, a01); michael@0: michael@0: // Expand to 16 bits per component. michael@0: a01a11 = _mm_unpacklo_epi8(a01a11, zero); michael@0: michael@0: // (a01 * (16-y)), (a11 * y) michael@0: a01a11 = _mm_mullo_epi16(a01a11, allY); michael@0: michael@0: // (a01 * (16-y) * x), (a11 * y * x) michael@0: a01a11 = _mm_mullo_epi16(a01a11, allX); michael@0: michael@0: // (a00*w00 + a01*w01, a10*w10 + a11*w11) michael@0: __m128i sum = _mm_add_epi16(a00a10, a01a11); michael@0: michael@0: // (DC, a00*w00 + a01*w01) michael@0: __m128i shifted = _mm_shuffle_epi32(sum, 0xEE); michael@0: michael@0: // (DC, a00*w00 + a01*w01 + a10*w10 + a11*w11) michael@0: sum = _mm_add_epi16(sum, shifted); michael@0: michael@0: // Divide each 16 bit component by 256. michael@0: sum = _mm_srli_epi16(sum, 8); michael@0: michael@0: // Pack lower 4 16 bit values of sum into lower 4 bytes. michael@0: sum = _mm_packus_epi16(sum, zero); michael@0: michael@0: // Extract low int and store. michael@0: *colors++ = _mm_cvtsi128_si32(sum); michael@0: } while (--count > 0); michael@0: } michael@0: michael@0: void S32_alpha_D32_filter_DX_SSE2(const SkBitmapProcState& s, michael@0: const uint32_t* xy, michael@0: int count, uint32_t* colors) { michael@0: SkASSERT(count > 0 && colors != NULL); michael@0: SkASSERT(s.fFilterLevel != SkPaint::kNone_FilterLevel); michael@0: SkASSERT(s.fBitmap->config() == SkBitmap::kARGB_8888_Config); michael@0: SkASSERT(s.fAlphaScale < 256); michael@0: michael@0: const char* srcAddr = static_cast(s.fBitmap->getPixels()); michael@0: size_t rb = s.fBitmap->rowBytes(); michael@0: uint32_t XY = *xy++; michael@0: unsigned y0 = XY >> 14; michael@0: const uint32_t* row0 = reinterpret_cast(srcAddr + (y0 >> 4) * rb); michael@0: const uint32_t* row1 = reinterpret_cast(srcAddr + (XY & 0x3FFF) * rb); michael@0: unsigned subY = y0 & 0xF; michael@0: michael@0: // ( 0, 0, 0, 0, 0, 0, 0, 16) michael@0: __m128i sixteen = _mm_cvtsi32_si128(16); michael@0: michael@0: // ( 0, 0, 0, 0, 16, 16, 16, 16) michael@0: sixteen = _mm_shufflelo_epi16(sixteen, 0); michael@0: michael@0: // ( 0, 0, 0, 0, 0, 0, 0, y) michael@0: __m128i allY = _mm_cvtsi32_si128(subY); michael@0: michael@0: // ( 0, 0, 0, 0, y, y, y, y) michael@0: allY = _mm_shufflelo_epi16(allY, 0); michael@0: michael@0: // ( 0, 0, 0, 0, 16-y, 16-y, 16-y, 16-y) michael@0: __m128i negY = _mm_sub_epi16(sixteen, allY); michael@0: michael@0: // (16-y, 16-y, 16-y, 16-y, y, y, y, y) michael@0: allY = _mm_unpacklo_epi64(allY, negY); michael@0: michael@0: // (16, 16, 16, 16, 16, 16, 16, 16 ) michael@0: sixteen = _mm_shuffle_epi32(sixteen, 0); michael@0: michael@0: // ( 0, 0, 0, 0, 0, 0, 0, 0) michael@0: __m128i zero = _mm_setzero_si128(); michael@0: michael@0: // ( alpha, alpha, alpha, alpha, alpha, alpha, alpha, alpha ) michael@0: __m128i alpha = _mm_set1_epi16(s.fAlphaScale); michael@0: michael@0: do { michael@0: uint32_t XX = *xy++; // x0:14 | 4 | x1:14 michael@0: unsigned x0 = XX >> 18; michael@0: unsigned x1 = XX & 0x3FFF; michael@0: michael@0: // (0, 0, 0, 0, 0, 0, 0, x) michael@0: __m128i allX = _mm_cvtsi32_si128((XX >> 14) & 0x0F); michael@0: michael@0: // (0, 0, 0, 0, x, x, x, x) michael@0: allX = _mm_shufflelo_epi16(allX, 0); michael@0: michael@0: // (x, x, x, x, x, x, x, x) michael@0: allX = _mm_shuffle_epi32(allX, 0); michael@0: michael@0: // (16-x, 16-x, 16-x, 16-x, 16-x, 16-x, 16-x) michael@0: __m128i negX = _mm_sub_epi16(sixteen, allX); michael@0: michael@0: // Load 4 samples (pixels). michael@0: __m128i a00 = _mm_cvtsi32_si128(row0[x0]); michael@0: __m128i a01 = _mm_cvtsi32_si128(row0[x1]); michael@0: __m128i a10 = _mm_cvtsi32_si128(row1[x0]); michael@0: __m128i a11 = _mm_cvtsi32_si128(row1[x1]); michael@0: michael@0: // (0, 0, a00, a10) michael@0: __m128i a00a10 = _mm_unpacklo_epi32(a10, a00); michael@0: michael@0: // Expand to 16 bits per component. michael@0: a00a10 = _mm_unpacklo_epi8(a00a10, zero); michael@0: michael@0: // ((a00 * (16-y)), (a10 * y)). michael@0: a00a10 = _mm_mullo_epi16(a00a10, allY); michael@0: michael@0: // (a00 * (16-y) * (16-x), a10 * y * (16-x)). michael@0: a00a10 = _mm_mullo_epi16(a00a10, negX); michael@0: michael@0: // (0, 0, a01, a10) michael@0: __m128i a01a11 = _mm_unpacklo_epi32(a11, a01); michael@0: michael@0: // Expand to 16 bits per component. michael@0: a01a11 = _mm_unpacklo_epi8(a01a11, zero); michael@0: michael@0: // (a01 * (16-y)), (a11 * y) michael@0: a01a11 = _mm_mullo_epi16(a01a11, allY); michael@0: michael@0: // (a01 * (16-y) * x), (a11 * y * x) michael@0: a01a11 = _mm_mullo_epi16(a01a11, allX); michael@0: michael@0: // (a00*w00 + a01*w01, a10*w10 + a11*w11) michael@0: __m128i sum = _mm_add_epi16(a00a10, a01a11); michael@0: michael@0: // (DC, a00*w00 + a01*w01) michael@0: __m128i shifted = _mm_shuffle_epi32(sum, 0xEE); michael@0: michael@0: // (DC, a00*w00 + a01*w01 + a10*w10 + a11*w11) michael@0: sum = _mm_add_epi16(sum, shifted); michael@0: michael@0: // Divide each 16 bit component by 256. michael@0: sum = _mm_srli_epi16(sum, 8); michael@0: michael@0: // Multiply by alpha. michael@0: sum = _mm_mullo_epi16(sum, alpha); michael@0: michael@0: // Divide each 16 bit component by 256. michael@0: sum = _mm_srli_epi16(sum, 8); michael@0: michael@0: // Pack lower 4 16 bit values of sum into lower 4 bytes. michael@0: sum = _mm_packus_epi16(sum, zero); michael@0: michael@0: // Extract low int and store. michael@0: *colors++ = _mm_cvtsi128_si32(sum); michael@0: } while (--count > 0); michael@0: } michael@0: michael@0: static inline uint32_t ClampX_ClampY_pack_filter(SkFixed f, unsigned max, michael@0: SkFixed one) { michael@0: unsigned i = SkClampMax(f >> 16, max); michael@0: i = (i << 4) | ((f >> 12) & 0xF); michael@0: return (i << 14) | SkClampMax((f + one) >> 16, max); michael@0: } michael@0: michael@0: /* SSE version of ClampX_ClampY_filter_scale() michael@0: * portable version is in core/SkBitmapProcState_matrix.h michael@0: */ michael@0: void ClampX_ClampY_filter_scale_SSE2(const SkBitmapProcState& s, uint32_t xy[], michael@0: int count, int x, int y) { michael@0: SkASSERT((s.fInvType & ~(SkMatrix::kTranslate_Mask | michael@0: SkMatrix::kScale_Mask)) == 0); michael@0: SkASSERT(s.fInvKy == 0); michael@0: michael@0: const unsigned maxX = s.fBitmap->width() - 1; michael@0: const SkFixed one = s.fFilterOneX; michael@0: const SkFixed dx = s.fInvSx; michael@0: SkFixed fx; michael@0: michael@0: SkPoint pt; michael@0: s.fInvProc(s.fInvMatrix, SkIntToScalar(x) + SK_ScalarHalf, michael@0: SkIntToScalar(y) + SK_ScalarHalf, &pt); michael@0: const SkFixed fy = SkScalarToFixed(pt.fY) - (s.fFilterOneY >> 1); michael@0: const unsigned maxY = s.fBitmap->height() - 1; michael@0: // compute our two Y values up front michael@0: *xy++ = ClampX_ClampY_pack_filter(fy, maxY, s.fFilterOneY); michael@0: // now initialize fx michael@0: fx = SkScalarToFixed(pt.fX) - (one >> 1); michael@0: michael@0: // test if we don't need to apply the tile proc michael@0: if (dx > 0 && (unsigned)(fx >> 16) <= maxX && michael@0: (unsigned)((fx + dx * (count - 1)) >> 16) < maxX) { michael@0: if (count >= 4) { michael@0: // SSE version of decal_filter_scale michael@0: while ((size_t(xy) & 0x0F) != 0) { michael@0: SkASSERT((fx >> (16 + 14)) == 0); michael@0: *xy++ = (fx >> 12 << 14) | ((fx >> 16) + 1); michael@0: fx += dx; michael@0: count--; michael@0: } michael@0: michael@0: __m128i wide_1 = _mm_set1_epi32(1); michael@0: __m128i wide_dx4 = _mm_set1_epi32(dx * 4); michael@0: __m128i wide_fx = _mm_set_epi32(fx + dx * 3, fx + dx * 2, michael@0: fx + dx, fx); michael@0: michael@0: while (count >= 4) { michael@0: __m128i wide_out; michael@0: michael@0: wide_out = _mm_slli_epi32(_mm_srai_epi32(wide_fx, 12), 14); michael@0: wide_out = _mm_or_si128(wide_out, _mm_add_epi32( michael@0: _mm_srai_epi32(wide_fx, 16), wide_1)); michael@0: michael@0: _mm_store_si128(reinterpret_cast<__m128i*>(xy), wide_out); michael@0: michael@0: xy += 4; michael@0: fx += dx * 4; michael@0: wide_fx = _mm_add_epi32(wide_fx, wide_dx4); michael@0: count -= 4; michael@0: } // while count >= 4 michael@0: } // if count >= 4 michael@0: michael@0: while (count-- > 0) { michael@0: SkASSERT((fx >> (16 + 14)) == 0); michael@0: *xy++ = (fx >> 12 << 14) | ((fx >> 16) + 1); michael@0: fx += dx; michael@0: } michael@0: } else { michael@0: // SSE2 only support 16bit interger max & min, so only process the case michael@0: // maxX less than the max 16bit interger. Actually maxX is the bitmap's michael@0: // height, there should be rare bitmap whose height will be greater michael@0: // than max 16bit interger in the real world. michael@0: if ((count >= 4) && (maxX <= 0xFFFF)) { michael@0: while (((size_t)xy & 0x0F) != 0) { michael@0: *xy++ = ClampX_ClampY_pack_filter(fx, maxX, one); michael@0: fx += dx; michael@0: count--; michael@0: } michael@0: michael@0: __m128i wide_fx = _mm_set_epi32(fx + dx * 3, fx + dx * 2, michael@0: fx + dx, fx); michael@0: __m128i wide_dx4 = _mm_set1_epi32(dx * 4); michael@0: __m128i wide_one = _mm_set1_epi32(one); michael@0: __m128i wide_maxX = _mm_set1_epi32(maxX); michael@0: __m128i wide_mask = _mm_set1_epi32(0xF); michael@0: michael@0: while (count >= 4) { michael@0: __m128i wide_i; michael@0: __m128i wide_lo; michael@0: __m128i wide_fx1; michael@0: michael@0: // i = SkClampMax(f>>16,maxX) michael@0: wide_i = _mm_max_epi16(_mm_srli_epi32(wide_fx, 16), michael@0: _mm_setzero_si128()); michael@0: wide_i = _mm_min_epi16(wide_i, wide_maxX); michael@0: michael@0: // i<<4 | TILEX_LOW_BITS(fx) michael@0: wide_lo = _mm_srli_epi32(wide_fx, 12); michael@0: wide_lo = _mm_and_si128(wide_lo, wide_mask); michael@0: wide_i = _mm_slli_epi32(wide_i, 4); michael@0: wide_i = _mm_or_si128(wide_i, wide_lo); michael@0: michael@0: // i<<14 michael@0: wide_i = _mm_slli_epi32(wide_i, 14); michael@0: michael@0: // SkClampMax(((f+one))>>16,max) michael@0: wide_fx1 = _mm_add_epi32(wide_fx, wide_one); michael@0: wide_fx1 = _mm_max_epi16(_mm_srli_epi32(wide_fx1, 16), michael@0: _mm_setzero_si128()); michael@0: wide_fx1 = _mm_min_epi16(wide_fx1, wide_maxX); michael@0: michael@0: // final combination michael@0: wide_i = _mm_or_si128(wide_i, wide_fx1); michael@0: _mm_store_si128(reinterpret_cast<__m128i*>(xy), wide_i); michael@0: michael@0: wide_fx = _mm_add_epi32(wide_fx, wide_dx4); michael@0: fx += dx * 4; michael@0: xy += 4; michael@0: count -= 4; michael@0: } // while count >= 4 michael@0: } // if count >= 4 michael@0: michael@0: while (count-- > 0) { michael@0: *xy++ = ClampX_ClampY_pack_filter(fx, maxX, one); michael@0: fx += dx; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* SSE version of ClampX_ClampY_nofilter_scale() michael@0: * portable version is in core/SkBitmapProcState_matrix.h michael@0: */ michael@0: void ClampX_ClampY_nofilter_scale_SSE2(const SkBitmapProcState& s, michael@0: uint32_t xy[], int count, int x, int y) { michael@0: SkASSERT((s.fInvType & ~(SkMatrix::kTranslate_Mask | michael@0: SkMatrix::kScale_Mask)) == 0); michael@0: michael@0: // we store y, x, x, x, x, x michael@0: const unsigned maxX = s.fBitmap->width() - 1; michael@0: SkFixed fx; michael@0: SkPoint pt; michael@0: s.fInvProc(s.fInvMatrix, SkIntToScalar(x) + SK_ScalarHalf, michael@0: SkIntToScalar(y) + SK_ScalarHalf, &pt); michael@0: fx = SkScalarToFixed(pt.fY); michael@0: const unsigned maxY = s.fBitmap->height() - 1; michael@0: *xy++ = SkClampMax(fx >> 16, maxY); michael@0: fx = SkScalarToFixed(pt.fX); michael@0: michael@0: if (0 == maxX) { michael@0: // all of the following X values must be 0 michael@0: memset(xy, 0, count * sizeof(uint16_t)); michael@0: return; michael@0: } michael@0: michael@0: const SkFixed dx = s.fInvSx; michael@0: michael@0: // test if we don't need to apply the tile proc michael@0: if ((unsigned)(fx >> 16) <= maxX && michael@0: (unsigned)((fx + dx * (count - 1)) >> 16) <= maxX) { michael@0: // SSE version of decal_nofilter_scale michael@0: if (count >= 8) { michael@0: while (((size_t)xy & 0x0F) != 0) { michael@0: *xy++ = pack_two_shorts(fx >> 16, (fx + dx) >> 16); michael@0: fx += 2 * dx; michael@0: count -= 2; michael@0: } michael@0: michael@0: __m128i wide_dx4 = _mm_set1_epi32(dx * 4); michael@0: __m128i wide_dx8 = _mm_add_epi32(wide_dx4, wide_dx4); michael@0: michael@0: __m128i wide_low = _mm_set_epi32(fx + dx * 3, fx + dx * 2, michael@0: fx + dx, fx); michael@0: __m128i wide_high = _mm_add_epi32(wide_low, wide_dx4); michael@0: michael@0: while (count >= 8) { michael@0: __m128i wide_out_low = _mm_srli_epi32(wide_low, 16); michael@0: __m128i wide_out_high = _mm_srli_epi32(wide_high, 16); michael@0: michael@0: __m128i wide_result = _mm_packs_epi32(wide_out_low, michael@0: wide_out_high); michael@0: _mm_store_si128(reinterpret_cast<__m128i*>(xy), wide_result); michael@0: michael@0: wide_low = _mm_add_epi32(wide_low, wide_dx8); michael@0: wide_high = _mm_add_epi32(wide_high, wide_dx8); michael@0: michael@0: xy += 4; michael@0: fx += dx * 8; michael@0: count -= 8; michael@0: } michael@0: } // if count >= 8 michael@0: michael@0: uint16_t* xx = reinterpret_cast(xy); michael@0: while (count-- > 0) { michael@0: *xx++ = SkToU16(fx >> 16); michael@0: fx += dx; michael@0: } michael@0: } else { michael@0: // SSE2 only support 16bit interger max & min, so only process the case michael@0: // maxX less than the max 16bit interger. Actually maxX is the bitmap's michael@0: // height, there should be rare bitmap whose height will be greater michael@0: // than max 16bit interger in the real world. michael@0: if ((count >= 8) && (maxX <= 0xFFFF)) { michael@0: while (((size_t)xy & 0x0F) != 0) { michael@0: *xy++ = pack_two_shorts(SkClampMax((fx + dx) >> 16, maxX), michael@0: SkClampMax(fx >> 16, maxX)); michael@0: fx += 2 * dx; michael@0: count -= 2; michael@0: } michael@0: michael@0: __m128i wide_dx4 = _mm_set1_epi32(dx * 4); michael@0: __m128i wide_dx8 = _mm_add_epi32(wide_dx4, wide_dx4); michael@0: michael@0: __m128i wide_low = _mm_set_epi32(fx + dx * 3, fx + dx * 2, michael@0: fx + dx, fx); michael@0: __m128i wide_high = _mm_add_epi32(wide_low, wide_dx4); michael@0: __m128i wide_maxX = _mm_set1_epi32(maxX); michael@0: michael@0: while (count >= 8) { michael@0: __m128i wide_out_low = _mm_srli_epi32(wide_low, 16); michael@0: __m128i wide_out_high = _mm_srli_epi32(wide_high, 16); michael@0: michael@0: wide_out_low = _mm_max_epi16(wide_out_low, michael@0: _mm_setzero_si128()); michael@0: wide_out_low = _mm_min_epi16(wide_out_low, wide_maxX); michael@0: wide_out_high = _mm_max_epi16(wide_out_high, michael@0: _mm_setzero_si128()); michael@0: wide_out_high = _mm_min_epi16(wide_out_high, wide_maxX); michael@0: michael@0: __m128i wide_result = _mm_packs_epi32(wide_out_low, michael@0: wide_out_high); michael@0: _mm_store_si128(reinterpret_cast<__m128i*>(xy), wide_result); michael@0: michael@0: wide_low = _mm_add_epi32(wide_low, wide_dx8); michael@0: wide_high = _mm_add_epi32(wide_high, wide_dx8); michael@0: michael@0: xy += 4; michael@0: fx += dx * 8; michael@0: count -= 8; michael@0: } michael@0: } // if count >= 8 michael@0: michael@0: uint16_t* xx = reinterpret_cast(xy); michael@0: while (count-- > 0) { michael@0: *xx++ = SkClampMax(fx >> 16, maxX); michael@0: fx += dx; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* SSE version of ClampX_ClampY_filter_affine() michael@0: * portable version is in core/SkBitmapProcState_matrix.h michael@0: */ michael@0: void ClampX_ClampY_filter_affine_SSE2(const SkBitmapProcState& s, michael@0: uint32_t xy[], int count, int x, int y) { michael@0: SkPoint srcPt; michael@0: s.fInvProc(s.fInvMatrix, michael@0: SkIntToScalar(x) + SK_ScalarHalf, michael@0: SkIntToScalar(y) + SK_ScalarHalf, &srcPt); michael@0: michael@0: SkFixed oneX = s.fFilterOneX; michael@0: SkFixed oneY = s.fFilterOneY; michael@0: SkFixed fx = SkScalarToFixed(srcPt.fX) - (oneX >> 1); michael@0: SkFixed fy = SkScalarToFixed(srcPt.fY) - (oneY >> 1); michael@0: SkFixed dx = s.fInvSx; michael@0: SkFixed dy = s.fInvKy; michael@0: unsigned maxX = s.fBitmap->width() - 1; michael@0: unsigned maxY = s.fBitmap->height() - 1; michael@0: michael@0: if (count >= 2 && (maxX <= 0xFFFF)) { michael@0: SkFixed dx2 = dx + dx; michael@0: SkFixed dy2 = dy + dy; michael@0: michael@0: __m128i wide_f = _mm_set_epi32(fx + dx, fy + dy, fx, fy); michael@0: __m128i wide_d2 = _mm_set_epi32(dx2, dy2, dx2, dy2); michael@0: __m128i wide_one = _mm_set_epi32(oneX, oneY, oneX, oneY); michael@0: __m128i wide_max = _mm_set_epi32(maxX, maxY, maxX, maxY); michael@0: __m128i wide_mask = _mm_set1_epi32(0xF); michael@0: michael@0: while (count >= 2) { michael@0: // i = SkClampMax(f>>16,maxX) michael@0: __m128i wide_i = _mm_max_epi16(_mm_srli_epi32(wide_f, 16), michael@0: _mm_setzero_si128()); michael@0: wide_i = _mm_min_epi16(wide_i, wide_max); michael@0: michael@0: // i<<4 | TILEX_LOW_BITS(f) michael@0: __m128i wide_lo = _mm_srli_epi32(wide_f, 12); michael@0: wide_lo = _mm_and_si128(wide_lo, wide_mask); michael@0: wide_i = _mm_slli_epi32(wide_i, 4); michael@0: wide_i = _mm_or_si128(wide_i, wide_lo); michael@0: michael@0: // i<<14 michael@0: wide_i = _mm_slli_epi32(wide_i, 14); michael@0: michael@0: // SkClampMax(((f+one))>>16,max) michael@0: __m128i wide_f1 = _mm_add_epi32(wide_f, wide_one); michael@0: wide_f1 = _mm_max_epi16(_mm_srli_epi32(wide_f1, 16), michael@0: _mm_setzero_si128()); michael@0: wide_f1 = _mm_min_epi16(wide_f1, wide_max); michael@0: michael@0: // final combination michael@0: wide_i = _mm_or_si128(wide_i, wide_f1); michael@0: _mm_storeu_si128(reinterpret_cast<__m128i*>(xy), wide_i); michael@0: michael@0: wide_f = _mm_add_epi32(wide_f, wide_d2); michael@0: michael@0: fx += dx2; michael@0: fy += dy2; michael@0: xy += 4; michael@0: count -= 2; michael@0: } // while count >= 2 michael@0: } // if count >= 2 michael@0: michael@0: while (count-- > 0) { michael@0: *xy++ = ClampX_ClampY_pack_filter(fy, maxY, oneY); michael@0: fy += dy; michael@0: *xy++ = ClampX_ClampY_pack_filter(fx, maxX, oneX); michael@0: fx += dx; michael@0: } michael@0: } michael@0: michael@0: /* SSE version of ClampX_ClampY_nofilter_affine() michael@0: * portable version is in core/SkBitmapProcState_matrix.h michael@0: */ michael@0: void ClampX_ClampY_nofilter_affine_SSE2(const SkBitmapProcState& s, michael@0: uint32_t xy[], int count, int x, int y) { michael@0: SkASSERT(s.fInvType & SkMatrix::kAffine_Mask); michael@0: SkASSERT((s.fInvType & ~(SkMatrix::kTranslate_Mask | michael@0: SkMatrix::kScale_Mask | michael@0: SkMatrix::kAffine_Mask)) == 0); michael@0: michael@0: SkPoint srcPt; michael@0: s.fInvProc(s.fInvMatrix, michael@0: SkIntToScalar(x) + SK_ScalarHalf, michael@0: SkIntToScalar(y) + SK_ScalarHalf, &srcPt); michael@0: michael@0: SkFixed fx = SkScalarToFixed(srcPt.fX); michael@0: SkFixed fy = SkScalarToFixed(srcPt.fY); michael@0: SkFixed dx = s.fInvSx; michael@0: SkFixed dy = s.fInvKy; michael@0: int maxX = s.fBitmap->width() - 1; michael@0: int maxY = s.fBitmap->height() - 1; michael@0: michael@0: if (count >= 4 && (maxX <= 0xFFFF)) { michael@0: while (((size_t)xy & 0x0F) != 0) { michael@0: *xy++ = (SkClampMax(fy >> 16, maxY) << 16) | michael@0: SkClampMax(fx >> 16, maxX); michael@0: fx += dx; michael@0: fy += dy; michael@0: count--; michael@0: } michael@0: michael@0: SkFixed dx4 = dx * 4; michael@0: SkFixed dy4 = dy * 4; michael@0: michael@0: __m128i wide_fx = _mm_set_epi32(fx + dx * 3, fx + dx * 2, michael@0: fx + dx, fx); michael@0: __m128i wide_fy = _mm_set_epi32(fy + dy * 3, fy + dy * 2, michael@0: fy + dy, fy); michael@0: __m128i wide_dx4 = _mm_set1_epi32(dx4); michael@0: __m128i wide_dy4 = _mm_set1_epi32(dy4); michael@0: michael@0: __m128i wide_maxX = _mm_set1_epi32(maxX); michael@0: __m128i wide_maxY = _mm_set1_epi32(maxY); michael@0: michael@0: while (count >= 4) { michael@0: // SkClampMax(fx>>16,maxX) michael@0: __m128i wide_lo = _mm_max_epi16(_mm_srli_epi32(wide_fx, 16), michael@0: _mm_setzero_si128()); michael@0: wide_lo = _mm_min_epi16(wide_lo, wide_maxX); michael@0: michael@0: // SkClampMax(fy>>16,maxY) michael@0: __m128i wide_hi = _mm_max_epi16(_mm_srli_epi32(wide_fy, 16), michael@0: _mm_setzero_si128()); michael@0: wide_hi = _mm_min_epi16(wide_hi, wide_maxY); michael@0: michael@0: // final combination michael@0: __m128i wide_i = _mm_or_si128(_mm_slli_epi32(wide_hi, 16), michael@0: wide_lo); michael@0: _mm_store_si128(reinterpret_cast<__m128i*>(xy), wide_i); michael@0: michael@0: wide_fx = _mm_add_epi32(wide_fx, wide_dx4); michael@0: wide_fy = _mm_add_epi32(wide_fy, wide_dy4); michael@0: michael@0: fx += dx4; michael@0: fy += dy4; michael@0: xy += 4; michael@0: count -= 4; michael@0: } // while count >= 4 michael@0: } // if count >= 4 michael@0: michael@0: while (count-- > 0) { michael@0: *xy++ = (SkClampMax(fy >> 16, maxY) << 16) | michael@0: SkClampMax(fx >> 16, maxX); michael@0: fx += dx; michael@0: fy += dy; michael@0: } michael@0: } michael@0: michael@0: /* SSE version of S32_D16_filter_DX_SSE2 michael@0: * Definition is in section of "D16 functions for SRC == 8888" in SkBitmapProcState.cpp michael@0: * It combines S32_opaque_D32_filter_DX_SSE2 and SkPixel32ToPixel16 michael@0: */ michael@0: void S32_D16_filter_DX_SSE2(const SkBitmapProcState& s, michael@0: const uint32_t* xy, michael@0: int count, uint16_t* colors) { michael@0: SkASSERT(count > 0 && colors != NULL); michael@0: SkASSERT(s.fFilterLevel != SkPaint::kNone_FilterLevel); michael@0: SkASSERT(s.fBitmap->config() == SkBitmap::kARGB_8888_Config); michael@0: SkASSERT(s.fBitmap->isOpaque()); michael@0: michael@0: SkPMColor dstColor; michael@0: const char* srcAddr = static_cast(s.fBitmap->getPixels()); michael@0: size_t rb = s.fBitmap->rowBytes(); michael@0: uint32_t XY = *xy++; michael@0: unsigned y0 = XY >> 14; michael@0: const uint32_t* row0 = reinterpret_cast(srcAddr + (y0 >> 4) * rb); michael@0: const uint32_t* row1 = reinterpret_cast(srcAddr + (XY & 0x3FFF) * rb); michael@0: unsigned subY = y0 & 0xF; michael@0: michael@0: // ( 0, 0, 0, 0, 0, 0, 0, 16) michael@0: __m128i sixteen = _mm_cvtsi32_si128(16); michael@0: michael@0: // ( 0, 0, 0, 0, 16, 16, 16, 16) michael@0: sixteen = _mm_shufflelo_epi16(sixteen, 0); michael@0: michael@0: // ( 0, 0, 0, 0, 0, 0, 0, y) michael@0: __m128i allY = _mm_cvtsi32_si128(subY); michael@0: michael@0: // ( 0, 0, 0, 0, y, y, y, y) michael@0: allY = _mm_shufflelo_epi16(allY, 0); michael@0: michael@0: // ( 0, 0, 0, 0, 16-y, 16-y, 16-y, 16-y) michael@0: __m128i negY = _mm_sub_epi16(sixteen, allY); michael@0: michael@0: // (16-y, 16-y, 16-y, 16-y, y, y, y, y) michael@0: allY = _mm_unpacklo_epi64(allY, negY); michael@0: michael@0: // (16, 16, 16, 16, 16, 16, 16, 16 ) michael@0: sixteen = _mm_shuffle_epi32(sixteen, 0); michael@0: michael@0: // ( 0, 0, 0, 0, 0, 0, 0, 0) michael@0: __m128i zero = _mm_setzero_si128(); michael@0: michael@0: do { michael@0: uint32_t XX = *xy++; // x0:14 | 4 | x1:14 michael@0: unsigned x0 = XX >> 18; michael@0: unsigned x1 = XX & 0x3FFF; michael@0: michael@0: // (0, 0, 0, 0, 0, 0, 0, x) michael@0: __m128i allX = _mm_cvtsi32_si128((XX >> 14) & 0x0F); michael@0: michael@0: // (0, 0, 0, 0, x, x, x, x) michael@0: allX = _mm_shufflelo_epi16(allX, 0); michael@0: michael@0: // (x, x, x, x, x, x, x, x) michael@0: allX = _mm_shuffle_epi32(allX, 0); michael@0: michael@0: // (16-x, 16-x, 16-x, 16-x, 16-x, 16-x, 16-x) michael@0: __m128i negX = _mm_sub_epi16(sixteen, allX); michael@0: michael@0: // Load 4 samples (pixels). michael@0: __m128i a00 = _mm_cvtsi32_si128(row0[x0]); michael@0: __m128i a01 = _mm_cvtsi32_si128(row0[x1]); michael@0: __m128i a10 = _mm_cvtsi32_si128(row1[x0]); michael@0: __m128i a11 = _mm_cvtsi32_si128(row1[x1]); michael@0: michael@0: // (0, 0, a00, a10) michael@0: __m128i a00a10 = _mm_unpacklo_epi32(a10, a00); michael@0: michael@0: // Expand to 16 bits per component. michael@0: a00a10 = _mm_unpacklo_epi8(a00a10, zero); michael@0: michael@0: // ((a00 * (16-y)), (a10 * y)). michael@0: a00a10 = _mm_mullo_epi16(a00a10, allY); michael@0: michael@0: // (a00 * (16-y) * (16-x), a10 * y * (16-x)). michael@0: a00a10 = _mm_mullo_epi16(a00a10, negX); michael@0: michael@0: // (0, 0, a01, a10) michael@0: __m128i a01a11 = _mm_unpacklo_epi32(a11, a01); michael@0: michael@0: // Expand to 16 bits per component. michael@0: a01a11 = _mm_unpacklo_epi8(a01a11, zero); michael@0: michael@0: // (a01 * (16-y)), (a11 * y) michael@0: a01a11 = _mm_mullo_epi16(a01a11, allY); michael@0: michael@0: // (a01 * (16-y) * x), (a11 * y * x) michael@0: a01a11 = _mm_mullo_epi16(a01a11, allX); michael@0: michael@0: // (a00*w00 + a01*w01, a10*w10 + a11*w11) michael@0: __m128i sum = _mm_add_epi16(a00a10, a01a11); michael@0: michael@0: // (DC, a00*w00 + a01*w01) michael@0: __m128i shifted = _mm_shuffle_epi32(sum, 0xEE); michael@0: michael@0: // (DC, a00*w00 + a01*w01 + a10*w10 + a11*w11) michael@0: sum = _mm_add_epi16(sum, shifted); michael@0: michael@0: // Divide each 16 bit component by 256. michael@0: sum = _mm_srli_epi16(sum, 8); michael@0: michael@0: // Pack lower 4 16 bit values of sum into lower 4 bytes. michael@0: sum = _mm_packus_epi16(sum, zero); michael@0: michael@0: // Extract low int and store. michael@0: dstColor = _mm_cvtsi128_si32(sum); michael@0: michael@0: //*colors++ = SkPixel32ToPixel16(dstColor); michael@0: // below is much faster than the above. It's tested for Android benchmark--Softweg michael@0: __m128i _m_temp1 = _mm_set1_epi32(dstColor); michael@0: __m128i _m_temp2 = _mm_srli_epi32(_m_temp1, 3); michael@0: michael@0: unsigned int r32 = _mm_cvtsi128_si32(_m_temp2); michael@0: unsigned r = (r32 & ((1<<5) -1)) << 11; michael@0: michael@0: _m_temp2 = _mm_srli_epi32(_m_temp2, 7); michael@0: unsigned int g32 = _mm_cvtsi128_si32(_m_temp2); michael@0: unsigned g = (g32 & ((1<<6) -1)) << 5; michael@0: michael@0: _m_temp2 = _mm_srli_epi32(_m_temp2, 9); michael@0: unsigned int b32 = _mm_cvtsi128_si32(_m_temp2); michael@0: unsigned b = (b32 & ((1<<5) -1)); michael@0: michael@0: *colors++ = r | g | b; michael@0: michael@0: } while (--count > 0); michael@0: }