rsCpuIntrinsicConvolve3x3.cpp revision 4cca49b13db92b13ca07c1d330ad450d1b10f507
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 RsdCpuScriptIntrinsicConvolve3x3 : 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 ~RsdCpuScriptIntrinsicConvolve3x3();
37    RsdCpuScriptIntrinsicConvolve3x3(RsdCpuReferenceImpl *ctx, const Script *s);
38
39protected:
40    float fp[16];
41    short ip[16];
42    ObjectBaseRef<Allocation> alloc;
43
44    static void kernel(const RsForEachStubParamStruct *p,
45                       uint32_t xstart, uint32_t xend,
46                       uint32_t instep, uint32_t outstep);
47};
48
49}
50}
51
52
53void RsdCpuScriptIntrinsicConvolve3x3::setGlobalObj(uint32_t slot, ObjectBase *data) {
54    rsAssert(slot == 1);
55    alloc.set(static_cast<Allocation *>(data));
56}
57
58void RsdCpuScriptIntrinsicConvolve3x3::setGlobalVar(uint32_t slot, const void *data,
59                                                    size_t dataLength) {
60    rsAssert(slot == 0);
61    memcpy (&fp, data, dataLength);
62    for(int ct=0; ct < 9; ct++) {
63        ip[ct] = (short)(fp[ct] * 255.f + 0.5f);
64    }
65}
66
67extern "C" void rsdIntrinsicConvolve3x3_K(void *dst, const void *y0, const void *y1,
68                                          const void *y2, const short *coef, uint32_t count);
69
70
71static void ConvolveOne(const RsForEachStubParamStruct *p, uint32_t x, uchar4 *out,
72                        const uchar4 *py0, const uchar4 *py1, const uchar4 *py2,
73                        const float* coeff) {
74
75    uint32_t x1 = rsMax((int32_t)x-1, 0);
76    uint32_t x2 = rsMin((int32_t)x+1, (int32_t)p->dimX-1);
77
78    float4 px = convert_float4(py0[x1]) * coeff[0] +
79                convert_float4(py0[x]) * coeff[1] +
80                convert_float4(py0[x2]) * coeff[2] +
81                convert_float4(py1[x1]) * coeff[3] +
82                convert_float4(py1[x]) * coeff[4] +
83                convert_float4(py1[x2]) * coeff[5] +
84                convert_float4(py2[x1]) * coeff[6] +
85                convert_float4(py2[x]) * coeff[7] +
86                convert_float4(py2[x2]) * coeff[8];
87
88    px = clamp(px, 0.f, 255.f);
89    uchar4 o = {(uchar)px.x, (uchar)px.y, (uchar)px.z, (uchar)px.w};
90    *out = o;
91}
92
93void RsdCpuScriptIntrinsicConvolve3x3::kernel(const RsForEachStubParamStruct *p,
94                                              uint32_t xstart, uint32_t xend,
95                                              uint32_t instep, uint32_t outstep) {
96    RsdCpuScriptIntrinsicConvolve3x3 *cp = (RsdCpuScriptIntrinsicConvolve3x3 *)p->usr;
97
98    if (!cp->alloc.get()) {
99        ALOGE("Convolve3x3 executed without input, skipping");
100        return;
101    }
102    const uchar *pin = (const uchar *)cp->alloc->mHal.drvState.lod[0].mallocPtr;
103    const size_t stride = cp->alloc->mHal.drvState.lod[0].stride;
104
105    uint32_t y1 = rsMin((int32_t)p->y + 1, (int32_t)(p->dimY-1));
106    uint32_t y2 = rsMax((int32_t)p->y - 1, 0);
107    const uchar4 *py0 = (const uchar4 *)(pin + stride * y2);
108    const uchar4 *py1 = (const uchar4 *)(pin + stride * p->y);
109    const uchar4 *py2 = (const uchar4 *)(pin + stride * y1);
110
111    uchar4 *out = (uchar4 *)p->out;
112    uint32_t x1 = xstart;
113    uint32_t x2 = xend;
114    if(x1 == 0) {
115        ConvolveOne(p, 0, out, py0, py1, py2, cp->fp);
116        x1 ++;
117        out++;
118    }
119
120    if(x2 > x1) {
121#if defined(ARCH_ARM_HAVE_NEON)
122        int32_t len = (x2 - x1 - 1) >> 1;
123        if(len > 0) {
124            rsdIntrinsicConvolve3x3_K(out, &py0[x1-1], &py1[x1-1], &py2[x1-1], cp->ip, len);
125            x1 += len << 1;
126            out += len << 1;
127        }
128#endif
129
130        while(x1 != x2) {
131            ConvolveOne(p, x1, out, py0, py1, py2, cp->fp);
132            out++;
133            x1++;
134        }
135    }
136}
137
138RsdCpuScriptIntrinsicConvolve3x3::RsdCpuScriptIntrinsicConvolve3x3(
139            RsdCpuReferenceImpl *ctx, const Script *s)
140            : RsdCpuScriptIntrinsic(ctx, s, RS_SCRIPT_INTRINSIC_ID_CONVOLVE_3x3) {
141
142    mRootPtr = &kernel;
143    for(int ct=0; ct < 9; ct++) {
144        fp[ct] = 1.f / 9.f;
145        ip[ct] = (short)(fp[ct] * 255.f + 0.5f);
146    }
147}
148
149RsdCpuScriptIntrinsicConvolve3x3::~RsdCpuScriptIntrinsicConvolve3x3() {
150}
151
152void RsdCpuScriptIntrinsicConvolve3x3::populateScript(Script *s) {
153    s->mHal.info.exportedVariableCount = 2;
154}
155
156void RsdCpuScriptIntrinsicConvolve3x3::invokeFreeChildren() {
157    alloc.clear();
158}
159
160
161RsdCpuScriptImpl * rsdIntrinsic_Convolve3x3(RsdCpuReferenceImpl *ctx, const Script *s) {
162
163    return new RsdCpuScriptIntrinsicConvolve3x3(ctx, s);
164}
165
166
167