This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
forked from chrisbmr/wine
-
Notifications
You must be signed in to change notification settings - Fork 10
/
present.c
1838 lines (1587 loc) · 58.5 KB
/
present.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
/*
* Wine ID3DAdapter9 support functions
*
* Copyright 2013 Joakim Sindholt
* Christoph Bumiller
* Copyright 2014 Tiziano Bacocco
* David Heidelberger
* Copyright 2014-2015 Axel Davy
* Copyright 2015 Patrick Rudolph
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include "wine/port.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(d3d9nine);
#include <d3dadapter/drm.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "dri3.h"
#include "wndproc.h"
#include "wine/library.h" // for wine_dl*
#include "wine/unicode.h" // for strcpyW
#ifndef D3DPRESENT_DONOTWAIT
#define D3DPRESENT_DONOTWAIT 0x00000001
#endif
#define WINE_D3DADAPTER_DRIVER_PRESENT_VERSION_MAJOR 1
#if defined (ID3DPresent_SetPresentParameters2)
#define WINE_D3DADAPTER_DRIVER_PRESENT_VERSION_MINOR 3
#elif defined (ID3DPresent_ResolutionMismatch)
#define WINE_D3DADAPTER_DRIVER_PRESENT_VERSION_MINOR 2
#elif defined (ID3DPresent_GetWindowOccluded)
#define WINE_D3DADAPTER_DRIVER_PRESENT_VERSION_MINOR 1
#else
#define WINE_D3DADAPTER_DRIVER_PRESENT_VERSION_MINOR 0
#endif
static const struct D3DAdapter9DRM *d3d9_drm = NULL;
#ifdef D3D9NINE_DRI2
static int is_dri2_fallback = 0;
#endif
/* Start section of x11drv.h */
#define X11DRV_ESCAPE 6789
enum x11drv_escape_codes
{
X11DRV_SET_DRAWABLE, /* set current drawable for a DC */
X11DRV_GET_DRAWABLE, /* get current drawable for a DC */
X11DRV_START_EXPOSURES, /* start graphics exposures */
X11DRV_END_EXPOSURES, /* end graphics exposures */
X11DRV_FLUSH_GL_DRAWABLE /* flush changes made to the gl drawable */
};
struct x11drv_escape_get_drawable
{
enum x11drv_escape_codes code; /* escape code (X11DRV_GET_DRAWABLE) */
Drawable drawable; /* X drawable */
Drawable gl_drawable; /* GL drawable */
int pixel_format; /* internal GL pixel format */
};
/* End section x11drv.h */
static XContext d3d_hwnd_context;
static CRITICAL_SECTION context_section;
static CRITICAL_SECTION_DEBUG critsect_debug =
{
0, 0, &context_section,
{ &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
0, 0, { (DWORD_PTR)(__FILE__ ": context_section") }
};
static CRITICAL_SECTION context_section = { &critsect_debug, -1, 0, 0, 0, 0 };
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 } };
struct d3d_drawable
{
Drawable drawable; /* X11 drawable */
HDC hdc;
HWND wnd; /* HWND (for convenience) */
RECT windowRect;
POINT offset; /* offset of the client area compared to the X11 drawable */
unsigned int width;
unsigned int height;
unsigned int depth;
};
struct DRI3Present
{
/* COM vtable */
void *vtable;
/* IUnknown reference count */
LONG refs;
D3DPRESENT_PARAMETERS params;
HWND focus_wnd;
PRESENTpriv *present_priv;
#ifdef D3D9NINE_DRI2
struct DRI2priv *dri2_priv;
#endif
WCHAR devname[32];
HCURSOR hCursor;
DEVMODEW initial_mode;
DWORD style;
DWORD style_ex;
BOOL reapply_mode;
BOOL ex;
BOOL resolution_mismatch;
BOOL occluded;
BOOL drop_wnd_messages;
BOOL no_window_changes;
Display *gdi_display;
UINT present_interval;
BOOL present_async;
BOOL present_swapeffectcopy;
BOOL allow_discard_delayed_release;
BOOL tear_free_discard;
struct d3d_drawable *d3d;
};
struct D3DWindowBuffer
{
PRESENTPixmapPriv *present_pixmap_priv;
};
static void free_d3dadapter_drawable(struct d3d_drawable *d3d)
{
ReleaseDC(d3d->wnd, d3d->hdc);
HeapFree(GetProcessHeap(), 0, d3d);
}
static void destroy_d3dadapter_drawable(Display *gdi_display, HWND hwnd)
{
struct d3d_drawable *d3d;
TRACE("This=%p hwnd=%p\n", gdi_display, hwnd);
EnterCriticalSection(&context_section);
if (!XFindContext(gdi_display, (XID)hwnd,
d3d_hwnd_context, (char **)&d3d))
{
XDeleteContext(gdi_display, (XID)hwnd, d3d_hwnd_context);
free_d3dadapter_drawable(d3d);
}
LeaveCriticalSection(&context_section);
}
static void DRI3Present_FillOffset(Display *gdi_display, struct d3d_drawable *d3d)
{
struct x11drv_escape_get_drawable extesc = { X11DRV_GET_DRAWABLE };
HWND desktop;
Drawable wineRoot;
POINT relWineRootPos;
POINT relXRootPos;
Drawable drawable;
HDC hdc;
Window Wroot;
int x, y;
unsigned int border_width;
TRACE("hwnd=%p\n", d3d->wnd);
/* Finding the offset is hard because a drawable
* doesn't always start a the top left of a hwnd window,
* for example if the windows window decoration is replaced
* by the window managed.
* In the case of non-virtual desktop, wine root is
* the X root.
* In the case of virtual desktop, We assume the root drawable
* begins at pos (0, 0) */
/* Note: Another method that works for virtual desktop,
* but fails else because of the window manager replacing
* decoration issue stated above:
* RECT wnd;
* GetWindowRect(hwnd, &wnd);
* MapWindowPoints(HWND_DESKTOP, hwnd, (LPPOINT) &wnd, 2);
* relWineRootPos.x = -wnd.left;
* relWineRootPos.y = -wnd.top;
*/
d3d->offset.x = d3d->offset.y = 0;
relWineRootPos.x = relWineRootPos.y = 0;
relXRootPos.x = relXRootPos.y = 0;
desktop = GetDesktopWindow();
hdc = GetDCEx(desktop, 0, DCX_CACHE | DCX_CLIPSIBLINGS);
if (!hdc)
return;
if (ExtEscape(hdc, X11DRV_ESCAPE, sizeof(extesc), (LPCSTR)&extesc,
sizeof(extesc), (LPSTR)&extesc) <= 0)
{
ERR("Unexpected error in X Drawable lookup (hwnd=%p, hdc=%p)\n", desktop, hdc);
ReleaseDC(desktop, hdc);
return;
}
ReleaseDC(desktop, hdc);
wineRoot = extesc.drawable;
if (!XGetGeometry(gdi_display, d3d->drawable, &Wroot, &x, &y, &d3d->width, &d3d->height, &border_width, &d3d->depth))
{
d3d->width = 0;
d3d->height = 0;
d3d->depth= 0;
}
/* The position of the top left client area
* compared to wine root window */
ClientToScreen(d3d->wnd, &relWineRootPos);
TRACE("Coord client area: %d %d\n", relWineRootPos.x, relWineRootPos.y);
/* Now we compute the position of the drawable
* compared to wine root window */
drawable = d3d->drawable;
while (1) {
Window Wparent, *Wchildren;
unsigned int numchildren, width, height, depth;
if (!XGetGeometry(gdi_display, drawable, &Wroot, &x, &y, &width, &height, &border_width, &depth))
break;
/* Should we really add border_width ? */
relXRootPos.x += x + border_width;
relXRootPos.y += y + border_width;
if (!XQueryTree(gdi_display, drawable, &Wroot, &Wparent, &Wchildren, &numchildren))
break;
if (Wchildren)
free(Wchildren);
drawable = Wparent;
if (drawable == Wroot || drawable == wineRoot)
{
TRACE("Successfully determined drawable pos (debug: %ld, %ld, %ld)\n", drawable, Wroot, wineRoot);
break;
}
}
TRACE("Coord drawable: %d %d\n", relXRootPos.x, relXRootPos.y);
d3d->offset.x = relWineRootPos.x - relXRootPos.x;
d3d->offset.y = relWineRootPos.y - relXRootPos.y;
TRACE("Offset: %d %d\n", d3d->offset.x, d3d->offset.y);
}
static struct d3d_drawable *create_d3dadapter_drawable(Display *gdi_display, HWND hwnd)
{
struct x11drv_escape_get_drawable extesc = { X11DRV_GET_DRAWABLE };
struct d3d_drawable *d3d;
TRACE("hwnd=%p\n", hwnd);
d3d = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d));
if (!d3d)
{
ERR("Couldn't allocate d3d_drawable.\n");
return NULL;
}
d3d->hdc = GetDCEx(hwnd, 0, DCX_CACHE | DCX_CLIPSIBLINGS);
if (ExtEscape(d3d->hdc, X11DRV_ESCAPE, sizeof(extesc), (LPCSTR)&extesc,
sizeof(extesc), (LPSTR)&extesc) <= 0)
{
ERR("Unexpected error in X Drawable lookup (hwnd=%p, hdc=%p)\n", hwnd, d3d->hdc);
ReleaseDC(hwnd, d3d->hdc);
HeapFree(GetProcessHeap(), 0, d3d);
return NULL;
}
TRACE("hwnd created drawable: %ld\n", extesc.drawable);
d3d->drawable = extesc.drawable;
d3d->wnd = hwnd;
GetWindowRect(hwnd, &d3d->windowRect);
DRI3Present_FillOffset(gdi_display, d3d);
return d3d;
}
static struct d3d_drawable *get_d3d_drawable(Display *gdi_display, HWND hwnd)
{
struct d3d_drawable *d3d, *race;
TRACE("hwnd=%p\n", hwnd);
EnterCriticalSection(&context_section);
if (!XFindContext(gdi_display, (XID)hwnd, d3d_hwnd_context, (char **)&d3d))
{
return d3d;
}
LeaveCriticalSection(&context_section);
TRACE("No d3d_drawable attached to hwnd %p, creating one.\n", hwnd);
d3d = create_d3dadapter_drawable(gdi_display, hwnd);
if (!d3d)
return NULL;
EnterCriticalSection(&context_section);
if (!XFindContext(gdi_display, (XID)hwnd,
d3d_hwnd_context, (char **)&race))
{
/* apparently someone beat us to creating this d3d drawable. Let's not
waste more time with X11 calls and just use theirs instead. */
free_d3dadapter_drawable(d3d);
return race;
}
XSaveContext(gdi_display, (XID)hwnd, d3d_hwnd_context, (char *)d3d);
return d3d;
}
static void release_d3d_drawable(struct d3d_drawable *d3d)
{
if (!d3d)
ERR("Driver internal error: d3d_drawable is NULL\n");
LeaveCriticalSection(&context_section);
}
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 */
(void) nine_unregister_window(This->focus_wnd);
if (This->d3d)
destroy_d3dadapter_drawable(This->gdi_display, This->d3d->wnd);
ChangeDisplaySettingsExW(This->devname, &(This->initial_mode), 0, CDS_FULLSCREEN, NULL);
PRESENTDestroy(This->gdi_display, This->present_priv);
#ifdef D3D9NINE_DRI2
if (is_dri2_fallback)
DRI2FallbackDestroy(This->dri2_priv);
#endif
HeapFree(GetProcessHeap(), 0, 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 HRESULT DRI3Present_ChangePresentParameters(struct DRI3Present *This,
D3DPRESENT_PARAMETERS *params);
static HRESULT WINAPI DRI3Present_SetPresentParameters(struct DRI3Present *This,
D3DPRESENT_PARAMETERS *pPresentationParameters,
D3DDISPLAYMODEEX *pFullscreenDisplayMode)
{
if (pFullscreenDisplayMode)
FIXME("Ignoring pFullscreenDisplayMode\n");
return DRI3Present_ChangePresentParameters(This, pPresentationParameters);
}
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;
#ifdef D3D9NINE_DRI2
if (is_dri2_fallback)
{
*out = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
sizeof(struct D3DWindowBuffer));
if (!DRI2FallbackPRESENTPixmap(This->present_priv, This->dri2_priv,
dmaBufFd, width, height, stride, depth,
bpp, &((*out)->present_pixmap_priv)))
{
ERR("DRI2FallbackPRESENTPixmap failed\n");
HeapFree(GetProcessHeap(), 0, *out);
return D3DERR_DRIVERINTERNALERROR;
}
return D3D_OK;
}
#endif
if (!DRI3PixmapFromDmaBuf(This->gdi_display, DefaultScreen(This->gdi_display),
dmaBufFd, width, height, stride, depth, bpp, &pixmap))
{
ERR("DRI3PixmapFromDmaBuf failed\n");
return D3DERR_DRIVERINTERNALERROR;
}
*out = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
sizeof(struct D3DWindowBuffer));
if (!PRESENTPixmapInit(This->present_priv, pixmap, &((*out)->present_pixmap_priv)))
{
ERR("PRESENTPixmapInit failed\n");
HeapFree(GetProcessHeap(), 0, *out);
return D3DERR_DRIVERINTERNALERROR;
}
TRACE("This=%p buffer=%p\n", This, *out);
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 */
TRACE("This=%p buffer=%p of priv %p\n", This, buffer, buffer->present_pixmap_priv);
PRESENTTryFreePixmap(This->gdi_display, buffer->present_pixmap_priv);
HeapFree(GetProcessHeap(), 0, buffer);
return D3D_OK;
}
static HRESULT WINAPI DRI3Present_WaitBufferReleased(struct DRI3Present *This,
struct D3DWindowBuffer *buffer)
{
TRACE("This=%p buffer=%p\n", This, buffer);
if(!PRESENTWaitPixmapReleased(buffer->present_pixmap_priv))
{
ERR("PRESENTWaitPixmapReleased failed\n");
return D3DERR_DRIVERINTERNALERROR;
}
return D3D_OK;
}
static HRESULT WINAPI DRI3Present_FrontBufferCopy(struct DRI3Present *This,
struct D3DWindowBuffer *buffer)
{
#ifdef D3D9NINE_DRI2
if (is_dri2_fallback)
return D3DERR_DRIVERINTERNALERROR;
#endif
if (PRESENTHelperCopyFront(This->gdi_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 )
{
struct d3d_drawable *d3d;
RECT dest_translate;
RECT windowRect;
RECT offset;
HWND hwnd;
if (hWndOverride)
hwnd = hWndOverride;
else if (This->params.hDeviceWindow)
hwnd = This->params.hDeviceWindow;
else
hwnd = This->focus_wnd;
TRACE("This=%p hwnd=%p\n", This, hwnd);
d3d = get_d3d_drawable(This->gdi_display, hwnd);
if (!d3d)
return D3DERR_DRIVERINTERNALERROR;
/* TODO: should we use a list here instead ? */
if (This->d3d && (This->d3d->wnd != d3d->wnd))
destroy_d3dadapter_drawable(This->gdi_display, This->d3d->wnd);
This->d3d = d3d;
GetWindowRect(d3d->wnd, &windowRect);
/* The "correct" way to detect offset changes
* would be to catch any window related change with a
* listener. But it is complicated and this heuristic
* is fast and should work well. */
if (windowRect.top != d3d->windowRect.top ||
windowRect.left != d3d->windowRect.left ||
windowRect.bottom != d3d->windowRect.bottom ||
windowRect.right != d3d->windowRect.right)
{
d3d->windowRect = windowRect;
DRI3Present_FillOffset(This->gdi_display, d3d);
}
GetClientRect(d3d->wnd, &offset);
offset.left += d3d->offset.x;
offset.top += d3d->offset.y;
offset.right += d3d->offset.x;
offset.bottom += d3d->offset.y;
if ((offset.top != 0) || (offset.left != 0))
{
if (!pDestRect)
pDestRect = (const RECT *) &offset;
else
{
dest_translate.top = pDestRect->top + offset.top;
dest_translate.left = pDestRect->left + offset.left;
dest_translate.bottom = pDestRect->bottom + offset.bottom;
dest_translate.right = pDestRect->right + offset.right;
pDestRect = (const RECT *) &dest_translate;
}
}
if (!PRESENTPixmap(This->gdi_display, d3d->drawable, buffer->present_pixmap_priv,
This->present_interval, This->present_async, This->present_swapeffectcopy,
pSourceRect, pDestRect, pDirtyRegion))
{
release_d3d_drawable(d3d);
TRACE("Present call failed\n");
return D3DERR_DRIVERINTERNALERROR;
}
release_d3d_drawable(d3d);
return D3D_OK;
}
/* Based on wine's wined3d_get_adapter_raster_status. */
static HRESULT WINAPI DRI3Present_GetRasterStatus( struct DRI3Present *This,
D3DRASTER_STATUS *pRasterStatus )
{
LONGLONG freq_per_frame, freq_per_line;
LARGE_INTEGER counter, freq_per_sec;
unsigned refresh_rate, height;
TRACE("This=%p, pRasterStatus=%p\n", This, pRasterStatus);
if (!QueryPerformanceCounter(&counter) || !QueryPerformanceFrequency(&freq_per_sec))
return D3DERR_INVALIDCALL;
if (This->params.Windowed)
{
refresh_rate = This->initial_mode.dmDisplayFrequency;
height = This->initial_mode.dmPelsHeight;
}
else
{
refresh_rate = This->params.FullScreen_RefreshRateInHz;
height = This->params.BackBufferHeight;
}
if (refresh_rate == 0)
refresh_rate = 60;
TRACE("refresh_rate=%u, height=%u\n", refresh_rate, height);
freq_per_frame = freq_per_sec.QuadPart / refresh_rate;
/* Assume 20 scan lines in the vertical blank. */
freq_per_line = freq_per_frame / (height + 20);
pRasterStatus->ScanLine = (counter.QuadPart % freq_per_frame) / freq_per_line;
if (pRasterStatus->ScanLine < height)
pRasterStatus->InVBlank = FALSE;
else
{
pRasterStatus->ScanLine = 0;
pRasterStatus->InVBlank = TRUE;
}
TRACE("Returning fake value, InVBlank %u, ScanLine %u.\n",
pRasterStatus->InVBlank, pRasterStatus->ScanLine);
return D3D_OK;
}
static HRESULT WINAPI DRI3Present_GetDisplayMode( struct DRI3Present *This,
D3DDISPLAYMODEEX *pMode, D3DDISPLAYROTATION *pRotation )
{
DEVMODEW dm;
ZeroMemory(&dm, sizeof(dm));
dm.dmSize = sizeof(dm);
EnumDisplaySettingsExW(This->devname, ENUM_CURRENT_SETTINGS, &dm, 0);
pMode->Width = dm.dmPelsWidth;
pMode->Height = dm.dmPelsHeight;
pMode->RefreshRate = dm.dmDisplayFrequency;
pMode->ScanLineOrdering = (dm.dmDisplayFlags & DM_INTERLACED) ?
D3DSCANLINEORDERING_INTERLACED : D3DSCANLINEORDERING_PROGRESSIVE;
/* XXX This is called "guessing" */
switch (dm.dmBitsPerPel)
{
case 32: pMode->Format = D3DFMT_X8R8G8B8; break;
case 24: pMode->Format = D3DFMT_R8G8B8; break;
case 16: pMode->Format = D3DFMT_R5G6B5; break;
default:
WARN("Unknown display format with %u bpp.\n", dm.dmBitsPerPel);
pMode->Format = D3DFMT_UNKNOWN;
}
switch (dm.dmDisplayOrientation)
{
case DMDO_DEFAULT: *pRotation = D3DDISPLAYROTATION_IDENTITY; break;
case DMDO_90: *pRotation = D3DDISPLAYROTATION_90; break;
case DMDO_180: *pRotation = D3DDISPLAYROTATION_180; break;
case DMDO_270: *pRotation = D3DDISPLAYROTATION_270; break;
default:
WARN("Unknown display rotation %u.\n", dm.dmDisplayOrientation);
*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 )
{
BOOL ok;
HWND draw_window;
if (!pPoint)
return D3DERR_INVALIDCALL;
draw_window = This->params.hDeviceWindow ?
This->params.hDeviceWindow : This->focus_wnd;
ok = GetCursorPos(pPoint);
ok = ok && ScreenToClient(draw_window, pPoint);
return ok ? S_OK : D3DERR_DRIVERINTERNALERROR;
}
static HRESULT WINAPI DRI3Present_SetCursorPos( struct DRI3Present *This, POINT *pPoint )
{
BOOL ok;
POINT real_pos;
if (!pPoint)
return D3DERR_INVALIDCALL;
ok = SetCursorPos(pPoint->x, pPoint->y);
if (!ok)
goto error;
ok = GetCursorPos(&real_pos);
if (!ok || real_pos.x != pPoint->x || real_pos.y != pPoint->y)
goto error;
return D3D_OK;
error:
SetCursor(NULL); /* Hide cursor rather than put wrong pos */
return D3DERR_DRIVERINTERNALERROR;
}
/* Note: assuming 32x32 cursor */
static HRESULT WINAPI DRI3Present_SetCursor( struct DRI3Present *This, void *pBitmap,
POINT *pHotspot, BOOL bShow )
{
if (pBitmap)
{
ICONINFO info;
HCURSOR cursor;
DWORD mask[32];
memset(mask, ~0, sizeof(mask));
if (!pHotspot)
return D3DERR_INVALIDCALL;
info.fIcon = FALSE;
info.xHotspot = pHotspot->x;
info.yHotspot = pHotspot->y;
info.hbmMask = CreateBitmap(32, 32, 1, 1, mask);
info.hbmColor = CreateBitmap(32, 32, 1, 32, pBitmap);
cursor = CreateIconIndirect(&info);
if (info.hbmMask) DeleteObject(info.hbmMask);
if (info.hbmColor) DeleteObject(info.hbmColor);
if (cursor)
DestroyCursor(This->hCursor);
This->hCursor = cursor;
}
SetCursor(bShow ? This->hCursor : NULL);
return D3D_OK;
}
static HRESULT WINAPI DRI3Present_SetGammaRamp( struct DRI3Present *This,
const D3DGAMMARAMP *pRamp, HWND hWndOverride )
{
HWND draw_window = This->params.hDeviceWindow ?
This->params.hDeviceWindow : This->focus_wnd;
HWND hWnd = hWndOverride ? hWndOverride : draw_window;
HDC hdc;
BOOL ok;
if (!pRamp)
return D3DERR_INVALIDCALL;
hdc = GetDC(hWnd);
ok = SetDeviceGammaRamp(hdc, (void *)pRamp);
ReleaseDC(hWnd, hdc);
return ok ? D3D_OK : D3DERR_DRIVERINTERNALERROR;
}
static HRESULT WINAPI DRI3Present_GetWindowInfo( struct DRI3Present *This,
HWND hWnd, int *width, int *height, int *depth )
{
HWND draw_window = This->params.hDeviceWindow ?
This->params.hDeviceWindow : This->focus_wnd;
HRESULT hr;
RECT pRect;
TRACE("This=%p hwnd=%p\n", This, hWnd);
/* For fullscreen modes, use the dimensions of the X11 window instead of
* the game window. This is for compability with Valve's "fullscreen hack",
* which won't switch to the game's resolution anymore, but instead scales
* the game window to the root window. Only then can page flipping be used.
*/
if (!This->params.Windowed && This->d3d)
if (This->d3d->width > 0 && This->d3d->height > 0 && This->d3d->depth > 0)
{
*width = This->d3d->width;
*height = This->d3d->height;
*depth = This->d3d->depth;
return D3D_OK;
}
if (!hWnd)
hWnd = draw_window;
hr = GetClientRect(hWnd, &pRect);
if (!hr)
return D3DERR_INVALIDCALL;
TRACE("pRect: %d %d %d %d\n", pRect.left, pRect.top, pRect.right, pRect.bottom);
*width = pRect.right - pRect.left;
*height = pRect.bottom - pRect.top;
*depth = 24; //TODO
return D3D_OK;
}
#if WINE_D3DADAPTER_DRIVER_PRESENT_VERSION_MINOR >= 1
static BOOL WINAPI DRI3Present_GetWindowOccluded(struct DRI3Present *This)
{
return This->occluded;
}
#endif
#if WINE_D3DADAPTER_DRIVER_PRESENT_VERSION_MINOR >= 2
static BOOL WINAPI DRI3Present_ResolutionMismatch(struct DRI3Present *This)
{
/* The resolution might change due to a third party app.
* Poll this function to get the device's resolution match.
* A device reset is required to restore the requested resolution.
*/
return This->resolution_mismatch;
}
static HANDLE WINAPI DRI3Present_CreateThread( struct DRI3Present *This,
void *pThreadfunc, void *pParam )
{
LPTHREAD_START_ROUTINE lpStartAddress =
(LPTHREAD_START_ROUTINE) pThreadfunc;
return CreateThread(NULL, 0, lpStartAddress, pParam, 0, NULL);
}
static BOOL WINAPI DRI3Present_WaitForThread( struct DRI3Present *This, HANDLE thread )
{
DWORD ExitCode = 0;
while (GetExitCodeThread(thread, &ExitCode) && ExitCode == STILL_ACTIVE)
Sleep(10);
return TRUE;
}
#endif
#if WINE_D3DADAPTER_DRIVER_PRESENT_VERSION_MINOR >= 3
static HRESULT WINAPI DRI3Present_SetPresentParameters2( struct DRI3Present *This, D3DPRESENT_PARAMETERS2 *pParams )
{
This->allow_discard_delayed_release = pParams->AllowDISCARDDelayedRelease;
This->tear_free_discard = pParams->AllowDISCARDDelayedRelease && pParams->TearFreeDISCARD;
return D3D_OK;
}
static BOOL WINAPI DRI3Present_IsBufferReleased( struct DRI3Present *This, struct D3DWindowBuffer *buffer )
{
TRACE("This=%p buffer=%p\n", This, buffer);
return PRESENTIsPixmapReleased(buffer->present_pixmap_priv);
}
static HRESULT WINAPI DRI3Present_WaitBufferReleaseEvent( struct DRI3Present *This )
{
PRESENTWaitReleaseEvent(This->present_priv);
return D3D_OK;
}
#endif
/*----------*/
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,
#if WINE_D3DADAPTER_DRIVER_PRESENT_VERSION_MINOR >= 1
(void *)DRI3Present_GetWindowOccluded,
#endif
#if WINE_D3DADAPTER_DRIVER_PRESENT_VERSION_MINOR >= 2
(void *)DRI3Present_ResolutionMismatch,
(void *)DRI3Present_CreateThread,
(void *)DRI3Present_WaitForThread,
#endif
#if WINE_D3DADAPTER_DRIVER_PRESENT_VERSION_MINOR >= 3
(void *)DRI3Present_SetPresentParameters2,
(void *)DRI3Present_IsBufferReleased,
(void *)DRI3Present_WaitBufferReleaseEvent,
#endif
};
/* The following code is based on WINE's wined3d/device.c and
* wined3d/swapchain.c and WINE's d3d9 files. */
static LONG fullscreen_style(LONG style)
{
/* Make sure the window is managed, otherwise we won't get keyboard input. */
style |= WS_POPUP | WS_SYSMENU;
style &= ~(WS_CAPTION | WS_THICKFRAME);
return style;
}
static LONG fullscreen_exstyle(LONG exstyle)
{
/* Filter out window decorations. */
exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
return exstyle;
}
static HRESULT DRI3Present_ChangeDisplaySettingsIfNeccessary(struct DRI3Present *This, DEVMODEW *new_mode)
{
DEVMODEW current_mode;
LONG hr;
/* Filter invalid resolution */
if (!new_mode->dmPelsWidth || !new_mode->dmPelsHeight)
return D3DERR_INVALIDCALL;
/* Ignore invalid frequency requested */
if (new_mode->dmDisplayFrequency > 1000)
new_mode->dmDisplayFrequency = 0;
ZeroMemory(¤t_mode, sizeof(DEVMODEW));
current_mode.dmSize = sizeof(DEVMODEW);
/* Only change the mode if necessary. */
if (!EnumDisplaySettingsW(This->devname, ENUM_CURRENT_SETTINGS, ¤t_mode))
ERR("Failed to get current display mode.\n");
else if (current_mode.dmPelsWidth != new_mode->dmPelsWidth
|| current_mode.dmPelsHeight != new_mode->dmPelsHeight
|| (current_mode.dmDisplayFrequency != new_mode->dmDisplayFrequency
&& (new_mode->dmFields & DM_DISPLAYFREQUENCY)))
{
hr = ChangeDisplaySettingsExW(This->devname, new_mode, 0, CDS_FULLSCREEN, NULL);
if (hr != DISP_CHANGE_SUCCESSFUL)
{
/* try again without display RefreshRate */
if (new_mode->dmFields & DM_DISPLAYFREQUENCY)
{
new_mode->dmFields &= ~DM_DISPLAYFREQUENCY;
new_mode->dmDisplayFrequency = 0;
hr = ChangeDisplaySettingsExW(This->devname, new_mode, 0, CDS_FULLSCREEN, NULL);
if (hr != DISP_CHANGE_SUCCESSFUL)
{
ERR("ChangeDisplaySettingsExW failed with 0x%08X\n", hr);
return D3DERR_INVALIDCALL;
}
}
else
{
ERR("ChangeDisplaySettingsExW failed with 0x%08X\n", hr);
return D3DERR_INVALIDCALL;
}
}
}
return D3D_OK;
}
LRESULT device_process_message(struct DRI3Present *present, HWND window, BOOL unicode,
UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
{
boolean drop_wnd_messages;
DEVMODEW current_mode;
DEVMODEW new_mode;
TRACE("Got message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
window, message, wparam, lparam);
if (present->drop_wnd_messages && message != WM_DISPLAYCHANGE)
{
TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
window, message, wparam, lparam);
if (unicode)
return DefWindowProcW(window, message, wparam, lparam);
else
return DefWindowProcA(window, message, wparam, lparam);
}
if (message == WM_DESTROY)
{
TRACE("unregister window %p.\n", window);
(void) nine_unregister_window(window);
}
else if (message == WM_DISPLAYCHANGE)
{
/* Ex restores display mode, while non Ex requires the
* user to call Device::Reset() */
ZeroMemory(¤t_mode, sizeof(DEVMODEW));
current_mode.dmSize = sizeof(current_mode);
if (!present->ex &&
!present->params.Windowed &&
present->params.hDeviceWindow &&
EnumDisplaySettingsW(present->devname, ENUM_CURRENT_SETTINGS, ¤t_mode) &&
(current_mode.dmPelsWidth != present->params.BackBufferWidth ||
current_mode.dmPelsHeight != present->params.BackBufferHeight))
{
present->resolution_mismatch = TRUE;
}
else
{
present->resolution_mismatch = FALSE;
}
}
else if (message == WM_ACTIVATEAPP)
{
drop_wnd_messages = present->drop_wnd_messages;
present->drop_wnd_messages = TRUE;
if (wparam == WA_INACTIVE)
{
present->occluded = TRUE;
present->reapply_mode = TRUE;
ZeroMemory(&new_mode, sizeof(DEVMODEW));
new_mode.dmSize = sizeof(new_mode);
if (EnumDisplaySettingsW(present->devname, ENUM_REGISTRY_SETTINGS, &new_mode))
DRI3Present_ChangeDisplaySettingsIfNeccessary(present, &new_mode);
if (!present->no_window_changes &&
IsWindowVisible(present->params.hDeviceWindow))
ShowWindow(present->params.hDeviceWindow, SW_MINIMIZE);
}
else
{
present->occluded = FALSE;
if (!present->no_window_changes)
{