Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/awt/X11GraphicsConfig.java
32287 views
/*1* Copyright (c) 1997, 2013, 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.BufferCapabilities.FlipContents;30import java.awt.Component;31import java.awt.Toolkit;32import java.awt.GraphicsConfiguration;33import java.awt.GraphicsDevice;34import java.awt.Image;35import java.awt.ImageCapabilities;36import java.awt.Transparency;37import java.awt.image.BufferedImage;38import java.awt.image.ColorModel;39import java.awt.color.ColorSpace;40import java.awt.image.ComponentColorModel;41import java.awt.image.DirectColorModel;42import java.awt.image.DataBuffer;43import java.awt.image.VolatileImage;44import java.awt.image.WritableRaster;45import java.awt.geom.AffineTransform;46import java.awt.Rectangle;47import sun.java2d.Disposer;48import sun.java2d.DisposerRecord;49import sun.java2d.SurfaceData;50import sun.java2d.loops.RenderLoops;51import sun.java2d.loops.SurfaceType;52import sun.java2d.loops.CompositeType;53import sun.java2d.x11.X11SurfaceData;54import sun.awt.image.OffScreenImage;55import sun.awt.image.SunVolatileImage;56import sun.awt.image.SurfaceManager;57import sun.awt.X11ComponentPeer;5859/**60* This is an implementation of a GraphicsConfiguration object for a61* single X11 visual.62*63* @see GraphicsEnvironment64* @see GraphicsDevice65*/66public class X11GraphicsConfig extends GraphicsConfiguration67implements SurfaceManager.ProxiedGraphicsConfig68{69protected X11GraphicsDevice screen;70protected int visual;71int depth;72int colormap;73ColorModel colorModel;74long aData;75boolean doubleBuffer;76private Object disposerReferent = new Object();77private BufferCapabilities bufferCaps;78private static ImageCapabilities imageCaps =79new ImageCapabilities(X11SurfaceData.isAccelerationEnabled());8081// will be set on native level from init()82protected int bitsPerPixel;8384protected SurfaceType surfaceType;8586public RenderLoops solidloops;8788public static X11GraphicsConfig getConfig(X11GraphicsDevice device,89int visualnum, int depth,90int colormap,91boolean doubleBuffer)92{93return new X11GraphicsConfig(device, visualnum, depth, colormap, doubleBuffer);94}9596/*97* Note this method is currently here for backward compatibility98* as this was the method used in jdk 1.2 beta4 to create the99* X11GraphicsConfig objects. Java3D code had called this method100* explicitly so without this, if a user tries to use JDK1.2 fcs101* with Java3D beta1, a NoSuchMethod execption is thrown and102* the program exits. REMOVE this method after Java3D fcs is103* released!104*/105public static X11GraphicsConfig getConfig(X11GraphicsDevice device,106int visualnum, int depth,107int colormap, int type)108{109return new X11GraphicsConfig(device, visualnum, depth, colormap, false);110}111112private native int getNumColors();113private native void init(int visualNum, int screen);114private native ColorModel makeColorModel();115116protected X11GraphicsConfig(X11GraphicsDevice device,117int visualnum, int depth,118int colormap, boolean doubleBuffer)119{120this.screen = device;121this.visual = visualnum;122this.doubleBuffer = doubleBuffer;123this.depth = depth;124this.colormap = colormap;125init (visualnum, screen.getScreen());126127// add a record to the Disposer so that we destroy the native128// AwtGraphicsConfigData when this object goes away (i.e. after a129// display change event)130long x11CfgData = getAData();131Disposer.addRecord(disposerReferent,132new X11GCDisposerRecord(x11CfgData));133}134135/**136* Return the graphics device associated with this configuration.137*/138public GraphicsDevice getDevice() {139return screen;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}180181public Object getProxyKey() {182return screen.getProxyKeyFor(getSurfaceType());183}184185/**186* Return the RenderLoops this type of destination uses for187* solid fills and strokes.188*/189public synchronized RenderLoops getSolidLoops(SurfaceType stype) {190if (solidloops == null) {191solidloops = SurfaceData.makeRenderLoops(SurfaceType.OpaqueColor,192CompositeType.SrcNoEa,193stype);194}195return solidloops;196}197198/**199* Returns the color model associated with this configuration.200*/201public synchronized ColorModel getColorModel() {202if (colorModel == null) {203// Force SystemColors to be resolved before we create the CM204java.awt.SystemColor.window.getRGB();205// This method, makeColorModel(), can return null if the206// toolkit is not initialized yet.207// The toolkit will then call back to this routine after it208// is initialized and makeColorModel() should return a non-null209// colorModel.210colorModel = makeColorModel();211if (colorModel == null)212colorModel = Toolkit.getDefaultToolkit ().getColorModel ();213}214215return colorModel;216}217218/**219* Returns the color model associated with this configuration that220* supports the specified transparency.221*/222public ColorModel getColorModel(int transparency) {223switch (transparency) {224case Transparency.OPAQUE:225return getColorModel();226case Transparency.BITMASK:227return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);228case Transparency.TRANSLUCENT:229return ColorModel.getRGBdefault();230default:231return null;232}233}234235public static DirectColorModel createDCM32(int rMask, int gMask, int bMask,236int aMask, boolean aPre) {237return new DirectColorModel(238ColorSpace.getInstance(ColorSpace.CS_sRGB),23932, rMask, gMask, bMask, aMask, aPre, DataBuffer.TYPE_INT);240}241242public static ComponentColorModel createABGRCCM() {243ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);244int[] nBits = {8, 8, 8, 8};245int[] bOffs = {3, 2, 1, 0};246return new ComponentColorModel(cs, nBits, true, true,247Transparency.TRANSLUCENT,248DataBuffer.TYPE_BYTE);249}250251/**252* Returns the default Transform for this configuration. This253* Transform is typically the Identity transform for most normal254* screens. Device coordinates for screen and printer devices will255* have the origin in the upper left-hand corner of the target region of256* the device, with X coordinates257* increasing to the right and Y coordinates increasing downwards.258* For image buffers, this Transform will be the Identity transform.259*/260public AffineTransform getDefaultTransform() {261return new AffineTransform();262}263264/**265*266* Returns a Transform that can be composed with the default Transform267* of a Graphics2D so that 72 units in user space will equal 1 inch268* in device space.269* Given a Graphics2D, g, one can reset the transformation to create270* such a mapping by using the following pseudocode:271* <pre>272* GraphicsConfiguration gc = g.getGraphicsConfiguration();273*274* g.setTransform(gc.getDefaultTransform());275* g.transform(gc.getNormalizingTransform());276* </pre>277* Note that sometimes this Transform will be identity (e.g. for278* printers or metafile output) and that this Transform is only279* as accurate as the information supplied by the underlying system.280* For image buffers, this Transform will be the Identity transform,281* since there is no valid distance measurement.282*/283public AffineTransform getNormalizingTransform() {284double xscale = getXResolution(screen.getScreen()) / 72.0;285double yscale = getYResolution(screen.getScreen()) / 72.0;286return new AffineTransform(xscale, 0.0, 0.0, yscale, 0.0, 0.0);287}288289private native double getXResolution(int screen);290private native double getYResolution(int screen);291292public long getAData() {293return aData;294}295296public String toString() {297return ("X11GraphicsConfig[dev="+screen+298",vis=0x"+Integer.toHexString(visual)+299"]");300}301302/*303* Initialize JNI field and method IDs for fields that may be304* accessed from C.305*/306private static native void initIDs();307308static {309initIDs ();310}311312public Rectangle getBounds() {313return pGetBounds(screen.getScreen());314}315316public native Rectangle pGetBounds(int screenNum);317318private static class XDBECapabilities extends BufferCapabilities {319public XDBECapabilities() {320super(imageCaps, imageCaps, FlipContents.UNDEFINED);321}322}323324public BufferCapabilities getBufferCapabilities() {325if (bufferCaps == null) {326if (doubleBuffer) {327bufferCaps = new XDBECapabilities();328} else {329bufferCaps = super.getBufferCapabilities();330}331}332return bufferCaps;333}334335public ImageCapabilities getImageCapabilities() {336return imageCaps;337}338339public boolean isDoubleBuffered() {340return doubleBuffer;341}342343private static native void dispose(long x11ConfigData);344345private static class X11GCDisposerRecord implements DisposerRecord {346private long x11ConfigData;347public X11GCDisposerRecord(long x11CfgData) {348this.x11ConfigData = x11CfgData;349}350public synchronized void dispose() {351if (x11ConfigData != 0L) {352X11GraphicsConfig.dispose(x11ConfigData);353x11ConfigData = 0L;354}355}356}357358/**359* The following methods are invoked from {M,X}Toolkit.java and360* X11ComponentPeer.java rather than having the X11-dependent361* implementations hardcoded in those classes. This way the appropriate362* actions are taken based on the peer's GraphicsConfig, whether it is363* an X11GraphicsConfig or a GLXGraphicsConfig.364*/365366/**367* Creates a new SurfaceData that will be associated with the given368* X11ComponentPeer.369*/370public SurfaceData createSurfaceData(X11ComponentPeer peer) {371return X11SurfaceData.createData(peer);372}373374/**375* Creates a new hidden-acceleration image of the given width and height376* that is associated with the target Component.377*/378public Image createAcceleratedImage(Component target,379int width, int height)380{381// As of 1.7 we no longer create pmoffscreens here...382ColorModel model = getColorModel(Transparency.OPAQUE);383WritableRaster wr =384model.createCompatibleWritableRaster(width, height);385return new OffScreenImage(target, model, wr,386model.isAlphaPremultiplied());387}388389/**390* The following methods correspond to the multibuffering methods in391* X11ComponentPeer.java...392*/393394private native long createBackBuffer(long window, int swapAction);395private native void swapBuffers(long window, int swapAction);396397/**398* Attempts to create an XDBE-based backbuffer for the given peer. If399* the requested configuration is not natively supported, an AWTException400* is thrown. Otherwise, if the backbuffer creation is successful, a401* handle to the native backbuffer is returned.402*/403public long createBackBuffer(X11ComponentPeer peer,404int numBuffers, BufferCapabilities caps)405throws AWTException406{407if (!X11GraphicsDevice.isDBESupported()) {408throw new AWTException("Page flipping is not supported");409}410if (numBuffers > 2) {411throw new AWTException(412"Only double or single buffering is supported");413}414BufferCapabilities configCaps = getBufferCapabilities();415if (!configCaps.isPageFlipping()) {416throw new AWTException("Page flipping is not supported");417}418419long window = peer.getContentWindow();420int swapAction = getSwapAction(caps.getFlipContents());421422return createBackBuffer(window, swapAction);423}424425/**426* Destroys the backbuffer object represented by the given handle value.427*/428public native void destroyBackBuffer(long backBuffer);429430/**431* Creates a VolatileImage that essentially wraps the target Component's432* backbuffer, using the provided backbuffer handle.433*/434public VolatileImage createBackBufferImage(Component target,435long backBuffer)436{437// it is possible for the component to have size 0x0, adjust it to438// be at least 1x1 to avoid IAE439int w = Math.max(1, target.getWidth());440int h = Math.max(1, target.getHeight());441return new SunVolatileImage(target,442w, h,443Long.valueOf(backBuffer));444}445446/**447* Performs the native XDBE flip operation for the given target Component.448*/449public void flip(X11ComponentPeer peer,450Component target, VolatileImage xBackBuffer,451int x1, int y1, int x2, int y2,452BufferCapabilities.FlipContents flipAction)453{454long window = peer.getContentWindow();455int swapAction = getSwapAction(flipAction);456swapBuffers(window, swapAction);457}458459/**460* Maps the given FlipContents constant to the associated XDBE swap461* action constant.462*/463private static int getSwapAction(464BufferCapabilities.FlipContents flipAction) {465if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {466return 0x01;467} else if (flipAction == BufferCapabilities.FlipContents.PRIOR) {468return 0x02;469} else if (flipAction == BufferCapabilities.FlipContents.COPIED) {470return 0x03;471} else {472return 0x00; // UNDEFINED473}474}475476@Override477public boolean isTranslucencyCapable() {478return isTranslucencyCapable(getAData());479}480481private native boolean isTranslucencyCapable(long x11ConfigData);482}483484485