rsCpuIntrinsicBlur.cpp revision 709a0978ae141198018ca9769f8d96292a8928e6
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#include "rsCpuIntrinsic.h"
18#include "rsCpuIntrinsicInlines.h"
19
20using namespace android;
21using namespace android::renderscript;
22
23namespace android {
24namespace renderscript {
25
26
27class RsdCpuScriptIntrinsicBlur : public RsdCpuScriptIntrinsic {
28public:
29    virtual void populateScript(Script *);
30    virtual void invokeFreeChildren();
31
32    virtual void setGlobalVar(uint32_t slot, const void *data, size_t dataLength);
33    virtual void setGlobalObj(uint32_t slot, ObjectBase *data);
34
35    virtual ~RsdCpuScriptIntrinsicBlur();
36    RsdCpuScriptIntrinsicBlur(RsdCpuReferenceImpl *ctx, const Script *s);
37
38protected:
39    float fp[104];
40    short ip[104];
41    float radius;
42    int iradius;
43    ObjectBaseRef<Allocation> alloc;
44
45    static void kernel(const RsForEachStubParamStruct *p,
46                       uint32_t xstart, uint32_t xend,
47                       uint32_t instep, uint32_t outstep);
48    void ComputeGaussianWeights();
49};
50
51}
52}
53
54
55void RsdCpuScriptIntrinsicBlur::ComputeGaussianWeights() {
56    // Compute gaussian weights for the blur
57    // e is the euler's number
58    float e = 2.718281828459045f;
59    float pi = 3.1415926535897932f;
60    // g(x) = ( 1 / sqrt( 2 * pi ) * sigma) * e ^ ( -x^2 / 2 * sigma^2 )
61    // x is of the form [-radius .. 0 .. radius]
62    // and sigma varies with radius.
63    // Based on some experimental radius values and sigma's
64    // we approximately fit sigma = f(radius) as
65    // sigma = radius * 0.4  + 0.6
66    // The larger the radius gets, the more our gaussian blur
67    // will resemble a box blur since with large sigma
68    // the gaussian curve begins to lose its shape
69    float sigma = 0.4f * radius + 0.6f;
70
71    // Now compute the coefficients. We will store some redundant values to save
72    // some math during the blur calculations precompute some values
73    float coeff1 = 1.0f / (sqrtf(2.0f * pi) * sigma);
74    float coeff2 = - 1.0f / (2.0f * sigma * sigma);
75
76    float normalizeFactor = 0.0f;
77    float floatR = 0.0f;
78    int r;
79    iradius = (float)ceil(radius) + 0.5f;
80    for (r = -iradius; r <= iradius; r ++) {
81        floatR = (float)r;
82        fp[r + iradius] = coeff1 * powf(e, floatR * floatR * coeff2);
83        normalizeFactor += fp[r + iradius];
84    }
85
86    //Now we need to normalize the weights because all our coefficients need to add up to one
87    normalizeFactor = 1.0f / normalizeFactor;
88    for (r = -iradius; r <= iradius; r ++) {
89        fp[r + iradius] *= normalizeFactor;
90        ip[r + iradius] = (short)(ip[r + iradius] * 32768);
91    }
92}
93
94void RsdCpuScriptIntrinsicBlur::setGlobalObj(uint32_t slot, ObjectBase *data) {
95    rsAssert(slot == 1);
96    alloc.set(static_cast<Allocation *>(data));
97}
98
99void RsdCpuScriptIntrinsicBlur::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
100    rsAssert(slot == 0);
101    radius = ((const float *)data)[0];
102    ComputeGaussianWeights();
103}
104
105
106
107static void OneV(const RsForEachStubParamStruct *p, float4 *out, int32_t x, int32_t y,
108                 const uchar *ptrIn, int iStride, const float* gPtr, int iradius) {
109
110    const uchar *pi = ptrIn + x*4;
111
112    float4 blurredPixel = 0;
113    for (int r = -iradius; r <= iradius; r ++) {
114        int validY = rsMax((y + r), 0);
115        validY = rsMin(validY, (int)(p->dimY - 1));
116        const uchar4 *pvy = (const uchar4 *)&pi[validY * iStride];
117        float4 pf = convert_float4(pvy[0]);
118        blurredPixel += pf * gPtr[0];
119        gPtr++;
120    }
121
122    out->xyzw = blurredPixel;
123}
124
125extern "C" void rsdIntrinsicBlurVF_K(void *dst, const void *pin, int stride, const void *gptr, int rct, int x1, int x2);
126extern "C" void rsdIntrinsicBlurHF_K(void *dst, const void *pin, const void *gptr, int rct, int x1, int x2);
127
128static void OneVF(float4 *out,
129                  const uchar *ptrIn, int iStride, const float* gPtr, int ct,
130                  int x1, int x2) {
131
132#if defined(ARCH_ARM_HAVE_NEON)
133    {
134        int t = (x2 - x1);
135        t &= ~1;
136        if(t) {
137            rsdIntrinsicBlurVF_K(out, ptrIn, iStride, gPtr, ct, x1, x1 + t);
138        }
139        x1 += t;
140    }
141#endif
142
143    while(x2 > x1) {
144        const uchar *pi = ptrIn;
145        float4 blurredPixel = 0;
146        const float* gp = gPtr;
147
148        for (int r = 0; r < ct; r++) {
149            float4 pf = convert_float4(((const uchar4 *)pi)[0]);
150            blurredPixel += pf * gp[0];
151            pi += iStride;
152            gp++;
153        }
154        out->xyzw = blurredPixel;
155        x1++;
156        out++;
157    }
158}
159
160static void OneH(const RsForEachStubParamStruct *p, uchar4 *out, int32_t x,
161                const float4 *ptrIn, const float* gPtr, int iradius) {
162
163    float4 blurredPixel = 0;
164    for (int r = -iradius; r <= iradius; r ++) {
165        int validX = rsMax((x + r), 0);
166        validX = rsMin(validX, (int)(p->dimX - 1));
167        float4 pf = ptrIn[validX];
168        blurredPixel += pf * gPtr[0];
169        gPtr++;
170    }
171
172    out->xyzw = convert_uchar4(blurredPixel);
173}
174
175
176void RsdCpuScriptIntrinsicBlur::kernel(const RsForEachStubParamStruct *p,
177                                       uint32_t xstart, uint32_t xend,
178                                       uint32_t instep, uint32_t outstep) {
179    float buf[4 * 2048];
180    RsdCpuScriptIntrinsicBlur *cp = (RsdCpuScriptIntrinsicBlur *)p->usr;
181    if (!cp->alloc.get()) {
182        ALOGE("Blur executed without input, skipping");
183        return;
184    }
185    const uchar *pin = (const uchar *)cp->alloc->mHal.drvState.lod[0].mallocPtr;
186    const size_t stride = cp->alloc->mHal.drvState.lod[0].stride;
187
188    uchar4 *out = (uchar4 *)p->out;
189    uint32_t x1 = xstart;
190    uint32_t x2 = xend;
191
192    float4 *fout = (float4 *)buf;
193    int y = p->y;
194    if ((y > cp->iradius) && (y < ((int)p->dimY - cp->iradius))) {
195        const uchar *pi = pin + (y - cp->iradius) * stride;
196        OneVF(fout, pi, stride, cp->fp, cp->iradius * 2 + 1, x1, x2);
197    } else {
198        while(x2 > x1) {
199            OneV(p, fout, x1, y, pin, stride, cp->fp, cp->iradius);
200            fout++;
201            x1++;
202        }
203    }
204
205    x1 = xstart;
206    while ((x1 < (uint32_t)cp->iradius) && (x1 < x2)) {
207        OneH(p, out, x1, (float4 *)buf, cp->fp, cp->iradius);
208        out++;
209        x1++;
210    }
211#if defined(ARCH_ARM_HAVE_NEON)
212    if ((x1 + cp->iradius) < x2) {
213        rsdIntrinsicBlurHF_K(out, ((float4 *)buf) - cp->iradius, cp->fp, cp->iradius * 2 + 1, x1, x2 - cp->iradius);
214        out += (x2 - cp->iradius) - x1;
215        x1 = x2 - cp->iradius;
216    }
217#endif
218    while(x2 > x1) {
219        OneH(p, out, x1, (float4 *)buf, cp->fp, cp->iradius);
220        out++;
221        x1++;
222    }
223
224}
225
226RsdCpuScriptIntrinsicBlur::RsdCpuScriptIntrinsicBlur(RsdCpuReferenceImpl *ctx, const Script *s)
227            : RsdCpuScriptIntrinsic(ctx, s, RS_SCRIPT_INTRINSIC_ID_BLUR) {
228
229    mRootPtr = &kernel;
230    radius = 5;
231    ComputeGaussianWeights();
232}
233
234RsdCpuScriptIntrinsicBlur::~RsdCpuScriptIntrinsicBlur() {
235}
236
237void RsdCpuScriptIntrinsicBlur::populateScript(Script *s) {
238    s->mHal.info.exportedVariableCount = 2;
239}
240
241void RsdCpuScriptIntrinsicBlur::invokeFreeChildren() {
242    alloc.clear();
243}
244
245
246RsdCpuScriptImpl * rsdIntrinsic_Blur(RsdCpuReferenceImpl *ctx, const Script *s) {
247
248    return new RsdCpuScriptIntrinsicBlur(ctx, s);
249}
250
251
252