Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 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 | |
| 18 | #include "rsCpuIntrinsic.h" |
| 19 | #include "rsCpuIntrinsicInlines.h" |
| 20 | |
| 21 | using namespace android; |
| 22 | using namespace android::renderscript; |
| 23 | |
| 24 | namespace android { |
| 25 | namespace renderscript { |
| 26 | |
| 27 | |
| 28 | class RsdCpuScriptIntrinsicConvolve3x3 : public RsdCpuScriptIntrinsic { |
| 29 | public: |
| 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 Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 37 | RsdCpuScriptIntrinsicConvolve3x3(RsdCpuReferenceImpl *ctx, const Script *s, const Element *); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 38 | |
| 39 | protected: |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 40 | float mFp[16]; |
| 41 | short mIp[16]; |
| 42 | ObjectBaseRef<const Allocation> mAlloc; |
| 43 | ObjectBaseRef<const Element> mElement; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 44 | |
| 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 | |
| 54 | void RsdCpuScriptIntrinsicConvolve3x3::setGlobalObj(uint32_t slot, ObjectBase *data) { |
| 55 | rsAssert(slot == 1); |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 56 | mAlloc.set(static_cast<Allocation *>(data)); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void RsdCpuScriptIntrinsicConvolve3x3::setGlobalVar(uint32_t slot, const void *data, |
| 60 | size_t dataLength) { |
| 61 | rsAssert(slot == 0); |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 62 | memcpy (&mFp, data, dataLength); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 63 | for(int ct=0; ct < 9; ct++) { |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 64 | mIp[ct] = (short)(mFp[ct] * 255.f + 0.5f); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | extern "C" void rsdIntrinsicConvolve3x3_K(void *dst, const void *y0, const void *y1, |
| 69 | const void *y2, const short *coef, uint32_t count); |
| 70 | |
| 71 | |
| 72 | static 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 Murray | 4cca49b | 2012-11-21 14:42:33 -0800 | [diff] [blame] | 77 | uint32_t x2 = rsMin((int32_t)x+1, (int32_t)p->dimX-1); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 78 | |
| 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 | |
| 94 | void 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 Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 99 | if (!cp->mAlloc.get()) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 100 | ALOGE("Convolve3x3 executed without input, skipping"); |
| 101 | return; |
| 102 | } |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 103 | 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 Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 105 | |
| 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 Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 116 | ConvolveOne(p, 0, out, py0, py1, py2, cp->mFp); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 117 | 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 Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 125 | rsdIntrinsicConvolve3x3_K(out, &py0[x1-1], &py1[x1-1], &py2[x1-1], cp->mIp, len); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 126 | x1 += len << 1; |
| 127 | out += len << 1; |
| 128 | } |
| 129 | #endif |
| 130 | |
| 131 | while(x1 != x2) { |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 132 | ConvolveOne(p, x1, out, py0, py1, py2, cp->mFp); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 133 | out++; |
| 134 | x1++; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | RsdCpuScriptIntrinsicConvolve3x3::RsdCpuScriptIntrinsicConvolve3x3( |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 140 | RsdCpuReferenceImpl *ctx, const Script *s, const Element *e) |
| 141 | : RsdCpuScriptIntrinsic(ctx, s, e, RS_SCRIPT_INTRINSIC_ID_CONVOLVE_3x3) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 142 | |
| 143 | mRootPtr = &kernel; |
| 144 | for(int ct=0; ct < 9; ct++) { |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 145 | mFp[ct] = 1.f / 9.f; |
| 146 | mIp[ct] = (short)(mFp[ct] * 255.f + 0.5f); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | RsdCpuScriptIntrinsicConvolve3x3::~RsdCpuScriptIntrinsicConvolve3x3() { |
| 151 | } |
| 152 | |
| 153 | void RsdCpuScriptIntrinsicConvolve3x3::populateScript(Script *s) { |
| 154 | s->mHal.info.exportedVariableCount = 2; |
| 155 | } |
| 156 | |
| 157 | void RsdCpuScriptIntrinsicConvolve3x3::invokeFreeChildren() { |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 158 | mAlloc.clear(); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 162 | RsdCpuScriptImpl * rsdIntrinsic_Convolve3x3(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 163 | |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 164 | return new RsdCpuScriptIntrinsicConvolve3x3(ctx, s, e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | |