mshtml: Added IHTMLBodyElement::onload property implementation.
[wine] / dlls / ddraw / ddraw.c
1 /*
2  * Copyright 1997-2000 Marcus Meissner
3  * Copyright 1998-2000 Lionel Ulmer
4  * Copyright 2000-2001 TransGaming Technologies Inc.
5  * Copyright 2006 Stefan Dösinger
6  * Copyright 2008 Denver Gingerich
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include "ddraw_private.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
29
30 /* Device identifier. Don't relay it to WineD3D */
31 static const DDDEVICEIDENTIFIER2 deviceidentifier =
32 {
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}},
39     0
40 };
41
42 static struct enum_device_entry
43 {
44     char interface_name[100];
45     char device_name[100];
46     const GUID *device_guid;
47 } device_list7[] =
48 {
49     /* T&L HAL device */
50     {
51         "WINE Direct3D7 Hardware Transform and Lighting acceleration using WineD3D",
52         "Wine D3D7 T&L HAL",
53         &IID_IDirect3DTnLHalDevice,
54     },
55
56     /* HAL device */
57     {
58         "WINE Direct3D7 Hardware acceleration using WineD3D",
59         "Direct3D HAL",
60         &IID_IDirect3DHALDevice,
61     },
62
63     /* RGB device */
64     {
65         "WINE Direct3D7 RGB Software Emulation using WineD3D",
66         "Wine D3D7 RGB",
67         &IID_IDirect3DRGBDevice,
68     },
69 };
70
71 static void STDMETHODCALLTYPE ddraw_null_wined3d_object_destroyed(void *parent) {}
72
73 const struct wined3d_parent_ops ddraw_null_wined3d_parent_ops =
74 {
75     ddraw_null_wined3d_object_destroyed,
76 };
77
78 static inline struct ddraw *impl_from_IDirectDraw(IDirectDraw *iface)
79 {
80     return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw_iface);
81 }
82
83 static inline struct ddraw *impl_from_IDirectDraw2(IDirectDraw2 *iface)
84 {
85     return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw2_iface);
86 }
87
88 static inline struct ddraw *impl_from_IDirectDraw4(IDirectDraw4 *iface)
89 {
90     return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw4_iface);
91 }
92
93 static inline struct ddraw *impl_from_IDirectDraw7(IDirectDraw7 *iface)
94 {
95     return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw7_iface);
96 }
97
98 static inline struct ddraw *impl_from_IDirect3D(IDirect3D *iface)
99 {
100     return CONTAINING_RECORD(iface, struct ddraw, IDirect3D_iface);
101 }
102
103 static inline struct ddraw *impl_from_IDirect3D2(IDirect3D2 *iface)
104 {
105     return CONTAINING_RECORD(iface, struct ddraw, IDirect3D2_iface);
106 }
107
108 static inline struct ddraw *impl_from_IDirect3D3(IDirect3D3 *iface)
109 {
110     return CONTAINING_RECORD(iface, struct ddraw, IDirect3D3_iface);
111 }
112
113 static inline struct ddraw *impl_from_IDirect3D7(IDirect3D7 *iface)
114 {
115     return CONTAINING_RECORD(iface, struct ddraw, IDirect3D7_iface);
116 }
117
118 /*****************************************************************************
119  * IUnknown Methods
120  *****************************************************************************/
121
122 /*****************************************************************************
123  * IDirectDraw7::QueryInterface
124  *
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
130  *
131  * Used for version 1, 2, 4 and 7
132  *
133  * Params:
134  *  refiid: Interface ID asked for
135  *  obj: Used to return the interface pointer
136  *
137  * Returns:
138  *  S_OK if an interface was found
139  *  E_NOINTERFACE if the requested interface wasn't found
140  *
141  *****************************************************************************/
142 static HRESULT WINAPI ddraw7_QueryInterface(IDirectDraw7 *iface, REFIID refiid, void **obj)
143 {
144     struct ddraw *This = impl_from_IDirectDraw7(iface);
145
146     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(refiid), obj);
147
148     /* Can change surface impl type */
149     wined3d_mutex_lock();
150
151     /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
152     *obj = NULL;
153
154     if(!refiid)
155     {
156         wined3d_mutex_unlock();
157         return DDERR_INVALIDPARAMS;
158     }
159
160     /* Check DirectDraw Interfaces */
161     if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
162          IsEqualGUID( &IID_IDirectDraw7, refiid ) )
163     {
164         *obj = &This->IDirectDraw7_iface;
165         TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
166     }
167     else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
168     {
169         *obj = &This->IDirectDraw4_iface;
170         TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
171     }
172     else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
173     {
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;
179     }
180     else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
181     {
182         *obj = &This->IDirectDraw2_iface;
183         TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
184     }
185     else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
186     {
187         *obj = &This->IDirectDraw_iface;
188         TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
189     }
190
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.
197      */
198     else if ( IsEqualGUID( &IID_IDirect3D  , refiid ) ||
199               IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
200               IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
201               IsEqualGUID( &IID_IDirect3D7 , refiid ) )
202     {
203         /* Check the surface implementation */
204         if (DefaultSurfaceType != WINED3D_SURFACE_TYPE_OPENGL)
205         {
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 */
208         }
209
210         if ( IsEqualGUID( &IID_IDirect3D  , refiid ) )
211         {
212             This->d3dversion = 1;
213             *obj = &This->IDirect3D_iface;
214             TRACE(" returning Direct3D interface at %p.\n", *obj);
215         }
216         else if ( IsEqualGUID( &IID_IDirect3D2  , refiid ) )
217         {
218             This->d3dversion = 2;
219             *obj = &This->IDirect3D2_iface;
220             TRACE(" returning Direct3D2 interface at %p.\n", *obj);
221         }
222         else if ( IsEqualGUID( &IID_IDirect3D3  , refiid ) )
223         {
224             This->d3dversion = 3;
225             *obj = &This->IDirect3D3_iface;
226             TRACE(" returning Direct3D3 interface at %p.\n", *obj);
227         }
228         else if(IsEqualGUID( &IID_IDirect3D7  , refiid ))
229         {
230             This->d3dversion = 7;
231             *obj = &This->IDirect3D7_iface;
232             TRACE(" returning Direct3D7 interface at %p.\n", *obj);
233         }
234     }
235     /* Unknown interface */
236     else
237     {
238         ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
239         wined3d_mutex_unlock();
240         return E_NOINTERFACE;
241     }
242
243     IUnknown_AddRef( (IUnknown *) *obj );
244     wined3d_mutex_unlock();
245
246     return S_OK;
247 }
248
249 static HRESULT WINAPI ddraw4_QueryInterface(IDirectDraw4 *iface, REFIID riid, void **object)
250 {
251     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
252
253     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
254
255     return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
256 }
257
258 static HRESULT WINAPI ddraw2_QueryInterface(IDirectDraw2 *iface, REFIID riid, void **object)
259 {
260     struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
261
262     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
263
264     return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
265 }
266
267 static HRESULT WINAPI ddraw1_QueryInterface(IDirectDraw *iface, REFIID riid, void **object)
268 {
269     struct ddraw *ddraw = impl_from_IDirectDraw(iface);
270
271     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
272
273     return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
274 }
275
276 static HRESULT WINAPI d3d7_QueryInterface(IDirect3D7 *iface, REFIID riid, void **object)
277 {
278     struct ddraw *ddraw = impl_from_IDirect3D7(iface);
279
280     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
281
282     return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
283 }
284
285 static HRESULT WINAPI d3d3_QueryInterface(IDirect3D3 *iface, REFIID riid, void **object)
286 {
287     struct ddraw *ddraw = impl_from_IDirect3D3(iface);
288
289     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
290
291     return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
292 }
293
294 static HRESULT WINAPI d3d2_QueryInterface(IDirect3D2 *iface, REFIID riid, void **object)
295 {
296     struct ddraw *ddraw = impl_from_IDirect3D2(iface);
297
298     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
299
300     return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
301 }
302
303 static HRESULT WINAPI d3d1_QueryInterface(IDirect3D *iface, REFIID riid, void **object)
304 {
305     struct ddraw *ddraw = impl_from_IDirect3D(iface);
306
307     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
308
309     return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
310 }
311
312 /*****************************************************************************
313  * IDirectDraw7::AddRef
314  *
315  * Increases the interfaces refcount, basically
316  *
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  * ddraw object.
322  *
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
326  *
327  * Returns: The new refcount
328  *
329  *****************************************************************************/
330 static ULONG WINAPI ddraw7_AddRef(IDirectDraw7 *iface)
331 {
332     struct ddraw *This = impl_from_IDirectDraw7(iface);
333     ULONG ref = InterlockedIncrement(&This->ref7);
334
335     TRACE("%p increasing refcount to %u.\n", This, ref);
336
337     if(ref == 1) InterlockedIncrement(&This->numIfaces);
338
339     return ref;
340 }
341
342 static ULONG WINAPI ddraw4_AddRef(IDirectDraw4 *iface)
343 {
344     struct ddraw *This = impl_from_IDirectDraw4(iface);
345     ULONG ref = InterlockedIncrement(&This->ref4);
346
347     TRACE("%p increasing refcount to %u.\n", This, ref);
348
349     if (ref == 1) InterlockedIncrement(&This->numIfaces);
350
351     return ref;
352 }
353
354 static ULONG WINAPI ddraw2_AddRef(IDirectDraw2 *iface)
355 {
356     struct ddraw *This = impl_from_IDirectDraw2(iface);
357     ULONG ref = InterlockedIncrement(&This->ref2);
358
359     TRACE("%p increasing refcount to %u.\n", This, ref);
360
361     if (ref == 1) InterlockedIncrement(&This->numIfaces);
362
363     return ref;
364 }
365
366 static ULONG WINAPI ddraw1_AddRef(IDirectDraw *iface)
367 {
368     struct ddraw *This = impl_from_IDirectDraw(iface);
369     ULONG ref = InterlockedIncrement(&This->ref1);
370
371     TRACE("%p increasing refcount to %u.\n", This, ref);
372
373     if (ref == 1) InterlockedIncrement(&This->numIfaces);
374
375     return ref;
376 }
377
378 static ULONG WINAPI d3d7_AddRef(IDirect3D7 *iface)
379 {
380     struct ddraw *This = impl_from_IDirect3D7(iface);
381
382     TRACE("iface %p.\n", iface);
383
384     return ddraw7_AddRef(&This->IDirectDraw7_iface);
385 }
386
387 static ULONG WINAPI d3d3_AddRef(IDirect3D3 *iface)
388 {
389     struct ddraw *This = impl_from_IDirect3D3(iface);
390
391     TRACE("iface %p.\n", iface);
392
393     return ddraw1_AddRef(&This->IDirectDraw_iface);
394 }
395
396 static ULONG WINAPI d3d2_AddRef(IDirect3D2 *iface)
397 {
398     struct ddraw *This = impl_from_IDirect3D2(iface);
399
400     TRACE("iface %p.\n", iface);
401
402     return ddraw1_AddRef(&This->IDirectDraw_iface);
403 }
404
405 static ULONG WINAPI d3d1_AddRef(IDirect3D *iface)
406 {
407     struct ddraw *This = impl_from_IDirect3D(iface);
408
409     TRACE("iface %p.\n", iface);
410
411     return ddraw1_AddRef(&This->IDirectDraw_iface);
412 }
413
414 void ddraw_destroy_swapchain(struct ddraw *ddraw)
415 {
416     TRACE("Destroying the swapchain.\n");
417
418     wined3d_swapchain_decref(ddraw->wined3d_swapchain);
419     ddraw->wined3d_swapchain = NULL;
420
421     if (DefaultSurfaceType == WINED3D_SURFACE_TYPE_OPENGL)
422     {
423         UINT i;
424
425         for (i = 0; i < ddraw->numConvertedDecls; ++i)
426         {
427             wined3d_vertex_declaration_decref(ddraw->decls[i].decl);
428         }
429         HeapFree(GetProcessHeap(), 0, ddraw->decls);
430         ddraw->numConvertedDecls = 0;
431
432         if (FAILED(wined3d_device_uninit_3d(ddraw->wined3d_device)))
433         {
434             ERR("Failed to uninit 3D.\n");
435         }
436         else
437         {
438             /* Free the d3d window if one was created. */
439             if (ddraw->d3d_window && ddraw->d3d_window != ddraw->dest_window)
440             {
441                 TRACE("Destroying the hidden render window %p.\n", ddraw->d3d_window);
442                 DestroyWindow(ddraw->d3d_window);
443                 ddraw->d3d_window = 0;
444             }
445         }
446
447         ddraw->d3d_initialized = FALSE;
448     }
449     else
450     {
451         wined3d_device_uninit_gdi(ddraw->wined3d_device);
452     }
453
454     ddraw_set_swapchain_window(ddraw, NULL);
455
456     TRACE("Swapchain destroyed.\n");
457 }
458
459 /*****************************************************************************
460  * ddraw_destroy
461  *
462  * Destroys a ddraw object if all refcounts are 0. This is to share code
463  * between the IDirectDrawX::Release functions
464  *
465  * Params:
466  *  This: DirectDraw object to destroy
467  *
468  *****************************************************************************/
469 static void ddraw_destroy(struct ddraw *This)
470 {
471     IDirectDraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, NULL, DDSCL_NORMAL);
472     IDirectDraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
473
474     /* Destroy the device window if we created one */
475     if(This->devicewindow != 0)
476     {
477         TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
478         DestroyWindow(This->devicewindow);
479         This->devicewindow = 0;
480     }
481
482     wined3d_mutex_lock();
483     list_remove(&This->ddraw_list_entry);
484     wined3d_mutex_unlock();
485
486     if (This->wined3d_swapchain)
487         ddraw_destroy_swapchain(This);
488     wined3d_device_decref(This->wined3d_device);
489     wined3d_decref(This->wined3d);
490
491     /* Now free the object */
492     HeapFree(GetProcessHeap(), 0, This);
493 }
494
495 /*****************************************************************************
496  * IDirectDraw7::Release
497  *
498  * Decreases the refcount. If the refcount falls to 0, the object is destroyed
499  *
500  * Returns: The new refcount
501  *****************************************************************************/
502 static ULONG WINAPI ddraw7_Release(IDirectDraw7 *iface)
503 {
504     struct ddraw *This = impl_from_IDirectDraw7(iface);
505     ULONG ref = InterlockedDecrement(&This->ref7);
506
507     TRACE("%p decreasing refcount to %u.\n", This, ref);
508
509     if (!ref && !InterlockedDecrement(&This->numIfaces))
510         ddraw_destroy(This);
511
512     return ref;
513 }
514
515 static ULONG WINAPI ddraw4_Release(IDirectDraw4 *iface)
516 {
517     struct ddraw *This = impl_from_IDirectDraw4(iface);
518     ULONG ref = InterlockedDecrement(&This->ref4);
519
520     TRACE("%p decreasing refcount to %u.\n", This, ref);
521
522     if (!ref && !InterlockedDecrement(&This->numIfaces))
523         ddraw_destroy(This);
524
525     return ref;
526 }
527
528 static ULONG WINAPI ddraw2_Release(IDirectDraw2 *iface)
529 {
530     struct ddraw *This = impl_from_IDirectDraw2(iface);
531     ULONG ref = InterlockedDecrement(&This->ref2);
532
533     TRACE("%p decreasing refcount to %u.\n", This, ref);
534
535     if (!ref && !InterlockedDecrement(&This->numIfaces))
536         ddraw_destroy(This);
537
538     return ref;
539 }
540
541 static ULONG WINAPI ddraw1_Release(IDirectDraw *iface)
542 {
543     struct ddraw *This = impl_from_IDirectDraw(iface);
544     ULONG ref = InterlockedDecrement(&This->ref1);
545
546     TRACE("%p decreasing refcount to %u.\n", This, ref);
547
548     if (!ref && !InterlockedDecrement(&This->numIfaces))
549         ddraw_destroy(This);
550
551     return ref;
552 }
553
554 static ULONG WINAPI d3d7_Release(IDirect3D7 *iface)
555 {
556     struct ddraw *This = impl_from_IDirect3D7(iface);
557
558     TRACE("iface %p.\n", iface);
559
560     return ddraw7_Release(&This->IDirectDraw7_iface);
561 }
562
563 static ULONG WINAPI d3d3_Release(IDirect3D3 *iface)
564 {
565     struct ddraw *This = impl_from_IDirect3D3(iface);
566
567     TRACE("iface %p.\n", iface);
568
569     return ddraw1_Release(&This->IDirectDraw_iface);
570 }
571
572 static ULONG WINAPI d3d2_Release(IDirect3D2 *iface)
573 {
574     struct ddraw *This = impl_from_IDirect3D2(iface);
575
576     TRACE("iface %p.\n", iface);
577
578     return ddraw1_Release(&This->IDirectDraw_iface);
579 }
580
581 static ULONG WINAPI d3d1_Release(IDirect3D *iface)
582 {
583     struct ddraw *This = impl_from_IDirect3D(iface);
584
585     TRACE("iface %p.\n", iface);
586
587     return ddraw1_Release(&This->IDirectDraw_iface);
588 }
589
590 /*****************************************************************************
591  * IDirectDraw methods
592  *****************************************************************************/
593
594 static HRESULT ddraw_set_focus_window(struct ddraw *ddraw, HWND window)
595 {
596     /* FIXME: This looks wrong, exclusive mode should imply a destination
597      * window. */
598     if ((ddraw->cooperative_level & DDSCL_EXCLUSIVE) && ddraw->dest_window)
599     {
600         TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET.\n");
601         return DDERR_HWNDALREADYSET;
602     }
603
604     ddraw->focuswindow = window;
605
606     /* Use the focus window for drawing too. */
607     ddraw->dest_window = ddraw->focuswindow;
608
609     return DD_OK;
610 }
611
612 static HRESULT ddraw_attach_d3d_device(struct ddraw *ddraw,
613         struct wined3d_swapchain_desc *swapchain_desc)
614 {
615     HWND window = swapchain_desc->device_window;
616     HRESULT hr;
617
618     TRACE("ddraw %p.\n", ddraw);
619
620     if (!window || window == GetDesktopWindow())
621     {
622         window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "Hidden D3D Window",
623                 WS_DISABLED, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
624                 NULL, NULL, NULL, NULL);
625         if (!window)
626         {
627             ERR("Failed to create window, last error %#x.\n", GetLastError());
628             return E_FAIL;
629         }
630
631         ShowWindow(window, SW_HIDE);   /* Just to be sure */
632         WARN("No window for the Direct3DDevice, created hidden window %p.\n", window);
633
634         swapchain_desc->device_window = window;
635     }
636     else
637     {
638         TRACE("Using existing window %p for Direct3D rendering.\n", window);
639     }
640     ddraw->d3d_window = window;
641
642     /* Set this NOW, otherwise creating the depth stencil surface will cause a
643      * recursive loop until ram or emulated video memory is full. */
644     ddraw->d3d_initialized = TRUE;
645     hr = wined3d_device_init_3d(ddraw->wined3d_device, swapchain_desc);
646     if (FAILED(hr))
647     {
648         ddraw->d3d_initialized = FALSE;
649         return hr;
650     }
651
652     ddraw->declArraySize = 2;
653     ddraw->decls = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw->decls) * ddraw->declArraySize);
654     if (!ddraw->decls)
655     {
656         ERR("Error allocating an array for the converted vertex decls.\n");
657         ddraw->declArraySize = 0;
658         hr = wined3d_device_uninit_3d(ddraw->wined3d_device);
659         return E_OUTOFMEMORY;
660     }
661
662     TRACE("Successfully initialized 3D.\n");
663
664     return DD_OK;
665 }
666
667 static HRESULT ddraw_create_swapchain(struct ddraw *ddraw, HWND window, BOOL windowed)
668 {
669     struct wined3d_swapchain_desc swapchain_desc;
670     struct wined3d_display_mode mode;
671     HRESULT hr = WINED3D_OK;
672
673     if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
674     {
675         ERR("Failed to get display mode.\n");
676         return hr;
677     }
678
679     memset(&swapchain_desc, 0, sizeof(swapchain_desc));
680     swapchain_desc.backbuffer_width = mode.width;
681     swapchain_desc.backbuffer_height = mode.height;
682     swapchain_desc.backbuffer_format = mode.format_id;
683     swapchain_desc.swap_effect = WINED3D_SWAP_EFFECT_COPY;
684     swapchain_desc.device_window = window;
685     swapchain_desc.windowed = windowed;
686
687     if (DefaultSurfaceType == WINED3D_SURFACE_TYPE_OPENGL)
688         hr = ddraw_attach_d3d_device(ddraw, &swapchain_desc);
689     else
690         hr = wined3d_device_init_gdi(ddraw->wined3d_device, &swapchain_desc);
691
692     if (FAILED(hr))
693     {
694         ERR("Failed to create swapchain, hr %#x.\n", hr);
695         return hr;
696     }
697
698     if (!(ddraw->wined3d_swapchain = wined3d_device_get_swapchain(ddraw->wined3d_device, 0)))
699     {
700         ERR("Failed to get swapchain.\n");
701         return DDERR_INVALIDPARAMS;
702     }
703
704     wined3d_swapchain_incref(ddraw->wined3d_swapchain);
705     ddraw_set_swapchain_window(ddraw, window);
706
707     return DD_OK;
708 }
709
710 /*****************************************************************************
711  * IDirectDraw7::SetCooperativeLevel
712  *
713  * Sets the cooperative level for the DirectDraw object, and the window
714  * assigned to it. The cooperative level determines the general behavior
715  * of the DirectDraw application
716  *
717  * Warning: This is quite tricky, as it's not really documented which
718  * cooperative levels can be combined with each other. If a game fails
719  * after this function, try to check the cooperative levels passed on
720  * Windows, and if it returns something different.
721  *
722  * If you think that this function caused the failure because it writes a
723  * fixme, be sure to run again with a +ddraw trace.
724  *
725  * What is known about cooperative levels (See the ddraw modes test):
726  * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
727  * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
728  * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
729  * DDSCL_FULLSCREEN can be activated
730  * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
731  *
732  * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
733  *                DDSCL_SETFOCUSWINDOW (partially),
734  *                DDSCL_MULTITHREADED (work in progress)
735  *
736  * Unhandled flags, which should be implemented
737  *  DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
738  *  expect any difference to a normal window for wine)
739  *  DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
740  *  rendering (Possible test case: Half-Life)
741  *
742  * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
743  *
744  * These don't seem very important for wine:
745  *  DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
746  *
747  * Returns:
748  *  DD_OK if the cooperative level was set successfully
749  *  DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
750  *  DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
751  *   (Probably others too, have to investigate)
752  *
753  *****************************************************************************/
754 static HRESULT WINAPI ddraw7_SetCooperativeLevel(IDirectDraw7 *iface, HWND hwnd, DWORD cooplevel)
755 {
756     struct ddraw *This = impl_from_IDirectDraw7(iface);
757     struct wined3d_surface *rt = NULL, *ds = NULL;
758     struct wined3d_stateblock *stateblock;
759     BOOL restore_state = FALSE;
760     HWND window;
761     HRESULT hr;
762
763     TRACE("iface %p, window %p, flags %#x.\n", iface, hwnd, cooplevel);
764     DDRAW_dump_cooperativelevel(cooplevel);
765
766     wined3d_mutex_lock();
767
768     /* Get the old window */
769     window = This->dest_window;
770
771     /* Tests suggest that we need one of them: */
772     if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
773                       DDSCL_NORMAL         |
774                       DDSCL_EXCLUSIVE      )))
775     {
776         TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
777         wined3d_mutex_unlock();
778         return DDERR_INVALIDPARAMS;
779     }
780
781     if ((cooplevel & DDSCL_CREATEDEVICEWINDOW) && !(cooplevel & DDSCL_EXCLUSIVE))
782     {
783         WARN("DDSCL_CREATEDEVICEWINDOW requires DDSCL_EXCLUSIVE.\n");
784         wined3d_mutex_unlock();
785         return DDERR_INVALIDPARAMS;
786     }
787
788     /* Handle those levels first which set various hwnds */
789     if ((cooplevel & DDSCL_SETFOCUSWINDOW) && !(cooplevel & DDSCL_CREATEDEVICEWINDOW))
790     {
791         /* This isn't compatible with a lot of flags */
792         if (cooplevel & (DDSCL_MULTITHREADED
793                 | DDSCL_FPUSETUP
794                 | DDSCL_FPUPRESERVE
795                 | DDSCL_ALLOWREBOOT
796                 | DDSCL_ALLOWMODEX
797                 | DDSCL_SETDEVICEWINDOW
798                 | DDSCL_NORMAL
799                 | DDSCL_EXCLUSIVE
800                 | DDSCL_FULLSCREEN))
801         {
802             WARN("Called with incompatible flags, returning DDERR_INVALIDPARAMS.\n");
803             wined3d_mutex_unlock();
804             return DDERR_INVALIDPARAMS;
805         }
806
807         hr = ddraw_set_focus_window(This, hwnd);
808         wined3d_mutex_unlock();
809         return hr;
810     }
811
812     if (cooplevel & DDSCL_EXCLUSIVE)
813     {
814         if (!(cooplevel & DDSCL_FULLSCREEN) || !(hwnd || (cooplevel & DDSCL_CREATEDEVICEWINDOW)))
815         {
816             WARN("DDSCL_EXCLUSIVE requires DDSCL_FULLSCREEN and a window.\n");
817             wined3d_mutex_unlock();
818             return DDERR_INVALIDPARAMS;
819         }
820
821         if (cooplevel & DDSCL_CREATEDEVICEWINDOW)
822         {
823             HWND device_window;
824
825             if (!This->focuswindow && !(cooplevel & DDSCL_SETFOCUSWINDOW))
826             {
827                 WARN("No focus window set.\n");
828                 wined3d_mutex_unlock();
829                 return DDERR_NOFOCUSWINDOW;
830             }
831
832             device_window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "DirectDrawDeviceWnd",
833                     WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
834                     NULL, NULL, NULL, NULL);
835             if (!device_window)
836             {
837                 ERR("Failed to create window, last error %#x.\n", GetLastError());
838                 wined3d_mutex_unlock();
839                 return E_FAIL;
840             }
841
842             ShowWindow(device_window, SW_SHOW);
843             TRACE("Created a device window %p.\n", device_window);
844
845             /* Native apparently leaks the created device window if setting the
846              * focus window below fails. */
847             This->cooperative_level |= DDSCL_CREATEDEVICEWINDOW;
848             This->devicewindow = device_window;
849
850             if (cooplevel & DDSCL_SETFOCUSWINDOW)
851             {
852                 if (!hwnd)
853                 {
854                     wined3d_mutex_unlock();
855                     return DDERR_NOHWND;
856                 }
857
858                 if (FAILED(hr = ddraw_set_focus_window(This, hwnd)))
859                 {
860                     wined3d_mutex_unlock();
861                     return hr;
862                 }
863             }
864
865             hwnd = device_window;
866         }
867     }
868     else
869     {
870         if (This->cooperative_level & DDSCL_CREATEDEVICEWINDOW)
871             DestroyWindow(This->devicewindow);
872         This->devicewindow = NULL;
873         This->focuswindow = NULL;
874     }
875
876     if ((This->cooperative_level & DDSCL_EXCLUSIVE)
877             && (hwnd != window || !(cooplevel & DDSCL_EXCLUSIVE)))
878         wined3d_device_release_focus_window(This->wined3d_device);
879
880     if ((cooplevel & DDSCL_FULLSCREEN) != (This->cooperative_level & DDSCL_FULLSCREEN) || hwnd != window)
881     {
882         if (This->cooperative_level & DDSCL_FULLSCREEN)
883             wined3d_device_restore_fullscreen_window(This->wined3d_device, window);
884
885         if (cooplevel & DDSCL_FULLSCREEN)
886         {
887             struct wined3d_display_mode display_mode;
888
889             wined3d_get_adapter_display_mode(This->wined3d, WINED3DADAPTER_DEFAULT, &display_mode, NULL);
890             wined3d_device_setup_fullscreen_window(This->wined3d_device, hwnd,
891                     display_mode.width, display_mode.height);
892         }
893     }
894
895     if ((cooplevel & DDSCL_EXCLUSIVE)
896             && (hwnd != window || !(This->cooperative_level & DDSCL_EXCLUSIVE)))
897     {
898         hr = wined3d_device_acquire_focus_window(This->wined3d_device, hwnd);
899         if (FAILED(hr))
900         {
901             ERR("Failed to acquire focus window, hr %#x.\n", hr);
902             wined3d_mutex_unlock();
903             return hr;
904         }
905     }
906
907     /* Don't override focus windows or private device windows */
908     if (hwnd && !This->focuswindow && !This->devicewindow && (hwnd != window))
909         This->dest_window = hwnd;
910
911     if (cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
912         wined3d_device_set_multithreaded(This->wined3d_device);
913
914     if (This->wined3d_swapchain)
915     {
916         if (DefaultSurfaceType != WINED3D_SURFACE_TYPE_GDI)
917         {
918             restore_state = TRUE;
919
920             if (FAILED(hr = wined3d_stateblock_create(This->wined3d_device, WINED3D_SBT_ALL, &stateblock)))
921             {
922                 ERR("Failed to create stateblock, hr %#x.\n", hr);
923                 wined3d_mutex_unlock();
924                 return hr;
925             }
926
927             wined3d_stateblock_capture(stateblock);
928             rt = wined3d_device_get_render_target(This->wined3d_device, 0);
929             if (rt == This->wined3d_frontbuffer)
930                 rt = NULL;
931             else if (rt)
932                 wined3d_surface_incref(rt);
933
934             if ((ds = wined3d_device_get_depth_stencil(This->wined3d_device)))
935                 wined3d_surface_incref(ds);
936         }
937
938         ddraw_destroy_swapchain(This);
939     }
940
941     if (FAILED(hr = ddraw_create_swapchain(This, This->dest_window, !(cooplevel & DDSCL_FULLSCREEN))))
942         ERR("Failed to create swapchain, hr %#x.\n", hr);
943
944     if (restore_state)
945     {
946         if (ds)
947         {
948             wined3d_device_set_depth_stencil(This->wined3d_device, ds);
949             wined3d_surface_decref(ds);
950         }
951
952         if (rt)
953         {
954             wined3d_device_set_render_target(This->wined3d_device, 0, rt, FALSE);
955             wined3d_surface_decref(rt);
956         }
957
958         wined3d_stateblock_apply(stateblock);
959         wined3d_stateblock_decref(stateblock);
960     }
961
962     /* Unhandled flags */
963     if(cooplevel & DDSCL_ALLOWREBOOT)
964         WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
965     if(cooplevel & DDSCL_ALLOWMODEX)
966         WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
967     if(cooplevel & DDSCL_FPUSETUP)
968         WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
969
970     /* Store the cooperative_level */
971     This->cooperative_level = cooplevel;
972     TRACE("SetCooperativeLevel retuning DD_OK\n");
973     wined3d_mutex_unlock();
974
975     return DD_OK;
976 }
977
978 static HRESULT WINAPI ddraw4_SetCooperativeLevel(IDirectDraw4 *iface, HWND window, DWORD flags)
979 {
980     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
981
982     TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
983
984     return ddraw7_SetCooperativeLevel(&ddraw->IDirectDraw7_iface, window, flags);
985 }
986
987 static HRESULT WINAPI ddraw2_SetCooperativeLevel(IDirectDraw2 *iface, HWND window, DWORD flags)
988 {
989     struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
990
991     TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
992
993     return ddraw7_SetCooperativeLevel(&ddraw->IDirectDraw7_iface, window, flags);
994 }
995
996 static HRESULT WINAPI ddraw1_SetCooperativeLevel(IDirectDraw *iface, HWND window, DWORD flags)
997 {
998     struct ddraw *ddraw = impl_from_IDirectDraw(iface);
999
1000     TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1001
1002     return ddraw7_SetCooperativeLevel(&ddraw->IDirectDraw7_iface, window, flags);
1003 }
1004
1005 /*****************************************************************************
1006  * IDirectDraw7::SetDisplayMode
1007  *
1008  * Sets the display screen resolution, color depth and refresh frequency
1009  * when in fullscreen mode (in theory).
1010  * Possible return values listed in the SDK suggest that this method fails
1011  * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
1012  * the display mode in DDSCL_NORMAL mode without an hwnd specified.
1013  * It seems to be valid to pass 0 for With and Height, this has to be tested
1014  * It could mean that the current video mode should be left as-is. (But why
1015  * call it then?)
1016  *
1017  * Params:
1018  *  Height, Width: Screen dimension
1019  *  BPP: Color depth in Bits per pixel
1020  *  Refreshrate: Screen refresh rate
1021  *  Flags: Other stuff
1022  *
1023  * Returns
1024  *  DD_OK on success
1025  *
1026  *****************************************************************************/
1027 static HRESULT WINAPI ddraw7_SetDisplayMode(IDirectDraw7 *iface, DWORD width, DWORD height,
1028         DWORD bpp, DWORD refresh_rate, DWORD flags)
1029 {
1030     struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1031     struct wined3d_display_mode mode;
1032     enum wined3d_format_id format;
1033     HRESULT hr;
1034
1035     TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1036             iface, width, height, bpp, refresh_rate, flags);
1037
1038     if (force_refresh_rate != 0)
1039     {
1040         TRACE("ForceRefreshRate overriding passed-in refresh rate (%u Hz) to %u Hz\n",
1041                 refresh_rate, force_refresh_rate);
1042         refresh_rate = force_refresh_rate;
1043     }
1044
1045     wined3d_mutex_lock();
1046
1047     if (!width || !height)
1048     {
1049         /* It looks like Need for Speed Porsche Unleashed expects DD_OK here. */
1050         wined3d_mutex_unlock();
1051         return DD_OK;
1052     }
1053
1054     switch (bpp)
1055     {
1056         case 8:  format = WINED3DFMT_P8_UINT;        break;
1057         case 15: format = WINED3DFMT_B5G5R5X1_UNORM; break;
1058         case 16: format = WINED3DFMT_B5G6R5_UNORM;   break;
1059         case 24: format = WINED3DFMT_B8G8R8_UNORM;   break;
1060         case 32: format = WINED3DFMT_B8G8R8X8_UNORM; break;
1061         default: format = WINED3DFMT_UNKNOWN;        break;
1062     }
1063
1064     /* Check the exclusive mode.
1065      * if(!(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
1066      *     return DDERR_NOEXCLUSIVEMODE;
1067      * This is WRONG. Don't know if the SDK is completely wrong and if there
1068      * are any conditions when DDERR_NOEXCLUSIVE is returned, but Half-Life
1069      * 1.1.1.1 (Steam version) depends on this. */
1070     mode.width = width;
1071     mode.height = height;
1072     mode.refresh_rate = refresh_rate;
1073     mode.format_id = format;
1074     mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
1075
1076     /* TODO: The possible return values from msdn suggest that the screen mode
1077      * can't be changed if a surface is locked or some drawing is in progress. */
1078     /* TODO: Lose the primary surface. */
1079     hr = wined3d_set_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode);
1080
1081     wined3d_mutex_unlock();
1082
1083     switch (hr)
1084     {
1085         case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1086         default:                      return hr;
1087     }
1088 }
1089
1090 static HRESULT WINAPI ddraw4_SetDisplayMode(IDirectDraw4 *iface, DWORD width, DWORD height,
1091         DWORD bpp, DWORD refresh_rate, DWORD flags)
1092 {
1093     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1094
1095     TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1096             iface, width, height, bpp, refresh_rate, flags);
1097
1098     return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1099 }
1100
1101 static HRESULT WINAPI ddraw2_SetDisplayMode(IDirectDraw2 *iface,
1102         DWORD width, DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
1103 {
1104     struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1105
1106     TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1107             iface, width, height, bpp, refresh_rate, flags);
1108
1109     return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1110 }
1111
1112 static HRESULT WINAPI ddraw1_SetDisplayMode(IDirectDraw *iface, DWORD width, DWORD height, DWORD bpp)
1113 {
1114     struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1115
1116     TRACE("iface %p, width %u, height %u, bpp %u.\n", iface, width, height, bpp);
1117
1118     return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, 0, 0);
1119 }
1120
1121 /*****************************************************************************
1122  * IDirectDraw7::RestoreDisplayMode
1123  *
1124  * Restores the display mode to what it was at creation time. Basically.
1125  *
1126  * A problem arises when there are 2 DirectDraw objects using the same hwnd:
1127  *  -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
1128  *  -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
1129  *  -> DD_1 is released. The screen should be left at 1024x768x32.
1130  *  -> DD_2 is released. The screen should be set to 1400x1050x32
1131  * This case is unhandled right now, but Empire Earth does it this way.
1132  * (But perhaps there is something in SetCooperativeLevel to prevent this)
1133  *
1134  * The msdn says that this method resets the display mode to what it was before
1135  * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
1136  *
1137  * Returns
1138  *  DD_OK on success
1139  *  DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
1140  *
1141  *****************************************************************************/
1142 static HRESULT WINAPI ddraw7_RestoreDisplayMode(IDirectDraw7 *iface)
1143 {
1144     struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1145     HRESULT hr;
1146
1147     TRACE("iface %p.\n", iface);
1148
1149     wined3d_mutex_lock();
1150
1151     hr = wined3d_set_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &ddraw->original_mode);
1152
1153     wined3d_mutex_unlock();
1154
1155     return hr;
1156 }
1157
1158 static HRESULT WINAPI ddraw4_RestoreDisplayMode(IDirectDraw4 *iface)
1159 {
1160     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1161
1162     TRACE("iface %p.\n", iface);
1163
1164     return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
1165 }
1166
1167 static HRESULT WINAPI ddraw2_RestoreDisplayMode(IDirectDraw2 *iface)
1168 {
1169     struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1170
1171     TRACE("iface %p.\n", iface);
1172
1173     return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
1174 }
1175
1176 static HRESULT WINAPI ddraw1_RestoreDisplayMode(IDirectDraw *iface)
1177 {
1178     struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1179
1180     TRACE("iface %p.\n", iface);
1181
1182     return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
1183 }
1184
1185 /*****************************************************************************
1186  * IDirectDraw7::GetCaps
1187  *
1188  * Returns the drives capabilities
1189  *
1190  * Used for version 1, 2, 4 and 7
1191  *
1192  * Params:
1193  *  DriverCaps: Structure to write the Hardware accelerated caps to
1194  *  HelCaps: Structure to write the emulation caps to
1195  *
1196  * Returns
1197  *  This implementation returns DD_OK only
1198  *
1199  *****************************************************************************/
1200 static HRESULT WINAPI ddraw7_GetCaps(IDirectDraw7 *iface, DDCAPS *DriverCaps, DDCAPS *HELCaps)
1201 {
1202     struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1203     DDCAPS caps;
1204     WINED3DCAPS winecaps;
1205     HRESULT hr;
1206     DDSCAPS2 ddscaps = {0, 0, 0, 0};
1207
1208     TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, DriverCaps, HELCaps);
1209
1210     /* One structure must be != NULL */
1211     if (!DriverCaps && !HELCaps)
1212     {
1213         WARN("Invalid parameters.\n");
1214         return DDERR_INVALIDPARAMS;
1215     }
1216
1217     memset(&caps, 0, sizeof(caps));
1218     memset(&winecaps, 0, sizeof(winecaps));
1219     caps.dwSize = sizeof(caps);
1220
1221     wined3d_mutex_lock();
1222     hr = wined3d_device_get_device_caps(ddraw->wined3d_device, &winecaps);
1223     if (FAILED(hr))
1224     {
1225         WARN("IWineD3DDevice::GetDeviceCaps failed\n");
1226         wined3d_mutex_unlock();
1227         return hr;
1228     }
1229
1230     hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
1231     wined3d_mutex_unlock();
1232     if(FAILED(hr)) {
1233         WARN("IDirectDraw7::GetAvailableVidMem failed\n");
1234         return hr;
1235     }
1236
1237     caps.dwCaps = winecaps.ddraw_caps.caps;
1238     caps.dwCaps2 = winecaps.ddraw_caps.caps2;
1239     caps.dwCKeyCaps = winecaps.ddraw_caps.color_key_caps;
1240     caps.dwFXCaps = winecaps.ddraw_caps.fx_caps;
1241     caps.dwPalCaps = winecaps.ddraw_caps.pal_caps;
1242     caps.ddsCaps.dwCaps = winecaps.ddraw_caps.dds_caps;
1243     caps.dwSVBCaps = winecaps.ddraw_caps.svb_caps;
1244     caps.dwSVBCKeyCaps = winecaps.ddraw_caps.svb_color_key_caps;
1245     caps.dwSVBFXCaps = winecaps.ddraw_caps.svb_fx_caps;
1246     caps.dwVSBCaps = winecaps.ddraw_caps.vsb_caps;
1247     caps.dwVSBCKeyCaps = winecaps.ddraw_caps.vsb_color_key_caps;
1248     caps.dwVSBFXCaps = winecaps.ddraw_caps.vsb_fx_caps;
1249     caps.dwSSBCaps = winecaps.ddraw_caps.ssb_caps;
1250     caps.dwSSBCKeyCaps = winecaps.ddraw_caps.ssb_color_key_caps;
1251     caps.dwSSBFXCaps = winecaps.ddraw_caps.ssb_fx_caps;
1252
1253     /* Even if WineD3D supports 3D rendering, remove the cap if ddraw is configured
1254      * not to use it
1255      */
1256     if (DefaultSurfaceType == WINED3D_SURFACE_TYPE_GDI)
1257     {
1258         caps.dwCaps &= ~DDCAPS_3D;
1259         caps.ddsCaps.dwCaps &= ~(DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER);
1260     }
1261     if (winecaps.ddraw_caps.stride_align)
1262     {
1263         caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
1264         caps.dwAlignStrideAlign = winecaps.ddraw_caps.stride_align;
1265     }
1266
1267     if(DriverCaps)
1268     {
1269         DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
1270         if (TRACE_ON(ddraw))
1271         {
1272             TRACE("Driver Caps :\n");
1273             DDRAW_dump_DDCAPS(DriverCaps);
1274         }
1275
1276     }
1277     if(HELCaps)
1278     {
1279         DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
1280         if (TRACE_ON(ddraw))
1281         {
1282             TRACE("HEL Caps :\n");
1283             DDRAW_dump_DDCAPS(HELCaps);
1284         }
1285     }
1286
1287     return DD_OK;
1288 }
1289
1290 static HRESULT WINAPI ddraw4_GetCaps(IDirectDraw4 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1291 {
1292     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1293
1294     TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1295
1296     return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1297 }
1298
1299 static HRESULT WINAPI ddraw2_GetCaps(IDirectDraw2 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1300 {
1301     struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1302
1303     TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1304
1305     return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1306 }
1307
1308 static HRESULT WINAPI ddraw1_GetCaps(IDirectDraw *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1309 {
1310     struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1311
1312     TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1313
1314     return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1315 }
1316
1317 /*****************************************************************************
1318  * IDirectDraw7::Compact
1319  *
1320  * No idea what it does, MSDN says it's not implemented.
1321  *
1322  * Returns
1323  *  DD_OK, but this is unchecked
1324  *
1325  *****************************************************************************/
1326 static HRESULT WINAPI ddraw7_Compact(IDirectDraw7 *iface)
1327 {
1328     TRACE("iface %p.\n", iface);
1329
1330     return DD_OK;
1331 }
1332
1333 static HRESULT WINAPI ddraw4_Compact(IDirectDraw4 *iface)
1334 {
1335     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1336
1337     TRACE("iface %p.\n", iface);
1338
1339     return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1340 }
1341
1342 static HRESULT WINAPI ddraw2_Compact(IDirectDraw2 *iface)
1343 {
1344     struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1345
1346     TRACE("iface %p.\n", iface);
1347
1348     return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1349 }
1350
1351 static HRESULT WINAPI ddraw1_Compact(IDirectDraw *iface)
1352 {
1353     struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1354
1355     TRACE("iface %p.\n", iface);
1356
1357     return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1358 }
1359
1360 /*****************************************************************************
1361  * IDirectDraw7::GetDisplayMode
1362  *
1363  * Returns information about the current display mode
1364  *
1365  * Exists in Version 1, 2, 4 and 7
1366  *
1367  * Params:
1368  *  DDSD: Address of a surface description structure to write the info to
1369  *
1370  * Returns
1371  *  DD_OK
1372  *
1373  *****************************************************************************/
1374 static HRESULT WINAPI ddraw7_GetDisplayMode(IDirectDraw7 *iface, DDSURFACEDESC2 *DDSD)
1375 {
1376     struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1377     struct wined3d_display_mode mode;
1378     HRESULT hr;
1379     DWORD Size;
1380
1381     TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
1382
1383     wined3d_mutex_lock();
1384     /* This seems sane */
1385     if (!DDSD)
1386     {
1387         wined3d_mutex_unlock();
1388         return DDERR_INVALIDPARAMS;
1389     }
1390
1391     if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
1392     {
1393         ERR("Failed to get display mode, hr %#x.\n", hr);
1394         wined3d_mutex_unlock();
1395         return hr;
1396     }
1397
1398     Size = DDSD->dwSize;
1399     memset(DDSD, 0, Size);
1400
1401     DDSD->dwSize = Size;
1402     DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
1403     DDSD->dwWidth = mode.width;
1404     DDSD->dwHeight = mode.height;
1405     DDSD->u2.dwRefreshRate = 60;
1406     DDSD->ddsCaps.dwCaps = 0;
1407     DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
1408     PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, mode.format_id);
1409     DDSD->u1.lPitch = mode.width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
1410
1411     if(TRACE_ON(ddraw))
1412     {
1413         TRACE("Returning surface desc :\n");
1414         DDRAW_dump_surface_desc(DDSD);
1415     }
1416
1417     wined3d_mutex_unlock();
1418
1419     return DD_OK;
1420 }
1421
1422 static HRESULT WINAPI ddraw4_GetDisplayMode(IDirectDraw4 *iface, DDSURFACEDESC2 *surface_desc)
1423 {
1424     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1425
1426     TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1427
1428     return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, surface_desc);
1429 }
1430
1431 static HRESULT WINAPI ddraw2_GetDisplayMode(IDirectDraw2 *iface, DDSURFACEDESC *surface_desc)
1432 {
1433     struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1434
1435     TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1436
1437     /* FIXME: Test sizes, properly convert surface_desc */
1438     return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1439 }
1440
1441 static HRESULT WINAPI ddraw1_GetDisplayMode(IDirectDraw *iface, DDSURFACEDESC *surface_desc)
1442 {
1443     struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1444
1445     TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1446
1447     /* FIXME: Test sizes, properly convert surface_desc */
1448     return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1449 }
1450
1451 /*****************************************************************************
1452  * IDirectDraw7::GetFourCCCodes
1453  *
1454  * Returns an array of supported FourCC codes.
1455  *
1456  * Exists in Version 1, 2, 4 and 7
1457  *
1458  * Params:
1459  *  NumCodes: Contains the number of Codes that Codes can carry. Returns the number
1460  *            of enumerated codes
1461  *  Codes: Pointer to an array of DWORDs where the supported codes are written
1462  *         to
1463  *
1464  * Returns
1465  *  Always returns DD_OK, as it's a stub for now
1466  *
1467  *****************************************************************************/
1468 static HRESULT WINAPI ddraw7_GetFourCCCodes(IDirectDraw7 *iface, DWORD *NumCodes, DWORD *Codes)
1469 {
1470     struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1471     static const enum wined3d_format_id formats[] =
1472     {
1473         WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
1474         WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
1475         WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
1476     };
1477     struct wined3d_display_mode mode;
1478     DWORD count = 0, i, outsize;
1479     HRESULT hr;
1480
1481     TRACE("iface %p, codes_count %p, codes %p.\n", iface, NumCodes, Codes);
1482
1483     if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
1484     {
1485         ERR("Failed to get display mode, hr %#x.\n", hr);
1486         return hr;
1487     }
1488
1489     outsize = NumCodes && Codes ? *NumCodes : 0;
1490
1491     for (i = 0; i < (sizeof(formats) / sizeof(formats[0])); ++i)
1492     {
1493         hr = wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, WINED3D_DEVICE_TYPE_HAL,
1494                 mode.format_id, 0, WINED3D_RTYPE_SURFACE, formats[i], DefaultSurfaceType);
1495         if (SUCCEEDED(hr))
1496         {
1497             if (count < outsize)
1498                 Codes[count] = formats[i];
1499             ++count;
1500         }
1501     }
1502     if(NumCodes) {
1503         TRACE("Returning %u FourCC codes\n", count);
1504         *NumCodes = count;
1505     }
1506
1507     return DD_OK;
1508 }
1509
1510 static HRESULT WINAPI ddraw4_GetFourCCCodes(IDirectDraw4 *iface, DWORD *codes_count, DWORD *codes)
1511 {
1512     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1513
1514     TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1515
1516     return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1517 }
1518
1519 static HRESULT WINAPI ddraw2_GetFourCCCodes(IDirectDraw2 *iface, DWORD *codes_count, DWORD *codes)
1520 {
1521     struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1522
1523     TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1524
1525     return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1526 }
1527
1528 static HRESULT WINAPI ddraw1_GetFourCCCodes(IDirectDraw *iface, DWORD *codes_count, DWORD *codes)
1529 {
1530     struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1531
1532     TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1533
1534     return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1535 }
1536
1537 static HRESULT WINAPI ddraw7_GetMonitorFrequency(IDirectDraw7 *iface, DWORD *frequency)
1538 {
1539     struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1540     struct wined3d_display_mode mode;
1541     HRESULT hr;
1542
1543     TRACE("iface %p, frequency %p.\n", iface, frequency);
1544
1545     wined3d_mutex_lock();
1546     hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL);
1547     wined3d_mutex_unlock();
1548     if (FAILED(hr))
1549     {
1550         WARN("Failed to get display mode, hr %#x.\n", hr);
1551         return hr;
1552     }
1553
1554     *frequency = mode.refresh_rate;
1555
1556     return DD_OK;
1557 }
1558
1559 static HRESULT WINAPI ddraw4_GetMonitorFrequency(IDirectDraw4 *iface, DWORD *frequency)
1560 {
1561     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1562
1563     TRACE("iface %p, frequency %p.\n", iface, frequency);
1564
1565     return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1566 }
1567
1568 static HRESULT WINAPI ddraw2_GetMonitorFrequency(IDirectDraw2 *iface, DWORD *frequency)
1569 {
1570     struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1571
1572     TRACE("iface %p, frequency %p.\n", iface, frequency);
1573
1574     return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1575 }
1576
1577 static HRESULT WINAPI ddraw1_GetMonitorFrequency(IDirectDraw *iface, DWORD *frequency)
1578 {
1579     struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1580
1581     TRACE("iface %p, frequency %p.\n", iface, frequency);
1582
1583     return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1584 }
1585
1586 static HRESULT WINAPI ddraw7_GetVerticalBlankStatus(IDirectDraw7 *iface, BOOL *status)
1587 {
1588     struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1589     struct wined3d_raster_status raster_status;
1590     HRESULT hr;
1591
1592     TRACE("iface %p, status %p.\n", iface, status);
1593
1594     if(!status)
1595         return DDERR_INVALIDPARAMS;
1596
1597     wined3d_mutex_lock();
1598     hr = wined3d_get_adapter_raster_status(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &raster_status);
1599     wined3d_mutex_unlock();
1600     if (FAILED(hr))
1601     {
1602         WARN("Failed to get raster status, hr %#x.\n", hr);
1603         return hr;
1604     }
1605
1606     *status = raster_status.in_vblank;
1607
1608     return DD_OK;
1609 }
1610
1611 static HRESULT WINAPI ddraw4_GetVerticalBlankStatus(IDirectDraw4 *iface, BOOL *status)
1612 {
1613     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1614
1615     TRACE("iface %p, status %p.\n", iface, status);
1616
1617     return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1618 }
1619
1620 static HRESULT WINAPI ddraw2_GetVerticalBlankStatus(IDirectDraw2 *iface, BOOL *status)
1621 {
1622     struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1623
1624     TRACE("iface %p, status %p.\n", iface, status);
1625
1626     return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1627 }
1628
1629 static HRESULT WINAPI ddraw1_GetVerticalBlankStatus(IDirectDraw *iface, BOOL *status)
1630 {
1631     struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1632
1633     TRACE("iface %p, status %p.\n", iface, status);
1634
1635     return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1636 }
1637
1638 /*****************************************************************************
1639  * IDirectDraw7::GetAvailableVidMem
1640  *
1641  * Returns the total and free video memory
1642  *
1643  * Params:
1644  *  Caps: Specifies the memory type asked for
1645  *  total: Pointer to a DWORD to be filled with the total memory
1646  *  free: Pointer to a DWORD to be filled with the free memory
1647  *
1648  * Returns
1649  *  DD_OK on success
1650  *  DDERR_INVALIDPARAMS of free and total are NULL
1651  *
1652  *****************************************************************************/
1653 static HRESULT WINAPI ddraw7_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total,
1654         DWORD *free)
1655 {
1656     struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1657     HRESULT hr = DD_OK;
1658
1659     TRACE("iface %p, caps %p, total %p, free %p.\n", iface, Caps, total, free);
1660
1661     if (TRACE_ON(ddraw))
1662     {
1663         TRACE("Asked for memory with description: ");
1664         DDRAW_dump_DDSCAPS2(Caps);
1665     }
1666     wined3d_mutex_lock();
1667
1668     /* Todo: System memory vs local video memory vs non-local video memory
1669      * The MSDN also mentions differences between texture memory and other
1670      * resources, but that's not important
1671      */
1672
1673     if( (!total) && (!free) )
1674     {
1675         wined3d_mutex_unlock();
1676         return DDERR_INVALIDPARAMS;
1677     }
1678
1679     if (free)
1680         *free = wined3d_device_get_available_texture_mem(ddraw->wined3d_device);
1681     if (total)
1682     {
1683         struct wined3d_adapter_identifier desc = {0};
1684
1685         hr = wined3d_get_adapter_identifier(ddraw->wined3d, WINED3DADAPTER_DEFAULT, 0, &desc);
1686         *total = desc.video_memory;
1687     }
1688
1689     wined3d_mutex_unlock();
1690
1691     return hr;
1692 }
1693
1694 static HRESULT WINAPI ddraw4_GetAvailableVidMem(IDirectDraw4 *iface,
1695         DDSCAPS2 *caps, DWORD *total, DWORD *free)
1696 {
1697     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1698
1699     TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1700
1701     return ddraw7_GetAvailableVidMem(&ddraw->IDirectDraw7_iface, caps, total, free);
1702 }
1703
1704 static HRESULT WINAPI ddraw2_GetAvailableVidMem(IDirectDraw2 *iface,
1705         DDSCAPS *caps, DWORD *total, DWORD *free)
1706 {
1707     struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1708     DDSCAPS2 caps2;
1709
1710     TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1711
1712     DDRAW_Convert_DDSCAPS_1_To_2(caps, &caps2);
1713     return ddraw7_GetAvailableVidMem(&ddraw->IDirectDraw7_iface, &caps2, total, free);
1714 }
1715
1716 /*****************************************************************************
1717  * IDirectDraw7::Initialize
1718  *
1719  * Initializes a DirectDraw interface.
1720  *
1721  * Params:
1722  *  GUID: Interface identifier. Well, don't know what this is really good
1723  *   for
1724  *
1725  * Returns
1726  *  Returns DD_OK on the first call,
1727  *  DDERR_ALREADYINITIALIZED on repeated calls
1728  *
1729  *****************************************************************************/
1730 static HRESULT WINAPI ddraw7_Initialize(IDirectDraw7 *iface, GUID *guid)
1731 {
1732     struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1733
1734     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1735
1736     if (ddraw->initialized)
1737         return DDERR_ALREADYINITIALIZED;
1738
1739     /* FIXME: To properly take the GUID into account we should call
1740      * ddraw_init() here instead of in DDRAW_Create(). */
1741     if (guid)
1742         FIXME("Ignoring guid %s.\n", debugstr_guid(guid));
1743
1744     ddraw->initialized = TRUE;
1745     return DD_OK;
1746 }
1747
1748 static HRESULT WINAPI ddraw4_Initialize(IDirectDraw4 *iface, GUID *guid)
1749 {
1750     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1751
1752     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1753
1754     return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
1755 }
1756
1757 static HRESULT WINAPI ddraw2_Initialize(IDirectDraw2 *iface, GUID *guid)
1758 {
1759     struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1760
1761     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1762
1763     return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
1764 }
1765
1766 static HRESULT WINAPI ddraw1_Initialize(IDirectDraw *iface, GUID *guid)
1767 {
1768     struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1769
1770     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1771
1772     return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
1773 }
1774
1775 static HRESULT WINAPI d3d1_Initialize(IDirect3D *iface, REFIID riid)
1776 {
1777     TRACE("iface %p, riid %s.\n", iface, debugstr_guid(riid));
1778
1779     return DDERR_ALREADYINITIALIZED;
1780 }
1781
1782 /*****************************************************************************
1783  * IDirectDraw7::FlipToGDISurface
1784  *
1785  * "Makes the surface that the GDI writes to the primary surface"
1786  * Looks like some windows specific thing we don't have to care about.
1787  * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1788  * show error boxes ;)
1789  * Well, just return DD_OK.
1790  *
1791  * Returns:
1792  *  Always returns DD_OK
1793  *
1794  *****************************************************************************/
1795 static HRESULT WINAPI ddraw7_FlipToGDISurface(IDirectDraw7 *iface)
1796 {
1797     FIXME("iface %p stub!\n", iface);
1798
1799     return DD_OK;
1800 }
1801
1802 static HRESULT WINAPI ddraw4_FlipToGDISurface(IDirectDraw4 *iface)
1803 {
1804     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1805
1806     TRACE("iface %p.\n", iface);
1807
1808     return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
1809 }
1810
1811 static HRESULT WINAPI ddraw2_FlipToGDISurface(IDirectDraw2 *iface)
1812 {
1813     struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1814
1815     TRACE("iface %p.\n", iface);
1816
1817     return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
1818 }
1819
1820 static HRESULT WINAPI ddraw1_FlipToGDISurface(IDirectDraw *iface)
1821 {
1822     struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1823
1824     TRACE("iface %p.\n", iface);
1825
1826     return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
1827 }
1828
1829 /*****************************************************************************
1830  * IDirectDraw7::WaitForVerticalBlank
1831  *
1832  * This method allows applications to get in sync with the vertical blank
1833  * interval.
1834  * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1835  * redraw the screen, most likely because of this stub
1836  *
1837  * Parameters:
1838  *  Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1839  *         or DDWAITVB_BLOCKEND
1840  *  h: Not used, according to MSDN
1841  *
1842  * Returns:
1843  *  Always returns DD_OK
1844  *
1845  *****************************************************************************/
1846 static HRESULT WINAPI ddraw7_WaitForVerticalBlank(IDirectDraw7 *iface, DWORD Flags, HANDLE event)
1847 {
1848     static BOOL hide;
1849
1850     TRACE("iface %p, flags %#x, event %p.\n", iface, Flags, event);
1851
1852     /* This function is called often, so print the fixme only once */
1853     if(!hide)
1854     {
1855         FIXME("iface %p, flags %#x, event %p stub!\n", iface, Flags, event);
1856         hide = TRUE;
1857     }
1858
1859     /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1860     if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1861         return DDERR_UNSUPPORTED; /* unchecked */
1862
1863     return DD_OK;
1864 }
1865
1866 static HRESULT WINAPI ddraw4_WaitForVerticalBlank(IDirectDraw4 *iface, DWORD flags, HANDLE event)
1867 {
1868     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1869
1870     TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1871
1872     return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
1873 }
1874
1875 static HRESULT WINAPI ddraw2_WaitForVerticalBlank(IDirectDraw2 *iface, DWORD flags, HANDLE event)
1876 {
1877     struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1878
1879     TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1880
1881     return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
1882 }
1883
1884 static HRESULT WINAPI ddraw1_WaitForVerticalBlank(IDirectDraw *iface, DWORD flags, HANDLE event)
1885 {
1886     struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1887
1888     TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1889
1890     return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
1891 }
1892
1893 static HRESULT WINAPI ddraw7_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1894 {
1895     struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1896     struct wined3d_raster_status raster_status;
1897     HRESULT hr;
1898
1899     TRACE("iface %p, line %p.\n", iface, Scanline);
1900
1901     wined3d_mutex_lock();
1902     hr = wined3d_get_adapter_raster_status(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &raster_status);
1903     wined3d_mutex_unlock();
1904     if (FAILED(hr))
1905     {
1906         WARN("Failed to get raster status, hr %#x.\n", hr);
1907         return hr;
1908     }
1909
1910     *Scanline = raster_status.scan_line;
1911
1912     if (raster_status.in_vblank)
1913         return DDERR_VERTICALBLANKINPROGRESS;
1914
1915     return DD_OK;
1916 }
1917
1918 static HRESULT WINAPI ddraw4_GetScanLine(IDirectDraw4 *iface, DWORD *line)
1919 {
1920     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1921
1922     TRACE("iface %p, line %p.\n", iface, line);
1923
1924     return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
1925 }
1926
1927 static HRESULT WINAPI ddraw2_GetScanLine(IDirectDraw2 *iface, DWORD *line)
1928 {
1929     struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1930
1931     TRACE("iface %p, line %p.\n", iface, line);
1932
1933     return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
1934 }
1935
1936 static HRESULT WINAPI ddraw1_GetScanLine(IDirectDraw *iface, DWORD *line)
1937 {
1938     struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1939
1940     TRACE("iface %p, line %p.\n", iface, line);
1941
1942     return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
1943 }
1944
1945 /*****************************************************************************
1946  * IDirectDraw7::TestCooperativeLevel
1947  *
1948  * Informs the application about the state of the video adapter, depending
1949  * on the cooperative level
1950  *
1951  * Returns:
1952  *  DD_OK if the device is in a sane state
1953  *  DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1954  *  if the state is not correct(See below)
1955  *
1956  *****************************************************************************/
1957 static HRESULT WINAPI ddraw7_TestCooperativeLevel(IDirectDraw7 *iface)
1958 {
1959     TRACE("iface %p.\n", iface);
1960
1961     return DD_OK;
1962 }
1963
1964 static HRESULT WINAPI ddraw4_TestCooperativeLevel(IDirectDraw4 *iface)
1965 {
1966     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1967
1968     TRACE("iface %p.\n", iface);
1969
1970     return ddraw7_TestCooperativeLevel(&ddraw->IDirectDraw7_iface);
1971 }
1972
1973 /*****************************************************************************
1974  * IDirectDraw7::GetGDISurface
1975  *
1976  * Returns the surface that GDI is treating as the primary surface.
1977  * For Wine this is the front buffer
1978  *
1979  * Params:
1980  *  GDISurface: Address to write the surface pointer to
1981  *
1982  * Returns:
1983  *  DD_OK if the surface was found
1984  *  DDERR_NOTFOUND if the GDI surface wasn't found
1985  *
1986  *****************************************************************************/
1987 static HRESULT WINAPI ddraw7_GetGDISurface(IDirectDraw7 *iface, IDirectDrawSurface7 **GDISurface)
1988 {
1989     struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1990
1991     TRACE("iface %p, surface %p.\n", iface, GDISurface);
1992
1993     wined3d_mutex_lock();
1994
1995     if (!(*GDISurface = &ddraw->primary->IDirectDrawSurface7_iface))
1996     {
1997         WARN("Primary not created yet.\n");
1998         wined3d_mutex_unlock();
1999         return DDERR_NOTFOUND;
2000     }
2001     IDirectDrawSurface7_AddRef(*GDISurface);
2002
2003     wined3d_mutex_unlock();
2004
2005     return DD_OK;
2006 }
2007
2008 static HRESULT WINAPI ddraw4_GetGDISurface(IDirectDraw4 *iface, IDirectDrawSurface4 **surface)
2009 {
2010     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2011     struct ddraw_surface *surface_impl;
2012     IDirectDrawSurface7 *surface7;
2013     HRESULT hr;
2014
2015     TRACE("iface %p, surface %p.\n", iface, surface);
2016
2017     hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2018     if (FAILED(hr))
2019     {
2020         *surface = NULL;
2021         return hr;
2022     }
2023     surface_impl = impl_from_IDirectDrawSurface7(surface7);
2024     *surface = &surface_impl->IDirectDrawSurface4_iface;
2025     IDirectDrawSurface4_AddRef(*surface);
2026     IDirectDrawSurface7_Release(surface7);
2027
2028     return hr;
2029 }
2030
2031 static HRESULT WINAPI ddraw2_GetGDISurface(IDirectDraw2 *iface, IDirectDrawSurface **surface)
2032 {
2033     struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2034     struct ddraw_surface *surface_impl;
2035     IDirectDrawSurface7 *surface7;
2036     HRESULT hr;
2037
2038     TRACE("iface %p, surface %p.\n", iface, surface);
2039
2040     hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2041     if (FAILED(hr))
2042     {
2043         *surface = NULL;
2044         return hr;
2045     }
2046     surface_impl = impl_from_IDirectDrawSurface7(surface7);
2047     *surface = &surface_impl->IDirectDrawSurface_iface;
2048     IDirectDrawSurface_AddRef(*surface);
2049     IDirectDrawSurface7_Release(surface7);
2050
2051     return hr;
2052 }
2053
2054 static HRESULT WINAPI ddraw1_GetGDISurface(IDirectDraw *iface, IDirectDrawSurface **surface)
2055 {
2056     struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2057     struct ddraw_surface *surface_impl;
2058     IDirectDrawSurface7 *surface7;
2059     HRESULT hr;
2060
2061     TRACE("iface %p, surface %p.\n", iface, surface);
2062
2063     hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2064     if (FAILED(hr))
2065     {
2066         *surface = NULL;
2067         return hr;
2068     }
2069     surface_impl = impl_from_IDirectDrawSurface7(surface7);
2070     *surface = &surface_impl->IDirectDrawSurface_iface;
2071     IDirectDrawSurface_AddRef(*surface);
2072     IDirectDrawSurface7_Release(surface7);
2073
2074     return hr;
2075 }
2076
2077 struct displaymodescallback_context
2078 {
2079     LPDDENUMMODESCALLBACK func;
2080     void *context;
2081 };
2082
2083 static HRESULT CALLBACK EnumDisplayModesCallbackThunk(DDSURFACEDESC2 *surface_desc, void *context)
2084 {
2085     struct displaymodescallback_context *cbcontext = context;
2086     DDSURFACEDESC desc;
2087
2088     DDSD2_to_DDSD(surface_desc, &desc);
2089     return cbcontext->func(&desc, cbcontext->context);
2090 }
2091
2092 /*****************************************************************************
2093  * IDirectDraw7::EnumDisplayModes
2094  *
2095  * Enumerates the supported Display modes. The modes can be filtered with
2096  * the DDSD parameter.
2097  *
2098  * Params:
2099  *  Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES. For old ddraw
2100  *         versions (3 and older?) this is reserved and must be 0.
2101  *  DDSD: Surface description to filter the modes
2102  *  Context: Pointer passed back to the callback function
2103  *  cb: Application-provided callback function
2104  *
2105  * Returns:
2106  *  DD_OK on success
2107  *  DDERR_INVALIDPARAMS if the callback wasn't set
2108  *
2109  *****************************************************************************/
2110 static HRESULT WINAPI ddraw7_EnumDisplayModes(IDirectDraw7 *iface, DWORD Flags,
2111         DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMMODESCALLBACK2 cb)
2112 {
2113     struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2114     struct wined3d_display_mode *enum_modes = NULL;
2115     struct wined3d_display_mode mode;
2116     unsigned int modenum, fmt;
2117     DDSURFACEDESC2 callback_sd;
2118     unsigned enum_mode_count = 0, enum_mode_array_size = 0;
2119     DDPIXELFORMAT pixelformat;
2120
2121     static const enum wined3d_format_id checkFormatList[] =
2122     {
2123         WINED3DFMT_B8G8R8X8_UNORM,
2124         WINED3DFMT_B5G6R5_UNORM,
2125         WINED3DFMT_P8_UINT,
2126     };
2127
2128     TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2129             iface, Flags, DDSD, Context, cb);
2130
2131     if (!cb)
2132         return DDERR_INVALIDPARAMS;
2133
2134     wined3d_mutex_lock();
2135     if(!(Flags & DDEDM_REFRESHRATES))
2136     {
2137         enum_mode_array_size = 16;
2138         enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(*enum_modes) * enum_mode_array_size);
2139         if (!enum_modes)
2140         {
2141             ERR("Out of memory\n");
2142             wined3d_mutex_unlock();
2143             return DDERR_OUTOFMEMORY;
2144         }
2145     }
2146
2147     pixelformat.dwSize = sizeof(pixelformat);
2148     for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
2149     {
2150         modenum = 0;
2151         while (wined3d_enum_adapter_modes(ddraw->wined3d, WINED3DADAPTER_DEFAULT, checkFormatList[fmt],
2152                 WINED3D_SCANLINE_ORDERING_UNKNOWN, modenum++, &mode) == WINED3D_OK)
2153         {
2154             PixelFormat_WineD3DtoDD(&pixelformat, mode.format_id);
2155             if (DDSD)
2156             {
2157                 if (DDSD->dwFlags & DDSD_WIDTH && mode.width != DDSD->dwWidth)
2158                     continue;
2159                 if (DDSD->dwFlags & DDSD_HEIGHT && mode.height != DDSD->dwHeight)
2160                     continue;
2161                 if (DDSD->dwFlags & DDSD_REFRESHRATE && mode.refresh_rate != DDSD->u2.dwRefreshRate)
2162                     continue;
2163                 if (DDSD->dwFlags & DDSD_PIXELFORMAT
2164                         && pixelformat.u1.dwRGBBitCount != DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount)
2165                     continue;
2166             }
2167
2168             if(!(Flags & DDEDM_REFRESHRATES))
2169             {
2170                 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
2171                  * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
2172                  * to be reduced to a single unique result in such case.
2173                  */
2174                 BOOL found = FALSE;
2175                 unsigned i;
2176
2177                 for (i = 0; i < enum_mode_count; i++)
2178                 {
2179                     if (enum_modes[i].width == mode.width && enum_modes[i].height == mode.height
2180                             && enum_modes[i].format_id == mode.format_id)
2181                     {
2182                         found = TRUE;
2183                         break;
2184                     }
2185                 }
2186
2187                 if(found) continue;
2188             }
2189
2190             memset(&callback_sd, 0, sizeof(callback_sd));
2191             callback_sd.dwSize = sizeof(callback_sd);
2192             callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2193
2194             callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH|DDSD_REFRESHRATE;
2195             if (Flags & DDEDM_REFRESHRATES)
2196                 callback_sd.u2.dwRefreshRate = mode.refresh_rate;
2197
2198             callback_sd.dwWidth = mode.width;
2199             callback_sd.dwHeight = mode.height;
2200
2201             callback_sd.u4.ddpfPixelFormat=pixelformat;
2202
2203             /* Calc pitch and DWORD align like MSDN says */
2204             callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.width;
2205             callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
2206
2207             TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
2208               callback_sd.u2.dwRefreshRate);
2209
2210             if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
2211             {
2212                 TRACE("Application asked to terminate the enumeration\n");
2213                 HeapFree(GetProcessHeap(), 0, enum_modes);
2214                 wined3d_mutex_unlock();
2215                 return DD_OK;
2216             }
2217
2218             if(!(Flags & DDEDM_REFRESHRATES))
2219             {
2220                 if (enum_mode_count == enum_mode_array_size)
2221                 {
2222                     struct wined3d_display_mode *new_enum_modes;
2223
2224                     enum_mode_array_size *= 2;
2225                     new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes,
2226                             sizeof(*new_enum_modes) * enum_mode_array_size);
2227                     if (!new_enum_modes)
2228                     {
2229                         ERR("Out of memory\n");
2230                         HeapFree(GetProcessHeap(), 0, enum_modes);
2231                         wined3d_mutex_unlock();
2232                         return DDERR_OUTOFMEMORY;
2233                     }
2234
2235                     enum_modes = new_enum_modes;
2236                 }
2237
2238                 enum_modes[enum_mode_count++] = mode;
2239             }
2240         }
2241     }
2242
2243     TRACE("End of enumeration\n");
2244     HeapFree(GetProcessHeap(), 0, enum_modes);
2245     wined3d_mutex_unlock();
2246
2247     return DD_OK;
2248 }
2249
2250 static HRESULT WINAPI ddraw4_EnumDisplayModes(IDirectDraw4 *iface, DWORD flags,
2251         DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMMODESCALLBACK2 callback)
2252 {
2253     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2254
2255     TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2256             iface, flags, surface_desc, context, callback);
2257
2258     return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags, surface_desc, context, callback);
2259 }
2260
2261 static HRESULT WINAPI ddraw2_EnumDisplayModes(IDirectDraw2 *iface, DWORD flags,
2262         DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2263 {
2264     struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2265     struct displaymodescallback_context cbcontext;
2266     DDSURFACEDESC2 surface_desc2;
2267
2268     TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2269             iface, flags, surface_desc, context, callback);
2270
2271     cbcontext.func = callback;
2272     cbcontext.context = context;
2273
2274     if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2275     return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags,
2276             surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2277 }
2278
2279 static HRESULT WINAPI ddraw1_EnumDisplayModes(IDirectDraw *iface, DWORD flags,
2280         DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2281 {
2282     struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2283     struct displaymodescallback_context cbcontext;
2284     DDSURFACEDESC2 surface_desc2;
2285
2286     TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2287             iface, flags, surface_desc, context, callback);
2288
2289     cbcontext.func = callback;
2290     cbcontext.context = context;
2291
2292     if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2293     return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags,
2294             surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2295 }
2296
2297 /*****************************************************************************
2298  * IDirectDraw7::EvaluateMode
2299  *
2300  * Used with IDirectDraw7::StartModeTest to test video modes.
2301  * EvaluateMode is used to pass or fail a mode, and continue with the next
2302  * mode
2303  *
2304  * Params:
2305  *  Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
2306  *  Timeout: Returns the amount of seconds left before the mode would have
2307  *           been failed automatically
2308  *
2309  * Returns:
2310  *  This implementation always DD_OK, because it's a stub
2311  *
2312  *****************************************************************************/
2313 static HRESULT WINAPI ddraw7_EvaluateMode(IDirectDraw7 *iface, DWORD Flags, DWORD *Timeout)
2314 {
2315     FIXME("iface %p, flags %#x, timeout %p stub!\n", iface, Flags, Timeout);
2316
2317     /* When implementing this, implement it in WineD3D */
2318
2319     return DD_OK;
2320 }
2321
2322 /*****************************************************************************
2323  * IDirectDraw7::GetDeviceIdentifier
2324  *
2325  * Returns the device identifier, which gives information about the driver
2326  * Our device identifier is defined at the beginning of this file.
2327  *
2328  * Params:
2329  *  DDDI: Address for the returned structure
2330  *  Flags: Can be DDGDI_GETHOSTIDENTIFIER
2331  *
2332  * Returns:
2333  *  On success it returns DD_OK
2334  *  DDERR_INVALIDPARAMS if DDDI is NULL
2335  *
2336  *****************************************************************************/
2337 static HRESULT WINAPI ddraw7_GetDeviceIdentifier(IDirectDraw7 *iface,
2338         DDDEVICEIDENTIFIER2 *DDDI, DWORD Flags)
2339 {
2340     TRACE("iface %p, device_identifier %p, flags %#x.\n", iface, DDDI, Flags);
2341
2342     if(!DDDI)
2343         return DDERR_INVALIDPARAMS;
2344
2345     /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
2346      * host adapter, if there's a secondary 3D adapter. This doesn't apply
2347      * to any modern hardware, nor is it interesting for Wine, so ignore it.
2348      * Size of DDDEVICEIDENTIFIER2 may be aligned to 8 bytes and thus 4
2349      * bytes too long. So only copy the relevant part of the structure
2350      */
2351
2352     memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
2353     return DD_OK;
2354 }
2355
2356 static HRESULT WINAPI ddraw4_GetDeviceIdentifier(IDirectDraw4 *iface,
2357         DDDEVICEIDENTIFIER *identifier, DWORD flags)
2358 {
2359     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2360     DDDEVICEIDENTIFIER2 identifier2;
2361     HRESULT hr;
2362
2363     TRACE("iface %p, identifier %p, flags %#x.\n", iface, identifier, flags);
2364
2365     hr = ddraw7_GetDeviceIdentifier(&ddraw->IDirectDraw7_iface, &identifier2, flags);
2366     DDRAW_Convert_DDDEVICEIDENTIFIER_2_To_1(&identifier2, identifier);
2367
2368     return hr;
2369 }
2370
2371 /*****************************************************************************
2372  * IDirectDraw7::GetSurfaceFromDC
2373  *
2374  * Returns the Surface for a GDI device context handle.
2375  * Is this related to IDirectDrawSurface::GetDC ???
2376  *
2377  * Params:
2378  *  hdc: hdc to return the surface for
2379  *  Surface: Address to write the surface pointer to
2380  *
2381  * Returns:
2382  *  Always returns DD_OK because it's a stub
2383  *
2384  *****************************************************************************/
2385 static HRESULT WINAPI ddraw7_GetSurfaceFromDC(IDirectDraw7 *iface, HDC hdc,
2386         IDirectDrawSurface7 **Surface)
2387 {
2388     struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2389     struct wined3d_surface *wined3d_surface;
2390     struct ddraw_surface *surface_impl;
2391
2392     TRACE("iface %p, dc %p, surface %p.\n", iface, hdc, Surface);
2393
2394     if (!Surface) return E_INVALIDARG;
2395
2396     if (!(wined3d_surface = wined3d_device_get_surface_from_dc(ddraw->wined3d_device, hdc)))
2397     {
2398         TRACE("No surface found for dc %p.\n", hdc);
2399         *Surface = NULL;
2400         return DDERR_NOTFOUND;
2401     }
2402
2403     surface_impl = wined3d_surface_get_parent(wined3d_surface);
2404     *Surface = &surface_impl->IDirectDrawSurface7_iface;
2405     IDirectDrawSurface7_AddRef(*Surface);
2406     TRACE("Returning surface %p.\n", Surface);
2407     return DD_OK;
2408 }
2409
2410 static HRESULT WINAPI ddraw4_GetSurfaceFromDC(IDirectDraw4 *iface, HDC dc,
2411         IDirectDrawSurface4 **surface)
2412 {
2413     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2414     struct ddraw_surface *surface_impl;
2415     IDirectDrawSurface7 *surface7;
2416     HRESULT hr;
2417
2418     TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2419
2420     if (!surface) return E_INVALIDARG;
2421
2422     hr = ddraw7_GetSurfaceFromDC(&ddraw->IDirectDraw7_iface, dc, &surface7);
2423     if (FAILED(hr))
2424     {
2425         *surface = NULL;
2426         return hr;
2427     }
2428     surface_impl = impl_from_IDirectDrawSurface7(surface7);
2429     /* Tests say this is true */
2430     *surface = (IDirectDrawSurface4 *)&surface_impl->IDirectDrawSurface_iface;
2431     IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
2432     IDirectDrawSurface7_Release(surface7);
2433
2434     return hr;
2435 }
2436
2437 /*****************************************************************************
2438  * IDirectDraw7::RestoreAllSurfaces
2439  *
2440  * Calls the restore method of all surfaces
2441  *
2442  * Params:
2443  *
2444  * Returns:
2445  *  Always returns DD_OK because it's a stub
2446  *
2447  *****************************************************************************/
2448 static HRESULT WINAPI ddraw7_RestoreAllSurfaces(IDirectDraw7 *iface)
2449 {
2450     FIXME("iface %p stub!\n", iface);
2451
2452     /* This isn't hard to implement: Enumerate all WineD3D surfaces,
2453      * get their parent and call their restore method. Do not implement
2454      * it in WineD3D, as restoring a surface means re-creating the
2455      * WineD3DDSurface
2456      */
2457     return DD_OK;
2458 }
2459
2460 static HRESULT WINAPI ddraw4_RestoreAllSurfaces(IDirectDraw4 *iface)
2461 {
2462     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2463
2464     TRACE("iface %p.\n", iface);
2465
2466     return ddraw7_RestoreAllSurfaces(&ddraw->IDirectDraw7_iface);
2467 }
2468
2469 /*****************************************************************************
2470  * IDirectDraw7::StartModeTest
2471  *
2472  * Tests the specified video modes to update the system registry with
2473  * refresh rate information. StartModeTest starts the mode test,
2474  * EvaluateMode is used to fail or pass a mode. If EvaluateMode
2475  * isn't called within 15 seconds, the mode is failed automatically
2476  *
2477  * As refresh rates are handled by the X server, I don't think this
2478  * Method is important
2479  *
2480  * Params:
2481  *  Modes: An array of mode specifications
2482  *  NumModes: The number of modes in Modes
2483  *  Flags: Some flags...
2484  *
2485  * Returns:
2486  *  Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
2487  *  if no modes are passed, DDERR_INVALIDPARAMS is returned,
2488  *  otherwise DD_OK
2489  *
2490  *****************************************************************************/
2491 static HRESULT WINAPI ddraw7_StartModeTest(IDirectDraw7 *iface, SIZE *Modes, DWORD NumModes, DWORD Flags)
2492 {
2493     FIXME("iface %p, modes %p, mode_count %u, flags %#x partial stub!\n",
2494             iface, Modes, NumModes, Flags);
2495
2496     /* This looks sane */
2497     if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
2498
2499     /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
2500      * As it is not, DDERR_TESTFINISHED is returned
2501      * (hopefully that's correct
2502      *
2503     if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
2504      * well, that value doesn't (yet) exist in the wine headers, so ignore it
2505      */
2506
2507     return DD_OK;
2508 }
2509
2510 /*****************************************************************************
2511  * ddraw_create_surface
2512  *
2513  * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
2514  * with the passed parameters.
2515  *
2516  * Params:
2517  *  DDSD: Description of the surface to create
2518  *  Surf: Address to store the interface pointer at
2519  *
2520  * Returns:
2521  *  DD_OK on success
2522  *
2523  *****************************************************************************/
2524 static HRESULT ddraw_create_surface(struct ddraw *ddraw, DDSURFACEDESC2 *pDDSD,
2525         struct ddraw_surface **surface, UINT level, UINT version)
2526 {
2527     HRESULT hr;
2528
2529     TRACE("ddraw %p, surface_desc %p, surface %p, level %u.\n",
2530             ddraw, pDDSD, surface, level);
2531
2532     if (TRACE_ON(ddraw))
2533     {
2534         TRACE("Requesting surface desc:\n");
2535         DDRAW_dump_surface_desc(pDDSD);
2536     }
2537
2538     if ((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && DefaultSurfaceType != WINED3D_SURFACE_TYPE_OPENGL)
2539     {
2540         WARN("The application requests a 3D capable surface, but a non-OpenGL surface type was set in the registry.\n");
2541         /* Do not fail surface creation, only fail 3D device creation. */
2542     }
2543
2544     /* Create the Surface object */
2545     *surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**surface));
2546     if (!*surface)
2547     {
2548         ERR("Failed to allocate surface memory.\n");
2549         return DDERR_OUTOFVIDEOMEMORY;
2550     }
2551
2552     hr = ddraw_surface_init(*surface, ddraw, pDDSD, level, version);
2553     if (FAILED(hr))
2554     {
2555         WARN("Failed to initialize surface, hr %#x.\n", hr);
2556         HeapFree(GetProcessHeap(), 0, *surface);
2557         return hr;
2558     }
2559
2560     /* Increase the surface counter, and attach the surface */
2561     list_add_head(&ddraw->surface_list, &(*surface)->surface_list_entry);
2562
2563     TRACE("Created surface %p.\n", *surface);
2564
2565     return DD_OK;
2566 }
2567
2568 static HRESULT CDECL ddraw_reset_enum_callback(struct wined3d_resource *resource)
2569 {
2570     return DD_OK;
2571 }
2572
2573 /*****************************************************************************
2574  * IDirectDraw7::CreateSurface
2575  *
2576  * Creates a new IDirectDrawSurface object and returns its interface.
2577  *
2578  * The surface connections with wined3d are a bit tricky. Basically it works
2579  * like this:
2580  *
2581  * |------------------------|               |-----------------|
2582  * | DDraw surface          |               | WineD3DSurface  |
2583  * |                        |               |                 |
2584  * |        WineD3DSurface  |-------------->|                 |
2585  * |        Child           |<------------->| Parent          |
2586  * |------------------------|               |-----------------|
2587  *
2588  * The DDraw surface is the parent of the wined3d surface, and it releases
2589  * the WineD3DSurface when the ddraw surface is destroyed.
2590  *
2591  * However, for all surfaces which can be in a container in WineD3D,
2592  * we have to do this. These surfaces are usually complex surfaces,
2593  * so this concerns primary surfaces with a front and a back buffer,
2594  * and textures.
2595  *
2596  * |------------------------|               |-----------------|
2597  * | DDraw surface          |               | Container       |
2598  * |                        |               |                 |
2599  * |                  Child |<------------->| Parent          |
2600  * |                Texture |<------------->|                 |
2601  * |         WineD3DSurface |<----|         |          Levels |<--|
2602  * | Complex connection     |     |         |                 |   |
2603  * |------------------------|     |         |-----------------|   |
2604  *  ^                             |                               |
2605  *  |                             |                               |
2606  *  |                             |                               |
2607  *  |    |------------------|     |         |-----------------|   |
2608  *  |    | IParent          |     |-------->| WineD3DSurface  |   |
2609  *  |    |                  |               |                 |   |
2610  *  |    |            Child |<------------->| Parent          |   |
2611  *  |    |                  |               |       Container |<--|
2612  *  |    |------------------|               |-----------------|   |
2613  *  |                                                             |
2614  *  |   |----------------------|                                  |
2615  *  |   | DDraw surface 2      |                                  |
2616  *  |   |                      |                                  |
2617  *  |<->| Complex root   Child |                                  |
2618  *  |   |              Texture |                                  |
2619  *  |   |       WineD3DSurface |<----|                            |
2620  *  |   |----------------------|     |                            |
2621  *  |                                |                            |
2622  *  |    |---------------------|     |      |-----------------|   |
2623  *  |    | IParent             |     |----->| WineD3DSurface  |   |
2624  *  |    |                     |            |                 |   |
2625  *  |    |               Child |<---------->| Parent          |   |
2626  *  |    |---------------------|            |       Container |<--|
2627  *  |                                       |-----------------|   |
2628  *  |                                                             |
2629  *  |             ---More surfaces can follow---                  |
2630  *
2631  * The reason is that the IWineD3DSwapchain(render target container)
2632  * and the IWineD3DTexure(Texture container) release the parents
2633  * of their surface's children, but by releasing the complex root
2634  * the surfaces which are complexly attached to it are destroyed
2635  * too. See IDirectDrawSurface::Release for a more detailed
2636  * explanation.
2637  *
2638  * Params:
2639  *  DDSD: Description of the surface to create
2640  *  Surf: Address to store the interface pointer at
2641  *  UnkOuter: Basically for aggregation support, but ddraw doesn't support
2642  *            aggregation, so it has to be NULL
2643  *
2644  * Returns:
2645  *  DD_OK on success
2646  *  CLASS_E_NOAGGREGATION if UnkOuter != NULL
2647  *  DDERR_* if an error occurs
2648  *
2649  *****************************************************************************/
2650 static HRESULT CreateSurface(struct ddraw *ddraw, DDSURFACEDESC2 *DDSD,
2651         struct ddraw_surface **surface, IUnknown *UnkOuter, UINT version)
2652 {
2653     struct ddraw_surface *object = NULL;
2654     struct wined3d_display_mode mode;
2655     HRESULT hr;
2656     DDSURFACEDESC2 desc2;
2657     const DWORD sysvidmem = DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
2658
2659     TRACE("ddraw %p, surface_desc %p, surface %p, outer_unknown %p.\n", ddraw, DDSD, surface, UnkOuter);
2660
2661     /* Some checks before we start */
2662     if (TRACE_ON(ddraw))
2663     {
2664         TRACE(" (%p) Requesting surface desc :\n", ddraw);
2665         DDRAW_dump_surface_desc(DDSD);
2666     }
2667
2668     if (UnkOuter != NULL)
2669     {
2670         FIXME("(%p) : outer != NULL?\n", ddraw);
2671         return CLASS_E_NOAGGREGATION; /* unchecked */
2672     }
2673
2674     if (!surface)
2675     {
2676         FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", ddraw);
2677         return E_POINTER; /* unchecked */
2678     }
2679
2680     if (!(DDSD->dwFlags & DDSD_CAPS))
2681     {
2682         /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2683         DDSD->dwFlags |= DDSD_CAPS;
2684     }
2685
2686     if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2687     {
2688         /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2689         DDSD->dwFlags &= ~DDSD_LPSURFACE;
2690     }
2691
2692     if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2693     {
2694         /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2695         WARN("(%p) Null surface pointer specified, ignore it!\n", ddraw);
2696         DDSD->dwFlags &= ~DDSD_LPSURFACE;
2697     }
2698
2699     if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2700        !(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
2701     {
2702         TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n",
2703                 ddraw);
2704         *surface = NULL;
2705         return DDERR_NOEXCLUSIVEMODE;
2706     }
2707
2708     if((DDSD->ddsCaps.dwCaps & (DDSCAPS_BACKBUFFER | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_BACKBUFFER | DDSCAPS_PRIMARYSURFACE))
2709     {
2710         WARN("Application wanted to create back buffer primary surface\n");
2711         return DDERR_INVALIDCAPS;
2712     }
2713
2714     if((DDSD->ddsCaps.dwCaps & sysvidmem) == sysvidmem)
2715     {
2716         /* This is a special switch in ddrawex.dll, but not allowed in ddraw.dll */
2717         WARN("Application tries to put the surface in both system and video memory\n");
2718         *surface = NULL;
2719         return DDERR_INVALIDCAPS;
2720     }
2721
2722     /* Check cube maps but only if the size includes them */
2723     if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2724     {
2725         if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2726            !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2727         {
2728             WARN("Cube map faces requested without cube map flag\n");
2729             return DDERR_INVALIDCAPS;
2730         }
2731         if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2732            (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2733         {
2734             WARN("Cube map without faces requested\n");
2735             return DDERR_INVALIDPARAMS;
2736         }
2737
2738         /* Quick tests confirm those can be created, but we don't do that yet */
2739         if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2740            (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2741         {
2742             FIXME("Partial cube maps not supported yet\n");
2743         }
2744     }
2745
2746     /* According to the msdn this flag is ignored by CreateSurface */
2747     if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2748         DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2749
2750     /* Modify some flags */
2751     copy_to_surfacedesc2(&desc2, DDSD);
2752     desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2753
2754     if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
2755     {
2756         ERR("Failed to get display mode, hr %#x.\n", hr);
2757         return hr;
2758     }
2759
2760     /* No pixelformat given? Use the current screen format */
2761     if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2762     {
2763         desc2.dwFlags |= DDSD_PIXELFORMAT;
2764         desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2765
2766         PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, mode.format_id);
2767     }
2768
2769     /* No Width or no Height? Use the original screen size
2770      */
2771     if(!(desc2.dwFlags & DDSD_WIDTH) ||
2772        !(desc2.dwFlags & DDSD_HEIGHT) )
2773     {
2774         /* Invalid for non-render targets */
2775         if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2776         {
2777             WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2778             *surface = NULL;
2779             return DDERR_INVALIDPARAMS;
2780         }
2781
2782         desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2783         desc2.dwWidth = mode.width;
2784         desc2.dwHeight = mode.height;
2785     }
2786
2787     if (!desc2.dwWidth || !desc2.dwHeight)
2788         return DDERR_INVALIDPARAMS;
2789
2790     /* Mipmap count fixes */
2791     if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2792     {
2793         if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2794         {
2795             if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2796             {
2797                 /* Mipmap count is given, should not be 0 */
2798                 if( desc2.u2.dwMipMapCount == 0 )
2799                     return DDERR_INVALIDPARAMS;
2800             }
2801             else
2802             {
2803                 /* Undocumented feature: Create sublevels until
2804                  * either the width or the height is 1
2805                  */
2806                 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2807                             desc2.dwWidth : desc2.dwHeight;
2808                 desc2.u2.dwMipMapCount = 0;
2809                 while( min )
2810                 {
2811                     desc2.u2.dwMipMapCount += 1;
2812                     min >>= 1;
2813                 }
2814             }
2815         }
2816         else
2817         {
2818             /* Not-complex mipmap -> Mipmapcount = 1 */
2819             desc2.u2.dwMipMapCount = 1;
2820         }
2821
2822         /* There's a mipmap count in the created surface in any case */
2823         desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2824     }
2825     /* If no mipmap is given, the texture has only one level */
2826
2827     /* The first surface is a front buffer, the back buffer is created afterwards */
2828     if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2829     {
2830         desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2831     }
2832
2833     /* The root surface in a cube map is positive x */
2834     if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2835     {
2836         desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2837         desc2.ddsCaps.dwCaps2 |=  DDSCAPS2_CUBEMAP_POSITIVEX;
2838     }
2839
2840     if ((desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) && (ddraw->cooperative_level & DDSCL_EXCLUSIVE))
2841     {
2842         struct wined3d_swapchain_desc swapchain_desc;
2843
2844         wined3d_swapchain_get_desc(ddraw->wined3d_swapchain, &swapchain_desc);
2845         swapchain_desc.backbuffer_width = mode.width;
2846         swapchain_desc.backbuffer_height = mode.height;
2847         swapchain_desc.backbuffer_format = mode.format_id;
2848
2849         hr = wined3d_device_reset(ddraw->wined3d_device,
2850                 &swapchain_desc, NULL, ddraw_reset_enum_callback);
2851         if (FAILED(hr))
2852         {
2853             ERR("Failed to reset device.\n");
2854             return hr;
2855         }
2856     }
2857
2858     /* Create the first surface */
2859     hr = ddraw_create_surface(ddraw, &desc2, &object, 0, version);
2860     if (FAILED(hr))
2861     {
2862         WARN("ddraw_create_surface failed, hr %#x.\n", hr);
2863         return hr;
2864     }
2865     object->is_complex_root = TRUE;
2866
2867     *surface = object;
2868
2869     /* Create Additional surfaces if necessary
2870      * This applies to Primary surfaces which have a back buffer count
2871      * set, but not to mipmap textures. In case of Mipmap textures,
2872      * wineD3D takes care of the creation of additional surfaces
2873      */
2874     if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2875     {
2876         struct ddraw_surface *last = object;
2877         UINT i;
2878
2879         desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2880         desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2881         desc2.dwBackBufferCount = 0;
2882
2883         for (i = 0; i < DDSD->dwBackBufferCount; ++i)
2884         {
2885             struct ddraw_surface *object2 = NULL;
2886
2887             if (FAILED(hr = ddraw_create_surface(ddraw, &desc2, &object2, 0, version)))
2888             {
2889                 if (version == 7)
2890                     IDirectDrawSurface7_Release(&object->IDirectDrawSurface7_iface);
2891                 else if (version == 4)
2892                     IDirectDrawSurface4_Release(&object->IDirectDrawSurface4_iface);
2893                 else
2894                     IDirectDrawSurface_Release(&object->IDirectDrawSurface_iface);
2895
2896                 return hr;
2897             }
2898
2899             /* Add the new surface to the complex attachment array. */
2900             last->complex_array[0] = object2;
2901             last = object2;
2902
2903             /* Remove the (possible) back buffer cap from the new surface
2904              * description, because only one surface in the flipping chain is a
2905              * back buffer, one is a front buffer, the others are just primary
2906              * surfaces. */
2907             desc2.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2908         }
2909     }
2910
2911     if (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2912         ddraw->primary = object;
2913
2914     /* Create a WineD3DTexture if a texture was requested */
2915     if (desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
2916         ddraw_surface_create_texture(object);
2917
2918     return hr;
2919 }
2920
2921 static HRESULT WINAPI ddraw7_CreateSurface(IDirectDraw7 *iface, DDSURFACEDESC2 *surface_desc,
2922         IDirectDrawSurface7 **surface, IUnknown *outer_unknown)
2923 {
2924     struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2925     struct ddraw_surface *impl;
2926     HRESULT hr;
2927
2928     TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2929             iface, surface_desc, surface, outer_unknown);
2930
2931     wined3d_mutex_lock();
2932
2933     if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2934     {
2935         WARN("Cooperative level not set.\n");
2936         wined3d_mutex_unlock();
2937         return DDERR_NOCOOPERATIVELEVELSET;
2938     }
2939
2940     if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
2941     {
2942         WARN("Application supplied invalid surface descriptor\n");
2943         wined3d_mutex_unlock();
2944         return DDERR_INVALIDPARAMS;
2945     }
2946
2947     if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
2948     {
2949         if (TRACE_ON(ddraw))
2950         {
2951             TRACE(" (%p) Requesting surface desc :\n", iface);
2952             DDRAW_dump_surface_desc(surface_desc);
2953         }
2954
2955         WARN("Application tried to create an explicit front or back buffer\n");
2956         wined3d_mutex_unlock();
2957         return DDERR_INVALIDCAPS;
2958     }
2959
2960     hr = CreateSurface(ddraw, surface_desc, &impl, outer_unknown, 7);
2961     wined3d_mutex_unlock();
2962     if (FAILED(hr))
2963     {
2964         *surface = NULL;
2965         return hr;
2966     }
2967
2968     *surface = &impl->IDirectDrawSurface7_iface;
2969     IDirectDraw7_AddRef(iface);
2970     impl->ifaceToRelease = (IUnknown *)iface;
2971
2972     return hr;
2973 }
2974
2975 static HRESULT WINAPI ddraw4_CreateSurface(IDirectDraw4 *iface,
2976         DDSURFACEDESC2 *surface_desc, IDirectDrawSurface4 **surface, IUnknown *outer_unknown)
2977 {
2978     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2979     struct ddraw_surface *impl;
2980     HRESULT hr;
2981
2982     TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2983             iface, surface_desc, surface, outer_unknown);
2984
2985     wined3d_mutex_lock();
2986
2987     if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
2988     {
2989         WARN("Cooperative level not set.\n");
2990         wined3d_mutex_unlock();
2991         return DDERR_NOCOOPERATIVELEVELSET;
2992     }
2993
2994     if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
2995     {
2996         WARN("Application supplied invalid surface descriptor\n");
2997         wined3d_mutex_unlock();
2998         return DDERR_INVALIDPARAMS;
2999     }
3000
3001     if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3002     {
3003         if (TRACE_ON(ddraw))
3004         {
3005             TRACE(" (%p) Requesting surface desc :\n", iface);
3006             DDRAW_dump_surface_desc(surface_desc);
3007         }
3008
3009         WARN("Application tried to create an explicit front or back buffer\n");
3010         wined3d_mutex_unlock();
3011         return DDERR_INVALIDCAPS;
3012     }
3013
3014     hr = CreateSurface(ddraw, surface_desc, &impl, outer_unknown, 4);
3015     wined3d_mutex_unlock();
3016     if (FAILED(hr))
3017     {
3018         *surface = NULL;
3019         return hr;
3020     }
3021
3022     *surface = &impl->IDirectDrawSurface4_iface;
3023     IDirectDraw4_AddRef(iface);
3024     impl->ifaceToRelease = (IUnknown *)iface;
3025
3026     return hr;
3027 }
3028
3029 static HRESULT WINAPI ddraw2_CreateSurface(IDirectDraw2 *iface,
3030         DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3031 {
3032     struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3033     struct ddraw_surface *impl;
3034     HRESULT hr;
3035     DDSURFACEDESC2 surface_desc2;
3036
3037     TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3038             iface, surface_desc, surface, outer_unknown);
3039
3040     wined3d_mutex_lock();
3041
3042     if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
3043     {
3044         WARN("Cooperative level not set.\n");
3045         wined3d_mutex_unlock();
3046         return DDERR_NOCOOPERATIVELEVELSET;
3047     }
3048
3049     if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3050     {
3051         WARN("Application supplied invalid surface descriptor\n");
3052         wined3d_mutex_unlock();
3053         return DDERR_INVALIDPARAMS;
3054     }
3055
3056     DDSD_to_DDSD2(surface_desc, &surface_desc2);
3057     if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3058     {
3059         if (TRACE_ON(ddraw))
3060         {
3061             TRACE(" (%p) Requesting surface desc :\n", iface);
3062             DDRAW_dump_surface_desc((LPDDSURFACEDESC2)surface_desc);
3063         }
3064
3065         WARN("Application tried to create an explicit front or back buffer\n");
3066         wined3d_mutex_unlock();
3067         return DDERR_INVALIDCAPS;
3068     }
3069
3070     hr = CreateSurface(ddraw, &surface_desc2, &impl, outer_unknown, 2);
3071     wined3d_mutex_unlock();
3072     if (FAILED(hr))
3073     {
3074         *surface = NULL;
3075         return hr;
3076     }
3077
3078     *surface = &impl->IDirectDrawSurface_iface;
3079     impl->ifaceToRelease = NULL;
3080
3081     return hr;
3082 }
3083
3084 static HRESULT WINAPI ddraw1_CreateSurface(IDirectDraw *iface,
3085         DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3086 {
3087     struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3088     struct ddraw_surface *impl;
3089     HRESULT hr;
3090     DDSURFACEDESC2 surface_desc2;
3091
3092     TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3093             iface, surface_desc, surface, outer_unknown);
3094
3095     wined3d_mutex_lock();
3096
3097     if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
3098     {
3099         WARN("Cooperative level not set.\n");
3100         wined3d_mutex_unlock();
3101         return DDERR_NOCOOPERATIVELEVELSET;
3102     }
3103
3104     if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3105     {
3106         WARN("Application supplied invalid surface descriptor\n");
3107         wined3d_mutex_unlock();
3108         return DDERR_INVALIDPARAMS;
3109     }
3110
3111     /* Remove front buffer flag, this causes failure in v7, and its added to normal
3112      * primaries anyway. */
3113     surface_desc->ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER;
3114     DDSD_to_DDSD2(surface_desc, &surface_desc2);
3115     hr = CreateSurface(ddraw, &surface_desc2, &impl, outer_unknown, 1);
3116     wined3d_mutex_unlock();
3117     if (FAILED(hr))
3118     {
3119         *surface = NULL;
3120         return hr;
3121     }
3122
3123     *surface = &impl->IDirectDrawSurface_iface;
3124     impl->ifaceToRelease = NULL;
3125
3126     return hr;
3127 }
3128
3129 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
3130 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
3131
3132 static BOOL
3133 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
3134                                     const DDPIXELFORMAT *provided)
3135 {
3136     /* Some flags must be present in both or neither for a match. */
3137     static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
3138         | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
3139         | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
3140
3141     if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3142         return FALSE;
3143
3144     if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
3145         return FALSE;
3146
3147     if (requested->dwFlags & DDPF_FOURCC)
3148         if (requested->dwFourCC != provided->dwFourCC)
3149             return FALSE;
3150
3151     if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
3152                               |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3153         if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
3154             return FALSE;
3155
3156     if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3157                               |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3158         if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
3159             return FALSE;
3160
3161     if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
3162         if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
3163             return FALSE;
3164
3165     /* I could be wrong about the bumpmapping. MSDN docs are vague. */
3166     if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3167                               |DDPF_BUMPDUDV))
3168         if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
3169             return FALSE;
3170
3171     if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
3172         if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
3173             return FALSE;
3174
3175     return TRUE;
3176 }
3177
3178 static BOOL ddraw_match_surface_desc(const DDSURFACEDESC2 *requested, const DDSURFACEDESC2 *provided)
3179 {
3180     struct compare_info
3181     {
3182         DWORD flag;
3183         ptrdiff_t offset;
3184         size_t size;
3185     };
3186
3187 #define CMP(FLAG, FIELD)                                \
3188         { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
3189           sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
3190
3191     static const struct compare_info compare[] =
3192     {
3193         CMP(ALPHABITDEPTH, dwAlphaBitDepth),
3194         CMP(BACKBUFFERCOUNT, dwBackBufferCount),
3195         CMP(CAPS, ddsCaps),
3196         CMP(CKDESTBLT, ddckCKDestBlt),
3197         CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
3198         CMP(CKSRCBLT, ddckCKSrcBlt),
3199         CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
3200         CMP(HEIGHT, dwHeight),
3201         CMP(LINEARSIZE, u1 /* dwLinearSize */),
3202         CMP(LPSURFACE, lpSurface),
3203         CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
3204         CMP(PITCH, u1 /* lPitch */),
3205         /* PIXELFORMAT: manual */
3206         CMP(REFRESHRATE, u2 /* dwRefreshRate */),
3207         CMP(TEXTURESTAGE, dwTextureStage),
3208         CMP(WIDTH, dwWidth),
3209         /* ZBUFFERBITDEPTH: "obsolete" */
3210     };
3211
3212 #undef CMP
3213
3214     unsigned int i;
3215
3216     if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3217         return FALSE;
3218
3219     for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
3220     {
3221         if (requested->dwFlags & compare[i].flag
3222             && memcmp((const char *)provided + compare[i].offset,
3223                       (const char *)requested + compare[i].offset,
3224                       compare[i].size) != 0)
3225             return FALSE;
3226     }
3227
3228     if (requested->dwFlags & DDSD_PIXELFORMAT)
3229     {
3230         if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
3231                                                 &provided->u4.ddpfPixelFormat))
3232             return FALSE;
3233     }
3234
3235     return TRUE;
3236 }
3237
3238 #undef DDENUMSURFACES_SEARCHTYPE
3239 #undef DDENUMSURFACES_MATCHTYPE
3240
3241 struct surfacescallback2_context
3242 {
3243     LPDDENUMSURFACESCALLBACK2 func;
3244     void *context;
3245 };
3246
3247 struct surfacescallback_context
3248 {
3249     LPDDENUMSURFACESCALLBACK func;
3250     void *context;
3251 };
3252
3253 static HRESULT CALLBACK EnumSurfacesCallback2Thunk(IDirectDrawSurface7 *surface,
3254         DDSURFACEDESC2 *surface_desc, void *context)
3255 {
3256     struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3257     struct surfacescallback2_context *cbcontext = context;
3258
3259     IDirectDrawSurface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
3260     IDirectDrawSurface7_Release(surface);
3261
3262     return cbcontext->func(&surface_impl->IDirectDrawSurface4_iface,
3263             surface_desc, cbcontext->context);
3264 }
3265
3266 static HRESULT CALLBACK EnumSurfacesCallbackThunk(IDirectDrawSurface7 *surface,
3267         DDSURFACEDESC2 *surface_desc, void *context)
3268 {
3269     struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3270     struct surfacescallback_context *cbcontext = context;
3271
3272     IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
3273     IDirectDrawSurface7_Release(surface);
3274
3275     return cbcontext->func(&surface_impl->IDirectDrawSurface_iface,
3276             (DDSURFACEDESC *)surface_desc, cbcontext->context);
3277 }
3278
3279 /*****************************************************************************
3280  * IDirectDraw7::EnumSurfaces
3281  *
3282  * Loops through all surfaces attached to this device and calls the
3283  * application callback. This can't be relayed to WineD3DDevice,
3284  * because some WineD3DSurfaces' parents are IParent objects
3285  *
3286  * Params:
3287  *  Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
3288  *  DDSD: Description to filter for
3289  *  Context: Application-provided pointer, it's passed unmodified to the
3290  *           Callback function
3291  *  Callback: Address to call for each surface
3292  *
3293  * Returns:
3294  *  DDERR_INVALIDPARAMS if the callback is NULL
3295  *  DD_OK on success
3296  *
3297  *****************************************************************************/
3298 static HRESULT WINAPI ddraw7_EnumSurfaces(IDirectDraw7 *iface, DWORD Flags,
3299         DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMSURFACESCALLBACK7 Callback)
3300 {
3301     struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
3302     struct ddraw_surface *surf;
3303     BOOL all, nomatch;
3304     DDSURFACEDESC2 desc;
3305     struct list *entry, *entry2;
3306
3307     TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3308             iface, Flags, DDSD, Context, Callback);
3309
3310     all = Flags & DDENUMSURFACES_ALL;
3311     nomatch = Flags & DDENUMSURFACES_NOMATCH;
3312
3313     if (!Callback)
3314         return DDERR_INVALIDPARAMS;
3315
3316     wined3d_mutex_lock();
3317
3318     /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
3319     LIST_FOR_EACH_SAFE(entry, entry2, &ddraw->surface_list)
3320     {
3321         surf = LIST_ENTRY(entry, struct ddraw_surface, surface_list_entry);
3322
3323         if (!surf->iface_count)
3324         {
3325             WARN("Not enumerating surface %p because it doesn't have any references.\n", surf);
3326             continue;
3327         }
3328
3329         if (all || (nomatch != ddraw_match_surface_desc(DDSD, &surf->surface_desc)))
3330         {
3331             TRACE("Enumerating surface %p.\n", surf);
3332             desc = surf->surface_desc;
3333             IDirectDrawSurface7_AddRef(&surf->IDirectDrawSurface7_iface);
3334             if (Callback(&surf->IDirectDrawSurface7_iface, &desc, Context) != DDENUMRET_OK)
3335             {
3336                 wined3d_mutex_unlock();
3337                 return DD_OK;
3338             }
3339         }
3340     }
3341
3342     wined3d_mutex_unlock();
3343
3344     return DD_OK;
3345 }
3346
3347 static HRESULT WINAPI ddraw4_EnumSurfaces(IDirectDraw4 *iface, DWORD flags,
3348         DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMSURFACESCALLBACK2 callback)
3349 {
3350     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3351     struct surfacescallback2_context cbcontext;
3352
3353     TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3354             iface, flags, surface_desc, context, callback);
3355
3356     cbcontext.func = callback;
3357     cbcontext.context = context;
3358
3359     return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags, surface_desc,
3360             &cbcontext, EnumSurfacesCallback2Thunk);
3361 }
3362
3363 static HRESULT WINAPI ddraw2_EnumSurfaces(IDirectDraw2 *iface, DWORD flags,
3364         DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3365 {
3366     struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3367     struct surfacescallback_context cbcontext;
3368     DDSURFACEDESC2 surface_desc2;
3369
3370     TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3371             iface, flags, surface_desc, context, callback);
3372
3373     cbcontext.func = callback;
3374     cbcontext.context = context;
3375
3376     if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3377     return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags,
3378             surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3379 }
3380
3381 static HRESULT WINAPI ddraw1_EnumSurfaces(IDirectDraw *iface, DWORD flags,
3382         DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3383 {
3384     struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3385     struct surfacescallback_context cbcontext;
3386     DDSURFACEDESC2 surface_desc2;
3387
3388     TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3389             iface, flags, surface_desc, context, callback);
3390
3391     cbcontext.func = callback;
3392     cbcontext.context = context;
3393
3394     if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3395     return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags,
3396             surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3397 }
3398
3399 /*****************************************************************************
3400  * DirectDrawCreateClipper (DDRAW.@)
3401  *
3402  * Creates a new IDirectDrawClipper object.
3403  *
3404  * Params:
3405  *  Clipper: Address to write the interface pointer to
3406  *  UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3407  *            NULL
3408  *
3409  * Returns:
3410  *  CLASS_E_NOAGGREGATION if UnkOuter != NULL
3411  *  E_OUTOFMEMORY if allocating the object failed
3412  *
3413  *****************************************************************************/
3414 HRESULT WINAPI DirectDrawCreateClipper(DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3415 {
3416     struct ddraw_clipper *object;
3417     HRESULT hr;
3418
3419     TRACE("flags %#x, clipper %p, outer_unknown %p.\n",
3420             flags, clipper, outer_unknown);
3421
3422     if (outer_unknown)
3423         return CLASS_E_NOAGGREGATION;
3424
3425     wined3d_mutex_lock();
3426
3427     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3428     if (!object)
3429     {
3430         wined3d_mutex_unlock();
3431         return E_OUTOFMEMORY;
3432     }
3433
3434     hr = ddraw_clipper_init(object);
3435     if (FAILED(hr))
3436     {
3437         WARN("Failed to initialize clipper, hr %#x.\n", hr);
3438         HeapFree(GetProcessHeap(), 0, object);
3439         wined3d_mutex_unlock();
3440         return hr;
3441     }
3442
3443     TRACE("Created clipper %p.\n", object);
3444     *clipper = &object->IDirectDrawClipper_iface;
3445     wined3d_mutex_unlock();
3446
3447     return DD_OK;
3448 }
3449
3450 /*****************************************************************************
3451  * IDirectDraw7::CreateClipper
3452  *
3453  * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3454  *
3455  *****************************************************************************/
3456 static HRESULT WINAPI ddraw7_CreateClipper(IDirectDraw7 *iface, DWORD Flags,
3457         IDirectDrawClipper **Clipper, IUnknown *UnkOuter)
3458 {
3459     TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3460             iface, Flags, Clipper, UnkOuter);
3461
3462     return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3463 }
3464
3465 static HRESULT WINAPI ddraw4_CreateClipper(IDirectDraw4 *iface, DWORD flags,
3466         IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3467 {
3468     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3469
3470     TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3471             iface, flags, clipper, outer_unknown);
3472
3473     return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3474 }
3475
3476 static HRESULT WINAPI ddraw2_CreateClipper(IDirectDraw2 *iface,
3477         DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3478 {
3479     struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3480
3481     TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3482             iface, flags, clipper, outer_unknown);
3483
3484     return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3485 }
3486
3487 static HRESULT WINAPI ddraw1_CreateClipper(IDirectDraw *iface,
3488         DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3489 {
3490     struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3491
3492     TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3493             iface, flags, clipper, outer_unknown);
3494
3495     return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3496 }
3497
3498 /*****************************************************************************
3499  * IDirectDraw7::CreatePalette
3500  *
3501  * Creates a new IDirectDrawPalette object
3502  *
3503  * Params:
3504  *  Flags: The flags for the new clipper
3505  *  ColorTable: Color table to assign to the new clipper
3506  *  Palette: Address to write the interface pointer to
3507  *  UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3508  *            NULL
3509  *
3510  * Returns:
3511  *  CLASS_E_NOAGGREGATION if UnkOuter != NULL
3512  *  E_OUTOFMEMORY if allocating the object failed
3513  *
3514  *****************************************************************************/
3515 static HRESULT WINAPI ddraw7_CreatePalette(IDirectDraw7 *iface, DWORD Flags,
3516         PALETTEENTRY *ColorTable, IDirectDrawPalette **Palette, IUnknown *pUnkOuter)
3517 {
3518     struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
3519     struct ddraw_palette *object;
3520     HRESULT hr;
3521
3522     TRACE("iface %p, flags %#x, color_table %p, palette %p, outer_unknown %p.\n",
3523             iface, Flags, ColorTable, Palette, pUnkOuter);
3524
3525     if (pUnkOuter)
3526         return CLASS_E_NOAGGREGATION;
3527
3528     wined3d_mutex_lock();
3529
3530     /* The refcount test shows that a cooplevel is required for this */
3531     if (!ddraw->cooperative_level)
3532     {
3533         WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3534         wined3d_mutex_unlock();
3535         return DDERR_NOCOOPERATIVELEVELSET;
3536     }
3537
3538     object = HeapAlloc(GetProcessHeap(), 0, sizeof(*object));
3539     if (!object)
3540     {
3541         ERR("Out of memory when allocating memory for a palette implementation\n");
3542         wined3d_mutex_unlock();
3543         return E_OUTOFMEMORY;
3544     }
3545
3546     hr = ddraw_palette_init(object, ddraw, Flags, ColorTable);
3547     if (FAILED(hr))
3548     {
3549         WARN("Failed to initialize palette, hr %#x.\n", hr);
3550         HeapFree(GetProcessHeap(), 0, object);
3551         wined3d_mutex_unlock();
3552         return hr;
3553     }
3554
3555     TRACE("Created palette %p.\n", object);
3556     *Palette = &object->IDirectDrawPalette_iface;
3557     wined3d_mutex_unlock();
3558
3559     return DD_OK;
3560 }
3561
3562 static HRESULT WINAPI ddraw4_CreatePalette(IDirectDraw4 *iface, DWORD flags, PALETTEENTRY *entries,
3563         IDirectDrawPalette **palette, IUnknown *outer_unknown)
3564 {
3565     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3566     HRESULT hr;
3567
3568     TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3569             iface, flags, entries, palette, outer_unknown);
3570
3571     hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3572     if (SUCCEEDED(hr) && *palette)
3573     {
3574         struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3575         IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3576         IDirectDraw4_AddRef(iface);
3577         impl->ifaceToRelease = (IUnknown *)iface;
3578     }
3579     return hr;
3580 }
3581
3582 static HRESULT WINAPI ddraw2_CreatePalette(IDirectDraw2 *iface, DWORD flags,
3583         PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3584 {
3585     struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3586     HRESULT hr;
3587
3588     TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3589             iface, flags, entries, palette, outer_unknown);
3590
3591     hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3592     if (SUCCEEDED(hr) && *palette)
3593     {
3594         struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3595         IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3596         impl->ifaceToRelease = NULL;
3597     }
3598
3599     return hr;
3600 }
3601
3602 static HRESULT WINAPI ddraw1_CreatePalette(IDirectDraw *iface, DWORD flags,
3603         PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3604 {
3605     struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3606     HRESULT hr;
3607
3608     TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3609             iface, flags, entries, palette, outer_unknown);
3610
3611     hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3612     if (SUCCEEDED(hr) && *palette)
3613     {
3614         struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3615         IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3616         impl->ifaceToRelease = NULL;
3617     }
3618
3619     return hr;
3620 }
3621
3622 /*****************************************************************************
3623  * IDirectDraw7::DuplicateSurface
3624  *
3625  * Duplicates a surface. The surface memory points to the same memory as
3626  * the original surface, and it's released when the last surface referencing
3627  * it is released. I guess that's beyond Wine's surface management right now
3628  * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3629  * test application to implement this)
3630  *
3631  * Params:
3632  *  Src: Address of the source surface
3633  *  Dest: Address to write the new surface pointer to
3634  *
3635  * Returns:
3636  *  See IDirectDraw7::CreateSurface
3637  *
3638  *****************************************************************************/
3639 static HRESULT WINAPI ddraw7_DuplicateSurface(IDirectDraw7 *iface,
3640         IDirectDrawSurface7 *Src, IDirectDrawSurface7 **Dest)
3641 {
3642     struct ddraw_surface *src_surface = unsafe_impl_from_IDirectDrawSurface7(Src);
3643
3644     FIXME("iface %p, src %p, dst %p partial stub!\n", iface, Src, Dest);
3645
3646     /* For now, simply create a new, independent surface */
3647     return IDirectDraw7_CreateSurface(iface, &src_surface->surface_desc, Dest, NULL);
3648 }
3649
3650 static HRESULT WINAPI ddraw4_DuplicateSurface(IDirectDraw4 *iface, IDirectDrawSurface4 *src,
3651         IDirectDrawSurface4 **dst)
3652 {
3653     struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src);
3654     struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3655     struct ddraw_surface *dst_impl;
3656     IDirectDrawSurface7 *dst7;
3657     HRESULT hr;
3658
3659     TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3660
3661     hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3662             src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3663     if (FAILED(hr))
3664     {
3665         *dst = NULL;
3666         return hr;
3667     }
3668     dst_impl = impl_from_IDirectDrawSurface7(dst7);
3669     *dst = &dst_impl->IDirectDrawSurface4_iface;
3670     IDirectDrawSurface4_AddRef(*dst);
3671     IDirectDrawSurface7_Release(dst7);
3672
3673     return hr;
3674 }
3675
3676 static HRESULT WINAPI ddraw2_DuplicateSurface(IDirectDraw2 *iface,
3677         IDirectDrawSurface *src, IDirectDrawSurface **dst)
3678 {
3679     struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3680     struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3681     struct ddraw_surface *dst_impl;
3682     IDirectDrawSurface7 *dst7;
3683     HRESULT hr;
3684
3685     TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3686
3687     hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3688             src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3689     if (FAILED(hr))
3690         return hr;
3691     dst_impl = impl_from_IDirectDrawSurface7(dst7);
3692     *dst = &dst_impl->IDirectDrawSurface_iface;
3693     IDirectDrawSurface_AddRef(*dst);
3694     IDirectDrawSurface7_Release(dst7);
3695
3696     return hr;
3697 }
3698
3699 static HRESULT WINAPI ddraw1_DuplicateSurface(IDirectDraw *iface, IDirectDrawSurface *src,
3700         IDirectDrawSurface **dst)
3701 {
3702     struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3703     struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3704     struct ddraw_surface *dst_impl;
3705     IDirectDrawSurface7 *dst7;
3706     HRESULT hr;
3707
3708     TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3709
3710     hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3711             src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3712     if (FAILED(hr))
3713         return hr;
3714     dst_impl = impl_from_IDirectDrawSurface7(dst7);
3715     *dst = &dst_impl->IDirectDrawSurface_iface;
3716     IDirectDrawSurface_AddRef(*dst);
3717     IDirectDrawSurface7_Release(dst7);
3718
3719     return hr;
3720 }
3721
3722 /*****************************************************************************
3723  * IDirect3D7::EnumDevices
3724  *
3725  * The EnumDevices method for IDirect3D7. It enumerates all supported
3726  * D3D7 devices. Currently the T&L, HAL and RGB devices are enumerated.
3727  *
3728  * Params:
3729  *  callback: Function to call for each enumerated device
3730  *  context: Pointer to pass back to the app
3731  *
3732  * Returns:
3733  *  D3D_OK, or the return value of the GetCaps call
3734  *
3735  *****************************************************************************/
3736 static HRESULT WINAPI d3d7_EnumDevices(IDirect3D7 *iface, LPD3DENUMDEVICESCALLBACK7 callback, void *context)
3737 {
3738     struct ddraw *ddraw = impl_from_IDirect3D7(iface);
3739     D3DDEVICEDESC7 device_desc7;
3740     D3DDEVICEDESC device_desc1;
3741     HRESULT hr;
3742     size_t i;
3743
3744     TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3745
3746     if (!callback)
3747         return DDERR_INVALIDPARAMS;
3748
3749     wined3d_mutex_lock();
3750
3751     hr = IDirect3DImpl_GetCaps(ddraw->wined3d, &device_desc1, &device_desc7);
3752     if (hr != D3D_OK)
3753     {
3754         wined3d_mutex_unlock();
3755         return hr;
3756     }
3757
3758     for (i = 0; i < sizeof(device_list7)/sizeof(device_list7[0]); i++)
3759     {
3760         HRESULT ret;
3761
3762         device_desc7.deviceGUID = *device_list7[i].device_guid;
3763         ret = callback(device_list7[i].interface_name, device_list7[i].device_name, &device_desc7, context);
3764         if (ret != DDENUMRET_OK)
3765         {
3766             TRACE("Application cancelled the enumeration.\n");
3767             wined3d_mutex_unlock();
3768             return D3D_OK;
3769         }
3770     }
3771
3772     TRACE("End of enumeration.\n");
3773
3774     wined3d_mutex_unlock();
3775
3776     return D3D_OK;
3777 }
3778
3779 /*****************************************************************************
3780  * IDirect3D3::EnumDevices
3781  *
3782  * Enumerates all supported Direct3DDevice interfaces. This is the
3783  * implementation for Direct3D 1 to Direc3D 3, Version 7 has its own.
3784  *
3785  * Version 1, 2 and 3
3786  *
3787  * Params:
3788  *  callback: Application-provided routine to call for each enumerated device
3789  *  Context: Pointer to pass to the callback
3790  *
3791  * Returns:
3792  *  D3D_OK on success,
3793  *  The result of IDirect3DImpl_GetCaps if it failed
3794  *
3795  *****************************************************************************/
3796 static HRESULT WINAPI d3d3_EnumDevices(IDirect3D3 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3797 {
3798     static CHAR wined3d_description[] = "Wine D3DDevice using WineD3D and OpenGL";
3799
3800     struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3801     D3DDEVICEDESC device_desc1, hal_desc, hel_desc;
3802     D3DDEVICEDESC7 device_desc7;
3803     HRESULT hr;
3804
3805     /* Some games (Motoracer 2 demo) have the bad idea to modify the device
3806      * name string. Let's put the string in a sufficiently sized array in
3807      * writable memory. */
3808     char device_name[50];
3809     strcpy(device_name,"Direct3D HEL");
3810
3811     TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3812
3813     if (!callback)
3814         return DDERR_INVALIDPARAMS;
3815
3816     wined3d_mutex_lock();
3817
3818     hr = IDirect3DImpl_GetCaps(ddraw->wined3d, &device_desc1, &device_desc7);
3819     if (hr != D3D_OK)
3820     {
3821         wined3d_mutex_unlock();
3822         return hr;
3823     }
3824
3825     /* Do I have to enumerate the reference id? Note from old d3d7:
3826      * "It seems that enumerating the reference IID on Direct3D 1 games
3827      * (AvP / Motoracer2) breaks them". So do not enumerate this iid in V1
3828      *
3829      * There's a registry key HKLM\Software\Microsoft\Direct3D\Drivers,
3830      * EnumReference which enables / disables enumerating the reference
3831      * rasterizer. It's a DWORD, 0 means disabled, 2 means enabled. The
3832      * enablerefrast.reg and disablerefrast.reg files in the DirectX 7.0 sdk
3833      * demo directory suggest this.
3834      *
3835      * Some games(GTA 2) seem to use the second enumerated device, so I have
3836      * to enumerate at least 2 devices. So enumerate the reference device to
3837      * have 2 devices.
3838      *
3839      * Other games (Rollcage) tell emulation and hal device apart by certain
3840      * flags. Rollcage expects D3DPTEXTURECAPS_POW2 to be set (yeah, it is a
3841      * limitation flag), and it refuses all devices that have the perspective
3842      * flag set. This way it refuses the emulation device, and HAL devices
3843      * never have POW2 unset in d3d7 on windows. */
3844     if (ddraw->d3dversion != 1)
3845     {
3846         static CHAR reference_description[] = "RGB Direct3D emulation";
3847
3848         TRACE("Enumerating WineD3D D3DDevice interface.\n");
3849         hal_desc = device_desc1;
3850         hel_desc = device_desc1;
3851         /* The rgb device has the pow2 flag set in the hel caps, but not in the hal caps. */
3852         hal_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3853                 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3854         hal_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3855                 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3856         /* RGB, RAMP and MMX devices have a HAL dcmColorModel of 0 */
3857         hal_desc.dcmColorModel = 0;
3858
3859         hr = callback((GUID *)&IID_IDirect3DRGBDevice, reference_description,
3860                 device_name, &hal_desc, &hel_desc, context);
3861         if (hr != D3DENUMRET_OK)
3862         {
3863             TRACE("Application cancelled the enumeration.\n");
3864             wined3d_mutex_unlock();
3865             return D3D_OK;
3866         }
3867     }
3868
3869     strcpy(device_name,"Direct3D HAL");
3870
3871     TRACE("Enumerating HAL Direct3D device.\n");
3872     hal_desc = device_desc1;
3873     hel_desc = device_desc1;
3874
3875     /* The hal device does not have the pow2 flag set in hel, but in hal. */
3876     hel_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3877             | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3878     hel_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3879             | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3880     /* HAL devices have a HEL dcmColorModel of 0 */
3881     hel_desc.dcmColorModel = 0;
3882
3883     hr = callback((GUID *)&IID_IDirect3DHALDevice, wined3d_description,
3884             device_name, &hal_desc, &hel_desc, context);
3885     if (hr != D3DENUMRET_OK)
3886     {
3887         TRACE("Application cancelled the enumeration.\n");
3888         wined3d_mutex_unlock();
3889         return D3D_OK;
3890     }
3891
3892     TRACE("End of enumeration.\n");
3893
3894     wined3d_mutex_unlock();
3895
3896     return D3D_OK;
3897 }
3898
3899 static HRESULT WINAPI d3d2_EnumDevices(IDirect3D2 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3900 {
3901     struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3902
3903     TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3904
3905     return d3d3_EnumDevices(&ddraw->IDirect3D3_iface, callback, context);
3906 }
3907
3908 static HRESULT WINAPI d3d1_EnumDevices(IDirect3D *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3909 {
3910     struct ddraw *ddraw = impl_from_IDirect3D(iface);
3911
3912     TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3913
3914     return d3d3_EnumDevices(&ddraw->IDirect3D3_iface, callback, context);
3915 }
3916
3917 /*****************************************************************************
3918  * IDirect3D3::CreateLight
3919  *
3920  * Creates an IDirect3DLight interface. This interface is used in
3921  * Direct3D3 or earlier for lighting. In Direct3D7 it has been replaced
3922  * by the DIRECT3DLIGHT7 structure. Wine's Direct3DLight implementation
3923  * uses the IDirect3DDevice7 interface with D3D7 lights.
3924  *
3925  * Version 1, 2 and 3
3926  *
3927  * Params:
3928  *  light: Address to store the new interface pointer
3929  *  outer_unknown: Basically for aggregation, but ddraw doesn't support it.
3930  *                 Must be NULL
3931  *
3932  * Returns:
3933  *  D3D_OK on success
3934  *  DDERR_OUTOFMEMORY if memory allocation failed
3935  *  CLASS_E_NOAGGREGATION if outer_unknown != NULL
3936  *
3937  *****************************************************************************/
3938 static HRESULT WINAPI d3d3_CreateLight(IDirect3D3 *iface, IDirect3DLight **light,
3939         IUnknown *outer_unknown)
3940 {
3941     struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3942     struct d3d_light *object;
3943
3944     TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3945
3946     if (outer_unknown) return CLASS_E_NOAGGREGATION;
3947
3948     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3949     if (!object)
3950     {
3951         ERR("Failed to allocate light memory.\n");
3952         return DDERR_OUTOFMEMORY;
3953     }
3954
3955     d3d_light_init(object, ddraw);
3956
3957     TRACE("Created light %p.\n", object);
3958     *light = &object->IDirect3DLight_iface;
3959
3960     return D3D_OK;
3961 }
3962
3963 static HRESULT WINAPI d3d2_CreateLight(IDirect3D2 *iface, IDirect3DLight **light, IUnknown *outer_unknown)
3964 {
3965     struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3966
3967     TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3968
3969     return d3d3_CreateLight(&ddraw->IDirect3D3_iface, light, outer_unknown);
3970 }
3971
3972 static HRESULT WINAPI d3d1_CreateLight(IDirect3D *iface, IDirect3DLight **light, IUnknown *outer_unknown)
3973 {
3974     struct ddraw *ddraw = impl_from_IDirect3D(iface);
3975
3976     TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
3977
3978     return d3d3_CreateLight(&ddraw->IDirect3D3_iface, light, outer_unknown);
3979 }
3980
3981 /*****************************************************************************
3982  * IDirect3D3::CreateMaterial
3983  *
3984  * Creates an IDirect3DMaterial interface. This interface is used by Direct3D3
3985  * and older versions. The IDirect3DMaterial implementation wraps its
3986  * functionality to IDirect3DDevice7::SetMaterial and friends.
3987  *
3988  * Version 1, 2 and 3
3989  *
3990  * Params:
3991  *  material: Address to store the new interface's pointer to
3992  *  outer_unknown: Basically for aggregation, but ddraw doesn't support it.
3993  *                 Must be NULL
3994  *
3995  * Returns:
3996  *  D3D_OK on success
3997  *  DDERR_OUTOFMEMORY if memory allocation failed
3998  *  CLASS_E_NOAGGREGATION if outer_unknown != NULL
3999  *
4000  *****************************************************************************/
4001 static HRESULT WINAPI d3d3_CreateMaterial(IDirect3D3 *iface, IDirect3DMaterial3 **material,
4002         IUnknown *outer_unknown)
4003 {
4004     struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4005     struct d3d_material *object;
4006
4007     TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4008
4009     if (outer_unknown) return CLASS_E_NOAGGREGATION;
4010
4011     object = d3d_material_create(ddraw);
4012     if (!object)
4013     {
4014         ERR("Failed to allocate material memory.\n");
4015         return DDERR_OUTOFMEMORY;
4016     }
4017
4018     TRACE("Created material %p.\n", object);
4019     *material = &object->IDirect3DMaterial3_iface;
4020
4021     return D3D_OK;
4022 }
4023
4024 static HRESULT WINAPI d3d2_CreateMaterial(IDirect3D2 *iface, IDirect3DMaterial2 **material,
4025         IUnknown *outer_unknown)
4026 {
4027     struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4028     struct d3d_material *object;
4029
4030     TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4031
4032     object = d3d_material_create(ddraw);
4033     if (!object)
4034     {
4035         ERR("Failed to allocate material memory.\n");
4036         return DDERR_OUTOFMEMORY;
4037     }
4038
4039     TRACE("Created material %p.\n", object);
4040     *material = &object->IDirect3DMaterial2_iface;
4041
4042     return D3D_OK;
4043 }
4044
4045 static HRESULT WINAPI d3d1_CreateMaterial(IDirect3D *iface, IDirect3DMaterial **material,
4046         IUnknown *outer_unknown)
4047 {
4048     struct ddraw *ddraw = impl_from_IDirect3D(iface);
4049     struct d3d_material *object;
4050
4051     TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4052
4053     object = d3d_material_create(ddraw);
4054     if (!object)
4055     {
4056         ERR("Failed to allocate material memory.\n");
4057         return DDERR_OUTOFMEMORY;
4058     }
4059
4060     TRACE("Created material %p.\n", object);
4061     *material = &object->IDirect3DMaterial_iface;
4062
4063     return D3D_OK;
4064 }
4065
4066 /*****************************************************************************
4067  * IDirect3D3::CreateViewport
4068  *
4069  * Creates an IDirect3DViewport interface. This interface is used
4070  * by Direct3D and earlier versions for Viewport management. In Direct3D7
4071  * it has been replaced by a viewport structure and
4072  * IDirect3DDevice7::*Viewport. Wine's IDirect3DViewport implementation
4073  * uses the IDirect3DDevice7 methods for its functionality
4074  *
4075  * Params:
4076  *  Viewport: Address to store the new interface pointer
4077  *  outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4078  *                 Must be NULL
4079  *
4080  * Returns:
4081  *  D3D_OK on success
4082  *  DDERR_OUTOFMEMORY if memory allocation failed
4083  *  CLASS_E_NOAGGREGATION if outer_unknown != NULL
4084  *
4085  *****************************************************************************/
4086 static HRESULT WINAPI d3d3_CreateViewport(IDirect3D3 *iface, IDirect3DViewport3 **viewport,
4087         IUnknown *outer_unknown)
4088 {
4089     struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4090     struct d3d_viewport *object;
4091
4092     TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4093
4094     if (outer_unknown) return CLASS_E_NOAGGREGATION;
4095
4096     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4097     if (!object)
4098     {
4099         ERR("Failed to allocate viewport memory.\n");
4100         return DDERR_OUTOFMEMORY;
4101     }
4102
4103     d3d_viewport_init(object, ddraw);
4104
4105     TRACE("Created viewport %p.\n", object);
4106     *viewport = &object->IDirect3DViewport3_iface;
4107
4108     return D3D_OK;
4109 }
4110
4111 static HRESULT WINAPI d3d2_CreateViewport(IDirect3D2 *iface, IDirect3DViewport2 **viewport, IUnknown *outer_unknown)
4112 {
4113     struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4114
4115     TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4116
4117     return d3d3_CreateViewport(&ddraw->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4118             outer_unknown);
4119 }
4120
4121 static HRESULT WINAPI d3d1_CreateViewport(IDirect3D *iface, IDirect3DViewport **viewport, IUnknown *outer_unknown)
4122 {
4123     struct ddraw *ddraw = impl_from_IDirect3D(iface);
4124
4125     TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4126
4127     return d3d3_CreateViewport(&ddraw->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4128             outer_unknown);
4129 }
4130
4131 /*****************************************************************************
4132  * IDirect3D3::FindDevice
4133  *
4134  * This method finds a device with the requested properties and returns a
4135  * device description
4136  *
4137  * Verion 1, 2 and 3
4138  * Params:
4139  *  fds: Describes the requested device characteristics
4140  *  fdr: Returns the device description
4141  *
4142  * Returns:
4143  *  D3D_OK on success
4144  *  DDERR_INVALIDPARAMS if no device was found
4145  *
4146  *****************************************************************************/
4147 static HRESULT WINAPI d3d3_FindDevice(IDirect3D3 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4148 {
4149     struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4150     D3DDEVICEDESC7 desc7;
4151     D3DDEVICEDESC desc1;
4152     HRESULT hr;
4153
4154     TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4155
4156     if (!fds || !fdr) return DDERR_INVALIDPARAMS;
4157
4158     if (fds->dwSize != sizeof(D3DFINDDEVICESEARCH)
4159             || fdr->dwSize != sizeof(D3DFINDDEVICERESULT))
4160         return DDERR_INVALIDPARAMS;
4161
4162     if ((fds->dwFlags & D3DFDS_COLORMODEL)
4163             && fds->dcmColorModel != D3DCOLOR_RGB)
4164     {
4165         WARN("Trying to request a non-RGB D3D color model. Not supported.\n");
4166         return DDERR_INVALIDPARAMS; /* No real idea what to return here :-) */
4167     }
4168
4169     if (fds->dwFlags & D3DFDS_GUID)
4170     {
4171         TRACE("Trying to match guid %s.\n", debugstr_guid(&(fds->guid)));
4172         if (!IsEqualGUID(&IID_D3DDEVICE_WineD3D, &fds->guid)
4173                 && !IsEqualGUID(&IID_IDirect3DHALDevice, &fds->guid)
4174                 && !IsEqualGUID(&IID_IDirect3DRGBDevice, &fds->guid))
4175         {
4176             WARN("No match for this GUID.\n");
4177             return DDERR_NOTFOUND;
4178         }
4179     }
4180
4181     /* Get the caps */
4182     hr = IDirect3DImpl_GetCaps(ddraw->wined3d, &desc1, &desc7);
4183     if (hr != D3D_OK) return hr;
4184
4185     /* Now return our own GUID */
4186     fdr->guid = IID_D3DDEVICE_WineD3D;
4187     fdr->ddHwDesc = desc1;
4188     fdr->ddSwDesc = desc1;
4189
4190     TRACE("Returning Wine's wined3d device with (undumped) capabilities.\n");
4191
4192     return D3D_OK;
4193 }
4194
4195 static HRESULT WINAPI d3d2_FindDevice(IDirect3D2 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4196 {
4197     struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4198
4199     TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4200
4201     return d3d3_FindDevice(&ddraw->IDirect3D3_iface, fds, fdr);
4202 }
4203
4204 static HRESULT WINAPI d3d1_FindDevice(IDirect3D *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4205 {
4206     struct ddraw *ddraw = impl_from_IDirect3D(iface);
4207
4208     TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4209
4210     return d3d3_FindDevice(&ddraw->IDirect3D3_iface, fds, fdr);
4211 }
4212
4213 /*****************************************************************************
4214  * IDirect3D7::CreateDevice
4215  *
4216  * Creates an IDirect3DDevice7 interface.
4217  *
4218  * Version 2, 3 and 7. IDirect3DDevice 1 interfaces are interfaces to
4219  * DirectDraw surfaces and are created with
4220  * IDirectDrawSurface::QueryInterface. This method uses CreateDevice to
4221  * create the device object and QueryInterfaces for IDirect3DDevice
4222  *
4223  * Params:
4224  *  refiid: IID of the device to create
4225  *  Surface: Initial rendertarget
4226  *  Device: Address to return the interface pointer
4227  *
4228  * Returns:
4229  *  D3D_OK on success
4230  *  DDERR_OUTOFMEMORY if memory allocation failed
4231  *  DDERR_INVALIDPARAMS if a device exists already
4232  *
4233  *****************************************************************************/
4234 static HRESULT WINAPI d3d7_CreateDevice(IDirect3D7 *iface, REFCLSID riid,
4235         IDirectDrawSurface7 *surface, IDirect3DDevice7 **device)
4236 {
4237     struct ddraw_surface *target = unsafe_impl_from_IDirectDrawSurface7(surface);
4238     struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4239     struct d3d_device *object;
4240     HRESULT hr;
4241
4242     TRACE("iface %p, riid %s, surface %p, device %p.\n", iface, debugstr_guid(riid), surface, device);
4243
4244     wined3d_mutex_lock();
4245     hr = d3d_device_create(ddraw, target, 7, &object, NULL);
4246     if (SUCCEEDED(hr))
4247         *device = &object->IDirect3DDevice7_iface;
4248     else
4249     {
4250         WARN("Failed to create device, hr %#x.\n", hr);
4251         *device = NULL;
4252     }
4253     wined3d_mutex_unlock();
4254
4255     return hr;
4256 }
4257
4258 static HRESULT WINAPI d3d3_CreateDevice(IDirect3D3 *iface, REFCLSID riid,
4259         IDirectDrawSurface4 *surface, IDirect3DDevice3 **device, IUnknown *outer_unknown)
4260 {
4261     struct ddraw_surface *surface_impl = unsafe_impl_from_IDirectDrawSurface4(surface);
4262     struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4263     struct d3d_device *device_impl;
4264     HRESULT hr;
4265
4266     TRACE("iface %p, riid %s, surface %p, device %p, outer_unknown %p.\n",
4267             iface, debugstr_guid(riid), surface, device, outer_unknown);
4268
4269     if (outer_unknown)
4270         return CLASS_E_NOAGGREGATION;
4271
4272     wined3d_mutex_lock();
4273     hr = d3d_device_create(ddraw, surface_impl, 3, &device_impl, NULL);
4274     if (SUCCEEDED(hr))
4275         *device = &device_impl->IDirect3DDevice3_iface;
4276     else
4277     {
4278         WARN("Failed to create device, hr %#x.\n", hr);
4279         *device = NULL;
4280     }
4281     wined3d_mutex_unlock();
4282
4283     return hr;
4284 }
4285
4286 static HRESULT WINAPI d3d2_CreateDevice(IDirect3D2 *iface, REFCLSID riid,
4287         IDirectDrawSurface *surface, IDirect3DDevice2 **device)
4288 {
4289     struct ddraw_surface *surface_impl = unsafe_impl_from_IDirectDrawSurface(surface);
4290     struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4291     struct d3d_device *device_impl;
4292     HRESULT hr;
4293
4294     TRACE("iface %p, riid %s, surface %p, device %p.\n",
4295             iface, debugstr_guid(riid), surface, device);
4296
4297     wined3d_mutex_lock();
4298     hr = d3d_device_create(ddraw, surface_impl, 2, &device_impl, NULL);
4299     if (SUCCEEDED(hr))
4300         *device = &device_impl->IDirect3DDevice2_iface;
4301     else
4302     {
4303         WARN("Failed to create device, hr %#x.\n", hr);
4304         *device = NULL;
4305     }
4306     wined3d_mutex_unlock();
4307
4308     return hr;
4309 }
4310
4311 /*****************************************************************************
4312  * IDirect3D7::CreateVertexBuffer
4313  *
4314  * Creates a new vertex buffer object and returns a IDirect3DVertexBuffer7
4315  * interface.
4316  *
4317  * Version 3 and 7
4318  *
4319  * Params:
4320  *  desc: Requested Vertex buffer properties
4321  *  vertex_buffer: Address to return the interface pointer at
4322  *  flags: Some flags, should be 0
4323  *
4324  * Returns
4325  *  D3D_OK on success
4326  *  DDERR_OUTOFMEMORY if memory allocation failed
4327  *  The return value of IWineD3DDevice::CreateVertexBuffer if this call fails
4328  *  DDERR_INVALIDPARAMS if desc or vertex_buffer are NULL
4329  *
4330  *****************************************************************************/
4331 static HRESULT WINAPI d3d7_CreateVertexBuffer(IDirect3D7 *iface, D3DVERTEXBUFFERDESC *desc,
4332         IDirect3DVertexBuffer7 **vertex_buffer, DWORD flags)
4333 {
4334     struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4335     struct d3d_vertex_buffer *object;
4336     HRESULT hr;
4337
4338     TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x.\n",
4339             iface, desc, vertex_buffer, flags);
4340
4341     if (!vertex_buffer || !desc) return DDERR_INVALIDPARAMS;
4342
4343     hr = d3d_vertex_buffer_create(&object, ddraw, desc);
4344     if (hr == D3D_OK)
4345     {
4346         TRACE("Created vertex buffer %p.\n", object);
4347         *vertex_buffer = &object->IDirect3DVertexBuffer7_iface;
4348     }
4349     else
4350         WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4351
4352     return hr;
4353 }
4354
4355 static HRESULT WINAPI d3d3_CreateVertexBuffer(IDirect3D3 *iface, D3DVERTEXBUFFERDESC *desc,
4356         IDirect3DVertexBuffer **vertex_buffer, DWORD flags, IUnknown *outer_unknown)
4357 {
4358     struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4359     struct d3d_vertex_buffer *object;
4360     HRESULT hr;
4361
4362     TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x, outer_unknown %p.\n",
4363             iface, desc, vertex_buffer, flags, outer_unknown);
4364
4365     if (outer_unknown)
4366         return CLASS_E_NOAGGREGATION;
4367     if (!vertex_buffer || !desc)
4368         return DDERR_INVALIDPARAMS;
4369
4370     hr = d3d_vertex_buffer_create(&object, ddraw, desc);
4371     if (hr == D3D_OK)
4372     {
4373         TRACE("Created vertex buffer %p.\n", object);
4374         *vertex_buffer = &object->IDirect3DVertexBuffer_iface;
4375     }
4376     else
4377         WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4378
4379     return hr;
4380 }
4381
4382 /*****************************************************************************
4383  * IDirect3D7::EnumZBufferFormats
4384  *
4385  * Enumerates all supported Z buffer pixel formats
4386  *
4387  * Version 3 and 7
4388  *
4389  * Params:
4390  *  device_iid:
4391  *  callback: callback to call for each pixel format
4392  *  context: Pointer to pass back to the callback
4393  *
4394  * Returns:
4395  *  D3D_OK on success
4396  *  DDERR_INVALIDPARAMS if callback is NULL
4397  *  For details, see IWineD3DDevice::EnumZBufferFormats
4398  *
4399  *****************************************************************************/
4400 static HRESULT WINAPI d3d7_EnumZBufferFormats(IDirect3D7 *iface, REFCLSID device_iid,
4401         LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4402 {
4403     struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4404     struct wined3d_display_mode mode;
4405     enum wined3d_device_type type;
4406     unsigned int i;
4407     HRESULT hr;
4408
4409     /* Order matters. Specifically, BattleZone II (full version) expects the
4410      * 16-bit depth formats to be listed before the 24 and 32 ones. */
4411     static const enum wined3d_format_id formats[] =
4412     {
4413         WINED3DFMT_S1_UINT_D15_UNORM,
4414         WINED3DFMT_D16_UNORM,
4415         WINED3DFMT_X8D24_UNORM,
4416         WINED3DFMT_S4X4_UINT_D24_UNORM,
4417         WINED3DFMT_D24_UNORM_S8_UINT,
4418         WINED3DFMT_D32_UNORM,
4419     };
4420
4421     TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4422             iface, debugstr_guid(device_iid), callback, context);
4423
4424     if (!callback) return DDERR_INVALIDPARAMS;
4425
4426     if (IsEqualGUID(device_iid, &IID_IDirect3DHALDevice)
4427             || IsEqualGUID(device_iid, &IID_IDirect3DTnLHalDevice)
4428             || IsEqualGUID(device_iid, &IID_D3DDEVICE_WineD3D))
4429     {
4430         TRACE("Asked for HAL device.\n");
4431         type = WINED3D_DEVICE_TYPE_HAL;
4432     }
4433     else if (IsEqualGUID(device_iid, &IID_IDirect3DRGBDevice)
4434             || IsEqualGUID(device_iid, &IID_IDirect3DMMXDevice))
4435     {
4436         TRACE("Asked for SW device.\n");
4437         type = WINED3D_DEVICE_TYPE_SW;
4438     }
4439     else if (IsEqualGUID(device_iid, &IID_IDirect3DRefDevice))
4440     {
4441         TRACE("Asked for REF device.\n");
4442         type = WINED3D_DEVICE_TYPE_REF;
4443     }
4444     else if (IsEqualGUID(device_iid, &IID_IDirect3DNullDevice))
4445     {
4446         TRACE("Asked for NULLREF device.\n");
4447         type = WINED3D_DEVICE_TYPE_NULLREF;
4448     }
4449     else
4450     {
4451         FIXME("Unexpected device GUID %s.\n", debugstr_guid(device_iid));
4452         type = WINED3D_DEVICE_TYPE_HAL;
4453     }
4454
4455     wined3d_mutex_lock();
4456     /* We need an adapter format from somewhere to please wined3d and WGL.
4457      * Use the current display mode. So far all cards offer the same depth
4458      * stencil format for all modes, but if some do not and applications do
4459      * not like that we'll have to find some workaround, like iterating over
4460      * all imaginable formats and collecting all the depth stencil formats we
4461      * can get. */
4462     if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
4463     {
4464         ERR("Failed to get display mode, hr %#x.\n", hr);
4465         wined3d_mutex_unlock();
4466         return hr;
4467     }
4468
4469     for (i = 0; i < (sizeof(formats) / sizeof(*formats)); ++i)
4470     {
4471         hr = wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, type, mode.format_id,
4472                 WINED3DUSAGE_DEPTHSTENCIL, WINED3D_RTYPE_SURFACE, formats[i], WINED3D_SURFACE_TYPE_OPENGL);
4473         if (SUCCEEDED(hr))
4474         {
4475             DDPIXELFORMAT pformat;
4476
4477             memset(&pformat, 0, sizeof(pformat));
4478             pformat.dwSize = sizeof(pformat);
4479             PixelFormat_WineD3DtoDD(&pformat, formats[i]);
4480
4481             TRACE("Enumerating wined3d format %#x.\n", formats[i]);
4482             hr = callback(&pformat, context);
4483             if (hr != DDENUMRET_OK)
4484             {
4485                 TRACE("Format enumeration cancelled by application.\n");
4486                 wined3d_mutex_unlock();
4487                 return D3D_OK;
4488             }
4489         }
4490     }
4491
4492     /* Historically some windows drivers used dwZBufferBitDepth=24 for WINED3DFMT_X8D24_UNORM,
4493      * while others used dwZBufferBitDepth=32. In either case the pitch matches a 32 bits per
4494      * pixel format, so we use dwZBufferBitDepth=32. Some games expect 24. Windows Vista and
4495      * newer enumerate both versions, so we do the same(bug 22434) */
4496     hr = wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, type, mode.format_id,
4497             WINED3DUSAGE_DEPTHSTENCIL, WINED3D_RTYPE_SURFACE, WINED3DFMT_X8D24_UNORM, WINED3D_SURFACE_TYPE_OPENGL);
4498     if (SUCCEEDED(hr))
4499     {
4500         DDPIXELFORMAT x8d24 =
4501         {
4502             sizeof(x8d24), DDPF_ZBUFFER, 0,
4503             {24}, {0x00000000}, {0x00ffffff}, {0x00000000}
4504         };
4505         TRACE("Enumerating WINED3DFMT_X8D24_UNORM, dwZBufferBitDepth=24 version\n");
4506         callback(&x8d24, context);
4507     }
4508
4509     TRACE("End of enumeration.\n");
4510
4511     wined3d_mutex_unlock();
4512
4513     return D3D_OK;
4514 }
4515
4516 static HRESULT WINAPI d3d3_EnumZBufferFormats(IDirect3D3 *iface, REFCLSID device_iid,
4517         LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4518 {
4519     struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4520
4521     TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4522             iface, debugstr_guid(device_iid), callback, context);
4523
4524     return d3d7_EnumZBufferFormats(&ddraw->IDirect3D7_iface, device_iid, callback, context);
4525 }
4526
4527 /*****************************************************************************
4528  * IDirect3D7::EvictManagedTextures
4529  *
4530  * Removes all managed textures (=surfaces with DDSCAPS2_TEXTUREMANAGE or
4531  * DDSCAPS2_D3DTEXTUREMANAGE caps) to be removed from video memory.
4532  *
4533  * Version 3 and 7
4534  *
4535  * Returns:
4536  *  D3D_OK, because it's a stub
4537  *
4538  *****************************************************************************/
4539 static HRESULT WINAPI d3d7_EvictManagedTextures(IDirect3D7 *iface)
4540 {
4541     struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4542
4543     TRACE("iface %p!\n", iface);
4544
4545     wined3d_mutex_lock();
4546     if (ddraw->d3d_initialized)
4547         wined3d_device_evict_managed_resources(ddraw->wined3d_device);
4548     wined3d_mutex_unlock();
4549
4550     return D3D_OK;
4551 }
4552
4553 static HRESULT WINAPI d3d3_EvictManagedTextures(IDirect3D3 *iface)
4554 {
4555     struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4556
4557     TRACE("iface %p.\n", iface);
4558
4559     return d3d7_EvictManagedTextures(&ddraw->IDirect3D7_iface);
4560 }
4561
4562 /*****************************************************************************
4563  * IDirect3DImpl_GetCaps
4564  *
4565  * This function retrieves the device caps from wined3d
4566  * and converts it into a D3D7 and D3D - D3D3 structure
4567  * This is a helper function called from various places in ddraw
4568  *
4569  * Params:
4570  *  wined3d: The interface to get the caps from
4571  *  desc1: Old D3D <3 structure to fill (needed)
4572  *  desc7: D3D7 device desc structure to fill (needed)
4573  *
4574  * Returns
4575  *  D3D_OK on success, or the return value of IWineD3D::GetCaps
4576  *
4577  *****************************************************************************/
4578 HRESULT IDirect3DImpl_GetCaps(const struct wined3d *wined3d, D3DDEVICEDESC *desc1, D3DDEVICEDESC7 *desc7)
4579 {
4580     WINED3DCAPS wined3d_caps;
4581     HRESULT hr;
4582
4583     TRACE("wined3d %p, desc1 %p, desc7 %p.\n", wined3d, desc1, desc7);
4584
4585     memset(&wined3d_caps, 0, sizeof(wined3d_caps));
4586
4587     wined3d_mutex_lock();
4588     hr = wined3d_get_device_caps(wined3d, 0, WINED3D_DEVICE_TYPE_HAL, &wined3d_caps);
4589     wined3d_mutex_unlock();
4590     if (FAILED(hr))
4591     {
4592         WARN("Failed to get device caps, hr %#x.\n", hr);
4593         return hr;
4594     }
4595
4596     /* Copy the results into the d3d7 and d3d3 structures */
4597     desc7->dwDevCaps = wined3d_caps.DevCaps;
4598     desc7->dpcLineCaps.dwMiscCaps = wined3d_caps.PrimitiveMiscCaps;
4599     desc7->dpcLineCaps.dwRasterCaps = wined3d_caps.RasterCaps;
4600     desc7->dpcLineCaps.dwZCmpCaps = wined3d_caps.ZCmpCaps;
4601     desc7->dpcLineCaps.dwSrcBlendCaps = wined3d_caps.SrcBlendCaps;
4602     desc7->dpcLineCaps.dwDestBlendCaps = wined3d_caps.DestBlendCaps;
4603     desc7->dpcLineCaps.dwAlphaCmpCaps = wined3d_caps.AlphaCmpCaps;
4604     desc7->dpcLineCaps.dwShadeCaps = wined3d_caps.ShadeCaps;
4605     desc7->dpcLineCaps.dwTextureCaps = wined3d_caps.TextureCaps;
4606     desc7->dpcLineCaps.dwTextureFilterCaps = wined3d_caps.TextureFilterCaps;
4607     desc7->dpcLineCaps.dwTextureAddressCaps = wined3d_caps.TextureAddressCaps;
4608
4609     desc7->dwMaxTextureWidth = wined3d_caps.MaxTextureWidth;
4610     desc7->dwMaxTextureHeight = wined3d_caps.MaxTextureHeight;
4611
4612     desc7->dwMaxTextureRepeat = wined3d_caps.MaxTextureRepeat;
4613     desc7->dwMaxTextureAspectRatio = wined3d_caps.MaxTextureAspectRatio;
4614     desc7->dwMaxAnisotropy = wined3d_caps.MaxAnisotropy;
4615     desc7->dvMaxVertexW = wined3d_caps.MaxVertexW;
4616
4617     desc7->dvGuardBandLeft = wined3d_caps.GuardBandLeft;
4618     desc7->dvGuardBandTop = wined3d_caps.GuardBandTop;
4619     desc7->dvGuardBandRight = wined3d_caps.GuardBandRight;
4620     desc7->dvGuardBandBottom = wined3d_caps.GuardBandBottom;
4621
4622     desc7->dvExtentsAdjust = wined3d_caps.ExtentsAdjust;
4623     desc7->dwStencilCaps = wined3d_caps.StencilCaps;
4624
4625     desc7->dwFVFCaps = wined3d_caps.FVFCaps;
4626     desc7->dwTextureOpCaps = wined3d_caps.TextureOpCaps;
4627
4628     desc7->dwVertexProcessingCaps = wined3d_caps.VertexProcessingCaps;
4629     desc7->dwMaxActiveLights = wined3d_caps.MaxActiveLights;
4630
4631     /* Remove all non-d3d7 caps */
4632     desc7->dwDevCaps &= (
4633         D3DDEVCAPS_FLOATTLVERTEX         | D3DDEVCAPS_SORTINCREASINGZ          | D3DDEVCAPS_SORTDECREASINGZ          |
4634         D3DDEVCAPS_SORTEXACT             | D3DDEVCAPS_EXECUTESYSTEMMEMORY      | D3DDEVCAPS_EXECUTEVIDEOMEMORY       |
4635         D3DDEVCAPS_TLVERTEXSYSTEMMEMORY  | D3DDEVCAPS_TLVERTEXVIDEOMEMORY      | D3DDEVCAPS_TEXTURESYSTEMMEMORY      |
4636         D3DDEVCAPS_TEXTUREVIDEOMEMORY    | D3DDEVCAPS_DRAWPRIMTLVERTEX         | D3DDEVCAPS_CANRENDERAFTERFLIP       |
4637         D3DDEVCAPS_TEXTURENONLOCALVIDMEM | D3DDEVCAPS_DRAWPRIMITIVES2          | D3DDEVCAPS_SEPARATETEXTUREMEMORIES  |
4638         D3DDEVCAPS_DRAWPRIMITIVES2EX     | D3DDEVCAPS_HWTRANSFORMANDLIGHT      | D3DDEVCAPS_CANBLTSYSTONONLOCAL      |
4639         D3DDEVCAPS_HWRASTERIZATION);
4640
4641     desc7->dwStencilCaps &= (
4642         D3DSTENCILCAPS_KEEP              | D3DSTENCILCAPS_ZERO                 | D3DSTENCILCAPS_REPLACE              |
4643         D3DSTENCILCAPS_INCRSAT           | D3DSTENCILCAPS_DECRSAT              | D3DSTENCILCAPS_INVERT               |
4644         D3DSTENCILCAPS_INCR              | D3DSTENCILCAPS_DECR);
4645
4646     /* FVF caps ?*/
4647
4648     desc7->dwTextureOpCaps &= (
4649         D3DTEXOPCAPS_DISABLE             | D3DTEXOPCAPS_SELECTARG1             | D3DTEXOPCAPS_SELECTARG2             |
4650         D3DTEXOPCAPS_MODULATE            | D3DTEXOPCAPS_MODULATE2X             | D3DTEXOPCAPS_MODULATE4X             |
4651         D3DTEXOPCAPS_ADD                 | D3DTEXOPCAPS_ADDSIGNED              | D3DTEXOPCAPS_ADDSIGNED2X            |
4652         D3DTEXOPCAPS_SUBTRACT            | D3DTEXOPCAPS_ADDSMOOTH              | D3DTEXOPCAPS_BLENDTEXTUREALPHA      |
4653         D3DTEXOPCAPS_BLENDFACTORALPHA    | D3DTEXOPCAPS_BLENDTEXTUREALPHAPM    | D3DTEXOPCAPS_BLENDCURRENTALPHA      |
4654         D3DTEXOPCAPS_PREMODULATE         | D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
4655         D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA | D3DTEXOPCAPS_BUMPENVMAP    |
4656         D3DTEXOPCAPS_BUMPENVMAPLUMINANCE | D3DTEXOPCAPS_DOTPRODUCT3);
4657
4658     desc7->dwVertexProcessingCaps &= (
4659         D3DVTXPCAPS_TEXGEN               | D3DVTXPCAPS_MATERIALSOURCE7         | D3DVTXPCAPS_VERTEXFOG               |
4660         D3DVTXPCAPS_DIRECTIONALLIGHTS    | D3DVTXPCAPS_POSITIONALLIGHTS        | D3DVTXPCAPS_LOCALVIEWER);
4661
4662     desc7->dpcLineCaps.dwMiscCaps &= (
4663         D3DPMISCCAPS_MASKPLANES          | D3DPMISCCAPS_MASKZ                  | D3DPMISCCAPS_LINEPATTERNREP         |
4664         D3DPMISCCAPS_CONFORMANT          | D3DPMISCCAPS_CULLNONE               | D3DPMISCCAPS_CULLCW                 |
4665         D3DPMISCCAPS_CULLCCW);
4666
4667     desc7->dpcLineCaps.dwRasterCaps &= (
4668         D3DPRASTERCAPS_DITHER            | D3DPRASTERCAPS_ROP2                 | D3DPRASTERCAPS_XOR                  |
4669         D3DPRASTERCAPS_PAT               | D3DPRASTERCAPS_ZTEST                | D3DPRASTERCAPS_SUBPIXEL             |
4670         D3DPRASTERCAPS_SUBPIXELX         | D3DPRASTERCAPS_FOGVERTEX            | D3DPRASTERCAPS_FOGTABLE             |
4671         D3DPRASTERCAPS_STIPPLE           | D3DPRASTERCAPS_ANTIALIASSORTDEPENDENT | D3DPRASTERCAPS_ANTIALIASSORTINDEPENDENT |
4672         D3DPRASTERCAPS_ANTIALIASEDGES    | D3DPRASTERCAPS_MIPMAPLODBIAS        | D3DPRASTERCAPS_ZBIAS                |
4673         D3DPRASTERCAPS_ZBUFFERLESSHSR    | D3DPRASTERCAPS_FOGRANGE             | D3DPRASTERCAPS_ANISOTROPY           |
4674         D3DPRASTERCAPS_WBUFFER           | D3DPRASTERCAPS_TRANSLUCENTSORTINDEPENDENT | D3DPRASTERCAPS_WFOG           |
4675         D3DPRASTERCAPS_ZFOG);
4676
4677     desc7->dpcLineCaps.dwZCmpCaps &= (
4678         D3DPCMPCAPS_NEVER                | D3DPCMPCAPS_LESS                    | D3DPCMPCAPS_EQUAL                   |
4679         D3DPCMPCAPS_LESSEQUAL            | D3DPCMPCAPS_GREATER                 | D3DPCMPCAPS_NOTEQUAL                |
4680         D3DPCMPCAPS_GREATEREQUAL         | D3DPCMPCAPS_ALWAYS);
4681
4682     desc7->dpcLineCaps.dwSrcBlendCaps &= (
4683         D3DPBLENDCAPS_ZERO               | D3DPBLENDCAPS_ONE                   | D3DPBLENDCAPS_SRCCOLOR              |
4684         D3DPBLENDCAPS_INVSRCCOLOR        | D3DPBLENDCAPS_SRCALPHA              | D3DPBLENDCAPS_INVSRCALPHA           |
4685         D3DPBLENDCAPS_DESTALPHA          | D3DPBLENDCAPS_INVDESTALPHA          | D3DPBLENDCAPS_DESTCOLOR             |
4686         D3DPBLENDCAPS_INVDESTCOLOR       | D3DPBLENDCAPS_SRCALPHASAT           | D3DPBLENDCAPS_BOTHSRCALPHA          |
4687         D3DPBLENDCAPS_BOTHINVSRCALPHA);
4688
4689     desc7->dpcLineCaps.dwDestBlendCaps &= (
4690         D3DPBLENDCAPS_ZERO               | D3DPBLENDCAPS_ONE                   | D3DPBLENDCAPS_SRCCOLOR              |
4691         D3DPBLENDCAPS_INVSRCCOLOR        | D3DPBLENDCAPS_SRCALPHA              | D3DPBLENDCAPS_INVSRCALPHA           |
4692         D3DPBLENDCAPS_DESTALPHA          | D3DPBLENDCAPS_INVDESTALPHA          | D3DPBLENDCAPS_DESTCOLOR             |
4693         D3DPBLENDCAPS_INVDESTCOLOR       | D3DPBLENDCAPS_SRCALPHASAT           | D3DPBLENDCAPS_BOTHSRCALPHA          |
4694         D3DPBLENDCAPS_BOTHINVSRCALPHA);
4695
4696     desc7->dpcLineCaps.dwAlphaCmpCaps &= (
4697         D3DPCMPCAPS_NEVER                | D3DPCMPCAPS_LESS                    | D3DPCMPCAPS_EQUAL                   |
4698         D3DPCMPCAPS_LESSEQUAL            | D3DPCMPCAPS_GREATER                 | D3DPCMPCAPS_NOTEQUAL                |
4699         D3DPCMPCAPS_GREATEREQUAL         | D3DPCMPCAPS_ALWAYS);
4700
4701     desc7->dpcLineCaps.dwShadeCaps &= (
4702         D3DPSHADECAPS_COLORFLATMONO      | D3DPSHADECAPS_COLORFLATRGB          | D3DPSHADECAPS_COLORGOURAUDMONO      |
4703         D3DPSHADECAPS_COLORGOURAUDRGB    | D3DPSHADECAPS_COLORPHONGMONO        | D3DPSHADECAPS_COLORPHONGRGB         |
4704         D3DPSHADECAPS_SPECULARFLATMONO   | D3DPSHADECAPS_SPECULARFLATRGB       | D3DPSHADECAPS_SPECULARGOURAUDMONO   |
4705         D3DPSHADECAPS_SPECULARGOURAUDRGB | D3DPSHADECAPS_SPECULARPHONGMONO     | D3DPSHADECAPS_SPECULARPHONGRGB      |
4706         D3DPSHADECAPS_ALPHAFLATBLEND     | D3DPSHADECAPS_ALPHAFLATSTIPPLED     | D3DPSHADECAPS_ALPHAGOURAUDBLEND     |
4707         D3DPSHADECAPS_ALPHAGOURAUDSTIPPLED | D3DPSHADECAPS_ALPHAPHONGBLEND     | D3DPSHADECAPS_ALPHAPHONGSTIPPLED    |
4708         D3DPSHADECAPS_FOGFLAT            | D3DPSHADECAPS_FOGGOURAUD            | D3DPSHADECAPS_FOGPHONG);
4709
4710     desc7->dpcLineCaps.dwTextureCaps &= (
4711         D3DPTEXTURECAPS_PERSPECTIVE      | D3DPTEXTURECAPS_POW2                | D3DPTEXTURECAPS_ALPHA               |
4712         D3DPTEXTURECAPS_TRANSPARENCY     | D3DPTEXTURECAPS_BORDER              | D3DPTEXTURECAPS_SQUAREONLY          |
4713         D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE | D3DPTEXTURECAPS_ALPHAPALETTE| D3DPTEXTURECAPS_NONPOW2CONDITIONAL  |
4714         D3DPTEXTURECAPS_PROJECTED        | D3DPTEXTURECAPS_CUBEMAP             | D3DPTEXTURECAPS_COLORKEYBLEND);
4715
4716     desc7->dpcLineCaps.dwTextureFilterCaps &= (
4717         D3DPTFILTERCAPS_NEAREST          | D3DPTFILTERCAPS_LINEAR              | D3DPTFILTERCAPS_MIPNEAREST          |
4718         D3DPTFILTERCAPS_MIPLINEAR        | D3DPTFILTERCAPS_LINEARMIPNEAREST    | D3DPTFILTERCAPS_LINEARMIPLINEAR     |
4719         D3DPTFILTERCAPS_MINFPOINT        | D3DPTFILTERCAPS_MINFLINEAR          | D3DPTFILTERCAPS_MINFANISOTROPIC     |
4720         D3DPTFILTERCAPS_MIPFPOINT        | D3DPTFILTERCAPS_MIPFLINEAR          | D3DPTFILTERCAPS_MAGFPOINT           |
4721         D3DPTFILTERCAPS_MAGFLINEAR       | D3DPTFILTERCAPS_MAGFANISOTROPIC     | D3DPTFILTERCAPS_MAGFAFLATCUBIC      |
4722         D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC);
4723
4724     desc7->dpcLineCaps.dwTextureBlendCaps &= (
4725         D3DPTBLENDCAPS_DECAL             | D3DPTBLENDCAPS_MODULATE             | D3DPTBLENDCAPS_DECALALPHA           |
4726         D3DPTBLENDCAPS_MODULATEALPHA     | D3DPTBLENDCAPS_DECALMASK            | D3DPTBLENDCAPS_MODULATEMASK         |
4727         D3DPTBLENDCAPS_COPY              | D3DPTBLENDCAPS_ADD);
4728
4729     desc7->dpcLineCaps.dwTextureAddressCaps &= (
4730         D3DPTADDRESSCAPS_WRAP            | D3DPTADDRESSCAPS_MIRROR             | D3DPTADDRESSCAPS_CLAMP              |
4731         D3DPTADDRESSCAPS_BORDER          | D3DPTADDRESSCAPS_INDEPENDENTUV);
4732
4733     if (!(desc7->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2))
4734     {
4735         /* DirectX7 always has the np2 flag set, no matter what the card
4736          * supports. Some old games (Rollcage) check the caps incorrectly.
4737          * If wined3d supports nonpow2 textures it also has np2 conditional
4738          * support. */
4739         desc7->dpcLineCaps.dwTextureCaps |= D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL;
4740     }
4741
4742     /* Fill the missing members, and do some fixup */
4743     desc7->dpcLineCaps.dwSize = sizeof(desc7->dpcLineCaps);
4744     desc7->dpcLineCaps.dwTextureBlendCaps = D3DPTBLENDCAPS_ADD | D3DPTBLENDCAPS_MODULATEMASK |
4745                                             D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_DECAL |
4746                                             D3DPTBLENDCAPS_DECALALPHA | D3DPTBLENDCAPS_DECALMASK |
4747                                             D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_MODULATEALPHA;
4748     desc7->dpcLineCaps.dwStippleWidth = 32;
4749     desc7->dpcLineCaps.dwStippleHeight = 32;
4750     /* Use the same for the TriCaps */
4751     desc7->dpcTriCaps = desc7->dpcLineCaps;
4752
4753     desc7->dwDeviceRenderBitDepth = DDBD_16 | DDBD_24 | DDBD_32;
4754     desc7->dwDeviceZBufferBitDepth = DDBD_16 | DDBD_24;
4755     desc7->dwMinTextureWidth = 1;
4756     desc7->dwMinTextureHeight = 1;
4757
4758     /* Convert DWORDs safely to WORDs */
4759     if (wined3d_caps.MaxTextureBlendStages > 0xffff) desc7->wMaxTextureBlendStages = 0xffff;
4760     else desc7->wMaxTextureBlendStages = (WORD)wined3d_caps.MaxTextureBlendStages;
4761     if (wined3d_caps.MaxSimultaneousTextures > 0xffff) desc7->wMaxSimultaneousTextures = 0xffff;
4762     else desc7->wMaxSimultaneousTextures = (WORD)wined3d_caps.MaxSimultaneousTextures;
4763
4764     if (wined3d_caps.MaxUserClipPlanes > 0xffff) desc7->wMaxUserClipPlanes = 0xffff;
4765     else desc7->wMaxUserClipPlanes = (WORD)wined3d_caps.MaxUserClipPlanes;
4766     if (wined3d_caps.MaxVertexBlendMatrices > 0xffff) desc7->wMaxVertexBlendMatrices = 0xffff;
4767     else desc7->wMaxVertexBlendMatrices = (WORD)wined3d_caps.MaxVertexBlendMatrices;
4768
4769     desc7->deviceGUID = IID_IDirect3DTnLHalDevice;
4770
4771     desc7->dwReserved1 = 0;
4772     desc7->dwReserved2 = 0;
4773     desc7->dwReserved3 = 0;
4774     desc7->dwReserved4 = 0;
4775
4776     /* Fill the old structure */
4777     memset(desc1, 0, sizeof(*desc1));
4778     desc1->dwSize = sizeof(D3DDEVICEDESC);
4779     desc1->dwFlags = D3DDD_COLORMODEL
4780             | D3DDD_DEVCAPS
4781             | D3DDD_TRANSFORMCAPS
4782             | D3DDD_BCLIPPING
4783             | D3DDD_LIGHTINGCAPS
4784             | D3DDD_LINECAPS
4785             | D3DDD_TRICAPS
4786             | D3DDD_DEVICERENDERBITDEPTH
4787             | D3DDD_DEVICEZBUFFERBITDEPTH
4788             | D3DDD_MAXBUFFERSIZE
4789             | D3DDD_MAXVERTEXCOUNT;
4790
4791     desc1->dcmColorModel = D3DCOLOR_RGB;
4792     desc1->dwDevCaps = desc7->dwDevCaps;
4793     desc1->dtcTransformCaps.dwSize = sizeof(D3DTRANSFORMCAPS);
4794     desc1->dtcTransformCaps.dwCaps = D3DTRANSFORMCAPS_CLIP;
4795     desc1->bClipping = TRUE;
4796     desc1->dlcLightingCaps.dwSize = sizeof(D3DLIGHTINGCAPS);
4797     desc1->dlcLightingCaps.dwCaps = D3DLIGHTCAPS_DIRECTIONAL
4798             | D3DLIGHTCAPS_PARALLELPOINT
4799             | D3DLIGHTCAPS_POINT
4800             | D3DLIGHTCAPS_SPOT;
4801
4802     desc1->dlcLightingCaps.dwLightingModel = D3DLIGHTINGMODEL_RGB;
4803     desc1->dlcLightingCaps.dwNumLights = desc7->dwMaxActiveLights;
4804
4805     desc1->dpcLineCaps.dwSize = sizeof(D3DPRIMCAPS);
4806     desc1->dpcLineCaps.dwMiscCaps = desc7->dpcLineCaps.dwMiscCaps;
4807     desc1->dpcLineCaps.dwRasterCaps = desc7->dpcLineCaps.dwRasterCaps;
4808     desc1->dpcLineCaps.dwZCmpCaps = desc7->dpcLineCaps.dwZCmpCaps;
4809     desc1->dpcLineCaps.dwSrcBlendCaps = desc7->dpcLineCaps.dwSrcBlendCaps;
4810     desc1->dpcLineCaps.dwDestBlendCaps = desc7->dpcLineCaps.dwDestBlendCaps;
4811     desc1->dpcLineCaps.dwShadeCaps = desc7->dpcLineCaps.dwShadeCaps;
4812     desc1->dpcLineCaps.dwTextureCaps = desc7->dpcLineCaps.dwTextureCaps;
4813     desc1->dpcLineCaps.dwTextureFilterCaps = desc7->dpcLineCaps.dwTextureFilterCaps;
4814     desc1->dpcLineCaps.dwTextureBlendCaps = desc7->dpcLineCaps.dwTextureBlendCaps;
4815     desc1->dpcLineCaps.dwTextureAddressCaps = desc7->dpcLineCaps.dwTextureAddressCaps;
4816     desc1->dpcLineCaps.dwStippleWidth = desc7->dpcLineCaps.dwStippleWidth;
4817     desc1->dpcLineCaps.dwAlphaCmpCaps = desc7->dpcLineCaps.dwAlphaCmpCaps;
4818
4819     desc1->dpcTriCaps.dwSize = sizeof(D3DPRIMCAPS);
4820     desc1->dpcTriCaps.dwMiscCaps = desc7->dpcTriCaps.dwMiscCaps;
4821     desc1->dpcTriCaps.dwRasterCaps = desc7->dpcTriCaps.dwRasterCaps;
4822     desc1->dpcTriCaps.dwZCmpCaps = desc7->dpcTriCaps.dwZCmpCaps;
4823     desc1->dpcTriCaps.dwSrcBlendCaps = desc7->dpcTriCaps.dwSrcBlendCaps;
4824     desc1->dpcTriCaps.dwDestBlendCaps = desc7->dpcTriCaps.dwDestBlendCaps;
4825     desc1->dpcTriCaps.dwShadeCaps = desc7->dpcTriCaps.dwShadeCaps;
4826     desc1->dpcTriCaps.dwTextureCaps = desc7->dpcTriCaps.dwTextureCaps;
4827     desc1->dpcTriCaps.dwTextureFilterCaps = desc7->dpcTriCaps.dwTextureFilterCaps;
4828     desc1->dpcTriCaps.dwTextureBlendCaps = desc7->dpcTriCaps.dwTextureBlendCaps;
4829     desc1->dpcTriCaps.dwTextureAddressCaps = desc7->dpcTriCaps.dwTextureAddressCaps;
4830     desc1->dpcTriCaps.dwStippleWidth = desc7->dpcTriCaps.dwStippleWidth;
4831     desc1->dpcTriCaps.dwAlphaCmpCaps = desc7->dpcTriCaps.dwAlphaCmpCaps;
4832
4833     desc1->dwDeviceRenderBitDepth = desc7->dwDeviceRenderBitDepth;
4834     desc1->dwDeviceZBufferBitDepth = desc7->dwDeviceZBufferBitDepth;
4835     desc1->dwMaxBufferSize = 0;
4836     desc1->dwMaxVertexCount = 65536;
4837     desc1->dwMinTextureWidth  = desc7->dwMinTextureWidth;
4838     desc1->dwMinTextureHeight = desc7->dwMinTextureHeight;
4839     desc1->dwMaxTextureWidth  = desc7->dwMaxTextureWidth;
4840     desc1->dwMaxTextureHeight = desc7->dwMaxTextureHeight;
4841     desc1->dwMinStippleWidth  = 1;
4842     desc1->dwMinStippleHeight = 1;
4843     desc1->dwMaxStippleWidth  = 32;
4844     desc1->dwMaxStippleHeight = 32;
4845     desc1->dwMaxTextureRepeat = desc7->dwMaxTextureRepeat;
4846     desc1->dwMaxTextureAspectRatio = desc7->dwMaxTextureAspectRatio;
4847     desc1->dwMaxAnisotropy = desc7->dwMaxAnisotropy;
4848     desc1->dvGuardBandLeft = desc7->dvGuardBandLeft;
4849     desc1->dvGuardBandRight = desc7->dvGuardBandRight;
4850     desc1->dvGuardBandTop = desc7->dvGuardBandTop;
4851     desc1->dvGuardBandBottom = desc7->dvGuardBandBottom;
4852     desc1->dvExtentsAdjust = desc7->dvExtentsAdjust;
4853     desc1->dwStencilCaps = desc7->dwStencilCaps;
4854     desc1->dwFVFCaps = desc7->dwFVFCaps;
4855     desc1->dwTextureOpCaps = desc7->dwTextureOpCaps;
4856     desc1->wMaxTextureBlendStages = desc7->wMaxTextureBlendStages;
4857     desc1->wMaxSimultaneousTextures = desc7->wMaxSimultaneousTextures;
4858
4859     return DD_OK;
4860 }
4861
4862 /*****************************************************************************
4863  * IDirectDraw7 VTable
4864  *****************************************************************************/
4865 static const struct IDirectDraw7Vtbl ddraw7_vtbl =
4866 {
4867     /* IUnknown */
4868     ddraw7_QueryInterface,
4869     ddraw7_AddRef,
4870     ddraw7_Release,
4871     /* IDirectDraw */
4872     ddraw7_Compact,
4873     ddraw7_CreateClipper,
4874     ddraw7_CreatePalette,
4875     ddraw7_CreateSurface,
4876     ddraw7_DuplicateSurface,
4877     ddraw7_EnumDisplayModes,
4878     ddraw7_EnumSurfaces,
4879     ddraw7_FlipToGDISurface,
4880     ddraw7_GetCaps,
4881     ddraw7_GetDisplayMode,
4882     ddraw7_GetFourCCCodes,
4883     ddraw7_GetGDISurface,
4884     ddraw7_GetMonitorFrequency,
4885     ddraw7_GetScanLine,
4886     ddraw7_GetVerticalBlankStatus,
4887     ddraw7_Initialize,
4888     ddraw7_RestoreDisplayMode,
4889     ddraw7_SetCooperativeLevel,
4890     ddraw7_SetDisplayMode,
4891     ddraw7_WaitForVerticalBlank,
4892     /* IDirectDraw2 */
4893     ddraw7_GetAvailableVidMem,
4894     /* IDirectDraw3 */
4895     ddraw7_GetSurfaceFromDC,
4896     /* IDirectDraw4 */
4897     ddraw7_RestoreAllSurfaces,
4898     ddraw7_TestCooperativeLevel,
4899     ddraw7_GetDeviceIdentifier,
4900     /* IDirectDraw7 */
4901     ddraw7_StartModeTest,
4902     ddraw7_EvaluateMode
4903 };
4904
4905 static const struct IDirectDraw4Vtbl ddraw4_vtbl =
4906 {
4907     /* IUnknown */
4908     ddraw4_QueryInterface,
4909     ddraw4_AddRef,
4910     ddraw4_Release,
4911     /* IDirectDraw */
4912     ddraw4_Compact,
4913     ddraw4_CreateClipper,
4914     ddraw4_CreatePalette,
4915     ddraw4_CreateSurface,
4916     ddraw4_DuplicateSurface,
4917     ddraw4_EnumDisplayModes,
4918     ddraw4_EnumSurfaces,
4919     ddraw4_FlipToGDISurface,
4920     ddraw4_GetCaps,
4921     ddraw4_GetDisplayMode,
4922     ddraw4_GetFourCCCodes,
4923     ddraw4_GetGDISurface,
4924     ddraw4_GetMonitorFrequency,
4925     ddraw4_GetScanLine,
4926     ddraw4_GetVerticalBlankStatus,
4927     ddraw4_Initialize,
4928     ddraw4_RestoreDisplayMode,
4929     ddraw4_SetCooperativeLevel,
4930     ddraw4_SetDisplayMode,
4931     ddraw4_WaitForVerticalBlank,
4932     /* IDirectDraw2 */
4933     ddraw4_GetAvailableVidMem,
4934     /* IDirectDraw3 */
4935     ddraw4_GetSurfaceFromDC,
4936     /* IDirectDraw4 */
4937     ddraw4_RestoreAllSurfaces,
4938     ddraw4_TestCooperativeLevel,
4939     ddraw4_GetDeviceIdentifier,
4940 };
4941
4942 static const struct IDirectDraw2Vtbl ddraw2_vtbl =
4943 {
4944     /* IUnknown */
4945     ddraw2_QueryInterface,
4946     ddraw2_AddRef,
4947     ddraw2_Release,
4948     /* IDirectDraw */
4949     ddraw2_Compact,
4950     ddraw2_CreateClipper,
4951     ddraw2_CreatePalette,
4952     ddraw2_CreateSurface,
4953     ddraw2_DuplicateSurface,
4954     ddraw2_EnumDisplayModes,
4955     ddraw2_EnumSurfaces,
4956     ddraw2_FlipToGDISurface,
4957     ddraw2_GetCaps,
4958     ddraw2_GetDisplayMode,
4959     ddraw2_GetFourCCCodes,
4960     ddraw2_GetGDISurface,
4961     ddraw2_GetMonitorFrequency,
4962     ddraw2_GetScanLine,
4963     ddraw2_GetVerticalBlankStatus,
4964     ddraw2_Initialize,
4965     ddraw2_RestoreDisplayMode,
4966     ddraw2_SetCooperativeLevel,
4967     ddraw2_SetDisplayMode,
4968     ddraw2_WaitForVerticalBlank,
4969     /* IDirectDraw2 */
4970     ddraw2_GetAvailableVidMem,
4971 };
4972
4973 static const struct IDirectDrawVtbl ddraw1_vtbl =
4974 {
4975     /* IUnknown */
4976     ddraw1_QueryInterface,
4977     ddraw1_AddRef,
4978     ddraw1_Release,
4979     /* IDirectDraw */
4980     ddraw1_Compact,
4981     ddraw1_CreateClipper,
4982     ddraw1_CreatePalette,
4983     ddraw1_CreateSurface,
4984     ddraw1_DuplicateSurface,
4985     ddraw1_EnumDisplayModes,
4986     ddraw1_EnumSurfaces,
4987     ddraw1_FlipToGDISurface,
4988     ddraw1_GetCaps,
4989     ddraw1_GetDisplayMode,
4990     ddraw1_GetFourCCCodes,
4991     ddraw1_GetGDISurface,
4992     ddraw1_GetMonitorFrequency,
4993     ddraw1_GetScanLine,
4994     ddraw1_GetVerticalBlankStatus,
4995     ddraw1_Initialize,
4996     ddraw1_RestoreDisplayMode,
4997     ddraw1_SetCooperativeLevel,
4998     ddraw1_SetDisplayMode,
4999     ddraw1_WaitForVerticalBlank,
5000 };
5001
5002 static const struct IDirect3D7Vtbl d3d7_vtbl =
5003 {
5004     /* IUnknown methods */
5005     d3d7_QueryInterface,
5006     d3d7_AddRef,
5007     d3d7_Release,
5008     /* IDirect3D7 methods */
5009     d3d7_EnumDevices,
5010     d3d7_CreateDevice,
5011     d3d7_CreateVertexBuffer,
5012     d3d7_EnumZBufferFormats,
5013     d3d7_EvictManagedTextures
5014 };
5015
5016 static const struct IDirect3D3Vtbl d3d3_vtbl =
5017 {
5018     /* IUnknown methods */
5019     d3d3_QueryInterface,
5020     d3d3_AddRef,
5021     d3d3_Release,
5022     /* IDirect3D3 methods */
5023     d3d3_EnumDevices,
5024     d3d3_CreateLight,
5025     d3d3_CreateMaterial,
5026     d3d3_CreateViewport,
5027     d3d3_FindDevice,
5028     d3d3_CreateDevice,
5029     d3d3_CreateVertexBuffer,
5030     d3d3_EnumZBufferFormats,
5031     d3d3_EvictManagedTextures
5032 };
5033
5034 static const struct IDirect3D2Vtbl d3d2_vtbl =
5035 {
5036     /* IUnknown methods */
5037     d3d2_QueryInterface,
5038     d3d2_AddRef,
5039     d3d2_Release,
5040     /* IDirect3D2 methods */
5041     d3d2_EnumDevices,
5042     d3d2_CreateLight,
5043     d3d2_CreateMaterial,
5044     d3d2_CreateViewport,
5045     d3d2_FindDevice,
5046     d3d2_CreateDevice
5047 };
5048
5049 static const struct IDirect3DVtbl d3d1_vtbl =
5050 {
5051     /* IUnknown methods */
5052     d3d1_QueryInterface,
5053     d3d1_AddRef,
5054     d3d1_Release,
5055     /* IDirect3D methods */
5056     d3d1_Initialize,
5057     d3d1_EnumDevices,
5058     d3d1_CreateLight,
5059     d3d1_CreateMaterial,
5060     d3d1_CreateViewport,
5061     d3d1_FindDevice
5062 };
5063
5064 /*****************************************************************************
5065  * ddraw_find_decl
5066  *
5067  * Finds the WineD3D vertex declaration for a specific fvf, and creates one
5068  * if none was found.
5069  *
5070  * This function is in ddraw.c and the DDraw object space because D3D7
5071  * vertex buffers are created using the IDirect3D interface to the ddraw
5072  * object, so they can be valid across D3D devices(theoretically. The ddraw
5073  * object also owns the wined3d device
5074  *
5075  * Parameters:
5076  *  This: Device
5077  *  fvf: Fvf to find the decl for
5078  *
5079  * Returns:
5080  *  NULL in case of an error, the vertex declaration for the FVF otherwise.
5081  *
5082  *****************************************************************************/
5083 struct wined3d_vertex_declaration *ddraw_find_decl(struct ddraw *This, DWORD fvf)
5084 {
5085     struct wined3d_vertex_declaration *pDecl = NULL;
5086     HRESULT hr;
5087     int p, low, high; /* deliberately signed */
5088     struct FvfToDecl *convertedDecls = This->decls;
5089
5090     TRACE("Searching for declaration for fvf %08x... ", fvf);
5091
5092     low = 0;
5093     high = This->numConvertedDecls - 1;
5094     while(low <= high) {
5095         p = (low + high) >> 1;
5096         TRACE("%d ", p);
5097         if(convertedDecls[p].fvf == fvf) {
5098             TRACE("found %p\n", convertedDecls[p].decl);
5099             return convertedDecls[p].decl;
5100         } else if(convertedDecls[p].fvf < fvf) {
5101             low = p + 1;
5102         } else {
5103             high = p - 1;
5104         }
5105     }
5106     TRACE("not found. Creating and inserting at position %d.\n", low);
5107
5108     hr = wined3d_vertex_declaration_create_from_fvf(This->wined3d_device,
5109             fvf, This, &ddraw_null_wined3d_parent_ops, &pDecl);
5110     if (hr != S_OK) return NULL;
5111
5112     if(This->declArraySize == This->numConvertedDecls) {
5113         int grow = max(This->declArraySize / 2, 8);
5114         convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
5115                                      sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
5116         if (!convertedDecls)
5117         {
5118             wined3d_vertex_declaration_decref(pDecl);
5119             return NULL;
5120         }
5121         This->decls = convertedDecls;
5122         This->declArraySize += grow;
5123     }
5124
5125     memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
5126     convertedDecls[low].decl = pDecl;
5127     convertedDecls[low].fvf = fvf;
5128     This->numConvertedDecls++;
5129
5130     TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
5131     return pDecl;
5132 }
5133
5134 static inline struct ddraw *ddraw_from_device_parent(struct wined3d_device_parent *device_parent)
5135 {
5136     return CONTAINING_RECORD(device_parent, struct ddraw, device_parent);
5137 }
5138
5139 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
5140         struct wined3d_device *device)
5141 {
5142     TRACE("device_parent %p, device %p.\n", device_parent, device);
5143 }
5144
5145 /* This is run from device_process_message() in wined3d, we can't take the
5146  * wined3d mutex. */
5147 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
5148 {
5149     struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
5150     MONITORINFO monitor_info;
5151     HMONITOR monitor;
5152     BOOL ret;
5153     RECT *r;
5154
5155     TRACE("device_parent %p.\n", device_parent);
5156
5157     if (!(ddraw->cooperative_level & DDSCL_EXCLUSIVE) || !ddraw->swapchain_window)
5158     {
5159         TRACE("Nothing to resize.\n");
5160         return;
5161     }
5162
5163     monitor = MonitorFromWindow(ddraw->swapchain_window, MONITOR_DEFAULTTOPRIMARY);
5164     monitor_info.cbSize = sizeof(monitor_info);
5165     if (!(ret = GetMonitorInfoW(monitor, &monitor_info)))
5166     {
5167         ERR("Failed to get monitor info.\n");
5168         return;
5169     }
5170
5171     r = &monitor_info.rcMonitor;
5172     TRACE("Resizing window %p to %s.\n", ddraw->swapchain_window, wine_dbgstr_rect(r));
5173
5174     if (!(ret = SetWindowPos(ddraw->swapchain_window, HWND_TOP, r->left, r->top,
5175             r->right - r->left, r->bottom - r->top, SWP_SHOWWINDOW | SWP_NOACTIVATE)))
5176         ERR("Failed to resize window.\n");
5177 }
5178
5179 static HRESULT CDECL device_parent_create_texture_surface(struct wined3d_device_parent *device_parent,
5180         void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
5181         enum wined3d_pool pool, UINT level, enum wined3d_cubemap_face face, struct wined3d_surface **surface)
5182 {
5183     struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
5184     struct ddraw_surface *tex_root = container_parent;
5185     DDSURFACEDESC2 desc = tex_root->surface_desc;
5186     struct ddraw_surface *ddraw_surface;
5187     HRESULT hr;
5188
5189     TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
5190             "\tpool %#x, level %u, face %u, surface %p.\n",
5191             device_parent, container_parent, width, height, format, usage, pool, level, face, surface);
5192
5193     /* The ddraw root surface is created before the wined3d texture. */
5194     if (!level && face == WINED3D_CUBEMAP_FACE_POSITIVE_X)
5195     {
5196         ddraw_surface = tex_root;
5197         goto done;
5198     }
5199
5200     desc.dwWidth = width;
5201     desc.dwHeight = height;
5202     if (level)
5203         desc.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
5204     else
5205         desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
5206
5207     if (desc.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5208     {
5209         desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
5210
5211         switch (face)
5212         {
5213             case WINED3D_CUBEMAP_FACE_POSITIVE_X:
5214                 desc.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
5215                 break;
5216
5217             case WINED3D_CUBEMAP_FACE_NEGATIVE_X:
5218                 desc.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
5219                 break;
5220
5221             case WINED3D_CUBEMAP_FACE_POSITIVE_Y:
5222                 desc.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
5223                 break;
5224
5225             case WINED3D_CUBEMAP_FACE_NEGATIVE_Y:
5226                 desc.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
5227                 break;
5228
5229             case WINED3D_CUBEMAP_FACE_POSITIVE_Z:
5230                 desc.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
5231                 break;
5232
5233             case WINED3D_CUBEMAP_FACE_NEGATIVE_Z:
5234                 desc.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
5235                 break;
5236
5237             default:
5238                 ERR("Unexpected cube face.\n");
5239                 return DDERR_INVALIDPARAMS;
5240         }
5241
5242     }
5243
5244     /* FIXME: Validate that format, usage, pool, etc. really make sense. */
5245     if (FAILED(hr = ddraw_create_surface(ddraw, &desc, &ddraw_surface, level, tex_root->version)))
5246         return hr;
5247
5248 done:
5249     *surface = ddraw_surface->wined3d_surface;
5250     wined3d_surface_incref(*surface);
5251
5252     return DD_OK;
5253 }
5254
5255 static void STDMETHODCALLTYPE ddraw_frontbuffer_destroyed(void *parent)
5256 {
5257     struct ddraw *ddraw = parent;
5258     ddraw->wined3d_frontbuffer = NULL;
5259 }
5260
5261 static const struct wined3d_parent_ops ddraw_frontbuffer_parent_ops =
5262 {
5263     ddraw_frontbuffer_destroyed,
5264 };
5265
5266 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
5267         void *container_parent, UINT width, UINT height, enum wined3d_format_id format_id, DWORD usage,
5268         enum wined3d_multisample_type multisample_type, DWORD multisample_quality, struct wined3d_surface **surface)
5269 {
5270     struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
5271     HRESULT hr;
5272
5273     TRACE("device_parent %p, container_parent %p, width %u, height %u, format_id %#x, usage %#x,\n"
5274             "\tmultisample_type %#x, multisample_quality %u, surface %p.\n",
5275             device_parent, container_parent, width, height, format_id, usage,
5276             multisample_type, multisample_quality, surface);
5277
5278     if (ddraw->wined3d_frontbuffer)
5279     {
5280         ERR("Frontbuffer already created.\n");
5281         return E_FAIL;
5282     }
5283
5284     if (SUCCEEDED(hr = wined3d_surface_create(ddraw->wined3d_device, width, height, format_id, 0,
5285             usage, WINED3D_POOL_DEFAULT, multisample_type, multisample_quality, DefaultSurfaceType,
5286             WINED3D_SURFACE_MAPPABLE, ddraw, &ddraw_frontbuffer_parent_ops, surface)))
5287         ddraw->wined3d_frontbuffer = *surface;
5288
5289     return hr;
5290 }
5291
5292 static HRESULT CDECL device_parent_create_volume(struct wined3d_device_parent *device_parent,
5293         void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
5294         enum wined3d_pool pool, DWORD usage, struct wined3d_volume **volume)
5295 {
5296     TRACE("device_parent %p, container_parent %p, width %u, height %u, depth %u, "
5297             "format %#x, pool %#x, usage %#x, volume %p.\n",
5298             device_parent, container_parent, width, height, depth,
5299             format, pool, usage, volume);
5300
5301     ERR("Not implemented!\n");
5302
5303     return E_NOTIMPL;
5304 }
5305
5306 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
5307         struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
5308 {
5309     struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
5310     HRESULT hr;
5311
5312     TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
5313
5314     if (ddraw->wined3d_swapchain)
5315     {
5316         ERR("Swapchain already created.\n");
5317         return E_FAIL;
5318     }
5319
5320     hr = wined3d_swapchain_create(ddraw->wined3d_device, desc,
5321             DefaultSurfaceType, NULL, &ddraw_null_wined3d_parent_ops, swapchain);
5322     if (FAILED(hr))
5323         WARN("Failed to create swapchain, hr %#x.\n", hr);
5324
5325     return hr;
5326 }
5327
5328 static const struct wined3d_device_parent_ops ddraw_wined3d_device_parent_ops =
5329 {
5330     device_parent_wined3d_device_created,
5331     device_parent_mode_changed,
5332     device_parent_create_swapchain_surface,
5333     device_parent_create_texture_surface,
5334     device_parent_create_volume,
5335     device_parent_create_swapchain,
5336 };
5337
5338 HRESULT ddraw_init(struct ddraw *ddraw, enum wined3d_device_type device_type)
5339 {
5340     HRESULT hr;
5341
5342     ddraw->IDirectDraw7_iface.lpVtbl = &ddraw7_vtbl;
5343     ddraw->IDirectDraw_iface.lpVtbl = &ddraw1_vtbl;
5344     ddraw->IDirectDraw2_iface.lpVtbl = &ddraw2_vtbl;
5345     ddraw->IDirectDraw4_iface.lpVtbl = &ddraw4_vtbl;
5346     ddraw->IDirect3D_iface.lpVtbl = &d3d1_vtbl;
5347     ddraw->IDirect3D2_iface.lpVtbl = &d3d2_vtbl;
5348     ddraw->IDirect3D3_iface.lpVtbl = &d3d3_vtbl;
5349     ddraw->IDirect3D7_iface.lpVtbl = &d3d7_vtbl;
5350     ddraw->device_parent.ops = &ddraw_wined3d_device_parent_ops;
5351     ddraw->numIfaces = 1;
5352     ddraw->ref7 = 1;
5353
5354     ddraw->wined3d = wined3d_create(7, WINED3D_LEGACY_DEPTH_BIAS);
5355     if (!ddraw->wined3d)
5356     {
5357         WARN("Failed to create a wined3d object.\n");
5358         return E_OUTOFMEMORY;
5359     }
5360
5361     if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d,
5362             WINED3DADAPTER_DEFAULT, &ddraw->original_mode, NULL)))
5363     {
5364         ERR("Failed to get display mode, hr %#x.\n", hr);
5365         wined3d_decref(ddraw->wined3d);
5366         return hr;
5367     }
5368
5369     hr = wined3d_device_create(ddraw->wined3d, WINED3DADAPTER_DEFAULT, device_type,
5370             NULL, 0, 8, &ddraw->device_parent, &ddraw->wined3d_device);
5371     if (FAILED(hr))
5372     {
5373         WARN("Failed to create a wined3d device, hr %#x.\n", hr);
5374         wined3d_decref(ddraw->wined3d);
5375         return hr;
5376     }
5377
5378     list_init(&ddraw->surface_list);
5379
5380     return DD_OK;
5381 }