rsCpuIntrinsicBlur.cpp revision 8b7117dfbcca8a74dfd83aaff7f82d2d7e3abd8f
1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "rsCpuIntrinsic.h" 18#include "rsCpuIntrinsicInlines.h" 19 20using namespace android; 21using namespace android::renderscript; 22 23namespace android { 24namespace renderscript { 25 26 27class RsdCpuScriptIntrinsicBlur : public RsdCpuScriptIntrinsic { 28public: 29 virtual void populateScript(Script *); 30 virtual void invokeFreeChildren(); 31 32 virtual void setGlobalVar(uint32_t slot, const void *data, size_t dataLength); 33 virtual void setGlobalObj(uint32_t slot, ObjectBase *data); 34 35 virtual ~RsdCpuScriptIntrinsicBlur(); 36 RsdCpuScriptIntrinsicBlur(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e); 37 38protected: 39 float mFp[104]; 40 uint16_t mIp[104]; 41 void **mScratch; 42 size_t *mScratchSize; 43 float mRadius; 44 int mIradius; 45 ObjectBaseRef<Allocation> mAlloc; 46 47 static void kernelU4(const RsForEachStubParamStruct *p, 48 uint32_t xstart, uint32_t xend, 49 uint32_t instep, uint32_t outstep); 50 static void kernelU1(const RsForEachStubParamStruct *p, 51 uint32_t xstart, uint32_t xend, 52 uint32_t instep, uint32_t outstep); 53 void ComputeGaussianWeights(); 54}; 55 56} 57} 58 59 60void RsdCpuScriptIntrinsicBlur::ComputeGaussianWeights() { 61 memset(mFp, 0, sizeof(mFp)); 62 memset(mIp, 0, sizeof(mIp)); 63 64 // Compute gaussian weights for the blur 65 // e is the euler's number 66 // TODO Define these constants only once 67 float e = 2.718281828459045f; 68 float pi = 3.1415926535897932f; 69 // g(x) = (1 / (sqrt(2 * pi) * sigma)) * e ^ (-x^2 / (2 * sigma^2)) 70 // x is of the form [-radius .. 0 .. radius] 71 // and sigma varies with the radius. 72 // Based on some experimental radius values and sigmas, 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 78 float sigma = 0.4f * mRadius + 0.6f; 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; 88 mIradius = (float)ceil(mRadius) + 0.5f; 89 for (r = -mIradius; r <= mIradius; r ++) { 90 floatR = (float)r; 91 mFp[r + mIradius] = coeff1 * powf(e, floatR * floatR * coeff2); 92 normalizeFactor += mFp[r + mIradius]; 93 } 94 95 // Now we need to normalize the weights because all our coefficients need to add up to one 96 normalizeFactor = 1.0f / normalizeFactor; 97 for (r = -mIradius; r <= mIradius; r ++) { 98 mFp[r + mIradius] *= normalizeFactor; 99 mIp[r + mIradius] = (uint16_t)(mFp[r + mIradius] * 65536.0f + 0.5f); 100 } 101} 102 103void RsdCpuScriptIntrinsicBlur::setGlobalObj(uint32_t slot, ObjectBase *data) { 104 rsAssert(slot == 1); 105 mAlloc.set(static_cast<Allocation *>(data)); 106} 107 108void RsdCpuScriptIntrinsicBlur::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) { 109 rsAssert(slot == 0); 110 mRadius = ((const float *)data)[0]; 111 ComputeGaussianWeights(); 112} 113 114 115 116static void OneVU4(const RsForEachStubParamStruct *p, float4 *out, int32_t x, int32_t y, 117 const uchar *ptrIn, int iStride, const float* gPtr, int iradius) { 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 134static void OneVU1(const RsForEachStubParamStruct *p, float *out, int32_t x, int32_t y, 135 const uchar *ptrIn, int iStride, const float* gPtr, int iradius) { 136 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 151 152extern "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); 154extern "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); 156 157#if defined(ARCH_X86_HAVE_SSSE3) 158extern "C" void rsdIntrinsicBlurVFU4_K(void *dst, const void *pin, int stride, const void *gptr, int rct, int x1, int ct); 159extern "C" void rsdIntrinsicBlurHFU4_K(void *dst, const void *pin, const void *gptr, int rct, int x1, int ct); 160extern "C" void rsdIntrinsicBlurHFU1_K(void *dst, const void *pin, const void *gptr, int rct, int x1, int ct); 161#endif 162 163static void OneVFU4(float4 *out, 164 const uchar *ptrIn, int iStride, const float* gPtr, int ct, 165 int x1, int x2) { 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 176 while(x2 > x1) { 177 const uchar *pi = ptrIn; 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++; 190 ptrIn+=4; 191 } 192} 193 194static void OneVFU1(float *out, 195 const uchar *ptrIn, int iStride, const float* gPtr, int ct, int x1, int x2) { 196 197 int len = x2 - x1; 198 199 while((x2 > x1) && (((uintptr_t)ptrIn) & 0x3)) { 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; 211 x1++; 212 out++; 213 ptrIn++; 214 len--; 215 } 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 228 while(len > 0) { 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++; 242 ptrIn++; 243 } 244} 245 246static void OneHU4(const RsForEachStubParamStruct *p, uchar4 *out, int32_t x, 247 const float4 *ptrIn, const float* gPtr, int iradius) { 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 261static void OneHU1(const RsForEachStubParamStruct *p, uchar *out, int32_t x, 262 const float *ptrIn, const float* gPtr, int iradius) { 263 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 277void RsdCpuScriptIntrinsicBlur::kernelU4(const RsForEachStubParamStruct *p, 278 uint32_t xstart, uint32_t xend, 279 uint32_t instep, uint32_t outstep) { 280 281 float4 stackbuf[2048]; 282 float4 *buf = &stackbuf[0]; 283 RsdCpuScriptIntrinsicBlur *cp = (RsdCpuScriptIntrinsicBlur *)p->usr; 284 if (!cp->mAlloc.get()) { 285 ALOGE("Blur executed without input, skipping"); 286 return; 287 } 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; 290 291 uchar4 *out = (uchar4 *)p->out; 292 uint32_t x1 = xstart; 293 uint32_t x2 = xend; 294 295#if defined(ARCH_ARM_HAVE_VFP) 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 303 if (p->dimX > 2048) { 304 if ((p->dimX > cp->mScratchSize[p->lid]) || !cp->mScratch[p->lid]) { 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); 307 cp->mScratchSize[p->lid] = p->dimX; 308 } 309 // realloc only aligns to 8 bytes so we manually align to 16. 310 buf = (float4 *) ((((intptr_t)cp->mScratch[p->lid]) + 15) & ~0xf); 311 } 312 float4 *fout = (float4 *)buf; 313 int y = p->y; 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); 317 } else { 318 while(x2 > x1) { 319 OneVU4(p, fout, x1, y, pin, stride, cp->mFp, cp->mIradius); 320 fout++; 321 x1++; 322 } 323 } 324 325 x1 = xstart; 326 while ((x1 < (uint32_t)cp->mIradius) && (x1 < x2)) { 327 OneHU4(p, out, x1, buf, cp->mFp, cp->mIradius); 328 out++; 329 x1++; 330 } 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 341 while(x2 > x1) { 342 OneHU4(p, out, x1, buf, cp->mFp, cp->mIradius); 343 out++; 344 x1++; 345 } 346} 347 348void RsdCpuScriptIntrinsicBlur::kernelU1(const RsForEachStubParamStruct *p, 349 uint32_t xstart, uint32_t xend, 350 uint32_t instep, uint32_t outstep) { 351 float buf[4 * 2048]; 352 RsdCpuScriptIntrinsicBlur *cp = (RsdCpuScriptIntrinsicBlur *)p->usr; 353 if (!cp->mAlloc.get()) { 354 ALOGE("Blur executed without input, skipping"); 355 return; 356 } 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; 359 360 uchar *out = (uchar *)p->out; 361 uint32_t x1 = xstart; 362 uint32_t x2 = xend; 363 364#if defined(ARCH_ARM_HAVE_VFP) 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 372 float *fout = (float *)buf; 373 int y = p->y; 374 if ((y > cp->mIradius) && (y < ((int)p->dimY - cp->mIradius -1))) { 375 const uchar *pi = pin + (y - cp->mIradius) * stride; 376 OneVFU1(fout, pi, stride, cp->mFp, cp->mIradius * 2 + 1, x1, x2); 377 } else { 378 while(x2 > x1) { 379 OneVU1(p, fout, x1, y, pin, stride, cp->mFp, cp->mIradius); 380 fout++; 381 x1++; 382 } 383 } 384 385 x1 = xstart; 386 while ((x1 < x2) && 387 ((x1 < (uint32_t)cp->mIradius) || (((uintptr_t)out) & 0x3))) { 388 OneHU1(p, out, x1, buf, cp->mFp, cp->mIradius); 389 out++; 390 x1++; 391 } 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 406 while(x2 > x1) { 407 OneHU1(p, out, x1, buf, cp->mFp, cp->mIradius); 408 out++; 409 x1++; 410 } 411} 412 413RsdCpuScriptIntrinsicBlur::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); 429 mRadius = 5; 430 431 mScratch = new void *[mCtx->getThreadCount()]; 432 mScratchSize = new size_t[mCtx->getThreadCount()]; 433 memset(mScratch, 0, sizeof(void *) * mCtx->getThreadCount()); 434 memset(mScratchSize, 0, sizeof(size_t) * mCtx->getThreadCount()); 435 436 ComputeGaussianWeights(); 437} 438 439RsdCpuScriptIntrinsicBlur::~RsdCpuScriptIntrinsicBlur() { 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 } 452} 453 454void RsdCpuScriptIntrinsicBlur::populateScript(Script *s) { 455 s->mHal.info.exportedVariableCount = 2; 456} 457 458void RsdCpuScriptIntrinsicBlur::invokeFreeChildren() { 459 mAlloc.clear(); 460} 461 462 463RsdCpuScriptImpl * rsdIntrinsic_Blur(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e) { 464 465 return new RsdCpuScriptIntrinsicBlur(ctx, s, e); 466} 467 468 469