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