blob: 3214d42c3e69a8c29ae837aa1e8aa8f0edc4a7da [file] [log] [blame]
Jason Samsd85e2832012-09-11 16:04:27 -07001/*
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 Sams709a0972012-11-15 18:18:04 -080017#include "rsCpuIntrinsic.h"
18#include "rsCpuIntrinsicInlines.h"
Jason Samsd85e2832012-09-11 16:04:27 -070019
20using namespace android;
21using namespace android::renderscript;
22
Jason Sams709a0972012-11-15 18:18:04 -080023namespace 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();
Jason Samsc905efd2012-11-26 15:20:18 -080036 RsdCpuScriptIntrinsicBlur(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e);
Jason Sams709a0972012-11-15 18:18:04 -080037
38protected:
Jason Samsc44d6702012-11-28 18:37:52 -080039 float mFp[104];
Simon Hosie44678802014-02-19 22:08:48 -080040 uint16_t mIp[104];
Jason Samsc44d6702012-11-28 18:37:52 -080041 void **mScratch;
42 size_t *mScratchSize;
43 float mRadius;
44 int mIradius;
45 ObjectBaseRef<Allocation> mAlloc;
Jason Sams709a0972012-11-15 18:18:04 -080046
Chris Wailes80ef6932014-07-08 11:22:18 -070047 static void kernelU4(const RsExpandKernelParams *p,
Jason Samsc905efd2012-11-26 15:20:18 -080048 uint32_t xstart, uint32_t xend,
Chris Wailes9ed79102014-07-25 15:53:28 -070049 uint32_t outstep);
Chris Wailes80ef6932014-07-08 11:22:18 -070050 static void kernelU1(const RsExpandKernelParams *p,
Jason Samsc905efd2012-11-26 15:20:18 -080051 uint32_t xstart, uint32_t xend,
Chris Wailes9ed79102014-07-25 15:53:28 -070052 uint32_t outstep);
Jason Sams709a0972012-11-15 18:18:04 -080053 void ComputeGaussianWeights();
Jason Samsd85e2832012-09-11 16:04:27 -070054};
55
Jason Sams709a0972012-11-15 18:18:04 -080056}
57}
58
59
60void RsdCpuScriptIntrinsicBlur::ComputeGaussianWeights() {
Jason Samsc44d6702012-11-28 18:37:52 -080061 memset(mFp, 0, sizeof(mFp));
62 memset(mIp, 0, sizeof(mIp));
Jason Sams7079cd82012-11-27 18:26:33 -080063
Jason Samsd85e2832012-09-11 16:04:27 -070064 // Compute gaussian weights for the blur
65 // e is the euler's number
Jean-Luc Brouillet8b7117d2014-04-30 13:48:03 -070066 // TODO Define these constants only once
Jason Samsd85e2832012-09-11 16:04:27 -070067 float e = 2.718281828459045f;
68 float pi = 3.1415926535897932f;
Jean-Luc Brouillet8b7117d2014-04-30 13:48:03 -070069 // g(x) = (1 / (sqrt(2 * pi) * sigma)) * e ^ (-x^2 / (2 * sigma^2))
Jason Samsd85e2832012-09-11 16:04:27 -070070 // x is of the form [-radius .. 0 .. radius]
Jean-Luc Brouillet8b7117d2014-04-30 13:48:03 -070071 // and sigma varies with the radius.
72 // Based on some experimental radius values and sigmas,
Jason Samsd85e2832012-09-11 16:04:27 -070073 // 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
Jason Samsc44d6702012-11-28 18:37:52 -080078 float sigma = 0.4f * mRadius + 0.6f;
Jason Samsd85e2832012-09-11 16:04:27 -070079
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;
Jason Samsc44d6702012-11-28 18:37:52 -080088 mIradius = (float)ceil(mRadius) + 0.5f;
89 for (r = -mIradius; r <= mIradius; r ++) {
Jason Samsd85e2832012-09-11 16:04:27 -070090 floatR = (float)r;
Jason Samsc44d6702012-11-28 18:37:52 -080091 mFp[r + mIradius] = coeff1 * powf(e, floatR * floatR * coeff2);
92 normalizeFactor += mFp[r + mIradius];
Jason Samsd85e2832012-09-11 16:04:27 -070093 }
94
Jean-Luc Brouillet8b7117d2014-04-30 13:48:03 -070095 // Now we need to normalize the weights because all our coefficients need to add up to one
Jason Samsd85e2832012-09-11 16:04:27 -070096 normalizeFactor = 1.0f / normalizeFactor;
Jason Samsc44d6702012-11-28 18:37:52 -080097 for (r = -mIradius; r <= mIradius; r ++) {
98 mFp[r + mIradius] *= normalizeFactor;
Simon Hosie44678802014-02-19 22:08:48 -080099 mIp[r + mIradius] = (uint16_t)(mFp[r + mIradius] * 65536.0f + 0.5f);
Jason Samsd85e2832012-09-11 16:04:27 -0700100 }
101}
102
Jason Sams709a0972012-11-15 18:18:04 -0800103void RsdCpuScriptIntrinsicBlur::setGlobalObj(uint32_t slot, ObjectBase *data) {
Jason Samsd85e2832012-09-11 16:04:27 -0700104 rsAssert(slot == 1);
Jason Samsc44d6702012-11-28 18:37:52 -0800105 mAlloc.set(static_cast<Allocation *>(data));
Jason Samsd85e2832012-09-11 16:04:27 -0700106}
107
Jason Sams709a0972012-11-15 18:18:04 -0800108void RsdCpuScriptIntrinsicBlur::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
Jason Samsd85e2832012-09-11 16:04:27 -0700109 rsAssert(slot == 0);
Jason Samsc44d6702012-11-28 18:37:52 -0800110 mRadius = ((const float *)data)[0];
Jason Sams709a0972012-11-15 18:18:04 -0800111 ComputeGaussianWeights();
Jason Samsd85e2832012-09-11 16:04:27 -0700112}
113
Jason Samsd85e2832012-09-11 16:04:27 -0700114
115
Chris Wailes80ef6932014-07-08 11:22:18 -0700116static void OneVU4(const RsExpandKernelParams *p, float4 *out, int32_t x, int32_t y,
Jason Samsc905efd2012-11-26 15:20:18 -0800117 const uchar *ptrIn, int iStride, const float* gPtr, int iradius) {
Jason Samsd85e2832012-09-11 16:04:27 -0700118
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
Chris Wailes80ef6932014-07-08 11:22:18 -0700134static void OneVU1(const RsExpandKernelParams *p, float *out, int32_t x, int32_t y,
Jason Samsc905efd2012-11-26 15:20:18 -0800135 const uchar *ptrIn, int iStride, const float* gPtr, int iradius) {
Jason Samse78e5142012-09-19 00:46:31 -0700136
Jason Samsc905efd2012-11-26 15:20:18 -0800137 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
Simon Hosie44678802014-02-19 22:08:48 -0800151
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);
Jason Samsc905efd2012-11-26 15:20:18 -0800156
Rose, James7b7060c2014-04-22 12:08:06 +0800157#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
Jason Samsc905efd2012-11-26 15:20:18 -0800163static void OneVFU4(float4 *out,
164 const uchar *ptrIn, int iStride, const float* gPtr, int ct,
165 int x1, int x2) {
Rose, James7b7060c2014-04-22 12:08:06 +0800166#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
Jason Samse78e5142012-09-19 00:46:31 -0700176 while(x2 > x1) {
Tim Murray2e5ef662012-10-22 14:46:39 -0700177 const uchar *pi = ptrIn;
Jason Samse78e5142012-09-19 00:46:31 -0700178 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++;
Jason Sams07821882013-04-02 13:15:58 -0700190 ptrIn+=4;
Jason Samse78e5142012-09-19 00:46:31 -0700191 }
192}
193
Jason Samsc905efd2012-11-26 15:20:18 -0800194static void OneVFU1(float *out,
Jason Samsc44d6702012-11-28 18:37:52 -0800195 const uchar *ptrIn, int iStride, const float* gPtr, int ct, int x1, int x2) {
Jason Samsc905efd2012-11-26 15:20:18 -0800196
Jason Samsc44d6702012-11-28 18:37:52 -0800197 int len = x2 - x1;
198
Tim Murray099bc262013-03-20 16:54:03 -0700199 while((x2 > x1) && (((uintptr_t)ptrIn) & 0x3)) {
Jason Sams7079cd82012-11-27 18:26:33 -0800200 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;
Jason Samsc44d6702012-11-28 18:37:52 -0800211 x1++;
Jason Sams7079cd82012-11-27 18:26:33 -0800212 out++;
213 ptrIn++;
Jason Samsb6d2d2a2013-02-22 18:10:04 -0800214 len--;
Jason Sams7079cd82012-11-27 18:26:33 -0800215 }
Rose, James7b7060c2014-04-22 12:08:06 +0800216#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
Jason Samsb6d2d2a2013-02-22 18:10:04 -0800228 while(len > 0) {
Jason Samsc905efd2012-11-26 15:20:18 -0800229 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++;
Jason Sams7079cd82012-11-27 18:26:33 -0800242 ptrIn++;
Jason Samsc905efd2012-11-26 15:20:18 -0800243 }
244}
245
Chris Wailes80ef6932014-07-08 11:22:18 -0700246static void OneHU4(const RsExpandKernelParams *p, uchar4 *out, int32_t x,
Jason Samsc905efd2012-11-26 15:20:18 -0800247 const float4 *ptrIn, const float* gPtr, int iradius) {
Jason Samsd85e2832012-09-11 16:04:27 -0700248
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
Chris Wailes80ef6932014-07-08 11:22:18 -0700261static void OneHU1(const RsExpandKernelParams *p, uchar *out, int32_t x,
Jason Samsc905efd2012-11-26 15:20:18 -0800262 const float *ptrIn, const float* gPtr, int iradius) {
Jason Samsd85e2832012-09-11 16:04:27 -0700263
Jason Samsc905efd2012-11-26 15:20:18 -0800264 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
Chris Wailes80ef6932014-07-08 11:22:18 -0700277void RsdCpuScriptIntrinsicBlur::kernelU4(const RsExpandKernelParams *p,
Jason Samsc905efd2012-11-26 15:20:18 -0800278 uint32_t xstart, uint32_t xend,
Chris Wailes9ed79102014-07-25 15:53:28 -0700279 uint32_t outstep) {
Jason Samsc44d6702012-11-28 18:37:52 -0800280
Stephen Hines2913f382013-01-14 20:44:09 -0800281 float4 stackbuf[2048];
282 float4 *buf = &stackbuf[0];
Jason Sams709a0972012-11-15 18:18:04 -0800283 RsdCpuScriptIntrinsicBlur *cp = (RsdCpuScriptIntrinsicBlur *)p->usr;
Jason Samsc44d6702012-11-28 18:37:52 -0800284 if (!cp->mAlloc.get()) {
Jason Samsb801b942012-10-10 12:07:38 -0700285 ALOGE("Blur executed without input, skipping");
286 return;
287 }
Jason Samsc44d6702012-11-28 18:37:52 -0800288 const uchar *pin = (const uchar *)cp->mAlloc->mHal.drvState.lod[0].mallocPtr;
289 const size_t stride = cp->mAlloc->mHal.drvState.lod[0].stride;
Jason Samsd85e2832012-09-11 16:04:27 -0700290
291 uchar4 *out = (uchar4 *)p->out;
292 uint32_t x1 = xstart;
293 uint32_t x2 = xend;
294
Jason Sams074424a2014-05-22 13:30:03 -0700295#if defined(ARCH_ARM_USE_INTRINSICS)
Simon Hosie44678802014-02-19 22:08:48 -0800296 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
Jason Samsc44d6702012-11-28 18:37:52 -0800303 if (p->dimX > 2048) {
304 if ((p->dimX > cp->mScratchSize[p->lid]) || !cp->mScratch[p->lid]) {
Jason Sams75adb822013-10-22 11:43:54 -0700305 // 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);
Jason Samsc44d6702012-11-28 18:37:52 -0800307 cp->mScratchSize[p->lid] = p->dimX;
308 }
Jason Sams75adb822013-10-22 11:43:54 -0700309 // realloc only aligns to 8 bytes so we manually align to 16.
310 buf = (float4 *) ((((intptr_t)cp->mScratch[p->lid]) + 15) & ~0xf);
Jason Samsc44d6702012-11-28 18:37:52 -0800311 }
Jason Samsd85e2832012-09-11 16:04:27 -0700312 float4 *fout = (float4 *)buf;
Jason Samse78e5142012-09-19 00:46:31 -0700313 int y = p->y;
Jason Samsc44d6702012-11-28 18:37:52 -0800314 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);
Jason Samse78e5142012-09-19 00:46:31 -0700317 } else {
318 while(x2 > x1) {
Jason Samsc44d6702012-11-28 18:37:52 -0800319 OneVU4(p, fout, x1, y, pin, stride, cp->mFp, cp->mIradius);
Jason Samse78e5142012-09-19 00:46:31 -0700320 fout++;
321 x1++;
322 }
Jason Samsd85e2832012-09-11 16:04:27 -0700323 }
324
325 x1 = xstart;
Jason Samsc44d6702012-11-28 18:37:52 -0800326 while ((x1 < (uint32_t)cp->mIradius) && (x1 < x2)) {
Stephen Hines2913f382013-01-14 20:44:09 -0800327 OneHU4(p, out, x1, buf, cp->mFp, cp->mIradius);
Jason Samse78e5142012-09-19 00:46:31 -0700328 out++;
329 x1++;
330 }
Rose, James7b7060c2014-04-22 12:08:06 +0800331#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
Jason Samsd85e2832012-09-11 16:04:27 -0700341 while(x2 > x1) {
Stephen Hines2913f382013-01-14 20:44:09 -0800342 OneHU4(p, out, x1, buf, cp->mFp, cp->mIradius);
Jason Samsd85e2832012-09-11 16:04:27 -0700343 out++;
344 x1++;
345 }
Jason Samsd85e2832012-09-11 16:04:27 -0700346}
347
Chris Wailes80ef6932014-07-08 11:22:18 -0700348void RsdCpuScriptIntrinsicBlur::kernelU1(const RsExpandKernelParams *p,
Jason Samsc905efd2012-11-26 15:20:18 -0800349 uint32_t xstart, uint32_t xend,
Chris Wailes9ed79102014-07-25 15:53:28 -0700350 uint32_t outstep) {
Jason Samsc905efd2012-11-26 15:20:18 -0800351 float buf[4 * 2048];
352 RsdCpuScriptIntrinsicBlur *cp = (RsdCpuScriptIntrinsicBlur *)p->usr;
Jason Samsc44d6702012-11-28 18:37:52 -0800353 if (!cp->mAlloc.get()) {
Jason Samsc905efd2012-11-26 15:20:18 -0800354 ALOGE("Blur executed without input, skipping");
355 return;
356 }
Jason Samsc44d6702012-11-28 18:37:52 -0800357 const uchar *pin = (const uchar *)cp->mAlloc->mHal.drvState.lod[0].mallocPtr;
358 const size_t stride = cp->mAlloc->mHal.drvState.lod[0].stride;
Jason Samsd85e2832012-09-11 16:04:27 -0700359
Jason Samsc905efd2012-11-26 15:20:18 -0800360 uchar *out = (uchar *)p->out;
361 uint32_t x1 = xstart;
362 uint32_t x2 = xend;
363
Jason Sams074424a2014-05-22 13:30:03 -0700364#if defined(ARCH_ARM_USE_INTRINSICS)
Simon Hosie44678802014-02-19 22:08:48 -0800365 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
Jason Samsc905efd2012-11-26 15:20:18 -0800372 float *fout = (float *)buf;
373 int y = p->y;
Jason Samsce0351d2013-01-25 19:44:04 -0800374 if ((y > cp->mIradius) && (y < ((int)p->dimY - cp->mIradius -1))) {
Jason Samsc44d6702012-11-28 18:37:52 -0800375 const uchar *pi = pin + (y - cp->mIradius) * stride;
376 OneVFU1(fout, pi, stride, cp->mFp, cp->mIradius * 2 + 1, x1, x2);
Jason Samsc905efd2012-11-26 15:20:18 -0800377 } else {
378 while(x2 > x1) {
Jason Samsc44d6702012-11-28 18:37:52 -0800379 OneVU1(p, fout, x1, y, pin, stride, cp->mFp, cp->mIradius);
Jason Samsc905efd2012-11-26 15:20:18 -0800380 fout++;
381 x1++;
382 }
383 }
384
385 x1 = xstart;
Jason Sams7079cd82012-11-27 18:26:33 -0800386 while ((x1 < x2) &&
Tim Murray099bc262013-03-20 16:54:03 -0700387 ((x1 < (uint32_t)cp->mIradius) || (((uintptr_t)out) & 0x3))) {
Jason Samsc44d6702012-11-28 18:37:52 -0800388 OneHU1(p, out, x1, buf, cp->mFp, cp->mIradius);
Jason Samsc905efd2012-11-26 15:20:18 -0800389 out++;
390 x1++;
391 }
Rose, James7b7060c2014-04-22 12:08:06 +0800392#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
Jason Samsc905efd2012-11-26 15:20:18 -0800406 while(x2 > x1) {
Jason Samsc44d6702012-11-28 18:37:52 -0800407 OneHU1(p, out, x1, buf, cp->mFp, cp->mIradius);
Jason Samsc905efd2012-11-26 15:20:18 -0800408 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
Chris Wailes44bef6f2014-08-12 13:51:10 -0700417 mRootPtr = nullptr;
Jason Samsc905efd2012-11-26 15:20:18 -0800418 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);
Jason Samsc44d6702012-11-28 18:37:52 -0800429 mRadius = 5;
430
431 mScratch = new void *[mCtx->getThreadCount()];
432 mScratchSize = new size_t[mCtx->getThreadCount()];
Jason Sams75adb822013-10-22 11:43:54 -0700433 memset(mScratch, 0, sizeof(void *) * mCtx->getThreadCount());
434 memset(mScratchSize, 0, sizeof(size_t) * mCtx->getThreadCount());
Jason Samsc44d6702012-11-28 18:37:52 -0800435
Jason Sams709a0972012-11-15 18:18:04 -0800436 ComputeGaussianWeights();
437}
Jason Samsd85e2832012-09-11 16:04:27 -0700438
Jason Sams709a0972012-11-15 18:18:04 -0800439RsdCpuScriptIntrinsicBlur::~RsdCpuScriptIntrinsicBlur() {
Jason Samsc44d6702012-11-28 18:37:52 -0800440 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 }
Jason Sams709a0972012-11-15 18:18:04 -0800452}
453
454void RsdCpuScriptIntrinsicBlur::populateScript(Script *s) {
455 s->mHal.info.exportedVariableCount = 2;
456}
457
458void RsdCpuScriptIntrinsicBlur::invokeFreeChildren() {
Jason Samsc44d6702012-11-28 18:37:52 -0800459 mAlloc.clear();
Jason Sams709a0972012-11-15 18:18:04 -0800460}
461
462
Jason Samsc905efd2012-11-26 15:20:18 -0800463RsdCpuScriptImpl * rsdIntrinsic_Blur(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e) {
Jason Sams709a0972012-11-15 18:18:04 -0800464
Jason Samsc905efd2012-11-26 15:20:18 -0800465 return new RsdCpuScriptIntrinsicBlur(ctx, s, e);
Jason Samsd85e2832012-09-11 16:04:27 -0700466}