ImageProcessingActivity.java revision dbad8eb5a8bb16488351c5236974812d282b7b82
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
17package com.android.rs.image;
18
19import android.app.Activity;
20import android.os.Bundle;
21import android.os.Handler;
22import android.os.Message;
23import android.graphics.BitmapFactory;
24import android.graphics.Bitmap;
25import android.graphics.Canvas;
26import android.view.SurfaceView;
27import android.widget.AdapterView;
28import android.widget.ArrayAdapter;
29import android.widget.ImageView;
30import android.widget.SeekBar;
31import android.widget.Spinner;
32import android.widget.TextView;
33import android.view.View;
34import android.util.Log;
35import android.renderscript.ScriptC;
36import android.renderscript.RenderScript;
37import android.renderscript.Type;
38import android.renderscript.Allocation;
39import android.renderscript.Element;
40import android.renderscript.Script;
41
42import android.os.Environment;
43import java.io.BufferedWriter;
44import java.io.File;
45import java.io.FileWriter;
46import java.io.IOException;
47
48public class ImageProcessingActivity extends Activity
49                                       implements SeekBar.OnSeekBarChangeListener {
50    private final String TAG = "Img";
51    public final String RESULT_FILE = "image_processing_result.csv";
52
53    RenderScript mRS;
54    Allocation mInPixelsAllocation;
55    Allocation mInPixelsAllocation2;
56    Allocation mOutPixelsAllocation;
57
58    static class DVFSWorkaround {
59        static class spinner extends Thread {
60            boolean mRun = true;
61            long mNextSleep;
62
63            spinner() {
64                setPriority(MIN_PRIORITY);
65                start();
66            }
67
68            public void run() {
69                while (mRun) {
70                    Thread.yield();
71                    synchronized(this) {
72                        long t = java.lang.System.currentTimeMillis();
73                        if (t > mNextSleep) {
74                            try {
75                                this.wait();
76                            } catch(InterruptedException e) {
77                            }
78                        }
79                    }
80                }
81            }
82
83            public void go(long t) {
84                synchronized(this) {
85                    mNextSleep = t;
86                    notifyAll();
87                }
88            }
89        }
90
91        spinner s1;
92        DVFSWorkaround() {
93            s1 = new spinner();
94        }
95
96        void go() {
97            long t = java.lang.System.currentTimeMillis() + 2000;
98            s1.go(t);
99        }
100
101        void destroy() {
102            synchronized(this) {
103                s1.mRun = false;
104                notifyAll();
105            }
106        }
107    }
108    DVFSWorkaround mDvfsWar = new DVFSWorkaround();
109
110
111    /**
112     * Define enum type for test names
113     */
114    public enum TestName {
115        // totally there are 38 test cases
116        LEVELS_VEC3_RELAXED ("Levels Vec3 Relaxed"),
117        LEVELS_VEC4_RELAXED ("Levels Vec4 Relaxed"),
118        LEVELS_VEC3_FULL ("Levels Vec3 Full"),
119        LEVELS_VEC4_FULL ("Levels Vec4 Full"),
120        BLUR_RADIUS_25 ("Blur radius 25"),
121        INTRINSIC_BLUE_RADIUS_25 ("Intrinsic Blur radius 25"),
122        GREYSCALE ("Greyscale"),
123        GRAIN ("Grain"),
124        FISHEYE_FULL ("Fisheye Full"),
125        FISHEYE_RELAXED ("Fisheye Relaxed"),
126        FISHEYE_APPROXIMATE_FULL ("Fisheye Approximate Full"),
127        FISHEYE_APPROXIMATE_RELAXED ("Fisheye Approximate Relaxed"),
128        VIGNETTE_FULL ("Vignette Full"),
129        VIGNETTE_RELAXED ("Vignette Relaxed"),
130        VIGNETTE_APPROXIMATE_FULL ("Vignette Approximate Full"),
131        VIGNETTE_APPROXIMATE_RELAXED ("Vignette Approximate Relaxed"),
132        GROUP_TEST_EMULATED ("Group Test (emulated)"),
133        GROUP_TEST_NATIVE ("Group Test (native)"),
134        CONVOLVE_3X3 ("Convolve 3x3"),
135        INTRINSICS_CONVOLVE_3X3 ("Intrinsics Convolve 3x3"),
136        COLOR_MATRIX ("ColorMatrix"),
137        INTRINSICS_COLOR_MATRIX ("Intrinsics ColorMatrix"),
138        INTRINSICS_COLOR_MATRIX_GREY ("Intrinsics ColorMatrix Grey"),
139        COPY ("Copy"),
140        CROSS_PROCESS_USING_LUT ("CrossProcess (using LUT)"),
141        CONVOLVE_5X5 ("Convolve 5x5"),
142        INTRINSICS_CONVOLVE_5X5 ("Intrinsics Convolve 5x5"),
143        MANDELBROT ("Mandelbrot"),
144        INTRINSICS_BLEND ("Intrinsics Blend"),
145        INTRINSICS_BLUR_25G ("Intrinsics Blur 25 uchar"),
146        VIBRANCE ("Vibrance"),
147        BW_FILTER ("BW Filter"),
148        SHADOWS ("Shadows"),
149        CONTRAST ("Contrast"),
150        EXPOSURE ("Exposure"),
151        WHITE_BALANCE ("White Balance"),
152        COLOR_CUBE ("Color Cube"),
153        COLOR_CUBE_3D_INTRINSIC ("Color Cube (3D LUT intrinsic)"),
154        USAGE_IO ("Usage io"),
155        ARTISTIC_1("Artistic 1"),
156        HISTOGRAM ("Histogram");
157
158
159        private final String name;
160
161        private TestName(String s) {
162            name = s;
163        }
164
165        // return quoted string as displayed test name
166        public String toString() {
167            return name;
168        }
169    }
170
171    Bitmap mBitmapIn;
172    Bitmap mBitmapIn2;
173    Bitmap mBitmapOut;
174
175    private Spinner mSpinner;
176    private SeekBar mBar1;
177    private SeekBar mBar2;
178    private SeekBar mBar3;
179    private SeekBar mBar4;
180    private SeekBar mBar5;
181    private TextView mText1;
182    private TextView mText2;
183    private TextView mText3;
184    private TextView mText4;
185    private TextView mText5;
186
187    private TextView mBenchmarkResult;
188    private Spinner mTestSpinner;
189
190    private ImageView mDisplayView;
191
192    private boolean mDoingBenchmark;
193
194    private TestBase mTest;
195    private int mRunCount;
196
197    public void updateDisplay() {
198        mHandler.sendMessage(Message.obtain());
199    }
200
201    private Handler mHandler = new Handler() {
202        // Allow the filter to complete without blocking the UI
203        // thread.  When the message arrives that the op is complete
204        // we will either mark completion or start a new filter if
205        // more work is ready.  Either way, display the result.
206        @Override
207        public void handleMessage(Message msg) {
208            boolean doTest = false;
209            synchronized(this) {
210                if (mRS == null) {
211                    return;
212                }
213                mTest.updateBitmap(mBitmapOut);
214                mDisplayView.invalidate();
215                if (mRunCount > 0) {
216                    mRunCount--;
217                    if (mRunCount > 0) {
218                        doTest = true;
219                    }
220                }
221
222                if (doTest) {
223                    mTest.runTestSendMessage();
224                }
225            }
226        }
227
228    };
229
230    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
231        if (fromUser) {
232
233            if (seekBar == mBar1) {
234                mTest.onBar1Changed(progress);
235            } else if (seekBar == mBar2) {
236                mTest.onBar2Changed(progress);
237            } else if (seekBar == mBar3) {
238                mTest.onBar3Changed(progress);
239            } else if (seekBar == mBar4) {
240                mTest.onBar4Changed(progress);
241            } else if (seekBar == mBar5) {
242                mTest.onBar5Changed(progress);
243            }
244
245            boolean doTest = false;
246            synchronized(this) {
247                if (mRunCount == 0) {
248                    doTest = true;
249                    mRunCount = 1;
250                } else {
251                    mRunCount = 2;
252                }
253            }
254            if (doTest) {
255                mTest.runTestSendMessage();
256            }
257        }
258    }
259
260    public void onStartTrackingTouch(SeekBar seekBar) {
261    }
262
263    public void onStopTrackingTouch(SeekBar seekBar) {
264    }
265
266    void setupBars() {
267        mSpinner.setVisibility(View.VISIBLE);
268        mTest.onSpinner1Setup(mSpinner);
269
270        mBar1.setVisibility(View.VISIBLE);
271        mText1.setVisibility(View.VISIBLE);
272        mTest.onBar1Setup(mBar1, mText1);
273
274        mBar2.setVisibility(View.VISIBLE);
275        mText2.setVisibility(View.VISIBLE);
276        mTest.onBar2Setup(mBar2, mText2);
277
278        mBar3.setVisibility(View.VISIBLE);
279        mText3.setVisibility(View.VISIBLE);
280        mTest.onBar3Setup(mBar3, mText3);
281
282        mBar4.setVisibility(View.VISIBLE);
283        mText4.setVisibility(View.VISIBLE);
284        mTest.onBar4Setup(mBar4, mText4);
285
286        mBar5.setVisibility(View.VISIBLE);
287        mText5.setVisibility(View.VISIBLE);
288        mTest.onBar5Setup(mBar5, mText5);
289    }
290
291
292    void changeTest(TestName testName) {
293        if (mTest != null) {
294            mTest.destroy();
295        }
296        switch(testName) {
297        case LEVELS_VEC3_RELAXED:
298            mTest = new LevelsV4(false, false);
299            break;
300        case LEVELS_VEC4_RELAXED:
301            mTest = new LevelsV4(false, true);
302            break;
303        case LEVELS_VEC3_FULL:
304            mTest = new LevelsV4(true, false);
305            break;
306        case LEVELS_VEC4_FULL:
307            mTest = new LevelsV4(true, true);
308            break;
309        case BLUR_RADIUS_25:
310            mTest = new Blur25(false);
311            break;
312        case INTRINSIC_BLUE_RADIUS_25:
313            mTest = new Blur25(true);
314            break;
315        case GREYSCALE:
316            mTest = new Greyscale();
317            break;
318        case GRAIN:
319            mTest = new Grain();
320            break;
321        case FISHEYE_FULL:
322            mTest = new Fisheye(false, false);
323            break;
324        case FISHEYE_RELAXED:
325            mTest = new Fisheye(false, true);
326            break;
327        case FISHEYE_APPROXIMATE_FULL:
328            mTest = new Fisheye(true, false);
329            break;
330        case FISHEYE_APPROXIMATE_RELAXED:
331            mTest = new Fisheye(true, true);
332            break;
333        case VIGNETTE_FULL:
334            mTest = new Vignette(false, false);
335            break;
336        case VIGNETTE_RELAXED:
337            mTest = new Vignette(false, true);
338            break;
339        case VIGNETTE_APPROXIMATE_FULL:
340            mTest = new Vignette(true, false);
341            break;
342        case VIGNETTE_APPROXIMATE_RELAXED:
343            mTest = new Vignette(true, true);
344            break;
345        case GROUP_TEST_EMULATED:
346            mTest = new GroupTest(false);
347            break;
348        case GROUP_TEST_NATIVE:
349            mTest = new GroupTest(true);
350            break;
351        case CONVOLVE_3X3:
352            mTest = new Convolve3x3(false);
353            break;
354        case INTRINSICS_CONVOLVE_3X3:
355            mTest = new Convolve3x3(true);
356            break;
357        case COLOR_MATRIX:
358            mTest = new ColorMatrix(false, false);
359            break;
360        case INTRINSICS_COLOR_MATRIX:
361            mTest = new ColorMatrix(true, false);
362            break;
363        case INTRINSICS_COLOR_MATRIX_GREY:
364            mTest = new ColorMatrix(true, true);
365            break;
366        case COPY:
367            mTest = new Copy();
368            break;
369        case CROSS_PROCESS_USING_LUT:
370            mTest = new CrossProcess();
371            break;
372        case CONVOLVE_5X5:
373            mTest = new Convolve5x5(false);
374            break;
375        case INTRINSICS_CONVOLVE_5X5:
376            mTest = new Convolve5x5(true);
377            break;
378        case MANDELBROT:
379            mTest = new Mandelbrot();
380            break;
381        case INTRINSICS_BLEND:
382            mTest = new Blend();
383            break;
384        case INTRINSICS_BLUR_25G:
385            mTest = new Blur25G();
386            break;
387        case VIBRANCE:
388            mTest = new Vibrance();
389            break;
390        case BW_FILTER:
391            mTest = new BWFilter();
392            break;
393        case SHADOWS:
394            mTest = new Shadows();
395            break;
396        case CONTRAST:
397            mTest = new Contrast();
398            break;
399        case EXPOSURE:
400            mTest = new Exposure();
401            break;
402        case WHITE_BALANCE:
403            mTest = new WhiteBalance();
404            break;
405        case COLOR_CUBE:
406            mTest = new ColorCube(false);
407            break;
408        case COLOR_CUBE_3D_INTRINSIC:
409            mTest = new ColorCube(true);
410            break;
411        case USAGE_IO:
412            mTest = new UsageIO();
413            break;
414        case ARTISTIC_1:
415            mTest = new Artistic1();
416            break;
417        case HISTOGRAM:
418            mTest = new Histogram();
419            break;
420        }
421
422        mTest.createBaseTest(this, mBitmapIn, mBitmapIn2, mBitmapOut);
423        setupBars();
424
425        mTest.runTest();
426        updateDisplay();
427        mBenchmarkResult.setText("Result: not run");
428    }
429
430    void setupTests() {
431        mTestSpinner.setAdapter(new ArrayAdapter<TestName>(
432            this, R.layout.spinner_layout, TestName.values()));
433    }
434
435    private AdapterView.OnItemSelectedListener mTestSpinnerListener =
436            new AdapterView.OnItemSelectedListener() {
437                public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
438                    changeTest(TestName.values()[pos]);
439                }
440
441                public void onNothingSelected(AdapterView parent) {
442
443                }
444            };
445
446    void init() {
447        mBitmapIn = loadBitmap(R.drawable.img1600x1067);
448        mBitmapIn2 = loadBitmap(R.drawable.img1600x1067b);
449        mBitmapOut = Bitmap.createBitmap(mBitmapIn.getWidth(), mBitmapIn.getHeight(),
450                                         mBitmapIn.getConfig());
451
452        mDisplayView = (ImageView) findViewById(R.id.display);
453        mDisplayView.setImageBitmap(mBitmapOut);
454
455        mSpinner = (Spinner) findViewById(R.id.spinner1);
456
457        mBar1 = (SeekBar) findViewById(R.id.slider1);
458        mBar2 = (SeekBar) findViewById(R.id.slider2);
459        mBar3 = (SeekBar) findViewById(R.id.slider3);
460        mBar4 = (SeekBar) findViewById(R.id.slider4);
461        mBar5 = (SeekBar) findViewById(R.id.slider5);
462
463        mBar1.setOnSeekBarChangeListener(this);
464        mBar2.setOnSeekBarChangeListener(this);
465        mBar3.setOnSeekBarChangeListener(this);
466        mBar4.setOnSeekBarChangeListener(this);
467        mBar5.setOnSeekBarChangeListener(this);
468
469        mText1 = (TextView) findViewById(R.id.slider1Text);
470        mText2 = (TextView) findViewById(R.id.slider2Text);
471        mText3 = (TextView) findViewById(R.id.slider3Text);
472        mText4 = (TextView) findViewById(R.id.slider4Text);
473        mText5 = (TextView) findViewById(R.id.slider5Text);
474
475        mTestSpinner = (Spinner) findViewById(R.id.filterselection);
476        mTestSpinner.setOnItemSelectedListener(mTestSpinnerListener);
477
478        mBenchmarkResult = (TextView) findViewById(R.id.benchmarkText);
479        mBenchmarkResult.setText("Result: not run");
480
481
482        mRS = RenderScript.create(this);
483        mInPixelsAllocation = Allocation.createFromBitmap(mRS, mBitmapIn,
484                                                          Allocation.MipmapControl.MIPMAP_NONE,
485                                                          Allocation.USAGE_SHARED |
486                                                          Allocation.USAGE_GRAPHICS_TEXTURE |
487                                                          Allocation.USAGE_SCRIPT);
488        mInPixelsAllocation2 = Allocation.createFromBitmap(mRS, mBitmapIn2,
489                                                           Allocation.MipmapControl.MIPMAP_NONE,
490                                                           Allocation.USAGE_SHARED |
491                                                           Allocation.USAGE_GRAPHICS_TEXTURE |
492                                                           Allocation.USAGE_SCRIPT);
493        mOutPixelsAllocation = Allocation.createFromBitmap(mRS, mBitmapOut);
494
495
496        setupTests();
497        changeTest(TestName.LEVELS_VEC3_RELAXED);
498    }
499
500    void cleanup() {
501        synchronized(this) {
502            RenderScript rs = mRS;
503            mRS = null;
504            while(mDoingBenchmark) {
505                try {
506                    Thread.sleep(1, 0);
507                } catch(InterruptedException e) {
508                }
509
510            }
511            rs.destroy();
512        }
513
514        mInPixelsAllocation = null;
515        mInPixelsAllocation2 = null;
516        mOutPixelsAllocation = null;
517        mBitmapIn = null;
518        mBitmapIn2 = null;
519        mBitmapOut = null;
520    }
521
522    @Override
523    protected void onCreate(Bundle savedInstanceState) {
524        super.onCreate(savedInstanceState);
525        setContentView(R.layout.main);
526
527        init();
528    }
529
530    @Override
531    protected void onPause() {
532        super.onPause();
533
534        cleanup();
535    }
536
537
538    @Override
539    protected void onResume() {
540        super.onResume();
541
542        init();
543    }
544
545    private Bitmap loadBitmap(int resource) {
546        final BitmapFactory.Options options = new BitmapFactory.Options();
547        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
548        return BitmapFactory.decodeResource(getResources(), resource, options);
549    }
550
551    // button hook
552    public void benchmark(View v) {
553        float t = getBenchmark();
554        //long javaTime = javaFilter();
555        //mBenchmarkResult.setText("RS: " + t + " ms  Java: " + javaTime + " ms");
556        mBenchmarkResult.setText("Result: " + t + " ms");
557        Log.v(TAG, "getBenchmark: Renderscript frame time core ms " + t);
558    }
559
560    public void benchmark_all(View v) {
561        // write result into a file
562        File externalStorage = Environment.getExternalStorageDirectory();
563        if (!externalStorage.canWrite()) {
564            Log.v(TAG, "sdcard is not writable");
565            return;
566        }
567        File resultFile = new File(externalStorage, RESULT_FILE);
568        resultFile.setWritable(true, false);
569        try {
570            BufferedWriter rsWriter = new BufferedWriter(new FileWriter(resultFile));
571            Log.v(TAG, "Saved results in: " + resultFile.getAbsolutePath());
572            for (TestName tn: TestName.values()) {
573                changeTest(tn);
574                float t = getBenchmark();
575                String s = new String("" + tn.toString() + ", " + t);
576                rsWriter.write(s + "\n");
577                Log.v(TAG, "Test " + s + "ms\n");
578            }
579            rsWriter.close();
580        } catch (IOException e) {
581            Log.v(TAG, "Unable to write result file " + e.getMessage());
582        }
583        changeTest(TestName.LEVELS_VEC3_RELAXED);
584    }
585
586
587
588    // For benchmark test
589    public float getBenchmark() {
590        if (mRS == null) {
591            return 0;
592        }
593        mDoingBenchmark = true;
594
595        mDvfsWar.go();
596        mTest.setupBenchmark();
597        long result = 0;
598
599        //Log.v(TAG, "Warming");
600        long t = java.lang.System.currentTimeMillis() + 250;
601        do {
602            mTest.runTest();
603            mTest.finish();
604        } while (t > java.lang.System.currentTimeMillis());
605
606        //Log.v(TAG, "Benchmarking");
607        int ct = 0;
608        t = java.lang.System.currentTimeMillis();
609        do {
610            mTest.runTest();
611            mTest.finish();
612            ct++;
613        } while ((t+1000) > java.lang.System.currentTimeMillis());
614        t = java.lang.System.currentTimeMillis() - t;
615        float ft = (float)t;
616        ft /= ct;
617
618        mTest.exitBenchmark();
619        mDoingBenchmark = false;
620
621        return ft;
622    }
623}
624