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