rsCpuIntrinsicBlur.cpp revision 7b7060c61e4182b29186849c5a857ea5f0898e56
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 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 77 float sigma = 0.4f * mRadius + 0.6f; 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; 87 mIradius = (float)ceil(mRadius) + 0.5f; 88 for (r = -mIradius; r <= mIradius; r ++) { 89 floatR = (float)r; 90 mFp[r + mIradius] = coeff1 * powf(e, floatR * floatR * coeff2); 91 normalizeFactor += mFp[r + mIradius]; 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; 96 for (r = -mIradius; r <= mIradius; r ++) { 97 mFp[r + mIradius] *= normalizeFactor; 98 mIp[r + mIradius] = (uint16_t)(mFp[r + mIradius] * 65536.0f + 0.5f); 99 } 100} 101 102void RsdCpuScriptIntrinsicBlur::setGlobalObj(uint32_t slot, ObjectBase *data) { 103 rsAssert(slot == 1); 104 mAlloc.set(static_cast<Allocation *>(data)); 105} 106 107void RsdCpuScriptIntrinsicBlur::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) { 108 rsAssert(slot == 0); 109 mRadius = ((const float *)data)[0]; 110 ComputeGaussianWeights(); 111} 112 113 114 115static void OneVU4(const RsForEachStubParamStruct *p, float4 *out, int32_t x, int32_t y, 116 const uchar *ptrIn, int iStride, const float* gPtr, int iradius) { 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 133static void OneVU1(const RsForEachStubParamStruct *p, float *out, int32_t x, int32_t y, 134 const uchar *ptrIn, int iStride, const float* gPtr, int iradius) { 135 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 151extern "C" void rsdIntrinsicBlurU1_K(uchar *out, uchar const *in, size_t w, size_t h, 152 size_t p, size_t x, size_t y, size_t count, size_t r, uint16_t const *tab); 153extern "C" void rsdIntrinsicBlurU4_K(uchar4 *out, uchar4 const *in, size_t w, size_t h, 154 size_t p, size_t x, size_t y, size_t count, size_t r, uint16_t const *tab); 155 156#if defined(ARCH_X86_HAVE_SSSE3) 157extern "C" void rsdIntrinsicBlurVFU4_K(void *dst, const void *pin, int stride, const void *gptr, int rct, int x1, int ct); 158extern "C" void rsdIntrinsicBlurHFU4_K(void *dst, const void *pin, const void *gptr, int rct, int x1, int ct); 159extern "C" void rsdIntrinsicBlurHFU1_K(void *dst, const void *pin, const void *gptr, int rct, int x1, int ct); 160#endif 161 162static void OneVFU4(float4 *out, 163 const uchar *ptrIn, int iStride, const float* gPtr, int ct, 164 int x1, int x2) { 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; 173 } 174#endif 175 while(x2 > x1) { 176 const uchar *pi = ptrIn; 177 float4 blurredPixel = 0; 178 const float* gp = gPtr; 179 180 for (int r = 0; r < ct; r++) { 181 float4 pf = convert_float4(((const uchar4 *)pi)[0]); 182 blurredPixel += pf * gp[0]; 183 pi += iStride; 184 gp++; 185 } 186 out->xyzw = blurredPixel; 187 x1++; 188 out++; 189 ptrIn+=4; 190 } 191} 192 193static void OneVFU1(float *out, 194 const uchar *ptrIn, int iStride, const float* gPtr, int ct, int x1, int x2) { 195 196 int len = x2 - x1; 197 198 while((x2 > x1) && (((uintptr_t)ptrIn) & 0x3)) { 199 const uchar *pi = ptrIn; 200 float blurredPixel = 0; 201 const float* gp = gPtr; 202 203 for (int r = 0; r < ct; r++) { 204 float pf = (float)pi[0]; 205 blurredPixel += pf * gp[0]; 206 pi += iStride; 207 gp++; 208 } 209 out[0] = blurredPixel; 210 x1++; 211 out++; 212 ptrIn++; 213 len--; 214 } 215#if defined(ARCH_X86_HAVE_SSSE3) 216 if (gArchUseSIMD && (x2 > x1)) { 217 int t = (x2 - x1) >> 2; 218 t &= ~1; 219 if (t) { 220 rsdIntrinsicBlurVFU4_K(out, ptrIn, iStride, gPtr, ct, 0, t ); 221 len -= t << 2; 222 ptrIn += t << 2; 223 out += t << 2; 224 } 225 } 226#endif 227 while(len > 0) { 228 const uchar *pi = ptrIn; 229 float blurredPixel = 0; 230 const float* gp = gPtr; 231 232 for (int r = 0; r < ct; r++) { 233 float pf = (float)pi[0]; 234 blurredPixel += pf * gp[0]; 235 pi += iStride; 236 gp++; 237 } 238 out[0] = blurredPixel; 239 len--; 240 out++; 241 ptrIn++; 242 } 243} 244 245static void OneHU4(const RsForEachStubParamStruct *p, uchar4 *out, int32_t x, 246 const float4 *ptrIn, const float* gPtr, int iradius) { 247 248 float4 blurredPixel = 0; 249 for (int r = -iradius; r <= iradius; r ++) { 250 int validX = rsMax((x + r), 0); 251 validX = rsMin(validX, (int)(p->dimX - 1)); 252 float4 pf = ptrIn[validX]; 253 blurredPixel += pf * gPtr[0]; 254 gPtr++; 255 } 256 257 out->xyzw = convert_uchar4(blurredPixel); 258} 259 260static void OneHU1(const RsForEachStubParamStruct *p, uchar *out, int32_t x, 261 const float *ptrIn, const float* gPtr, int iradius) { 262 263 float blurredPixel = 0; 264 for (int r = -iradius; r <= iradius; r ++) { 265 int validX = rsMax((x + r), 0); 266 validX = rsMin(validX, (int)(p->dimX - 1)); 267 float pf = ptrIn[validX]; 268 blurredPixel += pf * gPtr[0]; 269 gPtr++; 270 } 271 272 out[0] = (uchar)blurredPixel; 273} 274 275 276void RsdCpuScriptIntrinsicBlur::kernelU4(const RsForEachStubParamStruct *p, 277 uint32_t xstart, uint32_t xend, 278 uint32_t instep, uint32_t outstep) { 279 280 float4 stackbuf[2048]; 281 float4 *buf = &stackbuf[0]; 282 RsdCpuScriptIntrinsicBlur *cp = (RsdCpuScriptIntrinsicBlur *)p->usr; 283 if (!cp->mAlloc.get()) { 284 ALOGE("Blur executed without input, skipping"); 285 return; 286 } 287 const uchar *pin = (const uchar *)cp->mAlloc->mHal.drvState.lod[0].mallocPtr; 288 const size_t stride = cp->mAlloc->mHal.drvState.lod[0].stride; 289 290 uchar4 *out = (uchar4 *)p->out; 291 uint32_t x1 = xstart; 292 uint32_t x2 = xend; 293 294#if defined(ARCH_ARM_HAVE_VFP) 295 if (gArchUseSIMD) { 296 rsdIntrinsicBlurU4_K(out, (uchar4 const *)(pin + stride * p->y), p->dimX, p->dimY, 297 stride, x1, p->y, x2 - x1, cp->mIradius, cp->mIp + cp->mIradius); 298 return; 299 } 300#endif 301 302 if (p->dimX > 2048) { 303 if ((p->dimX > cp->mScratchSize[p->lid]) || !cp->mScratch[p->lid]) { 304 // Pad the side of the allocation by one unit to allow alignment later 305 cp->mScratch[p->lid] = realloc(cp->mScratch[p->lid], (p->dimX + 1) * 16); 306 cp->mScratchSize[p->lid] = p->dimX; 307 } 308 // realloc only aligns to 8 bytes so we manually align to 16. 309 buf = (float4 *) ((((intptr_t)cp->mScratch[p->lid]) + 15) & ~0xf); 310 } 311 float4 *fout = (float4 *)buf; 312 int y = p->y; 313 if ((y > cp->mIradius) && (y < ((int)p->dimY - cp->mIradius))) { 314 const uchar *pi = pin + (y - cp->mIradius) * stride; 315 OneVFU4(fout, pi, stride, cp->mFp, cp->mIradius * 2 + 1, x1, x2); 316 } else { 317 while(x2 > x1) { 318 OneVU4(p, fout, x1, y, pin, stride, cp->mFp, cp->mIradius); 319 fout++; 320 x1++; 321 } 322 } 323 324 x1 = xstart; 325 while ((x1 < (uint32_t)cp->mIradius) && (x1 < x2)) { 326 OneHU4(p, out, x1, buf, cp->mFp, cp->mIradius); 327 out++; 328 x1++; 329 } 330#if defined(ARCH_X86_HAVE_SSSE3) 331 if (gArchUseSIMD) { 332 if ((x1 + cp->mIradius) < x2) { 333 rsdIntrinsicBlurHFU4_K(out, buf - cp->mIradius, cp->mFp, 334 cp->mIradius * 2 + 1, x1, x2 - cp->mIradius); 335 out += (x2 - cp->mIradius) - x1; 336 x1 = x2 - cp->mIradius; 337 } 338 } 339#endif 340 while(x2 > x1) { 341 OneHU4(p, out, x1, buf, cp->mFp, cp->mIradius); 342 out++; 343 x1++; 344 } 345} 346 347void RsdCpuScriptIntrinsicBlur::kernelU1(const RsForEachStubParamStruct *p, 348 uint32_t xstart, uint32_t xend, 349 uint32_t instep, uint32_t outstep) { 350 float buf[4 * 2048]; 351 RsdCpuScriptIntrinsicBlur *cp = (RsdCpuScriptIntrinsicBlur *)p->usr; 352 if (!cp->mAlloc.get()) { 353 ALOGE("Blur executed without input, skipping"); 354 return; 355 } 356 const uchar *pin = (const uchar *)cp->mAlloc->mHal.drvState.lod[0].mallocPtr; 357 const size_t stride = cp->mAlloc->mHal.drvState.lod[0].stride; 358 359 uchar *out = (uchar *)p->out; 360 uint32_t x1 = xstart; 361 uint32_t x2 = xend; 362 363#if defined(ARCH_ARM_HAVE_VFP) 364 if (gArchUseSIMD) { 365 rsdIntrinsicBlurU1_K(out, pin + stride * p->y, p->dimX, p->dimY, 366 stride, x1, p->y, x2 - x1, cp->mIradius, cp->mIp + cp->mIradius); 367 return; 368 } 369#endif 370 371 float *fout = (float *)buf; 372 int y = p->y; 373 if ((y > cp->mIradius) && (y < ((int)p->dimY - cp->mIradius -1))) { 374 const uchar *pi = pin + (y - cp->mIradius) * stride; 375 OneVFU1(fout, pi, stride, cp->mFp, cp->mIradius * 2 + 1, x1, x2); 376 } else { 377 while(x2 > x1) { 378 OneVU1(p, fout, x1, y, pin, stride, cp->mFp, cp->mIradius); 379 fout++; 380 x1++; 381 } 382 } 383 384 x1 = xstart; 385 while ((x1 < x2) && 386 ((x1 < (uint32_t)cp->mIradius) || (((uintptr_t)out) & 0x3))) { 387 OneHU1(p, out, x1, buf, cp->mFp, cp->mIradius); 388 out++; 389 x1++; 390 } 391#if defined(ARCH_X86_HAVE_SSSE3) 392 if (gArchUseSIMD) { 393 if ((x1 + cp->mIradius) < x2) { 394 uint32_t len = x2 - (x1 + cp->mIradius); 395 len &= ~3; 396 if (len > 0) { 397 rsdIntrinsicBlurHFU1_K(out, ((float *)buf) - cp->mIradius, cp->mFp, 398 cp->mIradius * 2 + 1, x1, x1 + len); 399 out += len; 400 x1 += len; 401 } 402 } 403 } 404#endif 405 while(x2 > x1) { 406 OneHU1(p, out, x1, buf, cp->mFp, cp->mIradius); 407 out++; 408 x1++; 409 } 410} 411 412RsdCpuScriptIntrinsicBlur::RsdCpuScriptIntrinsicBlur(RsdCpuReferenceImpl *ctx, 413 const Script *s, const Element *e) 414 : RsdCpuScriptIntrinsic(ctx, s, e, RS_SCRIPT_INTRINSIC_ID_BLUR) { 415 416 mRootPtr = NULL; 417 if (e->getType() == RS_TYPE_UNSIGNED_8) { 418 switch (e->getVectorSize()) { 419 case 1: 420 mRootPtr = &kernelU1; 421 break; 422 case 4: 423 mRootPtr = &kernelU4; 424 break; 425 } 426 } 427 rsAssert(mRootPtr); 428 mRadius = 5; 429 430 mScratch = new void *[mCtx->getThreadCount()]; 431 mScratchSize = new size_t[mCtx->getThreadCount()]; 432 memset(mScratch, 0, sizeof(void *) * mCtx->getThreadCount()); 433 memset(mScratchSize, 0, sizeof(size_t) * mCtx->getThreadCount()); 434 435 ComputeGaussianWeights(); 436} 437 438RsdCpuScriptIntrinsicBlur::~RsdCpuScriptIntrinsicBlur() { 439 uint32_t threads = mCtx->getThreadCount(); 440 if (mScratch) { 441 for (size_t i = 0; i < threads; i++) { 442 if (mScratch[i]) { 443 free(mScratch[i]); 444 } 445 } 446 delete []mScratch; 447 } 448 if (mScratchSize) { 449 delete []mScratchSize; 450 } 451} 452 453void RsdCpuScriptIntrinsicBlur::populateScript(Script *s) { 454 s->mHal.info.exportedVariableCount = 2; 455} 456 457void RsdCpuScriptIntrinsicBlur::invokeFreeChildren() { 458 mAlloc.clear(); 459} 460 461 462RsdCpuScriptImpl * rsdIntrinsic_Blur(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e) { 463 464 return new RsdCpuScriptIntrinsicBlur(ctx, s, e); 465} 466 467 468