push 014043c4937c940c54cd1214c96e33a3b3c8cf7d
[wine/hacks.git] / dlls / ddraw / ddraw.c
blob4cac55d42ff64ca190386aaccdd35b83d1daa5b9
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
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <stdlib.h>
30 #define COBJMACROS
31 #define NONAMELESSUNION
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winerror.h"
36 #include "wingdi.h"
37 #include "wine/exception.h"
39 #include "ddraw.h"
40 #include "d3d.h"
42 #include "ddraw_private.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
47 static BOOL IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested, const DDSURFACEDESC2* provided);
48 static HRESULT WINAPI IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This, IDirectDrawSurfaceImpl *primary);
49 static HRESULT WINAPI IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This, DDSURFACEDESC2 *pDDSD, IDirectDrawSurfaceImpl **ppSurf, UINT level);
51 /* Device identifier. Don't relay it to WineD3D */
52 static const DDDEVICEIDENTIFIER2 deviceidentifier =
54 "display",
55 "DirectDraw HAL",
56 { { 0x00010001, 0x00010001 } },
57 0, 0, 0, 0,
58 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
59 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
63 /*****************************************************************************
64 * IUnknown Methods
65 *****************************************************************************/
67 /*****************************************************************************
68 * IDirectDraw7::QueryInterface
70 * Queries different interfaces of the DirectDraw object. It can return
71 * IDirectDraw interfaces in version 1, 2, 4 and 7, and IDirect3D interfaces
72 * in version 1, 2, 3 and 7. An IDirect3DDevice can be created with this
73 * method.
74 * The returned interface is AddRef()-ed before it's returned
76 * Used for version 1, 2, 4 and 7
78 * Params:
79 * refiid: Interface ID asked for
80 * obj: Used to return the interface pointer
82 * Returns:
83 * S_OK if an interface was found
84 * E_NOINTERFACE if the requested interface wasn't found
86 *****************************************************************************/
87 static HRESULT WINAPI
88 IDirectDrawImpl_QueryInterface(IDirectDraw7 *iface,
89 REFIID refiid,
90 void **obj)
92 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
94 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(refiid), obj);
96 /* Can change surface impl type */
97 EnterCriticalSection(&ddraw_cs);
99 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
100 *obj = NULL;
102 if(!refiid)
104 LeaveCriticalSection(&ddraw_cs);
105 return DDERR_INVALIDPARAMS;
108 /* Check DirectDraw Interfaces */
109 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
110 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
112 *obj = ICOM_INTERFACE(This, IDirectDraw7);
113 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
115 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
117 *obj = ICOM_INTERFACE(This, IDirectDraw4);
118 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
120 else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
122 *obj = ICOM_INTERFACE(This, IDirectDraw3);
123 TRACE("(%p) Returning IDirectDraw3 interface at %p\n", This, *obj);
125 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
127 *obj = ICOM_INTERFACE(This, IDirectDraw2);
128 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
130 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
132 *obj = ICOM_INTERFACE(This, IDirectDraw);
133 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
136 /* Direct3D
137 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
138 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
139 * who had this idea and why. The older interfaces can query and IDirect3D version
140 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
141 * and messy to implement with the common creation function, so it has been left out here.
143 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
144 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
145 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
146 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
148 /* Check the surface implementation */
149 if(This->ImplType == SURFACE_UNKNOWN)
151 /* Apps may create the IDirect3D Interface before the primary surface.
152 * set the surface implementation */
153 This->ImplType = SURFACE_OPENGL;
154 TRACE("(%p) Choosing OpenGL surfaces because a Direct3D interface was requested\n", This);
156 else if(This->ImplType != SURFACE_OPENGL && DefaultSurfaceType == SURFACE_UNKNOWN)
158 ERR("(%p) The App is requesting a D3D device, but a non-OpenGL surface type was choosen. Prepare for trouble!\n", This);
159 ERR(" (%p) You may want to contact wine-devel for help\n", This);
160 /* Should I assert(0) here??? */
162 else if(This->ImplType != SURFACE_OPENGL)
164 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
165 /* Do not abort here, only reject 3D Device creation */
168 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
170 This->d3dversion = 1;
171 *obj = ICOM_INTERFACE(This, IDirect3D);
172 TRACE(" returning Direct3D interface at %p.\n", *obj);
174 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
176 This->d3dversion = 2;
177 *obj = ICOM_INTERFACE(This, IDirect3D2);
178 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
180 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
182 This->d3dversion = 3;
183 *obj = ICOM_INTERFACE(This, IDirect3D3);
184 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
186 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
188 This->d3dversion = 7;
189 *obj = ICOM_INTERFACE(This, IDirect3D7);
190 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
194 /* Unknown interface */
195 else
197 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
198 LeaveCriticalSection(&ddraw_cs);
199 return E_NOINTERFACE;
202 IUnknown_AddRef( (IUnknown *) *obj );
203 LeaveCriticalSection(&ddraw_cs);
204 return S_OK;
207 /*****************************************************************************
208 * IDirectDraw7::AddRef
210 * Increases the interfaces refcount, basically
212 * DDraw refcounting is a bit tricky. The different DirectDraw interface
213 * versions have individual refcounts, but the IDirect3D interfaces do not.
214 * All interfaces are from one object, that means calling QueryInterface on an
215 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
216 * IDirectDrawImpl object.
218 * That means all AddRef and Release implementations of IDirectDrawX work
219 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
220 * except of IDirect3D7 which thunks to IDirectDraw7
222 * Returns: The new refcount
224 *****************************************************************************/
225 static ULONG WINAPI
226 IDirectDrawImpl_AddRef(IDirectDraw7 *iface)
228 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
229 ULONG ref = InterlockedIncrement(&This->ref7);
231 TRACE("(%p) : incrementing IDirectDraw7 refcount from %u.\n", This, ref -1);
233 if(ref == 1) InterlockedIncrement(&This->numIfaces);
235 return ref;
238 /*****************************************************************************
239 * IDirectDrawImpl_Destroy
241 * Destroys a ddraw object if all refcounts are 0. This is to share code
242 * between the IDirectDrawX::Release functions
244 * Params:
245 * This: DirectDraw object to destroy
247 *****************************************************************************/
248 void
249 IDirectDrawImpl_Destroy(IDirectDrawImpl *This)
251 /* Clear the cooplevel to restore window and display mode */
252 IDirectDraw7_SetCooperativeLevel(ICOM_INTERFACE(This, IDirectDraw7),
253 NULL,
254 DDSCL_NORMAL);
256 /* Destroy the device window if we created one */
257 if(This->devicewindow != 0)
259 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
260 DestroyWindow(This->devicewindow);
261 This->devicewindow = 0;
264 /* Unregister the window class */
265 UnregisterClassA(This->classname, 0);
267 EnterCriticalSection(&ddraw_cs);
268 list_remove(&This->ddraw_list_entry);
269 LeaveCriticalSection(&ddraw_cs);
271 /* Release the attached WineD3D stuff */
272 IWineD3DDevice_Release(This->wineD3DDevice);
273 IWineD3D_Release(This->wineD3D);
275 /* Now free the object */
276 HeapFree(GetProcessHeap(), 0, This);
279 /*****************************************************************************
280 * IDirectDraw7::Release
282 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
284 * Returns: The new refcount
285 *****************************************************************************/
286 static ULONG WINAPI
287 IDirectDrawImpl_Release(IDirectDraw7 *iface)
289 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
290 ULONG ref = InterlockedDecrement(&This->ref7);
292 TRACE("(%p)->() decrementing IDirectDraw7 refcount from %u.\n", This, ref +1);
294 if(ref == 0)
296 ULONG ifacecount = InterlockedDecrement(&This->numIfaces);
297 if(ifacecount == 0) IDirectDrawImpl_Destroy(This);
300 return ref;
303 /*****************************************************************************
304 * IDirectDraw methods
305 *****************************************************************************/
307 /*****************************************************************************
308 * IDirectDraw7::SetCooperativeLevel
310 * Sets the cooperative level for the DirectDraw object, and the window
311 * assigned to it. The cooperative level determines the general behavior
312 * of the DirectDraw application
314 * Warning: This is quite tricky, as it's not really documented which
315 * cooperative levels can be combined with each other. If a game fails
316 * after this function, try to check the cooperative levels passed on
317 * Windows, and if it returns something different.
319 * If you think that this function caused the failure because it writes a
320 * fixme, be sure to run again with a +ddraw trace.
322 * What is known about cooperative levels (See the ddraw modes test):
323 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
324 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
325 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
326 * DDSCL_FULLSCREEN can be activated
327 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
329 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
330 * DDSCL_SETFOCUSWINDOW (partially),
331 * DDSCL_MULTITHREADED (work in progress)
333 * Unhandled flags, which should be implemented
334 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
335 * expect any difference to a normal window for wine)
336 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
337 * rendering (Possible test case: Half-life)
339 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
341 * These seem not really imporant for wine
342 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
344 * Returns:
345 * DD_OK if the cooperative level was set successfully
346 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
347 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
348 * (Probably others too, have to investigate)
350 *****************************************************************************/
351 static HRESULT WINAPI
352 IDirectDrawImpl_SetCooperativeLevel(IDirectDraw7 *iface,
353 HWND hwnd,
354 DWORD cooplevel)
356 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
357 HWND window;
358 HRESULT hr;
360 TRACE("(%p)->(%p,%08x)\n",This,hwnd,cooplevel);
361 DDRAW_dump_cooperativelevel(cooplevel);
363 EnterCriticalSection(&ddraw_cs);
365 /* Get the old window */
366 hr = IWineD3DDevice_GetHWND(This->wineD3DDevice, &window);
367 if(hr != D3D_OK)
369 ERR("IWineD3DDevice::GetHWND failed, hr = %08x\n", hr);
370 LeaveCriticalSection(&ddraw_cs);
371 return hr;
374 /* Tests suggest that we need one of them: */
375 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
376 DDSCL_NORMAL |
377 DDSCL_EXCLUSIVE )))
379 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
380 LeaveCriticalSection(&ddraw_cs);
381 return DDERR_INVALIDPARAMS;
384 /* Handle those levels first which set various hwnds */
385 if(cooplevel & DDSCL_SETFOCUSWINDOW)
387 /* This isn't compatible with a lot of flags */
388 if(cooplevel & ( DDSCL_MULTITHREADED |
389 DDSCL_FPUSETUP |
390 DDSCL_FPUPRESERVE |
391 DDSCL_ALLOWREBOOT |
392 DDSCL_ALLOWMODEX |
393 DDSCL_SETDEVICEWINDOW |
394 DDSCL_NORMAL |
395 DDSCL_EXCLUSIVE |
396 DDSCL_FULLSCREEN ) )
398 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
399 LeaveCriticalSection(&ddraw_cs);
400 return DDERR_INVALIDPARAMS;
402 else if( (This->cooperative_level & DDSCL_FULLSCREEN) && window)
404 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET\n");
405 LeaveCriticalSection(&ddraw_cs);
406 return DDERR_HWNDALREADYSET;
409 This->focuswindow = hwnd;
410 /* Won't use the hwnd param for anything else */
411 hwnd = NULL;
413 /* Use the focus window for drawing too */
414 IWineD3DDevice_SetHWND(This->wineD3DDevice, This->focuswindow);
416 /* Destroy the device window, if we have one */
417 if(This->devicewindow)
419 DestroyWindow(This->devicewindow);
420 This->devicewindow = NULL;
423 /* DDSCL_NORMAL or DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE */
424 if(cooplevel & DDSCL_NORMAL)
426 /* Can't coexist with fullscreen or exclusive */
427 if(cooplevel & (DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE) )
429 TRACE("(%p) DDSCL_NORMAL is not compative with DDSCL_FULLSCREEN or DDSCL_EXCLUSIVE\n", This);
430 LeaveCriticalSection(&ddraw_cs);
431 return DDERR_INVALIDPARAMS;
434 /* Switching from fullscreen? */
435 if(This->cooperative_level & DDSCL_FULLSCREEN)
437 /* Restore the display mode */
438 IDirectDraw7_RestoreDisplayMode(iface);
440 This->cooperative_level &= ~DDSCL_FULLSCREEN;
441 This->cooperative_level &= ~DDSCL_EXCLUSIVE;
442 This->cooperative_level &= ~DDSCL_ALLOWMODEX;
445 /* Don't override focus windows or private device windows */
446 if( hwnd &&
447 !(This->focuswindow) &&
448 !(This->devicewindow) &&
449 (hwnd != window) )
451 IWineD3DDevice_SetHWND(This->wineD3DDevice, hwnd);
454 IWineD3DDevice_SetFullscreen(This->wineD3DDevice,
455 FALSE);
457 else if(cooplevel & DDSCL_FULLSCREEN)
459 /* Needs DDSCL_EXCLUSIVE */
460 if(!(cooplevel & DDSCL_EXCLUSIVE) )
462 TRACE("(%p) DDSCL_FULLSCREEN needs DDSCL_EXCLUSIVE\n", This);
463 LeaveCriticalSection(&ddraw_cs);
464 return DDERR_INVALIDPARAMS;
466 /* Need a HWND
467 if(hwnd == 0)
469 TRACE("(%p) DDSCL_FULLSCREEN needs a HWND\n", This);
470 return DDERR_INVALIDPARAMS;
474 This->cooperative_level &= ~DDSCL_NORMAL;
475 IWineD3DDevice_SetFullscreen(This->wineD3DDevice,
476 TRUE);
478 /* Don't override focus windows or private device windows */
479 if( hwnd &&
480 !(This->focuswindow) &&
481 !(This->devicewindow) &&
482 (hwnd != window) )
484 IWineD3DDevice_SetHWND(This->wineD3DDevice, hwnd);
487 else if(cooplevel & DDSCL_EXCLUSIVE)
489 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN\n", This);
490 LeaveCriticalSection(&ddraw_cs);
491 return DDERR_INVALIDPARAMS;
494 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
496 /* Don't create a device window if a focus window is set */
497 if( !(This->focuswindow) )
499 HWND devicewindow = CreateWindowExA(0, This->classname, "DDraw device window",
500 WS_POPUP, 0, 0,
501 GetSystemMetrics(SM_CXSCREEN),
502 GetSystemMetrics(SM_CYSCREEN),
503 NULL, NULL, GetModuleHandleA(0), NULL);
505 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
506 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
508 IWineD3DDevice_SetHWND(This->wineD3DDevice, devicewindow);
509 This->devicewindow = devicewindow;
513 if(cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
515 /* Enable thread safety in wined3d */
516 IWineD3DDevice_SetMultithreaded(This->wineD3DDevice);
519 /* Unhandled flags */
520 if(cooplevel & DDSCL_ALLOWREBOOT)
521 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
522 if(cooplevel & DDSCL_ALLOWMODEX)
523 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
524 if(cooplevel & DDSCL_FPUSETUP)
525 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
526 if(cooplevel & DDSCL_FPUPRESERVE)
527 WARN("(%p) Unhandled flag DDSCL_FPUPRESERVE, harmless\n", This);
529 /* Store the cooperative_level */
530 This->cooperative_level |= cooplevel;
531 TRACE("SetCooperativeLevel retuning DD_OK\n");
532 LeaveCriticalSection(&ddraw_cs);
533 return DD_OK;
536 /*****************************************************************************
537 * IDirectDraw7::SetDisplayMode
539 * Sets the display screen resolution, color depth and refresh frequency
540 * when in fullscreen mode (in theory).
541 * Possible return values listed in the SDK suggest that this method fails
542 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
543 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
544 * It seems to be valid to pass 0 for With and Height, this has to be tested
545 * It could mean that the current video mode should be left as-is. (But why
546 * call it then?)
548 * Params:
549 * Height, Width: Screen dimension
550 * BPP: Color depth in Bits per pixel
551 * Refreshrate: Screen refresh rate
552 * Flags: Other stuff
554 * Returns
555 * DD_OK on success
557 *****************************************************************************/
558 static HRESULT WINAPI
559 IDirectDrawImpl_SetDisplayMode(IDirectDraw7 *iface,
560 DWORD Width,
561 DWORD Height,
562 DWORD BPP,
563 DWORD RefreshRate,
564 DWORD Flags)
566 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
567 WINED3DDISPLAYMODE Mode;
568 HRESULT hr;
569 TRACE("(%p)->(%d,%d,%d,%d,%x: Relay!\n", This, Width, Height, BPP, RefreshRate, Flags);
571 EnterCriticalSection(&ddraw_cs);
572 if( !Width || !Height )
574 ERR("Width=%d, Height=%d, what to do?\n", Width, Height);
575 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
576 LeaveCriticalSection(&ddraw_cs);
577 return DD_OK;
580 /* Check the exclusive mode
581 if(!(This->cooperative_level & DDSCL_EXCLUSIVE))
582 return DDERR_NOEXCLUSIVEMODE;
583 * This is WRONG. Don't know if the SDK is completely
584 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
585 * is returned, but Half-Life 1.1.1.1 (Steam version)
586 * depends on this
589 Mode.Width = Width;
590 Mode.Height = Height;
591 Mode.RefreshRate = RefreshRate;
592 switch(BPP)
594 case 8: Mode.Format = WINED3DFMT_P8; break;
595 case 15: Mode.Format = WINED3DFMT_X1R5G5B5; break;
596 case 16: Mode.Format = WINED3DFMT_R5G6B5; break;
597 case 24: Mode.Format = WINED3DFMT_R8G8B8; break;
598 case 32: Mode.Format = WINED3DFMT_X8R8G8B8; break;
601 /* TODO: The possible return values from msdn suggest that
602 * the screen mode can't be changed if a surface is locked
603 * or some drawing is in progress
606 /* TODO: Lose the primary surface */
607 hr = IWineD3DDevice_SetDisplayMode(This->wineD3DDevice,
608 0, /* First swapchain */
609 &Mode);
610 LeaveCriticalSection(&ddraw_cs);
611 switch(hr)
613 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
614 default: return hr;
618 /*****************************************************************************
619 * IDirectDraw7::RestoreDisplayMode
621 * Restores the display mode to what it was at creation time. Basically.
623 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
624 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
625 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
626 * -> DD_1 is released. The screen should be left at 1024x768x32.
627 * -> DD_2 is released. The screen should be set to 1400x1050x32
628 * This case is unhandled right now, but Empire Earth does it this way.
629 * (But perhaps there is something in SetCooperativeLevel to prevent this)
631 * The msdn says that this method resets the display mode to what it was before
632 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
634 * Returns
635 * DD_OK on success
636 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
638 *****************************************************************************/
639 static HRESULT WINAPI
640 IDirectDrawImpl_RestoreDisplayMode(IDirectDraw7 *iface)
642 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
643 TRACE("(%p)\n", This);
645 return IDirectDraw7_SetDisplayMode(ICOM_INTERFACE(This, IDirectDraw7),
646 This->orig_width,
647 This->orig_height,
648 This->orig_bpp,
653 /*****************************************************************************
654 * IDirectDraw7::GetCaps
656 * Returns the drives capabilities
658 * Used for version 1, 2, 4 and 7
660 * Params:
661 * DriverCaps: Structure to write the Hardware accelerated caps to
662 * HelCaps: Structure to write the emulation caps to
664 * Returns
665 * This implementation returns DD_OK only
667 *****************************************************************************/
668 static HRESULT WINAPI
669 IDirectDrawImpl_GetCaps(IDirectDraw7 *iface,
670 DDCAPS *DriverCaps,
671 DDCAPS *HELCaps)
673 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
674 TRACE("(%p)->(%p,%p)\n", This, DriverCaps, HELCaps);
676 /* One structure must be != NULL */
677 if( (!DriverCaps) && (!HELCaps) )
679 ERR("(%p) Invalid params to IDirectDrawImpl_GetCaps\n", This);
680 return DDERR_INVALIDPARAMS;
683 if(DriverCaps)
685 DD_STRUCT_COPY_BYSIZE(DriverCaps, &This->caps);
686 if (TRACE_ON(ddraw))
688 TRACE("Driver Caps :\n");
689 DDRAW_dump_DDCAPS(DriverCaps);
693 if(HELCaps)
695 DD_STRUCT_COPY_BYSIZE(HELCaps, &This->caps);
696 if (TRACE_ON(ddraw))
698 TRACE("HEL Caps :\n");
699 DDRAW_dump_DDCAPS(HELCaps);
703 return DD_OK;
706 /*****************************************************************************
707 * IDirectDraw7::Compact
709 * No idea what it does, MSDN says it's not implemented.
711 * Returns
712 * DD_OK, but this is unchecked
714 *****************************************************************************/
715 static HRESULT WINAPI
716 IDirectDrawImpl_Compact(IDirectDraw7 *iface)
718 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
719 TRACE("(%p)\n", This);
721 return DD_OK;
724 /*****************************************************************************
725 * IDirectDraw7::GetDisplayMode
727 * Returns information about the current display mode
729 * Exists in Version 1, 2, 4 and 7
731 * Params:
732 * DDSD: Address of a surface description structure to write the info to
734 * Returns
735 * DD_OK
737 *****************************************************************************/
738 static HRESULT WINAPI
739 IDirectDrawImpl_GetDisplayMode(IDirectDraw7 *iface,
740 DDSURFACEDESC2 *DDSD)
742 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
743 HRESULT hr;
744 WINED3DDISPLAYMODE Mode;
745 DWORD Size;
746 TRACE("(%p)->(%p): Relay\n", This, DDSD);
748 EnterCriticalSection(&ddraw_cs);
749 /* This seems sane */
750 if(!DDSD)
752 LeaveCriticalSection(&ddraw_cs);
753 return DDERR_INVALIDPARAMS;
756 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
757 * so one method can be used for all versions (Hopefully)
759 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
760 0 /* swapchain 0 */,
761 &Mode);
762 if( hr != D3D_OK )
764 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
765 LeaveCriticalSection(&ddraw_cs);
766 return hr;
769 Size = DDSD->dwSize;
770 memset(DDSD, 0, Size);
772 DDSD->dwSize = Size;
773 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
774 DDSD->dwWidth = Mode.Width;
775 DDSD->dwHeight = Mode.Height;
776 DDSD->u2.dwRefreshRate = 60;
777 DDSD->ddsCaps.dwCaps = 0;
778 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
779 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, Mode.Format);
780 DDSD->u1.lPitch = Mode.Width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
782 if(TRACE_ON(ddraw))
784 TRACE("Returning surface desc :\n");
785 DDRAW_dump_surface_desc(DDSD);
788 LeaveCriticalSection(&ddraw_cs);
789 return DD_OK;
792 /*****************************************************************************
793 * IDirectDraw7::GetFourCCCodes
795 * Returns an array of supported FourCC codes.
797 * Exists in Version 1, 2, 4 and 7
799 * Params:
800 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
801 * of enumerated codes
802 * Codes: Pointer to an array of DWORDs where the supported codes are written
803 * to
805 * Returns
806 * Always returns DD_OK, as it's a stub for now
808 *****************************************************************************/
809 static HRESULT WINAPI
810 IDirectDrawImpl_GetFourCCCodes(IDirectDraw7 *iface,
811 DWORD *NumCodes, DWORD *Codes)
813 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
814 FIXME("(%p)->(%p, %p): Stub!\n", This, NumCodes, Codes);
816 if(NumCodes) *NumCodes = 0;
818 return DD_OK;
821 /*****************************************************************************
822 * IDirectDraw7::GetMonitorFrequency
824 * Returns the monitor's frequency
826 * Exists in Version 1, 2, 4 and 7
828 * Params:
829 * Freq: Pointer to a DWORD to write the frequency to
831 * Returns
832 * Always returns DD_OK
834 *****************************************************************************/
835 static HRESULT WINAPI
836 IDirectDrawImpl_GetMonitorFrequency(IDirectDraw7 *iface,
837 DWORD *Freq)
839 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
840 TRACE("(%p)->(%p)\n", This, Freq);
842 /* Ideally this should be in WineD3D, as it concerns the screen setup,
843 * but for now this should make the games happy
845 *Freq = 60;
846 return DD_OK;
849 /*****************************************************************************
850 * IDirectDraw7::GetVerticalBlankStatus
852 * Returns the Vertical blank status of the monitor. This should be in WineD3D
853 * too basically, but as it's a semi stub, I didn't create a function there
855 * Params:
856 * status: Pointer to a BOOL to be filled with the vertical blank status
858 * Returns
859 * DD_OK on success
860 * DDERR_INVALIDPARAMS if status is NULL
862 *****************************************************************************/
863 static HRESULT WINAPI
864 IDirectDrawImpl_GetVerticalBlankStatus(IDirectDraw7 *iface,
865 BOOL *status)
867 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
868 TRACE("(%p)->(%p)\n", This, status);
870 /* This looks sane, the MSDN suggests it too */
871 EnterCriticalSection(&ddraw_cs);
872 if(!status)
874 LeaveCriticalSection(&ddraw_cs);
875 return DDERR_INVALIDPARAMS;
878 *status = This->fake_vblank;
879 This->fake_vblank = !This->fake_vblank;
880 LeaveCriticalSection(&ddraw_cs);
881 return DD_OK;
884 /*****************************************************************************
885 * IDirectDraw7::GetAvailableVidMem
887 * Returns the total and free video memory
889 * Params:
890 * Caps: Specifies the memory type asked for
891 * total: Pointer to a DWORD to be filled with the total memory
892 * free: Pointer to a DWORD to be filled with the free memory
894 * Returns
895 * DD_OK on success
896 * DDERR_INVALIDPARAMS of free and total are NULL
898 *****************************************************************************/
899 static HRESULT WINAPI
900 IDirectDrawImpl_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total, DWORD *free)
902 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
903 TRACE("(%p)->(%p, %p, %p)\n", This, Caps, total, free);
905 if(TRACE_ON(ddraw))
907 TRACE("(%p) Asked for memory with description: ", This);
908 DDRAW_dump_DDSCAPS2(Caps);
910 EnterCriticalSection(&ddraw_cs);
912 /* Todo: System memory vs local video memory vs non-local video memory
913 * The MSDN also mentions differences between texture memory and other
914 * resources, but that's not important
917 if( (!total) && (!free) )
919 LeaveCriticalSection(&ddraw_cs);
920 return DDERR_INVALIDPARAMS;
923 if(total) *total = This->total_vidmem;
924 if(free) *free = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
926 LeaveCriticalSection(&ddraw_cs);
927 return DD_OK;
930 /*****************************************************************************
931 * IDirectDraw7::Initialize
933 * Initializes a DirectDraw interface.
935 * Params:
936 * GUID: Interface identifier. Well, don't know what this is really good
937 * for
939 * Returns
940 * Returns DD_OK on the first call,
941 * DDERR_ALREADYINITIALIZED on repeated calls
943 *****************************************************************************/
944 static HRESULT WINAPI
945 IDirectDrawImpl_Initialize(IDirectDraw7 *iface,
946 GUID *Guid)
948 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
949 TRACE("(%p)->(%s): No-op\n", This, debugstr_guid(Guid));
951 if(This->initialized)
953 return DDERR_ALREADYINITIALIZED;
955 else
957 return DD_OK;
961 /*****************************************************************************
962 * IDirectDraw7::FlipToGDISurface
964 * "Makes the surface that the GDI writes to the primary surface"
965 * Looks like some windows specific thing we don't have to care about.
966 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
967 * show error boxes ;)
968 * Well, just return DD_OK.
970 * Returns:
971 * Always returns DD_OK
973 *****************************************************************************/
974 static HRESULT WINAPI
975 IDirectDrawImpl_FlipToGDISurface(IDirectDraw7 *iface)
977 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
978 TRACE("(%p)\n", This);
980 return DD_OK;
983 /*****************************************************************************
984 * IDirectDraw7::WaitForVerticalBlank
986 * This method allows applications to get in sync with the vertical blank
987 * interval.
988 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
989 * redraw the screen, most likely because of this stub
991 * Parameters:
992 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
993 * or DDWAITVB_BLOCKEND
994 * h: Not used, according to MSDN
996 * Returns:
997 * Always returns DD_OK
999 *****************************************************************************/
1000 static HRESULT WINAPI
1001 IDirectDrawImpl_WaitForVerticalBlank(IDirectDraw7 *iface,
1002 DWORD Flags,
1003 HANDLE h)
1005 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1006 FIXME("(%p)->(%x,%p): Stub\n", This, Flags, h);
1008 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1009 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1010 return DDERR_UNSUPPORTED; /* unchecked */
1012 return DD_OK;
1015 /*****************************************************************************
1016 * IDirectDraw7::GetScanLine
1018 * Returns the scan line that is being drawn on the monitor
1020 * Parameters:
1021 * Scanline: Address to write the scan line value to
1023 * Returns:
1024 * Always returns DD_OK
1026 *****************************************************************************/
1027 static HRESULT WINAPI IDirectDrawImpl_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1029 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1030 static BOOL hide = FALSE;
1031 WINED3DDISPLAYMODE Mode;
1033 /* This function is called often, so print the fixme only once */
1034 EnterCriticalSection(&ddraw_cs);
1035 if(!hide)
1037 FIXME("(%p)->(%p): Semi-Stub\n", This, Scanline);
1038 hide = TRUE;
1041 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1043 &Mode);
1045 /* Fake the line sweeping of the monitor */
1046 /* FIXME: We should synchronize with a source to keep the refresh rate */
1047 *Scanline = This->cur_scanline++;
1048 /* Assume 20 scan lines in the vertical blank */
1049 if (This->cur_scanline >= Mode.Height + 20)
1050 This->cur_scanline = 0;
1052 LeaveCriticalSection(&ddraw_cs);
1053 return DD_OK;
1056 /*****************************************************************************
1057 * IDirectDraw7::TestCooperativeLevel
1059 * Informs the application about the state of the video adapter, depending
1060 * on the cooperative level
1062 * Returns:
1063 * DD_OK if the device is in a sane state
1064 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1065 * if the state is not correct(See below)
1067 *****************************************************************************/
1068 static HRESULT WINAPI
1069 IDirectDrawImpl_TestCooperativeLevel(IDirectDraw7 *iface)
1071 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1072 HRESULT hr;
1073 TRACE("(%p)\n", This);
1075 EnterCriticalSection(&ddraw_cs);
1076 /* Description from MSDN:
1077 * For fullscreen apps return DDERR_NOEXCLUSIVEMODE if the user switched
1078 * away from the app with e.g. alt-tab. Windowed apps receive
1079 * DDERR_EXCLUSIVEMODEALREADYSET if another application created a
1080 * DirectDraw object in exclusive mode. DDERR_WRONGMODE is returned,
1081 * when the video mode has changed
1084 hr = IWineD3DDevice_TestCooperativeLevel(This->wineD3DDevice);
1086 /* Fix the result value. These values are mapped from their
1087 * d3d9 counterpart.
1089 switch(hr)
1091 case WINED3DERR_DEVICELOST:
1092 if(This->cooperative_level & DDSCL_EXCLUSIVE)
1094 LeaveCriticalSection(&ddraw_cs);
1095 return DDERR_NOEXCLUSIVEMODE;
1097 else
1099 LeaveCriticalSection(&ddraw_cs);
1100 return DDERR_EXCLUSIVEMODEALREADYSET;
1103 case WINED3DERR_DEVICENOTRESET:
1104 LeaveCriticalSection(&ddraw_cs);
1105 return DD_OK;
1107 case WINED3D_OK:
1108 LeaveCriticalSection(&ddraw_cs);
1109 return DD_OK;
1111 case WINED3DERR_DRIVERINTERNALERROR:
1112 default:
1113 ERR("(%p) Unexpected return value %08x from wineD3D, "
1114 " returning DD_OK\n", This, hr);
1117 LeaveCriticalSection(&ddraw_cs);
1118 return DD_OK;
1121 /*****************************************************************************
1122 * IDirectDraw7::GetGDISurface
1124 * Returns the surface that GDI is treating as the primary surface.
1125 * For Wine this is the front buffer
1127 * Params:
1128 * GDISurface: Address to write the surface pointer to
1130 * Returns:
1131 * DD_OK if the surface was found
1132 * DDERR_NOTFOUND if the GDI surface wasn't found
1134 *****************************************************************************/
1135 static HRESULT WINAPI
1136 IDirectDrawImpl_GetGDISurface(IDirectDraw7 *iface,
1137 IDirectDrawSurface7 **GDISurface)
1139 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1140 IWineD3DSurface *Surf;
1141 IDirectDrawSurface7 *ddsurf;
1142 HRESULT hr;
1143 DDSCAPS2 ddsCaps;
1144 TRACE("(%p)->(%p)\n", This, GDISurface);
1146 /* Get the back buffer from the wineD3DDevice and search its
1147 * attached surfaces for the front buffer
1149 EnterCriticalSection(&ddraw_cs);
1150 hr = IWineD3DDevice_GetBackBuffer(This->wineD3DDevice,
1151 0, /* SwapChain */
1152 0, /* first back buffer*/
1153 WINED3DBACKBUFFER_TYPE_MONO,
1154 &Surf);
1156 if( (hr != D3D_OK) ||
1157 (!Surf) )
1159 ERR("IWineD3DDevice::GetBackBuffer failed\n");
1160 LeaveCriticalSection(&ddraw_cs);
1161 return DDERR_NOTFOUND;
1164 /* GetBackBuffer AddRef()ed the surface, release it */
1165 IWineD3DSurface_Release(Surf);
1167 IWineD3DSurface_GetParent(Surf,
1168 (IUnknown **) &ddsurf);
1169 IDirectDrawSurface7_Release(ddsurf); /* For the GetParent */
1171 /* Find the front buffer */
1172 ddsCaps.dwCaps = DDSCAPS_FRONTBUFFER;
1173 hr = IDirectDrawSurface7_GetAttachedSurface(ddsurf,
1174 &ddsCaps,
1175 GDISurface);
1176 if(hr != DD_OK)
1178 ERR("IDirectDrawSurface7::GetAttachedSurface failed, hr = %x\n", hr);
1181 /* The AddRef is OK this time */
1182 LeaveCriticalSection(&ddraw_cs);
1183 return hr;
1186 /*****************************************************************************
1187 * IDirectDraw7::EnumDisplayModes
1189 * Enumerates the supported Display modes. The modes can be filtered with
1190 * the DDSD parameter.
1192 * Params:
1193 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES
1194 * DDSD: Surface description to filter the modes
1195 * Context: Pointer passed back to the callback function
1196 * cb: Application-provided callback function
1198 * Returns:
1199 * DD_OK on success
1200 * DDERR_INVALIDPARAMS if the callback wasn't set
1202 *****************************************************************************/
1203 static HRESULT WINAPI
1204 IDirectDrawImpl_EnumDisplayModes(IDirectDraw7 *iface,
1205 DWORD Flags,
1206 DDSURFACEDESC2 *DDSD,
1207 void *Context,
1208 LPDDENUMMODESCALLBACK2 cb)
1210 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1211 unsigned int modenum, fmt;
1212 WINED3DFORMAT pixelformat = WINED3DFMT_UNKNOWN;
1213 WINED3DDISPLAYMODE mode;
1214 DDSURFACEDESC2 callback_sd;
1216 WINED3DFORMAT checkFormatList[] =
1218 WINED3DFMT_R8G8B8,
1219 WINED3DFMT_A8R8G8B8,
1220 WINED3DFMT_X8R8G8B8,
1221 WINED3DFMT_R5G6B5,
1222 WINED3DFMT_X1R5G5B5,
1223 WINED3DFMT_A1R5G5B5,
1224 WINED3DFMT_A4R4G4B4,
1225 WINED3DFMT_R3G3B2,
1226 WINED3DFMT_A8R3G3B2,
1227 WINED3DFMT_X4R4G4B4,
1228 WINED3DFMT_A2B10G10R10,
1229 WINED3DFMT_A8B8G8R8,
1230 WINED3DFMT_X8B8G8R8,
1231 WINED3DFMT_A2R10G10B10,
1232 WINED3DFMT_A8P8,
1233 WINED3DFMT_P8
1236 TRACE("(%p)->(%p,%p,%p): Relay\n", This, DDSD, Context, cb);
1238 EnterCriticalSection(&ddraw_cs);
1239 /* This looks sane */
1240 if(!cb)
1242 LeaveCriticalSection(&ddraw_cs);
1243 return DDERR_INVALIDPARAMS;
1246 if(DDSD)
1248 if ((DDSD->dwFlags & DDSD_PIXELFORMAT) && (DDSD->u4.ddpfPixelFormat.dwFlags & DDPF_RGB) )
1249 pixelformat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
1252 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
1254 if(pixelformat != WINED3DFMT_UNKNOWN && checkFormatList[fmt] != pixelformat)
1256 continue;
1259 modenum = 0;
1260 while(IWineD3D_EnumAdapterModes(This->wineD3D,
1261 WINED3DADAPTER_DEFAULT,
1262 checkFormatList[fmt],
1263 modenum++,
1264 &mode) == WINED3D_OK)
1266 if(DDSD)
1268 if(DDSD->dwFlags & DDSD_WIDTH && mode.Width != DDSD->dwWidth) continue;
1269 if(DDSD->dwFlags & DDSD_HEIGHT && mode.Height != DDSD->dwHeight) continue;
1272 memset(&callback_sd, 0, sizeof(callback_sd));
1273 callback_sd.dwSize = sizeof(callback_sd);
1274 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1276 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH;
1277 if(Flags & DDEDM_REFRESHRATES)
1279 callback_sd.dwFlags |= DDSD_REFRESHRATE;
1280 callback_sd.u2.dwRefreshRate = mode.RefreshRate;
1283 callback_sd.dwWidth = mode.Width;
1284 callback_sd.dwHeight = mode.Height;
1286 PixelFormat_WineD3DtoDD(&callback_sd.u4.ddpfPixelFormat, mode.Format);
1288 /* Calc pitch and DWORD align like MSDN says */
1289 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.Width;
1290 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
1292 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
1293 callback_sd.u2.dwRefreshRate);
1295 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
1297 TRACE("Application asked to terminate the enumeration\n");
1298 LeaveCriticalSection(&ddraw_cs);
1299 return DD_OK;
1304 TRACE("End of enumeration\n");
1305 LeaveCriticalSection(&ddraw_cs);
1306 return DD_OK;
1309 /*****************************************************************************
1310 * IDirectDraw7::EvaluateMode
1312 * Used with IDirectDraw7::StartModeTest to test video modes.
1313 * EvaluateMode is used to pass or fail a mode, and continue with the next
1314 * mode
1316 * Params:
1317 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
1318 * Timeout: Returns the amount of seconds left before the mode would have
1319 * been failed automatically
1321 * Returns:
1322 * This implementation always DD_OK, because it's a stub
1324 *****************************************************************************/
1325 static HRESULT WINAPI
1326 IDirectDrawImpl_EvaluateMode(IDirectDraw7 *iface,
1327 DWORD Flags,
1328 DWORD *Timeout)
1330 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1331 FIXME("(%p)->(%d,%p): Stub!\n", This, Flags, Timeout);
1333 /* When implementing this, implement it in WineD3D */
1335 return DD_OK;
1338 /*****************************************************************************
1339 * IDirectDraw7::GetDeviceIdentifier
1341 * Returns the device identifier, which gives information about the driver
1342 * Our device identifier is defined at the beginning of this file.
1344 * Params:
1345 * DDDI: Address for the returned structure
1346 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
1348 * Returns:
1349 * On success it returns DD_OK
1350 * DDERR_INVALIDPARAMS if DDDI is NULL
1352 *****************************************************************************/
1353 static HRESULT WINAPI
1354 IDirectDrawImpl_GetDeviceIdentifier(IDirectDraw7 *iface,
1355 DDDEVICEIDENTIFIER2 *DDDI,
1356 DWORD Flags)
1358 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1359 TRACE("(%p)->(%p,%08x)\n", This, DDDI, Flags);
1361 if(!DDDI)
1362 return DDERR_INVALIDPARAMS;
1364 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
1365 * host adapter, if there's a secondary 3D adapter. This doesn't apply
1366 * to any modern hardware, nor is it interesting for Wine, so ignore it
1369 *DDDI = deviceidentifier;
1370 return DD_OK;
1373 /*****************************************************************************
1374 * IDirectDraw7::GetSurfaceFromDC
1376 * Returns the Surface for a GDI device context handle.
1377 * Is this related to IDirectDrawSurface::GetDC ???
1379 * Params:
1380 * hdc: hdc to return the surface for
1381 * Surface: Address to write the surface pointer to
1383 * Returns:
1384 * Always returns DD_OK because it's a stub
1386 *****************************************************************************/
1387 static HRESULT WINAPI
1388 IDirectDrawImpl_GetSurfaceFromDC(IDirectDraw7 *iface,
1389 HDC hdc,
1390 IDirectDrawSurface7 **Surface)
1392 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1393 FIXME("(%p)->(%p,%p): Stub!\n", This, hdc, Surface);
1395 /* Implementation idea if needed: Loop through all surfaces and compare
1396 * their hdc with hdc. Implement it in WineD3D! */
1397 return DDERR_NOTFOUND;
1400 /*****************************************************************************
1401 * IDirectDraw7::RestoreAllSurfaces
1403 * Calls the restore method of all surfaces
1405 * Params:
1407 * Returns:
1408 * Always returns DD_OK because it's a stub
1410 *****************************************************************************/
1411 static HRESULT WINAPI
1412 IDirectDrawImpl_RestoreAllSurfaces(IDirectDraw7 *iface)
1414 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1415 FIXME("(%p): Stub\n", This);
1417 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
1418 * get their parent and call their restore method. Do not implement
1419 * it in WineD3D, as restoring a surface means re-creating the
1420 * WineD3DDSurface
1422 return DD_OK;
1425 /*****************************************************************************
1426 * IDirectDraw7::StartModeTest
1428 * Tests the specified video modes to update the system registry with
1429 * refresh rate information. StartModeTest starts the mode test,
1430 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
1431 * isn't called within 15 seconds, the mode is failed automatically
1433 * As refresh rates are handled by the X server, I don't think this
1434 * Method is important
1436 * Params:
1437 * Modes: An array of mode specifications
1438 * NumModes: The number of modes in Modes
1439 * Flags: Some flags...
1441 * Returns:
1442 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
1443 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
1444 * otherwise DD_OK
1446 *****************************************************************************/
1447 static HRESULT WINAPI
1448 IDirectDrawImpl_StartModeTest(IDirectDraw7 *iface,
1449 SIZE *Modes,
1450 DWORD NumModes,
1451 DWORD Flags)
1453 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1454 WARN("(%p)->(%p, %d, %x): Semi-Stub, most likely harmless\n", This, Modes, NumModes, Flags);
1456 /* This looks sane */
1457 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
1459 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
1460 * As it is not, DDERR_TESTFINISHED is returned
1461 * (hopefully that's correct
1463 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
1464 * well, that value doesn't (yet) exist in the wine headers, so ignore it
1467 return DD_OK;
1470 /*****************************************************************************
1471 * IDirectDrawImpl_RecreateSurfacesCallback
1473 * Enumeration callback for IDirectDrawImpl_RecreateAllSurfaces.
1474 * It re-recreates the WineD3DSurface. It's pretty straightforward
1476 *****************************************************************************/
1477 HRESULT WINAPI
1478 IDirectDrawImpl_RecreateSurfacesCallback(IDirectDrawSurface7 *surf,
1479 DDSURFACEDESC2 *desc,
1480 void *Context)
1482 IDirectDrawSurfaceImpl *surfImpl = ICOM_OBJECT(IDirectDrawSurfaceImpl,
1483 IDirectDrawSurface7,
1484 surf);
1485 IDirectDrawImpl *This = surfImpl->ddraw;
1486 IUnknown *Parent;
1487 IParentImpl *parImpl = NULL;
1488 IWineD3DSurface *wineD3DSurface;
1489 HRESULT hr;
1490 void *tmp;
1491 IWineD3DClipper *clipper = NULL;
1493 WINED3DSURFACE_DESC Desc;
1494 WINED3DFORMAT Format;
1495 WINED3DRESOURCETYPE Type;
1496 DWORD Usage;
1497 WINED3DPOOL Pool;
1498 UINT Size;
1500 WINED3DMULTISAMPLE_TYPE MultiSampleType;
1501 DWORD MultiSampleQuality;
1502 UINT Width;
1503 UINT Height;
1505 TRACE("(%p): Enumerated Surface %p\n", This, surfImpl);
1507 /* For the enumeration */
1508 IDirectDrawSurface7_Release(surf);
1510 if(surfImpl->ImplType == This->ImplType) return DDENUMRET_OK; /* Continue */
1512 /* Get the objects */
1513 wineD3DSurface = surfImpl->WineD3DSurface;
1514 IWineD3DSurface_GetParent(wineD3DSurface, &Parent);
1515 IUnknown_Release(Parent); /* For the getParent */
1517 /* Is the parent an IParent interface? */
1518 if(IUnknown_QueryInterface(Parent, &IID_IParent, &tmp) == S_OK)
1520 /* It is a IParent interface! */
1521 IUnknown_Release(Parent); /* For the QueryInterface */
1522 parImpl = ICOM_OBJECT(IParentImpl, IParent, Parent);
1523 /* Release the reference the parent interface is holding */
1524 IWineD3DSurface_Release(wineD3DSurface);
1527 /* get the clipper */
1528 IWineD3DSurface_GetClipper(wineD3DSurface, &clipper);
1530 /* Get the surface properties */
1531 Desc.Format = &Format;
1532 Desc.Type = &Type;
1533 Desc.Usage = &Usage;
1534 Desc.Pool = &Pool;
1535 Desc.Size = &Size;
1536 Desc.MultiSampleType = &MultiSampleType;
1537 Desc.MultiSampleQuality = &MultiSampleQuality;
1538 Desc.Width = &Width;
1539 Desc.Height = &Height;
1541 hr = IWineD3DSurface_GetDesc(wineD3DSurface, &Desc);
1542 if(hr != D3D_OK) return hr;
1544 /* Create the new surface */
1545 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice,
1546 Width, Height, Format,
1547 TRUE /* Lockable */,
1548 FALSE /* Discard */,
1549 surfImpl->mipmap_level,
1550 &surfImpl->WineD3DSurface,
1551 Type,
1552 Usage,
1553 Pool,
1554 MultiSampleType,
1555 MultiSampleQuality,
1556 0 /* SharedHandle */,
1557 This->ImplType,
1558 Parent);
1560 if(hr != D3D_OK)
1561 return hr;
1563 IWineD3DSurface_SetClipper(surfImpl->WineD3DSurface, clipper);
1565 /* Update the IParent if it exists */
1566 if(parImpl)
1568 parImpl->child = (IUnknown *) surfImpl->WineD3DSurface;
1569 /* Add a reference for the IParent */
1570 IWineD3DSurface_AddRef(surfImpl->WineD3DSurface);
1572 /* TODO: Copy the surface content, except for render targets */
1574 if(IWineD3DSurface_Release(wineD3DSurface) == 0)
1575 TRACE("Surface released successful, next surface\n");
1576 else
1577 ERR("Something's still holding the old WineD3DSurface\n");
1579 surfImpl->ImplType = This->ImplType;
1581 if(clipper)
1583 IWineD3DClipper_Release(clipper);
1585 return DDENUMRET_OK;
1588 /*****************************************************************************
1589 * IDirectDrawImpl_RecreateAllSurfaces
1591 * A function, that converts all wineD3DSurfaces to the new implementation type
1592 * It enumerates all surfaces with IWineD3DDevice::EnumSurfaces, creates a
1593 * new WineD3DSurface, copies the content and releases the old surface
1595 *****************************************************************************/
1596 static HRESULT
1597 IDirectDrawImpl_RecreateAllSurfaces(IDirectDrawImpl *This)
1599 DDSURFACEDESC2 desc;
1600 TRACE("(%p): Switch to implementation %d\n", This, This->ImplType);
1602 if(This->ImplType != SURFACE_OPENGL && This->d3d_initialized)
1604 /* Should happen almost never */
1605 FIXME("(%p) Switching to non-opengl surfaces with d3d started. Is this a bug?\n", This);
1606 /* Shutdown d3d */
1607 IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroyDepthStencilSurface, D3D7CB_DestroySwapChain);
1609 /* Contrary: D3D starting is handled by the caller, because it knows the render target */
1611 memset(&desc, 0, sizeof(desc));
1612 desc.dwSize = sizeof(desc);
1614 return IDirectDraw7_EnumSurfaces(ICOM_INTERFACE(This, IDirectDraw7),
1616 &desc,
1617 This,
1618 IDirectDrawImpl_RecreateSurfacesCallback);
1621 /*****************************************************************************
1622 * D3D7CB_CreateSurface
1624 * Callback function for IDirect3DDevice_CreateTexture. It searches for the
1625 * correct mipmap sublevel, and returns it to WineD3D.
1626 * The surfaces are created already by IDirectDraw7::CreateSurface
1628 * Params:
1629 * With, Height: With and height of the surface
1630 * Format: The requested format
1631 * Usage, Pool: D3DUSAGE and D3DPOOL of the surface
1632 * level: The mipmap level
1633 * Face: The cube map face type
1634 * Surface: Pointer to pass the created surface back at
1635 * SharedHandle: NULL
1637 * Returns:
1638 * D3D_OK
1640 *****************************************************************************/
1641 static HRESULT WINAPI
1642 D3D7CB_CreateSurface(IUnknown *device,
1643 IUnknown *pSuperior,
1644 UINT Width, UINT Height,
1645 WINED3DFORMAT Format,
1646 DWORD Usage, WINED3DPOOL Pool, UINT level,
1647 WINED3DCUBEMAP_FACES Face,
1648 IWineD3DSurface **Surface,
1649 HANDLE *SharedHandle)
1651 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
1652 IDirectDrawSurfaceImpl *surf = NULL;
1653 int i = 0;
1654 DDSCAPS2 searchcaps = This->tex_root->surface_desc.ddsCaps;
1655 TRACE("(%p) call back. surf=%p. Face %d level %d\n", device, This->tex_root, Face, level);
1657 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
1658 switch(Face)
1660 case WINED3DCUBEMAP_FACE_POSITIVE_X:
1661 TRACE("Asked for positive x\n");
1662 if(searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP) {
1663 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
1665 surf = This->tex_root; break;
1666 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
1667 TRACE("Asked for negative x\n");
1668 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
1669 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
1670 TRACE("Asked for positive y\n");
1671 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
1672 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
1673 TRACE("Asked for negative y\n");
1674 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
1675 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
1676 TRACE("Asked for positive z\n");
1677 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
1678 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
1679 TRACE("Asked for negative z\n");
1680 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
1681 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
1684 if(!surf)
1686 IDirectDrawSurface7 *attached;
1687 IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(This->tex_root, IDirectDrawSurface7),
1688 &searchcaps,
1689 &attached);
1690 surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attached);
1691 IDirectDrawSurface7_Release(attached);
1693 if(!surf) ERR("root search surface not found\n");
1695 /* Find the wanted mipmap. There are enough mipmaps in the chain */
1696 while(i < level)
1698 IDirectDrawSurface7 *attached;
1699 IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(surf, IDirectDrawSurface7),
1700 &searchcaps,
1701 &attached);
1702 if(!attached) ERR("Surface not found\n");
1703 surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attached);
1704 IDirectDrawSurface7_Release(attached);
1705 i++;
1708 /* Return the surface */
1709 *Surface = surf->WineD3DSurface;
1711 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *Surface, surf);
1712 return D3D_OK;
1715 ULONG WINAPI D3D7CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
1716 IUnknown* swapChainParent;
1717 TRACE("(%p) call back\n", pSwapChain);
1719 IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
1720 IUnknown_Release(swapChainParent);
1721 return IUnknown_Release(swapChainParent);
1724 ULONG WINAPI D3D7CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
1725 IUnknown* surfaceParent;
1726 TRACE("(%p) call back\n", pSurface);
1728 IWineD3DSurface_GetParent(pSurface, &surfaceParent);
1729 IUnknown_Release(surfaceParent);
1730 return IUnknown_Release(surfaceParent);
1733 /*****************************************************************************
1734 * IDirectDrawImpl_CreateNewSurface
1736 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
1737 * with the passed parameters.
1739 * Params:
1740 * DDSD: Description of the surface to create
1741 * Surf: Address to store the interface pointer at
1743 * Returns:
1744 * DD_OK on success
1746 *****************************************************************************/
1747 static HRESULT WINAPI
1748 IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This,
1749 DDSURFACEDESC2 *pDDSD,
1750 IDirectDrawSurfaceImpl **ppSurf,
1751 UINT level)
1753 HRESULT hr;
1754 UINT Width = 0, Height = 0;
1755 WINED3DFORMAT Format = WINED3DFMT_UNKNOWN;
1756 WINED3DRESOURCETYPE ResType = WINED3DRTYPE_SURFACE;
1757 DWORD Usage = 0;
1758 WINED3DSURFTYPE ImplType = This->ImplType;
1759 WINED3DSURFACE_DESC Desc;
1760 IUnknown *Parent;
1761 IParentImpl *parImpl = NULL;
1762 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
1764 /* Dummies for GetDesc */
1765 WINED3DPOOL dummy_d3dpool;
1766 WINED3DMULTISAMPLE_TYPE dummy_mst;
1767 UINT dummy_uint;
1768 DWORD dummy_dword;
1770 if (TRACE_ON(ddraw))
1772 TRACE(" (%p) Requesting surface desc :\n", This);
1773 DDRAW_dump_surface_desc(pDDSD);
1776 /* Select the surface type, if it wasn't choosen yet */
1777 if(ImplType == SURFACE_UNKNOWN)
1779 /* Use GL Surfaces if a D3DDEVICE Surface is requested */
1780 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1782 TRACE("(%p) Choosing GL surfaces because a 3DDEVICE Surface was requested\n", This);
1783 ImplType = SURFACE_OPENGL;
1786 /* Otherwise use GDI surfaces for now */
1787 else
1789 TRACE("(%p) Choosing GDI surfaces for 2D rendering\n", This);
1790 ImplType = SURFACE_GDI;
1793 /* Policy if all surface implementations are available:
1794 * First, check if a default type was set with winecfg. If not,
1795 * try Xrender surfaces, and use them if they work. Next, check if
1796 * accelerated OpenGL is available, and use GL surfaces in this
1797 * case. If all else fails, use GDI surfaces. If a 3DDEVICE surface
1798 * was created, always use OpenGL surfaces.
1800 * (Note: Xrender surfaces are not implemented for now, the
1801 * unaccelerated implementation uses GDI to render in Software)
1804 /* Store the type. If it needs to be changed, all WineD3DSurfaces have to
1805 * be re-created. This could be done with IDirectDrawSurface7::Restore
1807 This->ImplType = ImplType;
1809 else
1811 if((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE ) &&
1812 (This->ImplType != SURFACE_OPENGL ) && DefaultSurfaceType == SURFACE_UNKNOWN)
1814 /* We have to change to OpenGL,
1815 * and re-create all WineD3DSurfaces
1817 ImplType = SURFACE_OPENGL;
1818 This->ImplType = ImplType;
1819 TRACE("(%p) Re-creating all surfaces\n", This);
1820 IDirectDrawImpl_RecreateAllSurfaces(This);
1821 TRACE("(%p) Done recreating all surfaces\n", This);
1823 else if(This->ImplType != SURFACE_OPENGL && pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1825 WARN("The application requests a 3D capable surface, but a non-opengl surface was set in the registry\n");
1826 /* Do not fail surface creation, only fail 3D device creation */
1830 /* Get the correct wined3d usage */
1831 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE |
1832 DDSCAPS_BACKBUFFER |
1833 DDSCAPS_3DDEVICE ) )
1835 Usage |= WINED3DUSAGE_RENDERTARGET;
1837 pDDSD->ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY |
1838 DDSCAPS_VISIBLE;
1840 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
1842 Usage |= WINED3DUSAGE_OVERLAY;
1844 if(This->depthstencil || (pDDSD->ddsCaps.dwCaps & DDSCAPS_ZBUFFER) )
1846 /* The depth stencil creation callback sets this flag.
1847 * Set the WineD3D usage to let it know that it's a depth
1848 * Stencil surface.
1850 Usage |= WINED3DUSAGE_DEPTHSTENCIL;
1852 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
1854 Pool = WINED3DPOOL_SYSTEMMEM;
1856 else if(pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
1858 Pool = WINED3DPOOL_MANAGED;
1859 /* Managed textures have the system memory flag set */
1860 pDDSD->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
1862 else if(pDDSD->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
1864 /* Videomemory adds localvidmem, this is mutually exclusive with systemmemory
1865 * and texturemanage
1867 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
1870 Format = PixelFormat_DD2WineD3D(&pDDSD->u4.ddpfPixelFormat);
1871 if(Format == WINED3DFMT_UNKNOWN)
1873 ERR("Unsupported / Unknown pixelformat\n");
1874 return DDERR_INVALIDPIXELFORMAT;
1877 /* Create the Surface object */
1878 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
1879 if(!*ppSurf)
1881 ERR("(%p) Error allocating memory for a surface\n", This);
1882 return DDERR_OUTOFVIDEOMEMORY;
1884 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawSurface7, IDirectDrawSurface7_Vtbl);
1885 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawSurface3, IDirectDrawSurface3_Vtbl);
1886 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawGammaControl, IDirectDrawGammaControl_Vtbl);
1887 ICOM_INIT_INTERFACE(*ppSurf, IDirect3DTexture2, IDirect3DTexture2_Vtbl);
1888 ICOM_INIT_INTERFACE(*ppSurf, IDirect3DTexture, IDirect3DTexture1_Vtbl);
1889 (*ppSurf)->ref = 1;
1890 (*ppSurf)->version = 7;
1891 (*ppSurf)->ddraw = This;
1892 (*ppSurf)->surface_desc.dwSize = sizeof(DDSURFACEDESC2);
1893 (*ppSurf)->surface_desc.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1894 DD_STRUCT_COPY_BYSIZE(&(*ppSurf)->surface_desc, pDDSD);
1896 /* Surface attachments */
1897 (*ppSurf)->next_attached = NULL;
1898 (*ppSurf)->first_attached = *ppSurf;
1900 /* Needed to re-create the surface on an implementation change */
1901 (*ppSurf)->ImplType = ImplType;
1903 /* For D3DDevice creation */
1904 (*ppSurf)->isRenderTarget = FALSE;
1906 /* A trace message for debugging */
1907 TRACE("(%p) Created IDirectDrawSurface implementation structure at %p\n", This, *ppSurf);
1909 if(pDDSD->ddsCaps.dwCaps & ( DDSCAPS_PRIMARYSURFACE | DDSCAPS_TEXTURE | DDSCAPS_3DDEVICE) )
1911 /* Render targets and textures need a IParent interface,
1912 * because WineD3D will destroy them when the swapchain
1913 * is released
1915 parImpl = HeapAlloc(GetProcessHeap(), 0, sizeof(IParentImpl));
1916 if(!parImpl)
1918 ERR("Out of memory when allocating memory for a IParent implementation\n");
1919 return DDERR_OUTOFMEMORY;
1921 parImpl->ref = 1;
1922 ICOM_INIT_INTERFACE(parImpl, IParent, IParent_Vtbl);
1923 Parent = (IUnknown *) ICOM_INTERFACE(parImpl, IParent);
1924 TRACE("Using IParent interface %p as parent\n", parImpl);
1926 else
1928 /* Use the surface as parent */
1929 Parent = (IUnknown *) ICOM_INTERFACE(*ppSurf, IDirectDrawSurface7);
1930 TRACE("Using Surface interface %p as parent\n", *ppSurf);
1933 /* Now create the WineD3D Surface */
1934 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice,
1935 pDDSD->dwWidth,
1936 pDDSD->dwHeight,
1937 Format,
1938 TRUE /* Lockable */,
1939 FALSE /* Discard */,
1940 level,
1941 &(*ppSurf)->WineD3DSurface,
1942 ResType, Usage,
1943 Pool,
1944 WINED3DMULTISAMPLE_NONE,
1945 0 /* MultiSampleQuality */,
1946 0 /* SharedHandle */,
1947 ImplType,
1948 Parent);
1950 if(hr != D3D_OK)
1952 ERR("IWineD3DDevice::CreateSurface failed. hr = %08x\n", hr);
1953 return hr;
1956 /* Set the child of the parent implementation if it exists */
1957 if(parImpl)
1959 parImpl->child = (IUnknown *) (*ppSurf)->WineD3DSurface;
1960 /* The IParent releases the WineD3DSurface, and
1961 * the ddraw surface does that too. Hold a reference
1963 IWineD3DSurface_AddRef((*ppSurf)->WineD3DSurface);
1966 /* Increase the surface counter, and attach the surface */
1967 InterlockedIncrement(&This->surfaces);
1968 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
1970 /* Here we could store all created surfaces in the DirectDrawImpl structure,
1971 * But this could also be delegated to WineDDraw, as it keeps track of all its
1972 * resources. Not implemented for now, as there are more important things ;)
1975 /* Get the pixel format of the WineD3DSurface and store it.
1976 * Don't use the Format choosen above, WineD3D might have
1977 * changed it
1979 Desc.Format = &Format;
1980 Desc.Type = &ResType;
1981 Desc.Usage = &Usage;
1982 Desc.Pool = &dummy_d3dpool;
1983 Desc.Size = &dummy_uint;
1984 Desc.MultiSampleType = &dummy_mst;
1985 Desc.MultiSampleQuality = &dummy_dword;
1986 Desc.Width = &Width;
1987 Desc.Height = &Height;
1989 (*ppSurf)->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
1990 hr = IWineD3DSurface_GetDesc((*ppSurf)->WineD3DSurface, &Desc);
1991 if(hr != D3D_OK)
1993 ERR("IWineD3DSurface::GetDesc failed\n");
1994 IDirectDrawSurface7_Release( (IDirectDrawSurface7 *) *ppSurf);
1995 return hr;
1998 if(Format == WINED3DFMT_UNKNOWN)
2000 FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN\n");
2002 PixelFormat_WineD3DtoDD( &(*ppSurf)->surface_desc.u4.ddpfPixelFormat, Format);
2004 /* Anno 1602 stores the pitch right after surface creation, so make sure it's there.
2005 * I can't LockRect() the surface here because if OpenGL surfaces are in use, the
2006 * WineD3DDevice might not be useable for 3D yet, so an extra method was created.
2007 * TODO: Test other fourcc formats
2009 if(Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3 ||
2010 Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5)
2012 (*ppSurf)->surface_desc.dwFlags |= DDSD_LINEARSIZE;
2013 if(Format == WINED3DFMT_DXT1)
2015 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height) / 2;
2017 else
2019 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height);
2022 else
2024 (*ppSurf)->surface_desc.dwFlags |= DDSD_PITCH;
2025 (*ppSurf)->surface_desc.u1.lPitch = IWineD3DSurface_GetPitch((*ppSurf)->WineD3DSurface);
2028 /* Application passed a color key? Set it! */
2029 if(pDDSD->dwFlags & DDSD_CKDESTOVERLAY)
2031 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2032 DDCKEY_DESTOVERLAY,
2033 (WINEDDCOLORKEY *) &pDDSD->u3.ddckCKDestOverlay);
2035 if(pDDSD->dwFlags & DDSD_CKDESTBLT)
2037 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2038 DDCKEY_DESTBLT,
2039 (WINEDDCOLORKEY *) &pDDSD->ddckCKDestBlt);
2041 if(pDDSD->dwFlags & DDSD_CKSRCOVERLAY)
2043 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2044 DDCKEY_SRCOVERLAY,
2045 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcOverlay);
2047 if(pDDSD->dwFlags & DDSD_CKSRCBLT)
2049 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2050 DDCKEY_SRCBLT,
2051 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcBlt);
2053 if ( pDDSD->dwFlags & DDSD_LPSURFACE)
2055 hr = IWineD3DSurface_SetMem((*ppSurf)->WineD3DSurface, pDDSD->lpSurface);
2056 if(hr != WINED3D_OK)
2058 /* No need for a trace here, wined3d does that for us */
2059 IDirectDrawSurface7_Release(ICOM_INTERFACE((*ppSurf), IDirectDrawSurface7));
2060 return hr;
2064 return DD_OK;
2066 /*****************************************************************************
2067 * CreateAdditionalSurfaces
2069 * Creates a new mipmap chain.
2071 * Params:
2072 * root: Root surface to attach the newly created chain to
2073 * count: number of surfaces to create
2074 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2075 * effects on the caller
2076 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2077 * creates an additional surface without the mipmapping flags
2079 *****************************************************************************/
2080 static HRESULT
2081 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2082 IDirectDrawSurfaceImpl *root,
2083 UINT count,
2084 DDSURFACEDESC2 DDSD,
2085 BOOL CubeFaceRoot)
2087 UINT i, j, level = 0;
2088 HRESULT hr;
2089 IDirectDrawSurfaceImpl *last = root;
2091 for(i = 0; i < count; i++)
2093 IDirectDrawSurfaceImpl *object2 = NULL;
2095 /* increase the mipmap level, but only if a mipmap is created
2096 * In this case, also halve the size
2098 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2100 level++;
2101 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2102 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2103 /* Set the mipmap sublevel flag according to msdn */
2104 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2106 else
2108 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2110 CubeFaceRoot = FALSE;
2112 hr = IDirectDrawImpl_CreateNewSurface(This,
2113 &DDSD,
2114 &object2,
2115 level);
2116 if(hr != DD_OK)
2118 return hr;
2121 /* Add the new surface to the complex attachment array */
2122 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2124 if(last->complex_array[j]) continue;
2125 last->complex_array[j] = object2;
2126 break;
2128 last = object2;
2130 /* Remove the (possible) back buffer cap from the new surface description,
2131 * because only one surface in the flipping chain is a back buffer, one
2132 * is a front buffer, the others are just primary surfaces.
2134 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2136 return DD_OK;
2139 /*****************************************************************************
2140 * IDirectDraw7::CreateSurface
2142 * Creates a new IDirectDrawSurface object and returns its interface.
2144 * The surface connections with wined3d are a bit tricky. Basically it works
2145 * like this:
2147 * |------------------------| |-----------------|
2148 * | DDraw surface | | WineD3DSurface |
2149 * | | | |
2150 * | WineD3DSurface |-------------->| |
2151 * | Child |<------------->| Parent |
2152 * |------------------------| |-----------------|
2154 * The DDraw surface is the parent of the wined3d surface, and it releases
2155 * the WineD3DSurface when the ddraw surface is destroyed.
2157 * However, for all surfaces which can be in a container in WineD3D,
2158 * we have to do this. These surfaces are ususally complex surfaces,
2159 * so this concerns primary surfaces with a front and a back buffer,
2160 * and textures.
2162 * |------------------------| |-----------------|
2163 * | DDraw surface | | Containter |
2164 * | | | |
2165 * | Child |<------------->| Parent |
2166 * | Texture |<------------->| |
2167 * | WineD3DSurface |<----| | Levels |<--|
2168 * | Complex connection | | | | |
2169 * |------------------------| | |-----------------| |
2170 * ^ | |
2171 * | | |
2172 * | | |
2173 * | |------------------| | |-----------------| |
2174 * | | IParent | |-------->| WineD3DSurface | |
2175 * | | | | | |
2176 * | | Child |<------------->| Parent | |
2177 * | | | | Container |<--|
2178 * | |------------------| |-----------------| |
2179 * | |
2180 * | |----------------------| |
2181 * | | DDraw surface 2 | |
2182 * | | | |
2183 * |<->| Complex root Child | |
2184 * | | Texture | |
2185 * | | WineD3DSurface |<----| |
2186 * | |----------------------| | |
2187 * | | |
2188 * | |---------------------| | |-----------------| |
2189 * | | IParent | |----->| WineD3DSurface | |
2190 * | | | | | |
2191 * | | Child |<---------->| Parent | |
2192 * | |---------------------| | Container |<--|
2193 * | |-----------------| |
2194 * | |
2195 * | ---More surfaces can follow--- |
2197 * The reason is that the IWineD3DSwapchain(render target container)
2198 * and the IWineD3DTexure(Texture container) release the parents
2199 * of their surface's children, but by releasing the complex root
2200 * the surfaces which are complexly attached to it are destroyed
2201 * too. See IDirectDrawSurface::Release for a more detailed
2202 * explanation.
2204 * Params:
2205 * DDSD: Description of the surface to create
2206 * Surf: Address to store the interface pointer at
2207 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2208 * aggregation, so it has to be NULL
2210 * Returns:
2211 * DD_OK on success
2212 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2213 * DDERR_* if an error occurs
2215 *****************************************************************************/
2216 static HRESULT WINAPI
2217 IDirectDrawImpl_CreateSurface(IDirectDraw7 *iface,
2218 DDSURFACEDESC2 *DDSD,
2219 IDirectDrawSurface7 **Surf,
2220 IUnknown *UnkOuter)
2222 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2223 IDirectDrawSurfaceImpl *object = NULL;
2224 HRESULT hr;
2225 LONG extra_surfaces = 0;
2226 DDSURFACEDESC2 desc2;
2227 WINED3DDISPLAYMODE Mode;
2229 TRACE("(%p)->(%p,%p,%p)\n", This, DDSD, Surf, UnkOuter);
2231 /* Some checks before we start */
2232 if (TRACE_ON(ddraw))
2234 TRACE(" (%p) Requesting surface desc :\n", This);
2235 DDRAW_dump_surface_desc(DDSD);
2237 EnterCriticalSection(&ddraw_cs);
2239 if (UnkOuter != NULL)
2241 FIXME("(%p) : outer != NULL?\n", This);
2242 LeaveCriticalSection(&ddraw_cs);
2243 return CLASS_E_NOAGGREGATION; /* unchecked */
2246 if (Surf == NULL)
2248 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", This);
2249 LeaveCriticalSection(&ddraw_cs);
2250 return E_POINTER; /* unchecked */
2253 if (!(DDSD->dwFlags & DDSD_CAPS))
2255 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2256 DDSD->dwFlags |= DDSD_CAPS;
2258 if (DDSD->ddsCaps.dwCaps == 0)
2260 /* This has been checked on real Windows */
2261 DDSD->ddsCaps.dwCaps = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
2264 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2266 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2267 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2270 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2272 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2273 WARN("(%p) Null surface pointer specified, ignore it!\n", This);
2274 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2277 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2278 !(This->cooperative_level & DDSCL_EXCLUSIVE))
2280 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n", This);
2281 *Surf = NULL;
2282 LeaveCriticalSection(&ddraw_cs);
2283 return DDERR_NOEXCLUSIVEMODE;
2286 if(DDSD->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER)) {
2287 WARN("Application tried to create an explicit front or back buffer\n");
2288 LeaveCriticalSection(&ddraw_cs);
2289 return DDERR_INVALIDCAPS;
2291 /* Check cube maps but only if the size includes them */
2292 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2294 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2295 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2297 WARN("Cube map faces requested without cube map flag\n");
2298 LeaveCriticalSection(&ddraw_cs);
2299 return DDERR_INVALIDCAPS;
2301 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2302 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2304 WARN("Cube map without faces requested\n");
2305 LeaveCriticalSection(&ddraw_cs);
2306 return DDERR_INVALIDPARAMS;
2309 /* Quick tests confirm those can be created, but we don't do that yet */
2310 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2311 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2313 FIXME("Partial cube maps not supported yet\n");
2317 /* According to the msdn this flag is ignored by CreateSurface */
2318 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2319 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2321 /* Modify some flags */
2322 memset(&desc2, 0, sizeof(desc2));
2323 desc2.dwSize = sizeof(desc2); /* For the struct copy */
2324 DD_STRUCT_COPY_BYSIZE(&desc2, DDSD);
2325 desc2.dwSize = sizeof(desc2); /* To override a possibly smaller size */
2326 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2328 /* Get the video mode from WineD3D - we will need it */
2329 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
2330 0, /* Swapchain 0 */
2331 &Mode);
2332 if(FAILED(hr))
2334 ERR("Failed to read display mode from wined3d\n");
2335 switch(This->orig_bpp)
2337 case 8:
2338 Mode.Format = WINED3DFMT_P8;
2339 break;
2341 case 15:
2342 Mode.Format = WINED3DFMT_X1R5G5B5;
2343 break;
2345 case 16:
2346 Mode.Format = WINED3DFMT_R5G6B5;
2347 break;
2349 case 24:
2350 Mode.Format = WINED3DFMT_R8G8B8;
2351 break;
2353 case 32:
2354 Mode.Format = WINED3DFMT_X8R8G8B8;
2355 break;
2357 Mode.Width = This->orig_width;
2358 Mode.Height = This->orig_height;
2361 /* No pixelformat given? Use the current screen format */
2362 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2364 desc2.dwFlags |= DDSD_PIXELFORMAT;
2365 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2367 /* Wait: It could be a Z buffer */
2368 if(desc2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
2370 switch(desc2.u2.dwMipMapCount) /* Who had this glorious idea? */
2372 case 15:
2373 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D15S1);
2374 break;
2375 case 16:
2376 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D16);
2377 break;
2378 case 24:
2379 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D24X8);
2380 break;
2381 case 32:
2382 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D32);
2383 break;
2384 default:
2385 ERR("Unknown Z buffer bit depth\n");
2388 else
2390 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
2394 /* No Width or no Height? Use the original screen size
2396 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2397 !(desc2.dwFlags & DDSD_HEIGHT) )
2399 /* Invalid for non-render targets */
2400 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2402 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2403 *Surf = NULL;
2404 LeaveCriticalSection(&ddraw_cs);
2405 return DDERR_INVALIDPARAMS;
2408 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2409 desc2.dwWidth = Mode.Width;
2410 desc2.dwHeight = Mode.Height;
2413 /* Mipmap count fixes */
2414 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2416 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2418 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2420 /* Mipmap count is given, should not be 0 */
2421 if( desc2.u2.dwMipMapCount == 0 )
2423 LeaveCriticalSection(&ddraw_cs);
2424 return DDERR_INVALIDPARAMS;
2427 else
2429 /* Undocumented feature: Create sublevels until
2430 * either the width or the height is 1
2432 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2433 desc2.dwWidth : desc2.dwHeight;
2434 desc2.u2.dwMipMapCount = 0;
2435 while( min )
2437 desc2.u2.dwMipMapCount += 1;
2438 min >>= 1;
2442 else
2444 /* Not-complex mipmap -> Mipmapcount = 1 */
2445 desc2.u2.dwMipMapCount = 1;
2447 extra_surfaces = desc2.u2.dwMipMapCount - 1;
2449 /* There's a mipmap count in the created surface in any case */
2450 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2452 /* If no mipmap is given, the texture has only one level */
2454 /* The first surface is a front buffer, the back buffer is created afterwards */
2455 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2457 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2460 /* The root surface in a cube map is positive x */
2461 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2463 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2464 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2467 /* Create the first surface */
2468 hr = IDirectDrawImpl_CreateNewSurface(This, &desc2, &object, 0);
2469 if( hr != DD_OK)
2471 ERR("IDirectDrawImpl_CreateNewSurface failed with %08x\n", hr);
2472 LeaveCriticalSection(&ddraw_cs);
2473 return hr;
2475 object->is_complex_root = TRUE;
2477 *Surf = ICOM_INTERFACE(object, IDirectDrawSurface7);
2479 /* Create Additional surfaces if necessary
2480 * This applies to Primary surfaces which have a back buffer count
2481 * set, but not to mipmap textures. In case of Mipmap textures,
2482 * wineD3D takes care of the creation of additional surfaces
2484 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2486 extra_surfaces = DDSD->dwBackBufferCount;
2487 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2488 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2491 hr = DD_OK;
2492 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2494 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2495 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
2496 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2497 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
2498 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
2499 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2500 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
2501 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
2502 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2503 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
2504 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
2505 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2506 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
2507 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
2508 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2509 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
2510 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2513 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces, desc2, FALSE);
2514 if(hr != DD_OK)
2516 /* This destroys and possibly created surfaces too */
2517 IDirectDrawSurface_Release( ICOM_INTERFACE(object, IDirectDrawSurface7) );
2518 LeaveCriticalSection(&ddraw_cs);
2519 return hr;
2522 /* If the implementation is OpenGL and there's no d3ddevice, attach a d3ddevice
2523 * But attach the d3ddevice only if the currently created surface was
2524 * a primary surface (2D app in 3D mode) or a 3DDEVICE surface (3D app)
2525 * The only case I can think of where this doesn't apply is when a
2526 * 2D app was configured by the user to run with OpenGL and it didn't create
2527 * the render target as first surface. In this case the render target creation
2528 * will cause the 3D init.
2530 if( (This->ImplType == SURFACE_OPENGL) && !(This->d3d_initialized) &&
2531 desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE) )
2533 IDirectDrawSurfaceImpl *target = object, *surface;
2534 struct list *entry;
2536 /* Search for the primary to use as render target */
2537 LIST_FOR_EACH(entry, &This->surface_list)
2539 surface = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2540 if((surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER)) ==
2541 (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER))
2543 /* found */
2544 target = surface;
2545 TRACE("Using primary %p as render target\n", target);
2546 break;
2550 TRACE("(%p) Attaching a D3DDevice, rendertarget = %p\n", This, target);
2551 hr = IDirectDrawImpl_AttachD3DDevice(This, target);
2552 if(hr != D3D_OK)
2554 IDirectDrawSurfaceImpl *release_surf;
2555 ERR("IDirectDrawImpl_AttachD3DDevice failed, hr = %x\n", hr);
2556 *Surf = NULL;
2558 /* The before created surface structures are in an incomplete state here.
2559 * WineD3D holds the reference on the IParents, and it released them on the failure
2560 * already. So the regular release method implementation would fail on the attempt
2561 * to destroy either the IParents or the swapchain. So free the surface here.
2562 * The surface structure here is a list, not a tree, because onscreen targets
2563 * cannot be cube textures
2565 while(object)
2567 release_surf = object;
2568 object = object->complex_array[0];
2569 IDirectDrawSurfaceImpl_Destroy(release_surf);
2571 LeaveCriticalSection(&ddraw_cs);
2572 return hr;
2576 /* Addref the ddraw interface to keep an reference for each surface */
2577 IDirectDraw7_AddRef(iface);
2578 object->ifaceToRelease = (IUnknown *) iface;
2580 /* Create a WineD3DTexture if a texture was requested */
2581 if(desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
2583 UINT levels;
2584 WINED3DFORMAT Format;
2585 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
2587 This->tex_root = object;
2589 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2591 /* a mipmap is created, create enough levels */
2592 levels = desc2.u2.dwMipMapCount;
2594 else
2596 /* No mipmap is created, create one level */
2597 levels = 1;
2600 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM */
2601 if(DDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
2603 Pool = WINED3DPOOL_SYSTEMMEM;
2605 /* Should I forward the MANEGED cap to the managed pool ? */
2607 /* Get the format. It's set already by CreateNewSurface */
2608 Format = PixelFormat_DD2WineD3D(&object->surface_desc.u4.ddpfPixelFormat);
2610 /* The surfaces are already created, the callback only
2611 * passes the IWineD3DSurface to WineD3D
2613 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2615 hr = IWineD3DDevice_CreateCubeTexture(This->wineD3DDevice,
2616 DDSD->dwWidth, /* Edgelength */
2617 levels,
2618 0, /* usage */
2619 Format,
2620 Pool,
2621 (IWineD3DCubeTexture **) &object->wineD3DTexture,
2622 0, /* SharedHandle */
2623 (IUnknown *) ICOM_INTERFACE(object, IDirectDrawSurface7),
2624 D3D7CB_CreateSurface);
2626 else
2628 hr = IWineD3DDevice_CreateTexture(This->wineD3DDevice,
2629 DDSD->dwWidth, DDSD->dwHeight,
2630 levels, /* MipMapCount = Levels */
2631 0, /* usage */
2632 Format,
2633 Pool,
2634 (IWineD3DTexture **) &object->wineD3DTexture,
2635 0, /* SharedHandle */
2636 (IUnknown *) ICOM_INTERFACE(object, IDirectDrawSurface7),
2637 D3D7CB_CreateSurface );
2639 This->tex_root = NULL;
2642 LeaveCriticalSection(&ddraw_cs);
2643 return hr;
2646 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
2647 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
2649 static BOOL
2650 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
2651 const DDPIXELFORMAT *provided)
2653 /* Some flags must be present in both or neither for a match. */
2654 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
2655 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
2656 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
2658 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2659 return FALSE;
2661 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
2662 return FALSE;
2664 if (requested->dwFlags & DDPF_FOURCC)
2665 if (requested->dwFourCC != provided->dwFourCC)
2666 return FALSE;
2668 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
2669 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2670 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
2671 return FALSE;
2673 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2674 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2675 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
2676 return FALSE;
2678 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
2679 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
2680 return FALSE;
2682 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
2683 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2684 |DDPF_BUMPDUDV))
2685 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
2686 return FALSE;
2688 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
2689 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
2690 return FALSE;
2692 return TRUE;
2695 static BOOL
2696 IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested,
2697 const DDSURFACEDESC2* provided)
2699 struct compare_info
2701 DWORD flag;
2702 ptrdiff_t offset;
2703 size_t size;
2706 #define CMP(FLAG, FIELD) \
2707 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
2708 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
2710 static const struct compare_info compare[] =
2712 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
2713 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
2714 CMP(CAPS, ddsCaps),
2715 CMP(CKDESTBLT, ddckCKDestBlt),
2716 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
2717 CMP(CKSRCBLT, ddckCKSrcBlt),
2718 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
2719 CMP(HEIGHT, dwHeight),
2720 CMP(LINEARSIZE, u1 /* dwLinearSize */),
2721 CMP(LPSURFACE, lpSurface),
2722 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
2723 CMP(PITCH, u1 /* lPitch */),
2724 /* PIXELFORMAT: manual */
2725 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
2726 CMP(TEXTURESTAGE, dwTextureStage),
2727 CMP(WIDTH, dwWidth),
2728 /* ZBUFFERBITDEPTH: "obsolete" */
2731 #undef CMP
2733 unsigned int i;
2735 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2736 return FALSE;
2738 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
2740 if (requested->dwFlags & compare[i].flag
2741 && memcmp((const char *)provided + compare[i].offset,
2742 (const char *)requested + compare[i].offset,
2743 compare[i].size) != 0)
2744 return FALSE;
2747 if (requested->dwFlags & DDSD_PIXELFORMAT)
2749 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
2750 &provided->u4.ddpfPixelFormat))
2751 return FALSE;
2754 return TRUE;
2757 #undef DDENUMSURFACES_SEARCHTYPE
2758 #undef DDENUMSURFACES_MATCHTYPE
2760 /*****************************************************************************
2761 * IDirectDraw7::EnumSurfaces
2763 * Loops through all surfaces attached to this device and calls the
2764 * application callback. This can't be relayed to WineD3DDevice,
2765 * because some WineD3DSurfaces' parents are IParent objects
2767 * Params:
2768 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
2769 * DDSD: Description to filter for
2770 * Context: Application-provided pointer, it's passed unmodified to the
2771 * Callback function
2772 * Callback: Address to call for each surface
2774 * Returns:
2775 * DDERR_INVALIDPARAMS if the callback is NULL
2776 * DD_OK on success
2778 *****************************************************************************/
2779 static HRESULT WINAPI
2780 IDirectDrawImpl_EnumSurfaces(IDirectDraw7 *iface,
2781 DWORD Flags,
2782 DDSURFACEDESC2 *DDSD,
2783 void *Context,
2784 LPDDENUMSURFACESCALLBACK7 Callback)
2786 /* The surface enumeration is handled by WineDDraw,
2787 * because it keeps track of all surfaces attached to
2788 * it. The filtering is done by our callback function,
2789 * because WineDDraw doesn't handle ddraw-like surface
2790 * caps structures
2792 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2793 IDirectDrawSurfaceImpl *surf;
2794 BOOL all, nomatch;
2795 DDSURFACEDESC2 desc;
2796 struct list *entry, *entry2;
2798 all = Flags & DDENUMSURFACES_ALL;
2799 nomatch = Flags & DDENUMSURFACES_NOMATCH;
2801 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, DDSD, Context, Callback);
2802 EnterCriticalSection(&ddraw_cs);
2804 if(!Callback)
2806 LeaveCriticalSection(&ddraw_cs);
2807 return DDERR_INVALIDPARAMS;
2810 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
2811 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
2813 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2814 if (all || (nomatch != IDirectDrawImpl_DDSD_Match(DDSD, &surf->surface_desc)))
2816 desc = surf->surface_desc;
2817 IDirectDrawSurface7_AddRef(ICOM_INTERFACE(surf, IDirectDrawSurface7));
2818 if(Callback( ICOM_INTERFACE(surf, IDirectDrawSurface7), &desc, Context) != DDENUMRET_OK)
2820 LeaveCriticalSection(&ddraw_cs);
2821 return DD_OK;
2825 LeaveCriticalSection(&ddraw_cs);
2826 return DD_OK;
2829 static HRESULT WINAPI
2830 findRenderTarget(IDirectDrawSurface7 *surface,
2831 DDSURFACEDESC2 *desc,
2832 void *ctx)
2834 IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, surface);
2835 IDirectDrawSurfaceImpl **target = (IDirectDrawSurfaceImpl **) ctx;
2837 if(!surf->isRenderTarget) {
2838 *target = surf;
2839 IDirectDrawSurface7_Release(surface);
2840 return DDENUMRET_CANCEL;
2843 /* Recurse into the surface tree */
2844 IDirectDrawSurface7_EnumAttachedSurfaces(surface, ctx, findRenderTarget);
2846 IDirectDrawSurface7_Release(surface);
2847 if(*target) return DDENUMRET_CANCEL;
2848 else return DDENUMRET_OK; /* Continue with the next neighbor surface */
2851 /*****************************************************************************
2852 * D3D7CB_CreateRenderTarget
2854 * Callback called by WineD3D to create Surfaces for render target usage
2855 * This function takes the D3D target from the IDirectDrawImpl structure,
2856 * and returns the WineD3DSurface. To avoid double usage, the surface
2857 * is marked as render target afterwards
2859 * Params
2860 * device: The WineD3DDevice's parent
2861 * Width, Height, Format: Dimensions and pixelformat of the render target
2862 * Ignored, because the surface already exists
2863 * MultiSample, MultisampleQuality, Lockable: Ignored for the same reason
2864 * Lockable: ignored
2865 * ppSurface: Address to pass the surface pointer back at
2866 * pSharedHandle: Ignored
2868 * Returns:
2869 * Always returns D3D_OK
2871 *****************************************************************************/
2872 static HRESULT WINAPI
2873 D3D7CB_CreateRenderTarget(IUnknown *device, IUnknown *pSuperior,
2874 UINT Width, UINT Height,
2875 WINED3DFORMAT Format,
2876 WINED3DMULTISAMPLE_TYPE MultiSample,
2877 DWORD MultisampleQuality,
2878 BOOL Lockable,
2879 IWineD3DSurface** ppSurface,
2880 HANDLE* pSharedHandle)
2882 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
2883 IDirectDrawSurfaceImpl *d3dSurface = This->d3d_target, *target = NULL;
2884 TRACE("(%p) call back\n", device);
2886 if(d3dSurface->isRenderTarget)
2888 IDirectDrawSurface7_EnumAttachedSurfaces(ICOM_INTERFACE(d3dSurface, IDirectDrawSurface7),
2889 &target, findRenderTarget);
2891 else
2893 target = d3dSurface;
2896 if(!target)
2898 target = This->d3d_target;
2899 ERR(" (%p) : No DirectDrawSurface found to create the back buffer. Using the front buffer as back buffer. Uncertain consequences\n", This);
2902 /* TODO: Return failure if the dimensions do not match, but this shouldn't happen */
2904 *ppSurface = target->WineD3DSurface;
2905 target->isRenderTarget = TRUE;
2906 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *ppSurface, d3dSurface);
2907 return D3D_OK;
2910 static HRESULT WINAPI
2911 D3D7CB_CreateDepthStencilSurface(IUnknown *device,
2912 IUnknown *pSuperior,
2913 UINT Width,
2914 UINT Height,
2915 WINED3DFORMAT Format,
2916 WINED3DMULTISAMPLE_TYPE MultiSample,
2917 DWORD MultisampleQuality,
2918 BOOL Discard,
2919 IWineD3DSurface** ppSurface,
2920 HANDLE* pSharedHandle)
2922 /* Create a Depth Stencil surface to make WineD3D happy */
2923 HRESULT hr = D3D_OK;
2924 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
2925 DDSURFACEDESC2 ddsd;
2927 TRACE("(%p) call back\n", device);
2929 *ppSurface = NULL;
2931 /* Create a DirectDraw surface */
2932 memset(&ddsd, 0, sizeof(ddsd));
2933 ddsd.dwSize = sizeof(ddsd);
2934 ddsd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2935 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
2936 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
2937 ddsd.dwHeight = Height;
2938 ddsd.dwWidth = Width;
2939 if(Format != 0)
2941 PixelFormat_WineD3DtoDD(&ddsd.u4.ddpfPixelFormat, Format);
2943 else
2945 ddsd.dwFlags ^= DDSD_PIXELFORMAT;
2948 This->depthstencil = TRUE;
2949 hr = IDirectDraw7_CreateSurface((IDirectDraw7 *) This,
2950 &ddsd,
2951 (IDirectDrawSurface7 **) &This->DepthStencilBuffer,
2952 NULL);
2953 This->depthstencil = FALSE;
2954 if(FAILED(hr))
2956 ERR(" (%p) Creating a DepthStencil Surface failed, result = %x\n", This, hr);
2957 return hr;
2959 *ppSurface = This->DepthStencilBuffer->WineD3DSurface;
2960 return D3D_OK;
2963 /*****************************************************************************
2964 * D3D7CB_CreateAdditionalSwapChain
2966 * Callback function for WineD3D which creates a new WineD3DSwapchain
2967 * interface. It also creates an IParent interface to store that pointer,
2968 * so the WineD3DSwapchain has a parent and can be released when the D3D
2969 * device is destroyed
2970 *****************************************************************************/
2971 static HRESULT WINAPI
2972 D3D7CB_CreateAdditionalSwapChain(IUnknown *device,
2973 WINED3DPRESENT_PARAMETERS* pPresentationParameters,
2974 IWineD3DSwapChain ** ppSwapChain)
2976 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
2977 IParentImpl *object = NULL;
2978 HRESULT res = D3D_OK;
2979 IWineD3DSwapChain *swapchain;
2980 TRACE("(%p) call back\n", device);
2982 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IParentImpl));
2983 if (NULL == object)
2985 FIXME("Allocation of memory failed\n");
2986 *ppSwapChain = NULL;
2987 return DDERR_OUTOFVIDEOMEMORY;
2990 ICOM_INIT_INTERFACE(object, IParent, IParent_Vtbl);
2991 object->ref = 1;
2993 res = IWineD3DDevice_CreateAdditionalSwapChain(This->wineD3DDevice,
2994 pPresentationParameters,
2995 &swapchain,
2996 (IUnknown*) ICOM_INTERFACE(object, IParent),
2997 D3D7CB_CreateRenderTarget,
2998 D3D7CB_CreateDepthStencilSurface);
2999 if (res != D3D_OK)
3001 FIXME("(%p) call to IWineD3DDevice_CreateAdditionalSwapChain failed\n", This);
3002 HeapFree(GetProcessHeap(), 0 , object);
3003 *ppSwapChain = NULL;
3005 else
3007 *ppSwapChain = swapchain;
3008 object->child = (IUnknown *) swapchain;
3011 return res;
3014 /*****************************************************************************
3015 * IDirectDrawImpl_AttachD3DDevice
3017 * Initializes the D3D capabilities of WineD3D
3019 * Params:
3020 * primary: The primary surface for D3D
3022 * Returns
3023 * DD_OK on success,
3024 * DDERR_* otherwise
3026 *****************************************************************************/
3027 static HRESULT WINAPI
3028 IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This,
3029 IDirectDrawSurfaceImpl *primary)
3031 HRESULT hr;
3032 HWND window;
3034 WINED3DPRESENT_PARAMETERS localParameters;
3036 TRACE("(%p)->(%p)\n", This, primary);
3038 /* Get the window */
3039 hr = IWineD3DDevice_GetHWND(This->wineD3DDevice,
3040 &window);
3041 if(hr != D3D_OK)
3043 ERR("IWineD3DDevice::GetHWND failed\n");
3044 return hr;
3047 /* If there's no window, create a hidden window. WineD3D needs it */
3048 if(window == 0)
3050 window = CreateWindowExA(0, This->classname, "Hidden D3D Window",
3051 WS_DISABLED, 0, 0,
3052 GetSystemMetrics(SM_CXSCREEN),
3053 GetSystemMetrics(SM_CYSCREEN),
3054 NULL, NULL, GetModuleHandleA(0), NULL);
3056 ShowWindow(window, SW_HIDE); /* Just to be sure */
3057 WARN("(%p) No window for the Direct3DDevice, created a hidden window. HWND=%p\n", This, window);
3058 This->d3d_window = window;
3060 else
3062 TRACE("(%p) Using existing window %p for Direct3D rendering\n", This, window);
3065 /* Store the future Render Target surface */
3066 This->d3d_target = primary;
3068 /* Use the surface description for the device parameters, not the
3069 * Device settings. The app might render to an offscreen surface
3071 localParameters.BackBufferWidth = primary->surface_desc.dwWidth;
3072 localParameters.BackBufferHeight = primary->surface_desc.dwHeight;
3073 localParameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
3074 localParameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
3075 localParameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
3076 localParameters.MultiSampleQuality = 0;
3077 localParameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
3078 localParameters.hDeviceWindow = window;
3079 localParameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
3080 localParameters.EnableAutoDepthStencil = FALSE;
3081 localParameters.AutoDepthStencilFormat = WINED3DFMT_D16;
3082 localParameters.Flags = 0;
3083 localParameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
3084 localParameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
3086 TRACE("Passing mode %d\n", localParameters.BackBufferFormat);
3088 /* Set this NOW, otherwise creating the depth stencil surface will cause a
3089 * recursive loop until ram or emulated video memory is full
3091 This->d3d_initialized = TRUE;
3093 hr = IWineD3DDevice_Init3D(This->wineD3DDevice,
3094 &localParameters,
3095 D3D7CB_CreateAdditionalSwapChain);
3096 if(FAILED(hr))
3098 This->d3d_target = NULL;
3099 This->d3d_initialized = FALSE;
3100 return hr;
3103 This->declArraySize = 2;
3104 This->decls = HeapAlloc(GetProcessHeap(),
3105 HEAP_ZERO_MEMORY,
3106 sizeof(*This->decls) * This->declArraySize);
3107 if(!This->decls)
3109 ERR("Error allocating an array for the converted vertex decls\n");
3110 This->declArraySize = 0;
3111 hr = IWineD3DDevice_Uninit3D(This->wineD3DDevice,
3112 D3D7CB_DestroyDepthStencilSurface,
3113 D3D7CB_DestroySwapChain);
3114 return E_OUTOFMEMORY;
3117 /* Create an Index Buffer parent */
3118 TRACE("(%p) Successfully initialized 3D\n", This);
3119 return DD_OK;
3122 /*****************************************************************************
3123 * DirectDrawCreateClipper (DDRAW.@)
3125 * Creates a new IDirectDrawClipper object.
3127 * Params:
3128 * Clipper: Address to write the interface pointer to
3129 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3130 * NULL
3132 * Returns:
3133 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3134 * E_OUTOFMEMORY if allocating the object failed
3136 *****************************************************************************/
3137 HRESULT WINAPI
3138 DirectDrawCreateClipper(DWORD Flags,
3139 LPDIRECTDRAWCLIPPER *Clipper,
3140 IUnknown *UnkOuter)
3142 IDirectDrawClipperImpl* object;
3143 TRACE("(%08x,%p,%p)\n", Flags, Clipper, UnkOuter);
3145 EnterCriticalSection(&ddraw_cs);
3146 if (UnkOuter != NULL)
3148 LeaveCriticalSection(&ddraw_cs);
3149 return CLASS_E_NOAGGREGATION;
3152 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3153 sizeof(IDirectDrawClipperImpl));
3154 if (object == NULL)
3156 LeaveCriticalSection(&ddraw_cs);
3157 return E_OUTOFMEMORY;
3160 ICOM_INIT_INTERFACE(object, IDirectDrawClipper, IDirectDrawClipper_Vtbl);
3161 object->ref = 1;
3162 object->wineD3DClipper = pWineDirect3DCreateClipper((IUnknown *) object);
3163 if(!object->wineD3DClipper)
3165 HeapFree(GetProcessHeap(), 0, object);
3166 LeaveCriticalSection(&ddraw_cs);
3167 return E_OUTOFMEMORY;
3170 *Clipper = (IDirectDrawClipper *) object;
3171 LeaveCriticalSection(&ddraw_cs);
3172 return DD_OK;
3175 /*****************************************************************************
3176 * IDirectDraw7::CreateClipper
3178 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3180 *****************************************************************************/
3181 static HRESULT WINAPI
3182 IDirectDrawImpl_CreateClipper(IDirectDraw7 *iface,
3183 DWORD Flags,
3184 IDirectDrawClipper **Clipper,
3185 IUnknown *UnkOuter)
3187 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3188 TRACE("(%p)->(%x,%p,%p)\n", This, Flags, Clipper, UnkOuter);
3189 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3192 /*****************************************************************************
3193 * IDirectDraw7::CreatePalette
3195 * Creates a new IDirectDrawPalette object
3197 * Params:
3198 * Flags: The flags for the new clipper
3199 * ColorTable: Color table to assign to the new clipper
3200 * Palette: Address to write the interface pointer to
3201 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3202 * NULL
3204 * Returns:
3205 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3206 * E_OUTOFMEMORY if allocating the object failed
3208 *****************************************************************************/
3209 static HRESULT WINAPI
3210 IDirectDrawImpl_CreatePalette(IDirectDraw7 *iface,
3211 DWORD Flags,
3212 PALETTEENTRY *ColorTable,
3213 IDirectDrawPalette **Palette,
3214 IUnknown *pUnkOuter)
3216 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3217 IDirectDrawPaletteImpl *object;
3218 HRESULT hr = DDERR_GENERIC;
3219 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, ColorTable, Palette, pUnkOuter);
3221 EnterCriticalSection(&ddraw_cs);
3222 if(pUnkOuter != NULL)
3224 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
3225 LeaveCriticalSection(&ddraw_cs);
3226 return CLASS_E_NOAGGREGATION;
3229 /* The refcount test shows that a cooplevel is required for this */
3230 if(!This->cooperative_level)
3232 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3233 LeaveCriticalSection(&ddraw_cs);
3234 return DDERR_NOCOOPERATIVELEVELSET;
3237 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
3238 if(!object)
3240 ERR("Out of memory when allocating memory for a palette implementation\n");
3241 LeaveCriticalSection(&ddraw_cs);
3242 return E_OUTOFMEMORY;
3245 ICOM_INIT_INTERFACE(object, IDirectDrawPalette, IDirectDrawPalette_Vtbl);
3246 object->ref = 1;
3247 object->ddraw_owner = This;
3249 hr = IWineD3DDevice_CreatePalette(This->wineD3DDevice, Flags, ColorTable, &object->wineD3DPalette, (IUnknown *) ICOM_INTERFACE(object, IDirectDrawPalette) );
3250 if(hr != DD_OK)
3252 HeapFree(GetProcessHeap(), 0, object);
3253 LeaveCriticalSection(&ddraw_cs);
3254 return hr;
3257 IDirectDraw7_AddRef(iface);
3258 object->ifaceToRelease = (IUnknown *) iface;
3259 *Palette = ICOM_INTERFACE(object, IDirectDrawPalette);
3260 LeaveCriticalSection(&ddraw_cs);
3261 return DD_OK;
3264 /*****************************************************************************
3265 * IDirectDraw7::DuplicateSurface
3267 * Duplicates a surface. The surface memory points to the same memory as
3268 * the original surface, and it's released when the last surface referencing
3269 * it is released. I guess that's beyond Wine's surface management right now
3270 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3271 * test application to implement this)
3273 * Params:
3274 * Src: Address of the source surface
3275 * Dest: Address to write the new surface pointer to
3277 * Returns:
3278 * See IDirectDraw7::CreateSurface
3280 *****************************************************************************/
3281 static HRESULT WINAPI
3282 IDirectDrawImpl_DuplicateSurface(IDirectDraw7 *iface,
3283 IDirectDrawSurface7 *Src,
3284 IDirectDrawSurface7 **Dest)
3286 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3287 IDirectDrawSurfaceImpl *Surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Src);
3289 FIXME("(%p)->(%p,%p)\n", This, Surf, Dest);
3291 /* For now, simply create a new, independent surface */
3292 return IDirectDraw7_CreateSurface(iface,
3293 &Surf->surface_desc,
3294 Dest,
3295 NULL);
3298 /*****************************************************************************
3299 * IDirectDraw7 VTable
3300 *****************************************************************************/
3301 const IDirectDraw7Vtbl IDirectDraw7_Vtbl =
3303 /*** IUnknown ***/
3304 IDirectDrawImpl_QueryInterface,
3305 IDirectDrawImpl_AddRef,
3306 IDirectDrawImpl_Release,
3307 /*** IDirectDraw ***/
3308 IDirectDrawImpl_Compact,
3309 IDirectDrawImpl_CreateClipper,
3310 IDirectDrawImpl_CreatePalette,
3311 IDirectDrawImpl_CreateSurface,
3312 IDirectDrawImpl_DuplicateSurface,
3313 IDirectDrawImpl_EnumDisplayModes,
3314 IDirectDrawImpl_EnumSurfaces,
3315 IDirectDrawImpl_FlipToGDISurface,
3316 IDirectDrawImpl_GetCaps,
3317 IDirectDrawImpl_GetDisplayMode,
3318 IDirectDrawImpl_GetFourCCCodes,
3319 IDirectDrawImpl_GetGDISurface,
3320 IDirectDrawImpl_GetMonitorFrequency,
3321 IDirectDrawImpl_GetScanLine,
3322 IDirectDrawImpl_GetVerticalBlankStatus,
3323 IDirectDrawImpl_Initialize,
3324 IDirectDrawImpl_RestoreDisplayMode,
3325 IDirectDrawImpl_SetCooperativeLevel,
3326 IDirectDrawImpl_SetDisplayMode,
3327 IDirectDrawImpl_WaitForVerticalBlank,
3328 /*** IDirectDraw2 ***/
3329 IDirectDrawImpl_GetAvailableVidMem,
3330 /*** IDirectDraw7 ***/
3331 IDirectDrawImpl_GetSurfaceFromDC,
3332 IDirectDrawImpl_RestoreAllSurfaces,
3333 IDirectDrawImpl_TestCooperativeLevel,
3334 IDirectDrawImpl_GetDeviceIdentifier,
3335 /*** IDirectDraw7 ***/
3336 IDirectDrawImpl_StartModeTest,
3337 IDirectDrawImpl_EvaluateMode
3340 /*****************************************************************************
3341 * IDirectDrawImpl_FindDecl
3343 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
3344 * if none was found.
3346 * This function is in ddraw.c and the DDraw object space because D3D7
3347 * vertex buffers are created using the IDirect3D interface to the ddraw
3348 * object, so they can be valid across D3D devices(theoretically. The ddraw
3349 * object also owns the wined3d device
3351 * Parameters:
3352 * This: Device
3353 * fvf: Fvf to find the decl for
3355 * Returns:
3356 * NULL in case of an error, the IWineD3DVertexDeclaration interface for the
3357 * fvf otherwise.
3359 *****************************************************************************/
3360 IWineD3DVertexDeclaration *
3361 IDirectDrawImpl_FindDecl(IDirectDrawImpl *This,
3362 DWORD fvf)
3364 HRESULT hr;
3365 IWineD3DVertexDeclaration* pDecl = NULL;
3366 int p, low, high; /* deliberately signed */
3367 struct FvfToDecl *convertedDecls = This->decls;
3369 TRACE("Searching for declaration for fvf %08x... ", fvf);
3371 low = 0;
3372 high = This->numConvertedDecls - 1;
3373 while(low <= high) {
3374 p = (low + high) >> 1;
3375 TRACE("%d ", p);
3376 if(convertedDecls[p].fvf == fvf) {
3377 TRACE("found %p\n", convertedDecls[p].decl);
3378 return convertedDecls[p].decl;
3379 } else if(convertedDecls[p].fvf < fvf) {
3380 low = p + 1;
3381 } else {
3382 high = p - 1;
3385 TRACE("not found. Creating and inserting at position %d.\n", low);
3387 hr = IWineD3DDevice_CreateVertexDeclarationFromFVF(This->wineD3DDevice,
3388 &pDecl,
3389 (IUnknown *) ICOM_INTERFACE(This, IDirectDraw7),
3390 fvf);
3391 if (hr != S_OK) return NULL;
3393 if(This->declArraySize == This->numConvertedDecls) {
3394 int grow = max(This->declArraySize / 2, 8);
3395 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
3396 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
3397 if(!convertedDecls) {
3398 /* This will destroy it */
3399 IWineD3DVertexDeclaration_Release(pDecl);
3400 return NULL;
3402 This->decls = convertedDecls;
3403 This->declArraySize += grow;
3406 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
3407 convertedDecls[low].decl = pDecl;
3408 convertedDecls[low].fvf = fvf;
3409 This->numConvertedDecls++;
3411 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
3412 return pDecl;