• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 RsdCpuScriptIntrinsicConvolve5x5 : 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 ~RsdCpuScriptIntrinsicConvolve5x5();
37     RsdCpuScriptIntrinsicConvolve5x5(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e);
38 
39 protected:
40     float fp[28];
41     short ip[28];
42     ObjectBaseRef<Allocation> alloc;
43 
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 
setGlobalObj(uint32_t slot,ObjectBase * data)55 void RsdCpuScriptIntrinsicConvolve5x5::setGlobalObj(uint32_t slot, ObjectBase *data) {
56     rsAssert(slot == 1);
57     alloc.set(static_cast<Allocation *>(data));
58 }
59 
setGlobalVar(uint32_t slot,const void * data,size_t dataLength)60 void RsdCpuScriptIntrinsicConvolve5x5::setGlobalVar(uint32_t slot,
61                                                     const void *data, size_t dataLength) {
62     rsAssert(slot == 0);
63     memcpy (&fp, data, dataLength);
64     for(int ct=0; ct < 25; ct++) {
65         ip[ct] = (short)(fp[ct] * 255.f);
66     }
67 }
68 
69 
One(const RsForEachStubParamStruct * p,uint32_t x,uchar4 * out,const uchar4 * py0,const uchar4 * py1,const uchar4 * py2,const uchar4 * py3,const uchar4 * py4,const float * coeff)70 static void One(const RsForEachStubParamStruct *p, uint32_t x, uchar4 *out,
71                 const uchar4 *py0, const uchar4 *py1, const uchar4 *py2, const uchar4 *py3, const uchar4 *py4,
72                 const float* coeff) {
73 
74     uint32_t x0 = rsMax((int32_t)x-2, 0);
75     uint32_t x1 = rsMax((int32_t)x-1, 0);
76     uint32_t x2 = x;
77     uint32_t x3 = rsMin((int32_t)x+1, (int32_t)(p->dimX-1));
78     uint32_t x4 = rsMin((int32_t)x+2, (int32_t)(p->dimX-1));
79 
80     float4 px = convert_float4(py0[x0]) * coeff[0] +
81                 convert_float4(py0[x1]) * coeff[1] +
82                 convert_float4(py0[x2]) * coeff[2] +
83                 convert_float4(py0[x3]) * coeff[3] +
84                 convert_float4(py0[x4]) * coeff[4] +
85 
86                 convert_float4(py1[x0]) * coeff[5] +
87                 convert_float4(py1[x1]) * coeff[6] +
88                 convert_float4(py1[x2]) * coeff[7] +
89                 convert_float4(py1[x3]) * coeff[8] +
90                 convert_float4(py1[x4]) * coeff[9] +
91 
92                 convert_float4(py2[x0]) * coeff[10] +
93                 convert_float4(py2[x1]) * coeff[11] +
94                 convert_float4(py2[x2]) * coeff[12] +
95                 convert_float4(py2[x3]) * coeff[13] +
96                 convert_float4(py2[x4]) * coeff[14] +
97 
98                 convert_float4(py3[x0]) * coeff[15] +
99                 convert_float4(py3[x1]) * coeff[16] +
100                 convert_float4(py3[x2]) * coeff[17] +
101                 convert_float4(py3[x3]) * coeff[18] +
102                 convert_float4(py3[x4]) * coeff[19] +
103 
104                 convert_float4(py4[x0]) * coeff[20] +
105                 convert_float4(py4[x1]) * coeff[21] +
106                 convert_float4(py4[x2]) * coeff[22] +
107                 convert_float4(py4[x3]) * coeff[23] +
108                 convert_float4(py4[x4]) * coeff[24];
109 
110     px = clamp(px, 0.f, 255.f);
111     uchar4 o = {(uchar)px.x, (uchar)px.y, (uchar)px.z, (uchar)px.w};
112     //if ((out[0].r != o.r) || (out[0].y != o.y) || (out[0].z != o.z) || (out[0].w != o.w)) {
113         //ALOGE("x %i  %i,%i,%i,%i  %i,%i,%i,%i", x, o.x, o.y, o.z, o.w, out[0].x, out[0].y, out[0].z, out[0].w);
114     //}
115     //o.w = 0xff;
116     out->xyzw = o.xyzw;
117 }
118 
119 extern "C" void rsdIntrinsicConvolve5x5_K(void *dst, const void *y0, const void *y1,
120                                           const void *y2, const void *y3, const void *y4,
121                                           const short *coef, uint32_t count);
122 
kernel(const RsForEachStubParamStruct * p,uint32_t xstart,uint32_t xend,uint32_t instep,uint32_t outstep)123 void RsdCpuScriptIntrinsicConvolve5x5::kernel(const RsForEachStubParamStruct *p,
124                                               uint32_t xstart, uint32_t xend,
125                                               uint32_t instep, uint32_t outstep) {
126     RsdCpuScriptIntrinsicConvolve5x5 *cp = (RsdCpuScriptIntrinsicConvolve5x5 *)p->usr;
127     if (!cp->alloc.get()) {
128         ALOGE("Convolve5x5 executed without input, skipping");
129         return;
130     }
131     const uchar *pin = (const uchar *)cp->alloc->mHal.drvState.lod[0].mallocPtr;
132     const size_t stride = cp->alloc->mHal.drvState.lod[0].stride;
133 
134     uint32_t y0 = rsMax((int32_t)p->y-2, 0);
135     uint32_t y1 = rsMax((int32_t)p->y-1, 0);
136     uint32_t y2 = p->y;
137     uint32_t y3 = rsMin((int32_t)p->y+1, (int32_t)(p->dimY-1));
138     uint32_t y4 = rsMin((int32_t)p->y+2, (int32_t)(p->dimY-1));
139 
140     const uchar4 *py0 = (const uchar4 *)(pin + stride * y0);
141     const uchar4 *py1 = (const uchar4 *)(pin + stride * y1);
142     const uchar4 *py2 = (const uchar4 *)(pin + stride * y2);
143     const uchar4 *py3 = (const uchar4 *)(pin + stride * y3);
144     const uchar4 *py4 = (const uchar4 *)(pin + stride * y4);
145 
146     uchar4 *out = (uchar4 *)p->out;
147     uint32_t x1 = xstart;
148     uint32_t x2 = xend;
149 
150     while((x1 < x2) && (x1 < 2)) {
151         One(p, x1, out, py0, py1, py2, py3, py4, cp->fp);
152         out++;
153         x1++;
154     }
155 
156 #if defined(ARCH_ARM_HAVE_NEON)
157     if((x1 + 3) < x2) {
158         uint32_t len = (x2 - x1 - 3) >> 1;
159         rsdIntrinsicConvolve5x5_K(out, py0, py1, py2, py3, py4, cp->ip, len);
160         out += len << 1;
161         x1 += len << 1;
162     }
163 #endif
164 
165     while(x1 < x2) {
166         One(p, x1, out, py0, py1, py2, py3, py4, cp->fp);
167         out++;
168         x1++;
169     }
170 }
171 
172 
RsdCpuScriptIntrinsicConvolve5x5(RsdCpuReferenceImpl * ctx,const Script * s,const Element * e)173 RsdCpuScriptIntrinsicConvolve5x5::RsdCpuScriptIntrinsicConvolve5x5(
174             RsdCpuReferenceImpl *ctx, const Script *s, const Element *e)
175             : RsdCpuScriptIntrinsic(ctx, s, e, RS_SCRIPT_INTRINSIC_ID_CONVOLVE_5x5) {
176 
177     mRootPtr = &kernel;
178     for(int ct=0; ct < 25; ct++) {
179         fp[ct] = 1.f / 25.f;
180         ip[ct] = (short)(fp[ct] * 255.f);
181     }
182 }
183 
~RsdCpuScriptIntrinsicConvolve5x5()184 RsdCpuScriptIntrinsicConvolve5x5::~RsdCpuScriptIntrinsicConvolve5x5() {
185 }
186 
populateScript(Script * s)187 void RsdCpuScriptIntrinsicConvolve5x5::populateScript(Script *s) {
188     s->mHal.info.exportedVariableCount = 2;
189 }
190 
invokeFreeChildren()191 void RsdCpuScriptIntrinsicConvolve5x5::invokeFreeChildren() {
192     alloc.clear();
193 }
194 
195 
rsdIntrinsic_Convolve5x5(RsdCpuReferenceImpl * ctx,const Script * s,const Element * e)196 RsdCpuScriptImpl * rsdIntrinsic_Convolve5x5(RsdCpuReferenceImpl *ctx,
197                                             const Script *s, const Element *e) {
198 
199     return new RsdCpuScriptIntrinsicConvolve5x5(ctx, s, e);
200 }
201 
202 
203 
204