Path: blob/master/test/jdk/sun/java2d/pipe/hw/RSLAPITest/RSLAPITest.java
41159 views
/*1* Copyright (c) 2007, 2019, 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*/22/*23* @test24* @key headful25* @bug 6635805 6653780 6667607 819861326* @summary Tests that the resource sharing layer API is not broken27* @author [email protected]: area=Graphics28* @modules java.desktop/sun.java2d29* java.desktop/sun.java2d.pipe30* java.desktop/sun.java2d.pipe.hw31* @compile -XDignore.symbol.file=true RSLAPITest.java32* @run main/othervm RSLAPITest33* @run main/othervm -Dsun.java2d.noddraw=true RSLAPITest34*/3536import java.awt.Graphics;37import java.awt.GraphicsConfiguration;38import java.awt.GraphicsDevice;39import java.awt.GraphicsEnvironment;40import java.awt.Rectangle;41import java.awt.Transparency;42import java.awt.image.VolatileImage;43import java.util.HashSet;44import sun.java2d.DestSurfaceProvider;45import sun.java2d.Surface;46import sun.java2d.pipe.RenderQueue;47import sun.java2d.pipe.hw.AccelGraphicsConfig;48import sun.java2d.pipe.hw.AccelSurface;49import static java.awt.Transparency.*;50import java.lang.reflect.Field;51import static sun.java2d.pipe.hw.AccelSurface.*;52import static sun.java2d.pipe.hw.ContextCapabilities.*;5354public class RSLAPITest {55private static volatile boolean failed = false;5657public static void main(String[] args) {58GraphicsEnvironment ge =59GraphicsEnvironment.getLocalGraphicsEnvironment();60GraphicsDevice gd = ge.getDefaultScreenDevice();61GraphicsConfiguration gc = gd.getDefaultConfiguration();62testGC(gc);6364if (failed) {65throw new RuntimeException("Test FAILED. See err output for more");66}6768System.out.println("Test PASSED.");69}7071private static void testInvalidType(AccelSurface surface, int type) {72long ret = surface.getNativeResource(type);73System.out.printf(" getNativeResource(%d)=0x%x\n", type, ret);74if (ret != 0l) {75System.err.printf(76"FAILED: surface.getNativeResource(%d) returned" +77" 0x%s. It should have have returned 0L\n",78type, ret);79failed = true;80}81}8283private static void testD3DDeviceResourceField(final AccelSurface surface) {84try {85Class d3dc = Class.forName("sun.java2d.d3d.D3DSurfaceData");86if (d3dc.isInstance(surface)) {87Field f = d3dc.getDeclaredField("D3D_DEVICE_RESOURCE");88f.setAccessible(true);89int d3dDR = (Integer)f.get(null);9091System.out.printf(92" getNativeResource(D3D_DEVICE_RESOURCE)=0x%x\n",93surface.getNativeResource(d3dDR));94}95} catch (ClassNotFoundException e) {}96catch (IllegalAccessException e) {}97catch (NoSuchFieldException e) {98System.err.println("Failed: D3DSurfaceData.D3D_DEVICE_RESOURCE" +99" field not found!");100failed = true;101}102}103104private static void printSurface(Surface s) {105if (s instanceof AccelSurface) {106final AccelSurface surface = (AccelSurface) s;107System.out.println(" Accel Surface: ");108System.out.println(" type=" + surface.getType());109System.out.println(" bounds=" + surface.getBounds());110System.out.println(" nativeBounds=" + surface.getNativeBounds());111System.out.println(" isSurfaceLost=" + surface.isSurfaceLost());112System.out.println(" isValid=" + surface.isValid());113RenderQueue rq = surface.getContext().getRenderQueue();114rq.lock();115try {116rq.flushAndInvokeNow(new Runnable() {117public void run() {118System.out.printf(" getNativeResource(TEXTURE)=0x%x\n",119surface.getNativeResource(TEXTURE));120System.out.printf(" getNativeResource(RT_TEXTURE)=0x%x\n",121surface.getNativeResource(RT_TEXTURE));122System.out.printf(" getNativeResource(RT_PLAIN)=0x%x\n",123surface.getNativeResource(RT_PLAIN));124System.out.printf(125" getNativeResource(FLIP_BACKBUFFER)=0x%x\n",126surface.getNativeResource(FLIP_BACKBUFFER));127128testD3DDeviceResourceField(surface);129130testInvalidType(surface, -1);131testInvalidType(surface, -150);132testInvalidType(surface, 300);133testInvalidType(surface, Integer.MAX_VALUE);134testInvalidType(surface, Integer.MIN_VALUE);135}136});137} finally {138rq.unlock();139}140} else {141System.out.println("null accelerated surface");142}143}144145private static void printAGC(AccelGraphicsConfig agc) {146System.out.println("Accelerated Graphics Config: " + agc);147System.out.println("Capabilities:");148System.out.printf("AGC caps: 0x%x\n",149agc.getContextCapabilities().getCaps());150System.out.println(agc.getContextCapabilities());151}152153private static void testGC(GraphicsConfiguration gc) {154if (!(gc instanceof AccelGraphicsConfig)) {155System.out.println("Test passed: no hw accelerated configs found.");156return;157}158System.out.println("AccelGraphicsConfig exists, testing.");159AccelGraphicsConfig agc = (AccelGraphicsConfig) gc;160printAGC(agc);161162VolatileImage vi = gc.createCompatibleVolatileImage(10, 10);163vi.validate(gc);164if (vi instanceof DestSurfaceProvider) {165System.out.println("Passed: VI is DestSurfaceProvider");166Surface s = ((DestSurfaceProvider) vi).getDestSurface();167if (s instanceof AccelSurface) {168System.out.println("Passed: Obtained Accel Surface");169printSurface((AccelSurface) s);170}171Graphics g = vi.getGraphics();172if (g instanceof DestSurfaceProvider) {173System.out.println("Passed: VI graphics is " +174"DestSurfaceProvider");175printSurface(((DestSurfaceProvider) g).getDestSurface());176}177} else {178System.out.println("VI is not DestSurfaceProvider");179}180testVICreation(agc, CAPS_RT_TEXTURE_ALPHA, TRANSLUCENT, RT_TEXTURE);181testVICreation(agc, CAPS_RT_TEXTURE_OPAQUE, OPAQUE, RT_TEXTURE);182testVICreation(agc, CAPS_RT_PLAIN_ALPHA, TRANSLUCENT, RT_PLAIN);183testVICreation(agc, agc.getContextCapabilities().getCaps(), OPAQUE,184TEXTURE);185testForNPEDuringCreation(agc);186}187188private static void testVICreation(AccelGraphicsConfig agc, int cap,189int transparency, int type)190{191int caps = agc.getContextCapabilities().getCaps();192int w = 11, h = 17;193194VolatileImage vi =195agc.createCompatibleVolatileImage(w, h, transparency, type);196if ((cap & caps) != 0) {197if (vi == null) {198System.out.printf("Failed: cap=%d is supported but " +199"image wasn't created\n", cap);200throw new RuntimeException("Failed: image wasn't created " +201"for supported cap");202} else {203if (!(vi instanceof DestSurfaceProvider)) {204throw new RuntimeException("Failed: created VI is not " +205"DestSurfaceProvider");206}207Surface s = ((DestSurfaceProvider) vi).getDestSurface();208if (s instanceof AccelSurface) {209AccelSurface as = (AccelSurface) s;210printSurface(as);211if (as.getType() != type) {212throw new RuntimeException("Failed: returned VI is" +213" of incorrect type: " + as.getType() +214" requested type=" + type);215} else {216System.out.printf("Passed: VI of type %d was " +217"created for cap=%d\n", type, cap);218}219if (as.getType() == TEXTURE) {220boolean ex = false;221try {222Graphics g = vi.getGraphics();223g.dispose();224} catch (UnsupportedOperationException e) {225ex = true;226}227if (!ex) {228throw new RuntimeException("Failed: " +229"texture.getGraphics() didn't throw exception");230} else {231System.out.println("Passed: VI.getGraphics()" +232" threw exception for texture-based VI");233}234}235} else {236System.out.printf("Passed: VI of type %d was " +237"created for cap=%d but accel surface is null\n",238type, cap);239}240}241} else {242if (vi != null) {243throw new RuntimeException("Failed: created VI for " +244"unsupported cap=" + cap);245}246}247}248249private static void testForNPEDuringCreation(AccelGraphicsConfig agc) {250int iterations = 100;251HashSet<VolatileImage> vis = new HashSet<VolatileImage>();252GraphicsConfiguration gc = (GraphicsConfiguration)agc;253Rectangle r = gc.getBounds();254long ram = gc.getDevice().getAvailableAcceleratedMemory();255if (ram > 0) {256// guesstimate the number of iterations needed to exhaust vram257int i = 2 *258(int)(ram / (r.width * r.height * gc.getColorModel().getPixelSize()/8));259iterations = Math.max(iterations, i);260System.err.println("iterations="+iterations);261}262for (int i = 0; i < iterations; i++) {263VolatileImage vi =264agc.createCompatibleVolatileImage(r.width, r.height,265Transparency.OPAQUE,266AccelSurface.RT_PLAIN);267if (vi == null) {268break;269}270vis.add(vi);271}272for (VolatileImage vi : vis) {273vi.flush();274}275vis = null;276277System.out.println("Passed: testing for possible NPEs " +278"during VI creation");279}280}281282283