Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/performance/client/RenderPerfTest/src/renderperf/RenderPerfLCDTest.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.Color;
27
import java.awt.Dimension;
28
import java.awt.Frame;
29
import java.awt.Graphics;
30
import java.awt.Graphics2D;
31
import java.awt.RenderingHints;
32
import java.awt.Robot;
33
34
import java.awt.event.WindowAdapter;
35
import java.awt.event.WindowEvent;
36
import java.awt.geom.AffineTransform;
37
import java.awt.geom.Point2D;
38
import java.awt.geom.QuadCurve2D;
39
import java.awt.image.BufferedImage;
40
import java.io.IOException;
41
import java.lang.reflect.InvocationTargetException;
42
import java.lang.reflect.Method;
43
import java.util.HashSet;
44
import java.util.Objects;
45
import java.util.concurrent.CountDownLatch;
46
import java.util.concurrent.atomic.AtomicBoolean;
47
48
import javax.imageio.ImageIO;
49
import javax.swing.JPanel;
50
import javax.swing.SwingUtilities;
51
import javax.swing.Timer;
52
53
public class RenderPerfLCDTest {
54
private static HashSet<String> ignoredTests = new HashSet<>();
55
56
private final static int N = 1000;
57
private final static float WIDTH = 800;
58
private final static float HEIGHT = 800;
59
private final static float R = 25;
60
private final static int BW = 50;
61
private final static int BH = 50;
62
private final static int COUNT = 300;
63
private final static int DELAY = 10;
64
private final static int RESOLUTION = 5;
65
private final static int COLOR_TOLERANCE = 10;
66
private final static int MAX_MEASURE_TIME = 5000;
67
68
69
interface Configurable {
70
void configure(Graphics2D g2d);
71
}
72
73
interface Renderable {
74
void setup(Graphics2D g2d);
75
void render(Graphics2D g2d);
76
void update();
77
}
78
79
static class Particles {
80
private float[] bx;
81
private float[] by;
82
private float[] vx;
83
private float[] vy;
84
private float r;
85
private int n;
86
87
private float x0;
88
private float y0;
89
private float width;
90
private float height;
91
92
Particles(int n, float r, float x0, float y0, float width, float height) {
93
bx = new float[n];
94
by = new float[n];
95
vx = new float[n];
96
vy = new float[n];
97
this.n = n;
98
this.r = r;
99
this.x0 = x0;
100
this.y0 = y0;
101
this.width = width;
102
this.height = height;
103
for (int i = 0; i < n; i++) {
104
bx[i] = (float) (x0 + r + 0.1 + Math.random() * (width - 2 * r - 0.2 - x0));
105
by[i] = (float) (y0 + r + 0.1 + Math.random() * (height - 2 * r - 0.2 - y0));
106
vx[i] = 0.1f * (float) (Math.random() * 2 * r - r);
107
vy[i] = 0.1f * (float) (Math.random() * 2 * r - r);
108
}
109
110
}
111
112
void render(Graphics2D g2d, ParticleRenderer renderer) {
113
for (int i = 0; i < n; i++) {
114
renderer.render(g2d, i, bx, by, vx, vy);
115
}
116
}
117
118
void update() {
119
for (int i = 0; i < n; i++) {
120
bx[i] += vx[i];
121
if (bx[i] + r > width || bx[i] - r < x0) vx[i] = -vx[i];
122
by[i] += vy[i];
123
if (by[i] + r > height || by[i] - r < y0) vy[i] = -vy[i];
124
}
125
126
}
127
128
}
129
130
ParticleRenderable createPR(ParticleRenderer renderer) {
131
return new ParticleRenderable(renderer);
132
}
133
134
static class ParticleRenderable implements Renderable {
135
ParticleRenderer renderer;
136
Configurable configure;
137
138
ParticleRenderable(ParticleRenderer renderer, Configurable configure) {
139
this.renderer = renderer;
140
this.configure = configure;
141
}
142
143
ParticleRenderable(ParticleRenderer renderer) {
144
this(renderer, null);
145
}
146
147
@Override
148
public void setup(Graphics2D g2d) {
149
if (configure != null) configure.configure(g2d);
150
}
151
152
@Override
153
public void render(Graphics2D g2d) {
154
balls.render(g2d, renderer);
155
}
156
157
@Override
158
public void update() {
159
balls.update();
160
}
161
162
public ParticleRenderable configure(Configurable configure) {
163
this.configure = configure;
164
return this;
165
}
166
}
167
168
interface ParticleRenderer {
169
void render(Graphics2D g2d, int id, float[] x, float[] y, float[] vx, float[] vy);
170
171
}
172
173
static class WhiteTextParticleRenderer implements ParticleRenderer {
174
float r;
175
176
WhiteTextParticleRenderer(float r) {
177
this.r = r;
178
}
179
180
void setPaint(Graphics2D g2d, int id) {
181
g2d.setColor(Color.WHITE);
182
}
183
184
@Override
185
public void render(Graphics2D g2d, int id, float[] x, float[] y, float[] vx, float[] vy) {
186
setPaint(g2d, id);
187
g2d.drawString("The quick brown fox jumps over the lazy dog",
188
(int)(x[id] - r), (int)(y[id] - r));
189
g2d.drawString("The quick brown fox jumps over the lazy dog",
190
(int)(x[id] - r), (int)y[id]);
191
g2d.drawString("The quick brown fox jumps over the lazy dog",
192
(int)(x[id] - r), (int)(y[id] + r));
193
}
194
}
195
196
static class TextParticleRenderer extends WhiteTextParticleRenderer {
197
Color[] colors;
198
199
float r;
200
201
TextParticleRenderer(int n, float r) {
202
super(r);
203
colors = new Color[n];
204
this.r = r;
205
for (int i = 0; i < n; i++) {
206
colors[i] = new Color((float) Math.random(),
207
(float) Math.random(), (float) Math.random());
208
}
209
}
210
211
void setPaint(Graphics2D g2d, int id) {
212
g2d.setColor(colors[id % colors.length]);
213
}
214
}
215
216
static class PerfMeter {
217
private String name;
218
private int frame = 0;
219
220
private JPanel panel;
221
222
private long time;
223
private double execTime = 0;
224
private Color expColor = Color.RED;
225
AtomicBoolean waiting = new AtomicBoolean(false);
226
private double fps;
227
228
PerfMeter(String name) {
229
this.name = name;
230
}
231
232
PerfMeter exec(final Renderable renderable) throws Exception {
233
final CountDownLatch latch = new CountDownLatch(COUNT);
234
final CountDownLatch latchFrame = new CountDownLatch(1);
235
final long endTime = System.currentTimeMillis() + MAX_MEASURE_TIME;
236
237
final Frame f = new Frame();
238
f.addWindowListener(new WindowAdapter() {
239
@Override
240
public void windowClosed(WindowEvent e) {
241
latchFrame.countDown();
242
}
243
});
244
245
SwingUtilities.invokeAndWait(new Runnable() {
246
@Override
247
public void run() {
248
249
panel = new JPanel()
250
{
251
@Override
252
protected void paintComponent(Graphics g) {
253
254
super.paintComponent(g);
255
time = System.nanoTime();
256
Graphics2D g2d = (Graphics2D) g.create();
257
renderable.setup(g2d);
258
renderable.render(g2d);
259
g2d.setColor(expColor);
260
g.fillRect(0, 0, BW, BH);
261
}
262
};
263
264
panel.setPreferredSize(new Dimension((int)(WIDTH + BW), (int)(HEIGHT + BH)));
265
panel.setBackground(Color.BLACK);
266
f.add(panel);
267
//f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
268
f.pack();
269
f.setVisible(true);
270
}
271
});
272
Robot robot = new Robot();
273
274
Timer timer = new Timer(DELAY, e -> {
275
276
if (waiting.compareAndSet(false, true)) {
277
Color c = robot.getPixelColor(
278
panel.getTopLevelAncestor().getX() + panel.getTopLevelAncestor().getInsets().left + BW / 2,
279
panel.getTopLevelAncestor().getY() + panel.getTopLevelAncestor().getInsets().top + BW / 2);
280
if (isAlmostEqual(c, Color.BLUE)) {
281
expColor = Color.RED;
282
} else {
283
expColor = Color.BLUE;
284
}
285
renderable.update();
286
panel.getParent().repaint();
287
288
} else {
289
while (!isAlmostEqual(
290
robot.getPixelColor(
291
panel.getTopLevelAncestor().getX() + panel.getTopLevelAncestor().getInsets().left + BW/2,
292
panel.getTopLevelAncestor().getY() + panel.getTopLevelAncestor().getInsets().top + BH/2),
293
expColor))
294
{
295
try {
296
Thread.sleep(RESOLUTION);
297
} catch (InterruptedException ex) {
298
ex.printStackTrace();
299
}
300
}
301
time = System.nanoTime() - time;
302
execTime += time;
303
frame++;
304
waiting.set(false);
305
}
306
307
if (System.currentTimeMillis() < endTime) {
308
latch.countDown();
309
} else {
310
while(latch.getCount() > 0) latch.countDown();
311
}
312
});
313
timer.start();
314
latch.await();
315
SwingUtilities.invokeAndWait(() -> {
316
timer.stop();
317
f.setVisible(false);
318
f.dispose();
319
});
320
321
latchFrame.await();
322
if (execTime != 0 && frame != 0) {
323
fps = 1e9 / (execTime / frame);
324
} else {
325
fps = 0;
326
}
327
328
return this;
329
}
330
331
private void report() {
332
System.err.println(name + " : " + String.format("%.2f FPS", fps));
333
}
334
335
private boolean isAlmostEqual(Color c1, Color c2) {
336
return Math.abs(c1.getRed() - c2.getRed()) < COLOR_TOLERANCE ||
337
Math.abs(c1.getGreen() - c2.getGreen()) < COLOR_TOLERANCE ||
338
Math.abs(c1.getBlue() - c2.getBlue()) < COLOR_TOLERANCE;
339
340
}
341
}
342
343
private static final Particles balls = new Particles(N, R, BW, BH, WIDTH, HEIGHT);
344
private static final ParticleRenderer textRenderer = new TextParticleRenderer(N, R);
345
346
private static final Configurable TextLCD = (Graphics2D g2d) ->
347
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
348
RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
349
350
public void testTextBubblesLCD() throws Exception {
351
(new PerfMeter("TextLCD")).exec(createPR(textRenderer).configure(TextLCD)).report();
352
}
353
354
public static void main(String[] args)
355
throws InvocationTargetException, IllegalAccessException, NoSuchMethodException
356
{
357
RenderPerfLCDTest test = new RenderPerfLCDTest();
358
359
if (args.length > 0) {
360
for (String testCase : args) {
361
Method m = RenderPerfLCDTest.class.getDeclaredMethod(testCase);
362
m.invoke(test);
363
}
364
} else {
365
Method[] methods = RenderPerfLCDTest.class.getDeclaredMethods();
366
for (Method m : methods) {
367
if (m.getName().startsWith("test") && !ignoredTests.contains(m.getName())) {
368
m.invoke(test);
369
}
370
}
371
}
372
}
373
}
374
375