Merge "Fisheye filter: approx version, general performance improvements" into jb-mr1-dev

This commit is contained in:
Stephen Hines
2012-08-16 14:37:33 -07:00
committed by Android (Google) Code Review
6 changed files with 186 additions and 50 deletions

View File

@@ -26,12 +26,16 @@ import android.widget.TextView;
public class Fisheye extends TestBase { public class Fisheye extends TestBase {
private ScriptC_fisheye_full mScript_full = null; private ScriptC_fisheye_full mScript_full = null;
private ScriptC_fisheye_relaxed mScript_relaxed = 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 final boolean relaxed;
private float center_x = 0.5f; private float center_x = 0.5f;
private float center_y = 0.5f; private float center_y = 0.5f;
private float scale = 0.5f; private float scale = 0.5f;
public Fisheye(boolean relaxed) { public Fisheye(boolean approx, boolean relaxed) {
this.approx = approx;
this.relaxed = relaxed; this.relaxed = relaxed;
} }
@@ -68,7 +72,18 @@ public class Fisheye extends TestBase {
} }
private void do_init() { 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( mScript_relaxed.invoke_init_filter(
mInPixelsAllocation.getType().getX(), mInPixelsAllocation.getType().getX(),
mInPixelsAllocation.getType().getY(), center_x, center_y, mInPixelsAllocation.getType().getY(), center_x, center_y,
@@ -81,7 +96,19 @@ public class Fisheye extends TestBase {
} }
public void createTest(android.content.res.Resources res) { 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, mScript_relaxed = new ScriptC_fisheye_relaxed(mRS, res,
R.raw.fisheye_relaxed); R.raw.fisheye_relaxed);
mScript_relaxed.set_in_alloc(mInPixelsAllocation); mScript_relaxed.set_in_alloc(mInPixelsAllocation);
@@ -96,7 +123,12 @@ public class Fisheye extends TestBase {
} }
public void runTest() { 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); mScript_relaxed.forEach_root(mOutPixelsAllocation);
else else
mScript_full.forEach_root(mOutPixelsAllocation); mScript_full.forEach_root(mOutPixelsAllocation);

View File

@@ -144,30 +144,36 @@ public class ImageProcessingActivity extends Activity
mTest = new Grain(); mTest = new Grain();
break; break;
case 7: case 7:
mTest = new Fisheye(false); mTest = new Fisheye(false, false);
break; break;
case 8: case 8:
mTest = new Fisheye(true); mTest = new Fisheye(false, true);
break; break;
case 9: case 9:
mTest = new Vignette(false, false); mTest = new Fisheye(true, false);
break; break;
case 10: case 10:
mTest = new Vignette(false, true); mTest = new Fisheye(true, true);
break; break;
case 11: case 11:
mTest = new Vignette(true, false); mTest = new Vignette(false, false);
break; break;
case 12: case 12:
mTest = new Vignette(true, true); mTest = new Vignette(false, true);
break; break;
case 13: case 13:
mTest = new GroupTest(true); mTest = new Vignette(true, false);
break; break;
case 14: case 14:
mTest = new GroupTest(false); mTest = new Vignette(true, true);
break; break;
case 15: case 15:
mTest = new GroupTest(true);
break;
case 16:
mTest = new GroupTest(false);
break;
case 17:
mTest = new Intrinsics(0); mTest = new Intrinsics(0);
break; break;
} }
@@ -182,7 +188,7 @@ public class ImageProcessingActivity extends Activity
} }
void setupTests() { void setupTests() {
mTestNames = new String[16]; mTestNames = new String[18];
mTestNames[0] = "Levels Vec3 Relaxed"; mTestNames[0] = "Levels Vec3 Relaxed";
mTestNames[1] = "Levels Vec4 Relaxed"; mTestNames[1] = "Levels Vec4 Relaxed";
mTestNames[2] = "Levels Vec3 Full"; mTestNames[2] = "Levels Vec3 Full";
@@ -192,13 +198,15 @@ public class ImageProcessingActivity extends Activity
mTestNames[6] = "Grain"; mTestNames[6] = "Grain";
mTestNames[7] = "Fisheye Full"; mTestNames[7] = "Fisheye Full";
mTestNames[8] = "Fisheye Relaxed"; mTestNames[8] = "Fisheye Relaxed";
mTestNames[9] = "Vignette Full"; mTestNames[9] = "Fisheye Approximate Full";
mTestNames[10] = "Vignette Relaxed"; mTestNames[10] = "Fisheye Approximate Relaxed";
mTestNames[11] = "Vignette Approximate Full"; mTestNames[11] = "Vignette Full";
mTestNames[12] = "Vignette Approximate Relaxed"; mTestNames[12] = "Vignette Relaxed";
mTestNames[13] = "Group Test (emulated)"; mTestNames[13] = "Vignette Approximate Full";
mTestNames[14] = "Group Test (native)"; mTestNames[14] = "Vignette Approximate Relaxed";
mTestNames[15] = "Intrinsics Convolve 3x3"; mTestNames[15] = "Group Test (emulated)";
mTestNames[16] = "Group Test (native)";
mTestNames[17] = "Intrinsics Convolve 3x3";
mTestSpinner.setAdapter(new ArrayAdapter<String>( mTestSpinner.setAdapter(new ArrayAdapter<String>(
this, R.layout.spinner_layout, mTestNames)); this, R.layout.spinner_layout, mTestNames));
} }

