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