From 52541816fb308fcac31e86eb3293c2b28b0999de Mon Sep 17 00:00:00 2001 From: Jason Sams Date: Thu, 19 Jul 2012 16:31:47 -0700 Subject: [PATCH] Refactor ImageProcessing benchmark and stabilize results. Change-Id: I3d43bf855515a3ab707089e97cc310b561683f5f --- .../ImageProcessing/res/layout/main.xml | 54 +--- .../ImageProcessing/res/layout/spinner_layout.xml | 23 ++ .../src/com/android/rs/image/Blur25.java | 106 +++++++ .../src/com/android/rs/image/Greyscale.java | 40 +++ .../android/rs/image/ImageProcessingActivity.java | 333 +++++++++------------ .../src/com/android/rs/image/LevelsV4.java | 167 +++++++++++ .../src/com/android/rs/image/TestBase.java | 122 ++++++++ .../src/com/android/rs/image/greyscale.rs | 30 ++ .../src/com/android/rs/image/levels.rsh | 44 +++ .../src/com/android/rs/image/levels_full.rs | 21 ++ .../src/com/android/rs/image/levels_relaxed.rs | 22 ++ .../src/com/android/rs/image/threshold.rs | 2 +- .../src/com/android/rs/image/vertical_blur.rs | 32 +- 13 files changed, 742 insertions(+), 254 deletions(-) create mode 100644 tests/RenderScriptTests/ImageProcessing/res/layout/spinner_layout.xml create mode 100644 tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/Blur25.java create mode 100644 tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/Greyscale.java create mode 100644 tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/LevelsV4.java create mode 100644 tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/TestBase.java create mode 100644 tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/greyscale.rs create mode 100644 tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/levels.rsh create mode 100644 tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/levels_full.rs create mode 100644 tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/levels_relaxed.rs diff --git a/tests/RenderScriptTests/ImageProcessing/res/layout/main.xml b/tests/RenderScriptTests/ImageProcessing/res/layout/main.xml index 08a010ddc995..bd56d62d29d2 100644 --- a/tests/RenderScriptTests/ImageProcessing/res/layout/main.xml +++ b/tests/RenderScriptTests/ImageProcessing/res/layout/main.xml @@ -50,8 +50,12 @@ android:textSize="8pt" android:text="@string/saturation"/> + - - - - + android:text="@string/in_white"/> + + + + diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/Blur25.java b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/Blur25.java new file mode 100644 index 000000000000..697bbb1f9120 --- /dev/null +++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/Blur25.java @@ -0,0 +1,106 @@ +/* + * 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. + */ + +package com.android.rs.image; + +import java.lang.Math; + +import android.renderscript.Allocation; +import android.renderscript.Element; +import android.renderscript.RenderScript; +import android.renderscript.Script; +import android.renderscript.ScriptC; +import android.renderscript.Type; +import android.util.Log; +import android.widget.SeekBar; +import android.widget.TextView; + +public class Blur25 extends TestBase { + private int MAX_RADIUS = 25; + private ScriptC_threshold mScript; + private ScriptC_vertical_blur mScriptVBlur; + private ScriptC_horizontal_blur mScriptHBlur; + private int mRadius = MAX_RADIUS; + private float mSaturation = 1.0f; + private Allocation mScratchPixelsAllocation1; + private Allocation mScratchPixelsAllocation2; + + + public boolean onBar1Setup(SeekBar b, TextView t) { + t.setText("Radius"); + b.setProgress(100); + return true; + } + public boolean onBar2Setup(SeekBar b, TextView t) { + b.setProgress(50); + t.setText("Saturation"); + return true; + } + + + public void onBar1Changed(int progress) { + float fRadius = progress / 100.0f; + fRadius *= (float)(MAX_RADIUS); + mRadius = (int)fRadius; + mScript.set_radius(mRadius); + } + public void onBar2Changed(int progress) { + mSaturation = (float)progress / 50.0f; + mScriptVBlur.invoke_setSaturation(mSaturation); + } + + + public void createTest(android.content.res.Resources res) { + int width = mInPixelsAllocation.getType().getX(); + int height = mInPixelsAllocation.getType().getY(); + + Type.Builder tb = new Type.Builder(mRS, Element.F32_4(mRS)); + tb.setX(width); + tb.setY(height); + mScratchPixelsAllocation1 = Allocation.createTyped(mRS, tb.create()); + mScratchPixelsAllocation2 = Allocation.createTyped(mRS, tb.create()); + + mScriptVBlur = new ScriptC_vertical_blur(mRS, res, R.raw.vertical_blur); + mScriptHBlur = new ScriptC_horizontal_blur(mRS, res, R.raw.horizontal_blur); + + mScript = new ScriptC_threshold(mRS, res, R.raw.threshold); + mScript.set_width(width); + mScript.set_height(height); + mScript.set_radius(mRadius); + + mScriptVBlur.invoke_setSaturation(mSaturation); + + mScript.bind_InPixel(mInPixelsAllocation); + mScript.bind_OutPixel(mOutPixelsAllocation); + mScript.bind_ScratchPixel1(mScratchPixelsAllocation1); + mScript.bind_ScratchPixel2(mScratchPixelsAllocation2); + + mScript.set_vBlurScript(mScriptVBlur); + mScript.set_hBlurScript(mScriptHBlur); + } + + public void runTest() { + mScript.invoke_filter(); + } + + public void setupBenchmark() { + mScript.set_radius(MAX_RADIUS); + } + + public void exitBenchmark() { + mScript.set_radius(mRadius); + } +} diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/Greyscale.java b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/Greyscale.java new file mode 100644 index 000000000000..3db210a39a2a --- /dev/null +++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/Greyscale.java @@ -0,0 +1,40 @@ +/* + * 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. + */ + +package com.android.rs.image; + +import java.lang.Math; + +import android.renderscript.Allocation; +import android.renderscript.Element; +import android.renderscript.RenderScript; +import android.renderscript.Script; +import android.renderscript.ScriptC; +import android.renderscript.Type; +import android.util.Log; + +public class Greyscale extends TestBase { + private ScriptC_greyscale mScript; + + public void createTest(android.content.res.Resources res) { + mScript = new ScriptC_greyscale(mRS, res, R.raw.greyscale); + } + + public void runTest() { + mScript.forEach_root(mInPixelsAllocation, 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 73682600ec10..e085582c3027 100644 --- a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java +++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * 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. @@ -29,133 +29,151 @@ import android.renderscript.Element; import android.renderscript.Script; import android.view.SurfaceView; import android.view.SurfaceHolder; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.SeekBar; +import android.widget.Spinner; import android.widget.TextView; import android.view.View; import android.util.Log; import java.lang.Math; public class ImageProcessingActivity extends Activity - implements SurfaceHolder.Callback, - SeekBar.OnSeekBarChangeListener { + implements SeekBar.OnSeekBarChangeListener { private final String TAG = "Img"; - private Bitmap mBitmapIn; - private Bitmap mBitmapOut; - private ScriptC_threshold mScript; - private ScriptC_vertical_blur mScriptVBlur; - private ScriptC_horizontal_blur mScriptHBlur; - private int mRadius = 0; - private SeekBar mRadiusSeekBar; - - private float mInBlack = 0.0f; - private SeekBar mInBlackSeekBar; - private float mOutBlack = 0.0f; - private SeekBar mOutBlackSeekBar; - private float mInWhite = 255.0f; - private SeekBar mInWhiteSeekBar; - private float mOutWhite = 255.0f; - private SeekBar mOutWhiteSeekBar; - private float mGamma = 1.0f; - private SeekBar mGammaSeekBar; + Bitmap mBitmapIn; + Bitmap mBitmapOut; + String mTestNames[]; + + private SeekBar mBar1; + private SeekBar mBar2; + private SeekBar mBar3; + private SeekBar mBar4; + private SeekBar mBar5; + private TextView mText1; + private TextView mText2; + private TextView mText3; + private TextView mText4; + private TextView mText5; private float mSaturation = 1.0f; - private SeekBar mSaturationSeekBar; private TextView mBenchmarkResult; - - @SuppressWarnings({"FieldCanBeLocal"}) - private RenderScript mRS; - @SuppressWarnings({"FieldCanBeLocal"}) - private Type mPixelType; - @SuppressWarnings({"FieldCanBeLocal"}) - private Allocation mInPixelsAllocation; - @SuppressWarnings({"FieldCanBeLocal"}) - private Allocation mOutPixelsAllocation; - @SuppressWarnings({"FieldCanBeLocal"}) - private Allocation mScratchPixelsAllocation1; - private Allocation mScratchPixelsAllocation2; + private Spinner mTestSpinner; private SurfaceView mSurfaceView; private ImageView mDisplayView; - private boolean mIsProcessing; + private boolean mDoingBenchmark; - class FilterCallback extends RenderScript.RSMessageHandler { - private Runnable mAction = new Runnable() { - public void run() { + private TestBase mTest; - synchronized (mDisplayView) { - mIsProcessing = false; - } - mOutPixelsAllocation.copyTo(mBitmapOut); - mDisplayView.invalidate(); + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + if (fromUser) { + + if (seekBar == mBar1) { + mTest.onBar1Changed(progress); + } else if (seekBar == mBar2) { + mTest.onBar2Changed(progress); + } else if (seekBar == mBar3) { + mTest.onBar3Changed(progress); + } else if (seekBar == mBar4) { + mTest.onBar4Changed(progress); + } else if (seekBar == mBar5) { + mTest.onBar5Changed(progress); } - }; - @Override - public void run() { - mSurfaceView.removeCallbacks(mAction); - mSurfaceView.post(mAction); + mTest.runTest(); + mTest.updateBitmap(mBitmapOut); + mDisplayView.invalidate(); } } - int in[]; - int interm[]; - int out[]; - int MAX_RADIUS = 25; - // Store our coefficients here - float gaussian[]; + public void onStartTrackingTouch(SeekBar seekBar) { + } - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - if (fromUser) { + public void onStopTrackingTouch(SeekBar seekBar) { + } - if (seekBar == mRadiusSeekBar) { - float fRadius = progress / 100.0f; - fRadius *= (float)(MAX_RADIUS); - mRadius = (int)fRadius; - - mScript.set_radius(mRadius); - } else if (seekBar == mInBlackSeekBar) { - mInBlack = (float)progress; - mScriptVBlur.invoke_setLevels(mInBlack, mOutBlack, mInWhite, mOutWhite); - } else if (seekBar == mOutBlackSeekBar) { - mOutBlack = (float)progress; - mScriptVBlur.invoke_setLevels(mInBlack, mOutBlack, mInWhite, mOutWhite); - } else if (seekBar == mInWhiteSeekBar) { - mInWhite = (float)progress + 127.0f; - mScriptVBlur.invoke_setLevels(mInBlack, mOutBlack, mInWhite, mOutWhite); - } else if (seekBar == mOutWhiteSeekBar) { - mOutWhite = (float)progress + 127.0f; - mScriptVBlur.invoke_setLevels(mInBlack, mOutBlack, mInWhite, mOutWhite); - } else if (seekBar == mGammaSeekBar) { - mGamma = (float)progress/100.0f; - mGamma = Math.max(mGamma, 0.1f); - mGamma = 1.0f / mGamma; - mScriptVBlur.invoke_setGamma(mGamma); - } else if (seekBar == mSaturationSeekBar) { - mSaturation = (float)progress / 50.0f; - mScriptVBlur.invoke_setSaturation(mSaturation); - } + void setupBars() { + mBar1.setVisibility(View.VISIBLE); + mText1.setVisibility(View.VISIBLE); + mTest.onBar1Setup(mBar1, mText1); - synchronized (mDisplayView) { - if (mIsProcessing) { - return; - } - mIsProcessing = true; - } + mBar2.setVisibility(View.VISIBLE); + mText2.setVisibility(View.VISIBLE); + mTest.onBar2Setup(mBar2, mText2); - mScript.invoke_filter(); - } + mBar3.setVisibility(View.VISIBLE); + mText3.setVisibility(View.VISIBLE); + mTest.onBar3Setup(mBar3, mText3); + + mBar4.setVisibility(View.VISIBLE); + mText4.setVisibility(View.VISIBLE); + mTest.onBar4Setup(mBar4, mText4); + + mBar5.setVisibility(View.VISIBLE); + mText5.setVisibility(View.VISIBLE); + mTest.onBar5Setup(mBar5, mText5); } - public void onStartTrackingTouch(SeekBar seekBar) { + + void changeTest(int testID) { + switch(testID) { + case 0: + mTest = new LevelsV4(false, false); + break; + case 1: + mTest = new LevelsV4(false, true); + break; + case 2: + mTest = new LevelsV4(true, false); + break; + case 3: + mTest = new LevelsV4(true, true); + break; + case 4: + mTest = new Blur25(); + break; + case 5: + mTest = new Greyscale(); + break; + } + + mTest.createBaseTest(this, mBitmapIn); + setupBars(); + + mTest.runTest(); + mTest.updateBitmap(mBitmapOut); + mDisplayView.invalidate(); + mBenchmarkResult.setText("Result: not run"); } - public void onStopTrackingTouch(SeekBar seekBar) { + void setupTests() { + mTestNames = new String[6]; + mTestNames[0] = "Levels Vec3 Relaxed"; + mTestNames[1] = "Levels Vec4 Relaxed"; + mTestNames[2] = "Levels Vec3 Full"; + mTestNames[3] = "Levels Vec4 Full"; + mTestNames[4] = "Blur radius 25"; + mTestNames[5] = "Greyscale"; + mTestSpinner.setAdapter(new ArrayAdapter( + this, R.layout.spinner_layout, mTestNames)); } + private AdapterView.OnItemSelectedListener mTestSpinnerListener = + new AdapterView.OnItemSelectedListener() { + public void onItemSelected(AdapterView parent, View view, int pos, long id) { + changeTest(pos); + } + + public void onNothingSelected(AdapterView parent) { + + } + }; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -165,95 +183,39 @@ public class ImageProcessingActivity extends Activity mBitmapOut = loadBitmap(R.drawable.city); mSurfaceView = (SurfaceView) findViewById(R.id.surface); - mSurfaceView.getHolder().addCallback(this); mDisplayView = (ImageView) findViewById(R.id.display); mDisplayView.setImageBitmap(mBitmapOut); - mRadiusSeekBar = (SeekBar) findViewById(R.id.radius); - mRadiusSeekBar.setOnSeekBarChangeListener(this); - - mInBlackSeekBar = (SeekBar)findViewById(R.id.inBlack); - mInBlackSeekBar.setOnSeekBarChangeListener(this); - mInBlackSeekBar.setMax(128); - mInBlackSeekBar.setProgress(0); - mOutBlackSeekBar = (SeekBar)findViewById(R.id.outBlack); - mOutBlackSeekBar.setOnSeekBarChangeListener(this); - mOutBlackSeekBar.setMax(128); - mOutBlackSeekBar.setProgress(0); - - mInWhiteSeekBar = (SeekBar)findViewById(R.id.inWhite); - mInWhiteSeekBar.setOnSeekBarChangeListener(this); - mInWhiteSeekBar.setMax(128); - mInWhiteSeekBar.setProgress(128); - mOutWhiteSeekBar = (SeekBar)findViewById(R.id.outWhite); - mOutWhiteSeekBar.setOnSeekBarChangeListener(this); - mOutWhiteSeekBar.setMax(128); - mOutWhiteSeekBar.setProgress(128); - - mGammaSeekBar = (SeekBar)findViewById(R.id.inGamma); - mGammaSeekBar.setOnSeekBarChangeListener(this); - mGammaSeekBar.setMax(150); - mGammaSeekBar.setProgress(100); - - mSaturationSeekBar = (SeekBar)findViewById(R.id.inSaturation); - mSaturationSeekBar.setOnSeekBarChangeListener(this); - mSaturationSeekBar.setProgress(50); + mBar1 = (SeekBar) findViewById(R.id.slider1); + mBar2 = (SeekBar) findViewById(R.id.slider2); + mBar3 = (SeekBar) findViewById(R.id.slider3); + mBar4 = (SeekBar) findViewById(R.id.slider4); + mBar5 = (SeekBar) findViewById(R.id.slider5); - mBenchmarkResult = (TextView) findViewById(R.id.benchmarkText); - mBenchmarkResult.setText("Result: not run"); - } + mBar1.setOnSeekBarChangeListener(this); + mBar2.setOnSeekBarChangeListener(this); + mBar3.setOnSeekBarChangeListener(this); + mBar4.setOnSeekBarChangeListener(this); + mBar5.setOnSeekBarChangeListener(this); - public void surfaceCreated(SurfaceHolder holder) { - createScript(); - mScript.invoke_filter(); - mOutPixelsAllocation.copyTo(mBitmapOut); - } + mText1 = (TextView) findViewById(R.id.slider1Text); + mText2 = (TextView) findViewById(R.id.slider2Text); + mText3 = (TextView) findViewById(R.id.slider3Text); + mText4 = (TextView) findViewById(R.id.slider4Text); + mText5 = (TextView) findViewById(R.id.slider5Text); - public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { - } + mTestSpinner = (Spinner) findViewById(R.id.filterselection); + mTestSpinner.setOnItemSelectedListener(mTestSpinnerListener); - public void surfaceDestroyed(SurfaceHolder holder) { - } + mBenchmarkResult = (TextView) findViewById(R.id.benchmarkText); + mBenchmarkResult.setText("Result: not run"); - private void createScript() { - mRS = RenderScript.create(this); - mRS.setMessageHandler(new FilterCallback()); - - mInPixelsAllocation = Allocation.createFromBitmap(mRS, mBitmapIn, - Allocation.MipmapControl.MIPMAP_NONE, - Allocation.USAGE_SCRIPT); - mOutPixelsAllocation = Allocation.createFromBitmap(mRS, mBitmapOut, - Allocation.MipmapControl.MIPMAP_NONE, - Allocation.USAGE_SCRIPT); - - Type.Builder tb = new Type.Builder(mRS, Element.F32_4(mRS)); - tb.setX(mBitmapIn.getWidth()); - tb.setY(mBitmapIn.getHeight()); - mScratchPixelsAllocation1 = Allocation.createTyped(mRS, tb.create()); - mScratchPixelsAllocation2 = Allocation.createTyped(mRS, tb.create()); - - mScriptVBlur = new ScriptC_vertical_blur(mRS, getResources(), R.raw.vertical_blur); - mScriptHBlur = new ScriptC_horizontal_blur(mRS, getResources(), R.raw.horizontal_blur); - - mScript = new ScriptC_threshold(mRS, getResources(), R.raw.threshold); - mScript.set_width(mBitmapIn.getWidth()); - mScript.set_height(mBitmapIn.getHeight()); - mScript.set_radius(mRadius); - - mScriptVBlur.invoke_setLevels(mInBlack, mOutBlack, mInWhite, mOutWhite); - mScriptVBlur.invoke_setGamma(mGamma); - mScriptVBlur.invoke_setSaturation(mSaturation); - - mScript.bind_InPixel(mInPixelsAllocation); - mScript.bind_OutPixel(mOutPixelsAllocation); - mScript.bind_ScratchPixel1(mScratchPixelsAllocation1); - mScript.bind_ScratchPixel2(mScratchPixelsAllocation2); - - mScript.set_vBlurScript(mScriptVBlur); - mScript.set_hBlurScript(mScriptHBlur); + setupTests(); + changeTest(0); } + private Bitmap loadBitmap(int resource) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; @@ -278,26 +240,29 @@ public class ImageProcessingActivity extends Activity // For benchmark test public long getBenchmark() { - Log.v(TAG, "Benchmarking"); - int oldRadius = mRadius; - mRadius = MAX_RADIUS; - mScript.set_radius(mRadius); + mDoingBenchmark = true; - mScript.invoke_filter(); - mRS.finish(); + mTest.setupBenchmark(); + long result = 0; - long t = java.lang.System.currentTimeMillis(); + Log.v(TAG, "Warming"); + long t = java.lang.System.currentTimeMillis() + 2000; + do { + mTest.runTest(); + mTest.finish(); + } while (t > java.lang.System.currentTimeMillis()); - mScript.invoke_filter(); - mOutPixelsAllocation.copyTo(mBitmapOut); + Log.v(TAG, "Benchmarking"); + t = java.lang.System.currentTimeMillis(); + mTest.runTest(); + mTest.finish(); t = java.lang.System.currentTimeMillis() - t; + Log.v(TAG, "getBenchmark: Renderscript frame time core ms " + t); - mRadius = oldRadius; - mScript.set_radius(mRadius); + mTest.exitBenchmark(); + mDoingBenchmark = false; - mScript.invoke_filter(); - mOutPixelsAllocation.copyTo(mBitmapOut); return t; } } diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/LevelsV4.java b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/LevelsV4.java new file mode 100644 index 000000000000..9eb5647e4284 --- /dev/null +++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/LevelsV4.java @@ -0,0 +1,167 @@ +/* + * 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. + */ + +package com.android.rs.image; + +import java.lang.Math; + +import android.renderscript.Allocation; +import android.renderscript.Element; +import android.renderscript.RenderScript; +import android.renderscript.Matrix3f; +import android.renderscript.Script; +import android.renderscript.ScriptC; +import android.renderscript.Type; +import android.util.Log; +import android.widget.SeekBar; +import android.widget.TextView; + + +public class LevelsV4 extends TestBase { + private ScriptC_levels_relaxed mScriptR; + private ScriptC_levels_full mScriptF; + private float mInBlack = 0.0f; + private float mOutBlack = 0.0f; + private float mInWhite = 255.0f; + private float mOutWhite = 255.0f; + private float mSaturation = 1.0f; + + Matrix3f satMatrix = new Matrix3f(); + float mInWMinInB; + float mOutWMinOutB; + float mOverInWMinInB; + + boolean mUseFull; + boolean mUseV4; + + LevelsV4(boolean useFull, boolean useV4) { + mUseFull = useFull; + mUseV4 = useV4; + } + + + private void setLevels() { + mInWMinInB = mInWhite - mInBlack; + mOutWMinOutB = mOutWhite - mOutBlack; + mOverInWMinInB = 1.f / mInWMinInB; + + mScriptR.set_inBlack(mInBlack); + mScriptR.set_outBlack(mOutBlack); + mScriptR.set_inWMinInB(mInWMinInB); + mScriptR.set_outWMinOutB(mOutWMinOutB); + mScriptR.set_overInWMinInB(mOverInWMinInB); + mScriptF.set_inBlack(mInBlack); + mScriptF.set_outBlack(mOutBlack); + mScriptF.set_inWMinInB(mInWMinInB); + mScriptF.set_outWMinOutB(mOutWMinOutB); + mScriptF.set_overInWMinInB(mOverInWMinInB); + } + + private void setSaturation() { + float rWeight = 0.299f; + float gWeight = 0.587f; + float bWeight = 0.114f; + float oneMinusS = 1.0f - mSaturation; + + satMatrix.set(0, 0, oneMinusS * rWeight + mSaturation); + satMatrix.set(0, 1, oneMinusS * rWeight); + satMatrix.set(0, 2, oneMinusS * rWeight); + satMatrix.set(1, 0, oneMinusS * gWeight); + satMatrix.set(1, 1, oneMinusS * gWeight + mSaturation); + satMatrix.set(1, 2, oneMinusS * gWeight); + satMatrix.set(2, 0, oneMinusS * bWeight); + satMatrix.set(2, 1, oneMinusS * bWeight); + satMatrix.set(2, 2, oneMinusS * bWeight + mSaturation); + mScriptR.set_colorMat(satMatrix); + mScriptF.set_colorMat(satMatrix); + } + + public boolean onBar1Setup(SeekBar b, TextView t) { + b.setProgress(50); + t.setText("Saturation"); + return true; + } + public boolean onBar2Setup(SeekBar b, TextView t) { + b.setMax(128); + b.setProgress(0); + t.setText("In Black"); + return true; + } + public boolean onBar3Setup(SeekBar b, TextView t) { + b.setMax(128); + b.setProgress(0); + t.setText("Out Black"); + return true; + } + public boolean onBar4Setup(SeekBar b, TextView t) { + b.setMax(128); + b.setProgress(128); + t.setText("Out White"); + return true; + } + public boolean onBar5Setup(SeekBar b, TextView t) { + b.setMax(128); + b.setProgress(128); + t.setText("Out White"); + return true; + } + + public void onBar1Changed(int progress) { + mSaturation = (float)progress / 50.0f; + setSaturation(); + } + public void onBar2Changed(int progress) { + mInBlack = (float)progress; + setLevels(); + } + public void onBar3Changed(int progress) { + mOutBlack = (float)progress; + setLevels(); + } + public void onBar4Changed(int progress) { + mInWhite = (float)progress + 127.0f; + setLevels(); + } + public void onBar5Changed(int progress) { + mOutWhite = (float)progress + 127.0f; + setLevels(); + } + + public void createTest(android.content.res.Resources res) { + mScriptR = new ScriptC_levels_relaxed(mRS, res, R.raw.levels_relaxed); + mScriptF = new ScriptC_levels_full(mRS, res, R.raw.levels_full); + setSaturation(); + setLevels(); + } + + public void runTest() { + if (mUseFull) { + if (mUseV4) { + mScriptF.forEach_root4(mInPixelsAllocation, mOutPixelsAllocation); + } else { + mScriptF.forEach_root(mInPixelsAllocation, mOutPixelsAllocation); + } + } else { + if (mUseV4) { + mScriptR.forEach_root4(mInPixelsAllocation, mOutPixelsAllocation); + } else { + mScriptR.forEach_root(mInPixelsAllocation, mOutPixelsAllocation); + } + } + } + +} + diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/TestBase.java b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/TestBase.java new file mode 100644 index 000000000000..3a6241d9d9a5 --- /dev/null +++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/TestBase.java @@ -0,0 +1,122 @@ +/* + * 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. + */ + +package com.android.rs.image; + +import android.app.Activity; +import android.content.Context; +import android.os.Bundle; +import android.graphics.BitmapFactory; +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.renderscript.ScriptC; +import android.renderscript.RenderScript; +import android.renderscript.Type; +import android.renderscript.Allocation; +import android.renderscript.Element; +import android.renderscript.Script; +import android.view.SurfaceView; +import android.view.SurfaceHolder; +import android.widget.ImageView; +import android.widget.SeekBar; +import android.widget.TextView; +import android.view.View; +import android.util.Log; +import java.lang.Math; + +public class TestBase { + protected final String TAG = "Img"; + + protected RenderScript mRS; + protected Allocation mInPixelsAllocation; + protected Allocation mOutPixelsAllocation; + + // Override to use UI elements + public void onBar1Changed(int progress) { + } + public void onBar2Changed(int progress) { + } + public void onBar3Changed(int progress) { + } + public void onBar4Changed(int progress) { + } + public void onBar5Changed(int progress) { + } + + // Override to use UI elements + // Unused bars will be hidden. + public boolean onBar1Setup(SeekBar b, TextView t) { + b.setVisibility(View.INVISIBLE); + t.setVisibility(View.INVISIBLE); + return false; + } + public boolean onBar2Setup(SeekBar b, TextView t) { + b.setVisibility(View.INVISIBLE); + t.setVisibility(View.INVISIBLE); + return false; + } + public boolean onBar3Setup(SeekBar b, TextView t) { + b.setVisibility(View.INVISIBLE); + t.setVisibility(View.INVISIBLE); + return false; + } + public boolean onBar4Setup(SeekBar b, TextView t) { + b.setVisibility(View.INVISIBLE); + t.setVisibility(View.INVISIBLE); + return false; + } + public boolean onBar5Setup(SeekBar b, TextView t) { + b.setVisibility(View.INVISIBLE); + t.setVisibility(View.INVISIBLE); + return false; + } + + public final void createBaseTest(ImageProcessingActivity act, Bitmap b) { + mRS = RenderScript.create(act); + mInPixelsAllocation = Allocation.createFromBitmap(mRS, b, + Allocation.MipmapControl.MIPMAP_NONE, + Allocation.USAGE_SCRIPT); + mOutPixelsAllocation = Allocation.createFromBitmap(mRS, b, + Allocation.MipmapControl.MIPMAP_NONE, + Allocation.USAGE_SCRIPT); + createTest(act.getResources()); + } + + // Must override + public void createTest(android.content.res.Resources res) { + android.util.Log.e("img", "implement createTest"); + } + + // Must override + public void runTest() { + } + + public void finish() { + mRS.finish(); + } + + public void updateBitmap(Bitmap b) { + mOutPixelsAllocation.copyTo(b); + } + + // Override to configure specific benchmark config. + public void setupBenchmark() { + } + + // Override to reset after benchmark. + public void exitBenchmark() { + } +} diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/greyscale.rs b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/greyscale.rs new file mode 100644 index 000000000000..c420cac5d910 --- /dev/null +++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/greyscale.rs @@ -0,0 +1,30 @@ +/* + * 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 + +const static float3 gMonoMult = {0.299f, 0.587f, 0.114f}; + +void root(const uchar4 *v_in, uchar4 *v_out) { + float4 f4 = rsUnpackColor8888(*v_in); + + float3 mono = dot(f4.rgb, gMonoMult); + *v_out = rsPackColorTo8888(mono); +} + + diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/levels.rsh b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/levels.rsh new file mode 100644 index 000000000000..7c5d930f111c --- /dev/null +++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/levels.rsh @@ -0,0 +1,44 @@ +/* + * 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. + */ + +float inBlack; +float outBlack; +float inWMinInB; +float outWMinOutB; +float overInWMinInB; +rs_matrix3x3 colorMat; + +void root(const uchar4 *in, uchar4 *out, uint32_t x, uint32_t y) { + float3 pixel = convert_float4(in[0]).rgb; + pixel = rsMatrixMultiply(&colorMat, pixel); + pixel = clamp(pixel, 0.f, 255.f); + pixel = (pixel - inBlack) * overInWMinInB; + pixel = pixel * outWMinOutB + outBlack; + pixel = clamp(pixel, 0.f, 255.f); + out->xyz = convert_uchar3(pixel); + out->w = 0xff; +} + +void root4(const uchar4 *in, uchar4 *out, uint32_t x, uint32_t y) { + float4 pixel = convert_float4(in[0]); + pixel.rgb = rsMatrixMultiply(&colorMat, pixel.rgb); + pixel = clamp(pixel, 0.f, 255.f); + pixel = (pixel - inBlack) * overInWMinInB; + pixel = pixel * outWMinOutB + outBlack; + pixel = clamp(pixel, 0.f, 255.f); + out->xyzw = convert_uchar4(pixel); +} + diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/levels_full.rs b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/levels_full.rs new file mode 100644 index 000000000000..da6a29134b05 --- /dev/null +++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/levels_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 "levels.rsh" + diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/levels_relaxed.rs b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/levels_relaxed.rs new file mode 100644 index 000000000000..b1154455a26e --- /dev/null +++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/levels_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 "levels.rsh" + diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/threshold.rs b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/threshold.rs index d93238c2580c..77cd5be582a0 100644 --- a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/threshold.rs +++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/threshold.rs @@ -88,6 +88,6 @@ void filter() { fs.ain = rsGetAllocation(ScratchPixel2); rsForEach(vBlurScript, fs.ain, rsGetAllocation(OutPixel), &fs, sizeof(fs)); - rsSendToClientBlocking(CMD_FINISHED); + //rsSendToClientBlocking(CMD_FINISHED); } diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/vertical_blur.rs b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/vertical_blur.rs index a6da192f4059..60fd71bf6fd4 100644 --- a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/vertical_blur.rs +++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/vertical_blur.rs @@ -3,29 +3,9 @@ #include "ip.rsh" -static float inBlack; -static float outBlack; -static float inWhite; -static float outWhite; -static float3 gamma; static float saturation; - -static float inWMinInB; -static float outWMinOutB; -static float overInWMinInB; static rs_matrix3x3 colorMat; -void setLevels(float iBlk, float oBlk, float iWht, float oWht) { - inBlack = iBlk; - outBlack = oBlk; - inWhite = iWht; - outWhite = oWht; - - inWMinInB = inWhite - inBlack; - outWMinOutB = outWhite - outBlack; - overInWMinInB = 1.f / inWMinInB; -} - void setSaturation(float sat) { saturation = sat; @@ -52,10 +32,6 @@ void setSaturation(float sat) { rsMatrixSet(&colorMat, 2, 2, oneMinusS * bWeight + saturation); } -void setGamma(float g) { - gamma = (float3)g; -} - void root(uchar4 *out, const void *usrData, uint32_t x, uint32_t y) { const FilterStruct *fs = (const FilterStruct *)usrData; float3 blurredPixel = 0; @@ -76,12 +52,8 @@ void root(uchar4 *out, const void *usrData, uint32_t x, uint32_t y) { } float3 temp = rsMatrixMultiply(&colorMat, blurredPixel); - temp = (clamp(temp, 0.f, 255.f) - inBlack) * overInWMinInB; - if (gamma.x != 1.0f) - temp = pow(temp, (float3)gamma); - temp = clamp(temp * outWMinOutB + outBlack, 0.f, 255.f); - + temp = clamp(temp, 0.f, 255.f); out->xyz = convert_uchar3(temp); - //output->w = input->w; + out->w = 0xff; } -- 2.11.0