Path: blob/master/src/java.desktop/unix/classes/sun/awt/X11GraphicsConfig.java
41152 views
/*1* Copyright (c) 1997, 2021, 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.GraphicsConfiguration;31import java.awt.GraphicsDevice;32import java.awt.Image;33import java.awt.ImageCapabilities;34import java.awt.Rectangle;35import java.awt.Toolkit;36import java.awt.Transparency;37import java.awt.color.ColorSpace;38import java.awt.geom.AffineTransform;39import java.awt.image.ColorModel;40import java.awt.image.ComponentColorModel;41import java.awt.image.DataBuffer;42import java.awt.image.DirectColorModel;43import java.awt.image.VolatileImage;44import java.awt.image.WritableRaster;4546import sun.awt.image.OffScreenImage;47import sun.awt.image.SunVolatileImage;48import sun.awt.image.SurfaceManager;49import sun.java2d.Disposer;50import sun.java2d.DisposerRecord;51import sun.java2d.SurfaceData;52import sun.java2d.loops.CompositeType;53import sun.java2d.loops.RenderLoops;54import sun.java2d.loops.SurfaceType;55import sun.java2d.pipe.Region;56import sun.java2d.x11.X11SurfaceData;5758/**59* This is an implementation of a GraphicsConfiguration object for a60* single X11 visual.61*62* @see java.awt.GraphicsEnvironment63* @see GraphicsDevice64*/65public class X11GraphicsConfig extends GraphicsConfiguration66implements SurfaceManager.ProxiedGraphicsConfig67{68private final X11GraphicsDevice device;69protected int visual;70int depth;71int colormap;72ColorModel colorModel;73long aData;74boolean doubleBuffer;75private Object disposerReferent = new Object();76private BufferCapabilities bufferCaps;77private static ImageCapabilities imageCaps =78new ImageCapabilities(X11SurfaceData.isAccelerationEnabled());7980// will be set on native level from init()81protected int bitsPerPixel;8283protected SurfaceType surfaceType;8485public RenderLoops solidloops;8687public static X11GraphicsConfig getConfig(X11GraphicsDevice device,88int visualnum, int depth,89int colormap,90boolean doubleBuffer)91{92return new X11GraphicsConfig(device, visualnum, depth, colormap, doubleBuffer);93}9495/*96* Note this method is currently here for backward compatibility97* as this was the method used in jdk 1.2 beta4 to create the98* X11GraphicsConfig objects. Java3D code had called this method99* explicitly so without this, if a user tries to use JDK1.2 fcs100* with Java3D beta1, a NoSuchMethod execption is thrown and101* the program exits. REMOVE this method after Java3D fcs is102* released!103*/104public static X11GraphicsConfig getConfig(X11GraphicsDevice device,105int visualnum, int depth,106int colormap, int type)107{108return new X11GraphicsConfig(device, visualnum, depth, colormap, false);109}110111private native int getNumColors();112private native void init(int visualNum, int screen);113private native ColorModel makeColorModel();114115protected X11GraphicsConfig(X11GraphicsDevice device,116int visualnum, int depth,117int colormap, boolean doubleBuffer)118{119this.device = device;120this.visual = visualnum;121this.doubleBuffer = doubleBuffer;122this.depth = depth;123this.colormap = colormap;124init (visualnum, device.getScreen());125126// add a record to the Disposer so that we destroy the native127// AwtGraphicsConfigData when this object goes away (i.e. after a128// display change event)129long x11CfgData = getAData();130Disposer.addRecord(disposerReferent,131new X11GCDisposerRecord(x11CfgData));132}133134/**135* Return the graphics device associated with this configuration.136*/137@Override138public X11GraphicsDevice getDevice() {139return device;140}141142/**143* Returns the visual id associated with this configuration.144*/145public int getVisual () {146return visual;147}148149150/**151* Returns the depth associated with this configuration.152*/153public int getDepth () {154return depth;155}156157/**158* Returns the colormap associated with this configuration.159*/160public int getColormap () {161return colormap;162}163164/**165* Returns a number of bits allocated per pixel166* (might be different from depth)167*/168public int getBitsPerPixel() {169return bitsPerPixel;170}171172public synchronized SurfaceType getSurfaceType() {173if (surfaceType != null) {174return surfaceType;175}176177surfaceType = X11SurfaceData.getSurfaceType(this, Transparency.OPAQUE);178return surfaceType;179}180181@Override182public Object getProxyKey() {183return device.getProxyKeyFor(getSurfaceType());184}185186/**187* Return the RenderLoops this type of destination uses for188* solid fills and strokes.189*/190public synchronized RenderLoops getSolidLoops(SurfaceType stype) {191if (solidloops == null) {192solidloops = SurfaceData.makeRenderLoops(SurfaceType.OpaqueColor,193CompositeType.SrcNoEa,194stype);195}196return solidloops;197}198199/**200* Returns the color model associated with this configuration.201*/202@Override203public synchronized ColorModel getColorModel() {204if (colorModel == null) {205// Force SystemColors to be resolved before we create the CM206java.awt.SystemColor.window.getRGB();207// This method, makeColorModel(), can return null if the208// toolkit is not initialized yet.209// The toolkit will then call back to this routine after it210// is initialized and makeColorModel() should return a non-null211// colorModel.212colorModel = makeColorModel();213if (colorModel == null)214colorModel = Toolkit.getDefaultToolkit ().getColorModel ();215}216217return colorModel;218}219220/**221* Returns the color model associated with this configuration that222* supports the specified transparency.223*/224@Override225public ColorModel getColorModel(int transparency) {226switch (transparency) {227case Transparency.OPAQUE:228return getColorModel();229case Transparency.BITMASK:230return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);231case Transparency.TRANSLUCENT:232return ColorModel.getRGBdefault();233default:234return null;235}236}237238public static DirectColorModel createDCM32(int rMask, int gMask, int bMask,239int aMask, boolean aPre) {240return new DirectColorModel(241ColorSpace.getInstance(ColorSpace.CS_sRGB),24232, rMask, gMask, bMask, aMask, aPre, DataBuffer.TYPE_INT);243}244245public static ComponentColorModel createABGRCCM() {246ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);247int[] nBits = {8, 8, 8, 8};248int[] bOffs = {3, 2, 1, 0};249return new ComponentColorModel(cs, nBits, true, true,250Transparency.TRANSLUCENT,251DataBuffer.TYPE_BYTE);252}253254/**255* Returns the default Transform for this configuration. This256* Transform is typically the Identity transform for most normal257* screens. Device coordinates for screen and printer devices will258* have the origin in the upper left-hand corner of the target region of259* the device, with X coordinates260* increasing to the right and Y coordinates increasing downwards.261* For image buffers, this Transform will be the Identity transform.262*/263@Override264public AffineTransform getDefaultTransform() {265double scale = getScale();266return AffineTransform.getScaleInstance(scale, scale);267}268269public int getScale() {270return getDevice().getScaleFactor();271}272273public int scaleUp(int x) {274return Region.clipRound(x * (double)getScale());275}276277public int scaleDown(int x) {278return Region.clipRound(x / (double)getScale());279}280281/**282*283* Returns a Transform that can be composed with the default Transform284* of a Graphics2D so that 72 units in user space will equal 1 inch285* in device space.286* Given a Graphics2D, g, one can reset the transformation to create287* such a mapping by using the following pseudocode:288* <pre>289* GraphicsConfiguration gc = g.getGraphicsConfiguration();290*291* g.setTransform(gc.getDefaultTransform());292* g.transform(gc.getNormalizingTransform());293* </pre>294* Note that sometimes this Transform will be identity (e.g. for295* printers or metafile output) and that this Transform is only296* as accurate as the information supplied by the underlying system.297* For image buffers, this Transform will be the Identity transform,298* since there is no valid distance measurement.299*/300@Override301public AffineTransform getNormalizingTransform() {302double xscale = getXResolution(device.getScreen()) / 72.0;303double yscale = getYResolution(device.getScreen()) / 72.0;304return new AffineTransform(xscale, 0.0, 0.0, yscale, 0.0, 0.0);305}306307private native double getXResolution(int screen);308private native double getYResolution(int screen);309310public long getAData() {311return aData;312}313314public String toString() {315return ("X11GraphicsConfig[dev="+device+316",vis=0x"+Integer.toHexString(visual)+317"]");318}319320/*321* Initialize JNI field and method IDs for fields that may be322* accessed from C.323*/324private static native void initIDs();325326static {327initIDs ();328}329330@Override331public final Rectangle getBounds() {332return device.getBounds();333}334335private static class XDBECapabilities extends BufferCapabilities {336public XDBECapabilities() {337super(imageCaps, imageCaps, FlipContents.UNDEFINED);338}339}340341@Override342public BufferCapabilities getBufferCapabilities() {343if (bufferCaps == null) {344if (doubleBuffer) {345bufferCaps = new XDBECapabilities();346} else {347bufferCaps = super.getBufferCapabilities();348}349}350return bufferCaps;351}352353@Override354public ImageCapabilities getImageCapabilities() {355return imageCaps;356}357358public boolean isDoubleBuffered() {359return doubleBuffer;360}361362private static native void dispose(long x11ConfigData);363364private static class X11GCDisposerRecord implements DisposerRecord {365private long x11ConfigData;366public X11GCDisposerRecord(long x11CfgData) {367this.x11ConfigData = x11CfgData;368}369@Override370public synchronized void dispose() {371if (x11ConfigData != 0L) {372X11GraphicsConfig.dispose(x11ConfigData);373x11ConfigData = 0L;374}375}376}377378/**379* The following methods are invoked from {M,X}Toolkit.java and380* X11ComponentPeer.java rather than having the X11-dependent381* implementations hardcoded in those classes. This way the appropriate382* actions are taken based on the peer's GraphicsConfig, whether it is383* an X11GraphicsConfig or a GLXGraphicsConfig.384*/385386/**387* Creates a new SurfaceData that will be associated with the given388* X11ComponentPeer.389*/390public SurfaceData createSurfaceData(X11ComponentPeer peer) {391return X11SurfaceData.createData(peer);392}393394/**395* Creates a new hidden-acceleration image of the given width and height396* that is associated with the target Component.397*/398public Image createAcceleratedImage(Component target,399int width, int height)400{401// As of 1.7 we no longer create pmoffscreens here...402ColorModel model = getColorModel(Transparency.OPAQUE);403WritableRaster wr =404model.createCompatibleWritableRaster(width, height);405return new OffScreenImage(target, model, wr,406model.isAlphaPremultiplied());407}408409/**410* The following methods correspond to the multibuffering methods in411* X11ComponentPeer.java...412*/413414private native long createBackBuffer(long window, int swapAction);415private native void swapBuffers(long window, int swapAction);416417/**418* Attempts to create an XDBE-based backbuffer for the given peer. If419* the requested configuration is not natively supported, an AWTException420* is thrown. Otherwise, if the backbuffer creation is successful, a421* handle to the native backbuffer is returned.422*/423public long createBackBuffer(X11ComponentPeer peer,424int numBuffers, BufferCapabilities caps)425throws AWTException426{427if (!X11GraphicsDevice.isDBESupported()) {428throw new AWTException("Page flipping is not supported");429}430if (numBuffers > 2) {431throw new AWTException(432"Only double or single buffering is supported");433}434BufferCapabilities configCaps = getBufferCapabilities();435if (!configCaps.isPageFlipping()) {436throw new AWTException("Page flipping is not supported");437}438439long window = peer.getContentWindow();440int swapAction = getSwapAction(caps.getFlipContents());441442return createBackBuffer(window, swapAction);443}444445/**446* Destroys the backbuffer object represented by the given handle value.447*/448public native void destroyBackBuffer(long backBuffer);449450/**451* Creates a VolatileImage that essentially wraps the target Component's452* backbuffer, using the provided backbuffer handle.453*/454public VolatileImage createBackBufferImage(Component target,455long backBuffer)456{457// it is possible for the component to have size 0x0, adjust it to458// be at least 1x1 to avoid IAE459int w = Math.max(1, target.getWidth());460int h = Math.max(1, target.getHeight());461return new SunVolatileImage(target,462w, h,463Long.valueOf(backBuffer));464}465466/**467* Performs the native XDBE flip operation for the given target Component.468*/469public void flip(X11ComponentPeer peer,470Component target, VolatileImage xBackBuffer,471int x1, int y1, int x2, int y2,472BufferCapabilities.FlipContents flipAction)473{474long window = peer.getContentWindow();475int swapAction = getSwapAction(flipAction);476swapBuffers(window, swapAction);477}478479/**480* Maps the given FlipContents constant to the associated XDBE swap481* action constant.482*/483private static int getSwapAction(484BufferCapabilities.FlipContents flipAction) {485if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {486return 0x01;487} else if (flipAction == BufferCapabilities.FlipContents.PRIOR) {488return 0x02;489} else if (flipAction == BufferCapabilities.FlipContents.COPIED) {490return 0x03;491} else {492return 0x00; // UNDEFINED493}494}495496@Override497public boolean isTranslucencyCapable() {498return isTranslucencyCapable(getAData());499}500501private native boolean isTranslucencyCapable(long x11ConfigData);502}503504505