430N/A/*
2362N/A * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
430N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
430N/A *
430N/A * This code is free software; you can redistribute it and/or modify it
430N/A * under the terms of the GNU General Public License version 2 only, as
430N/A * published by the Free Software Foundation.
430N/A *
430N/A * This code is distributed in the hope that it will be useful, but WITHOUT
430N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
430N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
430N/A * version 2 for more details (a copy is included in the LICENSE file that
430N/A * accompanied this code).
430N/A *
430N/A * You should have received a copy of the GNU General Public License version
430N/A * 2 along with this work; if not, write to the Free Software Foundation,
430N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
430N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
430N/A */
430N/A/*
430N/A * @test
430N/A * @bug 6635805 6653780 6667607
430N/A * @summary Tests that the resource sharing layer API is not broken
430N/A * @author Dmitri.Trembovetski@sun.com: area=Graphics
430N/A * @compile -XDignore.symbol.file=true RSLAPITest.java
430N/A * @run main/othervm RSLAPITest
430N/A * @run main/othervm -Dsun.java2d.noddraw=true RSLAPITest
430N/A * @run main/othervm -Dsun.java2d.opengl=True RSLAPITest
430N/A */
430N/A
430N/Aimport java.awt.Graphics;
430N/Aimport java.awt.GraphicsConfiguration;
430N/Aimport java.awt.GraphicsDevice;
430N/Aimport java.awt.GraphicsEnvironment;
430N/Aimport java.awt.Rectangle;
430N/Aimport java.awt.Transparency;
430N/Aimport java.awt.image.VolatileImage;
430N/Aimport java.util.HashSet;
430N/Aimport sun.java2d.DestSurfaceProvider;
430N/Aimport sun.java2d.Surface;
430N/Aimport sun.java2d.pipe.BufferedContext;
430N/Aimport sun.java2d.pipe.RenderQueue;
430N/Aimport sun.java2d.pipe.hw.AccelDeviceEventListener;
430N/Aimport sun.java2d.pipe.hw.AccelGraphicsConfig;
430N/Aimport sun.java2d.pipe.hw.AccelSurface;
430N/Aimport static java.awt.Transparency.*;
430N/Aimport java.lang.reflect.Field;
430N/Aimport static sun.java2d.pipe.hw.AccelSurface.*;
430N/Aimport static sun.java2d.pipe.hw.ContextCapabilities.*;
430N/A
430N/Apublic class RSLAPITest {
430N/A private static volatile boolean failed = false;
430N/A
430N/A public static void main(String[] args) {
430N/A GraphicsEnvironment ge =
430N/A GraphicsEnvironment.getLocalGraphicsEnvironment();
430N/A GraphicsDevice gd = ge.getDefaultScreenDevice();
430N/A GraphicsConfiguration gc = gd.getDefaultConfiguration();
430N/A testGC(gc);
430N/A
430N/A if (failed) {
430N/A throw new RuntimeException("Test FAILED. See err output for more");
430N/A }
430N/A
430N/A System.out.println("Test PASSED.");
430N/A }
430N/A
430N/A private static void testInvalidType(AccelSurface surface, int type) {
430N/A long ret = surface.getNativeResource(type);
430N/A System.out.printf(" getNativeResource(%d)=0x%x\n", type, ret);
430N/A if (ret != 0l) {
430N/A System.err.printf(
430N/A "FAILED: surface.getNativeResource(%d) returned" +
430N/A " 0x%s. It should have have returned 0L\n",
430N/A type, ret);
430N/A failed = true;
430N/A }
430N/A }
430N/A
430N/A private static void testD3DDeviceResourceField(final AccelSurface surface) {
430N/A try {
430N/A Class d3dc = Class.forName("sun.java2d.d3d.D3DSurfaceData");
430N/A if (d3dc.isInstance(surface)) {
430N/A Field f = d3dc.getDeclaredField("D3D_DEVICE_RESOURCE");
430N/A f.setAccessible(true);
430N/A int d3dDR = (Integer)f.get(null);
430N/A
430N/A System.out.printf(
430N/A " getNativeResource(D3D_DEVICE_RESOURCE)=0x%x\n",
430N/A surface.getNativeResource(d3dDR));
430N/A }
430N/A } catch (ClassNotFoundException e) {}
430N/A catch (IllegalAccessException e) {}
430N/A catch (NoSuchFieldException e) {
430N/A System.err.println("Failed: D3DSurfaceData.D3D_DEVICE_RESOURCE" +
430N/A " field not found!");
430N/A failed = true;
430N/A }
430N/A }
430N/A
430N/A private static void printSurface(Surface s) {
430N/A if (s instanceof AccelSurface) {
430N/A final AccelSurface surface = (AccelSurface) s;
430N/A System.out.println(" Accel Surface: ");
430N/A System.out.println(" type=" + surface.getType());
430N/A System.out.println(" bounds=" + surface.getBounds());
430N/A System.out.println(" nativeBounds=" + surface.getNativeBounds());
430N/A System.out.println(" isSurfaceLost=" + surface.isSurfaceLost());
430N/A System.out.println(" isValid=" + surface.isValid());
430N/A RenderQueue rq = surface.getContext().getRenderQueue();
430N/A rq.lock();
430N/A try {
430N/A rq.flushAndInvokeNow(new Runnable() {
430N/A public void run() {
430N/A System.out.printf(" getNativeResource(TEXTURE)=0x%x\n",
430N/A surface.getNativeResource(TEXTURE));
430N/A System.out.printf(" getNativeResource(RT_TEXTURE)=0x%x\n",
430N/A surface.getNativeResource(RT_TEXTURE));
430N/A System.out.printf(" getNativeResource(RT_PLAIN)=0x%x\n",
430N/A surface.getNativeResource(RT_PLAIN));
430N/A System.out.printf(
430N/A " getNativeResource(FLIP_BACKBUFFER)=0x%x\n",
430N/A surface.getNativeResource(FLIP_BACKBUFFER));
430N/A
430N/A testD3DDeviceResourceField(surface);
430N/A
430N/A testInvalidType(surface, -1);
430N/A testInvalidType(surface, -150);
430N/A testInvalidType(surface, 300);
430N/A testInvalidType(surface, Integer.MAX_VALUE);
430N/A testInvalidType(surface, Integer.MIN_VALUE);
430N/A }
430N/A });
430N/A } finally {
430N/A rq.unlock();
430N/A }
430N/A } else {
430N/A System.out.println("null accelerated surface");
430N/A }
430N/A }
430N/A
430N/A private static void printAGC(AccelGraphicsConfig agc) {
430N/A System.out.println("Accelerated Graphics Config: " + agc);
430N/A System.out.println("Capabilities:");
430N/A System.out.printf("AGC caps: 0x%x\n",
430N/A agc.getContextCapabilities().getCaps());
430N/A System.out.println(agc.getContextCapabilities());
430N/A }
430N/A
430N/A private static void testGC(GraphicsConfiguration gc) {
430N/A if (!(gc instanceof AccelGraphicsConfig)) {
430N/A System.out.println("Test passed: no hw accelerated configs found.");
430N/A return;
430N/A }
430N/A System.out.println("AccelGraphicsConfig exists, testing.");
430N/A AccelGraphicsConfig agc = (AccelGraphicsConfig) gc;
430N/A printAGC(agc);
430N/A
430N/A testContext(agc);
430N/A
430N/A VolatileImage vi = gc.createCompatibleVolatileImage(10, 10);
430N/A vi.validate(gc);
430N/A if (vi instanceof DestSurfaceProvider) {
430N/A System.out.println("Passed: VI is DestSurfaceProvider");
430N/A Surface s = ((DestSurfaceProvider) vi).getDestSurface();
430N/A if (s instanceof AccelSurface) {
430N/A System.out.println("Passed: Obtained Accel Surface");
430N/A printSurface((AccelSurface) s);
430N/A }
430N/A Graphics g = vi.getGraphics();
430N/A if (g instanceof DestSurfaceProvider) {
430N/A System.out.println("Passed: VI graphics is " +
430N/A "DestSurfaceProvider");
430N/A printSurface(((DestSurfaceProvider) g).getDestSurface());
430N/A }
430N/A } else {
430N/A System.out.println("VI is not DestSurfaceProvider");
430N/A }
430N/A testVICreation(agc, CAPS_RT_TEXTURE_ALPHA, TRANSLUCENT, RT_TEXTURE);
430N/A testVICreation(agc, CAPS_RT_TEXTURE_OPAQUE, OPAQUE, RT_TEXTURE);
430N/A testVICreation(agc, CAPS_RT_PLAIN_ALPHA, TRANSLUCENT, RT_PLAIN);
430N/A testVICreation(agc, agc.getContextCapabilities().getCaps(), OPAQUE,
430N/A TEXTURE);
430N/A testForNPEDuringCreation(agc);
430N/A }
430N/A
430N/A private static void testVICreation(AccelGraphicsConfig agc, int cap,
430N/A int transparency, int type)
430N/A {
430N/A int caps = agc.getContextCapabilities().getCaps();
430N/A int w = 11, h = 17;
430N/A
430N/A VolatileImage vi =
430N/A agc.createCompatibleVolatileImage(w, h, transparency, type);
430N/A if ((cap & caps) != 0) {
430N/A if (vi == null) {
430N/A System.out.printf("Failed: cap=%d is supported but " +
430N/A "image wasn't created\n", cap);
430N/A throw new RuntimeException("Failed: image wasn't created " +
430N/A "for supported cap");
430N/A } else {
430N/A if (!(vi instanceof DestSurfaceProvider)) {
430N/A throw new RuntimeException("Failed: created VI is not " +
430N/A "DestSurfaceProvider");
430N/A }
430N/A Surface s = ((DestSurfaceProvider) vi).getDestSurface();
430N/A if (s instanceof AccelSurface) {
430N/A AccelSurface as = (AccelSurface) s;
430N/A printSurface(as);
430N/A if (as.getType() != type) {
430N/A throw new RuntimeException("Failed: returned VI is" +
430N/A " of incorrect type: " + as.getType() +
430N/A " requested type=" + type);
430N/A } else {
430N/A System.out.printf("Passed: VI of type %d was " +
430N/A "created for cap=%d\n", type, cap);
430N/A }
430N/A if (as.getType() == TEXTURE) {
430N/A boolean ex = false;
430N/A try {
430N/A Graphics g = vi.getGraphics();
430N/A g.dispose();
430N/A } catch (UnsupportedOperationException e) {
430N/A ex = true;
430N/A }
430N/A if (!ex) {
430N/A throw new RuntimeException("Failed: " +
430N/A "texture.getGraphics() didn't throw exception");
430N/A } else {
430N/A System.out.println("Passed: VI.getGraphics()" +
430N/A " threw exception for texture-based VI");
430N/A }
430N/A }
430N/A } else {
430N/A System.out.printf("Passed: VI of type %d was " +
430N/A "created for cap=%d but accel surface is null\n",
430N/A type, cap);
430N/A }
430N/A }
430N/A } else {
430N/A if (vi != null) {
430N/A throw new RuntimeException("Failed: created VI for " +
430N/A "unsupported cap=" + cap);
430N/A }
430N/A }
430N/A }
430N/A
430N/A private static void testContext(final AccelGraphicsConfig agc) {
430N/A BufferedContext c = agc.getContext();
430N/A final AccelDeviceEventListener l = new AccelDeviceEventListener() {
430N/A public void onDeviceDispose() {
430N/A System.out.println("onDeviceDispose invoked");
430N/A agc.removeDeviceEventListener(this);
430N/A }
430N/A public void onDeviceReset() {
430N/A System.out.println("onDeviceReset invoked");
430N/A }
430N/A };
430N/A agc.addDeviceEventListener(l);
430N/A
430N/A RenderQueue rq = c.getRenderQueue();
430N/A rq.lock();
430N/A try {
430N/A c.saveState();
430N/A rq.flushNow();
430N/A c.restoreState();
430N/A rq.flushNow();
430N/A System.out.println("Passed: Save/Restore");
430N/A } finally {
430N/A rq.unlock();
430N/A }
430N/A }
430N/A
430N/A private static void testForNPEDuringCreation(AccelGraphicsConfig agc) {
430N/A int iterations = 100;
430N/A HashSet<VolatileImage> vis = new HashSet<VolatileImage>();
430N/A GraphicsConfiguration gc = (GraphicsConfiguration)agc;
430N/A Rectangle r = gc.getBounds();
430N/A long ram = gc.getDevice().getAvailableAcceleratedMemory();
430N/A if (ram > 0) {
430N/A // guesstimate the number of iterations needed to exhaust vram
430N/A int i = 2 *
430N/A (int)(ram / (r.width * r.height * gc.getColorModel().getPixelSize()/8));
430N/A iterations = Math.max(iterations, i);
430N/A System.err.println("iterations="+iterations);
430N/A }
430N/A for (int i = 0; i < iterations; i++) {
430N/A VolatileImage vi =
430N/A agc.createCompatibleVolatileImage(r.width, r.height,
430N/A Transparency.OPAQUE,
430N/A AccelSurface.RT_PLAIN);
430N/A if (vi == null) {
430N/A break;
430N/A }
430N/A vis.add(vi);
430N/A }
430N/A for (VolatileImage vi : vis) {
430N/A vi.flush();
430N/A }
430N/A vis = null;
430N/A
430N/A System.out.println("Passed: testing for possible NPEs " +
430N/A "during VI creation");
430N/A }
430N/A}