| 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 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 20 | namespace android { |
| 21 | namespace renderscript { |
| 22 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 23 | class RsdCpuScriptIntrinsicBlur : public RsdCpuScriptIntrinsic { |
| 24 | public: |
| Stephen Hines | c060f14 | 2015-05-13 19:26:09 -0700 | [diff] [blame] | 25 | void populateScript(Script *) override; |
| 26 | void invokeFreeChildren() override; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 27 | |
| Stephen Hines | c060f14 | 2015-05-13 19:26:09 -0700 | [diff] [blame] | 28 | void setGlobalVar(uint32_t slot, const void *data, size_t dataLength) override; |
| 29 | void setGlobalObj(uint32_t slot, ObjectBase *data) override; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 30 | |
| Stephen Hines | c060f14 | 2015-05-13 19:26:09 -0700 | [diff] [blame] | 31 | ~RsdCpuScriptIntrinsicBlur() override; |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 32 | RsdCpuScriptIntrinsicBlur(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 33 | |
| 34 | protected: |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 35 | float mFp[104]; |
| Simon Hosie | 4467880 | 2014-02-19 22:08:48 -0800 | [diff] [blame] | 36 | uint16_t mIp[104]; |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 37 | void **mScratch; |
| 38 | size_t *mScratchSize; |
| 39 | float mRadius; |
| 40 | int mIradius; |
| 41 | ObjectBaseRef<Allocation> mAlloc; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 42 | |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 43 | static void kernelU4(const RsExpandKernelDriverInfo *info, |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 44 | uint32_t xstart, uint32_t xend, |
| Chris Wailes | 9ed7910 | 2014-07-25 15:53:28 -0700 | [diff] [blame] | 45 | uint32_t outstep); |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 46 | static void kernelU1(const RsExpandKernelDriverInfo *info, |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 47 | uint32_t xstart, uint32_t xend, |
| Chris Wailes | 9ed7910 | 2014-07-25 15:53:28 -0700 | [diff] [blame] | 48 | uint32_t outstep); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 49 | void ComputeGaussianWeights(); |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 52 | |
| 53 | void RsdCpuScriptIntrinsicBlur::ComputeGaussianWeights() { |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 54 | memset(mFp, 0, sizeof(mFp)); |
| 55 | memset(mIp, 0, sizeof(mIp)); |
| Jason Sams | 7079cd8 | 2012-11-27 18:26:33 -0800 | [diff] [blame] | 56 | |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 57 | // Compute gaussian weights for the blur |
| 58 | // e is the euler's number |
| Jean-Luc Brouillet | 8b7117d | 2014-04-30 13:48:03 -0700 | [diff] [blame] | 59 | // TODO Define these constants only once |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 60 | float e = 2.718281828459045f; |
| 61 | float pi = 3.1415926535897932f; |
| Jean-Luc Brouillet | 8b7117d | 2014-04-30 13:48:03 -0700 | [diff] [blame] | 62 | // 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] | 63 | // x is of the form [-radius .. 0 .. radius] |
| Jean-Luc Brouillet | 8b7117d | 2014-04-30 13:48:03 -0700 | [diff] [blame] | 64 | // and sigma varies with the radius. |
| 65 | // Based on some experimental radius values and sigmas, |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 66 | // we approximately fit sigma = f(radius) as |
| 67 | // sigma = radius * 0.4 + 0.6 |
| 68 | // The larger the radius gets, the more our gaussian blur |
| 69 | // will resemble a box blur since with large sigma |
| 70 | // the gaussian curve begins to lose its shape |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 71 | float sigma = 0.4f * mRadius + 0.6f; |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 72 | |
| 73 | // Now compute the coefficients. We will store some redundant values to save |
| 74 | // some math during the blur calculations precompute some values |
| 75 | float coeff1 = 1.0f / (sqrtf(2.0f * pi) * sigma); |
| 76 | float coeff2 = - 1.0f / (2.0f * sigma * sigma); |
| 77 | |
| 78 | float normalizeFactor = 0.0f; |
| 79 | float floatR = 0.0f; |
| 80 | int r; |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 81 | mIradius = (float)ceil(mRadius) + 0.5f; |
| 82 | for (r = -mIradius; r <= mIradius; r ++) { |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 83 | floatR = (float)r; |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 84 | mFp[r + mIradius] = coeff1 * powf(e, floatR * floatR * coeff2); |
| 85 | normalizeFactor += mFp[r + mIradius]; |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| Jean-Luc Brouillet | 8b7117d | 2014-04-30 13:48:03 -0700 | [diff] [blame] | 88 | // 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] | 89 | normalizeFactor = 1.0f / normalizeFactor; |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 90 | for (r = -mIradius; r <= mIradius; r ++) { |
| 91 | mFp[r + mIradius] *= normalizeFactor; |
| Simon Hosie | 4467880 | 2014-02-19 22:08:48 -0800 | [diff] [blame] | 92 | mIp[r + mIradius] = (uint16_t)(mFp[r + mIradius] * 65536.0f + 0.5f); |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 96 | void RsdCpuScriptIntrinsicBlur::setGlobalObj(uint32_t slot, ObjectBase *data) { |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 97 | rsAssert(slot == 1); |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 98 | mAlloc.set(static_cast<Allocation *>(data)); |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 101 | void RsdCpuScriptIntrinsicBlur::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) { |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 102 | rsAssert(slot == 0); |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 103 | mRadius = ((const float *)data)[0]; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 104 | ComputeGaussianWeights(); |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 107 | |
| 108 | |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 109 | static void OneVU4(const RsExpandKernelDriverInfo *info, float4 *out, int32_t x, int32_t y, |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 110 | const uchar *ptrIn, int iStride, const float* gPtr, int iradius) { |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 111 | |
| 112 | const uchar *pi = ptrIn + x*4; |
| 113 | |
| 114 | float4 blurredPixel = 0; |
| 115 | for (int r = -iradius; r <= iradius; r ++) { |
| 116 | int validY = rsMax((y + r), 0); |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 117 | validY = rsMin(validY, (int)(info->dim.y- 1)); |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 118 | const uchar4 *pvy = (const uchar4 *)&pi[validY * iStride]; |
| 119 | float4 pf = convert_float4(pvy[0]); |
| 120 | blurredPixel += pf * gPtr[0]; |
| 121 | gPtr++; |
| 122 | } |
| 123 | |
| Jason Sams | d25fef7 | 2014-08-21 16:18:39 -0700 | [diff] [blame] | 124 | out[0] = blurredPixel; |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 127 | static void OneVU1(const RsExpandKernelDriverInfo *info, float *out, int32_t x, int32_t y, |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 128 | const uchar *ptrIn, int iStride, const float* gPtr, int iradius) { |
| Jason Sams | e78e514 | 2012-09-19 00:46:31 -0700 | [diff] [blame] | 129 | |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 130 | const uchar *pi = ptrIn + x; |
| 131 | |
| 132 | float blurredPixel = 0; |
| 133 | for (int r = -iradius; r <= iradius; r ++) { |
| 134 | int validY = rsMax((y + r), 0); |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 135 | validY = rsMin(validY, (int)(info->dim.y - 1)); |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 136 | float pf = (float)pi[validY * iStride]; |
| 137 | blurredPixel += pf * gPtr[0]; |
| 138 | gPtr++; |
| 139 | } |
| 140 | |
| 141 | out[0] = blurredPixel; |
| 142 | } |
| 143 | |
| Chih-Hung Hsieh | 462de21 | 2016-11-16 11:33:57 -0800 | [diff] [blame^] | 144 | } // namespace renderscript |
| 145 | } // namespace android |
| 146 | |
| Simon Hosie | 4467880 | 2014-02-19 22:08:48 -0800 | [diff] [blame] | 147 | |
| 148 | extern "C" void rsdIntrinsicBlurU1_K(uchar *out, uchar const *in, size_t w, size_t h, |
| 149 | size_t p, size_t x, size_t y, size_t count, size_t r, uint16_t const *tab); |
| 150 | extern "C" void rsdIntrinsicBlurU4_K(uchar4 *out, uchar4 const *in, size_t w, size_t h, |
| 151 | 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] | 152 | |
| Rose, James | 7b7060c | 2014-04-22 12:08:06 +0800 | [diff] [blame] | 153 | #if defined(ARCH_X86_HAVE_SSSE3) |
| Dan Albert | ebf0eb9 | 2014-08-22 13:19:24 -0700 | [diff] [blame] | 154 | extern void rsdIntrinsicBlurVFU4_K(void *dst, const void *pin, int stride, const void *gptr, int rct, int x1, int ct); |
| 155 | extern void rsdIntrinsicBlurHFU4_K(void *dst, const void *pin, const void *gptr, int rct, int x1, int ct); |
| 156 | extern void rsdIntrinsicBlurHFU1_K(void *dst, const void *pin, const void *gptr, int rct, int x1, int ct); |
| Rose, James | 7b7060c | 2014-04-22 12:08:06 +0800 | [diff] [blame] | 157 | #endif |
| 158 | |
| Chih-Hung Hsieh | 462de21 | 2016-11-16 11:33:57 -0800 | [diff] [blame^] | 159 | using android::renderscript::gArchUseSIMD; |
| 160 | |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 161 | static void OneVFU4(float4 *out, |
| 162 | const uchar *ptrIn, int iStride, const float* gPtr, int ct, |
| 163 | int x1, int x2) { |
| Jason Sams | d25fef7 | 2014-08-21 16:18:39 -0700 | [diff] [blame] | 164 | out += x1; |
| Rose, James | 7b7060c | 2014-04-22 12:08:06 +0800 | [diff] [blame] | 165 | #if defined(ARCH_X86_HAVE_SSSE3) |
| 166 | if (gArchUseSIMD) { |
| 167 | int t = (x2 - x1); |
| 168 | t &= ~1; |
| 169 | if (t) { |
| 170 | rsdIntrinsicBlurVFU4_K(out, ptrIn, iStride, gPtr, ct, x1, x1 + t); |
| 171 | } |
| 172 | x1 += t; |
| Yong Chen | a0cdfe0 | 2014-09-01 13:20:26 +0800 | [diff] [blame] | 173 | out += t; |
| 174 | ptrIn += t << 2; |
| Rose, James | 7b7060c | 2014-04-22 12:08:06 +0800 | [diff] [blame] | 175 | } |
| 176 | #endif |
| Jason Sams | e78e514 | 2012-09-19 00:46:31 -0700 | [diff] [blame] | 177 | while(x2 > x1) { |
| Tim Murray | 2e5ef66 | 2012-10-22 14:46:39 -0700 | [diff] [blame] | 178 | const uchar *pi = ptrIn; |
| Jason Sams | e78e514 | 2012-09-19 00:46:31 -0700 | [diff] [blame] | 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++; |
| Jason Sams | 0782188 | 2013-04-02 13:15:58 -0700 | [diff] [blame] | 191 | ptrIn+=4; |
| Jason Sams | e78e514 | 2012-09-19 00:46:31 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 195 | static void OneVFU1(float *out, |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 196 | 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] | 197 | |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 198 | int len = x2 - x1; |
| Jason Sams | d25fef7 | 2014-08-21 16:18:39 -0700 | [diff] [blame] | 199 | out += x1; |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 200 | |
| Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 201 | while((x2 > x1) && (((uintptr_t)ptrIn) & 0x3)) { |
| Jason Sams | 7079cd8 | 2012-11-27 18:26:33 -0800 | [diff] [blame] | 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; |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 213 | x1++; |
| Jason Sams | 7079cd8 | 2012-11-27 18:26:33 -0800 | [diff] [blame] | 214 | out++; |
| 215 | ptrIn++; |
| Jason Sams | b6d2d2a | 2013-02-22 18:10:04 -0800 | [diff] [blame] | 216 | len--; |
| Jason Sams | 7079cd8 | 2012-11-27 18:26:33 -0800 | [diff] [blame] | 217 | } |
| Rose, James | 7b7060c | 2014-04-22 12:08:06 +0800 | [diff] [blame] | 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 |
| Jason Sams | b6d2d2a | 2013-02-22 18:10:04 -0800 | [diff] [blame] | 230 | while(len > 0) { |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 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++; |
| Jason Sams | 7079cd8 | 2012-11-27 18:26:33 -0800 | [diff] [blame] | 244 | ptrIn++; |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| Chih-Hung Hsieh | 462de21 | 2016-11-16 11:33:57 -0800 | [diff] [blame^] | 248 | using android::renderscript::rsMin; |
| 249 | using android::renderscript::rsMax; |
| 250 | |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 251 | static void OneHU4(const RsExpandKernelDriverInfo *info, uchar4 *out, int32_t x, |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 252 | const float4 *ptrIn, const float* gPtr, int iradius) { |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 253 | |
| 254 | float4 blurredPixel = 0; |
| 255 | for (int r = -iradius; r <= iradius; r ++) { |
| 256 | int validX = rsMax((x + r), 0); |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 257 | validX = rsMin(validX, (int)(info->dim.x - 1)); |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 258 | float4 pf = ptrIn[validX]; |
| 259 | blurredPixel += pf * gPtr[0]; |
| 260 | gPtr++; |
| 261 | } |
| 262 | |
| 263 | out->xyzw = convert_uchar4(blurredPixel); |
| 264 | } |
| 265 | |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 266 | static void OneHU1(const RsExpandKernelDriverInfo *info, uchar *out, int32_t x, |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 267 | const float *ptrIn, const float* gPtr, int iradius) { |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 268 | |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 269 | float blurredPixel = 0; |
| 270 | for (int r = -iradius; r <= iradius; r ++) { |
| 271 | int validX = rsMax((x + r), 0); |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 272 | validX = rsMin(validX, (int)(info->dim.x - 1)); |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 273 | float pf = ptrIn[validX]; |
| 274 | blurredPixel += pf * gPtr[0]; |
| 275 | gPtr++; |
| 276 | } |
| 277 | |
| 278 | out[0] = (uchar)blurredPixel; |
| 279 | } |
| 280 | |
| 281 | |
| Chih-Hung Hsieh | 462de21 | 2016-11-16 11:33:57 -0800 | [diff] [blame^] | 282 | namespace android { |
| 283 | namespace renderscript { |
| 284 | |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 285 | void RsdCpuScriptIntrinsicBlur::kernelU4(const RsExpandKernelDriverInfo *info, |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 286 | uint32_t xstart, uint32_t xend, |
| Chris Wailes | 9ed7910 | 2014-07-25 15:53:28 -0700 | [diff] [blame] | 287 | uint32_t outstep) { |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 288 | |
| Stephen Hines | 2913f38 | 2013-01-14 20:44:09 -0800 | [diff] [blame] | 289 | float4 stackbuf[2048]; |
| 290 | float4 *buf = &stackbuf[0]; |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 291 | RsdCpuScriptIntrinsicBlur *cp = (RsdCpuScriptIntrinsicBlur *)info->usr; |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 292 | if (!cp->mAlloc.get()) { |
| Jason Sams | b801b94 | 2012-10-10 12:07:38 -0700 | [diff] [blame] | 293 | ALOGE("Blur executed without input, skipping"); |
| 294 | return; |
| 295 | } |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 296 | const uchar *pin = (const uchar *)cp->mAlloc->mHal.drvState.lod[0].mallocPtr; |
| 297 | const size_t stride = cp->mAlloc->mHal.drvState.lod[0].stride; |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 298 | |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 299 | uchar4 *out = (uchar4 *)info->outPtr[0]; |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 300 | uint32_t x1 = xstart; |
| 301 | uint32_t x2 = xend; |
| 302 | |
| Jason Sams | 074424a | 2014-05-22 13:30:03 -0700 | [diff] [blame] | 303 | #if defined(ARCH_ARM_USE_INTRINSICS) |
| Simon Hosie | 5a1f196 | 2016-04-20 16:20:25 -0700 | [diff] [blame] | 304 | if (gArchUseSIMD && info->dim.x >= 4) { |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 305 | rsdIntrinsicBlurU4_K(out, (uchar4 const *)(pin + stride * info->current.y), |
| 306 | info->dim.x, info->dim.y, |
| 307 | stride, x1, info->current.y, x2 - x1, cp->mIradius, cp->mIp + cp->mIradius); |
| Simon Hosie | 4467880 | 2014-02-19 22:08:48 -0800 | [diff] [blame] | 308 | return; |
| 309 | } |
| 310 | #endif |
| 311 | |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 312 | if (info->dim.x > 2048) { |
| 313 | if ((info->dim.x > cp->mScratchSize[info->lid]) || !cp->mScratch[info->lid]) { |
| Jason Sams | 75adb82 | 2013-10-22 11:43:54 -0700 | [diff] [blame] | 314 | // Pad the side of the allocation by one unit to allow alignment later |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 315 | cp->mScratch[info->lid] = realloc(cp->mScratch[info->lid], (info->dim.x + 1) * 16); |
| 316 | cp->mScratchSize[info->lid] = info->dim.x; |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 317 | } |
| Jason Sams | 75adb82 | 2013-10-22 11:43:54 -0700 | [diff] [blame] | 318 | // realloc only aligns to 8 bytes so we manually align to 16. |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 319 | buf = (float4 *) ((((intptr_t)cp->mScratch[info->lid]) + 15) & ~0xf); |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 320 | } |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 321 | float4 *fout = (float4 *)buf; |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 322 | int y = info->current.y; |
| 323 | if ((y > cp->mIradius) && (y < ((int)info->dim.y - cp->mIradius))) { |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 324 | const uchar *pi = pin + (y - cp->mIradius) * stride; |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 325 | OneVFU4(fout, pi, stride, cp->mFp, cp->mIradius * 2 + 1, 0, info->dim.x); |
| Jason Sams | e78e514 | 2012-09-19 00:46:31 -0700 | [diff] [blame] | 326 | } else { |
| Jason Sams | d25fef7 | 2014-08-21 16:18:39 -0700 | [diff] [blame] | 327 | x1 = 0; |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 328 | while(info->dim.x > x1) { |
| 329 | OneVU4(info, fout, x1, y, pin, stride, cp->mFp, cp->mIradius); |
| Jason Sams | e78e514 | 2012-09-19 00:46:31 -0700 | [diff] [blame] | 330 | fout++; |
| 331 | x1++; |
| 332 | } |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | x1 = xstart; |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 336 | while ((x1 < (uint32_t)cp->mIradius) && (x1 < x2)) { |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 337 | OneHU4(info, out, x1, buf, cp->mFp, cp->mIradius); |
| Jason Sams | e78e514 | 2012-09-19 00:46:31 -0700 | [diff] [blame] | 338 | out++; |
| 339 | x1++; |
| 340 | } |
| Rose, James | 7b7060c | 2014-04-22 12:08:06 +0800 | [diff] [blame] | 341 | #if defined(ARCH_X86_HAVE_SSSE3) |
| 342 | if (gArchUseSIMD) { |
| 343 | if ((x1 + cp->mIradius) < x2) { |
| 344 | rsdIntrinsicBlurHFU4_K(out, buf - cp->mIradius, cp->mFp, |
| 345 | cp->mIradius * 2 + 1, x1, x2 - cp->mIradius); |
| 346 | out += (x2 - cp->mIradius) - x1; |
| 347 | x1 = x2 - cp->mIradius; |
| 348 | } |
| 349 | } |
| 350 | #endif |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 351 | while(x2 > x1) { |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 352 | OneHU4(info, out, x1, buf, cp->mFp, cp->mIradius); |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 353 | out++; |
| 354 | x1++; |
| 355 | } |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 358 | void RsdCpuScriptIntrinsicBlur::kernelU1(const RsExpandKernelDriverInfo *info, |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 359 | uint32_t xstart, uint32_t xend, |
| Chris Wailes | 9ed7910 | 2014-07-25 15:53:28 -0700 | [diff] [blame] | 360 | uint32_t outstep) { |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 361 | float buf[4 * 2048]; |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 362 | RsdCpuScriptIntrinsicBlur *cp = (RsdCpuScriptIntrinsicBlur *)info->usr; |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 363 | if (!cp->mAlloc.get()) { |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 364 | ALOGE("Blur executed without input, skipping"); |
| 365 | return; |
| 366 | } |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 367 | const uchar *pin = (const uchar *)cp->mAlloc->mHal.drvState.lod[0].mallocPtr; |
| 368 | const size_t stride = cp->mAlloc->mHal.drvState.lod[0].stride; |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 369 | |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 370 | uchar *out = (uchar *)info->outPtr[0]; |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 371 | uint32_t x1 = xstart; |
| 372 | uint32_t x2 = xend; |
| 373 | |
| Jason Sams | 074424a | 2014-05-22 13:30:03 -0700 | [diff] [blame] | 374 | #if defined(ARCH_ARM_USE_INTRINSICS) |
| Simon Hosie | 5a1f196 | 2016-04-20 16:20:25 -0700 | [diff] [blame] | 375 | if (gArchUseSIMD && info->dim.x >= 16) { |
| 376 | // The specialisation for r<=8 has an awkward prefill case, which is |
| 377 | // fiddly to resolve, where starting close to the right edge can cause |
| 378 | // a read beyond the end of input. So avoid that case here. |
| 379 | if (cp->mIradius > 8 || (info->dim.x - rsMax(0, (int32_t)x1 - 8)) >= 16) { |
| 380 | rsdIntrinsicBlurU1_K(out, pin + stride * info->current.y, info->dim.x, info->dim.y, |
| 381 | stride, x1, info->current.y, x2 - x1, cp->mIradius, cp->mIp + cp->mIradius); |
| 382 | return; |
| 383 | } |
| Simon Hosie | 4467880 | 2014-02-19 22:08:48 -0800 | [diff] [blame] | 384 | } |
| 385 | #endif |
| 386 | |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 387 | float *fout = (float *)buf; |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 388 | int y = info->current.y; |
| 389 | if ((y > cp->mIradius) && (y < ((int)info->dim.y - cp->mIradius -1))) { |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 390 | const uchar *pi = pin + (y - cp->mIradius) * stride; |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 391 | OneVFU1(fout, pi, stride, cp->mFp, cp->mIradius * 2 + 1, 0, info->dim.x); |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 392 | } else { |
| Jason Sams | d25fef7 | 2014-08-21 16:18:39 -0700 | [diff] [blame] | 393 | x1 = 0; |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 394 | while(info->dim.x > x1) { |
| 395 | OneVU1(info, fout, x1, y, pin, stride, cp->mFp, cp->mIradius); |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 396 | fout++; |
| 397 | x1++; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | x1 = xstart; |
| Jason Sams | 7079cd8 | 2012-11-27 18:26:33 -0800 | [diff] [blame] | 402 | while ((x1 < x2) && |
| Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 403 | ((x1 < (uint32_t)cp->mIradius) || (((uintptr_t)out) & 0x3))) { |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 404 | OneHU1(info, out, x1, buf, cp->mFp, cp->mIradius); |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 405 | out++; |
| 406 | x1++; |
| 407 | } |
| Rose, James | 7b7060c | 2014-04-22 12:08:06 +0800 | [diff] [blame] | 408 | #if defined(ARCH_X86_HAVE_SSSE3) |
| 409 | if (gArchUseSIMD) { |
| 410 | if ((x1 + cp->mIradius) < x2) { |
| 411 | uint32_t len = x2 - (x1 + cp->mIradius); |
| 412 | len &= ~3; |
| 413 | if (len > 0) { |
| 414 | rsdIntrinsicBlurHFU1_K(out, ((float *)buf) - cp->mIradius, cp->mFp, |
| 415 | cp->mIradius * 2 + 1, x1, x1 + len); |
| 416 | out += len; |
| 417 | x1 += len; |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | #endif |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 422 | while(x2 > x1) { |
| David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 423 | OneHU1(info, out, x1, buf, cp->mFp, cp->mIradius); |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 424 | out++; |
| 425 | x1++; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | RsdCpuScriptIntrinsicBlur::RsdCpuScriptIntrinsicBlur(RsdCpuReferenceImpl *ctx, |
| 430 | const Script *s, const Element *e) |
| 431 | : RsdCpuScriptIntrinsic(ctx, s, e, RS_SCRIPT_INTRINSIC_ID_BLUR) { |
| 432 | |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 433 | mRootPtr = nullptr; |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 434 | if (e->getType() == RS_TYPE_UNSIGNED_8) { |
| 435 | switch (e->getVectorSize()) { |
| 436 | case 1: |
| 437 | mRootPtr = &kernelU1; |
| 438 | break; |
| 439 | case 4: |
| 440 | mRootPtr = &kernelU4; |
| 441 | break; |
| 442 | } |
| 443 | } |
| 444 | rsAssert(mRootPtr); |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 445 | mRadius = 5; |
| 446 | |
| 447 | mScratch = new void *[mCtx->getThreadCount()]; |
| 448 | mScratchSize = new size_t[mCtx->getThreadCount()]; |
| Jason Sams | 75adb82 | 2013-10-22 11:43:54 -0700 | [diff] [blame] | 449 | memset(mScratch, 0, sizeof(void *) * mCtx->getThreadCount()); |
| 450 | memset(mScratchSize, 0, sizeof(size_t) * mCtx->getThreadCount()); |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 451 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 452 | ComputeGaussianWeights(); |
| 453 | } |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 454 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 455 | RsdCpuScriptIntrinsicBlur::~RsdCpuScriptIntrinsicBlur() { |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 456 | uint32_t threads = mCtx->getThreadCount(); |
| 457 | if (mScratch) { |
| 458 | for (size_t i = 0; i < threads; i++) { |
| 459 | if (mScratch[i]) { |
| 460 | free(mScratch[i]); |
| 461 | } |
| 462 | } |
| 463 | delete []mScratch; |
| 464 | } |
| 465 | if (mScratchSize) { |
| 466 | delete []mScratchSize; |
| 467 | } |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | void RsdCpuScriptIntrinsicBlur::populateScript(Script *s) { |
| 471 | s->mHal.info.exportedVariableCount = 2; |
| 472 | } |
| 473 | |
| 474 | void RsdCpuScriptIntrinsicBlur::invokeFreeChildren() { |
| Jason Sams | c44d670 | 2012-11-28 18:37:52 -0800 | [diff] [blame] | 475 | mAlloc.clear(); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 476 | } |
| 477 | |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 478 | RsdCpuScriptImpl * rsdIntrinsic_Blur(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e) { |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 479 | |
| Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 480 | return new RsdCpuScriptIntrinsicBlur(ctx, s, e); |
| Jason Sams | d85e283 | 2012-09-11 16:04:27 -0700 | [diff] [blame] | 481 | } |
| Chih-Hung Hsieh | 462de21 | 2016-11-16 11:33:57 -0800 | [diff] [blame^] | 482 | |
| 483 | } // namespace renderscript |
| 484 | } // namespace android |