| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 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 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 17 | #include "rsCpuIntrinsic.h" |
| 18 | #include "rsCpuIntrinsicInlines.h" |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 19 | |
| 20 | using namespace android; |
| 21 | using namespace android::renderscript; |
| 22 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 23 | namespace android { |
| 24 | namespace renderscript { |
| 25 | |
| 26 | |
| 27 | class RsdCpuScriptIntrinsicBlur : public RsdCpuScriptIntrinsic { |
| 28 | public: |
| 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(); |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 36 | RsdCpuScriptIntrinsicBlur(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 37 | |
| 38 | protected: |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 39 | float mFp[104]; |
| 40 | short mIp[104]; |
| 41 | void **mScratch; |
| 42 | size_t *mScratchSize; |
| 43 | float mRadius; |
| 44 | int mIradius; |
| 45 | ObjectBaseRef<Allocation> mAlloc; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 46 | |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 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); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 53 | void ComputeGaussianWeights(); |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | |
| 60 | void RsdCpuScriptIntrinsicBlur::ComputeGaussianWeights() { |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 61 | memset(mFp, 0, sizeof(mFp)); |
| 62 | memset(mIp, 0, sizeof(mIp)); |
| Jason Sams | 7079cd8 | 2012-11-27 18:26:33 -0800 | [diff] [blame] | 63 | |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 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 |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 77 | float sigma = 0.4f * mRadius + 0.6f; |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 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; |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 87 | mIradius = (float)ceil(mRadius) + 0.5f; |
| 88 | for (r = -mIradius; r <= mIradius; r ++) { |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 89 | floatR = (float)r; |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 90 | mFp[r + mIradius] = coeff1 * powf(e, floatR * floatR * coeff2); |
| 91 | normalizeFactor += mFp[r + mIradius]; |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 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; |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 96 | for (r = -mIradius; r <= mIradius; r ++) { |
| 97 | mFp[r + mIradius] *= normalizeFactor; |
| 98 | mIp[r + mIradius] = (short)(mIp[r + mIradius] * 32768); |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 102 | void RsdCpuScriptIntrinsicBlur::setGlobalObj(uint32_t slot, ObjectBase *data) { |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 103 | rsAssert(slot == 1); |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 104 | mAlloc.set(static_cast<Allocation *>(data)); |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 107 | void RsdCpuScriptIntrinsicBlur::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) { |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 108 | rsAssert(slot == 0); |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 109 | mRadius = ((const float *)data)[0]; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 110 | ComputeGaussianWeights(); |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 113 | |
| 114 | |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 115 | static void OneVU4(const RsForEachStubParamStruct *p, float4 *out, int32_t x, int32_t y, |
| 116 | const uchar *ptrIn, int iStride, const float* gPtr, int iradius) { |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 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 | |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 133 | static void OneVU1(const RsForEachStubParamStruct *p, float *out, int32_t x, int32_t y, |
| 134 | const uchar *ptrIn, int iStride, const float* gPtr, int iradius) { |
| Jason Sams | e78e514 | 2012-09-19 00:46:31 -0700 | [diff] [blame] | 135 | |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 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 | extern "C" void rsdIntrinsicBlurVFU4_K(void *dst, const void *pin, int stride, const void *gptr, int rct, int x1, int ct); |
| 151 | extern "C" void rsdIntrinsicBlurHFU4_K(void *dst, const void *pin, const void *gptr, int rct, int x1, int ct); |
| Jason Sams | 7079cd8 | 2012-11-27 18:26:33 -0800 | [diff] [blame] | 152 | extern "C" void rsdIntrinsicBlurHFU1_K(void *dst, const void *pin, const void *gptr, int rct, int x1, int ct); |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 153 | |
| 154 | static void OneVFU4(float4 *out, |
| 155 | const uchar *ptrIn, int iStride, const float* gPtr, int ct, |
| 156 | int x1, int x2) { |
| Jason Sams | e78e514 | 2012-09-19 00:46:31 -0700 | [diff] [blame] | 157 | |
| Jason Sams | f5ef8df | 2013-08-06 13:49:25 -0700 | [diff] [blame] | 158 | #if defined(ARCH_ARM_HAVE_VFP) |
| 159 | if (gArchUseSIMD) { |
| Jason Sams | 2207ab7 | 2012-09-19 13:44:55 -0700 | [diff] [blame] | 160 | int t = (x2 - x1); |
| 161 | t &= ~1; |
| 162 | if(t) { |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 163 | rsdIntrinsicBlurVFU4_K(out, ptrIn, iStride, gPtr, ct, x1, x1 + t); |
| Jason Sams | c859de1 | 2013-12-02 14:34:19 -0800 | [diff] [blame] | 164 | x1 += t; |
| 165 | ptrIn += t << 2; |
| 166 | out += t; |
| Jason Sams | 2207ab7 | 2012-09-19 13:44:55 -0700 | [diff] [blame] | 167 | } |
| Jason Sams | 2207ab7 | 2012-09-19 13:44:55 -0700 | [diff] [blame] | 168 | } |
| Jason Sams | e78e514 | 2012-09-19 00:46:31 -0700 | [diff] [blame] | 169 | #endif |
| 170 | |
| 171 | while(x2 > x1) { |
| Tim Murray | 2e5ef66 | 2012-10-22 14:46:39 -0700 | [diff] [blame] | 172 | const uchar *pi = ptrIn; |
| Jason Sams | e78e514 | 2012-09-19 00:46:31 -0700 | [diff] [blame] | 173 | float4 blurredPixel = 0; |
| 174 | const float* gp = gPtr; |
| 175 | |
| 176 | for (int r = 0; r < ct; r++) { |
| 177 | float4 pf = convert_float4(((const uchar4 *)pi)[0]); |
| 178 | blurredPixel += pf * gp[0]; |
| 179 | pi += iStride; |
| 180 | gp++; |
| 181 | } |
| 182 | out->xyzw = blurredPixel; |
| 183 | x1++; |
| 184 | out++; |
| Jason Sams | 0782188 | 2013-04-02 13:15:58 -0700 | [diff] [blame] | 185 | ptrIn+=4; |
| Jason Sams | e78e514 | 2012-09-19 00:46:31 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 189 | static void OneVFU1(float *out, |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 190 | const uchar *ptrIn, int iStride, const float* gPtr, int ct, int x1, int x2) { |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 191 | |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 192 | int len = x2 - x1; |
| 193 | |
| Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 194 | while((x2 > x1) && (((uintptr_t)ptrIn) & 0x3)) { |
| Jason Sams | 7079cd8 | 2012-11-27 18:26:33 -0800 | [diff] [blame] | 195 | const uchar *pi = ptrIn; |
| 196 | float blurredPixel = 0; |
| 197 | const float* gp = gPtr; |
| 198 | |
| 199 | for (int r = 0; r < ct; r++) { |
| 200 | float pf = (float)pi[0]; |
| 201 | blurredPixel += pf * gp[0]; |
| 202 | pi += iStride; |
| 203 | gp++; |
| 204 | } |
| 205 | out[0] = blurredPixel; |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 206 | x1++; |
| Jason Sams | 7079cd8 | 2012-11-27 18:26:33 -0800 | [diff] [blame] | 207 | out++; |
| 208 | ptrIn++; |
| Jason Sams | b6d2d2a | 2013-02-22 18:10:04 -0800 | [diff] [blame] | 209 | len--; |
| Jason Sams | 7079cd8 | 2012-11-27 18:26:33 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| Jason Sams | f5ef8df | 2013-08-06 13:49:25 -0700 | [diff] [blame] | 212 | #if defined(ARCH_ARM_HAVE_VFP) |
| 213 | if (gArchUseSIMD && (x2 > x1)) { |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 214 | int t = (x2 - x1) >> 2; |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 215 | t &= ~1; |
| 216 | if(t) { |
| Jason Sams | ce0351d | 2013-01-25 19:44:04 -0800 | [diff] [blame] | 217 | rsdIntrinsicBlurVFU4_K(out, ptrIn, iStride, gPtr, ct, 0, t ); |
| Jason Sams | 7079cd8 | 2012-11-27 18:26:33 -0800 | [diff] [blame] | 218 | len -= t << 2; |
| 219 | ptrIn += t << 2; |
| 220 | out += t << 2; |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 221 | } |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 222 | } |
| 223 | #endif |
| 224 | |
| Jason Sams | b6d2d2a | 2013-02-22 18:10:04 -0800 | [diff] [blame] | 225 | while(len > 0) { |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 226 | const uchar *pi = ptrIn; |
| 227 | float blurredPixel = 0; |
| 228 | const float* gp = gPtr; |
| 229 | |
| 230 | for (int r = 0; r < ct; r++) { |
| 231 | float pf = (float)pi[0]; |
| 232 | blurredPixel += pf * gp[0]; |
| 233 | pi += iStride; |
| 234 | gp++; |
| 235 | } |
| 236 | out[0] = blurredPixel; |
| 237 | len--; |
| 238 | out++; |
| Jason Sams | 7079cd8 | 2012-11-27 18:26:33 -0800 | [diff] [blame] | 239 | ptrIn++; |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | static void OneHU4(const RsForEachStubParamStruct *p, uchar4 *out, int32_t x, |
| 244 | const float4 *ptrIn, const float* gPtr, int iradius) { |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 245 | |
| 246 | float4 blurredPixel = 0; |
| 247 | for (int r = -iradius; r <= iradius; r ++) { |
| 248 | int validX = rsMax((x + r), 0); |
| 249 | validX = rsMin(validX, (int)(p->dimX - 1)); |
| 250 | float4 pf = ptrIn[validX]; |
| 251 | blurredPixel += pf * gPtr[0]; |
| 252 | gPtr++; |
| 253 | } |
| 254 | |
| 255 | out->xyzw = convert_uchar4(blurredPixel); |
| 256 | } |
| 257 | |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 258 | static void OneHU1(const RsForEachStubParamStruct *p, uchar *out, int32_t x, |
| 259 | const float *ptrIn, const float* gPtr, int iradius) { |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 260 | |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 261 | float blurredPixel = 0; |
| 262 | for (int r = -iradius; r <= iradius; r ++) { |
| 263 | int validX = rsMax((x + r), 0); |
| 264 | validX = rsMin(validX, (int)(p->dimX - 1)); |
| 265 | float pf = ptrIn[validX]; |
| 266 | blurredPixel += pf * gPtr[0]; |
| 267 | gPtr++; |
| 268 | } |
| 269 | |
| 270 | out[0] = (uchar)blurredPixel; |
| 271 | } |
| 272 | |
| 273 | |
| 274 | void RsdCpuScriptIntrinsicBlur::kernelU4(const RsForEachStubParamStruct *p, |
| 275 | uint32_t xstart, uint32_t xend, |
| 276 | uint32_t instep, uint32_t outstep) { |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 277 | |
| Stephen Hines | 2913f38 | 2013-01-14 20:44:09 -0800 | [diff] [blame] | 278 | float4 stackbuf[2048]; |
| 279 | float4 *buf = &stackbuf[0]; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 280 | RsdCpuScriptIntrinsicBlur *cp = (RsdCpuScriptIntrinsicBlur *)p->usr; |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 281 | if (!cp->mAlloc.get()) { |
| Jason Sams | b801b94 | 2012-10-10 12:07:38 -0700 | [diff] [blame] | 282 | ALOGE("Blur executed without input, skipping"); |
| 283 | return; |
| 284 | } |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 285 | const uchar *pin = (const uchar *)cp->mAlloc->mHal.drvState.lod[0].mallocPtr; |
| 286 | const size_t stride = cp->mAlloc->mHal.drvState.lod[0].stride; |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 287 | |
| 288 | uchar4 *out = (uchar4 *)p->out; |
| 289 | uint32_t x1 = xstart; |
| 290 | uint32_t x2 = xend; |
| 291 | |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 292 | if (p->dimX > 2048) { |
| 293 | if ((p->dimX > cp->mScratchSize[p->lid]) || !cp->mScratch[p->lid]) { |
| Jason Sams | 75adb82 | 2013-10-22 11:43:54 -0700 | [diff] [blame] | 294 | // Pad the side of the allocation by one unit to allow alignment later |
| 295 | cp->mScratch[p->lid] = realloc(cp->mScratch[p->lid], (p->dimX + 1) * 16); |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 296 | cp->mScratchSize[p->lid] = p->dimX; |
| 297 | } |
| Jason Sams | 75adb82 | 2013-10-22 11:43:54 -0700 | [diff] [blame] | 298 | // realloc only aligns to 8 bytes so we manually align to 16. |
| 299 | buf = (float4 *) ((((intptr_t)cp->mScratch[p->lid]) + 15) & ~0xf); |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 300 | } |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 301 | float4 *fout = (float4 *)buf; |
| Jason Sams | e78e514 | 2012-09-19 00:46:31 -0700 | [diff] [blame] | 302 | int y = p->y; |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 303 | if ((y > cp->mIradius) && (y < ((int)p->dimY - cp->mIradius))) { |
| 304 | const uchar *pi = pin + (y - cp->mIradius) * stride; |
| 305 | OneVFU4(fout, pi, stride, cp->mFp, cp->mIradius * 2 + 1, x1, x2); |
| Jason Sams | e78e514 | 2012-09-19 00:46:31 -0700 | [diff] [blame] | 306 | } else { |
| 307 | while(x2 > x1) { |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 308 | OneVU4(p, fout, x1, y, pin, stride, cp->mFp, cp->mIradius); |
| Jason Sams | e78e514 | 2012-09-19 00:46:31 -0700 | [diff] [blame] | 309 | fout++; |
| 310 | x1++; |
| 311 | } |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | x1 = xstart; |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 315 | while ((x1 < (uint32_t)cp->mIradius) && (x1 < x2)) { |
| Stephen Hines | 2913f38 | 2013-01-14 20:44:09 -0800 | [diff] [blame] | 316 | OneHU4(p, out, x1, buf, cp->mFp, cp->mIradius); |
| Jason Sams | e78e514 | 2012-09-19 00:46:31 -0700 | [diff] [blame] | 317 | out++; |
| 318 | x1++; |
| 319 | } |
| Jason Sams | f5ef8df | 2013-08-06 13:49:25 -0700 | [diff] [blame] | 320 | #if defined(ARCH_ARM_HAVE_VFP) |
| 321 | if (gArchUseSIMD) { |
| 322 | if ((x1 + cp->mIradius) < x2) { |
| 323 | rsdIntrinsicBlurHFU4_K(out, buf - cp->mIradius, cp->mFp, |
| 324 | cp->mIradius * 2 + 1, x1, x2 - cp->mIradius); |
| 325 | out += (x2 - cp->mIradius) - x1; |
| 326 | x1 = x2 - cp->mIradius; |
| 327 | } |
| Jason Sams | e78e514 | 2012-09-19 00:46:31 -0700 | [diff] [blame] | 328 | } |
| 329 | #endif |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 330 | while(x2 > x1) { |
| Stephen Hines | 2913f38 | 2013-01-14 20:44:09 -0800 | [diff] [blame] | 331 | OneHU4(p, out, x1, buf, cp->mFp, cp->mIradius); |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 332 | out++; |
| 333 | x1++; |
| 334 | } |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 337 | void RsdCpuScriptIntrinsicBlur::kernelU1(const RsForEachStubParamStruct *p, |
| 338 | uint32_t xstart, uint32_t xend, |
| 339 | uint32_t instep, uint32_t outstep) { |
| 340 | float buf[4 * 2048]; |
| 341 | RsdCpuScriptIntrinsicBlur *cp = (RsdCpuScriptIntrinsicBlur *)p->usr; |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 342 | if (!cp->mAlloc.get()) { |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 343 | ALOGE("Blur executed without input, skipping"); |
| 344 | return; |
| 345 | } |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 346 | const uchar *pin = (const uchar *)cp->mAlloc->mHal.drvState.lod[0].mallocPtr; |
| 347 | const size_t stride = cp->mAlloc->mHal.drvState.lod[0].stride; |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 348 | |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 349 | uchar *out = (uchar *)p->out; |
| 350 | uint32_t x1 = xstart; |
| 351 | uint32_t x2 = xend; |
| 352 | |
| 353 | float *fout = (float *)buf; |
| 354 | int y = p->y; |
| Jason Sams | ce0351d | 2013-01-25 19:44:04 -0800 | [diff] [blame] | 355 | if ((y > cp->mIradius) && (y < ((int)p->dimY - cp->mIradius -1))) { |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 356 | const uchar *pi = pin + (y - cp->mIradius) * stride; |
| 357 | OneVFU1(fout, pi, stride, cp->mFp, cp->mIradius * 2 + 1, x1, x2); |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 358 | } else { |
| 359 | while(x2 > x1) { |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 360 | OneVU1(p, fout, x1, y, pin, stride, cp->mFp, cp->mIradius); |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 361 | fout++; |
| 362 | x1++; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | x1 = xstart; |
| Jason Sams | 7079cd8 | 2012-11-27 18:26:33 -0800 | [diff] [blame] | 367 | while ((x1 < x2) && |
| Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 368 | ((x1 < (uint32_t)cp->mIradius) || (((uintptr_t)out) & 0x3))) { |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 369 | OneHU1(p, out, x1, buf, cp->mFp, cp->mIradius); |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 370 | out++; |
| 371 | x1++; |
| 372 | } |
| Jason Sams | f5ef8df | 2013-08-06 13:49:25 -0700 | [diff] [blame] | 373 | #if defined(ARCH_ARM_HAVE_VFP) |
| 374 | if (gArchUseSIMD) { |
| 375 | if ((x1 + cp->mIradius) < x2) { |
| 376 | uint32_t len = x2 - (x1 + cp->mIradius); |
| 377 | len &= ~3; |
| 378 | if (len > 0) { |
| 379 | rsdIntrinsicBlurHFU1_K(out, ((float *)buf) - cp->mIradius, cp->mFp, |
| 380 | cp->mIradius * 2 + 1, x1, x1 + len); |
| 381 | out += len; |
| 382 | x1 += len; |
| 383 | } |
| Jason Sams | b6d2d2a | 2013-02-22 18:10:04 -0800 | [diff] [blame] | 384 | } |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 385 | } |
| 386 | #endif |
| 387 | while(x2 > x1) { |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 388 | OneHU1(p, out, x1, buf, cp->mFp, cp->mIradius); |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 389 | out++; |
| 390 | x1++; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | RsdCpuScriptIntrinsicBlur::RsdCpuScriptIntrinsicBlur(RsdCpuReferenceImpl *ctx, |
| 395 | const Script *s, const Element *e) |
| 396 | : RsdCpuScriptIntrinsic(ctx, s, e, RS_SCRIPT_INTRINSIC_ID_BLUR) { |
| 397 | |
| 398 | mRootPtr = NULL; |
| 399 | if (e->getType() == RS_TYPE_UNSIGNED_8) { |
| 400 | switch (e->getVectorSize()) { |
| 401 | case 1: |
| 402 | mRootPtr = &kernelU1; |
| 403 | break; |
| 404 | case 4: |
| 405 | mRootPtr = &kernelU4; |
| 406 | break; |
| 407 | } |
| 408 | } |
| 409 | rsAssert(mRootPtr); |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 410 | mRadius = 5; |
| 411 | |
| 412 | mScratch = new void *[mCtx->getThreadCount()]; |
| 413 | mScratchSize = new size_t[mCtx->getThreadCount()]; |
| Jason Sams | 75adb82 | 2013-10-22 11:43:54 -0700 | [diff] [blame] | 414 | memset(mScratch, 0, sizeof(void *) * mCtx->getThreadCount()); |
| 415 | memset(mScratchSize, 0, sizeof(size_t) * mCtx->getThreadCount()); |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 416 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 417 | ComputeGaussianWeights(); |
| 418 | } |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 419 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 420 | RsdCpuScriptIntrinsicBlur::~RsdCpuScriptIntrinsicBlur() { |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 421 | uint32_t threads = mCtx->getThreadCount(); |
| 422 | if (mScratch) { |
| 423 | for (size_t i = 0; i < threads; i++) { |
| 424 | if (mScratch[i]) { |
| 425 | free(mScratch[i]); |
| 426 | } |
| 427 | } |
| 428 | delete []mScratch; |
| 429 | } |
| 430 | if (mScratchSize) { |
| 431 | delete []mScratchSize; |
| 432 | } |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void RsdCpuScriptIntrinsicBlur::populateScript(Script *s) { |
| 436 | s->mHal.info.exportedVariableCount = 2; |
| 437 | } |
| 438 | |
| 439 | void RsdCpuScriptIntrinsicBlur::invokeFreeChildren() { |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 440 | mAlloc.clear(); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 444 | RsdCpuScriptImpl * rsdIntrinsic_Blur(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e) { |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 445 | |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 446 | return new RsdCpuScriptIntrinsicBlur(ctx, s, e); |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | |