aboutsummaryrefslogtreecommitdiffstats
path: root/src/native/d3d/Canvas3D.cpp
blob: 3636e6d247c63f003393a6523cb2a0f04c1018e7 (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
/*
 * $RCSfile$
 *
 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
 *
 * Use is subject to license terms.
 *
 * $Revision$
 * $Date$
 * $State$
 */

#include "StdAfx.h"


extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_setDrawActive(
    JNIEnv *env,
    jobject obj,
    jint fd)
{
    // This function is only used for Solaris OpenGL
}


extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_widSync(
    JNIEnv *env,
    jobject obj,
    jint fd,
    jint numWindows)
{
    // This function is only used for Solaris OpenGL
}



extern "C" JNIEXPORT
jboolean JNICALL Java_javax_media_j3d_Canvas3D_useSharedCtx(
    JNIEnv *env, 
    jobject obj)
{
    return JNI_FALSE;
}



extern "C" JNIEXPORT
jlong JNICALL Java_javax_media_j3d_Canvas3D_createNewContext(
    JNIEnv  *env,
    jobject  obj,
    jlong    display,
    jint     window, 
    jint     vid,    
    jlong    fbConfigListPtr,
    jlong    sharedCtx,
    jboolean isSharedCtx,
    jboolean offScreen,
	jboolean glslLibraryAvailable,
    jboolean cgLibraryAvailable)
{
    HWND hwnd = WindowFromDC(reinterpret_cast<HDC>(jlong(window)));

    lock();
    D3dCtx* ctx = new D3dCtx(env, obj, hwnd, offScreen, vid);
    if (ctx == NULL) {
	printf("%s", getErrorMessage(OUTOFMEMORY));
	unlock();
	return 0;
    }

    if (offScreen) {

	jclass cls = (jclass) env->GetObjectClass(obj);
	jfieldID fieldId = env->GetFieldID(cls,
					   "offScreenCanvasSize", 
					   "Ljava/awt/Dimension;");
	jobject dimObj = env->GetObjectField(obj, fieldId);
	if (dimObj == NULL) {
	    // user invoke queryProperties()
	    ctx->offScreenWidth = 1;
	    ctx->offScreenHeight = 1;
	} else {
	    cls = (jclass) env->GetObjectClass(dimObj);
	    fieldId = env->GetFieldID(cls, "width", "I");
	    ctx->offScreenWidth = env->GetIntField(dimObj, fieldId);
	    fieldId = env->GetFieldID(cls, "height", "I");
	    ctx->offScreenHeight = env->GetIntField(dimObj, fieldId);
	}
    }

    if (!ctx->initialize(env, obj)) {
	delete ctx;
	unlock();
	return 0;
    } 
    d3dCtxList.push_back(ctx);

    unlock();
    return reinterpret_cast<jlong>(ctx);
}


extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_createQueryContext(
    JNIEnv *env,
    jobject obj,
    jlong display,
    jint window,
    jint vid,
    jlong fbConfigListPtr,
    jboolean offScreen,
    jint width,
    jint height,
	jboolean glslLibraryAvailable,
    jboolean cgLibraryAvailable)
{
    HWND hwnd = WindowFromDC(reinterpret_cast<HDC>(jlong(window)));

    lock();
    // always use offscreen for property since it
    // makes no difference in D3D and this will also
    // instruct initialize() to use offScreenWidth/Height
    // instead of current window width/height to create
    // context.

    D3dCtx* ctx = new D3dCtx(env, obj, hwnd, true, vid);
    if (ctx == NULL) {
	printf("%s", getErrorMessage(OUTOFMEMORY));
	unlock();
	return;
    }

    ctx->offScreenWidth = width;
    ctx->offScreenHeight = height;

    ctx->initialize(env, obj);
    delete ctx;
    unlock();
}

extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_useCtx(
    JNIEnv *env, 
    jclass cl, 
    jlong ctx, 
    jlong display, 
    jint window)
{
    // D3D doesn't have notation of current context
}


extern "C" JNIEXPORT
jint JNICALL Java_javax_media_j3d_Canvas3D_getNumCtxLights(
    JNIEnv *env, 
    jobject obj,
    jlong ctx)
{
   GetDevice2();

   int nlight = d3dCtx->deviceInfo->maxActiveLights;
   if (nlight <= 0) {
       // In emulation & referene mode, D3D return -1 
       // work around by setting 8.
       nlight = 8;
   }
   return nlight;
}


extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_composite(
    JNIEnv *env,
    jobject obj,
    jlong ctx,
    jint px,
    jint py,
    jint minX,
    jint minY,
    jint maxX,
    jint maxY,
    jint rasWidth,
    jbyteArray imageYdown,
    jint winWidth,
    jint winHeight)

{
    GetDevice();

    // However we use the following texturemapping function instead
    // so this will not invoke.
    if (d3dCtx->backSurface == NULL) {
	device->GetBackBuffer(0,0, D3DBACKBUFFER_TYPE_MONO,//iSwapChain is 0
			      &d3dCtx->backSurface);
    }    
    jbyte *byteData = (jbyte *) (env->GetPrimitiveArrayCritical(
					      imageYdown,   NULL));
    compositeDataToSurface(px, py,
			   minX, minY, maxX-minX, maxY-minY,
			   rasWidth,
			   byteData, d3dCtx->backSurface);
    env->ReleasePrimitiveArrayCritical(imageYdown, byteData, 0);
}

extern "C" JNIEXPORT
jboolean JNICALL Java_javax_media_j3d_Canvas3D_initTexturemapping(
    JNIEnv *env,
    jobject texture,
    jlong ctx,
    jint texWidth,
    jint texHeight,
    jint objectId)
{
    GetCtx2();


    if ((objectId >= 0) &&
	(objectId < d3dCtx->textureTableLen) &&
	(d3dCtx->textureTable[objectId] != NULL)) {
	// delete the previous texture reference
	// when canvas resize
	 Java_javax_media_j3d_Canvas3D_freeTexture(env,
						   NULL,
						   ctx,
						   objectId);
    }

    Java_javax_media_j3d_TextureRetained_bindTexture(
	 env, texture, ctx, objectId, TRUE);

     Java_javax_media_j3d_TextureRetained_updateTextureImage(
         env, texture, ctx, 1, 0, J3D_RGBA, 0, texWidth, texHeight, 0, NULL);
     
     return (d3dCtx->textureTable[objectId] != NULL);
}


extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_texturemapping(
    JNIEnv *env,
    jobject texture,
    jlong ctx,
    jint px,
    jint py,
    jint minX,
    jint minY,
    jint maxX,
    jint maxY,
    jint texWidth,
    jint texHeight,
    jint rasWidth,
    jint format,
    jint objectId,
    jbyteArray imageYdown,
    jint winWidth,
    jint winHeight)
{
    GetDevice();

    Java_javax_media_j3d_TextureRetained_bindTexture(
	 env, texture, ctx, objectId, TRUE);


    Java_javax_media_j3d_Texture2DRetained_updateTextureSubImage(
         env, texture, ctx, 0, minX, minY, J3D_RGBA, format,
	 minX, minY, rasWidth, maxX-minX, maxY-minY, imageYdown);

    LPDIRECT3DTEXTURE9 surf = d3dCtx->textureTable[objectId];

    if (surf == NULL) {
	if (debug) {
	    printf("[Java 3D] Fail to apply texture in J3DGraphics2D !\n");
	}
	return;
    }

    D3DTLVERTEX screenCoord;	
    DWORD zcmpfunc;

    screenCoord.sx = (px + minX) - 0.5f;
    screenCoord.sy = (py + minY) - 0.5f;

    // sz can be any number since we will disable z buffer
    // However rhw can't be 0, otherwise texture will not shown
    screenCoord.sz = 0.999f;
    screenCoord.rhw = 1;

    DWORD blendEnable;
    DWORD srcBlend;
    DWORD dstBlend;

    // disable z buffer
    device->GetRenderState(D3DRS_ZFUNC, &zcmpfunc);
    device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
    device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);

    device->GetRenderState(D3DRS_ALPHABLENDENABLE, &blendEnable);
    device->GetRenderState(D3DRS_SRCBLEND,  &srcBlend);
    device->GetRenderState(D3DRS_DESTBLEND, &dstBlend);

    device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
    device->SetRenderState(D3DRS_SRCBLEND,  D3DBLEND_SRCALPHA);
    device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

    drawTextureRect(d3dCtx, device, surf, screenCoord,
		    minX, minY, maxX, maxY,
		    maxX - minX, maxY - minY, false); 

    Java_javax_media_j3d_TextureRetained_bindTexture(
	 env, texture, ctx, objectId, FALSE);

    device->SetRenderState(D3DRS_ALPHABLENDENABLE, blendEnable);
    device->SetRenderState(D3DRS_SRCBLEND,  srcBlend);
    device->SetRenderState(D3DRS_DESTBLEND, dstBlend);
    device->SetRenderState(D3DRS_ZFUNC, zcmpfunc);
    device->SetRenderState(D3DRS_ZWRITEENABLE, 
			   d3dCtx->zWriteEnable);
}


extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_clear(
    JNIEnv *env,
    jobject obj,
    jlong ctx,
    jfloat r,
    jfloat g,
    jfloat b,
    jint winWidth,
    jint winHeight,
    jobject pa2d,
    jint imageScaleMode, 
    jbyteArray pixels_obj)
{

    GetDevice();

 /* Java 3D always clears the Z-buffer */

    if (!d3dCtx->zWriteEnable) {
	device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
    } 

    device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 
		  D3DCOLOR_COLORVALUE(r, g, b, 1.0f), 1.0, 0);

    if (pa2d) {
	jclass pa2d_class = env->GetObjectClass(pa2d);
	/*
	jfieldID id = env->GetFieldID(pa2d_class, "surfaceDirty", "I");
	if (env->GetIntField(pa2d, id) == NOTLIVE) {
	    return;
	}
	*/
	// It is possible that (1) Another user thread will free this
	// image. (2) Another Renderer thread in case of multiple screen
	// will invoke clear() at the same time.
	lockBackground();


	jfieldID id = env->GetFieldID(pa2d_class, "hashId", "I");
	int hashCode = env->GetIntField(pa2d, id);	

	D3dImageComponent *d3dImage =
	    D3dImageComponent::find(&BackgroundImageList, d3dCtx, hashCode);

	id = env->GetFieldID(pa2d_class, "width", "I");
	int width = env->GetIntField(pa2d, id);
	id = env->GetFieldID(pa2d_class, "height", "I");
	int height = env->GetIntField(pa2d, id);
	
	LPDIRECT3DTEXTURE9 surf;

	if ((d3dImage == NULL) || (d3dImage->surf == NULL)) {
	    surf = createSurfaceFromImage(env, pa2d, ctx,
					   width, height, pixels_obj);
	     if (surf == NULL) {
		 if (d3dImage != NULL) {
		     D3dImageComponent::remove(&BackgroundImageList, d3dImage);
		 }
		 unlockBackground();
		 return;
	     }

	     if (d3dImage == NULL) {
		 d3dImage = 
		     D3dImageComponent::add(&BackgroundImageList, d3dCtx, hashCode, surf);
	     } else {
		 // need to add this one because the new imageDirtyFlag may
		 // cause d3dImage->surf set to NULL
		 d3dImage->surf = surf;
	     } 
        }


	D3DTLVERTEX screenCoord;	
	DWORD zcmpfunc;
	boolean texModeRepeat;
	int scaleWidth, scaleHeight;
	float sw, sh;

	// sz can be any number since we already disable z buffer
	// However rhw can't be 0, otherwise texture will not shown
	screenCoord.sz = 0.999f;
	screenCoord.rhw = 1;

	// disable z buffer
	device->GetRenderState(D3DRS_ZFUNC, &zcmpfunc);
	device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
	device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);

	switch (imageScaleMode){
	case javax_media_j3d_Background_SCALE_NONE:
	    screenCoord.sx = -0.5f;
	    screenCoord.sy = -0.5f;
	    scaleWidth = width;
	    scaleHeight = height;
	    texModeRepeat = FALSE;
	    break;
	case javax_media_j3d_Background_SCALE_FIT_MIN:
	    screenCoord.sx = -0.5f;
	    screenCoord.sy = -0.5f;
	    sw = winWidth/(float) width;
	    sh = winHeight/(float) height;
	    if (sw >= sh) {
		scaleWidth = int(width*sh);
		scaleHeight = winHeight;
	    } else {
		scaleWidth = winWidth;
		scaleHeight = int(height*sw);		
	    }
	    texModeRepeat = FALSE;	    
	    break;
	case javax_media_j3d_Background_SCALE_FIT_MAX:
	    screenCoord.sx = -0.5f;
	    screenCoord.sy = -0.5f;
	    sw = winWidth/(float) width;
	    sh = winHeight/(float) height;
	    if (sw >= sh) {
		scaleWidth = winWidth;
		scaleHeight = int(height*sw);		
	    } else {
		scaleWidth = int(width*sh);
		scaleHeight = winHeight;		
	    }
	    texModeRepeat = FALSE;	    
	    break;	
	case javax_media_j3d_Background_SCALE_FIT_ALL:
	    screenCoord.sx = -0.5f;
	    screenCoord.sy = -0.5f;
	    scaleWidth = winWidth;
	    scaleHeight = winHeight;
	    texModeRepeat = FALSE;	    
	    break;
	case javax_media_j3d_Background_SCALE_REPEAT:	  
	    screenCoord.sx = -0.5f;
	    screenCoord.sy = -0.5f;
	    scaleWidth = winWidth;
	    scaleHeight = winHeight;
	    texModeRepeat = TRUE;
	    break;	    
	case javax_media_j3d_Background_SCALE_NONE_CENTER:
	    screenCoord.sx = (winWidth - width)/2.0f - 0.5f;
	    screenCoord.sy = (winHeight - height)/2.0f -0.5f;
	    scaleWidth = width;
	    scaleHeight = height;
	    texModeRepeat = FALSE;
	    break;
 	default:
	    printf("Unknown Background scale mode %d\n", imageScaleMode);
	}

	drawTextureRect(d3dCtx, device, d3dImage->surf,
			screenCoord, 0, 0, width, height, 
			scaleWidth, scaleHeight, texModeRepeat);

	device->SetRenderState(D3DRS_ZFUNC, zcmpfunc);
	device->SetRenderState(D3DRS_ZWRITEENABLE, 
			       d3dCtx->zWriteEnable);
	unlockBackground();
    } else {
	if (!d3dCtx->zWriteEnable) {
	    device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
	}
    }


}



extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_setRenderMode(
    JNIEnv *env, 
    jobject obj, 
    jlong ctx,
    jint mode,
    jboolean dbEnable)
{
    // D3D v8.0 doesn't support stereo
}



extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_clearAccum(
    JNIEnv *env, 
    jobject obj,
    jlong ctx)
{
    // D3D use full-scene antialiasing capbilities in device
    // instead of accumulation buffer (which it didn't support)
}



extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_accum(
    JNIEnv *env, 
    jobject obj, 
    jlong ctx,
    jfloat value)
{
    // D3D use full-scene antialiasing capbilities in device
    // instead of accumulation buffer (which didn't support)
}

extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_accumReturn(
    JNIEnv *env, 
    jobject obj,
    jlong ctx) 
{
    // D3D use full-scene antialiasing capbilities in device
    // instead of accumulation buffer (which it didn't support)
}

extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_setDepthBufferWriteEnable(
    JNIEnv *env, 
    jobject obj, 
    jlong ctx,
    jboolean mode)
{
    GetDevice();

    d3dCtx->zWriteEnable = mode;
    device->SetRenderState(D3DRS_ZWRITEENABLE, mode);
}


VOID freePointerList()
{
   if (useFreePointerList0) {
	if (freePointerList1.size() > 0) {
	    lockSurfaceList();
	    for (ITER_VOID p = freePointerList1.begin();  
		 p != freePointerList1.end(); ++p) {
		delete (*p);
	    }

	    freePointerList1.clear();
	    unlockSurfaceList();
	}
	useFreePointerList0 = false;
    } else {
	if (freePointerList0.size() > 0) {
	    lockSurfaceList();
	    for (ITER_VOID p = freePointerList0.begin();  
		 p != freePointerList0.end(); ++p) {
		delete (*p);
	    }

	    freePointerList0.clear();
	    unlockSurfaceList();
	}
	useFreePointerList0 = true;

    }
}


