15ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni/* 25ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni * Copyright (C) 2012 The Android Open Source Project 35ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni * 45ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni * Licensed under the Apache License, Version 2.0 (the "License"); 55ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni * you may not use this file except in compliance with the License. 65ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni * You may obtain a copy of the License at 75ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni * 85ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni * http://www.apache.org/licenses/LICENSE-2.0 95ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni * 105ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni * Unless required by applicable law or agreed to in writing, software 115ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni * distributed under the License is distributed on an "AS IS" BASIS, 125ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 135ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni * See the License for the specific language governing permissions and 145ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni * limitations under the License. 155ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni */ 165ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 175ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Nipackage com.android.rs.sgtest; 185ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 195ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.app.Activity; 205ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.os.Bundle; 215ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.os.Handler; 225ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.os.Message; 235ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.graphics.BitmapFactory; 245ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.graphics.Bitmap; 255ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.graphics.Canvas; 265ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.view.SurfaceView; 275ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.widget.AdapterView; 285ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.widget.ArrayAdapter; 295ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.widget.ImageView; 305ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.widget.SeekBar; 315ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.widget.Spinner; 325ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.widget.TextView; 335ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.view.View; 345ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.util.Log; 355ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.renderscript.ScriptC; 365ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.renderscript.RenderScript; 375ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.renderscript.Type; 385ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.renderscript.Allocation; 395ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.renderscript.Element; 405ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.renderscript.Script; 415ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 425ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport android.os.Environment; 435ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport java.io.BufferedWriter; 445ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport java.io.File; 455ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport java.io.FileWriter; 465ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport java.io.IOException; 475ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Niimport java.util.ArrayList; 485ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 495ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Nipublic class ScriptGroupTestActivity extends Activity 505ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni implements SeekBar.OnSeekBarChangeListener { 515ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni private final String TAG = "Img"; 525ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni public final String RESULT_FILE = "image_processing_result.csv"; 535ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 545ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni RenderScript mRS; 555ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni Allocation mInPixelsAllocation; 565ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni Allocation mOutPixelsAllocation; 575ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 585ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni Bitmap mBitmapOut; 595ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 605ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni private Spinner mSpinner; 615ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 625ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni private TextView mBenchmarkResult; 635ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni private Spinner mModeSpinner; 645ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni private Spinner mTestSpinner1; 655ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni private Spinner mTestSpinner2; 665ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 675ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni private ImageView mDisplayView; 685ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 695ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni private boolean mDoingBenchmark; 705ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 715ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni private TestBase mTest; 725ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni private int mRunCount; 735ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 745ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni public void updateDisplay() { 755ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mHandler.sendMessage(Message.obtain()); 765ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 775ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 785ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni private Handler mHandler = new Handler() { 795ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni // Allow the filter to complete without blocking the UI 805ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni // thread. When the message arrives that the op is complete 815ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni // we will either mark completion or start a new filter if 825ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni // more work is ready. Either way, display the result. 835ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni @Override 845ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni public void handleMessage(Message msg) { 855ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni boolean doTest = false; 865ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni synchronized(this) { 875ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni if (mRS == null) { 885ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni return; 895ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 905ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mTest.updateBitmap(mBitmapOut); 915ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mDisplayView.invalidate(); 925ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni if (mRunCount > 0) { 935ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mRunCount--; 945ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni if (mRunCount > 0) { 955ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni doTest = true; 965ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 975ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 985ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 995ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni if (doTest) { 1005ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mTest.runTestSendMessage(); 1015ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 1025ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 1035ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 1045ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 1055ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni }; 1065ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 1075ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 1085ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 1095ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 1105ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni public void onStartTrackingTouch(SeekBar seekBar) { 1115ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 1125ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 1135ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni public void onStopTrackingTouch(SeekBar seekBar) { 1145ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 1155ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 1165ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni void changeTest(int pos1, int pos2, int mode) { 1175ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni if (mTest != null) { 1185ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mTest.destroy(); 1195ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 1205ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 1215ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni final int[] index = new int[] { pos1, pos2 }; 1225ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mTest = new Filters(mode, index); 1235ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 1245ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mTest.createBaseTest(this); 1255ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 1265ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mTest.runTest(); 1275ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni updateDisplay(); 1285ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mBenchmarkResult.setText("Result: not run"); 1295ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 1305ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 1315ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni String getFilterName(int pos) { 1325ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni return Filters.mFilterClasses[pos].getSimpleName(); 1335ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 1345ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 1355ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni String[] getFilterNames() { 1365ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni ArrayList<String> list = new ArrayList<String>(); 1375ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni final int n = Filters.mFilterClasses.length; 1385ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni for (int i = 0; i < n; i++) { 1395ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni list.add(getFilterName(i)); 1405ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 1415ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni return list.toArray(new String[0]); 1425ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 1435ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 1445ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni void setupTests() { 1455ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni String[] names = getFilterNames(); 1465ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mModeSpinner.setAdapter(new ArrayAdapter<String>( 1475ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni this, R.layout.spinner_layout, new String[] {"emulated", "native"})); 1485ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mTestSpinner1.setAdapter(new ArrayAdapter<String>( 1495ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni this, R.layout.spinner_layout, names)); 1505ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mTestSpinner2.setAdapter(new ArrayAdapter<String>( 1515ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni this, R.layout.spinner_layout, names)); 1525ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 1535ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 1545ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni private AdapterView.OnItemSelectedListener mModeSpinnerListener = 1555ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni new AdapterView.OnItemSelectedListener() { 1565ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 1575ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni changeTest( 1585ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mTestSpinner1.getSelectedItemPosition(), 1595ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mTestSpinner2.getSelectedItemPosition(), 1605ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni pos); 1615ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 1625ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 1635ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni public void onNothingSelected(AdapterView parent) { 1645ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 1655ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni }; 1665ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 1675ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni private AdapterView.OnItemSelectedListener mTestSpinner1Listener = 1685ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni new AdapterView.OnItemSelectedListener() { 1695ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 1705ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni changeTest(pos, mTestSpinner2.getSelectedItemPosition(), 1715ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mModeSpinner.getSelectedItemPosition()); 1725ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 1735ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 1745ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni public void onNothingSelected(AdapterView parent) { 1755ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 1765ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni }; 1775ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 1785ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni private AdapterView.OnItemSelectedListener mTestSpinner2Listener = 1795ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni new AdapterView.OnItemSelectedListener() { 1805ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 1815ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni changeTest(mTestSpinner1.getSelectedItemPosition(), pos, 1825ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mModeSpinner.getSelectedItemPosition()); 1835ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 1845ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 1855ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni public void onNothingSelected(AdapterView parent) { 1865ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 1875ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni }; 1885ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 1895ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni void init() { 1905ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mRS = RenderScript.create(this); 1915ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mInPixelsAllocation = Allocation.createFromBitmapResource( 1925ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mRS, getResources(), R.drawable.img1600x1067); 1935ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mBitmapOut = Bitmap.createBitmap(mInPixelsAllocation.getType().getX(), 1945ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mInPixelsAllocation.getType().getY(), 1955ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni Bitmap.Config.ARGB_8888); 1965ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mOutPixelsAllocation = Allocation.createFromBitmap(mRS, mBitmapOut); 1975ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 1985ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mDisplayView = (ImageView) findViewById(R.id.display); 1995ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mDisplayView.setImageBitmap(mBitmapOut); 2005ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 2015ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mModeSpinner = (Spinner) findViewById(R.id.modeselection); 2025ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mModeSpinner.setOnItemSelectedListener(mModeSpinnerListener); 2035ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mTestSpinner1 = (Spinner) findViewById(R.id.filterselection); 2045ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mTestSpinner1.setOnItemSelectedListener(mTestSpinner1Listener); 2055ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mTestSpinner2 = (Spinner) findViewById(R.id.filter2selection); 2065ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mTestSpinner2.setOnItemSelectedListener(mTestSpinner2Listener); 2075ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 2085ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mBenchmarkResult = (TextView) findViewById(R.id.benchmarkText); 2095ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mBenchmarkResult.setText("Result: not run"); 2105ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 2115ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni setupTests(); 2125ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni changeTest(0, 0, 0); 2135ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 2145ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 2155ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni void cleanup() { 2165ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni synchronized(this) { 2175ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni RenderScript rs = mRS; 2185ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mRS = null; 2195ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni while(mDoingBenchmark) { 2205ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni try { 2215ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni Thread.sleep(1, 0); 2225ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } catch(InterruptedException e) { 2235ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 2245ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 2255ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 2265ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni rs.destroy(); 2275ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 2285ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 2295ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mInPixelsAllocation = null; 2305ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mOutPixelsAllocation = null; 2315ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mBitmapOut = null; 2325ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 2335ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 2345ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni @Override 2355ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni protected void onCreate(Bundle savedInstanceState) { 2365ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni super.onCreate(savedInstanceState); 2375ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni setContentView(R.layout.main); 2385ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 2395ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni init(); 2405ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 2415ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 2425ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni @Override 2435ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni protected void onPause() { 2445ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni super.onPause(); 2455ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 2465ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni cleanup(); 2475ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 2485ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 2495ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 2505ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni @Override 2515ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni protected void onResume() { 2525ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni super.onResume(); 2535ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 2545ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni if (null == mRS) { 2555ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni init(); 2565ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 2575ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 2585ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 2595ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni // button hook 2605ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni public void benchmark(View v) { 2615ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni float t = getBenchmark(); 2625ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni //long javaTime = javaFilter(); 2635ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni //mBenchmarkResult.setText("RS: " + t + " ms Java: " + javaTime + " ms"); 2645ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mBenchmarkResult.setText("Result: " + t + " ms"); 2655ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni Log.v(TAG, "getBenchmark: Renderscript frame time core ms " + t); 2665ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 2675ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 2685ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni public void benchmark_all(View v) { 2695ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni // write result into a file 2705ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni File externalStorage = Environment.getExternalStorageDirectory(); 2715ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni if (!externalStorage.canWrite()) { 2725ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni Log.v(TAG, "sdcard is not writable"); 2735ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni return; 2745ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 2755ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni File resultFile = new File(externalStorage, RESULT_FILE); 2765ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni resultFile.setWritable(true, false); 2775ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni try { 2785ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni BufferedWriter rsWriter = new BufferedWriter(new FileWriter(resultFile)); 2795ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni Log.v(TAG, "Saved results in: " + resultFile.getAbsolutePath()); 2805ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni final int n = Filters.mFilterClasses.length; 2815ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni for (int i = 0; i < n; i++) { 2825ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni for (int j = 0; j < n; j++) { 2835ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni for (int k = 0; k < 2; k++) { 2845ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni changeTest(i, j, k); 2855ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni float t = getBenchmark(); 2865ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni String tn = getFilterName(i) + "-" + getFilterName(j); 2875ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni if (k == 0) { 2885ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni tn += " (emulated)"; 2895ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } else { 2905ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni tn += " (native)"; 2915ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 2925ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni String s = new String("" + tn.toString() + ", " + t); 2935ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni rsWriter.write(s + "\n"); 2945ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni Log.v(TAG, "Test " + s + "ms\n"); 2955ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 2965ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 2975ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 2985ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni rsWriter.close(); 2995ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } catch (IOException e) { 3005ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni Log.v(TAG, "Unable to write result file " + e.getMessage()); 3015ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 3025ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni changeTest(0, 0, 0); 3035ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni Log.v(TAG, "result file:"+resultFile.getAbsolutePath()); 3045ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 3055ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 3065ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 3075ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 3085ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni // For benchmark test 3095ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni public float getBenchmark() { 3105ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni if (mRS == null) { 3115ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni return 0; 3125ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 3135ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mDoingBenchmark = true; 3145ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 3155ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mTest.setupBenchmark(); 3165ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni long result = 0; 3175ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 3185ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni //Log.v(TAG, "Warming"); 3195ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni long t = java.lang.System.currentTimeMillis() + 250; 3205ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni do { 3215ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mTest.runTest(); 3225ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mTest.finish(); 3235ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } while (t > java.lang.System.currentTimeMillis()); 3245ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 3255ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni //Log.v(TAG, "Benchmarking"); 3265ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni int ct = 0; 3275ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni t = java.lang.System.currentTimeMillis(); 3285ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni do { 3295ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mTest.runTest(); 3305ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mTest.finish(); 3315ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni ct++; 3325ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } while ((t+1000) > java.lang.System.currentTimeMillis()); 3335ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni t = java.lang.System.currentTimeMillis() - t; 3345ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni float ft = (float)t; 3355ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni ft /= ct; 3365ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 3375ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mTest.exitBenchmark(); 3385ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni mDoingBenchmark = false; 3395ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni 3405ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni return ft; 3415ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni } 3425ab5155f7e0e0904f8c11cd0dbdbf7832e3ac948Yang Ni} 343