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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
/*
* Copyright 1998-2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*
*/
package javax.media.j3d;
import java.awt.GraphicsConfigTemplate;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
/**
* This class is used to obtain a valid GraphicsConfiguration that can be used by Java 3D.
* A user instantiates one of these objects and then sets all
* non-default attributes as desired. The getBestConfiguration()
* method in the GraphicsDevice class is then called with this
* GraphicsConfigTemplate and the "best" GraphicsConfiguration is returned. The "best"
* GraphicsConfiguration means that this GraphicsConfiguration is supported and it
* meets or exceeds what was requested in the GraphicsConfigTemplate.
* Null is returned if no such "best" GraphicsConfiguration is found.
*
* @see GraphicsConfigTemplate
* @see GraphicsDevice
* @see GraphicsConfiguration
*/
public class GraphicsConfigTemplate3D extends GraphicsConfigTemplate {
int depthSize;
int doubleBuffer;
int blueSize;
int greenSize;
int redSize;
int sceneAntialiasing;
int stereo;
int stencilSize;
// Temporary variables use for passing argument to/from Request Renderer
Object testCfg;
static Object globalLock = new Object();
static Object monitorLock = new Object();
static volatile boolean threadWaiting = false;
/**
* Constructs a GraphicsConfigTemplate3D object with default parameters.
* The default values are as follows:
* <ul>
* depthSize : 16<br>
* doubleBuffer : REQUIRED<br>
* sceneAntialiasing : UNNECESSARY<br>
* stereo : UNNECESSARY<br>
* redSize : 2<br>
* greenSize : 2<br>
* blueSize : 2<br>
* stencilSize : 0<br>
* </ul>
*/
public GraphicsConfigTemplate3D() {
doubleBuffer = REQUIRED;
stereo = UNNECESSARY;
depthSize = 16;
stencilSize = 0;
redSize = greenSize = blueSize = 2;
sceneAntialiasing = UNNECESSARY;
}
/**
* Sets the double buffering requirement. It should be
* GraphicsConfigTemplate.REQUIRED, GraphicsConfigTemplate.PREFERRED,
* or GraphicsConfigTemplate.UNNECESSARY.
* If an invalid value is passed in, it is ignored.
* If the value of double buffering is
* GraphicsConfigTemplate.REQUIRED, and no GraphicsConfiguration is found
* that meets this requirement, null will be returned in getBestConfiguration().
* @param value the value to set this field to
*/
public void setDoubleBuffer(int value) {
if (value < REQUIRED && value > UNNECESSARY)
return;
doubleBuffer = value;
}
/**
* Retrieves the double buffering value.
* @return the current value of the doubleBuffer attribute
*/
public int getDoubleBuffer() {
return doubleBuffer;
}
/**
* Sets the stereo requirement. It should be
* GraphicsConfigTemplate.REQUIRED, GraphicsConfigTemplate.PREFERRED,
* or GraphicsConfigTemplate.UNNECESSARY. If an invalid value
* is passed in, it is ignored. If the value of stereo requirement is
* GraphicsConfigTemplate.REQUIRED, and no GraphicsConfiguration is found
* that meets this requirement, null will be returned in getBestConfiguration().
* @param value the value to set this field to
*/
public void setStereo(int value) {
if (value < REQUIRED && value > UNNECESSARY)
return;
stereo = value;
}
/**
* Retrieves the stereo value.
* @return the current value of the stereo attribute.
*/
public int getStereo() {
return stereo;
}
/**
* Sets the scene antialiasing requirement. It should be
* GraphicsConfigTemplate.REQUIRED, GraphicsConfigTemplate.PREFERRED,
* or GraphicsConfigTemplate.UNNECESSARY. If an invalid value
* is passed in, it is ignored. If the value of scene antialiasing is
* GraphicsConfigTemplate.REQUIRED, and no GraphicsConfiguration is found
* that meets this requirement, null will be returned in getBestConfiguration().
* @param value the value to set this field to
*/
public void setSceneAntialiasing(int value) {
if (value < REQUIRED && value > UNNECESSARY)
return;
sceneAntialiasing = value;
}
/**
* Retrieves the scene antialiasing value.
* @return the current value of the scene antialiasing attribute.
*/
public int getSceneAntialiasing() {
return sceneAntialiasing;
}
/**
* Sets the depth buffer size requirement. This is the minimum requirement.
* If no GraphicsConfiguration is found that meets or
* exceeds this minimum requirement, null will be returned in
* getBestConfiguration().
* @param value the value to set this field to
*/
public void setDepthSize(int value) {
if (value < 0)
return;
depthSize = value;
}
/**
* Retrieves the size of the depth buffer.
* @return the current value of the depthSize attribute
*/
public int getDepthSize() {
return depthSize;
}
/**
* Sets the stencil buffer size requirement.
* This is the minimum requirement.
* If no GraphicsConfiguration is found that meets or
* exceeds this minimum requirement, null will be returned in
* getBestConfiguration().
*
* @param value the value to set this field to
*
* @since Java 3D 1.4
*/
public void setStencilSize(int value) {
if (value < 0)
return;
stencilSize = value;
}
/**
* Retrieves the size of the stencil buffer.
*
* @return the current value of the stencilSize attribute
*
* @since Java 3D 1.4
*/
public int getStencilSize() {
return stencilSize;
}
/**
* Sets the number of red bits required. This is the minimum requirement.
* If no GraphicsConfiguration is found that meets or
* exceeds this minimum requirement, null will be returned in
* getBestConfiguration().
* @param value the value to set this field to
*/
public void setRedSize(int value) {
if (value < 0)
return;
redSize = value;
}
/**
* Retrieves the number of red bits requested by this template.
* @return the current value of the redSize attribute.
*/
public int getRedSize() {
return redSize;
}
/**
* Sets the number of green bits required. This is the minimum requirement.
* If no GraphicsConfiguration is found that meets or
* exceeds this minimum requirement, null will be returned in
* getBestConfiguration().
* @param value the value to set this field to
*/
public void setGreenSize(int value) {
if (value < 0)
return;
greenSize = value;
}
/**
* Retrieves the number of green bits requested by this template.
* @return the current value of the greenSize attribute.
*/
public int getGreenSize() {
return greenSize;
}
/**
* Sets the number of blue bits required. This is the minimum requirement.
* If no GraphicsConfiguration is found that meets or
* exceeds this minimum requirement, null will be returned in
* getBestConfiguration().
* @param value the value to set this field to
*/
public void setBlueSize(int value) {
if (value < 0)
return;
blueSize = value;
}
/**
* Retrieves the number of blue bits requested by this template.
* @return the current value of the blueSize attribute.
*/
public int getBlueSize() {
return blueSize;
}
/**
* Implement the abstract function of getBestConfiguration() in GraphicsConfigTemplate.
* Usually this function is not directly called by the user. It is
* implicitly called by getBestConfiguration() in GraphicsDevice.
* The method getBestConfiguration() in GraphicsDevice will return whatever this function returns.
* This function will return the "best" GraphicsConfiguration. The "best" GraphicsConfiguration
* means that this GraphicsConfiguration is supported and it meets or exceeds what was requested in the
* GraphicsConfigTemplate. If no such "best" GraphicsConfiguration is found, null is returned.
* @param gc the array of GraphicsConfigurations to choose from
*
* @return the best GraphicsConfiguration
*
* @see GraphicsDevice
*/
@Override
public GraphicsConfiguration
getBestConfiguration(GraphicsConfiguration[] gc) {
if ((gc == null) || (gc.length == 0) || (gc[0] == null)) {
return null;
}
synchronized (globalLock) {
testCfg = gc;
// It is possible that the followign postRequest will
// cause request renderer run immediately before
// runMonitor(WAIT). So we need to set
// threadWaiting to true.
threadWaiting = true;
// Prevent deadlock if invoke from Behavior callback since
// this thread has to wait Renderer thread to finish but
// MC can only handle postRequest and put it in Renderer
// queue when free.
if (Thread.currentThread() instanceof BehaviorScheduler) {
VirtualUniverse.mc.sendRenderMessage(gc[0], this,
MasterControl.GETBESTCONFIG);
} else {
VirtualUniverse.mc.postRequest(MasterControl.GETBESTCONFIG, this);
}
runMonitor(J3dThread.WAIT);
return (GraphicsConfiguration) testCfg;
}
}
/**
* Returns a boolean indicating whether or not the given
* GraphicsConfiguration can be used to create a drawing
* surface that can be rendered to.
*
* @param gc the GraphicsConfiguration object to test
*
* @return <code>true</code> if this GraphicsConfiguration object
* can be used to create surfaces that can be rendered to,
* <code>false</code> if the GraphicsConfiguration can not be used
* to create a drawing surface usable by this API.
*/
@Override
public boolean isGraphicsConfigSupported(GraphicsConfiguration gc) {
if (gc == null) {
return false;
}
synchronized (globalLock) {
testCfg = gc;
threadWaiting = true;
if (Thread.currentThread() instanceof BehaviorScheduler) {
VirtualUniverse.mc.sendRenderMessage(gc, this, MasterControl.ISCONFIGSUPPORT);
} else {
VirtualUniverse.mc.postRequest(MasterControl.ISCONFIGSUPPORT, this);
}
runMonitor(J3dThread.WAIT);
return ((Boolean) testCfg).booleanValue();
}
}
/**
* Set the stereo/doubleBuffer/sceneAntialiasingAccum
* and hasSceneAntialiasingMultiSamples flags in Canvas3D
*/
static void getGraphicsConfigFeatures(Canvas3D c) {
synchronized (globalLock) {
threadWaiting = true;
if (Thread.currentThread() instanceof BehaviorScheduler) {
VirtualUniverse.mc.sendRenderMessage(c.graphicsConfiguration, c,
MasterControl.SET_GRAPHICSCONFIG_FEATURES);
} else {
VirtualUniverse.mc.postRequest(MasterControl.SET_GRAPHICSCONFIG_FEATURES, c);
}
runMonitor(J3dThread.WAIT);
}
}
/**
* Set the queryProperties() map in Canvas3D
*/
static void setQueryProps(Canvas3D c) {
synchronized (globalLock) {
threadWaiting = true;
if (Thread.currentThread() instanceof BehaviorScheduler) {
VirtualUniverse.mc.sendRenderMessage(c.graphicsConfiguration, c,
MasterControl.SET_QUERYPROPERTIES);
} else {
VirtualUniverse.mc.postRequest(MasterControl.SET_QUERYPROPERTIES, c);
}
runMonitor(J3dThread.WAIT);
}
}
static void runMonitor(int action) {
// user thread will locked the globalLock when Renderer
// thread invoke this function so we can't use
// the same lock.
synchronized (monitorLock) {
switch (action) {
case J3dThread.WAIT:
// Issue 279 - loop until ready
while (threadWaiting) {
try {
monitorLock.wait();
} catch (InterruptedException e) {
System.err.println(e);
}
}
break;
case J3dThread.NOTIFY:
monitorLock.notify();
threadWaiting = false;
break;
}
}
}
// Return a string representing the value, one of:
// REQUIRED, PREFERRED, or UNNECESSARY
private static final String enumStr(int val) {
switch (val) {
case REQUIRED:
return "REQUIRED";
case PREFERRED:
return "PREFERRED";
case UNNECESSARY:
return "UNNECESSARY";
}
return "UNDEFINED";
}
/**
* Returns a string representation of this object.
* @return a string representation of this object.
*/
@Override
public String toString() {
return
"redSize : " + redSize + ", " +
"greenSize : " + greenSize + ", " +
"blueSize : " + blueSize + ", " +
"depthSize : " + depthSize + ", " +
"doubleBuffer : " + enumStr(doubleBuffer) + ", " +
"sceneAntialiasing : " + enumStr(sceneAntialiasing) + ", " +
"stereo : " + enumStr(stereo);
}
}
|