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