JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
DefaultCapabilitiesChooser.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003 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 * You acknowledge that this software is not designed or intended for use
34 * in the design, construction, operation or maintenance of any nuclear
35 * facility.
36 *
37 * Sun gratefully acknowledges that this software was originally authored
38 * and developed by Kenneth Bradley Russell and Christopher John Kline.
39 */
40
41package com.jogamp.nativewindow;
42
43import java.util.List;
44
45import com.jogamp.common.util.PropertyAccess;
46
47import jogamp.nativewindow.Debug;
48
49/** <P> The default implementation of the {@link
50 CapabilitiesChooser} interface, which provides consistent visual
51 selection behavior across platforms. The precise algorithm is
52 deliberately left loosely specified. Some properties are: </P>
53
54 <LI> Attempts to match as closely as possible the given
55 Capabilities, but will select one with fewer capabilities (i.e.,
56 lower color depth) if necessary.
57
58 <LI> If there is no exact match, prefers a more-capable visual to
59 a less-capable one.
60
61 <LI> If there is more than one exact match, chooses an arbitrary
62 one.
63
64 <LI> If a valid windowSystemRecommendedChoice parameter is
65 supplied, chooses that instead of using the cross-platform code.
66
67 </UL>
68*/
69
71 private static final boolean DEBUG;
72
73 static {
74 Debug.initSingleton();
75 DEBUG = PropertyAccess.isPropertyDefined("nativewindow.debug.CapabilitiesChooser", true);
76 }
77
78 private final static int NO_SCORE = -9999999;
79 private final static int COLOR_MISMATCH_PENALTY_SCALE = 36;
80
81 @Override
82 public int chooseCapabilities(final CapabilitiesImmutable desired,
83 final List<? extends CapabilitiesImmutable> available,
84 final int windowSystemRecommendedChoice) {
85 if (DEBUG) {
86 System.err.println("Desired: " + desired);
87 for (int i = 0; i < available.size(); i++) {
88 System.err.println("Available " + i + ": " + available.get(i));
89 }
90 System.err.println("Window system's recommended choice: " + windowSystemRecommendedChoice);
91 }
92 final int availnum = available.size();
93
94 if (windowSystemRecommendedChoice >= 0 &&
95 windowSystemRecommendedChoice < availnum &&
96 null != available.get(windowSystemRecommendedChoice)) {
97 if (DEBUG) {
98 System.err.println("Choosing window system's recommended choice of " + windowSystemRecommendedChoice);
99 System.err.println(available.get(windowSystemRecommendedChoice));
100 }
101 return windowSystemRecommendedChoice;
102 }
103
104 // Create score array
105 final int[] scores = new int[availnum];
106 for (int i = 0; i < availnum; i++) {
107 scores[i] = NO_SCORE;
108 }
109 // Compute score for each
110 for (int i = 0; i < availnum; i++) {
111 final CapabilitiesImmutable cur = available.get(i);
112 if (cur == null) {
113 continue;
114 }
115 if (desired.isOnscreen() && !cur.isOnscreen()) {
116 continue; // requested onscreen, but n/a
117 }
118
119 int score = 0;
120 // Compute difference in color depth
121 score += (COLOR_MISMATCH_PENALTY_SCALE *
122 ((cur.getRedBits() + cur.getGreenBits() + cur.getBlueBits() + cur.getAlphaBits()) -
123 (desired.getRedBits() + desired.getGreenBits() + desired.getBlueBits() + desired.getAlphaBits())));
124 scores[i] = score;
125 }
126
127 if (DEBUG) {
128 System.err.print("Scores: [");
129 for (int i = 0; i < availnum; i++) {
130 if (i > 0) {
131 System.err.print(",");
132 }
133 System.err.print(" " + scores[i]);
134 }
135 System.err.println(" ]");
136 }
137
138 // Ready to select. Choose score closest to 0.
139 int scoreClosestToZero = NO_SCORE;
140 int chosenIndex = -1;
141 for (int i = 0; i < availnum; i++) {
142 final int score = scores[i];
143 if (score == NO_SCORE) {
144 continue;
145 }
146 // Don't substitute a positive score for a smaller negative score
147 if ((scoreClosestToZero == NO_SCORE) ||
148 (Math.abs(score) < Math.abs(scoreClosestToZero) &&
149 ((sign(scoreClosestToZero) < 0) || (sign(score) > 0)))) {
150 scoreClosestToZero = score;
151 chosenIndex = i;
152 }
153 }
154 if (chosenIndex < 0) {
155 throw new NativeWindowException("Unable to select one of the provided Capabilities");
156 }
157 if (DEBUG) {
158 System.err.println("Chosen index: " + chosenIndex);
159 System.err.println("Chosen capabilities:");
160 System.err.println(available.get(chosenIndex));
161 }
162
163 return chosenIndex;
164 }
165
166 private static int sign(final int score) {
167 if (score < 0) {
168 return -1;
169 }
170 return 1;
171 }
172}
int chooseCapabilities(final CapabilitiesImmutable desired, final List<? extends CapabilitiesImmutable > available, final int windowSystemRecommendedChoice)
Chooses the index (0..available.length - 1) of the Capabilities most closely matching the desired one...
A generic exception for OpenGL errors used throughout the binding as a substitute for RuntimeExceptio...
Provides a mechanism by which applications can customize the window type selection for a given Capabi...
Specifies an immutable set of capabilities that a window's rendering context must support,...
int getAlphaBits()
Returns the number of bits for the color buffer's alpha component.
int getBlueBits()
Returns the number of bits for the color buffer's blue component.
int getRedBits()
Returns the number of bits for the color buffer's red component.
int getGreenBits()
Returns the number of bits for the color buffer's green component.
boolean isOnscreen()
Returns whether an on- or offscreen surface is requested, available or chosen.