This source file includes following definitions.
- ClampTo8
- BringBackTo8
- next_row_coordinate_
- AdvanceRow
- GetRowAddresses
- ConvolveHorizontally
- ConvolveVertically
- ConvolveVertically
- AddFilter
- AddFilter
- GetSingleFilter
- SetupSIMD
- BGRAConvolve2D
- SingleChannelConvolveX1D
- SingleChannelConvolveY1D
- SetUpGaussianConvolutionKernel
#include <algorithm>
#include "base/logging.h"
#include "skia/ext/convolver.h"
#include "skia/ext/convolver_SSE2.h"
#include "skia/ext/convolver_mips_dspr2.h"
#include "third_party/skia/include/core/SkSize.h"
#include "third_party/skia/include/core/SkTypes.h"
namespace skia {
namespace {
inline unsigned char ClampTo8(int a) {
if (static_cast<unsigned>(a) < 256)
return a;
if (a < 0)
return 0;
return 255;
}
inline unsigned char BringBackTo8(int a, bool take_absolute) {
a >>= ConvolutionFilter1D::kShiftBits;
if (take_absolute)
a = std::abs(a);
return ClampTo8(a);
}
class CircularRowBuffer {
public:
CircularRowBuffer(int dest_row_pixel_width, int max_y_filter_size,
int first_input_row)
: row_byte_width_(dest_row_pixel_width * 4),
num_rows_(max_y_filter_size),
next_row_(0),
next_row_coordinate_(first_input_row) {
buffer_.resize(row_byte_width_ * max_y_filter_size);
row_addresses_.resize(num_rows_);
}
unsigned char* AdvanceRow() {
unsigned char* row = &buffer_[next_row_ * row_byte_width_];
next_row_coordinate_++;
next_row_++;
if (next_row_ == num_rows_)
next_row_ = 0;
return row;
}
unsigned char* const* GetRowAddresses(int* first_row_index) {
*first_row_index = next_row_coordinate_ - num_rows_;
int cur_row = next_row_;
for (int i = 0; i < num_rows_; i++) {
row_addresses_[i] = &buffer_[cur_row * row_byte_width_];
cur_row++;
if (cur_row == num_rows_)
cur_row = 0;
}
return &row_addresses_[0];
}
private:
std::vector<unsigned char> buffer_;
int row_byte_width_;
int num_rows_;
int next_row_;
int next_row_coordinate_;
std::vector<unsigned char*> row_addresses_;
};
template<bool has_alpha>
void ConvolveHorizontally(const unsigned char* src_data,
const ConvolutionFilter1D& filter,
unsigned char* out_row) {
int num_values = filter.num_values();
for (int out_x = 0; out_x < num_values; out_x++) {
int filter_offset, filter_length;
const ConvolutionFilter1D::Fixed* filter_values =
filter.FilterForValue(out_x, &filter_offset, &filter_length);
const unsigned char* row_to_filter = &src_data[filter_offset * 4];
int accum[4] = {0};
for (int filter_x = 0; filter_x < filter_length; filter_x++) {
ConvolutionFilter1D::Fixed cur_filter = filter_values[filter_x];
accum[0] += cur_filter * row_to_filter[filter_x * 4 + 0];
accum[1] += cur_filter * row_to_filter[filter_x * 4 + 1];
accum[2] += cur_filter * row_to_filter[filter_x * 4 + 2];
if (has_alpha)
accum[3] += cur_filter * row_to_filter[filter_x * 4 + 3];
}
accum[0] >>= ConvolutionFilter1D::kShiftBits;
accum[1] >>= ConvolutionFilter1D::kShiftBits;
accum[2] >>= ConvolutionFilter1D::kShiftBits;
if (has_alpha)
accum[3] >>= ConvolutionFilter1D::kShiftBits;
out_row[out_x * 4 + 0] = ClampTo8(accum[0]);
out_row[out_x * 4 + 1] = ClampTo8(accum[1]);
out_row[out_x * 4 + 2] = ClampTo8(accum[2]);
if (has_alpha)
out_row[out_x * 4 + 3] = ClampTo8(accum[3]);
}
}
template<bool has_alpha>
void ConvolveVertically(const ConvolutionFilter1D::Fixed* filter_values,
int filter_length,
unsigned char* const* source_data_rows,
int pixel_width,
unsigned char* out_row) {
for (int out_x = 0; out_x < pixel_width; out_x++) {
int byte_offset = out_x * 4;
int accum[4] = {0};
for (int filter_y = 0; filter_y < filter_length; filter_y++) {
ConvolutionFilter1D::Fixed cur_filter = filter_values[filter_y];
accum[0] += cur_filter * source_data_rows[filter_y][byte_offset + 0];
accum[1] += cur_filter * source_data_rows[filter_y][byte_offset + 1];
accum[2] += cur_filter * source_data_rows[filter_y][byte_offset + 2];
if (has_alpha)
accum[3] += cur_filter * source_data_rows[filter_y][byte_offset + 3];
}
accum[0] >>= ConvolutionFilter1D::kShiftBits;
accum[1] >>= ConvolutionFilter1D::kShiftBits;
accum[2] >>= ConvolutionFilter1D::kShiftBits;
if (has_alpha)
accum[3] >>= ConvolutionFilter1D::kShiftBits;
out_row[byte_offset + 0] = ClampTo8(accum[0]);
out_row[byte_offset + 1] = ClampTo8(accum[1]);
out_row[byte_offset + 2] = ClampTo8(accum[2]);
if (has_alpha) {
unsigned char alpha = ClampTo8(accum[3]);
int max_color_channel = std::max(out_row[byte_offset + 0],
std::max(out_row[byte_offset + 1], out_row[byte_offset + 2]));
if (alpha < max_color_channel)
out_row[byte_offset + 3] = max_color_channel;
else
out_row[byte_offset + 3] = alpha;
} else {
out_row[byte_offset + 3] = 0xff;
}
}
}
void ConvolveVertically(const ConvolutionFilter1D::Fixed* filter_values,
int filter_length,
unsigned char* const* source_data_rows,
int pixel_width,
unsigned char* out_row,
bool source_has_alpha) {
if (source_has_alpha) {
ConvolveVertically<true>(filter_values, filter_length,
source_data_rows,
pixel_width,
out_row);
} else {
ConvolveVertically<false>(filter_values, filter_length,
source_data_rows,
pixel_width,
out_row);
}
}
}
ConvolutionFilter1D::ConvolutionFilter1D()
: max_filter_(0) {
}
ConvolutionFilter1D::~ConvolutionFilter1D() {
}
void ConvolutionFilter1D::AddFilter(int filter_offset,
const float* filter_values,
int filter_length) {
SkASSERT(filter_length > 0);
std::vector<Fixed> fixed_values;
fixed_values.reserve(filter_length);
for (int i = 0; i < filter_length; ++i)
fixed_values.push_back(FloatToFixed(filter_values[i]));
AddFilter(filter_offset, &fixed_values[0], filter_length);
}
void ConvolutionFilter1D::AddFilter(int filter_offset,
const Fixed* filter_values,
int filter_length) {
int filter_size = filter_length;
int first_non_zero = 0;
while (first_non_zero < filter_length && filter_values[first_non_zero] == 0)
first_non_zero++;
if (first_non_zero < filter_length) {
int last_non_zero = filter_length - 1;
while (last_non_zero >= 0 && filter_values[last_non_zero] == 0)
last_non_zero--;
filter_offset += first_non_zero;
filter_length = last_non_zero + 1 - first_non_zero;
SkASSERT(filter_length > 0);
for (int i = first_non_zero; i <= last_non_zero; i++)
filter_values_.push_back(filter_values[i]);
} else {
filter_length = 0;
}
FilterInstance instance;
instance.data_location = (static_cast<int>(filter_values_.size()) -
filter_length);
instance.offset = filter_offset;
instance.trimmed_length = filter_length;
instance.length = filter_size;
filters_.push_back(instance);
max_filter_ = std::max(max_filter_, filter_length);
}
const ConvolutionFilter1D::Fixed* ConvolutionFilter1D::GetSingleFilter(
int* specified_filter_length,
int* filter_offset,
int* filter_length) const {
const FilterInstance& filter = filters_[0];
*filter_offset = filter.offset;
*filter_length = filter.trimmed_length;
*specified_filter_length = filter.length;
if (filter.trimmed_length == 0)
return NULL;
return &filter_values_[filter.data_location];
}
typedef void (*ConvolveVertically_pointer)(
const ConvolutionFilter1D::Fixed* filter_values,
int filter_length,
unsigned char* const* source_data_rows,
int pixel_width,
unsigned char* out_row,
bool has_alpha);
typedef void (*Convolve4RowsHorizontally_pointer)(
const unsigned char* src_data[4],
const ConvolutionFilter1D& filter,
unsigned char* out_row[4]);
typedef void (*ConvolveHorizontally_pointer)(
const unsigned char* src_data,
const ConvolutionFilter1D& filter,
unsigned char* out_row,
bool has_alpha);
struct ConvolveProcs {
int extra_horizontal_reads;
ConvolveVertically_pointer convolve_vertically;
Convolve4RowsHorizontally_pointer convolve_4rows_horizontally;
ConvolveHorizontally_pointer convolve_horizontally;
};
void SetupSIMD(ConvolveProcs *procs) {
#ifdef SIMD_SSE2
base::CPU cpu;
if (cpu.has_sse2()) {
procs->extra_horizontal_reads = 3;
procs->convolve_vertically = &ConvolveVertically_SSE2;
procs->convolve_4rows_horizontally = &Convolve4RowsHorizontally_SSE2;
procs->convolve_horizontally = &ConvolveHorizontally_SSE2;
}
#elif defined SIMD_MIPS_DSPR2
procs->extra_horizontal_reads = 3;
procs->convolve_vertically = &ConvolveVertically_mips_dspr2;
procs->convolve_horizontally = &ConvolveHorizontally_mips_dspr2;
#endif
}
void BGRAConvolve2D(const unsigned char* source_data,
int source_byte_row_stride,
bool source_has_alpha,
const ConvolutionFilter1D& filter_x,
const ConvolutionFilter1D& filter_y,
int output_byte_row_stride,
unsigned char* output,
bool use_simd_if_possible) {
ConvolveProcs simd;
simd.extra_horizontal_reads = 0;
simd.convolve_vertically = NULL;
simd.convolve_4rows_horizontally = NULL;
simd.convolve_horizontally = NULL;
if (use_simd_if_possible) {
SetupSIMD(&simd);
}
int max_y_filter_size = filter_y.max_filter();
int filter_offset, filter_length;
const ConvolutionFilter1D::Fixed* filter_values =
filter_y.FilterForValue(0, &filter_offset, &filter_length);
int next_x_row = filter_offset;
int row_buffer_width = (filter_x.num_values() + 15) & ~0xF;
int row_buffer_height = max_y_filter_size +
(simd.convolve_4rows_horizontally ? 4 : 0);
CircularRowBuffer row_buffer(row_buffer_width,
row_buffer_height,
filter_offset);
SkASSERT(output_byte_row_stride >= filter_x.num_values() * 4);
int num_output_rows = filter_y.num_values();
int last_filter_offset, last_filter_length;
filter_x.FilterForValue(filter_x.num_values() - 1, &last_filter_offset,
&last_filter_length);
int avoid_simd_rows = 1 + simd.extra_horizontal_reads /
(last_filter_offset + last_filter_length);
filter_y.FilterForValue(num_output_rows - 1, &last_filter_offset,
&last_filter_length);
for (int out_y = 0; out_y < num_output_rows; out_y++) {
filter_values = filter_y.FilterForValue(out_y,
&filter_offset, &filter_length);
while (next_x_row < filter_offset + filter_length) {
if (simd.convolve_4rows_horizontally &&
next_x_row + 3 < last_filter_offset + last_filter_length -
avoid_simd_rows) {
const unsigned char* src[4];
unsigned char* out_row[4];
for (int i = 0; i < 4; ++i) {
src[i] = &source_data[(next_x_row + i) * source_byte_row_stride];
out_row[i] = row_buffer.AdvanceRow();
}
simd.convolve_4rows_horizontally(src, filter_x, out_row);
next_x_row += 4;
} else {
if (simd.convolve_horizontally &&
next_x_row < last_filter_offset + last_filter_length -
avoid_simd_rows) {
simd.convolve_horizontally(
&source_data[next_x_row * source_byte_row_stride],
filter_x, row_buffer.AdvanceRow(), source_has_alpha);
} else {
if (source_has_alpha) {
ConvolveHorizontally<true>(
&source_data[next_x_row * source_byte_row_stride],
filter_x, row_buffer.AdvanceRow());
} else {
ConvolveHorizontally<false>(
&source_data[next_x_row * source_byte_row_stride],
filter_x, row_buffer.AdvanceRow());
}
}
next_x_row++;
}
}
unsigned char* cur_output_row = &output[out_y * output_byte_row_stride];
int first_row_in_circular_buffer;
unsigned char* const* rows_to_convolve =
row_buffer.GetRowAddresses(&first_row_in_circular_buffer);
unsigned char* const* first_row_for_filter =
&rows_to_convolve[filter_offset - first_row_in_circular_buffer];
if (simd.convolve_vertically) {
simd.convolve_vertically(filter_values, filter_length,
first_row_for_filter,
filter_x.num_values(), cur_output_row,
source_has_alpha);
} else {
ConvolveVertically(filter_values, filter_length,
first_row_for_filter,
filter_x.num_values(), cur_output_row,
source_has_alpha);
}
}
}
void SingleChannelConvolveX1D(const unsigned char* source_data,
int source_byte_row_stride,
int input_channel_index,
int input_channel_count,
const ConvolutionFilter1D& filter,
const SkISize& image_size,
unsigned char* output,
int output_byte_row_stride,
int output_channel_index,
int output_channel_count,
bool absolute_values) {
int filter_offset, filter_length, filter_size;
const ConvolutionFilter1D::Fixed* filter_values =
filter.GetSingleFilter(&filter_size, &filter_offset, &filter_length);
if (filter_values == NULL || image_size.width() < filter_size) {
NOTREACHED();
return;
}
int centrepoint = filter_length / 2;
if (filter_size - filter_offset != 2 * filter_offset) {
centrepoint = filter_size / 2 - filter_offset;
}
const unsigned char* source_data_row = source_data;
unsigned char* output_row = output;
for (int r = 0; r < image_size.height(); ++r) {
unsigned char* target_byte = output_row + output_channel_index;
int c = 0;
for (; c < centrepoint; ++c, target_byte += output_channel_count) {
int accval = 0;
int i = 0;
int pixel_byte_index = input_channel_index;
for (; i < centrepoint - c; ++i)
accval += filter_values[i] * source_data_row[pixel_byte_index];
for (; i < filter_length; ++i, pixel_byte_index += input_channel_count)
accval += filter_values[i] * source_data_row[pixel_byte_index];
*target_byte = BringBackTo8(accval, absolute_values);
}
for (; c < image_size.width() - centrepoint;
++c, target_byte += output_channel_count) {
int accval = 0;
int pixel_byte_index = (c - centrepoint) * input_channel_count +
input_channel_index;
for (int i = 0; i < filter_length;
++i, pixel_byte_index += input_channel_count) {
accval += filter_values[i] * source_data_row[pixel_byte_index];
}
*target_byte = BringBackTo8(accval, absolute_values);
}
for (; c < image_size.width(); ++c, target_byte += output_channel_count) {
int accval = 0;
int overlap_taps = image_size.width() - c + centrepoint;
int pixel_byte_index = (c - centrepoint) * input_channel_count +
input_channel_index;
int i = 0;
for (; i < overlap_taps - 1; ++i, pixel_byte_index += input_channel_count)
accval += filter_values[i] * source_data_row[pixel_byte_index];
for (; i < filter_length; ++i)
accval += filter_values[i] * source_data_row[pixel_byte_index];
*target_byte = BringBackTo8(accval, absolute_values);
}
source_data_row += source_byte_row_stride;
output_row += output_byte_row_stride;
}
}
void SingleChannelConvolveY1D(const unsigned char* source_data,
int source_byte_row_stride,
int input_channel_index,
int input_channel_count,
const ConvolutionFilter1D& filter,
const SkISize& image_size,
unsigned char* output,
int output_byte_row_stride,
int output_channel_index,
int output_channel_count,
bool absolute_values) {
int filter_offset, filter_length, filter_size;
const ConvolutionFilter1D::Fixed* filter_values =
filter.GetSingleFilter(&filter_size, &filter_offset, &filter_length);
if (filter_values == NULL || image_size.height() < filter_size) {
NOTREACHED();
return;
}
int centrepoint = filter_length / 2;
if (filter_size - filter_offset != 2 * filter_offset) {
centrepoint = filter_size / 2 - filter_offset;
}
for (int c = 0; c < image_size.width(); ++c) {
unsigned char* target_byte = output + c * output_channel_count +
output_channel_index;
int r = 0;
for (; r < centrepoint; ++r, target_byte += output_byte_row_stride) {
int accval = 0;
int i = 0;
int pixel_byte_index = c * input_channel_count + input_channel_index;
for (; i < centrepoint - r; ++i)
accval += filter_values[i] * source_data[pixel_byte_index];
for (; i < filter_length; ++i, pixel_byte_index += source_byte_row_stride)
accval += filter_values[i] * source_data[pixel_byte_index];
*target_byte = BringBackTo8(accval, absolute_values);
}
for (; r < image_size.height() - centrepoint;
++r, target_byte += output_byte_row_stride) {
int accval = 0;
int pixel_byte_index = (r - centrepoint) * source_byte_row_stride +
c * input_channel_count + input_channel_index;
for (int i = 0; i < filter_length;
++i, pixel_byte_index += source_byte_row_stride) {
accval += filter_values[i] * source_data[pixel_byte_index];
}
*target_byte = BringBackTo8(accval, absolute_values);
}
for (; r < image_size.height();
++r, target_byte += output_byte_row_stride) {
int accval = 0;
int overlap_taps = image_size.height() - r + centrepoint;
int pixel_byte_index = (r - centrepoint) * source_byte_row_stride +
c * input_channel_count + input_channel_index;
int i = 0;
for (; i < overlap_taps - 1;
++i, pixel_byte_index += source_byte_row_stride) {
accval += filter_values[i] * source_data[pixel_byte_index];
}
for (; i < filter_length; ++i)
accval += filter_values[i] * source_data[pixel_byte_index];
*target_byte = BringBackTo8(accval, absolute_values);
}
}
}
void SetUpGaussianConvolutionKernel(ConvolutionFilter1D* filter,
float kernel_sigma,
bool derivative) {
DCHECK(filter != NULL);
DCHECK_GT(kernel_sigma, 0.0);
const int tail_length = static_cast<int>(4.0f * kernel_sigma + 0.5f);
const int kernel_size = tail_length * 2 + 1;
const float sigmasq = kernel_sigma * kernel_sigma;
std::vector<float> kernel_weights(kernel_size, 0.0);
float kernel_sum = 1.0f;
kernel_weights[tail_length] = 1.0f;
for (int ii = 1; ii <= tail_length; ++ii) {
float v = std::exp(-0.5f * ii * ii / sigmasq);
kernel_weights[tail_length + ii] = v;
kernel_weights[tail_length - ii] = v;
kernel_sum += 2.0f * v;
}
for (int i = 0; i < kernel_size; ++i)
kernel_weights[i] /= kernel_sum;
if (derivative) {
kernel_weights[tail_length] = 0.0;
for (int ii = 1; ii <= tail_length; ++ii) {
float v = sigmasq * kernel_weights[tail_length + ii] / ii;
kernel_weights[tail_length + ii] = v;
kernel_weights[tail_length - ii] = -v;
}
}
filter->AddFilter(0, &kernel_weights[0], kernel_weights.size());
}
}