Path: blob/master/test/jdk/java/awt/FullScreen/DisplayChangeVITest/DisplayChangeVITest.java
41154 views
/*1* Copyright (c) 2006, 2018, 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* @key headful26* @bug 6366359 819861327* @summary Test that we don't crash when changing from 8 to 16/32 bit modes28* @author [email protected] area=FullScreen29* @run main/othervm/timeout=200 DisplayChangeVITest30* @run main/othervm/timeout=200 -Dsun.java2d.d3d=false DisplayChangeVITest31*/3233import java.awt.Color;34import java.awt.DisplayMode;35import java.awt.Graphics;36import java.awt.GraphicsDevice;37import java.awt.GraphicsEnvironment;38import java.awt.event.KeyAdapter;39import java.awt.event.KeyEvent;40import java.awt.image.BufferedImage;41import java.awt.image.VolatileImage;42import java.lang.Exception;43import java.lang.Thread;44import java.util.ArrayList;45import java.util.Random;46import javax.swing.JFrame;4748/**49* The test enters fullscreen mode (if it's supported) and then tries50* to switch between display moes with different depths and dimensions51* while doing both rendering to the screen (via a VolatileImage)52* and Swing repainting just to make things more chaotic.53*54* The procedure is repeated TEST_REPS times (3 by default).55*56* Don't pay attention to what happens on the screen, it won't be pretty.57* If the test doesn't crash or throw exceptions, it passes, otherwise58* it fails.59*/60public class DisplayChangeVITest extends JFrame implements Runnable {6162private final Random rnd = new Random();63private VolatileImage bb;64private BufferedImage sprite;65private VolatileImage volSprite;6667private static boolean done = false;68private static final Object lock = new Object();69private static final int TEST_REPS = 3;7071private ArrayList<DisplayMode> dms;7273DisplayChangeVITest() {74selectDisplayModes();75addKeyListener(new KeyAdapter() {76public void keyPressed(KeyEvent e) {77if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {78synchronized (lock) {79done = true;80}81}82}83});84sprite = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);85sprite.getRaster().getDataBuffer();86Graphics g = sprite.getGraphics();87g.setColor(Color.yellow);88g.fillRect(0, 0, sprite.getWidth(), sprite.getHeight());89}9091void render(Graphics g) {92do {93// volatile images validated here94initBackbuffer();9596g.setColor(Color.black);97g.fillRect(0, 0, getWidth(), getHeight());9899Graphics gg = bb.getGraphics();100gg.setColor(new Color(rnd.nextInt(0x00ffffff)));101gg.fillRect(0, 0, bb.getWidth(), bb.getHeight());102for (int x = 0; x < 10; x++) {103gg.drawImage(sprite, x*200, 0, null);104gg.drawImage(volSprite, x*200, 500, null);105}106107g.drawImage(bb, 0, 0, null);108} while (bb.contentsLost());109}110111private static void sleep(long msec) {112try { Thread.sleep(msec); } catch (InterruptedException e) {}113}114115private int reps = 0;116public void run() {117GraphicsDevice gd = getGraphicsConfiguration().getDevice();118if (gd.isDisplayChangeSupported() && dms.size() > 0) {119while (!done && reps++ < TEST_REPS) {120for (DisplayMode dm : dms) {121System.err.printf("Entering DisplayMode[%dx%dx%d]\n",122dm.getWidth(), dm.getHeight(), dm.getBitDepth());123gd.setDisplayMode(dm);124125initBackbuffer();126for (int i = 0; i < 10; i++) {127// render to the screen128render(getGraphics());129// ask Swing to repaint130repaint();131sleep(100);132}133sleep(1500);134}135}136} else {137System.err.println("Display mode change " +138"not supported. Test passed.");139}140dispose();141synchronized (lock) {142done = true;143lock.notify();144}145}146147private void createBackbuffer() {148if (bb == null ||149bb.getWidth() != getWidth() || bb.getHeight() != getHeight())150{151bb = createVolatileImage(getWidth(), getHeight());152}153}154155private void initBackbuffer() {156createBackbuffer();157158int res = bb.validate(getGraphicsConfiguration());159if (res == VolatileImage.IMAGE_INCOMPATIBLE) {160bb = null;161createBackbuffer();162bb.validate(getGraphicsConfiguration());163res = VolatileImage.IMAGE_RESTORED;164}165if (res == VolatileImage.IMAGE_RESTORED) {166Graphics g = bb.getGraphics();167g.setColor(new Color(rnd.nextInt(0x00ffffff)));168g.fillRect(0, 0, bb.getWidth(), bb.getHeight());169170volSprite = createVolatileImage(100, 100);171}172volSprite.validate(getGraphicsConfiguration());173}174175private void selectDisplayModes() {176GraphicsDevice gd =177GraphicsEnvironment.getLocalGraphicsEnvironment().178getDefaultScreenDevice();179dms = new ArrayList<DisplayMode>();180DisplayMode dmArray[] = gd.getDisplayModes();181boolean found8 = false, found16 = false,182found24 = false, found32 = false;183for (DisplayMode dm : dmArray) {184if (!found8 &&185(dm.getBitDepth() == 8 ||186dm.getBitDepth() == DisplayMode.BIT_DEPTH_MULTI) &&187(dm.getWidth() >= 800 && dm.getWidth() < 1024))188{189dms.add(dm);190found8 = true;191continue;192}193if (!found32 &&194(dm.getBitDepth() == 32 ||195dm.getBitDepth() == DisplayMode.BIT_DEPTH_MULTI) &&196dm.getWidth() >= 1280)197{198dms.add(dm);199found32 = true;200continue;201}202if (!found16 &&203dm.getBitDepth() == 16 &&204(dm.getWidth() >= 1024 && dm.getWidth() < 1280))205{206dms.add(dm);207found16 = true;208continue;209}210if (found8 && found16 && found32) {211break;212}213}214System.err.println("Found display modes:");215for (DisplayMode dm : dms) {216System.err.printf("DisplayMode[%dx%dx%d]\n",217dm.getWidth(), dm.getHeight(), dm.getBitDepth());218}219}220221public static void main(String[] args) throws Exception {222DisplayChangeVITest test = new DisplayChangeVITest();223GraphicsDevice gd =224GraphicsEnvironment.getLocalGraphicsEnvironment().225getDefaultScreenDevice();226if (gd.isFullScreenSupported()) {227gd.setFullScreenWindow(test);228Thread t = new Thread(test);229t.run();230synchronized (lock) {231while (!done) {232try {233lock.wait(50);234} catch (InterruptedException ex) {235ex.printStackTrace();236}237}238}239System.err.println("Test Passed.");240} else {241System.err.println("Full screen not supported. Test passed.");242}243}244}245246247