mshtml: Added nsIChannel::SetContentCharset implementation.
[wine/multimedia.git] / dlls / ddraw / ddraw.c
blob09b4bc11f052d3719eee2c55195faffa0a29ca04
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 /* Device identifier. Don't relay it to WineD3D */
31 static const DDDEVICEIDENTIFIER2 deviceidentifier =
33 "display",
34 "DirectDraw HAL",
35 { { 0x00010001, 0x00010001 } },
36 0, 0, 0, 0,
37 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
38 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
42 static struct enum_device_entry
44 char interface_name[100];
45 char device_name[100];
46 const GUID *device_guid;
47 } device_list7[] =
49 /* T&L HAL device */
51 "WINE Direct3D7 Hardware Transform and Lighting acceleration using WineD3D",
52 "Wine D3D7 T&L HAL",
53 &IID_IDirect3DTnLHalDevice,
56 /* HAL device */
58 "WINE Direct3D7 Hardware acceleration using WineD3D",
59 "Direct3D HAL",
60 &IID_IDirect3DHALDevice,
63 /* RGB device */
65 "WINE Direct3D7 RGB Software Emulation using WineD3D",
66 "Wine D3D7 RGB",
67 &IID_IDirect3DRGBDevice,
71 static void STDMETHODCALLTYPE ddraw_null_wined3d_object_destroyed(void *parent) {}
73 const struct wined3d_parent_ops ddraw_null_wined3d_parent_ops =
75 ddraw_null_wined3d_object_destroyed,
78 static inline IDirectDrawImpl *impl_from_IDirectDraw(IDirectDraw *iface)
80 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw_iface);
83 static inline IDirectDrawImpl *impl_from_IDirectDraw2(IDirectDraw2 *iface)
85 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw2_iface);
88 static inline IDirectDrawImpl *impl_from_IDirectDraw4(IDirectDraw4 *iface)
90 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw4_iface);
93 static inline IDirectDrawImpl *impl_from_IDirectDraw7(IDirectDraw7 *iface)
95 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw7_iface);
98 static inline IDirectDrawImpl *impl_from_IDirect3D(IDirect3D *iface)
100 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D_iface);
103 static inline IDirectDrawImpl *impl_from_IDirect3D2(IDirect3D2 *iface)
105 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D2_iface);
108 static inline IDirectDrawImpl *impl_from_IDirect3D3(IDirect3D3 *iface)
110 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D3_iface);
113 static inline IDirectDrawImpl *impl_from_IDirect3D7(IDirect3D7 *iface)
115 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D7_iface);
118 /*****************************************************************************
119 * IUnknown Methods
120 *****************************************************************************/
122 /*****************************************************************************
123 * IDirectDraw7::QueryInterface
125 * Queries different interfaces of the DirectDraw object. It can return
126 * IDirectDraw interfaces in version 1, 2, 4 and 7, and IDirect3D interfaces
127 * in version 1, 2, 3 and 7. An IDirect3DDevice can be created with this
128 * method.
129 * The returned interface is AddRef()-ed before it's returned
131 * Used for version 1, 2, 4 and 7
133 * Params:
134 * refiid: Interface ID asked for
135 * obj: Used to return the interface pointer
137 * Returns:
138 * S_OK if an interface was found
139 * E_NOINTERFACE if the requested interface wasn't found
141 *****************************************************************************/
142 static HRESULT WINAPI ddraw7_QueryInterface(IDirectDraw7 *iface, REFIID refiid, void **obj)
144 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
146 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(refiid), obj);
148 /* Can change surface impl type */
149 wined3d_mutex_lock();
151 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
152 *obj = NULL;
154 if(!refiid)
156 wined3d_mutex_unlock();
157 return DDERR_INVALIDPARAMS;
160 /* Check DirectDraw Interfaces */
161 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
162 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
164 *obj = This;
165 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
167 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
169 *obj = &This->IDirectDraw4_iface;
170 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
172 else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
174 /* This Interface exists in ddrawex.dll, it is implemented in a wrapper */
175 WARN("IDirectDraw3 is not valid in ddraw.dll\n");
176 *obj = NULL;
177 wined3d_mutex_unlock();
178 return E_NOINTERFACE;
180 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
182 *obj = &This->IDirectDraw2_iface;
183 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
185 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
187 *obj = &This->IDirectDraw_iface;
188 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
191 /* Direct3D
192 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
193 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
194 * who had this idea and why. The older interfaces can query and IDirect3D version
195 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
196 * and messy to implement with the common creation function, so it has been left out here.
198 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
199 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
200 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
201 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
203 /* Check the surface implementation */
204 if (DefaultSurfaceType != SURFACE_OPENGL)
206 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
207 /* Do not abort here, only reject 3D Device creation */
210 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
212 This->d3dversion = 1;
213 *obj = &This->IDirect3D_iface;
214 TRACE(" returning Direct3D interface at %p.\n", *obj);
216 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
218 This->d3dversion = 2;
219 *obj = &This->IDirect3D2_iface;
220 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
222 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
224 This->d3dversion = 3;
225 *obj = &This->IDirect3D3_iface;
226 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
228 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
230 This->d3dversion = 7;
231 *obj = &This->IDirect3D7_iface;
232 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
235 /* Unknown interface */
236 else
238 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
239 wined3d_mutex_unlock();
240 return E_NOINTERFACE;
243 IUnknown_AddRef( (IUnknown *) *obj );
244 wined3d_mutex_unlock();
246 return S_OK;
249 static HRESULT WINAPI ddraw4_QueryInterface(IDirectDraw4 *iface, REFIID riid, void **object)
251 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
253 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
255 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
258 static HRESULT WINAPI ddraw2_QueryInterface(IDirectDraw2 *iface, REFIID riid, void **object)
260 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
262 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
264 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
267 static HRESULT WINAPI ddraw1_QueryInterface(IDirectDraw *iface, REFIID riid, void **object)
269 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
271 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
273 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
276 static HRESULT WINAPI d3d7_QueryInterface(IDirect3D7 *iface, REFIID riid, void **object)
278 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
280 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
282 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
285 static HRESULT WINAPI d3d3_QueryInterface(IDirect3D3 *iface, REFIID riid, void **object)
287 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
289 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
291 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
294 static HRESULT WINAPI d3d2_QueryInterface(IDirect3D2 *iface, REFIID riid, void **object)
296 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
298 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
300 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
303 static HRESULT WINAPI d3d1_QueryInterface(IDirect3D *iface, REFIID riid, void **object)
305 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
307 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
309 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
312 /*****************************************************************************
313 * IDirectDraw7::AddRef
315 * Increases the interfaces refcount, basically
317 * DDraw refcounting is a bit tricky. The different DirectDraw interface
318 * versions have individual refcounts, but the IDirect3D interfaces do not.
319 * All interfaces are from one object, that means calling QueryInterface on an
320 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
321 * IDirectDrawImpl object.
323 * That means all AddRef and Release implementations of IDirectDrawX work
324 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
325 * except of IDirect3D7 which thunks to IDirectDraw7
327 * Returns: The new refcount
329 *****************************************************************************/
330 static ULONG WINAPI ddraw7_AddRef(IDirectDraw7 *iface)
332 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
333 ULONG ref = InterlockedIncrement(&This->ref7);
335 TRACE("%p increasing refcount to %u.\n", This, ref);
337 if(ref == 1) InterlockedIncrement(&This->numIfaces);
339 return ref;
342 static ULONG WINAPI ddraw4_AddRef(IDirectDraw4 *iface)
344 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
345 ULONG ref = InterlockedIncrement(&This->ref4);
347 TRACE("%p increasing refcount to %u.\n", This, ref);
349 if (ref == 1) InterlockedIncrement(&This->numIfaces);
351 return ref;
354 static ULONG WINAPI ddraw2_AddRef(IDirectDraw2 *iface)
356 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
357 ULONG ref = InterlockedIncrement(&This->ref2);
359 TRACE("%p increasing refcount to %u.\n", This, ref);
361 if (ref == 1) InterlockedIncrement(&This->numIfaces);
363 return ref;
366 static ULONG WINAPI ddraw1_AddRef(IDirectDraw *iface)
368 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
369 ULONG ref = InterlockedIncrement(&This->ref1);
371 TRACE("%p increasing refcount to %u.\n", This, ref);
373 if (ref == 1) InterlockedIncrement(&This->numIfaces);
375 return ref;
378 static ULONG WINAPI d3d7_AddRef(IDirect3D7 *iface)
380 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
382 TRACE("iface %p.\n", iface);
384 return ddraw7_AddRef(&This->IDirectDraw7_iface);
387 static ULONG WINAPI d3d3_AddRef(IDirect3D3 *iface)
389 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
391 TRACE("iface %p.\n", iface);
393 return ddraw1_AddRef(&This->IDirectDraw_iface);
396 static ULONG WINAPI d3d2_AddRef(IDirect3D2 *iface)
398 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
400 TRACE("iface %p.\n", iface);
402 return ddraw1_AddRef(&This->IDirectDraw_iface);
405 static ULONG WINAPI d3d1_AddRef(IDirect3D *iface)
407 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
409 TRACE("iface %p.\n", iface);
411 return ddraw1_AddRef(&This->IDirectDraw_iface);
414 void ddraw_destroy_swapchain(IDirectDrawImpl *ddraw)
416 TRACE("Destroying the swapchain.\n");
418 wined3d_swapchain_decref(ddraw->wined3d_swapchain);
419 ddraw->wined3d_swapchain = NULL;
421 if (DefaultSurfaceType == SURFACE_OPENGL)
423 UINT i;
425 for (i = 0; i < ddraw->numConvertedDecls; ++i)
427 wined3d_vertex_declaration_decref(ddraw->decls[i].decl);
429 HeapFree(GetProcessHeap(), 0, ddraw->decls);
430 ddraw->numConvertedDecls = 0;
432 if (FAILED(wined3d_device_uninit_3d(ddraw->wined3d_device)))
434 ERR("Failed to uninit 3D.\n");
436 else
438 /* Free the d3d window if one was created. */
439 if (ddraw->d3d_window && ddraw->d3d_window != ddraw->dest_window)
441 TRACE("Destroying the hidden render window %p.\n", ddraw->d3d_window);
442 DestroyWindow(ddraw->d3d_window);
443 ddraw->d3d_window = 0;
447 ddraw->d3d_initialized = FALSE;
448 ddraw->d3d_target = NULL;
450 else
452 wined3d_device_uninit_gdi(ddraw->wined3d_device);
455 ddraw_set_swapchain_window(ddraw, NULL);
457 TRACE("Swapchain destroyed.\n");
460 /*****************************************************************************
461 * ddraw_destroy
463 * Destroys a ddraw object if all refcounts are 0. This is to share code
464 * between the IDirectDrawX::Release functions
466 * Params:
467 * This: DirectDraw object to destroy
469 *****************************************************************************/
470 static void ddraw_destroy(IDirectDrawImpl *This)
472 IDirectDraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, NULL, DDSCL_NORMAL);
473 IDirectDraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
475 /* Destroy the device window if we created one */
476 if(This->devicewindow != 0)
478 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
479 DestroyWindow(This->devicewindow);
480 This->devicewindow = 0;
483 wined3d_mutex_lock();
484 list_remove(&This->ddraw_list_entry);
485 wined3d_mutex_unlock();
487 /* This can happen more or less legitimately for ddraw 1 and 2, where
488 * surfaces don't keep a reference to the ddraw object. The surfaces
489 * will of course be broken after this, (and on native trying to do
490 * anything with them in that state results in an access violation), but
491 * the release of the ddraw object should succeed without crashing. */
492 if (This->wined3d_swapchain)
494 WARN("DirectDraw object is being destroyed while the swapchain still exists.\n");
495 ddraw_destroy_swapchain(This);
497 wined3d_device_decref(This->wined3d_device);
498 wined3d_decref(This->wineD3D);
500 /* Now free the object */
501 HeapFree(GetProcessHeap(), 0, This);
504 /*****************************************************************************
505 * IDirectDraw7::Release
507 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
509 * Returns: The new refcount
510 *****************************************************************************/
511 static ULONG WINAPI ddraw7_Release(IDirectDraw7 *iface)
513 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
514 ULONG ref = InterlockedDecrement(&This->ref7);
516 TRACE("%p decreasing refcount to %u.\n", This, ref);
518 if (!ref && !InterlockedDecrement(&This->numIfaces))
519 ddraw_destroy(This);
521 return ref;
524 static ULONG WINAPI ddraw4_Release(IDirectDraw4 *iface)
526 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
527 ULONG ref = InterlockedDecrement(&This->ref4);
529 TRACE("%p decreasing refcount to %u.\n", This, ref);
531 if (!ref && !InterlockedDecrement(&This->numIfaces))
532 ddraw_destroy(This);
534 return ref;
537 static ULONG WINAPI ddraw2_Release(IDirectDraw2 *iface)
539 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
540 ULONG ref = InterlockedDecrement(&This->ref2);
542 TRACE("%p decreasing refcount to %u.\n", This, ref);
544 if (!ref && !InterlockedDecrement(&This->numIfaces))
545 ddraw_destroy(This);
547 return ref;
550 static ULONG WINAPI ddraw1_Release(IDirectDraw *iface)
552 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
553 ULONG ref = InterlockedDecrement(&This->ref1);
555 TRACE("%p decreasing refcount to %u.\n", This, ref);
557 if (!ref && !InterlockedDecrement(&This->numIfaces))
558 ddraw_destroy(This);
560 return ref;
563 static ULONG WINAPI d3d7_Release(IDirect3D7 *iface)
565 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
567 TRACE("iface %p.\n", iface);
569 return ddraw7_Release(&This->IDirectDraw7_iface);
572 static ULONG WINAPI d3d3_Release(IDirect3D3 *iface)
574 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
576 TRACE("iface %p.\n", iface);
578 return ddraw1_Release(&This->IDirectDraw_iface);
581 static ULONG WINAPI d3d2_Release(IDirect3D2 *iface)
583 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
585 TRACE("iface %p.\n", iface);
587 return ddraw1_Release(&This->IDirectDraw_iface);
590 static ULONG WINAPI d3d1_Release(IDirect3D *iface)
592 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
594 TRACE("iface %p.\n", iface);
596 return ddraw1_Release(&This->IDirectDraw_iface);
599 /*****************************************************************************
600 * IDirectDraw methods
601 *****************************************************************************/
603 static HRESULT ddraw_set_focus_window(IDirectDrawImpl *ddraw, HWND window)
605 /* FIXME: This looks wrong, exclusive mode should imply a destination
606 * window. */
607 if ((ddraw->cooperative_level & DDSCL_EXCLUSIVE) && ddraw->dest_window)
609 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET.\n");
610 return DDERR_HWNDALREADYSET;
613 ddraw->focuswindow = window;
615 /* Use the focus window for drawing too. */
616 ddraw->dest_window = ddraw->focuswindow;
618 /* Destroy the device window, if we have one. */
619 if (ddraw->devicewindow)
621 DestroyWindow(ddraw->devicewindow);
622 ddraw->devicewindow = NULL;
625 return DD_OK;
628 /*****************************************************************************
629 * IDirectDraw7::SetCooperativeLevel
631 * Sets the cooperative level for the DirectDraw object, and the window
632 * assigned to it. The cooperative level determines the general behavior
633 * of the DirectDraw application
635 * Warning: This is quite tricky, as it's not really documented which
636 * cooperative levels can be combined with each other. If a game fails
637 * after this function, try to check the cooperative levels passed on
638 * Windows, and if it returns something different.
640 * If you think that this function caused the failure because it writes a
641 * fixme, be sure to run again with a +ddraw trace.
643 * What is known about cooperative levels (See the ddraw modes test):
644 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
645 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
646 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
647 * DDSCL_FULLSCREEN can be activated
648 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
650 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
651 * DDSCL_SETFOCUSWINDOW (partially),
652 * DDSCL_MULTITHREADED (work in progress)
654 * Unhandled flags, which should be implemented
655 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
656 * expect any difference to a normal window for wine)
657 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
658 * rendering (Possible test case: Half-Life)
660 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
662 * These don't seem very important for wine:
663 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
665 * Returns:
666 * DD_OK if the cooperative level was set successfully
667 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
668 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
669 * (Probably others too, have to investigate)
671 *****************************************************************************/
672 static HRESULT WINAPI ddraw7_SetCooperativeLevel(IDirectDraw7 *iface, HWND hwnd, DWORD cooplevel)
674 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
675 HWND window;
676 HRESULT hr;
678 TRACE("iface %p, window %p, flags %#x.\n", iface, hwnd, cooplevel);
679 DDRAW_dump_cooperativelevel(cooplevel);
681 wined3d_mutex_lock();
683 /* Get the old window */
684 window = This->dest_window;
686 /* Tests suggest that we need one of them: */
687 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
688 DDSCL_NORMAL |
689 DDSCL_EXCLUSIVE )))
691 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
692 wined3d_mutex_unlock();
693 return DDERR_INVALIDPARAMS;
696 /* Handle those levels first which set various hwnds */
697 if(cooplevel & DDSCL_SETFOCUSWINDOW)
699 /* This isn't compatible with a lot of flags */
700 if(cooplevel & ( DDSCL_MULTITHREADED |
701 DDSCL_CREATEDEVICEWINDOW |
702 DDSCL_FPUSETUP |
703 DDSCL_FPUPRESERVE |
704 DDSCL_ALLOWREBOOT |
705 DDSCL_ALLOWMODEX |
706 DDSCL_SETDEVICEWINDOW |
707 DDSCL_NORMAL |
708 DDSCL_EXCLUSIVE |
709 DDSCL_FULLSCREEN ) )
711 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
712 wined3d_mutex_unlock();
713 return DDERR_INVALIDPARAMS;
716 hr = ddraw_set_focus_window(This, hwnd);
717 wined3d_mutex_unlock();
718 return hr;
721 if(cooplevel & DDSCL_EXCLUSIVE)
723 if( !(cooplevel & DDSCL_FULLSCREEN) || !hwnd )
725 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN and a window\n", This);
726 wined3d_mutex_unlock();
727 return DDERR_INVALIDPARAMS;
731 if ((This->cooperative_level & DDSCL_EXCLUSIVE)
732 && (hwnd != window || !(cooplevel & DDSCL_EXCLUSIVE)))
733 wined3d_device_release_focus_window(This->wined3d_device);
735 if ((cooplevel & DDSCL_FULLSCREEN) != (This->cooperative_level & DDSCL_FULLSCREEN) || hwnd != window)
737 if (This->cooperative_level & DDSCL_FULLSCREEN)
738 wined3d_device_restore_fullscreen_window(This->wined3d_device, window);
740 if (cooplevel & DDSCL_FULLSCREEN)
742 struct wined3d_display_mode display_mode;
744 wined3d_get_adapter_display_mode(This->wineD3D, WINED3DADAPTER_DEFAULT, &display_mode);
745 wined3d_device_setup_fullscreen_window(This->wined3d_device, hwnd,
746 display_mode.width, display_mode.height);
750 if ((cooplevel & DDSCL_EXCLUSIVE)
751 && (hwnd != window || !(This->cooperative_level & DDSCL_EXCLUSIVE)))
753 hr = wined3d_device_acquire_focus_window(This->wined3d_device, hwnd);
754 if (FAILED(hr))
756 ERR("Failed to acquire focus window, hr %#x.\n", hr);
757 wined3d_mutex_unlock();
758 return hr;
762 /* Don't override focus windows or private device windows */
763 if (hwnd && !This->focuswindow && !This->devicewindow && (hwnd != window))
764 This->dest_window = hwnd;
766 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
768 /* Don't create a device window if a focus window is set */
769 if( !(This->focuswindow) )
771 HWND devicewindow = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "DDraw device window",
772 WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
773 NULL, NULL, NULL, NULL);
774 if (!devicewindow)
776 ERR("Failed to create window, last error %#x.\n", GetLastError());
777 wined3d_mutex_unlock();
778 return E_FAIL;
781 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
782 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
784 This->devicewindow = devicewindow;
785 This->dest_window = devicewindow;
789 if (cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
790 wined3d_device_set_multithreaded(This->wined3d_device);
792 /* Unhandled flags */
793 if(cooplevel & DDSCL_ALLOWREBOOT)
794 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
795 if(cooplevel & DDSCL_ALLOWMODEX)
796 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
797 if(cooplevel & DDSCL_FPUSETUP)
798 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
800 /* Store the cooperative_level */
801 This->cooperative_level = cooplevel;
802 TRACE("SetCooperativeLevel retuning DD_OK\n");
803 wined3d_mutex_unlock();
805 return DD_OK;
808 static HRESULT WINAPI ddraw4_SetCooperativeLevel(IDirectDraw4 *iface, HWND window, DWORD flags)
810 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
812 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
814 return ddraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, window, flags);
817 static HRESULT WINAPI ddraw2_SetCooperativeLevel(IDirectDraw2 *iface, HWND window, DWORD flags)
819 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
821 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
823 return ddraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, window, flags);
826 static HRESULT WINAPI ddraw1_SetCooperativeLevel(IDirectDraw *iface, HWND window, DWORD flags)
828 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
830 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
832 return ddraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, window, flags);
835 /*****************************************************************************
837 * Helper function for SetDisplayMode and RestoreDisplayMode
839 * Implements DirectDraw's SetDisplayMode, but ignores the value of
840 * ForceRefreshRate, since it is already handled by
841 * ddraw7_SetDisplayMode. RestoreDisplayMode can use this function
842 * without worrying that ForceRefreshRate will override the refresh rate. For
843 * argument and return value documentation, see
844 * ddraw7_SetDisplayMode.
846 *****************************************************************************/
847 static HRESULT ddraw_set_display_mode(IDirectDrawImpl *ddraw, DWORD Width, DWORD Height,
848 DWORD BPP, DWORD RefreshRate, DWORD Flags)
850 struct wined3d_display_mode mode;
851 enum wined3d_format_id format;
852 HRESULT hr;
854 TRACE("ddraw %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n", ddraw, Width,
855 Height, BPP, RefreshRate, Flags);
857 wined3d_mutex_lock();
858 if( !Width || !Height )
860 ERR("Width %u, Height %u, what to do?\n", Width, Height);
861 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
862 wined3d_mutex_unlock();
863 return DD_OK;
866 switch(BPP)
868 case 8: format = WINED3DFMT_P8_UINT; break;
869 case 15: format = WINED3DFMT_B5G5R5X1_UNORM; break;
870 case 16: format = WINED3DFMT_B5G6R5_UNORM; break;
871 case 24: format = WINED3DFMT_B8G8R8_UNORM; break;
872 case 32: format = WINED3DFMT_B8G8R8X8_UNORM; break;
873 default: format = WINED3DFMT_UNKNOWN; break;
876 if (FAILED(hr = wined3d_device_get_display_mode(ddraw->wined3d_device, 0, &mode)))
878 ERR("Failed to get current display mode, hr %#x.\n", hr);
880 else if (mode.width == Width
881 && mode.height == Height
882 && mode.format_id == format
883 && mode.refresh_rate == RefreshRate)
885 TRACE("Skipping redundant mode setting call.\n");
886 wined3d_mutex_unlock();
887 return DD_OK;
890 /* Check the exclusive mode
891 if(!(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
892 return DDERR_NOEXCLUSIVEMODE;
893 * This is WRONG. Don't know if the SDK is completely
894 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
895 * is returned, but Half-Life 1.1.1.1 (Steam version)
896 * depends on this
899 mode.width = Width;
900 mode.height = Height;
901 mode.refresh_rate = RefreshRate;
902 mode.format_id = format;
904 /* TODO: The possible return values from msdn suggest that
905 * the screen mode can't be changed if a surface is locked
906 * or some drawing is in progress
909 /* TODO: Lose the primary surface */
910 hr = wined3d_device_set_display_mode(ddraw->wined3d_device, 0, &mode);
912 if (ddraw->cooperative_level & DDSCL_EXCLUSIVE)
913 SetWindowPos(ddraw->dest_window, HWND_TOP, 0, 0, Width, Height, SWP_SHOWWINDOW | SWP_NOACTIVATE);
915 wined3d_mutex_unlock();
917 switch(hr)
919 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
920 default: return hr;
924 /*****************************************************************************
925 * IDirectDraw7::SetDisplayMode
927 * Sets the display screen resolution, color depth and refresh frequency
928 * when in fullscreen mode (in theory).
929 * Possible return values listed in the SDK suggest that this method fails
930 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
931 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
932 * It seems to be valid to pass 0 for With and Height, this has to be tested
933 * It could mean that the current video mode should be left as-is. (But why
934 * call it then?)
936 * Params:
937 * Height, Width: Screen dimension
938 * BPP: Color depth in Bits per pixel
939 * Refreshrate: Screen refresh rate
940 * Flags: Other stuff
942 * Returns
943 * DD_OK on success
945 *****************************************************************************/
946 static HRESULT WINAPI ddraw7_SetDisplayMode(IDirectDraw7 *iface, DWORD Width, DWORD Height,
947 DWORD BPP, DWORD RefreshRate, DWORD Flags)
949 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
951 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
952 iface, Width, Height, BPP, RefreshRate, Flags);
954 if (force_refresh_rate != 0)
956 TRACE("ForceRefreshRate overriding passed-in refresh rate (%u Hz) to %u Hz\n",
957 RefreshRate, force_refresh_rate);
958 RefreshRate = force_refresh_rate;
961 return ddraw_set_display_mode(This, Width, Height, BPP, RefreshRate, Flags);
964 static HRESULT WINAPI ddraw4_SetDisplayMode(IDirectDraw4 *iface, DWORD width, DWORD height,
965 DWORD bpp, DWORD refresh_rate, DWORD flags)
967 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
969 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
970 iface, width, height, bpp, refresh_rate, flags);
972 return ddraw7_SetDisplayMode(&This->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
975 static HRESULT WINAPI ddraw2_SetDisplayMode(IDirectDraw2 *iface,
976 DWORD width, DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
978 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
980 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
981 iface, width, height, bpp, refresh_rate, flags);
983 return ddraw7_SetDisplayMode(&This->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
986 static HRESULT WINAPI ddraw1_SetDisplayMode(IDirectDraw *iface, DWORD width, DWORD height, DWORD bpp)
988 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
990 TRACE("iface %p, width %u, height %u, bpp %u.\n", iface, width, height, bpp);
992 return ddraw7_SetDisplayMode(&This->IDirectDraw7_iface, width, height, bpp, 0, 0);
995 /*****************************************************************************
996 * IDirectDraw7::RestoreDisplayMode
998 * Restores the display mode to what it was at creation time. Basically.
1000 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
1001 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
1002 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
1003 * -> DD_1 is released. The screen should be left at 1024x768x32.
1004 * -> DD_2 is released. The screen should be set to 1400x1050x32
1005 * This case is unhandled right now, but Empire Earth does it this way.
1006 * (But perhaps there is something in SetCooperativeLevel to prevent this)
1008 * The msdn says that this method resets the display mode to what it was before
1009 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
1011 * Returns
1012 * DD_OK on success
1013 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
1015 *****************************************************************************/
1016 static HRESULT WINAPI ddraw7_RestoreDisplayMode(IDirectDraw7 *iface)
1018 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1020 TRACE("iface %p.\n", iface);
1022 return ddraw_set_display_mode(This, This->orig_width, This->orig_height, This->orig_bpp, 0, 0);
1025 static HRESULT WINAPI ddraw4_RestoreDisplayMode(IDirectDraw4 *iface)
1027 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1029 TRACE("iface %p.\n", iface);
1031 return ddraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
1034 static HRESULT WINAPI ddraw2_RestoreDisplayMode(IDirectDraw2 *iface)
1036 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1038 TRACE("iface %p.\n", iface);
1040 return ddraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
1043 static HRESULT WINAPI ddraw1_RestoreDisplayMode(IDirectDraw *iface)
1045 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1047 TRACE("iface %p.\n", iface);
1049 return ddraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
1052 /*****************************************************************************
1053 * IDirectDraw7::GetCaps
1055 * Returns the drives capabilities
1057 * Used for version 1, 2, 4 and 7
1059 * Params:
1060 * DriverCaps: Structure to write the Hardware accelerated caps to
1061 * HelCaps: Structure to write the emulation caps to
1063 * Returns
1064 * This implementation returns DD_OK only
1066 *****************************************************************************/
1067 static HRESULT WINAPI ddraw7_GetCaps(IDirectDraw7 *iface, DDCAPS *DriverCaps, DDCAPS *HELCaps)
1069 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1070 DDCAPS caps;
1071 WINED3DCAPS winecaps;
1072 HRESULT hr;
1073 DDSCAPS2 ddscaps = {0, 0, 0, 0};
1075 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, DriverCaps, HELCaps);
1077 /* One structure must be != NULL */
1078 if( (!DriverCaps) && (!HELCaps) )
1080 ERR("(%p) Invalid params to ddraw7_GetCaps\n", This);
1081 return DDERR_INVALIDPARAMS;
1084 memset(&caps, 0, sizeof(caps));
1085 memset(&winecaps, 0, sizeof(winecaps));
1086 caps.dwSize = sizeof(caps);
1088 wined3d_mutex_lock();
1089 hr = wined3d_device_get_device_caps(This->wined3d_device, &winecaps);
1090 if (FAILED(hr))
1092 WARN("IWineD3DDevice::GetDeviceCaps failed\n");
1093 wined3d_mutex_unlock();
1094 return hr;
1097 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
1098 wined3d_mutex_unlock();
1099 if(FAILED(hr)) {
1100 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
1101 return hr;
1104 caps.dwCaps = winecaps.DirectDrawCaps.Caps;
1105 caps.dwCaps2 = winecaps.DirectDrawCaps.Caps2;
1106 caps.dwCKeyCaps = winecaps.DirectDrawCaps.CKeyCaps;
1107 caps.dwFXCaps = winecaps.DirectDrawCaps.FXCaps;
1108 caps.dwPalCaps = winecaps.DirectDrawCaps.PalCaps;
1109 caps.ddsCaps.dwCaps = winecaps.DirectDrawCaps.ddsCaps;
1110 caps.dwSVBCaps = winecaps.DirectDrawCaps.SVBCaps;
1111 caps.dwSVBCKeyCaps = winecaps.DirectDrawCaps.SVBCKeyCaps;
1112 caps.dwSVBFXCaps = winecaps.DirectDrawCaps.SVBFXCaps;
1113 caps.dwVSBCaps = winecaps.DirectDrawCaps.VSBCaps;
1114 caps.dwVSBCKeyCaps = winecaps.DirectDrawCaps.VSBCKeyCaps;
1115 caps.dwVSBFXCaps = winecaps.DirectDrawCaps.VSBFXCaps;
1116 caps.dwSSBCaps = winecaps.DirectDrawCaps.SSBCaps;
1117 caps.dwSSBCKeyCaps = winecaps.DirectDrawCaps.SSBCKeyCaps;
1118 caps.dwSSBFXCaps = winecaps.DirectDrawCaps.SSBFXCaps;
1120 /* Even if WineD3D supports 3D rendering, remove the cap if ddraw is configured
1121 * not to use it
1123 if(DefaultSurfaceType == SURFACE_GDI) {
1124 caps.dwCaps &= ~DDCAPS_3D;
1125 caps.ddsCaps.dwCaps &= ~(DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER);
1127 if(winecaps.DirectDrawCaps.StrideAlign != 0) {
1128 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
1129 caps.dwAlignStrideAlign = winecaps.DirectDrawCaps.StrideAlign;
1132 if(DriverCaps)
1134 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
1135 if (TRACE_ON(ddraw))
1137 TRACE("Driver Caps :\n");
1138 DDRAW_dump_DDCAPS(DriverCaps);
1142 if(HELCaps)
1144 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
1145 if (TRACE_ON(ddraw))
1147 TRACE("HEL Caps :\n");
1148 DDRAW_dump_DDCAPS(HELCaps);
1152 return DD_OK;
1155 static HRESULT WINAPI ddraw4_GetCaps(IDirectDraw4 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1157 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1159 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1161 return ddraw7_GetCaps(&This->IDirectDraw7_iface, driver_caps, hel_caps);
1164 static HRESULT WINAPI ddraw2_GetCaps(IDirectDraw2 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1166 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1168 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1170 return ddraw7_GetCaps(&This->IDirectDraw7_iface, driver_caps, hel_caps);
1173 static HRESULT WINAPI ddraw1_GetCaps(IDirectDraw *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1175 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1177 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1179 return ddraw7_GetCaps(&This->IDirectDraw7_iface, driver_caps, hel_caps);
1182 /*****************************************************************************
1183 * IDirectDraw7::Compact
1185 * No idea what it does, MSDN says it's not implemented.
1187 * Returns
1188 * DD_OK, but this is unchecked
1190 *****************************************************************************/
1191 static HRESULT WINAPI ddraw7_Compact(IDirectDraw7 *iface)
1193 TRACE("iface %p.\n", iface);
1195 return DD_OK;
1198 static HRESULT WINAPI ddraw4_Compact(IDirectDraw4 *iface)
1200 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1202 TRACE("iface %p.\n", iface);
1204 return ddraw7_Compact(&This->IDirectDraw7_iface);
1207 static HRESULT WINAPI ddraw2_Compact(IDirectDraw2 *iface)
1209 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1211 TRACE("iface %p.\n", iface);
1213 return ddraw7_Compact(&This->IDirectDraw7_iface);
1216 static HRESULT WINAPI ddraw1_Compact(IDirectDraw *iface)
1218 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1220 TRACE("iface %p.\n", iface);
1222 return ddraw7_Compact(&This->IDirectDraw7_iface);
1225 /*****************************************************************************
1226 * IDirectDraw7::GetDisplayMode
1228 * Returns information about the current display mode
1230 * Exists in Version 1, 2, 4 and 7
1232 * Params:
1233 * DDSD: Address of a surface description structure to write the info to
1235 * Returns
1236 * DD_OK
1238 *****************************************************************************/
1239 static HRESULT WINAPI ddraw7_GetDisplayMode(IDirectDraw7 *iface, DDSURFACEDESC2 *DDSD)
1241 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1242 struct wined3d_display_mode mode;
1243 HRESULT hr;
1244 DWORD Size;
1246 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
1248 wined3d_mutex_lock();
1249 /* This seems sane */
1250 if (!DDSD)
1252 wined3d_mutex_unlock();
1253 return DDERR_INVALIDPARAMS;
1256 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
1257 * so one method can be used for all versions (Hopefully) */
1258 hr = wined3d_device_get_display_mode(This->wined3d_device, 0, &mode);
1259 if (FAILED(hr))
1261 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
1262 wined3d_mutex_unlock();
1263 return hr;
1266 Size = DDSD->dwSize;
1267 memset(DDSD, 0, Size);
1269 DDSD->dwSize = Size;
1270 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
1271 DDSD->dwWidth = mode.width;
1272 DDSD->dwHeight = mode.height;
1273 DDSD->u2.dwRefreshRate = 60;
1274 DDSD->ddsCaps.dwCaps = 0;
1275 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
1276 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, mode.format_id);
1277 DDSD->u1.lPitch = mode.width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
1279 if(TRACE_ON(ddraw))
1281 TRACE("Returning surface desc :\n");
1282 DDRAW_dump_surface_desc(DDSD);
1285 wined3d_mutex_unlock();
1287 return DD_OK;
1290 static HRESULT WINAPI ddraw4_GetDisplayMode(IDirectDraw4 *iface, DDSURFACEDESC2 *surface_desc)
1292 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1294 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1296 return ddraw7_GetDisplayMode(&This->IDirectDraw7_iface, surface_desc);
1299 static HRESULT WINAPI ddraw2_GetDisplayMode(IDirectDraw2 *iface, DDSURFACEDESC *surface_desc)
1301 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1303 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1305 /* FIXME: Test sizes, properly convert surface_desc */
1306 return ddraw7_GetDisplayMode(&This->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1309 static HRESULT WINAPI ddraw1_GetDisplayMode(IDirectDraw *iface, DDSURFACEDESC *surface_desc)
1311 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1313 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1315 /* FIXME: Test sizes, properly convert surface_desc */
1316 return ddraw7_GetDisplayMode(&This->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1319 /*****************************************************************************
1320 * IDirectDraw7::GetFourCCCodes
1322 * Returns an array of supported FourCC codes.
1324 * Exists in Version 1, 2, 4 and 7
1326 * Params:
1327 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
1328 * of enumerated codes
1329 * Codes: Pointer to an array of DWORDs where the supported codes are written
1330 * to
1332 * Returns
1333 * Always returns DD_OK, as it's a stub for now
1335 *****************************************************************************/
1336 static HRESULT WINAPI ddraw7_GetFourCCCodes(IDirectDraw7 *iface, DWORD *NumCodes, DWORD *Codes)
1338 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1339 static const enum wined3d_format_id formats[] =
1341 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
1342 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
1343 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
1345 struct wined3d_display_mode mode;
1346 DWORD count = 0, i, outsize;
1347 HRESULT hr;
1349 TRACE("iface %p, codes_count %p, codes %p.\n", iface, NumCodes, Codes);
1351 wined3d_device_get_display_mode(This->wined3d_device, 0, &mode);
1353 outsize = NumCodes && Codes ? *NumCodes : 0;
1355 for (i = 0; i < (sizeof(formats) / sizeof(formats[0])); ++i)
1357 hr = wined3d_check_device_format(This->wineD3D, WINED3DADAPTER_DEFAULT, WINED3DDEVTYPE_HAL,
1358 mode.format_id, 0, WINED3DRTYPE_SURFACE, formats[i], DefaultSurfaceType);
1359 if (SUCCEEDED(hr))
1361 if (count < outsize)
1362 Codes[count] = formats[i];
1363 ++count;
1366 if(NumCodes) {
1367 TRACE("Returning %u FourCC codes\n", count);
1368 *NumCodes = count;
1371 return DD_OK;
1374 static HRESULT WINAPI ddraw4_GetFourCCCodes(IDirectDraw4 *iface, DWORD *codes_count, DWORD *codes)
1376 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1378 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1380 return ddraw7_GetFourCCCodes(&This->IDirectDraw7_iface, codes_count, codes);
1383 static HRESULT WINAPI ddraw2_GetFourCCCodes(IDirectDraw2 *iface, DWORD *codes_count, DWORD *codes)
1385 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1387 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1389 return ddraw7_GetFourCCCodes(&This->IDirectDraw7_iface, codes_count, codes);
1392 static HRESULT WINAPI ddraw1_GetFourCCCodes(IDirectDraw *iface, DWORD *codes_count, DWORD *codes)
1394 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1396 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1398 return ddraw7_GetFourCCCodes(&This->IDirectDraw7_iface, codes_count, codes);
1401 /*****************************************************************************
1402 * IDirectDraw7::GetMonitorFrequency
1404 * Returns the monitor's frequency
1406 * Exists in Version 1, 2, 4 and 7
1408 * Params:
1409 * Freq: Pointer to a DWORD to write the frequency to
1411 * Returns
1412 * Always returns DD_OK
1414 *****************************************************************************/
1415 static HRESULT WINAPI ddraw7_GetMonitorFrequency(IDirectDraw7 *iface, DWORD *Freq)
1417 FIXME("iface %p, frequency %p stub!\n", iface, Freq);
1419 /* Ideally this should be in WineD3D, as it concerns the screen setup,
1420 * but for now this should make the games happy
1422 *Freq = 60;
1423 return DD_OK;
1426 static HRESULT WINAPI ddraw4_GetMonitorFrequency(IDirectDraw4 *iface, DWORD *frequency)
1428 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1430 TRACE("iface %p, frequency %p.\n", iface, frequency);
1432 return ddraw7_GetMonitorFrequency(&This->IDirectDraw7_iface, frequency);
1435 static HRESULT WINAPI ddraw2_GetMonitorFrequency(IDirectDraw2 *iface, DWORD *frequency)
1437 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1439 TRACE("iface %p, frequency %p.\n", iface, frequency);
1441 return ddraw7_GetMonitorFrequency(&This->IDirectDraw7_iface, frequency);
1444 static HRESULT WINAPI ddraw1_GetMonitorFrequency(IDirectDraw *iface, DWORD *frequency)
1446 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1448 TRACE("iface %p, frequency %p.\n", iface, frequency);
1450 return ddraw7_GetMonitorFrequency(&This->IDirectDraw7_iface, frequency);
1453 /*****************************************************************************
1454 * IDirectDraw7::GetVerticalBlankStatus
1456 * Returns the Vertical blank status of the monitor. This should be in WineD3D
1457 * too basically, but as it's a semi stub, I didn't create a function there
1459 * Params:
1460 * status: Pointer to a BOOL to be filled with the vertical blank status
1462 * Returns
1463 * DD_OK on success
1464 * DDERR_INVALIDPARAMS if status is NULL
1466 *****************************************************************************/
1467 static HRESULT WINAPI ddraw7_GetVerticalBlankStatus(IDirectDraw7 *iface, BOOL *status)
1469 static BOOL fake_vblank;
1471 TRACE("iface %p, status %p.\n", iface, status);
1473 if(!status)
1474 return DDERR_INVALIDPARAMS;
1476 *status = fake_vblank;
1477 fake_vblank = !fake_vblank;
1479 return DD_OK;
1482 static HRESULT WINAPI ddraw4_GetVerticalBlankStatus(IDirectDraw4 *iface, BOOL *status)
1484 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1486 TRACE("iface %p, status %p.\n", iface, status);
1488 return ddraw7_GetVerticalBlankStatus(&This->IDirectDraw7_iface, status);
1491 static HRESULT WINAPI ddraw2_GetVerticalBlankStatus(IDirectDraw2 *iface, BOOL *status)
1493 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1495 TRACE("iface %p, status %p.\n", iface, status);
1497 return ddraw7_GetVerticalBlankStatus(&This->IDirectDraw7_iface, status);
1500 static HRESULT WINAPI ddraw1_GetVerticalBlankStatus(IDirectDraw *iface, BOOL *status)
1502 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1504 TRACE("iface %p, status %p.\n", iface, status);
1506 return ddraw7_GetVerticalBlankStatus(&This->IDirectDraw7_iface, status);
1509 /*****************************************************************************
1510 * IDirectDraw7::GetAvailableVidMem
1512 * Returns the total and free video memory
1514 * Params:
1515 * Caps: Specifies the memory type asked for
1516 * total: Pointer to a DWORD to be filled with the total memory
1517 * free: Pointer to a DWORD to be filled with the free memory
1519 * Returns
1520 * DD_OK on success
1521 * DDERR_INVALIDPARAMS of free and total are NULL
1523 *****************************************************************************/
1524 static HRESULT WINAPI ddraw7_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total,
1525 DWORD *free)
1527 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1528 HRESULT hr = DD_OK;
1530 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, Caps, total, free);
1532 if(TRACE_ON(ddraw))
1534 TRACE("(%p) Asked for memory with description: ", This);
1535 DDRAW_dump_DDSCAPS2(Caps);
1537 wined3d_mutex_lock();
1539 /* Todo: System memory vs local video memory vs non-local video memory
1540 * The MSDN also mentions differences between texture memory and other
1541 * resources, but that's not important
1544 if( (!total) && (!free) )
1546 wined3d_mutex_unlock();
1547 return DDERR_INVALIDPARAMS;
1550 if (free)
1551 *free = wined3d_device_get_available_texture_mem(This->wined3d_device);
1552 if (total)
1554 WINED3DADAPTER_IDENTIFIER desc = {0};
1556 hr = wined3d_get_adapter_identifier(This->wineD3D, WINED3DADAPTER_DEFAULT, 0, &desc);
1557 *total = desc.video_memory;
1560 wined3d_mutex_unlock();
1562 return hr;
1565 static HRESULT WINAPI ddraw4_GetAvailableVidMem(IDirectDraw4 *iface,
1566 DDSCAPS2 *caps, DWORD *total, DWORD *free)
1568 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1570 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1572 return ddraw7_GetAvailableVidMem(&This->IDirectDraw7_iface, caps, total, free);
1575 static HRESULT WINAPI ddraw2_GetAvailableVidMem(IDirectDraw2 *iface,
1576 DDSCAPS *caps, DWORD *total, DWORD *free)
1578 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1579 DDSCAPS2 caps2;
1581 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1583 DDRAW_Convert_DDSCAPS_1_To_2(caps, &caps2);
1584 return ddraw7_GetAvailableVidMem(&This->IDirectDraw7_iface, &caps2, total, free);
1587 /*****************************************************************************
1588 * IDirectDraw7::Initialize
1590 * Initializes a DirectDraw interface.
1592 * Params:
1593 * GUID: Interface identifier. Well, don't know what this is really good
1594 * for
1596 * Returns
1597 * Returns DD_OK on the first call,
1598 * DDERR_ALREADYINITIALIZED on repeated calls
1600 *****************************************************************************/
1601 static HRESULT WINAPI ddraw7_Initialize(IDirectDraw7 *iface, GUID *guid)
1603 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1605 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1607 if (This->initialized)
1608 return DDERR_ALREADYINITIALIZED;
1610 /* FIXME: To properly take the GUID into account we should call
1611 * ddraw_init() here instead of in DDRAW_Create(). */
1612 if (guid)
1613 FIXME("Ignoring guid %s.\n", debugstr_guid(guid));
1615 This->initialized = TRUE;
1616 return DD_OK;
1619 static HRESULT WINAPI ddraw4_Initialize(IDirectDraw4 *iface, GUID *guid)
1621 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1623 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1625 return ddraw7_Initialize(&This->IDirectDraw7_iface, guid);
1628 static HRESULT WINAPI ddraw2_Initialize(IDirectDraw2 *iface, GUID *guid)
1630 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1632 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1634 return ddraw7_Initialize(&This->IDirectDraw7_iface, guid);
1637 static HRESULT WINAPI ddraw1_Initialize(IDirectDraw *iface, GUID *guid)
1639 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1641 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1643 return ddraw7_Initialize(&This->IDirectDraw7_iface, guid);
1646 static HRESULT WINAPI d3d1_Initialize(IDirect3D *iface, REFIID riid)
1648 TRACE("iface %p, riid %s.\n", iface, debugstr_guid(riid));
1650 return DDERR_ALREADYINITIALIZED;
1653 /*****************************************************************************
1654 * IDirectDraw7::FlipToGDISurface
1656 * "Makes the surface that the GDI writes to the primary surface"
1657 * Looks like some windows specific thing we don't have to care about.
1658 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1659 * show error boxes ;)
1660 * Well, just return DD_OK.
1662 * Returns:
1663 * Always returns DD_OK
1665 *****************************************************************************/
1666 static HRESULT WINAPI ddraw7_FlipToGDISurface(IDirectDraw7 *iface)
1668 FIXME("iface %p stub!\n", iface);
1670 return DD_OK;
1673 static HRESULT WINAPI ddraw4_FlipToGDISurface(IDirectDraw4 *iface)
1675 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1677 TRACE("iface %p.\n", iface);
1679 return ddraw7_FlipToGDISurface(&This->IDirectDraw7_iface);
1682 static HRESULT WINAPI ddraw2_FlipToGDISurface(IDirectDraw2 *iface)
1684 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1686 TRACE("iface %p.\n", iface);
1688 return ddraw7_FlipToGDISurface(&This->IDirectDraw7_iface);
1691 static HRESULT WINAPI ddraw1_FlipToGDISurface(IDirectDraw *iface)
1693 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1695 TRACE("iface %p.\n", iface);
1697 return ddraw7_FlipToGDISurface(&This->IDirectDraw7_iface);
1700 /*****************************************************************************
1701 * IDirectDraw7::WaitForVerticalBlank
1703 * This method allows applications to get in sync with the vertical blank
1704 * interval.
1705 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1706 * redraw the screen, most likely because of this stub
1708 * Parameters:
1709 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1710 * or DDWAITVB_BLOCKEND
1711 * h: Not used, according to MSDN
1713 * Returns:
1714 * Always returns DD_OK
1716 *****************************************************************************/
1717 static HRESULT WINAPI ddraw7_WaitForVerticalBlank(IDirectDraw7 *iface, DWORD Flags, HANDLE event)
1719 static BOOL hide;
1721 TRACE("iface %p, flags %#x, event %p.\n", iface, Flags, event);
1723 /* This function is called often, so print the fixme only once */
1724 if(!hide)
1726 FIXME("iface %p, flags %#x, event %p stub!\n", iface, Flags, event);
1727 hide = TRUE;
1730 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1731 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1732 return DDERR_UNSUPPORTED; /* unchecked */
1734 return DD_OK;
1737 static HRESULT WINAPI ddraw4_WaitForVerticalBlank(IDirectDraw4 *iface, DWORD flags, HANDLE event)
1739 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1741 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1743 return ddraw7_WaitForVerticalBlank(&This->IDirectDraw7_iface, flags, event);
1746 static HRESULT WINAPI ddraw2_WaitForVerticalBlank(IDirectDraw2 *iface, DWORD flags, HANDLE event)
1748 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1750 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1752 return ddraw7_WaitForVerticalBlank(&This->IDirectDraw7_iface, flags, event);
1755 static HRESULT WINAPI ddraw1_WaitForVerticalBlank(IDirectDraw *iface, DWORD flags, HANDLE event)
1757 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1759 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1761 return ddraw7_WaitForVerticalBlank(&This->IDirectDraw7_iface, flags, event);
1764 /*****************************************************************************
1765 * IDirectDraw7::GetScanLine
1767 * Returns the scan line that is being drawn on the monitor
1769 * Parameters:
1770 * Scanline: Address to write the scan line value to
1772 * Returns:
1773 * Always returns DD_OK
1775 *****************************************************************************/
1776 static HRESULT WINAPI ddraw7_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1778 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1779 struct wined3d_display_mode mode;
1780 static DWORD cur_scanline;
1781 static BOOL hide = FALSE;
1783 TRACE("iface %p, line %p.\n", iface, Scanline);
1785 /* This function is called often, so print the fixme only once */
1786 if(!hide)
1788 FIXME("iface %p, line %p partial stub!\n", iface, Scanline);
1789 hide = TRUE;
1792 wined3d_mutex_lock();
1793 wined3d_device_get_display_mode(This->wined3d_device, 0, &mode);
1794 wined3d_mutex_unlock();
1796 /* Fake the line sweeping of the monitor */
1797 /* FIXME: We should synchronize with a source to keep the refresh rate */
1798 *Scanline = cur_scanline++;
1799 /* Assume 20 scan lines in the vertical blank */
1800 if (cur_scanline >= mode.height + 20)
1801 cur_scanline = 0;
1803 return DD_OK;
1806 static HRESULT WINAPI ddraw4_GetScanLine(IDirectDraw4 *iface, DWORD *line)
1808 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1810 TRACE("iface %p, line %p.\n", iface, line);
1812 return ddraw7_GetScanLine(&This->IDirectDraw7_iface, line);
1815 static HRESULT WINAPI ddraw2_GetScanLine(IDirectDraw2 *iface, DWORD *line)
1817 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1819 TRACE("iface %p, line %p.\n", iface, line);
1821 return ddraw7_GetScanLine(&This->IDirectDraw7_iface, line);
1824 static HRESULT WINAPI ddraw1_GetScanLine(IDirectDraw *iface, DWORD *line)
1826 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1828 TRACE("iface %p, line %p.\n", iface, line);
1830 return ddraw7_GetScanLine(&This->IDirectDraw7_iface, line);
1833 /*****************************************************************************
1834 * IDirectDraw7::TestCooperativeLevel
1836 * Informs the application about the state of the video adapter, depending
1837 * on the cooperative level
1839 * Returns:
1840 * DD_OK if the device is in a sane state
1841 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1842 * if the state is not correct(See below)
1844 *****************************************************************************/
1845 static HRESULT WINAPI ddraw7_TestCooperativeLevel(IDirectDraw7 *iface)
1847 TRACE("iface %p.\n", iface);
1849 return DD_OK;
1852 static HRESULT WINAPI ddraw4_TestCooperativeLevel(IDirectDraw4 *iface)
1854 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1856 TRACE("iface %p.\n", iface);
1858 return ddraw7_TestCooperativeLevel(&This->IDirectDraw7_iface);
1861 /*****************************************************************************
1862 * IDirectDraw7::GetGDISurface
1864 * Returns the surface that GDI is treating as the primary surface.
1865 * For Wine this is the front buffer
1867 * Params:
1868 * GDISurface: Address to write the surface pointer to
1870 * Returns:
1871 * DD_OK if the surface was found
1872 * DDERR_NOTFOUND if the GDI surface wasn't found
1874 *****************************************************************************/
1875 static HRESULT WINAPI ddraw7_GetGDISurface(IDirectDraw7 *iface, IDirectDrawSurface7 **GDISurface)
1877 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1879 TRACE("iface %p, surface %p.\n", iface, GDISurface);
1881 wined3d_mutex_lock();
1883 if (!(*GDISurface = &This->primary->IDirectDrawSurface7_iface))
1885 WARN("Primary not created yet.\n");
1886 wined3d_mutex_unlock();
1887 return DDERR_NOTFOUND;
1889 IDirectDrawSurface7_AddRef(*GDISurface);
1891 wined3d_mutex_unlock();
1893 return DD_OK;
1896 static HRESULT WINAPI ddraw4_GetGDISurface(IDirectDraw4 *iface, IDirectDrawSurface4 **surface)
1898 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1899 IDirectDrawSurface7 *surface7;
1900 IDirectDrawSurfaceImpl *surface_impl;
1901 HRESULT hr;
1903 TRACE("iface %p, surface %p.\n", iface, surface);
1905 hr = ddraw7_GetGDISurface(&This->IDirectDraw7_iface, &surface7);
1906 if (FAILED(hr))
1908 *surface = NULL;
1909 return hr;
1911 surface_impl = impl_from_IDirectDrawSurface7(surface7);
1912 *surface = &surface_impl->IDirectDrawSurface4_iface;
1913 IDirectDrawSurface4_AddRef(*surface);
1914 IDirectDrawSurface7_Release(surface7);
1916 return hr;
1919 static HRESULT WINAPI ddraw2_GetGDISurface(IDirectDraw2 *iface, IDirectDrawSurface **surface)
1921 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1922 IDirectDrawSurface7 *surface7;
1923 IDirectDrawSurfaceImpl *surface_impl;
1924 HRESULT hr;
1926 TRACE("iface %p, surface %p.\n", iface, surface);
1928 hr = ddraw7_GetGDISurface(&This->IDirectDraw7_iface, &surface7);
1929 if (FAILED(hr))
1931 *surface = NULL;
1932 return hr;
1934 surface_impl = impl_from_IDirectDrawSurface7(surface7);
1935 *surface = &surface_impl->IDirectDrawSurface_iface;
1936 IDirectDrawSurface_AddRef(*surface);
1937 IDirectDrawSurface7_Release(surface7);
1939 return hr;
1942 static HRESULT WINAPI ddraw1_GetGDISurface(IDirectDraw *iface, IDirectDrawSurface **surface)
1944 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1945 IDirectDrawSurface7 *surface7;
1946 IDirectDrawSurfaceImpl *surface_impl;
1947 HRESULT hr;
1949 TRACE("iface %p, surface %p.\n", iface, surface);
1951 hr = ddraw7_GetGDISurface(&This->IDirectDraw7_iface, &surface7);
1952 if (FAILED(hr))
1954 *surface = NULL;
1955 return hr;
1957 surface_impl = impl_from_IDirectDrawSurface7(surface7);
1958 *surface = &surface_impl->IDirectDrawSurface_iface;
1959 IDirectDrawSurface_AddRef(*surface);
1960 IDirectDrawSurface7_Release(surface7);
1962 return hr;
1965 struct displaymodescallback_context
1967 LPDDENUMMODESCALLBACK func;
1968 void *context;
1971 static HRESULT CALLBACK EnumDisplayModesCallbackThunk(DDSURFACEDESC2 *surface_desc, void *context)
1973 struct displaymodescallback_context *cbcontext = context;
1974 DDSURFACEDESC desc;
1976 DDSD2_to_DDSD(surface_desc, &desc);
1977 return cbcontext->func(&desc, cbcontext->context);
1980 /*****************************************************************************
1981 * IDirectDraw7::EnumDisplayModes
1983 * Enumerates the supported Display modes. The modes can be filtered with
1984 * the DDSD parameter.
1986 * Params:
1987 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES. For old ddraw
1988 * versions (3 and older?) this is reserved and must be 0.
1989 * DDSD: Surface description to filter the modes
1990 * Context: Pointer passed back to the callback function
1991 * cb: Application-provided callback function
1993 * Returns:
1994 * DD_OK on success
1995 * DDERR_INVALIDPARAMS if the callback wasn't set
1997 *****************************************************************************/
1998 static HRESULT WINAPI ddraw7_EnumDisplayModes(IDirectDraw7 *iface, DWORD Flags,
1999 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMMODESCALLBACK2 cb)
2001 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
2002 struct wined3d_display_mode *enum_modes = NULL;
2003 struct wined3d_display_mode mode;
2004 unsigned int modenum, fmt;
2005 DDSURFACEDESC2 callback_sd;
2006 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
2007 DDPIXELFORMAT pixelformat;
2009 static const enum wined3d_format_id checkFormatList[] =
2011 WINED3DFMT_B8G8R8X8_UNORM,
2012 WINED3DFMT_B5G6R5_UNORM,
2013 WINED3DFMT_P8_UINT,
2016 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2017 iface, Flags, DDSD, Context, cb);
2019 if (!cb)
2020 return DDERR_INVALIDPARAMS;
2022 wined3d_mutex_lock();
2023 if(!(Flags & DDEDM_REFRESHRATES))
2025 enum_mode_array_size = 16;
2026 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(*enum_modes) * enum_mode_array_size);
2027 if (!enum_modes)
2029 ERR("Out of memory\n");
2030 wined3d_mutex_unlock();
2031 return DDERR_OUTOFMEMORY;
2035 pixelformat.dwSize = sizeof(pixelformat);
2036 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
2038 modenum = 0;
2039 while (wined3d_enum_adapter_modes(This->wineD3D, WINED3DADAPTER_DEFAULT,
2040 checkFormatList[fmt], modenum++, &mode) == WINED3D_OK)
2042 PixelFormat_WineD3DtoDD(&pixelformat, mode.format_id);
2043 if (DDSD)
2045 if (DDSD->dwFlags & DDSD_WIDTH && mode.width != DDSD->dwWidth)
2046 continue;
2047 if (DDSD->dwFlags & DDSD_HEIGHT && mode.height != DDSD->dwHeight)
2048 continue;
2049 if (DDSD->dwFlags & DDSD_REFRESHRATE && mode.refresh_rate != DDSD->u2.dwRefreshRate)
2050 continue;
2051 if (DDSD->dwFlags & DDSD_PIXELFORMAT
2052 && pixelformat.u1.dwRGBBitCount != DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount)
2053 continue;
2056 if(!(Flags & DDEDM_REFRESHRATES))
2058 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
2059 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
2060 * to be reduced to a single unique result in such case.
2062 BOOL found = FALSE;
2063 unsigned i;
2065 for (i = 0; i < enum_mode_count; i++)
2067 if (enum_modes[i].width == mode.width && enum_modes[i].height == mode.height
2068 && enum_modes[i].format_id == mode.format_id)
2070 found = TRUE;
2071 break;
2075 if(found) continue;
2078 memset(&callback_sd, 0, sizeof(callback_sd));
2079 callback_sd.dwSize = sizeof(callback_sd);
2080 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2082 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH|DDSD_REFRESHRATE;
2083 if (Flags & DDEDM_REFRESHRATES)
2084 callback_sd.u2.dwRefreshRate = mode.refresh_rate;
2086 callback_sd.dwWidth = mode.width;
2087 callback_sd.dwHeight = mode.height;
2089 callback_sd.u4.ddpfPixelFormat=pixelformat;
2091 /* Calc pitch and DWORD align like MSDN says */
2092 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.width;
2093 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
2095 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
2096 callback_sd.u2.dwRefreshRate);
2098 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
2100 TRACE("Application asked to terminate the enumeration\n");
2101 HeapFree(GetProcessHeap(), 0, enum_modes);
2102 wined3d_mutex_unlock();
2103 return DD_OK;
2106 if(!(Flags & DDEDM_REFRESHRATES))
2108 if (enum_mode_count == enum_mode_array_size)
2110 struct wined3d_display_mode *new_enum_modes;
2112 enum_mode_array_size *= 2;
2113 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes,
2114 sizeof(*new_enum_modes) * enum_mode_array_size);
2115 if (!new_enum_modes)
2117 ERR("Out of memory\n");
2118 HeapFree(GetProcessHeap(), 0, enum_modes);
2119 wined3d_mutex_unlock();
2120 return DDERR_OUTOFMEMORY;
2123 enum_modes = new_enum_modes;
2126 enum_modes[enum_mode_count++] = mode;
2131 TRACE("End of enumeration\n");
2132 HeapFree(GetProcessHeap(), 0, enum_modes);
2133 wined3d_mutex_unlock();
2135 return DD_OK;
2138 static HRESULT WINAPI ddraw4_EnumDisplayModes(IDirectDraw4 *iface, DWORD flags,
2139 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMMODESCALLBACK2 callback)
2141 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2143 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2144 iface, flags, surface_desc, context, callback);
2146 return ddraw7_EnumDisplayModes(&This->IDirectDraw7_iface, flags, surface_desc, context, callback);
2149 static HRESULT WINAPI ddraw2_EnumDisplayModes(IDirectDraw2 *iface, DWORD flags,
2150 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2152 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
2153 struct displaymodescallback_context cbcontext;
2154 DDSURFACEDESC2 surface_desc2;
2156 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2157 iface, flags, surface_desc, context, callback);
2159 cbcontext.func = callback;
2160 cbcontext.context = context;
2162 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2163 return ddraw7_EnumDisplayModes(&This->IDirectDraw7_iface, flags,
2164 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2167 static HRESULT WINAPI ddraw1_EnumDisplayModes(IDirectDraw *iface, DWORD flags,
2168 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2170 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
2171 struct displaymodescallback_context cbcontext;
2172 DDSURFACEDESC2 surface_desc2;
2174 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2175 iface, flags, surface_desc, context, callback);
2177 cbcontext.func = callback;
2178 cbcontext.context = context;
2180 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2181 return ddraw7_EnumDisplayModes(&This->IDirectDraw7_iface, flags,
2182 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2185 /*****************************************************************************
2186 * IDirectDraw7::EvaluateMode
2188 * Used with IDirectDraw7::StartModeTest to test video modes.
2189 * EvaluateMode is used to pass or fail a mode, and continue with the next
2190 * mode
2192 * Params:
2193 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
2194 * Timeout: Returns the amount of seconds left before the mode would have
2195 * been failed automatically
2197 * Returns:
2198 * This implementation always DD_OK, because it's a stub
2200 *****************************************************************************/
2201 static HRESULT WINAPI ddraw7_EvaluateMode(IDirectDraw7 *iface, DWORD Flags, DWORD *Timeout)
2203 FIXME("iface %p, flags %#x, timeout %p stub!\n", iface, Flags, Timeout);
2205 /* When implementing this, implement it in WineD3D */
2207 return DD_OK;
2210 /*****************************************************************************
2211 * IDirectDraw7::GetDeviceIdentifier
2213 * Returns the device identifier, which gives information about the driver
2214 * Our device identifier is defined at the beginning of this file.
2216 * Params:
2217 * DDDI: Address for the returned structure
2218 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
2220 * Returns:
2221 * On success it returns DD_OK
2222 * DDERR_INVALIDPARAMS if DDDI is NULL
2224 *****************************************************************************/
2225 static HRESULT WINAPI ddraw7_GetDeviceIdentifier(IDirectDraw7 *iface,
2226 DDDEVICEIDENTIFIER2 *DDDI, DWORD Flags)
2228 TRACE("iface %p, device_identifier %p, flags %#x.\n", iface, DDDI, Flags);
2230 if(!DDDI)
2231 return DDERR_INVALIDPARAMS;
2233 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
2234 * host adapter, if there's a secondary 3D adapter. This doesn't apply
2235 * to any modern hardware, nor is it interesting for Wine, so ignore it.
2236 * Size of DDDEVICEIDENTIFIER2 may be aligned to 8 bytes and thus 4
2237 * bytes too long. So only copy the relevant part of the structure
2240 memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
2241 return DD_OK;
2244 static HRESULT WINAPI ddraw4_GetDeviceIdentifier(IDirectDraw4 *iface,
2245 DDDEVICEIDENTIFIER *identifier, DWORD flags)
2247 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2248 DDDEVICEIDENTIFIER2 identifier2;
2249 HRESULT hr;
2251 TRACE("iface %p, identifier %p, flags %#x.\n", iface, identifier, flags);
2253 hr = ddraw7_GetDeviceIdentifier(&This->IDirectDraw7_iface, &identifier2, flags);
2254 DDRAW_Convert_DDDEVICEIDENTIFIER_2_To_1(&identifier2, identifier);
2256 return hr;
2259 /*****************************************************************************
2260 * IDirectDraw7::GetSurfaceFromDC
2262 * Returns the Surface for a GDI device context handle.
2263 * Is this related to IDirectDrawSurface::GetDC ???
2265 * Params:
2266 * hdc: hdc to return the surface for
2267 * Surface: Address to write the surface pointer to
2269 * Returns:
2270 * Always returns DD_OK because it's a stub
2272 *****************************************************************************/
2273 static HRESULT WINAPI ddraw7_GetSurfaceFromDC(IDirectDraw7 *iface, HDC hdc,
2274 IDirectDrawSurface7 **Surface)
2276 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
2277 struct wined3d_surface *wined3d_surface;
2278 HRESULT hr;
2280 TRACE("iface %p, dc %p, surface %p.\n", iface, hdc, Surface);
2282 if (!Surface) return E_INVALIDARG;
2284 hr = wined3d_device_get_surface_from_dc(This->wined3d_device, hdc, &wined3d_surface);
2285 if (FAILED(hr))
2287 TRACE("No surface found for dc %p.\n", hdc);
2288 *Surface = NULL;
2289 return DDERR_NOTFOUND;
2292 *Surface = wined3d_surface_get_parent(wined3d_surface);
2293 IDirectDrawSurface7_AddRef(*Surface);
2294 TRACE("Returning surface %p.\n", Surface);
2295 return DD_OK;
2298 static HRESULT WINAPI ddraw4_GetSurfaceFromDC(IDirectDraw4 *iface, HDC dc,
2299 IDirectDrawSurface4 **surface)
2301 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2302 IDirectDrawSurface7 *surface7;
2303 IDirectDrawSurfaceImpl *surface_impl;
2304 HRESULT hr;
2306 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2308 if (!surface) return E_INVALIDARG;
2310 hr = ddraw7_GetSurfaceFromDC(&This->IDirectDraw7_iface, dc, &surface7);
2311 if (FAILED(hr))
2313 *surface = NULL;
2314 return hr;
2316 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2317 /* Tests say this is true */
2318 *surface = (IDirectDrawSurface4 *)&surface_impl->IDirectDrawSurface_iface;
2319 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
2320 IDirectDrawSurface7_Release(surface7);
2322 return hr;
2325 /*****************************************************************************
2326 * IDirectDraw7::RestoreAllSurfaces
2328 * Calls the restore method of all surfaces
2330 * Params:
2332 * Returns:
2333 * Always returns DD_OK because it's a stub
2335 *****************************************************************************/
2336 static HRESULT WINAPI ddraw7_RestoreAllSurfaces(IDirectDraw7 *iface)
2338 FIXME("iface %p stub!\n", iface);
2340 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
2341 * get their parent and call their restore method. Do not implement
2342 * it in WineD3D, as restoring a surface means re-creating the
2343 * WineD3DDSurface
2345 return DD_OK;
2348 static HRESULT WINAPI ddraw4_RestoreAllSurfaces(IDirectDraw4 *iface)
2350 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2352 TRACE("iface %p.\n", iface);
2354 return ddraw7_RestoreAllSurfaces(&This->IDirectDraw7_iface);
2357 /*****************************************************************************
2358 * IDirectDraw7::StartModeTest
2360 * Tests the specified video modes to update the system registry with
2361 * refresh rate information. StartModeTest starts the mode test,
2362 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
2363 * isn't called within 15 seconds, the mode is failed automatically
2365 * As refresh rates are handled by the X server, I don't think this
2366 * Method is important
2368 * Params:
2369 * Modes: An array of mode specifications
2370 * NumModes: The number of modes in Modes
2371 * Flags: Some flags...
2373 * Returns:
2374 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
2375 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
2376 * otherwise DD_OK
2378 *****************************************************************************/
2379 static HRESULT WINAPI ddraw7_StartModeTest(IDirectDraw7 *iface, SIZE *Modes, DWORD NumModes, DWORD Flags)
2381 FIXME("iface %p, modes %p, mode_count %u, flags %#x partial stub!\n",
2382 iface, Modes, NumModes, Flags);
2384 /* This looks sane */
2385 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
2387 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
2388 * As it is not, DDERR_TESTFINISHED is returned
2389 * (hopefully that's correct
2391 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
2392 * well, that value doesn't (yet) exist in the wine headers, so ignore it
2395 return DD_OK;
2398 /*****************************************************************************
2399 * ddraw_create_surface
2401 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
2402 * with the passed parameters.
2404 * Params:
2405 * DDSD: Description of the surface to create
2406 * Surf: Address to store the interface pointer at
2408 * Returns:
2409 * DD_OK on success
2411 *****************************************************************************/
2412 static HRESULT ddraw_create_surface(IDirectDrawImpl *This, DDSURFACEDESC2 *pDDSD,
2413 IDirectDrawSurfaceImpl **ppSurf, UINT level, UINT version)
2415 HRESULT hr;
2417 TRACE("ddraw %p, surface_desc %p, surface %p, level %u.\n",
2418 This, pDDSD, ppSurf, level);
2420 if (TRACE_ON(ddraw))
2422 TRACE(" (%p) Requesting surface desc :\n", This);
2423 DDRAW_dump_surface_desc(pDDSD);
2426 if ((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && DefaultSurfaceType != SURFACE_OPENGL)
2428 WARN("The application requests a 3D capable surface, but a non-OpenGL surface type was set in the registry.\n");
2429 /* Do not fail surface creation, only fail 3D device creation. */
2432 /* Create the Surface object */
2433 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
2434 if(!*ppSurf)
2436 ERR("(%p) Error allocating memory for a surface\n", This);
2437 return DDERR_OUTOFVIDEOMEMORY;
2440 hr = ddraw_surface_init(*ppSurf, This, pDDSD, level, version);
2441 if (FAILED(hr))
2443 WARN("Failed to initialize surface, hr %#x.\n", hr);
2444 HeapFree(GetProcessHeap(), 0, *ppSurf);
2445 return hr;
2448 /* Increase the surface counter, and attach the surface */
2449 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
2451 TRACE("Created surface %p.\n", *ppSurf);
2453 return DD_OK;
2455 /*****************************************************************************
2456 * CreateAdditionalSurfaces
2458 * Creates a new mipmap chain.
2460 * Params:
2461 * root: Root surface to attach the newly created chain to
2462 * count: number of surfaces to create
2463 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2464 * effects on the caller
2465 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2466 * creates an additional surface without the mipmapping flags
2468 *****************************************************************************/
2469 static HRESULT
2470 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2471 IDirectDrawSurfaceImpl *root,
2472 UINT count,
2473 DDSURFACEDESC2 DDSD,
2474 BOOL CubeFaceRoot, UINT version)
2476 UINT i, j, level = 0;
2477 HRESULT hr;
2478 IDirectDrawSurfaceImpl *last = root;
2480 for(i = 0; i < count; i++)
2482 IDirectDrawSurfaceImpl *object2 = NULL;
2484 /* increase the mipmap level, but only if a mipmap is created
2485 * In this case, also halve the size
2487 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2489 level++;
2490 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2491 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2492 /* Set the mipmap sublevel flag according to msdn */
2493 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2495 else
2497 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2499 CubeFaceRoot = FALSE;
2501 hr = ddraw_create_surface(This, &DDSD, &object2, level, version);
2502 if(hr != DD_OK)
2504 return hr;
2507 /* Add the new surface to the complex attachment array */
2508 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2510 if(last->complex_array[j]) continue;
2511 last->complex_array[j] = object2;
2512 break;
2514 last = object2;
2516 /* Remove the (possible) back buffer cap from the new surface description,
2517 * because only one surface in the flipping chain is a back buffer, one
2518 * is a front buffer, the others are just primary surfaces.
2520 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2522 return DD_OK;
2525 static HRESULT ddraw_attach_d3d_device(IDirectDrawImpl *ddraw,
2526 WINED3DPRESENT_PARAMETERS *presentation_parameters)
2528 HWND window = presentation_parameters->hDeviceWindow;
2529 HRESULT hr;
2531 TRACE("ddraw %p.\n", ddraw);
2533 if (!window || window == GetDesktopWindow())
2535 window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "Hidden D3D Window",
2536 WS_DISABLED, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
2537 NULL, NULL, NULL, NULL);
2538 if (!window)
2540 ERR("Failed to create window, last error %#x.\n", GetLastError());
2541 return E_FAIL;
2544 ShowWindow(window, SW_HIDE); /* Just to be sure */
2545 WARN("No window for the Direct3DDevice, created hidden window %p.\n", window);
2547 presentation_parameters->hDeviceWindow = window;
2549 else
2551 TRACE("Using existing window %p for Direct3D rendering.\n", window);
2553 ddraw->d3d_window = window;
2555 /* Set this NOW, otherwise creating the depth stencil surface will cause a
2556 * recursive loop until ram or emulated video memory is full. */
2557 ddraw->d3d_initialized = TRUE;
2558 hr = wined3d_device_init_3d(ddraw->wined3d_device, presentation_parameters);
2559 if (FAILED(hr))
2561 ddraw->d3d_target = NULL;
2562 ddraw->d3d_initialized = FALSE;
2563 return hr;
2566 ddraw->declArraySize = 2;
2567 ddraw->decls = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw->decls) * ddraw->declArraySize);
2568 if (!ddraw->decls)
2570 ERR("Error allocating an array for the converted vertex decls.\n");
2571 ddraw->declArraySize = 0;
2572 hr = wined3d_device_uninit_3d(ddraw->wined3d_device);
2573 return E_OUTOFMEMORY;
2576 TRACE("Successfully initialized 3D.\n");
2578 return DD_OK;
2581 static HRESULT ddraw_create_swapchain(IDirectDrawImpl *ddraw, IDirectDrawSurfaceImpl *surface)
2583 WINED3DPRESENT_PARAMETERS presentation_parameters;
2584 struct wined3d_display_mode mode;
2585 HRESULT hr = WINED3D_OK;
2587 /* FIXME: wined3d_get_adapter_display_mode() would be more appropriate
2588 * here, since we don't actually have a swapchain yet, but
2589 * wined3d_device_get_display_mode() has some special handling for color
2590 * depth changes. */
2591 hr = wined3d_device_get_display_mode(ddraw->wined3d_device, 0, &mode);
2592 if (FAILED(hr))
2594 ERR("Failed to get display mode.\n");
2595 return hr;
2598 memset(&presentation_parameters, 0, sizeof(presentation_parameters));
2599 presentation_parameters.BackBufferWidth = mode.width;
2600 presentation_parameters.BackBufferHeight = mode.height;
2601 presentation_parameters.BackBufferFormat = mode.format_id;
2602 presentation_parameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
2603 presentation_parameters.hDeviceWindow = ddraw->dest_window;
2604 presentation_parameters.Windowed = !(ddraw->cooperative_level & DDSCL_FULLSCREEN);
2606 /* If the implementation is OpenGL and there's no d3ddevice, attach a
2607 * d3ddevice. But attach the d3ddevice only if the currently created
2608 * surface was a primary surface (2D app in 3D mode) or a 3DDEVICE surface
2609 * (3D app). The only case I can think of where this doesn't apply is when
2610 * a 2D app was configured by the user to run with OpenGL and it didn't
2611 * create the render target as first surface. In this case the render
2612 * target creation will cause the 3D init. */
2613 if (DefaultSurfaceType == SURFACE_OPENGL)
2615 hr = ddraw_attach_d3d_device(ddraw, &presentation_parameters);
2617 else if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2619 hr = wined3d_device_init_gdi(ddraw->wined3d_device, &presentation_parameters);
2620 if (FAILED(hr))
2621 WARN("Failed to initialize GDI ddraw implementation, hr %#x.\n", hr);
2624 if (SUCCEEDED(hr))
2626 if (FAILED(hr = wined3d_device_get_swapchain(ddraw->wined3d_device, 0, &ddraw->wined3d_swapchain)))
2628 ERR("Failed to get swapchain, hr %#x.\n", hr);
2629 ddraw->wined3d_swapchain = NULL;
2633 if (SUCCEEDED(hr))
2634 ddraw_set_swapchain_window(ddraw, ddraw->dest_window);
2636 return hr;
2639 /*****************************************************************************
2640 * IDirectDraw7::CreateSurface
2642 * Creates a new IDirectDrawSurface object and returns its interface.
2644 * The surface connections with wined3d are a bit tricky. Basically it works
2645 * like this:
2647 * |------------------------| |-----------------|
2648 * | DDraw surface | | WineD3DSurface |
2649 * | | | |
2650 * | WineD3DSurface |-------------->| |
2651 * | Child |<------------->| Parent |
2652 * |------------------------| |-----------------|
2654 * The DDraw surface is the parent of the wined3d surface, and it releases
2655 * the WineD3DSurface when the ddraw surface is destroyed.
2657 * However, for all surfaces which can be in a container in WineD3D,
2658 * we have to do this. These surfaces are usually complex surfaces,
2659 * so this concerns primary surfaces with a front and a back buffer,
2660 * and textures.
2662 * |------------------------| |-----------------|
2663 * | DDraw surface | | Container |
2664 * | | | |
2665 * | Child |<------------->| Parent |
2666 * | Texture |<------------->| |
2667 * | WineD3DSurface |<----| | Levels |<--|
2668 * | Complex connection | | | | |
2669 * |------------------------| | |-----------------| |
2670 * ^ | |
2671 * | | |
2672 * | | |
2673 * | |------------------| | |-----------------| |
2674 * | | IParent | |-------->| WineD3DSurface | |
2675 * | | | | | |
2676 * | | Child |<------------->| Parent | |
2677 * | | | | Container |<--|
2678 * | |------------------| |-----------------| |
2679 * | |
2680 * | |----------------------| |
2681 * | | DDraw surface 2 | |
2682 * | | | |
2683 * |<->| Complex root Child | |
2684 * | | Texture | |
2685 * | | WineD3DSurface |<----| |
2686 * | |----------------------| | |
2687 * | | |
2688 * | |---------------------| | |-----------------| |
2689 * | | IParent | |----->| WineD3DSurface | |
2690 * | | | | | |
2691 * | | Child |<---------->| Parent | |
2692 * | |---------------------| | Container |<--|
2693 * | |-----------------| |
2694 * | |
2695 * | ---More surfaces can follow--- |
2697 * The reason is that the IWineD3DSwapchain(render target container)
2698 * and the IWineD3DTexure(Texture container) release the parents
2699 * of their surface's children, but by releasing the complex root
2700 * the surfaces which are complexly attached to it are destroyed
2701 * too. See IDirectDrawSurface::Release for a more detailed
2702 * explanation.
2704 * Params:
2705 * DDSD: Description of the surface to create
2706 * Surf: Address to store the interface pointer at
2707 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2708 * aggregation, so it has to be NULL
2710 * Returns:
2711 * DD_OK on success
2712 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2713 * DDERR_* if an error occurs
2715 *****************************************************************************/
2716 static HRESULT CreateSurface(IDirectDrawImpl *ddraw, DDSURFACEDESC2 *DDSD,
2717 IDirectDrawSurfaceImpl **Surf, IUnknown *UnkOuter, UINT version)
2719 IDirectDrawSurfaceImpl *object = NULL;
2720 struct wined3d_display_mode mode;
2721 HRESULT hr;
2722 LONG extra_surfaces = 0;
2723 DDSURFACEDESC2 desc2;
2724 const DWORD sysvidmem = DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
2726 TRACE("ddraw %p, surface_desc %p, surface %p, outer_unknown %p.\n", ddraw, DDSD, Surf, UnkOuter);
2728 /* Some checks before we start */
2729 if (TRACE_ON(ddraw))
2731 TRACE(" (%p) Requesting surface desc :\n", ddraw);
2732 DDRAW_dump_surface_desc(DDSD);
2735 wined3d_mutex_lock();
2737 if (UnkOuter != NULL)
2739 FIXME("(%p) : outer != NULL?\n", ddraw);
2740 wined3d_mutex_unlock();
2741 return CLASS_E_NOAGGREGATION; /* unchecked */
2744 if (Surf == NULL)
2746 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", ddraw);
2747 wined3d_mutex_unlock();
2748 return E_POINTER; /* unchecked */
2751 if (!(DDSD->dwFlags & DDSD_CAPS))
2753 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2754 DDSD->dwFlags |= DDSD_CAPS;
2757 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2759 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2760 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2763 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2765 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2766 WARN("(%p) Null surface pointer specified, ignore it!\n", ddraw);
2767 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2770 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2771 !(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
2773 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n",
2774 ddraw);
2775 *Surf = NULL;
2776 wined3d_mutex_unlock();
2777 return DDERR_NOEXCLUSIVEMODE;
2780 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_BACKBUFFER | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_BACKBUFFER | DDSCAPS_PRIMARYSURFACE))
2782 WARN("Application wanted to create back buffer primary surface\n");
2783 wined3d_mutex_unlock();
2784 return DDERR_INVALIDCAPS;
2787 if((DDSD->ddsCaps.dwCaps & sysvidmem) == sysvidmem)
2789 /* This is a special switch in ddrawex.dll, but not allowed in ddraw.dll */
2790 WARN("Application tries to put the surface in both system and video memory\n");
2791 wined3d_mutex_unlock();
2792 *Surf = NULL;
2793 return DDERR_INVALIDCAPS;
2796 /* Check cube maps but only if the size includes them */
2797 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2799 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2800 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2802 WARN("Cube map faces requested without cube map flag\n");
2803 wined3d_mutex_unlock();
2804 return DDERR_INVALIDCAPS;
2806 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2807 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2809 WARN("Cube map without faces requested\n");
2810 wined3d_mutex_unlock();
2811 return DDERR_INVALIDPARAMS;
2814 /* Quick tests confirm those can be created, but we don't do that yet */
2815 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2816 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2818 FIXME("Partial cube maps not supported yet\n");
2822 /* According to the msdn this flag is ignored by CreateSurface */
2823 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2824 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2826 /* Modify some flags */
2827 copy_to_surfacedesc2(&desc2, DDSD);
2828 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2830 /* Get the video mode from WineD3D - we will need it */
2831 hr = wined3d_device_get_display_mode(ddraw->wined3d_device, 0, &mode);
2832 if (FAILED(hr))
2834 ERR("Failed to read display mode from wined3d\n");
2835 switch(ddraw->orig_bpp)
2837 case 8:
2838 mode.format_id = WINED3DFMT_P8_UINT;
2839 break;
2841 case 15:
2842 mode.format_id = WINED3DFMT_B5G5R5X1_UNORM;
2843 break;
2845 case 16:
2846 mode.format_id = WINED3DFMT_B5G6R5_UNORM;
2847 break;
2849 case 24:
2850 mode.format_id = WINED3DFMT_B8G8R8_UNORM;
2851 break;
2853 case 32:
2854 mode.format_id = WINED3DFMT_B8G8R8X8_UNORM;
2855 break;
2857 mode.width = ddraw->orig_width;
2858 mode.height = ddraw->orig_height;
2861 /* No pixelformat given? Use the current screen format */
2862 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2864 desc2.dwFlags |= DDSD_PIXELFORMAT;
2865 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2867 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, mode.format_id);
2870 /* No Width or no Height? Use the original screen size
2872 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2873 !(desc2.dwFlags & DDSD_HEIGHT) )
2875 /* Invalid for non-render targets */
2876 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2878 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2879 *Surf = NULL;
2880 wined3d_mutex_unlock();
2881 return DDERR_INVALIDPARAMS;
2884 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2885 desc2.dwWidth = mode.width;
2886 desc2.dwHeight = mode.height;
2889 if (!desc2.dwWidth || !desc2.dwHeight)
2891 wined3d_mutex_unlock();
2892 return DDERR_INVALIDPARAMS;
2895 /* Mipmap count fixes */
2896 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2898 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2900 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2902 /* Mipmap count is given, should not be 0 */
2903 if( desc2.u2.dwMipMapCount == 0 )
2905 wined3d_mutex_unlock();
2906 return DDERR_INVALIDPARAMS;
2909 else
2911 /* Undocumented feature: Create sublevels until
2912 * either the width or the height is 1
2914 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2915 desc2.dwWidth : desc2.dwHeight;
2916 desc2.u2.dwMipMapCount = 0;
2917 while( min )
2919 desc2.u2.dwMipMapCount += 1;
2920 min >>= 1;
2924 else
2926 /* Not-complex mipmap -> Mipmapcount = 1 */
2927 desc2.u2.dwMipMapCount = 1;
2929 extra_surfaces = desc2.u2.dwMipMapCount - 1;
2931 /* There's a mipmap count in the created surface in any case */
2932 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2934 /* If no mipmap is given, the texture has only one level */
2936 /* The first surface is a front buffer, the back buffer is created afterwards */
2937 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2939 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2942 /* The root surface in a cube map is positive x */
2943 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2945 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2946 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2949 /* Create the first surface */
2950 hr = ddraw_create_surface(ddraw, &desc2, &object, 0, version);
2951 if (FAILED(hr))
2953 WARN("ddraw_create_surface failed, hr %#x.\n", hr);
2954 wined3d_mutex_unlock();
2955 return hr;
2957 object->is_complex_root = TRUE;
2959 *Surf = object;
2961 /* Create Additional surfaces if necessary
2962 * This applies to Primary surfaces which have a back buffer count
2963 * set, but not to mipmap textures. In case of Mipmap textures,
2964 * wineD3D takes care of the creation of additional surfaces
2966 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2968 extra_surfaces = DDSD->dwBackBufferCount;
2969 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2970 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2971 desc2.dwBackBufferCount = 0;
2974 hr = DD_OK;
2975 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2977 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2978 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
2979 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE, version);
2980 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
2981 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
2982 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE, version);
2983 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
2984 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
2985 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE, version);
2986 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
2987 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
2988 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE, version);
2989 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
2990 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
2991 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE, version);
2992 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
2993 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2996 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces, desc2, FALSE, version);
2997 if(hr != DD_OK)
2999 /* This destroys and possibly created surfaces too */
3000 if (version == 7)
3002 IDirectDrawSurface7_Release(&object->IDirectDrawSurface7_iface);
3004 else if (version == 4)
3006 IDirectDrawSurface4_Release(&object->IDirectDrawSurface4_iface);
3008 else
3010 IDirectDrawSurface_Release(&object->IDirectDrawSurface_iface);
3012 wined3d_mutex_unlock();
3013 return hr;
3016 if (!ddraw->d3d_initialized && desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE))
3018 if (FAILED(hr = ddraw_create_swapchain(ddraw, object)))
3020 IDirectDrawSurfaceImpl *release_surf;
3021 ERR("Failed to create swapchain, hr %#x.\n", hr);
3022 *Surf = NULL;
3024 /* The earlier created surface structures are in an incomplete
3025 * state here. Wined3d holds the reference on the parents, and it
3026 * released them on the failure already. So the regular release
3027 * method implementation would fail on the attempt to destroy
3028 * either the parents or the swapchain. So free the surface here.
3029 * The surface structure here is a list, not a tree, because
3030 * onscreen targets cannot be cube textures. */
3031 while (object)
3033 release_surf = object;
3034 object = object->complex_array[0];
3035 ddraw_surface_destroy(release_surf);
3037 wined3d_mutex_unlock();
3038 return hr;
3041 if (DefaultSurfaceType == SURFACE_OPENGL)
3042 ddraw->d3d_target = ddraw->primary ? ddraw->primary : object;
3045 if (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
3046 ddraw->primary = object;
3048 /* Create a WineD3DTexture if a texture was requested */
3049 if (desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
3051 ddraw->tex_root = object;
3052 ddraw_surface_create_texture(object);
3053 ddraw->tex_root = NULL;
3056 wined3d_mutex_unlock();
3058 return hr;
3061 static HRESULT WINAPI ddraw7_CreateSurface(IDirectDraw7 *iface, DDSURFACEDESC2 *surface_desc,
3062 IDirectDrawSurface7 **surface, IUnknown *outer_unknown)
3064 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
3065 IDirectDrawSurfaceImpl *impl;
3066 HRESULT hr;
3068 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3069 iface, surface_desc, surface, outer_unknown);
3071 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
3073 WARN("Application supplied invalid surface descriptor\n");
3074 return DDERR_INVALIDPARAMS;
3077 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3079 if (TRACE_ON(ddraw))
3081 TRACE(" (%p) Requesting surface desc :\n", iface);
3082 DDRAW_dump_surface_desc(surface_desc);
3085 WARN("Application tried to create an explicit front or back buffer\n");
3086 return DDERR_INVALIDCAPS;
3089 hr = CreateSurface(This, surface_desc, &impl, outer_unknown, 7);
3090 if (FAILED(hr))
3092 *surface = NULL;
3093 return hr;
3096 *surface = &impl->IDirectDrawSurface7_iface;
3097 IDirectDraw7_AddRef(iface);
3098 impl->ifaceToRelease = (IUnknown *)iface;
3100 return hr;
3103 static HRESULT WINAPI ddraw4_CreateSurface(IDirectDraw4 *iface,
3104 DDSURFACEDESC2 *surface_desc, IDirectDrawSurface4 **surface, IUnknown *outer_unknown)
3106 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3107 IDirectDrawSurfaceImpl *impl;
3108 HRESULT hr;
3110 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3111 iface, surface_desc, surface, outer_unknown);
3113 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
3115 WARN("Application supplied invalid surface descriptor\n");
3116 return DDERR_INVALIDPARAMS;
3119 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3121 if (TRACE_ON(ddraw))
3123 TRACE(" (%p) Requesting surface desc :\n", iface);
3124 DDRAW_dump_surface_desc(surface_desc);
3127 WARN("Application tried to create an explicit front or back buffer\n");
3128 return DDERR_INVALIDCAPS;
3131 hr = CreateSurface(This, surface_desc, &impl, outer_unknown, 4);
3132 if (FAILED(hr))
3134 *surface = NULL;
3135 return hr;
3138 *surface = &impl->IDirectDrawSurface4_iface;
3139 IDirectDraw4_AddRef(iface);
3140 impl->ifaceToRelease = (IUnknown *)iface;
3142 return hr;
3145 static HRESULT WINAPI ddraw2_CreateSurface(IDirectDraw2 *iface,
3146 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3148 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3149 IDirectDrawSurfaceImpl *impl;
3150 HRESULT hr;
3151 DDSURFACEDESC2 surface_desc2;
3153 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3154 iface, surface_desc, surface, outer_unknown);
3156 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3158 WARN("Application supplied invalid surface descriptor\n");
3159 return DDERR_INVALIDPARAMS;
3162 DDSD_to_DDSD2(surface_desc, &surface_desc2);
3163 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3165 if (TRACE_ON(ddraw))
3167 TRACE(" (%p) Requesting surface desc :\n", iface);
3168 DDRAW_dump_surface_desc((LPDDSURFACEDESC2)surface_desc);
3171 WARN("Application tried to create an explicit front or back buffer\n");
3172 return DDERR_INVALIDCAPS;
3175 hr = CreateSurface(This, &surface_desc2, &impl, outer_unknown, 2);
3176 if (FAILED(hr))
3178 *surface = NULL;
3179 return hr;
3182 *surface = &impl->IDirectDrawSurface_iface;
3183 impl->ifaceToRelease = NULL;
3185 return hr;
3188 static HRESULT WINAPI ddraw1_CreateSurface(IDirectDraw *iface,
3189 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3191 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3192 IDirectDrawSurfaceImpl *impl;
3193 HRESULT hr;
3194 DDSURFACEDESC2 surface_desc2;
3196 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3197 iface, surface_desc, surface, outer_unknown);
3199 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3201 WARN("Application supplied invalid surface descriptor\n");
3202 return DDERR_INVALIDPARAMS;
3205 /* Remove front buffer flag, this causes failure in v7, and its added to normal
3206 * primaries anyway. */
3207 surface_desc->ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER;
3208 DDSD_to_DDSD2(surface_desc, &surface_desc2);
3209 hr = CreateSurface(This, &surface_desc2, &impl, outer_unknown, 1);
3210 if (FAILED(hr))
3212 *surface = NULL;
3213 return hr;
3216 *surface = &impl->IDirectDrawSurface_iface;
3217 impl->ifaceToRelease = NULL;
3219 return hr;
3222 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
3223 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
3225 static BOOL
3226 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
3227 const DDPIXELFORMAT *provided)
3229 /* Some flags must be present in both or neither for a match. */
3230 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
3231 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
3232 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
3234 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3235 return FALSE;
3237 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
3238 return FALSE;
3240 if (requested->dwFlags & DDPF_FOURCC)
3241 if (requested->dwFourCC != provided->dwFourCC)
3242 return FALSE;
3244 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
3245 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3246 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
3247 return FALSE;
3249 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3250 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3251 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
3252 return FALSE;
3254 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
3255 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
3256 return FALSE;
3258 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
3259 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3260 |DDPF_BUMPDUDV))
3261 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
3262 return FALSE;
3264 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
3265 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
3266 return FALSE;
3268 return TRUE;
3271 static BOOL ddraw_match_surface_desc(const DDSURFACEDESC2 *requested, const DDSURFACEDESC2 *provided)
3273 struct compare_info
3275 DWORD flag;
3276 ptrdiff_t offset;
3277 size_t size;
3280 #define CMP(FLAG, FIELD) \
3281 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
3282 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
3284 static const struct compare_info compare[] =
3286 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
3287 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
3288 CMP(CAPS, ddsCaps),
3289 CMP(CKDESTBLT, ddckCKDestBlt),
3290 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
3291 CMP(CKSRCBLT, ddckCKSrcBlt),
3292 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
3293 CMP(HEIGHT, dwHeight),
3294 CMP(LINEARSIZE, u1 /* dwLinearSize */),
3295 CMP(LPSURFACE, lpSurface),
3296 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
3297 CMP(PITCH, u1 /* lPitch */),
3298 /* PIXELFORMAT: manual */
3299 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
3300 CMP(TEXTURESTAGE, dwTextureStage),
3301 CMP(WIDTH, dwWidth),
3302 /* ZBUFFERBITDEPTH: "obsolete" */
3305 #undef CMP
3307 unsigned int i;
3309 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3310 return FALSE;
3312 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
3314 if (requested->dwFlags & compare[i].flag
3315 && memcmp((const char *)provided + compare[i].offset,
3316 (const char *)requested + compare[i].offset,
3317 compare[i].size) != 0)
3318 return FALSE;
3321 if (requested->dwFlags & DDSD_PIXELFORMAT)
3323 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
3324 &provided->u4.ddpfPixelFormat))
3325 return FALSE;
3328 return TRUE;
3331 #undef DDENUMSURFACES_SEARCHTYPE
3332 #undef DDENUMSURFACES_MATCHTYPE
3334 struct surfacescallback2_context
3336 LPDDENUMSURFACESCALLBACK2 func;
3337 void *context;
3340 struct surfacescallback_context
3342 LPDDENUMSURFACESCALLBACK func;
3343 void *context;
3346 static HRESULT CALLBACK EnumSurfacesCallback2Thunk(IDirectDrawSurface7 *surface,
3347 DDSURFACEDESC2 *surface_desc, void *context)
3349 IDirectDrawSurfaceImpl *surface_impl = impl_from_IDirectDrawSurface7(surface);
3350 struct surfacescallback2_context *cbcontext = context;
3352 IDirectDrawSurface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
3353 IDirectDrawSurface7_Release(surface);
3355 return cbcontext->func(&surface_impl->IDirectDrawSurface4_iface,
3356 surface_desc, cbcontext->context);
3359 static HRESULT CALLBACK EnumSurfacesCallbackThunk(IDirectDrawSurface7 *surface,
3360 DDSURFACEDESC2 *surface_desc, void *context)
3362 IDirectDrawSurfaceImpl *surface_impl = impl_from_IDirectDrawSurface7(surface);
3363 struct surfacescallback_context *cbcontext = context;
3365 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
3366 IDirectDrawSurface7_Release(surface);
3368 return cbcontext->func(&surface_impl->IDirectDrawSurface_iface,
3369 (DDSURFACEDESC *)surface_desc, cbcontext->context);
3372 /*****************************************************************************
3373 * IDirectDraw7::EnumSurfaces
3375 * Loops through all surfaces attached to this device and calls the
3376 * application callback. This can't be relayed to WineD3DDevice,
3377 * because some WineD3DSurfaces' parents are IParent objects
3379 * Params:
3380 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
3381 * DDSD: Description to filter for
3382 * Context: Application-provided pointer, it's passed unmodified to the
3383 * Callback function
3384 * Callback: Address to call for each surface
3386 * Returns:
3387 * DDERR_INVALIDPARAMS if the callback is NULL
3388 * DD_OK on success
3390 *****************************************************************************/
3391 static HRESULT WINAPI ddraw7_EnumSurfaces(IDirectDraw7 *iface, DWORD Flags,
3392 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMSURFACESCALLBACK7 Callback)
3394 /* The surface enumeration is handled by WineDDraw,
3395 * because it keeps track of all surfaces attached to
3396 * it. The filtering is done by our callback function,
3397 * because WineDDraw doesn't handle ddraw-like surface
3398 * caps structures
3400 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
3401 IDirectDrawSurfaceImpl *surf;
3402 BOOL all, nomatch;
3403 DDSURFACEDESC2 desc;
3404 struct list *entry, *entry2;
3406 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3407 iface, Flags, DDSD, Context, Callback);
3409 all = Flags & DDENUMSURFACES_ALL;
3410 nomatch = Flags & DDENUMSURFACES_NOMATCH;
3412 if (!Callback)
3413 return DDERR_INVALIDPARAMS;
3415 wined3d_mutex_lock();
3417 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
3418 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
3420 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
3421 if (all || (nomatch != ddraw_match_surface_desc(DDSD, &surf->surface_desc)))
3423 TRACE("Enumerating surface %p.\n", surf);
3424 desc = surf->surface_desc;
3425 IDirectDrawSurface7_AddRef(&surf->IDirectDrawSurface7_iface);
3426 if (Callback(&surf->IDirectDrawSurface7_iface, &desc, Context) != DDENUMRET_OK)
3428 wined3d_mutex_unlock();
3429 return DD_OK;
3434 wined3d_mutex_unlock();
3436 return DD_OK;
3439 static HRESULT WINAPI ddraw4_EnumSurfaces(IDirectDraw4 *iface, DWORD flags,
3440 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMSURFACESCALLBACK2 callback)
3442 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3443 struct surfacescallback2_context cbcontext;
3445 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3446 iface, flags, surface_desc, context, callback);
3448 cbcontext.func = callback;
3449 cbcontext.context = context;
3451 return ddraw7_EnumSurfaces(&This->IDirectDraw7_iface, flags, surface_desc,
3452 &cbcontext, EnumSurfacesCallback2Thunk);
3455 static HRESULT WINAPI ddraw2_EnumSurfaces(IDirectDraw2 *iface, DWORD flags,
3456 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3458 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3459 struct surfacescallback_context cbcontext;
3460 DDSURFACEDESC2 surface_desc2;
3462 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3463 iface, flags, surface_desc, context, callback);
3465 cbcontext.func = callback;
3466 cbcontext.context = context;
3468 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3469 return ddraw7_EnumSurfaces(&This->IDirectDraw7_iface, flags,
3470 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3473 static HRESULT WINAPI ddraw1_EnumSurfaces(IDirectDraw *iface, DWORD flags,
3474 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3476 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3477 struct surfacescallback_context cbcontext;
3478 DDSURFACEDESC2 surface_desc2;
3480 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3481 iface, flags, surface_desc, context, callback);
3483 cbcontext.func = callback;
3484 cbcontext.context = context;
3486 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3487 return ddraw7_EnumSurfaces(&This->IDirectDraw7_iface, flags,
3488 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3491 /*****************************************************************************
3492 * DirectDrawCreateClipper (DDRAW.@)
3494 * Creates a new IDirectDrawClipper object.
3496 * Params:
3497 * Clipper: Address to write the interface pointer to
3498 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3499 * NULL
3501 * Returns:
3502 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3503 * E_OUTOFMEMORY if allocating the object failed
3505 *****************************************************************************/
3506 HRESULT WINAPI
3507 DirectDrawCreateClipper(DWORD Flags,
3508 LPDIRECTDRAWCLIPPER *Clipper,
3509 IUnknown *UnkOuter)
3511 IDirectDrawClipperImpl* object;
3512 HRESULT hr;
3514 TRACE("flags %#x, clipper %p, outer_unknown %p.\n",
3515 Flags, Clipper, UnkOuter);
3517 if (UnkOuter)
3518 return CLASS_E_NOAGGREGATION;
3520 wined3d_mutex_lock();
3522 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3523 sizeof(IDirectDrawClipperImpl));
3524 if (object == NULL)
3526 wined3d_mutex_unlock();
3527 return E_OUTOFMEMORY;
3530 hr = ddraw_clipper_init(object);
3531 if (FAILED(hr))
3533 WARN("Failed to initialize clipper, hr %#x.\n", hr);
3534 HeapFree(GetProcessHeap(), 0, object);
3535 wined3d_mutex_unlock();
3536 return hr;
3539 TRACE("Created clipper %p.\n", object);
3540 *Clipper = &object->IDirectDrawClipper_iface;
3541 wined3d_mutex_unlock();
3543 return DD_OK;
3546 /*****************************************************************************
3547 * IDirectDraw7::CreateClipper
3549 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3551 *****************************************************************************/
3552 static HRESULT WINAPI ddraw7_CreateClipper(IDirectDraw7 *iface, DWORD Flags,
3553 IDirectDrawClipper **Clipper, IUnknown *UnkOuter)
3555 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3556 iface, Flags, Clipper, UnkOuter);
3558 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3561 static HRESULT WINAPI ddraw4_CreateClipper(IDirectDraw4 *iface, DWORD flags,
3562 IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3564 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3566 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3567 iface, flags, clipper, outer_unknown);
3569 return ddraw7_CreateClipper(&This->IDirectDraw7_iface, flags, clipper, outer_unknown);
3572 static HRESULT WINAPI ddraw2_CreateClipper(IDirectDraw2 *iface,
3573 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3575 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3577 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3578 iface, flags, clipper, outer_unknown);
3580 return ddraw7_CreateClipper(&This->IDirectDraw7_iface, flags, clipper, outer_unknown);
3583 static HRESULT WINAPI ddraw1_CreateClipper(IDirectDraw *iface,
3584 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3586 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3588 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3589 iface, flags, clipper, outer_unknown);
3591 return ddraw7_CreateClipper(&This->IDirectDraw7_iface, flags, clipper, outer_unknown);
3594 /*****************************************************************************
3595 * IDirectDraw7::CreatePalette
3597 * Creates a new IDirectDrawPalette object
3599 * Params:
3600 * Flags: The flags for the new clipper
3601 * ColorTable: Color table to assign to the new clipper
3602 * Palette: Address to write the interface pointer to
3603 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3604 * NULL
3606 * Returns:
3607 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3608 * E_OUTOFMEMORY if allocating the object failed
3610 *****************************************************************************/
3611 static HRESULT WINAPI ddraw7_CreatePalette(IDirectDraw7 *iface, DWORD Flags,
3612 PALETTEENTRY *ColorTable, IDirectDrawPalette **Palette, IUnknown *pUnkOuter)
3614 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
3615 IDirectDrawPaletteImpl *object;
3616 HRESULT hr;
3618 TRACE("iface %p, flags %#x, color_table %p, palette %p, outer_unknown %p.\n",
3619 iface, Flags, ColorTable, Palette, pUnkOuter);
3621 if (pUnkOuter)
3622 return CLASS_E_NOAGGREGATION;
3624 wined3d_mutex_lock();
3626 /* The refcount test shows that a cooplevel is required for this */
3627 if(!This->cooperative_level)
3629 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3630 wined3d_mutex_unlock();
3631 return DDERR_NOCOOPERATIVELEVELSET;
3634 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
3635 if(!object)
3637 ERR("Out of memory when allocating memory for a palette implementation\n");
3638 wined3d_mutex_unlock();
3639 return E_OUTOFMEMORY;
3642 hr = ddraw_palette_init(object, This, Flags, ColorTable);
3643 if (FAILED(hr))
3645 WARN("Failed to initialize palette, hr %#x.\n", hr);
3646 HeapFree(GetProcessHeap(), 0, object);
3647 wined3d_mutex_unlock();
3648 return hr;
3651 TRACE("Created palette %p.\n", object);
3652 *Palette = &object->IDirectDrawPalette_iface;
3653 wined3d_mutex_unlock();
3655 return DD_OK;
3658 static HRESULT WINAPI ddraw4_CreatePalette(IDirectDraw4 *iface, DWORD flags, PALETTEENTRY *entries,
3659 IDirectDrawPalette **palette, IUnknown *outer_unknown)
3661 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3662 HRESULT hr;
3664 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3665 iface, flags, entries, palette, outer_unknown);
3667 hr = ddraw7_CreatePalette(&This->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3668 if (SUCCEEDED(hr) && *palette)
3670 IDirectDrawPaletteImpl *impl = impl_from_IDirectDrawPalette(*palette);
3671 IDirectDraw7_Release(&This->IDirectDraw7_iface);
3672 IDirectDraw4_AddRef(iface);
3673 impl->ifaceToRelease = (IUnknown *)iface;
3675 return hr;
3678 static HRESULT WINAPI ddraw2_CreatePalette(IDirectDraw2 *iface, DWORD flags,
3679 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3681 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3682 HRESULT hr;
3684 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3685 iface, flags, entries, palette, outer_unknown);
3687 hr = ddraw7_CreatePalette(&This->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3688 if (SUCCEEDED(hr) && *palette)
3690 IDirectDrawPaletteImpl *impl = impl_from_IDirectDrawPalette(*palette);
3691 IDirectDraw7_Release(&This->IDirectDraw7_iface);
3692 impl->ifaceToRelease = NULL;
3695 return hr;
3698 static HRESULT WINAPI ddraw1_CreatePalette(IDirectDraw *iface, DWORD flags,
3699 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3701 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3702 HRESULT hr;
3704 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3705 iface, flags, entries, palette, outer_unknown);
3707 hr = ddraw7_CreatePalette(&This->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3708 if (SUCCEEDED(hr) && *palette)
3710 IDirectDrawPaletteImpl *impl = impl_from_IDirectDrawPalette(*palette);
3711 IDirectDraw7_Release(&This->IDirectDraw7_iface);
3712 impl->ifaceToRelease = NULL;
3715 return hr;
3718 /*****************************************************************************
3719 * IDirectDraw7::DuplicateSurface
3721 * Duplicates a surface. The surface memory points to the same memory as
3722 * the original surface, and it's released when the last surface referencing
3723 * it is released. I guess that's beyond Wine's surface management right now
3724 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3725 * test application to implement this)
3727 * Params:
3728 * Src: Address of the source surface
3729 * Dest: Address to write the new surface pointer to
3731 * Returns:
3732 * See IDirectDraw7::CreateSurface
3734 *****************************************************************************/
3735 static HRESULT WINAPI ddraw7_DuplicateSurface(IDirectDraw7 *iface,
3736 IDirectDrawSurface7 *Src, IDirectDrawSurface7 **Dest)
3738 IDirectDrawSurfaceImpl *Surf = unsafe_impl_from_IDirectDrawSurface7(Src);
3740 FIXME("iface %p, src %p, dst %p partial stub!\n", iface, Src, Dest);
3742 /* For now, simply create a new, independent surface */
3743 return IDirectDraw7_CreateSurface(iface,
3744 &Surf->surface_desc,
3745 Dest,
3746 NULL);
3749 static HRESULT WINAPI ddraw4_DuplicateSurface(IDirectDraw4 *iface, IDirectDrawSurface4 *src,
3750 IDirectDrawSurface4 **dst)
3752 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3753 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface4(src);
3754 IDirectDrawSurface7 *dst7;
3755 IDirectDrawSurfaceImpl *dst_impl;
3756 HRESULT hr;
3758 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3759 hr = ddraw7_DuplicateSurface(&This->IDirectDraw7_iface,
3760 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3761 if (FAILED(hr))
3763 *dst = NULL;
3764 return hr;
3766 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3767 *dst = &dst_impl->IDirectDrawSurface4_iface;
3768 IDirectDrawSurface4_AddRef(*dst);
3769 IDirectDrawSurface7_Release(dst7);
3771 return hr;
3774 static HRESULT WINAPI ddraw2_DuplicateSurface(IDirectDraw2 *iface,
3775 IDirectDrawSurface *src, IDirectDrawSurface **dst)
3777 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3778 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3779 IDirectDrawSurface7 *dst7;
3780 IDirectDrawSurfaceImpl *dst_impl;
3781 HRESULT hr;
3783 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3784 hr = ddraw7_DuplicateSurface(&This->IDirectDraw7_iface,
3785 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3786 if (FAILED(hr))
3787 return hr;
3788 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3789 *dst = &dst_impl->IDirectDrawSurface_iface;
3790 IDirectDrawSurface_AddRef(*dst);
3791 IDirectDrawSurface7_Release(dst7);
3793 return hr;
3796 static HRESULT WINAPI ddraw1_DuplicateSurface(IDirectDraw *iface, IDirectDrawSurface *src,
3797 IDirectDrawSurface **dst)
3799 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3800 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3801 IDirectDrawSurface7 *dst7;
3802 IDirectDrawSurfaceImpl *dst_impl;
3803 HRESULT hr;
3805 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3806 hr = ddraw7_DuplicateSurface(&This->IDirectDraw7_iface,
3807 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3808 if (FAILED(hr))
3809 return hr;
3810 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3811 *dst = &dst_impl->IDirectDrawSurface_iface;
3812 IDirectDrawSurface_AddRef(*dst);
3813 IDirectDrawSurface7_Release(dst7);
3815 return hr;
3818 /*****************************************************************************
3819 * IDirect3D7::EnumDevices
3821 * The EnumDevices method for IDirect3D7. It enumerates all supported
3822 * D3D7 devices. Currently the T&L, HAL and RGB devices are enumerated.
3824 * Params:
3825 * callback: Function to call for each enumerated device
3826 * context: Pointer to pass back to the app
3828 * Returns:
3829 * D3D_OK, or the return value of the GetCaps call
3831 *****************************************************************************/
3832 static HRESULT WINAPI d3d7_EnumDevices(IDirect3D7 *iface, LPD3DENUMDEVICESCALLBACK7 callback, void *context)
3834 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
3835 D3DDEVICEDESC7 device_desc7;
3836 D3DDEVICEDESC device_desc1;
3837 HRESULT hr;
3838 size_t i;
3840 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3842 if (!callback)
3843 return DDERR_INVALIDPARAMS;
3845 wined3d_mutex_lock();
3847 hr = IDirect3DImpl_GetCaps(This->wineD3D, &device_desc1, &device_desc7);
3848 if (hr != D3D_OK)
3850 wined3d_mutex_unlock();
3851 return hr;
3854 for (i = 0; i < sizeof(device_list7)/sizeof(device_list7[0]); i++)
3856 HRESULT ret;
3858 device_desc7.deviceGUID = *device_list7[i].device_guid;
3859 ret = callback(device_list7[i].interface_name, device_list7[i].device_name, &device_desc7, context);
3860 if (ret != DDENUMRET_OK)
3862 TRACE("Application cancelled the enumeration.\n");
3863 wined3d_mutex_unlock();
3864 return D3D_OK;
3868 TRACE("End of enumeration.\n");
3870 wined3d_mutex_unlock();
3872 return D3D_OK;
3875 /*****************************************************************************
3876 * IDirect3D3::EnumDevices
3878 * Enumerates all supported Direct3DDevice interfaces. This is the
3879 * implementation for Direct3D 1 to Direc3D 3, Version 7 has its own.
3881 * Version 1, 2 and 3
3883 * Params:
3884 * callback: Application-provided routine to call for each enumerated device
3885 * Context: Pointer to pass to the callback
3887 * Returns:
3888 * D3D_OK on success,
3889 * The result of IDirect3DImpl_GetCaps if it failed
3891 *****************************************************************************/
3892 static HRESULT WINAPI d3d3_EnumDevices(IDirect3D3 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3894 static CHAR wined3d_description[] = "Wine D3DDevice using WineD3D and OpenGL";
3896 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
3897 D3DDEVICEDESC device_desc1, hal_desc, hel_desc;
3898 D3DDEVICEDESC7 device_desc7;
3899 HRESULT hr;
3901 /* Some games (Motoracer 2 demo) have the bad idea to modify the device
3902 * name string. Let's put the string in a sufficiently sized array in
3903 * writable memory. */
3904 char device_name[50];
3905 strcpy(device_name,"Direct3D HEL");
3907 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3909 if (!callback)
3910 return DDERR_INVALIDPARAMS;
3912 wined3d_mutex_lock();
3914 hr = IDirect3DImpl_GetCaps(This->wineD3D, &device_desc1, &device_desc7);
3915 if (hr != D3D_OK)
3917 wined3d_mutex_unlock();
3918 return hr;
3921 /* Do I have to enumerate the reference id? Note from old d3d7:
3922 * "It seems that enumerating the reference IID on Direct3D 1 games
3923 * (AvP / Motoracer2) breaks them". So do not enumerate this iid in V1
3925 * There's a registry key HKLM\Software\Microsoft\Direct3D\Drivers,
3926 * EnumReference which enables / disables enumerating the reference
3927 * rasterizer. It's a DWORD, 0 means disabled, 2 means enabled. The
3928 * enablerefrast.reg and disablerefrast.reg files in the DirectX 7.0 sdk
3929 * demo directory suggest this.
3931 * Some games(GTA 2) seem to use the second enumerated device, so I have
3932 * to enumerate at least 2 devices. So enumerate the reference device to
3933 * have 2 devices.
3935 * Other games (Rollcage) tell emulation and hal device apart by certain
3936 * flags. Rollcage expects D3DPTEXTURECAPS_POW2 to be set (yeah, it is a
3937 * limitation flag), and it refuses all devices that have the perspective
3938 * flag set. This way it refuses the emulation device, and HAL devices
3939 * never have POW2 unset in d3d7 on windows. */
3940 if (This->d3dversion != 1)
3942 static CHAR reference_description[] = "RGB Direct3D emulation";
3944 TRACE("Enumerating WineD3D D3DDevice interface.\n");
3945 hal_desc = device_desc1;
3946 hel_desc = device_desc1;
3947 /* The rgb device has the pow2 flag set in the hel caps, but not in the hal caps. */
3948 hal_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3949 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3950 hal_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3951 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3952 /* RGB, RAMP and MMX devices have a HAL dcmColorModel of 0 */
3953 hal_desc.dcmColorModel = 0;
3955 hr = callback((GUID *)&IID_IDirect3DRGBDevice, reference_description,
3956 device_name, &hal_desc, &hel_desc, context);
3957 if (hr != D3DENUMRET_OK)
3959 TRACE("Application cancelled the enumeration.\n");
3960 wined3d_mutex_unlock();
3961 return D3D_OK;
3965 strcpy(device_name,"Direct3D HAL");
3967 TRACE("Enumerating HAL Direct3D device.\n");
3968 hal_desc = device_desc1;
3969 hel_desc = device_desc1;
3971 /* The hal device does not have the pow2 flag set in hel, but in hal. */
3972 hel_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3973 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3974 hel_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3975 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3976 /* HAL devices have a HEL dcmColorModel of 0 */
3977 hel_desc.dcmColorModel = 0;
3979 hr = callback((GUID *)&IID_IDirect3DHALDevice, wined3d_description,
3980 device_name, &hal_desc, &hel_desc, context);
3981 if (hr != D3DENUMRET_OK)
3983 TRACE("Application cancelled the enumeration.\n");
3984 wined3d_mutex_unlock();
3985 return D3D_OK;
3988 TRACE("End of enumeration.\n");
3990 wined3d_mutex_unlock();
3992 return D3D_OK;
3995 static HRESULT WINAPI d3d2_EnumDevices(IDirect3D2 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3997 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
3999 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4001 return d3d3_EnumDevices(&This->IDirect3D3_iface, callback, context);
4004 static HRESULT WINAPI d3d1_EnumDevices(IDirect3D *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
4006 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4008 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4010 return d3d3_EnumDevices(&This->IDirect3D3_iface, callback, context);
4013 /*****************************************************************************
4014 * IDirect3D3::CreateLight
4016 * Creates an IDirect3DLight interface. This interface is used in
4017 * Direct3D3 or earlier for lighting. In Direct3D7 it has been replaced
4018 * by the DIRECT3DLIGHT7 structure. Wine's Direct3DLight implementation
4019 * uses the IDirect3DDevice7 interface with D3D7 lights.
4021 * Version 1, 2 and 3
4023 * Params:
4024 * light: Address to store the new interface pointer
4025 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4026 * Must be NULL
4028 * Returns:
4029 * D3D_OK on success
4030 * DDERR_OUTOFMEMORY if memory allocation failed
4031 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4033 *****************************************************************************/
4034 static HRESULT WINAPI d3d3_CreateLight(IDirect3D3 *iface, IDirect3DLight **light,
4035 IUnknown *outer_unknown)
4037 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4038 IDirect3DLightImpl *object;
4040 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4042 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4044 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4045 if (!object)
4047 ERR("Failed to allocate light memory.\n");
4048 return DDERR_OUTOFMEMORY;
4051 d3d_light_init(object, This);
4053 TRACE("Created light %p.\n", object);
4054 *light = &object->IDirect3DLight_iface;
4056 return D3D_OK;
4059 static HRESULT WINAPI d3d2_CreateLight(IDirect3D2 *iface, IDirect3DLight **light, IUnknown *outer_unknown)
4061 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4063 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4065 return d3d3_CreateLight(&This->IDirect3D3_iface, light, outer_unknown);
4068 static HRESULT WINAPI d3d1_CreateLight(IDirect3D *iface, IDirect3DLight **light, IUnknown *outer_unknown)
4070 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4072 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4074 return d3d3_CreateLight(&This->IDirect3D3_iface, light, outer_unknown);
4077 /*****************************************************************************
4078 * IDirect3D3::CreateMaterial
4080 * Creates an IDirect3DMaterial interface. This interface is used by Direct3D3
4081 * and older versions. The IDirect3DMaterial implementation wraps its
4082 * functionality to IDirect3DDevice7::SetMaterial and friends.
4084 * Version 1, 2 and 3
4086 * Params:
4087 * material: Address to store the new interface's pointer to
4088 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4089 * Must be NULL
4091 * Returns:
4092 * D3D_OK on success
4093 * DDERR_OUTOFMEMORY if memory allocation failed
4094 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4096 *****************************************************************************/
4097 static HRESULT WINAPI d3d3_CreateMaterial(IDirect3D3 *iface, IDirect3DMaterial3 **material,
4098 IUnknown *outer_unknown)
4100 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4101 IDirect3DMaterialImpl *object;
4103 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4105 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4107 object = d3d_material_create(This);
4108 if (!object)
4110 ERR("Failed to allocate material memory.\n");
4111 return DDERR_OUTOFMEMORY;
4114 TRACE("Created material %p.\n", object);
4115 *material = &object->IDirect3DMaterial3_iface;
4117 return D3D_OK;
4120 static HRESULT WINAPI d3d2_CreateMaterial(IDirect3D2 *iface, IDirect3DMaterial2 **material,
4121 IUnknown *outer_unknown)
4123 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4124 IDirect3DMaterialImpl *object;
4126 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4128 object = d3d_material_create(This);
4129 if (!object)
4131 ERR("Failed to allocate material memory.\n");
4132 return DDERR_OUTOFMEMORY;
4135 TRACE("Created material %p.\n", object);
4136 *material = &object->IDirect3DMaterial2_iface;
4138 return D3D_OK;
4141 static HRESULT WINAPI d3d1_CreateMaterial(IDirect3D *iface, IDirect3DMaterial **material,
4142 IUnknown *outer_unknown)
4144 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4145 IDirect3DMaterialImpl *object;
4147 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4149 object = d3d_material_create(This);
4150 if (!object)
4152 ERR("Failed to allocate material memory.\n");
4153 return DDERR_OUTOFMEMORY;
4156 TRACE("Created material %p.\n", object);
4157 *material = &object->IDirect3DMaterial_iface;
4159 return D3D_OK;
4162 /*****************************************************************************
4163 * IDirect3D3::CreateViewport
4165 * Creates an IDirect3DViewport interface. This interface is used
4166 * by Direct3D and earlier versions for Viewport management. In Direct3D7
4167 * it has been replaced by a viewport structure and
4168 * IDirect3DDevice7::*Viewport. Wine's IDirect3DViewport implementation
4169 * uses the IDirect3DDevice7 methods for its functionality
4171 * Params:
4172 * Viewport: Address to store the new interface pointer
4173 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4174 * Must be NULL
4176 * Returns:
4177 * D3D_OK on success
4178 * DDERR_OUTOFMEMORY if memory allocation failed
4179 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4181 *****************************************************************************/
4182 static HRESULT WINAPI d3d3_CreateViewport(IDirect3D3 *iface, IDirect3DViewport3 **viewport,
4183 IUnknown *outer_unknown)
4185 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4186 IDirect3DViewportImpl *object;
4188 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4190 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4192 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4193 if (!object)
4195 ERR("Failed to allocate viewport memory.\n");
4196 return DDERR_OUTOFMEMORY;
4199 d3d_viewport_init(object, This);
4201 TRACE("Created viewport %p.\n", object);
4202 *viewport = &object->IDirect3DViewport3_iface;
4204 return D3D_OK;
4207 static HRESULT WINAPI d3d2_CreateViewport(IDirect3D2 *iface, IDirect3DViewport2 **viewport, IUnknown *outer_unknown)
4209 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4211 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4213 return d3d3_CreateViewport(&This->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4214 outer_unknown);
4217 static HRESULT WINAPI d3d1_CreateViewport(IDirect3D *iface, IDirect3DViewport **viewport, IUnknown *outer_unknown)
4219 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4221 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4223 return d3d3_CreateViewport(&This->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4224 outer_unknown);
4227 /*****************************************************************************
4228 * IDirect3D3::FindDevice
4230 * This method finds a device with the requested properties and returns a
4231 * device description
4233 * Verion 1, 2 and 3
4234 * Params:
4235 * fds: Describes the requested device characteristics
4236 * fdr: Returns the device description
4238 * Returns:
4239 * D3D_OK on success
4240 * DDERR_INVALIDPARAMS if no device was found
4242 *****************************************************************************/
4243 static HRESULT WINAPI d3d3_FindDevice(IDirect3D3 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4245 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4246 D3DDEVICEDESC7 desc7;
4247 D3DDEVICEDESC desc1;
4248 HRESULT hr;
4250 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4252 if (!fds || !fdr) return DDERR_INVALIDPARAMS;
4254 if (fds->dwSize != sizeof(D3DFINDDEVICESEARCH)
4255 || fdr->dwSize != sizeof(D3DFINDDEVICERESULT))
4256 return DDERR_INVALIDPARAMS;
4258 if ((fds->dwFlags & D3DFDS_COLORMODEL)
4259 && fds->dcmColorModel != D3DCOLOR_RGB)
4261 WARN("Trying to request a non-RGB D3D color model. Not supported.\n");
4262 return DDERR_INVALIDPARAMS; /* No real idea what to return here :-) */
4265 if (fds->dwFlags & D3DFDS_GUID)
4267 TRACE("Trying to match guid %s.\n", debugstr_guid(&(fds->guid)));
4268 if (!IsEqualGUID(&IID_D3DDEVICE_WineD3D, &fds->guid)
4269 && !IsEqualGUID(&IID_IDirect3DHALDevice, &fds->guid)
4270 && !IsEqualGUID(&IID_IDirect3DRGBDevice, &fds->guid))
4272 WARN("No match for this GUID.\n");
4273 return DDERR_NOTFOUND;
4277 /* Get the caps */
4278 hr = IDirect3DImpl_GetCaps(This->wineD3D, &desc1, &desc7);
4279 if (hr != D3D_OK) return hr;
4281 /* Now return our own GUID */
4282 fdr->guid = IID_D3DDEVICE_WineD3D;
4283 fdr->ddHwDesc = desc1;
4284 fdr->ddSwDesc = desc1;
4286 TRACE("Returning Wine's wined3d device with (undumped) capabilities.\n");
4288 return D3D_OK;
4291 static HRESULT WINAPI d3d2_FindDevice(IDirect3D2 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4293 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4295 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4297 return d3d3_FindDevice(&This->IDirect3D3_iface, fds, fdr);
4300 static HRESULT WINAPI d3d1_FindDevice(IDirect3D *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4302 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4304 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4306 return d3d3_FindDevice(&This->IDirect3D3_iface, fds, fdr);
4309 /*****************************************************************************
4310 * IDirect3D7::CreateDevice
4312 * Creates an IDirect3DDevice7 interface.
4314 * Version 2, 3 and 7. IDirect3DDevice 1 interfaces are interfaces to
4315 * DirectDraw surfaces and are created with
4316 * IDirectDrawSurface::QueryInterface. This method uses CreateDevice to
4317 * create the device object and QueryInterfaces for IDirect3DDevice
4319 * Params:
4320 * refiid: IID of the device to create
4321 * Surface: Initial rendertarget
4322 * Device: Address to return the interface pointer
4324 * Returns:
4325 * D3D_OK on success
4326 * DDERR_OUTOFMEMORY if memory allocation failed
4327 * DDERR_INVALIDPARAMS if a device exists already
4329 *****************************************************************************/
4330 static HRESULT WINAPI d3d7_CreateDevice(IDirect3D7 *iface, REFCLSID riid,
4331 IDirectDrawSurface7 *surface, IDirect3DDevice7 **device)
4333 IDirectDrawSurfaceImpl *target = unsafe_impl_from_IDirectDrawSurface7(surface);
4334 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4335 IDirect3DDeviceImpl *object;
4336 HRESULT hr;
4338 TRACE("iface %p, riid %s, surface %p, device %p.\n", iface, debugstr_guid(riid), surface, device);
4340 wined3d_mutex_lock();
4341 *device = NULL;
4343 /* Fail device creation if non-opengl surfaces are used. */
4344 if (DefaultSurfaceType != SURFACE_OPENGL)
4346 ERR("The application wants to create a Direct3D device, but non-opengl surfaces are set in the registry.\n");
4347 ERR("Please set the surface implementation to opengl or autodetection to allow 3D rendering.\n");
4349 /* We only hit this path if a default surface is set in the registry. Incorrect autodetection
4350 * is caught in CreateSurface or QueryInterface. */
4351 wined3d_mutex_unlock();
4352 return DDERR_NO3D;
4355 if (This->d3ddevice)
4357 FIXME("Only one Direct3D device per DirectDraw object supported.\n");
4358 wined3d_mutex_unlock();
4359 return DDERR_INVALIDPARAMS;
4362 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4363 if (!object)
4365 ERR("Failed to allocate device memory.\n");
4366 wined3d_mutex_unlock();
4367 return DDERR_OUTOFMEMORY;
4370 hr = d3d_device_init(object, This, target);
4371 if (FAILED(hr))
4373 WARN("Failed to initialize device, hr %#x.\n", hr);
4374 HeapFree(GetProcessHeap(), 0, object);
4375 wined3d_mutex_unlock();
4376 return hr;
4379 TRACE("Created device %p.\n", object);
4380 *device = &object->IDirect3DDevice7_iface;
4382 wined3d_mutex_unlock();
4384 return D3D_OK;
4387 static HRESULT WINAPI d3d3_CreateDevice(IDirect3D3 *iface, REFCLSID riid,
4388 IDirectDrawSurface4 *surface, IDirect3DDevice3 **device, IUnknown *outer_unknown)
4390 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4391 IDirectDrawSurfaceImpl *surface_impl = unsafe_impl_from_IDirectDrawSurface4(surface);
4392 IDirect3DDevice7 *device7;
4393 IDirect3DDeviceImpl *device_impl;
4394 HRESULT hr;
4396 TRACE("iface %p, riid %s, surface %p, device %p, outer_unknown %p.\n",
4397 iface, debugstr_guid(riid), surface, device, outer_unknown);
4399 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4401 hr = d3d7_CreateDevice(&This->IDirect3D7_iface, riid,
4402 surface_impl ? &surface_impl->IDirectDrawSurface7_iface : NULL, device ? &device7 : NULL);
4403 if (SUCCEEDED(hr))
4405 device_impl = impl_from_IDirect3DDevice7(device7);
4406 *device = &device_impl->IDirect3DDevice3_iface;
4409 return hr;
4412 static HRESULT WINAPI d3d2_CreateDevice(IDirect3D2 *iface, REFCLSID riid,
4413 IDirectDrawSurface *surface, IDirect3DDevice2 **device)
4415 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4416 IDirectDrawSurfaceImpl *surface_impl = unsafe_impl_from_IDirectDrawSurface(surface);
4417 IDirect3DDevice7 *device7;
4418 IDirect3DDeviceImpl *device_impl;
4419 HRESULT hr;
4421 TRACE("iface %p, riid %s, surface %p, device %p.\n",
4422 iface, debugstr_guid(riid), surface, device);
4424 hr = d3d7_CreateDevice(&This->IDirect3D7_iface, riid,
4425 surface_impl ? &surface_impl->IDirectDrawSurface7_iface : NULL, device ? &device7 : NULL);
4426 if (SUCCEEDED(hr))
4428 device_impl = impl_from_IDirect3DDevice7(device7);
4429 *device = &device_impl->IDirect3DDevice2_iface;
4432 return hr;
4435 /*****************************************************************************
4436 * IDirect3D7::CreateVertexBuffer
4438 * Creates a new vertex buffer object and returns a IDirect3DVertexBuffer7
4439 * interface.
4441 * Version 3 and 7
4443 * Params:
4444 * desc: Requested Vertex buffer properties
4445 * vertex_buffer: Address to return the interface pointer at
4446 * flags: Some flags, should be 0
4448 * Returns
4449 * D3D_OK on success
4450 * DDERR_OUTOFMEMORY if memory allocation failed
4451 * The return value of IWineD3DDevice::CreateVertexBuffer if this call fails
4452 * DDERR_INVALIDPARAMS if desc or vertex_buffer are NULL
4454 *****************************************************************************/
4455 static HRESULT WINAPI d3d7_CreateVertexBuffer(IDirect3D7 *iface, D3DVERTEXBUFFERDESC *desc,
4456 IDirect3DVertexBuffer7 **vertex_buffer, DWORD flags)
4458 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4459 IDirect3DVertexBufferImpl *object;
4460 HRESULT hr;
4462 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x.\n",
4463 iface, desc, vertex_buffer, flags);
4465 if (!vertex_buffer || !desc) return DDERR_INVALIDPARAMS;
4467 hr = d3d_vertex_buffer_create(&object, This, desc);
4468 if (hr == D3D_OK)
4470 TRACE("Created vertex buffer %p.\n", object);
4471 *vertex_buffer = &object->IDirect3DVertexBuffer7_iface;
4473 else
4474 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4476 return hr;
4479 static HRESULT WINAPI d3d3_CreateVertexBuffer(IDirect3D3 *iface, D3DVERTEXBUFFERDESC *desc,
4480 IDirect3DVertexBuffer **vertex_buffer, DWORD flags, IUnknown *outer_unknown)
4482 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4483 IDirect3DVertexBufferImpl *object;
4484 HRESULT hr;
4486 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x, outer_unknown %p.\n",
4487 iface, desc, vertex_buffer, flags, outer_unknown);
4489 if (outer_unknown)
4490 return CLASS_E_NOAGGREGATION;
4491 if (!vertex_buffer || !desc)
4492 return DDERR_INVALIDPARAMS;
4494 hr = d3d_vertex_buffer_create(&object, This, desc);
4495 if (hr == D3D_OK)
4497 TRACE("Created vertex buffer %p.\n", object);
4498 *vertex_buffer = &object->IDirect3DVertexBuffer_iface;
4500 else
4501 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4503 return hr;
4506 /*****************************************************************************
4507 * IDirect3D7::EnumZBufferFormats
4509 * Enumerates all supported Z buffer pixel formats
4511 * Version 3 and 7
4513 * Params:
4514 * device_iid:
4515 * callback: callback to call for each pixel format
4516 * context: Pointer to pass back to the callback
4518 * Returns:
4519 * D3D_OK on success
4520 * DDERR_INVALIDPARAMS if callback is NULL
4521 * For details, see IWineD3DDevice::EnumZBufferFormats
4523 *****************************************************************************/
4524 static HRESULT WINAPI d3d7_EnumZBufferFormats(IDirect3D7 *iface, REFCLSID device_iid,
4525 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4527 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4528 struct wined3d_display_mode mode;
4529 WINED3DDEVTYPE type;
4530 unsigned int i;
4531 HRESULT hr;
4533 /* Order matters. Specifically, BattleZone II (full version) expects the
4534 * 16-bit depth formats to be listed before the 24 and 32 ones. */
4535 static const enum wined3d_format_id formats[] =
4537 WINED3DFMT_S1_UINT_D15_UNORM,
4538 WINED3DFMT_D16_UNORM,
4539 WINED3DFMT_X8D24_UNORM,
4540 WINED3DFMT_S4X4_UINT_D24_UNORM,
4541 WINED3DFMT_D24_UNORM_S8_UINT,
4542 WINED3DFMT_D32_UNORM,
4545 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4546 iface, debugstr_guid(device_iid), callback, context);
4548 if (!callback) return DDERR_INVALIDPARAMS;
4550 if (IsEqualGUID(device_iid, &IID_IDirect3DHALDevice)
4551 || IsEqualGUID(device_iid, &IID_IDirect3DTnLHalDevice)
4552 || IsEqualGUID(device_iid, &IID_D3DDEVICE_WineD3D))
4554 TRACE("Asked for HAL device.\n");
4555 type = WINED3DDEVTYPE_HAL;
4557 else if (IsEqualGUID(device_iid, &IID_IDirect3DRGBDevice)
4558 || IsEqualGUID(device_iid, &IID_IDirect3DMMXDevice))
4560 TRACE("Asked for SW device.\n");
4561 type = WINED3DDEVTYPE_SW;
4563 else if (IsEqualGUID(device_iid, &IID_IDirect3DRefDevice))
4565 TRACE("Asked for REF device.\n");
4566 type = WINED3DDEVTYPE_REF;
4568 else if (IsEqualGUID(device_iid, &IID_IDirect3DNullDevice))
4570 TRACE("Asked for NULLREF device.\n");
4571 type = WINED3DDEVTYPE_NULLREF;
4573 else
4575 FIXME("Unexpected device GUID %s.\n", debugstr_guid(device_iid));
4576 type = WINED3DDEVTYPE_HAL;
4579 wined3d_mutex_lock();
4580 /* We need an adapter format from somewhere to please wined3d and WGL.
4581 * Use the current display mode. So far all cards offer the same depth
4582 * stencil format for all modes, but if some do not and applications do
4583 * not like that we'll have to find some workaround, like iterating over
4584 * all imaginable formats and collecting all the depth stencil formats we
4585 * can get. */
4586 hr = wined3d_device_get_display_mode(This->wined3d_device, 0, &mode);
4588 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); ++i)
4590 hr = wined3d_check_device_format(This->wineD3D, WINED3DADAPTER_DEFAULT, type, mode.format_id,
4591 WINED3DUSAGE_DEPTHSTENCIL, WINED3DRTYPE_SURFACE, formats[i], SURFACE_OPENGL);
4592 if (SUCCEEDED(hr))
4594 DDPIXELFORMAT pformat;
4596 memset(&pformat, 0, sizeof(pformat));
4597 pformat.dwSize = sizeof(pformat);
4598 PixelFormat_WineD3DtoDD(&pformat, formats[i]);
4600 TRACE("Enumerating wined3d format %#x.\n", formats[i]);
4601 hr = callback(&pformat, context);
4602 if (hr != DDENUMRET_OK)
4604 TRACE("Format enumeration cancelled by application.\n");
4605 wined3d_mutex_unlock();
4606 return D3D_OK;
4611 /* Historically some windows drivers used dwZBufferBitDepth=24 for WINED3DFMT_X8D24_UNORM,
4612 * while others used dwZBufferBitDepth=32. In either case the pitch matches a 32 bits per
4613 * pixel format, so we use dwZBufferBitDepth=32. Some games expect 24. Windows Vista and
4614 * newer enumerate both versions, so we do the same(bug 22434) */
4615 hr = wined3d_check_device_format(This->wineD3D, WINED3DADAPTER_DEFAULT, type, mode.format_id,
4616 WINED3DUSAGE_DEPTHSTENCIL, WINED3DRTYPE_SURFACE, WINED3DFMT_X8D24_UNORM, SURFACE_OPENGL);
4617 if (SUCCEEDED(hr))
4619 DDPIXELFORMAT x8d24 =
4621 sizeof(x8d24), DDPF_ZBUFFER, 0,
4622 {24}, {0x00000000}, {0x00ffffff}, {0x00000000}
4624 TRACE("Enumerating WINED3DFMT_X8D24_UNORM, dwZBufferBitDepth=24 version\n");
4625 callback(&x8d24, context);
4628 TRACE("End of enumeration.\n");
4630 wined3d_mutex_unlock();
4632 return D3D_OK;
4635 static HRESULT WINAPI d3d3_EnumZBufferFormats(IDirect3D3 *iface, REFCLSID device_iid,
4636 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4638 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4640 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4641 iface, debugstr_guid(device_iid), callback, context);
4643 return d3d7_EnumZBufferFormats(&This->IDirect3D7_iface, device_iid, callback, context);
4646 /*****************************************************************************
4647 * IDirect3D7::EvictManagedTextures
4649 * Removes all managed textures (=surfaces with DDSCAPS2_TEXTUREMANAGE or
4650 * DDSCAPS2_D3DTEXTUREMANAGE caps) to be removed from video memory.
4652 * Version 3 and 7
4654 * Returns:
4655 * D3D_OK, because it's a stub
4657 *****************************************************************************/
4658 static HRESULT WINAPI d3d7_EvictManagedTextures(IDirect3D7 *iface)
4660 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4662 TRACE("iface %p!\n", iface);
4664 wined3d_mutex_lock();
4665 if (This->d3d_initialized)
4666 wined3d_device_evict_managed_resources(This->wined3d_device);
4667 wined3d_mutex_unlock();
4669 return D3D_OK;
4672 static HRESULT WINAPI d3d3_EvictManagedTextures(IDirect3D3 *iface)
4674 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4676 TRACE("iface %p.\n", iface);
4678 return d3d7_EvictManagedTextures(&This->IDirect3D7_iface);
4681 /*****************************************************************************
4682 * IDirect3DImpl_GetCaps
4684 * This function retrieves the device caps from wined3d
4685 * and converts it into a D3D7 and D3D - D3D3 structure
4686 * This is a helper function called from various places in ddraw
4688 * Params:
4689 * wined3d: The interface to get the caps from
4690 * desc1: Old D3D <3 structure to fill (needed)
4691 * desc7: D3D7 device desc structure to fill (needed)
4693 * Returns
4694 * D3D_OK on success, or the return value of IWineD3D::GetCaps
4696 *****************************************************************************/
4697 HRESULT IDirect3DImpl_GetCaps(const struct wined3d *wined3d, D3DDEVICEDESC *desc1, D3DDEVICEDESC7 *desc7)
4699 WINED3DCAPS wined3d_caps;
4700 HRESULT hr;
4702 TRACE("wined3d %p, desc1 %p, desc7 %p.\n", wined3d, desc1, desc7);
4704 memset(&wined3d_caps, 0, sizeof(wined3d_caps));
4706 wined3d_mutex_lock();
4707 hr = wined3d_get_device_caps(wined3d, 0, WINED3DDEVTYPE_HAL, &wined3d_caps);
4708 wined3d_mutex_unlock();
4709 if (FAILED(hr))
4711 WARN("Failed to get device caps, hr %#x.\n", hr);
4712 return hr;
4715 /* Copy the results into the d3d7 and d3d3 structures */
4716 desc7->dwDevCaps = wined3d_caps.DevCaps;
4717 desc7->dpcLineCaps.dwMiscCaps = wined3d_caps.PrimitiveMiscCaps;
4718 desc7->dpcLineCaps.dwRasterCaps = wined3d_caps.RasterCaps;
4719 desc7->dpcLineCaps.dwZCmpCaps = wined3d_caps.ZCmpCaps;
4720 desc7->dpcLineCaps.dwSrcBlendCaps = wined3d_caps.SrcBlendCaps;
4721 desc7->dpcLineCaps.dwDestBlendCaps = wined3d_caps.DestBlendCaps;
4722 desc7->dpcLineCaps.dwAlphaCmpCaps = wined3d_caps.AlphaCmpCaps;
4723 desc7->dpcLineCaps.dwShadeCaps = wined3d_caps.ShadeCaps;
4724 desc7->dpcLineCaps.dwTextureCaps = wined3d_caps.TextureCaps;
4725 desc7->dpcLineCaps.dwTextureFilterCaps = wined3d_caps.TextureFilterCaps;
4726 desc7->dpcLineCaps.dwTextureAddressCaps = wined3d_caps.TextureAddressCaps;
4728 desc7->dwMaxTextureWidth = wined3d_caps.MaxTextureWidth;
4729 desc7->dwMaxTextureHeight = wined3d_caps.MaxTextureHeight;
4731 desc7->dwMaxTextureRepeat = wined3d_caps.MaxTextureRepeat;
4732 desc7->dwMaxTextureAspectRatio = wined3d_caps.MaxTextureAspectRatio;
4733 desc7->dwMaxAnisotropy = wined3d_caps.MaxAnisotropy;
4734 desc7->dvMaxVertexW = wined3d_caps.MaxVertexW;
4736 desc7->dvGuardBandLeft = wined3d_caps.GuardBandLeft;
4737 desc7->dvGuardBandTop = wined3d_caps.GuardBandTop;
4738 desc7->dvGuardBandRight = wined3d_caps.GuardBandRight;
4739 desc7->dvGuardBandBottom = wined3d_caps.GuardBandBottom;
4741 desc7->dvExtentsAdjust = wined3d_caps.ExtentsAdjust;
4742 desc7->dwStencilCaps = wined3d_caps.StencilCaps;
4744 desc7->dwFVFCaps = wined3d_caps.FVFCaps;
4745 desc7->dwTextureOpCaps = wined3d_caps.TextureOpCaps;
4747 desc7->dwVertexProcessingCaps = wined3d_caps.VertexProcessingCaps;
4748 desc7->dwMaxActiveLights = wined3d_caps.MaxActiveLights;
4750 /* Remove all non-d3d7 caps */
4751 desc7->dwDevCaps &= (
4752 D3DDEVCAPS_FLOATTLVERTEX | D3DDEVCAPS_SORTINCREASINGZ | D3DDEVCAPS_SORTDECREASINGZ |
4753 D3DDEVCAPS_SORTEXACT | D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY |
4754 D3DDEVCAPS_TLVERTEXSYSTEMMEMORY | D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY |
4755 D3DDEVCAPS_TEXTUREVIDEOMEMORY | D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP |
4756 D3DDEVCAPS_TEXTURENONLOCALVIDMEM | D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
4757 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT | D3DDEVCAPS_CANBLTSYSTONONLOCAL |
4758 D3DDEVCAPS_HWRASTERIZATION);
4760 desc7->dwStencilCaps &= (
4761 D3DSTENCILCAPS_KEEP | D3DSTENCILCAPS_ZERO | D3DSTENCILCAPS_REPLACE |
4762 D3DSTENCILCAPS_INCRSAT | D3DSTENCILCAPS_DECRSAT | D3DSTENCILCAPS_INVERT |
4763 D3DSTENCILCAPS_INCR | D3DSTENCILCAPS_DECR);
4765 /* FVF caps ?*/
4767 desc7->dwTextureOpCaps &= (
4768 D3DTEXOPCAPS_DISABLE | D3DTEXOPCAPS_SELECTARG1 | D3DTEXOPCAPS_SELECTARG2 |
4769 D3DTEXOPCAPS_MODULATE | D3DTEXOPCAPS_MODULATE2X | D3DTEXOPCAPS_MODULATE4X |
4770 D3DTEXOPCAPS_ADD | D3DTEXOPCAPS_ADDSIGNED | D3DTEXOPCAPS_ADDSIGNED2X |
4771 D3DTEXOPCAPS_SUBTRACT | D3DTEXOPCAPS_ADDSMOOTH | D3DTEXOPCAPS_BLENDTEXTUREALPHA |
4772 D3DTEXOPCAPS_BLENDFACTORALPHA | D3DTEXOPCAPS_BLENDTEXTUREALPHAPM | D3DTEXOPCAPS_BLENDCURRENTALPHA |
4773 D3DTEXOPCAPS_PREMODULATE | D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
4774 D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA | D3DTEXOPCAPS_BUMPENVMAP |
4775 D3DTEXOPCAPS_BUMPENVMAPLUMINANCE | D3DTEXOPCAPS_DOTPRODUCT3);
4777 desc7->dwVertexProcessingCaps &= (
4778 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_VERTEXFOG |
4779 D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER);
4781 desc7->dpcLineCaps.dwMiscCaps &= (
4782 D3DPMISCCAPS_MASKPLANES | D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP |
4783 D3DPMISCCAPS_CONFORMANT | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW |
4784 D3DPMISCCAPS_CULLCCW);
4786 desc7->dpcLineCaps.dwRasterCaps &= (
4787 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ROP2 | D3DPRASTERCAPS_XOR |
4788 D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_SUBPIXEL |
4789 D3DPRASTERCAPS_SUBPIXELX | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE |
4790 D3DPRASTERCAPS_STIPPLE | D3DPRASTERCAPS_ANTIALIASSORTDEPENDENT | D3DPRASTERCAPS_ANTIALIASSORTINDEPENDENT |
4791 D3DPRASTERCAPS_ANTIALIASEDGES | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBIAS |
4792 D3DPRASTERCAPS_ZBUFFERLESSHSR | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY |
4793 D3DPRASTERCAPS_WBUFFER | D3DPRASTERCAPS_TRANSLUCENTSORTINDEPENDENT | D3DPRASTERCAPS_WFOG |
4794 D3DPRASTERCAPS_ZFOG);
4796 desc7->dpcLineCaps.dwZCmpCaps &= (
4797 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
4798 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
4799 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
4801 desc7->dpcLineCaps.dwSrcBlendCaps &= (
4802 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
4803 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
4804 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
4805 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
4806 D3DPBLENDCAPS_BOTHINVSRCALPHA);
4808 desc7->dpcLineCaps.dwDestBlendCaps &= (
4809 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
4810 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
4811 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
4812 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
4813 D3DPBLENDCAPS_BOTHINVSRCALPHA);
4815 desc7->dpcLineCaps.dwAlphaCmpCaps &= (
4816 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
4817 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
4818 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
4820 desc7->dpcLineCaps.dwShadeCaps &= (
4821 D3DPSHADECAPS_COLORFLATMONO | D3DPSHADECAPS_COLORFLATRGB | D3DPSHADECAPS_COLORGOURAUDMONO |
4822 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_COLORPHONGMONO | D3DPSHADECAPS_COLORPHONGRGB |
4823 D3DPSHADECAPS_SPECULARFLATMONO | D3DPSHADECAPS_SPECULARFLATRGB | D3DPSHADECAPS_SPECULARGOURAUDMONO |
4824 D3DPSHADECAPS_SPECULARGOURAUDRGB | D3DPSHADECAPS_SPECULARPHONGMONO | D3DPSHADECAPS_SPECULARPHONGRGB |
4825 D3DPSHADECAPS_ALPHAFLATBLEND | D3DPSHADECAPS_ALPHAFLATSTIPPLED | D3DPSHADECAPS_ALPHAGOURAUDBLEND |
4826 D3DPSHADECAPS_ALPHAGOURAUDSTIPPLED | D3DPSHADECAPS_ALPHAPHONGBLEND | D3DPSHADECAPS_ALPHAPHONGSTIPPLED |
4827 D3DPSHADECAPS_FOGFLAT | D3DPSHADECAPS_FOGGOURAUD | D3DPSHADECAPS_FOGPHONG);
4829 desc7->dpcLineCaps.dwTextureCaps &= (
4830 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
4831 D3DPTEXTURECAPS_TRANSPARENCY | D3DPTEXTURECAPS_BORDER | D3DPTEXTURECAPS_SQUAREONLY |
4832 D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE | D3DPTEXTURECAPS_ALPHAPALETTE| D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
4833 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_COLORKEYBLEND);
4835 desc7->dpcLineCaps.dwTextureFilterCaps &= (
4836 D3DPTFILTERCAPS_NEAREST | D3DPTFILTERCAPS_LINEAR | D3DPTFILTERCAPS_MIPNEAREST |
4837 D3DPTFILTERCAPS_MIPLINEAR | D3DPTFILTERCAPS_LINEARMIPNEAREST | D3DPTFILTERCAPS_LINEARMIPLINEAR |
4838 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
4839 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
4840 D3DPTFILTERCAPS_MAGFLINEAR | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC |
4841 D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC);
4843 desc7->dpcLineCaps.dwTextureBlendCaps &= (
4844 D3DPTBLENDCAPS_DECAL | D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_DECALALPHA |
4845 D3DPTBLENDCAPS_MODULATEALPHA | D3DPTBLENDCAPS_DECALMASK | D3DPTBLENDCAPS_MODULATEMASK |
4846 D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_ADD);
4848 desc7->dpcLineCaps.dwTextureAddressCaps &= (
4849 D3DPTADDRESSCAPS_WRAP | D3DPTADDRESSCAPS_MIRROR | D3DPTADDRESSCAPS_CLAMP |
4850 D3DPTADDRESSCAPS_BORDER | D3DPTADDRESSCAPS_INDEPENDENTUV);
4852 if (!(desc7->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2))
4854 /* DirectX7 always has the np2 flag set, no matter what the card
4855 * supports. Some old games (Rollcage) check the caps incorrectly.
4856 * If wined3d supports nonpow2 textures it also has np2 conditional
4857 * support. */
4858 desc7->dpcLineCaps.dwTextureCaps |= D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL;
4861 /* Fill the missing members, and do some fixup */
4862 desc7->dpcLineCaps.dwSize = sizeof(desc7->dpcLineCaps);
4863 desc7->dpcLineCaps.dwTextureBlendCaps = D3DPTBLENDCAPS_ADD | D3DPTBLENDCAPS_MODULATEMASK |
4864 D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_DECAL |
4865 D3DPTBLENDCAPS_DECALALPHA | D3DPTBLENDCAPS_DECALMASK |
4866 D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_MODULATEALPHA;
4867 desc7->dpcLineCaps.dwStippleWidth = 32;
4868 desc7->dpcLineCaps.dwStippleHeight = 32;
4869 /* Use the same for the TriCaps */
4870 desc7->dpcTriCaps = desc7->dpcLineCaps;
4872 desc7->dwDeviceRenderBitDepth = DDBD_16 | DDBD_24 | DDBD_32;
4873 desc7->dwDeviceZBufferBitDepth = DDBD_16 | DDBD_24;
4874 desc7->dwMinTextureWidth = 1;
4875 desc7->dwMinTextureHeight = 1;
4877 /* Convert DWORDs safely to WORDs */
4878 if (wined3d_caps.MaxTextureBlendStages > 0xffff) desc7->wMaxTextureBlendStages = 0xffff;
4879 else desc7->wMaxTextureBlendStages = (WORD)wined3d_caps.MaxTextureBlendStages;
4880 if (wined3d_caps.MaxSimultaneousTextures > 0xffff) desc7->wMaxSimultaneousTextures = 0xffff;
4881 else desc7->wMaxSimultaneousTextures = (WORD)wined3d_caps.MaxSimultaneousTextures;
4883 if (wined3d_caps.MaxUserClipPlanes > 0xffff) desc7->wMaxUserClipPlanes = 0xffff;
4884 else desc7->wMaxUserClipPlanes = (WORD)wined3d_caps.MaxUserClipPlanes;
4885 if (wined3d_caps.MaxVertexBlendMatrices > 0xffff) desc7->wMaxVertexBlendMatrices = 0xffff;
4886 else desc7->wMaxVertexBlendMatrices = (WORD)wined3d_caps.MaxVertexBlendMatrices;
4888 desc7->deviceGUID = IID_IDirect3DTnLHalDevice;
4890 desc7->dwReserved1 = 0;
4891 desc7->dwReserved2 = 0;
4892 desc7->dwReserved3 = 0;
4893 desc7->dwReserved4 = 0;
4895 /* Fill the old structure */
4896 memset(desc1, 0, sizeof(*desc1));
4897 desc1->dwSize = sizeof(D3DDEVICEDESC);
4898 desc1->dwFlags = D3DDD_COLORMODEL
4899 | D3DDD_DEVCAPS
4900 | D3DDD_TRANSFORMCAPS
4901 | D3DDD_BCLIPPING
4902 | D3DDD_LIGHTINGCAPS
4903 | D3DDD_LINECAPS
4904 | D3DDD_TRICAPS
4905 | D3DDD_DEVICERENDERBITDEPTH
4906 | D3DDD_DEVICEZBUFFERBITDEPTH
4907 | D3DDD_MAXBUFFERSIZE
4908 | D3DDD_MAXVERTEXCOUNT;
4910 desc1->dcmColorModel = D3DCOLOR_RGB;
4911 desc1->dwDevCaps = desc7->dwDevCaps;
4912 desc1->dtcTransformCaps.dwSize = sizeof(D3DTRANSFORMCAPS);
4913 desc1->dtcTransformCaps.dwCaps = D3DTRANSFORMCAPS_CLIP;
4914 desc1->bClipping = TRUE;
4915 desc1->dlcLightingCaps.dwSize = sizeof(D3DLIGHTINGCAPS);
4916 desc1->dlcLightingCaps.dwCaps = D3DLIGHTCAPS_DIRECTIONAL
4917 | D3DLIGHTCAPS_PARALLELPOINT
4918 | D3DLIGHTCAPS_POINT
4919 | D3DLIGHTCAPS_SPOT;
4921 desc1->dlcLightingCaps.dwLightingModel = D3DLIGHTINGMODEL_RGB;
4922 desc1->dlcLightingCaps.dwNumLights = desc7->dwMaxActiveLights;
4924 desc1->dpcLineCaps.dwSize = sizeof(D3DPRIMCAPS);
4925 desc1->dpcLineCaps.dwMiscCaps = desc7->dpcLineCaps.dwMiscCaps;
4926 desc1->dpcLineCaps.dwRasterCaps = desc7->dpcLineCaps.dwRasterCaps;
4927 desc1->dpcLineCaps.dwZCmpCaps = desc7->dpcLineCaps.dwZCmpCaps;
4928 desc1->dpcLineCaps.dwSrcBlendCaps = desc7->dpcLineCaps.dwSrcBlendCaps;
4929 desc1->dpcLineCaps.dwDestBlendCaps = desc7->dpcLineCaps.dwDestBlendCaps;
4930 desc1->dpcLineCaps.dwShadeCaps = desc7->dpcLineCaps.dwShadeCaps;
4931 desc1->dpcLineCaps.dwTextureCaps = desc7->dpcLineCaps.dwTextureCaps;
4932 desc1->dpcLineCaps.dwTextureFilterCaps = desc7->dpcLineCaps.dwTextureFilterCaps;
4933 desc1->dpcLineCaps.dwTextureBlendCaps = desc7->dpcLineCaps.dwTextureBlendCaps;
4934 desc1->dpcLineCaps.dwTextureAddressCaps = desc7->dpcLineCaps.dwTextureAddressCaps;
4935 desc1->dpcLineCaps.dwStippleWidth = desc7->dpcLineCaps.dwStippleWidth;
4936 desc1->dpcLineCaps.dwAlphaCmpCaps = desc7->dpcLineCaps.dwAlphaCmpCaps;
4938 desc1->dpcTriCaps.dwSize = sizeof(D3DPRIMCAPS);
4939 desc1->dpcTriCaps.dwMiscCaps = desc7->dpcTriCaps.dwMiscCaps;
4940 desc1->dpcTriCaps.dwRasterCaps = desc7->dpcTriCaps.dwRasterCaps;
4941 desc1->dpcTriCaps.dwZCmpCaps = desc7->dpcTriCaps.dwZCmpCaps;
4942 desc1->dpcTriCaps.dwSrcBlendCaps = desc7->dpcTriCaps.dwSrcBlendCaps;
4943 desc1->dpcTriCaps.dwDestBlendCaps = desc7->dpcTriCaps.dwDestBlendCaps;
4944 desc1->dpcTriCaps.dwShadeCaps = desc7->dpcTriCaps.dwShadeCaps;
4945 desc1->dpcTriCaps.dwTextureCaps = desc7->dpcTriCaps.dwTextureCaps;
4946 desc1->dpcTriCaps.dwTextureFilterCaps = desc7->dpcTriCaps.dwTextureFilterCaps;
4947 desc1->dpcTriCaps.dwTextureBlendCaps = desc7->dpcTriCaps.dwTextureBlendCaps;
4948 desc1->dpcTriCaps.dwTextureAddressCaps = desc7->dpcTriCaps.dwTextureAddressCaps;
4949 desc1->dpcTriCaps.dwStippleWidth = desc7->dpcTriCaps.dwStippleWidth;
4950 desc1->dpcTriCaps.dwAlphaCmpCaps = desc7->dpcTriCaps.dwAlphaCmpCaps;
4952 desc1->dwDeviceRenderBitDepth = desc7->dwDeviceRenderBitDepth;
4953 desc1->dwDeviceZBufferBitDepth = desc7->dwDeviceZBufferBitDepth;
4954 desc1->dwMaxBufferSize = 0;
4955 desc1->dwMaxVertexCount = 65536;
4956 desc1->dwMinTextureWidth = desc7->dwMinTextureWidth;
4957 desc1->dwMinTextureHeight = desc7->dwMinTextureHeight;
4958 desc1->dwMaxTextureWidth = desc7->dwMaxTextureWidth;
4959 desc1->dwMaxTextureHeight = desc7->dwMaxTextureHeight;
4960 desc1->dwMinStippleWidth = 1;
4961 desc1->dwMinStippleHeight = 1;
4962 desc1->dwMaxStippleWidth = 32;
4963 desc1->dwMaxStippleHeight = 32;
4964 desc1->dwMaxTextureRepeat = desc7->dwMaxTextureRepeat;
4965 desc1->dwMaxTextureAspectRatio = desc7->dwMaxTextureAspectRatio;
4966 desc1->dwMaxAnisotropy = desc7->dwMaxAnisotropy;
4967 desc1->dvGuardBandLeft = desc7->dvGuardBandLeft;
4968 desc1->dvGuardBandRight = desc7->dvGuardBandRight;
4969 desc1->dvGuardBandTop = desc7->dvGuardBandTop;
4970 desc1->dvGuardBandBottom = desc7->dvGuardBandBottom;
4971 desc1->dvExtentsAdjust = desc7->dvExtentsAdjust;
4972 desc1->dwStencilCaps = desc7->dwStencilCaps;
4973 desc1->dwFVFCaps = desc7->dwFVFCaps;
4974 desc1->dwTextureOpCaps = desc7->dwTextureOpCaps;
4975 desc1->wMaxTextureBlendStages = desc7->wMaxTextureBlendStages;
4976 desc1->wMaxSimultaneousTextures = desc7->wMaxSimultaneousTextures;
4978 return DD_OK;
4981 /*****************************************************************************
4982 * IDirectDraw7 VTable
4983 *****************************************************************************/
4984 static const struct IDirectDraw7Vtbl ddraw7_vtbl =
4986 /* IUnknown */
4987 ddraw7_QueryInterface,
4988 ddraw7_AddRef,
4989 ddraw7_Release,
4990 /* IDirectDraw */
4991 ddraw7_Compact,
4992 ddraw7_CreateClipper,
4993 ddraw7_CreatePalette,
4994 ddraw7_CreateSurface,
4995 ddraw7_DuplicateSurface,
4996 ddraw7_EnumDisplayModes,
4997 ddraw7_EnumSurfaces,
4998 ddraw7_FlipToGDISurface,
4999 ddraw7_GetCaps,
5000 ddraw7_GetDisplayMode,
5001 ddraw7_GetFourCCCodes,
5002 ddraw7_GetGDISurface,
5003 ddraw7_GetMonitorFrequency,
5004 ddraw7_GetScanLine,
5005 ddraw7_GetVerticalBlankStatus,
5006 ddraw7_Initialize,
5007 ddraw7_RestoreDisplayMode,
5008 ddraw7_SetCooperativeLevel,
5009 ddraw7_SetDisplayMode,
5010 ddraw7_WaitForVerticalBlank,
5011 /* IDirectDraw2 */
5012 ddraw7_GetAvailableVidMem,
5013 /* IDirectDraw3 */
5014 ddraw7_GetSurfaceFromDC,
5015 /* IDirectDraw4 */
5016 ddraw7_RestoreAllSurfaces,
5017 ddraw7_TestCooperativeLevel,
5018 ddraw7_GetDeviceIdentifier,
5019 /* IDirectDraw7 */
5020 ddraw7_StartModeTest,
5021 ddraw7_EvaluateMode
5024 static const struct IDirectDraw4Vtbl ddraw4_vtbl =
5026 /* IUnknown */
5027 ddraw4_QueryInterface,
5028 ddraw4_AddRef,
5029 ddraw4_Release,
5030 /* IDirectDraw */
5031 ddraw4_Compact,
5032 ddraw4_CreateClipper,
5033 ddraw4_CreatePalette,
5034 ddraw4_CreateSurface,
5035 ddraw4_DuplicateSurface,
5036 ddraw4_EnumDisplayModes,
5037 ddraw4_EnumSurfaces,
5038 ddraw4_FlipToGDISurface,
5039 ddraw4_GetCaps,
5040 ddraw4_GetDisplayMode,
5041 ddraw4_GetFourCCCodes,
5042 ddraw4_GetGDISurface,
5043 ddraw4_GetMonitorFrequency,
5044 ddraw4_GetScanLine,
5045 ddraw4_GetVerticalBlankStatus,
5046 ddraw4_Initialize,
5047 ddraw4_RestoreDisplayMode,
5048 ddraw4_SetCooperativeLevel,
5049 ddraw4_SetDisplayMode,
5050 ddraw4_WaitForVerticalBlank,
5051 /* IDirectDraw2 */
5052 ddraw4_GetAvailableVidMem,
5053 /* IDirectDraw3 */
5054 ddraw4_GetSurfaceFromDC,
5055 /* IDirectDraw4 */
5056 ddraw4_RestoreAllSurfaces,
5057 ddraw4_TestCooperativeLevel,
5058 ddraw4_GetDeviceIdentifier,
5061 static const struct IDirectDraw2Vtbl ddraw2_vtbl =
5063 /* IUnknown */
5064 ddraw2_QueryInterface,
5065 ddraw2_AddRef,
5066 ddraw2_Release,
5067 /* IDirectDraw */
5068 ddraw2_Compact,
5069 ddraw2_CreateClipper,
5070 ddraw2_CreatePalette,
5071 ddraw2_CreateSurface,
5072 ddraw2_DuplicateSurface,
5073 ddraw2_EnumDisplayModes,
5074 ddraw2_EnumSurfaces,
5075 ddraw2_FlipToGDISurface,
5076 ddraw2_GetCaps,
5077 ddraw2_GetDisplayMode,
5078 ddraw2_GetFourCCCodes,
5079 ddraw2_GetGDISurface,
5080 ddraw2_GetMonitorFrequency,
5081 ddraw2_GetScanLine,
5082 ddraw2_GetVerticalBlankStatus,
5083 ddraw2_Initialize,
5084 ddraw2_RestoreDisplayMode,
5085 ddraw2_SetCooperativeLevel,
5086 ddraw2_SetDisplayMode,
5087 ddraw2_WaitForVerticalBlank,
5088 /* IDirectDraw2 */
5089 ddraw2_GetAvailableVidMem,
5092 static const struct IDirectDrawVtbl ddraw1_vtbl =
5094 /* IUnknown */
5095 ddraw1_QueryInterface,
5096 ddraw1_AddRef,
5097 ddraw1_Release,
5098 /* IDirectDraw */
5099 ddraw1_Compact,
5100 ddraw1_CreateClipper,
5101 ddraw1_CreatePalette,
5102 ddraw1_CreateSurface,
5103 ddraw1_DuplicateSurface,
5104 ddraw1_EnumDisplayModes,
5105 ddraw1_EnumSurfaces,
5106 ddraw1_FlipToGDISurface,
5107 ddraw1_GetCaps,
5108 ddraw1_GetDisplayMode,
5109 ddraw1_GetFourCCCodes,
5110 ddraw1_GetGDISurface,
5111 ddraw1_GetMonitorFrequency,
5112 ddraw1_GetScanLine,
5113 ddraw1_GetVerticalBlankStatus,
5114 ddraw1_Initialize,
5115 ddraw1_RestoreDisplayMode,
5116 ddraw1_SetCooperativeLevel,
5117 ddraw1_SetDisplayMode,
5118 ddraw1_WaitForVerticalBlank,
5121 static const struct IDirect3D7Vtbl d3d7_vtbl =
5123 /* IUnknown methods */
5124 d3d7_QueryInterface,
5125 d3d7_AddRef,
5126 d3d7_Release,
5127 /* IDirect3D7 methods */
5128 d3d7_EnumDevices,
5129 d3d7_CreateDevice,
5130 d3d7_CreateVertexBuffer,
5131 d3d7_EnumZBufferFormats,
5132 d3d7_EvictManagedTextures
5135 static const struct IDirect3D3Vtbl d3d3_vtbl =
5137 /* IUnknown methods */
5138 d3d3_QueryInterface,
5139 d3d3_AddRef,
5140 d3d3_Release,
5141 /* IDirect3D3 methods */
5142 d3d3_EnumDevices,
5143 d3d3_CreateLight,
5144 d3d3_CreateMaterial,
5145 d3d3_CreateViewport,
5146 d3d3_FindDevice,
5147 d3d3_CreateDevice,
5148 d3d3_CreateVertexBuffer,
5149 d3d3_EnumZBufferFormats,
5150 d3d3_EvictManagedTextures
5153 static const struct IDirect3D2Vtbl d3d2_vtbl =
5155 /* IUnknown methods */
5156 d3d2_QueryInterface,
5157 d3d2_AddRef,
5158 d3d2_Release,
5159 /* IDirect3D2 methods */
5160 d3d2_EnumDevices,
5161 d3d2_CreateLight,
5162 d3d2_CreateMaterial,
5163 d3d2_CreateViewport,
5164 d3d2_FindDevice,
5165 d3d2_CreateDevice
5168 static const struct IDirect3DVtbl d3d1_vtbl =
5170 /* IUnknown methods */
5171 d3d1_QueryInterface,
5172 d3d1_AddRef,
5173 d3d1_Release,
5174 /* IDirect3D methods */
5175 d3d1_Initialize,
5176 d3d1_EnumDevices,
5177 d3d1_CreateLight,
5178 d3d1_CreateMaterial,
5179 d3d1_CreateViewport,
5180 d3d1_FindDevice
5183 /*****************************************************************************
5184 * ddraw_find_decl
5186 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
5187 * if none was found.
5189 * This function is in ddraw.c and the DDraw object space because D3D7
5190 * vertex buffers are created using the IDirect3D interface to the ddraw
5191 * object, so they can be valid across D3D devices(theoretically. The ddraw
5192 * object also owns the wined3d device
5194 * Parameters:
5195 * This: Device
5196 * fvf: Fvf to find the decl for
5198 * Returns:
5199 * NULL in case of an error, the vertex declaration for the FVF otherwise.
5201 *****************************************************************************/
5202 struct wined3d_vertex_declaration *ddraw_find_decl(IDirectDrawImpl *This, DWORD fvf)
5204 struct wined3d_vertex_declaration *pDecl = NULL;
5205 HRESULT hr;
5206 int p, low, high; /* deliberately signed */
5207 struct FvfToDecl *convertedDecls = This->decls;
5209 TRACE("Searching for declaration for fvf %08x... ", fvf);
5211 low = 0;
5212 high = This->numConvertedDecls - 1;
5213 while(low <= high) {
5214 p = (low + high) >> 1;
5215 TRACE("%d ", p);
5216 if(convertedDecls[p].fvf == fvf) {
5217 TRACE("found %p\n", convertedDecls[p].decl);
5218 return convertedDecls[p].decl;
5219 } else if(convertedDecls[p].fvf < fvf) {
5220 low = p + 1;
5221 } else {
5222 high = p - 1;
5225 TRACE("not found. Creating and inserting at position %d.\n", low);
5227 hr = wined3d_vertex_declaration_create_from_fvf(This->wined3d_device,
5228 fvf, This, &ddraw_null_wined3d_parent_ops, &pDecl);
5229 if (hr != S_OK) return NULL;
5231 if(This->declArraySize == This->numConvertedDecls) {
5232 int grow = max(This->declArraySize / 2, 8);
5233 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
5234 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
5235 if (!convertedDecls)
5237 wined3d_vertex_declaration_decref(pDecl);
5238 return NULL;
5240 This->decls = convertedDecls;
5241 This->declArraySize += grow;
5244 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
5245 convertedDecls[low].decl = pDecl;
5246 convertedDecls[low].fvf = fvf;
5247 This->numConvertedDecls++;
5249 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
5250 return pDecl;
5253 static inline struct IDirectDrawImpl *ddraw_from_device_parent(struct wined3d_device_parent *device_parent)
5255 return CONTAINING_RECORD(device_parent, struct IDirectDrawImpl, device_parent);
5258 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
5259 struct wined3d_device *device)
5261 TRACE("device_parent %p, device %p.\n", device_parent, device);
5264 static HRESULT CDECL device_parent_create_surface(struct wined3d_device_parent *device_parent,
5265 void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
5266 WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, struct wined3d_surface **surface)
5268 struct IDirectDrawImpl *ddraw = ddraw_from_device_parent(device_parent);
5269 IDirectDrawSurfaceImpl *surf = NULL;
5270 UINT i = 0;
5271 DDSCAPS2 searchcaps = ddraw->tex_root->surface_desc.ddsCaps;
5273 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
5274 "\tpool %#x, level %u, face %u, surface %p.\n",
5275 device_parent, container_parent, width, height, format, usage, pool, level, face, surface);
5277 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
5278 switch(face)
5280 case WINED3DCUBEMAP_FACE_POSITIVE_X:
5281 TRACE("Asked for positive x\n");
5282 if (searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5284 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
5286 surf = ddraw->tex_root; break;
5287 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
5288 TRACE("Asked for negative x\n");
5289 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
5290 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
5291 TRACE("Asked for positive y\n");
5292 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
5293 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
5294 TRACE("Asked for negative y\n");
5295 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
5296 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
5297 TRACE("Asked for positive z\n");
5298 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
5299 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
5300 TRACE("Asked for negative z\n");
5301 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
5302 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
5305 if (!surf)
5307 IDirectDrawSurface7 *attached;
5308 IDirectDrawSurface7_GetAttachedSurface(&ddraw->tex_root->IDirectDrawSurface7_iface, &searchcaps, &attached);
5309 surf = unsafe_impl_from_IDirectDrawSurface7(attached);
5310 IDirectDrawSurface7_Release(attached);
5312 if (!surf) ERR("root search surface not found\n");
5314 /* Find the wanted mipmap. There are enough mipmaps in the chain */
5315 while (i < level)
5317 IDirectDrawSurface7 *attached;
5318 IDirectDrawSurface7_GetAttachedSurface(&surf->IDirectDrawSurface7_iface, &searchcaps, &attached);
5319 if(!attached) ERR("Surface not found\n");
5320 surf = impl_from_IDirectDrawSurface7(attached);
5321 IDirectDrawSurface7_Release(attached);
5322 ++i;
5325 /* Return the surface */
5326 *surface = surf->wined3d_surface;
5327 wined3d_surface_incref(*surface);
5329 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, surf);
5331 return D3D_OK;
5334 static void STDMETHODCALLTYPE ddraw_frontbuffer_destroyed(void *parent)
5336 struct IDirectDrawImpl *ddraw = parent;
5337 ddraw->wined3d_frontbuffer = NULL;
5340 static const struct wined3d_parent_ops ddraw_frontbuffer_parent_ops =
5342 ddraw_frontbuffer_destroyed,
5345 static HRESULT CDECL device_parent_create_rendertarget(struct wined3d_device_parent *device_parent,
5346 void *container_parent, UINT width, UINT height, enum wined3d_format_id format,
5347 WINED3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, BOOL lockable,
5348 struct wined3d_surface **surface)
5350 struct IDirectDrawImpl *ddraw = ddraw_from_device_parent(device_parent);
5351 HRESULT hr;
5353 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
5354 "\tmultisample_quality %u, lockable %u, surface %p.\n",
5355 device_parent, container_parent, width, height, format, multisample_type,
5356 multisample_quality, lockable, surface);
5358 if (ddraw->wined3d_frontbuffer)
5360 ERR("Frontbuffer already created.\n");
5361 return E_FAIL;
5364 hr = wined3d_surface_create(ddraw->wined3d_device, width, height, format, lockable, FALSE, 0,
5365 WINED3DUSAGE_RENDERTARGET, WINED3DPOOL_DEFAULT, multisample_type, multisample_quality,
5366 DefaultSurfaceType, ddraw, &ddraw_frontbuffer_parent_ops, surface);
5367 if (SUCCEEDED(hr))
5368 ddraw->wined3d_frontbuffer = *surface;
5370 return hr;
5373 static HRESULT CDECL device_parent_create_depth_stencil(struct wined3d_device_parent *device_parent,
5374 UINT width, UINT height, enum wined3d_format_id format, WINED3DMULTISAMPLE_TYPE multisample_type,
5375 DWORD multisample_quality, BOOL discard, struct wined3d_surface **surface)
5377 ERR("DirectDraw doesn't have and shouldn't try creating implicit depth buffers.\n");
5378 return E_NOTIMPL;
5381 static HRESULT CDECL device_parent_create_volume(struct wined3d_device_parent *device_parent,
5382 void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
5383 WINED3DPOOL pool, DWORD usage, struct wined3d_volume **volume)
5385 TRACE("device_parent %p, container_parent %p, width %u, height %u, depth %u, "
5386 "format %#x, pool %#x, usage %#x, volume %p.\n",
5387 device_parent, container_parent, width, height, depth,
5388 format, pool, usage, volume);
5390 ERR("Not implemented!\n");
5392 return E_NOTIMPL;
5395 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
5396 WINED3DPRESENT_PARAMETERS *present_parameters, struct wined3d_swapchain **swapchain)
5398 struct IDirectDrawImpl *ddraw = ddraw_from_device_parent(device_parent);
5399 HRESULT hr;
5401 TRACE("device_parent %p, present_parameters %p, swapchain %p.\n", device_parent, present_parameters, swapchain);
5403 if (ddraw->wined3d_swapchain)
5405 ERR("Swapchain already created.\n");
5406 return E_FAIL;
5409 hr = wined3d_swapchain_create(ddraw->wined3d_device, present_parameters,
5410 DefaultSurfaceType, NULL, &ddraw_null_wined3d_parent_ops, swapchain);
5411 if (FAILED(hr))
5412 WARN("Failed to create swapchain, hr %#x.\n", hr);
5414 return hr;
5417 static const struct wined3d_device_parent_ops ddraw_wined3d_device_parent_ops =
5419 device_parent_wined3d_device_created,
5420 device_parent_create_surface,
5421 device_parent_create_rendertarget,
5422 device_parent_create_depth_stencil,
5423 device_parent_create_volume,
5424 device_parent_create_swapchain,
5427 HRESULT ddraw_init(IDirectDrawImpl *ddraw, WINED3DDEVTYPE device_type)
5429 HRESULT hr;
5430 HDC hDC;
5432 ddraw->IDirectDraw7_iface.lpVtbl = &ddraw7_vtbl;
5433 ddraw->IDirectDraw_iface.lpVtbl = &ddraw1_vtbl;
5434 ddraw->IDirectDraw2_iface.lpVtbl = &ddraw2_vtbl;
5435 ddraw->IDirectDraw4_iface.lpVtbl = &ddraw4_vtbl;
5436 ddraw->IDirect3D_iface.lpVtbl = &d3d1_vtbl;
5437 ddraw->IDirect3D2_iface.lpVtbl = &d3d2_vtbl;
5438 ddraw->IDirect3D3_iface.lpVtbl = &d3d3_vtbl;
5439 ddraw->IDirect3D7_iface.lpVtbl = &d3d7_vtbl;
5440 ddraw->device_parent.ops = &ddraw_wined3d_device_parent_ops;
5441 ddraw->numIfaces = 1;
5442 ddraw->ref7 = 1;
5444 /* Get the current screen settings. */
5445 hDC = GetDC(0);
5446 ddraw->orig_bpp = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
5447 ReleaseDC(0, hDC);
5448 ddraw->orig_width = GetSystemMetrics(SM_CXSCREEN);
5449 ddraw->orig_height = GetSystemMetrics(SM_CYSCREEN);
5451 ddraw->wineD3D = wined3d_create(7, WINED3D_LEGACY_DEPTH_BIAS,
5452 &ddraw->IDirectDraw7_iface);
5453 if (!ddraw->wineD3D)
5455 WARN("Failed to create a wined3d object.\n");
5456 return E_OUTOFMEMORY;
5459 hr = wined3d_device_create(ddraw->wineD3D, WINED3DADAPTER_DEFAULT, device_type,
5460 NULL, 0, 8, &ddraw->device_parent, &ddraw->wined3d_device);
5461 if (FAILED(hr))
5463 WARN("Failed to create a wined3d device, hr %#x.\n", hr);
5464 wined3d_decref(ddraw->wineD3D);
5465 return hr;
5468 list_init(&ddraw->surface_list);
5470 return DD_OK;