Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/java2d/d3d/D3DGraphicsConfig.java
32288 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.d3d;2627import java.awt.AWTException;28import java.awt.BufferCapabilities;29import java.awt.BufferCapabilities.FlipContents;30import java.awt.Component;31import java.awt.Graphics;32import java.awt.ImageCapabilities;33import java.awt.Transparency;34import java.awt.color.ColorSpace;35import java.awt.image.ColorModel;36import java.awt.image.DataBuffer;37import java.awt.image.DirectColorModel;38import java.awt.image.VolatileImage;39import sun.awt.Win32GraphicsConfig;40import sun.awt.image.SunVolatileImage;41import sun.awt.image.SurfaceManager;42import sun.awt.windows.WComponentPeer;43import sun.java2d.Surface;44import sun.java2d.SurfaceData;45import sun.java2d.pipe.hw.AccelDeviceEventNotifier;46import sun.java2d.pipe.hw.AccelTypedVolatileImage;47import sun.java2d.pipe.hw.AccelGraphicsConfig;48import sun.java2d.pipe.hw.AccelSurface;49import sun.java2d.pipe.hw.ContextCapabilities;50import static sun.java2d.pipe.hw.AccelSurface.*;51import static sun.java2d.d3d.D3DContext.D3DContextCaps.*;52import sun.java2d.pipe.hw.AccelDeviceEventListener;5354public class D3DGraphicsConfig55extends Win32GraphicsConfig56implements AccelGraphicsConfig57{58private static ImageCapabilities imageCaps = new D3DImageCaps();5960private BufferCapabilities bufferCaps;61private D3DGraphicsDevice device;6263protected D3DGraphicsConfig(D3DGraphicsDevice device) {64super(device, 0);65this.device = device;66}6768public SurfaceData createManagedSurface(int w, int h, int transparency) {69return D3DSurfaceData.createData(this, w, h,70getColorModel(transparency),71null,72D3DSurfaceData.TEXTURE);73}7475@Override76public synchronized void displayChanged() {77super.displayChanged();78// the context could hold a reference to a D3DSurfaceData, which in79// turn has a reference back to this D3DGraphicsConfig, so in order80// for this instance to be disposed we need to break the connection81D3DRenderQueue rq = D3DRenderQueue.getInstance();82rq.lock();83try {84D3DContext.invalidateCurrentContext();85} finally {86rq.unlock();87}88}8990@Override91public ColorModel getColorModel(int transparency) {92switch (transparency) {93case Transparency.OPAQUE:94// REMIND: once the ColorModel spec is changed, this should be95// an opaque premultiplied DCM...96return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);97case Transparency.BITMASK:98return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);99case Transparency.TRANSLUCENT:100ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);101return new DirectColorModel(cs, 32,1020xff0000, 0xff00, 0xff, 0xff000000,103true, DataBuffer.TYPE_INT);104default:105return null;106}107}108109@Override110public String toString() {111return ("D3DGraphicsConfig[dev="+screen+",pixfmt="+visual+"]");112}113114/**115* The following methods are invoked from WComponentPeer.java rather116* than having the Win32-dependent implementations hardcoded in that117* class. This way the appropriate actions are taken based on the peer's118* GraphicsConfig, whether it is a Win32GraphicsConfig or a119* D3DGraphicsConfig.120*/121122/**123* Creates a new SurfaceData that will be associated with the given124* WComponentPeer. D3D9 doesn't allow rendering to the screen,125* so a GDI surface will be returned.126*/127@Override128public SurfaceData createSurfaceData(WComponentPeer peer,129int numBackBuffers)130{131return super.createSurfaceData(peer, numBackBuffers);132}133134/**135* The following methods correspond to the multibuffering methods in136* WComponentPeer.java...137*/138139/**140* Checks that the requested configuration is natively supported; if not,141* an AWTException is thrown.142*/143@Override144public void assertOperationSupported(Component target,145int numBuffers,146BufferCapabilities caps)147throws AWTException148{149if (numBuffers < 2 || numBuffers > 4) {150throw new AWTException("Only 2-4 buffers supported");151}152if (caps.getFlipContents() == BufferCapabilities.FlipContents.COPIED &&153numBuffers != 2)154{155throw new AWTException("FlipContents.COPIED is only" +156"supported for 2 buffers");157}158}159160/**161* Creates a D3D-based backbuffer for the given peer and returns the162* image wrapper.163*/164@Override165public VolatileImage createBackBuffer(WComponentPeer peer) {166Component target = (Component)peer.getTarget();167// it is possible for the component to have size 0x0, adjust it to168// be at least 1x1 to avoid IAE169int w = Math.max(1, target.getWidth());170int h = Math.max(1, target.getHeight());171return new SunVolatileImage(target, w, h, Boolean.TRUE);172}173174/**175* Performs the native D3D flip operation for the given target Component.176*/177@Override178public void flip(WComponentPeer peer,179Component target, VolatileImage backBuffer,180int x1, int y1, int x2, int y2,181BufferCapabilities.FlipContents flipAction)182{183// REMIND: we should actually get a surface data for the184// backBuffer's VI185SurfaceManager d3dvsm =186SurfaceManager.getManager(backBuffer);187SurfaceData sd = d3dvsm.getPrimarySurfaceData();188if (sd instanceof D3DSurfaceData) {189D3DSurfaceData d3dsd = (D3DSurfaceData)sd;190D3DSurfaceData.swapBuffers(d3dsd, x1, y1, x2, y2);191} else {192// the surface was likely lost could not have been restored193Graphics g = peer.getGraphics();194try {195g.drawImage(backBuffer,196x1, y1, x2, y2,197x1, y1, x2, y2,198null);199} finally {200g.dispose();201}202}203204if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {205Graphics g = backBuffer.getGraphics();206try {207g.setColor(target.getBackground());208g.fillRect(0, 0,209backBuffer.getWidth(),210backBuffer.getHeight());211} finally {212g.dispose();213}214}215}216217private static class D3DBufferCaps extends BufferCapabilities {218public D3DBufferCaps() {219// REMIND: should we indicate that the front-buffer220// (the on-screen rendering) is not accelerated?221super(imageCaps, imageCaps, FlipContents.UNDEFINED);222}223@Override224public boolean isMultiBufferAvailable() {225return true;226}227228}229230@Override231public BufferCapabilities getBufferCapabilities() {232if (bufferCaps == null) {233bufferCaps = new D3DBufferCaps();234}235return bufferCaps;236}237238private static class D3DImageCaps extends ImageCapabilities {239private D3DImageCaps() {240super(true);241}242@Override243public boolean isTrueVolatile() {244return true;245}246}247248@Override249public ImageCapabilities getImageCapabilities() {250return imageCaps;251}252253D3DGraphicsDevice getD3DDevice() {254return device;255}256257/**258* {@inheritDoc}259*260* @see sun.java2d.pipe.hw.BufferedContextProvider#getContext261*/262@Override263public D3DContext getContext() {264return device.getContext();265}266267/**268* {@inheritDoc}269*270* @see sun.java2d.pipe.hw.AccelGraphicsConfig#createCompatibleVolatileImage271*/272@Override273public VolatileImage274createCompatibleVolatileImage(int width, int height,275int transparency, int type)276{277if (type == FLIP_BACKBUFFER || type == WINDOW || type == UNDEFINED ||278transparency == Transparency.BITMASK)279{280return null;281}282boolean isOpaque = transparency == Transparency.OPAQUE;283if (type == RT_TEXTURE) {284int cap = isOpaque ? CAPS_RT_TEXTURE_OPAQUE : CAPS_RT_TEXTURE_ALPHA;285if (!device.isCapPresent(cap)) {286return null;287}288} else if (type == RT_PLAIN) {289if (!isOpaque && !device.isCapPresent(CAPS_RT_PLAIN_ALPHA)) {290return null;291}292}293294SunVolatileImage vi = new AccelTypedVolatileImage(this, width, height,295transparency, type);296Surface sd = vi.getDestSurface();297if (!(sd instanceof AccelSurface) ||298((AccelSurface)sd).getType() != type)299{300vi.flush();301vi = null;302}303304return vi;305}306307/**308* {@inheritDoc}309*310* @see sun.java2d.pipe.hw.AccelGraphicsConfig#getContextCapabilities311*/312@Override313public ContextCapabilities getContextCapabilities() {314return device.getContextCapabilities();315}316317@Override318public void addDeviceEventListener(AccelDeviceEventListener l) {319AccelDeviceEventNotifier.addListener(l, device.getScreen());320}321322@Override323public void removeDeviceEventListener(AccelDeviceEventListener l) {324AccelDeviceEventNotifier.removeListener(l);325}326}327328329