Chris Craik | c3683b5 | 2012-10-01 18:22:38 -0700 | [diff] [blame] | 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 | package com.android.test.hwuicompare; |
| 18 | |
| 19 | import java.util.ArrayList; |
| 20 | |
| 21 | import com.android.test.hwuicompare.R; |
| 22 | |
| 23 | import android.os.Bundle; |
| 24 | import android.os.Trace; |
| 25 | import android.util.Log; |
| 26 | import android.widget.ImageView; |
| 27 | import android.widget.Toast; |
| 28 | |
| 29 | public class AutomaticActivity extends CompareActivity { |
| 30 | private static final String LOG_TAG = "AutomaticActivity"; |
| 31 | private static final float ERROR_DISPLAY_THRESHOLD = 0.01f; |
| 32 | protected static final boolean DRAW_BITMAPS = false; |
| 33 | |
| 34 | private ImageView mSoftwareImageView = null; |
| 35 | private ImageView mHardwareImageView = null; |
| 36 | |
| 37 | private static final float[] ERROR_CUTOFFS = {0, 0.005f, 0.01f, 0.02f, 0.05f, 0.1f, 0.25f, 0.5f, 1f, 2f}; |
| 38 | private float[] mErrorRates = new float[ERROR_CUTOFFS.length]; |
| 39 | private float mTotalTests = 0; |
| 40 | private float mTotalError = 0; |
| 41 | |
| 42 | public abstract static class TestCallback { |
| 43 | abstract void report(String name, float value); |
| 44 | void complete() {} |
| 45 | } |
| 46 | |
| 47 | private ArrayList<TestCallback> mTestCallbacks = new ArrayList<TestCallback>(); |
| 48 | |
| 49 | Runnable mRunnable = new Runnable() { |
| 50 | @Override |
| 51 | public void run() { |
| 52 | loadBitmaps(); |
| 53 | if (mSoftwareBitmap == null || mHardwareBitmap == null) { |
| 54 | Log.e(LOG_TAG, "bitmap is null"); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | if (DRAW_BITMAPS) { |
| 59 | mSoftwareImageView.setImageBitmap(mSoftwareBitmap); |
| 60 | mHardwareImageView.setImageBitmap(mHardwareBitmap); |
| 61 | } |
| 62 | |
| 63 | Trace.traceBegin(Trace.TRACE_TAG_ALWAYS, "calculateError"); |
| 64 | float error = mErrorCalculator.calcErrorRS(mSoftwareBitmap, mHardwareBitmap); |
| 65 | Trace.traceEnd(Trace.TRACE_TAG_ALWAYS); |
| 66 | |
| 67 | if (error > ERROR_DISPLAY_THRESHOLD) { |
| 68 | String modname = ""; |
| 69 | for (String s : DisplayModifier.getLastAppliedModifications()) { |
| 70 | modname = modname.concat(s + "."); |
| 71 | } |
| 72 | Log.d(LOG_TAG, String.format("error for %s was %2.9f", modname, error)); |
| 73 | } |
| 74 | for (int i = 0; i < ERROR_CUTOFFS.length; i++) { |
| 75 | if (error <= ERROR_CUTOFFS[i]) break; |
| 76 | mErrorRates[i]++; |
| 77 | } |
| 78 | mTotalError += error; |
| 79 | mTotalTests++; |
| 80 | |
| 81 | if (DisplayModifier.step()) { |
| 82 | for (TestCallback c : mTestCallbacks) { |
| 83 | c.report("averageError", (mTotalError / mTotalTests)); |
| 84 | for (int i = 1; i < ERROR_CUTOFFS.length; i++) { |
| 85 | c.report(String.format("error over %1.3f", ERROR_CUTOFFS[i]), |
| 86 | mErrorRates[i]/mTotalTests); |
| 87 | } |
| 88 | c.complete(); |
| 89 | } |
| 90 | |
| 91 | Toast.makeText(getApplicationContext(), "done!", Toast.LENGTH_SHORT).show(); |
| 92 | finish(); |
| 93 | } else { |
| 94 | mHardwareView.invalidate(); |
| 95 | if (DRAW_BITMAPS) { |
| 96 | mSoftwareImageView.invalidate(); |
| 97 | mHardwareImageView.invalidate(); |
| 98 | } |
| 99 | } |
| 100 | mHandler.removeCallbacks(mRunnable); |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | @Override |
| 105 | protected void onPause() { |
| 106 | super.onPause(); |
| 107 | mHandler.removeCallbacks(mRunnable); |
| 108 | }; |
| 109 | |
| 110 | @Override |
| 111 | protected void onCreate(Bundle savedInstanceState) { |
| 112 | super.onCreate(savedInstanceState); |
| 113 | setContentView(R.layout.automatic_layout); |
| 114 | |
| 115 | mSoftwareImageView = (ImageView) findViewById(R.id.software_image_view); |
| 116 | mHardwareImageView = (ImageView) findViewById(R.id.hardware_image_view); |
| 117 | |
| 118 | onCreateCommon(mRunnable); |
| 119 | mTestCallbacks.add(new TestCallback() { |
| 120 | void report(String name, float value) { |
| 121 | Log.d(LOG_TAG, name + " " + value); |
| 122 | }; |
| 123 | }); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | protected boolean forceRecreateBitmaps() { |
| 128 | // disable, unless needed for drawing into imageviews |
| 129 | return DRAW_BITMAPS; |
| 130 | } |
| 131 | |
| 132 | // FOR TESTING |
| 133 | public void setCallback(TestCallback c) { |
| 134 | mTestCallbacks.add(c); |
| 135 | } |
| 136 | } |