1/*
2 * Copyright 2013 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8
9#include "SkColorPriv.h"
10#include "SkMorphology_opts_SSE2.h"
11
12#include <emmintrin.h>
13
14/* SSE2 version of dilateX, dilateY, erodeX, erodeY.
15 * portable versions are in src/effects/SkMorphologyImageFilter.cpp.
16 */
17
18enum MorphType {
19    kDilate, kErode
20};
21
22enum MorphDirection {
23    kX, kY
24};
25
26template<MorphType type, MorphDirection direction>
27static void SkMorph_SSE2(const SkPMColor* src, SkPMColor* dst, int radius,
28                         int width, int height, int srcStride, int dstStride)
29{
30    const int srcStrideX = direction == kX ? 1 : srcStride;
31    const int dstStrideX = direction == kX ? 1 : dstStride;
32    const int srcStrideY = direction == kX ? srcStride : 1;
33    const int dstStrideY = direction == kX ? dstStride : 1;
34    radius = SkMin32(radius, width - 1);
35    const SkPMColor* upperSrc = src + radius * srcStrideX;
36    for (int x = 0; x < width; ++x) {
37        const SkPMColor* lp = src;
38        const SkPMColor* up = upperSrc;
39        SkPMColor* dptr = dst;
40        for (int y = 0; y < height; ++y) {
41            __m128i max = type == kDilate ? _mm_setzero_si128() : _mm_set1_epi32(0xFFFFFFFF);
42            for (const SkPMColor* p = lp; p <= up; p += srcStrideX) {
43                __m128i src_pixel = _mm_cvtsi32_si128(*p);
44                max = type == kDilate ? _mm_max_epu8(src_pixel, max) : _mm_min_epu8(src_pixel, max);
45            }
46            *dptr = _mm_cvtsi128_si32(max);
47            dptr += dstStrideY;
48            lp += srcStrideY;
49            up += srcStrideY;
50        }
51        if (x >= radius) src += srcStrideX;
52        if (x + radius < width - 1) upperSrc += srcStrideX;
53        dst += dstStrideX;
54    }
55}
56
57void SkDilateX_SSE2(const SkPMColor* src, SkPMColor* dst, int radius,
58                    int width, int height, int srcStride, int dstStride)
59{
60    SkMorph_SSE2<kDilate, kX>(src, dst, radius, width, height, srcStride, dstStride);
61}
62
63void SkErodeX_SSE2(const SkPMColor* src, SkPMColor* dst, int radius,
64                   int width, int height, int srcStride, int dstStride)
65{
66    SkMorph_SSE2<kErode, kX>(src, dst, radius, width, height, srcStride, dstStride);
67}
68
69void SkDilateY_SSE2(const SkPMColor* src, SkPMColor* dst, int radius,
70                    int width, int height, int srcStride, int dstStride)
71{
72    SkMorph_SSE2<kDilate, kY>(src, dst, radius, width, height, srcStride, dstStride);
73}
74
75void SkErodeY_SSE2(const SkPMColor* src, SkPMColor* dst, int radius,
76                   int width, int height, int srcStride, int dstStride)
77{
78    SkMorph_SSE2<kErode, kY>(src, dst, radius, width, height, srcStride, dstStride);
79}
80