2 * Copyright 1997-2000 Marcus Meissner
3 * Copyright 1998-2000 Lionel Ulmer
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2006 Stefan Dösinger
6 * Copyright 2008 Denver Gingerich
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
32 #define NONAMELESSUNION
38 #include "wine/exception.h"
43 #include "ddraw_private.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
48 static BOOL IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested, const DDSURFACEDESC2* provided);
49 static HRESULT WINAPI IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This, IDirectDrawSurfaceImpl *primary);
50 static HRESULT WINAPI IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This, DDSURFACEDESC2 *pDDSD, IDirectDrawSurfaceImpl **ppSurf, UINT level);
52 /* Device identifier. Don't relay it to WineD3D */
53 static const DDDEVICEIDENTIFIER2 deviceidentifier =
57 { { 0x00010001, 0x00010001 } },
59 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
60 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
64 /*****************************************************************************
66 *****************************************************************************/
68 /*****************************************************************************
69 * IDirectDraw7::QueryInterface
71 * Queries different interfaces of the DirectDraw object. It can return
72 * IDirectDraw interfaces in version 1, 2, 4 and 7, and IDirect3D interfaces
73 * in version 1, 2, 3 and 7. An IDirect3DDevice can be created with this
75 * The returned interface is AddRef()-ed before it's returned
77 * Used for version 1, 2, 4 and 7
80 * refiid: Interface ID asked for
81 * obj: Used to return the interface pointer
84 * S_OK if an interface was found
85 * E_NOINTERFACE if the requested interface wasn't found
87 *****************************************************************************/
89 IDirectDrawImpl_QueryInterface(IDirectDraw7 *iface,
93 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
95 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(refiid), obj);
97 /* Can change surface impl type */
98 EnterCriticalSection(&ddraw_cs);
100 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
105 LeaveCriticalSection(&ddraw_cs);
106 return DDERR_INVALIDPARAMS;
109 /* Check DirectDraw Interfaces */
110 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
111 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
113 *obj = ICOM_INTERFACE(This, IDirectDraw7);
114 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
116 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
118 *obj = ICOM_INTERFACE(This, IDirectDraw4);
119 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
121 else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
123 *obj = ICOM_INTERFACE(This, IDirectDraw3);
124 TRACE("(%p) Returning IDirectDraw3 interface at %p\n", This, *obj);
126 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
128 *obj = ICOM_INTERFACE(This, IDirectDraw2);
129 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
131 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
133 *obj = ICOM_INTERFACE(This, IDirectDraw);
134 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
138 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
139 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
140 * who had this idea and why. The older interfaces can query and IDirect3D version
141 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
142 * and messy to implement with the common creation function, so it has been left out here.
144 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
145 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
146 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
147 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
149 /* Check the surface implementation */
150 if(This->ImplType == SURFACE_UNKNOWN)
152 /* Apps may create the IDirect3D Interface before the primary surface.
153 * set the surface implementation */
154 This->ImplType = SURFACE_OPENGL;
155 TRACE("(%p) Choosing OpenGL surfaces because a Direct3D interface was requested\n", This);
157 else if(This->ImplType != SURFACE_OPENGL && DefaultSurfaceType == SURFACE_UNKNOWN)
159 ERR("(%p) The App is requesting a D3D device, but a non-OpenGL surface type was choosen. Prepare for trouble!\n", This);
160 ERR(" (%p) You may want to contact wine-devel for help\n", This);
161 /* Should I assert(0) here??? */
163 else if(This->ImplType != SURFACE_OPENGL)
165 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
166 /* Do not abort here, only reject 3D Device creation */
169 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
171 This->d3dversion = 1;
172 *obj = ICOM_INTERFACE(This, IDirect3D);
173 TRACE(" returning Direct3D interface at %p.\n", *obj);
175 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
177 This->d3dversion = 2;
178 *obj = ICOM_INTERFACE(This, IDirect3D2);
179 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
181 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
183 This->d3dversion = 3;
184 *obj = ICOM_INTERFACE(This, IDirect3D3);
185 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
187 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
189 This->d3dversion = 7;
190 *obj = ICOM_INTERFACE(This, IDirect3D7);
191 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
195 /* Unknown interface */
198 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
199 LeaveCriticalSection(&ddraw_cs);
200 return E_NOINTERFACE;
203 IUnknown_AddRef( (IUnknown *) *obj );
204 LeaveCriticalSection(&ddraw_cs);
208 /*****************************************************************************
209 * IDirectDraw7::AddRef
211 * Increases the interfaces refcount, basically
213 * DDraw refcounting is a bit tricky. The different DirectDraw interface
214 * versions have individual refcounts, but the IDirect3D interfaces do not.
215 * All interfaces are from one object, that means calling QueryInterface on an
216 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
217 * IDirectDrawImpl object.
219 * That means all AddRef and Release implementations of IDirectDrawX work
220 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
221 * except of IDirect3D7 which thunks to IDirectDraw7
223 * Returns: The new refcount
225 *****************************************************************************/
227 IDirectDrawImpl_AddRef(IDirectDraw7 *iface)
229 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
230 ULONG ref = InterlockedIncrement(&This->ref7);
232 TRACE("(%p) : incrementing IDirectDraw7 refcount from %u.\n", This, ref -1);
234 if(ref == 1) InterlockedIncrement(&This->numIfaces);
239 /*****************************************************************************
240 * IDirectDrawImpl_Destroy
242 * Destroys a ddraw object if all refcounts are 0. This is to share code
243 * between the IDirectDrawX::Release functions
246 * This: DirectDraw object to destroy
248 *****************************************************************************/
250 IDirectDrawImpl_Destroy(IDirectDrawImpl *This)
252 /* Clear the cooplevel to restore window and display mode */
253 IDirectDraw7_SetCooperativeLevel(ICOM_INTERFACE(This, IDirectDraw7),
257 /* Destroy the device window if we created one */
258 if(This->devicewindow != 0)
260 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
261 DestroyWindow(This->devicewindow);
262 This->devicewindow = 0;
265 /* Unregister the window class */
266 UnregisterClassA(This->classname, 0);
268 EnterCriticalSection(&ddraw_cs);
269 list_remove(&This->ddraw_list_entry);
270 LeaveCriticalSection(&ddraw_cs);
272 /* Release the attached WineD3D stuff */
273 IWineD3DDevice_Release(This->wineD3DDevice);
274 IWineD3D_Release(This->wineD3D);
276 /* Now free the object */
277 HeapFree(GetProcessHeap(), 0, This);
280 /*****************************************************************************
281 * IDirectDraw7::Release
283 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
285 * Returns: The new refcount
286 *****************************************************************************/
288 IDirectDrawImpl_Release(IDirectDraw7 *iface)
290 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
291 ULONG ref = InterlockedDecrement(&This->ref7);
293 TRACE("(%p)->() decrementing IDirectDraw7 refcount from %u.\n", This, ref +1);
297 ULONG ifacecount = InterlockedDecrement(&This->numIfaces);
298 if(ifacecount == 0) IDirectDrawImpl_Destroy(This);
304 /*****************************************************************************
305 * IDirectDraw methods
306 *****************************************************************************/
308 /*****************************************************************************
309 * IDirectDraw7::SetCooperativeLevel
311 * Sets the cooperative level for the DirectDraw object, and the window
312 * assigned to it. The cooperative level determines the general behavior
313 * of the DirectDraw application
315 * Warning: This is quite tricky, as it's not really documented which
316 * cooperative levels can be combined with each other. If a game fails
317 * after this function, try to check the cooperative levels passed on
318 * Windows, and if it returns something different.
320 * If you think that this function caused the failure because it writes a
321 * fixme, be sure to run again with a +ddraw trace.
323 * What is known about cooperative levels (See the ddraw modes test):
324 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
325 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
326 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
327 * DDSCL_FULLSCREEN can be activated
328 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
330 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
331 * DDSCL_SETFOCUSWINDOW (partially),
332 * DDSCL_MULTITHREADED (work in progress)
334 * Unhandled flags, which should be implemented
335 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
336 * expect any difference to a normal window for wine)
337 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
338 * rendering (Possible test case: Half-life)
340 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
342 * These don't seem very important for wine:
343 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
346 * DD_OK if the cooperative level was set successfully
347 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
348 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
349 * (Probably others too, have to investigate)
351 *****************************************************************************/
352 static HRESULT WINAPI
353 IDirectDrawImpl_SetCooperativeLevel(IDirectDraw7 *iface,
357 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
361 TRACE("(%p)->(%p,%08x)\n",This,hwnd,cooplevel);
362 DDRAW_dump_cooperativelevel(cooplevel);
364 EnterCriticalSection(&ddraw_cs);
366 /* Get the old window */
367 hr = IWineD3DDevice_GetHWND(This->wineD3DDevice, &window);
370 ERR("IWineD3DDevice::GetHWND failed, hr = %08x\n", hr);
371 LeaveCriticalSection(&ddraw_cs);
375 /* Tests suggest that we need one of them: */
376 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
380 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
381 LeaveCriticalSection(&ddraw_cs);
382 return DDERR_INVALIDPARAMS;
385 /* Handle those levels first which set various hwnds */
386 if(cooplevel & DDSCL_SETFOCUSWINDOW)
388 /* This isn't compatible with a lot of flags */
389 if(cooplevel & ( DDSCL_MULTITHREADED |
394 DDSCL_SETDEVICEWINDOW |
399 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
400 LeaveCriticalSection(&ddraw_cs);
401 return DDERR_INVALIDPARAMS;
403 else if( (This->cooperative_level & DDSCL_FULLSCREEN) && window)
405 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET\n");
406 LeaveCriticalSection(&ddraw_cs);
407 return DDERR_HWNDALREADYSET;
410 This->focuswindow = hwnd;
411 /* Won't use the hwnd param for anything else */
414 /* Use the focus window for drawing too */
415 IWineD3DDevice_SetHWND(This->wineD3DDevice, This->focuswindow);
417 /* Destroy the device window, if we have one */
418 if(This->devicewindow)
420 DestroyWindow(This->devicewindow);
421 This->devicewindow = NULL;
424 /* DDSCL_NORMAL or DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE */
425 if(cooplevel & DDSCL_NORMAL)
427 /* Can't coexist with fullscreen or exclusive */
428 if(cooplevel & (DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE) )
430 TRACE("(%p) DDSCL_NORMAL is not compative with DDSCL_FULLSCREEN or DDSCL_EXCLUSIVE\n", This);
431 LeaveCriticalSection(&ddraw_cs);
432 return DDERR_INVALIDPARAMS;
435 /* Switching from fullscreen? */
436 if(This->cooperative_level & DDSCL_FULLSCREEN)
438 /* Restore the display mode */
439 IDirectDraw7_RestoreDisplayMode(iface);
441 This->cooperative_level &= ~DDSCL_FULLSCREEN;
442 This->cooperative_level &= ~DDSCL_EXCLUSIVE;
443 This->cooperative_level &= ~DDSCL_ALLOWMODEX;
446 /* Don't override focus windows or private device windows */
448 !(This->focuswindow) &&
449 !(This->devicewindow) &&
452 IWineD3DDevice_SetHWND(This->wineD3DDevice, hwnd);
455 IWineD3DDevice_SetFullscreen(This->wineD3DDevice,
458 else if(cooplevel & DDSCL_FULLSCREEN)
460 /* Needs DDSCL_EXCLUSIVE */
461 if(!(cooplevel & DDSCL_EXCLUSIVE) )
463 TRACE("(%p) DDSCL_FULLSCREEN needs DDSCL_EXCLUSIVE\n", This);
464 LeaveCriticalSection(&ddraw_cs);
465 return DDERR_INVALIDPARAMS;
470 TRACE("(%p) DDSCL_FULLSCREEN needs a HWND\n", This);
471 return DDERR_INVALIDPARAMS;
475 This->cooperative_level &= ~DDSCL_NORMAL;
476 IWineD3DDevice_SetFullscreen(This->wineD3DDevice,
479 /* Don't override focus windows or private device windows */
481 !(This->focuswindow) &&
482 !(This->devicewindow) &&
485 IWineD3DDevice_SetHWND(This->wineD3DDevice, hwnd);
488 else if(cooplevel & DDSCL_EXCLUSIVE)
490 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN\n", This);
491 LeaveCriticalSection(&ddraw_cs);
492 return DDERR_INVALIDPARAMS;
495 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
497 /* Don't create a device window if a focus window is set */
498 if( !(This->focuswindow) )
500 HWND devicewindow = CreateWindowExA(0, This->classname, "DDraw device window",
502 GetSystemMetrics(SM_CXSCREEN),
503 GetSystemMetrics(SM_CYSCREEN),
504 NULL, NULL, GetModuleHandleA(0), NULL);
506 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
507 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
509 IWineD3DDevice_SetHWND(This->wineD3DDevice, devicewindow);
510 This->devicewindow = devicewindow;
514 if(cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
516 /* Enable thread safety in wined3d */
517 IWineD3DDevice_SetMultithreaded(This->wineD3DDevice);
520 /* Unhandled flags */
521 if(cooplevel & DDSCL_ALLOWREBOOT)
522 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
523 if(cooplevel & DDSCL_ALLOWMODEX)
524 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
525 if(cooplevel & DDSCL_FPUSETUP)
526 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
527 if(cooplevel & DDSCL_FPUPRESERVE)
528 WARN("(%p) Unhandled flag DDSCL_FPUPRESERVE, harmless\n", This);
530 /* Store the cooperative_level */
531 This->cooperative_level |= cooplevel;
532 TRACE("SetCooperativeLevel retuning DD_OK\n");
533 LeaveCriticalSection(&ddraw_cs);
537 /*****************************************************************************
539 * Helper function for SetDisplayMode and RestoreDisplayMode
541 * Implements DirectDraw's SetDisplayMode, but ignores the value of
542 * ForceRefreshRate, since it is already handled by
543 * IDirectDrawImpl_SetDisplayMode. RestoreDisplayMode can use this function
544 * without worrying that ForceRefreshRate will override the refresh rate. For
545 * argument and return value documentation, see
546 * IDirectDrawImpl_SetDisplayMode.
548 *****************************************************************************/
550 IDirectDrawImpl_SetDisplayModeNoOverride(IDirectDraw7 *iface,
557 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
558 WINED3DDISPLAYMODE Mode;
560 TRACE("(%p)->(%d,%d,%d,%d,%x: Relay!\n", This, Width, Height, BPP, RefreshRate, Flags);
562 EnterCriticalSection(&ddraw_cs);
563 if( !Width || !Height )
565 ERR("Width=%d, Height=%d, what to do?\n", Width, Height);
566 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
567 LeaveCriticalSection(&ddraw_cs);
571 /* Check the exclusive mode
572 if(!(This->cooperative_level & DDSCL_EXCLUSIVE))
573 return DDERR_NOEXCLUSIVEMODE;
574 * This is WRONG. Don't know if the SDK is completely
575 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
576 * is returned, but Half-Life 1.1.1.1 (Steam version)
581 Mode.Height = Height;
582 Mode.RefreshRate = RefreshRate;
585 case 8: Mode.Format = WINED3DFMT_P8; break;
586 case 15: Mode.Format = WINED3DFMT_X1R5G5B5; break;
587 case 16: Mode.Format = WINED3DFMT_R5G6B5; break;
588 case 24: Mode.Format = WINED3DFMT_R8G8B8; break;
589 case 32: Mode.Format = WINED3DFMT_X8R8G8B8; break;
592 /* TODO: The possible return values from msdn suggest that
593 * the screen mode can't be changed if a surface is locked
594 * or some drawing is in progress
597 /* TODO: Lose the primary surface */
598 hr = IWineD3DDevice_SetDisplayMode(This->wineD3DDevice,
599 0, /* First swapchain */
601 LeaveCriticalSection(&ddraw_cs);
604 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
609 /*****************************************************************************
610 * IDirectDraw7::SetDisplayMode
612 * Sets the display screen resolution, color depth and refresh frequency
613 * when in fullscreen mode (in theory).
614 * Possible return values listed in the SDK suggest that this method fails
615 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
616 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
617 * It seems to be valid to pass 0 for With and Height, this has to be tested
618 * It could mean that the current video mode should be left as-is. (But why
622 * Height, Width: Screen dimension
623 * BPP: Color depth in Bits per pixel
624 * Refreshrate: Screen refresh rate
630 *****************************************************************************/
631 static HRESULT WINAPI
632 IDirectDrawImpl_SetDisplayMode(IDirectDraw7 *iface,
639 if (force_refresh_rate != 0)
641 TRACE("ForceRefreshRate overriding passed-in refresh rate (%d Hz) to %d Hz\n", RefreshRate, force_refresh_rate);
642 RefreshRate = force_refresh_rate;
645 return IDirectDrawImpl_SetDisplayModeNoOverride(iface, Width, Height, BPP,
649 /*****************************************************************************
650 * IDirectDraw7::RestoreDisplayMode
652 * Restores the display mode to what it was at creation time. Basically.
654 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
655 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
656 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
657 * -> DD_1 is released. The screen should be left at 1024x768x32.
658 * -> DD_2 is released. The screen should be set to 1400x1050x32
659 * This case is unhandled right now, but Empire Earth does it this way.
660 * (But perhaps there is something in SetCooperativeLevel to prevent this)
662 * The msdn says that this method resets the display mode to what it was before
663 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
667 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
669 *****************************************************************************/
670 static HRESULT WINAPI
671 IDirectDrawImpl_RestoreDisplayMode(IDirectDraw7 *iface)
673 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
674 TRACE("(%p)\n", This);
676 return IDirectDrawImpl_SetDisplayModeNoOverride(ICOM_INTERFACE(This, IDirectDraw7),
684 /*****************************************************************************
685 * IDirectDraw7::GetCaps
687 * Returns the drives capabilities
689 * Used for version 1, 2, 4 and 7
692 * DriverCaps: Structure to write the Hardware accelerated caps to
693 * HelCaps: Structure to write the emulation caps to
696 * This implementation returns DD_OK only
698 *****************************************************************************/
699 static HRESULT WINAPI
700 IDirectDrawImpl_GetCaps(IDirectDraw7 *iface,
704 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
705 TRACE("(%p)->(%p,%p)\n", This, DriverCaps, HELCaps);
707 /* One structure must be != NULL */
708 if( (!DriverCaps) && (!HELCaps) )
710 ERR("(%p) Invalid params to IDirectDrawImpl_GetCaps\n", This);
711 return DDERR_INVALIDPARAMS;
716 DD_STRUCT_COPY_BYSIZE(DriverCaps, &This->caps);
719 TRACE("Driver Caps :\n");
720 DDRAW_dump_DDCAPS(DriverCaps);
726 DD_STRUCT_COPY_BYSIZE(HELCaps, &This->caps);
729 TRACE("HEL Caps :\n");
730 DDRAW_dump_DDCAPS(HELCaps);
737 /*****************************************************************************
738 * IDirectDraw7::Compact
740 * No idea what it does, MSDN says it's not implemented.
743 * DD_OK, but this is unchecked
745 *****************************************************************************/
746 static HRESULT WINAPI
747 IDirectDrawImpl_Compact(IDirectDraw7 *iface)
749 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
750 TRACE("(%p)\n", This);
755 /*****************************************************************************
756 * IDirectDraw7::GetDisplayMode
758 * Returns information about the current display mode
760 * Exists in Version 1, 2, 4 and 7
763 * DDSD: Address of a surface description structure to write the info to
768 *****************************************************************************/
769 static HRESULT WINAPI
770 IDirectDrawImpl_GetDisplayMode(IDirectDraw7 *iface,
771 DDSURFACEDESC2 *DDSD)
773 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
775 WINED3DDISPLAYMODE Mode;
777 TRACE("(%p)->(%p): Relay\n", This, DDSD);
779 EnterCriticalSection(&ddraw_cs);
780 /* This seems sane */
783 LeaveCriticalSection(&ddraw_cs);
784 return DDERR_INVALIDPARAMS;
787 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
788 * so one method can be used for all versions (Hopefully)
790 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
795 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
796 LeaveCriticalSection(&ddraw_cs);
801 memset(DDSD, 0, Size);
804 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
805 DDSD->dwWidth = Mode.Width;
806 DDSD->dwHeight = Mode.Height;
807 DDSD->u2.dwRefreshRate = 60;
808 DDSD->ddsCaps.dwCaps = 0;
809 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
810 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, Mode.Format);
811 DDSD->u1.lPitch = Mode.Width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
815 TRACE("Returning surface desc :\n");
816 DDRAW_dump_surface_desc(DDSD);
819 LeaveCriticalSection(&ddraw_cs);
823 /*****************************************************************************
824 * IDirectDraw7::GetFourCCCodes
826 * Returns an array of supported FourCC codes.
828 * Exists in Version 1, 2, 4 and 7
831 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
832 * of enumerated codes
833 * Codes: Pointer to an array of DWORDs where the supported codes are written
837 * Always returns DD_OK, as it's a stub for now
839 *****************************************************************************/
840 static HRESULT WINAPI
841 IDirectDrawImpl_GetFourCCCodes(IDirectDraw7 *iface,
842 DWORD *NumCodes, DWORD *Codes)
844 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
845 FIXME("(%p)->(%p, %p): Stub!\n", This, NumCodes, Codes);
847 if(NumCodes) *NumCodes = 0;
852 /*****************************************************************************
853 * IDirectDraw7::GetMonitorFrequency
855 * Returns the monitor's frequency
857 * Exists in Version 1, 2, 4 and 7
860 * Freq: Pointer to a DWORD to write the frequency to
863 * Always returns DD_OK
865 *****************************************************************************/
866 static HRESULT WINAPI
867 IDirectDrawImpl_GetMonitorFrequency(IDirectDraw7 *iface,
870 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
871 TRACE("(%p)->(%p)\n", This, Freq);
873 /* Ideally this should be in WineD3D, as it concerns the screen setup,
874 * but for now this should make the games happy
880 /*****************************************************************************
881 * IDirectDraw7::GetVerticalBlankStatus
883 * Returns the Vertical blank status of the monitor. This should be in WineD3D
884 * too basically, but as it's a semi stub, I didn't create a function there
887 * status: Pointer to a BOOL to be filled with the vertical blank status
891 * DDERR_INVALIDPARAMS if status is NULL
893 *****************************************************************************/
894 static HRESULT WINAPI
895 IDirectDrawImpl_GetVerticalBlankStatus(IDirectDraw7 *iface,
898 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
899 TRACE("(%p)->(%p)\n", This, status);
901 /* This looks sane, the MSDN suggests it too */
902 EnterCriticalSection(&ddraw_cs);
905 LeaveCriticalSection(&ddraw_cs);
906 return DDERR_INVALIDPARAMS;
909 *status = This->fake_vblank;
910 This->fake_vblank = !This->fake_vblank;
911 LeaveCriticalSection(&ddraw_cs);
915 /*****************************************************************************
916 * IDirectDraw7::GetAvailableVidMem
918 * Returns the total and free video memory
921 * Caps: Specifies the memory type asked for
922 * total: Pointer to a DWORD to be filled with the total memory
923 * free: Pointer to a DWORD to be filled with the free memory
927 * DDERR_INVALIDPARAMS of free and total are NULL
929 *****************************************************************************/
930 static HRESULT WINAPI
931 IDirectDrawImpl_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total, DWORD *free)
933 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
934 TRACE("(%p)->(%p, %p, %p)\n", This, Caps, total, free);
938 TRACE("(%p) Asked for memory with description: ", This);
939 DDRAW_dump_DDSCAPS2(Caps);
941 EnterCriticalSection(&ddraw_cs);
943 /* Todo: System memory vs local video memory vs non-local video memory
944 * The MSDN also mentions differences between texture memory and other
945 * resources, but that's not important
948 if( (!total) && (!free) )
950 LeaveCriticalSection(&ddraw_cs);
951 return DDERR_INVALIDPARAMS;
954 if(total) *total = This->total_vidmem;
955 if(free) *free = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
957 LeaveCriticalSection(&ddraw_cs);
961 /*****************************************************************************
962 * IDirectDraw7::Initialize
964 * Initializes a DirectDraw interface.
967 * GUID: Interface identifier. Well, don't know what this is really good
971 * Returns DD_OK on the first call,
972 * DDERR_ALREADYINITIALIZED on repeated calls
974 *****************************************************************************/
975 static HRESULT WINAPI
976 IDirectDrawImpl_Initialize(IDirectDraw7 *iface,
979 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
980 TRACE("(%p)->(%s): No-op\n", This, debugstr_guid(Guid));
982 if(This->initialized)
984 return DDERR_ALREADYINITIALIZED;
992 /*****************************************************************************
993 * IDirectDraw7::FlipToGDISurface
995 * "Makes the surface that the GDI writes to the primary surface"
996 * Looks like some windows specific thing we don't have to care about.
997 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
998 * show error boxes ;)
999 * Well, just return DD_OK.
1002 * Always returns DD_OK
1004 *****************************************************************************/
1005 static HRESULT WINAPI
1006 IDirectDrawImpl_FlipToGDISurface(IDirectDraw7 *iface)
1008 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1009 TRACE("(%p)\n", This);
1014 /*****************************************************************************
1015 * IDirectDraw7::WaitForVerticalBlank
1017 * This method allows applications to get in sync with the vertical blank
1019 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1020 * redraw the screen, most likely because of this stub
1023 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1024 * or DDWAITVB_BLOCKEND
1025 * h: Not used, according to MSDN
1028 * Always returns DD_OK
1030 *****************************************************************************/
1031 static HRESULT WINAPI
1032 IDirectDrawImpl_WaitForVerticalBlank(IDirectDraw7 *iface,
1036 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1037 FIXME("(%p)->(%x,%p): Stub\n", This, Flags, h);
1039 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1040 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1041 return DDERR_UNSUPPORTED; /* unchecked */
1046 /*****************************************************************************
1047 * IDirectDraw7::GetScanLine
1049 * Returns the scan line that is being drawn on the monitor
1052 * Scanline: Address to write the scan line value to
1055 * Always returns DD_OK
1057 *****************************************************************************/
1058 static HRESULT WINAPI IDirectDrawImpl_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1060 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1061 static BOOL hide = FALSE;
1062 WINED3DDISPLAYMODE Mode;
1064 /* This function is called often, so print the fixme only once */
1065 EnterCriticalSection(&ddraw_cs);
1068 FIXME("(%p)->(%p): Semi-Stub\n", This, Scanline);
1072 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1076 /* Fake the line sweeping of the monitor */
1077 /* FIXME: We should synchronize with a source to keep the refresh rate */
1078 *Scanline = This->cur_scanline++;
1079 /* Assume 20 scan lines in the vertical blank */
1080 if (This->cur_scanline >= Mode.Height + 20)
1081 This->cur_scanline = 0;
1083 LeaveCriticalSection(&ddraw_cs);
1087 /*****************************************************************************
1088 * IDirectDraw7::TestCooperativeLevel
1090 * Informs the application about the state of the video adapter, depending
1091 * on the cooperative level
1094 * DD_OK if the device is in a sane state
1095 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1096 * if the state is not correct(See below)
1098 *****************************************************************************/
1099 static HRESULT WINAPI
1100 IDirectDrawImpl_TestCooperativeLevel(IDirectDraw7 *iface)
1102 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1104 TRACE("(%p)\n", This);
1106 EnterCriticalSection(&ddraw_cs);
1107 /* Description from MSDN:
1108 * For fullscreen apps return DDERR_NOEXCLUSIVEMODE if the user switched
1109 * away from the app with e.g. alt-tab. Windowed apps receive
1110 * DDERR_EXCLUSIVEMODEALREADYSET if another application created a
1111 * DirectDraw object in exclusive mode. DDERR_WRONGMODE is returned,
1112 * when the video mode has changed
1115 hr = IWineD3DDevice_TestCooperativeLevel(This->wineD3DDevice);
1117 /* Fix the result value. These values are mapped from their
1122 case WINED3DERR_DEVICELOST:
1123 if(This->cooperative_level & DDSCL_EXCLUSIVE)
1125 LeaveCriticalSection(&ddraw_cs);
1126 return DDERR_NOEXCLUSIVEMODE;
1130 LeaveCriticalSection(&ddraw_cs);
1131 return DDERR_EXCLUSIVEMODEALREADYSET;
1134 case WINED3DERR_DEVICENOTRESET:
1135 LeaveCriticalSection(&ddraw_cs);
1139 LeaveCriticalSection(&ddraw_cs);
1142 case WINED3DERR_DRIVERINTERNALERROR:
1144 ERR("(%p) Unexpected return value %08x from wineD3D, "
1145 " returning DD_OK\n", This, hr);
1148 LeaveCriticalSection(&ddraw_cs);
1152 /*****************************************************************************
1153 * IDirectDraw7::GetGDISurface
1155 * Returns the surface that GDI is treating as the primary surface.
1156 * For Wine this is the front buffer
1159 * GDISurface: Address to write the surface pointer to
1162 * DD_OK if the surface was found
1163 * DDERR_NOTFOUND if the GDI surface wasn't found
1165 *****************************************************************************/
1166 static HRESULT WINAPI
1167 IDirectDrawImpl_GetGDISurface(IDirectDraw7 *iface,
1168 IDirectDrawSurface7 **GDISurface)
1170 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1171 IWineD3DSurface *Surf;
1172 IDirectDrawSurface7 *ddsurf;
1175 TRACE("(%p)->(%p)\n", This, GDISurface);
1177 /* Get the back buffer from the wineD3DDevice and search its
1178 * attached surfaces for the front buffer
1180 EnterCriticalSection(&ddraw_cs);
1181 hr = IWineD3DDevice_GetBackBuffer(This->wineD3DDevice,
1183 0, /* first back buffer*/
1184 WINED3DBACKBUFFER_TYPE_MONO,
1187 if( (hr != D3D_OK) ||
1190 ERR("IWineD3DDevice::GetBackBuffer failed\n");
1191 LeaveCriticalSection(&ddraw_cs);
1192 return DDERR_NOTFOUND;
1195 /* GetBackBuffer AddRef()ed the surface, release it */
1196 IWineD3DSurface_Release(Surf);
1198 IWineD3DSurface_GetParent(Surf,
1199 (IUnknown **) &ddsurf);
1200 IDirectDrawSurface7_Release(ddsurf); /* For the GetParent */
1202 /* Find the front buffer */
1203 ddsCaps.dwCaps = DDSCAPS_FRONTBUFFER;
1204 hr = IDirectDrawSurface7_GetAttachedSurface(ddsurf,
1209 ERR("IDirectDrawSurface7::GetAttachedSurface failed, hr = %x\n", hr);
1212 /* The AddRef is OK this time */
1213 LeaveCriticalSection(&ddraw_cs);
1217 /*****************************************************************************
1218 * IDirectDraw7::EnumDisplayModes
1220 * Enumerates the supported Display modes. The modes can be filtered with
1221 * the DDSD parameter.
1224 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES
1225 * DDSD: Surface description to filter the modes
1226 * Context: Pointer passed back to the callback function
1227 * cb: Application-provided callback function
1231 * DDERR_INVALIDPARAMS if the callback wasn't set
1233 *****************************************************************************/
1234 static HRESULT WINAPI
1235 IDirectDrawImpl_EnumDisplayModes(IDirectDraw7 *iface,
1237 DDSURFACEDESC2 *DDSD,
1239 LPDDENUMMODESCALLBACK2 cb)
1241 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1242 unsigned int modenum, fmt;
1243 WINED3DFORMAT pixelformat = WINED3DFMT_UNKNOWN;
1244 WINED3DDISPLAYMODE mode;
1245 DDSURFACEDESC2 callback_sd;
1246 WINED3DDISPLAYMODE *enum_modes = NULL;
1247 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
1249 WINED3DFORMAT checkFormatList[] =
1252 WINED3DFMT_A8R8G8B8,
1253 WINED3DFMT_X8R8G8B8,
1255 WINED3DFMT_X1R5G5B5,
1256 WINED3DFMT_A1R5G5B5,
1257 WINED3DFMT_A4R4G4B4,
1259 WINED3DFMT_A8R3G3B2,
1260 WINED3DFMT_X4R4G4B4,
1261 WINED3DFMT_A2B10G10R10,
1262 WINED3DFMT_A8B8G8R8,
1263 WINED3DFMT_X8B8G8R8,
1264 WINED3DFMT_A2R10G10B10,
1269 TRACE("(%p)->(%p,%p,%p): Relay\n", This, DDSD, Context, cb);
1271 EnterCriticalSection(&ddraw_cs);
1272 /* This looks sane */
1275 LeaveCriticalSection(&ddraw_cs);
1276 return DDERR_INVALIDPARAMS;
1281 if ((DDSD->dwFlags & DDSD_PIXELFORMAT) && (DDSD->u4.ddpfPixelFormat.dwFlags & DDPF_RGB) )
1282 pixelformat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
1285 if(!(Flags & DDEDM_REFRESHRATES))
1287 enum_mode_array_size = 16;
1288 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1291 ERR("Out of memory\n");
1292 LeaveCriticalSection(&ddraw_cs);
1293 return DDERR_OUTOFMEMORY;
1297 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
1299 if(pixelformat != WINED3DFMT_UNKNOWN && checkFormatList[fmt] != pixelformat)
1305 while(IWineD3D_EnumAdapterModes(This->wineD3D,
1306 WINED3DADAPTER_DEFAULT,
1307 checkFormatList[fmt],
1309 &mode) == WINED3D_OK)
1313 if(DDSD->dwFlags & DDSD_WIDTH && mode.Width != DDSD->dwWidth) continue;
1314 if(DDSD->dwFlags & DDSD_HEIGHT && mode.Height != DDSD->dwHeight) continue;
1317 if(!(Flags & DDEDM_REFRESHRATES))
1319 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
1320 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
1321 * to be reduced to a single unique result in such case.
1326 for (i = 0; i < enum_mode_count; i++)
1328 if(enum_modes[i].Width == mode.Width && enum_modes[i].Height == mode.Height &&
1329 enum_modes[i].Format == mode.Format)
1339 memset(&callback_sd, 0, sizeof(callback_sd));
1340 callback_sd.dwSize = sizeof(callback_sd);
1341 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1343 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH;
1344 if(Flags & DDEDM_REFRESHRATES)
1346 callback_sd.dwFlags |= DDSD_REFRESHRATE;
1347 callback_sd.u2.dwRefreshRate = mode.RefreshRate;
1350 callback_sd.dwWidth = mode.Width;
1351 callback_sd.dwHeight = mode.Height;
1353 PixelFormat_WineD3DtoDD(&callback_sd.u4.ddpfPixelFormat, mode.Format);
1355 /* Calc pitch and DWORD align like MSDN says */
1356 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.Width;
1357 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
1359 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
1360 callback_sd.u2.dwRefreshRate);
1362 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
1364 TRACE("Application asked to terminate the enumeration\n");
1365 HeapFree(GetProcessHeap(), 0, enum_modes);
1366 LeaveCriticalSection(&ddraw_cs);
1370 if(!(Flags & DDEDM_REFRESHRATES))
1372 if (enum_mode_count == enum_mode_array_size)
1374 WINED3DDISPLAYMODE *new_enum_modes;
1376 enum_mode_array_size *= 2;
1377 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1379 if (!new_enum_modes)
1381 ERR("Out of memory\n");
1382 HeapFree(GetProcessHeap(), 0, enum_modes);
1383 LeaveCriticalSection(&ddraw_cs);
1384 return DDERR_OUTOFMEMORY;
1387 enum_modes = new_enum_modes;
1390 enum_modes[enum_mode_count++] = mode;
1395 TRACE("End of enumeration\n");
1396 HeapFree(GetProcessHeap(), 0, enum_modes);
1397 LeaveCriticalSection(&ddraw_cs);
1401 /*****************************************************************************
1402 * IDirectDraw7::EvaluateMode
1404 * Used with IDirectDraw7::StartModeTest to test video modes.
1405 * EvaluateMode is used to pass or fail a mode, and continue with the next
1409 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
1410 * Timeout: Returns the amount of seconds left before the mode would have
1411 * been failed automatically
1414 * This implementation always DD_OK, because it's a stub
1416 *****************************************************************************/
1417 static HRESULT WINAPI
1418 IDirectDrawImpl_EvaluateMode(IDirectDraw7 *iface,
1422 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1423 FIXME("(%p)->(%d,%p): Stub!\n", This, Flags, Timeout);
1425 /* When implementing this, implement it in WineD3D */
1430 /*****************************************************************************
1431 * IDirectDraw7::GetDeviceIdentifier
1433 * Returns the device identifier, which gives information about the driver
1434 * Our device identifier is defined at the beginning of this file.
1437 * DDDI: Address for the returned structure
1438 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
1441 * On success it returns DD_OK
1442 * DDERR_INVALIDPARAMS if DDDI is NULL
1444 *****************************************************************************/
1445 static HRESULT WINAPI
1446 IDirectDrawImpl_GetDeviceIdentifier(IDirectDraw7 *iface,
1447 DDDEVICEIDENTIFIER2 *DDDI,
1450 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1451 TRACE("(%p)->(%p,%08x)\n", This, DDDI, Flags);
1454 return DDERR_INVALIDPARAMS;
1456 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
1457 * host adapter, if there's a secondary 3D adapter. This doesn't apply
1458 * to any modern hardware, nor is it interesting for Wine, so ignore it
1461 *DDDI = deviceidentifier;
1465 /*****************************************************************************
1466 * IDirectDraw7::GetSurfaceFromDC
1468 * Returns the Surface for a GDI device context handle.
1469 * Is this related to IDirectDrawSurface::GetDC ???
1472 * hdc: hdc to return the surface for
1473 * Surface: Address to write the surface pointer to
1476 * Always returns DD_OK because it's a stub
1478 *****************************************************************************/
1479 static HRESULT WINAPI
1480 IDirectDrawImpl_GetSurfaceFromDC(IDirectDraw7 *iface,
1482 IDirectDrawSurface7 **Surface)
1484 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1485 FIXME("(%p)->(%p,%p): Stub!\n", This, hdc, Surface);
1487 /* Implementation idea if needed: Loop through all surfaces and compare
1488 * their hdc with hdc. Implement it in WineD3D! */
1489 return DDERR_NOTFOUND;
1492 /*****************************************************************************
1493 * IDirectDraw7::RestoreAllSurfaces
1495 * Calls the restore method of all surfaces
1500 * Always returns DD_OK because it's a stub
1502 *****************************************************************************/
1503 static HRESULT WINAPI
1504 IDirectDrawImpl_RestoreAllSurfaces(IDirectDraw7 *iface)
1506 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1507 FIXME("(%p): Stub\n", This);
1509 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
1510 * get their parent and call their restore method. Do not implement
1511 * it in WineD3D, as restoring a surface means re-creating the
1517 /*****************************************************************************
1518 * IDirectDraw7::StartModeTest
1520 * Tests the specified video modes to update the system registry with
1521 * refresh rate information. StartModeTest starts the mode test,
1522 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
1523 * isn't called within 15 seconds, the mode is failed automatically
1525 * As refresh rates are handled by the X server, I don't think this
1526 * Method is important
1529 * Modes: An array of mode specifications
1530 * NumModes: The number of modes in Modes
1531 * Flags: Some flags...
1534 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
1535 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
1538 *****************************************************************************/
1539 static HRESULT WINAPI
1540 IDirectDrawImpl_StartModeTest(IDirectDraw7 *iface,
1545 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1546 WARN("(%p)->(%p, %d, %x): Semi-Stub, most likely harmless\n", This, Modes, NumModes, Flags);
1548 /* This looks sane */
1549 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
1551 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
1552 * As it is not, DDERR_TESTFINISHED is returned
1553 * (hopefully that's correct
1555 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
1556 * well, that value doesn't (yet) exist in the wine headers, so ignore it
1562 /*****************************************************************************
1563 * IDirectDrawImpl_RecreateSurfacesCallback
1565 * Enumeration callback for IDirectDrawImpl_RecreateAllSurfaces.
1566 * It re-recreates the WineD3DSurface. It's pretty straightforward
1568 *****************************************************************************/
1570 IDirectDrawImpl_RecreateSurfacesCallback(IDirectDrawSurface7 *surf,
1571 DDSURFACEDESC2 *desc,
1574 IDirectDrawSurfaceImpl *surfImpl = ICOM_OBJECT(IDirectDrawSurfaceImpl,
1575 IDirectDrawSurface7,
1577 IDirectDrawImpl *This = surfImpl->ddraw;
1579 IParentImpl *parImpl = NULL;
1580 IWineD3DSurface *wineD3DSurface;
1583 IWineD3DClipper *clipper = NULL;
1585 WINED3DSURFACE_DESC Desc;
1586 WINED3DFORMAT Format;
1587 WINED3DRESOURCETYPE Type;
1592 WINED3DMULTISAMPLE_TYPE MultiSampleType;
1593 DWORD MultiSampleQuality;
1597 TRACE("(%p): Enumerated Surface %p\n", This, surfImpl);
1599 /* For the enumeration */
1600 IDirectDrawSurface7_Release(surf);
1602 if(surfImpl->ImplType == This->ImplType) return DDENUMRET_OK; /* Continue */
1604 /* Get the objects */
1605 wineD3DSurface = surfImpl->WineD3DSurface;
1606 IWineD3DSurface_GetParent(wineD3DSurface, &Parent);
1607 IUnknown_Release(Parent); /* For the getParent */
1609 /* Is the parent an IParent interface? */
1610 if(IUnknown_QueryInterface(Parent, &IID_IParent, &tmp) == S_OK)
1612 /* It is a IParent interface! */
1613 IUnknown_Release(Parent); /* For the QueryInterface */
1614 parImpl = ICOM_OBJECT(IParentImpl, IParent, Parent);
1615 /* Release the reference the parent interface is holding */
1616 IWineD3DSurface_Release(wineD3DSurface);
1619 /* get the clipper */
1620 IWineD3DSurface_GetClipper(wineD3DSurface, &clipper);
1622 /* Get the surface properties */
1623 Desc.Format = &Format;
1625 Desc.Usage = &Usage;
1628 Desc.MultiSampleType = &MultiSampleType;
1629 Desc.MultiSampleQuality = &MultiSampleQuality;
1630 Desc.Width = &Width;
1631 Desc.Height = &Height;
1633 hr = IWineD3DSurface_GetDesc(wineD3DSurface, &Desc);
1634 if(hr != D3D_OK) return hr;
1636 /* Create the new surface */
1637 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice,
1638 Width, Height, Format,
1639 TRUE /* Lockable */,
1640 FALSE /* Discard */,
1641 surfImpl->mipmap_level,
1642 &surfImpl->WineD3DSurface,
1648 0 /* SharedHandle */,
1655 IWineD3DSurface_SetClipper(surfImpl->WineD3DSurface, clipper);
1657 /* Update the IParent if it exists */
1660 parImpl->child = (IUnknown *) surfImpl->WineD3DSurface;
1661 /* Add a reference for the IParent */
1662 IWineD3DSurface_AddRef(surfImpl->WineD3DSurface);
1664 /* TODO: Copy the surface content, except for render targets */
1666 if(IWineD3DSurface_Release(wineD3DSurface) == 0)
1667 TRACE("Surface released successful, next surface\n");
1669 ERR("Something's still holding the old WineD3DSurface\n");
1671 surfImpl->ImplType = This->ImplType;
1675 IWineD3DClipper_Release(clipper);
1677 return DDENUMRET_OK;
1680 /*****************************************************************************
1681 * IDirectDrawImpl_RecreateAllSurfaces
1683 * A function, that converts all wineD3DSurfaces to the new implementation type
1684 * It enumerates all surfaces with IWineD3DDevice::EnumSurfaces, creates a
1685 * new WineD3DSurface, copies the content and releases the old surface
1687 *****************************************************************************/
1689 IDirectDrawImpl_RecreateAllSurfaces(IDirectDrawImpl *This)
1691 DDSURFACEDESC2 desc;
1692 TRACE("(%p): Switch to implementation %d\n", This, This->ImplType);
1694 if(This->ImplType != SURFACE_OPENGL && This->d3d_initialized)
1696 /* Should happen almost never */
1697 FIXME("(%p) Switching to non-opengl surfaces with d3d started. Is this a bug?\n", This);
1699 IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroyDepthStencilSurface, D3D7CB_DestroySwapChain);
1701 /* Contrary: D3D starting is handled by the caller, because it knows the render target */
1703 memset(&desc, 0, sizeof(desc));
1704 desc.dwSize = sizeof(desc);
1706 return IDirectDraw7_EnumSurfaces(ICOM_INTERFACE(This, IDirectDraw7),
1710 IDirectDrawImpl_RecreateSurfacesCallback);
1713 /*****************************************************************************
1714 * D3D7CB_CreateSurface
1716 * Callback function for IDirect3DDevice_CreateTexture. It searches for the
1717 * correct mipmap sublevel, and returns it to WineD3D.
1718 * The surfaces are created already by IDirectDraw7::CreateSurface
1721 * With, Height: With and height of the surface
1722 * Format: The requested format
1723 * Usage, Pool: D3DUSAGE and D3DPOOL of the surface
1724 * level: The mipmap level
1725 * Face: The cube map face type
1726 * Surface: Pointer to pass the created surface back at
1727 * SharedHandle: NULL
1732 *****************************************************************************/
1733 static HRESULT WINAPI
1734 D3D7CB_CreateSurface(IUnknown *device,
1735 IUnknown *pSuperior,
1736 UINT Width, UINT Height,
1737 WINED3DFORMAT Format,
1738 DWORD Usage, WINED3DPOOL Pool, UINT level,
1739 WINED3DCUBEMAP_FACES Face,
1740 IWineD3DSurface **Surface,
1741 HANDLE *SharedHandle)
1743 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
1744 IDirectDrawSurfaceImpl *surf = NULL;
1746 DDSCAPS2 searchcaps = This->tex_root->surface_desc.ddsCaps;
1747 TRACE("(%p) call back. surf=%p. Face %d level %d\n", device, This->tex_root, Face, level);
1749 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
1752 case WINED3DCUBEMAP_FACE_POSITIVE_X:
1753 TRACE("Asked for positive x\n");
1754 if(searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP) {
1755 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
1757 surf = This->tex_root; break;
1758 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
1759 TRACE("Asked for negative x\n");
1760 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
1761 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
1762 TRACE("Asked for positive y\n");
1763 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
1764 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
1765 TRACE("Asked for negative y\n");
1766 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
1767 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
1768 TRACE("Asked for positive z\n");
1769 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
1770 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
1771 TRACE("Asked for negative z\n");
1772 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
1773 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
1778 IDirectDrawSurface7 *attached;
1779 IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(This->tex_root, IDirectDrawSurface7),
1782 surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attached);
1783 IDirectDrawSurface7_Release(attached);
1785 if(!surf) ERR("root search surface not found\n");
1787 /* Find the wanted mipmap. There are enough mipmaps in the chain */
1790 IDirectDrawSurface7 *attached;
1791 IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(surf, IDirectDrawSurface7),
1794 if(!attached) ERR("Surface not found\n");
1795 surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attached);
1796 IDirectDrawSurface7_Release(attached);
1800 /* Return the surface */
1801 *Surface = surf->WineD3DSurface;
1803 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *Surface, surf);
1807 ULONG WINAPI D3D7CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
1808 IUnknown* swapChainParent;
1809 TRACE("(%p) call back\n", pSwapChain);
1811 IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
1812 IUnknown_Release(swapChainParent);
1813 return IUnknown_Release(swapChainParent);
1816 ULONG WINAPI D3D7CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
1817 IUnknown* surfaceParent;
1818 TRACE("(%p) call back\n", pSurface);
1820 IWineD3DSurface_GetParent(pSurface, &surfaceParent);
1821 IUnknown_Release(surfaceParent);
1822 return IUnknown_Release(surfaceParent);
1825 /*****************************************************************************
1826 * IDirectDrawImpl_CreateNewSurface
1828 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
1829 * with the passed parameters.
1832 * DDSD: Description of the surface to create
1833 * Surf: Address to store the interface pointer at
1838 *****************************************************************************/
1839 static HRESULT WINAPI
1840 IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This,
1841 DDSURFACEDESC2 *pDDSD,
1842 IDirectDrawSurfaceImpl **ppSurf,
1846 UINT Width = 0, Height = 0;
1847 WINED3DFORMAT Format = WINED3DFMT_UNKNOWN;
1848 WINED3DRESOURCETYPE ResType = WINED3DRTYPE_SURFACE;
1850 WINED3DSURFTYPE ImplType = This->ImplType;
1851 WINED3DSURFACE_DESC Desc;
1853 IParentImpl *parImpl = NULL;
1854 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
1856 /* Dummies for GetDesc */
1857 WINED3DPOOL dummy_d3dpool;
1858 WINED3DMULTISAMPLE_TYPE dummy_mst;
1862 if (TRACE_ON(ddraw))
1864 TRACE(" (%p) Requesting surface desc :\n", This);
1865 DDRAW_dump_surface_desc(pDDSD);
1868 /* Select the surface type, if it wasn't choosen yet */
1869 if(ImplType == SURFACE_UNKNOWN)
1871 /* Use GL Surfaces if a D3DDEVICE Surface is requested */
1872 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1874 TRACE("(%p) Choosing GL surfaces because a 3DDEVICE Surface was requested\n", This);
1875 ImplType = SURFACE_OPENGL;
1878 /* Otherwise use GDI surfaces for now */
1881 TRACE("(%p) Choosing GDI surfaces for 2D rendering\n", This);
1882 ImplType = SURFACE_GDI;
1885 /* Policy if all surface implementations are available:
1886 * First, check if a default type was set with winecfg. If not,
1887 * try Xrender surfaces, and use them if they work. Next, check if
1888 * accelerated OpenGL is available, and use GL surfaces in this
1889 * case. If all else fails, use GDI surfaces. If a 3DDEVICE surface
1890 * was created, always use OpenGL surfaces.
1892 * (Note: Xrender surfaces are not implemented for now, the
1893 * unaccelerated implementation uses GDI to render in Software)
1896 /* Store the type. If it needs to be changed, all WineD3DSurfaces have to
1897 * be re-created. This could be done with IDirectDrawSurface7::Restore
1899 This->ImplType = ImplType;
1903 if((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE ) &&
1904 (This->ImplType != SURFACE_OPENGL ) && DefaultSurfaceType == SURFACE_UNKNOWN)
1906 /* We have to change to OpenGL,
1907 * and re-create all WineD3DSurfaces
1909 ImplType = SURFACE_OPENGL;
1910 This->ImplType = ImplType;
1911 TRACE("(%p) Re-creating all surfaces\n", This);
1912 IDirectDrawImpl_RecreateAllSurfaces(This);
1913 TRACE("(%p) Done recreating all surfaces\n", This);
1915 else if(This->ImplType != SURFACE_OPENGL && pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1917 WARN("The application requests a 3D capable surface, but a non-opengl surface was set in the registry\n");
1918 /* Do not fail surface creation, only fail 3D device creation */
1922 /* Get the correct wined3d usage */
1923 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE |
1924 DDSCAPS_BACKBUFFER |
1925 DDSCAPS_3DDEVICE ) )
1927 Usage |= WINED3DUSAGE_RENDERTARGET;
1929 pDDSD->ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY |
1932 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
1934 Usage |= WINED3DUSAGE_OVERLAY;
1936 if(This->depthstencil || (pDDSD->ddsCaps.dwCaps & DDSCAPS_ZBUFFER) )
1938 /* The depth stencil creation callback sets this flag.
1939 * Set the WineD3D usage to let it know that it's a depth
1942 Usage |= WINED3DUSAGE_DEPTHSTENCIL;
1944 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
1946 Pool = WINED3DPOOL_SYSTEMMEM;
1948 else if(pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
1950 Pool = WINED3DPOOL_MANAGED;
1951 /* Managed textures have the system memory flag set */
1952 pDDSD->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
1954 else if(pDDSD->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
1956 /* Videomemory adds localvidmem, this is mutually exclusive with systemmemory
1959 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
1962 Format = PixelFormat_DD2WineD3D(&pDDSD->u4.ddpfPixelFormat);
1963 if(Format == WINED3DFMT_UNKNOWN)
1965 ERR("Unsupported / Unknown pixelformat\n");
1966 return DDERR_INVALIDPIXELFORMAT;
1969 /* Create the Surface object */
1970 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
1973 ERR("(%p) Error allocating memory for a surface\n", This);
1974 return DDERR_OUTOFVIDEOMEMORY;
1976 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawSurface7, IDirectDrawSurface7_Vtbl);
1977 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawSurface3, IDirectDrawSurface3_Vtbl);
1978 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawGammaControl, IDirectDrawGammaControl_Vtbl);
1979 ICOM_INIT_INTERFACE(*ppSurf, IDirect3DTexture2, IDirect3DTexture2_Vtbl);
1980 ICOM_INIT_INTERFACE(*ppSurf, IDirect3DTexture, IDirect3DTexture1_Vtbl);
1982 (*ppSurf)->version = 7;
1983 (*ppSurf)->ddraw = This;
1984 (*ppSurf)->surface_desc.dwSize = sizeof(DDSURFACEDESC2);
1985 (*ppSurf)->surface_desc.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1986 DD_STRUCT_COPY_BYSIZE(&(*ppSurf)->surface_desc, pDDSD);
1988 /* Surface attachments */
1989 (*ppSurf)->next_attached = NULL;
1990 (*ppSurf)->first_attached = *ppSurf;
1992 /* Needed to re-create the surface on an implementation change */
1993 (*ppSurf)->ImplType = ImplType;
1995 /* For D3DDevice creation */
1996 (*ppSurf)->isRenderTarget = FALSE;
1998 /* A trace message for debugging */
1999 TRACE("(%p) Created IDirectDrawSurface implementation structure at %p\n", This, *ppSurf);
2001 if(pDDSD->ddsCaps.dwCaps & ( DDSCAPS_PRIMARYSURFACE | DDSCAPS_TEXTURE | DDSCAPS_3DDEVICE) )
2003 /* Render targets and textures need a IParent interface,
2004 * because WineD3D will destroy them when the swapchain
2007 parImpl = HeapAlloc(GetProcessHeap(), 0, sizeof(IParentImpl));
2010 ERR("Out of memory when allocating memory for a IParent implementation\n");
2011 return DDERR_OUTOFMEMORY;
2014 ICOM_INIT_INTERFACE(parImpl, IParent, IParent_Vtbl);
2015 Parent = (IUnknown *) ICOM_INTERFACE(parImpl, IParent);
2016 TRACE("Using IParent interface %p as parent\n", parImpl);
2020 /* Use the surface as parent */
2021 Parent = (IUnknown *) ICOM_INTERFACE(*ppSurf, IDirectDrawSurface7);
2022 TRACE("Using Surface interface %p as parent\n", *ppSurf);
2025 /* Now create the WineD3D Surface */
2026 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice,
2030 TRUE /* Lockable */,
2031 FALSE /* Discard */,
2033 &(*ppSurf)->WineD3DSurface,
2036 WINED3DMULTISAMPLE_NONE,
2037 0 /* MultiSampleQuality */,
2038 0 /* SharedHandle */,
2044 ERR("IWineD3DDevice::CreateSurface failed. hr = %08x\n", hr);
2048 /* Set the child of the parent implementation if it exists */
2051 parImpl->child = (IUnknown *) (*ppSurf)->WineD3DSurface;
2052 /* The IParent releases the WineD3DSurface, and
2053 * the ddraw surface does that too. Hold a reference
2055 IWineD3DSurface_AddRef((*ppSurf)->WineD3DSurface);
2058 /* Increase the surface counter, and attach the surface */
2059 InterlockedIncrement(&This->surfaces);
2060 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
2062 /* Here we could store all created surfaces in the DirectDrawImpl structure,
2063 * But this could also be delegated to WineDDraw, as it keeps track of all its
2064 * resources. Not implemented for now, as there are more important things ;)
2067 /* Get the pixel format of the WineD3DSurface and store it.
2068 * Don't use the Format choosen above, WineD3D might have
2071 Desc.Format = &Format;
2072 Desc.Type = &ResType;
2073 Desc.Usage = &Usage;
2074 Desc.Pool = &dummy_d3dpool;
2075 Desc.Size = &dummy_uint;
2076 Desc.MultiSampleType = &dummy_mst;
2077 Desc.MultiSampleQuality = &dummy_dword;
2078 Desc.Width = &Width;
2079 Desc.Height = &Height;
2081 (*ppSurf)->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
2082 hr = IWineD3DSurface_GetDesc((*ppSurf)->WineD3DSurface, &Desc);
2085 ERR("IWineD3DSurface::GetDesc failed\n");
2086 IDirectDrawSurface7_Release( (IDirectDrawSurface7 *) *ppSurf);
2090 if(Format == WINED3DFMT_UNKNOWN)
2092 FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN\n");
2094 PixelFormat_WineD3DtoDD( &(*ppSurf)->surface_desc.u4.ddpfPixelFormat, Format);
2096 /* Anno 1602 stores the pitch right after surface creation, so make sure it's there.
2097 * I can't LockRect() the surface here because if OpenGL surfaces are in use, the
2098 * WineD3DDevice might not be usable for 3D yet, so an extra method was created.
2099 * TODO: Test other fourcc formats
2101 if(Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3 ||
2102 Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5)
2104 (*ppSurf)->surface_desc.dwFlags |= DDSD_LINEARSIZE;
2105 if(Format == WINED3DFMT_DXT1)
2107 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height) / 2;
2111 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height);
2116 (*ppSurf)->surface_desc.dwFlags |= DDSD_PITCH;
2117 (*ppSurf)->surface_desc.u1.lPitch = IWineD3DSurface_GetPitch((*ppSurf)->WineD3DSurface);
2120 /* Application passed a color key? Set it! */
2121 if(pDDSD->dwFlags & DDSD_CKDESTOVERLAY)
2123 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2125 (WINEDDCOLORKEY *) &pDDSD->u3.ddckCKDestOverlay);
2127 if(pDDSD->dwFlags & DDSD_CKDESTBLT)
2129 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2131 (WINEDDCOLORKEY *) &pDDSD->ddckCKDestBlt);
2133 if(pDDSD->dwFlags & DDSD_CKSRCOVERLAY)
2135 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2137 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcOverlay);
2139 if(pDDSD->dwFlags & DDSD_CKSRCBLT)
2141 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2143 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcBlt);
2145 if ( pDDSD->dwFlags & DDSD_LPSURFACE)
2147 hr = IWineD3DSurface_SetMem((*ppSurf)->WineD3DSurface, pDDSD->lpSurface);
2148 if(hr != WINED3D_OK)
2150 /* No need for a trace here, wined3d does that for us */
2151 IDirectDrawSurface7_Release(ICOM_INTERFACE((*ppSurf), IDirectDrawSurface7));
2158 /*****************************************************************************
2159 * CreateAdditionalSurfaces
2161 * Creates a new mipmap chain.
2164 * root: Root surface to attach the newly created chain to
2165 * count: number of surfaces to create
2166 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2167 * effects on the caller
2168 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2169 * creates an additional surface without the mipmapping flags
2171 *****************************************************************************/
2173 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2174 IDirectDrawSurfaceImpl *root,
2176 DDSURFACEDESC2 DDSD,
2179 UINT i, j, level = 0;
2181 IDirectDrawSurfaceImpl *last = root;
2183 for(i = 0; i < count; i++)
2185 IDirectDrawSurfaceImpl *object2 = NULL;
2187 /* increase the mipmap level, but only if a mipmap is created
2188 * In this case, also halve the size
2190 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2193 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2194 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2195 /* Set the mipmap sublevel flag according to msdn */
2196 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2200 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2202 CubeFaceRoot = FALSE;
2204 hr = IDirectDrawImpl_CreateNewSurface(This,
2213 /* Add the new surface to the complex attachment array */
2214 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2216 if(last->complex_array[j]) continue;
2217 last->complex_array[j] = object2;
2222 /* Remove the (possible) back buffer cap from the new surface description,
2223 * because only one surface in the flipping chain is a back buffer, one
2224 * is a front buffer, the others are just primary surfaces.
2226 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2231 /*****************************************************************************
2232 * IDirectDraw7::CreateSurface
2234 * Creates a new IDirectDrawSurface object and returns its interface.
2236 * The surface connections with wined3d are a bit tricky. Basically it works
2239 * |------------------------| |-----------------|
2240 * | DDraw surface | | WineD3DSurface |
2242 * | WineD3DSurface |-------------->| |
2243 * | Child |<------------->| Parent |
2244 * |------------------------| |-----------------|
2246 * The DDraw surface is the parent of the wined3d surface, and it releases
2247 * the WineD3DSurface when the ddraw surface is destroyed.
2249 * However, for all surfaces which can be in a container in WineD3D,
2250 * we have to do this. These surfaces are usually complex surfaces,
2251 * so this concerns primary surfaces with a front and a back buffer,
2254 * |------------------------| |-----------------|
2255 * | DDraw surface | | Container |
2257 * | Child |<------------->| Parent |
2258 * | Texture |<------------->| |
2259 * | WineD3DSurface |<----| | Levels |<--|
2260 * | Complex connection | | | | |
2261 * |------------------------| | |-----------------| |
2265 * | |------------------| | |-----------------| |
2266 * | | IParent | |-------->| WineD3DSurface | |
2268 * | | Child |<------------->| Parent | |
2269 * | | | | Container |<--|
2270 * | |------------------| |-----------------| |
2272 * | |----------------------| |
2273 * | | DDraw surface 2 | |
2275 * |<->| Complex root Child | |
2277 * | | WineD3DSurface |<----| |
2278 * | |----------------------| | |
2280 * | |---------------------| | |-----------------| |
2281 * | | IParent | |----->| WineD3DSurface | |
2283 * | | Child |<---------->| Parent | |
2284 * | |---------------------| | Container |<--|
2285 * | |-----------------| |
2287 * | ---More surfaces can follow--- |
2289 * The reason is that the IWineD3DSwapchain(render target container)
2290 * and the IWineD3DTexure(Texture container) release the parents
2291 * of their surface's children, but by releasing the complex root
2292 * the surfaces which are complexly attached to it are destroyed
2293 * too. See IDirectDrawSurface::Release for a more detailed
2297 * DDSD: Description of the surface to create
2298 * Surf: Address to store the interface pointer at
2299 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2300 * aggregation, so it has to be NULL
2304 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2305 * DDERR_* if an error occurs
2307 *****************************************************************************/
2308 static HRESULT WINAPI
2309 IDirectDrawImpl_CreateSurface(IDirectDraw7 *iface,
2310 DDSURFACEDESC2 *DDSD,
2311 IDirectDrawSurface7 **Surf,
2314 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2315 IDirectDrawSurfaceImpl *object = NULL;
2317 LONG extra_surfaces = 0;
2318 DDSURFACEDESC2 desc2;
2319 WINED3DDISPLAYMODE Mode;
2321 TRACE("(%p)->(%p,%p,%p)\n", This, DDSD, Surf, UnkOuter);
2323 /* Some checks before we start */
2324 if (TRACE_ON(ddraw))
2326 TRACE(" (%p) Requesting surface desc :\n", This);
2327 DDRAW_dump_surface_desc(DDSD);
2329 EnterCriticalSection(&ddraw_cs);
2331 if (UnkOuter != NULL)
2333 FIXME("(%p) : outer != NULL?\n", This);
2334 LeaveCriticalSection(&ddraw_cs);
2335 return CLASS_E_NOAGGREGATION; /* unchecked */
2340 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", This);
2341 LeaveCriticalSection(&ddraw_cs);
2342 return E_POINTER; /* unchecked */
2345 if (!(DDSD->dwFlags & DDSD_CAPS))
2347 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2348 DDSD->dwFlags |= DDSD_CAPS;
2350 if (DDSD->ddsCaps.dwCaps == 0)
2352 /* This has been checked on real Windows */
2353 DDSD->ddsCaps.dwCaps = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
2356 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2358 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2359 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2362 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2364 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2365 WARN("(%p) Null surface pointer specified, ignore it!\n", This);
2366 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2369 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2370 !(This->cooperative_level & DDSCL_EXCLUSIVE))
2372 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n", This);
2374 LeaveCriticalSection(&ddraw_cs);
2375 return DDERR_NOEXCLUSIVEMODE;
2378 if(DDSD->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER)) {
2379 WARN("Application tried to create an explicit front or back buffer\n");
2380 LeaveCriticalSection(&ddraw_cs);
2381 return DDERR_INVALIDCAPS;
2383 /* Check cube maps but only if the size includes them */
2384 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2386 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2387 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2389 WARN("Cube map faces requested without cube map flag\n");
2390 LeaveCriticalSection(&ddraw_cs);
2391 return DDERR_INVALIDCAPS;
2393 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2394 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2396 WARN("Cube map without faces requested\n");
2397 LeaveCriticalSection(&ddraw_cs);
2398 return DDERR_INVALIDPARAMS;
2401 /* Quick tests confirm those can be created, but we don't do that yet */
2402 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2403 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2405 FIXME("Partial cube maps not supported yet\n");
2409 /* According to the msdn this flag is ignored by CreateSurface */
2410 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2411 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2413 /* Modify some flags */
2414 memset(&desc2, 0, sizeof(desc2));
2415 desc2.dwSize = sizeof(desc2); /* For the struct copy */
2416 DD_STRUCT_COPY_BYSIZE(&desc2, DDSD);
2417 desc2.dwSize = sizeof(desc2); /* To override a possibly smaller size */
2418 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2420 /* Get the video mode from WineD3D - we will need it */
2421 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
2422 0, /* Swapchain 0 */
2426 ERR("Failed to read display mode from wined3d\n");
2427 switch(This->orig_bpp)
2430 Mode.Format = WINED3DFMT_P8;
2434 Mode.Format = WINED3DFMT_X1R5G5B5;
2438 Mode.Format = WINED3DFMT_R5G6B5;
2442 Mode.Format = WINED3DFMT_R8G8B8;
2446 Mode.Format = WINED3DFMT_X8R8G8B8;
2449 Mode.Width = This->orig_width;
2450 Mode.Height = This->orig_height;
2453 /* No pixelformat given? Use the current screen format */
2454 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2456 desc2.dwFlags |= DDSD_PIXELFORMAT;
2457 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2459 /* Wait: It could be a Z buffer */
2460 if(desc2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
2462 switch(desc2.u2.dwMipMapCount) /* Who had this glorious idea? */
2465 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D15S1);
2468 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D16);
2471 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D24X8);
2474 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D32);
2477 ERR("Unknown Z buffer bit depth\n");
2482 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
2486 /* No Width or no Height? Use the original screen size
2488 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2489 !(desc2.dwFlags & DDSD_HEIGHT) )
2491 /* Invalid for non-render targets */
2492 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2494 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2496 LeaveCriticalSection(&ddraw_cs);
2497 return DDERR_INVALIDPARAMS;
2500 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2501 desc2.dwWidth = Mode.Width;
2502 desc2.dwHeight = Mode.Height;
2505 /* Mipmap count fixes */
2506 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2508 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2510 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2512 /* Mipmap count is given, should not be 0 */
2513 if( desc2.u2.dwMipMapCount == 0 )
2515 LeaveCriticalSection(&ddraw_cs);
2516 return DDERR_INVALIDPARAMS;
2521 /* Undocumented feature: Create sublevels until
2522 * either the width or the height is 1
2524 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2525 desc2.dwWidth : desc2.dwHeight;
2526 desc2.u2.dwMipMapCount = 0;
2529 desc2.u2.dwMipMapCount += 1;
2536 /* Not-complex mipmap -> Mipmapcount = 1 */
2537 desc2.u2.dwMipMapCount = 1;
2539 extra_surfaces = desc2.u2.dwMipMapCount - 1;
2541 /* There's a mipmap count in the created surface in any case */
2542 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2544 /* If no mipmap is given, the texture has only one level */
2546 /* The first surface is a front buffer, the back buffer is created afterwards */
2547 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2549 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2552 /* The root surface in a cube map is positive x */
2553 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2555 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2556 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2559 /* Create the first surface */
2560 hr = IDirectDrawImpl_CreateNewSurface(This, &desc2, &object, 0);
2563 ERR("IDirectDrawImpl_CreateNewSurface failed with %08x\n", hr);
2564 LeaveCriticalSection(&ddraw_cs);
2567 object->is_complex_root = TRUE;
2569 *Surf = ICOM_INTERFACE(object, IDirectDrawSurface7);
2571 /* Create Additional surfaces if necessary
2572 * This applies to Primary surfaces which have a back buffer count
2573 * set, but not to mipmap textures. In case of Mipmap textures,
2574 * wineD3D takes care of the creation of additional surfaces
2576 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2578 extra_surfaces = DDSD->dwBackBufferCount;
2579 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2580 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2584 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2586 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2587 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
2588 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2589 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
2590 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
2591 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2592 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
2593 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
2594 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2595 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
2596 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
2597 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2598 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
2599 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
2600 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2601 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
2602 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2605 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces, desc2, FALSE);
2608 /* This destroys and possibly created surfaces too */
2609 IDirectDrawSurface_Release( ICOM_INTERFACE(object, IDirectDrawSurface7) );
2610 LeaveCriticalSection(&ddraw_cs);
2614 /* If the implementation is OpenGL and there's no d3ddevice, attach a d3ddevice
2615 * But attach the d3ddevice only if the currently created surface was
2616 * a primary surface (2D app in 3D mode) or a 3DDEVICE surface (3D app)
2617 * The only case I can think of where this doesn't apply is when a
2618 * 2D app was configured by the user to run with OpenGL and it didn't create
2619 * the render target as first surface. In this case the render target creation
2620 * will cause the 3D init.
2622 if( (This->ImplType == SURFACE_OPENGL) && !(This->d3d_initialized) &&
2623 desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE) )
2625 IDirectDrawSurfaceImpl *target = object, *surface;
2628 /* Search for the primary to use as render target */
2629 LIST_FOR_EACH(entry, &This->surface_list)
2631 surface = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2632 if((surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER)) ==
2633 (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER))
2637 TRACE("Using primary %p as render target\n", target);
2642 TRACE("(%p) Attaching a D3DDevice, rendertarget = %p\n", This, target);
2643 hr = IDirectDrawImpl_AttachD3DDevice(This, target);
2646 IDirectDrawSurfaceImpl *release_surf;
2647 ERR("IDirectDrawImpl_AttachD3DDevice failed, hr = %x\n", hr);
2650 /* The before created surface structures are in an incomplete state here.
2651 * WineD3D holds the reference on the IParents, and it released them on the failure
2652 * already. So the regular release method implementation would fail on the attempt
2653 * to destroy either the IParents or the swapchain. So free the surface here.
2654 * The surface structure here is a list, not a tree, because onscreen targets
2655 * cannot be cube textures
2659 release_surf = object;
2660 object = object->complex_array[0];
2661 IDirectDrawSurfaceImpl_Destroy(release_surf);
2663 LeaveCriticalSection(&ddraw_cs);
2668 /* Addref the ddraw interface to keep an reference for each surface */
2669 IDirectDraw7_AddRef(iface);
2670 object->ifaceToRelease = (IUnknown *) iface;
2672 /* Create a WineD3DTexture if a texture was requested */
2673 if(desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
2676 WINED3DFORMAT Format;
2677 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
2679 This->tex_root = object;
2681 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2683 /* a mipmap is created, create enough levels */
2684 levels = desc2.u2.dwMipMapCount;
2688 /* No mipmap is created, create one level */
2692 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM */
2693 if(DDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
2695 Pool = WINED3DPOOL_SYSTEMMEM;
2697 /* Should I forward the MANAGED cap to the managed pool ? */
2699 /* Get the format. It's set already by CreateNewSurface */
2700 Format = PixelFormat_DD2WineD3D(&object->surface_desc.u4.ddpfPixelFormat);
2702 /* The surfaces are already created, the callback only
2703 * passes the IWineD3DSurface to WineD3D
2705 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2707 hr = IWineD3DDevice_CreateCubeTexture(This->wineD3DDevice,
2708 DDSD->dwWidth, /* Edgelength */
2713 (IWineD3DCubeTexture **) &object->wineD3DTexture,
2714 0, /* SharedHandle */
2715 (IUnknown *) ICOM_INTERFACE(object, IDirectDrawSurface7),
2716 D3D7CB_CreateSurface);
2720 hr = IWineD3DDevice_CreateTexture(This->wineD3DDevice,
2721 DDSD->dwWidth, DDSD->dwHeight,
2722 levels, /* MipMapCount = Levels */
2726 (IWineD3DTexture **) &object->wineD3DTexture,
2727 0, /* SharedHandle */
2728 (IUnknown *) ICOM_INTERFACE(object, IDirectDrawSurface7),
2729 D3D7CB_CreateSurface );
2731 This->tex_root = NULL;
2734 LeaveCriticalSection(&ddraw_cs);
2738 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
2739 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
2742 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
2743 const DDPIXELFORMAT *provided)
2745 /* Some flags must be present in both or neither for a match. */
2746 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
2747 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
2748 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
2750 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2753 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
2756 if (requested->dwFlags & DDPF_FOURCC)
2757 if (requested->dwFourCC != provided->dwFourCC)
2760 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
2761 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2762 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
2765 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2766 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2767 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
2770 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
2771 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
2774 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
2775 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2777 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
2780 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
2781 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
2788 IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested,
2789 const DDSURFACEDESC2* provided)
2798 #define CMP(FLAG, FIELD) \
2799 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
2800 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
2802 static const struct compare_info compare[] =
2804 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
2805 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
2807 CMP(CKDESTBLT, ddckCKDestBlt),
2808 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
2809 CMP(CKSRCBLT, ddckCKSrcBlt),
2810 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
2811 CMP(HEIGHT, dwHeight),
2812 CMP(LINEARSIZE, u1 /* dwLinearSize */),
2813 CMP(LPSURFACE, lpSurface),
2814 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
2815 CMP(PITCH, u1 /* lPitch */),
2816 /* PIXELFORMAT: manual */
2817 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
2818 CMP(TEXTURESTAGE, dwTextureStage),
2819 CMP(WIDTH, dwWidth),
2820 /* ZBUFFERBITDEPTH: "obsolete" */
2827 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2830 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
2832 if (requested->dwFlags & compare[i].flag
2833 && memcmp((const char *)provided + compare[i].offset,
2834 (const char *)requested + compare[i].offset,
2835 compare[i].size) != 0)
2839 if (requested->dwFlags & DDSD_PIXELFORMAT)
2841 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
2842 &provided->u4.ddpfPixelFormat))
2849 #undef DDENUMSURFACES_SEARCHTYPE
2850 #undef DDENUMSURFACES_MATCHTYPE
2852 /*****************************************************************************
2853 * IDirectDraw7::EnumSurfaces
2855 * Loops through all surfaces attached to this device and calls the
2856 * application callback. This can't be relayed to WineD3DDevice,
2857 * because some WineD3DSurfaces' parents are IParent objects
2860 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
2861 * DDSD: Description to filter for
2862 * Context: Application-provided pointer, it's passed unmodified to the
2864 * Callback: Address to call for each surface
2867 * DDERR_INVALIDPARAMS if the callback is NULL
2870 *****************************************************************************/
2871 static HRESULT WINAPI
2872 IDirectDrawImpl_EnumSurfaces(IDirectDraw7 *iface,
2874 DDSURFACEDESC2 *DDSD,
2876 LPDDENUMSURFACESCALLBACK7 Callback)
2878 /* The surface enumeration is handled by WineDDraw,
2879 * because it keeps track of all surfaces attached to
2880 * it. The filtering is done by our callback function,
2881 * because WineDDraw doesn't handle ddraw-like surface
2884 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2885 IDirectDrawSurfaceImpl *surf;
2887 DDSURFACEDESC2 desc;
2888 struct list *entry, *entry2;
2890 all = Flags & DDENUMSURFACES_ALL;
2891 nomatch = Flags & DDENUMSURFACES_NOMATCH;
2893 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, DDSD, Context, Callback);
2894 EnterCriticalSection(&ddraw_cs);
2898 LeaveCriticalSection(&ddraw_cs);
2899 return DDERR_INVALIDPARAMS;
2902 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
2903 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
2905 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2906 if (all || (nomatch != IDirectDrawImpl_DDSD_Match(DDSD, &surf->surface_desc)))
2908 desc = surf->surface_desc;
2909 IDirectDrawSurface7_AddRef(ICOM_INTERFACE(surf, IDirectDrawSurface7));
2910 if(Callback( ICOM_INTERFACE(surf, IDirectDrawSurface7), &desc, Context) != DDENUMRET_OK)
2912 LeaveCriticalSection(&ddraw_cs);
2917 LeaveCriticalSection(&ddraw_cs);
2921 static HRESULT WINAPI
2922 findRenderTarget(IDirectDrawSurface7 *surface,
2923 DDSURFACEDESC2 *desc,
2926 IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, surface);
2927 IDirectDrawSurfaceImpl **target = (IDirectDrawSurfaceImpl **) ctx;
2929 if(!surf->isRenderTarget) {
2931 IDirectDrawSurface7_Release(surface);
2932 return DDENUMRET_CANCEL;
2935 /* Recurse into the surface tree */
2936 IDirectDrawSurface7_EnumAttachedSurfaces(surface, ctx, findRenderTarget);
2938 IDirectDrawSurface7_Release(surface);
2939 if(*target) return DDENUMRET_CANCEL;
2940 else return DDENUMRET_OK; /* Continue with the next neighbor surface */
2943 /*****************************************************************************
2944 * D3D7CB_CreateRenderTarget
2946 * Callback called by WineD3D to create Surfaces for render target usage
2947 * This function takes the D3D target from the IDirectDrawImpl structure,
2948 * and returns the WineD3DSurface. To avoid double usage, the surface
2949 * is marked as render target afterwards
2952 * device: The WineD3DDevice's parent
2953 * Width, Height, Format: Dimensions and pixelformat of the render target
2954 * Ignored, because the surface already exists
2955 * MultiSample, MultisampleQuality, Lockable: Ignored for the same reason
2957 * ppSurface: Address to pass the surface pointer back at
2958 * pSharedHandle: Ignored
2961 * Always returns D3D_OK
2963 *****************************************************************************/
2964 static HRESULT WINAPI
2965 D3D7CB_CreateRenderTarget(IUnknown *device, IUnknown *pSuperior,
2966 UINT Width, UINT Height,
2967 WINED3DFORMAT Format,
2968 WINED3DMULTISAMPLE_TYPE MultiSample,
2969 DWORD MultisampleQuality,
2971 IWineD3DSurface** ppSurface,
2972 HANDLE* pSharedHandle)
2974 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
2975 IDirectDrawSurfaceImpl *d3dSurface = This->d3d_target, *target = NULL;
2976 TRACE("(%p) call back\n", device);
2978 if(d3dSurface->isRenderTarget)
2980 IDirectDrawSurface7_EnumAttachedSurfaces(ICOM_INTERFACE(d3dSurface, IDirectDrawSurface7),
2981 &target, findRenderTarget);
2985 target = d3dSurface;
2990 target = This->d3d_target;
2991 ERR(" (%p) : No DirectDrawSurface found to create the back buffer. Using the front buffer as back buffer. Uncertain consequences\n", This);
2994 /* TODO: Return failure if the dimensions do not match, but this shouldn't happen */
2996 *ppSurface = target->WineD3DSurface;
2997 target->isRenderTarget = TRUE;
2998 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *ppSurface, d3dSurface);
3002 static HRESULT WINAPI
3003 D3D7CB_CreateDepthStencilSurface(IUnknown *device,
3004 IUnknown *pSuperior,
3007 WINED3DFORMAT Format,
3008 WINED3DMULTISAMPLE_TYPE MultiSample,
3009 DWORD MultisampleQuality,
3011 IWineD3DSurface** ppSurface,
3012 HANDLE* pSharedHandle)
3014 /* Create a Depth Stencil surface to make WineD3D happy */
3015 HRESULT hr = D3D_OK;
3016 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
3017 DDSURFACEDESC2 ddsd;
3019 TRACE("(%p) call back\n", device);
3023 /* Create a DirectDraw surface */
3024 memset(&ddsd, 0, sizeof(ddsd));
3025 ddsd.dwSize = sizeof(ddsd);
3026 ddsd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
3027 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
3028 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
3029 ddsd.dwHeight = Height;
3030 ddsd.dwWidth = Width;
3033 PixelFormat_WineD3DtoDD(&ddsd.u4.ddpfPixelFormat, Format);
3037 ddsd.dwFlags ^= DDSD_PIXELFORMAT;
3040 This->depthstencil = TRUE;
3041 hr = IDirectDraw7_CreateSurface((IDirectDraw7 *) This,
3043 (IDirectDrawSurface7 **) &This->DepthStencilBuffer,
3045 This->depthstencil = FALSE;
3048 ERR(" (%p) Creating a DepthStencil Surface failed, result = %x\n", This, hr);
3051 *ppSurface = This->DepthStencilBuffer->WineD3DSurface;
3055 /*****************************************************************************
3056 * D3D7CB_CreateAdditionalSwapChain
3058 * Callback function for WineD3D which creates a new WineD3DSwapchain
3059 * interface. It also creates an IParent interface to store that pointer,
3060 * so the WineD3DSwapchain has a parent and can be released when the D3D
3061 * device is destroyed
3062 *****************************************************************************/
3063 static HRESULT WINAPI
3064 D3D7CB_CreateAdditionalSwapChain(IUnknown *device,
3065 WINED3DPRESENT_PARAMETERS* pPresentationParameters,
3066 IWineD3DSwapChain ** ppSwapChain)
3068 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
3069 IParentImpl *object = NULL;
3070 HRESULT res = D3D_OK;
3071 IWineD3DSwapChain *swapchain;
3072 TRACE("(%p) call back\n", device);
3074 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IParentImpl));
3077 FIXME("Allocation of memory failed\n");
3078 *ppSwapChain = NULL;
3079 return DDERR_OUTOFVIDEOMEMORY;
3082 ICOM_INIT_INTERFACE(object, IParent, IParent_Vtbl);
3085 res = IWineD3DDevice_CreateAdditionalSwapChain(This->wineD3DDevice,
3086 pPresentationParameters,
3088 (IUnknown*) ICOM_INTERFACE(object, IParent),
3089 D3D7CB_CreateRenderTarget,
3090 D3D7CB_CreateDepthStencilSurface);
3093 FIXME("(%p) call to IWineD3DDevice_CreateAdditionalSwapChain failed\n", This);
3094 HeapFree(GetProcessHeap(), 0 , object);
3095 *ppSwapChain = NULL;
3099 *ppSwapChain = swapchain;
3100 object->child = (IUnknown *) swapchain;
3106 /*****************************************************************************
3107 * IDirectDrawImpl_AttachD3DDevice
3109 * Initializes the D3D capabilities of WineD3D
3112 * primary: The primary surface for D3D
3118 *****************************************************************************/
3119 static HRESULT WINAPI
3120 IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This,
3121 IDirectDrawSurfaceImpl *primary)
3126 WINED3DPRESENT_PARAMETERS localParameters;
3128 TRACE("(%p)->(%p)\n", This, primary);
3130 /* Get the window */
3131 hr = IWineD3DDevice_GetHWND(This->wineD3DDevice,
3135 ERR("IWineD3DDevice::GetHWND failed\n");
3139 /* If there's no window, create a hidden window. WineD3D needs it */
3142 window = CreateWindowExA(0, This->classname, "Hidden D3D Window",
3144 GetSystemMetrics(SM_CXSCREEN),
3145 GetSystemMetrics(SM_CYSCREEN),
3146 NULL, NULL, GetModuleHandleA(0), NULL);
3148 ShowWindow(window, SW_HIDE); /* Just to be sure */
3149 WARN("(%p) No window for the Direct3DDevice, created a hidden window. HWND=%p\n", This, window);
3150 This->d3d_window = window;
3154 TRACE("(%p) Using existing window %p for Direct3D rendering\n", This, window);
3157 /* Store the future Render Target surface */
3158 This->d3d_target = primary;
3160 /* Use the surface description for the device parameters, not the
3161 * Device settings. The app might render to an offscreen surface
3163 localParameters.BackBufferWidth = primary->surface_desc.dwWidth;
3164 localParameters.BackBufferHeight = primary->surface_desc.dwHeight;
3165 localParameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
3166 localParameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
3167 localParameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
3168 localParameters.MultiSampleQuality = 0;
3169 localParameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
3170 localParameters.hDeviceWindow = window;
3171 localParameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
3172 localParameters.EnableAutoDepthStencil = TRUE;
3173 localParameters.AutoDepthStencilFormat = WINED3DFMT_D16;
3174 localParameters.Flags = 0;
3175 localParameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
3176 localParameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
3178 TRACE("Passing mode %d\n", localParameters.BackBufferFormat);
3180 /* Set this NOW, otherwise creating the depth stencil surface will cause a
3181 * recursive loop until ram or emulated video memory is full
3183 This->d3d_initialized = TRUE;
3185 hr = IWineD3DDevice_Init3D(This->wineD3DDevice,
3187 D3D7CB_CreateAdditionalSwapChain);
3190 This->d3d_target = NULL;
3191 This->d3d_initialized = FALSE;
3195 This->declArraySize = 2;
3196 This->decls = HeapAlloc(GetProcessHeap(),
3198 sizeof(*This->decls) * This->declArraySize);
3201 ERR("Error allocating an array for the converted vertex decls\n");
3202 This->declArraySize = 0;
3203 hr = IWineD3DDevice_Uninit3D(This->wineD3DDevice,
3204 D3D7CB_DestroyDepthStencilSurface,
3205 D3D7CB_DestroySwapChain);
3206 return E_OUTOFMEMORY;
3209 /* Create an Index Buffer parent */
3210 TRACE("(%p) Successfully initialized 3D\n", This);
3214 /*****************************************************************************
3215 * DirectDrawCreateClipper (DDRAW.@)
3217 * Creates a new IDirectDrawClipper object.
3220 * Clipper: Address to write the interface pointer to
3221 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3225 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3226 * E_OUTOFMEMORY if allocating the object failed
3228 *****************************************************************************/
3230 DirectDrawCreateClipper(DWORD Flags,
3231 LPDIRECTDRAWCLIPPER *Clipper,
3234 IDirectDrawClipperImpl* object;
3235 TRACE("(%08x,%p,%p)\n", Flags, Clipper, UnkOuter);
3237 EnterCriticalSection(&ddraw_cs);
3238 if (UnkOuter != NULL)
3240 LeaveCriticalSection(&ddraw_cs);
3241 return CLASS_E_NOAGGREGATION;
3244 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3245 sizeof(IDirectDrawClipperImpl));
3248 LeaveCriticalSection(&ddraw_cs);
3249 return E_OUTOFMEMORY;
3252 ICOM_INIT_INTERFACE(object, IDirectDrawClipper, IDirectDrawClipper_Vtbl);
3254 object->wineD3DClipper = pWineDirect3DCreateClipper((IUnknown *) object);
3255 if(!object->wineD3DClipper)
3257 HeapFree(GetProcessHeap(), 0, object);
3258 LeaveCriticalSection(&ddraw_cs);
3259 return E_OUTOFMEMORY;
3262 *Clipper = (IDirectDrawClipper *) object;
3263 LeaveCriticalSection(&ddraw_cs);
3267 /*****************************************************************************
3268 * IDirectDraw7::CreateClipper
3270 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3272 *****************************************************************************/
3273 static HRESULT WINAPI
3274 IDirectDrawImpl_CreateClipper(IDirectDraw7 *iface,
3276 IDirectDrawClipper **Clipper,
3279 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3280 TRACE("(%p)->(%x,%p,%p)\n", This, Flags, Clipper, UnkOuter);
3281 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3284 /*****************************************************************************
3285 * IDirectDraw7::CreatePalette
3287 * Creates a new IDirectDrawPalette object
3290 * Flags: The flags for the new clipper
3291 * ColorTable: Color table to assign to the new clipper
3292 * Palette: Address to write the interface pointer to
3293 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3297 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3298 * E_OUTOFMEMORY if allocating the object failed
3300 *****************************************************************************/
3301 static HRESULT WINAPI
3302 IDirectDrawImpl_CreatePalette(IDirectDraw7 *iface,
3304 PALETTEENTRY *ColorTable,
3305 IDirectDrawPalette **Palette,
3306 IUnknown *pUnkOuter)
3308 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3309 IDirectDrawPaletteImpl *object;
3310 HRESULT hr = DDERR_GENERIC;
3311 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, ColorTable, Palette, pUnkOuter);
3313 EnterCriticalSection(&ddraw_cs);
3314 if(pUnkOuter != NULL)
3316 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
3317 LeaveCriticalSection(&ddraw_cs);
3318 return CLASS_E_NOAGGREGATION;
3321 /* The refcount test shows that a cooplevel is required for this */
3322 if(!This->cooperative_level)
3324 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3325 LeaveCriticalSection(&ddraw_cs);
3326 return DDERR_NOCOOPERATIVELEVELSET;
3329 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
3332 ERR("Out of memory when allocating memory for a palette implementation\n");
3333 LeaveCriticalSection(&ddraw_cs);
3334 return E_OUTOFMEMORY;
3337 ICOM_INIT_INTERFACE(object, IDirectDrawPalette, IDirectDrawPalette_Vtbl);
3339 object->ddraw_owner = This;
3341 hr = IWineD3DDevice_CreatePalette(This->wineD3DDevice, Flags, ColorTable, &object->wineD3DPalette, (IUnknown *) ICOM_INTERFACE(object, IDirectDrawPalette) );
3344 HeapFree(GetProcessHeap(), 0, object);
3345 LeaveCriticalSection(&ddraw_cs);
3349 IDirectDraw7_AddRef(iface);
3350 object->ifaceToRelease = (IUnknown *) iface;
3351 *Palette = ICOM_INTERFACE(object, IDirectDrawPalette);
3352 LeaveCriticalSection(&ddraw_cs);
3356 /*****************************************************************************
3357 * IDirectDraw7::DuplicateSurface
3359 * Duplicates a surface. The surface memory points to the same memory as
3360 * the original surface, and it's released when the last surface referencing
3361 * it is released. I guess that's beyond Wine's surface management right now
3362 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3363 * test application to implement this)
3366 * Src: Address of the source surface
3367 * Dest: Address to write the new surface pointer to
3370 * See IDirectDraw7::CreateSurface
3372 *****************************************************************************/
3373 static HRESULT WINAPI
3374 IDirectDrawImpl_DuplicateSurface(IDirectDraw7 *iface,
3375 IDirectDrawSurface7 *Src,
3376 IDirectDrawSurface7 **Dest)
3378 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3379 IDirectDrawSurfaceImpl *Surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Src);
3381 FIXME("(%p)->(%p,%p)\n", This, Surf, Dest);
3383 /* For now, simply create a new, independent surface */
3384 return IDirectDraw7_CreateSurface(iface,
3385 &Surf->surface_desc,
3390 /*****************************************************************************
3391 * IDirectDraw7 VTable
3392 *****************************************************************************/
3393 const IDirectDraw7Vtbl IDirectDraw7_Vtbl =
3396 IDirectDrawImpl_QueryInterface,
3397 IDirectDrawImpl_AddRef,
3398 IDirectDrawImpl_Release,
3399 /*** IDirectDraw ***/
3400 IDirectDrawImpl_Compact,
3401 IDirectDrawImpl_CreateClipper,
3402 IDirectDrawImpl_CreatePalette,
3403 IDirectDrawImpl_CreateSurface,
3404 IDirectDrawImpl_DuplicateSurface,
3405 IDirectDrawImpl_EnumDisplayModes,
3406 IDirectDrawImpl_EnumSurfaces,
3407 IDirectDrawImpl_FlipToGDISurface,
3408 IDirectDrawImpl_GetCaps,
3409 IDirectDrawImpl_GetDisplayMode,
3410 IDirectDrawImpl_GetFourCCCodes,
3411 IDirectDrawImpl_GetGDISurface,
3412 IDirectDrawImpl_GetMonitorFrequency,
3413 IDirectDrawImpl_GetScanLine,
3414 IDirectDrawImpl_GetVerticalBlankStatus,
3415 IDirectDrawImpl_Initialize,
3416 IDirectDrawImpl_RestoreDisplayMode,
3417 IDirectDrawImpl_SetCooperativeLevel,
3418 IDirectDrawImpl_SetDisplayMode,
3419 IDirectDrawImpl_WaitForVerticalBlank,
3420 /*** IDirectDraw2 ***/
3421 IDirectDrawImpl_GetAvailableVidMem,
3422 /*** IDirectDraw7 ***/
3423 IDirectDrawImpl_GetSurfaceFromDC,
3424 IDirectDrawImpl_RestoreAllSurfaces,
3425 IDirectDrawImpl_TestCooperativeLevel,
3426 IDirectDrawImpl_GetDeviceIdentifier,
3427 /*** IDirectDraw7 ***/
3428 IDirectDrawImpl_StartModeTest,
3429 IDirectDrawImpl_EvaluateMode
3432 /*****************************************************************************
3433 * IDirectDrawImpl_FindDecl
3435 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
3436 * if none was found.
3438 * This function is in ddraw.c and the DDraw object space because D3D7
3439 * vertex buffers are created using the IDirect3D interface to the ddraw
3440 * object, so they can be valid across D3D devices(theoretically. The ddraw
3441 * object also owns the wined3d device
3445 * fvf: Fvf to find the decl for
3448 * NULL in case of an error, the IWineD3DVertexDeclaration interface for the
3451 *****************************************************************************/
3452 IWineD3DVertexDeclaration *
3453 IDirectDrawImpl_FindDecl(IDirectDrawImpl *This,
3457 IWineD3DVertexDeclaration* pDecl = NULL;
3458 int p, low, high; /* deliberately signed */
3459 struct FvfToDecl *convertedDecls = This->decls;
3461 TRACE("Searching for declaration for fvf %08x... ", fvf);
3464 high = This->numConvertedDecls - 1;
3465 while(low <= high) {
3466 p = (low + high) >> 1;
3468 if(convertedDecls[p].fvf == fvf) {
3469 TRACE("found %p\n", convertedDecls[p].decl);
3470 return convertedDecls[p].decl;
3471 } else if(convertedDecls[p].fvf < fvf) {
3477 TRACE("not found. Creating and inserting at position %d.\n", low);
3479 hr = IWineD3DDevice_CreateVertexDeclarationFromFVF(This->wineD3DDevice,
3481 (IUnknown *) ICOM_INTERFACE(This, IDirectDraw7),
3483 if (hr != S_OK) return NULL;
3485 if(This->declArraySize == This->numConvertedDecls) {
3486 int grow = max(This->declArraySize / 2, 8);
3487 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
3488 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
3489 if(!convertedDecls) {
3490 /* This will destroy it */
3491 IWineD3DVertexDeclaration_Release(pDecl);
3494 This->decls = convertedDecls;
3495 This->declArraySize += grow;
3498 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
3499 convertedDecls[low].decl = pDecl;
3500 convertedDecls[low].fvf = fvf;
3501 This->numConvertedDecls++;
3503 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);