rsCpuIntrinsicBlur.cpp revision c905efd76fdcc1b8846b229bf7d991d185a7b4b7
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 fp[104]; 40 short ip[104]; 41 float radius; 42 int iradius; 43 ObjectBaseRef<Allocation> alloc; 44 45 static void kernelU4(const RsForEachStubParamStruct *p, 46 uint32_t xstart, uint32_t xend, 47 uint32_t instep, uint32_t outstep); 48 static void kernelU1(const RsForEachStubParamStruct *p, 49 uint32_t xstart, uint32_t xend, 50 uint32_t instep, uint32_t outstep); 51 void ComputeGaussianWeights(); 52}; 53 54} 55} 56 57 58void RsdCpuScriptIntrinsicBlur::ComputeGaussianWeights() { 59 // Compute gaussian weights for the blur 60 // e is the euler's number 61 float e = 2.718281828459045f; 62 float pi = 3.1415926535897932f; 63 // g(x) = ( 1 / sqrt( 2 * pi ) * sigma) * e ^ ( -x^2 / 2 * sigma^2 ) 64 // x is of the form [-radius .. 0 .. radius] 65 // and sigma varies with radius. 66 // Based on some experimental radius values and sigma's 67 // we approximately fit sigma = f(radius) as 68 // sigma = radius * 0.4 + 0.6 69 // The larger the radius gets, the more our gaussian blur 70 // will resemble a box blur since with large sigma 71 // the gaussian curve begins to lose its shape 72 float sigma = 0.4f * radius + 0.6f; 73 74 // Now compute the coefficients. We will store some redundant values to save 75 // some math during the blur calculations precompute some values 76 float coeff1 = 1.0f / (sqrtf(2.0f * pi) * sigma); 77 float coeff2 = - 1.0f / (2.0f * sigma * sigma); 78 79 float normalizeFactor = 0.0f; 80 float floatR = 0.0f; 81 int r; 82 iradius = (float)ceil(radius) + 0.5f; 83 for (r = -iradius; r <= iradius; r ++) { 84 floatR = (float)r; 85 fp[r + iradius] = coeff1 * powf(e, floatR * floatR * coeff2); 86 normalizeFactor += fp[r + iradius]; 87 } 88 89 //Now we need to normalize the weights because all our coefficients need to add up to one 90 normalizeFactor = 1.0f / normalizeFactor; 91 for (r = -iradius; r <= iradius; r ++) { 92 fp[r + iradius] *= normalizeFactor; 93 ip[r + iradius] = (short)(ip[r + iradius] * 32768); 94 } 95} 96 97void RsdCpuScriptIntrinsicBlur::setGlobalObj(uint32_t slot, ObjectBase *data) { 98 rsAssert(slot == 1); 99 alloc.set(static_cast<Allocation *>(data)); 100} 101 102void RsdCpuScriptIntrinsicBlur::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) { 103 rsAssert(slot == 0); 104 radius = ((const float *)data)[0]; 105 ComputeGaussianWeights(); 106} 107 108 109 110static void OneVU4(const RsForEachStubParamStruct *p, float4 *out, int32_t x, int32_t y, 111 const uchar *ptrIn, int iStride, const float* gPtr, int iradius) { 112 113 const uchar *pi = ptrIn + x*4; 114 115 float4 blurredPixel = 0; 116 for (int r = -iradius; r <= iradius; r ++) { 117 int validY = rsMax((y + r), 0); 118 validY = rsMin(validY, (int)(p->dimY - 1)); 119 const uchar4 *pvy = (const uchar4 *)&pi[validY * iStride]; 120 float4 pf = convert_float4(pvy[0]); 121 blurredPixel += pf * gPtr[0]; 122 gPtr++; 123 } 124 125 out->xyzw = blurredPixel; 126} 127 128static void OneVU1(const RsForEachStubParamStruct *p, float *out, int32_t x, int32_t y, 129 const uchar *ptrIn, int iStride, const float* gPtr, int iradius) { 130 131 const uchar *pi = ptrIn + x; 132 133 float blurredPixel = 0; 134 for (int r = -iradius; r <= iradius; r ++) { 135 int validY = rsMax((y + r), 0); 136 validY = rsMin(validY, (int)(p->dimY - 1)); 137 float pf = (float)pi[validY * iStride]; 138 blurredPixel += pf * gPtr[0]; 139 gPtr++; 140 } 141 142 out[0] = blurredPixel; 143} 144 145extern "C" void rsdIntrinsicBlurVFU4_K(void *dst, const void *pin, int stride, const void *gptr, int rct, int x1, int ct); 146extern "C" void rsdIntrinsicBlurHFU4_K(void *dst, const void *pin, const void *gptr, int rct, int x1, int ct); 147 148static void OneVFU4(float4 *out, 149 const uchar *ptrIn, int iStride, const float* gPtr, int ct, 150 int x1, int x2) { 151 152#if defined(ARCH_ARM_HAVE_NEON) 153 { 154 int t = (x2 - x1); 155 t &= ~1; 156 if(t) { 157 rsdIntrinsicBlurVFU4_K(out, ptrIn, iStride, gPtr, ct, x1, x1 + t); 158 } 159 x1 += t; 160 } 161#endif 162 163 while(x2 > x1) { 164 const uchar *pi = ptrIn; 165 float4 blurredPixel = 0; 166 const float* gp = gPtr; 167 168 for (int r = 0; r < ct; r++) { 169 float4 pf = convert_float4(((const uchar4 *)pi)[0]); 170 blurredPixel += pf * gp[0]; 171 pi += iStride; 172 gp++; 173 } 174 out->xyzw = blurredPixel; 175 x1++; 176 out++; 177 } 178} 179 180static void OneVFU1(float *out, 181 const uchar *ptrIn, int iStride, const float* gPtr, int ct, int len) { 182 183#if defined(ARCH_ARM_HAVE_NEON) 184 { 185 int t = len >> 2; 186 t &= ~1; 187 if(t) { 188 rsdIntrinsicBlurVFU4_K(out, ptrIn, iStride, gPtr, ct, 0, len); 189 } 190 len -= t << 2; 191 ptrIn += t << 2; 192 out += t << 2; 193 } 194#endif 195 196 while(len) { 197 const uchar *pi = ptrIn; 198 float blurredPixel = 0; 199 const float* gp = gPtr; 200 201 for (int r = 0; r < ct; r++) { 202 float pf = (float)pi[0]; 203 blurredPixel += pf * gp[0]; 204 pi += iStride; 205 gp++; 206 } 207 out[0] = blurredPixel; 208 len--; 209 out++; 210 } 211} 212 213static void OneHU4(const RsForEachStubParamStruct *p, uchar4 *out, int32_t x, 214 const float4 *ptrIn, const float* gPtr, int iradius) { 215 216 float4 blurredPixel = 0; 217 for (int r = -iradius; r <= iradius; r ++) { 218 int validX = rsMax((x + r), 0); 219 validX = rsMin(validX, (int)(p->dimX - 1)); 220 float4 pf = ptrIn[validX]; 221 blurredPixel += pf * gPtr[0]; 222 gPtr++; 223 } 224 225 out->xyzw = convert_uchar4(blurredPixel); 226} 227 228static void OneHU1(const RsForEachStubParamStruct *p, uchar *out, int32_t x, 229 const float *ptrIn, const float* gPtr, int iradius) { 230 231 float blurredPixel = 0; 232 for (int r = -iradius; r <= iradius; r ++) { 233 int validX = rsMax((x + r), 0); 234 validX = rsMin(validX, (int)(p->dimX - 1)); 235 float pf = ptrIn[validX]; 236 blurredPixel += pf * gPtr[0]; 237 gPtr++; 238 } 239 240 out[0] = (uchar)blurredPixel; 241} 242 243 244void RsdCpuScriptIntrinsicBlur::kernelU4(const RsForEachStubParamStruct *p, 245 uint32_t xstart, uint32_t xend, 246 uint32_t instep, uint32_t outstep) { 247 float buf[4 * 2048]; 248 RsdCpuScriptIntrinsicBlur *cp = (RsdCpuScriptIntrinsicBlur *)p->usr; 249 if (!cp->alloc.get()) { 250 ALOGE("Blur executed without input, skipping"); 251 return; 252 } 253 const uchar *pin = (const uchar *)cp->alloc->mHal.drvState.lod[0].mallocPtr; 254 const size_t stride = cp->alloc->mHal.drvState.lod[0].stride; 255 256 uchar4 *out = (uchar4 *)p->out; 257 uint32_t x1 = xstart; 258 uint32_t x2 = xend; 259 260 float4 *fout = (float4 *)buf; 261 int y = p->y; 262 if ((y > cp->iradius) && (y < ((int)p->dimY - cp->iradius))) { 263 const uchar *pi = pin + (y - cp->iradius) * stride; 264 OneVFU4(fout, pi, stride, cp->fp, cp->iradius * 2 + 1, x1, x2); 265 } else { 266 while(x2 > x1) { 267 OneVU4(p, fout, x1, y, pin, stride, cp->fp, cp->iradius); 268 fout++; 269 x1++; 270 } 271 } 272 273 x1 = xstart; 274 while ((x1 < (uint32_t)cp->iradius) && (x1 < x2)) { 275 OneHU4(p, out, x1, (float4 *)buf, cp->fp, cp->iradius); 276 out++; 277 x1++; 278 } 279#if defined(ARCH_ARM_HAVE_NEON) 280 if ((x1 + cp->iradius) < x2) { 281 rsdIntrinsicBlurHFU4_K(out, ((float4 *)buf) - cp->iradius, cp->fp, 282 cp->iradius * 2 + 1, x1, x2 - cp->iradius); 283 out += (x2 - cp->iradius) - x1; 284 x1 = x2 - cp->iradius; 285 } 286#endif 287 while(x2 > x1) { 288 OneHU4(p, out, x1, (float4 *)buf, cp->fp, cp->iradius); 289 out++; 290 x1++; 291 } 292} 293 294void RsdCpuScriptIntrinsicBlur::kernelU1(const RsForEachStubParamStruct *p, 295 uint32_t xstart, uint32_t xend, 296 uint32_t instep, uint32_t outstep) { 297 float buf[4 * 2048]; 298 RsdCpuScriptIntrinsicBlur *cp = (RsdCpuScriptIntrinsicBlur *)p->usr; 299 if (!cp->alloc.get()) { 300 ALOGE("Blur executed without input, skipping"); 301 return; 302 } 303 const uchar *pin = (const uchar *)cp->alloc->mHal.drvState.lod[0].mallocPtr; 304 const size_t stride = cp->alloc->mHal.drvState.lod[0].stride; 305 306 uchar *out = (uchar *)p->out; 307 uint32_t x1 = xstart; 308 uint32_t x2 = xend; 309 310 float *fout = (float *)buf; 311 int y = p->y; 312 if ((y > cp->iradius) && (y < ((int)p->dimY - cp->iradius))) { 313 const uchar *pi = pin + (y - cp->iradius) * stride; 314 OneVFU1(fout, pi, stride, cp->fp, cp->iradius * 2 + 1, x2-x1); 315 } else { 316 while(x2 > x1) { 317 OneVU1(p, fout, x1, y, pin, stride, cp->fp, cp->iradius); 318 fout++; 319 x1++; 320 } 321 } 322 323 x1 = xstart; 324 while ((x1 < (uint32_t)cp->iradius) && (x1 < x2)) { 325 OneHU1(p, out, x1, buf, cp->fp, cp->iradius); 326 out++; 327 x1++; 328 } 329#if 0//defined(ARCH_ARM_HAVE_NEON) 330 if ((x1 + cp->iradius) < x2) { 331 rsdIntrinsicBlurHFU4_K(out, ((float4 *)buf) - cp->iradius, cp->fp, cp->iradius * 2 + 1, x1, 0, x2 - cp->iradius); 332 out += (x2 - cp->iradius) - x1; 333 x1 = x2 - cp->iradius; 334 } 335#endif 336 while(x2 > x1) { 337 OneHU1(p, out, x1, buf, cp->fp, cp->iradius); 338 out++; 339 x1++; 340 } 341} 342 343RsdCpuScriptIntrinsicBlur::RsdCpuScriptIntrinsicBlur(RsdCpuReferenceImpl *ctx, 344 const Script *s, const Element *e) 345 : RsdCpuScriptIntrinsic(ctx, s, e, RS_SCRIPT_INTRINSIC_ID_BLUR) { 346 347 mRootPtr = NULL; 348 if (e->getType() == RS_TYPE_UNSIGNED_8) { 349 switch (e->getVectorSize()) { 350 case 1: 351 mRootPtr = &kernelU1; 352 break; 353 case 4: 354 mRootPtr = &kernelU4; 355 break; 356 } 357 } 358 rsAssert(mRootPtr); 359 radius = 5; 360 ComputeGaussianWeights(); 361} 362 363RsdCpuScriptIntrinsicBlur::~RsdCpuScriptIntrinsicBlur() { 364} 365 366void RsdCpuScriptIntrinsicBlur::populateScript(Script *s) { 367 s->mHal.info.exportedVariableCount = 2; 368} 369 370void RsdCpuScriptIntrinsicBlur::invokeFreeChildren() { 371 alloc.clear(); 372} 373 374 375RsdCpuScriptImpl * rsdIntrinsic_Blur(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e) { 376 377 return new RsdCpuScriptIntrinsicBlur(ctx, s, e); 378} 379 380 381