Path: blob/master/test/jdk/java/awt/Graphics2D/MTGraphicsAccessTest/MTGraphicsAccessTest.java
41153 views
/*1* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24@test25@bug 5089429 6982632 814580826@summary Checks that we don't crash if rendering operations and state27changes are performed on a graphics context from different threads.2829@author [email protected] area=Graphics30@run main MTGraphicsAccessTest31*/3233import java.awt.*;34import java.awt.image.*;35import java.awt.geom.*;36import java.util.concurrent.atomic.AtomicInteger;3738public class MTGraphicsAccessTest {3940// in seconds41static final long STANDALONE_RUN_TIME = 20;42static final long JTREG_RUN_TIME = 7;4344static boolean standaloneMode;45static boolean allowExceptions = true;46static long testRunTime;4748volatile boolean done;49AtomicInteger stillRunning = new AtomicInteger(0);50volatile int numexceptions;5152Graphics2D sharedGraphics;53BufferedImage sharedBI =54new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB);5556static final Paint colors[] = {57Color.red,58new Color(0x7f, 0xff, 0x00, 0x7f),59new GradientPaint(0, 0, Color.red,6050, 50, new Color(0x7f, 0xff, 0x00, 0x7f)),61};62static final Font fonts[] = {63new Font("Dialog", Font.PLAIN, 12),64new Font("Dialog", Font.BOLD, 16),65new Font("Dialog", Font.ITALIC, 18),66};67static final AlphaComposite comps[] = {68AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f),69AlphaComposite.Src,70AlphaComposite.Xor,71AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f),72null,73};74static final Stroke strokes[] = {75new BasicStroke(),76new BasicStroke(0.0f),77new BasicStroke(2.0f),78new BasicStroke(2.0f, BasicStroke.CAP_ROUND,79BasicStroke.JOIN_BEVEL),80new BasicStroke(5.0f, BasicStroke.CAP_SQUARE,81BasicStroke.JOIN_ROUND),82new BasicStroke(0.0f, BasicStroke.CAP_ROUND,83BasicStroke.JOIN_ROUND, 0,84new float[]{0,6,0,6}, 0),85};86static final AffineTransform transforms[] = {87new AffineTransform(),88AffineTransform.getRotateInstance(10.0),89AffineTransform.getShearInstance(10.0, 4.0),90AffineTransform.getScaleInstance(1.1, 1.2),91AffineTransform.getScaleInstance(3.0, 2.0),92};9394public MTGraphicsAccessTest() {95BufferedImage bi =96new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB);97sharedGraphics = (Graphics2D)bi.getGraphics();9899done = false;100numexceptions = 0;101102for (int i = 0; i < (standaloneMode ? stateChangers.length : 3); i++) {103(new TesterThread(stateChangers[i])).start();104}105for (int i = 0; i < (standaloneMode ? renderTests.length : 5); i++) {106(new TesterThread(renderTests[i])).start();107}108109mysleep(testRunTime);110done = true;111while (stillRunning.get() > 0) { mysleep(500); }112113if (numexceptions == 0) {114System.err.println("Test passed");115} else if (!allowExceptions) {116throw new RuntimeException("Test failed with "+117numexceptions+" exceptions");118} else {119System.err.println("Test finished with "+120numexceptions+" exceptions");121}122}123124private void mysleep(long time) {125try {126// add +/-5ms variance to increase randomness127Thread.sleep(time + (long)(5 - Math.random()*10));128} catch (InterruptedException e) {};129}130131public static void usage(String message) {132if (message != null) {133System.err.println(message);134}135System.err.println("Usage: MTGraphicsAccessTest [-full] "+136"[-time N/forever] [-help]");137System.err.println(" -full: run full suite of tests "+138"(default: limited number of tests is run)");139System.err.println(" -time N: test duration in seconds/forever"+140" (default: "+JTREG_RUN_TIME+"s for the short suite, "+141STANDALONE_RUN_TIME+"s for the full suite)");142System.err.println(" -help: print this help page");143System.exit(1);144}145146public static void main(String[] args) {147boolean testRunSet = false;148for (int i = 0; i < args.length; i++) {149if ("-full".equals(args[i])) {150standaloneMode = true;151System.err.println("Running complete list of tests");152} else if ("-noexc".equals(args[i])) {153allowExceptions = false;154} else if ("-time".equals(args[i])) {155try {156String time = args[++i];157if ("forever".equals(time)) {158testRunTime = (Long.MAX_VALUE - 20)/1000;159} else {160testRunTime = 1000*Integer.parseInt(time);161}162testRunSet = true;163} catch (NumberFormatException e) {164usage("Can't parse number of seconds: " + args[i]);165} catch (ArrayIndexOutOfBoundsException e1) {166usage("Missing the 'seconds' argument for -time parameter");167}168} else if ("-help".equals(args[i])) {169usage(null);170} else {171usage("Unknown argument:" + args[i]);172}173}174175if (!testRunSet) {176testRunTime = 1000 *177(standaloneMode ? STANDALONE_RUN_TIME : JTREG_RUN_TIME);178}179180System.err.println("Approximate test run time: "+181testRunTime/1000+" seconds");182183new MTGraphicsAccessTest();184}185186class TesterThread extends Thread {187Runnable testRunnable;188189public TesterThread(Runnable testRunnable) {190stillRunning.incrementAndGet();191this.testRunnable = testRunnable;192}193194public void run() {195try {196while (!done) {197try {198testRunnable.run();199Thread.yield();200} catch (Throwable t) {201numexceptions++;202t.printStackTrace();203}204}205} finally {206stillRunning.decrementAndGet();207}208}209}210211final Runnable stateChangers[] = {212new Runnable() {213public void run() {214sharedGraphics.setClip(10, 10, 30, 30);215mysleep(10);216}217},218new Runnable() {219public void run() {220sharedGraphics.setClip(10, 10, 30, 30);221mysleep(10);222}223},224new Runnable() {225int c = 0;226public void run() {227sharedGraphics.setPaint(colors[c++ % colors.length]);228mysleep(10);229}230},231new Runnable() {232boolean AA = false;233public void run() {234if (AA) {235sharedGraphics.setRenderingHint(236RenderingHints.KEY_ANTIALIASING,237RenderingHints.VALUE_ANTIALIAS_ON);238} else {239sharedGraphics.setRenderingHint(240RenderingHints.KEY_ANTIALIASING,241RenderingHints.VALUE_ANTIALIAS_OFF);242}243AA = !AA;244mysleep(10);245}246},247new Runnable() {248int t = 0;249public void run() {250sharedGraphics.setTransform(251transforms[t++ % transforms.length]);252mysleep(10);253}254},255new Runnable() {256int c = 0;257public void run() {258AlphaComposite comp = comps[c++ % comps.length];259if (comp == null) {260sharedGraphics.setXORMode(Color.green);261} else {262sharedGraphics.setComposite(comp);263}264mysleep(10);265}266},267new Runnable() {268int s = 0;269public void run() {270sharedGraphics.setStroke(strokes[s++ % strokes.length]);271mysleep(10);272}273},274new Runnable() {275int f = 0;276public void run() {277sharedGraphics.setFont(fonts[f++ % fonts.length]);278mysleep(10);279}280},281};282283final Runnable renderTests[] = {284new Runnable() {285public void run() {286sharedGraphics.drawLine(10, 10, 30, 30);287}288},289new Runnable() {290public void run() {291sharedGraphics.drawLine(10, 10, 30, 30);292}293},294new Runnable() {295public void run() {296sharedGraphics.drawRect(10, 10, 30, 30);297}298},299new Runnable() {300public void run() {301sharedGraphics.fillRect(10, 10, 30, 30);302}303},304new Runnable() {305public void run() {306sharedGraphics.drawString("Stuff", 10, 10);307}308},309new Runnable() {310public void run() {311sharedGraphics.draw3DRect(10, 10, 30, 30, true);312}313},314new Runnable() {315public void run() {316sharedGraphics.drawImage(sharedBI, 10, 10, null);317}318},319new Runnable() {320public void run() {321sharedGraphics.fill3DRect(10, 10, 30, 30, false);322}323},324// REMIND: copyArea doesn't work when transform is set..325// new Runnable() {326// public void run() {327// sharedGraphics.copyArea(10, 10, 30, 30, 20, 20);328// }329// },330new Runnable() {331public void run() {332sharedGraphics.drawRoundRect(10, 10, 30, 30, 20, 20);333}334},335new Runnable() {336public void run() {337sharedGraphics.fillRoundRect(10, 10, 30, 30, 20, 20);338}339},340new Runnable() {341public void run() {342sharedGraphics.drawArc(10, 10, 30, 30, 0, 90);343}344},345new Runnable() {346public void run() {347sharedGraphics.fillArc(10, 10, 30, 30, 0, 90);348}349},350new Runnable() {351public void run() {352sharedGraphics.drawOval(10, 10, 30, 30);353}354},355new Runnable() {356public void run() {357sharedGraphics.fillOval(10, 10, 30, 30);358}359}360};361}362363364