mfplat/tests: Add a separate test function for MFGetPlaneSize().
[wine.git] / dlls / ddraw / ddraw.c
blob4e17d44f66c0e0ad258d8719a2c80b5794c2ab26
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
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.
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.
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
24 #include "ddraw_private.h"
25 #include "ddrawi.h"
26 #include "d3dhal.h"
28 #include "wine/exception.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
32 static const struct ddraw *exclusive_ddraw;
33 static HWND exclusive_window;
35 /* Device identifier. Don't relay it to WineD3D */
36 static const DDDEVICEIDENTIFIER2 deviceidentifier =
38 "vga.dll", /* default 2D driver */
39 "DirectDraw HAL",
40 { { 0x00010001, 0x00010001 } },
41 0, 0, 0, 0,
42 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
43 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
47 static struct enum_device_entry
49 char interface_name[100];
50 char device_name[100];
51 const GUID *device_guid;
52 } device_list7[] =
54 /* T&L HAL device */
56 "WINE Direct3D7 Hardware Transform and Lighting acceleration using WineD3D",
57 "Wine D3D7 T&L HAL",
58 &IID_IDirect3DTnLHalDevice,
61 /* HAL device */
63 "WINE Direct3D7 Hardware acceleration using WineD3D",
64 "Direct3D HAL",
65 &IID_IDirect3DHALDevice,
68 /* RGB device */
70 "WINE Direct3D7 RGB Software Emulation using WineD3D",
71 "Wine D3D7 RGB",
72 &IID_IDirect3DRGBDevice,
76 static void STDMETHODCALLTYPE ddraw_null_wined3d_object_destroyed(void *parent) {}
78 const struct wined3d_parent_ops ddraw_null_wined3d_parent_ops =
80 ddraw_null_wined3d_object_destroyed,
83 static void CDECL ddraw_swapchain_windowed_state_changed(struct wined3d_swapchain_state_parent *parent,
84 BOOL windowed)
86 TRACE("parent %p, windowed %d.\n", parent, windowed);
89 static const struct wined3d_swapchain_state_parent_ops ddraw_swapchain_state_parent_ops =
91 ddraw_swapchain_windowed_state_changed,
94 static inline struct ddraw *impl_from_IDirectDraw(IDirectDraw *iface)
96 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw_iface);
99 static inline struct ddraw *impl_from_IDirectDraw2(IDirectDraw2 *iface)
101 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw2_iface);
104 static inline struct ddraw *impl_from_IDirectDraw4(IDirectDraw4 *iface)
106 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw4_iface);
109 static inline struct ddraw *impl_from_IDirectDraw7(IDirectDraw7 *iface)
111 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw7_iface);
114 static inline struct ddraw *impl_from_IDirect3D(IDirect3D *iface)
116 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D_iface);
119 static inline struct ddraw *impl_from_IDirect3D2(IDirect3D2 *iface)
121 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D2_iface);
124 static inline struct ddraw *impl_from_IDirect3D3(IDirect3D3 *iface)
126 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D3_iface);
129 static inline struct ddraw *impl_from_IDirect3D7(IDirect3D7 *iface)
131 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D7_iface);
134 static HRESULT WINAPI ddraw7_QueryInterface(IDirectDraw7 *iface, REFIID riid, void **out)
136 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
138 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
140 if (!riid)
142 *out = NULL;
143 return DDERR_INVALIDPARAMS;
146 /* The refcount unit test revealed that an IDirect3D7 interface can only
147 * be queried from a DirectDraw object that was created as an IDirectDraw7
148 * interface. The older interfaces can query any IDirect3D version except
149 * 7, because they are all initially created as IDirectDraw. This isn't
150 * really crucial behavior, and messy to implement with the common
151 * creation function, so it has been left out here. */
152 if (IsEqualGUID(&IID_IDirectDraw7, riid)
153 || IsEqualGUID(&IID_IUnknown, riid))
155 *out = &ddraw->IDirectDraw7_iface;
156 TRACE("Returning IDirectDraw7 interface %p.\n", *out);
158 else if (IsEqualGUID(&IID_IDirectDraw4, riid))
160 *out = &ddraw->IDirectDraw4_iface;
161 TRACE("Returning IDirectDraw4 interface %p.\n", *out);
163 else if (IsEqualGUID(&IID_IDirectDraw2, riid))
165 *out = &ddraw->IDirectDraw2_iface;
166 TRACE("Returning IDirectDraw2 interface %p.\n", *out);
168 else if (IsEqualGUID(&IID_IDirectDraw, riid))
170 *out = &ddraw->IDirectDraw_iface;
171 TRACE("Returning IDirectDraw interface %p.\n", *out);
173 else if (IsEqualGUID(&IID_IDirect3D7, riid))
175 ddraw->d3dversion = 7;
176 *out = &ddraw->IDirect3D7_iface;
177 TRACE("Returning Direct3D7 interface %p.\n", *out);
179 else if (IsEqualGUID(&IID_IDirect3D3, riid))
181 ddraw->d3dversion = 3;
182 *out = &ddraw->IDirect3D3_iface;
183 TRACE("Returning Direct3D3 interface %p.\n", *out);
185 else if (IsEqualGUID(&IID_IDirect3D2, riid))
187 ddraw->d3dversion = 2;
188 *out = &ddraw->IDirect3D2_iface;
189 TRACE("Returning Direct3D2 interface %p.\n", *out);
191 else if (IsEqualGUID(&IID_IDirect3D, riid))
193 ddraw->d3dversion = 1;
194 *out = &ddraw->IDirect3D_iface;
195 TRACE("Returning Direct3D interface %p.\n", *out);
197 /* Unknown interface */
198 else
200 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
201 *out = NULL;
202 return E_NOINTERFACE;
205 IUnknown_AddRef((IUnknown *)*out);
206 return S_OK;
209 static HRESULT WINAPI ddraw4_QueryInterface(IDirectDraw4 *iface, REFIID riid, void **object)
211 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
213 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
215 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
218 static HRESULT WINAPI ddraw2_QueryInterface(IDirectDraw2 *iface, REFIID riid, void **object)
220 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
222 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
224 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
227 static HRESULT WINAPI ddraw1_QueryInterface(IDirectDraw *iface, REFIID riid, void **object)
229 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
231 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
233 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
236 static HRESULT WINAPI d3d7_QueryInterface(IDirect3D7 *iface, REFIID riid, void **object)
238 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
240 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
242 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
245 static HRESULT WINAPI d3d3_QueryInterface(IDirect3D3 *iface, REFIID riid, void **object)
247 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
249 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
251 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
254 static HRESULT WINAPI d3d2_QueryInterface(IDirect3D2 *iface, REFIID riid, void **object)
256 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
258 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
260 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
263 static HRESULT WINAPI d3d1_QueryInterface(IDirect3D *iface, REFIID riid, void **object)
265 struct ddraw *ddraw = impl_from_IDirect3D(iface);
267 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
269 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
272 /*****************************************************************************
273 * IDirectDraw7::AddRef
275 * Increases the interfaces refcount, basically
277 * DDraw refcounting is a bit tricky. The different DirectDraw interface
278 * versions have individual refcounts, but the IDirect3D interfaces do not.
279 * All interfaces are from one object, that means calling QueryInterface on an
280 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
281 * ddraw object.
283 * That means all AddRef and Release implementations of IDirectDrawX work
284 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
285 * except of IDirect3D7 which thunks to IDirectDraw7
287 * Returns: The new refcount
289 *****************************************************************************/
290 static ULONG WINAPI ddraw7_AddRef(IDirectDraw7 *iface)
292 struct ddraw *This = impl_from_IDirectDraw7(iface);
293 ULONG ref = InterlockedIncrement(&This->ref7);
295 TRACE("%p increasing refcount to %u.\n", This, ref);
297 if(ref == 1) InterlockedIncrement(&This->numIfaces);
299 return ref;
302 static ULONG WINAPI ddraw4_AddRef(IDirectDraw4 *iface)
304 struct ddraw *This = impl_from_IDirectDraw4(iface);
305 ULONG ref = InterlockedIncrement(&This->ref4);
307 TRACE("%p increasing refcount to %u.\n", This, ref);
309 if (ref == 1) InterlockedIncrement(&This->numIfaces);
311 return ref;
314 static ULONG WINAPI ddraw2_AddRef(IDirectDraw2 *iface)
316 struct ddraw *This = impl_from_IDirectDraw2(iface);
317 ULONG ref = InterlockedIncrement(&This->ref2);
319 TRACE("%p increasing refcount to %u.\n", This, ref);
321 if (ref == 1) InterlockedIncrement(&This->numIfaces);
323 return ref;
326 static ULONG WINAPI ddraw1_AddRef(IDirectDraw *iface)
328 struct ddraw *This = impl_from_IDirectDraw(iface);
329 ULONG ref = InterlockedIncrement(&This->ref1);
331 TRACE("%p increasing refcount to %u.\n", This, ref);
333 if (ref == 1) InterlockedIncrement(&This->numIfaces);
335 return ref;
338 static ULONG WINAPI d3d7_AddRef(IDirect3D7 *iface)
340 struct ddraw *This = impl_from_IDirect3D7(iface);
342 TRACE("iface %p.\n", iface);
344 return ddraw7_AddRef(&This->IDirectDraw7_iface);
347 static ULONG WINAPI d3d3_AddRef(IDirect3D3 *iface)
349 struct ddraw *This = impl_from_IDirect3D3(iface);
351 TRACE("iface %p.\n", iface);
353 return ddraw1_AddRef(&This->IDirectDraw_iface);
356 static ULONG WINAPI d3d2_AddRef(IDirect3D2 *iface)
358 struct ddraw *This = impl_from_IDirect3D2(iface);
360 TRACE("iface %p.\n", iface);
362 return ddraw1_AddRef(&This->IDirectDraw_iface);
365 static ULONG WINAPI d3d1_AddRef(IDirect3D *iface)
367 struct ddraw *This = impl_from_IDirect3D(iface);
369 TRACE("iface %p.\n", iface);
371 return ddraw1_AddRef(&This->IDirectDraw_iface);
374 static void ddraw_destroy_swapchain(struct ddraw *ddraw)
376 unsigned int i;
378 TRACE("Destroying the swapchain.\n");
380 wined3d_swapchain_decref(ddraw->wined3d_swapchain);
382 for (i = 0; i < ddraw->numConvertedDecls; ++i)
384 wined3d_vertex_declaration_decref(ddraw->decls[i].decl);
386 heap_free(ddraw->decls);
387 ddraw->numConvertedDecls = 0;
389 wined3d_swapchain_decref(ddraw->wined3d_swapchain);
390 ddraw->wined3d_swapchain = NULL;
392 /* Free the d3d window if one was created. */
393 if (ddraw->d3d_window && ddraw->d3d_window != ddraw->dest_window)
395 TRACE("Destroying the hidden render window %p.\n", ddraw->d3d_window);
396 DestroyWindow(ddraw->d3d_window);
397 ddraw->d3d_window = 0;
400 ddraw->flags &= ~DDRAW_D3D_INITIALIZED;
402 ddraw_set_swapchain_window(ddraw, NULL);
404 TRACE("Swapchain destroyed.\n");
407 /*****************************************************************************
408 * ddraw_destroy
410 * Destroys a ddraw object if all refcounts are 0. This is to share code
411 * between the IDirectDrawX::Release functions
413 * Params:
414 * This: DirectDraw object to destroy
416 *****************************************************************************/
417 static void ddraw_destroy(struct ddraw *This)
419 IDirectDraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, NULL, DDSCL_NORMAL);
420 IDirectDraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
422 /* Destroy the device window if we created one */
423 if(This->devicewindow != 0)
425 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
426 DestroyWindow(This->devicewindow);
427 This->devicewindow = 0;
430 wined3d_mutex_lock();
431 list_remove(&This->ddraw_list_entry);
432 wined3d_mutex_unlock();
434 if (This->wined3d_swapchain)
435 ddraw_destroy_swapchain(This);
436 wined3d_stateblock_decref(This->state);
437 wined3d_device_decref(This->wined3d_device);
438 wined3d_decref(This->wined3d);
440 if (This->d3ddevice)
441 This->d3ddevice->ddraw = NULL;
443 /* Now free the object */
444 heap_free(This);
447 /*****************************************************************************
448 * IDirectDraw7::Release
450 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
452 * Returns: The new refcount
453 *****************************************************************************/
454 static ULONG WINAPI ddraw7_Release(IDirectDraw7 *iface)
456 struct ddraw *This = impl_from_IDirectDraw7(iface);
457 ULONG ref = InterlockedDecrement(&This->ref7);
459 TRACE("%p decreasing refcount to %u.\n", This, ref);
461 if (!ref && !InterlockedDecrement(&This->numIfaces))
462 ddraw_destroy(This);
464 return ref;
467 static ULONG WINAPI ddraw4_Release(IDirectDraw4 *iface)
469 struct ddraw *This = impl_from_IDirectDraw4(iface);
470 ULONG ref = InterlockedDecrement(&This->ref4);
472 TRACE("%p decreasing refcount to %u.\n", This, ref);
474 if (!ref && !InterlockedDecrement(&This->numIfaces))
475 ddraw_destroy(This);
477 return ref;
480 static ULONG WINAPI ddraw2_Release(IDirectDraw2 *iface)
482 struct ddraw *This = impl_from_IDirectDraw2(iface);
483 ULONG ref = InterlockedDecrement(&This->ref2);
485 TRACE("%p decreasing refcount to %u.\n", This, ref);
487 if (!ref && !InterlockedDecrement(&This->numIfaces))
488 ddraw_destroy(This);
490 return ref;
493 static ULONG WINAPI ddraw1_Release(IDirectDraw *iface)
495 struct ddraw *This = impl_from_IDirectDraw(iface);
496 ULONG ref = InterlockedDecrement(&This->ref1);
498 TRACE("%p decreasing refcount to %u.\n", This, ref);
500 if (!ref && !InterlockedDecrement(&This->numIfaces))
501 ddraw_destroy(This);
503 return ref;
506 static ULONG WINAPI d3d7_Release(IDirect3D7 *iface)
508 struct ddraw *This = impl_from_IDirect3D7(iface);
510 TRACE("iface %p.\n", iface);
512 return ddraw7_Release(&This->IDirectDraw7_iface);
515 static ULONG WINAPI d3d3_Release(IDirect3D3 *iface)
517 struct ddraw *This = impl_from_IDirect3D3(iface);
519 TRACE("iface %p.\n", iface);
521 return ddraw1_Release(&This->IDirectDraw_iface);
524 static ULONG WINAPI d3d2_Release(IDirect3D2 *iface)
526 struct ddraw *This = impl_from_IDirect3D2(iface);
528 TRACE("iface %p.\n", iface);
530 return ddraw1_Release(&This->IDirectDraw_iface);
533 static ULONG WINAPI d3d1_Release(IDirect3D *iface)
535 struct ddraw *This = impl_from_IDirect3D(iface);
537 TRACE("iface %p.\n", iface);
539 return ddraw1_Release(&This->IDirectDraw_iface);
542 /*****************************************************************************
543 * IDirectDraw methods
544 *****************************************************************************/
546 static HRESULT ddraw_set_focus_window(struct ddraw *ddraw, HWND window)
548 /* FIXME: This looks wrong, exclusive mode should imply a destination
549 * window. */
550 if ((ddraw->cooperative_level & DDSCL_EXCLUSIVE) && ddraw->dest_window)
552 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET.\n");
553 return DDERR_HWNDALREADYSET;
556 ddraw->focuswindow = window;
558 return DD_OK;
561 static HRESULT ddraw_attach_d3d_device(struct ddraw *ddraw, HWND window,
562 DWORD cooplevel, struct wined3d_swapchain **wined3d_swapchain)
564 struct wined3d_swapchain_desc swapchain_desc;
565 struct wined3d_display_mode mode;
566 HRESULT hr;
568 TRACE("ddraw %p.\n", ddraw);
570 if (FAILED(hr = wined3d_output_get_display_mode(ddraw->wined3d_output, &mode, NULL)))
572 ERR("Failed to get display mode.\n");
573 return hr;
576 memset(&swapchain_desc, 0, sizeof(swapchain_desc));
577 swapchain_desc.output = ddraw->wined3d_output;
578 swapchain_desc.backbuffer_width = mode.width;
579 swapchain_desc.backbuffer_height = mode.height;
580 swapchain_desc.backbuffer_format = mode.format_id;
581 swapchain_desc.backbuffer_bind_flags = 0;
582 swapchain_desc.backbuffer_count = 1;
583 swapchain_desc.swap_effect = WINED3D_SWAP_EFFECT_DISCARD;
584 swapchain_desc.device_window = window;
585 swapchain_desc.windowed = !(cooplevel & DDSCL_FULLSCREEN);
586 swapchain_desc.flags = WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH | WINED3D_SWAPCHAIN_IMPLICIT;
588 if ((cooplevel & DDSCL_NOWINDOWCHANGES) || window != GetForegroundWindow())
589 swapchain_desc.flags |= WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES;
591 if (ddraw->flags & DDRAW_NO3D)
592 return wined3d_swapchain_create(ddraw->wined3d_device, &swapchain_desc,
593 &ddraw->state_parent, NULL, &ddraw_null_wined3d_parent_ops, wined3d_swapchain);
595 if (!window || window == GetDesktopWindow())
597 window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "Hidden D3D Window",
598 WS_DISABLED, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
599 NULL, NULL, NULL, NULL);
600 if (!window)
602 ERR("Failed to create window, last error %#x.\n", GetLastError());
603 return E_FAIL;
606 ShowWindow(window, SW_HIDE); /* Just to be sure */
607 WARN("No window for the Direct3DDevice, created hidden window %p.\n", window);
609 swapchain_desc.device_window = window;
611 else
613 TRACE("Using existing window %p for Direct3D rendering.\n", window);
615 ddraw->d3d_window = window;
617 /* Set this NOW, otherwise creating the depth stencil surface will cause a
618 * recursive loop until ram or emulated video memory is full. */
619 ddraw->flags |= DDRAW_D3D_INITIALIZED;
620 if (FAILED(hr = wined3d_swapchain_create(ddraw->wined3d_device, &swapchain_desc,
621 &ddraw->state_parent, NULL, &ddraw_null_wined3d_parent_ops, wined3d_swapchain)))
623 ddraw->flags &= ~DDRAW_D3D_INITIALIZED;
624 DestroyWindow(window);
625 ddraw->d3d_window = NULL;
626 return hr;
629 ddraw->declArraySize = 2;
630 if (!(ddraw->decls = heap_alloc_zero(ddraw->declArraySize * sizeof(*ddraw->decls))))
632 ERR("Error allocating an array for the converted vertex decls.\n");
633 ddraw->declArraySize = 0;
634 wined3d_swapchain_decref(*wined3d_swapchain);
635 DestroyWindow(window);
636 ddraw->d3d_window = NULL;
637 return E_OUTOFMEMORY;
640 TRACE("Successfully initialized 3D.\n");
642 return DD_OK;
645 static HRESULT ddraw_create_swapchain(struct ddraw *ddraw, HWND window, DWORD cooplevel)
647 HRESULT hr;
649 if (ddraw->wined3d_swapchain)
651 ERR("Swapchain already created.\n");
652 return E_FAIL;
655 if (FAILED(hr = ddraw_attach_d3d_device(ddraw, window, cooplevel, &ddraw->wined3d_swapchain)))
657 ERR("Failed to create swapchain, hr %#x.\n", hr);
658 return hr;
660 wined3d_swapchain_incref(ddraw->wined3d_swapchain);
662 ddraw_set_swapchain_window(ddraw, window);
664 if (ddraw->primary && ddraw->primary->palette)
665 wined3d_swapchain_set_palette(ddraw->wined3d_swapchain, ddraw->primary->palette->wined3d_palette);
667 return DD_OK;
670 /*****************************************************************************
671 * IDirectDraw7::RestoreDisplayMode
673 * Restores the display mode to what it was at creation time. Basically.
675 * Returns
676 * DD_OK on success
677 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
679 *****************************************************************************/
680 static HRESULT WINAPI ddraw7_RestoreDisplayMode(IDirectDraw7 *iface)
682 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
683 struct wined3d_display_mode mode;
684 RECT clip_rect;
685 HRESULT hr;
687 TRACE("iface %p.\n", iface);
689 wined3d_mutex_lock();
691 if (!(ddraw->flags & DDRAW_RESTORE_MODE))
693 wined3d_mutex_unlock();
694 return DD_OK;
697 if (exclusive_ddraw && exclusive_ddraw != ddraw)
699 wined3d_mutex_unlock();
700 return DDERR_NOEXCLUSIVEMODE;
703 if (SUCCEEDED(hr = wined3d_restore_display_modes(ddraw->wined3d)))
705 ddraw->flags &= ~DDRAW_RESTORE_MODE;
706 if (ddraw->cooperative_level & DDSCL_EXCLUSIVE &&
707 SUCCEEDED(hr = wined3d_output_get_display_mode(ddraw->wined3d_output, &mode, NULL)))
709 SetRect(&clip_rect, 0, 0, mode.width, mode.height);
710 ClipCursor(&clip_rect);
714 InterlockedCompareExchange(&ddraw->device_state, DDRAW_DEVICE_STATE_NOT_RESTORED, DDRAW_DEVICE_STATE_OK);
716 wined3d_mutex_unlock();
718 return hr;
721 static HRESULT WINAPI ddraw4_RestoreDisplayMode(IDirectDraw4 *iface)
723 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
725 TRACE("iface %p.\n", iface);
727 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
730 static HRESULT WINAPI ddraw2_RestoreDisplayMode(IDirectDraw2 *iface)
732 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
734 TRACE("iface %p.\n", iface);
736 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
739 static HRESULT WINAPI ddraw1_RestoreDisplayMode(IDirectDraw *iface)
741 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
743 TRACE("iface %p.\n", iface);
745 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
748 /*****************************************************************************
749 * IDirectDraw7::SetCooperativeLevel
751 * Sets the cooperative level for the DirectDraw object, and the window
752 * assigned to it. The cooperative level determines the general behavior
753 * of the DirectDraw application
755 * Warning: This is quite tricky, as it's not really documented which
756 * cooperative levels can be combined with each other. If a game fails
757 * after this function, try to check the cooperative levels passed on
758 * Windows, and if it returns something different.
760 * If you think that this function caused the failure because it writes a
761 * fixme, be sure to run again with a +ddraw trace.
763 * What is known about cooperative levels (See the ddraw modes test):
764 * DDSCL_EXCLUSIVE requires DDSCL_FULLSCREEN.
765 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE.
766 * Unlike what msdn claims, DDSCL_NORMAL | DDSCL_FULLSCREEN is allowed.
767 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
768 * DDSCL_EXCLUSIVE can be activated.
769 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES or
770 * DDSCL_CREATEDEVICEWINDOW.
772 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
773 * DDSCL_CREATEDEVICEWINDOW, DDSCL_SETDEVICEWINDOW
774 * DDSCL_SETFOCUSWINDOW (partially),
775 * DDSCL_MULTITHREADED (work in progress)
776 * DDSCL_FPUPRESERVE (see device.c)
778 * Unsure about this: DDSCL_FPUSETUP
780 * These don't seem very important for wine:
781 * DDSCL_ALLOWREBOOT, DDSCL_ALLOWMODEX
783 * Returns:
784 * DD_OK if the cooperative level was set successfully
785 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
786 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
787 * (Probably others too, have to investigate)
789 *****************************************************************************/
790 static HRESULT ddraw_set_cooperative_level(struct ddraw *ddraw, HWND window,
791 DWORD cooplevel, BOOL restore_mode_on_normal)
793 struct wined3d_rendertarget_view *rtv = NULL, *dsv = NULL;
794 struct wined3d_stateblock *stateblock;
795 BOOL restore_state = FALSE;
796 RECT clip_rect;
797 HRESULT hr;
799 TRACE("ddraw %p, window %p, flags %#x, restore_mode_on_normal %x.\n", ddraw, window, cooplevel,
800 restore_mode_on_normal);
801 DDRAW_dump_cooperativelevel(cooplevel);
803 wined3d_mutex_lock();
805 if (ddraw->flags & DDRAW_SCL_RECURSIVE)
807 WARN("Recursive call, returning DD_OK.\n");
808 hr = DD_OK;
809 goto done;
811 ddraw->flags |= DDRAW_SCL_RECURSIVE;
813 /* Tests suggest that we need one of them: */
814 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
815 DDSCL_NORMAL |
816 DDSCL_EXCLUSIVE )))
818 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
819 hr = DDERR_INVALIDPARAMS;
820 goto done;
823 if ((cooplevel & DDSCL_CREATEDEVICEWINDOW) && !(cooplevel & DDSCL_EXCLUSIVE))
825 WARN("DDSCL_CREATEDEVICEWINDOW requires DDSCL_EXCLUSIVE.\n");
826 hr = DDERR_INVALIDPARAMS;
827 goto done;
830 /* Handle those levels first which set various hwnds */
831 if ((cooplevel & DDSCL_SETFOCUSWINDOW) && !(cooplevel & DDSCL_CREATEDEVICEWINDOW))
833 /* This isn't compatible with a lot of flags */
834 if (cooplevel & (DDSCL_MULTITHREADED
835 | DDSCL_FPUSETUP
836 | DDSCL_FPUPRESERVE
837 | DDSCL_ALLOWREBOOT
838 | DDSCL_ALLOWMODEX
839 | DDSCL_SETDEVICEWINDOW
840 | DDSCL_NORMAL
841 | DDSCL_EXCLUSIVE
842 | DDSCL_FULLSCREEN))
844 WARN("Called with incompatible flags, returning DDERR_INVALIDPARAMS.\n");
845 hr = DDERR_INVALIDPARAMS;
846 goto done;
849 hr = ddraw_set_focus_window(ddraw, window);
850 goto done;
853 if (cooplevel & DDSCL_EXCLUSIVE)
855 if (!(cooplevel & DDSCL_FULLSCREEN) || !(window || (cooplevel & DDSCL_CREATEDEVICEWINDOW)))
857 WARN("DDSCL_EXCLUSIVE requires DDSCL_FULLSCREEN and a window.\n");
858 hr = DDERR_INVALIDPARAMS;
859 goto done;
862 if (cooplevel & DDSCL_CREATEDEVICEWINDOW)
864 HWND device_window;
866 if (!ddraw->focuswindow && !(cooplevel & DDSCL_SETFOCUSWINDOW))
868 WARN("No focus window set.\n");
869 hr = DDERR_NOFOCUSWINDOW;
870 goto done;
873 device_window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "DirectDrawDeviceWnd",
874 WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
875 NULL, NULL, NULL, NULL);
876 if (!device_window)
878 ERR("Failed to create window, last error %#x.\n", GetLastError());
879 hr = E_FAIL;
880 goto done;
883 ShowWindow(device_window, SW_SHOW);
884 TRACE("Created a device window %p.\n", device_window);
886 /* Native apparently leaks the created device window if setting the
887 * focus window below fails. */
888 ddraw->cooperative_level |= DDSCL_CREATEDEVICEWINDOW;
889 ddraw->devicewindow = device_window;
891 if (cooplevel & DDSCL_SETFOCUSWINDOW)
893 if (!window)
895 hr = DDERR_NOHWND;
896 goto done;
899 if (FAILED(hr = ddraw_set_focus_window(ddraw, window)))
900 goto done;
903 window = device_window;
906 else
908 if (ddraw->cooperative_level & DDSCL_CREATEDEVICEWINDOW)
909 DestroyWindow(ddraw->devicewindow);
910 ddraw->devicewindow = NULL;
911 ddraw->focuswindow = NULL;
914 if ((cooplevel & DDSCL_EXCLUSIVE) && exclusive_window != window)
916 ddraw->device_state = DDRAW_DEVICE_STATE_NOT_RESTORED;
917 exclusive_window = window;
920 if (cooplevel & DDSCL_MULTITHREADED && !(ddraw->cooperative_level & DDSCL_MULTITHREADED))
921 wined3d_device_set_multithreaded(ddraw->wined3d_device);
923 if (ddraw->wined3d_swapchain)
925 if (!(ddraw->flags & DDRAW_NO3D))
927 restore_state = TRUE;
929 if (FAILED(hr = wined3d_stateblock_create(ddraw->wined3d_device,
930 ddraw->state, WINED3D_SBT_ALL, &stateblock)))
932 ERR("Failed to create stateblock, hr %#x.\n", hr);
933 goto done;
936 rtv = wined3d_device_context_get_rendertarget_view(ddraw->immediate_context, 0);
937 /* Rendering to ddraw->wined3d_frontbuffer. */
938 if (rtv && !wined3d_rendertarget_view_get_sub_resource_parent(rtv))
939 rtv = NULL;
940 else if (rtv)
941 wined3d_rendertarget_view_incref(rtv);
943 if ((dsv = wined3d_device_context_get_depth_stencil_view(ddraw->immediate_context)))
944 wined3d_rendertarget_view_incref(dsv);
947 ddraw_destroy_swapchain(ddraw);
950 if (FAILED(hr = ddraw_create_swapchain(ddraw, window, cooplevel)))
951 ERR("Failed to create swapchain, hr %#x.\n", hr);
953 if (restore_state)
955 if (dsv)
957 wined3d_device_context_set_depth_stencil_view(ddraw->immediate_context, dsv);
958 wined3d_rendertarget_view_decref(dsv);
961 if (rtv)
963 wined3d_device_context_set_rendertarget_views(ddraw->immediate_context, 0, 1, &rtv, FALSE);
964 wined3d_rendertarget_view_decref(rtv);
967 wined3d_stateblock_apply(stateblock, ddraw->state);
968 wined3d_stateblock_decref(stateblock);
971 if (!(cooplevel & DDSCL_EXCLUSIVE) && (ddraw->cooperative_level & DDSCL_EXCLUSIVE))
973 /* When going from exclusive mode to normal, ddraw removes the
974 topmost bit unless the DDSCL_NOWINDOWCHANGES flag is set in
975 this call that sets it to normal, not in the old coop level. */
976 if (!(cooplevel & DDSCL_NOWINDOWCHANGES))
977 SetWindowPos(window, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
979 if (restore_mode_on_normal && FAILED(ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface)))
980 ERR("RestoreDisplayMode failed\n");
981 ClipCursor(NULL);
984 if ((ddraw->cooperative_level & DDSCL_EXCLUSIVE)
985 && (window != ddraw->dest_window || !(cooplevel & DDSCL_EXCLUSIVE)))
986 wined3d_device_release_focus_window(ddraw->wined3d_device);
988 if ((cooplevel & DDSCL_EXCLUSIVE)
989 && (window != ddraw->dest_window || !(ddraw->cooperative_level & DDSCL_EXCLUSIVE)))
991 hr = wined3d_device_acquire_focus_window(ddraw->wined3d_device, window);
992 if (FAILED(hr))
994 ERR("Failed to acquire focus window, hr %#x.\n", hr);
995 goto done;
999 if (cooplevel & DDSCL_EXCLUSIVE)
1001 SetRect(&clip_rect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
1002 ClipCursor(&clip_rect);
1005 /* Unhandled flags */
1006 if (cooplevel & DDSCL_ALLOWREBOOT)
1007 WARN("Unhandled flag DDSCL_ALLOWREBOOT, harmless\n");
1008 if (cooplevel & DDSCL_ALLOWMODEX)
1009 WARN("Unhandled flag DDSCL_ALLOWMODEX, harmless\n");
1010 if (cooplevel & DDSCL_FPUSETUP)
1011 WARN("Unhandled flag DDSCL_FPUSETUP, harmless\n");
1013 if (cooplevel & DDSCL_EXCLUSIVE)
1014 exclusive_ddraw = ddraw;
1015 else if (exclusive_ddraw == ddraw)
1016 exclusive_ddraw = NULL;
1018 /* Store the cooperative_level */
1019 ddraw->cooperative_level = cooplevel;
1020 ddraw->dest_window = window;
1022 TRACE("SetCooperativeLevel returning DD_OK\n");
1023 hr = DD_OK;
1024 done:
1025 ddraw->flags &= ~DDRAW_SCL_RECURSIVE;
1026 wined3d_mutex_unlock();
1028 return hr;
1031 static HRESULT WINAPI ddraw7_SetCooperativeLevel(IDirectDraw7 *iface, HWND window, DWORD flags)
1033 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1035 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1037 return ddraw_set_cooperative_level(ddraw, window, flags, !(ddraw->flags & DDRAW_SCL_DDRAW1));
1040 static HRESULT WINAPI ddraw4_SetCooperativeLevel(IDirectDraw4 *iface, HWND window, DWORD flags)
1042 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1044 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1046 return ddraw_set_cooperative_level(ddraw, window, flags, !(ddraw->flags & DDRAW_SCL_DDRAW1));
1049 static HRESULT WINAPI ddraw2_SetCooperativeLevel(IDirectDraw2 *iface, HWND window, DWORD flags)
1051 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1053 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1055 return ddraw_set_cooperative_level(ddraw, window, flags, !(ddraw->flags & DDRAW_SCL_DDRAW1));
1058 static HRESULT WINAPI ddraw1_SetCooperativeLevel(IDirectDraw *iface, HWND window, DWORD flags)
1060 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1061 HRESULT hr;
1063 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1065 hr = ddraw_set_cooperative_level(ddraw, window, flags, FALSE);
1066 if (SUCCEEDED(hr))
1067 ddraw->flags |= DDRAW_SCL_DDRAW1;
1068 return hr;
1071 /*****************************************************************************
1072 * IDirectDraw7::SetDisplayMode
1074 * Sets the display screen resolution, color depth and refresh frequency
1075 * when in fullscreen mode (in theory).
1076 * Possible return values listed in the SDK suggest that this method fails
1077 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
1078 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
1079 * It seems to be valid to pass 0 for With and Height, this has to be tested
1080 * It could mean that the current video mode should be left as-is. (But why
1081 * call it then?)
1083 * Params:
1084 * Height, Width: Screen dimension
1085 * BPP: Color depth in Bits per pixel
1086 * Refreshrate: Screen refresh rate
1087 * Flags: Other stuff
1089 * Returns
1090 * DD_OK on success
1092 *****************************************************************************/
1093 static HRESULT WINAPI ddraw7_SetDisplayMode(IDirectDraw7 *iface, DWORD width, DWORD height,
1094 DWORD bpp, DWORD refresh_rate, DWORD flags)
1096 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1097 struct wined3d_display_mode mode;
1098 enum wined3d_format_id format;
1099 RECT clip_rect;
1100 HRESULT hr;
1102 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1103 iface, width, height, bpp, refresh_rate, flags);
1105 if (force_refresh_rate != 0)
1107 TRACE("ForceRefreshRate overriding passed-in refresh rate (%u Hz) to %u Hz\n",
1108 refresh_rate, force_refresh_rate);
1109 refresh_rate = force_refresh_rate;
1112 wined3d_mutex_lock();
1114 if (exclusive_ddraw && exclusive_ddraw != ddraw)
1116 wined3d_mutex_unlock();
1117 return DDERR_NOEXCLUSIVEMODE;
1120 if (!width || !height)
1122 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here. */
1123 wined3d_mutex_unlock();
1124 return DD_OK;
1127 switch (bpp)
1129 case 8: format = WINED3DFMT_P8_UINT; break;
1130 case 15: format = WINED3DFMT_B5G5R5X1_UNORM; break;
1131 case 16: format = WINED3DFMT_B5G6R5_UNORM; break;
1132 case 24: format = WINED3DFMT_B8G8R8_UNORM; break;
1133 case 32: format = WINED3DFMT_B8G8R8X8_UNORM; break;
1134 default: format = WINED3DFMT_UNKNOWN; break;
1137 mode.width = width;
1138 mode.height = height;
1139 mode.refresh_rate = refresh_rate;
1140 mode.format_id = format;
1141 mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
1143 /* TODO: The possible return values from msdn suggest that the screen mode
1144 * can't be changed if a surface is locked or some drawing is in progress. */
1145 if (SUCCEEDED(hr = wined3d_output_set_display_mode(ddraw->wined3d_output, &mode)))
1147 if (ddraw->primary)
1149 DDSURFACEDESC2 *surface_desc = &ddraw->primary->surface_desc;
1151 if (FAILED(hr = wined3d_swapchain_resize_buffers(ddraw->wined3d_swapchain, 0,
1152 surface_desc->dwWidth, surface_desc->dwHeight, mode.format_id, WINED3D_MULTISAMPLE_NONE, 0)))
1153 ERR("Failed to resize buffers, hr %#x.\n", hr);
1154 else
1155 ddrawformat_from_wined3dformat(&ddraw->primary->surface_desc.u4.ddpfPixelFormat, mode.format_id);
1157 ddraw->flags |= DDRAW_RESTORE_MODE;
1159 if (ddraw->cooperative_level & DDSCL_EXCLUSIVE)
1161 SetRect(&clip_rect, 0, 0, width, height);
1162 ClipCursor(&clip_rect);
1166 InterlockedCompareExchange(&ddraw->device_state, DDRAW_DEVICE_STATE_NOT_RESTORED, DDRAW_DEVICE_STATE_OK);
1168 wined3d_mutex_unlock();
1170 return hr_ddraw_from_wined3d(hr);
1173 static HRESULT WINAPI ddraw4_SetDisplayMode(IDirectDraw4 *iface, DWORD width, DWORD height,
1174 DWORD bpp, DWORD refresh_rate, DWORD flags)
1176 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1178 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1179 iface, width, height, bpp, refresh_rate, flags);
1181 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1184 static HRESULT WINAPI ddraw2_SetDisplayMode(IDirectDraw2 *iface,
1185 DWORD width, DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
1187 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1189 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1190 iface, width, height, bpp, refresh_rate, flags);
1192 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1195 static HRESULT WINAPI ddraw1_SetDisplayMode(IDirectDraw *iface, DWORD width, DWORD height, DWORD bpp)
1197 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1199 TRACE("iface %p, width %u, height %u, bpp %u.\n", iface, width, height, bpp);
1201 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, 0, 0);
1204 void ddraw_d3dcaps1_from_7(D3DDEVICEDESC *caps1, D3DDEVICEDESC7 *caps7)
1206 memset(caps1, 0, sizeof(*caps1));
1207 caps1->dwSize = sizeof(*caps1);
1208 caps1->dwFlags = D3DDD_COLORMODEL
1209 | D3DDD_DEVCAPS
1210 | D3DDD_TRANSFORMCAPS
1211 | D3DDD_BCLIPPING
1212 | D3DDD_LIGHTINGCAPS
1213 | D3DDD_LINECAPS
1214 | D3DDD_TRICAPS
1215 | D3DDD_DEVICERENDERBITDEPTH
1216 | D3DDD_DEVICEZBUFFERBITDEPTH
1217 | D3DDD_MAXBUFFERSIZE
1218 | D3DDD_MAXVERTEXCOUNT;
1219 caps1->dcmColorModel = D3DCOLOR_RGB;
1220 caps1->dwDevCaps = caps7->dwDevCaps;
1221 caps1->dtcTransformCaps.dwSize = sizeof(caps1->dtcTransformCaps);
1222 caps1->dtcTransformCaps.dwCaps = D3DTRANSFORMCAPS_CLIP;
1223 caps1->bClipping = TRUE;
1224 caps1->dlcLightingCaps.dwSize = sizeof(caps1->dlcLightingCaps);
1225 caps1->dlcLightingCaps.dwCaps = D3DLIGHTCAPS_DIRECTIONAL
1226 | D3DLIGHTCAPS_PARALLELPOINT
1227 | D3DLIGHTCAPS_POINT
1228 | D3DLIGHTCAPS_SPOT;
1229 caps1->dlcLightingCaps.dwLightingModel = D3DLIGHTINGMODEL_RGB;
1230 caps1->dlcLightingCaps.dwNumLights = caps7->dwMaxActiveLights;
1231 caps1->dpcLineCaps = caps7->dpcLineCaps;
1232 caps1->dpcTriCaps = caps7->dpcTriCaps;
1233 caps1->dwDeviceRenderBitDepth = caps7->dwDeviceRenderBitDepth;
1234 caps1->dwDeviceZBufferBitDepth = caps7->dwDeviceZBufferBitDepth;
1235 caps1->dwMaxBufferSize = 0;
1236 caps1->dwMaxVertexCount = 65536;
1237 caps1->dwMinTextureWidth = caps7->dwMinTextureWidth;
1238 caps1->dwMinTextureHeight = caps7->dwMinTextureHeight;
1239 caps1->dwMaxTextureWidth = caps7->dwMaxTextureWidth;
1240 caps1->dwMaxTextureHeight = caps7->dwMaxTextureHeight;
1241 caps1->dwMinStippleWidth = 1;
1242 caps1->dwMinStippleHeight = 1;
1243 caps1->dwMaxStippleWidth = 32;
1244 caps1->dwMaxStippleHeight = 32;
1245 caps1->dwMaxTextureRepeat = caps7->dwMaxTextureRepeat;
1246 caps1->dwMaxTextureAspectRatio = caps7->dwMaxTextureAspectRatio;
1247 caps1->dwMaxAnisotropy = caps7->dwMaxAnisotropy;
1248 caps1->dvGuardBandLeft = caps7->dvGuardBandLeft;
1249 caps1->dvGuardBandTop = caps7->dvGuardBandTop;
1250 caps1->dvGuardBandRight = caps7->dvGuardBandRight;
1251 caps1->dvGuardBandBottom = caps7->dvGuardBandBottom;
1252 caps1->dvExtentsAdjust = caps7->dvExtentsAdjust;
1253 caps1->dwStencilCaps = caps7->dwStencilCaps;
1254 caps1->dwFVFCaps = caps7->dwFVFCaps;
1255 caps1->dwTextureOpCaps = caps7->dwTextureOpCaps;
1256 caps1->wMaxTextureBlendStages = caps7->wMaxTextureBlendStages;
1257 caps1->wMaxSimultaneousTextures = caps7->wMaxSimultaneousTextures;
1260 HRESULT ddraw_get_d3dcaps(const struct ddraw *ddraw, D3DDEVICEDESC7 *caps)
1262 struct wined3d_caps wined3d_caps;
1263 HRESULT hr;
1265 TRACE("ddraw %p, caps %p.\n", ddraw, caps);
1267 memset(&wined3d_caps, 0, sizeof(wined3d_caps));
1269 wined3d_mutex_lock();
1270 hr = wined3d_get_device_caps(ddraw->wined3d_adapter, WINED3D_DEVICE_TYPE_HAL, &wined3d_caps);
1271 wined3d_mutex_unlock();
1272 if (FAILED(hr))
1274 WARN("Failed to get device caps, hr %#x.\n", hr);
1275 return hr;
1278 caps->dwDevCaps = wined3d_caps.DevCaps;
1279 caps->dpcLineCaps.dwMiscCaps = wined3d_caps.PrimitiveMiscCaps;
1280 caps->dpcLineCaps.dwRasterCaps = wined3d_caps.RasterCaps;
1281 caps->dpcLineCaps.dwZCmpCaps = wined3d_caps.ZCmpCaps;
1282 caps->dpcLineCaps.dwSrcBlendCaps = wined3d_caps.SrcBlendCaps;
1283 caps->dpcLineCaps.dwDestBlendCaps = wined3d_caps.DestBlendCaps;
1284 caps->dpcLineCaps.dwAlphaCmpCaps = wined3d_caps.AlphaCmpCaps;
1285 caps->dpcLineCaps.dwShadeCaps = wined3d_caps.ShadeCaps;
1286 caps->dpcLineCaps.dwTextureCaps = wined3d_caps.TextureCaps;
1287 caps->dpcLineCaps.dwTextureFilterCaps = wined3d_caps.TextureFilterCaps;
1288 caps->dpcLineCaps.dwTextureAddressCaps = wined3d_caps.TextureAddressCaps;
1290 caps->dwMaxTextureWidth = wined3d_caps.MaxTextureWidth;
1291 caps->dwMaxTextureHeight = wined3d_caps.MaxTextureHeight;
1293 caps->dwMaxTextureRepeat = wined3d_caps.MaxTextureRepeat;
1294 caps->dwMaxTextureAspectRatio = wined3d_caps.MaxTextureAspectRatio;
1295 caps->dwMaxAnisotropy = wined3d_caps.MaxAnisotropy;
1296 caps->dvMaxVertexW = wined3d_caps.MaxVertexW;
1298 caps->dvGuardBandLeft = wined3d_caps.GuardBandLeft;
1299 caps->dvGuardBandTop = wined3d_caps.GuardBandTop;
1300 caps->dvGuardBandRight = wined3d_caps.GuardBandRight;
1301 caps->dvGuardBandBottom = wined3d_caps.GuardBandBottom;
1303 caps->dvExtentsAdjust = wined3d_caps.ExtentsAdjust;
1304 caps->dwStencilCaps = wined3d_caps.StencilCaps;
1306 caps->dwFVFCaps = wined3d_caps.FVFCaps;
1307 caps->dwTextureOpCaps = wined3d_caps.TextureOpCaps;
1309 caps->dwVertexProcessingCaps = wined3d_caps.VertexProcessingCaps;
1310 caps->dwMaxActiveLights = wined3d_caps.MaxActiveLights;
1312 /* Remove all non-d3d7 caps */
1313 caps->dwDevCaps &= (
1314 D3DDEVCAPS_FLOATTLVERTEX | D3DDEVCAPS_SORTINCREASINGZ | D3DDEVCAPS_SORTDECREASINGZ |
1315 D3DDEVCAPS_SORTEXACT | D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY |
1316 D3DDEVCAPS_TLVERTEXSYSTEMMEMORY | D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY |
1317 D3DDEVCAPS_TEXTUREVIDEOMEMORY | D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP |
1318 D3DDEVCAPS_TEXTURENONLOCALVIDMEM | D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
1319 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT | D3DDEVCAPS_CANBLTSYSTONONLOCAL |
1320 D3DDEVCAPS_HWRASTERIZATION);
1322 caps->dwStencilCaps &= (
1323 D3DSTENCILCAPS_KEEP | D3DSTENCILCAPS_ZERO | D3DSTENCILCAPS_REPLACE |
1324 D3DSTENCILCAPS_INCRSAT | D3DSTENCILCAPS_DECRSAT | D3DSTENCILCAPS_INVERT |
1325 D3DSTENCILCAPS_INCR | D3DSTENCILCAPS_DECR);
1327 /* FVF caps ?*/
1329 caps->dwTextureOpCaps &= (
1330 D3DTEXOPCAPS_DISABLE | D3DTEXOPCAPS_SELECTARG1 | D3DTEXOPCAPS_SELECTARG2 |
1331 D3DTEXOPCAPS_MODULATE | D3DTEXOPCAPS_MODULATE2X | D3DTEXOPCAPS_MODULATE4X |
1332 D3DTEXOPCAPS_ADD | D3DTEXOPCAPS_ADDSIGNED | D3DTEXOPCAPS_ADDSIGNED2X |
1333 D3DTEXOPCAPS_SUBTRACT | D3DTEXOPCAPS_ADDSMOOTH | D3DTEXOPCAPS_BLENDTEXTUREALPHA |
1334 D3DTEXOPCAPS_BLENDFACTORALPHA | D3DTEXOPCAPS_BLENDTEXTUREALPHAPM | D3DTEXOPCAPS_BLENDCURRENTALPHA |
1335 D3DTEXOPCAPS_PREMODULATE | D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
1336 D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA | D3DTEXOPCAPS_BUMPENVMAP |
1337 D3DTEXOPCAPS_BUMPENVMAPLUMINANCE | D3DTEXOPCAPS_DOTPRODUCT3);
1339 caps->dwVertexProcessingCaps &= (
1340 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_VERTEXFOG |
1341 D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER);
1343 caps->dpcLineCaps.dwMiscCaps &= (
1344 D3DPMISCCAPS_MASKPLANES | D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP |
1345 D3DPMISCCAPS_CONFORMANT | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW |
1346 D3DPMISCCAPS_CULLCCW);
1348 caps->dpcLineCaps.dwRasterCaps &= (
1349 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ROP2 | D3DPRASTERCAPS_XOR |
1350 D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_SUBPIXEL |
1351 D3DPRASTERCAPS_SUBPIXELX | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE |
1352 D3DPRASTERCAPS_STIPPLE | D3DPRASTERCAPS_ANTIALIASSORTDEPENDENT | D3DPRASTERCAPS_ANTIALIASSORTINDEPENDENT |
1353 D3DPRASTERCAPS_ANTIALIASEDGES | D3DPRASTERCAPS_MIPMAPLODBIAS |
1354 D3DPRASTERCAPS_ZBUFFERLESSHSR | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY |
1355 D3DPRASTERCAPS_WBUFFER | D3DPRASTERCAPS_TRANSLUCENTSORTINDEPENDENT | D3DPRASTERCAPS_WFOG |
1356 D3DPRASTERCAPS_ZFOG | WINED3DPRASTERCAPS_DEPTHBIAS);
1357 if (caps->dpcLineCaps.dwRasterCaps & WINED3DPRASTERCAPS_DEPTHBIAS)
1358 caps->dpcLineCaps.dwRasterCaps = (caps->dpcLineCaps.dwRasterCaps | D3DPRASTERCAPS_ZBIAS)
1359 & ~WINED3DPRASTERCAPS_DEPTHBIAS;
1360 if (wined3d_caps.LineCaps & WINED3DLINECAPS_ANTIALIAS)
1361 caps->dpcLineCaps.dwRasterCaps |= D3DPRASTERCAPS_ANTIALIASEDGES;
1363 caps->dpcLineCaps.dwZCmpCaps &= (
1364 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
1365 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
1366 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
1368 caps->dpcLineCaps.dwSrcBlendCaps &= (
1369 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
1370 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
1371 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
1372 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
1373 D3DPBLENDCAPS_BOTHINVSRCALPHA);
1375 caps->dpcLineCaps.dwDestBlendCaps &= (
1376 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
1377 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
1378 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
1379 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
1380 D3DPBLENDCAPS_BOTHINVSRCALPHA);
1382 caps->dpcLineCaps.dwAlphaCmpCaps &= (
1383 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
1384 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
1385 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
1387 caps->dpcLineCaps.dwShadeCaps &= (
1388 D3DPSHADECAPS_COLORFLATMONO | D3DPSHADECAPS_COLORFLATRGB | D3DPSHADECAPS_COLORGOURAUDMONO |
1389 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_COLORPHONGMONO | D3DPSHADECAPS_COLORPHONGRGB |
1390 D3DPSHADECAPS_SPECULARFLATMONO | D3DPSHADECAPS_SPECULARFLATRGB | D3DPSHADECAPS_SPECULARGOURAUDMONO |
1391 D3DPSHADECAPS_SPECULARGOURAUDRGB | D3DPSHADECAPS_SPECULARPHONGMONO | D3DPSHADECAPS_SPECULARPHONGRGB |
1392 D3DPSHADECAPS_ALPHAFLATBLEND | D3DPSHADECAPS_ALPHAFLATSTIPPLED | D3DPSHADECAPS_ALPHAGOURAUDBLEND |
1393 D3DPSHADECAPS_ALPHAGOURAUDSTIPPLED | D3DPSHADECAPS_ALPHAPHONGBLEND | D3DPSHADECAPS_ALPHAPHONGSTIPPLED |
1394 D3DPSHADECAPS_FOGFLAT | D3DPSHADECAPS_FOGGOURAUD | D3DPSHADECAPS_FOGPHONG);
1396 caps->dpcLineCaps.dwTextureCaps &= (
1397 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
1398 D3DPTEXTURECAPS_TRANSPARENCY | D3DPTEXTURECAPS_BORDER | D3DPTEXTURECAPS_SQUAREONLY |
1399 D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE | D3DPTEXTURECAPS_ALPHAPALETTE| D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
1400 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_COLORKEYBLEND);
1402 caps->dpcLineCaps.dwTextureFilterCaps &= (
1403 D3DPTFILTERCAPS_NEAREST | D3DPTFILTERCAPS_LINEAR | D3DPTFILTERCAPS_MIPNEAREST |
1404 D3DPTFILTERCAPS_MIPLINEAR | D3DPTFILTERCAPS_LINEARMIPNEAREST | D3DPTFILTERCAPS_LINEARMIPLINEAR |
1405 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
1406 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
1407 D3DPTFILTERCAPS_MAGFLINEAR | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC |
1408 D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC);
1410 caps->dpcLineCaps.dwTextureAddressCaps &= (
1411 D3DPTADDRESSCAPS_WRAP | D3DPTADDRESSCAPS_MIRROR | D3DPTADDRESSCAPS_CLAMP |
1412 D3DPTADDRESSCAPS_BORDER | D3DPTADDRESSCAPS_INDEPENDENTUV);
1414 if (!(caps->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2))
1416 /* DirectX7 always has the np2 flag set, no matter what the card
1417 * supports. Some old games (Rollcage) check the caps incorrectly.
1418 * If wined3d supports nonpow2 textures it also has np2 conditional
1419 * support. */
1420 caps->dpcLineCaps.dwTextureCaps |= D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL;
1423 /* Fill the missing members, and do some fixup */
1424 caps->dpcLineCaps.dwSize = sizeof(caps->dpcLineCaps);
1425 caps->dpcLineCaps.dwTextureBlendCaps = D3DPTBLENDCAPS_ADD
1426 | D3DPTBLENDCAPS_MODULATEMASK
1427 | D3DPTBLENDCAPS_COPY
1428 | D3DPTBLENDCAPS_DECAL
1429 | D3DPTBLENDCAPS_DECALALPHA
1430 | D3DPTBLENDCAPS_DECALMASK
1431 | D3DPTBLENDCAPS_MODULATE
1432 | D3DPTBLENDCAPS_MODULATEALPHA;
1433 caps->dpcLineCaps.dwStippleWidth = 32;
1434 caps->dpcLineCaps.dwStippleHeight = 32;
1435 /* Use the same for the TriCaps */
1436 caps->dpcTriCaps = caps->dpcLineCaps;
1438 caps->dwDeviceRenderBitDepth = DDBD_16 | DDBD_24 | DDBD_32;
1439 caps->dwDeviceZBufferBitDepth = DDBD_16 | DDBD_24;
1440 caps->dwMinTextureWidth = 1;
1441 caps->dwMinTextureHeight = 1;
1443 /* Convert DWORDs safely to WORDs */
1444 caps->wMaxTextureBlendStages = min(wined3d_caps.MaxTextureBlendStages, 0xffff);
1445 caps->wMaxSimultaneousTextures = min(wined3d_caps.MaxSimultaneousTextures, 0xffff);
1446 caps->wMaxUserClipPlanes = min(wined3d_caps.MaxUserClipPlanes, D3DMAXUSERCLIPPLANES);
1447 caps->wMaxVertexBlendMatrices = min(wined3d_caps.MaxVertexBlendMatrices, 0xffff);
1449 caps->deviceGUID = IID_IDirect3DTnLHalDevice;
1451 caps->dwReserved1 = 0;
1452 caps->dwReserved2 = 0;
1453 caps->dwReserved3 = 0;
1454 caps->dwReserved4 = 0;
1456 return DD_OK;
1459 /*****************************************************************************
1460 * IDirectDraw7::GetCaps
1462 * Returns the drives capabilities
1464 * Used for version 1, 2, 4 and 7
1466 * Params:
1467 * DriverCaps: Structure to write the Hardware accelerated caps to
1468 * HelCaps: Structure to write the emulation caps to
1470 * Returns
1471 * This implementation returns DD_OK only
1473 *****************************************************************************/
1474 static HRESULT WINAPI ddraw7_GetCaps(IDirectDraw7 *iface, DDCAPS *DriverCaps, DDCAPS *HELCaps)
1476 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1477 DDSCAPS2 ddscaps = {0, 0, 0, {0}};
1478 struct wined3d_caps winecaps;
1479 DDCAPS caps;
1480 HRESULT hr;
1482 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, DriverCaps, HELCaps);
1484 /* One structure must be != NULL */
1485 if (!DriverCaps && !HELCaps)
1487 WARN("Invalid parameters.\n");
1488 return DDERR_INVALIDPARAMS;
1491 memset(&caps, 0, sizeof(caps));
1492 memset(&winecaps, 0, sizeof(winecaps));
1493 caps.dwSize = sizeof(caps);
1495 wined3d_mutex_lock();
1496 hr = wined3d_device_get_device_caps(ddraw->wined3d_device, &winecaps);
1497 if (FAILED(hr))
1499 WARN("Failed to get device caps, %#x.\n", hr);
1500 wined3d_mutex_unlock();
1501 return hr;
1504 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
1505 if (FAILED(hr))
1507 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
1508 wined3d_mutex_unlock();
1509 return hr;
1512 hr = IDirectDraw7_GetFourCCCodes(iface, &caps.dwNumFourCCCodes, NULL);
1513 wined3d_mutex_unlock();
1514 if (FAILED(hr))
1516 WARN("IDirectDraw7::GetFourCCCodes failed\n");
1517 return hr;
1520 caps.dwCaps = winecaps.ddraw_caps.caps;
1521 caps.dwCaps2 = winecaps.ddraw_caps.caps2;
1522 caps.dwCKeyCaps = winecaps.ddraw_caps.color_key_caps;
1523 caps.dwFXCaps = winecaps.ddraw_caps.fx_caps;
1524 caps.dwPalCaps = DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE;
1525 caps.ddsCaps.dwCaps = winecaps.ddraw_caps.dds_caps;
1526 caps.dwSVBCaps = winecaps.ddraw_caps.svb_caps;
1527 caps.dwSVBCKeyCaps = winecaps.ddraw_caps.svb_color_key_caps;
1528 caps.dwSVBFXCaps = winecaps.ddraw_caps.svb_fx_caps;
1529 caps.dwVSBCaps = winecaps.ddraw_caps.vsb_caps;
1530 caps.dwVSBCKeyCaps = winecaps.ddraw_caps.vsb_color_key_caps;
1531 caps.dwVSBFXCaps = winecaps.ddraw_caps.vsb_fx_caps;
1532 caps.dwSSBCaps = winecaps.ddraw_caps.ssb_caps;
1533 caps.dwSSBCKeyCaps = winecaps.ddraw_caps.ssb_color_key_caps;
1534 caps.dwSSBFXCaps = winecaps.ddraw_caps.ssb_fx_caps;
1536 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
1537 caps.dwAlignStrideAlign = DDRAW_STRIDE_ALIGNMENT;
1539 caps.ddsOldCaps.dwCaps = caps.ddsCaps.dwCaps;
1541 if(DriverCaps)
1543 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
1544 if (TRACE_ON(ddraw))
1546 TRACE("Driver Caps :\n");
1547 DDRAW_dump_DDCAPS(DriverCaps);
1551 if(HELCaps)
1553 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
1554 if (TRACE_ON(ddraw))
1556 TRACE("HEL Caps :\n");
1557 DDRAW_dump_DDCAPS(HELCaps);
1561 return DD_OK;
1564 static HRESULT WINAPI ddraw4_GetCaps(IDirectDraw4 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1566 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1568 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1570 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1573 static HRESULT WINAPI ddraw2_GetCaps(IDirectDraw2 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1575 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1577 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1579 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1582 static HRESULT WINAPI ddraw1_GetCaps(IDirectDraw *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1584 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1586 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1588 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1591 /*****************************************************************************
1592 * IDirectDraw7::Compact
1594 * No idea what it does, MSDN says it's not implemented.
1596 * Returns
1597 * DD_OK, but this is unchecked
1599 *****************************************************************************/
1600 static HRESULT WINAPI ddraw7_Compact(IDirectDraw7 *iface)
1602 TRACE("iface %p.\n", iface);
1604 return DD_OK;
1607 static HRESULT WINAPI ddraw4_Compact(IDirectDraw4 *iface)
1609 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1611 TRACE("iface %p.\n", iface);
1613 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1616 static HRESULT WINAPI ddraw2_Compact(IDirectDraw2 *iface)
1618 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1620 TRACE("iface %p.\n", iface);
1622 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1625 static HRESULT WINAPI ddraw1_Compact(IDirectDraw *iface)
1627 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1629 TRACE("iface %p.\n", iface);
1631 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1634 /*****************************************************************************
1635 * IDirectDraw7::GetDisplayMode
1637 * Returns information about the current display mode
1639 * Exists in versions 1, 2, 4 and 7
1641 * Params:
1642 * DDSD: Address of a surface description structure to write the info to
1644 * Returns
1645 * DD_OK
1647 *****************************************************************************/
1648 static HRESULT WINAPI ddraw7_GetDisplayMode(IDirectDraw7 *iface, DDSURFACEDESC2 *DDSD)
1650 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1651 struct wined3d_display_mode mode;
1652 HRESULT hr;
1654 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
1656 /* This seems sane */
1657 if (!DDSD || (DDSD->dwSize != sizeof(DDSURFACEDESC) && DDSD->dwSize != sizeof(DDSURFACEDESC2)))
1658 return DDERR_INVALIDPARAMS;
1660 wined3d_mutex_lock();
1662 if (FAILED(hr = wined3d_output_get_display_mode(ddraw->wined3d_output, &mode, NULL)))
1664 ERR("Failed to get display mode, hr %#x.\n", hr);
1665 wined3d_mutex_unlock();
1666 return hr;
1669 memset(DDSD, 0, DDSD->dwSize);
1670 DDSD->dwSize = sizeof(*DDSD);
1671 DDSD->dwFlags = DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
1672 DDSD->dwWidth = mode.width;
1673 DDSD->dwHeight = mode.height;
1674 DDSD->u2.dwRefreshRate = mode.refresh_rate;
1675 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
1676 ddrawformat_from_wined3dformat(&DDSD->u4.ddpfPixelFormat, mode.format_id);
1677 DDSD->u1.lPitch = mode.width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
1679 if(TRACE_ON(ddraw))
1681 TRACE("Returning surface desc :\n");
1682 DDRAW_dump_surface_desc(DDSD);
1685 wined3d_mutex_unlock();
1687 return DD_OK;
1690 static HRESULT WINAPI ddraw4_GetDisplayMode(IDirectDraw4 *iface, DDSURFACEDESC2 *surface_desc)
1692 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1694 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1696 return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, surface_desc);
1699 static HRESULT WINAPI ddraw2_GetDisplayMode(IDirectDraw2 *iface, DDSURFACEDESC *surface_desc)
1701 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1702 HRESULT hr;
1704 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1706 hr = ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1707 if (SUCCEEDED(hr)) surface_desc->dwSize = sizeof(*surface_desc);
1708 return hr;
1711 static HRESULT WINAPI ddraw1_GetDisplayMode(IDirectDraw *iface, DDSURFACEDESC *surface_desc)
1713 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1714 HRESULT hr;
1716 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1718 hr = ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1719 if (SUCCEEDED(hr)) surface_desc->dwSize = sizeof(*surface_desc);
1720 return hr;
1723 /*****************************************************************************
1724 * IDirectDraw7::GetFourCCCodes
1726 * Returns an array of supported FourCC codes.
1728 * Exists in versions 1, 2, 4 and 7
1730 * Params:
1731 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
1732 * of enumerated codes
1733 * Codes: Pointer to an array of DWORDs where the supported codes are written
1734 * to
1736 * Returns
1737 * Always returns DD_OK, as it's a stub for now
1739 *****************************************************************************/
1740 static HRESULT WINAPI ddraw7_GetFourCCCodes(IDirectDraw7 *iface, DWORD *NumCodes, DWORD *Codes)
1742 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1743 static const enum wined3d_format_id formats[] =
1745 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
1746 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
1747 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
1749 struct wined3d_display_mode mode;
1750 DWORD count = 0, i, outsize;
1751 HRESULT hr;
1753 TRACE("iface %p, codes_count %p, codes %p.\n", iface, NumCodes, Codes);
1755 if (FAILED(hr = wined3d_output_get_display_mode(ddraw->wined3d_output, &mode, NULL)))
1757 ERR("Failed to get display mode, hr %#x.\n", hr);
1758 return hr;
1761 outsize = NumCodes && Codes ? *NumCodes : 0;
1763 for (i = 0; i < ARRAY_SIZE(formats); ++i)
1765 if (SUCCEEDED(wined3d_check_device_format(ddraw->wined3d, ddraw->wined3d_adapter, WINED3D_DEVICE_TYPE_HAL,
1766 mode.format_id, 0, 0, WINED3D_RTYPE_TEXTURE_2D, formats[i])))
1768 if (count < outsize)
1769 Codes[count] = formats[i];
1770 ++count;
1773 if(NumCodes) {
1774 TRACE("Returning %u FourCC codes\n", count);
1775 *NumCodes = count;
1778 return DD_OK;
1781 static HRESULT WINAPI ddraw4_GetFourCCCodes(IDirectDraw4 *iface, DWORD *codes_count, DWORD *codes)
1783 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1785 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1787 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1790 static HRESULT WINAPI ddraw2_GetFourCCCodes(IDirectDraw2 *iface, DWORD *codes_count, DWORD *codes)
1792 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1794 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1796 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1799 static HRESULT WINAPI ddraw1_GetFourCCCodes(IDirectDraw *iface, DWORD *codes_count, DWORD *codes)
1801 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1803 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1805 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1808 static HRESULT WINAPI ddraw7_GetMonitorFrequency(IDirectDraw7 *iface, DWORD *frequency)
1810 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1811 struct wined3d_display_mode mode;
1812 HRESULT hr;
1814 TRACE("iface %p, frequency %p.\n", iface, frequency);
1816 wined3d_mutex_lock();
1817 hr = wined3d_output_get_display_mode(ddraw->wined3d_output, &mode, NULL);
1818 wined3d_mutex_unlock();
1819 if (FAILED(hr))
1821 WARN("Failed to get display mode, hr %#x.\n", hr);
1822 return hr;
1825 *frequency = mode.refresh_rate;
1827 return DD_OK;
1830 static HRESULT WINAPI ddraw4_GetMonitorFrequency(IDirectDraw4 *iface, DWORD *frequency)
1832 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1834 TRACE("iface %p, frequency %p.\n", iface, frequency);
1836 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1839 static HRESULT WINAPI ddraw2_GetMonitorFrequency(IDirectDraw2 *iface, DWORD *frequency)
1841 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1843 TRACE("iface %p, frequency %p.\n", iface, frequency);
1845 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1848 static HRESULT WINAPI ddraw1_GetMonitorFrequency(IDirectDraw *iface, DWORD *frequency)
1850 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1852 TRACE("iface %p, frequency %p.\n", iface, frequency);
1854 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1857 static HRESULT WINAPI ddraw7_GetVerticalBlankStatus(IDirectDraw7 *iface, BOOL *status)
1859 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1860 struct wined3d_raster_status raster_status;
1861 HRESULT hr;
1863 TRACE("iface %p, status %p.\n", iface, status);
1865 if(!status)
1866 return DDERR_INVALIDPARAMS;
1868 wined3d_mutex_lock();
1869 hr = wined3d_output_get_raster_status(ddraw->wined3d_output, &raster_status);
1870 wined3d_mutex_unlock();
1871 if (FAILED(hr))
1873 WARN("Failed to get raster status, hr %#x.\n", hr);
1874 return hr;
1877 *status = raster_status.in_vblank;
1879 return DD_OK;
1882 static HRESULT WINAPI ddraw4_GetVerticalBlankStatus(IDirectDraw4 *iface, BOOL *status)
1884 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1886 TRACE("iface %p, status %p.\n", iface, status);
1888 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1891 static HRESULT WINAPI ddraw2_GetVerticalBlankStatus(IDirectDraw2 *iface, BOOL *status)
1893 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1895 TRACE("iface %p, status %p.\n", iface, status);
1897 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1900 static HRESULT WINAPI ddraw1_GetVerticalBlankStatus(IDirectDraw *iface, BOOL *status)
1902 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1904 TRACE("iface %p, status %p.\n", iface, status);
1906 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1909 /*****************************************************************************
1910 * IDirectDraw7::GetAvailableVidMem
1912 * Returns the total and free video memory
1914 * Params:
1915 * caps: Specifies the memory type asked for
1916 * total: Pointer to a DWORD to be filled with the total memory
1917 * free: Pointer to a DWORD to be filled with the free memory
1919 * Returns
1920 * DD_OK on success
1921 * DDERR_INVALIDPARAMS if free and total are NULL
1923 *****************************************************************************/
1924 static HRESULT WINAPI ddraw7_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *caps, DWORD *total,
1925 DWORD *free)
1927 unsigned int framebuffer_size, total_vidmem, free_vidmem;
1928 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1929 struct wined3d_display_mode mode;
1930 HRESULT hr = DD_OK;
1932 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1934 if (!total && !free)
1935 return DDERR_INVALIDPARAMS;
1937 if (TRACE_ON(ddraw))
1939 TRACE("Asked for memory with description: ");
1940 DDRAW_dump_DDSCAPS2(caps);
1942 wined3d_mutex_lock();
1944 /* Todo: System memory vs local video memory vs non-local video memory
1945 * The MSDN also mentions differences between texture memory and other
1946 * resources, but that's not important
1949 /* Some applications (e.g. 3DMark 2000) assume that the reported amount of
1950 * video memory doesn't include the memory used by the default framebuffer.
1952 if (FAILED(hr = wined3d_output_get_display_mode(ddraw->wined3d_output, &mode, NULL)))
1954 WARN("Failed to get display mode, hr %#x.\n", hr);
1955 wined3d_mutex_unlock();
1956 return hr;
1958 framebuffer_size = wined3d_calculate_format_pitch(ddraw->wined3d_adapter,
1959 mode.format_id, mode.width);
1960 framebuffer_size *= mode.height;
1962 if (free)
1964 free_vidmem = wined3d_device_get_available_texture_mem(ddraw->wined3d_device);
1965 *free = framebuffer_size > free_vidmem ? 0 : free_vidmem - framebuffer_size;
1966 TRACE("Free video memory %#x.\n", *free);
1969 if (total)
1971 struct wined3d_adapter_identifier desc = {0};
1973 hr = wined3d_adapter_get_identifier(ddraw->wined3d_adapter, 0, &desc);
1974 total_vidmem = min(UINT_MAX, desc.video_memory);
1975 *total = framebuffer_size > total_vidmem ? 0 : total_vidmem - framebuffer_size;
1976 TRACE("Total video memory %#x.\n", *total);
1979 wined3d_mutex_unlock();
1981 return hr;
1984 static HRESULT WINAPI ddraw4_GetAvailableVidMem(IDirectDraw4 *iface,
1985 DDSCAPS2 *caps, DWORD *total, DWORD *free)
1987 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1989 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1991 return ddraw7_GetAvailableVidMem(&ddraw->IDirectDraw7_iface, caps, total, free);
1994 static HRESULT WINAPI ddraw2_GetAvailableVidMem(IDirectDraw2 *iface,
1995 DDSCAPS *caps, DWORD *total, DWORD *free)
1997 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1998 DDSCAPS2 caps2;
2000 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
2002 DDRAW_Convert_DDSCAPS_1_To_2(caps, &caps2);
2003 return ddraw7_GetAvailableVidMem(&ddraw->IDirectDraw7_iface, &caps2, total, free);
2006 /*****************************************************************************
2007 * IDirectDraw7::Initialize
2009 * Initializes a DirectDraw interface.
2011 * Params:
2012 * GUID: Interface identifier. Well, don't know what this is really good
2013 * for
2015 * Returns
2016 * Returns DD_OK on the first call,
2017 * DDERR_ALREADYINITIALIZED on repeated calls
2019 *****************************************************************************/
2020 static HRESULT WINAPI ddraw7_Initialize(IDirectDraw7 *iface, GUID *guid)
2022 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2024 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
2026 if (ddraw->flags & DDRAW_INITIALIZED)
2027 return DDERR_ALREADYINITIALIZED;
2029 /* FIXME: To properly take the GUID into account we should call
2030 * ddraw_init() here instead of in DDRAW_Create(). */
2031 if (guid)
2032 FIXME("Ignoring guid %s.\n", debugstr_guid(guid));
2034 ddraw->flags |= DDRAW_INITIALIZED;
2035 return DD_OK;
2038 static HRESULT WINAPI ddraw4_Initialize(IDirectDraw4 *iface, GUID *guid)
2040 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2042 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
2044 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
2047 static HRESULT WINAPI ddraw2_Initialize(IDirectDraw2 *iface, GUID *guid)
2049 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2051 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
2053 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
2056 static HRESULT WINAPI ddraw1_Initialize(IDirectDraw *iface, GUID *guid)
2058 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2060 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
2062 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
2065 static HRESULT WINAPI d3d1_Initialize(IDirect3D *iface, REFIID riid)
2067 TRACE("iface %p, riid %s.\n", iface, debugstr_guid(riid));
2069 return DDERR_ALREADYINITIALIZED;
2072 /*****************************************************************************
2073 * IDirectDraw7::FlipToGDISurface
2075 * "Makes the surface that the GDI writes to the primary surface"
2076 * Looks like some windows specific thing we don't have to care about.
2077 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
2078 * show error boxes ;)
2079 * Well, just return DD_OK.
2081 * Returns:
2082 * Always returns DD_OK
2084 *****************************************************************************/
2085 static HRESULT WINAPI ddraw7_FlipToGDISurface(IDirectDraw7 *iface)
2087 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2088 IDirectDrawSurface7 *gdi_surface;
2089 struct ddraw_surface *gdi_impl;
2090 HRESULT hr;
2092 TRACE("iface %p.\n", iface);
2094 wined3d_mutex_lock();
2096 if (FAILED(hr = IDirectDraw7_GetGDISurface(iface, &gdi_surface)))
2098 WARN("Failed to retrieve GDI surface, hr %#x.\n", hr);
2099 wined3d_mutex_unlock();
2100 return hr;
2103 gdi_impl = impl_from_IDirectDrawSurface7(gdi_surface);
2104 if (gdi_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
2105 hr = DD_OK;
2106 else
2107 hr = IDirectDrawSurface7_Flip(&ddraw->primary->IDirectDrawSurface7_iface, gdi_surface, DDFLIP_WAIT);
2108 IDirectDrawSurface7_Release(gdi_surface);
2110 wined3d_mutex_unlock();
2112 return hr;
2115 static HRESULT WINAPI ddraw4_FlipToGDISurface(IDirectDraw4 *iface)
2117 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2119 TRACE("iface %p.\n", iface);
2121 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
2124 static HRESULT WINAPI ddraw2_FlipToGDISurface(IDirectDraw2 *iface)
2126 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2128 TRACE("iface %p.\n", iface);
2130 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
2133 static HRESULT WINAPI ddraw1_FlipToGDISurface(IDirectDraw *iface)
2135 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2137 TRACE("iface %p.\n", iface);
2139 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
2142 /*****************************************************************************
2143 * IDirectDraw7::WaitForVerticalBlank
2145 * This method allows applications to get in sync with the vertical blank
2146 * interval.
2147 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
2148 * redraw the screen, most likely because of this stub
2150 * Parameters:
2151 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
2152 * or DDWAITVB_BLOCKEND
2153 * h: Not used, according to MSDN
2155 * Returns:
2156 * Always returns DD_OK
2158 *****************************************************************************/
2159 static HRESULT WINAPI ddraw7_WaitForVerticalBlank(IDirectDraw7 *iface, DWORD Flags, HANDLE event)
2161 static BOOL hide;
2163 TRACE("iface %p, flags %#x, event %p.\n", iface, Flags, event);
2165 /* This function is called often, so print the fixme only once */
2166 if(!hide)
2168 FIXME("iface %p, flags %#x, event %p stub!\n", iface, Flags, event);
2169 hide = TRUE;
2172 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
2173 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
2174 return DDERR_UNSUPPORTED; /* unchecked */
2176 return DD_OK;
2179 static HRESULT WINAPI ddraw4_WaitForVerticalBlank(IDirectDraw4 *iface, DWORD flags, HANDLE event)
2181 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2183 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
2185 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
2188 static HRESULT WINAPI ddraw2_WaitForVerticalBlank(IDirectDraw2 *iface, DWORD flags, HANDLE event)
2190 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2192 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
2194 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
2197 static HRESULT WINAPI ddraw1_WaitForVerticalBlank(IDirectDraw *iface, DWORD flags, HANDLE event)
2199 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2201 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
2203 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
2206 static HRESULT WINAPI ddraw7_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
2208 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2209 struct wined3d_raster_status raster_status;
2210 HRESULT hr;
2212 TRACE("iface %p, line %p.\n", iface, Scanline);
2214 wined3d_mutex_lock();
2215 hr = wined3d_output_get_raster_status(ddraw->wined3d_output, &raster_status);
2216 wined3d_mutex_unlock();
2217 if (FAILED(hr))
2219 WARN("Failed to get raster status, hr %#x.\n", hr);
2220 return hr;
2223 *Scanline = raster_status.scan_line;
2225 if (raster_status.in_vblank)
2226 return DDERR_VERTICALBLANKINPROGRESS;
2228 return DD_OK;
2231 static HRESULT WINAPI ddraw4_GetScanLine(IDirectDraw4 *iface, DWORD *line)
2233 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2235 TRACE("iface %p, line %p.\n", iface, line);
2237 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
2240 static HRESULT WINAPI ddraw2_GetScanLine(IDirectDraw2 *iface, DWORD *line)
2242 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2244 TRACE("iface %p, line %p.\n", iface, line);
2246 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
2249 static HRESULT WINAPI ddraw1_GetScanLine(IDirectDraw *iface, DWORD *line)
2251 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2253 TRACE("iface %p, line %p.\n", iface, line);
2255 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
2258 static HRESULT WINAPI ddraw7_TestCooperativeLevel(IDirectDraw7 *iface)
2260 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2262 TRACE("iface %p.\n", iface);
2264 return ddraw->device_state == DDRAW_DEVICE_STATE_LOST ? DDERR_NOEXCLUSIVEMODE : DD_OK;
2267 static HRESULT WINAPI ddraw4_TestCooperativeLevel(IDirectDraw4 *iface)
2269 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2271 TRACE("iface %p.\n", iface);
2273 return ddraw7_TestCooperativeLevel(&ddraw->IDirectDraw7_iface);
2276 /*****************************************************************************
2277 * IDirectDraw7::GetGDISurface
2279 * Returns the surface that GDI is treating as the primary surface.
2280 * For Wine this is the front buffer
2282 * Params:
2283 * GDISurface: Address to write the surface pointer to
2285 * Returns:
2286 * DD_OK if the surface was found
2287 * DDERR_NOTFOUND if the GDI surface wasn't found
2289 *****************************************************************************/
2290 static HRESULT WINAPI ddraw7_GetGDISurface(IDirectDraw7 *iface, IDirectDrawSurface7 **surface)
2292 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2293 struct ddraw_surface *ddraw_surface;
2295 TRACE("iface %p, surface %p.\n", iface, surface);
2297 wined3d_mutex_lock();
2299 if (!ddraw->gdi_surface || !(ddraw_surface = wined3d_texture_get_sub_resource_parent(ddraw->gdi_surface, 0)))
2301 WARN("GDI surface not available.\n");
2302 *surface = NULL;
2303 wined3d_mutex_unlock();
2304 return DDERR_NOTFOUND;
2306 *surface = &ddraw_surface->IDirectDrawSurface7_iface;
2307 IDirectDrawSurface7_AddRef(*surface);
2309 wined3d_mutex_unlock();
2311 return DD_OK;
2314 static HRESULT WINAPI ddraw4_GetGDISurface(IDirectDraw4 *iface, IDirectDrawSurface4 **surface)
2316 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2317 struct ddraw_surface *surface_impl;
2318 IDirectDrawSurface7 *surface7;
2319 HRESULT hr;
2321 TRACE("iface %p, surface %p.\n", iface, surface);
2323 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2324 if (FAILED(hr))
2326 *surface = NULL;
2327 return hr;
2329 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2330 *surface = &surface_impl->IDirectDrawSurface4_iface;
2331 IDirectDrawSurface4_AddRef(*surface);
2332 IDirectDrawSurface7_Release(surface7);
2334 return hr;
2337 static HRESULT WINAPI ddraw2_GetGDISurface(IDirectDraw2 *iface, IDirectDrawSurface **surface)
2339 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2340 struct ddraw_surface *surface_impl;
2341 IDirectDrawSurface7 *surface7;
2342 HRESULT hr;
2344 TRACE("iface %p, surface %p.\n", iface, surface);
2346 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2347 if (FAILED(hr))
2349 *surface = NULL;
2350 return hr;
2352 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2353 *surface = &surface_impl->IDirectDrawSurface_iface;
2354 IDirectDrawSurface_AddRef(*surface);
2355 IDirectDrawSurface7_Release(surface7);
2357 return hr;
2360 static HRESULT WINAPI ddraw1_GetGDISurface(IDirectDraw *iface, IDirectDrawSurface **surface)
2362 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2363 struct ddraw_surface *surface_impl;
2364 IDirectDrawSurface7 *surface7;
2365 HRESULT hr;
2367 TRACE("iface %p, surface %p.\n", iface, surface);
2369 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2370 if (FAILED(hr))
2372 *surface = NULL;
2373 return hr;
2375 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2376 *surface = &surface_impl->IDirectDrawSurface_iface;
2377 IDirectDrawSurface_AddRef(*surface);
2378 IDirectDrawSurface7_Release(surface7);
2380 return hr;
2383 struct displaymodescallback_context
2385 LPDDENUMMODESCALLBACK func;
2386 void *context;
2389 static HRESULT CALLBACK EnumDisplayModesCallbackThunk(DDSURFACEDESC2 *surface_desc, void *context)
2391 struct displaymodescallback_context *cbcontext = context;
2392 DDSURFACEDESC desc;
2394 DDSD2_to_DDSD(surface_desc, &desc);
2395 return cbcontext->func(&desc, cbcontext->context);
2398 /*****************************************************************************
2399 * IDirectDraw7::EnumDisplayModes
2401 * Enumerates the supported Display modes. The modes can be filtered with
2402 * the DDSD parameter.
2404 * Params:
2405 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES. For old ddraw
2406 * versions (3 and older?) this is reserved and must be 0.
2407 * DDSD: Surface description to filter the modes
2408 * Context: Pointer passed back to the callback function
2409 * cb: Application-provided callback function
2411 * Returns:
2412 * DD_OK on success
2413 * DDERR_INVALIDPARAMS if the callback wasn't set
2415 *****************************************************************************/
2416 static HRESULT WINAPI ddraw7_EnumDisplayModes(IDirectDraw7 *iface, DWORD Flags,
2417 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMMODESCALLBACK2 cb)
2419 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2420 struct wined3d_display_mode *enum_modes = NULL;
2421 struct wined3d_display_mode mode;
2422 unsigned int modenum, fmt;
2423 DDSURFACEDESC2 callback_sd;
2424 unsigned enum_mode_count = 0, enum_mode_array_size = 16;
2425 DDPIXELFORMAT pixelformat;
2427 static const enum wined3d_format_id checkFormatList[] =
2429 WINED3DFMT_B8G8R8X8_UNORM,
2430 WINED3DFMT_B5G6R5_UNORM,
2431 WINED3DFMT_P8_UINT,
2434 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2435 iface, Flags, DDSD, Context, cb);
2437 if (!cb)
2438 return DDERR_INVALIDPARAMS;
2440 if (!(enum_modes = heap_alloc(enum_mode_array_size * sizeof(*enum_modes))))
2441 return DDERR_OUTOFMEMORY;
2443 wined3d_mutex_lock();
2445 pixelformat.dwSize = sizeof(pixelformat);
2446 for(fmt = 0; fmt < ARRAY_SIZE(checkFormatList); fmt++)
2448 modenum = 0;
2449 while (wined3d_output_get_mode(ddraw->wined3d_output, checkFormatList[fmt],
2450 WINED3D_SCANLINE_ORDERING_UNKNOWN, modenum++, &mode) == WINED3D_OK)
2452 BOOL found = FALSE;
2453 unsigned i;
2455 ddrawformat_from_wined3dformat(&pixelformat, mode.format_id);
2456 if (DDSD)
2458 if (DDSD->dwFlags & DDSD_WIDTH && mode.width != DDSD->dwWidth)
2459 continue;
2460 if (DDSD->dwFlags & DDSD_HEIGHT && mode.height != DDSD->dwHeight)
2461 continue;
2462 if (DDSD->dwFlags & DDSD_REFRESHRATE && mode.refresh_rate != DDSD->u2.dwRefreshRate)
2463 continue;
2464 if (DDSD->dwFlags & DDSD_PIXELFORMAT
2465 && pixelformat.u1.dwRGBBitCount != DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount)
2466 continue;
2469 /* DX docs state EnumDisplayMode should return only unique modes */
2470 for (i = 0; i < enum_mode_count; i++)
2472 if (enum_modes[i].width == mode.width && enum_modes[i].height == mode.height
2473 && enum_modes[i].format_id == mode.format_id
2474 && (enum_modes[i].refresh_rate == mode.refresh_rate || !(Flags & DDEDM_REFRESHRATES)))
2476 found = TRUE;
2477 break;
2480 if(found) continue;
2482 memset(&callback_sd, 0, sizeof(callback_sd));
2483 callback_sd.dwSize = sizeof(callback_sd);
2484 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2486 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH|DDSD_REFRESHRATE;
2487 if (Flags & DDEDM_REFRESHRATES)
2488 callback_sd.u2.dwRefreshRate = mode.refresh_rate;
2490 callback_sd.dwWidth = mode.width;
2491 callback_sd.dwHeight = mode.height;
2493 callback_sd.u4.ddpfPixelFormat=pixelformat;
2495 /* Calc pitch and DWORD align like MSDN says */
2496 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.width;
2497 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
2499 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
2500 callback_sd.u2.dwRefreshRate);
2502 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
2504 TRACE("Application asked to terminate the enumeration\n");
2505 heap_free(enum_modes);
2506 wined3d_mutex_unlock();
2507 return DD_OK;
2510 if (enum_mode_count == enum_mode_array_size)
2512 struct wined3d_display_mode *new_enum_modes;
2514 enum_mode_array_size *= 2;
2515 if (!(new_enum_modes = heap_realloc(enum_modes, enum_mode_array_size * sizeof(*new_enum_modes))))
2517 heap_free(enum_modes);
2518 wined3d_mutex_unlock();
2519 return DDERR_OUTOFMEMORY;
2522 enum_modes = new_enum_modes;
2524 enum_modes[enum_mode_count++] = mode;
2528 TRACE("End of enumeration\n");
2529 heap_free(enum_modes);
2530 wined3d_mutex_unlock();
2532 return DD_OK;
2535 static HRESULT WINAPI ddraw4_EnumDisplayModes(IDirectDraw4 *iface, DWORD flags,
2536 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMMODESCALLBACK2 callback)
2538 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2540 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2541 iface, flags, surface_desc, context, callback);
2543 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags, surface_desc, context, callback);
2546 static HRESULT WINAPI ddraw2_EnumDisplayModes(IDirectDraw2 *iface, DWORD flags,
2547 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2549 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2550 struct displaymodescallback_context cbcontext;
2551 DDSURFACEDESC2 surface_desc2;
2553 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2554 iface, flags, surface_desc, context, callback);
2556 cbcontext.func = callback;
2557 cbcontext.context = context;
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);
2564 static HRESULT WINAPI ddraw1_EnumDisplayModes(IDirectDraw *iface, DWORD flags,
2565 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2567 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2568 struct displaymodescallback_context cbcontext;
2569 DDSURFACEDESC2 surface_desc2;
2571 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2572 iface, flags, surface_desc, context, callback);
2574 cbcontext.func = callback;
2575 cbcontext.context = context;
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);
2582 /*****************************************************************************
2583 * IDirectDraw7::EvaluateMode
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
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
2594 * Returns:
2595 * This implementation always DD_OK, because it's a stub
2597 *****************************************************************************/
2598 static HRESULT WINAPI ddraw7_EvaluateMode(IDirectDraw7 *iface, DWORD Flags, DWORD *Timeout)
2600 FIXME("iface %p, flags %#x, timeout %p stub!\n", iface, Flags, Timeout);
2602 /* When implementing this, implement it in WineD3D */
2604 return DD_OK;
2607 /*****************************************************************************
2608 * IDirectDraw7::GetDeviceIdentifier
2610 * Returns the device identifier, which gives information about the driver
2611 * Our device identifier is defined at the beginning of this file.
2613 * Params:
2614 * DDDI: Address for the returned structure
2615 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
2617 * Returns:
2618 * On success it returns DD_OK
2619 * DDERR_INVALIDPARAMS if DDDI is NULL
2621 *****************************************************************************/
2622 static HRESULT WINAPI ddraw7_GetDeviceIdentifier(IDirectDraw7 *iface,
2623 DDDEVICEIDENTIFIER2 *DDDI, DWORD Flags)
2625 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2626 struct wined3d_adapter_identifier adapter_id;
2627 HRESULT hr = S_OK;
2629 TRACE("iface %p, device_identifier %p, flags %#x.\n", iface, DDDI, Flags);
2631 if (!DDDI)
2632 return DDERR_INVALIDPARAMS;
2634 if (Flags & DDGDI_GETHOSTIDENTIFIER)
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
2643 memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
2644 return DD_OK;
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 wined3d_mutex_lock();
2653 hr = wined3d_adapter_get_identifier(ddraw->wined3d_adapter, WINED3DENUM_WHQL_LEVEL, &adapter_id);
2654 wined3d_mutex_unlock();
2655 if (FAILED(hr)) return hr;
2657 DDDI->liDriverVersion = adapter_id.driver_version;
2658 DDDI->dwVendorId = adapter_id.vendor_id;
2659 DDDI->dwDeviceId = adapter_id.device_id;
2660 DDDI->dwSubSysId = adapter_id.subsystem_id;
2661 DDDI->dwRevision = adapter_id.revision;
2662 DDDI->guidDeviceIdentifier = adapter_id.device_identifier;
2663 DDDI->dwWHQLLevel = adapter_id.whql_level;
2664 return DD_OK;
2667 static HRESULT WINAPI ddraw4_GetDeviceIdentifier(IDirectDraw4 *iface,
2668 DDDEVICEIDENTIFIER *identifier, DWORD flags)
2670 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2671 DDDEVICEIDENTIFIER2 identifier2;
2672 HRESULT hr;
2674 TRACE("iface %p, identifier %p, flags %#x.\n", iface, identifier, flags);
2676 hr = ddraw7_GetDeviceIdentifier(&ddraw->IDirectDraw7_iface, &identifier2, flags);
2677 DDRAW_Convert_DDDEVICEIDENTIFIER_2_To_1(&identifier2, identifier);
2679 return hr;
2682 /*****************************************************************************
2683 * IDirectDraw7::GetSurfaceFromDC
2685 * Returns the Surface for a GDI device context handle.
2686 * Is this related to IDirectDrawSurface::GetDC ???
2688 * Params:
2689 * hdc: hdc to return the surface for
2690 * Surface: Address to write the surface pointer to
2692 * Returns:
2693 * Always returns DD_OK because it's a stub
2695 *****************************************************************************/
2696 static HRESULT WINAPI ddraw7_GetSurfaceFromDC(IDirectDraw7 *iface,
2697 HDC dc, IDirectDrawSurface7 **surface)
2699 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2700 struct ddraw_surface *surface_impl;
2702 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2704 if (!surface)
2705 return E_INVALIDARG;
2707 if (!dc)
2708 goto done;
2710 wined3d_mutex_lock();
2711 LIST_FOR_EACH_ENTRY(surface_impl, &ddraw->surface_list, struct ddraw_surface, surface_list_entry)
2713 if (surface_impl->dc != dc)
2714 continue;
2716 TRACE("Found surface %p for dc %p.\n", surface_impl, dc);
2717 *surface = &surface_impl->IDirectDrawSurface7_iface;
2718 IDirectDrawSurface7_AddRef(*surface);
2719 wined3d_mutex_unlock();
2720 return DD_OK;
2722 wined3d_mutex_unlock();
2724 done:
2725 TRACE("No surface found for dc %p.\n", dc);
2726 *surface = NULL;
2727 return DDERR_NOTFOUND;
2730 static HRESULT WINAPI ddraw4_GetSurfaceFromDC(IDirectDraw4 *iface, HDC dc,
2731 IDirectDrawSurface4 **surface)
2733 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2734 struct ddraw_surface *surface_impl;
2735 IDirectDrawSurface7 *surface7;
2736 HRESULT hr;
2738 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2740 if (!surface) return E_INVALIDARG;
2742 hr = ddraw7_GetSurfaceFromDC(&ddraw->IDirectDraw7_iface, dc, &surface7);
2743 if (FAILED(hr))
2745 *surface = NULL;
2746 return hr;
2748 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2749 /* Tests say this is true */
2750 *surface = (IDirectDrawSurface4 *)&surface_impl->IDirectDrawSurface_iface;
2751 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
2752 IDirectDrawSurface7_Release(surface7);
2754 return hr;
2757 static HRESULT CALLBACK restore_callback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *desc, void *context)
2759 IDirectDrawSurface_Restore(surface);
2760 IDirectDrawSurface_Release(surface);
2762 return DDENUMRET_OK;
2765 static HRESULT WINAPI ddraw7_RestoreAllSurfaces(IDirectDraw7 *iface)
2767 TRACE("iface %p.\n", iface);
2769 return IDirectDraw7_EnumSurfaces(iface, DDENUMSURFACES_ALL | DDENUMSURFACES_DOESEXIST,
2770 NULL, NULL, restore_callback);
2773 static HRESULT WINAPI ddraw4_RestoreAllSurfaces(IDirectDraw4 *iface)
2775 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2777 TRACE("iface %p.\n", iface);
2779 return ddraw7_RestoreAllSurfaces(&ddraw->IDirectDraw7_iface);
2782 /*****************************************************************************
2783 * IDirectDraw7::StartModeTest
2785 * Tests the specified video modes to update the system registry with
2786 * refresh rate information. StartModeTest starts the mode test,
2787 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
2788 * isn't called within 15 seconds, the mode is failed automatically
2790 * As refresh rates are handled by the X server, I don't think this
2791 * Method is important
2793 * Params:
2794 * Modes: An array of mode specifications
2795 * NumModes: The number of modes in Modes
2796 * Flags: Some flags...
2798 * Returns:
2799 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
2800 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
2801 * otherwise DD_OK
2803 *****************************************************************************/
2804 static HRESULT WINAPI ddraw7_StartModeTest(IDirectDraw7 *iface, SIZE *Modes, DWORD NumModes, DWORD Flags)
2806 FIXME("iface %p, modes %p, mode_count %u, flags %#x partial stub!\n",
2807 iface, Modes, NumModes, Flags);
2809 /* This looks sane */
2810 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
2812 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
2813 * As it is not, DDERR_TESTFINISHED is returned
2814 * (hopefully that's correct
2816 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
2817 * well, that value doesn't (yet) exist in the wine headers, so ignore it
2820 return DD_OK;
2823 static HRESULT WINAPI ddraw7_CreateSurface(IDirectDraw7 *iface, DDSURFACEDESC2 *surface_desc,
2824 IDirectDrawSurface7 **surface, IUnknown *outer_unknown)
2826 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2827 struct ddraw_surface *impl;
2828 HRESULT hr;
2830 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2831 iface, surface_desc, surface, outer_unknown);
2833 wined3d_mutex_lock();
2835 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2837 WARN("Cooperative level not set.\n");
2838 wined3d_mutex_unlock();
2839 return DDERR_NOCOOPERATIVELEVELSET;
2842 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
2844 WARN("Application supplied invalid surface descriptor\n");
2845 wined3d_mutex_unlock();
2846 return DDERR_INVALIDPARAMS;
2849 __TRY
2851 *surface = NULL;
2853 __EXCEPT_PAGE_FAULT
2855 WARN("Surface pointer %p is invalid.\n", surface);
2856 wined3d_mutex_unlock();
2857 return DDERR_INVALIDPARAMS;
2859 __ENDTRY;
2861 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
2863 if (TRACE_ON(ddraw))
2865 TRACE(" (%p) Requesting surface desc :\n", iface);
2866 DDRAW_dump_surface_desc(surface_desc);
2869 WARN("Application tried to create an explicit front or back buffer\n");
2870 wined3d_mutex_unlock();
2871 return DDERR_INVALIDCAPS;
2874 hr = ddraw_surface_create(ddraw, surface_desc, &impl, outer_unknown, 7);
2875 wined3d_mutex_unlock();
2876 if (FAILED(hr))
2877 return hr;
2879 *surface = &impl->IDirectDrawSurface7_iface;
2880 IDirectDraw7_AddRef(iface);
2881 impl->ifaceToRelease = (IUnknown *)iface;
2883 return hr;
2886 static HRESULT WINAPI ddraw4_CreateSurface(IDirectDraw4 *iface,
2887 DDSURFACEDESC2 *surface_desc, IDirectDrawSurface4 **surface, IUnknown *outer_unknown)
2889 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2890 struct ddraw_surface *impl;
2891 HRESULT hr;
2893 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2894 iface, surface_desc, surface, outer_unknown);
2896 wined3d_mutex_lock();
2898 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2900 WARN("Cooperative level not set.\n");
2901 wined3d_mutex_unlock();
2902 return DDERR_NOCOOPERATIVELEVELSET;
2905 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
2907 WARN("Application supplied invalid surface descriptor\n");
2908 wined3d_mutex_unlock();
2909 return DDERR_INVALIDPARAMS;
2912 __TRY
2914 *surface = NULL;
2916 __EXCEPT_PAGE_FAULT
2918 WARN("Surface pointer %p is invalid.\n", surface);
2919 wined3d_mutex_unlock();
2920 return DDERR_INVALIDPARAMS;
2922 __ENDTRY;
2924 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
2926 if (TRACE_ON(ddraw))
2928 TRACE(" (%p) Requesting surface desc :\n", iface);
2929 DDRAW_dump_surface_desc(surface_desc);
2932 WARN("Application tried to create an explicit front or back buffer\n");
2933 wined3d_mutex_unlock();
2934 return DDERR_INVALIDCAPS;
2937 hr = ddraw_surface_create(ddraw, surface_desc, &impl, outer_unknown, 4);
2938 wined3d_mutex_unlock();
2939 if (FAILED(hr))
2940 return hr;
2942 *surface = &impl->IDirectDrawSurface4_iface;
2943 IDirectDraw4_AddRef(iface);
2944 impl->ifaceToRelease = (IUnknown *)iface;
2946 return hr;
2949 static HRESULT WINAPI ddraw2_CreateSurface(IDirectDraw2 *iface,
2950 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
2952 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2953 struct ddraw_surface *impl;
2954 HRESULT hr;
2955 DDSURFACEDESC2 surface_desc2;
2957 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2958 iface, surface_desc, surface, outer_unknown);
2960 wined3d_mutex_lock();
2962 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2964 WARN("Cooperative level not set.\n");
2965 wined3d_mutex_unlock();
2966 return DDERR_NOCOOPERATIVELEVELSET;
2969 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
2971 WARN("Application supplied invalid surface descriptor\n");
2972 wined3d_mutex_unlock();
2973 return DDERR_INVALIDPARAMS;
2976 __TRY
2978 *surface = NULL;
2980 __EXCEPT_PAGE_FAULT
2982 WARN("Surface pointer %p is invalid.\n", surface);
2983 wined3d_mutex_unlock();
2984 return DDERR_INVALIDPARAMS;
2986 __ENDTRY;
2988 DDSD_to_DDSD2(surface_desc, &surface_desc2);
2989 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
2991 if (TRACE_ON(ddraw))
2993 TRACE(" (%p) Requesting surface desc :\n", iface);
2994 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
2997 WARN("Application tried to create an explicit front or back buffer\n");
2998 wined3d_mutex_unlock();
2999 return DDERR_INVALIDCAPS;
3002 hr = ddraw_surface_create(ddraw, &surface_desc2, &impl, outer_unknown, 2);
3003 wined3d_mutex_unlock();
3004 if (FAILED(hr))
3005 return hr;
3007 *surface = &impl->IDirectDrawSurface_iface;
3008 impl->ifaceToRelease = NULL;
3010 return hr;
3013 static HRESULT WINAPI ddraw1_CreateSurface(IDirectDraw *iface,
3014 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3016 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3017 struct ddraw_surface *impl;
3018 HRESULT hr;
3019 DDSURFACEDESC2 surface_desc2;
3021 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3022 iface, surface_desc, surface, outer_unknown);
3024 wined3d_mutex_lock();
3026 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
3028 WARN("Cooperative level not set.\n");
3029 wined3d_mutex_unlock();
3030 return DDERR_NOCOOPERATIVELEVELSET;
3033 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3035 WARN("Application supplied invalid surface descriptor\n");
3036 wined3d_mutex_unlock();
3037 return DDERR_INVALIDPARAMS;
3040 __TRY
3042 *surface = NULL;
3044 __EXCEPT_PAGE_FAULT
3046 WARN("Surface pointer %p is invalid.\n", surface);
3047 wined3d_mutex_unlock();
3048 return DDERR_INVALIDPARAMS;
3050 __ENDTRY;
3052 if ((surface_desc->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER))
3053 == (DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER)
3054 || (surface_desc->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER))
3055 == ((DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER)))
3057 WARN("Application tried to create an explicit front or back buffer.\n");
3058 wined3d_mutex_unlock();
3059 return DDERR_INVALIDCAPS;
3062 DDSD_to_DDSD2(surface_desc, &surface_desc2);
3063 hr = ddraw_surface_create(ddraw, &surface_desc2, &impl, outer_unknown, 1);
3064 wined3d_mutex_unlock();
3065 if (FAILED(hr))
3066 return hr;
3068 *surface = &impl->IDirectDrawSurface_iface;
3069 impl->ifaceToRelease = NULL;
3071 return hr;
3074 static BOOL
3075 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
3076 const DDPIXELFORMAT *provided)
3078 /* Some flags must be present in both or neither for a match. */
3079 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
3080 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
3081 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
3083 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3084 return FALSE;
3086 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
3087 return FALSE;
3089 if (requested->dwFlags & DDPF_FOURCC)
3090 if (requested->dwFourCC != provided->dwFourCC)
3091 return FALSE;
3093 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
3094 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3095 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
3096 return FALSE;
3098 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3099 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3100 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
3101 return FALSE;
3103 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
3104 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
3105 return FALSE;
3107 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
3108 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3109 |DDPF_BUMPDUDV))
3110 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
3111 return FALSE;
3113 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
3114 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
3115 return FALSE;
3117 return TRUE;
3120 static BOOL ddraw_match_surface_desc(const DDSURFACEDESC2 *requested, const DDSURFACEDESC2 *provided)
3122 struct compare_info
3124 DWORD flag;
3125 ptrdiff_t offset;
3126 size_t size;
3129 #define CMP(FLAG, FIELD) \
3130 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
3131 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
3133 static const struct compare_info compare[] =
3135 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
3136 CMP(BACKBUFFERCOUNT, u5.dwBackBufferCount),
3137 CMP(CAPS, ddsCaps),
3138 CMP(CKDESTBLT, ddckCKDestBlt),
3139 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
3140 CMP(CKSRCBLT, ddckCKSrcBlt),
3141 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
3142 CMP(HEIGHT, dwHeight),
3143 CMP(LINEARSIZE, u1 /* dwLinearSize */),
3144 CMP(LPSURFACE, lpSurface),
3145 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
3146 CMP(PITCH, u1 /* lPitch */),
3147 /* PIXELFORMAT: manual */
3148 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
3149 CMP(TEXTURESTAGE, dwTextureStage),
3150 CMP(WIDTH, dwWidth),
3151 /* ZBUFFERBITDEPTH: "obsolete" */
3154 #undef CMP
3156 unsigned int i;
3158 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3159 return FALSE;
3161 for (i=0; i < ARRAY_SIZE(compare); i++)
3163 if (requested->dwFlags & compare[i].flag
3164 && memcmp((const char *)provided + compare[i].offset,
3165 (const char *)requested + compare[i].offset,
3166 compare[i].size) != 0)
3167 return FALSE;
3170 if (requested->dwFlags & DDSD_PIXELFORMAT)
3172 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
3173 &provided->u4.ddpfPixelFormat))
3174 return FALSE;
3177 return TRUE;
3180 struct surfacescallback2_context
3182 LPDDENUMSURFACESCALLBACK2 func;
3183 void *context;
3186 struct surfacescallback_context
3188 LPDDENUMSURFACESCALLBACK func;
3189 void *context;
3192 static HRESULT CALLBACK EnumSurfacesCallback2Thunk(IDirectDrawSurface7 *surface,
3193 DDSURFACEDESC2 *surface_desc, void *context)
3195 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3196 struct surfacescallback2_context *cbcontext = context;
3198 if (!surface)
3199 return cbcontext->func(NULL, surface_desc, cbcontext->context);
3201 IDirectDrawSurface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
3202 IDirectDrawSurface7_Release(surface);
3204 return cbcontext->func(&surface_impl->IDirectDrawSurface4_iface,
3205 surface_desc, cbcontext->context);
3208 static HRESULT CALLBACK EnumSurfacesCallbackThunk(IDirectDrawSurface7 *surface,
3209 DDSURFACEDESC2 *surface_desc, void *context)
3211 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3212 struct surfacescallback_context *cbcontext = context;
3214 if (!surface)
3215 return cbcontext->func(NULL, (DDSURFACEDESC *)surface_desc, cbcontext->context);
3217 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
3218 IDirectDrawSurface7_Release(surface);
3220 return cbcontext->func(&surface_impl->IDirectDrawSurface_iface,
3221 (DDSURFACEDESC *)surface_desc, cbcontext->context);
3224 struct enum_surface_mode_params
3226 IDirectDraw7 *ddraw;
3227 const DDSURFACEDESC2 *desc;
3228 LPDDENUMSURFACESCALLBACK7 callback;
3229 void *context;
3232 static HRESULT CALLBACK enum_surface_mode_callback(DDSURFACEDESC2 *surface_desc, void *context)
3234 const struct enum_surface_mode_params *params = context;
3235 DDSURFACEDESC2 desc = *params->desc;
3236 IDirectDrawSurface7 *surface;
3238 desc.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT | DDSD_PITCH | DDSD_PIXELFORMAT;
3239 desc.dwWidth = surface_desc->dwWidth;
3240 desc.dwHeight = surface_desc->dwHeight;
3241 desc.u1.lPitch = surface_desc->u1.lPitch;
3242 desc.u4.ddpfPixelFormat = surface_desc->u4.ddpfPixelFormat;
3244 if (SUCCEEDED(ddraw7_CreateSurface(params->ddraw, &desc, &surface, NULL)))
3246 IDirectDrawSurface7_Release(surface);
3247 return params->callback(NULL, &desc, params->context);
3249 return DDENUMRET_OK;
3252 /*****************************************************************************
3253 * IDirectDraw7::EnumSurfaces
3255 * Loops through all surfaces attached to this device and calls the
3256 * application callback. This can't be relayed to WineD3DDevice,
3257 * because some WineD3DSurfaces' parents are IParent objects
3259 * Params:
3260 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
3261 * DDSD: Description to filter for
3262 * Context: Application-provided pointer, it's passed unmodified to the
3263 * Callback function
3264 * Callback: Address to call for each surface
3266 * Returns:
3267 * DDERR_INVALIDPARAMS if the callback is NULL
3268 * DD_OK on success
3270 *****************************************************************************/
3271 static HRESULT WINAPI ddraw7_EnumSurfaces(IDirectDraw7 *iface, DWORD flags,
3272 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMSURFACESCALLBACK7 callback)
3274 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
3275 HRESULT hr = DD_OK;
3277 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3278 iface, flags, surface_desc, context, callback);
3280 if (!callback)
3281 return DDERR_INVALIDPARAMS;
3283 if (flags & DDENUMSURFACES_CANBECREATED)
3285 IDirectDrawSurface7 *surface;
3287 if ((flags & (DDENUMSURFACES_ALL | DDENUMSURFACES_MATCH | DDENUMSURFACES_NOMATCH)) != DDENUMSURFACES_MATCH)
3288 return DDERR_INVALIDPARAMS;
3290 wined3d_mutex_lock();
3292 if (surface_desc->dwFlags & (DDSD_WIDTH | DDSD_HEIGHT))
3294 if (SUCCEEDED(ddraw7_CreateSurface(iface, surface_desc, &surface, NULL)))
3296 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3297 callback(NULL, &surface_impl->surface_desc, context);
3298 IDirectDrawSurface7_Release(surface);
3301 else
3303 DDSURFACEDESC2 desc =
3305 .dwSize = sizeof(desc),
3306 .dwFlags = DDSD_PIXELFORMAT,
3307 .u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT),
3309 struct enum_surface_mode_params params =
3311 .ddraw = iface,
3312 .desc = surface_desc,
3313 .callback = callback,
3314 .context = context,
3316 struct wined3d_display_mode mode;
3318 if (FAILED(hr = wined3d_output_get_display_mode(ddraw->wined3d_output, &mode, NULL)))
3320 ERR("Failed to get display mode, hr %#x.\n", hr);
3321 wined3d_mutex_unlock();
3322 return hr_ddraw_from_wined3d(hr);
3325 ddrawformat_from_wined3dformat(&desc.u4.ddpfPixelFormat, mode.format_id);
3326 hr = ddraw7_EnumDisplayModes(iface, 0, &desc, &params, enum_surface_mode_callback);
3329 wined3d_mutex_unlock();
3331 else if (flags & DDENUMSURFACES_DOESEXIST)
3333 struct ddraw_surface *surface, *cursor;
3334 BOOL nomatch = !!(flags & DDENUMSURFACES_NOMATCH);
3336 wined3d_mutex_lock();
3338 /* Use the safe enumeration, as the callback may destroy surfaces. */
3339 LIST_FOR_EACH_ENTRY_SAFE(surface, cursor, &ddraw->surface_list, struct ddraw_surface, surface_list_entry)
3341 if (!surface->iface_count)
3343 WARN("Not enumerating surface %p because it doesn't have any references.\n", surface);
3344 continue;
3347 if ((flags & DDENUMSURFACES_ALL)
3348 || nomatch != ddraw_match_surface_desc(surface_desc, &surface->surface_desc))
3350 DDSURFACEDESC2 desc = surface->surface_desc;
3352 TRACE("Enumerating surface %p.\n", surface);
3353 IDirectDrawSurface7_AddRef(&surface->IDirectDrawSurface7_iface);
3354 if (callback(&surface->IDirectDrawSurface7_iface, &desc, context) != DDENUMRET_OK)
3355 break;
3359 wined3d_mutex_unlock();
3361 else
3363 return DDERR_INVALIDPARAMS;
3366 return hr;
3369 static HRESULT WINAPI ddraw4_EnumSurfaces(IDirectDraw4 *iface, DWORD flags,
3370 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMSURFACESCALLBACK2 callback)
3372 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3373 struct surfacescallback2_context cbcontext;
3375 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3376 iface, flags, surface_desc, context, callback);
3378 cbcontext.func = callback;
3379 cbcontext.context = context;
3381 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags, surface_desc,
3382 &cbcontext, EnumSurfacesCallback2Thunk);
3385 static HRESULT WINAPI ddraw2_EnumSurfaces(IDirectDraw2 *iface, DWORD flags,
3386 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3388 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3389 struct surfacescallback_context cbcontext;
3390 DDSURFACEDESC2 surface_desc2;
3392 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3393 iface, flags, surface_desc, context, callback);
3395 cbcontext.func = callback;
3396 cbcontext.context = context;
3398 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3399 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags,
3400 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3403 static HRESULT WINAPI ddraw1_EnumSurfaces(IDirectDraw *iface, DWORD flags,
3404 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3406 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3407 struct surfacescallback_context cbcontext;
3408 DDSURFACEDESC2 surface_desc2;
3410 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3411 iface, flags, surface_desc, context, callback);
3413 cbcontext.func = callback;
3414 cbcontext.context = context;
3416 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3417 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags,
3418 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3421 /*****************************************************************************
3422 * DirectDrawCreateClipper (DDRAW.@)
3424 * Creates a new IDirectDrawClipper object.
3426 * Params:
3427 * Clipper: Address to write the interface pointer to
3428 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3429 * NULL
3431 * Returns:
3432 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3433 * E_OUTOFMEMORY if allocating the object failed
3435 *****************************************************************************/
3436 HRESULT WINAPI DirectDrawCreateClipper(DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3438 struct ddraw_clipper *object;
3439 HRESULT hr;
3441 TRACE("flags %#x, clipper %p, outer_unknown %p.\n",
3442 flags, clipper, outer_unknown);
3444 if (outer_unknown)
3445 return CLASS_E_NOAGGREGATION;
3447 wined3d_mutex_lock();
3449 if (!(object = heap_alloc_zero(sizeof(*object))))
3451 wined3d_mutex_unlock();
3452 return E_OUTOFMEMORY;
3455 hr = ddraw_clipper_init(object);
3456 if (FAILED(hr))
3458 WARN("Failed to initialize clipper, hr %#x.\n", hr);
3459 heap_free(object);
3460 wined3d_mutex_unlock();
3461 return hr;
3464 TRACE("Created clipper %p.\n", object);
3465 *clipper = &object->IDirectDrawClipper_iface;
3466 wined3d_mutex_unlock();
3468 return DD_OK;
3471 /*****************************************************************************
3472 * IDirectDraw7::CreateClipper
3474 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3476 *****************************************************************************/
3477 static HRESULT WINAPI ddraw7_CreateClipper(IDirectDraw7 *iface, DWORD Flags,
3478 IDirectDrawClipper **Clipper, IUnknown *UnkOuter)
3480 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3481 iface, Flags, Clipper, UnkOuter);
3483 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3486 static HRESULT WINAPI ddraw4_CreateClipper(IDirectDraw4 *iface, DWORD flags,
3487 IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3489 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3491 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3492 iface, flags, clipper, outer_unknown);
3494 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3497 static HRESULT WINAPI ddraw2_CreateClipper(IDirectDraw2 *iface,
3498 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3500 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3502 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3503 iface, flags, clipper, outer_unknown);
3505 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3508 static HRESULT WINAPI ddraw1_CreateClipper(IDirectDraw *iface,
3509 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3511 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3513 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3514 iface, flags, clipper, outer_unknown);
3516 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3519 /*****************************************************************************
3520 * IDirectDraw7::CreatePalette
3522 * Creates a new IDirectDrawPalette object
3524 * Params:
3525 * Flags: The flags for the new clipper
3526 * ColorTable: Color table to assign to the new clipper
3527 * Palette: Address to write the interface pointer to
3528 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3529 * NULL
3531 * Returns:
3532 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3533 * E_OUTOFMEMORY if allocating the object failed
3535 *****************************************************************************/
3536 static HRESULT WINAPI ddraw7_CreatePalette(IDirectDraw7 *iface, DWORD Flags,
3537 PALETTEENTRY *ColorTable, IDirectDrawPalette **Palette, IUnknown *pUnkOuter)
3539 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
3540 struct ddraw_palette *object;
3541 HRESULT hr;
3543 TRACE("iface %p, flags %#x, color_table %p, palette %p, outer_unknown %p.\n",
3544 iface, Flags, ColorTable, Palette, pUnkOuter);
3546 if (pUnkOuter)
3547 return CLASS_E_NOAGGREGATION;
3549 wined3d_mutex_lock();
3551 /* The refcount test shows that a cooplevel is required for this */
3552 if (!ddraw->cooperative_level)
3554 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3555 wined3d_mutex_unlock();
3556 return DDERR_NOCOOPERATIVELEVELSET;
3559 if (!(object = heap_alloc(sizeof(*object))))
3561 ERR("Out of memory when allocating memory for a palette implementation\n");
3562 wined3d_mutex_unlock();
3563 return E_OUTOFMEMORY;
3566 hr = ddraw_palette_init(object, ddraw, Flags, ColorTable);
3567 if (FAILED(hr))
3569 WARN("Failed to initialize palette, hr %#x.\n", hr);
3570 heap_free(object);
3571 wined3d_mutex_unlock();
3572 return hr;
3575 TRACE("Created palette %p.\n", object);
3576 *Palette = &object->IDirectDrawPalette_iface;
3577 wined3d_mutex_unlock();
3579 return DD_OK;
3582 static HRESULT WINAPI ddraw4_CreatePalette(IDirectDraw4 *iface, DWORD flags, PALETTEENTRY *entries,
3583 IDirectDrawPalette **palette, IUnknown *outer_unknown)
3585 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3586 HRESULT hr;
3588 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3589 iface, flags, entries, palette, outer_unknown);
3591 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3592 if (SUCCEEDED(hr) && *palette)
3594 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3595 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3596 IDirectDraw4_AddRef(iface);
3597 impl->ifaceToRelease = (IUnknown *)iface;
3599 return hr;
3602 static HRESULT WINAPI ddraw2_CreatePalette(IDirectDraw2 *iface, DWORD flags,
3603 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3605 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3606 HRESULT hr;
3608 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3609 iface, flags, entries, palette, outer_unknown);
3611 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3612 if (SUCCEEDED(hr) && *palette)
3614 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3615 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3616 impl->ifaceToRelease = NULL;
3619 return hr;
3622 static HRESULT WINAPI ddraw1_CreatePalette(IDirectDraw *iface, DWORD flags,
3623 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3625 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3626 HRESULT hr;
3628 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3629 iface, flags, entries, palette, outer_unknown);
3631 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3632 if (SUCCEEDED(hr) && *palette)
3634 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3635 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3636 impl->ifaceToRelease = NULL;
3639 return hr;
3642 /*****************************************************************************
3643 * IDirectDraw7::DuplicateSurface
3645 * Duplicates a surface. The surface memory points to the same memory as
3646 * the original surface, and it's released when the last surface referencing
3647 * it is released. I guess that's beyond Wine's surface management right now
3648 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3649 * test application to implement this)
3651 * Params:
3652 * Src: Address of the source surface
3653 * Dest: Address to write the new surface pointer to
3655 * Returns:
3656 * See IDirectDraw7::CreateSurface
3658 *****************************************************************************/
3659 static HRESULT WINAPI ddraw7_DuplicateSurface(IDirectDraw7 *iface,
3660 IDirectDrawSurface7 *Src, IDirectDrawSurface7 **Dest)
3662 struct ddraw_surface *src_surface = unsafe_impl_from_IDirectDrawSurface7(Src);
3664 FIXME("iface %p, src %p, dst %p partial stub!\n", iface, Src, Dest);
3666 /* For now, simply create a new, independent surface */
3667 return IDirectDraw7_CreateSurface(iface, &src_surface->surface_desc, Dest, NULL);
3670 static HRESULT WINAPI ddraw4_DuplicateSurface(IDirectDraw4 *iface, IDirectDrawSurface4 *src,
3671 IDirectDrawSurface4 **dst)
3673 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src);
3674 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3675 struct ddraw_surface *dst_impl;
3676 IDirectDrawSurface7 *dst7;
3677 HRESULT hr;
3679 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3681 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3682 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3683 if (FAILED(hr))
3685 *dst = NULL;
3686 return hr;
3688 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3689 *dst = &dst_impl->IDirectDrawSurface4_iface;
3690 IDirectDrawSurface4_AddRef(*dst);
3691 IDirectDrawSurface7_Release(dst7);
3693 return hr;
3696 static HRESULT WINAPI ddraw2_DuplicateSurface(IDirectDraw2 *iface,
3697 IDirectDrawSurface *src, IDirectDrawSurface **dst)
3699 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3700 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3701 struct ddraw_surface *dst_impl;
3702 IDirectDrawSurface7 *dst7;
3703 HRESULT hr;
3705 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3707 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3708 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3709 if (FAILED(hr))
3710 return hr;
3711 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3712 *dst = &dst_impl->IDirectDrawSurface_iface;
3713 IDirectDrawSurface_AddRef(*dst);
3714 IDirectDrawSurface7_Release(dst7);
3716 return hr;
3719 static HRESULT WINAPI ddraw1_DuplicateSurface(IDirectDraw *iface, IDirectDrawSurface *src,
3720 IDirectDrawSurface **dst)
3722 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3723 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3724 struct ddraw_surface *dst_impl;
3725 IDirectDrawSurface7 *dst7;
3726 HRESULT hr;
3728 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3730 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3731 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3732 if (FAILED(hr))
3733 return hr;
3734 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3735 *dst = &dst_impl->IDirectDrawSurface_iface;
3736 IDirectDrawSurface_AddRef(*dst);
3737 IDirectDrawSurface7_Release(dst7);
3739 return hr;
3742 /*****************************************************************************
3743 * IDirect3D7::EnumDevices
3745 * The EnumDevices method for IDirect3D7. It enumerates all supported
3746 * D3D7 devices. Currently the T&L, HAL and RGB devices are enumerated.
3748 * Params:
3749 * callback: Function to call for each enumerated device
3750 * context: Pointer to pass back to the app
3752 * Returns:
3753 * D3D_OK, or the return value of the GetCaps call
3755 *****************************************************************************/
3756 static HRESULT WINAPI d3d7_EnumDevices(IDirect3D7 *iface, LPD3DENUMDEVICESCALLBACK7 callback, void *context)
3758 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
3759 D3DDEVICEDESC7 device_desc7;
3760 HRESULT hr;
3761 size_t i;
3763 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3765 if (!callback)
3766 return DDERR_INVALIDPARAMS;
3768 wined3d_mutex_lock();
3770 if (FAILED(hr = ddraw_get_d3dcaps(ddraw, &device_desc7)))
3772 wined3d_mutex_unlock();
3773 return hr;
3776 for (i = 0; i < ARRAY_SIZE(device_list7); i++)
3778 HRESULT ret;
3780 device_desc7.deviceGUID = *device_list7[i].device_guid;
3781 ret = callback(device_list7[i].interface_name, device_list7[i].device_name, &device_desc7, context);
3782 if (ret != DDENUMRET_OK)
3784 TRACE("Application cancelled the enumeration.\n");
3785 wined3d_mutex_unlock();
3786 return D3D_OK;
3790 TRACE("End of enumeration.\n");
3792 wined3d_mutex_unlock();
3794 return D3D_OK;
3797 /*****************************************************************************
3798 * IDirect3D3::EnumDevices
3800 * Enumerates all supported Direct3DDevice interfaces. This is the
3801 * implementation for Direct3D 1 to Direc3D 3, Version 7 has its own.
3803 * Versions 1, 2 and 3
3805 * Params:
3806 * callback: Application-provided routine to call for each enumerated device
3807 * Context: Pointer to pass to the callback
3809 * Returns:
3810 * D3D_OK on success,
3811 * The result of IDirect3DImpl_GetCaps if it failed
3813 *****************************************************************************/
3814 static HRESULT WINAPI d3d3_EnumDevices(IDirect3D3 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3816 static CHAR wined3d_description[] = "Wine D3DDevice using WineD3D and OpenGL";
3818 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3819 D3DDEVICEDESC device_desc1, hal_desc, hel_desc;
3820 D3DDEVICEDESC7 device_desc7;
3821 HRESULT hr;
3823 /* Some games (Motoracer 2 demo) have the bad idea to modify the device
3824 * name string. Let's put the string in a sufficiently sized array in
3825 * writable memory. */
3826 char device_name[50];
3827 strcpy(device_name,"Direct3D HEL");
3829 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3831 if (!callback)
3832 return DDERR_INVALIDPARAMS;
3834 wined3d_mutex_lock();
3836 if (FAILED(hr = ddraw_get_d3dcaps(ddraw, &device_desc7)))
3838 wined3d_mutex_unlock();
3839 return hr;
3841 ddraw_d3dcaps1_from_7(&device_desc1, &device_desc7);
3843 /* Do I have to enumerate the reference id? Note from old d3d7:
3844 * "It seems that enumerating the reference IID on Direct3D 1 games
3845 * (AvP / Motoracer2) breaks them". So do not enumerate this iid in V1
3847 * There's a registry key HKLM\Software\Microsoft\Direct3D\Drivers,
3848 * EnumReference which enables / disables enumerating the reference
3849 * rasterizer. It's a DWORD, 0 means disabled, 2 means enabled. The
3850 * enablerefrast.reg and disablerefrast.reg files in the DirectX 7.0 sdk
3851 * demo directory suggest this.
3853 * Some games(GTA 2) seem to use the second enumerated device, so I have
3854 * to enumerate at least 2 devices. So enumerate the reference device to
3855 * have 2 devices.
3857 * Other games (Rollcage) tell emulation and hal device apart by certain
3858 * flags. Rollcage expects D3DPTEXTURECAPS_POW2 to be set (yeah, it is a
3859 * limitation flag), and it refuses all devices that have the perspective
3860 * flag set. This way it refuses the emulation device, and HAL devices
3861 * never have POW2 unset in d3d7 on windows. */
3862 if (ddraw->d3dversion != 1)
3864 static CHAR reference_description[] = "RGB Direct3D emulation";
3866 TRACE("Enumerating WineD3D D3DDevice interface.\n");
3867 hal_desc = device_desc1;
3868 hel_desc = device_desc1;
3869 /* The rgb device has the pow2 flag set in the hel caps, but not in the hal caps. */
3870 hal_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3871 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3872 hal_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3873 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3874 /* RGB, RAMP and MMX devices have a HAL dcmColorModel of 0 */
3875 hal_desc.dcmColorModel = 0;
3876 /* RGB, RAMP and MMX devices cannot report HAL hardware flags */
3877 hal_desc.dwFlags = 0;
3879 hr = callback((GUID *)&IID_IDirect3DRGBDevice, reference_description,
3880 device_name, &hal_desc, &hel_desc, context);
3881 if (hr != D3DENUMRET_OK)
3883 TRACE("Application cancelled the enumeration.\n");
3884 wined3d_mutex_unlock();
3885 return D3D_OK;
3889 strcpy(device_name,"Direct3D HAL");
3891 TRACE("Enumerating HAL Direct3D device.\n");
3892 hal_desc = device_desc1;
3893 hel_desc = device_desc1;
3895 /* The hal device does not have the pow2 flag set in hel, but in hal. */
3896 hel_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3897 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3898 hel_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3899 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3900 /* HAL devices have a HEL dcmColorModel of 0 */
3901 hel_desc.dcmColorModel = 0;
3903 hr = callback((GUID *)&IID_IDirect3DHALDevice, wined3d_description,
3904 device_name, &hal_desc, &hel_desc, context);
3905 if (hr != D3DENUMRET_OK)
3907 TRACE("Application cancelled the enumeration.\n");
3908 wined3d_mutex_unlock();
3909 return D3D_OK;
3912 TRACE("End of enumeration.\n");
3914 wined3d_mutex_unlock();
3916 return D3D_OK;
3919 static HRESULT WINAPI d3d2_EnumDevices(IDirect3D2 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3921 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3923 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3925 return d3d3_EnumDevices(&ddraw->IDirect3D3_iface, callback, context);
3928 static HRESULT WINAPI d3d1_EnumDevices(IDirect3D *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3930 struct ddraw *ddraw = impl_from_IDirect3D(iface);
3932 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3934 return d3d3_EnumDevices(&ddraw->IDirect3D3_iface, callback, context);
3937 /*****************************************************************************
3938 * IDirect3D3::CreateLight
3940 * Creates an IDirect3DLight interface. This interface is used in
3941 * Direct3D3 or earlier for lighting. In Direct3D7 it has been replaced
3942 * by the DIRECT3DLIGHT7 structure. Wine's Direct3DLight implementation
3943 * uses the IDirect3DDevice7 interface with D3D7 lights.
3945 * Versions 1, 2 and 3
3947 * Params:
3948 * light: Address to store the new interface pointer
3949 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
3950 * Must be NULL
3952 * Returns:
3953 * D3D_OK on success
3954 * DDERR_OUTOFMEMORY if memory allocation failed
3955 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
3957 *****************************************************************************/
3958 static HRESULT WINAPI d3d3_CreateLight(IDirect3D3 *iface, IDirect3DLight **light,
3959 IUnknown *outer_unknown)
3961 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3962 struct d3d_light *object;
3964 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3966 if (outer_unknown)
3967 return CLASS_E_NOAGGREGATION;
3969 if (!(object = heap_alloc_zero(sizeof(*object))))
3971 ERR("Failed to allocate light memory.\n");
3972 return DDERR_OUTOFMEMORY;
3975 d3d_light_init(object, ddraw);
3977 TRACE("Created light %p.\n", object);
3978 *light = &object->IDirect3DLight_iface;
3980 return D3D_OK;
3983 static HRESULT WINAPI d3d2_CreateLight(IDirect3D2 *iface, IDirect3DLight **light, IUnknown *outer_unknown)
3985 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3987 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3989 return d3d3_CreateLight(&ddraw->IDirect3D3_iface, light, outer_unknown);
3992 static HRESULT WINAPI d3d1_CreateLight(IDirect3D *iface, IDirect3DLight **light, IUnknown *outer_unknown)
3994 struct ddraw *ddraw = impl_from_IDirect3D(iface);
3996 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3998 return d3d3_CreateLight(&ddraw->IDirect3D3_iface, light, outer_unknown);
4001 /*****************************************************************************
4002 * IDirect3D3::CreateMaterial
4004 * Creates an IDirect3DMaterial interface. This interface is used by Direct3D3
4005 * and older versions. The IDirect3DMaterial implementation wraps its
4006 * functionality to IDirect3DDevice7::SetMaterial and friends.
4008 * Versions 1, 2 and 3
4010 * Params:
4011 * material: Address to store the new interface's pointer to
4012 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4013 * Must be NULL
4015 * Returns:
4016 * D3D_OK on success
4017 * DDERR_OUTOFMEMORY if memory allocation failed
4018 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4020 *****************************************************************************/
4021 static HRESULT WINAPI d3d3_CreateMaterial(IDirect3D3 *iface, IDirect3DMaterial3 **material,
4022 IUnknown *outer_unknown)
4024 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4025 struct d3d_material *object;
4027 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4029 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4031 object = d3d_material_create(ddraw);
4032 if (!object)
4034 ERR("Failed to allocate material memory.\n");
4035 return DDERR_OUTOFMEMORY;
4038 TRACE("Created material %p.\n", object);
4039 *material = &object->IDirect3DMaterial3_iface;
4041 return D3D_OK;
4044 static HRESULT WINAPI d3d2_CreateMaterial(IDirect3D2 *iface, IDirect3DMaterial2 **material,
4045 IUnknown *outer_unknown)
4047 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4048 struct d3d_material *object;
4050 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4052 object = d3d_material_create(ddraw);
4053 if (!object)
4055 ERR("Failed to allocate material memory.\n");
4056 return DDERR_OUTOFMEMORY;
4059 TRACE("Created material %p.\n", object);
4060 *material = &object->IDirect3DMaterial2_iface;
4062 return D3D_OK;
4065 static HRESULT WINAPI d3d1_CreateMaterial(IDirect3D *iface, IDirect3DMaterial **material,
4066 IUnknown *outer_unknown)
4068 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4069 struct d3d_material *object;
4071 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4073 object = d3d_material_create(ddraw);
4074 if (!object)
4076 ERR("Failed to allocate material memory.\n");
4077 return DDERR_OUTOFMEMORY;
4080 TRACE("Created material %p.\n", object);
4081 *material = &object->IDirect3DMaterial_iface;
4083 return D3D_OK;
4086 /*****************************************************************************
4087 * IDirect3D3::CreateViewport
4089 * Creates an IDirect3DViewport interface. This interface is used
4090 * by Direct3D and earlier versions for Viewport management. In Direct3D7
4091 * it has been replaced by a viewport structure and
4092 * IDirect3DDevice7::*Viewport. Wine's IDirect3DViewport implementation
4093 * uses the IDirect3DDevice7 methods for its functionality
4095 * Params:
4096 * Viewport: Address to store the new interface pointer
4097 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4098 * Must be NULL
4100 * Returns:
4101 * D3D_OK on success
4102 * DDERR_OUTOFMEMORY if memory allocation failed
4103 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4105 *****************************************************************************/
4106 static HRESULT WINAPI d3d3_CreateViewport(IDirect3D3 *iface, IDirect3DViewport3 **viewport,
4107 IUnknown *outer_unknown)
4109 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4110 struct d3d_viewport *object;
4112 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4114 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4116 if (!(object = heap_alloc_zero(sizeof(*object))))
4118 ERR("Failed to allocate viewport memory.\n");
4119 return DDERR_OUTOFMEMORY;
4122 d3d_viewport_init(object, ddraw);
4124 TRACE("Created viewport %p.\n", object);
4125 *viewport = &object->IDirect3DViewport3_iface;
4127 return D3D_OK;
4130 static HRESULT WINAPI d3d2_CreateViewport(IDirect3D2 *iface, IDirect3DViewport2 **viewport, IUnknown *outer_unknown)
4132 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4134 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4136 return d3d3_CreateViewport(&ddraw->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4137 outer_unknown);
4140 static HRESULT WINAPI d3d1_CreateViewport(IDirect3D *iface, IDirect3DViewport **viewport, IUnknown *outer_unknown)
4142 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4144 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4146 return d3d3_CreateViewport(&ddraw->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4147 outer_unknown);
4150 static HRESULT ddraw_find_device(struct ddraw *ddraw, const D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr,
4151 unsigned int guid_count, const GUID * const *guids, DWORD device_desc_size)
4153 struct ddraw_find_device_result_v1
4155 DWORD size;
4156 GUID guid;
4157 D3DDEVICEDESC_V1 hw_desc;
4158 D3DDEVICEDESC_V1 sw_desc;
4159 } *fdr1;
4160 struct ddraw_find_device_result_v2
4162 DWORD size;
4163 GUID guid;
4164 D3DDEVICEDESC_V2 hw_desc;
4165 D3DDEVICEDESC_V2 sw_desc;
4166 } *fdr2;
4167 D3DDEVICEDESC7 desc7;
4168 D3DDEVICEDESC desc1;
4169 unsigned int i;
4170 HRESULT hr;
4172 TRACE("ddraw %p, fds %p, fdr %p, guid_count %u, guids %p, device_desc_size %u.\n",
4173 ddraw, fds, fdr, guid_count, guids, device_desc_size);
4175 if (!fds || !fdr)
4176 return DDERR_INVALIDPARAMS;
4178 if (fds->dwSize != sizeof(*fds))
4180 WARN("Got invalid search structure size %u.\n", fds->dwSize);
4181 return DDERR_INVALIDPARAMS;
4184 if (fdr->dwSize != sizeof(*fdr) && fdr->dwSize != sizeof(*fdr2) && fdr->dwSize != sizeof(*fdr1))
4186 WARN("Got invalid result structure size %u.\n", fdr->dwSize);
4187 return DDERR_INVALIDPARAMS;
4190 if (fds->dwFlags & D3DFDS_COLORMODEL)
4191 WARN("Ignoring colour model %#x.\n", fds->dcmColorModel);
4193 if (fds->dwFlags & D3DFDS_GUID)
4195 BOOL found = FALSE;
4197 TRACE("Trying to match GUID %s.\n", debugstr_guid(&fds->guid));
4199 if ((ddraw->flags & DDRAW_NO3D) && IsEqualGUID(&fds->guid, &IID_IDirect3DHALDevice))
4201 WARN("HAL device not available without 3D support.\n");
4202 return DDERR_NOTFOUND;
4205 for (i = 0; i < guid_count; ++i)
4207 if (IsEqualGUID(guids[i], &fds->guid))
4209 found = TRUE;
4210 break;
4214 if (!found)
4216 WARN("Failed to match GUID %s.\n", debugstr_guid(&fds->guid));
4217 return DDERR_NOTFOUND;
4221 /* Get the caps */
4222 if (FAILED(hr = ddraw_get_d3dcaps(ddraw, &desc7)))
4223 return hr;
4225 /* Now return our own GUID */
4226 ddraw_d3dcaps1_from_7(&desc1, &desc7);
4227 fdr->guid = IID_D3DDEVICE_WineD3D;
4229 /* Note that "device_desc_size" doesn't necessarily have any relation to
4230 * the actual structure size. However, this matches the behaviour of
4231 * Windows since at least Windows 2000. */
4232 if (fdr->dwSize == sizeof(*fdr1))
4234 fdr1 = (struct ddraw_find_device_result_v1 *)fdr;
4235 memcpy(&fdr1->hw_desc, &desc1, sizeof(fdr1->hw_desc));
4236 fdr1->hw_desc.dwSize = device_desc_size;
4237 memcpy(&fdr1->sw_desc, &desc1, sizeof(fdr1->sw_desc));
4238 fdr1->sw_desc.dwSize = device_desc_size;
4240 else if (fdr->dwSize == sizeof(*fdr2))
4242 fdr2 = (struct ddraw_find_device_result_v2 *)fdr;
4243 memcpy(&fdr2->hw_desc, &desc1, sizeof(fdr2->hw_desc));
4244 fdr2->hw_desc.dwSize = device_desc_size;
4245 memcpy(&fdr2->sw_desc, &desc1, sizeof(fdr2->sw_desc));
4246 fdr2->sw_desc.dwSize = device_desc_size;
4248 else
4250 fdr->ddHwDesc = desc1;
4251 fdr->ddHwDesc.dwSize = device_desc_size;
4252 fdr->ddSwDesc = desc1;
4253 fdr->ddSwDesc.dwSize = device_desc_size;
4256 TRACE("Returning Wine's wined3d device with (undumped) capabilities.\n");
4258 return D3D_OK;
4261 static HRESULT WINAPI d3d3_FindDevice(IDirect3D3 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4263 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4264 static const GUID * const guids[] =
4266 &IID_D3DDEVICE_WineD3D,
4267 &IID_IDirect3DHALDevice,
4268 &IID_IDirect3DRGBDevice,
4271 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4273 return ddraw_find_device(ddraw, fds, fdr, ARRAY_SIZE(guids), guids, sizeof(D3DDEVICEDESC_V3));
4276 static HRESULT WINAPI d3d2_FindDevice(IDirect3D2 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4278 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4279 static const GUID * const guids[] =
4281 &IID_D3DDEVICE_WineD3D,
4282 &IID_IDirect3DHALDevice,
4283 &IID_IDirect3DMMXDevice,
4284 &IID_IDirect3DRGBDevice,
4285 &IID_IDirect3DRampDevice,
4288 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4290 return ddraw_find_device(ddraw, fds, fdr, ARRAY_SIZE(guids), guids, sizeof(D3DDEVICEDESC_V2));
4293 static HRESULT WINAPI d3d1_FindDevice(IDirect3D *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4295 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4296 static const GUID * const guids[] =
4298 &IID_D3DDEVICE_WineD3D,
4299 &IID_IDirect3DHALDevice,
4300 &IID_IDirect3DRGBDevice,
4301 &IID_IDirect3DRampDevice,
4304 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4306 return ddraw_find_device(ddraw, fds, fdr, ARRAY_SIZE(guids), guids, sizeof(D3DDEVICEDESC_V1));
4309 /*****************************************************************************
4310 * IDirect3D7::CreateDevice
4312 * Creates an IDirect3DDevice7 interface.
4314 * Versions 2, 3 and 7. IDirect3DDevice 1 interfaces are interfaces to
4315 * DirectDraw surfaces and are created with
4316 * IDirectDrawSurface::QueryInterface. This method uses CreateDevice to
4317 * create the device object and QueryInterfaces for IDirect3DDevice
4319 * Params:
4320 * refiid: IID of the device to create
4321 * Surface: Initial rendertarget
4322 * Device: Address to return the interface pointer
4324 * Returns:
4325 * D3D_OK on success
4326 * DDERR_OUTOFMEMORY if memory allocation failed
4327 * DDERR_INVALIDPARAMS if a device exists already
4329 *****************************************************************************/
4330 static HRESULT WINAPI d3d7_CreateDevice(IDirect3D7 *iface, REFCLSID riid,
4331 IDirectDrawSurface7 *surface, IDirect3DDevice7 **device)
4333 struct ddraw_surface *target = unsafe_impl_from_IDirectDrawSurface7(surface);
4334 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4335 struct d3d_device *object;
4336 HRESULT hr;
4338 TRACE("iface %p, riid %s, surface %p, device %p.\n", iface, debugstr_guid(riid), surface, device);
4340 wined3d_mutex_lock();
4341 if (SUCCEEDED(hr = d3d_device_create(ddraw, riid, target, (IUnknown *)surface, 7, &object, NULL)))
4343 *device = &object->IDirect3DDevice7_iface;
4345 else
4347 WARN("Failed to create device, hr %#x.\n", hr);
4348 *device = NULL;
4350 wined3d_mutex_unlock();
4352 return hr;
4355 static HRESULT WINAPI d3d3_CreateDevice(IDirect3D3 *iface, REFCLSID riid,
4356 IDirectDrawSurface4 *surface, IDirect3DDevice3 **device, IUnknown *outer_unknown)
4358 struct ddraw_surface *surface_impl = unsafe_impl_from_IDirectDrawSurface4(surface);
4359 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4360 struct d3d_device *device_impl;
4361 HRESULT hr;
4363 TRACE("iface %p, riid %s, surface %p, device %p, outer_unknown %p.\n",
4364 iface, debugstr_guid(riid), surface, device, outer_unknown);
4366 if (outer_unknown)
4367 return CLASS_E_NOAGGREGATION;
4369 wined3d_mutex_lock();
4370 if (SUCCEEDED(hr = d3d_device_create(ddraw, riid, surface_impl, (IUnknown *)surface, 3, &device_impl, NULL)))
4372 *device = &device_impl->IDirect3DDevice3_iface;
4374 else
4376 WARN("Failed to create device, hr %#x.\n", hr);
4377 *device = NULL;
4379 wined3d_mutex_unlock();
4381 return hr;
4384 static HRESULT WINAPI d3d2_CreateDevice(IDirect3D2 *iface, REFCLSID riid,
4385 IDirectDrawSurface *surface, IDirect3DDevice2 **device)
4387 struct ddraw_surface *surface_impl = unsafe_impl_from_IDirectDrawSurface(surface);
4388 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4389 struct d3d_device *device_impl;
4390 HRESULT hr;
4392 TRACE("iface %p, riid %s, surface %p, device %p.\n",
4393 iface, debugstr_guid(riid), surface, device);
4395 wined3d_mutex_lock();
4396 if (SUCCEEDED(hr = d3d_device_create(ddraw, riid, surface_impl, (IUnknown *)surface, 2, &device_impl, NULL)))
4398 *device = &device_impl->IDirect3DDevice2_iface;
4400 else
4402 WARN("Failed to create device, hr %#x.\n", hr);
4403 *device = NULL;
4405 wined3d_mutex_unlock();
4407 return hr;
4410 /*****************************************************************************
4411 * IDirect3D7::CreateVertexBuffer
4413 * Creates a new vertex buffer object and returns a IDirect3DVertexBuffer7
4414 * interface.
4416 * Versions 3 and 7
4418 * Params:
4419 * desc: Requested Vertex buffer properties
4420 * vertex_buffer: Address to return the interface pointer at
4421 * flags: Some flags, should be 0
4423 * Returns
4424 * D3D_OK on success
4425 * DDERR_OUTOFMEMORY if memory allocation failed
4426 * DDERR_INVALIDPARAMS if desc or vertex_buffer is NULL
4428 *****************************************************************************/
4429 static HRESULT WINAPI d3d7_CreateVertexBuffer(IDirect3D7 *iface, D3DVERTEXBUFFERDESC *desc,
4430 IDirect3DVertexBuffer7 **vertex_buffer, DWORD flags)
4432 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4433 struct d3d_vertex_buffer *object;
4434 HRESULT hr;
4436 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x.\n",
4437 iface, desc, vertex_buffer, flags);
4439 if (!vertex_buffer || !desc) return DDERR_INVALIDPARAMS;
4441 hr = d3d_vertex_buffer_create(&object, ddraw, desc);
4442 if (hr == D3D_OK)
4444 TRACE("Created vertex buffer %p.\n", object);
4445 *vertex_buffer = &object->IDirect3DVertexBuffer7_iface;
4447 else
4448 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4450 return hr;
4453 static HRESULT WINAPI d3d3_CreateVertexBuffer(IDirect3D3 *iface, D3DVERTEXBUFFERDESC *desc,
4454 IDirect3DVertexBuffer **vertex_buffer, DWORD flags, IUnknown *outer_unknown)
4456 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4457 struct d3d_vertex_buffer *object;
4458 HRESULT hr;
4460 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x, outer_unknown %p.\n",
4461 iface, desc, vertex_buffer, flags, outer_unknown);
4463 if (outer_unknown)
4464 return CLASS_E_NOAGGREGATION;
4465 if (!vertex_buffer || !desc)
4466 return DDERR_INVALIDPARAMS;
4468 hr = d3d_vertex_buffer_create(&object, ddraw, desc);
4469 if (hr == D3D_OK)
4471 TRACE("Created vertex buffer %p.\n", object);
4472 *vertex_buffer = (IDirect3DVertexBuffer *)&object->IDirect3DVertexBuffer7_iface;
4474 else
4475 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4477 return hr;
4480 /*****************************************************************************
4481 * IDirect3D7::EnumZBufferFormats
4483 * Enumerates all supported Z buffer pixel formats
4485 * Versions 3 and 7
4487 * Params:
4488 * device_iid:
4489 * callback: callback to call for each pixel format
4490 * context: Pointer to pass back to the callback
4492 * Returns:
4493 * D3D_OK on success
4494 * DDERR_INVALIDPARAMS if callback is NULL
4496 *****************************************************************************/
4497 static HRESULT WINAPI d3d7_EnumZBufferFormats(IDirect3D7 *iface, REFCLSID device_iid,
4498 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4500 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4501 struct wined3d_display_mode mode;
4502 enum wined3d_device_type type;
4503 unsigned int i;
4504 HRESULT hr;
4506 /* Order matters. Specifically, BattleZone II (full version) expects the
4507 * 16-bit depth formats to be listed before the 24 and 32 ones. */
4508 static const enum wined3d_format_id formats[] =
4510 WINED3DFMT_S1_UINT_D15_UNORM,
4511 WINED3DFMT_D16_UNORM,
4512 WINED3DFMT_X8D24_UNORM,
4513 WINED3DFMT_S4X4_UINT_D24_UNORM,
4514 WINED3DFMT_D24_UNORM_S8_UINT,
4515 WINED3DFMT_D32_UNORM,
4518 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4519 iface, debugstr_guid(device_iid), callback, context);
4521 if (!callback) return DDERR_INVALIDPARAMS;
4523 if (IsEqualGUID(device_iid, &IID_IDirect3DHALDevice)
4524 || IsEqualGUID(device_iid, &IID_IDirect3DTnLHalDevice)
4525 || IsEqualGUID(device_iid, &IID_D3DDEVICE_WineD3D))
4527 TRACE("Asked for HAL device.\n");
4528 type = WINED3D_DEVICE_TYPE_HAL;
4530 else if (IsEqualGUID(device_iid, &IID_IDirect3DRGBDevice)
4531 || IsEqualGUID(device_iid, &IID_IDirect3DMMXDevice))
4533 TRACE("Asked for SW device.\n");
4534 type = WINED3D_DEVICE_TYPE_SW;
4536 else if (IsEqualGUID(device_iid, &IID_IDirect3DRefDevice))
4538 TRACE("Asked for REF device.\n");
4539 type = WINED3D_DEVICE_TYPE_REF;
4541 else if (IsEqualGUID(device_iid, &IID_IDirect3DNullDevice))
4543 TRACE("Asked for NULLREF device.\n");
4544 type = WINED3D_DEVICE_TYPE_NULLREF;
4546 else
4548 FIXME("Unexpected device GUID %s.\n", debugstr_guid(device_iid));
4549 type = WINED3D_DEVICE_TYPE_HAL;
4552 wined3d_mutex_lock();
4553 /* We need an adapter format from somewhere to please wined3d and WGL.
4554 * Use the current display mode. So far all cards offer the same depth
4555 * stencil format for all modes, but if some do not and applications do
4556 * not like that we'll have to find some workaround, like iterating over
4557 * all imaginable formats and collecting all the depth stencil formats we
4558 * can get. */
4559 if (FAILED(hr = wined3d_output_get_display_mode(ddraw->wined3d_output, &mode, NULL)))
4561 ERR("Failed to get display mode, hr %#x.\n", hr);
4562 wined3d_mutex_unlock();
4563 return hr;
4566 for (i = 0; i < ARRAY_SIZE(formats); ++i)
4568 if (SUCCEEDED(wined3d_check_device_format(ddraw->wined3d, ddraw->wined3d_adapter, type,
4569 mode.format_id, 0, WINED3D_BIND_DEPTH_STENCIL, WINED3D_RTYPE_TEXTURE_2D, formats[i])))
4571 DDPIXELFORMAT pformat;
4573 memset(&pformat, 0, sizeof(pformat));
4574 pformat.dwSize = sizeof(pformat);
4575 ddrawformat_from_wined3dformat(&pformat, formats[i]);
4577 TRACE("Enumerating wined3d format %#x.\n", formats[i]);
4578 hr = callback(&pformat, context);
4579 if (hr != DDENUMRET_OK)
4581 TRACE("Format enumeration cancelled by application.\n");
4582 wined3d_mutex_unlock();
4583 return D3D_OK;
4588 /* Historically some windows drivers used dwZBufferBitDepth=24 for WINED3DFMT_X8D24_UNORM,
4589 * while others used dwZBufferBitDepth=32. In either case the pitch matches a 32 bits per
4590 * pixel format, so we use dwZBufferBitDepth=32. Some games expect 24. Windows Vista and
4591 * newer enumerate both versions, so we do the same(bug 22434) */
4592 if (SUCCEEDED(wined3d_check_device_format(ddraw->wined3d, ddraw->wined3d_adapter, type, mode.format_id,
4593 0, WINED3D_BIND_DEPTH_STENCIL, WINED3D_RTYPE_TEXTURE_2D, WINED3DFMT_X8D24_UNORM)))
4595 DDPIXELFORMAT x8d24 =
4597 sizeof(x8d24), DDPF_ZBUFFER, 0,
4598 {24}, {0x00000000}, {0x00ffffff}, {0x00000000}
4600 TRACE("Enumerating WINED3DFMT_X8D24_UNORM, dwZBufferBitDepth=24 version\n");
4601 callback(&x8d24, context);
4604 TRACE("End of enumeration.\n");
4606 wined3d_mutex_unlock();
4608 return D3D_OK;
4611 static HRESULT WINAPI d3d3_EnumZBufferFormats(IDirect3D3 *iface, REFCLSID device_iid,
4612 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4614 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4616 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4617 iface, debugstr_guid(device_iid), callback, context);
4619 return d3d7_EnumZBufferFormats(&ddraw->IDirect3D7_iface, device_iid, callback, context);
4622 /*****************************************************************************
4623 * IDirect3D7::EvictManagedTextures
4625 * Removes all managed textures (=surfaces with DDSCAPS2_TEXTUREMANAGE or
4626 * DDSCAPS2_D3DTEXTUREMANAGE caps) to be removed from video memory.
4628 * Versions 3 and 7
4630 * Returns:
4631 * D3D_OK, because it's a stub
4633 *****************************************************************************/
4634 static HRESULT WINAPI d3d7_EvictManagedTextures(IDirect3D7 *iface)
4636 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4638 TRACE("iface %p!\n", iface);
4640 wined3d_mutex_lock();
4641 if (ddraw->flags & DDRAW_D3D_INITIALIZED)
4642 wined3d_device_evict_managed_resources(ddraw->wined3d_device);
4643 wined3d_mutex_unlock();
4645 return D3D_OK;
4648 static HRESULT WINAPI d3d3_EvictManagedTextures(IDirect3D3 *iface)
4650 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4652 TRACE("iface %p.\n", iface);
4654 return d3d7_EvictManagedTextures(&ddraw->IDirect3D7_iface);
4657 /*****************************************************************************
4658 * IDirectDraw7 VTable
4659 *****************************************************************************/
4660 static const struct IDirectDraw7Vtbl ddraw7_vtbl =
4662 /* IUnknown */
4663 ddraw7_QueryInterface,
4664 ddraw7_AddRef,
4665 ddraw7_Release,
4666 /* IDirectDraw */
4667 ddraw7_Compact,
4668 ddraw7_CreateClipper,
4669 ddraw7_CreatePalette,
4670 ddraw7_CreateSurface,
4671 ddraw7_DuplicateSurface,
4672 ddraw7_EnumDisplayModes,
4673 ddraw7_EnumSurfaces,
4674 ddraw7_FlipToGDISurface,
4675 ddraw7_GetCaps,
4676 ddraw7_GetDisplayMode,
4677 ddraw7_GetFourCCCodes,
4678 ddraw7_GetGDISurface,
4679 ddraw7_GetMonitorFrequency,
4680 ddraw7_GetScanLine,
4681 ddraw7_GetVerticalBlankStatus,
4682 ddraw7_Initialize,
4683 ddraw7_RestoreDisplayMode,
4684 ddraw7_SetCooperativeLevel,
4685 ddraw7_SetDisplayMode,
4686 ddraw7_WaitForVerticalBlank,
4687 /* IDirectDraw2 */
4688 ddraw7_GetAvailableVidMem,
4689 /* IDirectDraw3 */
4690 ddraw7_GetSurfaceFromDC,
4691 /* IDirectDraw4 */
4692 ddraw7_RestoreAllSurfaces,
4693 ddraw7_TestCooperativeLevel,
4694 ddraw7_GetDeviceIdentifier,
4695 /* IDirectDraw7 */
4696 ddraw7_StartModeTest,
4697 ddraw7_EvaluateMode
4700 static const struct IDirectDraw4Vtbl ddraw4_vtbl =
4702 /* IUnknown */
4703 ddraw4_QueryInterface,
4704 ddraw4_AddRef,
4705 ddraw4_Release,
4706 /* IDirectDraw */
4707 ddraw4_Compact,
4708 ddraw4_CreateClipper,
4709 ddraw4_CreatePalette,
4710 ddraw4_CreateSurface,
4711 ddraw4_DuplicateSurface,
4712 ddraw4_EnumDisplayModes,
4713 ddraw4_EnumSurfaces,
4714 ddraw4_FlipToGDISurface,
4715 ddraw4_GetCaps,
4716 ddraw4_GetDisplayMode,
4717 ddraw4_GetFourCCCodes,
4718 ddraw4_GetGDISurface,
4719 ddraw4_GetMonitorFrequency,
4720 ddraw4_GetScanLine,
4721 ddraw4_GetVerticalBlankStatus,
4722 ddraw4_Initialize,
4723 ddraw4_RestoreDisplayMode,
4724 ddraw4_SetCooperativeLevel,
4725 ddraw4_SetDisplayMode,
4726 ddraw4_WaitForVerticalBlank,
4727 /* IDirectDraw2 */
4728 ddraw4_GetAvailableVidMem,
4729 /* IDirectDraw3 */
4730 ddraw4_GetSurfaceFromDC,
4731 /* IDirectDraw4 */
4732 ddraw4_RestoreAllSurfaces,
4733 ddraw4_TestCooperativeLevel,
4734 ddraw4_GetDeviceIdentifier,
4737 static const struct IDirectDraw2Vtbl ddraw2_vtbl =
4739 /* IUnknown */
4740 ddraw2_QueryInterface,
4741 ddraw2_AddRef,
4742 ddraw2_Release,
4743 /* IDirectDraw */
4744 ddraw2_Compact,
4745 ddraw2_CreateClipper,
4746 ddraw2_CreatePalette,
4747 ddraw2_CreateSurface,
4748 ddraw2_DuplicateSurface,
4749 ddraw2_EnumDisplayModes,
4750 ddraw2_EnumSurfaces,
4751 ddraw2_FlipToGDISurface,
4752 ddraw2_GetCaps,
4753 ddraw2_GetDisplayMode,
4754 ddraw2_GetFourCCCodes,
4755 ddraw2_GetGDISurface,
4756 ddraw2_GetMonitorFrequency,
4757 ddraw2_GetScanLine,
4758 ddraw2_GetVerticalBlankStatus,
4759 ddraw2_Initialize,
4760 ddraw2_RestoreDisplayMode,
4761 ddraw2_SetCooperativeLevel,
4762 ddraw2_SetDisplayMode,
4763 ddraw2_WaitForVerticalBlank,
4764 /* IDirectDraw2 */
4765 ddraw2_GetAvailableVidMem,
4768 /* Bad Mojo Redux expects this vtbl to be writable. */
4769 static struct IDirectDrawVtbl ddraw1_vtbl =
4771 /* IUnknown */
4772 ddraw1_QueryInterface,
4773 ddraw1_AddRef,
4774 ddraw1_Release,
4775 /* IDirectDraw */
4776 ddraw1_Compact,
4777 ddraw1_CreateClipper,
4778 ddraw1_CreatePalette,
4779 ddraw1_CreateSurface,
4780 ddraw1_DuplicateSurface,
4781 ddraw1_EnumDisplayModes,
4782 ddraw1_EnumSurfaces,
4783 ddraw1_FlipToGDISurface,
4784 ddraw1_GetCaps,
4785 ddraw1_GetDisplayMode,
4786 ddraw1_GetFourCCCodes,
4787 ddraw1_GetGDISurface,
4788 ddraw1_GetMonitorFrequency,
4789 ddraw1_GetScanLine,
4790 ddraw1_GetVerticalBlankStatus,
4791 ddraw1_Initialize,
4792 ddraw1_RestoreDisplayMode,
4793 ddraw1_SetCooperativeLevel,
4794 ddraw1_SetDisplayMode,
4795 ddraw1_WaitForVerticalBlank,
4798 static const struct IDirect3D7Vtbl d3d7_vtbl =
4800 /* IUnknown methods */
4801 d3d7_QueryInterface,
4802 d3d7_AddRef,
4803 d3d7_Release,
4804 /* IDirect3D7 methods */
4805 d3d7_EnumDevices,
4806 d3d7_CreateDevice,
4807 d3d7_CreateVertexBuffer,
4808 d3d7_EnumZBufferFormats,
4809 d3d7_EvictManagedTextures
4812 static const struct IDirect3D3Vtbl d3d3_vtbl =
4814 /* IUnknown methods */
4815 d3d3_QueryInterface,
4816 d3d3_AddRef,
4817 d3d3_Release,
4818 /* IDirect3D3 methods */
4819 d3d3_EnumDevices,
4820 d3d3_CreateLight,
4821 d3d3_CreateMaterial,
4822 d3d3_CreateViewport,
4823 d3d3_FindDevice,
4824 d3d3_CreateDevice,
4825 d3d3_CreateVertexBuffer,
4826 d3d3_EnumZBufferFormats,
4827 d3d3_EvictManagedTextures
4830 static const struct IDirect3D2Vtbl d3d2_vtbl =
4832 /* IUnknown methods */
4833 d3d2_QueryInterface,
4834 d3d2_AddRef,
4835 d3d2_Release,
4836 /* IDirect3D2 methods */
4837 d3d2_EnumDevices,
4838 d3d2_CreateLight,
4839 d3d2_CreateMaterial,
4840 d3d2_CreateViewport,
4841 d3d2_FindDevice,
4842 d3d2_CreateDevice
4845 static const struct IDirect3DVtbl d3d1_vtbl =
4847 /* IUnknown methods */
4848 d3d1_QueryInterface,
4849 d3d1_AddRef,
4850 d3d1_Release,
4851 /* IDirect3D methods */
4852 d3d1_Initialize,
4853 d3d1_EnumDevices,
4854 d3d1_CreateLight,
4855 d3d1_CreateMaterial,
4856 d3d1_CreateViewport,
4857 d3d1_FindDevice
4860 /*****************************************************************************
4861 * ddraw_find_decl
4863 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
4864 * if none was found.
4866 * This function is in ddraw.c and the DDraw object space because D3D7
4867 * vertex buffers are created using the IDirect3D interface to the ddraw
4868 * object, so they can be valid across D3D devices(theoretically. The ddraw
4869 * object also owns the wined3d device
4871 * Parameters:
4872 * This: Device
4873 * fvf: Fvf to find the decl for
4875 * Returns:
4876 * NULL in case of an error, the vertex declaration for the FVF otherwise.
4878 *****************************************************************************/
4879 struct wined3d_vertex_declaration *ddraw_find_decl(struct ddraw *This, DWORD fvf)
4881 struct wined3d_vertex_declaration *pDecl = NULL;
4882 HRESULT hr;
4883 int p, low, high; /* deliberately signed */
4884 struct FvfToDecl *convertedDecls = This->decls;
4886 TRACE("Searching for declaration for fvf %08x... ", fvf);
4888 low = 0;
4889 high = This->numConvertedDecls - 1;
4890 while(low <= high) {
4891 p = (low + high) >> 1;
4892 TRACE("%d ", p);
4893 if(convertedDecls[p].fvf == fvf) {
4894 TRACE("found %p\n", convertedDecls[p].decl);
4895 return convertedDecls[p].decl;
4896 } else if(convertedDecls[p].fvf < fvf) {
4897 low = p + 1;
4898 } else {
4899 high = p - 1;
4902 TRACE("not found. Creating and inserting at position %d.\n", low);
4904 hr = wined3d_vertex_declaration_create_from_fvf(This->wined3d_device,
4905 fvf, This, &ddraw_null_wined3d_parent_ops, &pDecl);
4906 if (hr != S_OK) return NULL;
4908 if (This->declArraySize == This->numConvertedDecls)
4910 unsigned int grow = max(This->declArraySize / 2, 8);
4912 if (!(convertedDecls = heap_realloc(convertedDecls,
4913 (This->numConvertedDecls + grow) * sizeof(*convertedDecls))))
4915 wined3d_vertex_declaration_decref(pDecl);
4916 return NULL;
4918 This->decls = convertedDecls;
4919 This->declArraySize += grow;
4922 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
4923 convertedDecls[low].decl = pDecl;
4924 convertedDecls[low].fvf = fvf;
4925 This->numConvertedDecls++;
4927 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
4928 return pDecl;
4931 static inline struct ddraw *ddraw_from_device_parent(struct wined3d_device_parent *device_parent)
4933 return CONTAINING_RECORD(device_parent, struct ddraw, device_parent);
4936 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
4937 struct wined3d_device *device)
4939 TRACE("device_parent %p, device %p.\n", device_parent, device);
4942 /* This is run from device_process_message() in wined3d, we can't take the
4943 * wined3d mutex. */
4944 /* FIXME: We only get mode change notifications in exclusive mode, but we
4945 * should mark surfaces as lost on mode changes in DDSCL_NORMAL mode as well. */
4946 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
4948 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4949 struct wined3d_output_desc output_desc;
4950 RECT *r;
4952 TRACE("device_parent %p.\n", device_parent);
4954 if (!(ddraw->cooperative_level & DDSCL_EXCLUSIVE) || !ddraw->swapchain_window)
4956 TRACE("Nothing to resize.\n");
4957 return;
4960 if (FAILED(wined3d_output_get_desc(ddraw->wined3d_output, &output_desc)))
4962 ERR("Failed to get output description.\n");
4963 return;
4966 r = &output_desc.desktop_rect;
4967 TRACE("Resizing window %p to %s.\n", ddraw->swapchain_window, wine_dbgstr_rect(r));
4969 if (!SetWindowPos(ddraw->swapchain_window, HWND_TOP, r->left, r->top,
4970 r->right - r->left, r->bottom - r->top, SWP_SHOWWINDOW | SWP_NOACTIVATE))
4971 ERR("Failed to resize window.\n");
4973 InterlockedCompareExchange(&ddraw->device_state, DDRAW_DEVICE_STATE_NOT_RESTORED, DDRAW_DEVICE_STATE_OK);
4976 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
4978 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4980 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
4982 if (!activate)
4984 ddraw->device_state = DDRAW_DEVICE_STATE_LOST;
4985 exclusive_window = NULL;
4987 else
4989 InterlockedCompareExchange(&ddraw->device_state, DDRAW_DEVICE_STATE_NOT_RESTORED, DDRAW_DEVICE_STATE_LOST);
4993 void ddraw_update_lost_surfaces(struct ddraw *ddraw)
4995 struct ddraw_surface *surface;
4997 /* Railroad Tycoon 2 tries to restore surfaces from within a
4998 * WM_QUERYNEWPALETTE message handler and expects it to succeed. We
4999 * haven't received the WM_ACTIVATEAPP message by that point, so the
5000 * device state is still DDRAW_DEVICE_STATE_LOST, even though we are in
5001 * the foreground. */
5002 if (ddraw->device_state == DDRAW_DEVICE_STATE_LOST)
5004 HWND window = ddraw->focuswindow ? ddraw->focuswindow : ddraw->dest_window;
5006 if (window && GetForegroundWindow() == window)
5007 ddraw->device_state = DDRAW_DEVICE_STATE_NOT_RESTORED;
5010 if (ddraw->device_state != DDRAW_DEVICE_STATE_NOT_RESTORED)
5011 return;
5013 LIST_FOR_EACH_ENTRY(surface, &ddraw->surface_list, struct ddraw_surface, surface_list_entry)
5015 surface->is_lost = ddraw_surface_can_be_lost(surface);
5017 ddraw->device_state = DDRAW_DEVICE_STATE_OK;
5020 static HRESULT CDECL device_parent_texture_sub_resource_created(struct wined3d_device_parent *device_parent,
5021 enum wined3d_resource_type type, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
5022 void **parent, const struct wined3d_parent_ops **parent_ops)
5024 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
5025 struct ddraw_surface *ddraw_surface;
5027 TRACE("device_parent %p, type %#x, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
5028 device_parent, type, wined3d_texture, sub_resource_idx, parent, parent_ops);
5030 /* We have a swapchain or wined3d internal texture. */
5031 if (type != WINED3D_RTYPE_TEXTURE_2D || !wined3d_texture_get_parent(wined3d_texture)
5032 || wined3d_texture_get_parent(wined3d_texture) == ddraw)
5034 *parent = NULL;
5035 *parent_ops = &ddraw_null_wined3d_parent_ops;
5037 return DD_OK;
5040 if (!(ddraw_surface = heap_alloc_zero(sizeof(*ddraw_surface))))
5042 ERR("Failed to allocate surface memory.\n");
5043 return DDERR_OUTOFVIDEOMEMORY;
5046 ddraw_surface_init(ddraw_surface, ddraw, wined3d_texture, sub_resource_idx, parent_ops);
5047 *parent = ddraw_surface;
5049 ddraw_update_lost_surfaces(ddraw);
5050 list_add_head(&ddraw->surface_list, &ddraw_surface->surface_list_entry);
5052 TRACE("Created ddraw surface %p.\n", ddraw_surface);
5054 return DD_OK;
5057 static void STDMETHODCALLTYPE ddraw_frontbuffer_destroyed(void *parent)
5059 struct ddraw *ddraw = parent;
5060 ddraw->wined3d_frontbuffer = NULL;
5063 static const struct wined3d_parent_ops ddraw_frontbuffer_parent_ops =
5065 ddraw_frontbuffer_destroyed,
5068 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
5069 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
5070 struct wined3d_texture **texture)
5072 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
5073 const struct wined3d_parent_ops *parent_ops;
5074 HRESULT hr;
5076 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
5077 device_parent, container_parent, desc, texture_flags, texture);
5079 if (!ddraw->wined3d_frontbuffer)
5080 parent_ops = &ddraw_frontbuffer_parent_ops;
5081 else
5082 parent_ops = &ddraw_null_wined3d_parent_ops;
5084 if (FAILED(hr = wined3d_texture_create(ddraw->wined3d_device, desc, 1, 1,
5085 texture_flags, NULL, ddraw, parent_ops, texture)))
5087 WARN("Failed to create texture, hr %#x.\n", hr);
5088 return hr;
5091 if (!ddraw->wined3d_frontbuffer)
5092 ddraw->wined3d_frontbuffer = *texture;
5094 return hr;
5097 static const struct wined3d_device_parent_ops ddraw_wined3d_device_parent_ops =
5099 device_parent_wined3d_device_created,
5100 device_parent_mode_changed,
5101 device_parent_activate,
5102 device_parent_texture_sub_resource_created,
5103 device_parent_create_swapchain_texture,
5106 HRESULT ddraw_init(struct ddraw *ddraw, DWORD flags, enum wined3d_device_type device_type)
5108 struct wined3d_caps caps;
5109 HRESULT hr;
5111 static const enum wined3d_feature_level feature_levels[] =
5113 WINED3D_FEATURE_LEVEL_7,
5114 WINED3D_FEATURE_LEVEL_6,
5115 WINED3D_FEATURE_LEVEL_5,
5118 ddraw->IDirectDraw7_iface.lpVtbl = &ddraw7_vtbl;
5119 ddraw->IDirectDraw_iface.lpVtbl = &ddraw1_vtbl;
5120 ddraw->IDirectDraw2_iface.lpVtbl = &ddraw2_vtbl;
5121 ddraw->IDirectDraw4_iface.lpVtbl = &ddraw4_vtbl;
5122 ddraw->IDirect3D_iface.lpVtbl = &d3d1_vtbl;
5123 ddraw->IDirect3D2_iface.lpVtbl = &d3d2_vtbl;
5124 ddraw->IDirect3D3_iface.lpVtbl = &d3d3_vtbl;
5125 ddraw->IDirect3D7_iface.lpVtbl = &d3d7_vtbl;
5126 ddraw->device_parent.ops = &ddraw_wined3d_device_parent_ops;
5127 ddraw->state_parent.ops = &ddraw_swapchain_state_parent_ops;
5128 ddraw->numIfaces = 1;
5129 ddraw->ref7 = 1;
5131 flags |= DDRAW_WINED3D_FLAGS;
5132 if (!(ddraw->wined3d = wined3d_create(flags)))
5134 flags |= WINED3D_NO3D;
5135 if (!(ddraw->wined3d = wined3d_create(flags)))
5137 WARN("Failed to create a wined3d object.\n");
5138 return E_FAIL;
5142 if (!(ddraw->wined3d_adapter = wined3d_get_adapter(ddraw->wined3d, WINED3DADAPTER_DEFAULT)))
5144 WARN("Failed to get the default wined3d adapter.\n");
5145 wined3d_decref(ddraw->wined3d);
5146 return E_FAIL;
5149 if (!(ddraw->wined3d_output = wined3d_adapter_get_output(ddraw->wined3d_adapter, 0)))
5151 WARN("Failed to get the default wined3d output.\n");
5152 wined3d_decref(ddraw->wined3d);
5153 return E_FAIL;
5156 if (FAILED(hr = wined3d_get_device_caps(ddraw->wined3d_adapter, device_type, &caps)))
5158 ERR("Failed to get device caps, hr %#x.\n", hr);
5159 wined3d_decref(ddraw->wined3d);
5160 return E_FAIL;
5163 if (!(caps.ddraw_caps.caps & WINEDDCAPS_3D))
5165 WARN("Created a wined3d object without 3D support.\n");
5166 ddraw->flags |= DDRAW_NO3D;
5169 if (FAILED(hr = wined3d_device_create(ddraw->wined3d, ddraw->wined3d_adapter, device_type,
5170 NULL, 0, DDRAW_STRIDE_ALIGNMENT, feature_levels, ARRAY_SIZE(feature_levels),
5171 &ddraw->device_parent, &ddraw->wined3d_device)))
5173 WARN("Failed to create a wined3d device, hr %#x.\n", hr);
5174 wined3d_decref(ddraw->wined3d);
5175 return hr;
5177 ddraw->immediate_context = wined3d_device_get_immediate_context(ddraw->wined3d_device);
5179 list_init(&ddraw->surface_list);
5181 if (FAILED(hr = wined3d_stateblock_create(ddraw->wined3d_device, NULL, WINED3D_SBT_PRIMARY, &ddraw->state)))
5183 ERR("Failed to create the primary stateblock, hr %#x.\n", hr);
5184 wined3d_device_decref(ddraw->wined3d_device);
5185 wined3d_decref(ddraw->wined3d);
5186 return hr;
5188 ddraw->stateblock_state = wined3d_stateblock_get_state(ddraw->state);
5190 return DD_OK;