1 /*
2  * Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved.
3  * Copyright (c) 2010 JogAmp Community. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * - Redistribution of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * - Redistribution in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
22  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
23  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
24  * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
25  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
27  * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
28  * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
29  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
30  * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
31  * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32  */
33 
34 package com.jogamp.nativewindow.x11;
35 
36 import com.jogamp.common.util.Bitfield;
37 import com.jogamp.nativewindow.CapabilitiesImmutable;
38 
39 import com.jogamp.nativewindow.MutableGraphicsConfiguration;
40 
41 import jogamp.nativewindow.x11.X11Capabilities;
42 import jogamp.nativewindow.x11.X11Lib;
43 import jogamp.nativewindow.x11.XRenderDirectFormat;
44 import jogamp.nativewindow.x11.XRenderPictFormat;
45 import jogamp.nativewindow.x11.XVisualInfo;
46 
47 /** Encapsulates a graphics configuration, or OpenGL pixel format, on
48     X11 platforms. Objects of this type are returned from {@link
49     com.jogamp.nativewindow.GraphicsConfigurationFactory#chooseGraphicsConfiguration
50     GraphicsConfigurationFactory.chooseGraphicsConfiguration()} on X11
51     platforms when toolkits other than the AWT are being used.  */
52 
53 public class X11GraphicsConfiguration extends MutableGraphicsConfiguration implements Cloneable {
54     private XVisualInfo info;
55 
56     // FBConfig
57 
XVisual2XRenderMask(final long dpy, final long visual)58     protected static XRenderDirectFormat XVisual2XRenderMask(final long dpy, final long visual) {
59         final XRenderPictFormat xRenderPictFormat = XRenderPictFormat.create();
60         return XVisual2XRenderMask(dpy, visual, xRenderPictFormat);
61     }
XVisual2XRenderMask(final long dpy, final long visual, final XRenderPictFormat dest)62     protected static XRenderDirectFormat XVisual2XRenderMask(final long dpy, final long visual, final XRenderPictFormat dest) {
63         if( !X11Lib.XRenderFindVisualFormat(dpy, visual, dest) ) {
64             return null;
65         } else {
66             return dest.getDirect();
67         }
68     }
69 
XVisualInfo2X11Capabilities(final X11GraphicsDevice device, final XVisualInfo info)70     public static X11Capabilities XVisualInfo2X11Capabilities(final X11GraphicsDevice device, final XVisualInfo info) {
71         final long display = device.getHandle();
72         final X11Capabilities res = new X11Capabilities(info);
73 
74         final XRenderDirectFormat xrmask = ( null != info ) ? XVisual2XRenderMask( display, info.getVisual() ) : null ;
75         final int alphaMask = ( null != xrmask ) ? xrmask.getAlphaMask() : 0;
76         if( 0 < alphaMask ) {
77             res.setBackgroundOpaque(false);
78             res.setTransparentRedValue(xrmask.getRedMask());
79             res.setTransparentGreenValue(xrmask.getGreenMask());
80             res.setTransparentBlueValue(xrmask.getBlueMask());
81             res.setTransparentAlphaValue(alphaMask);
82         } else {
83             res.setBackgroundOpaque(true);
84         }
85         // ALPHA shall be set at last - due to it's auto setting by the above (!opaque / samples)
86         res.setRedBits       (Bitfield.Util.bitCount((int)info.getRed_mask()));
87         res.setGreenBits     (Bitfield.Util.bitCount((int)info.getGreen_mask()));
88         res.setBlueBits      (Bitfield.Util.bitCount((int)info.getBlue_mask()));
89         res.setAlphaBits     (Bitfield.Util.bitCount(alphaMask));
90 
91         return res;
92     }
93 
X11GraphicsConfiguration(final X11GraphicsScreen screen, final CapabilitiesImmutable capsChosen, final CapabilitiesImmutable capsRequested, final XVisualInfo info)94     public X11GraphicsConfiguration(final X11GraphicsScreen screen,
95                                     final CapabilitiesImmutable capsChosen, final CapabilitiesImmutable capsRequested,
96                                     final XVisualInfo info) {
97         super(screen, capsChosen, capsRequested);
98         this.info = info;
99     }
100 
101     @Override
clone()102     public Object clone() {
103       return super.clone();
104     }
105 
getXVisualInfo()106     final public XVisualInfo getXVisualInfo() {
107         return info;
108     }
109 
setXVisualInfo(final XVisualInfo info)110     final protected void setXVisualInfo(final XVisualInfo info) {
111         this.info = info;
112     }
113 
getXVisualID()114     final public int getXVisualID() {
115         return (null!=info)?(int)info.getVisualid():0;
116     }
117 
118     @Override
toString()119     public String toString() {
120         return getClass().getSimpleName()+"["+getScreen()+", visualID 0x" + Long.toHexString(getXVisualID()) +
121                                        ",\n\tchosen    " + capabilitiesChosen+
122                                        ",\n\trequested " + capabilitiesRequested+
123                                        "]";
124     }
125 }
126