mfreadwrite/reader: Add missing allocation check (Coverity).
[wine/zf.git] / dlls / ddraw / ddraw.c
blob5582e9862a1eefa733ff65bc721f474fdd33e2db
1 /*
2 * Copyright 1997-2000 Marcus Meissner
3 * Copyright 1998-2000 Lionel Ulmer
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2006 Stefan Dösinger
6 * Copyright 2008 Denver Gingerich
7 * Copyright 2007-2008, 2011, 2013 Stefan Dösinger for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "ddraw_private.h"
25 #include "ddrawi.h"
26 #include "d3dhal.h"
28 #include "wine/exception.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
32 static const struct ddraw *exclusive_ddraw;
33 static HWND exclusive_window;
35 /* Device identifier. Don't relay it to WineD3D */
36 static const DDDEVICEIDENTIFIER2 deviceidentifier =
38 "vga.dll", /* default 2D driver */
39 "DirectDraw HAL",
40 { { 0x00010001, 0x00010001 } },
41 0, 0, 0, 0,
42 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
43 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
47 static struct enum_device_entry
49 char interface_name[100];
50 char device_name[100];
51 const GUID *device_guid;
52 } device_list7[] =
54 /* T&L HAL device */
56 "WINE Direct3D7 Hardware Transform and Lighting acceleration using WineD3D",
57 "Wine D3D7 T&L HAL",
58 &IID_IDirect3DTnLHalDevice,
61 /* HAL device */
63 "WINE Direct3D7 Hardware acceleration using WineD3D",
64 "Direct3D HAL",
65 &IID_IDirect3DHALDevice,
68 /* RGB device */
70 "WINE Direct3D7 RGB Software Emulation using WineD3D",
71 "Wine D3D7 RGB",
72 &IID_IDirect3DRGBDevice,
76 static void STDMETHODCALLTYPE ddraw_null_wined3d_object_destroyed(void *parent) {}
78 const struct wined3d_parent_ops ddraw_null_wined3d_parent_ops =
80 ddraw_null_wined3d_object_destroyed,
83 static void CDECL ddraw_swapchain_windowed_state_changed(struct wined3d_swapchain_state_parent *parent,
84 BOOL windowed)
86 TRACE("parent %p, windowed %d.\n", parent, windowed);
89 static const struct wined3d_swapchain_state_parent_ops ddraw_swapchain_state_parent_ops =
91 ddraw_swapchain_windowed_state_changed,
94 static inline struct ddraw *impl_from_IDirectDraw(IDirectDraw *iface)
96 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw_iface);
99 static inline struct ddraw *impl_from_IDirectDraw2(IDirectDraw2 *iface)
101 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw2_iface);
104 static inline struct ddraw *impl_from_IDirectDraw4(IDirectDraw4 *iface)
106 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw4_iface);
109 static inline struct ddraw *impl_from_IDirectDraw7(IDirectDraw7 *iface)
111 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw7_iface);
114 static inline struct ddraw *impl_from_IDirect3D(IDirect3D *iface)
116 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D_iface);
119 static inline struct ddraw *impl_from_IDirect3D2(IDirect3D2 *iface)
121 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D2_iface);
124 static inline struct ddraw *impl_from_IDirect3D3(IDirect3D3 *iface)
126 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D3_iface);
129 static inline struct ddraw *impl_from_IDirect3D7(IDirect3D7 *iface)
131 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D7_iface);
134 static HRESULT WINAPI ddraw7_QueryInterface(IDirectDraw7 *iface, REFIID riid, void **out)
136 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
138 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
140 if (!riid)
142 *out = NULL;
143 return DDERR_INVALIDPARAMS;
146 /* The refcount unit test revealed that an IDirect3D7 interface can only
147 * be queried from a DirectDraw object that was created as an IDirectDraw7
148 * interface. The older interfaces can query any IDirect3D version except
149 * 7, because they are all initially created as IDirectDraw. This isn't
150 * really crucial behavior, and messy to implement with the common
151 * creation function, so it has been left out here. */
152 if (IsEqualGUID(&IID_IDirectDraw7, riid)
153 || IsEqualGUID(&IID_IUnknown, riid))
155 *out = &ddraw->IDirectDraw7_iface;
156 TRACE("Returning IDirectDraw7 interface %p.\n", *out);
158 else if (IsEqualGUID(&IID_IDirectDraw4, riid))
160 *out = &ddraw->IDirectDraw4_iface;
161 TRACE("Returning IDirectDraw4 interface %p.\n", *out);
163 else if (IsEqualGUID(&IID_IDirectDraw2, riid))
165 *out = &ddraw->IDirectDraw2_iface;
166 TRACE("Returning IDirectDraw2 interface %p.\n", *out);
168 else if (IsEqualGUID(&IID_IDirectDraw, riid))
170 *out = &ddraw->IDirectDraw_iface;
171 TRACE("Returning IDirectDraw interface %p.\n", *out);
173 else if (IsEqualGUID(&IID_IDirect3D7, riid))
175 ddraw->d3dversion = 7;
176 *out = &ddraw->IDirect3D7_iface;
177 TRACE("Returning Direct3D7 interface %p.\n", *out);
179 else if (IsEqualGUID(&IID_IDirect3D3, riid))
181 ddraw->d3dversion = 3;
182 *out = &ddraw->IDirect3D3_iface;
183 TRACE("Returning Direct3D3 interface %p.\n", *out);
185 else if (IsEqualGUID(&IID_IDirect3D2, riid))
187 ddraw->d3dversion = 2;
188 *out = &ddraw->IDirect3D2_iface;
189 TRACE("Returning Direct3D2 interface %p.\n", *out);
191 else if (IsEqualGUID(&IID_IDirect3D, riid))
193 ddraw->d3dversion = 1;
194 *out = &ddraw->IDirect3D_iface;
195 TRACE("Returning Direct3D interface %p.\n", *out);
197 /* Unknown interface */
198 else
200 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
201 *out = NULL;
202 return E_NOINTERFACE;
205 IUnknown_AddRef((IUnknown *)*out);
206 return S_OK;
209 static HRESULT WINAPI ddraw4_QueryInterface(IDirectDraw4 *iface, REFIID riid, void **object)
211 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
213 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
215 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
218 static HRESULT WINAPI ddraw2_QueryInterface(IDirectDraw2 *iface, REFIID riid, void **object)
220 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
222 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
224 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
227 static HRESULT WINAPI ddraw1_QueryInterface(IDirectDraw *iface, REFIID riid, void **object)
229 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
231 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
233 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
236 static HRESULT WINAPI d3d7_QueryInterface(IDirect3D7 *iface, REFIID riid, void **object)
238 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
240 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
242 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
245 static HRESULT WINAPI d3d3_QueryInterface(IDirect3D3 *iface, REFIID riid, void **object)
247 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
249 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
251 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
254 static HRESULT WINAPI d3d2_QueryInterface(IDirect3D2 *iface, REFIID riid, void **object)
256 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
258 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
260 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
263 static HRESULT WINAPI d3d1_QueryInterface(IDirect3D *iface, REFIID riid, void **object)
265 struct ddraw *ddraw = impl_from_IDirect3D(iface);
267 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
269 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
272 /*****************************************************************************
273 * IDirectDraw7::AddRef
275 * Increases the interfaces refcount, basically
277 * DDraw refcounting is a bit tricky. The different DirectDraw interface
278 * versions have individual refcounts, but the IDirect3D interfaces do not.
279 * All interfaces are from one object, that means calling QueryInterface on an
280 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
281 * ddraw object.
283 * That means all AddRef and Release implementations of IDirectDrawX work
284 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
285 * except of IDirect3D7 which thunks to IDirectDraw7
287 * Returns: The new refcount
289 *****************************************************************************/
290 static ULONG WINAPI ddraw7_AddRef(IDirectDraw7 *iface)
292 struct ddraw *This = impl_from_IDirectDraw7(iface);
293 ULONG ref = InterlockedIncrement(&This->ref7);
295 TRACE("%p increasing refcount to %u.\n", This, ref);
297 if(ref == 1) InterlockedIncrement(&This->numIfaces);
299 return ref;
302 static ULONG WINAPI ddraw4_AddRef(IDirectDraw4 *iface)
304 struct ddraw *This = impl_from_IDirectDraw4(iface);
305 ULONG ref = InterlockedIncrement(&This->ref4);
307 TRACE("%p increasing refcount to %u.\n", This, ref);
309 if (ref == 1) InterlockedIncrement(&This->numIfaces);
311 return ref;
314 static ULONG WINAPI ddraw2_AddRef(IDirectDraw2 *iface)
316 struct ddraw *This = impl_from_IDirectDraw2(iface);
317 ULONG ref = InterlockedIncrement(&This->ref2);
319 TRACE("%p increasing refcount to %u.\n", This, ref);
321 if (ref == 1) InterlockedIncrement(&This->numIfaces);
323 return ref;
326 static ULONG WINAPI ddraw1_AddRef(IDirectDraw *iface)
328 struct ddraw *This = impl_from_IDirectDraw(iface);
329 ULONG ref = InterlockedIncrement(&This->ref1);
331 TRACE("%p increasing refcount to %u.\n", This, ref);
333 if (ref == 1) InterlockedIncrement(&This->numIfaces);
335 return ref;
338 static ULONG WINAPI d3d7_AddRef(IDirect3D7 *iface)
340 struct ddraw *This = impl_from_IDirect3D7(iface);
342 TRACE("iface %p.\n", iface);
344 return ddraw7_AddRef(&This->IDirectDraw7_iface);
347 static ULONG WINAPI d3d3_AddRef(IDirect3D3 *iface)
349 struct ddraw *This = impl_from_IDirect3D3(iface);
351 TRACE("iface %p.\n", iface);
353 return ddraw1_AddRef(&This->IDirectDraw_iface);
356 static ULONG WINAPI d3d2_AddRef(IDirect3D2 *iface)
358 struct ddraw *This = impl_from_IDirect3D2(iface);
360 TRACE("iface %p.\n", iface);
362 return ddraw1_AddRef(&This->IDirectDraw_iface);
365 static ULONG WINAPI d3d1_AddRef(IDirect3D *iface)
367 struct ddraw *This = impl_from_IDirect3D(iface);
369 TRACE("iface %p.\n", iface);
371 return ddraw1_AddRef(&This->IDirectDraw_iface);
374 static void ddraw_destroy_swapchain(struct ddraw *ddraw)
376 unsigned int i;
378 TRACE("Destroying the swapchain.\n");
380 wined3d_swapchain_decref(ddraw->wined3d_swapchain);
382 for (i = 0; i < ddraw->numConvertedDecls; ++i)
384 wined3d_vertex_declaration_decref(ddraw->decls[i].decl);
386 heap_free(ddraw->decls);
387 ddraw->numConvertedDecls = 0;
389 wined3d_swapchain_decref(ddraw->wined3d_swapchain);
390 ddraw->wined3d_swapchain = NULL;
392 /* Free the d3d window if one was created. */
393 if (ddraw->d3d_window && ddraw->d3d_window != ddraw->dest_window)
395 TRACE("Destroying the hidden render window %p.\n", ddraw->d3d_window);
396 DestroyWindow(ddraw->d3d_window);
397 ddraw->d3d_window = 0;
400 ddraw->flags &= ~DDRAW_D3D_INITIALIZED;
402 ddraw_set_swapchain_window(ddraw, NULL);
404 TRACE("Swapchain destroyed.\n");
407 /*****************************************************************************
408 * ddraw_destroy
410 * Destroys a ddraw object if all refcounts are 0. This is to share code
411 * between the IDirectDrawX::Release functions
413 * Params:
414 * This: DirectDraw object to destroy
416 *****************************************************************************/
417 static void ddraw_destroy(struct ddraw *This)
419 IDirectDraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, NULL, DDSCL_NORMAL);
420 IDirectDraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
422 /* Destroy the device window if we created one */
423 if(This->devicewindow != 0)
425 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
426 DestroyWindow(This->devicewindow);
427 This->devicewindow = 0;
430 wined3d_mutex_lock();
431 list_remove(&This->ddraw_list_entry);
432 wined3d_mutex_unlock();
434 if (This->wined3d_swapchain)
435 ddraw_destroy_swapchain(This);
436 wined3d_stateblock_decref(This->state);
437 wined3d_device_decref(This->wined3d_device);
438 wined3d_decref(This->wined3d);
440 if (This->d3ddevice)
441 This->d3ddevice->ddraw = NULL;
443 /* Now free the object */
444 heap_free(This);
447 /*****************************************************************************
448 * IDirectDraw7::Release
450 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
452 * Returns: The new refcount
453 *****************************************************************************/
454 static ULONG WINAPI ddraw7_Release(IDirectDraw7 *iface)
456 struct ddraw *This = impl_from_IDirectDraw7(iface);
457 ULONG ref = InterlockedDecrement(&This->ref7);
459 TRACE("%p decreasing refcount to %u.\n", This, ref);
461 if (!ref && !InterlockedDecrement(&This->numIfaces))
462 ddraw_destroy(This);
464 return ref;
467 static ULONG WINAPI ddraw4_Release(IDirectDraw4 *iface)
469 struct ddraw *This = impl_from_IDirectDraw4(iface);
470 ULONG ref = InterlockedDecrement(&This->ref4);
472 TRACE("%p decreasing refcount to %u.\n", This, ref);
474 if (!ref && !InterlockedDecrement(&This->numIfaces))
475 ddraw_destroy(This);
477 return ref;
480 static ULONG WINAPI ddraw2_Release(IDirectDraw2 *iface)
482 struct ddraw *This = impl_from_IDirectDraw2(iface);
483 ULONG ref = InterlockedDecrement(&This->ref2);
485 TRACE("%p decreasing refcount to %u.\n", This, ref);
487 if (!ref && !InterlockedDecrement(&This->numIfaces))
488 ddraw_destroy(This);
490 return ref;
493 static ULONG WINAPI ddraw1_Release(IDirectDraw *iface)
495 struct ddraw *This = impl_from_IDirectDraw(iface);
496 ULONG ref = InterlockedDecrement(&This->ref1);
498 TRACE("%p decreasing refcount to %u.\n", This, ref);
500 if (!ref && !InterlockedDecrement(&This->numIfaces))
501 ddraw_destroy(This);
503 return ref;
506 static ULONG WINAPI d3d7_Release(IDirect3D7 *iface)
508 struct ddraw *This = impl_from_IDirect3D7(iface);
510 TRACE("iface %p.\n", iface);
512 return ddraw7_Release(&This->IDirectDraw7_iface);
515 static ULONG WINAPI d3d3_Release(IDirect3D3 *iface)
517 struct ddraw *This = impl_from_IDirect3D3(iface);
519 TRACE("iface %p.\n", iface);
521 return ddraw1_Release(&This->IDirectDraw_iface);
524 static ULONG WINAPI d3d2_Release(IDirect3D2 *iface)
526 struct ddraw *This = impl_from_IDirect3D2(iface);
528 TRACE("iface %p.\n", iface);
530 return ddraw1_Release(&This->IDirectDraw_iface);
533 static ULONG WINAPI d3d1_Release(IDirect3D *iface)
535 struct ddraw *This = impl_from_IDirect3D(iface);
537 TRACE("iface %p.\n", iface);
539 return ddraw1_Release(&This->IDirectDraw_iface);
542 /*****************************************************************************
543 * IDirectDraw methods
544 *****************************************************************************/
546 static HRESULT ddraw_set_focus_window(struct ddraw *ddraw, HWND window)
548 /* FIXME: This looks wrong, exclusive mode should imply a destination
549 * window. */
550 if ((ddraw->cooperative_level & DDSCL_EXCLUSIVE) && ddraw->dest_window)
552 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET.\n");
553 return DDERR_HWNDALREADYSET;
556 ddraw->focuswindow = window;
558 return DD_OK;
561 static HRESULT ddraw_attach_d3d_device(struct ddraw *ddraw, HWND window,
562 BOOL windowed, struct wined3d_swapchain **wined3d_swapchain)
564 struct wined3d_swapchain_desc swapchain_desc;
565 struct wined3d_display_mode mode;
566 HRESULT hr;
568 TRACE("ddraw %p.\n", ddraw);
570 if (FAILED(hr = wined3d_output_get_display_mode(ddraw->wined3d_output, &mode, NULL)))
572 ERR("Failed to get display mode.\n");
573 return hr;
576 memset(&swapchain_desc, 0, sizeof(swapchain_desc));
577 swapchain_desc.output = ddraw->wined3d_output;
578 swapchain_desc.backbuffer_width = mode.width;
579 swapchain_desc.backbuffer_height = mode.height;
580 swapchain_desc.backbuffer_format = mode.format_id;
581 swapchain_desc.backbuffer_bind_flags = 0;
582 swapchain_desc.backbuffer_count = 1;
583 swapchain_desc.swap_effect = WINED3D_SWAP_EFFECT_DISCARD;
584 swapchain_desc.device_window = window;
585 swapchain_desc.windowed = windowed;
586 swapchain_desc.flags = WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH
587 | WINED3D_SWAPCHAIN_IMPLICIT | WINED3D_SWAPCHAIN_NO_WINDOW_CHANGES;
589 if (ddraw->flags & DDRAW_NO3D)
590 return wined3d_swapchain_create(ddraw->wined3d_device, &swapchain_desc,
591 &ddraw->state_parent, NULL, &ddraw_null_wined3d_parent_ops, wined3d_swapchain);
593 if (!window || window == GetDesktopWindow())
595 window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "Hidden D3D Window",
596 WS_DISABLED, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
597 NULL, NULL, NULL, NULL);
598 if (!window)
600 ERR("Failed to create window, last error %#x.\n", GetLastError());
601 return E_FAIL;
604 ShowWindow(window, SW_HIDE); /* Just to be sure */
605 WARN("No window for the Direct3DDevice, created hidden window %p.\n", window);
607 swapchain_desc.device_window = window;
609 else
611 TRACE("Using existing window %p for Direct3D rendering.\n", window);
613 ddraw->d3d_window = window;
615 /* Set this NOW, otherwise creating the depth stencil surface will cause a
616 * recursive loop until ram or emulated video memory is full. */
617 ddraw->flags |= DDRAW_D3D_INITIALIZED;
618 if (FAILED(hr = wined3d_swapchain_create(ddraw->wined3d_device, &swapchain_desc,
619 &ddraw->state_parent, NULL, &ddraw_null_wined3d_parent_ops, wined3d_swapchain)))
621 ddraw->flags &= ~DDRAW_D3D_INITIALIZED;
622 DestroyWindow(window);
623 ddraw->d3d_window = NULL;
624 return hr;
627 ddraw->declArraySize = 2;
628 if (!(ddraw->decls = heap_alloc_zero(ddraw->declArraySize * sizeof(*ddraw->decls))))
630 ERR("Error allocating an array for the converted vertex decls.\n");
631 ddraw->declArraySize = 0;
632 wined3d_swapchain_decref(*wined3d_swapchain);
633 DestroyWindow(window);
634 ddraw->d3d_window = NULL;
635 return E_OUTOFMEMORY;
638 TRACE("Successfully initialized 3D.\n");
640 return DD_OK;
643 static HRESULT ddraw_create_swapchain(struct ddraw *ddraw, HWND window, BOOL windowed)
645 HRESULT hr;
647 if (ddraw->wined3d_swapchain)
649 ERR("Swapchain already created.\n");
650 return E_FAIL;
653 if (FAILED(hr = ddraw_attach_d3d_device(ddraw, window, windowed, &ddraw->wined3d_swapchain)))
655 ERR("Failed to create swapchain, hr %#x.\n", hr);
656 return hr;
658 wined3d_swapchain_incref(ddraw->wined3d_swapchain);
660 ddraw_set_swapchain_window(ddraw, window);
662 if (ddraw->primary && ddraw->primary->palette)
663 wined3d_swapchain_set_palette(ddraw->wined3d_swapchain, ddraw->primary->palette->wined3d_palette);
665 return DD_OK;
668 /*****************************************************************************
669 * IDirectDraw7::RestoreDisplayMode
671 * Restores the display mode to what it was at creation time. Basically.
673 * Returns
674 * DD_OK on success
675 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
677 *****************************************************************************/
678 static HRESULT WINAPI ddraw7_RestoreDisplayMode(IDirectDraw7 *iface)
680 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
681 struct wined3d_display_mode mode;
682 RECT clip_rect;
683 HRESULT hr;
685 TRACE("iface %p.\n", iface);
687 wined3d_mutex_lock();
689 if (!(ddraw->flags & DDRAW_RESTORE_MODE))
691 wined3d_mutex_unlock();
692 return DD_OK;
695 if (exclusive_ddraw && exclusive_ddraw != ddraw)
697 wined3d_mutex_unlock();
698 return DDERR_NOEXCLUSIVEMODE;
701 if (SUCCEEDED(hr = wined3d_restore_display_modes(ddraw->wined3d)))
703 ddraw->flags &= ~DDRAW_RESTORE_MODE;
704 if (ddraw->cooperative_level & DDSCL_EXCLUSIVE &&
705 SUCCEEDED(hr = wined3d_output_get_display_mode(ddraw->wined3d_output, &mode, NULL)))
707 SetRect(&clip_rect, 0, 0, mode.width, mode.height);
708 ClipCursor(&clip_rect);
712 InterlockedCompareExchange(&ddraw->device_state, DDRAW_DEVICE_STATE_NOT_RESTORED, DDRAW_DEVICE_STATE_OK);
714 wined3d_mutex_unlock();
716 return hr;
719 static HRESULT WINAPI ddraw4_RestoreDisplayMode(IDirectDraw4 *iface)
721 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
723 TRACE("iface %p.\n", iface);
725 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
728 static HRESULT WINAPI ddraw2_RestoreDisplayMode(IDirectDraw2 *iface)
730 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
732 TRACE("iface %p.\n", iface);
734 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
737 static HRESULT WINAPI ddraw1_RestoreDisplayMode(IDirectDraw *iface)
739 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
741 TRACE("iface %p.\n", iface);
743 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
746 /*****************************************************************************
747 * IDirectDraw7::SetCooperativeLevel
749 * Sets the cooperative level for the DirectDraw object, and the window
750 * assigned to it. The cooperative level determines the general behavior
751 * of the DirectDraw application
753 * Warning: This is quite tricky, as it's not really documented which
754 * cooperative levels can be combined with each other. If a game fails
755 * after this function, try to check the cooperative levels passed on
756 * Windows, and if it returns something different.
758 * If you think that this function caused the failure because it writes a
759 * fixme, be sure to run again with a +ddraw trace.
761 * What is known about cooperative levels (See the ddraw modes test):
762 * DDSCL_EXCLUSIVE requires DDSCL_FULLSCREEN.
763 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE.
764 * Unlike what msdn claims, DDSCL_NORMAL | DDSCL_FULLSCREEN is allowed.
765 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
766 * DDSCL_EXCLUSIVE can be activated.
767 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES or
768 * DDSCL_CREATEDEVICEWINDOW.
770 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
771 * DDSCL_CREATEDEVICEWINDOW, DDSCL_SETDEVICEWINDOW
772 * DDSCL_SETFOCUSWINDOW (partially),
773 * DDSCL_MULTITHREADED (work in progress)
774 * DDSCL_FPUPRESERVE (see device.c)
776 * Unsure about this: DDSCL_FPUSETUP
778 * These don't seem very important for wine:
779 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
781 * Returns:
782 * DD_OK if the cooperative level was set successfully
783 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
784 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
785 * (Probably others too, have to investigate)
787 *****************************************************************************/
788 static HRESULT ddraw_set_cooperative_level(struct ddraw *ddraw, HWND window,
789 DWORD cooplevel, BOOL restore_mode_on_normal)
791 struct wined3d_rendertarget_view *rtv = NULL, *dsv = NULL;
792 struct wined3d_stateblock *stateblock;
793 BOOL restore_state = FALSE;
794 RECT clip_rect;
795 HRESULT hr;
797 TRACE("ddraw %p, window %p, flags %#x, restore_mode_on_normal %x.\n", ddraw, window, cooplevel,
798 restore_mode_on_normal);
799 DDRAW_dump_cooperativelevel(cooplevel);
801 wined3d_mutex_lock();
803 if (ddraw->flags & DDRAW_SCL_RECURSIVE)
805 WARN("Recursive call, returning DD_OK.\n");
806 hr = DD_OK;
807 goto done;
809 ddraw->flags |= DDRAW_SCL_RECURSIVE;
811 /* Tests suggest that we need one of them: */
812 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
813 DDSCL_NORMAL |
814 DDSCL_EXCLUSIVE )))
816 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
817 hr = DDERR_INVALIDPARAMS;
818 goto done;
821 if ((cooplevel & DDSCL_CREATEDEVICEWINDOW) && !(cooplevel & DDSCL_EXCLUSIVE))
823 WARN("DDSCL_CREATEDEVICEWINDOW requires DDSCL_EXCLUSIVE.\n");
824 hr = DDERR_INVALIDPARAMS;
825 goto done;
828 /* Handle those levels first which set various hwnds */
829 if ((cooplevel & DDSCL_SETFOCUSWINDOW) && !(cooplevel & DDSCL_CREATEDEVICEWINDOW))
831 /* This isn't compatible with a lot of flags */
832 if (cooplevel & (DDSCL_MULTITHREADED
833 | DDSCL_FPUSETUP
834 | DDSCL_FPUPRESERVE
835 | DDSCL_ALLOWREBOOT
836 | DDSCL_ALLOWMODEX
837 | DDSCL_SETDEVICEWINDOW
838 | DDSCL_NORMAL
839 | DDSCL_EXCLUSIVE
840 | DDSCL_FULLSCREEN))
842 WARN("Called with incompatible flags, returning DDERR_INVALIDPARAMS.\n");
843 hr = DDERR_INVALIDPARAMS;
844 goto done;
847 hr = ddraw_set_focus_window(ddraw, window);
848 goto done;
851 if (cooplevel & DDSCL_EXCLUSIVE)
853 if (!(cooplevel & DDSCL_FULLSCREEN) || !(window || (cooplevel & DDSCL_CREATEDEVICEWINDOW)))
855 WARN("DDSCL_EXCLUSIVE requires DDSCL_FULLSCREEN and a window.\n");
856 hr = DDERR_INVALIDPARAMS;
857 goto done;
860 if (cooplevel & DDSCL_CREATEDEVICEWINDOW)
862 HWND device_window;
864 if (!ddraw->focuswindow && !(cooplevel & DDSCL_SETFOCUSWINDOW))
866 WARN("No focus window set.\n");
867 hr = DDERR_NOFOCUSWINDOW;
868 goto done;
871 device_window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "DirectDrawDeviceWnd",
872 WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
873 NULL, NULL, NULL, NULL);
874 if (!device_window)
876 ERR("Failed to create window, last error %#x.\n", GetLastError());
877 hr = E_FAIL;
878 goto done;
881 ShowWindow(device_window, SW_SHOW);
882 TRACE("Created a device window %p.\n", device_window);
884 /* Native apparently leaks the created device window if setting the
885 * focus window below fails. */
886 ddraw->cooperative_level |= DDSCL_CREATEDEVICEWINDOW;
887 ddraw->devicewindow = device_window;
889 if (cooplevel & DDSCL_SETFOCUSWINDOW)
891 if (!window)
893 hr = DDERR_NOHWND;
894 goto done;
897 if (FAILED(hr = ddraw_set_focus_window(ddraw, window)))
898 goto done;
901 window = device_window;
904 else
906 if (ddraw->cooperative_level & DDSCL_CREATEDEVICEWINDOW)
907 DestroyWindow(ddraw->devicewindow);
908 ddraw->devicewindow = NULL;
909 ddraw->focuswindow = NULL;
912 if ((cooplevel & DDSCL_EXCLUSIVE) && exclusive_window != window)
914 ddraw->device_state = DDRAW_DEVICE_STATE_NOT_RESTORED;
915 exclusive_window = window;
918 if (cooplevel & DDSCL_MULTITHREADED && !(ddraw->cooperative_level & DDSCL_MULTITHREADED))
919 wined3d_device_set_multithreaded(ddraw->wined3d_device);
921 if (ddraw->wined3d_swapchain)
923 if (!(ddraw->flags & DDRAW_NO3D))
925 restore_state = TRUE;
927 if (FAILED(hr = wined3d_stateblock_create(ddraw->wined3d_device,
928 ddraw->state, WINED3D_SBT_ALL, &stateblock)))
930 ERR("Failed to create stateblock, hr %#x.\n", hr);
931 goto done;
934 rtv = wined3d_device_get_rendertarget_view(ddraw->wined3d_device, 0);
935 /* Rendering to ddraw->wined3d_frontbuffer. */
936 if (rtv && !wined3d_rendertarget_view_get_sub_resource_parent(rtv))
937 rtv = NULL;
938 else if (rtv)
939 wined3d_rendertarget_view_incref(rtv);
941 if ((dsv = wined3d_device_get_depth_stencil_view(ddraw->wined3d_device)))
942 wined3d_rendertarget_view_incref(dsv);
945 ddraw_destroy_swapchain(ddraw);
948 if (FAILED(hr = ddraw_create_swapchain(ddraw, window, !(cooplevel & DDSCL_FULLSCREEN))))
949 ERR("Failed to create swapchain, hr %#x.\n", hr);
951 if (restore_state)
953 if (dsv)
955 wined3d_device_context_set_depth_stencil_view(ddraw->immediate_context, dsv);
956 wined3d_rendertarget_view_decref(dsv);
959 if (rtv)
961 wined3d_device_context_set_rendertarget_view(ddraw->immediate_context, 0, rtv, FALSE);
962 wined3d_rendertarget_view_decref(rtv);
965 wined3d_stateblock_apply(stateblock, ddraw->state);
966 wined3d_stateblock_decref(stateblock);
969 if (!(cooplevel & DDSCL_EXCLUSIVE) && (ddraw->cooperative_level & DDSCL_EXCLUSIVE))
971 if (restore_mode_on_normal && FAILED(ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface)))
972 ERR("RestoreDisplayMode failed\n");
973 ClipCursor(NULL);
976 if ((ddraw->cooperative_level & DDSCL_EXCLUSIVE)
977 && (window != ddraw->dest_window || !(cooplevel & DDSCL_EXCLUSIVE)))
978 wined3d_device_release_focus_window(ddraw->wined3d_device);
980 if ((cooplevel & DDSCL_EXCLUSIVE)
981 && (window != ddraw->dest_window || !(ddraw->cooperative_level & DDSCL_EXCLUSIVE)))
983 hr = wined3d_device_acquire_focus_window(ddraw->wined3d_device, window);
984 if (FAILED(hr))
986 ERR("Failed to acquire focus window, hr %#x.\n", hr);
987 goto done;
991 if (cooplevel & DDSCL_EXCLUSIVE)
993 SetRect(&clip_rect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
994 ClipCursor(&clip_rect);
997 /* Unhandled flags */
998 if (cooplevel & DDSCL_ALLOWREBOOT)
999 WARN("Unhandled flag DDSCL_ALLOWREBOOT, harmless\n");
1000 if (cooplevel & DDSCL_ALLOWMODEX)
1001 WARN("Unhandled flag DDSCL_ALLOWMODEX, harmless\n");
1002 if (cooplevel & DDSCL_FPUSETUP)
1003 WARN("Unhandled flag DDSCL_FPUSETUP, harmless\n");
1005 if (cooplevel & DDSCL_EXCLUSIVE)
1006 exclusive_ddraw = ddraw;
1007 else if (exclusive_ddraw == ddraw)
1008 exclusive_ddraw = NULL;
1010 /* Store the cooperative_level */
1011 ddraw->cooperative_level = cooplevel;
1012 ddraw->dest_window = window;
1014 TRACE("SetCooperativeLevel returning DD_OK\n");
1015 hr = DD_OK;
1016 done:
1017 ddraw->flags &= ~DDRAW_SCL_RECURSIVE;
1018 wined3d_mutex_unlock();
1020 return hr;
1023 static HRESULT WINAPI ddraw7_SetCooperativeLevel(IDirectDraw7 *iface, HWND window, DWORD flags)
1025 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1027 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1029 return ddraw_set_cooperative_level(ddraw, window, flags, !(ddraw->flags & DDRAW_SCL_DDRAW1));
1032 static HRESULT WINAPI ddraw4_SetCooperativeLevel(IDirectDraw4 *iface, HWND window, DWORD flags)
1034 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1036 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1038 return ddraw_set_cooperative_level(ddraw, window, flags, !(ddraw->flags & DDRAW_SCL_DDRAW1));
1041 static HRESULT WINAPI ddraw2_SetCooperativeLevel(IDirectDraw2 *iface, HWND window, DWORD flags)
1043 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1045 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1047 return ddraw_set_cooperative_level(ddraw, window, flags, !(ddraw->flags & DDRAW_SCL_DDRAW1));
1050 static HRESULT WINAPI ddraw1_SetCooperativeLevel(IDirectDraw *iface, HWND window, DWORD flags)
1052 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1053 HRESULT hr;
1055 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1057 hr = ddraw_set_cooperative_level(ddraw, window, flags, FALSE);
1058 if (SUCCEEDED(hr))
1059 ddraw->flags |= DDRAW_SCL_DDRAW1;
1060 return hr;
1063 /*****************************************************************************
1064 * IDirectDraw7::SetDisplayMode
1066 * Sets the display screen resolution, color depth and refresh frequency
1067 * when in fullscreen mode (in theory).
1068 * Possible return values listed in the SDK suggest that this method fails
1069 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
1070 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
1071 * It seems to be valid to pass 0 for With and Height, this has to be tested
1072 * It could mean that the current video mode should be left as-is. (But why
1073 * call it then?)
1075 * Params:
1076 * Height, Width: Screen dimension
1077 * BPP: Color depth in Bits per pixel
1078 * Refreshrate: Screen refresh rate
1079 * Flags: Other stuff
1081 * Returns
1082 * DD_OK on success
1084 *****************************************************************************/
1085 static HRESULT WINAPI ddraw7_SetDisplayMode(IDirectDraw7 *iface, DWORD width, DWORD height,
1086 DWORD bpp, DWORD refresh_rate, DWORD flags)
1088 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1089 struct wined3d_display_mode mode;
1090 enum wined3d_format_id format;
1091 RECT clip_rect;
1092 HRESULT hr;
1094 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1095 iface, width, height, bpp, refresh_rate, flags);
1097 if (force_refresh_rate != 0)
1099 TRACE("ForceRefreshRate overriding passed-in refresh rate (%u Hz) to %u Hz\n",
1100 refresh_rate, force_refresh_rate);
1101 refresh_rate = force_refresh_rate;
1104 wined3d_mutex_lock();
1106 if (exclusive_ddraw && exclusive_ddraw != ddraw)
1108 wined3d_mutex_unlock();
1109 return DDERR_NOEXCLUSIVEMODE;
1112 if (!width || !height)
1114 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here. */
1115 wined3d_mutex_unlock();
1116 return DD_OK;
1119 switch (bpp)
1121 case 8: format = WINED3DFMT_P8_UINT; break;
1122 case 15: format = WINED3DFMT_B5G5R5X1_UNORM; break;
1123 case 16: format = WINED3DFMT_B5G6R5_UNORM; break;
1124 case 24: format = WINED3DFMT_B8G8R8_UNORM; break;
1125 case 32: format = WINED3DFMT_B8G8R8X8_UNORM; break;
1126 default: format = WINED3DFMT_UNKNOWN; break;
1129 mode.width = width;
1130 mode.height = height;
1131 mode.refresh_rate = refresh_rate;
1132 mode.format_id = format;
1133 mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
1135 /* TODO: The possible return values from msdn suggest that the screen mode
1136 * can't be changed if a surface is locked or some drawing is in progress. */
1137 if (SUCCEEDED(hr = wined3d_output_set_display_mode(ddraw->wined3d_output, &mode)))
1139 if (ddraw->primary)
1141 DDSURFACEDESC2 *surface_desc = &ddraw->primary->surface_desc;
1143 if (FAILED(hr = wined3d_swapchain_resize_buffers(ddraw->wined3d_swapchain, 0,
1144 surface_desc->dwWidth, surface_desc->dwHeight, mode.format_id, WINED3D_MULTISAMPLE_NONE, 0)))
1145 ERR("Failed to resize buffers, hr %#x.\n", hr);
1146 else
1147 ddrawformat_from_wined3dformat(&ddraw->primary->surface_desc.u4.ddpfPixelFormat, mode.format_id);
1149 ddraw->flags |= DDRAW_RESTORE_MODE;
1151 if (ddraw->cooperative_level & DDSCL_EXCLUSIVE)
1153 SetRect(&clip_rect, 0, 0, width, height);
1154 ClipCursor(&clip_rect);
1158 InterlockedCompareExchange(&ddraw->device_state, DDRAW_DEVICE_STATE_NOT_RESTORED, DDRAW_DEVICE_STATE_OK);
1160 wined3d_mutex_unlock();
1162 return hr_ddraw_from_wined3d(hr);
1165 static HRESULT WINAPI ddraw4_SetDisplayMode(IDirectDraw4 *iface, DWORD width, DWORD height,
1166 DWORD bpp, DWORD refresh_rate, DWORD flags)
1168 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1170 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1171 iface, width, height, bpp, refresh_rate, flags);
1173 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1176 static HRESULT WINAPI ddraw2_SetDisplayMode(IDirectDraw2 *iface,
1177 DWORD width, DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
1179 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1181 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1182 iface, width, height, bpp, refresh_rate, flags);
1184 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1187 static HRESULT WINAPI ddraw1_SetDisplayMode(IDirectDraw *iface, DWORD width, DWORD height, DWORD bpp)
1189 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1191 TRACE("iface %p, width %u, height %u, bpp %u.\n", iface, width, height, bpp);
1193 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, 0, 0);
1196 void ddraw_d3dcaps1_from_7(D3DDEVICEDESC *caps1, D3DDEVICEDESC7 *caps7)
1198 memset(caps1, 0, sizeof(*caps1));
1199 caps1->dwSize = sizeof(*caps1);
1200 caps1->dwFlags = D3DDD_COLORMODEL
1201 | D3DDD_DEVCAPS
1202 | D3DDD_TRANSFORMCAPS
1203 | D3DDD_BCLIPPING
1204 | D3DDD_LIGHTINGCAPS
1205 | D3DDD_LINECAPS
1206 | D3DDD_TRICAPS
1207 | D3DDD_DEVICERENDERBITDEPTH
1208 | D3DDD_DEVICEZBUFFERBITDEPTH
1209 | D3DDD_MAXBUFFERSIZE
1210 | D3DDD_MAXVERTEXCOUNT;
1211 caps1->dcmColorModel = D3DCOLOR_RGB;
1212 caps1->dwDevCaps = caps7->dwDevCaps;
1213 caps1->dtcTransformCaps.dwSize = sizeof(caps1->dtcTransformCaps);
1214 caps1->dtcTransformCaps.dwCaps = D3DTRANSFORMCAPS_CLIP;
1215 caps1->bClipping = TRUE;
1216 caps1->dlcLightingCaps.dwSize = sizeof(caps1->dlcLightingCaps);
1217 caps1->dlcLightingCaps.dwCaps = D3DLIGHTCAPS_DIRECTIONAL
1218 | D3DLIGHTCAPS_PARALLELPOINT
1219 | D3DLIGHTCAPS_POINT
1220 | D3DLIGHTCAPS_SPOT;
1221 caps1->dlcLightingCaps.dwLightingModel = D3DLIGHTINGMODEL_RGB;
1222 caps1->dlcLightingCaps.dwNumLights = caps7->dwMaxActiveLights;
1223 caps1->dpcLineCaps = caps7->dpcLineCaps;
1224 caps1->dpcTriCaps = caps7->dpcTriCaps;
1225 caps1->dwDeviceRenderBitDepth = caps7->dwDeviceRenderBitDepth;
1226 caps1->dwDeviceZBufferBitDepth = caps7->dwDeviceZBufferBitDepth;
1227 caps1->dwMaxBufferSize = 0;
1228 caps1->dwMaxVertexCount = 65536;
1229 caps1->dwMinTextureWidth = caps7->dwMinTextureWidth;
1230 caps1->dwMinTextureHeight = caps7->dwMinTextureHeight;
1231 caps1->dwMaxTextureWidth = caps7->dwMaxTextureWidth;
1232 caps1->dwMaxTextureHeight = caps7->dwMaxTextureHeight;
1233 caps1->dwMinStippleWidth = 1;
1234 caps1->dwMinStippleHeight = 1;
1235 caps1->dwMaxStippleWidth = 32;
1236 caps1->dwMaxStippleHeight = 32;
1237 caps1->dwMaxTextureRepeat = caps7->dwMaxTextureRepeat;
1238 caps1->dwMaxTextureAspectRatio = caps7->dwMaxTextureAspectRatio;
1239 caps1->dwMaxAnisotropy = caps7->dwMaxAnisotropy;
1240 caps1->dvGuardBandLeft = caps7->dvGuardBandLeft;
1241 caps1->dvGuardBandTop = caps7->dvGuardBandTop;
1242 caps1->dvGuardBandRight = caps7->dvGuardBandRight;
1243 caps1->dvGuardBandBottom = caps7->dvGuardBandBottom;
1244 caps1->dvExtentsAdjust = caps7->dvExtentsAdjust;
1245 caps1->dwStencilCaps = caps7->dwStencilCaps;
1246 caps1->dwFVFCaps = caps7->dwFVFCaps;
1247 caps1->dwTextureOpCaps = caps7->dwTextureOpCaps;
1248 caps1->wMaxTextureBlendStages = caps7->wMaxTextureBlendStages;
1249 caps1->wMaxSimultaneousTextures = caps7->wMaxSimultaneousTextures;
1252 HRESULT ddraw_get_d3dcaps(const struct ddraw *ddraw, D3DDEVICEDESC7 *caps)
1254 struct wined3d_caps wined3d_caps;
1255 HRESULT hr;
1257 TRACE("ddraw %p, caps %p.\n", ddraw, caps);
1259 memset(&wined3d_caps, 0, sizeof(wined3d_caps));
1261 wined3d_mutex_lock();
1262 hr = wined3d_get_device_caps(ddraw->wined3d_adapter, WINED3D_DEVICE_TYPE_HAL, &wined3d_caps);
1263 wined3d_mutex_unlock();
1264 if (FAILED(hr))
1266 WARN("Failed to get device caps, hr %#x.\n", hr);
1267 return hr;
1270 caps->dwDevCaps = wined3d_caps.DevCaps;
1271 caps->dpcLineCaps.dwMiscCaps = wined3d_caps.PrimitiveMiscCaps;
1272 caps->dpcLineCaps.dwRasterCaps = wined3d_caps.RasterCaps;
1273 caps->dpcLineCaps.dwZCmpCaps = wined3d_caps.ZCmpCaps;
1274 caps->dpcLineCaps.dwSrcBlendCaps = wined3d_caps.SrcBlendCaps;
1275 caps->dpcLineCaps.dwDestBlendCaps = wined3d_caps.DestBlendCaps;
1276 caps->dpcLineCaps.dwAlphaCmpCaps = wined3d_caps.AlphaCmpCaps;
1277 caps->dpcLineCaps.dwShadeCaps = wined3d_caps.ShadeCaps;
1278 caps->dpcLineCaps.dwTextureCaps = wined3d_caps.TextureCaps;
1279 caps->dpcLineCaps.dwTextureFilterCaps = wined3d_caps.TextureFilterCaps;
1280 caps->dpcLineCaps.dwTextureAddressCaps = wined3d_caps.TextureAddressCaps;
1282 caps->dwMaxTextureWidth = wined3d_caps.MaxTextureWidth;
1283 caps->dwMaxTextureHeight = wined3d_caps.MaxTextureHeight;
1285 caps->dwMaxTextureRepeat = wined3d_caps.MaxTextureRepeat;
1286 caps->dwMaxTextureAspectRatio = wined3d_caps.MaxTextureAspectRatio;
1287 caps->dwMaxAnisotropy = wined3d_caps.MaxAnisotropy;
1288 caps->dvMaxVertexW = wined3d_caps.MaxVertexW;
1290 caps->dvGuardBandLeft = wined3d_caps.GuardBandLeft;
1291 caps->dvGuardBandTop = wined3d_caps.GuardBandTop;
1292 caps->dvGuardBandRight = wined3d_caps.GuardBandRight;
1293 caps->dvGuardBandBottom = wined3d_caps.GuardBandBottom;
1295 caps->dvExtentsAdjust = wined3d_caps.ExtentsAdjust;
1296 caps->dwStencilCaps = wined3d_caps.StencilCaps;
1298 caps->dwFVFCaps = wined3d_caps.FVFCaps;
1299 caps->dwTextureOpCaps = wined3d_caps.TextureOpCaps;
1301 caps->dwVertexProcessingCaps = wined3d_caps.VertexProcessingCaps;
1302 caps->dwMaxActiveLights = wined3d_caps.MaxActiveLights;
1304 /* Remove all non-d3d7 caps */
1305 caps->dwDevCaps &= (
1306 D3DDEVCAPS_FLOATTLVERTEX | D3DDEVCAPS_SORTINCREASINGZ | D3DDEVCAPS_SORTDECREASINGZ |
1307 D3DDEVCAPS_SORTEXACT | D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY |
1308 D3DDEVCAPS_TLVERTEXSYSTEMMEMORY | D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY |
1309 D3DDEVCAPS_TEXTUREVIDEOMEMORY | D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP |
1310 D3DDEVCAPS_TEXTURENONLOCALVIDMEM | D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
1311 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT | D3DDEVCAPS_CANBLTSYSTONONLOCAL |
1312 D3DDEVCAPS_HWRASTERIZATION);
1314 caps->dwStencilCaps &= (
1315 D3DSTENCILCAPS_KEEP | D3DSTENCILCAPS_ZERO | D3DSTENCILCAPS_REPLACE |
1316 D3DSTENCILCAPS_INCRSAT | D3DSTENCILCAPS_DECRSAT | D3DSTENCILCAPS_INVERT |
1317 D3DSTENCILCAPS_INCR | D3DSTENCILCAPS_DECR);
1319 /* FVF caps ?*/
1321 caps->dwTextureOpCaps &= (
1322 D3DTEXOPCAPS_DISABLE | D3DTEXOPCAPS_SELECTARG1 | D3DTEXOPCAPS_SELECTARG2 |
1323 D3DTEXOPCAPS_MODULATE | D3DTEXOPCAPS_MODULATE2X | D3DTEXOPCAPS_MODULATE4X |
1324 D3DTEXOPCAPS_ADD | D3DTEXOPCAPS_ADDSIGNED | D3DTEXOPCAPS_ADDSIGNED2X |
1325 D3DTEXOPCAPS_SUBTRACT | D3DTEXOPCAPS_ADDSMOOTH | D3DTEXOPCAPS_BLENDTEXTUREALPHA |
1326 D3DTEXOPCAPS_BLENDFACTORALPHA | D3DTEXOPCAPS_BLENDTEXTUREALPHAPM | D3DTEXOPCAPS_BLENDCURRENTALPHA |
1327 D3DTEXOPCAPS_PREMODULATE | D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
1328 D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA | D3DTEXOPCAPS_BUMPENVMAP |
1329 D3DTEXOPCAPS_BUMPENVMAPLUMINANCE | D3DTEXOPCAPS_DOTPRODUCT3);
1331 caps->dwVertexProcessingCaps &= (
1332 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_VERTEXFOG |
1333 D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER);
1335 caps->dpcLineCaps.dwMiscCaps &= (
1336 D3DPMISCCAPS_MASKPLANES | D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP |
1337 D3DPMISCCAPS_CONFORMANT | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW |
1338 D3DPMISCCAPS_CULLCCW);
1340 caps->dpcLineCaps.dwRasterCaps &= (
1341 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ROP2 | D3DPRASTERCAPS_XOR |
1342 D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_SUBPIXEL |
1343 D3DPRASTERCAPS_SUBPIXELX | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE |
1344 D3DPRASTERCAPS_STIPPLE | D3DPRASTERCAPS_ANTIALIASSORTDEPENDENT | D3DPRASTERCAPS_ANTIALIASSORTINDEPENDENT |
1345 D3DPRASTERCAPS_ANTIALIASEDGES | D3DPRASTERCAPS_MIPMAPLODBIAS |
1346 D3DPRASTERCAPS_ZBUFFERLESSHSR | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY |
1347 D3DPRASTERCAPS_WBUFFER | D3DPRASTERCAPS_TRANSLUCENTSORTINDEPENDENT | D3DPRASTERCAPS_WFOG |
1348 D3DPRASTERCAPS_ZFOG | WINED3DPRASTERCAPS_DEPTHBIAS);
1349 if (caps->dpcLineCaps.dwRasterCaps & WINED3DPRASTERCAPS_DEPTHBIAS)
1350 caps->dpcLineCaps.dwRasterCaps = (caps->dpcLineCaps.dwRasterCaps | D3DPRASTERCAPS_ZBIAS)
1351 & ~WINED3DPRASTERCAPS_DEPTHBIAS;
1352 if (wined3d_caps.LineCaps & WINED3DLINECAPS_ANTIALIAS)
1353 caps->dpcLineCaps.dwRasterCaps |= D3DPRASTERCAPS_ANTIALIASEDGES;
1355 caps->dpcLineCaps.dwZCmpCaps &= (
1356 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
1357 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
1358 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
1360 caps->dpcLineCaps.dwSrcBlendCaps &= (
1361 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
1362 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
1363 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
1364 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
1365 D3DPBLENDCAPS_BOTHINVSRCALPHA);
1367 caps->dpcLineCaps.dwDestBlendCaps &= (
1368 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
1369 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
1370 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
1371 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
1372 D3DPBLENDCAPS_BOTHINVSRCALPHA);
1374 caps->dpcLineCaps.dwAlphaCmpCaps &= (
1375 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
1376 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
1377 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
1379 caps->dpcLineCaps.dwShadeCaps &= (
1380 D3DPSHADECAPS_COLORFLATMONO | D3DPSHADECAPS_COLORFLATRGB | D3DPSHADECAPS_COLORGOURAUDMONO |
1381 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_COLORPHONGMONO | D3DPSHADECAPS_COLORPHONGRGB |
1382 D3DPSHADECAPS_SPECULARFLATMONO | D3DPSHADECAPS_SPECULARFLATRGB | D3DPSHADECAPS_SPECULARGOURAUDMONO |
1383 D3DPSHADECAPS_SPECULARGOURAUDRGB | D3DPSHADECAPS_SPECULARPHONGMONO | D3DPSHADECAPS_SPECULARPHONGRGB |
1384 D3DPSHADECAPS_ALPHAFLATBLEND | D3DPSHADECAPS_ALPHAFLATSTIPPLED | D3DPSHADECAPS_ALPHAGOURAUDBLEND |
1385 D3DPSHADECAPS_ALPHAGOURAUDSTIPPLED | D3DPSHADECAPS_ALPHAPHONGBLEND | D3DPSHADECAPS_ALPHAPHONGSTIPPLED |
1386 D3DPSHADECAPS_FOGFLAT | D3DPSHADECAPS_FOGGOURAUD | D3DPSHADECAPS_FOGPHONG);
1388 caps->dpcLineCaps.dwTextureCaps &= (
1389 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
1390 D3DPTEXTURECAPS_TRANSPARENCY | D3DPTEXTURECAPS_BORDER | D3DPTEXTURECAPS_SQUAREONLY |
1391 D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE | D3DPTEXTURECAPS_ALPHAPALETTE| D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
1392 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_COLORKEYBLEND);
1394 caps->dpcLineCaps.dwTextureFilterCaps &= (
1395 D3DPTFILTERCAPS_NEAREST | D3DPTFILTERCAPS_LINEAR | D3DPTFILTERCAPS_MIPNEAREST |
1396 D3DPTFILTERCAPS_MIPLINEAR | D3DPTFILTERCAPS_LINEARMIPNEAREST | D3DPTFILTERCAPS_LINEARMIPLINEAR |
1397 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
1398 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
1399 D3DPTFILTERCAPS_MAGFLINEAR | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC |
1400 D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC);
1402 caps->dpcLineCaps.dwTextureAddressCaps &= (
1403 D3DPTADDRESSCAPS_WRAP | D3DPTADDRESSCAPS_MIRROR | D3DPTADDRESSCAPS_CLAMP |
1404 D3DPTADDRESSCAPS_BORDER | D3DPTADDRESSCAPS_INDEPENDENTUV);
1406 if (!(caps->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2))
1408 /* DirectX7 always has the np2 flag set, no matter what the card
1409 * supports. Some old games (Rollcage) check the caps incorrectly.
1410 * If wined3d supports nonpow2 textures it also has np2 conditional
1411 * support. */
1412 caps->dpcLineCaps.dwTextureCaps |= D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL;
1415 /* Fill the missing members, and do some fixup */
1416 caps->dpcLineCaps.dwSize = sizeof(caps->dpcLineCaps);
1417 caps->dpcLineCaps.dwTextureBlendCaps = D3DPTBLENDCAPS_ADD
1418 | D3DPTBLENDCAPS_MODULATEMASK
1419 | D3DPTBLENDCAPS_COPY
1420 | D3DPTBLENDCAPS_DECAL
1421 | D3DPTBLENDCAPS_DECALALPHA
1422 | D3DPTBLENDCAPS_DECALMASK
1423 | D3DPTBLENDCAPS_MODULATE
1424 | D3DPTBLENDCAPS_MODULATEALPHA;
1425 caps->dpcLineCaps.dwStippleWidth = 32;
1426 caps->dpcLineCaps.dwStippleHeight = 32;
1427 /* Use the same for the TriCaps */
1428 caps->dpcTriCaps = caps->dpcLineCaps;
1430 caps->dwDeviceRenderBitDepth = DDBD_16 | DDBD_24 | DDBD_32;
1431 caps->dwDeviceZBufferBitDepth = DDBD_16 | DDBD_24;
1432 caps->dwMinTextureWidth = 1;
1433 caps->dwMinTextureHeight = 1;
1435 /* Convert DWORDs safely to WORDs */
1436 caps->wMaxTextureBlendStages = min(wined3d_caps.MaxTextureBlendStages, 0xffff);
1437 caps->wMaxSimultaneousTextures = min(wined3d_caps.MaxSimultaneousTextures, 0xffff);
1438 caps->wMaxUserClipPlanes = min(wined3d_caps.MaxUserClipPlanes, D3DMAXUSERCLIPPLANES);
1439 caps->wMaxVertexBlendMatrices = min(wined3d_caps.MaxVertexBlendMatrices, 0xffff);
1441 caps->deviceGUID = IID_IDirect3DTnLHalDevice;
1443 caps->dwReserved1 = 0;
1444 caps->dwReserved2 = 0;
1445 caps->dwReserved3 = 0;
1446 caps->dwReserved4 = 0;
1448 return DD_OK;
1451 /*****************************************************************************
1452 * IDirectDraw7::GetCaps
1454 * Returns the drives capabilities
1456 * Used for version 1, 2, 4 and 7
1458 * Params:
1459 * DriverCaps: Structure to write the Hardware accelerated caps to
1460 * HelCaps: Structure to write the emulation caps to
1462 * Returns
1463 * This implementation returns DD_OK only
1465 *****************************************************************************/
1466 static HRESULT WINAPI ddraw7_GetCaps(IDirectDraw7 *iface, DDCAPS *DriverCaps, DDCAPS *HELCaps)
1468 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1469 DDSCAPS2 ddscaps = {0, 0, 0, {0}};
1470 struct wined3d_caps winecaps;
1471 DDCAPS caps;
1472 HRESULT hr;
1474 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, DriverCaps, HELCaps);
1476 /* One structure must be != NULL */
1477 if (!DriverCaps && !HELCaps)
1479 WARN("Invalid parameters.\n");
1480 return DDERR_INVALIDPARAMS;
1483 memset(&caps, 0, sizeof(caps));
1484 memset(&winecaps, 0, sizeof(winecaps));
1485 caps.dwSize = sizeof(caps);
1487 wined3d_mutex_lock();
1488 hr = wined3d_device_get_device_caps(ddraw->wined3d_device, &winecaps);
1489 if (FAILED(hr))
1491 WARN("Failed to get device caps, %#x.\n", hr);
1492 wined3d_mutex_unlock();
1493 return hr;
1496 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
1497 if (FAILED(hr))
1499 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
1500 wined3d_mutex_unlock();
1501 return hr;
1504 hr = IDirectDraw7_GetFourCCCodes(iface, &caps.dwNumFourCCCodes, NULL);
1505 wined3d_mutex_unlock();
1506 if (FAILED(hr))
1508 WARN("IDirectDraw7::GetFourCCCodes failed\n");
1509 return hr;
1512 caps.dwCaps = winecaps.ddraw_caps.caps;
1513 caps.dwCaps2 = winecaps.ddraw_caps.caps2;
1514 caps.dwCKeyCaps = winecaps.ddraw_caps.color_key_caps;
1515 caps.dwFXCaps = winecaps.ddraw_caps.fx_caps;
1516 caps.dwPalCaps = DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE;
1517 caps.ddsCaps.dwCaps = winecaps.ddraw_caps.dds_caps;
1518 caps.dwSVBCaps = winecaps.ddraw_caps.svb_caps;
1519 caps.dwSVBCKeyCaps = winecaps.ddraw_caps.svb_color_key_caps;
1520 caps.dwSVBFXCaps = winecaps.ddraw_caps.svb_fx_caps;
1521 caps.dwVSBCaps = winecaps.ddraw_caps.vsb_caps;
1522 caps.dwVSBCKeyCaps = winecaps.ddraw_caps.vsb_color_key_caps;
1523 caps.dwVSBFXCaps = winecaps.ddraw_caps.vsb_fx_caps;
1524 caps.dwSSBCaps = winecaps.ddraw_caps.ssb_caps;
1525 caps.dwSSBCKeyCaps = winecaps.ddraw_caps.ssb_color_key_caps;
1526 caps.dwSSBFXCaps = winecaps.ddraw_caps.ssb_fx_caps;
1528 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
1529 caps.dwAlignStrideAlign = DDRAW_STRIDE_ALIGNMENT;
1531 caps.ddsOldCaps.dwCaps = caps.ddsCaps.dwCaps;
1533 if(DriverCaps)
1535 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
1536 if (TRACE_ON(ddraw))
1538 TRACE("Driver Caps :\n");
1539 DDRAW_dump_DDCAPS(DriverCaps);
1543 if(HELCaps)
1545 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
1546 if (TRACE_ON(ddraw))
1548 TRACE("HEL Caps :\n");
1549 DDRAW_dump_DDCAPS(HELCaps);
1553 return DD_OK;
1556 static HRESULT WINAPI ddraw4_GetCaps(IDirectDraw4 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1558 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1560 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1562 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1565 static HRESULT WINAPI ddraw2_GetCaps(IDirectDraw2 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1567 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1569 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1571 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1574 static HRESULT WINAPI ddraw1_GetCaps(IDirectDraw *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1576 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1578 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1580 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1583 /*****************************************************************************
1584 * IDirectDraw7::Compact
1586 * No idea what it does, MSDN says it's not implemented.
1588 * Returns
1589 * DD_OK, but this is unchecked
1591 *****************************************************************************/
1592 static HRESULT WINAPI ddraw7_Compact(IDirectDraw7 *iface)
1594 TRACE("iface %p.\n", iface);
1596 return DD_OK;
1599 static HRESULT WINAPI ddraw4_Compact(IDirectDraw4 *iface)
1601 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1603 TRACE("iface %p.\n", iface);
1605 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1608 static HRESULT WINAPI ddraw2_Compact(IDirectDraw2 *iface)
1610 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1612 TRACE("iface %p.\n", iface);
1614 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1617 static HRESULT WINAPI ddraw1_Compact(IDirectDraw *iface)
1619 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1621 TRACE("iface %p.\n", iface);
1623 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1626 /*****************************************************************************
1627 * IDirectDraw7::GetDisplayMode
1629 * Returns information about the current display mode
1631 * Exists in versions 1, 2, 4 and 7
1633 * Params:
1634 * DDSD: Address of a surface description structure to write the info to
1636 * Returns
1637 * DD_OK
1639 *****************************************************************************/
1640 static HRESULT WINAPI ddraw7_GetDisplayMode(IDirectDraw7 *iface, DDSURFACEDESC2 *DDSD)
1642 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1643 struct wined3d_display_mode mode;
1644 HRESULT hr;
1646 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
1648 /* This seems sane */
1649 if (!DDSD || (DDSD->dwSize != sizeof(DDSURFACEDESC) && DDSD->dwSize != sizeof(DDSURFACEDESC2)))
1650 return DDERR_INVALIDPARAMS;
1652 wined3d_mutex_lock();
1654 if (FAILED(hr = wined3d_output_get_display_mode(ddraw->wined3d_output, &mode, NULL)))
1656 ERR("Failed to get display mode, hr %#x.\n", hr);
1657 wined3d_mutex_unlock();
1658 return hr;
1661 memset(DDSD, 0, DDSD->dwSize);
1662 DDSD->dwSize = sizeof(*DDSD);
1663 DDSD->dwFlags = DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
1664 DDSD->dwWidth = mode.width;
1665 DDSD->dwHeight = mode.height;
1666 DDSD->u2.dwRefreshRate = mode.refresh_rate;
1667 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
1668 ddrawformat_from_wined3dformat(&DDSD->u4.ddpfPixelFormat, mode.format_id);
1669 DDSD->u1.lPitch = mode.width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
1671 if(TRACE_ON(ddraw))
1673 TRACE("Returning surface desc :\n");
1674 DDRAW_dump_surface_desc(DDSD);
1677 wined3d_mutex_unlock();
1679 return DD_OK;
1682 static HRESULT WINAPI ddraw4_GetDisplayMode(IDirectDraw4 *iface, DDSURFACEDESC2 *surface_desc)
1684 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1686 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1688 return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, surface_desc);
1691 static HRESULT WINAPI ddraw2_GetDisplayMode(IDirectDraw2 *iface, DDSURFACEDESC *surface_desc)
1693 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1694 HRESULT hr;
1696 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1698 hr = ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1699 if (SUCCEEDED(hr)) surface_desc->dwSize = sizeof(*surface_desc);
1700 return hr;
1703 static HRESULT WINAPI ddraw1_GetDisplayMode(IDirectDraw *iface, DDSURFACEDESC *surface_desc)
1705 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1706 HRESULT hr;
1708 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1710 hr = ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1711 if (SUCCEEDED(hr)) surface_desc->dwSize = sizeof(*surface_desc);
1712 return hr;
1715 /*****************************************************************************
1716 * IDirectDraw7::GetFourCCCodes
1718 * Returns an array of supported FourCC codes.
1720 * Exists in versions 1, 2, 4 and 7
1722 * Params:
1723 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
1724 * of enumerated codes
1725 * Codes: Pointer to an array of DWORDs where the supported codes are written
1726 * to
1728 * Returns
1729 * Always returns DD_OK, as it's a stub for now
1731 *****************************************************************************/
1732 static HRESULT WINAPI ddraw7_GetFourCCCodes(IDirectDraw7 *iface, DWORD *NumCodes, DWORD *Codes)
1734 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1735 static const enum wined3d_format_id formats[] =
1737 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
1738 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
1739 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
1741 struct wined3d_display_mode mode;
1742 DWORD count = 0, i, outsize;
1743 HRESULT hr;
1745 TRACE("iface %p, codes_count %p, codes %p.\n", iface, NumCodes, Codes);
1747 if (FAILED(hr = wined3d_output_get_display_mode(ddraw->wined3d_output, &mode, NULL)))
1749 ERR("Failed to get display mode, hr %#x.\n", hr);
1750 return hr;
1753 outsize = NumCodes && Codes ? *NumCodes : 0;
1755 for (i = 0; i < ARRAY_SIZE(formats); ++i)
1757 if (SUCCEEDED(wined3d_check_device_format(ddraw->wined3d, ddraw->wined3d_adapter, WINED3D_DEVICE_TYPE_HAL,
1758 mode.format_id, 0, 0, WINED3D_RTYPE_TEXTURE_2D, formats[i])))
1760 if (count < outsize)
1761 Codes[count] = formats[i];
1762 ++count;
1765 if(NumCodes) {
1766 TRACE("Returning %u FourCC codes\n", count);
1767 *NumCodes = count;
1770 return DD_OK;
1773 static HRESULT WINAPI ddraw4_GetFourCCCodes(IDirectDraw4 *iface, DWORD *codes_count, DWORD *codes)
1775 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1777 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1779 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1782 static HRESULT WINAPI ddraw2_GetFourCCCodes(IDirectDraw2 *iface, DWORD *codes_count, DWORD *codes)
1784 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1786 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1788 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1791 static HRESULT WINAPI ddraw1_GetFourCCCodes(IDirectDraw *iface, DWORD *codes_count, DWORD *codes)
1793 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1795 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1797 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1800 static HRESULT WINAPI ddraw7_GetMonitorFrequency(IDirectDraw7 *iface, DWORD *frequency)
1802 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1803 struct wined3d_display_mode mode;
1804 HRESULT hr;
1806 TRACE("iface %p, frequency %p.\n", iface, frequency);
1808 wined3d_mutex_lock();
1809 hr = wined3d_output_get_display_mode(ddraw->wined3d_output, &mode, NULL);
1810 wined3d_mutex_unlock();
1811 if (FAILED(hr))
1813 WARN("Failed to get display mode, hr %#x.\n", hr);
1814 return hr;
1817 *frequency = mode.refresh_rate;
1819 return DD_OK;
1822 static HRESULT WINAPI ddraw4_GetMonitorFrequency(IDirectDraw4 *iface, DWORD *frequency)
1824 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1826 TRACE("iface %p, frequency %p.\n", iface, frequency);
1828 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1831 static HRESULT WINAPI ddraw2_GetMonitorFrequency(IDirectDraw2 *iface, DWORD *frequency)
1833 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1835 TRACE("iface %p, frequency %p.\n", iface, frequency);
1837 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1840 static HRESULT WINAPI ddraw1_GetMonitorFrequency(IDirectDraw *iface, DWORD *frequency)
1842 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1844 TRACE("iface %p, frequency %p.\n", iface, frequency);
1846 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1849 static HRESULT WINAPI ddraw7_GetVerticalBlankStatus(IDirectDraw7 *iface, BOOL *status)
1851 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1852 struct wined3d_raster_status raster_status;
1853 HRESULT hr;
1855 TRACE("iface %p, status %p.\n", iface, status);
1857 if(!status)
1858 return DDERR_INVALIDPARAMS;
1860 wined3d_mutex_lock();
1861 hr = wined3d_output_get_raster_status(ddraw->wined3d_output, &raster_status);
1862 wined3d_mutex_unlock();
1863 if (FAILED(hr))
1865 WARN("Failed to get raster status, hr %#x.\n", hr);
1866 return hr;
1869 *status = raster_status.in_vblank;
1871 return DD_OK;
1874 static HRESULT WINAPI ddraw4_GetVerticalBlankStatus(IDirectDraw4 *iface, BOOL *status)
1876 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1878 TRACE("iface %p, status %p.\n", iface, status);
1880 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1883 static HRESULT WINAPI ddraw2_GetVerticalBlankStatus(IDirectDraw2 *iface, BOOL *status)
1885 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1887 TRACE("iface %p, status %p.\n", iface, status);
1889 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1892 static HRESULT WINAPI ddraw1_GetVerticalBlankStatus(IDirectDraw *iface, BOOL *status)
1894 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1896 TRACE("iface %p, status %p.\n", iface, status);
1898 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1901 /*****************************************************************************
1902 * IDirectDraw7::GetAvailableVidMem
1904 * Returns the total and free video memory
1906 * Params:
1907 * caps: Specifies the memory type asked for
1908 * total: Pointer to a DWORD to be filled with the total memory
1909 * free: Pointer to a DWORD to be filled with the free memory
1911 * Returns
1912 * DD_OK on success
1913 * DDERR_INVALIDPARAMS if free and total are NULL
1915 *****************************************************************************/
1916 static HRESULT WINAPI ddraw7_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *caps, DWORD *total,
1917 DWORD *free)
1919 unsigned int framebuffer_size, total_vidmem, free_vidmem;
1920 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1921 struct wined3d_display_mode mode;
1922 HRESULT hr = DD_OK;
1924 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1926 if (!total && !free)
1927 return DDERR_INVALIDPARAMS;
1929 if (TRACE_ON(ddraw))
1931 TRACE("Asked for memory with description: ");
1932 DDRAW_dump_DDSCAPS2(caps);
1934 wined3d_mutex_lock();
1936 /* Todo: System memory vs local video memory vs non-local video memory
1937 * The MSDN also mentions differences between texture memory and other
1938 * resources, but that's not important
1941 /* Some applications (e.g. 3DMark 2000) assume that the reported amount of
1942 * video memory doesn't include the memory used by the default framebuffer.
1944 if (FAILED(hr = wined3d_output_get_display_mode(ddraw->wined3d_output, &mode, NULL)))
1946 WARN("Failed to get display mode, hr %#x.\n", hr);
1947 wined3d_mutex_unlock();
1948 return hr;
1950 framebuffer_size = wined3d_calculate_format_pitch(ddraw->wined3d_adapter,
1951 mode.format_id, mode.width);
1952 framebuffer_size *= mode.height;
1954 if (free)
1956 free_vidmem = wined3d_device_get_available_texture_mem(ddraw->wined3d_device);
1957 *free = framebuffer_size > free_vidmem ? 0 : free_vidmem - framebuffer_size;
1958 TRACE("Free video memory %#x.\n", *free);
1961 if (total)
1963 struct wined3d_adapter_identifier desc = {0};
1965 hr = wined3d_adapter_get_identifier(ddraw->wined3d_adapter, 0, &desc);
1966 total_vidmem = min(UINT_MAX, desc.video_memory);
1967 *total = framebuffer_size > total_vidmem ? 0 : total_vidmem - framebuffer_size;
1968 TRACE("Total video memory %#x.\n", *total);
1971 wined3d_mutex_unlock();
1973 return hr;
1976 static HRESULT WINAPI ddraw4_GetAvailableVidMem(IDirectDraw4 *iface,
1977 DDSCAPS2 *caps, DWORD *total, DWORD *free)
1979 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1981 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1983 return ddraw7_GetAvailableVidMem(&ddraw->IDirectDraw7_iface, caps, total, free);
1986 static HRESULT WINAPI ddraw2_GetAvailableVidMem(IDirectDraw2 *iface,
1987 DDSCAPS *caps, DWORD *total, DWORD *free)
1989 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1990 DDSCAPS2 caps2;
1992 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1994 DDRAW_Convert_DDSCAPS_1_To_2(caps, &caps2);
1995 return ddraw7_GetAvailableVidMem(&ddraw->IDirectDraw7_iface, &caps2, total, free);
1998 /*****************************************************************************
1999 * IDirectDraw7::Initialize
2001 * Initializes a DirectDraw interface.
2003 * Params:
2004 * GUID: Interface identifier. Well, don't know what this is really good
2005 * for
2007 * Returns
2008 * Returns DD_OK on the first call,
2009 * DDERR_ALREADYINITIALIZED on repeated calls
2011 *****************************************************************************/
2012 static HRESULT WINAPI ddraw7_Initialize(IDirectDraw7 *iface, GUID *guid)
2014 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2016 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
2018 if (ddraw->flags & DDRAW_INITIALIZED)
2019 return DDERR_ALREADYINITIALIZED;
2021 /* FIXME: To properly take the GUID into account we should call
2022 * ddraw_init() here instead of in DDRAW_Create(). */
2023 if (guid)
2024 FIXME("Ignoring guid %s.\n", debugstr_guid(guid));
2026 ddraw->flags |= DDRAW_INITIALIZED;
2027 return DD_OK;
2030 static HRESULT WINAPI ddraw4_Initialize(IDirectDraw4 *iface, GUID *guid)
2032 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2034 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
2036 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
2039 static HRESULT WINAPI ddraw2_Initialize(IDirectDraw2 *iface, GUID *guid)
2041 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2043 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
2045 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
2048 static HRESULT WINAPI ddraw1_Initialize(IDirectDraw *iface, GUID *guid)
2050 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2052 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
2054 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
2057 static HRESULT WINAPI d3d1_Initialize(IDirect3D *iface, REFIID riid)
2059 TRACE("iface %p, riid %s.\n", iface, debugstr_guid(riid));
2061 return DDERR_ALREADYINITIALIZED;
2064 /*****************************************************************************
2065 * IDirectDraw7::FlipToGDISurface
2067 * "Makes the surface that the GDI writes to the primary surface"
2068 * Looks like some windows specific thing we don't have to care about.
2069 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
2070 * show error boxes ;)
2071 * Well, just return DD_OK.
2073 * Returns:
2074 * Always returns DD_OK
2076 *****************************************************************************/
2077 static HRESULT WINAPI ddraw7_FlipToGDISurface(IDirectDraw7 *iface)
2079 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2080 IDirectDrawSurface7 *gdi_surface;
2081 struct ddraw_surface *gdi_impl;
2082 HRESULT hr;
2084 TRACE("iface %p.\n", iface);
2086 wined3d_mutex_lock();
2088 if (FAILED(hr = IDirectDraw7_GetGDISurface(iface, &gdi_surface)))
2090 WARN("Failed to retrieve GDI surface, hr %#x.\n", hr);
2091 wined3d_mutex_unlock();
2092 return hr;
2095 gdi_impl = impl_from_IDirectDrawSurface7(gdi_surface);
2096 if (gdi_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
2097 hr = DD_OK;
2098 else
2099 hr = IDirectDrawSurface7_Flip(&ddraw->primary->IDirectDrawSurface7_iface, gdi_surface, DDFLIP_WAIT);
2100 IDirectDrawSurface7_Release(gdi_surface);
2102 wined3d_mutex_unlock();
2104 return hr;
2107 static HRESULT WINAPI ddraw4_FlipToGDISurface(IDirectDraw4 *iface)
2109 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2111 TRACE("iface %p.\n", iface);
2113 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
2116 static HRESULT WINAPI ddraw2_FlipToGDISurface(IDirectDraw2 *iface)
2118 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2120 TRACE("iface %p.\n", iface);
2122 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
2125 static HRESULT WINAPI ddraw1_FlipToGDISurface(IDirectDraw *iface)
2127 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2129 TRACE("iface %p.\n", iface);
2131 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
2134 /*****************************************************************************
2135 * IDirectDraw7::WaitForVerticalBlank
2137 * This method allows applications to get in sync with the vertical blank
2138 * interval.
2139 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
2140 * redraw the screen, most likely because of this stub
2142 * Parameters:
2143 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
2144 * or DDWAITVB_BLOCKEND
2145 * h: Not used, according to MSDN
2147 * Returns:
2148 * Always returns DD_OK
2150 *****************************************************************************/
2151 static HRESULT WINAPI ddraw7_WaitForVerticalBlank(IDirectDraw7 *iface, DWORD Flags, HANDLE event)
2153 static BOOL hide;
2155 TRACE("iface %p, flags %#x, event %p.\n", iface, Flags, event);
2157 /* This function is called often, so print the fixme only once */
2158 if(!hide)
2160 FIXME("iface %p, flags %#x, event %p stub!\n", iface, Flags, event);
2161 hide = TRUE;
2164 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
2165 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
2166 return DDERR_UNSUPPORTED; /* unchecked */
2168 return DD_OK;
2171 static HRESULT WINAPI ddraw4_WaitForVerticalBlank(IDirectDraw4 *iface, DWORD flags, HANDLE event)
2173 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2175 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
2177 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
2180 static HRESULT WINAPI ddraw2_WaitForVerticalBlank(IDirectDraw2 *iface, DWORD flags, HANDLE event)
2182 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2184 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
2186 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
2189 static HRESULT WINAPI ddraw1_WaitForVerticalBlank(IDirectDraw *iface, DWORD flags, HANDLE event)
2191 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2193 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
2195 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
2198 static HRESULT WINAPI ddraw7_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
2200 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2201 struct wined3d_raster_status raster_status;
2202 HRESULT hr;
2204 TRACE("iface %p, line %p.\n", iface, Scanline);
2206 wined3d_mutex_lock();
2207 hr = wined3d_output_get_raster_status(ddraw->wined3d_output, &raster_status);
2208 wined3d_mutex_unlock();
2209 if (FAILED(hr))
2211 WARN("Failed to get raster status, hr %#x.\n", hr);
2212 return hr;
2215 *Scanline = raster_status.scan_line;
2217 if (raster_status.in_vblank)
2218 return DDERR_VERTICALBLANKINPROGRESS;
2220 return DD_OK;
2223 static HRESULT WINAPI ddraw4_GetScanLine(IDirectDraw4 *iface, DWORD *line)
2225 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2227 TRACE("iface %p, line %p.\n", iface, line);
2229 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
2232 static HRESULT WINAPI ddraw2_GetScanLine(IDirectDraw2 *iface, DWORD *line)
2234 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2236 TRACE("iface %p, line %p.\n", iface, line);
2238 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
2241 static HRESULT WINAPI ddraw1_GetScanLine(IDirectDraw *iface, DWORD *line)
2243 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2245 TRACE("iface %p, line %p.\n", iface, line);
2247 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
2250 static HRESULT WINAPI ddraw7_TestCooperativeLevel(IDirectDraw7 *iface)
2252 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2254 TRACE("iface %p.\n", iface);
2256 return ddraw->device_state == DDRAW_DEVICE_STATE_LOST ? DDERR_NOEXCLUSIVEMODE : DD_OK;
2259 static HRESULT WINAPI ddraw4_TestCooperativeLevel(IDirectDraw4 *iface)
2261 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2263 TRACE("iface %p.\n", iface);
2265 return ddraw7_TestCooperativeLevel(&ddraw->IDirectDraw7_iface);
2268 /*****************************************************************************
2269 * IDirectDraw7::GetGDISurface
2271 * Returns the surface that GDI is treating as the primary surface.
2272 * For Wine this is the front buffer
2274 * Params:
2275 * GDISurface: Address to write the surface pointer to
2277 * Returns:
2278 * DD_OK if the surface was found
2279 * DDERR_NOTFOUND if the GDI surface wasn't found
2281 *****************************************************************************/
2282 static HRESULT WINAPI ddraw7_GetGDISurface(IDirectDraw7 *iface, IDirectDrawSurface7 **surface)
2284 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2285 struct ddraw_surface *ddraw_surface;
2287 TRACE("iface %p, surface %p.\n", iface, surface);
2289 wined3d_mutex_lock();
2291 if (!ddraw->gdi_surface || !(ddraw_surface = wined3d_texture_get_sub_resource_parent(ddraw->gdi_surface, 0)))
2293 WARN("GDI surface not available.\n");
2294 *surface = NULL;
2295 wined3d_mutex_unlock();
2296 return DDERR_NOTFOUND;
2298 *surface = &ddraw_surface->IDirectDrawSurface7_iface;
2299 IDirectDrawSurface7_AddRef(*surface);
2301 wined3d_mutex_unlock();
2303 return DD_OK;
2306 static HRESULT WINAPI ddraw4_GetGDISurface(IDirectDraw4 *iface, IDirectDrawSurface4 **surface)
2308 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2309 struct ddraw_surface *surface_impl;
2310 IDirectDrawSurface7 *surface7;
2311 HRESULT hr;
2313 TRACE("iface %p, surface %p.\n", iface, surface);
2315 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2316 if (FAILED(hr))
2318 *surface = NULL;
2319 return hr;
2321 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2322 *surface = &surface_impl->IDirectDrawSurface4_iface;
2323 IDirectDrawSurface4_AddRef(*surface);
2324 IDirectDrawSurface7_Release(surface7);
2326 return hr;
2329 static HRESULT WINAPI ddraw2_GetGDISurface(IDirectDraw2 *iface, IDirectDrawSurface **surface)
2331 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2332 struct ddraw_surface *surface_impl;
2333 IDirectDrawSurface7 *surface7;
2334 HRESULT hr;
2336 TRACE("iface %p, surface %p.\n", iface, surface);
2338 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2339 if (FAILED(hr))
2341 *surface = NULL;
2342 return hr;
2344 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2345 *surface = &surface_impl->IDirectDrawSurface_iface;
2346 IDirectDrawSurface_AddRef(*surface);
2347 IDirectDrawSurface7_Release(surface7);
2349 return hr;
2352 static HRESULT WINAPI ddraw1_GetGDISurface(IDirectDraw *iface, IDirectDrawSurface **surface)
2354 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2355 struct ddraw_surface *surface_impl;
2356 IDirectDrawSurface7 *surface7;
2357 HRESULT hr;
2359 TRACE("iface %p, surface %p.\n", iface, surface);
2361 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2362 if (FAILED(hr))
2364 *surface = NULL;
2365 return hr;
2367 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2368 *surface = &surface_impl->IDirectDrawSurface_iface;
2369 IDirectDrawSurface_AddRef(*surface);
2370 IDirectDrawSurface7_Release(surface7);
2372 return hr;
2375 struct displaymodescallback_context
2377 LPDDENUMMODESCALLBACK func;
2378 void *context;
2381 static HRESULT CALLBACK EnumDisplayModesCallbackThunk(DDSURFACEDESC2 *surface_desc, void *context)
2383 struct displaymodescallback_context *cbcontext = context;
2384 DDSURFACEDESC desc;
2386 DDSD2_to_DDSD(surface_desc, &desc);
2387 return cbcontext->func(&desc, cbcontext->context);
2390 /*****************************************************************************
2391 * IDirectDraw7::EnumDisplayModes
2393 * Enumerates the supported Display modes. The modes can be filtered with
2394 * the DDSD parameter.
2396 * Params:
2397 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES. For old ddraw
2398 * versions (3 and older?) this is reserved and must be 0.
2399 * DDSD: Surface description to filter the modes
2400 * Context: Pointer passed back to the callback function
2401 * cb: Application-provided callback function
2403 * Returns:
2404 * DD_OK on success
2405 * DDERR_INVALIDPARAMS if the callback wasn't set
2407 *****************************************************************************/
2408 static HRESULT WINAPI ddraw7_EnumDisplayModes(IDirectDraw7 *iface, DWORD Flags,
2409 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMMODESCALLBACK2 cb)
2411 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2412 struct wined3d_display_mode *enum_modes = NULL;
2413 struct wined3d_display_mode mode;
2414 unsigned int modenum, fmt;
2415 DDSURFACEDESC2 callback_sd;
2416 unsigned enum_mode_count = 0, enum_mode_array_size = 16;
2417 DDPIXELFORMAT pixelformat;
2419 static const enum wined3d_format_id checkFormatList[] =
2421 WINED3DFMT_B8G8R8X8_UNORM,
2422 WINED3DFMT_B5G6R5_UNORM,
2423 WINED3DFMT_P8_UINT,
2426 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2427 iface, Flags, DDSD, Context, cb);
2429 if (!cb)
2430 return DDERR_INVALIDPARAMS;
2432 if (!(enum_modes = heap_alloc(enum_mode_array_size * sizeof(*enum_modes))))
2433 return DDERR_OUTOFMEMORY;
2435 wined3d_mutex_lock();
2437 pixelformat.dwSize = sizeof(pixelformat);
2438 for(fmt = 0; fmt < ARRAY_SIZE(checkFormatList); fmt++)
2440 modenum = 0;
2441 while (wined3d_output_get_mode(ddraw->wined3d_output, checkFormatList[fmt],
2442 WINED3D_SCANLINE_ORDERING_UNKNOWN, modenum++, &mode) == WINED3D_OK)
2444 BOOL found = FALSE;
2445 unsigned i;
2447 ddrawformat_from_wined3dformat(&pixelformat, mode.format_id);
2448 if (DDSD)
2450 if (DDSD->dwFlags & DDSD_WIDTH && mode.width != DDSD->dwWidth)
2451 continue;
2452 if (DDSD->dwFlags & DDSD_HEIGHT && mode.height != DDSD->dwHeight)
2453 continue;
2454 if (DDSD->dwFlags & DDSD_REFRESHRATE && mode.refresh_rate != DDSD->u2.dwRefreshRate)
2455 continue;
2456 if (DDSD->dwFlags & DDSD_PIXELFORMAT
2457 && pixelformat.u1.dwRGBBitCount != DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount)
2458 continue;
2461 /* DX docs state EnumDisplayMode should return only unique modes */
2462 for (i = 0; i < enum_mode_count; i++)
2464 if (enum_modes[i].width == mode.width && enum_modes[i].height == mode.height
2465 && enum_modes[i].format_id == mode.format_id
2466 && (enum_modes[i].refresh_rate == mode.refresh_rate || !(Flags & DDEDM_REFRESHRATES)))
2468 found = TRUE;
2469 break;
2472 if(found) continue;
2474 memset(&callback_sd, 0, sizeof(callback_sd));
2475 callback_sd.dwSize = sizeof(callback_sd);
2476 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2478 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH|DDSD_REFRESHRATE;
2479 if (Flags & DDEDM_REFRESHRATES)
2480 callback_sd.u2.dwRefreshRate = mode.refresh_rate;
2482 callback_sd.dwWidth = mode.width;
2483 callback_sd.dwHeight = mode.height;
2485 callback_sd.u4.ddpfPixelFormat=pixelformat;
2487 /* Calc pitch and DWORD align like MSDN says */
2488 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.width;
2489 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
2491 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
2492 callback_sd.u2.dwRefreshRate);
2494 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
2496 TRACE("Application asked to terminate the enumeration\n");
2497 heap_free(enum_modes);
2498 wined3d_mutex_unlock();
2499 return DD_OK;
2502 if (enum_mode_count == enum_mode_array_size)
2504 struct wined3d_display_mode *new_enum_modes;
2506 enum_mode_array_size *= 2;
2507 if (!(new_enum_modes = heap_realloc(enum_modes, enum_mode_array_size * sizeof(*new_enum_modes))))
2509 heap_free(enum_modes);
2510 wined3d_mutex_unlock();
2511 return DDERR_OUTOFMEMORY;
2514 enum_modes = new_enum_modes;
2516 enum_modes[enum_mode_count++] = mode;
2520 TRACE("End of enumeration\n");
2521 heap_free(enum_modes);
2522 wined3d_mutex_unlock();
2524 return DD_OK;
2527 static HRESULT WINAPI ddraw4_EnumDisplayModes(IDirectDraw4 *iface, DWORD flags,
2528 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMMODESCALLBACK2 callback)
2530 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2532 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2533 iface, flags, surface_desc, context, callback);
2535 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags, surface_desc, context, callback);
2538 static HRESULT WINAPI ddraw2_EnumDisplayModes(IDirectDraw2 *iface, DWORD flags,
2539 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2541 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2542 struct displaymodescallback_context cbcontext;
2543 DDSURFACEDESC2 surface_desc2;
2545 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2546 iface, flags, surface_desc, context, callback);
2548 cbcontext.func = callback;
2549 cbcontext.context = context;
2551 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2552 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags,
2553 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2556 static HRESULT WINAPI ddraw1_EnumDisplayModes(IDirectDraw *iface, DWORD flags,
2557 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2559 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2560 struct displaymodescallback_context cbcontext;
2561 DDSURFACEDESC2 surface_desc2;
2563 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2564 iface, flags, surface_desc, context, callback);
2566 cbcontext.func = callback;
2567 cbcontext.context = context;
2569 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2570 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags,
2571 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2574 /*****************************************************************************
2575 * IDirectDraw7::EvaluateMode
2577 * Used with IDirectDraw7::StartModeTest to test video modes.
2578 * EvaluateMode is used to pass or fail a mode, and continue with the next
2579 * mode
2581 * Params:
2582 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
2583 * Timeout: Returns the amount of seconds left before the mode would have
2584 * been failed automatically
2586 * Returns:
2587 * This implementation always DD_OK, because it's a stub
2589 *****************************************************************************/
2590 static HRESULT WINAPI ddraw7_EvaluateMode(IDirectDraw7 *iface, DWORD Flags, DWORD *Timeout)
2592 FIXME("iface %p, flags %#x, timeout %p stub!\n", iface, Flags, Timeout);
2594 /* When implementing this, implement it in WineD3D */
2596 return DD_OK;
2599 /*****************************************************************************
2600 * IDirectDraw7::GetDeviceIdentifier
2602 * Returns the device identifier, which gives information about the driver
2603 * Our device identifier is defined at the beginning of this file.
2605 * Params:
2606 * DDDI: Address for the returned structure
2607 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
2609 * Returns:
2610 * On success it returns DD_OK
2611 * DDERR_INVALIDPARAMS if DDDI is NULL
2613 *****************************************************************************/
2614 static HRESULT WINAPI ddraw7_GetDeviceIdentifier(IDirectDraw7 *iface,
2615 DDDEVICEIDENTIFIER2 *DDDI, DWORD Flags)
2617 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2618 struct wined3d_adapter_identifier adapter_id;
2619 HRESULT hr = S_OK;
2621 TRACE("iface %p, device_identifier %p, flags %#x.\n", iface, DDDI, Flags);
2623 if (!DDDI)
2624 return DDERR_INVALIDPARAMS;
2626 if (Flags & DDGDI_GETHOSTIDENTIFIER)
2628 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
2629 * host adapter, if there's a secondary 3D adapter. This doesn't apply
2630 * to any modern hardware, nor is it interesting for Wine, so ignore it.
2631 * Size of DDDEVICEIDENTIFIER2 may be aligned to 8 bytes and thus 4
2632 * bytes too long. So only copy the relevant part of the structure
2635 memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
2636 return DD_OK;
2639 /* Drakan: Order of the Flame expects accurate D3D device information from ddraw */
2640 adapter_id.driver = DDDI->szDriver;
2641 adapter_id.driver_size = sizeof(DDDI->szDriver);
2642 adapter_id.description = DDDI->szDescription;
2643 adapter_id.description_size = sizeof(DDDI->szDescription);
2644 wined3d_mutex_lock();
2645 hr = wined3d_adapter_get_identifier(ddraw->wined3d_adapter, WINED3DENUM_WHQL_LEVEL, &adapter_id);
2646 wined3d_mutex_unlock();
2647 if (FAILED(hr)) return hr;
2649 DDDI->liDriverVersion = adapter_id.driver_version;
2650 DDDI->dwVendorId = adapter_id.vendor_id;
2651 DDDI->dwDeviceId = adapter_id.device_id;
2652 DDDI->dwSubSysId = adapter_id.subsystem_id;
2653 DDDI->dwRevision = adapter_id.revision;
2654 DDDI->guidDeviceIdentifier = adapter_id.device_identifier;
2655 DDDI->dwWHQLLevel = adapter_id.whql_level;
2656 return DD_OK;
2659 static HRESULT WINAPI ddraw4_GetDeviceIdentifier(IDirectDraw4 *iface,
2660 DDDEVICEIDENTIFIER *identifier, DWORD flags)
2662 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2663 DDDEVICEIDENTIFIER2 identifier2;
2664 HRESULT hr;
2666 TRACE("iface %p, identifier %p, flags %#x.\n", iface, identifier, flags);
2668 hr = ddraw7_GetDeviceIdentifier(&ddraw->IDirectDraw7_iface, &identifier2, flags);
2669 DDRAW_Convert_DDDEVICEIDENTIFIER_2_To_1(&identifier2, identifier);
2671 return hr;
2674 /*****************************************************************************
2675 * IDirectDraw7::GetSurfaceFromDC
2677 * Returns the Surface for a GDI device context handle.
2678 * Is this related to IDirectDrawSurface::GetDC ???
2680 * Params:
2681 * hdc: hdc to return the surface for
2682 * Surface: Address to write the surface pointer to
2684 * Returns:
2685 * Always returns DD_OK because it's a stub
2687 *****************************************************************************/
2688 static HRESULT WINAPI ddraw7_GetSurfaceFromDC(IDirectDraw7 *iface,
2689 HDC dc, IDirectDrawSurface7 **surface)
2691 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2692 struct ddraw_surface *surface_impl;
2694 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2696 if (!surface)
2697 return E_INVALIDARG;
2699 if (!dc)
2700 goto done;
2702 wined3d_mutex_lock();
2703 LIST_FOR_EACH_ENTRY(surface_impl, &ddraw->surface_list, struct ddraw_surface, surface_list_entry)
2705 if (surface_impl->dc != dc)
2706 continue;
2708 TRACE("Found surface %p for dc %p.\n", surface_impl, dc);
2709 *surface = &surface_impl->IDirectDrawSurface7_iface;
2710 IDirectDrawSurface7_AddRef(*surface);
2711 wined3d_mutex_unlock();
2712 return DD_OK;
2714 wined3d_mutex_unlock();
2716 done:
2717 TRACE("No surface found for dc %p.\n", dc);
2718 *surface = NULL;
2719 return DDERR_NOTFOUND;
2722 static HRESULT WINAPI ddraw4_GetSurfaceFromDC(IDirectDraw4 *iface, HDC dc,
2723 IDirectDrawSurface4 **surface)
2725 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2726 struct ddraw_surface *surface_impl;
2727 IDirectDrawSurface7 *surface7;
2728 HRESULT hr;
2730 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2732 if (!surface) return E_INVALIDARG;
2734 hr = ddraw7_GetSurfaceFromDC(&ddraw->IDirectDraw7_iface, dc, &surface7);
2735 if (FAILED(hr))
2737 *surface = NULL;
2738 return hr;
2740 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2741 /* Tests say this is true */
2742 *surface = (IDirectDrawSurface4 *)&surface_impl->IDirectDrawSurface_iface;
2743 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
2744 IDirectDrawSurface7_Release(surface7);
2746 return hr;
2749 static HRESULT CALLBACK restore_callback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *desc, void *context)
2751 IDirectDrawSurface_Restore(surface);
2752 IDirectDrawSurface_Release(surface);
2754 return DDENUMRET_OK;
2757 static HRESULT WINAPI ddraw7_RestoreAllSurfaces(IDirectDraw7 *iface)
2759 TRACE("iface %p.\n", iface);
2761 return IDirectDraw7_EnumSurfaces(iface, DDENUMSURFACES_ALL | DDENUMSURFACES_DOESEXIST,
2762 NULL, NULL, restore_callback);
2765 static HRESULT WINAPI ddraw4_RestoreAllSurfaces(IDirectDraw4 *iface)
2767 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2769 TRACE("iface %p.\n", iface);
2771 return ddraw7_RestoreAllSurfaces(&ddraw->IDirectDraw7_iface);
2774 /*****************************************************************************
2775 * IDirectDraw7::StartModeTest
2777 * Tests the specified video modes to update the system registry with
2778 * refresh rate information. StartModeTest starts the mode test,
2779 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
2780 * isn't called within 15 seconds, the mode is failed automatically
2782 * As refresh rates are handled by the X server, I don't think this
2783 * Method is important
2785 * Params:
2786 * Modes: An array of mode specifications
2787 * NumModes: The number of modes in Modes
2788 * Flags: Some flags...
2790 * Returns:
2791 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
2792 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
2793 * otherwise DD_OK
2795 *****************************************************************************/
2796 static HRESULT WINAPI ddraw7_StartModeTest(IDirectDraw7 *iface, SIZE *Modes, DWORD NumModes, DWORD Flags)
2798 FIXME("iface %p, modes %p, mode_count %u, flags %#x partial stub!\n",
2799 iface, Modes, NumModes, Flags);
2801 /* This looks sane */
2802 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
2804 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
2805 * As it is not, DDERR_TESTFINISHED is returned
2806 * (hopefully that's correct
2808 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
2809 * well, that value doesn't (yet) exist in the wine headers, so ignore it
2812 return DD_OK;
2815 static HRESULT WINAPI ddraw7_CreateSurface(IDirectDraw7 *iface, DDSURFACEDESC2 *surface_desc,
2816 IDirectDrawSurface7 **surface, IUnknown *outer_unknown)
2818 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2819 struct ddraw_surface *impl;
2820 HRESULT hr;
2822 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2823 iface, surface_desc, surface, outer_unknown);
2825 wined3d_mutex_lock();
2827 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2829 WARN("Cooperative level not set.\n");
2830 wined3d_mutex_unlock();
2831 return DDERR_NOCOOPERATIVELEVELSET;
2834 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
2836 WARN("Application supplied invalid surface descriptor\n");
2837 wined3d_mutex_unlock();
2838 return DDERR_INVALIDPARAMS;
2841 __TRY
2843 *surface = NULL;
2845 __EXCEPT_PAGE_FAULT
2847 WARN("Surface pointer %p is invalid.\n", surface);
2848 wined3d_mutex_unlock();
2849 return DDERR_INVALIDPARAMS;
2851 __ENDTRY;
2853 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
2855 if (TRACE_ON(ddraw))
2857 TRACE(" (%p) Requesting surface desc :\n", iface);
2858 DDRAW_dump_surface_desc(surface_desc);
2861 WARN("Application tried to create an explicit front or back buffer\n");
2862 wined3d_mutex_unlock();
2863 return DDERR_INVALIDCAPS;
2866 hr = ddraw_surface_create(ddraw, surface_desc, &impl, outer_unknown, 7);
2867 wined3d_mutex_unlock();
2868 if (FAILED(hr))
2869 return hr;
2871 *surface = &impl->IDirectDrawSurface7_iface;
2872 IDirectDraw7_AddRef(iface);
2873 impl->ifaceToRelease = (IUnknown *)iface;
2875 return hr;
2878 static HRESULT WINAPI ddraw4_CreateSurface(IDirectDraw4 *iface,
2879 DDSURFACEDESC2 *surface_desc, IDirectDrawSurface4 **surface, IUnknown *outer_unknown)
2881 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2882 struct ddraw_surface *impl;
2883 HRESULT hr;
2885 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2886 iface, surface_desc, surface, outer_unknown);
2888 wined3d_mutex_lock();
2890 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2892 WARN("Cooperative level not set.\n");
2893 wined3d_mutex_unlock();
2894 return DDERR_NOCOOPERATIVELEVELSET;
2897 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
2899 WARN("Application supplied invalid surface descriptor\n");
2900 wined3d_mutex_unlock();
2901 return DDERR_INVALIDPARAMS;
2904 __TRY
2906 *surface = NULL;
2908 __EXCEPT_PAGE_FAULT
2910 WARN("Surface pointer %p is invalid.\n", surface);
2911 wined3d_mutex_unlock();
2912 return DDERR_INVALIDPARAMS;
2914 __ENDTRY;
2916 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
2918 if (TRACE_ON(ddraw))
2920 TRACE(" (%p) Requesting surface desc :\n", iface);
2921 DDRAW_dump_surface_desc(surface_desc);
2924 WARN("Application tried to create an explicit front or back buffer\n");
2925 wined3d_mutex_unlock();
2926 return DDERR_INVALIDCAPS;
2929 hr = ddraw_surface_create(ddraw, surface_desc, &impl, outer_unknown, 4);
2930 wined3d_mutex_unlock();
2931 if (FAILED(hr))
2932 return hr;
2934 *surface = &impl->IDirectDrawSurface4_iface;
2935 IDirectDraw4_AddRef(iface);
2936 impl->ifaceToRelease = (IUnknown *)iface;
2938 return hr;
2941 static HRESULT WINAPI ddraw2_CreateSurface(IDirectDraw2 *iface,
2942 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
2944 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2945 struct ddraw_surface *impl;
2946 HRESULT hr;
2947 DDSURFACEDESC2 surface_desc2;
2949 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2950 iface, surface_desc, surface, outer_unknown);
2952 wined3d_mutex_lock();
2954 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2956 WARN("Cooperative level not set.\n");
2957 wined3d_mutex_unlock();
2958 return DDERR_NOCOOPERATIVELEVELSET;
2961 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
2963 WARN("Application supplied invalid surface descriptor\n");
2964 wined3d_mutex_unlock();
2965 return DDERR_INVALIDPARAMS;
2968 __TRY
2970 *surface = NULL;
2972 __EXCEPT_PAGE_FAULT
2974 WARN("Surface pointer %p is invalid.\n", surface);
2975 wined3d_mutex_unlock();
2976 return DDERR_INVALIDPARAMS;
2978 __ENDTRY;
2980 DDSD_to_DDSD2(surface_desc, &surface_desc2);
2981 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
2983 if (TRACE_ON(ddraw))
2985 TRACE(" (%p) Requesting surface desc :\n", iface);
2986 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
2989 WARN("Application tried to create an explicit front or back buffer\n");
2990 wined3d_mutex_unlock();
2991 return DDERR_INVALIDCAPS;
2994 hr = ddraw_surface_create(ddraw, &surface_desc2, &impl, outer_unknown, 2);
2995 wined3d_mutex_unlock();
2996 if (FAILED(hr))
2997 return hr;
2999 *surface = &impl->IDirectDrawSurface_iface;
3000 impl->ifaceToRelease = NULL;
3002 return hr;
3005 static HRESULT WINAPI ddraw1_CreateSurface(IDirectDraw *iface,
3006 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3008 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3009 struct ddraw_surface *impl;
3010 HRESULT hr;
3011 DDSURFACEDESC2 surface_desc2;
3013 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3014 iface, surface_desc, surface, outer_unknown);
3016 wined3d_mutex_lock();
3018 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
3020 WARN("Cooperative level not set.\n");
3021 wined3d_mutex_unlock();
3022 return DDERR_NOCOOPERATIVELEVELSET;
3025 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3027 WARN("Application supplied invalid surface descriptor\n");
3028 wined3d_mutex_unlock();
3029 return DDERR_INVALIDPARAMS;
3032 __TRY
3034 *surface = NULL;
3036 __EXCEPT_PAGE_FAULT
3038 WARN("Surface pointer %p is invalid.\n", surface);
3039 wined3d_mutex_unlock();
3040 return DDERR_INVALIDPARAMS;
3042 __ENDTRY;
3044 if ((surface_desc->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER))
3045 == (DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER)
3046 || (surface_desc->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER))
3047 == ((DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER)))
3049 WARN("Application tried to create an explicit front or back buffer.\n");
3050 wined3d_mutex_unlock();
3051 return DDERR_INVALIDCAPS;
3054 DDSD_to_DDSD2(surface_desc, &surface_desc2);
3055 hr = ddraw_surface_create(ddraw, &surface_desc2, &impl, outer_unknown, 1);
3056 wined3d_mutex_unlock();
3057 if (FAILED(hr))
3058 return hr;
3060 *surface = &impl->IDirectDrawSurface_iface;
3061 impl->ifaceToRelease = NULL;
3063 return hr;
3066 static BOOL
3067 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
3068 const DDPIXELFORMAT *provided)
3070 /* Some flags must be present in both or neither for a match. */
3071 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
3072 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
3073 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
3075 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3076 return FALSE;
3078 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
3079 return FALSE;
3081 if (requested->dwFlags & DDPF_FOURCC)
3082 if (requested->dwFourCC != provided->dwFourCC)
3083 return FALSE;
3085 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
3086 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3087 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
3088 return FALSE;
3090 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3091 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3092 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
3093 return FALSE;
3095 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
3096 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
3097 return FALSE;
3099 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
3100 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3101 |DDPF_BUMPDUDV))
3102 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
3103 return FALSE;
3105 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
3106 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
3107 return FALSE;
3109 return TRUE;
3112 static BOOL ddraw_match_surface_desc(const DDSURFACEDESC2 *requested, const DDSURFACEDESC2 *provided)
3114 struct compare_info
3116 DWORD flag;
3117 ptrdiff_t offset;
3118 size_t size;
3121 #define CMP(FLAG, FIELD) \
3122 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
3123 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
3125 static const struct compare_info compare[] =
3127 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
3128 CMP(BACKBUFFERCOUNT, u5.dwBackBufferCount),
3129 CMP(CAPS, ddsCaps),
3130 CMP(CKDESTBLT, ddckCKDestBlt),
3131 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
3132 CMP(CKSRCBLT, ddckCKSrcBlt),
3133 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
3134 CMP(HEIGHT, dwHeight),
3135 CMP(LINEARSIZE, u1 /* dwLinearSize */),
3136 CMP(LPSURFACE, lpSurface),
3137 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
3138 CMP(PITCH, u1 /* lPitch */),
3139 /* PIXELFORMAT: manual */
3140 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
3141 CMP(TEXTURESTAGE, dwTextureStage),
3142 CMP(WIDTH, dwWidth),
3143 /* ZBUFFERBITDEPTH: "obsolete" */
3146 #undef CMP
3148 unsigned int i;
3150 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3151 return FALSE;
3153 for (i=0; i < ARRAY_SIZE(compare); i++)
3155 if (requested->dwFlags & compare[i].flag
3156 && memcmp((const char *)provided + compare[i].offset,
3157 (const char *)requested + compare[i].offset,
3158 compare[i].size) != 0)
3159 return FALSE;
3162 if (requested->dwFlags & DDSD_PIXELFORMAT)
3164 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
3165 &provided->u4.ddpfPixelFormat))
3166 return FALSE;
3169 return TRUE;
3172 struct surfacescallback2_context
3174 LPDDENUMSURFACESCALLBACK2 func;
3175 void *context;
3178 struct surfacescallback_context
3180 LPDDENUMSURFACESCALLBACK func;
3181 void *context;
3184 static HRESULT CALLBACK EnumSurfacesCallback2Thunk(IDirectDrawSurface7 *surface,
3185 DDSURFACEDESC2 *surface_desc, void *context)
3187 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3188 struct surfacescallback2_context *cbcontext = context;
3190 if (!surface)
3191 return cbcontext->func(NULL, surface_desc, cbcontext->context);
3193 IDirectDrawSurface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
3194 IDirectDrawSurface7_Release(surface);
3196 return cbcontext->func(&surface_impl->IDirectDrawSurface4_iface,
3197 surface_desc, cbcontext->context);
3200 static HRESULT CALLBACK EnumSurfacesCallbackThunk(IDirectDrawSurface7 *surface,
3201 DDSURFACEDESC2 *surface_desc, void *context)
3203 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3204 struct surfacescallback_context *cbcontext = context;
3206 if (!surface)
3207 return cbcontext->func(NULL, (DDSURFACEDESC *)surface_desc, cbcontext->context);
3209 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
3210 IDirectDrawSurface7_Release(surface);
3212 return cbcontext->func(&surface_impl->IDirectDrawSurface_iface,
3213 (DDSURFACEDESC *)surface_desc, cbcontext->context);
3216 struct enum_surface_mode_params
3218 IDirectDraw7 *ddraw;
3219 const DDSURFACEDESC2 *desc;
3220 LPDDENUMSURFACESCALLBACK7 callback;
3221 void *context;
3224 static HRESULT CALLBACK enum_surface_mode_callback(DDSURFACEDESC2 *surface_desc, void *context)
3226 const struct enum_surface_mode_params *params = context;
3227 DDSURFACEDESC2 desc = *params->desc;
3228 IDirectDrawSurface7 *surface;
3230 desc.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT | DDSD_PITCH | DDSD_PIXELFORMAT;
3231 desc.dwWidth = surface_desc->dwWidth;
3232 desc.dwHeight = surface_desc->dwHeight;
3233 desc.u1.lPitch = surface_desc->u1.lPitch;
3234 desc.u4.ddpfPixelFormat = surface_desc->u4.ddpfPixelFormat;
3236 if (SUCCEEDED(ddraw7_CreateSurface(params->ddraw, &desc, &surface, NULL)))
3238 IDirectDrawSurface7_Release(surface);
3239 return params->callback(NULL, &desc, params->context);
3241 return DDENUMRET_OK;
3244 /*****************************************************************************
3245 * IDirectDraw7::EnumSurfaces
3247 * Loops through all surfaces attached to this device and calls the
3248 * application callback. This can't be relayed to WineD3DDevice,
3249 * because some WineD3DSurfaces' parents are IParent objects
3251 * Params:
3252 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
3253 * DDSD: Description to filter for
3254 * Context: Application-provided pointer, it's passed unmodified to the
3255 * Callback function
3256 * Callback: Address to call for each surface
3258 * Returns:
3259 * DDERR_INVALIDPARAMS if the callback is NULL
3260 * DD_OK on success
3262 *****************************************************************************/
3263 static HRESULT WINAPI ddraw7_EnumSurfaces(IDirectDraw7 *iface, DWORD flags,
3264 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMSURFACESCALLBACK7 callback)
3266 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
3267 HRESULT hr = DD_OK;
3269 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3270 iface, flags, surface_desc, context, callback);
3272 if (!callback)
3273 return DDERR_INVALIDPARAMS;
3275 if (flags & DDENUMSURFACES_CANBECREATED)
3277 IDirectDrawSurface7 *surface;
3279 if ((flags & (DDENUMSURFACES_ALL | DDENUMSURFACES_MATCH | DDENUMSURFACES_NOMATCH)) != DDENUMSURFACES_MATCH)
3280 return DDERR_INVALIDPARAMS;
3282 wined3d_mutex_lock();
3284 if (surface_desc->dwFlags & (DDSD_WIDTH | DDSD_HEIGHT))
3286 if (SUCCEEDED(ddraw7_CreateSurface(iface, surface_desc, &surface, NULL)))
3288 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3289 callback(NULL, &surface_impl->surface_desc, context);
3290 IDirectDrawSurface7_Release(surface);
3293 else
3295 DDSURFACEDESC2 desc =
3297 .dwSize = sizeof(desc),
3298 .dwFlags = DDSD_PIXELFORMAT,
3299 .u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT),
3301 struct enum_surface_mode_params params =
3303 .ddraw = iface,
3304 .desc = surface_desc,
3305 .callback = callback,
3306 .context = context,
3308 struct wined3d_display_mode mode;
3310 if (FAILED(hr = wined3d_output_get_display_mode(ddraw->wined3d_output, &mode, NULL)))
3312 ERR("Failed to get display mode, hr %#x.\n", hr);
3313 wined3d_mutex_unlock();
3314 return hr_ddraw_from_wined3d(hr);
3317 ddrawformat_from_wined3dformat(&desc.u4.ddpfPixelFormat, mode.format_id);
3318 hr = ddraw7_EnumDisplayModes(iface, 0, &desc, &params, enum_surface_mode_callback);
3321 wined3d_mutex_unlock();
3323 else if (flags & DDENUMSURFACES_DOESEXIST)
3325 struct ddraw_surface *surface, *cursor;
3326 BOOL nomatch = !!(flags & DDENUMSURFACES_NOMATCH);
3328 wined3d_mutex_lock();
3330 /* Use the safe enumeration, as the callback may destroy surfaces. */
3331 LIST_FOR_EACH_ENTRY_SAFE(surface, cursor, &ddraw->surface_list, struct ddraw_surface, surface_list_entry)
3333 if (!surface->iface_count)
3335 WARN("Not enumerating surface %p because it doesn't have any references.\n", surface);
3336 continue;
3339 if ((flags & DDENUMSURFACES_ALL)
3340 || nomatch != ddraw_match_surface_desc(surface_desc, &surface->surface_desc))
3342 DDSURFACEDESC2 desc = surface->surface_desc;
3344 TRACE("Enumerating surface %p.\n", surface);
3345 IDirectDrawSurface7_AddRef(&surface->IDirectDrawSurface7_iface);
3346 if (callback(&surface->IDirectDrawSurface7_iface, &desc, context) != DDENUMRET_OK)
3347 break;
3351 wined3d_mutex_unlock();
3353 else
3355 return DDERR_INVALIDPARAMS;
3358 return hr;
3361 static HRESULT WINAPI ddraw4_EnumSurfaces(IDirectDraw4 *iface, DWORD flags,
3362 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMSURFACESCALLBACK2 callback)
3364 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3365 struct surfacescallback2_context cbcontext;
3367 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3368 iface, flags, surface_desc, context, callback);
3370 cbcontext.func = callback;
3371 cbcontext.context = context;
3373 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags, surface_desc,
3374 &cbcontext, EnumSurfacesCallback2Thunk);
3377 static HRESULT WINAPI ddraw2_EnumSurfaces(IDirectDraw2 *iface, DWORD flags,
3378 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3380 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3381 struct surfacescallback_context cbcontext;
3382 DDSURFACEDESC2 surface_desc2;
3384 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3385 iface, flags, surface_desc, context, callback);
3387 cbcontext.func = callback;
3388 cbcontext.context = context;
3390 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3391 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags,
3392 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3395 static HRESULT WINAPI ddraw1_EnumSurfaces(IDirectDraw *iface, DWORD flags,
3396 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3398 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3399 struct surfacescallback_context cbcontext;
3400 DDSURFACEDESC2 surface_desc2;
3402 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3403 iface, flags, surface_desc, context, callback);
3405 cbcontext.func = callback;
3406 cbcontext.context = context;
3408 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3409 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags,
3410 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3413 /*****************************************************************************
3414 * DirectDrawCreateClipper (DDRAW.@)
3416 * Creates a new IDirectDrawClipper object.
3418 * Params:
3419 * Clipper: Address to write the interface pointer to
3420 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3421 * NULL
3423 * Returns:
3424 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3425 * E_OUTOFMEMORY if allocating the object failed
3427 *****************************************************************************/
3428 HRESULT WINAPI DirectDrawCreateClipper(DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3430 struct ddraw_clipper *object;
3431 HRESULT hr;
3433 TRACE("flags %#x, clipper %p, outer_unknown %p.\n",
3434 flags, clipper, outer_unknown);
3436 if (outer_unknown)
3437 return CLASS_E_NOAGGREGATION;
3439 wined3d_mutex_lock();
3441 if (!(object = heap_alloc_zero(sizeof(*object))))
3443 wined3d_mutex_unlock();
3444 return E_OUTOFMEMORY;
3447 hr = ddraw_clipper_init(object);
3448 if (FAILED(hr))
3450 WARN("Failed to initialize clipper, hr %#x.\n", hr);
3451 heap_free(object);
3452 wined3d_mutex_unlock();
3453 return hr;
3456 TRACE("Created clipper %p.\n", object);
3457 *clipper = &object->IDirectDrawClipper_iface;
3458 wined3d_mutex_unlock();
3460 return DD_OK;
3463 /*****************************************************************************
3464 * IDirectDraw7::CreateClipper
3466 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3468 *****************************************************************************/
3469 static HRESULT WINAPI ddraw7_CreateClipper(IDirectDraw7 *iface, DWORD Flags,
3470 IDirectDrawClipper **Clipper, IUnknown *UnkOuter)
3472 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3473 iface, Flags, Clipper, UnkOuter);
3475 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3478 static HRESULT WINAPI ddraw4_CreateClipper(IDirectDraw4 *iface, DWORD flags,
3479 IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3481 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3483 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3484 iface, flags, clipper, outer_unknown);
3486 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3489 static HRESULT WINAPI ddraw2_CreateClipper(IDirectDraw2 *iface,
3490 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3492 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3494 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3495 iface, flags, clipper, outer_unknown);
3497 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3500 static HRESULT WINAPI ddraw1_CreateClipper(IDirectDraw *iface,
3501 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3503 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3505 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3506 iface, flags, clipper, outer_unknown);
3508 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3511 /*****************************************************************************
3512 * IDirectDraw7::CreatePalette
3514 * Creates a new IDirectDrawPalette object
3516 * Params:
3517 * Flags: The flags for the new clipper
3518 * ColorTable: Color table to assign to the new clipper
3519 * Palette: Address to write the interface pointer to
3520 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3521 * NULL
3523 * Returns:
3524 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3525 * E_OUTOFMEMORY if allocating the object failed
3527 *****************************************************************************/
3528 static HRESULT WINAPI ddraw7_CreatePalette(IDirectDraw7 *iface, DWORD Flags,
3529 PALETTEENTRY *ColorTable, IDirectDrawPalette **Palette, IUnknown *pUnkOuter)
3531 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
3532 struct ddraw_palette *object;
3533 HRESULT hr;
3535 TRACE("iface %p, flags %#x, color_table %p, palette %p, outer_unknown %p.\n",
3536 iface, Flags, ColorTable, Palette, pUnkOuter);
3538 if (pUnkOuter)
3539 return CLASS_E_NOAGGREGATION;
3541 wined3d_mutex_lock();
3543 /* The refcount test shows that a cooplevel is required for this */
3544 if (!ddraw->cooperative_level)
3546 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3547 wined3d_mutex_unlock();
3548 return DDERR_NOCOOPERATIVELEVELSET;
3551 if (!(object = heap_alloc(sizeof(*object))))
3553 ERR("Out of memory when allocating memory for a palette implementation\n");
3554 wined3d_mutex_unlock();
3555 return E_OUTOFMEMORY;
3558 hr = ddraw_palette_init(object, ddraw, Flags, ColorTable);
3559 if (FAILED(hr))
3561 WARN("Failed to initialize palette, hr %#x.\n", hr);
3562 heap_free(object);
3563 wined3d_mutex_unlock();
3564 return hr;
3567 TRACE("Created palette %p.\n", object);
3568 *Palette = &object->IDirectDrawPalette_iface;
3569 wined3d_mutex_unlock();
3571 return DD_OK;
3574 static HRESULT WINAPI ddraw4_CreatePalette(IDirectDraw4 *iface, DWORD flags, PALETTEENTRY *entries,
3575 IDirectDrawPalette **palette, IUnknown *outer_unknown)
3577 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3578 HRESULT hr;
3580 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3581 iface, flags, entries, palette, outer_unknown);
3583 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3584 if (SUCCEEDED(hr) && *palette)
3586 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3587 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3588 IDirectDraw4_AddRef(iface);
3589 impl->ifaceToRelease = (IUnknown *)iface;
3591 return hr;
3594 static HRESULT WINAPI ddraw2_CreatePalette(IDirectDraw2 *iface, DWORD flags,
3595 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3597 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3598 HRESULT hr;
3600 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3601 iface, flags, entries, palette, outer_unknown);
3603 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3604 if (SUCCEEDED(hr) && *palette)
3606 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3607 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3608 impl->ifaceToRelease = NULL;
3611 return hr;
3614 static HRESULT WINAPI ddraw1_CreatePalette(IDirectDraw *iface, DWORD flags,
3615 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3617 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3618 HRESULT hr;
3620 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3621 iface, flags, entries, palette, outer_unknown);
3623 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3624 if (SUCCEEDED(hr) && *palette)
3626 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3627 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3628 impl->ifaceToRelease = NULL;
3631 return hr;
3634 /*****************************************************************************
3635 * IDirectDraw7::DuplicateSurface
3637 * Duplicates a surface. The surface memory points to the same memory as
3638 * the original surface, and it's released when the last surface referencing
3639 * it is released. I guess that's beyond Wine's surface management right now
3640 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3641 * test application to implement this)
3643 * Params:
3644 * Src: Address of the source surface
3645 * Dest: Address to write the new surface pointer to
3647 * Returns:
3648 * See IDirectDraw7::CreateSurface
3650 *****************************************************************************/
3651 static HRESULT WINAPI ddraw7_DuplicateSurface(IDirectDraw7 *iface,
3652 IDirectDrawSurface7 *Src, IDirectDrawSurface7 **Dest)
3654 struct ddraw_surface *src_surface = unsafe_impl_from_IDirectDrawSurface7(Src);
3656 FIXME("iface %p, src %p, dst %p partial stub!\n", iface, Src, Dest);
3658 /* For now, simply create a new, independent surface */
3659 return IDirectDraw7_CreateSurface(iface, &src_surface->surface_desc, Dest, NULL);
3662 static HRESULT WINAPI ddraw4_DuplicateSurface(IDirectDraw4 *iface, IDirectDrawSurface4 *src,
3663 IDirectDrawSurface4 **dst)
3665 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src);
3666 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3667 struct ddraw_surface *dst_impl;
3668 IDirectDrawSurface7 *dst7;
3669 HRESULT hr;
3671 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3673 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3674 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3675 if (FAILED(hr))
3677 *dst = NULL;
3678 return hr;
3680 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3681 *dst = &dst_impl->IDirectDrawSurface4_iface;
3682 IDirectDrawSurface4_AddRef(*dst);
3683 IDirectDrawSurface7_Release(dst7);
3685 return hr;
3688 static HRESULT WINAPI ddraw2_DuplicateSurface(IDirectDraw2 *iface,
3689 IDirectDrawSurface *src, IDirectDrawSurface **dst)
3691 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3692 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3693 struct ddraw_surface *dst_impl;
3694 IDirectDrawSurface7 *dst7;
3695 HRESULT hr;
3697 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3699 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3700 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3701 if (FAILED(hr))
3702 return hr;
3703 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3704 *dst = &dst_impl->IDirectDrawSurface_iface;
3705 IDirectDrawSurface_AddRef(*dst);
3706 IDirectDrawSurface7_Release(dst7);
3708 return hr;
3711 static HRESULT WINAPI ddraw1_DuplicateSurface(IDirectDraw *iface, IDirectDrawSurface *src,
3712 IDirectDrawSurface **dst)
3714 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3715 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3716 struct ddraw_surface *dst_impl;
3717 IDirectDrawSurface7 *dst7;
3718 HRESULT hr;
3720 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3722 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3723 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3724 if (FAILED(hr))
3725 return hr;
3726 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3727 *dst = &dst_impl->IDirectDrawSurface_iface;
3728 IDirectDrawSurface_AddRef(*dst);
3729 IDirectDrawSurface7_Release(dst7);
3731 return hr;
3734 /*****************************************************************************
3735 * IDirect3D7::EnumDevices
3737 * The EnumDevices method for IDirect3D7. It enumerates all supported
3738 * D3D7 devices. Currently the T&L, HAL and RGB devices are enumerated.
3740 * Params:
3741 * callback: Function to call for each enumerated device
3742 * context: Pointer to pass back to the app
3744 * Returns:
3745 * D3D_OK, or the return value of the GetCaps call
3747 *****************************************************************************/
3748 static HRESULT WINAPI d3d7_EnumDevices(IDirect3D7 *iface, LPD3DENUMDEVICESCALLBACK7 callback, void *context)
3750 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
3751 D3DDEVICEDESC7 device_desc7;
3752 HRESULT hr;
3753 size_t i;
3755 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3757 if (!callback)
3758 return DDERR_INVALIDPARAMS;
3760 wined3d_mutex_lock();
3762 if (FAILED(hr = ddraw_get_d3dcaps(ddraw, &device_desc7)))
3764 wined3d_mutex_unlock();
3765 return hr;
3768 for (i = 0; i < ARRAY_SIZE(device_list7); i++)
3770 HRESULT ret;
3772 device_desc7.deviceGUID = *device_list7[i].device_guid;
3773 ret = callback(device_list7[i].interface_name, device_list7[i].device_name, &device_desc7, context);
3774 if (ret != DDENUMRET_OK)
3776 TRACE("Application cancelled the enumeration.\n");
3777 wined3d_mutex_unlock();
3778 return D3D_OK;
3782 TRACE("End of enumeration.\n");
3784 wined3d_mutex_unlock();
3786 return D3D_OK;
3789 /*****************************************************************************
3790 * IDirect3D3::EnumDevices
3792 * Enumerates all supported Direct3DDevice interfaces. This is the
3793 * implementation for Direct3D 1 to Direc3D 3, Version 7 has its own.
3795 * Versions 1, 2 and 3
3797 * Params:
3798 * callback: Application-provided routine to call for each enumerated device
3799 * Context: Pointer to pass to the callback
3801 * Returns:
3802 * D3D_OK on success,
3803 * The result of IDirect3DImpl_GetCaps if it failed
3805 *****************************************************************************/
3806 static HRESULT WINAPI d3d3_EnumDevices(IDirect3D3 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3808 static CHAR wined3d_description[] = "Wine D3DDevice using WineD3D and OpenGL";
3810 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3811 D3DDEVICEDESC device_desc1, hal_desc, hel_desc;
3812 D3DDEVICEDESC7 device_desc7;
3813 HRESULT hr;
3815 /* Some games (Motoracer 2 demo) have the bad idea to modify the device
3816 * name string. Let's put the string in a sufficiently sized array in
3817 * writable memory. */
3818 char device_name[50];
3819 strcpy(device_name,"Direct3D HEL");
3821 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3823 if (!callback)
3824 return DDERR_INVALIDPARAMS;
3826 wined3d_mutex_lock();
3828 if (FAILED(hr = ddraw_get_d3dcaps(ddraw, &device_desc7)))
3830 wined3d_mutex_unlock();
3831 return hr;
3833 ddraw_d3dcaps1_from_7(&device_desc1, &device_desc7);
3835 /* Do I have to enumerate the reference id? Note from old d3d7:
3836 * "It seems that enumerating the reference IID on Direct3D 1 games
3837 * (AvP / Motoracer2) breaks them". So do not enumerate this iid in V1
3839 * There's a registry key HKLM\Software\Microsoft\Direct3D\Drivers,
3840 * EnumReference which enables / disables enumerating the reference
3841 * rasterizer. It's a DWORD, 0 means disabled, 2 means enabled. The
3842 * enablerefrast.reg and disablerefrast.reg files in the DirectX 7.0 sdk
3843 * demo directory suggest this.
3845 * Some games(GTA 2) seem to use the second enumerated device, so I have
3846 * to enumerate at least 2 devices. So enumerate the reference device to
3847 * have 2 devices.
3849 * Other games (Rollcage) tell emulation and hal device apart by certain
3850 * flags. Rollcage expects D3DPTEXTURECAPS_POW2 to be set (yeah, it is a
3851 * limitation flag), and it refuses all devices that have the perspective
3852 * flag set. This way it refuses the emulation device, and HAL devices
3853 * never have POW2 unset in d3d7 on windows. */
3854 if (ddraw->d3dversion != 1)
3856 static CHAR reference_description[] = "RGB Direct3D emulation";
3858 TRACE("Enumerating WineD3D D3DDevice interface.\n");
3859 hal_desc = device_desc1;
3860 hel_desc = device_desc1;
3861 /* The rgb device has the pow2 flag set in the hel caps, but not in the hal caps. */
3862 hal_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3863 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3864 hal_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3865 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3866 /* RGB, RAMP and MMX devices have a HAL dcmColorModel of 0 */
3867 hal_desc.dcmColorModel = 0;
3868 /* RGB, RAMP and MMX devices cannot report HAL hardware flags */
3869 hal_desc.dwFlags = 0;
3871 hr = callback((GUID *)&IID_IDirect3DRGBDevice, reference_description,
3872 device_name, &hal_desc, &hel_desc, context);
3873 if (hr != D3DENUMRET_OK)
3875 TRACE("Application cancelled the enumeration.\n");
3876 wined3d_mutex_unlock();
3877 return D3D_OK;
3881 strcpy(device_name,"Direct3D HAL");
3883 TRACE("Enumerating HAL Direct3D device.\n");
3884 hal_desc = device_desc1;
3885 hel_desc = device_desc1;
3887 /* The hal device does not have the pow2 flag set in hel, but in hal. */
3888 hel_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3889 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3890 hel_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3891 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3892 /* HAL devices have a HEL dcmColorModel of 0 */
3893 hel_desc.dcmColorModel = 0;
3895 hr = callback((GUID *)&IID_IDirect3DHALDevice, wined3d_description,
3896 device_name, &hal_desc, &hel_desc, context);
3897 if (hr != D3DENUMRET_OK)
3899 TRACE("Application cancelled the enumeration.\n");
3900 wined3d_mutex_unlock();
3901 return D3D_OK;
3904 TRACE("End of enumeration.\n");
3906 wined3d_mutex_unlock();
3908 return D3D_OK;
3911 static HRESULT WINAPI d3d2_EnumDevices(IDirect3D2 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3913 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3915 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3917 return d3d3_EnumDevices(&ddraw->IDirect3D3_iface, callback, context);
3920 static HRESULT WINAPI d3d1_EnumDevices(IDirect3D *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3922 struct ddraw *ddraw = impl_from_IDirect3D(iface);
3924 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3926 return d3d3_EnumDevices(&ddraw->IDirect3D3_iface, callback, context);
3929 /*****************************************************************************
3930 * IDirect3D3::CreateLight
3932 * Creates an IDirect3DLight interface. This interface is used in
3933 * Direct3D3 or earlier for lighting. In Direct3D7 it has been replaced
3934 * by the DIRECT3DLIGHT7 structure. Wine's Direct3DLight implementation
3935 * uses the IDirect3DDevice7 interface with D3D7 lights.
3937 * Versions 1, 2 and 3
3939 * Params:
3940 * light: Address to store the new interface pointer
3941 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
3942 * Must be NULL
3944 * Returns:
3945 * D3D_OK on success
3946 * DDERR_OUTOFMEMORY if memory allocation failed
3947 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
3949 *****************************************************************************/
3950 static HRESULT WINAPI d3d3_CreateLight(IDirect3D3 *iface, IDirect3DLight **light,
3951 IUnknown *outer_unknown)
3953 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3954 struct d3d_light *object;
3956 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3958 if (outer_unknown)
3959 return CLASS_E_NOAGGREGATION;
3961 if (!(object = heap_alloc_zero(sizeof(*object))))
3963 ERR("Failed to allocate light memory.\n");
3964 return DDERR_OUTOFMEMORY;
3967 d3d_light_init(object, ddraw);
3969 TRACE("Created light %p.\n", object);
3970 *light = &object->IDirect3DLight_iface;
3972 return D3D_OK;
3975 static HRESULT WINAPI d3d2_CreateLight(IDirect3D2 *iface, IDirect3DLight **light, IUnknown *outer_unknown)
3977 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3979 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3981 return d3d3_CreateLight(&ddraw->IDirect3D3_iface, light, outer_unknown);
3984 static HRESULT WINAPI d3d1_CreateLight(IDirect3D *iface, IDirect3DLight **light, IUnknown *outer_unknown)
3986 struct ddraw *ddraw = impl_from_IDirect3D(iface);
3988 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3990 return d3d3_CreateLight(&ddraw->IDirect3D3_iface, light, outer_unknown);
3993 /*****************************************************************************
3994 * IDirect3D3::CreateMaterial
3996 * Creates an IDirect3DMaterial interface. This interface is used by Direct3D3
3997 * and older versions. The IDirect3DMaterial implementation wraps its
3998 * functionality to IDirect3DDevice7::SetMaterial and friends.
4000 * Versions 1, 2 and 3
4002 * Params:
4003 * material: Address to store the new interface's pointer to
4004 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4005 * Must be NULL
4007 * Returns:
4008 * D3D_OK on success
4009 * DDERR_OUTOFMEMORY if memory allocation failed
4010 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4012 *****************************************************************************/
4013 static HRESULT WINAPI d3d3_CreateMaterial(IDirect3D3 *iface, IDirect3DMaterial3 **material,
4014 IUnknown *outer_unknown)
4016 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4017 struct d3d_material *object;
4019 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4021 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4023 object = d3d_material_create(ddraw);
4024 if (!object)
4026 ERR("Failed to allocate material memory.\n");
4027 return DDERR_OUTOFMEMORY;
4030 TRACE("Created material %p.\n", object);
4031 *material = &object->IDirect3DMaterial3_iface;
4033 return D3D_OK;
4036 static HRESULT WINAPI d3d2_CreateMaterial(IDirect3D2 *iface, IDirect3DMaterial2 **material,
4037 IUnknown *outer_unknown)
4039 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4040 struct d3d_material *object;
4042 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4044 object = d3d_material_create(ddraw);
4045 if (!object)
4047 ERR("Failed to allocate material memory.\n");
4048 return DDERR_OUTOFMEMORY;
4051 TRACE("Created material %p.\n", object);
4052 *material = &object->IDirect3DMaterial2_iface;
4054 return D3D_OK;
4057 static HRESULT WINAPI d3d1_CreateMaterial(IDirect3D *iface, IDirect3DMaterial **material,
4058 IUnknown *outer_unknown)
4060 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4061 struct d3d_material *object;
4063 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4065 object = d3d_material_create(ddraw);
4066 if (!object)
4068 ERR("Failed to allocate material memory.\n");
4069 return DDERR_OUTOFMEMORY;
4072 TRACE("Created material %p.\n", object);
4073 *material = &object->IDirect3DMaterial_iface;
4075 return D3D_OK;
4078 /*****************************************************************************
4079 * IDirect3D3::CreateViewport
4081 * Creates an IDirect3DViewport interface. This interface is used
4082 * by Direct3D and earlier versions for Viewport management. In Direct3D7
4083 * it has been replaced by a viewport structure and
4084 * IDirect3DDevice7::*Viewport. Wine's IDirect3DViewport implementation
4085 * uses the IDirect3DDevice7 methods for its functionality
4087 * Params:
4088 * Viewport: Address to store the new interface pointer
4089 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4090 * Must be NULL
4092 * Returns:
4093 * D3D_OK on success
4094 * DDERR_OUTOFMEMORY if memory allocation failed
4095 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4097 *****************************************************************************/
4098 static HRESULT WINAPI d3d3_CreateViewport(IDirect3D3 *iface, IDirect3DViewport3 **viewport,
4099 IUnknown *outer_unknown)
4101 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4102 struct d3d_viewport *object;
4104 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4106 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4108 if (!(object = heap_alloc_zero(sizeof(*object))))
4110 ERR("Failed to allocate viewport memory.\n");
4111 return DDERR_OUTOFMEMORY;
4114 d3d_viewport_init(object, ddraw);
4116 TRACE("Created viewport %p.\n", object);
4117 *viewport = &object->IDirect3DViewport3_iface;
4119 return D3D_OK;
4122 static HRESULT WINAPI d3d2_CreateViewport(IDirect3D2 *iface, IDirect3DViewport2 **viewport, IUnknown *outer_unknown)
4124 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4126 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4128 return d3d3_CreateViewport(&ddraw->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4129 outer_unknown);
4132 static HRESULT WINAPI d3d1_CreateViewport(IDirect3D *iface, IDirect3DViewport **viewport, IUnknown *outer_unknown)
4134 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4136 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4138 return d3d3_CreateViewport(&ddraw->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4139 outer_unknown);
4142 static HRESULT ddraw_find_device(struct ddraw *ddraw, const D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr,
4143 unsigned int guid_count, const GUID * const *guids, DWORD device_desc_size)
4145 struct ddraw_find_device_result_v1
4147 DWORD size;
4148 GUID guid;
4149 D3DDEVICEDESC_V1 hw_desc;
4150 D3DDEVICEDESC_V1 sw_desc;
4151 } *fdr1;
4152 struct ddraw_find_device_result_v2
4154 DWORD size;
4155 GUID guid;
4156 D3DDEVICEDESC_V2 hw_desc;
4157 D3DDEVICEDESC_V2 sw_desc;
4158 } *fdr2;
4159 D3DDEVICEDESC7 desc7;
4160 D3DDEVICEDESC desc1;
4161 unsigned int i;
4162 HRESULT hr;
4164 TRACE("ddraw %p, fds %p, fdr %p, guid_count %u, guids %p, device_desc_size %u.\n",
4165 ddraw, fds, fdr, guid_count, guids, device_desc_size);
4167 if (!fds || !fdr)
4168 return DDERR_INVALIDPARAMS;
4170 if (fds->dwSize != sizeof(*fds))
4172 WARN("Got invalid search structure size %u.\n", fds->dwSize);
4173 return DDERR_INVALIDPARAMS;
4176 if (fdr->dwSize != sizeof(*fdr) && fdr->dwSize != sizeof(*fdr2) && fdr->dwSize != sizeof(*fdr1))
4178 WARN("Got invalid result structure size %u.\n", fdr->dwSize);
4179 return DDERR_INVALIDPARAMS;
4182 if (fds->dwFlags & D3DFDS_COLORMODEL)
4183 WARN("Ignoring colour model %#x.\n", fds->dcmColorModel);
4185 if (fds->dwFlags & D3DFDS_GUID)
4187 BOOL found = FALSE;
4189 TRACE("Trying to match GUID %s.\n", debugstr_guid(&fds->guid));
4191 if ((ddraw->flags & DDRAW_NO3D) && IsEqualGUID(&fds->guid, &IID_IDirect3DHALDevice))
4193 WARN("HAL device not available without 3D support.\n");
4194 return DDERR_NOTFOUND;
4197 for (i = 0; i < guid_count; ++i)
4199 if (IsEqualGUID(guids[i], &fds->guid))
4201 found = TRUE;
4202 break;
4206 if (!found)
4208 WARN("Failed to match GUID %s.\n", debugstr_guid(&fds->guid));
4209 return DDERR_NOTFOUND;
4213 /* Get the caps */
4214 if (FAILED(hr = ddraw_get_d3dcaps(ddraw, &desc7)))
4215 return hr;
4217 /* Now return our own GUID */
4218 ddraw_d3dcaps1_from_7(&desc1, &desc7);
4219 fdr->guid = IID_D3DDEVICE_WineD3D;
4221 /* Note that "device_desc_size" doesn't necessarily have any relation to
4222 * the actual structure size. However, this matches the behaviour of
4223 * Windows since at least Windows 2000. */
4224 if (fdr->dwSize == sizeof(*fdr1))
4226 fdr1 = (struct ddraw_find_device_result_v1 *)fdr;
4227 memcpy(&fdr1->hw_desc, &desc1, sizeof(fdr1->hw_desc));
4228 fdr1->hw_desc.dwSize = device_desc_size;
4229 memcpy(&fdr1->sw_desc, &desc1, sizeof(fdr1->sw_desc));
4230 fdr1->sw_desc.dwSize = device_desc_size;
4232 else if (fdr->dwSize == sizeof(*fdr2))
4234 fdr2 = (struct ddraw_find_device_result_v2 *)fdr;
4235 memcpy(&fdr2->hw_desc, &desc1, sizeof(fdr2->hw_desc));
4236 fdr2->hw_desc.dwSize = device_desc_size;
4237 memcpy(&fdr2->sw_desc, &desc1, sizeof(fdr2->sw_desc));
4238 fdr2->sw_desc.dwSize = device_desc_size;
4240 else
4242 fdr->ddHwDesc = desc1;
4243 fdr->ddHwDesc.dwSize = device_desc_size;
4244 fdr->ddSwDesc = desc1;
4245 fdr->ddSwDesc.dwSize = device_desc_size;
4248 TRACE("Returning Wine's wined3d device with (undumped) capabilities.\n");
4250 return D3D_OK;
4253 static HRESULT WINAPI d3d3_FindDevice(IDirect3D3 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4255 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4256 static const GUID * const guids[] =
4258 &IID_D3DDEVICE_WineD3D,
4259 &IID_IDirect3DHALDevice,
4260 &IID_IDirect3DRGBDevice,
4263 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4265 return ddraw_find_device(ddraw, fds, fdr, ARRAY_SIZE(guids), guids, sizeof(D3DDEVICEDESC_V3));
4268 static HRESULT WINAPI d3d2_FindDevice(IDirect3D2 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4270 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4271 static const GUID * const guids[] =
4273 &IID_D3DDEVICE_WineD3D,
4274 &IID_IDirect3DHALDevice,
4275 &IID_IDirect3DMMXDevice,
4276 &IID_IDirect3DRGBDevice,
4277 &IID_IDirect3DRampDevice,
4280 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4282 return ddraw_find_device(ddraw, fds, fdr, ARRAY_SIZE(guids), guids, sizeof(D3DDEVICEDESC_V2));
4285 static HRESULT WINAPI d3d1_FindDevice(IDirect3D *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4287 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4288 static const GUID * const guids[] =
4290 &IID_D3DDEVICE_WineD3D,
4291 &IID_IDirect3DHALDevice,
4292 &IID_IDirect3DRGBDevice,
4293 &IID_IDirect3DRampDevice,
4296 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4298 return ddraw_find_device(ddraw, fds, fdr, ARRAY_SIZE(guids), guids, sizeof(D3DDEVICEDESC_V1));
4301 /*****************************************************************************
4302 * IDirect3D7::CreateDevice
4304 * Creates an IDirect3DDevice7 interface.
4306 * Versions 2, 3 and 7. IDirect3DDevice 1 interfaces are interfaces to
4307 * DirectDraw surfaces and are created with
4308 * IDirectDrawSurface::QueryInterface. This method uses CreateDevice to
4309 * create the device object and QueryInterfaces for IDirect3DDevice
4311 * Params:
4312 * refiid: IID of the device to create
4313 * Surface: Initial rendertarget
4314 * Device: Address to return the interface pointer
4316 * Returns:
4317 * D3D_OK on success
4318 * DDERR_OUTOFMEMORY if memory allocation failed
4319 * DDERR_INVALIDPARAMS if a device exists already
4321 *****************************************************************************/
4322 static HRESULT WINAPI d3d7_CreateDevice(IDirect3D7 *iface, REFCLSID riid,
4323 IDirectDrawSurface7 *surface, IDirect3DDevice7 **device)
4325 struct ddraw_surface *target = unsafe_impl_from_IDirectDrawSurface7(surface);
4326 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4327 struct d3d_device *object;
4328 HRESULT hr;
4330 TRACE("iface %p, riid %s, surface %p, device %p.\n", iface, debugstr_guid(riid), surface, device);
4332 wined3d_mutex_lock();
4333 if (SUCCEEDED(hr = d3d_device_create(ddraw, riid, target, (IUnknown *)surface, 7, &object, NULL)))
4335 *device = &object->IDirect3DDevice7_iface;
4337 else
4339 WARN("Failed to create device, hr %#x.\n", hr);
4340 *device = NULL;
4342 wined3d_mutex_unlock();
4344 return hr;
4347 static HRESULT WINAPI d3d3_CreateDevice(IDirect3D3 *iface, REFCLSID riid,
4348 IDirectDrawSurface4 *surface, IDirect3DDevice3 **device, IUnknown *outer_unknown)
4350 struct ddraw_surface *surface_impl = unsafe_impl_from_IDirectDrawSurface4(surface);
4351 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4352 struct d3d_device *device_impl;
4353 HRESULT hr;
4355 TRACE("iface %p, riid %s, surface %p, device %p, outer_unknown %p.\n",
4356 iface, debugstr_guid(riid), surface, device, outer_unknown);
4358 if (outer_unknown)
4359 return CLASS_E_NOAGGREGATION;
4361 wined3d_mutex_lock();
4362 if (SUCCEEDED(hr = d3d_device_create(ddraw, riid, surface_impl, (IUnknown *)surface, 3, &device_impl, NULL)))
4364 *device = &device_impl->IDirect3DDevice3_iface;
4366 else
4368 WARN("Failed to create device, hr %#x.\n", hr);
4369 *device = NULL;
4371 wined3d_mutex_unlock();
4373 return hr;
4376 static HRESULT WINAPI d3d2_CreateDevice(IDirect3D2 *iface, REFCLSID riid,
4377 IDirectDrawSurface *surface, IDirect3DDevice2 **device)
4379 struct ddraw_surface *surface_impl = unsafe_impl_from_IDirectDrawSurface(surface);
4380 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4381 struct d3d_device *device_impl;
4382 HRESULT hr;
4384 TRACE("iface %p, riid %s, surface %p, device %p.\n",
4385 iface, debugstr_guid(riid), surface, device);
4387 wined3d_mutex_lock();
4388 if (SUCCEEDED(hr = d3d_device_create(ddraw, riid, surface_impl, (IUnknown *)surface, 2, &device_impl, NULL)))
4390 *device = &device_impl->IDirect3DDevice2_iface;
4392 else
4394 WARN("Failed to create device, hr %#x.\n", hr);
4395 *device = NULL;
4397 wined3d_mutex_unlock();
4399 return hr;
4402 /*****************************************************************************
4403 * IDirect3D7::CreateVertexBuffer
4405 * Creates a new vertex buffer object and returns a IDirect3DVertexBuffer7
4406 * interface.
4408 * Versions 3 and 7
4410 * Params:
4411 * desc: Requested Vertex buffer properties
4412 * vertex_buffer: Address to return the interface pointer at
4413 * flags: Some flags, should be 0
4415 * Returns
4416 * D3D_OK on success
4417 * DDERR_OUTOFMEMORY if memory allocation failed
4418 * DDERR_INVALIDPARAMS if desc or vertex_buffer is NULL
4420 *****************************************************************************/
4421 static HRESULT WINAPI d3d7_CreateVertexBuffer(IDirect3D7 *iface, D3DVERTEXBUFFERDESC *desc,
4422 IDirect3DVertexBuffer7 **vertex_buffer, DWORD flags)
4424 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4425 struct d3d_vertex_buffer *object;
4426 HRESULT hr;
4428 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x.\n",
4429 iface, desc, vertex_buffer, flags);
4431 if (!vertex_buffer || !desc) return DDERR_INVALIDPARAMS;
4433 hr = d3d_vertex_buffer_create(&object, ddraw, desc);
4434 if (hr == D3D_OK)
4436 TRACE("Created vertex buffer %p.\n", object);
4437 *vertex_buffer = &object->IDirect3DVertexBuffer7_iface;
4439 else
4440 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4442 return hr;
4445 static HRESULT WINAPI d3d3_CreateVertexBuffer(IDirect3D3 *iface, D3DVERTEXBUFFERDESC *desc,
4446 IDirect3DVertexBuffer **vertex_buffer, DWORD flags, IUnknown *outer_unknown)
4448 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4449 struct d3d_vertex_buffer *object;
4450 HRESULT hr;
4452 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x, outer_unknown %p.\n",
4453 iface, desc, vertex_buffer, flags, outer_unknown);
4455 if (outer_unknown)
4456 return CLASS_E_NOAGGREGATION;
4457 if (!vertex_buffer || !desc)
4458 return DDERR_INVALIDPARAMS;
4460 hr = d3d_vertex_buffer_create(&object, ddraw, desc);
4461 if (hr == D3D_OK)
4463 TRACE("Created vertex buffer %p.\n", object);
4464 *vertex_buffer = (IDirect3DVertexBuffer *)&object->IDirect3DVertexBuffer7_iface;
4466 else
4467 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4469 return hr;
4472 /*****************************************************************************
4473 * IDirect3D7::EnumZBufferFormats
4475 * Enumerates all supported Z buffer pixel formats
4477 * Versions 3 and 7
4479 * Params:
4480 * device_iid:
4481 * callback: callback to call for each pixel format
4482 * context: Pointer to pass back to the callback
4484 * Returns:
4485 * D3D_OK on success
4486 * DDERR_INVALIDPARAMS if callback is NULL
4488 *****************************************************************************/
4489 static HRESULT WINAPI d3d7_EnumZBufferFormats(IDirect3D7 *iface, REFCLSID device_iid,
4490 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4492 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4493 struct wined3d_display_mode mode;
4494 enum wined3d_device_type type;
4495 unsigned int i;
4496 HRESULT hr;
4498 /* Order matters. Specifically, BattleZone II (full version) expects the
4499 * 16-bit depth formats to be listed before the 24 and 32 ones. */
4500 static const enum wined3d_format_id formats[] =
4502 WINED3DFMT_S1_UINT_D15_UNORM,
4503 WINED3DFMT_D16_UNORM,
4504 WINED3DFMT_X8D24_UNORM,
4505 WINED3DFMT_S4X4_UINT_D24_UNORM,
4506 WINED3DFMT_D24_UNORM_S8_UINT,
4507 WINED3DFMT_D32_UNORM,
4510 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4511 iface, debugstr_guid(device_iid), callback, context);
4513 if (!callback) return DDERR_INVALIDPARAMS;
4515 if (IsEqualGUID(device_iid, &IID_IDirect3DHALDevice)
4516 || IsEqualGUID(device_iid, &IID_IDirect3DTnLHalDevice)
4517 || IsEqualGUID(device_iid, &IID_D3DDEVICE_WineD3D))
4519 TRACE("Asked for HAL device.\n");
4520 type = WINED3D_DEVICE_TYPE_HAL;
4522 else if (IsEqualGUID(device_iid, &IID_IDirect3DRGBDevice)
4523 || IsEqualGUID(device_iid, &IID_IDirect3DMMXDevice))
4525 TRACE("Asked for SW device.\n");
4526 type = WINED3D_DEVICE_TYPE_SW;
4528 else if (IsEqualGUID(device_iid, &IID_IDirect3DRefDevice))
4530 TRACE("Asked for REF device.\n");
4531 type = WINED3D_DEVICE_TYPE_REF;
4533 else if (IsEqualGUID(device_iid, &IID_IDirect3DNullDevice))
4535 TRACE("Asked for NULLREF device.\n");
4536 type = WINED3D_DEVICE_TYPE_NULLREF;
4538 else
4540 FIXME("Unexpected device GUID %s.\n", debugstr_guid(device_iid));
4541 type = WINED3D_DEVICE_TYPE_HAL;
4544 wined3d_mutex_lock();
4545 /* We need an adapter format from somewhere to please wined3d and WGL.
4546 * Use the current display mode. So far all cards offer the same depth
4547 * stencil format for all modes, but if some do not and applications do
4548 * not like that we'll have to find some workaround, like iterating over
4549 * all imaginable formats and collecting all the depth stencil formats we
4550 * can get. */
4551 if (FAILED(hr = wined3d_output_get_display_mode(ddraw->wined3d_output, &mode, NULL)))
4553 ERR("Failed to get display mode, hr %#x.\n", hr);
4554 wined3d_mutex_unlock();
4555 return hr;
4558 for (i = 0; i < ARRAY_SIZE(formats); ++i)
4560 if (SUCCEEDED(wined3d_check_device_format(ddraw->wined3d, ddraw->wined3d_adapter, type,
4561 mode.format_id, 0, WINED3D_BIND_DEPTH_STENCIL, WINED3D_RTYPE_TEXTURE_2D, formats[i])))
4563 DDPIXELFORMAT pformat;
4565 memset(&pformat, 0, sizeof(pformat));
4566 pformat.dwSize = sizeof(pformat);
4567 ddrawformat_from_wined3dformat(&pformat, formats[i]);
4569 TRACE("Enumerating wined3d format %#x.\n", formats[i]);
4570 hr = callback(&pformat, context);
4571 if (hr != DDENUMRET_OK)
4573 TRACE("Format enumeration cancelled by application.\n");
4574 wined3d_mutex_unlock();
4575 return D3D_OK;
4580 /* Historically some windows drivers used dwZBufferBitDepth=24 for WINED3DFMT_X8D24_UNORM,
4581 * while others used dwZBufferBitDepth=32. In either case the pitch matches a 32 bits per
4582 * pixel format, so we use dwZBufferBitDepth=32. Some games expect 24. Windows Vista and
4583 * newer enumerate both versions, so we do the same(bug 22434) */
4584 if (SUCCEEDED(wined3d_check_device_format(ddraw->wined3d, ddraw->wined3d_adapter, type, mode.format_id,
4585 0, WINED3D_BIND_DEPTH_STENCIL, WINED3D_RTYPE_TEXTURE_2D, WINED3DFMT_X8D24_UNORM)))
4587 DDPIXELFORMAT x8d24 =
4589 sizeof(x8d24), DDPF_ZBUFFER, 0,
4590 {24}, {0x00000000}, {0x00ffffff}, {0x00000000}
4592 TRACE("Enumerating WINED3DFMT_X8D24_UNORM, dwZBufferBitDepth=24 version\n");
4593 callback(&x8d24, context);
4596 TRACE("End of enumeration.\n");
4598 wined3d_mutex_unlock();
4600 return D3D_OK;
4603 static HRESULT WINAPI d3d3_EnumZBufferFormats(IDirect3D3 *iface, REFCLSID device_iid,
4604 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4606 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4608 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4609 iface, debugstr_guid(device_iid), callback, context);
4611 return d3d7_EnumZBufferFormats(&ddraw->IDirect3D7_iface, device_iid, callback, context);
4614 /*****************************************************************************
4615 * IDirect3D7::EvictManagedTextures
4617 * Removes all managed textures (=surfaces with DDSCAPS2_TEXTUREMANAGE or
4618 * DDSCAPS2_D3DTEXTUREMANAGE caps) to be removed from video memory.
4620 * Versions 3 and 7
4622 * Returns:
4623 * D3D_OK, because it's a stub
4625 *****************************************************************************/
4626 static HRESULT WINAPI d3d7_EvictManagedTextures(IDirect3D7 *iface)
4628 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4630 TRACE("iface %p!\n", iface);
4632 wined3d_mutex_lock();
4633 if (ddraw->flags & DDRAW_D3D_INITIALIZED)
4634 wined3d_device_evict_managed_resources(ddraw->wined3d_device);
4635 wined3d_mutex_unlock();
4637 return D3D_OK;
4640 static HRESULT WINAPI d3d3_EvictManagedTextures(IDirect3D3 *iface)
4642 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4644 TRACE("iface %p.\n", iface);
4646 return d3d7_EvictManagedTextures(&ddraw->IDirect3D7_iface);
4649 /*****************************************************************************
4650 * IDirectDraw7 VTable
4651 *****************************************************************************/
4652 static const struct IDirectDraw7Vtbl ddraw7_vtbl =
4654 /* IUnknown */
4655 ddraw7_QueryInterface,
4656 ddraw7_AddRef,
4657 ddraw7_Release,
4658 /* IDirectDraw */
4659 ddraw7_Compact,
4660 ddraw7_CreateClipper,
4661 ddraw7_CreatePalette,
4662 ddraw7_CreateSurface,
4663 ddraw7_DuplicateSurface,
4664 ddraw7_EnumDisplayModes,
4665 ddraw7_EnumSurfaces,
4666 ddraw7_FlipToGDISurface,
4667 ddraw7_GetCaps,
4668 ddraw7_GetDisplayMode,
4669 ddraw7_GetFourCCCodes,
4670 ddraw7_GetGDISurface,
4671 ddraw7_GetMonitorFrequency,
4672 ddraw7_GetScanLine,
4673 ddraw7_GetVerticalBlankStatus,
4674 ddraw7_Initialize,
4675 ddraw7_RestoreDisplayMode,
4676 ddraw7_SetCooperativeLevel,
4677 ddraw7_SetDisplayMode,
4678 ddraw7_WaitForVerticalBlank,
4679 /* IDirectDraw2 */
4680 ddraw7_GetAvailableVidMem,
4681 /* IDirectDraw3 */
4682 ddraw7_GetSurfaceFromDC,
4683 /* IDirectDraw4 */
4684 ddraw7_RestoreAllSurfaces,
4685 ddraw7_TestCooperativeLevel,
4686 ddraw7_GetDeviceIdentifier,
4687 /* IDirectDraw7 */
4688 ddraw7_StartModeTest,
4689 ddraw7_EvaluateMode
4692 static const struct IDirectDraw4Vtbl ddraw4_vtbl =
4694 /* IUnknown */
4695 ddraw4_QueryInterface,
4696 ddraw4_AddRef,
4697 ddraw4_Release,
4698 /* IDirectDraw */
4699 ddraw4_Compact,
4700 ddraw4_CreateClipper,
4701 ddraw4_CreatePalette,
4702 ddraw4_CreateSurface,
4703 ddraw4_DuplicateSurface,
4704 ddraw4_EnumDisplayModes,
4705 ddraw4_EnumSurfaces,
4706 ddraw4_FlipToGDISurface,
4707 ddraw4_GetCaps,
4708 ddraw4_GetDisplayMode,
4709 ddraw4_GetFourCCCodes,
4710 ddraw4_GetGDISurface,
4711 ddraw4_GetMonitorFrequency,
4712 ddraw4_GetScanLine,
4713 ddraw4_GetVerticalBlankStatus,
4714 ddraw4_Initialize,
4715 ddraw4_RestoreDisplayMode,
4716 ddraw4_SetCooperativeLevel,
4717 ddraw4_SetDisplayMode,
4718 ddraw4_WaitForVerticalBlank,
4719 /* IDirectDraw2 */
4720 ddraw4_GetAvailableVidMem,
4721 /* IDirectDraw3 */
4722 ddraw4_GetSurfaceFromDC,
4723 /* IDirectDraw4 */
4724 ddraw4_RestoreAllSurfaces,
4725 ddraw4_TestCooperativeLevel,
4726 ddraw4_GetDeviceIdentifier,
4729 static const struct IDirectDraw2Vtbl ddraw2_vtbl =
4731 /* IUnknown */
4732 ddraw2_QueryInterface,
4733 ddraw2_AddRef,
4734 ddraw2_Release,
4735 /* IDirectDraw */
4736 ddraw2_Compact,
4737 ddraw2_CreateClipper,
4738 ddraw2_CreatePalette,
4739 ddraw2_CreateSurface,
4740 ddraw2_DuplicateSurface,
4741 ddraw2_EnumDisplayModes,
4742 ddraw2_EnumSurfaces,
4743 ddraw2_FlipToGDISurface,
4744 ddraw2_GetCaps,
4745 ddraw2_GetDisplayMode,
4746 ddraw2_GetFourCCCodes,
4747 ddraw2_GetGDISurface,
4748 ddraw2_GetMonitorFrequency,
4749 ddraw2_GetScanLine,
4750 ddraw2_GetVerticalBlankStatus,
4751 ddraw2_Initialize,
4752 ddraw2_RestoreDisplayMode,
4753 ddraw2_SetCooperativeLevel,
4754 ddraw2_SetDisplayMode,
4755 ddraw2_WaitForVerticalBlank,
4756 /* IDirectDraw2 */
4757 ddraw2_GetAvailableVidMem,
4760 /* Bad Mojo Redux expects this vtbl to be writable. */
4761 static struct IDirectDrawVtbl ddraw1_vtbl =
4763 /* IUnknown */
4764 ddraw1_QueryInterface,
4765 ddraw1_AddRef,
4766 ddraw1_Release,
4767 /* IDirectDraw */
4768 ddraw1_Compact,
4769 ddraw1_CreateClipper,
4770 ddraw1_CreatePalette,
4771 ddraw1_CreateSurface,
4772 ddraw1_DuplicateSurface,
4773 ddraw1_EnumDisplayModes,
4774 ddraw1_EnumSurfaces,
4775 ddraw1_FlipToGDISurface,
4776 ddraw1_GetCaps,
4777 ddraw1_GetDisplayMode,
4778 ddraw1_GetFourCCCodes,
4779 ddraw1_GetGDISurface,
4780 ddraw1_GetMonitorFrequency,
4781 ddraw1_GetScanLine,
4782 ddraw1_GetVerticalBlankStatus,
4783 ddraw1_Initialize,
4784 ddraw1_RestoreDisplayMode,
4785 ddraw1_SetCooperativeLevel,
4786 ddraw1_SetDisplayMode,
4787 ddraw1_WaitForVerticalBlank,
4790 static const struct IDirect3D7Vtbl d3d7_vtbl =
4792 /* IUnknown methods */
4793 d3d7_QueryInterface,
4794 d3d7_AddRef,
4795 d3d7_Release,
4796 /* IDirect3D7 methods */
4797 d3d7_EnumDevices,
4798 d3d7_CreateDevice,
4799 d3d7_CreateVertexBuffer,
4800 d3d7_EnumZBufferFormats,
4801 d3d7_EvictManagedTextures
4804 static const struct IDirect3D3Vtbl d3d3_vtbl =
4806 /* IUnknown methods */
4807 d3d3_QueryInterface,
4808 d3d3_AddRef,
4809 d3d3_Release,
4810 /* IDirect3D3 methods */
4811 d3d3_EnumDevices,
4812 d3d3_CreateLight,
4813 d3d3_CreateMaterial,
4814 d3d3_CreateViewport,
4815 d3d3_FindDevice,
4816 d3d3_CreateDevice,
4817 d3d3_CreateVertexBuffer,
4818 d3d3_EnumZBufferFormats,
4819 d3d3_EvictManagedTextures
4822 static const struct IDirect3D2Vtbl d3d2_vtbl =
4824 /* IUnknown methods */
4825 d3d2_QueryInterface,
4826 d3d2_AddRef,
4827 d3d2_Release,
4828 /* IDirect3D2 methods */
4829 d3d2_EnumDevices,
4830 d3d2_CreateLight,
4831 d3d2_CreateMaterial,
4832 d3d2_CreateViewport,
4833 d3d2_FindDevice,
4834 d3d2_CreateDevice
4837 static const struct IDirect3DVtbl d3d1_vtbl =
4839 /* IUnknown methods */
4840 d3d1_QueryInterface,
4841 d3d1_AddRef,
4842 d3d1_Release,
4843 /* IDirect3D methods */
4844 d3d1_Initialize,
4845 d3d1_EnumDevices,
4846 d3d1_CreateLight,
4847 d3d1_CreateMaterial,
4848 d3d1_CreateViewport,
4849 d3d1_FindDevice
4852 /*****************************************************************************
4853 * ddraw_find_decl
4855 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
4856 * if none was found.
4858 * This function is in ddraw.c and the DDraw object space because D3D7
4859 * vertex buffers are created using the IDirect3D interface to the ddraw
4860 * object, so they can be valid across D3D devices(theoretically. The ddraw
4861 * object also owns the wined3d device
4863 * Parameters:
4864 * This: Device
4865 * fvf: Fvf to find the decl for
4867 * Returns:
4868 * NULL in case of an error, the vertex declaration for the FVF otherwise.
4870 *****************************************************************************/
4871 struct wined3d_vertex_declaration *ddraw_find_decl(struct ddraw *This, DWORD fvf)
4873 struct wined3d_vertex_declaration *pDecl = NULL;
4874 HRESULT hr;
4875 int p, low, high; /* deliberately signed */
4876 struct FvfToDecl *convertedDecls = This->decls;
4878 TRACE("Searching for declaration for fvf %08x... ", fvf);
4880 low = 0;
4881 high = This->numConvertedDecls - 1;
4882 while(low <= high) {
4883 p = (low + high) >> 1;
4884 TRACE("%d ", p);
4885 if(convertedDecls[p].fvf == fvf) {
4886 TRACE("found %p\n", convertedDecls[p].decl);
4887 return convertedDecls[p].decl;
4888 } else if(convertedDecls[p].fvf < fvf) {
4889 low = p + 1;
4890 } else {
4891 high = p - 1;
4894 TRACE("not found. Creating and inserting at position %d.\n", low);
4896 hr = wined3d_vertex_declaration_create_from_fvf(This->wined3d_device,
4897 fvf, This, &ddraw_null_wined3d_parent_ops, &pDecl);
4898 if (hr != S_OK) return NULL;
4900 if (This->declArraySize == This->numConvertedDecls)
4902 unsigned int grow = max(This->declArraySize / 2, 8);
4904 if (!(convertedDecls = heap_realloc(convertedDecls,
4905 (This->numConvertedDecls + grow) * sizeof(*convertedDecls))))
4907 wined3d_vertex_declaration_decref(pDecl);
4908 return NULL;
4910 This->decls = convertedDecls;
4911 This->declArraySize += grow;
4914 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
4915 convertedDecls[low].decl = pDecl;
4916 convertedDecls[low].fvf = fvf;
4917 This->numConvertedDecls++;
4919 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
4920 return pDecl;
4923 static inline struct ddraw *ddraw_from_device_parent(struct wined3d_device_parent *device_parent)
4925 return CONTAINING_RECORD(device_parent, struct ddraw, device_parent);
4928 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
4929 struct wined3d_device *device)
4931 TRACE("device_parent %p, device %p.\n", device_parent, device);
4934 /* This is run from device_process_message() in wined3d, we can't take the
4935 * wined3d mutex. */
4936 /* FIXME: We only get mode change notifications in exclusive mode, but we
4937 * should mark surfaces as lost on mode changes in DDSCL_NORMAL mode as well. */
4938 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
4940 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4941 struct wined3d_output_desc output_desc;
4942 RECT *r;
4944 TRACE("device_parent %p.\n", device_parent);
4946 if (!(ddraw->cooperative_level & DDSCL_EXCLUSIVE) || !ddraw->swapchain_window)
4948 TRACE("Nothing to resize.\n");
4949 return;
4952 if (FAILED(wined3d_output_get_desc(ddraw->wined3d_output, &output_desc)))
4954 ERR("Failed to get output description.\n");
4955 return;
4958 r = &output_desc.desktop_rect;
4959 TRACE("Resizing window %p to %s.\n", ddraw->swapchain_window, wine_dbgstr_rect(r));
4961 if (!SetWindowPos(ddraw->swapchain_window, HWND_TOP, r->left, r->top,
4962 r->right - r->left, r->bottom - r->top, SWP_SHOWWINDOW | SWP_NOACTIVATE))
4963 ERR("Failed to resize window.\n");
4965 InterlockedCompareExchange(&ddraw->device_state, DDRAW_DEVICE_STATE_NOT_RESTORED, DDRAW_DEVICE_STATE_OK);
4968 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
4970 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
4972 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
4974 if (!activate)
4976 ddraw->device_state = DDRAW_DEVICE_STATE_LOST;
4977 exclusive_window = NULL;
4979 else
4981 InterlockedCompareExchange(&ddraw->device_state, DDRAW_DEVICE_STATE_NOT_RESTORED, DDRAW_DEVICE_STATE_LOST);
4985 void ddraw_update_lost_surfaces(struct ddraw *ddraw)
4987 struct ddraw_surface *surface;
4989 /* Railroad Tycoon 2 tries to restore surfaces from within a
4990 * WM_QUERYNEWPALETTE message handler and expects it to succeed. We
4991 * haven't received the WM_ACTIVATEAPP message by that point, so the
4992 * device state is still DDRAW_DEVICE_STATE_LOST, even though we are in
4993 * the foreground. */
4994 if (ddraw->device_state == DDRAW_DEVICE_STATE_LOST)
4996 HWND window = ddraw->focuswindow ? ddraw->focuswindow : ddraw->dest_window;
4998 if (window && GetForegroundWindow() == window)
4999 ddraw->device_state = DDRAW_DEVICE_STATE_NOT_RESTORED;
5002 if (ddraw->device_state != DDRAW_DEVICE_STATE_NOT_RESTORED)
5003 return;
5005 LIST_FOR_EACH_ENTRY(surface, &ddraw->surface_list, struct ddraw_surface, surface_list_entry)
5007 surface->is_lost = ddraw_surface_can_be_lost(surface);
5009 ddraw->device_state = DDRAW_DEVICE_STATE_OK;
5012 static HRESULT CDECL device_parent_texture_sub_resource_created(struct wined3d_device_parent *device_parent,
5013 enum wined3d_resource_type type, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
5014 void **parent, const struct wined3d_parent_ops **parent_ops)
5016 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
5017 struct ddraw_surface *ddraw_surface;
5019 TRACE("device_parent %p, type %#x, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
5020 device_parent, type, wined3d_texture, sub_resource_idx, parent, parent_ops);
5022 /* We have a swapchain or wined3d internal texture. */
5023 if (type != WINED3D_RTYPE_TEXTURE_2D || !wined3d_texture_get_parent(wined3d_texture)
5024 || wined3d_texture_get_parent(wined3d_texture) == ddraw)
5026 *parent = NULL;
5027 *parent_ops = &ddraw_null_wined3d_parent_ops;
5029 return DD_OK;
5032 if (!(ddraw_surface = heap_alloc_zero(sizeof(*ddraw_surface))))
5034 ERR("Failed to allocate surface memory.\n");
5035 return DDERR_OUTOFVIDEOMEMORY;
5038 ddraw_surface_init(ddraw_surface, ddraw, wined3d_texture, sub_resource_idx, parent_ops);
5039 *parent = ddraw_surface;
5041 ddraw_update_lost_surfaces(ddraw);
5042 list_add_head(&ddraw->surface_list, &ddraw_surface->surface_list_entry);
5044 TRACE("Created ddraw surface %p.\n", ddraw_surface);
5046 return DD_OK;
5049 static void STDMETHODCALLTYPE ddraw_frontbuffer_destroyed(void *parent)
5051 struct ddraw *ddraw = parent;
5052 ddraw->wined3d_frontbuffer = NULL;
5055 static const struct wined3d_parent_ops ddraw_frontbuffer_parent_ops =
5057 ddraw_frontbuffer_destroyed,
5060 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
5061 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
5062 struct wined3d_texture **texture)
5064 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
5065 const struct wined3d_parent_ops *parent_ops;
5066 HRESULT hr;
5068 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
5069 device_parent, container_parent, desc, texture_flags, texture);
5071 if (!ddraw->wined3d_frontbuffer)
5072 parent_ops = &ddraw_frontbuffer_parent_ops;
5073 else
5074 parent_ops = &ddraw_null_wined3d_parent_ops;
5076 if (FAILED(hr = wined3d_texture_create(ddraw->wined3d_device, desc, 1, 1,
5077 texture_flags, NULL, ddraw, parent_ops, texture)))
5079 WARN("Failed to create texture, hr %#x.\n", hr);
5080 return hr;
5083 if (!ddraw->wined3d_frontbuffer)
5084 ddraw->wined3d_frontbuffer = *texture;
5086 return hr;
5089 static const struct wined3d_device_parent_ops ddraw_wined3d_device_parent_ops =
5091 device_parent_wined3d_device_created,
5092 device_parent_mode_changed,
5093 device_parent_activate,
5094 device_parent_texture_sub_resource_created,
5095 device_parent_create_swapchain_texture,
5098 HRESULT ddraw_init(struct ddraw *ddraw, DWORD flags, enum wined3d_device_type device_type)
5100 struct wined3d_caps caps;
5101 HRESULT hr;
5103 static const enum wined3d_feature_level feature_levels[] =
5105 WINED3D_FEATURE_LEVEL_7,
5106 WINED3D_FEATURE_LEVEL_6,
5107 WINED3D_FEATURE_LEVEL_5,
5110 ddraw->IDirectDraw7_iface.lpVtbl = &ddraw7_vtbl;
5111 ddraw->IDirectDraw_iface.lpVtbl = &ddraw1_vtbl;
5112 ddraw->IDirectDraw2_iface.lpVtbl = &ddraw2_vtbl;
5113 ddraw->IDirectDraw4_iface.lpVtbl = &ddraw4_vtbl;
5114 ddraw->IDirect3D_iface.lpVtbl = &d3d1_vtbl;
5115 ddraw->IDirect3D2_iface.lpVtbl = &d3d2_vtbl;
5116 ddraw->IDirect3D3_iface.lpVtbl = &d3d3_vtbl;
5117 ddraw->IDirect3D7_iface.lpVtbl = &d3d7_vtbl;
5118 ddraw->device_parent.ops = &ddraw_wined3d_device_parent_ops;
5119 ddraw->state_parent.ops = &ddraw_swapchain_state_parent_ops;
5120 ddraw->numIfaces = 1;
5121 ddraw->ref7 = 1;
5123 flags |= DDRAW_WINED3D_FLAGS;
5124 if (!(ddraw->wined3d = wined3d_create(flags)))
5126 flags |= WINED3D_NO3D;
5127 if (!(ddraw->wined3d = wined3d_create(flags)))
5129 WARN("Failed to create a wined3d object.\n");
5130 return E_FAIL;
5134 if (!(ddraw->wined3d_adapter = wined3d_get_adapter(ddraw->wined3d, WINED3DADAPTER_DEFAULT)))
5136 WARN("Failed to get the default wined3d adapter.\n");
5137 wined3d_decref(ddraw->wined3d);
5138 return E_FAIL;
5141 if (!(ddraw->wined3d_output = wined3d_adapter_get_output(ddraw->wined3d_adapter, 0)))
5143 WARN("Failed to get the default wined3d output.\n");
5144 wined3d_decref(ddraw->wined3d);
5145 return E_FAIL;
5148 if (FAILED(hr = wined3d_get_device_caps(ddraw->wined3d_adapter, device_type, &caps)))
5150 ERR("Failed to get device caps, hr %#x.\n", hr);
5151 wined3d_decref(ddraw->wined3d);
5152 return E_FAIL;
5155 if (!(caps.ddraw_caps.caps & WINEDDCAPS_3D))
5157 WARN("Created a wined3d object without 3D support.\n");
5158 ddraw->flags |= DDRAW_NO3D;
5161 if (FAILED(hr = wined3d_device_create(ddraw->wined3d, ddraw->wined3d_adapter, device_type,
5162 NULL, 0, DDRAW_STRIDE_ALIGNMENT, feature_levels, ARRAY_SIZE(feature_levels),
5163 &ddraw->device_parent, &ddraw->wined3d_device)))
5165 WARN("Failed to create a wined3d device, hr %#x.\n", hr);
5166 wined3d_decref(ddraw->wined3d);
5167 return hr;
5169 ddraw->immediate_context = wined3d_device_get_immediate_context(ddraw->wined3d_device);
5171 list_init(&ddraw->surface_list);
5173 if (FAILED(hr = wined3d_stateblock_create(ddraw->wined3d_device, NULL, WINED3D_SBT_PRIMARY, &ddraw->state)))
5175 ERR("Failed to create the primary stateblock, hr %#x.\n", hr);
5176 wined3d_device_decref(ddraw->wined3d_device);
5177 wined3d_decref(ddraw->wined3d);
5178 return hr;
5180 ddraw->stateblock_state = wined3d_stateblock_get_state(ddraw->state);
5182 return DD_OK;