diff options
author | Francis Kung <fkung@redhat.com> | 2006-06-16 20:46:53 +0000 |
---|---|---|
committer | Francis Kung <fkung@redhat.com> | 2006-06-16 20:46:53 +0000 |
commit | 6c705c22bdc03603f10d407212aa0f24796a63a2 (patch) | |
tree | 593e0a8ee6e8c83e244f797d4c9f641350df104e /examples | |
parent | 1fbedaed0d3cf71b4e9b0eeb82f7fef5acdbd016 (diff) | |
download | classpath-6c705c22bdc03603f10d407212aa0f24796a63a2.tar.gz |
2006-06-16 Francis Kung <fkung@redhat.com>
* examples/gnu/classpath/examples/swing/Demo.java:
(mkButtonBar): Rename FillRect to JNIOverhead.
(mkMenuBar): Rename FillRect to JNIOverhead.
* examples/gnu/classpath/examples/swing/FillRect.java: Removed.
* examples/gnu/classpath/examples/java2d/aicas.png: New file.
* examples/gnu/classpath/examples/java2d/J2dBenchmark.java: New file.
* examples/gnu/classpath/examples/java2d/JNIOverhead.java:
Moved from old FillRect.
* examples/gnu/classpath/examples/java2d/palme.java: New file.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/gnu/classpath/examples/java2d/J2dBenchmark.java | 1169 | ||||
-rw-r--r-- | examples/gnu/classpath/examples/java2d/JNIOverhead.java (renamed from examples/gnu/classpath/examples/swing/FillRect.java) | 16 | ||||
-rw-r--r-- | examples/gnu/classpath/examples/java2d/aicas.png | bin | 0 -> 2320 bytes | |||
-rw-r--r-- | examples/gnu/classpath/examples/java2d/palme.gif | bin | 0 -> 502 bytes | |||
-rw-r--r-- | examples/gnu/classpath/examples/swing/Demo.java | 10 |
5 files changed, 1184 insertions, 11 deletions
diff --git a/examples/gnu/classpath/examples/java2d/J2dBenchmark.java b/examples/gnu/classpath/examples/java2d/J2dBenchmark.java new file mode 100644 index 000000000..29aeef8ad --- /dev/null +++ b/examples/gnu/classpath/examples/java2d/J2dBenchmark.java @@ -0,0 +1,1169 @@ +/* J2dBenchmark.java -- Benchmarking utility for java2d, + based on the Aicas AWT benchmarker + Copyright (C) 2006 Free Software Foundation, Inc. + + This file is part of GNU Classpath examples. + + GNU Classpath is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + GNU Classpath is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU Classpath; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. */ + + +package gnu.classpath.examples.java2d; + +import java.awt.BorderLayout; +import java.awt.Canvas; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Frame; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Image; +import java.awt.Insets; +import java.awt.Label; +import java.awt.Panel; +import java.awt.Rectangle; +import java.awt.Toolkit; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.awt.geom.Arc2D; +import java.awt.geom.CubicCurve2D; +import java.awt.geom.Ellipse2D; +import java.awt.geom.GeneralPath; +import java.awt.geom.Line2D; +import java.awt.geom.QuadCurve2D; +import java.awt.geom.Rectangle2D; +import java.awt.geom.RoundRectangle2D; +import java.net.URL; +import java.util.Iterator; +import java.util.Map; +import java.util.StringTokenizer; +import java.util.TreeMap; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class J2dBenchmark + extends Panel +{ + /** + * Default number of test-iterations. + */ + private static final int DEFAULT_TEST_SIZE = 1000; + + /** + * Default screen size. + */ + private static final int DEFAULT_SCREEN_WIDTH = 320; + + private static final int DEFAULT_SCREEN_HEIGHT = 240; + + /** + * Java2D tests. + */ + private static final int J2DTEST_ARC = 1 << 0; + + private static final int J2DTEST_CUBICCURVE = 1 << 1; + + private static final int J2DTEST_ELLIPSE = 1 << 2; + + private static final int J2DTEST_GENERALPATH = 1 << 3; + + private static final int J2DTEST_LINE = 1 << 4; + + private static final int J2DTEST_QUADCURVE = 1 << 5; + + private static final int J2DTEST_RECTANGLE = 1 << 6; + + private static final int J2DTEST_ROUNDRECTANGLE = 1 << 7; + + private static final int J2DTEST_IMAGE = 1 << 8; + + private static final int J2DTEST_NONE = 0; + + /* + private static final int J2DTEST_ALL = J2DTEST_ARC | J2DTEST_CUBICCURVE + | J2DTEST_ELLIPSE + | J2DTEST_GENERALPATH | J2DTEST_LINE + | J2DTEST_QUADCURVE + | J2DTEST_RECTANGLE + | J2DTEST_ROUNDRECTANGLE + | J2DTEST_IMAGE; + */ + private static final int J2DTEST_ALL = J2DTEST_ARC | J2DTEST_CUBICCURVE + | J2DTEST_ELLIPSE + | J2DTEST_LINE + | J2DTEST_QUADCURVE + | J2DTEST_RECTANGLE + | J2DTEST_ROUNDRECTANGLE + | J2DTEST_IMAGE; + + int iterations = 1; + + private int screenWidth = DEFAULT_SCREEN_WIDTH; + + private int screenHeight = DEFAULT_SCREEN_HEIGHT; + + boolean doubleBufferFlag = true; + + private int awtTests = J2DTEST_ALL; + + private Label testLabel; + + private String testContext = ""; + + Logger logger = Logger.getLogger("J2dGraphicsBenchmark"); + + private Image pngTestImage; + + private Image gifTestImage; + + private TestSet testSetMap = new TestSet(); + + public J2dBenchmark() + { + pngTestImage = loadImage("aicas.png"); + gifTestImage = loadImage("palme.gif"); + + setLayout(new BorderLayout()); + testLabel = new Label(); + add(testLabel, BorderLayout.NORTH); + add(new GraphicsTest(), BorderLayout.CENTER); + } + + void setTestContext(String testName) + { + logger.logp(Level.INFO, "J2dGraphicsBenchmark", "recordTest", + "--- Starting new test context: " + testName); + testContext = testName; + testLabel.setText(testName); + } + + private void recordTest(String testName, long time) + { + logger.logp(Level.INFO, "J2dGraphicsBenchmark", "recordTest", + testContext + ": " + testName + " duration (ms): " + time); + TestRecorder recorder = testSetMap.getTest(testName); + if (recorder == null) + { + recorder = new TestRecorder(testName); + testSetMap.putTest(testName, recorder); + } + recorder.addRun(time); + } + + void printReport() + { + for (Iterator i = testSetMap.testIterator(); i.hasNext();) + { + TestRecorder recorder = testSetMap.getTest((String) i.next()); + System.out.println("TEST " + recorder.getTestName() + ": average " + + recorder.getAverage() + "ms [" + + recorder.getMinTime() + "-" + + recorder.getMaxTime() + "]"); + } + } + + public static void main(String[] args) + { + int awtTests; + int i; + boolean endOfOptionsFlag; + J2dBenchmark speed = new J2dBenchmark(); + + // Parse arguments. + i = 0; + endOfOptionsFlag = false; + awtTests = J2DTEST_NONE; + while (i < args.length) + { + if (! endOfOptionsFlag) + { + if (args[i].equals("--help") || args[i].equals("-help") + || args[i].equals("-h")) + { + System.out.println("Usage: J2dBenchmark [<options>] [<test> ...]"); + System.out.println(""); + System.out.println("Options: -i|--iterations=<n|-1> - number of iterations (-1 is infinite; default " + + speed.iterations + ")"); + System.out.println(" -w|--width=<n> - screen width; default " + + DEFAULT_SCREEN_WIDTH); + System.out.println(" -h|--height=<n> - screen height; default " + + DEFAULT_SCREEN_HEIGHT); + System.out.println(" -n|--noDoubleBuffer - disable double-buffering test"); + System.out.println(""); + System.out.println("Tests: arc"); + System.out.println(" cubiccurve"); + System.out.println(" ellipse"); + // System.out.println(" generalpath"); + System.out.println(" line"); + System.out.println(" quadcurve"); + System.out.println(" rectangle"); + System.out.println(" roundrectangle"); + System.out.println(" image"); + System.exit(1); + } + else if ((args[i].startsWith("-i=") || args[i].startsWith("--iterations="))) + { + speed.iterations = Integer.parseInt(args[i].substring(args[i].indexOf('=') + 1)); + i += 1; + continue; + } + else if ((args[i].equals("-i") || args[i].equals("--iterations"))) + { + if ((i + 1) >= args.length) + { + System.err.println("ERROR: No argument given for option '" + + args[i] + "'!"); + System.exit(2); + } + speed.iterations = Integer.parseInt(args[i + 1]); + i += 2; + continue; + } + else if ((args[i].startsWith("-w=") || args[i].startsWith("--width="))) + { + speed.screenWidth = Integer.parseInt(args[i].substring(args[i].indexOf('=') + 1)); + i += 1; + continue; + } + else if ((args[i].equals("-w") || args[i].equals("--width"))) + { + if ((i + 1) >= args.length) + { + System.err.println("ERROR: No argument given for option '" + + args[i] + "'!"); + System.exit(2); + } + speed.screenWidth = Integer.parseInt(args[i + 1]); + i += 2; + continue; + } + else if ((args[i].startsWith("-h=") || args[i].startsWith("--height="))) + { + speed.screenHeight = Integer.parseInt(args[i].substring(args[i].indexOf('=') + 1)); + i += 1; + continue; + } + else if ((args[i].equals("-h") || args[i].equals("--height"))) + { + if ((i + 1) >= args.length) + { + System.err.println("ERROR: No argument given for option '" + + args[i] + "'!"); + System.exit(2); + } + speed.screenHeight = Integer.parseInt(args[i + 1]); + i += 2; + continue; + } + else if ((args[i].equals("-n") || args[i].equals("--noDoubleBuffer"))) + { + speed.doubleBufferFlag = false; + i += 1; + continue; + } + else if (args[i].equals("--")) + { + endOfOptionsFlag = true; + i += 1; + continue; + } + else if (args[i].startsWith("-")) + { + System.err.println("ERROR: Unknown option '" + args[i] + "'!"); + System.exit(2); + } + } + StringTokenizer tokenizer = new StringTokenizer(args[i], " +,"); + while (tokenizer.hasMoreTokens()) + { + String s = tokenizer.nextToken().toLowerCase(); + if (s.equals("arc")) + awtTests |= J2DTEST_ARC; + else if (s.equals("cubiccurve")) + awtTests |= J2DTEST_CUBICCURVE; + else if (s.equals("ellipse")) + awtTests |= J2DTEST_ELLIPSE; + else if (s.equals("generalpath")) + awtTests |= J2DTEST_GENERALPATH; + else if (s.equals("line")) + awtTests |= J2DTEST_LINE; + else if (s.equals("quadcurve")) + awtTests |= J2DTEST_QUADCURVE; + else if (s.equals("rectangle")) + awtTests |= J2DTEST_RECTANGLE; + else if (s.equals("roundrectangle")) + awtTests |= J2DTEST_ROUNDRECTANGLE; + else if (s.equals("image")) + awtTests |= J2DTEST_IMAGE; + else + { + System.err.println("Unknown AWT test '" + s + "'!"); + System.exit(2); + } + } + i += 1; + } + if (awtTests != J2DTEST_NONE) + speed.awtTests = awtTests; + + // Create graphics. + final Frame frame = new Frame("J2dGraphicsBenchmark"); + + frame.addWindowListener(new WindowAdapter() + { + public void windowClosing(WindowEvent e) + { + frame.setVisible(false); + System.exit(0); + } + }); + + frame.add(speed, BorderLayout.CENTER); + frame.setSize(speed.screenWidth, speed.screenHeight); + frame.setVisible(true); + + // Insets are correctly set only after the native peer was created. + Insets insets = frame.getInsets(); + // The internal size of the frame should be 320x240. + frame.setSize(320 + insets.right + insets.left, 240 + insets.top + + insets.bottom); + } + + private Image loadImage(String imageName) + { + Image result = null; + logger.logp(Level.INFO, "J2dGraphicsBenchmark", "loadImage", + "Loading image: " + imageName); + URL url = getClass().getResource(imageName); + if (url != null) + { + result = Toolkit.getDefaultToolkit().getImage(url); + prepareImage(result, this); + } + else + { + logger.logp(Level.WARNING, "J2dGraphicsBenchmark", "loadImage", + "Could not locate image resource in class path: " + + imageName); + } + return result; + } + + /** + * Executes the test methods. + * + * @param g The Graphics2D object that is used to paint. + * @param size The size of the canvas. + */ + void runTestSet(Graphics2D g, Dimension size) + { + if ((awtTests & J2DTEST_ARC) != 0) + { + test_drawArc(g, size); + test_fillArc(g, size); + } + + if ((awtTests & J2DTEST_CUBICCURVE) != 0) + { + test_drawCubicCurve(g, size); + } + + if ((awtTests & J2DTEST_ELLIPSE) != 0) + { + test_drawEllipse(g, size); + test_fillEllipse(g, size); + } + + if ((awtTests & J2DTEST_GENERALPATH) != 0) + { + // Current implementation doesn't work + test_drawGeneralPath(g, size); + test_fillGeneralPath(g, size); + } + + if ((awtTests & J2DTEST_LINE) != 0) + { + test_drawLine(g, size); + } + + if ((awtTests & J2DTEST_QUADCURVE) != 0) + { + test_drawQuadCurve(g, size); + } + + if ((awtTests & J2DTEST_RECTANGLE) != 0) + { + test_drawRectangle(g, size); + test_fillRectangle(g, size); + } + + if ((awtTests & J2DTEST_ROUNDRECTANGLE) != 0) + { + test_drawRoundRectangle(g, size); + test_fillRoundRectangle(g, size); + } + + if ((awtTests & J2DTEST_IMAGE) != 0) + { + test_drawImage(g, size); + test_drawTransparentImage(g, size); + } + } + + /** + * Gets a new random Color. + * + * @returna new random Color + */ + private Color getNextColor() + { + return new Color((int) (Math.random() * 254) + 1, + (int) (Math.random() * 254) + 1, + (int) (Math.random() * 254) + 1); + } + + /** + * Draws random arcs within the given dimensions. + * + * @param g The Graphics2D object that is used to paint. + * @param size The size of the canvas. + */ + private void test_drawArc(Graphics2D g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + int minSize; + long startTime; + long endTime; + minSize = 10; + startTime = System.currentTimeMillis(); + for (int i = 0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int x = (int) (Math.random() * (size.width - minSize + 1)); + int y = (int) (Math.random() * (size.height - minSize + 1)); + int width = (int) (Math.random() * (size.width - x - minSize) + minSize); + int height = (int) (Math.random() * (size.height - y - minSize) + minSize); + int startAngle = (int) (Math.random() * 360); + int arcAngle = (int) (Math.random() * 360 - startAngle); + + Arc2D arc = new Arc2D.Double(x, y, width, height, startAngle, arcAngle, + Arc2D.OPEN); + g.draw(arc); + } + endTime = System.currentTimeMillis(); + recordTest("draw(Arc2D.Double) " + maxTests + " times", + (endTime - startTime)); + } + + /** + * Draws random filled arcs within the given dimensions. + * + * @param g The Graphics2D object that is used to paint. + * @param size The size of the canvas. + */ + private void test_fillArc(Graphics2D g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + int minSize; + long startTime; + long endTime; + minSize = 10; + startTime = System.currentTimeMillis(); + for (int i = 0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int x = (int) (Math.random() * (size.width - minSize + 1)); + int y = (int) (Math.random() * (size.height - minSize + 1)); + int width = (int) (Math.random() * (size.width - x - minSize) + minSize); + int height = (int) (Math.random() * (size.height - y - minSize) + minSize); + int startAngle = (int) (Math.random() * 360); + int arcAngle = (int) (Math.random() * 360); + + Arc2D arc = new Arc2D.Double(x, y, width, height, startAngle, arcAngle, + Arc2D.OPEN); + g.fill(arc); + } + endTime = System.currentTimeMillis(); + recordTest("fill(Arc2D.Double) " + maxTests + " times", + (endTime - startTime)); + } + + /** + * Draws random cubic curves within the given dimensions. + * + * @param g The Graphics2D object that is used to paint. + * @param size The size of the canvas. + */ + private void test_drawCubicCurve(Graphics2D g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + int minSize = 10; + long startTime = System.currentTimeMillis(); + for (int i = 0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int x1 = (int) (Math.random() * (size.width - minSize)); + int y1 = (int) (Math.random() * (size.height - minSize)); + int xc1 = (int) (Math.random() * (size.width - minSize)); + int yc1 = (int) (Math.random() * (size.height - minSize)); + int xc2 = (int) (Math.random() * (size.width - minSize)); + int yc2 = (int) (Math.random() * (size.height - minSize)); + int x2 = (int) (Math.random() * (size.width - minSize)); + int y2 = (int) (Math.random() * (size.height - minSize)); + + CubicCurve2D curve = new CubicCurve2D.Double(x1, y1, xc1, yc1, xc2, + yc2, x2, y2); + g.draw(curve); + } + long endTime = System.currentTimeMillis(); + recordTest("draw(CubicCurve2D.Double) " + maxTests + " times", + (endTime - startTime)); + } + + /** + * Draws random ellipses within the given dimensions. + * + * @param g The Graphics2D object that is used to paint. + * @param size The size of the canvas. + */ + private void test_drawEllipse(Graphics2D g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + int minSize = 10; + long startTime = System.currentTimeMillis(); + for (int i = 0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int x1 = (int) (Math.random() * (size.width - minSize)); + int y1 = (int) (Math.random() * (size.height - minSize)); + int x2 = (int) (Math.random() * (size.width - minSize)); + int y2 = (int) (Math.random() * (size.height - minSize)); + Ellipse2D ellipse = new Ellipse2D.Double(x1, y1, x2, y2); + g.draw(ellipse); + } + long endTime = System.currentTimeMillis(); + recordTest("draw(Ellipse.Double) " + maxTests + " times", + (endTime - startTime)); + } + + /** + * Draws random ellipses within the given dimensions. + * + * @param g The Graphics2D object that is used to paint. + * @param size The size of the canvas. + */ + private void test_fillEllipse(Graphics2D g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + int minSize = 10; + long startTime = System.currentTimeMillis(); + for (int i = 0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int x1 = (int) (Math.random() * (size.width - minSize)); + int y1 = (int) (Math.random() * (size.height - minSize)); + int x2 = (int) (Math.random() * (size.width - minSize)); + int y2 = (int) (Math.random() * (size.height - minSize)); + Ellipse2D ellipse = new Ellipse2D.Double(x1, y1, x2, y2); + g.fill(ellipse); + } + long endTime = System.currentTimeMillis(); + recordTest("fill(Ellipse.Double) " + maxTests + " times", + (endTime - startTime)); + } + + // TODO: fix the GeneralPath methods. + /** + * Draws random polygons within the given dimensions. + * + * @param g The Graphics2D object that is used to paint. + * @param size The size of the canvas. + */ + private void test_drawGeneralPath(Graphics2D g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + long startTime = System.currentTimeMillis(); + + for (int i = 0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int points = (int) (Math.random() * 6) + 2; + GeneralPath shape = new GeneralPath(); + shape.moveTo((float) Math.random() * (size.width), + (float) Math.random() * (size.height)); + for (int j = 0; j < points; j += 1) + { + shape.lineTo((float) (Math.random() * (size.width)), + (float) (Math.random() * (size.height))); + } + g.draw(shape); + } + long endTime = System.currentTimeMillis(); + recordTest("draw(GeneralPath) " + maxTests + " times", + (endTime - startTime)); + } + + /** + * Draws random filled polygons within the given dimensions. + * + * @param g The Graphics2D object that is used to paint. + * @param size The size of the canvas. + */ + private void test_fillGeneralPath(Graphics2D g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + long startTime = System.currentTimeMillis(); + + GeneralPath shape = new GeneralPath(); + shape.moveTo((float) Math.random() * (size.width), (float) Math.random() + * (size.height)); + + for (int i = 0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int points = (int) (Math.random() * 6) + 2; + for (int j = 0; j < points; j += 1) + { + shape.lineTo((float) (Math.random() * (size.width)), + (float) (Math.random() * (size.height))); + } + g.fill(shape); + } + long endTime = System.currentTimeMillis(); + recordTest("fill(GeneralPath) " + maxTests + " times", + (endTime - startTime)); + } + + /** + * Draws random lines within the given dimensions. + * + * @param g The Graphics2D object that is used to paint. + * @param size The size of the canvas. + */ + private void test_drawLine(Graphics2D g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + int minSize = 10; + long startTime = System.currentTimeMillis(); + for (int i = 0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int x1 = (int) (Math.random() * (size.width - minSize)); + int y1 = (int) (Math.random() * (size.height - minSize)); + int x2 = (int) (Math.random() * (size.width - minSize)); + int y2 = (int) (Math.random() * (size.height - minSize)); + Line2D line = new Line2D.Double(x1, y1, x2, y2); + g.draw(line); + } + long endTime = System.currentTimeMillis(); + recordTest("draw(Line2D.Double) " + maxTests + " times", + (endTime - startTime)); + } + + /** + * Draws random quadratic curves within the given dimensions. + * + * @param g The Graphics2D object that is used to paint. + * @param size The size of the canvas. + */ + private void test_drawQuadCurve(Graphics2D g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + int minSize = 10; + long startTime = System.currentTimeMillis(); + for (int i = 0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int x1 = (int) (Math.random() * (size.width - minSize)); + int y1 = (int) (Math.random() * (size.height - minSize)); + int xc = (int) (Math.random() * (size.width - minSize)); + int yc = (int) (Math.random() * (size.height - minSize)); + int x2 = (int) (Math.random() * (size.width - minSize)); + int y2 = (int) (Math.random() * (size.height - minSize)); + + QuadCurve2D curve = new QuadCurve2D.Double(x1, y1, xc, yc, x2, y2); + g.draw(curve); + } + long endTime = System.currentTimeMillis(); + recordTest("draw(QuadCurve2D.Double) " + maxTests + " times", + (endTime - startTime)); + } + + /** + * Draws random rectangles within the given dimensions. + * + * @param g The Graphics2D object that is used to paint. + * @param size The size of the canvas. + */ + private void test_drawRectangle(Graphics2D g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + int minSize = 10; + long startTime = System.currentTimeMillis(); + for (int i = 0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int x1 = (int) (Math.random() * (size.width - minSize)); + int y1 = (int) (Math.random() * (size.height - minSize)); + int x2 = (int) (Math.random() * (size.width - minSize)); + int y2 = (int) (Math.random() * (size.height - minSize)); + Rectangle2D rect = new Rectangle2D.Double(x1, y1, x2, y2); + g.draw(rect); + } + long endTime = System.currentTimeMillis(); + recordTest("draw(Rectangle.Double) " + maxTests + " times", + (endTime - startTime)); + } + + /** + * Draws random rectangles within the given dimensions. + * + * @param g The Graphics2D object that is used to paint. + * @param size The size of the canvas. + */ + private void test_fillRectangle(Graphics2D g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + int minSize = 10; + long startTime = System.currentTimeMillis(); + for (int i = 0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int x1 = (int) (Math.random() * (size.width - minSize)); + int y1 = (int) (Math.random() * (size.height - minSize)); + int x2 = (int) (Math.random() * (size.width - minSize)); + int y2 = (int) (Math.random() * (size.height - minSize)); + Rectangle2D rect = new Rectangle2D.Double(x1, y1, x2, y2); + g.fill(rect); + } + long endTime = System.currentTimeMillis(); + recordTest("fill(Rectangle.Double) " + maxTests + " times", + (endTime - startTime)); + } + + /** + * Draws random rounded rectangles within the given dimensions. + * + * @param g The Graphics2D object that is used to paint. + * @param size The size of the canvas. + */ + private void test_drawRoundRectangle(Graphics2D g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + int minSize; + long startTime; + long endTime; + minSize = 10; + startTime = System.currentTimeMillis(); + for (int i = 0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int x = (int) (Math.random() * (size.width - minSize + 1)); + int y = (int) (Math.random() * (size.height - minSize + 1)); + int width = (int) (Math.random() * (size.width - x - minSize) + minSize); + int height = (int) (Math.random() * (size.height - y - minSize) + minSize); + int arcWidth = (int) (Math.random() * (width - 1) + 1); + int arcHeight = (int) (Math.random() * (height - 1) + 5); + RoundRectangle2D rect = new RoundRectangle2D.Double(x, y, width, + height, arcWidth, + arcHeight); + g.draw(rect); + } + endTime = System.currentTimeMillis(); + recordTest("draw(RoundRectangle.Double) " + maxTests + " times", + (endTime - startTime)); + } + + /** + * Draws random filled rounded rectangles within the given dimensions. + * + * @param g The Graphics2D object that is used to paint. + * @param size The size of the canvas. + */ + private void test_fillRoundRectangle(Graphics2D g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + int minSize; + long startTime; + long endTime; + minSize = 10; + startTime = System.currentTimeMillis(); + for (int i = 0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int x = (int) (Math.random() * (size.width - minSize + 1)); + int y = (int) (Math.random() * (size.height - minSize + 1)); + int width = (int) (Math.random() * (size.width - x - minSize) + minSize); + int height = (int) (Math.random() * (size.height - y - minSize) + minSize); + int arcWidth = (int) (Math.random() * (width - 1) + 1); + int arcHeight = (int) (Math.random() * (height - 1) + 5); + RoundRectangle2D rect = new RoundRectangle2D.Double(x, y, width, + height, arcWidth, + arcHeight); + g.fill(rect); + } + endTime = System.currentTimeMillis(); + recordTest("fill(RoundRectangle.Double) " + maxTests + " times", + (endTime - startTime)); + } + + /** + * Draws random images within the given dimensions. + * + * @param g The Graphics2D object that is used to paint. + * @param size The size of the canvas. + */ + private void test_drawImage(Graphics2D g, Dimension size) + { + if (gifTestImage == null) + { + logger.logp(Level.WARNING, "J2dGraphicsBenchmark", "runTestSet", + "Skipping 'test_drawImage' due to missing resource."); + return; + } + + int maxTests = DEFAULT_TEST_SIZE / 2; + if (maxTests == 0) + maxTests = 1; + int imageWidth = gifTestImage.getWidth(this); + int imageHeight = gifTestImage.getHeight(this); + long startTime = System.currentTimeMillis(); + for (int i = 0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int x = (int) (Math.random() * (size.width - imageWidth + 1)); + int y = (int) (Math.random() * (size.height - imageHeight + 1)); + g.drawImage(gifTestImage, x, y, this); + } + long endTime = System.currentTimeMillis(); + recordTest("drawImage " + maxTests + " times", (endTime - startTime)); + } + + /** + * Draws random transparent images within the given dimensions. + * + * @param g The Graphics object that is used to paint. + * @param size The size of the canvas. + */ + private void test_drawTransparentImage(Graphics2D g, Dimension size) + { + if (pngTestImage == null) + { + logger.logp(Level.WARNING, "AicasGraphicsBenchmark", "runTestSet", + "Skipping 'test_drawTransparentImage' due to missing resource."); + return; + } + + int maxTests = DEFAULT_TEST_SIZE / 5; + if (maxTests == 0) + maxTests = 1; + int imageWidth = pngTestImage.getWidth(this); + int imageHeight = pngTestImage.getHeight(this); + long startTime = System.currentTimeMillis(); + for (int i = 0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int x = (int) (Math.random() * (size.width - imageWidth + 1)); + int y = (int) (Math.random() * (size.height - imageHeight + 1)); + g.drawImage(pngTestImage, x, y, this); + } + long endTime = System.currentTimeMillis(); + recordTest("draw transparent image " + maxTests + " times", + (endTime - startTime)); + } + + private class GraphicsTest + extends Canvas + implements Runnable + { + Thread paintThread; + + boolean done = false; + + boolean doPaint = false; + + boolean withClipping = false; + + public GraphicsTest() + { + paintThread = new Thread(this); + paintThread.start(); + } + + public void run() + { + int runCount = 0; + while (! done) + { + runCount++; + + try + { + synchronized (this) + { + while (! doPaint) + { + try + { + wait(200); + } + catch (InterruptedException exception) + { + return; + } + } + } + + if (iterations != 0) + System.out.println("--- run...(" + + runCount + + "/" + + iterations + + ") ------------------------------------------------------"); + + Graphics g = getGraphics(); + Dimension size = getSize(); + logger.logp(Level.INFO, "J2dGraphicsBenchmark.GraphicsTest", + "run", "Start testing non-double-buffered drawing"); + runSet_noClipping((Graphics2D) g, size); + runSet_zeroClipping((Graphics2D) g, size); + runSet_withClipping((Graphics2D) g, size); + g.dispose(); + + if (doubleBufferFlag) + { + logger.logp(Level.INFO, "J2dGraphicsBenchmark.GraphicsTest", + "run", "Start testing double-buffered drawing"); + Graphics canvas = getGraphics(); + Image doublebuffer = createImage(size.width, size.height); + g = doublebuffer.getGraphics(); + runSet_noClipping((Graphics2D) g, size); + g.dispose(); + canvas.drawImage(doublebuffer, 0, 0, this); + + g = doublebuffer.getGraphics(); + runSet_withClipping((Graphics2D) g, size); + g.dispose(); + canvas.drawImage(doublebuffer, 0, 0, this); + + g = doublebuffer.getGraphics(); + runSet_zeroClipping((Graphics2D) g, size); + g.dispose(); + canvas.drawImage(doublebuffer, 0, 0, this); + canvas.dispose(); + } + + printReport(); + + if (iterations != 1) + { + if (iterations != - 1) + iterations--; + } + else + { + System.out.println("--- done --------------------------------------------------------"); + synchronized (this) + { + doPaint = false; + } + done = true; + } + } + catch (Error error) + { + System.err.println("Error: " + error); + System.exit(129); + } + } + System.exit(0); + } + + private void runSet_zeroClipping(Graphics2D g, Dimension size) + { + int clipped_width; + int clipped_height; + int clipped_x; + int clipped_y; + + clipped_width = 0; + clipped_height = 0; + clipped_x = (size.width) / 2; + clipped_y = (size.height) / 2; + + Rectangle fullWindow = new Rectangle(0, 0, size.width, size.height); + g.setClip(fullWindow); + g.setColor(Color.BLACK); + g.fill(fullWindow); + + Rectangle windowBorder = new Rectangle(0, 0, size.width - 1, + size.width - 1); + g.setColor(Color.WHITE); + g.draw(windowBorder); + + Rectangle innerBorder = new Rectangle(clipped_x - 1, clipped_y - 1, + clipped_width + 2, + clipped_height + 2); + g.fill(innerBorder); + + Rectangle innerBox = new Rectangle(clipped_x, clipped_y, clipped_width, + clipped_height); + g.clip(innerBox); + g.setColor(Color.BLACK); + g.fill(fullWindow); + + setTestContext("clipping to zero"); + + runTestSet(g, size); + } + + private void runSet_withClipping(Graphics2D g, Dimension size) + { + int clipped_width = 2 * size.width / 3; + int clipped_height = 2 * size.height / 3; + int clipped_x = (size.width - clipped_width) / 2; + int clipped_y = (size.height - clipped_height) / 2; + + Rectangle fullWindow = new Rectangle(0, 0, size.width, size.height); + g.setClip(fullWindow); + + g.setColor(Color.BLACK); + g.fill(fullWindow); + + Rectangle windowBorder = new Rectangle(0, 0, size.width - 1, + size.height - 1); + g.setColor(Color.GREEN); + g.draw(windowBorder); + + Rectangle innerBorder = new Rectangle(clipped_x - 1, clipped_y - 1, + clipped_width + 2, + clipped_height + 2); + g.setColor(Color.WHITE); + g.fill(innerBorder); + + Rectangle innerBox = new Rectangle(clipped_x, clipped_y, clipped_width, + clipped_height); + g.clip(innerBox); + + g.setColor(Color.BLACK); + g.fill(fullWindow); + + setTestContext("with clipping"); + + runTestSet(g, size); + } + + public void runSet_noClipping(Graphics2D g, Dimension size) + { + Rectangle fullWindow = new Rectangle(0, 0, size.width, size.height); + g.setColor(Color.BLACK); + g.fill(fullWindow); + + setTestContext("without clipping"); + + runTestSet(g, size); + } + + public void paint(Graphics g) + { + synchronized (this) + { + doPaint = true; + notify(); + } + } + } +} + +class TestContext +{ +} + +class TestSet +{ + private Map testsMap = new TreeMap(); + + public void putTest(String testName, TestRecorder recoder) + { + testsMap.put(testName, recoder); + } + + public TestRecorder getTest(String testName) + { + return (TestRecorder) testsMap.get(testName); + } + + public Iterator testIterator() + { + return testsMap.keySet().iterator(); + } +} + +class TestRecorder +{ + String test; + + long totalTime = 0; + + long minTime = Long.MAX_VALUE; + + long maxTime = Long.MIN_VALUE; + + int runCount = 0; + + /** + * @return Returns the maxTime. + */ + public final long getMaxTime() + { + return maxTime; + } + + /** + * @return Returns the minTime. + */ + public final long getMinTime() + { + return minTime; + } + + /** + * @return Returns the test name. + */ + public final String getTestName() + { + return test; + } + + public final double getAverage() + { + return ((double) totalTime) / ((double) runCount); + } + + public TestRecorder(String testName) + { + test = testName; + } + + public void addRun(long time) + { + totalTime += time; + if (minTime > time) + minTime = time; + if (maxTime < time) + maxTime = time; + runCount += 1; + } +} diff --git a/examples/gnu/classpath/examples/swing/FillRect.java b/examples/gnu/classpath/examples/java2d/JNIOverhead.java index 7c6f140a0..efa57d2a3 100644 --- a/examples/gnu/classpath/examples/swing/FillRect.java +++ b/examples/gnu/classpath/examples/java2d/JNIOverhead.java @@ -1,4 +1,4 @@ -/* FillRect.java - demonstrator for classpath/gcj fillrect performance issue +/* JNIOverhead.java - demonstrator for classpath/gcj fillrect performance issue Copyright (C) 2006 Free Software Foundation, Inc. This file is part of GNU Classpath examples. @@ -18,7 +18,9 @@ along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -package gnu.classpath.examples.swing; +package gnu.classpath.examples.java2d; + +import gnu.classpath.examples.swing.DemoFactory; import java.awt.BorderLayout; import java.awt.Color; @@ -38,12 +40,12 @@ import javax.swing.SwingUtilities; /** * @author Norman Hendrich */ -public class FillRect +public class JNIOverhead extends JPanel implements ActionListener { - static FillRect fillRectDemo; + static JNIOverhead fillRectDemo; LCDCanvas lcd; Worker worker; @@ -80,7 +82,7 @@ public class FillRect } } - public FillRect() + public JNIOverhead() { setSize(nx, ny); createContent(); @@ -320,7 +322,7 @@ public class FillRect public static void main(String args[]) throws Exception { - fillRectDemo = new FillRect(); + fillRectDemo = new JNIOverhead(); for (int i = 0; i < args.length; i++) { if ("-help".equals(args[i])) @@ -371,7 +373,7 @@ public class FillRect { public JComponent createDemo() { - fillRectDemo = new FillRect(); + fillRectDemo = new JNIOverhead(); SwingUtilities.invokeLater (new Runnable() { diff --git a/examples/gnu/classpath/examples/java2d/aicas.png b/examples/gnu/classpath/examples/java2d/aicas.png Binary files differnew file mode 100644 index 000000000..dcf39654b --- /dev/null +++ b/examples/gnu/classpath/examples/java2d/aicas.png diff --git a/examples/gnu/classpath/examples/java2d/palme.gif b/examples/gnu/classpath/examples/java2d/palme.gif Binary files differnew file mode 100644 index 000000000..694794655 --- /dev/null +++ b/examples/gnu/classpath/examples/java2d/palme.gif diff --git a/examples/gnu/classpath/examples/swing/Demo.java b/examples/gnu/classpath/examples/swing/Demo.java index f1daaac33..ad20fec7e 100644 --- a/examples/gnu/classpath/examples/swing/Demo.java +++ b/examples/gnu/classpath/examples/swing/Demo.java @@ -22,6 +22,8 @@ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA package gnu.classpath.examples.swing; +import gnu.classpath.examples.java2d.JNIOverhead; + import java.awt.*; import java.awt.event.*; @@ -156,8 +158,8 @@ public class Demo examples.add(new JMenuItem(new PopupAction("NavigationFilter", NavigationFilterDemo.createDemoFactory()))); - examples.add(new JMenuItem(new PopupAction("Paint Performance", - FillRect.createDemoFactory()))); + examples.add(new JMenuItem(new PopupAction("JNI Overhead", + JNIOverhead.createDemoFactory()))); final JMenuItem vmMenu; @@ -549,8 +551,8 @@ public class Demo TreeDemo.createDemoFactory()))); panel.add(new JButton(new PopupAction("Theme Editor", MetalThemeEditor.createDemoFactory()))); - panel.add(new JButton(new PopupAction("Paint Performance", - FillRect.createDemoFactory()))); + panel.add(new JButton(new PopupAction("JNI Overhead", + JNIOverhead.createDemoFactory()))); JButton exitDisposer = mkDisposerButton(frame); panel.add(exitDisposer); |