From 50b1ba0083115c4fb578f13e0b94c6d80b5b1841 Mon Sep 17 00:00:00 2001 From: Rajeev Sharma Date: Wed, 15 Aug 2012 15:16:52 -0700 Subject: [PATCH] Fisheye filter: approx version, general performance improvements Lightly refactor original filter and rework math for performance. Add approx version which uses approx_rsqrt, approx_sqrt, and the new approx_atan function. Change-Id: I796d50da05c1684167696ea4da37d7d75fc78626 --- .../src/com/android/rs/image/Fisheye.java | 40 +++++++++++-- .../rs/image/ImageProcessingActivity.java | 40 ++++++++----- .../src/com/android/rs/image/fisheye.rsh | 55 ++++++++---------- .../com/android/rs/image/fisheye_approx.rsh | 58 +++++++++++++++++++ .../android/rs/image/fisheye_approx_full.rs | 21 +++++++ .../rs/image/fisheye_approx_relaxed.rs | 22 +++++++ 6 files changed, 186 insertions(+), 50 deletions(-) create mode 100644 tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/fisheye_approx.rsh create mode 100644 tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/fisheye_approx_full.rs create mode 100644 tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/fisheye_approx_relaxed.rs diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/Fisheye.java b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/Fisheye.java index bf68f919f0357..81868b1036859 100644 --- a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/Fisheye.java +++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/Fisheye.java @@ -26,12 +26,16 @@ import android.widget.TextView; public class Fisheye extends TestBase { private ScriptC_fisheye_full mScript_full = null; private ScriptC_fisheye_relaxed mScript_relaxed = null; + private ScriptC_fisheye_approx_full mScript_approx_full = null; + private ScriptC_fisheye_approx_relaxed mScript_approx_relaxed = null; + private final boolean approx; private final boolean relaxed; private float center_x = 0.5f; private float center_y = 0.5f; private float scale = 0.5f; - public Fisheye(boolean relaxed) { + public Fisheye(boolean approx, boolean relaxed) { + this.approx = approx; this.relaxed = relaxed; } @@ -68,7 +72,18 @@ public class Fisheye extends TestBase { } private void do_init() { - if (relaxed) + if (approx) { + if (relaxed) + mScript_approx_relaxed.invoke_init_filter( + mInPixelsAllocation.getType().getX(), + mInPixelsAllocation.getType().getY(), center_x, + center_y, scale); + else + mScript_approx_full.invoke_init_filter( + mInPixelsAllocation.getType().getX(), + mInPixelsAllocation.getType().getY(), center_x, + center_y, scale); + } else if (relaxed) mScript_relaxed.invoke_init_filter( mInPixelsAllocation.getType().getX(), mInPixelsAllocation.getType().getY(), center_x, center_y, @@ -81,7 +96,19 @@ public class Fisheye extends TestBase { } public void createTest(android.content.res.Resources res) { - if (relaxed) { + if (approx) { + if (relaxed) { + mScript_approx_relaxed = new ScriptC_fisheye_approx_relaxed(mRS, + res, R.raw.fisheye_approx_relaxed); + mScript_approx_relaxed.set_in_alloc(mInPixelsAllocation); + mScript_approx_relaxed.set_sampler(Sampler.CLAMP_LINEAR(mRS)); + } else { + mScript_approx_full = new ScriptC_fisheye_approx_full(mRS, res, + R.raw.fisheye_approx_full); + mScript_approx_full.set_in_alloc(mInPixelsAllocation); + mScript_approx_full.set_sampler(Sampler.CLAMP_LINEAR(mRS)); + } + } else if (relaxed) { mScript_relaxed = new ScriptC_fisheye_relaxed(mRS, res, R.raw.fisheye_relaxed); mScript_relaxed.set_in_alloc(mInPixelsAllocation); @@ -96,7 +123,12 @@ public class Fisheye extends TestBase { } public void runTest() { - if (relaxed) + if (approx) { + if (relaxed) + mScript_approx_relaxed.forEach_root(mOutPixelsAllocation); + else + mScript_approx_full.forEach_root(mOutPixelsAllocation); + } else if (relaxed) mScript_relaxed.forEach_root(mOutPixelsAllocation); else mScript_full.forEach_root(mOutPixelsAllocation); diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java index 3eec7f5bf0e01..07626a350a79b 100644 --- a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java +++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java @@ -144,30 +144,36 @@ public class ImageProcessingActivity extends Activity mTest = new Grain(); break; case 7: - mTest = new Fisheye(false); + mTest = new Fisheye(false, false); break; case 8: - mTest = new Fisheye(true); + mTest = new Fisheye(false, true); break; case 9: - mTest = new Vignette(false, false); + mTest = new Fisheye(true, false); break; case 10: - mTest = new Vignette(false, true); + mTest = new Fisheye(true, true); break; case 11: - mTest = new Vignette(true, false); + mTest = new Vignette(false, false); break; case 12: - mTest = new Vignette(true, true); + mTest = new Vignette(false, true); break; case 13: - mTest = new GroupTest(true); + mTest = new Vignette(true, false); break; case 14: - mTest = new GroupTest(false); + mTest = new Vignette(true, true); break; case 15: + mTest = new GroupTest(true); + break; + case 16: + mTest = new GroupTest(false); + break; + case 17: mTest = new Intrinsics(0); break; } @@ -182,7 +188,7 @@ public class ImageProcessingActivity extends Activity } void setupTests() { - mTestNames = new String[16]; + mTestNames = new String[18]; mTestNames[0] = "Levels Vec3 Relaxed"; mTestNames[1] = "Levels Vec4 Relaxed"; mTestNames[2] = "Levels Vec3 Full"; @@ -192,13 +198,15 @@ public class ImageProcessingActivity extends Activity mTestNames[6] = "Grain"; mTestNames[7] = "Fisheye Full"; mTestNames[8] = "Fisheye Relaxed"; - mTestNames[9] = "Vignette Full"; - mTestNames[10] = "Vignette Relaxed"; - mTestNames[11] = "Vignette Approximate Full"; - mTestNames[12] = "Vignette Approximate Relaxed"; - mTestNames[13] = "Group Test (emulated)"; - mTestNames[14] = "Group Test (native)"; - mTestNames[15] = "Intrinsics Convolve 3x3"; + mTestNames[9] = "Fisheye Approximate Full"; + mTestNames[10] = "Fisheye Approximate Relaxed"; + mTestNames[11] = "Vignette Full"; + mTestNames[12] = "Vignette Relaxed"; + mTestNames[13] = "Vignette Approximate Full"; + mTestNames[14] = "Vignette Approximate Relaxed"; + mTestNames[15] = "Group Test (emulated)"; + mTestNames[16] = "Group Test (native)"; + mTestNames[17] = "Intrinsics Convolve 3x3"; mTestSpinner.setAdapter(new ArrayAdapter( this, R.layout.spinner_layout, mTestNames)); } diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/fisheye.rsh b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/fisheye.rsh index 4dcfc1daaca01..3809912eac893 100644 --- a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/fisheye.rsh +++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/fisheye.rsh @@ -17,46 +17,41 @@ rs_allocation in_alloc; rs_sampler sampler; -static float2 center, dimensions; -static float2 scale; -static float alpha; -static float radius2; -static float factor; - -void init_filter(uint32_t dim_x, uint32_t dim_y, float focus_x, float focus_y, float k) { - center.x = focus_x; - center.y = focus_y; - dimensions.x = (float)dim_x; - dimensions.y = (float)dim_y; +static float2 center, neg_center, inv_dimensions, axis_scale; +static float alpha, radius2, factor; +void init_filter(uint32_t dim_x, uint32_t dim_y, float center_x, float center_y, float k) { + center.x = center_x; + center.y = center_y; + neg_center = -center; + inv_dimensions.x = 1.f / (float)dim_x; + inv_dimensions.y = 1.f / (float)dim_y; alpha = k * 2.0 + 0.75; - float bound2 = 0.25; - if (dim_x > dim_y) { - scale.x = 1.0; - scale.y = dimensions.y / dimensions.x; - bound2 *= (scale.y*scale.y + 1); - } else { - scale.x = dimensions.x / dimensions.y; - scale.y = 1.0; - bound2 *= (scale.x*scale.x + 1); - } + + axis_scale = (float2)1.f; + if (dim_x > dim_y) + axis_scale.y = (float)dim_y / (float)dim_x; + else + axis_scale.x = (float)dim_x / (float)dim_y; + + const float bound2 = 0.25 * (axis_scale.x*axis_scale.x + axis_scale.y*axis_scale.y); const float bound = sqrt(bound2); const float radius = 1.15 * bound; radius2 = radius*radius; - const float max_radian = 0.5f * M_PI - atan(alpha / bound * sqrt(radius2 - bound2)); + const float max_radian = M_PI_2 - atan(alpha / bound * sqrt(radius2 - bound2)); factor = bound / max_radian; } void root(uchar4 *out, uint32_t x, uint32_t y) { // Convert x and y to floating point coordinates with center as origin - float2 coord; - coord.x = (float)x / dimensions.x; - coord.y = (float)y / dimensions.y; - coord -= center; - const float dist = length(scale * coord); - const float radian = M_PI_2 - atan((alpha * sqrt(radius2 - dist * dist)) / dist); - const float scalar = radian * factor / dist; - const float2 new_coord = coord * scalar + center; + const float2 inCoord = {(float)x, (float)y}; + const float2 coord = mad(inCoord, inv_dimensions, neg_center); + const float2 scaledCoord = axis_scale * coord; + const float dist2 = scaledCoord.x*scaledCoord.x + scaledCoord.y*scaledCoord.y; + const float inv_dist = rsqrt(dist2); + const float radian = M_PI_2 - atan((alpha * sqrt(radius2 - dist2)) * inv_dist); + const float scalar = radian * factor * inv_dist; + const float2 new_coord = mad(coord, scalar, center); const float4 fout = rsSample(in_alloc, sampler, new_coord); *out = rsPackColorTo8888(fout); } diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/fisheye_approx.rsh b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/fisheye_approx.rsh new file mode 100644 index 0000000000000..008acbe699442 --- /dev/null +++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/fisheye_approx.rsh @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +rs_allocation in_alloc; +rs_sampler sampler; + +static float2 center, neg_center, inv_dimensions, axis_scale; +static float alpha, radius2, factor; + +void init_filter(uint32_t dim_x, uint32_t dim_y, float center_x, float center_y, float k) { + center.x = center_x; + center.y = center_y; + neg_center = -center; + inv_dimensions.x = 1.f / (float)dim_x; + inv_dimensions.y = 1.f / (float)dim_y; + alpha = k * 2.0 + 0.75; + + axis_scale = (float2)1.f; + if (dim_x > dim_y) + axis_scale.y = (float)dim_y / (float)dim_x; + else + axis_scale.x = (float)dim_x / (float)dim_y; + + const float bound2 = 0.25 * (axis_scale.x*axis_scale.x + axis_scale.y*axis_scale.y); + const float bound = sqrt(bound2); + const float radius = 1.15 * bound; + radius2 = radius*radius; + const float max_radian = M_PI_2 - atan(alpha / bound * sqrt(radius2 - bound2)); + factor = bound / max_radian; +} + +void root(uchar4 *out, uint32_t x, uint32_t y) { + // Convert x and y to floating point coordinates with center as origin + const float2 inCoord = {(float)x, (float)y}; + const float2 coord = mad(inCoord, inv_dimensions, neg_center); + const float2 scaledCoord = axis_scale * coord; + const float dist2 = scaledCoord.x*scaledCoord.x + scaledCoord.y*scaledCoord.y; + const float inv_dist = approx_rsqrt(dist2); + const float radian = M_PI_2 - approx_atan((alpha * approx_sqrt(radius2 - dist2)) * inv_dist); + const float scalar = radian * factor * inv_dist; + const float2 new_coord = mad(coord, scalar, center); + const float4 fout = rsSample(in_alloc, sampler, new_coord); + *out = rsPackColorTo8888(fout); +} + diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/fisheye_approx_full.rs b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/fisheye_approx_full.rs new file mode 100644 index 0000000000000..1ea37dbe76563 --- /dev/null +++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/fisheye_approx_full.rs @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma version(1) +#pragma rs java_package_name(com.android.rs.image) + +#include "fisheye_approx.rsh" + diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/fisheye_approx_relaxed.rs b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/fisheye_approx_relaxed.rs new file mode 100644 index 0000000000000..3e76368319fbf --- /dev/null +++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/fisheye_approx_relaxed.rs @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma version(1) +#pragma rs java_package_name(com.android.rs.image) +#pragma rs_fp_relaxed + +#include "fisheye_approx.rsh" +