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 "rsdCore.h"
19#include "rsdIntrinsics.h"
20#include "rsdAllocation.h"
21
22#include "rsdIntrinsicInlines.h"
23
24using namespace android;
25using namespace android::renderscript;
26
27struct ConvolveParams {
28    float fp[16];
29    short ip[16];
30    ObjectBaseRef<Allocation> alloc;
31};
32
33static void Convolve3x3_Bind(const Context *dc, const Script *script,
34                             void * intrinsicData, uint32_t slot, Allocation *data) {
35    ConvolveParams *cp = (ConvolveParams *)intrinsicData;
36    rsAssert(slot == 1);
37    cp->alloc.set(data);
38}
39
40static void Convolve3x3_SetVar(const Context *dc, const Script *script, void * intrinsicData,
41                               uint32_t slot, void *data, size_t dataLength) {
42    ConvolveParams *cp = (ConvolveParams *)intrinsicData;
43
44    rsAssert(slot == 0);
45    memcpy (cp->fp, data, dataLength);
46    for(int ct=0; ct < 9; ct++) {
47        cp->ip[ct] = (short)(cp->fp[ct] * 255.f + 0.5f);
48    }
49}
50
51extern "C" void rsdIntrinsicConvolve3x3_K(void *dst, const void *y0, const void *y1, const void *y2, const short *coef, uint32_t count);
52
53
54static void ConvolveOne(const RsForEachStubParamStruct *p, uint32_t x, uchar4 *out,
55                        const uchar4 *py0, const uchar4 *py1, const uchar4 *py2,
56                        const float* coeff) {
57
58    uint32_t x1 = rsMax((int32_t)x-1, 0);
59    uint32_t x2 = rsMin((int32_t)x+1, (int32_t)p->dimX-1);
60
61    float4 px = convert_float4(py0[x1]) * coeff[0] +
62                convert_float4(py0[x]) * coeff[1] +
63                convert_float4(py0[x2]) * coeff[2] +
64                convert_float4(py1[x1]) * coeff[3] +
65                convert_float4(py1[x]) * coeff[4] +
66                convert_float4(py1[x2]) * coeff[5] +
67                convert_float4(py2[x1]) * coeff[6] +
68                convert_float4(py2[x]) * coeff[7] +
69                convert_float4(py2[x2]) * coeff[8];
70
71    px = clamp(px, 0.f, 255.f);
72    uchar4 o = {(uchar)px.x, (uchar)px.y, (uchar)px.z, (uchar)px.w};
73    *out = o;
74}
75
76static void Convolve3x3_uchar4(const RsForEachStubParamStruct *p,
77                                    uint32_t xstart, uint32_t xend,
78                                    uint32_t instep, uint32_t outstep) {
79    ConvolveParams *cp = (ConvolveParams *)p->usr;
80
81    if (!cp->alloc.get()) {
82        ALOGE("Convolve3x3 executed without input, skipping");
83        return;
84    }
85    DrvAllocation *din = (DrvAllocation *)cp->alloc->mHal.drv;
86    const uchar *pin = (const uchar *)din->lod[0].mallocPtr;
87
88    uint32_t y1 = rsMin((int32_t)p->y + 1, (int32_t)(p->dimY-1));
89    uint32_t y2 = rsMax((int32_t)p->y - 1, 0);
90    const uchar4 *py0 = (const uchar4 *)(pin + din->lod[0].stride * y2);
91    const uchar4 *py1 = (const uchar4 *)(pin + din->lod[0].stride * p->y);
92    const uchar4 *py2 = (const uchar4 *)(pin + din->lod[0].stride * y1);
93
94    uchar4 *out = (uchar4 *)p->out;
95    uint32_t x1 = xstart;
96    uint32_t x2 = xend;
97    if(x1 == 0) {
98        ConvolveOne(p, 0, out, py0, py1, py2, cp->fp);
99        x1 ++;
100        out++;
101    }
102
103    if(x2 > x1) {
104#if defined(ARCH_ARM_HAVE_NEON)
105        int32_t len = (x2 - x1 - 1) >> 1;
106        if(len > 0) {
107            rsdIntrinsicConvolve3x3_K(out, &py0[x1-1], &py1[x1-1], &py2[x1-1], cp->ip, len);
108            x1 += len << 1;
109            out += len << 1;
110        }
111#endif
112
113        while(x1 != x2) {
114            ConvolveOne(p, x1, out, py0, py1, py2, cp->fp);
115            out++;
116            x1++;
117        }
118    }
119}
120
121void * rsdIntrinsic_InitConvolve3x3(const android::renderscript::Context *dc,
122                                    android::renderscript::Script *script,
123                                    RsdIntriniscFuncs_t *funcs) {
124
125    script->mHal.info.exportedVariableCount = 2;
126    funcs->setVarObj = Convolve3x3_Bind;
127    funcs->setVar = Convolve3x3_SetVar;
128    funcs->root = Convolve3x3_uchar4;
129
130    ConvolveParams *cp = (ConvolveParams *)calloc(1, sizeof(ConvolveParams));
131    for(int ct=0; ct < 9; ct++) {
132        cp->fp[ct] = 1.f / 9.f;
133        cp->ip[ct] = (short)(cp->fp[ct] * 255.f + 0.5f);
134    }
135    return cp;
136}
137
138
139