-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathRenderer.java
More file actions
1809 lines (1551 loc) · 67.2 KB
/
Renderer.java
File metadata and controls
1809 lines (1551 loc) · 67.2 KB
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 1997-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.
*
*/
/*
* Portions of this code were derived from work done by the Blackdown
* group (www.blackdown.org), who did the initial Linux implementation
* of the Java 3D API.
*/
package javax.media.j3d;
import java.awt.GraphicsConfiguration;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.logging.Level;
class Renderer extends J3dThread {
// This action causes this thread to wait
static final int WAIT = 0;
// This action causes this thread to notify the view, and then wait.
static final int NOTIFY_AND_WAIT = 1;
// This action causes this thread to be notified
static final int NOTIFY = 2;
// The following are DecalGroup rendering states
static final int DECAL_NONE = 0;
static final int DECAL_1ST_CHILD = 1;
static final int DECAL_NTH_CHILD = 2;
// stuff for scene antialiasing
static final int NUM_ACCUMULATION_SAMPLES = 8;
static final float ACCUM_SAMPLES_X[] =
{ -0.54818f, 0.56438f, 0.39462f, -0.54498f,
-0.83790f, -0.39263f, 0.32254f, 0.84216f};
static final float ACCUM_SAMPLES_Y[] =
{ 0.55331f, -0.53495f, 0.41540f, -0.52829f,
0.82102f, -0.27383f, 0.09133f, -0.84399f};
static final float accumValue = 1.0f / NUM_ACCUMULATION_SAMPLES;
// The following are Render arguments
static final int RENDER = 0;
static final int SWAP = 1;
static final int REQUESTRENDER = 2;
static final int REQUESTCLEANUP = 3;
// Renderer Structure used for the messaging to the renderer
RendererStructure rendererStructure = new RendererStructure();
// vworldtoVpc matrix for background geometry
Transform3D bgVworldToVpc = new Transform3D();
private static int numInstances = 0;
private int instanceNum = -1;
// Local copy of sharedStereZBuffer flag
boolean sharedStereoZBuffer;
// This is the id for the underlying sharable graphics context
Context sharedCtx = null;
// since the sharedCtx id can be the same as the previous one,
// we need to keep a time stamp to differentiate the contexts with the
// same id
long sharedCtxTimeStamp = 0;
// display and drawable, used to free shared context
private Drawable sharedCtxDrawable = null;
/**
* This is the id of the current rendering context
*/
Context currentCtx = null;
/**
* This is the id of the current rendering drawable
*/
Drawable currentDrawable = null;
// an unique bit to identify this renderer
int rendererBit = 0;
// an unique number to identify this renderer : ( rendererBit = 1 << rendererId)
int rendererId = 0;
// List of renderMolecules that are dirty due to additions
// or removal of renderAtoms from their display list set
// of renderAtoms
ArrayList<RenderMolecule> dirtyRenderMoleculeList = new ArrayList<RenderMolecule>();
// List of individual dlists that need to be rebuilt
ArrayList<RenderAtomListInfo> dirtyRenderAtomList = new ArrayList<RenderAtomListInfo>();
// List of (Rm, rInfo) pair of individual dlists that need to be rebuilt
ArrayList<Object[]> dirtyDlistPerRinfoList = new ArrayList<Object[]>();
// Texture and display list that should be freed
ArrayList<Integer> textureIdResourceFreeList = new ArrayList<Integer>();
ArrayList<Integer> displayListResourceFreeList = new ArrayList<Integer>();
// Texture that should be reload
ArrayList<TextureRetained> textureReloadList = new ArrayList<TextureRetained>();
J3dMessage[] renderMessage;
// The screen for this Renderer. Note that this renderer may share
// by both on screen and off screen. When view unregister, we need
// to set both reference to null.
Screen3D onScreen;
Screen3D offScreen;
// full screen anti-aliasing projection matrices
Transform3D accumLeftProj = new Transform3D();
Transform3D accumRightProj = new Transform3D();
Transform3D accumInfLeftProj = new Transform3D();
Transform3D accumInfRightProj = new Transform3D();
// rendering messages
J3dMessage m[];
int nmesg = 0;
// List of contexts created
ArrayList<Context> listOfCtxs = new ArrayList<Context>();
// Parallel list of canvases
ArrayList<Canvas3D> listOfCanvases = new ArrayList<Canvas3D>();
boolean needToRebuildDisplayList = false;
// True when either one of dirtyRenderMoleculeList,
// dirtyDlistPerRinfoList, dirtyRenderAtomList size > 0
boolean dirtyDisplayList = false;
// Remember OGL context resources to free
// before context is destroy.
// It is used when sharedCtx = true;
ArrayList<TextureRetained> textureIDResourceTable = new ArrayList<TextureRetained>(5);
// Instrumentation of Java 3D renderer
private long lastSwapTime = System.nanoTime();
private synchronized int newInstanceNum() {
return (++numInstances);
}
@Override
int getInstanceNum() {
if (instanceNum == -1)
instanceNum = newInstanceNum();
return instanceNum;
}
/**
* Constructs a new Renderer
*/
Renderer(ThreadGroup t) {
super(t);
setName("J3D-Renderer-" + getInstanceNum());
type = J3dThread.RENDER_THREAD;
rendererId = VirtualUniverse.mc.getRendererId();
rendererBit = (1 << rendererId);
renderMessage = new J3dMessage[1];
}
/**
* The main loop for the renderer.
*/
@Override
void doWork(long referenceTime) {
RenderBin renderBin = null;
Canvas3D cv, canvas=null;
Object firstArg;
View view = null;
int stereo_mode;
int num_stereo_passes, num_accum_passes = 1;
int pass, apass, i, j;
boolean doAccum = false;
double accumDx = 0.0f, accumDy = 0.0f;
double accumDxFactor = 1.0f, accumDyFactor = 1.0f;
double accumLeftX = 0.0, accumLeftY = 0.0,
accumRightX = 0.0, accumRightY = 0.0,
accumInfLeftX = 0.0, accumInfLeftY = 0.0,
accumInfRightX = 0.0, accumInfRightY = 0.0;
int opArg;
Transform3D t3d = null;
opArg = ((Integer)args[0]).intValue();
try {
if (opArg == SWAP) {
Object [] swapArray = (Object[])args[2];
view = (View)args[3];
for (i=0; i<swapArray.length; i++) {
cv = (Canvas3D) swapArray[i];
if (!cv.isRunning) {
continue;
}
doneSwap: try {
if (!cv.validCanvas) {
continue;
}
if (cv.active && (cv.ctx != null) &&
(cv.view != null) && (cv.imageReady)) {
// don't swap double buffered AuoOffScreenCanvas3D/JCanvas3D
// manual offscreen rendering doesn't pass this code (opArg == SWAP)
if (cv.useDoubleBuffer && !cv.offScreen) {
synchronized (cv.drawingSurfaceObject) {
if (cv.validCtx) {
if (VirtualUniverse.mc.doDsiRenderLock) {
// Set doDsiLock flag for rendering based on system
// property, If we force DSI lock for swap
// buffer, we lose most of the parallelism that having
// multiple renderers gives us.
if (!cv.drawingSurfaceObject.renderLock()) {
break doneSwap;
}
cv.makeCtxCurrent();
cv.syncRender(cv.ctx, true);
cv.swapBuffers(cv.ctx, cv.drawable);
cv.drawingSurfaceObject.unLock();
} else {
cv.makeCtxCurrent();
cv.syncRender(cv.ctx, true);
cv.swapBuffers(cv.ctx, cv.drawable);
}
}
}
}
cv.view.inCanvasCallback = true;
try {
cv.postSwap();
} catch (RuntimeException e) {
System.err.println("Exception occurred during Canvas3D callback:");
e.printStackTrace();
} catch (Error e) {
// Issue 264 - catch Error so Renderer doesn't die
System.err.println("Error occurred during Canvas3D callback:");
e.printStackTrace();
}
// reset flag
cv.imageReady = false;
cv.view.inCanvasCallback = false;
// Clear canvasDirty bit ONLY when postSwap() success
if (MasterControl.isStatsLoggable(Level.INFO)) {
// Instrumentation of Java 3D renderer
long currSwapTime = System.nanoTime();
long deltaTime = currSwapTime - lastSwapTime;
lastSwapTime = currSwapTime;
VirtualUniverse.mc.recordTime(MasterControl.TimeType.TOTAL_FRAME, deltaTime);
}
// Set all dirty bits except environment set and lightbin
// they are only set dirty if the last used light bin or
// environment set values for this canvas change between
// one frame and other
if (!cv.ctxChanged) {
cv.canvasDirty = (0xffff & ~(Canvas3D.LIGHTBIN_DIRTY |
Canvas3D.LIGHTENABLES_DIRTY |
Canvas3D.AMBIENTLIGHT_DIRTY |
Canvas3D.MODELCLIP_DIRTY |
Canvas3D.VIEW_MATRIX_DIRTY |
Canvas3D.FOG_DIRTY));
// Force reload of transform next frame
cv.modelMatrix = null;
// Force the cached renderAtom to null
cv.ra = null;
} else {
cv.ctxChanged = false;
}
}
} catch (NullPointerException ne) {
// Ignore NPE
if (VirtualUniverse.mc.doDsiRenderLock) {
cv.drawingSurfaceObject.unLock();
}
} catch (RuntimeException ex) {
ex.printStackTrace();
if (VirtualUniverse.mc.doDsiRenderLock) {
cv.drawingSurfaceObject.unLock();
}
// Issue 260 : indicate fatal error and notify error listeners
cv.setFatalError();
RenderingError err =
new RenderingError(RenderingError.UNEXPECTED_RENDERING_ERROR,
J3dI18N.getString("Renderer0"));
err.setCanvas3D(cv);
err.setGraphicsDevice(cv.graphicsConfiguration.getDevice());
notifyErrorListeners(err);
}
cv.releaseCtx();
}
if (view != null) { // STOP_TIMER
// incElapsedFrames() is delay until MC:updateMirroObject
if (view.viewCache.getDoHeadTracking()) {
VirtualUniverse.mc.sendRunMessage(view,
J3dThread.RENDER_THREAD);
}
}
} else if (opArg == REQUESTCLEANUP) {
Integer mtype = (Integer) args[2];
if (mtype == MasterControl.REMOVEALLCTXS_CLEANUP) {
// from MasterControl when View is last views
removeAllCtxs();
} else if (mtype == MasterControl.FREECONTEXT_CLEANUP) {
// from MasterControl freeContext(View v)
cv = (Canvas3D) args[1];
removeCtx(cv, cv.drawable, cv.ctx,
true, true, false);
} else if (mtype == MasterControl.RESETCANVAS_CLEANUP) {
// from MasterControl RESET_CANVAS postRequest
cv = (Canvas3D) args[1];
if (cv.ctx != null) {
cv.makeCtxCurrent();
}
cv.freeContextResources(cv.screen.renderer, true, cv.ctx);
} else if (mtype == MasterControl.REMOVECTX_CLEANUP) {
// from Canvas3D removeCtx() postRequest
Object[] obj = (Object []) args[1];
Canvas3D c = (Canvas3D) obj[0];
removeCtx(c,
(Drawable) obj[2],
(Context) obj[3],
false, !c.offScreen,
false);
}
return;
} else { // RENDER || REQUESTRENDER
int renderType;
nmesg = 0;
int totalMessages = 0;
if (opArg == RENDER) {
m = renderMessage;
m[0] = new J3dMessage();
// Issue 131: Set appropriate message type
if (((Canvas3D)args[1]).offScreen) {
m[0].type = J3dMessage.RENDER_OFFSCREEN;
}
else {
m[0].type = J3dMessage.RENDER_RETAINED;
}
m[0].incRefcount();
m[0].args[0] = args[1];
totalMessages = 1;
} else { // REQUESTRENDER
m = rendererStructure.getMessages();
totalMessages = rendererStructure.getNumMessage();
if (totalMessages <= 0) {
return;
}
}
doneRender: while (nmesg < totalMessages) {
firstArg = m[nmesg].args[0];
if (firstArg == null) {
Object secondArg = m[nmesg].args[1];
if (secondArg instanceof Canvas3D) {
// message from Canvas3Ds to destroy Context
Integer reqType = (Integer) m[nmesg].args[2];
Canvas3D c = (Canvas3D) secondArg;
if (reqType == MasterControl.SET_GRAPHICSCONFIG_FEATURES) {
try {
if (c.offScreen) {
// NEW : offscreen supports double buffering
c.doubleBufferAvailable = c.hasDoubleBuffer(); // was : false
// offScreen canvas doesn't supports stereo
c.stereoAvailable = false;
} else {
c.doubleBufferAvailable = c.hasDoubleBuffer();
c.stereoAvailable = c.hasStereo();
}
// Setup stencil related variables.
c.actualStencilSize = c.getStencilSize();
boolean userOwnsStencil = c.requestedStencilSize > 0;
c.userStencilAvailable =
(userOwnsStencil && (c.actualStencilSize > 0));
c.systemStencilAvailable =
(!userOwnsStencil && (c.actualStencilSize > 0));
c.sceneAntialiasingMultiSamplesAvailable =
c.hasSceneAntialiasingMultisample();
if (c.sceneAntialiasingMultiSamplesAvailable) {
c.sceneAntialiasingAvailable = true;
} else {
c.sceneAntialiasingAvailable =
c.hasSceneAntialiasingAccum();
}
} catch (RuntimeException ex) {
ex.printStackTrace();
// Issue 260 : indicate fatal error and notify error listeners
c.setFatalError();
RenderingError err =
new RenderingError(RenderingError.GRAPHICS_CONFIG_ERROR,
J3dI18N.getString("Renderer1"));
err.setCanvas3D(c);
err.setGraphicsDevice(c.graphicsConfiguration.getDevice());
notifyErrorListeners(err);
}
GraphicsConfigTemplate3D.runMonitor(J3dThread.NOTIFY);
} else if (reqType == MasterControl.SET_QUERYPROPERTIES){
try {
c.createQueryContext();
} catch (RuntimeException ex) {
ex.printStackTrace();
// Issue 260 : indicate fatal error and notify error listeners
c.setFatalError();
RenderingError err =
new RenderingError(RenderingError.CONTEXT_CREATION_ERROR,
J3dI18N.getString("Renderer2"));
err.setCanvas3D(c);
err.setGraphicsDevice(c.graphicsConfiguration.getDevice());
notifyErrorListeners(err);
}
// currentCtx change after we create a new context
GraphicsConfigTemplate3D.runMonitor(J3dThread.NOTIFY);
currentCtx = null;
currentDrawable = null;
}
} else if (secondArg instanceof Integer) {
// Issue 121 - This was formerly used as a message from
// the now-nonexistant TextureRetained finalize() method
// to free the texture id
throw new AssertionError();
} else if (secondArg instanceof GeometryArrayRetained) {
// message from GeometryArrayRetained
// clearLive() to free D3D array
//((GeometryArrayRetained) secondArg).freeD3DArray(false);
} else if (secondArg instanceof GraphicsConfigTemplate3D) {
GraphicsConfigTemplate3D gct =
(GraphicsConfigTemplate3D) secondArg;
Integer reqType = (Integer) m[nmesg].args[2];
if (reqType == MasterControl.GETBESTCONFIG) {
GraphicsConfiguration gcfg = null;
GraphicsConfiguration [] gcList = (GraphicsConfiguration []) gct.testCfg;
try {
gcfg = Pipeline.getPipeline().getBestConfiguration(gct, gcList);
} catch (NullPointerException npe) {
npe.printStackTrace();
} catch (RuntimeException ex) {
ex.printStackTrace();
// Issue 260 : notify error listeners
RenderingError err =
new RenderingError(RenderingError.GRAPHICS_CONFIG_ERROR,
J3dI18N.getString("Renderer3"));
err.setGraphicsDevice(gcList[0].getDevice());
notifyErrorListeners(err);
}
gct.testCfg = gcfg;
} else if (reqType == MasterControl.ISCONFIGSUPPORT) {
boolean rval = false;
GraphicsConfiguration gc = (GraphicsConfiguration) gct.testCfg;
try {
if (Pipeline.getPipeline().isGraphicsConfigSupported(gct, gc)) {
rval = true;
}
} catch (NullPointerException npe) {
npe.printStackTrace();
} catch (RuntimeException ex) {
ex.printStackTrace();
// Issue 260 : notify error listeners
RenderingError err =
new RenderingError(RenderingError.GRAPHICS_CONFIG_ERROR,
J3dI18N.getString("Renderer4"));
err.setGraphicsDevice(gc.getDevice());
notifyErrorListeners(err);
}
gct.testCfg = Boolean.valueOf(rval);
}
GraphicsConfigTemplate3D.runMonitor(J3dThread.NOTIFY);
}
m[nmesg++].decRefcount();
continue;
}
canvas = (Canvas3D) firstArg;
renderType = m[nmesg].type;
if (renderType == J3dMessage.CREATE_OFFSCREENBUFFER) {
// Fix for issue 18.
// Fix for issue 20.
canvas.drawable = null;
try {
// Issue 396. Pass in a null ctx for 2 reasons :
// 1) We should not use ctx field directly without buffering in a msg.
// 2) canvas.ctx should be null.
canvas.drawable =
canvas.createOffScreenBuffer(null,
canvas.offScreenCanvasSize.width,
canvas.offScreenCanvasSize.height);
} catch (RuntimeException ex) {
ex.printStackTrace();
}
if (canvas.drawable == null) {
// Issue 260 : indicate fatal error and notify error listeners
canvas.setFatalError();
RenderingError err =
new RenderingError(RenderingError.OFF_SCREEN_BUFFER_ERROR,
J3dI18N.getString("Renderer5"));
err.setCanvas3D(canvas);
err.setGraphicsDevice(canvas.graphicsConfiguration.getDevice());
notifyErrorListeners(err);
}
canvas.offScreenBufferPending = false;
m[nmesg++].decRefcount();
continue;
}
else if (renderType == J3dMessage.DESTROY_CTX_AND_OFFSCREENBUFFER) {
Object[] obj = m[nmesg].args;
// Fix for issue 175: destroy ctx & off-screen buffer
// Fix for issue 340: get display, drawable & ctx from msg
removeCtx(canvas,
(Drawable) obj[2],
(Context) obj[3],
false, !canvas.offScreen, true);
canvas.offScreenBufferPending = false;
m[nmesg++].decRefcount();
continue;
} else if (renderType == J3dMessage.ALLOCATE_CANVASID) {
canvas.allocateCanvasId();
} else if (renderType == J3dMessage.FREE_CANVASID) {
canvas.freeCanvasId();
}
if ((canvas.view == null) || !canvas.firstPaintCalled) {
// This happen when the canvas just remove from the View
if (renderType == J3dMessage.RENDER_OFFSCREEN) {
canvas.offScreenRendering = false;
}
m[nmesg++].decRefcount();
continue;
}
if (!canvas.validCanvas &&
(renderType != J3dMessage.RENDER_OFFSCREEN)) {
m[nmesg++].decRefcount();
continue;
}
if (renderType == J3dMessage.RESIZE_CANVAS) {
// render the image again after resize
VirtualUniverse.mc.sendRunMessage(canvas.view, J3dThread.RENDER_THREAD);
m[nmesg++].decRefcount();
} else if (renderType == J3dMessage.TOGGLE_CANVAS) {
VirtualUniverse.mc.sendRunMessage(canvas.view, J3dThread.RENDER_THREAD);
m[nmesg++].decRefcount();
} else if (renderType == J3dMessage.RENDER_IMMEDIATE) {
int command = ((Integer)m[nmesg].args[1]).intValue();
//System.err.println("command= " + command);
if (canvas.isFatalError()) {
continue;
}
if (canvas.ctx == null) {
synchronized (VirtualUniverse.mc.contextCreationLock) {
canvas.ctx = canvas.createNewContext(null, false);
if (canvas.ctx == null) {
canvas.drawingSurfaceObject.unLock();
// Issue 260 : indicate fatal error and notify error listeners
canvas.setFatalError();
RenderingError err =
new RenderingError(RenderingError.CONTEXT_CREATION_ERROR,
J3dI18N.getString("Renderer7"));
err.setCanvas3D(canvas);
err.setGraphicsDevice(canvas.graphicsConfiguration.getDevice());
notifyErrorListeners(err);
break doneRender;
}
// createNewContext finishes with a release, re-make current so the init calls below work
canvas.makeCtxCurrent();
if (canvas.graphics2D != null) {
canvas.graphics2D.init();
}
canvas.ctxTimeStamp = VirtualUniverse.mc.getContextTimeStamp();
canvas.screen.renderer.listOfCtxs.add(canvas.ctx);
canvas.screen.renderer.listOfCanvases.add(canvas);
// enable separate specular color
canvas.enableSeparateSpecularColor();
}
// create the cache texture state in canvas
// for state download checking purpose
if (canvas.texUnitState == null) {
canvas.createTexUnitState();
}
canvas.drawingSurfaceObject.contextValidated();
canvas.screen.renderer.currentCtx = canvas.ctx;
canvas.screen.renderer.currentDrawable = canvas.drawable;
canvas.graphicsContext3D.initializeState();
canvas.ctxChanged = true;
canvas.canvasDirty = 0xffff;
// Update Appearance
canvas.graphicsContext3D.updateState(canvas.view.renderBin, RenderMolecule.SURFACE);
canvas.currentLights = new LightRetained[canvas.getNumCtxLights(canvas.ctx)];
for (j=0; j<canvas.currentLights.length; j++) {
canvas.currentLights[j] = null;
}
}
canvas.makeCtxCurrent();
try {
switch (command) {
case GraphicsContext3D.CLEAR:
canvas.graphicsContext3D.doClear();
break;
case GraphicsContext3D.DRAW:
canvas.graphicsContext3D.doDraw(
(Geometry)m[nmesg].args[2]);
break;
case GraphicsContext3D.SWAP:
canvas.doSwap();
break;
case GraphicsContext3D.READ_RASTER:
canvas.graphicsContext3D.doReadRaster(
(Raster)m[nmesg].args[2]);
break;
case GraphicsContext3D.SET_APPEARANCE:
canvas.graphicsContext3D.doSetAppearance(
(Appearance)m[nmesg].args[2]);
break;
case GraphicsContext3D.SET_BACKGROUND:
canvas.graphicsContext3D.doSetBackground(
(Background)m[nmesg].args[2]);
break;
case GraphicsContext3D.SET_FOG:
canvas.graphicsContext3D.doSetFog(
(Fog)m[nmesg].args[2]);
break;
case GraphicsContext3D.SET_LIGHT:
canvas.graphicsContext3D.doSetLight(
(Light)m[nmesg].args[2],
((Integer)m[nmesg].args[3]).intValue());
break;
case GraphicsContext3D.INSERT_LIGHT:
canvas.graphicsContext3D.doInsertLight(
(Light)m[nmesg].args[2],
((Integer)m[nmesg].args[3]).intValue());
break;
case GraphicsContext3D.REMOVE_LIGHT:
canvas.graphicsContext3D.doRemoveLight(
((Integer)m[nmesg].args[2]).intValue());
break;
case GraphicsContext3D.ADD_LIGHT:
canvas.graphicsContext3D.doAddLight(
(Light)m[nmesg].args[2]);
break;
case GraphicsContext3D.SET_HI_RES:
canvas.graphicsContext3D.doSetHiRes(
(HiResCoord)m[nmesg].args[2]);
break;
case GraphicsContext3D.SET_MODEL_TRANSFORM:
t3d = (Transform3D)m[nmesg].args[2];
canvas.graphicsContext3D.doSetModelTransform(t3d);
break;
case GraphicsContext3D.MULTIPLY_MODEL_TRANSFORM:
t3d = (Transform3D)m[nmesg].args[2];
canvas.graphicsContext3D.doMultiplyModelTransform(t3d);
break;
case GraphicsContext3D.SET_SOUND:
canvas.graphicsContext3D.doSetSound(
(Sound)m[nmesg].args[2],
((Integer)m[nmesg].args[3]).intValue());
break;
case GraphicsContext3D.INSERT_SOUND:
canvas.graphicsContext3D.doInsertSound(
(Sound)m[nmesg].args[2],
((Integer)m[nmesg].args[3]).intValue());
break;
case GraphicsContext3D.REMOVE_SOUND:
canvas.graphicsContext3D.doRemoveSound(
((Integer)m[nmesg].args[2]).intValue());
break;
case GraphicsContext3D.ADD_SOUND:
canvas.graphicsContext3D.doAddSound(
(Sound)m[nmesg].args[2]);
break;
case GraphicsContext3D.SET_AURAL_ATTRIBUTES:
canvas.graphicsContext3D.doSetAuralAttributes(
(AuralAttributes)m[nmesg].args[2]);
break;
case GraphicsContext3D.SET_BUFFER_OVERRIDE:
canvas.graphicsContext3D.doSetBufferOverride(
((Boolean)m[nmesg].args[2]).booleanValue());
break;
case GraphicsContext3D.SET_FRONT_BUFFER_RENDERING:
canvas.graphicsContext3D.doSetFrontBufferRendering(
((Boolean)m[nmesg].args[2]).booleanValue());
break;
case GraphicsContext3D.SET_STEREO_MODE:
canvas.graphicsContext3D.doSetStereoMode(
((Integer)m[nmesg].args[2]).intValue());
break;
case GraphicsContext3D.FLUSH:
canvas.graphicsContext3D.doFlush(
((Boolean)m[nmesg].args[2]).booleanValue());
break;
case GraphicsContext3D.FLUSH2D:
canvas.graphics2D.doFlush();
break;
case GraphicsContext3D.DRAWANDFLUSH2D:
Object ar[] = m[nmesg].args;
canvas.graphics2D.doDrawAndFlushImage(
(BufferedImage) ar[2],
((Point) ar[3]).x,
((Point) ar[3]).y,
(ImageObserver) ar[4]);
break;
case GraphicsContext3D.DISPOSE2D:
// Issue 583 - the graphics2D field may be null here
if (canvas.graphics2D != null) {
canvas.graphics2D.doDispose();
}
break;
case GraphicsContext3D.SET_MODELCLIP:
canvas.graphicsContext3D.doSetModelClip(
(ModelClip)m[nmesg].args[2]);
break;
default:
break;
}
} catch (RuntimeException ex) {
ex.printStackTrace();
// Issue 260 : indicate fatal error and notify error listeners
canvas.setFatalError();
RenderingError err =
new RenderingError(RenderingError.CONTEXT_CREATION_ERROR,
J3dI18N.getString("Renderer6"));
err.setCanvas3D(canvas);
err.setGraphicsDevice(canvas.graphicsConfiguration.getDevice());
notifyErrorListeners(err);
}
m[nmesg++].decRefcount();
canvas.releaseCtx();
} else { // retained mode rendering
long startRenderTime = 0L;
if (MasterControl.isStatsLoggable(Level.INFO)) {
// Instrumentation of Java 3D renderer
startRenderTime = System.nanoTime();
}
m[nmesg++].decRefcount();
if (canvas.isFatalError()) {
continue;
}
ImageComponent2DRetained offBufRetained = null;
if (renderType == J3dMessage.RENDER_OFFSCREEN) {
// Issue 131: set offScreenRendering flag here, since it
// otherwise won't be set for auto-off-screen rendering
// (which doesn't use renderOffScreenBuffer)
canvas.offScreenRendering = true;
if (canvas.drawable == null || !canvas.active) {
canvas.offScreenRendering = false;
continue;
} else {
offBufRetained = (ImageComponent2DRetained)
canvas.offScreenBuffer.retained;
if (offBufRetained.isByReference()) {
offBufRetained.geomLock.getLock();
}
offBufRetained.evaluateExtensions(canvas);
}
} else if (!canvas.active) {
continue;
}
// Issue 78 - need to get the drawingSurface info every
// frame; this is necessary since the HDC (window ID)
// on Windows can become invalidated without our
// being notified!
if (!canvas.offScreen) {
canvas.drawingSurfaceObject.getDrawingSurfaceObjectInfo();
}
renderBin = canvas.view.renderBin;
// setup rendering context
// We need to catch NullPointerException when the dsi
// gets yanked from us during a remove.
if (canvas.useSharedCtx) {
if (sharedCtx == null) {
sharedCtxDrawable = canvas.drawable;
// Always lock for context create
if (!canvas.drawingSurfaceObject.renderLock()) {
if ((offBufRetained != null) &&
offBufRetained.isByReference()) {
offBufRetained.geomLock.unLock();
}
canvas.offScreenRendering = false;
break doneRender;
}
synchronized (VirtualUniverse.mc.contextCreationLock) {
sharedCtx = null;
try {
sharedCtx = canvas.createNewContext(null, true);
} catch (RuntimeException ex) {
ex.printStackTrace();
}
if (sharedCtx == null) {
canvas.drawingSurfaceObject.unLock();
if ((offBufRetained != null) &&
offBufRetained.isByReference()) {
offBufRetained.geomLock.unLock();
}
canvas.offScreenRendering = false;
// Issue 260 : indicate fatal error and notify error listeners
canvas.setFatalError();
RenderingError err =
new RenderingError(RenderingError.CONTEXT_CREATION_ERROR,
J3dI18N.getString("Renderer7"));
err.setCanvas3D(canvas);
err.setGraphicsDevice(canvas.graphicsConfiguration.getDevice());
notifyErrorListeners(err);
break doneRender;
}
sharedCtxTimeStamp =
VirtualUniverse.mc.getContextTimeStamp();
needToRebuildDisplayList = true;
}
canvas.drawingSurfaceObject.unLock();
}
}
if (canvas.ctx == null) {
// Always lock for context create
if (!canvas.drawingSurfaceObject.renderLock()) {
if ((offBufRetained != null) &&
offBufRetained.isByReference()) {
offBufRetained.geomLock.unLock();
}
canvas.offScreenRendering = false;
break doneRender;
}
synchronized (VirtualUniverse.mc.contextCreationLock) {
canvas.ctx = null;
try {
canvas.ctx = canvas.createNewContext(sharedCtx, false);
} catch (RuntimeException ex) {
ex.printStackTrace();
}
if (canvas.ctx == null) {
canvas.drawingSurfaceObject.unLock();
if ((offBufRetained != null) &&
offBufRetained.isByReference()) {
offBufRetained.geomLock.unLock();
}
canvas.offScreenRendering = false;
// Issue 260 : indicate fatal error and notify error listeners
canvas.setFatalError();
RenderingError err =
new RenderingError(RenderingError.CONTEXT_CREATION_ERROR,
J3dI18N.getString("Renderer7"));
err.setCanvas3D(canvas);
err.setGraphicsDevice(canvas.graphicsConfiguration.getDevice());
notifyErrorListeners(err);
break doneRender;
}
if (canvas.graphics2D != null) {
canvas.graphics2D.init();
}
canvas.ctxTimeStamp =
VirtualUniverse.mc.getContextTimeStamp();
listOfCtxs.add(canvas.ctx);
listOfCanvases.add(canvas);
if (renderBin.nodeComponentList.size() > 0) {
for (i = 0; i < renderBin.nodeComponentList.size(); i++) {
NodeComponentRetained nc = (NodeComponentRetained)renderBin.nodeComponentList.get(i);
if(nc instanceof ImageComponentRetained) {
((ImageComponentRetained)nc).evaluateExtensions(canvas);
}
}
}
// enable separate specular color
canvas.enableSeparateSpecularColor();
}
// create the cache texture state in canvas
// for state download checking purpose
if (canvas.texUnitState == null) {
canvas.createTexUnitState();
}
canvas.resetImmediateRendering();
canvas.drawingSurfaceObject.contextValidated();
if (!canvas.useSharedCtx) {
canvas.needToRebuildDisplayList = true;
}
canvas.drawingSurfaceObject.unLock();
} else {