blob: 020fa6f29a6771eee65e38315dc571b328081499 [file] [log] [blame]
Jason Sams709a0972012-11-15 18:18:04 -08001/*
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
18#include "rsCpuIntrinsic.h"
19#include "rsCpuIntrinsicInlines.h"
20
21using namespace android;
22using namespace android::renderscript;
23
24namespace android {
25namespace renderscript {
26
27
28class RsdCpuScriptIntrinsicConvolve3x3 : public RsdCpuScriptIntrinsic {
29public:
30 virtual void populateScript(Script *);
31 virtual void invokeFreeChildren();
32
33 virtual void setGlobalVar(uint32_t slot, const void *data, size_t dataLength);
34 virtual void setGlobalObj(uint32_t slot, ObjectBase *data);
35
36 virtual ~RsdCpuScriptIntrinsicConvolve3x3();
Jason Samsc905efd2012-11-26 15:20:18 -080037 RsdCpuScriptIntrinsicConvolve3x3(RsdCpuReferenceImpl *ctx, const Script *s, const Element *);
Jason Sams709a0972012-11-15 18:18:04 -080038
39protected:
Jason Samsc905efd2012-11-26 15:20:18 -080040 float mFp[16];
41 short mIp[16];
42 ObjectBaseRef<const Allocation> mAlloc;
43 ObjectBaseRef<const Element> mElement;
Jason Sams709a0972012-11-15 18:18:04 -080044
45 static void kernel(const RsForEachStubParamStruct *p,
46 uint32_t xstart, uint32_t xend,
47 uint32_t instep, uint32_t outstep);
48};
49
50}
51}
52
53
54void RsdCpuScriptIntrinsicConvolve3x3::setGlobalObj(uint32_t slot, ObjectBase *data) {
55 rsAssert(slot == 1);
Jason Samsc905efd2012-11-26 15:20:18 -080056 mAlloc.set(static_cast<Allocation *>(data));
Jason Sams709a0972012-11-15 18:18:04 -080057}
58
59void RsdCpuScriptIntrinsicConvolve3x3::setGlobalVar(uint32_t slot, const void *data,
60 size_t dataLength) {
61 rsAssert(slot == 0);
Jason Samsc905efd2012-11-26 15:20:18 -080062 memcpy (&mFp, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -080063 for(int ct=0; ct < 9; ct++) {
Jason Samsc905efd2012-11-26 15:20:18 -080064 mIp[ct] = (short)(mFp[ct] * 255.f + 0.5f);
Jason Sams709a0972012-11-15 18:18:04 -080065 }
66}
67
68extern "C" void rsdIntrinsicConvolve3x3_K(void *dst, const void *y0, const void *y1,
69 const void *y2, const short *coef, uint32_t count);
70
71
72static void ConvolveOne(const RsForEachStubParamStruct *p, uint32_t x, uchar4 *out,
73 const uchar4 *py0, const uchar4 *py1, const uchar4 *py2,
74 const float* coeff) {
75
76 uint32_t x1 = rsMax((int32_t)x-1, 0);
Tim Murray4cca49b2012-11-21 14:42:33 -080077 uint32_t x2 = rsMin((int32_t)x+1, (int32_t)p->dimX-1);
Jason Sams709a0972012-11-15 18:18:04 -080078
79 float4 px = convert_float4(py0[x1]) * coeff[0] +
80 convert_float4(py0[x]) * coeff[1] +
81 convert_float4(py0[x2]) * coeff[2] +
82 convert_float4(py1[x1]) * coeff[3] +
83 convert_float4(py1[x]) * coeff[4] +
84 convert_float4(py1[x2]) * coeff[5] +
85 convert_float4(py2[x1]) * coeff[6] +
86 convert_float4(py2[x]) * coeff[7] +
87 convert_float4(py2[x2]) * coeff[8];
88
89 px = clamp(px, 0.f, 255.f);
90 uchar4 o = {(uchar)px.x, (uchar)px.y, (uchar)px.z, (uchar)px.w};
91 *out = o;
92}
93
94void RsdCpuScriptIntrinsicConvolve3x3::kernel(const RsForEachStubParamStruct *p,
95 uint32_t xstart, uint32_t xend,
96 uint32_t instep, uint32_t outstep) {
97 RsdCpuScriptIntrinsicConvolve3x3 *cp = (RsdCpuScriptIntrinsicConvolve3x3 *)p->usr;
98
Jason Samsc905efd2012-11-26 15:20:18 -080099 if (!cp->mAlloc.get()) {
Jason Sams709a0972012-11-15 18:18:04 -0800100 ALOGE("Convolve3x3 executed without input, skipping");
101 return;
102 }
Jason Samsc905efd2012-11-26 15:20:18 -0800103 const uchar *pin = (const uchar *)cp->mAlloc->mHal.drvState.lod[0].mallocPtr;
104 const size_t stride = cp->mAlloc->mHal.drvState.lod[0].stride;
Jason Sams709a0972012-11-15 18:18:04 -0800105
106 uint32_t y1 = rsMin((int32_t)p->y + 1, (int32_t)(p->dimY-1));
107 uint32_t y2 = rsMax((int32_t)p->y - 1, 0);
108 const uchar4 *py0 = (const uchar4 *)(pin + stride * y2);
109 const uchar4 *py1 = (const uchar4 *)(pin + stride * p->y);
110 const uchar4 *py2 = (const uchar4 *)(pin + stride * y1);
111
112 uchar4 *out = (uchar4 *)p->out;
113 uint32_t x1 = xstart;
114 uint32_t x2 = xend;
115 if(x1 == 0) {
Jason Samsc905efd2012-11-26 15:20:18 -0800116 ConvolveOne(p, 0, out, py0, py1, py2, cp->mFp);
Jason Sams709a0972012-11-15 18:18:04 -0800117 x1 ++;
118 out++;
119 }
120
121 if(x2 > x1) {
122#if defined(ARCH_ARM_HAVE_NEON)
123 int32_t len = (x2 - x1 - 1) >> 1;
124 if(len > 0) {
Jason Samsc905efd2012-11-26 15:20:18 -0800125 rsdIntrinsicConvolve3x3_K(out, &py0[x1-1], &py1[x1-1], &py2[x1-1], cp->mIp, len);
Jason Sams709a0972012-11-15 18:18:04 -0800126 x1 += len << 1;
127 out += len << 1;
128 }
129#endif
130
131 while(x1 != x2) {
Jason Samsc905efd2012-11-26 15:20:18 -0800132 ConvolveOne(p, x1, out, py0, py1, py2, cp->mFp);
Jason Sams709a0972012-11-15 18:18:04 -0800133 out++;
134 x1++;
135 }
136 }
137}
138
139RsdCpuScriptIntrinsicConvolve3x3::RsdCpuScriptIntrinsicConvolve3x3(
Jason Samsc905efd2012-11-26 15:20:18 -0800140 RsdCpuReferenceImpl *ctx, const Script *s, const Element *e)
141 : RsdCpuScriptIntrinsic(ctx, s, e, RS_SCRIPT_INTRINSIC_ID_CONVOLVE_3x3) {
Jason Sams709a0972012-11-15 18:18:04 -0800142
143 mRootPtr = &kernel;
144 for(int ct=0; ct < 9; ct++) {
Jason Samsc905efd2012-11-26 15:20:18 -0800145 mFp[ct] = 1.f / 9.f;
146 mIp[ct] = (short)(mFp[ct] * 255.f + 0.5f);
Jason Sams709a0972012-11-15 18:18:04 -0800147 }
148}
149
150RsdCpuScriptIntrinsicConvolve3x3::~RsdCpuScriptIntrinsicConvolve3x3() {
151}
152
153void RsdCpuScriptIntrinsicConvolve3x3::populateScript(Script *s) {
154 s->mHal.info.exportedVariableCount = 2;
155}
156
157void RsdCpuScriptIntrinsicConvolve3x3::invokeFreeChildren() {
Jason Samsc905efd2012-11-26 15:20:18 -0800158 mAlloc.clear();
Jason Sams709a0972012-11-15 18:18:04 -0800159}
160
161
Jason Samsc905efd2012-11-26 15:20:18 -0800162RsdCpuScriptImpl * rsdIntrinsic_Convolve3x3(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e) {
Jason Sams709a0972012-11-15 18:18:04 -0800163
Jason Samsc905efd2012-11-26 15:20:18 -0800164 return new RsdCpuScriptIntrinsicConvolve3x3(ctx, s, e);
Jason Sams709a0972012-11-15 18:18:04 -0800165}
166
167