ddraw: Prevent mode changes when a different ddraw object is in exclusive mode.
[wine.git] / dlls / ddraw / ddraw.c
blob8a554f3ea975fe82f6db17a07bf0bbe797dec192
1 /*
2 * Copyright 1997-2000 Marcus Meissner
3 * Copyright 1998-2000 Lionel Ulmer
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2006 Stefan Dösinger
6 * Copyright 2008 Denver Gingerich
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include "ddraw_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
30 static const struct ddraw *exclusive_ddraw;
32 /* Device identifier. Don't relay it to WineD3D */
33 static const DDDEVICEIDENTIFIER2 deviceidentifier =
35 "display",
36 "DirectDraw HAL",
37 { { 0x00010001, 0x00010001 } },
38 0, 0, 0, 0,
39 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
40 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
44 static struct enum_device_entry
46 char interface_name[100];
47 char device_name[100];
48 const GUID *device_guid;
49 } device_list7[] =
51 /* T&L HAL device */
53 "WINE Direct3D7 Hardware Transform and Lighting acceleration using WineD3D",
54 "Wine D3D7 T&L HAL",
55 &IID_IDirect3DTnLHalDevice,
58 /* HAL device */
60 "WINE Direct3D7 Hardware acceleration using WineD3D",
61 "Direct3D HAL",
62 &IID_IDirect3DHALDevice,
65 /* RGB device */
67 "WINE Direct3D7 RGB Software Emulation using WineD3D",
68 "Wine D3D7 RGB",
69 &IID_IDirect3DRGBDevice,
73 static void STDMETHODCALLTYPE ddraw_null_wined3d_object_destroyed(void *parent) {}
75 const struct wined3d_parent_ops ddraw_null_wined3d_parent_ops =
77 ddraw_null_wined3d_object_destroyed,
80 static inline struct ddraw *impl_from_IDirectDraw(IDirectDraw *iface)
82 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw_iface);
85 static inline struct ddraw *impl_from_IDirectDraw2(IDirectDraw2 *iface)
87 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw2_iface);
90 static inline struct ddraw *impl_from_IDirectDraw4(IDirectDraw4 *iface)
92 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw4_iface);
95 static inline struct ddraw *impl_from_IDirectDraw7(IDirectDraw7 *iface)
97 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw7_iface);
100 static inline struct ddraw *impl_from_IDirect3D(IDirect3D *iface)
102 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D_iface);
105 static inline struct ddraw *impl_from_IDirect3D2(IDirect3D2 *iface)
107 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D2_iface);
110 static inline struct ddraw *impl_from_IDirect3D3(IDirect3D3 *iface)
112 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D3_iface);
115 static inline struct ddraw *impl_from_IDirect3D7(IDirect3D7 *iface)
117 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D7_iface);
120 /*****************************************************************************
121 * IUnknown Methods
122 *****************************************************************************/
124 /*****************************************************************************
125 * IDirectDraw7::QueryInterface
127 * Queries different interfaces of the DirectDraw object. It can return
128 * IDirectDraw interfaces in version 1, 2, 4 and 7, and IDirect3D interfaces
129 * in version 1, 2, 3 and 7. An IDirect3DDevice can be created with this
130 * method.
131 * The returned interface is AddRef()-ed before it's returned
133 * Used for version 1, 2, 4 and 7
135 * Params:
136 * refiid: Interface ID asked for
137 * obj: Used to return the interface pointer
139 * Returns:
140 * S_OK if an interface was found
141 * E_NOINTERFACE if the requested interface wasn't found
143 *****************************************************************************/
144 static HRESULT WINAPI ddraw7_QueryInterface(IDirectDraw7 *iface, REFIID refiid, void **obj)
146 struct ddraw *This = impl_from_IDirectDraw7(iface);
148 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(refiid), obj);
150 /* Can change surface impl type */
151 wined3d_mutex_lock();
153 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
154 *obj = NULL;
156 if(!refiid)
158 wined3d_mutex_unlock();
159 return DDERR_INVALIDPARAMS;
162 /* Check DirectDraw Interfaces */
163 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
164 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
166 *obj = &This->IDirectDraw7_iface;
167 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
169 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
171 *obj = &This->IDirectDraw4_iface;
172 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
174 else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
176 /* This Interface exists in ddrawex.dll, it is implemented in a wrapper */
177 WARN("IDirectDraw3 is not valid in ddraw.dll\n");
178 *obj = NULL;
179 wined3d_mutex_unlock();
180 return E_NOINTERFACE;
182 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
184 *obj = &This->IDirectDraw2_iface;
185 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
187 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
189 *obj = &This->IDirectDraw_iface;
190 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
193 /* Direct3D
194 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
195 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
196 * who had this idea and why. The older interfaces can query and IDirect3D version
197 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
198 * and messy to implement with the common creation function, so it has been left out here.
200 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
201 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
202 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
203 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
205 /* Check the surface implementation */
206 if (DefaultSurfaceType != WINED3D_SURFACE_TYPE_OPENGL)
208 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
209 /* Do not abort here, only reject 3D Device creation */
212 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
214 This->d3dversion = 1;
215 *obj = &This->IDirect3D_iface;
216 TRACE(" returning Direct3D interface at %p.\n", *obj);
218 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
220 This->d3dversion = 2;
221 *obj = &This->IDirect3D2_iface;
222 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
224 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
226 This->d3dversion = 3;
227 *obj = &This->IDirect3D3_iface;
228 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
230 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
232 This->d3dversion = 7;
233 *obj = &This->IDirect3D7_iface;
234 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
237 /* Unknown interface */
238 else
240 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
241 wined3d_mutex_unlock();
242 return E_NOINTERFACE;
245 IUnknown_AddRef( (IUnknown *) *obj );
246 wined3d_mutex_unlock();
248 return S_OK;
251 static HRESULT WINAPI ddraw4_QueryInterface(IDirectDraw4 *iface, REFIID riid, void **object)
253 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
255 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
257 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
260 static HRESULT WINAPI ddraw2_QueryInterface(IDirectDraw2 *iface, REFIID riid, void **object)
262 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
264 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
266 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
269 static HRESULT WINAPI ddraw1_QueryInterface(IDirectDraw *iface, REFIID riid, void **object)
271 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
273 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
275 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
278 static HRESULT WINAPI d3d7_QueryInterface(IDirect3D7 *iface, REFIID riid, void **object)
280 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
282 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
284 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
287 static HRESULT WINAPI d3d3_QueryInterface(IDirect3D3 *iface, REFIID riid, void **object)
289 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
291 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
293 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
296 static HRESULT WINAPI d3d2_QueryInterface(IDirect3D2 *iface, REFIID riid, void **object)
298 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
300 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
302 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
305 static HRESULT WINAPI d3d1_QueryInterface(IDirect3D *iface, REFIID riid, void **object)
307 struct ddraw *ddraw = impl_from_IDirect3D(iface);
309 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
311 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
314 /*****************************************************************************
315 * IDirectDraw7::AddRef
317 * Increases the interfaces refcount, basically
319 * DDraw refcounting is a bit tricky. The different DirectDraw interface
320 * versions have individual refcounts, but the IDirect3D interfaces do not.
321 * All interfaces are from one object, that means calling QueryInterface on an
322 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
323 * ddraw object.
325 * That means all AddRef and Release implementations of IDirectDrawX work
326 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
327 * except of IDirect3D7 which thunks to IDirectDraw7
329 * Returns: The new refcount
331 *****************************************************************************/
332 static ULONG WINAPI ddraw7_AddRef(IDirectDraw7 *iface)
334 struct ddraw *This = impl_from_IDirectDraw7(iface);
335 ULONG ref = InterlockedIncrement(&This->ref7);
337 TRACE("%p increasing refcount to %u.\n", This, ref);
339 if(ref == 1) InterlockedIncrement(&This->numIfaces);
341 return ref;
344 static ULONG WINAPI ddraw4_AddRef(IDirectDraw4 *iface)
346 struct ddraw *This = impl_from_IDirectDraw4(iface);
347 ULONG ref = InterlockedIncrement(&This->ref4);
349 TRACE("%p increasing refcount to %u.\n", This, ref);
351 if (ref == 1) InterlockedIncrement(&This->numIfaces);
353 return ref;
356 static ULONG WINAPI ddraw2_AddRef(IDirectDraw2 *iface)
358 struct ddraw *This = impl_from_IDirectDraw2(iface);
359 ULONG ref = InterlockedIncrement(&This->ref2);
361 TRACE("%p increasing refcount to %u.\n", This, ref);
363 if (ref == 1) InterlockedIncrement(&This->numIfaces);
365 return ref;
368 static ULONG WINAPI ddraw1_AddRef(IDirectDraw *iface)
370 struct ddraw *This = impl_from_IDirectDraw(iface);
371 ULONG ref = InterlockedIncrement(&This->ref1);
373 TRACE("%p increasing refcount to %u.\n", This, ref);
375 if (ref == 1) InterlockedIncrement(&This->numIfaces);
377 return ref;
380 static ULONG WINAPI d3d7_AddRef(IDirect3D7 *iface)
382 struct ddraw *This = impl_from_IDirect3D7(iface);
384 TRACE("iface %p.\n", iface);
386 return ddraw7_AddRef(&This->IDirectDraw7_iface);
389 static ULONG WINAPI d3d3_AddRef(IDirect3D3 *iface)
391 struct ddraw *This = impl_from_IDirect3D3(iface);
393 TRACE("iface %p.\n", iface);
395 return ddraw1_AddRef(&This->IDirectDraw_iface);
398 static ULONG WINAPI d3d2_AddRef(IDirect3D2 *iface)
400 struct ddraw *This = impl_from_IDirect3D2(iface);
402 TRACE("iface %p.\n", iface);
404 return ddraw1_AddRef(&This->IDirectDraw_iface);
407 static ULONG WINAPI d3d1_AddRef(IDirect3D *iface)
409 struct ddraw *This = impl_from_IDirect3D(iface);
411 TRACE("iface %p.\n", iface);
413 return ddraw1_AddRef(&This->IDirectDraw_iface);
416 void ddraw_destroy_swapchain(struct ddraw *ddraw)
418 TRACE("Destroying the swapchain.\n");
420 wined3d_swapchain_decref(ddraw->wined3d_swapchain);
421 ddraw->wined3d_swapchain = NULL;
423 if (DefaultSurfaceType == WINED3D_SURFACE_TYPE_OPENGL)
425 UINT i;
427 for (i = 0; i < ddraw->numConvertedDecls; ++i)
429 wined3d_vertex_declaration_decref(ddraw->decls[i].decl);
431 HeapFree(GetProcessHeap(), 0, ddraw->decls);
432 ddraw->numConvertedDecls = 0;
434 if (FAILED(wined3d_device_uninit_3d(ddraw->wined3d_device)))
436 ERR("Failed to uninit 3D.\n");
438 else
440 /* Free the d3d window if one was created. */
441 if (ddraw->d3d_window && ddraw->d3d_window != ddraw->dest_window)
443 TRACE("Destroying the hidden render window %p.\n", ddraw->d3d_window);
444 DestroyWindow(ddraw->d3d_window);
445 ddraw->d3d_window = 0;
449 ddraw->d3d_initialized = FALSE;
451 else
453 wined3d_device_uninit_gdi(ddraw->wined3d_device);
456 ddraw_set_swapchain_window(ddraw, NULL);
458 TRACE("Swapchain destroyed.\n");
461 /*****************************************************************************
462 * ddraw_destroy
464 * Destroys a ddraw object if all refcounts are 0. This is to share code
465 * between the IDirectDrawX::Release functions
467 * Params:
468 * This: DirectDraw object to destroy
470 *****************************************************************************/
471 static void ddraw_destroy(struct ddraw *This)
473 IDirectDraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, NULL, DDSCL_NORMAL);
474 IDirectDraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
476 /* Destroy the device window if we created one */
477 if(This->devicewindow != 0)
479 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
480 DestroyWindow(This->devicewindow);
481 This->devicewindow = 0;
484 wined3d_mutex_lock();
485 list_remove(&This->ddraw_list_entry);
486 wined3d_mutex_unlock();
488 if (This->wined3d_swapchain)
489 ddraw_destroy_swapchain(This);
490 wined3d_device_decref(This->wined3d_device);
491 wined3d_decref(This->wined3d);
493 /* Now free the object */
494 HeapFree(GetProcessHeap(), 0, This);
497 /*****************************************************************************
498 * IDirectDraw7::Release
500 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
502 * Returns: The new refcount
503 *****************************************************************************/
504 static ULONG WINAPI ddraw7_Release(IDirectDraw7 *iface)
506 struct ddraw *This = impl_from_IDirectDraw7(iface);
507 ULONG ref = InterlockedDecrement(&This->ref7);
509 TRACE("%p decreasing refcount to %u.\n", This, ref);
511 if (!ref && !InterlockedDecrement(&This->numIfaces))
512 ddraw_destroy(This);
514 return ref;
517 static ULONG WINAPI ddraw4_Release(IDirectDraw4 *iface)
519 struct ddraw *This = impl_from_IDirectDraw4(iface);
520 ULONG ref = InterlockedDecrement(&This->ref4);
522 TRACE("%p decreasing refcount to %u.\n", This, ref);
524 if (!ref && !InterlockedDecrement(&This->numIfaces))
525 ddraw_destroy(This);
527 return ref;
530 static ULONG WINAPI ddraw2_Release(IDirectDraw2 *iface)
532 struct ddraw *This = impl_from_IDirectDraw2(iface);
533 ULONG ref = InterlockedDecrement(&This->ref2);
535 TRACE("%p decreasing refcount to %u.\n", This, ref);
537 if (!ref && !InterlockedDecrement(&This->numIfaces))
538 ddraw_destroy(This);
540 return ref;
543 static ULONG WINAPI ddraw1_Release(IDirectDraw *iface)
545 struct ddraw *This = impl_from_IDirectDraw(iface);
546 ULONG ref = InterlockedDecrement(&This->ref1);
548 TRACE("%p decreasing refcount to %u.\n", This, ref);
550 if (!ref && !InterlockedDecrement(&This->numIfaces))
551 ddraw_destroy(This);
553 return ref;
556 static ULONG WINAPI d3d7_Release(IDirect3D7 *iface)
558 struct ddraw *This = impl_from_IDirect3D7(iface);
560 TRACE("iface %p.\n", iface);
562 return ddraw7_Release(&This->IDirectDraw7_iface);
565 static ULONG WINAPI d3d3_Release(IDirect3D3 *iface)
567 struct ddraw *This = impl_from_IDirect3D3(iface);
569 TRACE("iface %p.\n", iface);
571 return ddraw1_Release(&This->IDirectDraw_iface);
574 static ULONG WINAPI d3d2_Release(IDirect3D2 *iface)
576 struct ddraw *This = impl_from_IDirect3D2(iface);
578 TRACE("iface %p.\n", iface);
580 return ddraw1_Release(&This->IDirectDraw_iface);
583 static ULONG WINAPI d3d1_Release(IDirect3D *iface)
585 struct ddraw *This = impl_from_IDirect3D(iface);
587 TRACE("iface %p.\n", iface);
589 return ddraw1_Release(&This->IDirectDraw_iface);
592 /*****************************************************************************
593 * IDirectDraw methods
594 *****************************************************************************/
596 static HRESULT ddraw_set_focus_window(struct ddraw *ddraw, HWND window)
598 /* FIXME: This looks wrong, exclusive mode should imply a destination
599 * window. */
600 if ((ddraw->cooperative_level & DDSCL_EXCLUSIVE) && ddraw->dest_window)
602 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET.\n");
603 return DDERR_HWNDALREADYSET;
606 ddraw->focuswindow = window;
608 /* Use the focus window for drawing too. */
609 ddraw->dest_window = ddraw->focuswindow;
611 return DD_OK;
614 static HRESULT ddraw_attach_d3d_device(struct ddraw *ddraw,
615 struct wined3d_swapchain_desc *swapchain_desc)
617 HWND window = swapchain_desc->device_window;
618 HRESULT hr;
620 TRACE("ddraw %p.\n", ddraw);
622 if (!window || window == GetDesktopWindow())
624 window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "Hidden D3D Window",
625 WS_DISABLED, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
626 NULL, NULL, NULL, NULL);
627 if (!window)
629 ERR("Failed to create window, last error %#x.\n", GetLastError());
630 return E_FAIL;
633 ShowWindow(window, SW_HIDE); /* Just to be sure */
634 WARN("No window for the Direct3DDevice, created hidden window %p.\n", window);
636 swapchain_desc->device_window = window;
638 else
640 TRACE("Using existing window %p for Direct3D rendering.\n", window);
642 ddraw->d3d_window = window;
644 /* Set this NOW, otherwise creating the depth stencil surface will cause a
645 * recursive loop until ram or emulated video memory is full. */
646 ddraw->d3d_initialized = TRUE;
647 hr = wined3d_device_init_3d(ddraw->wined3d_device, swapchain_desc);
648 if (FAILED(hr))
650 ddraw->d3d_initialized = FALSE;
651 return hr;
654 ddraw->declArraySize = 2;
655 ddraw->decls = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw->decls) * ddraw->declArraySize);
656 if (!ddraw->decls)
658 ERR("Error allocating an array for the converted vertex decls.\n");
659 ddraw->declArraySize = 0;
660 hr = wined3d_device_uninit_3d(ddraw->wined3d_device);
661 return E_OUTOFMEMORY;
664 TRACE("Successfully initialized 3D.\n");
666 return DD_OK;
669 static HRESULT ddraw_create_swapchain(struct ddraw *ddraw, HWND window, BOOL windowed)
671 struct wined3d_swapchain_desc swapchain_desc;
672 struct wined3d_display_mode mode;
673 HRESULT hr = WINED3D_OK;
675 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
677 ERR("Failed to get display mode.\n");
678 return hr;
681 memset(&swapchain_desc, 0, sizeof(swapchain_desc));
682 swapchain_desc.backbuffer_width = mode.width;
683 swapchain_desc.backbuffer_height = mode.height;
684 swapchain_desc.backbuffer_format = mode.format_id;
685 swapchain_desc.swap_effect = WINED3D_SWAP_EFFECT_COPY;
686 swapchain_desc.device_window = window;
687 swapchain_desc.windowed = windowed;
689 if (DefaultSurfaceType == WINED3D_SURFACE_TYPE_OPENGL)
690 hr = ddraw_attach_d3d_device(ddraw, &swapchain_desc);
691 else
692 hr = wined3d_device_init_gdi(ddraw->wined3d_device, &swapchain_desc);
694 if (FAILED(hr))
696 ERR("Failed to create swapchain, hr %#x.\n", hr);
697 return hr;
700 if (!(ddraw->wined3d_swapchain = wined3d_device_get_swapchain(ddraw->wined3d_device, 0)))
702 ERR("Failed to get swapchain.\n");
703 return DDERR_INVALIDPARAMS;
706 wined3d_swapchain_incref(ddraw->wined3d_swapchain);
707 ddraw_set_swapchain_window(ddraw, window);
709 return DD_OK;
712 /*****************************************************************************
713 * IDirectDraw7::SetCooperativeLevel
715 * Sets the cooperative level for the DirectDraw object, and the window
716 * assigned to it. The cooperative level determines the general behavior
717 * of the DirectDraw application
719 * Warning: This is quite tricky, as it's not really documented which
720 * cooperative levels can be combined with each other. If a game fails
721 * after this function, try to check the cooperative levels passed on
722 * Windows, and if it returns something different.
724 * If you think that this function caused the failure because it writes a
725 * fixme, be sure to run again with a +ddraw trace.
727 * What is known about cooperative levels (See the ddraw modes test):
728 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
729 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
730 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
731 * DDSCL_FULLSCREEN can be activated
732 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
734 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
735 * DDSCL_SETFOCUSWINDOW (partially),
736 * DDSCL_MULTITHREADED (work in progress)
738 * Unhandled flags, which should be implemented
739 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
740 * expect any difference to a normal window for wine)
741 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
742 * rendering (Possible test case: Half-Life)
744 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
746 * These don't seem very important for wine:
747 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
749 * Returns:
750 * DD_OK if the cooperative level was set successfully
751 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
752 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
753 * (Probably others too, have to investigate)
755 *****************************************************************************/
756 static HRESULT WINAPI ddraw7_SetCooperativeLevel(IDirectDraw7 *iface, HWND hwnd, DWORD cooplevel)
758 struct ddraw *This = impl_from_IDirectDraw7(iface);
759 struct wined3d_surface *rt = NULL, *ds = NULL;
760 struct wined3d_stateblock *stateblock;
761 BOOL restore_state = FALSE;
762 HWND window;
763 HRESULT hr;
765 TRACE("iface %p, window %p, flags %#x.\n", iface, hwnd, cooplevel);
766 DDRAW_dump_cooperativelevel(cooplevel);
768 wined3d_mutex_lock();
770 /* Get the old window */
771 window = This->dest_window;
773 /* Tests suggest that we need one of them: */
774 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
775 DDSCL_NORMAL |
776 DDSCL_EXCLUSIVE )))
778 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
779 wined3d_mutex_unlock();
780 return DDERR_INVALIDPARAMS;
783 if ((cooplevel & DDSCL_CREATEDEVICEWINDOW) && !(cooplevel & DDSCL_EXCLUSIVE))
785 WARN("DDSCL_CREATEDEVICEWINDOW requires DDSCL_EXCLUSIVE.\n");
786 wined3d_mutex_unlock();
787 return DDERR_INVALIDPARAMS;
790 /* Handle those levels first which set various hwnds */
791 if ((cooplevel & DDSCL_SETFOCUSWINDOW) && !(cooplevel & DDSCL_CREATEDEVICEWINDOW))
793 /* This isn't compatible with a lot of flags */
794 if (cooplevel & (DDSCL_MULTITHREADED
795 | DDSCL_FPUSETUP
796 | DDSCL_FPUPRESERVE
797 | DDSCL_ALLOWREBOOT
798 | DDSCL_ALLOWMODEX
799 | DDSCL_SETDEVICEWINDOW
800 | DDSCL_NORMAL
801 | DDSCL_EXCLUSIVE
802 | DDSCL_FULLSCREEN))
804 WARN("Called with incompatible flags, returning DDERR_INVALIDPARAMS.\n");
805 wined3d_mutex_unlock();
806 return DDERR_INVALIDPARAMS;
809 hr = ddraw_set_focus_window(This, hwnd);
810 wined3d_mutex_unlock();
811 return hr;
814 if (cooplevel & DDSCL_EXCLUSIVE)
816 if (!(cooplevel & DDSCL_FULLSCREEN) || !(hwnd || (cooplevel & DDSCL_CREATEDEVICEWINDOW)))
818 WARN("DDSCL_EXCLUSIVE requires DDSCL_FULLSCREEN and a window.\n");
819 wined3d_mutex_unlock();
820 return DDERR_INVALIDPARAMS;
823 if (cooplevel & DDSCL_CREATEDEVICEWINDOW)
825 HWND device_window;
827 if (!This->focuswindow && !(cooplevel & DDSCL_SETFOCUSWINDOW))
829 WARN("No focus window set.\n");
830 wined3d_mutex_unlock();
831 return DDERR_NOFOCUSWINDOW;
834 device_window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "DirectDrawDeviceWnd",
835 WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
836 NULL, NULL, NULL, NULL);
837 if (!device_window)
839 ERR("Failed to create window, last error %#x.\n", GetLastError());
840 wined3d_mutex_unlock();
841 return E_FAIL;
844 ShowWindow(device_window, SW_SHOW);
845 TRACE("Created a device window %p.\n", device_window);
847 /* Native apparently leaks the created device window if setting the
848 * focus window below fails. */
849 This->cooperative_level |= DDSCL_CREATEDEVICEWINDOW;
850 This->devicewindow = device_window;
852 if (cooplevel & DDSCL_SETFOCUSWINDOW)
854 if (!hwnd)
856 wined3d_mutex_unlock();
857 return DDERR_NOHWND;
860 if (FAILED(hr = ddraw_set_focus_window(This, hwnd)))
862 wined3d_mutex_unlock();
863 return hr;
867 hwnd = device_window;
870 else
872 if (This->cooperative_level & DDSCL_CREATEDEVICEWINDOW)
873 DestroyWindow(This->devicewindow);
874 This->devicewindow = NULL;
875 This->focuswindow = NULL;
878 if ((This->cooperative_level & DDSCL_EXCLUSIVE)
879 && (hwnd != window || !(cooplevel & DDSCL_EXCLUSIVE)))
880 wined3d_device_release_focus_window(This->wined3d_device);
882 if ((cooplevel & DDSCL_FULLSCREEN) != (This->cooperative_level & DDSCL_FULLSCREEN) || hwnd != window)
884 if (This->cooperative_level & DDSCL_FULLSCREEN)
885 wined3d_device_restore_fullscreen_window(This->wined3d_device, window);
887 if (cooplevel & DDSCL_FULLSCREEN)
889 struct wined3d_display_mode display_mode;
891 wined3d_get_adapter_display_mode(This->wined3d, WINED3DADAPTER_DEFAULT, &display_mode, NULL);
892 wined3d_device_setup_fullscreen_window(This->wined3d_device, hwnd,
893 display_mode.width, display_mode.height);
897 if ((cooplevel & DDSCL_EXCLUSIVE)
898 && (hwnd != window || !(This->cooperative_level & DDSCL_EXCLUSIVE)))
900 hr = wined3d_device_acquire_focus_window(This->wined3d_device, hwnd);
901 if (FAILED(hr))
903 ERR("Failed to acquire focus window, hr %#x.\n", hr);
904 wined3d_mutex_unlock();
905 return hr;
909 /* Don't override focus windows or private device windows */
910 if (hwnd && !This->focuswindow && !This->devicewindow && (hwnd != window))
911 This->dest_window = hwnd;
913 if (cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
914 wined3d_device_set_multithreaded(This->wined3d_device);
916 if (This->wined3d_swapchain)
918 if (DefaultSurfaceType != WINED3D_SURFACE_TYPE_GDI)
920 restore_state = TRUE;
922 if (FAILED(hr = wined3d_stateblock_create(This->wined3d_device, WINED3D_SBT_ALL, &stateblock)))
924 ERR("Failed to create stateblock, hr %#x.\n", hr);
925 wined3d_mutex_unlock();
926 return hr;
929 wined3d_stateblock_capture(stateblock);
930 rt = wined3d_device_get_render_target(This->wined3d_device, 0);
931 if (rt == This->wined3d_frontbuffer)
932 rt = NULL;
933 else if (rt)
934 wined3d_surface_incref(rt);
936 if ((ds = wined3d_device_get_depth_stencil(This->wined3d_device)))
937 wined3d_surface_incref(ds);
940 ddraw_destroy_swapchain(This);
943 if (FAILED(hr = ddraw_create_swapchain(This, This->dest_window, !(cooplevel & DDSCL_FULLSCREEN))))
944 ERR("Failed to create swapchain, hr %#x.\n", hr);
946 if (restore_state)
948 if (ds)
950 wined3d_device_set_depth_stencil(This->wined3d_device, ds);
951 wined3d_surface_decref(ds);
954 if (rt)
956 wined3d_device_set_render_target(This->wined3d_device, 0, rt, FALSE);
957 wined3d_surface_decref(rt);
960 wined3d_stateblock_apply(stateblock);
961 wined3d_stateblock_decref(stateblock);
964 /* Unhandled flags */
965 if(cooplevel & DDSCL_ALLOWREBOOT)
966 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
967 if(cooplevel & DDSCL_ALLOWMODEX)
968 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
969 if(cooplevel & DDSCL_FPUSETUP)
970 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
972 if (cooplevel & DDSCL_EXCLUSIVE)
973 exclusive_ddraw = This;
974 else if (exclusive_ddraw == This)
975 exclusive_ddraw = NULL;
977 /* Store the cooperative_level */
978 This->cooperative_level = cooplevel;
979 TRACE("SetCooperativeLevel retuning DD_OK\n");
980 wined3d_mutex_unlock();
982 return DD_OK;
985 static HRESULT WINAPI ddraw4_SetCooperativeLevel(IDirectDraw4 *iface, HWND window, DWORD flags)
987 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
989 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
991 return ddraw7_SetCooperativeLevel(&ddraw->IDirectDraw7_iface, window, flags);
994 static HRESULT WINAPI ddraw2_SetCooperativeLevel(IDirectDraw2 *iface, HWND window, DWORD flags)
996 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
998 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1000 return ddraw7_SetCooperativeLevel(&ddraw->IDirectDraw7_iface, window, flags);
1003 static HRESULT WINAPI ddraw1_SetCooperativeLevel(IDirectDraw *iface, HWND window, DWORD flags)
1005 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1007 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1009 return ddraw7_SetCooperativeLevel(&ddraw->IDirectDraw7_iface, window, flags);
1012 /*****************************************************************************
1013 * IDirectDraw7::SetDisplayMode
1015 * Sets the display screen resolution, color depth and refresh frequency
1016 * when in fullscreen mode (in theory).
1017 * Possible return values listed in the SDK suggest that this method fails
1018 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
1019 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
1020 * It seems to be valid to pass 0 for With and Height, this has to be tested
1021 * It could mean that the current video mode should be left as-is. (But why
1022 * call it then?)
1024 * Params:
1025 * Height, Width: Screen dimension
1026 * BPP: Color depth in Bits per pixel
1027 * Refreshrate: Screen refresh rate
1028 * Flags: Other stuff
1030 * Returns
1031 * DD_OK on success
1033 *****************************************************************************/
1034 static HRESULT WINAPI ddraw7_SetDisplayMode(IDirectDraw7 *iface, DWORD width, DWORD height,
1035 DWORD bpp, DWORD refresh_rate, DWORD flags)
1037 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1038 struct wined3d_display_mode mode;
1039 enum wined3d_format_id format;
1040 HRESULT hr;
1042 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1043 iface, width, height, bpp, refresh_rate, flags);
1045 if (force_refresh_rate != 0)
1047 TRACE("ForceRefreshRate overriding passed-in refresh rate (%u Hz) to %u Hz\n",
1048 refresh_rate, force_refresh_rate);
1049 refresh_rate = force_refresh_rate;
1052 wined3d_mutex_lock();
1054 if (exclusive_ddraw && exclusive_ddraw != ddraw)
1056 wined3d_mutex_unlock();
1057 return DDERR_NOEXCLUSIVEMODE;
1060 if (!width || !height)
1062 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here. */
1063 wined3d_mutex_unlock();
1064 return DD_OK;
1067 switch (bpp)
1069 case 8: format = WINED3DFMT_P8_UINT; break;
1070 case 15: format = WINED3DFMT_B5G5R5X1_UNORM; break;
1071 case 16: format = WINED3DFMT_B5G6R5_UNORM; break;
1072 case 24: format = WINED3DFMT_B8G8R8_UNORM; break;
1073 case 32: format = WINED3DFMT_B8G8R8X8_UNORM; break;
1074 default: format = WINED3DFMT_UNKNOWN; break;
1077 mode.width = width;
1078 mode.height = height;
1079 mode.refresh_rate = refresh_rate;
1080 mode.format_id = format;
1081 mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
1083 /* TODO: The possible return values from msdn suggest that the screen mode
1084 * can't be changed if a surface is locked or some drawing is in progress. */
1085 /* TODO: Lose the primary surface. */
1086 hr = wined3d_set_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode);
1088 wined3d_mutex_unlock();
1090 switch (hr)
1092 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1093 default: return hr;
1097 static HRESULT WINAPI ddraw4_SetDisplayMode(IDirectDraw4 *iface, DWORD width, DWORD height,
1098 DWORD bpp, DWORD refresh_rate, DWORD flags)
1100 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1102 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1103 iface, width, height, bpp, refresh_rate, flags);
1105 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1108 static HRESULT WINAPI ddraw2_SetDisplayMode(IDirectDraw2 *iface,
1109 DWORD width, DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
1111 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1113 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1114 iface, width, height, bpp, refresh_rate, flags);
1116 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1119 static HRESULT WINAPI ddraw1_SetDisplayMode(IDirectDraw *iface, DWORD width, DWORD height, DWORD bpp)
1121 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1123 TRACE("iface %p, width %u, height %u, bpp %u.\n", iface, width, height, bpp);
1125 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, 0, 0);
1128 /*****************************************************************************
1129 * IDirectDraw7::RestoreDisplayMode
1131 * Restores the display mode to what it was at creation time. Basically.
1133 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
1134 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
1135 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
1136 * -> DD_1 is released. The screen should be left at 1024x768x32.
1137 * -> DD_2 is released. The screen should be set to 1400x1050x32
1138 * This case is unhandled right now, but Empire Earth does it this way.
1139 * (But perhaps there is something in SetCooperativeLevel to prevent this)
1141 * The msdn says that this method resets the display mode to what it was before
1142 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
1144 * Returns
1145 * DD_OK on success
1146 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
1148 *****************************************************************************/
1149 static HRESULT WINAPI ddraw7_RestoreDisplayMode(IDirectDraw7 *iface)
1151 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1152 HRESULT hr;
1154 TRACE("iface %p.\n", iface);
1156 wined3d_mutex_lock();
1158 if (exclusive_ddraw && exclusive_ddraw != ddraw)
1160 wined3d_mutex_unlock();
1161 return DDERR_NOEXCLUSIVEMODE;
1164 hr = wined3d_set_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &ddraw->original_mode);
1166 wined3d_mutex_unlock();
1168 return hr;
1171 static HRESULT WINAPI ddraw4_RestoreDisplayMode(IDirectDraw4 *iface)
1173 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1175 TRACE("iface %p.\n", iface);
1177 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
1180 static HRESULT WINAPI ddraw2_RestoreDisplayMode(IDirectDraw2 *iface)
1182 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1184 TRACE("iface %p.\n", iface);
1186 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
1189 static HRESULT WINAPI ddraw1_RestoreDisplayMode(IDirectDraw *iface)
1191 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1193 TRACE("iface %p.\n", iface);
1195 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
1198 /*****************************************************************************
1199 * IDirectDraw7::GetCaps
1201 * Returns the drives capabilities
1203 * Used for version 1, 2, 4 and 7
1205 * Params:
1206 * DriverCaps: Structure to write the Hardware accelerated caps to
1207 * HelCaps: Structure to write the emulation caps to
1209 * Returns
1210 * This implementation returns DD_OK only
1212 *****************************************************************************/
1213 static HRESULT WINAPI ddraw7_GetCaps(IDirectDraw7 *iface, DDCAPS *DriverCaps, DDCAPS *HELCaps)
1215 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1216 DDCAPS caps;
1217 WINED3DCAPS winecaps;
1218 HRESULT hr;
1219 DDSCAPS2 ddscaps = {0, 0, 0, 0};
1221 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, DriverCaps, HELCaps);
1223 /* One structure must be != NULL */
1224 if (!DriverCaps && !HELCaps)
1226 WARN("Invalid parameters.\n");
1227 return DDERR_INVALIDPARAMS;
1230 memset(&caps, 0, sizeof(caps));
1231 memset(&winecaps, 0, sizeof(winecaps));
1232 caps.dwSize = sizeof(caps);
1234 wined3d_mutex_lock();
1235 hr = wined3d_device_get_device_caps(ddraw->wined3d_device, &winecaps);
1236 if (FAILED(hr))
1238 WARN("IWineD3DDevice::GetDeviceCaps failed\n");
1239 wined3d_mutex_unlock();
1240 return hr;
1243 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
1244 wined3d_mutex_unlock();
1245 if(FAILED(hr)) {
1246 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
1247 return hr;
1250 caps.dwCaps = winecaps.ddraw_caps.caps;
1251 caps.dwCaps2 = winecaps.ddraw_caps.caps2;
1252 caps.dwCKeyCaps = winecaps.ddraw_caps.color_key_caps;
1253 caps.dwFXCaps = winecaps.ddraw_caps.fx_caps;
1254 caps.dwPalCaps = winecaps.ddraw_caps.pal_caps;
1255 caps.ddsCaps.dwCaps = winecaps.ddraw_caps.dds_caps;
1256 caps.dwSVBCaps = winecaps.ddraw_caps.svb_caps;
1257 caps.dwSVBCKeyCaps = winecaps.ddraw_caps.svb_color_key_caps;
1258 caps.dwSVBFXCaps = winecaps.ddraw_caps.svb_fx_caps;
1259 caps.dwVSBCaps = winecaps.ddraw_caps.vsb_caps;
1260 caps.dwVSBCKeyCaps = winecaps.ddraw_caps.vsb_color_key_caps;
1261 caps.dwVSBFXCaps = winecaps.ddraw_caps.vsb_fx_caps;
1262 caps.dwSSBCaps = winecaps.ddraw_caps.ssb_caps;
1263 caps.dwSSBCKeyCaps = winecaps.ddraw_caps.ssb_color_key_caps;
1264 caps.dwSSBFXCaps = winecaps.ddraw_caps.ssb_fx_caps;
1266 /* Even if WineD3D supports 3D rendering, remove the cap if ddraw is configured
1267 * not to use it
1269 if (DefaultSurfaceType == WINED3D_SURFACE_TYPE_GDI)
1271 caps.dwCaps &= ~DDCAPS_3D;
1272 caps.ddsCaps.dwCaps &= ~(DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER);
1274 if (winecaps.ddraw_caps.stride_align)
1276 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
1277 caps.dwAlignStrideAlign = winecaps.ddraw_caps.stride_align;
1280 if(DriverCaps)
1282 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
1283 if (TRACE_ON(ddraw))
1285 TRACE("Driver Caps :\n");
1286 DDRAW_dump_DDCAPS(DriverCaps);
1290 if(HELCaps)
1292 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
1293 if (TRACE_ON(ddraw))
1295 TRACE("HEL Caps :\n");
1296 DDRAW_dump_DDCAPS(HELCaps);
1300 return DD_OK;
1303 static HRESULT WINAPI ddraw4_GetCaps(IDirectDraw4 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1305 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1307 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1309 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1312 static HRESULT WINAPI ddraw2_GetCaps(IDirectDraw2 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1314 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1316 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1318 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1321 static HRESULT WINAPI ddraw1_GetCaps(IDirectDraw *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1323 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1325 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1327 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1330 /*****************************************************************************
1331 * IDirectDraw7::Compact
1333 * No idea what it does, MSDN says it's not implemented.
1335 * Returns
1336 * DD_OK, but this is unchecked
1338 *****************************************************************************/
1339 static HRESULT WINAPI ddraw7_Compact(IDirectDraw7 *iface)
1341 TRACE("iface %p.\n", iface);
1343 return DD_OK;
1346 static HRESULT WINAPI ddraw4_Compact(IDirectDraw4 *iface)
1348 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1350 TRACE("iface %p.\n", iface);
1352 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1355 static HRESULT WINAPI ddraw2_Compact(IDirectDraw2 *iface)
1357 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1359 TRACE("iface %p.\n", iface);
1361 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1364 static HRESULT WINAPI ddraw1_Compact(IDirectDraw *iface)
1366 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1368 TRACE("iface %p.\n", iface);
1370 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1373 /*****************************************************************************
1374 * IDirectDraw7::GetDisplayMode
1376 * Returns information about the current display mode
1378 * Exists in Version 1, 2, 4 and 7
1380 * Params:
1381 * DDSD: Address of a surface description structure to write the info to
1383 * Returns
1384 * DD_OK
1386 *****************************************************************************/
1387 static HRESULT WINAPI ddraw7_GetDisplayMode(IDirectDraw7 *iface, DDSURFACEDESC2 *DDSD)
1389 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1390 struct wined3d_display_mode mode;
1391 HRESULT hr;
1392 DWORD Size;
1394 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
1396 wined3d_mutex_lock();
1397 /* This seems sane */
1398 if (!DDSD)
1400 wined3d_mutex_unlock();
1401 return DDERR_INVALIDPARAMS;
1404 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
1406 ERR("Failed to get display mode, hr %#x.\n", hr);
1407 wined3d_mutex_unlock();
1408 return hr;
1411 Size = DDSD->dwSize;
1412 memset(DDSD, 0, Size);
1414 DDSD->dwSize = Size;
1415 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
1416 DDSD->dwWidth = mode.width;
1417 DDSD->dwHeight = mode.height;
1418 DDSD->u2.dwRefreshRate = 60;
1419 DDSD->ddsCaps.dwCaps = 0;
1420 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
1421 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, mode.format_id);
1422 DDSD->u1.lPitch = mode.width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
1424 if(TRACE_ON(ddraw))
1426 TRACE("Returning surface desc :\n");
1427 DDRAW_dump_surface_desc(DDSD);
1430 wined3d_mutex_unlock();
1432 return DD_OK;
1435 static HRESULT WINAPI ddraw4_GetDisplayMode(IDirectDraw4 *iface, DDSURFACEDESC2 *surface_desc)
1437 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1439 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1441 return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, surface_desc);
1444 static HRESULT WINAPI ddraw2_GetDisplayMode(IDirectDraw2 *iface, DDSURFACEDESC *surface_desc)
1446 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1448 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1450 /* FIXME: Test sizes, properly convert surface_desc */
1451 return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1454 static HRESULT WINAPI ddraw1_GetDisplayMode(IDirectDraw *iface, DDSURFACEDESC *surface_desc)
1456 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1458 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1460 /* FIXME: Test sizes, properly convert surface_desc */
1461 return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1464 /*****************************************************************************
1465 * IDirectDraw7::GetFourCCCodes
1467 * Returns an array of supported FourCC codes.
1469 * Exists in Version 1, 2, 4 and 7
1471 * Params:
1472 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
1473 * of enumerated codes
1474 * Codes: Pointer to an array of DWORDs where the supported codes are written
1475 * to
1477 * Returns
1478 * Always returns DD_OK, as it's a stub for now
1480 *****************************************************************************/
1481 static HRESULT WINAPI ddraw7_GetFourCCCodes(IDirectDraw7 *iface, DWORD *NumCodes, DWORD *Codes)
1483 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1484 static const enum wined3d_format_id formats[] =
1486 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
1487 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
1488 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
1490 struct wined3d_display_mode mode;
1491 DWORD count = 0, i, outsize;
1492 HRESULT hr;
1494 TRACE("iface %p, codes_count %p, codes %p.\n", iface, NumCodes, Codes);
1496 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
1498 ERR("Failed to get display mode, hr %#x.\n", hr);
1499 return hr;
1502 outsize = NumCodes && Codes ? *NumCodes : 0;
1504 for (i = 0; i < (sizeof(formats) / sizeof(formats[0])); ++i)
1506 hr = wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, WINED3D_DEVICE_TYPE_HAL,
1507 mode.format_id, 0, WINED3D_RTYPE_SURFACE, formats[i], DefaultSurfaceType);
1508 if (SUCCEEDED(hr))
1510 if (count < outsize)
1511 Codes[count] = formats[i];
1512 ++count;
1515 if(NumCodes) {
1516 TRACE("Returning %u FourCC codes\n", count);
1517 *NumCodes = count;
1520 return DD_OK;
1523 static HRESULT WINAPI ddraw4_GetFourCCCodes(IDirectDraw4 *iface, DWORD *codes_count, DWORD *codes)
1525 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1527 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1529 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1532 static HRESULT WINAPI ddraw2_GetFourCCCodes(IDirectDraw2 *iface, DWORD *codes_count, DWORD *codes)
1534 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1536 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1538 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1541 static HRESULT WINAPI ddraw1_GetFourCCCodes(IDirectDraw *iface, DWORD *codes_count, DWORD *codes)
1543 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1545 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1547 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1550 static HRESULT WINAPI ddraw7_GetMonitorFrequency(IDirectDraw7 *iface, DWORD *frequency)
1552 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1553 struct wined3d_display_mode mode;
1554 HRESULT hr;
1556 TRACE("iface %p, frequency %p.\n", iface, frequency);
1558 wined3d_mutex_lock();
1559 hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL);
1560 wined3d_mutex_unlock();
1561 if (FAILED(hr))
1563 WARN("Failed to get display mode, hr %#x.\n", hr);
1564 return hr;
1567 *frequency = mode.refresh_rate;
1569 return DD_OK;
1572 static HRESULT WINAPI ddraw4_GetMonitorFrequency(IDirectDraw4 *iface, DWORD *frequency)
1574 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1576 TRACE("iface %p, frequency %p.\n", iface, frequency);
1578 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1581 static HRESULT WINAPI ddraw2_GetMonitorFrequency(IDirectDraw2 *iface, DWORD *frequency)
1583 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1585 TRACE("iface %p, frequency %p.\n", iface, frequency);
1587 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1590 static HRESULT WINAPI ddraw1_GetMonitorFrequency(IDirectDraw *iface, DWORD *frequency)
1592 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1594 TRACE("iface %p, frequency %p.\n", iface, frequency);
1596 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1599 static HRESULT WINAPI ddraw7_GetVerticalBlankStatus(IDirectDraw7 *iface, BOOL *status)
1601 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1602 struct wined3d_raster_status raster_status;
1603 HRESULT hr;
1605 TRACE("iface %p, status %p.\n", iface, status);
1607 if(!status)
1608 return DDERR_INVALIDPARAMS;
1610 wined3d_mutex_lock();
1611 hr = wined3d_get_adapter_raster_status(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &raster_status);
1612 wined3d_mutex_unlock();
1613 if (FAILED(hr))
1615 WARN("Failed to get raster status, hr %#x.\n", hr);
1616 return hr;
1619 *status = raster_status.in_vblank;
1621 return DD_OK;
1624 static HRESULT WINAPI ddraw4_GetVerticalBlankStatus(IDirectDraw4 *iface, BOOL *status)
1626 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1628 TRACE("iface %p, status %p.\n", iface, status);
1630 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1633 static HRESULT WINAPI ddraw2_GetVerticalBlankStatus(IDirectDraw2 *iface, BOOL *status)
1635 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1637 TRACE("iface %p, status %p.\n", iface, status);
1639 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1642 static HRESULT WINAPI ddraw1_GetVerticalBlankStatus(IDirectDraw *iface, BOOL *status)
1644 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1646 TRACE("iface %p, status %p.\n", iface, status);
1648 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1651 /*****************************************************************************
1652 * IDirectDraw7::GetAvailableVidMem
1654 * Returns the total and free video memory
1656 * Params:
1657 * Caps: Specifies the memory type asked for
1658 * total: Pointer to a DWORD to be filled with the total memory
1659 * free: Pointer to a DWORD to be filled with the free memory
1661 * Returns
1662 * DD_OK on success
1663 * DDERR_INVALIDPARAMS of free and total are NULL
1665 *****************************************************************************/
1666 static HRESULT WINAPI ddraw7_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total,
1667 DWORD *free)
1669 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1670 HRESULT hr = DD_OK;
1672 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, Caps, total, free);
1674 if (TRACE_ON(ddraw))
1676 TRACE("Asked for memory with description: ");
1677 DDRAW_dump_DDSCAPS2(Caps);
1679 wined3d_mutex_lock();
1681 /* Todo: System memory vs local video memory vs non-local video memory
1682 * The MSDN also mentions differences between texture memory and other
1683 * resources, but that's not important
1686 if( (!total) && (!free) )
1688 wined3d_mutex_unlock();
1689 return DDERR_INVALIDPARAMS;
1692 if (free)
1693 *free = wined3d_device_get_available_texture_mem(ddraw->wined3d_device);
1694 if (total)
1696 struct wined3d_adapter_identifier desc = {0};
1698 hr = wined3d_get_adapter_identifier(ddraw->wined3d, WINED3DADAPTER_DEFAULT, 0, &desc);
1699 *total = desc.video_memory;
1702 wined3d_mutex_unlock();
1704 return hr;
1707 static HRESULT WINAPI ddraw4_GetAvailableVidMem(IDirectDraw4 *iface,
1708 DDSCAPS2 *caps, DWORD *total, DWORD *free)
1710 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1712 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1714 return ddraw7_GetAvailableVidMem(&ddraw->IDirectDraw7_iface, caps, total, free);
1717 static HRESULT WINAPI ddraw2_GetAvailableVidMem(IDirectDraw2 *iface,
1718 DDSCAPS *caps, DWORD *total, DWORD *free)
1720 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1721 DDSCAPS2 caps2;
1723 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1725 DDRAW_Convert_DDSCAPS_1_To_2(caps, &caps2);
1726 return ddraw7_GetAvailableVidMem(&ddraw->IDirectDraw7_iface, &caps2, total, free);
1729 /*****************************************************************************
1730 * IDirectDraw7::Initialize
1732 * Initializes a DirectDraw interface.
1734 * Params:
1735 * GUID: Interface identifier. Well, don't know what this is really good
1736 * for
1738 * Returns
1739 * Returns DD_OK on the first call,
1740 * DDERR_ALREADYINITIALIZED on repeated calls
1742 *****************************************************************************/
1743 static HRESULT WINAPI ddraw7_Initialize(IDirectDraw7 *iface, GUID *guid)
1745 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1747 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1749 if (ddraw->initialized)
1750 return DDERR_ALREADYINITIALIZED;
1752 /* FIXME: To properly take the GUID into account we should call
1753 * ddraw_init() here instead of in DDRAW_Create(). */
1754 if (guid)
1755 FIXME("Ignoring guid %s.\n", debugstr_guid(guid));
1757 ddraw->initialized = TRUE;
1758 return DD_OK;
1761 static HRESULT WINAPI ddraw4_Initialize(IDirectDraw4 *iface, GUID *guid)
1763 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1765 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1767 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
1770 static HRESULT WINAPI ddraw2_Initialize(IDirectDraw2 *iface, GUID *guid)
1772 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1774 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1776 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
1779 static HRESULT WINAPI ddraw1_Initialize(IDirectDraw *iface, GUID *guid)
1781 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1783 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1785 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
1788 static HRESULT WINAPI d3d1_Initialize(IDirect3D *iface, REFIID riid)
1790 TRACE("iface %p, riid %s.\n", iface, debugstr_guid(riid));
1792 return DDERR_ALREADYINITIALIZED;
1795 /*****************************************************************************
1796 * IDirectDraw7::FlipToGDISurface
1798 * "Makes the surface that the GDI writes to the primary surface"
1799 * Looks like some windows specific thing we don't have to care about.
1800 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1801 * show error boxes ;)
1802 * Well, just return DD_OK.
1804 * Returns:
1805 * Always returns DD_OK
1807 *****************************************************************************/
1808 static HRESULT WINAPI ddraw7_FlipToGDISurface(IDirectDraw7 *iface)
1810 FIXME("iface %p stub!\n", iface);
1812 return DD_OK;
1815 static HRESULT WINAPI ddraw4_FlipToGDISurface(IDirectDraw4 *iface)
1817 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1819 TRACE("iface %p.\n", iface);
1821 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
1824 static HRESULT WINAPI ddraw2_FlipToGDISurface(IDirectDraw2 *iface)
1826 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1828 TRACE("iface %p.\n", iface);
1830 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
1833 static HRESULT WINAPI ddraw1_FlipToGDISurface(IDirectDraw *iface)
1835 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1837 TRACE("iface %p.\n", iface);
1839 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
1842 /*****************************************************************************
1843 * IDirectDraw7::WaitForVerticalBlank
1845 * This method allows applications to get in sync with the vertical blank
1846 * interval.
1847 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1848 * redraw the screen, most likely because of this stub
1850 * Parameters:
1851 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1852 * or DDWAITVB_BLOCKEND
1853 * h: Not used, according to MSDN
1855 * Returns:
1856 * Always returns DD_OK
1858 *****************************************************************************/
1859 static HRESULT WINAPI ddraw7_WaitForVerticalBlank(IDirectDraw7 *iface, DWORD Flags, HANDLE event)
1861 static BOOL hide;
1863 TRACE("iface %p, flags %#x, event %p.\n", iface, Flags, event);
1865 /* This function is called often, so print the fixme only once */
1866 if(!hide)
1868 FIXME("iface %p, flags %#x, event %p stub!\n", iface, Flags, event);
1869 hide = TRUE;
1872 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1873 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1874 return DDERR_UNSUPPORTED; /* unchecked */
1876 return DD_OK;
1879 static HRESULT WINAPI ddraw4_WaitForVerticalBlank(IDirectDraw4 *iface, DWORD flags, HANDLE event)
1881 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1883 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1885 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
1888 static HRESULT WINAPI ddraw2_WaitForVerticalBlank(IDirectDraw2 *iface, DWORD flags, HANDLE event)
1890 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1892 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1894 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
1897 static HRESULT WINAPI ddraw1_WaitForVerticalBlank(IDirectDraw *iface, DWORD flags, HANDLE event)
1899 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1901 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1903 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
1906 static HRESULT WINAPI ddraw7_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1908 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1909 struct wined3d_raster_status raster_status;
1910 HRESULT hr;
1912 TRACE("iface %p, line %p.\n", iface, Scanline);
1914 wined3d_mutex_lock();
1915 hr = wined3d_get_adapter_raster_status(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &raster_status);
1916 wined3d_mutex_unlock();
1917 if (FAILED(hr))
1919 WARN("Failed to get raster status, hr %#x.\n", hr);
1920 return hr;
1923 *Scanline = raster_status.scan_line;
1925 if (raster_status.in_vblank)
1926 return DDERR_VERTICALBLANKINPROGRESS;
1928 return DD_OK;
1931 static HRESULT WINAPI ddraw4_GetScanLine(IDirectDraw4 *iface, DWORD *line)
1933 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1935 TRACE("iface %p, line %p.\n", iface, line);
1937 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
1940 static HRESULT WINAPI ddraw2_GetScanLine(IDirectDraw2 *iface, DWORD *line)
1942 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1944 TRACE("iface %p, line %p.\n", iface, line);
1946 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
1949 static HRESULT WINAPI ddraw1_GetScanLine(IDirectDraw *iface, DWORD *line)
1951 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1953 TRACE("iface %p, line %p.\n", iface, line);
1955 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
1958 /*****************************************************************************
1959 * IDirectDraw7::TestCooperativeLevel
1961 * Informs the application about the state of the video adapter, depending
1962 * on the cooperative level
1964 * Returns:
1965 * DD_OK if the device is in a sane state
1966 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1967 * if the state is not correct(See below)
1969 *****************************************************************************/
1970 static HRESULT WINAPI ddraw7_TestCooperativeLevel(IDirectDraw7 *iface)
1972 TRACE("iface %p.\n", iface);
1974 return DD_OK;
1977 static HRESULT WINAPI ddraw4_TestCooperativeLevel(IDirectDraw4 *iface)
1979 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1981 TRACE("iface %p.\n", iface);
1983 return ddraw7_TestCooperativeLevel(&ddraw->IDirectDraw7_iface);
1986 /*****************************************************************************
1987 * IDirectDraw7::GetGDISurface
1989 * Returns the surface that GDI is treating as the primary surface.
1990 * For Wine this is the front buffer
1992 * Params:
1993 * GDISurface: Address to write the surface pointer to
1995 * Returns:
1996 * DD_OK if the surface was found
1997 * DDERR_NOTFOUND if the GDI surface wasn't found
1999 *****************************************************************************/
2000 static HRESULT WINAPI ddraw7_GetGDISurface(IDirectDraw7 *iface, IDirectDrawSurface7 **GDISurface)
2002 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2004 TRACE("iface %p, surface %p.\n", iface, GDISurface);
2006 wined3d_mutex_lock();
2008 if (!(*GDISurface = &ddraw->primary->IDirectDrawSurface7_iface))
2010 WARN("Primary not created yet.\n");
2011 wined3d_mutex_unlock();
2012 return DDERR_NOTFOUND;
2014 IDirectDrawSurface7_AddRef(*GDISurface);
2016 wined3d_mutex_unlock();
2018 return DD_OK;
2021 static HRESULT WINAPI ddraw4_GetGDISurface(IDirectDraw4 *iface, IDirectDrawSurface4 **surface)
2023 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2024 struct ddraw_surface *surface_impl;
2025 IDirectDrawSurface7 *surface7;
2026 HRESULT hr;
2028 TRACE("iface %p, surface %p.\n", iface, surface);
2030 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2031 if (FAILED(hr))
2033 *surface = NULL;
2034 return hr;
2036 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2037 *surface = &surface_impl->IDirectDrawSurface4_iface;
2038 IDirectDrawSurface4_AddRef(*surface);
2039 IDirectDrawSurface7_Release(surface7);
2041 return hr;
2044 static HRESULT WINAPI ddraw2_GetGDISurface(IDirectDraw2 *iface, IDirectDrawSurface **surface)
2046 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2047 struct ddraw_surface *surface_impl;
2048 IDirectDrawSurface7 *surface7;
2049 HRESULT hr;
2051 TRACE("iface %p, surface %p.\n", iface, surface);
2053 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2054 if (FAILED(hr))
2056 *surface = NULL;
2057 return hr;
2059 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2060 *surface = &surface_impl->IDirectDrawSurface_iface;
2061 IDirectDrawSurface_AddRef(*surface);
2062 IDirectDrawSurface7_Release(surface7);
2064 return hr;
2067 static HRESULT WINAPI ddraw1_GetGDISurface(IDirectDraw *iface, IDirectDrawSurface **surface)
2069 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2070 struct ddraw_surface *surface_impl;
2071 IDirectDrawSurface7 *surface7;
2072 HRESULT hr;
2074 TRACE("iface %p, surface %p.\n", iface, surface);
2076 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2077 if (FAILED(hr))
2079 *surface = NULL;
2080 return hr;
2082 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2083 *surface = &surface_impl->IDirectDrawSurface_iface;
2084 IDirectDrawSurface_AddRef(*surface);
2085 IDirectDrawSurface7_Release(surface7);
2087 return hr;
2090 struct displaymodescallback_context
2092 LPDDENUMMODESCALLBACK func;
2093 void *context;
2096 static HRESULT CALLBACK EnumDisplayModesCallbackThunk(DDSURFACEDESC2 *surface_desc, void *context)
2098 struct displaymodescallback_context *cbcontext = context;
2099 DDSURFACEDESC desc;
2101 DDSD2_to_DDSD(surface_desc, &desc);
2102 return cbcontext->func(&desc, cbcontext->context);
2105 /*****************************************************************************
2106 * IDirectDraw7::EnumDisplayModes
2108 * Enumerates the supported Display modes. The modes can be filtered with
2109 * the DDSD parameter.
2111 * Params:
2112 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES. For old ddraw
2113 * versions (3 and older?) this is reserved and must be 0.
2114 * DDSD: Surface description to filter the modes
2115 * Context: Pointer passed back to the callback function
2116 * cb: Application-provided callback function
2118 * Returns:
2119 * DD_OK on success
2120 * DDERR_INVALIDPARAMS if the callback wasn't set
2122 *****************************************************************************/
2123 static HRESULT WINAPI ddraw7_EnumDisplayModes(IDirectDraw7 *iface, DWORD Flags,
2124 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMMODESCALLBACK2 cb)
2126 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2127 struct wined3d_display_mode *enum_modes = NULL;
2128 struct wined3d_display_mode mode;
2129 unsigned int modenum, fmt;
2130 DDSURFACEDESC2 callback_sd;
2131 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
2132 DDPIXELFORMAT pixelformat;
2134 static const enum wined3d_format_id checkFormatList[] =
2136 WINED3DFMT_B8G8R8X8_UNORM,
2137 WINED3DFMT_B5G6R5_UNORM,
2138 WINED3DFMT_P8_UINT,
2141 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2142 iface, Flags, DDSD, Context, cb);
2144 if (!cb)
2145 return DDERR_INVALIDPARAMS;
2147 wined3d_mutex_lock();
2148 if(!(Flags & DDEDM_REFRESHRATES))
2150 enum_mode_array_size = 16;
2151 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(*enum_modes) * enum_mode_array_size);
2152 if (!enum_modes)
2154 ERR("Out of memory\n");
2155 wined3d_mutex_unlock();
2156 return DDERR_OUTOFMEMORY;
2160 pixelformat.dwSize = sizeof(pixelformat);
2161 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
2163 modenum = 0;
2164 while (wined3d_enum_adapter_modes(ddraw->wined3d, WINED3DADAPTER_DEFAULT, checkFormatList[fmt],
2165 WINED3D_SCANLINE_ORDERING_UNKNOWN, modenum++, &mode) == WINED3D_OK)
2167 PixelFormat_WineD3DtoDD(&pixelformat, mode.format_id);
2168 if (DDSD)
2170 if (DDSD->dwFlags & DDSD_WIDTH && mode.width != DDSD->dwWidth)
2171 continue;
2172 if (DDSD->dwFlags & DDSD_HEIGHT && mode.height != DDSD->dwHeight)
2173 continue;
2174 if (DDSD->dwFlags & DDSD_REFRESHRATE && mode.refresh_rate != DDSD->u2.dwRefreshRate)
2175 continue;
2176 if (DDSD->dwFlags & DDSD_PIXELFORMAT
2177 && pixelformat.u1.dwRGBBitCount != DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount)
2178 continue;
2181 if(!(Flags & DDEDM_REFRESHRATES))
2183 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
2184 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
2185 * to be reduced to a single unique result in such case.
2187 BOOL found = FALSE;
2188 unsigned i;
2190 for (i = 0; i < enum_mode_count; i++)
2192 if (enum_modes[i].width == mode.width && enum_modes[i].height == mode.height
2193 && enum_modes[i].format_id == mode.format_id)
2195 found = TRUE;
2196 break;
2200 if(found) continue;
2203 memset(&callback_sd, 0, sizeof(callback_sd));
2204 callback_sd.dwSize = sizeof(callback_sd);
2205 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2207 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH|DDSD_REFRESHRATE;
2208 if (Flags & DDEDM_REFRESHRATES)
2209 callback_sd.u2.dwRefreshRate = mode.refresh_rate;
2211 callback_sd.dwWidth = mode.width;
2212 callback_sd.dwHeight = mode.height;
2214 callback_sd.u4.ddpfPixelFormat=pixelformat;
2216 /* Calc pitch and DWORD align like MSDN says */
2217 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.width;
2218 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
2220 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
2221 callback_sd.u2.dwRefreshRate);
2223 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
2225 TRACE("Application asked to terminate the enumeration\n");
2226 HeapFree(GetProcessHeap(), 0, enum_modes);
2227 wined3d_mutex_unlock();
2228 return DD_OK;
2231 if(!(Flags & DDEDM_REFRESHRATES))
2233 if (enum_mode_count == enum_mode_array_size)
2235 struct wined3d_display_mode *new_enum_modes;
2237 enum_mode_array_size *= 2;
2238 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes,
2239 sizeof(*new_enum_modes) * enum_mode_array_size);
2240 if (!new_enum_modes)
2242 ERR("Out of memory\n");
2243 HeapFree(GetProcessHeap(), 0, enum_modes);
2244 wined3d_mutex_unlock();
2245 return DDERR_OUTOFMEMORY;
2248 enum_modes = new_enum_modes;
2251 enum_modes[enum_mode_count++] = mode;
2256 TRACE("End of enumeration\n");
2257 HeapFree(GetProcessHeap(), 0, enum_modes);
2258 wined3d_mutex_unlock();
2260 return DD_OK;
2263 static HRESULT WINAPI ddraw4_EnumDisplayModes(IDirectDraw4 *iface, DWORD flags,
2264 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMMODESCALLBACK2 callback)
2266 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2268 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2269 iface, flags, surface_desc, context, callback);
2271 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags, surface_desc, context, callback);
2274 static HRESULT WINAPI ddraw2_EnumDisplayModes(IDirectDraw2 *iface, DWORD flags,
2275 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2277 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2278 struct displaymodescallback_context cbcontext;
2279 DDSURFACEDESC2 surface_desc2;
2281 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2282 iface, flags, surface_desc, context, callback);
2284 cbcontext.func = callback;
2285 cbcontext.context = context;
2287 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2288 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags,
2289 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2292 static HRESULT WINAPI ddraw1_EnumDisplayModes(IDirectDraw *iface, DWORD flags,
2293 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2295 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2296 struct displaymodescallback_context cbcontext;
2297 DDSURFACEDESC2 surface_desc2;
2299 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2300 iface, flags, surface_desc, context, callback);
2302 cbcontext.func = callback;
2303 cbcontext.context = context;
2305 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2306 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags,
2307 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2310 /*****************************************************************************
2311 * IDirectDraw7::EvaluateMode
2313 * Used with IDirectDraw7::StartModeTest to test video modes.
2314 * EvaluateMode is used to pass or fail a mode, and continue with the next
2315 * mode
2317 * Params:
2318 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
2319 * Timeout: Returns the amount of seconds left before the mode would have
2320 * been failed automatically
2322 * Returns:
2323 * This implementation always DD_OK, because it's a stub
2325 *****************************************************************************/
2326 static HRESULT WINAPI ddraw7_EvaluateMode(IDirectDraw7 *iface, DWORD Flags, DWORD *Timeout)
2328 FIXME("iface %p, flags %#x, timeout %p stub!\n", iface, Flags, Timeout);
2330 /* When implementing this, implement it in WineD3D */
2332 return DD_OK;
2335 /*****************************************************************************
2336 * IDirectDraw7::GetDeviceIdentifier
2338 * Returns the device identifier, which gives information about the driver
2339 * Our device identifier is defined at the beginning of this file.
2341 * Params:
2342 * DDDI: Address for the returned structure
2343 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
2345 * Returns:
2346 * On success it returns DD_OK
2347 * DDERR_INVALIDPARAMS if DDDI is NULL
2349 *****************************************************************************/
2350 static HRESULT WINAPI ddraw7_GetDeviceIdentifier(IDirectDraw7 *iface,
2351 DDDEVICEIDENTIFIER2 *DDDI, DWORD Flags)
2353 TRACE("iface %p, device_identifier %p, flags %#x.\n", iface, DDDI, Flags);
2355 if(!DDDI)
2356 return DDERR_INVALIDPARAMS;
2358 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
2359 * host adapter, if there's a secondary 3D adapter. This doesn't apply
2360 * to any modern hardware, nor is it interesting for Wine, so ignore it.
2361 * Size of DDDEVICEIDENTIFIER2 may be aligned to 8 bytes and thus 4
2362 * bytes too long. So only copy the relevant part of the structure
2365 memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
2366 return DD_OK;
2369 static HRESULT WINAPI ddraw4_GetDeviceIdentifier(IDirectDraw4 *iface,
2370 DDDEVICEIDENTIFIER *identifier, DWORD flags)
2372 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2373 DDDEVICEIDENTIFIER2 identifier2;
2374 HRESULT hr;
2376 TRACE("iface %p, identifier %p, flags %#x.\n", iface, identifier, flags);
2378 hr = ddraw7_GetDeviceIdentifier(&ddraw->IDirectDraw7_iface, &identifier2, flags);
2379 DDRAW_Convert_DDDEVICEIDENTIFIER_2_To_1(&identifier2, identifier);
2381 return hr;
2384 /*****************************************************************************
2385 * IDirectDraw7::GetSurfaceFromDC
2387 * Returns the Surface for a GDI device context handle.
2388 * Is this related to IDirectDrawSurface::GetDC ???
2390 * Params:
2391 * hdc: hdc to return the surface for
2392 * Surface: Address to write the surface pointer to
2394 * Returns:
2395 * Always returns DD_OK because it's a stub
2397 *****************************************************************************/
2398 static HRESULT WINAPI ddraw7_GetSurfaceFromDC(IDirectDraw7 *iface, HDC hdc,
2399 IDirectDrawSurface7 **Surface)
2401 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2402 struct wined3d_surface *wined3d_surface;
2403 struct ddraw_surface *surface_impl;
2405 TRACE("iface %p, dc %p, surface %p.\n", iface, hdc, Surface);
2407 if (!Surface) return E_INVALIDARG;
2409 if (!(wined3d_surface = wined3d_device_get_surface_from_dc(ddraw->wined3d_device, hdc)))
2411 TRACE("No surface found for dc %p.\n", hdc);
2412 *Surface = NULL;
2413 return DDERR_NOTFOUND;
2416 surface_impl = wined3d_surface_get_parent(wined3d_surface);
2417 *Surface = &surface_impl->IDirectDrawSurface7_iface;
2418 IDirectDrawSurface7_AddRef(*Surface);
2419 TRACE("Returning surface %p.\n", Surface);
2420 return DD_OK;
2423 static HRESULT WINAPI ddraw4_GetSurfaceFromDC(IDirectDraw4 *iface, HDC dc,
2424 IDirectDrawSurface4 **surface)
2426 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2427 struct ddraw_surface *surface_impl;
2428 IDirectDrawSurface7 *surface7;
2429 HRESULT hr;
2431 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2433 if (!surface) return E_INVALIDARG;
2435 hr = ddraw7_GetSurfaceFromDC(&ddraw->IDirectDraw7_iface, dc, &surface7);
2436 if (FAILED(hr))
2438 *surface = NULL;
2439 return hr;
2441 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2442 /* Tests say this is true */
2443 *surface = (IDirectDrawSurface4 *)&surface_impl->IDirectDrawSurface_iface;
2444 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
2445 IDirectDrawSurface7_Release(surface7);
2447 return hr;
2450 /*****************************************************************************
2451 * IDirectDraw7::RestoreAllSurfaces
2453 * Calls the restore method of all surfaces
2455 * Params:
2457 * Returns:
2458 * Always returns DD_OK because it's a stub
2460 *****************************************************************************/
2461 static HRESULT WINAPI ddraw7_RestoreAllSurfaces(IDirectDraw7 *iface)
2463 FIXME("iface %p stub!\n", iface);
2465 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
2466 * get their parent and call their restore method. Do not implement
2467 * it in WineD3D, as restoring a surface means re-creating the
2468 * WineD3DDSurface
2470 return DD_OK;
2473 static HRESULT WINAPI ddraw4_RestoreAllSurfaces(IDirectDraw4 *iface)
2475 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2477 TRACE("iface %p.\n", iface);
2479 return ddraw7_RestoreAllSurfaces(&ddraw->IDirectDraw7_iface);
2482 /*****************************************************************************
2483 * IDirectDraw7::StartModeTest
2485 * Tests the specified video modes to update the system registry with
2486 * refresh rate information. StartModeTest starts the mode test,
2487 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
2488 * isn't called within 15 seconds, the mode is failed automatically
2490 * As refresh rates are handled by the X server, I don't think this
2491 * Method is important
2493 * Params:
2494 * Modes: An array of mode specifications
2495 * NumModes: The number of modes in Modes
2496 * Flags: Some flags...
2498 * Returns:
2499 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
2500 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
2501 * otherwise DD_OK
2503 *****************************************************************************/
2504 static HRESULT WINAPI ddraw7_StartModeTest(IDirectDraw7 *iface, SIZE *Modes, DWORD NumModes, DWORD Flags)
2506 FIXME("iface %p, modes %p, mode_count %u, flags %#x partial stub!\n",
2507 iface, Modes, NumModes, Flags);
2509 /* This looks sane */
2510 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
2512 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
2513 * As it is not, DDERR_TESTFINISHED is returned
2514 * (hopefully that's correct
2516 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
2517 * well, that value doesn't (yet) exist in the wine headers, so ignore it
2520 return DD_OK;
2523 /*****************************************************************************
2524 * ddraw_create_surface
2526 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
2527 * with the passed parameters.
2529 * Params:
2530 * DDSD: Description of the surface to create
2531 * Surf: Address to store the interface pointer at
2533 * Returns:
2534 * DD_OK on success
2536 *****************************************************************************/
2537 static HRESULT ddraw_create_surface(struct ddraw *ddraw, DDSURFACEDESC2 *pDDSD,
2538 struct ddraw_surface **surface, UINT level, UINT version)
2540 HRESULT hr;
2542 TRACE("ddraw %p, surface_desc %p, surface %p, level %u.\n",
2543 ddraw, pDDSD, surface, level);
2545 if (TRACE_ON(ddraw))
2547 TRACE("Requesting surface desc:\n");
2548 DDRAW_dump_surface_desc(pDDSD);
2551 if ((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && DefaultSurfaceType != WINED3D_SURFACE_TYPE_OPENGL)
2553 WARN("The application requests a 3D capable surface, but a non-OpenGL surface type was set in the registry.\n");
2554 /* Do not fail surface creation, only fail 3D device creation. */
2557 /* Create the Surface object */
2558 *surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**surface));
2559 if (!*surface)
2561 ERR("Failed to allocate surface memory.\n");
2562 return DDERR_OUTOFVIDEOMEMORY;
2565 hr = ddraw_surface_init(*surface, ddraw, pDDSD, level, version);
2566 if (FAILED(hr))
2568 WARN("Failed to initialize surface, hr %#x.\n", hr);
2569 HeapFree(GetProcessHeap(), 0, *surface);
2570 return hr;
2573 /* Increase the surface counter, and attach the surface */
2574 list_add_head(&ddraw->surface_list, &(*surface)->surface_list_entry);
2576 TRACE("Created surface %p.\n", *surface);
2578 return DD_OK;
2581 static HRESULT CDECL ddraw_reset_enum_callback(struct wined3d_resource *resource)
2583 return DD_OK;
2586 /*****************************************************************************
2587 * IDirectDraw7::CreateSurface
2589 * Creates a new IDirectDrawSurface object and returns its interface.
2591 * The surface connections with wined3d are a bit tricky. Basically it works
2592 * like this:
2594 * |------------------------| |-----------------|
2595 * | DDraw surface | | WineD3DSurface |
2596 * | | | |
2597 * | WineD3DSurface |-------------->| |
2598 * | Child |<------------->| Parent |
2599 * |------------------------| |-----------------|
2601 * The DDraw surface is the parent of the wined3d surface, and it releases
2602 * the WineD3DSurface when the ddraw surface is destroyed.
2604 * However, for all surfaces which can be in a container in WineD3D,
2605 * we have to do this. These surfaces are usually complex surfaces,
2606 * so this concerns primary surfaces with a front and a back buffer,
2607 * and textures.
2609 * |------------------------| |-----------------|
2610 * | DDraw surface | | Container |
2611 * | | | |
2612 * | Child |<------------->| Parent |
2613 * | Texture |<------------->| |
2614 * | WineD3DSurface |<----| | Levels |<--|
2615 * | Complex connection | | | | |
2616 * |------------------------| | |-----------------| |
2617 * ^ | |
2618 * | | |
2619 * | | |
2620 * | |------------------| | |-----------------| |
2621 * | | IParent | |-------->| WineD3DSurface | |
2622 * | | | | | |
2623 * | | Child |<------------->| Parent | |
2624 * | | | | Container |<--|
2625 * | |------------------| |-----------------| |
2626 * | |
2627 * | |----------------------| |
2628 * | | DDraw surface 2 | |
2629 * | | | |
2630 * |<->| Complex root Child | |
2631 * | | Texture | |
2632 * | | WineD3DSurface |<----| |
2633 * | |----------------------| | |
2634 * | | |
2635 * | |---------------------| | |-----------------| |
2636 * | | IParent | |----->| WineD3DSurface | |
2637 * | | | | | |
2638 * | | Child |<---------->| Parent | |
2639 * | |---------------------| | Container |<--|
2640 * | |-----------------| |
2641 * | |
2642 * | ---More surfaces can follow--- |
2644 * The reason is that the IWineD3DSwapchain(render target container)
2645 * and the IWineD3DTexure(Texture container) release the parents
2646 * of their surface's children, but by releasing the complex root
2647 * the surfaces which are complexly attached to it are destroyed
2648 * too. See IDirectDrawSurface::Release for a more detailed
2649 * explanation.
2651 * Params:
2652 * DDSD: Description of the surface to create
2653 * Surf: Address to store the interface pointer at
2654 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2655 * aggregation, so it has to be NULL
2657 * Returns:
2658 * DD_OK on success
2659 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2660 * DDERR_* if an error occurs
2662 *****************************************************************************/
2663 static HRESULT CreateSurface(struct ddraw *ddraw, DDSURFACEDESC2 *DDSD,
2664 struct ddraw_surface **surface, IUnknown *UnkOuter, UINT version)
2666 struct ddraw_surface *object = NULL;
2667 struct wined3d_display_mode mode;
2668 HRESULT hr;
2669 DDSURFACEDESC2 desc2;
2670 const DWORD sysvidmem = DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
2672 TRACE("ddraw %p, surface_desc %p, surface %p, outer_unknown %p.\n", ddraw, DDSD, surface, UnkOuter);
2674 /* Some checks before we start */
2675 if (TRACE_ON(ddraw))
2677 TRACE(" (%p) Requesting surface desc :\n", ddraw);
2678 DDRAW_dump_surface_desc(DDSD);
2681 if (UnkOuter != NULL)
2683 FIXME("(%p) : outer != NULL?\n", ddraw);
2684 return CLASS_E_NOAGGREGATION; /* unchecked */
2687 if (!surface)
2689 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", ddraw);
2690 return E_POINTER; /* unchecked */
2693 if (!(DDSD->dwFlags & DDSD_CAPS))
2695 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2696 DDSD->dwFlags |= DDSD_CAPS;
2699 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2701 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2702 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2705 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2707 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2708 WARN("(%p) Null surface pointer specified, ignore it!\n", ddraw);
2709 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2712 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2713 !(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
2715 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n",
2716 ddraw);
2717 *surface = NULL;
2718 return DDERR_NOEXCLUSIVEMODE;
2721 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_BACKBUFFER | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_BACKBUFFER | DDSCAPS_PRIMARYSURFACE))
2723 WARN("Application wanted to create back buffer primary surface\n");
2724 return DDERR_INVALIDCAPS;
2727 if((DDSD->ddsCaps.dwCaps & sysvidmem) == sysvidmem)
2729 /* This is a special switch in ddrawex.dll, but not allowed in ddraw.dll */
2730 WARN("Application tries to put the surface in both system and video memory\n");
2731 *surface = NULL;
2732 return DDERR_INVALIDCAPS;
2735 /* Check cube maps but only if the size includes them */
2736 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2738 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2739 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2741 WARN("Cube map faces requested without cube map flag\n");
2742 return DDERR_INVALIDCAPS;
2744 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2745 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2747 WARN("Cube map without faces requested\n");
2748 return DDERR_INVALIDPARAMS;
2751 /* Quick tests confirm those can be created, but we don't do that yet */
2752 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2753 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2755 FIXME("Partial cube maps not supported yet\n");
2759 /* According to the msdn this flag is ignored by CreateSurface */
2760 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2761 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2763 /* Modify some flags */
2764 copy_to_surfacedesc2(&desc2, DDSD);
2765 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2767 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
2769 ERR("Failed to get display mode, hr %#x.\n", hr);
2770 return hr;
2773 /* No pixelformat given? Use the current screen format */
2774 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2776 desc2.dwFlags |= DDSD_PIXELFORMAT;
2777 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2779 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, mode.format_id);
2782 /* No Width or no Height? Use the original screen size
2784 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2785 !(desc2.dwFlags & DDSD_HEIGHT) )
2787 /* Invalid for non-render targets */
2788 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2790 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2791 *surface = NULL;
2792 return DDERR_INVALIDPARAMS;
2795 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2796 desc2.dwWidth = mode.width;
2797 desc2.dwHeight = mode.height;
2800 if (!desc2.dwWidth || !desc2.dwHeight)
2801 return DDERR_INVALIDPARAMS;
2803 /* Mipmap count fixes */
2804 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2806 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2808 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2810 /* Mipmap count is given, should not be 0 */
2811 if( desc2.u2.dwMipMapCount == 0 )
2812 return DDERR_INVALIDPARAMS;
2814 else
2816 /* Undocumented feature: Create sublevels until
2817 * either the width or the height is 1
2819 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2820 desc2.dwWidth : desc2.dwHeight;
2821 desc2.u2.dwMipMapCount = 0;
2822 while( min )
2824 desc2.u2.dwMipMapCount += 1;
2825 min >>= 1;
2829 else
2831 /* Not-complex mipmap -> Mipmapcount = 1 */
2832 desc2.u2.dwMipMapCount = 1;
2835 /* There's a mipmap count in the created surface in any case */
2836 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2838 /* If no mipmap is given, the texture has only one level */
2840 /* The first surface is a front buffer, the back buffer is created afterwards */
2841 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2843 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2846 /* The root surface in a cube map is positive x */
2847 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2849 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2850 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2853 if ((desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) && (ddraw->cooperative_level & DDSCL_EXCLUSIVE))
2855 struct wined3d_swapchain_desc swapchain_desc;
2857 wined3d_swapchain_get_desc(ddraw->wined3d_swapchain, &swapchain_desc);
2858 swapchain_desc.backbuffer_width = mode.width;
2859 swapchain_desc.backbuffer_height = mode.height;
2860 swapchain_desc.backbuffer_format = mode.format_id;
2862 hr = wined3d_device_reset(ddraw->wined3d_device,
2863 &swapchain_desc, NULL, ddraw_reset_enum_callback);
2864 if (FAILED(hr))
2866 ERR("Failed to reset device.\n");
2867 return hr;
2871 /* Create the first surface */
2872 hr = ddraw_create_surface(ddraw, &desc2, &object, 0, version);
2873 if (FAILED(hr))
2875 WARN("ddraw_create_surface failed, hr %#x.\n", hr);
2876 return hr;
2878 object->is_complex_root = TRUE;
2880 *surface = object;
2882 /* Create Additional surfaces if necessary
2883 * This applies to Primary surfaces which have a back buffer count
2884 * set, but not to mipmap textures. In case of Mipmap textures,
2885 * wineD3D takes care of the creation of additional surfaces
2887 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2889 struct ddraw_surface *last = object;
2890 UINT i;
2892 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2893 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2894 desc2.dwBackBufferCount = 0;
2896 for (i = 0; i < DDSD->dwBackBufferCount; ++i)
2898 struct ddraw_surface *object2 = NULL;
2900 if (FAILED(hr = ddraw_create_surface(ddraw, &desc2, &object2, 0, version)))
2902 if (version == 7)
2903 IDirectDrawSurface7_Release(&object->IDirectDrawSurface7_iface);
2904 else if (version == 4)
2905 IDirectDrawSurface4_Release(&object->IDirectDrawSurface4_iface);
2906 else
2907 IDirectDrawSurface_Release(&object->IDirectDrawSurface_iface);
2909 return hr;
2912 /* Add the new surface to the complex attachment array. */
2913 last->complex_array[0] = object2;
2914 last = object2;
2916 /* Remove the (possible) back buffer cap from the new surface
2917 * description, because only one surface in the flipping chain is a
2918 * back buffer, one is a front buffer, the others are just primary
2919 * surfaces. */
2920 desc2.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2924 if (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2925 ddraw->primary = object;
2927 /* Create a WineD3DTexture if a texture was requested */
2928 if (desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
2929 ddraw_surface_create_texture(object);
2931 return hr;
2934 static HRESULT WINAPI ddraw7_CreateSurface(IDirectDraw7 *iface, DDSURFACEDESC2 *surface_desc,
2935 IDirectDrawSurface7 **surface, IUnknown *outer_unknown)
2937 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2938 struct ddraw_surface *impl;
2939 HRESULT hr;
2941 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2942 iface, surface_desc, surface, outer_unknown);
2944 wined3d_mutex_lock();
2946 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2948 WARN("Cooperative level not set.\n");
2949 wined3d_mutex_unlock();
2950 return DDERR_NOCOOPERATIVELEVELSET;
2953 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
2955 WARN("Application supplied invalid surface descriptor\n");
2956 wined3d_mutex_unlock();
2957 return DDERR_INVALIDPARAMS;
2960 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
2962 if (TRACE_ON(ddraw))
2964 TRACE(" (%p) Requesting surface desc :\n", iface);
2965 DDRAW_dump_surface_desc(surface_desc);
2968 WARN("Application tried to create an explicit front or back buffer\n");
2969 wined3d_mutex_unlock();
2970 return DDERR_INVALIDCAPS;
2973 hr = CreateSurface(ddraw, surface_desc, &impl, outer_unknown, 7);
2974 wined3d_mutex_unlock();
2975 if (FAILED(hr))
2977 *surface = NULL;
2978 return hr;
2981 *surface = &impl->IDirectDrawSurface7_iface;
2982 IDirectDraw7_AddRef(iface);
2983 impl->ifaceToRelease = (IUnknown *)iface;
2985 return hr;
2988 static HRESULT WINAPI ddraw4_CreateSurface(IDirectDraw4 *iface,
2989 DDSURFACEDESC2 *surface_desc, IDirectDrawSurface4 **surface, IUnknown *outer_unknown)
2991 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2992 struct ddraw_surface *impl;
2993 HRESULT hr;
2995 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2996 iface, surface_desc, surface, outer_unknown);
2998 wined3d_mutex_lock();
3000 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
3002 WARN("Cooperative level not set.\n");
3003 wined3d_mutex_unlock();
3004 return DDERR_NOCOOPERATIVELEVELSET;
3007 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
3009 WARN("Application supplied invalid surface descriptor\n");
3010 wined3d_mutex_unlock();
3011 return DDERR_INVALIDPARAMS;
3014 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3016 if (TRACE_ON(ddraw))
3018 TRACE(" (%p) Requesting surface desc :\n", iface);
3019 DDRAW_dump_surface_desc(surface_desc);
3022 WARN("Application tried to create an explicit front or back buffer\n");
3023 wined3d_mutex_unlock();
3024 return DDERR_INVALIDCAPS;
3027 hr = CreateSurface(ddraw, surface_desc, &impl, outer_unknown, 4);
3028 wined3d_mutex_unlock();
3029 if (FAILED(hr))
3031 *surface = NULL;
3032 return hr;
3035 *surface = &impl->IDirectDrawSurface4_iface;
3036 IDirectDraw4_AddRef(iface);
3037 impl->ifaceToRelease = (IUnknown *)iface;
3039 return hr;
3042 static HRESULT WINAPI ddraw2_CreateSurface(IDirectDraw2 *iface,
3043 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3045 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3046 struct ddraw_surface *impl;
3047 HRESULT hr;
3048 DDSURFACEDESC2 surface_desc2;
3050 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3051 iface, surface_desc, surface, outer_unknown);
3053 wined3d_mutex_lock();
3055 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
3057 WARN("Cooperative level not set.\n");
3058 wined3d_mutex_unlock();
3059 return DDERR_NOCOOPERATIVELEVELSET;
3062 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3064 WARN("Application supplied invalid surface descriptor\n");
3065 wined3d_mutex_unlock();
3066 return DDERR_INVALIDPARAMS;
3069 DDSD_to_DDSD2(surface_desc, &surface_desc2);
3070 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3072 if (TRACE_ON(ddraw))
3074 TRACE(" (%p) Requesting surface desc :\n", iface);
3075 DDRAW_dump_surface_desc((LPDDSURFACEDESC2)surface_desc);
3078 WARN("Application tried to create an explicit front or back buffer\n");
3079 wined3d_mutex_unlock();
3080 return DDERR_INVALIDCAPS;
3083 hr = CreateSurface(ddraw, &surface_desc2, &impl, outer_unknown, 2);
3084 wined3d_mutex_unlock();
3085 if (FAILED(hr))
3087 *surface = NULL;
3088 return hr;
3091 *surface = &impl->IDirectDrawSurface_iface;
3092 impl->ifaceToRelease = NULL;
3094 return hr;
3097 static HRESULT WINAPI ddraw1_CreateSurface(IDirectDraw *iface,
3098 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3100 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3101 struct ddraw_surface *impl;
3102 HRESULT hr;
3103 DDSURFACEDESC2 surface_desc2;
3105 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3106 iface, surface_desc, surface, outer_unknown);
3108 wined3d_mutex_lock();
3110 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
3112 WARN("Cooperative level not set.\n");
3113 wined3d_mutex_unlock();
3114 return DDERR_NOCOOPERATIVELEVELSET;
3117 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3119 WARN("Application supplied invalid surface descriptor\n");
3120 wined3d_mutex_unlock();
3121 return DDERR_INVALIDPARAMS;
3124 /* Remove front buffer flag, this causes failure in v7, and its added to normal
3125 * primaries anyway. */
3126 surface_desc->ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER;
3127 DDSD_to_DDSD2(surface_desc, &surface_desc2);
3128 hr = CreateSurface(ddraw, &surface_desc2, &impl, outer_unknown, 1);
3129 wined3d_mutex_unlock();
3130 if (FAILED(hr))
3132 *surface = NULL;
3133 return hr;
3136 *surface = &impl->IDirectDrawSurface_iface;
3137 impl->ifaceToRelease = NULL;
3139 return hr;
3142 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
3143 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
3145 static BOOL
3146 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
3147 const DDPIXELFORMAT *provided)
3149 /* Some flags must be present in both or neither for a match. */
3150 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
3151 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
3152 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
3154 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3155 return FALSE;
3157 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
3158 return FALSE;
3160 if (requested->dwFlags & DDPF_FOURCC)
3161 if (requested->dwFourCC != provided->dwFourCC)
3162 return FALSE;
3164 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
3165 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3166 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
3167 return FALSE;
3169 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3170 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3171 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
3172 return FALSE;
3174 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
3175 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
3176 return FALSE;
3178 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
3179 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3180 |DDPF_BUMPDUDV))
3181 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
3182 return FALSE;
3184 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
3185 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
3186 return FALSE;
3188 return TRUE;
3191 static BOOL ddraw_match_surface_desc(const DDSURFACEDESC2 *requested, const DDSURFACEDESC2 *provided)
3193 struct compare_info
3195 DWORD flag;
3196 ptrdiff_t offset;
3197 size_t size;
3200 #define CMP(FLAG, FIELD) \
3201 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
3202 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
3204 static const struct compare_info compare[] =
3206 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
3207 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
3208 CMP(CAPS, ddsCaps),
3209 CMP(CKDESTBLT, ddckCKDestBlt),
3210 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
3211 CMP(CKSRCBLT, ddckCKSrcBlt),
3212 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
3213 CMP(HEIGHT, dwHeight),
3214 CMP(LINEARSIZE, u1 /* dwLinearSize */),
3215 CMP(LPSURFACE, lpSurface),
3216 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
3217 CMP(PITCH, u1 /* lPitch */),
3218 /* PIXELFORMAT: manual */
3219 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
3220 CMP(TEXTURESTAGE, dwTextureStage),
3221 CMP(WIDTH, dwWidth),
3222 /* ZBUFFERBITDEPTH: "obsolete" */
3225 #undef CMP
3227 unsigned int i;
3229 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3230 return FALSE;
3232 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
3234 if (requested->dwFlags & compare[i].flag
3235 && memcmp((const char *)provided + compare[i].offset,
3236 (const char *)requested + compare[i].offset,
3237 compare[i].size) != 0)
3238 return FALSE;
3241 if (requested->dwFlags & DDSD_PIXELFORMAT)
3243 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
3244 &provided->u4.ddpfPixelFormat))
3245 return FALSE;
3248 return TRUE;
3251 #undef DDENUMSURFACES_SEARCHTYPE
3252 #undef DDENUMSURFACES_MATCHTYPE
3254 struct surfacescallback2_context
3256 LPDDENUMSURFACESCALLBACK2 func;
3257 void *context;
3260 struct surfacescallback_context
3262 LPDDENUMSURFACESCALLBACK func;
3263 void *context;
3266 static HRESULT CALLBACK EnumSurfacesCallback2Thunk(IDirectDrawSurface7 *surface,
3267 DDSURFACEDESC2 *surface_desc, void *context)
3269 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3270 struct surfacescallback2_context *cbcontext = context;
3272 IDirectDrawSurface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
3273 IDirectDrawSurface7_Release(surface);
3275 return cbcontext->func(&surface_impl->IDirectDrawSurface4_iface,
3276 surface_desc, cbcontext->context);
3279 static HRESULT CALLBACK EnumSurfacesCallbackThunk(IDirectDrawSurface7 *surface,
3280 DDSURFACEDESC2 *surface_desc, void *context)
3282 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3283 struct surfacescallback_context *cbcontext = context;
3285 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
3286 IDirectDrawSurface7_Release(surface);
3288 return cbcontext->func(&surface_impl->IDirectDrawSurface_iface,
3289 (DDSURFACEDESC *)surface_desc, cbcontext->context);
3292 /*****************************************************************************
3293 * IDirectDraw7::EnumSurfaces
3295 * Loops through all surfaces attached to this device and calls the
3296 * application callback. This can't be relayed to WineD3DDevice,
3297 * because some WineD3DSurfaces' parents are IParent objects
3299 * Params:
3300 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
3301 * DDSD: Description to filter for
3302 * Context: Application-provided pointer, it's passed unmodified to the
3303 * Callback function
3304 * Callback: Address to call for each surface
3306 * Returns:
3307 * DDERR_INVALIDPARAMS if the callback is NULL
3308 * DD_OK on success
3310 *****************************************************************************/
3311 static HRESULT WINAPI ddraw7_EnumSurfaces(IDirectDraw7 *iface, DWORD Flags,
3312 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMSURFACESCALLBACK7 Callback)
3314 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
3315 struct ddraw_surface *surf;
3316 BOOL all, nomatch;
3317 DDSURFACEDESC2 desc;
3318 struct list *entry, *entry2;
3320 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3321 iface, Flags, DDSD, Context, Callback);
3323 all = Flags & DDENUMSURFACES_ALL;
3324 nomatch = Flags & DDENUMSURFACES_NOMATCH;
3326 if (!Callback)
3327 return DDERR_INVALIDPARAMS;
3329 wined3d_mutex_lock();
3331 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
3332 LIST_FOR_EACH_SAFE(entry, entry2, &ddraw->surface_list)
3334 surf = LIST_ENTRY(entry, struct ddraw_surface, surface_list_entry);
3336 if (!surf->iface_count)
3338 WARN("Not enumerating surface %p because it doesn't have any references.\n", surf);
3339 continue;
3342 if (all || (nomatch != ddraw_match_surface_desc(DDSD, &surf->surface_desc)))
3344 TRACE("Enumerating surface %p.\n", surf);
3345 desc = surf->surface_desc;
3346 IDirectDrawSurface7_AddRef(&surf->IDirectDrawSurface7_iface);
3347 if (Callback(&surf->IDirectDrawSurface7_iface, &desc, Context) != DDENUMRET_OK)
3349 wined3d_mutex_unlock();
3350 return DD_OK;
3355 wined3d_mutex_unlock();
3357 return DD_OK;
3360 static HRESULT WINAPI ddraw4_EnumSurfaces(IDirectDraw4 *iface, DWORD flags,
3361 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMSURFACESCALLBACK2 callback)
3363 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3364 struct surfacescallback2_context cbcontext;
3366 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3367 iface, flags, surface_desc, context, callback);
3369 cbcontext.func = callback;
3370 cbcontext.context = context;
3372 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags, surface_desc,
3373 &cbcontext, EnumSurfacesCallback2Thunk);
3376 static HRESULT WINAPI ddraw2_EnumSurfaces(IDirectDraw2 *iface, DWORD flags,
3377 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3379 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3380 struct surfacescallback_context cbcontext;
3381 DDSURFACEDESC2 surface_desc2;
3383 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3384 iface, flags, surface_desc, context, callback);
3386 cbcontext.func = callback;
3387 cbcontext.context = context;
3389 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3390 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags,
3391 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3394 static HRESULT WINAPI ddraw1_EnumSurfaces(IDirectDraw *iface, DWORD flags,
3395 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3397 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3398 struct surfacescallback_context cbcontext;
3399 DDSURFACEDESC2 surface_desc2;
3401 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3402 iface, flags, surface_desc, context, callback);
3404 cbcontext.func = callback;
3405 cbcontext.context = context;
3407 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3408 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags,
3409 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3412 /*****************************************************************************
3413 * DirectDrawCreateClipper (DDRAW.@)
3415 * Creates a new IDirectDrawClipper object.
3417 * Params:
3418 * Clipper: Address to write the interface pointer to
3419 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3420 * NULL
3422 * Returns:
3423 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3424 * E_OUTOFMEMORY if allocating the object failed
3426 *****************************************************************************/
3427 HRESULT WINAPI DirectDrawCreateClipper(DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3429 struct ddraw_clipper *object;
3430 HRESULT hr;
3432 TRACE("flags %#x, clipper %p, outer_unknown %p.\n",
3433 flags, clipper, outer_unknown);
3435 if (outer_unknown)
3436 return CLASS_E_NOAGGREGATION;
3438 wined3d_mutex_lock();
3440 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3441 if (!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 HeapFree(GetProcessHeap(), 0, 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 object = HeapAlloc(GetProcessHeap(), 0, sizeof(*object));
3552 if (!object)
3554 ERR("Out of memory when allocating memory for a palette implementation\n");
3555 wined3d_mutex_unlock();
3556 return E_OUTOFMEMORY;
3559 hr = ddraw_palette_init(object, ddraw, Flags, ColorTable);
3560 if (FAILED(hr))
3562 WARN("Failed to initialize palette, hr %#x.\n", hr);
3563 HeapFree(GetProcessHeap(), 0, object);
3564 wined3d_mutex_unlock();
3565 return hr;
3568 TRACE("Created palette %p.\n", object);
3569 *Palette = &object->IDirectDrawPalette_iface;
3570 wined3d_mutex_unlock();
3572 return DD_OK;
3575 static HRESULT WINAPI ddraw4_CreatePalette(IDirectDraw4 *iface, DWORD flags, PALETTEENTRY *entries,
3576 IDirectDrawPalette **palette, IUnknown *outer_unknown)
3578 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3579 HRESULT hr;
3581 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3582 iface, flags, entries, palette, outer_unknown);
3584 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3585 if (SUCCEEDED(hr) && *palette)
3587 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3588 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3589 IDirectDraw4_AddRef(iface);
3590 impl->ifaceToRelease = (IUnknown *)iface;
3592 return hr;
3595 static HRESULT WINAPI ddraw2_CreatePalette(IDirectDraw2 *iface, DWORD flags,
3596 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3598 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3599 HRESULT hr;
3601 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3602 iface, flags, entries, palette, outer_unknown);
3604 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3605 if (SUCCEEDED(hr) && *palette)
3607 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3608 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3609 impl->ifaceToRelease = NULL;
3612 return hr;
3615 static HRESULT WINAPI ddraw1_CreatePalette(IDirectDraw *iface, DWORD flags,
3616 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3618 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3619 HRESULT hr;
3621 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3622 iface, flags, entries, palette, outer_unknown);
3624 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3625 if (SUCCEEDED(hr) && *palette)
3627 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3628 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3629 impl->ifaceToRelease = NULL;
3632 return hr;
3635 /*****************************************************************************
3636 * IDirectDraw7::DuplicateSurface
3638 * Duplicates a surface. The surface memory points to the same memory as
3639 * the original surface, and it's released when the last surface referencing
3640 * it is released. I guess that's beyond Wine's surface management right now
3641 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3642 * test application to implement this)
3644 * Params:
3645 * Src: Address of the source surface
3646 * Dest: Address to write the new surface pointer to
3648 * Returns:
3649 * See IDirectDraw7::CreateSurface
3651 *****************************************************************************/
3652 static HRESULT WINAPI ddraw7_DuplicateSurface(IDirectDraw7 *iface,
3653 IDirectDrawSurface7 *Src, IDirectDrawSurface7 **Dest)
3655 struct ddraw_surface *src_surface = unsafe_impl_from_IDirectDrawSurface7(Src);
3657 FIXME("iface %p, src %p, dst %p partial stub!\n", iface, Src, Dest);
3659 /* For now, simply create a new, independent surface */
3660 return IDirectDraw7_CreateSurface(iface, &src_surface->surface_desc, Dest, NULL);
3663 static HRESULT WINAPI ddraw4_DuplicateSurface(IDirectDraw4 *iface, IDirectDrawSurface4 *src,
3664 IDirectDrawSurface4 **dst)
3666 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src);
3667 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3668 struct ddraw_surface *dst_impl;
3669 IDirectDrawSurface7 *dst7;
3670 HRESULT hr;
3672 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3674 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3675 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3676 if (FAILED(hr))
3678 *dst = NULL;
3679 return hr;
3681 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3682 *dst = &dst_impl->IDirectDrawSurface4_iface;
3683 IDirectDrawSurface4_AddRef(*dst);
3684 IDirectDrawSurface7_Release(dst7);
3686 return hr;
3689 static HRESULT WINAPI ddraw2_DuplicateSurface(IDirectDraw2 *iface,
3690 IDirectDrawSurface *src, IDirectDrawSurface **dst)
3692 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3693 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3694 struct ddraw_surface *dst_impl;
3695 IDirectDrawSurface7 *dst7;
3696 HRESULT hr;
3698 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3700 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3701 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3702 if (FAILED(hr))
3703 return hr;
3704 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3705 *dst = &dst_impl->IDirectDrawSurface_iface;
3706 IDirectDrawSurface_AddRef(*dst);
3707 IDirectDrawSurface7_Release(dst7);
3709 return hr;
3712 static HRESULT WINAPI ddraw1_DuplicateSurface(IDirectDraw *iface, IDirectDrawSurface *src,
3713 IDirectDrawSurface **dst)
3715 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3716 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3717 struct ddraw_surface *dst_impl;
3718 IDirectDrawSurface7 *dst7;
3719 HRESULT hr;
3721 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3723 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3724 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3725 if (FAILED(hr))
3726 return hr;
3727 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3728 *dst = &dst_impl->IDirectDrawSurface_iface;
3729 IDirectDrawSurface_AddRef(*dst);
3730 IDirectDrawSurface7_Release(dst7);
3732 return hr;
3735 /*****************************************************************************
3736 * IDirect3D7::EnumDevices
3738 * The EnumDevices method for IDirect3D7. It enumerates all supported
3739 * D3D7 devices. Currently the T&L, HAL and RGB devices are enumerated.
3741 * Params:
3742 * callback: Function to call for each enumerated device
3743 * context: Pointer to pass back to the app
3745 * Returns:
3746 * D3D_OK, or the return value of the GetCaps call
3748 *****************************************************************************/
3749 static HRESULT WINAPI d3d7_EnumDevices(IDirect3D7 *iface, LPD3DENUMDEVICESCALLBACK7 callback, void *context)
3751 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
3752 D3DDEVICEDESC7 device_desc7;
3753 D3DDEVICEDESC device_desc1;
3754 HRESULT hr;
3755 size_t i;
3757 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3759 if (!callback)
3760 return DDERR_INVALIDPARAMS;
3762 wined3d_mutex_lock();
3764 hr = IDirect3DImpl_GetCaps(ddraw->wined3d, &device_desc1, &device_desc7);
3765 if (hr != D3D_OK)
3767 wined3d_mutex_unlock();
3768 return hr;
3771 for (i = 0; i < sizeof(device_list7)/sizeof(device_list7[0]); i++)
3773 HRESULT ret;
3775 device_desc7.deviceGUID = *device_list7[i].device_guid;
3776 ret = callback(device_list7[i].interface_name, device_list7[i].device_name, &device_desc7, context);
3777 if (ret != DDENUMRET_OK)
3779 TRACE("Application cancelled the enumeration.\n");
3780 wined3d_mutex_unlock();
3781 return D3D_OK;
3785 TRACE("End of enumeration.\n");
3787 wined3d_mutex_unlock();
3789 return D3D_OK;
3792 /*****************************************************************************
3793 * IDirect3D3::EnumDevices
3795 * Enumerates all supported Direct3DDevice interfaces. This is the
3796 * implementation for Direct3D 1 to Direc3D 3, Version 7 has its own.
3798 * Version 1, 2 and 3
3800 * Params:
3801 * callback: Application-provided routine to call for each enumerated device
3802 * Context: Pointer to pass to the callback
3804 * Returns:
3805 * D3D_OK on success,
3806 * The result of IDirect3DImpl_GetCaps if it failed
3808 *****************************************************************************/
3809 static HRESULT WINAPI d3d3_EnumDevices(IDirect3D3 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3811 static CHAR wined3d_description[] = "Wine D3DDevice using WineD3D and OpenGL";
3813 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3814 D3DDEVICEDESC device_desc1, hal_desc, hel_desc;
3815 D3DDEVICEDESC7 device_desc7;
3816 HRESULT hr;
3818 /* Some games (Motoracer 2 demo) have the bad idea to modify the device
3819 * name string. Let's put the string in a sufficiently sized array in
3820 * writable memory. */
3821 char device_name[50];
3822 strcpy(device_name,"Direct3D HEL");
3824 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3826 if (!callback)
3827 return DDERR_INVALIDPARAMS;
3829 wined3d_mutex_lock();
3831 hr = IDirect3DImpl_GetCaps(ddraw->wined3d, &device_desc1, &device_desc7);
3832 if (hr != D3D_OK)
3834 wined3d_mutex_unlock();
3835 return hr;
3838 /* Do I have to enumerate the reference id? Note from old d3d7:
3839 * "It seems that enumerating the reference IID on Direct3D 1 games
3840 * (AvP / Motoracer2) breaks them". So do not enumerate this iid in V1
3842 * There's a registry key HKLM\Software\Microsoft\Direct3D\Drivers,
3843 * EnumReference which enables / disables enumerating the reference
3844 * rasterizer. It's a DWORD, 0 means disabled, 2 means enabled. The
3845 * enablerefrast.reg and disablerefrast.reg files in the DirectX 7.0 sdk
3846 * demo directory suggest this.
3848 * Some games(GTA 2) seem to use the second enumerated device, so I have
3849 * to enumerate at least 2 devices. So enumerate the reference device to
3850 * have 2 devices.
3852 * Other games (Rollcage) tell emulation and hal device apart by certain
3853 * flags. Rollcage expects D3DPTEXTURECAPS_POW2 to be set (yeah, it is a
3854 * limitation flag), and it refuses all devices that have the perspective
3855 * flag set. This way it refuses the emulation device, and HAL devices
3856 * never have POW2 unset in d3d7 on windows. */
3857 if (ddraw->d3dversion != 1)
3859 static CHAR reference_description[] = "RGB Direct3D emulation";
3861 TRACE("Enumerating WineD3D D3DDevice interface.\n");
3862 hal_desc = device_desc1;
3863 hel_desc = device_desc1;
3864 /* The rgb device has the pow2 flag set in the hel caps, but not in the hal caps. */
3865 hal_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3866 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3867 hal_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3868 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3869 /* RGB, RAMP and MMX devices have a HAL dcmColorModel of 0 */
3870 hal_desc.dcmColorModel = 0;
3872 hr = callback((GUID *)&IID_IDirect3DRGBDevice, reference_description,
3873 device_name, &hal_desc, &hel_desc, context);
3874 if (hr != D3DENUMRET_OK)
3876 TRACE("Application cancelled the enumeration.\n");
3877 wined3d_mutex_unlock();
3878 return D3D_OK;
3882 strcpy(device_name,"Direct3D HAL");
3884 TRACE("Enumerating HAL Direct3D device.\n");
3885 hal_desc = device_desc1;
3886 hel_desc = device_desc1;
3888 /* The hal device does not have the pow2 flag set in hel, but in hal. */
3889 hel_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3890 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3891 hel_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3892 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3893 /* HAL devices have a HEL dcmColorModel of 0 */
3894 hel_desc.dcmColorModel = 0;
3896 hr = callback((GUID *)&IID_IDirect3DHALDevice, wined3d_description,
3897 device_name, &hal_desc, &hel_desc, context);
3898 if (hr != D3DENUMRET_OK)
3900 TRACE("Application cancelled the enumeration.\n");
3901 wined3d_mutex_unlock();
3902 return D3D_OK;
3905 TRACE("End of enumeration.\n");
3907 wined3d_mutex_unlock();
3909 return D3D_OK;
3912 static HRESULT WINAPI d3d2_EnumDevices(IDirect3D2 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3914 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3916 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3918 return d3d3_EnumDevices(&ddraw->IDirect3D3_iface, callback, context);
3921 static HRESULT WINAPI d3d1_EnumDevices(IDirect3D *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3923 struct ddraw *ddraw = impl_from_IDirect3D(iface);
3925 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3927 return d3d3_EnumDevices(&ddraw->IDirect3D3_iface, callback, context);
3930 /*****************************************************************************
3931 * IDirect3D3::CreateLight
3933 * Creates an IDirect3DLight interface. This interface is used in
3934 * Direct3D3 or earlier for lighting. In Direct3D7 it has been replaced
3935 * by the DIRECT3DLIGHT7 structure. Wine's Direct3DLight implementation
3936 * uses the IDirect3DDevice7 interface with D3D7 lights.
3938 * Version 1, 2 and 3
3940 * Params:
3941 * light: Address to store the new interface pointer
3942 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
3943 * Must be NULL
3945 * Returns:
3946 * D3D_OK on success
3947 * DDERR_OUTOFMEMORY if memory allocation failed
3948 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
3950 *****************************************************************************/
3951 static HRESULT WINAPI d3d3_CreateLight(IDirect3D3 *iface, IDirect3DLight **light,
3952 IUnknown *outer_unknown)
3954 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3955 struct d3d_light *object;
3957 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3959 if (outer_unknown) return CLASS_E_NOAGGREGATION;
3961 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3962 if (!object)
3964 ERR("Failed to allocate light memory.\n");
3965 return DDERR_OUTOFMEMORY;
3968 d3d_light_init(object, ddraw);
3970 TRACE("Created light %p.\n", object);
3971 *light = &object->IDirect3DLight_iface;
3973 return D3D_OK;
3976 static HRESULT WINAPI d3d2_CreateLight(IDirect3D2 *iface, IDirect3DLight **light, IUnknown *outer_unknown)
3978 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3980 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3982 return d3d3_CreateLight(&ddraw->IDirect3D3_iface, light, outer_unknown);
3985 static HRESULT WINAPI d3d1_CreateLight(IDirect3D *iface, IDirect3DLight **light, IUnknown *outer_unknown)
3987 struct ddraw *ddraw = impl_from_IDirect3D(iface);
3989 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3991 return d3d3_CreateLight(&ddraw->IDirect3D3_iface, light, outer_unknown);
3994 /*****************************************************************************
3995 * IDirect3D3::CreateMaterial
3997 * Creates an IDirect3DMaterial interface. This interface is used by Direct3D3
3998 * and older versions. The IDirect3DMaterial implementation wraps its
3999 * functionality to IDirect3DDevice7::SetMaterial and friends.
4001 * Version 1, 2 and 3
4003 * Params:
4004 * material: Address to store the new interface's pointer to
4005 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4006 * Must be NULL
4008 * Returns:
4009 * D3D_OK on success
4010 * DDERR_OUTOFMEMORY if memory allocation failed
4011 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4013 *****************************************************************************/
4014 static HRESULT WINAPI d3d3_CreateMaterial(IDirect3D3 *iface, IDirect3DMaterial3 **material,
4015 IUnknown *outer_unknown)
4017 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4018 struct d3d_material *object;
4020 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4022 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4024 object = d3d_material_create(ddraw);
4025 if (!object)
4027 ERR("Failed to allocate material memory.\n");
4028 return DDERR_OUTOFMEMORY;
4031 TRACE("Created material %p.\n", object);
4032 *material = &object->IDirect3DMaterial3_iface;
4034 return D3D_OK;
4037 static HRESULT WINAPI d3d2_CreateMaterial(IDirect3D2 *iface, IDirect3DMaterial2 **material,
4038 IUnknown *outer_unknown)
4040 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4041 struct d3d_material *object;
4043 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4045 object = d3d_material_create(ddraw);
4046 if (!object)
4048 ERR("Failed to allocate material memory.\n");
4049 return DDERR_OUTOFMEMORY;
4052 TRACE("Created material %p.\n", object);
4053 *material = &object->IDirect3DMaterial2_iface;
4055 return D3D_OK;
4058 static HRESULT WINAPI d3d1_CreateMaterial(IDirect3D *iface, IDirect3DMaterial **material,
4059 IUnknown *outer_unknown)
4061 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4062 struct d3d_material *object;
4064 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4066 object = d3d_material_create(ddraw);
4067 if (!object)
4069 ERR("Failed to allocate material memory.\n");
4070 return DDERR_OUTOFMEMORY;
4073 TRACE("Created material %p.\n", object);
4074 *material = &object->IDirect3DMaterial_iface;
4076 return D3D_OK;
4079 /*****************************************************************************
4080 * IDirect3D3::CreateViewport
4082 * Creates an IDirect3DViewport interface. This interface is used
4083 * by Direct3D and earlier versions for Viewport management. In Direct3D7
4084 * it has been replaced by a viewport structure and
4085 * IDirect3DDevice7::*Viewport. Wine's IDirect3DViewport implementation
4086 * uses the IDirect3DDevice7 methods for its functionality
4088 * Params:
4089 * Viewport: Address to store the new interface pointer
4090 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4091 * Must be NULL
4093 * Returns:
4094 * D3D_OK on success
4095 * DDERR_OUTOFMEMORY if memory allocation failed
4096 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4098 *****************************************************************************/
4099 static HRESULT WINAPI d3d3_CreateViewport(IDirect3D3 *iface, IDirect3DViewport3 **viewport,
4100 IUnknown *outer_unknown)
4102 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4103 struct d3d_viewport *object;
4105 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4107 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4109 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4110 if (!object)
4112 ERR("Failed to allocate viewport memory.\n");
4113 return DDERR_OUTOFMEMORY;
4116 d3d_viewport_init(object, ddraw);
4118 TRACE("Created viewport %p.\n", object);
4119 *viewport = &object->IDirect3DViewport3_iface;
4121 return D3D_OK;
4124 static HRESULT WINAPI d3d2_CreateViewport(IDirect3D2 *iface, IDirect3DViewport2 **viewport, IUnknown *outer_unknown)
4126 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4128 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4130 return d3d3_CreateViewport(&ddraw->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4131 outer_unknown);
4134 static HRESULT WINAPI d3d1_CreateViewport(IDirect3D *iface, IDirect3DViewport **viewport, IUnknown *outer_unknown)
4136 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4138 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4140 return d3d3_CreateViewport(&ddraw->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4141 outer_unknown);
4144 /*****************************************************************************
4145 * IDirect3D3::FindDevice
4147 * This method finds a device with the requested properties and returns a
4148 * device description
4150 * Verion 1, 2 and 3
4151 * Params:
4152 * fds: Describes the requested device characteristics
4153 * fdr: Returns the device description
4155 * Returns:
4156 * D3D_OK on success
4157 * DDERR_INVALIDPARAMS if no device was found
4159 *****************************************************************************/
4160 static HRESULT WINAPI d3d3_FindDevice(IDirect3D3 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4162 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4163 D3DDEVICEDESC7 desc7;
4164 D3DDEVICEDESC desc1;
4165 HRESULT hr;
4167 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4169 if (!fds || !fdr) return DDERR_INVALIDPARAMS;
4171 if (fds->dwSize != sizeof(D3DFINDDEVICESEARCH)
4172 || fdr->dwSize != sizeof(D3DFINDDEVICERESULT))
4173 return DDERR_INVALIDPARAMS;
4175 if ((fds->dwFlags & D3DFDS_COLORMODEL)
4176 && fds->dcmColorModel != D3DCOLOR_RGB)
4178 WARN("Trying to request a non-RGB D3D color model. Not supported.\n");
4179 return DDERR_INVALIDPARAMS; /* No real idea what to return here :-) */
4182 if (fds->dwFlags & D3DFDS_GUID)
4184 TRACE("Trying to match guid %s.\n", debugstr_guid(&(fds->guid)));
4185 if (!IsEqualGUID(&IID_D3DDEVICE_WineD3D, &fds->guid)
4186 && !IsEqualGUID(&IID_IDirect3DHALDevice, &fds->guid)
4187 && !IsEqualGUID(&IID_IDirect3DRGBDevice, &fds->guid))
4189 WARN("No match for this GUID.\n");
4190 return DDERR_NOTFOUND;
4194 /* Get the caps */
4195 hr = IDirect3DImpl_GetCaps(ddraw->wined3d, &desc1, &desc7);
4196 if (hr != D3D_OK) return hr;
4198 /* Now return our own GUID */
4199 fdr->guid = IID_D3DDEVICE_WineD3D;
4200 fdr->ddHwDesc = desc1;
4201 fdr->ddSwDesc = desc1;
4203 TRACE("Returning Wine's wined3d device with (undumped) capabilities.\n");
4205 return D3D_OK;
4208 static HRESULT WINAPI d3d2_FindDevice(IDirect3D2 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4210 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4212 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4214 return d3d3_FindDevice(&ddraw->IDirect3D3_iface, fds, fdr);
4217 static HRESULT WINAPI d3d1_FindDevice(IDirect3D *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4219 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4221 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4223 return d3d3_FindDevice(&ddraw->IDirect3D3_iface, fds, fdr);
4226 /*****************************************************************************
4227 * IDirect3D7::CreateDevice
4229 * Creates an IDirect3DDevice7 interface.
4231 * Version 2, 3 and 7. IDirect3DDevice 1 interfaces are interfaces to
4232 * DirectDraw surfaces and are created with
4233 * IDirectDrawSurface::QueryInterface. This method uses CreateDevice to
4234 * create the device object and QueryInterfaces for IDirect3DDevice
4236 * Params:
4237 * refiid: IID of the device to create
4238 * Surface: Initial rendertarget
4239 * Device: Address to return the interface pointer
4241 * Returns:
4242 * D3D_OK on success
4243 * DDERR_OUTOFMEMORY if memory allocation failed
4244 * DDERR_INVALIDPARAMS if a device exists already
4246 *****************************************************************************/
4247 static HRESULT WINAPI d3d7_CreateDevice(IDirect3D7 *iface, REFCLSID riid,
4248 IDirectDrawSurface7 *surface, IDirect3DDevice7 **device)
4250 struct ddraw_surface *target = unsafe_impl_from_IDirectDrawSurface7(surface);
4251 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4252 struct d3d_device *object;
4253 HRESULT hr;
4255 TRACE("iface %p, riid %s, surface %p, device %p.\n", iface, debugstr_guid(riid), surface, device);
4257 wined3d_mutex_lock();
4258 hr = d3d_device_create(ddraw, target, 7, &object, NULL);
4259 if (SUCCEEDED(hr))
4260 *device = &object->IDirect3DDevice7_iface;
4261 else
4263 WARN("Failed to create device, hr %#x.\n", hr);
4264 *device = NULL;
4266 wined3d_mutex_unlock();
4268 return hr;
4271 static HRESULT WINAPI d3d3_CreateDevice(IDirect3D3 *iface, REFCLSID riid,
4272 IDirectDrawSurface4 *surface, IDirect3DDevice3 **device, IUnknown *outer_unknown)
4274 struct ddraw_surface *surface_impl = unsafe_impl_from_IDirectDrawSurface4(surface);
4275 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4276 struct d3d_device *device_impl;
4277 HRESULT hr;
4279 TRACE("iface %p, riid %s, surface %p, device %p, outer_unknown %p.\n",
4280 iface, debugstr_guid(riid), surface, device, outer_unknown);
4282 if (outer_unknown)
4283 return CLASS_E_NOAGGREGATION;
4285 wined3d_mutex_lock();
4286 hr = d3d_device_create(ddraw, surface_impl, 3, &device_impl, NULL);
4287 if (SUCCEEDED(hr))
4288 *device = &device_impl->IDirect3DDevice3_iface;
4289 else
4291 WARN("Failed to create device, hr %#x.\n", hr);
4292 *device = NULL;
4294 wined3d_mutex_unlock();
4296 return hr;
4299 static HRESULT WINAPI d3d2_CreateDevice(IDirect3D2 *iface, REFCLSID riid,
4300 IDirectDrawSurface *surface, IDirect3DDevice2 **device)
4302 struct ddraw_surface *surface_impl = unsafe_impl_from_IDirectDrawSurface(surface);
4303 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4304 struct d3d_device *device_impl;
4305 HRESULT hr;
4307 TRACE("iface %p, riid %s, surface %p, device %p.\n",
4308 iface, debugstr_guid(riid), surface, device);
4310 wined3d_mutex_lock();
4311 hr = d3d_device_create(ddraw, surface_impl, 2, &device_impl, NULL);
4312 if (SUCCEEDED(hr))
4313 *device = &device_impl->IDirect3DDevice2_iface;
4314 else
4316 WARN("Failed to create device, hr %#x.\n", hr);
4317 *device = NULL;
4319 wined3d_mutex_unlock();
4321 return hr;
4324 /*****************************************************************************
4325 * IDirect3D7::CreateVertexBuffer
4327 * Creates a new vertex buffer object and returns a IDirect3DVertexBuffer7
4328 * interface.
4330 * Version 3 and 7
4332 * Params:
4333 * desc: Requested Vertex buffer properties
4334 * vertex_buffer: Address to return the interface pointer at
4335 * flags: Some flags, should be 0
4337 * Returns
4338 * D3D_OK on success
4339 * DDERR_OUTOFMEMORY if memory allocation failed
4340 * The return value of IWineD3DDevice::CreateVertexBuffer if this call fails
4341 * DDERR_INVALIDPARAMS if desc or vertex_buffer are NULL
4343 *****************************************************************************/
4344 static HRESULT WINAPI d3d7_CreateVertexBuffer(IDirect3D7 *iface, D3DVERTEXBUFFERDESC *desc,
4345 IDirect3DVertexBuffer7 **vertex_buffer, DWORD flags)
4347 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4348 struct d3d_vertex_buffer *object;
4349 HRESULT hr;
4351 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x.\n",
4352 iface, desc, vertex_buffer, flags);
4354 if (!vertex_buffer || !desc) return DDERR_INVALIDPARAMS;
4356 hr = d3d_vertex_buffer_create(&object, ddraw, desc);
4357 if (hr == D3D_OK)
4359 TRACE("Created vertex buffer %p.\n", object);
4360 *vertex_buffer = &object->IDirect3DVertexBuffer7_iface;
4362 else
4363 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4365 return hr;
4368 static HRESULT WINAPI d3d3_CreateVertexBuffer(IDirect3D3 *iface, D3DVERTEXBUFFERDESC *desc,
4369 IDirect3DVertexBuffer **vertex_buffer, DWORD flags, IUnknown *outer_unknown)
4371 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4372 struct d3d_vertex_buffer *object;
4373 HRESULT hr;
4375 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x, outer_unknown %p.\n",
4376 iface, desc, vertex_buffer, flags, outer_unknown);
4378 if (outer_unknown)
4379 return CLASS_E_NOAGGREGATION;
4380 if (!vertex_buffer || !desc)
4381 return DDERR_INVALIDPARAMS;
4383 hr = d3d_vertex_buffer_create(&object, ddraw, desc);
4384 if (hr == D3D_OK)
4386 TRACE("Created vertex buffer %p.\n", object);
4387 *vertex_buffer = &object->IDirect3DVertexBuffer_iface;
4389 else
4390 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4392 return hr;
4395 /*****************************************************************************
4396 * IDirect3D7::EnumZBufferFormats
4398 * Enumerates all supported Z buffer pixel formats
4400 * Version 3 and 7
4402 * Params:
4403 * device_iid:
4404 * callback: callback to call for each pixel format
4405 * context: Pointer to pass back to the callback
4407 * Returns:
4408 * D3D_OK on success
4409 * DDERR_INVALIDPARAMS if callback is NULL
4410 * For details, see IWineD3DDevice::EnumZBufferFormats
4412 *****************************************************************************/
4413 static HRESULT WINAPI d3d7_EnumZBufferFormats(IDirect3D7 *iface, REFCLSID device_iid,
4414 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4416 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4417 struct wined3d_display_mode mode;
4418 enum wined3d_device_type type;
4419 unsigned int i;
4420 HRESULT hr;
4422 /* Order matters. Specifically, BattleZone II (full version) expects the
4423 * 16-bit depth formats to be listed before the 24 and 32 ones. */
4424 static const enum wined3d_format_id formats[] =
4426 WINED3DFMT_S1_UINT_D15_UNORM,
4427 WINED3DFMT_D16_UNORM,
4428 WINED3DFMT_X8D24_UNORM,
4429 WINED3DFMT_S4X4_UINT_D24_UNORM,
4430 WINED3DFMT_D24_UNORM_S8_UINT,
4431 WINED3DFMT_D32_UNORM,
4434 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4435 iface, debugstr_guid(device_iid), callback, context);
4437 if (!callback) return DDERR_INVALIDPARAMS;
4439 if (IsEqualGUID(device_iid, &IID_IDirect3DHALDevice)
4440 || IsEqualGUID(device_iid, &IID_IDirect3DTnLHalDevice)
4441 || IsEqualGUID(device_iid, &IID_D3DDEVICE_WineD3D))
4443 TRACE("Asked for HAL device.\n");
4444 type = WINED3D_DEVICE_TYPE_HAL;
4446 else if (IsEqualGUID(device_iid, &IID_IDirect3DRGBDevice)
4447 || IsEqualGUID(device_iid, &IID_IDirect3DMMXDevice))
4449 TRACE("Asked for SW device.\n");
4450 type = WINED3D_DEVICE_TYPE_SW;
4452 else if (IsEqualGUID(device_iid, &IID_IDirect3DRefDevice))
4454 TRACE("Asked for REF device.\n");
4455 type = WINED3D_DEVICE_TYPE_REF;
4457 else if (IsEqualGUID(device_iid, &IID_IDirect3DNullDevice))
4459 TRACE("Asked for NULLREF device.\n");
4460 type = WINED3D_DEVICE_TYPE_NULLREF;
4462 else
4464 FIXME("Unexpected device GUID %s.\n", debugstr_guid(device_iid));
4465 type = WINED3D_DEVICE_TYPE_HAL;
4468 wined3d_mutex_lock();
4469 /* We need an adapter format from somewhere to please wined3d and WGL.
4470 * Use the current display mode. So far all cards offer the same depth
4471 * stencil format for all modes, but if some do not and applications do
4472 * not like that we'll have to find some workaround, like iterating over
4473 * all imaginable formats and collecting all the depth stencil formats we
4474 * can get. */
4475 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
4477 ERR("Failed to get display mode, hr %#x.\n", hr);
4478 wined3d_mutex_unlock();
4479 return hr;
4482 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); ++i)
4484 hr = wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, type, mode.format_id,
4485 WINED3DUSAGE_DEPTHSTENCIL, WINED3D_RTYPE_SURFACE, formats[i], WINED3D_SURFACE_TYPE_OPENGL);
4486 if (SUCCEEDED(hr))
4488 DDPIXELFORMAT pformat;
4490 memset(&pformat, 0, sizeof(pformat));
4491 pformat.dwSize = sizeof(pformat);
4492 PixelFormat_WineD3DtoDD(&pformat, formats[i]);
4494 TRACE("Enumerating wined3d format %#x.\n", formats[i]);
4495 hr = callback(&pformat, context);
4496 if (hr != DDENUMRET_OK)
4498 TRACE("Format enumeration cancelled by application.\n");
4499 wined3d_mutex_unlock();
4500 return D3D_OK;
4505 /* Historically some windows drivers used dwZBufferBitDepth=24 for WINED3DFMT_X8D24_UNORM,
4506 * while others used dwZBufferBitDepth=32. In either case the pitch matches a 32 bits per
4507 * pixel format, so we use dwZBufferBitDepth=32. Some games expect 24. Windows Vista and
4508 * newer enumerate both versions, so we do the same(bug 22434) */
4509 hr = wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, type, mode.format_id,
4510 WINED3DUSAGE_DEPTHSTENCIL, WINED3D_RTYPE_SURFACE, WINED3DFMT_X8D24_UNORM, WINED3D_SURFACE_TYPE_OPENGL);
4511 if (SUCCEEDED(hr))
4513 DDPIXELFORMAT x8d24 =
4515 sizeof(x8d24), DDPF_ZBUFFER, 0,
4516 {24}, {0x00000000}, {0x00ffffff}, {0x00000000}
4518 TRACE("Enumerating WINED3DFMT_X8D24_UNORM, dwZBufferBitDepth=24 version\n");
4519 callback(&x8d24, context);
4522 TRACE("End of enumeration.\n");
4524 wined3d_mutex_unlock();
4526 return D3D_OK;
4529 static HRESULT WINAPI d3d3_EnumZBufferFormats(IDirect3D3 *iface, REFCLSID device_iid,
4530 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4532 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4534 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4535 iface, debugstr_guid(device_iid), callback, context);
4537 return d3d7_EnumZBufferFormats(&ddraw->IDirect3D7_iface, device_iid, callback, context);
4540 /*****************************************************************************
4541 * IDirect3D7::EvictManagedTextures
4543 * Removes all managed textures (=surfaces with DDSCAPS2_TEXTUREMANAGE or
4544 * DDSCAPS2_D3DTEXTUREMANAGE caps) to be removed from video memory.
4546 * Version 3 and 7
4548 * Returns:
4549 * D3D_OK, because it's a stub
4551 *****************************************************************************/
4552 static HRESULT WINAPI d3d7_EvictManagedTextures(IDirect3D7 *iface)
4554 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4556 TRACE("iface %p!\n", iface);
4558 wined3d_mutex_lock();
4559 if (ddraw->d3d_initialized)
4560 wined3d_device_evict_managed_resources(ddraw->wined3d_device);
4561 wined3d_mutex_unlock();
4563 return D3D_OK;
4566 static HRESULT WINAPI d3d3_EvictManagedTextures(IDirect3D3 *iface)
4568 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4570 TRACE("iface %p.\n", iface);
4572 return d3d7_EvictManagedTextures(&ddraw->IDirect3D7_iface);
4575 /*****************************************************************************
4576 * IDirect3DImpl_GetCaps
4578 * This function retrieves the device caps from wined3d
4579 * and converts it into a D3D7 and D3D - D3D3 structure
4580 * This is a helper function called from various places in ddraw
4582 * Params:
4583 * wined3d: The interface to get the caps from
4584 * desc1: Old D3D <3 structure to fill (needed)
4585 * desc7: D3D7 device desc structure to fill (needed)
4587 * Returns
4588 * D3D_OK on success, or the return value of IWineD3D::GetCaps
4590 *****************************************************************************/
4591 HRESULT IDirect3DImpl_GetCaps(const struct wined3d *wined3d, D3DDEVICEDESC *desc1, D3DDEVICEDESC7 *desc7)
4593 WINED3DCAPS wined3d_caps;
4594 HRESULT hr;
4596 TRACE("wined3d %p, desc1 %p, desc7 %p.\n", wined3d, desc1, desc7);
4598 memset(&wined3d_caps, 0, sizeof(wined3d_caps));
4600 wined3d_mutex_lock();
4601 hr = wined3d_get_device_caps(wined3d, 0, WINED3D_DEVICE_TYPE_HAL, &wined3d_caps);
4602 wined3d_mutex_unlock();
4603 if (FAILED(hr))
4605 WARN("Failed to get device caps, hr %#x.\n", hr);
4606 return hr;
4609 /* Copy the results into the d3d7 and d3d3 structures */
4610 desc7->dwDevCaps = wined3d_caps.DevCaps;
4611 desc7->dpcLineCaps.dwMiscCaps = wined3d_caps.PrimitiveMiscCaps;
4612 desc7->dpcLineCaps.dwRasterCaps = wined3d_caps.RasterCaps;
4613 desc7->dpcLineCaps.dwZCmpCaps = wined3d_caps.ZCmpCaps;
4614 desc7->dpcLineCaps.dwSrcBlendCaps = wined3d_caps.SrcBlendCaps;
4615 desc7->dpcLineCaps.dwDestBlendCaps = wined3d_caps.DestBlendCaps;
4616 desc7->dpcLineCaps.dwAlphaCmpCaps = wined3d_caps.AlphaCmpCaps;
4617 desc7->dpcLineCaps.dwShadeCaps = wined3d_caps.ShadeCaps;
4618 desc7->dpcLineCaps.dwTextureCaps = wined3d_caps.TextureCaps;
4619 desc7->dpcLineCaps.dwTextureFilterCaps = wined3d_caps.TextureFilterCaps;
4620 desc7->dpcLineCaps.dwTextureAddressCaps = wined3d_caps.TextureAddressCaps;
4622 desc7->dwMaxTextureWidth = wined3d_caps.MaxTextureWidth;
4623 desc7->dwMaxTextureHeight = wined3d_caps.MaxTextureHeight;
4625 desc7->dwMaxTextureRepeat = wined3d_caps.MaxTextureRepeat;
4626 desc7->dwMaxTextureAspectRatio = wined3d_caps.MaxTextureAspectRatio;
4627 desc7->dwMaxAnisotropy = wined3d_caps.MaxAnisotropy;
4628 desc7->dvMaxVertexW = wined3d_caps.MaxVertexW;
4630 desc7->dvGuardBandLeft = wined3d_caps.GuardBandLeft;
4631 desc7->dvGuardBandTop = wined3d_caps.GuardBandTop;
4632 desc7->dvGuardBandRight = wined3d_caps.GuardBandRight;
4633 desc7->dvGuardBandBottom = wined3d_caps.GuardBandBottom;
4635 desc7->dvExtentsAdjust = wined3d_caps.ExtentsAdjust;
4636 desc7->dwStencilCaps = wined3d_caps.StencilCaps;
4638 desc7->dwFVFCaps = wined3d_caps.FVFCaps;
4639 desc7->dwTextureOpCaps = wined3d_caps.TextureOpCaps;
4641 desc7->dwVertexProcessingCaps = wined3d_caps.VertexProcessingCaps;
4642 desc7->dwMaxActiveLights = wined3d_caps.MaxActiveLights;
4644 /* Remove all non-d3d7 caps */
4645 desc7->dwDevCaps &= (
4646 D3DDEVCAPS_FLOATTLVERTEX | D3DDEVCAPS_SORTINCREASINGZ | D3DDEVCAPS_SORTDECREASINGZ |
4647 D3DDEVCAPS_SORTEXACT | D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY |
4648 D3DDEVCAPS_TLVERTEXSYSTEMMEMORY | D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY |
4649 D3DDEVCAPS_TEXTUREVIDEOMEMORY | D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP |
4650 D3DDEVCAPS_TEXTURENONLOCALVIDMEM | D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
4651 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT | D3DDEVCAPS_CANBLTSYSTONONLOCAL |
4652 D3DDEVCAPS_HWRASTERIZATION);
4654 desc7->dwStencilCaps &= (
4655 D3DSTENCILCAPS_KEEP | D3DSTENCILCAPS_ZERO | D3DSTENCILCAPS_REPLACE |
4656 D3DSTENCILCAPS_INCRSAT | D3DSTENCILCAPS_DECRSAT | D3DSTENCILCAPS_INVERT |
4657 D3DSTENCILCAPS_INCR | D3DSTENCILCAPS_DECR);
4659 /* FVF caps ?*/
4661 desc7->dwTextureOpCaps &= (
4662 D3DTEXOPCAPS_DISABLE | D3DTEXOPCAPS_SELECTARG1 | D3DTEXOPCAPS_SELECTARG2 |
4663 D3DTEXOPCAPS_MODULATE | D3DTEXOPCAPS_MODULATE2X | D3DTEXOPCAPS_MODULATE4X |
4664 D3DTEXOPCAPS_ADD | D3DTEXOPCAPS_ADDSIGNED | D3DTEXOPCAPS_ADDSIGNED2X |
4665 D3DTEXOPCAPS_SUBTRACT | D3DTEXOPCAPS_ADDSMOOTH | D3DTEXOPCAPS_BLENDTEXTUREALPHA |
4666 D3DTEXOPCAPS_BLENDFACTORALPHA | D3DTEXOPCAPS_BLENDTEXTUREALPHAPM | D3DTEXOPCAPS_BLENDCURRENTALPHA |
4667 D3DTEXOPCAPS_PREMODULATE | D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
4668 D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA | D3DTEXOPCAPS_BUMPENVMAP |
4669 D3DTEXOPCAPS_BUMPENVMAPLUMINANCE | D3DTEXOPCAPS_DOTPRODUCT3);
4671 desc7->dwVertexProcessingCaps &= (
4672 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_VERTEXFOG |
4673 D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER);
4675 desc7->dpcLineCaps.dwMiscCaps &= (
4676 D3DPMISCCAPS_MASKPLANES | D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP |
4677 D3DPMISCCAPS_CONFORMANT | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW |
4678 D3DPMISCCAPS_CULLCCW);
4680 desc7->dpcLineCaps.dwRasterCaps &= (
4681 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ROP2 | D3DPRASTERCAPS_XOR |
4682 D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_SUBPIXEL |
4683 D3DPRASTERCAPS_SUBPIXELX | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE |
4684 D3DPRASTERCAPS_STIPPLE | D3DPRASTERCAPS_ANTIALIASSORTDEPENDENT | D3DPRASTERCAPS_ANTIALIASSORTINDEPENDENT |
4685 D3DPRASTERCAPS_ANTIALIASEDGES | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBIAS |
4686 D3DPRASTERCAPS_ZBUFFERLESSHSR | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY |
4687 D3DPRASTERCAPS_WBUFFER | D3DPRASTERCAPS_TRANSLUCENTSORTINDEPENDENT | D3DPRASTERCAPS_WFOG |
4688 D3DPRASTERCAPS_ZFOG);
4690 desc7->dpcLineCaps.dwZCmpCaps &= (
4691 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
4692 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
4693 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
4695 desc7->dpcLineCaps.dwSrcBlendCaps &= (
4696 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
4697 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
4698 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
4699 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
4700 D3DPBLENDCAPS_BOTHINVSRCALPHA);
4702 desc7->dpcLineCaps.dwDestBlendCaps &= (
4703 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
4704 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
4705 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
4706 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
4707 D3DPBLENDCAPS_BOTHINVSRCALPHA);
4709 desc7->dpcLineCaps.dwAlphaCmpCaps &= (
4710 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
4711 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
4712 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
4714 desc7->dpcLineCaps.dwShadeCaps &= (
4715 D3DPSHADECAPS_COLORFLATMONO | D3DPSHADECAPS_COLORFLATRGB | D3DPSHADECAPS_COLORGOURAUDMONO |
4716 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_COLORPHONGMONO | D3DPSHADECAPS_COLORPHONGRGB |
4717 D3DPSHADECAPS_SPECULARFLATMONO | D3DPSHADECAPS_SPECULARFLATRGB | D3DPSHADECAPS_SPECULARGOURAUDMONO |
4718 D3DPSHADECAPS_SPECULARGOURAUDRGB | D3DPSHADECAPS_SPECULARPHONGMONO | D3DPSHADECAPS_SPECULARPHONGRGB |
4719 D3DPSHADECAPS_ALPHAFLATBLEND | D3DPSHADECAPS_ALPHAFLATSTIPPLED | D3DPSHADECAPS_ALPHAGOURAUDBLEND |
4720 D3DPSHADECAPS_ALPHAGOURAUDSTIPPLED | D3DPSHADECAPS_ALPHAPHONGBLEND | D3DPSHADECAPS_ALPHAPHONGSTIPPLED |
4721 D3DPSHADECAPS_FOGFLAT | D3DPSHADECAPS_FOGGOURAUD | D3DPSHADECAPS_FOGPHONG);
4723 desc7->dpcLineCaps.dwTextureCaps &= (
4724 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
4725 D3DPTEXTURECAPS_TRANSPARENCY | D3DPTEXTURECAPS_BORDER | D3DPTEXTURECAPS_SQUAREONLY |
4726 D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE | D3DPTEXTURECAPS_ALPHAPALETTE| D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
4727 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_COLORKEYBLEND);
4729 desc7->dpcLineCaps.dwTextureFilterCaps &= (
4730 D3DPTFILTERCAPS_NEAREST | D3DPTFILTERCAPS_LINEAR | D3DPTFILTERCAPS_MIPNEAREST |
4731 D3DPTFILTERCAPS_MIPLINEAR | D3DPTFILTERCAPS_LINEARMIPNEAREST | D3DPTFILTERCAPS_LINEARMIPLINEAR |
4732 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
4733 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
4734 D3DPTFILTERCAPS_MAGFLINEAR | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC |
4735 D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC);
4737 desc7->dpcLineCaps.dwTextureBlendCaps &= (
4738 D3DPTBLENDCAPS_DECAL | D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_DECALALPHA |
4739 D3DPTBLENDCAPS_MODULATEALPHA | D3DPTBLENDCAPS_DECALMASK | D3DPTBLENDCAPS_MODULATEMASK |
4740 D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_ADD);
4742 desc7->dpcLineCaps.dwTextureAddressCaps &= (
4743 D3DPTADDRESSCAPS_WRAP | D3DPTADDRESSCAPS_MIRROR | D3DPTADDRESSCAPS_CLAMP |
4744 D3DPTADDRESSCAPS_BORDER | D3DPTADDRESSCAPS_INDEPENDENTUV);
4746 if (!(desc7->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2))
4748 /* DirectX7 always has the np2 flag set, no matter what the card
4749 * supports. Some old games (Rollcage) check the caps incorrectly.
4750 * If wined3d supports nonpow2 textures it also has np2 conditional
4751 * support. */
4752 desc7->dpcLineCaps.dwTextureCaps |= D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL;
4755 /* Fill the missing members, and do some fixup */
4756 desc7->dpcLineCaps.dwSize = sizeof(desc7->dpcLineCaps);
4757 desc7->dpcLineCaps.dwTextureBlendCaps = D3DPTBLENDCAPS_ADD | D3DPTBLENDCAPS_MODULATEMASK |
4758 D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_DECAL |
4759 D3DPTBLENDCAPS_DECALALPHA | D3DPTBLENDCAPS_DECALMASK |
4760 D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_MODULATEALPHA;
4761 desc7->dpcLineCaps.dwStippleWidth = 32;
4762 desc7->dpcLineCaps.dwStippleHeight = 32;
4763 /* Use the same for the TriCaps */
4764 desc7->dpcTriCaps = desc7->dpcLineCaps;
4766 desc7->dwDeviceRenderBitDepth = DDBD_16 | DDBD_24 | DDBD_32;
4767 desc7->dwDeviceZBufferBitDepth = DDBD_16 | DDBD_24;
4768 desc7->dwMinTextureWidth = 1;
4769 desc7->dwMinTextureHeight = 1;
4771 /* Convert DWORDs safely to WORDs */
4772 if (wined3d_caps.MaxTextureBlendStages > 0xffff) desc7->wMaxTextureBlendStages = 0xffff;
4773 else desc7->wMaxTextureBlendStages = (WORD)wined3d_caps.MaxTextureBlendStages;
4774 if (wined3d_caps.MaxSimultaneousTextures > 0xffff) desc7->wMaxSimultaneousTextures = 0xffff;
4775 else desc7->wMaxSimultaneousTextures = (WORD)wined3d_caps.MaxSimultaneousTextures;
4777 if (wined3d_caps.MaxUserClipPlanes > 0xffff) desc7->wMaxUserClipPlanes = 0xffff;
4778 else desc7->wMaxUserClipPlanes = (WORD)wined3d_caps.MaxUserClipPlanes;
4779 if (wined3d_caps.MaxVertexBlendMatrices > 0xffff) desc7->wMaxVertexBlendMatrices = 0xffff;
4780 else desc7->wMaxVertexBlendMatrices = (WORD)wined3d_caps.MaxVertexBlendMatrices;
4782 desc7->deviceGUID = IID_IDirect3DTnLHalDevice;
4784 desc7->dwReserved1 = 0;
4785 desc7->dwReserved2 = 0;
4786 desc7->dwReserved3 = 0;
4787 desc7->dwReserved4 = 0;
4789 /* Fill the old structure */
4790 memset(desc1, 0, sizeof(*desc1));
4791 desc1->dwSize = sizeof(D3DDEVICEDESC);
4792 desc1->dwFlags = D3DDD_COLORMODEL
4793 | D3DDD_DEVCAPS
4794 | D3DDD_TRANSFORMCAPS
4795 | D3DDD_BCLIPPING
4796 | D3DDD_LIGHTINGCAPS
4797 | D3DDD_LINECAPS
4798 | D3DDD_TRICAPS
4799 | D3DDD_DEVICERENDERBITDEPTH
4800 | D3DDD_DEVICEZBUFFERBITDEPTH
4801 | D3DDD_MAXBUFFERSIZE
4802 | D3DDD_MAXVERTEXCOUNT;
4804 desc1->dcmColorModel = D3DCOLOR_RGB;
4805 desc1->dwDevCaps = desc7->dwDevCaps;
4806 desc1->dtcTransformCaps.dwSize = sizeof(D3DTRANSFORMCAPS);
4807 desc1->dtcTransformCaps.dwCaps = D3DTRANSFORMCAPS_CLIP;
4808 desc1->bClipping = TRUE;
4809 desc1->dlcLightingCaps.dwSize = sizeof(D3DLIGHTINGCAPS);
4810 desc1->dlcLightingCaps.dwCaps = D3DLIGHTCAPS_DIRECTIONAL
4811 | D3DLIGHTCAPS_PARALLELPOINT
4812 | D3DLIGHTCAPS_POINT
4813 | D3DLIGHTCAPS_SPOT;
4815 desc1->dlcLightingCaps.dwLightingModel = D3DLIGHTINGMODEL_RGB;
4816 desc1->dlcLightingCaps.dwNumLights = desc7->dwMaxActiveLights;
4818 desc1->dpcLineCaps.dwSize = sizeof(D3DPRIMCAPS);
4819 desc1->dpcLineCaps.dwMiscCaps = desc7->dpcLineCaps.dwMiscCaps;
4820 desc1->dpcLineCaps.dwRasterCaps = desc7->dpcLineCaps.dwRasterCaps;
4821 desc1->dpcLineCaps.dwZCmpCaps = desc7->dpcLineCaps.dwZCmpCaps;
4822 desc1->dpcLineCaps.dwSrcBlendCaps = desc7->dpcLineCaps.dwSrcBlendCaps;
4823 desc1->dpcLineCaps.dwDestBlendCaps = desc7->dpcLineCaps.dwDestBlendCaps;
4824 desc1->dpcLineCaps.dwShadeCaps = desc7->dpcLineCaps.dwShadeCaps;
4825 desc1->dpcLineCaps.dwTextureCaps = desc7->dpcLineCaps.dwTextureCaps;
4826 desc1->dpcLineCaps.dwTextureFilterCaps = desc7->dpcLineCaps.dwTextureFilterCaps;
4827 desc1->dpcLineCaps.dwTextureBlendCaps = desc7->dpcLineCaps.dwTextureBlendCaps;
4828 desc1->dpcLineCaps.dwTextureAddressCaps = desc7->dpcLineCaps.dwTextureAddressCaps;
4829 desc1->dpcLineCaps.dwStippleWidth = desc7->dpcLineCaps.dwStippleWidth;
4830 desc1->dpcLineCaps.dwAlphaCmpCaps = desc7->dpcLineCaps.dwAlphaCmpCaps;
4832 desc1->dpcTriCaps.dwSize = sizeof(D3DPRIMCAPS);
4833 desc1->dpcTriCaps.dwMiscCaps = desc7->dpcTriCaps.dwMiscCaps;
4834 desc1->dpcTriCaps.dwRasterCaps = desc7->dpcTriCaps.dwRasterCaps;
4835 desc1->dpcTriCaps.dwZCmpCaps = desc7->dpcTriCaps.dwZCmpCaps;
4836 desc1->dpcTriCaps.dwSrcBlendCaps = desc7->dpcTriCaps.dwSrcBlendCaps;
4837 desc1->dpcTriCaps.dwDestBlendCaps = desc7->dpcTriCaps.dwDestBlendCaps;
4838 desc1->dpcTriCaps.dwShadeCaps = desc7->dpcTriCaps.dwShadeCaps;
4839 desc1->dpcTriCaps.dwTextureCaps = desc7->dpcTriCaps.dwTextureCaps;
4840 desc1->dpcTriCaps.dwTextureFilterCaps = desc7->dpcTriCaps.dwTextureFilterCaps;
4841 desc1->dpcTriCaps.dwTextureBlendCaps = desc7->dpcTriCaps.dwTextureBlendCaps;
4842 desc1->dpcTriCaps.dwTextureAddressCaps = desc7->dpcTriCaps.dwTextureAddressCaps;
4843 desc1->dpcTriCaps.dwStippleWidth = desc7->dpcTriCaps.dwStippleWidth;
4844 desc1->dpcTriCaps.dwAlphaCmpCaps = desc7->dpcTriCaps.dwAlphaCmpCaps;
4846 desc1->dwDeviceRenderBitDepth = desc7->dwDeviceRenderBitDepth;
4847 desc1->dwDeviceZBufferBitDepth = desc7->dwDeviceZBufferBitDepth;
4848 desc1->dwMaxBufferSize = 0;
4849 desc1->dwMaxVertexCount = 65536;
4850 desc1->dwMinTextureWidth = desc7->dwMinTextureWidth;
4851 desc1->dwMinTextureHeight = desc7->dwMinTextureHeight;
4852 desc1->dwMaxTextureWidth = desc7->dwMaxTextureWidth;
4853 desc1->dwMaxTextureHeight = desc7->dwMaxTextureHeight;
4854 desc1->dwMinStippleWidth = 1;
4855 desc1->dwMinStippleHeight = 1;
4856 desc1->dwMaxStippleWidth = 32;
4857 desc1->dwMaxStippleHeight = 32;
4858 desc1->dwMaxTextureRepeat = desc7->dwMaxTextureRepeat;
4859 desc1->dwMaxTextureAspectRatio = desc7->dwMaxTextureAspectRatio;
4860 desc1->dwMaxAnisotropy = desc7->dwMaxAnisotropy;
4861 desc1->dvGuardBandLeft = desc7->dvGuardBandLeft;
4862 desc1->dvGuardBandRight = desc7->dvGuardBandRight;
4863 desc1->dvGuardBandTop = desc7->dvGuardBandTop;
4864 desc1->dvGuardBandBottom = desc7->dvGuardBandBottom;
4865 desc1->dvExtentsAdjust = desc7->dvExtentsAdjust;
4866 desc1->dwStencilCaps = desc7->dwStencilCaps;
4867 desc1->dwFVFCaps = desc7->dwFVFCaps;
4868 desc1->dwTextureOpCaps = desc7->dwTextureOpCaps;
4869 desc1->wMaxTextureBlendStages = desc7->wMaxTextureBlendStages;
4870 desc1->wMaxSimultaneousTextures = desc7->wMaxSimultaneousTextures;
4872 return DD_OK;
4875 /*****************************************************************************
4876 * IDirectDraw7 VTable
4877 *****************************************************************************/
4878 static const struct IDirectDraw7Vtbl ddraw7_vtbl =
4880 /* IUnknown */
4881 ddraw7_QueryInterface,
4882 ddraw7_AddRef,
4883 ddraw7_Release,
4884 /* IDirectDraw */
4885 ddraw7_Compact,
4886 ddraw7_CreateClipper,
4887 ddraw7_CreatePalette,
4888 ddraw7_CreateSurface,
4889 ddraw7_DuplicateSurface,
4890 ddraw7_EnumDisplayModes,
4891 ddraw7_EnumSurfaces,
4892 ddraw7_FlipToGDISurface,
4893 ddraw7_GetCaps,
4894 ddraw7_GetDisplayMode,
4895 ddraw7_GetFourCCCodes,
4896 ddraw7_GetGDISurface,
4897 ddraw7_GetMonitorFrequency,
4898 ddraw7_GetScanLine,
4899 ddraw7_GetVerticalBlankStatus,
4900 ddraw7_Initialize,
4901 ddraw7_RestoreDisplayMode,
4902 ddraw7_SetCooperativeLevel,
4903 ddraw7_SetDisplayMode,
4904 ddraw7_WaitForVerticalBlank,
4905 /* IDirectDraw2 */
4906 ddraw7_GetAvailableVidMem,
4907 /* IDirectDraw3 */
4908 ddraw7_GetSurfaceFromDC,
4909 /* IDirectDraw4 */
4910 ddraw7_RestoreAllSurfaces,
4911 ddraw7_TestCooperativeLevel,
4912 ddraw7_GetDeviceIdentifier,
4913 /* IDirectDraw7 */
4914 ddraw7_StartModeTest,
4915 ddraw7_EvaluateMode
4918 static const struct IDirectDraw4Vtbl ddraw4_vtbl =
4920 /* IUnknown */
4921 ddraw4_QueryInterface,
4922 ddraw4_AddRef,
4923 ddraw4_Release,
4924 /* IDirectDraw */
4925 ddraw4_Compact,
4926 ddraw4_CreateClipper,
4927 ddraw4_CreatePalette,
4928 ddraw4_CreateSurface,
4929 ddraw4_DuplicateSurface,
4930 ddraw4_EnumDisplayModes,
4931 ddraw4_EnumSurfaces,
4932 ddraw4_FlipToGDISurface,
4933 ddraw4_GetCaps,
4934 ddraw4_GetDisplayMode,
4935 ddraw4_GetFourCCCodes,
4936 ddraw4_GetGDISurface,
4937 ddraw4_GetMonitorFrequency,
4938 ddraw4_GetScanLine,
4939 ddraw4_GetVerticalBlankStatus,
4940 ddraw4_Initialize,
4941 ddraw4_RestoreDisplayMode,
4942 ddraw4_SetCooperativeLevel,
4943 ddraw4_SetDisplayMode,
4944 ddraw4_WaitForVerticalBlank,
4945 /* IDirectDraw2 */
4946 ddraw4_GetAvailableVidMem,
4947 /* IDirectDraw3 */
4948 ddraw4_GetSurfaceFromDC,
4949 /* IDirectDraw4 */
4950 ddraw4_RestoreAllSurfaces,
4951 ddraw4_TestCooperativeLevel,
4952 ddraw4_GetDeviceIdentifier,
4955 static const struct IDirectDraw2Vtbl ddraw2_vtbl =
4957 /* IUnknown */
4958 ddraw2_QueryInterface,
4959 ddraw2_AddRef,
4960 ddraw2_Release,
4961 /* IDirectDraw */
4962 ddraw2_Compact,
4963 ddraw2_CreateClipper,
4964 ddraw2_CreatePalette,
4965 ddraw2_CreateSurface,
4966 ddraw2_DuplicateSurface,
4967 ddraw2_EnumDisplayModes,
4968 ddraw2_EnumSurfaces,
4969 ddraw2_FlipToGDISurface,
4970 ddraw2_GetCaps,
4971 ddraw2_GetDisplayMode,
4972 ddraw2_GetFourCCCodes,
4973 ddraw2_GetGDISurface,
4974 ddraw2_GetMonitorFrequency,
4975 ddraw2_GetScanLine,
4976 ddraw2_GetVerticalBlankStatus,
4977 ddraw2_Initialize,
4978 ddraw2_RestoreDisplayMode,
4979 ddraw2_SetCooperativeLevel,
4980 ddraw2_SetDisplayMode,
4981 ddraw2_WaitForVerticalBlank,
4982 /* IDirectDraw2 */
4983 ddraw2_GetAvailableVidMem,
4986 static const struct IDirectDrawVtbl ddraw1_vtbl =
4988 /* IUnknown */
4989 ddraw1_QueryInterface,
4990 ddraw1_AddRef,
4991 ddraw1_Release,
4992 /* IDirectDraw */
4993 ddraw1_Compact,
4994 ddraw1_CreateClipper,
4995 ddraw1_CreatePalette,
4996 ddraw1_CreateSurface,
4997 ddraw1_DuplicateSurface,
4998 ddraw1_EnumDisplayModes,
4999 ddraw1_EnumSurfaces,
5000 ddraw1_FlipToGDISurface,
5001 ddraw1_GetCaps,
5002 ddraw1_GetDisplayMode,
5003 ddraw1_GetFourCCCodes,
5004 ddraw1_GetGDISurface,
5005 ddraw1_GetMonitorFrequency,
5006 ddraw1_GetScanLine,
5007 ddraw1_GetVerticalBlankStatus,
5008 ddraw1_Initialize,
5009 ddraw1_RestoreDisplayMode,
5010 ddraw1_SetCooperativeLevel,
5011 ddraw1_SetDisplayMode,
5012 ddraw1_WaitForVerticalBlank,
5015 static const struct IDirect3D7Vtbl d3d7_vtbl =
5017 /* IUnknown methods */
5018 d3d7_QueryInterface,
5019 d3d7_AddRef,
5020 d3d7_Release,
5021 /* IDirect3D7 methods */
5022 d3d7_EnumDevices,
5023 d3d7_CreateDevice,
5024 d3d7_CreateVertexBuffer,
5025 d3d7_EnumZBufferFormats,
5026 d3d7_EvictManagedTextures
5029 static const struct IDirect3D3Vtbl d3d3_vtbl =
5031 /* IUnknown methods */
5032 d3d3_QueryInterface,
5033 d3d3_AddRef,
5034 d3d3_Release,
5035 /* IDirect3D3 methods */
5036 d3d3_EnumDevices,
5037 d3d3_CreateLight,
5038 d3d3_CreateMaterial,
5039 d3d3_CreateViewport,
5040 d3d3_FindDevice,
5041 d3d3_CreateDevice,
5042 d3d3_CreateVertexBuffer,
5043 d3d3_EnumZBufferFormats,
5044 d3d3_EvictManagedTextures
5047 static const struct IDirect3D2Vtbl d3d2_vtbl =
5049 /* IUnknown methods */
5050 d3d2_QueryInterface,
5051 d3d2_AddRef,
5052 d3d2_Release,
5053 /* IDirect3D2 methods */
5054 d3d2_EnumDevices,
5055 d3d2_CreateLight,
5056 d3d2_CreateMaterial,
5057 d3d2_CreateViewport,
5058 d3d2_FindDevice,
5059 d3d2_CreateDevice
5062 static const struct IDirect3DVtbl d3d1_vtbl =
5064 /* IUnknown methods */
5065 d3d1_QueryInterface,
5066 d3d1_AddRef,
5067 d3d1_Release,
5068 /* IDirect3D methods */
5069 d3d1_Initialize,
5070 d3d1_EnumDevices,
5071 d3d1_CreateLight,
5072 d3d1_CreateMaterial,
5073 d3d1_CreateViewport,
5074 d3d1_FindDevice
5077 /*****************************************************************************
5078 * ddraw_find_decl
5080 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
5081 * if none was found.
5083 * This function is in ddraw.c and the DDraw object space because D3D7
5084 * vertex buffers are created using the IDirect3D interface to the ddraw
5085 * object, so they can be valid across D3D devices(theoretically. The ddraw
5086 * object also owns the wined3d device
5088 * Parameters:
5089 * This: Device
5090 * fvf: Fvf to find the decl for
5092 * Returns:
5093 * NULL in case of an error, the vertex declaration for the FVF otherwise.
5095 *****************************************************************************/
5096 struct wined3d_vertex_declaration *ddraw_find_decl(struct ddraw *This, DWORD fvf)
5098 struct wined3d_vertex_declaration *pDecl = NULL;
5099 HRESULT hr;
5100 int p, low, high; /* deliberately signed */
5101 struct FvfToDecl *convertedDecls = This->decls;
5103 TRACE("Searching for declaration for fvf %08x... ", fvf);
5105 low = 0;
5106 high = This->numConvertedDecls - 1;
5107 while(low <= high) {
5108 p = (low + high) >> 1;
5109 TRACE("%d ", p);
5110 if(convertedDecls[p].fvf == fvf) {
5111 TRACE("found %p\n", convertedDecls[p].decl);
5112 return convertedDecls[p].decl;
5113 } else if(convertedDecls[p].fvf < fvf) {
5114 low = p + 1;
5115 } else {
5116 high = p - 1;
5119 TRACE("not found. Creating and inserting at position %d.\n", low);
5121 hr = wined3d_vertex_declaration_create_from_fvf(This->wined3d_device,
5122 fvf, This, &ddraw_null_wined3d_parent_ops, &pDecl);
5123 if (hr != S_OK) return NULL;
5125 if(This->declArraySize == This->numConvertedDecls) {
5126 int grow = max(This->declArraySize / 2, 8);
5127 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
5128 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
5129 if (!convertedDecls)
5131 wined3d_vertex_declaration_decref(pDecl);
5132 return NULL;
5134 This->decls = convertedDecls;
5135 This->declArraySize += grow;
5138 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
5139 convertedDecls[low].decl = pDecl;
5140 convertedDecls[low].fvf = fvf;
5141 This->numConvertedDecls++;
5143 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
5144 return pDecl;
5147 static inline struct ddraw *ddraw_from_device_parent(struct wined3d_device_parent *device_parent)
5149 return CONTAINING_RECORD(device_parent, struct ddraw, device_parent);
5152 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
5153 struct wined3d_device *device)
5155 TRACE("device_parent %p, device %p.\n", device_parent, device);
5158 /* This is run from device_process_message() in wined3d, we can't take the
5159 * wined3d mutex. */
5160 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
5162 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
5163 MONITORINFO monitor_info;
5164 HMONITOR monitor;
5165 BOOL ret;
5166 RECT *r;
5168 TRACE("device_parent %p.\n", device_parent);
5170 if (!(ddraw->cooperative_level & DDSCL_EXCLUSIVE) || !ddraw->swapchain_window)
5172 TRACE("Nothing to resize.\n");
5173 return;
5176 monitor = MonitorFromWindow(ddraw->swapchain_window, MONITOR_DEFAULTTOPRIMARY);
5177 monitor_info.cbSize = sizeof(monitor_info);
5178 if (!(ret = GetMonitorInfoW(monitor, &monitor_info)))
5180 ERR("Failed to get monitor info.\n");
5181 return;
5184 r = &monitor_info.rcMonitor;
5185 TRACE("Resizing window %p to %s.\n", ddraw->swapchain_window, wine_dbgstr_rect(r));
5187 if (!(ret = SetWindowPos(ddraw->swapchain_window, HWND_TOP, r->left, r->top,
5188 r->right - r->left, r->bottom - r->top, SWP_SHOWWINDOW | SWP_NOACTIVATE)))
5189 ERR("Failed to resize window.\n");
5192 static HRESULT CDECL device_parent_create_texture_surface(struct wined3d_device_parent *device_parent,
5193 void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
5194 enum wined3d_pool pool, UINT level, enum wined3d_cubemap_face face, struct wined3d_surface **surface)
5196 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
5197 struct ddraw_surface *tex_root = container_parent;
5198 DDSURFACEDESC2 desc = tex_root->surface_desc;
5199 struct ddraw_surface *ddraw_surface;
5200 HRESULT hr;
5202 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
5203 "\tpool %#x, level %u, face %u, surface %p.\n",
5204 device_parent, container_parent, width, height, format, usage, pool, level, face, surface);
5206 /* The ddraw root surface is created before the wined3d texture. */
5207 if (!level && face == WINED3D_CUBEMAP_FACE_POSITIVE_X)
5209 ddraw_surface = tex_root;
5210 goto done;
5213 desc.dwWidth = width;
5214 desc.dwHeight = height;
5215 if (level)
5216 desc.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
5217 else
5218 desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
5220 if (desc.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5222 desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
5224 switch (face)
5226 case WINED3D_CUBEMAP_FACE_POSITIVE_X:
5227 desc.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
5228 break;
5230 case WINED3D_CUBEMAP_FACE_NEGATIVE_X:
5231 desc.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
5232 break;
5234 case WINED3D_CUBEMAP_FACE_POSITIVE_Y:
5235 desc.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
5236 break;
5238 case WINED3D_CUBEMAP_FACE_NEGATIVE_Y:
5239 desc.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
5240 break;
5242 case WINED3D_CUBEMAP_FACE_POSITIVE_Z:
5243 desc.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
5244 break;
5246 case WINED3D_CUBEMAP_FACE_NEGATIVE_Z:
5247 desc.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
5248 break;
5250 default:
5251 ERR("Unexpected cube face.\n");
5252 return DDERR_INVALIDPARAMS;
5257 /* FIXME: Validate that format, usage, pool, etc. really make sense. */
5258 if (FAILED(hr = ddraw_create_surface(ddraw, &desc, &ddraw_surface, level, tex_root->version)))
5259 return hr;
5261 done:
5262 *surface = ddraw_surface->wined3d_surface;
5263 wined3d_surface_incref(*surface);
5265 return DD_OK;
5268 static void STDMETHODCALLTYPE ddraw_frontbuffer_destroyed(void *parent)
5270 struct ddraw *ddraw = parent;
5271 ddraw->wined3d_frontbuffer = NULL;
5274 static const struct wined3d_parent_ops ddraw_frontbuffer_parent_ops =
5276 ddraw_frontbuffer_destroyed,
5279 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
5280 void *container_parent, UINT width, UINT height, enum wined3d_format_id format_id, DWORD usage,
5281 enum wined3d_multisample_type multisample_type, DWORD multisample_quality, struct wined3d_surface **surface)
5283 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
5284 HRESULT hr;
5286 TRACE("device_parent %p, container_parent %p, width %u, height %u, format_id %#x, usage %#x,\n"
5287 "\tmultisample_type %#x, multisample_quality %u, surface %p.\n",
5288 device_parent, container_parent, width, height, format_id, usage,
5289 multisample_type, multisample_quality, surface);
5291 if (ddraw->wined3d_frontbuffer)
5293 ERR("Frontbuffer already created.\n");
5294 return E_FAIL;
5297 if (SUCCEEDED(hr = wined3d_surface_create(ddraw->wined3d_device, width, height, format_id, 0,
5298 usage, WINED3D_POOL_DEFAULT, multisample_type, multisample_quality, DefaultSurfaceType,
5299 WINED3D_SURFACE_MAPPABLE, ddraw, &ddraw_frontbuffer_parent_ops, surface)))
5300 ddraw->wined3d_frontbuffer = *surface;
5302 return hr;
5305 static HRESULT CDECL device_parent_create_volume(struct wined3d_device_parent *device_parent,
5306 void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
5307 enum wined3d_pool pool, DWORD usage, struct wined3d_volume **volume)
5309 TRACE("device_parent %p, container_parent %p, width %u, height %u, depth %u, "
5310 "format %#x, pool %#x, usage %#x, volume %p.\n",
5311 device_parent, container_parent, width, height, depth,
5312 format, pool, usage, volume);
5314 ERR("Not implemented!\n");
5316 return E_NOTIMPL;
5319 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
5320 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
5322 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
5323 HRESULT hr;
5325 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
5327 if (ddraw->wined3d_swapchain)
5329 ERR("Swapchain already created.\n");
5330 return E_FAIL;
5333 hr = wined3d_swapchain_create(ddraw->wined3d_device, desc,
5334 DefaultSurfaceType, NULL, &ddraw_null_wined3d_parent_ops, swapchain);
5335 if (FAILED(hr))
5336 WARN("Failed to create swapchain, hr %#x.\n", hr);
5338 return hr;
5341 static const struct wined3d_device_parent_ops ddraw_wined3d_device_parent_ops =
5343 device_parent_wined3d_device_created,
5344 device_parent_mode_changed,
5345 device_parent_create_swapchain_surface,
5346 device_parent_create_texture_surface,
5347 device_parent_create_volume,
5348 device_parent_create_swapchain,
5351 HRESULT ddraw_init(struct ddraw *ddraw, enum wined3d_device_type device_type)
5353 HRESULT hr;
5355 ddraw->IDirectDraw7_iface.lpVtbl = &ddraw7_vtbl;
5356 ddraw->IDirectDraw_iface.lpVtbl = &ddraw1_vtbl;
5357 ddraw->IDirectDraw2_iface.lpVtbl = &ddraw2_vtbl;
5358 ddraw->IDirectDraw4_iface.lpVtbl = &ddraw4_vtbl;
5359 ddraw->IDirect3D_iface.lpVtbl = &d3d1_vtbl;
5360 ddraw->IDirect3D2_iface.lpVtbl = &d3d2_vtbl;
5361 ddraw->IDirect3D3_iface.lpVtbl = &d3d3_vtbl;
5362 ddraw->IDirect3D7_iface.lpVtbl = &d3d7_vtbl;
5363 ddraw->device_parent.ops = &ddraw_wined3d_device_parent_ops;
5364 ddraw->numIfaces = 1;
5365 ddraw->ref7 = 1;
5367 ddraw->wined3d = wined3d_create(7, WINED3D_LEGACY_DEPTH_BIAS);
5368 if (!ddraw->wined3d)
5370 WARN("Failed to create a wined3d object.\n");
5371 return E_OUTOFMEMORY;
5374 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d,
5375 WINED3DADAPTER_DEFAULT, &ddraw->original_mode, NULL)))
5377 ERR("Failed to get display mode, hr %#x.\n", hr);
5378 wined3d_decref(ddraw->wined3d);
5379 return hr;
5382 hr = wined3d_device_create(ddraw->wined3d, WINED3DADAPTER_DEFAULT, device_type,
5383 NULL, 0, 8, &ddraw->device_parent, &ddraw->wined3d_device);
5384 if (FAILED(hr))
5386 WARN("Failed to create a wined3d device, hr %#x.\n", hr);
5387 wined3d_decref(ddraw->wined3d);
5388 return hr;
5391 list_init(&ddraw->surface_list);
5393 return DD_OK;