rsCpuIntrinsicBlur.cpp revision c905efd76fdcc1b8846b229bf7d991d185a7b4b7
1a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)/*
2a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) * Copyright (C) 2012 The Android Open Source Project
3a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) *
4a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) * Licensed under the Apache License, Version 2.0 (the "License");
590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) * you may not use this file except in compliance with the License.
690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) * You may obtain a copy of the License at
7a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) *
8a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) *      http://www.apache.org/licenses/LICENSE-2.0
990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) *
10a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) * Unless required by applicable law or agreed to in writing, software
11a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) * distributed under the License is distributed on an "AS IS" BASIS,
12a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) * See the License for the specific language governing permissions and
14a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) * limitations under the License.
1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) */
1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
17a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "rsCpuIntrinsic.h"
18a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "rsCpuIntrinsicInlines.h"
198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
20a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)using namespace android;
2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)using namespace android::renderscript;
2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace android {
24a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)namespace renderscript {
2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
26a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
27a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)class RsdCpuScriptIntrinsicBlur : public RsdCpuScriptIntrinsic {
28a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)public:
29a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    virtual void populateScript(Script *);
30a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    virtual void invokeFreeChildren();
31a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
32f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    virtual void setGlobalVar(uint32_t slot, const void *data, size_t dataLength);
33a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    virtual void setGlobalObj(uint32_t slot, ObjectBase *data);
34a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
35a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    virtual ~RsdCpuScriptIntrinsicBlur();
36a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    RsdCpuScriptIntrinsicBlur(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e);
37a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
38a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)protected:
39a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    float fp[104];
40a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    short ip[104];
41a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    float radius;
42a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    int iradius;
43a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    ObjectBaseRef<Allocation> alloc;
44a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
45a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    static void kernelU4(const RsForEachStubParamStruct *p,
46a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                         uint32_t xstart, uint32_t xend,
47a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                         uint32_t instep, uint32_t outstep);
48a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    static void kernelU1(const RsForEachStubParamStruct *p,
49a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                         uint32_t xstart, uint32_t xend,
50a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                         uint32_t instep, uint32_t outstep);
51f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    void ComputeGaussianWeights();
52a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)};
53a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
54a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
55a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
56a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
57a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
58a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void RsdCpuScriptIntrinsicBlur::ComputeGaussianWeights() {
59a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // Compute gaussian weights for the blur
60a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // e is the euler's number
61a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    float e = 2.718281828459045f;
62a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    float pi = 3.1415926535897932f;
63a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // g(x) = ( 1 / sqrt( 2 * pi ) * sigma) * e ^ ( -x^2 / 2 * sigma^2 )
64a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // x is of the form [-radius .. 0 .. radius]
65a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // and sigma varies with radius.
66a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // Based on some experimental radius values and sigma's
67a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // we approximately fit sigma = f(radius) as
68a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // sigma = radius * 0.4  + 0.6
69a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // The larger the radius gets, the more our gaussian blur
70a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // will resemble a box blur since with large sigma
71f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // the gaussian curve begins to lose its shape
72a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    float sigma = 0.4f * radius + 0.6f;
73a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
74a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // Now compute the coefficients. We will store some redundant values to save
75a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // some math during the blur calculations precompute some values
76a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    float coeff1 = 1.0f / (sqrtf(2.0f * pi) * sigma);
77a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    float coeff2 = - 1.0f / (2.0f * sigma * sigma);
78a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
79a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    float normalizeFactor = 0.0f;
80a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    float floatR = 0.0f;
81a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    int r;
82a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    iradius = (float)ceil(radius) + 0.5f;
83a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    for (r = -iradius; r <= iradius; r ++) {
84a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        floatR = (float)r;
8590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        fp[r + iradius] = coeff1 * powf(e, floatR * floatR * coeff2);
86a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        normalizeFactor += fp[r + iradius];
87a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    }
8890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
8990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //Now we need to normalize the weights because all our coefficients need to add up to one
9090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    normalizeFactor = 1.0f / normalizeFactor;
91    for (r = -iradius; r <= iradius; r ++) {
92        fp[r + iradius] *= normalizeFactor;
93        ip[r + iradius] = (short)(ip[r + iradius] * 32768);
94    }
95}
96
97void RsdCpuScriptIntrinsicBlur::setGlobalObj(uint32_t slot, ObjectBase *data) {
98    rsAssert(slot == 1);
99    alloc.set(static_cast<Allocation *>(data));
100}
101
102void RsdCpuScriptIntrinsicBlur::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
103    rsAssert(slot == 0);
104    radius = ((const float *)data)[0];
105    ComputeGaussianWeights();
106}
107
108
109
110static void OneVU4(const RsForEachStubParamStruct *p, float4 *out, int32_t x, int32_t y,
111                   const uchar *ptrIn, int iStride, const float* gPtr, int iradius) {
112
113    const uchar *pi = ptrIn + x*4;
114
115    float4 blurredPixel = 0;
116    for (int r = -iradius; r <= iradius; r ++) {
117        int validY = rsMax((y + r), 0);
118        validY = rsMin(validY, (int)(p->dimY - 1));
119        const uchar4 *pvy = (const uchar4 *)&pi[validY * iStride];
120        float4 pf = convert_float4(pvy[0]);
121        blurredPixel += pf * gPtr[0];
122        gPtr++;
123    }
124
125    out->xyzw = blurredPixel;
126}
127
128static void OneVU1(const RsForEachStubParamStruct *p, float *out, int32_t x, int32_t y,
129                   const uchar *ptrIn, int iStride, const float* gPtr, int iradius) {
130
131    const uchar *pi = ptrIn + x;
132
133    float blurredPixel = 0;
134    for (int r = -iradius; r <= iradius; r ++) {
135        int validY = rsMax((y + r), 0);
136        validY = rsMin(validY, (int)(p->dimY - 1));
137        float pf = (float)pi[validY * iStride];
138        blurredPixel += pf * gPtr[0];
139        gPtr++;
140    }
141
142    out[0] = blurredPixel;
143}
144
145extern "C" void rsdIntrinsicBlurVFU4_K(void *dst, const void *pin, int stride, const void *gptr, int rct, int x1, int ct);
146extern "C" void rsdIntrinsicBlurHFU4_K(void *dst, const void *pin, const void *gptr, int rct, int x1, int ct);
147
148static void OneVFU4(float4 *out,
149                    const uchar *ptrIn, int iStride, const float* gPtr, int ct,
150                    int x1, int x2) {
151
152#if defined(ARCH_ARM_HAVE_NEON)
153    {
154        int t = (x2 - x1);
155        t &= ~1;
156        if(t) {
157            rsdIntrinsicBlurVFU4_K(out, ptrIn, iStride, gPtr, ct, x1, x1 + t);
158        }
159        x1 += t;
160    }
161#endif
162
163    while(x2 > x1) {
164        const uchar *pi = ptrIn;
165        float4 blurredPixel = 0;
166        const float* gp = gPtr;
167
168        for (int r = 0; r < ct; r++) {
169            float4 pf = convert_float4(((const uchar4 *)pi)[0]);
170            blurredPixel += pf * gp[0];
171            pi += iStride;
172            gp++;
173        }
174        out->xyzw = blurredPixel;
175        x1++;
176        out++;
177    }
178}
179
180static void OneVFU1(float *out,
181                    const uchar *ptrIn, int iStride, const float* gPtr, int ct, int len) {
182
183#if defined(ARCH_ARM_HAVE_NEON)
184    {
185        int t = len >> 2;
186        t &= ~1;
187        if(t) {
188            rsdIntrinsicBlurVFU4_K(out, ptrIn, iStride, gPtr, ct, 0, len);
189        }
190        len -= t << 2;
191        ptrIn += t << 2;
192        out += t << 2;
193    }
194#endif
195
196    while(len) {
197        const uchar *pi = ptrIn;
198        float blurredPixel = 0;
199        const float* gp = gPtr;
200
201        for (int r = 0; r < ct; r++) {
202            float pf = (float)pi[0];
203            blurredPixel += pf * gp[0];
204            pi += iStride;
205            gp++;
206        }
207        out[0] = blurredPixel;
208        len--;
209        out++;
210    }
211}
212
213static void OneHU4(const RsForEachStubParamStruct *p, uchar4 *out, int32_t x,
214                   const float4 *ptrIn, const float* gPtr, int iradius) {
215
216    float4 blurredPixel = 0;
217    for (int r = -iradius; r <= iradius; r ++) {
218        int validX = rsMax((x + r), 0);
219        validX = rsMin(validX, (int)(p->dimX - 1));
220        float4 pf = ptrIn[validX];
221        blurredPixel += pf * gPtr[0];
222        gPtr++;
223    }
224
225    out->xyzw = convert_uchar4(blurredPixel);
226}
227
228static void OneHU1(const RsForEachStubParamStruct *p, uchar *out, int32_t x,
229                   const float *ptrIn, const float* gPtr, int iradius) {
230
231    float blurredPixel = 0;
232    for (int r = -iradius; r <= iradius; r ++) {
233        int validX = rsMax((x + r), 0);
234        validX = rsMin(validX, (int)(p->dimX - 1));
235        float pf = ptrIn[validX];
236        blurredPixel += pf * gPtr[0];
237        gPtr++;
238    }
239
240    out[0] = (uchar)blurredPixel;
241}
242
243
244void RsdCpuScriptIntrinsicBlur::kernelU4(const RsForEachStubParamStruct *p,
245                                         uint32_t xstart, uint32_t xend,
246                                         uint32_t instep, uint32_t outstep) {
247    float buf[4 * 2048];
248    RsdCpuScriptIntrinsicBlur *cp = (RsdCpuScriptIntrinsicBlur *)p->usr;
249    if (!cp->alloc.get()) {
250        ALOGE("Blur executed without input, skipping");
251        return;
252    }
253    const uchar *pin = (const uchar *)cp->alloc->mHal.drvState.lod[0].mallocPtr;
254    const size_t stride = cp->alloc->mHal.drvState.lod[0].stride;
255
256    uchar4 *out = (uchar4 *)p->out;
257    uint32_t x1 = xstart;
258    uint32_t x2 = xend;
259
260    float4 *fout = (float4 *)buf;
261    int y = p->y;
262    if ((y > cp->iradius) && (y < ((int)p->dimY - cp->iradius))) {
263        const uchar *pi = pin + (y - cp->iradius) * stride;
264        OneVFU4(fout, pi, stride, cp->fp, cp->iradius * 2 + 1, x1, x2);
265    } else {
266        while(x2 > x1) {
267            OneVU4(p, fout, x1, y, pin, stride, cp->fp, cp->iradius);
268            fout++;
269            x1++;
270        }
271    }
272
273    x1 = xstart;
274    while ((x1 < (uint32_t)cp->iradius) && (x1 < x2)) {
275        OneHU4(p, out, x1, (float4 *)buf, cp->fp, cp->iradius);
276        out++;
277        x1++;
278    }
279#if defined(ARCH_ARM_HAVE_NEON)
280    if ((x1 + cp->iradius) < x2) {
281        rsdIntrinsicBlurHFU4_K(out, ((float4 *)buf) - cp->iradius, cp->fp,
282                               cp->iradius * 2 + 1, x1, x2 - cp->iradius);
283        out += (x2 - cp->iradius) - x1;
284        x1 = x2 - cp->iradius;
285    }
286#endif
287    while(x2 > x1) {
288        OneHU4(p, out, x1, (float4 *)buf, cp->fp, cp->iradius);
289        out++;
290        x1++;
291    }
292}
293
294void RsdCpuScriptIntrinsicBlur::kernelU1(const RsForEachStubParamStruct *p,
295                                         uint32_t xstart, uint32_t xend,
296                                         uint32_t instep, uint32_t outstep) {
297    float buf[4 * 2048];
298    RsdCpuScriptIntrinsicBlur *cp = (RsdCpuScriptIntrinsicBlur *)p->usr;
299    if (!cp->alloc.get()) {
300        ALOGE("Blur executed without input, skipping");
301        return;
302    }
303    const uchar *pin = (const uchar *)cp->alloc->mHal.drvState.lod[0].mallocPtr;
304    const size_t stride = cp->alloc->mHal.drvState.lod[0].stride;
305
306    uchar *out = (uchar *)p->out;
307    uint32_t x1 = xstart;
308    uint32_t x2 = xend;
309
310    float *fout = (float *)buf;
311    int y = p->y;
312    if ((y > cp->iradius) && (y < ((int)p->dimY - cp->iradius))) {
313        const uchar *pi = pin + (y - cp->iradius) * stride;
314        OneVFU1(fout, pi, stride, cp->fp, cp->iradius * 2 + 1, x2-x1);
315    } else {
316        while(x2 > x1) {
317            OneVU1(p, fout, x1, y, pin, stride, cp->fp, cp->iradius);
318            fout++;
319            x1++;
320        }
321    }
322
323    x1 = xstart;
324    while ((x1 < (uint32_t)cp->iradius) && (x1 < x2)) {
325        OneHU1(p, out, x1, buf, cp->fp, cp->iradius);
326        out++;
327        x1++;
328    }
329#if 0//defined(ARCH_ARM_HAVE_NEON)
330    if ((x1 + cp->iradius) < x2) {
331        rsdIntrinsicBlurHFU4_K(out, ((float4 *)buf) - cp->iradius, cp->fp, cp->iradius * 2 + 1, x1, 0, x2 - cp->iradius);
332        out += (x2 - cp->iradius) - x1;
333        x1 = x2 - cp->iradius;
334    }
335#endif
336    while(x2 > x1) {
337        OneHU1(p, out, x1, buf, cp->fp, cp->iradius);
338        out++;
339        x1++;
340    }
341}
342
343RsdCpuScriptIntrinsicBlur::RsdCpuScriptIntrinsicBlur(RsdCpuReferenceImpl *ctx,
344                                                     const Script *s, const Element *e)
345            : RsdCpuScriptIntrinsic(ctx, s, e, RS_SCRIPT_INTRINSIC_ID_BLUR) {
346
347    mRootPtr = NULL;
348    if (e->getType() == RS_TYPE_UNSIGNED_8) {
349        switch (e->getVectorSize()) {
350        case 1:
351            mRootPtr = &kernelU1;
352            break;
353        case 4:
354            mRootPtr = &kernelU4;
355            break;
356        }
357    }
358    rsAssert(mRootPtr);
359    radius = 5;
360    ComputeGaussianWeights();
361}
362
363RsdCpuScriptIntrinsicBlur::~RsdCpuScriptIntrinsicBlur() {
364}
365
366void RsdCpuScriptIntrinsicBlur::populateScript(Script *s) {
367    s->mHal.info.exportedVariableCount = 2;
368}
369
370void RsdCpuScriptIntrinsicBlur::invokeFreeChildren() {
371    alloc.clear();
372}
373
374
375RsdCpuScriptImpl * rsdIntrinsic_Blur(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e) {
376
377    return new RsdCpuScriptIntrinsicBlur(ctx, s, e);
378}
379
380
381