Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/java2d/pipe/hw/RSLAPITest/RSLAPITest.java
38867 views
/*1* Copyright (c) 2007, 2008, 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* @bug 6635805 6653780 666760725* @summary Tests that the resource sharing layer API is not broken26* @author [email protected]: area=Graphics27* @compile -XDignore.symbol.file=true RSLAPITest.java28* @run main/othervm RSLAPITest29* @run main/othervm -Dsun.java2d.noddraw=true RSLAPITest30* @run main/othervm -Dsun.java2d.opengl=True RSLAPITest31*/3233import java.awt.Graphics;34import java.awt.GraphicsConfiguration;35import java.awt.GraphicsDevice;36import java.awt.GraphicsEnvironment;37import java.awt.Rectangle;38import java.awt.Transparency;39import java.awt.image.VolatileImage;40import java.util.HashSet;41import sun.java2d.DestSurfaceProvider;42import sun.java2d.Surface;43import sun.java2d.pipe.BufferedContext;44import sun.java2d.pipe.RenderQueue;45import sun.java2d.pipe.hw.AccelDeviceEventListener;46import sun.java2d.pipe.hw.AccelGraphicsConfig;47import sun.java2d.pipe.hw.AccelSurface;48import static java.awt.Transparency.*;49import java.lang.reflect.Field;50import static sun.java2d.pipe.hw.AccelSurface.*;51import static sun.java2d.pipe.hw.ContextCapabilities.*;5253public class RSLAPITest {54private static volatile boolean failed = false;5556public static void main(String[] args) {57GraphicsEnvironment ge =58GraphicsEnvironment.getLocalGraphicsEnvironment();59GraphicsDevice gd = ge.getDefaultScreenDevice();60GraphicsConfiguration gc = gd.getDefaultConfiguration();61testGC(gc);6263if (failed) {64throw new RuntimeException("Test FAILED. See err output for more");65}6667System.out.println("Test PASSED.");68}6970private static void testInvalidType(AccelSurface surface, int type) {71long ret = surface.getNativeResource(type);72System.out.printf(" getNativeResource(%d)=0x%x\n", type, ret);73if (ret != 0l) {74System.err.printf(75"FAILED: surface.getNativeResource(%d) returned" +76" 0x%s. It should have have returned 0L\n",77type, ret);78failed = true;79}80}8182private static void testD3DDeviceResourceField(final AccelSurface surface) {83try {84Class d3dc = Class.forName("sun.java2d.d3d.D3DSurfaceData");85if (d3dc.isInstance(surface)) {86Field f = d3dc.getDeclaredField("D3D_DEVICE_RESOURCE");87f.setAccessible(true);88int d3dDR = (Integer)f.get(null);8990System.out.printf(91" getNativeResource(D3D_DEVICE_RESOURCE)=0x%x\n",92surface.getNativeResource(d3dDR));93}94} catch (ClassNotFoundException e) {}95catch (IllegalAccessException e) {}96catch (NoSuchFieldException e) {97System.err.println("Failed: D3DSurfaceData.D3D_DEVICE_RESOURCE" +98" field not found!");99failed = true;100}101}102103private static void printSurface(Surface s) {104if (s instanceof AccelSurface) {105final AccelSurface surface = (AccelSurface) s;106System.out.println(" Accel Surface: ");107System.out.println(" type=" + surface.getType());108System.out.println(" bounds=" + surface.getBounds());109System.out.println(" nativeBounds=" + surface.getNativeBounds());110System.out.println(" isSurfaceLost=" + surface.isSurfaceLost());111System.out.println(" isValid=" + surface.isValid());112RenderQueue rq = surface.getContext().getRenderQueue();113rq.lock();114try {115rq.flushAndInvokeNow(new Runnable() {116public void run() {117System.out.printf(" getNativeResource(TEXTURE)=0x%x\n",118surface.getNativeResource(TEXTURE));119System.out.printf(" getNativeResource(RT_TEXTURE)=0x%x\n",120surface.getNativeResource(RT_TEXTURE));121System.out.printf(" getNativeResource(RT_PLAIN)=0x%x\n",122surface.getNativeResource(RT_PLAIN));123System.out.printf(124" getNativeResource(FLIP_BACKBUFFER)=0x%x\n",125surface.getNativeResource(FLIP_BACKBUFFER));126127testD3DDeviceResourceField(surface);128129testInvalidType(surface, -1);130testInvalidType(surface, -150);131testInvalidType(surface, 300);132testInvalidType(surface, Integer.MAX_VALUE);133testInvalidType(surface, Integer.MIN_VALUE);134}135});136} finally {137rq.unlock();138}139} else {140System.out.println("null accelerated surface");141}142}143144private static void printAGC(AccelGraphicsConfig agc) {145System.out.println("Accelerated Graphics Config: " + agc);146System.out.println("Capabilities:");147System.out.printf("AGC caps: 0x%x\n",148agc.getContextCapabilities().getCaps());149System.out.println(agc.getContextCapabilities());150}151152private static void testGC(GraphicsConfiguration gc) {153if (!(gc instanceof AccelGraphicsConfig)) {154System.out.println("Test passed: no hw accelerated configs found.");155return;156}157System.out.println("AccelGraphicsConfig exists, testing.");158AccelGraphicsConfig agc = (AccelGraphicsConfig) gc;159printAGC(agc);160161testContext(agc);162163VolatileImage vi = gc.createCompatibleVolatileImage(10, 10);164vi.validate(gc);165if (vi instanceof DestSurfaceProvider) {166System.out.println("Passed: VI is DestSurfaceProvider");167Surface s = ((DestSurfaceProvider) vi).getDestSurface();168if (s instanceof AccelSurface) {169System.out.println("Passed: Obtained Accel Surface");170printSurface((AccelSurface) s);171}172Graphics g = vi.getGraphics();173if (g instanceof DestSurfaceProvider) {174System.out.println("Passed: VI graphics is " +175"DestSurfaceProvider");176printSurface(((DestSurfaceProvider) g).getDestSurface());177}178} else {179System.out.println("VI is not DestSurfaceProvider");180}181testVICreation(agc, CAPS_RT_TEXTURE_ALPHA, TRANSLUCENT, RT_TEXTURE);182testVICreation(agc, CAPS_RT_TEXTURE_OPAQUE, OPAQUE, RT_TEXTURE);183testVICreation(agc, CAPS_RT_PLAIN_ALPHA, TRANSLUCENT, RT_PLAIN);184testVICreation(agc, agc.getContextCapabilities().getCaps(), OPAQUE,185TEXTURE);186testForNPEDuringCreation(agc);187}188189private static void testVICreation(AccelGraphicsConfig agc, int cap,190int transparency, int type)191{192int caps = agc.getContextCapabilities().getCaps();193int w = 11, h = 17;194195VolatileImage vi =196agc.createCompatibleVolatileImage(w, h, transparency, type);197if ((cap & caps) != 0) {198if (vi == null) {199System.out.printf("Failed: cap=%d is supported but " +200"image wasn't created\n", cap);201throw new RuntimeException("Failed: image wasn't created " +202"for supported cap");203} else {204if (!(vi instanceof DestSurfaceProvider)) {205throw new RuntimeException("Failed: created VI is not " +206"DestSurfaceProvider");207}208Surface s = ((DestSurfaceProvider) vi).getDestSurface();209if (s instanceof AccelSurface) {210AccelSurface as = (AccelSurface) s;211printSurface(as);212if (as.getType() != type) {213throw new RuntimeException("Failed: returned VI is" +214" of incorrect type: " + as.getType() +215" requested type=" + type);216} else {217System.out.printf("Passed: VI of type %d was " +218"created for cap=%d\n", type, cap);219}220if (as.getType() == TEXTURE) {221boolean ex = false;222try {223Graphics g = vi.getGraphics();224g.dispose();225} catch (UnsupportedOperationException e) {226ex = true;227}228if (!ex) {229throw new RuntimeException("Failed: " +230"texture.getGraphics() didn't throw exception");231} else {232System.out.println("Passed: VI.getGraphics()" +233" threw exception for texture-based VI");234}235}236} else {237System.out.printf("Passed: VI of type %d was " +238"created for cap=%d but accel surface is null\n",239type, cap);240}241}242} else {243if (vi != null) {244throw new RuntimeException("Failed: created VI for " +245"unsupported cap=" + cap);246}247}248}249250private static void testContext(final AccelGraphicsConfig agc) {251BufferedContext c = agc.getContext();252final AccelDeviceEventListener l = new AccelDeviceEventListener() {253public void onDeviceDispose() {254System.out.println("onDeviceDispose invoked");255agc.removeDeviceEventListener(this);256}257public void onDeviceReset() {258System.out.println("onDeviceReset invoked");259}260};261agc.addDeviceEventListener(l);262263RenderQueue rq = c.getRenderQueue();264rq.lock();265try {266c.saveState();267rq.flushNow();268c.restoreState();269rq.flushNow();270System.out.println("Passed: Save/Restore");271} finally {272rq.unlock();273}274}275276private static void testForNPEDuringCreation(AccelGraphicsConfig agc) {277int iterations = 100;278HashSet<VolatileImage> vis = new HashSet<VolatileImage>();279GraphicsConfiguration gc = (GraphicsConfiguration)agc;280Rectangle r = gc.getBounds();281long ram = gc.getDevice().getAvailableAcceleratedMemory();282if (ram > 0) {283// guesstimate the number of iterations needed to exhaust vram284int i = 2 *285(int)(ram / (r.width * r.height * gc.getColorModel().getPixelSize()/8));286iterations = Math.max(iterations, i);287System.err.println("iterations="+iterations);288}289for (int i = 0; i < iterations; i++) {290VolatileImage vi =291agc.createCompatibleVolatileImage(r.width, r.height,292Transparency.OPAQUE,293AccelSurface.RT_PLAIN);294if (vi == null) {295break;296}297vis.add(vi);298}299for (VolatileImage vi : vis) {300vi.flush();301}302vis = null;303304System.out.println("Passed: testing for possible NPEs " +305"during VI creation");306}307}308309310