View File

@@ -17,46 +17,41 @@
rs_allocation in_alloc; rs_allocation in_alloc;
rs_sampler sampler; rs_sampler sampler;
static float2 center, dimensions; static float2 center, neg_center, inv_dimensions, axis_scale;
static float2 scale; static float alpha, radius2, factor;
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;
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; alpha = k * 2.0 + 0.75;
float bound2 = 0.25;
if (dim_x > dim_y) { axis_scale = (float2)1.f;
scale.x = 1.0; if (dim_x > dim_y)
scale.y = dimensions.y / dimensions.x; axis_scale.y = (float)dim_y / (float)dim_x;
bound2 *= (scale.y*scale.y + 1); else
} else { axis_scale.x = (float)dim_x / (float)dim_y;
scale.x = dimensions.x / dimensions.y;
scale.y = 1.0; const float bound2 = 0.25 * (axis_scale.x*axis_scale.x + axis_scale.y*axis_scale.y);
bound2 *= (scale.x*scale.x + 1);
}
const float bound = sqrt(bound2); const float bound = sqrt(bound2);
const float radius = 1.15 * bound; const float radius = 1.15 * bound;
radius2 = radius*radius; 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; factor = bound / max_radian;
} }
void root(uchar4 *out, uint32_t x, uint32_t y) { void root(uchar4 *out, uint32_t x, uint32_t y) {
// Convert x and y to floating point coordinates with center as origin // Convert x and y to floating point coordinates with center as origin
float2 coord; const float2 inCoord = {(float)x, (float)y};
coord.x = (float)x / dimensions.x; const float2 coord = mad(inCoord, inv_dimensions, neg_center);
coord.y = (float)y / dimensions.y; const float2 scaledCoord = axis_scale * coord;
coord -= center; const float dist2 = scaledCoord.x*scaledCoord.x + scaledCoord.y*scaledCoord.y;
const float dist = length(scale * coord); const float inv_dist = rsqrt(dist2);
const float radian = M_PI_2 - atan((alpha * sqrt(radius2 - dist * dist)) / dist); const float radian = M_PI_2 - atan((alpha * sqrt(radius2 - dist2)) * inv_dist);
const float scalar = radian * factor / dist; const float scalar = radian * factor * inv_dist;
const float2 new_coord = coord * scalar + center; const float2 new_coord = mad(coord, scalar, center);
const float4 fout = rsSample(in_alloc, sampler, new_coord); const float4 fout = rsSample(in_alloc, sampler, new_coord);
*out = rsPackColorTo8888(fout); *out = rsPackColorTo8888(fout);
} }

View File

@@ -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);
}

View File

@@ -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"

View File

@@ -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"