1/*
2 * Copyright (C) 2013 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
17package com.android.rs.image2;
18
19import android.view.Menu;
20import android.view.MenuItem;
21import android.view.MenuInflater;
22
23import android.app.Activity;
24import android.os.Bundle;
25import android.os.Handler;
26import android.graphics.Point;
27import android.view.SurfaceView;
28import android.widget.AdapterView;
29import android.widget.ArrayAdapter;
30import android.widget.ImageView;
31import android.widget.SeekBar;
32import android.widget.Spinner;
33import android.widget.ToggleButton;
34import android.widget.TextView;
35import android.widget.CompoundButton;
36import android.widget.ListView;
37import android.view.View;
38import java.util.ArrayList;
39import java.util.ListIterator;
40import android.util.Log;
41import android.content.Intent;
42
43import android.os.Environment;
44import java.io.BufferedWriter;
45import java.io.File;
46import java.io.FileWriter;
47import java.io.IOException;
48
49public class IPControls extends Activity {
50    private final String TAG = "Img";
51    public final String RESULT_FILE = "ip_compat_result.csv";
52
53    private Spinner mResolutionSpinner;
54    private ListView mTestListView;
55    private TextView mResultView;
56
57    private ArrayAdapter<String> mTestListAdapter;
58    private ArrayList<String> mTestList = new ArrayList<String>();
59
60    private boolean mSettings[] = {true, true, false, false, false};
61    private static final int SETTING_ANIMATE = 0;
62    private static final int SETTING_DISPLAY = 1;
63    private static final int SETTING_LONG_RUN = 2;
64    private static final int SETTING_PAUSE = 3;
65    private static final int SETTING_USAGE_IO = 4;
66
67    private float mResults[];
68
69    public enum Resolutions {
70        RES_1080P(1920, 1080, "1080p (1920x1080)"),
71        RES_720P(1280, 720, "720p (1280x720)"),
72        RES_WVGA(800, 480, "WVGA (800x480)");
73
74        private final String name;
75        public final int width;
76        public final int height;
77
78        private Resolutions(int w, int h, String s) {
79            width = w;
80            height = h;
81            name = s;
82        }
83
84        // return quoted string as displayed test name
85        public String toString() {
86            return name;
87        }
88    }
89    private Resolutions mResolution;
90
91    @Override
92    public boolean onCreateOptionsMenu(Menu menu) {
93        // Inflate the menu items for use in the action bar
94        MenuInflater inflater = getMenuInflater();
95        inflater.inflate(R.menu.main_activity_actions, menu);
96
97        MenuItem searchItem = menu.findItem(R.id.action_res);
98        mResolutionSpinner = (Spinner) searchItem.getActionView();
99
100        mResolutionSpinner.setOnItemSelectedListener(mResolutionSpinnerListener);
101        mResolutionSpinner.setAdapter(new ArrayAdapter<Resolutions>(
102            this, R.layout.spinner_layout, Resolutions.values()));
103
104        // Choose one of the image sizes that is close to the resolution
105        // of the screen.
106        Point size = new Point();
107        getWindowManager().getDefaultDisplay().getSize(size);
108        boolean didSet = false;
109        int md = (size.x > size.y) ? size.x : size.y;
110        for (int ct=0; ct < Resolutions.values().length; ct++) {
111            if (Resolutions.values()[ct].width <= (int)(md * 1.2)) {
112                mResolutionSpinner.setSelection(ct);
113                didSet = true;
114                break;
115            }
116        }
117        if (!didSet) {
118            // If no good resolution was found, pick the lowest one.
119            mResolutionSpinner.setSelection(Resolutions.values().length - 1);
120        }
121
122        return super.onCreateOptionsMenu(menu);
123    }
124
125
126    private AdapterView.OnItemSelectedListener mResolutionSpinnerListener =
127            new AdapterView.OnItemSelectedListener() {
128                public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
129                    mResolution = Resolutions.values()[pos];
130                }
131
132                public void onNothingSelected(AdapterView parent) {
133                }
134            };
135
136    void launchDemo(int id) {
137        int testList[] = new int[1];
138        testList[0] = id;
139
140        Intent intent = makeBasicLaunchIntent();
141        intent.putExtra("tests", testList);
142        intent.putExtra("demo", true);
143        startActivityForResult(intent, 0);
144    }
145
146    void init() {
147
148        for (int i=0; i < IPTestList.TestName.values().length; i++) {
149            mTestList.add(IPTestList.TestName.values()[i].toString());
150        }
151
152        mTestListView = (ListView) findViewById(R.id.test_list);
153        mTestListAdapter = new ArrayAdapter(this,
154                android.R.layout.simple_list_item_activated_1,
155                mTestList);
156
157        mTestListView.setAdapter(mTestListAdapter);
158        mTestListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
159        mTestListAdapter.notifyDataSetChanged();
160
161        mResultView = (TextView) findViewById(R.id.results);
162
163        mTestListView.setOnItemLongClickListener(new ListView.OnItemLongClickListener() {
164                public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
165                        int pos, long id) {
166                    launchDemo(pos);
167                    return true;
168                }
169            });
170    }
171
172    @Override
173    protected void onCreate(Bundle savedInstanceState) {
174        super.onCreate(savedInstanceState);
175        setContentView(R.layout.controls);
176        init();
177    }
178
179    @Override
180    protected void onPause() {
181        super.onPause();
182    }
183
184
185    @Override
186    protected void onResume() {
187        super.onResume();
188    }
189
190    private void checkGroup(int group) {
191        IPTestList.TestName t[] = IPTestList.TestName.values();
192        for (int i=0; i < t.length; i++) {
193            mTestListView.setItemChecked(i, group == t[i].group);
194        }
195    }
196
197    Intent makeBasicLaunchIntent() {
198        Intent intent = new Intent(this, ImageProcessingActivity2.class);
199        intent.putExtra("enable long", mSettings[SETTING_LONG_RUN]);
200        intent.putExtra("enable pause", mSettings[SETTING_PAUSE]);
201        intent.putExtra("enable animate", mSettings[SETTING_ANIMATE]);
202        intent.putExtra("enable display", mSettings[SETTING_DISPLAY]);
203        intent.putExtra("enable io", mSettings[SETTING_USAGE_IO]);
204        intent.putExtra("resolution X", mResolution.width);
205        intent.putExtra("resolution Y", mResolution.height);
206        return intent;
207    }
208
209    public void btnRun(View v) {
210        IPTestList.TestName t[] = IPTestList.TestName.values();
211
212        int count = 0;
213        for (int i = 0; i < t.length; i++) {
214            if (mTestListView.isItemChecked(i)) {
215                count++;
216            }
217        }
218        if (count == 0) {
219            return;
220        }
221
222        int testList[] = new int[count];
223        count = 0;
224        for (int i = 0; i < t.length; i++) {
225            if (mTestListView.isItemChecked(i)) {
226                testList[count++] = i;
227            }
228        }
229
230        Intent intent = makeBasicLaunchIntent();
231        intent.putExtra("tests", testList);
232        startActivityForResult(intent, 0);
233    }
234
235    // Rebase normalizes the performance result for the resolution and
236    // compares it to a baseline device.
237    float rebase(float v, IPTestList.TestName t) {
238        if (v > 0.001) {
239            v = t.baseline / v;
240        }
241        float pr = (1920.f / mResolution.width) * (1080.f / mResolution.height);
242        return v / pr;
243    }
244
245    private void writeResults() {
246        // write result into a file
247        File externalStorage = Environment.getExternalStorageDirectory();
248        if (!externalStorage.canWrite()) {
249            Log.v(TAG, "sdcard is not writable");
250            return;
251        }
252        File resultFile = new File(externalStorage, RESULT_FILE);
253        resultFile.setWritable(true, false);
254        try {
255            BufferedWriter rsWriter = new BufferedWriter(new FileWriter(resultFile));
256            Log.v(TAG, "Saved results in: " + resultFile.getAbsolutePath());
257            java.text.DecimalFormat df = new java.text.DecimalFormat("######.##");
258
259            for (int ct=0; ct < IPTestList.TestName.values().length; ct++) {
260                IPTestList.TestName t = IPTestList.TestName.values()[ct];
261                final float r = mResults[ct];
262                float r2 = rebase(r, t);
263                String s = new String("" + t.toString() + ", " + df.format(r) + ", " + df.format(r2));
264                rsWriter.write(s + "\n");
265            }
266            rsWriter.close();
267        } catch (IOException e) {
268            Log.v(TAG, "Unable to write result file " + e.getMessage());
269        }
270    }
271
272    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
273        if (requestCode == 0) {
274            if (resultCode == RESULT_OK) {
275                java.text.DecimalFormat df = new java.text.DecimalFormat("######.#");
276                mResults = new float[IPTestList.TestName.values().length];
277
278                float r[] = data.getFloatArrayExtra("results");
279                int id[] = data.getIntArrayExtra("tests");
280
281                for (int ct=0; ct < id.length; ct++) {
282                    IPTestList.TestName t = IPTestList.TestName.values()[id[ct]];
283
284                    String s = t.toString() + "   " + df.format(rebase(r[ct], t)) +
285                            "X,   " + df.format(r[ct]) + "ms";
286                    mTestList.set(id[ct], s);
287                    mTestListAdapter.notifyDataSetChanged();
288                    mResults[id[ct]] = r[ct];
289                }
290
291                double geometricMean[] = {1.0, 1.0, 1.0};
292                double count[] = {0, 0, 0};
293                for (int ct=0; ct < IPTestList.TestName.values().length; ct++) {
294                    IPTestList.TestName t = IPTestList.TestName.values()[ct];
295                    geometricMean[t.group] *= rebase(mResults[ct], t);
296                    count[t.group] += 1.0;
297                }
298                geometricMean[0] = java.lang.Math.pow(geometricMean[0], 1.0 / count[0]);
299                geometricMean[1] = java.lang.Math.pow(geometricMean[1], 1.0 / count[1]);
300                geometricMean[2] = java.lang.Math.pow(geometricMean[2], 1.0 / count[2]);
301
302                String s = "Results:  fp full=" + df.format(geometricMean[0]) +
303                        ",  fp relaxed=" +df.format(geometricMean[1]) +
304                        ",  intrinsics=" + df.format(geometricMean[2]);
305                mResultView.setText(s);
306                writeResults();
307            }
308        }
309    }
310
311    public boolean onOptionsItemSelected(MenuItem item) {
312        // Handle presses on the action bar items
313        switch(item.getItemId()) {
314            case R.id.action_settings:
315                IPSettings newFragment = new IPSettings(mSettings);
316                newFragment.show(getFragmentManager(), "settings");
317                return true;
318            default:
319                return super.onOptionsItemSelected(item);
320        }
321    }
322
323    public void btnSelAll(View v) {
324        IPTestList.TestName t[] = IPTestList.TestName.values();
325        for (int i=0; i < t.length; i++) {
326            mTestListView.setItemChecked(i, true);
327        }
328    }
329
330    public void btnSelNone(View v) {
331        checkGroup(-1);
332    }
333
334    public void btnSettings(View v) {
335        IPSettings newFragment = new IPSettings(mSettings);
336        newFragment.show(getFragmentManager(), "settings");
337    }
338
339    public void btnSelIntrinsic(View v) {
340        checkGroup(IPTestList.INTRINSIC);
341    }
342
343
344
345}
346