[D3D8][D3D9][DDRAW][WINED3D] Sync with Wine Staging 2.9. This work couldn't have...
[reactos.git] / reactos / dll / directx / wine / ddraw / ddraw.c
1 /*
2 * Copyright 1997-2000 Marcus Meissner
3 * Copyright 1998-2000 Lionel Ulmer
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2006 Stefan Dösinger
6 * Copyright 2008 Denver Gingerich
7 * Copyright 2007-2008, 2011, 2013 Stefan Dösinger for CodeWeavers
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24 #include "ddraw_private.h"
25
26 #include <wine/exception.h>
27
28 static const struct ddraw *exclusive_ddraw;
29 static HWND exclusive_window;
30
31 /* Device identifier. Don't relay it to WineD3D */
32 static const DDDEVICEIDENTIFIER2 deviceidentifier =
33 {
34 "vga.dll", /* default 2D driver */
35 "DirectDraw HAL",
36 { { 0x00010001, 0x00010001 } },
37 0, 0, 0, 0,
38 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
39 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
40 0
41 };
42
43 static struct enum_device_entry
44 {
45 char interface_name[100];
46 char device_name[100];
47 const GUID *device_guid;
48 DWORD remove_caps;
49 } device_list7[] =
50 {
51 /* T&L HAL device */
52 {
53 "WINE Direct3D7 Hardware Transform and Lighting acceleration using WineD3D",
54 "Wine D3D7 T&L HAL",
55 &IID_IDirect3DTnLHalDevice,
56 0,
57 },
58
59 /* HAL device */
60 {
61 "WINE Direct3D7 Hardware acceleration using WineD3D",
62 "Direct3D HAL",
63 &IID_IDirect3DHALDevice,
64 0,
65 },
66
67 /* RGB device */
68 {
69 "WINE Direct3D7 RGB Software Emulation using WineD3D",
70 "Wine D3D7 RGB",
71 &IID_IDirect3DRGBDevice,
72 D3DDEVCAPS_HWTRANSFORMANDLIGHT,
73 },
74 };
75
76 static void STDMETHODCALLTYPE ddraw_null_wined3d_object_destroyed(void *parent) {}
77
78 const struct wined3d_parent_ops ddraw_null_wined3d_parent_ops =
79 {
80 ddraw_null_wined3d_object_destroyed,
81 };
82
83 static inline struct ddraw *impl_from_IDirectDraw(IDirectDraw *iface)
84 {
85 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw_iface);
86 }
87
88 static inline struct ddraw *impl_from_IDirectDraw2(IDirectDraw2 *iface)
89 {
90 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw2_iface);
91 }
92
93 static inline struct ddraw *impl_from_IDirectDraw4(IDirectDraw4 *iface)
94 {
95 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw4_iface);
96 }
97
98 static inline struct ddraw *impl_from_IDirectDraw7(IDirectDraw7 *iface)
99 {
100 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw7_iface);
101 }
102
103 static inline struct ddraw *impl_from_IDirect3D(IDirect3D *iface)
104 {
105 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D_iface);
106 }
107
108 static inline struct ddraw *impl_from_IDirect3D2(IDirect3D2 *iface)
109 {
110 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D2_iface);
111 }
112
113 static inline struct ddraw *impl_from_IDirect3D3(IDirect3D3 *iface)
114 {
115 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D3_iface);
116 }
117
118 static inline struct ddraw *impl_from_IDirect3D7(IDirect3D7 *iface)
119 {
120 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D7_iface);
121 }
122
123 static HRESULT WINAPI ddraw7_QueryInterface(IDirectDraw7 *iface, REFIID riid, void **out)
124 {
125 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
126
127 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
128
129 if (!riid)
130 {
131 *out = NULL;
132 return DDERR_INVALIDPARAMS;
133 }
134
135 /* The refcount unit test revealed that an IDirect3D7 interface can only
136 * be queried from a DirectDraw object that was created as an IDirectDraw7
137 * interface. The older interfaces can query any IDirect3D version except
138 * 7, because they are all initially created as IDirectDraw. This isn't
139 * really crucial behavior, and messy to implement with the common
140 * creation function, so it has been left out here. */
141 if (IsEqualGUID(&IID_IDirectDraw7, riid)
142 || IsEqualGUID(&IID_IUnknown, riid))
143 {
144 *out = &ddraw->IDirectDraw7_iface;
145 TRACE("Returning IDirectDraw7 interface %p.\n", *out);
146 }
147 else if (IsEqualGUID(&IID_IDirectDraw4, riid))
148 {
149 *out = &ddraw->IDirectDraw4_iface;
150 TRACE("Returning IDirectDraw4 interface %p.\n", *out);
151 }
152 else if (IsEqualGUID(&IID_IDirectDraw2, riid))
153 {
154 *out = &ddraw->IDirectDraw2_iface;
155 TRACE("Returning IDirectDraw2 interface %p.\n", *out);
156 }
157 else if (IsEqualGUID(&IID_IDirectDraw, riid))
158 {
159 *out = &ddraw->IDirectDraw_iface;
160 TRACE("Returning IDirectDraw interface %p.\n", *out);
161 }
162 else if (IsEqualGUID(&IID_IDirect3D7, riid))
163 {
164 ddraw->d3dversion = 7;
165 *out = &ddraw->IDirect3D7_iface;
166 TRACE("Returning Direct3D7 interface %p.\n", *out);
167 }
168 else if (IsEqualGUID(&IID_IDirect3D3, riid))
169 {
170 ddraw->d3dversion = 3;
171 *out = &ddraw->IDirect3D3_iface;
172 TRACE("Returning Direct3D3 interface %p.\n", *out);
173 }
174 else if (IsEqualGUID(&IID_IDirect3D2, riid))
175 {
176 ddraw->d3dversion = 2;
177 *out = &ddraw->IDirect3D2_iface;
178 TRACE("Returning Direct3D2 interface %p.\n", *out);
179 }
180 else if (IsEqualGUID(&IID_IDirect3D, riid))
181 {
182 ddraw->d3dversion = 1;
183 *out = &ddraw->IDirect3D_iface;
184 TRACE("Returning Direct3D interface %p.\n", *out);
185 }
186 /* Unknown interface */
187 else
188 {
189 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
190 *out = NULL;
191 return E_NOINTERFACE;
192 }
193
194 IUnknown_AddRef((IUnknown *)*out);
195 return S_OK;
196 }
197
198 static HRESULT WINAPI ddraw4_QueryInterface(IDirectDraw4 *iface, REFIID riid, void **object)
199 {
200 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
201
202 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
203
204 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
205 }
206
207 static HRESULT WINAPI ddraw2_QueryInterface(IDirectDraw2 *iface, REFIID riid, void **object)
208 {
209 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
210
211 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
212
213 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
214 }
215
216 static HRESULT WINAPI ddraw1_QueryInterface(IDirectDraw *iface, REFIID riid, void **object)
217 {
218 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
219
220 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
221
222 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
223 }
224
225 static HRESULT WINAPI d3d7_QueryInterface(IDirect3D7 *iface, REFIID riid, void **object)
226 {
227 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
228
229 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
230
231 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
232 }
233
234 static HRESULT WINAPI d3d3_QueryInterface(IDirect3D3 *iface, REFIID riid, void **object)
235 {
236 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
237
238 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
239
240 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
241 }
242
243 static HRESULT WINAPI d3d2_QueryInterface(IDirect3D2 *iface, REFIID riid, void **object)
244 {
245 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
246
247 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
248
249 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
250 }
251
252 static HRESULT WINAPI d3d1_QueryInterface(IDirect3D *iface, REFIID riid, void **object)
253 {
254 struct ddraw *ddraw = impl_from_IDirect3D(iface);
255
256 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
257
258 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
259 }
260
261 /*****************************************************************************
262 * IDirectDraw7::AddRef
263 *
264 * Increases the interfaces refcount, basically
265 *
266 * DDraw refcounting is a bit tricky. The different DirectDraw interface
267 * versions have individual refcounts, but the IDirect3D interfaces do not.
268 * All interfaces are from one object, that means calling QueryInterface on an
269 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
270 * ddraw object.
271 *
272 * That means all AddRef and Release implementations of IDirectDrawX work
273 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
274 * except of IDirect3D7 which thunks to IDirectDraw7
275 *
276 * Returns: The new refcount
277 *
278 *****************************************************************************/
279 static ULONG WINAPI ddraw7_AddRef(IDirectDraw7 *iface)
280 {
281 struct ddraw *This = impl_from_IDirectDraw7(iface);
282 ULONG ref = InterlockedIncrement(&This->ref7);
283
284 TRACE("%p increasing refcount to %u.\n", This, ref);
285
286 if(ref == 1) InterlockedIncrement(&This->numIfaces);
287
288 return ref;
289 }
290
291 static ULONG WINAPI ddraw4_AddRef(IDirectDraw4 *iface)
292 {
293 struct ddraw *This = impl_from_IDirectDraw4(iface);
294 ULONG ref = InterlockedIncrement(&This->ref4);
295
296 TRACE("%p increasing refcount to %u.\n", This, ref);
297
298 if (ref == 1) InterlockedIncrement(&This->numIfaces);
299
300 return ref;
301 }
302
303 static ULONG WINAPI ddraw2_AddRef(IDirectDraw2 *iface)
304 {
305 struct ddraw *This = impl_from_IDirectDraw2(iface);
306 ULONG ref = InterlockedIncrement(&This->ref2);
307
308 TRACE("%p increasing refcount to %u.\n", This, ref);
309
310 if (ref == 1) InterlockedIncrement(&This->numIfaces);
311
312 return ref;
313 }
314
315 static ULONG WINAPI ddraw1_AddRef(IDirectDraw *iface)
316 {
317 struct ddraw *This = impl_from_IDirectDraw(iface);
318 ULONG ref = InterlockedIncrement(&This->ref1);
319
320 TRACE("%p increasing refcount to %u.\n", This, ref);
321
322 if (ref == 1) InterlockedIncrement(&This->numIfaces);
323
324 return ref;
325 }
326
327 static ULONG WINAPI d3d7_AddRef(IDirect3D7 *iface)
328 {
329 struct ddraw *This = impl_from_IDirect3D7(iface);
330
331 TRACE("iface %p.\n", iface);
332
333 return ddraw7_AddRef(&This->IDirectDraw7_iface);
334 }
335
336 static ULONG WINAPI d3d3_AddRef(IDirect3D3 *iface)
337 {
338 struct ddraw *This = impl_from_IDirect3D3(iface);
339
340 TRACE("iface %p.\n", iface);
341
342 return ddraw1_AddRef(&This->IDirectDraw_iface);
343 }
344
345 static ULONG WINAPI d3d2_AddRef(IDirect3D2 *iface)
346 {
347 struct ddraw *This = impl_from_IDirect3D2(iface);
348
349 TRACE("iface %p.\n", iface);
350
351 return ddraw1_AddRef(&This->IDirectDraw_iface);
352 }
353
354 static ULONG WINAPI d3d1_AddRef(IDirect3D *iface)
355 {
356 struct ddraw *This = impl_from_IDirect3D(iface);
357
358 TRACE("iface %p.\n", iface);
359
360 return ddraw1_AddRef(&This->IDirectDraw_iface);
361 }
362
363 static void ddraw_destroy_swapchain(struct ddraw *ddraw)
364 {
365 TRACE("Destroying the swapchain.\n");
366
367 wined3d_swapchain_decref(ddraw->wined3d_swapchain);
368 ddraw->wined3d_swapchain = NULL;
369
370 if (!(ddraw->flags & DDRAW_NO3D))
371 {
372 UINT i;
373
374 for (i = 0; i < ddraw->numConvertedDecls; ++i)
375 {
376 wined3d_vertex_declaration_decref(ddraw->decls[i].decl);
377 }
378 HeapFree(GetProcessHeap(), 0, ddraw->decls);
379 ddraw->numConvertedDecls = 0;
380
381 if (FAILED(wined3d_device_uninit_3d(ddraw->wined3d_device)))
382 {
383 ERR("Failed to uninit 3D.\n");
384 }
385 else
386 {
387 /* Free the d3d window if one was created. */
388 if (ddraw->d3d_window && ddraw->d3d_window != ddraw->dest_window)
389 {
390 TRACE("Destroying the hidden render window %p.\n", ddraw->d3d_window);
391 DestroyWindow(ddraw->d3d_window);
392 ddraw->d3d_window = 0;
393 }
394 }
395
396 ddraw->flags &= ~DDRAW_D3D_INITIALIZED;
397 }
398 else
399 {
400 wined3d_device_uninit_gdi(ddraw->wined3d_device);
401 }
402
403 ddraw_set_swapchain_window(ddraw, NULL);
404
405 TRACE("Swapchain destroyed.\n");
406 }
407
408 /*****************************************************************************
409 * ddraw_destroy
410 *
411 * Destroys a ddraw object if all refcounts are 0. This is to share code
412 * between the IDirectDrawX::Release functions
413 *
414 * Params:
415 * This: DirectDraw object to destroy
416 *
417 *****************************************************************************/
418 static void ddraw_destroy(struct ddraw *This)
419 {
420 IDirectDraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, NULL, DDSCL_NORMAL);
421 IDirectDraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
422
423 /* Destroy the device window if we created one */
424 if(This->devicewindow != 0)
425 {
426 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
427 DestroyWindow(This->devicewindow);
428 This->devicewindow = 0;
429 }
430
431 wined3d_mutex_lock();
432 list_remove(&This->ddraw_list_entry);
433 wined3d_mutex_unlock();
434
435 if (This->wined3d_swapchain)
436 ddraw_destroy_swapchain(This);
437 wined3d_device_decref(This->wined3d_device);
438 wined3d_decref(This->wined3d);
439
440 if (This->d3ddevice)
441 This->d3ddevice->ddraw = NULL;
442
443 /* Now free the object */
444 HeapFree(GetProcessHeap(), 0, This);
445 }
446
447 /*****************************************************************************
448 * IDirectDraw7::Release
449 *
450 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
451 *
452 * Returns: The new refcount
453 *****************************************************************************/
454 static ULONG WINAPI ddraw7_Release(IDirectDraw7 *iface)
455 {
456 struct ddraw *This = impl_from_IDirectDraw7(iface);
457 ULONG ref = InterlockedDecrement(&This->ref7);
458
459 TRACE("%p decreasing refcount to %u.\n", This, ref);
460
461 if (!ref && !InterlockedDecrement(&This->numIfaces))
462 ddraw_destroy(This);
463
464 return ref;
465 }
466
467 static ULONG WINAPI ddraw4_Release(IDirectDraw4 *iface)
468 {
469 struct ddraw *This = impl_from_IDirectDraw4(iface);
470 ULONG ref = InterlockedDecrement(&This->ref4);
471
472 TRACE("%p decreasing refcount to %u.\n", This, ref);
473
474 if (!ref && !InterlockedDecrement(&This->numIfaces))
475 ddraw_destroy(This);
476
477 return ref;
478 }
479
480 static ULONG WINAPI ddraw2_Release(IDirectDraw2 *iface)
481 {
482 struct ddraw *This = impl_from_IDirectDraw2(iface);
483 ULONG ref = InterlockedDecrement(&This->ref2);
484
485 TRACE("%p decreasing refcount to %u.\n", This, ref);
486
487 if (!ref && !InterlockedDecrement(&This->numIfaces))
488 ddraw_destroy(This);
489
490 return ref;
491 }
492
493 static ULONG WINAPI ddraw1_Release(IDirectDraw *iface)
494 {
495 struct ddraw *This = impl_from_IDirectDraw(iface);
496 ULONG ref = InterlockedDecrement(&This->ref1);
497
498 TRACE("%p decreasing refcount to %u.\n", This, ref);
499
500 if (!ref && !InterlockedDecrement(&This->numIfaces))
501 ddraw_destroy(This);
502
503 return ref;
504 }
505
506 static ULONG WINAPI d3d7_Release(IDirect3D7 *iface)
507 {
508 struct ddraw *This = impl_from_IDirect3D7(iface);
509
510 TRACE("iface %p.\n", iface);
511
512 return ddraw7_Release(&This->IDirectDraw7_iface);
513 }
514
515 static ULONG WINAPI d3d3_Release(IDirect3D3 *iface)
516 {
517 struct ddraw *This = impl_from_IDirect3D3(iface);
518
519 TRACE("iface %p.\n", iface);
520
521 return ddraw1_Release(&This->IDirectDraw_iface);
522 }
523
524 static ULONG WINAPI d3d2_Release(IDirect3D2 *iface)
525 {
526 struct ddraw *This = impl_from_IDirect3D2(iface);
527
528 TRACE("iface %p.\n", iface);
529
530 return ddraw1_Release(&This->IDirectDraw_iface);
531 }
532
533 static ULONG WINAPI d3d1_Release(IDirect3D *iface)
534 {
535 struct ddraw *This = impl_from_IDirect3D(iface);
536
537 TRACE("iface %p.\n", iface);
538
539 return ddraw1_Release(&This->IDirectDraw_iface);
540 }
541
542 /*****************************************************************************
543 * IDirectDraw methods
544 *****************************************************************************/
545
546 static HRESULT ddraw_set_focus_window(struct ddraw *ddraw, HWND window)
547 {
548 /* FIXME: This looks wrong, exclusive mode should imply a destination
549 * window. */
550 if ((ddraw->cooperative_level & DDSCL_EXCLUSIVE) && ddraw->dest_window)
551 {
552 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET.\n");
553 return DDERR_HWNDALREADYSET;
554 }
555
556 ddraw->focuswindow = window;
557
558 return DD_OK;
559 }
560
561 static HRESULT ddraw_attach_d3d_device(struct ddraw *ddraw,
562 struct wined3d_swapchain_desc *swapchain_desc)
563 {
564 HWND window = swapchain_desc->device_window;
565 HRESULT hr;
566
567 TRACE("ddraw %p.\n", ddraw);
568
569 if (!window || window == GetDesktopWindow())
570 {
571 window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "Hidden D3D Window",
572 WS_DISABLED, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
573 NULL, NULL, NULL, NULL);
574 if (!window)
575 {
576 ERR("Failed to create window, last error %#x.\n", GetLastError());
577 return E_FAIL;
578 }
579
580 ShowWindow(window, SW_HIDE); /* Just to be sure */
581 WARN("No window for the Direct3DDevice, created hidden window %p.\n", window);
582
583 swapchain_desc->device_window = window;
584 }
585 else
586 {
587 TRACE("Using existing window %p for Direct3D rendering.\n", window);
588 }
589 ddraw->d3d_window = window;
590
591 /* Set this NOW, otherwise creating the depth stencil surface will cause a
592 * recursive loop until ram or emulated video memory is full. */
593 ddraw->flags |= DDRAW_D3D_INITIALIZED;
594 hr = wined3d_device_init_3d(ddraw->wined3d_device, swapchain_desc);
595 if (FAILED(hr))
596 {
597 ddraw->flags &= ~DDRAW_D3D_INITIALIZED;
598 return hr;
599 }
600
601 ddraw->declArraySize = 2;
602 ddraw->decls = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw->decls) * ddraw->declArraySize);
603 if (!ddraw->decls)
604 {
605 ERR("Error allocating an array for the converted vertex decls.\n");
606 ddraw->declArraySize = 0;
607 hr = wined3d_device_uninit_3d(ddraw->wined3d_device);
608 return E_OUTOFMEMORY;
609 }
610
611 TRACE("Successfully initialized 3D.\n");
612
613 return DD_OK;
614 }
615
616 static HRESULT ddraw_create_swapchain(struct ddraw *ddraw, HWND window, BOOL windowed)
617 {
618 struct wined3d_swapchain_desc swapchain_desc;
619 struct wined3d_display_mode mode;
620 HRESULT hr = WINED3D_OK;
621
622 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
623 {
624 ERR("Failed to get display mode.\n");
625 return hr;
626 }
627
628 memset(&swapchain_desc, 0, sizeof(swapchain_desc));
629 swapchain_desc.backbuffer_width = mode.width;
630 swapchain_desc.backbuffer_height = mode.height;
631 swapchain_desc.backbuffer_format = mode.format_id;
632 swapchain_desc.swap_effect = WINED3D_SWAP_EFFECT_COPY;
633 swapchain_desc.device_window = window;
634 swapchain_desc.windowed = windowed;
635 swapchain_desc.flags = WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
636
637 if (!(ddraw->flags & DDRAW_NO3D))
638 hr = ddraw_attach_d3d_device(ddraw, &swapchain_desc);
639 else
640 hr = wined3d_device_init_gdi(ddraw->wined3d_device, &swapchain_desc);
641
642 if (FAILED(hr))
643 {
644 ERR("Failed to create swapchain, hr %#x.\n", hr);
645 return hr;
646 }
647
648 if (!(ddraw->wined3d_swapchain = wined3d_device_get_swapchain(ddraw->wined3d_device, 0)))
649 {
650 ERR("Failed to get swapchain.\n");
651 return DDERR_INVALIDPARAMS;
652 }
653
654 wined3d_swapchain_incref(ddraw->wined3d_swapchain);
655 ddraw_set_swapchain_window(ddraw, window);
656
657 if (ddraw->primary && ddraw->primary->palette)
658 wined3d_swapchain_set_palette(ddraw->wined3d_swapchain, ddraw->primary->palette->wined3d_palette);
659
660 return DD_OK;
661 }
662
663 /*****************************************************************************
664 * IDirectDraw7::RestoreDisplayMode
665 *
666 * Restores the display mode to what it was at creation time. Basically.
667 *
668 * Returns
669 * DD_OK on success
670 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
671 *
672 *****************************************************************************/
673 static HRESULT WINAPI ddraw7_RestoreDisplayMode(IDirectDraw7 *iface)
674 {
675 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
676 HRESULT hr;
677
678 TRACE("iface %p.\n", iface);
679
680 wined3d_mutex_lock();
681
682 if (!(ddraw->flags & DDRAW_RESTORE_MODE))
683 {
684 wined3d_mutex_unlock();
685 return DD_OK;
686 }
687
688 if (exclusive_ddraw && exclusive_ddraw != ddraw)
689 {
690 wined3d_mutex_unlock();
691 return DDERR_NOEXCLUSIVEMODE;
692 }
693
694 if (SUCCEEDED(hr = wined3d_set_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, NULL)))
695 ddraw->flags &= ~DDRAW_RESTORE_MODE;
696
697 InterlockedCompareExchange(&ddraw->device_state, DDRAW_DEVICE_STATE_NOT_RESTORED, DDRAW_DEVICE_STATE_OK);
698
699 wined3d_mutex_unlock();
700
701 return hr;
702 }
703
704 static HRESULT WINAPI ddraw4_RestoreDisplayMode(IDirectDraw4 *iface)
705 {
706 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
707
708 TRACE("iface %p.\n", iface);
709
710 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
711 }
712
713 static HRESULT WINAPI ddraw2_RestoreDisplayMode(IDirectDraw2 *iface)
714 {
715 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
716
717 TRACE("iface %p.\n", iface);
718
719 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
720 }
721
722 static HRESULT WINAPI ddraw1_RestoreDisplayMode(IDirectDraw *iface)
723 {
724 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
725
726 TRACE("iface %p.\n", iface);
727
728 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
729 }
730
731 /*****************************************************************************
732 * IDirectDraw7::SetCooperativeLevel
733 *
734 * Sets the cooperative level for the DirectDraw object, and the window
735 * assigned to it. The cooperative level determines the general behavior
736 * of the DirectDraw application
737 *
738 * Warning: This is quite tricky, as it's not really documented which
739 * cooperative levels can be combined with each other. If a game fails
740 * after this function, try to check the cooperative levels passed on
741 * Windows, and if it returns something different.
742 *
743 * If you think that this function caused the failure because it writes a
744 * fixme, be sure to run again with a +ddraw trace.
745 *
746 * What is known about cooperative levels (See the ddraw modes test):
747 * DDSCL_EXCLUSIVE requires DDSCL_FULLSCREEN.
748 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE.
749 * Unlike what msdn claims, DDSCL_NORMAL | DDSCL_FULLSCREEN is allowed.
750 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
751 * DDSCL_EXCLUSIVE can be activated.
752 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES or
753 * DDSCL_CREATEDEVICEWINDOW.
754 *
755 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
756 * DDSCL_CREATEDEVICEWINDOW, DDSCL_SETDEVICEWINDOW
757 * DDSCL_SETFOCUSWINDOW (partially),
758 * DDSCL_MULTITHREADED (work in progress)
759 * DDSCL_FPUPRESERVE (see device.c)
760 *
761 * Unsure about this: DDSCL_FPUSETUP
762 *
763 * These don't seem very important for wine:
764 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
765 *
766 * Returns:
767 * DD_OK if the cooperative level was set successfully
768 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
769 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
770 * (Probably others too, have to investigate)
771 *
772 *****************************************************************************/
773 static HRESULT ddraw_set_cooperative_level(struct ddraw *ddraw, HWND window,
774 DWORD cooplevel, BOOL restore_mode_on_normal)
775 {
776 struct wined3d_rendertarget_view *rtv = NULL, *dsv = NULL;
777 struct wined3d_stateblock *stateblock;
778 BOOL restore_state = FALSE;
779 HRESULT hr;
780
781 TRACE("ddraw %p, window %p, flags %#x, restore_mode_on_normal %x.\n", ddraw, window, cooplevel,
782 restore_mode_on_normal);
783 DDRAW_dump_cooperativelevel(cooplevel);
784
785 wined3d_mutex_lock();
786
787 if (ddraw->flags & DDRAW_SCL_RECURSIVE)
788 {
789 WARN("Recursive call, returning DD_OK.\n");
790 hr = DD_OK;
791 goto done;
792 }
793 ddraw->flags |= DDRAW_SCL_RECURSIVE;
794
795 /* Tests suggest that we need one of them: */
796 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
797 DDSCL_NORMAL |
798 DDSCL_EXCLUSIVE )))
799 {
800 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
801 hr = DDERR_INVALIDPARAMS;
802 goto done;
803 }
804
805 if ((cooplevel & DDSCL_CREATEDEVICEWINDOW) && !(cooplevel & DDSCL_EXCLUSIVE))
806 {
807 WARN("DDSCL_CREATEDEVICEWINDOW requires DDSCL_EXCLUSIVE.\n");
808 hr = DDERR_INVALIDPARAMS;
809 goto done;
810 }
811
812 /* Handle those levels first which set various hwnds */
813 if ((cooplevel & DDSCL_SETFOCUSWINDOW) && !(cooplevel & DDSCL_CREATEDEVICEWINDOW))
814 {
815 /* This isn't compatible with a lot of flags */
816 if (cooplevel & (DDSCL_MULTITHREADED
817 | DDSCL_FPUSETUP
818 | DDSCL_FPUPRESERVE
819 | DDSCL_ALLOWREBOOT
820 | DDSCL_ALLOWMODEX
821 | DDSCL_SETDEVICEWINDOW
822 | DDSCL_NORMAL
823 | DDSCL_EXCLUSIVE
824 | DDSCL_FULLSCREEN))
825 {
826 WARN("Called with incompatible flags, returning DDERR_INVALIDPARAMS.\n");
827 hr = DDERR_INVALIDPARAMS;
828 goto done;
829 }
830
831 hr = ddraw_set_focus_window(ddraw, window);
832 goto done;
833 }
834
835 if (cooplevel & DDSCL_EXCLUSIVE)
836 {
837 if (!(cooplevel & DDSCL_FULLSCREEN) || !(window || (cooplevel & DDSCL_CREATEDEVICEWINDOW)))
838 {
839 WARN("DDSCL_EXCLUSIVE requires DDSCL_FULLSCREEN and a window.\n");
840 hr = DDERR_INVALIDPARAMS;
841 goto done;
842 }
843
844 if (cooplevel & DDSCL_CREATEDEVICEWINDOW)
845 {
846 HWND device_window;
847
848 if (!ddraw->focuswindow && !(cooplevel & DDSCL_SETFOCUSWINDOW))
849 {
850 WARN("No focus window set.\n");
851 hr = DDERR_NOFOCUSWINDOW;
852 goto done;
853 }
854
855 device_window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "DirectDrawDeviceWnd",
856 WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
857 NULL, NULL, NULL, NULL);
858 if (!device_window)
859 {
860 ERR("Failed to create window, last error %#x.\n", GetLastError());
861 hr = E_FAIL;
862 goto done;
863 }
864
865 ShowWindow(device_window, SW_SHOW);
866 TRACE("Created a device window %p.\n", device_window);
867
868 /* Native apparently leaks the created device window if setting the
869 * focus window below fails. */
870 ddraw->cooperative_level |= DDSCL_CREATEDEVICEWINDOW;
871 ddraw->devicewindow = device_window;
872
873 if (cooplevel & DDSCL_SETFOCUSWINDOW)
874 {
875 if (!window)
876 {
877 hr = DDERR_NOHWND;
878 goto done;
879 }
880
881 if (FAILED(hr = ddraw_set_focus_window(ddraw, window)))
882 goto done;
883 }
884
885 window = device_window;
886 }
887 }
888 else
889 {
890 if (ddraw->cooperative_level & DDSCL_CREATEDEVICEWINDOW)
891 DestroyWindow(ddraw->devicewindow);
892 ddraw->devicewindow = NULL;
893 ddraw->focuswindow = NULL;
894 }
895
896 if ((cooplevel & DDSCL_FULLSCREEN) != (ddraw->cooperative_level & DDSCL_FULLSCREEN) || window != ddraw->dest_window)
897 {
898 if (ddraw->cooperative_level & DDSCL_FULLSCREEN)
899 wined3d_device_restore_fullscreen_window(ddraw->wined3d_device, ddraw->dest_window, NULL);
900
901 if (cooplevel & DDSCL_FULLSCREEN)
902 {
903 struct wined3d_display_mode display_mode;
904
905 wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &display_mode, NULL);
906 wined3d_device_setup_fullscreen_window(ddraw->wined3d_device, window,
907 display_mode.width, display_mode.height);
908 }
909 }
910
911 if ((cooplevel & DDSCL_EXCLUSIVE) && exclusive_window != window)
912 {
913 ddraw->device_state = DDRAW_DEVICE_STATE_NOT_RESTORED;
914 exclusive_window = window;
915 }
916
917 if (cooplevel & DDSCL_MULTITHREADED && !(ddraw->cooperative_level & DDSCL_MULTITHREADED))
918 wined3d_device_set_multithreaded(ddraw->wined3d_device);
919
920 if (ddraw->wined3d_swapchain)
921 {
922 if (!(ddraw->flags & DDRAW_NO3D))
923 {
924 restore_state = TRUE;
925
926 if (FAILED(hr = wined3d_stateblock_create(ddraw->wined3d_device, WINED3D_SBT_ALL, &stateblock)))
927 {
928 ERR("Failed to create stateblock, hr %#x.\n", hr);
929 goto done;
930 }
931
932 wined3d_stateblock_capture(stateblock);
933 rtv = wined3d_device_get_rendertarget_view(ddraw->wined3d_device, 0);
934 /* Rendering to ddraw->wined3d_frontbuffer. */
935 if (rtv && !wined3d_rendertarget_view_get_sub_resource_parent(rtv))
936 rtv = NULL;
937 else if (rtv)
938 wined3d_rendertarget_view_incref(rtv);
939
940 if ((dsv = wined3d_device_get_depth_stencil_view(ddraw->wined3d_device)))
941 wined3d_rendertarget_view_incref(dsv);
942 }
943
944 ddraw_destroy_swapchain(ddraw);
945 }
946
947 if (FAILED(hr = ddraw_create_swapchain(ddraw, window, !(cooplevel & DDSCL_FULLSCREEN))))
948 ERR("Failed to create swapchain, hr %#x.\n", hr);
949
950 if (restore_state)
951 {
952 if (dsv)
953 {
954 wined3d_device_set_depth_stencil_view(ddraw->wined3d_device, dsv);
955 wined3d_rendertarget_view_decref(dsv);
956 }
957
958 if (rtv)
959 {
960 wined3d_device_set_rendertarget_view(ddraw->wined3d_device, 0, rtv, FALSE);
961 wined3d_rendertarget_view_decref(rtv);
962 }
963
964 wined3d_stateblock_apply(stateblock);
965 wined3d_stateblock_decref(stateblock);
966 }
967
968 if (!(cooplevel & DDSCL_EXCLUSIVE) && (ddraw->cooperative_level & DDSCL_EXCLUSIVE)
969 && restore_mode_on_normal)
970 {
971 hr = ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
972 if (FAILED(hr))
973 ERR("RestoreDisplayMode failed\n");
974 }
975
976 if ((ddraw->cooperative_level & DDSCL_EXCLUSIVE)
977 && (window != ddraw->dest_window || !(cooplevel & DDSCL_EXCLUSIVE)))
978 wined3d_device_release_focus_window(ddraw->wined3d_device);
979
980 if ((cooplevel & DDSCL_EXCLUSIVE)
981 && (window != ddraw->dest_window || !(ddraw->cooperative_level & DDSCL_EXCLUSIVE)))
982 {
983 hr = wined3d_device_acquire_focus_window(ddraw->wined3d_device, window);
984 if (FAILED(hr))
985 {
986 ERR("Failed to acquire focus window, hr %#x.\n", hr);
987 goto done;
988 }
989 }
990
991 /* Unhandled flags */
992 if (cooplevel & DDSCL_ALLOWREBOOT)
993 WARN("Unhandled flag DDSCL_ALLOWREBOOT, harmless\n");
994 if (cooplevel & DDSCL_ALLOWMODEX)
995 WARN("Unhandled flag DDSCL_ALLOWMODEX, harmless\n");
996 if (cooplevel & DDSCL_FPUSETUP)
997 WARN("Unhandled flag DDSCL_FPUSETUP, harmless\n");
998
999 if (cooplevel & DDSCL_EXCLUSIVE)
1000 exclusive_ddraw = ddraw;
1001 else if (exclusive_ddraw == ddraw)
1002 exclusive_ddraw = NULL;
1003
1004 /* Store the cooperative_level */
1005 ddraw->cooperative_level = cooplevel;
1006 ddraw->dest_window = window;
1007
1008 TRACE("SetCooperativeLevel returning DD_OK\n");
1009 hr = DD_OK;
1010 done:
1011 ddraw->flags &= ~DDRAW_SCL_RECURSIVE;
1012 wined3d_mutex_unlock();
1013
1014 return hr;
1015 }
1016
1017 static HRESULT WINAPI ddraw7_SetCooperativeLevel(IDirectDraw7 *iface, HWND window, DWORD flags)
1018 {
1019 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1020
1021 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1022
1023 return ddraw_set_cooperative_level(ddraw, window, flags, !(ddraw->flags & DDRAW_SCL_DDRAW1));
1024 }
1025
1026 static HRESULT WINAPI ddraw4_SetCooperativeLevel(IDirectDraw4 *iface, HWND window, DWORD flags)
1027 {
1028 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1029
1030 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1031
1032 return ddraw_set_cooperative_level(ddraw, window, flags, !(ddraw->flags & DDRAW_SCL_DDRAW1));
1033 }
1034
1035 static HRESULT WINAPI ddraw2_SetCooperativeLevel(IDirectDraw2 *iface, HWND window, DWORD flags)
1036 {
1037 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1038
1039 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1040
1041 return ddraw_set_cooperative_level(ddraw, window, flags, !(ddraw->flags & DDRAW_SCL_DDRAW1));
1042 }
1043
1044 static HRESULT WINAPI ddraw1_SetCooperativeLevel(IDirectDraw *iface, HWND window, DWORD flags)
1045 {
1046 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1047 HRESULT hr;
1048
1049 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1050
1051 hr = ddraw_set_cooperative_level(ddraw, window, flags, FALSE);
1052 if (SUCCEEDED(hr))
1053 ddraw->flags |= DDRAW_SCL_DDRAW1;
1054 return hr;
1055 }
1056
1057 /*****************************************************************************
1058 * IDirectDraw7::SetDisplayMode
1059 *
1060 * Sets the display screen resolution, color depth and refresh frequency
1061 * when in fullscreen mode (in theory).
1062 * Possible return values listed in the SDK suggest that this method fails
1063 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
1064 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
1065 * It seems to be valid to pass 0 for With and Height, this has to be tested
1066 * It could mean that the current video mode should be left as-is. (But why
1067 * call it then?)
1068 *
1069 * Params:
1070 * Height, Width: Screen dimension
1071 * BPP: Color depth in Bits per pixel
1072 * Refreshrate: Screen refresh rate
1073 * Flags: Other stuff
1074 *
1075 * Returns
1076 * DD_OK on success
1077 *
1078 *****************************************************************************/
1079 static HRESULT WINAPI ddraw7_SetDisplayMode(IDirectDraw7 *iface, DWORD width, DWORD height,
1080 DWORD bpp, DWORD refresh_rate, DWORD flags)
1081 {
1082 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1083 struct wined3d_display_mode mode;
1084 enum wined3d_format_id format;
1085 HRESULT hr;
1086
1087 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1088 iface, width, height, bpp, refresh_rate, flags);
1089
1090 if (force_refresh_rate != 0)
1091 {
1092 TRACE("ForceRefreshRate overriding passed-in refresh rate (%u Hz) to %u Hz\n",
1093 refresh_rate, force_refresh_rate);
1094 refresh_rate = force_refresh_rate;
1095 }
1096
1097 wined3d_mutex_lock();
1098
1099 if (exclusive_ddraw && exclusive_ddraw != ddraw)
1100 {
1101 wined3d_mutex_unlock();
1102 return DDERR_NOEXCLUSIVEMODE;
1103 }
1104
1105 if (!width || !height)
1106 {
1107 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here. */
1108 wined3d_mutex_unlock();
1109 return DD_OK;
1110 }
1111
1112 switch (bpp)
1113 {
1114 case 8: format = WINED3DFMT_P8_UINT; break;
1115 case 15: format = WINED3DFMT_B5G5R5X1_UNORM; break;
1116 case 16: format = WINED3DFMT_B5G6R5_UNORM; break;
1117 case 24: format = WINED3DFMT_B8G8R8_UNORM; break;
1118 case 32: format = WINED3DFMT_B8G8R8X8_UNORM; break;
1119 default: format = WINED3DFMT_UNKNOWN; break;
1120 }
1121
1122 mode.width = width;
1123 mode.height = height;
1124 mode.refresh_rate = refresh_rate;
1125 mode.format_id = format;
1126 mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
1127
1128 /* TODO: The possible return values from msdn suggest that the screen mode
1129 * can't be changed if a surface is locked or some drawing is in progress. */
1130 if (SUCCEEDED(hr = wined3d_set_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode)))
1131 {
1132 if (ddraw->primary)
1133 {
1134 DDSURFACEDESC2 *surface_desc = &ddraw->primary->surface_desc;
1135
1136 if (FAILED(hr = wined3d_swapchain_resize_buffers(ddraw->wined3d_swapchain, 0,
1137 surface_desc->dwWidth, surface_desc->dwHeight, mode.format_id, WINED3D_MULTISAMPLE_NONE, 0)))
1138 ERR("Failed to resize buffers, hr %#x.\n", hr);
1139 else
1140 ddrawformat_from_wined3dformat(&ddraw->primary->surface_desc.u4.ddpfPixelFormat, mode.format_id);
1141 }
1142 ddraw->flags |= DDRAW_RESTORE_MODE;
1143 }
1144
1145 InterlockedCompareExchange(&ddraw->device_state, DDRAW_DEVICE_STATE_NOT_RESTORED, DDRAW_DEVICE_STATE_OK);
1146
1147 wined3d_mutex_unlock();
1148
1149 switch (hr)
1150 {
1151 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1152 default: return hr;
1153 }
1154 }
1155
1156 static HRESULT WINAPI ddraw4_SetDisplayMode(IDirectDraw4 *iface, DWORD width, DWORD height,
1157 DWORD bpp, DWORD refresh_rate, DWORD flags)
1158 {
1159 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1160
1161 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1162 iface, width, height, bpp, refresh_rate, flags);
1163
1164 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1165 }
1166
1167 static HRESULT WINAPI ddraw2_SetDisplayMode(IDirectDraw2 *iface,
1168 DWORD width, DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
1169 {
1170 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1171
1172 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1173 iface, width, height, bpp, refresh_rate, flags);
1174
1175 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1176 }
1177
1178 static HRESULT WINAPI ddraw1_SetDisplayMode(IDirectDraw *iface, DWORD width, DWORD height, DWORD bpp)
1179 {
1180 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1181
1182 TRACE("iface %p, width %u, height %u, bpp %u.\n", iface, width, height, bpp);
1183
1184 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, 0, 0);
1185 }
1186
1187 void ddraw_d3dcaps1_from_7(D3DDEVICEDESC *caps1, D3DDEVICEDESC7 *caps7)
1188 {
1189 memset(caps1, 0, sizeof(*caps1));
1190 caps1->dwSize = sizeof(*caps1);
1191 caps1->dwFlags = D3DDD_COLORMODEL
1192 | D3DDD_DEVCAPS
1193 | D3DDD_TRANSFORMCAPS
1194 | D3DDD_BCLIPPING
1195 | D3DDD_LIGHTINGCAPS
1196 | D3DDD_LINECAPS
1197 | D3DDD_TRICAPS
1198 | D3DDD_DEVICERENDERBITDEPTH
1199 | D3DDD_DEVICEZBUFFERBITDEPTH
1200 | D3DDD_MAXBUFFERSIZE
1201 | D3DDD_MAXVERTEXCOUNT;
1202 caps1->dcmColorModel = D3DCOLOR_RGB;
1203 caps1->dwDevCaps = caps7->dwDevCaps;
1204 caps1->dtcTransformCaps.dwSize = sizeof(caps1->dtcTransformCaps);
1205 caps1->dtcTransformCaps.dwCaps = D3DTRANSFORMCAPS_CLIP;
1206 caps1->bClipping = TRUE;
1207 caps1->dlcLightingCaps.dwSize = sizeof(caps1->dlcLightingCaps);
1208 caps1->dlcLightingCaps.dwCaps = D3DLIGHTCAPS_DIRECTIONAL
1209 | D3DLIGHTCAPS_PARALLELPOINT
1210 | D3DLIGHTCAPS_POINT
1211 | D3DLIGHTCAPS_SPOT;
1212 caps1->dlcLightingCaps.dwLightingModel = D3DLIGHTINGMODEL_RGB;
1213 caps1->dlcLightingCaps.dwNumLights = caps7->dwMaxActiveLights;
1214 caps1->dpcLineCaps = caps7->dpcLineCaps;
1215 caps1->dpcTriCaps = caps7->dpcTriCaps;
1216 caps1->dwDeviceRenderBitDepth = caps7->dwDeviceRenderBitDepth;
1217 caps1->dwDeviceZBufferBitDepth = caps7->dwDeviceZBufferBitDepth;
1218 caps1->dwMaxBufferSize = 0;
1219 caps1->dwMaxVertexCount = 65536;
1220 caps1->dwMinTextureWidth = caps7->dwMinTextureWidth;
1221 caps1->dwMinTextureHeight = caps7->dwMinTextureHeight;
1222 caps1->dwMaxTextureWidth = caps7->dwMaxTextureWidth;
1223 caps1->dwMaxTextureHeight = caps7->dwMaxTextureHeight;
1224 caps1->dwMinStippleWidth = 1;
1225 caps1->dwMinStippleHeight = 1;
1226 caps1->dwMaxStippleWidth = 32;
1227 caps1->dwMaxStippleHeight = 32;
1228 caps1->dwMaxTextureRepeat = caps7->dwMaxTextureRepeat;
1229 caps1->dwMaxTextureAspectRatio = caps7->dwMaxTextureAspectRatio;
1230 caps1->dwMaxAnisotropy = caps7->dwMaxAnisotropy;
1231 caps1->dvGuardBandLeft = caps7->dvGuardBandLeft;
1232 caps1->dvGuardBandTop = caps7->dvGuardBandTop;
1233 caps1->dvGuardBandRight = caps7->dvGuardBandRight;
1234 caps1->dvGuardBandBottom = caps7->dvGuardBandBottom;
1235 caps1->dvExtentsAdjust = caps7->dvExtentsAdjust;
1236 caps1->dwStencilCaps = caps7->dwStencilCaps;
1237 caps1->dwFVFCaps = caps7->dwFVFCaps;
1238 caps1->dwTextureOpCaps = caps7->dwTextureOpCaps;
1239 caps1->wMaxTextureBlendStages = caps7->wMaxTextureBlendStages;
1240 caps1->wMaxSimultaneousTextures = caps7->wMaxSimultaneousTextures;
1241 }
1242
1243 HRESULT ddraw_get_d3dcaps(const struct ddraw *ddraw, D3DDEVICEDESC7 *caps)
1244 {
1245 WINED3DCAPS wined3d_caps;
1246 HRESULT hr;
1247
1248 TRACE("ddraw %p, caps %p.\n", ddraw, caps);
1249
1250 memset(&wined3d_caps, 0, sizeof(wined3d_caps));
1251
1252 wined3d_mutex_lock();
1253 hr = wined3d_get_device_caps(ddraw->wined3d, 0, WINED3D_DEVICE_TYPE_HAL, &wined3d_caps);
1254 wined3d_mutex_unlock();
1255 if (FAILED(hr))
1256 {
1257 WARN("Failed to get device caps, hr %#x.\n", hr);
1258 return hr;
1259 }
1260
1261 caps->dwDevCaps = wined3d_caps.DevCaps;
1262 caps->dpcLineCaps.dwMiscCaps = wined3d_caps.PrimitiveMiscCaps;
1263 caps->dpcLineCaps.dwRasterCaps = wined3d_caps.RasterCaps;
1264 caps->dpcLineCaps.dwZCmpCaps = wined3d_caps.ZCmpCaps;
1265 caps->dpcLineCaps.dwSrcBlendCaps = wined3d_caps.SrcBlendCaps;
1266 caps->dpcLineCaps.dwDestBlendCaps = wined3d_caps.DestBlendCaps;
1267 caps->dpcLineCaps.dwAlphaCmpCaps = wined3d_caps.AlphaCmpCaps;
1268 caps->dpcLineCaps.dwShadeCaps = wined3d_caps.ShadeCaps;
1269 caps->dpcLineCaps.dwTextureCaps = wined3d_caps.TextureCaps;
1270 caps->dpcLineCaps.dwTextureFilterCaps = wined3d_caps.TextureFilterCaps;
1271 caps->dpcLineCaps.dwTextureAddressCaps = wined3d_caps.TextureAddressCaps;
1272
1273 caps->dwMaxTextureWidth = wined3d_caps.MaxTextureWidth;
1274 caps->dwMaxTextureHeight = wined3d_caps.MaxTextureHeight;
1275
1276 caps->dwMaxTextureRepeat = wined3d_caps.MaxTextureRepeat;
1277 caps->dwMaxTextureAspectRatio = wined3d_caps.MaxTextureAspectRatio;
1278 caps->dwMaxAnisotropy = wined3d_caps.MaxAnisotropy;
1279 caps->dvMaxVertexW = wined3d_caps.MaxVertexW;
1280
1281 caps->dvGuardBandLeft = wined3d_caps.GuardBandLeft;
1282 caps->dvGuardBandTop = wined3d_caps.GuardBandTop;
1283 caps->dvGuardBandRight = wined3d_caps.GuardBandRight;
1284 caps->dvGuardBandBottom = wined3d_caps.GuardBandBottom;
1285
1286 caps->dvExtentsAdjust = wined3d_caps.ExtentsAdjust;
1287 caps->dwStencilCaps = wined3d_caps.StencilCaps;
1288
1289 caps->dwFVFCaps = wined3d_caps.FVFCaps;
1290 caps->dwTextureOpCaps = wined3d_caps.TextureOpCaps;
1291
1292 caps->dwVertexProcessingCaps = wined3d_caps.VertexProcessingCaps;
1293 caps->dwMaxActiveLights = wined3d_caps.MaxActiveLights;
1294
1295 /* Remove all non-d3d7 caps */
1296 caps->dwDevCaps &= (
1297 D3DDEVCAPS_FLOATTLVERTEX | D3DDEVCAPS_SORTINCREASINGZ | D3DDEVCAPS_SORTDECREASINGZ |
1298 D3DDEVCAPS_SORTEXACT | D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY |
1299 D3DDEVCAPS_TLVERTEXSYSTEMMEMORY | D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY |
1300 D3DDEVCAPS_TEXTUREVIDEOMEMORY | D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP |
1301 D3DDEVCAPS_TEXTURENONLOCALVIDMEM | D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
1302 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT | D3DDEVCAPS_CANBLTSYSTONONLOCAL |
1303 D3DDEVCAPS_HWRASTERIZATION);
1304
1305 caps->dwStencilCaps &= (
1306 D3DSTENCILCAPS_KEEP | D3DSTENCILCAPS_ZERO | D3DSTENCILCAPS_REPLACE |
1307 D3DSTENCILCAPS_INCRSAT | D3DSTENCILCAPS_DECRSAT | D3DSTENCILCAPS_INVERT |
1308 D3DSTENCILCAPS_INCR | D3DSTENCILCAPS_DECR);
1309
1310 /* FVF caps ?*/
1311
1312 caps->dwTextureOpCaps &= (
1313 D3DTEXOPCAPS_DISABLE | D3DTEXOPCAPS_SELECTARG1 | D3DTEXOPCAPS_SELECTARG2 |
1314 D3DTEXOPCAPS_MODULATE | D3DTEXOPCAPS_MODULATE2X | D3DTEXOPCAPS_MODULATE4X |
1315 D3DTEXOPCAPS_ADD | D3DTEXOPCAPS_ADDSIGNED | D3DTEXOPCAPS_ADDSIGNED2X |
1316 D3DTEXOPCAPS_SUBTRACT | D3DTEXOPCAPS_ADDSMOOTH | D3DTEXOPCAPS_BLENDTEXTUREALPHA |
1317 D3DTEXOPCAPS_BLENDFACTORALPHA | D3DTEXOPCAPS_BLENDTEXTUREALPHAPM | D3DTEXOPCAPS_BLENDCURRENTALPHA |
1318 D3DTEXOPCAPS_PREMODULATE | D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
1319 D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA | D3DTEXOPCAPS_BUMPENVMAP |
1320 D3DTEXOPCAPS_BUMPENVMAPLUMINANCE | D3DTEXOPCAPS_DOTPRODUCT3);
1321
1322 caps->dwVertexProcessingCaps &= (
1323 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_VERTEXFOG |
1324 D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER);
1325
1326 caps->dpcLineCaps.dwMiscCaps &= (
1327 D3DPMISCCAPS_MASKPLANES | D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP |
1328 D3DPMISCCAPS_CONFORMANT | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW |
1329 D3DPMISCCAPS_CULLCCW);
1330
1331 caps->dpcLineCaps.dwRasterCaps &= (
1332 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ROP2 | D3DPRASTERCAPS_XOR |
1333 D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_SUBPIXEL |
1334 D3DPRASTERCAPS_SUBPIXELX | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE |
1335 D3DPRASTERCAPS_STIPPLE | D3DPRASTERCAPS_ANTIALIASSORTDEPENDENT | D3DPRASTERCAPS_ANTIALIASSORTINDEPENDENT |
1336 D3DPRASTERCAPS_ANTIALIASEDGES | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBIAS |
1337 D3DPRASTERCAPS_ZBUFFERLESSHSR | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY |
1338 D3DPRASTERCAPS_WBUFFER | D3DPRASTERCAPS_TRANSLUCENTSORTINDEPENDENT | D3DPRASTERCAPS_WFOG |
1339 D3DPRASTERCAPS_ZFOG);
1340
1341 caps->dpcLineCaps.dwZCmpCaps &= (
1342 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
1343 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
1344 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
1345
1346 caps->dpcLineCaps.dwSrcBlendCaps &= (
1347 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
1348 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
1349 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
1350 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
1351 D3DPBLENDCAPS_BOTHINVSRCALPHA);
1352
1353 caps->dpcLineCaps.dwDestBlendCaps &= (
1354 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
1355 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
1356 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
1357 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
1358 D3DPBLENDCAPS_BOTHINVSRCALPHA);
1359
1360 caps->dpcLineCaps.dwAlphaCmpCaps &= (
1361 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
1362 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
1363 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
1364
1365 caps->dpcLineCaps.dwShadeCaps &= (
1366 D3DPSHADECAPS_COLORFLATMONO | D3DPSHADECAPS_COLORFLATRGB | D3DPSHADECAPS_COLORGOURAUDMONO |
1367 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_COLORPHONGMONO | D3DPSHADECAPS_COLORPHONGRGB |
1368 D3DPSHADECAPS_SPECULARFLATMONO | D3DPSHADECAPS_SPECULARFLATRGB | D3DPSHADECAPS_SPECULARGOURAUDMONO |
1369 D3DPSHADECAPS_SPECULARGOURAUDRGB | D3DPSHADECAPS_SPECULARPHONGMONO | D3DPSHADECAPS_SPECULARPHONGRGB |
1370 D3DPSHADECAPS_ALPHAFLATBLEND | D3DPSHADECAPS_ALPHAFLATSTIPPLED | D3DPSHADECAPS_ALPHAGOURAUDBLEND |
1371 D3DPSHADECAPS_ALPHAGOURAUDSTIPPLED | D3DPSHADECAPS_ALPHAPHONGBLEND | D3DPSHADECAPS_ALPHAPHONGSTIPPLED |
1372 D3DPSHADECAPS_FOGFLAT | D3DPSHADECAPS_FOGGOURAUD | D3DPSHADECAPS_FOGPHONG);
1373
1374 caps->dpcLineCaps.dwTextureCaps &= (
1375 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
1376 D3DPTEXTURECAPS_TRANSPARENCY | D3DPTEXTURECAPS_BORDER | D3DPTEXTURECAPS_SQUAREONLY |
1377 D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE | D3DPTEXTURECAPS_ALPHAPALETTE| D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
1378 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_COLORKEYBLEND);
1379
1380 caps->dpcLineCaps.dwTextureFilterCaps &= (
1381 D3DPTFILTERCAPS_NEAREST | D3DPTFILTERCAPS_LINEAR | D3DPTFILTERCAPS_MIPNEAREST |
1382 D3DPTFILTERCAPS_MIPLINEAR | D3DPTFILTERCAPS_LINEARMIPNEAREST | D3DPTFILTERCAPS_LINEARMIPLINEAR |
1383 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
1384 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
1385 D3DPTFILTERCAPS_MAGFLINEAR | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC |
1386 D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC);
1387
1388 caps->dpcLineCaps.dwTextureAddressCaps &= (
1389 D3DPTADDRESSCAPS_WRAP | D3DPTADDRESSCAPS_MIRROR | D3DPTADDRESSCAPS_CLAMP |
1390 D3DPTADDRESSCAPS_BORDER | D3DPTADDRESSCAPS_INDEPENDENTUV);
1391
1392 if (!(caps->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2))
1393 {
1394 /* DirectX7 always has the np2 flag set, no matter what the card
1395 * supports. Some old games (Rollcage) check the caps incorrectly.
1396 * If wined3d supports nonpow2 textures it also has np2 conditional
1397 * support. */
1398 caps->dpcLineCaps.dwTextureCaps |= D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL;
1399 }
1400
1401 /* Fill the missing members, and do some fixup */
1402 caps->dpcLineCaps.dwSize = sizeof(caps->dpcLineCaps);
1403 caps->dpcLineCaps.dwTextureBlendCaps = D3DPTBLENDCAPS_ADD
1404 | D3DPTBLENDCAPS_MODULATEMASK
1405 | D3DPTBLENDCAPS_COPY
1406 | D3DPTBLENDCAPS_DECAL
1407 | D3DPTBLENDCAPS_DECALALPHA
1408 | D3DPTBLENDCAPS_DECALMASK
1409 | D3DPTBLENDCAPS_MODULATE
1410 | D3DPTBLENDCAPS_MODULATEALPHA;
1411 caps->dpcLineCaps.dwStippleWidth = 32;
1412 caps->dpcLineCaps.dwStippleHeight = 32;
1413 /* Use the same for the TriCaps */
1414 caps->dpcTriCaps = caps->dpcLineCaps;
1415
1416 caps->dwDeviceRenderBitDepth = DDBD_16 | DDBD_24 | DDBD_32;
1417 caps->dwDeviceZBufferBitDepth = DDBD_16 | DDBD_24;
1418 caps->dwMinTextureWidth = 1;
1419 caps->dwMinTextureHeight = 1;
1420
1421 /* Convert DWORDs safely to WORDs */
1422 if (wined3d_caps.MaxTextureBlendStages > 0xffff)
1423 caps->wMaxTextureBlendStages = 0xffff;
1424 else
1425 caps->wMaxTextureBlendStages = (WORD)wined3d_caps.MaxTextureBlendStages;
1426 if (wined3d_caps.MaxSimultaneousTextures > 0xffff)
1427 caps->wMaxSimultaneousTextures = 0xffff;
1428 else
1429 caps->wMaxSimultaneousTextures = (WORD)wined3d_caps.MaxSimultaneousTextures;
1430
1431 if (wined3d_caps.MaxUserClipPlanes > 0xffff)
1432 caps->wMaxUserClipPlanes = 0xffff;
1433 else
1434 caps->wMaxUserClipPlanes = (WORD)wined3d_caps.MaxUserClipPlanes;
1435 if (wined3d_caps.MaxVertexBlendMatrices > 0xffff)
1436 caps->wMaxVertexBlendMatrices = 0xffff;
1437 else
1438 caps->wMaxVertexBlendMatrices = (WORD)wined3d_caps.MaxVertexBlendMatrices;
1439
1440 caps->deviceGUID = IID_IDirect3DTnLHalDevice;
1441
1442 caps->dwReserved1 = 0;
1443 caps->dwReserved2 = 0;
1444 caps->dwReserved3 = 0;
1445 caps->dwReserved4 = 0;
1446
1447 return DD_OK;
1448 }
1449
1450 HRESULT CALLBACK enum_zbuffer(DDPIXELFORMAT *format, void *ctx)
1451 {
1452 DDCAPS *caps = ctx;
1453
1454 switch (format->u1.dwZBufferBitDepth)
1455 {
1456 case 8:
1457 caps->dwZBufferBitDepths |= DDBD_8;
1458 break;
1459 case 16:
1460 caps->dwZBufferBitDepths |= DDBD_16;
1461 break;
1462 case 24:
1463 caps->dwZBufferBitDepths |= DDBD_24;
1464 break;
1465 case 32:
1466 caps->dwZBufferBitDepths |= DDBD_32;
1467 break;
1468 }
1469 return D3DENUMRET_OK;
1470 }
1471
1472 /*****************************************************************************
1473 * IDirectDraw7::GetCaps
1474 *
1475 * Returns the drives capabilities
1476 *
1477 * Used for version 1, 2, 4 and 7
1478 *
1479 * Params:
1480 * DriverCaps: Structure to write the Hardware accelerated caps to
1481 * HelCaps: Structure to write the emulation caps to
1482 *
1483 * Returns
1484 * This implementation returns DD_OK only
1485 *
1486 *****************************************************************************/
1487 static HRESULT WINAPI ddraw7_GetCaps(IDirectDraw7 *iface, DDCAPS *DriverCaps, DDCAPS *HELCaps)
1488 {
1489 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1490 DDCAPS caps;
1491 WINED3DCAPS winecaps;
1492 HRESULT hr;
1493 DDSCAPS2 ddscaps = {0, 0, 0, {0}};
1494
1495 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, DriverCaps, HELCaps);
1496
1497 /* One structure must be != NULL */
1498 if (!DriverCaps && !HELCaps)
1499 {
1500 WARN("Invalid parameters.\n");
1501 return DDERR_INVALIDPARAMS;
1502 }
1503
1504 memset(&caps, 0, sizeof(caps));
1505 memset(&winecaps, 0, sizeof(winecaps));
1506 caps.dwSize = sizeof(caps);
1507
1508 wined3d_mutex_lock();
1509 hr = wined3d_device_get_device_caps(ddraw->wined3d_device, &winecaps);
1510 if (FAILED(hr))
1511 {
1512 WARN("Failed to get device caps, %#x.\n", hr);
1513 wined3d_mutex_unlock();
1514 return hr;
1515 }
1516
1517 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
1518 if (FAILED(hr))
1519 {
1520 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
1521 wined3d_mutex_unlock();
1522 return hr;
1523 }
1524
1525 hr = IDirectDraw7_GetFourCCCodes(iface, &caps.dwNumFourCCCodes, NULL);
1526 wined3d_mutex_unlock();
1527 if (FAILED(hr))
1528 {
1529 WARN("IDirectDraw7::GetFourCCCodes failed\n");
1530 return hr;
1531 }
1532
1533 caps.dwCaps = winecaps.ddraw_caps.caps;
1534 caps.dwCaps2 = winecaps.ddraw_caps.caps2;
1535 caps.dwCKeyCaps = winecaps.ddraw_caps.color_key_caps;
1536 caps.dwFXCaps = winecaps.ddraw_caps.fx_caps;
1537 caps.dwPalCaps = DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE;
1538 caps.ddsCaps.dwCaps = winecaps.ddraw_caps.dds_caps;
1539 caps.dwSVBCaps = winecaps.ddraw_caps.svb_caps;
1540 caps.dwSVBCKeyCaps = winecaps.ddraw_caps.svb_color_key_caps;
1541 caps.dwSVBFXCaps = winecaps.ddraw_caps.svb_fx_caps;
1542 caps.dwVSBCaps = winecaps.ddraw_caps.vsb_caps;
1543 caps.dwVSBCKeyCaps = winecaps.ddraw_caps.vsb_color_key_caps;
1544 caps.dwVSBFXCaps = winecaps.ddraw_caps.vsb_fx_caps;
1545 caps.dwSSBCaps = winecaps.ddraw_caps.ssb_caps;
1546 caps.dwSSBCKeyCaps = winecaps.ddraw_caps.ssb_color_key_caps;
1547 caps.dwSSBFXCaps = winecaps.ddraw_caps.ssb_fx_caps;
1548
1549 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
1550 caps.dwAlignStrideAlign = DDRAW_STRIDE_ALIGNMENT;
1551
1552 caps.ddsOldCaps.dwCaps = caps.ddsCaps.dwCaps;
1553
1554 IDirect3D7_EnumZBufferFormats(&ddraw->IDirect3D7_iface, &IID_IDirect3DHALDevice, enum_zbuffer, &caps);
1555
1556 if(DriverCaps)
1557 {
1558 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
1559 if (TRACE_ON(ddraw))
1560 {
1561 TRACE("Driver Caps :\n");
1562 DDRAW_dump_DDCAPS(DriverCaps);
1563 }
1564
1565 }
1566 if(HELCaps)
1567 {
1568 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
1569 if (TRACE_ON(ddraw))
1570 {
1571 TRACE("HEL Caps :\n");
1572 DDRAW_dump_DDCAPS(HELCaps);
1573 }
1574 }
1575
1576 return DD_OK;
1577 }
1578
1579 static HRESULT WINAPI ddraw4_GetCaps(IDirectDraw4 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1580 {
1581 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1582
1583 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1584
1585 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1586 }
1587
1588 static HRESULT WINAPI ddraw2_GetCaps(IDirectDraw2 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1589 {
1590 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1591
1592 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1593
1594 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1595 }
1596
1597 static HRESULT WINAPI ddraw1_GetCaps(IDirectDraw *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1598 {
1599 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1600
1601 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1602
1603 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1604 }
1605
1606 /*****************************************************************************
1607 * IDirectDraw7::Compact
1608 *
1609 * No idea what it does, MSDN says it's not implemented.
1610 *
1611 * Returns
1612 * DD_OK, but this is unchecked
1613 *
1614 *****************************************************************************/
1615 static HRESULT WINAPI ddraw7_Compact(IDirectDraw7 *iface)
1616 {
1617 TRACE("iface %p.\n", iface);
1618
1619 return DD_OK;
1620 }
1621
1622 static HRESULT WINAPI ddraw4_Compact(IDirectDraw4 *iface)
1623 {
1624 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1625
1626 TRACE("iface %p.\n", iface);
1627
1628 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1629 }
1630
1631 static HRESULT WINAPI ddraw2_Compact(IDirectDraw2 *iface)
1632 {
1633 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1634
1635 TRACE("iface %p.\n", iface);
1636
1637 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1638 }
1639
1640 static HRESULT WINAPI ddraw1_Compact(IDirectDraw *iface)
1641 {
1642 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1643
1644 TRACE("iface %p.\n", iface);
1645
1646 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1647 }
1648
1649 /*****************************************************************************
1650 * IDirectDraw7::GetDisplayMode
1651 *
1652 * Returns information about the current display mode
1653 *
1654 * Exists in versions 1, 2, 4 and 7
1655 *
1656 * Params:
1657 * DDSD: Address of a surface description structure to write the info to
1658 *
1659 * Returns
1660 * DD_OK
1661 *
1662 *****************************************************************************/
1663 static HRESULT WINAPI ddraw7_GetDisplayMode(IDirectDraw7 *iface, DDSURFACEDESC2 *DDSD)
1664 {
1665 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1666 struct wined3d_display_mode mode;
1667 HRESULT hr;
1668 DWORD Size;
1669
1670 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
1671
1672 wined3d_mutex_lock();
1673 /* This seems sane */
1674 if (!DDSD)
1675 {
1676 wined3d_mutex_unlock();
1677 return DDERR_INVALIDPARAMS;
1678 }
1679
1680 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
1681 {
1682 ERR("Failed to get display mode, hr %#x.\n", hr);
1683 wined3d_mutex_unlock();
1684 return hr;
1685 }
1686
1687 Size = DDSD->dwSize;
1688 memset(DDSD, 0, Size);
1689
1690 DDSD->dwSize = Size;
1691 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
1692 DDSD->dwWidth = mode.width;
1693 DDSD->dwHeight = mode.height;
1694 DDSD->u2.dwRefreshRate = 60;
1695 DDSD->ddsCaps.dwCaps = 0;
1696 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
1697 ddrawformat_from_wined3dformat(&DDSD->u4.ddpfPixelFormat, mode.format_id);
1698 DDSD->u1.lPitch = mode.width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
1699
1700 if(TRACE_ON(ddraw))
1701 {
1702 TRACE("Returning surface desc :\n");
1703 DDRAW_dump_surface_desc(DDSD);
1704 }
1705
1706 wined3d_mutex_unlock();
1707
1708 return DD_OK;
1709 }
1710
1711 static HRESULT WINAPI ddraw4_GetDisplayMode(IDirectDraw4 *iface, DDSURFACEDESC2 *surface_desc)
1712 {
1713 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1714
1715 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1716
1717 return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, surface_desc);
1718 }
1719
1720 static HRESULT WINAPI ddraw2_GetDisplayMode(IDirectDraw2 *iface, DDSURFACEDESC *surface_desc)
1721 {
1722 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1723
1724 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1725
1726 /* FIXME: Test sizes, properly convert surface_desc */
1727 return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1728 }
1729
1730 static HRESULT WINAPI ddraw1_GetDisplayMode(IDirectDraw *iface, DDSURFACEDESC *surface_desc)
1731 {
1732 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1733
1734 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1735
1736 /* FIXME: Test sizes, properly convert surface_desc */
1737 return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1738 }
1739
1740 /*****************************************************************************
1741 * IDirectDraw7::GetFourCCCodes
1742 *
1743 * Returns an array of supported FourCC codes.
1744 *
1745 * Exists in versions 1, 2, 4 and 7
1746 *
1747 * Params:
1748 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
1749 * of enumerated codes
1750 * Codes: Pointer to an array of DWORDs where the supported codes are written
1751 * to
1752 *
1753 * Returns
1754 * Always returns DD_OK, as it's a stub for now
1755 *
1756 *****************************************************************************/
1757 static HRESULT WINAPI ddraw7_GetFourCCCodes(IDirectDraw7 *iface, DWORD *NumCodes, DWORD *Codes)
1758 {
1759 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1760 static const enum wined3d_format_id formats[] =
1761 {
1762 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
1763 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
1764 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
1765 };
1766 struct wined3d_display_mode mode;
1767 DWORD count = 0, i, outsize;
1768 HRESULT hr;
1769
1770 TRACE("iface %p, codes_count %p, codes %p.\n", iface, NumCodes, Codes);
1771
1772 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
1773 {
1774 ERR("Failed to get display mode, hr %#x.\n", hr);
1775 return hr;
1776 }
1777
1778 outsize = NumCodes && Codes ? *NumCodes : 0;
1779
1780 for (i = 0; i < (sizeof(formats) / sizeof(formats[0])); ++i)
1781 {
1782 if (SUCCEEDED(wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, WINED3D_DEVICE_TYPE_HAL,
1783 mode.format_id, 0, WINED3D_RTYPE_TEXTURE_2D, formats[i])))
1784 {
1785 if (count < outsize)
1786 Codes[count] = formats[i];
1787 ++count;
1788 }
1789 }
1790 if(NumCodes) {
1791 TRACE("Returning %u FourCC codes\n", count);
1792 *NumCodes = count;
1793 }
1794
1795 return DD_OK;
1796 }
1797
1798 static HRESULT WINAPI ddraw4_GetFourCCCodes(IDirectDraw4 *iface, DWORD *codes_count, DWORD *codes)
1799 {
1800 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1801
1802 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1803
1804 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1805 }
1806
1807 static HRESULT WINAPI ddraw2_GetFourCCCodes(IDirectDraw2 *iface, DWORD *codes_count, DWORD *codes)
1808 {
1809 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1810
1811 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1812
1813 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1814 }
1815
1816 static HRESULT WINAPI ddraw1_GetFourCCCodes(IDirectDraw *iface, DWORD *codes_count, DWORD *codes)
1817 {
1818 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1819
1820 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1821
1822 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1823 }
1824
1825 static HRESULT WINAPI ddraw7_GetMonitorFrequency(IDirectDraw7 *iface, DWORD *frequency)
1826 {
1827 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1828 struct wined3d_display_mode mode;
1829 HRESULT hr;
1830
1831 TRACE("iface %p, frequency %p.\n", iface, frequency);
1832
1833 wined3d_mutex_lock();
1834 hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL);
1835 wined3d_mutex_unlock();
1836 if (FAILED(hr))
1837 {
1838 WARN("Failed to get display mode, hr %#x.\n", hr);
1839 return hr;
1840 }
1841
1842 *frequency = mode.refresh_rate;
1843
1844 return DD_OK;
1845 }
1846
1847 static HRESULT WINAPI ddraw4_GetMonitorFrequency(IDirectDraw4 *iface, DWORD *frequency)
1848 {
1849 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1850
1851 TRACE("iface %p, frequency %p.\n", iface, frequency);
1852
1853 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1854 }
1855
1856 static HRESULT WINAPI ddraw2_GetMonitorFrequency(IDirectDraw2 *iface, DWORD *frequency)
1857 {
1858 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1859
1860 TRACE("iface %p, frequency %p.\n", iface, frequency);
1861
1862 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1863 }
1864
1865 static HRESULT WINAPI ddraw1_GetMonitorFrequency(IDirectDraw *iface, DWORD *frequency)
1866 {
1867 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1868
1869 TRACE("iface %p, frequency %p.\n", iface, frequency);
1870
1871 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1872 }
1873
1874 static HRESULT WINAPI ddraw7_GetVerticalBlankStatus(IDirectDraw7 *iface, BOOL *status)
1875 {
1876 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1877 struct wined3d_raster_status raster_status;
1878 HRESULT hr;
1879
1880 TRACE("iface %p, status %p.\n", iface, status);
1881
1882 if(!status)
1883 return DDERR_INVALIDPARAMS;
1884
1885 wined3d_mutex_lock();
1886 hr = wined3d_get_adapter_raster_status(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &raster_status);
1887 wined3d_mutex_unlock();
1888 if (FAILED(hr))
1889 {
1890 WARN("Failed to get raster status, hr %#x.\n", hr);
1891 return hr;
1892 }
1893
1894 *status = raster_status.in_vblank;
1895
1896 return DD_OK;
1897 }
1898
1899 static HRESULT WINAPI ddraw4_GetVerticalBlankStatus(IDirectDraw4 *iface, BOOL *status)
1900 {
1901 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1902
1903 TRACE("iface %p, status %p.\n", iface, status);
1904
1905 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1906 }
1907
1908 static HRESULT WINAPI ddraw2_GetVerticalBlankStatus(IDirectDraw2 *iface, BOOL *status)
1909 {
1910 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1911
1912 TRACE("iface %p, status %p.\n", iface, status);
1913
1914 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1915 }
1916
1917 static HRESULT WINAPI ddraw1_GetVerticalBlankStatus(IDirectDraw *iface, BOOL *status)
1918 {
1919 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1920
1921 TRACE("iface %p, status %p.\n", iface, status);
1922
1923 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1924 }
1925
1926 /*****************************************************************************
1927 * IDirectDraw7::GetAvailableVidMem
1928 *
1929 * Returns the total and free video memory
1930 *
1931 * Params:
1932 * caps: Specifies the memory type asked for
1933 * total: Pointer to a DWORD to be filled with the total memory
1934 * free: Pointer to a DWORD to be filled with the free memory
1935 *
1936 * Returns
1937 * DD_OK on success
1938 * DDERR_INVALIDPARAMS if free and total are NULL
1939 *
1940 *****************************************************************************/
1941 static HRESULT WINAPI ddraw7_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *caps, DWORD *total,
1942 DWORD *free)
1943 {
1944 unsigned int framebuffer_size, total_vidmem, free_vidmem;
1945 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1946 struct wined3d_display_mode mode;
1947 HRESULT hr = DD_OK;
1948
1949 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1950
1951 if (!total && !free)
1952 return DDERR_INVALIDPARAMS;
1953
1954 if (TRACE_ON(ddraw))
1955 {
1956 TRACE("Asked for memory with description: ");
1957 DDRAW_dump_DDSCAPS2(caps);
1958 }
1959 wined3d_mutex_lock();
1960
1961 /* Todo: System memory vs local video memory vs non-local video memory
1962 * The MSDN also mentions differences between texture memory and other
1963 * resources, but that's not important
1964 */
1965
1966 /* Some applications (e.g. 3DMark 2000) assume that the reported amount of
1967 * video memory doesn't include the memory used by the default framebuffer.
1968 */
1969 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
1970 {
1971 WARN("Failed to get display mode, hr %#x.\n", hr);
1972 wined3d_mutex_unlock();
1973 return hr;
1974 }
1975 framebuffer_size = wined3d_calculate_format_pitch(ddraw->wined3d, WINED3DADAPTER_DEFAULT,
1976 mode.format_id, mode.width);
1977 framebuffer_size *= mode.height;
1978
1979 if (free)
1980 {
1981 free_vidmem = wined3d_device_get_available_texture_mem(ddraw->wined3d_device);
1982 *free = framebuffer_size > free_vidmem ? 0 : free_vidmem - framebuffer_size;
1983 TRACE("Free video memory %#x.\n", *free);
1984 }
1985
1986 if (total)
1987 {
1988 struct wined3d_adapter_identifier desc = {0};
1989
1990 hr = wined3d_get_adapter_identifier(ddraw->wined3d, WINED3DADAPTER_DEFAULT, 0, &desc);
1991 total_vidmem = min(UINT_MAX, desc.video_memory);
1992 *total = framebuffer_size > total_vidmem ? 0 : total_vidmem - framebuffer_size;
1993 TRACE("Total video memory %#x.\n", *total);
1994 }
1995
1996 wined3d_mutex_unlock();
1997
1998 return hr;
1999 }
2000
2001 static HRESULT WINAPI ddraw4_GetAvailableVidMem(IDirectDraw4 *iface,
2002 DDSCAPS2 *caps, DWORD *total, DWORD *free)
2003 {
2004 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2005
2006 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
2007
2008 return ddraw7_GetAvailableVidMem(&ddraw->IDirectDraw7_iface, caps, total, free);
2009 }
2010
2011 static HRESULT WINAPI ddraw2_GetAvailableVidMem(IDirectDraw2 *iface,
2012 DDSCAPS *caps, DWORD *total, DWORD *free)
2013 {
2014 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2015 DDSCAPS2 caps2;
2016
2017 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
2018
2019 DDRAW_Convert_DDSCAPS_1_To_2(caps, &caps2);
2020 return ddraw7_GetAvailableVidMem(&ddraw->IDirectDraw7_iface, &caps2, total, free);
2021 }
2022
2023 /*****************************************************************************
2024 * IDirectDraw7::Initialize
2025 *
2026 * Initializes a DirectDraw interface.
2027 *
2028 * Params:
2029 * GUID: Interface identifier. Well, don't know what this is really good
2030 * for
2031 *
2032 * Returns
2033 * Returns DD_OK on the first call,
2034 * DDERR_ALREADYINITIALIZED on repeated calls
2035 *
2036 *****************************************************************************/
2037 static HRESULT WINAPI ddraw7_Initialize(IDirectDraw7 *iface, GUID *guid)
2038 {
2039 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2040
2041 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
2042
2043 if (ddraw->flags & DDRAW_INITIALIZED)
2044 return DDERR_ALREADYINITIALIZED;
2045
2046 /* FIXME: To properly take the GUID into account we should call
2047 * ddraw_init() here instead of in DDRAW_Create(). */
2048 if (guid)
2049 FIXME("Ignoring guid %s.\n", debugstr_guid(guid));
2050
2051 ddraw->flags |= DDRAW_INITIALIZED;
2052 return DD_OK;
2053 }
2054
2055 static HRESULT WINAPI ddraw4_Initialize(IDirectDraw4 *iface, GUID *guid)
2056 {
2057 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2058
2059 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
2060
2061 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
2062 }
2063
2064 static HRESULT WINAPI ddraw2_Initialize(IDirectDraw2 *iface, GUID *guid)
2065 {
2066 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2067
2068 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
2069
2070 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
2071 }
2072
2073 static HRESULT WINAPI ddraw1_Initialize(IDirectDraw *iface, GUID *guid)
2074 {
2075 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2076
2077 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
2078
2079 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
2080 }
2081
2082 static HRESULT WINAPI d3d1_Initialize(IDirect3D *iface, REFIID riid)
2083 {
2084 TRACE("iface %p, riid %s.\n", iface, debugstr_guid(riid));
2085
2086 return DDERR_ALREADYINITIALIZED;
2087 }
2088
2089 /*****************************************************************************
2090 * IDirectDraw7::FlipToGDISurface
2091 *
2092 * "Makes the surface that the GDI writes to the primary surface"
2093 * Looks like some windows specific thing we don't have to care about.
2094 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
2095 * show error boxes ;)
2096 * Well, just return DD_OK.
2097 *
2098 * Returns:
2099 * Always returns DD_OK
2100 *
2101 *****************************************************************************/
2102 static HRESULT WINAPI ddraw7_FlipToGDISurface(IDirectDraw7 *iface)
2103 {
2104 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2105
2106 TRACE("iface %p.\n", iface);
2107
2108 ddraw->flags |= DDRAW_GDI_FLIP;
2109
2110 if (ddraw->primary)
2111 ddraw_surface_update_frontbuffer(ddraw->primary, NULL, FALSE);
2112
2113 return DD_OK;
2114 }
2115
2116 static HRESULT WINAPI ddraw4_FlipToGDISurface(IDirectDraw4 *iface)
2117 {
2118 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2119
2120 TRACE("iface %p.\n", iface);
2121
2122 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
2123 }
2124
2125 static HRESULT WINAPI ddraw2_FlipToGDISurface(IDirectDraw2 *iface)
2126 {
2127 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2128
2129 TRACE("iface %p.\n", iface);
2130
2131 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
2132 }
2133
2134 static HRESULT WINAPI ddraw1_FlipToGDISurface(IDirectDraw *iface)
2135 {
2136 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2137
2138 TRACE("iface %p.\n", iface);
2139
2140 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
2141 }
2142
2143 /*****************************************************************************
2144 * IDirectDraw7::WaitForVerticalBlank
2145 *
2146 * This method allows applications to get in sync with the vertical blank
2147 * interval.
2148 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
2149 * redraw the screen, most likely because of this stub
2150 *
2151 * Parameters:
2152 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
2153 * or DDWAITVB_BLOCKEND
2154 * h: Not used, according to MSDN
2155 *
2156 * Returns:
2157 * Always returns DD_OK
2158 *
2159 *****************************************************************************/
2160 static HRESULT WINAPI ddraw7_WaitForVerticalBlank(IDirectDraw7 *iface, DWORD Flags, HANDLE event)
2161 {
2162 static BOOL hide;
2163
2164 TRACE("iface %p, flags %#x, event %p.\n", iface, Flags, event);
2165
2166 /* This function is called often, so print the fixme only once */
2167 if(!hide)
2168 {
2169 FIXME("iface %p, flags %#x, event %p stub!\n", iface, Flags, event);
2170 hide = TRUE;
2171 }
2172
2173 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
2174 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
2175 return DDERR_UNSUPPORTED; /* unchecked */
2176
2177 return DD_OK;
2178 }
2179
2180 static HRESULT WINAPI ddraw4_WaitForVerticalBlank(IDirectDraw4 *iface, DWORD flags, HANDLE event)
2181 {
2182 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2183
2184 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
2185
2186 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
2187 }
2188
2189 static HRESULT WINAPI ddraw2_WaitForVerticalBlank(IDirectDraw2 *iface, DWORD flags, HANDLE event)
2190 {
2191 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2192
2193 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
2194
2195 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
2196 }
2197
2198 static HRESULT WINAPI ddraw1_WaitForVerticalBlank(IDirectDraw *iface, DWORD flags, HANDLE event)
2199 {
2200 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2201
2202 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
2203
2204 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
2205 }
2206
2207 static HRESULT WINAPI ddraw7_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
2208 {
2209 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2210 struct wined3d_raster_status raster_status;
2211 HRESULT hr;
2212
2213 TRACE("iface %p, line %p.\n", iface, Scanline);
2214
2215 wined3d_mutex_lock();
2216 hr = wined3d_get_adapter_raster_status(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &raster_status);
2217 wined3d_mutex_unlock();
2218 if (FAILED(hr))
2219 {
2220 WARN("Failed to get raster status, hr %#x.\n", hr);
2221 return hr;
2222 }
2223
2224 *Scanline = raster_status.scan_line;
2225
2226 if (raster_status.in_vblank)
2227 return DDERR_VERTICALBLANKINPROGRESS;
2228
2229 return DD_OK;
2230 }
2231
2232 static HRESULT WINAPI ddraw4_GetScanLine(IDirectDraw4 *iface, DWORD *line)
2233 {
2234 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2235
2236 TRACE("iface %p, line %p.\n", iface, line);
2237
2238 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
2239 }
2240
2241 static HRESULT WINAPI ddraw2_GetScanLine(IDirectDraw2 *iface, DWORD *line)
2242 {
2243 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2244
2245 TRACE("iface %p, line %p.\n", iface, line);
2246
2247 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
2248 }
2249
2250 static HRESULT WINAPI ddraw1_GetScanLine(IDirectDraw *iface, DWORD *line)
2251 {
2252 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2253
2254 TRACE("iface %p, line %p.\n", iface, line);
2255
2256 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
2257 }
2258
2259 static HRESULT WINAPI ddraw7_TestCooperativeLevel(IDirectDraw7 *iface)
2260 {
2261 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2262
2263 TRACE("iface %p.\n", iface);
2264
2265 return ddraw->device_state == DDRAW_DEVICE_STATE_LOST ? DDERR_NOEXCLUSIVEMODE : DD_OK;
2266 }
2267
2268 static HRESULT WINAPI ddraw4_TestCooperativeLevel(IDirectDraw4 *iface)
2269 {
2270 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2271
2272 TRACE("iface %p.\n", iface);
2273
2274 return ddraw7_TestCooperativeLevel(&ddraw->IDirectDraw7_iface);
2275 }
2276
2277 /*****************************************************************************
2278 * IDirectDraw7::GetGDISurface
2279 *
2280 * Returns the surface that GDI is treating as the primary surface.
2281 * For Wine this is the front buffer
2282 *
2283 * Params:
2284 * GDISurface: Address to write the surface pointer to
2285 *
2286 * Returns:
2287 * DD_OK if the surface was found
2288 * DDERR_NOTFOUND if the GDI surface wasn't found
2289 *
2290 *****************************************************************************/
2291 static HRESULT WINAPI ddraw7_GetGDISurface(IDirectDraw7 *iface, IDirectDrawSurface7 **GDISurface)
2292 {
2293 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2294
2295 TRACE("iface %p, surface %p.\n", iface, GDISurface);
2296
2297 wined3d_mutex_lock();
2298
2299 if (!(*GDISurface = &ddraw->primary->IDirectDrawSurface7_iface))
2300 {
2301 WARN("Primary not created yet.\n");
2302 wined3d_mutex_unlock();
2303 return DDERR_NOTFOUND;
2304 }
2305 IDirectDrawSurface7_AddRef(*GDISurface);
2306
2307 wined3d_mutex_unlock();
2308
2309 return DD_OK;
2310 }
2311
2312 static HRESULT WINAPI ddraw4_GetGDISurface(IDirectDraw4 *iface, IDirectDrawSurface4 **surface)
2313 {
2314 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2315 struct ddraw_surface *surface_impl;
2316 IDirectDrawSurface7 *surface7;
2317 HRESULT hr;
2318
2319 TRACE("iface %p, surface %p.\n", iface, surface);
2320
2321 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2322 if (FAILED(hr))
2323 {
2324 *surface = NULL;
2325 return hr;
2326 }
2327 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2328 *surface = &surface_impl->IDirectDrawSurface4_iface;
2329 IDirectDrawSurface4_AddRef(*surface);
2330 IDirectDrawSurface7_Release(surface7);
2331
2332 return hr;
2333 }
2334
2335 static HRESULT WINAPI ddraw2_GetGDISurface(IDirectDraw2 *iface, IDirectDrawSurface **surface)
2336 {
2337 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2338 struct ddraw_surface *surface_impl;
2339 IDirectDrawSurface7 *surface7;
2340 HRESULT hr;
2341
2342 TRACE("iface %p, surface %p.\n", iface, surface);
2343
2344 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2345 if (FAILED(hr))
2346 {
2347 *surface = NULL;
2348 return hr;
2349 }
2350 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2351 *surface = &surface_impl->IDirectDrawSurface_iface;
2352 IDirectDrawSurface_AddRef(*surface);
2353 IDirectDrawSurface7_Release(surface7);
2354
2355 return hr;
2356 }
2357
2358 static HRESULT WINAPI ddraw1_GetGDISurface(IDirectDraw *iface, IDirectDrawSurface **surface)
2359 {
2360 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2361 struct ddraw_surface *surface_impl;
2362 IDirectDrawSurface7 *surface7;
2363 HRESULT hr;
2364
2365 TRACE("iface %p, surface %p.\n", iface, surface);
2366
2367 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2368 if (FAILED(hr))
2369 {
2370 *surface = NULL;
2371 return hr;
2372 }
2373 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2374 *surface = &surface_impl->IDirectDrawSurface_iface;
2375 IDirectDrawSurface_AddRef(*surface);
2376 IDirectDrawSurface7_Release(surface7);
2377
2378 return hr;
2379 }
2380
2381 struct displaymodescallback_context
2382 {
2383 LPDDENUMMODESCALLBACK func;
2384 void *context;
2385 };
2386
2387 static HRESULT CALLBACK EnumDisplayModesCallbackThunk(DDSURFACEDESC2 *surface_desc, void *context)
2388 {
2389 struct displaymodescallback_context *cbcontext = context;
2390 DDSURFACEDESC desc;
2391
2392 DDSD2_to_DDSD(surface_desc, &desc);
2393 return cbcontext->func(&desc, cbcontext->context);
2394 }
2395
2396 /*****************************************************************************
2397 * IDirectDraw7::EnumDisplayModes
2398 *
2399 * Enumerates the supported Display modes. The modes can be filtered with
2400 * the DDSD parameter.
2401 *
2402 * Params:
2403 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES. For old ddraw
2404 * versions (3 and older?) this is reserved and must be 0.
2405 * DDSD: Surface description to filter the modes
2406 * Context: Pointer passed back to the callback function
2407 * cb: Application-provided callback function
2408 *
2409 * Returns:
2410 * DD_OK on success
2411 * DDERR_INVALIDPARAMS if the callback wasn't set
2412 *
2413 *****************************************************************************/
2414 static HRESULT WINAPI ddraw7_EnumDisplayModes(IDirectDraw7 *iface, DWORD Flags,
2415 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMMODESCALLBACK2 cb)
2416 {
2417 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2418 struct wined3d_display_mode *enum_modes = NULL;
2419 struct wined3d_display_mode mode;
2420 unsigned int modenum, fmt;
2421 DDSURFACEDESC2 callback_sd;
2422 unsigned enum_mode_count = 0, enum_mode_array_size = 16;
2423 DDPIXELFORMAT pixelformat;
2424
2425 static const enum wined3d_format_id checkFormatList[] =
2426 {
2427 WINED3DFMT_B8G8R8X8_UNORM,
2428 WINED3DFMT_B5G6R5_UNORM,
2429 WINED3DFMT_P8_UINT,
2430 };
2431
2432 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2433 iface, Flags, DDSD, Context, cb);
2434
2435 if (!cb)
2436 return DDERR_INVALIDPARAMS;
2437
2438 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(*enum_modes) * enum_mode_array_size);
2439 if (!enum_modes) return DDERR_OUTOFMEMORY;
2440
2441 wined3d_mutex_lock();
2442
2443 pixelformat.dwSize = sizeof(pixelformat);
2444 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
2445 {
2446 modenum = 0;
2447 while (wined3d_enum_adapter_modes(ddraw->wined3d, WINED3DADAPTER_DEFAULT, checkFormatList[fmt],
2448 WINED3D_SCANLINE_ORDERING_UNKNOWN, modenum++, &mode) == WINED3D_OK)
2449 {
2450 BOOL found = FALSE;
2451 unsigned i;
2452
2453 ddrawformat_from_wined3dformat(&pixelformat, mode.format_id);
2454 if (DDSD)
2455 {
2456 if (DDSD->dwFlags & DDSD_WIDTH && mode.width != DDSD->dwWidth)
2457 continue;
2458 if (DDSD->dwFlags & DDSD_HEIGHT && mode.height != DDSD->dwHeight)
2459 continue;
2460 if (DDSD->dwFlags & DDSD_REFRESHRATE && mode.refresh_rate != DDSD->u2.dwRefreshRate)
2461 continue;
2462 if (DDSD->dwFlags & DDSD_PIXELFORMAT
2463 && pixelformat.u1.dwRGBBitCount != DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount)
2464 continue;
2465 }
2466
2467 /* DX docs state EnumDisplayMode should return only unique modes */
2468 for (i = 0; i < enum_mode_count; i++)
2469 {
2470 if (enum_modes[i].width == mode.width && enum_modes[i].height == mode.height
2471 && enum_modes[i].format_id == mode.format_id
2472 && (enum_modes[i].refresh_rate == mode.refresh_rate || !(Flags & DDEDM_REFRESHRATES)))
2473 {
2474 found = TRUE;
2475 break;
2476 }
2477 }
2478 if(found) continue;
2479
2480 memset(&callback_sd, 0, sizeof(callback_sd));
2481 callback_sd.dwSize = sizeof(callback_sd);
2482 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2483
2484 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH|DDSD_REFRESHRATE;
2485 if (Flags & DDEDM_REFRESHRATES)
2486 callback_sd.u2.dwRefreshRate = mode.refresh_rate;
2487
2488 callback_sd.dwWidth = mode.width;
2489 callback_sd.dwHeight = mode.height;
2490
2491 callback_sd.u4.ddpfPixelFormat=pixelformat;
2492
2493 /* Calc pitch and DWORD align like MSDN says */
2494 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.width;
2495 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
2496
2497 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
2498 callback_sd.u2.dwRefreshRate);
2499
2500 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
2501 {
2502 TRACE("Application asked to terminate the enumeration\n");
2503 HeapFree(GetProcessHeap(), 0, enum_modes);
2504 wined3d_mutex_unlock();
2505 return DD_OK;
2506 }
2507
2508 if (enum_mode_count == enum_mode_array_size)
2509 {
2510 struct wined3d_display_mode *new_enum_modes;
2511
2512 enum_mode_array_size *= 2;
2513 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes,
2514 sizeof(*new_enum_modes) * enum_mode_array_size);
2515 if (!new_enum_modes)
2516 {
2517 HeapFree(GetProcessHeap(), 0, enum_modes);
2518 wined3d_mutex_unlock();
2519 return DDERR_OUTOFMEMORY;
2520 }
2521
2522 enum_modes = new_enum_modes;
2523 }
2524 enum_modes[enum_mode_count++] = mode;
2525 }
2526 }
2527
2528 TRACE("End of enumeration\n");
2529 HeapFree(GetProcessHeap(), 0, enum_modes);
2530 wined3d_mutex_unlock();
2531
2532 return DD_OK;
2533 }
2534
2535 static HRESULT WINAPI ddraw4_EnumDisplayModes(IDirectDraw4 *iface, DWORD flags,
2536 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMMODESCALLBACK2 callback)
2537 {
2538 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2539
2540 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2541 iface, flags, surface_desc, context, callback);
2542
2543 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags, surface_desc, context, callback);
2544 }
2545
2546 static HRESULT WINAPI ddraw2_EnumDisplayModes(IDirectDraw2 *iface, DWORD flags,
2547 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2548 {
2549 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2550 struct displaymodescallback_context cbcontext;
2551 DDSURFACEDESC2 surface_desc2;
2552
2553 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2554 iface, flags, surface_desc, context, callback);
2555
2556 cbcontext.func = callback;
2557 cbcontext.context = context;
2558
2559 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2560 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags,
2561 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2562 }
2563
2564 static HRESULT WINAPI ddraw1_EnumDisplayModes(IDirectDraw *iface, DWORD flags,
2565 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2566 {
2567 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2568 struct displaymodescallback_context cbcontext;
2569 DDSURFACEDESC2 surface_desc2;
2570
2571 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2572 iface, flags, surface_desc, context, callback);
2573
2574 cbcontext.func = callback;
2575 cbcontext.context = context;
2576
2577 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2578 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags,
2579 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2580 }
2581
2582 /*****************************************************************************
2583 * IDirectDraw7::EvaluateMode
2584 *
2585 * Used with IDirectDraw7::StartModeTest to test video modes.
2586 * EvaluateMode is used to pass or fail a mode, and continue with the next
2587 * mode
2588 *
2589 * Params:
2590 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
2591 * Timeout: Returns the amount of seconds left before the mode would have
2592 * been failed automatically
2593 *
2594 * Returns:
2595 * This implementation always DD_OK, because it's a stub
2596 *
2597 *****************************************************************************/
2598 static HRESULT WINAPI ddraw7_EvaluateMode(IDirectDraw7 *iface, DWORD Flags, DWORD *Timeout)
2599 {
2600 FIXME("iface %p, flags %#x, timeout %p stub!\n", iface, Flags, Timeout);
2601
2602 /* When implementing this, implement it in WineD3D */
2603
2604 return DD_OK;
2605 }
2606
2607 /*****************************************************************************
2608 * IDirectDraw7::GetDeviceIdentifier
2609 *
2610 * Returns the device identifier, which gives information about the driver
2611 * Our device identifier is defined at the beginning of this file.
2612 *
2613 * Params:
2614 * DDDI: Address for the returned structure
2615 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
2616 *
2617 * Returns:
2618 * On success it returns DD_OK
2619 * DDERR_INVALIDPARAMS if DDDI is NULL
2620 *
2621 *****************************************************************************/
2622 static HRESULT WINAPI ddraw7_GetDeviceIdentifier(IDirectDraw7 *iface,
2623 DDDEVICEIDENTIFIER2 *DDDI, DWORD Flags)
2624 {
2625 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2626 struct wined3d_adapter_identifier adapter_id;
2627 HRESULT hr = S_OK;
2628
2629 TRACE("iface %p, device_identifier %p, flags %#x.\n", iface, DDDI, Flags);
2630
2631 if (!DDDI)
2632 return DDERR_INVALIDPARAMS;
2633
2634 if (Flags & DDGDI_GETHOSTIDENTIFIER)
2635 {
2636 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
2637 * host adapter, if there's a secondary 3D adapter. This doesn't apply
2638 * to any modern hardware, nor is it interesting for Wine, so ignore it.
2639 * Size of DDDEVICEIDENTIFIER2 may be aligned to 8 bytes and thus 4
2640 * bytes too long. So only copy the relevant part of the structure
2641 */
2642
2643 memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
2644 return DD_OK;
2645 }
2646
2647 /* Drakan: Order of the Flame expects accurate D3D device information from ddraw */
2648 adapter_id.driver = DDDI->szDriver;
2649 adapter_id.driver_size = sizeof(DDDI->szDriver);
2650 adapter_id.description = DDDI->szDescription;
2651 adapter_id.description_size = sizeof(DDDI->szDescription);
2652 adapter_id.device_name_size = 0;
2653 wined3d_mutex_lock();
2654 hr = wined3d_get_adapter_identifier(ddraw->wined3d, WINED3DADAPTER_DEFAULT, 0x0, &adapter_id);
2655 wined3d_mutex_unlock();
2656 if (FAILED(hr)) return hr;
2657
2658 DDDI->liDriverVersion = adapter_id.driver_version;
2659 DDDI->dwVendorId = adapter_id.vendor_id;
2660 DDDI->dwDeviceId = adapter_id.device_id;
2661 DDDI->dwSubSysId = adapter_id.subsystem_id;
2662 DDDI->dwRevision = adapter_id.revision;
2663 DDDI->guidDeviceIdentifier = adapter_id.device_identifier;
2664 DDDI->dwWHQLLevel = adapter_id.whql_level;
2665 return DD_OK;
2666 }
2667
2668 static HRESULT WINAPI ddraw4_GetDeviceIdentifier(IDirectDraw4 *iface,
2669 DDDEVICEIDENTIFIER *identifier, DWORD flags)
2670 {
2671 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2672 DDDEVICEIDENTIFIER2 identifier2;
2673 HRESULT hr;
2674
2675 TRACE("iface %p, identifier %p, flags %#x.\n", iface, identifier, flags);
2676
2677 hr = ddraw7_GetDeviceIdentifier(&ddraw->IDirectDraw7_iface, &identifier2, flags);
2678 DDRAW_Convert_DDDEVICEIDENTIFIER_2_To_1(&identifier2, identifier);
2679
2680 return hr;
2681 }
2682
2683 /*****************************************************************************
2684 * IDirectDraw7::GetSurfaceFromDC
2685 *
2686 * Returns the Surface for a GDI device context handle.
2687 * Is this related to IDirectDrawSurface::GetDC ???
2688 *
2689 * Params:
2690 * hdc: hdc to return the surface for
2691 * Surface: Address to write the surface pointer to
2692 *
2693 * Returns:
2694 * Always returns DD_OK because it's a stub
2695 *
2696 *****************************************************************************/
2697 static HRESULT WINAPI ddraw7_GetSurfaceFromDC(IDirectDraw7 *iface,
2698 HDC dc, IDirectDrawSurface7 **surface)
2699 {
2700 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2701 struct ddraw_surface *surface_impl;
2702
2703 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2704
2705 if (!surface)
2706 return E_INVALIDARG;
2707
2708 if (!dc)
2709 goto done;
2710
2711 wined3d_mutex_lock();
2712 LIST_FOR_EACH_ENTRY(surface_impl, &ddraw->surface_list, struct ddraw_surface, surface_list_entry)
2713 {
2714 if (surface_impl->dc != dc)
2715 continue;
2716
2717 TRACE("Found surface %p for dc %p.\n", surface_impl, dc);
2718 *surface = &surface_impl->IDirectDrawSurface7_iface;
2719 IDirectDrawSurface7_AddRef(*surface);
2720 wined3d_mutex_unlock();
2721 return DD_OK;
2722 }
2723 wined3d_mutex_unlock();
2724
2725 done:
2726 TRACE("No surface found for dc %p.\n", dc);
2727 *surface = NULL;
2728 return DDERR_NOTFOUND;
2729 }
2730
2731 static HRESULT WINAPI ddraw4_GetSurfaceFromDC(IDirectDraw4 *iface, HDC dc,
2732 IDirectDrawSurface4 **surface)
2733 {
2734 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2735 struct ddraw_surface *surface_impl;
2736 IDirectDrawSurface7 *surface7;
2737 HRESULT hr;
2738
2739 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2740
2741 if (!surface) return E_INVALIDARG;
2742
2743 hr = ddraw7_GetSurfaceFromDC(&ddraw->IDirectDraw7_iface, dc, &surface7);
2744 if (FAILED(hr))
2745 {
2746 *surface = NULL;
2747 return hr;
2748 }
2749 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2750 /* Tests say this is true */
2751 *surface = (IDirectDrawSurface4 *)&surface_impl->IDirectDrawSurface_iface;
2752 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
2753 IDirectDrawSurface7_Release(surface7);
2754
2755 return hr;
2756 }
2757
2758 static HRESULT CALLBACK restore_callback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *desc, void *context)
2759 {
2760 IDirectDrawSurface_Restore(surface);
2761 IDirectDrawSurface_Release(surface);
2762
2763 return DDENUMRET_OK;
2764 }
2765
2766 static HRESULT WINAPI ddraw7_RestoreAllSurfaces(IDirectDraw7 *iface)
2767 {
2768 TRACE("iface %p.\n", iface);
2769
2770 return IDirectDraw7_EnumSurfaces(iface, DDENUMSURFACES_ALL | DDENUMSURFACES_DOESEXIST,
2771 NULL, NULL, restore_callback);
2772 }
2773
2774 static HRESULT WINAPI ddraw4_RestoreAllSurfaces(IDirectDraw4 *iface)
2775 {
2776 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2777
2778 TRACE("iface %p.\n", iface);
2779
2780 return ddraw7_RestoreAllSurfaces(&ddraw->IDirectDraw7_iface);
2781 }
2782
2783 /*****************************************************************************
2784 * IDirectDraw7::StartModeTest
2785 *
2786 * Tests the specified video modes to update the system registry with
2787 * refresh rate information. StartModeTest starts the mode test,
2788 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
2789 * isn't called within 15 seconds, the mode is failed automatically
2790 *
2791 * As refresh rates are handled by the X server, I don't think this
2792 * Method is important
2793 *
2794 * Params:
2795 * Modes: An array of mode specifications
2796 * NumModes: The number of modes in Modes
2797 * Flags: Some flags...
2798 *
2799 * Returns:
2800 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
2801 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
2802 * otherwise DD_OK
2803 *
2804 *****************************************************************************/
2805 static HRESULT WINAPI ddraw7_StartModeTest(IDirectDraw7 *iface, SIZE *Modes, DWORD NumModes, DWORD Flags)
2806 {
2807 FIXME("iface %p, modes %p, mode_count %u, flags %#x partial stub!\n",
2808 iface, Modes, NumModes, Flags);
2809
2810 /* This looks sane */
2811 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
2812
2813 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
2814 * As it is not, DDERR_TESTFINISHED is returned
2815 * (hopefully that's correct
2816 *
2817 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
2818 * well, that value doesn't (yet) exist in the wine headers, so ignore it
2819 */
2820
2821 return DD_OK;
2822 }
2823
2824 static HRESULT WINAPI ddraw7_CreateSurface(IDirectDraw7 *iface, DDSURFACEDESC2 *surface_desc,
2825 IDirectDrawSurface7 **surface, IUnknown *outer_unknown)
2826 {
2827 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2828 struct ddraw_surface *impl;
2829 HRESULT hr;
2830
2831 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2832 iface, surface_desc, surface, outer_unknown);
2833
2834 wined3d_mutex_lock();
2835
2836 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2837 {
2838 WARN("Cooperative level not set.\n");
2839 wined3d_mutex_unlock();
2840 return DDERR_NOCOOPERATIVELEVELSET;
2841 }
2842
2843 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
2844 {
2845 WARN("Application supplied invalid surface descriptor\n");
2846 wined3d_mutex_unlock();
2847 return DDERR_INVALIDPARAMS;
2848 }
2849
2850 __TRY
2851 {
2852 *surface = NULL;
2853 }
2854 __EXCEPT_PAGE_FAULT
2855 {
2856 WARN("Surface pointer %p is invalid.\n", surface);
2857 wined3d_mutex_unlock();
2858 return DDERR_INVALIDPARAMS;
2859 }
2860 __ENDTRY;
2861
2862 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
2863 {
2864 if (TRACE_ON(ddraw))
2865 {
2866 TRACE(" (%p) Requesting surface desc :\n", iface);
2867 DDRAW_dump_surface_desc(surface_desc);
2868 }
2869
2870 WARN("Application tried to create an explicit front or back buffer\n");
2871 wined3d_mutex_unlock();
2872 return DDERR_INVALIDCAPS;
2873 }
2874
2875 hr = ddraw_surface_create(ddraw, surface_desc, &impl, outer_unknown, 7);
2876 wined3d_mutex_unlock();
2877 if (FAILED(hr))
2878 return hr;
2879
2880 *surface = &impl->IDirectDrawSurface7_iface;
2881 IDirectDraw7_AddRef(iface);
2882 impl->ifaceToRelease = (IUnknown *)iface;
2883
2884 return hr;
2885 }
2886
2887 static HRESULT WINAPI ddraw4_CreateSurface(IDirectDraw4 *iface,
2888 DDSURFACEDESC2 *surface_desc, IDirectDrawSurface4 **surface, IUnknown *outer_unknown)
2889 {
2890 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2891 struct ddraw_surface *impl;
2892 HRESULT hr;
2893
2894 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2895 iface, surface_desc, surface, outer_unknown);
2896
2897 wined3d_mutex_lock();
2898
2899 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2900 {
2901 WARN("Cooperative level not set.\n");
2902 wined3d_mutex_unlock();
2903 return DDERR_NOCOOPERATIVELEVELSET;
2904 }
2905
2906 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
2907 {
2908 WARN("Application supplied invalid surface descriptor\n");
2909 wined3d_mutex_unlock();
2910 return DDERR_INVALIDPARAMS;
2911 }
2912
2913 __TRY
2914 {
2915 *surface = NULL;
2916 }
2917 __EXCEPT_PAGE_FAULT
2918 {
2919 WARN("Surface pointer %p is invalid.\n", surface);
2920 wined3d_mutex_unlock();
2921 return DDERR_INVALIDPARAMS;
2922 }
2923 __ENDTRY;
2924
2925 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
2926 {
2927 if (TRACE_ON(ddraw))
2928 {
2929 TRACE(" (%p) Requesting surface desc :\n", iface);
2930 DDRAW_dump_surface_desc(surface_desc);
2931 }
2932
2933 WARN("Application tried to create an explicit front or back buffer\n");
2934 wined3d_mutex_unlock();
2935 return DDERR_INVALIDCAPS;
2936 }
2937
2938 hr = ddraw_surface_create(ddraw, surface_desc, &impl, outer_unknown, 4);
2939 wined3d_mutex_unlock();
2940 if (FAILED(hr))
2941 return hr;
2942
2943 *surface = &impl->IDirectDrawSurface4_iface;
2944 IDirectDraw4_AddRef(iface);
2945 impl->ifaceToRelease = (IUnknown *)iface;
2946
2947 return hr;
2948 }
2949
2950 static HRESULT WINAPI ddraw2_CreateSurface(IDirectDraw2 *iface,
2951 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
2952 {
2953 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2954 struct ddraw_surface *impl;
2955 HRESULT hr;
2956 DDSURFACEDESC2 surface_desc2;
2957
2958 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2959 iface, surface_desc, surface, outer_unknown);
2960
2961 wined3d_mutex_lock();
2962
2963 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2964 {
2965 WARN("Cooperative level not set.\n");
2966 wined3d_mutex_unlock();
2967 return DDERR_NOCOOPERATIVELEVELSET;
2968 }
2969
2970 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
2971 {
2972 WARN("Application supplied invalid surface descriptor\n");
2973 wined3d_mutex_unlock();
2974 return DDERR_INVALIDPARAMS;
2975 }
2976
2977 __TRY
2978 {
2979 *surface = NULL;
2980 }
2981 __EXCEPT_PAGE_FAULT
2982 {
2983 WARN("Surface pointer %p is invalid.\n", surface);
2984 wined3d_mutex_unlock();
2985 return DDERR_INVALIDPARAMS;
2986 }
2987 __ENDTRY;
2988
2989 DDSD_to_DDSD2(surface_desc, &surface_desc2);
2990 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
2991 {
2992 if (TRACE_ON(ddraw))
2993 {
2994 TRACE(" (%p) Requesting surface desc :\n", iface);
2995 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
2996 }
2997
2998 WARN("Application tried to create an explicit front or back buffer\n");
2999 wined3d_mutex_unlock();
3000 return DDERR_INVALIDCAPS;
3001 }
3002
3003 hr = ddraw_surface_create(ddraw, &surface_desc2, &impl, outer_unknown, 2);
3004 wined3d_mutex_unlock();
3005 if (FAILED(hr))
3006 return hr;
3007
3008 *surface = &impl->IDirectDrawSurface_iface;
3009 impl->ifaceToRelease = NULL;
3010
3011 return hr;
3012 }
3013
3014 static HRESULT WINAPI ddraw1_CreateSurface(IDirectDraw *iface,
3015 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3016 {
3017 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3018 struct ddraw_surface *impl;
3019 HRESULT hr;
3020 DDSURFACEDESC2 surface_desc2;
3021
3022 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3023 iface, surface_desc, surface, outer_unknown);
3024
3025 wined3d_mutex_lock();
3026
3027 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
3028 {
3029 WARN("Cooperative level not set.\n");
3030 wined3d_mutex_unlock();
3031 return DDERR_NOCOOPERATIVELEVELSET;
3032 }
3033
3034 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3035 {
3036 WARN("Application supplied invalid surface descriptor\n");
3037 wined3d_mutex_unlock();
3038 return DDERR_INVALIDPARAMS;
3039 }
3040
3041 __TRY
3042 {
3043 *surface = NULL;
3044 }
3045 __EXCEPT_PAGE_FAULT
3046 {
3047 WARN("Surface pointer %p is invalid.\n", surface);
3048 wined3d_mutex_unlock();
3049 return DDERR_INVALIDPARAMS;
3050 }
3051 __ENDTRY;
3052
3053 if ((surface_desc->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER))
3054 == (DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER)
3055 || (surface_desc->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER))
3056 == ((DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER)))
3057 {
3058 WARN("Application tried to create an explicit front or back buffer.\n");
3059 wined3d_mutex_unlock();
3060 return DDERR_INVALIDCAPS;
3061 }
3062
3063 DDSD_to_DDSD2(surface_desc, &surface_desc2);
3064 hr = ddraw_surface_create(ddraw, &surface_desc2, &impl, outer_unknown, 1);
3065 wined3d_mutex_unlock();
3066 if (FAILED(hr))
3067 return hr;
3068
3069 *surface = &impl->IDirectDrawSurface_iface;
3070 impl->ifaceToRelease = NULL;
3071
3072 return hr;
3073 }
3074
3075 static BOOL
3076 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
3077 const DDPIXELFORMAT *provided)
3078 {
3079 /* Some flags must be present in both or neither for a match. */
3080 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
3081 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
3082 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
3083
3084 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3085 return FALSE;
3086
3087 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
3088 return FALSE;
3089
3090 if (requested->dwFlags & DDPF_FOURCC)
3091 if (requested->dwFourCC != provided->dwFourCC)
3092 return FALSE;
3093
3094 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
3095 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3096 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
3097 return FALSE;
3098
3099 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3100 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3101 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
3102 return FALSE;
3103
3104 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
3105 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
3106 return FALSE;
3107
3108 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
3109 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3110 |DDPF_BUMPDUDV))
3111 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
3112 return FALSE;
3113
3114 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
3115 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
3116 return FALSE;
3117
3118 return TRUE;
3119 }
3120
3121 static BOOL ddraw_match_surface_desc(const DDSURFACEDESC2 *requested, const DDSURFACEDESC2 *provided)
3122 {
3123 struct compare_info
3124 {
3125 DWORD flag;
3126 ptrdiff_t offset;
3127 size_t size;
3128 };
3129
3130 #define CMP(FLAG, FIELD) \
3131 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
3132 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
3133
3134 static const struct compare_info compare[] =
3135 {
3136 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
3137 CMP(BACKBUFFERCOUNT, u5.dwBackBufferCount),
3138 CMP(CAPS, ddsCaps),
3139 CMP(CKDESTBLT, ddckCKDestBlt),
3140 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
3141 CMP(CKSRCBLT, ddckCKSrcBlt),
3142 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
3143 CMP(HEIGHT, dwHeight),
3144 CMP(LINEARSIZE, u1 /* dwLinearSize */),
3145 CMP(LPSURFACE, lpSurface),
3146 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
3147 CMP(PITCH, u1 /* lPitch */),
3148 /* PIXELFORMAT: manual */
3149 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
3150 CMP(TEXTURESTAGE, dwTextureStage),
3151 CMP(WIDTH, dwWidth),
3152 /* ZBUFFERBITDEPTH: "obsolete" */
3153 };
3154
3155 #undef CMP
3156
3157 unsigned int i;
3158
3159 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3160 return FALSE;
3161
3162 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
3163 {
3164 if (requested->dwFlags & compare[i].flag
3165 && memcmp((const char *)provided + compare[i].offset,
3166 (const char *)requested + compare[i].offset,
3167 compare[i].size) != 0)
3168 return FALSE;
3169 }
3170
3171 if (requested->dwFlags & DDSD_PIXELFORMAT)
3172 {
3173 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
3174 &provided->u4.ddpfPixelFormat))
3175 return FALSE;
3176 }
3177
3178 return TRUE;
3179 }
3180
3181 struct surfacescallback2_context
3182 {
3183 LPDDENUMSURFACESCALLBACK2 func;
3184 void *context;
3185 };
3186
3187 struct surfacescallback_context
3188 {
3189 LPDDENUMSURFACESCALLBACK func;
3190 void *context;
3191 };
3192
3193 static HRESULT CALLBACK EnumSurfacesCallback2Thunk(IDirectDrawSurface7 *surface,
3194 DDSURFACEDESC2 *surface_desc, void *context)
3195 {
3196 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3197 struct surfacescallback2_context *cbcontext = context;
3198
3199 IDirectDrawSurface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
3200 IDirectDrawSurface7_Release(surface);
3201
3202 return cbcontext->func(&surface_impl->IDirectDrawSurface4_iface,
3203 surface_desc, cbcontext->context);
3204 }
3205
3206 static HRESULT CALLBACK EnumSurfacesCallbackThunk(IDirectDrawSurface7 *surface,
3207 DDSURFACEDESC2 *surface_desc, void *context)
3208 {
3209 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3210 struct surfacescallback_context *cbcontext = context;
3211
3212 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
3213 IDirectDrawSurface7_Release(surface);
3214
3215 return cbcontext->func(&surface_impl->IDirectDrawSurface_iface,
3216 (DDSURFACEDESC *)surface_desc, cbcontext->context);
3217 }
3218
3219 /*****************************************************************************
3220 * IDirectDraw7::EnumSurfaces
3221 *
3222 * Loops through all surfaces attached to this device and calls the
3223 * application callback. This can't be relayed to WineD3DDevice,
3224 * because some WineD3DSurfaces' parents are IParent objects
3225 *
3226 * Params:
3227 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
3228 * DDSD: Description to filter for
3229 * Context: Application-provided pointer, it's passed unmodified to the
3230 * Callback function
3231 * Callback: Address to call for each surface
3232 *
3233 * Returns:
3234 * DDERR_INVALIDPARAMS if the callback is NULL
3235 * DD_OK on success
3236 *
3237 *****************************************************************************/
3238 static HRESULT WINAPI ddraw7_EnumSurfaces(IDirectDraw7 *iface, DWORD Flags,
3239 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMSURFACESCALLBACK7 Callback)
3240 {
3241 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
3242 struct ddraw_surface *surf;
3243 DWORD match_flags = Flags & (DDENUMSURFACES_ALL | DDENUMSURFACES_NOMATCH | DDENUMSURFACES_MATCH);
3244
3245 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3246 iface, Flags, DDSD, Context, Callback);
3247
3248 if (!Callback)
3249 return DDERR_INVALIDPARAMS;
3250
3251 if (Flags & DDENUMSURFACES_CANBECREATED)
3252 {
3253 IDirectDrawSurface7 *surface;
3254 DDSURFACEDESC2 testdesc;
3255 HRESULT hr;
3256
3257 if (match_flags != DDENUMSURFACES_MATCH)
3258 return DDERR_INVALIDPARAMS;
3259
3260 if (!DDSD)
3261 return DDERR_INVALIDPARAMS;
3262
3263 memcpy(&testdesc, DDSD, sizeof(testdesc));
3264 if (!(testdesc.dwFlags & DDSD_WIDTH))
3265 {
3266 testdesc.dwFlags |= DDSD_WIDTH;
3267 testdesc.dwWidth = 512;
3268 }
3269 if (!(testdesc.dwFlags & DDSD_HEIGHT))
3270 {
3271 testdesc.dwFlags |= DDSD_HEIGHT;
3272 testdesc.dwHeight = 512;
3273 }
3274
3275 hr = IDirectDraw7_CreateSurface(iface, &testdesc, &surface, NULL);
3276 if (SUCCEEDED(hr))
3277 {
3278 surf = unsafe_impl_from_IDirectDrawSurface7(surface);
3279 Callback(NULL, &surf->surface_desc, Context);
3280 IDirectDrawSurface7_Release(surface);
3281 }
3282 else
3283 ERR("Failed to create surface, hr %#x.\n", hr);
3284 }
3285 else if (Flags & DDENUMSURFACES_DOESEXIST)
3286 {
3287 BOOL all, nomatch;
3288 DDSURFACEDESC2 desc;
3289 struct list *entry, *entry2;
3290
3291 /* a combination of match flags is not allowed */
3292 if (match_flags != 0 &&
3293 match_flags != DDENUMSURFACES_ALL &&
3294 match_flags != DDENUMSURFACES_MATCH &&
3295 match_flags != DDENUMSURFACES_NOMATCH)
3296 return DDERR_INVALIDPARAMS;
3297
3298 all = (Flags & DDENUMSURFACES_ALL) != 0;
3299 nomatch = (Flags & DDENUMSURFACES_NOMATCH) != 0;
3300
3301 if (!all && !DDSD)
3302 return DDERR_INVALIDPARAMS;
3303
3304 wined3d_mutex_lock();
3305
3306 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
3307 LIST_FOR_EACH_SAFE(entry, entry2, &ddraw->surface_list)
3308 {
3309 surf = LIST_ENTRY(entry, struct ddraw_surface, surface_list_entry);
3310
3311 if (!surf->iface_count)
3312 {
3313 WARN("Not enumerating surface %p because it doesn't have any references.\n", surf);
3314 continue;
3315 }
3316
3317 if (all || (nomatch != ddraw_match_surface_desc(DDSD, &surf->surface_desc)))
3318 {
3319 TRACE("Enumerating surface %p.\n", surf);
3320 desc = surf->surface_desc;
3321 IDirectDrawSurface7_AddRef(&surf->IDirectDrawSurface7_iface);
3322 if (Callback(&surf->IDirectDrawSurface7_iface, &desc, Context) != DDENUMRET_OK)
3323 {
3324 wined3d_mutex_unlock();
3325 return DD_OK;
3326 }
3327 }
3328 }
3329
3330 wined3d_mutex_unlock();
3331 }
3332 else
3333 return DDERR_INVALIDPARAMS;
3334
3335 return DD_OK;
3336 }
3337
3338 static HRESULT WINAPI ddraw4_EnumSurfaces(IDirectDraw4 *iface, DWORD flags,
3339 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMSURFACESCALLBACK2 callback)
3340 {
3341 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3342 struct surfacescallback2_context cbcontext;
3343
3344 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3345 iface, flags, surface_desc, context, callback);
3346
3347 cbcontext.func = callback;
3348 cbcontext.context = context;
3349
3350 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags, surface_desc,
3351 &cbcontext, EnumSurfacesCallback2Thunk);
3352 }
3353
3354 static HRESULT WINAPI ddraw2_EnumSurfaces(IDirectDraw2 *iface, DWORD flags,
3355 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3356 {
3357 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3358 struct surfacescallback_context cbcontext;
3359 DDSURFACEDESC2 surface_desc2;
3360
3361 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3362 iface, flags, surface_desc, context, callback);
3363
3364 cbcontext.func = callback;
3365 cbcontext.context = context;
3366
3367 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3368 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags,
3369 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3370 }
3371
3372 static HRESULT WINAPI ddraw1_EnumSurfaces(IDirectDraw *iface, DWORD flags,
3373 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3374 {
3375 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3376 struct surfacescallback_context cbcontext;
3377 DDSURFACEDESC2 surface_desc2;
3378
3379 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3380 iface, flags, surface_desc, context, callback);
3381
3382 cbcontext.func = callback;
3383 cbcontext.context = context;
3384
3385 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3386 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags,
3387 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3388 }
3389
3390 /*****************************************************************************
3391 * DirectDrawCreateClipper (DDRAW.@)
3392 *
3393 * Creates a new IDirectDrawClipper object.
3394 *
3395 * Params:
3396 * Clipper: Address to write the interface pointer to
3397 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3398 * NULL
3399 *
3400 * Returns:
3401 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3402 * E_OUTOFMEMORY if allocating the object failed
3403 *
3404 *****************************************************************************/
3405 HRESULT WINAPI DirectDrawCreateClipper(DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3406 {
3407 struct ddraw_clipper *object;
3408 HRESULT hr;
3409
3410 TRACE("flags %#x, clipper %p, outer_unknown %p.\n",
3411 flags, clipper, outer_unknown);
3412
3413 if (outer_unknown)
3414 return CLASS_E_NOAGGREGATION;
3415
3416 wined3d_mutex_lock();
3417
3418 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3419 if (!object)
3420 {
3421 wined3d_mutex_unlock();
3422 return E_OUTOFMEMORY;
3423 }
3424
3425 hr = ddraw_clipper_init(object);
3426 if (FAILED(hr))
3427 {
3428 WARN("Failed to initialize clipper, hr %#x.\n", hr);
3429 HeapFree(GetProcessHeap(), 0, object);
3430 wined3d_mutex_unlock();
3431 return hr;
3432 }
3433
3434 TRACE("Created clipper %p.\n", object);
3435 *clipper = &object->IDirectDrawClipper_iface;
3436 wined3d_mutex_unlock();
3437
3438 return DD_OK;
3439 }
3440
3441 /*****************************************************************************
3442 * IDirectDraw7::CreateClipper
3443 *
3444 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3445 *
3446 *****************************************************************************/
3447 static HRESULT WINAPI ddraw7_CreateClipper(IDirectDraw7 *iface, DWORD Flags,
3448 IDirectDrawClipper **Clipper, IUnknown *UnkOuter)
3449 {
3450 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3451 iface, Flags, Clipper, UnkOuter);
3452
3453 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3454 }
3455
3456 static HRESULT WINAPI ddraw4_CreateClipper(IDirectDraw4 *iface, DWORD flags,
3457 IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3458 {
3459 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3460
3461 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3462 iface, flags, clipper, outer_unknown);
3463
3464 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3465 }
3466
3467 static HRESULT WINAPI ddraw2_CreateClipper(IDirectDraw2 *iface,
3468 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3469 {
3470 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3471
3472 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3473 iface, flags, clipper, outer_unknown);
3474
3475 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3476 }
3477
3478 static HRESULT WINAPI ddraw1_CreateClipper(IDirectDraw *iface,
3479 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3480 {
3481 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3482
3483 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3484 iface, flags, clipper, outer_unknown);
3485
3486 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3487 }
3488
3489 /*****************************************************************************
3490 * IDirectDraw7::CreatePalette
3491 *
3492 * Creates a new IDirectDrawPalette object
3493 *
3494 * Params:
3495 * Flags: The flags for the new clipper
3496 * ColorTable: Color table to assign to the new clipper
3497 * Palette: Address to write the interface pointer to
3498 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3499 * NULL
3500 *
3501 * Returns:
3502 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3503 * E_OUTOFMEMORY if allocating the object failed
3504 *
3505 *****************************************************************************/
3506 static HRESULT WINAPI ddraw7_CreatePalette(IDirectDraw7 *iface, DWORD Flags,
3507 PALETTEENTRY *ColorTable, IDirectDrawPalette **Palette, IUnknown *pUnkOuter)
3508 {
3509 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
3510 struct ddraw_palette *object;
3511 HRESULT hr;
3512
3513 TRACE("iface %p, flags %#x, color_table %p, palette %p, outer_unknown %p.\n",
3514 iface, Flags, ColorTable, Palette, pUnkOuter);
3515
3516 if (pUnkOuter)
3517 return CLASS_E_NOAGGREGATION;
3518
3519 wined3d_mutex_lock();
3520
3521 /* The refcount test shows that a cooplevel is required for this */
3522 if (!ddraw->cooperative_level)
3523 {
3524 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3525 wined3d_mutex_unlock();
3526 return DDERR_NOCOOPERATIVELEVELSET;
3527 }
3528
3529 object = HeapAlloc(GetProcessHeap(), 0, sizeof(*object));
3530 if (!object)
3531 {
3532 ERR("Out of memory when allocating memory for a palette implementation\n");
3533 wined3d_mutex_unlock();
3534 return E_OUTOFMEMORY;
3535 }
3536
3537 hr = ddraw_palette_init(object, ddraw, Flags, ColorTable);
3538 if (FAILED(hr))
3539 {
3540 WARN("Failed to initialize palette, hr %#x.\n", hr);
3541 HeapFree(GetProcessHeap(), 0, object);
3542 wined3d_mutex_unlock();
3543 return hr;
3544 }
3545
3546 TRACE("Created palette %p.\n", object);
3547 *Palette = &object->IDirectDrawPalette_iface;
3548 wined3d_mutex_unlock();
3549
3550 return DD_OK;
3551 }
3552
3553 static HRESULT WINAPI ddraw4_CreatePalette(IDirectDraw4 *iface, DWORD flags, PALETTEENTRY *entries,
3554 IDirectDrawPalette **palette, IUnknown *outer_unknown)
3555 {
3556 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3557 HRESULT hr;
3558
3559 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3560 iface, flags, entries, palette, outer_unknown);
3561
3562 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3563 if (SUCCEEDED(hr) && *palette)
3564 {
3565 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3566 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3567 IDirectDraw4_AddRef(iface);
3568 impl->ifaceToRelease = (IUnknown *)iface;
3569 }
3570 return hr;
3571 }
3572
3573 static HRESULT WINAPI ddraw2_CreatePalette(IDirectDraw2 *iface, DWORD flags,
3574 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3575 {
3576 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3577 HRESULT hr;
3578
3579 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3580 iface, flags, entries, palette, outer_unknown);
3581
3582 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3583 if (SUCCEEDED(hr) && *palette)
3584 {
3585 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3586 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3587 impl->ifaceToRelease = NULL;
3588 }
3589
3590 return hr;
3591 }
3592
3593 static HRESULT WINAPI ddraw1_CreatePalette(IDirectDraw *iface, DWORD flags,
3594 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3595 {
3596 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3597 HRESULT hr;
3598
3599 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3600 iface, flags, entries, palette, outer_unknown);
3601
3602 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3603 if (SUCCEEDED(hr) && *palette)
3604 {
3605 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3606 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3607 impl->ifaceToRelease = NULL;
3608 }
3609
3610 return hr;
3611 }
3612
3613 /*****************************************************************************
3614 * IDirectDraw7::DuplicateSurface
3615 *
3616 * Duplicates a surface. The surface memory points to the same memory as
3617 * the original surface, and it's released when the last surface referencing
3618 * it is released. I guess that's beyond Wine's surface management right now
3619 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3620 * test application to implement this)
3621 *
3622 * Params:
3623 * Src: Address of the source surface
3624 * Dest: Address to write the new surface pointer to
3625 *
3626 * Returns:
3627 * See IDirectDraw7::CreateSurface
3628 *
3629 *****************************************************************************/
3630 static HRESULT WINAPI ddraw7_DuplicateSurface(IDirectDraw7 *iface,
3631 IDirectDrawSurface7 *Src, IDirectDrawSurface7 **Dest)
3632 {
3633 struct ddraw_surface *src_surface = unsafe_impl_from_IDirectDrawSurface7(Src);
3634
3635 FIXME("iface %p, src %p, dst %p partial stub!\n", iface, Src, Dest);
3636
3637 /* For now, simply create a new, independent surface */
3638 return IDirectDraw7_CreateSurface(iface, &src_surface->surface_desc, Dest, NULL);
3639 }
3640
3641 static HRESULT WINAPI ddraw4_DuplicateSurface(IDirectDraw4 *iface, IDirectDrawSurface4 *src,
3642 IDirectDrawSurface4 **dst)
3643 {
3644 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src);
3645 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3646 struct ddraw_surface *dst_impl;
3647 IDirectDrawSurface7 *dst7;
3648 HRESULT hr;
3649
3650 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3651
3652 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3653 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3654 if (FAILED(hr))
3655 {
3656 *dst = NULL;
3657 return hr;
3658 }
3659 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3660 *dst = &dst_impl->IDirectDrawSurface4_iface;
3661 IDirectDrawSurface4_AddRef(*dst);
3662 IDirectDrawSurface7_Release(dst7);
3663
3664 return hr;
3665 }
3666
3667 static HRESULT WINAPI ddraw2_DuplicateSurface(IDirectDraw2 *iface,
3668 IDirectDrawSurface *src, IDirectDrawSurface **dst)
3669 {
3670 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3671 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3672 struct ddraw_surface *dst_impl;
3673 IDirectDrawSurface7 *dst7;
3674 HRESULT hr;
3675
3676 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3677
3678 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3679 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3680 if (FAILED(hr))
3681 return hr;
3682 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3683 *dst = &dst_impl->IDirectDrawSurface_iface;
3684 IDirectDrawSurface_AddRef(*dst);
3685 IDirectDrawSurface7_Release(dst7);
3686
3687 return hr;
3688 }
3689
3690 static HRESULT WINAPI ddraw1_DuplicateSurface(IDirectDraw *iface, IDirectDrawSurface *src,
3691 IDirectDrawSurface **dst)
3692 {
3693 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3694 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3695 struct ddraw_surface *dst_impl;
3696 IDirectDrawSurface7 *dst7;
3697 HRESULT hr;
3698
3699 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3700
3701 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3702 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3703 if (FAILED(hr))
3704 return hr;
3705 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3706 *dst = &dst_impl->IDirectDrawSurface_iface;
3707 IDirectDrawSurface_AddRef(*dst);
3708 IDirectDrawSurface7_Release(dst7);
3709
3710 return hr;
3711 }
3712
3713 /*****************************************************************************
3714 * IDirect3D7::EnumDevices
3715 *
3716 * The EnumDevices method for IDirect3D7. It enumerates all supported
3717 * D3D7 devices. Currently the T&L, HAL and RGB devices are enumerated.
3718 *
3719 * Params:
3720 * callback: Function to call for each enumerated device
3721 * context: Pointer to pass back to the app
3722 *
3723 * Returns:
3724 * D3D_OK, or the return value of the GetCaps call
3725 *
3726 *****************************************************************************/
3727 static HRESULT WINAPI d3d7_EnumDevices(IDirect3D7 *iface, LPD3DENUMDEVICESCALLBACK7 callback, void *context)
3728 {
3729 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
3730 D3DDEVICEDESC7 device_desc7;
3731 DWORD dev_caps;
3732 HRESULT hr;
3733 size_t i;
3734
3735 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3736
3737 if (!callback)
3738 return DDERR_INVALIDPARAMS;
3739
3740 wined3d_mutex_lock();
3741
3742 if (FAILED(hr = ddraw_get_d3dcaps(ddraw, &device_desc7)))
3743 {
3744 wined3d_mutex_unlock();
3745 return hr;
3746 }
3747
3748 dev_caps = device_desc7.dwDevCaps;
3749
3750 for (i = 0; i < sizeof(device_list7)/sizeof(device_list7[0]); i++)
3751 {
3752 HRESULT ret;
3753
3754 device_desc7.deviceGUID = *device_list7[i].device_guid;
3755 device_desc7.dwDevCaps = dev_caps & ~device_list7[i].remove_caps;
3756
3757 ret = callback(device_list7[i].interface_name, device_list7[i].device_name, &device_desc7, context);
3758 if (ret != DDENUMRET_OK)
3759 {
3760 TRACE("Application cancelled the enumeration.\n");
3761 wined3d_mutex_unlock();
3762 return D3D_OK;
3763 }
3764 }
3765
3766 TRACE("End of enumeration.\n");
3767
3768 wined3d_mutex_unlock();
3769
3770 return D3D_OK;
3771 }
3772
3773 /*****************************************************************************
3774 * IDirect3D3::EnumDevices
3775 *
3776 * Enumerates all supported Direct3DDevice interfaces. This is the
3777 * implementation for Direct3D 1 to Direc3D 3, Version 7 has its own.
3778 *
3779 * Versions 1, 2 and 3
3780 *
3781 * Params:
3782 * callback: Application-provided routine to call for each enumerated device
3783 * Context: Pointer to pass to the callback
3784 *
3785 * Returns:
3786 * D3D_OK on success,
3787 * The result of IDirect3DImpl_GetCaps if it failed
3788 *
3789 *****************************************************************************/
3790 static HRESULT WINAPI d3d3_EnumDevices(IDirect3D3 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3791 {
3792 static CHAR wined3d_description[] = "Wine D3DDevice using WineD3D and OpenGL";
3793
3794 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3795 D3DDEVICEDESC device_desc1, hal_desc, hel_desc;
3796 D3DDEVICEDESC7 device_desc7;
3797 HRESULT hr;
3798
3799 /* Some games (Motoracer 2 demo) have the bad idea to modify the device
3800 * name string. Let's put the string in a sufficiently sized array in
3801 * writable memory. */
3802 char device_name[50];
3803 strcpy(device_name,"Direct3D HEL");
3804
3805 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3806
3807 if (!callback)
3808 return DDERR_INVALIDPARAMS;
3809
3810 wined3d_mutex_lock();
3811
3812 if (FAILED(hr = ddraw_get_d3dcaps(ddraw, &device_desc7)))
3813 {
3814 wined3d_mutex_unlock();
3815 return hr;
3816 }
3817 ddraw_d3dcaps1_from_7(&device_desc1, &device_desc7);
3818
3819 /* Do I have to enumerate the reference id? Note from old d3d7:
3820 * "It seems that enumerating the reference IID on Direct3D 1 games
3821 * (AvP / Motoracer2) breaks them". So do not enumerate this iid in V1
3822 *
3823 * There's a registry key HKLM\Software\Microsoft\Direct3D\Drivers,
3824 * EnumReference which enables / disables enumerating the reference
3825 * rasterizer. It's a DWORD, 0 means disabled, 2 means enabled. The
3826 * enablerefrast.reg and disablerefrast.reg files in the DirectX 7.0 sdk
3827 * demo directory suggest this.
3828 *
3829 * Some games(GTA 2) seem to use the second enumerated device, so I have
3830 * to enumerate at least 2 devices. So enumerate the reference device to
3831 * have 2 devices.
3832 *
3833 * Other games (Rollcage) tell emulation and hal device apart by certain
3834 * flags. Rollcage expects D3DPTEXTURECAPS_POW2 to be set (yeah, it is a
3835 * limitation flag), and it refuses all devices that have the perspective
3836 * flag set. This way it refuses the emulation device, and HAL devices
3837 * never have POW2 unset in d3d7 on windows. */
3838 if (ddraw->d3dversion != 1)
3839 {
3840 static CHAR reference_description[] = "RGB Direct3D emulation";
3841
3842 TRACE("Enumerating WineD3D D3DDevice interface.\n");
3843 hal_desc = device_desc1;
3844 hel_desc = device_desc1;
3845 /* The rgb device has the pow2 flag set in the hel caps, but not in the hal caps. */
3846 hal_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3847 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3848 hal_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3849 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3850 /* RGB, RAMP and MMX devices have a HAL dcmColorModel of 0 */
3851 hal_desc.dcmColorModel = 0;
3852 /* RGB, RAMP and MMX devices cannot report HAL hardware flags */
3853 hal_desc.dwFlags = 0;
3854
3855 hr = callback((GUID *)&IID_IDirect3DRGBDevice, reference_description,
3856 device_name, &hal_desc, &hel_desc, context);
3857 if (hr != D3DENUMRET_OK)
3858 {
3859 TRACE("Application cancelled the enumeration.\n");
3860 wined3d_mutex_unlock();
3861 return D3D_OK;
3862 }
3863 }
3864
3865 strcpy(device_name,"Direct3D HAL");
3866
3867 TRACE("Enumerating HAL Direct3D device.\n");
3868 hal_desc = device_desc1;
3869 hel_desc = device_desc1;
3870
3871 /* The hal device does not have the pow2 flag set in hel, but in hal. */
3872 hel_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3873 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3874 hel_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3875 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3876 /* HAL devices have a HEL dcmColorModel of 0 */
3877 hel_desc.dcmColorModel = 0;
3878
3879 hr = callback((GUID *)&IID_IDirect3DHALDevice, wined3d_description,
3880 device_name, &hal_desc, &hel_desc, context);
3881 if (hr != D3DENUMRET_OK)
3882 {
3883 TRACE("Application cancelled the enumeration.\n");
3884 wined3d_mutex_unlock();
3885 return D3D_OK;
3886 }
3887
3888 TRACE("End of enumeration.\n");
3889
3890 wined3d_mutex_unlock();
3891
3892 return D3D_OK;
3893 }
3894
3895 static HRESULT WINAPI d3d2_EnumDevices(IDirect3D2 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3896 {
3897 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3898
3899 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3900
3901 return d3d3_EnumDevices(&ddraw->IDirect3D3_iface, callback, context);
3902 }
3903
3904 static HRESULT WINAPI d3d1_EnumDevices(IDirect3D *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3905 {
3906 struct ddraw *ddraw = impl_from_IDirect3D(iface);
3907
3908 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3909
3910 return d3d3_EnumDevices(&ddraw->IDirect3D3_iface, callback, context);
3911 }
3912
3913 /*****************************************************************************
3914 * IDirect3D3::CreateLight
3915 *
3916 * Creates an IDirect3DLight interface. This interface is used in
3917 * Direct3D3 or earlier for lighting. In Direct3D7 it has been replaced
3918 * by the DIRECT3DLIGHT7 structure. Wine's Direct3DLight implementation
3919 * uses the IDirect3DDevice7 interface with D3D7 lights.
3920 *
3921 * Versions 1, 2 and 3
3922 *
3923 * Params:
3924 * light: Address to store the new interface pointer
3925 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
3926 * Must be NULL
3927 *
3928 * Returns:
3929 * D3D_OK on success
3930 * DDERR_OUTOFMEMORY if memory allocation failed
3931 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
3932 *
3933 *****************************************************************************/
3934 static HRESULT WINAPI d3d3_CreateLight(IDirect3D3 *iface, IDirect3DLight **light,
3935 IUnknown *outer_unknown)
3936 {
3937 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3938 struct d3d_light *object;
3939
3940 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3941
3942 if (outer_unknown) return CLASS_E_NOAGGREGATION;
3943
3944 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3945 if (!object)
3946 {
3947 ERR("Failed to allocate light memory.\n");
3948 return DDERR_OUTOFMEMORY;
3949 }
3950
3951 d3d_light_init(object, ddraw);
3952
3953 TRACE("Created light %p.\n", object);
3954 *light = &object->IDirect3DLight_iface;
3955
3956 return D3D_OK;
3957 }
3958
3959 static HRESULT WINAPI d3d2_CreateLight(IDirect3D2 *iface, IDirect3DLight **light, IUnknown *outer_unknown)
3960 {
3961 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3962
3963 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3964
3965 return d3d3_CreateLight(&ddraw->IDirect3D3_iface, light, outer_unknown);
3966 }
3967
3968 static HRESULT WINAPI d3d1_CreateLight(IDirect3D *iface, IDirect3DLight **light, IUnknown *outer_unknown)
3969 {
3970 struct ddraw *ddraw = impl_from_IDirect3D(iface);
3971
3972 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3973
3974 return d3d3_CreateLight(&ddraw->IDirect3D3_iface, light, outer_unknown);
3975 }
3976
3977 /*****************************************************************************
3978 * IDirect3D3::CreateMaterial
3979 *
3980 * Creates an IDirect3DMaterial interface. This interface is used by Direct3D3
3981 * and older versions. The IDirect3DMaterial implementation wraps its
3982 * functionality to IDirect3DDevice7::SetMaterial and friends.
3983 *
3984 * Versions 1, 2 and 3
3985 *
3986 * Params:
3987 * material: Address to store the new interface's pointer to
3988 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
3989 * Must be NULL
3990 *
3991 * Returns:
3992 * D3D_OK on success
3993 * DDERR_OUTOFMEMORY if memory allocation failed
3994 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
3995 *
3996 *****************************************************************************/
3997 static HRESULT WINAPI d3d3_CreateMaterial(IDirect3D3 *iface, IDirect3DMaterial3 **material,
3998 IUnknown *outer_unknown)
3999 {
4000 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4001 struct d3d_material *object;
4002
4003 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4004
4005 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4006
4007 object = d3d_material_create(ddraw);
4008 if (!object)
4009 {
4010 ERR("Failed to allocate material memory.\n");
4011 return DDERR_OUTOFMEMORY;
4012 }
4013
4014 TRACE("Created material %p.\n", object);
4015 *material = &object->IDirect3DMaterial3_iface;
4016
4017 return D3D_OK;
4018 }
4019
4020 static HRESULT WINAPI d3d2_CreateMaterial(IDirect3D2 *iface, IDirect3DMaterial2 **material,
4021 IUnknown *outer_unknown)
4022 {
4023 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4024 struct d3d_material *object;
4025
4026 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4027
4028 object = d3d_material_create(ddraw);
4029 if (!object)
4030 {
4031 ERR("Failed to allocate material memory.\n");
4032 return DDERR_OUTOFMEMORY;
4033 }
4034
4035 TRACE("Created material %p.\n", object);
4036 *material = &object->IDirect3DMaterial2_iface;
4037
4038 return D3D_OK;
4039 }
4040
4041 static HRESULT WINAPI d3d1_CreateMaterial(IDirect3D *iface, IDirect3DMaterial **material,
4042 IUnknown *outer_unknown)
4043 {
4044 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4045 struct d3d_material *object;
4046
4047 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4048
4049 object = d3d_material_create(ddraw);
4050 if (!object)
4051 {
4052 ERR("Failed to allocate material memory.\n");
4053 return DDERR_OUTOFMEMORY;
4054 }
4055
4056 TRACE("Created material %p.\n", object);
4057 *material = &object->IDirect3DMaterial_iface;
4058
4059 return D3D_OK;
4060 }
4061
4062 /*****************************************************************************
4063 * IDirect3D3::CreateViewport
4064 *
4065 * Creates an IDirect3DViewport interface. This interface is used
4066 * by Direct3D and earlier versions for Viewport management. In Direct3D7
4067 * it has been replaced by a viewport structure and
4068 * IDirect3DDevice7::*Viewport. Wine's IDirect3DViewport implementation
4069 * uses the IDirect3DDevice7 methods for its functionality
4070 *
4071 * Params:
4072 * Viewport: Address to store the new interface pointer
4073 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4074 * Must be NULL
4075 *
4076 * Returns:
4077 * D3D_OK on success
4078 * DDERR_OUTOFMEMORY if memory allocation failed
4079 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4080 *
4081 *****************************************************************************/
4082 static HRESULT WINAPI d3d3_CreateViewport(IDirect3D3 *iface, IDirect3DViewport3 **viewport,
4083 IUnknown *outer_unknown)
4084 {
4085 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4086 struct d3d_viewport *object;
4087
4088 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4089
4090 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4091
4092 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4093 if (!object)
4094 {
4095 ERR("Failed to allocate viewport memory.\n");
4096 return DDERR_OUTOFMEMORY;
4097 }
4098
4099 d3d_viewport_init(object, ddraw);
4100
4101 TRACE("Created viewport %p.\n", object);
4102 *viewport = &object->IDirect3DViewport3_iface;
4103
4104 return D3D_OK;
4105 }
4106
4107 static HRESULT WINAPI d3d2_CreateViewport(IDirect3D2 *iface, IDirect3DViewport2 **viewport, IUnknown *outer_unknown)
4108 {
4109 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4110
4111 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4112
4113 return d3d3_CreateViewport(&ddraw->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4114 outer_unknown);
4115 }
4116
4117 static HRESULT WINAPI d3d1_CreateViewport(IDirect3D *iface, IDirect3DViewport **viewport, IUnknown *outer_unknown)
4118 {
4119 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4120
4121 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4122
4123 return d3d3_CreateViewport(&ddraw->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4124 outer_unknown);
4125 }
4126
4127 /*****************************************************************************
4128 * IDirect3D3::FindDevice
4129 *
4130 * This method finds a device with the requested properties and returns a
4131 * device description
4132 *
4133 * Versions 1, 2 and 3
4134 * Params:
4135 * fds: Describes the requested device characteristics
4136 * fdr: Returns the device description
4137 *
4138 * Returns:
4139 * D3D_OK on success
4140 * DDERR_INVALIDPARAMS if no device was found
4141 *
4142 *****************************************************************************/
4143 static HRESULT WINAPI d3d3_FindDevice(IDirect3D3 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4144 {
4145 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4146 D3DDEVICEDESC7 desc7;
4147 D3DDEVICEDESC desc1;
4148 HRESULT hr;
4149
4150 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4151
4152 if (!fds || !fdr) return DDERR_INVALIDPARAMS;
4153
4154 if (fds->dwSize != sizeof(D3DFINDDEVICESEARCH) || (fdr->dwSize != sizeof(D3DFINDDEVICERESULT1) &&
4155 fdr->dwSize != sizeof(D3DFINDDEVICERESULT2) && fdr->dwSize != sizeof(D3DFINDDEVICERESULT)))
4156 return DDERR_INVALIDPARAMS;
4157
4158 if ((fds->dwFlags & D3DFDS_COLORMODEL)
4159 && fds->dcmColorModel != D3DCOLOR_RGB)
4160 {
4161 WARN("Trying to request a non-RGB D3D color model. Not supported.\n");
4162 return DDERR_INVALIDPARAMS; /* No real idea what to return here :-) */
4163 }
4164
4165 if (fds->dwFlags & D3DFDS_GUID)
4166 {
4167 TRACE("Trying to match guid %s.\n", debugstr_guid(&(fds->guid)));
4168 if (!IsEqualGUID(&IID_D3DDEVICE_WineD3D, &fds->guid)
4169 && !IsEqualGUID(&IID_IDirect3DHALDevice, &fds->guid)
4170 && !IsEqualGUID(&IID_IDirect3DRGBDevice, &fds->guid))
4171 {
4172 WARN("No match for this GUID.\n");
4173 return DDERR_NOTFOUND;
4174 }
4175 }
4176
4177 /* Get the caps */
4178 if (FAILED(hr = ddraw_get_d3dcaps(ddraw, &desc7)))
4179 return hr;
4180
4181 /* Now return our own GUID */
4182 ddraw_d3dcaps1_from_7(&desc1, &desc7);
4183 fdr->guid = IID_D3DDEVICE_WineD3D;
4184
4185 if (fdr->dwSize == sizeof(D3DFINDDEVICERESULT1))
4186 {
4187 D3DFINDDEVICERESULT1 *fdr1 = (D3DFINDDEVICERESULT1 *)fdr;
4188 memcpy(&fdr1->ddHwDesc, &desc1, sizeof(fdr1->ddHwDesc));
4189 memcpy(&fdr1->ddSwDesc, &desc1, sizeof(fdr1->ddSwDesc));
4190 }
4191 else if (fdr->dwSize == sizeof(D3DFINDDEVICERESULT2))
4192 {
4193 D3DFINDDEVICERESULT2 *fdr2 = (D3DFINDDEVICERESULT2 *)fdr;
4194 memcpy(&fdr2->ddHwDesc, &desc1, sizeof(fdr2->ddHwDesc));
4195 memcpy(&fdr2->ddSwDesc, &desc1, sizeof(fdr2->ddSwDesc));
4196 }
4197 else
4198 {
4199 fdr->ddHwDesc = desc1;
4200 fdr->ddSwDesc = desc1;
4201 }
4202
4203 TRACE("Returning Wine's wined3d device with (undumped) capabilities.\n");
4204
4205 return D3D_OK;
4206 }
4207
4208 static HRESULT WINAPI d3d2_FindDevice(IDirect3D2 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4209 {
4210 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4211
4212 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4213
4214 return d3d3_FindDevice(&ddraw->IDirect3D3_iface, fds, fdr);
4215 }
4216
4217 static HRESULT WINAPI d3d1_FindDevice(IDirect3D *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4218 {
4219 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4220
4221 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4222
4223 return d3d3_FindDevice(&ddraw->IDirect3D3_iface, fds, fdr);
4224 }
4225
4226 /*****************************************************************************
4227 * IDirect3D7::CreateDevice
4228 *
4229 * Creates an IDirect3DDevice7 interface.
4230 *
4231 * Versions 2, 3 and 7. IDirect3DDevice 1 interfaces are interfaces to
4232 * DirectDraw surfaces and are created with
4233 * IDirectDrawSurface::QueryInterface. This method uses CreateDevice to
4234 * create the device object and QueryInterfaces for IDirect3DDevice
4235 *
4236 * Params:
4237 * refiid: IID of the device to create
4238 * Surface: Initial rendertarget
4239 * Device: Address to return the interface pointer
4240 *
4241 * Returns:
4242 * D3D_OK on success
4243 * DDERR_OUTOFMEMORY if memory allocation failed
4244 * DDERR_INVALIDPARAMS if a device exists already
4245 *
4246 *****************************************************************************/
4247 static HRESULT WINAPI d3d7_CreateDevice(IDirect3D7 *iface, REFCLSID riid,
4248 IDirectDrawSurface7 *surface, IDirect3DDevice7 **device)
4249 {
4250 struct ddraw_surface *target = unsafe_impl_from_IDirectDrawSurface7(surface);
4251 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4252 struct d3d_device *object;
4253 HRESULT hr;
4254
4255 TRACE("iface %p, riid %s, surface %p, device %p.\n", iface, debugstr_guid(riid), surface, device);
4256
4257 wined3d_mutex_lock();
4258 if (SUCCEEDED(hr = d3d_device_create(ddraw, riid, target, (IUnknown *)surface, 7, &object, NULL)))
4259 {
4260 *device = &object->IDirect3DDevice7_iface;
4261 }
4262 else
4263 {
4264 WARN("Failed to create device, hr %#x.\n", hr);
4265 *device = NULL;
4266 }
4267 wined3d_mutex_unlock();
4268
4269 return hr;
4270 }
4271
4272 static HRESULT WINAPI d3d3_CreateDevice(IDirect3D3 *iface, REFCLSID riid,
4273 IDirectDrawSurface4 *surface, IDirect3DDevice3 **device, IUnknown *outer_unknown)
4274 {
4275 struct ddraw_surface *surface_impl = unsafe_impl_from_IDirectDrawSurface4(surface);
4276 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4277 struct d3d_device *device_impl;
4278 HRESULT hr;
4279
4280 TRACE("iface %p, riid %s, surface %p, device %p, outer_unknown %p.\n",
4281 iface, debugstr_guid(riid), surface, device, outer_unknown);
4282
4283 if (outer_unknown)
4284 return CLASS_E_NOAGGREGATION;
4285
4286 wined3d_mutex_lock();
4287 if (SUCCEEDED(hr = d3d_device_create(ddraw, riid, surface_impl, (IUnknown *)surface, 3, &device_impl, NULL)))
4288 {
4289 *device = &device_impl->IDirect3DDevice3_iface;
4290 }
4291 else
4292 {
4293 WARN("Failed to create device, hr %#x.\n", hr);
4294 *device = NULL;
4295 }
4296 wined3d_mutex_unlock();
4297
4298 return hr;
4299 }
4300
4301 static HRESULT WINAPI d3d2_CreateDevice(IDirect3D2 *iface, REFCLSID riid,
4302 IDirectDrawSurface *surface, IDirect3DDevice2 **device)
4303 {
4304 struct ddraw_surface *surface_impl = unsafe_impl_from_IDirectDrawSurface(surface);
4305 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4306 struct d3d_device *device_impl;
4307 HRESULT hr;
4308
4309 TRACE("iface %p, riid %s, surface %p, device %p.\n",
4310 iface, debugstr_guid(riid), surface, device);
4311
4312 wined3d_mutex_lock();
4313 if (SUCCEEDED(hr = d3d_device_create(ddraw, riid, surface_impl, (IUnknown *)surface, 2, &device_impl, NULL)))
4314 {
4315 *device = &device_impl->IDirect3DDevice2_iface;
4316 }
4317 else
4318 {
4319 WARN("Failed to create device, hr %#x.\n", hr);
4320 *device = NULL;
4321 }
4322 wined3d_mutex_unlock();
4323
4324 return hr;
4325 }
4326
4327 /*****************************************************************************
4328 * IDirect3D7::CreateVertexBuffer
4329 *
4330 * Creates a new vertex buffer object and returns a IDirect3DVertexBuffer7
4331 * interface.
4332 *
4333 * Versions 3 and 7
4334 *
4335 * Params:
4336 * desc: Requested Vertex buffer properties
4337 * vertex_buffer: Address to return the interface pointer at
4338 * flags: Some flags, should be 0
4339 *
4340 * Returns
4341 * D3D_OK on success
4342 * DDERR_OUTOFMEMORY if memory allocation failed
4343 * DDERR_INVALIDPARAMS if desc or vertex_buffer is NULL
4344 *
4345 *****************************************************************************/
4346 static HRESULT WINAPI d3d7_CreateVertexBuffer(IDirect3D7 *iface, D3DVERTEXBUFFERDESC *desc,
4347 IDirect3DVertexBuffer7 **vertex_buffer, DWORD flags)
4348 {
4349 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4350 struct d3d_vertex_buffer *object;
4351 HRESULT hr;
4352
4353 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x.\n",
4354 iface, desc, vertex_buffer, flags);
4355
4356 if (!vertex_buffer || !desc) return DDERR_INVALIDPARAMS;
4357
4358 hr = d3d_vertex_buffer_create(&object, ddraw, desc);
4359 if (hr == D3D_OK)
4360 {
4361 TRACE("Created vertex buffer %p.\n", object);
4362 *vertex_buffer = &object->IDirect3DVertexBuffer7_iface;
4363 }
4364 else
4365 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4366
4367 return hr;
4368 }
4369
4370 static HRESULT WINAPI d3d3_CreateVertexBuffer(IDirect3D3 *iface, D3DVERTEXBUFFERDESC *desc,
4371 IDirect3DVertexBuffer **vertex_buffer, DWORD flags, IUnknown *outer_unknown)
4372 {
4373 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4374 struct d3d_vertex_buffer *object;
4375 HRESULT hr;
4376
4377 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x, outer_unknown %p.\n",
4378 iface, desc, vertex_buffer, flags, outer_unknown);
4379
4380 if (outer_unknown)
4381 return CLASS_E_NOAGGREGATION;
4382 if (!vertex_buffer || !desc)
4383 return DDERR_INVALIDPARAMS;
4384
4385 hr = d3d_vertex_buffer_create(&object, ddraw, desc);
4386 if (hr == D3D_OK)
4387 {
4388 TRACE("Created vertex buffer %p.\n", object);
4389 *vertex_buffer = (IDirect3DVertexBuffer *)&object->IDirect3DVertexBuffer7_iface;
4390 }
4391 else
4392 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4393
4394 return hr;
4395 }
4396
4397 /*****************************************************************************
4398 * IDirect3D7::EnumZBufferFormats
4399 *
4400 * Enumerates all supported Z buffer pixel formats
4401 *
4402 * Versions 3 and 7
4403 *
4404 * Params:
4405 * device_iid:
4406 * callback: callback to call for each pixel format
4407 * context: Pointer to pass back to the callback
4408 *
4409 * Returns:
4410 * D3D_OK on success
4411 * DDERR_INVALIDPARAMS if callback is NULL
4412 *
4413 *****************************************************************************/
4414 static HRESULT WINAPI d3d7_EnumZBufferFormats(IDirect3D7 *iface, REFCLSID device_iid,
4415 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4416 {
4417 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4418 struct wined3d_display_mode mode;
4419 enum wined3d_device_type type;
4420 unsigned int i;
4421 HRESULT hr;
4422
4423 /* Order matters. Specifically, BattleZone II (full version) expects the
4424 * 16-bit depth formats to be listed before the 24 and 32 ones. */
4425 static const enum wined3d_format_id formats[] =
4426 {
4427 WINED3DFMT_S1_UINT_D15_UNORM,
4428 WINED3DFMT_D16_UNORM,
4429 WINED3DFMT_X8D24_UNORM,
4430 WINED3DFMT_S4X4_UINT_D24_UNORM,
4431 WINED3DFMT_D24_UNORM_S8_UINT,
4432 WINED3DFMT_D32_UNORM,
4433 };
4434
4435 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4436 iface, debugstr_guid(device_iid), callback, context);
4437
4438 if (!callback) return DDERR_INVALIDPARAMS;
4439
4440 if (IsEqualGUID(device_iid, &IID_IDirect3DHALDevice)
4441 || IsEqualGUID(device_iid, &IID_IDirect3DTnLHalDevice)
4442 || IsEqualGUID(device_iid, &IID_D3DDEVICE_WineD3D))
4443 {
4444 TRACE("Asked for HAL device.\n");
4445 type = WINED3D_DEVICE_TYPE_HAL;
4446 }
4447 else if (IsEqualGUID(device_iid, &IID_IDirect3DRGBDevice)
4448 || IsEqualGUID(device_iid, &IID_IDirect3DMMXDevice))
4449 {
4450 TRACE("Asked for SW device.\n");
4451 type = WINED3D_DEVICE_TYPE_SW;
4452 }
4453 else if (IsEqualGUID(device_iid, &IID_IDirect3DRefDevice))
4454 {
4455 TRACE("Asked for REF device.\n");
4456 type = WINED3D_DEVICE_TYPE_REF;
4457 }
4458 else if (IsEqualGUID(device_iid, &IID_IDirect3DNullDevice))
4459 {
4460 TRACE("Asked for NULLREF device.\n");
4461 type = WINED3D_DEVICE_TYPE_NULLREF;
4462 }
4463 else
4464 {
4465 FIXME("Unexpected device GUID %s.\n", debugstr_guid(device_iid));
4466 type = WINED3D_DEVICE_TYPE_HAL;
4467 }
4468
4469 wined3d_mutex_lock();
4470 /* We need an adapter format from somewhere to please wined3d and WGL.
4471 * Use the current display mode. So far all cards offer the same depth
4472 * stencil format for all modes, but if some do not and applications do
4473 * not like that we'll have to find some workaround, like iterating over
4474 * all imaginable formats and collecting all the depth stencil formats we
4475 * can get. */
4476 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
4477 {
4478 ERR("Failed to get display mode, hr %#x.\n", hr);
4479 wined3d_mutex_unlock();
4480 return hr;
4481 }
4482
4483 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); ++i)
4484 {
4485 if (SUCCEEDED(wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, type, mode.format_id,
4486 WINED3DUSAGE_DEPTHSTENCIL, WINED3D_RTYPE_TEXTURE_2D, formats[i])))
4487 {
4488 DDPIXELFORMAT pformat;
4489
4490 memset(&pformat, 0, sizeof(pformat));
4491 pformat.dwSize = sizeof(pformat);
4492 ddrawformat_from_wined3dformat(&pformat, formats[i]);
4493
4494 TRACE("Enumerating wined3d format %#x.\n", formats[i]);
4495 hr = callback(&pformat, context);
4496 if (hr != DDENUMRET_OK)
4497 {
4498 TRACE("Format enumeration cancelled by application.\n");
4499 wined3d_mutex_unlock();
4500 return D3D_OK;
4501 }
4502 }
4503 }
4504
4505 /* Historically some windows drivers used dwZBufferBitDepth=24 for WINED3DFMT_X8D24_UNORM,
4506 * while others used dwZBufferBitDepth=32. In either case the pitch matches a 32 bits per
4507 * pixel format, so we use dwZBufferBitDepth=32. Some games expect 24. Windows Vista and
4508 * newer enumerate both versions, so we do the same(bug 22434) */
4509 if (SUCCEEDED(wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, type, mode.format_id,
4510 WINED3DUSAGE_DEPTHSTENCIL, WINED3D_RTYPE_TEXTURE_2D, WINED3DFMT_X8D24_UNORM)))
4511 {
4512 DDPIXELFORMAT x8d24 =
4513 {
4514 sizeof(x8d24), DDPF_ZBUFFER, 0,
4515 {24}, {0x00000000}, {0x00ffffff}, {0x00000000}
4516 };
4517 TRACE("Enumerating WINED3DFMT_X8D24_UNORM, dwZBufferBitDepth=24 version\n");
4518 callback(&x8d24, context);
4519 }
4520
4521 TRACE("End of enumeration.\n");
4522
4523 wined3d_mutex_unlock();
4524
4525 return D3D_OK;
4526 }
4527
4528 static HRESULT WINAPI d3d3_EnumZBufferFormats(IDirect3D3 *iface, REFCLSID device_iid,
4529 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4530 {
4531 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4532
4533 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4534 iface, debugstr_guid(device_iid), callback, context);
4535
4536 return d3d7_EnumZBufferFormats(&ddraw->IDirect3D7_iface, device_iid, callback, context);
4537 }
4538
4539 /*****************************************************************************
4540 * IDirect3D7::EvictManagedTextures
4541 *
4542 * Removes all managed textures (=surfaces with DDSCAPS2_TEXTUREMANAGE or
4543 * DDSCAPS2_D3DTEXTUREMANAGE caps) to be removed from video memory.
4544 *
4545 * Versions 3 and 7
4546 *
4547 * Returns:
4548 * D3D_OK, because it's a stub
4549 *
4550 *****************************************************************************/
4551 static HRESULT WINAPI d3d7_EvictManagedTextures(IDirect3D7 *iface)
4552 {
4553 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4554
4555 TRACE("iface %p!\n", iface);
4556
4557 wined3d_mutex_lock();
4558 if (ddraw->flags & DDRAW_D3D_INITIALIZED)
4559 wined3d_device_evict_managed_resources(ddraw->wined3d_device);
4560 wined3d_mutex_unlock();
4561
4562 return D3D_OK;
4563 }
4564
4565 static HRESULT WINAPI d3d3_EvictManagedTextures(IDirect3D3 *iface)
4566 {
4567 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4568
4569 TRACE("iface %p.\n", iface);
4570
4571 return d3d7_EvictManagedTextures(&ddraw->IDirect3D7_iface);
4572 }
4573
4574 /*****************************************************************************
4575 * IDirectDraw7 VTable
4576 *****************************************************************************/
4577 static const struct IDirectDraw7Vtbl ddraw7_vtbl =
4578 {
4579 /* IUnknown */
4580 ddraw7_QueryInterface,
4581 ddraw7_AddRef,
4582 ddraw7_Release,
4583 /* IDirectDraw */
4584 ddraw7_Compact,
4585 ddraw7_CreateClipper,
4586 ddraw7_CreatePalette,
4587 ddraw7_CreateSurface,
4588 ddraw7_DuplicateSurface,
4589 ddraw7_EnumDisplayModes,
4590 ddraw7_EnumSurfaces,
4591 ddraw7_FlipToGDISurface,
4592 ddraw7_GetCaps,
4593 ddraw7_GetDisplayMode,
4594 ddraw7_GetFourCCCodes,
4595 ddraw7_GetGDISurface,
4596 ddraw7_GetMonitorFrequency,
4597 ddraw7_GetScanLine,
4598 ddraw7_GetVerticalBlankStatus,
4599 ddraw7_Initialize,
4600 ddraw7_RestoreDisplayMode,
4601 ddraw7_SetCooperativeLevel,
4602 ddraw7_SetDisplayMode,
4603 ddraw7_WaitForVerticalBlank,
4604 /* IDirectDraw2 */
4605 ddraw7_GetAvailableVidMem,
4606 /* IDirectDraw3 */
4607 ddraw7_GetSurfaceFromDC,
4608 /* IDirectDraw4 */
4609 ddraw7_RestoreAllSurfaces,
4610 ddraw7_TestCooperativeLevel,
4611 ddraw7_GetDeviceIdentifier,
4612 /* IDirectDraw7 */
4613 ddraw7_StartModeTest,
4614 ddraw7_EvaluateMode
4615 };
4616
4617 static const struct IDirectDraw4Vtbl ddraw4_vtbl =
4618 {
4619 /* IUnknown */
4620 ddraw4_QueryInterface,
4621 ddraw4_AddRef,
4622 ddraw4_Release,
4623 /* IDirectDraw */
4624 ddraw4_Compact,
4625 ddraw4_CreateClipper,
4626 ddraw4_CreatePalette,
4627 ddraw4_CreateSurface,
4628 ddraw4_DuplicateSurface,
4629 ddraw4_EnumDisplayModes,
4630 ddraw4_EnumSurfaces,
4631 ddraw4_FlipToGDISurface,
4632 ddraw4_GetCaps,
4633 ddraw4_GetDisplayMode,
4634 ddraw4_GetFourCCCodes,
4635 ddraw4_GetGDISurface,
4636 ddraw4_GetMonitorFrequency,
4637 ddraw4_GetScanLine,
4638 ddraw4_GetVerticalBlankStatus,
4639 ddraw4_Initialize,
4640 ddraw4_RestoreDisplayMode,
4641 ddraw4_SetCooperativeLevel,
4642 ddraw4_SetDisplayMode,
4643 ddraw4_WaitForVerticalBlank,
4644 /* IDirectDraw2 */
4645 ddraw4_GetAvailableVidMem,
4646 /* IDirectDraw3 */
4647 ddraw4_GetSurfaceFromDC,
4648 /* IDirectDraw4 */
4649 ddraw4_RestoreAllSurfaces,
4650 ddraw4_TestCooperativeLevel,
4651 ddraw4_GetDeviceIdentifier,
4652 };
4653
4654 static const struct IDirectDraw2Vtbl ddraw2_vtbl =
4655 {
4656 /* IUnknown */
4657 ddraw2_QueryInterface,
4658 ddraw2_AddRef,
4659 ddraw2_Release,
4660 /* IDirectDraw */
4661 ddraw2_Compact,
4662 ddraw2_CreateClipper,
4663 ddraw2_CreatePalette,
4664 ddraw2_CreateSurface,
4665 ddraw2_DuplicateSurface,
4666 ddraw2_EnumDisplayModes,
4667 ddraw2_EnumSurfaces,
4668 ddraw2_FlipToGDISurface,
4669 ddraw2_GetCaps,
4670 ddraw2_GetDisplayMode,
4671 ddraw2_GetFourCCCodes,
4672 ddraw2_GetGDISurface,
4673 ddraw2_GetMonitorFrequency,
4674 ddraw2_GetScanLine,
4675 ddraw2_GetVerticalBlankStatus,
4676 ddraw2_Initialize,
4677 ddraw2_RestoreDisplayMode,
4678 ddraw2_SetCooperativeLevel,
4679 ddraw2_SetDisplayMode,
4680 ddraw2_WaitForVerticalBlank,
4681 /* IDirectDraw2 */
4682 ddraw2_GetAvailableVidMem,
4683 };
4684
4685 static struct IDirectDrawVtbl ddraw1_vtbl =
4686 {
4687 /* IUnknown */
4688 ddraw1_QueryInterface,
4689 ddraw1_AddRef,
4690 ddraw1_Release,
4691 /* IDirectDraw */
4692 ddraw1_Compact,
4693 ddraw1_CreateClipper,
4694 ddraw1_CreatePalette,
4695 ddraw1_CreateSurface,
4696 ddraw1_DuplicateSurface,
4697 ddraw1_EnumDisplayModes,
4698 ddraw1_EnumSurfaces,
4699 ddraw1_FlipToGDISurface,
4700 ddraw1_GetCaps,
4701 ddraw1_GetDisplayMode,
4702 ddraw1_GetFourCCCodes,
4703 ddraw1_GetGDISurface,
4704 ddraw1_GetMonitorFrequency,
4705 ddraw1_GetScanLine,
4706 ddraw1_GetVerticalBlankStatus,
4707 ddraw1_Initialize,
4708 ddraw1_RestoreDisplayMode,
4709 ddraw1_SetCooperativeLevel,
4710 ddraw1_SetDisplayMode,
4711 ddraw1_WaitForVerticalBlank,
4712 };
4713
4714 static const struct IDirect3D7Vtbl d3d7_vtbl =
4715 {
4716 /* IUnknown methods */
4717 d3d7_QueryInterface,
4718 d3d7_AddRef,
4719 d3d7_Release,
4720 /* IDirect3D7 methods */
4721 d3d7_EnumDevices,
4722 d3d7_CreateDevice,
4723 d3d7_CreateVertexBuffer,
4724 d3d7_EnumZBufferFormats,
4725 d3d7_EvictManagedTextures
4726 };
4727
4728 static const struct IDirect3D3Vtbl d3d3_vtbl =
4729 {
4730 /* IUnknown methods */
4731 d3d3_QueryInterface,
4732 d3d3_AddRef,
4733 d3d3_Release,
4734 /* IDirect3D3 methods */
4735 d3d3_EnumDevices,
4736 d3d3_CreateLight,
4737 d3d3_CreateMaterial,
4738 d3d3_CreateViewport,
4739 d3d3_FindDevice,
4740 d3d3_CreateDevice,
4741 d3d3_CreateVertexBuffer,
4742 d3d3_EnumZBufferFormats,
4743 d3d3_EvictManagedTextures
4744 };
4745
4746 static const struct IDirect3D2Vtbl d3d2_vtbl =
4747 {
4748 /* IUnknown methods */
4749 d3d2_QueryInterface,
4750 d3d2_AddRef,
4751 d3d2_Release,
4752 /* IDirect3D2 methods */
4753 d3d2_EnumDevices,
4754 d3d2_CreateLight,
4755 d3d2_CreateMaterial,
4756 d3d2_CreateViewport,
4757 d3d2_FindDevice,
4758 d3d2_CreateDevice
4759 };
4760
4761 static const struct IDirect3DVtbl d3d1_vtbl =
4762 {
4763 /* IUnknown methods */
4764 d3d1_QueryInterface,
4765 d3d1_AddRef,
4766 d3d1_Release,
4767 /* IDirect3D methods */
4768 d3d1_Initialize,
4769 d3d1_EnumDevices,
4770 d3d1_CreateLight,
4771 d3d1_CreateMaterial,
4772 d3d1_CreateViewport,
4773 d3d1_FindDevice
4774 };
4775
4776 /*****************************************************************************
4777 * ddraw_find_decl
4778 *
4779 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
4780 * if none was found.
4781 *
4782 * This function is in ddraw.c and the DDraw object space because D3D7
4783 * vertex buffers are created using the IDirect3D interface to the ddraw
4784 * object, so they can be valid across D3D devices(theoretically. The ddraw
4785 * object also owns the wined3d device
4786 *
4787 * Parameters:
4788 * This: Device
4789 * fvf: Fvf to find the decl for
4790 *
4791 * Returns:
4792 * NULL in case of an error, the vertex declaration for the FVF otherwise.
4793 *
4794 *****************************************************************************/
4795 struct wined3d_vertex_declaration *ddraw_find_decl(struct ddraw *This, DWORD fvf)
4796 {
4797 struct wined3d_vertex_declaration *pDecl = NULL;
4798 HRESULT hr;
4799 int p, low, high; /* deliberately signed */
4800 struct FvfToDecl *convertedDecls = This->decls;
4801
4802 TRACE("Searching for declaration for fvf %08x... ", fvf);
4803
4804 low = 0;
4805 high = This->numConvertedDecls - 1;
4806 while(low <= high) {
4807 p = (low + high) >> 1;
4808 TRACE("%d ", p);
4809 if(convertedDecls[p].fvf == fvf) {
4810 TRACE("found %p\n", convertedDecls[p].decl);
4811 return convertedDecls[p].decl;
4812 } else if(convertedDecls[p].fvf < fvf) {
4813 low = p + 1;
4814 } else {
4815 high = p - 1;
4816 }
4817 }
4818 TRACE("not found. Creating and inserting at position %d.\n", low);
4819
4820 hr = wined3d_vertex_declaration_create_from_fvf(This->wined3d_device,
4821 fvf, This, &ddraw_null_wined3d_parent_ops, &pDecl);
4822 if (hr != S_OK) return NULL;
4823
4824 if(This->declArraySize == This->numConvertedDecls) {
4825 int grow = max(This->declArraySize / 2, 8);
4826 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
4827 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
4828 if (!convertedDecls)
4829 {
4830 wined3d_vertex_declaration_decref(pDecl);
4831 return NULL;
4832 }
4833 This->decls = convertedDecls;
4834 This->declArraySize += grow;
4835 }
4836
4837 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
4838 convertedDecls[low].decl = pDecl;
4839 convertedDecls[low].fvf = fvf;
4840 This->numConvertedDecls++;
4841
4842 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
4843 return pDecl;
4844 }
4845
4846 static inline struct ddraw *ddraw_from_device_parent(struct wined3d_device_parent *device_parent)
4847 {
4848 return CONTAINING_RECORD(device_parent, struct ddraw, device_parent);
4849 }
4850
4851 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
4852 struct wined3d_device *device)
4853 {
4854 TRACE("device_parent %p, device %p.\n", device_parent, device);
4855 }
4856
4857 /* This is run from device_process_message() in wined3d, we can't take the
4858 * wined3d mutex. */
4859 /* FIXME: We only get mode change notifications in exclusive mode, but we
4860 * should mark surfaces as lost on mode changes in DDSCL_NORMAL mode as well. */
4861 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
4862 {
4863 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4864 MONITORINFO monitor_info;
4865 HMONITOR monitor;
4866 RECT *r;
4867
4868 TRACE("device_parent %p.\n", device_parent);
4869
4870 if (!(ddraw->cooperative_level & DDSCL_EXCLUSIVE) || !ddraw->swapchain_window)
4871 {
4872 TRACE("Nothing to resize.\n");
4873 return;
4874 }
4875
4876 monitor = MonitorFromWindow(ddraw->swapchain_window, MONITOR_DEFAULTTOPRIMARY);
4877 monitor_info.cbSize = sizeof(monitor_info);
4878 if (!GetMonitorInfoW(monitor, &monitor_info))
4879 {
4880 ERR("Failed to get monitor info.\n");
4881 return;
4882 }
4883
4884 r = &monitor_info.rcMonitor;
4885 TRACE("Resizing window %p to %s.\n", ddraw->swapchain_window, wine_dbgstr_rect(r));
4886
4887 if (!SetWindowPos(ddraw->swapchain_window, HWND_TOP, r->left, r->top,
4888 r->right - r->left, r->bottom - r->top, SWP_SHOWWINDOW | SWP_NOACTIVATE))
4889 ERR("Failed to resize window.\n");
4890
4891 InterlockedCompareExchange(&ddraw->device_state, DDRAW_DEVICE_STATE_NOT_RESTORED, DDRAW_DEVICE_STATE_OK);
4892 }
4893
4894 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
4895 {
4896 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4897
4898 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
4899
4900 if (!activate)
4901 {
4902 ddraw->device_state = DDRAW_DEVICE_STATE_LOST;
4903 exclusive_window = NULL;
4904 }
4905 else
4906 {
4907 InterlockedCompareExchange(&ddraw->device_state, DDRAW_DEVICE_STATE_NOT_RESTORED, DDRAW_DEVICE_STATE_LOST);
4908 }
4909 }
4910
4911 void ddraw_update_lost_surfaces(struct ddraw *ddraw)
4912 {
4913 struct ddraw_surface *surface;
4914
4915 if (ddraw->device_state != DDRAW_DEVICE_STATE_NOT_RESTORED)
4916 return;
4917
4918 LIST_FOR_EACH_ENTRY(surface, &ddraw->surface_list, struct ddraw_surface, surface_list_entry)
4919 {
4920 surface->is_lost = TRUE;
4921 }
4922 ddraw->device_state = DDRAW_DEVICE_STATE_OK;
4923 }
4924
4925 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
4926 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
4927 void **parent, const struct wined3d_parent_ops **parent_ops)
4928 {
4929 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4930 struct ddraw_surface *ddraw_surface;
4931
4932 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
4933 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
4934
4935 /* We have a swapchain or wined3d internal texture. */
4936 if (!wined3d_texture_get_parent(wined3d_texture) || wined3d_texture_get_parent(wined3d_texture) == ddraw)
4937 {
4938 *parent = NULL;
4939 *parent_ops = &ddraw_null_wined3d_parent_ops;
4940
4941 return DD_OK;
4942 }
4943
4944 if (!(ddraw_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw_surface))))
4945 {
4946 ERR("Failed to allocate surface memory.\n");
4947 return DDERR_OUTOFVIDEOMEMORY;
4948 }
4949
4950 ddraw_surface_init(ddraw_surface, ddraw, wined3d_texture, sub_resource_idx, parent_ops);
4951 *parent = ddraw_surface;
4952
4953 ddraw_update_lost_surfaces(ddraw);
4954 list_add_head(&ddraw->surface_list, &ddraw_surface->surface_list_entry);
4955
4956 TRACE("Created ddraw surface %p.\n", ddraw_surface);
4957
4958 return DD_OK;
4959 }
4960
4961 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
4962 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
4963 void **parent, const struct wined3d_parent_ops **parent_ops)
4964 {
4965 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
4966 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
4967
4968 *parent = NULL;
4969 *parent_ops = &ddraw_null_wined3d_parent_ops;
4970
4971 return DD_OK;
4972 }
4973
4974 static void STDMETHODCALLTYPE ddraw_frontbuffer_destroyed(void *parent)
4975 {
4976 struct ddraw *ddraw = parent;
4977 ddraw->wined3d_frontbuffer = NULL;
4978 }
4979
4980 static const struct wined3d_parent_ops ddraw_frontbuffer_parent_ops =
4981 {
4982 ddraw_frontbuffer_destroyed,
4983 };
4984
4985 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
4986 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
4987 struct wined3d_texture **texture)
4988 {
4989 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4990 HRESULT hr;
4991
4992 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
4993 device_parent, container_parent, desc, texture_flags, texture);
4994
4995 if (ddraw->wined3d_frontbuffer)
4996 {
4997 ERR("Frontbuffer already created.\n");
4998 return E_FAIL;
4999 }
5000
5001 if (FAILED(hr = wined3d_texture_create(ddraw->wined3d_device, desc, 1, 1,
5002 texture_flags | WINED3D_TEXTURE_CREATE_MAPPABLE, NULL, ddraw, &ddraw_frontbuffer_parent_ops, texture)))
5003 {
5004 WARN("Failed to create texture, hr %#x.\n", hr);
5005 return hr;
5006 }
5007
5008 ddraw->wined3d_frontbuffer = *texture;
5009
5010 return hr;
5011 }
5012
5013 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
5014 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
5015 {
5016 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
5017 HRESULT hr;
5018
5019 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
5020
5021 if (ddraw->wined3d_swapchain)
5022 {
5023 ERR("Swapchain already created.\n");
5024 return E_FAIL;
5025 }
5026
5027 if (FAILED(hr = wined3d_swapchain_create(ddraw->wined3d_device, desc, NULL,
5028 &ddraw_null_wined3d_parent_ops, swapchain)))
5029 WARN("Failed to create swapchain, hr %#x.\n", hr);
5030
5031 return hr;
5032 }
5033
5034 static const struct wined3d_device_parent_ops ddraw_wined3d_device_parent_ops =
5035 {
5036 device_parent_wined3d_device_created,
5037 device_parent_mode_changed,
5038 device_parent_activate,
5039 device_parent_surface_created,
5040 device_parent_volume_created,
5041 device_parent_create_swapchain_texture,
5042 device_parent_create_swapchain,
5043 };
5044
5045 HRESULT ddraw_init(struct ddraw *ddraw, DWORD flags, enum wined3d_device_type device_type)
5046 {
5047 WINED3DCAPS caps;
5048 HRESULT hr;
5049
5050 ddraw->IDirectDraw7_iface.lpVtbl = &ddraw7_vtbl;
5051 ddraw->IDirectDraw_iface.lpVtbl = &ddraw1_vtbl;
5052 ddraw->IDirectDraw2_iface.lpVtbl = &ddraw2_vtbl;
5053 ddraw->IDirectDraw4_iface.lpVtbl = &ddraw4_vtbl;
5054 ddraw->IDirect3D_iface.lpVtbl = &d3d1_vtbl;
5055 ddraw->IDirect3D2_iface.lpVtbl = &d3d2_vtbl;
5056 ddraw->IDirect3D3_iface.lpVtbl = &d3d3_vtbl;
5057 ddraw->IDirect3D7_iface.lpVtbl = &d3d7_vtbl;
5058 ddraw->device_parent.ops = &ddraw_wined3d_device_parent_ops;
5059 ddraw->numIfaces = 1;
5060 ddraw->ref7 = 1;
5061
5062 flags |= DDRAW_WINED3D_FLAGS;
5063 if (!(ddraw->wined3d = wined3d_create(flags)))
5064 {
5065 flags |= WINED3D_NO3D;
5066 if (!(ddraw->wined3d = wined3d_create(flags)))
5067 {
5068 WARN("Failed to create a wined3d object.\n");
5069 return E_FAIL;
5070 }
5071 }
5072
5073 if (FAILED(hr = wined3d_get_device_caps(ddraw->wined3d, WINED3DADAPTER_DEFAULT, device_type, &caps)))
5074 {
5075 ERR("Failed to get device caps, hr %#x.\n", hr);
5076 wined3d_decref(ddraw->wined3d);
5077 return E_FAIL;
5078 }
5079
5080 if (!(caps.ddraw_caps.caps & WINEDDCAPS_3D))
5081 {
5082 WARN("Created a wined3d object without 3D support.\n");
5083 ddraw->flags |= DDRAW_NO3D;
5084 }
5085
5086 if (FAILED(hr = wined3d_device_create(ddraw->wined3d, WINED3DADAPTER_DEFAULT, device_type,
5087 NULL, 0, DDRAW_STRIDE_ALIGNMENT, &ddraw->device_parent, &ddraw->wined3d_device)))
5088 {
5089 WARN("Failed to create a wined3d device, hr %#x.\n", hr);
5090 wined3d_decref(ddraw->wined3d);
5091 return hr;
5092 }
5093
5094 list_init(&ddraw->surface_list);
5095
5096 return DD_OK;
5097 }