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 "rsdCore.h" 19#include "rsdIntrinsics.h" 20#include "rsdAllocation.h" 21 22#include "rsdIntrinsicInlines.h" 23 24using namespace android; 25using namespace android::renderscript; 26 27struct ConvolveParams { 28 float fp[28]; 29 short ip[28]; 30 ObjectBaseRef<Allocation> alloc; 31}; 32 33static void Convolve5x5_Bind(const Context *dc, const Script *script, 34 void * intrinsicData, uint32_t slot, Allocation *data) { 35 ConvolveParams *cp = (ConvolveParams *)intrinsicData; 36 rsAssert(slot == 1); 37 cp->alloc.set(data); 38} 39 40static void Convolve5x5_SetVar(const Context *dc, const Script *script, void * intrinsicData, 41 uint32_t slot, void *data, size_t dataLength) { 42 ConvolveParams *cp = (ConvolveParams *)intrinsicData; 43 44 rsAssert(slot == 0); 45 memcpy (cp->fp, data, dataLength); 46 for(int ct=0; ct < 25; ct++) { 47 cp->ip[ct] = (short)(cp->fp[ct] * 255.f + 0.5f); 48 } 49} 50 51 52static void One(const RsForEachStubParamStruct *p, uint32_t x, uchar4 *out, 53 const uchar4 *py0, const uchar4 *py1, const uchar4 *py2, const uchar4 *py3, const uchar4 *py4, 54 const float* coeff) { 55 56 uint32_t x0 = rsMax((int32_t)x-2, 0); 57 uint32_t x1 = rsMax((int32_t)x-1, 0); 58 uint32_t x2 = x; 59 uint32_t x3 = rsMin((int32_t)x+1, (int32_t)(p->dimX-1)); 60 uint32_t x4 = rsMin((int32_t)x+2, (int32_t)(p->dimX-1)); 61 62 float4 px = convert_float4(py0[x0]) * coeff[0] + 63 convert_float4(py0[x1]) * coeff[1] + 64 convert_float4(py0[x2]) * coeff[2] + 65 convert_float4(py0[x3]) * coeff[3] + 66 convert_float4(py0[x4]) * coeff[4] + 67 68 convert_float4(py1[x0]) * coeff[5] + 69 convert_float4(py1[x1]) * coeff[6] + 70 convert_float4(py1[x2]) * coeff[7] + 71 convert_float4(py1[x3]) * coeff[8] + 72 convert_float4(py1[x4]) * coeff[9] + 73 74 convert_float4(py2[x0]) * coeff[10] + 75 convert_float4(py2[x1]) * coeff[11] + 76 convert_float4(py2[x2]) * coeff[12] + 77 convert_float4(py2[x3]) * coeff[13] + 78 convert_float4(py2[x4]) * coeff[14] + 79 80 convert_float4(py3[x0]) * coeff[15] + 81 convert_float4(py3[x1]) * coeff[16] + 82 convert_float4(py3[x2]) * coeff[17] + 83 convert_float4(py3[x3]) * coeff[18] + 84 convert_float4(py3[x4]) * coeff[19] + 85 86 convert_float4(py4[x0]) * coeff[20] + 87 convert_float4(py4[x1]) * coeff[21] + 88 convert_float4(py4[x2]) * coeff[22] + 89 convert_float4(py4[x3]) * coeff[23] + 90 convert_float4(py4[x4]) * coeff[24]; 91 92 px = clamp(px, 0.f, 255.f); 93 uchar4 o = {(uchar)px.x, (uchar)px.y, (uchar)px.z, (uchar)px.w}; 94 *out = o; 95} 96 97extern "C" void rsdIntrinsicConvolve5x5_K(void *dst, const void *y0, const void *y1, 98 const void *y2, const void *y3, const void *y4, 99 const short *coef, uint32_t count); 100 101static void Convolve5x5_uchar4(const RsForEachStubParamStruct *p, 102 uint32_t xstart, uint32_t xend, 103 uint32_t instep, uint32_t outstep) { 104 ConvolveParams *cp = (ConvolveParams *)p->usr; 105 if (!cp->alloc.get()) { 106 ALOGE("Convolve5x5 executed without input, skipping"); 107 return; 108 } 109 DrvAllocation *din = (DrvAllocation *)cp->alloc->mHal.drv; 110 const uchar *pin = (const uchar *)din->lod[0].mallocPtr; 111 112 uint32_t y0 = rsMax((int32_t)p->y-2, 0); 113 uint32_t y1 = rsMax((int32_t)p->y-1, 0); 114 uint32_t y2 = p->y; 115 uint32_t y3 = rsMin((int32_t)p->y+1, (int32_t)(p->dimY-1)); 116 uint32_t y4 = rsMin((int32_t)p->y+2, (int32_t)(p->dimY-1)); 117 118 const uchar4 *py0 = (const uchar4 *)(pin + din->lod[0].stride * y0); 119 const uchar4 *py1 = (const uchar4 *)(pin + din->lod[0].stride * y1); 120 const uchar4 *py2 = (const uchar4 *)(pin + din->lod[0].stride * y2); 121 const uchar4 *py3 = (const uchar4 *)(pin + din->lod[0].stride * y3); 122 const uchar4 *py4 = (const uchar4 *)(pin + din->lod[0].stride * y4); 123 124 uchar4 *out = (uchar4 *)p->out; 125 uint32_t x1 = xstart; 126 uint32_t x2 = xend; 127 128 while((x1 < x2) && (x1 < 2)) { 129 One(p, x1, out, py0, py1, py2, py3, py4, cp->fp); 130 out++; 131 x1++; 132 } 133 134#if defined(ARCH_ARM_HAVE_NEON) 135 if((x1 + 3) < x2) { 136 uint32_t len = (x2 - x1 - 3) >> 1; 137 rsdIntrinsicConvolve5x5_K(out, py0+x1-2, py1+x1-2, py2+x1-2, py3+x1-2, py4+x1-2, cp->ip, len); 138 out += len << 1; 139 x1 += len << 1; 140 } 141#endif 142 143 while(x1 < x2) { 144 One(p, x1, out, py0, py1, py2, py3, py4, cp->fp); 145 out++; 146 x1++; 147 } 148} 149 150void * rsdIntrinsic_InitConvolve5x5(const android::renderscript::Context *dc, 151 android::renderscript::Script *script, 152 RsdIntriniscFuncs_t *funcs) { 153 154 script->mHal.info.exportedVariableCount = 2; 155 funcs->setVarObj = Convolve5x5_Bind; 156 funcs->setVar = Convolve5x5_SetVar; 157 funcs->root = Convolve5x5_uchar4; 158 159 ConvolveParams *cp = (ConvolveParams *)calloc(1, sizeof(ConvolveParams)); 160 for(int ct=0; ct < 25; ct++) { 161 cp->fp[ct] = 1.f / 25.f; 162 cp->ip[ct] = (short)(cp->fp[ct] * 255.f + 0.5f); 163 } 164 return cp; 165} 166 167 168