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 namespace renderscript {
24 
25 #define LOG_TAG "renderscript.toolkit.Lut3d"
26 
27 /**
28  * Converts a RGBA buffer using a 3D cube.
29  */
30 class Lut3dTask : public Task {
31     // The input array we're transforming.
32     const uchar4* mIn;
33     // Where we'll store the transformed result.
34     uchar4* mOut;
35     // The size of each of the three cube dimensions. We don't make use of the last value.
36     int4 mCubeDimension;
37     // The translation cube, in row major format.
38     const uchar* mCubeTable;
39 
40     /**
41      * Converts a subset of a line of the 2D buffer.
42      *
43      * @param in The start of the data to transform.
44      * @param out Where to store the result.
45      * @param length The number of 4-byte vectors to transform.
46      */
47     void kernel(const uchar4* in, uchar4* out, uint32_t length);
48 
49     // Process a 2D tile of the overall work. threadIndex identifies which thread does the work.
50     void processData(int threadIndex, size_t startX, size_t startY, size_t endX,
51                      size_t endY) override;
52 
53    public:
Lut3dTask(const uint8_t * input,uint8_t * output,size_t sizeX,size_t sizeY,const uint8_t * cube,int cubeSizeX,int cubeSizeY,int cubeSizeZ,const Restriction * restriction)54     Lut3dTask(const uint8_t* input, uint8_t* output, size_t sizeX, size_t sizeY,
55               const uint8_t* cube, int cubeSizeX, int cubeSizeY, int cubeSizeZ,
56               const Restriction* restriction)
57         : Task{sizeX, sizeY, 4, true, restriction},
58           mIn{reinterpret_cast<const uchar4*>(input)},
59           mOut{reinterpret_cast<uchar4*>(output)},
60           mCubeDimension{cubeSizeX, cubeSizeY, cubeSizeZ, 0},
61           mCubeTable{cube} {}
62 };
63 
64 extern "C" void rsdIntrinsic3DLUT_K(void* dst, void const* in, size_t count, void const* lut,
65                                     int32_t pitchy, int32_t pitchz, int dimx, int dimy, int dimz);
66 
kernel(const uchar4 * in,uchar4 * out,uint32_t length)67 void Lut3dTask::kernel(const uchar4* in, uchar4* out, uint32_t length) {
68     uint32_t x1 = 0;
69     uint32_t x2 = length;
70 
71     const uchar* bp = mCubeTable;
72 
73     int4 dims = mCubeDimension - 1;
74 
75     const float4 m = (float4)(1.f / 255.f) * convert<float4>(dims);
76     const int4 coordMul = convert<int4>(m * (float4)0x8000);
77     const size_t stride_y = mCubeDimension.x * 4;
78     const size_t stride_z = stride_y * mCubeDimension.y;
79 
80     // ALOGE("strides %zu %zu", stride_y, stride_z);
81 
82 #if defined(ARCH_ARM_USE_INTRINSICS)
83     if (mUsesSimd) {
84         int32_t len = x2 - x1;
85         if (len > 0) {
86             rsdIntrinsic3DLUT_K(out, in, len, bp, stride_y, stride_z, dims.x, dims.y, dims.z);
87             x1 += len;
88             out += len;
89             in += len;
90         }
91     }
92 #endif
93 
94     while (x1 < x2) {
95         int4 baseCoord = convert<int4>(*in) * coordMul;
96         int4 coord1 = baseCoord >> (int4)15;
97         // int4 coord2 = min(coord1 + 1, gDims - 1);
98 
99         int4 weight2 = baseCoord & 0x7fff;
100         int4 weight1 = (int4)0x8000 - weight2;
101 
102         // ALOGE("coord1      %08x %08x %08x %08x", coord1.x, coord1.y, coord1.z, coord1.w);
103         const uchar* bp2 = bp + (coord1.x * 4) + (coord1.y * stride_y) + (coord1.z * stride_z);
104         const uchar4* pt_00 = (const uchar4*)&bp2[0];
105         const uchar4* pt_10 = (const uchar4*)&bp2[stride_y];
106         const uchar4* pt_01 = (const uchar4*)&bp2[stride_z];
107         const uchar4* pt_11 = (const uchar4*)&bp2[stride_y + stride_z];
108 
109         uint4 v000 = convert<uint4>(pt_00[0]);
110         uint4 v100 = convert<uint4>(pt_00[1]);
111         uint4 v010 = convert<uint4>(pt_10[0]);
112         uint4 v110 = convert<uint4>(pt_10[1]);
113         uint4 v001 = convert<uint4>(pt_01[0]);
114         uint4 v101 = convert<uint4>(pt_01[1]);
115         uint4 v011 = convert<uint4>(pt_11[0]);
116         uint4 v111 = convert<uint4>(pt_11[1]);
117 
118         uint4 yz00 = ((v000 * weight1.x) + (v100 * weight2.x)) >> (int4)7;
119         uint4 yz10 = ((v010 * weight1.x) + (v110 * weight2.x)) >> (int4)7;
120         uint4 yz01 = ((v001 * weight1.x) + (v101 * weight2.x)) >> (int4)7;
121         uint4 yz11 = ((v011 * weight1.x) + (v111 * weight2.x)) >> (int4)7;
122 
123         uint4 z0 = ((yz00 * weight1.y) + (yz10 * weight2.y)) >> (int4)15;
124         uint4 z1 = ((yz01 * weight1.y) + (yz11 * weight2.y)) >> (int4)15;
125 
126         uint4 v = ((z0 * weight1.z) + (z1 * weight2.z)) >> (int4)15;
127         uint4 v2 = (v + 0x7f) >> (int4)8;
128 
129         uchar4 ret = convert<uchar4>(v2);
130         ret.w = in->w;
131 
132 #if 0
133         if (!x1) {
134             ALOGE("in          %08x %08x %08x %08x", in->r, in->g, in->b, in->a);
135             ALOGE("baseCoord   %08x %08x %08x %08x", baseCoord.x, baseCoord.y, baseCoord.z,
136                   baseCoord.w);
137             ALOGE("coord1      %08x %08x %08x %08x", coord1.x, coord1.y, coord1.z, coord1.w);
138             ALOGE("weight1     %08x %08x %08x %08x", weight1.x, weight1.y, weight1.z, weight1.w);
139             ALOGE("weight2     %08x %08x %08x %08x", weight2.x, weight2.y, weight2.z, weight2.w);
140 
141             ALOGE("v000        %08x %08x %08x %08x", v000.x, v000.y, v000.z, v000.w);
142             ALOGE("v100        %08x %08x %08x %08x", v100.x, v100.y, v100.z, v100.w);
143             ALOGE("yz00        %08x %08x %08x %08x", yz00.x, yz00.y, yz00.z, yz00.w);
144             ALOGE("z0          %08x %08x %08x %08x", z0.x, z0.y, z0.z, z0.w);
145 
146             ALOGE("v           %08x %08x %08x %08x", v.x, v.y, v.z, v.w);
147             ALOGE("v2          %08x %08x %08x %08x", v2.x, v2.y, v2.z, v2.w);
148         }
149 #endif
150         *out = ret;
151 
152         in++;
153         out++;
154         x1++;
155     }
156 }
157 
processData(int,size_t startX,size_t startY,size_t endX,size_t endY)158 void Lut3dTask::processData(int /* threadIndex */, size_t startX, size_t startY, size_t endX,
159                             size_t endY) {
160     for (size_t y = startY; y < endY; y++) {
161         size_t offset = mSizeX * y + startX;
162         kernel(mIn + offset, mOut + offset, endX - startX);
163     }
164 }
165 
lut3d(const uint8_t * input,uint8_t * output,size_t sizeX,size_t sizeY,const uint8_t * cube,size_t cubeSizeX,size_t cubeSizeY,size_t cubeSizeZ,const Restriction * restriction)166 void RenderScriptToolkit::lut3d(const uint8_t* input, uint8_t* output, size_t sizeX, size_t sizeY,
167                                 const uint8_t* cube, size_t cubeSizeX, size_t cubeSizeY,
168                                 size_t cubeSizeZ, const Restriction* restriction) {
169 #ifdef ANDROID_RENDERSCRIPT_TOOLKIT_VALIDATE
170     if (!validRestriction(LOG_TAG, sizeX, sizeY, restriction)) {
171         return;
172     }
173 #endif
174 
175     Lut3dTask task(input, output, sizeX, sizeY, cube, cubeSizeX, cubeSizeY, cubeSizeZ, restriction);
176     processor->doTask(&task);
177 }
178 
179 }  // namespace renderscript
180