summaryrefslogtreecommitdiff
path: root/BenchmarkFramework/app/src/main/java/graphics_benchmarks/graphics/Tester.java
blob: 763690796c07f036b0656de0d3acd0f3b453c11d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright (C) 2010 0xlab - http://0xlab.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package graphics_benchmarks.graphics;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;

import android.os.SystemClock;

import android.app.Activity;
import android.content.Context;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.os.Bundle;
import android.widget.*;
import android.view.*;
import android.graphics.Canvas;
import android.os.*;

import org.linaro.iasenov.benchmarkframework.MainActivity;

import java.lang.reflect.Method;

public abstract class Tester extends Activity {
    private String TAG = "";
    public final static String PACKAGE = "graphics_benchmarks.graphics";
    int mRound;
    int mNow;
    int mIndex;

    protected long mTesterStart = 0;
    protected long mTesterEnd   = 0;

    protected abstract String getTag();
    protected abstract int sleepBeforeStart();
    protected abstract int sleepBetweenRound();
    protected abstract void oneRound();

    protected String mSourceTag = "unknown";
    private boolean mNextRound = true;

    protected boolean mDropTouchEvent     = true;
    protected boolean mDropTrackballEvent = true;

    ProgressDialog progressDialog;

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        TAG = getTag();

        //Prevent screen off cause this will interrupt the test
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        Intent intent = getIntent();
        if (intent != null) {
            mRound = intent.getIntExtra(MainActivity.GRAPHICS_ROUND, 100);
            //mSourceTag = Case.getSource(intent);
            //mIndex     = Case.getIndex(intent);
        } else {
            mRound = 80;
            mIndex = -1;
        }
        mNow   = mRound;


        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("Step " + MainActivity.mStep + " From " + MainActivity.mFrom);
        progressDialog.setMessage(MainActivity.mSelectedTxt + " is running please wait...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        progressDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

        progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
            // Set a click listener for progress dialog cancel button
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.i(TAG, "Cancel");
                Intent output = new Intent();
                setResult(Activity.RESULT_CANCELED, output);

                // dismiss the progress dialog
                progressDialog.dismiss();

                finish();

            }
        });

        progressDialog.show();
    }

    @Override
    protected void onPause() {
        super.onPause();
        interruptTester();
    }

    /* drop the annoying event */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (mDropTouchEvent) {
            return false;
        } else {
            return super.dispatchTouchEvent(ev);
        }
    }

    @Override
    public boolean dispatchTrackballEvent(MotionEvent ev) {
        if (mDropTrackballEvent) {
            return false;
        } else {
            return super.dispatchTouchEvent(ev);
        }
    }

    protected void startTester() {
        TesterThread thread = new TesterThread(sleepBeforeStart(), sleepBetweenRound());
        thread.start();

        /*
        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("Step " + MainActivity.mStep + " From " + MainActivity.mFrom);
        progressDialog.setMessage(MainActivity.mSelectedTxt + " is running please wait...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        progressDialog.show();
        */

    }

    public void interruptTester() {
        mNow = 0;
        finish();
    }

    /**
     * Call this method if you finish your testing.
     *
     * @param start The starting time of testing round
     * @param end The ending time of testing round
     */


    public void finishTester(long start, long end) {

        if(progressDialog !=null){
            progressDialog.dismiss();
        }

        mTesterStart = start;
        mTesterEnd   = end;
        Intent output = new Intent();
        /*
        if (mSourceTag == null || mSourceTag.equals("")) {
            Case.putSource(intent, "unknown");
        } else {
            Case.putSource(intent, mSourceTag);
        }
        */

        //Case.putIndex(intent, mIndex);
        //saveResult(intent);
        output.putExtra(MainActivity.GRAPHICS_ELAPSED_TIME, mTesterEnd - mTesterStart);
        output.putExtra(MainActivity.GRAPHICS_ROUND, mRound);
        setResult(Activity.RESULT_OK, output);


        finish();
    }


    /**
     * Save the benchmarking result into intent
     * If this Case and Tester has their own way to pass benchmarking result
     * just override this method
     *
     * @param intent The intent will return to Case
     */
    /*
    protected boolean saveResult(Intent intent) {
        long elapse = mTesterEnd - mTesterStart;
        Case.putResult(intent, elapse);
        return true;
    }
    */

    public void resetCounter() {
        mNow = mRound;
    }

    public void decreaseCounter() {

        if (mNow == mRound) {
            mTesterStart = SystemClock.uptimeMillis();
        } else if (mNow == 1) {
            mTesterEnd = SystemClock.uptimeMillis();
        }

        mNow = mNow - 1;
        mNextRound = true;
        //Log.i(TAG, "mNow:"+ mNow);
    }

    public boolean isTesterFinished() {
        return (mNow <= 0);
    }



    class TesterThread extends Thread {
        int mSleepingStart;
        int mSleepingTime;
        TesterThread(int sleepStart, int sleepPeriod) {
            mSleepingStart = sleepStart;
            mSleepingTime  = sleepPeriod;
        }

        private void lazyLoop() throws Exception {
            while (!isTesterFinished()) {
                if (mNextRound) {
                    mNextRound = false;
                    oneRound();
                } else {
                    sleep(mSleepingTime);
                    // TODO: 
                    // Benchmarks that calculates frequencies (e.g. fps) should be time,
                    // for example, GL cases should run for a fixed time, and calculate 
                    // # of frames rendered, instead of periodically checking if fixed 
                    // # of frames had been rendered (which can hurt performance).
                }
            }
        }

        private void nervousLoop() throws Exception {
            while (!isTesterFinished()) {
                oneRound();
            }
        }

        private void sleepLoop() throws Exception {
            while (!isTesterFinished()) {
                oneRound();
                sleep(mSleepingTime);
            }
        }

        public void run() {
            try {
                sleep(mSleepingStart);

                long start = SystemClock.uptimeMillis();

                lazyLoop();

                long end = SystemClock.uptimeMillis();
                finishTester(start, end);
            } catch (Exception e) {
                    e.printStackTrace();
            }
        }
    }
}