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