2 * Copyright 1997-2000 Marcus Meissner
3 * Copyright 1998-2000 Lionel Ulmer
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2006 Stefan Dösinger
6 * Copyright 2008 Denver Gingerich
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
32 #define NONAMELESSUNION
38 #include "wine/exception.h"
43 #include "ddraw_private.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
48 static BOOL IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested, const DDSURFACEDESC2* provided);
49 static HRESULT WINAPI IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This, IDirectDrawSurfaceImpl *primary);
50 static HRESULT WINAPI IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This, DDSURFACEDESC2 *pDDSD, IDirectDrawSurfaceImpl **ppSurf, UINT level);
51 static HRESULT WINAPI IDirectDrawImpl_CreateGDISwapChain(IDirectDrawImpl *This, IDirectDrawSurfaceImpl *primary);
53 /* Device identifier. Don't relay it to WineD3D */
54 static const DDDEVICEIDENTIFIER2 deviceidentifier =
58 { { 0x00010001, 0x00010001 } },
60 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
61 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
65 /*****************************************************************************
67 *****************************************************************************/
69 /*****************************************************************************
70 * IDirectDraw7::QueryInterface
72 * Queries different interfaces of the DirectDraw object. It can return
73 * IDirectDraw interfaces in version 1, 2, 4 and 7, and IDirect3D interfaces
74 * in version 1, 2, 3 and 7. An IDirect3DDevice can be created with this
76 * The returned interface is AddRef()-ed before it's returned
78 * Used for version 1, 2, 4 and 7
81 * refiid: Interface ID asked for
82 * obj: Used to return the interface pointer
85 * S_OK if an interface was found
86 * E_NOINTERFACE if the requested interface wasn't found
88 *****************************************************************************/
90 IDirectDrawImpl_QueryInterface(IDirectDraw7 *iface,
94 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
96 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(refiid), obj);
98 /* Can change surface impl type */
99 EnterCriticalSection(&ddraw_cs);
101 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
106 LeaveCriticalSection(&ddraw_cs);
107 return DDERR_INVALIDPARAMS;
110 /* Check DirectDraw Interfaces */
111 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
112 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
114 *obj = ICOM_INTERFACE(This, IDirectDraw7);
115 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
117 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
119 *obj = ICOM_INTERFACE(This, IDirectDraw4);
120 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
122 else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
124 *obj = ICOM_INTERFACE(This, IDirectDraw3);
125 TRACE("(%p) Returning IDirectDraw3 interface at %p\n", This, *obj);
127 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
129 *obj = ICOM_INTERFACE(This, IDirectDraw2);
130 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
132 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
134 *obj = ICOM_INTERFACE(This, IDirectDraw);
135 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
139 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
140 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
141 * who had this idea and why. The older interfaces can query and IDirect3D version
142 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
143 * and messy to implement with the common creation function, so it has been left out here.
145 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
146 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
147 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
148 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
150 /* Check the surface implementation */
151 if(This->ImplType == SURFACE_UNKNOWN)
153 /* Apps may create the IDirect3D Interface before the primary surface.
154 * set the surface implementation */
155 This->ImplType = SURFACE_OPENGL;
156 TRACE("(%p) Choosing OpenGL surfaces because a Direct3D interface was requested\n", This);
158 else if(This->ImplType != SURFACE_OPENGL && DefaultSurfaceType == SURFACE_UNKNOWN)
160 ERR("(%p) The App is requesting a D3D device, but a non-OpenGL surface type was choosen. Prepare for trouble!\n", This);
161 ERR(" (%p) You may want to contact wine-devel for help\n", This);
162 /* Should I assert(0) here??? */
164 else if(This->ImplType != SURFACE_OPENGL)
166 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
167 /* Do not abort here, only reject 3D Device creation */
170 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
172 This->d3dversion = 1;
173 *obj = ICOM_INTERFACE(This, IDirect3D);
174 TRACE(" returning Direct3D interface at %p.\n", *obj);
176 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
178 This->d3dversion = 2;
179 *obj = ICOM_INTERFACE(This, IDirect3D2);
180 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
182 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
184 This->d3dversion = 3;
185 *obj = ICOM_INTERFACE(This, IDirect3D3);
186 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
188 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
190 This->d3dversion = 7;
191 *obj = ICOM_INTERFACE(This, IDirect3D7);
192 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
196 /* Unknown interface */
199 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
200 LeaveCriticalSection(&ddraw_cs);
201 return E_NOINTERFACE;
204 IUnknown_AddRef( (IUnknown *) *obj );
205 LeaveCriticalSection(&ddraw_cs);
209 /*****************************************************************************
210 * IDirectDraw7::AddRef
212 * Increases the interfaces refcount, basically
214 * DDraw refcounting is a bit tricky. The different DirectDraw interface
215 * versions have individual refcounts, but the IDirect3D interfaces do not.
216 * All interfaces are from one object, that means calling QueryInterface on an
217 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
218 * IDirectDrawImpl object.
220 * That means all AddRef and Release implementations of IDirectDrawX work
221 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
222 * except of IDirect3D7 which thunks to IDirectDraw7
224 * Returns: The new refcount
226 *****************************************************************************/
228 IDirectDrawImpl_AddRef(IDirectDraw7 *iface)
230 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
231 ULONG ref = InterlockedIncrement(&This->ref7);
233 TRACE("(%p) : incrementing IDirectDraw7 refcount from %u.\n", This, ref -1);
235 if(ref == 1) InterlockedIncrement(&This->numIfaces);
240 /*****************************************************************************
241 * IDirectDrawImpl_Destroy
243 * Destroys a ddraw object if all refcounts are 0. This is to share code
244 * between the IDirectDrawX::Release functions
247 * This: DirectDraw object to destroy
249 *****************************************************************************/
251 IDirectDrawImpl_Destroy(IDirectDrawImpl *This)
253 /* Clear the cooplevel to restore window and display mode */
254 IDirectDraw7_SetCooperativeLevel(ICOM_INTERFACE(This, IDirectDraw7),
258 /* Destroy the device window if we created one */
259 if(This->devicewindow != 0)
261 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
262 DestroyWindow(This->devicewindow);
263 This->devicewindow = 0;
266 /* Unregister the window class */
267 UnregisterClassA(This->classname, 0);
269 EnterCriticalSection(&ddraw_cs);
270 list_remove(&This->ddraw_list_entry);
271 LeaveCriticalSection(&ddraw_cs);
273 /* Release the attached WineD3D stuff */
274 IWineD3DDevice_Release(This->wineD3DDevice);
275 IWineD3D_Release(This->wineD3D);
277 /* Now free the object */
278 HeapFree(GetProcessHeap(), 0, This);
281 /*****************************************************************************
282 * IDirectDraw7::Release
284 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
286 * Returns: The new refcount
287 *****************************************************************************/
289 IDirectDrawImpl_Release(IDirectDraw7 *iface)
291 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
292 ULONG ref = InterlockedDecrement(&This->ref7);
294 TRACE("(%p)->() decrementing IDirectDraw7 refcount from %u.\n", This, ref +1);
298 ULONG ifacecount = InterlockedDecrement(&This->numIfaces);
299 if(ifacecount == 0) IDirectDrawImpl_Destroy(This);
305 /*****************************************************************************
306 * IDirectDraw methods
307 *****************************************************************************/
309 /*****************************************************************************
310 * IDirectDraw7::SetCooperativeLevel
312 * Sets the cooperative level for the DirectDraw object, and the window
313 * assigned to it. The cooperative level determines the general behavior
314 * of the DirectDraw application
316 * Warning: This is quite tricky, as it's not really documented which
317 * cooperative levels can be combined with each other. If a game fails
318 * after this function, try to check the cooperative levels passed on
319 * Windows, and if it returns something different.
321 * If you think that this function caused the failure because it writes a
322 * fixme, be sure to run again with a +ddraw trace.
324 * What is known about cooperative levels (See the ddraw modes test):
325 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
326 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
327 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
328 * DDSCL_FULLSCREEN can be activated
329 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
331 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
332 * DDSCL_SETFOCUSWINDOW (partially),
333 * DDSCL_MULTITHREADED (work in progress)
335 * Unhandled flags, which should be implemented
336 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
337 * expect any difference to a normal window for wine)
338 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
339 * rendering (Possible test case: Half-life)
341 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
343 * These don't seem very important for wine:
344 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
347 * DD_OK if the cooperative level was set successfully
348 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
349 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
350 * (Probably others too, have to investigate)
352 *****************************************************************************/
353 static HRESULT WINAPI
354 IDirectDrawImpl_SetCooperativeLevel(IDirectDraw7 *iface,
358 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
361 TRACE("(%p)->(%p,%08x)\n",This,hwnd,cooplevel);
362 DDRAW_dump_cooperativelevel(cooplevel);
364 EnterCriticalSection(&ddraw_cs);
366 /* Get the old window */
367 window = This->dest_window;
369 /* Tests suggest that we need one of them: */
370 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
374 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
375 LeaveCriticalSection(&ddraw_cs);
376 return DDERR_INVALIDPARAMS;
379 /* Handle those levels first which set various hwnds */
380 if(cooplevel & DDSCL_SETFOCUSWINDOW)
382 /* This isn't compatible with a lot of flags */
383 if(cooplevel & ( DDSCL_MULTITHREADED |
388 DDSCL_SETDEVICEWINDOW |
393 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
394 LeaveCriticalSection(&ddraw_cs);
395 return DDERR_INVALIDPARAMS;
397 else if( (This->cooperative_level & DDSCL_FULLSCREEN) && window)
399 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET\n");
400 LeaveCriticalSection(&ddraw_cs);
401 return DDERR_HWNDALREADYSET;
404 This->focuswindow = hwnd;
405 /* Won't use the hwnd param for anything else */
408 /* Use the focus window for drawing too */
409 This->dest_window = This->focuswindow;
411 /* Destroy the device window, if we have one */
412 if(This->devicewindow)
414 DestroyWindow(This->devicewindow);
415 This->devicewindow = NULL;
418 /* DDSCL_NORMAL or DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE */
419 if(cooplevel & DDSCL_NORMAL)
421 /* Can't coexist with fullscreen or exclusive */
422 if(cooplevel & (DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE) )
424 TRACE("(%p) DDSCL_NORMAL is not compative with DDSCL_FULLSCREEN or DDSCL_EXCLUSIVE\n", This);
425 LeaveCriticalSection(&ddraw_cs);
426 return DDERR_INVALIDPARAMS;
429 /* Switching from fullscreen? */
430 if(This->cooperative_level & DDSCL_FULLSCREEN)
432 /* Restore the display mode */
433 IDirectDraw7_RestoreDisplayMode(iface);
435 This->cooperative_level &= ~DDSCL_FULLSCREEN;
436 This->cooperative_level &= ~DDSCL_EXCLUSIVE;
437 This->cooperative_level &= ~DDSCL_ALLOWMODEX;
440 /* Don't override focus windows or private device windows */
442 !(This->focuswindow) &&
443 !(This->devicewindow) &&
446 This->dest_window = hwnd;
449 else if(cooplevel & DDSCL_FULLSCREEN)
451 /* Needs DDSCL_EXCLUSIVE */
452 if(!(cooplevel & DDSCL_EXCLUSIVE) )
454 TRACE("(%p) DDSCL_FULLSCREEN needs DDSCL_EXCLUSIVE\n", This);
455 LeaveCriticalSection(&ddraw_cs);
456 return DDERR_INVALIDPARAMS;
461 TRACE("(%p) DDSCL_FULLSCREEN needs a HWND\n", This);
462 return DDERR_INVALIDPARAMS;
466 This->cooperative_level &= ~DDSCL_NORMAL;
468 /* Don't override focus windows or private device windows */
470 !(This->focuswindow) &&
471 !(This->devicewindow) &&
474 This->dest_window = hwnd;
477 else if(cooplevel & DDSCL_EXCLUSIVE)
479 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN\n", This);
480 LeaveCriticalSection(&ddraw_cs);
481 return DDERR_INVALIDPARAMS;
484 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
486 /* Don't create a device window if a focus window is set */
487 if( !(This->focuswindow) )
489 HWND devicewindow = CreateWindowExA(0, This->classname, "DDraw device window",
491 GetSystemMetrics(SM_CXSCREEN),
492 GetSystemMetrics(SM_CYSCREEN),
493 NULL, NULL, GetModuleHandleA(0), NULL);
495 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
496 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
498 This->devicewindow = devicewindow;
499 This->dest_window = devicewindow;
503 if(cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
505 /* Enable thread safety in wined3d */
506 IWineD3DDevice_SetMultithreaded(This->wineD3DDevice);
509 /* Unhandled flags */
510 if(cooplevel & DDSCL_ALLOWREBOOT)
511 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
512 if(cooplevel & DDSCL_ALLOWMODEX)
513 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
514 if(cooplevel & DDSCL_FPUSETUP)
515 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
517 /* Store the cooperative_level */
518 This->cooperative_level |= cooplevel;
519 TRACE("SetCooperativeLevel retuning DD_OK\n");
520 LeaveCriticalSection(&ddraw_cs);
524 /*****************************************************************************
526 * Helper function for SetDisplayMode and RestoreDisplayMode
528 * Implements DirectDraw's SetDisplayMode, but ignores the value of
529 * ForceRefreshRate, since it is already handled by
530 * IDirectDrawImpl_SetDisplayMode. RestoreDisplayMode can use this function
531 * without worrying that ForceRefreshRate will override the refresh rate. For
532 * argument and return value documentation, see
533 * IDirectDrawImpl_SetDisplayMode.
535 *****************************************************************************/
537 IDirectDrawImpl_SetDisplayModeNoOverride(IDirectDraw7 *iface,
544 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
545 WINED3DDISPLAYMODE Mode;
547 TRACE("(%p)->(%d,%d,%d,%d,%x: Relay!\n", This, Width, Height, BPP, RefreshRate, Flags);
549 EnterCriticalSection(&ddraw_cs);
550 if( !Width || !Height )
552 ERR("Width=%d, Height=%d, what to do?\n", Width, Height);
553 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
554 LeaveCriticalSection(&ddraw_cs);
558 /* Check the exclusive mode
559 if(!(This->cooperative_level & DDSCL_EXCLUSIVE))
560 return DDERR_NOEXCLUSIVEMODE;
561 * This is WRONG. Don't know if the SDK is completely
562 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
563 * is returned, but Half-Life 1.1.1.1 (Steam version)
568 Mode.Height = Height;
569 Mode.RefreshRate = RefreshRate;
572 case 8: Mode.Format = WINED3DFMT_P8; break;
573 case 15: Mode.Format = WINED3DFMT_X1R5G5B5; break;
574 case 16: Mode.Format = WINED3DFMT_R5G6B5; break;
575 case 24: Mode.Format = WINED3DFMT_R8G8B8; break;
576 case 32: Mode.Format = WINED3DFMT_X8R8G8B8; break;
579 /* TODO: The possible return values from msdn suggest that
580 * the screen mode can't be changed if a surface is locked
581 * or some drawing is in progress
584 /* TODO: Lose the primary surface */
585 hr = IWineD3DDevice_SetDisplayMode(This->wineD3DDevice,
586 0, /* First swapchain */
588 LeaveCriticalSection(&ddraw_cs);
591 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
596 /*****************************************************************************
597 * IDirectDraw7::SetDisplayMode
599 * Sets the display screen resolution, color depth and refresh frequency
600 * when in fullscreen mode (in theory).
601 * Possible return values listed in the SDK suggest that this method fails
602 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
603 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
604 * It seems to be valid to pass 0 for With and Height, this has to be tested
605 * It could mean that the current video mode should be left as-is. (But why
609 * Height, Width: Screen dimension
610 * BPP: Color depth in Bits per pixel
611 * Refreshrate: Screen refresh rate
617 *****************************************************************************/
618 static HRESULT WINAPI
619 IDirectDrawImpl_SetDisplayMode(IDirectDraw7 *iface,
626 if (force_refresh_rate != 0)
628 TRACE("ForceRefreshRate overriding passed-in refresh rate (%d Hz) to %d Hz\n", RefreshRate, force_refresh_rate);
629 RefreshRate = force_refresh_rate;
632 return IDirectDrawImpl_SetDisplayModeNoOverride(iface, Width, Height, BPP,
636 /*****************************************************************************
637 * IDirectDraw7::RestoreDisplayMode
639 * Restores the display mode to what it was at creation time. Basically.
641 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
642 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
643 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
644 * -> DD_1 is released. The screen should be left at 1024x768x32.
645 * -> DD_2 is released. The screen should be set to 1400x1050x32
646 * This case is unhandled right now, but Empire Earth does it this way.
647 * (But perhaps there is something in SetCooperativeLevel to prevent this)
649 * The msdn says that this method resets the display mode to what it was before
650 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
654 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
656 *****************************************************************************/
657 static HRESULT WINAPI
658 IDirectDrawImpl_RestoreDisplayMode(IDirectDraw7 *iface)
660 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
661 TRACE("(%p)\n", This);
663 return IDirectDrawImpl_SetDisplayModeNoOverride(ICOM_INTERFACE(This, IDirectDraw7),
671 /*****************************************************************************
672 * IDirectDraw7::GetCaps
674 * Returns the drives capabilities
676 * Used for version 1, 2, 4 and 7
679 * DriverCaps: Structure to write the Hardware accelerated caps to
680 * HelCaps: Structure to write the emulation caps to
683 * This implementation returns DD_OK only
685 *****************************************************************************/
686 static HRESULT WINAPI
687 IDirectDrawImpl_GetCaps(IDirectDraw7 *iface,
691 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
693 WINED3DCAPS winecaps;
695 DDSCAPS2 ddscaps = {0, 0, 0, 0};
696 TRACE("(%p)->(%p,%p)\n", This, DriverCaps, HELCaps);
698 /* One structure must be != NULL */
699 if( (!DriverCaps) && (!HELCaps) )
701 ERR("(%p) Invalid params to IDirectDrawImpl_GetCaps\n", This);
702 return DDERR_INVALIDPARAMS;
705 memset(&caps, 0, sizeof(caps));
706 memset(&winecaps, 0, sizeof(winecaps));
707 caps.dwSize = sizeof(caps);
708 EnterCriticalSection(&ddraw_cs);
709 hr = IWineD3DDevice_GetDeviceCaps(This->wineD3DDevice, &winecaps);
711 WARN("IWineD3DDevice::GetDeviceCaps failed\n");
712 LeaveCriticalSection(&ddraw_cs);
716 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
717 LeaveCriticalSection(&ddraw_cs);
719 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
723 caps.dwCaps = winecaps.DirectDrawCaps.Caps;
724 caps.dwCaps2 = winecaps.DirectDrawCaps.Caps2;
725 caps.dwCKeyCaps = winecaps.DirectDrawCaps.CKeyCaps;
726 caps.dwFXCaps = winecaps.DirectDrawCaps.FXCaps;
727 caps.dwPalCaps = winecaps.DirectDrawCaps.PalCaps;
728 caps.ddsCaps.dwCaps = winecaps.DirectDrawCaps.ddsCaps;
729 caps.dwSVBCaps = winecaps.DirectDrawCaps.SVBCaps;
730 caps.dwSVBCKeyCaps = winecaps.DirectDrawCaps.SVBCKeyCaps;
731 caps.dwSVBFXCaps = winecaps.DirectDrawCaps.SVBFXCaps;
732 caps.dwVSBCaps = winecaps.DirectDrawCaps.VSBCaps;
733 caps.dwVSBCKeyCaps = winecaps.DirectDrawCaps.VSBCKeyCaps;
734 caps.dwVSBFXCaps = winecaps.DirectDrawCaps.VSBFXCaps;
735 caps.dwSSBCaps = winecaps.DirectDrawCaps.SSBCaps;
736 caps.dwSSBCKeyCaps = winecaps.DirectDrawCaps.SSBCKeyCaps;
737 caps.dwSSBFXCaps = winecaps.DirectDrawCaps.SSBFXCaps;
739 /* Even if WineD3D supports 3D rendering, remove the cap if ddraw is configured
742 if(DefaultSurfaceType == SURFACE_GDI) {
743 caps.dwCaps &= ~DDCAPS_3D;
744 caps.ddsCaps.dwCaps &= ~(DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER);
746 if(winecaps.DirectDrawCaps.StrideAlign != 0) {
747 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
748 caps.dwAlignStrideAlign = winecaps.DirectDrawCaps.StrideAlign;
753 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
756 TRACE("Driver Caps :\n");
757 DDRAW_dump_DDCAPS(DriverCaps);
763 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
766 TRACE("HEL Caps :\n");
767 DDRAW_dump_DDCAPS(HELCaps);
774 /*****************************************************************************
775 * IDirectDraw7::Compact
777 * No idea what it does, MSDN says it's not implemented.
780 * DD_OK, but this is unchecked
782 *****************************************************************************/
783 static HRESULT WINAPI
784 IDirectDrawImpl_Compact(IDirectDraw7 *iface)
786 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
787 TRACE("(%p)\n", This);
792 /*****************************************************************************
793 * IDirectDraw7::GetDisplayMode
795 * Returns information about the current display mode
797 * Exists in Version 1, 2, 4 and 7
800 * DDSD: Address of a surface description structure to write the info to
805 *****************************************************************************/
806 static HRESULT WINAPI
807 IDirectDrawImpl_GetDisplayMode(IDirectDraw7 *iface,
808 DDSURFACEDESC2 *DDSD)
810 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
812 WINED3DDISPLAYMODE Mode;
814 TRACE("(%p)->(%p): Relay\n", This, DDSD);
816 EnterCriticalSection(&ddraw_cs);
817 /* This seems sane */
820 LeaveCriticalSection(&ddraw_cs);
821 return DDERR_INVALIDPARAMS;
824 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
825 * so one method can be used for all versions (Hopefully)
827 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
832 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
833 LeaveCriticalSection(&ddraw_cs);
838 memset(DDSD, 0, Size);
841 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
842 DDSD->dwWidth = Mode.Width;
843 DDSD->dwHeight = Mode.Height;
844 DDSD->u2.dwRefreshRate = 60;
845 DDSD->ddsCaps.dwCaps = 0;
846 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
847 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, Mode.Format);
848 DDSD->u1.lPitch = Mode.Width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
852 TRACE("Returning surface desc :\n");
853 DDRAW_dump_surface_desc(DDSD);
856 LeaveCriticalSection(&ddraw_cs);
860 /*****************************************************************************
861 * IDirectDraw7::GetFourCCCodes
863 * Returns an array of supported FourCC codes.
865 * Exists in Version 1, 2, 4 and 7
868 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
869 * of enumerated codes
870 * Codes: Pointer to an array of DWORDs where the supported codes are written
874 * Always returns DD_OK, as it's a stub for now
876 *****************************************************************************/
877 static HRESULT WINAPI
878 IDirectDrawImpl_GetFourCCCodes(IDirectDraw7 *iface,
879 DWORD *NumCodes, DWORD *Codes)
881 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
882 WINED3DFORMAT formats[] = {
883 WINED3DFMT_YUY2, WINED3DFMT_UYVY,
884 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
886 DWORD count = 0, i, outsize;
888 WINED3DDISPLAYMODE d3ddm;
889 TRACE("(%p)->(%p, %p)\n", This, NumCodes, Codes);
891 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
895 outsize = NumCodes && Codes ? *NumCodes : 0;
897 for(i = 0; i < (sizeof(formats) / sizeof(formats[0])); i++) {
898 hr = IWineD3D_CheckDeviceFormat(This->wineD3D,
899 WINED3DADAPTER_DEFAULT,
901 d3ddm.Format /* AdapterFormat */,
903 WINED3DRTYPE_SURFACE,
906 if(count < outsize) {
907 Codes[count] = formats[i];
913 TRACE("Returning %u FourCC codes\n", count);
920 /*****************************************************************************
921 * IDirectDraw7::GetMonitorFrequency
923 * Returns the monitor's frequency
925 * Exists in Version 1, 2, 4 and 7
928 * Freq: Pointer to a DWORD to write the frequency to
931 * Always returns DD_OK
933 *****************************************************************************/
934 static HRESULT WINAPI
935 IDirectDrawImpl_GetMonitorFrequency(IDirectDraw7 *iface,
938 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
939 TRACE("(%p)->(%p)\n", This, Freq);
941 /* Ideally this should be in WineD3D, as it concerns the screen setup,
942 * but for now this should make the games happy
948 /*****************************************************************************
949 * IDirectDraw7::GetVerticalBlankStatus
951 * Returns the Vertical blank status of the monitor. This should be in WineD3D
952 * too basically, but as it's a semi stub, I didn't create a function there
955 * status: Pointer to a BOOL to be filled with the vertical blank status
959 * DDERR_INVALIDPARAMS if status is NULL
961 *****************************************************************************/
962 static HRESULT WINAPI
963 IDirectDrawImpl_GetVerticalBlankStatus(IDirectDraw7 *iface,
966 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
967 TRACE("(%p)->(%p)\n", This, status);
969 /* This looks sane, the MSDN suggests it too */
970 EnterCriticalSection(&ddraw_cs);
973 LeaveCriticalSection(&ddraw_cs);
974 return DDERR_INVALIDPARAMS;
977 *status = This->fake_vblank;
978 This->fake_vblank = !This->fake_vblank;
979 LeaveCriticalSection(&ddraw_cs);
983 /*****************************************************************************
984 * IDirectDraw7::GetAvailableVidMem
986 * Returns the total and free video memory
989 * Caps: Specifies the memory type asked for
990 * total: Pointer to a DWORD to be filled with the total memory
991 * free: Pointer to a DWORD to be filled with the free memory
995 * DDERR_INVALIDPARAMS of free and total are NULL
997 *****************************************************************************/
998 static HRESULT WINAPI
999 IDirectDrawImpl_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total, DWORD *free)
1001 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1002 TRACE("(%p)->(%p, %p, %p)\n", This, Caps, total, free);
1006 TRACE("(%p) Asked for memory with description: ", This);
1007 DDRAW_dump_DDSCAPS2(Caps);
1009 EnterCriticalSection(&ddraw_cs);
1011 /* Todo: System memory vs local video memory vs non-local video memory
1012 * The MSDN also mentions differences between texture memory and other
1013 * resources, but that's not important
1016 if( (!total) && (!free) )
1018 LeaveCriticalSection(&ddraw_cs);
1019 return DDERR_INVALIDPARAMS;
1022 if(total) *total = This->total_vidmem;
1023 if(free) *free = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
1025 LeaveCriticalSection(&ddraw_cs);
1029 /*****************************************************************************
1030 * IDirectDraw7::Initialize
1032 * Initializes a DirectDraw interface.
1035 * GUID: Interface identifier. Well, don't know what this is really good
1039 * Returns DD_OK on the first call,
1040 * DDERR_ALREADYINITIALIZED on repeated calls
1042 *****************************************************************************/
1043 static HRESULT WINAPI
1044 IDirectDrawImpl_Initialize(IDirectDraw7 *iface,
1047 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1048 TRACE("(%p)->(%s): No-op\n", This, debugstr_guid(Guid));
1050 if(This->initialized)
1052 return DDERR_ALREADYINITIALIZED;
1060 /*****************************************************************************
1061 * IDirectDraw7::FlipToGDISurface
1063 * "Makes the surface that the GDI writes to the primary surface"
1064 * Looks like some windows specific thing we don't have to care about.
1065 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1066 * show error boxes ;)
1067 * Well, just return DD_OK.
1070 * Always returns DD_OK
1072 *****************************************************************************/
1073 static HRESULT WINAPI
1074 IDirectDrawImpl_FlipToGDISurface(IDirectDraw7 *iface)
1076 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1077 TRACE("(%p)\n", This);
1082 /*****************************************************************************
1083 * IDirectDraw7::WaitForVerticalBlank
1085 * This method allows applications to get in sync with the vertical blank
1087 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1088 * redraw the screen, most likely because of this stub
1091 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1092 * or DDWAITVB_BLOCKEND
1093 * h: Not used, according to MSDN
1096 * Always returns DD_OK
1098 *****************************************************************************/
1099 static HRESULT WINAPI
1100 IDirectDrawImpl_WaitForVerticalBlank(IDirectDraw7 *iface,
1104 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1105 FIXME("(%p)->(%x,%p): Stub\n", This, Flags, h);
1107 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1108 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1109 return DDERR_UNSUPPORTED; /* unchecked */
1114 /*****************************************************************************
1115 * IDirectDraw7::GetScanLine
1117 * Returns the scan line that is being drawn on the monitor
1120 * Scanline: Address to write the scan line value to
1123 * Always returns DD_OK
1125 *****************************************************************************/
1126 static HRESULT WINAPI IDirectDrawImpl_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1128 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1129 static BOOL hide = FALSE;
1130 WINED3DDISPLAYMODE Mode;
1132 /* This function is called often, so print the fixme only once */
1133 EnterCriticalSection(&ddraw_cs);
1136 FIXME("(%p)->(%p): Semi-Stub\n", This, Scanline);
1140 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1144 /* Fake the line sweeping of the monitor */
1145 /* FIXME: We should synchronize with a source to keep the refresh rate */
1146 *Scanline = This->cur_scanline++;
1147 /* Assume 20 scan lines in the vertical blank */
1148 if (This->cur_scanline >= Mode.Height + 20)
1149 This->cur_scanline = 0;
1151 LeaveCriticalSection(&ddraw_cs);
1155 /*****************************************************************************
1156 * IDirectDraw7::TestCooperativeLevel
1158 * Informs the application about the state of the video adapter, depending
1159 * on the cooperative level
1162 * DD_OK if the device is in a sane state
1163 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1164 * if the state is not correct(See below)
1166 *****************************************************************************/
1167 static HRESULT WINAPI
1168 IDirectDrawImpl_TestCooperativeLevel(IDirectDraw7 *iface)
1170 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1172 TRACE("(%p)\n", This);
1174 EnterCriticalSection(&ddraw_cs);
1175 /* Description from MSDN:
1176 * For fullscreen apps return DDERR_NOEXCLUSIVEMODE if the user switched
1177 * away from the app with e.g. alt-tab. Windowed apps receive
1178 * DDERR_EXCLUSIVEMODEALREADYSET if another application created a
1179 * DirectDraw object in exclusive mode. DDERR_WRONGMODE is returned,
1180 * when the video mode has changed
1183 hr = IWineD3DDevice_TestCooperativeLevel(This->wineD3DDevice);
1185 /* Fix the result value. These values are mapped from their
1190 case WINED3DERR_DEVICELOST:
1191 if(This->cooperative_level & DDSCL_EXCLUSIVE)
1193 LeaveCriticalSection(&ddraw_cs);
1194 return DDERR_NOEXCLUSIVEMODE;
1198 LeaveCriticalSection(&ddraw_cs);
1199 return DDERR_EXCLUSIVEMODEALREADYSET;
1202 case WINED3DERR_DEVICENOTRESET:
1203 LeaveCriticalSection(&ddraw_cs);
1207 LeaveCriticalSection(&ddraw_cs);
1210 case WINED3DERR_DRIVERINTERNALERROR:
1212 ERR("(%p) Unexpected return value %08x from wineD3D, "
1213 " returning DD_OK\n", This, hr);
1216 LeaveCriticalSection(&ddraw_cs);
1220 /*****************************************************************************
1221 * IDirectDraw7::GetGDISurface
1223 * Returns the surface that GDI is treating as the primary surface.
1224 * For Wine this is the front buffer
1227 * GDISurface: Address to write the surface pointer to
1230 * DD_OK if the surface was found
1231 * DDERR_NOTFOUND if the GDI surface wasn't found
1233 *****************************************************************************/
1234 static HRESULT WINAPI
1235 IDirectDrawImpl_GetGDISurface(IDirectDraw7 *iface,
1236 IDirectDrawSurface7 **GDISurface)
1238 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1239 IWineD3DSurface *Surf;
1240 IDirectDrawSurface7 *ddsurf;
1243 TRACE("(%p)->(%p)\n", This, GDISurface);
1245 /* Get the back buffer from the wineD3DDevice and search its
1246 * attached surfaces for the front buffer
1248 EnterCriticalSection(&ddraw_cs);
1249 hr = IWineD3DDevice_GetBackBuffer(This->wineD3DDevice,
1251 0, /* first back buffer*/
1252 WINED3DBACKBUFFER_TYPE_MONO,
1255 if( (hr != D3D_OK) ||
1258 ERR("IWineD3DDevice::GetBackBuffer failed\n");
1259 LeaveCriticalSection(&ddraw_cs);
1260 return DDERR_NOTFOUND;
1263 /* GetBackBuffer AddRef()ed the surface, release it */
1264 IWineD3DSurface_Release(Surf);
1266 IWineD3DSurface_GetParent(Surf,
1267 (IUnknown **) &ddsurf);
1268 IDirectDrawSurface7_Release(ddsurf); /* For the GetParent */
1270 /* Find the front buffer */
1271 ddsCaps.dwCaps = DDSCAPS_FRONTBUFFER;
1272 hr = IDirectDrawSurface7_GetAttachedSurface(ddsurf,
1277 ERR("IDirectDrawSurface7::GetAttachedSurface failed, hr = %x\n", hr);
1280 /* The AddRef is OK this time */
1281 LeaveCriticalSection(&ddraw_cs);
1285 /*****************************************************************************
1286 * IDirectDraw7::EnumDisplayModes
1288 * Enumerates the supported Display modes. The modes can be filtered with
1289 * the DDSD parameter.
1292 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES
1293 * DDSD: Surface description to filter the modes
1294 * Context: Pointer passed back to the callback function
1295 * cb: Application-provided callback function
1299 * DDERR_INVALIDPARAMS if the callback wasn't set
1301 *****************************************************************************/
1302 static HRESULT WINAPI
1303 IDirectDrawImpl_EnumDisplayModes(IDirectDraw7 *iface,
1305 DDSURFACEDESC2 *DDSD,
1307 LPDDENUMMODESCALLBACK2 cb)
1309 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1310 unsigned int modenum, fmt;
1311 WINED3DFORMAT pixelformat = WINED3DFMT_UNKNOWN;
1312 WINED3DDISPLAYMODE mode;
1313 DDSURFACEDESC2 callback_sd;
1314 WINED3DDISPLAYMODE *enum_modes = NULL;
1315 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
1317 WINED3DFORMAT checkFormatList[] =
1320 WINED3DFMT_A8R8G8B8,
1321 WINED3DFMT_X8R8G8B8,
1323 WINED3DFMT_X1R5G5B5,
1324 WINED3DFMT_A1R5G5B5,
1325 WINED3DFMT_A4R4G4B4,
1327 WINED3DFMT_A8R3G3B2,
1328 WINED3DFMT_X4R4G4B4,
1329 WINED3DFMT_A2B10G10R10,
1330 WINED3DFMT_A8B8G8R8,
1331 WINED3DFMT_X8B8G8R8,
1332 WINED3DFMT_A2R10G10B10,
1337 TRACE("(%p)->(%p,%p,%p): Relay\n", This, DDSD, Context, cb);
1339 EnterCriticalSection(&ddraw_cs);
1340 /* This looks sane */
1343 LeaveCriticalSection(&ddraw_cs);
1344 return DDERR_INVALIDPARAMS;
1349 if ((DDSD->dwFlags & DDSD_PIXELFORMAT) && (DDSD->u4.ddpfPixelFormat.dwFlags & DDPF_RGB) )
1350 pixelformat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
1353 if(!(Flags & DDEDM_REFRESHRATES))
1355 enum_mode_array_size = 16;
1356 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1359 ERR("Out of memory\n");
1360 LeaveCriticalSection(&ddraw_cs);
1361 return DDERR_OUTOFMEMORY;
1365 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
1367 if(pixelformat != WINED3DFMT_UNKNOWN && checkFormatList[fmt] != pixelformat)
1373 while(IWineD3D_EnumAdapterModes(This->wineD3D,
1374 WINED3DADAPTER_DEFAULT,
1375 checkFormatList[fmt],
1377 &mode) == WINED3D_OK)
1381 if(DDSD->dwFlags & DDSD_WIDTH && mode.Width != DDSD->dwWidth) continue;
1382 if(DDSD->dwFlags & DDSD_HEIGHT && mode.Height != DDSD->dwHeight) continue;
1385 if(!(Flags & DDEDM_REFRESHRATES))
1387 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
1388 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
1389 * to be reduced to a single unique result in such case.
1394 for (i = 0; i < enum_mode_count; i++)
1396 if(enum_modes[i].Width == mode.Width && enum_modes[i].Height == mode.Height &&
1397 enum_modes[i].Format == mode.Format)
1407 memset(&callback_sd, 0, sizeof(callback_sd));
1408 callback_sd.dwSize = sizeof(callback_sd);
1409 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1411 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH;
1412 if(Flags & DDEDM_REFRESHRATES)
1414 callback_sd.dwFlags |= DDSD_REFRESHRATE;
1415 callback_sd.u2.dwRefreshRate = mode.RefreshRate;
1418 callback_sd.dwWidth = mode.Width;
1419 callback_sd.dwHeight = mode.Height;
1421 PixelFormat_WineD3DtoDD(&callback_sd.u4.ddpfPixelFormat, mode.Format);
1423 /* Calc pitch and DWORD align like MSDN says */
1424 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.Width;
1425 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
1427 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
1428 callback_sd.u2.dwRefreshRate);
1430 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
1432 TRACE("Application asked to terminate the enumeration\n");
1433 HeapFree(GetProcessHeap(), 0, enum_modes);
1434 LeaveCriticalSection(&ddraw_cs);
1438 if(!(Flags & DDEDM_REFRESHRATES))
1440 if (enum_mode_count == enum_mode_array_size)
1442 WINED3DDISPLAYMODE *new_enum_modes;
1444 enum_mode_array_size *= 2;
1445 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1447 if (!new_enum_modes)
1449 ERR("Out of memory\n");
1450 HeapFree(GetProcessHeap(), 0, enum_modes);
1451 LeaveCriticalSection(&ddraw_cs);
1452 return DDERR_OUTOFMEMORY;
1455 enum_modes = new_enum_modes;
1458 enum_modes[enum_mode_count++] = mode;
1463 TRACE("End of enumeration\n");
1464 HeapFree(GetProcessHeap(), 0, enum_modes);
1465 LeaveCriticalSection(&ddraw_cs);
1469 /*****************************************************************************
1470 * IDirectDraw7::EvaluateMode
1472 * Used with IDirectDraw7::StartModeTest to test video modes.
1473 * EvaluateMode is used to pass or fail a mode, and continue with the next
1477 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
1478 * Timeout: Returns the amount of seconds left before the mode would have
1479 * been failed automatically
1482 * This implementation always DD_OK, because it's a stub
1484 *****************************************************************************/
1485 static HRESULT WINAPI
1486 IDirectDrawImpl_EvaluateMode(IDirectDraw7 *iface,
1490 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1491 FIXME("(%p)->(%d,%p): Stub!\n", This, Flags, Timeout);
1493 /* When implementing this, implement it in WineD3D */
1498 /*****************************************************************************
1499 * IDirectDraw7::GetDeviceIdentifier
1501 * Returns the device identifier, which gives information about the driver
1502 * Our device identifier is defined at the beginning of this file.
1505 * DDDI: Address for the returned structure
1506 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
1509 * On success it returns DD_OK
1510 * DDERR_INVALIDPARAMS if DDDI is NULL
1512 *****************************************************************************/
1513 static HRESULT WINAPI
1514 IDirectDrawImpl_GetDeviceIdentifier(IDirectDraw7 *iface,
1515 DDDEVICEIDENTIFIER2 *DDDI,
1518 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1519 TRACE("(%p)->(%p,%08x)\n", This, DDDI, Flags);
1522 return DDERR_INVALIDPARAMS;
1524 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
1525 * host adapter, if there's a secondary 3D adapter. This doesn't apply
1526 * to any modern hardware, nor is it interesting for Wine, so ignore it
1529 *DDDI = deviceidentifier;
1533 /*****************************************************************************
1534 * IDirectDraw7::GetSurfaceFromDC
1536 * Returns the Surface for a GDI device context handle.
1537 * Is this related to IDirectDrawSurface::GetDC ???
1540 * hdc: hdc to return the surface for
1541 * Surface: Address to write the surface pointer to
1544 * Always returns DD_OK because it's a stub
1546 *****************************************************************************/
1547 static HRESULT WINAPI
1548 IDirectDrawImpl_GetSurfaceFromDC(IDirectDraw7 *iface,
1550 IDirectDrawSurface7 **Surface)
1552 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1553 FIXME("(%p)->(%p,%p): Stub!\n", This, hdc, Surface);
1555 /* Implementation idea if needed: Loop through all surfaces and compare
1556 * their hdc with hdc. Implement it in WineD3D! */
1557 return DDERR_NOTFOUND;
1560 /*****************************************************************************
1561 * IDirectDraw7::RestoreAllSurfaces
1563 * Calls the restore method of all surfaces
1568 * Always returns DD_OK because it's a stub
1570 *****************************************************************************/
1571 static HRESULT WINAPI
1572 IDirectDrawImpl_RestoreAllSurfaces(IDirectDraw7 *iface)
1574 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1575 FIXME("(%p): Stub\n", This);
1577 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
1578 * get their parent and call their restore method. Do not implement
1579 * it in WineD3D, as restoring a surface means re-creating the
1585 /*****************************************************************************
1586 * IDirectDraw7::StartModeTest
1588 * Tests the specified video modes to update the system registry with
1589 * refresh rate information. StartModeTest starts the mode test,
1590 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
1591 * isn't called within 15 seconds, the mode is failed automatically
1593 * As refresh rates are handled by the X server, I don't think this
1594 * Method is important
1597 * Modes: An array of mode specifications
1598 * NumModes: The number of modes in Modes
1599 * Flags: Some flags...
1602 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
1603 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
1606 *****************************************************************************/
1607 static HRESULT WINAPI
1608 IDirectDrawImpl_StartModeTest(IDirectDraw7 *iface,
1613 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1614 WARN("(%p)->(%p, %d, %x): Semi-Stub, most likely harmless\n", This, Modes, NumModes, Flags);
1616 /* This looks sane */
1617 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
1619 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
1620 * As it is not, DDERR_TESTFINISHED is returned
1621 * (hopefully that's correct
1623 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
1624 * well, that value doesn't (yet) exist in the wine headers, so ignore it
1630 /*****************************************************************************
1631 * IDirectDrawImpl_RecreateSurfacesCallback
1633 * Enumeration callback for IDirectDrawImpl_RecreateAllSurfaces.
1634 * It re-recreates the WineD3DSurface. It's pretty straightforward
1636 *****************************************************************************/
1638 IDirectDrawImpl_RecreateSurfacesCallback(IDirectDrawSurface7 *surf,
1639 DDSURFACEDESC2 *desc,
1642 IDirectDrawSurfaceImpl *surfImpl = ICOM_OBJECT(IDirectDrawSurfaceImpl,
1643 IDirectDrawSurface7,
1645 IDirectDrawImpl *This = surfImpl->ddraw;
1647 IParentImpl *parImpl = NULL;
1648 IWineD3DSurface *wineD3DSurface;
1649 IWineD3DSwapChain *swapchain;
1652 IWineD3DClipper *clipper = NULL;
1654 WINED3DSURFACE_DESC Desc;
1655 WINED3DFORMAT Format;
1656 WINED3DRESOURCETYPE Type;
1661 WINED3DMULTISAMPLE_TYPE MultiSampleType;
1662 DWORD MultiSampleQuality;
1666 TRACE("(%p): Enumerated Surface %p\n", This, surfImpl);
1668 /* For the enumeration */
1669 IDirectDrawSurface7_Release(surf);
1671 if(surfImpl->ImplType == This->ImplType) return DDENUMRET_OK; /* Continue */
1673 /* Get the objects */
1674 swapchain = surfImpl->wineD3DSwapChain;
1675 surfImpl->wineD3DSwapChain = NULL;
1676 wineD3DSurface = surfImpl->WineD3DSurface;
1677 IWineD3DSurface_GetParent(wineD3DSurface, &Parent);
1678 IUnknown_Release(Parent); /* For the getParent */
1680 /* Is the parent an IParent interface? */
1681 if(IUnknown_QueryInterface(Parent, &IID_IParent, &tmp) == S_OK)
1683 /* It is a IParent interface! */
1684 IUnknown_Release(Parent); /* For the QueryInterface */
1685 parImpl = ICOM_OBJECT(IParentImpl, IParent, Parent);
1686 /* Release the reference the parent interface is holding */
1687 IWineD3DSurface_Release(wineD3DSurface);
1690 /* get the clipper */
1691 IWineD3DSurface_GetClipper(wineD3DSurface, &clipper);
1693 /* Get the surface properties */
1694 Desc.Format = &Format;
1696 Desc.Usage = &Usage;
1699 Desc.MultiSampleType = &MultiSampleType;
1700 Desc.MultiSampleQuality = &MultiSampleQuality;
1701 Desc.Width = &Width;
1702 Desc.Height = &Height;
1704 hr = IWineD3DSurface_GetDesc(wineD3DSurface, &Desc);
1705 if(hr != D3D_OK) return hr;
1708 /* If there's a swapchain, it owns the IParent interface. Create a new one for the
1711 parImpl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*parImpl));
1712 ICOM_INIT_INTERFACE(parImpl, IParent, IParent_Vtbl);
1715 Parent = (IUnknown *) parImpl;
1718 /* Create the new surface */
1719 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice,
1720 Width, Height, Format,
1721 TRUE /* Lockable */,
1722 FALSE /* Discard */,
1723 surfImpl->mipmap_level,
1724 &surfImpl->WineD3DSurface,
1730 0 /* SharedHandle */,
1737 IWineD3DSurface_SetClipper(surfImpl->WineD3DSurface, clipper);
1739 /* Update the IParent if it exists */
1742 parImpl->child = (IUnknown *) surfImpl->WineD3DSurface;
1743 /* Add a reference for the IParent */
1744 IWineD3DSurface_AddRef(surfImpl->WineD3DSurface);
1746 /* TODO: Copy the surface content, except for render targets */
1748 /* If there's a swapchain, it owns the wined3d surfaces. So Destroy
1752 /* The backbuffers have the swapchain set as well, but the primary
1753 * owns it and destroys it
1755 if(surfImpl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
1756 IWineD3DDevice_UninitGDI(This->wineD3DDevice, D3D7CB_DestroySwapChain);
1758 surfImpl->isRenderTarget = FALSE;
1760 if(IWineD3DSurface_Release(wineD3DSurface) == 0)
1761 TRACE("Surface released successful, next surface\n");
1763 ERR("Something's still holding the old WineD3DSurface\n");
1766 surfImpl->ImplType = This->ImplType;
1770 IWineD3DClipper_Release(clipper);
1772 return DDENUMRET_OK;
1775 /*****************************************************************************
1776 * IDirectDrawImpl_RecreateAllSurfaces
1778 * A function, that converts all wineD3DSurfaces to the new implementation type
1779 * It enumerates all surfaces with IWineD3DDevice::EnumSurfaces, creates a
1780 * new WineD3DSurface, copies the content and releases the old surface
1782 *****************************************************************************/
1784 IDirectDrawImpl_RecreateAllSurfaces(IDirectDrawImpl *This)
1786 DDSURFACEDESC2 desc;
1787 TRACE("(%p): Switch to implementation %d\n", This, This->ImplType);
1789 if(This->ImplType != SURFACE_OPENGL && This->d3d_initialized)
1791 /* Should happen almost never */
1792 FIXME("(%p) Switching to non-opengl surfaces with d3d started. Is this a bug?\n", This);
1794 IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroyDepthStencilSurface, D3D7CB_DestroySwapChain);
1796 /* Contrary: D3D starting is handled by the caller, because it knows the render target */
1798 memset(&desc, 0, sizeof(desc));
1799 desc.dwSize = sizeof(desc);
1801 return IDirectDraw7_EnumSurfaces(ICOM_INTERFACE(This, IDirectDraw7),
1805 IDirectDrawImpl_RecreateSurfacesCallback);
1808 /*****************************************************************************
1809 * D3D7CB_CreateSurface
1811 * Callback function for IDirect3DDevice_CreateTexture. It searches for the
1812 * correct mipmap sublevel, and returns it to WineD3D.
1813 * The surfaces are created already by IDirectDraw7::CreateSurface
1816 * With, Height: With and height of the surface
1817 * Format: The requested format
1818 * Usage, Pool: D3DUSAGE and D3DPOOL of the surface
1819 * level: The mipmap level
1820 * Face: The cube map face type
1821 * Surface: Pointer to pass the created surface back at
1822 * SharedHandle: NULL
1827 *****************************************************************************/
1828 static HRESULT WINAPI
1829 D3D7CB_CreateSurface(IUnknown *device,
1830 IUnknown *pSuperior,
1831 UINT Width, UINT Height,
1832 WINED3DFORMAT Format,
1833 DWORD Usage, WINED3DPOOL Pool, UINT level,
1834 WINED3DCUBEMAP_FACES Face,
1835 IWineD3DSurface **Surface,
1836 HANDLE *SharedHandle)
1838 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
1839 IDirectDrawSurfaceImpl *surf = NULL;
1841 DDSCAPS2 searchcaps = This->tex_root->surface_desc.ddsCaps;
1842 TRACE("(%p) call back. surf=%p. Face %d level %d\n", device, This->tex_root, Face, level);
1844 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
1847 case WINED3DCUBEMAP_FACE_POSITIVE_X:
1848 TRACE("Asked for positive x\n");
1849 if(searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP) {
1850 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
1852 surf = This->tex_root; break;
1853 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
1854 TRACE("Asked for negative x\n");
1855 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
1856 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
1857 TRACE("Asked for positive y\n");
1858 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
1859 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
1860 TRACE("Asked for negative y\n");
1861 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
1862 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
1863 TRACE("Asked for positive z\n");
1864 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
1865 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
1866 TRACE("Asked for negative z\n");
1867 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
1868 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
1873 IDirectDrawSurface7 *attached;
1874 IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(This->tex_root, IDirectDrawSurface7),
1877 surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attached);
1878 IDirectDrawSurface7_Release(attached);
1880 if(!surf) ERR("root search surface not found\n");
1882 /* Find the wanted mipmap. There are enough mipmaps in the chain */
1885 IDirectDrawSurface7 *attached;
1886 IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(surf, IDirectDrawSurface7),
1889 if(!attached) ERR("Surface not found\n");
1890 surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attached);
1891 IDirectDrawSurface7_Release(attached);
1895 /* Return the surface */
1896 *Surface = surf->WineD3DSurface;
1898 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *Surface, surf);
1902 ULONG WINAPI D3D7CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
1903 IUnknown* swapChainParent;
1904 TRACE("(%p) call back\n", pSwapChain);
1906 IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
1907 IUnknown_Release(swapChainParent);
1908 return IUnknown_Release(swapChainParent);
1911 ULONG WINAPI D3D7CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
1912 IUnknown* surfaceParent;
1913 TRACE("(%p) call back\n", pSurface);
1915 IWineD3DSurface_GetParent(pSurface, &surfaceParent);
1916 IUnknown_Release(surfaceParent);
1917 return IUnknown_Release(surfaceParent);
1920 /*****************************************************************************
1921 * IDirectDrawImpl_CreateNewSurface
1923 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
1924 * with the passed parameters.
1927 * DDSD: Description of the surface to create
1928 * Surf: Address to store the interface pointer at
1933 *****************************************************************************/
1934 static HRESULT WINAPI
1935 IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This,
1936 DDSURFACEDESC2 *pDDSD,
1937 IDirectDrawSurfaceImpl **ppSurf,
1941 UINT Width = 0, Height = 0;
1942 WINED3DFORMAT Format = WINED3DFMT_UNKNOWN;
1943 WINED3DRESOURCETYPE ResType = WINED3DRTYPE_SURFACE;
1945 WINED3DSURFTYPE ImplType = This->ImplType;
1946 WINED3DSURFACE_DESC Desc;
1948 IParentImpl *parImpl = NULL;
1949 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
1951 /* Dummies for GetDesc */
1952 WINED3DPOOL dummy_d3dpool;
1953 WINED3DMULTISAMPLE_TYPE dummy_mst;
1957 if (TRACE_ON(ddraw))
1959 TRACE(" (%p) Requesting surface desc :\n", This);
1960 DDRAW_dump_surface_desc(pDDSD);
1963 /* Select the surface type, if it wasn't choosen yet */
1964 if(ImplType == SURFACE_UNKNOWN)
1966 /* Use GL Surfaces if a D3DDEVICE Surface is requested */
1967 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1969 TRACE("(%p) Choosing GL surfaces because a 3DDEVICE Surface was requested\n", This);
1970 ImplType = SURFACE_OPENGL;
1973 /* Otherwise use GDI surfaces for now */
1976 TRACE("(%p) Choosing GDI surfaces for 2D rendering\n", This);
1977 ImplType = SURFACE_GDI;
1980 /* Policy if all surface implementations are available:
1981 * First, check if a default type was set with winecfg. If not,
1982 * try Xrender surfaces, and use them if they work. Next, check if
1983 * accelerated OpenGL is available, and use GL surfaces in this
1984 * case. If all else fails, use GDI surfaces. If a 3DDEVICE surface
1985 * was created, always use OpenGL surfaces.
1987 * (Note: Xrender surfaces are not implemented for now, the
1988 * unaccelerated implementation uses GDI to render in Software)
1991 /* Store the type. If it needs to be changed, all WineD3DSurfaces have to
1992 * be re-created. This could be done with IDirectDrawSurface7::Restore
1994 This->ImplType = ImplType;
1998 if((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE ) &&
1999 (This->ImplType != SURFACE_OPENGL ) && DefaultSurfaceType == SURFACE_UNKNOWN)
2001 /* We have to change to OpenGL,
2002 * and re-create all WineD3DSurfaces
2004 ImplType = SURFACE_OPENGL;
2005 This->ImplType = ImplType;
2006 TRACE("(%p) Re-creating all surfaces\n", This);
2007 IDirectDrawImpl_RecreateAllSurfaces(This);
2008 TRACE("(%p) Done recreating all surfaces\n", This);
2010 else if(This->ImplType != SURFACE_OPENGL && pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
2012 WARN("The application requests a 3D capable surface, but a non-opengl surface was set in the registry\n");
2013 /* Do not fail surface creation, only fail 3D device creation */
2017 if (!(pDDSD->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY)) &&
2018 !((pDDSD->ddsCaps.dwCaps & DDSCAPS_TEXTURE) && (pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)) )
2020 /* Tests show surfaces without memory flags get these flags added right after creation. */
2021 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
2023 /* Get the correct wined3d usage */
2024 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE |
2025 DDSCAPS_3DDEVICE ) )
2027 Usage |= WINED3DUSAGE_RENDERTARGET;
2029 pDDSD->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
2031 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
2033 Usage |= WINED3DUSAGE_OVERLAY;
2035 if(This->depthstencil || (pDDSD->ddsCaps.dwCaps & DDSCAPS_ZBUFFER) )
2037 /* The depth stencil creation callback sets this flag.
2038 * Set the WineD3D usage to let it know that it's a depth
2041 Usage |= WINED3DUSAGE_DEPTHSTENCIL;
2043 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
2045 Pool = WINED3DPOOL_SYSTEMMEM;
2047 else if(pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
2049 Pool = WINED3DPOOL_MANAGED;
2050 /* Managed textures have the system memory flag set */
2051 pDDSD->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
2053 else if(pDDSD->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
2055 /* Videomemory adds localvidmem, this is mutually exclusive with systemmemory
2058 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
2061 Format = PixelFormat_DD2WineD3D(&pDDSD->u4.ddpfPixelFormat);
2062 if(Format == WINED3DFMT_UNKNOWN)
2064 ERR("Unsupported / Unknown pixelformat\n");
2065 return DDERR_INVALIDPIXELFORMAT;
2068 /* Create the Surface object */
2069 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
2072 ERR("(%p) Error allocating memory for a surface\n", This);
2073 return DDERR_OUTOFVIDEOMEMORY;
2075 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawSurface7, IDirectDrawSurface7_Vtbl);
2076 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawSurface3, IDirectDrawSurface3_Vtbl);
2077 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawGammaControl, IDirectDrawGammaControl_Vtbl);
2078 ICOM_INIT_INTERFACE(*ppSurf, IDirect3DTexture2, IDirect3DTexture2_Vtbl);
2079 ICOM_INIT_INTERFACE(*ppSurf, IDirect3DTexture, IDirect3DTexture1_Vtbl);
2081 (*ppSurf)->version = 7;
2082 (*ppSurf)->ddraw = This;
2083 (*ppSurf)->surface_desc.dwSize = sizeof(DDSURFACEDESC2);
2084 (*ppSurf)->surface_desc.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2085 DD_STRUCT_COPY_BYSIZE(&(*ppSurf)->surface_desc, pDDSD);
2087 /* Surface attachments */
2088 (*ppSurf)->next_attached = NULL;
2089 (*ppSurf)->first_attached = *ppSurf;
2091 /* Needed to re-create the surface on an implementation change */
2092 (*ppSurf)->ImplType = ImplType;
2094 /* For D3DDevice creation */
2095 (*ppSurf)->isRenderTarget = FALSE;
2097 /* A trace message for debugging */
2098 TRACE("(%p) Created IDirectDrawSurface implementation structure at %p\n", This, *ppSurf);
2100 if(pDDSD->ddsCaps.dwCaps & ( DDSCAPS_PRIMARYSURFACE | DDSCAPS_TEXTURE | DDSCAPS_3DDEVICE) )
2102 /* Render targets and textures need a IParent interface,
2103 * because WineD3D will destroy them when the swapchain
2106 parImpl = HeapAlloc(GetProcessHeap(), 0, sizeof(IParentImpl));
2109 ERR("Out of memory when allocating memory for a IParent implementation\n");
2110 return DDERR_OUTOFMEMORY;
2113 ICOM_INIT_INTERFACE(parImpl, IParent, IParent_Vtbl);
2114 Parent = (IUnknown *) ICOM_INTERFACE(parImpl, IParent);
2115 TRACE("Using IParent interface %p as parent\n", parImpl);
2119 /* Use the surface as parent */
2120 Parent = (IUnknown *) ICOM_INTERFACE(*ppSurf, IDirectDrawSurface7);
2121 TRACE("Using Surface interface %p as parent\n", *ppSurf);
2124 /* Now create the WineD3D Surface */
2125 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice,
2129 TRUE /* Lockable */,
2130 FALSE /* Discard */,
2132 &(*ppSurf)->WineD3DSurface,
2135 WINED3DMULTISAMPLE_NONE,
2136 0 /* MultiSampleQuality */,
2137 0 /* SharedHandle */,
2143 ERR("IWineD3DDevice::CreateSurface failed. hr = %08x\n", hr);
2147 /* Set the child of the parent implementation if it exists */
2150 parImpl->child = (IUnknown *) (*ppSurf)->WineD3DSurface;
2151 /* The IParent releases the WineD3DSurface, and
2152 * the ddraw surface does that too. Hold a reference
2154 IWineD3DSurface_AddRef((*ppSurf)->WineD3DSurface);
2157 /* Increase the surface counter, and attach the surface */
2158 InterlockedIncrement(&This->surfaces);
2159 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
2161 /* Here we could store all created surfaces in the DirectDrawImpl structure,
2162 * But this could also be delegated to WineDDraw, as it keeps track of all its
2163 * resources. Not implemented for now, as there are more important things ;)
2166 /* Get the pixel format of the WineD3DSurface and store it.
2167 * Don't use the Format choosen above, WineD3D might have
2170 Desc.Format = &Format;
2171 Desc.Type = &ResType;
2172 Desc.Usage = &Usage;
2173 Desc.Pool = &dummy_d3dpool;
2174 Desc.Size = &dummy_uint;
2175 Desc.MultiSampleType = &dummy_mst;
2176 Desc.MultiSampleQuality = &dummy_dword;
2177 Desc.Width = &Width;
2178 Desc.Height = &Height;
2180 (*ppSurf)->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
2181 hr = IWineD3DSurface_GetDesc((*ppSurf)->WineD3DSurface, &Desc);
2184 ERR("IWineD3DSurface::GetDesc failed\n");
2185 IDirectDrawSurface7_Release( (IDirectDrawSurface7 *) *ppSurf);
2189 if(Format == WINED3DFMT_UNKNOWN)
2191 FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN\n");
2193 PixelFormat_WineD3DtoDD( &(*ppSurf)->surface_desc.u4.ddpfPixelFormat, Format);
2195 /* Anno 1602 stores the pitch right after surface creation, so make sure it's there.
2196 * I can't LockRect() the surface here because if OpenGL surfaces are in use, the
2197 * WineD3DDevice might not be usable for 3D yet, so an extra method was created.
2198 * TODO: Test other fourcc formats
2200 if(Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3 ||
2201 Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5)
2203 (*ppSurf)->surface_desc.dwFlags |= DDSD_LINEARSIZE;
2204 if(Format == WINED3DFMT_DXT1)
2206 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height) / 2;
2210 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height);
2215 (*ppSurf)->surface_desc.dwFlags |= DDSD_PITCH;
2216 (*ppSurf)->surface_desc.u1.lPitch = IWineD3DSurface_GetPitch((*ppSurf)->WineD3DSurface);
2219 /* Application passed a color key? Set it! */
2220 if(pDDSD->dwFlags & DDSD_CKDESTOVERLAY)
2222 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2224 (WINEDDCOLORKEY *) &pDDSD->u3.ddckCKDestOverlay);
2226 if(pDDSD->dwFlags & DDSD_CKDESTBLT)
2228 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2230 (WINEDDCOLORKEY *) &pDDSD->ddckCKDestBlt);
2232 if(pDDSD->dwFlags & DDSD_CKSRCOVERLAY)
2234 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2236 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcOverlay);
2238 if(pDDSD->dwFlags & DDSD_CKSRCBLT)
2240 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2242 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcBlt);
2244 if ( pDDSD->dwFlags & DDSD_LPSURFACE)
2246 hr = IWineD3DSurface_SetMem((*ppSurf)->WineD3DSurface, pDDSD->lpSurface);
2247 if(hr != WINED3D_OK)
2249 /* No need for a trace here, wined3d does that for us */
2250 IDirectDrawSurface7_Release(ICOM_INTERFACE((*ppSurf), IDirectDrawSurface7));
2257 /*****************************************************************************
2258 * CreateAdditionalSurfaces
2260 * Creates a new mipmap chain.
2263 * root: Root surface to attach the newly created chain to
2264 * count: number of surfaces to create
2265 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2266 * effects on the caller
2267 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2268 * creates an additional surface without the mipmapping flags
2270 *****************************************************************************/
2272 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2273 IDirectDrawSurfaceImpl *root,
2275 DDSURFACEDESC2 DDSD,
2278 UINT i, j, level = 0;
2280 IDirectDrawSurfaceImpl *last = root;
2282 for(i = 0; i < count; i++)
2284 IDirectDrawSurfaceImpl *object2 = NULL;
2286 /* increase the mipmap level, but only if a mipmap is created
2287 * In this case, also halve the size
2289 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2292 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2293 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2294 /* Set the mipmap sublevel flag according to msdn */
2295 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2299 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2301 CubeFaceRoot = FALSE;
2303 hr = IDirectDrawImpl_CreateNewSurface(This,
2312 /* Add the new surface to the complex attachment array */
2313 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2315 if(last->complex_array[j]) continue;
2316 last->complex_array[j] = object2;
2321 /* Remove the (possible) back buffer cap from the new surface description,
2322 * because only one surface in the flipping chain is a back buffer, one
2323 * is a front buffer, the others are just primary surfaces.
2325 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2330 /*****************************************************************************
2331 * IDirectDraw7::CreateSurface
2333 * Creates a new IDirectDrawSurface object and returns its interface.
2335 * The surface connections with wined3d are a bit tricky. Basically it works
2338 * |------------------------| |-----------------|
2339 * | DDraw surface | | WineD3DSurface |
2341 * | WineD3DSurface |-------------->| |
2342 * | Child |<------------->| Parent |
2343 * |------------------------| |-----------------|
2345 * The DDraw surface is the parent of the wined3d surface, and it releases
2346 * the WineD3DSurface when the ddraw surface is destroyed.
2348 * However, for all surfaces which can be in a container in WineD3D,
2349 * we have to do this. These surfaces are usually complex surfaces,
2350 * so this concerns primary surfaces with a front and a back buffer,
2353 * |------------------------| |-----------------|
2354 * | DDraw surface | | Container |
2356 * | Child |<------------->| Parent |
2357 * | Texture |<------------->| |
2358 * | WineD3DSurface |<----| | Levels |<--|
2359 * | Complex connection | | | | |
2360 * |------------------------| | |-----------------| |
2364 * | |------------------| | |-----------------| |
2365 * | | IParent | |-------->| WineD3DSurface | |
2367 * | | Child |<------------->| Parent | |
2368 * | | | | Container |<--|
2369 * | |------------------| |-----------------| |
2371 * | |----------------------| |
2372 * | | DDraw surface 2 | |
2374 * |<->| Complex root Child | |
2376 * | | WineD3DSurface |<----| |
2377 * | |----------------------| | |
2379 * | |---------------------| | |-----------------| |
2380 * | | IParent | |----->| WineD3DSurface | |
2382 * | | Child |<---------->| Parent | |
2383 * | |---------------------| | Container |<--|
2384 * | |-----------------| |
2386 * | ---More surfaces can follow--- |
2388 * The reason is that the IWineD3DSwapchain(render target container)
2389 * and the IWineD3DTexure(Texture container) release the parents
2390 * of their surface's children, but by releasing the complex root
2391 * the surfaces which are complexly attached to it are destroyed
2392 * too. See IDirectDrawSurface::Release for a more detailed
2396 * DDSD: Description of the surface to create
2397 * Surf: Address to store the interface pointer at
2398 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2399 * aggregation, so it has to be NULL
2403 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2404 * DDERR_* if an error occurs
2406 *****************************************************************************/
2407 static HRESULT WINAPI
2408 IDirectDrawImpl_CreateSurface(IDirectDraw7 *iface,
2409 DDSURFACEDESC2 *DDSD,
2410 IDirectDrawSurface7 **Surf,
2413 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2414 IDirectDrawSurfaceImpl *object = NULL;
2416 LONG extra_surfaces = 0;
2417 DDSURFACEDESC2 desc2;
2418 WINED3DDISPLAYMODE Mode;
2420 TRACE("(%p)->(%p,%p,%p)\n", This, DDSD, Surf, UnkOuter);
2422 /* Some checks before we start */
2423 if (TRACE_ON(ddraw))
2425 TRACE(" (%p) Requesting surface desc :\n", This);
2426 DDRAW_dump_surface_desc(DDSD);
2428 EnterCriticalSection(&ddraw_cs);
2430 if (UnkOuter != NULL)
2432 FIXME("(%p) : outer != NULL?\n", This);
2433 LeaveCriticalSection(&ddraw_cs);
2434 return CLASS_E_NOAGGREGATION; /* unchecked */
2439 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", This);
2440 LeaveCriticalSection(&ddraw_cs);
2441 return E_POINTER; /* unchecked */
2444 if (!(DDSD->dwFlags & DDSD_CAPS))
2446 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2447 DDSD->dwFlags |= DDSD_CAPS;
2450 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2452 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2453 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2456 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2458 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2459 WARN("(%p) Null surface pointer specified, ignore it!\n", This);
2460 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2463 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2464 !(This->cooperative_level & DDSCL_EXCLUSIVE))
2466 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n", This);
2468 LeaveCriticalSection(&ddraw_cs);
2469 return DDERR_NOEXCLUSIVEMODE;
2472 if(DDSD->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER)) {
2473 WARN("Application tried to create an explicit front or back buffer\n");
2474 LeaveCriticalSection(&ddraw_cs);
2475 return DDERR_INVALIDCAPS;
2477 /* Check cube maps but only if the size includes them */
2478 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2480 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2481 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2483 WARN("Cube map faces requested without cube map flag\n");
2484 LeaveCriticalSection(&ddraw_cs);
2485 return DDERR_INVALIDCAPS;
2487 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2488 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2490 WARN("Cube map without faces requested\n");
2491 LeaveCriticalSection(&ddraw_cs);
2492 return DDERR_INVALIDPARAMS;
2495 /* Quick tests confirm those can be created, but we don't do that yet */
2496 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2497 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2499 FIXME("Partial cube maps not supported yet\n");
2503 /* According to the msdn this flag is ignored by CreateSurface */
2504 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2505 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2507 /* Modify some flags */
2508 memset(&desc2, 0, sizeof(desc2));
2509 desc2.dwSize = sizeof(desc2); /* For the struct copy */
2510 DD_STRUCT_COPY_BYSIZE(&desc2, DDSD);
2511 desc2.dwSize = sizeof(desc2); /* To override a possibly smaller size */
2512 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2514 /* Get the video mode from WineD3D - we will need it */
2515 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
2516 0, /* Swapchain 0 */
2520 ERR("Failed to read display mode from wined3d\n");
2521 switch(This->orig_bpp)
2524 Mode.Format = WINED3DFMT_P8;
2528 Mode.Format = WINED3DFMT_X1R5G5B5;
2532 Mode.Format = WINED3DFMT_R5G6B5;
2536 Mode.Format = WINED3DFMT_R8G8B8;
2540 Mode.Format = WINED3DFMT_X8R8G8B8;
2543 Mode.Width = This->orig_width;
2544 Mode.Height = This->orig_height;
2547 /* No pixelformat given? Use the current screen format */
2548 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2550 desc2.dwFlags |= DDSD_PIXELFORMAT;
2551 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2553 /* Wait: It could be a Z buffer */
2554 if(desc2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
2556 switch(desc2.u2.dwMipMapCount) /* Who had this glorious idea? */
2559 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D15S1);
2562 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D16);
2565 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D24X8);
2568 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D32);
2571 ERR("Unknown Z buffer bit depth\n");
2576 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
2580 /* No Width or no Height? Use the original screen size
2582 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2583 !(desc2.dwFlags & DDSD_HEIGHT) )
2585 /* Invalid for non-render targets */
2586 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2588 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2590 LeaveCriticalSection(&ddraw_cs);
2591 return DDERR_INVALIDPARAMS;
2594 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2595 desc2.dwWidth = Mode.Width;
2596 desc2.dwHeight = Mode.Height;
2599 /* Mipmap count fixes */
2600 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2602 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2604 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2606 /* Mipmap count is given, should not be 0 */
2607 if( desc2.u2.dwMipMapCount == 0 )
2609 LeaveCriticalSection(&ddraw_cs);
2610 return DDERR_INVALIDPARAMS;
2615 /* Undocumented feature: Create sublevels until
2616 * either the width or the height is 1
2618 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2619 desc2.dwWidth : desc2.dwHeight;
2620 desc2.u2.dwMipMapCount = 0;
2623 desc2.u2.dwMipMapCount += 1;
2630 /* Not-complex mipmap -> Mipmapcount = 1 */
2631 desc2.u2.dwMipMapCount = 1;
2633 extra_surfaces = desc2.u2.dwMipMapCount - 1;
2635 /* There's a mipmap count in the created surface in any case */
2636 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2638 /* If no mipmap is given, the texture has only one level */
2640 /* The first surface is a front buffer, the back buffer is created afterwards */
2641 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2643 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2646 /* The root surface in a cube map is positive x */
2647 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2649 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2650 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2653 /* Create the first surface */
2654 hr = IDirectDrawImpl_CreateNewSurface(This, &desc2, &object, 0);
2657 ERR("IDirectDrawImpl_CreateNewSurface failed with %08x\n", hr);
2658 LeaveCriticalSection(&ddraw_cs);
2661 object->is_complex_root = TRUE;
2663 *Surf = ICOM_INTERFACE(object, IDirectDrawSurface7);
2665 /* Create Additional surfaces if necessary
2666 * This applies to Primary surfaces which have a back buffer count
2667 * set, but not to mipmap textures. In case of Mipmap textures,
2668 * wineD3D takes care of the creation of additional surfaces
2670 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2672 extra_surfaces = DDSD->dwBackBufferCount;
2673 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2674 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2678 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2680 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2681 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
2682 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2683 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
2684 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
2685 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2686 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
2687 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
2688 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2689 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
2690 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
2691 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2692 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
2693 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
2694 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2695 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
2696 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2699 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces, desc2, FALSE);
2702 /* This destroys and possibly created surfaces too */
2703 IDirectDrawSurface_Release( ICOM_INTERFACE(object, IDirectDrawSurface7) );
2704 LeaveCriticalSection(&ddraw_cs);
2708 /* If the implementation is OpenGL and there's no d3ddevice, attach a d3ddevice
2709 * But attach the d3ddevice only if the currently created surface was
2710 * a primary surface (2D app in 3D mode) or a 3DDEVICE surface (3D app)
2711 * The only case I can think of where this doesn't apply is when a
2712 * 2D app was configured by the user to run with OpenGL and it didn't create
2713 * the render target as first surface. In this case the render target creation
2714 * will cause the 3D init.
2716 if( (This->ImplType == SURFACE_OPENGL) && !(This->d3d_initialized) &&
2717 desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE) )
2719 IDirectDrawSurfaceImpl *target = object, *surface;
2722 /* Search for the primary to use as render target */
2723 LIST_FOR_EACH(entry, &This->surface_list)
2725 surface = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2726 if((surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER)) ==
2727 (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER))
2731 TRACE("Using primary %p as render target\n", target);
2736 TRACE("(%p) Attaching a D3DDevice, rendertarget = %p\n", This, target);
2737 hr = IDirectDrawImpl_AttachD3DDevice(This, target);
2740 IDirectDrawSurfaceImpl *release_surf;
2741 ERR("IDirectDrawImpl_AttachD3DDevice failed, hr = %x\n", hr);
2744 /* The before created surface structures are in an incomplete state here.
2745 * WineD3D holds the reference on the IParents, and it released them on the failure
2746 * already. So the regular release method implementation would fail on the attempt
2747 * to destroy either the IParents or the swapchain. So free the surface here.
2748 * The surface structure here is a list, not a tree, because onscreen targets
2749 * cannot be cube textures
2753 release_surf = object;
2754 object = object->complex_array[0];
2755 IDirectDrawSurfaceImpl_Destroy(release_surf);
2757 LeaveCriticalSection(&ddraw_cs);
2760 } else if(!(This->d3d_initialized) && desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
2761 IDirectDrawImpl_CreateGDISwapChain(This, object);
2764 /* Addref the ddraw interface to keep an reference for each surface */
2765 IDirectDraw7_AddRef(iface);
2766 object->ifaceToRelease = (IUnknown *) iface;
2768 /* Create a WineD3DTexture if a texture was requested */
2769 if(desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
2772 WINED3DFORMAT Format;
2773 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
2775 This->tex_root = object;
2777 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2779 /* a mipmap is created, create enough levels */
2780 levels = desc2.u2.dwMipMapCount;
2784 /* No mipmap is created, create one level */
2788 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM */
2789 if(DDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
2791 Pool = WINED3DPOOL_SYSTEMMEM;
2793 /* Should I forward the MANAGED cap to the managed pool ? */
2795 /* Get the format. It's set already by CreateNewSurface */
2796 Format = PixelFormat_DD2WineD3D(&object->surface_desc.u4.ddpfPixelFormat);
2798 /* The surfaces are already created, the callback only
2799 * passes the IWineD3DSurface to WineD3D
2801 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2803 hr = IWineD3DDevice_CreateCubeTexture(This->wineD3DDevice,
2804 DDSD->dwWidth, /* Edgelength */
2809 (IWineD3DCubeTexture **) &object->wineD3DTexture,
2810 0, /* SharedHandle */
2811 (IUnknown *) ICOM_INTERFACE(object, IDirectDrawSurface7),
2812 D3D7CB_CreateSurface);
2816 hr = IWineD3DDevice_CreateTexture(This->wineD3DDevice,
2817 DDSD->dwWidth, DDSD->dwHeight,
2818 levels, /* MipMapCount = Levels */
2822 (IWineD3DTexture **) &object->wineD3DTexture,
2823 0, /* SharedHandle */
2824 (IUnknown *) ICOM_INTERFACE(object, IDirectDrawSurface7),
2825 D3D7CB_CreateSurface );
2827 This->tex_root = NULL;
2830 LeaveCriticalSection(&ddraw_cs);
2834 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
2835 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
2838 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
2839 const DDPIXELFORMAT *provided)
2841 /* Some flags must be present in both or neither for a match. */
2842 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
2843 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
2844 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
2846 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2849 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
2852 if (requested->dwFlags & DDPF_FOURCC)
2853 if (requested->dwFourCC != provided->dwFourCC)
2856 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
2857 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2858 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
2861 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2862 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2863 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
2866 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
2867 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
2870 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
2871 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2873 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
2876 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
2877 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
2884 IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested,
2885 const DDSURFACEDESC2* provided)
2894 #define CMP(FLAG, FIELD) \
2895 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
2896 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
2898 static const struct compare_info compare[] =
2900 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
2901 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
2903 CMP(CKDESTBLT, ddckCKDestBlt),
2904 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
2905 CMP(CKSRCBLT, ddckCKSrcBlt),
2906 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
2907 CMP(HEIGHT, dwHeight),
2908 CMP(LINEARSIZE, u1 /* dwLinearSize */),
2909 CMP(LPSURFACE, lpSurface),
2910 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
2911 CMP(PITCH, u1 /* lPitch */),
2912 /* PIXELFORMAT: manual */
2913 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
2914 CMP(TEXTURESTAGE, dwTextureStage),
2915 CMP(WIDTH, dwWidth),
2916 /* ZBUFFERBITDEPTH: "obsolete" */
2923 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2926 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
2928 if (requested->dwFlags & compare[i].flag
2929 && memcmp((const char *)provided + compare[i].offset,
2930 (const char *)requested + compare[i].offset,
2931 compare[i].size) != 0)
2935 if (requested->dwFlags & DDSD_PIXELFORMAT)
2937 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
2938 &provided->u4.ddpfPixelFormat))
2945 #undef DDENUMSURFACES_SEARCHTYPE
2946 #undef DDENUMSURFACES_MATCHTYPE
2948 /*****************************************************************************
2949 * IDirectDraw7::EnumSurfaces
2951 * Loops through all surfaces attached to this device and calls the
2952 * application callback. This can't be relayed to WineD3DDevice,
2953 * because some WineD3DSurfaces' parents are IParent objects
2956 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
2957 * DDSD: Description to filter for
2958 * Context: Application-provided pointer, it's passed unmodified to the
2960 * Callback: Address to call for each surface
2963 * DDERR_INVALIDPARAMS if the callback is NULL
2966 *****************************************************************************/
2967 static HRESULT WINAPI
2968 IDirectDrawImpl_EnumSurfaces(IDirectDraw7 *iface,
2970 DDSURFACEDESC2 *DDSD,
2972 LPDDENUMSURFACESCALLBACK7 Callback)
2974 /* The surface enumeration is handled by WineDDraw,
2975 * because it keeps track of all surfaces attached to
2976 * it. The filtering is done by our callback function,
2977 * because WineDDraw doesn't handle ddraw-like surface
2980 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2981 IDirectDrawSurfaceImpl *surf;
2983 DDSURFACEDESC2 desc;
2984 struct list *entry, *entry2;
2986 all = Flags & DDENUMSURFACES_ALL;
2987 nomatch = Flags & DDENUMSURFACES_NOMATCH;
2989 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, DDSD, Context, Callback);
2990 EnterCriticalSection(&ddraw_cs);
2994 LeaveCriticalSection(&ddraw_cs);
2995 return DDERR_INVALIDPARAMS;
2998 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
2999 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
3001 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
3002 if (all || (nomatch != IDirectDrawImpl_DDSD_Match(DDSD, &surf->surface_desc)))
3004 desc = surf->surface_desc;
3005 IDirectDrawSurface7_AddRef(ICOM_INTERFACE(surf, IDirectDrawSurface7));
3006 if(Callback( ICOM_INTERFACE(surf, IDirectDrawSurface7), &desc, Context) != DDENUMRET_OK)
3008 LeaveCriticalSection(&ddraw_cs);
3013 LeaveCriticalSection(&ddraw_cs);
3017 static HRESULT WINAPI
3018 findRenderTarget(IDirectDrawSurface7 *surface,
3019 DDSURFACEDESC2 *desc,
3022 IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, surface);
3023 IDirectDrawSurfaceImpl **target = (IDirectDrawSurfaceImpl **) ctx;
3025 if(!surf->isRenderTarget) {
3027 IDirectDrawSurface7_Release(surface);
3028 return DDENUMRET_CANCEL;
3031 /* Recurse into the surface tree */
3032 IDirectDrawSurface7_EnumAttachedSurfaces(surface, ctx, findRenderTarget);
3034 IDirectDrawSurface7_Release(surface);
3035 if(*target) return DDENUMRET_CANCEL;
3036 else return DDENUMRET_OK; /* Continue with the next neighbor surface */
3039 /*****************************************************************************
3040 * D3D7CB_CreateRenderTarget
3042 * Callback called by WineD3D to create Surfaces for render target usage
3043 * This function takes the D3D target from the IDirectDrawImpl structure,
3044 * and returns the WineD3DSurface. To avoid double usage, the surface
3045 * is marked as render target afterwards
3048 * device: The WineD3DDevice's parent
3049 * Width, Height, Format: Dimensions and pixelformat of the render target
3050 * Ignored, because the surface already exists
3051 * MultiSample, MultisampleQuality, Lockable: Ignored for the same reason
3053 * ppSurface: Address to pass the surface pointer back at
3054 * pSharedHandle: Ignored
3057 * Always returns D3D_OK
3059 *****************************************************************************/
3060 static HRESULT WINAPI
3061 D3D7CB_CreateRenderTarget(IUnknown *device, IUnknown *pSuperior,
3062 UINT Width, UINT Height,
3063 WINED3DFORMAT Format,
3064 WINED3DMULTISAMPLE_TYPE MultiSample,
3065 DWORD MultisampleQuality,
3067 IWineD3DSurface** ppSurface,
3068 HANDLE* pSharedHandle)
3070 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
3071 IDirectDrawSurfaceImpl *d3dSurface = This->d3d_target, *target = NULL;
3072 TRACE("(%p) call back\n", device);
3074 if(d3dSurface->isRenderTarget)
3076 IDirectDrawSurface7_EnumAttachedSurfaces(ICOM_INTERFACE(d3dSurface, IDirectDrawSurface7),
3077 &target, findRenderTarget);
3081 target = d3dSurface;
3086 target = This->d3d_target;
3087 ERR(" (%p) : No DirectDrawSurface found to create the back buffer. Using the front buffer as back buffer. Uncertain consequences\n", This);
3090 /* TODO: Return failure if the dimensions do not match, but this shouldn't happen */
3092 *ppSurface = target->WineD3DSurface;
3093 target->isRenderTarget = TRUE;
3094 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *ppSurface, d3dSurface);
3098 static HRESULT WINAPI
3099 D3D7CB_CreateDepthStencilSurface(IUnknown *device,
3100 IUnknown *pSuperior,
3103 WINED3DFORMAT Format,
3104 WINED3DMULTISAMPLE_TYPE MultiSample,
3105 DWORD MultisampleQuality,
3107 IWineD3DSurface** ppSurface,
3108 HANDLE* pSharedHandle)
3110 /* Create a Depth Stencil surface to make WineD3D happy */
3111 HRESULT hr = D3D_OK;
3112 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
3113 DDSURFACEDESC2 ddsd;
3115 TRACE("(%p) call back\n", device);
3119 /* Create a DirectDraw surface */
3120 memset(&ddsd, 0, sizeof(ddsd));
3121 ddsd.dwSize = sizeof(ddsd);
3122 ddsd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
3123 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
3124 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
3125 ddsd.dwHeight = Height;
3126 ddsd.dwWidth = Width;
3129 PixelFormat_WineD3DtoDD(&ddsd.u4.ddpfPixelFormat, Format);
3133 ddsd.dwFlags ^= DDSD_PIXELFORMAT;
3136 This->depthstencil = TRUE;
3137 hr = IDirectDraw7_CreateSurface((IDirectDraw7 *) This,
3139 (IDirectDrawSurface7 **) &This->DepthStencilBuffer,
3141 This->depthstencil = FALSE;
3144 ERR(" (%p) Creating a DepthStencil Surface failed, result = %x\n", This, hr);
3147 *ppSurface = This->DepthStencilBuffer->WineD3DSurface;
3151 /*****************************************************************************
3152 * D3D7CB_CreateAdditionalSwapChain
3154 * Callback function for WineD3D which creates a new WineD3DSwapchain
3155 * interface. It also creates an IParent interface to store that pointer,
3156 * so the WineD3DSwapchain has a parent and can be released when the D3D
3157 * device is destroyed
3158 *****************************************************************************/
3159 static HRESULT WINAPI
3160 D3D7CB_CreateAdditionalSwapChain(IUnknown *device,
3161 WINED3DPRESENT_PARAMETERS* pPresentationParameters,
3162 IWineD3DSwapChain ** ppSwapChain)
3164 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
3165 IParentImpl *object = NULL;
3166 HRESULT res = D3D_OK;
3167 IWineD3DSwapChain *swapchain;
3168 IDirectDrawSurfaceImpl *iterator;
3169 TRACE("(%p) call back\n", device);
3171 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IParentImpl));
3174 FIXME("Allocation of memory failed\n");
3175 *ppSwapChain = NULL;
3176 return DDERR_OUTOFVIDEOMEMORY;
3179 ICOM_INIT_INTERFACE(object, IParent, IParent_Vtbl);
3182 res = IWineD3DDevice_CreateAdditionalSwapChain(This->wineD3DDevice,
3183 pPresentationParameters,
3185 (IUnknown*) ICOM_INTERFACE(object, IParent),
3186 D3D7CB_CreateRenderTarget,
3187 D3D7CB_CreateDepthStencilSurface,
3191 FIXME("(%p) call to IWineD3DDevice_CreateAdditionalSwapChain failed\n", This);
3192 HeapFree(GetProcessHeap(), 0 , object);
3193 *ppSwapChain = NULL;
3197 *ppSwapChain = swapchain;
3198 object->child = (IUnknown *) swapchain;
3199 This->d3d_target->wineD3DSwapChain = swapchain;
3200 iterator = This->d3d_target->complex_array[0];
3202 iterator->wineD3DSwapChain = swapchain;
3203 iterator = iterator->complex_array[0];
3210 static HRESULT WINAPI IDirectDrawImpl_CreateGDISwapChain(IDirectDrawImpl *This,
3211 IDirectDrawSurfaceImpl *primary) {
3213 WINED3DPRESENT_PARAMETERS presentation_parameters;
3216 window = This->dest_window;
3218 memset(&presentation_parameters, 0, sizeof(presentation_parameters));
3220 /* Use the surface description for the device parameters, not the
3221 * Device settings. The app might render to an offscreen surface
3223 presentation_parameters.BackBufferWidth = primary->surface_desc.dwWidth;
3224 presentation_parameters.BackBufferHeight = primary->surface_desc.dwHeight;
3225 presentation_parameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
3226 presentation_parameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
3227 presentation_parameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
3228 presentation_parameters.MultiSampleQuality = 0;
3229 presentation_parameters.SwapEffect = WINED3DSWAPEFFECT_FLIP;
3230 presentation_parameters.hDeviceWindow = window;
3231 presentation_parameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
3232 presentation_parameters.EnableAutoDepthStencil = FALSE; /* Not on GDI swapchains */
3233 presentation_parameters.AutoDepthStencilFormat = 0;
3234 presentation_parameters.Flags = 0;
3235 presentation_parameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
3236 presentation_parameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
3238 This->d3d_target = primary;
3239 hr = IWineD3DDevice_InitGDI(This->wineD3DDevice,
3240 &presentation_parameters,
3241 D3D7CB_CreateAdditionalSwapChain);
3242 This->d3d_target = NULL;
3246 FIXME("(%p) call to IWineD3DDevice_CreateAdditionalSwapChain failed\n", This);
3247 primary->wineD3DSwapChain = NULL;
3252 /*****************************************************************************
3253 * IDirectDrawImpl_AttachD3DDevice
3255 * Initializes the D3D capabilities of WineD3D
3258 * primary: The primary surface for D3D
3264 *****************************************************************************/
3265 static HRESULT WINAPI
3266 IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This,
3267 IDirectDrawSurfaceImpl *primary)
3270 HWND window = This->dest_window;
3272 WINED3DPRESENT_PARAMETERS localParameters;
3274 TRACE("(%p)->(%p)\n", This, primary);
3276 /* If there's no window, create a hidden window. WineD3D needs it */
3279 window = CreateWindowExA(0, This->classname, "Hidden D3D Window",
3281 GetSystemMetrics(SM_CXSCREEN),
3282 GetSystemMetrics(SM_CYSCREEN),
3283 NULL, NULL, GetModuleHandleA(0), NULL);
3285 ShowWindow(window, SW_HIDE); /* Just to be sure */
3286 WARN("(%p) No window for the Direct3DDevice, created a hidden window. HWND=%p\n", This, window);
3290 TRACE("(%p) Using existing window %p for Direct3D rendering\n", This, window);
3292 This->d3d_window = window;
3294 /* Store the future Render Target surface */
3295 This->d3d_target = primary;
3297 /* Use the surface description for the device parameters, not the
3298 * Device settings. The app might render to an offscreen surface
3300 localParameters.BackBufferWidth = primary->surface_desc.dwWidth;
3301 localParameters.BackBufferHeight = primary->surface_desc.dwHeight;
3302 localParameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
3303 localParameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
3304 localParameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
3305 localParameters.MultiSampleQuality = 0;
3306 localParameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
3307 localParameters.hDeviceWindow = window;
3308 localParameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
3309 localParameters.EnableAutoDepthStencil = TRUE;
3310 localParameters.AutoDepthStencilFormat = WINED3DFMT_D16;
3311 localParameters.Flags = 0;
3312 localParameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
3313 localParameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
3315 TRACE("Passing mode %d\n", localParameters.BackBufferFormat);
3317 /* Set this NOW, otherwise creating the depth stencil surface will cause a
3318 * recursive loop until ram or emulated video memory is full
3320 This->d3d_initialized = TRUE;
3322 hr = IWineD3DDevice_Init3D(This->wineD3DDevice,
3324 D3D7CB_CreateAdditionalSwapChain);
3327 This->d3d_target = NULL;
3328 This->d3d_initialized = FALSE;
3332 This->declArraySize = 2;
3333 This->decls = HeapAlloc(GetProcessHeap(),
3335 sizeof(*This->decls) * This->declArraySize);
3338 ERR("Error allocating an array for the converted vertex decls\n");
3339 This->declArraySize = 0;
3340 hr = IWineD3DDevice_Uninit3D(This->wineD3DDevice,
3341 D3D7CB_DestroyDepthStencilSurface,
3342 D3D7CB_DestroySwapChain);
3343 return E_OUTOFMEMORY;
3346 /* Create an Index Buffer parent */
3347 TRACE("(%p) Successfully initialized 3D\n", This);
3351 /*****************************************************************************
3352 * DirectDrawCreateClipper (DDRAW.@)
3354 * Creates a new IDirectDrawClipper object.
3357 * Clipper: Address to write the interface pointer to
3358 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3362 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3363 * E_OUTOFMEMORY if allocating the object failed
3365 *****************************************************************************/
3367 DirectDrawCreateClipper(DWORD Flags,
3368 LPDIRECTDRAWCLIPPER *Clipper,
3371 IDirectDrawClipperImpl* object;
3372 TRACE("(%08x,%p,%p)\n", Flags, Clipper, UnkOuter);
3374 EnterCriticalSection(&ddraw_cs);
3375 if (UnkOuter != NULL)
3377 LeaveCriticalSection(&ddraw_cs);
3378 return CLASS_E_NOAGGREGATION;
3383 LeaveCriticalSection(&ddraw_cs);
3384 return DDERR_NODIRECTDRAWSUPPORT;
3387 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3388 sizeof(IDirectDrawClipperImpl));
3391 LeaveCriticalSection(&ddraw_cs);
3392 return E_OUTOFMEMORY;
3395 ICOM_INIT_INTERFACE(object, IDirectDrawClipper, IDirectDrawClipper_Vtbl);
3397 object->wineD3DClipper = pWineDirect3DCreateClipper((IUnknown *) object);
3398 if(!object->wineD3DClipper)
3400 HeapFree(GetProcessHeap(), 0, object);
3401 LeaveCriticalSection(&ddraw_cs);
3402 return E_OUTOFMEMORY;
3405 *Clipper = (IDirectDrawClipper *) object;
3406 LeaveCriticalSection(&ddraw_cs);
3410 /*****************************************************************************
3411 * IDirectDraw7::CreateClipper
3413 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3415 *****************************************************************************/
3416 static HRESULT WINAPI
3417 IDirectDrawImpl_CreateClipper(IDirectDraw7 *iface,
3419 IDirectDrawClipper **Clipper,
3422 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3423 TRACE("(%p)->(%x,%p,%p)\n", This, Flags, Clipper, UnkOuter);
3424 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3427 /*****************************************************************************
3428 * IDirectDraw7::CreatePalette
3430 * Creates a new IDirectDrawPalette object
3433 * Flags: The flags for the new clipper
3434 * ColorTable: Color table to assign to the new clipper
3435 * Palette: Address to write the interface pointer to
3436 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3440 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3441 * E_OUTOFMEMORY if allocating the object failed
3443 *****************************************************************************/
3444 static HRESULT WINAPI
3445 IDirectDrawImpl_CreatePalette(IDirectDraw7 *iface,
3447 PALETTEENTRY *ColorTable,
3448 IDirectDrawPalette **Palette,
3449 IUnknown *pUnkOuter)
3451 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3452 IDirectDrawPaletteImpl *object;
3453 HRESULT hr = DDERR_GENERIC;
3454 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, ColorTable, Palette, pUnkOuter);
3456 EnterCriticalSection(&ddraw_cs);
3457 if(pUnkOuter != NULL)
3459 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
3460 LeaveCriticalSection(&ddraw_cs);
3461 return CLASS_E_NOAGGREGATION;
3464 /* The refcount test shows that a cooplevel is required for this */
3465 if(!This->cooperative_level)
3467 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3468 LeaveCriticalSection(&ddraw_cs);
3469 return DDERR_NOCOOPERATIVELEVELSET;
3472 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
3475 ERR("Out of memory when allocating memory for a palette implementation\n");
3476 LeaveCriticalSection(&ddraw_cs);
3477 return E_OUTOFMEMORY;
3480 ICOM_INIT_INTERFACE(object, IDirectDrawPalette, IDirectDrawPalette_Vtbl);
3482 object->ddraw_owner = This;
3484 hr = IWineD3DDevice_CreatePalette(This->wineD3DDevice, Flags, ColorTable, &object->wineD3DPalette, (IUnknown *) ICOM_INTERFACE(object, IDirectDrawPalette) );
3487 HeapFree(GetProcessHeap(), 0, object);
3488 LeaveCriticalSection(&ddraw_cs);
3492 IDirectDraw7_AddRef(iface);
3493 object->ifaceToRelease = (IUnknown *) iface;
3494 *Palette = ICOM_INTERFACE(object, IDirectDrawPalette);
3495 LeaveCriticalSection(&ddraw_cs);
3499 /*****************************************************************************
3500 * IDirectDraw7::DuplicateSurface
3502 * Duplicates a surface. The surface memory points to the same memory as
3503 * the original surface, and it's released when the last surface referencing
3504 * it is released. I guess that's beyond Wine's surface management right now
3505 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3506 * test application to implement this)
3509 * Src: Address of the source surface
3510 * Dest: Address to write the new surface pointer to
3513 * See IDirectDraw7::CreateSurface
3515 *****************************************************************************/
3516 static HRESULT WINAPI
3517 IDirectDrawImpl_DuplicateSurface(IDirectDraw7 *iface,
3518 IDirectDrawSurface7 *Src,
3519 IDirectDrawSurface7 **Dest)
3521 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3522 IDirectDrawSurfaceImpl *Surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Src);
3524 FIXME("(%p)->(%p,%p)\n", This, Surf, Dest);
3526 /* For now, simply create a new, independent surface */
3527 return IDirectDraw7_CreateSurface(iface,
3528 &Surf->surface_desc,
3533 /*****************************************************************************
3534 * IDirectDraw7 VTable
3535 *****************************************************************************/
3536 const IDirectDraw7Vtbl IDirectDraw7_Vtbl =
3539 IDirectDrawImpl_QueryInterface,
3540 IDirectDrawImpl_AddRef,
3541 IDirectDrawImpl_Release,
3542 /*** IDirectDraw ***/
3543 IDirectDrawImpl_Compact,
3544 IDirectDrawImpl_CreateClipper,
3545 IDirectDrawImpl_CreatePalette,
3546 IDirectDrawImpl_CreateSurface,
3547 IDirectDrawImpl_DuplicateSurface,
3548 IDirectDrawImpl_EnumDisplayModes,
3549 IDirectDrawImpl_EnumSurfaces,
3550 IDirectDrawImpl_FlipToGDISurface,
3551 IDirectDrawImpl_GetCaps,
3552 IDirectDrawImpl_GetDisplayMode,
3553 IDirectDrawImpl_GetFourCCCodes,
3554 IDirectDrawImpl_GetGDISurface,
3555 IDirectDrawImpl_GetMonitorFrequency,
3556 IDirectDrawImpl_GetScanLine,
3557 IDirectDrawImpl_GetVerticalBlankStatus,
3558 IDirectDrawImpl_Initialize,
3559 IDirectDrawImpl_RestoreDisplayMode,
3560 IDirectDrawImpl_SetCooperativeLevel,
3561 IDirectDrawImpl_SetDisplayMode,
3562 IDirectDrawImpl_WaitForVerticalBlank,
3563 /*** IDirectDraw2 ***/
3564 IDirectDrawImpl_GetAvailableVidMem,
3565 /*** IDirectDraw7 ***/
3566 IDirectDrawImpl_GetSurfaceFromDC,
3567 IDirectDrawImpl_RestoreAllSurfaces,
3568 IDirectDrawImpl_TestCooperativeLevel,
3569 IDirectDrawImpl_GetDeviceIdentifier,
3570 /*** IDirectDraw7 ***/
3571 IDirectDrawImpl_StartModeTest,
3572 IDirectDrawImpl_EvaluateMode
3575 /*****************************************************************************
3576 * IDirectDrawImpl_FindDecl
3578 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
3579 * if none was found.
3581 * This function is in ddraw.c and the DDraw object space because D3D7
3582 * vertex buffers are created using the IDirect3D interface to the ddraw
3583 * object, so they can be valid across D3D devices(theoretically. The ddraw
3584 * object also owns the wined3d device
3588 * fvf: Fvf to find the decl for
3591 * NULL in case of an error, the IWineD3DVertexDeclaration interface for the
3594 *****************************************************************************/
3595 IWineD3DVertexDeclaration *
3596 IDirectDrawImpl_FindDecl(IDirectDrawImpl *This,
3600 IWineD3DVertexDeclaration* pDecl = NULL;
3601 int p, low, high; /* deliberately signed */
3602 struct FvfToDecl *convertedDecls = This->decls;
3604 TRACE("Searching for declaration for fvf %08x... ", fvf);
3607 high = This->numConvertedDecls - 1;
3608 while(low <= high) {
3609 p = (low + high) >> 1;
3611 if(convertedDecls[p].fvf == fvf) {
3612 TRACE("found %p\n", convertedDecls[p].decl);
3613 return convertedDecls[p].decl;
3614 } else if(convertedDecls[p].fvf < fvf) {
3620 TRACE("not found. Creating and inserting at position %d.\n", low);
3622 hr = IWineD3DDevice_CreateVertexDeclarationFromFVF(This->wineD3DDevice,
3624 (IUnknown *) ICOM_INTERFACE(This, IDirectDraw7),
3626 if (hr != S_OK) return NULL;
3628 if(This->declArraySize == This->numConvertedDecls) {
3629 int grow = max(This->declArraySize / 2, 8);
3630 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
3631 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
3632 if(!convertedDecls) {
3633 /* This will destroy it */
3634 IWineD3DVertexDeclaration_Release(pDecl);
3637 This->decls = convertedDecls;
3638 This->declArraySize += grow;
3641 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
3642 convertedDecls[low].decl = pDecl;
3643 convertedDecls[low].fvf = fvf;
3644 This->numConvertedDecls++;
3646 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);