aboutsummaryrefslogtreecommitdiffstats
path: root/gl4java/drawable/Win32SunJDK13GLDrawableFactory.java
blob: 8953ceb97e63b5ca0b65d483f2c8df376ae741f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package gl4java.drawable;

import java.awt.*;
import java.lang.reflect.*;
import java.util.*;
import sun.awt.*;
import gl4java.*;
import java.security.*;

public class Win32SunJDK13GLDrawableFactory 
	extends SunJDK13GLDrawableFactory 
{
    // Use reflection to get access to internal methods in sun.awt.*
    private static Method getMaxConfigsMethod;
    private static Method getDefaultPixIDMethod;

    static {
        try {
	   getMaxConfigsMethod = (Method)
	     AccessController.doPrivileged(new PrivilegedAction() {
	      public Object run() 
	      {
                try {
			return
			sun.awt.Win32GraphicsDevice.class.
			    getDeclaredMethod("getMaxConfigs",
					      new Class[] { Integer.TYPE });
		} catch (Exception e) {
		    e.printStackTrace();
		    throw new InternalError(e.toString());
		}
	      }});

            getMaxConfigsMethod.setAccessible(true);

            getDefaultPixIDMethod = (Method)
	     AccessController.doPrivileged(new PrivilegedAction() {
	      public Object run() 
	      {
                try {
			return
			sun.awt.Win32GraphicsDevice.class.
			    getDeclaredMethod("getDefaultPixID",
					      new Class[] { Integer.TYPE });
		} catch (Exception e) {
		    e.printStackTrace();
		    throw new InternalError(e.toString());
		}
	      }});
            getDefaultPixIDMethod.setAccessible(true);
        } catch (Exception e) {
            e.printStackTrace();
            throw new InternalError(e.toString());
        }
    }

    public Win32SunJDK13GLDrawableFactory() {
        // describeAllGraphicsConfigurations();
    }

    public GraphicsConfiguration
        getGraphicsConfiguration(GLCapabilities capabilities,
                                 GraphicsDevice device) {
        // On Windows, ChoosePixelFormat does not work as desired; it
        // shoehorns the given pixel format descriptor into what a
        // particular window can render. Instead we enumerate all possible
        // pixel formats for a given GraphicsDevice and try to find one
        // matching the given capabilities. To do this we provide
        // accessors which query the OpenGL-related properties of a
        // GraphicsConfiguration. FIXME: should implement better selection
        // algorithm; this one does not choose the most minimal.

        Win32GraphicsConfig[] configs =
            getAllGraphicsConfigurations((Win32GraphicsDevice) device);
    
        int maxDepth = 0;
	int maxColor = 0;
	int maxStencil = 0;
	int maxAccum = 0;

        for (int i = 0; i < configs.length; i++) {
	    Win32GraphicsConfig config = configs[i];

	    if( ! getConfigSupportsOpenGL(config) ) 
	    	continue;

	    if(getConfigDepthBits(config)>maxDepth) maxDepth=getConfigDepthBits(config);
	    if(getConfigColorBits(config)>maxColor) maxColor=getConfigColorBits(config); 
	    if(getConfigStencilBits(config)>maxStencil) maxStencil=getConfigStencilBits(config);
	    if(getConfigAccumBits(config)>maxAccum) maxAccum=getConfigAccumBits(config);
        }

	if(maxDepth>24) maxDepth=24;

	/** fall down to overall's maximum **/
	if(capabilities.getDepthBits()>maxDepth) capabilities.setDepthBits(maxDepth);
	if(capabilities.getStencilBits()>maxStencil) capabilities.setStencilBits(maxStencil);

	if(getCapsColorBits(capabilities)>maxColor)
	{ capabilities.setRedBits(maxColor/3); 
	  capabilities.setGreenBits(maxColor/3+maxColor%3); 
	  capabilities.setBlueBits(maxColor/3); 
	}
		
	if(getCapsAccumBits(capabilities)>maxAccum)
	{ capabilities.setAccumRedBits(maxAccum/3); 
	  capabilities.setAccumGreenBits(maxAccum/3+maxAccum%3); 
	  capabilities.setAccumBlueBits(maxAccum/3); 
	}
		
        Win32GraphicsConfig config = configsSupportsCapabilities(configs, capabilities, maxDepth);
	if(config!=null) return config;

	/* lets .. fall down .. individual .. */

	/* general normals .. */
	boolean tryit = false;
	if(capabilities.getDepthBits()>24) { capabilities.setDepthBits(24); tryit=true; }
	else if(capabilities.getDepthBits()>16) { capabilities.setDepthBits(16); tryit=true; }
	if(getCapsColorBits(capabilities)>24) 
	{ capabilities.setRedBits(8); capabilities.setGreenBits(8); capabilities.setBlueBits(8); tryit=true; }
	if(capabilities.getAlphaBits()>8) { capabilities.setAlphaBits(8); tryit=true; }
        if(tryit) config = configsSupportsCapabilities(configs, capabilities, maxDepth);
	if(config!=null) return config; tryit=false;

	/* no stereo .. */
	if(capabilities.getStereo()) { capabilities.setStereo(false); tryit=true; }
        if(tryit) config = configsSupportsCapabilities(configs, capabilities, maxDepth);
	if(config!=null) return config; tryit=false;

	/* stencil<=16 .. */
	if(capabilities.getStencilBits()>16) { capabilities.setStencilBits(16); tryit=true; }
        if(tryit) config = configsSupportsCapabilities(configs, capabilities, maxDepth);
	if(config!=null) return config; tryit=false;

	/* accum rgb each <=16.. */
	if(getCapsAccumBits(capabilities)>48) 
	{ capabilities.setAccumRedBits(16); capabilities.setAccumGreenBits(16); capabilities.setAccumBlueBits(16); 
	  tryit=true; }
        if(tryit) config = configsSupportsCapabilities(configs, capabilities, maxDepth);
	if(config!=null) return config; tryit=false;

	/* stencil<=8 .. */
	if(capabilities.getStencilBits()>8) { capabilities.setStencilBits(8); tryit=true; }
        if(tryit) config = configsSupportsCapabilities(configs, capabilities, maxDepth);
	if(config!=null) return config; tryit=false;

	/* accum rgb each <=8.. */
	if(getCapsAccumBits(capabilities)>24) 
	{ capabilities.setAccumRedBits(8); capabilities.setAccumGreenBits(8); capabilities.setAccumBlueBits(8); 
	  tryit=true; }
        if(tryit) config = configsSupportsCapabilities(configs, capabilities, maxDepth);
	if(config!=null) return config; tryit=false;

	/* stencil=0 .. */
	if(capabilities.getStencilBits()>0) { capabilities.setStencilBits(0); tryit=true; }
        if(tryit) config = configsSupportsCapabilities(configs, capabilities, maxDepth);
	if(config!=null) return config; tryit=false;

	/* alpha=0 */
	if(capabilities.getAlphaBits()>0) { capabilities.setAlphaBits(0); tryit=true; }
        if(tryit) config = configsSupportsCapabilities(configs, capabilities, maxDepth);
	if(config!=null) return config; tryit=false;
    
        return null;
    }

    //----------------------------------------------------------------------
    // Internals only below this point
    //

    private Win32GraphicsConfig configsSupportsCapabilities(Win32GraphicsConfig[] configs, 
    							    GLCapabilities glCaps,
							    int maxDepth)
    {
        if(GLContext.gljNativeDebug)
	{
	    System.out.println("---------------");
	    System.out.println("---------------");
	    System.out.println("---------------");
	    System.out.println("->check caps: "+glCaps);
	    System.out.println("---------------");
	}
        for (int i = 0; i < configs.length; i++) {
            if (configSupportsCapabilities(configs[i], glCaps, maxDepth)) {
		glCaps.setNativeVisualID( (long) configs[i].getVisual() );
                return configs[i];
            }
        }
	return null;
    }

    private Win32GraphicsConfig[]
        getAllGraphicsConfigurations(Win32GraphicsDevice device)
    {
        try {
            int max =
                ((Integer) getMaxConfigsMethod.invoke(device,
                                                      new Object[] {
                                                          new Integer(device.getScreen())
                                                              })).intValue();
            int defaultPixID =
                ((Integer) getDefaultPixIDMethod.invoke(device,
                                                        new Object[] {
                                                            new Integer(device.getScreen())
                                                                })).intValue();
            java.util.List l = new ArrayList(max);
            if (defaultPixID == 0) {
                // From Win32GraphicsDevice: workaround for failing GDI calls
                l.add(Win32GraphicsConfig.getConfig(device, defaultPixID));
            } else {
                for (int i = 1; i <= max; i++) {
                    l.add(Win32GraphicsConfig.getConfig(device, i));
                }
            }
            Win32GraphicsConfig[] configs = new Win32GraphicsConfig[l.size()];
            l.toArray(configs);
            return configs;
        } catch (Exception e) {
            e.printStackTrace();
            throw new InternalError(e.toString());
        }
    }

    private boolean configSupportsCapabilities(Win32GraphicsConfig config,
                                               GLCapabilities caps, int maxDepth)
    {
        boolean res = (   getConfigSupportsOpenGL(config)
                   && getConfigDoubleBuffered(config) == caps.getDoubleBuffered()
                   && caps.getTrueColor()      == getConfigTrueColor(config)
                   && caps.getDepthBits()      <= getConfigDepthBits(config) 
                   && maxDepth                 >= getConfigDepthBits(config) 
                   && caps.getStencilBits()    <= getConfigStencilBits(config)
		   && (caps.getStereo()        ?  getConfigStereo(config):true) 
                   && getCapsColorBits(caps)   <= getConfigColorBits(config)
                   /* && caps.getAlphaBits()      <= getConfigAlphaBits(config) N.A. */
                   && getCapsAccumBits(caps)   <= getConfigAccumBits(config));

        if(GLContext.gljNativeDebug)
	{
	    System.out.println("->against config: ");
	    describeGraphicsConfiguration(config);
	    System.out.println("---------------");
	    System.out.println("result: "+res);
	    System.out.println("---------------");
        }
	return res;
    }

    private static int getCapsColorBits(GLCapabilities caps) {
        return caps.getRedBits() + caps.getGreenBits() + caps.getBlueBits();
    }

    private static int getCapsAccumBits(GLCapabilities caps) {
        return caps.getAccumRedBits() + caps.getAccumGreenBits() + caps.getAccumBlueBits();
    }

    private static boolean getConfigSupportsOpenGL(Win32GraphicsConfig config) {
        return getVisualSupportsOpenGL(getScreen(config), config.getVisual());
    }

    private static boolean getConfigDoubleBuffered(Win32GraphicsConfig config) {
        return getVisualDoubleBuffered(getScreen(config), config.getVisual());
    }

    private static boolean getConfigTrueColor(Win32GraphicsConfig config) {
        return getVisualTrueColor(getScreen(config), config.getVisual());
    }

    private static boolean getConfigStereo(Win32GraphicsConfig config) {
        return getVisualStereo(getScreen(config), config.getVisual());
    }

    private static int getConfigDepthBits(Win32GraphicsConfig config) {
        return getVisualDepthBits(getScreen(config), config.getVisual());
    }

    private static int getConfigStencilBits(Win32GraphicsConfig config) {
        return getVisualStencilBits(getScreen(config), config.getVisual());
    }

    private static int getConfigColorShiftBits(Win32GraphicsConfig config) {
        return getVisualColorShiftBits(getScreen(config), config.getVisual());
    }

    private static int getConfigColorBits(Win32GraphicsConfig config) {
        return getVisualColorBits(getScreen(config), config.getVisual());
    }

    private static int getConfigAlphaBits(Win32GraphicsConfig config) {
        return getVisualAlphaBits(getScreen(config), config.getVisual());
    }

    private static int getConfigAccumBits(Win32GraphicsConfig config) {
        return getVisualAccumBits(getScreen(config), config.getVisual());
    }

    private static int getScreen(Win32GraphicsConfig config) {
        return ((Win32GraphicsDevice) config.getDevice()).getScreen();
    }

    // Native support routines that are not in GraphicsConfiguration or
    // Win32GraphicsConfig. These are only necessary on Windows because
    // X11's glXChooseVisual can implement getGraphicsConfiguration
    // directly.
    private static native boolean getVisualSupportsOpenGL (int screen, int pixelFormatIdx);
    private static native boolean getVisualDoubleBuffered (int screen, int pixelFormatIdx);
    private static native boolean getVisualTrueColor      (int screen, int pixelFormatIdx);
    private static native boolean getVisualStereo         (int screen, int pixelFormatIdx);
    private static native int     getVisualDepthBits      (int screen, int pixelFormatIdx);
    private static native int     getVisualStencilBits    (int screen, int pixelFormatIdx);
    private static native int     getVisualColorShiftBits (int screen, int pixelFormatIdx);
    private static native int     getVisualColorBits      (int screen, int pixelFormatIdx);
    private static native int     getVisualAlphaBits      (int screen, int pixelFormatIdx);
    private static native int     getVisualAccumBits      (int screen, int pixelFormatIdx);

  // Debugging only
    private void describeAllGraphicsConfigurations() {
        Win32GraphicsConfig[] configs =
            getAllGraphicsConfigurations(
                (Win32GraphicsDevice)
                GraphicsEnvironment.getLocalGraphicsEnvironment().
                    getDefaultScreenDevice()
            );
        System.err.println(configs.length + " graphics configurations found");
        for (int i = 0; i < configs.length; i++) {
            System.err.println(i + ".");
            Win32GraphicsConfig config = configs[i];
	    describeGraphicsConfiguration(config);
            System.err.println();
        }
    }

    private void describeGraphicsConfiguration(Win32GraphicsConfig config) 
    {
            boolean supportsOpenGL = getConfigSupportsOpenGL(config);
            System.err.println(" SupportsOpenGL: " + supportsOpenGL);
            if (supportsOpenGL) {
                System.err.print(" DoubleBuffered: " + getConfigDoubleBuffered(config));
                System.err.print(" TrueColor: "      + getConfigTrueColor(config));
                System.err.println(" Stereo: "         + getConfigStereo(config));
                System.err.print(" DepthBits: "      + getConfigDepthBits(config));
                System.err.println(" StencilBits: "    + getConfigStencilBits(config));
                System.err.print(" ColorBits: "      + getConfigColorBits(config));
                System.err.print(" AlphaBits: "      + getConfigAlphaBits(config));
                System.err.println(" AccumBits: "      + getConfigAccumBits(config));
            }
    }
}