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[16]; 29 short ip[16]; 30 ObjectBaseRef<Allocation> alloc; 31}; 32 33static void Convolve3x3_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 Convolve3x3_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 < 9; ct++) { 47 cp->ip[ct] = (short)(cp->fp[ct] * 255.f + 0.5f); 48 } 49} 50 51extern "C" void rsdIntrinsicConvolve3x3_K(void *dst, const void *y0, const void *y1, const void *y2, const short *coef, uint32_t count); 52 53 54static void ConvolveOne(const RsForEachStubParamStruct *p, uint32_t x, uchar4 *out, 55 const uchar4 *py0, const uchar4 *py1, const uchar4 *py2, 56 const float* coeff) { 57 58 uint32_t x1 = rsMax((int32_t)x-1, 0); 59 uint32_t x2 = rsMin((int32_t)x+1, (int32_t)p->dimX); 60 61 float4 px = convert_float4(py0[x1]) * coeff[0] + 62 convert_float4(py0[x]) * coeff[1] + 63 convert_float4(py0[x2]) * coeff[2] + 64 convert_float4(py1[x1]) * coeff[3] + 65 convert_float4(py1[x]) * coeff[4] + 66 convert_float4(py1[x2]) * coeff[5] + 67 convert_float4(py2[x1]) * coeff[6] + 68 convert_float4(py2[x]) * coeff[7] + 69 convert_float4(py2[x2]) * coeff[8]; 70 71 px = clamp(px, 0.f, 255.f); 72 uchar4 o = {(uchar)px.x, (uchar)px.y, (uchar)px.z, (uchar)px.w}; 73 *out = o; 74} 75 76static void Convolve3x3_uchar4(const RsForEachStubParamStruct *p, 77 uint32_t xstart, uint32_t xend, 78 uint32_t instep, uint32_t outstep) { 79 ConvolveParams *cp = (ConvolveParams *)p->usr; 80 DrvAllocation *din = (DrvAllocation *)cp->alloc->mHal.drv; 81 const uchar *pin = (const uchar *)din->lod[0].mallocPtr; 82 83 uint32_t y1 = rsMin((int32_t)p->y + 1, (int32_t)(p->dimY-1)); 84 uint32_t y2 = rsMax((int32_t)p->y - 1, 0); 85 const uchar4 *py0 = (const uchar4 *)(pin + din->lod[0].stride * y2); 86 const uchar4 *py1 = (const uchar4 *)(pin + din->lod[0].stride * p->y); 87 const uchar4 *py2 = (const uchar4 *)(pin + din->lod[0].stride * y1); 88 89 uchar4 *out = (uchar4 *)p->out; 90 uint32_t x1 = xstart; 91 uint32_t x2 = xend; 92 if(x1 == 0) { 93 ConvolveOne(p, 0, out, py0, py1, py2, cp->fp); 94 x1 ++; 95 out++; 96 } 97 98 if(x2 > x1) { 99#if defined(ARCH_ARM_HAVE_NEON) 100 int32_t len = (x2 - x1 - 1) >> 1; 101 if(len > 0) { 102 rsdIntrinsicConvolve3x3_K(out, &py0[x1-1], &py1[x1-1], &py2[x1-1], cp->ip, len); 103 x1 += len << 1; 104 out += len << 1; 105 } 106#endif 107 108 while(x1 != x2) { 109 ConvolveOne(p, x1, out, py0, py1, py2, cp->fp); 110 out++; 111 x1++; 112 } 113 } 114} 115 116void * rsdIntrinsic_InitConvolve3x3(const android::renderscript::Context *dc, 117 android::renderscript::Script *script, 118 RsdIntriniscFuncs_t *funcs) { 119 120 script->mHal.info.exportedVariableCount = 2; 121 funcs->bind = Convolve3x3_Bind; 122 funcs->setVar = Convolve3x3_SetVar; 123 funcs->root = Convolve3x3_uchar4; 124 125 ConvolveParams *cp = (ConvolveParams *)calloc(1, sizeof(ConvolveParams)); 126 for(int ct=0; ct < 9; ct++) { 127 cp->fp[ct] = 1.f / 9.f; 128 cp->ip[ct] = (short)(cp->fp[ct] * 255.f + 0.5f); 129 } 130 return cp; 131} 132 133 134