extern "C" JNIEXPORT
jint JNICALL Java_javax_media_j3d_Canvas3D_swapBuffers(
    JNIEnv *env,
    jobject obj,
    jlong ctx,
    jlong display,
    jint win)
{
    GetDevice2();

    int retCode = NOCHANGE;

    HRESULT hr = device->Present(NULL, NULL, NULL, NULL);

    if (FAILED(hr)) {
	hr = device->TestCooperativeLevel();
	if (D3DERR_DEVICELOST == hr) {
	    return NOCHANGE;
	}
	if (D3DERR_DEVICENOTRESET == hr) {
	    if (debug) {
		printf("Buffer swap error %s, try Reset() the surface... \n",
		       DXGetErrorString9(hr));	    
	    }
	    retCode = d3dCtx->resetSurface(env, obj);
	    GetDevice2();
	    hr = device->Present(NULL, NULL, NULL, NULL);
	    if (FAILED(hr)) {
		if (debug) {
		    printf("Buffer swap error %s \n",
			   DXGetErrorString9(hr));
		}
	    }
	} 

    }

    d3dCtx->freeList();
    freePointerList();
    return retCode;
}

extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_syncRender(
      JNIEnv *env, 
      jobject obj, 
      jlong ctx,
      jboolean waitFlag)  
{
    // do nothing since D3D always wait in Blt
}


extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_newDisplayList(
    JNIEnv *env,
    jobject obj,
    jlong ctx,
    jint id)
{
    GetCtx();

    if (id <= 0) {
	if (debug) {
	    printf("In Canvas3D.newDisplayList id pass in = %d !\n", id);
	}
	return;
    }

    if (id >= d3dCtx->dlTableSize) {
	int newSize = d3dCtx->dlTableSize << 1;
	if (id >= newSize) {
	    newSize = id+1;
	}
	int i=0; 
	LPD3DDISPLAYLIST *newTable =  new LPD3DDISPLAYLIST[newSize];

	if (newTable == NULL) {
	    printf("%s", getErrorMessage(OUTOFMEMORY));
	    exit(1);
	}
	// entry 0 is not used
	newTable[0] = NULL;
	while (++i < d3dCtx->dlTableSize) {
	    newTable[i] = d3dCtx->displayListTable[i];
	}
	while (i < newSize) {
	    newTable[i++] = NULL;
	}
	d3dCtx->dlTableSize = newSize;	
	SafeDelete(d3dCtx->displayListTable);
	d3dCtx->displayListTable = newTable;
    }

    if (d3dCtx->displayListTable[id] != NULL) {
	SafeDelete(d3dCtx->displayListTable[id]);
    }
    d3dCtx->displayListTable[id] = new D3dDisplayList();
    if (d3dCtx->displayListTable[id] == NULL) {
	printf("%s", getErrorMessage(OUTOFMEMORY));
	exit(1);
    }
    d3dCtx->currDisplayListID = id;
}

extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_endDisplayList(
    JNIEnv *env,
    jobject obj,
    jlong ctx)
{
    GetDevice();
    d3dCtx->displayListTable[d3dCtx->currDisplayListID]->optimize(d3dCtx);
    d3dCtx->currDisplayListID = 0;
}

extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_callDisplayList(
    JNIEnv *env,
    jobject obj,
    jlong ctx,
    jint id,
    jboolean isNonUniformScale)
{
    GetDevice();
    
    // TODO: Remove this two safe checks when release
    //  d3dCtx->displayListTable[id]->render(d3dCtx);
     

    if ((id <= 0) || (id >= d3dCtx->dlTableSize)) {
	if (debug) {
	    if (id <= 0) {
		printf("[Java 3D] Invalid Display List ID %d is invoked !\n", id);
	    } else {
		printf("[Java 3D] Display List ID %d not yet initialize !\n", id);
	    }
	}
	return;
    }

    LPD3DDISPLAYLIST dl = d3dCtx->displayListTable[id];

    if (dl == NULL) {
	if (debug) {
	    printf("[Java 3D] Display List ID %d not yet initialize !\n", id);
	}
	return;
    }
    dl->render(d3dCtx);

}

extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_freeDisplayList(
    JNIEnv *env,
    jclass cl,
    jlong ctx,
    jint id)
{
    GetCtx();

    if ((id < 0) || (id >= d3dCtx->dlTableSize)) {
	if (debug) {
	    printf("[Java 3D] FreeDisplayList, id %d not within table range %d!\n", id,
		   d3dCtx->dlTableSize);
	}
	return;
    }

    SafeDelete(d3dCtx->displayListTable[id]);
}


/* 
   Native function to delete OGL texture object after j3d texture object
   has been deleted by java garbage collector.
 */
extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_freeTexture(
    JNIEnv *env,
    jclass cls,
    jlong ctx,
    jint id)
{
    GetDevice();

    for (int i=0; i < d3dCtx->bindTextureIdLen; i++) {    
	if (d3dCtx->bindTextureId[i] == id) {
	    device->SetTexture(i, NULL);
	    d3dCtx->bindTextureId[i] = -1;
	}
    }

    if ((id >= d3dCtx->textureTableLen) || (id < 1)) {
	if (debug) {
	    printf("Internal Error : freeTexture ID %d, textureTableLen %d \n", 
		   id, d3dCtx->textureTableLen);
	}
	return;
    }

    d3dCtx->freeResource(d3dCtx->textureTable[id]);
    d3dCtx->textureTable[id] = NULL;
}


extern "C" JNIEXPORT 
jboolean JNICALL Java_javax_media_j3d_Canvas3D_isTexture3DAvailable(
    JNIEnv *env,
    jobject obj,
    jlong ctx)
{
    return JNI_FALSE;
}


extern "C" JNIEXPORT 
jint JNICALL Java_javax_media_j3d_Canvas3D_getTextureColorTableSize(
    JNIEnv *env,
    jobject obj,
    jlong ctx)
{
    // Not support by D3D
    return 0;
}


extern "C" JNIEXPORT 
jint JNICALL Java_javax_media_j3d_Canvas3D_getTextureUnitCount(
    JNIEnv *env,
    jobject obj,
    jlong ctx)
{
    GetCtx2();
    return d3dCtx->deviceInfo->maxTextureUnitStageSupport;
}


extern "C" JNIEXPORT
jint JNICALL Java_javax_media_j3d_Canvas3D_createOffScreenBuffer(
    JNIEnv *env,
    jobject obj,
    jlong ctx,
    jlong display,
    jint window,
    jlong fbConfigListPtr,
    jint width,
    jint height)
{

    
    if (ctx == 0) {
	// createContext() will be invoked later in Renderer
	return 1;
    }  else {
	GetCtx2();
	d3dCtx->d3dPresent.BackBufferWidth = width;
	d3dCtx->d3dPresent.BackBufferHeight = height;
	return SUCCEEDED(d3dCtx->resetSurface(env, obj));
    }
}



extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_destroyContext(
    JNIEnv *env,
    jclass cl,
    jlong display,
    jint window,
    jlong ctx)
{
    GetDevice();

    lock();
    d3dCtxList.erase(find(d3dCtxList.begin(), d3dCtxList.end(), d3dCtx));
    delete d3dCtx;
    unlock();

    Java_javax_media_j3d_Renderer_D3DCleanUp(env, cl);
}

extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_destroyOffScreenBuffer(
    JNIEnv *env,
    jobject obj,
    jlong ctx,
    jlong display,
    jlong fbConfigListPtr,
    jint window)
{
    // do nothing, since the old buffer will destory 
    // in createOffScreenBuffer
}


extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_readOffScreenBuffer(
    JNIEnv *env,
    jobject obj,
    jlong ctx,
    jint format,
    jint width,
    jint height)
{
    GetDevice();

    if (format == FORMAT_USHORT_GRAY) {
	printf("[Java 3D] readOffScreenBuffer not support FORMAT_USHORT_GRAY\n");
	return;
    }

    if (d3dCtx->backSurface == NULL) {
	HRESULT hr = device->GetBackBuffer(0,0, D3DBACKBUFFER_TYPE_MONO, //iSwapChain is 0
					   &d3dCtx->backSurface);
	if (FAILED(hr)) {
	    printf("[Java 3D] GetBackBuffer fail %s\n",
		   DXGetErrorString9(hr));
	    return;
	}
    }

    jclass cv_class =  env->GetObjectClass(obj);

    jfieldID byteData_field = env->GetFieldID(cv_class, "byteBuffer", "[B");
    jbyteArray byteData_array = (jbyteArray) env->GetObjectField(obj, byteData_field);
    jbyte *byteData = (jbyte *) env->GetPrimitiveArrayCritical(
					       byteData_array, NULL);

    copyDataFromSurface(format, 0, 0, width, height, byteData, 
			d3dCtx->backSurface);

    env->ReleasePrimitiveArrayCritical(byteData_array, byteData, 0);
    return;
}


extern "C" JNIEXPORT
jint JNICALL Java_javax_media_j3d_Canvas3D_resizeD3DCanvas(
    JNIEnv *env,
    jobject obj,
    jlong ctx)
{
    int status;

    GetCtx2();
    lock();
    status = d3dCtx->resize(env, obj);
    unlock();

    return status;
}


extern "C" JNIEXPORT
jint JNICALL Java_javax_media_j3d_Canvas3D_toggleFullScreenMode(
    JNIEnv *env,
    jobject obj,
    jlong ctx)
{
    int status;

    GetCtx2();
    lock();
    status = d3dCtx->toggleMode(!d3dCtx->bFullScreen, env, obj);
    unlock();  
    if (status == RECREATEDFAIL) {
	return RECREATEDDRAW;
    }
    return status;
}

extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_setFullSceneAntialiasing(
    JNIEnv *env, 
    jobject obj, 
    jlong ctx,
    jboolean enable)
{
    GetDevice();

    if (!implicitMultisample) {
	device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, enable);
    }
}

extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Renderer_D3DCleanUp(
    JNIEnv *env,
    jobject obj)
{
    lock();
    if (d3dCtxList.empty()) {
	D3dDriverInfo::release();
    }
    unlock();

    // Need to call it two times to free both list0 and list1
    freePointerList();
    freePointerList();
}

extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_ImageComponent2DRetained_freeD3DSurface(
    JNIEnv *env,
    jobject image,
    jint hashCode)

{

    lockImage();
    D3dImageComponent::remove(&RasterList, hashCode);
    unlockImage();
    lockBackground();
    D3dImageComponent::remove(&BackgroundImageList, hashCode);
    unlockBackground();
}


extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_beginScene(
       JNIEnv *env,
       jobject obj, 
       jlong ctx)
{
    GetDevice();
    device->BeginScene();
}


extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_Canvas3D_endScene(
       JNIEnv *env,
       jobject obj, 
       jlong ctx)
{
    GetDevice();
    device->EndScene();
}


extern "C" JNIEXPORT
jboolean JNICALL Java_javax_media_j3d_Canvas3D_validGraphicsMode(
       JNIEnv *env,
       jobject obj) 
{
    DEVMODE devMode;
    
    EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devMode);
    return (devMode.dmBitsPerPel > 8);
}