rsCpuIntrinsicConvolve5x5.cpp revision c905efd76fdcc1b8846b229bf7d991d185a7b4b7
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18#include "rsCpuIntrinsic.h"
19#include "rsCpuIntrinsicInlines.h"
20
21using namespace android;
22using namespace android::renderscript;
23
24namespace android {
25namespace renderscript {
26
27
28class RsdCpuScriptIntrinsicConvolve5x5 : public RsdCpuScriptIntrinsic {
29public:
30    virtual void populateScript(Script *);
31    virtual void invokeFreeChildren();
32
33    virtual void setGlobalVar(uint32_t slot, const void *data, size_t dataLength);
34    virtual void setGlobalObj(uint32_t slot, ObjectBase *data);
35
36    virtual ~RsdCpuScriptIntrinsicConvolve5x5();
37    RsdCpuScriptIntrinsicConvolve5x5(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e);
38
39protected:
40    float fp[28];
41    short ip[28];
42    ObjectBaseRef<Allocation> alloc;
43
44
45    static void kernel(const RsForEachStubParamStruct *p,
46                       uint32_t xstart, uint32_t xend,
47                       uint32_t instep, uint32_t outstep);
48
49
50};
51
52}
53}
54
55void RsdCpuScriptIntrinsicConvolve5x5::setGlobalObj(uint32_t slot, ObjectBase *data) {
56    rsAssert(slot == 1);
57    alloc.set(static_cast<Allocation *>(data));
58}
59
60void RsdCpuScriptIntrinsicConvolve5x5::setGlobalVar(uint32_t slot,
61                                                    const void *data, size_t dataLength) {
62    rsAssert(slot == 0);
63    memcpy (&fp, data, dataLength);
64    for(int ct=0; ct < 25; ct++) {
65        ip[ct] = (short)(fp[ct] * 255.f + 0.5f);
66    }
67}
68
69
70static void One(const RsForEachStubParamStruct *p, uint32_t x, uchar4 *out,
71                const uchar4 *py0, const uchar4 *py1, const uchar4 *py2, const uchar4 *py3, const uchar4 *py4,
72                const float* coeff) {
73
74    uint32_t x0 = rsMax((int32_t)x-2, 0);
75    uint32_t x1 = rsMax((int32_t)x-1, 0);
76    uint32_t x2 = x;
77    uint32_t x3 = rsMin((int32_t)x+1, (int32_t)(p->dimX-1));
78    uint32_t x4 = rsMin((int32_t)x+2, (int32_t)(p->dimX-1));
79
80    float4 px = convert_float4(py0[x0]) * coeff[0] +
81                convert_float4(py0[x1]) * coeff[1] +
82                convert_float4(py0[x2]) * coeff[2] +
83                convert_float4(py0[x3]) * coeff[3] +
84                convert_float4(py0[x4]) * coeff[4] +
85
86                convert_float4(py1[x0]) * coeff[5] +
87                convert_float4(py1[x1]) * coeff[6] +
88                convert_float4(py1[x2]) * coeff[7] +
89                convert_float4(py1[x3]) * coeff[8] +
90                convert_float4(py1[x4]) * coeff[9] +
91
92                convert_float4(py2[x0]) * coeff[10] +
93                convert_float4(py2[x1]) * coeff[11] +
94                convert_float4(py2[x2]) * coeff[12] +
95                convert_float4(py2[x3]) * coeff[13] +
96                convert_float4(py2[x4]) * coeff[14] +
97
98                convert_float4(py3[x0]) * coeff[15] +
99                convert_float4(py3[x1]) * coeff[16] +
100                convert_float4(py3[x2]) * coeff[17] +
101                convert_float4(py3[x3]) * coeff[18] +
102                convert_float4(py3[x4]) * coeff[19] +
103
104                convert_float4(py4[x0]) * coeff[20] +
105                convert_float4(py4[x1]) * coeff[21] +
106                convert_float4(py4[x2]) * coeff[22] +
107                convert_float4(py4[x3]) * coeff[23] +
108                convert_float4(py4[x4]) * coeff[24];
109
110    px = clamp(px, 0.f, 255.f);
111    uchar4 o = {(uchar)px.x, (uchar)px.y, (uchar)px.z, (uchar)px.w};
112    *out = o;
113}
114
115extern "C" void rsdIntrinsicConvolve5x5_K(void *dst, const void *y0, const void *y1,
116                                          const void *y2, const void *y3, const void *y4,
117                                          const short *coef, uint32_t count);
118
119void RsdCpuScriptIntrinsicConvolve5x5::kernel(const RsForEachStubParamStruct *p,
120                                              uint32_t xstart, uint32_t xend,
121                                              uint32_t instep, uint32_t outstep) {
122    RsdCpuScriptIntrinsicConvolve5x5 *cp = (RsdCpuScriptIntrinsicConvolve5x5 *)p->usr;
123    if (!cp->alloc.get()) {
124        ALOGE("Convolve5x5 executed without input, skipping");
125        return;
126    }
127    const uchar *pin = (const uchar *)cp->alloc->mHal.drvState.lod[0].mallocPtr;
128    const size_t stride = cp->alloc->mHal.drvState.lod[0].stride;
129
130    uint32_t y0 = rsMax((int32_t)p->y-2, 0);
131    uint32_t y1 = rsMax((int32_t)p->y-1, 0);
132    uint32_t y2 = p->y;
133    uint32_t y3 = rsMin((int32_t)p->y+1, (int32_t)(p->dimY-1));
134    uint32_t y4 = rsMin((int32_t)p->y+2, (int32_t)(p->dimY-1));
135
136    const uchar4 *py0 = (const uchar4 *)(pin + stride * y0);
137    const uchar4 *py1 = (const uchar4 *)(pin + stride * y1);
138    const uchar4 *py2 = (const uchar4 *)(pin + stride * y2);
139    const uchar4 *py3 = (const uchar4 *)(pin + stride * y3);
140    const uchar4 *py4 = (const uchar4 *)(pin + stride * y4);
141
142    uchar4 *out = (uchar4 *)p->out;
143    uint32_t x1 = xstart;
144    uint32_t x2 = xend;
145
146    while((x1 < x2) && (x1 < 2)) {
147        One(p, x1, out, py0, py1, py2, py3, py4, cp->fp);
148        out++;
149        x1++;
150    }
151
152#if defined(ARCH_ARM_HAVE_NEON)
153    if((x1 + 3) < x2) {
154        uint32_t len = (x2 - x1 - 3) >> 1;
155        rsdIntrinsicConvolve5x5_K(out, py0, py1, py2, py3, py4, cp->ip, len);
156        out += len << 1;
157        x1 += len << 1;
158    }
159#endif
160
161    while(x1 < x2) {
162        One(p, x1, out, py0, py1, py2, py3, py4, cp->fp);
163        out++;
164        x1++;
165    }
166}
167
168
169RsdCpuScriptIntrinsicConvolve5x5::RsdCpuScriptIntrinsicConvolve5x5(
170            RsdCpuReferenceImpl *ctx, const Script *s, const Element *e)
171            : RsdCpuScriptIntrinsic(ctx, s, e, RS_SCRIPT_INTRINSIC_ID_CONVOLVE_5x5) {
172
173    mRootPtr = &kernel;
174    for(int ct=0; ct < 9; ct++) {
175        fp[ct] = 1.f / 25.f;
176        ip[ct] = (short)(fp[ct] * 255.f + 0.5f);
177    }
178}
179
180RsdCpuScriptIntrinsicConvolve5x5::~RsdCpuScriptIntrinsicConvolve5x5() {
181}
182
183void RsdCpuScriptIntrinsicConvolve5x5::populateScript(Script *s) {
184    s->mHal.info.exportedVariableCount = 2;
185}
186
187void RsdCpuScriptIntrinsicConvolve5x5::invokeFreeChildren() {
188    alloc.clear();
189}
190
191
192RsdCpuScriptImpl * rsdIntrinsic_Convolve5x5(RsdCpuReferenceImpl *ctx,
193                                            const Script *s, const Element *e) {
194
195    return new RsdCpuScriptIntrinsicConvolve5x5(ctx, s, e);
196}
197
198
199
200