blob: 0ee4ef658f34f3d04773637c2468ebd734ba2f89 [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
Jason Sams709a0972012-11-15 18:18:04 -080020namespace android {
21namespace renderscript {
22
Jason Sams709a0972012-11-15 18:18:04 -080023class RsdCpuScriptIntrinsicBlur : public RsdCpuScriptIntrinsic {
24public:
Stephen Hinesc060f142015-05-13 19:26:09 -070025 void populateScript(Script *) override;
26 void invokeFreeChildren() override;
Jason Sams709a0972012-11-15 18:18:04 -080027
Stephen Hinesc060f142015-05-13 19:26:09 -070028 void setGlobalVar(uint32_t slot, const void *data, size_t dataLength) override;
29 void setGlobalObj(uint32_t slot, ObjectBase *data) override;
Jason Sams709a0972012-11-15 18:18:04 -080030
Stephen Hinesc060f142015-05-13 19:26:09 -070031 ~RsdCpuScriptIntrinsicBlur() override;
Jason Samsc905efd2012-11-26 15:20:18 -080032 RsdCpuScriptIntrinsicBlur(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e);
Jason Sams709a0972012-11-15 18:18:04 -080033
34protected:
Hidehiko Abef6009df2016-07-08 16:38:14 +090035 // The size of the kernel radius is limited to 25 in ScriptIntrinsicBlur.java.
36 // So, the max kernel size is 51 (= 2 * 25 + 1).
37 // Considering SSSE3 case, which requires the size is multiple of 4,
38 // at least 52 words are necessary. Values outside of the kernel should be 0.
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
David Grossb0abb142015-03-12 15:23:03 -070047 static void kernelU4(const RsExpandKernelDriverInfo *info,
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);
David Grossb0abb142015-03-12 15:23:03 -070050 static void kernelU1(const RsExpandKernelDriverInfo *info,
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
57void RsdCpuScriptIntrinsicBlur::ComputeGaussianWeights() {
Jason Samsc44d6702012-11-28 18:37:52 -080058 memset(mFp, 0, sizeof(mFp));
59 memset(mIp, 0, sizeof(mIp));
Jason Sams7079cd82012-11-27 18:26:33 -080060
Jason Samsd85e2832012-09-11 16:04:27 -070061 // Compute gaussian weights for the blur
62 // e is the euler's number
Jean-Luc Brouillet8b7117d2014-04-30 13:48:03 -070063 // TODO Define these constants only once
Jason Samsd85e2832012-09-11 16:04:27 -070064 float e = 2.718281828459045f;
65 float pi = 3.1415926535897932f;
Jean-Luc Brouillet8b7117d2014-04-30 13:48:03 -070066 // g(x) = (1 / (sqrt(2 * pi) * sigma)) * e ^ (-x^2 / (2 * sigma^2))
Jason Samsd85e2832012-09-11 16:04:27 -070067 // x is of the form [-radius .. 0 .. radius]
Jean-Luc Brouillet8b7117d2014-04-30 13:48:03 -070068 // and sigma varies with the radius.
69 // Based on some experimental radius values and sigmas,
Jason Samsd85e2832012-09-11 16:04:27 -070070 // we approximately fit sigma = f(radius) as
71 // sigma = radius * 0.4 + 0.6
72 // The larger the radius gets, the more our gaussian blur
73 // will resemble a box blur since with large sigma
74 // the gaussian curve begins to lose its shape
Jason Samsc44d6702012-11-28 18:37:52 -080075 float sigma = 0.4f * mRadius + 0.6f;
Jason Samsd85e2832012-09-11 16:04:27 -070076
77 // Now compute the coefficients. We will store some redundant values to save
78 // some math during the blur calculations precompute some values
79 float coeff1 = 1.0f / (sqrtf(2.0f * pi) * sigma);
80 float coeff2 = - 1.0f / (2.0f * sigma * sigma);
81
82 float normalizeFactor = 0.0f;
83 float floatR = 0.0f;
84 int r;
Jason Samsc44d6702012-11-28 18:37:52 -080085 mIradius = (float)ceil(mRadius) + 0.5f;
86 for (r = -mIradius; r <= mIradius; r ++) {
Jason Samsd85e2832012-09-11 16:04:27 -070087 floatR = (float)r;
Jason Samsc44d6702012-11-28 18:37:52 -080088 mFp[r + mIradius] = coeff1 * powf(e, floatR * floatR * coeff2);
89 normalizeFactor += mFp[r + mIradius];
Jason Samsd85e2832012-09-11 16:04:27 -070090 }
91
Jean-Luc Brouillet8b7117d2014-04-30 13:48:03 -070092 // Now we need to normalize the weights because all our coefficients need to add up to one
Jason Samsd85e2832012-09-11 16:04:27 -070093 normalizeFactor = 1.0f / normalizeFactor;
Jason Samsc44d6702012-11-28 18:37:52 -080094 for (r = -mIradius; r <= mIradius; r ++) {
95 mFp[r + mIradius] *= normalizeFactor;
Simon Hosie44678802014-02-19 22:08:48 -080096 mIp[r + mIradius] = (uint16_t)(mFp[r + mIradius] * 65536.0f + 0.5f);
Jason Samsd85e2832012-09-11 16:04:27 -070097 }
98}
99
Jason Sams709a0972012-11-15 18:18:04 -0800100void RsdCpuScriptIntrinsicBlur::setGlobalObj(uint32_t slot, ObjectBase *data) {
Jason Samsd85e2832012-09-11 16:04:27 -0700101 rsAssert(slot == 1);
Jason Samsc44d6702012-11-28 18:37:52 -0800102 mAlloc.set(static_cast<Allocation *>(data));
Jason Samsd85e2832012-09-11 16:04:27 -0700103}
104
Jason Sams709a0972012-11-15 18:18:04 -0800105void RsdCpuScriptIntrinsicBlur::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
Jason Samsd85e2832012-09-11 16:04:27 -0700106 rsAssert(slot == 0);
Jason Samsc44d6702012-11-28 18:37:52 -0800107 mRadius = ((const float *)data)[0];
Jason Sams709a0972012-11-15 18:18:04 -0800108 ComputeGaussianWeights();
Jason Samsd85e2832012-09-11 16:04:27 -0700109}
110
Jason Samsd85e2832012-09-11 16:04:27 -0700111
112
David Grossb0abb142015-03-12 15:23:03 -0700113static void OneVU4(const RsExpandKernelDriverInfo *info, float4 *out, int32_t x, int32_t y,
Jason Samsc905efd2012-11-26 15:20:18 -0800114 const uchar *ptrIn, int iStride, const float* gPtr, int iradius) {
Jason Samsd85e2832012-09-11 16:04:27 -0700115
116 const uchar *pi = ptrIn + x*4;
117
118 float4 blurredPixel = 0;
119 for (int r = -iradius; r <= iradius; r ++) {
120 int validY = rsMax((y + r), 0);
David Grossb0abb142015-03-12 15:23:03 -0700121 validY = rsMin(validY, (int)(info->dim.y- 1));
Jason Samsd85e2832012-09-11 16:04:27 -0700122 const uchar4 *pvy = (const uchar4 *)&pi[validY * iStride];
123 float4 pf = convert_float4(pvy[0]);
124 blurredPixel += pf * gPtr[0];
125 gPtr++;
126 }
127
Jason Samsd25fef72014-08-21 16:18:39 -0700128 out[0] = blurredPixel;
Jason Samsd85e2832012-09-11 16:04:27 -0700129}
130
David Grossb0abb142015-03-12 15:23:03 -0700131static void OneVU1(const RsExpandKernelDriverInfo *info, float *out, int32_t x, int32_t y,
Jason Samsc905efd2012-11-26 15:20:18 -0800132 const uchar *ptrIn, int iStride, const float* gPtr, int iradius) {
Jason Samse78e5142012-09-19 00:46:31 -0700133
Jason Samsc905efd2012-11-26 15:20:18 -0800134 const uchar *pi = ptrIn + x;
135
136 float blurredPixel = 0;
137 for (int r = -iradius; r <= iradius; r ++) {
138 int validY = rsMax((y + r), 0);
David Grossb0abb142015-03-12 15:23:03 -0700139 validY = rsMin(validY, (int)(info->dim.y - 1));
Jason Samsc905efd2012-11-26 15:20:18 -0800140 float pf = (float)pi[validY * iStride];
141 blurredPixel += pf * gPtr[0];
142 gPtr++;
143 }
144
145 out[0] = blurredPixel;
146}
147
Chih-Hung Hsieh462de212016-11-16 11:33:57 -0800148} // namespace renderscript
149} // namespace android
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)
Dan Albertebf0eb92014-08-22 13:19:24 -0700158extern void rsdIntrinsicBlurVFU4_K(void *dst, const void *pin, int stride, const void *gptr, int rct, int x1, int ct);
159extern void rsdIntrinsicBlurHFU4_K(void *dst, const void *pin, const void *gptr, int rct, int x1, int ct);
160extern void rsdIntrinsicBlurHFU1_K(void *dst, const void *pin, const void *gptr, int rct, int x1, int ct);
Rose, James7b7060c2014-04-22 12:08:06 +0800161#endif
162
Chih-Hung Hsieh462de212016-11-16 11:33:57 -0800163using android::renderscript::gArchUseSIMD;
164
Jason Samsc905efd2012-11-26 15:20:18 -0800165static void OneVFU4(float4 *out,
166 const uchar *ptrIn, int iStride, const float* gPtr, int ct,
167 int x1, int x2) {
Jason Samsd25fef72014-08-21 16:18:39 -0700168 out += x1;
Rose, James7b7060c2014-04-22 12:08:06 +0800169#if defined(ARCH_X86_HAVE_SSSE3)
170 if (gArchUseSIMD) {
171 int t = (x2 - x1);
172 t &= ~1;
173 if (t) {
174 rsdIntrinsicBlurVFU4_K(out, ptrIn, iStride, gPtr, ct, x1, x1 + t);
175 }
176 x1 += t;
Yong Chena0cdfe02014-09-01 13:20:26 +0800177 out += t;
178 ptrIn += t << 2;
Rose, James7b7060c2014-04-22 12:08:06 +0800179 }
180#endif
Jason Samse78e5142012-09-19 00:46:31 -0700181 while(x2 > x1) {
Tim Murray2e5ef662012-10-22 14:46:39 -0700182 const uchar *pi = ptrIn;
Jason Samse78e5142012-09-19 00:46:31 -0700183 float4 blurredPixel = 0;
184 const float* gp = gPtr;
185
186 for (int r = 0; r < ct; r++) {
187 float4 pf = convert_float4(((const uchar4 *)pi)[0]);
188 blurredPixel += pf * gp[0];
189 pi += iStride;
190 gp++;
191 }
192 out->xyzw = blurredPixel;
193 x1++;
194 out++;
Jason Sams07821882013-04-02 13:15:58 -0700195 ptrIn+=4;
Jason Samse78e5142012-09-19 00:46:31 -0700196 }
197}
198
Jason Samsc905efd2012-11-26 15:20:18 -0800199static void OneVFU1(float *out,
Jason Samsc44d6702012-11-28 18:37:52 -0800200 const uchar *ptrIn, int iStride, const float* gPtr, int ct, int x1, int x2) {
Jason Samsc905efd2012-11-26 15:20:18 -0800201
Jason Samsc44d6702012-11-28 18:37:52 -0800202 int len = x2 - x1;
Jason Samsd25fef72014-08-21 16:18:39 -0700203 out += x1;
Jason Samsc44d6702012-11-28 18:37:52 -0800204
Tim Murray099bc262013-03-20 16:54:03 -0700205 while((x2 > x1) && (((uintptr_t)ptrIn) & 0x3)) {
Jason Sams7079cd82012-11-27 18:26:33 -0800206 const uchar *pi = ptrIn;
207 float blurredPixel = 0;
208 const float* gp = gPtr;
209
210 for (int r = 0; r < ct; r++) {
211 float pf = (float)pi[0];
212 blurredPixel += pf * gp[0];
213 pi += iStride;
214 gp++;
215 }
216 out[0] = blurredPixel;
Jason Samsc44d6702012-11-28 18:37:52 -0800217 x1++;
Jason Sams7079cd82012-11-27 18:26:33 -0800218 out++;
219 ptrIn++;
Jason Samsb6d2d2a2013-02-22 18:10:04 -0800220 len--;
Jason Sams7079cd82012-11-27 18:26:33 -0800221 }
Rose, James7b7060c2014-04-22 12:08:06 +0800222#if defined(ARCH_X86_HAVE_SSSE3)
223 if (gArchUseSIMD && (x2 > x1)) {
224 int t = (x2 - x1) >> 2;
225 t &= ~1;
226 if (t) {
227 rsdIntrinsicBlurVFU4_K(out, ptrIn, iStride, gPtr, ct, 0, t );
228 len -= t << 2;
229 ptrIn += t << 2;
230 out += t << 2;
231 }
232 }
233#endif
Jason Samsb6d2d2a2013-02-22 18:10:04 -0800234 while(len > 0) {
Jason Samsc905efd2012-11-26 15:20:18 -0800235 const uchar *pi = ptrIn;
236 float blurredPixel = 0;
237 const float* gp = gPtr;
238
239 for (int r = 0; r < ct; r++) {
240 float pf = (float)pi[0];
241 blurredPixel += pf * gp[0];
242 pi += iStride;
243 gp++;
244 }
245 out[0] = blurredPixel;
246 len--;
247 out++;
Jason Sams7079cd82012-11-27 18:26:33 -0800248 ptrIn++;
Jason Samsc905efd2012-11-26 15:20:18 -0800249 }
250}
251
Chih-Hung Hsieh462de212016-11-16 11:33:57 -0800252using android::renderscript::rsMin;
253using android::renderscript::rsMax;
254
David Grossb0abb142015-03-12 15:23:03 -0700255static void OneHU4(const RsExpandKernelDriverInfo *info, uchar4 *out, int32_t x,
Jason Samsc905efd2012-11-26 15:20:18 -0800256 const float4 *ptrIn, const float* gPtr, int iradius) {
Jason Samsd85e2832012-09-11 16:04:27 -0700257
258 float4 blurredPixel = 0;
259 for (int r = -iradius; r <= iradius; r ++) {
260 int validX = rsMax((x + r), 0);
David Grossb0abb142015-03-12 15:23:03 -0700261 validX = rsMin(validX, (int)(info->dim.x - 1));
Jason Samsd85e2832012-09-11 16:04:27 -0700262 float4 pf = ptrIn[validX];
263 blurredPixel += pf * gPtr[0];
264 gPtr++;
265 }
266
267 out->xyzw = convert_uchar4(blurredPixel);
268}
269
David Grossb0abb142015-03-12 15:23:03 -0700270static void OneHU1(const RsExpandKernelDriverInfo *info, uchar *out, int32_t x,
Jason Samsc905efd2012-11-26 15:20:18 -0800271 const float *ptrIn, const float* gPtr, int iradius) {
Jason Samsd85e2832012-09-11 16:04:27 -0700272
Jason Samsc905efd2012-11-26 15:20:18 -0800273 float blurredPixel = 0;
274 for (int r = -iradius; r <= iradius; r ++) {
275 int validX = rsMax((x + r), 0);
David Grossb0abb142015-03-12 15:23:03 -0700276 validX = rsMin(validX, (int)(info->dim.x - 1));
Jason Samsc905efd2012-11-26 15:20:18 -0800277 float pf = ptrIn[validX];
278 blurredPixel += pf * gPtr[0];
279 gPtr++;
280 }
281
282 out[0] = (uchar)blurredPixel;
283}
284
285
Chih-Hung Hsieh462de212016-11-16 11:33:57 -0800286namespace android {
287namespace renderscript {
288
David Grossb0abb142015-03-12 15:23:03 -0700289void RsdCpuScriptIntrinsicBlur::kernelU4(const RsExpandKernelDriverInfo *info,
Jason Samsc905efd2012-11-26 15:20:18 -0800290 uint32_t xstart, uint32_t xend,
Chris Wailes9ed79102014-07-25 15:53:28 -0700291 uint32_t outstep) {
Jason Samsc44d6702012-11-28 18:37:52 -0800292
Stephen Hines2913f382013-01-14 20:44:09 -0800293 float4 stackbuf[2048];
294 float4 *buf = &stackbuf[0];
David Grossb0abb142015-03-12 15:23:03 -0700295 RsdCpuScriptIntrinsicBlur *cp = (RsdCpuScriptIntrinsicBlur *)info->usr;
Jason Samsc44d6702012-11-28 18:37:52 -0800296 if (!cp->mAlloc.get()) {
Jason Samsb801b942012-10-10 12:07:38 -0700297 ALOGE("Blur executed without input, skipping");
298 return;
299 }
Jason Samsc44d6702012-11-28 18:37:52 -0800300 const uchar *pin = (const uchar *)cp->mAlloc->mHal.drvState.lod[0].mallocPtr;
301 const size_t stride = cp->mAlloc->mHal.drvState.lod[0].stride;
Jason Samsd85e2832012-09-11 16:04:27 -0700302
David Grossb0abb142015-03-12 15:23:03 -0700303 uchar4 *out = (uchar4 *)info->outPtr[0];
Jason Samsd85e2832012-09-11 16:04:27 -0700304 uint32_t x1 = xstart;
305 uint32_t x2 = xend;
306
Jason Sams074424a2014-05-22 13:30:03 -0700307#if defined(ARCH_ARM_USE_INTRINSICS)
Simon Hosie5a1f1962016-04-20 16:20:25 -0700308 if (gArchUseSIMD && info->dim.x >= 4) {
David Grossb0abb142015-03-12 15:23:03 -0700309 rsdIntrinsicBlurU4_K(out, (uchar4 const *)(pin + stride * info->current.y),
310 info->dim.x, info->dim.y,
311 stride, x1, info->current.y, x2 - x1, cp->mIradius, cp->mIp + cp->mIradius);
Simon Hosie44678802014-02-19 22:08:48 -0800312 return;
313 }
314#endif
315
David Grossb0abb142015-03-12 15:23:03 -0700316 if (info->dim.x > 2048) {
317 if ((info->dim.x > cp->mScratchSize[info->lid]) || !cp->mScratch[info->lid]) {
Jason Sams75adb822013-10-22 11:43:54 -0700318 // Pad the side of the allocation by one unit to allow alignment later
David Grossb0abb142015-03-12 15:23:03 -0700319 cp->mScratch[info->lid] = realloc(cp->mScratch[info->lid], (info->dim.x + 1) * 16);
320 cp->mScratchSize[info->lid] = info->dim.x;
Jason Samsc44d6702012-11-28 18:37:52 -0800321 }
Jason Sams75adb822013-10-22 11:43:54 -0700322 // realloc only aligns to 8 bytes so we manually align to 16.
David Grossb0abb142015-03-12 15:23:03 -0700323 buf = (float4 *) ((((intptr_t)cp->mScratch[info->lid]) + 15) & ~0xf);
Jason Samsc44d6702012-11-28 18:37:52 -0800324 }
Jason Samsd85e2832012-09-11 16:04:27 -0700325 float4 *fout = (float4 *)buf;
David Grossb0abb142015-03-12 15:23:03 -0700326 int y = info->current.y;
327 if ((y > cp->mIradius) && (y < ((int)info->dim.y - cp->mIradius))) {
Jason Samsc44d6702012-11-28 18:37:52 -0800328 const uchar *pi = pin + (y - cp->mIradius) * stride;
David Grossb0abb142015-03-12 15:23:03 -0700329 OneVFU4(fout, pi, stride, cp->mFp, cp->mIradius * 2 + 1, 0, info->dim.x);
Jason Samse78e5142012-09-19 00:46:31 -0700330 } else {
Jason Samsd25fef72014-08-21 16:18:39 -0700331 x1 = 0;
David Grossb0abb142015-03-12 15:23:03 -0700332 while(info->dim.x > x1) {
333 OneVU4(info, fout, x1, y, pin, stride, cp->mFp, cp->mIradius);
Jason Samse78e5142012-09-19 00:46:31 -0700334 fout++;
335 x1++;
336 }
Jason Samsd85e2832012-09-11 16:04:27 -0700337 }
338
339 x1 = xstart;
Jason Samsc44d6702012-11-28 18:37:52 -0800340 while ((x1 < (uint32_t)cp->mIradius) && (x1 < x2)) {
David Grossb0abb142015-03-12 15:23:03 -0700341 OneHU4(info, out, x1, buf, cp->mFp, cp->mIradius);
Jason Samse78e5142012-09-19 00:46:31 -0700342 out++;
343 x1++;
344 }
Rose, James7b7060c2014-04-22 12:08:06 +0800345#if defined(ARCH_X86_HAVE_SSSE3)
346 if (gArchUseSIMD) {
347 if ((x1 + cp->mIradius) < x2) {
348 rsdIntrinsicBlurHFU4_K(out, buf - cp->mIradius, cp->mFp,
349 cp->mIradius * 2 + 1, x1, x2 - cp->mIradius);
350 out += (x2 - cp->mIradius) - x1;
351 x1 = x2 - cp->mIradius;
352 }
353 }
354#endif
Jason Samsd85e2832012-09-11 16:04:27 -0700355 while(x2 > x1) {
David Grossb0abb142015-03-12 15:23:03 -0700356 OneHU4(info, out, x1, buf, cp->mFp, cp->mIradius);
Jason Samsd85e2832012-09-11 16:04:27 -0700357 out++;
358 x1++;
359 }
Jason Samsd85e2832012-09-11 16:04:27 -0700360}
361
David Grossb0abb142015-03-12 15:23:03 -0700362void RsdCpuScriptIntrinsicBlur::kernelU1(const RsExpandKernelDriverInfo *info,
Jason Samsc905efd2012-11-26 15:20:18 -0800363 uint32_t xstart, uint32_t xend,
Chris Wailes9ed79102014-07-25 15:53:28 -0700364 uint32_t outstep) {
Jason Samsc905efd2012-11-26 15:20:18 -0800365 float buf[4 * 2048];
David Grossb0abb142015-03-12 15:23:03 -0700366 RsdCpuScriptIntrinsicBlur *cp = (RsdCpuScriptIntrinsicBlur *)info->usr;
Jason Samsc44d6702012-11-28 18:37:52 -0800367 if (!cp->mAlloc.get()) {
Jason Samsc905efd2012-11-26 15:20:18 -0800368 ALOGE("Blur executed without input, skipping");
369 return;
370 }
Jason Samsc44d6702012-11-28 18:37:52 -0800371 const uchar *pin = (const uchar *)cp->mAlloc->mHal.drvState.lod[0].mallocPtr;
372 const size_t stride = cp->mAlloc->mHal.drvState.lod[0].stride;
Jason Samsd85e2832012-09-11 16:04:27 -0700373
David Grossb0abb142015-03-12 15:23:03 -0700374 uchar *out = (uchar *)info->outPtr[0];
Jason Samsc905efd2012-11-26 15:20:18 -0800375 uint32_t x1 = xstart;
376 uint32_t x2 = xend;
377
Jason Sams074424a2014-05-22 13:30:03 -0700378#if defined(ARCH_ARM_USE_INTRINSICS)
Simon Hosie5a1f1962016-04-20 16:20:25 -0700379 if (gArchUseSIMD && info->dim.x >= 16) {
380 // The specialisation for r<=8 has an awkward prefill case, which is
381 // fiddly to resolve, where starting close to the right edge can cause
382 // a read beyond the end of input. So avoid that case here.
383 if (cp->mIradius > 8 || (info->dim.x - rsMax(0, (int32_t)x1 - 8)) >= 16) {
384 rsdIntrinsicBlurU1_K(out, pin + stride * info->current.y, info->dim.x, info->dim.y,
385 stride, x1, info->current.y, x2 - x1, cp->mIradius, cp->mIp + cp->mIradius);
386 return;
387 }
Simon Hosie44678802014-02-19 22:08:48 -0800388 }
389#endif
390
Jason Samsc905efd2012-11-26 15:20:18 -0800391 float *fout = (float *)buf;
David Grossb0abb142015-03-12 15:23:03 -0700392 int y = info->current.y;
393 if ((y > cp->mIradius) && (y < ((int)info->dim.y - cp->mIradius -1))) {
Jason Samsc44d6702012-11-28 18:37:52 -0800394 const uchar *pi = pin + (y - cp->mIradius) * stride;
David Grossb0abb142015-03-12 15:23:03 -0700395 OneVFU1(fout, pi, stride, cp->mFp, cp->mIradius * 2 + 1, 0, info->dim.x);
Jason Samsc905efd2012-11-26 15:20:18 -0800396 } else {
Jason Samsd25fef72014-08-21 16:18:39 -0700397 x1 = 0;
David Grossb0abb142015-03-12 15:23:03 -0700398 while(info->dim.x > x1) {
399 OneVU1(info, fout, x1, y, pin, stride, cp->mFp, cp->mIradius);
Jason Samsc905efd2012-11-26 15:20:18 -0800400 fout++;
401 x1++;
402 }
403 }
404
405 x1 = xstart;
Jason Sams7079cd82012-11-27 18:26:33 -0800406 while ((x1 < x2) &&
Tim Murray099bc262013-03-20 16:54:03 -0700407 ((x1 < (uint32_t)cp->mIradius) || (((uintptr_t)out) & 0x3))) {
David Grossb0abb142015-03-12 15:23:03 -0700408 OneHU1(info, out, x1, buf, cp->mFp, cp->mIradius);
Jason Samsc905efd2012-11-26 15:20:18 -0800409 out++;
410 x1++;
411 }
Rose, James7b7060c2014-04-22 12:08:06 +0800412#if defined(ARCH_X86_HAVE_SSSE3)
413 if (gArchUseSIMD) {
414 if ((x1 + cp->mIradius) < x2) {
415 uint32_t len = x2 - (x1 + cp->mIradius);
416 len &= ~3;
Hidehiko Abef6009df2016-07-08 16:38:14 +0900417
418 // rsdIntrinsicBlurHFU1_K() processes each four float values in |buf| at once, so it
419 // nees to ensure four more values can be accessed in order to avoid accessing
420 // uninitialized buffer.
421 if (len > 4) {
422 len -= 4;
Rose, James7b7060c2014-04-22 12:08:06 +0800423 rsdIntrinsicBlurHFU1_K(out, ((float *)buf) - cp->mIradius, cp->mFp,
424 cp->mIradius * 2 + 1, x1, x1 + len);
425 out += len;
426 x1 += len;
427 }
428 }
429 }
430#endif
Jason Samsc905efd2012-11-26 15:20:18 -0800431 while(x2 > x1) {
David Grossb0abb142015-03-12 15:23:03 -0700432 OneHU1(info, out, x1, buf, cp->mFp, cp->mIradius);
Jason Samsc905efd2012-11-26 15:20:18 -0800433 out++;
434 x1++;
435 }
436}
437
438RsdCpuScriptIntrinsicBlur::RsdCpuScriptIntrinsicBlur(RsdCpuReferenceImpl *ctx,
439 const Script *s, const Element *e)
440 : RsdCpuScriptIntrinsic(ctx, s, e, RS_SCRIPT_INTRINSIC_ID_BLUR) {
441
Chris Wailes44bef6f2014-08-12 13:51:10 -0700442 mRootPtr = nullptr;
Jason Samsc905efd2012-11-26 15:20:18 -0800443 if (e->getType() == RS_TYPE_UNSIGNED_8) {
444 switch (e->getVectorSize()) {
445 case 1:
446 mRootPtr = &kernelU1;
447 break;
448 case 4:
449 mRootPtr = &kernelU4;
450 break;
451 }
452 }
453 rsAssert(mRootPtr);
Jason Samsc44d6702012-11-28 18:37:52 -0800454 mRadius = 5;
455
456 mScratch = new void *[mCtx->getThreadCount()];
457 mScratchSize = new size_t[mCtx->getThreadCount()];
Jason Sams75adb822013-10-22 11:43:54 -0700458 memset(mScratch, 0, sizeof(void *) * mCtx->getThreadCount());
459 memset(mScratchSize, 0, sizeof(size_t) * mCtx->getThreadCount());
Jason Samsc44d6702012-11-28 18:37:52 -0800460
Jason Sams709a0972012-11-15 18:18:04 -0800461 ComputeGaussianWeights();
462}
Jason Samsd85e2832012-09-11 16:04:27 -0700463
Jason Sams709a0972012-11-15 18:18:04 -0800464RsdCpuScriptIntrinsicBlur::~RsdCpuScriptIntrinsicBlur() {
Jason Samsc44d6702012-11-28 18:37:52 -0800465 uint32_t threads = mCtx->getThreadCount();
466 if (mScratch) {
467 for (size_t i = 0; i < threads; i++) {
468 if (mScratch[i]) {
469 free(mScratch[i]);
470 }
471 }
472 delete []mScratch;
473 }
474 if (mScratchSize) {
475 delete []mScratchSize;
476 }
Jason Sams709a0972012-11-15 18:18:04 -0800477}
478
479void RsdCpuScriptIntrinsicBlur::populateScript(Script *s) {
480 s->mHal.info.exportedVariableCount = 2;
481}
482
483void RsdCpuScriptIntrinsicBlur::invokeFreeChildren() {
Jason Samsc44d6702012-11-28 18:37:52 -0800484 mAlloc.clear();
Jason Sams709a0972012-11-15 18:18:04 -0800485}
486
Jason Samsc905efd2012-11-26 15:20:18 -0800487RsdCpuScriptImpl * rsdIntrinsic_Blur(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e) {
Jason Sams709a0972012-11-15 18:18:04 -0800488
Jason Samsc905efd2012-11-26 15:20:18 -0800489 return new RsdCpuScriptIntrinsicBlur(ctx, s, e);
Jason Samsd85e2832012-09-11 16:04:27 -0700490}
Chih-Hung Hsieh462de212016-11-16 11:33:57 -0800491
492} // namespace renderscript
493} // namespace android