blob: 668338442836a6512c14555b76f23da6d70973e5 [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:
Stephen Hinesc060f142015-05-13 19:26:09 -070029 void populateScript(Script *) override;
30 void invokeFreeChildren() override;
Jason Sams709a0972012-11-15 18:18:04 -080031
Stephen Hinesc060f142015-05-13 19:26:09 -070032 void setGlobalVar(uint32_t slot, const void *data, size_t dataLength) override;
33 void setGlobalObj(uint32_t slot, ObjectBase *data) override;
Jason Sams709a0972012-11-15 18:18:04 -080034
Stephen Hinesc060f142015-05-13 19:26:09 -070035 ~RsdCpuScriptIntrinsicBlur() override;
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:
Hidehiko Abef6009df2016-07-08 16:38:14 +090039 // The size of the kernel radius is limited to 25 in ScriptIntrinsicBlur.java.
40 // So, the max kernel size is 51 (= 2 * 25 + 1).
41 // Considering SSSE3 case, which requires the size is multiple of 4,
42 // at least 52 words are necessary. Values outside of the kernel should be 0.
Jason Samsc44d6702012-11-28 18:37:52 -080043 float mFp[104];
Simon Hosie44678802014-02-19 22:08:48 -080044 uint16_t mIp[104];
Jason Samsc44d6702012-11-28 18:37:52 -080045 void **mScratch;
46 size_t *mScratchSize;
47 float mRadius;
48 int mIradius;
49 ObjectBaseRef<Allocation> mAlloc;
Jason Sams709a0972012-11-15 18:18:04 -080050
David Grossb0abb142015-03-12 15:23:03 -070051 static void kernelU4(const RsExpandKernelDriverInfo *info,
Jason Samsc905efd2012-11-26 15:20:18 -080052 uint32_t xstart, uint32_t xend,
Chris Wailes9ed79102014-07-25 15:53:28 -070053 uint32_t outstep);
David Grossb0abb142015-03-12 15:23:03 -070054 static void kernelU1(const RsExpandKernelDriverInfo *info,
Jason Samsc905efd2012-11-26 15:20:18 -080055 uint32_t xstart, uint32_t xend,
Chris Wailes9ed79102014-07-25 15:53:28 -070056 uint32_t outstep);
Jason Sams709a0972012-11-15 18:18:04 -080057 void ComputeGaussianWeights();
Jason Samsd85e2832012-09-11 16:04:27 -070058};
59
Jason Sams709a0972012-11-15 18:18:04 -080060}
61}
62
63
64void RsdCpuScriptIntrinsicBlur::ComputeGaussianWeights() {
Jason Samsc44d6702012-11-28 18:37:52 -080065 memset(mFp, 0, sizeof(mFp));
66 memset(mIp, 0, sizeof(mIp));
Jason Sams7079cd82012-11-27 18:26:33 -080067
Jason Samsd85e2832012-09-11 16:04:27 -070068 // Compute gaussian weights for the blur
69 // e is the euler's number
Jean-Luc Brouillet8b7117d2014-04-30 13:48:03 -070070 // TODO Define these constants only once
Jason Samsd85e2832012-09-11 16:04:27 -070071 float e = 2.718281828459045f;
72 float pi = 3.1415926535897932f;
Jean-Luc Brouillet8b7117d2014-04-30 13:48:03 -070073 // g(x) = (1 / (sqrt(2 * pi) * sigma)) * e ^ (-x^2 / (2 * sigma^2))
Jason Samsd85e2832012-09-11 16:04:27 -070074 // x is of the form [-radius .. 0 .. radius]
Jean-Luc Brouillet8b7117d2014-04-30 13:48:03 -070075 // and sigma varies with the radius.
76 // Based on some experimental radius values and sigmas,
Jason Samsd85e2832012-09-11 16:04:27 -070077 // we approximately fit sigma = f(radius) as
78 // sigma = radius * 0.4 + 0.6
79 // The larger the radius gets, the more our gaussian blur
80 // will resemble a box blur since with large sigma
81 // the gaussian curve begins to lose its shape
Jason Samsc44d6702012-11-28 18:37:52 -080082 float sigma = 0.4f * mRadius + 0.6f;
Jason Samsd85e2832012-09-11 16:04:27 -070083
84 // Now compute the coefficients. We will store some redundant values to save
85 // some math during the blur calculations precompute some values
86 float coeff1 = 1.0f / (sqrtf(2.0f * pi) * sigma);
87 float coeff2 = - 1.0f / (2.0f * sigma * sigma);
88
89 float normalizeFactor = 0.0f;
90 float floatR = 0.0f;
91 int r;
Jason Samsc44d6702012-11-28 18:37:52 -080092 mIradius = (float)ceil(mRadius) + 0.5f;
93 for (r = -mIradius; r <= mIradius; r ++) {
Jason Samsd85e2832012-09-11 16:04:27 -070094 floatR = (float)r;
Jason Samsc44d6702012-11-28 18:37:52 -080095 mFp[r + mIradius] = coeff1 * powf(e, floatR * floatR * coeff2);
96 normalizeFactor += mFp[r + mIradius];
Jason Samsd85e2832012-09-11 16:04:27 -070097 }
98
Jean-Luc Brouillet8b7117d2014-04-30 13:48:03 -070099 // Now we need to normalize the weights because all our coefficients need to add up to one
Jason Samsd85e2832012-09-11 16:04:27 -0700100 normalizeFactor = 1.0f / normalizeFactor;
Jason Samsc44d6702012-11-28 18:37:52 -0800101 for (r = -mIradius; r <= mIradius; r ++) {
102 mFp[r + mIradius] *= normalizeFactor;
Simon Hosie44678802014-02-19 22:08:48 -0800103 mIp[r + mIradius] = (uint16_t)(mFp[r + mIradius] * 65536.0f + 0.5f);
Jason Samsd85e2832012-09-11 16:04:27 -0700104 }
105}
106
Jason Sams709a0972012-11-15 18:18:04 -0800107void RsdCpuScriptIntrinsicBlur::setGlobalObj(uint32_t slot, ObjectBase *data) {
Jason Samsd85e2832012-09-11 16:04:27 -0700108 rsAssert(slot == 1);
Jason Samsc44d6702012-11-28 18:37:52 -0800109 mAlloc.set(static_cast<Allocation *>(data));
Jason Samsd85e2832012-09-11 16:04:27 -0700110}
111
Jason Sams709a0972012-11-15 18:18:04 -0800112void RsdCpuScriptIntrinsicBlur::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
Jason Samsd85e2832012-09-11 16:04:27 -0700113 rsAssert(slot == 0);
Jason Samsc44d6702012-11-28 18:37:52 -0800114 mRadius = ((const float *)data)[0];
Jason Sams709a0972012-11-15 18:18:04 -0800115 ComputeGaussianWeights();
Jason Samsd85e2832012-09-11 16:04:27 -0700116}
117
Jason Samsd85e2832012-09-11 16:04:27 -0700118
119
David Grossb0abb142015-03-12 15:23:03 -0700120static void OneVU4(const RsExpandKernelDriverInfo *info, float4 *out, int32_t x, int32_t y,
Jason Samsc905efd2012-11-26 15:20:18 -0800121 const uchar *ptrIn, int iStride, const float* gPtr, int iradius) {
Jason Samsd85e2832012-09-11 16:04:27 -0700122
123 const uchar *pi = ptrIn + x*4;
124
125 float4 blurredPixel = 0;
126 for (int r = -iradius; r <= iradius; r ++) {
127 int validY = rsMax((y + r), 0);
David Grossb0abb142015-03-12 15:23:03 -0700128 validY = rsMin(validY, (int)(info->dim.y- 1));
Jason Samsd85e2832012-09-11 16:04:27 -0700129 const uchar4 *pvy = (const uchar4 *)&pi[validY * iStride];
130 float4 pf = convert_float4(pvy[0]);
131 blurredPixel += pf * gPtr[0];
132 gPtr++;
133 }
134
Jason Samsd25fef72014-08-21 16:18:39 -0700135 out[0] = blurredPixel;
Jason Samsd85e2832012-09-11 16:04:27 -0700136}
137
David Grossb0abb142015-03-12 15:23:03 -0700138static void OneVU1(const RsExpandKernelDriverInfo *info, float *out, int32_t x, int32_t y,
Jason Samsc905efd2012-11-26 15:20:18 -0800139 const uchar *ptrIn, int iStride, const float* gPtr, int iradius) {
Jason Samse78e5142012-09-19 00:46:31 -0700140
Jason Samsc905efd2012-11-26 15:20:18 -0800141 const uchar *pi = ptrIn + x;
142
143 float blurredPixel = 0;
144 for (int r = -iradius; r <= iradius; r ++) {
145 int validY = rsMax((y + r), 0);
David Grossb0abb142015-03-12 15:23:03 -0700146 validY = rsMin(validY, (int)(info->dim.y - 1));
Jason Samsc905efd2012-11-26 15:20:18 -0800147 float pf = (float)pi[validY * iStride];
148 blurredPixel += pf * gPtr[0];
149 gPtr++;
150 }
151
152 out[0] = blurredPixel;
153}
154
Simon Hosie44678802014-02-19 22:08:48 -0800155
156extern "C" void rsdIntrinsicBlurU1_K(uchar *out, uchar const *in, size_t w, size_t h,
157 size_t p, size_t x, size_t y, size_t count, size_t r, uint16_t const *tab);
158extern "C" void rsdIntrinsicBlurU4_K(uchar4 *out, uchar4 const *in, size_t w, size_t h,
159 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 -0800160
Rose, James7b7060c2014-04-22 12:08:06 +0800161#if defined(ARCH_X86_HAVE_SSSE3)
Dan Albertebf0eb92014-08-22 13:19:24 -0700162extern void rsdIntrinsicBlurVFU4_K(void *dst, const void *pin, int stride, const void *gptr, int rct, int x1, int ct);
163extern void rsdIntrinsicBlurHFU4_K(void *dst, const void *pin, const void *gptr, int rct, int x1, int ct);
164extern void rsdIntrinsicBlurHFU1_K(void *dst, const void *pin, const void *gptr, int rct, int x1, int ct);
Rose, James7b7060c2014-04-22 12:08:06 +0800165#endif
166
Jason Samsc905efd2012-11-26 15:20:18 -0800167static void OneVFU4(float4 *out,
168 const uchar *ptrIn, int iStride, const float* gPtr, int ct,
169 int x1, int x2) {
Jason Samsd25fef72014-08-21 16:18:39 -0700170 out += x1;
Rose, James7b7060c2014-04-22 12:08:06 +0800171#if defined(ARCH_X86_HAVE_SSSE3)
172 if (gArchUseSIMD) {
173 int t = (x2 - x1);
174 t &= ~1;
175 if (t) {
176 rsdIntrinsicBlurVFU4_K(out, ptrIn, iStride, gPtr, ct, x1, x1 + t);
177 }
178 x1 += t;
Yong Chena0cdfe02014-09-01 13:20:26 +0800179 out += t;
180 ptrIn += t << 2;
Rose, James7b7060c2014-04-22 12:08:06 +0800181 }
182#endif
Jason Samse78e5142012-09-19 00:46:31 -0700183 while(x2 > x1) {
Tim Murray2e5ef662012-10-22 14:46:39 -0700184 const uchar *pi = ptrIn;
Jason Samse78e5142012-09-19 00:46:31 -0700185 float4 blurredPixel = 0;
186 const float* gp = gPtr;
187
188 for (int r = 0; r < ct; r++) {
189 float4 pf = convert_float4(((const uchar4 *)pi)[0]);
190 blurredPixel += pf * gp[0];
191 pi += iStride;
192 gp++;
193 }
194 out->xyzw = blurredPixel;
195 x1++;
196 out++;
Jason Sams07821882013-04-02 13:15:58 -0700197 ptrIn+=4;
Jason Samse78e5142012-09-19 00:46:31 -0700198 }
199}
200
Jason Samsc905efd2012-11-26 15:20:18 -0800201static void OneVFU1(float *out,
Jason Samsc44d6702012-11-28 18:37:52 -0800202 const uchar *ptrIn, int iStride, const float* gPtr, int ct, int x1, int x2) {
Jason Samsc905efd2012-11-26 15:20:18 -0800203
Jason Samsc44d6702012-11-28 18:37:52 -0800204 int len = x2 - x1;
Jason Samsd25fef72014-08-21 16:18:39 -0700205 out += x1;
Jason Samsc44d6702012-11-28 18:37:52 -0800206
Tim Murray099bc262013-03-20 16:54:03 -0700207 while((x2 > x1) && (((uintptr_t)ptrIn) & 0x3)) {
Jason Sams7079cd82012-11-27 18:26:33 -0800208 const uchar *pi = ptrIn;
209 float blurredPixel = 0;
210 const float* gp = gPtr;
211
212 for (int r = 0; r < ct; r++) {
213 float pf = (float)pi[0];
214 blurredPixel += pf * gp[0];
215 pi += iStride;
216 gp++;
217 }
218 out[0] = blurredPixel;
Jason Samsc44d6702012-11-28 18:37:52 -0800219 x1++;
Jason Sams7079cd82012-11-27 18:26:33 -0800220 out++;
221 ptrIn++;
Jason Samsb6d2d2a2013-02-22 18:10:04 -0800222 len--;
Jason Sams7079cd82012-11-27 18:26:33 -0800223 }
Rose, James7b7060c2014-04-22 12:08:06 +0800224#if defined(ARCH_X86_HAVE_SSSE3)
225 if (gArchUseSIMD && (x2 > x1)) {
226 int t = (x2 - x1) >> 2;
227 t &= ~1;
228 if (t) {
229 rsdIntrinsicBlurVFU4_K(out, ptrIn, iStride, gPtr, ct, 0, t );
230 len -= t << 2;
231 ptrIn += t << 2;
232 out += t << 2;
233 }
234 }
235#endif
Jason Samsb6d2d2a2013-02-22 18:10:04 -0800236 while(len > 0) {
Jason Samsc905efd2012-11-26 15:20:18 -0800237 const uchar *pi = ptrIn;
238 float blurredPixel = 0;
239 const float* gp = gPtr;
240
241 for (int r = 0; r < ct; r++) {
242 float pf = (float)pi[0];
243 blurredPixel += pf * gp[0];
244 pi += iStride;
245 gp++;
246 }
247 out[0] = blurredPixel;
248 len--;
249 out++;
Jason Sams7079cd82012-11-27 18:26:33 -0800250 ptrIn++;
Jason Samsc905efd2012-11-26 15:20:18 -0800251 }
252}
253
David Grossb0abb142015-03-12 15:23:03 -0700254static void OneHU4(const RsExpandKernelDriverInfo *info, uchar4 *out, int32_t x,
Jason Samsc905efd2012-11-26 15:20:18 -0800255 const float4 *ptrIn, const float* gPtr, int iradius) {
Jason Samsd85e2832012-09-11 16:04:27 -0700256
257 float4 blurredPixel = 0;
258 for (int r = -iradius; r <= iradius; r ++) {
259 int validX = rsMax((x + r), 0);
David Grossb0abb142015-03-12 15:23:03 -0700260 validX = rsMin(validX, (int)(info->dim.x - 1));
Jason Samsd85e2832012-09-11 16:04:27 -0700261 float4 pf = ptrIn[validX];
262 blurredPixel += pf * gPtr[0];
263 gPtr++;
264 }
265
266 out->xyzw = convert_uchar4(blurredPixel);
267}
268
David Grossb0abb142015-03-12 15:23:03 -0700269static void OneHU1(const RsExpandKernelDriverInfo *info, uchar *out, int32_t x,
Jason Samsc905efd2012-11-26 15:20:18 -0800270 const float *ptrIn, const float* gPtr, int iradius) {
Jason Samsd85e2832012-09-11 16:04:27 -0700271
Jason Samsc905efd2012-11-26 15:20:18 -0800272 float blurredPixel = 0;
273 for (int r = -iradius; r <= iradius; r ++) {
274 int validX = rsMax((x + r), 0);
David Grossb0abb142015-03-12 15:23:03 -0700275 validX = rsMin(validX, (int)(info->dim.x - 1));
Jason Samsc905efd2012-11-26 15:20:18 -0800276 float pf = ptrIn[validX];
277 blurredPixel += pf * gPtr[0];
278 gPtr++;
279 }
280
281 out[0] = (uchar)blurredPixel;
282}
283
284
David Grossb0abb142015-03-12 15:23:03 -0700285void RsdCpuScriptIntrinsicBlur::kernelU4(const RsExpandKernelDriverInfo *info,
Jason Samsc905efd2012-11-26 15:20:18 -0800286 uint32_t xstart, uint32_t xend,
Chris Wailes9ed79102014-07-25 15:53:28 -0700287 uint32_t outstep) {
Jason Samsc44d6702012-11-28 18:37:52 -0800288
Stephen Hines2913f382013-01-14 20:44:09 -0800289 float4 stackbuf[2048];
290 float4 *buf = &stackbuf[0];
David Grossb0abb142015-03-12 15:23:03 -0700291 RsdCpuScriptIntrinsicBlur *cp = (RsdCpuScriptIntrinsicBlur *)info->usr;
Jason Samsc44d6702012-11-28 18:37:52 -0800292 if (!cp->mAlloc.get()) {
Jason Samsb801b942012-10-10 12:07:38 -0700293 ALOGE("Blur executed without input, skipping");
294 return;
295 }
Jason Samsc44d6702012-11-28 18:37:52 -0800296 const uchar *pin = (const uchar *)cp->mAlloc->mHal.drvState.lod[0].mallocPtr;
297 const size_t stride = cp->mAlloc->mHal.drvState.lod[0].stride;
Jason Samsd85e2832012-09-11 16:04:27 -0700298
David Grossb0abb142015-03-12 15:23:03 -0700299 uchar4 *out = (uchar4 *)info->outPtr[0];
Jason Samsd85e2832012-09-11 16:04:27 -0700300 uint32_t x1 = xstart;
301 uint32_t x2 = xend;
302
Jason Sams074424a2014-05-22 13:30:03 -0700303#if defined(ARCH_ARM_USE_INTRINSICS)
Simon Hosie5a1f1962016-04-20 16:20:25 -0700304 if (gArchUseSIMD && info->dim.x >= 4) {
David Grossb0abb142015-03-12 15:23:03 -0700305 rsdIntrinsicBlurU4_K(out, (uchar4 const *)(pin + stride * info->current.y),
306 info->dim.x, info->dim.y,
307 stride, x1, info->current.y, x2 - x1, cp->mIradius, cp->mIp + cp->mIradius);
Simon Hosie44678802014-02-19 22:08:48 -0800308 return;
309 }
310#endif
311
David Grossb0abb142015-03-12 15:23:03 -0700312 if (info->dim.x > 2048) {
313 if ((info->dim.x > cp->mScratchSize[info->lid]) || !cp->mScratch[info->lid]) {
Jason Sams75adb822013-10-22 11:43:54 -0700314 // Pad the side of the allocation by one unit to allow alignment later
David Grossb0abb142015-03-12 15:23:03 -0700315 cp->mScratch[info->lid] = realloc(cp->mScratch[info->lid], (info->dim.x + 1) * 16);
316 cp->mScratchSize[info->lid] = info->dim.x;
Jason Samsc44d6702012-11-28 18:37:52 -0800317 }
Jason Sams75adb822013-10-22 11:43:54 -0700318 // realloc only aligns to 8 bytes so we manually align to 16.
David Grossb0abb142015-03-12 15:23:03 -0700319 buf = (float4 *) ((((intptr_t)cp->mScratch[info->lid]) + 15) & ~0xf);
Jason Samsc44d6702012-11-28 18:37:52 -0800320 }
Jason Samsd85e2832012-09-11 16:04:27 -0700321 float4 *fout = (float4 *)buf;
David Grossb0abb142015-03-12 15:23:03 -0700322 int y = info->current.y;
323 if ((y > cp->mIradius) && (y < ((int)info->dim.y - cp->mIradius))) {
Jason Samsc44d6702012-11-28 18:37:52 -0800324 const uchar *pi = pin + (y - cp->mIradius) * stride;
David Grossb0abb142015-03-12 15:23:03 -0700325 OneVFU4(fout, pi, stride, cp->mFp, cp->mIradius * 2 + 1, 0, info->dim.x);
Jason Samse78e5142012-09-19 00:46:31 -0700326 } else {
Jason Samsd25fef72014-08-21 16:18:39 -0700327 x1 = 0;
David Grossb0abb142015-03-12 15:23:03 -0700328 while(info->dim.x > x1) {
329 OneVU4(info, fout, x1, y, pin, stride, cp->mFp, cp->mIradius);
Jason Samse78e5142012-09-19 00:46:31 -0700330 fout++;
331 x1++;
332 }
Jason Samsd85e2832012-09-11 16:04:27 -0700333 }
334
335 x1 = xstart;
Jason Samsc44d6702012-11-28 18:37:52 -0800336 while ((x1 < (uint32_t)cp->mIradius) && (x1 < x2)) {
David Grossb0abb142015-03-12 15:23:03 -0700337 OneHU4(info, out, x1, buf, cp->mFp, cp->mIradius);
Jason Samse78e5142012-09-19 00:46:31 -0700338 out++;
339 x1++;
340 }
Rose, James7b7060c2014-04-22 12:08:06 +0800341#if defined(ARCH_X86_HAVE_SSSE3)
342 if (gArchUseSIMD) {
343 if ((x1 + cp->mIradius) < x2) {
344 rsdIntrinsicBlurHFU4_K(out, buf - cp->mIradius, cp->mFp,
345 cp->mIradius * 2 + 1, x1, x2 - cp->mIradius);
346 out += (x2 - cp->mIradius) - x1;
347 x1 = x2 - cp->mIradius;
348 }
349 }
350#endif
Jason Samsd85e2832012-09-11 16:04:27 -0700351 while(x2 > x1) {
David Grossb0abb142015-03-12 15:23:03 -0700352 OneHU4(info, out, x1, buf, cp->mFp, cp->mIradius);
Jason Samsd85e2832012-09-11 16:04:27 -0700353 out++;
354 x1++;
355 }
Jason Samsd85e2832012-09-11 16:04:27 -0700356}
357
David Grossb0abb142015-03-12 15:23:03 -0700358void RsdCpuScriptIntrinsicBlur::kernelU1(const RsExpandKernelDriverInfo *info,
Jason Samsc905efd2012-11-26 15:20:18 -0800359 uint32_t xstart, uint32_t xend,
Chris Wailes9ed79102014-07-25 15:53:28 -0700360 uint32_t outstep) {
Jason Samsc905efd2012-11-26 15:20:18 -0800361 float buf[4 * 2048];
David Grossb0abb142015-03-12 15:23:03 -0700362 RsdCpuScriptIntrinsicBlur *cp = (RsdCpuScriptIntrinsicBlur *)info->usr;
Jason Samsc44d6702012-11-28 18:37:52 -0800363 if (!cp->mAlloc.get()) {
Jason Samsc905efd2012-11-26 15:20:18 -0800364 ALOGE("Blur executed without input, skipping");
365 return;
366 }
Jason Samsc44d6702012-11-28 18:37:52 -0800367 const uchar *pin = (const uchar *)cp->mAlloc->mHal.drvState.lod[0].mallocPtr;
368 const size_t stride = cp->mAlloc->mHal.drvState.lod[0].stride;
Jason Samsd85e2832012-09-11 16:04:27 -0700369
David Grossb0abb142015-03-12 15:23:03 -0700370 uchar *out = (uchar *)info->outPtr[0];
Jason Samsc905efd2012-11-26 15:20:18 -0800371 uint32_t x1 = xstart;
372 uint32_t x2 = xend;
373
Jason Sams074424a2014-05-22 13:30:03 -0700374#if defined(ARCH_ARM_USE_INTRINSICS)
Simon Hosie5a1f1962016-04-20 16:20:25 -0700375 if (gArchUseSIMD && info->dim.x >= 16) {
376 // The specialisation for r<=8 has an awkward prefill case, which is
377 // fiddly to resolve, where starting close to the right edge can cause
378 // a read beyond the end of input. So avoid that case here.
379 if (cp->mIradius > 8 || (info->dim.x - rsMax(0, (int32_t)x1 - 8)) >= 16) {
380 rsdIntrinsicBlurU1_K(out, pin + stride * info->current.y, info->dim.x, info->dim.y,
381 stride, x1, info->current.y, x2 - x1, cp->mIradius, cp->mIp + cp->mIradius);
382 return;
383 }
Simon Hosie44678802014-02-19 22:08:48 -0800384 }
385#endif
386
Jason Samsc905efd2012-11-26 15:20:18 -0800387 float *fout = (float *)buf;
David Grossb0abb142015-03-12 15:23:03 -0700388 int y = info->current.y;
389 if ((y > cp->mIradius) && (y < ((int)info->dim.y - cp->mIradius -1))) {
Jason Samsc44d6702012-11-28 18:37:52 -0800390 const uchar *pi = pin + (y - cp->mIradius) * stride;
David Grossb0abb142015-03-12 15:23:03 -0700391 OneVFU1(fout, pi, stride, cp->mFp, cp->mIradius * 2 + 1, 0, info->dim.x);
Jason Samsc905efd2012-11-26 15:20:18 -0800392 } else {
Jason Samsd25fef72014-08-21 16:18:39 -0700393 x1 = 0;
David Grossb0abb142015-03-12 15:23:03 -0700394 while(info->dim.x > x1) {
395 OneVU1(info, fout, x1, y, pin, stride, cp->mFp, cp->mIradius);
Jason Samsc905efd2012-11-26 15:20:18 -0800396 fout++;
397 x1++;
398 }
399 }
400
401 x1 = xstart;
Jason Sams7079cd82012-11-27 18:26:33 -0800402 while ((x1 < x2) &&
Tim Murray099bc262013-03-20 16:54:03 -0700403 ((x1 < (uint32_t)cp->mIradius) || (((uintptr_t)out) & 0x3))) {
David Grossb0abb142015-03-12 15:23:03 -0700404 OneHU1(info, out, x1, buf, cp->mFp, cp->mIradius);
Jason Samsc905efd2012-11-26 15:20:18 -0800405 out++;
406 x1++;
407 }
Rose, James7b7060c2014-04-22 12:08:06 +0800408#if defined(ARCH_X86_HAVE_SSSE3)
409 if (gArchUseSIMD) {
410 if ((x1 + cp->mIradius) < x2) {
411 uint32_t len = x2 - (x1 + cp->mIradius);
412 len &= ~3;
Hidehiko Abef6009df2016-07-08 16:38:14 +0900413
414 // rsdIntrinsicBlurHFU1_K() processes each four float values in |buf| at once, so it
415 // nees to ensure four more values can be accessed in order to avoid accessing
416 // uninitialized buffer.
417 if (len > 4) {
418 len -= 4;
Rose, James7b7060c2014-04-22 12:08:06 +0800419 rsdIntrinsicBlurHFU1_K(out, ((float *)buf) - cp->mIradius, cp->mFp,
420 cp->mIradius * 2 + 1, x1, x1 + len);
421 out += len;
422 x1 += len;
423 }
424 }
425 }
426#endif
Jason Samsc905efd2012-11-26 15:20:18 -0800427 while(x2 > x1) {
David Grossb0abb142015-03-12 15:23:03 -0700428 OneHU1(info, out, x1, buf, cp->mFp, cp->mIradius);
Jason Samsc905efd2012-11-26 15:20:18 -0800429 out++;
430 x1++;
431 }
432}
433
434RsdCpuScriptIntrinsicBlur::RsdCpuScriptIntrinsicBlur(RsdCpuReferenceImpl *ctx,
435 const Script *s, const Element *e)
436 : RsdCpuScriptIntrinsic(ctx, s, e, RS_SCRIPT_INTRINSIC_ID_BLUR) {
437
Chris Wailes44bef6f2014-08-12 13:51:10 -0700438 mRootPtr = nullptr;
Jason Samsc905efd2012-11-26 15:20:18 -0800439 if (e->getType() == RS_TYPE_UNSIGNED_8) {
440 switch (e->getVectorSize()) {
441 case 1:
442 mRootPtr = &kernelU1;
443 break;
444 case 4:
445 mRootPtr = &kernelU4;
446 break;
447 }
448 }
449 rsAssert(mRootPtr);
Jason Samsc44d6702012-11-28 18:37:52 -0800450 mRadius = 5;
451
452 mScratch = new void *[mCtx->getThreadCount()];
453 mScratchSize = new size_t[mCtx->getThreadCount()];
Jason Sams75adb822013-10-22 11:43:54 -0700454 memset(mScratch, 0, sizeof(void *) * mCtx->getThreadCount());
455 memset(mScratchSize, 0, sizeof(size_t) * mCtx->getThreadCount());
Jason Samsc44d6702012-11-28 18:37:52 -0800456
Jason Sams709a0972012-11-15 18:18:04 -0800457 ComputeGaussianWeights();
458}
Jason Samsd85e2832012-09-11 16:04:27 -0700459
Jason Sams709a0972012-11-15 18:18:04 -0800460RsdCpuScriptIntrinsicBlur::~RsdCpuScriptIntrinsicBlur() {
Jason Samsc44d6702012-11-28 18:37:52 -0800461 uint32_t threads = mCtx->getThreadCount();
462 if (mScratch) {
463 for (size_t i = 0; i < threads; i++) {
464 if (mScratch[i]) {
465 free(mScratch[i]);
466 }
467 }
468 delete []mScratch;
469 }
470 if (mScratchSize) {
471 delete []mScratchSize;
472 }
Jason Sams709a0972012-11-15 18:18:04 -0800473}
474
475void RsdCpuScriptIntrinsicBlur::populateScript(Script *s) {
476 s->mHal.info.exportedVariableCount = 2;
477}
478
479void RsdCpuScriptIntrinsicBlur::invokeFreeChildren() {
Jason Samsc44d6702012-11-28 18:37:52 -0800480 mAlloc.clear();
Jason Sams709a0972012-11-15 18:18:04 -0800481}
482
483
Jason Samsc905efd2012-11-26 15:20:18 -0800484RsdCpuScriptImpl * rsdIntrinsic_Blur(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e) {
Jason Sams709a0972012-11-15 18:18:04 -0800485
Jason Samsc905efd2012-11-26 15:20:18 -0800486 return new RsdCpuScriptIntrinsicBlur(ctx, s, e);
Jason Samsd85e2832012-09-11 16:04:27 -0700487}