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