Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/performance/client/RenderPerfTest/src/renderperf/RenderPerfTest.java
41154 views
1
/*
2
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
package renderperf;
25
26
import java.awt.AlphaComposite;
27
import java.awt.Color;
28
import java.awt.Dimension;
29
import java.awt.Font;
30
import java.awt.Graphics;
31
import java.awt.Graphics2D;
32
import java.awt.Image;
33
import java.awt.LinearGradientPaint;
34
import java.awt.RadialGradientPaint;
35
import java.awt.RenderingHints;
36
import java.awt.Robot;
37
38
import java.awt.event.WindowAdapter;
39
import java.awt.event.WindowEvent;
40
import java.awt.geom.AffineTransform;
41
import java.awt.geom.Ellipse2D;
42
import java.awt.geom.Point2D;
43
import java.awt.geom.QuadCurve2D;
44
45
import java.awt.image.BufferedImage;
46
import java.awt.image.DataBuffer;
47
import java.awt.image.DataBufferByte;
48
import java.awt.image.DataBufferInt;
49
import java.awt.image.DataBufferShort;
50
51
import java.io.IOException;
52
import java.lang.reflect.InvocationTargetException;
53
import java.lang.reflect.Method;
54
import java.util.HashSet;
55
import java.util.Objects;
56
import java.util.concurrent.CountDownLatch;
57
import java.util.concurrent.atomic.AtomicBoolean;
58
59
import javax.imageio.ImageIO;
60
import javax.swing.JFrame;
61
import javax.swing.JPanel;
62
import javax.swing.SwingUtilities;
63
import javax.swing.Timer;
64
import javax.swing.WindowConstants;
65
66
public class RenderPerfTest {
67
private static HashSet<String> ignoredTests = new HashSet<>();
68
69
static {
70
ignoredTests.add("testWiredBoxAA");
71
}
72
73
private final static int N = 1000;
74
private final static float WIDTH = 800;
75
private final static float HEIGHT = 800;
76
private final static float R = 25;
77
private final static int BW = 50;
78
private final static int BH = 50;
79
private final static int COUNT = 300;
80
private final static int DELAY = 10;
81
private final static int RESOLUTION = 5;
82
private final static int COLOR_TOLERANCE = 10;
83
private final static int MAX_MEASURE_TIME = 5000;
84
85
86
interface Configurable {
87
void configure(Graphics2D g2d);
88
}
89
90
interface Renderable {
91
void setup(Graphics2D g2d);
92
void render(Graphics2D g2d);
93
void update();
94
}
95
96
static class Particles {
97
private float[] bx;
98
private float[] by;
99
private float[] vx;
100
private float[] vy;
101
private float r;
102
private int n;
103
104
private float x0;
105
private float y0;
106
private float width;
107
private float height;
108
109
Particles(int n, float r, float x0, float y0, float width, float height) {
110
bx = new float[n];
111
by = new float[n];
112
vx = new float[n];
113
vy = new float[n];
114
this.n = n;
115
this.r = r;
116
this.x0 = x0;
117
this.y0 = y0;
118
this.width = width;
119
this.height = height;
120
for (int i = 0; i < n; i++) {
121
bx[i] = (float) (x0 + r + 0.1 + Math.random() * (width - 2 * r - 0.2 - x0));
122
by[i] = (float) (y0 + r + 0.1 + Math.random() * (height - 2 * r - 0.2 - y0));
123
vx[i] = 0.1f * (float) (Math.random() * 2 * r - r);
124
vy[i] = 0.1f * (float) (Math.random() * 2 * r - r);
125
}
126
127
}
128
129
void render(Graphics2D g2d, ParticleRenderer renderer) {
130
for (int i = 0; i < n; i++) {
131
renderer.render(g2d, i, bx, by, vx, vy);
132
}
133
}
134
135
void update() {
136
for (int i = 0; i < n; i++) {
137
bx[i] += vx[i];
138
if (bx[i] + r > width || bx[i] - r < x0) vx[i] = -vx[i];
139
by[i] += vy[i];
140
if (by[i] + r > height || by[i] - r < y0) vy[i] = -vy[i];
141
}
142
143
}
144
145
}
146
147
ParticleRenderable createPR(ParticleRenderer renderer) {
148
return new ParticleRenderable(renderer);
149
}
150
151
static class ParticleRenderable implements Renderable {
152
ParticleRenderer renderer;
153
Configurable configure;
154
155
ParticleRenderable(ParticleRenderer renderer, Configurable configure) {
156
this.renderer = renderer;
157
this.configure = configure;
158
}
159
160
ParticleRenderable(ParticleRenderer renderer) {
161
this(renderer, null);
162
}
163
164
@Override
165
public void setup(Graphics2D g2d) {
166
if (configure != null) configure.configure(g2d);
167
}
168
169
@Override
170
public void render(Graphics2D g2d) {
171
balls.render(g2d, renderer);
172
}
173
174
@Override
175
public void update() {
176
balls.update();
177
}
178
179
public ParticleRenderable configure(Configurable configure) {
180
this.configure = configure;
181
return this;
182
}
183
}
184
185
interface ParticleRenderer {
186
void render(Graphics2D g2d, int id, float[] x, float[] y, float[] vx, float[] vy);
187
188
}
189
190
static class FlatParticleRenderer implements ParticleRenderer {
191
Color[] colors;
192
float r;
193
194
FlatParticleRenderer(int n, float r) {
195
colors = new Color[n];
196
this.r = r;
197
for (int i = 0; i < n; i++) {
198
colors[i] = new Color((float) Math.random(),
199
(float) Math.random(), (float) Math.random());
200
}
201
}
202
203
@Override
204
public void render(Graphics2D g2d, int id, float[] x, float[] y, float[] vx, float[] vy) {
205
g2d.setColor(colors[id % colors.length]);
206
g2d.fillOval((int)(x[id] - r), (int)(y[id] - r), (int)(2*r), (int)(2*r));
207
}
208
209
}
210
211
static class ClipFlatParticleRenderer extends FlatParticleRenderer {
212
213
ClipFlatParticleRenderer(int n, float r) {
214
super(n, r);
215
}
216
217
@Override
218
public void render(Graphics2D g2d, int id, float[] x, float[] y, float[] vx, float[] vy) {
219
if ((id % 10) == 0) {
220
g2d.setColor(colors[id % colors.length]);
221
g2d.setClip(new Ellipse2D.Double((int) (x[id] - r), (int) (y[id] - r), (int) (2 * r), (int) (2 * r)));
222
g2d.fillRect((int) (x[id] - 2 * r), (int) (y[id] - 2 * r), (int) (4 * r), (int) (4 * r));
223
}
224
}
225
226
}
227
static class WhiteTextParticleRenderer implements ParticleRenderer {
228
float r;
229
230
WhiteTextParticleRenderer(float r) {
231
this.r = r;
232
}
233
234
void setPaint(Graphics2D g2d, int id) {
235
g2d.setColor(Color.WHITE);
236
}
237
238
@Override
239
public void render(Graphics2D g2d, int id, float[] x, float[] y, float[] vx, float[] vy) {
240
setPaint(g2d, id);
241
g2d.drawString("The quick brown fox jumps over the lazy dog",
242
(int)(x[id] - r), (int)(y[id] - r));
243
g2d.drawString("The quick brown fox jumps over the lazy dog",
244
(int)(x[id] - r), (int)y[id]);
245
g2d.drawString("The quick brown fox jumps over the lazy dog",
246
(int)(x[id] - r), (int)(y[id] + r));
247
}
248
}
249
250
static class TextParticleRenderer extends WhiteTextParticleRenderer {
251
Color[] colors;
252
253
float r;
254
255
TextParticleRenderer(int n, float r) {
256
super(r);
257
colors = new Color[n];
258
this.r = r;
259
for (int i = 0; i < n; i++) {
260
colors[i] = new Color((float) Math.random(),
261
(float) Math.random(), (float) Math.random());
262
}
263
}
264
265
void setPaint(Graphics2D g2d, int id) {
266
g2d.setColor(colors[id % colors.length]);
267
}
268
}
269
270
static class LargeTextParticleRenderer extends TextParticleRenderer {
271
272
LargeTextParticleRenderer(int n, float r) {
273
super(n, r);
274
}
275
276
@Override
277
public void render(Graphics2D g2d, int id, float[] x, float[] y, float[] vx, float[] vy) {
278
setPaint(g2d, id);
279
Font font = new Font("LucidaGrande", Font.PLAIN, 32);
280
g2d.setFont(font);
281
g2d.drawString("The quick brown fox jumps over the lazy dog",
282
(int)(x[id] - r), (int)(y[id] - r));
283
g2d.drawString("The quick brown fox jumps over the lazy dog",
284
(int)(x[id] - r), (int)y[id]);
285
g2d.drawString("The quick brown fox jumps over the lazy dog",
286
(int)(x[id] - r), (int)(y[id] + r));
287
}
288
}
289
290
static class FlatOvalRotParticleRenderer extends FlatParticleRenderer {
291
292
293
FlatOvalRotParticleRenderer(int n, float r) {
294
super(n, r);
295
}
296
297
void setPaint(Graphics2D g2d, int id) {
298
g2d.setColor(colors[id % colors.length]);
299
}
300
301
@Override
302
public void render(Graphics2D g2d, int id, float[] x, float[] y, float[] vx, float[] vy) {
303
setPaint(g2d, id);
304
if (Math.abs(vx[id] + vy[id]) > 0.001) {
305
AffineTransform t = (AffineTransform) g2d.getTransform().clone();
306
double l = vx[id] / Math.sqrt(vx[id] * vx[id] + vy[id] * vy[id]);
307
if (vy[id] < 0) {
308
l = -l;
309
}
310
g2d.translate(x[id], y[id]);
311
g2d.rotate(Math.acos(l));
312
g2d.fillOval(-(int)r, (int)(-0.5*r), (int) (2 * r), (int)r);
313
g2d.setTransform(t);
314
} else {
315
g2d.fillOval((int)(x[id] - r), (int)(y[id] - 0.5*r),
316
(int) (2 * r), (int) r);
317
}
318
}
319
}
320
321
static class LinGradOvalRotParticleRenderer extends FlatOvalRotParticleRenderer {
322
323
324
LinGradOvalRotParticleRenderer(int n, float r) {
325
super(n, r);
326
}
327
328
@Override
329
void setPaint(Graphics2D g2d, int id) {
330
Point2D start = new Point2D.Double(- r, - 0.5*r);
331
Point2D end = new Point2D.Double( 2 * r, r);
332
float[] dist = {0.0f, 1.0f};
333
Color[] cls = {colors[id %colors.length], colors[(colors.length - id) %colors.length]};
334
LinearGradientPaint p =
335
new LinearGradientPaint(start, end, dist, cls);
336
g2d.setPaint(p);
337
}
338
}
339
340
static class LinGrad3OvalRotParticleRenderer extends FlatOvalRotParticleRenderer {
341
342
343
LinGrad3OvalRotParticleRenderer(int n, float r) {
344
super(n, r);
345
}
346
347
@Override
348
void setPaint(Graphics2D g2d, int id) {
349
Point2D start = new Point2D.Double(- r, - 0.5*r);
350
Point2D end = new Point2D.Double( 2 * r, r);
351
float[] dist = {0.0f, 0.5f, 1.0f};
352
Color[] cls = {
353
colors[id %colors.length],
354
colors[(colors.length - id) %colors.length],
355
colors[(id*5) %colors.length]};
356
LinearGradientPaint p =
357
new LinearGradientPaint(start, end, dist, cls);
358
g2d.setPaint(p);
359
}
360
}
361
362
static class RadGrad3OvalRotParticleRenderer extends FlatOvalRotParticleRenderer {
363
364
365
RadGrad3OvalRotParticleRenderer(int n, float r) {
366
super(n, r);
367
}
368
369
@Override
370
void setPaint(Graphics2D g2d, int id) {
371
Point2D start = new Point2D.Double();
372
float[] dist = {0.0f, 0.5f, 1.0f};
373
Color[] cls = {
374
colors[id %colors.length],
375
colors[(colors.length - id) %colors.length],
376
colors[(id*5) %colors.length]};
377
RadialGradientPaint p =
378
new RadialGradientPaint(start, r, dist, cls);
379
g2d.setPaint(p);
380
}
381
}
382
383
static class FlatBoxParticleRenderer extends FlatParticleRenderer {
384
385
386
FlatBoxParticleRenderer(int n, float r) {
387
super(n, r);
388
}
389
@Override
390
public void render(Graphics2D g2d, int id, float[] x, float[] y, float[] vx, float[] vy) {
391
g2d.setColor(colors[id % colors.length]);
392
g2d.fillRect((int)(x[id] - r), (int)(y[id] - r), (int)(2*r), (int)(2*r));
393
394
}
395
396
}
397
398
static class ClipFlatBoxParticleRenderer extends FlatParticleRenderer {
399
400
401
ClipFlatBoxParticleRenderer(int n, float r) {
402
super(n, r);
403
}
404
@Override
405
public void render(Graphics2D g2d, int id, float[] x, float[] y, float[] vx, float[] vy) {
406
if ((id % 10) == 0) {
407
g2d.setColor(colors[id % colors.length]);
408
g2d.setClip((int) (x[id] - r), (int) (y[id] - r), (int) (2 * r), (int) (2 * r));
409
g2d.fillRect((int) (x[id] - 2 * r), (int) (y[id] - 2 * r), (int) (4 * r), (int) (4 * r));
410
}
411
}
412
}
413
414
static class ImgParticleRenderer extends FlatParticleRenderer {
415
BufferedImage dukeImg;
416
417
ImgParticleRenderer(int n, float r) {
418
super(n, r);
419
try {
420
dukeImg = ImageIO.read(
421
Objects.requireNonNull(
422
RenderPerfTest.class.getClassLoader().getResourceAsStream(
423
"renderperf/images/duke.png")));
424
} catch (IOException e) {
425
throw new RuntimeException(e);
426
}
427
}
428
429
@Override
430
public void render(Graphics2D g2d, int id, float[] x, float[] y, float[] vx, float[] vy) {
431
g2d.setColor(colors[id % colors.length]);
432
g2d.drawImage(dukeImg, (int)(x[id] - r), (int)(y[id] - r), (int)(2*r), (int)(2*r), null);
433
}
434
435
}
436
437
static class FlatBoxRotParticleRenderer extends FlatParticleRenderer {
438
439
440
FlatBoxRotParticleRenderer(int n, float r) {
441
super(n, r);
442
}
443
@Override
444
public void render(Graphics2D g2d, int id, float[] x, float[] y, float[] vx, float[] vy) {
445
g2d.setColor(colors[id % colors.length]);
446
if (Math.abs(vx[id] + vy[id]) > 0.001) {
447
AffineTransform t = (AffineTransform) g2d.getTransform().clone();
448
double l = vx[id] / Math.sqrt(vx[id] * vx[id] + vy[id] * vy[id]);
449
if (vy[id] < 0) {
450
l = -l;
451
}
452
g2d.translate(x[id], y[id]);
453
g2d.rotate(Math.acos(l));
454
g2d.fillRect(-(int)r, -(int)r, (int) (2 * r), (int) (2 * r));
455
g2d.setTransform(t);
456
} else {
457
g2d.fillRect((int)(x[id] - r), (int)(y[id] - r),
458
(int) (2 * r), (int) (2 * r));
459
}
460
}
461
}
462
463
static class WiredParticleRenderer extends FlatParticleRenderer {
464
465
466
WiredParticleRenderer(int n, float r) {
467
super(n, r);
468
}
469
@Override
470
public void render(Graphics2D g2d, int id, float[] x, float[] y, float[] vx, float[] vy) {
471
g2d.setColor(colors[id % colors.length]);
472
g2d.drawOval((int)(x[id] - r), (int)(y[id] - r), (int)(2*r), (int)(2*r));
473
}
474
475
}
476
static class WiredBoxParticleRenderer extends FlatParticleRenderer {
477
478
WiredBoxParticleRenderer(int n, float r) {
479
super(n, r);
480
}
481
482
@Override
483
public void render(Graphics2D g2d, int id, float[] x, float[] y, float[] vx, float[] vy) {
484
g2d.setColor(colors[id % colors.length]);
485
g2d.drawRect((int)(x[id] - r), (int)(y[id] - r), (int)(2*r), (int)(2*r));
486
}
487
488
}
489
static class SegParticleRenderer extends FlatParticleRenderer {
490
491
SegParticleRenderer(int n, float r) {
492
super(n, r);
493
}
494
495
@Override
496
public void render(Graphics2D g2d, int id, float[] x, float[] y, float[] vx, float[] vy) {
497
double v = Math.sqrt(vx[id]*vx[id]+vy[id]*vy[id]);
498
float nvx = (float) (vx[id]/v);
499
float nvy = (float) (vy[id]/v);
500
g2d.setColor(colors[id % colors.length]);
501
g2d.drawLine((int)(x[id] - r*nvx), (int)(y[id] - r*nvy),
502
(int)(x[id] + 2*r*nvx), (int)(y[id] + 2*r*nvy));
503
}
504
505
}
506
507
508
static class WiredQuadParticleRenderer extends FlatParticleRenderer {
509
510
WiredQuadParticleRenderer(int n, float r) {
511
super(n, r);
512
}
513
514
@Override
515
public void render(Graphics2D g2d, int id, float[] x, float[] y, float[] vx, float[] vy) {
516
if (id > 2 && (id % 3) == 0) {
517
g2d.setColor(colors[id % colors.length]);
518
g2d.draw(new QuadCurve2D.Float(x[id-3], y[id-3], x[id-2], y[id-2], x[id-1], y[id-1]));
519
}
520
521
}
522
}
523
524
static class FlatQuadParticleRenderer extends FlatParticleRenderer {
525
526
FlatQuadParticleRenderer(int n, float r) {
527
super(n, r);
528
}
529
530
@Override
531
public void render(Graphics2D g2d, int id, float[] x, float[] y, float[] vx, float[] vy) {
532
if (id > 2 && (id % 3) == 0) {
533
g2d.setColor(colors[id % colors.length]);
534
g2d.fill(new QuadCurve2D.Float(x[id-3], y[id-3], x[id-2], y[id-2], x[id-1], y[id-1]));
535
}
536
537
}
538
}
539
540
static class BlitImageParticleRenderer extends FlatParticleRenderer {
541
BufferedImage image;
542
543
BlitImageParticleRenderer(int n, float r, BufferedImage img) {
544
super(n, r);
545
image = img;
546
fill(image);
547
}
548
549
@Override
550
public void render(Graphics2D g2d, int id, float[] x, float[] y, float[] vx, float[] vy) {
551
g2d.drawImage(image, (int)(x[id] - r), (int)(y[id] - r), (int)(2*r), (int)(2*r), null);
552
}
553
554
private static void fill(final Image image) {
555
final Graphics2D graphics = (Graphics2D) image.getGraphics();
556
graphics.setComposite(AlphaComposite.Src);
557
for (int i = 0; i < image.getHeight(null); ++i) {
558
graphics.setColor(new Color(i, 0, 0));
559
graphics.fillRect(0, i, image.getWidth(null), 1);
560
}
561
graphics.dispose();
562
}
563
564
}
565
566
static class SwBlitImageParticleRenderer extends BlitImageParticleRenderer {
567
568
SwBlitImageParticleRenderer(int n, float r, final int type) {
569
super(n, r, makeUnmanagedBI(type));
570
}
571
572
private static BufferedImage makeUnmanagedBI(final int type) {
573
final BufferedImage bi = new BufferedImage(17, 33, type);
574
final DataBuffer db = bi.getRaster().getDataBuffer();
575
if (db instanceof DataBufferInt) {
576
((DataBufferInt) db).getData();
577
} else if (db instanceof DataBufferShort) {
578
((DataBufferShort) db).getData();
579
} else if (db instanceof DataBufferByte) {
580
((DataBufferByte) db).getData();
581
}
582
bi.setAccelerationPriority(0.0f);
583
return bi;
584
}
585
}
586
587
static class SurfaceBlitImageParticleRenderer extends BlitImageParticleRenderer {
588
589
SurfaceBlitImageParticleRenderer(int n, float r, final int type) {
590
super(n, r, makeManagedBI(type));
591
}
592
593
private static BufferedImage makeManagedBI(final int type) {
594
final BufferedImage bi = new BufferedImage(17, 33, type);
595
bi.setAccelerationPriority(1.0f);
596
return bi;
597
}
598
}
599
600
static class PerfMeter {
601
private String name;
602
private int frame = 0;
603
604
private JPanel panel;
605
606
private long time;
607
private double execTime = 0;
608
private Color expColor = Color.RED;
609
AtomicBoolean waiting = new AtomicBoolean(false);
610
private double fps;
611
612
PerfMeter(String name) {
613
this.name = name;
614
}
615
616
PerfMeter exec(final Renderable renderable) throws Exception {
617
final CountDownLatch latch = new CountDownLatch(COUNT);
618
final CountDownLatch latchFrame = new CountDownLatch(1);
619
final long endTime = System.currentTimeMillis() + MAX_MEASURE_TIME;
620
621
final JFrame f = new JFrame();
622
f.addWindowListener(new WindowAdapter() {
623
@Override
624
public void windowClosed(WindowEvent e) {
625
latchFrame.countDown();
626
}
627
});
628
629
SwingUtilities.invokeAndWait(new Runnable() {
630
@Override
631
public void run() {
632
633
panel = new JPanel()
634
{
635
@Override
636
protected void paintComponent(Graphics g) {
637
638
super.paintComponent(g);
639
time = System.nanoTime();
640
Graphics2D g2d = (Graphics2D) g.create();
641
renderable.setup(g2d);
642
renderable.render(g2d);
643
g2d.setColor(expColor);
644
g.fillRect(0, 0, BW, BH);
645
}
646
};
647
648
panel.setPreferredSize(new Dimension((int)(WIDTH + BW), (int)(HEIGHT + BH)));
649
panel.setBackground(Color.BLACK);
650
f.add(panel);
651
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
652
f.pack();
653
f.setVisible(true);
654
}
655
});
656
Robot robot = new Robot();
657
658
Timer timer = new Timer(DELAY, e -> {
659
660
if (waiting.compareAndSet(false, true)) {
661
Color c = robot.getPixelColor(
662
panel.getTopLevelAncestor().getX() + panel.getTopLevelAncestor().getInsets().left + BW / 2,
663
panel.getTopLevelAncestor().getY() + panel.getTopLevelAncestor().getInsets().top + BW / 2);
664
if (isAlmostEqual(c, Color.BLUE)) {
665
expColor = Color.RED;
666
} else {
667
expColor = Color.BLUE;
668
}
669
renderable.update();
670
panel.getParent().repaint();
671
672
} else {
673
while (!isAlmostEqual(
674
robot.getPixelColor(
675
panel.getTopLevelAncestor().getX() + panel.getTopLevelAncestor().getInsets().left + BW/2,
676
panel.getTopLevelAncestor().getY() + panel.getTopLevelAncestor().getInsets().top + BH/2),
677
expColor))
678
{
679
try {
680
Thread.sleep(RESOLUTION);
681
} catch (InterruptedException ex) {
682
ex.printStackTrace();
683
}
684
}
685
time = System.nanoTime() - time;
686
execTime += time;
687
frame++;
688
waiting.set(false);
689
}
690
691
if (System.currentTimeMillis() < endTime) {
692
latch.countDown();
693
} else {
694
while(latch.getCount() > 0) latch.countDown();
695
}
696
});
697
timer.start();
698
latch.await();
699
SwingUtilities.invokeAndWait(() -> {
700
timer.stop();
701
f.setVisible(false);
702
f.dispose();
703
});
704
705
latchFrame.await();
706
if (execTime != 0 && frame != 0) {
707
fps = 1e9 / (execTime / frame);
708
} else {
709
fps = 0;
710
}
711
712
return this;
713
}
714
715
private void report() {
716
System.err.println(name + " : " + String.format("%.2f FPS", fps));
717
}
718
719
private boolean isAlmostEqual(Color c1, Color c2) {
720
return Math.abs(c1.getRed() - c2.getRed()) < COLOR_TOLERANCE ||
721
Math.abs(c1.getGreen() - c2.getGreen()) < COLOR_TOLERANCE ||
722
Math.abs(c1.getBlue() - c2.getBlue()) < COLOR_TOLERANCE;
723
724
}
725
}
726
727
private static final Particles balls = new Particles(N, R, BW, BH, WIDTH, HEIGHT);
728
private static final ParticleRenderer flatRenderer = new FlatParticleRenderer(N, R);
729
private static final ParticleRenderer clipFlatRenderer = new ClipFlatParticleRenderer(N, R);
730
private static final ParticleRenderer flatOvalRotRenderer = new FlatOvalRotParticleRenderer(N, R);
731
private static final ParticleRenderer flatBoxRenderer = new FlatBoxParticleRenderer(N, R);
732
private static final ParticleRenderer clipFlatBoxParticleRenderer = new ClipFlatBoxParticleRenderer(N, R);
733
private static final ParticleRenderer flatBoxRotRenderer = new FlatBoxRotParticleRenderer(N, R);
734
private static final ParticleRenderer linGradOvalRotRenderer = new LinGradOvalRotParticleRenderer(N, R);
735
private static final ParticleRenderer linGrad3OvalRotRenderer = new LinGrad3OvalRotParticleRenderer(N, R);
736
private static final ParticleRenderer radGrad3OvalRotRenderer = new RadGrad3OvalRotParticleRenderer(N, R);
737
private static final ParticleRenderer wiredRenderer = new WiredParticleRenderer(N, R);
738
private static final ParticleRenderer wiredBoxRenderer = new WiredBoxParticleRenderer(N, R);
739
private static final ParticleRenderer segRenderer = new SegParticleRenderer(N, R);
740
private static final ParticleRenderer flatQuadRenderer = new FlatQuadParticleRenderer(N, R);
741
private static final ParticleRenderer wiredQuadRenderer = new WiredQuadParticleRenderer(N, R);
742
private static final ParticleRenderer imgRenderer = new ImgParticleRenderer(N, R);
743
private static final ParticleRenderer textRenderer = new TextParticleRenderer(N, R);
744
private static final ParticleRenderer largeTextRenderer = new LargeTextParticleRenderer(N, R);
745
private static final ParticleRenderer whiteTextRenderer = new WhiteTextParticleRenderer(R);
746
private static final ParticleRenderer argbSwBlitImageRenderer = new SwBlitImageParticleRenderer(N, R, BufferedImage.TYPE_INT_ARGB);
747
private static final ParticleRenderer bgrSwBlitImageRenderer = new SwBlitImageParticleRenderer(N, R, BufferedImage.TYPE_INT_BGR);
748
private static final ParticleRenderer argbSurfaceBlitImageRenderer = new SurfaceBlitImageParticleRenderer(N, R, BufferedImage.TYPE_INT_ARGB);
749
private static final ParticleRenderer bgrSurfaceBlitImageRenderer = new SurfaceBlitImageParticleRenderer(N, R, BufferedImage.TYPE_INT_BGR);
750
751
private static final Configurable AA = (Graphics2D g2d) ->
752
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
753
RenderingHints.VALUE_ANTIALIAS_ON);
754
755
private static final Configurable TextLCD = (Graphics2D g2d) ->
756
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
757
RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
758
759
private static final Configurable TextAA = (Graphics2D g2d) ->
760
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
761
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
762
763
private static final Configurable XORMode = (Graphics2D g2d) ->
764
{g2d.setXORMode(Color.WHITE);};
765
766
private static final Configurable XORModeLCDText = (Graphics2D g2d) ->
767
{g2d.setXORMode(Color.WHITE);
768
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
769
RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);};
770
771
772
public void testFlatOval() throws Exception {
773
(new PerfMeter("FlatOval")).exec(createPR(flatRenderer)).report();
774
}
775
776
public void testFlatOvalAA() throws Exception {
777
(new PerfMeter("FlatOvalAA")).exec(createPR(flatRenderer).configure(AA)).report();
778
}
779
780
public void testClipFlatOval() throws Exception {
781
(new PerfMeter("ClipFlatOval")).exec(createPR(clipFlatRenderer)).report();
782
}
783
784
public void testClipFlatOvalAA() throws Exception {
785
(new PerfMeter("ClipFlatOvalAA")).exec(createPR(clipFlatRenderer).configure(AA)).report();
786
}
787
788
public void testFlatBox() throws Exception {
789
(new PerfMeter("FlatBox")).exec(createPR(flatBoxRenderer)).report();
790
}
791
792
public void testFlatBoxAA() throws Exception {
793
(new PerfMeter("FlatBoxAA")).exec(createPR(flatBoxRenderer).configure(AA)).report();
794
}
795
796
public void testClipFlatBox() throws Exception {
797
(new PerfMeter("ClipFlatBox")).exec(createPR(clipFlatBoxParticleRenderer)).report();
798
}
799
800
public void testClipFlatBoxAA() throws Exception {
801
(new PerfMeter("ClipFlatBoxAA")).exec(createPR(clipFlatBoxParticleRenderer).configure(AA)).report();
802
}
803
804
public void testImage() throws Exception {
805
(new PerfMeter("Image")).exec(createPR(imgRenderer)).report();
806
}
807
808
public void testImageAA() throws Exception {
809
(new PerfMeter("ImageAA")).exec(createPR(imgRenderer).configure(AA)).report();
810
}
811
812
public void testRotatedBox() throws Exception {
813
(new PerfMeter("RotatedBox")).exec(createPR(flatBoxRotRenderer)).report();
814
}
815
816
public void testRotatedBoxAA() throws Exception {
817
(new PerfMeter("RotatedBoxAA")).exec(createPR(flatBoxRotRenderer).configure(AA)).report();
818
}
819
820
public void testRotatedOval() throws Exception {
821
(new PerfMeter("RotatedOval")).exec(createPR(flatOvalRotRenderer)).report();
822
}
823
824
public void testRotatedOvalAA() throws Exception {
825
(new PerfMeter("RotatedOvalAA")).exec(createPR(flatOvalRotRenderer).configure(AA)).report();
826
}
827
828
public void testLinGrad3RotatedOval() throws Exception {
829
(new PerfMeter("LinGrad3RotatedOval")).exec(createPR(linGrad3OvalRotRenderer)).report();
830
}
831
832
public void testLinGrad3RotatedOvalAA() throws Exception {
833
(new PerfMeter("LinGrad3RotatedOvalAA")).exec(createPR(linGrad3OvalRotRenderer).configure(AA)).report();
834
}
835
836
public void testRadGrad3RotatedOval() throws Exception {
837
(new PerfMeter("RadGrad3RotatedOval")).exec(createPR(radGrad3OvalRotRenderer)).report();
838
}
839
840
public void testRadGrad3RotatedOvalAA() throws Exception {
841
(new PerfMeter("RadGrad3RotatedOvalAA")).exec(createPR(radGrad3OvalRotRenderer).configure(AA)).report();
842
}
843
844
public void testLinGradRotatedOval() throws Exception {
845
(new PerfMeter("LinGradRotatedOval")).exec(createPR(linGradOvalRotRenderer)).report();
846
}
847
848
public void testLinGradRotatedOvalAA() throws Exception {
849
(new PerfMeter("LinGradRotatedOvalAA")).exec(createPR(linGradOvalRotRenderer).configure(AA)).report();
850
}
851
852
public void testWiredBubbles() throws Exception {
853
(new PerfMeter("WiredBubbles")).exec(createPR(wiredRenderer)).report();
854
}
855
856
public void testWiredBubblesAA() throws Exception {
857
(new PerfMeter("WiredBubblesAA")).exec(createPR(wiredRenderer).configure(AA)).report();
858
}
859
860
public void testWiredBox() throws Exception {
861
(new PerfMeter("WiredBox")).exec(createPR(wiredBoxRenderer)).report();
862
}
863
864
public void testWiredBoxAA() throws Exception {
865
(new PerfMeter("WiredBoxAA")).exec(createPR(wiredBoxRenderer).configure(AA)).report();
866
}
867
868
public void testLines() throws Exception {
869
(new PerfMeter("Lines")).exec(createPR(segRenderer)).report();
870
}
871
872
public void testLinesAA() throws Exception {
873
(new PerfMeter("LinesAA")).exec(createPR(segRenderer).configure(AA)).report();
874
}
875
876
public void testFlatQuad() throws Exception {
877
(new PerfMeter("FlatQuad")).exec(createPR(flatQuadRenderer)).report();
878
}
879
880
public void testFlatQuadAA() throws Exception {
881
(new PerfMeter("FlatQuadAA")).exec(createPR(flatQuadRenderer).configure(AA)).report();
882
}
883
884
public void testWiredQuad() throws Exception {
885
(new PerfMeter("WiredQuad")).exec(createPR(wiredQuadRenderer)).report();
886
}
887
888
public void testWiredQuadAA() throws Exception {
889
(new PerfMeter("WiredQuadAA")).exec(createPR(wiredQuadRenderer).configure(AA)).report();
890
}
891
892
public void testTextNoAA() throws Exception {
893
(new PerfMeter("TextNoAA")).exec(createPR(textRenderer)).report();
894
}
895
896
public void testTextLCD() throws Exception {
897
(new PerfMeter("TextLCD")).exec(createPR(textRenderer).configure(TextLCD)).report();
898
}
899
900
public void testTextGray() throws Exception {
901
(new PerfMeter("TextGray")).exec(createPR(textRenderer).configure(TextAA)).report();
902
}
903
904
public void testLargeTextNoAA() throws Exception {
905
(new PerfMeter("LargeTextNoAA")).exec(createPR(largeTextRenderer)).report();
906
}
907
908
public void testLargeTextLCD() throws Exception {
909
(new PerfMeter("LargeTextLCD")).exec(createPR(largeTextRenderer).configure(TextLCD)).report();
910
}
911
912
public void testLargeTextGray() throws Exception {
913
(new PerfMeter("LargeTextGray")).exec(createPR(largeTextRenderer).configure(TextAA)).report();
914
}
915
public void testWhiteTextNoAA() throws Exception {
916
(new PerfMeter("WhiteTextNoAA")).exec(createPR(whiteTextRenderer)).report();
917
}
918
919
public void testWhiteTextLCD() throws Exception {
920
(new PerfMeter("WhiteTextLCD")).exec(createPR(whiteTextRenderer).configure(TextLCD)).report();
921
}
922
923
public void testWhiteTextGray() throws Exception {
924
(new PerfMeter("WhiteTextGray")).exec(createPR(whiteTextRenderer).configure(TextAA)).report();
925
}
926
927
public void testArgbSwBlitImage() throws Exception {
928
(new PerfMeter("ArgbSwBlitImage")).exec(createPR(argbSwBlitImageRenderer)).report();
929
}
930
931
public void testBgrSwBlitImage() throws Exception {
932
(new PerfMeter("BgrSwBlitImage")).exec(createPR(bgrSwBlitImageRenderer)).report();
933
}
934
935
public void testArgbSurfaceBlitImage() throws Exception {
936
(new PerfMeter("ArgbSurfaceBlitImageRenderer")).exec(createPR(argbSurfaceBlitImageRenderer)).report();
937
}
938
939
public void testBgrSurfaceBlitImage() throws Exception {
940
(new PerfMeter("BgrSurfaceBlitImage")).exec(createPR(bgrSurfaceBlitImageRenderer)).report();
941
}
942
943
public void testFlatOval_XOR() throws Exception {
944
(new PerfMeter("FlatOval_XOR")).exec(createPR(flatRenderer).configure(XORMode)).report();
945
}
946
947
public void testRotatedBox_XOR() throws Exception {
948
(new PerfMeter("RotatedBox_XOR")).exec(createPR(flatBoxRotRenderer).configure(XORMode)).report();
949
}
950
951
public void testLines_XOR() throws Exception {
952
(new PerfMeter("Lines_XOR")).exec(createPR(segRenderer).configure(XORMode)).report();
953
}
954
955
public void testImage_XOR() throws Exception {
956
(new PerfMeter("Image_XOR")).exec(createPR(imgRenderer).configure(XORMode)).report();
957
}
958
959
public void testTextNoAA_XOR() throws Exception {
960
(new PerfMeter("TextNoAA_XOR")).exec(createPR(textRenderer).configure(XORMode)).report();
961
}
962
963
public void testTextLCD_XOR() throws Exception {
964
(new PerfMeter("TextLCD_XOR")).exec(createPR(textRenderer).configure(XORModeLCDText)).report();
965
}
966
967
public static void main(String[] args)
968
throws InvocationTargetException, IllegalAccessException, NoSuchMethodException
969
{
970
RenderPerfTest test = new RenderPerfTest();
971
972
if (args.length > 0) {
973
for (String testCase : args) {
974
Method m = RenderPerfTest.class.getDeclaredMethod("test" + testCase);
975
m.invoke(test);
976
}
977
} else {
978
Method[] methods = RenderPerfTest.class.getDeclaredMethods();
979
for (Method m : methods) {
980
if (m.getName().startsWith("test") && !ignoredTests.contains(m.getName())) {
981
m.invoke(test);
982
}
983
}
984
}
985
}
986
}
987
988