Path: blob/master/src/java.desktop/windows/classes/sun/awt/Win32GraphicsConfig.java
41153 views
/*1* Copyright (c) 1997, 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. 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.awt;2627import java.awt.AWTException;28import java.awt.BufferCapabilities;29import java.awt.Component;30import java.awt.Graphics;31import java.awt.GraphicsConfiguration;32import java.awt.GraphicsDevice;33import java.awt.GraphicsEnvironment;34import java.awt.Image;35import java.awt.Rectangle;36import java.awt.Transparency;37import java.awt.geom.AffineTransform;38import java.awt.image.ColorModel;39import java.awt.image.DirectColorModel;40import java.awt.image.VolatileImage;41import java.awt.image.WritableRaster;4243import sun.awt.image.OffScreenImage;44import sun.awt.image.SunVolatileImage;45import sun.awt.image.SurfaceManager;46import sun.awt.windows.WComponentPeer;47import sun.java2d.SurfaceData;48import sun.java2d.loops.CompositeType;49import sun.java2d.loops.RenderLoops;50import sun.java2d.loops.SurfaceType;51import sun.java2d.windows.GDIWindowSurfaceData;5253/**54* This is an implementation of a GraphicsConfiguration object for a55* single Win32 visual.56*57* @see GraphicsEnvironment58* @see GraphicsDevice59*/60public class Win32GraphicsConfig extends GraphicsConfiguration61implements DisplayChangedListener, SurfaceManager.ProxiedGraphicsConfig62{63private final Win32GraphicsDevice device;64protected int visual; //PixelFormatID65protected RenderLoops solidloops;6667private static native void initIDs();6869static {70initIDs();71}7273/**74* Returns a Win32GraphicsConfiguration object with the given device75* and PixelFormat. Note that this method does NOT check to ensure that76* the returned Win32GraphicsConfig will correctly support rendering into a77* Java window. This method is provided so that client code can do its78* own checking as to the appropriateness of a particular PixelFormat.79* Safer access to Win32GraphicsConfigurations is provided by80* Win32GraphicsDevice.getConfigurations().81*/82public static Win32GraphicsConfig getConfig(Win32GraphicsDevice device,83int pixFormatID)84{85return new Win32GraphicsConfig(device, pixFormatID);86}8788/**89* @deprecated as of JDK version 1.390* replaced by {@code getConfig()}91*/92@Deprecated93public Win32GraphicsConfig(GraphicsDevice device, int visualnum) {94this.device = (Win32GraphicsDevice)device;95this.visual = visualnum;96((Win32GraphicsDevice)device).addDisplayChangedListener(this);97}9899/**100* Return the graphics device associated with this configuration.101*/102@Override103public Win32GraphicsDevice getDevice() {104return device;105}106107/**108* Return the PixelFormatIndex this GraphicsConfig uses109*/110public int getVisual() {111return visual;112}113114@Override115public Object getProxyKey() {116return device;117}118119/**120* Return the RenderLoops this type of destination uses for121* solid fills and strokes.122*/123private SurfaceType sTypeOrig = null;124public synchronized RenderLoops getSolidLoops(SurfaceType stype) {125if (solidloops == null || sTypeOrig != stype) {126solidloops = SurfaceData.makeRenderLoops(SurfaceType.OpaqueColor,127CompositeType.SrcNoEa,128stype);129sTypeOrig = stype;130}131return solidloops;132}133134/**135* Returns the color model associated with this configuration.136*/137@Override138public synchronized ColorModel getColorModel() {139return device.getColorModel();140}141142/**143* Returns a new color model for this configuration. This call144* is only used internally, by images and components that are145* associated with the graphics device. When attributes of that146* device change (for example, when the device palette is updated),147* then this device-based color model will be updated internally148* to reflect the new situation.149*/150public ColorModel getDeviceColorModel() {151return device.getDynamicColorModel();152}153154/**155* Returns the color model associated with this configuration that156* supports the specified transparency.157*/158@Override159public ColorModel getColorModel(int transparency) {160switch (transparency) {161case Transparency.OPAQUE:162return getColorModel();163case Transparency.BITMASK:164return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);165case Transparency.TRANSLUCENT:166return ColorModel.getRGBdefault();167default:168return null;169}170}171172/**173* Returns the default Transform for this configuration. This174* Transform is typically the Identity transform for most normal175* screens. Device coordinates for screen and printer devices will176* have the origin in the upper left-hand corner of the target region of177* the device, with X coordinates178* increasing to the right and Y coordinates increasing downwards.179* For image buffers, this Transform will be the Identity transform.180*/181@Override182public AffineTransform getDefaultTransform() {183double scaleX = device.getDefaultScaleX();184double scaleY = device.getDefaultScaleY();185return AffineTransform.getScaleInstance(scaleX, scaleY);186}187188/**189*190* Returns a Transform that can be composed with the default Transform191* of a Graphics2D so that 72 units in user space will equal 1 inch192* in device space.193* Given a Graphics2D, g, one can reset the transformation to create194* such a mapping by using the following pseudocode:195* <pre>196* GraphicsConfiguration gc = g.getGraphicsConfiguration();197*198* g.setTransform(gc.getDefaultTransform());199* g.transform(gc.getNormalizingTransform());200* </pre>201* Note that sometimes this Transform will be identity (e.g. for202* printers or metafile output) and that this Transform is only203* as accurate as the information supplied by the underlying system.204* For image buffers, this Transform will be the Identity transform,205* since there is no valid distance measurement.206*/207@Override208public AffineTransform getNormalizingTransform() {209Win32GraphicsEnvironment ge = (Win32GraphicsEnvironment)210GraphicsEnvironment.getLocalGraphicsEnvironment();211double xscale = ge.getXResolution() / 72.0;212double yscale = ge.getYResolution() / 72.0;213return new AffineTransform(xscale, 0.0, 0.0, yscale, 0.0, 0.0);214}215216public String toString() {217return (super.toString()+"[dev="+device+",pixfmt="+visual+"]");218}219220private native Rectangle getBounds(int screen);221222@Override223public Rectangle getBounds() {224return getBounds(device.getScreen());225}226227@Override228public synchronized void displayChanged() {229solidloops = null;230}231232@Override233public void paletteChanged() {}234235/**236* The following methods are invoked from WComponentPeer.java rather237* than having the Win32-dependent implementations hardcoded in that238* class. This way the appropriate actions are taken based on the peer's239* GraphicsConfig, whether it is a Win32GraphicsConfig or a240* WGLGraphicsConfig.241*/242243/**244* Creates a new SurfaceData that will be associated with the given245* WComponentPeer.246*/247public SurfaceData createSurfaceData(WComponentPeer peer,248int numBackBuffers)249{250return GDIWindowSurfaceData.createData(peer);251}252253/**254* Creates a new managed image of the given width and height255* that is associated with the target Component.256*/257public Image createAcceleratedImage(Component target,258int width, int height)259{260ColorModel model = getColorModel(Transparency.OPAQUE);261WritableRaster wr =262model.createCompatibleWritableRaster(width, height);263return new OffScreenImage(target, model, wr,264model.isAlphaPremultiplied());265}266267/**268* The following methods correspond to the multibuffering methods in269* WComponentPeer.java...270*/271272/**273* Checks that the requested configuration is natively supported; if not,274* an AWTException is thrown.275*/276public void assertOperationSupported(Component target,277int numBuffers,278BufferCapabilities caps)279throws AWTException280{281// the default pipeline doesn't support flip buffer strategy282throw new AWTException(283"The operation requested is not supported");284}285286/**287* This method is called from WComponentPeer when a surface data is replaced288* REMIND: while the default pipeline doesn't support flipping, it may289* happen that the accelerated device may have this graphics config290* (like if the device restoration failed when one device exits fs mode291* while others remain).292*/293public VolatileImage createBackBuffer(WComponentPeer peer) {294Component target = (Component)peer.getTarget();295return new SunVolatileImage(target,296target.getWidth(), target.getHeight(),297Boolean.TRUE);298}299300/**301* Performs the native flip operation for the given target Component.302*303* REMIND: we should really not get here because that would mean that304* a FLIP BufferStrategy has been created, and one could only be created305* if accelerated pipeline is present but in some rare (and transitional)306* cases it may happen that the accelerated graphics device may have a307* default graphics configuraiton, so this is just a precaution.308*/309public void flip(WComponentPeer peer,310Component target, VolatileImage backBuffer,311int x1, int y1, int x2, int y2,312BufferCapabilities.FlipContents flipAction)313{314if (flipAction == BufferCapabilities.FlipContents.COPIED ||315flipAction == BufferCapabilities.FlipContents.UNDEFINED) {316Graphics g = peer.getGraphics();317try {318g.drawImage(backBuffer,319x1, y1, x2, y2,320x1, y1, x2, y2,321null);322} finally {323g.dispose();324}325} else if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {326Graphics g = backBuffer.getGraphics();327try {328g.setColor(target.getBackground());329g.fillRect(0, 0,330backBuffer.getWidth(),331backBuffer.getHeight());332} finally {333g.dispose();334}335}336// the rest of the flip actions are not supported337}338339@Override340public boolean isTranslucencyCapable() {341//XXX: worth checking if 8-bit? Anyway, it doesn't hurt.342return true;343}344}345346347