зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1805757 - Move SkConvolver into the tree. r=aosmond
Differential Revision: https://phabricator.services.mozilla.com/D164732
This commit is contained in:
Родитель
650c726cb3
Коммит
0ebc2df843
|
@ -5,17 +5,14 @@
|
|||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "ConvolutionFilter.h"
|
||||
#include "skia/src/core/SkBitmapFilter.h"
|
||||
#include "skia/src/core/SkConvolver.h"
|
||||
#include "skia/src/core/SkOpts.h"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include "mozilla/Vector.h"
|
||||
#include "HelpersSkia.h"
|
||||
#include "SkConvolver.h"
|
||||
#include "skia/include/core/SkBitmap.h"
|
||||
|
||||
namespace mozilla::gfx {
|
||||
|
||||
ConvolutionFilter::ConvolutionFilter()
|
||||
: mFilter(MakeUnique<SkConvolutionFilter1D>()) {}
|
||||
: mFilter(MakeUnique<skia::SkConvolutionFilter1D>()) {}
|
||||
|
||||
ConvolutionFilter::~ConvolutionFilter() = default;
|
||||
|
||||
|
@ -35,7 +32,7 @@ bool ConvolutionFilter::GetFilterOffsetAndLength(int32_t aRowIndex,
|
|||
|
||||
void ConvolutionFilter::ConvolveHorizontally(const uint8_t* aSrc, uint8_t* aDst,
|
||||
bool aHasAlpha) {
|
||||
SkOpts::convolve_horizontally(aSrc, *mFilter, aDst, aHasAlpha);
|
||||
skia::convolve_horizontally(aSrc, *mFilter, aDst, aHasAlpha);
|
||||
}
|
||||
|
||||
void ConvolutionFilter::ConvolveVertically(uint8_t* const* aSrc, uint8_t* aDst,
|
||||
|
@ -47,134 +44,71 @@ void ConvolutionFilter::ConvolveVertically(uint8_t* const* aSrc, uint8_t* aDst,
|
|||
int32_t filterLength;
|
||||
auto filterValues =
|
||||
mFilter->FilterForValue(aRowIndex, &filterOffset, &filterLength);
|
||||
SkOpts::convolve_vertically(filterValues, filterLength, aSrc, aRowSize, aDst,
|
||||
aHasAlpha);
|
||||
skia::convolve_vertically(filterValues, filterLength, aSrc, aRowSize, aDst,
|
||||
aHasAlpha);
|
||||
}
|
||||
|
||||
/* ConvolutionFilter::ComputeResizeFactor is derived from Skia's
|
||||
* SkBitmapScaler/SkResizeFilter::computeFactors. It is governed by Skia's
|
||||
* BSD-style license (see gfx/skia/LICENSE) and the following copyright:
|
||||
* Copyright (c) 2015 Google Inc.
|
||||
*/
|
||||
bool ConvolutionFilter::ComputeResizeFilter(ResizeMethod aResizeMethod,
|
||||
int32_t aSrcSize,
|
||||
int32_t aDstSize) {
|
||||
typedef SkConvolutionFilter1D::ConvolutionFixed Fixed;
|
||||
|
||||
if (aSrcSize < 0 || aDstSize < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
UniquePtr<SkBitmapFilter> bitmapFilter;
|
||||
switch (aResizeMethod) {
|
||||
case ResizeMethod::BOX:
|
||||
bitmapFilter = MakeUnique<SkBoxFilter>();
|
||||
break;
|
||||
case ResizeMethod::TRIANGLE:
|
||||
bitmapFilter = MakeUnique<SkTriangleFilter>();
|
||||
break;
|
||||
return mFilter->ComputeFilterValues(skia::SkBoxFilter(), aSrcSize,
|
||||
aDstSize);
|
||||
case ResizeMethod::LANCZOS3:
|
||||
bitmapFilter = MakeUnique<SkLanczosFilter>();
|
||||
break;
|
||||
case ResizeMethod::HAMMING:
|
||||
bitmapFilter = MakeUnique<SkHammingFilter>();
|
||||
break;
|
||||
case ResizeMethod::MITCHELL:
|
||||
bitmapFilter = MakeUnique<SkMitchellFilter>();
|
||||
break;
|
||||
return mFilter->ComputeFilterValues(skia::SkLanczosFilter(), aSrcSize,
|
||||
aDstSize);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// When we're doing a magnification, the scale will be larger than one. This
|
||||
// means the destination pixels are much smaller than the source pixels, and
|
||||
// that the range covered by the filter won't necessarily cover any source
|
||||
// pixel boundaries. Therefore, we use these clamped values (max of 1) for
|
||||
// some computations.
|
||||
float scale = float(aDstSize) / float(aSrcSize);
|
||||
float clampedScale = std::min(1.0f, scale);
|
||||
// This is how many source pixels from the center we need to count
|
||||
// to support the filtering function.
|
||||
float srcSupport = bitmapFilter->width() / clampedScale;
|
||||
float invScale = 1.0f / scale;
|
||||
|
||||
Vector<float, 64> filterValues;
|
||||
Vector<Fixed, 64> fixedFilterValues;
|
||||
|
||||
// Loop over all pixels in the output range. We will generate one set of
|
||||
// filter values for each one. Those values will tell us how to blend the
|
||||
// source pixels to compute the destination pixel.
|
||||
|
||||
// This value is computed based on how SkTDArray::resizeStorageToAtLeast works
|
||||
// in order to ensure that it does not overflow or assert. That functions
|
||||
// computes
|
||||
// n+4 + (n+4)/4
|
||||
// and we want to to fit in a 32 bit signed int. Equating that to 2^31-1 and
|
||||
// solving n gives n = (2^31-6)*4/5 = 1717986913.6
|
||||
const int32_t maxToPassToReserveAdditional = 1717986913;
|
||||
|
||||
int32_t filterValueCount = int32_t(ceil(aDstSize * srcSupport * 2));
|
||||
if (aDstSize > maxToPassToReserveAdditional || filterValueCount < 0 ||
|
||||
filterValueCount > maxToPassToReserveAdditional) {
|
||||
bool Scale(uint8_t* srcData, int32_t srcWidth, int32_t srcHeight,
|
||||
int32_t srcStride, uint8_t* dstData, int32_t dstWidth,
|
||||
int32_t dstHeight, int32_t dstStride, SurfaceFormat format) {
|
||||
if (!srcData || !dstData || srcWidth < 1 || srcHeight < 1 || dstWidth < 1 ||
|
||||
dstHeight < 1) {
|
||||
return false;
|
||||
}
|
||||
mFilter->reserveAdditional(aDstSize, filterValueCount);
|
||||
for (int32_t destI = 0; destI < aDstSize; destI++) {
|
||||
// This is the pixel in the source directly under the pixel in the dest.
|
||||
// Note that we base computations on the "center" of the pixels. To see
|
||||
// why, observe that the destination pixel at coordinates (0, 0) in a 5.0x
|
||||
// downscale should "cover" the pixels around the pixel with *its center*
|
||||
// at coordinates (2.5, 2.5) in the source, not those around (0, 0).
|
||||
// Hence we need to scale coordinates (0.5, 0.5), not (0, 0).
|
||||
float srcPixel = (static_cast<float>(destI) + 0.5f) * invScale;
|
||||
|
||||
// Compute the (inclusive) range of source pixels the filter covers.
|
||||
float srcBegin = std::max(0.0f, floorf(srcPixel - srcSupport));
|
||||
float srcEnd = std::min(aSrcSize - 1.0f, ceilf(srcPixel + srcSupport));
|
||||
SkPixmap srcPixmap(MakeSkiaImageInfo(IntSize(srcWidth, srcHeight), format),
|
||||
srcData, srcStride);
|
||||
|
||||
// Compute the unnormalized filter value at each location of the source
|
||||
// it covers.
|
||||
|
||||
// Sum of the filter values for normalizing.
|
||||
// Distance from the center of the filter, this is the filter coordinate
|
||||
// in source space. We also need to consider the center of the pixel
|
||||
// when comparing distance against 'srcPixel'. In the 5x downscale
|
||||
// example used above the distance from the center of the filter to
|
||||
// the pixel with coordinates (2, 2) should be 0, because its center
|
||||
// is at (2.5, 2.5).
|
||||
float destFilterDist = (srcBegin + 0.5f - srcPixel) * clampedScale;
|
||||
int32_t filterCount = int32_t(srcEnd - srcBegin) + 1;
|
||||
if (filterCount <= 0 || !filterValues.resize(filterCount) ||
|
||||
!fixedFilterValues.resize(filterCount)) {
|
||||
// Rescaler is compatible with N32 only. Convert to N32 if needed.
|
||||
SkBitmap tmpBitmap;
|
||||
if (srcPixmap.colorType() != kN32_SkColorType) {
|
||||
if (!tmpBitmap.tryAllocPixels(
|
||||
SkImageInfo::MakeN32Premul(srcWidth, srcHeight)) ||
|
||||
!tmpBitmap.writePixels(srcPixmap) ||
|
||||
!tmpBitmap.peekPixels(&srcPixmap)) {
|
||||
return false;
|
||||
}
|
||||
float filterSum = bitmapFilter->evaluate_n(
|
||||
destFilterDist, clampedScale, filterCount, filterValues.begin());
|
||||
|
||||
// The filter must be normalized so that we don't affect the brightness of
|
||||
// the image. Convert to normalized fixed point.
|
||||
Fixed fixedSum = 0;
|
||||
float invFilterSum = 1.0f / filterSum;
|
||||
for (int32_t fixedI = 0; fixedI < filterCount; fixedI++) {
|
||||
Fixed curFixed = SkConvolutionFilter1D::FloatToFixed(
|
||||
filterValues[fixedI] * invFilterSum);
|
||||
fixedSum += curFixed;
|
||||
fixedFilterValues[fixedI] = curFixed;
|
||||
}
|
||||
|
||||
// The conversion to fixed point will leave some rounding errors, which
|
||||
// we add back in to avoid affecting the brightness of the image. We
|
||||
// arbitrarily add this to the center of the filter array (this won't always
|
||||
// be the center of the filter function since it could get clipped on the
|
||||
// edges, but it doesn't matter enough to worry about that case).
|
||||
Fixed leftovers = SkConvolutionFilter1D::FloatToFixed(1) - fixedSum;
|
||||
fixedFilterValues[filterCount / 2] += leftovers;
|
||||
|
||||
mFilter->AddFilter(int32_t(srcBegin), fixedFilterValues.begin(),
|
||||
filterCount);
|
||||
}
|
||||
|
||||
return mFilter->maxFilter() > 0 && mFilter->numValues() == aDstSize;
|
||||
ConvolutionFilter xFilter;
|
||||
ConvolutionFilter yFilter;
|
||||
ConvolutionFilter* xOrYFilter = &xFilter;
|
||||
bool isSquare = srcWidth == srcHeight && dstWidth == dstHeight;
|
||||
if (!xFilter.ComputeResizeFilter(ConvolutionFilter::ResizeMethod::LANCZOS3,
|
||||
srcWidth, dstWidth)) {
|
||||
return false;
|
||||
}
|
||||
if (!isSquare) {
|
||||
if (!yFilter.ComputeResizeFilter(ConvolutionFilter::ResizeMethod::LANCZOS3,
|
||||
srcHeight, dstHeight)) {
|
||||
return false;
|
||||
}
|
||||
xOrYFilter = &yFilter;
|
||||
}
|
||||
|
||||
return skia::BGRAConvolve2D(
|
||||
static_cast<const uint8_t*>(srcPixmap.addr()), int(srcPixmap.rowBytes()),
|
||||
!srcPixmap.isOpaque(), xFilter.GetSkiaFilter(),
|
||||
xOrYFilter->GetSkiaFilter(), int(dstStride), dstData);
|
||||
}
|
||||
|
||||
} // namespace mozilla::gfx
|
||||
|
|
|
@ -9,7 +9,9 @@
|
|||
|
||||
#include "mozilla/UniquePtr.h"
|
||||
|
||||
namespace skia {
|
||||
class SkConvolutionFilter1D;
|
||||
}
|
||||
|
||||
namespace mozilla {
|
||||
namespace gfx {
|
||||
|
@ -29,7 +31,7 @@ class ConvolutionFilter final {
|
|||
void ConvolveVertically(uint8_t* const* aSrc, uint8_t* aDst,
|
||||
int32_t aRowIndex, int32_t aRowSize, bool aHasAlpha);
|
||||
|
||||
enum class ResizeMethod { BOX, TRIANGLE, LANCZOS3, HAMMING, MITCHELL };
|
||||
enum class ResizeMethod { BOX, LANCZOS3 };
|
||||
|
||||
bool ComputeResizeFilter(ResizeMethod aResizeMethod, int32_t aSrcSize,
|
||||
int32_t aDstSize);
|
||||
|
@ -38,8 +40,12 @@ class ConvolutionFilter final {
|
|||
return (aBytes + 31) & ~31;
|
||||
}
|
||||
|
||||
const skia::SkConvolutionFilter1D& GetSkiaFilter() const {
|
||||
return *mFilter.get();
|
||||
}
|
||||
|
||||
private:
|
||||
UniquePtr<SkConvolutionFilter1D> mFilter;
|
||||
UniquePtr<skia::SkConvolutionFilter1D> mFilter;
|
||||
};
|
||||
|
||||
} // namespace gfx
|
||||
|
|
|
@ -0,0 +1,104 @@
|
|||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
||||
// Copyright (c) 2011-2016 Google Inc.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the gfx/skia/LICENSE file.
|
||||
|
||||
#include "SkConvolver.h"
|
||||
#include <immintrin.h>
|
||||
|
||||
namespace skia {
|
||||
|
||||
void convolve_vertically_avx2(
|
||||
const SkConvolutionFilter1D::ConvolutionFixed* filter, int filterLen,
|
||||
unsigned char* const* srcRows, int width, unsigned char* out,
|
||||
bool hasAlpha) {
|
||||
// It's simpler to work with the output array in terms of 4-byte pixels.
|
||||
auto* dst = (int*)out;
|
||||
|
||||
// Output up to eight pixels per iteration.
|
||||
for (int x = 0; x < width; x += 8) {
|
||||
// Accumulated result for 4 (non-adjacent) pairs of pixels,
|
||||
// with each channel in signed 17.14 fixed point.
|
||||
auto accum04 = _mm256_setzero_si256(), accum15 = _mm256_setzero_si256(),
|
||||
accum26 = _mm256_setzero_si256(), accum37 = _mm256_setzero_si256();
|
||||
|
||||
// Convolve with the filter. (This inner loop is where we spend ~all our
|
||||
// time.) While we can, we consume 2 filter coefficients and 2 rows of 8
|
||||
// pixels each at a time.
|
||||
auto convolve_16_pixels = [&](__m256i interlaced_coeffs,
|
||||
__m256i pixels_01234567,
|
||||
__m256i pixels_89ABCDEF) {
|
||||
// Interlaced R0R8 G0G8 B0B8 A0A8 R1R9 G1G9... 32 8-bit values each.
|
||||
auto _08194C5D = _mm256_unpacklo_epi8(pixels_01234567, pixels_89ABCDEF),
|
||||
_2A3B6E7F = _mm256_unpackhi_epi8(pixels_01234567, pixels_89ABCDEF);
|
||||
|
||||
// Still interlaced R0R8 G0G8... as above, each channel expanded to 16-bit
|
||||
// lanes.
|
||||
auto _084C = _mm256_unpacklo_epi8(_08194C5D, _mm256_setzero_si256()),
|
||||
_195D = _mm256_unpackhi_epi8(_08194C5D, _mm256_setzero_si256()),
|
||||
_2A6E = _mm256_unpacklo_epi8(_2A3B6E7F, _mm256_setzero_si256()),
|
||||
_3B7F = _mm256_unpackhi_epi8(_2A3B6E7F, _mm256_setzero_si256());
|
||||
|
||||
// accum0_R += R0*coeff0 + R8*coeff1, etc.
|
||||
accum04 = _mm256_add_epi32(accum04,
|
||||
_mm256_madd_epi16(_084C, interlaced_coeffs));
|
||||
accum15 = _mm256_add_epi32(accum15,
|
||||
_mm256_madd_epi16(_195D, interlaced_coeffs));
|
||||
accum26 = _mm256_add_epi32(accum26,
|
||||
_mm256_madd_epi16(_2A6E, interlaced_coeffs));
|
||||
accum37 = _mm256_add_epi32(accum37,
|
||||
_mm256_madd_epi16(_3B7F, interlaced_coeffs));
|
||||
};
|
||||
|
||||
int i = 0;
|
||||
for (; i < filterLen / 2 * 2; i += 2) {
|
||||
convolve_16_pixels(
|
||||
_mm256_set1_epi32(*(const int32_t*)(filter + i)),
|
||||
_mm256_loadu_si256((const __m256i*)(srcRows[i + 0] + x * 4)),
|
||||
_mm256_loadu_si256((const __m256i*)(srcRows[i + 1] + x * 4)));
|
||||
}
|
||||
if (i < filterLen) {
|
||||
convolve_16_pixels(
|
||||
_mm256_set1_epi32(*(const int16_t*)(filter + i)),
|
||||
_mm256_loadu_si256((const __m256i*)(srcRows[i] + x * 4)),
|
||||
_mm256_setzero_si256());
|
||||
}
|
||||
|
||||
// Trim the fractional parts off the accumulators.
|
||||
accum04 = _mm256_srai_epi32(accum04, 14);
|
||||
accum15 = _mm256_srai_epi32(accum15, 14);
|
||||
accum26 = _mm256_srai_epi32(accum26, 14);
|
||||
accum37 = _mm256_srai_epi32(accum37, 14);
|
||||
|
||||
// Pack back down to 8-bit channels.
|
||||
auto pixels = _mm256_packus_epi16(_mm256_packs_epi32(accum04, accum15),
|
||||
_mm256_packs_epi32(accum26, accum37));
|
||||
|
||||
if (hasAlpha) {
|
||||
// Clamp alpha to the max of r,g,b to make sure we stay premultiplied.
|
||||
__m256i max_rg = _mm256_max_epu8(pixels, _mm256_srli_epi32(pixels, 8)),
|
||||
max_rgb = _mm256_max_epu8(max_rg, _mm256_srli_epi32(pixels, 16));
|
||||
pixels = _mm256_max_epu8(pixels, _mm256_slli_epi32(max_rgb, 24));
|
||||
} else {
|
||||
// Force opaque.
|
||||
pixels = _mm256_or_si256(pixels, _mm256_set1_epi32(0xff000000));
|
||||
}
|
||||
|
||||
// Normal path to store 8 pixels.
|
||||
if (x + 8 <= width) {
|
||||
_mm256_storeu_si256((__m256i*)dst, pixels);
|
||||
dst += 8;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Store one pixel at a time on the last iteration.
|
||||
for (int i = x; i < width; i++) {
|
||||
*dst++ = _mm_cvtsi128_si32(_mm256_castsi256_si128(pixels));
|
||||
pixels = _mm256_permutevar8x32_epi32(
|
||||
pixels, _mm256_setr_epi32(1, 2, 3, 4, 5, 6, 7, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace skia
|
|
@ -0,0 +1,287 @@
|
|||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
||||
// Copyright (c) 2011-2016 Google Inc.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the gfx/skia/LICENSE file.
|
||||
|
||||
#include "SkConvolver.h"
|
||||
#include "mozilla/Attributes.h"
|
||||
#include <arm_neon.h>
|
||||
|
||||
namespace skia {
|
||||
|
||||
static MOZ_ALWAYS_INLINE void AccumRemainder(
|
||||
const unsigned char* pixelsLeft,
|
||||
const SkConvolutionFilter1D::ConvolutionFixed* filterValues,
|
||||
int32x4_t& accum, int r) {
|
||||
int remainder[4] = {0};
|
||||
for (int i = 0; i < r; i++) {
|
||||
SkConvolutionFilter1D::ConvolutionFixed coeff = filterValues[i];
|
||||
remainder[0] += coeff * pixelsLeft[i * 4 + 0];
|
||||
remainder[1] += coeff * pixelsLeft[i * 4 + 1];
|
||||
remainder[2] += coeff * pixelsLeft[i * 4 + 2];
|
||||
remainder[3] += coeff * pixelsLeft[i * 4 + 3];
|
||||
}
|
||||
int32x4_t t = {remainder[0], remainder[1], remainder[2], remainder[3]};
|
||||
accum += t;
|
||||
}
|
||||
|
||||
// Convolves horizontally along a single row. The row data is given in
|
||||
// |srcData| and continues for the numValues() of the filter.
|
||||
void convolve_horizontally_neon(const unsigned char* srcData,
|
||||
const SkConvolutionFilter1D& filter,
|
||||
unsigned char* outRow, bool /*hasAlpha*/) {
|
||||
// Loop over each pixel on this row in the output image.
|
||||
int numValues = filter.numValues();
|
||||
for (int outX = 0; outX < numValues; outX++) {
|
||||
uint8x8_t coeff_mask0 = vcreate_u8(0x0100010001000100);
|
||||
uint8x8_t coeff_mask1 = vcreate_u8(0x0302030203020302);
|
||||
uint8x8_t coeff_mask2 = vcreate_u8(0x0504050405040504);
|
||||
uint8x8_t coeff_mask3 = vcreate_u8(0x0706070607060706);
|
||||
// Get the filter that determines the current output pixel.
|
||||
int filterOffset, filterLength;
|
||||
const SkConvolutionFilter1D::ConvolutionFixed* filterValues =
|
||||
filter.FilterForValue(outX, &filterOffset, &filterLength);
|
||||
|
||||
// Compute the first pixel in this row that the filter affects. It will
|
||||
// touch |filterLength| pixels (4 bytes each) after this.
|
||||
const unsigned char* rowToFilter = &srcData[filterOffset * 4];
|
||||
|
||||
// Apply the filter to the row to get the destination pixel in |accum|.
|
||||
int32x4_t accum = vdupq_n_s32(0);
|
||||
for (int filterX = 0; filterX < filterLength >> 2; filterX++) {
|
||||
// Load 4 coefficients
|
||||
int16x4_t coeffs, coeff0, coeff1, coeff2, coeff3;
|
||||
coeffs = vld1_s16(filterValues);
|
||||
coeff0 = vreinterpret_s16_u8(
|
||||
vtbl1_u8(vreinterpret_u8_s16(coeffs), coeff_mask0));
|
||||
coeff1 = vreinterpret_s16_u8(
|
||||
vtbl1_u8(vreinterpret_u8_s16(coeffs), coeff_mask1));
|
||||
coeff2 = vreinterpret_s16_u8(
|
||||
vtbl1_u8(vreinterpret_u8_s16(coeffs), coeff_mask2));
|
||||
coeff3 = vreinterpret_s16_u8(
|
||||
vtbl1_u8(vreinterpret_u8_s16(coeffs), coeff_mask3));
|
||||
|
||||
// Load pixels and calc
|
||||
uint8x16_t pixels = vld1q_u8(rowToFilter);
|
||||
int16x8_t p01_16 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(pixels)));
|
||||
int16x8_t p23_16 = vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(pixels)));
|
||||
|
||||
int16x4_t p0_src = vget_low_s16(p01_16);
|
||||
int16x4_t p1_src = vget_high_s16(p01_16);
|
||||
int16x4_t p2_src = vget_low_s16(p23_16);
|
||||
int16x4_t p3_src = vget_high_s16(p23_16);
|
||||
|
||||
int32x4_t p0 = vmull_s16(p0_src, coeff0);
|
||||
int32x4_t p1 = vmull_s16(p1_src, coeff1);
|
||||
int32x4_t p2 = vmull_s16(p2_src, coeff2);
|
||||
int32x4_t p3 = vmull_s16(p3_src, coeff3);
|
||||
|
||||
accum += p0;
|
||||
accum += p1;
|
||||
accum += p2;
|
||||
accum += p3;
|
||||
|
||||
// Advance the pointers
|
||||
rowToFilter += 16;
|
||||
filterValues += 4;
|
||||
}
|
||||
|
||||
int r = filterLength & 3;
|
||||
if (r) {
|
||||
int remainder_offset = (filterOffset + filterLength - r) * 4;
|
||||
AccumRemainder(srcData + remainder_offset, filterValues, accum, r);
|
||||
}
|
||||
|
||||
// Bring this value back in range. All of the filter scaling factors
|
||||
// are in fixed point with kShiftBits bits of fractional part.
|
||||
accum = vshrq_n_s32(accum, SkConvolutionFilter1D::kShiftBits);
|
||||
|
||||
// Pack and store the new pixel.
|
||||
int16x4_t accum16 = vqmovn_s32(accum);
|
||||
uint8x8_t accum8 = vqmovun_s16(vcombine_s16(accum16, accum16));
|
||||
vst1_lane_u32(reinterpret_cast<uint32_t*>(outRow),
|
||||
vreinterpret_u32_u8(accum8), 0);
|
||||
outRow += 4;
|
||||
}
|
||||
}
|
||||
|
||||
// Does vertical convolution to produce one output row. The filter values and
|
||||
// length are given in the first two parameters. These are applied to each
|
||||
// of the rows pointed to in the |sourceDataRows| array, with each row
|
||||
// being |pixelWidth| wide.
|
||||
//
|
||||
// The output must have room for |pixelWidth * 4| bytes.
|
||||
template <bool hasAlpha>
|
||||
static void ConvolveVertically(
|
||||
const SkConvolutionFilter1D::ConvolutionFixed* filterValues,
|
||||
int filterLength, unsigned char* const* sourceDataRows, int pixelWidth,
|
||||
unsigned char* outRow) {
|
||||
int width = pixelWidth & ~3;
|
||||
|
||||
// Output four pixels per iteration (16 bytes).
|
||||
for (int outX = 0; outX < width; outX += 4) {
|
||||
// Accumulated result for each pixel. 32 bits per RGBA channel.
|
||||
int32x4_t accum0 = vdupq_n_s32(0);
|
||||
int32x4_t accum1 = vdupq_n_s32(0);
|
||||
int32x4_t accum2 = vdupq_n_s32(0);
|
||||
int32x4_t accum3 = vdupq_n_s32(0);
|
||||
|
||||
// Convolve with one filter coefficient per iteration.
|
||||
for (int filterY = 0; filterY < filterLength; filterY++) {
|
||||
// Duplicate the filter coefficient 4 times.
|
||||
// [16] cj cj cj cj
|
||||
int16x4_t coeff16 = vdup_n_s16(filterValues[filterY]);
|
||||
|
||||
// Load four pixels (16 bytes) together.
|
||||
// [8] a3 b3 g3 r3 a2 b2 g2 r2 a1 b1 g1 r1 a0 b0 g0 r0
|
||||
uint8x16_t src8 = vld1q_u8(&sourceDataRows[filterY][outX << 2]);
|
||||
|
||||
int16x8_t src16_01 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(src8)));
|
||||
int16x8_t src16_23 = vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(src8)));
|
||||
int16x4_t src16_0 = vget_low_s16(src16_01);
|
||||
int16x4_t src16_1 = vget_high_s16(src16_01);
|
||||
int16x4_t src16_2 = vget_low_s16(src16_23);
|
||||
int16x4_t src16_3 = vget_high_s16(src16_23);
|
||||
|
||||
accum0 += vmull_s16(src16_0, coeff16);
|
||||
accum1 += vmull_s16(src16_1, coeff16);
|
||||
accum2 += vmull_s16(src16_2, coeff16);
|
||||
accum3 += vmull_s16(src16_3, coeff16);
|
||||
}
|
||||
|
||||
// Shift right for fixed point implementation.
|
||||
accum0 = vshrq_n_s32(accum0, SkConvolutionFilter1D::kShiftBits);
|
||||
accum1 = vshrq_n_s32(accum1, SkConvolutionFilter1D::kShiftBits);
|
||||
accum2 = vshrq_n_s32(accum2, SkConvolutionFilter1D::kShiftBits);
|
||||
accum3 = vshrq_n_s32(accum3, SkConvolutionFilter1D::kShiftBits);
|
||||
|
||||
// Packing 32 bits |accum| to 16 bits per channel (signed saturation).
|
||||
// [16] a1 b1 g1 r1 a0 b0 g0 r0
|
||||
int16x8_t accum16_0 = vcombine_s16(vqmovn_s32(accum0), vqmovn_s32(accum1));
|
||||
// [16] a3 b3 g3 r3 a2 b2 g2 r2
|
||||
int16x8_t accum16_1 = vcombine_s16(vqmovn_s32(accum2), vqmovn_s32(accum3));
|
||||
|
||||
// Packing 16 bits |accum| to 8 bits per channel (unsigned saturation).
|
||||
// [8] a3 b3 g3 r3 a2 b2 g2 r2 a1 b1 g1 r1 a0 b0 g0 r0
|
||||
uint8x16_t accum8 =
|
||||
vcombine_u8(vqmovun_s16(accum16_0), vqmovun_s16(accum16_1));
|
||||
|
||||
if (hasAlpha) {
|
||||
// Compute the max(ri, gi, bi) for each pixel.
|
||||
// [8] xx a3 b3 g3 xx a2 b2 g2 xx a1 b1 g1 xx a0 b0 g0
|
||||
uint8x16_t a =
|
||||
vreinterpretq_u8_u32(vshrq_n_u32(vreinterpretq_u32_u8(accum8), 8));
|
||||
// [8] xx xx xx max3 xx xx xx max2 xx xx xx max1 xx xx xx max0
|
||||
uint8x16_t b = vmaxq_u8(a, accum8); // Max of r and g
|
||||
// [8] xx xx a3 b3 xx xx a2 b2 xx xx a1 b1 xx xx a0 b0
|
||||
a = vreinterpretq_u8_u32(vshrq_n_u32(vreinterpretq_u32_u8(accum8), 16));
|
||||
// [8] xx xx xx max3 xx xx xx max2 xx xx xx max1 xx xx xx max0
|
||||
b = vmaxq_u8(a, b); // Max of r and g and b.
|
||||
// [8] max3 00 00 00 max2 00 00 00 max1 00 00 00 max0 00 00 00
|
||||
b = vreinterpretq_u8_u32(vshlq_n_u32(vreinterpretq_u32_u8(b), 24));
|
||||
|
||||
// Make sure the value of alpha channel is always larger than maximum
|
||||
// value of color channels.
|
||||
accum8 = vmaxq_u8(b, accum8);
|
||||
} else {
|
||||
// Set value of alpha channels to 0xFF.
|
||||
accum8 = vreinterpretq_u8_u32(vreinterpretq_u32_u8(accum8) |
|
||||
vdupq_n_u32(0xFF000000));
|
||||
}
|
||||
|
||||
// Store the convolution result (16 bytes) and advance the pixel pointers.
|
||||
vst1q_u8(outRow, accum8);
|
||||
outRow += 16;
|
||||
}
|
||||
|
||||
// Process the leftovers when the width of the output is not divisible
|
||||
// by 4, that is at most 3 pixels.
|
||||
int r = pixelWidth & 3;
|
||||
if (r) {
|
||||
int32x4_t accum0 = vdupq_n_s32(0);
|
||||
int32x4_t accum1 = vdupq_n_s32(0);
|
||||
int32x4_t accum2 = vdupq_n_s32(0);
|
||||
|
||||
for (int filterY = 0; filterY < filterLength; ++filterY) {
|
||||
int16x4_t coeff16 = vdup_n_s16(filterValues[filterY]);
|
||||
|
||||
// [8] a3 b3 g3 r3 a2 b2 g2 r2 a1 b1 g1 r1 a0 b0 g0 r0
|
||||
uint8x16_t src8 = vld1q_u8(&sourceDataRows[filterY][width << 2]);
|
||||
|
||||
int16x8_t src16_01 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(src8)));
|
||||
int16x8_t src16_23 = vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(src8)));
|
||||
int16x4_t src16_0 = vget_low_s16(src16_01);
|
||||
int16x4_t src16_1 = vget_high_s16(src16_01);
|
||||
int16x4_t src16_2 = vget_low_s16(src16_23);
|
||||
|
||||
accum0 += vmull_s16(src16_0, coeff16);
|
||||
accum1 += vmull_s16(src16_1, coeff16);
|
||||
accum2 += vmull_s16(src16_2, coeff16);
|
||||
}
|
||||
|
||||
accum0 = vshrq_n_s32(accum0, SkConvolutionFilter1D::kShiftBits);
|
||||
accum1 = vshrq_n_s32(accum1, SkConvolutionFilter1D::kShiftBits);
|
||||
accum2 = vshrq_n_s32(accum2, SkConvolutionFilter1D::kShiftBits);
|
||||
|
||||
int16x8_t accum16_0 = vcombine_s16(vqmovn_s32(accum0), vqmovn_s32(accum1));
|
||||
int16x8_t accum16_1 = vcombine_s16(vqmovn_s32(accum2), vqmovn_s32(accum2));
|
||||
|
||||
uint8x16_t accum8 =
|
||||
vcombine_u8(vqmovun_s16(accum16_0), vqmovun_s16(accum16_1));
|
||||
|
||||
if (hasAlpha) {
|
||||
// Compute the max(ri, gi, bi) for each pixel.
|
||||
// [8] xx a3 b3 g3 xx a2 b2 g2 xx a1 b1 g1 xx a0 b0 g0
|
||||
uint8x16_t a =
|
||||
vreinterpretq_u8_u32(vshrq_n_u32(vreinterpretq_u32_u8(accum8), 8));
|
||||
// [8] xx xx xx max3 xx xx xx max2 xx xx xx max1 xx xx xx max0
|
||||
uint8x16_t b = vmaxq_u8(a, accum8); // Max of r and g
|
||||
// [8] xx xx a3 b3 xx xx a2 b2 xx xx a1 b1 xx xx a0 b0
|
||||
a = vreinterpretq_u8_u32(vshrq_n_u32(vreinterpretq_u32_u8(accum8), 16));
|
||||
// [8] xx xx xx max3 xx xx xx max2 xx xx xx max1 xx xx xx max0
|
||||
b = vmaxq_u8(a, b); // Max of r and g and b.
|
||||
// [8] max3 00 00 00 max2 00 00 00 max1 00 00 00 max0 00 00 00
|
||||
b = vreinterpretq_u8_u32(vshlq_n_u32(vreinterpretq_u32_u8(b), 24));
|
||||
// Make sure the value of alpha channel is always larger than maximum
|
||||
// value of color channels.
|
||||
accum8 = vmaxq_u8(b, accum8);
|
||||
} else {
|
||||
// Set value of alpha channels to 0xFF.
|
||||
accum8 = vreinterpretq_u8_u32(vreinterpretq_u32_u8(accum8) |
|
||||
vdupq_n_u32(0xFF000000));
|
||||
}
|
||||
|
||||
switch (r) {
|
||||
case 1:
|
||||
vst1q_lane_u32(reinterpret_cast<uint32_t*>(outRow),
|
||||
vreinterpretq_u32_u8(accum8), 0);
|
||||
break;
|
||||
case 2:
|
||||
vst1_u32(reinterpret_cast<uint32_t*>(outRow),
|
||||
vreinterpret_u32_u8(vget_low_u8(accum8)));
|
||||
break;
|
||||
case 3:
|
||||
vst1_u32(reinterpret_cast<uint32_t*>(outRow),
|
||||
vreinterpret_u32_u8(vget_low_u8(accum8)));
|
||||
vst1q_lane_u32(reinterpret_cast<uint32_t*>(outRow + 8),
|
||||
vreinterpretq_u32_u8(accum8), 2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void convolve_vertically_neon(
|
||||
const SkConvolutionFilter1D::ConvolutionFixed* filterValues,
|
||||
int filterLength, unsigned char* const* sourceDataRows, int pixelWidth,
|
||||
unsigned char* outRow, bool hasAlpha) {
|
||||
if (hasAlpha) {
|
||||
ConvolveVertically<true>(filterValues, filterLength, sourceDataRows,
|
||||
pixelWidth, outRow);
|
||||
} else {
|
||||
ConvolveVertically<false>(filterValues, filterLength, sourceDataRows,
|
||||
pixelWidth, outRow);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace skia
|
|
@ -0,0 +1,304 @@
|
|||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
||||
// Copyright (c) 2011-2016 Google Inc.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the gfx/skia/LICENSE file.
|
||||
|
||||
#include "SkConvolver.h"
|
||||
#include "mozilla/Attributes.h"
|
||||
#include <immintrin.h>
|
||||
|
||||
namespace skia {
|
||||
|
||||
static MOZ_ALWAYS_INLINE void AccumRemainder(
|
||||
const unsigned char* pixelsLeft,
|
||||
const SkConvolutionFilter1D::ConvolutionFixed* filterValues, __m128i& accum,
|
||||
int r) {
|
||||
int remainder[4] = {0};
|
||||
for (int i = 0; i < r; i++) {
|
||||
SkConvolutionFilter1D::ConvolutionFixed coeff = filterValues[i];
|
||||
remainder[0] += coeff * pixelsLeft[i * 4 + 0];
|
||||
remainder[1] += coeff * pixelsLeft[i * 4 + 1];
|
||||
remainder[2] += coeff * pixelsLeft[i * 4 + 2];
|
||||
remainder[3] += coeff * pixelsLeft[i * 4 + 3];
|
||||
}
|
||||
__m128i t =
|
||||
_mm_setr_epi32(remainder[0], remainder[1], remainder[2], remainder[3]);
|
||||
accum = _mm_add_epi32(accum, t);
|
||||
}
|
||||
|
||||
// Convolves horizontally along a single row. The row data is given in
|
||||
// |srcData| and continues for the numValues() of the filter.
|
||||
void convolve_horizontally_sse2(const unsigned char* srcData,
|
||||
const SkConvolutionFilter1D& filter,
|
||||
unsigned char* outRow, bool /*hasAlpha*/) {
|
||||
// Output one pixel each iteration, calculating all channels (RGBA) together.
|
||||
int numValues = filter.numValues();
|
||||
for (int outX = 0; outX < numValues; outX++) {
|
||||
// Get the filter that determines the current output pixel.
|
||||
int filterOffset, filterLength;
|
||||
const SkConvolutionFilter1D::ConvolutionFixed* filterValues =
|
||||
filter.FilterForValue(outX, &filterOffset, &filterLength);
|
||||
|
||||
// Compute the first pixel in this row that the filter affects. It will
|
||||
// touch |filterLength| pixels (4 bytes each) after this.
|
||||
const unsigned char* rowToFilter = &srcData[filterOffset * 4];
|
||||
|
||||
__m128i zero = _mm_setzero_si128();
|
||||
__m128i accum = _mm_setzero_si128();
|
||||
|
||||
// We will load and accumulate with four coefficients per iteration.
|
||||
for (int filterX = 0; filterX < filterLength >> 2; filterX++) {
|
||||
// Load 4 coefficients => duplicate 1st and 2nd of them for all channels.
|
||||
__m128i coeff, coeff16;
|
||||
// [16] xx xx xx xx c3 c2 c1 c0
|
||||
coeff = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(filterValues));
|
||||
// [16] xx xx xx xx c1 c1 c0 c0
|
||||
coeff16 = _mm_shufflelo_epi16(coeff, _MM_SHUFFLE(1, 1, 0, 0));
|
||||
// [16] c1 c1 c1 c1 c0 c0 c0 c0
|
||||
coeff16 = _mm_unpacklo_epi16(coeff16, coeff16);
|
||||
|
||||
// Load four pixels => unpack the first two pixels to 16 bits =>
|
||||
// multiply with coefficients => accumulate the convolution result.
|
||||
// [8] a3 b3 g3 r3 a2 b2 g2 r2 a1 b1 g1 r1 a0 b0 g0 r0
|
||||
__m128i src8 =
|
||||
_mm_loadu_si128(reinterpret_cast<const __m128i*>(rowToFilter));
|
||||
// [16] a1 b1 g1 r1 a0 b0 g0 r0
|
||||
__m128i src16 = _mm_unpacklo_epi8(src8, zero);
|
||||
__m128i mul_hi = _mm_mulhi_epi16(src16, coeff16);
|
||||
__m128i mul_lo = _mm_mullo_epi16(src16, coeff16);
|
||||
// [32] a0*c0 b0*c0 g0*c0 r0*c0
|
||||
__m128i t = _mm_unpacklo_epi16(mul_lo, mul_hi);
|
||||
accum = _mm_add_epi32(accum, t);
|
||||
// [32] a1*c1 b1*c1 g1*c1 r1*c1
|
||||
t = _mm_unpackhi_epi16(mul_lo, mul_hi);
|
||||
accum = _mm_add_epi32(accum, t);
|
||||
|
||||
// Duplicate 3rd and 4th coefficients for all channels =>
|
||||
// unpack the 3rd and 4th pixels to 16 bits => multiply with coefficients
|
||||
// => accumulate the convolution results.
|
||||
// [16] xx xx xx xx c3 c3 c2 c2
|
||||
coeff16 = _mm_shufflelo_epi16(coeff, _MM_SHUFFLE(3, 3, 2, 2));
|
||||
// [16] c3 c3 c3 c3 c2 c2 c2 c2
|
||||
coeff16 = _mm_unpacklo_epi16(coeff16, coeff16);
|
||||
// [16] a3 g3 b3 r3 a2 g2 b2 r2
|
||||
src16 = _mm_unpackhi_epi8(src8, zero);
|
||||
mul_hi = _mm_mulhi_epi16(src16, coeff16);
|
||||
mul_lo = _mm_mullo_epi16(src16, coeff16);
|
||||
// [32] a2*c2 b2*c2 g2*c2 r2*c2
|
||||
t = _mm_unpacklo_epi16(mul_lo, mul_hi);
|
||||
accum = _mm_add_epi32(accum, t);
|
||||
// [32] a3*c3 b3*c3 g3*c3 r3*c3
|
||||
t = _mm_unpackhi_epi16(mul_lo, mul_hi);
|
||||
accum = _mm_add_epi32(accum, t);
|
||||
|
||||
// Advance the pixel and coefficients pointers.
|
||||
rowToFilter += 16;
|
||||
filterValues += 4;
|
||||
}
|
||||
|
||||
// When |filterLength| is not divisible by 4, we accumulate the last 1 - 3
|
||||
// coefficients one at a time.
|
||||
int r = filterLength & 3;
|
||||
if (r) {
|
||||
int remainderOffset = (filterOffset + filterLength - r) * 4;
|
||||
AccumRemainder(srcData + remainderOffset, filterValues, accum, r);
|
||||
}
|
||||
|
||||
// Shift right for fixed point implementation.
|
||||
accum = _mm_srai_epi32(accum, SkConvolutionFilter1D::kShiftBits);
|
||||
|
||||
// Packing 32 bits |accum| to 16 bits per channel (signed saturation).
|
||||
accum = _mm_packs_epi32(accum, zero);
|
||||
// Packing 16 bits |accum| to 8 bits per channel (unsigned saturation).
|
||||
accum = _mm_packus_epi16(accum, zero);
|
||||
|
||||
// Store the pixel value of 32 bits.
|
||||
*(reinterpret_cast<int*>(outRow)) = _mm_cvtsi128_si32(accum);
|
||||
outRow += 4;
|
||||
}
|
||||
}
|
||||
|
||||
// Does vertical convolution to produce one output row. The filter values and
|
||||
// length are given in the first two parameters. These are applied to each
|
||||
// of the rows pointed to in the |sourceDataRows| array, with each row
|
||||
// being |pixelWidth| wide.
|
||||
//
|
||||
// The output must have room for |pixelWidth * 4| bytes.
|
||||
template <bool hasAlpha>
|
||||
static void ConvolveVertically(
|
||||
const SkConvolutionFilter1D::ConvolutionFixed* filterValues,
|
||||
int filterLength, unsigned char* const* sourceDataRows, int pixelWidth,
|
||||
unsigned char* outRow) {
|
||||
// Output four pixels per iteration (16 bytes).
|
||||
int width = pixelWidth & ~3;
|
||||
__m128i zero = _mm_setzero_si128();
|
||||
for (int outX = 0; outX < width; outX += 4) {
|
||||
// Accumulated result for each pixel. 32 bits per RGBA channel.
|
||||
__m128i accum0 = _mm_setzero_si128();
|
||||
__m128i accum1 = _mm_setzero_si128();
|
||||
__m128i accum2 = _mm_setzero_si128();
|
||||
__m128i accum3 = _mm_setzero_si128();
|
||||
|
||||
// Convolve with one filter coefficient per iteration.
|
||||
for (int filterY = 0; filterY < filterLength; filterY++) {
|
||||
// Duplicate the filter coefficient 8 times.
|
||||
// [16] cj cj cj cj cj cj cj cj
|
||||
__m128i coeff16 = _mm_set1_epi16(filterValues[filterY]);
|
||||
|
||||
// Load four pixels (16 bytes) together.
|
||||
// [8] a3 b3 g3 r3 a2 b2 g2 r2 a1 b1 g1 r1 a0 b0 g0 r0
|
||||
const __m128i* src =
|
||||
reinterpret_cast<const __m128i*>(&sourceDataRows[filterY][outX << 2]);
|
||||
__m128i src8 = _mm_loadu_si128(src);
|
||||
|
||||
// Unpack 1st and 2nd pixels from 8 bits to 16 bits for each channels =>
|
||||
// multiply with current coefficient => accumulate the result.
|
||||
// [16] a1 b1 g1 r1 a0 b0 g0 r0
|
||||
__m128i src16 = _mm_unpacklo_epi8(src8, zero);
|
||||
__m128i mul_hi = _mm_mulhi_epi16(src16, coeff16);
|
||||
__m128i mul_lo = _mm_mullo_epi16(src16, coeff16);
|
||||
// [32] a0 b0 g0 r0
|
||||
__m128i t = _mm_unpacklo_epi16(mul_lo, mul_hi);
|
||||
accum0 = _mm_add_epi32(accum0, t);
|
||||
// [32] a1 b1 g1 r1
|
||||
t = _mm_unpackhi_epi16(mul_lo, mul_hi);
|
||||
accum1 = _mm_add_epi32(accum1, t);
|
||||
|
||||
// Unpack 3rd and 4th pixels from 8 bits to 16 bits for each channels =>
|
||||
// multiply with current coefficient => accumulate the result.
|
||||
// [16] a3 b3 g3 r3 a2 b2 g2 r2
|
||||
src16 = _mm_unpackhi_epi8(src8, zero);
|
||||
mul_hi = _mm_mulhi_epi16(src16, coeff16);
|
||||
mul_lo = _mm_mullo_epi16(src16, coeff16);
|
||||
// [32] a2 b2 g2 r2
|
||||
t = _mm_unpacklo_epi16(mul_lo, mul_hi);
|
||||
accum2 = _mm_add_epi32(accum2, t);
|
||||
// [32] a3 b3 g3 r3
|
||||
t = _mm_unpackhi_epi16(mul_lo, mul_hi);
|
||||
accum3 = _mm_add_epi32(accum3, t);
|
||||
}
|
||||
|
||||
// Shift right for fixed point implementation.
|
||||
accum0 = _mm_srai_epi32(accum0, SkConvolutionFilter1D::kShiftBits);
|
||||
accum1 = _mm_srai_epi32(accum1, SkConvolutionFilter1D::kShiftBits);
|
||||
accum2 = _mm_srai_epi32(accum2, SkConvolutionFilter1D::kShiftBits);
|
||||
accum3 = _mm_srai_epi32(accum3, SkConvolutionFilter1D::kShiftBits);
|
||||
|
||||
// Packing 32 bits |accum| to 16 bits per channel (signed saturation).
|
||||
// [16] a1 b1 g1 r1 a0 b0 g0 r0
|
||||
accum0 = _mm_packs_epi32(accum0, accum1);
|
||||
// [16] a3 b3 g3 r3 a2 b2 g2 r2
|
||||
accum2 = _mm_packs_epi32(accum2, accum3);
|
||||
|
||||
// Packing 16 bits |accum| to 8 bits per channel (unsigned saturation).
|
||||
// [8] a3 b3 g3 r3 a2 b2 g2 r2 a1 b1 g1 r1 a0 b0 g0 r0
|
||||
accum0 = _mm_packus_epi16(accum0, accum2);
|
||||
|
||||
if (hasAlpha) {
|
||||
// Compute the max(ri, gi, bi) for each pixel.
|
||||
// [8] xx a3 b3 g3 xx a2 b2 g2 xx a1 b1 g1 xx a0 b0 g0
|
||||
__m128i a = _mm_srli_epi32(accum0, 8);
|
||||
// [8] xx xx xx max3 xx xx xx max2 xx xx xx max1 xx xx xx max0
|
||||
__m128i b = _mm_max_epu8(a, accum0); // Max of r and g.
|
||||
// [8] xx xx a3 b3 xx xx a2 b2 xx xx a1 b1 xx xx a0 b0
|
||||
a = _mm_srli_epi32(accum0, 16);
|
||||
// [8] xx xx xx max3 xx xx xx max2 xx xx xx max1 xx xx xx max0
|
||||
b = _mm_max_epu8(a, b); // Max of r and g and b.
|
||||
// [8] max3 00 00 00 max2 00 00 00 max1 00 00 00 max0 00 00 00
|
||||
b = _mm_slli_epi32(b, 24);
|
||||
|
||||
// Make sure the value of alpha channel is always larger than maximum
|
||||
// value of color channels.
|
||||
accum0 = _mm_max_epu8(b, accum0);
|
||||
} else {
|
||||
// Set value of alpha channels to 0xFF.
|
||||
__m128i mask = _mm_set1_epi32(0xff000000);
|
||||
accum0 = _mm_or_si128(accum0, mask);
|
||||
}
|
||||
|
||||
// Store the convolution result (16 bytes) and advance the pixel pointers.
|
||||
_mm_storeu_si128(reinterpret_cast<__m128i*>(outRow), accum0);
|
||||
outRow += 16;
|
||||
}
|
||||
|
||||
// When the width of the output is not divisible by 4, We need to save one
|
||||
// pixel (4 bytes) each time. And also the fourth pixel is always absent.
|
||||
int r = pixelWidth & 3;
|
||||
if (r) {
|
||||
__m128i accum0 = _mm_setzero_si128();
|
||||
__m128i accum1 = _mm_setzero_si128();
|
||||
__m128i accum2 = _mm_setzero_si128();
|
||||
for (int filterY = 0; filterY < filterLength; ++filterY) {
|
||||
__m128i coeff16 = _mm_set1_epi16(filterValues[filterY]);
|
||||
// [8] a3 b3 g3 r3 a2 b2 g2 r2 a1 b1 g1 r1 a0 b0 g0 r0
|
||||
const __m128i* src = reinterpret_cast<const __m128i*>(
|
||||
&sourceDataRows[filterY][width << 2]);
|
||||
__m128i src8 = _mm_loadu_si128(src);
|
||||
// [16] a1 b1 g1 r1 a0 b0 g0 r0
|
||||
__m128i src16 = _mm_unpacklo_epi8(src8, zero);
|
||||
__m128i mul_hi = _mm_mulhi_epi16(src16, coeff16);
|
||||
__m128i mul_lo = _mm_mullo_epi16(src16, coeff16);
|
||||
// [32] a0 b0 g0 r0
|
||||
__m128i t = _mm_unpacklo_epi16(mul_lo, mul_hi);
|
||||
accum0 = _mm_add_epi32(accum0, t);
|
||||
// [32] a1 b1 g1 r1
|
||||
t = _mm_unpackhi_epi16(mul_lo, mul_hi);
|
||||
accum1 = _mm_add_epi32(accum1, t);
|
||||
// [16] a3 b3 g3 r3 a2 b2 g2 r2
|
||||
src16 = _mm_unpackhi_epi8(src8, zero);
|
||||
mul_hi = _mm_mulhi_epi16(src16, coeff16);
|
||||
mul_lo = _mm_mullo_epi16(src16, coeff16);
|
||||
// [32] a2 b2 g2 r2
|
||||
t = _mm_unpacklo_epi16(mul_lo, mul_hi);
|
||||
accum2 = _mm_add_epi32(accum2, t);
|
||||
}
|
||||
|
||||
accum0 = _mm_srai_epi32(accum0, SkConvolutionFilter1D::kShiftBits);
|
||||
accum1 = _mm_srai_epi32(accum1, SkConvolutionFilter1D::kShiftBits);
|
||||
accum2 = _mm_srai_epi32(accum2, SkConvolutionFilter1D::kShiftBits);
|
||||
// [16] a1 b1 g1 r1 a0 b0 g0 r0
|
||||
accum0 = _mm_packs_epi32(accum0, accum1);
|
||||
// [16] a3 b3 g3 r3 a2 b2 g2 r2
|
||||
accum2 = _mm_packs_epi32(accum2, zero);
|
||||
// [8] a3 b3 g3 r3 a2 b2 g2 r2 a1 b1 g1 r1 a0 b0 g0 r0
|
||||
accum0 = _mm_packus_epi16(accum0, accum2);
|
||||
if (hasAlpha) {
|
||||
// [8] xx a3 b3 g3 xx a2 b2 g2 xx a1 b1 g1 xx a0 b0 g0
|
||||
__m128i a = _mm_srli_epi32(accum0, 8);
|
||||
// [8] xx xx xx max3 xx xx xx max2 xx xx xx max1 xx xx xx max0
|
||||
__m128i b = _mm_max_epu8(a, accum0); // Max of r and g.
|
||||
// [8] xx xx a3 b3 xx xx a2 b2 xx xx a1 b1 xx xx a0 b0
|
||||
a = _mm_srli_epi32(accum0, 16);
|
||||
// [8] xx xx xx max3 xx xx xx max2 xx xx xx max1 xx xx xx max0
|
||||
b = _mm_max_epu8(a, b); // Max of r and g and b.
|
||||
// [8] max3 00 00 00 max2 00 00 00 max1 00 00 00 max0 00 00 00
|
||||
b = _mm_slli_epi32(b, 24);
|
||||
accum0 = _mm_max_epu8(b, accum0);
|
||||
} else {
|
||||
__m128i mask = _mm_set1_epi32(0xff000000);
|
||||
accum0 = _mm_or_si128(accum0, mask);
|
||||
}
|
||||
|
||||
for (int i = 0; i < r; i++) {
|
||||
*(reinterpret_cast<int*>(outRow)) = _mm_cvtsi128_si32(accum0);
|
||||
accum0 = _mm_srli_si128(accum0, 4);
|
||||
outRow += 4;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void convolve_vertically_sse2(
|
||||
const SkConvolutionFilter1D::ConvolutionFixed* filterValues,
|
||||
int filterLength, unsigned char* const* sourceDataRows, int pixelWidth,
|
||||
unsigned char* outRow, bool hasAlpha) {
|
||||
if (hasAlpha) {
|
||||
ConvolveVertically<true>(filterValues, filterLength, sourceDataRows,
|
||||
pixelWidth, outRow);
|
||||
} else {
|
||||
ConvolveVertically<false>(filterValues, filterLength, sourceDataRows,
|
||||
pixelWidth, outRow);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace skia
|
|
@ -1,39 +0,0 @@
|
|||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "Scale.h"
|
||||
|
||||
#include "HelpersSkia.h"
|
||||
#include "skia/src/core/SkBitmapScaler.h"
|
||||
|
||||
namespace mozilla {
|
||||
namespace gfx {
|
||||
|
||||
bool Scale(uint8_t* srcData, int32_t srcWidth, int32_t srcHeight,
|
||||
int32_t srcStride, uint8_t* dstData, int32_t dstWidth,
|
||||
int32_t dstHeight, int32_t dstStride, SurfaceFormat format) {
|
||||
SkPixmap srcPixmap(MakeSkiaImageInfo(IntSize(srcWidth, srcHeight), format),
|
||||
srcData, srcStride);
|
||||
|
||||
// Rescaler is compatible with N32 only. Convert to N32 if needed.
|
||||
SkBitmap tmpBitmap;
|
||||
if (srcPixmap.colorType() != kN32_SkColorType) {
|
||||
if (!tmpBitmap.tryAllocPixels(
|
||||
SkImageInfo::MakeN32Premul(srcWidth, srcHeight)) ||
|
||||
!tmpBitmap.writePixels(srcPixmap) ||
|
||||
!tmpBitmap.peekPixels(&srcPixmap)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
SkPixmap dstPixmap(SkImageInfo::MakeN32Premul(dstWidth, dstHeight), dstData,
|
||||
dstStride);
|
||||
return SkBitmapScaler::Resize(dstPixmap, srcPixmap,
|
||||
SkBitmapScaler::RESIZE_LANCZOS3);
|
||||
}
|
||||
|
||||
} // namespace gfx
|
||||
} // namespace mozilla
|
|
@ -0,0 +1,559 @@
|
|||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
||||
// Copyright (c) 2011-2016 Google Inc.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the gfx/skia/LICENSE file.
|
||||
|
||||
#include "SkConvolver.h"
|
||||
#include "mozilla/Vector.h"
|
||||
|
||||
#ifdef USE_SSE2
|
||||
# include "mozilla/SSE.h"
|
||||
#endif
|
||||
|
||||
#ifdef USE_NEON
|
||||
# include "mozilla/arm.h"
|
||||
#endif
|
||||
|
||||
namespace skia {
|
||||
|
||||
// Converts the argument to an 8-bit unsigned value by clamping to the range
|
||||
// 0-255.
|
||||
static inline unsigned char ClampTo8(int a) {
|
||||
if (static_cast<unsigned>(a) < 256) {
|
||||
return a; // Avoid the extra check in the common case.
|
||||
}
|
||||
if (a < 0) {
|
||||
return 0;
|
||||
}
|
||||
return 255;
|
||||
}
|
||||
|
||||
// Convolves horizontally along a single row. The row data is given in
|
||||
// |srcData| and continues for the numValues() of the filter.
|
||||
template <bool hasAlpha>
|
||||
void ConvolveHorizontally(const unsigned char* srcData,
|
||||
const SkConvolutionFilter1D& filter,
|
||||
unsigned char* outRow) {
|
||||
// Loop over each pixel on this row in the output image.
|
||||
int numValues = filter.numValues();
|
||||
for (int outX = 0; outX < numValues; outX++) {
|
||||
// Get the filter that determines the current output pixel.
|
||||
int filterOffset, filterLength;
|
||||
const SkConvolutionFilter1D::ConvolutionFixed* filterValues =
|
||||
filter.FilterForValue(outX, &filterOffset, &filterLength);
|
||||
|
||||
// Compute the first pixel in this row that the filter affects. It will
|
||||
// touch |filterLength| pixels (4 bytes each) after this.
|
||||
const unsigned char* rowToFilter = &srcData[filterOffset * 4];
|
||||
|
||||
// Apply the filter to the row to get the destination pixel in |accum|.
|
||||
int accum[4] = {0};
|
||||
for (int filterX = 0; filterX < filterLength; filterX++) {
|
||||
SkConvolutionFilter1D::ConvolutionFixed curFilter = filterValues[filterX];
|
||||
accum[0] += curFilter * rowToFilter[filterX * 4 + 0];
|
||||
accum[1] += curFilter * rowToFilter[filterX * 4 + 1];
|
||||
accum[2] += curFilter * rowToFilter[filterX * 4 + 2];
|
||||
if (hasAlpha) {
|
||||
accum[3] += curFilter * rowToFilter[filterX * 4 + 3];
|
||||
}
|
||||
}
|
||||
|
||||
// Bring this value back in range. All of the filter scaling factors
|
||||
// are in fixed point with kShiftBits bits of fractional part.
|
||||
accum[0] >>= SkConvolutionFilter1D::kShiftBits;
|
||||
accum[1] >>= SkConvolutionFilter1D::kShiftBits;
|
||||
accum[2] >>= SkConvolutionFilter1D::kShiftBits;
|
||||
|
||||
if (hasAlpha) {
|
||||
accum[3] >>= SkConvolutionFilter1D::kShiftBits;
|
||||
}
|
||||
|
||||
// Store the new pixel.
|
||||
outRow[outX * 4 + 0] = ClampTo8(accum[0]);
|
||||
outRow[outX * 4 + 1] = ClampTo8(accum[1]);
|
||||
outRow[outX * 4 + 2] = ClampTo8(accum[2]);
|
||||
if (hasAlpha) {
|
||||
outRow[outX * 4 + 3] = ClampTo8(accum[3]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Does vertical convolution to produce one output row. The filter values and
|
||||
// length are given in the first two parameters. These are applied to each
|
||||
// of the rows pointed to in the |sourceDataRows| array, with each row
|
||||
// being |pixelWidth| wide.
|
||||
//
|
||||
// The output must have room for |pixelWidth * 4| bytes.
|
||||
template <bool hasAlpha>
|
||||
void ConvolveVertically(
|
||||
const SkConvolutionFilter1D::ConvolutionFixed* filterValues,
|
||||
int filterLength, unsigned char* const* sourceDataRows, int pixelWidth,
|
||||
unsigned char* outRow) {
|
||||
// We go through each column in the output and do a vertical convolution,
|
||||
// generating one output pixel each time.
|
||||
for (int outX = 0; outX < pixelWidth; outX++) {
|
||||
// Compute the number of bytes over in each row that the current column
|
||||
// we're convolving starts at. The pixel will cover the next 4 bytes.
|
||||
int byteOffset = outX * 4;
|
||||
|
||||
// Apply the filter to one column of pixels.
|
||||
int accum[4] = {0};
|
||||
for (int filterY = 0; filterY < filterLength; filterY++) {
|
||||
SkConvolutionFilter1D::ConvolutionFixed curFilter = filterValues[filterY];
|
||||
accum[0] += curFilter * sourceDataRows[filterY][byteOffset + 0];
|
||||
accum[1] += curFilter * sourceDataRows[filterY][byteOffset + 1];
|
||||
accum[2] += curFilter * sourceDataRows[filterY][byteOffset + 2];
|
||||
if (hasAlpha) {
|
||||
accum[3] += curFilter * sourceDataRows[filterY][byteOffset + 3];
|
||||
}
|
||||
}
|
||||
|
||||
// Bring this value back in range. All of the filter scaling factors
|
||||
// are in fixed point with kShiftBits bits of precision.
|
||||
accum[0] >>= SkConvolutionFilter1D::kShiftBits;
|
||||
accum[1] >>= SkConvolutionFilter1D::kShiftBits;
|
||||
accum[2] >>= SkConvolutionFilter1D::kShiftBits;
|
||||
if (hasAlpha) {
|
||||
accum[3] >>= SkConvolutionFilter1D::kShiftBits;
|
||||
}
|
||||
|
||||
// Store the new pixel.
|
||||
outRow[byteOffset + 0] = ClampTo8(accum[0]);
|
||||
outRow[byteOffset + 1] = ClampTo8(accum[1]);
|
||||
outRow[byteOffset + 2] = ClampTo8(accum[2]);
|
||||
|
||||
if (hasAlpha) {
|
||||
unsigned char alpha = ClampTo8(accum[3]);
|
||||
|
||||
// Make sure the alpha channel doesn't come out smaller than any of the
|
||||
// color channels. We use premultipled alpha channels, so this should
|
||||
// never happen, but rounding errors will cause this from time to time.
|
||||
// These "impossible" colors will cause overflows (and hence random pixel
|
||||
// values) when the resulting bitmap is drawn to the screen.
|
||||
//
|
||||
// We only need to do this when generating the final output row (here).
|
||||
int maxColorChannel =
|
||||
std::max(outRow[byteOffset + 0],
|
||||
std::max(outRow[byteOffset + 1], outRow[byteOffset + 2]));
|
||||
if (alpha < maxColorChannel) {
|
||||
outRow[byteOffset + 3] = maxColorChannel;
|
||||
} else {
|
||||
outRow[byteOffset + 3] = alpha;
|
||||
}
|
||||
} else {
|
||||
// No alpha channel, the image is opaque.
|
||||
outRow[byteOffset + 3] = 0xff;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USE_SSE2
|
||||
void convolve_vertically_avx2(const int16_t* filter, int filterLen,
|
||||
uint8_t* const* srcRows, int width, uint8_t* out,
|
||||
bool hasAlpha);
|
||||
void convolve_horizontally_sse2(const unsigned char* srcData,
|
||||
const SkConvolutionFilter1D& filter,
|
||||
unsigned char* outRow, bool hasAlpha);
|
||||
void convolve_vertically_sse2(const int16_t* filter, int filterLen,
|
||||
uint8_t* const* srcRows, int width, uint8_t* out,
|
||||
bool hasAlpha);
|
||||
#elif defined(USE_NEON)
|
||||
void convolve_horizontally_neon(const unsigned char* srcData,
|
||||
const SkConvolutionFilter1D& filter,
|
||||
unsigned char* outRow, bool hasAlpha);
|
||||
void convolve_vertically_neon(const int16_t* filter, int filterLen,
|
||||
uint8_t* const* srcRows, int width, uint8_t* out,
|
||||
bool hasAlpha);
|
||||
#endif
|
||||
|
||||
void convolve_horizontally(const unsigned char* srcData,
|
||||
const SkConvolutionFilter1D& filter,
|
||||
unsigned char* outRow, bool hasAlpha) {
|
||||
#ifdef USE_SSE2
|
||||
if (mozilla::supports_sse2()) {
|
||||
convolve_horizontally_sse2(srcData, filter, outRow, hasAlpha);
|
||||
return;
|
||||
}
|
||||
#elif defined(USE_NEON)
|
||||
if (mozilla::supports_neon()) {
|
||||
convolve_horizontally_neon(srcData, filter, outRow, hasAlpha);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
if (hasAlpha) {
|
||||
ConvolveHorizontally<true>(srcData, filter, outRow);
|
||||
} else {
|
||||
ConvolveHorizontally<false>(srcData, filter, outRow);
|
||||
}
|
||||
}
|
||||
|
||||
void convolve_vertically(
|
||||
const SkConvolutionFilter1D::ConvolutionFixed* filterValues,
|
||||
int filterLength, unsigned char* const* sourceDataRows, int pixelWidth,
|
||||
unsigned char* outRow, bool hasAlpha) {
|
||||
#ifdef USE_SSE2
|
||||
if (mozilla::supports_avx2()) {
|
||||
convolve_vertically_avx2(filterValues, filterLength, sourceDataRows,
|
||||
pixelWidth, outRow, hasAlpha);
|
||||
return;
|
||||
}
|
||||
if (mozilla::supports_sse2()) {
|
||||
convolve_vertically_sse2(filterValues, filterLength, sourceDataRows,
|
||||
pixelWidth, outRow, hasAlpha);
|
||||
return;
|
||||
}
|
||||
#elif defined(USE_NEON)
|
||||
if (mozilla::supports_neon()) {
|
||||
convolve_vertically_neon(filterValues, filterLength, sourceDataRows,
|
||||
pixelWidth, outRow, hasAlpha);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
if (hasAlpha) {
|
||||
ConvolveVertically<true>(filterValues, filterLength, sourceDataRows,
|
||||
pixelWidth, outRow);
|
||||
} else {
|
||||
ConvolveVertically<false>(filterValues, filterLength, sourceDataRows,
|
||||
pixelWidth, outRow);
|
||||
}
|
||||
}
|
||||
|
||||
// Stores a list of rows in a circular buffer. The usage is you write into it
|
||||
// by calling AdvanceRow. It will keep track of which row in the buffer it
|
||||
// should use next, and the total number of rows added.
|
||||
class CircularRowBuffer {
|
||||
public:
|
||||
// The number of pixels in each row is given in |sourceRowPixelWidth|.
|
||||
// The maximum number of rows needed in the buffer is |maxYFilterSize|
|
||||
// (we only need to store enough rows for the biggest filter).
|
||||
//
|
||||
// We use the |firstInputRow| to compute the coordinates of all of the
|
||||
// following rows returned by Advance().
|
||||
CircularRowBuffer(int destRowPixelWidth, int maxYFilterSize,
|
||||
int firstInputRow)
|
||||
: fRowByteWidth(destRowPixelWidth * 4),
|
||||
fNumRows(maxYFilterSize),
|
||||
fNextRow(0),
|
||||
fNextRowCoordinate(firstInputRow) {
|
||||
fBuffer.resize(fRowByteWidth * maxYFilterSize);
|
||||
fRowAddresses.resize(fNumRows);
|
||||
}
|
||||
|
||||
// Moves to the next row in the buffer, returning a pointer to the beginning
|
||||
// of it.
|
||||
unsigned char* advanceRow() {
|
||||
unsigned char* row = &fBuffer[fNextRow * fRowByteWidth];
|
||||
fNextRowCoordinate++;
|
||||
|
||||
// Set the pointer to the next row to use, wrapping around if necessary.
|
||||
fNextRow++;
|
||||
if (fNextRow == fNumRows) {
|
||||
fNextRow = 0;
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
// Returns a pointer to an "unrolled" array of rows. These rows will start
|
||||
// at the y coordinate placed into |*firstRowIndex| and will continue in
|
||||
// order for the maximum number of rows in this circular buffer.
|
||||
//
|
||||
// The |firstRowIndex_| may be negative. This means the circular buffer
|
||||
// starts before the top of the image (it hasn't been filled yet).
|
||||
unsigned char* const* GetRowAddresses(int* firstRowIndex) {
|
||||
// Example for a 4-element circular buffer holding coords 6-9.
|
||||
// Row 0 Coord 8
|
||||
// Row 1 Coord 9
|
||||
// Row 2 Coord 6 <- fNextRow = 2, fNextRowCoordinate = 10.
|
||||
// Row 3 Coord 7
|
||||
//
|
||||
// The "next" row is also the first (lowest) coordinate. This computation
|
||||
// may yield a negative value, but that's OK, the math will work out
|
||||
// since the user of this buffer will compute the offset relative
|
||||
// to the firstRowIndex and the negative rows will never be used.
|
||||
*firstRowIndex = fNextRowCoordinate - fNumRows;
|
||||
|
||||
int curRow = fNextRow;
|
||||
for (int i = 0; i < fNumRows; i++) {
|
||||
fRowAddresses[i] = &fBuffer[curRow * fRowByteWidth];
|
||||
|
||||
// Advance to the next row, wrapping if necessary.
|
||||
curRow++;
|
||||
if (curRow == fNumRows) {
|
||||
curRow = 0;
|
||||
}
|
||||
}
|
||||
return &fRowAddresses[0];
|
||||
}
|
||||
|
||||
private:
|
||||
// The buffer storing the rows. They are packed, each one fRowByteWidth.
|
||||
std::vector<unsigned char> fBuffer;
|
||||
|
||||
// Number of bytes per row in the |buffer|.
|
||||
int fRowByteWidth;
|
||||
|
||||
// The number of rows available in the buffer.
|
||||
int fNumRows;
|
||||
|
||||
// The next row index we should write into. This wraps around as the
|
||||
// circular buffer is used.
|
||||
int fNextRow;
|
||||
|
||||
// The y coordinate of the |fNextRow|. This is incremented each time a
|
||||
// new row is appended and does not wrap.
|
||||
int fNextRowCoordinate;
|
||||
|
||||
// Buffer used by GetRowAddresses().
|
||||
std::vector<unsigned char*> fRowAddresses;
|
||||
};
|
||||
|
||||
SkConvolutionFilter1D::SkConvolutionFilter1D() : fMaxFilter(0) {}
|
||||
|
||||
SkConvolutionFilter1D::~SkConvolutionFilter1D() = default;
|
||||
|
||||
void SkConvolutionFilter1D::AddFilter(int filterOffset,
|
||||
const ConvolutionFixed* filterValues,
|
||||
int filterLength) {
|
||||
// It is common for leading/trailing filter values to be zeros. In such
|
||||
// cases it is beneficial to only store the central factors.
|
||||
// For a scaling to 1/4th in each dimension using a Lanczos-2 filter on
|
||||
// a 1080p image this optimization gives a ~10% speed improvement.
|
||||
int filterSize = filterLength;
|
||||
int firstNonZero = 0;
|
||||
while (firstNonZero < filterLength && filterValues[firstNonZero] == 0) {
|
||||
firstNonZero++;
|
||||
}
|
||||
|
||||
if (firstNonZero < filterLength) {
|
||||
// Here we have at least one non-zero factor.
|
||||
int lastNonZero = filterLength - 1;
|
||||
while (lastNonZero >= 0 && filterValues[lastNonZero] == 0) {
|
||||
lastNonZero--;
|
||||
}
|
||||
|
||||
filterOffset += firstNonZero;
|
||||
filterLength = lastNonZero + 1 - firstNonZero;
|
||||
MOZ_ASSERT(filterLength > 0);
|
||||
|
||||
fFilterValues.insert(fFilterValues.end(), &filterValues[firstNonZero],
|
||||
&filterValues[lastNonZero + 1]);
|
||||
} else {
|
||||
// Here all the factors were zeroes.
|
||||
filterLength = 0;
|
||||
}
|
||||
|
||||
FilterInstance instance = {
|
||||
// We pushed filterLength elements onto fFilterValues
|
||||
int(fFilterValues.size()) - filterLength, filterOffset, filterLength,
|
||||
filterSize};
|
||||
fFilters.push_back(instance);
|
||||
|
||||
fMaxFilter = std::max(fMaxFilter, filterLength);
|
||||
}
|
||||
|
||||
bool SkConvolutionFilter1D::ComputeFilterValues(
|
||||
const SkBitmapFilter& aBitmapFilter, int32_t aSrcSize, int32_t aDstSize) {
|
||||
// When we're doing a magnification, the scale will be larger than one. This
|
||||
// means the destination pixels are much smaller than the source pixels, and
|
||||
// that the range covered by the filter won't necessarily cover any source
|
||||
// pixel boundaries. Therefore, we use these clamped values (max of 1) for
|
||||
// some computations.
|
||||
float scale = float(aDstSize) / float(aSrcSize);
|
||||
float clampedScale = std::min(1.0f, scale);
|
||||
// This is how many source pixels from the center we need to count
|
||||
// to support the filtering function.
|
||||
float srcSupport = aBitmapFilter.width() / clampedScale;
|
||||
float invScale = 1.0f / scale;
|
||||
|
||||
mozilla::Vector<float, 64> filterValues;
|
||||
mozilla::Vector<ConvolutionFixed, 64> fixedFilterValues;
|
||||
|
||||
// Loop over all pixels in the output range. We will generate one set of
|
||||
// filter values for each one. Those values will tell us how to blend the
|
||||
// source pixels to compute the destination pixel.
|
||||
|
||||
// This value is computed based on how SkTDArray::resizeStorageToAtLeast works
|
||||
// in order to ensure that it does not overflow or assert. That functions
|
||||
// computes
|
||||
// n+4 + (n+4)/4
|
||||
// and we want to to fit in a 32 bit signed int. Equating that to 2^31-1 and
|
||||
// solving n gives n = (2^31-6)*4/5 = 1717986913.6
|
||||
const int32_t maxToPassToReserveAdditional = 1717986913;
|
||||
|
||||
int32_t filterValueCount = int32_t(ceilf(aDstSize * srcSupport * 2));
|
||||
if (aDstSize > maxToPassToReserveAdditional || filterValueCount < 0 ||
|
||||
filterValueCount > maxToPassToReserveAdditional) {
|
||||
return false;
|
||||
}
|
||||
reserveAdditional(aDstSize, filterValueCount);
|
||||
for (int32_t destI = 0; destI < aDstSize; destI++) {
|
||||
// This is the pixel in the source directly under the pixel in the dest.
|
||||
// Note that we base computations on the "center" of the pixels. To see
|
||||
// why, observe that the destination pixel at coordinates (0, 0) in a 5.0x
|
||||
// downscale should "cover" the pixels around the pixel with *its center*
|
||||
// at coordinates (2.5, 2.5) in the source, not those around (0, 0).
|
||||
// Hence we need to scale coordinates (0.5, 0.5), not (0, 0).
|
||||
float srcPixel = (static_cast<float>(destI) + 0.5f) * invScale;
|
||||
|
||||
// Compute the (inclusive) range of source pixels the filter covers.
|
||||
float srcBegin = std::max(0.0f, floorf(srcPixel - srcSupport));
|
||||
float srcEnd = std::min(aSrcSize - 1.0f, ceilf(srcPixel + srcSupport));
|
||||
|
||||
// Compute the unnormalized filter value at each location of the source
|
||||
// it covers.
|
||||
|
||||
// Sum of the filter values for normalizing.
|
||||
// Distance from the center of the filter, this is the filter coordinate
|
||||
// in source space. We also need to consider the center of the pixel
|
||||
// when comparing distance against 'srcPixel'. In the 5x downscale
|
||||
// example used above the distance from the center of the filter to
|
||||
// the pixel with coordinates (2, 2) should be 0, because its center
|
||||
// is at (2.5, 2.5).
|
||||
int32_t filterCount = int32_t(srcEnd - srcBegin) + 1;
|
||||
if (filterCount <= 0 || !filterValues.resize(filterCount) ||
|
||||
!fixedFilterValues.resize(filterCount)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
float destFilterDist = (srcBegin + 0.5f - srcPixel) * clampedScale;
|
||||
float filterSum = 0.0f;
|
||||
for (int32_t index = 0; index < filterCount; index++) {
|
||||
float filterValue = aBitmapFilter.evaluate(destFilterDist);
|
||||
filterValues[index] = filterValue;
|
||||
filterSum += filterValue;
|
||||
destFilterDist += clampedScale;
|
||||
}
|
||||
|
||||
// The filter must be normalized so that we don't affect the brightness of
|
||||
// the image. Convert to normalized fixed point.
|
||||
ConvolutionFixed fixedSum = 0;
|
||||
float invFilterSum = 1.0f / filterSum;
|
||||
for (int32_t fixedI = 0; fixedI < filterCount; fixedI++) {
|
||||
ConvolutionFixed curFixed = ToFixed(filterValues[fixedI] * invFilterSum);
|
||||
fixedSum += curFixed;
|
||||
fixedFilterValues[fixedI] = curFixed;
|
||||
}
|
||||
|
||||
// The conversion to fixed point will leave some rounding errors, which
|
||||
// we add back in to avoid affecting the brightness of the image. We
|
||||
// arbitrarily add this to the center of the filter array (this won't always
|
||||
// be the center of the filter function since it could get clipped on the
|
||||
// edges, but it doesn't matter enough to worry about that case).
|
||||
ConvolutionFixed leftovers = ToFixed(1) - fixedSum;
|
||||
fixedFilterValues[filterCount / 2] += leftovers;
|
||||
|
||||
AddFilter(int32_t(srcBegin), fixedFilterValues.begin(), filterCount);
|
||||
}
|
||||
|
||||
return maxFilter() > 0 && numValues() == aDstSize;
|
||||
}
|
||||
|
||||
// Does a two-dimensional convolution on the given source image.
|
||||
//
|
||||
// It is assumed the source pixel offsets referenced in the input filters
|
||||
// reference only valid pixels, so the source image size is not required. Each
|
||||
// row of the source image starts |sourceByteRowStride| after the previous
|
||||
// one (this allows you to have rows with some padding at the end).
|
||||
//
|
||||
// The result will be put into the given output buffer. The destination image
|
||||
// size will be xfilter.numValues() * yfilter.numValues() pixels. It will be
|
||||
// in rows of exactly xfilter.numValues() * 4 bytes.
|
||||
//
|
||||
// |sourceHasAlpha| is a hint that allows us to avoid doing computations on
|
||||
// the alpha channel if the image is opaque. If you don't know, set this to
|
||||
// true and it will work properly, but setting this to false will be a few
|
||||
// percent faster if you know the image is opaque.
|
||||
//
|
||||
// The layout in memory is assumed to be 4-bytes per pixel in B-G-R-A order
|
||||
// (this is ARGB when loaded into 32-bit words on a little-endian machine).
|
||||
/**
|
||||
* Returns false if it was unable to perform the convolution/rescale. in which
|
||||
* case the output buffer is assumed to be undefined.
|
||||
*/
|
||||
bool BGRAConvolve2D(const unsigned char* sourceData, int sourceByteRowStride,
|
||||
bool sourceHasAlpha, const SkConvolutionFilter1D& filterX,
|
||||
const SkConvolutionFilter1D& filterY,
|
||||
int outputByteRowStride, unsigned char* output) {
|
||||
int maxYFilterSize = filterY.maxFilter();
|
||||
|
||||
// The next row in the input that we will generate a horizontally
|
||||
// convolved row for. If the filter doesn't start at the beginning of the
|
||||
// image (this is the case when we are only resizing a subset), then we
|
||||
// don't want to generate any output rows before that. Compute the starting
|
||||
// row for convolution as the first pixel for the first vertical filter.
|
||||
int filterOffset = 0, filterLength = 0;
|
||||
const SkConvolutionFilter1D::ConvolutionFixed* filterValues =
|
||||
filterY.FilterForValue(0, &filterOffset, &filterLength);
|
||||
int nextXRow = filterOffset;
|
||||
|
||||
// We loop over each row in the input doing a horizontal convolution. This
|
||||
// will result in a horizontally convolved image. We write the results into
|
||||
// a circular buffer of convolved rows and do vertical convolution as rows
|
||||
// are available. This prevents us from having to store the entire
|
||||
// intermediate image and helps cache coherency.
|
||||
// We will need four extra rows to allow horizontal convolution could be done
|
||||
// simultaneously. We also pad each row in row buffer to be aligned-up to
|
||||
// 32 bytes.
|
||||
// TODO(jiesun): We do not use aligned load from row buffer in vertical
|
||||
// convolution pass yet. Somehow Windows does not like it.
|
||||
int rowBufferWidth = (filterX.numValues() + 31) & ~0x1F;
|
||||
int rowBufferHeight = maxYFilterSize;
|
||||
|
||||
// check for too-big allocation requests : crbug.com/528628
|
||||
{
|
||||
int64_t size = int64_t(rowBufferWidth) * int64_t(rowBufferHeight);
|
||||
// need some limit, to avoid over-committing success from malloc, but then
|
||||
// crashing when we try to actually use the memory.
|
||||
// 100meg seems big enough to allow "normal" zoom factors and image sizes
|
||||
// through while avoiding the crash seen by the bug (crbug.com/528628)
|
||||
if (size > 100 * 1024 * 1024) {
|
||||
// printf_stderr("BGRAConvolve2D: tmp allocation [%lld] too
|
||||
// big\n", size);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
CircularRowBuffer rowBuffer(rowBufferWidth, rowBufferHeight, filterOffset);
|
||||
|
||||
// Loop over every possible output row, processing just enough horizontal
|
||||
// convolutions to run each subsequent vertical convolution.
|
||||
MOZ_ASSERT(outputByteRowStride >= filterX.numValues() * 4);
|
||||
int numOutputRows = filterY.numValues();
|
||||
|
||||
// We need to check which is the last line to convolve before we advance 4
|
||||
// lines in one iteration.
|
||||
int lastFilterOffset, lastFilterLength;
|
||||
filterY.FilterForValue(numOutputRows - 1, &lastFilterOffset,
|
||||
&lastFilterLength);
|
||||
|
||||
for (int outY = 0; outY < numOutputRows; outY++) {
|
||||
filterValues = filterY.FilterForValue(outY, &filterOffset, &filterLength);
|
||||
|
||||
// Generate output rows until we have enough to run the current filter.
|
||||
while (nextXRow < filterOffset + filterLength) {
|
||||
convolve_horizontally(
|
||||
&sourceData[(uint64_t)nextXRow * sourceByteRowStride], filterX,
|
||||
rowBuffer.advanceRow(), sourceHasAlpha);
|
||||
nextXRow++;
|
||||
}
|
||||
|
||||
// Compute where in the output image this row of final data will go.
|
||||
unsigned char* curOutputRow = &output[(uint64_t)outY * outputByteRowStride];
|
||||
|
||||
// Get the list of rows that the circular buffer has, in order.
|
||||
int firstRowInCircularBuffer;
|
||||
unsigned char* const* rowsToConvolve =
|
||||
rowBuffer.GetRowAddresses(&firstRowInCircularBuffer);
|
||||
|
||||
// Now compute the start of the subset of those rows that the filter needs.
|
||||
unsigned char* const* firstRowForFilter =
|
||||
&rowsToConvolve[filterOffset - firstRowInCircularBuffer];
|
||||
|
||||
convolve_vertically(filterValues, filterLength, firstRowForFilter,
|
||||
filterX.numValues(), curOutputRow, sourceHasAlpha);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace skia
|
|
@ -0,0 +1,169 @@
|
|||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
||||
// Copyright (c) 2011-2016 Google Inc.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the gfx/skia/LICENSE file.
|
||||
|
||||
#ifndef MOZILLA_GFX_SKCONVOLVER_H_
|
||||
#define MOZILLA_GFX_SKCONVOLVER_H_
|
||||
|
||||
#include "mozilla/Assertions.h"
|
||||
#include <cfloat>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
|
||||
namespace skia {
|
||||
|
||||
class SkBitmapFilter {
|
||||
public:
|
||||
explicit SkBitmapFilter(float width) : fWidth(width) {}
|
||||
virtual ~SkBitmapFilter() = default;
|
||||
|
||||
float width() const { return fWidth; }
|
||||
virtual float evaluate(float x) const = 0;
|
||||
|
||||
protected:
|
||||
float fWidth;
|
||||
};
|
||||
|
||||
class SkBoxFilter final : public SkBitmapFilter {
|
||||
public:
|
||||
explicit SkBoxFilter(float width = 0.5f) : SkBitmapFilter(width) {}
|
||||
|
||||
float evaluate(float x) const override {
|
||||
return (x >= -fWidth && x < fWidth) ? 1.0f : 0.0f;
|
||||
}
|
||||
};
|
||||
|
||||
class SkLanczosFilter final : public SkBitmapFilter {
|
||||
public:
|
||||
explicit SkLanczosFilter(float width = 3.0f) : SkBitmapFilter(width) {}
|
||||
|
||||
float evaluate(float x) const override {
|
||||
if (x <= -fWidth || x >= fWidth) {
|
||||
return 0.0f; // Outside of the window.
|
||||
}
|
||||
if (x > -FLT_EPSILON && x < FLT_EPSILON) {
|
||||
return 1.0f; // Special case the discontinuity at the origin.
|
||||
}
|
||||
float xpi = x * float(M_PI);
|
||||
return (sinf(xpi) / xpi) * // sinc(x)
|
||||
sinf(xpi / fWidth) / (xpi / fWidth); // sinc(x/fWidth)
|
||||
}
|
||||
};
|
||||
|
||||
// Represents a filter in one dimension. Each output pixel has one entry in this
|
||||
// object for the filter values contributing to it. You build up the filter
|
||||
// list by calling AddFilter for each output pixel (in order).
|
||||
//
|
||||
// We do 2-dimensional convolution by first convolving each row by one
|
||||
// SkConvolutionFilter1D, then convolving each column by another one.
|
||||
//
|
||||
// Entries are stored in ConvolutionFixed point, shifted left by kShiftBits.
|
||||
class SkConvolutionFilter1D {
|
||||
public:
|
||||
using ConvolutionFixed = short;
|
||||
|
||||
// The number of bits that ConvolutionFixed point values are shifted by.
|
||||
enum { kShiftBits = 14 };
|
||||
|
||||
SkConvolutionFilter1D();
|
||||
~SkConvolutionFilter1D();
|
||||
|
||||
// Convert between floating point and our ConvolutionFixed point
|
||||
// representation.
|
||||
static ConvolutionFixed ToFixed(float f) {
|
||||
return static_cast<ConvolutionFixed>(f * (1 << kShiftBits));
|
||||
}
|
||||
|
||||
// Returns the maximum pixel span of a filter.
|
||||
int maxFilter() const { return fMaxFilter; }
|
||||
|
||||
// Returns the number of filters in this filter. This is the dimension of the
|
||||
// output image.
|
||||
int numValues() const { return static_cast<int>(fFilters.size()); }
|
||||
|
||||
void reserveAdditional(int filterCount, int filterValueCount) {
|
||||
fFilters.reserve(fFilters.size() + filterCount);
|
||||
fFilterValues.reserve(fFilterValues.size() + filterValueCount);
|
||||
}
|
||||
|
||||
// Appends the given list of scaling values for generating a given output
|
||||
// pixel. |filterOffset| is the distance from the edge of the image to where
|
||||
// the scaling factors start. The scaling factors apply to the source pixels
|
||||
// starting from this position, and going for the next |filterLength| pixels.
|
||||
//
|
||||
// You will probably want to make sure your input is normalized (that is,
|
||||
// all entries in |filterValuesg| sub to one) to prevent affecting the overall
|
||||
// brighness of the image.
|
||||
//
|
||||
// The filterLength must be > 0.
|
||||
void AddFilter(int filterOffset, const ConvolutionFixed* filterValues,
|
||||
int filterLength);
|
||||
|
||||
// Retrieves a filter for the given |valueOffset|, a position in the output
|
||||
// image in the direction we're convolving. The offset and length of the
|
||||
// filter values are put into the corresponding out arguments (see AddFilter
|
||||
// above for what these mean), and a pointer to the first scaling factor is
|
||||
// returned. There will be |filterLength| values in this array.
|
||||
inline const ConvolutionFixed* FilterForValue(int valueOffset,
|
||||
int* filterOffset,
|
||||
int* filterLength) const {
|
||||
const FilterInstance& filter = fFilters[valueOffset];
|
||||
*filterOffset = filter.fOffset;
|
||||
*filterLength = filter.fTrimmedLength;
|
||||
if (filter.fTrimmedLength == 0) {
|
||||
return nullptr;
|
||||
}
|
||||
return &fFilterValues[filter.fDataLocation];
|
||||
}
|
||||
|
||||
bool ComputeFilterValues(const SkBitmapFilter& aBitmapFilter,
|
||||
int32_t aSrcSize, int32_t aDstSize);
|
||||
|
||||
private:
|
||||
struct FilterInstance {
|
||||
// Offset within filterValues for this instance of the filter.
|
||||
int fDataLocation;
|
||||
|
||||
// Distance from the left of the filter to the center. IN PIXELS
|
||||
int fOffset;
|
||||
|
||||
// Number of values in this filter instance.
|
||||
int fTrimmedLength;
|
||||
|
||||
// Filter length as specified. Note that this may be different from
|
||||
// 'trimmed_length' if leading/trailing zeros of the original floating
|
||||
// point form were clipped differently on each tail.
|
||||
int fLength;
|
||||
};
|
||||
|
||||
// Stores the information for each filter added to this class.
|
||||
std::vector<FilterInstance> fFilters;
|
||||
|
||||
// We store all the filter values in this flat list, indexed by
|
||||
// |FilterInstance.data_location| to avoid the mallocs required for storing
|
||||
// each one separately.
|
||||
std::vector<ConvolutionFixed> fFilterValues;
|
||||
|
||||
// The maximum size of any filter we've added.
|
||||
int fMaxFilter;
|
||||
};
|
||||
|
||||
void convolve_horizontally(const unsigned char* srcData,
|
||||
const SkConvolutionFilter1D& filter,
|
||||
unsigned char* outRow, bool hasAlpha);
|
||||
|
||||
void convolve_vertically(
|
||||
const SkConvolutionFilter1D::ConvolutionFixed* filterValues,
|
||||
int filterLength, unsigned char* const* sourceDataRows, int pixelWidth,
|
||||
unsigned char* outRow, bool hasAlpha);
|
||||
|
||||
bool BGRAConvolve2D(const unsigned char* sourceData, int sourceByteRowStride,
|
||||
bool sourceHasAlpha, const SkConvolutionFilter1D& filterX,
|
||||
const SkConvolutionFilter1D& filterY,
|
||||
int outputByteRowStride, unsigned char* output);
|
||||
|
||||
} // namespace skia
|
||||
|
||||
#endif /* MOZILLA_GFX_SKCONVOLVER_H_ */
|
|
@ -123,6 +123,8 @@ EXPORTS.mozilla.gfx += [
|
|||
if CONFIG["INTEL_ARCHITECTURE"]:
|
||||
SOURCES += [
|
||||
"BlurSSE2.cpp",
|
||||
"ConvolutionFilterAVX2.cpp",
|
||||
"ConvolutionFilterSSE2.cpp",
|
||||
"FilterProcessingSSE2.cpp",
|
||||
"ImageScalingSSE2.cpp",
|
||||
"ssse3-scaler.c",
|
||||
|
@ -134,6 +136,8 @@ if CONFIG["INTEL_ARCHITECTURE"]:
|
|||
# The file uses SSE2 intrinsics, so it needs special compile flags on some
|
||||
# compilers.
|
||||
SOURCES["BlurSSE2.cpp"].flags += CONFIG["SSE2_FLAGS"]
|
||||
SOURCES["ConvolutionFilterAVX2.cpp"].flags += ["-mavx2"]
|
||||
SOURCES["ConvolutionFilterSSE2.cpp"].flags += CONFIG["SSE2_FLAGS"]
|
||||
SOURCES["FilterProcessingSSE2.cpp"].flags += CONFIG["SSE2_FLAGS"]
|
||||
SOURCES["ImageScalingSSE2.cpp"].flags += CONFIG["SSE2_FLAGS"]
|
||||
SOURCES["SwizzleAVX2.cpp"].flags += ["-mavx2"]
|
||||
|
@ -173,9 +177,9 @@ UNIFIED_SOURCES += [
|
|||
"PathSkia.cpp",
|
||||
"Quaternion.cpp",
|
||||
"RecordedEvent.cpp",
|
||||
"Scale.cpp",
|
||||
"ScaledFontBase.cpp",
|
||||
"SFNTData.cpp",
|
||||
"SkConvolver.cpp",
|
||||
"SourceSurfaceCairo.cpp",
|
||||
"SourceSurfaceRawData.cpp",
|
||||
"SourceSurfaceSkia.cpp",
|
||||
|
@ -199,11 +203,13 @@ if CONFIG["MOZ_WIDGET_TOOLKIT"] == "cocoa":
|
|||
if CONFIG["CPU_ARCH"] == "aarch64" or CONFIG["BUILD_ARM_NEON"]:
|
||||
SOURCES += [
|
||||
"BlurNEON.cpp",
|
||||
"ConvolutionFilterNEON.cpp",
|
||||
"LuminanceNEON.cpp",
|
||||
"SwizzleNEON.cpp",
|
||||
]
|
||||
DEFINES["USE_NEON"] = True
|
||||
SOURCES["BlurNEON.cpp"].flags += CONFIG["NEON_FLAGS"]
|
||||
SOURCES["ConvolutionFilterNEON.cpp"].flags += CONFIG["NEON_FLAGS"]
|
||||
SOURCES["LuminanceNEON.cpp"].flags += CONFIG["NEON_FLAGS"]
|
||||
SOURCES["SwizzleNEON.cpp"].flags += CONFIG["NEON_FLAGS"]
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче