Skip to content

Commit 9196c9a

Browse files
stephenhinesAndroid (Google) Code Review
authored andcommitted
Merge "Fisheye filter: approx version, general performance improvements" into jb-mr1-dev
2 parents a4a5a85 + 50b1ba0 commit 9196c9a

File tree

6 files changed

+186
-50
lines changed

6 files changed

+186
-50
lines changed

tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/Fisheye.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@
2626
public class Fisheye extends TestBase {
2727
private ScriptC_fisheye_full mScript_full = null;
2828
private ScriptC_fisheye_relaxed mScript_relaxed = null;
29+
private ScriptC_fisheye_approx_full mScript_approx_full = null;
30+
private ScriptC_fisheye_approx_relaxed mScript_approx_relaxed = null;
31+
private final boolean approx;
2932
private final boolean relaxed;
3033
private float center_x = 0.5f;
3134
private float center_y = 0.5f;
3235
private float scale = 0.5f;
3336

34-
public Fisheye(boolean relaxed) {
37+
public Fisheye(boolean approx, boolean relaxed) {
38+
this.approx = approx;
3539
this.relaxed = relaxed;
3640
}
3741

@@ -68,7 +72,18 @@ public void onBar3Changed(int progress) {
6872
}
6973

7074
private void do_init() {
71-
if (relaxed)
75+
if (approx) {
76+
if (relaxed)
77+
mScript_approx_relaxed.invoke_init_filter(
78+
mInPixelsAllocation.getType().getX(),
79+
mInPixelsAllocation.getType().getY(), center_x,
80+
center_y, scale);
81+
else
82+
mScript_approx_full.invoke_init_filter(
83+
mInPixelsAllocation.getType().getX(),
84+
mInPixelsAllocation.getType().getY(), center_x,
85+
center_y, scale);
86+
} else if (relaxed)
7287
mScript_relaxed.invoke_init_filter(
7388
mInPixelsAllocation.getType().getX(),
7489
mInPixelsAllocation.getType().getY(), center_x, center_y,
@@ -81,7 +96,19 @@ private void do_init() {
8196
}
8297

8398
public void createTest(android.content.res.Resources res) {
84-
if (relaxed) {
99+
if (approx) {
100+
if (relaxed) {
101+
mScript_approx_relaxed = new ScriptC_fisheye_approx_relaxed(mRS,
102+
res, R.raw.fisheye_approx_relaxed);
103+
mScript_approx_relaxed.set_in_alloc(mInPixelsAllocation);
104+
mScript_approx_relaxed.set_sampler(Sampler.CLAMP_LINEAR(mRS));
105+
} else {
106+
mScript_approx_full = new ScriptC_fisheye_approx_full(mRS, res,
107+
R.raw.fisheye_approx_full);
108+
mScript_approx_full.set_in_alloc(mInPixelsAllocation);
109+
mScript_approx_full.set_sampler(Sampler.CLAMP_LINEAR(mRS));
110+
}
111+
} else if (relaxed) {
85112
mScript_relaxed = new ScriptC_fisheye_relaxed(mRS, res,
86113
R.raw.fisheye_relaxed);
87114
mScript_relaxed.set_in_alloc(mInPixelsAllocation);
@@ -96,7 +123,12 @@ public void createTest(android.content.res.Resources res) {
96123
}
97124

98125
public void runTest() {
99-
if (relaxed)
126+
if (approx) {
127+
if (relaxed)
128+
mScript_approx_relaxed.forEach_root(mOutPixelsAllocation);
129+
else
130+
mScript_approx_full.forEach_root(mOutPixelsAllocation);
131+
} else if (relaxed)
100132
mScript_relaxed.forEach_root(mOutPixelsAllocation);
101133
else
102134
mScript_full.forEach_root(mOutPixelsAllocation);

tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -144,30 +144,36 @@ void changeTest(int testID) {
144144
mTest = new Grain();
145145
break;
146146
case 7:
147-
mTest = new Fisheye(false);
147+
mTest = new Fisheye(false, false);
148148
break;
149149
case 8:
150-
mTest = new Fisheye(true);
150+
mTest = new Fisheye(false, true);
151151
break;
152152
case 9:
153-
mTest = new Vignette(false, false);
153+
mTest = new Fisheye(true, false);
154154
break;
155155
case 10:
156-
mTest = new Vignette(false, true);
156+
mTest = new Fisheye(true, true);
157157
break;
158158
case 11:
159-
mTest = new Vignette(true, false);
159+
mTest = new Vignette(false, false);
160160
break;
161161
case 12:
162-
mTest = new Vignette(true, true);
162+
mTest = new Vignette(false, true);
163163
break;
164164
case 13:
165-
mTest = new GroupTest(true);
165+
mTest = new Vignette(true, false);
166166
break;
167167
case 14:
168-
mTest = new GroupTest(false);
168+
mTest = new Vignette(true, true);
169169
break;
170170
case 15:
171+
mTest = new GroupTest(true);
172+
break;
173+
case 16:
174+
mTest = new GroupTest(false);
175+
break;
176+
case 17:
171177
mTest = new Intrinsics(0);
172178
break;
173179
}
@@ -182,7 +188,7 @@ void changeTest(int testID) {
182188
}
183189

184190
void setupTests() {
185-
mTestNames = new String[16];
191+
mTestNames = new String[18];
186192
mTestNames[0] = "Levels Vec3 Relaxed";
187193
mTestNames[1] = "Levels Vec4 Relaxed";
188194
mTestNames[2] = "Levels Vec3 Full";
@@ -192,13 +198,15 @@ void setupTests() {
192198
mTestNames[6] = "Grain";
193199
mTestNames[7] = "Fisheye Full";
194200
mTestNames[8] = "Fisheye Relaxed";
195-
mTestNames[9] = "Vignette Full";
196-
mTestNames[10] = "Vignette Relaxed";
197-
mTestNames[11] = "Vignette Approximate Full";
198-
mTestNames[12] = "Vignette Approximate Relaxed";
199-
mTestNames[13] = "Group Test (emulated)";
200-
mTestNames[14] = "Group Test (native)";
201-
mTestNames[15] = "Intrinsics Convolve 3x3";
201+
mTestNames[9] = "Fisheye Approximate Full";
202+
mTestNames[10] = "Fisheye Approximate Relaxed";
203+
mTestNames[11] = "Vignette Full";
204+
mTestNames[12] = "Vignette Relaxed";
205+
mTestNames[13] = "Vignette Approximate Full";
206+
mTestNames[14] = "Vignette Approximate Relaxed";
207+
mTestNames[15] = "Group Test (emulated)";
208+
mTestNames[16] = "Group Test (native)";
209+
mTestNames[17] = "Intrinsics Convolve 3x3";
202210
mTestSpinner.setAdapter(new ArrayAdapter<String>(
203211
this, R.layout.spinner_layout, mTestNames));
204212
}

tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/fisheye.rsh

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,46 +17,41 @@
1717
rs_allocation in_alloc;
1818
rs_sampler sampler;
1919

20-
static float2 center, dimensions;
21-
static float2 scale;
22-
static float alpha;
23-
static float radius2;
24-
static float factor;
25-
26-
void init_filter(uint32_t dim_x, uint32_t dim_y, float focus_x, float focus_y, float k) {
27-
center.x = focus_x;
28-
center.y = focus_y;
29-
dimensions.x = (float)dim_x;
30-
dimensions.y = (float)dim_y;
20+
static float2 center, neg_center, inv_dimensions, axis_scale;
21+
static float alpha, radius2, factor;
3122

23+
void init_filter(uint32_t dim_x, uint32_t dim_y, float center_x, float center_y, float k) {
24+
center.x = center_x;
25+
center.y = center_y;
26+
neg_center = -center;
27+
inv_dimensions.x = 1.f / (float)dim_x;
28+
inv_dimensions.y = 1.f / (float)dim_y;
3229
alpha = k * 2.0 + 0.75;
33-
float bound2 = 0.25;
34-
if (dim_x > dim_y) {
35-
scale.x = 1.0;
36-
scale.y = dimensions.y / dimensions.x;
37-
bound2 *= (scale.y*scale.y + 1);
38-
} else {
39-
scale.x = dimensions.x / dimensions.y;
40-
scale.y = 1.0;
41-
bound2 *= (scale.x*scale.x + 1);
42-
}
30+
31+
axis_scale = (float2)1.f;
32+
if (dim_x > dim_y)
33+
axis_scale.y = (float)dim_y / (float)dim_x;
34+
else
35+
axis_scale.x = (float)dim_x / (float)dim_y;
36+
37+
const float bound2 = 0.25 * (axis_scale.x*axis_scale.x + axis_scale.y*axis_scale.y);
4338
const float bound = sqrt(bound2);
4439
const float radius = 1.15 * bound;
4540
radius2 = radius*radius;
46-
const float max_radian = 0.5f * M_PI - atan(alpha / bound * sqrt(radius2 - bound2));
41+
const float max_radian = M_PI_2 - atan(alpha / bound * sqrt(radius2 - bound2));
4742
factor = bound / max_radian;
4843
}
4944

5045
void root(uchar4 *out, uint32_t x, uint32_t y) {
5146
// Convert x and y to floating point coordinates with center as origin
52-
float2 coord;
53-
coord.x = (float)x / dimensions.x;
54-
coord.y = (float)y / dimensions.y;
55-
coord -= center;
56-
const float dist = length(scale * coord);
57-
const float radian = M_PI_2 - atan((alpha * sqrt(radius2 - dist * dist)) / dist);
58-
const float scalar = radian * factor / dist;
59-
const float2 new_coord = coord * scalar + center;
47+
const float2 inCoord = {(float)x, (float)y};
48+
const float2 coord = mad(inCoord, inv_dimensions, neg_center);
49+
const float2 scaledCoord = axis_scale * coord;
50+
const float dist2 = scaledCoord.x*scaledCoord.x + scaledCoord.y*scaledCoord.y;
51+
const float inv_dist = rsqrt(dist2);
52+
const float radian = M_PI_2 - atan((alpha * sqrt(radius2 - dist2)) * inv_dist);
53+
const float scalar = radian * factor * inv_dist;
54+
const float2 new_coord = mad(coord, scalar, center);
6055
const float4 fout = rsSample(in_alloc, sampler, new_coord);
6156
*out = rsPackColorTo8888(fout);
6257
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
rs_allocation in_alloc;
18+
rs_sampler sampler;
19+
20+
static float2 center, neg_center, inv_dimensions, axis_scale;
21+
static float alpha, radius2, factor;
22+
23+
void init_filter(uint32_t dim_x, uint32_t dim_y, float center_x, float center_y, float k) {
24+
center.x = center_x;
25+
center.y = center_y;
26+
neg_center = -center;
27+
inv_dimensions.x = 1.f / (float)dim_x;
28+
inv_dimensions.y = 1.f / (float)dim_y;
29+
alpha = k * 2.0 + 0.75;
30+
31+
axis_scale = (float2)1.f;
32+
if (dim_x > dim_y)
33+
axis_scale.y = (float)dim_y / (float)dim_x;
34+
else
35+
axis_scale.x = (float)dim_x / (float)dim_y;
36+
37+
const float bound2 = 0.25 * (axis_scale.x*axis_scale.x + axis_scale.y*axis_scale.y);
38+
const float bound = sqrt(bound2);
39+
const float radius = 1.15 * bound;
40+
radius2 = radius*radius;
41+
const float max_radian = M_PI_2 - atan(alpha / bound * sqrt(radius2 - bound2));
42+
factor = bound / max_radian;
43+
}
44+
45+
void root(uchar4 *out, uint32_t x, uint32_t y) {
46+
// Convert x and y to floating point coordinates with center as origin
47+
const float2 inCoord = {(float)x, (float)y};
48+
const float2 coord = mad(inCoord, inv_dimensions, neg_center);
49+
const float2 scaledCoord = axis_scale * coord;
50+
const float dist2 = scaledCoord.x*scaledCoord.x + scaledCoord.y*scaledCoord.y;
51+
const float inv_dist = approx_rsqrt(dist2);
52+
const float radian = M_PI_2 - approx_atan((alpha * approx_sqrt(radius2 - dist2)) * inv_dist);
53+
const float scalar = radian * factor * inv_dist;
54+
const float2 new_coord = mad(coord, scalar, center);
55+
const float4 fout = rsSample(in_alloc, sampler, new_coord);
56+
*out = rsPackColorTo8888(fout);
57+
}
58+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
#pragma version(1)
18+
#pragma rs java_package_name(com.android.rs.image)
19+
20+
#include "fisheye_approx.rsh"
21+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
#pragma version(1)
18+
#pragma rs java_package_name(com.android.rs.image)
19+
#pragma rs_fp_relaxed
20+
21+
#include "fisheye_approx.rsh"
22+

0 commit comments

Comments
 (0)