1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.awt;
  27 
  28 import java.awt.AWTException;
  29 import java.awt.BufferCapabilities;
  30 import java.awt.Component;
  31 import java.awt.Toolkit;
  32 import java.awt.GraphicsConfiguration;
  33 import java.awt.GraphicsDevice;
  34 import java.awt.Image;
  35 import java.awt.ImageCapabilities;
  36 import java.awt.Transparency;
  37 import java.awt.image.ColorModel;
  38 import java.awt.color.ColorSpace;
  39 import java.awt.image.ComponentColorModel;
  40 import java.awt.image.DirectColorModel;
  41 import java.awt.image.DataBuffer;
  42 import java.awt.image.VolatileImage;
  43 import java.awt.image.WritableRaster;
  44 import java.awt.geom.AffineTransform;
  45 import java.awt.Rectangle;
  46 import sun.java2d.Disposer;
  47 import sun.java2d.DisposerRecord;
  48 import sun.java2d.SurfaceData;
  49 import sun.java2d.loops.RenderLoops;
  50 import sun.java2d.loops.SurfaceType;
  51 import sun.java2d.loops.CompositeType;
  52 import sun.java2d.x11.X11SurfaceData;
  53 import sun.awt.image.OffScreenImage;
  54 import sun.awt.image.SunVolatileImage;
  55 import sun.awt.image.SurfaceManager;
  56 
  57 /**
  58  * This is an implementation of a GraphicsConfiguration object for a
  59  * single X11 visual.
  60  *
  61  * @see java.awt.GraphicsEnvironment
  62  * @see GraphicsDevice
  63  */
  64 public class X11GraphicsConfig extends GraphicsConfiguration
  65     implements SurfaceManager.ProxiedGraphicsConfig
  66 {
  67     protected X11GraphicsDevice screen;
  68     protected int visual;
  69     int depth;
  70     int colormap;
  71     ColorModel colorModel;
  72     long aData;
  73     boolean doubleBuffer;
  74     private Object disposerReferent = new Object();
  75     private BufferCapabilities bufferCaps;
  76     private static ImageCapabilities imageCaps =
  77         new ImageCapabilities(X11SurfaceData.isAccelerationEnabled());
  78 
  79     // will be set on native level from init()
  80     protected int bitsPerPixel;
  81 
  82     protected SurfaceType surfaceType;
  83 
  84     public RenderLoops solidloops;
  85 
  86     public static X11GraphicsConfig getConfig(X11GraphicsDevice device,
  87                                               int visualnum, int depth,
  88                                               int colormap,
  89                                               boolean doubleBuffer)
  90     {
  91         return new X11GraphicsConfig(device, visualnum, depth, colormap, doubleBuffer);
  92     }
  93 
  94     /*
  95      * Note this method is currently here for backward compatibility
  96      * as this was the method used in jdk 1.2 beta4 to create the
  97      * X11GraphicsConfig objects. Java3D code had called this method
  98      * explicitly so without this, if a user tries to use JDK1.2 fcs
  99      * with Java3D beta1, a NoSuchMethod execption is thrown and
 100      * the program exits. REMOVE this method after Java3D fcs is
 101      * released!
 102      */
 103     public static X11GraphicsConfig getConfig(X11GraphicsDevice device,
 104                                               int visualnum, int depth,
 105                                               int colormap, int type)
 106     {
 107         return new X11GraphicsConfig(device, visualnum, depth, colormap, false);
 108     }
 109 
 110     private native int getNumColors();
 111     private native void init(int visualNum, int screen);
 112     private native ColorModel makeColorModel();
 113 
 114     protected X11GraphicsConfig(X11GraphicsDevice device,
 115                                 int visualnum, int depth,
 116                                 int colormap, boolean doubleBuffer)
 117     {
 118         this.screen = device;
 119         this.visual = visualnum;
 120         this.doubleBuffer = doubleBuffer;
 121         this.depth = depth;
 122         this.colormap = colormap;
 123         init (visualnum, screen.getScreen());
 124 
 125         // add a record to the Disposer so that we destroy the native
 126         // AwtGraphicsConfigData when this object goes away (i.e. after a
 127         // display change event)
 128         long x11CfgData = getAData();
 129         Disposer.addRecord(disposerReferent,
 130                            new X11GCDisposerRecord(x11CfgData));
 131     }
 132 
 133     /**
 134      * Return the graphics device associated with this configuration.
 135      */
 136     public GraphicsDevice getDevice() {
 137         return screen;
 138     }
 139 
 140     /**
 141      * Returns the visual id associated with this configuration.
 142      */
 143     public int getVisual () {
 144         return visual;
 145     }
 146 
 147 
 148     /**
 149      * Returns the depth associated with this configuration.
 150      */
 151     public int getDepth () {
 152         return depth;
 153     }
 154 
 155     /**
 156      * Returns the colormap associated with this configuration.
 157      */
 158     public int getColormap () {
 159         return colormap;
 160     }
 161 
 162     /**
 163      * Returns a number of bits allocated per pixel
 164      * (might be different from depth)
 165      */
 166     public int getBitsPerPixel() {
 167         return bitsPerPixel;
 168     }
 169 
 170     public synchronized SurfaceType getSurfaceType() {
 171         if (surfaceType != null) {
 172             return surfaceType;
 173         }
 174 
 175         surfaceType = X11SurfaceData.getSurfaceType(this, Transparency.OPAQUE);
 176         return surfaceType;
 177     }
 178 
 179     public Object getProxyKey() {
 180         return screen.getProxyKeyFor(getSurfaceType());
 181     }
 182 
 183     /**
 184      * Return the RenderLoops this type of destination uses for
 185      * solid fills and strokes.
 186      */
 187     public synchronized RenderLoops getSolidLoops(SurfaceType stype) {
 188         if (solidloops == null) {
 189             solidloops = SurfaceData.makeRenderLoops(SurfaceType.OpaqueColor,
 190                                                      CompositeType.SrcNoEa,
 191                                                      stype);
 192         }
 193         return solidloops;
 194     }
 195 
 196     /**
 197      * Returns the color model associated with this configuration.
 198      */
 199     public synchronized ColorModel getColorModel() {
 200         if (colorModel == null)  {
 201             // Force SystemColors to be resolved before we create the CM
 202             java.awt.SystemColor.window.getRGB();
 203             // This method, makeColorModel(), can return null if the
 204             // toolkit is not initialized yet.
 205             // The toolkit will then call back to this routine after it
 206             // is initialized and makeColorModel() should return a non-null
 207             // colorModel.
 208             colorModel = makeColorModel();
 209             if (colorModel == null)
 210                 colorModel = Toolkit.getDefaultToolkit ().getColorModel ();
 211         }
 212 
 213         return colorModel;
 214     }
 215 
 216     /**
 217      * Returns the color model associated with this configuration that
 218      * supports the specified transparency.
 219      */
 220     public ColorModel getColorModel(int transparency) {
 221         switch (transparency) {
 222         case Transparency.OPAQUE:
 223             return getColorModel();
 224         case Transparency.BITMASK:
 225             return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
 226         case Transparency.TRANSLUCENT:
 227             return ColorModel.getRGBdefault();
 228         default:
 229             return null;
 230         }
 231     }
 232 
 233     public static DirectColorModel createDCM32(int rMask, int gMask, int bMask,
 234                                                int aMask, boolean aPre) {
 235         return new DirectColorModel(
 236             ColorSpace.getInstance(ColorSpace.CS_sRGB),
 237             32, rMask, gMask, bMask, aMask, aPre, DataBuffer.TYPE_INT);
 238     }
 239 
 240     public static ComponentColorModel createABGRCCM() {
 241         ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
 242         int[] nBits = {8, 8, 8, 8};
 243         int[] bOffs = {3, 2, 1, 0};
 244         return new ComponentColorModel(cs, nBits, true, true,
 245                                        Transparency.TRANSLUCENT,
 246                                        DataBuffer.TYPE_BYTE);
 247     }
 248 
 249     /**
 250      * Returns the default Transform for this configuration.  This
 251      * Transform is typically the Identity transform for most normal
 252      * screens.  Device coordinates for screen and printer devices will
 253      * have the origin in the upper left-hand corner of the target region of
 254      * the device, with X coordinates
 255      * increasing to the right and Y coordinates increasing downwards.
 256      * For image buffers, this Transform will be the Identity transform.
 257      */
 258     public AffineTransform getDefaultTransform() {
 259         return new AffineTransform();
 260     }
 261 
 262     /**
 263      *
 264      * Returns a Transform that can be composed with the default Transform
 265      * of a Graphics2D so that 72 units in user space will equal 1 inch
 266      * in device space.
 267      * Given a Graphics2D, g, one can reset the transformation to create
 268      * such a mapping by using the following pseudocode:
 269      * <pre>
 270      *      GraphicsConfiguration gc = g.getGraphicsConfiguration();
 271      *
 272      *      g.setTransform(gc.getDefaultTransform());
 273      *      g.transform(gc.getNormalizingTransform());
 274      * </pre>
 275      * Note that sometimes this Transform will be identity (e.g. for
 276      * printers or metafile output) and that this Transform is only
 277      * as accurate as the information supplied by the underlying system.
 278      * For image buffers, this Transform will be the Identity transform,
 279      * since there is no valid distance measurement.
 280      */
 281     public AffineTransform getNormalizingTransform() {
 282         double xscale = getXResolution(screen.getScreen()) / 72.0;
 283         double yscale = getYResolution(screen.getScreen()) / 72.0;
 284         return new AffineTransform(xscale, 0.0, 0.0, yscale, 0.0, 0.0);
 285     }
 286 
 287     private native double getXResolution(int screen);
 288     private native double getYResolution(int screen);
 289 
 290     public long getAData() {
 291         return aData;
 292     }
 293 
 294     public String toString() {
 295         return ("X11GraphicsConfig[dev="+screen+
 296                 ",vis=0x"+Integer.toHexString(visual)+
 297                 "]");
 298     }
 299 
 300     /*
 301      * Initialize JNI field and method IDs for fields that may be
 302      *  accessed from C.
 303      */
 304     private static native void initIDs();
 305 
 306     static {
 307         initIDs ();
 308     }
 309 
 310     public Rectangle getBounds() {
 311         return pGetBounds(screen.getScreen());
 312     }
 313 
 314     private native Rectangle pGetBounds(int screenNum);
 315 
 316     private static class XDBECapabilities extends BufferCapabilities {
 317         public XDBECapabilities() {
 318             super(imageCaps, imageCaps, FlipContents.UNDEFINED);
 319         }
 320     }
 321 
 322     public BufferCapabilities getBufferCapabilities() {
 323         if (bufferCaps == null) {
 324             if (doubleBuffer) {
 325                 bufferCaps = new XDBECapabilities();
 326             } else {
 327                 bufferCaps = super.getBufferCapabilities();
 328             }
 329         }
 330         return bufferCaps;
 331     }
 332 
 333     public ImageCapabilities getImageCapabilities() {
 334         return imageCaps;
 335     }
 336 
 337     public boolean isDoubleBuffered() {
 338         return doubleBuffer;
 339     }
 340 
 341     private static native void dispose(long x11ConfigData);
 342 
 343     private static class X11GCDisposerRecord implements DisposerRecord {
 344         private long x11ConfigData;
 345         public X11GCDisposerRecord(long x11CfgData) {
 346             this.x11ConfigData = x11CfgData;
 347         }
 348         public synchronized void dispose() {
 349             if (x11ConfigData != 0L) {
 350                 X11GraphicsConfig.dispose(x11ConfigData);
 351                 x11ConfigData = 0L;
 352             }
 353         }
 354     }
 355 
 356     /**
 357      * The following methods are invoked from {M,X}Toolkit.java and
 358      * X11ComponentPeer.java rather than having the X11-dependent
 359      * implementations hardcoded in those classes.  This way the appropriate
 360      * actions are taken based on the peer's GraphicsConfig, whether it is
 361      * an X11GraphicsConfig or a GLXGraphicsConfig.
 362      */
 363 
 364     /**
 365      * Creates a new SurfaceData that will be associated with the given
 366      * X11ComponentPeer.
 367      */
 368     public SurfaceData createSurfaceData(X11ComponentPeer peer) {
 369         return X11SurfaceData.createData(peer);
 370     }
 371 
 372     /**
 373      * Creates a new hidden-acceleration image of the given width and height
 374      * that is associated with the target Component.
 375      */
 376     public Image createAcceleratedImage(Component target,
 377                                         int width, int height)
 378     {
 379         // As of 1.7 we no longer create pmoffscreens here...
 380         ColorModel model = getColorModel(Transparency.OPAQUE);
 381         WritableRaster wr =
 382             model.createCompatibleWritableRaster(width, height);
 383         return new OffScreenImage(target, model, wr,
 384                                   model.isAlphaPremultiplied());
 385     }
 386 
 387     /**
 388      * The following methods correspond to the multibuffering methods in
 389      * X11ComponentPeer.java...
 390      */
 391 
 392     private native long createBackBuffer(long window, int swapAction);
 393     private native void swapBuffers(long window, int swapAction);
 394 
 395     /**
 396      * Attempts to create an XDBE-based backbuffer for the given peer.  If
 397      * the requested configuration is not natively supported, an AWTException
 398      * is thrown.  Otherwise, if the backbuffer creation is successful, a
 399      * handle to the native backbuffer is returned.
 400      */
 401     public long createBackBuffer(X11ComponentPeer peer,
 402                                  int numBuffers, BufferCapabilities caps)
 403         throws AWTException
 404     {
 405         if (!X11GraphicsDevice.isDBESupported()) {
 406             throw new AWTException("Page flipping is not supported");
 407         }
 408         if (numBuffers > 2) {
 409             throw new AWTException(
 410                 "Only double or single buffering is supported");
 411         }
 412         BufferCapabilities configCaps = getBufferCapabilities();
 413         if (!configCaps.isPageFlipping()) {
 414             throw new AWTException("Page flipping is not supported");
 415         }
 416 
 417         long window = peer.getContentWindow();
 418         int swapAction = getSwapAction(caps.getFlipContents());
 419 
 420         return createBackBuffer(window, swapAction);
 421     }
 422 
 423     /**
 424      * Destroys the backbuffer object represented by the given handle value.
 425      */
 426     public native void destroyBackBuffer(long backBuffer);
 427 
 428     /**
 429      * Creates a VolatileImage that essentially wraps the target Component's
 430      * backbuffer, using the provided backbuffer handle.
 431      */
 432     public VolatileImage createBackBufferImage(Component target,
 433                                                long backBuffer)
 434     {
 435         return new SunVolatileImage(target,
 436                                     target.getWidth(), target.getHeight(),
 437                                     Long.valueOf(backBuffer));
 438     }
 439 
 440     /**
 441      * Performs the native XDBE flip operation for the given target Component.
 442      */
 443     public void flip(X11ComponentPeer peer,
 444                      Component target, VolatileImage xBackBuffer,
 445                      int x1, int y1, int x2, int y2,
 446                      BufferCapabilities.FlipContents flipAction)
 447     {
 448         long window = peer.getContentWindow();
 449         int swapAction = getSwapAction(flipAction);
 450         swapBuffers(window, swapAction);
 451     }
 452 
 453     /**
 454      * Maps the given FlipContents constant to the associated XDBE swap
 455      * action constant.
 456      */
 457     private static int getSwapAction(
 458         BufferCapabilities.FlipContents flipAction) {
 459         if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {
 460             return 0x01;
 461         } else if (flipAction == BufferCapabilities.FlipContents.PRIOR) {
 462             return 0x02;
 463         } else if (flipAction == BufferCapabilities.FlipContents.COPIED) {
 464             return 0x03;
 465         } else {
 466             return 0x00; // UNDEFINED
 467         }
 468     }
 469 
 470     @Override
 471     public boolean isTranslucencyCapable() {
 472         return isTranslucencyCapable(getAData());
 473     }
 474 
 475     private native boolean isTranslucencyCapable(long x11ConfigData);
 476 }