2 * Copyright 1997-2000 Marcus Meissner
3 * Copyright 1998-2000 Lionel Ulmer
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2006 Stefan Dösinger
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
31 #define NONAMELESSUNION
37 #include "wine/exception.h"
42 #include "ddraw_private.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
47 static BOOL IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested, const DDSURFACEDESC2* provided);
48 static HRESULT WINAPI IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This, IDirectDrawSurfaceImpl *primary);
49 static HRESULT WINAPI IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This, DDSURFACEDESC2 *pDDSD, IDirectDrawSurfaceImpl **ppSurf, UINT level);
51 /* Device identifier. Don't relay it to WineD3D */
52 static const DDDEVICEIDENTIFIER2 deviceidentifier =
56 { { 0x00010001, 0x00010001 } },
58 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
59 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
63 /*****************************************************************************
65 *****************************************************************************/
67 /*****************************************************************************
68 * IDirectDraw7::QueryInterface
70 * Queries different interfaces of the DirectDraw object. It can return
71 * IDirectDraw interfaces in version 1, 2, 4 and 7, and IDirect3D interfaces
72 * in version 1, 2, 3 and 7. An IDirect3DDevice can be created with this
74 * The returned interface is AddRef()-ed before it's returned
76 * Rules for QueryInterface:
77 * http://msdn.microsoft.com/library/default.asp? \
78 * url=/library/en-us/com/html/6db17ed8-06e4-4bae-bc26-113176cc7e0e.asp
80 * Used for version 1, 2, 4 and 7
83 * refiid: Interface ID asked for
84 * obj: Used to return the interface pointer
87 * S_OK if an interface was found
88 * E_NOINTERFACE if the requested interface wasn't found
90 *****************************************************************************/
92 IDirectDrawImpl_QueryInterface(IDirectDraw7 *iface,
96 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
98 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(refiid), obj);
100 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
104 return DDERR_INVALIDPARAMS;
106 /* Check DirectDraw Interfaces */
107 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
108 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
110 *obj = ICOM_INTERFACE(This, IDirectDraw7);
111 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
113 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
115 *obj = ICOM_INTERFACE(This, IDirectDraw4);
116 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
118 else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
120 *obj = ICOM_INTERFACE(This, IDirectDraw3);
121 TRACE("(%p) Returning IDirectDraw3 interface at %p\n", This, *obj);
123 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
125 *obj = ICOM_INTERFACE(This, IDirectDraw2);
126 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
128 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
130 *obj = ICOM_INTERFACE(This, IDirectDraw);
131 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
135 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
136 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
137 * who had this idea and why. The older interfaces can query and IDirect3D version
138 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
139 * and messy to implement with the common creation function, so it has been left out here.
141 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
142 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
143 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
144 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
146 /* Check the surface implementation */
147 if(This->ImplType == SURFACE_UNKNOWN)
149 /* Apps may create the IDirect3D Interface before the primary surface.
150 * set the surface implementation */
151 This->ImplType = SURFACE_OPENGL;
152 TRACE("(%p) Choosing OpenGL surfaces because a Direct3D interface was requested\n", This);
154 else if(This->ImplType != SURFACE_OPENGL && DefaultSurfaceType == SURFACE_UNKNOWN)
156 ERR("(%p) The App is requesting a D3D device, but a non-OpenGL surface type was choosen. Prepare for trouble!\n", This);
157 ERR(" (%p) You may want to contact wine-devel for help\n", This);
158 /* Should I assert(0) here??? */
160 else if(This->ImplType != SURFACE_OPENGL)
162 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
163 /* Do not abort here, only reject 3D Device creation */
166 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
168 This->d3dversion = 1;
169 *obj = ICOM_INTERFACE(This, IDirect3D);
170 TRACE(" returning Direct3D interface at %p.\n", *obj);
172 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
174 This->d3dversion = 2;
175 *obj = ICOM_INTERFACE(This, IDirect3D2);
176 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
178 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
180 This->d3dversion = 3;
181 *obj = ICOM_INTERFACE(This, IDirect3D3);
182 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
184 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
186 This->d3dversion = 7;
187 *obj = ICOM_INTERFACE(This, IDirect3D7);
188 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
192 /* Unknown interface */
195 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
196 return E_NOINTERFACE;
199 IUnknown_AddRef( (IUnknown *) *obj );
203 /*****************************************************************************
204 * IDirectDraw7::AddRef
206 * Increases the interfaces refcount, basically
208 * DDraw refcounting is a bit tricky. The different DirectDraw interface
209 * versions have individual refcounts, but the IDirect3D interfaces do not.
210 * All interfaces are from one object, that means calling QueryInterface on an
211 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
212 * IDirectDrawImpl object.
214 * That means all AddRef and Release implementations of IDirectDrawX work
215 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
216 * except of IDirect3D7 which thunks to IDirectDraw7
218 * Returns: The new refcount
220 *****************************************************************************/
222 IDirectDrawImpl_AddRef(IDirectDraw7 *iface)
224 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
225 ULONG ref = InterlockedIncrement(&This->ref7);
227 TRACE("(%p) : incrementing IDirectDraw7 refcount from %u.\n", This, ref -1);
229 if(ref == 1) InterlockedIncrement(&This->numIfaces);
234 /*****************************************************************************
235 * IDirectDrawImpl_Destroy
237 * Destroys a ddraw object if all refcounts are 0. This is to share code
238 * between the IDirectDrawX::Release functions
241 * This: DirectDraw object to destroy
243 *****************************************************************************/
245 IDirectDrawImpl_Destroy(IDirectDrawImpl *This)
249 for(i = 0; i < This->numConvertedDecls; i++)
251 IWineD3DVertexDeclaration_Release(This->decls[i].decl);
253 HeapFree(GetProcessHeap(), 0, This->decls);
255 /* Clear the cooplevel to restore window and display mode */
256 IDirectDraw7_SetCooperativeLevel(ICOM_INTERFACE(This, IDirectDraw7),
260 /* Destroy the device window if we created one */
261 if(This->devicewindow != 0)
263 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
264 DestroyWindow(This->devicewindow);
265 This->devicewindow = 0;
268 /* Unregister the window class */
269 UnregisterClassA(This->classname, 0);
271 EnterCriticalSection(&ddraw_cs);
272 list_remove(&This->ddraw_list_entry);
273 LeaveCriticalSection(&ddraw_cs);
275 /* Release the attached WineD3D stuff */
276 IWineD3DDevice_Release(This->wineD3DDevice);
277 IWineD3D_Release(This->wineD3D);
279 /* Now free the object */
280 HeapFree(GetProcessHeap(), 0, This);
283 /*****************************************************************************
284 * IDirectDraw7::Release
286 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
288 * Returns: The new refcount
289 *****************************************************************************/
291 IDirectDrawImpl_Release(IDirectDraw7 *iface)
293 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
294 ULONG ref = InterlockedDecrement(&This->ref7);
296 TRACE("(%p)->() decrementing IDirectDraw7 refcount from %u.\n", This, ref +1);
300 ULONG ifacecount = InterlockedDecrement(&This->numIfaces);
301 if(ifacecount == 0) IDirectDrawImpl_Destroy(This);
307 /*****************************************************************************
308 * IDirectDraw methods
309 *****************************************************************************/
311 /*****************************************************************************
312 * IDirectDraw7::SetCooperativeLevel
314 * Sets the cooperative level for the DirectDraw object, and the window
315 * assigned to it. The cooperative level determines the general behavior
316 * of the DirectDraw application
318 * Warning: This is quite tricky, as it's not really documented which
319 * cooperative levels can be combined with each other. If a game fails
320 * after this function, try to check the cooperative levels passed on
321 * Windows, and if it returns something different.
323 * If you think that this function caused the failure because it writes a
324 * fixme, be sure to run again with a +ddraw trace.
326 * What is known about cooperative levels (See the ddraw modes test):
327 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
328 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
329 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
330 * DDSCL_FULLSCREEN can be activated
331 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
333 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
334 * DDSCL_SETFOCUSWINDOW (partially),
335 * DDSCL_MULTITHREADED (work in progress)
337 * Unhandled flags, which should be implemented
338 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
339 * expect any difference to a normal window for wine)
340 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
341 * rendering (Possible test case: Half-life)
343 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
345 * These seem not really imporant for wine
346 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
349 * DD_OK if the cooperative level was set successfully
350 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
351 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
352 * (Probably others too, have to investigate)
354 *****************************************************************************/
355 static HRESULT WINAPI
356 IDirectDrawImpl_SetCooperativeLevel(IDirectDraw7 *iface,
360 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
364 FIXME("(%p)->(%p,%08x)\n",This,hwnd,cooplevel);
365 DDRAW_dump_cooperativelevel(cooplevel);
367 /* Get the old window */
368 hr = IWineD3DDevice_GetHWND(This->wineD3DDevice, &window);
371 ERR("IWineD3DDevice::GetHWND failed, hr = %08x\n", hr);
375 /* Tests suggest that we need one of them: */
376 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
380 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
381 return DDERR_INVALIDPARAMS;
384 /* Handle those levels first which set various hwnds */
385 if(cooplevel & DDSCL_SETFOCUSWINDOW)
387 /* This isn't compatible with a lot of flags */
388 if(cooplevel & ( DDSCL_MULTITHREADED |
393 DDSCL_SETDEVICEWINDOW |
398 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
399 return DDERR_INVALIDPARAMS;
401 else if( (This->cooperative_level & DDSCL_FULLSCREEN) && window)
403 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET\n");
404 return DDERR_HWNDALREADYSET;
407 This->focuswindow = hwnd;
408 /* Won't use the hwnd param for anything else */
411 /* Use the focus window for drawing too */
412 IWineD3DDevice_SetHWND(This->wineD3DDevice, This->focuswindow);
414 /* Destroy the device window, if we have one */
415 if(This->devicewindow)
417 DestroyWindow(This->devicewindow);
418 This->devicewindow = NULL;
421 /* DDSCL_NORMAL or DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE */
422 if(cooplevel & DDSCL_NORMAL)
424 /* Can't coexist with fullscreen or exclusive */
425 if(cooplevel & (DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE) )
427 TRACE("(%p) DDSCL_NORMAL is not compative with DDSCL_FULLSCREEN or DDSCL_EXCLUSIVE\n", This);
428 return DDERR_INVALIDPARAMS;
431 /* Switching from fullscreen? */
432 if(This->cooperative_level & DDSCL_FULLSCREEN)
434 /* Restore the display mode */
435 IDirectDraw7_RestoreDisplayMode(iface);
437 This->cooperative_level &= ~DDSCL_FULLSCREEN;
438 This->cooperative_level &= ~DDSCL_EXCLUSIVE;
439 This->cooperative_level &= ~DDSCL_ALLOWMODEX;
442 /* Don't override focus windows or private device windows */
444 !(This->focuswindow) &&
445 !(This->devicewindow) &&
448 IWineD3DDevice_SetHWND(This->wineD3DDevice, hwnd);
451 IWineD3DDevice_SetFullscreen(This->wineD3DDevice,
454 else if(cooplevel & DDSCL_FULLSCREEN)
456 /* Needs DDSCL_EXCLUSIVE */
457 if(!(cooplevel & DDSCL_EXCLUSIVE) )
459 TRACE("(%p) DDSCL_FULLSCREEN needs DDSCL_EXCLUSIVE\n", This);
460 return DDERR_INVALIDPARAMS;
465 TRACE("(%p) DDSCL_FULLSCREEN needs a HWND\n", This);
466 return DDERR_INVALIDPARAMS;
470 This->cooperative_level &= ~DDSCL_NORMAL;
471 IWineD3DDevice_SetFullscreen(This->wineD3DDevice,
474 /* Don't override focus windows or private device windows */
476 !(This->focuswindow) &&
477 !(This->devicewindow) &&
480 IWineD3DDevice_SetHWND(This->wineD3DDevice, hwnd);
483 else if(cooplevel & DDSCL_EXCLUSIVE)
485 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN\n", This);
486 return DDERR_INVALIDPARAMS;
489 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
491 /* Don't create a device window if a focus window is set */
492 if( !(This->focuswindow) )
494 HWND devicewindow = CreateWindowExA(0, This->classname, "DDraw device window",
496 GetSystemMetrics(SM_CXSCREEN),
497 GetSystemMetrics(SM_CYSCREEN),
498 NULL, NULL, GetModuleHandleA(0), NULL);
500 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
501 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
503 IWineD3DDevice_SetHWND(This->wineD3DDevice, devicewindow);
504 This->devicewindow = devicewindow;
508 if(cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
510 FIXME("DirectDraw is not thread safe yet\n");
512 /* Enable thread safety in wined3d */
513 IWineD3DDevice_SetMultithreaded(This->wineD3DDevice);
516 /* Unhandled flags */
517 if(cooplevel & DDSCL_ALLOWREBOOT)
518 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
519 if(cooplevel & DDSCL_ALLOWMODEX)
520 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
521 if(cooplevel & DDSCL_FPUSETUP)
522 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
523 if(cooplevel & DDSCL_FPUPRESERVE)
524 WARN("(%p) Unhandled flag DDSCL_FPUPRESERVE, harmless\n", This);
526 /* Store the cooperative_level */
527 This->cooperative_level |= cooplevel;
528 TRACE("SetCooperativeLevel retuning DD_OK\n");
532 /*****************************************************************************
533 * IDirectDraw7::SetDisplayMode
535 * Sets the display screen resolution, color depth and refresh frequency
536 * when in fullscreen mode (in theory).
537 * Possible return values listed in the SDK suggest that this method fails
538 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
539 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
540 * It seems to be valid to pass 0 for With and Height, this has to be tested
541 * It could mean that the current video mode should be left as-is. (But why
545 * Height, Width: Screen dimension
546 * BPP: Color depth in Bits per pixel
547 * Refreshrate: Screen refresh rate
553 *****************************************************************************/
554 static HRESULT WINAPI
555 IDirectDrawImpl_SetDisplayMode(IDirectDraw7 *iface,
562 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
563 WINED3DDISPLAYMODE Mode;
565 TRACE("(%p)->(%d,%d,%d,%d,%x: Relay!\n", This, Width, Height, BPP, RefreshRate, Flags);
567 if( !Width || !Height )
569 ERR("Width=%d, Height=%d, what to do?\n", Width, Height);
570 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
574 /* Check the exclusive mode
575 if(!(This->cooperative_level & DDSCL_EXCLUSIVE))
576 return DDERR_NOEXCLUSIVEMODE;
577 * This is WRONG. Don't know if the SDK is completely
578 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
579 * is returned, but Half-Life 1.1.1.1 (Steam version)
584 Mode.Height = Height;
585 Mode.RefreshRate = RefreshRate;
588 case 8: Mode.Format = WINED3DFMT_P8; break;
589 case 15: Mode.Format = WINED3DFMT_X1R5G5B5; break;
590 case 16: Mode.Format = WINED3DFMT_R5G6B5; break;
591 case 24: Mode.Format = WINED3DFMT_R8G8B8; break;
592 case 32: Mode.Format = WINED3DFMT_A8R8G8B8; break;
595 /* TODO: The possible return values from msdn suggest that
596 * the screen mode can't be changed if a surface is locked
597 * or some drawing is in progress
600 /* TODO: Lose the primary surface */
601 hr = IWineD3DDevice_SetDisplayMode(This->wineD3DDevice,
602 0, /* First swapchain */
606 case WINED3DERR_NOTAVAILABLE: return DDERR_INVALIDMODE;
611 /*****************************************************************************
612 * IDirectDraw7::RestoreDisplayMode
614 * Restores the display mode to what it was at creation time. Basically.
616 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
617 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
618 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
619 * -> DD_1 is released. The screen should be left at 1024x768x32.
620 * -> DD_2 is released. The screen should be set to 1400x1050x32
621 * This case is unhandled right now, but Empire Earth does it this way.
622 * (But perhaps there is something in SetCooperativeLevel to prevent this)
624 * The msdn says that this method resets the display mode to what it was before
625 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
629 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
631 *****************************************************************************/
632 static HRESULT WINAPI
633 IDirectDrawImpl_RestoreDisplayMode(IDirectDraw7 *iface)
635 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
636 TRACE("(%p)\n", This);
638 return IDirectDraw7_SetDisplayMode(ICOM_INTERFACE(This, IDirectDraw7),
646 /*****************************************************************************
647 * IDirectDraw7::GetCaps
649 * Returns the drives capabilities
651 * Used for version 1, 2, 4 and 7
654 * DriverCaps: Structure to write the Hardware accelerated caps to
655 * HelCaps: Structure to write the emulation caps to
658 * This implementation returns DD_OK only
660 *****************************************************************************/
661 static HRESULT WINAPI
662 IDirectDrawImpl_GetCaps(IDirectDraw7 *iface,
666 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
667 TRACE("(%p)->(%p,%p)\n", This, DriverCaps, HELCaps);
669 /* One structure must be != NULL */
670 if( (!DriverCaps) && (!HELCaps) )
672 ERR("(%p) Invalid params to IDirectDrawImpl_GetCaps\n", This);
673 return DDERR_INVALIDPARAMS;
678 DD_STRUCT_COPY_BYSIZE(DriverCaps, &This->caps);
681 TRACE("Driver Caps :\n");
682 DDRAW_dump_DDCAPS(DriverCaps);
688 DD_STRUCT_COPY_BYSIZE(HELCaps, &This->caps);
691 TRACE("HEL Caps :\n");
692 DDRAW_dump_DDCAPS(HELCaps);
699 /*****************************************************************************
700 * IDirectDraw7::Compact
702 * No idea what it does, MSDN says it's not implemented.
705 * DD_OK, but this is unchecked
707 *****************************************************************************/
708 static HRESULT WINAPI
709 IDirectDrawImpl_Compact(IDirectDraw7 *iface)
711 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
712 TRACE("(%p)\n", This);
717 /*****************************************************************************
718 * IDirectDraw7::GetDisplayMode
720 * Returns information about the current display mode
722 * Exists in Version 1, 2, 4 and 7
725 * DDSD: Address of a surface description structure to write the info to
730 *****************************************************************************/
731 static HRESULT WINAPI
732 IDirectDrawImpl_GetDisplayMode(IDirectDraw7 *iface,
733 DDSURFACEDESC2 *DDSD)
735 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
737 WINED3DDISPLAYMODE Mode;
739 TRACE("(%p)->(%p): Relay\n", This, DDSD);
741 /* This seems sane */
744 return DDERR_INVALIDPARAMS;
747 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
748 * so one method can be used for all versions (Hopefully)
750 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
755 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
760 memset(DDSD, 0, Size);
763 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
764 DDSD->dwWidth = Mode.Width;
765 DDSD->dwHeight = Mode.Height;
766 DDSD->u2.dwRefreshRate = 60;
767 DDSD->ddsCaps.dwCaps = 0;
768 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
769 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, Mode.Format);
770 DDSD->u1.lPitch = Mode.Width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
774 TRACE("Returning surface desc :\n");
775 DDRAW_dump_surface_desc(DDSD);
781 /*****************************************************************************
782 * IDirectDraw7::GetFourCCCodes
784 * Returns an array of supported FourCC codes.
786 * Exists in Version 1, 2, 4 and 7
789 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
790 * of enumerated codes
791 * Codes: Pointer to an array of DWORDs where the supported codes are written
795 * Always returns DD_OK, as it's a stub for now
797 *****************************************************************************/
798 static HRESULT WINAPI
799 IDirectDrawImpl_GetFourCCCodes(IDirectDraw7 *iface,
800 DWORD *NumCodes, DWORD *Codes)
802 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
803 FIXME("(%p)->(%p, %p): Stub!\n", This, NumCodes, Codes);
805 if(NumCodes) *NumCodes = 0;
810 /*****************************************************************************
811 * IDirectDraw7::GetMonitorFrequency
813 * Returns the monitor's frequency
815 * Exists in Version 1, 2, 4 and 7
818 * Freq: Pointer to a DWORD to write the frequency to
821 * Always returns DD_OK
823 *****************************************************************************/
824 static HRESULT WINAPI
825 IDirectDrawImpl_GetMonitorFrequency(IDirectDraw7 *iface,
828 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
829 TRACE("(%p)->(%p)\n", This, Freq);
831 /* Ideally this should be in WineD3D, as it concerns the screen setup,
832 * but for now this should make the games happy
838 /*****************************************************************************
839 * IDirectDraw7::GetVerticalBlankStatus
841 * Returns the Vertical blank status of the monitor. This should be in WineD3D
842 * too basically, but as it's a semi stub, I didn't create a function there
845 * status: Pointer to a BOOL to be filled with the vertical blank status
849 * DDERR_INVALIDPARAMS if status is NULL
851 *****************************************************************************/
852 static HRESULT WINAPI
853 IDirectDrawImpl_GetVerticalBlankStatus(IDirectDraw7 *iface,
856 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
857 TRACE("(%p)->(%p)\n", This, status);
859 /* This looks sane, the MSDN suggests it too */
860 if(!status) return DDERR_INVALIDPARAMS;
862 *status = This->fake_vblank;
863 This->fake_vblank = !This->fake_vblank;
867 /*****************************************************************************
868 * IDirectDraw7::GetAvailableVidMem
870 * Returns the total and free video memory
873 * Caps: Specifies the memory type asked for
874 * total: Pointer to a DWORD to be filled with the total memory
875 * free: Pointer to a DWORD to be filled with the free memory
879 * DDERR_INVALIDPARAMS of free and total are NULL
881 *****************************************************************************/
882 static HRESULT WINAPI
883 IDirectDrawImpl_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total, DWORD *free)
885 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
886 TRACE("(%p)->(%p, %p, %p)\n", This, Caps, total, free);
890 TRACE("(%p) Asked for memory with description: ", This);
891 DDRAW_dump_DDSCAPS2(Caps);
895 /* Todo: System memory vs local video memory vs non-local video memory
896 * The MSDN also mentions differences between texture memory and other
897 * resources, but that's not important
900 if( (!total) && (!free) ) return DDERR_INVALIDPARAMS;
902 if(total) *total = This->total_vidmem;
903 if(free) *free = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
908 /*****************************************************************************
909 * IDirectDraw7::Initialize
911 * Initializes a DirectDraw interface.
914 * GUID: Interface identifier. Well, don't know what this is really good
918 * Returns DD_OK on the first call,
919 * DDERR_ALREADYINITIALIZED on repeated calls
921 *****************************************************************************/
922 static HRESULT WINAPI
923 IDirectDrawImpl_Initialize(IDirectDraw7 *iface,
926 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
927 TRACE("(%p)->(%s): No-op\n", This, debugstr_guid(Guid));
929 if(This->initialized)
931 return DDERR_ALREADYINITIALIZED;
939 /*****************************************************************************
940 * IDirectDraw7::FlipToGDISurface
942 * "Makes the surface that the GDI writes to the primary surface"
943 * Looks like some windows specific thing we don't have to care about.
944 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
945 * show error boxes ;)
946 * Well, just return DD_OK.
949 * Always returns DD_OK
951 *****************************************************************************/
952 static HRESULT WINAPI
953 IDirectDrawImpl_FlipToGDISurface(IDirectDraw7 *iface)
955 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
956 TRACE("(%p)\n", This);
961 /*****************************************************************************
962 * IDirectDraw7::WaitForVerticalBlank
964 * This method allows applications to get in sync with the vertical blank
966 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
967 * redraw the screen, most likely because of this stub
970 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
971 * or DDWAITVB_BLOCKEND
972 * h: Not used, according to MSDN
975 * Always returns DD_OK
977 *****************************************************************************/
978 static HRESULT WINAPI
979 IDirectDrawImpl_WaitForVerticalBlank(IDirectDraw7 *iface,
983 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
984 FIXME("(%p)->(%x,%p): Stub\n", This, Flags, h);
986 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
987 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
988 return DDERR_UNSUPPORTED; /* unchecked */
993 /*****************************************************************************
994 * IDirectDraw7::GetScanLine
996 * Returns the scan line that is being drawn on the monitor
999 * Scanline: Address to write the scan line value to
1002 * Always returns DD_OK
1004 *****************************************************************************/
1005 static HRESULT WINAPI IDirectDrawImpl_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1007 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1008 static BOOL hide = FALSE;
1009 WINED3DDISPLAYMODE Mode;
1011 /* This function is called often, so print the fixme only once */
1014 FIXME("(%p)->(%p): Semi-Stub\n", This, Scanline);
1018 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1022 /* Fake the line sweeping of the monitor */
1023 /* FIXME: We should synchronize with a source to keep the refresh rate */
1024 *Scanline = This->cur_scanline++;
1025 /* Assume 20 scan lines in the vertical blank */
1026 if (This->cur_scanline >= Mode.Height + 20)
1027 This->cur_scanline = 0;
1032 /*****************************************************************************
1033 * IDirectDraw7::TestCooperativeLevel
1035 * Informs the application about the state of the video adapter, depending
1036 * on the cooperative level
1039 * DD_OK if the device is in a sane state
1040 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1041 * if the state is not correct(See below)
1043 *****************************************************************************/
1044 static HRESULT WINAPI
1045 IDirectDrawImpl_TestCooperativeLevel(IDirectDraw7 *iface)
1047 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1049 TRACE("(%p)\n", This);
1051 /* Description from MSDN:
1052 * For fullscreen apps return DDERR_NOEXCLUSIVEMODE if the user switched
1053 * away from the app with e.g. alt-tab. Windowed apps receive
1054 * DDERR_EXCLUSIVEMODEALREADYSET if another application created a
1055 * DirectDraw object in exclusive mode. DDERR_WRONGMODE is returned,
1056 * when the video mode has changed
1059 hr = IWineD3DDevice_TestCooperativeLevel(This->wineD3DDevice);
1061 /* Fix the result value. These values are mapped from their
1066 case WINED3DERR_DEVICELOST:
1067 if(This->cooperative_level & DDSCL_EXCLUSIVE)
1069 return DDERR_NOEXCLUSIVEMODE;
1073 return DDERR_EXCLUSIVEMODEALREADYSET;
1076 case WINED3DERR_DEVICENOTRESET:
1082 case WINED3DERR_DRIVERINTERNALERROR:
1084 ERR("(%p) Unexpected return value %08x from wineD3D, "
1085 " returning DD_OK\n", This, hr);
1091 /*****************************************************************************
1092 * IDirectDraw7::GetGDISurface
1094 * Returns the surface that GDI is treating as the primary surface.
1095 * For Wine this is the front buffer
1098 * GDISurface: Address to write the surface pointer to
1101 * DD_OK if the surface was found
1102 * DDERR_NOTFOUND if the GDI surface wasn't found
1104 *****************************************************************************/
1105 static HRESULT WINAPI
1106 IDirectDrawImpl_GetGDISurface(IDirectDraw7 *iface,
1107 IDirectDrawSurface7 **GDISurface)
1109 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1110 IWineD3DSurface *Surf;
1111 IDirectDrawSurface7 *ddsurf;
1114 TRACE("(%p)->(%p)\n", This, GDISurface);
1116 /* Get the back buffer from the wineD3DDevice and search its
1117 * attached surfaces for the front buffer
1119 hr = IWineD3DDevice_GetBackBuffer(This->wineD3DDevice,
1121 0, /* first back buffer*/
1122 WINED3DBACKBUFFER_TYPE_MONO,
1125 if( (hr != D3D_OK) ||
1128 ERR("IWineD3DDevice::GetBackBuffer failed\n");
1129 return DDERR_NOTFOUND;
1132 /* GetBackBuffer AddRef()ed the surface, release it */
1133 IWineD3DSurface_Release(Surf);
1135 IWineD3DSurface_GetParent(Surf,
1136 (IUnknown **) &ddsurf);
1137 IDirectDrawSurface7_Release(ddsurf); /* For the GetParent */
1139 /* Find the front buffer */
1140 ddsCaps.dwCaps = DDSCAPS_FRONTBUFFER;
1141 hr = IDirectDrawSurface7_GetAttachedSurface(ddsurf,
1146 ERR("IDirectDrawSurface7::GetAttachedSurface failed, hr = %x\n", hr);
1149 /* The AddRef is OK this time */
1153 /*****************************************************************************
1154 * IDirectDraw7::EnumDisplayModes
1156 * Enumerates the supported Display modes. The modes can be filtered with
1157 * the DDSD parameter.
1160 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES
1161 * DDSD: Surface description to filter the modes
1162 * Context: Pointer passed back to the callback function
1163 * cb: Application-provided callback function
1167 * DDERR_INVALIDPARAMS if the callback wasn't set
1169 *****************************************************************************/
1170 static HRESULT WINAPI
1171 IDirectDrawImpl_EnumDisplayModes(IDirectDraw7 *iface,
1173 DDSURFACEDESC2 *DDSD,
1175 LPDDENUMMODESCALLBACK2 cb)
1177 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1178 unsigned int modenum, fmt;
1179 WINED3DFORMAT pixelformat = WINED3DFMT_UNKNOWN;
1180 WINED3DDISPLAYMODE mode;
1181 DDSURFACEDESC2 callback_sd;
1183 WINED3DFORMAT checkFormatList[] =
1186 WINED3DFMT_A8R8G8B8,
1187 WINED3DFMT_X8R8G8B8,
1189 WINED3DFMT_X1R5G5B5,
1190 WINED3DFMT_A1R5G5B5,
1191 WINED3DFMT_A4R4G4B4,
1193 WINED3DFMT_A8R3G3B2,
1194 WINED3DFMT_X4R4G4B4,
1195 WINED3DFMT_A2B10G10R10,
1196 WINED3DFMT_A8B8G8R8,
1197 WINED3DFMT_X8B8G8R8,
1198 WINED3DFMT_A2R10G10B10,
1203 TRACE("(%p)->(%p,%p,%p): Relay\n", This, DDSD, Context, cb);
1205 /* This looks sane */
1206 if(!cb) return DDERR_INVALIDPARAMS;
1210 if ((DDSD->dwFlags & DDSD_PIXELFORMAT) && (DDSD->u4.ddpfPixelFormat.dwFlags & DDPF_RGB) )
1211 pixelformat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
1214 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
1216 if(pixelformat != WINED3DFMT_UNKNOWN && checkFormatList[fmt] != pixelformat)
1222 while(IWineD3D_EnumAdapterModes(This->wineD3D,
1223 WINED3DADAPTER_DEFAULT,
1224 checkFormatList[fmt],
1226 &mode) == WINED3D_OK)
1230 if(DDSD->dwFlags & DDSD_WIDTH && mode.Width != DDSD->dwWidth) continue;
1231 if(DDSD->dwFlags & DDSD_HEIGHT && mode.Height != DDSD->dwHeight) continue;
1234 memset(&callback_sd, 0, sizeof(callback_sd));
1235 callback_sd.dwSize = sizeof(callback_sd);
1236 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1238 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH;
1239 if(Flags & DDEDM_REFRESHRATES)
1241 callback_sd.dwFlags |= DDSD_REFRESHRATE;
1242 callback_sd.u2.dwRefreshRate = mode.RefreshRate;
1245 callback_sd.dwWidth = mode.Width;
1246 callback_sd.dwHeight = mode.Height;
1248 PixelFormat_WineD3DtoDD(&callback_sd.u4.ddpfPixelFormat, mode.Format);
1250 TRACE("Enumerating %dx%d@%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount);
1252 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
1254 TRACE("Application asked to terminate the enumeration\n");
1260 TRACE("End of enumeration\n");
1264 /*****************************************************************************
1265 * IDirectDraw7::EvaluateMode
1267 * Used with IDirectDraw7::StartModeTest to test video modes.
1268 * EvaluateMode is used to pass or fail a mode, and continue with the next
1272 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
1273 * Timeout: Returns the amount of seconds left before the mode would have
1274 * been failed automatically
1277 * This implementation always DD_OK, because it's a stub
1279 *****************************************************************************/
1280 static HRESULT WINAPI
1281 IDirectDrawImpl_EvaluateMode(IDirectDraw7 *iface,
1285 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1286 FIXME("(%p)->(%d,%p): Stub!\n", This, Flags, Timeout);
1288 /* When implementing this, implement it in WineD3D */
1293 /*****************************************************************************
1294 * IDirectDraw7::GetDeviceIdentifier
1296 * Returns the device identifier, which gives information about the driver
1297 * Our device identifier is defined at the beginning of this file.
1300 * DDDI: Address for the returned structure
1301 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
1304 * On success it returns DD_OK
1305 * DDERR_INVALIDPARAMS if DDDI is NULL
1307 *****************************************************************************/
1308 static HRESULT WINAPI
1309 IDirectDrawImpl_GetDeviceIdentifier(IDirectDraw7 *iface,
1310 DDDEVICEIDENTIFIER2 *DDDI,
1313 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1314 TRACE("(%p)->(%p,%08x)\n", This, DDDI, Flags);
1317 return DDERR_INVALIDPARAMS;
1319 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
1320 * host adapter, if there's a secondary 3D adapter. This doesn't apply
1321 * to any modern hardware, nor is it interesting for Wine, so ignore it
1324 *DDDI = deviceidentifier;
1328 /*****************************************************************************
1329 * IDirectDraw7::GetSurfaceFromDC
1331 * Returns the Surface for a GDI device context handle.
1332 * Is this related to IDirectDrawSurface::GetDC ???
1335 * hdc: hdc to return the surface for
1336 * Surface: Address to write the surface pointer to
1339 * Always returns DD_OK because it's a stub
1341 *****************************************************************************/
1342 static HRESULT WINAPI
1343 IDirectDrawImpl_GetSurfaceFromDC(IDirectDraw7 *iface,
1345 IDirectDrawSurface7 **Surface)
1347 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1348 FIXME("(%p)->(%p,%p): Stub!\n", This, hdc, Surface);
1350 /* Implementation idea if needed: Loop through all surfaces and compare
1351 * their hdc with hdc. Implement it in WineD3D! */
1352 return DDERR_NOTFOUND;
1355 /*****************************************************************************
1356 * IDirectDraw7::RestoreAllSurfaces
1358 * Calls the restore method of all surfaces
1363 * Always returns DD_OK because it's a stub
1365 *****************************************************************************/
1366 static HRESULT WINAPI
1367 IDirectDrawImpl_RestoreAllSurfaces(IDirectDraw7 *iface)
1369 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1370 FIXME("(%p): Stub\n", This);
1372 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
1373 * get their parent and call their restore method. Do not implement
1374 * it in WineD3D, as restoring a surface means re-creating the
1380 /*****************************************************************************
1381 * IDirectDraw7::StartModeTest
1383 * Tests the specified video modes to update the system registry with
1384 * refresh rate information. StartModeTest starts the mode test,
1385 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
1386 * isn't called within 15 seconds, the mode is failed automatically
1388 * As refresh rates are handled by the X server, I don't think this
1389 * Method is important
1392 * Modes: An array of mode specifications
1393 * NumModes: The number of modes in Modes
1394 * Flags: Some flags...
1397 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
1398 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
1401 *****************************************************************************/
1402 static HRESULT WINAPI
1403 IDirectDrawImpl_StartModeTest(IDirectDraw7 *iface,
1408 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1409 WARN("(%p)->(%p, %d, %x): Semi-Stub, most likely harmless\n", This, Modes, NumModes, Flags);
1411 /* This looks sane */
1412 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
1414 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
1415 * As it is not, DDERR_TESTFINISHED is returned
1416 * (hopefully that's correct
1418 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
1419 * well, that value doesn't (yet) exist in the wine headers, so ignore it
1425 /*****************************************************************************
1426 * IDirectDrawImpl_RecreateSurfacesCallback
1428 * Enumeration callback for IDirectDrawImpl_RecreateAllSurfaces.
1429 * It re-recreates the WineD3DSurface. It's pretty straightforward
1431 *****************************************************************************/
1433 IDirectDrawImpl_RecreateSurfacesCallback(IDirectDrawSurface7 *surf,
1434 DDSURFACEDESC2 *desc,
1437 IDirectDrawSurfaceImpl *surfImpl = ICOM_OBJECT(IDirectDrawSurfaceImpl,
1438 IDirectDrawSurface7,
1440 IDirectDrawImpl *This = surfImpl->ddraw;
1442 IParentImpl *parImpl = NULL;
1443 IWineD3DSurface *wineD3DSurface;
1446 IWineD3DClipper *clipper = NULL;
1448 WINED3DSURFACE_DESC Desc;
1449 WINED3DFORMAT Format;
1450 WINED3DRESOURCETYPE Type;
1455 WINED3DMULTISAMPLE_TYPE MultiSampleType;
1456 DWORD MultiSampleQuality;
1460 TRACE("(%p): Enumerated Surface %p\n", This, surfImpl);
1462 /* For the enumeration */
1463 IDirectDrawSurface7_Release(surf);
1465 if(surfImpl->ImplType == This->ImplType) return DDENUMRET_OK; /* Continue */
1467 /* Get the objects */
1468 wineD3DSurface = surfImpl->WineD3DSurface;
1469 IWineD3DSurface_GetParent(wineD3DSurface, &Parent);
1470 IUnknown_Release(Parent); /* For the getParent */
1472 /* Is the parent an IParent interface? */
1473 if(IUnknown_QueryInterface(Parent, &IID_IParent, &tmp) == S_OK)
1475 /* It is a IParent interface! */
1476 IUnknown_Release(Parent); /* For the QueryInterface */
1477 parImpl = ICOM_OBJECT(IParentImpl, IParent, Parent);
1478 /* Release the reference the parent interface is holding */
1479 IWineD3DSurface_Release(wineD3DSurface);
1482 /* get the clipper */
1483 IWineD3DSurface_GetClipper(wineD3DSurface, &clipper);
1485 /* Get the surface properties */
1486 Desc.Format = &Format;
1488 Desc.Usage = &Usage;
1491 Desc.MultiSampleType = &MultiSampleType;
1492 Desc.MultiSampleQuality = &MultiSampleQuality;
1493 Desc.Width = &Width;
1494 Desc.Height = &Height;
1496 hr = IWineD3DSurface_GetDesc(wineD3DSurface, &Desc);
1497 if(hr != D3D_OK) return hr;
1499 /* Create the new surface */
1500 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice,
1501 Width, Height, Format,
1502 TRUE /* Lockable */,
1503 FALSE /* Discard */,
1504 surfImpl->mipmap_level,
1505 &surfImpl->WineD3DSurface,
1511 0 /* SharedHandle */,
1518 IWineD3DSurface_SetClipper(surfImpl->WineD3DSurface, clipper);
1520 /* Update the IParent if it exists */
1523 parImpl->child = (IUnknown *) surfImpl->WineD3DSurface;
1524 /* Add a reference for the IParent */
1525 IWineD3DSurface_AddRef(surfImpl->WineD3DSurface);
1527 /* TODO: Copy the surface content, except for render targets */
1529 if(IWineD3DSurface_Release(wineD3DSurface) == 0)
1530 TRACE("Surface released successful, next surface\n");
1532 ERR("Something's still holding the old WineD3DSurface\n");
1534 surfImpl->ImplType = This->ImplType;
1538 IWineD3DClipper_Release(clipper);
1540 return DDENUMRET_OK;
1543 /*****************************************************************************
1544 * IDirectDrawImpl_RecreateAllSurfaces
1546 * A function, that converts all wineD3DSurfaces to the new implementation type
1547 * It enumerates all surfaces with IWineD3DDevice::EnumSurfaces, creates a
1548 * new WineD3DSurface, copies the content and releases the old surface
1550 *****************************************************************************/
1552 IDirectDrawImpl_RecreateAllSurfaces(IDirectDrawImpl *This)
1554 DDSURFACEDESC2 desc;
1555 TRACE("(%p): Switch to implementation %d\n", This, This->ImplType);
1557 if(This->ImplType != SURFACE_OPENGL && This->d3d_initialized)
1559 /* Should happen almost never */
1560 FIXME("(%p) Switching to non-opengl surfaces with d3d started. Is this a bug?\n", This);
1562 IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroyDepthStencilSurface, D3D7CB_DestroySwapChain);
1564 /* Contrary: D3D starting is handled by the caller, because it knows the render target */
1566 memset(&desc, 0, sizeof(desc));
1567 desc.dwSize = sizeof(desc);
1569 return IDirectDraw7_EnumSurfaces(ICOM_INTERFACE(This, IDirectDraw7),
1573 IDirectDrawImpl_RecreateSurfacesCallback);
1576 /*****************************************************************************
1577 * D3D7CB_CreateSurface
1579 * Callback function for IDirect3DDevice_CreateTexture. It searches for the
1580 * correct mipmap sublevel, and returns it to WineD3D.
1581 * The surfaces are created already by IDirectDraw7::CreateSurface
1584 * With, Height: With and height of the surface
1585 * Format: The requested format
1586 * Usage, Pool: D3DUSAGE and D3DPOOL of the surface
1587 * level: The mipmap level
1588 * Face: The cube map face type
1589 * Surface: Pointer to pass the created surface back at
1590 * SharedHandle: NULL
1595 *****************************************************************************/
1596 static HRESULT WINAPI
1597 D3D7CB_CreateSurface(IUnknown *device,
1598 IUnknown *pSuperior,
1599 UINT Width, UINT Height,
1600 WINED3DFORMAT Format,
1601 DWORD Usage, WINED3DPOOL Pool, UINT level,
1602 WINED3DCUBEMAP_FACES Face,
1603 IWineD3DSurface **Surface,
1604 HANDLE *SharedHandle)
1606 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
1607 IDirectDrawSurfaceImpl *surf = NULL;
1609 DDSCAPS2 searchcaps = This->tex_root->surface_desc.ddsCaps;
1610 TRACE("(%p) call back. surf=%p. Face %d level %d\n", device, This->tex_root, Face, level);
1612 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
1615 case WINED3DCUBEMAP_FACE_POSITIVE_X:
1616 TRACE("Asked for positive x\n");
1617 if(searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP) {
1618 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
1620 surf = This->tex_root; break;
1621 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
1622 TRACE("Asked for negative x\n");
1623 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
1624 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
1625 TRACE("Asked for positive y\n");
1626 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
1627 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
1628 TRACE("Asked for negative y\n");
1629 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
1630 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
1631 TRACE("Asked for positive z\n");
1632 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
1633 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
1634 TRACE("Asked for negative z\n");
1635 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
1636 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
1641 IDirectDrawSurface7 *attached;
1642 IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(This->tex_root, IDirectDrawSurface7),
1645 surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attached);
1646 IDirectDrawSurface7_Release(attached);
1648 if(!surf) ERR("root search surface not found\n");
1650 /* Find the wanted mipmap. There are enough mipmaps in the chain */
1653 IDirectDrawSurface7 *attached;
1654 IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(surf, IDirectDrawSurface7),
1657 if(!attached) ERR("Surface not found\n");
1658 surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attached);
1659 IDirectDrawSurface7_Release(attached);
1663 /* Return the surface */
1664 *Surface = surf->WineD3DSurface;
1666 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *Surface, surf);
1670 ULONG WINAPI D3D7CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
1671 IUnknown* swapChainParent;
1672 TRACE("(%p) call back\n", pSwapChain);
1674 IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
1675 IUnknown_Release(swapChainParent);
1676 return IUnknown_Release(swapChainParent);
1679 ULONG WINAPI D3D7CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
1680 IUnknown* surfaceParent;
1681 TRACE("(%p) call back\n", pSurface);
1683 IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
1684 IUnknown_Release(surfaceParent);
1685 return IUnknown_Release(surfaceParent);
1688 /*****************************************************************************
1689 * IDirectDrawImpl_CreateNewSurface
1691 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
1692 * with the passed parameters.
1695 * DDSD: Description of the surface to create
1696 * Surf: Address to store the interface pointer at
1701 *****************************************************************************/
1702 static HRESULT WINAPI
1703 IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This,
1704 DDSURFACEDESC2 *pDDSD,
1705 IDirectDrawSurfaceImpl **ppSurf,
1709 UINT Width = 0, Height = 0;
1710 WINED3DFORMAT Format = WINED3DFMT_UNKNOWN;
1711 WINED3DRESOURCETYPE ResType = WINED3DRTYPE_SURFACE;
1713 WINED3DSURFTYPE ImplType = This->ImplType;
1714 WINED3DSURFACE_DESC Desc;
1716 IParentImpl *parImpl = NULL;
1717 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
1719 /* Dummies for GetDesc */
1720 WINED3DPOOL dummy_d3dpool;
1721 WINED3DMULTISAMPLE_TYPE dummy_mst;
1725 if (TRACE_ON(ddraw))
1727 TRACE(" (%p) Requesting surface desc :\n", This);
1728 DDRAW_dump_surface_desc(pDDSD);
1731 /* Select the surface type, if it wasn't choosen yet */
1732 if(ImplType == SURFACE_UNKNOWN)
1734 /* Use GL Surfaces if a D3DDEVICE Surface is requested */
1735 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1737 TRACE("(%p) Choosing GL surfaces because a 3DDEVICE Surface was requested\n", This);
1738 ImplType = SURFACE_OPENGL;
1741 /* Otherwise use GDI surfaces for now */
1744 TRACE("(%p) Choosing GDI surfaces for 2D rendering\n", This);
1745 ImplType = SURFACE_GDI;
1748 /* Policy if all surface implementations are available:
1749 * First, check if a default type was set with winecfg. If not,
1750 * try Xrender surfaces, and use them if they work. Next, check if
1751 * accelerated OpenGL is available, and use GL surfaces in this
1752 * case. If all else fails, use GDI surfaces. If a 3DDEVICE surface
1753 * was created, always use OpenGL surfaces.
1755 * (Note: Xrender surfaces are not implemented for now, the
1756 * unaccelerated implementation uses GDI to render in Software)
1759 /* Store the type. If it needs to be changed, all WineD3DSurfaces have to
1760 * be re-created. This could be done with IDirectDrawSurface7::Restore
1762 This->ImplType = ImplType;
1766 if((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE ) &&
1767 (This->ImplType != SURFACE_OPENGL ) && DefaultSurfaceType == SURFACE_UNKNOWN)
1769 /* We have to change to OpenGL,
1770 * and re-create all WineD3DSurfaces
1772 ImplType = SURFACE_OPENGL;
1773 This->ImplType = ImplType;
1774 TRACE("(%p) Re-creating all surfaces\n", This);
1775 IDirectDrawImpl_RecreateAllSurfaces(This);
1776 TRACE("(%p) Done recreating all surfaces\n", This);
1778 else if(This->ImplType != SURFACE_OPENGL && pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1780 WARN("The application requests a 3D capable surface, but a non-opengl surface was set in the registry\n");
1781 /* Do not fail surface creation, only fail 3D device creation */
1785 /* Get the correct wined3d usage */
1786 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE |
1787 DDSCAPS_BACKBUFFER |
1788 DDSCAPS_3DDEVICE ) )
1790 Usage |= WINED3DUSAGE_RENDERTARGET;
1792 pDDSD->ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY |
1795 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
1797 Usage |= WINED3DUSAGE_OVERLAY;
1799 if(This->depthstencil || (pDDSD->ddsCaps.dwCaps & DDSCAPS_ZBUFFER) )
1801 /* The depth stencil creation callback sets this flag.
1802 * Set the WineD3D usage to let it know that it's a depth
1805 Usage |= WINED3DUSAGE_DEPTHSTENCIL;
1807 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
1809 Pool = WINED3DPOOL_SYSTEMMEM;
1811 else if(pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
1813 Pool = WINED3DPOOL_MANAGED;
1814 /* Managed textures have the system memory flag set */
1815 pDDSD->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
1817 else if(pDDSD->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
1819 /* Videomemory adds localvidmem, this is mutually exclusive with systemmemory
1822 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
1825 Format = PixelFormat_DD2WineD3D(&pDDSD->u4.ddpfPixelFormat);
1826 if(Format == WINED3DFMT_UNKNOWN)
1828 ERR("Unsupported / Unknown pixelformat\n");
1829 return DDERR_INVALIDPIXELFORMAT;
1832 /* Create the Surface object */
1833 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
1836 ERR("(%p) Error allocating memory for a surface\n", This);
1837 return DDERR_OUTOFVIDEOMEMORY;
1839 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawSurface7, IDirectDrawSurface7_Vtbl);
1840 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawSurface3, IDirectDrawSurface3_Vtbl);
1841 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawGammaControl, IDirectDrawGammaControl_Vtbl);
1842 ICOM_INIT_INTERFACE(*ppSurf, IDirect3DTexture2, IDirect3DTexture2_Vtbl);
1843 ICOM_INIT_INTERFACE(*ppSurf, IDirect3DTexture, IDirect3DTexture1_Vtbl);
1845 (*ppSurf)->version = 7;
1846 (*ppSurf)->ddraw = This;
1847 (*ppSurf)->surface_desc.dwSize = sizeof(DDSURFACEDESC2);
1848 (*ppSurf)->surface_desc.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1849 DD_STRUCT_COPY_BYSIZE(&(*ppSurf)->surface_desc, pDDSD);
1851 /* Surface attachments */
1852 (*ppSurf)->next_attached = NULL;
1853 (*ppSurf)->first_attached = *ppSurf;
1855 /* Needed to re-create the surface on an implementation change */
1856 (*ppSurf)->ImplType = ImplType;
1858 /* For D3DDevice creation */
1859 (*ppSurf)->isRenderTarget = FALSE;
1861 /* A trace message for debugging */
1862 TRACE("(%p) Created IDirectDrawSurface implementation structure at %p\n", This, *ppSurf);
1864 if(pDDSD->ddsCaps.dwCaps & ( DDSCAPS_PRIMARYSURFACE | DDSCAPS_TEXTURE | DDSCAPS_3DDEVICE) )
1866 /* Render targets and textures need a IParent interface,
1867 * because WineD3D will destroy them when the swapchain
1870 parImpl = HeapAlloc(GetProcessHeap(), 0, sizeof(IParentImpl));
1873 ERR("Out of memory when allocating memory for a IParent implementation\n");
1874 return DDERR_OUTOFMEMORY;
1877 ICOM_INIT_INTERFACE(parImpl, IParent, IParent_Vtbl);
1878 Parent = (IUnknown *) ICOM_INTERFACE(parImpl, IParent);
1879 TRACE("Using IParent interface %p as parent\n", parImpl);
1883 /* Use the surface as parent */
1884 Parent = (IUnknown *) ICOM_INTERFACE(*ppSurf, IDirectDrawSurface7);
1885 TRACE("Using Surface interface %p as parent\n", *ppSurf);
1888 /* Now create the WineD3D Surface */
1889 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice,
1893 TRUE /* Lockable */,
1894 FALSE /* Discard */,
1896 &(*ppSurf)->WineD3DSurface,
1899 WINED3DMULTISAMPLE_NONE,
1900 0 /* MultiSampleQuality */,
1901 0 /* SharedHandle */,
1907 ERR("IWineD3DDevice::CreateSurface failed. hr = %08x\n", hr);
1911 /* Set the child of the parent implementation if it exists */
1914 parImpl->child = (IUnknown *) (*ppSurf)->WineD3DSurface;
1915 /* The IParent releases the WineD3DSurface, and
1916 * the ddraw surface does that too. Hold a reference
1918 IWineD3DSurface_AddRef((*ppSurf)->WineD3DSurface);
1921 /* Increase the surface counter, and attach the surface */
1922 InterlockedIncrement(&This->surfaces);
1923 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
1925 /* Here we could store all created surfaces in the DirectDrawImpl structure,
1926 * But this could also be delegated to WineDDraw, as it keeps track of all its
1927 * resources. Not implemented for now, as there are more important things ;)
1930 /* Get the pixel format of the WineD3DSurface and store it.
1931 * Don't use the Format choosen above, WineD3D might have
1934 Desc.Format = &Format;
1935 Desc.Type = &ResType;
1936 Desc.Usage = &Usage;
1937 Desc.Pool = &dummy_d3dpool;
1938 Desc.Size = &dummy_uint;
1939 Desc.MultiSampleType = &dummy_mst;
1940 Desc.MultiSampleQuality = &dummy_dword;
1941 Desc.Width = &Width;
1942 Desc.Height = &Height;
1944 (*ppSurf)->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
1945 hr = IWineD3DSurface_GetDesc((*ppSurf)->WineD3DSurface, &Desc);
1948 ERR("IWineD3DSurface::GetDesc failed\n");
1949 IDirectDrawSurface7_Release( (IDirectDrawSurface7 *) *ppSurf);
1953 if(Format == WINED3DFMT_UNKNOWN)
1955 FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN\n");
1957 PixelFormat_WineD3DtoDD( &(*ppSurf)->surface_desc.u4.ddpfPixelFormat, Format);
1959 /* Anno 1602 stores the pitch right after surface creation, so make sure it's there.
1960 * I can't LockRect() the surface here because if OpenGL surfaces are in use, the
1961 * WineD3DDevice might not be useable for 3D yet, so an extra method was created.
1962 * TODO: Test other fourcc formats
1964 if(Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3 ||
1965 Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5)
1967 (*ppSurf)->surface_desc.dwFlags |= DDSD_LINEARSIZE;
1968 if(Format == WINED3DFMT_DXT1)
1970 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height) / 2;
1974 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height);
1979 (*ppSurf)->surface_desc.dwFlags |= DDSD_PITCH;
1980 (*ppSurf)->surface_desc.u1.lPitch = IWineD3DSurface_GetPitch((*ppSurf)->WineD3DSurface);
1983 /* Application passed a color key? Set it! */
1984 if(pDDSD->dwFlags & DDSD_CKDESTOVERLAY)
1986 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
1988 (WINEDDCOLORKEY *) &pDDSD->u3.ddckCKDestOverlay);
1990 if(pDDSD->dwFlags & DDSD_CKDESTBLT)
1992 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
1994 (WINEDDCOLORKEY *) &pDDSD->ddckCKDestBlt);
1996 if(pDDSD->dwFlags & DDSD_CKSRCOVERLAY)
1998 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2000 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcOverlay);
2002 if(pDDSD->dwFlags & DDSD_CKSRCBLT)
2004 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2006 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcBlt);
2008 if ( pDDSD->dwFlags & DDSD_LPSURFACE)
2010 hr = IWineD3DSurface_SetMem((*ppSurf)->WineD3DSurface, pDDSD->lpSurface);
2011 if(hr != WINED3D_OK)
2013 /* No need for a trace here, wined3d does that for us */
2014 IDirectDrawSurface7_Release(ICOM_INTERFACE((*ppSurf), IDirectDrawSurface7));
2021 /*****************************************************************************
2022 * CreateAdditionalSurfaces
2024 * Creates a new mipmap chain.
2027 * root: Root surface to attach the newly created chain to
2028 * count: number of surfaces to create
2029 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2030 * effects on the caller
2031 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2032 * creates an additional surface without the mipmapping flags
2034 *****************************************************************************/
2036 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2037 IDirectDrawSurfaceImpl *root,
2039 DDSURFACEDESC2 DDSD,
2042 UINT i, j, level = 0;
2044 IDirectDrawSurfaceImpl *last = root;
2046 for(i = 0; i < count; i++)
2048 IDirectDrawSurfaceImpl *object2 = NULL;
2050 /* increase the mipmap level, but only if a mipmap is created
2051 * In this case, also halve the size
2053 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2056 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2057 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2058 /* Set the mipmap sublevel flag according to msdn */
2059 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2063 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2065 CubeFaceRoot = FALSE;
2067 hr = IDirectDrawImpl_CreateNewSurface(This,
2076 /* Add the new surface to the complex attachment array */
2077 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2079 if(last->complex_array[j]) continue;
2080 last->complex_array[j] = object2;
2085 /* Remove the (possible) back buffer cap from the new surface description,
2086 * because only one surface in the flipping chain is a back buffer, one
2087 * is a front buffer, the others are just primary surfaces.
2089 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2094 /*****************************************************************************
2095 * IDirectDraw7::CreateSurface
2097 * Creates a new IDirectDrawSurface object and returns its interface.
2099 * The surface connections with wined3d are a bit tricky. Basically it works
2102 * |------------------------| |-----------------|
2103 * | DDraw surface | | WineD3DSurface |
2105 * | WineD3DSurface |-------------->| |
2106 * | Child |<------------->| Parent |
2107 * |------------------------| |-----------------|
2109 * The DDraw surface is the parent of the wined3d surface, and it releases
2110 * the WineD3DSurface when the ddraw surface is destroyed.
2112 * However, for all surfaces which can be in a container in WineD3D,
2113 * we have to do this. These surfaces are ususally complex surfaces,
2114 * so this concerns primary surfaces with a front and a back buffer,
2117 * |------------------------| |-----------------|
2118 * | DDraw surface | | Containter |
2120 * | Child |<------------->| Parent |
2121 * | Texture |<------------->| |
2122 * | WineD3DSurface |<----| | Levels |<--|
2123 * | Complex connection | | | | |
2124 * |------------------------| | |-----------------| |
2128 * | |------------------| | |-----------------| |
2129 * | | IParent | |-------->| WineD3DSurface | |
2131 * | | Child |<------------->| Parent | |
2132 * | | | | Container |<--|
2133 * | |------------------| |-----------------| |
2135 * | |----------------------| |
2136 * | | DDraw surface 2 | |
2138 * |<->| Complex root Child | |
2140 * | | WineD3DSurface |<----| |
2141 * | |----------------------| | |
2143 * | |---------------------| | |-----------------| |
2144 * | | IParent | |----->| WineD3DSurface | |
2146 * | | Child |<---------->| Parent | |
2147 * | |---------------------| | Container |<--|
2148 * | |-----------------| |
2150 * | ---More surfaces can follow--- |
2152 * The reason is that the IWineD3DSwapchain(render target container)
2153 * and the IWineD3DTexure(Texture container) release the parents
2154 * of their surface's children, but by releasing the complex root
2155 * the surfaces which are complexly attached to it are destroyed
2156 * too. See IDirectDrawSurface::Release for a more detailed
2160 * DDSD: Description of the surface to create
2161 * Surf: Address to store the interface pointer at
2162 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2163 * aggregation, so it has to be NULL
2167 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2168 * DDERR_* if an error occurs
2170 *****************************************************************************/
2171 static HRESULT WINAPI
2172 IDirectDrawImpl_CreateSurface(IDirectDraw7 *iface,
2173 DDSURFACEDESC2 *DDSD,
2174 IDirectDrawSurface7 **Surf,
2177 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2178 IDirectDrawSurfaceImpl *object = NULL;
2180 LONG extra_surfaces = 0;
2181 DDSURFACEDESC2 desc2;
2182 WINED3DDISPLAYMODE Mode;
2184 TRACE("(%p)->(%p,%p,%p)\n", This, DDSD, Surf, UnkOuter);
2186 /* Some checks before we start */
2187 if (TRACE_ON(ddraw))
2189 TRACE(" (%p) Requesting surface desc :\n", This);
2190 DDRAW_dump_surface_desc(DDSD);
2193 if (UnkOuter != NULL)
2195 FIXME("(%p) : outer != NULL?\n", This);
2196 return CLASS_E_NOAGGREGATION; /* unchecked */
2201 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", This);
2202 return E_POINTER; /* unchecked */
2205 if (!(DDSD->dwFlags & DDSD_CAPS))
2207 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2208 DDSD->dwFlags |= DDSD_CAPS;
2210 if (DDSD->ddsCaps.dwCaps == 0)
2212 /* This has been checked on real Windows */
2213 DDSD->ddsCaps.dwCaps = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
2216 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2218 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2219 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2222 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2224 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2225 WARN("(%p) Null surface pointer specified, ignore it!\n", This);
2226 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2229 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2230 !(This->cooperative_level & DDSCL_EXCLUSIVE))
2232 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n", This);
2234 return DDERR_NOEXCLUSIVEMODE;
2237 if(DDSD->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER)) {
2238 WARN("Application tried to create an explicit front or back buffer\n");
2239 return DDERR_INVALIDCAPS;
2241 /* Check cube maps but only if the size includes them */
2242 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2244 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2245 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2247 WARN("Cube map faces requested without cube map flag\n");
2248 return DDERR_INVALIDCAPS;
2250 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2251 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2253 WARN("Cube map without faces requested\n");
2254 return DDERR_INVALIDPARAMS;
2257 /* Quick tests confirm those can be created, but we don't do that yet */
2258 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2259 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2261 FIXME("Partial cube maps not supported yet\n");
2265 /* According to the msdn this flag is ignored by CreateSurface */
2266 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2267 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2269 /* Modify some flags */
2270 memset(&desc2, 0, sizeof(desc2));
2271 desc2.dwSize = sizeof(desc2); /* For the struct copy */
2272 DD_STRUCT_COPY_BYSIZE(&desc2, DDSD);
2273 desc2.dwSize = sizeof(desc2); /* To override a possibly smaller size */
2274 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2276 /* Get the video mode from WineD3D - we will need it */
2277 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
2278 0, /* Swapchain 0 */
2282 ERR("Failed to read display mode from wined3d\n");
2283 switch(This->orig_bpp)
2286 Mode.Format = WINED3DFMT_P8;
2290 Mode.Format = WINED3DFMT_X1R5G5B5;
2294 Mode.Format = WINED3DFMT_R5G6B5;
2298 Mode.Format = WINED3DFMT_R8G8B8;
2302 Mode.Format = WINED3DFMT_X8R8G8B8;
2305 Mode.Width = This->orig_width;
2306 Mode.Height = This->orig_height;
2309 /* No pixelformat given? Use the current screen format */
2310 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2312 desc2.dwFlags |= DDSD_PIXELFORMAT;
2313 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2315 /* Wait: It could be a Z buffer */
2316 if(desc2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
2318 switch(desc2.u2.dwMipMapCount) /* Who had this glorious idea? */
2321 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D15S1);
2324 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D16);
2327 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D24X8);
2330 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D32);
2333 ERR("Unknown Z buffer bit depth\n");
2338 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
2342 /* No Width or no Height? Use the original screen size
2344 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2345 !(desc2.dwFlags & DDSD_HEIGHT) )
2347 /* Invalid for non-render targets */
2348 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2350 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2352 return DDERR_INVALIDPARAMS;
2355 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2356 desc2.dwWidth = Mode.Width;
2357 desc2.dwHeight = Mode.Height;
2360 /* Mipmap count fixes */
2361 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2363 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2365 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2367 /* Mipmap count is given, nothing to do */
2371 /* Undocumented feature: Create sublevels until
2372 * either the width or the height is 1
2374 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2375 desc2.dwWidth : desc2.dwHeight;
2376 desc2.u2.dwMipMapCount = 0;
2379 desc2.u2.dwMipMapCount += 1;
2386 /* Not-complex mipmap -> Mipmapcount = 1 */
2387 desc2.u2.dwMipMapCount = 1;
2389 extra_surfaces = desc2.u2.dwMipMapCount - 1;
2391 /* There's a mipmap count in the created surface in any case */
2392 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2394 /* If no mipmap is given, the texture has only one level */
2396 /* The first surface is a front buffer, the back buffer is created afterwards */
2397 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2399 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2402 /* The root surface in a cube map is positive x */
2403 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2405 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2406 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2409 /* Create the first surface */
2410 hr = IDirectDrawImpl_CreateNewSurface(This, &desc2, &object, 0);
2413 ERR("IDirectDrawImpl_CreateNewSurface failed with %08x\n", hr);
2416 object->is_complex_root = TRUE;
2418 *Surf = ICOM_INTERFACE(object, IDirectDrawSurface7);
2420 /* Create Additional surfaces if necessary
2421 * This applies to Primary surfaces which have a back buffer count
2422 * set, but not to mipmap textures. In case of Mipmap textures,
2423 * wineD3D takes care of the creation of additional surfaces
2425 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2427 extra_surfaces = DDSD->dwBackBufferCount;
2428 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2429 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2433 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2435 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2436 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
2437 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2438 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
2439 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
2440 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2441 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
2442 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
2443 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2444 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
2445 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
2446 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2447 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
2448 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
2449 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2450 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
2451 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2454 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces, desc2, FALSE);
2457 /* This destroys and possibly created surfaces too */
2458 IDirectDrawSurface_Release( ICOM_INTERFACE(object, IDirectDrawSurface7) );
2462 /* Addref the ddraw interface to keep an reference for each surface */
2463 IDirectDraw7_AddRef(iface);
2464 object->ifaceToRelease = (IUnknown *) iface;
2466 /* If the implementation is OpenGL and there's no d3ddevice, attach a d3ddevice
2467 * But attach the d3ddevice only if the currently created surface was
2468 * a primary surface (2D app in 3D mode) or a 3DDEVICE surface (3D app)
2469 * The only case I can think of where this doesn't apply is when a
2470 * 2D app was configured by the user to run with OpenGL and it didn't create
2471 * the render target as first surface. In this case the render target creation
2472 * will cause the 3D init.
2474 if( (This->ImplType == SURFACE_OPENGL) && !(This->d3d_initialized) &&
2475 desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE) )
2477 IDirectDrawSurfaceImpl *target = object, *surface;
2480 /* Search for the primary to use as render target */
2481 LIST_FOR_EACH(entry, &This->surface_list)
2483 surface = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2484 if((surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER)) ==
2485 (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER))
2489 TRACE("Using primary %p as render target\n", target);
2494 TRACE("(%p) Attaching a D3DDevice, rendertarget = %p\n", This, target);
2495 hr = IDirectDrawImpl_AttachD3DDevice(This, target);
2498 ERR("IDirectDrawImpl_AttachD3DDevice failed, hr = %x\n", hr);
2502 /* Create a WineD3DTexture if a texture was requested */
2503 if(desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
2506 WINED3DFORMAT Format;
2507 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
2509 This->tex_root = object;
2511 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2513 /* a mipmap is created, create enough levels */
2514 levels = desc2.u2.dwMipMapCount;
2518 /* No mipmap is created, create one level */
2522 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM */
2523 if(DDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
2525 Pool = WINED3DPOOL_SYSTEMMEM;
2527 /* Should I forward the MANEGED cap to the managed pool ? */
2529 /* Get the format. It's set already by CreateNewSurface */
2530 Format = PixelFormat_DD2WineD3D(&object->surface_desc.u4.ddpfPixelFormat);
2532 /* The surfaces are already created, the callback only
2533 * passes the IWineD3DSurface to WineD3D
2535 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2537 hr = IWineD3DDevice_CreateCubeTexture(This->wineD3DDevice,
2538 DDSD->dwWidth, /* Edgelength */
2543 (IWineD3DCubeTexture **) &object->wineD3DTexture,
2544 0, /* SharedHandle */
2545 (IUnknown *) ICOM_INTERFACE(object, IDirectDrawSurface7),
2546 D3D7CB_CreateSurface);
2550 hr = IWineD3DDevice_CreateTexture(This->wineD3DDevice,
2551 DDSD->dwWidth, DDSD->dwHeight,
2552 levels, /* MipMapCount = Levels */
2556 (IWineD3DTexture **) &object->wineD3DTexture,
2557 0, /* SharedHandle */
2558 (IUnknown *) ICOM_INTERFACE(object, IDirectDrawSurface7),
2559 D3D7CB_CreateSurface );
2561 This->tex_root = NULL;
2567 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
2568 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
2571 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
2572 const DDPIXELFORMAT *provided)
2574 /* Some flags must be present in both or neither for a match. */
2575 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
2576 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
2577 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
2579 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2582 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
2585 if (requested->dwFlags & DDPF_FOURCC)
2586 if (requested->dwFourCC != provided->dwFourCC)
2589 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
2590 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2591 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
2594 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2595 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2596 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
2599 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
2600 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
2603 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
2604 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2606 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
2609 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
2610 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
2617 IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested,
2618 const DDSURFACEDESC2* provided)
2627 #define CMP(FLAG, FIELD) \
2628 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
2629 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
2631 static const struct compare_info compare[] =
2633 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
2634 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
2636 CMP(CKDESTBLT, ddckCKDestBlt),
2637 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
2638 CMP(CKSRCBLT, ddckCKSrcBlt),
2639 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
2640 CMP(HEIGHT, dwHeight),
2641 CMP(LINEARSIZE, u1 /* dwLinearSize */),
2642 CMP(LPSURFACE, lpSurface),
2643 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
2644 CMP(PITCH, u1 /* lPitch */),
2645 /* PIXELFORMAT: manual */
2646 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
2647 CMP(TEXTURESTAGE, dwTextureStage),
2648 CMP(WIDTH, dwWidth),
2649 /* ZBUFFERBITDEPTH: "obsolete" */
2656 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2659 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
2661 if (requested->dwFlags & compare[i].flag
2662 && memcmp((const char *)provided + compare[i].offset,
2663 (const char *)requested + compare[i].offset,
2664 compare[i].size) != 0)
2668 if (requested->dwFlags & DDSD_PIXELFORMAT)
2670 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
2671 &provided->u4.ddpfPixelFormat))
2678 #undef DDENUMSURFACES_SEARCHTYPE
2679 #undef DDENUMSURFACES_MATCHTYPE
2681 /*****************************************************************************
2682 * IDirectDraw7::EnumSurfaces
2684 * Loops through all surfaces attached to this device and calls the
2685 * application callback. This can't be relayed to WineD3DDevice,
2686 * because some WineD3DSurfaces' parents are IParent objects
2689 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
2690 * DDSD: Description to filter for
2691 * Context: Application-provided pointer, it's passed unmodified to the
2693 * Callback: Address to call for each surface
2696 * DDERR_INVALIDPARAMS if the callback is NULL
2699 *****************************************************************************/
2700 static HRESULT WINAPI
2701 IDirectDrawImpl_EnumSurfaces(IDirectDraw7 *iface,
2703 DDSURFACEDESC2 *DDSD,
2705 LPDDENUMSURFACESCALLBACK7 Callback)
2707 /* The surface enumeration is handled by WineDDraw,
2708 * because it keeps track of all surfaces attached to
2709 * it. The filtering is done by our callback function,
2710 * because WineDDraw doesn't handle ddraw-like surface
2713 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2714 IDirectDrawSurfaceImpl *surf;
2716 DDSURFACEDESC2 desc;
2717 struct list *entry, *entry2;
2719 all = Flags & DDENUMSURFACES_ALL;
2720 nomatch = Flags & DDENUMSURFACES_NOMATCH;
2722 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, DDSD, Context, Callback);
2725 return DDERR_INVALIDPARAMS;
2727 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
2728 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
2730 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2731 if (all || (nomatch != IDirectDrawImpl_DDSD_Match(DDSD, &surf->surface_desc)))
2733 desc = surf->surface_desc;
2734 IDirectDrawSurface7_AddRef(ICOM_INTERFACE(surf, IDirectDrawSurface7));
2735 if(Callback( ICOM_INTERFACE(surf, IDirectDrawSurface7), &desc, Context) != DDENUMRET_OK)
2742 static HRESULT WINAPI
2743 findRenderTarget(IDirectDrawSurface7 *surface,
2744 DDSURFACEDESC2 *desc,
2747 IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, surface);
2748 IDirectDrawSurfaceImpl **target = (IDirectDrawSurfaceImpl **) ctx;
2750 if(!surf->isRenderTarget) {
2752 IDirectDrawSurface7_Release(surface);
2753 return DDENUMRET_CANCEL;
2756 /* Recurse into the surface tree */
2757 IDirectDrawSurface7_EnumAttachedSurfaces(surface, ctx, findRenderTarget);
2759 IDirectDrawSurface7_Release(surface);
2760 if(*target) return DDENUMRET_CANCEL;
2761 else return DDENUMRET_OK; /* Continue with the next neighbor surface */
2764 /*****************************************************************************
2765 * D3D7CB_CreateRenderTarget
2767 * Callback called by WineD3D to create Surfaces for render target usage
2768 * This function takes the D3D target from the IDirectDrawImpl structure,
2769 * and returns the WineD3DSurface. To avoid double usage, the surface
2770 * is marked as render target afterwards
2773 * device: The WineD3DDevice's parent
2774 * Width, Height, Format: Dimensions and pixelformat of the render target
2775 * Ignored, because the surface already exists
2776 * MultiSample, MultisampleQuality, Lockable: Ignored for the same reason
2778 * ppSurface: Address to pass the surface pointer back at
2779 * pSharedHandle: Ignored
2782 * Always returns D3D_OK
2784 *****************************************************************************/
2785 static HRESULT WINAPI
2786 D3D7CB_CreateRenderTarget(IUnknown *device, IUnknown *pSuperior,
2787 UINT Width, UINT Height,
2788 WINED3DFORMAT Format,
2789 WINED3DMULTISAMPLE_TYPE MultiSample,
2790 DWORD MultisampleQuality,
2792 IWineD3DSurface** ppSurface,
2793 HANDLE* pSharedHandle)
2795 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
2796 IDirectDrawSurfaceImpl *d3dSurface = This->d3d_target, *target = NULL;
2797 TRACE("(%p) call back\n", device);
2799 if(d3dSurface->isRenderTarget)
2801 IDirectDrawSurface7_EnumAttachedSurfaces(ICOM_INTERFACE(d3dSurface, IDirectDrawSurface7),
2802 &target, findRenderTarget);
2806 target = d3dSurface;
2811 target = This->d3d_target;
2812 ERR(" (%p) : No DirectDrawSurface found to create the back buffer. Using the front buffer as back buffer. Uncertain consequences\n", This);
2815 /* TODO: Return failure if the dimensions do not match, but this shouldn't happen */
2817 *ppSurface = target->WineD3DSurface;
2818 target->isRenderTarget = TRUE;
2819 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *ppSurface, d3dSurface);
2823 static HRESULT WINAPI
2824 D3D7CB_CreateDepthStencilSurface(IUnknown *device,
2825 IUnknown *pSuperior,
2828 WINED3DFORMAT Format,
2829 WINED3DMULTISAMPLE_TYPE MultiSample,
2830 DWORD MultisampleQuality,
2832 IWineD3DSurface** ppSurface,
2833 HANDLE* pSharedHandle)
2835 /* Create a Depth Stencil surface to make WineD3D happy */
2836 HRESULT hr = D3D_OK;
2837 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
2838 DDSURFACEDESC2 ddsd;
2840 TRACE("(%p) call back\n", device);
2844 /* Create a DirectDraw surface */
2845 memset(&ddsd, 0, sizeof(ddsd));
2846 ddsd.dwSize = sizeof(ddsd);
2847 ddsd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2848 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
2849 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
2850 ddsd.dwHeight = Height;
2851 ddsd.dwWidth = Width;
2854 PixelFormat_WineD3DtoDD(&ddsd.u4.ddpfPixelFormat, Format);
2858 ddsd.dwFlags ^= DDSD_PIXELFORMAT;
2861 This->depthstencil = TRUE;
2862 hr = IDirectDraw7_CreateSurface((IDirectDraw7 *) This,
2864 (IDirectDrawSurface7 **) &This->DepthStencilBuffer,
2866 This->depthstencil = FALSE;
2869 ERR(" (%p) Creating a DepthStencil Surface failed, result = %x\n", This, hr);
2872 *ppSurface = This->DepthStencilBuffer->WineD3DSurface;
2876 /*****************************************************************************
2877 * D3D7CB_CreateAdditionalSwapChain
2879 * Callback function for WineD3D which creates a new WineD3DSwapchain
2880 * interface. It also creates an IParent interface to store that pointer,
2881 * so the WineD3DSwapchain has a parent and can be released when the D3D
2882 * device is destroyed
2883 *****************************************************************************/
2884 static HRESULT WINAPI
2885 D3D7CB_CreateAdditionalSwapChain(IUnknown *device,
2886 WINED3DPRESENT_PARAMETERS* pPresentationParameters,
2887 IWineD3DSwapChain ** ppSwapChain)
2889 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
2890 IParentImpl *object = NULL;
2891 HRESULT res = D3D_OK;
2892 IWineD3DSwapChain *swapchain;
2893 TRACE("(%p) call back\n", device);
2895 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IParentImpl));
2898 FIXME("Allocation of memory failed\n");
2899 *ppSwapChain = NULL;
2900 return DDERR_OUTOFVIDEOMEMORY;
2903 ICOM_INIT_INTERFACE(object, IParent, IParent_Vtbl);
2906 res = IWineD3DDevice_CreateAdditionalSwapChain(This->wineD3DDevice,
2907 pPresentationParameters,
2909 (IUnknown*) ICOM_INTERFACE(object, IParent),
2910 D3D7CB_CreateRenderTarget,
2911 D3D7CB_CreateDepthStencilSurface);
2914 FIXME("(%p) call to IWineD3DDevice_CreateAdditionalSwapChain failed\n", This);
2915 HeapFree(GetProcessHeap(), 0 , object);
2916 *ppSwapChain = NULL;
2920 *ppSwapChain = swapchain;
2921 object->child = (IUnknown *) swapchain;
2927 /*****************************************************************************
2928 * IDirectDrawImpl_AttachD3DDevice
2930 * Initializes the D3D capabilities of WineD3D
2933 * primary: The primary surface for D3D
2939 *****************************************************************************/
2940 static HRESULT WINAPI
2941 IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This,
2942 IDirectDrawSurfaceImpl *primary)
2947 WINED3DPRESENT_PARAMETERS localParameters;
2949 TRACE("(%p)->(%p)\n", This, primary);
2951 /* Get the window */
2952 hr = IWineD3DDevice_GetHWND(This->wineD3DDevice,
2956 ERR("IWineD3DDevice::GetHWND failed\n");
2960 /* If there's no window, create a hidden window. WineD3D needs it */
2963 window = CreateWindowExA(0, This->classname, "Hidden D3D Window",
2965 GetSystemMetrics(SM_CXSCREEN),
2966 GetSystemMetrics(SM_CYSCREEN),
2967 NULL, NULL, GetModuleHandleA(0), NULL);
2969 ShowWindow(window, SW_HIDE); /* Just to be sure */
2970 WARN("(%p) No window for the Direct3DDevice, created a hidden window. HWND=%p\n", This, window);
2971 This->d3d_window = window;
2975 TRACE("(%p) Using existing window %p for Direct3D rendering\n", This, window);
2978 /* Store the future Render Target surface */
2979 This->d3d_target = primary;
2981 /* Use the surface description for the device parameters, not the
2982 * Device settings. The app might render to an offscreen surface
2984 localParameters.BackBufferWidth = primary->surface_desc.dwWidth;
2985 localParameters.BackBufferHeight = primary->surface_desc.dwHeight;
2986 localParameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2987 localParameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
2988 localParameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
2989 localParameters.MultiSampleQuality = 0;
2990 localParameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
2991 localParameters.hDeviceWindow = window;
2992 localParameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
2993 localParameters.EnableAutoDepthStencil = FALSE;
2994 localParameters.AutoDepthStencilFormat = WINED3DFMT_D16;
2995 localParameters.Flags = 0;
2996 localParameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
2997 localParameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
2999 TRACE("Passing mode %d\n", localParameters.BackBufferFormat);
3001 /* Set this NOW, otherwise creating the depth stencil surface will cause a
3002 * recursive loop until ram or emulated video memory is full
3004 This->d3d_initialized = TRUE;
3006 hr = IWineD3DDevice_Init3D(This->wineD3DDevice,
3008 D3D7CB_CreateAdditionalSwapChain);
3011 This->wineD3DDevice = NULL;
3015 /* Create an Index Buffer parent */
3016 TRACE("(%p) Successfully initialized 3D\n", This);
3020 /*****************************************************************************
3021 * DirectDrawCreateClipper (DDRAW.@)
3023 * Creates a new IDirectDrawClipper object.
3026 * Clipper: Address to write the interface pointer to
3027 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3031 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3032 * E_OUTOFMEMORY if allocating the object failed
3034 *****************************************************************************/
3036 DirectDrawCreateClipper(DWORD Flags,
3037 IDirectDrawClipper **Clipper,
3040 IDirectDrawClipperImpl* object;
3041 TRACE("(%08x,%p,%p)\n", Flags, Clipper, UnkOuter);
3043 if (UnkOuter != NULL) return CLASS_E_NOAGGREGATION;
3045 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3046 sizeof(IDirectDrawClipperImpl));
3047 if (object == NULL) return E_OUTOFMEMORY;
3049 ICOM_INIT_INTERFACE(object, IDirectDrawClipper, IDirectDrawClipper_Vtbl);
3051 object->wineD3DClipper = pWineDirect3DCreateClipper((IUnknown *) object);
3052 if(!object->wineD3DClipper)
3054 HeapFree(GetProcessHeap(), 0, object);
3055 return E_OUTOFMEMORY;
3058 *Clipper = (IDirectDrawClipper *) object;
3062 /*****************************************************************************
3063 * IDirectDraw7::CreateClipper
3065 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3067 *****************************************************************************/
3068 static HRESULT WINAPI
3069 IDirectDrawImpl_CreateClipper(IDirectDraw7 *iface,
3071 IDirectDrawClipper **Clipper,
3074 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3075 TRACE("(%p)->(%x,%p,%p)\n", This, Flags, Clipper, UnkOuter);
3076 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3079 /*****************************************************************************
3080 * IDirectDraw7::CreatePalette
3082 * Creates a new IDirectDrawPalette object
3085 * Flags: The flags for the new clipper
3086 * ColorTable: Color table to assign to the new clipper
3087 * Palette: Address to write the interface pointer to
3088 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3092 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3093 * E_OUTOFMEMORY if allocating the object failed
3095 *****************************************************************************/
3096 static HRESULT WINAPI
3097 IDirectDrawImpl_CreatePalette(IDirectDraw7 *iface,
3099 PALETTEENTRY *ColorTable,
3100 IDirectDrawPalette **Palette,
3101 IUnknown *pUnkOuter)
3103 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3104 IDirectDrawPaletteImpl *object;
3105 HRESULT hr = DDERR_GENERIC;
3106 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, ColorTable, Palette, pUnkOuter);
3108 if(pUnkOuter != NULL)
3110 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
3111 return CLASS_E_NOAGGREGATION;
3114 /* The refcount test shows that a cooplevel is required for this */
3115 if(!This->cooperative_level)
3117 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3118 return DDERR_NOCOOPERATIVELEVELSET;
3121 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
3124 ERR("Out of memory when allocating memory for a palette implementation\n");
3125 return E_OUTOFMEMORY;
3128 ICOM_INIT_INTERFACE(object, IDirectDrawPalette, IDirectDrawPalette_Vtbl);
3130 object->ddraw_owner = This;
3132 hr = IWineD3DDevice_CreatePalette(This->wineD3DDevice, Flags, ColorTable, &object->wineD3DPalette, (IUnknown *) ICOM_INTERFACE(object, IDirectDrawPalette) );
3135 HeapFree(GetProcessHeap(), 0, object);
3139 IDirectDraw7_AddRef(iface);
3140 object->ifaceToRelease = (IUnknown *) iface;
3141 *Palette = ICOM_INTERFACE(object, IDirectDrawPalette);
3145 /*****************************************************************************
3146 * IDirectDraw7::DuplicateSurface
3148 * Duplicates a surface. The surface memory points to the same memory as
3149 * the original surface, and it's released when the last surface referencing
3150 * it is released. I guess that's beyond Wine's surface management right now
3151 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3152 * test application to implement this)
3155 * Src: Address of the source surface
3156 * Dest: Address to write the new surface pointer to
3159 * See IDirectDraw7::CreateSurface
3161 *****************************************************************************/
3162 static HRESULT WINAPI
3163 IDirectDrawImpl_DuplicateSurface(IDirectDraw7 *iface,
3164 IDirectDrawSurface7 *Src,
3165 IDirectDrawSurface7 **Dest)
3167 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3168 IDirectDrawSurfaceImpl *Surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Src);
3170 FIXME("(%p)->(%p,%p)\n", This, Surf, Dest);
3172 /* For now, simply create a new, independent surface */
3173 return IDirectDraw7_CreateSurface(iface,
3174 &Surf->surface_desc,
3179 /*****************************************************************************
3180 * IDirectDraw7 VTable
3181 *****************************************************************************/
3182 const IDirectDraw7Vtbl IDirectDraw7_Vtbl =
3185 IDirectDrawImpl_QueryInterface,
3186 IDirectDrawImpl_AddRef,
3187 IDirectDrawImpl_Release,
3188 /*** IDirectDraw ***/
3189 IDirectDrawImpl_Compact,
3190 IDirectDrawImpl_CreateClipper,
3191 IDirectDrawImpl_CreatePalette,
3192 IDirectDrawImpl_CreateSurface,
3193 IDirectDrawImpl_DuplicateSurface,
3194 IDirectDrawImpl_EnumDisplayModes,
3195 IDirectDrawImpl_EnumSurfaces,
3196 IDirectDrawImpl_FlipToGDISurface,
3197 IDirectDrawImpl_GetCaps,
3198 IDirectDrawImpl_GetDisplayMode,
3199 IDirectDrawImpl_GetFourCCCodes,
3200 IDirectDrawImpl_GetGDISurface,
3201 IDirectDrawImpl_GetMonitorFrequency,
3202 IDirectDrawImpl_GetScanLine,
3203 IDirectDrawImpl_GetVerticalBlankStatus,
3204 IDirectDrawImpl_Initialize,
3205 IDirectDrawImpl_RestoreDisplayMode,
3206 IDirectDrawImpl_SetCooperativeLevel,
3207 IDirectDrawImpl_SetDisplayMode,
3208 IDirectDrawImpl_WaitForVerticalBlank,
3209 /*** IDirectDraw2 ***/
3210 IDirectDrawImpl_GetAvailableVidMem,
3211 /*** IDirectDraw7 ***/
3212 IDirectDrawImpl_GetSurfaceFromDC,
3213 IDirectDrawImpl_RestoreAllSurfaces,
3214 IDirectDrawImpl_TestCooperativeLevel,
3215 IDirectDrawImpl_GetDeviceIdentifier,
3216 /*** IDirectDraw7 ***/
3217 IDirectDrawImpl_StartModeTest,
3218 IDirectDrawImpl_EvaluateMode
3221 /*****************************************************************************
3222 * IDirectDrawImpl_FindDecl
3224 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
3225 * if none was found.
3227 * This function is in ddraw.c and the DDraw object space because D3D7
3228 * vertex buffers are created using the IDirect3D interface to the ddraw
3229 * object, so they can be valid across D3D devices(theoretically. The ddraw
3230 * object also owns the wined3d device
3234 * fvf: Fvf to find the decl for
3237 * NULL in case of an error, the IWineD3DVertexDeclaration interface for the
3240 *****************************************************************************/
3241 IWineD3DVertexDeclaration *
3242 IDirectDrawImpl_FindDecl(IDirectDrawImpl *This,
3246 IWineD3DVertexDeclaration* pDecl = NULL;
3247 int p, low, high; /* deliberately signed */
3248 struct FvfToDecl *convertedDecls = This->decls;
3250 TRACE("Searching for declaration for fvf %08x... ", fvf);
3253 high = This->numConvertedDecls - 1;
3254 while(low <= high) {
3255 p = (low + high) >> 1;
3257 if(convertedDecls[p].fvf == fvf) {
3258 TRACE("found %p\n", convertedDecls[p].decl);
3259 return convertedDecls[p].decl;
3260 } else if(convertedDecls[p].fvf < fvf) {
3266 TRACE("not found. Creating and inserting at position %d.\n", low);
3268 hr = IWineD3DDevice_CreateVertexDeclarationFromFVF(This->wineD3DDevice,
3270 (IUnknown *) ICOM_INTERFACE(This, IDirectDraw7),
3272 if (hr != S_OK) return NULL;
3274 if(This->declArraySize == This->numConvertedDecls) {
3275 int grow = max(This->declArraySize / 2, 8);
3276 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
3277 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
3278 if(!convertedDecls) {
3279 /* This will destroy it */
3280 IWineD3DVertexDeclaration_Release(pDecl);
3283 This->decls = convertedDecls;
3284 This->declArraySize += grow;
3287 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
3288 convertedDecls[low].decl = pDecl;
3289 convertedDecls[low].fvf = fvf;
3290 This->numConvertedDecls++;
3292 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);