forked from axeldavy/Xnine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SDL_nine.c
1378 lines (1186 loc) · 42.1 KB
/
SDL_nine.c
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
// ----------------------------------------
// SDL_nine
#include "SDL_nine.h"
#include <dlfcn.h>
#include <malloc.h>
#include <memory.h>
#include <fcntl.h>
#include <time.h>
#include <X11/Xlib.h>
#include <xcb/xcb.h>
#include <xcb/dri3.h>
#include <xcb/present.h>
#include <X11/Xlib-xcb.h>
#include <d3dadapter/drm.h>
#include <SDL.h>
#include <SDL_syswm.h>
#include "dri3.h"
// ----------- utils funcs / stubs ------------------------
#define TRACE(...)
#define FIXME(...) fprintf(stderr, __VA_ARGS__)
#define WARN(...) fprintf(stderr, __VA_ARGS__)
#define ERR(...) fprintf(stderr, __VA_ARGS__)
static inline BOOL IsEqualGUID(const GUID* a, const GUID* b)
{
return memcmp(a,b,sizeof(GUID)) == 0;
}
static const char* debugstr_guid(const GUID* id)
{
return "GUID";
}
static inline LONG InterlockedIncrement( LONG volatile *dest )
{
return __sync_add_and_fetch(dest, 1);;
}
static inline LONG InterlockedDecrement( LONG volatile *dest )
{
return __sync_sub_and_fetch(dest, 1);;
}
void *get_device_vtable();
// ---------------------------------------------------------------------------------------
static const D3DFORMAT ConvertFromSDL(Uint32 format)
{
switch (format)
{
case SDL_PIXELFORMAT_ARGB4444: return D3DFMT_A4R4G4B4;
case SDL_PIXELFORMAT_RGB332: return D3DFMT_R3G3B2;
case SDL_PIXELFORMAT_ARGB1555: return D3DFMT_A1R5G5B5;
case SDL_PIXELFORMAT_RGB555: return D3DFMT_X1R5G5B5;
case SDL_PIXELFORMAT_RGB565: return D3DFMT_R5G6B5;
case SDL_PIXELFORMAT_RGB24: return D3DFMT_R8G8B8;
case SDL_PIXELFORMAT_RGB888: return D3DFMT_X8R8G8B8;
case SDL_PIXELFORMAT_ARGB8888: return D3DFMT_A8R8G8B8;
case SDL_PIXELFORMAT_ARGB2101010: return D3DFMT_A2R10G10B10;
default:
case SDL_PIXELFORMAT_UNKNOWN: return D3DFMT_UNKNOWN;
}
}
static const Uint32 ConvertToSDL(D3DFORMAT format)
{
switch (format)
{
case D3DFMT_A4R4G4B4: return SDL_PIXELFORMAT_ARGB4444;
case D3DFMT_R3G3B2: return SDL_PIXELFORMAT_RGB332;
case D3DFMT_A1R5G5B5: return SDL_PIXELFORMAT_ARGB1555;
case D3DFMT_X1R5G5B5: return SDL_PIXELFORMAT_RGB555;
case D3DFMT_R5G6B5: return SDL_PIXELFORMAT_RGB565;
case D3DFMT_R8G8B8: return SDL_PIXELFORMAT_RGB24;
case D3DFMT_X8R8G8B8: return SDL_PIXELFORMAT_RGB888;
case D3DFMT_A8R8G8B8: return SDL_PIXELFORMAT_ARGB8888;
case D3DFMT_A2R10G10B10: return SDL_PIXELFORMAT_ARGB2101010;
default:
case D3DFMT_UNKNOWN: return SDL_PIXELFORMAT_UNKNOWN;
}
}
// ---- dlls/winex11.drv/d3dadapter.c ----------------------------------------------------------------
#include <libdrm/drm.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>
/*#ifndef D3DPRESENT_DONOTWAIT
#define D3DPRESENT_DONOTWAIT 0x00000001
#endif*/
#if D3DADAPTER9_WITHDRI2
static int is_dri2_fallback = 0;
#endif
const GUID IID_IDirect3D9Ex = { 0x02177241, 0x69FC, 0x400C, {0x8F, 0xF1, 0x93, 0xA4, 0x4D, 0xF6, 0x86, 0x1D}};
const GUID IID_IDirect3D9 = { 0x81BDCBCA, 0x64D4, 0x426D, {0xAE, 0x8D, 0xAD, 0x1, 0x47, 0xF4, 0x27, 0x5C}};
const GUID IID_ID3DPresent = { 0x77D60E80, 0xF1E6, 0x11DF, { 0x9E, 0x39, 0x95, 0x0C, 0xDF, 0xD7, 0x20, 0x85 } };
const GUID IID_ID3DPresentGroup = { 0xB9C3016E, 0xF32A, 0x11DF, { 0x9C, 0x18, 0x92, 0xEA, 0xDE, 0xD7, 0x20, 0x85 } };
const GUID IID_IUnknown = { 0x00000000, 0x0000, 0x0000, { 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 } };
struct DRI3Present
{
/* COM vtable */
void *vtable;
/* IUnknown reference count */
LONG refs;
D3DPRESENT_PARAMETERS params;
PRESENTpriv *present_priv;
#if D3DADAPTER9_WITHDRI2
struct DRI2priv *dri2_priv;
#endif
SDL_Window* sdl_win;
Display* x11_display;
Window x11_window;
};
struct D3DWindowBuffer
{
PRESENTPixmapPriv *present_pixmap_priv;
};
/* DRI3Present_vtable are passed to nine which expects WINAPI functions */
static ULONG WINAPI
DRI3Present_AddRef( struct DRI3Present *This )
{
ULONG refs = InterlockedIncrement(&This->refs);
TRACE("%p increasing refcount to %u.\n", This, refs);
return refs;
}
static ULONG WINAPI
DRI3Present_Release( struct DRI3Present *This )
{
ULONG refs = InterlockedDecrement(&This->refs);
TRACE("%p decreasing refcount to %u.\n", This, refs);
if (refs == 0) {
/* dtor */
SDL_SetWindowFullscreen(This->sdl_win, FALSE);
PRESENTDestroy(This->x11_display, This->present_priv);
#if D3DADAPTER9_WITHDRI2
if (is_dri2_fallback)
DRI2FallbackDestroy(This->dri2_priv);
#endif
free(This);
}
return refs;
}
static HRESULT WINAPI
DRI3Present_QueryInterface( struct DRI3Present *This,
REFIID riid,
void **ppvObject )
{
if (!ppvObject) { return E_POINTER; }
if (IsEqualGUID(&IID_ID3DPresent, riid) ||
IsEqualGUID(&IID_IUnknown, riid)) {
*ppvObject = This;
DRI3Present_AddRef(This);
return S_OK;
}
WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
*ppvObject = NULL;
return E_NOINTERFACE;
}
static void
DRI3Present_ChangePresentParameters( struct DRI3Present *This,
D3DPRESENT_PARAMETERS *params,
BOOL first_time);
static HRESULT WINAPI
DRI3Present_SetPresentParameters( struct DRI3Present *This,
D3DPRESENT_PARAMETERS *pPresentationParameters,
D3DDISPLAYMODEEX *pFullscreenDisplayMode )
{
if (!pPresentationParameters) {
WARN("pPresentationParameters is NULL.\n");
return D3DERR_INVALIDCALL;
}
if (pPresentationParameters->Windowed) {
SDL_SetWindowFullscreen(This->sdl_win, FALSE);
DRI3Present_ChangePresentParameters(This, pPresentationParameters, FALSE);
}
else {
SDL_DisplayMode* mode = NULL;
SDL_DisplayMode curr;
int err;
if (!pFullscreenDisplayMode) {
if (SDL_GetCurrentDisplayMode(0, &curr) < 0) {
WARN("SDL_GetCurrentDisplayMode failed");
return D3DERR_INVALIDCALL;
}
mode = &curr;
goto current;
}
SDL_DisplayMode target;
SDL_DisplayMode closest;
memset(&target, 0, sizeof(target));
memset(&closest, 0, sizeof(closest));
// msdn: "When switching to full-screen mode,
// Direct3D will try to find a desktop format that matches the back buffer format,
// so that back buffer and front buffer formats will be identical (to eliminate the need for color conversion)."
Uint32 preferred_format_for_backbuffer = ConvertToSDL(pPresentationParameters->BackBufferFormat);
target.w = pFullscreenDisplayMode->Height;
target.h = pFullscreenDisplayMode->Width;
target.refresh_rate = pFullscreenDisplayMode->RefreshRate;
if (preferred_format_for_backbuffer != SDL_PIXELFORMAT_UNKNOWN)
target.format = preferred_format_for_backbuffer;
else
target.format = ConvertToSDL(pFullscreenDisplayMode->Format);
if (FALSE) {
/*
* this doesn't seem to be a good idea:
* it returns different mode even when the request mode exits and works...
*/
int Adapter = 0;
mode = SDL_GetClosestDisplayMode(Adapter, &target, &closest);
if (!mode) {
WARN("Could not find requested fullscreen display mode (%dx%d %dHz, format = %d).\n", pFullscreenDisplayMode->Width, pFullscreenDisplayMode->Height, pFullscreenDisplayMode->RefreshRate, pFullscreenDisplayMode->Format);
}
}
else {
mode = ⌖
}
current:
err = SDL_SetWindowDisplayMode(This->sdl_win, mode);
if (err < 0) {
WARN("SDL_SetWindowDisplayMode returned an error: %s\n", SDL_GetError());
return D3DERR_INVALIDCALL;
}
err = SDL_SetWindowFullscreen(This->sdl_win, SDL_WINDOW_FULLSCREEN);
if (err < 0) {
WARN("SDL_SetWindowFullscreen returned an error: %s\n", SDL_GetError());
return D3DERR_INVALIDCALL;
}
DRI3Present_ChangePresentParameters(This, pPresentationParameters, FALSE);
}
return D3D_OK;
}
static HRESULT WINAPI
DRI3Present_D3DWindowBufferFromDmaBuf( struct DRI3Present *This,
int dmaBufFd,
int width,
int height,
int stride,
int depth,
int bpp,
struct D3DWindowBuffer **out)
{
Pixmap pixmap;
#if D3DADAPTER9_WITHDRI2
if (is_dri2_fallback) {
*out = calloc(1, sizeof(struct D3DWindowBuffer));
DRI2FallbackPRESENTPixmap(This->present_priv, This->dri2_priv,
dmaBufFd, width, height, stride, depth,
bpp,
&((*out)->present_pixmap_priv));
return D3D_OK;
}
#endif
if (!DRI3PixmapFromDmaBuf(This->x11_display, DefaultScreen(This->x11_display),
dmaBufFd, width, height, stride, depth,
bpp, &pixmap ))
return D3DERR_DRIVERINTERNALERROR;
*out = calloc(1, sizeof(struct D3DWindowBuffer));
PRESENTPixmapInit(This->present_priv, pixmap, &((*out)->present_pixmap_priv));
return D3D_OK;
}
static HRESULT WINAPI
DRI3Present_DestroyD3DWindowBuffer( struct DRI3Present *This,
struct D3DWindowBuffer *buffer )
{
/* the pixmap is managed by the PRESENT backend.
* But if it can delete it right away, we may have
* better performance */
PRESENTTryFreePixmap(This->x11_display, buffer->present_pixmap_priv);
free(buffer);
return D3D_OK;
}
static HRESULT WINAPI
DRI3Present_WaitBufferReleased( struct DRI3Present *This,
struct D3DWindowBuffer *buffer)
{
(void) This;
PRESENTWaitPixmapReleased(buffer->present_pixmap_priv);
return D3D_OK;
}
static HRESULT WINAPI
DRI3Present_FrontBufferCopy( struct DRI3Present *This,
struct D3DWindowBuffer *buffer )
{
#if D3DADAPTER9_WITHDRI2
if (is_dri2_fallback)
return D3DERR_DRIVERINTERNALERROR;
#endif
/* TODO: use dc_rect */
if (PRESENTHelperCopyFront(This->x11_display, buffer->present_pixmap_priv))
return D3D_OK;
else
return D3DERR_DRIVERINTERNALERROR;
}
static HRESULT WINAPI
DRI3Present_PresentBuffer( struct DRI3Present *This,
struct D3DWindowBuffer *buffer,
HWND hWndOverride,
const RECT *pSourceRect,
const RECT *pDestRect,
const RGNDATA *pDirtyRegion,
DWORD Flags )
{
/* TODO?
if (d3d->dc_rect.top != 0 &&
d3d->dc_rect.left != 0) {
if (!pDestRect)
pDestRect = (const RECT *) &(d3d->dc_rect);
else {
dest_translate.top = pDestRect->top + d3d->dc_rect.top;
dest_translate.left = pDestRect->left + d3d->dc_rect.left;
dest_translate.bottom = pDestRect->bottom + d3d->dc_rect.bottom;
dest_translate.right = pDestRect->right + d3d->dc_rect.right;
pDestRect = (const RECT *) &dest_translate;
}
}
*/
if (!PRESENTPixmap(This->x11_display, This->x11_window, buffer->present_pixmap_priv,
&This->params, pSourceRect, pDestRect, pDirtyRegion))
return D3DERR_DRIVERINTERNALERROR;
return D3D_OK;
}
static HRESULT WINAPI
DRI3Present_GetRasterStatus( struct DRI3Present *This,
D3DRASTER_STATUS *pRasterStatus )
{
FIXME("(%p, %p), stub!\n", This, pRasterStatus);
return D3DERR_INVALIDCALL;
}
static HRESULT WINAPI
DRI3Present_GetDisplayMode( struct DRI3Present *This,
D3DDISPLAYMODEEX *pMode,
D3DDISPLAYROTATION *pRotation )
{
int Adapter = 0;
SDL_DisplayMode mode;
int err = SDL_GetDesktopDisplayMode(Adapter, &mode);
if (err < 0) {
WARN("SDL_GetCurrentDisplayMode returned an error: %s\n", SDL_GetError());
return D3DERR_INVALIDCALL;
}
pMode->Width = mode.w;
pMode->Height = mode.h;
pMode->RefreshRate = mode.refresh_rate;
pMode->Format = ConvertFromSDL(mode.format);
pMode->ScanLineOrdering = D3DSCANLINEORDERING_PROGRESSIVE;
*pRotation = D3DDISPLAYROTATION_IDENTITY;
return D3D_OK;
}
static HRESULT WINAPI
DRI3Present_GetPresentStats( struct DRI3Present *This,
D3DPRESENTSTATS *pStats )
{
FIXME("(%p, %p), stub!\n", This, pStats);
return D3DERR_INVALIDCALL;
}
static HRESULT WINAPI
DRI3Present_GetCursorPos( struct DRI3Present *This,
POINT *pPoint )
{
// FIXME faked
BOOL ok;
if (!pPoint)
return D3DERR_INVALIDCALL;
ok = TRUE;
memset(pPoint,0,sizeof(POINT));
return ok ? S_OK : D3DERR_DRIVERINTERNALERROR;
}
static HRESULT WINAPI
DRI3Present_SetCursorPos( struct DRI3Present *This,
POINT *pPoint )
{
// FIXME faked
if (!pPoint)
return D3DERR_INVALIDCALL;
return S_OK;
}
static HRESULT WINAPI
DRI3Present_SetCursor( struct DRI3Present *This,
void *pBitmap,
POINT *pHotspot,
BOOL bShow )
{
// FIXME faked
return D3D_OK;
}
static HRESULT WINAPI
DRI3Present_SetGammaRamp( struct DRI3Present *This,
const D3DGAMMARAMP *pRamp,
HWND hWndOverride )
{
// FIXME faked
return D3D_OK;
}
static HRESULT WINAPI
DRI3Present_GetWindowInfo( struct DRI3Present *This,
HWND hWnd,
int *width, int *height, int *depth )
{
int w,h;
SDL_GetWindowSize(This->sdl_win, &w, &h);
Uint32 format = SDL_GetWindowPixelFormat(This->sdl_win);
*width = w;
*height = h;
*depth = format != SDL_PIXELFORMAT_UNKNOWN ? SDL_BITSPERPIXEL(format) : 24;
return D3D_OK;
}
/*----------*/
static ID3DPresentVtbl DRI3Present_vtable = {
(void *)DRI3Present_QueryInterface,
(void *)DRI3Present_AddRef,
(void *)DRI3Present_Release,
(void *)DRI3Present_SetPresentParameters,
(void *)DRI3Present_D3DWindowBufferFromDmaBuf,
(void *)DRI3Present_DestroyD3DWindowBuffer,
(void *)DRI3Present_WaitBufferReleased,
(void *)DRI3Present_FrontBufferCopy,
(void *)DRI3Present_PresentBuffer,
(void *)DRI3Present_GetRasterStatus,
(void *)DRI3Present_GetDisplayMode,
(void *)DRI3Present_GetPresentStats,
(void *)DRI3Present_GetCursorPos,
(void *)DRI3Present_SetCursorPos,
(void *)DRI3Present_SetCursor,
(void *)DRI3Present_SetGammaRamp,
(void *)DRI3Present_GetWindowInfo
};
static void
DRI3Present_ChangePresentParameters( struct DRI3Present *This,
D3DPRESENT_PARAMETERS *params,
BOOL first_time)
{
(void) first_time; /* will be used to manage screen res if windowed mode change */
if (params->hDeviceWindow && params->hDeviceWindow != This->sdl_win) {
WARN("Changing hDeviceWindow not supported\n");
}
int w,h;
SDL_GetWindowSize(This->sdl_win, &w, &h);
params->BackBufferWidth = w;
params->BackBufferHeight = h;
This->params = *params;
}
static HRESULT
DRI3Present_new( SDL_Window* sdl_win,
D3DPRESENT_PARAMETERS *params,
struct DRI3Present **out )
{
struct DRI3Present *This;
if (!sdl_win) {
ERR("No SDL_Window specified for presentation backend.\n");
return D3DERR_INVALIDCALL;
}
SDL_SysWMinfo info;
SDL_VERSION(&info.version);
SDL_bool Ok = SDL_GetWindowWMInfo(sdl_win, &info);
if (!Ok) {
ERR("Invalid SDL_Window specified for presentation backend.\n");
return D3DERR_INVALIDCALL;
}
This = calloc(1, sizeof(struct DRI3Present));
if (!This) {
return E_OUTOFMEMORY;
}
This->vtable = &DRI3Present_vtable;
This->refs = 1;
This->sdl_win = sdl_win;
This->x11_display = info.info.x11.display;
This->x11_window = info.info.x11.window;
DRI3Present_ChangePresentParameters(This, params, TRUE);
PRESENTInit(info.info.x11.display, &(This->present_priv));
#if D3DADAPTER9_WITHDRI2
if (is_dri2_fallback)
DRI2FallbackInit(info.info.x11.display, &(This->dri2_priv));
#endif
*out = This;
return D3D_OK;
}
struct DRI3PresentGroup
{
/* COM vtable */
void *vtable;
/* IUnknown reference count */
LONG refs;
struct DRI3Present **present_backends;
unsigned npresent_backends;
};
/* DRI3PresentGroup_vtable functions are passed to nine and must be WINAPI */
static ULONG WINAPI
DRI3PresentGroup_AddRef( struct DRI3PresentGroup *This )
{
ULONG refs = InterlockedIncrement(&This->refs);
TRACE("%p increasing refcount to %u.\n", This, refs);
return refs;
}
static ULONG WINAPI
DRI3PresentGroup_Release( struct DRI3PresentGroup *This )
{
ULONG refs = InterlockedDecrement(&This->refs);
TRACE("%p decreasing refcount to %u.\n", This, refs);
if (refs == 0) {
unsigned i;
if (This->present_backends) {
for (i = 0; i < This->npresent_backends; ++i) {
if (This->present_backends[i])
DRI3Present_Release(This->present_backends[i]);
}
free(This->present_backends);
}
free(This);
}
return refs;
}
static HRESULT WINAPI
DRI3PresentGroup_QueryInterface( struct DRI3PresentGroup *This,
REFIID riid,
void **ppvObject )
{
if (!ppvObject) { return E_POINTER; }
if (IsEqualGUID(&IID_ID3DPresentGroup, riid) ||
IsEqualGUID(&IID_IUnknown, riid)) {
*ppvObject = This;
DRI3PresentGroup_AddRef(This);
return S_OK;
}
WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
*ppvObject = NULL;
return E_NOINTERFACE;
}
static UINT WINAPI
DRI3PresentGroup_GetMultiheadCount( struct DRI3PresentGroup *This )
{
FIXME("(%p), stub!\n", This);
return 1;
}
static HRESULT WINAPI
DRI3PresentGroup_GetPresent( struct DRI3PresentGroup *This,
UINT Index,
ID3DPresent **ppPresent )
{
if (Index >= DRI3PresentGroup_GetMultiheadCount(This)) {
ERR("Index >= MultiHeadCount\n");
return D3DERR_INVALIDCALL;
}
DRI3Present_AddRef(This->present_backends[Index]);
*ppPresent = (ID3DPresent *)This->present_backends[Index];
return D3D_OK;
}
static HRESULT WINAPI
DRI3PresentGroup_CreateAdditionalPresent( struct DRI3PresentGroup *This,
D3DPRESENT_PARAMETERS *pPresentationParameters,
ID3DPresent **ppPresent )
{
FIXME("(%p, %p, %p), stub!\n", This, pPresentationParameters, ppPresent);
return D3DERR_INVALIDCALL;
}
static void WINAPI
DRI3PresentGroup_GetVersion( struct DRI3PresentGroup *This,
int *major,
int *minor)
{
*major = 1;
*minor = 0;
}
static ID3DPresentGroupVtbl DRI3PresentGroup_vtable = {
(void *)DRI3PresentGroup_QueryInterface,
(void *)DRI3PresentGroup_AddRef,
(void *)DRI3PresentGroup_Release,
(void *)DRI3PresentGroup_GetMultiheadCount,
(void *)DRI3PresentGroup_GetPresent,
(void *)DRI3PresentGroup_CreateAdditionalPresent,
(void *)DRI3PresentGroup_GetVersion
};
static HRESULT
dri3_create_present_group( SDL_Window* win,
UINT adapter,
D3DPRESENT_PARAMETERS *params,
unsigned nparams,
ID3DPresentGroup **group )
{
struct DRI3PresentGroup *This = calloc(1, sizeof(struct DRI3PresentGroup));
HRESULT hr;
unsigned i;
if (!This) {
ERR("Out of memory.\n");
return E_OUTOFMEMORY;
}
This->vtable = &DRI3PresentGroup_vtable;
This->refs = 1;
This->npresent_backends = nparams;
This->present_backends = calloc(This->npresent_backends, sizeof(struct DRI3Present *));
if (!This->present_backends) {
DRI3PresentGroup_Release(This);
ERR("Out of memory.\n");
return E_OUTOFMEMORY;
}
if (nparams != 1) { adapter = 0; }
for (i = 0; i < This->npresent_backends; ++i) {
/* create an ID3DPresent for it */
hr = DRI3Present_new(win, ¶ms[i],
&This->present_backends[i]);
if (FAILED(hr)) {
DRI3PresentGroup_Release(This);
return hr;
}
}
*group = (ID3DPresentGroup *)This;
TRACE("Returning %p\n", *group);
return D3D_OK;
}
// --------- dlls/d3d9/d3dadapter9.c ----------------------------------------------------------------
struct d3dadapter9
{
/* COM vtable */
void *vtable;
void *vtable_mirror; // for WINAPI
/* IUnknown reference count */
LONG refs;
/* simple test, one adapter */
ID3DAdapter9 *adapter;
/* true if it implements IDirect3D9Ex */
BOOL ex;
};
/* convenience wrapper for calls into ID3D9Adapter */
#define ADAPTER_PROC(name, ...) \
ID3DAdapter9_##name(This->adapter, ## __VA_ARGS__)
static HRESULT
d3dadapter9_CheckDeviceFormat( struct d3dadapter9 *This,
UINT Adapter,
D3DDEVTYPE DeviceType,
D3DFORMAT AdapterFormat,
DWORD Usage,
D3DRESOURCETYPE RType,
D3DFORMAT CheckFormat );
/* d3dadapter9_vtable functions are passed to the app, not nine */
static ULONG ALT_WINAPI
d3dadapter9_AddRef( struct d3dadapter9 *This )
{
ULONG refs = InterlockedIncrement(&This->refs);
TRACE("%p increasing refcount to %u.\n", This, refs);
return refs;
}
static ULONG ALT_WINAPI
d3dadapter9_Release( struct d3dadapter9 *This )
{
ULONG refs = InterlockedDecrement(&This->refs);
TRACE("%p decreasing refcount to %u.\n", This, refs);
if (refs == 0) {
/* dtor */
if (This->adapter){
ID3DAdapter9_Release(This->adapter);
}
free(This);
}
return refs;
}
static HRESULT ALT_WINAPI
d3dadapter9_QueryInterface( struct d3dadapter9 *This,
REFIID riid,
void **ppvObject )
{
if (!ppvObject) { return E_POINTER; }
if ((IsEqualGUID(&IID_IDirect3D9Ex, riid) && This->ex) ||
IsEqualGUID(&IID_IDirect3D9, riid) ||
IsEqualGUID(&IID_IUnknown, riid)) {
*ppvObject = This;
d3dadapter9_AddRef(This);
return S_OK;
}
WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
*ppvObject = NULL;
return E_NOINTERFACE;
}
static HRESULT ALT_WINAPI
d3dadapter9_RegisterSoftwareDevice( struct d3dadapter9 *This,
void *pInitializeFunction )
{
FIXME("(%p, %p), stub!\n", This, pInitializeFunction);
return D3DERR_INVALIDCALL;
}
static UINT ALT_WINAPI
d3dadapter9_GetAdapterCount( struct d3dadapter9 *This )
{
return This ? 1 : 0;
}
static HRESULT ALT_WINAPI
d3dadapter9_GetAdapterIdentifier( struct d3dadapter9 *This,
UINT Adapter,
DWORD Flags,
D3DADAPTER_IDENTIFIER9 *pIdentifier )
{
HRESULT hr;
if (Adapter >= d3dadapter9_GetAdapterCount(This)) { return D3DERR_INVALIDCALL; }
hr = ADAPTER_PROC(GetAdapterIdentifier, Flags, pIdentifier);
return hr;
}
static UINT ALT_WINAPI
d3dadapter9_GetAdapterModeCount( struct d3dadapter9 *This,
UINT Adapter,
D3DFORMAT Format )
{
if (Adapter >= d3dadapter9_GetAdapterCount(This)) {
WARN("Adapter %u does not exist.\n", Adapter);
return 0;
}
if (FAILED(d3dadapter9_CheckDeviceFormat(This, Adapter, D3DDEVTYPE_HAL,
Format, D3DUSAGE_RENDERTARGET,
D3DRTYPE_SURFACE, Format))) {
WARN("DeviceFormat not available.\n");
return 0;
}
int NumMatchingModes = 0;
int NumModes = SDL_GetNumDisplayModes(Adapter);
int i;
for (i=0;i<NumModes;i++) {
SDL_DisplayMode mode;
int err = SDL_GetDisplayMode(Adapter, i, &mode);
if (err < 0) {
WARN("SDL_GetDisplayMode returned an error: %s\n", SDL_GetError());
return 0;
}
if (Format == ConvertFromSDL(mode.format))
NumMatchingModes ++;
}
return NumMatchingModes;
}
static HRESULT ALT_WINAPI
d3dadapter9_EnumAdapterModes( struct d3dadapter9 *This,
UINT Adapter,
D3DFORMAT Format,
UINT ModeIndex,
D3DDISPLAYMODE *pMode )
{
HRESULT hr;
if (Adapter >= d3dadapter9_GetAdapterCount(This)) {
WARN("Adapter %u does not exist.\n", Adapter);
return D3DERR_INVALIDCALL;
}
if (!pMode) {
WARN("pMode is NULL.\n");
return D3DERR_INVALIDCALL;
}
hr = d3dadapter9_CheckDeviceFormat(This, Adapter, D3DDEVTYPE_HAL,
Format, D3DUSAGE_RENDERTARGET,
D3DRTYPE_SURFACE, Format);
if (FAILED(hr)) {
TRACE("DeviceFormat not available.\n");
return hr;
}
int IndexMatchingModes = 0;
int NumModes = SDL_GetNumDisplayModes(Adapter);
int i;
for (i=0;i<NumModes;i++) {
SDL_DisplayMode mode;
int err = SDL_GetDisplayMode(Adapter, i, &mode);
if (err < 0) {
WARN("SDL_GetDisplayMode returned an error: %s\n", SDL_GetError());
return D3DERR_INVALIDCALL;
}
if (Format != ConvertFromSDL(mode.format))
continue;
if (IndexMatchingModes == ModeIndex) {
TRACE("DiplayMode: %dx%d@%dHz, d3dformat=%d\n", mode.w, mode.h, mode.refresh_rate, Format);
pMode->Width = mode.w;
pMode->Height = mode.h;
pMode->RefreshRate = mode.refresh_rate;
pMode->Format = Format;
return D3D_OK;
}
IndexMatchingModes ++;
}
WARN("invalid mode for format %d on adapter %d: %d\n", Format, Adapter, ModeIndex);
return D3DERR_INVALIDCALL;
}
static HRESULT ALT_WINAPI
d3dadapter9_GetAdapterDisplayMode( struct d3dadapter9 *This,
UINT Adapter,
D3DDISPLAYMODE *pMode )
{
if (Adapter >= d3dadapter9_GetAdapterCount(This)) {
WARN("Adapter %u does not exist.\n", Adapter);
return D3DERR_INVALIDCALL;
}
SDL_DisplayMode mode;
int err = SDL_GetCurrentDisplayMode(Adapter, &mode);
if (err < 0) {
WARN("SDL_GetCurrentDisplayMode returned an error: %s\n", SDL_GetError());
return D3DERR_INVALIDCALL;
}
pMode->Width = mode.w;
pMode->Height = mode.h;
pMode->RefreshRate = mode.refresh_rate;
pMode->Format = ConvertFromSDL(mode.format);
return D3D_OK;
}
static HRESULT ALT_WINAPI
d3dadapter9_CheckDeviceType( struct d3dadapter9 *This,
UINT Adapter,
D3DDEVTYPE DevType,
D3DFORMAT AdapterFormat,
D3DFORMAT BackBufferFormat,
BOOL bWindowed )
{
if (Adapter >= d3dadapter9_GetAdapterCount(This)) { return D3DERR_INVALIDCALL; }
return ADAPTER_PROC(CheckDeviceType,
DevType, AdapterFormat, BackBufferFormat, bWindowed);
}
static HRESULT ALT_WINAPI
d3dadapter9_CheckDeviceFormat( struct d3dadapter9 *This,
UINT Adapter,
D3DDEVTYPE DeviceType,
D3DFORMAT AdapterFormat,
DWORD Usage,
D3DRESOURCETYPE RType,
D3DFORMAT CheckFormat )
{
if (Adapter >= d3dadapter9_GetAdapterCount(This)) { return D3DERR_INVALIDCALL; }
return ADAPTER_PROC(CheckDeviceFormat,
DeviceType, AdapterFormat, Usage, RType, CheckFormat);
}
static HRESULT ALT_WINAPI
d3dadapter9_CheckDeviceMultiSampleType( struct d3dadapter9 *This,
UINT Adapter,
D3DDEVTYPE DeviceType,
D3DFORMAT SurfaceFormat,
BOOL Windowed,
D3DMULTISAMPLE_TYPE MultiSampleType,
DWORD *pQualityLevels )
{
if (Adapter >= d3dadapter9_GetAdapterCount(This)) { return D3DERR_INVALIDCALL; }
return ADAPTER_PROC(CheckDeviceMultiSampleType, DeviceType, SurfaceFormat,
Windowed, MultiSampleType, pQualityLevels);
}
static HRESULT ALT_WINAPI
d3dadapter9_CheckDepthStencilMatch( struct d3dadapter9 *This,
UINT Adapter,
D3DDEVTYPE DeviceType,
D3DFORMAT AdapterFormat,
D3DFORMAT RenderTargetFormat,
D3DFORMAT DepthStencilFormat )
{
if (Adapter >= d3dadapter9_GetAdapterCount(This)) { return D3DERR_INVALIDCALL; }
return ADAPTER_PROC(CheckDepthStencilMatch, DeviceType, AdapterFormat,
RenderTargetFormat, DepthStencilFormat);
}
static HRESULT ALT_WINAPI
d3dadapter9_CheckDeviceFormatConversion( struct d3dadapter9 *This,
UINT Adapter,
D3DDEVTYPE DeviceType,
D3DFORMAT SourceFormat,
D3DFORMAT TargetFormat )
{
if (Adapter >= d3dadapter9_GetAdapterCount(This)) { return D3DERR_INVALIDCALL; }
return ADAPTER_PROC(CheckDeviceFormatConversion,
DeviceType, SourceFormat, TargetFormat);
}
static HRESULT ALT_WINAPI
d3dadapter9_GetDeviceCaps( struct d3dadapter9 *This,
UINT Adapter,
D3DDEVTYPE DeviceType,
D3DCAPS9 *pCaps )
{
HRESULT hr;
if (Adapter >= d3dadapter9_GetAdapterCount(This)) { return D3DERR_INVALIDCALL; }
hr = ADAPTER_PROC(GetDeviceCaps, DeviceType, pCaps);
if (FAILED(hr)) { return hr; }