rsCpuIntrinsicBlur.cpp revision 7079cd8ddb0874d431446aea305b8c3b10e4f0ba
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, const Element *e);
37
38protected:
39    float fp[104];
40    short ip[104];
41    float radius;
42    int iradius;
43    ObjectBaseRef<Allocation> alloc;
44
45    static void kernelU4(const RsForEachStubParamStruct *p,
46                         uint32_t xstart, uint32_t xend,
47                         uint32_t instep, uint32_t outstep);
48    static void kernelU1(const RsForEachStubParamStruct *p,
49                         uint32_t xstart, uint32_t xend,
50                         uint32_t instep, uint32_t outstep);
51    void ComputeGaussianWeights();
52};
53
54}
55}
56
57
58void RsdCpuScriptIntrinsicBlur::ComputeGaussianWeights() {
59    memset(fp, 0, sizeof(fp));
60    memset(ip, 0, sizeof(ip));
61
62    // Compute gaussian weights for the blur
63    // e is the euler's number
64    float e = 2.718281828459045f;
65    float pi = 3.1415926535897932f;
66    // g(x) = ( 1 / sqrt( 2 * pi ) * sigma) * e ^ ( -x^2 / 2 * sigma^2 )
67    // x is of the form [-radius .. 0 .. radius]
68    // and sigma varies with radius.
69    // Based on some experimental radius values and sigma's
70    // we approximately fit sigma = f(radius) as
71    // sigma = radius * 0.4  + 0.6
72    // The larger the radius gets, the more our gaussian blur
73    // will resemble a box blur since with large sigma
74    // the gaussian curve begins to lose its shape
75    float sigma = 0.4f * radius + 0.6f;
76
77    // Now compute the coefficients. We will store some redundant values to save
78    // some math during the blur calculations precompute some values
79    float coeff1 = 1.0f / (sqrtf(2.0f * pi) * sigma);
80    float coeff2 = - 1.0f / (2.0f * sigma * sigma);
81
82    float normalizeFactor = 0.0f;
83    float floatR = 0.0f;
84    int r;
85    iradius = (float)ceil(radius) + 0.5f;
86    for (r = -iradius; r <= iradius; r ++) {
87        floatR = (float)r;
88        fp[r + iradius] = coeff1 * powf(e, floatR * floatR * coeff2);
89        normalizeFactor += fp[r + iradius];
90    }
91
92    //Now we need to normalize the weights because all our coefficients need to add up to one
93    normalizeFactor = 1.0f / normalizeFactor;
94    for (r = -iradius; r <= iradius; r ++) {
95        fp[r + iradius] *= normalizeFactor;
96        ip[r + iradius] = (short)(ip[r + iradius] * 32768);
97    }
98}
99
100void RsdCpuScriptIntrinsicBlur::setGlobalObj(uint32_t slot, ObjectBase *data) {
101    rsAssert(slot == 1);
102    alloc.set(static_cast<Allocation *>(data));
103}
104
105void RsdCpuScriptIntrinsicBlur::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
106    rsAssert(slot == 0);
107    radius = ((const float *)data)[0];
108    ComputeGaussianWeights();
109}
110
111
112
113static void OneVU4(const RsForEachStubParamStruct *p, float4 *out, int32_t x, int32_t y,
114                   const uchar *ptrIn, int iStride, const float* gPtr, int iradius) {
115
116    const uchar *pi = ptrIn + x*4;
117
118    float4 blurredPixel = 0;
119    for (int r = -iradius; r <= iradius; r ++) {
120        int validY = rsMax((y + r), 0);
121        validY = rsMin(validY, (int)(p->dimY - 1));
122        const uchar4 *pvy = (const uchar4 *)&pi[validY * iStride];
123        float4 pf = convert_float4(pvy[0]);
124        blurredPixel += pf * gPtr[0];
125        gPtr++;
126    }
127
128    out->xyzw = blurredPixel;
129}
130
131static void OneVU1(const RsForEachStubParamStruct *p, float *out, int32_t x, int32_t y,
132                   const uchar *ptrIn, int iStride, const float* gPtr, int iradius) {
133
134    const uchar *pi = ptrIn + x;
135
136    float blurredPixel = 0;
137    for (int r = -iradius; r <= iradius; r ++) {
138        int validY = rsMax((y + r), 0);
139        validY = rsMin(validY, (int)(p->dimY - 1));
140        float pf = (float)pi[validY * iStride];
141        blurredPixel += pf * gPtr[0];
142        gPtr++;
143    }
144
145    out[0] = blurredPixel;
146}
147
148extern "C" void rsdIntrinsicBlurVFU4_K(void *dst, const void *pin, int stride, const void *gptr, int rct, int x1, int ct);
149extern "C" void rsdIntrinsicBlurHFU4_K(void *dst, const void *pin, const void *gptr, int rct, int x1, int ct);
150extern "C" void rsdIntrinsicBlurHFU1_K(void *dst, const void *pin, const void *gptr, int rct, int x1, int ct);
151
152static void OneVFU4(float4 *out,
153                    const uchar *ptrIn, int iStride, const float* gPtr, int ct,
154                    int x1, int x2) {
155
156#if defined(ARCH_ARM_HAVE_NEON)
157    {
158        int t = (x2 - x1);
159        t &= ~1;
160        if(t) {
161            rsdIntrinsicBlurVFU4_K(out, ptrIn, iStride, gPtr, ct, x1, x1 + t);
162        }
163        x1 += t;
164    }
165#endif
166
167    while(x2 > x1) {
168        const uchar *pi = ptrIn;
169        float4 blurredPixel = 0;
170        const float* gp = gPtr;
171
172        for (int r = 0; r < ct; r++) {
173            float4 pf = convert_float4(((const uchar4 *)pi)[0]);
174            blurredPixel += pf * gp[0];
175            pi += iStride;
176            gp++;
177        }
178        out->xyzw = blurredPixel;
179        x1++;
180        out++;
181        ptrIn++;
182    }
183}
184
185static void OneVFU1(float *out,
186                    const uchar *ptrIn, int iStride, const float* gPtr, int ct, int len) {
187
188    while(len && (((int)ptrIn) & 0x3)) {
189        const uchar *pi = ptrIn;
190        float blurredPixel = 0;
191        const float* gp = gPtr;
192
193        for (int r = 0; r < ct; r++) {
194            float pf = (float)pi[0];
195            blurredPixel += pf * gp[0];
196            pi += iStride;
197            gp++;
198        }
199        out[0] = blurredPixel;
200        len--;
201        out++;
202        ptrIn++;
203    }
204
205#if defined(ARCH_ARM_HAVE_NEON)
206    {
207        int t = len >> 2;
208        t &= ~1;
209        if(t) {
210            rsdIntrinsicBlurVFU4_K(out, ptrIn, iStride, gPtr, ct, 0, t << 2);
211            len -= t << 2;
212            ptrIn += t << 2;
213            out += t << 2;
214        }
215    }
216#endif
217
218    while(len) {
219        const uchar *pi = ptrIn;
220        float blurredPixel = 0;
221        const float* gp = gPtr;
222
223        for (int r = 0; r < ct; r++) {
224            float pf = (float)pi[0];
225            blurredPixel += pf * gp[0];
226            pi += iStride;
227            gp++;
228        }
229        out[0] = blurredPixel;
230        len--;
231        out++;
232        ptrIn++;
233    }
234}
235
236static void OneHU4(const RsForEachStubParamStruct *p, uchar4 *out, int32_t x,
237                   const float4 *ptrIn, const float* gPtr, int iradius) {
238
239    float4 blurredPixel = 0;
240    for (int r = -iradius; r <= iradius; r ++) {
241        int validX = rsMax((x + r), 0);
242        validX = rsMin(validX, (int)(p->dimX - 1));
243        float4 pf = ptrIn[validX];
244        blurredPixel += pf * gPtr[0];
245        gPtr++;
246    }
247
248    out->xyzw = convert_uchar4(blurredPixel);
249}
250
251static void OneHU1(const RsForEachStubParamStruct *p, uchar *out, int32_t x,
252                   const float *ptrIn, const float* gPtr, int iradius) {
253
254    float blurredPixel = 0;
255    for (int r = -iradius; r <= iradius; r ++) {
256        int validX = rsMax((x + r), 0);
257        validX = rsMin(validX, (int)(p->dimX - 1));
258        float pf = ptrIn[validX];
259        blurredPixel += pf * gPtr[0];
260        gPtr++;
261    }
262
263    out[0] = (uchar)blurredPixel;
264}
265
266
267void RsdCpuScriptIntrinsicBlur::kernelU4(const RsForEachStubParamStruct *p,
268                                         uint32_t xstart, uint32_t xend,
269                                         uint32_t instep, uint32_t outstep) {
270    // fixme
271    float buf[4 * 2048];
272    RsdCpuScriptIntrinsicBlur *cp = (RsdCpuScriptIntrinsicBlur *)p->usr;
273    if (!cp->alloc.get()) {
274        ALOGE("Blur executed without input, skipping");
275        return;
276    }
277    const uchar *pin = (const uchar *)cp->alloc->mHal.drvState.lod[0].mallocPtr;
278    const size_t stride = cp->alloc->mHal.drvState.lod[0].stride;
279
280    uchar4 *out = (uchar4 *)p->out;
281    uint32_t x1 = xstart;
282    uint32_t x2 = xend;
283
284    float4 *fout = (float4 *)buf;
285    int y = p->y;
286    if ((y > cp->iradius) && (y < ((int)p->dimY - cp->iradius))) {
287        const uchar *pi = pin + (y - cp->iradius) * stride;
288        OneVFU4(fout, pi, stride, cp->fp, cp->iradius * 2 + 1, x1, x2);
289    } else {
290        while(x2 > x1) {
291            OneVU4(p, fout, x1, y, pin, stride, cp->fp, cp->iradius);
292            fout++;
293            x1++;
294        }
295    }
296
297    x1 = xstart;
298    while ((x1 < (uint32_t)cp->iradius) && (x1 < x2)) {
299        OneHU4(p, out, x1, (float4 *)buf, cp->fp, cp->iradius);
300        out++;
301        x1++;
302    }
303#if defined(ARCH_ARM_HAVE_NEON)
304    if ((x1 + cp->iradius) < x2) {
305        rsdIntrinsicBlurHFU4_K(out, ((float4 *)buf) - cp->iradius, cp->fp,
306                               cp->iradius * 2 + 1, x1, x2 - cp->iradius);
307        out += (x2 - cp->iradius) - x1;
308        x1 = x2 - cp->iradius;
309    }
310#endif
311    while(x2 > x1) {
312        OneHU4(p, out, x1, (float4 *)buf, cp->fp, cp->iradius);
313        out++;
314        x1++;
315    }
316}
317
318void RsdCpuScriptIntrinsicBlur::kernelU1(const RsForEachStubParamStruct *p,
319                                         uint32_t xstart, uint32_t xend,
320                                         uint32_t instep, uint32_t outstep) {
321    float buf[4 * 2048];
322    RsdCpuScriptIntrinsicBlur *cp = (RsdCpuScriptIntrinsicBlur *)p->usr;
323    if (!cp->alloc.get()) {
324        ALOGE("Blur executed without input, skipping");
325        return;
326    }
327    const uchar *pin = (const uchar *)cp->alloc->mHal.drvState.lod[0].mallocPtr;
328    const size_t stride = cp->alloc->mHal.drvState.lod[0].stride;
329
330    uchar *out = (uchar *)p->out;
331    uint32_t x1 = xstart;
332    uint32_t x2 = xend;
333
334    float *fout = (float *)buf;
335    int y = p->y;
336    if ((y > cp->iradius) && (y < ((int)p->dimY - cp->iradius))) {
337        const uchar *pi = pin + (y - cp->iradius) * stride;
338        OneVFU1(fout, pi, stride, cp->fp, cp->iradius * 2 + 1, x2-x1);
339    } else {
340        while(x2 > x1) {
341            OneVU1(p, fout, x1, y, pin, stride, cp->fp, cp->iradius);
342            fout++;
343            x1++;
344        }
345    }
346
347    x1 = xstart;
348    while ((x1 < x2) &&
349           ((x1 < (uint32_t)cp->iradius) || (((int)out) & 0x3))) {
350        OneHU1(p, out, x1, buf, cp->fp, cp->iradius);
351        out++;
352        x1++;
353    }
354#if defined(ARCH_ARM_HAVE_NEON)
355    if ((x1 + cp->iradius) < x2) {
356        uint32_t len = x2 - (x1 + cp->iradius);
357        len >>= 2;
358        rsdIntrinsicBlurHFU1_K(out, ((float4 *)buf) - cp->iradius, cp->fp,
359                               cp->iradius * 2 + 1, x1, x1+len);
360        out += len << 2;
361        x1 += len << 2;
362    }
363#endif
364    while(x2 > x1) {
365        OneHU1(p, out, x1, buf, cp->fp, cp->iradius);
366        out++;
367        x1++;
368    }
369}
370
371RsdCpuScriptIntrinsicBlur::RsdCpuScriptIntrinsicBlur(RsdCpuReferenceImpl *ctx,
372                                                     const Script *s, const Element *e)
373            : RsdCpuScriptIntrinsic(ctx, s, e, RS_SCRIPT_INTRINSIC_ID_BLUR) {
374
375    mRootPtr = NULL;
376    if (e->getType() == RS_TYPE_UNSIGNED_8) {
377        switch (e->getVectorSize()) {
378        case 1:
379            mRootPtr = &kernelU1;
380            break;
381        case 4:
382            mRootPtr = &kernelU4;
383            break;
384        }
385    }
386    rsAssert(mRootPtr);
387    radius = 5;
388    ComputeGaussianWeights();
389}
390
391RsdCpuScriptIntrinsicBlur::~RsdCpuScriptIntrinsicBlur() {
392}
393
394void RsdCpuScriptIntrinsicBlur::populateScript(Script *s) {
395    s->mHal.info.exportedVariableCount = 2;
396}
397
398void RsdCpuScriptIntrinsicBlur::invokeFreeChildren() {
399    alloc.clear();
400}
401
402
403RsdCpuScriptImpl * rsdIntrinsic_Blur(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e) {
404
405    return new RsdCpuScriptIntrinsicBlur(ctx, s, e);
406}
407
408
409