-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathBlurBenchmarkTask.java
161 lines (135 loc) · 5.81 KB
/
BlurBenchmarkTask.java
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
package at.favre.app.blurbenchmark;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;
import androidx.renderscript.RenderScript;
import java.io.IOException;
import at.favre.app.blurbenchmark.blur.EBlurAlgorithm;
import at.favre.app.blurbenchmark.models.BenchmarkImage;
import at.favre.app.blurbenchmark.models.BenchmarkWrapper;
import at.favre.app.blurbenchmark.models.StatInfo;
import at.favre.app.blurbenchmark.util.BenchmarkUtil;
import at.favre.app.blurbenchmark.util.BitmapUtil;
import at.favre.app.blurbenchmark.util.BlurUtil;
/**
* This is the the task for completing a single Benchmark with
* the given image, blur radius, algorithm and rounds.
* <p>
* It uses warmup rounds to warmup the VM. After the benchmark
* the statistics and downscaled versions of the blurred images
* are store to disk.
*
* @author pfavre
* @since 2014/04/14
*/
public class BlurBenchmarkTask extends AsyncTask<Void, Void, BenchmarkWrapper> {
private static final String TAG = BlurBenchmarkTask.class.getSimpleName();
private static final int WARMUP_ROUNDS = 5;
private StatInfo statInfo;
private long startWholeProcess;
private int bitmapDrawableResId;
private String absolutePath;
private Bitmap master;
private int benchmarkRounds;
private int radius;
private EBlurAlgorithm algorithm;
private Context ctx;
private RenderScript rs;
private boolean run = false;
private boolean isCustomPic = false;
public BlurBenchmarkTask(BenchmarkImage image, int benchmarkRounds, int radius, EBlurAlgorithm algorithm, RenderScript rs, Context ctx) {
if (image.isResId()) {
this.bitmapDrawableResId = image.getResId();
} else {
this.absolutePath = image.getAbsolutePath();
this.isCustomPic = true;
}
this.benchmarkRounds = benchmarkRounds;
this.radius = radius;
this.algorithm = algorithm;
this.rs = rs;
this.ctx = ctx;
}
@Override
protected void onPreExecute() {
Log.d(TAG, "Start test with " + radius + "px radius, " + benchmarkRounds + "rounds in " + algorithm);
startWholeProcess = BenchmarkUtil.elapsedRealTimeNanos();
}
@Override
protected BenchmarkWrapper doInBackground(Void... voids) {
try {
run = true;
long startReadBitmap = BenchmarkUtil.elapsedRealTimeNanos();
master = loadBitmap();
if (master == null) {
throw new IOException("Could not load bitmap");
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
master.setHasMipMap(false);
long readBitmapDuration = (BenchmarkUtil.elapsedRealTimeNanos() - startReadBitmap) / 1000000l;
statInfo = new StatInfo(master.getHeight(), master.getWidth(), radius, algorithm, benchmarkRounds, BitmapUtil.sizeOf(master));
statInfo.setLoadBitmap(readBitmapDuration);
Bitmap blurredBitmap = null;
//if just quick round, skip warmup
if (benchmarkRounds > WARMUP_ROUNDS) {
Log.d(TAG, "Warmup");
for (int i = 0; i < WARMUP_ROUNDS; i++) {
if (!run) {
break;
}
BenchmarkUtil.elapsedRealTimeNanos();
blurredBitmap = master.copy(master.getConfig(), true);
blurredBitmap = BlurUtil.blur(rs, ctx, blurredBitmap, radius, algorithm);
}
} else {
Log.d(TAG, "Skip warmup");
}
Log.d(TAG, "Start benchmark");
for (int i = 0; i < benchmarkRounds; i++) {
if (!run) {
break;
}
long startBlur = BenchmarkUtil.elapsedRealTimeNanos();
blurredBitmap = master.copy(master.getConfig(), true);
blurredBitmap = BlurUtil.blur(rs, ctx, blurredBitmap, radius, algorithm);
statInfo.getBenchmarkData().add((BenchmarkUtil.elapsedRealTimeNanos() - startBlur) / 1000000d);
}
if (!run) {
return null;
}
statInfo.setBenchmarkDuration((BenchmarkUtil.elapsedRealTimeNanos() - startWholeProcess) / 1000000l);
String fileName = master.getWidth() + "x" + master.getHeight() + "_" + radius + "px_" + algorithm + ".png";
return new BenchmarkWrapper(BitmapUtil.saveBitmapDownscaled(blurredBitmap, fileName, BitmapUtil.getCacheDir(ctx), false, 800, 800),
BitmapUtil.saveBitmapDownscaled(BitmapUtil.flip(blurredBitmap), "mirror_" + fileName, BitmapUtil.getCacheDir(ctx), true, 300, 300),
statInfo, isCustomPic);
} catch (Throwable e) {
Log.e(TAG, "Could not complete benchmark", e);
statInfo.setException(e);
return new BenchmarkWrapper(null, null, statInfo, isCustomPic);
}
}
private Bitmap loadBitmap() {
if (isCustomPic && absolutePath != null) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inMutable = true;
return BitmapFactory.decodeFile(absolutePath, options);
} else {
final BitmapFactory.Options options = new BitmapFactory.Options();
return BitmapFactory.decodeResource(ctx.getResources(), bitmapDrawableResId, options);
}
}
public void cancelBenchmark() {
run = false;
Log.d(TAG, "canceled");
}
@Override
protected void onPostExecute(BenchmarkWrapper bitmap) {
master.recycle();
master = null;
Log.d(TAG, "test done");
}
}