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 #include <cstdint>
18 
19 #include "RenderScriptToolkit.h"
20 #include "TaskProcessor.h"
21 #include "Utils.h"
22 
23 #define LOG_TAG "renderscript.toolkit.Convolve3x3"
24 
25 namespace renderscript {
26 
27 extern "C" void rsdIntrinsicConvolve3x3_K(void* dst, const void* y0, const void* y1, const void* y2,
28                                           const int16_t* coef, uint32_t count);
29 
30 class Convolve3x3Task : public Task {
31     const void* mIn;
32     void* mOut;
33     // Even though we have exactly 9 coefficients, store them in an array of size 16 so that
34     // the SIMD instructions can load them in chunks multiple of 8.
35     float mFp[16];
36     int16_t mIp[16];
37 
38     void kernelU4(uchar* out, uint32_t xstart, uint32_t xend, const uchar* py0, const uchar* py1,
39                   const uchar* py2);
40     void convolveU4(const uchar* pin, uchar* pout, size_t vectorSize, size_t sizeX, size_t sizeY,
41                     size_t startX, size_t startY, size_t endX, size_t endY);
42 
43     // Process a 2D tile of the overall work. threadIndex identifies which thread does the work.
44     void processData(int threadIndex, size_t startX, size_t startY, size_t endX,
45                      size_t endY) override;
46 
47    public:
Convolve3x3Task(const void * in,void * out,size_t vectorSize,size_t sizeX,size_t sizeY,const float * coefficients,const Restriction * restriction)48     Convolve3x3Task(const void* in, void* out, size_t vectorSize, size_t sizeX, size_t sizeY,
49                     const float* coefficients, const Restriction* restriction)
50         : Task{sizeX, sizeY, vectorSize, false, restriction}, mIn{in}, mOut{out} {
51         for (int ct = 0; ct < 9; ct++) {
52             mFp[ct] = coefficients[ct];
53             if (mFp[ct] >= 0) {
54                 mIp[ct] = (int16_t)(mFp[ct] * 256.f + 0.5f);
55             } else {
56                 mIp[ct] = (int16_t)(mFp[ct] * 256.f - 0.5f);
57             }
58         }
59     }
60 };
61 
62 /**
63  * Computes one convolution and stores the result in the output. This is used for uchar, uchar2,
64  * uchar3, and uchar4 vectors.
65  *
66  * @tparam InputOutputType Type of the input and output arrays. A vector type, e.g. uchar4.
67  * @tparam ComputationType Type we use for the intermediate computations.
68  * @param x The index in the row of the value we'll convolve.
69  * @param out The location in the output array where we store the value.
70  * @param py0 The start of the top row.
71  * @param py1 The start of the middle row.
72  * @param py2 The start of the bottom row.
73  * @param coeff Pointer to the float coefficients, in row major format.
74  * @param sizeX The number of cells of one row.
75  */
76 template <typename InputOutputType, typename ComputationType>
convolveOneU(uint32_t x,InputOutputType * out,const InputOutputType * py0,const InputOutputType * py1,const InputOutputType * py2,const float * coeff,int32_t sizeX)77 static void convolveOneU(uint32_t x, InputOutputType* out, const InputOutputType* py0,
78                          const InputOutputType* py1, const InputOutputType* py2, const float* coeff,
79                          int32_t sizeX) {
80     uint32_t x1 = std::max((int32_t)x - 1, 0);
81     uint32_t x2 = std::min((int32_t)x + 1, sizeX - 1);
82 
83     ComputationType px = convert<ComputationType>(py0[x1]) * coeff[0] +
84                          convert<ComputationType>(py0[x]) * coeff[1] +
85                          convert<ComputationType>(py0[x2]) * coeff[2] +
86                          convert<ComputationType>(py1[x1]) * coeff[3] +
87                          convert<ComputationType>(py1[x]) * coeff[4] +
88                          convert<ComputationType>(py1[x2]) * coeff[5] +
89                          convert<ComputationType>(py2[x1]) * coeff[6] +
90                          convert<ComputationType>(py2[x]) * coeff[7] +
91                          convert<ComputationType>(py2[x2]) * coeff[8];
92 
93     px = clamp(px + 0.5f, 0.f, 255.f);
94     *out = convert<InputOutputType>(px);
95 }
96 
97 #ifdef ANDROID_RENDERSCRIPT_TOOLKIT_SUPPORTS_FLOAT
98 /**
99  * Computes one convolution and stores the result in the output. This is used for float, float2,
100  * float3, and float4 vectors.
101  *
102  * @tparam InputOutputType Type of the input and output arrays. A vector type, e.g. float4.
103  * @param x The index in the row of the value we'll convolve.
104  * @param out The location in the output array where we store the value.
105  * @param py0 The start of the top row.
106  * @param py1 The start of the middle row.
107  * @param py2 The start of the bottom row.
108  * @param coeff Pointer to the float coefficients, in row major format.
109  * @param sizeX The number of cells of one row.
110  */
111 template <typename InputOutputType>
ConvolveOneF(uint32_t x,InputOutputType * out,const InputOutputType * py0,const InputOutputType * py1,const InputOutputType * py2,const float * coeff,int32_t sizeX)112 static void ConvolveOneF(uint32_t x, InputOutputType* out, const InputOutputType* py0,
113                          const InputOutputType* py1, const InputOutputType* py2, const float* coeff,
114                          int32_t sizeX) {
115     uint32_t x1 = std::max((int32_t)x - 1, 0);
116     uint32_t x2 = std::min((int32_t)x + 1, sizeX - 1);
117     *out = (py0[x1] * coeff[0]) + (py0[x] * coeff[1]) + (py0[x2] * coeff[2]) +
118            (py1[x1] * coeff[3]) + (py1[x] * coeff[4]) + (py1[x2] * coeff[5]) +
119            (py2[x1] * coeff[6]) + (py2[x] * coeff[7]) + (py2[x2] * coeff[8]);
120 }
121 #endif  // ANDROID_RENDERSCRIPT_TOOLKIT_SUPPORTS_FLOAT
122 
123 /**
124  * This function convolves one line.
125  *
126  * @param pout Where to place the next output.
127  * @param xstart Index in the X direction of where to start.
128  * @param xend End index
129  * @param ppy0 Points to the start of the previous line.
130  * @param ppy1 Points to the start of the current line.
131  * @param ppy2 Points to the start of the next line.
132  */
kernelU4(uchar * pout,uint32_t xstart,uint32_t xend,const uchar * ppy0,const uchar * ppy1,const uchar * ppy2)133 void Convolve3x3Task::kernelU4(uchar* pout, uint32_t xstart, uint32_t xend, const uchar* ppy0,
134                                const uchar* ppy1, const uchar* ppy2) {
135     uchar4* out = (uchar4*)pout;
136     const uchar4* py0 = (const uchar4*)ppy0;
137     const uchar4* py1 = (const uchar4*)ppy1;
138     const uchar4* py2 = (const uchar4*)ppy2;
139 
140     uint32_t x1 = xstart;
141     uint32_t x2 = xend;
142     if (x1 == 0) {
143         convolveOneU<uchar4, float4>(0, out, py0, py1, py2, mFp, mSizeX);
144         x1++;
145         out++;
146     }
147 
148     if (x2 > x1) {
149 #if defined(ARCH_ARM_USE_INTRINSICS) || defined(ARCH_X86_HAVE_SSSE3)
150         if (mUsesSimd) {
151             int32_t len = (x2 - x1 - 1) >> 1;
152             if (len > 0) {
153                 rsdIntrinsicConvolve3x3_K(out, &py0[x1 - 1], &py1[x1 - 1], &py2[x1 - 1], mIp, len);
154                 x1 += len << 1;
155                 out += len << 1;
156             }
157         }
158 #endif
159 
160         while (x1 != x2) {
161             convolveOneU<uchar4, float4>(x1, out, py0, py1, py2, mFp, mSizeX);
162             out++;
163             x1++;
164         }
165     }
166 }
167 
168 #ifdef ANDROID_RENDERSCRIPT_TOOLKIT_SUPPORTS_FLOAT
169 template <typename T>
RsdCpuScriptIntrinsicConvolve3x3_kernelF(void * in,T * out,uint32_t xstart,uint32_t xend,uint32_t currentY,size_t sizeX,size_t sizeY,size_t vectorSize,float * fp)170 void RsdCpuScriptIntrinsicConvolve3x3_kernelF(void* in, T* out, uint32_t xstart, uint32_t xend,
171                                               uint32_t currentY, size_t sizeX, size_t sizeY,
172                                               size_t vectorSize, float* fp) {
173     const uchar* pin = (const uchar*)in;
174     const size_t stride = sizeX * vectorSize * 4;  // float takes 4 bytes
175 
176     uint32_t y1 = std::min((int32_t)currentY + 1, (int32_t)(sizeY - 1));
177     uint32_t y2 = std::max((int32_t)currentY - 1, 0);
178     const T* py0 = (const T*)(pin + stride * y2);
179     const T* py1 = (const T*)(pin + stride * currentY);
180     const T* py2 = (const T*)(pin + stride * y1);
181 
182     for (uint32_t x = xstart; x < xend; x++, out++) {
183         ConvolveOneF<T>(x, out, py0, py1, py2, fp, sizeX);
184     }
185 }
186 #endif  // ANDROID_RENDERSCRIPT_TOOLKIT_SUPPORTS_FLOAT
187 
188 template <typename InputOutputType, typename ComputationType>
convolveU(const uchar * pin,uchar * pout,size_t vectorSize,size_t sizeX,size_t sizeY,size_t startX,size_t startY,size_t endX,size_t endY,float * fp)189 static void convolveU(const uchar* pin, uchar* pout, size_t vectorSize, size_t sizeX, size_t sizeY,
190                       size_t startX, size_t startY, size_t endX, size_t endY, float* fp) {
191     const size_t stride = vectorSize * sizeX;
192     for (size_t y = startY; y < endY; y++) {
193         uint32_t y1 = std::min((int32_t)y + 1, (int32_t)(sizeY - 1));
194         uint32_t y2 = std::max((int32_t)y - 1, 0);
195 
196         size_t offset = (y * sizeX + startX) * vectorSize;
197         InputOutputType* px = (InputOutputType*)(pout + offset);
198         InputOutputType* py0 = (InputOutputType*)(pin + stride * y2);
199         InputOutputType* py1 = (InputOutputType*)(pin + stride * y);
200         InputOutputType* py2 = (InputOutputType*)(pin + stride * y1);
201         for (uint32_t x = startX; x < endX; x++, px++) {
202             convolveOneU<InputOutputType, ComputationType>(x, px, py0, py1, py2, fp, sizeX);
203         }
204     }
205 }
206 
convolveU4(const uchar * pin,uchar * pout,size_t vectorSize,size_t sizeX,size_t sizeY,size_t startX,size_t startY,size_t endX,size_t endY)207 void Convolve3x3Task::convolveU4(const uchar* pin, uchar* pout, size_t vectorSize, size_t sizeX,
208                                  size_t sizeY, size_t startX, size_t startY, size_t endX,
209                                  size_t endY) {
210     const size_t stride = paddedSize(vectorSize) * sizeX;
211     for (size_t y = startY; y < endY; y++) {
212         uint32_t y1 = std::min((int32_t)y + 1, (int32_t)(sizeY - 1));
213         uint32_t y2 = std::max((int32_t)y - 1, 0);
214 
215         size_t offset = (y * sizeX + startX) * paddedSize(vectorSize);
216         uchar* px = pout + offset;
217         const uchar* py0 = pin + stride * y2;
218         const uchar* py1 = pin + stride * y;
219         const uchar* py2 = pin + stride * y1;
220         kernelU4(px, startX, endX, py0, py1, py2);
221     }
222 }
223 
processData(int,size_t startX,size_t startY,size_t endX,size_t endY)224 void Convolve3x3Task::processData(int /* threadIndex */, size_t startX, size_t startY, size_t endX,
225                                   size_t endY) {
226     // ALOGI("Thread %d start tile from (%zd, %zd) to (%zd, %zd)", threadIndex, startX, startY,
227     // endX, endY);
228     switch (mVectorSize) {
229         case 1:
230             convolveU<uchar, float>((const uchar*)mIn, (uchar*)mOut, mVectorSize, mSizeX, mSizeY,
231                                     startX, startY, endX, endY, mFp);
232             break;
233         case 2:
234             convolveU<uchar2, float2>((const uchar*)mIn, (uchar*)mOut, mVectorSize, mSizeX, mSizeY,
235                                       startX, startY, endX, endY, mFp);
236             break;
237         case 3:
238         case 4:
239             convolveU4((const uchar*)mIn, (uchar*)mOut, mVectorSize, mSizeX, mSizeY, startX, startY,
240                        endX, endY);
241             break;
242     }
243 }
244 
convolve3x3(const void * in,void * out,size_t vectorSize,size_t sizeX,size_t sizeY,const float * coefficients,const Restriction * restriction)245 void RenderScriptToolkit::convolve3x3(const void* in, void* out, size_t vectorSize, size_t sizeX,
246                                       size_t sizeY, const float* coefficients,
247                                       const Restriction* restriction) {
248 #ifdef ANDROID_RENDERSCRIPT_TOOLKIT_VALIDATE
249     if (!validRestriction(LOG_TAG, sizeX, sizeY, restriction)) {
250         return;
251     }
252     if (vectorSize < 1 || vectorSize > 4) {
253         ALOGE("The vectorSize should be between 1 and 4. %zu provided.", vectorSize);
254         return;
255     }
256 #endif
257 
258     Convolve3x3Task task(in, out, vectorSize, sizeX, sizeY, coefficients, restriction);
259     processor->doTask(&task);
260 }
261 
262 }  // namespace renderscript
263