vbscript: Added support for title and type arguments of MsgBox.
[wine.git] / dlls / ddraw / ddraw.c
blob011c1159da930d7c4713fad9990c7a9c17d1ff6a
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 "config.h"
25 #include "wine/port.h"
27 #include "ddraw_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
31 static struct wined3d_display_mode original_mode;
32 static const struct ddraw *exclusive_ddraw;
33 static BOOL restore_mode;
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 inline struct ddraw *impl_from_IDirectDraw(IDirectDraw *iface)
85 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw_iface);
88 static inline struct ddraw *impl_from_IDirectDraw2(IDirectDraw2 *iface)
90 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw2_iface);
93 static inline struct ddraw *impl_from_IDirectDraw4(IDirectDraw4 *iface)
95 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw4_iface);
98 static inline struct ddraw *impl_from_IDirectDraw7(IDirectDraw7 *iface)
100 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw7_iface);
103 static inline struct ddraw *impl_from_IDirect3D(IDirect3D *iface)
105 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D_iface);
108 static inline struct ddraw *impl_from_IDirect3D2(IDirect3D2 *iface)
110 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D2_iface);
113 static inline struct ddraw *impl_from_IDirect3D3(IDirect3D3 *iface)
115 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D3_iface);
118 static inline struct ddraw *impl_from_IDirect3D7(IDirect3D7 *iface)
120 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D7_iface);
123 static HRESULT WINAPI ddraw7_QueryInterface(IDirectDraw7 *iface, REFIID riid, void **out)
125 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
127 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
129 if (!riid)
131 *out = NULL;
132 return DDERR_INVALIDPARAMS;
135 /* The refcount unit test revealed that an IDirect3D7 interface can only
136 * be queried from a DirectDraw object that was created as an IDirectDraw7
137 * interface. The older interfaces can query any IDirect3D version except
138 * 7, because they are all initially created as IDirectDraw. This isn't
139 * really crucial behavior, and messy to implement with the common
140 * creation function, so it has been left out here. */
141 if (IsEqualGUID(&IID_IDirectDraw7, riid)
142 || IsEqualGUID(&IID_IUnknown, riid))
144 *out = &ddraw->IDirectDraw7_iface;
145 TRACE("Returning IDirectDraw7 interface %p.\n", *out);
147 else if (IsEqualGUID(&IID_IDirectDraw4, riid))
149 *out = &ddraw->IDirectDraw4_iface;
150 TRACE("Returning IDirectDraw4 interface %p.\n", *out);
152 else if (IsEqualGUID(&IID_IDirectDraw2, riid))
154 *out = &ddraw->IDirectDraw2_iface;
155 TRACE("Returning IDirectDraw2 interface %p.\n", *out);
157 else if (IsEqualGUID(&IID_IDirectDraw, riid))
159 *out = &ddraw->IDirectDraw_iface;
160 TRACE("Returning IDirectDraw interface %p.\n", *out);
162 else if (IsEqualGUID(&IID_IDirect3D7, riid))
164 ddraw->d3dversion = 7;
165 *out = &ddraw->IDirect3D7_iface;
166 TRACE("Returning Direct3D7 interface %p.\n", *out);
168 else if (IsEqualGUID(&IID_IDirect3D3, riid))
170 ddraw->d3dversion = 3;
171 *out = &ddraw->IDirect3D3_iface;
172 TRACE("Returning Direct3D3 interface %p.\n", *out);
174 else if (IsEqualGUID(&IID_IDirect3D2, riid))
176 ddraw->d3dversion = 2;
177 *out = &ddraw->IDirect3D2_iface;
178 TRACE("Returning Direct3D2 interface %p.\n", *out);
180 else if (IsEqualGUID(&IID_IDirect3D, riid))
182 ddraw->d3dversion = 1;
183 *out = &ddraw->IDirect3D_iface;
184 TRACE("Returning Direct3D interface %p.\n", *out);
186 /* Unknown interface */
187 else
189 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
190 *out = NULL;
191 return E_NOINTERFACE;
194 IUnknown_AddRef((IUnknown *)*out);
195 return S_OK;
198 static HRESULT WINAPI ddraw4_QueryInterface(IDirectDraw4 *iface, REFIID riid, void **object)
200 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
202 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
204 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
207 static HRESULT WINAPI ddraw2_QueryInterface(IDirectDraw2 *iface, REFIID riid, void **object)
209 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
211 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
213 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
216 static HRESULT WINAPI ddraw1_QueryInterface(IDirectDraw *iface, REFIID riid, void **object)
218 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
220 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
222 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
225 static HRESULT WINAPI d3d7_QueryInterface(IDirect3D7 *iface, REFIID riid, void **object)
227 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
229 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
231 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
234 static HRESULT WINAPI d3d3_QueryInterface(IDirect3D3 *iface, REFIID riid, void **object)
236 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
238 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
240 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
243 static HRESULT WINAPI d3d2_QueryInterface(IDirect3D2 *iface, REFIID riid, void **object)
245 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
247 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
249 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
252 static HRESULT WINAPI d3d1_QueryInterface(IDirect3D *iface, REFIID riid, void **object)
254 struct ddraw *ddraw = impl_from_IDirect3D(iface);
256 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
258 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
261 /*****************************************************************************
262 * IDirectDraw7::AddRef
264 * Increases the interfaces refcount, basically
266 * DDraw refcounting is a bit tricky. The different DirectDraw interface
267 * versions have individual refcounts, but the IDirect3D interfaces do not.
268 * All interfaces are from one object, that means calling QueryInterface on an
269 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
270 * ddraw object.
272 * That means all AddRef and Release implementations of IDirectDrawX work
273 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
274 * except of IDirect3D7 which thunks to IDirectDraw7
276 * Returns: The new refcount
278 *****************************************************************************/
279 static ULONG WINAPI ddraw7_AddRef(IDirectDraw7 *iface)
281 struct ddraw *This = impl_from_IDirectDraw7(iface);
282 ULONG ref = InterlockedIncrement(&This->ref7);
284 TRACE("%p increasing refcount to %u.\n", This, ref);
286 if(ref == 1) InterlockedIncrement(&This->numIfaces);
288 return ref;
291 static ULONG WINAPI ddraw4_AddRef(IDirectDraw4 *iface)
293 struct ddraw *This = impl_from_IDirectDraw4(iface);
294 ULONG ref = InterlockedIncrement(&This->ref4);
296 TRACE("%p increasing refcount to %u.\n", This, ref);
298 if (ref == 1) InterlockedIncrement(&This->numIfaces);
300 return ref;
303 static ULONG WINAPI ddraw2_AddRef(IDirectDraw2 *iface)
305 struct ddraw *This = impl_from_IDirectDraw2(iface);
306 ULONG ref = InterlockedIncrement(&This->ref2);
308 TRACE("%p increasing refcount to %u.\n", This, ref);
310 if (ref == 1) InterlockedIncrement(&This->numIfaces);
312 return ref;
315 static ULONG WINAPI ddraw1_AddRef(IDirectDraw *iface)
317 struct ddraw *This = impl_from_IDirectDraw(iface);
318 ULONG ref = InterlockedIncrement(&This->ref1);
320 TRACE("%p increasing refcount to %u.\n", This, ref);
322 if (ref == 1) InterlockedIncrement(&This->numIfaces);
324 return ref;
327 static ULONG WINAPI d3d7_AddRef(IDirect3D7 *iface)
329 struct ddraw *This = impl_from_IDirect3D7(iface);
331 TRACE("iface %p.\n", iface);
333 return ddraw7_AddRef(&This->IDirectDraw7_iface);
336 static ULONG WINAPI d3d3_AddRef(IDirect3D3 *iface)
338 struct ddraw *This = impl_from_IDirect3D3(iface);
340 TRACE("iface %p.\n", iface);
342 return ddraw1_AddRef(&This->IDirectDraw_iface);
345 static ULONG WINAPI d3d2_AddRef(IDirect3D2 *iface)
347 struct ddraw *This = impl_from_IDirect3D2(iface);
349 TRACE("iface %p.\n", iface);
351 return ddraw1_AddRef(&This->IDirectDraw_iface);
354 static ULONG WINAPI d3d1_AddRef(IDirect3D *iface)
356 struct ddraw *This = impl_from_IDirect3D(iface);
358 TRACE("iface %p.\n", iface);
360 return ddraw1_AddRef(&This->IDirectDraw_iface);
363 void ddraw_destroy_swapchain(struct ddraw *ddraw)
365 TRACE("Destroying the swapchain.\n");
367 wined3d_swapchain_decref(ddraw->wined3d_swapchain);
368 ddraw->wined3d_swapchain = NULL;
370 if (!(ddraw->flags & DDRAW_NO3D))
372 UINT i;
374 for (i = 0; i < ddraw->numConvertedDecls; ++i)
376 wined3d_vertex_declaration_decref(ddraw->decls[i].decl);
378 HeapFree(GetProcessHeap(), 0, ddraw->decls);
379 ddraw->numConvertedDecls = 0;
381 if (FAILED(wined3d_device_uninit_3d(ddraw->wined3d_device)))
383 ERR("Failed to uninit 3D.\n");
385 else
387 /* Free the d3d window if one was created. */
388 if (ddraw->d3d_window && ddraw->d3d_window != ddraw->dest_window)
390 TRACE("Destroying the hidden render window %p.\n", ddraw->d3d_window);
391 DestroyWindow(ddraw->d3d_window);
392 ddraw->d3d_window = 0;
396 ddraw->flags &= ~DDRAW_D3D_INITIALIZED;
398 else
400 wined3d_device_uninit_gdi(ddraw->wined3d_device);
403 ddraw_set_swapchain_window(ddraw, NULL);
405 TRACE("Swapchain destroyed.\n");
408 /*****************************************************************************
409 * ddraw_destroy
411 * Destroys a ddraw object if all refcounts are 0. This is to share code
412 * between the IDirectDrawX::Release functions
414 * Params:
415 * This: DirectDraw object to destroy
417 *****************************************************************************/
418 static void ddraw_destroy(struct ddraw *This)
420 IDirectDraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, NULL, DDSCL_NORMAL);
421 IDirectDraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
423 /* Destroy the device window if we created one */
424 if(This->devicewindow != 0)
426 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
427 DestroyWindow(This->devicewindow);
428 This->devicewindow = 0;
431 wined3d_mutex_lock();
432 list_remove(&This->ddraw_list_entry);
433 wined3d_mutex_unlock();
435 if (This->wined3d_swapchain)
436 ddraw_destroy_swapchain(This);
437 wined3d_device_decref(This->wined3d_device);
438 wined3d_decref(This->wined3d);
440 /* Now free the object */
441 HeapFree(GetProcessHeap(), 0, This);
444 /*****************************************************************************
445 * IDirectDraw7::Release
447 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
449 * Returns: The new refcount
450 *****************************************************************************/
451 static ULONG WINAPI ddraw7_Release(IDirectDraw7 *iface)
453 struct ddraw *This = impl_from_IDirectDraw7(iface);
454 ULONG ref = InterlockedDecrement(&This->ref7);
456 TRACE("%p decreasing refcount to %u.\n", This, ref);
458 if (!ref && !InterlockedDecrement(&This->numIfaces))
459 ddraw_destroy(This);
461 return ref;
464 static ULONG WINAPI ddraw4_Release(IDirectDraw4 *iface)
466 struct ddraw *This = impl_from_IDirectDraw4(iface);
467 ULONG ref = InterlockedDecrement(&This->ref4);
469 TRACE("%p decreasing refcount to %u.\n", This, ref);
471 if (!ref && !InterlockedDecrement(&This->numIfaces))
472 ddraw_destroy(This);
474 return ref;
477 static ULONG WINAPI ddraw2_Release(IDirectDraw2 *iface)
479 struct ddraw *This = impl_from_IDirectDraw2(iface);
480 ULONG ref = InterlockedDecrement(&This->ref2);
482 TRACE("%p decreasing refcount to %u.\n", This, ref);
484 if (!ref && !InterlockedDecrement(&This->numIfaces))
485 ddraw_destroy(This);
487 return ref;
490 static ULONG WINAPI ddraw1_Release(IDirectDraw *iface)
492 struct ddraw *This = impl_from_IDirectDraw(iface);
493 ULONG ref = InterlockedDecrement(&This->ref1);
495 TRACE("%p decreasing refcount to %u.\n", This, ref);
497 if (!ref && !InterlockedDecrement(&This->numIfaces))
498 ddraw_destroy(This);
500 return ref;
503 static ULONG WINAPI d3d7_Release(IDirect3D7 *iface)
505 struct ddraw *This = impl_from_IDirect3D7(iface);
507 TRACE("iface %p.\n", iface);
509 return ddraw7_Release(&This->IDirectDraw7_iface);
512 static ULONG WINAPI d3d3_Release(IDirect3D3 *iface)
514 struct ddraw *This = impl_from_IDirect3D3(iface);
516 TRACE("iface %p.\n", iface);
518 return ddraw1_Release(&This->IDirectDraw_iface);
521 static ULONG WINAPI d3d2_Release(IDirect3D2 *iface)
523 struct ddraw *This = impl_from_IDirect3D2(iface);
525 TRACE("iface %p.\n", iface);
527 return ddraw1_Release(&This->IDirectDraw_iface);
530 static ULONG WINAPI d3d1_Release(IDirect3D *iface)
532 struct ddraw *This = impl_from_IDirect3D(iface);
534 TRACE("iface %p.\n", iface);
536 return ddraw1_Release(&This->IDirectDraw_iface);
539 /*****************************************************************************
540 * IDirectDraw methods
541 *****************************************************************************/
543 static HRESULT ddraw_set_focus_window(struct ddraw *ddraw, HWND window)
545 /* FIXME: This looks wrong, exclusive mode should imply a destination
546 * window. */
547 if ((ddraw->cooperative_level & DDSCL_EXCLUSIVE) && ddraw->dest_window)
549 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET.\n");
550 return DDERR_HWNDALREADYSET;
553 ddraw->focuswindow = window;
555 return DD_OK;
558 static HRESULT ddraw_attach_d3d_device(struct ddraw *ddraw,
559 struct wined3d_swapchain_desc *swapchain_desc)
561 HWND window = swapchain_desc->device_window;
562 HRESULT hr;
564 TRACE("ddraw %p.\n", ddraw);
566 if (!window || window == GetDesktopWindow())
568 window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "Hidden D3D Window",
569 WS_DISABLED, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
570 NULL, NULL, NULL, NULL);
571 if (!window)
573 ERR("Failed to create window, last error %#x.\n", GetLastError());
574 return E_FAIL;
577 ShowWindow(window, SW_HIDE); /* Just to be sure */
578 WARN("No window for the Direct3DDevice, created hidden window %p.\n", window);
580 swapchain_desc->device_window = window;
582 else
584 TRACE("Using existing window %p for Direct3D rendering.\n", window);
586 ddraw->d3d_window = window;
588 /* Set this NOW, otherwise creating the depth stencil surface will cause a
589 * recursive loop until ram or emulated video memory is full. */
590 ddraw->flags |= DDRAW_D3D_INITIALIZED;
591 hr = wined3d_device_init_3d(ddraw->wined3d_device, swapchain_desc);
592 if (FAILED(hr))
594 ddraw->flags &= ~DDRAW_D3D_INITIALIZED;
595 return hr;
598 ddraw->declArraySize = 2;
599 ddraw->decls = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw->decls) * ddraw->declArraySize);
600 if (!ddraw->decls)
602 ERR("Error allocating an array for the converted vertex decls.\n");
603 ddraw->declArraySize = 0;
604 hr = wined3d_device_uninit_3d(ddraw->wined3d_device);
605 return E_OUTOFMEMORY;
608 TRACE("Successfully initialized 3D.\n");
610 return DD_OK;
613 static HRESULT ddraw_create_swapchain(struct ddraw *ddraw, HWND window, BOOL windowed)
615 struct wined3d_swapchain_desc swapchain_desc;
616 struct wined3d_display_mode mode;
617 HRESULT hr = WINED3D_OK;
619 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
621 ERR("Failed to get display mode.\n");
622 return hr;
625 memset(&swapchain_desc, 0, sizeof(swapchain_desc));
626 swapchain_desc.backbuffer_width = mode.width;
627 swapchain_desc.backbuffer_height = mode.height;
628 swapchain_desc.backbuffer_format = mode.format_id;
629 swapchain_desc.swap_effect = WINED3D_SWAP_EFFECT_COPY;
630 swapchain_desc.device_window = window;
631 swapchain_desc.windowed = windowed;
633 if (!(ddraw->flags & DDRAW_NO3D))
634 hr = ddraw_attach_d3d_device(ddraw, &swapchain_desc);
635 else
636 hr = wined3d_device_init_gdi(ddraw->wined3d_device, &swapchain_desc);
638 if (FAILED(hr))
640 ERR("Failed to create swapchain, hr %#x.\n", hr);
641 return hr;
644 if (!(ddraw->wined3d_swapchain = wined3d_device_get_swapchain(ddraw->wined3d_device, 0)))
646 ERR("Failed to get swapchain.\n");
647 return DDERR_INVALIDPARAMS;
650 wined3d_swapchain_incref(ddraw->wined3d_swapchain);
651 ddraw_set_swapchain_window(ddraw, window);
653 return DD_OK;
656 /*****************************************************************************
657 * IDirectDraw7::RestoreDisplayMode
659 * Restores the display mode to what it was at creation time. Basically.
661 * Returns
662 * DD_OK on success
663 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
665 *****************************************************************************/
666 static HRESULT WINAPI ddraw7_RestoreDisplayMode(IDirectDraw7 *iface)
668 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
669 HRESULT hr;
671 TRACE("iface %p.\n", iface);
673 wined3d_mutex_lock();
675 if (!(ddraw->flags & DDRAW_RESTORE_MODE))
677 wined3d_mutex_unlock();
678 return DD_OK;
681 if (exclusive_ddraw && exclusive_ddraw != ddraw)
683 wined3d_mutex_unlock();
684 return DDERR_NOEXCLUSIVEMODE;
687 if (SUCCEEDED(hr = wined3d_set_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &original_mode)))
689 ddraw->flags &= ~DDRAW_RESTORE_MODE;
690 restore_mode = FALSE;
693 wined3d_mutex_unlock();
695 return hr;
698 static HRESULT WINAPI ddraw4_RestoreDisplayMode(IDirectDraw4 *iface)
700 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
702 TRACE("iface %p.\n", iface);
704 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
707 static HRESULT WINAPI ddraw2_RestoreDisplayMode(IDirectDraw2 *iface)
709 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
711 TRACE("iface %p.\n", iface);
713 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
716 static HRESULT WINAPI ddraw1_RestoreDisplayMode(IDirectDraw *iface)
718 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
720 TRACE("iface %p.\n", iface);
722 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
725 /*****************************************************************************
726 * IDirectDraw7::SetCooperativeLevel
728 * Sets the cooperative level for the DirectDraw object, and the window
729 * assigned to it. The cooperative level determines the general behavior
730 * of the DirectDraw application
732 * Warning: This is quite tricky, as it's not really documented which
733 * cooperative levels can be combined with each other. If a game fails
734 * after this function, try to check the cooperative levels passed on
735 * Windows, and if it returns something different.
737 * If you think that this function caused the failure because it writes a
738 * fixme, be sure to run again with a +ddraw trace.
740 * What is known about cooperative levels (See the ddraw modes test):
741 * DDSCL_EXCLUSIVE requires DDSCL_FULLSCREEN.
742 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE.
743 * Unlike what msdn claims, DDSCL_NORMAL | DDSCL_FULLSCREEN is allowed.
744 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
745 * DDSCL_EXCLUSIVE can be activated.
746 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES or
747 * DDSCL_CREATEDEVICEWINDOW.
749 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
750 * DDSCL_CREATEDEVICEWINDOW, DDSCL_SETDEVICEWINDOW
751 * DDSCL_SETFOCUSWINDOW (partially),
752 * DDSCL_MULTITHREADED (work in progress)
753 * DDSCL_FPUPRESERVE (see device.c)
755 * Unsure about this: DDSCL_FPUSETUP
757 * These don't seem very important for wine:
758 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
760 * Returns:
761 * DD_OK if the cooperative level was set successfully
762 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
763 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
764 * (Probably others too, have to investigate)
766 *****************************************************************************/
767 static HRESULT ddraw_set_cooperative_level(struct ddraw *ddraw, HWND window,
768 DWORD cooplevel, BOOL restore_mode_on_normal)
770 struct wined3d_surface *rt = NULL, *ds = NULL;
771 struct wined3d_stateblock *stateblock;
772 BOOL restore_state = FALSE;
773 HRESULT hr;
775 TRACE("ddraw %p, window %p, flags %#x, restore_mode_on_normal %x.\n", ddraw, window, cooplevel,
776 restore_mode_on_normal);
777 DDRAW_dump_cooperativelevel(cooplevel);
779 wined3d_mutex_lock();
781 if (ddraw->flags & DDRAW_SCL_RECURSIVE)
783 WARN("Recursive call, returning DD_OK.\n");
784 hr = DD_OK;
785 goto done;
787 ddraw->flags |= DDRAW_SCL_RECURSIVE;
789 /* Tests suggest that we need one of them: */
790 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
791 DDSCL_NORMAL |
792 DDSCL_EXCLUSIVE )))
794 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
795 hr = DDERR_INVALIDPARAMS;
796 goto done;
799 if ((cooplevel & DDSCL_CREATEDEVICEWINDOW) && !(cooplevel & DDSCL_EXCLUSIVE))
801 WARN("DDSCL_CREATEDEVICEWINDOW requires DDSCL_EXCLUSIVE.\n");
802 hr = DDERR_INVALIDPARAMS;
803 goto done;
806 /* Handle those levels first which set various hwnds */
807 if ((cooplevel & DDSCL_SETFOCUSWINDOW) && !(cooplevel & DDSCL_CREATEDEVICEWINDOW))
809 /* This isn't compatible with a lot of flags */
810 if (cooplevel & (DDSCL_MULTITHREADED
811 | DDSCL_FPUSETUP
812 | DDSCL_FPUPRESERVE
813 | DDSCL_ALLOWREBOOT
814 | DDSCL_ALLOWMODEX
815 | DDSCL_SETDEVICEWINDOW
816 | DDSCL_NORMAL
817 | DDSCL_EXCLUSIVE
818 | DDSCL_FULLSCREEN))
820 WARN("Called with incompatible flags, returning DDERR_INVALIDPARAMS.\n");
821 hr = DDERR_INVALIDPARAMS;
822 goto done;
825 hr = ddraw_set_focus_window(ddraw, window);
826 goto done;
829 if (cooplevel & DDSCL_EXCLUSIVE)
831 if (!(cooplevel & DDSCL_FULLSCREEN) || !(window || (cooplevel & DDSCL_CREATEDEVICEWINDOW)))
833 WARN("DDSCL_EXCLUSIVE requires DDSCL_FULLSCREEN and a window.\n");
834 hr = DDERR_INVALIDPARAMS;
835 goto done;
838 if (cooplevel & DDSCL_CREATEDEVICEWINDOW)
840 HWND device_window;
842 if (!ddraw->focuswindow && !(cooplevel & DDSCL_SETFOCUSWINDOW))
844 WARN("No focus window set.\n");
845 hr = DDERR_NOFOCUSWINDOW;
846 goto done;
849 device_window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "DirectDrawDeviceWnd",
850 WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
851 NULL, NULL, NULL, NULL);
852 if (!device_window)
854 ERR("Failed to create window, last error %#x.\n", GetLastError());
855 hr = E_FAIL;
856 goto done;
859 ShowWindow(device_window, SW_SHOW);
860 TRACE("Created a device window %p.\n", device_window);
862 /* Native apparently leaks the created device window if setting the
863 * focus window below fails. */
864 ddraw->cooperative_level |= DDSCL_CREATEDEVICEWINDOW;
865 ddraw->devicewindow = device_window;
867 if (cooplevel & DDSCL_SETFOCUSWINDOW)
869 if (!window)
871 hr = DDERR_NOHWND;
872 goto done;
875 if (FAILED(hr = ddraw_set_focus_window(ddraw, window)))
876 goto done;
879 window = device_window;
882 else
884 if (ddraw->cooperative_level & DDSCL_CREATEDEVICEWINDOW)
885 DestroyWindow(ddraw->devicewindow);
886 ddraw->devicewindow = NULL;
887 ddraw->focuswindow = NULL;
890 if ((cooplevel & DDSCL_FULLSCREEN) != (ddraw->cooperative_level & DDSCL_FULLSCREEN) || window != ddraw->dest_window)
892 if (ddraw->cooperative_level & DDSCL_FULLSCREEN)
893 wined3d_device_restore_fullscreen_window(ddraw->wined3d_device, ddraw->dest_window);
895 if (cooplevel & DDSCL_FULLSCREEN)
897 struct wined3d_display_mode display_mode;
899 wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &display_mode, NULL);
900 wined3d_device_setup_fullscreen_window(ddraw->wined3d_device, window,
901 display_mode.width, display_mode.height);
905 if (cooplevel & DDSCL_MULTITHREADED && !(ddraw->cooperative_level & DDSCL_MULTITHREADED))
906 wined3d_device_set_multithreaded(ddraw->wined3d_device);
908 if (ddraw->wined3d_swapchain)
910 if (!(ddraw->flags & DDRAW_NO3D))
912 restore_state = TRUE;
914 if (FAILED(hr = wined3d_stateblock_create(ddraw->wined3d_device, WINED3D_SBT_ALL, &stateblock)))
916 ERR("Failed to create stateblock, hr %#x.\n", hr);
917 goto done;
920 wined3d_stateblock_capture(stateblock);
921 rt = wined3d_device_get_render_target(ddraw->wined3d_device, 0);
922 if (rt == ddraw->wined3d_frontbuffer)
923 rt = NULL;
924 else if (rt)
925 wined3d_surface_incref(rt);
927 if ((ds = wined3d_device_get_depth_stencil(ddraw->wined3d_device)))
928 wined3d_surface_incref(ds);
931 ddraw_destroy_swapchain(ddraw);
934 if (FAILED(hr = ddraw_create_swapchain(ddraw, window, !(cooplevel & DDSCL_FULLSCREEN))))
935 ERR("Failed to create swapchain, hr %#x.\n", hr);
937 if (restore_state)
939 if (ds)
941 wined3d_device_set_depth_stencil(ddraw->wined3d_device, ds);
942 wined3d_surface_decref(ds);
945 if (rt)
947 wined3d_device_set_render_target(ddraw->wined3d_device, 0, rt, FALSE);
948 wined3d_surface_decref(rt);
951 wined3d_stateblock_apply(stateblock);
952 wined3d_stateblock_decref(stateblock);
955 if (!(cooplevel & DDSCL_EXCLUSIVE) && (ddraw->cooperative_level & DDSCL_EXCLUSIVE)
956 && restore_mode_on_normal)
958 hr = ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
959 if (FAILED(hr))
960 ERR("RestoreDisplayMode failed\n");
963 if ((ddraw->cooperative_level & DDSCL_EXCLUSIVE)
964 && (window != ddraw->dest_window || !(cooplevel & DDSCL_EXCLUSIVE)))
965 wined3d_device_release_focus_window(ddraw->wined3d_device);
967 if ((cooplevel & DDSCL_EXCLUSIVE)
968 && (window != ddraw->dest_window || !(ddraw->cooperative_level & DDSCL_EXCLUSIVE)))
970 hr = wined3d_device_acquire_focus_window(ddraw->wined3d_device, window);
971 if (FAILED(hr))
973 ERR("Failed to acquire focus window, hr %#x.\n", hr);
974 goto done;
978 /* Unhandled flags */
979 if (cooplevel & DDSCL_ALLOWREBOOT)
980 WARN("Unhandled flag DDSCL_ALLOWREBOOT, harmless\n");
981 if (cooplevel & DDSCL_ALLOWMODEX)
982 WARN("Unhandled flag DDSCL_ALLOWMODEX, harmless\n");
983 if (cooplevel & DDSCL_FPUSETUP)
984 WARN("Unhandled flag DDSCL_FPUSETUP, harmless\n");
986 if (cooplevel & DDSCL_EXCLUSIVE)
987 exclusive_ddraw = ddraw;
988 else if (exclusive_ddraw == ddraw)
989 exclusive_ddraw = NULL;
991 /* Store the cooperative_level */
992 ddraw->cooperative_level = cooplevel;
993 ddraw->dest_window = window;
995 TRACE("SetCooperativeLevel retuning DD_OK\n");
996 hr = DD_OK;
997 done:
998 ddraw->flags &= ~DDRAW_SCL_RECURSIVE;
999 wined3d_mutex_unlock();
1001 return hr;
1004 static HRESULT WINAPI ddraw7_SetCooperativeLevel(IDirectDraw7 *iface, HWND window, DWORD flags)
1006 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1008 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1010 return ddraw_set_cooperative_level(ddraw, window, flags, !(ddraw->flags & DDRAW_SCL_DDRAW1));
1013 static HRESULT WINAPI ddraw4_SetCooperativeLevel(IDirectDraw4 *iface, HWND window, DWORD flags)
1015 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1017 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1019 return ddraw_set_cooperative_level(ddraw, window, flags, !(ddraw->flags & DDRAW_SCL_DDRAW1));
1022 static HRESULT WINAPI ddraw2_SetCooperativeLevel(IDirectDraw2 *iface, HWND window, DWORD flags)
1024 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1026 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1028 return ddraw_set_cooperative_level(ddraw, window, flags, !(ddraw->flags & DDRAW_SCL_DDRAW1));
1031 static HRESULT WINAPI ddraw1_SetCooperativeLevel(IDirectDraw *iface, HWND window, DWORD flags)
1033 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1034 HRESULT hr;
1036 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1038 hr = ddraw_set_cooperative_level(ddraw, window, flags, FALSE);
1039 if (SUCCEEDED(hr))
1040 ddraw->flags |= DDRAW_SCL_DDRAW1;
1041 return hr;
1044 /*****************************************************************************
1045 * IDirectDraw7::SetDisplayMode
1047 * Sets the display screen resolution, color depth and refresh frequency
1048 * when in fullscreen mode (in theory).
1049 * Possible return values listed in the SDK suggest that this method fails
1050 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
1051 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
1052 * It seems to be valid to pass 0 for With and Height, this has to be tested
1053 * It could mean that the current video mode should be left as-is. (But why
1054 * call it then?)
1056 * Params:
1057 * Height, Width: Screen dimension
1058 * BPP: Color depth in Bits per pixel
1059 * Refreshrate: Screen refresh rate
1060 * Flags: Other stuff
1062 * Returns
1063 * DD_OK on success
1065 *****************************************************************************/
1066 static HRESULT WINAPI ddraw7_SetDisplayMode(IDirectDraw7 *iface, DWORD width, DWORD height,
1067 DWORD bpp, DWORD refresh_rate, DWORD flags)
1069 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1070 struct wined3d_display_mode mode;
1071 enum wined3d_format_id format;
1072 HRESULT hr;
1074 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1075 iface, width, height, bpp, refresh_rate, flags);
1077 if (force_refresh_rate != 0)
1079 TRACE("ForceRefreshRate overriding passed-in refresh rate (%u Hz) to %u Hz\n",
1080 refresh_rate, force_refresh_rate);
1081 refresh_rate = force_refresh_rate;
1084 wined3d_mutex_lock();
1086 if (exclusive_ddraw && exclusive_ddraw != ddraw)
1088 wined3d_mutex_unlock();
1089 return DDERR_NOEXCLUSIVEMODE;
1092 if (!width || !height)
1094 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here. */
1095 wined3d_mutex_unlock();
1096 return DD_OK;
1099 if (!restore_mode && FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d,
1100 WINED3DADAPTER_DEFAULT, &original_mode, NULL)))
1101 ERR("Failed to get current display mode, hr %#x.\n", hr);
1103 switch (bpp)
1105 case 8: format = WINED3DFMT_P8_UINT; break;
1106 case 15: format = WINED3DFMT_B5G5R5X1_UNORM; break;
1107 case 16: format = WINED3DFMT_B5G6R5_UNORM; break;
1108 case 24: format = WINED3DFMT_B8G8R8_UNORM; break;
1109 case 32: format = WINED3DFMT_B8G8R8X8_UNORM; break;
1110 default: format = WINED3DFMT_UNKNOWN; break;
1113 mode.width = width;
1114 mode.height = height;
1115 mode.refresh_rate = refresh_rate;
1116 mode.format_id = format;
1117 mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
1119 /* TODO: The possible return values from msdn suggest that the screen mode
1120 * can't be changed if a surface is locked or some drawing is in progress. */
1121 /* TODO: Lose the primary surface. */
1122 if (SUCCEEDED(hr = wined3d_set_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode)))
1124 ddraw->flags |= DDRAW_RESTORE_MODE;
1125 restore_mode = TRUE;
1128 wined3d_mutex_unlock();
1130 switch (hr)
1132 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1133 default: return hr;
1137 static HRESULT WINAPI ddraw4_SetDisplayMode(IDirectDraw4 *iface, DWORD width, DWORD height,
1138 DWORD bpp, DWORD refresh_rate, DWORD flags)
1140 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1142 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1143 iface, width, height, bpp, refresh_rate, flags);
1145 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1148 static HRESULT WINAPI ddraw2_SetDisplayMode(IDirectDraw2 *iface,
1149 DWORD width, DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
1151 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1153 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1154 iface, width, height, bpp, refresh_rate, flags);
1156 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1159 static HRESULT WINAPI ddraw1_SetDisplayMode(IDirectDraw *iface, DWORD width, DWORD height, DWORD bpp)
1161 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1163 TRACE("iface %p, width %u, height %u, bpp %u.\n", iface, width, height, bpp);
1165 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, 0, 0);
1168 void ddraw_d3dcaps1_from_7(D3DDEVICEDESC *caps1, D3DDEVICEDESC7 *caps7)
1170 memset(caps1, 0, sizeof(*caps1));
1171 caps1->dwSize = sizeof(*caps1);
1172 caps1->dwFlags = D3DDD_COLORMODEL
1173 | D3DDD_DEVCAPS
1174 | D3DDD_TRANSFORMCAPS
1175 | D3DDD_BCLIPPING
1176 | D3DDD_LIGHTINGCAPS
1177 | D3DDD_LINECAPS
1178 | D3DDD_TRICAPS
1179 | D3DDD_DEVICERENDERBITDEPTH
1180 | D3DDD_DEVICEZBUFFERBITDEPTH
1181 | D3DDD_MAXBUFFERSIZE
1182 | D3DDD_MAXVERTEXCOUNT;
1183 caps1->dcmColorModel = D3DCOLOR_RGB;
1184 caps1->dwDevCaps = caps7->dwDevCaps;
1185 caps1->dtcTransformCaps.dwSize = sizeof(caps1->dtcTransformCaps);
1186 caps1->dtcTransformCaps.dwCaps = D3DTRANSFORMCAPS_CLIP;
1187 caps1->bClipping = TRUE;
1188 caps1->dlcLightingCaps.dwSize = sizeof(caps1->dlcLightingCaps);
1189 caps1->dlcLightingCaps.dwCaps = D3DLIGHTCAPS_DIRECTIONAL
1190 | D3DLIGHTCAPS_PARALLELPOINT
1191 | D3DLIGHTCAPS_POINT
1192 | D3DLIGHTCAPS_SPOT;
1193 caps1->dlcLightingCaps.dwLightingModel = D3DLIGHTINGMODEL_RGB;
1194 caps1->dlcLightingCaps.dwNumLights = caps7->dwMaxActiveLights;
1195 caps1->dpcLineCaps = caps7->dpcLineCaps;
1196 caps1->dpcTriCaps = caps7->dpcTriCaps;
1197 caps1->dwDeviceRenderBitDepth = caps7->dwDeviceRenderBitDepth;
1198 caps1->dwDeviceZBufferBitDepth = caps7->dwDeviceZBufferBitDepth;
1199 caps1->dwMaxBufferSize = 0;
1200 caps1->dwMaxVertexCount = 65536;
1201 caps1->dwMinTextureWidth = caps7->dwMinTextureWidth;
1202 caps1->dwMinTextureHeight = caps7->dwMinTextureHeight;
1203 caps1->dwMaxTextureWidth = caps7->dwMaxTextureWidth;
1204 caps1->dwMaxTextureHeight = caps7->dwMaxTextureHeight;
1205 caps1->dwMinStippleWidth = 1;
1206 caps1->dwMinStippleHeight = 1;
1207 caps1->dwMaxStippleWidth = 32;
1208 caps1->dwMaxStippleHeight = 32;
1209 caps1->dwMaxTextureRepeat = caps7->dwMaxTextureRepeat;
1210 caps1->dwMaxTextureAspectRatio = caps7->dwMaxTextureAspectRatio;
1211 caps1->dwMaxAnisotropy = caps7->dwMaxAnisotropy;
1212 caps1->dvGuardBandLeft = caps7->dvGuardBandLeft;
1213 caps1->dvGuardBandTop = caps7->dvGuardBandTop;
1214 caps1->dvGuardBandRight = caps7->dvGuardBandRight;
1215 caps1->dvGuardBandBottom = caps7->dvGuardBandBottom;
1216 caps1->dvExtentsAdjust = caps7->dvExtentsAdjust;
1217 caps1->dwStencilCaps = caps7->dwStencilCaps;
1218 caps1->dwFVFCaps = caps7->dwFVFCaps;
1219 caps1->dwTextureOpCaps = caps7->dwTextureOpCaps;
1220 caps1->wMaxTextureBlendStages = caps7->wMaxTextureBlendStages;
1221 caps1->wMaxSimultaneousTextures = caps7->wMaxSimultaneousTextures;
1224 HRESULT ddraw_get_d3dcaps(const struct ddraw *ddraw, D3DDEVICEDESC7 *caps)
1226 WINED3DCAPS wined3d_caps;
1227 HRESULT hr;
1229 TRACE("ddraw %p, caps %p.\n", ddraw, caps);
1231 memset(&wined3d_caps, 0, sizeof(wined3d_caps));
1233 wined3d_mutex_lock();
1234 hr = wined3d_get_device_caps(ddraw->wined3d, 0, WINED3D_DEVICE_TYPE_HAL, &wined3d_caps);
1235 wined3d_mutex_unlock();
1236 if (FAILED(hr))
1238 WARN("Failed to get device caps, hr %#x.\n", hr);
1239 return hr;
1242 caps->dwDevCaps = wined3d_caps.DevCaps;
1243 caps->dpcLineCaps.dwMiscCaps = wined3d_caps.PrimitiveMiscCaps;
1244 caps->dpcLineCaps.dwRasterCaps = wined3d_caps.RasterCaps;
1245 caps->dpcLineCaps.dwZCmpCaps = wined3d_caps.ZCmpCaps;
1246 caps->dpcLineCaps.dwSrcBlendCaps = wined3d_caps.SrcBlendCaps;
1247 caps->dpcLineCaps.dwDestBlendCaps = wined3d_caps.DestBlendCaps;
1248 caps->dpcLineCaps.dwAlphaCmpCaps = wined3d_caps.AlphaCmpCaps;
1249 caps->dpcLineCaps.dwShadeCaps = wined3d_caps.ShadeCaps;
1250 caps->dpcLineCaps.dwTextureCaps = wined3d_caps.TextureCaps;
1251 caps->dpcLineCaps.dwTextureFilterCaps = wined3d_caps.TextureFilterCaps;
1252 caps->dpcLineCaps.dwTextureAddressCaps = wined3d_caps.TextureAddressCaps;
1254 caps->dwMaxTextureWidth = wined3d_caps.MaxTextureWidth;
1255 caps->dwMaxTextureHeight = wined3d_caps.MaxTextureHeight;
1257 caps->dwMaxTextureRepeat = wined3d_caps.MaxTextureRepeat;
1258 caps->dwMaxTextureAspectRatio = wined3d_caps.MaxTextureAspectRatio;
1259 caps->dwMaxAnisotropy = wined3d_caps.MaxAnisotropy;
1260 caps->dvMaxVertexW = wined3d_caps.MaxVertexW;
1262 caps->dvGuardBandLeft = wined3d_caps.GuardBandLeft;
1263 caps->dvGuardBandTop = wined3d_caps.GuardBandTop;
1264 caps->dvGuardBandRight = wined3d_caps.GuardBandRight;
1265 caps->dvGuardBandBottom = wined3d_caps.GuardBandBottom;
1267 caps->dvExtentsAdjust = wined3d_caps.ExtentsAdjust;
1268 caps->dwStencilCaps = wined3d_caps.StencilCaps;
1270 caps->dwFVFCaps = wined3d_caps.FVFCaps;
1271 caps->dwTextureOpCaps = wined3d_caps.TextureOpCaps;
1273 caps->dwVertexProcessingCaps = wined3d_caps.VertexProcessingCaps;
1274 caps->dwMaxActiveLights = wined3d_caps.MaxActiveLights;
1276 /* Remove all non-d3d7 caps */
1277 caps->dwDevCaps &= (
1278 D3DDEVCAPS_FLOATTLVERTEX | D3DDEVCAPS_SORTINCREASINGZ | D3DDEVCAPS_SORTDECREASINGZ |
1279 D3DDEVCAPS_SORTEXACT | D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY |
1280 D3DDEVCAPS_TLVERTEXSYSTEMMEMORY | D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY |
1281 D3DDEVCAPS_TEXTUREVIDEOMEMORY | D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP |
1282 D3DDEVCAPS_TEXTURENONLOCALVIDMEM | D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
1283 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT | D3DDEVCAPS_CANBLTSYSTONONLOCAL |
1284 D3DDEVCAPS_HWRASTERIZATION);
1286 caps->dwStencilCaps &= (
1287 D3DSTENCILCAPS_KEEP | D3DSTENCILCAPS_ZERO | D3DSTENCILCAPS_REPLACE |
1288 D3DSTENCILCAPS_INCRSAT | D3DSTENCILCAPS_DECRSAT | D3DSTENCILCAPS_INVERT |
1289 D3DSTENCILCAPS_INCR | D3DSTENCILCAPS_DECR);
1291 /* FVF caps ?*/
1293 caps->dwTextureOpCaps &= (
1294 D3DTEXOPCAPS_DISABLE | D3DTEXOPCAPS_SELECTARG1 | D3DTEXOPCAPS_SELECTARG2 |
1295 D3DTEXOPCAPS_MODULATE | D3DTEXOPCAPS_MODULATE2X | D3DTEXOPCAPS_MODULATE4X |
1296 D3DTEXOPCAPS_ADD | D3DTEXOPCAPS_ADDSIGNED | D3DTEXOPCAPS_ADDSIGNED2X |
1297 D3DTEXOPCAPS_SUBTRACT | D3DTEXOPCAPS_ADDSMOOTH | D3DTEXOPCAPS_BLENDTEXTUREALPHA |
1298 D3DTEXOPCAPS_BLENDFACTORALPHA | D3DTEXOPCAPS_BLENDTEXTUREALPHAPM | D3DTEXOPCAPS_BLENDCURRENTALPHA |
1299 D3DTEXOPCAPS_PREMODULATE | D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
1300 D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA | D3DTEXOPCAPS_BUMPENVMAP |
1301 D3DTEXOPCAPS_BUMPENVMAPLUMINANCE | D3DTEXOPCAPS_DOTPRODUCT3);
1303 caps->dwVertexProcessingCaps &= (
1304 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_VERTEXFOG |
1305 D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER);
1307 caps->dpcLineCaps.dwMiscCaps &= (
1308 D3DPMISCCAPS_MASKPLANES | D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP |
1309 D3DPMISCCAPS_CONFORMANT | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW |
1310 D3DPMISCCAPS_CULLCCW);
1312 caps->dpcLineCaps.dwRasterCaps &= (
1313 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ROP2 | D3DPRASTERCAPS_XOR |
1314 D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_SUBPIXEL |
1315 D3DPRASTERCAPS_SUBPIXELX | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE |
1316 D3DPRASTERCAPS_STIPPLE | D3DPRASTERCAPS_ANTIALIASSORTDEPENDENT | D3DPRASTERCAPS_ANTIALIASSORTINDEPENDENT |
1317 D3DPRASTERCAPS_ANTIALIASEDGES | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBIAS |
1318 D3DPRASTERCAPS_ZBUFFERLESSHSR | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY |
1319 D3DPRASTERCAPS_WBUFFER | D3DPRASTERCAPS_TRANSLUCENTSORTINDEPENDENT | D3DPRASTERCAPS_WFOG |
1320 D3DPRASTERCAPS_ZFOG);
1322 caps->dpcLineCaps.dwZCmpCaps &= (
1323 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
1324 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
1325 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
1327 caps->dpcLineCaps.dwSrcBlendCaps &= (
1328 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
1329 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
1330 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
1331 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
1332 D3DPBLENDCAPS_BOTHINVSRCALPHA);
1334 caps->dpcLineCaps.dwDestBlendCaps &= (
1335 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
1336 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
1337 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
1338 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
1339 D3DPBLENDCAPS_BOTHINVSRCALPHA);
1341 caps->dpcLineCaps.dwAlphaCmpCaps &= (
1342 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
1343 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
1344 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
1346 caps->dpcLineCaps.dwShadeCaps &= (
1347 D3DPSHADECAPS_COLORFLATMONO | D3DPSHADECAPS_COLORFLATRGB | D3DPSHADECAPS_COLORGOURAUDMONO |
1348 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_COLORPHONGMONO | D3DPSHADECAPS_COLORPHONGRGB |
1349 D3DPSHADECAPS_SPECULARFLATMONO | D3DPSHADECAPS_SPECULARFLATRGB | D3DPSHADECAPS_SPECULARGOURAUDMONO |
1350 D3DPSHADECAPS_SPECULARGOURAUDRGB | D3DPSHADECAPS_SPECULARPHONGMONO | D3DPSHADECAPS_SPECULARPHONGRGB |
1351 D3DPSHADECAPS_ALPHAFLATBLEND | D3DPSHADECAPS_ALPHAFLATSTIPPLED | D3DPSHADECAPS_ALPHAGOURAUDBLEND |
1352 D3DPSHADECAPS_ALPHAGOURAUDSTIPPLED | D3DPSHADECAPS_ALPHAPHONGBLEND | D3DPSHADECAPS_ALPHAPHONGSTIPPLED |
1353 D3DPSHADECAPS_FOGFLAT | D3DPSHADECAPS_FOGGOURAUD | D3DPSHADECAPS_FOGPHONG);
1355 caps->dpcLineCaps.dwTextureCaps &= (
1356 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
1357 D3DPTEXTURECAPS_TRANSPARENCY | D3DPTEXTURECAPS_BORDER | D3DPTEXTURECAPS_SQUAREONLY |
1358 D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE | D3DPTEXTURECAPS_ALPHAPALETTE| D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
1359 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_COLORKEYBLEND);
1361 caps->dpcLineCaps.dwTextureFilterCaps &= (
1362 D3DPTFILTERCAPS_NEAREST | D3DPTFILTERCAPS_LINEAR | D3DPTFILTERCAPS_MIPNEAREST |
1363 D3DPTFILTERCAPS_MIPLINEAR | D3DPTFILTERCAPS_LINEARMIPNEAREST | D3DPTFILTERCAPS_LINEARMIPLINEAR |
1364 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
1365 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
1366 D3DPTFILTERCAPS_MAGFLINEAR | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC |
1367 D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC);
1369 caps->dpcLineCaps.dwTextureAddressCaps &= (
1370 D3DPTADDRESSCAPS_WRAP | D3DPTADDRESSCAPS_MIRROR | D3DPTADDRESSCAPS_CLAMP |
1371 D3DPTADDRESSCAPS_BORDER | D3DPTADDRESSCAPS_INDEPENDENTUV);
1373 if (!(caps->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2))
1375 /* DirectX7 always has the np2 flag set, no matter what the card
1376 * supports. Some old games (Rollcage) check the caps incorrectly.
1377 * If wined3d supports nonpow2 textures it also has np2 conditional
1378 * support. */
1379 caps->dpcLineCaps.dwTextureCaps |= D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL;
1382 /* Fill the missing members, and do some fixup */
1383 caps->dpcLineCaps.dwSize = sizeof(caps->dpcLineCaps);
1384 caps->dpcLineCaps.dwTextureBlendCaps = D3DPTBLENDCAPS_ADD
1385 | D3DPTBLENDCAPS_MODULATEMASK
1386 | D3DPTBLENDCAPS_COPY
1387 | D3DPTBLENDCAPS_DECAL
1388 | D3DPTBLENDCAPS_DECALALPHA
1389 | D3DPTBLENDCAPS_DECALMASK
1390 | D3DPTBLENDCAPS_MODULATE
1391 | D3DPTBLENDCAPS_MODULATEALPHA;
1392 caps->dpcLineCaps.dwStippleWidth = 32;
1393 caps->dpcLineCaps.dwStippleHeight = 32;
1394 /* Use the same for the TriCaps */
1395 caps->dpcTriCaps = caps->dpcLineCaps;
1397 caps->dwDeviceRenderBitDepth = DDBD_16 | DDBD_24 | DDBD_32;
1398 caps->dwDeviceZBufferBitDepth = DDBD_16 | DDBD_24;
1399 caps->dwMinTextureWidth = 1;
1400 caps->dwMinTextureHeight = 1;
1402 /* Convert DWORDs safely to WORDs */
1403 if (wined3d_caps.MaxTextureBlendStages > 0xffff)
1404 caps->wMaxTextureBlendStages = 0xffff;
1405 else
1406 caps->wMaxTextureBlendStages = (WORD)wined3d_caps.MaxTextureBlendStages;
1407 if (wined3d_caps.MaxSimultaneousTextures > 0xffff)
1408 caps->wMaxSimultaneousTextures = 0xffff;
1409 else
1410 caps->wMaxSimultaneousTextures = (WORD)wined3d_caps.MaxSimultaneousTextures;
1412 if (wined3d_caps.MaxUserClipPlanes > 0xffff)
1413 caps->wMaxUserClipPlanes = 0xffff;
1414 else
1415 caps->wMaxUserClipPlanes = (WORD)wined3d_caps.MaxUserClipPlanes;
1416 if (wined3d_caps.MaxVertexBlendMatrices > 0xffff)
1417 caps->wMaxVertexBlendMatrices = 0xffff;
1418 else
1419 caps->wMaxVertexBlendMatrices = (WORD)wined3d_caps.MaxVertexBlendMatrices;
1421 caps->deviceGUID = IID_IDirect3DTnLHalDevice;
1423 caps->dwReserved1 = 0;
1424 caps->dwReserved2 = 0;
1425 caps->dwReserved3 = 0;
1426 caps->dwReserved4 = 0;
1428 return DD_OK;
1431 /*****************************************************************************
1432 * IDirectDraw7::GetCaps
1434 * Returns the drives capabilities
1436 * Used for version 1, 2, 4 and 7
1438 * Params:
1439 * DriverCaps: Structure to write the Hardware accelerated caps to
1440 * HelCaps: Structure to write the emulation caps to
1442 * Returns
1443 * This implementation returns DD_OK only
1445 *****************************************************************************/
1446 static HRESULT WINAPI ddraw7_GetCaps(IDirectDraw7 *iface, DDCAPS *DriverCaps, DDCAPS *HELCaps)
1448 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1449 DDCAPS caps;
1450 WINED3DCAPS winecaps;
1451 HRESULT hr;
1452 DDSCAPS2 ddscaps = {0, 0, 0, 0};
1454 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, DriverCaps, HELCaps);
1456 /* One structure must be != NULL */
1457 if (!DriverCaps && !HELCaps)
1459 WARN("Invalid parameters.\n");
1460 return DDERR_INVALIDPARAMS;
1463 memset(&caps, 0, sizeof(caps));
1464 memset(&winecaps, 0, sizeof(winecaps));
1465 caps.dwSize = sizeof(caps);
1467 wined3d_mutex_lock();
1468 hr = wined3d_device_get_device_caps(ddraw->wined3d_device, &winecaps);
1469 if (FAILED(hr))
1471 WARN("IWineD3DDevice::GetDeviceCaps failed\n");
1472 wined3d_mutex_unlock();
1473 return hr;
1476 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
1477 if (FAILED(hr))
1479 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
1480 wined3d_mutex_unlock();
1481 return hr;
1484 hr = IDirectDraw7_GetFourCCCodes(iface, &caps.dwNumFourCCCodes, NULL);
1485 wined3d_mutex_unlock();
1486 if (FAILED(hr))
1488 WARN("IDirectDraw7::GetFourCCCodes failed\n");
1489 return hr;
1492 caps.dwCaps = winecaps.ddraw_caps.caps;
1493 caps.dwCaps2 = winecaps.ddraw_caps.caps2;
1494 caps.dwCKeyCaps = winecaps.ddraw_caps.color_key_caps;
1495 caps.dwFXCaps = winecaps.ddraw_caps.fx_caps;
1496 caps.dwPalCaps = DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE;
1497 caps.ddsCaps.dwCaps = winecaps.ddraw_caps.dds_caps;
1498 caps.dwSVBCaps = winecaps.ddraw_caps.svb_caps;
1499 caps.dwSVBCKeyCaps = winecaps.ddraw_caps.svb_color_key_caps;
1500 caps.dwSVBFXCaps = winecaps.ddraw_caps.svb_fx_caps;
1501 caps.dwVSBCaps = winecaps.ddraw_caps.vsb_caps;
1502 caps.dwVSBCKeyCaps = winecaps.ddraw_caps.vsb_color_key_caps;
1503 caps.dwVSBFXCaps = winecaps.ddraw_caps.vsb_fx_caps;
1504 caps.dwSSBCaps = winecaps.ddraw_caps.ssb_caps;
1505 caps.dwSSBCKeyCaps = winecaps.ddraw_caps.ssb_color_key_caps;
1506 caps.dwSSBFXCaps = winecaps.ddraw_caps.ssb_fx_caps;
1508 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
1509 caps.dwAlignStrideAlign = DDRAW_STRIDE_ALIGNMENT;
1511 if(DriverCaps)
1513 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
1514 if (TRACE_ON(ddraw))
1516 TRACE("Driver Caps :\n");
1517 DDRAW_dump_DDCAPS(DriverCaps);
1521 if(HELCaps)
1523 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
1524 if (TRACE_ON(ddraw))
1526 TRACE("HEL Caps :\n");
1527 DDRAW_dump_DDCAPS(HELCaps);
1531 return DD_OK;
1534 static HRESULT WINAPI ddraw4_GetCaps(IDirectDraw4 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1536 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1538 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1540 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1543 static HRESULT WINAPI ddraw2_GetCaps(IDirectDraw2 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1545 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1547 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1549 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1552 static HRESULT WINAPI ddraw1_GetCaps(IDirectDraw *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1554 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1556 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1558 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1561 /*****************************************************************************
1562 * IDirectDraw7::Compact
1564 * No idea what it does, MSDN says it's not implemented.
1566 * Returns
1567 * DD_OK, but this is unchecked
1569 *****************************************************************************/
1570 static HRESULT WINAPI ddraw7_Compact(IDirectDraw7 *iface)
1572 TRACE("iface %p.\n", iface);
1574 return DD_OK;
1577 static HRESULT WINAPI ddraw4_Compact(IDirectDraw4 *iface)
1579 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1581 TRACE("iface %p.\n", iface);
1583 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1586 static HRESULT WINAPI ddraw2_Compact(IDirectDraw2 *iface)
1588 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1590 TRACE("iface %p.\n", iface);
1592 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1595 static HRESULT WINAPI ddraw1_Compact(IDirectDraw *iface)
1597 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1599 TRACE("iface %p.\n", iface);
1601 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1604 /*****************************************************************************
1605 * IDirectDraw7::GetDisplayMode
1607 * Returns information about the current display mode
1609 * Exists in Version 1, 2, 4 and 7
1611 * Params:
1612 * DDSD: Address of a surface description structure to write the info to
1614 * Returns
1615 * DD_OK
1617 *****************************************************************************/
1618 static HRESULT WINAPI ddraw7_GetDisplayMode(IDirectDraw7 *iface, DDSURFACEDESC2 *DDSD)
1620 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1621 struct wined3d_display_mode mode;
1622 HRESULT hr;
1623 DWORD Size;
1625 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
1627 wined3d_mutex_lock();
1628 /* This seems sane */
1629 if (!DDSD)
1631 wined3d_mutex_unlock();
1632 return DDERR_INVALIDPARAMS;
1635 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
1637 ERR("Failed to get display mode, hr %#x.\n", hr);
1638 wined3d_mutex_unlock();
1639 return hr;
1642 Size = DDSD->dwSize;
1643 memset(DDSD, 0, Size);
1645 DDSD->dwSize = Size;
1646 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
1647 DDSD->dwWidth = mode.width;
1648 DDSD->dwHeight = mode.height;
1649 DDSD->u2.dwRefreshRate = 60;
1650 DDSD->ddsCaps.dwCaps = 0;
1651 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
1652 ddrawformat_from_wined3dformat(&DDSD->u4.ddpfPixelFormat, mode.format_id);
1653 DDSD->u1.lPitch = mode.width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
1655 if(TRACE_ON(ddraw))
1657 TRACE("Returning surface desc :\n");
1658 DDRAW_dump_surface_desc(DDSD);
1661 wined3d_mutex_unlock();
1663 return DD_OK;
1666 static HRESULT WINAPI ddraw4_GetDisplayMode(IDirectDraw4 *iface, DDSURFACEDESC2 *surface_desc)
1668 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1670 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1672 return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, surface_desc);
1675 static HRESULT WINAPI ddraw2_GetDisplayMode(IDirectDraw2 *iface, DDSURFACEDESC *surface_desc)
1677 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1679 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1681 /* FIXME: Test sizes, properly convert surface_desc */
1682 return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1685 static HRESULT WINAPI ddraw1_GetDisplayMode(IDirectDraw *iface, DDSURFACEDESC *surface_desc)
1687 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1689 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1691 /* FIXME: Test sizes, properly convert surface_desc */
1692 return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1695 /*****************************************************************************
1696 * IDirectDraw7::GetFourCCCodes
1698 * Returns an array of supported FourCC codes.
1700 * Exists in Version 1, 2, 4 and 7
1702 * Params:
1703 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
1704 * of enumerated codes
1705 * Codes: Pointer to an array of DWORDs where the supported codes are written
1706 * to
1708 * Returns
1709 * Always returns DD_OK, as it's a stub for now
1711 *****************************************************************************/
1712 static HRESULT WINAPI ddraw7_GetFourCCCodes(IDirectDraw7 *iface, DWORD *NumCodes, DWORD *Codes)
1714 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1715 static const enum wined3d_format_id formats[] =
1717 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
1718 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
1719 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
1721 struct wined3d_display_mode mode;
1722 DWORD count = 0, i, outsize;
1723 HRESULT hr;
1725 TRACE("iface %p, codes_count %p, codes %p.\n", iface, NumCodes, Codes);
1727 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
1729 ERR("Failed to get display mode, hr %#x.\n", hr);
1730 return hr;
1733 outsize = NumCodes && Codes ? *NumCodes : 0;
1735 for (i = 0; i < (sizeof(formats) / sizeof(formats[0])); ++i)
1737 if (SUCCEEDED(wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, WINED3D_DEVICE_TYPE_HAL,
1738 mode.format_id, 0, WINED3D_RTYPE_SURFACE, formats[i])))
1740 if (count < outsize)
1741 Codes[count] = formats[i];
1742 ++count;
1745 if(NumCodes) {
1746 TRACE("Returning %u FourCC codes\n", count);
1747 *NumCodes = count;
1750 return DD_OK;
1753 static HRESULT WINAPI ddraw4_GetFourCCCodes(IDirectDraw4 *iface, DWORD *codes_count, DWORD *codes)
1755 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1757 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1759 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1762 static HRESULT WINAPI ddraw2_GetFourCCCodes(IDirectDraw2 *iface, DWORD *codes_count, DWORD *codes)
1764 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1766 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1768 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1771 static HRESULT WINAPI ddraw1_GetFourCCCodes(IDirectDraw *iface, DWORD *codes_count, DWORD *codes)
1773 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1775 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1777 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1780 static HRESULT WINAPI ddraw7_GetMonitorFrequency(IDirectDraw7 *iface, DWORD *frequency)
1782 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1783 struct wined3d_display_mode mode;
1784 HRESULT hr;
1786 TRACE("iface %p, frequency %p.\n", iface, frequency);
1788 wined3d_mutex_lock();
1789 hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL);
1790 wined3d_mutex_unlock();
1791 if (FAILED(hr))
1793 WARN("Failed to get display mode, hr %#x.\n", hr);
1794 return hr;
1797 *frequency = mode.refresh_rate;
1799 return DD_OK;
1802 static HRESULT WINAPI ddraw4_GetMonitorFrequency(IDirectDraw4 *iface, DWORD *frequency)
1804 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1806 TRACE("iface %p, frequency %p.\n", iface, frequency);
1808 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1811 static HRESULT WINAPI ddraw2_GetMonitorFrequency(IDirectDraw2 *iface, DWORD *frequency)
1813 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1815 TRACE("iface %p, frequency %p.\n", iface, frequency);
1817 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1820 static HRESULT WINAPI ddraw1_GetMonitorFrequency(IDirectDraw *iface, DWORD *frequency)
1822 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1824 TRACE("iface %p, frequency %p.\n", iface, frequency);
1826 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1829 static HRESULT WINAPI ddraw7_GetVerticalBlankStatus(IDirectDraw7 *iface, BOOL *status)
1831 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1832 struct wined3d_raster_status raster_status;
1833 HRESULT hr;
1835 TRACE("iface %p, status %p.\n", iface, status);
1837 if(!status)
1838 return DDERR_INVALIDPARAMS;
1840 wined3d_mutex_lock();
1841 hr = wined3d_get_adapter_raster_status(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &raster_status);
1842 wined3d_mutex_unlock();
1843 if (FAILED(hr))
1845 WARN("Failed to get raster status, hr %#x.\n", hr);
1846 return hr;
1849 *status = raster_status.in_vblank;
1851 return DD_OK;
1854 static HRESULT WINAPI ddraw4_GetVerticalBlankStatus(IDirectDraw4 *iface, BOOL *status)
1856 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1858 TRACE("iface %p, status %p.\n", iface, status);
1860 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1863 static HRESULT WINAPI ddraw2_GetVerticalBlankStatus(IDirectDraw2 *iface, BOOL *status)
1865 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1867 TRACE("iface %p, status %p.\n", iface, status);
1869 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1872 static HRESULT WINAPI ddraw1_GetVerticalBlankStatus(IDirectDraw *iface, BOOL *status)
1874 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1876 TRACE("iface %p, status %p.\n", iface, status);
1878 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1881 /*****************************************************************************
1882 * IDirectDraw7::GetAvailableVidMem
1884 * Returns the total and free video memory
1886 * Params:
1887 * Caps: Specifies the memory type asked for
1888 * total: Pointer to a DWORD to be filled with the total memory
1889 * free: Pointer to a DWORD to be filled with the free memory
1891 * Returns
1892 * DD_OK on success
1893 * DDERR_INVALIDPARAMS if free and total are NULL
1895 *****************************************************************************/
1896 static HRESULT WINAPI ddraw7_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total,
1897 DWORD *free)
1899 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1900 HRESULT hr = DD_OK;
1902 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, Caps, total, free);
1904 if (TRACE_ON(ddraw))
1906 TRACE("Asked for memory with description: ");
1907 DDRAW_dump_DDSCAPS2(Caps);
1909 wined3d_mutex_lock();
1911 /* Todo: System memory vs local video memory vs non-local video memory
1912 * The MSDN also mentions differences between texture memory and other
1913 * resources, but that's not important
1916 if( (!total) && (!free) )
1918 wined3d_mutex_unlock();
1919 return DDERR_INVALIDPARAMS;
1922 if (free)
1923 *free = wined3d_device_get_available_texture_mem(ddraw->wined3d_device);
1924 if (total)
1926 struct wined3d_adapter_identifier desc = {0};
1928 hr = wined3d_get_adapter_identifier(ddraw->wined3d, WINED3DADAPTER_DEFAULT, 0, &desc);
1929 *total = desc.video_memory;
1932 wined3d_mutex_unlock();
1934 return hr;
1937 static HRESULT WINAPI ddraw4_GetAvailableVidMem(IDirectDraw4 *iface,
1938 DDSCAPS2 *caps, DWORD *total, DWORD *free)
1940 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1942 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1944 return ddraw7_GetAvailableVidMem(&ddraw->IDirectDraw7_iface, caps, total, free);
1947 static HRESULT WINAPI ddraw2_GetAvailableVidMem(IDirectDraw2 *iface,
1948 DDSCAPS *caps, DWORD *total, DWORD *free)
1950 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1951 DDSCAPS2 caps2;
1953 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1955 DDRAW_Convert_DDSCAPS_1_To_2(caps, &caps2);
1956 return ddraw7_GetAvailableVidMem(&ddraw->IDirectDraw7_iface, &caps2, total, free);
1959 /*****************************************************************************
1960 * IDirectDraw7::Initialize
1962 * Initializes a DirectDraw interface.
1964 * Params:
1965 * GUID: Interface identifier. Well, don't know what this is really good
1966 * for
1968 * Returns
1969 * Returns DD_OK on the first call,
1970 * DDERR_ALREADYINITIALIZED on repeated calls
1972 *****************************************************************************/
1973 static HRESULT WINAPI ddraw7_Initialize(IDirectDraw7 *iface, GUID *guid)
1975 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1977 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1979 if (ddraw->flags & DDRAW_INITIALIZED)
1980 return DDERR_ALREADYINITIALIZED;
1982 /* FIXME: To properly take the GUID into account we should call
1983 * ddraw_init() here instead of in DDRAW_Create(). */
1984 if (guid)
1985 FIXME("Ignoring guid %s.\n", debugstr_guid(guid));
1987 ddraw->flags |= DDRAW_INITIALIZED;
1988 return DD_OK;
1991 static HRESULT WINAPI ddraw4_Initialize(IDirectDraw4 *iface, GUID *guid)
1993 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1995 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1997 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
2000 static HRESULT WINAPI ddraw2_Initialize(IDirectDraw2 *iface, GUID *guid)
2002 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2004 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
2006 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
2009 static HRESULT WINAPI ddraw1_Initialize(IDirectDraw *iface, GUID *guid)
2011 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2013 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
2015 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
2018 static HRESULT WINAPI d3d1_Initialize(IDirect3D *iface, REFIID riid)
2020 TRACE("iface %p, riid %s.\n", iface, debugstr_guid(riid));
2022 return DDERR_ALREADYINITIALIZED;
2025 /*****************************************************************************
2026 * IDirectDraw7::FlipToGDISurface
2028 * "Makes the surface that the GDI writes to the primary surface"
2029 * Looks like some windows specific thing we don't have to care about.
2030 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
2031 * show error boxes ;)
2032 * Well, just return DD_OK.
2034 * Returns:
2035 * Always returns DD_OK
2037 *****************************************************************************/
2038 static HRESULT WINAPI ddraw7_FlipToGDISurface(IDirectDraw7 *iface)
2040 FIXME("iface %p stub!\n", iface);
2042 return DD_OK;
2045 static HRESULT WINAPI ddraw4_FlipToGDISurface(IDirectDraw4 *iface)
2047 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2049 TRACE("iface %p.\n", iface);
2051 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
2054 static HRESULT WINAPI ddraw2_FlipToGDISurface(IDirectDraw2 *iface)
2056 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2058 TRACE("iface %p.\n", iface);
2060 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
2063 static HRESULT WINAPI ddraw1_FlipToGDISurface(IDirectDraw *iface)
2065 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2067 TRACE("iface %p.\n", iface);
2069 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
2072 /*****************************************************************************
2073 * IDirectDraw7::WaitForVerticalBlank
2075 * This method allows applications to get in sync with the vertical blank
2076 * interval.
2077 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
2078 * redraw the screen, most likely because of this stub
2080 * Parameters:
2081 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
2082 * or DDWAITVB_BLOCKEND
2083 * h: Not used, according to MSDN
2085 * Returns:
2086 * Always returns DD_OK
2088 *****************************************************************************/
2089 static HRESULT WINAPI ddraw7_WaitForVerticalBlank(IDirectDraw7 *iface, DWORD Flags, HANDLE event)
2091 static BOOL hide;
2093 TRACE("iface %p, flags %#x, event %p.\n", iface, Flags, event);
2095 /* This function is called often, so print the fixme only once */
2096 if(!hide)
2098 FIXME("iface %p, flags %#x, event %p stub!\n", iface, Flags, event);
2099 hide = TRUE;
2102 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
2103 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
2104 return DDERR_UNSUPPORTED; /* unchecked */
2106 return DD_OK;
2109 static HRESULT WINAPI ddraw4_WaitForVerticalBlank(IDirectDraw4 *iface, DWORD flags, HANDLE event)
2111 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2113 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
2115 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
2118 static HRESULT WINAPI ddraw2_WaitForVerticalBlank(IDirectDraw2 *iface, DWORD flags, HANDLE event)
2120 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2122 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
2124 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
2127 static HRESULT WINAPI ddraw1_WaitForVerticalBlank(IDirectDraw *iface, DWORD flags, HANDLE event)
2129 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2131 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
2133 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
2136 static HRESULT WINAPI ddraw7_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
2138 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2139 struct wined3d_raster_status raster_status;
2140 HRESULT hr;
2142 TRACE("iface %p, line %p.\n", iface, Scanline);
2144 wined3d_mutex_lock();
2145 hr = wined3d_get_adapter_raster_status(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &raster_status);
2146 wined3d_mutex_unlock();
2147 if (FAILED(hr))
2149 WARN("Failed to get raster status, hr %#x.\n", hr);
2150 return hr;
2153 *Scanline = raster_status.scan_line;
2155 if (raster_status.in_vblank)
2156 return DDERR_VERTICALBLANKINPROGRESS;
2158 return DD_OK;
2161 static HRESULT WINAPI ddraw4_GetScanLine(IDirectDraw4 *iface, DWORD *line)
2163 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2165 TRACE("iface %p, line %p.\n", iface, line);
2167 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
2170 static HRESULT WINAPI ddraw2_GetScanLine(IDirectDraw2 *iface, DWORD *line)
2172 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2174 TRACE("iface %p, line %p.\n", iface, line);
2176 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
2179 static HRESULT WINAPI ddraw1_GetScanLine(IDirectDraw *iface, DWORD *line)
2181 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2183 TRACE("iface %p, line %p.\n", iface, line);
2185 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
2188 static HRESULT WINAPI ddraw7_TestCooperativeLevel(IDirectDraw7 *iface)
2190 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2192 TRACE("iface %p.\n", iface);
2194 return ddraw->device_state == DDRAW_DEVICE_STATE_OK ? DD_OK : DDERR_NOEXCLUSIVEMODE;
2197 static HRESULT WINAPI ddraw4_TestCooperativeLevel(IDirectDraw4 *iface)
2199 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2201 TRACE("iface %p.\n", iface);
2203 return ddraw7_TestCooperativeLevel(&ddraw->IDirectDraw7_iface);
2206 /*****************************************************************************
2207 * IDirectDraw7::GetGDISurface
2209 * Returns the surface that GDI is treating as the primary surface.
2210 * For Wine this is the front buffer
2212 * Params:
2213 * GDISurface: Address to write the surface pointer to
2215 * Returns:
2216 * DD_OK if the surface was found
2217 * DDERR_NOTFOUND if the GDI surface wasn't found
2219 *****************************************************************************/
2220 static HRESULT WINAPI ddraw7_GetGDISurface(IDirectDraw7 *iface, IDirectDrawSurface7 **GDISurface)
2222 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2224 TRACE("iface %p, surface %p.\n", iface, GDISurface);
2226 wined3d_mutex_lock();
2228 if (!(*GDISurface = &ddraw->primary->IDirectDrawSurface7_iface))
2230 WARN("Primary not created yet.\n");
2231 wined3d_mutex_unlock();
2232 return DDERR_NOTFOUND;
2234 IDirectDrawSurface7_AddRef(*GDISurface);
2236 wined3d_mutex_unlock();
2238 return DD_OK;
2241 static HRESULT WINAPI ddraw4_GetGDISurface(IDirectDraw4 *iface, IDirectDrawSurface4 **surface)
2243 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2244 struct ddraw_surface *surface_impl;
2245 IDirectDrawSurface7 *surface7;
2246 HRESULT hr;
2248 TRACE("iface %p, surface %p.\n", iface, surface);
2250 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2251 if (FAILED(hr))
2253 *surface = NULL;
2254 return hr;
2256 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2257 *surface = &surface_impl->IDirectDrawSurface4_iface;
2258 IDirectDrawSurface4_AddRef(*surface);
2259 IDirectDrawSurface7_Release(surface7);
2261 return hr;
2264 static HRESULT WINAPI ddraw2_GetGDISurface(IDirectDraw2 *iface, IDirectDrawSurface **surface)
2266 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2267 struct ddraw_surface *surface_impl;
2268 IDirectDrawSurface7 *surface7;
2269 HRESULT hr;
2271 TRACE("iface %p, surface %p.\n", iface, surface);
2273 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2274 if (FAILED(hr))
2276 *surface = NULL;
2277 return hr;
2279 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2280 *surface = &surface_impl->IDirectDrawSurface_iface;
2281 IDirectDrawSurface_AddRef(*surface);
2282 IDirectDrawSurface7_Release(surface7);
2284 return hr;
2287 static HRESULT WINAPI ddraw1_GetGDISurface(IDirectDraw *iface, IDirectDrawSurface **surface)
2289 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2290 struct ddraw_surface *surface_impl;
2291 IDirectDrawSurface7 *surface7;
2292 HRESULT hr;
2294 TRACE("iface %p, surface %p.\n", iface, surface);
2296 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2297 if (FAILED(hr))
2299 *surface = NULL;
2300 return hr;
2302 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2303 *surface = &surface_impl->IDirectDrawSurface_iface;
2304 IDirectDrawSurface_AddRef(*surface);
2305 IDirectDrawSurface7_Release(surface7);
2307 return hr;
2310 struct displaymodescallback_context
2312 LPDDENUMMODESCALLBACK func;
2313 void *context;
2316 static HRESULT CALLBACK EnumDisplayModesCallbackThunk(DDSURFACEDESC2 *surface_desc, void *context)
2318 struct displaymodescallback_context *cbcontext = context;
2319 DDSURFACEDESC desc;
2321 DDSD2_to_DDSD(surface_desc, &desc);
2322 return cbcontext->func(&desc, cbcontext->context);
2325 /*****************************************************************************
2326 * IDirectDraw7::EnumDisplayModes
2328 * Enumerates the supported Display modes. The modes can be filtered with
2329 * the DDSD parameter.
2331 * Params:
2332 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES. For old ddraw
2333 * versions (3 and older?) this is reserved and must be 0.
2334 * DDSD: Surface description to filter the modes
2335 * Context: Pointer passed back to the callback function
2336 * cb: Application-provided callback function
2338 * Returns:
2339 * DD_OK on success
2340 * DDERR_INVALIDPARAMS if the callback wasn't set
2342 *****************************************************************************/
2343 static HRESULT WINAPI ddraw7_EnumDisplayModes(IDirectDraw7 *iface, DWORD Flags,
2344 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMMODESCALLBACK2 cb)
2346 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2347 struct wined3d_display_mode *enum_modes = NULL;
2348 struct wined3d_display_mode mode;
2349 unsigned int modenum, fmt;
2350 DDSURFACEDESC2 callback_sd;
2351 unsigned enum_mode_count = 0, enum_mode_array_size = 16;
2352 DDPIXELFORMAT pixelformat;
2354 static const enum wined3d_format_id checkFormatList[] =
2356 WINED3DFMT_B8G8R8X8_UNORM,
2357 WINED3DFMT_B5G6R5_UNORM,
2358 WINED3DFMT_P8_UINT,
2361 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2362 iface, Flags, DDSD, Context, cb);
2364 if (!cb)
2365 return DDERR_INVALIDPARAMS;
2367 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(*enum_modes) * enum_mode_array_size);
2368 if (!enum_modes) return DDERR_OUTOFMEMORY;
2370 wined3d_mutex_lock();
2372 pixelformat.dwSize = sizeof(pixelformat);
2373 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
2375 modenum = 0;
2376 while (wined3d_enum_adapter_modes(ddraw->wined3d, WINED3DADAPTER_DEFAULT, checkFormatList[fmt],
2377 WINED3D_SCANLINE_ORDERING_UNKNOWN, modenum++, &mode) == WINED3D_OK)
2379 BOOL found = FALSE;
2380 unsigned i;
2382 ddrawformat_from_wined3dformat(&pixelformat, mode.format_id);
2383 if (DDSD)
2385 if (DDSD->dwFlags & DDSD_WIDTH && mode.width != DDSD->dwWidth)
2386 continue;
2387 if (DDSD->dwFlags & DDSD_HEIGHT && mode.height != DDSD->dwHeight)
2388 continue;
2389 if (DDSD->dwFlags & DDSD_REFRESHRATE && mode.refresh_rate != DDSD->u2.dwRefreshRate)
2390 continue;
2391 if (DDSD->dwFlags & DDSD_PIXELFORMAT
2392 && pixelformat.u1.dwRGBBitCount != DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount)
2393 continue;
2396 /* DX docs state EnumDisplayMode should return only unique modes */
2397 for (i = 0; i < enum_mode_count; i++)
2399 if (enum_modes[i].width == mode.width && enum_modes[i].height == mode.height
2400 && enum_modes[i].format_id == mode.format_id
2401 && (enum_modes[i].refresh_rate == mode.refresh_rate || !(Flags & DDEDM_REFRESHRATES)))
2403 found = TRUE;
2404 break;
2407 if(found) continue;
2409 memset(&callback_sd, 0, sizeof(callback_sd));
2410 callback_sd.dwSize = sizeof(callback_sd);
2411 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2413 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH|DDSD_REFRESHRATE;
2414 if (Flags & DDEDM_REFRESHRATES)
2415 callback_sd.u2.dwRefreshRate = mode.refresh_rate;
2417 callback_sd.dwWidth = mode.width;
2418 callback_sd.dwHeight = mode.height;
2420 callback_sd.u4.ddpfPixelFormat=pixelformat;
2422 /* Calc pitch and DWORD align like MSDN says */
2423 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.width;
2424 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
2426 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
2427 callback_sd.u2.dwRefreshRate);
2429 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
2431 TRACE("Application asked to terminate the enumeration\n");
2432 HeapFree(GetProcessHeap(), 0, enum_modes);
2433 wined3d_mutex_unlock();
2434 return DD_OK;
2437 if (enum_mode_count == enum_mode_array_size)
2439 struct wined3d_display_mode *new_enum_modes;
2441 enum_mode_array_size *= 2;
2442 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes,
2443 sizeof(*new_enum_modes) * enum_mode_array_size);
2444 if (!new_enum_modes)
2446 HeapFree(GetProcessHeap(), 0, enum_modes);
2447 wined3d_mutex_unlock();
2448 return DDERR_OUTOFMEMORY;
2451 enum_modes = new_enum_modes;
2453 enum_modes[enum_mode_count++] = mode;
2457 TRACE("End of enumeration\n");
2458 HeapFree(GetProcessHeap(), 0, enum_modes);
2459 wined3d_mutex_unlock();
2461 return DD_OK;
2464 static HRESULT WINAPI ddraw4_EnumDisplayModes(IDirectDraw4 *iface, DWORD flags,
2465 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMMODESCALLBACK2 callback)
2467 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2469 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2470 iface, flags, surface_desc, context, callback);
2472 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags, surface_desc, context, callback);
2475 static HRESULT WINAPI ddraw2_EnumDisplayModes(IDirectDraw2 *iface, DWORD flags,
2476 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2478 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2479 struct displaymodescallback_context cbcontext;
2480 DDSURFACEDESC2 surface_desc2;
2482 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2483 iface, flags, surface_desc, context, callback);
2485 cbcontext.func = callback;
2486 cbcontext.context = context;
2488 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2489 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags,
2490 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2493 static HRESULT WINAPI ddraw1_EnumDisplayModes(IDirectDraw *iface, DWORD flags,
2494 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2496 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2497 struct displaymodescallback_context cbcontext;
2498 DDSURFACEDESC2 surface_desc2;
2500 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2501 iface, flags, surface_desc, context, callback);
2503 cbcontext.func = callback;
2504 cbcontext.context = context;
2506 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2507 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags,
2508 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2511 /*****************************************************************************
2512 * IDirectDraw7::EvaluateMode
2514 * Used with IDirectDraw7::StartModeTest to test video modes.
2515 * EvaluateMode is used to pass or fail a mode, and continue with the next
2516 * mode
2518 * Params:
2519 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
2520 * Timeout: Returns the amount of seconds left before the mode would have
2521 * been failed automatically
2523 * Returns:
2524 * This implementation always DD_OK, because it's a stub
2526 *****************************************************************************/
2527 static HRESULT WINAPI ddraw7_EvaluateMode(IDirectDraw7 *iface, DWORD Flags, DWORD *Timeout)
2529 FIXME("iface %p, flags %#x, timeout %p stub!\n", iface, Flags, Timeout);
2531 /* When implementing this, implement it in WineD3D */
2533 return DD_OK;
2536 /*****************************************************************************
2537 * IDirectDraw7::GetDeviceIdentifier
2539 * Returns the device identifier, which gives information about the driver
2540 * Our device identifier is defined at the beginning of this file.
2542 * Params:
2543 * DDDI: Address for the returned structure
2544 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
2546 * Returns:
2547 * On success it returns DD_OK
2548 * DDERR_INVALIDPARAMS if DDDI is NULL
2550 *****************************************************************************/
2551 static HRESULT WINAPI ddraw7_GetDeviceIdentifier(IDirectDraw7 *iface,
2552 DDDEVICEIDENTIFIER2 *DDDI, DWORD Flags)
2554 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2555 struct wined3d_adapter_identifier adapter_id;
2556 HRESULT hr = S_OK;
2558 TRACE("iface %p, device_identifier %p, flags %#x.\n", iface, DDDI, Flags);
2560 if (!DDDI)
2561 return DDERR_INVALIDPARAMS;
2563 if (Flags & DDGDI_GETHOSTIDENTIFIER)
2565 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
2566 * host adapter, if there's a secondary 3D adapter. This doesn't apply
2567 * to any modern hardware, nor is it interesting for Wine, so ignore it.
2568 * Size of DDDEVICEIDENTIFIER2 may be aligned to 8 bytes and thus 4
2569 * bytes too long. So only copy the relevant part of the structure
2572 memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
2573 return DD_OK;
2576 /* Drakan: Order of the Flame expects accurate D3D device information from ddraw */
2577 adapter_id.driver = DDDI->szDriver;
2578 adapter_id.driver_size = sizeof(DDDI->szDriver);
2579 adapter_id.description = DDDI->szDescription;
2580 adapter_id.description_size = sizeof(DDDI->szDescription);
2581 adapter_id.device_name_size = 0;
2582 wined3d_mutex_lock();
2583 hr = wined3d_get_adapter_identifier(ddraw->wined3d, WINED3DADAPTER_DEFAULT, 0x0, &adapter_id);
2584 wined3d_mutex_unlock();
2585 if (FAILED(hr)) return hr;
2587 DDDI->liDriverVersion = adapter_id.driver_version;
2588 DDDI->dwVendorId = adapter_id.vendor_id;
2589 DDDI->dwDeviceId = adapter_id.device_id;
2590 DDDI->dwSubSysId = adapter_id.subsystem_id;
2591 DDDI->dwRevision = adapter_id.revision;
2592 DDDI->guidDeviceIdentifier = adapter_id.device_identifier;
2593 DDDI->dwWHQLLevel = adapter_id.whql_level;
2594 return DD_OK;
2597 static HRESULT WINAPI ddraw4_GetDeviceIdentifier(IDirectDraw4 *iface,
2598 DDDEVICEIDENTIFIER *identifier, DWORD flags)
2600 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2601 DDDEVICEIDENTIFIER2 identifier2;
2602 HRESULT hr;
2604 TRACE("iface %p, identifier %p, flags %#x.\n", iface, identifier, flags);
2606 hr = ddraw7_GetDeviceIdentifier(&ddraw->IDirectDraw7_iface, &identifier2, flags);
2607 DDRAW_Convert_DDDEVICEIDENTIFIER_2_To_1(&identifier2, identifier);
2609 return hr;
2612 /*****************************************************************************
2613 * IDirectDraw7::GetSurfaceFromDC
2615 * Returns the Surface for a GDI device context handle.
2616 * Is this related to IDirectDrawSurface::GetDC ???
2618 * Params:
2619 * hdc: hdc to return the surface for
2620 * Surface: Address to write the surface pointer to
2622 * Returns:
2623 * Always returns DD_OK because it's a stub
2625 *****************************************************************************/
2626 static HRESULT WINAPI ddraw7_GetSurfaceFromDC(IDirectDraw7 *iface, HDC hdc,
2627 IDirectDrawSurface7 **Surface)
2629 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2630 struct wined3d_surface *wined3d_surface;
2631 struct ddraw_surface *surface_impl;
2633 TRACE("iface %p, dc %p, surface %p.\n", iface, hdc, Surface);
2635 if (!Surface) return E_INVALIDARG;
2637 if (!(wined3d_surface = wined3d_device_get_surface_from_dc(ddraw->wined3d_device, hdc)))
2639 TRACE("No surface found for dc %p.\n", hdc);
2640 *Surface = NULL;
2641 return DDERR_NOTFOUND;
2644 surface_impl = wined3d_surface_get_parent(wined3d_surface);
2645 *Surface = &surface_impl->IDirectDrawSurface7_iface;
2646 IDirectDrawSurface7_AddRef(*Surface);
2647 TRACE("Returning surface %p.\n", Surface);
2648 return DD_OK;
2651 static HRESULT WINAPI ddraw4_GetSurfaceFromDC(IDirectDraw4 *iface, HDC dc,
2652 IDirectDrawSurface4 **surface)
2654 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2655 struct ddraw_surface *surface_impl;
2656 IDirectDrawSurface7 *surface7;
2657 HRESULT hr;
2659 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2661 if (!surface) return E_INVALIDARG;
2663 hr = ddraw7_GetSurfaceFromDC(&ddraw->IDirectDraw7_iface, dc, &surface7);
2664 if (FAILED(hr))
2666 *surface = NULL;
2667 return hr;
2669 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2670 /* Tests say this is true */
2671 *surface = (IDirectDrawSurface4 *)&surface_impl->IDirectDrawSurface_iface;
2672 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
2673 IDirectDrawSurface7_Release(surface7);
2675 return hr;
2678 static HRESULT CALLBACK restore_callback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *desc, void *context)
2680 IDirectDrawSurface_Restore(surface);
2681 IDirectDrawSurface_Release(surface);
2683 return DDENUMRET_OK;
2686 static HRESULT WINAPI ddraw7_RestoreAllSurfaces(IDirectDraw7 *iface)
2688 TRACE("iface %p.\n", iface);
2690 return IDirectDraw7_EnumSurfaces(iface, DDENUMSURFACES_ALL | DDENUMSURFACES_DOESEXIST,
2691 NULL, NULL, restore_callback);
2694 static HRESULT WINAPI ddraw4_RestoreAllSurfaces(IDirectDraw4 *iface)
2696 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2698 TRACE("iface %p.\n", iface);
2700 return ddraw7_RestoreAllSurfaces(&ddraw->IDirectDraw7_iface);
2703 /*****************************************************************************
2704 * IDirectDraw7::StartModeTest
2706 * Tests the specified video modes to update the system registry with
2707 * refresh rate information. StartModeTest starts the mode test,
2708 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
2709 * isn't called within 15 seconds, the mode is failed automatically
2711 * As refresh rates are handled by the X server, I don't think this
2712 * Method is important
2714 * Params:
2715 * Modes: An array of mode specifications
2716 * NumModes: The number of modes in Modes
2717 * Flags: Some flags...
2719 * Returns:
2720 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
2721 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
2722 * otherwise DD_OK
2724 *****************************************************************************/
2725 static HRESULT WINAPI ddraw7_StartModeTest(IDirectDraw7 *iface, SIZE *Modes, DWORD NumModes, DWORD Flags)
2727 FIXME("iface %p, modes %p, mode_count %u, flags %#x partial stub!\n",
2728 iface, Modes, NumModes, Flags);
2730 /* This looks sane */
2731 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
2733 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
2734 * As it is not, DDERR_TESTFINISHED is returned
2735 * (hopefully that's correct
2737 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
2738 * well, that value doesn't (yet) exist in the wine headers, so ignore it
2741 return DD_OK;
2744 static HRESULT WINAPI ddraw7_CreateSurface(IDirectDraw7 *iface, DDSURFACEDESC2 *surface_desc,
2745 IDirectDrawSurface7 **surface, IUnknown *outer_unknown)
2747 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2748 struct ddraw_surface *impl;
2749 HRESULT hr;
2751 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2752 iface, surface_desc, surface, outer_unknown);
2754 wined3d_mutex_lock();
2756 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2758 WARN("Cooperative level not set.\n");
2759 wined3d_mutex_unlock();
2760 return DDERR_NOCOOPERATIVELEVELSET;
2763 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
2765 WARN("Application supplied invalid surface descriptor\n");
2766 wined3d_mutex_unlock();
2767 return DDERR_INVALIDPARAMS;
2770 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
2772 if (TRACE_ON(ddraw))
2774 TRACE(" (%p) Requesting surface desc :\n", iface);
2775 DDRAW_dump_surface_desc(surface_desc);
2778 WARN("Application tried to create an explicit front or back buffer\n");
2779 wined3d_mutex_unlock();
2780 return DDERR_INVALIDCAPS;
2783 hr = ddraw_surface_create(ddraw, surface_desc, &impl, outer_unknown, 7);
2784 wined3d_mutex_unlock();
2785 if (FAILED(hr))
2787 *surface = NULL;
2788 return hr;
2791 *surface = &impl->IDirectDrawSurface7_iface;
2792 IDirectDraw7_AddRef(iface);
2793 impl->ifaceToRelease = (IUnknown *)iface;
2795 return hr;
2798 static HRESULT WINAPI ddraw4_CreateSurface(IDirectDraw4 *iface,
2799 DDSURFACEDESC2 *surface_desc, IDirectDrawSurface4 **surface, IUnknown *outer_unknown)
2801 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2802 struct ddraw_surface *impl;
2803 HRESULT hr;
2805 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2806 iface, surface_desc, surface, outer_unknown);
2808 wined3d_mutex_lock();
2810 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2812 WARN("Cooperative level not set.\n");
2813 wined3d_mutex_unlock();
2814 return DDERR_NOCOOPERATIVELEVELSET;
2817 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
2819 WARN("Application supplied invalid surface descriptor\n");
2820 wined3d_mutex_unlock();
2821 return DDERR_INVALIDPARAMS;
2824 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
2826 if (TRACE_ON(ddraw))
2828 TRACE(" (%p) Requesting surface desc :\n", iface);
2829 DDRAW_dump_surface_desc(surface_desc);
2832 WARN("Application tried to create an explicit front or back buffer\n");
2833 wined3d_mutex_unlock();
2834 return DDERR_INVALIDCAPS;
2837 hr = ddraw_surface_create(ddraw, surface_desc, &impl, outer_unknown, 4);
2838 wined3d_mutex_unlock();
2839 if (FAILED(hr))
2841 *surface = NULL;
2842 return hr;
2845 *surface = &impl->IDirectDrawSurface4_iface;
2846 IDirectDraw4_AddRef(iface);
2847 impl->ifaceToRelease = (IUnknown *)iface;
2849 return hr;
2852 static HRESULT WINAPI ddraw2_CreateSurface(IDirectDraw2 *iface,
2853 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
2855 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2856 struct ddraw_surface *impl;
2857 HRESULT hr;
2858 DDSURFACEDESC2 surface_desc2;
2860 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2861 iface, surface_desc, surface, outer_unknown);
2863 wined3d_mutex_lock();
2865 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2867 WARN("Cooperative level not set.\n");
2868 wined3d_mutex_unlock();
2869 return DDERR_NOCOOPERATIVELEVELSET;
2872 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
2874 WARN("Application supplied invalid surface descriptor\n");
2875 wined3d_mutex_unlock();
2876 return DDERR_INVALIDPARAMS;
2879 DDSD_to_DDSD2(surface_desc, &surface_desc2);
2880 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
2882 if (TRACE_ON(ddraw))
2884 TRACE(" (%p) Requesting surface desc :\n", iface);
2885 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
2888 WARN("Application tried to create an explicit front or back buffer\n");
2889 wined3d_mutex_unlock();
2890 return DDERR_INVALIDCAPS;
2893 hr = ddraw_surface_create(ddraw, &surface_desc2, &impl, outer_unknown, 2);
2894 wined3d_mutex_unlock();
2895 if (FAILED(hr))
2897 *surface = NULL;
2898 return hr;
2901 *surface = &impl->IDirectDrawSurface_iface;
2902 impl->ifaceToRelease = NULL;
2904 return hr;
2907 static HRESULT WINAPI ddraw1_CreateSurface(IDirectDraw *iface,
2908 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
2910 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2911 struct ddraw_surface *impl;
2912 HRESULT hr;
2913 DDSURFACEDESC2 surface_desc2;
2915 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2916 iface, surface_desc, surface, outer_unknown);
2918 wined3d_mutex_lock();
2920 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2922 WARN("Cooperative level not set.\n");
2923 wined3d_mutex_unlock();
2924 return DDERR_NOCOOPERATIVELEVELSET;
2927 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
2929 WARN("Application supplied invalid surface descriptor\n");
2930 wined3d_mutex_unlock();
2931 return DDERR_INVALIDPARAMS;
2934 if ((surface_desc->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER))
2935 == (DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER)
2936 || (surface_desc->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER))
2937 == ((DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER)))
2939 WARN("Application tried to create an explicit front or back buffer.\n");
2940 wined3d_mutex_unlock();
2941 return DDERR_INVALIDCAPS;
2944 DDSD_to_DDSD2(surface_desc, &surface_desc2);
2945 hr = ddraw_surface_create(ddraw, &surface_desc2, &impl, outer_unknown, 1);
2946 wined3d_mutex_unlock();
2947 if (FAILED(hr))
2949 *surface = NULL;
2950 return hr;
2953 *surface = &impl->IDirectDrawSurface_iface;
2954 impl->ifaceToRelease = NULL;
2956 return hr;
2959 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
2960 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
2962 static BOOL
2963 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
2964 const DDPIXELFORMAT *provided)
2966 /* Some flags must be present in both or neither for a match. */
2967 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
2968 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
2969 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
2971 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2972 return FALSE;
2974 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
2975 return FALSE;
2977 if (requested->dwFlags & DDPF_FOURCC)
2978 if (requested->dwFourCC != provided->dwFourCC)
2979 return FALSE;
2981 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
2982 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2983 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
2984 return FALSE;
2986 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2987 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2988 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
2989 return FALSE;
2991 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
2992 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
2993 return FALSE;
2995 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
2996 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2997 |DDPF_BUMPDUDV))
2998 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
2999 return FALSE;
3001 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
3002 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
3003 return FALSE;
3005 return TRUE;
3008 static BOOL ddraw_match_surface_desc(const DDSURFACEDESC2 *requested, const DDSURFACEDESC2 *provided)
3010 struct compare_info
3012 DWORD flag;
3013 ptrdiff_t offset;
3014 size_t size;
3017 #define CMP(FLAG, FIELD) \
3018 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
3019 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
3021 static const struct compare_info compare[] =
3023 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
3024 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
3025 CMP(CAPS, ddsCaps),
3026 CMP(CKDESTBLT, ddckCKDestBlt),
3027 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
3028 CMP(CKSRCBLT, ddckCKSrcBlt),
3029 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
3030 CMP(HEIGHT, dwHeight),
3031 CMP(LINEARSIZE, u1 /* dwLinearSize */),
3032 CMP(LPSURFACE, lpSurface),
3033 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
3034 CMP(PITCH, u1 /* lPitch */),
3035 /* PIXELFORMAT: manual */
3036 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
3037 CMP(TEXTURESTAGE, dwTextureStage),
3038 CMP(WIDTH, dwWidth),
3039 /* ZBUFFERBITDEPTH: "obsolete" */
3042 #undef CMP
3044 unsigned int i;
3046 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3047 return FALSE;
3049 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
3051 if (requested->dwFlags & compare[i].flag
3052 && memcmp((const char *)provided + compare[i].offset,
3053 (const char *)requested + compare[i].offset,
3054 compare[i].size) != 0)
3055 return FALSE;
3058 if (requested->dwFlags & DDSD_PIXELFORMAT)
3060 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
3061 &provided->u4.ddpfPixelFormat))
3062 return FALSE;
3065 return TRUE;
3068 #undef DDENUMSURFACES_SEARCHTYPE
3069 #undef DDENUMSURFACES_MATCHTYPE
3071 struct surfacescallback2_context
3073 LPDDENUMSURFACESCALLBACK2 func;
3074 void *context;
3077 struct surfacescallback_context
3079 LPDDENUMSURFACESCALLBACK func;
3080 void *context;
3083 static HRESULT CALLBACK EnumSurfacesCallback2Thunk(IDirectDrawSurface7 *surface,
3084 DDSURFACEDESC2 *surface_desc, void *context)
3086 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3087 struct surfacescallback2_context *cbcontext = context;
3089 IDirectDrawSurface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
3090 IDirectDrawSurface7_Release(surface);
3092 return cbcontext->func(&surface_impl->IDirectDrawSurface4_iface,
3093 surface_desc, cbcontext->context);
3096 static HRESULT CALLBACK EnumSurfacesCallbackThunk(IDirectDrawSurface7 *surface,
3097 DDSURFACEDESC2 *surface_desc, void *context)
3099 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3100 struct surfacescallback_context *cbcontext = context;
3102 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
3103 IDirectDrawSurface7_Release(surface);
3105 return cbcontext->func(&surface_impl->IDirectDrawSurface_iface,
3106 (DDSURFACEDESC *)surface_desc, cbcontext->context);
3109 /*****************************************************************************
3110 * IDirectDraw7::EnumSurfaces
3112 * Loops through all surfaces attached to this device and calls the
3113 * application callback. This can't be relayed to WineD3DDevice,
3114 * because some WineD3DSurfaces' parents are IParent objects
3116 * Params:
3117 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
3118 * DDSD: Description to filter for
3119 * Context: Application-provided pointer, it's passed unmodified to the
3120 * Callback function
3121 * Callback: Address to call for each surface
3123 * Returns:
3124 * DDERR_INVALIDPARAMS if the callback is NULL
3125 * DD_OK on success
3127 *****************************************************************************/
3128 static HRESULT WINAPI ddraw7_EnumSurfaces(IDirectDraw7 *iface, DWORD Flags,
3129 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMSURFACESCALLBACK7 Callback)
3131 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
3132 struct ddraw_surface *surf;
3133 BOOL all, nomatch;
3134 DDSURFACEDESC2 desc;
3135 struct list *entry, *entry2;
3137 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3138 iface, Flags, DDSD, Context, Callback);
3140 all = Flags & DDENUMSURFACES_ALL;
3141 nomatch = Flags & DDENUMSURFACES_NOMATCH;
3143 if (!Callback)
3144 return DDERR_INVALIDPARAMS;
3146 wined3d_mutex_lock();
3148 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
3149 LIST_FOR_EACH_SAFE(entry, entry2, &ddraw->surface_list)
3151 surf = LIST_ENTRY(entry, struct ddraw_surface, surface_list_entry);
3153 if (!surf->iface_count)
3155 WARN("Not enumerating surface %p because it doesn't have any references.\n", surf);
3156 continue;
3159 if (all || (nomatch != ddraw_match_surface_desc(DDSD, &surf->surface_desc)))
3161 TRACE("Enumerating surface %p.\n", surf);
3162 desc = surf->surface_desc;
3163 IDirectDrawSurface7_AddRef(&surf->IDirectDrawSurface7_iface);
3164 if (Callback(&surf->IDirectDrawSurface7_iface, &desc, Context) != DDENUMRET_OK)
3166 wined3d_mutex_unlock();
3167 return DD_OK;
3172 wined3d_mutex_unlock();
3174 return DD_OK;
3177 static HRESULT WINAPI ddraw4_EnumSurfaces(IDirectDraw4 *iface, DWORD flags,
3178 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMSURFACESCALLBACK2 callback)
3180 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3181 struct surfacescallback2_context cbcontext;
3183 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3184 iface, flags, surface_desc, context, callback);
3186 cbcontext.func = callback;
3187 cbcontext.context = context;
3189 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags, surface_desc,
3190 &cbcontext, EnumSurfacesCallback2Thunk);
3193 static HRESULT WINAPI ddraw2_EnumSurfaces(IDirectDraw2 *iface, DWORD flags,
3194 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3196 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3197 struct surfacescallback_context cbcontext;
3198 DDSURFACEDESC2 surface_desc2;
3200 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3201 iface, flags, surface_desc, context, callback);
3203 cbcontext.func = callback;
3204 cbcontext.context = context;
3206 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3207 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags,
3208 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3211 static HRESULT WINAPI ddraw1_EnumSurfaces(IDirectDraw *iface, DWORD flags,
3212 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3214 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3215 struct surfacescallback_context cbcontext;
3216 DDSURFACEDESC2 surface_desc2;
3218 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3219 iface, flags, surface_desc, context, callback);
3221 cbcontext.func = callback;
3222 cbcontext.context = context;
3224 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3225 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags,
3226 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3229 /*****************************************************************************
3230 * DirectDrawCreateClipper (DDRAW.@)
3232 * Creates a new IDirectDrawClipper object.
3234 * Params:
3235 * Clipper: Address to write the interface pointer to
3236 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3237 * NULL
3239 * Returns:
3240 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3241 * E_OUTOFMEMORY if allocating the object failed
3243 *****************************************************************************/
3244 HRESULT WINAPI DirectDrawCreateClipper(DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3246 struct ddraw_clipper *object;
3247 HRESULT hr;
3249 TRACE("flags %#x, clipper %p, outer_unknown %p.\n",
3250 flags, clipper, outer_unknown);
3252 if (outer_unknown)
3253 return CLASS_E_NOAGGREGATION;
3255 wined3d_mutex_lock();
3257 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3258 if (!object)
3260 wined3d_mutex_unlock();
3261 return E_OUTOFMEMORY;
3264 hr = ddraw_clipper_init(object);
3265 if (FAILED(hr))
3267 WARN("Failed to initialize clipper, hr %#x.\n", hr);
3268 HeapFree(GetProcessHeap(), 0, object);
3269 wined3d_mutex_unlock();
3270 return hr;
3273 TRACE("Created clipper %p.\n", object);
3274 *clipper = &object->IDirectDrawClipper_iface;
3275 wined3d_mutex_unlock();
3277 return DD_OK;
3280 /*****************************************************************************
3281 * IDirectDraw7::CreateClipper
3283 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3285 *****************************************************************************/
3286 static HRESULT WINAPI ddraw7_CreateClipper(IDirectDraw7 *iface, DWORD Flags,
3287 IDirectDrawClipper **Clipper, IUnknown *UnkOuter)
3289 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3290 iface, Flags, Clipper, UnkOuter);
3292 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3295 static HRESULT WINAPI ddraw4_CreateClipper(IDirectDraw4 *iface, DWORD flags,
3296 IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3298 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3300 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3301 iface, flags, clipper, outer_unknown);
3303 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3306 static HRESULT WINAPI ddraw2_CreateClipper(IDirectDraw2 *iface,
3307 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3309 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3311 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3312 iface, flags, clipper, outer_unknown);
3314 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3317 static HRESULT WINAPI ddraw1_CreateClipper(IDirectDraw *iface,
3318 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3320 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3322 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3323 iface, flags, clipper, outer_unknown);
3325 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3328 /*****************************************************************************
3329 * IDirectDraw7::CreatePalette
3331 * Creates a new IDirectDrawPalette object
3333 * Params:
3334 * Flags: The flags for the new clipper
3335 * ColorTable: Color table to assign to the new clipper
3336 * Palette: Address to write the interface pointer to
3337 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3338 * NULL
3340 * Returns:
3341 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3342 * E_OUTOFMEMORY if allocating the object failed
3344 *****************************************************************************/
3345 static HRESULT WINAPI ddraw7_CreatePalette(IDirectDraw7 *iface, DWORD Flags,
3346 PALETTEENTRY *ColorTable, IDirectDrawPalette **Palette, IUnknown *pUnkOuter)
3348 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
3349 struct ddraw_palette *object;
3350 HRESULT hr;
3352 TRACE("iface %p, flags %#x, color_table %p, palette %p, outer_unknown %p.\n",
3353 iface, Flags, ColorTable, Palette, pUnkOuter);
3355 if (pUnkOuter)
3356 return CLASS_E_NOAGGREGATION;
3358 wined3d_mutex_lock();
3360 /* The refcount test shows that a cooplevel is required for this */
3361 if (!ddraw->cooperative_level)
3363 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3364 wined3d_mutex_unlock();
3365 return DDERR_NOCOOPERATIVELEVELSET;
3368 object = HeapAlloc(GetProcessHeap(), 0, sizeof(*object));
3369 if (!object)
3371 ERR("Out of memory when allocating memory for a palette implementation\n");
3372 wined3d_mutex_unlock();
3373 return E_OUTOFMEMORY;
3376 hr = ddraw_palette_init(object, ddraw, Flags, ColorTable);
3377 if (FAILED(hr))
3379 WARN("Failed to initialize palette, hr %#x.\n", hr);
3380 HeapFree(GetProcessHeap(), 0, object);
3381 wined3d_mutex_unlock();
3382 return hr;
3385 TRACE("Created palette %p.\n", object);
3386 *Palette = &object->IDirectDrawPalette_iface;
3387 wined3d_mutex_unlock();
3389 return DD_OK;
3392 static HRESULT WINAPI ddraw4_CreatePalette(IDirectDraw4 *iface, DWORD flags, PALETTEENTRY *entries,
3393 IDirectDrawPalette **palette, IUnknown *outer_unknown)
3395 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3396 HRESULT hr;
3398 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3399 iface, flags, entries, palette, outer_unknown);
3401 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3402 if (SUCCEEDED(hr) && *palette)
3404 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3405 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3406 IDirectDraw4_AddRef(iface);
3407 impl->ifaceToRelease = (IUnknown *)iface;
3409 return hr;
3412 static HRESULT WINAPI ddraw2_CreatePalette(IDirectDraw2 *iface, DWORD flags,
3413 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3415 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3416 HRESULT hr;
3418 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3419 iface, flags, entries, palette, outer_unknown);
3421 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3422 if (SUCCEEDED(hr) && *palette)
3424 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3425 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3426 impl->ifaceToRelease = NULL;
3429 return hr;
3432 static HRESULT WINAPI ddraw1_CreatePalette(IDirectDraw *iface, DWORD flags,
3433 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3435 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3436 HRESULT hr;
3438 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3439 iface, flags, entries, palette, outer_unknown);
3441 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3442 if (SUCCEEDED(hr) && *palette)
3444 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3445 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3446 impl->ifaceToRelease = NULL;
3449 return hr;
3452 /*****************************************************************************
3453 * IDirectDraw7::DuplicateSurface
3455 * Duplicates a surface. The surface memory points to the same memory as
3456 * the original surface, and it's released when the last surface referencing
3457 * it is released. I guess that's beyond Wine's surface management right now
3458 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3459 * test application to implement this)
3461 * Params:
3462 * Src: Address of the source surface
3463 * Dest: Address to write the new surface pointer to
3465 * Returns:
3466 * See IDirectDraw7::CreateSurface
3468 *****************************************************************************/
3469 static HRESULT WINAPI ddraw7_DuplicateSurface(IDirectDraw7 *iface,
3470 IDirectDrawSurface7 *Src, IDirectDrawSurface7 **Dest)
3472 struct ddraw_surface *src_surface = unsafe_impl_from_IDirectDrawSurface7(Src);
3474 FIXME("iface %p, src %p, dst %p partial stub!\n", iface, Src, Dest);
3476 /* For now, simply create a new, independent surface */
3477 return IDirectDraw7_CreateSurface(iface, &src_surface->surface_desc, Dest, NULL);
3480 static HRESULT WINAPI ddraw4_DuplicateSurface(IDirectDraw4 *iface, IDirectDrawSurface4 *src,
3481 IDirectDrawSurface4 **dst)
3483 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src);
3484 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3485 struct ddraw_surface *dst_impl;
3486 IDirectDrawSurface7 *dst7;
3487 HRESULT hr;
3489 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3491 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3492 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3493 if (FAILED(hr))
3495 *dst = NULL;
3496 return hr;
3498 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3499 *dst = &dst_impl->IDirectDrawSurface4_iface;
3500 IDirectDrawSurface4_AddRef(*dst);
3501 IDirectDrawSurface7_Release(dst7);
3503 return hr;
3506 static HRESULT WINAPI ddraw2_DuplicateSurface(IDirectDraw2 *iface,
3507 IDirectDrawSurface *src, IDirectDrawSurface **dst)
3509 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3510 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3511 struct ddraw_surface *dst_impl;
3512 IDirectDrawSurface7 *dst7;
3513 HRESULT hr;
3515 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3517 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3518 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3519 if (FAILED(hr))
3520 return hr;
3521 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3522 *dst = &dst_impl->IDirectDrawSurface_iface;
3523 IDirectDrawSurface_AddRef(*dst);
3524 IDirectDrawSurface7_Release(dst7);
3526 return hr;
3529 static HRESULT WINAPI ddraw1_DuplicateSurface(IDirectDraw *iface, IDirectDrawSurface *src,
3530 IDirectDrawSurface **dst)
3532 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3533 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3534 struct ddraw_surface *dst_impl;
3535 IDirectDrawSurface7 *dst7;
3536 HRESULT hr;
3538 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3540 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3541 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3542 if (FAILED(hr))
3543 return hr;
3544 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3545 *dst = &dst_impl->IDirectDrawSurface_iface;
3546 IDirectDrawSurface_AddRef(*dst);
3547 IDirectDrawSurface7_Release(dst7);
3549 return hr;
3552 /*****************************************************************************
3553 * IDirect3D7::EnumDevices
3555 * The EnumDevices method for IDirect3D7. It enumerates all supported
3556 * D3D7 devices. Currently the T&L, HAL and RGB devices are enumerated.
3558 * Params:
3559 * callback: Function to call for each enumerated device
3560 * context: Pointer to pass back to the app
3562 * Returns:
3563 * D3D_OK, or the return value of the GetCaps call
3565 *****************************************************************************/
3566 static HRESULT WINAPI d3d7_EnumDevices(IDirect3D7 *iface, LPD3DENUMDEVICESCALLBACK7 callback, void *context)
3568 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
3569 D3DDEVICEDESC7 device_desc7;
3570 HRESULT hr;
3571 size_t i;
3573 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3575 if (!callback)
3576 return DDERR_INVALIDPARAMS;
3578 wined3d_mutex_lock();
3580 if (FAILED(hr = ddraw_get_d3dcaps(ddraw, &device_desc7)))
3582 wined3d_mutex_unlock();
3583 return hr;
3586 for (i = 0; i < sizeof(device_list7)/sizeof(device_list7[0]); i++)
3588 HRESULT ret;
3590 device_desc7.deviceGUID = *device_list7[i].device_guid;
3591 ret = callback(device_list7[i].interface_name, device_list7[i].device_name, &device_desc7, context);
3592 if (ret != DDENUMRET_OK)
3594 TRACE("Application cancelled the enumeration.\n");
3595 wined3d_mutex_unlock();
3596 return D3D_OK;
3600 TRACE("End of enumeration.\n");
3602 wined3d_mutex_unlock();
3604 return D3D_OK;
3607 /*****************************************************************************
3608 * IDirect3D3::EnumDevices
3610 * Enumerates all supported Direct3DDevice interfaces. This is the
3611 * implementation for Direct3D 1 to Direc3D 3, Version 7 has its own.
3613 * Version 1, 2 and 3
3615 * Params:
3616 * callback: Application-provided routine to call for each enumerated device
3617 * Context: Pointer to pass to the callback
3619 * Returns:
3620 * D3D_OK on success,
3621 * The result of IDirect3DImpl_GetCaps if it failed
3623 *****************************************************************************/
3624 static HRESULT WINAPI d3d3_EnumDevices(IDirect3D3 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3626 static CHAR wined3d_description[] = "Wine D3DDevice using WineD3D and OpenGL";
3628 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3629 D3DDEVICEDESC device_desc1, hal_desc, hel_desc;
3630 D3DDEVICEDESC7 device_desc7;
3631 HRESULT hr;
3633 /* Some games (Motoracer 2 demo) have the bad idea to modify the device
3634 * name string. Let's put the string in a sufficiently sized array in
3635 * writable memory. */
3636 char device_name[50];
3637 strcpy(device_name,"Direct3D HEL");
3639 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3641 if (!callback)
3642 return DDERR_INVALIDPARAMS;
3644 wined3d_mutex_lock();
3646 if (FAILED(hr = ddraw_get_d3dcaps(ddraw, &device_desc7)))
3648 wined3d_mutex_unlock();
3649 return hr;
3651 ddraw_d3dcaps1_from_7(&device_desc1, &device_desc7);
3653 /* Do I have to enumerate the reference id? Note from old d3d7:
3654 * "It seems that enumerating the reference IID on Direct3D 1 games
3655 * (AvP / Motoracer2) breaks them". So do not enumerate this iid in V1
3657 * There's a registry key HKLM\Software\Microsoft\Direct3D\Drivers,
3658 * EnumReference which enables / disables enumerating the reference
3659 * rasterizer. It's a DWORD, 0 means disabled, 2 means enabled. The
3660 * enablerefrast.reg and disablerefrast.reg files in the DirectX 7.0 sdk
3661 * demo directory suggest this.
3663 * Some games(GTA 2) seem to use the second enumerated device, so I have
3664 * to enumerate at least 2 devices. So enumerate the reference device to
3665 * have 2 devices.
3667 * Other games (Rollcage) tell emulation and hal device apart by certain
3668 * flags. Rollcage expects D3DPTEXTURECAPS_POW2 to be set (yeah, it is a
3669 * limitation flag), and it refuses all devices that have the perspective
3670 * flag set. This way it refuses the emulation device, and HAL devices
3671 * never have POW2 unset in d3d7 on windows. */
3672 if (ddraw->d3dversion != 1)
3674 static CHAR reference_description[] = "RGB Direct3D emulation";
3676 TRACE("Enumerating WineD3D D3DDevice interface.\n");
3677 hal_desc = device_desc1;
3678 hel_desc = device_desc1;
3679 /* The rgb device has the pow2 flag set in the hel caps, but not in the hal caps. */
3680 hal_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3681 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3682 hal_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3683 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3684 /* RGB, RAMP and MMX devices have a HAL dcmColorModel of 0 */
3685 hal_desc.dcmColorModel = 0;
3687 hr = callback((GUID *)&IID_IDirect3DRGBDevice, reference_description,
3688 device_name, &hal_desc, &hel_desc, context);
3689 if (hr != D3DENUMRET_OK)
3691 TRACE("Application cancelled the enumeration.\n");
3692 wined3d_mutex_unlock();
3693 return D3D_OK;
3697 strcpy(device_name,"Direct3D HAL");
3699 TRACE("Enumerating HAL Direct3D device.\n");
3700 hal_desc = device_desc1;
3701 hel_desc = device_desc1;
3703 /* The hal device does not have the pow2 flag set in hel, but in hal. */
3704 hel_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3705 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3706 hel_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3707 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3708 /* HAL devices have a HEL dcmColorModel of 0 */
3709 hel_desc.dcmColorModel = 0;
3711 hr = callback((GUID *)&IID_IDirect3DHALDevice, wined3d_description,
3712 device_name, &hal_desc, &hel_desc, context);
3713 if (hr != D3DENUMRET_OK)
3715 TRACE("Application cancelled the enumeration.\n");
3716 wined3d_mutex_unlock();
3717 return D3D_OK;
3720 TRACE("End of enumeration.\n");
3722 wined3d_mutex_unlock();
3724 return D3D_OK;
3727 static HRESULT WINAPI d3d2_EnumDevices(IDirect3D2 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3729 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3731 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3733 return d3d3_EnumDevices(&ddraw->IDirect3D3_iface, callback, context);
3736 static HRESULT WINAPI d3d1_EnumDevices(IDirect3D *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3738 struct ddraw *ddraw = impl_from_IDirect3D(iface);
3740 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3742 return d3d3_EnumDevices(&ddraw->IDirect3D3_iface, callback, context);
3745 /*****************************************************************************
3746 * IDirect3D3::CreateLight
3748 * Creates an IDirect3DLight interface. This interface is used in
3749 * Direct3D3 or earlier for lighting. In Direct3D7 it has been replaced
3750 * by the DIRECT3DLIGHT7 structure. Wine's Direct3DLight implementation
3751 * uses the IDirect3DDevice7 interface with D3D7 lights.
3753 * Version 1, 2 and 3
3755 * Params:
3756 * light: Address to store the new interface pointer
3757 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
3758 * Must be NULL
3760 * Returns:
3761 * D3D_OK on success
3762 * DDERR_OUTOFMEMORY if memory allocation failed
3763 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
3765 *****************************************************************************/
3766 static HRESULT WINAPI d3d3_CreateLight(IDirect3D3 *iface, IDirect3DLight **light,
3767 IUnknown *outer_unknown)
3769 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3770 struct d3d_light *object;
3772 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3774 if (outer_unknown) return CLASS_E_NOAGGREGATION;
3776 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3777 if (!object)
3779 ERR("Failed to allocate light memory.\n");
3780 return DDERR_OUTOFMEMORY;
3783 d3d_light_init(object, ddraw);
3785 TRACE("Created light %p.\n", object);
3786 *light = &object->IDirect3DLight_iface;
3788 return D3D_OK;
3791 static HRESULT WINAPI d3d2_CreateLight(IDirect3D2 *iface, IDirect3DLight **light, IUnknown *outer_unknown)
3793 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3795 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3797 return d3d3_CreateLight(&ddraw->IDirect3D3_iface, light, outer_unknown);
3800 static HRESULT WINAPI d3d1_CreateLight(IDirect3D *iface, IDirect3DLight **light, IUnknown *outer_unknown)
3802 struct ddraw *ddraw = impl_from_IDirect3D(iface);
3804 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3806 return d3d3_CreateLight(&ddraw->IDirect3D3_iface, light, outer_unknown);
3809 /*****************************************************************************
3810 * IDirect3D3::CreateMaterial
3812 * Creates an IDirect3DMaterial interface. This interface is used by Direct3D3
3813 * and older versions. The IDirect3DMaterial implementation wraps its
3814 * functionality to IDirect3DDevice7::SetMaterial and friends.
3816 * Version 1, 2 and 3
3818 * Params:
3819 * material: Address to store the new interface's pointer to
3820 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
3821 * Must be NULL
3823 * Returns:
3824 * D3D_OK on success
3825 * DDERR_OUTOFMEMORY if memory allocation failed
3826 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
3828 *****************************************************************************/
3829 static HRESULT WINAPI d3d3_CreateMaterial(IDirect3D3 *iface, IDirect3DMaterial3 **material,
3830 IUnknown *outer_unknown)
3832 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3833 struct d3d_material *object;
3835 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
3837 if (outer_unknown) return CLASS_E_NOAGGREGATION;
3839 object = d3d_material_create(ddraw);
3840 if (!object)
3842 ERR("Failed to allocate material memory.\n");
3843 return DDERR_OUTOFMEMORY;
3846 TRACE("Created material %p.\n", object);
3847 *material = &object->IDirect3DMaterial3_iface;
3849 return D3D_OK;
3852 static HRESULT WINAPI d3d2_CreateMaterial(IDirect3D2 *iface, IDirect3DMaterial2 **material,
3853 IUnknown *outer_unknown)
3855 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3856 struct d3d_material *object;
3858 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
3860 object = d3d_material_create(ddraw);
3861 if (!object)
3863 ERR("Failed to allocate material memory.\n");
3864 return DDERR_OUTOFMEMORY;
3867 TRACE("Created material %p.\n", object);
3868 *material = &object->IDirect3DMaterial2_iface;
3870 return D3D_OK;
3873 static HRESULT WINAPI d3d1_CreateMaterial(IDirect3D *iface, IDirect3DMaterial **material,
3874 IUnknown *outer_unknown)
3876 struct ddraw *ddraw = impl_from_IDirect3D(iface);
3877 struct d3d_material *object;
3879 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
3881 object = d3d_material_create(ddraw);
3882 if (!object)
3884 ERR("Failed to allocate material memory.\n");
3885 return DDERR_OUTOFMEMORY;
3888 TRACE("Created material %p.\n", object);
3889 *material = &object->IDirect3DMaterial_iface;
3891 return D3D_OK;
3894 /*****************************************************************************
3895 * IDirect3D3::CreateViewport
3897 * Creates an IDirect3DViewport interface. This interface is used
3898 * by Direct3D and earlier versions for Viewport management. In Direct3D7
3899 * it has been replaced by a viewport structure and
3900 * IDirect3DDevice7::*Viewport. Wine's IDirect3DViewport implementation
3901 * uses the IDirect3DDevice7 methods for its functionality
3903 * Params:
3904 * Viewport: Address to store the new interface pointer
3905 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
3906 * Must be NULL
3908 * Returns:
3909 * D3D_OK on success
3910 * DDERR_OUTOFMEMORY if memory allocation failed
3911 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
3913 *****************************************************************************/
3914 static HRESULT WINAPI d3d3_CreateViewport(IDirect3D3 *iface, IDirect3DViewport3 **viewport,
3915 IUnknown *outer_unknown)
3917 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3918 struct d3d_viewport *object;
3920 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
3922 if (outer_unknown) return CLASS_E_NOAGGREGATION;
3924 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3925 if (!object)
3927 ERR("Failed to allocate viewport memory.\n");
3928 return DDERR_OUTOFMEMORY;
3931 d3d_viewport_init(object, ddraw);
3933 TRACE("Created viewport %p.\n", object);
3934 *viewport = &object->IDirect3DViewport3_iface;
3936 return D3D_OK;
3939 static HRESULT WINAPI d3d2_CreateViewport(IDirect3D2 *iface, IDirect3DViewport2 **viewport, IUnknown *outer_unknown)
3941 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3943 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
3945 return d3d3_CreateViewport(&ddraw->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
3946 outer_unknown);
3949 static HRESULT WINAPI d3d1_CreateViewport(IDirect3D *iface, IDirect3DViewport **viewport, IUnknown *outer_unknown)
3951 struct ddraw *ddraw = impl_from_IDirect3D(iface);
3953 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
3955 return d3d3_CreateViewport(&ddraw->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
3956 outer_unknown);
3959 /*****************************************************************************
3960 * IDirect3D3::FindDevice
3962 * This method finds a device with the requested properties and returns a
3963 * device description
3965 * Verion 1, 2 and 3
3966 * Params:
3967 * fds: Describes the requested device characteristics
3968 * fdr: Returns the device description
3970 * Returns:
3971 * D3D_OK on success
3972 * DDERR_INVALIDPARAMS if no device was found
3974 *****************************************************************************/
3975 static HRESULT WINAPI d3d3_FindDevice(IDirect3D3 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
3977 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3978 D3DDEVICEDESC7 desc7;
3979 D3DDEVICEDESC desc1;
3980 HRESULT hr;
3982 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
3984 if (!fds || !fdr) return DDERR_INVALIDPARAMS;
3986 if (fds->dwSize != sizeof(D3DFINDDEVICESEARCH)
3987 || fdr->dwSize != sizeof(D3DFINDDEVICERESULT))
3988 return DDERR_INVALIDPARAMS;
3990 if ((fds->dwFlags & D3DFDS_COLORMODEL)
3991 && fds->dcmColorModel != D3DCOLOR_RGB)
3993 WARN("Trying to request a non-RGB D3D color model. Not supported.\n");
3994 return DDERR_INVALIDPARAMS; /* No real idea what to return here :-) */
3997 if (fds->dwFlags & D3DFDS_GUID)
3999 TRACE("Trying to match guid %s.\n", debugstr_guid(&(fds->guid)));
4000 if (!IsEqualGUID(&IID_D3DDEVICE_WineD3D, &fds->guid)
4001 && !IsEqualGUID(&IID_IDirect3DHALDevice, &fds->guid)
4002 && !IsEqualGUID(&IID_IDirect3DRGBDevice, &fds->guid))
4004 WARN("No match for this GUID.\n");
4005 return DDERR_NOTFOUND;
4009 /* Get the caps */
4010 if (FAILED(hr = ddraw_get_d3dcaps(ddraw, &desc7)))
4011 return hr;
4013 /* Now return our own GUID */
4014 ddraw_d3dcaps1_from_7(&desc1, &desc7);
4015 fdr->guid = IID_D3DDEVICE_WineD3D;
4016 fdr->ddHwDesc = desc1;
4017 fdr->ddSwDesc = desc1;
4019 TRACE("Returning Wine's wined3d device with (undumped) capabilities.\n");
4021 return D3D_OK;
4024 static HRESULT WINAPI d3d2_FindDevice(IDirect3D2 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4026 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4028 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4030 return d3d3_FindDevice(&ddraw->IDirect3D3_iface, fds, fdr);
4033 static HRESULT WINAPI d3d1_FindDevice(IDirect3D *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4035 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4037 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4039 return d3d3_FindDevice(&ddraw->IDirect3D3_iface, fds, fdr);
4042 /*****************************************************************************
4043 * IDirect3D7::CreateDevice
4045 * Creates an IDirect3DDevice7 interface.
4047 * Version 2, 3 and 7. IDirect3DDevice 1 interfaces are interfaces to
4048 * DirectDraw surfaces and are created with
4049 * IDirectDrawSurface::QueryInterface. This method uses CreateDevice to
4050 * create the device object and QueryInterfaces for IDirect3DDevice
4052 * Params:
4053 * refiid: IID of the device to create
4054 * Surface: Initial rendertarget
4055 * Device: Address to return the interface pointer
4057 * Returns:
4058 * D3D_OK on success
4059 * DDERR_OUTOFMEMORY if memory allocation failed
4060 * DDERR_INVALIDPARAMS if a device exists already
4062 *****************************************************************************/
4063 static HRESULT WINAPI d3d7_CreateDevice(IDirect3D7 *iface, REFCLSID riid,
4064 IDirectDrawSurface7 *surface, IDirect3DDevice7 **device)
4066 struct ddraw_surface *target = unsafe_impl_from_IDirectDrawSurface7(surface);
4067 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4068 struct d3d_device *object;
4069 HRESULT hr;
4071 TRACE("iface %p, riid %s, surface %p, device %p.\n", iface, debugstr_guid(riid), surface, device);
4073 wined3d_mutex_lock();
4074 if (SUCCEEDED(hr = d3d_device_create(ddraw, target, (IUnknown *)surface, 7, &object, NULL)))
4076 *device = &object->IDirect3DDevice7_iface;
4078 else
4080 WARN("Failed to create device, hr %#x.\n", hr);
4081 *device = NULL;
4083 wined3d_mutex_unlock();
4085 return hr;
4088 static HRESULT WINAPI d3d3_CreateDevice(IDirect3D3 *iface, REFCLSID riid,
4089 IDirectDrawSurface4 *surface, IDirect3DDevice3 **device, IUnknown *outer_unknown)
4091 struct ddraw_surface *surface_impl = unsafe_impl_from_IDirectDrawSurface4(surface);
4092 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4093 struct d3d_device *device_impl;
4094 HRESULT hr;
4096 TRACE("iface %p, riid %s, surface %p, device %p, outer_unknown %p.\n",
4097 iface, debugstr_guid(riid), surface, device, outer_unknown);
4099 if (outer_unknown)
4100 return CLASS_E_NOAGGREGATION;
4102 wined3d_mutex_lock();
4103 if (SUCCEEDED(hr = d3d_device_create(ddraw, surface_impl, (IUnknown *)surface, 3, &device_impl, NULL)))
4105 *device = &device_impl->IDirect3DDevice3_iface;
4107 else
4109 WARN("Failed to create device, hr %#x.\n", hr);
4110 *device = NULL;
4112 wined3d_mutex_unlock();
4114 return hr;
4117 static HRESULT WINAPI d3d2_CreateDevice(IDirect3D2 *iface, REFCLSID riid,
4118 IDirectDrawSurface *surface, IDirect3DDevice2 **device)
4120 struct ddraw_surface *surface_impl = unsafe_impl_from_IDirectDrawSurface(surface);
4121 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4122 struct d3d_device *device_impl;
4123 HRESULT hr;
4125 TRACE("iface %p, riid %s, surface %p, device %p.\n",
4126 iface, debugstr_guid(riid), surface, device);
4128 wined3d_mutex_lock();
4129 if (SUCCEEDED(hr = d3d_device_create(ddraw, surface_impl, (IUnknown *)surface, 2, &device_impl, NULL)))
4131 *device = &device_impl->IDirect3DDevice2_iface;
4133 else
4135 WARN("Failed to create device, hr %#x.\n", hr);
4136 *device = NULL;
4138 wined3d_mutex_unlock();
4140 return hr;
4143 /*****************************************************************************
4144 * IDirect3D7::CreateVertexBuffer
4146 * Creates a new vertex buffer object and returns a IDirect3DVertexBuffer7
4147 * interface.
4149 * Version 3 and 7
4151 * Params:
4152 * desc: Requested Vertex buffer properties
4153 * vertex_buffer: Address to return the interface pointer at
4154 * flags: Some flags, should be 0
4156 * Returns
4157 * D3D_OK on success
4158 * DDERR_OUTOFMEMORY if memory allocation failed
4159 * The return value of IWineD3DDevice::CreateVertexBuffer if this call fails
4160 * DDERR_INVALIDPARAMS if desc or vertex_buffer is NULL
4162 *****************************************************************************/
4163 static HRESULT WINAPI d3d7_CreateVertexBuffer(IDirect3D7 *iface, D3DVERTEXBUFFERDESC *desc,
4164 IDirect3DVertexBuffer7 **vertex_buffer, DWORD flags)
4166 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4167 struct d3d_vertex_buffer *object;
4168 HRESULT hr;
4170 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x.\n",
4171 iface, desc, vertex_buffer, flags);
4173 if (!vertex_buffer || !desc) return DDERR_INVALIDPARAMS;
4175 hr = d3d_vertex_buffer_create(&object, ddraw, desc);
4176 if (hr == D3D_OK)
4178 TRACE("Created vertex buffer %p.\n", object);
4179 *vertex_buffer = &object->IDirect3DVertexBuffer7_iface;
4181 else
4182 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4184 return hr;
4187 static HRESULT WINAPI d3d3_CreateVertexBuffer(IDirect3D3 *iface, D3DVERTEXBUFFERDESC *desc,
4188 IDirect3DVertexBuffer **vertex_buffer, DWORD flags, IUnknown *outer_unknown)
4190 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4191 struct d3d_vertex_buffer *object;
4192 HRESULT hr;
4194 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x, outer_unknown %p.\n",
4195 iface, desc, vertex_buffer, flags, outer_unknown);
4197 if (outer_unknown)
4198 return CLASS_E_NOAGGREGATION;
4199 if (!vertex_buffer || !desc)
4200 return DDERR_INVALIDPARAMS;
4202 hr = d3d_vertex_buffer_create(&object, ddraw, desc);
4203 if (hr == D3D_OK)
4205 TRACE("Created vertex buffer %p.\n", object);
4206 *vertex_buffer = &object->IDirect3DVertexBuffer_iface;
4208 else
4209 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4211 return hr;
4214 /*****************************************************************************
4215 * IDirect3D7::EnumZBufferFormats
4217 * Enumerates all supported Z buffer pixel formats
4219 * Version 3 and 7
4221 * Params:
4222 * device_iid:
4223 * callback: callback to call for each pixel format
4224 * context: Pointer to pass back to the callback
4226 * Returns:
4227 * D3D_OK on success
4228 * DDERR_INVALIDPARAMS if callback is NULL
4229 * For details, see IWineD3DDevice::EnumZBufferFormats
4231 *****************************************************************************/
4232 static HRESULT WINAPI d3d7_EnumZBufferFormats(IDirect3D7 *iface, REFCLSID device_iid,
4233 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4235 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4236 struct wined3d_display_mode mode;
4237 enum wined3d_device_type type;
4238 unsigned int i;
4239 HRESULT hr;
4241 /* Order matters. Specifically, BattleZone II (full version) expects the
4242 * 16-bit depth formats to be listed before the 24 and 32 ones. */
4243 static const enum wined3d_format_id formats[] =
4245 WINED3DFMT_S1_UINT_D15_UNORM,
4246 WINED3DFMT_D16_UNORM,
4247 WINED3DFMT_X8D24_UNORM,
4248 WINED3DFMT_S4X4_UINT_D24_UNORM,
4249 WINED3DFMT_D24_UNORM_S8_UINT,
4250 WINED3DFMT_D32_UNORM,
4253 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4254 iface, debugstr_guid(device_iid), callback, context);
4256 if (!callback) return DDERR_INVALIDPARAMS;
4258 if (IsEqualGUID(device_iid, &IID_IDirect3DHALDevice)
4259 || IsEqualGUID(device_iid, &IID_IDirect3DTnLHalDevice)
4260 || IsEqualGUID(device_iid, &IID_D3DDEVICE_WineD3D))
4262 TRACE("Asked for HAL device.\n");
4263 type = WINED3D_DEVICE_TYPE_HAL;
4265 else if (IsEqualGUID(device_iid, &IID_IDirect3DRGBDevice)
4266 || IsEqualGUID(device_iid, &IID_IDirect3DMMXDevice))
4268 TRACE("Asked for SW device.\n");
4269 type = WINED3D_DEVICE_TYPE_SW;
4271 else if (IsEqualGUID(device_iid, &IID_IDirect3DRefDevice))
4273 TRACE("Asked for REF device.\n");
4274 type = WINED3D_DEVICE_TYPE_REF;
4276 else if (IsEqualGUID(device_iid, &IID_IDirect3DNullDevice))
4278 TRACE("Asked for NULLREF device.\n");
4279 type = WINED3D_DEVICE_TYPE_NULLREF;
4281 else
4283 FIXME("Unexpected device GUID %s.\n", debugstr_guid(device_iid));
4284 type = WINED3D_DEVICE_TYPE_HAL;
4287 wined3d_mutex_lock();
4288 /* We need an adapter format from somewhere to please wined3d and WGL.
4289 * Use the current display mode. So far all cards offer the same depth
4290 * stencil format for all modes, but if some do not and applications do
4291 * not like that we'll have to find some workaround, like iterating over
4292 * all imaginable formats and collecting all the depth stencil formats we
4293 * can get. */
4294 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
4296 ERR("Failed to get display mode, hr %#x.\n", hr);
4297 wined3d_mutex_unlock();
4298 return hr;
4301 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); ++i)
4303 if (SUCCEEDED(wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, type, mode.format_id,
4304 WINED3DUSAGE_DEPTHSTENCIL, WINED3D_RTYPE_SURFACE, formats[i])))
4306 DDPIXELFORMAT pformat;
4308 memset(&pformat, 0, sizeof(pformat));
4309 pformat.dwSize = sizeof(pformat);
4310 ddrawformat_from_wined3dformat(&pformat, formats[i]);
4312 TRACE("Enumerating wined3d format %#x.\n", formats[i]);
4313 hr = callback(&pformat, context);
4314 if (hr != DDENUMRET_OK)
4316 TRACE("Format enumeration cancelled by application.\n");
4317 wined3d_mutex_unlock();
4318 return D3D_OK;
4323 /* Historically some windows drivers used dwZBufferBitDepth=24 for WINED3DFMT_X8D24_UNORM,
4324 * while others used dwZBufferBitDepth=32. In either case the pitch matches a 32 bits per
4325 * pixel format, so we use dwZBufferBitDepth=32. Some games expect 24. Windows Vista and
4326 * newer enumerate both versions, so we do the same(bug 22434) */
4327 if (SUCCEEDED(wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, type, mode.format_id,
4328 WINED3DUSAGE_DEPTHSTENCIL, WINED3D_RTYPE_SURFACE, WINED3DFMT_X8D24_UNORM)))
4330 DDPIXELFORMAT x8d24 =
4332 sizeof(x8d24), DDPF_ZBUFFER, 0,
4333 {24}, {0x00000000}, {0x00ffffff}, {0x00000000}
4335 TRACE("Enumerating WINED3DFMT_X8D24_UNORM, dwZBufferBitDepth=24 version\n");
4336 callback(&x8d24, context);
4339 TRACE("End of enumeration.\n");
4341 wined3d_mutex_unlock();
4343 return D3D_OK;
4346 static HRESULT WINAPI d3d3_EnumZBufferFormats(IDirect3D3 *iface, REFCLSID device_iid,
4347 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4349 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4351 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4352 iface, debugstr_guid(device_iid), callback, context);
4354 return d3d7_EnumZBufferFormats(&ddraw->IDirect3D7_iface, device_iid, callback, context);
4357 /*****************************************************************************
4358 * IDirect3D7::EvictManagedTextures
4360 * Removes all managed textures (=surfaces with DDSCAPS2_TEXTUREMANAGE or
4361 * DDSCAPS2_D3DTEXTUREMANAGE caps) to be removed from video memory.
4363 * Version 3 and 7
4365 * Returns:
4366 * D3D_OK, because it's a stub
4368 *****************************************************************************/
4369 static HRESULT WINAPI d3d7_EvictManagedTextures(IDirect3D7 *iface)
4371 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4373 TRACE("iface %p!\n", iface);
4375 wined3d_mutex_lock();
4376 if (ddraw->flags & DDRAW_D3D_INITIALIZED)
4377 wined3d_device_evict_managed_resources(ddraw->wined3d_device);
4378 wined3d_mutex_unlock();
4380 return D3D_OK;
4383 static HRESULT WINAPI d3d3_EvictManagedTextures(IDirect3D3 *iface)
4385 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4387 TRACE("iface %p.\n", iface);
4389 return d3d7_EvictManagedTextures(&ddraw->IDirect3D7_iface);
4392 /*****************************************************************************
4393 * IDirectDraw7 VTable
4394 *****************************************************************************/
4395 static const struct IDirectDraw7Vtbl ddraw7_vtbl =
4397 /* IUnknown */
4398 ddraw7_QueryInterface,
4399 ddraw7_AddRef,
4400 ddraw7_Release,
4401 /* IDirectDraw */
4402 ddraw7_Compact,
4403 ddraw7_CreateClipper,
4404 ddraw7_CreatePalette,
4405 ddraw7_CreateSurface,
4406 ddraw7_DuplicateSurface,
4407 ddraw7_EnumDisplayModes,
4408 ddraw7_EnumSurfaces,
4409 ddraw7_FlipToGDISurface,
4410 ddraw7_GetCaps,
4411 ddraw7_GetDisplayMode,
4412 ddraw7_GetFourCCCodes,
4413 ddraw7_GetGDISurface,
4414 ddraw7_GetMonitorFrequency,
4415 ddraw7_GetScanLine,
4416 ddraw7_GetVerticalBlankStatus,
4417 ddraw7_Initialize,
4418 ddraw7_RestoreDisplayMode,
4419 ddraw7_SetCooperativeLevel,
4420 ddraw7_SetDisplayMode,
4421 ddraw7_WaitForVerticalBlank,
4422 /* IDirectDraw2 */
4423 ddraw7_GetAvailableVidMem,
4424 /* IDirectDraw3 */
4425 ddraw7_GetSurfaceFromDC,
4426 /* IDirectDraw4 */
4427 ddraw7_RestoreAllSurfaces,
4428 ddraw7_TestCooperativeLevel,
4429 ddraw7_GetDeviceIdentifier,
4430 /* IDirectDraw7 */
4431 ddraw7_StartModeTest,
4432 ddraw7_EvaluateMode
4435 static const struct IDirectDraw4Vtbl ddraw4_vtbl =
4437 /* IUnknown */
4438 ddraw4_QueryInterface,
4439 ddraw4_AddRef,
4440 ddraw4_Release,
4441 /* IDirectDraw */
4442 ddraw4_Compact,
4443 ddraw4_CreateClipper,
4444 ddraw4_CreatePalette,
4445 ddraw4_CreateSurface,
4446 ddraw4_DuplicateSurface,
4447 ddraw4_EnumDisplayModes,
4448 ddraw4_EnumSurfaces,
4449 ddraw4_FlipToGDISurface,
4450 ddraw4_GetCaps,
4451 ddraw4_GetDisplayMode,
4452 ddraw4_GetFourCCCodes,
4453 ddraw4_GetGDISurface,
4454 ddraw4_GetMonitorFrequency,
4455 ddraw4_GetScanLine,
4456 ddraw4_GetVerticalBlankStatus,
4457 ddraw4_Initialize,
4458 ddraw4_RestoreDisplayMode,
4459 ddraw4_SetCooperativeLevel,
4460 ddraw4_SetDisplayMode,
4461 ddraw4_WaitForVerticalBlank,
4462 /* IDirectDraw2 */
4463 ddraw4_GetAvailableVidMem,
4464 /* IDirectDraw3 */
4465 ddraw4_GetSurfaceFromDC,
4466 /* IDirectDraw4 */
4467 ddraw4_RestoreAllSurfaces,
4468 ddraw4_TestCooperativeLevel,
4469 ddraw4_GetDeviceIdentifier,
4472 static const struct IDirectDraw2Vtbl ddraw2_vtbl =
4474 /* IUnknown */
4475 ddraw2_QueryInterface,
4476 ddraw2_AddRef,
4477 ddraw2_Release,
4478 /* IDirectDraw */
4479 ddraw2_Compact,
4480 ddraw2_CreateClipper,
4481 ddraw2_CreatePalette,
4482 ddraw2_CreateSurface,
4483 ddraw2_DuplicateSurface,
4484 ddraw2_EnumDisplayModes,
4485 ddraw2_EnumSurfaces,
4486 ddraw2_FlipToGDISurface,
4487 ddraw2_GetCaps,
4488 ddraw2_GetDisplayMode,
4489 ddraw2_GetFourCCCodes,
4490 ddraw2_GetGDISurface,
4491 ddraw2_GetMonitorFrequency,
4492 ddraw2_GetScanLine,
4493 ddraw2_GetVerticalBlankStatus,
4494 ddraw2_Initialize,
4495 ddraw2_RestoreDisplayMode,
4496 ddraw2_SetCooperativeLevel,
4497 ddraw2_SetDisplayMode,
4498 ddraw2_WaitForVerticalBlank,
4499 /* IDirectDraw2 */
4500 ddraw2_GetAvailableVidMem,
4503 static const struct IDirectDrawVtbl ddraw1_vtbl =
4505 /* IUnknown */
4506 ddraw1_QueryInterface,
4507 ddraw1_AddRef,
4508 ddraw1_Release,
4509 /* IDirectDraw */
4510 ddraw1_Compact,
4511 ddraw1_CreateClipper,
4512 ddraw1_CreatePalette,
4513 ddraw1_CreateSurface,
4514 ddraw1_DuplicateSurface,
4515 ddraw1_EnumDisplayModes,
4516 ddraw1_EnumSurfaces,
4517 ddraw1_FlipToGDISurface,
4518 ddraw1_GetCaps,
4519 ddraw1_GetDisplayMode,
4520 ddraw1_GetFourCCCodes,
4521 ddraw1_GetGDISurface,
4522 ddraw1_GetMonitorFrequency,
4523 ddraw1_GetScanLine,
4524 ddraw1_GetVerticalBlankStatus,
4525 ddraw1_Initialize,
4526 ddraw1_RestoreDisplayMode,
4527 ddraw1_SetCooperativeLevel,
4528 ddraw1_SetDisplayMode,
4529 ddraw1_WaitForVerticalBlank,
4532 static const struct IDirect3D7Vtbl d3d7_vtbl =
4534 /* IUnknown methods */
4535 d3d7_QueryInterface,
4536 d3d7_AddRef,
4537 d3d7_Release,
4538 /* IDirect3D7 methods */
4539 d3d7_EnumDevices,
4540 d3d7_CreateDevice,
4541 d3d7_CreateVertexBuffer,
4542 d3d7_EnumZBufferFormats,
4543 d3d7_EvictManagedTextures
4546 static const struct IDirect3D3Vtbl d3d3_vtbl =
4548 /* IUnknown methods */
4549 d3d3_QueryInterface,
4550 d3d3_AddRef,
4551 d3d3_Release,
4552 /* IDirect3D3 methods */
4553 d3d3_EnumDevices,
4554 d3d3_CreateLight,
4555 d3d3_CreateMaterial,
4556 d3d3_CreateViewport,
4557 d3d3_FindDevice,
4558 d3d3_CreateDevice,
4559 d3d3_CreateVertexBuffer,
4560 d3d3_EnumZBufferFormats,
4561 d3d3_EvictManagedTextures
4564 static const struct IDirect3D2Vtbl d3d2_vtbl =
4566 /* IUnknown methods */
4567 d3d2_QueryInterface,
4568 d3d2_AddRef,
4569 d3d2_Release,
4570 /* IDirect3D2 methods */
4571 d3d2_EnumDevices,
4572 d3d2_CreateLight,
4573 d3d2_CreateMaterial,
4574 d3d2_CreateViewport,
4575 d3d2_FindDevice,
4576 d3d2_CreateDevice
4579 static const struct IDirect3DVtbl d3d1_vtbl =
4581 /* IUnknown methods */
4582 d3d1_QueryInterface,
4583 d3d1_AddRef,
4584 d3d1_Release,
4585 /* IDirect3D methods */
4586 d3d1_Initialize,
4587 d3d1_EnumDevices,
4588 d3d1_CreateLight,
4589 d3d1_CreateMaterial,
4590 d3d1_CreateViewport,
4591 d3d1_FindDevice
4594 /*****************************************************************************
4595 * ddraw_find_decl
4597 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
4598 * if none was found.
4600 * This function is in ddraw.c and the DDraw object space because D3D7
4601 * vertex buffers are created using the IDirect3D interface to the ddraw
4602 * object, so they can be valid across D3D devices(theoretically. The ddraw
4603 * object also owns the wined3d device
4605 * Parameters:
4606 * This: Device
4607 * fvf: Fvf to find the decl for
4609 * Returns:
4610 * NULL in case of an error, the vertex declaration for the FVF otherwise.
4612 *****************************************************************************/
4613 struct wined3d_vertex_declaration *ddraw_find_decl(struct ddraw *This, DWORD fvf)
4615 struct wined3d_vertex_declaration *pDecl = NULL;
4616 HRESULT hr;
4617 int p, low, high; /* deliberately signed */
4618 struct FvfToDecl *convertedDecls = This->decls;
4620 TRACE("Searching for declaration for fvf %08x... ", fvf);
4622 low = 0;
4623 high = This->numConvertedDecls - 1;
4624 while(low <= high) {
4625 p = (low + high) >> 1;
4626 TRACE("%d ", p);
4627 if(convertedDecls[p].fvf == fvf) {
4628 TRACE("found %p\n", convertedDecls[p].decl);
4629 return convertedDecls[p].decl;
4630 } else if(convertedDecls[p].fvf < fvf) {
4631 low = p + 1;
4632 } else {
4633 high = p - 1;
4636 TRACE("not found. Creating and inserting at position %d.\n", low);
4638 hr = wined3d_vertex_declaration_create_from_fvf(This->wined3d_device,
4639 fvf, This, &ddraw_null_wined3d_parent_ops, &pDecl);
4640 if (hr != S_OK) return NULL;
4642 if(This->declArraySize == This->numConvertedDecls) {
4643 int grow = max(This->declArraySize / 2, 8);
4644 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
4645 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
4646 if (!convertedDecls)
4648 wined3d_vertex_declaration_decref(pDecl);
4649 return NULL;
4651 This->decls = convertedDecls;
4652 This->declArraySize += grow;
4655 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
4656 convertedDecls[low].decl = pDecl;
4657 convertedDecls[low].fvf = fvf;
4658 This->numConvertedDecls++;
4660 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
4661 return pDecl;
4664 static inline struct ddraw *ddraw_from_device_parent(struct wined3d_device_parent *device_parent)
4666 return CONTAINING_RECORD(device_parent, struct ddraw, device_parent);
4669 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
4670 struct wined3d_device *device)
4672 TRACE("device_parent %p, device %p.\n", device_parent, device);
4675 /* This is run from device_process_message() in wined3d, we can't take the
4676 * wined3d mutex. */
4677 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
4679 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4680 MONITORINFO monitor_info;
4681 HMONITOR monitor;
4682 RECT *r;
4684 TRACE("device_parent %p.\n", device_parent);
4686 if (!(ddraw->cooperative_level & DDSCL_EXCLUSIVE) || !ddraw->swapchain_window)
4688 TRACE("Nothing to resize.\n");
4689 return;
4692 monitor = MonitorFromWindow(ddraw->swapchain_window, MONITOR_DEFAULTTOPRIMARY);
4693 monitor_info.cbSize = sizeof(monitor_info);
4694 if (!GetMonitorInfoW(monitor, &monitor_info))
4696 ERR("Failed to get monitor info.\n");
4697 return;
4700 r = &monitor_info.rcMonitor;
4701 TRACE("Resizing window %p to %s.\n", ddraw->swapchain_window, wine_dbgstr_rect(r));
4703 if (!SetWindowPos(ddraw->swapchain_window, HWND_TOP, r->left, r->top,
4704 r->right - r->left, r->bottom - r->top, SWP_SHOWWINDOW | SWP_NOACTIVATE))
4705 ERR("Failed to resize window.\n");
4708 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
4710 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4712 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
4714 if (!activate)
4715 InterlockedCompareExchange(&ddraw->device_state, DDRAW_DEVICE_STATE_LOST, DDRAW_DEVICE_STATE_OK);
4716 else
4717 InterlockedCompareExchange(&ddraw->device_state, DDRAW_DEVICE_STATE_OK, DDRAW_DEVICE_STATE_LOST);
4720 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
4721 void *container_parent, struct wined3d_surface *surface,
4722 void **parent, const struct wined3d_parent_ops **parent_ops)
4724 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4725 struct ddraw_surface *ddraw_surface;
4726 HRESULT hr;
4728 TRACE("device_parent %p, container_parent %p, surface %p, parent %p, parent_ops %p.\n",
4729 device_parent, container_parent, surface, parent, parent_ops);
4731 /* We have a swapchain or wined3d internal texture. */
4732 if (!container_parent || container_parent == ddraw)
4734 *parent = NULL;
4735 *parent_ops = &ddraw_null_wined3d_parent_ops;
4737 return DD_OK;
4740 if (!(ddraw_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw_surface))))
4742 ERR("Failed to allocate surface memory.\n");
4743 return DDERR_OUTOFVIDEOMEMORY;
4746 if (FAILED(hr = ddraw_surface_init(ddraw_surface, ddraw, container_parent, surface, parent_ops)))
4748 WARN("Failed to initialize surface, hr %#x.\n", hr);
4749 HeapFree(GetProcessHeap(), 0, ddraw_surface);
4750 return hr;
4753 *parent = ddraw_surface;
4754 list_add_head(&ddraw->surface_list, &ddraw_surface->surface_list_entry);
4756 TRACE("Created ddraw surface %p.\n", ddraw_surface);
4758 return DD_OK;
4761 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
4762 void *container_parent, struct wined3d_volume *volume,
4763 void **parent, const struct wined3d_parent_ops **parent_ops)
4765 TRACE("device_parent %p, container_parent %p, volume %p, parent %p, parent_ops %p.\n",
4766 device_parent, container_parent, volume, parent, parent_ops);
4768 *parent = NULL;
4769 *parent_ops = &ddraw_null_wined3d_parent_ops;
4771 return DD_OK;
4774 static void STDMETHODCALLTYPE ddraw_frontbuffer_destroyed(void *parent)
4776 struct ddraw *ddraw = parent;
4777 ddraw->wined3d_frontbuffer = NULL;
4780 static const struct wined3d_parent_ops ddraw_frontbuffer_parent_ops =
4782 ddraw_frontbuffer_destroyed,
4785 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
4786 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_surface **surface)
4788 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4789 struct wined3d_resource_desc texture_desc;
4790 struct wined3d_texture *texture;
4791 HRESULT hr;
4793 TRACE("device_parent %p, container_parent %p, desc %p, surface %p.\n",
4794 device_parent, container_parent, desc, surface);
4796 if (ddraw->wined3d_frontbuffer)
4798 ERR("Frontbuffer already created.\n");
4799 return E_FAIL;
4802 texture_desc = *desc;
4803 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE;
4804 if (FAILED(hr = wined3d_texture_create(ddraw->wined3d_device, &texture_desc, 1,
4805 WINED3D_SURFACE_MAPPABLE, ddraw, &ddraw_frontbuffer_parent_ops, &texture)))
4807 WARN("Failed to create texture, hr %#x.\n", hr);
4808 return hr;
4811 *surface = wined3d_surface_from_resource(wined3d_texture_get_sub_resource(texture, 0));
4812 ddraw->wined3d_frontbuffer = *surface;
4813 wined3d_surface_incref(*surface);
4814 wined3d_texture_decref(texture);
4816 return hr;
4819 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
4820 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
4822 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4823 HRESULT hr;
4825 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
4827 if (ddraw->wined3d_swapchain)
4829 ERR("Swapchain already created.\n");
4830 return E_FAIL;
4833 if (FAILED(hr = wined3d_swapchain_create(ddraw->wined3d_device, desc, NULL,
4834 &ddraw_null_wined3d_parent_ops, swapchain)))
4835 WARN("Failed to create swapchain, hr %#x.\n", hr);
4837 return hr;
4840 static const struct wined3d_device_parent_ops ddraw_wined3d_device_parent_ops =
4842 device_parent_wined3d_device_created,
4843 device_parent_mode_changed,
4844 device_parent_activate,
4845 device_parent_surface_created,
4846 device_parent_volume_created,
4847 device_parent_create_swapchain_surface,
4848 device_parent_create_swapchain,
4851 HRESULT ddraw_init(struct ddraw *ddraw, enum wined3d_device_type device_type)
4853 WINED3DCAPS caps;
4854 DWORD flags;
4855 HRESULT hr;
4857 ddraw->IDirectDraw7_iface.lpVtbl = &ddraw7_vtbl;
4858 ddraw->IDirectDraw_iface.lpVtbl = &ddraw1_vtbl;
4859 ddraw->IDirectDraw2_iface.lpVtbl = &ddraw2_vtbl;
4860 ddraw->IDirectDraw4_iface.lpVtbl = &ddraw4_vtbl;
4861 ddraw->IDirect3D_iface.lpVtbl = &d3d1_vtbl;
4862 ddraw->IDirect3D2_iface.lpVtbl = &d3d2_vtbl;
4863 ddraw->IDirect3D3_iface.lpVtbl = &d3d3_vtbl;
4864 ddraw->IDirect3D7_iface.lpVtbl = &d3d7_vtbl;
4865 ddraw->device_parent.ops = &ddraw_wined3d_device_parent_ops;
4866 ddraw->numIfaces = 1;
4867 ddraw->ref7 = 1;
4869 flags = WINED3D_LEGACY_DEPTH_BIAS | WINED3D_VIDMEM_ACCOUNTING;
4870 if (!(ddraw->wined3d = wined3d_create(flags)))
4872 if (!(ddraw->wined3d = wined3d_create(flags | WINED3D_NO3D)))
4874 WARN("Failed to create a wined3d object.\n");
4875 return E_FAIL;
4879 if (FAILED(hr = wined3d_get_device_caps(ddraw->wined3d, WINED3DADAPTER_DEFAULT, device_type, &caps)))
4881 ERR("Failed to get device caps, hr %#x.\n", hr);
4882 wined3d_decref(ddraw->wined3d);
4883 return E_FAIL;
4886 if (!(caps.ddraw_caps.caps & WINEDDCAPS_3D))
4888 WARN("Created a wined3d object without 3D support.\n");
4889 ddraw->flags |= DDRAW_NO3D;
4892 if (FAILED(hr = wined3d_device_create(ddraw->wined3d, WINED3DADAPTER_DEFAULT, device_type,
4893 NULL, 0, DDRAW_STRIDE_ALIGNMENT, &ddraw->device_parent, &ddraw->wined3d_device)))
4895 WARN("Failed to create a wined3d device, hr %#x.\n", hr);
4896 wined3d_decref(ddraw->wined3d);
4897 return hr;
4900 list_init(&ddraw->surface_list);
4902 return DD_OK;