Home | History | Annotate | Download | only in driver
      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 
     24 using namespace android;
     25 using namespace android::renderscript;
     26 
     27 struct ConvolveParams {
     28     float fp[28];
     29     short ip[28];
     30     ObjectBaseRef<Allocation> alloc;
     31 };
     32 
     33 static 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 
     40 static 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 
     52 static 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 
     97 extern "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 
    101 static 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     DrvAllocation *din = (DrvAllocation *)cp->alloc->mHal.drv;
    106     const uchar *pin = (const uchar *)din->lod[0].mallocPtr;
    107 
    108     uint32_t y0 = rsMax((int32_t)p->y-2, 0);
    109     uint32_t y1 = rsMax((int32_t)p->y-1, 0);
    110     uint32_t y2 = p->y;
    111     uint32_t y3 = rsMin((int32_t)p->y+1, (int32_t)(p->dimY-1));
    112     uint32_t y4 = rsMin((int32_t)p->y+2, (int32_t)(p->dimY-1));
    113 
    114     const uchar4 *py0 = (const uchar4 *)(pin + din->lod[0].stride * y0);
    115     const uchar4 *py1 = (const uchar4 *)(pin + din->lod[0].stride * y1);
    116     const uchar4 *py2 = (const uchar4 *)(pin + din->lod[0].stride * y2);
    117     const uchar4 *py3 = (const uchar4 *)(pin + din->lod[0].stride * y3);
    118     const uchar4 *py4 = (const uchar4 *)(pin + din->lod[0].stride * y4);
    119 
    120     uchar4 *out = (uchar4 *)p->out;
    121     uint32_t x1 = xstart;
    122     uint32_t x2 = xend;
    123 
    124     while((x1 < x2) && (x1 < 2)) {
    125         One(p, x1, out, py0, py1, py2, py3, py4, cp->fp);
    126         out++;
    127         x1++;
    128     }
    129 
    130 #if defined(ARCH_ARM_HAVE_NEON)
    131     if((x1 + 3) < x2) {
    132         uint32_t len = (x2 - x1 - 3) >> 1;
    133         rsdIntrinsicConvolve5x5_K(out, py0, py1, py2, py3, py4, cp->ip, len);
    134         out += len << 1;
    135         x1 += len << 1;
    136     }
    137 #endif
    138 
    139     while(x1 < x2) {
    140         One(p, x1, out, py0, py1, py2, py3, py4, cp->fp);
    141         out++;
    142         x1++;
    143     }
    144 }
    145 
    146 void * rsdIntrinsic_InitConvolve5x5(const android::renderscript::Context *dc,
    147                                     android::renderscript::Script *script,
    148                                     RsdIntriniscFuncs_t *funcs) {
    149 
    150     script->mHal.info.exportedVariableCount = 2;
    151     funcs->bind = Convolve5x5_Bind;
    152     funcs->setVar = Convolve5x5_SetVar;
    153     funcs->root = Convolve5x5_uchar4;
    154 
    155     ConvolveParams *cp = (ConvolveParams *)calloc(1, sizeof(ConvolveParams));
    156     for(int ct=0; ct < 25; ct++) {
    157         cp->fp[ct] = 1.f / 25.f;
    158         cp->ip[ct] = (short)(cp->fp[ct] * 255.f + 0.5f);
    159     }
    160     return cp;
    161 }
    162 
    163 
    164