d3d8/tests: Add test for clip planes limits.
[wine.git] / dlls / ddraw / ddraw.c
blobd3e491c3e8bbc3cc36f7238d3cf090ba34f4e1d7
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 #include "wine/exception.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
33 static const struct ddraw *exclusive_ddraw;
34 static HWND exclusive_window;
36 /* Device identifier. Don't relay it to WineD3D */
37 static const DDDEVICEIDENTIFIER2 deviceidentifier =
39 "vga.dll", /* default 2D driver */
40 "DirectDraw HAL",
41 { { 0x00010001, 0x00010001 } },
42 0, 0, 0, 0,
43 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
44 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
48 static struct enum_device_entry
50 char interface_name[100];
51 char device_name[100];
52 const GUID *device_guid;
53 } device_list7[] =
55 /* T&L HAL device */
57 "WINE Direct3D7 Hardware Transform and Lighting acceleration using WineD3D",
58 "Wine D3D7 T&L HAL",
59 &IID_IDirect3DTnLHalDevice,
62 /* HAL device */
64 "WINE Direct3D7 Hardware acceleration using WineD3D",
65 "Direct3D HAL",
66 &IID_IDirect3DHALDevice,
69 /* RGB device */
71 "WINE Direct3D7 RGB Software Emulation using WineD3D",
72 "Wine D3D7 RGB",
73 &IID_IDirect3DRGBDevice,
77 static void STDMETHODCALLTYPE ddraw_null_wined3d_object_destroyed(void *parent) {}
79 const struct wined3d_parent_ops ddraw_null_wined3d_parent_ops =
81 ddraw_null_wined3d_object_destroyed,
84 static inline struct ddraw *impl_from_IDirectDraw(IDirectDraw *iface)
86 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw_iface);
89 static inline struct ddraw *impl_from_IDirectDraw2(IDirectDraw2 *iface)
91 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw2_iface);
94 static inline struct ddraw *impl_from_IDirectDraw4(IDirectDraw4 *iface)
96 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw4_iface);
99 static inline struct ddraw *impl_from_IDirectDraw7(IDirectDraw7 *iface)
101 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw7_iface);
104 static inline struct ddraw *impl_from_IDirect3D(IDirect3D *iface)
106 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D_iface);
109 static inline struct ddraw *impl_from_IDirect3D2(IDirect3D2 *iface)
111 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D2_iface);
114 static inline struct ddraw *impl_from_IDirect3D3(IDirect3D3 *iface)
116 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D3_iface);
119 static inline struct ddraw *impl_from_IDirect3D7(IDirect3D7 *iface)
121 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D7_iface);
124 static HRESULT WINAPI ddraw7_QueryInterface(IDirectDraw7 *iface, REFIID riid, void **out)
126 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
128 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
130 if (!riid)
132 *out = NULL;
133 return DDERR_INVALIDPARAMS;
136 /* The refcount unit test revealed that an IDirect3D7 interface can only
137 * be queried from a DirectDraw object that was created as an IDirectDraw7
138 * interface. The older interfaces can query any IDirect3D version except
139 * 7, because they are all initially created as IDirectDraw. This isn't
140 * really crucial behavior, and messy to implement with the common
141 * creation function, so it has been left out here. */
142 if (IsEqualGUID(&IID_IDirectDraw7, riid)
143 || IsEqualGUID(&IID_IUnknown, riid))
145 *out = &ddraw->IDirectDraw7_iface;
146 TRACE("Returning IDirectDraw7 interface %p.\n", *out);
148 else if (IsEqualGUID(&IID_IDirectDraw4, riid))
150 *out = &ddraw->IDirectDraw4_iface;
151 TRACE("Returning IDirectDraw4 interface %p.\n", *out);
153 else if (IsEqualGUID(&IID_IDirectDraw2, riid))
155 *out = &ddraw->IDirectDraw2_iface;
156 TRACE("Returning IDirectDraw2 interface %p.\n", *out);
158 else if (IsEqualGUID(&IID_IDirectDraw, riid))
160 *out = &ddraw->IDirectDraw_iface;
161 TRACE("Returning IDirectDraw interface %p.\n", *out);
163 else if (IsEqualGUID(&IID_IDirect3D7, riid))
165 ddraw->d3dversion = 7;
166 *out = &ddraw->IDirect3D7_iface;
167 TRACE("Returning Direct3D7 interface %p.\n", *out);
169 else if (IsEqualGUID(&IID_IDirect3D3, riid))
171 ddraw->d3dversion = 3;
172 *out = &ddraw->IDirect3D3_iface;
173 TRACE("Returning Direct3D3 interface %p.\n", *out);
175 else if (IsEqualGUID(&IID_IDirect3D2, riid))
177 ddraw->d3dversion = 2;
178 *out = &ddraw->IDirect3D2_iface;
179 TRACE("Returning Direct3D2 interface %p.\n", *out);
181 else if (IsEqualGUID(&IID_IDirect3D, riid))
183 ddraw->d3dversion = 1;
184 *out = &ddraw->IDirect3D_iface;
185 TRACE("Returning Direct3D interface %p.\n", *out);
187 /* Unknown interface */
188 else
190 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
191 *out = NULL;
192 return E_NOINTERFACE;
195 IUnknown_AddRef((IUnknown *)*out);
196 return S_OK;
199 static HRESULT WINAPI ddraw4_QueryInterface(IDirectDraw4 *iface, REFIID riid, void **object)
201 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
203 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
205 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
208 static HRESULT WINAPI ddraw2_QueryInterface(IDirectDraw2 *iface, REFIID riid, void **object)
210 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
212 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
214 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
217 static HRESULT WINAPI ddraw1_QueryInterface(IDirectDraw *iface, REFIID riid, void **object)
219 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
221 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
223 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
226 static HRESULT WINAPI d3d7_QueryInterface(IDirect3D7 *iface, REFIID riid, void **object)
228 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
230 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
232 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
235 static HRESULT WINAPI d3d3_QueryInterface(IDirect3D3 *iface, REFIID riid, void **object)
237 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
239 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
241 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
244 static HRESULT WINAPI d3d2_QueryInterface(IDirect3D2 *iface, REFIID riid, void **object)
246 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
248 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
250 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
253 static HRESULT WINAPI d3d1_QueryInterface(IDirect3D *iface, REFIID riid, void **object)
255 struct ddraw *ddraw = impl_from_IDirect3D(iface);
257 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
259 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
262 /*****************************************************************************
263 * IDirectDraw7::AddRef
265 * Increases the interfaces refcount, basically
267 * DDraw refcounting is a bit tricky. The different DirectDraw interface
268 * versions have individual refcounts, but the IDirect3D interfaces do not.
269 * All interfaces are from one object, that means calling QueryInterface on an
270 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
271 * ddraw object.
273 * That means all AddRef and Release implementations of IDirectDrawX work
274 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
275 * except of IDirect3D7 which thunks to IDirectDraw7
277 * Returns: The new refcount
279 *****************************************************************************/
280 static ULONG WINAPI ddraw7_AddRef(IDirectDraw7 *iface)
282 struct ddraw *This = impl_from_IDirectDraw7(iface);
283 ULONG ref = InterlockedIncrement(&This->ref7);
285 TRACE("%p increasing refcount to %u.\n", This, ref);
287 if(ref == 1) InterlockedIncrement(&This->numIfaces);
289 return ref;
292 static ULONG WINAPI ddraw4_AddRef(IDirectDraw4 *iface)
294 struct ddraw *This = impl_from_IDirectDraw4(iface);
295 ULONG ref = InterlockedIncrement(&This->ref4);
297 TRACE("%p increasing refcount to %u.\n", This, ref);
299 if (ref == 1) InterlockedIncrement(&This->numIfaces);
301 return ref;
304 static ULONG WINAPI ddraw2_AddRef(IDirectDraw2 *iface)
306 struct ddraw *This = impl_from_IDirectDraw2(iface);
307 ULONG ref = InterlockedIncrement(&This->ref2);
309 TRACE("%p increasing refcount to %u.\n", This, ref);
311 if (ref == 1) InterlockedIncrement(&This->numIfaces);
313 return ref;
316 static ULONG WINAPI ddraw1_AddRef(IDirectDraw *iface)
318 struct ddraw *This = impl_from_IDirectDraw(iface);
319 ULONG ref = InterlockedIncrement(&This->ref1);
321 TRACE("%p increasing refcount to %u.\n", This, ref);
323 if (ref == 1) InterlockedIncrement(&This->numIfaces);
325 return ref;
328 static ULONG WINAPI d3d7_AddRef(IDirect3D7 *iface)
330 struct ddraw *This = impl_from_IDirect3D7(iface);
332 TRACE("iface %p.\n", iface);
334 return ddraw7_AddRef(&This->IDirectDraw7_iface);
337 static ULONG WINAPI d3d3_AddRef(IDirect3D3 *iface)
339 struct ddraw *This = impl_from_IDirect3D3(iface);
341 TRACE("iface %p.\n", iface);
343 return ddraw1_AddRef(&This->IDirectDraw_iface);
346 static ULONG WINAPI d3d2_AddRef(IDirect3D2 *iface)
348 struct ddraw *This = impl_from_IDirect3D2(iface);
350 TRACE("iface %p.\n", iface);
352 return ddraw1_AddRef(&This->IDirectDraw_iface);
355 static ULONG WINAPI d3d1_AddRef(IDirect3D *iface)
357 struct ddraw *This = impl_from_IDirect3D(iface);
359 TRACE("iface %p.\n", iface);
361 return ddraw1_AddRef(&This->IDirectDraw_iface);
364 static void ddraw_destroy_swapchain(struct ddraw *ddraw)
366 TRACE("Destroying the swapchain.\n");
368 wined3d_swapchain_decref(ddraw->wined3d_swapchain);
369 ddraw->wined3d_swapchain = NULL;
371 if (!(ddraw->flags & DDRAW_NO3D))
373 UINT i;
375 for (i = 0; i < ddraw->numConvertedDecls; ++i)
377 wined3d_vertex_declaration_decref(ddraw->decls[i].decl);
379 HeapFree(GetProcessHeap(), 0, ddraw->decls);
380 ddraw->numConvertedDecls = 0;
382 if (FAILED(wined3d_device_uninit_3d(ddraw->wined3d_device)))
384 ERR("Failed to uninit 3D.\n");
386 else
388 /* Free the d3d window if one was created. */
389 if (ddraw->d3d_window && ddraw->d3d_window != ddraw->dest_window)
391 TRACE("Destroying the hidden render window %p.\n", ddraw->d3d_window);
392 DestroyWindow(ddraw->d3d_window);
393 ddraw->d3d_window = 0;
397 ddraw->flags &= ~DDRAW_D3D_INITIALIZED;
399 else
401 wined3d_device_uninit_gdi(ddraw->wined3d_device);
404 ddraw_set_swapchain_window(ddraw, NULL);
406 TRACE("Swapchain destroyed.\n");
409 /*****************************************************************************
410 * ddraw_destroy
412 * Destroys a ddraw object if all refcounts are 0. This is to share code
413 * between the IDirectDrawX::Release functions
415 * Params:
416 * This: DirectDraw object to destroy
418 *****************************************************************************/
419 static void ddraw_destroy(struct ddraw *This)
421 IDirectDraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, NULL, DDSCL_NORMAL);
422 IDirectDraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
424 /* Destroy the device window if we created one */
425 if(This->devicewindow != 0)
427 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
428 DestroyWindow(This->devicewindow);
429 This->devicewindow = 0;
432 wined3d_mutex_lock();
433 list_remove(&This->ddraw_list_entry);
434 wined3d_mutex_unlock();
436 if (This->wined3d_swapchain)
437 ddraw_destroy_swapchain(This);
438 wined3d_device_decref(This->wined3d_device);
439 wined3d_decref(This->wined3d);
441 if (This->d3ddevice)
442 This->d3ddevice->ddraw = NULL;
444 /* Now free the object */
445 HeapFree(GetProcessHeap(), 0, This);
448 /*****************************************************************************
449 * IDirectDraw7::Release
451 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
453 * Returns: The new refcount
454 *****************************************************************************/
455 static ULONG WINAPI ddraw7_Release(IDirectDraw7 *iface)
457 struct ddraw *This = impl_from_IDirectDraw7(iface);
458 ULONG ref = InterlockedDecrement(&This->ref7);
460 TRACE("%p decreasing refcount to %u.\n", This, ref);
462 if (!ref && !InterlockedDecrement(&This->numIfaces))
463 ddraw_destroy(This);
465 return ref;
468 static ULONG WINAPI ddraw4_Release(IDirectDraw4 *iface)
470 struct ddraw *This = impl_from_IDirectDraw4(iface);
471 ULONG ref = InterlockedDecrement(&This->ref4);
473 TRACE("%p decreasing refcount to %u.\n", This, ref);
475 if (!ref && !InterlockedDecrement(&This->numIfaces))
476 ddraw_destroy(This);
478 return ref;
481 static ULONG WINAPI ddraw2_Release(IDirectDraw2 *iface)
483 struct ddraw *This = impl_from_IDirectDraw2(iface);
484 ULONG ref = InterlockedDecrement(&This->ref2);
486 TRACE("%p decreasing refcount to %u.\n", This, ref);
488 if (!ref && !InterlockedDecrement(&This->numIfaces))
489 ddraw_destroy(This);
491 return ref;
494 static ULONG WINAPI ddraw1_Release(IDirectDraw *iface)
496 struct ddraw *This = impl_from_IDirectDraw(iface);
497 ULONG ref = InterlockedDecrement(&This->ref1);
499 TRACE("%p decreasing refcount to %u.\n", This, ref);
501 if (!ref && !InterlockedDecrement(&This->numIfaces))
502 ddraw_destroy(This);
504 return ref;
507 static ULONG WINAPI d3d7_Release(IDirect3D7 *iface)
509 struct ddraw *This = impl_from_IDirect3D7(iface);
511 TRACE("iface %p.\n", iface);
513 return ddraw7_Release(&This->IDirectDraw7_iface);
516 static ULONG WINAPI d3d3_Release(IDirect3D3 *iface)
518 struct ddraw *This = impl_from_IDirect3D3(iface);
520 TRACE("iface %p.\n", iface);
522 return ddraw1_Release(&This->IDirectDraw_iface);
525 static ULONG WINAPI d3d2_Release(IDirect3D2 *iface)
527 struct ddraw *This = impl_from_IDirect3D2(iface);
529 TRACE("iface %p.\n", iface);
531 return ddraw1_Release(&This->IDirectDraw_iface);
534 static ULONG WINAPI d3d1_Release(IDirect3D *iface)
536 struct ddraw *This = impl_from_IDirect3D(iface);
538 TRACE("iface %p.\n", iface);
540 return ddraw1_Release(&This->IDirectDraw_iface);
543 /*****************************************************************************
544 * IDirectDraw methods
545 *****************************************************************************/
547 static HRESULT ddraw_set_focus_window(struct ddraw *ddraw, HWND window)
549 /* FIXME: This looks wrong, exclusive mode should imply a destination
550 * window. */
551 if ((ddraw->cooperative_level & DDSCL_EXCLUSIVE) && ddraw->dest_window)
553 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET.\n");
554 return DDERR_HWNDALREADYSET;
557 ddraw->focuswindow = window;
559 return DD_OK;
562 static HRESULT ddraw_attach_d3d_device(struct ddraw *ddraw,
563 struct wined3d_swapchain_desc *swapchain_desc)
565 HWND window = swapchain_desc->device_window;
566 HRESULT hr;
568 TRACE("ddraw %p.\n", ddraw);
570 if (!window || window == GetDesktopWindow())
572 window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "Hidden D3D Window",
573 WS_DISABLED, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
574 NULL, NULL, NULL, NULL);
575 if (!window)
577 ERR("Failed to create window, last error %#x.\n", GetLastError());
578 return E_FAIL;
581 ShowWindow(window, SW_HIDE); /* Just to be sure */
582 WARN("No window for the Direct3DDevice, created hidden window %p.\n", window);
584 swapchain_desc->device_window = window;
586 else
588 TRACE("Using existing window %p for Direct3D rendering.\n", window);
590 ddraw->d3d_window = window;
592 /* Set this NOW, otherwise creating the depth stencil surface will cause a
593 * recursive loop until ram or emulated video memory is full. */
594 ddraw->flags |= DDRAW_D3D_INITIALIZED;
595 hr = wined3d_device_init_3d(ddraw->wined3d_device, swapchain_desc);
596 if (FAILED(hr))
598 ddraw->flags &= ~DDRAW_D3D_INITIALIZED;
599 return hr;
602 ddraw->declArraySize = 2;
603 ddraw->decls = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw->decls) * ddraw->declArraySize);
604 if (!ddraw->decls)
606 ERR("Error allocating an array for the converted vertex decls.\n");
607 ddraw->declArraySize = 0;
608 hr = wined3d_device_uninit_3d(ddraw->wined3d_device);
609 return E_OUTOFMEMORY;
612 TRACE("Successfully initialized 3D.\n");
614 return DD_OK;
617 static HRESULT ddraw_create_swapchain(struct ddraw *ddraw, HWND window, BOOL windowed)
619 struct wined3d_swapchain_desc swapchain_desc;
620 struct wined3d_display_mode mode;
621 HRESULT hr = WINED3D_OK;
623 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
625 ERR("Failed to get display mode.\n");
626 return hr;
629 memset(&swapchain_desc, 0, sizeof(swapchain_desc));
630 swapchain_desc.backbuffer_width = mode.width;
631 swapchain_desc.backbuffer_height = mode.height;
632 swapchain_desc.backbuffer_format = mode.format_id;
633 swapchain_desc.swap_effect = WINED3D_SWAP_EFFECT_COPY;
634 swapchain_desc.device_window = window;
635 swapchain_desc.windowed = windowed;
636 swapchain_desc.flags = WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
638 if (!(ddraw->flags & DDRAW_NO3D))
639 hr = ddraw_attach_d3d_device(ddraw, &swapchain_desc);
640 else
641 hr = wined3d_device_init_gdi(ddraw->wined3d_device, &swapchain_desc);
643 if (FAILED(hr))
645 ERR("Failed to create swapchain, hr %#x.\n", hr);
646 return hr;
649 if (!(ddraw->wined3d_swapchain = wined3d_device_get_swapchain(ddraw->wined3d_device, 0)))
651 ERR("Failed to get swapchain.\n");
652 return DDERR_INVALIDPARAMS;
655 wined3d_swapchain_incref(ddraw->wined3d_swapchain);
656 ddraw_set_swapchain_window(ddraw, window);
658 if (ddraw->primary && ddraw->primary->palette)
659 wined3d_swapchain_set_palette(ddraw->wined3d_swapchain, ddraw->primary->palette->wined3d_palette);
661 return DD_OK;
664 /*****************************************************************************
665 * IDirectDraw7::RestoreDisplayMode
667 * Restores the display mode to what it was at creation time. Basically.
669 * Returns
670 * DD_OK on success
671 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
673 *****************************************************************************/
674 static HRESULT WINAPI ddraw7_RestoreDisplayMode(IDirectDraw7 *iface)
676 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
677 HRESULT hr;
679 TRACE("iface %p.\n", iface);
681 wined3d_mutex_lock();
683 if (!(ddraw->flags & DDRAW_RESTORE_MODE))
685 wined3d_mutex_unlock();
686 return DD_OK;
689 if (exclusive_ddraw && exclusive_ddraw != ddraw)
691 wined3d_mutex_unlock();
692 return DDERR_NOEXCLUSIVEMODE;
695 if (SUCCEEDED(hr = wined3d_set_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, NULL)))
696 ddraw->flags &= ~DDRAW_RESTORE_MODE;
698 InterlockedCompareExchange(&ddraw->device_state, DDRAW_DEVICE_STATE_NOT_RESTORED, DDRAW_DEVICE_STATE_OK);
700 wined3d_mutex_unlock();
702 return hr;
705 static HRESULT WINAPI ddraw4_RestoreDisplayMode(IDirectDraw4 *iface)
707 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
709 TRACE("iface %p.\n", iface);
711 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
714 static HRESULT WINAPI ddraw2_RestoreDisplayMode(IDirectDraw2 *iface)
716 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
718 TRACE("iface %p.\n", iface);
720 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
723 static HRESULT WINAPI ddraw1_RestoreDisplayMode(IDirectDraw *iface)
725 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
727 TRACE("iface %p.\n", iface);
729 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
732 /*****************************************************************************
733 * IDirectDraw7::SetCooperativeLevel
735 * Sets the cooperative level for the DirectDraw object, and the window
736 * assigned to it. The cooperative level determines the general behavior
737 * of the DirectDraw application
739 * Warning: This is quite tricky, as it's not really documented which
740 * cooperative levels can be combined with each other. If a game fails
741 * after this function, try to check the cooperative levels passed on
742 * Windows, and if it returns something different.
744 * If you think that this function caused the failure because it writes a
745 * fixme, be sure to run again with a +ddraw trace.
747 * What is known about cooperative levels (See the ddraw modes test):
748 * DDSCL_EXCLUSIVE requires DDSCL_FULLSCREEN.
749 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE.
750 * Unlike what msdn claims, DDSCL_NORMAL | DDSCL_FULLSCREEN is allowed.
751 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
752 * DDSCL_EXCLUSIVE can be activated.
753 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES or
754 * DDSCL_CREATEDEVICEWINDOW.
756 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
757 * DDSCL_CREATEDEVICEWINDOW, DDSCL_SETDEVICEWINDOW
758 * DDSCL_SETFOCUSWINDOW (partially),
759 * DDSCL_MULTITHREADED (work in progress)
760 * DDSCL_FPUPRESERVE (see device.c)
762 * Unsure about this: DDSCL_FPUSETUP
764 * These don't seem very important for wine:
765 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
767 * Returns:
768 * DD_OK if the cooperative level was set successfully
769 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
770 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
771 * (Probably others too, have to investigate)
773 *****************************************************************************/
774 static HRESULT ddraw_set_cooperative_level(struct ddraw *ddraw, HWND window,
775 DWORD cooplevel, BOOL restore_mode_on_normal)
777 struct wined3d_rendertarget_view *rtv = NULL, *dsv = NULL;
778 struct wined3d_stateblock *stateblock;
779 BOOL restore_state = FALSE;
780 HRESULT hr;
782 TRACE("ddraw %p, window %p, flags %#x, restore_mode_on_normal %x.\n", ddraw, window, cooplevel,
783 restore_mode_on_normal);
784 DDRAW_dump_cooperativelevel(cooplevel);
786 wined3d_mutex_lock();
788 if (ddraw->flags & DDRAW_SCL_RECURSIVE)
790 WARN("Recursive call, returning DD_OK.\n");
791 hr = DD_OK;
792 goto done;
794 ddraw->flags |= DDRAW_SCL_RECURSIVE;
796 /* Tests suggest that we need one of them: */
797 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
798 DDSCL_NORMAL |
799 DDSCL_EXCLUSIVE )))
801 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
802 hr = DDERR_INVALIDPARAMS;
803 goto done;
806 if ((cooplevel & DDSCL_CREATEDEVICEWINDOW) && !(cooplevel & DDSCL_EXCLUSIVE))
808 WARN("DDSCL_CREATEDEVICEWINDOW requires DDSCL_EXCLUSIVE.\n");
809 hr = DDERR_INVALIDPARAMS;
810 goto done;
813 /* Handle those levels first which set various hwnds */
814 if ((cooplevel & DDSCL_SETFOCUSWINDOW) && !(cooplevel & DDSCL_CREATEDEVICEWINDOW))
816 /* This isn't compatible with a lot of flags */
817 if (cooplevel & (DDSCL_MULTITHREADED
818 | DDSCL_FPUSETUP
819 | DDSCL_FPUPRESERVE
820 | DDSCL_ALLOWREBOOT
821 | DDSCL_ALLOWMODEX
822 | DDSCL_SETDEVICEWINDOW
823 | DDSCL_NORMAL
824 | DDSCL_EXCLUSIVE
825 | DDSCL_FULLSCREEN))
827 WARN("Called with incompatible flags, returning DDERR_INVALIDPARAMS.\n");
828 hr = DDERR_INVALIDPARAMS;
829 goto done;
832 hr = ddraw_set_focus_window(ddraw, window);
833 goto done;
836 if (cooplevel & DDSCL_EXCLUSIVE)
838 if (!(cooplevel & DDSCL_FULLSCREEN) || !(window || (cooplevel & DDSCL_CREATEDEVICEWINDOW)))
840 WARN("DDSCL_EXCLUSIVE requires DDSCL_FULLSCREEN and a window.\n");
841 hr = DDERR_INVALIDPARAMS;
842 goto done;
845 if (cooplevel & DDSCL_CREATEDEVICEWINDOW)
847 HWND device_window;
849 if (!ddraw->focuswindow && !(cooplevel & DDSCL_SETFOCUSWINDOW))
851 WARN("No focus window set.\n");
852 hr = DDERR_NOFOCUSWINDOW;
853 goto done;
856 device_window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "DirectDrawDeviceWnd",
857 WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
858 NULL, NULL, NULL, NULL);
859 if (!device_window)
861 ERR("Failed to create window, last error %#x.\n", GetLastError());
862 hr = E_FAIL;
863 goto done;
866 ShowWindow(device_window, SW_SHOW);
867 TRACE("Created a device window %p.\n", device_window);
869 /* Native apparently leaks the created device window if setting the
870 * focus window below fails. */
871 ddraw->cooperative_level |= DDSCL_CREATEDEVICEWINDOW;
872 ddraw->devicewindow = device_window;
874 if (cooplevel & DDSCL_SETFOCUSWINDOW)
876 if (!window)
878 hr = DDERR_NOHWND;
879 goto done;
882 if (FAILED(hr = ddraw_set_focus_window(ddraw, window)))
883 goto done;
886 window = device_window;
889 else
891 if (ddraw->cooperative_level & DDSCL_CREATEDEVICEWINDOW)
892 DestroyWindow(ddraw->devicewindow);
893 ddraw->devicewindow = NULL;
894 ddraw->focuswindow = NULL;
897 if ((cooplevel & DDSCL_FULLSCREEN) != (ddraw->cooperative_level & DDSCL_FULLSCREEN) || window != ddraw->dest_window)
899 if (ddraw->cooperative_level & DDSCL_FULLSCREEN)
900 wined3d_device_restore_fullscreen_window(ddraw->wined3d_device, ddraw->dest_window, NULL);
902 if (cooplevel & DDSCL_FULLSCREEN)
904 struct wined3d_display_mode display_mode;
906 wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &display_mode, NULL);
907 wined3d_device_setup_fullscreen_window(ddraw->wined3d_device, window,
908 display_mode.width, display_mode.height);
912 if ((cooplevel & DDSCL_EXCLUSIVE) && exclusive_window != window)
914 ddraw->device_state = DDRAW_DEVICE_STATE_NOT_RESTORED;
915 exclusive_window = window;
918 if (cooplevel & DDSCL_MULTITHREADED && !(ddraw->cooperative_level & DDSCL_MULTITHREADED))
919 wined3d_device_set_multithreaded(ddraw->wined3d_device);
921 if (ddraw->wined3d_swapchain)
923 if (!(ddraw->flags & DDRAW_NO3D))
925 restore_state = TRUE;
927 if (FAILED(hr = wined3d_stateblock_create(ddraw->wined3d_device, WINED3D_SBT_ALL, &stateblock)))
929 ERR("Failed to create stateblock, hr %#x.\n", hr);
930 goto done;
933 wined3d_stateblock_capture(stateblock);
934 rtv = wined3d_device_get_rendertarget_view(ddraw->wined3d_device, 0);
935 /* Rendering to ddraw->wined3d_frontbuffer. */
936 if (rtv && !wined3d_rendertarget_view_get_sub_resource_parent(rtv))
937 rtv = NULL;
938 else if (rtv)
939 wined3d_rendertarget_view_incref(rtv);
941 if ((dsv = wined3d_device_get_depth_stencil_view(ddraw->wined3d_device)))
942 wined3d_rendertarget_view_incref(dsv);
945 ddraw_destroy_swapchain(ddraw);
948 if (FAILED(hr = ddraw_create_swapchain(ddraw, window, !(cooplevel & DDSCL_FULLSCREEN))))
949 ERR("Failed to create swapchain, hr %#x.\n", hr);
951 if (restore_state)
953 if (dsv)
955 wined3d_device_set_depth_stencil_view(ddraw->wined3d_device, dsv);
956 wined3d_rendertarget_view_decref(dsv);
959 if (rtv)
961 wined3d_device_set_rendertarget_view(ddraw->wined3d_device, 0, rtv, FALSE);
962 wined3d_rendertarget_view_decref(rtv);
965 wined3d_stateblock_apply(stateblock);
966 wined3d_stateblock_decref(stateblock);
969 if (!(cooplevel & DDSCL_EXCLUSIVE) && (ddraw->cooperative_level & DDSCL_EXCLUSIVE)
970 && restore_mode_on_normal)
972 hr = ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
973 if (FAILED(hr))
974 ERR("RestoreDisplayMode failed\n");
977 if ((ddraw->cooperative_level & DDSCL_EXCLUSIVE)
978 && (window != ddraw->dest_window || !(cooplevel & DDSCL_EXCLUSIVE)))
979 wined3d_device_release_focus_window(ddraw->wined3d_device);
981 if ((cooplevel & DDSCL_EXCLUSIVE)
982 && (window != ddraw->dest_window || !(ddraw->cooperative_level & DDSCL_EXCLUSIVE)))
984 hr = wined3d_device_acquire_focus_window(ddraw->wined3d_device, window);
985 if (FAILED(hr))
987 ERR("Failed to acquire focus window, hr %#x.\n", hr);
988 goto done;
992 /* Unhandled flags */
993 if (cooplevel & DDSCL_ALLOWREBOOT)
994 WARN("Unhandled flag DDSCL_ALLOWREBOOT, harmless\n");
995 if (cooplevel & DDSCL_ALLOWMODEX)
996 WARN("Unhandled flag DDSCL_ALLOWMODEX, harmless\n");
997 if (cooplevel & DDSCL_FPUSETUP)
998 WARN("Unhandled flag DDSCL_FPUSETUP, harmless\n");
1000 if (cooplevel & DDSCL_EXCLUSIVE)
1001 exclusive_ddraw = ddraw;
1002 else if (exclusive_ddraw == ddraw)
1003 exclusive_ddraw = NULL;
1005 /* Store the cooperative_level */
1006 ddraw->cooperative_level = cooplevel;
1007 ddraw->dest_window = window;
1009 TRACE("SetCooperativeLevel returning DD_OK\n");
1010 hr = DD_OK;
1011 done:
1012 ddraw->flags &= ~DDRAW_SCL_RECURSIVE;
1013 wined3d_mutex_unlock();
1015 return hr;
1018 static HRESULT WINAPI ddraw7_SetCooperativeLevel(IDirectDraw7 *iface, HWND window, DWORD flags)
1020 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1022 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1024 return ddraw_set_cooperative_level(ddraw, window, flags, !(ddraw->flags & DDRAW_SCL_DDRAW1));
1027 static HRESULT WINAPI ddraw4_SetCooperativeLevel(IDirectDraw4 *iface, HWND window, DWORD flags)
1029 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1031 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1033 return ddraw_set_cooperative_level(ddraw, window, flags, !(ddraw->flags & DDRAW_SCL_DDRAW1));
1036 static HRESULT WINAPI ddraw2_SetCooperativeLevel(IDirectDraw2 *iface, HWND window, DWORD flags)
1038 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1040 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1042 return ddraw_set_cooperative_level(ddraw, window, flags, !(ddraw->flags & DDRAW_SCL_DDRAW1));
1045 static HRESULT WINAPI ddraw1_SetCooperativeLevel(IDirectDraw *iface, HWND window, DWORD flags)
1047 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1048 HRESULT hr;
1050 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1052 hr = ddraw_set_cooperative_level(ddraw, window, flags, FALSE);
1053 if (SUCCEEDED(hr))
1054 ddraw->flags |= DDRAW_SCL_DDRAW1;
1055 return hr;
1058 /*****************************************************************************
1059 * IDirectDraw7::SetDisplayMode
1061 * Sets the display screen resolution, color depth and refresh frequency
1062 * when in fullscreen mode (in theory).
1063 * Possible return values listed in the SDK suggest that this method fails
1064 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
1065 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
1066 * It seems to be valid to pass 0 for With and Height, this has to be tested
1067 * It could mean that the current video mode should be left as-is. (But why
1068 * call it then?)
1070 * Params:
1071 * Height, Width: Screen dimension
1072 * BPP: Color depth in Bits per pixel
1073 * Refreshrate: Screen refresh rate
1074 * Flags: Other stuff
1076 * Returns
1077 * DD_OK on success
1079 *****************************************************************************/
1080 static HRESULT WINAPI ddraw7_SetDisplayMode(IDirectDraw7 *iface, DWORD width, DWORD height,
1081 DWORD bpp, DWORD refresh_rate, DWORD flags)
1083 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1084 struct wined3d_display_mode mode;
1085 enum wined3d_format_id format;
1086 HRESULT hr;
1088 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1089 iface, width, height, bpp, refresh_rate, flags);
1091 if (force_refresh_rate != 0)
1093 TRACE("ForceRefreshRate overriding passed-in refresh rate (%u Hz) to %u Hz\n",
1094 refresh_rate, force_refresh_rate);
1095 refresh_rate = force_refresh_rate;
1098 wined3d_mutex_lock();
1100 if (exclusive_ddraw && exclusive_ddraw != ddraw)
1102 wined3d_mutex_unlock();
1103 return DDERR_NOEXCLUSIVEMODE;
1106 if (!width || !height)
1108 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here. */
1109 wined3d_mutex_unlock();
1110 return DD_OK;
1113 switch (bpp)
1115 case 8: format = WINED3DFMT_P8_UINT; break;
1116 case 15: format = WINED3DFMT_B5G5R5X1_UNORM; break;
1117 case 16: format = WINED3DFMT_B5G6R5_UNORM; break;
1118 case 24: format = WINED3DFMT_B8G8R8_UNORM; break;
1119 case 32: format = WINED3DFMT_B8G8R8X8_UNORM; break;
1120 default: format = WINED3DFMT_UNKNOWN; break;
1123 mode.width = width;
1124 mode.height = height;
1125 mode.refresh_rate = refresh_rate;
1126 mode.format_id = format;
1127 mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
1129 /* TODO: The possible return values from msdn suggest that the screen mode
1130 * can't be changed if a surface is locked or some drawing is in progress. */
1131 if (SUCCEEDED(hr = wined3d_set_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode)))
1133 if (ddraw->primary)
1135 DDSURFACEDESC2 *surface_desc = &ddraw->primary->surface_desc;
1137 if (FAILED(hr = wined3d_swapchain_resize_buffers(ddraw->wined3d_swapchain, 0,
1138 surface_desc->dwWidth, surface_desc->dwHeight, mode.format_id, WINED3D_MULTISAMPLE_NONE, 0)))
1139 ERR("Failed to resize buffers, hr %#x.\n", hr);
1140 else
1141 ddrawformat_from_wined3dformat(&ddraw->primary->surface_desc.u4.ddpfPixelFormat, mode.format_id);
1143 ddraw->flags |= DDRAW_RESTORE_MODE;
1146 InterlockedCompareExchange(&ddraw->device_state, DDRAW_DEVICE_STATE_NOT_RESTORED, DDRAW_DEVICE_STATE_OK);
1148 wined3d_mutex_unlock();
1150 switch (hr)
1152 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1153 default: return hr;
1157 static HRESULT WINAPI ddraw4_SetDisplayMode(IDirectDraw4 *iface, DWORD width, DWORD height,
1158 DWORD bpp, DWORD refresh_rate, DWORD flags)
1160 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1162 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1163 iface, width, height, bpp, refresh_rate, flags);
1165 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1168 static HRESULT WINAPI ddraw2_SetDisplayMode(IDirectDraw2 *iface,
1169 DWORD width, DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
1171 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1173 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1174 iface, width, height, bpp, refresh_rate, flags);
1176 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1179 static HRESULT WINAPI ddraw1_SetDisplayMode(IDirectDraw *iface, DWORD width, DWORD height, DWORD bpp)
1181 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1183 TRACE("iface %p, width %u, height %u, bpp %u.\n", iface, width, height, bpp);
1185 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, 0, 0);
1188 void ddraw_d3dcaps1_from_7(D3DDEVICEDESC *caps1, D3DDEVICEDESC7 *caps7)
1190 memset(caps1, 0, sizeof(*caps1));
1191 caps1->dwSize = sizeof(*caps1);
1192 caps1->dwFlags = D3DDD_COLORMODEL
1193 | D3DDD_DEVCAPS
1194 | D3DDD_TRANSFORMCAPS
1195 | D3DDD_BCLIPPING
1196 | D3DDD_LIGHTINGCAPS
1197 | D3DDD_LINECAPS
1198 | D3DDD_TRICAPS
1199 | D3DDD_DEVICERENDERBITDEPTH
1200 | D3DDD_DEVICEZBUFFERBITDEPTH
1201 | D3DDD_MAXBUFFERSIZE
1202 | D3DDD_MAXVERTEXCOUNT;
1203 caps1->dcmColorModel = D3DCOLOR_RGB;
1204 caps1->dwDevCaps = caps7->dwDevCaps;
1205 caps1->dtcTransformCaps.dwSize = sizeof(caps1->dtcTransformCaps);
1206 caps1->dtcTransformCaps.dwCaps = D3DTRANSFORMCAPS_CLIP;
1207 caps1->bClipping = TRUE;
1208 caps1->dlcLightingCaps.dwSize = sizeof(caps1->dlcLightingCaps);
1209 caps1->dlcLightingCaps.dwCaps = D3DLIGHTCAPS_DIRECTIONAL
1210 | D3DLIGHTCAPS_PARALLELPOINT
1211 | D3DLIGHTCAPS_POINT
1212 | D3DLIGHTCAPS_SPOT;
1213 caps1->dlcLightingCaps.dwLightingModel = D3DLIGHTINGMODEL_RGB;
1214 caps1->dlcLightingCaps.dwNumLights = caps7->dwMaxActiveLights;
1215 caps1->dpcLineCaps = caps7->dpcLineCaps;
1216 caps1->dpcTriCaps = caps7->dpcTriCaps;
1217 caps1->dwDeviceRenderBitDepth = caps7->dwDeviceRenderBitDepth;
1218 caps1->dwDeviceZBufferBitDepth = caps7->dwDeviceZBufferBitDepth;
1219 caps1->dwMaxBufferSize = 0;
1220 caps1->dwMaxVertexCount = 65536;
1221 caps1->dwMinTextureWidth = caps7->dwMinTextureWidth;
1222 caps1->dwMinTextureHeight = caps7->dwMinTextureHeight;
1223 caps1->dwMaxTextureWidth = caps7->dwMaxTextureWidth;
1224 caps1->dwMaxTextureHeight = caps7->dwMaxTextureHeight;
1225 caps1->dwMinStippleWidth = 1;
1226 caps1->dwMinStippleHeight = 1;
1227 caps1->dwMaxStippleWidth = 32;
1228 caps1->dwMaxStippleHeight = 32;
1229 caps1->dwMaxTextureRepeat = caps7->dwMaxTextureRepeat;
1230 caps1->dwMaxTextureAspectRatio = caps7->dwMaxTextureAspectRatio;
1231 caps1->dwMaxAnisotropy = caps7->dwMaxAnisotropy;
1232 caps1->dvGuardBandLeft = caps7->dvGuardBandLeft;
1233 caps1->dvGuardBandTop = caps7->dvGuardBandTop;
1234 caps1->dvGuardBandRight = caps7->dvGuardBandRight;
1235 caps1->dvGuardBandBottom = caps7->dvGuardBandBottom;
1236 caps1->dvExtentsAdjust = caps7->dvExtentsAdjust;
1237 caps1->dwStencilCaps = caps7->dwStencilCaps;
1238 caps1->dwFVFCaps = caps7->dwFVFCaps;
1239 caps1->dwTextureOpCaps = caps7->dwTextureOpCaps;
1240 caps1->wMaxTextureBlendStages = caps7->wMaxTextureBlendStages;
1241 caps1->wMaxSimultaneousTextures = caps7->wMaxSimultaneousTextures;
1244 HRESULT ddraw_get_d3dcaps(const struct ddraw *ddraw, D3DDEVICEDESC7 *caps)
1246 WINED3DCAPS wined3d_caps;
1247 HRESULT hr;
1249 TRACE("ddraw %p, caps %p.\n", ddraw, caps);
1251 memset(&wined3d_caps, 0, sizeof(wined3d_caps));
1253 wined3d_mutex_lock();
1254 hr = wined3d_get_device_caps(ddraw->wined3d, 0, WINED3D_DEVICE_TYPE_HAL, &wined3d_caps);
1255 wined3d_mutex_unlock();
1256 if (FAILED(hr))
1258 WARN("Failed to get device caps, hr %#x.\n", hr);
1259 return hr;
1262 caps->dwDevCaps = wined3d_caps.DevCaps;
1263 caps->dpcLineCaps.dwMiscCaps = wined3d_caps.PrimitiveMiscCaps;
1264 caps->dpcLineCaps.dwRasterCaps = wined3d_caps.RasterCaps;
1265 caps->dpcLineCaps.dwZCmpCaps = wined3d_caps.ZCmpCaps;
1266 caps->dpcLineCaps.dwSrcBlendCaps = wined3d_caps.SrcBlendCaps;
1267 caps->dpcLineCaps.dwDestBlendCaps = wined3d_caps.DestBlendCaps;
1268 caps->dpcLineCaps.dwAlphaCmpCaps = wined3d_caps.AlphaCmpCaps;
1269 caps->dpcLineCaps.dwShadeCaps = wined3d_caps.ShadeCaps;
1270 caps->dpcLineCaps.dwTextureCaps = wined3d_caps.TextureCaps;
1271 caps->dpcLineCaps.dwTextureFilterCaps = wined3d_caps.TextureFilterCaps;
1272 caps->dpcLineCaps.dwTextureAddressCaps = wined3d_caps.TextureAddressCaps;
1274 caps->dwMaxTextureWidth = wined3d_caps.MaxTextureWidth;
1275 caps->dwMaxTextureHeight = wined3d_caps.MaxTextureHeight;
1277 caps->dwMaxTextureRepeat = wined3d_caps.MaxTextureRepeat;
1278 caps->dwMaxTextureAspectRatio = wined3d_caps.MaxTextureAspectRatio;
1279 caps->dwMaxAnisotropy = wined3d_caps.MaxAnisotropy;
1280 caps->dvMaxVertexW = wined3d_caps.MaxVertexW;
1282 caps->dvGuardBandLeft = wined3d_caps.GuardBandLeft;
1283 caps->dvGuardBandTop = wined3d_caps.GuardBandTop;
1284 caps->dvGuardBandRight = wined3d_caps.GuardBandRight;
1285 caps->dvGuardBandBottom = wined3d_caps.GuardBandBottom;
1287 caps->dvExtentsAdjust = wined3d_caps.ExtentsAdjust;
1288 caps->dwStencilCaps = wined3d_caps.StencilCaps;
1290 caps->dwFVFCaps = wined3d_caps.FVFCaps;
1291 caps->dwTextureOpCaps = wined3d_caps.TextureOpCaps;
1293 caps->dwVertexProcessingCaps = wined3d_caps.VertexProcessingCaps;
1294 caps->dwMaxActiveLights = wined3d_caps.MaxActiveLights;
1296 /* Remove all non-d3d7 caps */
1297 caps->dwDevCaps &= (
1298 D3DDEVCAPS_FLOATTLVERTEX | D3DDEVCAPS_SORTINCREASINGZ | D3DDEVCAPS_SORTDECREASINGZ |
1299 D3DDEVCAPS_SORTEXACT | D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY |
1300 D3DDEVCAPS_TLVERTEXSYSTEMMEMORY | D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY |
1301 D3DDEVCAPS_TEXTUREVIDEOMEMORY | D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP |
1302 D3DDEVCAPS_TEXTURENONLOCALVIDMEM | D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
1303 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT | D3DDEVCAPS_CANBLTSYSTONONLOCAL |
1304 D3DDEVCAPS_HWRASTERIZATION);
1306 caps->dwStencilCaps &= (
1307 D3DSTENCILCAPS_KEEP | D3DSTENCILCAPS_ZERO | D3DSTENCILCAPS_REPLACE |
1308 D3DSTENCILCAPS_INCRSAT | D3DSTENCILCAPS_DECRSAT | D3DSTENCILCAPS_INVERT |
1309 D3DSTENCILCAPS_INCR | D3DSTENCILCAPS_DECR);
1311 /* FVF caps ?*/
1313 caps->dwTextureOpCaps &= (
1314 D3DTEXOPCAPS_DISABLE | D3DTEXOPCAPS_SELECTARG1 | D3DTEXOPCAPS_SELECTARG2 |
1315 D3DTEXOPCAPS_MODULATE | D3DTEXOPCAPS_MODULATE2X | D3DTEXOPCAPS_MODULATE4X |
1316 D3DTEXOPCAPS_ADD | D3DTEXOPCAPS_ADDSIGNED | D3DTEXOPCAPS_ADDSIGNED2X |
1317 D3DTEXOPCAPS_SUBTRACT | D3DTEXOPCAPS_ADDSMOOTH | D3DTEXOPCAPS_BLENDTEXTUREALPHA |
1318 D3DTEXOPCAPS_BLENDFACTORALPHA | D3DTEXOPCAPS_BLENDTEXTUREALPHAPM | D3DTEXOPCAPS_BLENDCURRENTALPHA |
1319 D3DTEXOPCAPS_PREMODULATE | D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
1320 D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA | D3DTEXOPCAPS_BUMPENVMAP |
1321 D3DTEXOPCAPS_BUMPENVMAPLUMINANCE | D3DTEXOPCAPS_DOTPRODUCT3);
1323 caps->dwVertexProcessingCaps &= (
1324 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_VERTEXFOG |
1325 D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER);
1327 caps->dpcLineCaps.dwMiscCaps &= (
1328 D3DPMISCCAPS_MASKPLANES | D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP |
1329 D3DPMISCCAPS_CONFORMANT | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW |
1330 D3DPMISCCAPS_CULLCCW);
1332 caps->dpcLineCaps.dwRasterCaps &= (
1333 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ROP2 | D3DPRASTERCAPS_XOR |
1334 D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_SUBPIXEL |
1335 D3DPRASTERCAPS_SUBPIXELX | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE |
1336 D3DPRASTERCAPS_STIPPLE | D3DPRASTERCAPS_ANTIALIASSORTDEPENDENT | D3DPRASTERCAPS_ANTIALIASSORTINDEPENDENT |
1337 D3DPRASTERCAPS_ANTIALIASEDGES | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBIAS |
1338 D3DPRASTERCAPS_ZBUFFERLESSHSR | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY |
1339 D3DPRASTERCAPS_WBUFFER | D3DPRASTERCAPS_TRANSLUCENTSORTINDEPENDENT | D3DPRASTERCAPS_WFOG |
1340 D3DPRASTERCAPS_ZFOG);
1342 caps->dpcLineCaps.dwZCmpCaps &= (
1343 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
1344 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
1345 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
1347 caps->dpcLineCaps.dwSrcBlendCaps &= (
1348 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
1349 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
1350 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
1351 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
1352 D3DPBLENDCAPS_BOTHINVSRCALPHA);
1354 caps->dpcLineCaps.dwDestBlendCaps &= (
1355 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
1356 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
1357 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
1358 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
1359 D3DPBLENDCAPS_BOTHINVSRCALPHA);
1361 caps->dpcLineCaps.dwAlphaCmpCaps &= (
1362 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
1363 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
1364 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
1366 caps->dpcLineCaps.dwShadeCaps &= (
1367 D3DPSHADECAPS_COLORFLATMONO | D3DPSHADECAPS_COLORFLATRGB | D3DPSHADECAPS_COLORGOURAUDMONO |
1368 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_COLORPHONGMONO | D3DPSHADECAPS_COLORPHONGRGB |
1369 D3DPSHADECAPS_SPECULARFLATMONO | D3DPSHADECAPS_SPECULARFLATRGB | D3DPSHADECAPS_SPECULARGOURAUDMONO |
1370 D3DPSHADECAPS_SPECULARGOURAUDRGB | D3DPSHADECAPS_SPECULARPHONGMONO | D3DPSHADECAPS_SPECULARPHONGRGB |
1371 D3DPSHADECAPS_ALPHAFLATBLEND | D3DPSHADECAPS_ALPHAFLATSTIPPLED | D3DPSHADECAPS_ALPHAGOURAUDBLEND |
1372 D3DPSHADECAPS_ALPHAGOURAUDSTIPPLED | D3DPSHADECAPS_ALPHAPHONGBLEND | D3DPSHADECAPS_ALPHAPHONGSTIPPLED |
1373 D3DPSHADECAPS_FOGFLAT | D3DPSHADECAPS_FOGGOURAUD | D3DPSHADECAPS_FOGPHONG);
1375 caps->dpcLineCaps.dwTextureCaps &= (
1376 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
1377 D3DPTEXTURECAPS_TRANSPARENCY | D3DPTEXTURECAPS_BORDER | D3DPTEXTURECAPS_SQUAREONLY |
1378 D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE | D3DPTEXTURECAPS_ALPHAPALETTE| D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
1379 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_COLORKEYBLEND);
1381 caps->dpcLineCaps.dwTextureFilterCaps &= (
1382 D3DPTFILTERCAPS_NEAREST | D3DPTFILTERCAPS_LINEAR | D3DPTFILTERCAPS_MIPNEAREST |
1383 D3DPTFILTERCAPS_MIPLINEAR | D3DPTFILTERCAPS_LINEARMIPNEAREST | D3DPTFILTERCAPS_LINEARMIPLINEAR |
1384 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
1385 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
1386 D3DPTFILTERCAPS_MAGFLINEAR | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC |
1387 D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC);
1389 caps->dpcLineCaps.dwTextureAddressCaps &= (
1390 D3DPTADDRESSCAPS_WRAP | D3DPTADDRESSCAPS_MIRROR | D3DPTADDRESSCAPS_CLAMP |
1391 D3DPTADDRESSCAPS_BORDER | D3DPTADDRESSCAPS_INDEPENDENTUV);
1393 if (!(caps->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2))
1395 /* DirectX7 always has the np2 flag set, no matter what the card
1396 * supports. Some old games (Rollcage) check the caps incorrectly.
1397 * If wined3d supports nonpow2 textures it also has np2 conditional
1398 * support. */
1399 caps->dpcLineCaps.dwTextureCaps |= D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL;
1402 /* Fill the missing members, and do some fixup */
1403 caps->dpcLineCaps.dwSize = sizeof(caps->dpcLineCaps);
1404 caps->dpcLineCaps.dwTextureBlendCaps = D3DPTBLENDCAPS_ADD
1405 | D3DPTBLENDCAPS_MODULATEMASK
1406 | D3DPTBLENDCAPS_COPY
1407 | D3DPTBLENDCAPS_DECAL
1408 | D3DPTBLENDCAPS_DECALALPHA
1409 | D3DPTBLENDCAPS_DECALMASK
1410 | D3DPTBLENDCAPS_MODULATE
1411 | D3DPTBLENDCAPS_MODULATEALPHA;
1412 caps->dpcLineCaps.dwStippleWidth = 32;
1413 caps->dpcLineCaps.dwStippleHeight = 32;
1414 /* Use the same for the TriCaps */
1415 caps->dpcTriCaps = caps->dpcLineCaps;
1417 caps->dwDeviceRenderBitDepth = DDBD_16 | DDBD_24 | DDBD_32;
1418 caps->dwDeviceZBufferBitDepth = DDBD_16 | DDBD_24;
1419 caps->dwMinTextureWidth = 1;
1420 caps->dwMinTextureHeight = 1;
1422 /* Convert DWORDs safely to WORDs */
1423 caps->wMaxTextureBlendStages = min(wined3d_caps.MaxTextureBlendStages, 0xffff);
1424 caps->wMaxSimultaneousTextures = min(wined3d_caps.MaxSimultaneousTextures, 0xffff);
1425 caps->wMaxUserClipPlanes = min(wined3d_caps.MaxUserClipPlanes, D3DMAXUSERCLIPPLANES);
1426 caps->wMaxVertexBlendMatrices = min(wined3d_caps.MaxVertexBlendMatrices, 0xffff);
1428 caps->deviceGUID = IID_IDirect3DTnLHalDevice;
1430 caps->dwReserved1 = 0;
1431 caps->dwReserved2 = 0;
1432 caps->dwReserved3 = 0;
1433 caps->dwReserved4 = 0;
1435 return DD_OK;
1438 /*****************************************************************************
1439 * IDirectDraw7::GetCaps
1441 * Returns the drives capabilities
1443 * Used for version 1, 2, 4 and 7
1445 * Params:
1446 * DriverCaps: Structure to write the Hardware accelerated caps to
1447 * HelCaps: Structure to write the emulation caps to
1449 * Returns
1450 * This implementation returns DD_OK only
1452 *****************************************************************************/
1453 static HRESULT WINAPI ddraw7_GetCaps(IDirectDraw7 *iface, DDCAPS *DriverCaps, DDCAPS *HELCaps)
1455 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1456 DDCAPS caps;
1457 WINED3DCAPS winecaps;
1458 HRESULT hr;
1459 DDSCAPS2 ddscaps = {0, 0, 0, {0}};
1461 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, DriverCaps, HELCaps);
1463 /* One structure must be != NULL */
1464 if (!DriverCaps && !HELCaps)
1466 WARN("Invalid parameters.\n");
1467 return DDERR_INVALIDPARAMS;
1470 memset(&caps, 0, sizeof(caps));
1471 memset(&winecaps, 0, sizeof(winecaps));
1472 caps.dwSize = sizeof(caps);
1474 wined3d_mutex_lock();
1475 hr = wined3d_device_get_device_caps(ddraw->wined3d_device, &winecaps);
1476 if (FAILED(hr))
1478 WARN("Failed to get device caps, %#x.\n", hr);
1479 wined3d_mutex_unlock();
1480 return hr;
1483 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
1484 if (FAILED(hr))
1486 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
1487 wined3d_mutex_unlock();
1488 return hr;
1491 hr = IDirectDraw7_GetFourCCCodes(iface, &caps.dwNumFourCCCodes, NULL);
1492 wined3d_mutex_unlock();
1493 if (FAILED(hr))
1495 WARN("IDirectDraw7::GetFourCCCodes failed\n");
1496 return hr;
1499 caps.dwCaps = winecaps.ddraw_caps.caps;
1500 caps.dwCaps2 = winecaps.ddraw_caps.caps2;
1501 caps.dwCKeyCaps = winecaps.ddraw_caps.color_key_caps;
1502 caps.dwFXCaps = winecaps.ddraw_caps.fx_caps;
1503 caps.dwPalCaps = DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE;
1504 caps.ddsCaps.dwCaps = winecaps.ddraw_caps.dds_caps;
1505 caps.dwSVBCaps = winecaps.ddraw_caps.svb_caps;
1506 caps.dwSVBCKeyCaps = winecaps.ddraw_caps.svb_color_key_caps;
1507 caps.dwSVBFXCaps = winecaps.ddraw_caps.svb_fx_caps;
1508 caps.dwVSBCaps = winecaps.ddraw_caps.vsb_caps;
1509 caps.dwVSBCKeyCaps = winecaps.ddraw_caps.vsb_color_key_caps;
1510 caps.dwVSBFXCaps = winecaps.ddraw_caps.vsb_fx_caps;
1511 caps.dwSSBCaps = winecaps.ddraw_caps.ssb_caps;
1512 caps.dwSSBCKeyCaps = winecaps.ddraw_caps.ssb_color_key_caps;
1513 caps.dwSSBFXCaps = winecaps.ddraw_caps.ssb_fx_caps;
1515 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
1516 caps.dwAlignStrideAlign = DDRAW_STRIDE_ALIGNMENT;
1518 if(DriverCaps)
1520 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
1521 if (TRACE_ON(ddraw))
1523 TRACE("Driver Caps :\n");
1524 DDRAW_dump_DDCAPS(DriverCaps);
1528 if(HELCaps)
1530 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
1531 if (TRACE_ON(ddraw))
1533 TRACE("HEL Caps :\n");
1534 DDRAW_dump_DDCAPS(HELCaps);
1538 return DD_OK;
1541 static HRESULT WINAPI ddraw4_GetCaps(IDirectDraw4 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1543 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1545 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1547 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1550 static HRESULT WINAPI ddraw2_GetCaps(IDirectDraw2 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1552 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1554 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1556 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1559 static HRESULT WINAPI ddraw1_GetCaps(IDirectDraw *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1561 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1563 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1565 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1568 /*****************************************************************************
1569 * IDirectDraw7::Compact
1571 * No idea what it does, MSDN says it's not implemented.
1573 * Returns
1574 * DD_OK, but this is unchecked
1576 *****************************************************************************/
1577 static HRESULT WINAPI ddraw7_Compact(IDirectDraw7 *iface)
1579 TRACE("iface %p.\n", iface);
1581 return DD_OK;
1584 static HRESULT WINAPI ddraw4_Compact(IDirectDraw4 *iface)
1586 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1588 TRACE("iface %p.\n", iface);
1590 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1593 static HRESULT WINAPI ddraw2_Compact(IDirectDraw2 *iface)
1595 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1597 TRACE("iface %p.\n", iface);
1599 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1602 static HRESULT WINAPI ddraw1_Compact(IDirectDraw *iface)
1604 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1606 TRACE("iface %p.\n", iface);
1608 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1611 /*****************************************************************************
1612 * IDirectDraw7::GetDisplayMode
1614 * Returns information about the current display mode
1616 * Exists in versions 1, 2, 4 and 7
1618 * Params:
1619 * DDSD: Address of a surface description structure to write the info to
1621 * Returns
1622 * DD_OK
1624 *****************************************************************************/
1625 static HRESULT WINAPI ddraw7_GetDisplayMode(IDirectDraw7 *iface, DDSURFACEDESC2 *DDSD)
1627 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1628 struct wined3d_display_mode mode;
1629 HRESULT hr;
1630 DWORD Size;
1632 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
1634 wined3d_mutex_lock();
1635 /* This seems sane */
1636 if (!DDSD)
1638 wined3d_mutex_unlock();
1639 return DDERR_INVALIDPARAMS;
1642 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
1644 ERR("Failed to get display mode, hr %#x.\n", hr);
1645 wined3d_mutex_unlock();
1646 return hr;
1649 Size = DDSD->dwSize;
1650 memset(DDSD, 0, Size);
1652 DDSD->dwSize = Size;
1653 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
1654 DDSD->dwWidth = mode.width;
1655 DDSD->dwHeight = mode.height;
1656 DDSD->u2.dwRefreshRate = 60;
1657 DDSD->ddsCaps.dwCaps = 0;
1658 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
1659 ddrawformat_from_wined3dformat(&DDSD->u4.ddpfPixelFormat, mode.format_id);
1660 DDSD->u1.lPitch = mode.width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
1662 if(TRACE_ON(ddraw))
1664 TRACE("Returning surface desc :\n");
1665 DDRAW_dump_surface_desc(DDSD);
1668 wined3d_mutex_unlock();
1670 return DD_OK;
1673 static HRESULT WINAPI ddraw4_GetDisplayMode(IDirectDraw4 *iface, DDSURFACEDESC2 *surface_desc)
1675 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1677 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1679 return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, surface_desc);
1682 static HRESULT WINAPI ddraw2_GetDisplayMode(IDirectDraw2 *iface, DDSURFACEDESC *surface_desc)
1684 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1686 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1688 /* FIXME: Test sizes, properly convert surface_desc */
1689 return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1692 static HRESULT WINAPI ddraw1_GetDisplayMode(IDirectDraw *iface, DDSURFACEDESC *surface_desc)
1694 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1696 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1698 /* FIXME: Test sizes, properly convert surface_desc */
1699 return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1702 /*****************************************************************************
1703 * IDirectDraw7::GetFourCCCodes
1705 * Returns an array of supported FourCC codes.
1707 * Exists in versions 1, 2, 4 and 7
1709 * Params:
1710 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
1711 * of enumerated codes
1712 * Codes: Pointer to an array of DWORDs where the supported codes are written
1713 * to
1715 * Returns
1716 * Always returns DD_OK, as it's a stub for now
1718 *****************************************************************************/
1719 static HRESULT WINAPI ddraw7_GetFourCCCodes(IDirectDraw7 *iface, DWORD *NumCodes, DWORD *Codes)
1721 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1722 static const enum wined3d_format_id formats[] =
1724 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
1725 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
1726 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
1728 struct wined3d_display_mode mode;
1729 DWORD count = 0, i, outsize;
1730 HRESULT hr;
1732 TRACE("iface %p, codes_count %p, codes %p.\n", iface, NumCodes, Codes);
1734 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
1736 ERR("Failed to get display mode, hr %#x.\n", hr);
1737 return hr;
1740 outsize = NumCodes && Codes ? *NumCodes : 0;
1742 for (i = 0; i < (sizeof(formats) / sizeof(formats[0])); ++i)
1744 if (SUCCEEDED(wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, WINED3D_DEVICE_TYPE_HAL,
1745 mode.format_id, 0, WINED3D_RTYPE_TEXTURE_2D, formats[i])))
1747 if (count < outsize)
1748 Codes[count] = formats[i];
1749 ++count;
1752 if(NumCodes) {
1753 TRACE("Returning %u FourCC codes\n", count);
1754 *NumCodes = count;
1757 return DD_OK;
1760 static HRESULT WINAPI ddraw4_GetFourCCCodes(IDirectDraw4 *iface, DWORD *codes_count, DWORD *codes)
1762 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1764 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1766 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1769 static HRESULT WINAPI ddraw2_GetFourCCCodes(IDirectDraw2 *iface, DWORD *codes_count, DWORD *codes)
1771 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1773 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1775 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1778 static HRESULT WINAPI ddraw1_GetFourCCCodes(IDirectDraw *iface, DWORD *codes_count, DWORD *codes)
1780 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1782 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1784 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1787 static HRESULT WINAPI ddraw7_GetMonitorFrequency(IDirectDraw7 *iface, DWORD *frequency)
1789 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1790 struct wined3d_display_mode mode;
1791 HRESULT hr;
1793 TRACE("iface %p, frequency %p.\n", iface, frequency);
1795 wined3d_mutex_lock();
1796 hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL);
1797 wined3d_mutex_unlock();
1798 if (FAILED(hr))
1800 WARN("Failed to get display mode, hr %#x.\n", hr);
1801 return hr;
1804 *frequency = mode.refresh_rate;
1806 return DD_OK;
1809 static HRESULT WINAPI ddraw4_GetMonitorFrequency(IDirectDraw4 *iface, DWORD *frequency)
1811 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1813 TRACE("iface %p, frequency %p.\n", iface, frequency);
1815 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1818 static HRESULT WINAPI ddraw2_GetMonitorFrequency(IDirectDraw2 *iface, DWORD *frequency)
1820 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1822 TRACE("iface %p, frequency %p.\n", iface, frequency);
1824 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1827 static HRESULT WINAPI ddraw1_GetMonitorFrequency(IDirectDraw *iface, DWORD *frequency)
1829 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1831 TRACE("iface %p, frequency %p.\n", iface, frequency);
1833 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1836 static HRESULT WINAPI ddraw7_GetVerticalBlankStatus(IDirectDraw7 *iface, BOOL *status)
1838 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1839 struct wined3d_raster_status raster_status;
1840 HRESULT hr;
1842 TRACE("iface %p, status %p.\n", iface, status);
1844 if(!status)
1845 return DDERR_INVALIDPARAMS;
1847 wined3d_mutex_lock();
1848 hr = wined3d_get_adapter_raster_status(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &raster_status);
1849 wined3d_mutex_unlock();
1850 if (FAILED(hr))
1852 WARN("Failed to get raster status, hr %#x.\n", hr);
1853 return hr;
1856 *status = raster_status.in_vblank;
1858 return DD_OK;
1861 static HRESULT WINAPI ddraw4_GetVerticalBlankStatus(IDirectDraw4 *iface, BOOL *status)
1863 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1865 TRACE("iface %p, status %p.\n", iface, status);
1867 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1870 static HRESULT WINAPI ddraw2_GetVerticalBlankStatus(IDirectDraw2 *iface, BOOL *status)
1872 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1874 TRACE("iface %p, status %p.\n", iface, status);
1876 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1879 static HRESULT WINAPI ddraw1_GetVerticalBlankStatus(IDirectDraw *iface, BOOL *status)
1881 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1883 TRACE("iface %p, status %p.\n", iface, status);
1885 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1888 /*****************************************************************************
1889 * IDirectDraw7::GetAvailableVidMem
1891 * Returns the total and free video memory
1893 * Params:
1894 * caps: Specifies the memory type asked for
1895 * total: Pointer to a DWORD to be filled with the total memory
1896 * free: Pointer to a DWORD to be filled with the free memory
1898 * Returns
1899 * DD_OK on success
1900 * DDERR_INVALIDPARAMS if free and total are NULL
1902 *****************************************************************************/
1903 static HRESULT WINAPI ddraw7_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *caps, DWORD *total,
1904 DWORD *free)
1906 unsigned int framebuffer_size, total_vidmem, free_vidmem;
1907 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1908 struct wined3d_display_mode mode;
1909 HRESULT hr = DD_OK;
1911 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1913 if (!total && !free)
1914 return DDERR_INVALIDPARAMS;
1916 if (TRACE_ON(ddraw))
1918 TRACE("Asked for memory with description: ");
1919 DDRAW_dump_DDSCAPS2(caps);
1921 wined3d_mutex_lock();
1923 /* Todo: System memory vs local video memory vs non-local video memory
1924 * The MSDN also mentions differences between texture memory and other
1925 * resources, but that's not important
1928 /* Some applications (e.g. 3DMark 2000) assume that the reported amount of
1929 * video memory doesn't include the memory used by the default framebuffer.
1931 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
1933 WARN("Failed to get display mode, hr %#x.\n", hr);
1934 wined3d_mutex_unlock();
1935 return hr;
1937 framebuffer_size = wined3d_calculate_format_pitch(ddraw->wined3d, WINED3DADAPTER_DEFAULT,
1938 mode.format_id, mode.width);
1939 framebuffer_size *= mode.height;
1941 if (free)
1943 free_vidmem = wined3d_device_get_available_texture_mem(ddraw->wined3d_device);
1944 *free = framebuffer_size > free_vidmem ? 0 : free_vidmem - framebuffer_size;
1945 TRACE("Free video memory %#x.\n", *free);
1948 if (total)
1950 struct wined3d_adapter_identifier desc = {0};
1952 hr = wined3d_get_adapter_identifier(ddraw->wined3d, WINED3DADAPTER_DEFAULT, 0, &desc);
1953 total_vidmem = min(UINT_MAX, desc.video_memory);
1954 *total = framebuffer_size > total_vidmem ? 0 : total_vidmem - framebuffer_size;
1955 TRACE("Total video memory %#x.\n", *total);
1958 wined3d_mutex_unlock();
1960 return hr;
1963 static HRESULT WINAPI ddraw4_GetAvailableVidMem(IDirectDraw4 *iface,
1964 DDSCAPS2 *caps, DWORD *total, DWORD *free)
1966 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1968 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1970 return ddraw7_GetAvailableVidMem(&ddraw->IDirectDraw7_iface, caps, total, free);
1973 static HRESULT WINAPI ddraw2_GetAvailableVidMem(IDirectDraw2 *iface,
1974 DDSCAPS *caps, DWORD *total, DWORD *free)
1976 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1977 DDSCAPS2 caps2;
1979 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1981 DDRAW_Convert_DDSCAPS_1_To_2(caps, &caps2);
1982 return ddraw7_GetAvailableVidMem(&ddraw->IDirectDraw7_iface, &caps2, total, free);
1985 /*****************************************************************************
1986 * IDirectDraw7::Initialize
1988 * Initializes a DirectDraw interface.
1990 * Params:
1991 * GUID: Interface identifier. Well, don't know what this is really good
1992 * for
1994 * Returns
1995 * Returns DD_OK on the first call,
1996 * DDERR_ALREADYINITIALIZED on repeated calls
1998 *****************************************************************************/
1999 static HRESULT WINAPI ddraw7_Initialize(IDirectDraw7 *iface, GUID *guid)
2001 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2003 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
2005 if (ddraw->flags & DDRAW_INITIALIZED)
2006 return DDERR_ALREADYINITIALIZED;
2008 /* FIXME: To properly take the GUID into account we should call
2009 * ddraw_init() here instead of in DDRAW_Create(). */
2010 if (guid)
2011 FIXME("Ignoring guid %s.\n", debugstr_guid(guid));
2013 ddraw->flags |= DDRAW_INITIALIZED;
2014 return DD_OK;
2017 static HRESULT WINAPI ddraw4_Initialize(IDirectDraw4 *iface, GUID *guid)
2019 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2021 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
2023 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
2026 static HRESULT WINAPI ddraw2_Initialize(IDirectDraw2 *iface, GUID *guid)
2028 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2030 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
2032 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
2035 static HRESULT WINAPI ddraw1_Initialize(IDirectDraw *iface, GUID *guid)
2037 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2039 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
2041 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
2044 static HRESULT WINAPI d3d1_Initialize(IDirect3D *iface, REFIID riid)
2046 TRACE("iface %p, riid %s.\n", iface, debugstr_guid(riid));
2048 return DDERR_ALREADYINITIALIZED;
2051 /*****************************************************************************
2052 * IDirectDraw7::FlipToGDISurface
2054 * "Makes the surface that the GDI writes to the primary surface"
2055 * Looks like some windows specific thing we don't have to care about.
2056 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
2057 * show error boxes ;)
2058 * Well, just return DD_OK.
2060 * Returns:
2061 * Always returns DD_OK
2063 *****************************************************************************/
2064 static HRESULT WINAPI ddraw7_FlipToGDISurface(IDirectDraw7 *iface)
2066 FIXME("iface %p stub!\n", iface);
2068 return DD_OK;
2071 static HRESULT WINAPI ddraw4_FlipToGDISurface(IDirectDraw4 *iface)
2073 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2075 TRACE("iface %p.\n", iface);
2077 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
2080 static HRESULT WINAPI ddraw2_FlipToGDISurface(IDirectDraw2 *iface)
2082 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2084 TRACE("iface %p.\n", iface);
2086 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
2089 static HRESULT WINAPI ddraw1_FlipToGDISurface(IDirectDraw *iface)
2091 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2093 TRACE("iface %p.\n", iface);
2095 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
2098 /*****************************************************************************
2099 * IDirectDraw7::WaitForVerticalBlank
2101 * This method allows applications to get in sync with the vertical blank
2102 * interval.
2103 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
2104 * redraw the screen, most likely because of this stub
2106 * Parameters:
2107 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
2108 * or DDWAITVB_BLOCKEND
2109 * h: Not used, according to MSDN
2111 * Returns:
2112 * Always returns DD_OK
2114 *****************************************************************************/
2115 static HRESULT WINAPI ddraw7_WaitForVerticalBlank(IDirectDraw7 *iface, DWORD Flags, HANDLE event)
2117 static BOOL hide;
2119 TRACE("iface %p, flags %#x, event %p.\n", iface, Flags, event);
2121 /* This function is called often, so print the fixme only once */
2122 if(!hide)
2124 FIXME("iface %p, flags %#x, event %p stub!\n", iface, Flags, event);
2125 hide = TRUE;
2128 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
2129 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
2130 return DDERR_UNSUPPORTED; /* unchecked */
2132 return DD_OK;
2135 static HRESULT WINAPI ddraw4_WaitForVerticalBlank(IDirectDraw4 *iface, DWORD flags, HANDLE event)
2137 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2139 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
2141 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
2144 static HRESULT WINAPI ddraw2_WaitForVerticalBlank(IDirectDraw2 *iface, DWORD flags, HANDLE event)
2146 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2148 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
2150 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
2153 static HRESULT WINAPI ddraw1_WaitForVerticalBlank(IDirectDraw *iface, DWORD flags, HANDLE event)
2155 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2157 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
2159 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
2162 static HRESULT WINAPI ddraw7_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
2164 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2165 struct wined3d_raster_status raster_status;
2166 HRESULT hr;
2168 TRACE("iface %p, line %p.\n", iface, Scanline);
2170 wined3d_mutex_lock();
2171 hr = wined3d_get_adapter_raster_status(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &raster_status);
2172 wined3d_mutex_unlock();
2173 if (FAILED(hr))
2175 WARN("Failed to get raster status, hr %#x.\n", hr);
2176 return hr;
2179 *Scanline = raster_status.scan_line;
2181 if (raster_status.in_vblank)
2182 return DDERR_VERTICALBLANKINPROGRESS;
2184 return DD_OK;
2187 static HRESULT WINAPI ddraw4_GetScanLine(IDirectDraw4 *iface, DWORD *line)
2189 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2191 TRACE("iface %p, line %p.\n", iface, line);
2193 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
2196 static HRESULT WINAPI ddraw2_GetScanLine(IDirectDraw2 *iface, DWORD *line)
2198 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2200 TRACE("iface %p, line %p.\n", iface, line);
2202 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
2205 static HRESULT WINAPI ddraw1_GetScanLine(IDirectDraw *iface, DWORD *line)
2207 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2209 TRACE("iface %p, line %p.\n", iface, line);
2211 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
2214 static HRESULT WINAPI ddraw7_TestCooperativeLevel(IDirectDraw7 *iface)
2216 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2218 TRACE("iface %p.\n", iface);
2220 return ddraw->device_state == DDRAW_DEVICE_STATE_LOST ? DDERR_NOEXCLUSIVEMODE : DD_OK;
2223 static HRESULT WINAPI ddraw4_TestCooperativeLevel(IDirectDraw4 *iface)
2225 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2227 TRACE("iface %p.\n", iface);
2229 return ddraw7_TestCooperativeLevel(&ddraw->IDirectDraw7_iface);
2232 /*****************************************************************************
2233 * IDirectDraw7::GetGDISurface
2235 * Returns the surface that GDI is treating as the primary surface.
2236 * For Wine this is the front buffer
2238 * Params:
2239 * GDISurface: Address to write the surface pointer to
2241 * Returns:
2242 * DD_OK if the surface was found
2243 * DDERR_NOTFOUND if the GDI surface wasn't found
2245 *****************************************************************************/
2246 static HRESULT WINAPI ddraw7_GetGDISurface(IDirectDraw7 *iface, IDirectDrawSurface7 **GDISurface)
2248 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2250 TRACE("iface %p, surface %p.\n", iface, GDISurface);
2252 wined3d_mutex_lock();
2254 if (!(*GDISurface = &ddraw->primary->IDirectDrawSurface7_iface))
2256 WARN("Primary not created yet.\n");
2257 wined3d_mutex_unlock();
2258 return DDERR_NOTFOUND;
2260 IDirectDrawSurface7_AddRef(*GDISurface);
2262 wined3d_mutex_unlock();
2264 return DD_OK;
2267 static HRESULT WINAPI ddraw4_GetGDISurface(IDirectDraw4 *iface, IDirectDrawSurface4 **surface)
2269 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2270 struct ddraw_surface *surface_impl;
2271 IDirectDrawSurface7 *surface7;
2272 HRESULT hr;
2274 TRACE("iface %p, surface %p.\n", iface, surface);
2276 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2277 if (FAILED(hr))
2279 *surface = NULL;
2280 return hr;
2282 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2283 *surface = &surface_impl->IDirectDrawSurface4_iface;
2284 IDirectDrawSurface4_AddRef(*surface);
2285 IDirectDrawSurface7_Release(surface7);
2287 return hr;
2290 static HRESULT WINAPI ddraw2_GetGDISurface(IDirectDraw2 *iface, IDirectDrawSurface **surface)
2292 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2293 struct ddraw_surface *surface_impl;
2294 IDirectDrawSurface7 *surface7;
2295 HRESULT hr;
2297 TRACE("iface %p, surface %p.\n", iface, surface);
2299 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2300 if (FAILED(hr))
2302 *surface = NULL;
2303 return hr;
2305 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2306 *surface = &surface_impl->IDirectDrawSurface_iface;
2307 IDirectDrawSurface_AddRef(*surface);
2308 IDirectDrawSurface7_Release(surface7);
2310 return hr;
2313 static HRESULT WINAPI ddraw1_GetGDISurface(IDirectDraw *iface, IDirectDrawSurface **surface)
2315 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2316 struct ddraw_surface *surface_impl;
2317 IDirectDrawSurface7 *surface7;
2318 HRESULT hr;
2320 TRACE("iface %p, surface %p.\n", iface, surface);
2322 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2323 if (FAILED(hr))
2325 *surface = NULL;
2326 return hr;
2328 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2329 *surface = &surface_impl->IDirectDrawSurface_iface;
2330 IDirectDrawSurface_AddRef(*surface);
2331 IDirectDrawSurface7_Release(surface7);
2333 return hr;
2336 struct displaymodescallback_context
2338 LPDDENUMMODESCALLBACK func;
2339 void *context;
2342 static HRESULT CALLBACK EnumDisplayModesCallbackThunk(DDSURFACEDESC2 *surface_desc, void *context)
2344 struct displaymodescallback_context *cbcontext = context;
2345 DDSURFACEDESC desc;
2347 DDSD2_to_DDSD(surface_desc, &desc);
2348 return cbcontext->func(&desc, cbcontext->context);
2351 /*****************************************************************************
2352 * IDirectDraw7::EnumDisplayModes
2354 * Enumerates the supported Display modes. The modes can be filtered with
2355 * the DDSD parameter.
2357 * Params:
2358 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES. For old ddraw
2359 * versions (3 and older?) this is reserved and must be 0.
2360 * DDSD: Surface description to filter the modes
2361 * Context: Pointer passed back to the callback function
2362 * cb: Application-provided callback function
2364 * Returns:
2365 * DD_OK on success
2366 * DDERR_INVALIDPARAMS if the callback wasn't set
2368 *****************************************************************************/
2369 static HRESULT WINAPI ddraw7_EnumDisplayModes(IDirectDraw7 *iface, DWORD Flags,
2370 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMMODESCALLBACK2 cb)
2372 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2373 struct wined3d_display_mode *enum_modes = NULL;
2374 struct wined3d_display_mode mode;
2375 unsigned int modenum, fmt;
2376 DDSURFACEDESC2 callback_sd;
2377 unsigned enum_mode_count = 0, enum_mode_array_size = 16;
2378 DDPIXELFORMAT pixelformat;
2380 static const enum wined3d_format_id checkFormatList[] =
2382 WINED3DFMT_B8G8R8X8_UNORM,
2383 WINED3DFMT_B5G6R5_UNORM,
2384 WINED3DFMT_P8_UINT,
2387 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2388 iface, Flags, DDSD, Context, cb);
2390 if (!cb)
2391 return DDERR_INVALIDPARAMS;
2393 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(*enum_modes) * enum_mode_array_size);
2394 if (!enum_modes) return DDERR_OUTOFMEMORY;
2396 wined3d_mutex_lock();
2398 pixelformat.dwSize = sizeof(pixelformat);
2399 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
2401 modenum = 0;
2402 while (wined3d_enum_adapter_modes(ddraw->wined3d, WINED3DADAPTER_DEFAULT, checkFormatList[fmt],
2403 WINED3D_SCANLINE_ORDERING_UNKNOWN, modenum++, &mode) == WINED3D_OK)
2405 BOOL found = FALSE;
2406 unsigned i;
2408 ddrawformat_from_wined3dformat(&pixelformat, mode.format_id);
2409 if (DDSD)
2411 if (DDSD->dwFlags & DDSD_WIDTH && mode.width != DDSD->dwWidth)
2412 continue;
2413 if (DDSD->dwFlags & DDSD_HEIGHT && mode.height != DDSD->dwHeight)
2414 continue;
2415 if (DDSD->dwFlags & DDSD_REFRESHRATE && mode.refresh_rate != DDSD->u2.dwRefreshRate)
2416 continue;
2417 if (DDSD->dwFlags & DDSD_PIXELFORMAT
2418 && pixelformat.u1.dwRGBBitCount != DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount)
2419 continue;
2422 /* DX docs state EnumDisplayMode should return only unique modes */
2423 for (i = 0; i < enum_mode_count; i++)
2425 if (enum_modes[i].width == mode.width && enum_modes[i].height == mode.height
2426 && enum_modes[i].format_id == mode.format_id
2427 && (enum_modes[i].refresh_rate == mode.refresh_rate || !(Flags & DDEDM_REFRESHRATES)))
2429 found = TRUE;
2430 break;
2433 if(found) continue;
2435 memset(&callback_sd, 0, sizeof(callback_sd));
2436 callback_sd.dwSize = sizeof(callback_sd);
2437 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2439 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH|DDSD_REFRESHRATE;
2440 if (Flags & DDEDM_REFRESHRATES)
2441 callback_sd.u2.dwRefreshRate = mode.refresh_rate;
2443 callback_sd.dwWidth = mode.width;
2444 callback_sd.dwHeight = mode.height;
2446 callback_sd.u4.ddpfPixelFormat=pixelformat;
2448 /* Calc pitch and DWORD align like MSDN says */
2449 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.width;
2450 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
2452 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
2453 callback_sd.u2.dwRefreshRate);
2455 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
2457 TRACE("Application asked to terminate the enumeration\n");
2458 HeapFree(GetProcessHeap(), 0, enum_modes);
2459 wined3d_mutex_unlock();
2460 return DD_OK;
2463 if (enum_mode_count == enum_mode_array_size)
2465 struct wined3d_display_mode *new_enum_modes;
2467 enum_mode_array_size *= 2;
2468 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes,
2469 sizeof(*new_enum_modes) * enum_mode_array_size);
2470 if (!new_enum_modes)
2472 HeapFree(GetProcessHeap(), 0, enum_modes);
2473 wined3d_mutex_unlock();
2474 return DDERR_OUTOFMEMORY;
2477 enum_modes = new_enum_modes;
2479 enum_modes[enum_mode_count++] = mode;
2483 TRACE("End of enumeration\n");
2484 HeapFree(GetProcessHeap(), 0, enum_modes);
2485 wined3d_mutex_unlock();
2487 return DD_OK;
2490 static HRESULT WINAPI ddraw4_EnumDisplayModes(IDirectDraw4 *iface, DWORD flags,
2491 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMMODESCALLBACK2 callback)
2493 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2495 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2496 iface, flags, surface_desc, context, callback);
2498 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags, surface_desc, context, callback);
2501 static HRESULT WINAPI ddraw2_EnumDisplayModes(IDirectDraw2 *iface, DWORD flags,
2502 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2504 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2505 struct displaymodescallback_context cbcontext;
2506 DDSURFACEDESC2 surface_desc2;
2508 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2509 iface, flags, surface_desc, context, callback);
2511 cbcontext.func = callback;
2512 cbcontext.context = context;
2514 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2515 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags,
2516 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2519 static HRESULT WINAPI ddraw1_EnumDisplayModes(IDirectDraw *iface, DWORD flags,
2520 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2522 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2523 struct displaymodescallback_context cbcontext;
2524 DDSURFACEDESC2 surface_desc2;
2526 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2527 iface, flags, surface_desc, context, callback);
2529 cbcontext.func = callback;
2530 cbcontext.context = context;
2532 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2533 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags,
2534 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2537 /*****************************************************************************
2538 * IDirectDraw7::EvaluateMode
2540 * Used with IDirectDraw7::StartModeTest to test video modes.
2541 * EvaluateMode is used to pass or fail a mode, and continue with the next
2542 * mode
2544 * Params:
2545 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
2546 * Timeout: Returns the amount of seconds left before the mode would have
2547 * been failed automatically
2549 * Returns:
2550 * This implementation always DD_OK, because it's a stub
2552 *****************************************************************************/
2553 static HRESULT WINAPI ddraw7_EvaluateMode(IDirectDraw7 *iface, DWORD Flags, DWORD *Timeout)
2555 FIXME("iface %p, flags %#x, timeout %p stub!\n", iface, Flags, Timeout);
2557 /* When implementing this, implement it in WineD3D */
2559 return DD_OK;
2562 /*****************************************************************************
2563 * IDirectDraw7::GetDeviceIdentifier
2565 * Returns the device identifier, which gives information about the driver
2566 * Our device identifier is defined at the beginning of this file.
2568 * Params:
2569 * DDDI: Address for the returned structure
2570 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
2572 * Returns:
2573 * On success it returns DD_OK
2574 * DDERR_INVALIDPARAMS if DDDI is NULL
2576 *****************************************************************************/
2577 static HRESULT WINAPI ddraw7_GetDeviceIdentifier(IDirectDraw7 *iface,
2578 DDDEVICEIDENTIFIER2 *DDDI, DWORD Flags)
2580 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2581 struct wined3d_adapter_identifier adapter_id;
2582 HRESULT hr = S_OK;
2584 TRACE("iface %p, device_identifier %p, flags %#x.\n", iface, DDDI, Flags);
2586 if (!DDDI)
2587 return DDERR_INVALIDPARAMS;
2589 if (Flags & DDGDI_GETHOSTIDENTIFIER)
2591 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
2592 * host adapter, if there's a secondary 3D adapter. This doesn't apply
2593 * to any modern hardware, nor is it interesting for Wine, so ignore it.
2594 * Size of DDDEVICEIDENTIFIER2 may be aligned to 8 bytes and thus 4
2595 * bytes too long. So only copy the relevant part of the structure
2598 memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
2599 return DD_OK;
2602 /* Drakan: Order of the Flame expects accurate D3D device information from ddraw */
2603 adapter_id.driver = DDDI->szDriver;
2604 adapter_id.driver_size = sizeof(DDDI->szDriver);
2605 adapter_id.description = DDDI->szDescription;
2606 adapter_id.description_size = sizeof(DDDI->szDescription);
2607 adapter_id.device_name_size = 0;
2608 wined3d_mutex_lock();
2609 hr = wined3d_get_adapter_identifier(ddraw->wined3d, WINED3DADAPTER_DEFAULT, 0x0, &adapter_id);
2610 wined3d_mutex_unlock();
2611 if (FAILED(hr)) return hr;
2613 DDDI->liDriverVersion = adapter_id.driver_version;
2614 DDDI->dwVendorId = adapter_id.vendor_id;
2615 DDDI->dwDeviceId = adapter_id.device_id;
2616 DDDI->dwSubSysId = adapter_id.subsystem_id;
2617 DDDI->dwRevision = adapter_id.revision;
2618 DDDI->guidDeviceIdentifier = adapter_id.device_identifier;
2619 DDDI->dwWHQLLevel = adapter_id.whql_level;
2620 return DD_OK;
2623 static HRESULT WINAPI ddraw4_GetDeviceIdentifier(IDirectDraw4 *iface,
2624 DDDEVICEIDENTIFIER *identifier, DWORD flags)
2626 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2627 DDDEVICEIDENTIFIER2 identifier2;
2628 HRESULT hr;
2630 TRACE("iface %p, identifier %p, flags %#x.\n", iface, identifier, flags);
2632 hr = ddraw7_GetDeviceIdentifier(&ddraw->IDirectDraw7_iface, &identifier2, flags);
2633 DDRAW_Convert_DDDEVICEIDENTIFIER_2_To_1(&identifier2, identifier);
2635 return hr;
2638 /*****************************************************************************
2639 * IDirectDraw7::GetSurfaceFromDC
2641 * Returns the Surface for a GDI device context handle.
2642 * Is this related to IDirectDrawSurface::GetDC ???
2644 * Params:
2645 * hdc: hdc to return the surface for
2646 * Surface: Address to write the surface pointer to
2648 * Returns:
2649 * Always returns DD_OK because it's a stub
2651 *****************************************************************************/
2652 static HRESULT WINAPI ddraw7_GetSurfaceFromDC(IDirectDraw7 *iface,
2653 HDC dc, IDirectDrawSurface7 **surface)
2655 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2656 struct ddraw_surface *surface_impl;
2658 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2660 if (!surface)
2661 return E_INVALIDARG;
2663 if (!dc)
2664 goto done;
2666 wined3d_mutex_lock();
2667 LIST_FOR_EACH_ENTRY(surface_impl, &ddraw->surface_list, struct ddraw_surface, surface_list_entry)
2669 if (surface_impl->dc != dc)
2670 continue;
2672 TRACE("Found surface %p for dc %p.\n", surface_impl, dc);
2673 *surface = &surface_impl->IDirectDrawSurface7_iface;
2674 IDirectDrawSurface7_AddRef(*surface);
2675 wined3d_mutex_unlock();
2676 return DD_OK;
2678 wined3d_mutex_unlock();
2680 done:
2681 TRACE("No surface found for dc %p.\n", dc);
2682 *surface = NULL;
2683 return DDERR_NOTFOUND;
2686 static HRESULT WINAPI ddraw4_GetSurfaceFromDC(IDirectDraw4 *iface, HDC dc,
2687 IDirectDrawSurface4 **surface)
2689 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2690 struct ddraw_surface *surface_impl;
2691 IDirectDrawSurface7 *surface7;
2692 HRESULT hr;
2694 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2696 if (!surface) return E_INVALIDARG;
2698 hr = ddraw7_GetSurfaceFromDC(&ddraw->IDirectDraw7_iface, dc, &surface7);
2699 if (FAILED(hr))
2701 *surface = NULL;
2702 return hr;
2704 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2705 /* Tests say this is true */
2706 *surface = (IDirectDrawSurface4 *)&surface_impl->IDirectDrawSurface_iface;
2707 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
2708 IDirectDrawSurface7_Release(surface7);
2710 return hr;
2713 static HRESULT CALLBACK restore_callback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *desc, void *context)
2715 IDirectDrawSurface_Restore(surface);
2716 IDirectDrawSurface_Release(surface);
2718 return DDENUMRET_OK;
2721 static HRESULT WINAPI ddraw7_RestoreAllSurfaces(IDirectDraw7 *iface)
2723 TRACE("iface %p.\n", iface);
2725 return IDirectDraw7_EnumSurfaces(iface, DDENUMSURFACES_ALL | DDENUMSURFACES_DOESEXIST,
2726 NULL, NULL, restore_callback);
2729 static HRESULT WINAPI ddraw4_RestoreAllSurfaces(IDirectDraw4 *iface)
2731 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2733 TRACE("iface %p.\n", iface);
2735 return ddraw7_RestoreAllSurfaces(&ddraw->IDirectDraw7_iface);
2738 /*****************************************************************************
2739 * IDirectDraw7::StartModeTest
2741 * Tests the specified video modes to update the system registry with
2742 * refresh rate information. StartModeTest starts the mode test,
2743 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
2744 * isn't called within 15 seconds, the mode is failed automatically
2746 * As refresh rates are handled by the X server, I don't think this
2747 * Method is important
2749 * Params:
2750 * Modes: An array of mode specifications
2751 * NumModes: The number of modes in Modes
2752 * Flags: Some flags...
2754 * Returns:
2755 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
2756 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
2757 * otherwise DD_OK
2759 *****************************************************************************/
2760 static HRESULT WINAPI ddraw7_StartModeTest(IDirectDraw7 *iface, SIZE *Modes, DWORD NumModes, DWORD Flags)
2762 FIXME("iface %p, modes %p, mode_count %u, flags %#x partial stub!\n",
2763 iface, Modes, NumModes, Flags);
2765 /* This looks sane */
2766 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
2768 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
2769 * As it is not, DDERR_TESTFINISHED is returned
2770 * (hopefully that's correct
2772 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
2773 * well, that value doesn't (yet) exist in the wine headers, so ignore it
2776 return DD_OK;
2779 static HRESULT WINAPI ddraw7_CreateSurface(IDirectDraw7 *iface, DDSURFACEDESC2 *surface_desc,
2780 IDirectDrawSurface7 **surface, IUnknown *outer_unknown)
2782 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2783 struct ddraw_surface *impl;
2784 HRESULT hr;
2786 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2787 iface, surface_desc, surface, outer_unknown);
2789 wined3d_mutex_lock();
2791 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2793 WARN("Cooperative level not set.\n");
2794 wined3d_mutex_unlock();
2795 return DDERR_NOCOOPERATIVELEVELSET;
2798 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
2800 WARN("Application supplied invalid surface descriptor\n");
2801 wined3d_mutex_unlock();
2802 return DDERR_INVALIDPARAMS;
2805 __TRY
2807 *surface = NULL;
2809 __EXCEPT_PAGE_FAULT
2811 WARN("Surface pointer %p is invalid.\n", surface);
2812 wined3d_mutex_unlock();
2813 return DDERR_INVALIDPARAMS;
2815 __ENDTRY;
2817 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
2819 if (TRACE_ON(ddraw))
2821 TRACE(" (%p) Requesting surface desc :\n", iface);
2822 DDRAW_dump_surface_desc(surface_desc);
2825 WARN("Application tried to create an explicit front or back buffer\n");
2826 wined3d_mutex_unlock();
2827 return DDERR_INVALIDCAPS;
2830 hr = ddraw_surface_create(ddraw, surface_desc, &impl, outer_unknown, 7);
2831 wined3d_mutex_unlock();
2832 if (FAILED(hr))
2833 return hr;
2835 *surface = &impl->IDirectDrawSurface7_iface;
2836 IDirectDraw7_AddRef(iface);
2837 impl->ifaceToRelease = (IUnknown *)iface;
2839 return hr;
2842 static HRESULT WINAPI ddraw4_CreateSurface(IDirectDraw4 *iface,
2843 DDSURFACEDESC2 *surface_desc, IDirectDrawSurface4 **surface, IUnknown *outer_unknown)
2845 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2846 struct ddraw_surface *impl;
2847 HRESULT hr;
2849 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2850 iface, surface_desc, surface, outer_unknown);
2852 wined3d_mutex_lock();
2854 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2856 WARN("Cooperative level not set.\n");
2857 wined3d_mutex_unlock();
2858 return DDERR_NOCOOPERATIVELEVELSET;
2861 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
2863 WARN("Application supplied invalid surface descriptor\n");
2864 wined3d_mutex_unlock();
2865 return DDERR_INVALIDPARAMS;
2868 __TRY
2870 *surface = NULL;
2872 __EXCEPT_PAGE_FAULT
2874 WARN("Surface pointer %p is invalid.\n", surface);
2875 wined3d_mutex_unlock();
2876 return DDERR_INVALIDPARAMS;
2878 __ENDTRY;
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(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_desc, &impl, outer_unknown, 4);
2894 wined3d_mutex_unlock();
2895 if (FAILED(hr))
2896 return hr;
2898 *surface = &impl->IDirectDrawSurface4_iface;
2899 IDirectDraw4_AddRef(iface);
2900 impl->ifaceToRelease = (IUnknown *)iface;
2902 return hr;
2905 static HRESULT WINAPI ddraw2_CreateSurface(IDirectDraw2 *iface,
2906 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
2908 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2909 struct ddraw_surface *impl;
2910 HRESULT hr;
2911 DDSURFACEDESC2 surface_desc2;
2913 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2914 iface, surface_desc, surface, outer_unknown);
2916 wined3d_mutex_lock();
2918 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2920 WARN("Cooperative level not set.\n");
2921 wined3d_mutex_unlock();
2922 return DDERR_NOCOOPERATIVELEVELSET;
2925 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
2927 WARN("Application supplied invalid surface descriptor\n");
2928 wined3d_mutex_unlock();
2929 return DDERR_INVALIDPARAMS;
2932 __TRY
2934 *surface = NULL;
2936 __EXCEPT_PAGE_FAULT
2938 WARN("Surface pointer %p is invalid.\n", surface);
2939 wined3d_mutex_unlock();
2940 return DDERR_INVALIDPARAMS;
2942 __ENDTRY;
2944 DDSD_to_DDSD2(surface_desc, &surface_desc2);
2945 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
2947 if (TRACE_ON(ddraw))
2949 TRACE(" (%p) Requesting surface desc :\n", iface);
2950 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
2953 WARN("Application tried to create an explicit front or back buffer\n");
2954 wined3d_mutex_unlock();
2955 return DDERR_INVALIDCAPS;
2958 hr = ddraw_surface_create(ddraw, &surface_desc2, &impl, outer_unknown, 2);
2959 wined3d_mutex_unlock();
2960 if (FAILED(hr))
2961 return hr;
2963 *surface = &impl->IDirectDrawSurface_iface;
2964 impl->ifaceToRelease = NULL;
2966 return hr;
2969 static HRESULT WINAPI ddraw1_CreateSurface(IDirectDraw *iface,
2970 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
2972 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2973 struct ddraw_surface *impl;
2974 HRESULT hr;
2975 DDSURFACEDESC2 surface_desc2;
2977 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2978 iface, surface_desc, surface, outer_unknown);
2980 wined3d_mutex_lock();
2982 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2984 WARN("Cooperative level not set.\n");
2985 wined3d_mutex_unlock();
2986 return DDERR_NOCOOPERATIVELEVELSET;
2989 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
2991 WARN("Application supplied invalid surface descriptor\n");
2992 wined3d_mutex_unlock();
2993 return DDERR_INVALIDPARAMS;
2996 __TRY
2998 *surface = NULL;
3000 __EXCEPT_PAGE_FAULT
3002 WARN("Surface pointer %p is invalid.\n", surface);
3003 wined3d_mutex_unlock();
3004 return DDERR_INVALIDPARAMS;
3006 __ENDTRY;
3008 if ((surface_desc->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER))
3009 == (DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER)
3010 || (surface_desc->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER))
3011 == ((DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER)))
3013 WARN("Application tried to create an explicit front or back buffer.\n");
3014 wined3d_mutex_unlock();
3015 return DDERR_INVALIDCAPS;
3018 DDSD_to_DDSD2(surface_desc, &surface_desc2);
3019 hr = ddraw_surface_create(ddraw, &surface_desc2, &impl, outer_unknown, 1);
3020 wined3d_mutex_unlock();
3021 if (FAILED(hr))
3022 return hr;
3024 *surface = &impl->IDirectDrawSurface_iface;
3025 impl->ifaceToRelease = NULL;
3027 return hr;
3030 static BOOL
3031 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
3032 const DDPIXELFORMAT *provided)
3034 /* Some flags must be present in both or neither for a match. */
3035 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
3036 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
3037 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
3039 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3040 return FALSE;
3042 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
3043 return FALSE;
3045 if (requested->dwFlags & DDPF_FOURCC)
3046 if (requested->dwFourCC != provided->dwFourCC)
3047 return FALSE;
3049 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
3050 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3051 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
3052 return FALSE;
3054 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3055 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3056 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
3057 return FALSE;
3059 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
3060 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
3061 return FALSE;
3063 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
3064 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3065 |DDPF_BUMPDUDV))
3066 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
3067 return FALSE;
3069 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
3070 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
3071 return FALSE;
3073 return TRUE;
3076 static BOOL ddraw_match_surface_desc(const DDSURFACEDESC2 *requested, const DDSURFACEDESC2 *provided)
3078 struct compare_info
3080 DWORD flag;
3081 ptrdiff_t offset;
3082 size_t size;
3085 #define CMP(FLAG, FIELD) \
3086 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
3087 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
3089 static const struct compare_info compare[] =
3091 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
3092 CMP(BACKBUFFERCOUNT, u5.dwBackBufferCount),
3093 CMP(CAPS, ddsCaps),
3094 CMP(CKDESTBLT, ddckCKDestBlt),
3095 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
3096 CMP(CKSRCBLT, ddckCKSrcBlt),
3097 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
3098 CMP(HEIGHT, dwHeight),
3099 CMP(LINEARSIZE, u1 /* dwLinearSize */),
3100 CMP(LPSURFACE, lpSurface),
3101 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
3102 CMP(PITCH, u1 /* lPitch */),
3103 /* PIXELFORMAT: manual */
3104 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
3105 CMP(TEXTURESTAGE, dwTextureStage),
3106 CMP(WIDTH, dwWidth),
3107 /* ZBUFFERBITDEPTH: "obsolete" */
3110 #undef CMP
3112 unsigned int i;
3114 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3115 return FALSE;
3117 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
3119 if (requested->dwFlags & compare[i].flag
3120 && memcmp((const char *)provided + compare[i].offset,
3121 (const char *)requested + compare[i].offset,
3122 compare[i].size) != 0)
3123 return FALSE;
3126 if (requested->dwFlags & DDSD_PIXELFORMAT)
3128 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
3129 &provided->u4.ddpfPixelFormat))
3130 return FALSE;
3133 return TRUE;
3136 struct surfacescallback2_context
3138 LPDDENUMSURFACESCALLBACK2 func;
3139 void *context;
3142 struct surfacescallback_context
3144 LPDDENUMSURFACESCALLBACK func;
3145 void *context;
3148 static HRESULT CALLBACK EnumSurfacesCallback2Thunk(IDirectDrawSurface7 *surface,
3149 DDSURFACEDESC2 *surface_desc, void *context)
3151 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3152 struct surfacescallback2_context *cbcontext = context;
3154 IDirectDrawSurface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
3155 IDirectDrawSurface7_Release(surface);
3157 return cbcontext->func(&surface_impl->IDirectDrawSurface4_iface,
3158 surface_desc, cbcontext->context);
3161 static HRESULT CALLBACK EnumSurfacesCallbackThunk(IDirectDrawSurface7 *surface,
3162 DDSURFACEDESC2 *surface_desc, void *context)
3164 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3165 struct surfacescallback_context *cbcontext = context;
3167 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
3168 IDirectDrawSurface7_Release(surface);
3170 return cbcontext->func(&surface_impl->IDirectDrawSurface_iface,
3171 (DDSURFACEDESC *)surface_desc, cbcontext->context);
3174 /*****************************************************************************
3175 * IDirectDraw7::EnumSurfaces
3177 * Loops through all surfaces attached to this device and calls the
3178 * application callback. This can't be relayed to WineD3DDevice,
3179 * because some WineD3DSurfaces' parents are IParent objects
3181 * Params:
3182 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
3183 * DDSD: Description to filter for
3184 * Context: Application-provided pointer, it's passed unmodified to the
3185 * Callback function
3186 * Callback: Address to call for each surface
3188 * Returns:
3189 * DDERR_INVALIDPARAMS if the callback is NULL
3190 * DD_OK on success
3192 *****************************************************************************/
3193 static HRESULT WINAPI ddraw7_EnumSurfaces(IDirectDraw7 *iface, DWORD Flags,
3194 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMSURFACESCALLBACK7 Callback)
3196 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
3197 struct ddraw_surface *surf;
3198 BOOL all, nomatch;
3199 DDSURFACEDESC2 desc;
3200 struct list *entry, *entry2;
3202 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3203 iface, Flags, DDSD, Context, Callback);
3205 all = Flags & DDENUMSURFACES_ALL;
3206 nomatch = Flags & DDENUMSURFACES_NOMATCH;
3208 if (!Callback)
3209 return DDERR_INVALIDPARAMS;
3211 wined3d_mutex_lock();
3213 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
3214 LIST_FOR_EACH_SAFE(entry, entry2, &ddraw->surface_list)
3216 surf = LIST_ENTRY(entry, struct ddraw_surface, surface_list_entry);
3218 if (!surf->iface_count)
3220 WARN("Not enumerating surface %p because it doesn't have any references.\n", surf);
3221 continue;
3224 if (all || (nomatch != ddraw_match_surface_desc(DDSD, &surf->surface_desc)))
3226 TRACE("Enumerating surface %p.\n", surf);
3227 desc = surf->surface_desc;
3228 IDirectDrawSurface7_AddRef(&surf->IDirectDrawSurface7_iface);
3229 if (Callback(&surf->IDirectDrawSurface7_iface, &desc, Context) != DDENUMRET_OK)
3231 wined3d_mutex_unlock();
3232 return DD_OK;
3237 wined3d_mutex_unlock();
3239 return DD_OK;
3242 static HRESULT WINAPI ddraw4_EnumSurfaces(IDirectDraw4 *iface, DWORD flags,
3243 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMSURFACESCALLBACK2 callback)
3245 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3246 struct surfacescallback2_context cbcontext;
3248 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3249 iface, flags, surface_desc, context, callback);
3251 cbcontext.func = callback;
3252 cbcontext.context = context;
3254 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags, surface_desc,
3255 &cbcontext, EnumSurfacesCallback2Thunk);
3258 static HRESULT WINAPI ddraw2_EnumSurfaces(IDirectDraw2 *iface, DWORD flags,
3259 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3261 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3262 struct surfacescallback_context cbcontext;
3263 DDSURFACEDESC2 surface_desc2;
3265 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3266 iface, flags, surface_desc, context, callback);
3268 cbcontext.func = callback;
3269 cbcontext.context = context;
3271 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3272 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags,
3273 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3276 static HRESULT WINAPI ddraw1_EnumSurfaces(IDirectDraw *iface, DWORD flags,
3277 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3279 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3280 struct surfacescallback_context cbcontext;
3281 DDSURFACEDESC2 surface_desc2;
3283 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3284 iface, flags, surface_desc, context, callback);
3286 cbcontext.func = callback;
3287 cbcontext.context = context;
3289 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3290 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags,
3291 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3294 /*****************************************************************************
3295 * DirectDrawCreateClipper (DDRAW.@)
3297 * Creates a new IDirectDrawClipper object.
3299 * Params:
3300 * Clipper: Address to write the interface pointer to
3301 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3302 * NULL
3304 * Returns:
3305 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3306 * E_OUTOFMEMORY if allocating the object failed
3308 *****************************************************************************/
3309 HRESULT WINAPI DirectDrawCreateClipper(DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3311 struct ddraw_clipper *object;
3312 HRESULT hr;
3314 TRACE("flags %#x, clipper %p, outer_unknown %p.\n",
3315 flags, clipper, outer_unknown);
3317 if (outer_unknown)
3318 return CLASS_E_NOAGGREGATION;
3320 wined3d_mutex_lock();
3322 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3323 if (!object)
3325 wined3d_mutex_unlock();
3326 return E_OUTOFMEMORY;
3329 hr = ddraw_clipper_init(object);
3330 if (FAILED(hr))
3332 WARN("Failed to initialize clipper, hr %#x.\n", hr);
3333 HeapFree(GetProcessHeap(), 0, object);
3334 wined3d_mutex_unlock();
3335 return hr;
3338 TRACE("Created clipper %p.\n", object);
3339 *clipper = &object->IDirectDrawClipper_iface;
3340 wined3d_mutex_unlock();
3342 return DD_OK;
3345 /*****************************************************************************
3346 * IDirectDraw7::CreateClipper
3348 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3350 *****************************************************************************/
3351 static HRESULT WINAPI ddraw7_CreateClipper(IDirectDraw7 *iface, DWORD Flags,
3352 IDirectDrawClipper **Clipper, IUnknown *UnkOuter)
3354 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3355 iface, Flags, Clipper, UnkOuter);
3357 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3360 static HRESULT WINAPI ddraw4_CreateClipper(IDirectDraw4 *iface, DWORD flags,
3361 IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3363 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3365 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3366 iface, flags, clipper, outer_unknown);
3368 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3371 static HRESULT WINAPI ddraw2_CreateClipper(IDirectDraw2 *iface,
3372 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3374 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3376 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3377 iface, flags, clipper, outer_unknown);
3379 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3382 static HRESULT WINAPI ddraw1_CreateClipper(IDirectDraw *iface,
3383 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3385 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3387 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3388 iface, flags, clipper, outer_unknown);
3390 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3393 /*****************************************************************************
3394 * IDirectDraw7::CreatePalette
3396 * Creates a new IDirectDrawPalette object
3398 * Params:
3399 * Flags: The flags for the new clipper
3400 * ColorTable: Color table to assign to the new clipper
3401 * Palette: Address to write the interface pointer to
3402 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3403 * NULL
3405 * Returns:
3406 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3407 * E_OUTOFMEMORY if allocating the object failed
3409 *****************************************************************************/
3410 static HRESULT WINAPI ddraw7_CreatePalette(IDirectDraw7 *iface, DWORD Flags,
3411 PALETTEENTRY *ColorTable, IDirectDrawPalette **Palette, IUnknown *pUnkOuter)
3413 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
3414 struct ddraw_palette *object;
3415 HRESULT hr;
3417 TRACE("iface %p, flags %#x, color_table %p, palette %p, outer_unknown %p.\n",
3418 iface, Flags, ColorTable, Palette, pUnkOuter);
3420 if (pUnkOuter)
3421 return CLASS_E_NOAGGREGATION;
3423 wined3d_mutex_lock();
3425 /* The refcount test shows that a cooplevel is required for this */
3426 if (!ddraw->cooperative_level)
3428 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3429 wined3d_mutex_unlock();
3430 return DDERR_NOCOOPERATIVELEVELSET;
3433 object = HeapAlloc(GetProcessHeap(), 0, sizeof(*object));
3434 if (!object)
3436 ERR("Out of memory when allocating memory for a palette implementation\n");
3437 wined3d_mutex_unlock();
3438 return E_OUTOFMEMORY;
3441 hr = ddraw_palette_init(object, ddraw, Flags, ColorTable);
3442 if (FAILED(hr))
3444 WARN("Failed to initialize palette, hr %#x.\n", hr);
3445 HeapFree(GetProcessHeap(), 0, object);
3446 wined3d_mutex_unlock();
3447 return hr;
3450 TRACE("Created palette %p.\n", object);
3451 *Palette = &object->IDirectDrawPalette_iface;
3452 wined3d_mutex_unlock();
3454 return DD_OK;
3457 static HRESULT WINAPI ddraw4_CreatePalette(IDirectDraw4 *iface, DWORD flags, PALETTEENTRY *entries,
3458 IDirectDrawPalette **palette, IUnknown *outer_unknown)
3460 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3461 HRESULT hr;
3463 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3464 iface, flags, entries, palette, outer_unknown);
3466 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3467 if (SUCCEEDED(hr) && *palette)
3469 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3470 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3471 IDirectDraw4_AddRef(iface);
3472 impl->ifaceToRelease = (IUnknown *)iface;
3474 return hr;
3477 static HRESULT WINAPI ddraw2_CreatePalette(IDirectDraw2 *iface, DWORD flags,
3478 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3480 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3481 HRESULT hr;
3483 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3484 iface, flags, entries, palette, outer_unknown);
3486 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3487 if (SUCCEEDED(hr) && *palette)
3489 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3490 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3491 impl->ifaceToRelease = NULL;
3494 return hr;
3497 static HRESULT WINAPI ddraw1_CreatePalette(IDirectDraw *iface, DWORD flags,
3498 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3500 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3501 HRESULT hr;
3503 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3504 iface, flags, entries, palette, outer_unknown);
3506 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3507 if (SUCCEEDED(hr) && *palette)
3509 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3510 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3511 impl->ifaceToRelease = NULL;
3514 return hr;
3517 /*****************************************************************************
3518 * IDirectDraw7::DuplicateSurface
3520 * Duplicates a surface. The surface memory points to the same memory as
3521 * the original surface, and it's released when the last surface referencing
3522 * it is released. I guess that's beyond Wine's surface management right now
3523 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3524 * test application to implement this)
3526 * Params:
3527 * Src: Address of the source surface
3528 * Dest: Address to write the new surface pointer to
3530 * Returns:
3531 * See IDirectDraw7::CreateSurface
3533 *****************************************************************************/
3534 static HRESULT WINAPI ddraw7_DuplicateSurface(IDirectDraw7 *iface,
3535 IDirectDrawSurface7 *Src, IDirectDrawSurface7 **Dest)
3537 struct ddraw_surface *src_surface = unsafe_impl_from_IDirectDrawSurface7(Src);
3539 FIXME("iface %p, src %p, dst %p partial stub!\n", iface, Src, Dest);
3541 /* For now, simply create a new, independent surface */
3542 return IDirectDraw7_CreateSurface(iface, &src_surface->surface_desc, Dest, NULL);
3545 static HRESULT WINAPI ddraw4_DuplicateSurface(IDirectDraw4 *iface, IDirectDrawSurface4 *src,
3546 IDirectDrawSurface4 **dst)
3548 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src);
3549 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3550 struct ddraw_surface *dst_impl;
3551 IDirectDrawSurface7 *dst7;
3552 HRESULT hr;
3554 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3556 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3557 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3558 if (FAILED(hr))
3560 *dst = NULL;
3561 return hr;
3563 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3564 *dst = &dst_impl->IDirectDrawSurface4_iface;
3565 IDirectDrawSurface4_AddRef(*dst);
3566 IDirectDrawSurface7_Release(dst7);
3568 return hr;
3571 static HRESULT WINAPI ddraw2_DuplicateSurface(IDirectDraw2 *iface,
3572 IDirectDrawSurface *src, IDirectDrawSurface **dst)
3574 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3575 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3576 struct ddraw_surface *dst_impl;
3577 IDirectDrawSurface7 *dst7;
3578 HRESULT hr;
3580 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3582 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3583 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3584 if (FAILED(hr))
3585 return hr;
3586 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3587 *dst = &dst_impl->IDirectDrawSurface_iface;
3588 IDirectDrawSurface_AddRef(*dst);
3589 IDirectDrawSurface7_Release(dst7);
3591 return hr;
3594 static HRESULT WINAPI ddraw1_DuplicateSurface(IDirectDraw *iface, IDirectDrawSurface *src,
3595 IDirectDrawSurface **dst)
3597 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3598 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3599 struct ddraw_surface *dst_impl;
3600 IDirectDrawSurface7 *dst7;
3601 HRESULT hr;
3603 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3605 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3606 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3607 if (FAILED(hr))
3608 return hr;
3609 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3610 *dst = &dst_impl->IDirectDrawSurface_iface;
3611 IDirectDrawSurface_AddRef(*dst);
3612 IDirectDrawSurface7_Release(dst7);
3614 return hr;
3617 /*****************************************************************************
3618 * IDirect3D7::EnumDevices
3620 * The EnumDevices method for IDirect3D7. It enumerates all supported
3621 * D3D7 devices. Currently the T&L, HAL and RGB devices are enumerated.
3623 * Params:
3624 * callback: Function to call for each enumerated device
3625 * context: Pointer to pass back to the app
3627 * Returns:
3628 * D3D_OK, or the return value of the GetCaps call
3630 *****************************************************************************/
3631 static HRESULT WINAPI d3d7_EnumDevices(IDirect3D7 *iface, LPD3DENUMDEVICESCALLBACK7 callback, void *context)
3633 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
3634 D3DDEVICEDESC7 device_desc7;
3635 HRESULT hr;
3636 size_t i;
3638 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3640 if (!callback)
3641 return DDERR_INVALIDPARAMS;
3643 wined3d_mutex_lock();
3645 if (FAILED(hr = ddraw_get_d3dcaps(ddraw, &device_desc7)))
3647 wined3d_mutex_unlock();
3648 return hr;
3651 for (i = 0; i < sizeof(device_list7)/sizeof(device_list7[0]); i++)
3653 HRESULT ret;
3655 device_desc7.deviceGUID = *device_list7[i].device_guid;
3656 ret = callback(device_list7[i].interface_name, device_list7[i].device_name, &device_desc7, context);
3657 if (ret != DDENUMRET_OK)
3659 TRACE("Application cancelled the enumeration.\n");
3660 wined3d_mutex_unlock();
3661 return D3D_OK;
3665 TRACE("End of enumeration.\n");
3667 wined3d_mutex_unlock();
3669 return D3D_OK;
3672 /*****************************************************************************
3673 * IDirect3D3::EnumDevices
3675 * Enumerates all supported Direct3DDevice interfaces. This is the
3676 * implementation for Direct3D 1 to Direc3D 3, Version 7 has its own.
3678 * Versions 1, 2 and 3
3680 * Params:
3681 * callback: Application-provided routine to call for each enumerated device
3682 * Context: Pointer to pass to the callback
3684 * Returns:
3685 * D3D_OK on success,
3686 * The result of IDirect3DImpl_GetCaps if it failed
3688 *****************************************************************************/
3689 static HRESULT WINAPI d3d3_EnumDevices(IDirect3D3 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3691 static CHAR wined3d_description[] = "Wine D3DDevice using WineD3D and OpenGL";
3693 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3694 D3DDEVICEDESC device_desc1, hal_desc, hel_desc;
3695 D3DDEVICEDESC7 device_desc7;
3696 HRESULT hr;
3698 /* Some games (Motoracer 2 demo) have the bad idea to modify the device
3699 * name string. Let's put the string in a sufficiently sized array in
3700 * writable memory. */
3701 char device_name[50];
3702 strcpy(device_name,"Direct3D HEL");
3704 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3706 if (!callback)
3707 return DDERR_INVALIDPARAMS;
3709 wined3d_mutex_lock();
3711 if (FAILED(hr = ddraw_get_d3dcaps(ddraw, &device_desc7)))
3713 wined3d_mutex_unlock();
3714 return hr;
3716 ddraw_d3dcaps1_from_7(&device_desc1, &device_desc7);
3718 /* Do I have to enumerate the reference id? Note from old d3d7:
3719 * "It seems that enumerating the reference IID on Direct3D 1 games
3720 * (AvP / Motoracer2) breaks them". So do not enumerate this iid in V1
3722 * There's a registry key HKLM\Software\Microsoft\Direct3D\Drivers,
3723 * EnumReference which enables / disables enumerating the reference
3724 * rasterizer. It's a DWORD, 0 means disabled, 2 means enabled. The
3725 * enablerefrast.reg and disablerefrast.reg files in the DirectX 7.0 sdk
3726 * demo directory suggest this.
3728 * Some games(GTA 2) seem to use the second enumerated device, so I have
3729 * to enumerate at least 2 devices. So enumerate the reference device to
3730 * have 2 devices.
3732 * Other games (Rollcage) tell emulation and hal device apart by certain
3733 * flags. Rollcage expects D3DPTEXTURECAPS_POW2 to be set (yeah, it is a
3734 * limitation flag), and it refuses all devices that have the perspective
3735 * flag set. This way it refuses the emulation device, and HAL devices
3736 * never have POW2 unset in d3d7 on windows. */
3737 if (ddraw->d3dversion != 1)
3739 static CHAR reference_description[] = "RGB Direct3D emulation";
3741 TRACE("Enumerating WineD3D D3DDevice interface.\n");
3742 hal_desc = device_desc1;
3743 hel_desc = device_desc1;
3744 /* The rgb device has the pow2 flag set in the hel caps, but not in the hal caps. */
3745 hal_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3746 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3747 hal_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3748 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3749 /* RGB, RAMP and MMX devices have a HAL dcmColorModel of 0 */
3750 hal_desc.dcmColorModel = 0;
3751 /* RGB, RAMP and MMX devices cannot report HAL hardware flags */
3752 hal_desc.dwFlags = 0;
3754 hr = callback((GUID *)&IID_IDirect3DRGBDevice, reference_description,
3755 device_name, &hal_desc, &hel_desc, context);
3756 if (hr != D3DENUMRET_OK)
3758 TRACE("Application cancelled the enumeration.\n");
3759 wined3d_mutex_unlock();
3760 return D3D_OK;
3764 strcpy(device_name,"Direct3D HAL");
3766 TRACE("Enumerating HAL Direct3D device.\n");
3767 hal_desc = device_desc1;
3768 hel_desc = device_desc1;
3770 /* The hal device does not have the pow2 flag set in hel, but in hal. */
3771 hel_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3772 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3773 hel_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3774 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3775 /* HAL devices have a HEL dcmColorModel of 0 */
3776 hel_desc.dcmColorModel = 0;
3778 hr = callback((GUID *)&IID_IDirect3DHALDevice, wined3d_description,
3779 device_name, &hal_desc, &hel_desc, context);
3780 if (hr != D3DENUMRET_OK)
3782 TRACE("Application cancelled the enumeration.\n");
3783 wined3d_mutex_unlock();
3784 return D3D_OK;
3787 TRACE("End of enumeration.\n");
3789 wined3d_mutex_unlock();
3791 return D3D_OK;
3794 static HRESULT WINAPI d3d2_EnumDevices(IDirect3D2 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3796 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3798 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3800 return d3d3_EnumDevices(&ddraw->IDirect3D3_iface, callback, context);
3803 static HRESULT WINAPI d3d1_EnumDevices(IDirect3D *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3805 struct ddraw *ddraw = impl_from_IDirect3D(iface);
3807 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3809 return d3d3_EnumDevices(&ddraw->IDirect3D3_iface, callback, context);
3812 /*****************************************************************************
3813 * IDirect3D3::CreateLight
3815 * Creates an IDirect3DLight interface. This interface is used in
3816 * Direct3D3 or earlier for lighting. In Direct3D7 it has been replaced
3817 * by the DIRECT3DLIGHT7 structure. Wine's Direct3DLight implementation
3818 * uses the IDirect3DDevice7 interface with D3D7 lights.
3820 * Versions 1, 2 and 3
3822 * Params:
3823 * light: Address to store the new interface pointer
3824 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
3825 * Must be NULL
3827 * Returns:
3828 * D3D_OK on success
3829 * DDERR_OUTOFMEMORY if memory allocation failed
3830 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
3832 *****************************************************************************/
3833 static HRESULT WINAPI d3d3_CreateLight(IDirect3D3 *iface, IDirect3DLight **light,
3834 IUnknown *outer_unknown)
3836 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3837 struct d3d_light *object;
3839 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3841 if (outer_unknown) return CLASS_E_NOAGGREGATION;
3843 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3844 if (!object)
3846 ERR("Failed to allocate light memory.\n");
3847 return DDERR_OUTOFMEMORY;
3850 d3d_light_init(object, ddraw);
3852 TRACE("Created light %p.\n", object);
3853 *light = &object->IDirect3DLight_iface;
3855 return D3D_OK;
3858 static HRESULT WINAPI d3d2_CreateLight(IDirect3D2 *iface, IDirect3DLight **light, IUnknown *outer_unknown)
3860 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3862 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3864 return d3d3_CreateLight(&ddraw->IDirect3D3_iface, light, outer_unknown);
3867 static HRESULT WINAPI d3d1_CreateLight(IDirect3D *iface, IDirect3DLight **light, IUnknown *outer_unknown)
3869 struct ddraw *ddraw = impl_from_IDirect3D(iface);
3871 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3873 return d3d3_CreateLight(&ddraw->IDirect3D3_iface, light, outer_unknown);
3876 /*****************************************************************************
3877 * IDirect3D3::CreateMaterial
3879 * Creates an IDirect3DMaterial interface. This interface is used by Direct3D3
3880 * and older versions. The IDirect3DMaterial implementation wraps its
3881 * functionality to IDirect3DDevice7::SetMaterial and friends.
3883 * Versions 1, 2 and 3
3885 * Params:
3886 * material: Address to store the new interface's pointer to
3887 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
3888 * Must be NULL
3890 * Returns:
3891 * D3D_OK on success
3892 * DDERR_OUTOFMEMORY if memory allocation failed
3893 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
3895 *****************************************************************************/
3896 static HRESULT WINAPI d3d3_CreateMaterial(IDirect3D3 *iface, IDirect3DMaterial3 **material,
3897 IUnknown *outer_unknown)
3899 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3900 struct d3d_material *object;
3902 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
3904 if (outer_unknown) return CLASS_E_NOAGGREGATION;
3906 object = d3d_material_create(ddraw);
3907 if (!object)
3909 ERR("Failed to allocate material memory.\n");
3910 return DDERR_OUTOFMEMORY;
3913 TRACE("Created material %p.\n", object);
3914 *material = &object->IDirect3DMaterial3_iface;
3916 return D3D_OK;
3919 static HRESULT WINAPI d3d2_CreateMaterial(IDirect3D2 *iface, IDirect3DMaterial2 **material,
3920 IUnknown *outer_unknown)
3922 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3923 struct d3d_material *object;
3925 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
3927 object = d3d_material_create(ddraw);
3928 if (!object)
3930 ERR("Failed to allocate material memory.\n");
3931 return DDERR_OUTOFMEMORY;
3934 TRACE("Created material %p.\n", object);
3935 *material = &object->IDirect3DMaterial2_iface;
3937 return D3D_OK;
3940 static HRESULT WINAPI d3d1_CreateMaterial(IDirect3D *iface, IDirect3DMaterial **material,
3941 IUnknown *outer_unknown)
3943 struct ddraw *ddraw = impl_from_IDirect3D(iface);
3944 struct d3d_material *object;
3946 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
3948 object = d3d_material_create(ddraw);
3949 if (!object)
3951 ERR("Failed to allocate material memory.\n");
3952 return DDERR_OUTOFMEMORY;
3955 TRACE("Created material %p.\n", object);
3956 *material = &object->IDirect3DMaterial_iface;
3958 return D3D_OK;
3961 /*****************************************************************************
3962 * IDirect3D3::CreateViewport
3964 * Creates an IDirect3DViewport interface. This interface is used
3965 * by Direct3D and earlier versions for Viewport management. In Direct3D7
3966 * it has been replaced by a viewport structure and
3967 * IDirect3DDevice7::*Viewport. Wine's IDirect3DViewport implementation
3968 * uses the IDirect3DDevice7 methods for its functionality
3970 * Params:
3971 * Viewport: Address to store the new interface pointer
3972 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
3973 * Must be NULL
3975 * Returns:
3976 * D3D_OK on success
3977 * DDERR_OUTOFMEMORY if memory allocation failed
3978 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
3980 *****************************************************************************/
3981 static HRESULT WINAPI d3d3_CreateViewport(IDirect3D3 *iface, IDirect3DViewport3 **viewport,
3982 IUnknown *outer_unknown)
3984 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3985 struct d3d_viewport *object;
3987 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
3989 if (outer_unknown) return CLASS_E_NOAGGREGATION;
3991 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3992 if (!object)
3994 ERR("Failed to allocate viewport memory.\n");
3995 return DDERR_OUTOFMEMORY;
3998 d3d_viewport_init(object, ddraw);
4000 TRACE("Created viewport %p.\n", object);
4001 *viewport = &object->IDirect3DViewport3_iface;
4003 return D3D_OK;
4006 static HRESULT WINAPI d3d2_CreateViewport(IDirect3D2 *iface, IDirect3DViewport2 **viewport, IUnknown *outer_unknown)
4008 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4010 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4012 return d3d3_CreateViewport(&ddraw->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4013 outer_unknown);
4016 static HRESULT WINAPI d3d1_CreateViewport(IDirect3D *iface, IDirect3DViewport **viewport, IUnknown *outer_unknown)
4018 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4020 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4022 return d3d3_CreateViewport(&ddraw->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4023 outer_unknown);
4026 /*****************************************************************************
4027 * IDirect3D3::FindDevice
4029 * This method finds a device with the requested properties and returns a
4030 * device description
4032 * Versions 1, 2 and 3
4033 * Params:
4034 * fds: Describes the requested device characteristics
4035 * fdr: Returns the device description
4037 * Returns:
4038 * D3D_OK on success
4039 * DDERR_INVALIDPARAMS if no device was found
4041 *****************************************************************************/
4042 static HRESULT WINAPI d3d3_FindDevice(IDirect3D3 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4044 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4045 D3DDEVICEDESC7 desc7;
4046 D3DDEVICEDESC desc1;
4047 HRESULT hr;
4049 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4051 if (!fds || !fdr) return DDERR_INVALIDPARAMS;
4053 if (fds->dwSize != sizeof(D3DFINDDEVICESEARCH)
4054 || fdr->dwSize != sizeof(D3DFINDDEVICERESULT))
4055 return DDERR_INVALIDPARAMS;
4057 if ((fds->dwFlags & D3DFDS_COLORMODEL)
4058 && fds->dcmColorModel != D3DCOLOR_RGB)
4060 WARN("Trying to request a non-RGB D3D color model. Not supported.\n");
4061 return DDERR_INVALIDPARAMS; /* No real idea what to return here :-) */
4064 if (fds->dwFlags & D3DFDS_GUID)
4066 TRACE("Trying to match guid %s.\n", debugstr_guid(&(fds->guid)));
4067 if (!IsEqualGUID(&IID_D3DDEVICE_WineD3D, &fds->guid)
4068 && !IsEqualGUID(&IID_IDirect3DHALDevice, &fds->guid)
4069 && !IsEqualGUID(&IID_IDirect3DRGBDevice, &fds->guid))
4071 WARN("No match for this GUID.\n");
4072 return DDERR_NOTFOUND;
4076 /* Get the caps */
4077 if (FAILED(hr = ddraw_get_d3dcaps(ddraw, &desc7)))
4078 return hr;
4080 /* Now return our own GUID */
4081 ddraw_d3dcaps1_from_7(&desc1, &desc7);
4082 fdr->guid = IID_D3DDEVICE_WineD3D;
4083 fdr->ddHwDesc = desc1;
4084 fdr->ddSwDesc = desc1;
4086 TRACE("Returning Wine's wined3d device with (undumped) capabilities.\n");
4088 return D3D_OK;
4091 static HRESULT WINAPI d3d2_FindDevice(IDirect3D2 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4093 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4095 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4097 return d3d3_FindDevice(&ddraw->IDirect3D3_iface, fds, fdr);
4100 static HRESULT WINAPI d3d1_FindDevice(IDirect3D *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4102 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4104 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4106 return d3d3_FindDevice(&ddraw->IDirect3D3_iface, fds, fdr);
4109 /*****************************************************************************
4110 * IDirect3D7::CreateDevice
4112 * Creates an IDirect3DDevice7 interface.
4114 * Versions 2, 3 and 7. IDirect3DDevice 1 interfaces are interfaces to
4115 * DirectDraw surfaces and are created with
4116 * IDirectDrawSurface::QueryInterface. This method uses CreateDevice to
4117 * create the device object and QueryInterfaces for IDirect3DDevice
4119 * Params:
4120 * refiid: IID of the device to create
4121 * Surface: Initial rendertarget
4122 * Device: Address to return the interface pointer
4124 * Returns:
4125 * D3D_OK on success
4126 * DDERR_OUTOFMEMORY if memory allocation failed
4127 * DDERR_INVALIDPARAMS if a device exists already
4129 *****************************************************************************/
4130 static HRESULT WINAPI d3d7_CreateDevice(IDirect3D7 *iface, REFCLSID riid,
4131 IDirectDrawSurface7 *surface, IDirect3DDevice7 **device)
4133 struct ddraw_surface *target = unsafe_impl_from_IDirectDrawSurface7(surface);
4134 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4135 struct d3d_device *object;
4136 HRESULT hr;
4138 TRACE("iface %p, riid %s, surface %p, device %p.\n", iface, debugstr_guid(riid), surface, device);
4140 wined3d_mutex_lock();
4141 if (SUCCEEDED(hr = d3d_device_create(ddraw, target, (IUnknown *)surface, 7, &object, NULL)))
4143 *device = &object->IDirect3DDevice7_iface;
4145 else
4147 WARN("Failed to create device, hr %#x.\n", hr);
4148 *device = NULL;
4150 wined3d_mutex_unlock();
4152 return hr;
4155 static HRESULT WINAPI d3d3_CreateDevice(IDirect3D3 *iface, REFCLSID riid,
4156 IDirectDrawSurface4 *surface, IDirect3DDevice3 **device, IUnknown *outer_unknown)
4158 struct ddraw_surface *surface_impl = unsafe_impl_from_IDirectDrawSurface4(surface);
4159 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4160 struct d3d_device *device_impl;
4161 HRESULT hr;
4163 TRACE("iface %p, riid %s, surface %p, device %p, outer_unknown %p.\n",
4164 iface, debugstr_guid(riid), surface, device, outer_unknown);
4166 if (outer_unknown)
4167 return CLASS_E_NOAGGREGATION;
4169 wined3d_mutex_lock();
4170 if (SUCCEEDED(hr = d3d_device_create(ddraw, surface_impl, (IUnknown *)surface, 3, &device_impl, NULL)))
4172 *device = &device_impl->IDirect3DDevice3_iface;
4174 else
4176 WARN("Failed to create device, hr %#x.\n", hr);
4177 *device = NULL;
4179 wined3d_mutex_unlock();
4181 return hr;
4184 static HRESULT WINAPI d3d2_CreateDevice(IDirect3D2 *iface, REFCLSID riid,
4185 IDirectDrawSurface *surface, IDirect3DDevice2 **device)
4187 struct ddraw_surface *surface_impl = unsafe_impl_from_IDirectDrawSurface(surface);
4188 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4189 struct d3d_device *device_impl;
4190 HRESULT hr;
4192 TRACE("iface %p, riid %s, surface %p, device %p.\n",
4193 iface, debugstr_guid(riid), surface, device);
4195 wined3d_mutex_lock();
4196 if (SUCCEEDED(hr = d3d_device_create(ddraw, surface_impl, (IUnknown *)surface, 2, &device_impl, NULL)))
4198 *device = &device_impl->IDirect3DDevice2_iface;
4200 else
4202 WARN("Failed to create device, hr %#x.\n", hr);
4203 *device = NULL;
4205 wined3d_mutex_unlock();
4207 return hr;
4210 /*****************************************************************************
4211 * IDirect3D7::CreateVertexBuffer
4213 * Creates a new vertex buffer object and returns a IDirect3DVertexBuffer7
4214 * interface.
4216 * Versions 3 and 7
4218 * Params:
4219 * desc: Requested Vertex buffer properties
4220 * vertex_buffer: Address to return the interface pointer at
4221 * flags: Some flags, should be 0
4223 * Returns
4224 * D3D_OK on success
4225 * DDERR_OUTOFMEMORY if memory allocation failed
4226 * DDERR_INVALIDPARAMS if desc or vertex_buffer is NULL
4228 *****************************************************************************/
4229 static HRESULT WINAPI d3d7_CreateVertexBuffer(IDirect3D7 *iface, D3DVERTEXBUFFERDESC *desc,
4230 IDirect3DVertexBuffer7 **vertex_buffer, DWORD flags)
4232 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4233 struct d3d_vertex_buffer *object;
4234 HRESULT hr;
4236 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x.\n",
4237 iface, desc, vertex_buffer, flags);
4239 if (!vertex_buffer || !desc) return DDERR_INVALIDPARAMS;
4241 hr = d3d_vertex_buffer_create(&object, ddraw, desc);
4242 if (hr == D3D_OK)
4244 TRACE("Created vertex buffer %p.\n", object);
4245 *vertex_buffer = &object->IDirect3DVertexBuffer7_iface;
4247 else
4248 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4250 return hr;
4253 static HRESULT WINAPI d3d3_CreateVertexBuffer(IDirect3D3 *iface, D3DVERTEXBUFFERDESC *desc,
4254 IDirect3DVertexBuffer **vertex_buffer, DWORD flags, IUnknown *outer_unknown)
4256 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4257 struct d3d_vertex_buffer *object;
4258 HRESULT hr;
4260 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x, outer_unknown %p.\n",
4261 iface, desc, vertex_buffer, flags, outer_unknown);
4263 if (outer_unknown)
4264 return CLASS_E_NOAGGREGATION;
4265 if (!vertex_buffer || !desc)
4266 return DDERR_INVALIDPARAMS;
4268 hr = d3d_vertex_buffer_create(&object, ddraw, desc);
4269 if (hr == D3D_OK)
4271 TRACE("Created vertex buffer %p.\n", object);
4272 *vertex_buffer = (IDirect3DVertexBuffer *)&object->IDirect3DVertexBuffer7_iface;
4274 else
4275 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4277 return hr;
4280 /*****************************************************************************
4281 * IDirect3D7::EnumZBufferFormats
4283 * Enumerates all supported Z buffer pixel formats
4285 * Versions 3 and 7
4287 * Params:
4288 * device_iid:
4289 * callback: callback to call for each pixel format
4290 * context: Pointer to pass back to the callback
4292 * Returns:
4293 * D3D_OK on success
4294 * DDERR_INVALIDPARAMS if callback is NULL
4296 *****************************************************************************/
4297 static HRESULT WINAPI d3d7_EnumZBufferFormats(IDirect3D7 *iface, REFCLSID device_iid,
4298 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4300 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4301 struct wined3d_display_mode mode;
4302 enum wined3d_device_type type;
4303 unsigned int i;
4304 HRESULT hr;
4306 /* Order matters. Specifically, BattleZone II (full version) expects the
4307 * 16-bit depth formats to be listed before the 24 and 32 ones. */
4308 static const enum wined3d_format_id formats[] =
4310 WINED3DFMT_S1_UINT_D15_UNORM,
4311 WINED3DFMT_D16_UNORM,
4312 WINED3DFMT_X8D24_UNORM,
4313 WINED3DFMT_S4X4_UINT_D24_UNORM,
4314 WINED3DFMT_D24_UNORM_S8_UINT,
4315 WINED3DFMT_D32_UNORM,
4318 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4319 iface, debugstr_guid(device_iid), callback, context);
4321 if (!callback) return DDERR_INVALIDPARAMS;
4323 if (IsEqualGUID(device_iid, &IID_IDirect3DHALDevice)
4324 || IsEqualGUID(device_iid, &IID_IDirect3DTnLHalDevice)
4325 || IsEqualGUID(device_iid, &IID_D3DDEVICE_WineD3D))
4327 TRACE("Asked for HAL device.\n");
4328 type = WINED3D_DEVICE_TYPE_HAL;
4330 else if (IsEqualGUID(device_iid, &IID_IDirect3DRGBDevice)
4331 || IsEqualGUID(device_iid, &IID_IDirect3DMMXDevice))
4333 TRACE("Asked for SW device.\n");
4334 type = WINED3D_DEVICE_TYPE_SW;
4336 else if (IsEqualGUID(device_iid, &IID_IDirect3DRefDevice))
4338 TRACE("Asked for REF device.\n");
4339 type = WINED3D_DEVICE_TYPE_REF;
4341 else if (IsEqualGUID(device_iid, &IID_IDirect3DNullDevice))
4343 TRACE("Asked for NULLREF device.\n");
4344 type = WINED3D_DEVICE_TYPE_NULLREF;
4346 else
4348 FIXME("Unexpected device GUID %s.\n", debugstr_guid(device_iid));
4349 type = WINED3D_DEVICE_TYPE_HAL;
4352 wined3d_mutex_lock();
4353 /* We need an adapter format from somewhere to please wined3d and WGL.
4354 * Use the current display mode. So far all cards offer the same depth
4355 * stencil format for all modes, but if some do not and applications do
4356 * not like that we'll have to find some workaround, like iterating over
4357 * all imaginable formats and collecting all the depth stencil formats we
4358 * can get. */
4359 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
4361 ERR("Failed to get display mode, hr %#x.\n", hr);
4362 wined3d_mutex_unlock();
4363 return hr;
4366 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); ++i)
4368 if (SUCCEEDED(wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, type, mode.format_id,
4369 WINED3DUSAGE_DEPTHSTENCIL, WINED3D_RTYPE_TEXTURE_2D, formats[i])))
4371 DDPIXELFORMAT pformat;
4373 memset(&pformat, 0, sizeof(pformat));
4374 pformat.dwSize = sizeof(pformat);
4375 ddrawformat_from_wined3dformat(&pformat, formats[i]);
4377 TRACE("Enumerating wined3d format %#x.\n", formats[i]);
4378 hr = callback(&pformat, context);
4379 if (hr != DDENUMRET_OK)
4381 TRACE("Format enumeration cancelled by application.\n");
4382 wined3d_mutex_unlock();
4383 return D3D_OK;
4388 /* Historically some windows drivers used dwZBufferBitDepth=24 for WINED3DFMT_X8D24_UNORM,
4389 * while others used dwZBufferBitDepth=32. In either case the pitch matches a 32 bits per
4390 * pixel format, so we use dwZBufferBitDepth=32. Some games expect 24. Windows Vista and
4391 * newer enumerate both versions, so we do the same(bug 22434) */
4392 if (SUCCEEDED(wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, type, mode.format_id,
4393 WINED3DUSAGE_DEPTHSTENCIL, WINED3D_RTYPE_TEXTURE_2D, WINED3DFMT_X8D24_UNORM)))
4395 DDPIXELFORMAT x8d24 =
4397 sizeof(x8d24), DDPF_ZBUFFER, 0,
4398 {24}, {0x00000000}, {0x00ffffff}, {0x00000000}
4400 TRACE("Enumerating WINED3DFMT_X8D24_UNORM, dwZBufferBitDepth=24 version\n");
4401 callback(&x8d24, context);
4404 TRACE("End of enumeration.\n");
4406 wined3d_mutex_unlock();
4408 return D3D_OK;
4411 static HRESULT WINAPI d3d3_EnumZBufferFormats(IDirect3D3 *iface, REFCLSID device_iid,
4412 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4414 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4416 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4417 iface, debugstr_guid(device_iid), callback, context);
4419 return d3d7_EnumZBufferFormats(&ddraw->IDirect3D7_iface, device_iid, callback, context);
4422 /*****************************************************************************
4423 * IDirect3D7::EvictManagedTextures
4425 * Removes all managed textures (=surfaces with DDSCAPS2_TEXTUREMANAGE or
4426 * DDSCAPS2_D3DTEXTUREMANAGE caps) to be removed from video memory.
4428 * Versions 3 and 7
4430 * Returns:
4431 * D3D_OK, because it's a stub
4433 *****************************************************************************/
4434 static HRESULT WINAPI d3d7_EvictManagedTextures(IDirect3D7 *iface)
4436 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4438 TRACE("iface %p!\n", iface);
4440 wined3d_mutex_lock();
4441 if (ddraw->flags & DDRAW_D3D_INITIALIZED)
4442 wined3d_device_evict_managed_resources(ddraw->wined3d_device);
4443 wined3d_mutex_unlock();
4445 return D3D_OK;
4448 static HRESULT WINAPI d3d3_EvictManagedTextures(IDirect3D3 *iface)
4450 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4452 TRACE("iface %p.\n", iface);
4454 return d3d7_EvictManagedTextures(&ddraw->IDirect3D7_iface);
4457 /*****************************************************************************
4458 * IDirectDraw7 VTable
4459 *****************************************************************************/
4460 static const struct IDirectDraw7Vtbl ddraw7_vtbl =
4462 /* IUnknown */
4463 ddraw7_QueryInterface,
4464 ddraw7_AddRef,
4465 ddraw7_Release,
4466 /* IDirectDraw */
4467 ddraw7_Compact,
4468 ddraw7_CreateClipper,
4469 ddraw7_CreatePalette,
4470 ddraw7_CreateSurface,
4471 ddraw7_DuplicateSurface,
4472 ddraw7_EnumDisplayModes,
4473 ddraw7_EnumSurfaces,
4474 ddraw7_FlipToGDISurface,
4475 ddraw7_GetCaps,
4476 ddraw7_GetDisplayMode,
4477 ddraw7_GetFourCCCodes,
4478 ddraw7_GetGDISurface,
4479 ddraw7_GetMonitorFrequency,
4480 ddraw7_GetScanLine,
4481 ddraw7_GetVerticalBlankStatus,
4482 ddraw7_Initialize,
4483 ddraw7_RestoreDisplayMode,
4484 ddraw7_SetCooperativeLevel,
4485 ddraw7_SetDisplayMode,
4486 ddraw7_WaitForVerticalBlank,
4487 /* IDirectDraw2 */
4488 ddraw7_GetAvailableVidMem,
4489 /* IDirectDraw3 */
4490 ddraw7_GetSurfaceFromDC,
4491 /* IDirectDraw4 */
4492 ddraw7_RestoreAllSurfaces,
4493 ddraw7_TestCooperativeLevel,
4494 ddraw7_GetDeviceIdentifier,
4495 /* IDirectDraw7 */
4496 ddraw7_StartModeTest,
4497 ddraw7_EvaluateMode
4500 static const struct IDirectDraw4Vtbl ddraw4_vtbl =
4502 /* IUnknown */
4503 ddraw4_QueryInterface,
4504 ddraw4_AddRef,
4505 ddraw4_Release,
4506 /* IDirectDraw */
4507 ddraw4_Compact,
4508 ddraw4_CreateClipper,
4509 ddraw4_CreatePalette,
4510 ddraw4_CreateSurface,
4511 ddraw4_DuplicateSurface,
4512 ddraw4_EnumDisplayModes,
4513 ddraw4_EnumSurfaces,
4514 ddraw4_FlipToGDISurface,
4515 ddraw4_GetCaps,
4516 ddraw4_GetDisplayMode,
4517 ddraw4_GetFourCCCodes,
4518 ddraw4_GetGDISurface,
4519 ddraw4_GetMonitorFrequency,
4520 ddraw4_GetScanLine,
4521 ddraw4_GetVerticalBlankStatus,
4522 ddraw4_Initialize,
4523 ddraw4_RestoreDisplayMode,
4524 ddraw4_SetCooperativeLevel,
4525 ddraw4_SetDisplayMode,
4526 ddraw4_WaitForVerticalBlank,
4527 /* IDirectDraw2 */
4528 ddraw4_GetAvailableVidMem,
4529 /* IDirectDraw3 */
4530 ddraw4_GetSurfaceFromDC,
4531 /* IDirectDraw4 */
4532 ddraw4_RestoreAllSurfaces,
4533 ddraw4_TestCooperativeLevel,
4534 ddraw4_GetDeviceIdentifier,
4537 static const struct IDirectDraw2Vtbl ddraw2_vtbl =
4539 /* IUnknown */
4540 ddraw2_QueryInterface,
4541 ddraw2_AddRef,
4542 ddraw2_Release,
4543 /* IDirectDraw */
4544 ddraw2_Compact,
4545 ddraw2_CreateClipper,
4546 ddraw2_CreatePalette,
4547 ddraw2_CreateSurface,
4548 ddraw2_DuplicateSurface,
4549 ddraw2_EnumDisplayModes,
4550 ddraw2_EnumSurfaces,
4551 ddraw2_FlipToGDISurface,
4552 ddraw2_GetCaps,
4553 ddraw2_GetDisplayMode,
4554 ddraw2_GetFourCCCodes,
4555 ddraw2_GetGDISurface,
4556 ddraw2_GetMonitorFrequency,
4557 ddraw2_GetScanLine,
4558 ddraw2_GetVerticalBlankStatus,
4559 ddraw2_Initialize,
4560 ddraw2_RestoreDisplayMode,
4561 ddraw2_SetCooperativeLevel,
4562 ddraw2_SetDisplayMode,
4563 ddraw2_WaitForVerticalBlank,
4564 /* IDirectDraw2 */
4565 ddraw2_GetAvailableVidMem,
4568 static const struct IDirectDrawVtbl ddraw1_vtbl =
4570 /* IUnknown */
4571 ddraw1_QueryInterface,
4572 ddraw1_AddRef,
4573 ddraw1_Release,
4574 /* IDirectDraw */
4575 ddraw1_Compact,
4576 ddraw1_CreateClipper,
4577 ddraw1_CreatePalette,
4578 ddraw1_CreateSurface,
4579 ddraw1_DuplicateSurface,
4580 ddraw1_EnumDisplayModes,
4581 ddraw1_EnumSurfaces,
4582 ddraw1_FlipToGDISurface,
4583 ddraw1_GetCaps,
4584 ddraw1_GetDisplayMode,
4585 ddraw1_GetFourCCCodes,
4586 ddraw1_GetGDISurface,
4587 ddraw1_GetMonitorFrequency,
4588 ddraw1_GetScanLine,
4589 ddraw1_GetVerticalBlankStatus,
4590 ddraw1_Initialize,
4591 ddraw1_RestoreDisplayMode,
4592 ddraw1_SetCooperativeLevel,
4593 ddraw1_SetDisplayMode,
4594 ddraw1_WaitForVerticalBlank,
4597 static const struct IDirect3D7Vtbl d3d7_vtbl =
4599 /* IUnknown methods */
4600 d3d7_QueryInterface,
4601 d3d7_AddRef,
4602 d3d7_Release,
4603 /* IDirect3D7 methods */
4604 d3d7_EnumDevices,
4605 d3d7_CreateDevice,
4606 d3d7_CreateVertexBuffer,
4607 d3d7_EnumZBufferFormats,
4608 d3d7_EvictManagedTextures
4611 static const struct IDirect3D3Vtbl d3d3_vtbl =
4613 /* IUnknown methods */
4614 d3d3_QueryInterface,
4615 d3d3_AddRef,
4616 d3d3_Release,
4617 /* IDirect3D3 methods */
4618 d3d3_EnumDevices,
4619 d3d3_CreateLight,
4620 d3d3_CreateMaterial,
4621 d3d3_CreateViewport,
4622 d3d3_FindDevice,
4623 d3d3_CreateDevice,
4624 d3d3_CreateVertexBuffer,
4625 d3d3_EnumZBufferFormats,
4626 d3d3_EvictManagedTextures
4629 static const struct IDirect3D2Vtbl d3d2_vtbl =
4631 /* IUnknown methods */
4632 d3d2_QueryInterface,
4633 d3d2_AddRef,
4634 d3d2_Release,
4635 /* IDirect3D2 methods */
4636 d3d2_EnumDevices,
4637 d3d2_CreateLight,
4638 d3d2_CreateMaterial,
4639 d3d2_CreateViewport,
4640 d3d2_FindDevice,
4641 d3d2_CreateDevice
4644 static const struct IDirect3DVtbl d3d1_vtbl =
4646 /* IUnknown methods */
4647 d3d1_QueryInterface,
4648 d3d1_AddRef,
4649 d3d1_Release,
4650 /* IDirect3D methods */
4651 d3d1_Initialize,
4652 d3d1_EnumDevices,
4653 d3d1_CreateLight,
4654 d3d1_CreateMaterial,
4655 d3d1_CreateViewport,
4656 d3d1_FindDevice
4659 /*****************************************************************************
4660 * ddraw_find_decl
4662 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
4663 * if none was found.
4665 * This function is in ddraw.c and the DDraw object space because D3D7
4666 * vertex buffers are created using the IDirect3D interface to the ddraw
4667 * object, so they can be valid across D3D devices(theoretically. The ddraw
4668 * object also owns the wined3d device
4670 * Parameters:
4671 * This: Device
4672 * fvf: Fvf to find the decl for
4674 * Returns:
4675 * NULL in case of an error, the vertex declaration for the FVF otherwise.
4677 *****************************************************************************/
4678 struct wined3d_vertex_declaration *ddraw_find_decl(struct ddraw *This, DWORD fvf)
4680 struct wined3d_vertex_declaration *pDecl = NULL;
4681 HRESULT hr;
4682 int p, low, high; /* deliberately signed */
4683 struct FvfToDecl *convertedDecls = This->decls;
4685 TRACE("Searching for declaration for fvf %08x... ", fvf);
4687 low = 0;
4688 high = This->numConvertedDecls - 1;
4689 while(low <= high) {
4690 p = (low + high) >> 1;
4691 TRACE("%d ", p);
4692 if(convertedDecls[p].fvf == fvf) {
4693 TRACE("found %p\n", convertedDecls[p].decl);
4694 return convertedDecls[p].decl;
4695 } else if(convertedDecls[p].fvf < fvf) {
4696 low = p + 1;
4697 } else {
4698 high = p - 1;
4701 TRACE("not found. Creating and inserting at position %d.\n", low);
4703 hr = wined3d_vertex_declaration_create_from_fvf(This->wined3d_device,
4704 fvf, This, &ddraw_null_wined3d_parent_ops, &pDecl);
4705 if (hr != S_OK) return NULL;
4707 if(This->declArraySize == This->numConvertedDecls) {
4708 int grow = max(This->declArraySize / 2, 8);
4709 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
4710 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
4711 if (!convertedDecls)
4713 wined3d_vertex_declaration_decref(pDecl);
4714 return NULL;
4716 This->decls = convertedDecls;
4717 This->declArraySize += grow;
4720 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
4721 convertedDecls[low].decl = pDecl;
4722 convertedDecls[low].fvf = fvf;
4723 This->numConvertedDecls++;
4725 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
4726 return pDecl;
4729 static inline struct ddraw *ddraw_from_device_parent(struct wined3d_device_parent *device_parent)
4731 return CONTAINING_RECORD(device_parent, struct ddraw, device_parent);
4734 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
4735 struct wined3d_device *device)
4737 TRACE("device_parent %p, device %p.\n", device_parent, device);
4740 /* This is run from device_process_message() in wined3d, we can't take the
4741 * wined3d mutex. */
4742 /* FIXME: We only get mode change notifications in exclusive mode, but we
4743 * should mark surfaces as lost on mode changes in DDSCL_NORMAL mode as well. */
4744 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
4746 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4747 MONITORINFO monitor_info;
4748 HMONITOR monitor;
4749 RECT *r;
4751 TRACE("device_parent %p.\n", device_parent);
4753 if (!(ddraw->cooperative_level & DDSCL_EXCLUSIVE) || !ddraw->swapchain_window)
4755 TRACE("Nothing to resize.\n");
4756 return;
4759 monitor = MonitorFromWindow(ddraw->swapchain_window, MONITOR_DEFAULTTOPRIMARY);
4760 monitor_info.cbSize = sizeof(monitor_info);
4761 if (!GetMonitorInfoW(monitor, &monitor_info))
4763 ERR("Failed to get monitor info.\n");
4764 return;
4767 r = &monitor_info.rcMonitor;
4768 TRACE("Resizing window %p to %s.\n", ddraw->swapchain_window, wine_dbgstr_rect(r));
4770 if (!SetWindowPos(ddraw->swapchain_window, HWND_TOP, r->left, r->top,
4771 r->right - r->left, r->bottom - r->top, SWP_SHOWWINDOW | SWP_NOACTIVATE))
4772 ERR("Failed to resize window.\n");
4774 InterlockedCompareExchange(&ddraw->device_state, DDRAW_DEVICE_STATE_NOT_RESTORED, DDRAW_DEVICE_STATE_OK);
4777 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
4779 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4781 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
4783 if (!activate)
4785 ddraw->device_state = DDRAW_DEVICE_STATE_LOST;
4786 exclusive_window = NULL;
4788 else
4790 InterlockedCompareExchange(&ddraw->device_state, DDRAW_DEVICE_STATE_NOT_RESTORED, DDRAW_DEVICE_STATE_LOST);
4794 void ddraw_update_lost_surfaces(struct ddraw *ddraw)
4796 struct ddraw_surface *surface;
4798 if (ddraw->device_state != DDRAW_DEVICE_STATE_NOT_RESTORED)
4799 return;
4801 LIST_FOR_EACH_ENTRY(surface, &ddraw->surface_list, struct ddraw_surface, surface_list_entry)
4803 surface->is_lost = TRUE;
4805 ddraw->device_state = DDRAW_DEVICE_STATE_OK;
4808 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
4809 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
4810 void **parent, const struct wined3d_parent_ops **parent_ops)
4812 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4813 struct ddraw_surface *ddraw_surface;
4815 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
4816 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
4818 /* We have a swapchain or wined3d internal texture. */
4819 if (!wined3d_texture_get_parent(wined3d_texture) || wined3d_texture_get_parent(wined3d_texture) == ddraw)
4821 *parent = NULL;
4822 *parent_ops = &ddraw_null_wined3d_parent_ops;
4824 return DD_OK;
4827 if (!(ddraw_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw_surface))))
4829 ERR("Failed to allocate surface memory.\n");
4830 return DDERR_OUTOFVIDEOMEMORY;
4833 ddraw_surface_init(ddraw_surface, ddraw, wined3d_texture, sub_resource_idx, parent_ops);
4834 *parent = ddraw_surface;
4836 ddraw_update_lost_surfaces(ddraw);
4837 list_add_head(&ddraw->surface_list, &ddraw_surface->surface_list_entry);
4839 TRACE("Created ddraw surface %p.\n", ddraw_surface);
4841 return DD_OK;
4844 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
4845 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
4846 void **parent, const struct wined3d_parent_ops **parent_ops)
4848 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
4849 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
4851 *parent = NULL;
4852 *parent_ops = &ddraw_null_wined3d_parent_ops;
4854 return DD_OK;
4857 static void STDMETHODCALLTYPE ddraw_frontbuffer_destroyed(void *parent)
4859 struct ddraw *ddraw = parent;
4860 ddraw->wined3d_frontbuffer = NULL;
4863 static const struct wined3d_parent_ops ddraw_frontbuffer_parent_ops =
4865 ddraw_frontbuffer_destroyed,
4868 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
4869 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
4870 struct wined3d_texture **texture)
4872 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4873 HRESULT hr;
4875 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
4876 device_parent, container_parent, desc, texture_flags, texture);
4878 if (ddraw->wined3d_frontbuffer)
4880 ERR("Frontbuffer already created.\n");
4881 return E_FAIL;
4884 if (FAILED(hr = wined3d_texture_create(ddraw->wined3d_device, desc, 1, 1,
4885 texture_flags | WINED3D_TEXTURE_CREATE_MAPPABLE, NULL, ddraw, &ddraw_frontbuffer_parent_ops, texture)))
4887 WARN("Failed to create texture, hr %#x.\n", hr);
4888 return hr;
4891 ddraw->wined3d_frontbuffer = *texture;
4893 return hr;
4896 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
4897 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
4899 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4900 HRESULT hr;
4902 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
4904 if (ddraw->wined3d_swapchain)
4906 ERR("Swapchain already created.\n");
4907 return E_FAIL;
4910 if (FAILED(hr = wined3d_swapchain_create(ddraw->wined3d_device, desc, NULL,
4911 &ddraw_null_wined3d_parent_ops, swapchain)))
4912 WARN("Failed to create swapchain, hr %#x.\n", hr);
4914 return hr;
4917 static const struct wined3d_device_parent_ops ddraw_wined3d_device_parent_ops =
4919 device_parent_wined3d_device_created,
4920 device_parent_mode_changed,
4921 device_parent_activate,
4922 device_parent_surface_created,
4923 device_parent_volume_created,
4924 device_parent_create_swapchain_texture,
4925 device_parent_create_swapchain,
4928 HRESULT ddraw_init(struct ddraw *ddraw, DWORD flags, enum wined3d_device_type device_type)
4930 WINED3DCAPS caps;
4931 HRESULT hr;
4933 ddraw->IDirectDraw7_iface.lpVtbl = &ddraw7_vtbl;
4934 ddraw->IDirectDraw_iface.lpVtbl = &ddraw1_vtbl;
4935 ddraw->IDirectDraw2_iface.lpVtbl = &ddraw2_vtbl;
4936 ddraw->IDirectDraw4_iface.lpVtbl = &ddraw4_vtbl;
4937 ddraw->IDirect3D_iface.lpVtbl = &d3d1_vtbl;
4938 ddraw->IDirect3D2_iface.lpVtbl = &d3d2_vtbl;
4939 ddraw->IDirect3D3_iface.lpVtbl = &d3d3_vtbl;
4940 ddraw->IDirect3D7_iface.lpVtbl = &d3d7_vtbl;
4941 ddraw->device_parent.ops = &ddraw_wined3d_device_parent_ops;
4942 ddraw->numIfaces = 1;
4943 ddraw->ref7 = 1;
4945 flags |= DDRAW_WINED3D_FLAGS;
4946 if (!(ddraw->wined3d = wined3d_create(flags)))
4948 flags |= WINED3D_NO3D;
4949 if (!(ddraw->wined3d = wined3d_create(flags)))
4951 WARN("Failed to create a wined3d object.\n");
4952 return E_FAIL;
4956 if (FAILED(hr = wined3d_get_device_caps(ddraw->wined3d, WINED3DADAPTER_DEFAULT, device_type, &caps)))
4958 ERR("Failed to get device caps, hr %#x.\n", hr);
4959 wined3d_decref(ddraw->wined3d);
4960 return E_FAIL;
4963 if (!(caps.ddraw_caps.caps & WINEDDCAPS_3D))
4965 WARN("Created a wined3d object without 3D support.\n");
4966 ddraw->flags |= DDRAW_NO3D;
4969 if (FAILED(hr = wined3d_device_create(ddraw->wined3d, WINED3DADAPTER_DEFAULT, device_type,
4970 NULL, 0, DDRAW_STRIDE_ALIGNMENT, &ddraw->device_parent, &ddraw->wined3d_device)))
4972 WARN("Failed to create a wined3d device, hr %#x.\n", hr);
4973 wined3d_decref(ddraw->wined3d);
4974 return hr;
4977 list_init(&ddraw->surface_list);
4979 return DD_OK;