ddraw: Handle the "DirectDrawRenderer" registry key in wined3d.
[wine/multimedia.git] / dlls / ddraw / ddraw.c
blob6836d12920e063c3cc9ab8b55eb63920427cfa2e
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
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include "ddraw_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
30 static struct wined3d_display_mode original_mode;
31 static const struct ddraw *exclusive_ddraw;
32 static BOOL restore_mode;
34 /* Device identifier. Don't relay it to WineD3D */
35 static const DDDEVICEIDENTIFIER2 deviceidentifier =
37 "display",
38 "DirectDraw HAL",
39 { { 0x00010001, 0x00010001 } },
40 0, 0, 0, 0,
41 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
42 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
46 static struct enum_device_entry
48 char interface_name[100];
49 char device_name[100];
50 const GUID *device_guid;
51 } device_list7[] =
53 /* T&L HAL device */
55 "WINE Direct3D7 Hardware Transform and Lighting acceleration using WineD3D",
56 "Wine D3D7 T&L HAL",
57 &IID_IDirect3DTnLHalDevice,
60 /* HAL device */
62 "WINE Direct3D7 Hardware acceleration using WineD3D",
63 "Direct3D HAL",
64 &IID_IDirect3DHALDevice,
67 /* RGB device */
69 "WINE Direct3D7 RGB Software Emulation using WineD3D",
70 "Wine D3D7 RGB",
71 &IID_IDirect3DRGBDevice,
75 static void STDMETHODCALLTYPE ddraw_null_wined3d_object_destroyed(void *parent) {}
77 const struct wined3d_parent_ops ddraw_null_wined3d_parent_ops =
79 ddraw_null_wined3d_object_destroyed,
82 static inline struct ddraw *impl_from_IDirectDraw(IDirectDraw *iface)
84 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw_iface);
87 static inline struct ddraw *impl_from_IDirectDraw2(IDirectDraw2 *iface)
89 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw2_iface);
92 static inline struct ddraw *impl_from_IDirectDraw4(IDirectDraw4 *iface)
94 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw4_iface);
97 static inline struct ddraw *impl_from_IDirectDraw7(IDirectDraw7 *iface)
99 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw7_iface);
102 static inline struct ddraw *impl_from_IDirect3D(IDirect3D *iface)
104 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D_iface);
107 static inline struct ddraw *impl_from_IDirect3D2(IDirect3D2 *iface)
109 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D2_iface);
112 static inline struct ddraw *impl_from_IDirect3D3(IDirect3D3 *iface)
114 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D3_iface);
117 static inline struct ddraw *impl_from_IDirect3D7(IDirect3D7 *iface)
119 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D7_iface);
122 static HRESULT WINAPI ddraw7_QueryInterface(IDirectDraw7 *iface, REFIID riid, void **out)
124 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
126 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
128 if (!riid)
130 *out = NULL;
131 return DDERR_INVALIDPARAMS;
134 /* The refcount unit test revealed that an IDirect3D7 interface can only
135 * be queried from a DirectDraw object that was created as an IDirectDraw7
136 * interface. The older interfaces can query any IDirect3D version except
137 * 7, because they are all initially created as IDirectDraw. This isn't
138 * really crucial behavior, and messy to implement with the common
139 * creation function, so it has been left out here. */
140 if (IsEqualGUID(&IID_IDirectDraw7, riid)
141 || IsEqualGUID(&IID_IUnknown, riid))
143 *out = &ddraw->IDirectDraw7_iface;
144 TRACE("Returning IDirectDraw7 interface %p.\n", *out);
146 else if (IsEqualGUID(&IID_IDirectDraw4, riid))
148 *out = &ddraw->IDirectDraw4_iface;
149 TRACE("Returning IDirectDraw4 interface %p.\n", *out);
151 else if (IsEqualGUID(&IID_IDirectDraw2, riid))
153 *out = &ddraw->IDirectDraw2_iface;
154 TRACE("Returning IDirectDraw2 interface %p.\n", *out);
156 else if (IsEqualGUID(&IID_IDirectDraw, riid))
158 *out = &ddraw->IDirectDraw_iface;
159 TRACE("Returning IDirectDraw interface %p.\n", *out);
161 else if (IsEqualGUID(&IID_IDirect3D7, riid))
163 ddraw->d3dversion = 7;
164 *out = &ddraw->IDirect3D7_iface;
165 TRACE("Returning Direct3D7 interface %p.\n", *out);
167 else if (IsEqualGUID(&IID_IDirect3D3, riid))
169 ddraw->d3dversion = 3;
170 *out = &ddraw->IDirect3D3_iface;
171 TRACE("Returning Direct3D3 interface %p.\n", *out);
173 else if (IsEqualGUID(&IID_IDirect3D2, riid))
175 ddraw->d3dversion = 2;
176 *out = &ddraw->IDirect3D2_iface;
177 TRACE("Returning Direct3D2 interface %p.\n", *out);
179 else if (IsEqualGUID(&IID_IDirect3D, riid))
181 ddraw->d3dversion = 1;
182 *out = &ddraw->IDirect3D_iface;
183 TRACE("Returning Direct3D interface %p.\n", *out);
185 /* Unknown interface */
186 else
188 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
189 *out = NULL;
190 return E_NOINTERFACE;
193 IUnknown_AddRef((IUnknown *)*out);
194 return S_OK;
197 static HRESULT WINAPI ddraw4_QueryInterface(IDirectDraw4 *iface, REFIID riid, void **object)
199 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
201 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
203 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
206 static HRESULT WINAPI ddraw2_QueryInterface(IDirectDraw2 *iface, REFIID riid, void **object)
208 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
210 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
212 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
215 static HRESULT WINAPI ddraw1_QueryInterface(IDirectDraw *iface, REFIID riid, void **object)
217 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
219 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
221 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
224 static HRESULT WINAPI d3d7_QueryInterface(IDirect3D7 *iface, REFIID riid, void **object)
226 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
228 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
230 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
233 static HRESULT WINAPI d3d3_QueryInterface(IDirect3D3 *iface, REFIID riid, void **object)
235 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
237 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
239 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
242 static HRESULT WINAPI d3d2_QueryInterface(IDirect3D2 *iface, REFIID riid, void **object)
244 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
246 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
248 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
251 static HRESULT WINAPI d3d1_QueryInterface(IDirect3D *iface, REFIID riid, void **object)
253 struct ddraw *ddraw = impl_from_IDirect3D(iface);
255 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
257 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
260 /*****************************************************************************
261 * IDirectDraw7::AddRef
263 * Increases the interfaces refcount, basically
265 * DDraw refcounting is a bit tricky. The different DirectDraw interface
266 * versions have individual refcounts, but the IDirect3D interfaces do not.
267 * All interfaces are from one object, that means calling QueryInterface on an
268 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
269 * ddraw object.
271 * That means all AddRef and Release implementations of IDirectDrawX work
272 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
273 * except of IDirect3D7 which thunks to IDirectDraw7
275 * Returns: The new refcount
277 *****************************************************************************/
278 static ULONG WINAPI ddraw7_AddRef(IDirectDraw7 *iface)
280 struct ddraw *This = impl_from_IDirectDraw7(iface);
281 ULONG ref = InterlockedIncrement(&This->ref7);
283 TRACE("%p increasing refcount to %u.\n", This, ref);
285 if(ref == 1) InterlockedIncrement(&This->numIfaces);
287 return ref;
290 static ULONG WINAPI ddraw4_AddRef(IDirectDraw4 *iface)
292 struct ddraw *This = impl_from_IDirectDraw4(iface);
293 ULONG ref = InterlockedIncrement(&This->ref4);
295 TRACE("%p increasing refcount to %u.\n", This, ref);
297 if (ref == 1) InterlockedIncrement(&This->numIfaces);
299 return ref;
302 static ULONG WINAPI ddraw2_AddRef(IDirectDraw2 *iface)
304 struct ddraw *This = impl_from_IDirectDraw2(iface);
305 ULONG ref = InterlockedIncrement(&This->ref2);
307 TRACE("%p increasing refcount to %u.\n", This, ref);
309 if (ref == 1) InterlockedIncrement(&This->numIfaces);
311 return ref;
314 static ULONG WINAPI ddraw1_AddRef(IDirectDraw *iface)
316 struct ddraw *This = impl_from_IDirectDraw(iface);
317 ULONG ref = InterlockedIncrement(&This->ref1);
319 TRACE("%p increasing refcount to %u.\n", This, ref);
321 if (ref == 1) InterlockedIncrement(&This->numIfaces);
323 return ref;
326 static ULONG WINAPI d3d7_AddRef(IDirect3D7 *iface)
328 struct ddraw *This = impl_from_IDirect3D7(iface);
330 TRACE("iface %p.\n", iface);
332 return ddraw7_AddRef(&This->IDirectDraw7_iface);
335 static ULONG WINAPI d3d3_AddRef(IDirect3D3 *iface)
337 struct ddraw *This = impl_from_IDirect3D3(iface);
339 TRACE("iface %p.\n", iface);
341 return ddraw1_AddRef(&This->IDirectDraw_iface);
344 static ULONG WINAPI d3d2_AddRef(IDirect3D2 *iface)
346 struct ddraw *This = impl_from_IDirect3D2(iface);
348 TRACE("iface %p.\n", iface);
350 return ddraw1_AddRef(&This->IDirectDraw_iface);
353 static ULONG WINAPI d3d1_AddRef(IDirect3D *iface)
355 struct ddraw *This = impl_from_IDirect3D(iface);
357 TRACE("iface %p.\n", iface);
359 return ddraw1_AddRef(&This->IDirectDraw_iface);
362 void ddraw_destroy_swapchain(struct ddraw *ddraw)
364 TRACE("Destroying the swapchain.\n");
366 wined3d_swapchain_decref(ddraw->wined3d_swapchain);
367 ddraw->wined3d_swapchain = NULL;
369 if (!(ddraw->flags & DDRAW_NO3D))
371 UINT i;
373 for (i = 0; i < ddraw->numConvertedDecls; ++i)
375 wined3d_vertex_declaration_decref(ddraw->decls[i].decl);
377 HeapFree(GetProcessHeap(), 0, ddraw->decls);
378 ddraw->numConvertedDecls = 0;
380 if (FAILED(wined3d_device_uninit_3d(ddraw->wined3d_device)))
382 ERR("Failed to uninit 3D.\n");
384 else
386 /* Free the d3d window if one was created. */
387 if (ddraw->d3d_window && ddraw->d3d_window != ddraw->dest_window)
389 TRACE("Destroying the hidden render window %p.\n", ddraw->d3d_window);
390 DestroyWindow(ddraw->d3d_window);
391 ddraw->d3d_window = 0;
395 ddraw->flags &= ~DDRAW_D3D_INITIALIZED;
397 else
399 wined3d_device_uninit_gdi(ddraw->wined3d_device);
402 ddraw_set_swapchain_window(ddraw, NULL);
404 TRACE("Swapchain destroyed.\n");
407 /*****************************************************************************
408 * ddraw_destroy
410 * Destroys a ddraw object if all refcounts are 0. This is to share code
411 * between the IDirectDrawX::Release functions
413 * Params:
414 * This: DirectDraw object to destroy
416 *****************************************************************************/
417 static void ddraw_destroy(struct ddraw *This)
419 IDirectDraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, NULL, DDSCL_NORMAL);
420 IDirectDraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
422 /* Destroy the device window if we created one */
423 if(This->devicewindow != 0)
425 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
426 DestroyWindow(This->devicewindow);
427 This->devicewindow = 0;
430 wined3d_mutex_lock();
431 list_remove(&This->ddraw_list_entry);
432 wined3d_mutex_unlock();
434 if (This->wined3d_swapchain)
435 ddraw_destroy_swapchain(This);
436 wined3d_device_decref(This->wined3d_device);
437 wined3d_decref(This->wined3d);
439 /* Now free the object */
440 HeapFree(GetProcessHeap(), 0, This);
443 /*****************************************************************************
444 * IDirectDraw7::Release
446 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
448 * Returns: The new refcount
449 *****************************************************************************/
450 static ULONG WINAPI ddraw7_Release(IDirectDraw7 *iface)
452 struct ddraw *This = impl_from_IDirectDraw7(iface);
453 ULONG ref = InterlockedDecrement(&This->ref7);
455 TRACE("%p decreasing refcount to %u.\n", This, ref);
457 if (!ref && !InterlockedDecrement(&This->numIfaces))
458 ddraw_destroy(This);
460 return ref;
463 static ULONG WINAPI ddraw4_Release(IDirectDraw4 *iface)
465 struct ddraw *This = impl_from_IDirectDraw4(iface);
466 ULONG ref = InterlockedDecrement(&This->ref4);
468 TRACE("%p decreasing refcount to %u.\n", This, ref);
470 if (!ref && !InterlockedDecrement(&This->numIfaces))
471 ddraw_destroy(This);
473 return ref;
476 static ULONG WINAPI ddraw2_Release(IDirectDraw2 *iface)
478 struct ddraw *This = impl_from_IDirectDraw2(iface);
479 ULONG ref = InterlockedDecrement(&This->ref2);
481 TRACE("%p decreasing refcount to %u.\n", This, ref);
483 if (!ref && !InterlockedDecrement(&This->numIfaces))
484 ddraw_destroy(This);
486 return ref;
489 static ULONG WINAPI ddraw1_Release(IDirectDraw *iface)
491 struct ddraw *This = impl_from_IDirectDraw(iface);
492 ULONG ref = InterlockedDecrement(&This->ref1);
494 TRACE("%p decreasing refcount to %u.\n", This, ref);
496 if (!ref && !InterlockedDecrement(&This->numIfaces))
497 ddraw_destroy(This);
499 return ref;
502 static ULONG WINAPI d3d7_Release(IDirect3D7 *iface)
504 struct ddraw *This = impl_from_IDirect3D7(iface);
506 TRACE("iface %p.\n", iface);
508 return ddraw7_Release(&This->IDirectDraw7_iface);
511 static ULONG WINAPI d3d3_Release(IDirect3D3 *iface)
513 struct ddraw *This = impl_from_IDirect3D3(iface);
515 TRACE("iface %p.\n", iface);
517 return ddraw1_Release(&This->IDirectDraw_iface);
520 static ULONG WINAPI d3d2_Release(IDirect3D2 *iface)
522 struct ddraw *This = impl_from_IDirect3D2(iface);
524 TRACE("iface %p.\n", iface);
526 return ddraw1_Release(&This->IDirectDraw_iface);
529 static ULONG WINAPI d3d1_Release(IDirect3D *iface)
531 struct ddraw *This = impl_from_IDirect3D(iface);
533 TRACE("iface %p.\n", iface);
535 return ddraw1_Release(&This->IDirectDraw_iface);
538 /*****************************************************************************
539 * IDirectDraw methods
540 *****************************************************************************/
542 static HRESULT ddraw_set_focus_window(struct ddraw *ddraw, HWND window)
544 /* FIXME: This looks wrong, exclusive mode should imply a destination
545 * window. */
546 if ((ddraw->cooperative_level & DDSCL_EXCLUSIVE) && ddraw->dest_window)
548 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET.\n");
549 return DDERR_HWNDALREADYSET;
552 ddraw->focuswindow = window;
554 return DD_OK;
557 static HRESULT ddraw_attach_d3d_device(struct ddraw *ddraw,
558 struct wined3d_swapchain_desc *swapchain_desc)
560 HWND window = swapchain_desc->device_window;
561 HRESULT hr;
563 TRACE("ddraw %p.\n", ddraw);
565 if (!window || window == GetDesktopWindow())
567 window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "Hidden D3D Window",
568 WS_DISABLED, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
569 NULL, NULL, NULL, NULL);
570 if (!window)
572 ERR("Failed to create window, last error %#x.\n", GetLastError());
573 return E_FAIL;
576 ShowWindow(window, SW_HIDE); /* Just to be sure */
577 WARN("No window for the Direct3DDevice, created hidden window %p.\n", window);
579 swapchain_desc->device_window = window;
581 else
583 TRACE("Using existing window %p for Direct3D rendering.\n", window);
585 ddraw->d3d_window = window;
587 /* Set this NOW, otherwise creating the depth stencil surface will cause a
588 * recursive loop until ram or emulated video memory is full. */
589 ddraw->flags |= DDRAW_D3D_INITIALIZED;
590 hr = wined3d_device_init_3d(ddraw->wined3d_device, swapchain_desc);
591 if (FAILED(hr))
593 ddraw->flags &= ~DDRAW_D3D_INITIALIZED;
594 return hr;
597 ddraw->declArraySize = 2;
598 ddraw->decls = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw->decls) * ddraw->declArraySize);
599 if (!ddraw->decls)
601 ERR("Error allocating an array for the converted vertex decls.\n");
602 ddraw->declArraySize = 0;
603 hr = wined3d_device_uninit_3d(ddraw->wined3d_device);
604 return E_OUTOFMEMORY;
607 TRACE("Successfully initialized 3D.\n");
609 return DD_OK;
612 static HRESULT ddraw_create_swapchain(struct ddraw *ddraw, HWND window, BOOL windowed)
614 struct wined3d_swapchain_desc swapchain_desc;
615 struct wined3d_display_mode mode;
616 HRESULT hr = WINED3D_OK;
618 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
620 ERR("Failed to get display mode.\n");
621 return hr;
624 memset(&swapchain_desc, 0, sizeof(swapchain_desc));
625 swapchain_desc.backbuffer_width = mode.width;
626 swapchain_desc.backbuffer_height = mode.height;
627 swapchain_desc.backbuffer_format = mode.format_id;
628 swapchain_desc.swap_effect = WINED3D_SWAP_EFFECT_COPY;
629 swapchain_desc.device_window = window;
630 swapchain_desc.windowed = windowed;
632 if (!(ddraw->flags & DDRAW_NO3D))
633 hr = ddraw_attach_d3d_device(ddraw, &swapchain_desc);
634 else
635 hr = wined3d_device_init_gdi(ddraw->wined3d_device, &swapchain_desc);
637 if (FAILED(hr))
639 ERR("Failed to create swapchain, hr %#x.\n", hr);
640 return hr;
643 if (!(ddraw->wined3d_swapchain = wined3d_device_get_swapchain(ddraw->wined3d_device, 0)))
645 ERR("Failed to get swapchain.\n");
646 return DDERR_INVALIDPARAMS;
649 wined3d_swapchain_incref(ddraw->wined3d_swapchain);
650 ddraw_set_swapchain_window(ddraw, window);
652 return DD_OK;
655 /*****************************************************************************
656 * IDirectDraw7::SetCooperativeLevel
658 * Sets the cooperative level for the DirectDraw object, and the window
659 * assigned to it. The cooperative level determines the general behavior
660 * of the DirectDraw application
662 * Warning: This is quite tricky, as it's not really documented which
663 * cooperative levels can be combined with each other. If a game fails
664 * after this function, try to check the cooperative levels passed on
665 * Windows, and if it returns something different.
667 * If you think that this function caused the failure because it writes a
668 * fixme, be sure to run again with a +ddraw trace.
670 * What is known about cooperative levels (See the ddraw modes test):
671 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
672 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
673 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
674 * DDSCL_FULLSCREEN can be activated
675 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
677 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
678 * DDSCL_SETFOCUSWINDOW (partially),
679 * DDSCL_MULTITHREADED (work in progress)
681 * Unhandled flags, which should be implemented
682 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
683 * expect any difference to a normal window for wine)
684 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
685 * rendering (Possible test case: Half-Life)
687 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
689 * These don't seem very important for wine:
690 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
692 * Returns:
693 * DD_OK if the cooperative level was set successfully
694 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
695 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
696 * (Probably others too, have to investigate)
698 *****************************************************************************/
699 static HRESULT WINAPI ddraw7_SetCooperativeLevel(IDirectDraw7 *iface, HWND window, DWORD cooplevel)
701 struct ddraw *This = impl_from_IDirectDraw7(iface);
702 struct wined3d_surface *rt = NULL, *ds = NULL;
703 struct wined3d_stateblock *stateblock;
704 BOOL restore_state = FALSE;
705 HRESULT hr;
707 TRACE("iface %p, window %p, flags %#x.\n", iface, window, cooplevel);
708 DDRAW_dump_cooperativelevel(cooplevel);
710 wined3d_mutex_lock();
712 /* Tests suggest that we need one of them: */
713 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
714 DDSCL_NORMAL |
715 DDSCL_EXCLUSIVE )))
717 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
718 wined3d_mutex_unlock();
719 return DDERR_INVALIDPARAMS;
722 if ((cooplevel & DDSCL_CREATEDEVICEWINDOW) && !(cooplevel & DDSCL_EXCLUSIVE))
724 WARN("DDSCL_CREATEDEVICEWINDOW requires DDSCL_EXCLUSIVE.\n");
725 wined3d_mutex_unlock();
726 return DDERR_INVALIDPARAMS;
729 /* Handle those levels first which set various hwnds */
730 if ((cooplevel & DDSCL_SETFOCUSWINDOW) && !(cooplevel & DDSCL_CREATEDEVICEWINDOW))
732 /* This isn't compatible with a lot of flags */
733 if (cooplevel & (DDSCL_MULTITHREADED
734 | DDSCL_FPUSETUP
735 | DDSCL_FPUPRESERVE
736 | DDSCL_ALLOWREBOOT
737 | DDSCL_ALLOWMODEX
738 | DDSCL_SETDEVICEWINDOW
739 | DDSCL_NORMAL
740 | DDSCL_EXCLUSIVE
741 | DDSCL_FULLSCREEN))
743 WARN("Called with incompatible flags, returning DDERR_INVALIDPARAMS.\n");
744 wined3d_mutex_unlock();
745 return DDERR_INVALIDPARAMS;
748 hr = ddraw_set_focus_window(This, window);
749 wined3d_mutex_unlock();
750 return hr;
753 if (cooplevel & DDSCL_EXCLUSIVE)
755 if (!(cooplevel & DDSCL_FULLSCREEN) || !(window || (cooplevel & DDSCL_CREATEDEVICEWINDOW)))
757 WARN("DDSCL_EXCLUSIVE requires DDSCL_FULLSCREEN and a window.\n");
758 wined3d_mutex_unlock();
759 return DDERR_INVALIDPARAMS;
762 if (cooplevel & DDSCL_CREATEDEVICEWINDOW)
764 HWND device_window;
766 if (!This->focuswindow && !(cooplevel & DDSCL_SETFOCUSWINDOW))
768 WARN("No focus window set.\n");
769 wined3d_mutex_unlock();
770 return DDERR_NOFOCUSWINDOW;
773 device_window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "DirectDrawDeviceWnd",
774 WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
775 NULL, NULL, NULL, NULL);
776 if (!device_window)
778 ERR("Failed to create window, last error %#x.\n", GetLastError());
779 wined3d_mutex_unlock();
780 return E_FAIL;
783 ShowWindow(device_window, SW_SHOW);
784 TRACE("Created a device window %p.\n", device_window);
786 /* Native apparently leaks the created device window if setting the
787 * focus window below fails. */
788 This->cooperative_level |= DDSCL_CREATEDEVICEWINDOW;
789 This->devicewindow = device_window;
791 if (cooplevel & DDSCL_SETFOCUSWINDOW)
793 if (!window)
795 wined3d_mutex_unlock();
796 return DDERR_NOHWND;
799 if (FAILED(hr = ddraw_set_focus_window(This, window)))
801 wined3d_mutex_unlock();
802 return hr;
806 window = device_window;
809 else
811 if (This->cooperative_level & DDSCL_CREATEDEVICEWINDOW)
812 DestroyWindow(This->devicewindow);
813 This->devicewindow = NULL;
814 This->focuswindow = NULL;
817 if ((This->cooperative_level & DDSCL_EXCLUSIVE)
818 && (window != This->dest_window || !(cooplevel & DDSCL_EXCLUSIVE)))
819 wined3d_device_release_focus_window(This->wined3d_device);
821 if ((cooplevel & DDSCL_FULLSCREEN) != (This->cooperative_level & DDSCL_FULLSCREEN) || window != This->dest_window)
823 if (This->cooperative_level & DDSCL_FULLSCREEN)
824 wined3d_device_restore_fullscreen_window(This->wined3d_device, This->dest_window);
826 if (cooplevel & DDSCL_FULLSCREEN)
828 struct wined3d_display_mode display_mode;
830 wined3d_get_adapter_display_mode(This->wined3d, WINED3DADAPTER_DEFAULT, &display_mode, NULL);
831 wined3d_device_setup_fullscreen_window(This->wined3d_device, window,
832 display_mode.width, display_mode.height);
836 if ((cooplevel & DDSCL_EXCLUSIVE)
837 && (window != This->dest_window || !(This->cooperative_level & DDSCL_EXCLUSIVE)))
839 hr = wined3d_device_acquire_focus_window(This->wined3d_device, window);
840 if (FAILED(hr))
842 ERR("Failed to acquire focus window, hr %#x.\n", hr);
843 wined3d_mutex_unlock();
844 return hr;
848 if (cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
849 wined3d_device_set_multithreaded(This->wined3d_device);
851 if (This->wined3d_swapchain)
853 if (!(This->flags & DDRAW_NO3D))
855 restore_state = TRUE;
857 if (FAILED(hr = wined3d_stateblock_create(This->wined3d_device, WINED3D_SBT_ALL, &stateblock)))
859 ERR("Failed to create stateblock, hr %#x.\n", hr);
860 wined3d_mutex_unlock();
861 return hr;
864 wined3d_stateblock_capture(stateblock);
865 rt = wined3d_device_get_render_target(This->wined3d_device, 0);
866 if (rt == This->wined3d_frontbuffer)
867 rt = NULL;
868 else if (rt)
869 wined3d_surface_incref(rt);
871 if ((ds = wined3d_device_get_depth_stencil(This->wined3d_device)))
872 wined3d_surface_incref(ds);
875 ddraw_destroy_swapchain(This);
878 if (FAILED(hr = ddraw_create_swapchain(This, window, !(cooplevel & DDSCL_FULLSCREEN))))
879 ERR("Failed to create swapchain, hr %#x.\n", hr);
881 if (restore_state)
883 if (ds)
885 wined3d_device_set_depth_stencil(This->wined3d_device, ds);
886 wined3d_surface_decref(ds);
889 if (rt)
891 wined3d_device_set_render_target(This->wined3d_device, 0, rt, FALSE);
892 wined3d_surface_decref(rt);
895 wined3d_stateblock_apply(stateblock);
896 wined3d_stateblock_decref(stateblock);
899 /* Unhandled flags */
900 if(cooplevel & DDSCL_ALLOWREBOOT)
901 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
902 if(cooplevel & DDSCL_ALLOWMODEX)
903 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
904 if(cooplevel & DDSCL_FPUSETUP)
905 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
907 if (cooplevel & DDSCL_EXCLUSIVE)
908 exclusive_ddraw = This;
909 else if (exclusive_ddraw == This)
910 exclusive_ddraw = NULL;
912 /* Store the cooperative_level */
913 This->cooperative_level = cooplevel;
914 This->dest_window = window;
915 TRACE("SetCooperativeLevel retuning DD_OK\n");
916 wined3d_mutex_unlock();
918 return DD_OK;
921 static HRESULT WINAPI ddraw4_SetCooperativeLevel(IDirectDraw4 *iface, HWND window, DWORD flags)
923 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
925 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
927 return ddraw7_SetCooperativeLevel(&ddraw->IDirectDraw7_iface, window, flags);
930 static HRESULT WINAPI ddraw2_SetCooperativeLevel(IDirectDraw2 *iface, HWND window, DWORD flags)
932 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
934 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
936 return ddraw7_SetCooperativeLevel(&ddraw->IDirectDraw7_iface, window, flags);
939 static HRESULT WINAPI ddraw1_SetCooperativeLevel(IDirectDraw *iface, HWND window, DWORD flags)
941 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
943 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
945 return ddraw7_SetCooperativeLevel(&ddraw->IDirectDraw7_iface, window, flags);
948 /*****************************************************************************
949 * IDirectDraw7::SetDisplayMode
951 * Sets the display screen resolution, color depth and refresh frequency
952 * when in fullscreen mode (in theory).
953 * Possible return values listed in the SDK suggest that this method fails
954 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
955 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
956 * It seems to be valid to pass 0 for With and Height, this has to be tested
957 * It could mean that the current video mode should be left as-is. (But why
958 * call it then?)
960 * Params:
961 * Height, Width: Screen dimension
962 * BPP: Color depth in Bits per pixel
963 * Refreshrate: Screen refresh rate
964 * Flags: Other stuff
966 * Returns
967 * DD_OK on success
969 *****************************************************************************/
970 static HRESULT WINAPI ddraw7_SetDisplayMode(IDirectDraw7 *iface, DWORD width, DWORD height,
971 DWORD bpp, DWORD refresh_rate, DWORD flags)
973 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
974 struct wined3d_display_mode mode;
975 enum wined3d_format_id format;
976 HRESULT hr;
978 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
979 iface, width, height, bpp, refresh_rate, flags);
981 if (force_refresh_rate != 0)
983 TRACE("ForceRefreshRate overriding passed-in refresh rate (%u Hz) to %u Hz\n",
984 refresh_rate, force_refresh_rate);
985 refresh_rate = force_refresh_rate;
988 wined3d_mutex_lock();
990 if (exclusive_ddraw && exclusive_ddraw != ddraw)
992 wined3d_mutex_unlock();
993 return DDERR_NOEXCLUSIVEMODE;
996 if (!width || !height)
998 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here. */
999 wined3d_mutex_unlock();
1000 return DD_OK;
1003 if (!restore_mode && FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d,
1004 WINED3DADAPTER_DEFAULT, &original_mode, NULL)))
1005 ERR("Failed to get current display mode, hr %#x.\n", hr);
1007 switch (bpp)
1009 case 8: format = WINED3DFMT_P8_UINT; break;
1010 case 15: format = WINED3DFMT_B5G5R5X1_UNORM; break;
1011 case 16: format = WINED3DFMT_B5G6R5_UNORM; break;
1012 case 24: format = WINED3DFMT_B8G8R8_UNORM; break;
1013 case 32: format = WINED3DFMT_B8G8R8X8_UNORM; break;
1014 default: format = WINED3DFMT_UNKNOWN; break;
1017 mode.width = width;
1018 mode.height = height;
1019 mode.refresh_rate = refresh_rate;
1020 mode.format_id = format;
1021 mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
1023 /* TODO: The possible return values from msdn suggest that the screen mode
1024 * can't be changed if a surface is locked or some drawing is in progress. */
1025 /* TODO: Lose the primary surface. */
1026 if (SUCCEEDED(hr = wined3d_set_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode)))
1028 ddraw->flags |= DDRAW_RESTORE_MODE;
1029 restore_mode = TRUE;
1032 wined3d_mutex_unlock();
1034 switch (hr)
1036 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1037 default: return hr;
1041 static HRESULT WINAPI ddraw4_SetDisplayMode(IDirectDraw4 *iface, DWORD width, DWORD height,
1042 DWORD bpp, DWORD refresh_rate, DWORD flags)
1044 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1046 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1047 iface, width, height, bpp, refresh_rate, flags);
1049 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1052 static HRESULT WINAPI ddraw2_SetDisplayMode(IDirectDraw2 *iface,
1053 DWORD width, DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
1055 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1057 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1058 iface, width, height, bpp, refresh_rate, flags);
1060 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1063 static HRESULT WINAPI ddraw1_SetDisplayMode(IDirectDraw *iface, DWORD width, DWORD height, DWORD bpp)
1065 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1067 TRACE("iface %p, width %u, height %u, bpp %u.\n", iface, width, height, bpp);
1069 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, 0, 0);
1072 /*****************************************************************************
1073 * IDirectDraw7::RestoreDisplayMode
1075 * Restores the display mode to what it was at creation time. Basically.
1077 * Returns
1078 * DD_OK on success
1079 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
1081 *****************************************************************************/
1082 static HRESULT WINAPI ddraw7_RestoreDisplayMode(IDirectDraw7 *iface)
1084 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1085 HRESULT hr;
1087 TRACE("iface %p.\n", iface);
1089 wined3d_mutex_lock();
1091 if (!(ddraw->flags & DDRAW_RESTORE_MODE))
1093 wined3d_mutex_unlock();
1094 return DD_OK;
1097 if (exclusive_ddraw && exclusive_ddraw != ddraw)
1099 wined3d_mutex_unlock();
1100 return DDERR_NOEXCLUSIVEMODE;
1103 if (SUCCEEDED(hr = wined3d_set_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &original_mode)))
1105 ddraw->flags &= ~DDRAW_RESTORE_MODE;
1106 restore_mode = FALSE;
1109 wined3d_mutex_unlock();
1111 return hr;
1114 static HRESULT WINAPI ddraw4_RestoreDisplayMode(IDirectDraw4 *iface)
1116 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1118 TRACE("iface %p.\n", iface);
1120 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
1123 static HRESULT WINAPI ddraw2_RestoreDisplayMode(IDirectDraw2 *iface)
1125 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1127 TRACE("iface %p.\n", iface);
1129 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
1132 static HRESULT WINAPI ddraw1_RestoreDisplayMode(IDirectDraw *iface)
1134 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1136 TRACE("iface %p.\n", iface);
1138 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
1141 /*****************************************************************************
1142 * IDirectDraw7::GetCaps
1144 * Returns the drives capabilities
1146 * Used for version 1, 2, 4 and 7
1148 * Params:
1149 * DriverCaps: Structure to write the Hardware accelerated caps to
1150 * HelCaps: Structure to write the emulation caps to
1152 * Returns
1153 * This implementation returns DD_OK only
1155 *****************************************************************************/
1156 static HRESULT WINAPI ddraw7_GetCaps(IDirectDraw7 *iface, DDCAPS *DriverCaps, DDCAPS *HELCaps)
1158 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1159 DDCAPS caps;
1160 WINED3DCAPS winecaps;
1161 HRESULT hr;
1162 DDSCAPS2 ddscaps = {0, 0, 0, 0};
1164 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, DriverCaps, HELCaps);
1166 /* One structure must be != NULL */
1167 if (!DriverCaps && !HELCaps)
1169 WARN("Invalid parameters.\n");
1170 return DDERR_INVALIDPARAMS;
1173 memset(&caps, 0, sizeof(caps));
1174 memset(&winecaps, 0, sizeof(winecaps));
1175 caps.dwSize = sizeof(caps);
1177 wined3d_mutex_lock();
1178 hr = wined3d_device_get_device_caps(ddraw->wined3d_device, &winecaps);
1179 if (FAILED(hr))
1181 WARN("IWineD3DDevice::GetDeviceCaps failed\n");
1182 wined3d_mutex_unlock();
1183 return hr;
1186 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
1187 wined3d_mutex_unlock();
1188 if(FAILED(hr)) {
1189 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
1190 return hr;
1193 caps.dwCaps = winecaps.ddraw_caps.caps;
1194 caps.dwCaps2 = winecaps.ddraw_caps.caps2;
1195 caps.dwCKeyCaps = winecaps.ddraw_caps.color_key_caps;
1196 caps.dwFXCaps = winecaps.ddraw_caps.fx_caps;
1197 caps.dwPalCaps = winecaps.ddraw_caps.pal_caps;
1198 caps.ddsCaps.dwCaps = winecaps.ddraw_caps.dds_caps;
1199 caps.dwSVBCaps = winecaps.ddraw_caps.svb_caps;
1200 caps.dwSVBCKeyCaps = winecaps.ddraw_caps.svb_color_key_caps;
1201 caps.dwSVBFXCaps = winecaps.ddraw_caps.svb_fx_caps;
1202 caps.dwVSBCaps = winecaps.ddraw_caps.vsb_caps;
1203 caps.dwVSBCKeyCaps = winecaps.ddraw_caps.vsb_color_key_caps;
1204 caps.dwVSBFXCaps = winecaps.ddraw_caps.vsb_fx_caps;
1205 caps.dwSSBCaps = winecaps.ddraw_caps.ssb_caps;
1206 caps.dwSSBCKeyCaps = winecaps.ddraw_caps.ssb_color_key_caps;
1207 caps.dwSSBFXCaps = winecaps.ddraw_caps.ssb_fx_caps;
1209 if (winecaps.ddraw_caps.stride_align)
1211 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
1212 caps.dwAlignStrideAlign = winecaps.ddraw_caps.stride_align;
1215 if(DriverCaps)
1217 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
1218 if (TRACE_ON(ddraw))
1220 TRACE("Driver Caps :\n");
1221 DDRAW_dump_DDCAPS(DriverCaps);
1225 if(HELCaps)
1227 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
1228 if (TRACE_ON(ddraw))
1230 TRACE("HEL Caps :\n");
1231 DDRAW_dump_DDCAPS(HELCaps);
1235 return DD_OK;
1238 static HRESULT WINAPI ddraw4_GetCaps(IDirectDraw4 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1240 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1242 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1244 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1247 static HRESULT WINAPI ddraw2_GetCaps(IDirectDraw2 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1249 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1251 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1253 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1256 static HRESULT WINAPI ddraw1_GetCaps(IDirectDraw *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1258 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1260 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1262 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1265 /*****************************************************************************
1266 * IDirectDraw7::Compact
1268 * No idea what it does, MSDN says it's not implemented.
1270 * Returns
1271 * DD_OK, but this is unchecked
1273 *****************************************************************************/
1274 static HRESULT WINAPI ddraw7_Compact(IDirectDraw7 *iface)
1276 TRACE("iface %p.\n", iface);
1278 return DD_OK;
1281 static HRESULT WINAPI ddraw4_Compact(IDirectDraw4 *iface)
1283 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1285 TRACE("iface %p.\n", iface);
1287 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1290 static HRESULT WINAPI ddraw2_Compact(IDirectDraw2 *iface)
1292 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1294 TRACE("iface %p.\n", iface);
1296 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1299 static HRESULT WINAPI ddraw1_Compact(IDirectDraw *iface)
1301 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1303 TRACE("iface %p.\n", iface);
1305 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1308 /*****************************************************************************
1309 * IDirectDraw7::GetDisplayMode
1311 * Returns information about the current display mode
1313 * Exists in Version 1, 2, 4 and 7
1315 * Params:
1316 * DDSD: Address of a surface description structure to write the info to
1318 * Returns
1319 * DD_OK
1321 *****************************************************************************/
1322 static HRESULT WINAPI ddraw7_GetDisplayMode(IDirectDraw7 *iface, DDSURFACEDESC2 *DDSD)
1324 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1325 struct wined3d_display_mode mode;
1326 HRESULT hr;
1327 DWORD Size;
1329 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
1331 wined3d_mutex_lock();
1332 /* This seems sane */
1333 if (!DDSD)
1335 wined3d_mutex_unlock();
1336 return DDERR_INVALIDPARAMS;
1339 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
1341 ERR("Failed to get display mode, hr %#x.\n", hr);
1342 wined3d_mutex_unlock();
1343 return hr;
1346 Size = DDSD->dwSize;
1347 memset(DDSD, 0, Size);
1349 DDSD->dwSize = Size;
1350 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
1351 DDSD->dwWidth = mode.width;
1352 DDSD->dwHeight = mode.height;
1353 DDSD->u2.dwRefreshRate = 60;
1354 DDSD->ddsCaps.dwCaps = 0;
1355 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
1356 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, mode.format_id);
1357 DDSD->u1.lPitch = mode.width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
1359 if(TRACE_ON(ddraw))
1361 TRACE("Returning surface desc :\n");
1362 DDRAW_dump_surface_desc(DDSD);
1365 wined3d_mutex_unlock();
1367 return DD_OK;
1370 static HRESULT WINAPI ddraw4_GetDisplayMode(IDirectDraw4 *iface, DDSURFACEDESC2 *surface_desc)
1372 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1374 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1376 return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, surface_desc);
1379 static HRESULT WINAPI ddraw2_GetDisplayMode(IDirectDraw2 *iface, DDSURFACEDESC *surface_desc)
1381 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1383 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1385 /* FIXME: Test sizes, properly convert surface_desc */
1386 return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1389 static HRESULT WINAPI ddraw1_GetDisplayMode(IDirectDraw *iface, DDSURFACEDESC *surface_desc)
1391 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1393 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1395 /* FIXME: Test sizes, properly convert surface_desc */
1396 return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1399 /*****************************************************************************
1400 * IDirectDraw7::GetFourCCCodes
1402 * Returns an array of supported FourCC codes.
1404 * Exists in Version 1, 2, 4 and 7
1406 * Params:
1407 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
1408 * of enumerated codes
1409 * Codes: Pointer to an array of DWORDs where the supported codes are written
1410 * to
1412 * Returns
1413 * Always returns DD_OK, as it's a stub for now
1415 *****************************************************************************/
1416 static HRESULT WINAPI ddraw7_GetFourCCCodes(IDirectDraw7 *iface, DWORD *NumCodes, DWORD *Codes)
1418 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1419 static const enum wined3d_format_id formats[] =
1421 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
1422 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
1423 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
1425 struct wined3d_display_mode mode;
1426 DWORD count = 0, i, outsize;
1427 HRESULT hr;
1429 TRACE("iface %p, codes_count %p, codes %p.\n", iface, NumCodes, Codes);
1431 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
1433 ERR("Failed to get display mode, hr %#x.\n", hr);
1434 return hr;
1437 outsize = NumCodes && Codes ? *NumCodes : 0;
1439 for (i = 0; i < (sizeof(formats) / sizeof(formats[0])); ++i)
1441 if (SUCCEEDED(wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, WINED3D_DEVICE_TYPE_HAL,
1442 mode.format_id, 0, WINED3D_RTYPE_SURFACE, formats[i])))
1444 if (count < outsize)
1445 Codes[count] = formats[i];
1446 ++count;
1449 if(NumCodes) {
1450 TRACE("Returning %u FourCC codes\n", count);
1451 *NumCodes = count;
1454 return DD_OK;
1457 static HRESULT WINAPI ddraw4_GetFourCCCodes(IDirectDraw4 *iface, DWORD *codes_count, DWORD *codes)
1459 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1461 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1463 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1466 static HRESULT WINAPI ddraw2_GetFourCCCodes(IDirectDraw2 *iface, DWORD *codes_count, DWORD *codes)
1468 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1470 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1472 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1475 static HRESULT WINAPI ddraw1_GetFourCCCodes(IDirectDraw *iface, DWORD *codes_count, DWORD *codes)
1477 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1479 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1481 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1484 static HRESULT WINAPI ddraw7_GetMonitorFrequency(IDirectDraw7 *iface, DWORD *frequency)
1486 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1487 struct wined3d_display_mode mode;
1488 HRESULT hr;
1490 TRACE("iface %p, frequency %p.\n", iface, frequency);
1492 wined3d_mutex_lock();
1493 hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL);
1494 wined3d_mutex_unlock();
1495 if (FAILED(hr))
1497 WARN("Failed to get display mode, hr %#x.\n", hr);
1498 return hr;
1501 *frequency = mode.refresh_rate;
1503 return DD_OK;
1506 static HRESULT WINAPI ddraw4_GetMonitorFrequency(IDirectDraw4 *iface, DWORD *frequency)
1508 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1510 TRACE("iface %p, frequency %p.\n", iface, frequency);
1512 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1515 static HRESULT WINAPI ddraw2_GetMonitorFrequency(IDirectDraw2 *iface, DWORD *frequency)
1517 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1519 TRACE("iface %p, frequency %p.\n", iface, frequency);
1521 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1524 static HRESULT WINAPI ddraw1_GetMonitorFrequency(IDirectDraw *iface, DWORD *frequency)
1526 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1528 TRACE("iface %p, frequency %p.\n", iface, frequency);
1530 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1533 static HRESULT WINAPI ddraw7_GetVerticalBlankStatus(IDirectDraw7 *iface, BOOL *status)
1535 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1536 struct wined3d_raster_status raster_status;
1537 HRESULT hr;
1539 TRACE("iface %p, status %p.\n", iface, status);
1541 if(!status)
1542 return DDERR_INVALIDPARAMS;
1544 wined3d_mutex_lock();
1545 hr = wined3d_get_adapter_raster_status(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &raster_status);
1546 wined3d_mutex_unlock();
1547 if (FAILED(hr))
1549 WARN("Failed to get raster status, hr %#x.\n", hr);
1550 return hr;
1553 *status = raster_status.in_vblank;
1555 return DD_OK;
1558 static HRESULT WINAPI ddraw4_GetVerticalBlankStatus(IDirectDraw4 *iface, BOOL *status)
1560 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1562 TRACE("iface %p, status %p.\n", iface, status);
1564 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1567 static HRESULT WINAPI ddraw2_GetVerticalBlankStatus(IDirectDraw2 *iface, BOOL *status)
1569 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1571 TRACE("iface %p, status %p.\n", iface, status);
1573 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1576 static HRESULT WINAPI ddraw1_GetVerticalBlankStatus(IDirectDraw *iface, BOOL *status)
1578 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1580 TRACE("iface %p, status %p.\n", iface, status);
1582 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1585 /*****************************************************************************
1586 * IDirectDraw7::GetAvailableVidMem
1588 * Returns the total and free video memory
1590 * Params:
1591 * Caps: Specifies the memory type asked for
1592 * total: Pointer to a DWORD to be filled with the total memory
1593 * free: Pointer to a DWORD to be filled with the free memory
1595 * Returns
1596 * DD_OK on success
1597 * DDERR_INVALIDPARAMS of free and total are NULL
1599 *****************************************************************************/
1600 static HRESULT WINAPI ddraw7_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total,
1601 DWORD *free)
1603 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1604 HRESULT hr = DD_OK;
1606 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, Caps, total, free);
1608 if (TRACE_ON(ddraw))
1610 TRACE("Asked for memory with description: ");
1611 DDRAW_dump_DDSCAPS2(Caps);
1613 wined3d_mutex_lock();
1615 /* Todo: System memory vs local video memory vs non-local video memory
1616 * The MSDN also mentions differences between texture memory and other
1617 * resources, but that's not important
1620 if( (!total) && (!free) )
1622 wined3d_mutex_unlock();
1623 return DDERR_INVALIDPARAMS;
1626 if (free)
1627 *free = wined3d_device_get_available_texture_mem(ddraw->wined3d_device);
1628 if (total)
1630 struct wined3d_adapter_identifier desc = {0};
1632 hr = wined3d_get_adapter_identifier(ddraw->wined3d, WINED3DADAPTER_DEFAULT, 0, &desc);
1633 *total = desc.video_memory;
1636 wined3d_mutex_unlock();
1638 return hr;
1641 static HRESULT WINAPI ddraw4_GetAvailableVidMem(IDirectDraw4 *iface,
1642 DDSCAPS2 *caps, DWORD *total, DWORD *free)
1644 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1646 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1648 return ddraw7_GetAvailableVidMem(&ddraw->IDirectDraw7_iface, caps, total, free);
1651 static HRESULT WINAPI ddraw2_GetAvailableVidMem(IDirectDraw2 *iface,
1652 DDSCAPS *caps, DWORD *total, DWORD *free)
1654 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1655 DDSCAPS2 caps2;
1657 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1659 DDRAW_Convert_DDSCAPS_1_To_2(caps, &caps2);
1660 return ddraw7_GetAvailableVidMem(&ddraw->IDirectDraw7_iface, &caps2, total, free);
1663 /*****************************************************************************
1664 * IDirectDraw7::Initialize
1666 * Initializes a DirectDraw interface.
1668 * Params:
1669 * GUID: Interface identifier. Well, don't know what this is really good
1670 * for
1672 * Returns
1673 * Returns DD_OK on the first call,
1674 * DDERR_ALREADYINITIALIZED on repeated calls
1676 *****************************************************************************/
1677 static HRESULT WINAPI ddraw7_Initialize(IDirectDraw7 *iface, GUID *guid)
1679 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1681 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1683 if (ddraw->flags & DDRAW_INITIALIZED)
1684 return DDERR_ALREADYINITIALIZED;
1686 /* FIXME: To properly take the GUID into account we should call
1687 * ddraw_init() here instead of in DDRAW_Create(). */
1688 if (guid)
1689 FIXME("Ignoring guid %s.\n", debugstr_guid(guid));
1691 ddraw->flags |= DDRAW_INITIALIZED;
1692 return DD_OK;
1695 static HRESULT WINAPI ddraw4_Initialize(IDirectDraw4 *iface, GUID *guid)
1697 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1699 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1701 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
1704 static HRESULT WINAPI ddraw2_Initialize(IDirectDraw2 *iface, GUID *guid)
1706 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1708 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1710 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
1713 static HRESULT WINAPI ddraw1_Initialize(IDirectDraw *iface, GUID *guid)
1715 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1717 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1719 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
1722 static HRESULT WINAPI d3d1_Initialize(IDirect3D *iface, REFIID riid)
1724 TRACE("iface %p, riid %s.\n", iface, debugstr_guid(riid));
1726 return DDERR_ALREADYINITIALIZED;
1729 /*****************************************************************************
1730 * IDirectDraw7::FlipToGDISurface
1732 * "Makes the surface that the GDI writes to the primary surface"
1733 * Looks like some windows specific thing we don't have to care about.
1734 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1735 * show error boxes ;)
1736 * Well, just return DD_OK.
1738 * Returns:
1739 * Always returns DD_OK
1741 *****************************************************************************/
1742 static HRESULT WINAPI ddraw7_FlipToGDISurface(IDirectDraw7 *iface)
1744 FIXME("iface %p stub!\n", iface);
1746 return DD_OK;
1749 static HRESULT WINAPI ddraw4_FlipToGDISurface(IDirectDraw4 *iface)
1751 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1753 TRACE("iface %p.\n", iface);
1755 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
1758 static HRESULT WINAPI ddraw2_FlipToGDISurface(IDirectDraw2 *iface)
1760 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1762 TRACE("iface %p.\n", iface);
1764 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
1767 static HRESULT WINAPI ddraw1_FlipToGDISurface(IDirectDraw *iface)
1769 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1771 TRACE("iface %p.\n", iface);
1773 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
1776 /*****************************************************************************
1777 * IDirectDraw7::WaitForVerticalBlank
1779 * This method allows applications to get in sync with the vertical blank
1780 * interval.
1781 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1782 * redraw the screen, most likely because of this stub
1784 * Parameters:
1785 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1786 * or DDWAITVB_BLOCKEND
1787 * h: Not used, according to MSDN
1789 * Returns:
1790 * Always returns DD_OK
1792 *****************************************************************************/
1793 static HRESULT WINAPI ddraw7_WaitForVerticalBlank(IDirectDraw7 *iface, DWORD Flags, HANDLE event)
1795 static BOOL hide;
1797 TRACE("iface %p, flags %#x, event %p.\n", iface, Flags, event);
1799 /* This function is called often, so print the fixme only once */
1800 if(!hide)
1802 FIXME("iface %p, flags %#x, event %p stub!\n", iface, Flags, event);
1803 hide = TRUE;
1806 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1807 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1808 return DDERR_UNSUPPORTED; /* unchecked */
1810 return DD_OK;
1813 static HRESULT WINAPI ddraw4_WaitForVerticalBlank(IDirectDraw4 *iface, DWORD flags, HANDLE event)
1815 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1817 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1819 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
1822 static HRESULT WINAPI ddraw2_WaitForVerticalBlank(IDirectDraw2 *iface, DWORD flags, HANDLE event)
1824 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1826 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1828 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
1831 static HRESULT WINAPI ddraw1_WaitForVerticalBlank(IDirectDraw *iface, DWORD flags, HANDLE event)
1833 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1835 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1837 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
1840 static HRESULT WINAPI ddraw7_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1842 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1843 struct wined3d_raster_status raster_status;
1844 HRESULT hr;
1846 TRACE("iface %p, line %p.\n", iface, Scanline);
1848 wined3d_mutex_lock();
1849 hr = wined3d_get_adapter_raster_status(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &raster_status);
1850 wined3d_mutex_unlock();
1851 if (FAILED(hr))
1853 WARN("Failed to get raster status, hr %#x.\n", hr);
1854 return hr;
1857 *Scanline = raster_status.scan_line;
1859 if (raster_status.in_vblank)
1860 return DDERR_VERTICALBLANKINPROGRESS;
1862 return DD_OK;
1865 static HRESULT WINAPI ddraw4_GetScanLine(IDirectDraw4 *iface, DWORD *line)
1867 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1869 TRACE("iface %p, line %p.\n", iface, line);
1871 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
1874 static HRESULT WINAPI ddraw2_GetScanLine(IDirectDraw2 *iface, DWORD *line)
1876 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1878 TRACE("iface %p, line %p.\n", iface, line);
1880 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
1883 static HRESULT WINAPI ddraw1_GetScanLine(IDirectDraw *iface, DWORD *line)
1885 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1887 TRACE("iface %p, line %p.\n", iface, line);
1889 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
1892 /*****************************************************************************
1893 * IDirectDraw7::TestCooperativeLevel
1895 * Informs the application about the state of the video adapter, depending
1896 * on the cooperative level
1898 * Returns:
1899 * DD_OK if the device is in a sane state
1900 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1901 * if the state is not correct(See below)
1903 *****************************************************************************/
1904 static HRESULT WINAPI ddraw7_TestCooperativeLevel(IDirectDraw7 *iface)
1906 TRACE("iface %p.\n", iface);
1908 return DD_OK;
1911 static HRESULT WINAPI ddraw4_TestCooperativeLevel(IDirectDraw4 *iface)
1913 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1915 TRACE("iface %p.\n", iface);
1917 return ddraw7_TestCooperativeLevel(&ddraw->IDirectDraw7_iface);
1920 /*****************************************************************************
1921 * IDirectDraw7::GetGDISurface
1923 * Returns the surface that GDI is treating as the primary surface.
1924 * For Wine this is the front buffer
1926 * Params:
1927 * GDISurface: Address to write the surface pointer to
1929 * Returns:
1930 * DD_OK if the surface was found
1931 * DDERR_NOTFOUND if the GDI surface wasn't found
1933 *****************************************************************************/
1934 static HRESULT WINAPI ddraw7_GetGDISurface(IDirectDraw7 *iface, IDirectDrawSurface7 **GDISurface)
1936 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1938 TRACE("iface %p, surface %p.\n", iface, GDISurface);
1940 wined3d_mutex_lock();
1942 if (!(*GDISurface = &ddraw->primary->IDirectDrawSurface7_iface))
1944 WARN("Primary not created yet.\n");
1945 wined3d_mutex_unlock();
1946 return DDERR_NOTFOUND;
1948 IDirectDrawSurface7_AddRef(*GDISurface);
1950 wined3d_mutex_unlock();
1952 return DD_OK;
1955 static HRESULT WINAPI ddraw4_GetGDISurface(IDirectDraw4 *iface, IDirectDrawSurface4 **surface)
1957 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1958 struct ddraw_surface *surface_impl;
1959 IDirectDrawSurface7 *surface7;
1960 HRESULT hr;
1962 TRACE("iface %p, surface %p.\n", iface, surface);
1964 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
1965 if (FAILED(hr))
1967 *surface = NULL;
1968 return hr;
1970 surface_impl = impl_from_IDirectDrawSurface7(surface7);
1971 *surface = &surface_impl->IDirectDrawSurface4_iface;
1972 IDirectDrawSurface4_AddRef(*surface);
1973 IDirectDrawSurface7_Release(surface7);
1975 return hr;
1978 static HRESULT WINAPI ddraw2_GetGDISurface(IDirectDraw2 *iface, IDirectDrawSurface **surface)
1980 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1981 struct ddraw_surface *surface_impl;
1982 IDirectDrawSurface7 *surface7;
1983 HRESULT hr;
1985 TRACE("iface %p, surface %p.\n", iface, surface);
1987 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
1988 if (FAILED(hr))
1990 *surface = NULL;
1991 return hr;
1993 surface_impl = impl_from_IDirectDrawSurface7(surface7);
1994 *surface = &surface_impl->IDirectDrawSurface_iface;
1995 IDirectDrawSurface_AddRef(*surface);
1996 IDirectDrawSurface7_Release(surface7);
1998 return hr;
2001 static HRESULT WINAPI ddraw1_GetGDISurface(IDirectDraw *iface, IDirectDrawSurface **surface)
2003 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2004 struct ddraw_surface *surface_impl;
2005 IDirectDrawSurface7 *surface7;
2006 HRESULT hr;
2008 TRACE("iface %p, surface %p.\n", iface, surface);
2010 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2011 if (FAILED(hr))
2013 *surface = NULL;
2014 return hr;
2016 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2017 *surface = &surface_impl->IDirectDrawSurface_iface;
2018 IDirectDrawSurface_AddRef(*surface);
2019 IDirectDrawSurface7_Release(surface7);
2021 return hr;
2024 struct displaymodescallback_context
2026 LPDDENUMMODESCALLBACK func;
2027 void *context;
2030 static HRESULT CALLBACK EnumDisplayModesCallbackThunk(DDSURFACEDESC2 *surface_desc, void *context)
2032 struct displaymodescallback_context *cbcontext = context;
2033 DDSURFACEDESC desc;
2035 DDSD2_to_DDSD(surface_desc, &desc);
2036 return cbcontext->func(&desc, cbcontext->context);
2039 /*****************************************************************************
2040 * IDirectDraw7::EnumDisplayModes
2042 * Enumerates the supported Display modes. The modes can be filtered with
2043 * the DDSD parameter.
2045 * Params:
2046 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES. For old ddraw
2047 * versions (3 and older?) this is reserved and must be 0.
2048 * DDSD: Surface description to filter the modes
2049 * Context: Pointer passed back to the callback function
2050 * cb: Application-provided callback function
2052 * Returns:
2053 * DD_OK on success
2054 * DDERR_INVALIDPARAMS if the callback wasn't set
2056 *****************************************************************************/
2057 static HRESULT WINAPI ddraw7_EnumDisplayModes(IDirectDraw7 *iface, DWORD Flags,
2058 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMMODESCALLBACK2 cb)
2060 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2061 struct wined3d_display_mode *enum_modes = NULL;
2062 struct wined3d_display_mode mode;
2063 unsigned int modenum, fmt;
2064 DDSURFACEDESC2 callback_sd;
2065 unsigned enum_mode_count = 0, enum_mode_array_size = 16;
2066 DDPIXELFORMAT pixelformat;
2068 static const enum wined3d_format_id checkFormatList[] =
2070 WINED3DFMT_B8G8R8X8_UNORM,
2071 WINED3DFMT_B5G6R5_UNORM,
2072 WINED3DFMT_P8_UINT,
2075 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2076 iface, Flags, DDSD, Context, cb);
2078 if (!cb)
2079 return DDERR_INVALIDPARAMS;
2081 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(*enum_modes) * enum_mode_array_size);
2082 if (!enum_modes) return DDERR_OUTOFMEMORY;
2084 wined3d_mutex_lock();
2086 pixelformat.dwSize = sizeof(pixelformat);
2087 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
2089 modenum = 0;
2090 while (wined3d_enum_adapter_modes(ddraw->wined3d, WINED3DADAPTER_DEFAULT, checkFormatList[fmt],
2091 WINED3D_SCANLINE_ORDERING_UNKNOWN, modenum++, &mode) == WINED3D_OK)
2093 BOOL found = FALSE;
2094 unsigned i;
2096 PixelFormat_WineD3DtoDD(&pixelformat, mode.format_id);
2097 if (DDSD)
2099 if (DDSD->dwFlags & DDSD_WIDTH && mode.width != DDSD->dwWidth)
2100 continue;
2101 if (DDSD->dwFlags & DDSD_HEIGHT && mode.height != DDSD->dwHeight)
2102 continue;
2103 if (DDSD->dwFlags & DDSD_REFRESHRATE && mode.refresh_rate != DDSD->u2.dwRefreshRate)
2104 continue;
2105 if (DDSD->dwFlags & DDSD_PIXELFORMAT
2106 && pixelformat.u1.dwRGBBitCount != DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount)
2107 continue;
2110 /* DX docs state EnumDisplayMode should return only unique modes */
2111 for (i = 0; i < enum_mode_count; i++)
2113 if (enum_modes[i].width == mode.width && enum_modes[i].height == mode.height
2114 && enum_modes[i].format_id == mode.format_id
2115 && (enum_modes[i].refresh_rate == mode.refresh_rate || !(Flags & DDEDM_REFRESHRATES)))
2117 found = TRUE;
2118 break;
2121 if(found) continue;
2123 memset(&callback_sd, 0, sizeof(callback_sd));
2124 callback_sd.dwSize = sizeof(callback_sd);
2125 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2127 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH|DDSD_REFRESHRATE;
2128 if (Flags & DDEDM_REFRESHRATES)
2129 callback_sd.u2.dwRefreshRate = mode.refresh_rate;
2131 callback_sd.dwWidth = mode.width;
2132 callback_sd.dwHeight = mode.height;
2134 callback_sd.u4.ddpfPixelFormat=pixelformat;
2136 /* Calc pitch and DWORD align like MSDN says */
2137 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.width;
2138 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
2140 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
2141 callback_sd.u2.dwRefreshRate);
2143 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
2145 TRACE("Application asked to terminate the enumeration\n");
2146 HeapFree(GetProcessHeap(), 0, enum_modes);
2147 wined3d_mutex_unlock();
2148 return DD_OK;
2151 if (enum_mode_count == enum_mode_array_size)
2153 struct wined3d_display_mode *new_enum_modes;
2155 enum_mode_array_size *= 2;
2156 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes,
2157 sizeof(*new_enum_modes) * enum_mode_array_size);
2158 if (!new_enum_modes)
2160 HeapFree(GetProcessHeap(), 0, enum_modes);
2161 wined3d_mutex_unlock();
2162 return DDERR_OUTOFMEMORY;
2165 enum_modes = new_enum_modes;
2167 enum_modes[enum_mode_count++] = mode;
2171 TRACE("End of enumeration\n");
2172 HeapFree(GetProcessHeap(), 0, enum_modes);
2173 wined3d_mutex_unlock();
2175 return DD_OK;
2178 static HRESULT WINAPI ddraw4_EnumDisplayModes(IDirectDraw4 *iface, DWORD flags,
2179 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMMODESCALLBACK2 callback)
2181 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2183 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2184 iface, flags, surface_desc, context, callback);
2186 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags, surface_desc, context, callback);
2189 static HRESULT WINAPI ddraw2_EnumDisplayModes(IDirectDraw2 *iface, DWORD flags,
2190 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2192 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2193 struct displaymodescallback_context cbcontext;
2194 DDSURFACEDESC2 surface_desc2;
2196 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2197 iface, flags, surface_desc, context, callback);
2199 cbcontext.func = callback;
2200 cbcontext.context = context;
2202 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2203 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags,
2204 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2207 static HRESULT WINAPI ddraw1_EnumDisplayModes(IDirectDraw *iface, DWORD flags,
2208 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2210 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2211 struct displaymodescallback_context cbcontext;
2212 DDSURFACEDESC2 surface_desc2;
2214 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2215 iface, flags, surface_desc, context, callback);
2217 cbcontext.func = callback;
2218 cbcontext.context = context;
2220 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2221 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags,
2222 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2225 /*****************************************************************************
2226 * IDirectDraw7::EvaluateMode
2228 * Used with IDirectDraw7::StartModeTest to test video modes.
2229 * EvaluateMode is used to pass or fail a mode, and continue with the next
2230 * mode
2232 * Params:
2233 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
2234 * Timeout: Returns the amount of seconds left before the mode would have
2235 * been failed automatically
2237 * Returns:
2238 * This implementation always DD_OK, because it's a stub
2240 *****************************************************************************/
2241 static HRESULT WINAPI ddraw7_EvaluateMode(IDirectDraw7 *iface, DWORD Flags, DWORD *Timeout)
2243 FIXME("iface %p, flags %#x, timeout %p stub!\n", iface, Flags, Timeout);
2245 /* When implementing this, implement it in WineD3D */
2247 return DD_OK;
2250 /*****************************************************************************
2251 * IDirectDraw7::GetDeviceIdentifier
2253 * Returns the device identifier, which gives information about the driver
2254 * Our device identifier is defined at the beginning of this file.
2256 * Params:
2257 * DDDI: Address for the returned structure
2258 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
2260 * Returns:
2261 * On success it returns DD_OK
2262 * DDERR_INVALIDPARAMS if DDDI is NULL
2264 *****************************************************************************/
2265 static HRESULT WINAPI ddraw7_GetDeviceIdentifier(IDirectDraw7 *iface,
2266 DDDEVICEIDENTIFIER2 *DDDI, DWORD Flags)
2268 TRACE("iface %p, device_identifier %p, flags %#x.\n", iface, DDDI, Flags);
2270 if(!DDDI)
2271 return DDERR_INVALIDPARAMS;
2273 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
2274 * host adapter, if there's a secondary 3D adapter. This doesn't apply
2275 * to any modern hardware, nor is it interesting for Wine, so ignore it.
2276 * Size of DDDEVICEIDENTIFIER2 may be aligned to 8 bytes and thus 4
2277 * bytes too long. So only copy the relevant part of the structure
2280 memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
2281 return DD_OK;
2284 static HRESULT WINAPI ddraw4_GetDeviceIdentifier(IDirectDraw4 *iface,
2285 DDDEVICEIDENTIFIER *identifier, DWORD flags)
2287 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2288 DDDEVICEIDENTIFIER2 identifier2;
2289 HRESULT hr;
2291 TRACE("iface %p, identifier %p, flags %#x.\n", iface, identifier, flags);
2293 hr = ddraw7_GetDeviceIdentifier(&ddraw->IDirectDraw7_iface, &identifier2, flags);
2294 DDRAW_Convert_DDDEVICEIDENTIFIER_2_To_1(&identifier2, identifier);
2296 return hr;
2299 /*****************************************************************************
2300 * IDirectDraw7::GetSurfaceFromDC
2302 * Returns the Surface for a GDI device context handle.
2303 * Is this related to IDirectDrawSurface::GetDC ???
2305 * Params:
2306 * hdc: hdc to return the surface for
2307 * Surface: Address to write the surface pointer to
2309 * Returns:
2310 * Always returns DD_OK because it's a stub
2312 *****************************************************************************/
2313 static HRESULT WINAPI ddraw7_GetSurfaceFromDC(IDirectDraw7 *iface, HDC hdc,
2314 IDirectDrawSurface7 **Surface)
2316 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2317 struct wined3d_surface *wined3d_surface;
2318 struct ddraw_surface *surface_impl;
2320 TRACE("iface %p, dc %p, surface %p.\n", iface, hdc, Surface);
2322 if (!Surface) return E_INVALIDARG;
2324 if (!(wined3d_surface = wined3d_device_get_surface_from_dc(ddraw->wined3d_device, hdc)))
2326 TRACE("No surface found for dc %p.\n", hdc);
2327 *Surface = NULL;
2328 return DDERR_NOTFOUND;
2331 surface_impl = wined3d_surface_get_parent(wined3d_surface);
2332 *Surface = &surface_impl->IDirectDrawSurface7_iface;
2333 IDirectDrawSurface7_AddRef(*Surface);
2334 TRACE("Returning surface %p.\n", Surface);
2335 return DD_OK;
2338 static HRESULT WINAPI ddraw4_GetSurfaceFromDC(IDirectDraw4 *iface, HDC dc,
2339 IDirectDrawSurface4 **surface)
2341 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2342 struct ddraw_surface *surface_impl;
2343 IDirectDrawSurface7 *surface7;
2344 HRESULT hr;
2346 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2348 if (!surface) return E_INVALIDARG;
2350 hr = ddraw7_GetSurfaceFromDC(&ddraw->IDirectDraw7_iface, dc, &surface7);
2351 if (FAILED(hr))
2353 *surface = NULL;
2354 return hr;
2356 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2357 /* Tests say this is true */
2358 *surface = (IDirectDrawSurface4 *)&surface_impl->IDirectDrawSurface_iface;
2359 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
2360 IDirectDrawSurface7_Release(surface7);
2362 return hr;
2365 /*****************************************************************************
2366 * IDirectDraw7::RestoreAllSurfaces
2368 * Calls the restore method of all surfaces
2370 * Params:
2372 * Returns:
2373 * Always returns DD_OK because it's a stub
2375 *****************************************************************************/
2376 static HRESULT WINAPI ddraw7_RestoreAllSurfaces(IDirectDraw7 *iface)
2378 FIXME("iface %p stub!\n", iface);
2380 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
2381 * get their parent and call their restore method. Do not implement
2382 * it in WineD3D, as restoring a surface means re-creating the
2383 * WineD3DDSurface
2385 return DD_OK;
2388 static HRESULT WINAPI ddraw4_RestoreAllSurfaces(IDirectDraw4 *iface)
2390 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2392 TRACE("iface %p.\n", iface);
2394 return ddraw7_RestoreAllSurfaces(&ddraw->IDirectDraw7_iface);
2397 /*****************************************************************************
2398 * IDirectDraw7::StartModeTest
2400 * Tests the specified video modes to update the system registry with
2401 * refresh rate information. StartModeTest starts the mode test,
2402 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
2403 * isn't called within 15 seconds, the mode is failed automatically
2405 * As refresh rates are handled by the X server, I don't think this
2406 * Method is important
2408 * Params:
2409 * Modes: An array of mode specifications
2410 * NumModes: The number of modes in Modes
2411 * Flags: Some flags...
2413 * Returns:
2414 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
2415 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
2416 * otherwise DD_OK
2418 *****************************************************************************/
2419 static HRESULT WINAPI ddraw7_StartModeTest(IDirectDraw7 *iface, SIZE *Modes, DWORD NumModes, DWORD Flags)
2421 FIXME("iface %p, modes %p, mode_count %u, flags %#x partial stub!\n",
2422 iface, Modes, NumModes, Flags);
2424 /* This looks sane */
2425 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
2427 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
2428 * As it is not, DDERR_TESTFINISHED is returned
2429 * (hopefully that's correct
2431 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
2432 * well, that value doesn't (yet) exist in the wine headers, so ignore it
2435 return DD_OK;
2438 /*****************************************************************************
2439 * ddraw_create_surface
2441 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
2442 * with the passed parameters.
2444 * Params:
2445 * DDSD: Description of the surface to create
2446 * Surf: Address to store the interface pointer at
2448 * Returns:
2449 * DD_OK on success
2451 *****************************************************************************/
2452 static HRESULT ddraw_create_surface(struct ddraw *ddraw, DDSURFACEDESC2 *pDDSD,
2453 struct ddraw_surface **surface, UINT version)
2455 HRESULT hr;
2457 TRACE("ddraw %p, surface_desc %p, surface %p.\n", ddraw, pDDSD, surface);
2459 if (TRACE_ON(ddraw))
2461 TRACE("Requesting surface desc:\n");
2462 DDRAW_dump_surface_desc(pDDSD);
2465 if ((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && (ddraw->flags & DDRAW_NO3D))
2467 WARN("The application requests a 3D capable surface, but the ddraw object was created without 3D support.\n");
2468 /* Do not fail surface creation, only fail 3D device creation. */
2471 /* Create the Surface object */
2472 *surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**surface));
2473 if (!*surface)
2475 ERR("Failed to allocate surface memory.\n");
2476 return DDERR_OUTOFVIDEOMEMORY;
2479 if (FAILED(hr = ddraw_surface_init(*surface, ddraw, pDDSD, version)))
2481 WARN("Failed to initialize surface, hr %#x.\n", hr);
2482 HeapFree(GetProcessHeap(), 0, *surface);
2483 return hr;
2486 /* Increase the surface counter, and attach the surface */
2487 list_add_head(&ddraw->surface_list, &(*surface)->surface_list_entry);
2489 TRACE("Created surface %p.\n", *surface);
2491 return DD_OK;
2494 static HRESULT CDECL ddraw_reset_enum_callback(struct wined3d_resource *resource)
2496 return DD_OK;
2499 /*****************************************************************************
2500 * IDirectDraw7::CreateSurface
2502 * Creates a new IDirectDrawSurface object and returns its interface.
2504 * The surface connections with wined3d are a bit tricky. Basically it works
2505 * like this:
2507 * |------------------------| |-----------------|
2508 * | DDraw surface | | WineD3DSurface |
2509 * | | | |
2510 * | WineD3DSurface |-------------->| |
2511 * | Child |<------------->| Parent |
2512 * |------------------------| |-----------------|
2514 * The DDraw surface is the parent of the wined3d surface, and it releases
2515 * the WineD3DSurface when the ddraw surface is destroyed.
2517 * However, for all surfaces which can be in a container in WineD3D,
2518 * we have to do this. These surfaces are usually complex surfaces,
2519 * so this concerns primary surfaces with a front and a back buffer,
2520 * and textures.
2522 * |------------------------| |-----------------|
2523 * | DDraw surface | | Container |
2524 * | | | |
2525 * | Child |<------------->| Parent |
2526 * | Texture |<------------->| |
2527 * | WineD3DSurface |<----| | Levels |<--|
2528 * | Complex connection | | | | |
2529 * |------------------------| | |-----------------| |
2530 * ^ | |
2531 * | | |
2532 * | | |
2533 * | |------------------| | |-----------------| |
2534 * | | IParent | |-------->| WineD3DSurface | |
2535 * | | | | | |
2536 * | | Child |<------------->| Parent | |
2537 * | | | | Container |<--|
2538 * | |------------------| |-----------------| |
2539 * | |
2540 * | |----------------------| |
2541 * | | DDraw surface 2 | |
2542 * | | | |
2543 * |<->| Complex root Child | |
2544 * | | Texture | |
2545 * | | WineD3DSurface |<----| |
2546 * | |----------------------| | |
2547 * | | |
2548 * | |---------------------| | |-----------------| |
2549 * | | IParent | |----->| WineD3DSurface | |
2550 * | | | | | |
2551 * | | Child |<---------->| Parent | |
2552 * | |---------------------| | Container |<--|
2553 * | |-----------------| |
2554 * | |
2555 * | ---More surfaces can follow--- |
2557 * The reason is that the IWineD3DSwapchain(render target container)
2558 * and the IWineD3DTexure(Texture container) release the parents
2559 * of their surface's children, but by releasing the complex root
2560 * the surfaces which are complexly attached to it are destroyed
2561 * too. See IDirectDrawSurface::Release for a more detailed
2562 * explanation.
2564 * Params:
2565 * DDSD: Description of the surface to create
2566 * Surf: Address to store the interface pointer at
2567 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2568 * aggregation, so it has to be NULL
2570 * Returns:
2571 * DD_OK on success
2572 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2573 * DDERR_* if an error occurs
2575 *****************************************************************************/
2576 static HRESULT CreateSurface(struct ddraw *ddraw, DDSURFACEDESC2 *DDSD,
2577 struct ddraw_surface **surface, IUnknown *UnkOuter, UINT version)
2579 struct ddraw_surface *object = NULL;
2580 struct wined3d_display_mode mode;
2581 HRESULT hr;
2582 DDSURFACEDESC2 desc2;
2583 const DWORD sysvidmem = DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
2585 TRACE("ddraw %p, surface_desc %p, surface %p, outer_unknown %p.\n", ddraw, DDSD, surface, UnkOuter);
2587 /* Some checks before we start */
2588 if (TRACE_ON(ddraw))
2590 TRACE(" (%p) Requesting surface desc :\n", ddraw);
2591 DDRAW_dump_surface_desc(DDSD);
2594 if (UnkOuter != NULL)
2596 FIXME("(%p) : outer != NULL?\n", ddraw);
2597 return CLASS_E_NOAGGREGATION; /* unchecked */
2600 if (!surface)
2602 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", ddraw);
2603 return E_POINTER; /* unchecked */
2606 if (!(DDSD->dwFlags & DDSD_CAPS))
2608 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2609 DDSD->dwFlags |= DDSD_CAPS;
2612 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2614 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2615 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2618 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2620 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2621 WARN("(%p) Null surface pointer specified, ignore it!\n", ddraw);
2622 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2625 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2626 !(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
2628 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n",
2629 ddraw);
2630 *surface = NULL;
2631 return DDERR_NOEXCLUSIVEMODE;
2634 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_BACKBUFFER | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_BACKBUFFER | DDSCAPS_PRIMARYSURFACE))
2636 WARN("Application wanted to create back buffer primary surface\n");
2637 return DDERR_INVALIDCAPS;
2640 if((DDSD->ddsCaps.dwCaps & sysvidmem) == sysvidmem)
2642 /* This is a special switch in ddrawex.dll, but not allowed in ddraw.dll */
2643 WARN("Application tries to put the surface in both system and video memory\n");
2644 *surface = NULL;
2645 return DDERR_INVALIDCAPS;
2648 /* Check cube maps but only if the size includes them */
2649 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2651 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2652 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2654 WARN("Cube map faces requested without cube map flag\n");
2655 return DDERR_INVALIDCAPS;
2657 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2658 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2660 WARN("Cube map without faces requested\n");
2661 return DDERR_INVALIDPARAMS;
2664 /* Quick tests confirm those can be created, but we don't do that yet */
2665 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2666 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2668 FIXME("Partial cube maps not supported yet\n");
2672 /* According to the msdn this flag is ignored by CreateSurface */
2673 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2674 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2676 /* Modify some flags */
2677 copy_to_surfacedesc2(&desc2, DDSD);
2678 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2680 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
2682 ERR("Failed to get display mode, hr %#x.\n", hr);
2683 return hr;
2686 /* No pixelformat given? Use the current screen format */
2687 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2689 desc2.dwFlags |= DDSD_PIXELFORMAT;
2690 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2692 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, mode.format_id);
2695 /* No Width or no Height? Use the original screen size
2697 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2698 !(desc2.dwFlags & DDSD_HEIGHT) )
2700 /* Invalid for non-render targets */
2701 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2703 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2704 *surface = NULL;
2705 return DDERR_INVALIDPARAMS;
2708 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2709 desc2.dwWidth = mode.width;
2710 desc2.dwHeight = mode.height;
2713 if (!desc2.dwWidth || !desc2.dwHeight)
2714 return DDERR_INVALIDPARAMS;
2716 /* Mipmap count fixes */
2717 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2719 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2721 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2723 /* Mipmap count is given, should not be 0 */
2724 if( desc2.u2.dwMipMapCount == 0 )
2725 return DDERR_INVALIDPARAMS;
2727 else
2729 /* Undocumented feature: Create sublevels until
2730 * either the width or the height is 1
2732 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2733 desc2.dwWidth : desc2.dwHeight;
2734 desc2.u2.dwMipMapCount = 0;
2735 while( min )
2737 desc2.u2.dwMipMapCount += 1;
2738 min >>= 1;
2742 else
2744 /* Not-complex mipmap -> Mipmapcount = 1 */
2745 desc2.u2.dwMipMapCount = 1;
2748 /* There's a mipmap count in the created surface in any case */
2749 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2751 /* If no mipmap is given, the texture has only one level */
2753 /* The first surface is a front buffer, the back buffer is created afterwards */
2754 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2756 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2759 /* The root surface in a cube map is positive x */
2760 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2762 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2763 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2766 if ((desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) && (ddraw->cooperative_level & DDSCL_EXCLUSIVE))
2768 struct wined3d_swapchain_desc swapchain_desc;
2770 wined3d_swapchain_get_desc(ddraw->wined3d_swapchain, &swapchain_desc);
2771 swapchain_desc.backbuffer_width = mode.width;
2772 swapchain_desc.backbuffer_height = mode.height;
2773 swapchain_desc.backbuffer_format = mode.format_id;
2775 hr = wined3d_device_reset(ddraw->wined3d_device,
2776 &swapchain_desc, NULL, ddraw_reset_enum_callback, TRUE);
2777 if (FAILED(hr))
2779 ERR("Failed to reset device.\n");
2780 return hr;
2784 /* Create the first surface */
2785 if (FAILED(hr = ddraw_create_surface(ddraw, &desc2, &object, version)))
2787 WARN("ddraw_create_surface failed, hr %#x.\n", hr);
2788 return hr;
2790 object->is_complex_root = TRUE;
2792 *surface = object;
2794 /* Create Additional surfaces if necessary
2795 * This applies to Primary surfaces which have a back buffer count
2796 * set, but not to mipmap textures. In case of Mipmap textures,
2797 * wineD3D takes care of the creation of additional surfaces
2799 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2801 struct ddraw_surface *last = object;
2802 UINT i;
2804 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2805 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2806 desc2.dwBackBufferCount = 0;
2808 for (i = 0; i < DDSD->dwBackBufferCount; ++i)
2810 struct ddraw_surface *object2 = NULL;
2812 if (FAILED(hr = ddraw_create_surface(ddraw, &desc2, &object2, version)))
2814 if (version == 7)
2815 IDirectDrawSurface7_Release(&object->IDirectDrawSurface7_iface);
2816 else if (version == 4)
2817 IDirectDrawSurface4_Release(&object->IDirectDrawSurface4_iface);
2818 else
2819 IDirectDrawSurface_Release(&object->IDirectDrawSurface_iface);
2821 return hr;
2824 /* Add the new surface to the complex attachment array. */
2825 last->complex_array[0] = object2;
2826 last = object2;
2828 /* Remove the (possible) back buffer cap from the new surface
2829 * description, because only one surface in the flipping chain is a
2830 * back buffer, one is a front buffer, the others are just primary
2831 * surfaces. */
2832 desc2.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2836 if (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2837 ddraw->primary = object;
2839 /* Create a WineD3DTexture if a texture was requested */
2840 if (desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
2841 ddraw_surface_create_texture(object);
2843 return hr;
2846 static HRESULT WINAPI ddraw7_CreateSurface(IDirectDraw7 *iface, DDSURFACEDESC2 *surface_desc,
2847 IDirectDrawSurface7 **surface, IUnknown *outer_unknown)
2849 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2850 struct ddraw_surface *impl;
2851 HRESULT hr;
2853 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2854 iface, surface_desc, surface, outer_unknown);
2856 wined3d_mutex_lock();
2858 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2860 WARN("Cooperative level not set.\n");
2861 wined3d_mutex_unlock();
2862 return DDERR_NOCOOPERATIVELEVELSET;
2865 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
2867 WARN("Application supplied invalid surface descriptor\n");
2868 wined3d_mutex_unlock();
2869 return DDERR_INVALIDPARAMS;
2872 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
2874 if (TRACE_ON(ddraw))
2876 TRACE(" (%p) Requesting surface desc :\n", iface);
2877 DDRAW_dump_surface_desc(surface_desc);
2880 WARN("Application tried to create an explicit front or back buffer\n");
2881 wined3d_mutex_unlock();
2882 return DDERR_INVALIDCAPS;
2885 hr = CreateSurface(ddraw, surface_desc, &impl, outer_unknown, 7);
2886 wined3d_mutex_unlock();
2887 if (FAILED(hr))
2889 *surface = NULL;
2890 return hr;
2893 *surface = &impl->IDirectDrawSurface7_iface;
2894 IDirectDraw7_AddRef(iface);
2895 impl->ifaceToRelease = (IUnknown *)iface;
2897 return hr;
2900 static HRESULT WINAPI ddraw4_CreateSurface(IDirectDraw4 *iface,
2901 DDSURFACEDESC2 *surface_desc, IDirectDrawSurface4 **surface, IUnknown *outer_unknown)
2903 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2904 struct ddraw_surface *impl;
2905 HRESULT hr;
2907 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2908 iface, surface_desc, surface, outer_unknown);
2910 wined3d_mutex_lock();
2912 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2914 WARN("Cooperative level not set.\n");
2915 wined3d_mutex_unlock();
2916 return DDERR_NOCOOPERATIVELEVELSET;
2919 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
2921 WARN("Application supplied invalid surface descriptor\n");
2922 wined3d_mutex_unlock();
2923 return DDERR_INVALIDPARAMS;
2926 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
2928 if (TRACE_ON(ddraw))
2930 TRACE(" (%p) Requesting surface desc :\n", iface);
2931 DDRAW_dump_surface_desc(surface_desc);
2934 WARN("Application tried to create an explicit front or back buffer\n");
2935 wined3d_mutex_unlock();
2936 return DDERR_INVALIDCAPS;
2939 hr = CreateSurface(ddraw, surface_desc, &impl, outer_unknown, 4);
2940 wined3d_mutex_unlock();
2941 if (FAILED(hr))
2943 *surface = NULL;
2944 return hr;
2947 *surface = &impl->IDirectDrawSurface4_iface;
2948 IDirectDraw4_AddRef(iface);
2949 impl->ifaceToRelease = (IUnknown *)iface;
2951 return hr;
2954 static HRESULT WINAPI ddraw2_CreateSurface(IDirectDraw2 *iface,
2955 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
2957 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2958 struct ddraw_surface *impl;
2959 HRESULT hr;
2960 DDSURFACEDESC2 surface_desc2;
2962 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2963 iface, surface_desc, surface, outer_unknown);
2965 wined3d_mutex_lock();
2967 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2969 WARN("Cooperative level not set.\n");
2970 wined3d_mutex_unlock();
2971 return DDERR_NOCOOPERATIVELEVELSET;
2974 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
2976 WARN("Application supplied invalid surface descriptor\n");
2977 wined3d_mutex_unlock();
2978 return DDERR_INVALIDPARAMS;
2981 DDSD_to_DDSD2(surface_desc, &surface_desc2);
2982 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
2984 if (TRACE_ON(ddraw))
2986 TRACE(" (%p) Requesting surface desc :\n", iface);
2987 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
2990 WARN("Application tried to create an explicit front or back buffer\n");
2991 wined3d_mutex_unlock();
2992 return DDERR_INVALIDCAPS;
2995 hr = CreateSurface(ddraw, &surface_desc2, &impl, outer_unknown, 2);
2996 wined3d_mutex_unlock();
2997 if (FAILED(hr))
2999 *surface = NULL;
3000 return hr;
3003 *surface = &impl->IDirectDrawSurface_iface;
3004 impl->ifaceToRelease = NULL;
3006 return hr;
3009 static HRESULT WINAPI ddraw1_CreateSurface(IDirectDraw *iface,
3010 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3012 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3013 struct ddraw_surface *impl;
3014 HRESULT hr;
3015 DDSURFACEDESC2 surface_desc2;
3017 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3018 iface, surface_desc, surface, outer_unknown);
3020 wined3d_mutex_lock();
3022 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
3024 WARN("Cooperative level not set.\n");
3025 wined3d_mutex_unlock();
3026 return DDERR_NOCOOPERATIVELEVELSET;
3029 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3031 WARN("Application supplied invalid surface descriptor\n");
3032 wined3d_mutex_unlock();
3033 return DDERR_INVALIDPARAMS;
3036 /* Remove front buffer flag, this causes failure in v7, and its added to normal
3037 * primaries anyway. */
3038 surface_desc->ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER;
3039 DDSD_to_DDSD2(surface_desc, &surface_desc2);
3040 hr = CreateSurface(ddraw, &surface_desc2, &impl, outer_unknown, 1);
3041 wined3d_mutex_unlock();
3042 if (FAILED(hr))
3044 *surface = NULL;
3045 return hr;
3048 *surface = &impl->IDirectDrawSurface_iface;
3049 impl->ifaceToRelease = NULL;
3051 return hr;
3054 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
3055 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
3057 static BOOL
3058 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
3059 const DDPIXELFORMAT *provided)
3061 /* Some flags must be present in both or neither for a match. */
3062 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
3063 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
3064 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
3066 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3067 return FALSE;
3069 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
3070 return FALSE;
3072 if (requested->dwFlags & DDPF_FOURCC)
3073 if (requested->dwFourCC != provided->dwFourCC)
3074 return FALSE;
3076 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
3077 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3078 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
3079 return FALSE;
3081 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3082 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3083 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
3084 return FALSE;
3086 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
3087 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
3088 return FALSE;
3090 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
3091 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3092 |DDPF_BUMPDUDV))
3093 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
3094 return FALSE;
3096 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
3097 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
3098 return FALSE;
3100 return TRUE;
3103 static BOOL ddraw_match_surface_desc(const DDSURFACEDESC2 *requested, const DDSURFACEDESC2 *provided)
3105 struct compare_info
3107 DWORD flag;
3108 ptrdiff_t offset;
3109 size_t size;
3112 #define CMP(FLAG, FIELD) \
3113 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
3114 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
3116 static const struct compare_info compare[] =
3118 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
3119 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
3120 CMP(CAPS, ddsCaps),
3121 CMP(CKDESTBLT, ddckCKDestBlt),
3122 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
3123 CMP(CKSRCBLT, ddckCKSrcBlt),
3124 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
3125 CMP(HEIGHT, dwHeight),
3126 CMP(LINEARSIZE, u1 /* dwLinearSize */),
3127 CMP(LPSURFACE, lpSurface),
3128 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
3129 CMP(PITCH, u1 /* lPitch */),
3130 /* PIXELFORMAT: manual */
3131 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
3132 CMP(TEXTURESTAGE, dwTextureStage),
3133 CMP(WIDTH, dwWidth),
3134 /* ZBUFFERBITDEPTH: "obsolete" */
3137 #undef CMP
3139 unsigned int i;
3141 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3142 return FALSE;
3144 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
3146 if (requested->dwFlags & compare[i].flag
3147 && memcmp((const char *)provided + compare[i].offset,
3148 (const char *)requested + compare[i].offset,
3149 compare[i].size) != 0)
3150 return FALSE;
3153 if (requested->dwFlags & DDSD_PIXELFORMAT)
3155 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
3156 &provided->u4.ddpfPixelFormat))
3157 return FALSE;
3160 return TRUE;
3163 #undef DDENUMSURFACES_SEARCHTYPE
3164 #undef DDENUMSURFACES_MATCHTYPE
3166 struct surfacescallback2_context
3168 LPDDENUMSURFACESCALLBACK2 func;
3169 void *context;
3172 struct surfacescallback_context
3174 LPDDENUMSURFACESCALLBACK func;
3175 void *context;
3178 static HRESULT CALLBACK EnumSurfacesCallback2Thunk(IDirectDrawSurface7 *surface,
3179 DDSURFACEDESC2 *surface_desc, void *context)
3181 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3182 struct surfacescallback2_context *cbcontext = context;
3184 IDirectDrawSurface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
3185 IDirectDrawSurface7_Release(surface);
3187 return cbcontext->func(&surface_impl->IDirectDrawSurface4_iface,
3188 surface_desc, cbcontext->context);
3191 static HRESULT CALLBACK EnumSurfacesCallbackThunk(IDirectDrawSurface7 *surface,
3192 DDSURFACEDESC2 *surface_desc, void *context)
3194 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3195 struct surfacescallback_context *cbcontext = context;
3197 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
3198 IDirectDrawSurface7_Release(surface);
3200 return cbcontext->func(&surface_impl->IDirectDrawSurface_iface,
3201 (DDSURFACEDESC *)surface_desc, cbcontext->context);
3204 /*****************************************************************************
3205 * IDirectDraw7::EnumSurfaces
3207 * Loops through all surfaces attached to this device and calls the
3208 * application callback. This can't be relayed to WineD3DDevice,
3209 * because some WineD3DSurfaces' parents are IParent objects
3211 * Params:
3212 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
3213 * DDSD: Description to filter for
3214 * Context: Application-provided pointer, it's passed unmodified to the
3215 * Callback function
3216 * Callback: Address to call for each surface
3218 * Returns:
3219 * DDERR_INVALIDPARAMS if the callback is NULL
3220 * DD_OK on success
3222 *****************************************************************************/
3223 static HRESULT WINAPI ddraw7_EnumSurfaces(IDirectDraw7 *iface, DWORD Flags,
3224 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMSURFACESCALLBACK7 Callback)
3226 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
3227 struct ddraw_surface *surf;
3228 BOOL all, nomatch;
3229 DDSURFACEDESC2 desc;
3230 struct list *entry, *entry2;
3232 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3233 iface, Flags, DDSD, Context, Callback);
3235 all = Flags & DDENUMSURFACES_ALL;
3236 nomatch = Flags & DDENUMSURFACES_NOMATCH;
3238 if (!Callback)
3239 return DDERR_INVALIDPARAMS;
3241 wined3d_mutex_lock();
3243 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
3244 LIST_FOR_EACH_SAFE(entry, entry2, &ddraw->surface_list)
3246 surf = LIST_ENTRY(entry, struct ddraw_surface, surface_list_entry);
3248 if (!surf->iface_count)
3250 WARN("Not enumerating surface %p because it doesn't have any references.\n", surf);
3251 continue;
3254 if (all || (nomatch != ddraw_match_surface_desc(DDSD, &surf->surface_desc)))
3256 TRACE("Enumerating surface %p.\n", surf);
3257 desc = surf->surface_desc;
3258 IDirectDrawSurface7_AddRef(&surf->IDirectDrawSurface7_iface);
3259 if (Callback(&surf->IDirectDrawSurface7_iface, &desc, Context) != DDENUMRET_OK)
3261 wined3d_mutex_unlock();
3262 return DD_OK;
3267 wined3d_mutex_unlock();
3269 return DD_OK;
3272 static HRESULT WINAPI ddraw4_EnumSurfaces(IDirectDraw4 *iface, DWORD flags,
3273 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMSURFACESCALLBACK2 callback)
3275 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3276 struct surfacescallback2_context cbcontext;
3278 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3279 iface, flags, surface_desc, context, callback);
3281 cbcontext.func = callback;
3282 cbcontext.context = context;
3284 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags, surface_desc,
3285 &cbcontext, EnumSurfacesCallback2Thunk);
3288 static HRESULT WINAPI ddraw2_EnumSurfaces(IDirectDraw2 *iface, DWORD flags,
3289 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3291 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3292 struct surfacescallback_context cbcontext;
3293 DDSURFACEDESC2 surface_desc2;
3295 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3296 iface, flags, surface_desc, context, callback);
3298 cbcontext.func = callback;
3299 cbcontext.context = context;
3301 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3302 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags,
3303 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3306 static HRESULT WINAPI ddraw1_EnumSurfaces(IDirectDraw *iface, DWORD flags,
3307 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3309 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3310 struct surfacescallback_context cbcontext;
3311 DDSURFACEDESC2 surface_desc2;
3313 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3314 iface, flags, surface_desc, context, callback);
3316 cbcontext.func = callback;
3317 cbcontext.context = context;
3319 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3320 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags,
3321 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3324 /*****************************************************************************
3325 * DirectDrawCreateClipper (DDRAW.@)
3327 * Creates a new IDirectDrawClipper object.
3329 * Params:
3330 * Clipper: Address to write the interface pointer to
3331 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3332 * NULL
3334 * Returns:
3335 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3336 * E_OUTOFMEMORY if allocating the object failed
3338 *****************************************************************************/
3339 HRESULT WINAPI DirectDrawCreateClipper(DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3341 struct ddraw_clipper *object;
3342 HRESULT hr;
3344 TRACE("flags %#x, clipper %p, outer_unknown %p.\n",
3345 flags, clipper, outer_unknown);
3347 if (outer_unknown)
3348 return CLASS_E_NOAGGREGATION;
3350 wined3d_mutex_lock();
3352 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3353 if (!object)
3355 wined3d_mutex_unlock();
3356 return E_OUTOFMEMORY;
3359 hr = ddraw_clipper_init(object);
3360 if (FAILED(hr))
3362 WARN("Failed to initialize clipper, hr %#x.\n", hr);
3363 HeapFree(GetProcessHeap(), 0, object);
3364 wined3d_mutex_unlock();
3365 return hr;
3368 TRACE("Created clipper %p.\n", object);
3369 *clipper = &object->IDirectDrawClipper_iface;
3370 wined3d_mutex_unlock();
3372 return DD_OK;
3375 /*****************************************************************************
3376 * IDirectDraw7::CreateClipper
3378 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3380 *****************************************************************************/
3381 static HRESULT WINAPI ddraw7_CreateClipper(IDirectDraw7 *iface, DWORD Flags,
3382 IDirectDrawClipper **Clipper, IUnknown *UnkOuter)
3384 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3385 iface, Flags, Clipper, UnkOuter);
3387 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3390 static HRESULT WINAPI ddraw4_CreateClipper(IDirectDraw4 *iface, DWORD flags,
3391 IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3393 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3395 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3396 iface, flags, clipper, outer_unknown);
3398 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3401 static HRESULT WINAPI ddraw2_CreateClipper(IDirectDraw2 *iface,
3402 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3404 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3406 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3407 iface, flags, clipper, outer_unknown);
3409 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3412 static HRESULT WINAPI ddraw1_CreateClipper(IDirectDraw *iface,
3413 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3415 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3417 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3418 iface, flags, clipper, outer_unknown);
3420 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3423 /*****************************************************************************
3424 * IDirectDraw7::CreatePalette
3426 * Creates a new IDirectDrawPalette object
3428 * Params:
3429 * Flags: The flags for the new clipper
3430 * ColorTable: Color table to assign to the new clipper
3431 * Palette: Address to write the interface pointer to
3432 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3433 * NULL
3435 * Returns:
3436 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3437 * E_OUTOFMEMORY if allocating the object failed
3439 *****************************************************************************/
3440 static HRESULT WINAPI ddraw7_CreatePalette(IDirectDraw7 *iface, DWORD Flags,
3441 PALETTEENTRY *ColorTable, IDirectDrawPalette **Palette, IUnknown *pUnkOuter)
3443 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
3444 struct ddraw_palette *object;
3445 HRESULT hr;
3447 TRACE("iface %p, flags %#x, color_table %p, palette %p, outer_unknown %p.\n",
3448 iface, Flags, ColorTable, Palette, pUnkOuter);
3450 if (pUnkOuter)
3451 return CLASS_E_NOAGGREGATION;
3453 wined3d_mutex_lock();
3455 /* The refcount test shows that a cooplevel is required for this */
3456 if (!ddraw->cooperative_level)
3458 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3459 wined3d_mutex_unlock();
3460 return DDERR_NOCOOPERATIVELEVELSET;
3463 object = HeapAlloc(GetProcessHeap(), 0, sizeof(*object));
3464 if (!object)
3466 ERR("Out of memory when allocating memory for a palette implementation\n");
3467 wined3d_mutex_unlock();
3468 return E_OUTOFMEMORY;
3471 hr = ddraw_palette_init(object, ddraw, Flags, ColorTable);
3472 if (FAILED(hr))
3474 WARN("Failed to initialize palette, hr %#x.\n", hr);
3475 HeapFree(GetProcessHeap(), 0, object);
3476 wined3d_mutex_unlock();
3477 return hr;
3480 TRACE("Created palette %p.\n", object);
3481 *Palette = &object->IDirectDrawPalette_iface;
3482 wined3d_mutex_unlock();
3484 return DD_OK;
3487 static HRESULT WINAPI ddraw4_CreatePalette(IDirectDraw4 *iface, DWORD flags, PALETTEENTRY *entries,
3488 IDirectDrawPalette **palette, IUnknown *outer_unknown)
3490 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3491 HRESULT hr;
3493 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3494 iface, flags, entries, palette, outer_unknown);
3496 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3497 if (SUCCEEDED(hr) && *palette)
3499 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3500 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3501 IDirectDraw4_AddRef(iface);
3502 impl->ifaceToRelease = (IUnknown *)iface;
3504 return hr;
3507 static HRESULT WINAPI ddraw2_CreatePalette(IDirectDraw2 *iface, DWORD flags,
3508 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3510 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3511 HRESULT hr;
3513 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3514 iface, flags, entries, palette, outer_unknown);
3516 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3517 if (SUCCEEDED(hr) && *palette)
3519 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3520 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3521 impl->ifaceToRelease = NULL;
3524 return hr;
3527 static HRESULT WINAPI ddraw1_CreatePalette(IDirectDraw *iface, DWORD flags,
3528 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3530 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3531 HRESULT hr;
3533 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3534 iface, flags, entries, palette, outer_unknown);
3536 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3537 if (SUCCEEDED(hr) && *palette)
3539 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3540 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3541 impl->ifaceToRelease = NULL;
3544 return hr;
3547 /*****************************************************************************
3548 * IDirectDraw7::DuplicateSurface
3550 * Duplicates a surface. The surface memory points to the same memory as
3551 * the original surface, and it's released when the last surface referencing
3552 * it is released. I guess that's beyond Wine's surface management right now
3553 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3554 * test application to implement this)
3556 * Params:
3557 * Src: Address of the source surface
3558 * Dest: Address to write the new surface pointer to
3560 * Returns:
3561 * See IDirectDraw7::CreateSurface
3563 *****************************************************************************/
3564 static HRESULT WINAPI ddraw7_DuplicateSurface(IDirectDraw7 *iface,
3565 IDirectDrawSurface7 *Src, IDirectDrawSurface7 **Dest)
3567 struct ddraw_surface *src_surface = unsafe_impl_from_IDirectDrawSurface7(Src);
3569 FIXME("iface %p, src %p, dst %p partial stub!\n", iface, Src, Dest);
3571 /* For now, simply create a new, independent surface */
3572 return IDirectDraw7_CreateSurface(iface, &src_surface->surface_desc, Dest, NULL);
3575 static HRESULT WINAPI ddraw4_DuplicateSurface(IDirectDraw4 *iface, IDirectDrawSurface4 *src,
3576 IDirectDrawSurface4 **dst)
3578 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src);
3579 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3580 struct ddraw_surface *dst_impl;
3581 IDirectDrawSurface7 *dst7;
3582 HRESULT hr;
3584 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3586 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3587 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3588 if (FAILED(hr))
3590 *dst = NULL;
3591 return hr;
3593 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3594 *dst = &dst_impl->IDirectDrawSurface4_iface;
3595 IDirectDrawSurface4_AddRef(*dst);
3596 IDirectDrawSurface7_Release(dst7);
3598 return hr;
3601 static HRESULT WINAPI ddraw2_DuplicateSurface(IDirectDraw2 *iface,
3602 IDirectDrawSurface *src, IDirectDrawSurface **dst)
3604 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3605 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3606 struct ddraw_surface *dst_impl;
3607 IDirectDrawSurface7 *dst7;
3608 HRESULT hr;
3610 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3612 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3613 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3614 if (FAILED(hr))
3615 return hr;
3616 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3617 *dst = &dst_impl->IDirectDrawSurface_iface;
3618 IDirectDrawSurface_AddRef(*dst);
3619 IDirectDrawSurface7_Release(dst7);
3621 return hr;
3624 static HRESULT WINAPI ddraw1_DuplicateSurface(IDirectDraw *iface, IDirectDrawSurface *src,
3625 IDirectDrawSurface **dst)
3627 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3628 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3629 struct ddraw_surface *dst_impl;
3630 IDirectDrawSurface7 *dst7;
3631 HRESULT hr;
3633 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3635 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3636 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3637 if (FAILED(hr))
3638 return hr;
3639 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3640 *dst = &dst_impl->IDirectDrawSurface_iface;
3641 IDirectDrawSurface_AddRef(*dst);
3642 IDirectDrawSurface7_Release(dst7);
3644 return hr;
3647 /*****************************************************************************
3648 * IDirect3D7::EnumDevices
3650 * The EnumDevices method for IDirect3D7. It enumerates all supported
3651 * D3D7 devices. Currently the T&L, HAL and RGB devices are enumerated.
3653 * Params:
3654 * callback: Function to call for each enumerated device
3655 * context: Pointer to pass back to the app
3657 * Returns:
3658 * D3D_OK, or the return value of the GetCaps call
3660 *****************************************************************************/
3661 static HRESULT WINAPI d3d7_EnumDevices(IDirect3D7 *iface, LPD3DENUMDEVICESCALLBACK7 callback, void *context)
3663 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
3664 D3DDEVICEDESC7 device_desc7;
3665 D3DDEVICEDESC device_desc1;
3666 HRESULT hr;
3667 size_t i;
3669 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3671 if (!callback)
3672 return DDERR_INVALIDPARAMS;
3674 wined3d_mutex_lock();
3676 hr = IDirect3DImpl_GetCaps(ddraw->wined3d, &device_desc1, &device_desc7);
3677 if (hr != D3D_OK)
3679 wined3d_mutex_unlock();
3680 return hr;
3683 for (i = 0; i < sizeof(device_list7)/sizeof(device_list7[0]); i++)
3685 HRESULT ret;
3687 device_desc7.deviceGUID = *device_list7[i].device_guid;
3688 ret = callback(device_list7[i].interface_name, device_list7[i].device_name, &device_desc7, context);
3689 if (ret != DDENUMRET_OK)
3691 TRACE("Application cancelled the enumeration.\n");
3692 wined3d_mutex_unlock();
3693 return D3D_OK;
3697 TRACE("End of enumeration.\n");
3699 wined3d_mutex_unlock();
3701 return D3D_OK;
3704 /*****************************************************************************
3705 * IDirect3D3::EnumDevices
3707 * Enumerates all supported Direct3DDevice interfaces. This is the
3708 * implementation for Direct3D 1 to Direc3D 3, Version 7 has its own.
3710 * Version 1, 2 and 3
3712 * Params:
3713 * callback: Application-provided routine to call for each enumerated device
3714 * Context: Pointer to pass to the callback
3716 * Returns:
3717 * D3D_OK on success,
3718 * The result of IDirect3DImpl_GetCaps if it failed
3720 *****************************************************************************/
3721 static HRESULT WINAPI d3d3_EnumDevices(IDirect3D3 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3723 static CHAR wined3d_description[] = "Wine D3DDevice using WineD3D and OpenGL";
3725 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3726 D3DDEVICEDESC device_desc1, hal_desc, hel_desc;
3727 D3DDEVICEDESC7 device_desc7;
3728 HRESULT hr;
3730 /* Some games (Motoracer 2 demo) have the bad idea to modify the device
3731 * name string. Let's put the string in a sufficiently sized array in
3732 * writable memory. */
3733 char device_name[50];
3734 strcpy(device_name,"Direct3D HEL");
3736 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3738 if (!callback)
3739 return DDERR_INVALIDPARAMS;
3741 wined3d_mutex_lock();
3743 hr = IDirect3DImpl_GetCaps(ddraw->wined3d, &device_desc1, &device_desc7);
3744 if (hr != D3D_OK)
3746 wined3d_mutex_unlock();
3747 return hr;
3750 /* Do I have to enumerate the reference id? Note from old d3d7:
3751 * "It seems that enumerating the reference IID on Direct3D 1 games
3752 * (AvP / Motoracer2) breaks them". So do not enumerate this iid in V1
3754 * There's a registry key HKLM\Software\Microsoft\Direct3D\Drivers,
3755 * EnumReference which enables / disables enumerating the reference
3756 * rasterizer. It's a DWORD, 0 means disabled, 2 means enabled. The
3757 * enablerefrast.reg and disablerefrast.reg files in the DirectX 7.0 sdk
3758 * demo directory suggest this.
3760 * Some games(GTA 2) seem to use the second enumerated device, so I have
3761 * to enumerate at least 2 devices. So enumerate the reference device to
3762 * have 2 devices.
3764 * Other games (Rollcage) tell emulation and hal device apart by certain
3765 * flags. Rollcage expects D3DPTEXTURECAPS_POW2 to be set (yeah, it is a
3766 * limitation flag), and it refuses all devices that have the perspective
3767 * flag set. This way it refuses the emulation device, and HAL devices
3768 * never have POW2 unset in d3d7 on windows. */
3769 if (ddraw->d3dversion != 1)
3771 static CHAR reference_description[] = "RGB Direct3D emulation";
3773 TRACE("Enumerating WineD3D D3DDevice interface.\n");
3774 hal_desc = device_desc1;
3775 hel_desc = device_desc1;
3776 /* The rgb device has the pow2 flag set in the hel caps, but not in the hal caps. */
3777 hal_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3778 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3779 hal_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3780 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3781 /* RGB, RAMP and MMX devices have a HAL dcmColorModel of 0 */
3782 hal_desc.dcmColorModel = 0;
3784 hr = callback((GUID *)&IID_IDirect3DRGBDevice, reference_description,
3785 device_name, &hal_desc, &hel_desc, context);
3786 if (hr != D3DENUMRET_OK)
3788 TRACE("Application cancelled the enumeration.\n");
3789 wined3d_mutex_unlock();
3790 return D3D_OK;
3794 strcpy(device_name,"Direct3D HAL");
3796 TRACE("Enumerating HAL Direct3D device.\n");
3797 hal_desc = device_desc1;
3798 hel_desc = device_desc1;
3800 /* The hal device does not have the pow2 flag set in hel, but in hal. */
3801 hel_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3802 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3803 hel_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3804 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3805 /* HAL devices have a HEL dcmColorModel of 0 */
3806 hel_desc.dcmColorModel = 0;
3808 hr = callback((GUID *)&IID_IDirect3DHALDevice, wined3d_description,
3809 device_name, &hal_desc, &hel_desc, context);
3810 if (hr != D3DENUMRET_OK)
3812 TRACE("Application cancelled the enumeration.\n");
3813 wined3d_mutex_unlock();
3814 return D3D_OK;
3817 TRACE("End of enumeration.\n");
3819 wined3d_mutex_unlock();
3821 return D3D_OK;
3824 static HRESULT WINAPI d3d2_EnumDevices(IDirect3D2 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3826 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3828 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3830 return d3d3_EnumDevices(&ddraw->IDirect3D3_iface, callback, context);
3833 static HRESULT WINAPI d3d1_EnumDevices(IDirect3D *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3835 struct ddraw *ddraw = impl_from_IDirect3D(iface);
3837 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3839 return d3d3_EnumDevices(&ddraw->IDirect3D3_iface, callback, context);
3842 /*****************************************************************************
3843 * IDirect3D3::CreateLight
3845 * Creates an IDirect3DLight interface. This interface is used in
3846 * Direct3D3 or earlier for lighting. In Direct3D7 it has been replaced
3847 * by the DIRECT3DLIGHT7 structure. Wine's Direct3DLight implementation
3848 * uses the IDirect3DDevice7 interface with D3D7 lights.
3850 * Version 1, 2 and 3
3852 * Params:
3853 * light: Address to store the new interface pointer
3854 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
3855 * Must be NULL
3857 * Returns:
3858 * D3D_OK on success
3859 * DDERR_OUTOFMEMORY if memory allocation failed
3860 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
3862 *****************************************************************************/
3863 static HRESULT WINAPI d3d3_CreateLight(IDirect3D3 *iface, IDirect3DLight **light,
3864 IUnknown *outer_unknown)
3866 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3867 struct d3d_light *object;
3869 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3871 if (outer_unknown) return CLASS_E_NOAGGREGATION;
3873 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3874 if (!object)
3876 ERR("Failed to allocate light memory.\n");
3877 return DDERR_OUTOFMEMORY;
3880 d3d_light_init(object, ddraw);
3882 TRACE("Created light %p.\n", object);
3883 *light = &object->IDirect3DLight_iface;
3885 return D3D_OK;
3888 static HRESULT WINAPI d3d2_CreateLight(IDirect3D2 *iface, IDirect3DLight **light, IUnknown *outer_unknown)
3890 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3892 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3894 return d3d3_CreateLight(&ddraw->IDirect3D3_iface, light, outer_unknown);
3897 static HRESULT WINAPI d3d1_CreateLight(IDirect3D *iface, IDirect3DLight **light, IUnknown *outer_unknown)
3899 struct ddraw *ddraw = impl_from_IDirect3D(iface);
3901 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3903 return d3d3_CreateLight(&ddraw->IDirect3D3_iface, light, outer_unknown);
3906 /*****************************************************************************
3907 * IDirect3D3::CreateMaterial
3909 * Creates an IDirect3DMaterial interface. This interface is used by Direct3D3
3910 * and older versions. The IDirect3DMaterial implementation wraps its
3911 * functionality to IDirect3DDevice7::SetMaterial and friends.
3913 * Version 1, 2 and 3
3915 * Params:
3916 * material: Address to store the new interface's pointer to
3917 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
3918 * Must be NULL
3920 * Returns:
3921 * D3D_OK on success
3922 * DDERR_OUTOFMEMORY if memory allocation failed
3923 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
3925 *****************************************************************************/
3926 static HRESULT WINAPI d3d3_CreateMaterial(IDirect3D3 *iface, IDirect3DMaterial3 **material,
3927 IUnknown *outer_unknown)
3929 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3930 struct d3d_material *object;
3932 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
3934 if (outer_unknown) return CLASS_E_NOAGGREGATION;
3936 object = d3d_material_create(ddraw);
3937 if (!object)
3939 ERR("Failed to allocate material memory.\n");
3940 return DDERR_OUTOFMEMORY;
3943 TRACE("Created material %p.\n", object);
3944 *material = &object->IDirect3DMaterial3_iface;
3946 return D3D_OK;
3949 static HRESULT WINAPI d3d2_CreateMaterial(IDirect3D2 *iface, IDirect3DMaterial2 **material,
3950 IUnknown *outer_unknown)
3952 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3953 struct d3d_material *object;
3955 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
3957 object = d3d_material_create(ddraw);
3958 if (!object)
3960 ERR("Failed to allocate material memory.\n");
3961 return DDERR_OUTOFMEMORY;
3964 TRACE("Created material %p.\n", object);
3965 *material = &object->IDirect3DMaterial2_iface;
3967 return D3D_OK;
3970 static HRESULT WINAPI d3d1_CreateMaterial(IDirect3D *iface, IDirect3DMaterial **material,
3971 IUnknown *outer_unknown)
3973 struct ddraw *ddraw = impl_from_IDirect3D(iface);
3974 struct d3d_material *object;
3976 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
3978 object = d3d_material_create(ddraw);
3979 if (!object)
3981 ERR("Failed to allocate material memory.\n");
3982 return DDERR_OUTOFMEMORY;
3985 TRACE("Created material %p.\n", object);
3986 *material = &object->IDirect3DMaterial_iface;
3988 return D3D_OK;
3991 /*****************************************************************************
3992 * IDirect3D3::CreateViewport
3994 * Creates an IDirect3DViewport interface. This interface is used
3995 * by Direct3D and earlier versions for Viewport management. In Direct3D7
3996 * it has been replaced by a viewport structure and
3997 * IDirect3DDevice7::*Viewport. Wine's IDirect3DViewport implementation
3998 * uses the IDirect3DDevice7 methods for its functionality
4000 * Params:
4001 * Viewport: Address to store the new interface pointer
4002 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4003 * Must be NULL
4005 * Returns:
4006 * D3D_OK on success
4007 * DDERR_OUTOFMEMORY if memory allocation failed
4008 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4010 *****************************************************************************/
4011 static HRESULT WINAPI d3d3_CreateViewport(IDirect3D3 *iface, IDirect3DViewport3 **viewport,
4012 IUnknown *outer_unknown)
4014 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4015 struct d3d_viewport *object;
4017 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4019 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4021 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4022 if (!object)
4024 ERR("Failed to allocate viewport memory.\n");
4025 return DDERR_OUTOFMEMORY;
4028 d3d_viewport_init(object, ddraw);
4030 TRACE("Created viewport %p.\n", object);
4031 *viewport = &object->IDirect3DViewport3_iface;
4033 return D3D_OK;
4036 static HRESULT WINAPI d3d2_CreateViewport(IDirect3D2 *iface, IDirect3DViewport2 **viewport, IUnknown *outer_unknown)
4038 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4040 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4042 return d3d3_CreateViewport(&ddraw->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4043 outer_unknown);
4046 static HRESULT WINAPI d3d1_CreateViewport(IDirect3D *iface, IDirect3DViewport **viewport, IUnknown *outer_unknown)
4048 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4050 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4052 return d3d3_CreateViewport(&ddraw->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4053 outer_unknown);
4056 /*****************************************************************************
4057 * IDirect3D3::FindDevice
4059 * This method finds a device with the requested properties and returns a
4060 * device description
4062 * Verion 1, 2 and 3
4063 * Params:
4064 * fds: Describes the requested device characteristics
4065 * fdr: Returns the device description
4067 * Returns:
4068 * D3D_OK on success
4069 * DDERR_INVALIDPARAMS if no device was found
4071 *****************************************************************************/
4072 static HRESULT WINAPI d3d3_FindDevice(IDirect3D3 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4074 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4075 D3DDEVICEDESC7 desc7;
4076 D3DDEVICEDESC desc1;
4077 HRESULT hr;
4079 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4081 if (!fds || !fdr) return DDERR_INVALIDPARAMS;
4083 if (fds->dwSize != sizeof(D3DFINDDEVICESEARCH)
4084 || fdr->dwSize != sizeof(D3DFINDDEVICERESULT))
4085 return DDERR_INVALIDPARAMS;
4087 if ((fds->dwFlags & D3DFDS_COLORMODEL)
4088 && fds->dcmColorModel != D3DCOLOR_RGB)
4090 WARN("Trying to request a non-RGB D3D color model. Not supported.\n");
4091 return DDERR_INVALIDPARAMS; /* No real idea what to return here :-) */
4094 if (fds->dwFlags & D3DFDS_GUID)
4096 TRACE("Trying to match guid %s.\n", debugstr_guid(&(fds->guid)));
4097 if (!IsEqualGUID(&IID_D3DDEVICE_WineD3D, &fds->guid)
4098 && !IsEqualGUID(&IID_IDirect3DHALDevice, &fds->guid)
4099 && !IsEqualGUID(&IID_IDirect3DRGBDevice, &fds->guid))
4101 WARN("No match for this GUID.\n");
4102 return DDERR_NOTFOUND;
4106 /* Get the caps */
4107 hr = IDirect3DImpl_GetCaps(ddraw->wined3d, &desc1, &desc7);
4108 if (hr != D3D_OK) return hr;
4110 /* Now return our own GUID */
4111 fdr->guid = IID_D3DDEVICE_WineD3D;
4112 fdr->ddHwDesc = desc1;
4113 fdr->ddSwDesc = desc1;
4115 TRACE("Returning Wine's wined3d device with (undumped) capabilities.\n");
4117 return D3D_OK;
4120 static HRESULT WINAPI d3d2_FindDevice(IDirect3D2 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4122 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4124 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4126 return d3d3_FindDevice(&ddraw->IDirect3D3_iface, fds, fdr);
4129 static HRESULT WINAPI d3d1_FindDevice(IDirect3D *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4131 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4133 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4135 return d3d3_FindDevice(&ddraw->IDirect3D3_iface, fds, fdr);
4138 /*****************************************************************************
4139 * IDirect3D7::CreateDevice
4141 * Creates an IDirect3DDevice7 interface.
4143 * Version 2, 3 and 7. IDirect3DDevice 1 interfaces are interfaces to
4144 * DirectDraw surfaces and are created with
4145 * IDirectDrawSurface::QueryInterface. This method uses CreateDevice to
4146 * create the device object and QueryInterfaces for IDirect3DDevice
4148 * Params:
4149 * refiid: IID of the device to create
4150 * Surface: Initial rendertarget
4151 * Device: Address to return the interface pointer
4153 * Returns:
4154 * D3D_OK on success
4155 * DDERR_OUTOFMEMORY if memory allocation failed
4156 * DDERR_INVALIDPARAMS if a device exists already
4158 *****************************************************************************/
4159 static HRESULT WINAPI d3d7_CreateDevice(IDirect3D7 *iface, REFCLSID riid,
4160 IDirectDrawSurface7 *surface, IDirect3DDevice7 **device)
4162 struct ddraw_surface *target = unsafe_impl_from_IDirectDrawSurface7(surface);
4163 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4164 struct d3d_device *object;
4165 HRESULT hr;
4167 TRACE("iface %p, riid %s, surface %p, device %p.\n", iface, debugstr_guid(riid), surface, device);
4169 wined3d_mutex_lock();
4170 hr = d3d_device_create(ddraw, target, 7, &object, NULL);
4171 if (SUCCEEDED(hr))
4172 *device = &object->IDirect3DDevice7_iface;
4173 else
4175 WARN("Failed to create device, hr %#x.\n", hr);
4176 *device = NULL;
4178 wined3d_mutex_unlock();
4180 return hr;
4183 static HRESULT WINAPI d3d3_CreateDevice(IDirect3D3 *iface, REFCLSID riid,
4184 IDirectDrawSurface4 *surface, IDirect3DDevice3 **device, IUnknown *outer_unknown)
4186 struct ddraw_surface *surface_impl = unsafe_impl_from_IDirectDrawSurface4(surface);
4187 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4188 struct d3d_device *device_impl;
4189 HRESULT hr;
4191 TRACE("iface %p, riid %s, surface %p, device %p, outer_unknown %p.\n",
4192 iface, debugstr_guid(riid), surface, device, outer_unknown);
4194 if (outer_unknown)
4195 return CLASS_E_NOAGGREGATION;
4197 wined3d_mutex_lock();
4198 hr = d3d_device_create(ddraw, surface_impl, 3, &device_impl, NULL);
4199 if (SUCCEEDED(hr))
4200 *device = &device_impl->IDirect3DDevice3_iface;
4201 else
4203 WARN("Failed to create device, hr %#x.\n", hr);
4204 *device = NULL;
4206 wined3d_mutex_unlock();
4208 return hr;
4211 static HRESULT WINAPI d3d2_CreateDevice(IDirect3D2 *iface, REFCLSID riid,
4212 IDirectDrawSurface *surface, IDirect3DDevice2 **device)
4214 struct ddraw_surface *surface_impl = unsafe_impl_from_IDirectDrawSurface(surface);
4215 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4216 struct d3d_device *device_impl;
4217 HRESULT hr;
4219 TRACE("iface %p, riid %s, surface %p, device %p.\n",
4220 iface, debugstr_guid(riid), surface, device);
4222 wined3d_mutex_lock();
4223 hr = d3d_device_create(ddraw, surface_impl, 2, &device_impl, NULL);
4224 if (SUCCEEDED(hr))
4225 *device = &device_impl->IDirect3DDevice2_iface;
4226 else
4228 WARN("Failed to create device, hr %#x.\n", hr);
4229 *device = NULL;
4231 wined3d_mutex_unlock();
4233 return hr;
4236 /*****************************************************************************
4237 * IDirect3D7::CreateVertexBuffer
4239 * Creates a new vertex buffer object and returns a IDirect3DVertexBuffer7
4240 * interface.
4242 * Version 3 and 7
4244 * Params:
4245 * desc: Requested Vertex buffer properties
4246 * vertex_buffer: Address to return the interface pointer at
4247 * flags: Some flags, should be 0
4249 * Returns
4250 * D3D_OK on success
4251 * DDERR_OUTOFMEMORY if memory allocation failed
4252 * The return value of IWineD3DDevice::CreateVertexBuffer if this call fails
4253 * DDERR_INVALIDPARAMS if desc or vertex_buffer are NULL
4255 *****************************************************************************/
4256 static HRESULT WINAPI d3d7_CreateVertexBuffer(IDirect3D7 *iface, D3DVERTEXBUFFERDESC *desc,
4257 IDirect3DVertexBuffer7 **vertex_buffer, DWORD flags)
4259 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4260 struct d3d_vertex_buffer *object;
4261 HRESULT hr;
4263 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x.\n",
4264 iface, desc, vertex_buffer, flags);
4266 if (!vertex_buffer || !desc) return DDERR_INVALIDPARAMS;
4268 hr = d3d_vertex_buffer_create(&object, ddraw, desc);
4269 if (hr == D3D_OK)
4271 TRACE("Created vertex buffer %p.\n", object);
4272 *vertex_buffer = &object->IDirect3DVertexBuffer7_iface;
4274 else
4275 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4277 return hr;
4280 static HRESULT WINAPI d3d3_CreateVertexBuffer(IDirect3D3 *iface, D3DVERTEXBUFFERDESC *desc,
4281 IDirect3DVertexBuffer **vertex_buffer, DWORD flags, IUnknown *outer_unknown)
4283 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4284 struct d3d_vertex_buffer *object;
4285 HRESULT hr;
4287 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x, outer_unknown %p.\n",
4288 iface, desc, vertex_buffer, flags, outer_unknown);
4290 if (outer_unknown)
4291 return CLASS_E_NOAGGREGATION;
4292 if (!vertex_buffer || !desc)
4293 return DDERR_INVALIDPARAMS;
4295 hr = d3d_vertex_buffer_create(&object, ddraw, desc);
4296 if (hr == D3D_OK)
4298 TRACE("Created vertex buffer %p.\n", object);
4299 *vertex_buffer = &object->IDirect3DVertexBuffer_iface;
4301 else
4302 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4304 return hr;
4307 /*****************************************************************************
4308 * IDirect3D7::EnumZBufferFormats
4310 * Enumerates all supported Z buffer pixel formats
4312 * Version 3 and 7
4314 * Params:
4315 * device_iid:
4316 * callback: callback to call for each pixel format
4317 * context: Pointer to pass back to the callback
4319 * Returns:
4320 * D3D_OK on success
4321 * DDERR_INVALIDPARAMS if callback is NULL
4322 * For details, see IWineD3DDevice::EnumZBufferFormats
4324 *****************************************************************************/
4325 static HRESULT WINAPI d3d7_EnumZBufferFormats(IDirect3D7 *iface, REFCLSID device_iid,
4326 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4328 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4329 struct wined3d_display_mode mode;
4330 enum wined3d_device_type type;
4331 unsigned int i;
4332 HRESULT hr;
4334 /* Order matters. Specifically, BattleZone II (full version) expects the
4335 * 16-bit depth formats to be listed before the 24 and 32 ones. */
4336 static const enum wined3d_format_id formats[] =
4338 WINED3DFMT_S1_UINT_D15_UNORM,
4339 WINED3DFMT_D16_UNORM,
4340 WINED3DFMT_X8D24_UNORM,
4341 WINED3DFMT_S4X4_UINT_D24_UNORM,
4342 WINED3DFMT_D24_UNORM_S8_UINT,
4343 WINED3DFMT_D32_UNORM,
4346 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4347 iface, debugstr_guid(device_iid), callback, context);
4349 if (!callback) return DDERR_INVALIDPARAMS;
4351 if (IsEqualGUID(device_iid, &IID_IDirect3DHALDevice)
4352 || IsEqualGUID(device_iid, &IID_IDirect3DTnLHalDevice)
4353 || IsEqualGUID(device_iid, &IID_D3DDEVICE_WineD3D))
4355 TRACE("Asked for HAL device.\n");
4356 type = WINED3D_DEVICE_TYPE_HAL;
4358 else if (IsEqualGUID(device_iid, &IID_IDirect3DRGBDevice)
4359 || IsEqualGUID(device_iid, &IID_IDirect3DMMXDevice))
4361 TRACE("Asked for SW device.\n");
4362 type = WINED3D_DEVICE_TYPE_SW;
4364 else if (IsEqualGUID(device_iid, &IID_IDirect3DRefDevice))
4366 TRACE("Asked for REF device.\n");
4367 type = WINED3D_DEVICE_TYPE_REF;
4369 else if (IsEqualGUID(device_iid, &IID_IDirect3DNullDevice))
4371 TRACE("Asked for NULLREF device.\n");
4372 type = WINED3D_DEVICE_TYPE_NULLREF;
4374 else
4376 FIXME("Unexpected device GUID %s.\n", debugstr_guid(device_iid));
4377 type = WINED3D_DEVICE_TYPE_HAL;
4380 wined3d_mutex_lock();
4381 /* We need an adapter format from somewhere to please wined3d and WGL.
4382 * Use the current display mode. So far all cards offer the same depth
4383 * stencil format for all modes, but if some do not and applications do
4384 * not like that we'll have to find some workaround, like iterating over
4385 * all imaginable formats and collecting all the depth stencil formats we
4386 * can get. */
4387 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
4389 ERR("Failed to get display mode, hr %#x.\n", hr);
4390 wined3d_mutex_unlock();
4391 return hr;
4394 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); ++i)
4396 if (SUCCEEDED(wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, type, mode.format_id,
4397 WINED3DUSAGE_DEPTHSTENCIL, WINED3D_RTYPE_SURFACE, formats[i])))
4399 DDPIXELFORMAT pformat;
4401 memset(&pformat, 0, sizeof(pformat));
4402 pformat.dwSize = sizeof(pformat);
4403 PixelFormat_WineD3DtoDD(&pformat, formats[i]);
4405 TRACE("Enumerating wined3d format %#x.\n", formats[i]);
4406 hr = callback(&pformat, context);
4407 if (hr != DDENUMRET_OK)
4409 TRACE("Format enumeration cancelled by application.\n");
4410 wined3d_mutex_unlock();
4411 return D3D_OK;
4416 /* Historically some windows drivers used dwZBufferBitDepth=24 for WINED3DFMT_X8D24_UNORM,
4417 * while others used dwZBufferBitDepth=32. In either case the pitch matches a 32 bits per
4418 * pixel format, so we use dwZBufferBitDepth=32. Some games expect 24. Windows Vista and
4419 * newer enumerate both versions, so we do the same(bug 22434) */
4420 if (SUCCEEDED(wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, type, mode.format_id,
4421 WINED3DUSAGE_DEPTHSTENCIL, WINED3D_RTYPE_SURFACE, WINED3DFMT_X8D24_UNORM)))
4423 DDPIXELFORMAT x8d24 =
4425 sizeof(x8d24), DDPF_ZBUFFER, 0,
4426 {24}, {0x00000000}, {0x00ffffff}, {0x00000000}
4428 TRACE("Enumerating WINED3DFMT_X8D24_UNORM, dwZBufferBitDepth=24 version\n");
4429 callback(&x8d24, context);
4432 TRACE("End of enumeration.\n");
4434 wined3d_mutex_unlock();
4436 return D3D_OK;
4439 static HRESULT WINAPI d3d3_EnumZBufferFormats(IDirect3D3 *iface, REFCLSID device_iid,
4440 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4442 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4444 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4445 iface, debugstr_guid(device_iid), callback, context);
4447 return d3d7_EnumZBufferFormats(&ddraw->IDirect3D7_iface, device_iid, callback, context);
4450 /*****************************************************************************
4451 * IDirect3D7::EvictManagedTextures
4453 * Removes all managed textures (=surfaces with DDSCAPS2_TEXTUREMANAGE or
4454 * DDSCAPS2_D3DTEXTUREMANAGE caps) to be removed from video memory.
4456 * Version 3 and 7
4458 * Returns:
4459 * D3D_OK, because it's a stub
4461 *****************************************************************************/
4462 static HRESULT WINAPI d3d7_EvictManagedTextures(IDirect3D7 *iface)
4464 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4466 TRACE("iface %p!\n", iface);
4468 wined3d_mutex_lock();
4469 if (ddraw->flags & DDRAW_D3D_INITIALIZED)
4470 wined3d_device_evict_managed_resources(ddraw->wined3d_device);
4471 wined3d_mutex_unlock();
4473 return D3D_OK;
4476 static HRESULT WINAPI d3d3_EvictManagedTextures(IDirect3D3 *iface)
4478 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4480 TRACE("iface %p.\n", iface);
4482 return d3d7_EvictManagedTextures(&ddraw->IDirect3D7_iface);
4485 /*****************************************************************************
4486 * IDirect3DImpl_GetCaps
4488 * This function retrieves the device caps from wined3d
4489 * and converts it into a D3D7 and D3D - D3D3 structure
4490 * This is a helper function called from various places in ddraw
4492 * Params:
4493 * wined3d: The interface to get the caps from
4494 * desc1: Old D3D <3 structure to fill (needed)
4495 * desc7: D3D7 device desc structure to fill (needed)
4497 * Returns
4498 * D3D_OK on success, or the return value of IWineD3D::GetCaps
4500 *****************************************************************************/
4501 HRESULT IDirect3DImpl_GetCaps(const struct wined3d *wined3d, D3DDEVICEDESC *desc1, D3DDEVICEDESC7 *desc7)
4503 WINED3DCAPS wined3d_caps;
4504 HRESULT hr;
4506 TRACE("wined3d %p, desc1 %p, desc7 %p.\n", wined3d, desc1, desc7);
4508 memset(&wined3d_caps, 0, sizeof(wined3d_caps));
4510 wined3d_mutex_lock();
4511 hr = wined3d_get_device_caps(wined3d, 0, WINED3D_DEVICE_TYPE_HAL, &wined3d_caps);
4512 wined3d_mutex_unlock();
4513 if (FAILED(hr))
4515 WARN("Failed to get device caps, hr %#x.\n", hr);
4516 return hr;
4519 /* Copy the results into the d3d7 and d3d3 structures */
4520 desc7->dwDevCaps = wined3d_caps.DevCaps;
4521 desc7->dpcLineCaps.dwMiscCaps = wined3d_caps.PrimitiveMiscCaps;
4522 desc7->dpcLineCaps.dwRasterCaps = wined3d_caps.RasterCaps;
4523 desc7->dpcLineCaps.dwZCmpCaps = wined3d_caps.ZCmpCaps;
4524 desc7->dpcLineCaps.dwSrcBlendCaps = wined3d_caps.SrcBlendCaps;
4525 desc7->dpcLineCaps.dwDestBlendCaps = wined3d_caps.DestBlendCaps;
4526 desc7->dpcLineCaps.dwAlphaCmpCaps = wined3d_caps.AlphaCmpCaps;
4527 desc7->dpcLineCaps.dwShadeCaps = wined3d_caps.ShadeCaps;
4528 desc7->dpcLineCaps.dwTextureCaps = wined3d_caps.TextureCaps;
4529 desc7->dpcLineCaps.dwTextureFilterCaps = wined3d_caps.TextureFilterCaps;
4530 desc7->dpcLineCaps.dwTextureAddressCaps = wined3d_caps.TextureAddressCaps;
4532 desc7->dwMaxTextureWidth = wined3d_caps.MaxTextureWidth;
4533 desc7->dwMaxTextureHeight = wined3d_caps.MaxTextureHeight;
4535 desc7->dwMaxTextureRepeat = wined3d_caps.MaxTextureRepeat;
4536 desc7->dwMaxTextureAspectRatio = wined3d_caps.MaxTextureAspectRatio;
4537 desc7->dwMaxAnisotropy = wined3d_caps.MaxAnisotropy;
4538 desc7->dvMaxVertexW = wined3d_caps.MaxVertexW;
4540 desc7->dvGuardBandLeft = wined3d_caps.GuardBandLeft;
4541 desc7->dvGuardBandTop = wined3d_caps.GuardBandTop;
4542 desc7->dvGuardBandRight = wined3d_caps.GuardBandRight;
4543 desc7->dvGuardBandBottom = wined3d_caps.GuardBandBottom;
4545 desc7->dvExtentsAdjust = wined3d_caps.ExtentsAdjust;
4546 desc7->dwStencilCaps = wined3d_caps.StencilCaps;
4548 desc7->dwFVFCaps = wined3d_caps.FVFCaps;
4549 desc7->dwTextureOpCaps = wined3d_caps.TextureOpCaps;
4551 desc7->dwVertexProcessingCaps = wined3d_caps.VertexProcessingCaps;
4552 desc7->dwMaxActiveLights = wined3d_caps.MaxActiveLights;
4554 /* Remove all non-d3d7 caps */
4555 desc7->dwDevCaps &= (
4556 D3DDEVCAPS_FLOATTLVERTEX | D3DDEVCAPS_SORTINCREASINGZ | D3DDEVCAPS_SORTDECREASINGZ |
4557 D3DDEVCAPS_SORTEXACT | D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY |
4558 D3DDEVCAPS_TLVERTEXSYSTEMMEMORY | D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY |
4559 D3DDEVCAPS_TEXTUREVIDEOMEMORY | D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP |
4560 D3DDEVCAPS_TEXTURENONLOCALVIDMEM | D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
4561 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT | D3DDEVCAPS_CANBLTSYSTONONLOCAL |
4562 D3DDEVCAPS_HWRASTERIZATION);
4564 desc7->dwStencilCaps &= (
4565 D3DSTENCILCAPS_KEEP | D3DSTENCILCAPS_ZERO | D3DSTENCILCAPS_REPLACE |
4566 D3DSTENCILCAPS_INCRSAT | D3DSTENCILCAPS_DECRSAT | D3DSTENCILCAPS_INVERT |
4567 D3DSTENCILCAPS_INCR | D3DSTENCILCAPS_DECR);
4569 /* FVF caps ?*/
4571 desc7->dwTextureOpCaps &= (
4572 D3DTEXOPCAPS_DISABLE | D3DTEXOPCAPS_SELECTARG1 | D3DTEXOPCAPS_SELECTARG2 |
4573 D3DTEXOPCAPS_MODULATE | D3DTEXOPCAPS_MODULATE2X | D3DTEXOPCAPS_MODULATE4X |
4574 D3DTEXOPCAPS_ADD | D3DTEXOPCAPS_ADDSIGNED | D3DTEXOPCAPS_ADDSIGNED2X |
4575 D3DTEXOPCAPS_SUBTRACT | D3DTEXOPCAPS_ADDSMOOTH | D3DTEXOPCAPS_BLENDTEXTUREALPHA |
4576 D3DTEXOPCAPS_BLENDFACTORALPHA | D3DTEXOPCAPS_BLENDTEXTUREALPHAPM | D3DTEXOPCAPS_BLENDCURRENTALPHA |
4577 D3DTEXOPCAPS_PREMODULATE | D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
4578 D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA | D3DTEXOPCAPS_BUMPENVMAP |
4579 D3DTEXOPCAPS_BUMPENVMAPLUMINANCE | D3DTEXOPCAPS_DOTPRODUCT3);
4581 desc7->dwVertexProcessingCaps &= (
4582 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_VERTEXFOG |
4583 D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER);
4585 desc7->dpcLineCaps.dwMiscCaps &= (
4586 D3DPMISCCAPS_MASKPLANES | D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP |
4587 D3DPMISCCAPS_CONFORMANT | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW |
4588 D3DPMISCCAPS_CULLCCW);
4590 desc7->dpcLineCaps.dwRasterCaps &= (
4591 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ROP2 | D3DPRASTERCAPS_XOR |
4592 D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_SUBPIXEL |
4593 D3DPRASTERCAPS_SUBPIXELX | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE |
4594 D3DPRASTERCAPS_STIPPLE | D3DPRASTERCAPS_ANTIALIASSORTDEPENDENT | D3DPRASTERCAPS_ANTIALIASSORTINDEPENDENT |
4595 D3DPRASTERCAPS_ANTIALIASEDGES | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBIAS |
4596 D3DPRASTERCAPS_ZBUFFERLESSHSR | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY |
4597 D3DPRASTERCAPS_WBUFFER | D3DPRASTERCAPS_TRANSLUCENTSORTINDEPENDENT | D3DPRASTERCAPS_WFOG |
4598 D3DPRASTERCAPS_ZFOG);
4600 desc7->dpcLineCaps.dwZCmpCaps &= (
4601 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
4602 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
4603 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
4605 desc7->dpcLineCaps.dwSrcBlendCaps &= (
4606 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
4607 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
4608 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
4609 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
4610 D3DPBLENDCAPS_BOTHINVSRCALPHA);
4612 desc7->dpcLineCaps.dwDestBlendCaps &= (
4613 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
4614 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
4615 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
4616 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
4617 D3DPBLENDCAPS_BOTHINVSRCALPHA);
4619 desc7->dpcLineCaps.dwAlphaCmpCaps &= (
4620 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
4621 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
4622 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
4624 desc7->dpcLineCaps.dwShadeCaps &= (
4625 D3DPSHADECAPS_COLORFLATMONO | D3DPSHADECAPS_COLORFLATRGB | D3DPSHADECAPS_COLORGOURAUDMONO |
4626 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_COLORPHONGMONO | D3DPSHADECAPS_COLORPHONGRGB |
4627 D3DPSHADECAPS_SPECULARFLATMONO | D3DPSHADECAPS_SPECULARFLATRGB | D3DPSHADECAPS_SPECULARGOURAUDMONO |
4628 D3DPSHADECAPS_SPECULARGOURAUDRGB | D3DPSHADECAPS_SPECULARPHONGMONO | D3DPSHADECAPS_SPECULARPHONGRGB |
4629 D3DPSHADECAPS_ALPHAFLATBLEND | D3DPSHADECAPS_ALPHAFLATSTIPPLED | D3DPSHADECAPS_ALPHAGOURAUDBLEND |
4630 D3DPSHADECAPS_ALPHAGOURAUDSTIPPLED | D3DPSHADECAPS_ALPHAPHONGBLEND | D3DPSHADECAPS_ALPHAPHONGSTIPPLED |
4631 D3DPSHADECAPS_FOGFLAT | D3DPSHADECAPS_FOGGOURAUD | D3DPSHADECAPS_FOGPHONG);
4633 desc7->dpcLineCaps.dwTextureCaps &= (
4634 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
4635 D3DPTEXTURECAPS_TRANSPARENCY | D3DPTEXTURECAPS_BORDER | D3DPTEXTURECAPS_SQUAREONLY |
4636 D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE | D3DPTEXTURECAPS_ALPHAPALETTE| D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
4637 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_COLORKEYBLEND);
4639 desc7->dpcLineCaps.dwTextureFilterCaps &= (
4640 D3DPTFILTERCAPS_NEAREST | D3DPTFILTERCAPS_LINEAR | D3DPTFILTERCAPS_MIPNEAREST |
4641 D3DPTFILTERCAPS_MIPLINEAR | D3DPTFILTERCAPS_LINEARMIPNEAREST | D3DPTFILTERCAPS_LINEARMIPLINEAR |
4642 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
4643 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
4644 D3DPTFILTERCAPS_MAGFLINEAR | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC |
4645 D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC);
4647 desc7->dpcLineCaps.dwTextureAddressCaps &= (
4648 D3DPTADDRESSCAPS_WRAP | D3DPTADDRESSCAPS_MIRROR | D3DPTADDRESSCAPS_CLAMP |
4649 D3DPTADDRESSCAPS_BORDER | D3DPTADDRESSCAPS_INDEPENDENTUV);
4651 if (!(desc7->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2))
4653 /* DirectX7 always has the np2 flag set, no matter what the card
4654 * supports. Some old games (Rollcage) check the caps incorrectly.
4655 * If wined3d supports nonpow2 textures it also has np2 conditional
4656 * support. */
4657 desc7->dpcLineCaps.dwTextureCaps |= D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL;
4660 /* Fill the missing members, and do some fixup */
4661 desc7->dpcLineCaps.dwSize = sizeof(desc7->dpcLineCaps);
4662 desc7->dpcLineCaps.dwTextureBlendCaps = D3DPTBLENDCAPS_ADD | D3DPTBLENDCAPS_MODULATEMASK |
4663 D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_DECAL |
4664 D3DPTBLENDCAPS_DECALALPHA | D3DPTBLENDCAPS_DECALMASK |
4665 D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_MODULATEALPHA;
4666 desc7->dpcLineCaps.dwStippleWidth = 32;
4667 desc7->dpcLineCaps.dwStippleHeight = 32;
4668 /* Use the same for the TriCaps */
4669 desc7->dpcTriCaps = desc7->dpcLineCaps;
4671 desc7->dwDeviceRenderBitDepth = DDBD_16 | DDBD_24 | DDBD_32;
4672 desc7->dwDeviceZBufferBitDepth = DDBD_16 | DDBD_24;
4673 desc7->dwMinTextureWidth = 1;
4674 desc7->dwMinTextureHeight = 1;
4676 /* Convert DWORDs safely to WORDs */
4677 if (wined3d_caps.MaxTextureBlendStages > 0xffff) desc7->wMaxTextureBlendStages = 0xffff;
4678 else desc7->wMaxTextureBlendStages = (WORD)wined3d_caps.MaxTextureBlendStages;
4679 if (wined3d_caps.MaxSimultaneousTextures > 0xffff) desc7->wMaxSimultaneousTextures = 0xffff;
4680 else desc7->wMaxSimultaneousTextures = (WORD)wined3d_caps.MaxSimultaneousTextures;
4682 if (wined3d_caps.MaxUserClipPlanes > 0xffff) desc7->wMaxUserClipPlanes = 0xffff;
4683 else desc7->wMaxUserClipPlanes = (WORD)wined3d_caps.MaxUserClipPlanes;
4684 if (wined3d_caps.MaxVertexBlendMatrices > 0xffff) desc7->wMaxVertexBlendMatrices = 0xffff;
4685 else desc7->wMaxVertexBlendMatrices = (WORD)wined3d_caps.MaxVertexBlendMatrices;
4687 desc7->deviceGUID = IID_IDirect3DTnLHalDevice;
4689 desc7->dwReserved1 = 0;
4690 desc7->dwReserved2 = 0;
4691 desc7->dwReserved3 = 0;
4692 desc7->dwReserved4 = 0;
4694 /* Fill the old structure */
4695 memset(desc1, 0, sizeof(*desc1));
4696 desc1->dwSize = sizeof(D3DDEVICEDESC);
4697 desc1->dwFlags = D3DDD_COLORMODEL
4698 | D3DDD_DEVCAPS
4699 | D3DDD_TRANSFORMCAPS
4700 | D3DDD_BCLIPPING
4701 | D3DDD_LIGHTINGCAPS
4702 | D3DDD_LINECAPS
4703 | D3DDD_TRICAPS
4704 | D3DDD_DEVICERENDERBITDEPTH
4705 | D3DDD_DEVICEZBUFFERBITDEPTH
4706 | D3DDD_MAXBUFFERSIZE
4707 | D3DDD_MAXVERTEXCOUNT;
4709 desc1->dcmColorModel = D3DCOLOR_RGB;
4710 desc1->dwDevCaps = desc7->dwDevCaps;
4711 desc1->dtcTransformCaps.dwSize = sizeof(D3DTRANSFORMCAPS);
4712 desc1->dtcTransformCaps.dwCaps = D3DTRANSFORMCAPS_CLIP;
4713 desc1->bClipping = TRUE;
4714 desc1->dlcLightingCaps.dwSize = sizeof(D3DLIGHTINGCAPS);
4715 desc1->dlcLightingCaps.dwCaps = D3DLIGHTCAPS_DIRECTIONAL
4716 | D3DLIGHTCAPS_PARALLELPOINT
4717 | D3DLIGHTCAPS_POINT
4718 | D3DLIGHTCAPS_SPOT;
4720 desc1->dlcLightingCaps.dwLightingModel = D3DLIGHTINGMODEL_RGB;
4721 desc1->dlcLightingCaps.dwNumLights = desc7->dwMaxActiveLights;
4723 desc1->dpcLineCaps.dwSize = sizeof(D3DPRIMCAPS);
4724 desc1->dpcLineCaps.dwMiscCaps = desc7->dpcLineCaps.dwMiscCaps;
4725 desc1->dpcLineCaps.dwRasterCaps = desc7->dpcLineCaps.dwRasterCaps;
4726 desc1->dpcLineCaps.dwZCmpCaps = desc7->dpcLineCaps.dwZCmpCaps;
4727 desc1->dpcLineCaps.dwSrcBlendCaps = desc7->dpcLineCaps.dwSrcBlendCaps;
4728 desc1->dpcLineCaps.dwDestBlendCaps = desc7->dpcLineCaps.dwDestBlendCaps;
4729 desc1->dpcLineCaps.dwShadeCaps = desc7->dpcLineCaps.dwShadeCaps;
4730 desc1->dpcLineCaps.dwTextureCaps = desc7->dpcLineCaps.dwTextureCaps;
4731 desc1->dpcLineCaps.dwTextureFilterCaps = desc7->dpcLineCaps.dwTextureFilterCaps;
4732 desc1->dpcLineCaps.dwTextureBlendCaps = desc7->dpcLineCaps.dwTextureBlendCaps;
4733 desc1->dpcLineCaps.dwTextureAddressCaps = desc7->dpcLineCaps.dwTextureAddressCaps;
4734 desc1->dpcLineCaps.dwStippleWidth = desc7->dpcLineCaps.dwStippleWidth;
4735 desc1->dpcLineCaps.dwAlphaCmpCaps = desc7->dpcLineCaps.dwAlphaCmpCaps;
4737 desc1->dpcTriCaps.dwSize = sizeof(D3DPRIMCAPS);
4738 desc1->dpcTriCaps.dwMiscCaps = desc7->dpcTriCaps.dwMiscCaps;
4739 desc1->dpcTriCaps.dwRasterCaps = desc7->dpcTriCaps.dwRasterCaps;
4740 desc1->dpcTriCaps.dwZCmpCaps = desc7->dpcTriCaps.dwZCmpCaps;
4741 desc1->dpcTriCaps.dwSrcBlendCaps = desc7->dpcTriCaps.dwSrcBlendCaps;
4742 desc1->dpcTriCaps.dwDestBlendCaps = desc7->dpcTriCaps.dwDestBlendCaps;
4743 desc1->dpcTriCaps.dwShadeCaps = desc7->dpcTriCaps.dwShadeCaps;
4744 desc1->dpcTriCaps.dwTextureCaps = desc7->dpcTriCaps.dwTextureCaps;
4745 desc1->dpcTriCaps.dwTextureFilterCaps = desc7->dpcTriCaps.dwTextureFilterCaps;
4746 desc1->dpcTriCaps.dwTextureBlendCaps = desc7->dpcTriCaps.dwTextureBlendCaps;
4747 desc1->dpcTriCaps.dwTextureAddressCaps = desc7->dpcTriCaps.dwTextureAddressCaps;
4748 desc1->dpcTriCaps.dwStippleWidth = desc7->dpcTriCaps.dwStippleWidth;
4749 desc1->dpcTriCaps.dwAlphaCmpCaps = desc7->dpcTriCaps.dwAlphaCmpCaps;
4751 desc1->dwDeviceRenderBitDepth = desc7->dwDeviceRenderBitDepth;
4752 desc1->dwDeviceZBufferBitDepth = desc7->dwDeviceZBufferBitDepth;
4753 desc1->dwMaxBufferSize = 0;
4754 desc1->dwMaxVertexCount = 65536;
4755 desc1->dwMinTextureWidth = desc7->dwMinTextureWidth;
4756 desc1->dwMinTextureHeight = desc7->dwMinTextureHeight;
4757 desc1->dwMaxTextureWidth = desc7->dwMaxTextureWidth;
4758 desc1->dwMaxTextureHeight = desc7->dwMaxTextureHeight;
4759 desc1->dwMinStippleWidth = 1;
4760 desc1->dwMinStippleHeight = 1;
4761 desc1->dwMaxStippleWidth = 32;
4762 desc1->dwMaxStippleHeight = 32;
4763 desc1->dwMaxTextureRepeat = desc7->dwMaxTextureRepeat;
4764 desc1->dwMaxTextureAspectRatio = desc7->dwMaxTextureAspectRatio;
4765 desc1->dwMaxAnisotropy = desc7->dwMaxAnisotropy;
4766 desc1->dvGuardBandLeft = desc7->dvGuardBandLeft;
4767 desc1->dvGuardBandRight = desc7->dvGuardBandRight;
4768 desc1->dvGuardBandTop = desc7->dvGuardBandTop;
4769 desc1->dvGuardBandBottom = desc7->dvGuardBandBottom;
4770 desc1->dvExtentsAdjust = desc7->dvExtentsAdjust;
4771 desc1->dwStencilCaps = desc7->dwStencilCaps;
4772 desc1->dwFVFCaps = desc7->dwFVFCaps;
4773 desc1->dwTextureOpCaps = desc7->dwTextureOpCaps;
4774 desc1->wMaxTextureBlendStages = desc7->wMaxTextureBlendStages;
4775 desc1->wMaxSimultaneousTextures = desc7->wMaxSimultaneousTextures;
4777 return DD_OK;
4780 /*****************************************************************************
4781 * IDirectDraw7 VTable
4782 *****************************************************************************/
4783 static const struct IDirectDraw7Vtbl ddraw7_vtbl =
4785 /* IUnknown */
4786 ddraw7_QueryInterface,
4787 ddraw7_AddRef,
4788 ddraw7_Release,
4789 /* IDirectDraw */
4790 ddraw7_Compact,
4791 ddraw7_CreateClipper,
4792 ddraw7_CreatePalette,
4793 ddraw7_CreateSurface,
4794 ddraw7_DuplicateSurface,
4795 ddraw7_EnumDisplayModes,
4796 ddraw7_EnumSurfaces,
4797 ddraw7_FlipToGDISurface,
4798 ddraw7_GetCaps,
4799 ddraw7_GetDisplayMode,
4800 ddraw7_GetFourCCCodes,
4801 ddraw7_GetGDISurface,
4802 ddraw7_GetMonitorFrequency,
4803 ddraw7_GetScanLine,
4804 ddraw7_GetVerticalBlankStatus,
4805 ddraw7_Initialize,
4806 ddraw7_RestoreDisplayMode,
4807 ddraw7_SetCooperativeLevel,
4808 ddraw7_SetDisplayMode,
4809 ddraw7_WaitForVerticalBlank,
4810 /* IDirectDraw2 */
4811 ddraw7_GetAvailableVidMem,
4812 /* IDirectDraw3 */
4813 ddraw7_GetSurfaceFromDC,
4814 /* IDirectDraw4 */
4815 ddraw7_RestoreAllSurfaces,
4816 ddraw7_TestCooperativeLevel,
4817 ddraw7_GetDeviceIdentifier,
4818 /* IDirectDraw7 */
4819 ddraw7_StartModeTest,
4820 ddraw7_EvaluateMode
4823 static const struct IDirectDraw4Vtbl ddraw4_vtbl =
4825 /* IUnknown */
4826 ddraw4_QueryInterface,
4827 ddraw4_AddRef,
4828 ddraw4_Release,
4829 /* IDirectDraw */
4830 ddraw4_Compact,
4831 ddraw4_CreateClipper,
4832 ddraw4_CreatePalette,
4833 ddraw4_CreateSurface,
4834 ddraw4_DuplicateSurface,
4835 ddraw4_EnumDisplayModes,
4836 ddraw4_EnumSurfaces,
4837 ddraw4_FlipToGDISurface,
4838 ddraw4_GetCaps,
4839 ddraw4_GetDisplayMode,
4840 ddraw4_GetFourCCCodes,
4841 ddraw4_GetGDISurface,
4842 ddraw4_GetMonitorFrequency,
4843 ddraw4_GetScanLine,
4844 ddraw4_GetVerticalBlankStatus,
4845 ddraw4_Initialize,
4846 ddraw4_RestoreDisplayMode,
4847 ddraw4_SetCooperativeLevel,
4848 ddraw4_SetDisplayMode,
4849 ddraw4_WaitForVerticalBlank,
4850 /* IDirectDraw2 */
4851 ddraw4_GetAvailableVidMem,
4852 /* IDirectDraw3 */
4853 ddraw4_GetSurfaceFromDC,
4854 /* IDirectDraw4 */
4855 ddraw4_RestoreAllSurfaces,
4856 ddraw4_TestCooperativeLevel,
4857 ddraw4_GetDeviceIdentifier,
4860 static const struct IDirectDraw2Vtbl ddraw2_vtbl =
4862 /* IUnknown */
4863 ddraw2_QueryInterface,
4864 ddraw2_AddRef,
4865 ddraw2_Release,
4866 /* IDirectDraw */
4867 ddraw2_Compact,
4868 ddraw2_CreateClipper,
4869 ddraw2_CreatePalette,
4870 ddraw2_CreateSurface,
4871 ddraw2_DuplicateSurface,
4872 ddraw2_EnumDisplayModes,
4873 ddraw2_EnumSurfaces,
4874 ddraw2_FlipToGDISurface,
4875 ddraw2_GetCaps,
4876 ddraw2_GetDisplayMode,
4877 ddraw2_GetFourCCCodes,
4878 ddraw2_GetGDISurface,
4879 ddraw2_GetMonitorFrequency,
4880 ddraw2_GetScanLine,
4881 ddraw2_GetVerticalBlankStatus,
4882 ddraw2_Initialize,
4883 ddraw2_RestoreDisplayMode,
4884 ddraw2_SetCooperativeLevel,
4885 ddraw2_SetDisplayMode,
4886 ddraw2_WaitForVerticalBlank,
4887 /* IDirectDraw2 */
4888 ddraw2_GetAvailableVidMem,
4891 static const struct IDirectDrawVtbl ddraw1_vtbl =
4893 /* IUnknown */
4894 ddraw1_QueryInterface,
4895 ddraw1_AddRef,
4896 ddraw1_Release,
4897 /* IDirectDraw */
4898 ddraw1_Compact,
4899 ddraw1_CreateClipper,
4900 ddraw1_CreatePalette,
4901 ddraw1_CreateSurface,
4902 ddraw1_DuplicateSurface,
4903 ddraw1_EnumDisplayModes,
4904 ddraw1_EnumSurfaces,
4905 ddraw1_FlipToGDISurface,
4906 ddraw1_GetCaps,
4907 ddraw1_GetDisplayMode,
4908 ddraw1_GetFourCCCodes,
4909 ddraw1_GetGDISurface,
4910 ddraw1_GetMonitorFrequency,
4911 ddraw1_GetScanLine,
4912 ddraw1_GetVerticalBlankStatus,
4913 ddraw1_Initialize,
4914 ddraw1_RestoreDisplayMode,
4915 ddraw1_SetCooperativeLevel,
4916 ddraw1_SetDisplayMode,
4917 ddraw1_WaitForVerticalBlank,
4920 static const struct IDirect3D7Vtbl d3d7_vtbl =
4922 /* IUnknown methods */
4923 d3d7_QueryInterface,
4924 d3d7_AddRef,
4925 d3d7_Release,
4926 /* IDirect3D7 methods */
4927 d3d7_EnumDevices,
4928 d3d7_CreateDevice,
4929 d3d7_CreateVertexBuffer,
4930 d3d7_EnumZBufferFormats,
4931 d3d7_EvictManagedTextures
4934 static const struct IDirect3D3Vtbl d3d3_vtbl =
4936 /* IUnknown methods */
4937 d3d3_QueryInterface,
4938 d3d3_AddRef,
4939 d3d3_Release,
4940 /* IDirect3D3 methods */
4941 d3d3_EnumDevices,
4942 d3d3_CreateLight,
4943 d3d3_CreateMaterial,
4944 d3d3_CreateViewport,
4945 d3d3_FindDevice,
4946 d3d3_CreateDevice,
4947 d3d3_CreateVertexBuffer,
4948 d3d3_EnumZBufferFormats,
4949 d3d3_EvictManagedTextures
4952 static const struct IDirect3D2Vtbl d3d2_vtbl =
4954 /* IUnknown methods */
4955 d3d2_QueryInterface,
4956 d3d2_AddRef,
4957 d3d2_Release,
4958 /* IDirect3D2 methods */
4959 d3d2_EnumDevices,
4960 d3d2_CreateLight,
4961 d3d2_CreateMaterial,
4962 d3d2_CreateViewport,
4963 d3d2_FindDevice,
4964 d3d2_CreateDevice
4967 static const struct IDirect3DVtbl d3d1_vtbl =
4969 /* IUnknown methods */
4970 d3d1_QueryInterface,
4971 d3d1_AddRef,
4972 d3d1_Release,
4973 /* IDirect3D methods */
4974 d3d1_Initialize,
4975 d3d1_EnumDevices,
4976 d3d1_CreateLight,
4977 d3d1_CreateMaterial,
4978 d3d1_CreateViewport,
4979 d3d1_FindDevice
4982 /*****************************************************************************
4983 * ddraw_find_decl
4985 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
4986 * if none was found.
4988 * This function is in ddraw.c and the DDraw object space because D3D7
4989 * vertex buffers are created using the IDirect3D interface to the ddraw
4990 * object, so they can be valid across D3D devices(theoretically. The ddraw
4991 * object also owns the wined3d device
4993 * Parameters:
4994 * This: Device
4995 * fvf: Fvf to find the decl for
4997 * Returns:
4998 * NULL in case of an error, the vertex declaration for the FVF otherwise.
5000 *****************************************************************************/
5001 struct wined3d_vertex_declaration *ddraw_find_decl(struct ddraw *This, DWORD fvf)
5003 struct wined3d_vertex_declaration *pDecl = NULL;
5004 HRESULT hr;
5005 int p, low, high; /* deliberately signed */
5006 struct FvfToDecl *convertedDecls = This->decls;
5008 TRACE("Searching for declaration for fvf %08x... ", fvf);
5010 low = 0;
5011 high = This->numConvertedDecls - 1;
5012 while(low <= high) {
5013 p = (low + high) >> 1;
5014 TRACE("%d ", p);
5015 if(convertedDecls[p].fvf == fvf) {
5016 TRACE("found %p\n", convertedDecls[p].decl);
5017 return convertedDecls[p].decl;
5018 } else if(convertedDecls[p].fvf < fvf) {
5019 low = p + 1;
5020 } else {
5021 high = p - 1;
5024 TRACE("not found. Creating and inserting at position %d.\n", low);
5026 hr = wined3d_vertex_declaration_create_from_fvf(This->wined3d_device,
5027 fvf, This, &ddraw_null_wined3d_parent_ops, &pDecl);
5028 if (hr != S_OK) return NULL;
5030 if(This->declArraySize == This->numConvertedDecls) {
5031 int grow = max(This->declArraySize / 2, 8);
5032 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
5033 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
5034 if (!convertedDecls)
5036 wined3d_vertex_declaration_decref(pDecl);
5037 return NULL;
5039 This->decls = convertedDecls;
5040 This->declArraySize += grow;
5043 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
5044 convertedDecls[low].decl = pDecl;
5045 convertedDecls[low].fvf = fvf;
5046 This->numConvertedDecls++;
5048 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
5049 return pDecl;
5052 static inline struct ddraw *ddraw_from_device_parent(struct wined3d_device_parent *device_parent)
5054 return CONTAINING_RECORD(device_parent, struct ddraw, device_parent);
5057 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
5058 struct wined3d_device *device)
5060 TRACE("device_parent %p, device %p.\n", device_parent, device);
5063 /* This is run from device_process_message() in wined3d, we can't take the
5064 * wined3d mutex. */
5065 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
5067 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
5068 MONITORINFO monitor_info;
5069 HMONITOR monitor;
5070 RECT *r;
5072 TRACE("device_parent %p.\n", device_parent);
5074 if (!(ddraw->cooperative_level & DDSCL_EXCLUSIVE) || !ddraw->swapchain_window)
5076 TRACE("Nothing to resize.\n");
5077 return;
5080 monitor = MonitorFromWindow(ddraw->swapchain_window, MONITOR_DEFAULTTOPRIMARY);
5081 monitor_info.cbSize = sizeof(monitor_info);
5082 if (!GetMonitorInfoW(monitor, &monitor_info))
5084 ERR("Failed to get monitor info.\n");
5085 return;
5088 r = &monitor_info.rcMonitor;
5089 TRACE("Resizing window %p to %s.\n", ddraw->swapchain_window, wine_dbgstr_rect(r));
5091 if (!SetWindowPos(ddraw->swapchain_window, HWND_TOP, r->left, r->top,
5092 r->right - r->left, r->bottom - r->top, SWP_SHOWWINDOW | SWP_NOACTIVATE))
5093 ERR("Failed to resize window.\n");
5096 static HRESULT CDECL device_parent_create_texture_surface(struct wined3d_device_parent *device_parent,
5097 void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
5098 enum wined3d_pool pool, UINT sub_resource_idx, struct wined3d_surface **surface)
5100 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
5101 struct ddraw_surface *tex_root = container_parent;
5102 DDSURFACEDESC2 desc = tex_root->surface_desc;
5103 struct ddraw_surface *ddraw_surface;
5104 HRESULT hr;
5106 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
5107 "\tpool %#x, sub_resource_idx %u, surface %p.\n",
5108 device_parent, container_parent, width, height, format, usage, pool, sub_resource_idx, surface);
5110 /* The ddraw root surface is created before the wined3d texture. */
5111 if (!sub_resource_idx)
5113 ddraw_surface = tex_root;
5114 goto done;
5117 desc.dwWidth = width;
5118 desc.dwHeight = height;
5120 /* FIXME: Validate that format, usage, pool, etc. really make sense. */
5121 if (FAILED(hr = ddraw_create_surface(ddraw, &desc, &ddraw_surface, tex_root->version)))
5122 return hr;
5124 done:
5125 *surface = ddraw_surface->wined3d_surface;
5126 wined3d_surface_incref(*surface);
5128 return DD_OK;
5131 static void STDMETHODCALLTYPE ddraw_frontbuffer_destroyed(void *parent)
5133 struct ddraw *ddraw = parent;
5134 ddraw->wined3d_frontbuffer = NULL;
5137 static const struct wined3d_parent_ops ddraw_frontbuffer_parent_ops =
5139 ddraw_frontbuffer_destroyed,
5142 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
5143 void *container_parent, UINT width, UINT height, enum wined3d_format_id format_id, DWORD usage,
5144 enum wined3d_multisample_type multisample_type, DWORD multisample_quality, struct wined3d_surface **surface)
5146 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
5147 HRESULT hr;
5149 TRACE("device_parent %p, container_parent %p, width %u, height %u, format_id %#x, usage %#x,\n"
5150 "\tmultisample_type %#x, multisample_quality %u, surface %p.\n",
5151 device_parent, container_parent, width, height, format_id, usage,
5152 multisample_type, multisample_quality, surface);
5154 if (ddraw->wined3d_frontbuffer)
5156 ERR("Frontbuffer already created.\n");
5157 return E_FAIL;
5160 if (SUCCEEDED(hr = wined3d_surface_create(ddraw->wined3d_device, width, height, format_id, usage,
5161 WINED3D_POOL_DEFAULT, multisample_type, multisample_quality, WINED3D_SURFACE_MAPPABLE,
5162 ddraw, &ddraw_frontbuffer_parent_ops, surface)))
5163 ddraw->wined3d_frontbuffer = *surface;
5165 return hr;
5168 static HRESULT CDECL device_parent_create_volume(struct wined3d_device_parent *device_parent,
5169 void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
5170 enum wined3d_pool pool, DWORD usage, struct wined3d_volume **volume)
5172 TRACE("device_parent %p, container_parent %p, width %u, height %u, depth %u, "
5173 "format %#x, pool %#x, usage %#x, volume %p.\n",
5174 device_parent, container_parent, width, height, depth,
5175 format, pool, usage, volume);
5177 ERR("Not implemented!\n");
5179 return E_NOTIMPL;
5182 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
5183 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
5185 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
5186 HRESULT hr;
5188 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
5190 if (ddraw->wined3d_swapchain)
5192 ERR("Swapchain already created.\n");
5193 return E_FAIL;
5196 if (FAILED(hr = wined3d_swapchain_create(ddraw->wined3d_device, desc, NULL,
5197 &ddraw_null_wined3d_parent_ops, swapchain)))
5198 WARN("Failed to create swapchain, hr %#x.\n", hr);
5200 return hr;
5203 static const struct wined3d_device_parent_ops ddraw_wined3d_device_parent_ops =
5205 device_parent_wined3d_device_created,
5206 device_parent_mode_changed,
5207 device_parent_create_swapchain_surface,
5208 device_parent_create_texture_surface,
5209 device_parent_create_volume,
5210 device_parent_create_swapchain,
5213 HRESULT ddraw_init(struct ddraw *ddraw, enum wined3d_device_type device_type)
5215 WINED3DCAPS caps;
5216 DWORD flags;
5217 HRESULT hr;
5219 ddraw->IDirectDraw7_iface.lpVtbl = &ddraw7_vtbl;
5220 ddraw->IDirectDraw_iface.lpVtbl = &ddraw1_vtbl;
5221 ddraw->IDirectDraw2_iface.lpVtbl = &ddraw2_vtbl;
5222 ddraw->IDirectDraw4_iface.lpVtbl = &ddraw4_vtbl;
5223 ddraw->IDirect3D_iface.lpVtbl = &d3d1_vtbl;
5224 ddraw->IDirect3D2_iface.lpVtbl = &d3d2_vtbl;
5225 ddraw->IDirect3D3_iface.lpVtbl = &d3d3_vtbl;
5226 ddraw->IDirect3D7_iface.lpVtbl = &d3d7_vtbl;
5227 ddraw->device_parent.ops = &ddraw_wined3d_device_parent_ops;
5228 ddraw->numIfaces = 1;
5229 ddraw->ref7 = 1;
5231 flags = WINED3D_LEGACY_DEPTH_BIAS | WINED3D_VIDMEM_ACCOUNTING;
5232 if (!(ddraw->wined3d = wined3d_create(7, flags)))
5234 if (!(ddraw->wined3d = wined3d_create(7, flags | WINED3D_NO3D)))
5236 WARN("Failed to create a wined3d object.\n");
5237 return E_FAIL;
5241 if (FAILED(hr = wined3d_get_device_caps(ddraw->wined3d, WINED3DADAPTER_DEFAULT, device_type, &caps)))
5243 ERR("Failed to get device caps, hr %#x.\n", hr);
5244 wined3d_decref(ddraw->wined3d);
5245 return E_FAIL;
5248 if (!(caps.ddraw_caps.caps & WINEDDCAPS_3D))
5250 WARN("Created a wined3d object without 3D support.\n");
5251 ddraw->flags |= DDRAW_NO3D;
5254 hr = wined3d_device_create(ddraw->wined3d, WINED3DADAPTER_DEFAULT, device_type,
5255 NULL, 0, 8, &ddraw->device_parent, &ddraw->wined3d_device);
5256 if (FAILED(hr))
5258 WARN("Failed to create a wined3d device, hr %#x.\n", hr);
5259 wined3d_decref(ddraw->wined3d);
5260 return hr;
5263 list_init(&ddraw->surface_list);
5265 return DD_OK;