mstask: Improve stubs for ITask::GetTaskFlags and ITask::GetFlags.
[wine.git] / dlls / ddraw / ddraw.c
blob32f01722630dd6e6e5a06f10c3a4eaa5c2c1173f
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 heap_free(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 heap_free(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 if (!(ddraw->decls = heap_alloc_zero(ddraw->declArraySize * sizeof(*ddraw->decls))))
605 ERR("Error allocating an array for the converted vertex decls.\n");
606 ddraw->declArraySize = 0;
607 hr = wined3d_device_uninit_3d(ddraw->wined3d_device);
608 return E_OUTOFMEMORY;
611 TRACE("Successfully initialized 3D.\n");
613 return DD_OK;
616 static HRESULT ddraw_create_swapchain(struct ddraw *ddraw, HWND window, BOOL windowed)
618 struct wined3d_swapchain_desc swapchain_desc;
619 struct wined3d_display_mode mode;
620 HRESULT hr = WINED3D_OK;
622 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
624 ERR("Failed to get display mode.\n");
625 return hr;
628 memset(&swapchain_desc, 0, sizeof(swapchain_desc));
629 swapchain_desc.backbuffer_width = mode.width;
630 swapchain_desc.backbuffer_height = mode.height;
631 swapchain_desc.backbuffer_format = mode.format_id;
632 swapchain_desc.backbuffer_usage = 0;
633 swapchain_desc.backbuffer_count = 1;
634 swapchain_desc.swap_effect = WINED3D_SWAP_EFFECT_DISCARD;
635 swapchain_desc.device_window = window;
636 swapchain_desc.windowed = windowed;
637 swapchain_desc.flags = WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
639 if (!(ddraw->flags & DDRAW_NO3D))
640 hr = ddraw_attach_d3d_device(ddraw, &swapchain_desc);
641 else
642 hr = wined3d_device_init_gdi(ddraw->wined3d_device, &swapchain_desc);
644 if (FAILED(hr))
646 ERR("Failed to create swapchain, hr %#x.\n", hr);
647 return hr;
650 if (!(ddraw->wined3d_swapchain = wined3d_device_get_swapchain(ddraw->wined3d_device, 0)))
652 ERR("Failed to get swapchain.\n");
653 return DDERR_INVALIDPARAMS;
656 wined3d_swapchain_incref(ddraw->wined3d_swapchain);
657 ddraw_set_swapchain_window(ddraw, window);
659 if (ddraw->primary && ddraw->primary->palette)
660 wined3d_swapchain_set_palette(ddraw->wined3d_swapchain, ddraw->primary->palette->wined3d_palette);
662 return DD_OK;
665 /*****************************************************************************
666 * IDirectDraw7::RestoreDisplayMode
668 * Restores the display mode to what it was at creation time. Basically.
670 * Returns
671 * DD_OK on success
672 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
674 *****************************************************************************/
675 static HRESULT WINAPI ddraw7_RestoreDisplayMode(IDirectDraw7 *iface)
677 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
678 HRESULT hr;
680 TRACE("iface %p.\n", iface);
682 wined3d_mutex_lock();
684 if (!(ddraw->flags & DDRAW_RESTORE_MODE))
686 wined3d_mutex_unlock();
687 return DD_OK;
690 if (exclusive_ddraw && exclusive_ddraw != ddraw)
692 wined3d_mutex_unlock();
693 return DDERR_NOEXCLUSIVEMODE;
696 if (SUCCEEDED(hr = wined3d_set_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, NULL)))
697 ddraw->flags &= ~DDRAW_RESTORE_MODE;
699 InterlockedCompareExchange(&ddraw->device_state, DDRAW_DEVICE_STATE_NOT_RESTORED, DDRAW_DEVICE_STATE_OK);
701 wined3d_mutex_unlock();
703 return hr;
706 static HRESULT WINAPI ddraw4_RestoreDisplayMode(IDirectDraw4 *iface)
708 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
710 TRACE("iface %p.\n", iface);
712 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
715 static HRESULT WINAPI ddraw2_RestoreDisplayMode(IDirectDraw2 *iface)
717 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
719 TRACE("iface %p.\n", iface);
721 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
724 static HRESULT WINAPI ddraw1_RestoreDisplayMode(IDirectDraw *iface)
726 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
728 TRACE("iface %p.\n", iface);
730 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
733 /*****************************************************************************
734 * IDirectDraw7::SetCooperativeLevel
736 * Sets the cooperative level for the DirectDraw object, and the window
737 * assigned to it. The cooperative level determines the general behavior
738 * of the DirectDraw application
740 * Warning: This is quite tricky, as it's not really documented which
741 * cooperative levels can be combined with each other. If a game fails
742 * after this function, try to check the cooperative levels passed on
743 * Windows, and if it returns something different.
745 * If you think that this function caused the failure because it writes a
746 * fixme, be sure to run again with a +ddraw trace.
748 * What is known about cooperative levels (See the ddraw modes test):
749 * DDSCL_EXCLUSIVE requires DDSCL_FULLSCREEN.
750 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE.
751 * Unlike what msdn claims, DDSCL_NORMAL | DDSCL_FULLSCREEN is allowed.
752 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
753 * DDSCL_EXCLUSIVE can be activated.
754 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES or
755 * DDSCL_CREATEDEVICEWINDOW.
757 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
758 * DDSCL_CREATEDEVICEWINDOW, DDSCL_SETDEVICEWINDOW
759 * DDSCL_SETFOCUSWINDOW (partially),
760 * DDSCL_MULTITHREADED (work in progress)
761 * DDSCL_FPUPRESERVE (see device.c)
763 * Unsure about this: DDSCL_FPUSETUP
765 * These don't seem very important for wine:
766 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
768 * Returns:
769 * DD_OK if the cooperative level was set successfully
770 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
771 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
772 * (Probably others too, have to investigate)
774 *****************************************************************************/
775 static HRESULT ddraw_set_cooperative_level(struct ddraw *ddraw, HWND window,
776 DWORD cooplevel, BOOL restore_mode_on_normal)
778 struct wined3d_rendertarget_view *rtv = NULL, *dsv = NULL;
779 struct wined3d_stateblock *stateblock;
780 BOOL restore_state = FALSE;
781 HRESULT hr;
783 TRACE("ddraw %p, window %p, flags %#x, restore_mode_on_normal %x.\n", ddraw, window, cooplevel,
784 restore_mode_on_normal);
785 DDRAW_dump_cooperativelevel(cooplevel);
787 wined3d_mutex_lock();
789 if (ddraw->flags & DDRAW_SCL_RECURSIVE)
791 WARN("Recursive call, returning DD_OK.\n");
792 hr = DD_OK;
793 goto done;
795 ddraw->flags |= DDRAW_SCL_RECURSIVE;
797 /* Tests suggest that we need one of them: */
798 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
799 DDSCL_NORMAL |
800 DDSCL_EXCLUSIVE )))
802 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
803 hr = DDERR_INVALIDPARAMS;
804 goto done;
807 if ((cooplevel & DDSCL_CREATEDEVICEWINDOW) && !(cooplevel & DDSCL_EXCLUSIVE))
809 WARN("DDSCL_CREATEDEVICEWINDOW requires DDSCL_EXCLUSIVE.\n");
810 hr = DDERR_INVALIDPARAMS;
811 goto done;
814 /* Handle those levels first which set various hwnds */
815 if ((cooplevel & DDSCL_SETFOCUSWINDOW) && !(cooplevel & DDSCL_CREATEDEVICEWINDOW))
817 /* This isn't compatible with a lot of flags */
818 if (cooplevel & (DDSCL_MULTITHREADED
819 | DDSCL_FPUSETUP
820 | DDSCL_FPUPRESERVE
821 | DDSCL_ALLOWREBOOT
822 | DDSCL_ALLOWMODEX
823 | DDSCL_SETDEVICEWINDOW
824 | DDSCL_NORMAL
825 | DDSCL_EXCLUSIVE
826 | DDSCL_FULLSCREEN))
828 WARN("Called with incompatible flags, returning DDERR_INVALIDPARAMS.\n");
829 hr = DDERR_INVALIDPARAMS;
830 goto done;
833 hr = ddraw_set_focus_window(ddraw, window);
834 goto done;
837 if (cooplevel & DDSCL_EXCLUSIVE)
839 if (!(cooplevel & DDSCL_FULLSCREEN) || !(window || (cooplevel & DDSCL_CREATEDEVICEWINDOW)))
841 WARN("DDSCL_EXCLUSIVE requires DDSCL_FULLSCREEN and a window.\n");
842 hr = DDERR_INVALIDPARAMS;
843 goto done;
846 if (cooplevel & DDSCL_CREATEDEVICEWINDOW)
848 HWND device_window;
850 if (!ddraw->focuswindow && !(cooplevel & DDSCL_SETFOCUSWINDOW))
852 WARN("No focus window set.\n");
853 hr = DDERR_NOFOCUSWINDOW;
854 goto done;
857 device_window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "DirectDrawDeviceWnd",
858 WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
859 NULL, NULL, NULL, NULL);
860 if (!device_window)
862 ERR("Failed to create window, last error %#x.\n", GetLastError());
863 hr = E_FAIL;
864 goto done;
867 ShowWindow(device_window, SW_SHOW);
868 TRACE("Created a device window %p.\n", device_window);
870 /* Native apparently leaks the created device window if setting the
871 * focus window below fails. */
872 ddraw->cooperative_level |= DDSCL_CREATEDEVICEWINDOW;
873 ddraw->devicewindow = device_window;
875 if (cooplevel & DDSCL_SETFOCUSWINDOW)
877 if (!window)
879 hr = DDERR_NOHWND;
880 goto done;
883 if (FAILED(hr = ddraw_set_focus_window(ddraw, window)))
884 goto done;
887 window = device_window;
890 else
892 if (ddraw->cooperative_level & DDSCL_CREATEDEVICEWINDOW)
893 DestroyWindow(ddraw->devicewindow);
894 ddraw->devicewindow = NULL;
895 ddraw->focuswindow = NULL;
898 if ((cooplevel & DDSCL_FULLSCREEN) != (ddraw->cooperative_level & DDSCL_FULLSCREEN) || window != ddraw->dest_window)
900 if (ddraw->cooperative_level & DDSCL_FULLSCREEN)
901 wined3d_device_restore_fullscreen_window(ddraw->wined3d_device, ddraw->dest_window, NULL);
903 if (cooplevel & DDSCL_FULLSCREEN)
905 struct wined3d_display_mode display_mode;
907 wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &display_mode, NULL);
908 wined3d_device_setup_fullscreen_window(ddraw->wined3d_device, window,
909 display_mode.width, display_mode.height);
913 if ((cooplevel & DDSCL_EXCLUSIVE) && exclusive_window != window)
915 ddraw->device_state = DDRAW_DEVICE_STATE_NOT_RESTORED;
916 exclusive_window = window;
919 if (cooplevel & DDSCL_MULTITHREADED && !(ddraw->cooperative_level & DDSCL_MULTITHREADED))
920 wined3d_device_set_multithreaded(ddraw->wined3d_device);
922 if (ddraw->wined3d_swapchain)
924 if (!(ddraw->flags & DDRAW_NO3D))
926 restore_state = TRUE;
928 if (FAILED(hr = wined3d_stateblock_create(ddraw->wined3d_device, WINED3D_SBT_ALL, &stateblock)))
930 ERR("Failed to create stateblock, hr %#x.\n", hr);
931 goto done;
934 wined3d_stateblock_capture(stateblock);
935 rtv = wined3d_device_get_rendertarget_view(ddraw->wined3d_device, 0);
936 /* Rendering to ddraw->wined3d_frontbuffer. */
937 if (rtv && !wined3d_rendertarget_view_get_sub_resource_parent(rtv))
938 rtv = NULL;
939 else if (rtv)
940 wined3d_rendertarget_view_incref(rtv);
942 if ((dsv = wined3d_device_get_depth_stencil_view(ddraw->wined3d_device)))
943 wined3d_rendertarget_view_incref(dsv);
946 ddraw_destroy_swapchain(ddraw);
949 if (FAILED(hr = ddraw_create_swapchain(ddraw, window, !(cooplevel & DDSCL_FULLSCREEN))))
950 ERR("Failed to create swapchain, hr %#x.\n", hr);
952 if (restore_state)
954 if (dsv)
956 wined3d_device_set_depth_stencil_view(ddraw->wined3d_device, dsv);
957 wined3d_rendertarget_view_decref(dsv);
960 if (rtv)
962 wined3d_device_set_rendertarget_view(ddraw->wined3d_device, 0, rtv, FALSE);
963 wined3d_rendertarget_view_decref(rtv);
966 wined3d_stateblock_apply(stateblock);
967 wined3d_stateblock_decref(stateblock);
970 if (!(cooplevel & DDSCL_EXCLUSIVE) && (ddraw->cooperative_level & DDSCL_EXCLUSIVE)
971 && restore_mode_on_normal)
973 hr = ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
974 if (FAILED(hr))
975 ERR("RestoreDisplayMode failed\n");
978 if ((ddraw->cooperative_level & DDSCL_EXCLUSIVE)
979 && (window != ddraw->dest_window || !(cooplevel & DDSCL_EXCLUSIVE)))
980 wined3d_device_release_focus_window(ddraw->wined3d_device);
982 if ((cooplevel & DDSCL_EXCLUSIVE)
983 && (window != ddraw->dest_window || !(ddraw->cooperative_level & DDSCL_EXCLUSIVE)))
985 hr = wined3d_device_acquire_focus_window(ddraw->wined3d_device, window);
986 if (FAILED(hr))
988 ERR("Failed to acquire focus window, hr %#x.\n", hr);
989 goto done;
993 /* Unhandled flags */
994 if (cooplevel & DDSCL_ALLOWREBOOT)
995 WARN("Unhandled flag DDSCL_ALLOWREBOOT, harmless\n");
996 if (cooplevel & DDSCL_ALLOWMODEX)
997 WARN("Unhandled flag DDSCL_ALLOWMODEX, harmless\n");
998 if (cooplevel & DDSCL_FPUSETUP)
999 WARN("Unhandled flag DDSCL_FPUSETUP, harmless\n");
1001 if (cooplevel & DDSCL_EXCLUSIVE)
1002 exclusive_ddraw = ddraw;
1003 else if (exclusive_ddraw == ddraw)
1004 exclusive_ddraw = NULL;
1006 /* Store the cooperative_level */
1007 ddraw->cooperative_level = cooplevel;
1008 ddraw->dest_window = window;
1010 TRACE("SetCooperativeLevel returning DD_OK\n");
1011 hr = DD_OK;
1012 done:
1013 ddraw->flags &= ~DDRAW_SCL_RECURSIVE;
1014 wined3d_mutex_unlock();
1016 return hr;
1019 static HRESULT WINAPI ddraw7_SetCooperativeLevel(IDirectDraw7 *iface, HWND window, DWORD flags)
1021 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1023 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1025 return ddraw_set_cooperative_level(ddraw, window, flags, !(ddraw->flags & DDRAW_SCL_DDRAW1));
1028 static HRESULT WINAPI ddraw4_SetCooperativeLevel(IDirectDraw4 *iface, HWND window, DWORD flags)
1030 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1032 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1034 return ddraw_set_cooperative_level(ddraw, window, flags, !(ddraw->flags & DDRAW_SCL_DDRAW1));
1037 static HRESULT WINAPI ddraw2_SetCooperativeLevel(IDirectDraw2 *iface, HWND window, DWORD flags)
1039 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1041 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1043 return ddraw_set_cooperative_level(ddraw, window, flags, !(ddraw->flags & DDRAW_SCL_DDRAW1));
1046 static HRESULT WINAPI ddraw1_SetCooperativeLevel(IDirectDraw *iface, HWND window, DWORD flags)
1048 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1049 HRESULT hr;
1051 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1053 hr = ddraw_set_cooperative_level(ddraw, window, flags, FALSE);
1054 if (SUCCEEDED(hr))
1055 ddraw->flags |= DDRAW_SCL_DDRAW1;
1056 return hr;
1059 /*****************************************************************************
1060 * IDirectDraw7::SetDisplayMode
1062 * Sets the display screen resolution, color depth and refresh frequency
1063 * when in fullscreen mode (in theory).
1064 * Possible return values listed in the SDK suggest that this method fails
1065 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
1066 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
1067 * It seems to be valid to pass 0 for With and Height, this has to be tested
1068 * It could mean that the current video mode should be left as-is. (But why
1069 * call it then?)
1071 * Params:
1072 * Height, Width: Screen dimension
1073 * BPP: Color depth in Bits per pixel
1074 * Refreshrate: Screen refresh rate
1075 * Flags: Other stuff
1077 * Returns
1078 * DD_OK on success
1080 *****************************************************************************/
1081 static HRESULT WINAPI ddraw7_SetDisplayMode(IDirectDraw7 *iface, DWORD width, DWORD height,
1082 DWORD bpp, DWORD refresh_rate, DWORD flags)
1084 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1085 struct wined3d_display_mode mode;
1086 enum wined3d_format_id format;
1087 HRESULT hr;
1089 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1090 iface, width, height, bpp, refresh_rate, flags);
1092 if (force_refresh_rate != 0)
1094 TRACE("ForceRefreshRate overriding passed-in refresh rate (%u Hz) to %u Hz\n",
1095 refresh_rate, force_refresh_rate);
1096 refresh_rate = force_refresh_rate;
1099 wined3d_mutex_lock();
1101 if (exclusive_ddraw && exclusive_ddraw != ddraw)
1103 wined3d_mutex_unlock();
1104 return DDERR_NOEXCLUSIVEMODE;
1107 if (!width || !height)
1109 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here. */
1110 wined3d_mutex_unlock();
1111 return DD_OK;
1114 switch (bpp)
1116 case 8: format = WINED3DFMT_P8_UINT; break;
1117 case 15: format = WINED3DFMT_B5G5R5X1_UNORM; break;
1118 case 16: format = WINED3DFMT_B5G6R5_UNORM; break;
1119 case 24: format = WINED3DFMT_B8G8R8_UNORM; break;
1120 case 32: format = WINED3DFMT_B8G8R8X8_UNORM; break;
1121 default: format = WINED3DFMT_UNKNOWN; break;
1124 mode.width = width;
1125 mode.height = height;
1126 mode.refresh_rate = refresh_rate;
1127 mode.format_id = format;
1128 mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
1130 /* TODO: The possible return values from msdn suggest that the screen mode
1131 * can't be changed if a surface is locked or some drawing is in progress. */
1132 if (SUCCEEDED(hr = wined3d_set_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode)))
1134 if (ddraw->primary)
1136 DDSURFACEDESC2 *surface_desc = &ddraw->primary->surface_desc;
1138 if (FAILED(hr = wined3d_swapchain_resize_buffers(ddraw->wined3d_swapchain, 0,
1139 surface_desc->dwWidth, surface_desc->dwHeight, mode.format_id, WINED3D_MULTISAMPLE_NONE, 0)))
1140 ERR("Failed to resize buffers, hr %#x.\n", hr);
1141 else
1142 ddrawformat_from_wined3dformat(&ddraw->primary->surface_desc.u4.ddpfPixelFormat, mode.format_id);
1144 ddraw->flags |= DDRAW_RESTORE_MODE;
1147 InterlockedCompareExchange(&ddraw->device_state, DDRAW_DEVICE_STATE_NOT_RESTORED, DDRAW_DEVICE_STATE_OK);
1149 wined3d_mutex_unlock();
1151 switch (hr)
1153 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1154 default: return hr;
1158 static HRESULT WINAPI ddraw4_SetDisplayMode(IDirectDraw4 *iface, DWORD width, DWORD height,
1159 DWORD bpp, DWORD refresh_rate, DWORD flags)
1161 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1163 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1164 iface, width, height, bpp, refresh_rate, flags);
1166 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1169 static HRESULT WINAPI ddraw2_SetDisplayMode(IDirectDraw2 *iface,
1170 DWORD width, DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
1172 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1174 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1175 iface, width, height, bpp, refresh_rate, flags);
1177 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1180 static HRESULT WINAPI ddraw1_SetDisplayMode(IDirectDraw *iface, DWORD width, DWORD height, DWORD bpp)
1182 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1184 TRACE("iface %p, width %u, height %u, bpp %u.\n", iface, width, height, bpp);
1186 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, 0, 0);
1189 void ddraw_d3dcaps1_from_7(D3DDEVICEDESC *caps1, D3DDEVICEDESC7 *caps7)
1191 memset(caps1, 0, sizeof(*caps1));
1192 caps1->dwSize = sizeof(*caps1);
1193 caps1->dwFlags = D3DDD_COLORMODEL
1194 | D3DDD_DEVCAPS
1195 | D3DDD_TRANSFORMCAPS
1196 | D3DDD_BCLIPPING
1197 | D3DDD_LIGHTINGCAPS
1198 | D3DDD_LINECAPS
1199 | D3DDD_TRICAPS
1200 | D3DDD_DEVICERENDERBITDEPTH
1201 | D3DDD_DEVICEZBUFFERBITDEPTH
1202 | D3DDD_MAXBUFFERSIZE
1203 | D3DDD_MAXVERTEXCOUNT;
1204 caps1->dcmColorModel = D3DCOLOR_RGB;
1205 caps1->dwDevCaps = caps7->dwDevCaps;
1206 caps1->dtcTransformCaps.dwSize = sizeof(caps1->dtcTransformCaps);
1207 caps1->dtcTransformCaps.dwCaps = D3DTRANSFORMCAPS_CLIP;
1208 caps1->bClipping = TRUE;
1209 caps1->dlcLightingCaps.dwSize = sizeof(caps1->dlcLightingCaps);
1210 caps1->dlcLightingCaps.dwCaps = D3DLIGHTCAPS_DIRECTIONAL
1211 | D3DLIGHTCAPS_PARALLELPOINT
1212 | D3DLIGHTCAPS_POINT
1213 | D3DLIGHTCAPS_SPOT;
1214 caps1->dlcLightingCaps.dwLightingModel = D3DLIGHTINGMODEL_RGB;
1215 caps1->dlcLightingCaps.dwNumLights = caps7->dwMaxActiveLights;
1216 caps1->dpcLineCaps = caps7->dpcLineCaps;
1217 caps1->dpcTriCaps = caps7->dpcTriCaps;
1218 caps1->dwDeviceRenderBitDepth = caps7->dwDeviceRenderBitDepth;
1219 caps1->dwDeviceZBufferBitDepth = caps7->dwDeviceZBufferBitDepth;
1220 caps1->dwMaxBufferSize = 0;
1221 caps1->dwMaxVertexCount = 65536;
1222 caps1->dwMinTextureWidth = caps7->dwMinTextureWidth;
1223 caps1->dwMinTextureHeight = caps7->dwMinTextureHeight;
1224 caps1->dwMaxTextureWidth = caps7->dwMaxTextureWidth;
1225 caps1->dwMaxTextureHeight = caps7->dwMaxTextureHeight;
1226 caps1->dwMinStippleWidth = 1;
1227 caps1->dwMinStippleHeight = 1;
1228 caps1->dwMaxStippleWidth = 32;
1229 caps1->dwMaxStippleHeight = 32;
1230 caps1->dwMaxTextureRepeat = caps7->dwMaxTextureRepeat;
1231 caps1->dwMaxTextureAspectRatio = caps7->dwMaxTextureAspectRatio;
1232 caps1->dwMaxAnisotropy = caps7->dwMaxAnisotropy;
1233 caps1->dvGuardBandLeft = caps7->dvGuardBandLeft;
1234 caps1->dvGuardBandTop = caps7->dvGuardBandTop;
1235 caps1->dvGuardBandRight = caps7->dvGuardBandRight;
1236 caps1->dvGuardBandBottom = caps7->dvGuardBandBottom;
1237 caps1->dvExtentsAdjust = caps7->dvExtentsAdjust;
1238 caps1->dwStencilCaps = caps7->dwStencilCaps;
1239 caps1->dwFVFCaps = caps7->dwFVFCaps;
1240 caps1->dwTextureOpCaps = caps7->dwTextureOpCaps;
1241 caps1->wMaxTextureBlendStages = caps7->wMaxTextureBlendStages;
1242 caps1->wMaxSimultaneousTextures = caps7->wMaxSimultaneousTextures;
1245 HRESULT ddraw_get_d3dcaps(const struct ddraw *ddraw, D3DDEVICEDESC7 *caps)
1247 WINED3DCAPS wined3d_caps;
1248 HRESULT hr;
1250 TRACE("ddraw %p, caps %p.\n", ddraw, caps);
1252 memset(&wined3d_caps, 0, sizeof(wined3d_caps));
1254 wined3d_mutex_lock();
1255 hr = wined3d_get_device_caps(ddraw->wined3d, 0, WINED3D_DEVICE_TYPE_HAL, &wined3d_caps);
1256 wined3d_mutex_unlock();
1257 if (FAILED(hr))
1259 WARN("Failed to get device caps, hr %#x.\n", hr);
1260 return hr;
1263 caps->dwDevCaps = wined3d_caps.DevCaps;
1264 caps->dpcLineCaps.dwMiscCaps = wined3d_caps.PrimitiveMiscCaps;
1265 caps->dpcLineCaps.dwRasterCaps = wined3d_caps.RasterCaps;
1266 caps->dpcLineCaps.dwZCmpCaps = wined3d_caps.ZCmpCaps;
1267 caps->dpcLineCaps.dwSrcBlendCaps = wined3d_caps.SrcBlendCaps;
1268 caps->dpcLineCaps.dwDestBlendCaps = wined3d_caps.DestBlendCaps;
1269 caps->dpcLineCaps.dwAlphaCmpCaps = wined3d_caps.AlphaCmpCaps;
1270 caps->dpcLineCaps.dwShadeCaps = wined3d_caps.ShadeCaps;
1271 caps->dpcLineCaps.dwTextureCaps = wined3d_caps.TextureCaps;
1272 caps->dpcLineCaps.dwTextureFilterCaps = wined3d_caps.TextureFilterCaps;
1273 caps->dpcLineCaps.dwTextureAddressCaps = wined3d_caps.TextureAddressCaps;
1275 caps->dwMaxTextureWidth = wined3d_caps.MaxTextureWidth;
1276 caps->dwMaxTextureHeight = wined3d_caps.MaxTextureHeight;
1278 caps->dwMaxTextureRepeat = wined3d_caps.MaxTextureRepeat;
1279 caps->dwMaxTextureAspectRatio = wined3d_caps.MaxTextureAspectRatio;
1280 caps->dwMaxAnisotropy = wined3d_caps.MaxAnisotropy;
1281 caps->dvMaxVertexW = wined3d_caps.MaxVertexW;
1283 caps->dvGuardBandLeft = wined3d_caps.GuardBandLeft;
1284 caps->dvGuardBandTop = wined3d_caps.GuardBandTop;
1285 caps->dvGuardBandRight = wined3d_caps.GuardBandRight;
1286 caps->dvGuardBandBottom = wined3d_caps.GuardBandBottom;
1288 caps->dvExtentsAdjust = wined3d_caps.ExtentsAdjust;
1289 caps->dwStencilCaps = wined3d_caps.StencilCaps;
1291 caps->dwFVFCaps = wined3d_caps.FVFCaps;
1292 caps->dwTextureOpCaps = wined3d_caps.TextureOpCaps;
1294 caps->dwVertexProcessingCaps = wined3d_caps.VertexProcessingCaps;
1295 caps->dwMaxActiveLights = wined3d_caps.MaxActiveLights;
1297 /* Remove all non-d3d7 caps */
1298 caps->dwDevCaps &= (
1299 D3DDEVCAPS_FLOATTLVERTEX | D3DDEVCAPS_SORTINCREASINGZ | D3DDEVCAPS_SORTDECREASINGZ |
1300 D3DDEVCAPS_SORTEXACT | D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY |
1301 D3DDEVCAPS_TLVERTEXSYSTEMMEMORY | D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY |
1302 D3DDEVCAPS_TEXTUREVIDEOMEMORY | D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP |
1303 D3DDEVCAPS_TEXTURENONLOCALVIDMEM | D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
1304 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT | D3DDEVCAPS_CANBLTSYSTONONLOCAL |
1305 D3DDEVCAPS_HWRASTERIZATION);
1307 caps->dwStencilCaps &= (
1308 D3DSTENCILCAPS_KEEP | D3DSTENCILCAPS_ZERO | D3DSTENCILCAPS_REPLACE |
1309 D3DSTENCILCAPS_INCRSAT | D3DSTENCILCAPS_DECRSAT | D3DSTENCILCAPS_INVERT |
1310 D3DSTENCILCAPS_INCR | D3DSTENCILCAPS_DECR);
1312 /* FVF caps ?*/
1314 caps->dwTextureOpCaps &= (
1315 D3DTEXOPCAPS_DISABLE | D3DTEXOPCAPS_SELECTARG1 | D3DTEXOPCAPS_SELECTARG2 |
1316 D3DTEXOPCAPS_MODULATE | D3DTEXOPCAPS_MODULATE2X | D3DTEXOPCAPS_MODULATE4X |
1317 D3DTEXOPCAPS_ADD | D3DTEXOPCAPS_ADDSIGNED | D3DTEXOPCAPS_ADDSIGNED2X |
1318 D3DTEXOPCAPS_SUBTRACT | D3DTEXOPCAPS_ADDSMOOTH | D3DTEXOPCAPS_BLENDTEXTUREALPHA |
1319 D3DTEXOPCAPS_BLENDFACTORALPHA | D3DTEXOPCAPS_BLENDTEXTUREALPHAPM | D3DTEXOPCAPS_BLENDCURRENTALPHA |
1320 D3DTEXOPCAPS_PREMODULATE | D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
1321 D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA | D3DTEXOPCAPS_BUMPENVMAP |
1322 D3DTEXOPCAPS_BUMPENVMAPLUMINANCE | D3DTEXOPCAPS_DOTPRODUCT3);
1324 caps->dwVertexProcessingCaps &= (
1325 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_VERTEXFOG |
1326 D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER);
1328 caps->dpcLineCaps.dwMiscCaps &= (
1329 D3DPMISCCAPS_MASKPLANES | D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP |
1330 D3DPMISCCAPS_CONFORMANT | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW |
1331 D3DPMISCCAPS_CULLCCW);
1333 caps->dpcLineCaps.dwRasterCaps &= (
1334 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ROP2 | D3DPRASTERCAPS_XOR |
1335 D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_SUBPIXEL |
1336 D3DPRASTERCAPS_SUBPIXELX | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE |
1337 D3DPRASTERCAPS_STIPPLE | D3DPRASTERCAPS_ANTIALIASSORTDEPENDENT | D3DPRASTERCAPS_ANTIALIASSORTINDEPENDENT |
1338 D3DPRASTERCAPS_ANTIALIASEDGES | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBIAS |
1339 D3DPRASTERCAPS_ZBUFFERLESSHSR | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY |
1340 D3DPRASTERCAPS_WBUFFER | D3DPRASTERCAPS_TRANSLUCENTSORTINDEPENDENT | D3DPRASTERCAPS_WFOG |
1341 D3DPRASTERCAPS_ZFOG);
1343 caps->dpcLineCaps.dwZCmpCaps &= (
1344 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
1345 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
1346 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
1348 caps->dpcLineCaps.dwSrcBlendCaps &= (
1349 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
1350 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
1351 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
1352 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
1353 D3DPBLENDCAPS_BOTHINVSRCALPHA);
1355 caps->dpcLineCaps.dwDestBlendCaps &= (
1356 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
1357 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
1358 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
1359 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
1360 D3DPBLENDCAPS_BOTHINVSRCALPHA);
1362 caps->dpcLineCaps.dwAlphaCmpCaps &= (
1363 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
1364 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
1365 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
1367 caps->dpcLineCaps.dwShadeCaps &= (
1368 D3DPSHADECAPS_COLORFLATMONO | D3DPSHADECAPS_COLORFLATRGB | D3DPSHADECAPS_COLORGOURAUDMONO |
1369 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_COLORPHONGMONO | D3DPSHADECAPS_COLORPHONGRGB |
1370 D3DPSHADECAPS_SPECULARFLATMONO | D3DPSHADECAPS_SPECULARFLATRGB | D3DPSHADECAPS_SPECULARGOURAUDMONO |
1371 D3DPSHADECAPS_SPECULARGOURAUDRGB | D3DPSHADECAPS_SPECULARPHONGMONO | D3DPSHADECAPS_SPECULARPHONGRGB |
1372 D3DPSHADECAPS_ALPHAFLATBLEND | D3DPSHADECAPS_ALPHAFLATSTIPPLED | D3DPSHADECAPS_ALPHAGOURAUDBLEND |
1373 D3DPSHADECAPS_ALPHAGOURAUDSTIPPLED | D3DPSHADECAPS_ALPHAPHONGBLEND | D3DPSHADECAPS_ALPHAPHONGSTIPPLED |
1374 D3DPSHADECAPS_FOGFLAT | D3DPSHADECAPS_FOGGOURAUD | D3DPSHADECAPS_FOGPHONG);
1376 caps->dpcLineCaps.dwTextureCaps &= (
1377 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
1378 D3DPTEXTURECAPS_TRANSPARENCY | D3DPTEXTURECAPS_BORDER | D3DPTEXTURECAPS_SQUAREONLY |
1379 D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE | D3DPTEXTURECAPS_ALPHAPALETTE| D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
1380 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_COLORKEYBLEND);
1382 caps->dpcLineCaps.dwTextureFilterCaps &= (
1383 D3DPTFILTERCAPS_NEAREST | D3DPTFILTERCAPS_LINEAR | D3DPTFILTERCAPS_MIPNEAREST |
1384 D3DPTFILTERCAPS_MIPLINEAR | D3DPTFILTERCAPS_LINEARMIPNEAREST | D3DPTFILTERCAPS_LINEARMIPLINEAR |
1385 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
1386 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
1387 D3DPTFILTERCAPS_MAGFLINEAR | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC |
1388 D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC);
1390 caps->dpcLineCaps.dwTextureAddressCaps &= (
1391 D3DPTADDRESSCAPS_WRAP | D3DPTADDRESSCAPS_MIRROR | D3DPTADDRESSCAPS_CLAMP |
1392 D3DPTADDRESSCAPS_BORDER | D3DPTADDRESSCAPS_INDEPENDENTUV);
1394 if (!(caps->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2))
1396 /* DirectX7 always has the np2 flag set, no matter what the card
1397 * supports. Some old games (Rollcage) check the caps incorrectly.
1398 * If wined3d supports nonpow2 textures it also has np2 conditional
1399 * support. */
1400 caps->dpcLineCaps.dwTextureCaps |= D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL;
1403 /* Fill the missing members, and do some fixup */
1404 caps->dpcLineCaps.dwSize = sizeof(caps->dpcLineCaps);
1405 caps->dpcLineCaps.dwTextureBlendCaps = D3DPTBLENDCAPS_ADD
1406 | D3DPTBLENDCAPS_MODULATEMASK
1407 | D3DPTBLENDCAPS_COPY
1408 | D3DPTBLENDCAPS_DECAL
1409 | D3DPTBLENDCAPS_DECALALPHA
1410 | D3DPTBLENDCAPS_DECALMASK
1411 | D3DPTBLENDCAPS_MODULATE
1412 | D3DPTBLENDCAPS_MODULATEALPHA;
1413 caps->dpcLineCaps.dwStippleWidth = 32;
1414 caps->dpcLineCaps.dwStippleHeight = 32;
1415 /* Use the same for the TriCaps */
1416 caps->dpcTriCaps = caps->dpcLineCaps;
1418 caps->dwDeviceRenderBitDepth = DDBD_16 | DDBD_24 | DDBD_32;
1419 caps->dwDeviceZBufferBitDepth = DDBD_16 | DDBD_24;
1420 caps->dwMinTextureWidth = 1;
1421 caps->dwMinTextureHeight = 1;
1423 /* Convert DWORDs safely to WORDs */
1424 caps->wMaxTextureBlendStages = min(wined3d_caps.MaxTextureBlendStages, 0xffff);
1425 caps->wMaxSimultaneousTextures = min(wined3d_caps.MaxSimultaneousTextures, 0xffff);
1426 caps->wMaxUserClipPlanes = min(wined3d_caps.MaxUserClipPlanes, D3DMAXUSERCLIPPLANES);
1427 caps->wMaxVertexBlendMatrices = min(wined3d_caps.MaxVertexBlendMatrices, 0xffff);
1429 caps->deviceGUID = IID_IDirect3DTnLHalDevice;
1431 caps->dwReserved1 = 0;
1432 caps->dwReserved2 = 0;
1433 caps->dwReserved3 = 0;
1434 caps->dwReserved4 = 0;
1436 return DD_OK;
1439 /*****************************************************************************
1440 * IDirectDraw7::GetCaps
1442 * Returns the drives capabilities
1444 * Used for version 1, 2, 4 and 7
1446 * Params:
1447 * DriverCaps: Structure to write the Hardware accelerated caps to
1448 * HelCaps: Structure to write the emulation caps to
1450 * Returns
1451 * This implementation returns DD_OK only
1453 *****************************************************************************/
1454 static HRESULT WINAPI ddraw7_GetCaps(IDirectDraw7 *iface, DDCAPS *DriverCaps, DDCAPS *HELCaps)
1456 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1457 DDCAPS caps;
1458 WINED3DCAPS winecaps;
1459 HRESULT hr;
1460 DDSCAPS2 ddscaps = {0, 0, 0, {0}};
1462 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, DriverCaps, HELCaps);
1464 /* One structure must be != NULL */
1465 if (!DriverCaps && !HELCaps)
1467 WARN("Invalid parameters.\n");
1468 return DDERR_INVALIDPARAMS;
1471 memset(&caps, 0, sizeof(caps));
1472 memset(&winecaps, 0, sizeof(winecaps));
1473 caps.dwSize = sizeof(caps);
1475 wined3d_mutex_lock();
1476 hr = wined3d_device_get_device_caps(ddraw->wined3d_device, &winecaps);
1477 if (FAILED(hr))
1479 WARN("Failed to get device caps, %#x.\n", hr);
1480 wined3d_mutex_unlock();
1481 return hr;
1484 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
1485 if (FAILED(hr))
1487 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
1488 wined3d_mutex_unlock();
1489 return hr;
1492 hr = IDirectDraw7_GetFourCCCodes(iface, &caps.dwNumFourCCCodes, NULL);
1493 wined3d_mutex_unlock();
1494 if (FAILED(hr))
1496 WARN("IDirectDraw7::GetFourCCCodes failed\n");
1497 return hr;
1500 caps.dwCaps = winecaps.ddraw_caps.caps;
1501 caps.dwCaps2 = winecaps.ddraw_caps.caps2;
1502 caps.dwCKeyCaps = winecaps.ddraw_caps.color_key_caps;
1503 caps.dwFXCaps = winecaps.ddraw_caps.fx_caps;
1504 caps.dwPalCaps = DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE;
1505 caps.ddsCaps.dwCaps = winecaps.ddraw_caps.dds_caps;
1506 caps.dwSVBCaps = winecaps.ddraw_caps.svb_caps;
1507 caps.dwSVBCKeyCaps = winecaps.ddraw_caps.svb_color_key_caps;
1508 caps.dwSVBFXCaps = winecaps.ddraw_caps.svb_fx_caps;
1509 caps.dwVSBCaps = winecaps.ddraw_caps.vsb_caps;
1510 caps.dwVSBCKeyCaps = winecaps.ddraw_caps.vsb_color_key_caps;
1511 caps.dwVSBFXCaps = winecaps.ddraw_caps.vsb_fx_caps;
1512 caps.dwSSBCaps = winecaps.ddraw_caps.ssb_caps;
1513 caps.dwSSBCKeyCaps = winecaps.ddraw_caps.ssb_color_key_caps;
1514 caps.dwSSBFXCaps = winecaps.ddraw_caps.ssb_fx_caps;
1516 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
1517 caps.dwAlignStrideAlign = DDRAW_STRIDE_ALIGNMENT;
1519 if(DriverCaps)
1521 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
1522 if (TRACE_ON(ddraw))
1524 TRACE("Driver Caps :\n");
1525 DDRAW_dump_DDCAPS(DriverCaps);
1529 if(HELCaps)
1531 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
1532 if (TRACE_ON(ddraw))
1534 TRACE("HEL Caps :\n");
1535 DDRAW_dump_DDCAPS(HELCaps);
1539 return DD_OK;
1542 static HRESULT WINAPI ddraw4_GetCaps(IDirectDraw4 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1544 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1546 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1548 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1551 static HRESULT WINAPI ddraw2_GetCaps(IDirectDraw2 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1553 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1555 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1557 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1560 static HRESULT WINAPI ddraw1_GetCaps(IDirectDraw *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1562 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1564 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1566 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1569 /*****************************************************************************
1570 * IDirectDraw7::Compact
1572 * No idea what it does, MSDN says it's not implemented.
1574 * Returns
1575 * DD_OK, but this is unchecked
1577 *****************************************************************************/
1578 static HRESULT WINAPI ddraw7_Compact(IDirectDraw7 *iface)
1580 TRACE("iface %p.\n", iface);
1582 return DD_OK;
1585 static HRESULT WINAPI ddraw4_Compact(IDirectDraw4 *iface)
1587 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1589 TRACE("iface %p.\n", iface);
1591 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1594 static HRESULT WINAPI ddraw2_Compact(IDirectDraw2 *iface)
1596 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1598 TRACE("iface %p.\n", iface);
1600 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1603 static HRESULT WINAPI ddraw1_Compact(IDirectDraw *iface)
1605 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1607 TRACE("iface %p.\n", iface);
1609 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1612 /*****************************************************************************
1613 * IDirectDraw7::GetDisplayMode
1615 * Returns information about the current display mode
1617 * Exists in versions 1, 2, 4 and 7
1619 * Params:
1620 * DDSD: Address of a surface description structure to write the info to
1622 * Returns
1623 * DD_OK
1625 *****************************************************************************/
1626 static HRESULT WINAPI ddraw7_GetDisplayMode(IDirectDraw7 *iface, DDSURFACEDESC2 *DDSD)
1628 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1629 struct wined3d_display_mode mode;
1630 HRESULT hr;
1632 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
1634 /* This seems sane */
1635 if (!DDSD || (DDSD->dwSize != sizeof(DDSURFACEDESC) && DDSD->dwSize != sizeof(DDSURFACEDESC2)))
1636 return DDERR_INVALIDPARAMS;
1638 wined3d_mutex_lock();
1640 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
1642 ERR("Failed to get display mode, hr %#x.\n", hr);
1643 wined3d_mutex_unlock();
1644 return hr;
1647 memset(DDSD, 0, DDSD->dwSize);
1648 DDSD->dwSize = sizeof(*DDSD);
1649 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
1650 DDSD->dwWidth = mode.width;
1651 DDSD->dwHeight = mode.height;
1652 DDSD->u2.dwRefreshRate = 60;
1653 DDSD->ddsCaps.dwCaps = 0;
1654 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
1655 ddrawformat_from_wined3dformat(&DDSD->u4.ddpfPixelFormat, mode.format_id);
1656 DDSD->u1.lPitch = mode.width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
1658 if(TRACE_ON(ddraw))
1660 TRACE("Returning surface desc :\n");
1661 DDRAW_dump_surface_desc(DDSD);
1664 wined3d_mutex_unlock();
1666 return DD_OK;
1669 static HRESULT WINAPI ddraw4_GetDisplayMode(IDirectDraw4 *iface, DDSURFACEDESC2 *surface_desc)
1671 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1673 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1675 return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, surface_desc);
1678 static HRESULT WINAPI ddraw2_GetDisplayMode(IDirectDraw2 *iface, DDSURFACEDESC *surface_desc)
1680 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1681 HRESULT hr;
1683 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1685 hr = ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1686 if (SUCCEEDED(hr)) surface_desc->dwSize = sizeof(*surface_desc);
1687 return hr;
1690 static HRESULT WINAPI ddraw1_GetDisplayMode(IDirectDraw *iface, DDSURFACEDESC *surface_desc)
1692 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1693 HRESULT hr;
1695 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1697 hr = ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1698 if (SUCCEEDED(hr)) surface_desc->dwSize = sizeof(*surface_desc);
1699 return hr;
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 < ARRAY_SIZE(formats); ++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 if (!(enum_modes = heap_alloc(enum_mode_array_size * sizeof(*enum_modes))))
2394 return DDERR_OUTOFMEMORY;
2396 wined3d_mutex_lock();
2398 pixelformat.dwSize = sizeof(pixelformat);
2399 for(fmt = 0; fmt < ARRAY_SIZE(checkFormatList); 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 heap_free(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 if (!(new_enum_modes = heap_realloc(enum_modes, enum_mode_array_size * sizeof(*new_enum_modes))))
2470 heap_free(enum_modes);
2471 wined3d_mutex_unlock();
2472 return DDERR_OUTOFMEMORY;
2475 enum_modes = new_enum_modes;
2477 enum_modes[enum_mode_count++] = mode;
2481 TRACE("End of enumeration\n");
2482 heap_free(enum_modes);
2483 wined3d_mutex_unlock();
2485 return DD_OK;
2488 static HRESULT WINAPI ddraw4_EnumDisplayModes(IDirectDraw4 *iface, DWORD flags,
2489 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMMODESCALLBACK2 callback)
2491 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2493 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2494 iface, flags, surface_desc, context, callback);
2496 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags, surface_desc, context, callback);
2499 static HRESULT WINAPI ddraw2_EnumDisplayModes(IDirectDraw2 *iface, DWORD flags,
2500 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2502 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2503 struct displaymodescallback_context cbcontext;
2504 DDSURFACEDESC2 surface_desc2;
2506 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2507 iface, flags, surface_desc, context, callback);
2509 cbcontext.func = callback;
2510 cbcontext.context = context;
2512 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2513 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags,
2514 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2517 static HRESULT WINAPI ddraw1_EnumDisplayModes(IDirectDraw *iface, DWORD flags,
2518 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2520 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2521 struct displaymodescallback_context cbcontext;
2522 DDSURFACEDESC2 surface_desc2;
2524 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2525 iface, flags, surface_desc, context, callback);
2527 cbcontext.func = callback;
2528 cbcontext.context = context;
2530 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2531 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags,
2532 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2535 /*****************************************************************************
2536 * IDirectDraw7::EvaluateMode
2538 * Used with IDirectDraw7::StartModeTest to test video modes.
2539 * EvaluateMode is used to pass or fail a mode, and continue with the next
2540 * mode
2542 * Params:
2543 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
2544 * Timeout: Returns the amount of seconds left before the mode would have
2545 * been failed automatically
2547 * Returns:
2548 * This implementation always DD_OK, because it's a stub
2550 *****************************************************************************/
2551 static HRESULT WINAPI ddraw7_EvaluateMode(IDirectDraw7 *iface, DWORD Flags, DWORD *Timeout)
2553 FIXME("iface %p, flags %#x, timeout %p stub!\n", iface, Flags, Timeout);
2555 /* When implementing this, implement it in WineD3D */
2557 return DD_OK;
2560 /*****************************************************************************
2561 * IDirectDraw7::GetDeviceIdentifier
2563 * Returns the device identifier, which gives information about the driver
2564 * Our device identifier is defined at the beginning of this file.
2566 * Params:
2567 * DDDI: Address for the returned structure
2568 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
2570 * Returns:
2571 * On success it returns DD_OK
2572 * DDERR_INVALIDPARAMS if DDDI is NULL
2574 *****************************************************************************/
2575 static HRESULT WINAPI ddraw7_GetDeviceIdentifier(IDirectDraw7 *iface,
2576 DDDEVICEIDENTIFIER2 *DDDI, DWORD Flags)
2578 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2579 struct wined3d_adapter_identifier adapter_id;
2580 HRESULT hr = S_OK;
2582 TRACE("iface %p, device_identifier %p, flags %#x.\n", iface, DDDI, Flags);
2584 if (!DDDI)
2585 return DDERR_INVALIDPARAMS;
2587 if (Flags & DDGDI_GETHOSTIDENTIFIER)
2589 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
2590 * host adapter, if there's a secondary 3D adapter. This doesn't apply
2591 * to any modern hardware, nor is it interesting for Wine, so ignore it.
2592 * Size of DDDEVICEIDENTIFIER2 may be aligned to 8 bytes and thus 4
2593 * bytes too long. So only copy the relevant part of the structure
2596 memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
2597 return DD_OK;
2600 /* Drakan: Order of the Flame expects accurate D3D device information from ddraw */
2601 adapter_id.driver = DDDI->szDriver;
2602 adapter_id.driver_size = sizeof(DDDI->szDriver);
2603 adapter_id.description = DDDI->szDescription;
2604 adapter_id.description_size = sizeof(DDDI->szDescription);
2605 adapter_id.device_name_size = 0;
2606 wined3d_mutex_lock();
2607 hr = wined3d_get_adapter_identifier(ddraw->wined3d, WINED3DADAPTER_DEFAULT, 0x0, &adapter_id);
2608 wined3d_mutex_unlock();
2609 if (FAILED(hr)) return hr;
2611 DDDI->liDriverVersion = adapter_id.driver_version;
2612 DDDI->dwVendorId = adapter_id.vendor_id;
2613 DDDI->dwDeviceId = adapter_id.device_id;
2614 DDDI->dwSubSysId = adapter_id.subsystem_id;
2615 DDDI->dwRevision = adapter_id.revision;
2616 DDDI->guidDeviceIdentifier = adapter_id.device_identifier;
2617 DDDI->dwWHQLLevel = adapter_id.whql_level;
2618 return DD_OK;
2621 static HRESULT WINAPI ddraw4_GetDeviceIdentifier(IDirectDraw4 *iface,
2622 DDDEVICEIDENTIFIER *identifier, DWORD flags)
2624 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2625 DDDEVICEIDENTIFIER2 identifier2;
2626 HRESULT hr;
2628 TRACE("iface %p, identifier %p, flags %#x.\n", iface, identifier, flags);
2630 hr = ddraw7_GetDeviceIdentifier(&ddraw->IDirectDraw7_iface, &identifier2, flags);
2631 DDRAW_Convert_DDDEVICEIDENTIFIER_2_To_1(&identifier2, identifier);
2633 return hr;
2636 /*****************************************************************************
2637 * IDirectDraw7::GetSurfaceFromDC
2639 * Returns the Surface for a GDI device context handle.
2640 * Is this related to IDirectDrawSurface::GetDC ???
2642 * Params:
2643 * hdc: hdc to return the surface for
2644 * Surface: Address to write the surface pointer to
2646 * Returns:
2647 * Always returns DD_OK because it's a stub
2649 *****************************************************************************/
2650 static HRESULT WINAPI ddraw7_GetSurfaceFromDC(IDirectDraw7 *iface,
2651 HDC dc, IDirectDrawSurface7 **surface)
2653 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2654 struct ddraw_surface *surface_impl;
2656 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2658 if (!surface)
2659 return E_INVALIDARG;
2661 if (!dc)
2662 goto done;
2664 wined3d_mutex_lock();
2665 LIST_FOR_EACH_ENTRY(surface_impl, &ddraw->surface_list, struct ddraw_surface, surface_list_entry)
2667 if (surface_impl->dc != dc)
2668 continue;
2670 TRACE("Found surface %p for dc %p.\n", surface_impl, dc);
2671 *surface = &surface_impl->IDirectDrawSurface7_iface;
2672 IDirectDrawSurface7_AddRef(*surface);
2673 wined3d_mutex_unlock();
2674 return DD_OK;
2676 wined3d_mutex_unlock();
2678 done:
2679 TRACE("No surface found for dc %p.\n", dc);
2680 *surface = NULL;
2681 return DDERR_NOTFOUND;
2684 static HRESULT WINAPI ddraw4_GetSurfaceFromDC(IDirectDraw4 *iface, HDC dc,
2685 IDirectDrawSurface4 **surface)
2687 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2688 struct ddraw_surface *surface_impl;
2689 IDirectDrawSurface7 *surface7;
2690 HRESULT hr;
2692 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2694 if (!surface) return E_INVALIDARG;
2696 hr = ddraw7_GetSurfaceFromDC(&ddraw->IDirectDraw7_iface, dc, &surface7);
2697 if (FAILED(hr))
2699 *surface = NULL;
2700 return hr;
2702 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2703 /* Tests say this is true */
2704 *surface = (IDirectDrawSurface4 *)&surface_impl->IDirectDrawSurface_iface;
2705 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
2706 IDirectDrawSurface7_Release(surface7);
2708 return hr;
2711 static HRESULT CALLBACK restore_callback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *desc, void *context)
2713 IDirectDrawSurface_Restore(surface);
2714 IDirectDrawSurface_Release(surface);
2716 return DDENUMRET_OK;
2719 static HRESULT WINAPI ddraw7_RestoreAllSurfaces(IDirectDraw7 *iface)
2721 TRACE("iface %p.\n", iface);
2723 return IDirectDraw7_EnumSurfaces(iface, DDENUMSURFACES_ALL | DDENUMSURFACES_DOESEXIST,
2724 NULL, NULL, restore_callback);
2727 static HRESULT WINAPI ddraw4_RestoreAllSurfaces(IDirectDraw4 *iface)
2729 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2731 TRACE("iface %p.\n", iface);
2733 return ddraw7_RestoreAllSurfaces(&ddraw->IDirectDraw7_iface);
2736 /*****************************************************************************
2737 * IDirectDraw7::StartModeTest
2739 * Tests the specified video modes to update the system registry with
2740 * refresh rate information. StartModeTest starts the mode test,
2741 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
2742 * isn't called within 15 seconds, the mode is failed automatically
2744 * As refresh rates are handled by the X server, I don't think this
2745 * Method is important
2747 * Params:
2748 * Modes: An array of mode specifications
2749 * NumModes: The number of modes in Modes
2750 * Flags: Some flags...
2752 * Returns:
2753 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
2754 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
2755 * otherwise DD_OK
2757 *****************************************************************************/
2758 static HRESULT WINAPI ddraw7_StartModeTest(IDirectDraw7 *iface, SIZE *Modes, DWORD NumModes, DWORD Flags)
2760 FIXME("iface %p, modes %p, mode_count %u, flags %#x partial stub!\n",
2761 iface, Modes, NumModes, Flags);
2763 /* This looks sane */
2764 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
2766 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
2767 * As it is not, DDERR_TESTFINISHED is returned
2768 * (hopefully that's correct
2770 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
2771 * well, that value doesn't (yet) exist in the wine headers, so ignore it
2774 return DD_OK;
2777 static HRESULT WINAPI ddraw7_CreateSurface(IDirectDraw7 *iface, DDSURFACEDESC2 *surface_desc,
2778 IDirectDrawSurface7 **surface, IUnknown *outer_unknown)
2780 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2781 struct ddraw_surface *impl;
2782 HRESULT hr;
2784 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2785 iface, surface_desc, surface, outer_unknown);
2787 wined3d_mutex_lock();
2789 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2791 WARN("Cooperative level not set.\n");
2792 wined3d_mutex_unlock();
2793 return DDERR_NOCOOPERATIVELEVELSET;
2796 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
2798 WARN("Application supplied invalid surface descriptor\n");
2799 wined3d_mutex_unlock();
2800 return DDERR_INVALIDPARAMS;
2803 __TRY
2805 *surface = NULL;
2807 __EXCEPT_PAGE_FAULT
2809 WARN("Surface pointer %p is invalid.\n", surface);
2810 wined3d_mutex_unlock();
2811 return DDERR_INVALIDPARAMS;
2813 __ENDTRY;
2815 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
2817 if (TRACE_ON(ddraw))
2819 TRACE(" (%p) Requesting surface desc :\n", iface);
2820 DDRAW_dump_surface_desc(surface_desc);
2823 WARN("Application tried to create an explicit front or back buffer\n");
2824 wined3d_mutex_unlock();
2825 return DDERR_INVALIDCAPS;
2828 hr = ddraw_surface_create(ddraw, surface_desc, &impl, outer_unknown, 7);
2829 wined3d_mutex_unlock();
2830 if (FAILED(hr))
2831 return hr;
2833 *surface = &impl->IDirectDrawSurface7_iface;
2834 IDirectDraw7_AddRef(iface);
2835 impl->ifaceToRelease = (IUnknown *)iface;
2837 return hr;
2840 static HRESULT WINAPI ddraw4_CreateSurface(IDirectDraw4 *iface,
2841 DDSURFACEDESC2 *surface_desc, IDirectDrawSurface4 **surface, IUnknown *outer_unknown)
2843 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2844 struct ddraw_surface *impl;
2845 HRESULT hr;
2847 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2848 iface, surface_desc, surface, outer_unknown);
2850 wined3d_mutex_lock();
2852 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2854 WARN("Cooperative level not set.\n");
2855 wined3d_mutex_unlock();
2856 return DDERR_NOCOOPERATIVELEVELSET;
2859 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
2861 WARN("Application supplied invalid surface descriptor\n");
2862 wined3d_mutex_unlock();
2863 return DDERR_INVALIDPARAMS;
2866 __TRY
2868 *surface = NULL;
2870 __EXCEPT_PAGE_FAULT
2872 WARN("Surface pointer %p is invalid.\n", surface);
2873 wined3d_mutex_unlock();
2874 return DDERR_INVALIDPARAMS;
2876 __ENDTRY;
2878 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
2880 if (TRACE_ON(ddraw))
2882 TRACE(" (%p) Requesting surface desc :\n", iface);
2883 DDRAW_dump_surface_desc(surface_desc);
2886 WARN("Application tried to create an explicit front or back buffer\n");
2887 wined3d_mutex_unlock();
2888 return DDERR_INVALIDCAPS;
2891 hr = ddraw_surface_create(ddraw, surface_desc, &impl, outer_unknown, 4);
2892 wined3d_mutex_unlock();
2893 if (FAILED(hr))
2894 return hr;
2896 *surface = &impl->IDirectDrawSurface4_iface;
2897 IDirectDraw4_AddRef(iface);
2898 impl->ifaceToRelease = (IUnknown *)iface;
2900 return hr;
2903 static HRESULT WINAPI ddraw2_CreateSurface(IDirectDraw2 *iface,
2904 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
2906 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2907 struct ddraw_surface *impl;
2908 HRESULT hr;
2909 DDSURFACEDESC2 surface_desc2;
2911 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2912 iface, surface_desc, surface, outer_unknown);
2914 wined3d_mutex_lock();
2916 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2918 WARN("Cooperative level not set.\n");
2919 wined3d_mutex_unlock();
2920 return DDERR_NOCOOPERATIVELEVELSET;
2923 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
2925 WARN("Application supplied invalid surface descriptor\n");
2926 wined3d_mutex_unlock();
2927 return DDERR_INVALIDPARAMS;
2930 __TRY
2932 *surface = NULL;
2934 __EXCEPT_PAGE_FAULT
2936 WARN("Surface pointer %p is invalid.\n", surface);
2937 wined3d_mutex_unlock();
2938 return DDERR_INVALIDPARAMS;
2940 __ENDTRY;
2942 DDSD_to_DDSD2(surface_desc, &surface_desc2);
2943 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
2945 if (TRACE_ON(ddraw))
2947 TRACE(" (%p) Requesting surface desc :\n", iface);
2948 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
2951 WARN("Application tried to create an explicit front or back buffer\n");
2952 wined3d_mutex_unlock();
2953 return DDERR_INVALIDCAPS;
2956 hr = ddraw_surface_create(ddraw, &surface_desc2, &impl, outer_unknown, 2);
2957 wined3d_mutex_unlock();
2958 if (FAILED(hr))
2959 return hr;
2961 *surface = &impl->IDirectDrawSurface_iface;
2962 impl->ifaceToRelease = NULL;
2964 return hr;
2967 static HRESULT WINAPI ddraw1_CreateSurface(IDirectDraw *iface,
2968 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
2970 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2971 struct ddraw_surface *impl;
2972 HRESULT hr;
2973 DDSURFACEDESC2 surface_desc2;
2975 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2976 iface, surface_desc, surface, outer_unknown);
2978 wined3d_mutex_lock();
2980 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2982 WARN("Cooperative level not set.\n");
2983 wined3d_mutex_unlock();
2984 return DDERR_NOCOOPERATIVELEVELSET;
2987 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
2989 WARN("Application supplied invalid surface descriptor\n");
2990 wined3d_mutex_unlock();
2991 return DDERR_INVALIDPARAMS;
2994 __TRY
2996 *surface = NULL;
2998 __EXCEPT_PAGE_FAULT
3000 WARN("Surface pointer %p is invalid.\n", surface);
3001 wined3d_mutex_unlock();
3002 return DDERR_INVALIDPARAMS;
3004 __ENDTRY;
3006 if ((surface_desc->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER))
3007 == (DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER)
3008 || (surface_desc->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER))
3009 == ((DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER)))
3011 WARN("Application tried to create an explicit front or back buffer.\n");
3012 wined3d_mutex_unlock();
3013 return DDERR_INVALIDCAPS;
3016 DDSD_to_DDSD2(surface_desc, &surface_desc2);
3017 hr = ddraw_surface_create(ddraw, &surface_desc2, &impl, outer_unknown, 1);
3018 wined3d_mutex_unlock();
3019 if (FAILED(hr))
3020 return hr;
3022 *surface = &impl->IDirectDrawSurface_iface;
3023 impl->ifaceToRelease = NULL;
3025 return hr;
3028 static BOOL
3029 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
3030 const DDPIXELFORMAT *provided)
3032 /* Some flags must be present in both or neither for a match. */
3033 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
3034 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
3035 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
3037 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3038 return FALSE;
3040 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
3041 return FALSE;
3043 if (requested->dwFlags & DDPF_FOURCC)
3044 if (requested->dwFourCC != provided->dwFourCC)
3045 return FALSE;
3047 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
3048 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3049 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
3050 return FALSE;
3052 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3053 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3054 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
3055 return FALSE;
3057 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
3058 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
3059 return FALSE;
3061 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
3062 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3063 |DDPF_BUMPDUDV))
3064 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
3065 return FALSE;
3067 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
3068 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
3069 return FALSE;
3071 return TRUE;
3074 static BOOL ddraw_match_surface_desc(const DDSURFACEDESC2 *requested, const DDSURFACEDESC2 *provided)
3076 struct compare_info
3078 DWORD flag;
3079 ptrdiff_t offset;
3080 size_t size;
3083 #define CMP(FLAG, FIELD) \
3084 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
3085 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
3087 static const struct compare_info compare[] =
3089 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
3090 CMP(BACKBUFFERCOUNT, u5.dwBackBufferCount),
3091 CMP(CAPS, ddsCaps),
3092 CMP(CKDESTBLT, ddckCKDestBlt),
3093 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
3094 CMP(CKSRCBLT, ddckCKSrcBlt),
3095 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
3096 CMP(HEIGHT, dwHeight),
3097 CMP(LINEARSIZE, u1 /* dwLinearSize */),
3098 CMP(LPSURFACE, lpSurface),
3099 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
3100 CMP(PITCH, u1 /* lPitch */),
3101 /* PIXELFORMAT: manual */
3102 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
3103 CMP(TEXTURESTAGE, dwTextureStage),
3104 CMP(WIDTH, dwWidth),
3105 /* ZBUFFERBITDEPTH: "obsolete" */
3108 #undef CMP
3110 unsigned int i;
3112 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3113 return FALSE;
3115 for (i=0; i < ARRAY_SIZE(compare); i++)
3117 if (requested->dwFlags & compare[i].flag
3118 && memcmp((const char *)provided + compare[i].offset,
3119 (const char *)requested + compare[i].offset,
3120 compare[i].size) != 0)
3121 return FALSE;
3124 if (requested->dwFlags & DDSD_PIXELFORMAT)
3126 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
3127 &provided->u4.ddpfPixelFormat))
3128 return FALSE;
3131 return TRUE;
3134 struct surfacescallback2_context
3136 LPDDENUMSURFACESCALLBACK2 func;
3137 void *context;
3140 struct surfacescallback_context
3142 LPDDENUMSURFACESCALLBACK func;
3143 void *context;
3146 static HRESULT CALLBACK EnumSurfacesCallback2Thunk(IDirectDrawSurface7 *surface,
3147 DDSURFACEDESC2 *surface_desc, void *context)
3149 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3150 struct surfacescallback2_context *cbcontext = context;
3152 IDirectDrawSurface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
3153 IDirectDrawSurface7_Release(surface);
3155 return cbcontext->func(&surface_impl->IDirectDrawSurface4_iface,
3156 surface_desc, cbcontext->context);
3159 static HRESULT CALLBACK EnumSurfacesCallbackThunk(IDirectDrawSurface7 *surface,
3160 DDSURFACEDESC2 *surface_desc, void *context)
3162 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3163 struct surfacescallback_context *cbcontext = context;
3165 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
3166 IDirectDrawSurface7_Release(surface);
3168 return cbcontext->func(&surface_impl->IDirectDrawSurface_iface,
3169 (DDSURFACEDESC *)surface_desc, cbcontext->context);
3172 /*****************************************************************************
3173 * IDirectDraw7::EnumSurfaces
3175 * Loops through all surfaces attached to this device and calls the
3176 * application callback. This can't be relayed to WineD3DDevice,
3177 * because some WineD3DSurfaces' parents are IParent objects
3179 * Params:
3180 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
3181 * DDSD: Description to filter for
3182 * Context: Application-provided pointer, it's passed unmodified to the
3183 * Callback function
3184 * Callback: Address to call for each surface
3186 * Returns:
3187 * DDERR_INVALIDPARAMS if the callback is NULL
3188 * DD_OK on success
3190 *****************************************************************************/
3191 static HRESULT WINAPI ddraw7_EnumSurfaces(IDirectDraw7 *iface, DWORD Flags,
3192 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMSURFACESCALLBACK7 Callback)
3194 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
3195 struct ddraw_surface *surf;
3196 BOOL all, nomatch;
3197 DDSURFACEDESC2 desc;
3198 struct list *entry, *entry2;
3200 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3201 iface, Flags, DDSD, Context, Callback);
3203 all = Flags & DDENUMSURFACES_ALL;
3204 nomatch = Flags & DDENUMSURFACES_NOMATCH;
3206 if (!Callback)
3207 return DDERR_INVALIDPARAMS;
3209 wined3d_mutex_lock();
3211 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
3212 LIST_FOR_EACH_SAFE(entry, entry2, &ddraw->surface_list)
3214 surf = LIST_ENTRY(entry, struct ddraw_surface, surface_list_entry);
3216 if (!surf->iface_count)
3218 WARN("Not enumerating surface %p because it doesn't have any references.\n", surf);
3219 continue;
3222 if (all || (nomatch != ddraw_match_surface_desc(DDSD, &surf->surface_desc)))
3224 TRACE("Enumerating surface %p.\n", surf);
3225 desc = surf->surface_desc;
3226 IDirectDrawSurface7_AddRef(&surf->IDirectDrawSurface7_iface);
3227 if (Callback(&surf->IDirectDrawSurface7_iface, &desc, Context) != DDENUMRET_OK)
3229 wined3d_mutex_unlock();
3230 return DD_OK;
3235 wined3d_mutex_unlock();
3237 return DD_OK;
3240 static HRESULT WINAPI ddraw4_EnumSurfaces(IDirectDraw4 *iface, DWORD flags,
3241 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMSURFACESCALLBACK2 callback)
3243 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3244 struct surfacescallback2_context cbcontext;
3246 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3247 iface, flags, surface_desc, context, callback);
3249 cbcontext.func = callback;
3250 cbcontext.context = context;
3252 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags, surface_desc,
3253 &cbcontext, EnumSurfacesCallback2Thunk);
3256 static HRESULT WINAPI ddraw2_EnumSurfaces(IDirectDraw2 *iface, DWORD flags,
3257 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3259 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3260 struct surfacescallback_context cbcontext;
3261 DDSURFACEDESC2 surface_desc2;
3263 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3264 iface, flags, surface_desc, context, callback);
3266 cbcontext.func = callback;
3267 cbcontext.context = context;
3269 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3270 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags,
3271 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3274 static HRESULT WINAPI ddraw1_EnumSurfaces(IDirectDraw *iface, DWORD flags,
3275 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3277 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3278 struct surfacescallback_context cbcontext;
3279 DDSURFACEDESC2 surface_desc2;
3281 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3282 iface, flags, surface_desc, context, callback);
3284 cbcontext.func = callback;
3285 cbcontext.context = context;
3287 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3288 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags,
3289 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3292 /*****************************************************************************
3293 * DirectDrawCreateClipper (DDRAW.@)
3295 * Creates a new IDirectDrawClipper object.
3297 * Params:
3298 * Clipper: Address to write the interface pointer to
3299 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3300 * NULL
3302 * Returns:
3303 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3304 * E_OUTOFMEMORY if allocating the object failed
3306 *****************************************************************************/
3307 HRESULT WINAPI DirectDrawCreateClipper(DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3309 struct ddraw_clipper *object;
3310 HRESULT hr;
3312 TRACE("flags %#x, clipper %p, outer_unknown %p.\n",
3313 flags, clipper, outer_unknown);
3315 if (outer_unknown)
3316 return CLASS_E_NOAGGREGATION;
3318 wined3d_mutex_lock();
3320 if (!(object = heap_alloc_zero(sizeof(*object))))
3322 wined3d_mutex_unlock();
3323 return E_OUTOFMEMORY;
3326 hr = ddraw_clipper_init(object);
3327 if (FAILED(hr))
3329 WARN("Failed to initialize clipper, hr %#x.\n", hr);
3330 heap_free(object);
3331 wined3d_mutex_unlock();
3332 return hr;
3335 TRACE("Created clipper %p.\n", object);
3336 *clipper = &object->IDirectDrawClipper_iface;
3337 wined3d_mutex_unlock();
3339 return DD_OK;
3342 /*****************************************************************************
3343 * IDirectDraw7::CreateClipper
3345 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3347 *****************************************************************************/
3348 static HRESULT WINAPI ddraw7_CreateClipper(IDirectDraw7 *iface, DWORD Flags,
3349 IDirectDrawClipper **Clipper, IUnknown *UnkOuter)
3351 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3352 iface, Flags, Clipper, UnkOuter);
3354 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3357 static HRESULT WINAPI ddraw4_CreateClipper(IDirectDraw4 *iface, DWORD flags,
3358 IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3360 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3362 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3363 iface, flags, clipper, outer_unknown);
3365 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3368 static HRESULT WINAPI ddraw2_CreateClipper(IDirectDraw2 *iface,
3369 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3371 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3373 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3374 iface, flags, clipper, outer_unknown);
3376 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3379 static HRESULT WINAPI ddraw1_CreateClipper(IDirectDraw *iface,
3380 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3382 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3384 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3385 iface, flags, clipper, outer_unknown);
3387 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3390 /*****************************************************************************
3391 * IDirectDraw7::CreatePalette
3393 * Creates a new IDirectDrawPalette object
3395 * Params:
3396 * Flags: The flags for the new clipper
3397 * ColorTable: Color table to assign to the new clipper
3398 * Palette: Address to write the interface pointer to
3399 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3400 * NULL
3402 * Returns:
3403 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3404 * E_OUTOFMEMORY if allocating the object failed
3406 *****************************************************************************/
3407 static HRESULT WINAPI ddraw7_CreatePalette(IDirectDraw7 *iface, DWORD Flags,
3408 PALETTEENTRY *ColorTable, IDirectDrawPalette **Palette, IUnknown *pUnkOuter)
3410 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
3411 struct ddraw_palette *object;
3412 HRESULT hr;
3414 TRACE("iface %p, flags %#x, color_table %p, palette %p, outer_unknown %p.\n",
3415 iface, Flags, ColorTable, Palette, pUnkOuter);
3417 if (pUnkOuter)
3418 return CLASS_E_NOAGGREGATION;
3420 wined3d_mutex_lock();
3422 /* The refcount test shows that a cooplevel is required for this */
3423 if (!ddraw->cooperative_level)
3425 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3426 wined3d_mutex_unlock();
3427 return DDERR_NOCOOPERATIVELEVELSET;
3430 if (!(object = heap_alloc(sizeof(*object))))
3432 ERR("Out of memory when allocating memory for a palette implementation\n");
3433 wined3d_mutex_unlock();
3434 return E_OUTOFMEMORY;
3437 hr = ddraw_palette_init(object, ddraw, Flags, ColorTable);
3438 if (FAILED(hr))
3440 WARN("Failed to initialize palette, hr %#x.\n", hr);
3441 heap_free(object);
3442 wined3d_mutex_unlock();
3443 return hr;
3446 TRACE("Created palette %p.\n", object);
3447 *Palette = &object->IDirectDrawPalette_iface;
3448 wined3d_mutex_unlock();
3450 return DD_OK;
3453 static HRESULT WINAPI ddraw4_CreatePalette(IDirectDraw4 *iface, DWORD flags, PALETTEENTRY *entries,
3454 IDirectDrawPalette **palette, IUnknown *outer_unknown)
3456 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3457 HRESULT hr;
3459 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3460 iface, flags, entries, palette, outer_unknown);
3462 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3463 if (SUCCEEDED(hr) && *palette)
3465 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3466 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3467 IDirectDraw4_AddRef(iface);
3468 impl->ifaceToRelease = (IUnknown *)iface;
3470 return hr;
3473 static HRESULT WINAPI ddraw2_CreatePalette(IDirectDraw2 *iface, DWORD flags,
3474 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3476 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3477 HRESULT hr;
3479 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3480 iface, flags, entries, palette, outer_unknown);
3482 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3483 if (SUCCEEDED(hr) && *palette)
3485 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3486 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3487 impl->ifaceToRelease = NULL;
3490 return hr;
3493 static HRESULT WINAPI ddraw1_CreatePalette(IDirectDraw *iface, DWORD flags,
3494 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3496 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3497 HRESULT hr;
3499 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3500 iface, flags, entries, palette, outer_unknown);
3502 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3503 if (SUCCEEDED(hr) && *palette)
3505 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3506 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3507 impl->ifaceToRelease = NULL;
3510 return hr;
3513 /*****************************************************************************
3514 * IDirectDraw7::DuplicateSurface
3516 * Duplicates a surface. The surface memory points to the same memory as
3517 * the original surface, and it's released when the last surface referencing
3518 * it is released. I guess that's beyond Wine's surface management right now
3519 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3520 * test application to implement this)
3522 * Params:
3523 * Src: Address of the source surface
3524 * Dest: Address to write the new surface pointer to
3526 * Returns:
3527 * See IDirectDraw7::CreateSurface
3529 *****************************************************************************/
3530 static HRESULT WINAPI ddraw7_DuplicateSurface(IDirectDraw7 *iface,
3531 IDirectDrawSurface7 *Src, IDirectDrawSurface7 **Dest)
3533 struct ddraw_surface *src_surface = unsafe_impl_from_IDirectDrawSurface7(Src);
3535 FIXME("iface %p, src %p, dst %p partial stub!\n", iface, Src, Dest);
3537 /* For now, simply create a new, independent surface */
3538 return IDirectDraw7_CreateSurface(iface, &src_surface->surface_desc, Dest, NULL);
3541 static HRESULT WINAPI ddraw4_DuplicateSurface(IDirectDraw4 *iface, IDirectDrawSurface4 *src,
3542 IDirectDrawSurface4 **dst)
3544 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src);
3545 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3546 struct ddraw_surface *dst_impl;
3547 IDirectDrawSurface7 *dst7;
3548 HRESULT hr;
3550 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3552 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3553 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3554 if (FAILED(hr))
3556 *dst = NULL;
3557 return hr;
3559 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3560 *dst = &dst_impl->IDirectDrawSurface4_iface;
3561 IDirectDrawSurface4_AddRef(*dst);
3562 IDirectDrawSurface7_Release(dst7);
3564 return hr;
3567 static HRESULT WINAPI ddraw2_DuplicateSurface(IDirectDraw2 *iface,
3568 IDirectDrawSurface *src, IDirectDrawSurface **dst)
3570 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3571 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3572 struct ddraw_surface *dst_impl;
3573 IDirectDrawSurface7 *dst7;
3574 HRESULT hr;
3576 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3578 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3579 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3580 if (FAILED(hr))
3581 return hr;
3582 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3583 *dst = &dst_impl->IDirectDrawSurface_iface;
3584 IDirectDrawSurface_AddRef(*dst);
3585 IDirectDrawSurface7_Release(dst7);
3587 return hr;
3590 static HRESULT WINAPI ddraw1_DuplicateSurface(IDirectDraw *iface, IDirectDrawSurface *src,
3591 IDirectDrawSurface **dst)
3593 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3594 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3595 struct ddraw_surface *dst_impl;
3596 IDirectDrawSurface7 *dst7;
3597 HRESULT hr;
3599 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3601 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3602 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3603 if (FAILED(hr))
3604 return hr;
3605 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3606 *dst = &dst_impl->IDirectDrawSurface_iface;
3607 IDirectDrawSurface_AddRef(*dst);
3608 IDirectDrawSurface7_Release(dst7);
3610 return hr;
3613 /*****************************************************************************
3614 * IDirect3D7::EnumDevices
3616 * The EnumDevices method for IDirect3D7. It enumerates all supported
3617 * D3D7 devices. Currently the T&L, HAL and RGB devices are enumerated.
3619 * Params:
3620 * callback: Function to call for each enumerated device
3621 * context: Pointer to pass back to the app
3623 * Returns:
3624 * D3D_OK, or the return value of the GetCaps call
3626 *****************************************************************************/
3627 static HRESULT WINAPI d3d7_EnumDevices(IDirect3D7 *iface, LPD3DENUMDEVICESCALLBACK7 callback, void *context)
3629 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
3630 D3DDEVICEDESC7 device_desc7;
3631 HRESULT hr;
3632 size_t i;
3634 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3636 if (!callback)
3637 return DDERR_INVALIDPARAMS;
3639 wined3d_mutex_lock();
3641 if (FAILED(hr = ddraw_get_d3dcaps(ddraw, &device_desc7)))
3643 wined3d_mutex_unlock();
3644 return hr;
3647 for (i = 0; i < ARRAY_SIZE(device_list7); i++)
3649 HRESULT ret;
3651 device_desc7.deviceGUID = *device_list7[i].device_guid;
3652 ret = callback(device_list7[i].interface_name, device_list7[i].device_name, &device_desc7, context);
3653 if (ret != DDENUMRET_OK)
3655 TRACE("Application cancelled the enumeration.\n");
3656 wined3d_mutex_unlock();
3657 return D3D_OK;
3661 TRACE("End of enumeration.\n");
3663 wined3d_mutex_unlock();
3665 return D3D_OK;
3668 /*****************************************************************************
3669 * IDirect3D3::EnumDevices
3671 * Enumerates all supported Direct3DDevice interfaces. This is the
3672 * implementation for Direct3D 1 to Direc3D 3, Version 7 has its own.
3674 * Versions 1, 2 and 3
3676 * Params:
3677 * callback: Application-provided routine to call for each enumerated device
3678 * Context: Pointer to pass to the callback
3680 * Returns:
3681 * D3D_OK on success,
3682 * The result of IDirect3DImpl_GetCaps if it failed
3684 *****************************************************************************/
3685 static HRESULT WINAPI d3d3_EnumDevices(IDirect3D3 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3687 static CHAR wined3d_description[] = "Wine D3DDevice using WineD3D and OpenGL";
3689 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3690 D3DDEVICEDESC device_desc1, hal_desc, hel_desc;
3691 D3DDEVICEDESC7 device_desc7;
3692 HRESULT hr;
3694 /* Some games (Motoracer 2 demo) have the bad idea to modify the device
3695 * name string. Let's put the string in a sufficiently sized array in
3696 * writable memory. */
3697 char device_name[50];
3698 strcpy(device_name,"Direct3D HEL");
3700 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3702 if (!callback)
3703 return DDERR_INVALIDPARAMS;
3705 wined3d_mutex_lock();
3707 if (FAILED(hr = ddraw_get_d3dcaps(ddraw, &device_desc7)))
3709 wined3d_mutex_unlock();
3710 return hr;
3712 ddraw_d3dcaps1_from_7(&device_desc1, &device_desc7);
3714 /* Do I have to enumerate the reference id? Note from old d3d7:
3715 * "It seems that enumerating the reference IID on Direct3D 1 games
3716 * (AvP / Motoracer2) breaks them". So do not enumerate this iid in V1
3718 * There's a registry key HKLM\Software\Microsoft\Direct3D\Drivers,
3719 * EnumReference which enables / disables enumerating the reference
3720 * rasterizer. It's a DWORD, 0 means disabled, 2 means enabled. The
3721 * enablerefrast.reg and disablerefrast.reg files in the DirectX 7.0 sdk
3722 * demo directory suggest this.
3724 * Some games(GTA 2) seem to use the second enumerated device, so I have
3725 * to enumerate at least 2 devices. So enumerate the reference device to
3726 * have 2 devices.
3728 * Other games (Rollcage) tell emulation and hal device apart by certain
3729 * flags. Rollcage expects D3DPTEXTURECAPS_POW2 to be set (yeah, it is a
3730 * limitation flag), and it refuses all devices that have the perspective
3731 * flag set. This way it refuses the emulation device, and HAL devices
3732 * never have POW2 unset in d3d7 on windows. */
3733 if (ddraw->d3dversion != 1)
3735 static CHAR reference_description[] = "RGB Direct3D emulation";
3737 TRACE("Enumerating WineD3D D3DDevice interface.\n");
3738 hal_desc = device_desc1;
3739 hel_desc = device_desc1;
3740 /* The rgb device has the pow2 flag set in the hel caps, but not in the hal caps. */
3741 hal_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3742 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3743 hal_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3744 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3745 /* RGB, RAMP and MMX devices have a HAL dcmColorModel of 0 */
3746 hal_desc.dcmColorModel = 0;
3747 /* RGB, RAMP and MMX devices cannot report HAL hardware flags */
3748 hal_desc.dwFlags = 0;
3750 hr = callback((GUID *)&IID_IDirect3DRGBDevice, reference_description,
3751 device_name, &hal_desc, &hel_desc, context);
3752 if (hr != D3DENUMRET_OK)
3754 TRACE("Application cancelled the enumeration.\n");
3755 wined3d_mutex_unlock();
3756 return D3D_OK;
3760 strcpy(device_name,"Direct3D HAL");
3762 TRACE("Enumerating HAL Direct3D device.\n");
3763 hal_desc = device_desc1;
3764 hel_desc = device_desc1;
3766 /* The hal device does not have the pow2 flag set in hel, but in hal. */
3767 hel_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3768 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3769 hel_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3770 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3771 /* HAL devices have a HEL dcmColorModel of 0 */
3772 hel_desc.dcmColorModel = 0;
3774 hr = callback((GUID *)&IID_IDirect3DHALDevice, wined3d_description,
3775 device_name, &hal_desc, &hel_desc, context);
3776 if (hr != D3DENUMRET_OK)
3778 TRACE("Application cancelled the enumeration.\n");
3779 wined3d_mutex_unlock();
3780 return D3D_OK;
3783 TRACE("End of enumeration.\n");
3785 wined3d_mutex_unlock();
3787 return D3D_OK;
3790 static HRESULT WINAPI d3d2_EnumDevices(IDirect3D2 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3792 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3794 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3796 return d3d3_EnumDevices(&ddraw->IDirect3D3_iface, callback, context);
3799 static HRESULT WINAPI d3d1_EnumDevices(IDirect3D *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3801 struct ddraw *ddraw = impl_from_IDirect3D(iface);
3803 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3805 return d3d3_EnumDevices(&ddraw->IDirect3D3_iface, callback, context);
3808 /*****************************************************************************
3809 * IDirect3D3::CreateLight
3811 * Creates an IDirect3DLight interface. This interface is used in
3812 * Direct3D3 or earlier for lighting. In Direct3D7 it has been replaced
3813 * by the DIRECT3DLIGHT7 structure. Wine's Direct3DLight implementation
3814 * uses the IDirect3DDevice7 interface with D3D7 lights.
3816 * Versions 1, 2 and 3
3818 * Params:
3819 * light: Address to store the new interface pointer
3820 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
3821 * Must be NULL
3823 * Returns:
3824 * D3D_OK on success
3825 * DDERR_OUTOFMEMORY if memory allocation failed
3826 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
3828 *****************************************************************************/
3829 static HRESULT WINAPI d3d3_CreateLight(IDirect3D3 *iface, IDirect3DLight **light,
3830 IUnknown *outer_unknown)
3832 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3833 struct d3d_light *object;
3835 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3837 if (outer_unknown)
3838 return CLASS_E_NOAGGREGATION;
3840 if (!(object = heap_alloc_zero(sizeof(*object))))
3842 ERR("Failed to allocate light memory.\n");
3843 return DDERR_OUTOFMEMORY;
3846 d3d_light_init(object, ddraw);
3848 TRACE("Created light %p.\n", object);
3849 *light = &object->IDirect3DLight_iface;
3851 return D3D_OK;
3854 static HRESULT WINAPI d3d2_CreateLight(IDirect3D2 *iface, IDirect3DLight **light, IUnknown *outer_unknown)
3856 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3858 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3860 return d3d3_CreateLight(&ddraw->IDirect3D3_iface, light, outer_unknown);
3863 static HRESULT WINAPI d3d1_CreateLight(IDirect3D *iface, IDirect3DLight **light, IUnknown *outer_unknown)
3865 struct ddraw *ddraw = impl_from_IDirect3D(iface);
3867 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3869 return d3d3_CreateLight(&ddraw->IDirect3D3_iface, light, outer_unknown);
3872 /*****************************************************************************
3873 * IDirect3D3::CreateMaterial
3875 * Creates an IDirect3DMaterial interface. This interface is used by Direct3D3
3876 * and older versions. The IDirect3DMaterial implementation wraps its
3877 * functionality to IDirect3DDevice7::SetMaterial and friends.
3879 * Versions 1, 2 and 3
3881 * Params:
3882 * material: Address to store the new interface's pointer to
3883 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
3884 * Must be NULL
3886 * Returns:
3887 * D3D_OK on success
3888 * DDERR_OUTOFMEMORY if memory allocation failed
3889 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
3891 *****************************************************************************/
3892 static HRESULT WINAPI d3d3_CreateMaterial(IDirect3D3 *iface, IDirect3DMaterial3 **material,
3893 IUnknown *outer_unknown)
3895 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3896 struct d3d_material *object;
3898 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
3900 if (outer_unknown) return CLASS_E_NOAGGREGATION;
3902 object = d3d_material_create(ddraw);
3903 if (!object)
3905 ERR("Failed to allocate material memory.\n");
3906 return DDERR_OUTOFMEMORY;
3909 TRACE("Created material %p.\n", object);
3910 *material = &object->IDirect3DMaterial3_iface;
3912 return D3D_OK;
3915 static HRESULT WINAPI d3d2_CreateMaterial(IDirect3D2 *iface, IDirect3DMaterial2 **material,
3916 IUnknown *outer_unknown)
3918 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3919 struct d3d_material *object;
3921 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
3923 object = d3d_material_create(ddraw);
3924 if (!object)
3926 ERR("Failed to allocate material memory.\n");
3927 return DDERR_OUTOFMEMORY;
3930 TRACE("Created material %p.\n", object);
3931 *material = &object->IDirect3DMaterial2_iface;
3933 return D3D_OK;
3936 static HRESULT WINAPI d3d1_CreateMaterial(IDirect3D *iface, IDirect3DMaterial **material,
3937 IUnknown *outer_unknown)
3939 struct ddraw *ddraw = impl_from_IDirect3D(iface);
3940 struct d3d_material *object;
3942 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
3944 object = d3d_material_create(ddraw);
3945 if (!object)
3947 ERR("Failed to allocate material memory.\n");
3948 return DDERR_OUTOFMEMORY;
3951 TRACE("Created material %p.\n", object);
3952 *material = &object->IDirect3DMaterial_iface;
3954 return D3D_OK;
3957 /*****************************************************************************
3958 * IDirect3D3::CreateViewport
3960 * Creates an IDirect3DViewport interface. This interface is used
3961 * by Direct3D and earlier versions for Viewport management. In Direct3D7
3962 * it has been replaced by a viewport structure and
3963 * IDirect3DDevice7::*Viewport. Wine's IDirect3DViewport implementation
3964 * uses the IDirect3DDevice7 methods for its functionality
3966 * Params:
3967 * Viewport: Address to store the new interface pointer
3968 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
3969 * Must be NULL
3971 * Returns:
3972 * D3D_OK on success
3973 * DDERR_OUTOFMEMORY if memory allocation failed
3974 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
3976 *****************************************************************************/
3977 static HRESULT WINAPI d3d3_CreateViewport(IDirect3D3 *iface, IDirect3DViewport3 **viewport,
3978 IUnknown *outer_unknown)
3980 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3981 struct d3d_viewport *object;
3983 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
3985 if (outer_unknown) return CLASS_E_NOAGGREGATION;
3987 if (!(object = heap_alloc_zero(sizeof(*object))))
3989 ERR("Failed to allocate viewport memory.\n");
3990 return DDERR_OUTOFMEMORY;
3993 d3d_viewport_init(object, ddraw);
3995 TRACE("Created viewport %p.\n", object);
3996 *viewport = &object->IDirect3DViewport3_iface;
3998 return D3D_OK;
4001 static HRESULT WINAPI d3d2_CreateViewport(IDirect3D2 *iface, IDirect3DViewport2 **viewport, IUnknown *outer_unknown)
4003 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4005 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4007 return d3d3_CreateViewport(&ddraw->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4008 outer_unknown);
4011 static HRESULT WINAPI d3d1_CreateViewport(IDirect3D *iface, IDirect3DViewport **viewport, IUnknown *outer_unknown)
4013 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4015 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4017 return d3d3_CreateViewport(&ddraw->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4018 outer_unknown);
4021 /*****************************************************************************
4022 * IDirect3D3::FindDevice
4024 * This method finds a device with the requested properties and returns a
4025 * device description
4027 * Versions 1, 2 and 3
4028 * Params:
4029 * fds: Describes the requested device characteristics
4030 * fdr: Returns the device description
4032 * Returns:
4033 * D3D_OK on success
4034 * DDERR_INVALIDPARAMS if no device was found
4036 *****************************************************************************/
4037 static HRESULT WINAPI d3d3_FindDevice(IDirect3D3 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4039 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4040 D3DDEVICEDESC7 desc7;
4041 D3DDEVICEDESC desc1;
4042 HRESULT hr;
4044 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4046 if (!fds || !fdr) return DDERR_INVALIDPARAMS;
4048 if (fds->dwSize != sizeof(D3DFINDDEVICESEARCH)
4049 || fdr->dwSize != sizeof(D3DFINDDEVICERESULT))
4050 return DDERR_INVALIDPARAMS;
4052 if ((fds->dwFlags & D3DFDS_COLORMODEL)
4053 && fds->dcmColorModel != D3DCOLOR_RGB)
4055 WARN("Trying to request a non-RGB D3D color model. Not supported.\n");
4056 return DDERR_INVALIDPARAMS; /* No real idea what to return here :-) */
4059 if (fds->dwFlags & D3DFDS_GUID)
4061 TRACE("Trying to match guid %s.\n", debugstr_guid(&(fds->guid)));
4062 if (!IsEqualGUID(&IID_D3DDEVICE_WineD3D, &fds->guid)
4063 && !IsEqualGUID(&IID_IDirect3DHALDevice, &fds->guid)
4064 && !IsEqualGUID(&IID_IDirect3DRGBDevice, &fds->guid))
4066 WARN("No match for this GUID.\n");
4067 return DDERR_NOTFOUND;
4071 /* Get the caps */
4072 if (FAILED(hr = ddraw_get_d3dcaps(ddraw, &desc7)))
4073 return hr;
4075 /* Now return our own GUID */
4076 ddraw_d3dcaps1_from_7(&desc1, &desc7);
4077 fdr->guid = IID_D3DDEVICE_WineD3D;
4078 fdr->ddHwDesc = desc1;
4079 fdr->ddSwDesc = desc1;
4081 TRACE("Returning Wine's wined3d device with (undumped) capabilities.\n");
4083 return D3D_OK;
4086 static HRESULT WINAPI d3d2_FindDevice(IDirect3D2 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4088 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4090 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4092 return d3d3_FindDevice(&ddraw->IDirect3D3_iface, fds, fdr);
4095 static HRESULT WINAPI d3d1_FindDevice(IDirect3D *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4097 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4099 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4101 return d3d3_FindDevice(&ddraw->IDirect3D3_iface, fds, fdr);
4104 /*****************************************************************************
4105 * IDirect3D7::CreateDevice
4107 * Creates an IDirect3DDevice7 interface.
4109 * Versions 2, 3 and 7. IDirect3DDevice 1 interfaces are interfaces to
4110 * DirectDraw surfaces and are created with
4111 * IDirectDrawSurface::QueryInterface. This method uses CreateDevice to
4112 * create the device object and QueryInterfaces for IDirect3DDevice
4114 * Params:
4115 * refiid: IID of the device to create
4116 * Surface: Initial rendertarget
4117 * Device: Address to return the interface pointer
4119 * Returns:
4120 * D3D_OK on success
4121 * DDERR_OUTOFMEMORY if memory allocation failed
4122 * DDERR_INVALIDPARAMS if a device exists already
4124 *****************************************************************************/
4125 static HRESULT WINAPI d3d7_CreateDevice(IDirect3D7 *iface, REFCLSID riid,
4126 IDirectDrawSurface7 *surface, IDirect3DDevice7 **device)
4128 struct ddraw_surface *target = unsafe_impl_from_IDirectDrawSurface7(surface);
4129 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4130 struct d3d_device *object;
4131 HRESULT hr;
4133 TRACE("iface %p, riid %s, surface %p, device %p.\n", iface, debugstr_guid(riid), surface, device);
4135 wined3d_mutex_lock();
4136 if (SUCCEEDED(hr = d3d_device_create(ddraw, target, (IUnknown *)surface, 7, &object, NULL)))
4138 *device = &object->IDirect3DDevice7_iface;
4140 else
4142 WARN("Failed to create device, hr %#x.\n", hr);
4143 *device = NULL;
4145 wined3d_mutex_unlock();
4147 return hr;
4150 static HRESULT WINAPI d3d3_CreateDevice(IDirect3D3 *iface, REFCLSID riid,
4151 IDirectDrawSurface4 *surface, IDirect3DDevice3 **device, IUnknown *outer_unknown)
4153 struct ddraw_surface *surface_impl = unsafe_impl_from_IDirectDrawSurface4(surface);
4154 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4155 struct d3d_device *device_impl;
4156 HRESULT hr;
4158 TRACE("iface %p, riid %s, surface %p, device %p, outer_unknown %p.\n",
4159 iface, debugstr_guid(riid), surface, device, outer_unknown);
4161 if (outer_unknown)
4162 return CLASS_E_NOAGGREGATION;
4164 wined3d_mutex_lock();
4165 if (SUCCEEDED(hr = d3d_device_create(ddraw, surface_impl, (IUnknown *)surface, 3, &device_impl, NULL)))
4167 *device = &device_impl->IDirect3DDevice3_iface;
4169 else
4171 WARN("Failed to create device, hr %#x.\n", hr);
4172 *device = NULL;
4174 wined3d_mutex_unlock();
4176 return hr;
4179 static HRESULT WINAPI d3d2_CreateDevice(IDirect3D2 *iface, REFCLSID riid,
4180 IDirectDrawSurface *surface, IDirect3DDevice2 **device)
4182 struct ddraw_surface *surface_impl = unsafe_impl_from_IDirectDrawSurface(surface);
4183 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4184 struct d3d_device *device_impl;
4185 HRESULT hr;
4187 TRACE("iface %p, riid %s, surface %p, device %p.\n",
4188 iface, debugstr_guid(riid), surface, device);
4190 wined3d_mutex_lock();
4191 if (SUCCEEDED(hr = d3d_device_create(ddraw, surface_impl, (IUnknown *)surface, 2, &device_impl, NULL)))
4193 *device = &device_impl->IDirect3DDevice2_iface;
4195 else
4197 WARN("Failed to create device, hr %#x.\n", hr);
4198 *device = NULL;
4200 wined3d_mutex_unlock();
4202 return hr;
4205 /*****************************************************************************
4206 * IDirect3D7::CreateVertexBuffer
4208 * Creates a new vertex buffer object and returns a IDirect3DVertexBuffer7
4209 * interface.
4211 * Versions 3 and 7
4213 * Params:
4214 * desc: Requested Vertex buffer properties
4215 * vertex_buffer: Address to return the interface pointer at
4216 * flags: Some flags, should be 0
4218 * Returns
4219 * D3D_OK on success
4220 * DDERR_OUTOFMEMORY if memory allocation failed
4221 * DDERR_INVALIDPARAMS if desc or vertex_buffer is NULL
4223 *****************************************************************************/
4224 static HRESULT WINAPI d3d7_CreateVertexBuffer(IDirect3D7 *iface, D3DVERTEXBUFFERDESC *desc,
4225 IDirect3DVertexBuffer7 **vertex_buffer, DWORD flags)
4227 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4228 struct d3d_vertex_buffer *object;
4229 HRESULT hr;
4231 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x.\n",
4232 iface, desc, vertex_buffer, flags);
4234 if (!vertex_buffer || !desc) return DDERR_INVALIDPARAMS;
4236 hr = d3d_vertex_buffer_create(&object, ddraw, desc);
4237 if (hr == D3D_OK)
4239 TRACE("Created vertex buffer %p.\n", object);
4240 *vertex_buffer = &object->IDirect3DVertexBuffer7_iface;
4242 else
4243 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4245 return hr;
4248 static HRESULT WINAPI d3d3_CreateVertexBuffer(IDirect3D3 *iface, D3DVERTEXBUFFERDESC *desc,
4249 IDirect3DVertexBuffer **vertex_buffer, DWORD flags, IUnknown *outer_unknown)
4251 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4252 struct d3d_vertex_buffer *object;
4253 HRESULT hr;
4255 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x, outer_unknown %p.\n",
4256 iface, desc, vertex_buffer, flags, outer_unknown);
4258 if (outer_unknown)
4259 return CLASS_E_NOAGGREGATION;
4260 if (!vertex_buffer || !desc)
4261 return DDERR_INVALIDPARAMS;
4263 hr = d3d_vertex_buffer_create(&object, ddraw, desc);
4264 if (hr == D3D_OK)
4266 TRACE("Created vertex buffer %p.\n", object);
4267 *vertex_buffer = (IDirect3DVertexBuffer *)&object->IDirect3DVertexBuffer7_iface;
4269 else
4270 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4272 return hr;
4275 /*****************************************************************************
4276 * IDirect3D7::EnumZBufferFormats
4278 * Enumerates all supported Z buffer pixel formats
4280 * Versions 3 and 7
4282 * Params:
4283 * device_iid:
4284 * callback: callback to call for each pixel format
4285 * context: Pointer to pass back to the callback
4287 * Returns:
4288 * D3D_OK on success
4289 * DDERR_INVALIDPARAMS if callback is NULL
4291 *****************************************************************************/
4292 static HRESULT WINAPI d3d7_EnumZBufferFormats(IDirect3D7 *iface, REFCLSID device_iid,
4293 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4295 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4296 struct wined3d_display_mode mode;
4297 enum wined3d_device_type type;
4298 unsigned int i;
4299 HRESULT hr;
4301 /* Order matters. Specifically, BattleZone II (full version) expects the
4302 * 16-bit depth formats to be listed before the 24 and 32 ones. */
4303 static const enum wined3d_format_id formats[] =
4305 WINED3DFMT_S1_UINT_D15_UNORM,
4306 WINED3DFMT_D16_UNORM,
4307 WINED3DFMT_X8D24_UNORM,
4308 WINED3DFMT_S4X4_UINT_D24_UNORM,
4309 WINED3DFMT_D24_UNORM_S8_UINT,
4310 WINED3DFMT_D32_UNORM,
4313 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4314 iface, debugstr_guid(device_iid), callback, context);
4316 if (!callback) return DDERR_INVALIDPARAMS;
4318 if (IsEqualGUID(device_iid, &IID_IDirect3DHALDevice)
4319 || IsEqualGUID(device_iid, &IID_IDirect3DTnLHalDevice)
4320 || IsEqualGUID(device_iid, &IID_D3DDEVICE_WineD3D))
4322 TRACE("Asked for HAL device.\n");
4323 type = WINED3D_DEVICE_TYPE_HAL;
4325 else if (IsEqualGUID(device_iid, &IID_IDirect3DRGBDevice)
4326 || IsEqualGUID(device_iid, &IID_IDirect3DMMXDevice))
4328 TRACE("Asked for SW device.\n");
4329 type = WINED3D_DEVICE_TYPE_SW;
4331 else if (IsEqualGUID(device_iid, &IID_IDirect3DRefDevice))
4333 TRACE("Asked for REF device.\n");
4334 type = WINED3D_DEVICE_TYPE_REF;
4336 else if (IsEqualGUID(device_iid, &IID_IDirect3DNullDevice))
4338 TRACE("Asked for NULLREF device.\n");
4339 type = WINED3D_DEVICE_TYPE_NULLREF;
4341 else
4343 FIXME("Unexpected device GUID %s.\n", debugstr_guid(device_iid));
4344 type = WINED3D_DEVICE_TYPE_HAL;
4347 wined3d_mutex_lock();
4348 /* We need an adapter format from somewhere to please wined3d and WGL.
4349 * Use the current display mode. So far all cards offer the same depth
4350 * stencil format for all modes, but if some do not and applications do
4351 * not like that we'll have to find some workaround, like iterating over
4352 * all imaginable formats and collecting all the depth stencil formats we
4353 * can get. */
4354 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
4356 ERR("Failed to get display mode, hr %#x.\n", hr);
4357 wined3d_mutex_unlock();
4358 return hr;
4361 for (i = 0; i < ARRAY_SIZE(formats); ++i)
4363 if (SUCCEEDED(wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, type, mode.format_id,
4364 WINED3DUSAGE_DEPTHSTENCIL, WINED3D_RTYPE_TEXTURE_2D, formats[i])))
4366 DDPIXELFORMAT pformat;
4368 memset(&pformat, 0, sizeof(pformat));
4369 pformat.dwSize = sizeof(pformat);
4370 ddrawformat_from_wined3dformat(&pformat, formats[i]);
4372 TRACE("Enumerating wined3d format %#x.\n", formats[i]);
4373 hr = callback(&pformat, context);
4374 if (hr != DDENUMRET_OK)
4376 TRACE("Format enumeration cancelled by application.\n");
4377 wined3d_mutex_unlock();
4378 return D3D_OK;
4383 /* Historically some windows drivers used dwZBufferBitDepth=24 for WINED3DFMT_X8D24_UNORM,
4384 * while others used dwZBufferBitDepth=32. In either case the pitch matches a 32 bits per
4385 * pixel format, so we use dwZBufferBitDepth=32. Some games expect 24. Windows Vista and
4386 * newer enumerate both versions, so we do the same(bug 22434) */
4387 if (SUCCEEDED(wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, type, mode.format_id,
4388 WINED3DUSAGE_DEPTHSTENCIL, WINED3D_RTYPE_TEXTURE_2D, WINED3DFMT_X8D24_UNORM)))
4390 DDPIXELFORMAT x8d24 =
4392 sizeof(x8d24), DDPF_ZBUFFER, 0,
4393 {24}, {0x00000000}, {0x00ffffff}, {0x00000000}
4395 TRACE("Enumerating WINED3DFMT_X8D24_UNORM, dwZBufferBitDepth=24 version\n");
4396 callback(&x8d24, context);
4399 TRACE("End of enumeration.\n");
4401 wined3d_mutex_unlock();
4403 return D3D_OK;
4406 static HRESULT WINAPI d3d3_EnumZBufferFormats(IDirect3D3 *iface, REFCLSID device_iid,
4407 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4409 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4411 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4412 iface, debugstr_guid(device_iid), callback, context);
4414 return d3d7_EnumZBufferFormats(&ddraw->IDirect3D7_iface, device_iid, callback, context);
4417 /*****************************************************************************
4418 * IDirect3D7::EvictManagedTextures
4420 * Removes all managed textures (=surfaces with DDSCAPS2_TEXTUREMANAGE or
4421 * DDSCAPS2_D3DTEXTUREMANAGE caps) to be removed from video memory.
4423 * Versions 3 and 7
4425 * Returns:
4426 * D3D_OK, because it's a stub
4428 *****************************************************************************/
4429 static HRESULT WINAPI d3d7_EvictManagedTextures(IDirect3D7 *iface)
4431 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4433 TRACE("iface %p!\n", iface);
4435 wined3d_mutex_lock();
4436 if (ddraw->flags & DDRAW_D3D_INITIALIZED)
4437 wined3d_device_evict_managed_resources(ddraw->wined3d_device);
4438 wined3d_mutex_unlock();
4440 return D3D_OK;
4443 static HRESULT WINAPI d3d3_EvictManagedTextures(IDirect3D3 *iface)
4445 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4447 TRACE("iface %p.\n", iface);
4449 return d3d7_EvictManagedTextures(&ddraw->IDirect3D7_iface);
4452 /*****************************************************************************
4453 * IDirectDraw7 VTable
4454 *****************************************************************************/
4455 static const struct IDirectDraw7Vtbl ddraw7_vtbl =
4457 /* IUnknown */
4458 ddraw7_QueryInterface,
4459 ddraw7_AddRef,
4460 ddraw7_Release,
4461 /* IDirectDraw */
4462 ddraw7_Compact,
4463 ddraw7_CreateClipper,
4464 ddraw7_CreatePalette,
4465 ddraw7_CreateSurface,
4466 ddraw7_DuplicateSurface,
4467 ddraw7_EnumDisplayModes,
4468 ddraw7_EnumSurfaces,
4469 ddraw7_FlipToGDISurface,
4470 ddraw7_GetCaps,
4471 ddraw7_GetDisplayMode,
4472 ddraw7_GetFourCCCodes,
4473 ddraw7_GetGDISurface,
4474 ddraw7_GetMonitorFrequency,
4475 ddraw7_GetScanLine,
4476 ddraw7_GetVerticalBlankStatus,
4477 ddraw7_Initialize,
4478 ddraw7_RestoreDisplayMode,
4479 ddraw7_SetCooperativeLevel,
4480 ddraw7_SetDisplayMode,
4481 ddraw7_WaitForVerticalBlank,
4482 /* IDirectDraw2 */
4483 ddraw7_GetAvailableVidMem,
4484 /* IDirectDraw3 */
4485 ddraw7_GetSurfaceFromDC,
4486 /* IDirectDraw4 */
4487 ddraw7_RestoreAllSurfaces,
4488 ddraw7_TestCooperativeLevel,
4489 ddraw7_GetDeviceIdentifier,
4490 /* IDirectDraw7 */
4491 ddraw7_StartModeTest,
4492 ddraw7_EvaluateMode
4495 static const struct IDirectDraw4Vtbl ddraw4_vtbl =
4497 /* IUnknown */
4498 ddraw4_QueryInterface,
4499 ddraw4_AddRef,
4500 ddraw4_Release,
4501 /* IDirectDraw */
4502 ddraw4_Compact,
4503 ddraw4_CreateClipper,
4504 ddraw4_CreatePalette,
4505 ddraw4_CreateSurface,
4506 ddraw4_DuplicateSurface,
4507 ddraw4_EnumDisplayModes,
4508 ddraw4_EnumSurfaces,
4509 ddraw4_FlipToGDISurface,
4510 ddraw4_GetCaps,
4511 ddraw4_GetDisplayMode,
4512 ddraw4_GetFourCCCodes,
4513 ddraw4_GetGDISurface,
4514 ddraw4_GetMonitorFrequency,
4515 ddraw4_GetScanLine,
4516 ddraw4_GetVerticalBlankStatus,
4517 ddraw4_Initialize,
4518 ddraw4_RestoreDisplayMode,
4519 ddraw4_SetCooperativeLevel,
4520 ddraw4_SetDisplayMode,
4521 ddraw4_WaitForVerticalBlank,
4522 /* IDirectDraw2 */
4523 ddraw4_GetAvailableVidMem,
4524 /* IDirectDraw3 */
4525 ddraw4_GetSurfaceFromDC,
4526 /* IDirectDraw4 */
4527 ddraw4_RestoreAllSurfaces,
4528 ddraw4_TestCooperativeLevel,
4529 ddraw4_GetDeviceIdentifier,
4532 static const struct IDirectDraw2Vtbl ddraw2_vtbl =
4534 /* IUnknown */
4535 ddraw2_QueryInterface,
4536 ddraw2_AddRef,
4537 ddraw2_Release,
4538 /* IDirectDraw */
4539 ddraw2_Compact,
4540 ddraw2_CreateClipper,
4541 ddraw2_CreatePalette,
4542 ddraw2_CreateSurface,
4543 ddraw2_DuplicateSurface,
4544 ddraw2_EnumDisplayModes,
4545 ddraw2_EnumSurfaces,
4546 ddraw2_FlipToGDISurface,
4547 ddraw2_GetCaps,
4548 ddraw2_GetDisplayMode,
4549 ddraw2_GetFourCCCodes,
4550 ddraw2_GetGDISurface,
4551 ddraw2_GetMonitorFrequency,
4552 ddraw2_GetScanLine,
4553 ddraw2_GetVerticalBlankStatus,
4554 ddraw2_Initialize,
4555 ddraw2_RestoreDisplayMode,
4556 ddraw2_SetCooperativeLevel,
4557 ddraw2_SetDisplayMode,
4558 ddraw2_WaitForVerticalBlank,
4559 /* IDirectDraw2 */
4560 ddraw2_GetAvailableVidMem,
4563 static const struct IDirectDrawVtbl ddraw1_vtbl =
4565 /* IUnknown */
4566 ddraw1_QueryInterface,
4567 ddraw1_AddRef,
4568 ddraw1_Release,
4569 /* IDirectDraw */
4570 ddraw1_Compact,
4571 ddraw1_CreateClipper,
4572 ddraw1_CreatePalette,
4573 ddraw1_CreateSurface,
4574 ddraw1_DuplicateSurface,
4575 ddraw1_EnumDisplayModes,
4576 ddraw1_EnumSurfaces,
4577 ddraw1_FlipToGDISurface,
4578 ddraw1_GetCaps,
4579 ddraw1_GetDisplayMode,
4580 ddraw1_GetFourCCCodes,
4581 ddraw1_GetGDISurface,
4582 ddraw1_GetMonitorFrequency,
4583 ddraw1_GetScanLine,
4584 ddraw1_GetVerticalBlankStatus,
4585 ddraw1_Initialize,
4586 ddraw1_RestoreDisplayMode,
4587 ddraw1_SetCooperativeLevel,
4588 ddraw1_SetDisplayMode,
4589 ddraw1_WaitForVerticalBlank,
4592 static const struct IDirect3D7Vtbl d3d7_vtbl =
4594 /* IUnknown methods */
4595 d3d7_QueryInterface,
4596 d3d7_AddRef,
4597 d3d7_Release,
4598 /* IDirect3D7 methods */
4599 d3d7_EnumDevices,
4600 d3d7_CreateDevice,
4601 d3d7_CreateVertexBuffer,
4602 d3d7_EnumZBufferFormats,
4603 d3d7_EvictManagedTextures
4606 static const struct IDirect3D3Vtbl d3d3_vtbl =
4608 /* IUnknown methods */
4609 d3d3_QueryInterface,
4610 d3d3_AddRef,
4611 d3d3_Release,
4612 /* IDirect3D3 methods */
4613 d3d3_EnumDevices,
4614 d3d3_CreateLight,
4615 d3d3_CreateMaterial,
4616 d3d3_CreateViewport,
4617 d3d3_FindDevice,
4618 d3d3_CreateDevice,
4619 d3d3_CreateVertexBuffer,
4620 d3d3_EnumZBufferFormats,
4621 d3d3_EvictManagedTextures
4624 static const struct IDirect3D2Vtbl d3d2_vtbl =
4626 /* IUnknown methods */
4627 d3d2_QueryInterface,
4628 d3d2_AddRef,
4629 d3d2_Release,
4630 /* IDirect3D2 methods */
4631 d3d2_EnumDevices,
4632 d3d2_CreateLight,
4633 d3d2_CreateMaterial,
4634 d3d2_CreateViewport,
4635 d3d2_FindDevice,
4636 d3d2_CreateDevice
4639 static const struct IDirect3DVtbl d3d1_vtbl =
4641 /* IUnknown methods */
4642 d3d1_QueryInterface,
4643 d3d1_AddRef,
4644 d3d1_Release,
4645 /* IDirect3D methods */
4646 d3d1_Initialize,
4647 d3d1_EnumDevices,
4648 d3d1_CreateLight,
4649 d3d1_CreateMaterial,
4650 d3d1_CreateViewport,
4651 d3d1_FindDevice
4654 /*****************************************************************************
4655 * ddraw_find_decl
4657 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
4658 * if none was found.
4660 * This function is in ddraw.c and the DDraw object space because D3D7
4661 * vertex buffers are created using the IDirect3D interface to the ddraw
4662 * object, so they can be valid across D3D devices(theoretically. The ddraw
4663 * object also owns the wined3d device
4665 * Parameters:
4666 * This: Device
4667 * fvf: Fvf to find the decl for
4669 * Returns:
4670 * NULL in case of an error, the vertex declaration for the FVF otherwise.
4672 *****************************************************************************/
4673 struct wined3d_vertex_declaration *ddraw_find_decl(struct ddraw *This, DWORD fvf)
4675 struct wined3d_vertex_declaration *pDecl = NULL;
4676 HRESULT hr;
4677 int p, low, high; /* deliberately signed */
4678 struct FvfToDecl *convertedDecls = This->decls;
4680 TRACE("Searching for declaration for fvf %08x... ", fvf);
4682 low = 0;
4683 high = This->numConvertedDecls - 1;
4684 while(low <= high) {
4685 p = (low + high) >> 1;
4686 TRACE("%d ", p);
4687 if(convertedDecls[p].fvf == fvf) {
4688 TRACE("found %p\n", convertedDecls[p].decl);
4689 return convertedDecls[p].decl;
4690 } else if(convertedDecls[p].fvf < fvf) {
4691 low = p + 1;
4692 } else {
4693 high = p - 1;
4696 TRACE("not found. Creating and inserting at position %d.\n", low);
4698 hr = wined3d_vertex_declaration_create_from_fvf(This->wined3d_device,
4699 fvf, This, &ddraw_null_wined3d_parent_ops, &pDecl);
4700 if (hr != S_OK) return NULL;
4702 if (This->declArraySize == This->numConvertedDecls)
4704 unsigned int grow = max(This->declArraySize / 2, 8);
4706 if (!(convertedDecls = heap_realloc(convertedDecls,
4707 (This->numConvertedDecls + grow) * sizeof(*convertedDecls))))
4709 wined3d_vertex_declaration_decref(pDecl);
4710 return NULL;
4712 This->decls = convertedDecls;
4713 This->declArraySize += grow;
4716 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
4717 convertedDecls[low].decl = pDecl;
4718 convertedDecls[low].fvf = fvf;
4719 This->numConvertedDecls++;
4721 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
4722 return pDecl;
4725 static inline struct ddraw *ddraw_from_device_parent(struct wined3d_device_parent *device_parent)
4727 return CONTAINING_RECORD(device_parent, struct ddraw, device_parent);
4730 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
4731 struct wined3d_device *device)
4733 TRACE("device_parent %p, device %p.\n", device_parent, device);
4736 /* This is run from device_process_message() in wined3d, we can't take the
4737 * wined3d mutex. */
4738 /* FIXME: We only get mode change notifications in exclusive mode, but we
4739 * should mark surfaces as lost on mode changes in DDSCL_NORMAL mode as well. */
4740 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
4742 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4743 MONITORINFO monitor_info;
4744 HMONITOR monitor;
4745 RECT *r;
4747 TRACE("device_parent %p.\n", device_parent);
4749 if (!(ddraw->cooperative_level & DDSCL_EXCLUSIVE) || !ddraw->swapchain_window)
4751 TRACE("Nothing to resize.\n");
4752 return;
4755 monitor = MonitorFromWindow(ddraw->swapchain_window, MONITOR_DEFAULTTOPRIMARY);
4756 monitor_info.cbSize = sizeof(monitor_info);
4757 if (!GetMonitorInfoW(monitor, &monitor_info))
4759 ERR("Failed to get monitor info.\n");
4760 return;
4763 r = &monitor_info.rcMonitor;
4764 TRACE("Resizing window %p to %s.\n", ddraw->swapchain_window, wine_dbgstr_rect(r));
4766 if (!SetWindowPos(ddraw->swapchain_window, HWND_TOP, r->left, r->top,
4767 r->right - r->left, r->bottom - r->top, SWP_SHOWWINDOW | SWP_NOACTIVATE))
4768 ERR("Failed to resize window.\n");
4770 InterlockedCompareExchange(&ddraw->device_state, DDRAW_DEVICE_STATE_NOT_RESTORED, DDRAW_DEVICE_STATE_OK);
4773 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
4775 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4777 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
4779 if (!activate)
4781 ddraw->device_state = DDRAW_DEVICE_STATE_LOST;
4782 exclusive_window = NULL;
4784 else
4786 InterlockedCompareExchange(&ddraw->device_state, DDRAW_DEVICE_STATE_NOT_RESTORED, DDRAW_DEVICE_STATE_LOST);
4790 void ddraw_update_lost_surfaces(struct ddraw *ddraw)
4792 struct ddraw_surface *surface;
4794 if (ddraw->device_state != DDRAW_DEVICE_STATE_NOT_RESTORED)
4795 return;
4797 LIST_FOR_EACH_ENTRY(surface, &ddraw->surface_list, struct ddraw_surface, surface_list_entry)
4799 surface->is_lost = TRUE;
4801 ddraw->device_state = DDRAW_DEVICE_STATE_OK;
4804 static HRESULT CDECL device_parent_texture_sub_resource_created(struct wined3d_device_parent *device_parent,
4805 enum wined3d_resource_type type, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
4806 void **parent, const struct wined3d_parent_ops **parent_ops)
4808 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4809 struct ddraw_surface *ddraw_surface;
4811 TRACE("device_parent %p, type %#x, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
4812 device_parent, type, wined3d_texture, sub_resource_idx, parent, parent_ops);
4814 /* We have a swapchain or wined3d internal texture. */
4815 if (type != WINED3D_RTYPE_TEXTURE_2D || !wined3d_texture_get_parent(wined3d_texture)
4816 || wined3d_texture_get_parent(wined3d_texture) == ddraw)
4818 *parent = NULL;
4819 *parent_ops = &ddraw_null_wined3d_parent_ops;
4821 return DD_OK;
4824 if (!(ddraw_surface = heap_alloc_zero(sizeof(*ddraw_surface))))
4826 ERR("Failed to allocate surface memory.\n");
4827 return DDERR_OUTOFVIDEOMEMORY;
4830 ddraw_surface_init(ddraw_surface, ddraw, wined3d_texture, sub_resource_idx, parent_ops);
4831 *parent = ddraw_surface;
4833 ddraw_update_lost_surfaces(ddraw);
4834 list_add_head(&ddraw->surface_list, &ddraw_surface->surface_list_entry);
4836 TRACE("Created ddraw surface %p.\n", ddraw_surface);
4838 return DD_OK;
4841 static void STDMETHODCALLTYPE ddraw_frontbuffer_destroyed(void *parent)
4843 struct ddraw *ddraw = parent;
4844 ddraw->wined3d_frontbuffer = NULL;
4847 static const struct wined3d_parent_ops ddraw_frontbuffer_parent_ops =
4849 ddraw_frontbuffer_destroyed,
4852 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
4853 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
4854 struct wined3d_texture **texture)
4856 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4857 const struct wined3d_parent_ops *parent_ops;
4858 HRESULT hr;
4860 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
4861 device_parent, container_parent, desc, texture_flags, texture);
4863 if (!ddraw->wined3d_frontbuffer)
4864 parent_ops = &ddraw_frontbuffer_parent_ops;
4865 else
4866 parent_ops = &ddraw_null_wined3d_parent_ops;
4868 if (FAILED(hr = wined3d_texture_create(ddraw->wined3d_device, desc, 1, 1,
4869 texture_flags | WINED3D_TEXTURE_CREATE_MAPPABLE, NULL, ddraw, parent_ops, texture)))
4871 WARN("Failed to create texture, hr %#x.\n", hr);
4872 return hr;
4875 if (!ddraw->wined3d_frontbuffer)
4876 ddraw->wined3d_frontbuffer = *texture;
4878 return hr;
4881 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
4882 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
4884 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4885 HRESULT hr;
4887 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
4889 if (ddraw->wined3d_swapchain)
4891 ERR("Swapchain already created.\n");
4892 return E_FAIL;
4895 if (FAILED(hr = wined3d_swapchain_create(ddraw->wined3d_device, desc, NULL,
4896 &ddraw_null_wined3d_parent_ops, swapchain)))
4897 WARN("Failed to create swapchain, hr %#x.\n", hr);
4899 return hr;
4902 static const struct wined3d_device_parent_ops ddraw_wined3d_device_parent_ops =
4904 device_parent_wined3d_device_created,
4905 device_parent_mode_changed,
4906 device_parent_activate,
4907 device_parent_texture_sub_resource_created,
4908 device_parent_create_swapchain_texture,
4909 device_parent_create_swapchain,
4912 HRESULT ddraw_init(struct ddraw *ddraw, DWORD flags, enum wined3d_device_type device_type)
4914 WINED3DCAPS caps;
4915 HRESULT hr;
4917 ddraw->IDirectDraw7_iface.lpVtbl = &ddraw7_vtbl;
4918 ddraw->IDirectDraw_iface.lpVtbl = &ddraw1_vtbl;
4919 ddraw->IDirectDraw2_iface.lpVtbl = &ddraw2_vtbl;
4920 ddraw->IDirectDraw4_iface.lpVtbl = &ddraw4_vtbl;
4921 ddraw->IDirect3D_iface.lpVtbl = &d3d1_vtbl;
4922 ddraw->IDirect3D2_iface.lpVtbl = &d3d2_vtbl;
4923 ddraw->IDirect3D3_iface.lpVtbl = &d3d3_vtbl;
4924 ddraw->IDirect3D7_iface.lpVtbl = &d3d7_vtbl;
4925 ddraw->device_parent.ops = &ddraw_wined3d_device_parent_ops;
4926 ddraw->numIfaces = 1;
4927 ddraw->ref7 = 1;
4929 flags |= DDRAW_WINED3D_FLAGS;
4930 if (!(ddraw->wined3d = wined3d_create(flags)))
4932 flags |= WINED3D_NO3D;
4933 if (!(ddraw->wined3d = wined3d_create(flags)))
4935 WARN("Failed to create a wined3d object.\n");
4936 return E_FAIL;
4940 if (FAILED(hr = wined3d_get_device_caps(ddraw->wined3d, WINED3DADAPTER_DEFAULT, device_type, &caps)))
4942 ERR("Failed to get device caps, hr %#x.\n", hr);
4943 wined3d_decref(ddraw->wined3d);
4944 return E_FAIL;
4947 if (!(caps.ddraw_caps.caps & WINEDDCAPS_3D))
4949 WARN("Created a wined3d object without 3D support.\n");
4950 ddraw->flags |= DDRAW_NO3D;
4953 if (FAILED(hr = wined3d_device_create(ddraw->wined3d, WINED3DADAPTER_DEFAULT, device_type,
4954 NULL, 0, DDRAW_STRIDE_ALIGNMENT, &ddraw->device_parent, &ddraw->wined3d_device)))
4956 WARN("Failed to create a wined3d device, hr %#x.\n", hr);
4957 wined3d_decref(ddraw->wined3d);
4958 return hr;
4961 list_init(&ddraw->surface_list);
4963 return DD_OK;