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