d3d: Remove IWineD3DDevice::SetHwnd.
[wine/multimedia.git] / dlls / ddraw / ddraw.c
blob6ae02698fca9d293a1710a31ceb9fa9d1c544b2d
1 /*
2 * Copyright 1997-2000 Marcus Meissner
3 * Copyright 1998-2000 Lionel Ulmer
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2006 Stefan Dösinger
6 * Copyright 2008 Denver Gingerich
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <stdlib.h>
31 #define COBJMACROS
32 #define NONAMELESSUNION
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "wingdi.h"
38 #include "wine/exception.h"
40 #include "ddraw.h"
41 #include "d3d.h"
43 #include "ddraw_private.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
48 static BOOL IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested, const DDSURFACEDESC2* provided);
49 static HRESULT WINAPI IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This, IDirectDrawSurfaceImpl *primary);
50 static HRESULT WINAPI IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This, DDSURFACEDESC2 *pDDSD, IDirectDrawSurfaceImpl **ppSurf, UINT level);
51 static HRESULT WINAPI IDirectDrawImpl_CreateGDISwapChain(IDirectDrawImpl *This, IDirectDrawSurfaceImpl *primary);
53 /* Device identifier. Don't relay it to WineD3D */
54 static const DDDEVICEIDENTIFIER2 deviceidentifier =
56 "display",
57 "DirectDraw HAL",
58 { { 0x00010001, 0x00010001 } },
59 0, 0, 0, 0,
60 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
61 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
65 /*****************************************************************************
66 * IUnknown Methods
67 *****************************************************************************/
69 /*****************************************************************************
70 * IDirectDraw7::QueryInterface
72 * Queries different interfaces of the DirectDraw object. It can return
73 * IDirectDraw interfaces in version 1, 2, 4 and 7, and IDirect3D interfaces
74 * in version 1, 2, 3 and 7. An IDirect3DDevice can be created with this
75 * method.
76 * The returned interface is AddRef()-ed before it's returned
78 * Used for version 1, 2, 4 and 7
80 * Params:
81 * refiid: Interface ID asked for
82 * obj: Used to return the interface pointer
84 * Returns:
85 * S_OK if an interface was found
86 * E_NOINTERFACE if the requested interface wasn't found
88 *****************************************************************************/
89 static HRESULT WINAPI
90 IDirectDrawImpl_QueryInterface(IDirectDraw7 *iface,
91 REFIID refiid,
92 void **obj)
94 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
96 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(refiid), obj);
98 /* Can change surface impl type */
99 EnterCriticalSection(&ddraw_cs);
101 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
102 *obj = NULL;
104 if(!refiid)
106 LeaveCriticalSection(&ddraw_cs);
107 return DDERR_INVALIDPARAMS;
110 /* Check DirectDraw Interfaces */
111 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
112 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
114 *obj = ICOM_INTERFACE(This, IDirectDraw7);
115 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
117 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
119 *obj = ICOM_INTERFACE(This, IDirectDraw4);
120 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
122 else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
124 *obj = ICOM_INTERFACE(This, IDirectDraw3);
125 TRACE("(%p) Returning IDirectDraw3 interface at %p\n", This, *obj);
127 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
129 *obj = ICOM_INTERFACE(This, IDirectDraw2);
130 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
132 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
134 *obj = ICOM_INTERFACE(This, IDirectDraw);
135 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
138 /* Direct3D
139 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
140 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
141 * who had this idea and why. The older interfaces can query and IDirect3D version
142 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
143 * and messy to implement with the common creation function, so it has been left out here.
145 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
146 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
147 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
148 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
150 /* Check the surface implementation */
151 if(This->ImplType == SURFACE_UNKNOWN)
153 /* Apps may create the IDirect3D Interface before the primary surface.
154 * set the surface implementation */
155 This->ImplType = SURFACE_OPENGL;
156 TRACE("(%p) Choosing OpenGL surfaces because a Direct3D interface was requested\n", This);
158 else if(This->ImplType != SURFACE_OPENGL && DefaultSurfaceType == SURFACE_UNKNOWN)
160 ERR("(%p) The App is requesting a D3D device, but a non-OpenGL surface type was choosen. Prepare for trouble!\n", This);
161 ERR(" (%p) You may want to contact wine-devel for help\n", This);
162 /* Should I assert(0) here??? */
164 else if(This->ImplType != SURFACE_OPENGL)
166 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
167 /* Do not abort here, only reject 3D Device creation */
170 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
172 This->d3dversion = 1;
173 *obj = ICOM_INTERFACE(This, IDirect3D);
174 TRACE(" returning Direct3D interface at %p.\n", *obj);
176 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
178 This->d3dversion = 2;
179 *obj = ICOM_INTERFACE(This, IDirect3D2);
180 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
182 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
184 This->d3dversion = 3;
185 *obj = ICOM_INTERFACE(This, IDirect3D3);
186 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
188 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
190 This->d3dversion = 7;
191 *obj = ICOM_INTERFACE(This, IDirect3D7);
192 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
196 /* Unknown interface */
197 else
199 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
200 LeaveCriticalSection(&ddraw_cs);
201 return E_NOINTERFACE;
204 IUnknown_AddRef( (IUnknown *) *obj );
205 LeaveCriticalSection(&ddraw_cs);
206 return S_OK;
209 /*****************************************************************************
210 * IDirectDraw7::AddRef
212 * Increases the interfaces refcount, basically
214 * DDraw refcounting is a bit tricky. The different DirectDraw interface
215 * versions have individual refcounts, but the IDirect3D interfaces do not.
216 * All interfaces are from one object, that means calling QueryInterface on an
217 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
218 * IDirectDrawImpl object.
220 * That means all AddRef and Release implementations of IDirectDrawX work
221 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
222 * except of IDirect3D7 which thunks to IDirectDraw7
224 * Returns: The new refcount
226 *****************************************************************************/
227 static ULONG WINAPI
228 IDirectDrawImpl_AddRef(IDirectDraw7 *iface)
230 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
231 ULONG ref = InterlockedIncrement(&This->ref7);
233 TRACE("(%p) : incrementing IDirectDraw7 refcount from %u.\n", This, ref -1);
235 if(ref == 1) InterlockedIncrement(&This->numIfaces);
237 return ref;
240 /*****************************************************************************
241 * IDirectDrawImpl_Destroy
243 * Destroys a ddraw object if all refcounts are 0. This is to share code
244 * between the IDirectDrawX::Release functions
246 * Params:
247 * This: DirectDraw object to destroy
249 *****************************************************************************/
250 void
251 IDirectDrawImpl_Destroy(IDirectDrawImpl *This)
253 /* Clear the cooplevel to restore window and display mode */
254 IDirectDraw7_SetCooperativeLevel(ICOM_INTERFACE(This, IDirectDraw7),
255 NULL,
256 DDSCL_NORMAL);
258 /* Destroy the device window if we created one */
259 if(This->devicewindow != 0)
261 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
262 DestroyWindow(This->devicewindow);
263 This->devicewindow = 0;
266 /* Unregister the window class */
267 UnregisterClassA(This->classname, 0);
269 EnterCriticalSection(&ddraw_cs);
270 list_remove(&This->ddraw_list_entry);
271 LeaveCriticalSection(&ddraw_cs);
273 /* Release the attached WineD3D stuff */
274 IWineD3DDevice_Release(This->wineD3DDevice);
275 IWineD3D_Release(This->wineD3D);
277 /* Now free the object */
278 HeapFree(GetProcessHeap(), 0, This);
281 /*****************************************************************************
282 * IDirectDraw7::Release
284 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
286 * Returns: The new refcount
287 *****************************************************************************/
288 static ULONG WINAPI
289 IDirectDrawImpl_Release(IDirectDraw7 *iface)
291 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
292 ULONG ref = InterlockedDecrement(&This->ref7);
294 TRACE("(%p)->() decrementing IDirectDraw7 refcount from %u.\n", This, ref +1);
296 if(ref == 0)
298 ULONG ifacecount = InterlockedDecrement(&This->numIfaces);
299 if(ifacecount == 0) IDirectDrawImpl_Destroy(This);
302 return ref;
305 /*****************************************************************************
306 * IDirectDraw methods
307 *****************************************************************************/
309 /*****************************************************************************
310 * IDirectDraw7::SetCooperativeLevel
312 * Sets the cooperative level for the DirectDraw object, and the window
313 * assigned to it. The cooperative level determines the general behavior
314 * of the DirectDraw application
316 * Warning: This is quite tricky, as it's not really documented which
317 * cooperative levels can be combined with each other. If a game fails
318 * after this function, try to check the cooperative levels passed on
319 * Windows, and if it returns something different.
321 * If you think that this function caused the failure because it writes a
322 * fixme, be sure to run again with a +ddraw trace.
324 * What is known about cooperative levels (See the ddraw modes test):
325 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
326 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
327 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
328 * DDSCL_FULLSCREEN can be activated
329 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
331 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
332 * DDSCL_SETFOCUSWINDOW (partially),
333 * DDSCL_MULTITHREADED (work in progress)
335 * Unhandled flags, which should be implemented
336 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
337 * expect any difference to a normal window for wine)
338 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
339 * rendering (Possible test case: Half-life)
341 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
343 * These don't seem very important for wine:
344 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
346 * Returns:
347 * DD_OK if the cooperative level was set successfully
348 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
349 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
350 * (Probably others too, have to investigate)
352 *****************************************************************************/
353 static HRESULT WINAPI
354 IDirectDrawImpl_SetCooperativeLevel(IDirectDraw7 *iface,
355 HWND hwnd,
356 DWORD cooplevel)
358 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
359 HWND window;
361 TRACE("(%p)->(%p,%08x)\n",This,hwnd,cooplevel);
362 DDRAW_dump_cooperativelevel(cooplevel);
364 EnterCriticalSection(&ddraw_cs);
366 /* Get the old window */
367 window = This->dest_window;
369 /* Tests suggest that we need one of them: */
370 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
371 DDSCL_NORMAL |
372 DDSCL_EXCLUSIVE )))
374 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
375 LeaveCriticalSection(&ddraw_cs);
376 return DDERR_INVALIDPARAMS;
379 /* Handle those levels first which set various hwnds */
380 if(cooplevel & DDSCL_SETFOCUSWINDOW)
382 /* This isn't compatible with a lot of flags */
383 if(cooplevel & ( DDSCL_MULTITHREADED |
384 DDSCL_FPUSETUP |
385 DDSCL_FPUPRESERVE |
386 DDSCL_ALLOWREBOOT |
387 DDSCL_ALLOWMODEX |
388 DDSCL_SETDEVICEWINDOW |
389 DDSCL_NORMAL |
390 DDSCL_EXCLUSIVE |
391 DDSCL_FULLSCREEN ) )
393 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
394 LeaveCriticalSection(&ddraw_cs);
395 return DDERR_INVALIDPARAMS;
397 else if( (This->cooperative_level & DDSCL_FULLSCREEN) && window)
399 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET\n");
400 LeaveCriticalSection(&ddraw_cs);
401 return DDERR_HWNDALREADYSET;
404 This->focuswindow = hwnd;
405 /* Won't use the hwnd param for anything else */
406 hwnd = NULL;
408 /* Use the focus window for drawing too */
409 This->dest_window = This->focuswindow;
411 /* Destroy the device window, if we have one */
412 if(This->devicewindow)
414 DestroyWindow(This->devicewindow);
415 This->devicewindow = NULL;
418 /* DDSCL_NORMAL or DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE */
419 if(cooplevel & DDSCL_NORMAL)
421 /* Can't coexist with fullscreen or exclusive */
422 if(cooplevel & (DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE) )
424 TRACE("(%p) DDSCL_NORMAL is not compative with DDSCL_FULLSCREEN or DDSCL_EXCLUSIVE\n", This);
425 LeaveCriticalSection(&ddraw_cs);
426 return DDERR_INVALIDPARAMS;
429 /* Switching from fullscreen? */
430 if(This->cooperative_level & DDSCL_FULLSCREEN)
432 /* Restore the display mode */
433 IDirectDraw7_RestoreDisplayMode(iface);
435 This->cooperative_level &= ~DDSCL_FULLSCREEN;
436 This->cooperative_level &= ~DDSCL_EXCLUSIVE;
437 This->cooperative_level &= ~DDSCL_ALLOWMODEX;
440 /* Don't override focus windows or private device windows */
441 if( hwnd &&
442 !(This->focuswindow) &&
443 !(This->devicewindow) &&
444 (hwnd != window) )
446 This->dest_window = hwnd;
449 IWineD3DDevice_SetFullscreen(This->wineD3DDevice,
450 FALSE);
452 else if(cooplevel & DDSCL_FULLSCREEN)
454 /* Needs DDSCL_EXCLUSIVE */
455 if(!(cooplevel & DDSCL_EXCLUSIVE) )
457 TRACE("(%p) DDSCL_FULLSCREEN needs DDSCL_EXCLUSIVE\n", This);
458 LeaveCriticalSection(&ddraw_cs);
459 return DDERR_INVALIDPARAMS;
461 /* Need a HWND
462 if(hwnd == 0)
464 TRACE("(%p) DDSCL_FULLSCREEN needs a HWND\n", This);
465 return DDERR_INVALIDPARAMS;
469 This->cooperative_level &= ~DDSCL_NORMAL;
470 IWineD3DDevice_SetFullscreen(This->wineD3DDevice,
471 TRUE);
473 /* Don't override focus windows or private device windows */
474 if( hwnd &&
475 !(This->focuswindow) &&
476 !(This->devicewindow) &&
477 (hwnd != window) )
479 This->dest_window = hwnd;
482 else if(cooplevel & DDSCL_EXCLUSIVE)
484 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN\n", This);
485 LeaveCriticalSection(&ddraw_cs);
486 return DDERR_INVALIDPARAMS;
489 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
491 /* Don't create a device window if a focus window is set */
492 if( !(This->focuswindow) )
494 HWND devicewindow = CreateWindowExA(0, This->classname, "DDraw device window",
495 WS_POPUP, 0, 0,
496 GetSystemMetrics(SM_CXSCREEN),
497 GetSystemMetrics(SM_CYSCREEN),
498 NULL, NULL, GetModuleHandleA(0), NULL);
500 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
501 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
503 This->devicewindow = devicewindow;
504 This->dest_window = devicewindow;
508 if(cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
510 /* Enable thread safety in wined3d */
511 IWineD3DDevice_SetMultithreaded(This->wineD3DDevice);
514 /* Unhandled flags */
515 if(cooplevel & DDSCL_ALLOWREBOOT)
516 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
517 if(cooplevel & DDSCL_ALLOWMODEX)
518 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
519 if(cooplevel & DDSCL_FPUSETUP)
520 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
522 /* Store the cooperative_level */
523 This->cooperative_level |= cooplevel;
524 TRACE("SetCooperativeLevel retuning DD_OK\n");
525 LeaveCriticalSection(&ddraw_cs);
526 return DD_OK;
529 /*****************************************************************************
531 * Helper function for SetDisplayMode and RestoreDisplayMode
533 * Implements DirectDraw's SetDisplayMode, but ignores the value of
534 * ForceRefreshRate, since it is already handled by
535 * IDirectDrawImpl_SetDisplayMode. RestoreDisplayMode can use this function
536 * without worrying that ForceRefreshRate will override the refresh rate. For
537 * argument and return value documentation, see
538 * IDirectDrawImpl_SetDisplayMode.
540 *****************************************************************************/
541 static HRESULT
542 IDirectDrawImpl_SetDisplayModeNoOverride(IDirectDraw7 *iface,
543 DWORD Width,
544 DWORD Height,
545 DWORD BPP,
546 DWORD RefreshRate,
547 DWORD Flags)
549 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
550 WINED3DDISPLAYMODE Mode;
551 HRESULT hr;
552 TRACE("(%p)->(%d,%d,%d,%d,%x: Relay!\n", This, Width, Height, BPP, RefreshRate, Flags);
554 EnterCriticalSection(&ddraw_cs);
555 if( !Width || !Height )
557 ERR("Width=%d, Height=%d, what to do?\n", Width, Height);
558 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
559 LeaveCriticalSection(&ddraw_cs);
560 return DD_OK;
563 /* Check the exclusive mode
564 if(!(This->cooperative_level & DDSCL_EXCLUSIVE))
565 return DDERR_NOEXCLUSIVEMODE;
566 * This is WRONG. Don't know if the SDK is completely
567 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
568 * is returned, but Half-Life 1.1.1.1 (Steam version)
569 * depends on this
572 Mode.Width = Width;
573 Mode.Height = Height;
574 Mode.RefreshRate = RefreshRate;
575 switch(BPP)
577 case 8: Mode.Format = WINED3DFMT_P8; break;
578 case 15: Mode.Format = WINED3DFMT_X1R5G5B5; break;
579 case 16: Mode.Format = WINED3DFMT_R5G6B5; break;
580 case 24: Mode.Format = WINED3DFMT_R8G8B8; break;
581 case 32: Mode.Format = WINED3DFMT_X8R8G8B8; break;
584 /* TODO: The possible return values from msdn suggest that
585 * the screen mode can't be changed if a surface is locked
586 * or some drawing is in progress
589 /* TODO: Lose the primary surface */
590 hr = IWineD3DDevice_SetDisplayMode(This->wineD3DDevice,
591 0, /* First swapchain */
592 &Mode);
593 LeaveCriticalSection(&ddraw_cs);
594 switch(hr)
596 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
597 default: return hr;
601 /*****************************************************************************
602 * IDirectDraw7::SetDisplayMode
604 * Sets the display screen resolution, color depth and refresh frequency
605 * when in fullscreen mode (in theory).
606 * Possible return values listed in the SDK suggest that this method fails
607 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
608 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
609 * It seems to be valid to pass 0 for With and Height, this has to be tested
610 * It could mean that the current video mode should be left as-is. (But why
611 * call it then?)
613 * Params:
614 * Height, Width: Screen dimension
615 * BPP: Color depth in Bits per pixel
616 * Refreshrate: Screen refresh rate
617 * Flags: Other stuff
619 * Returns
620 * DD_OK on success
622 *****************************************************************************/
623 static HRESULT WINAPI
624 IDirectDrawImpl_SetDisplayMode(IDirectDraw7 *iface,
625 DWORD Width,
626 DWORD Height,
627 DWORD BPP,
628 DWORD RefreshRate,
629 DWORD Flags)
631 if (force_refresh_rate != 0)
633 TRACE("ForceRefreshRate overriding passed-in refresh rate (%d Hz) to %d Hz\n", RefreshRate, force_refresh_rate);
634 RefreshRate = force_refresh_rate;
637 return IDirectDrawImpl_SetDisplayModeNoOverride(iface, Width, Height, BPP,
638 RefreshRate, Flags);
641 /*****************************************************************************
642 * IDirectDraw7::RestoreDisplayMode
644 * Restores the display mode to what it was at creation time. Basically.
646 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
647 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
648 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
649 * -> DD_1 is released. The screen should be left at 1024x768x32.
650 * -> DD_2 is released. The screen should be set to 1400x1050x32
651 * This case is unhandled right now, but Empire Earth does it this way.
652 * (But perhaps there is something in SetCooperativeLevel to prevent this)
654 * The msdn says that this method resets the display mode to what it was before
655 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
657 * Returns
658 * DD_OK on success
659 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
661 *****************************************************************************/
662 static HRESULT WINAPI
663 IDirectDrawImpl_RestoreDisplayMode(IDirectDraw7 *iface)
665 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
666 TRACE("(%p)\n", This);
668 return IDirectDrawImpl_SetDisplayModeNoOverride(ICOM_INTERFACE(This, IDirectDraw7),
669 This->orig_width,
670 This->orig_height,
671 This->orig_bpp,
676 /*****************************************************************************
677 * IDirectDraw7::GetCaps
679 * Returns the drives capabilities
681 * Used for version 1, 2, 4 and 7
683 * Params:
684 * DriverCaps: Structure to write the Hardware accelerated caps to
685 * HelCaps: Structure to write the emulation caps to
687 * Returns
688 * This implementation returns DD_OK only
690 *****************************************************************************/
691 static HRESULT WINAPI
692 IDirectDrawImpl_GetCaps(IDirectDraw7 *iface,
693 DDCAPS *DriverCaps,
694 DDCAPS *HELCaps)
696 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
697 DDCAPS caps;
698 WINED3DCAPS winecaps;
699 HRESULT hr;
700 DDSCAPS2 ddscaps = {0, 0, 0, 0};
701 TRACE("(%p)->(%p,%p)\n", This, DriverCaps, HELCaps);
703 /* One structure must be != NULL */
704 if( (!DriverCaps) && (!HELCaps) )
706 ERR("(%p) Invalid params to IDirectDrawImpl_GetCaps\n", This);
707 return DDERR_INVALIDPARAMS;
710 memset(&caps, 0, sizeof(caps));
711 memset(&winecaps, 0, sizeof(winecaps));
712 caps.dwSize = sizeof(caps);
713 EnterCriticalSection(&ddraw_cs);
714 hr = IWineD3DDevice_GetDeviceCaps(This->wineD3DDevice, &winecaps);
715 if(FAILED(hr)) {
716 WARN("IWineD3DDevice::GetDeviceCaps failed\n");
717 LeaveCriticalSection(&ddraw_cs);
718 return hr;
721 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
722 LeaveCriticalSection(&ddraw_cs);
723 if(FAILED(hr)) {
724 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
725 return hr;
728 caps.dwCaps = winecaps.DirectDrawCaps.Caps;
729 caps.dwCaps2 = winecaps.DirectDrawCaps.Caps2;
730 caps.dwCKeyCaps = winecaps.DirectDrawCaps.CKeyCaps;
731 caps.dwFXCaps = winecaps.DirectDrawCaps.FXCaps;
732 caps.dwPalCaps = winecaps.DirectDrawCaps.PalCaps;
733 caps.ddsCaps.dwCaps = winecaps.DirectDrawCaps.ddsCaps;
734 caps.dwSVBCaps = winecaps.DirectDrawCaps.SVBCaps;
735 caps.dwSVBCKeyCaps = winecaps.DirectDrawCaps.SVBCKeyCaps;
736 caps.dwSVBFXCaps = winecaps.DirectDrawCaps.SVBFXCaps;
737 caps.dwVSBCaps = winecaps.DirectDrawCaps.VSBCaps;
738 caps.dwVSBCKeyCaps = winecaps.DirectDrawCaps.VSBCKeyCaps;
739 caps.dwVSBFXCaps = winecaps.DirectDrawCaps.VSBFXCaps;
740 caps.dwSSBCaps = winecaps.DirectDrawCaps.SSBCaps;
741 caps.dwSSBCKeyCaps = winecaps.DirectDrawCaps.SSBCKeyCaps;
742 caps.dwSSBFXCaps = winecaps.DirectDrawCaps.SSBFXCaps;
744 /* Even if WineD3D supports 3D rendering, remove the cap if ddraw is configured
745 * not to use it
747 if(DefaultSurfaceType == SURFACE_GDI) {
748 caps.dwCaps &= ~DDCAPS_3D;
749 caps.ddsCaps.dwCaps &= ~(DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER);
751 if(winecaps.DirectDrawCaps.StrideAlign != 0) {
752 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
753 caps.dwAlignStrideAlign = winecaps.DirectDrawCaps.StrideAlign;
756 if(DriverCaps)
758 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
759 if (TRACE_ON(ddraw))
761 TRACE("Driver Caps :\n");
762 DDRAW_dump_DDCAPS(DriverCaps);
766 if(HELCaps)
768 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
769 if (TRACE_ON(ddraw))
771 TRACE("HEL Caps :\n");
772 DDRAW_dump_DDCAPS(HELCaps);
776 return DD_OK;
779 /*****************************************************************************
780 * IDirectDraw7::Compact
782 * No idea what it does, MSDN says it's not implemented.
784 * Returns
785 * DD_OK, but this is unchecked
787 *****************************************************************************/
788 static HRESULT WINAPI
789 IDirectDrawImpl_Compact(IDirectDraw7 *iface)
791 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
792 TRACE("(%p)\n", This);
794 return DD_OK;
797 /*****************************************************************************
798 * IDirectDraw7::GetDisplayMode
800 * Returns information about the current display mode
802 * Exists in Version 1, 2, 4 and 7
804 * Params:
805 * DDSD: Address of a surface description structure to write the info to
807 * Returns
808 * DD_OK
810 *****************************************************************************/
811 static HRESULT WINAPI
812 IDirectDrawImpl_GetDisplayMode(IDirectDraw7 *iface,
813 DDSURFACEDESC2 *DDSD)
815 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
816 HRESULT hr;
817 WINED3DDISPLAYMODE Mode;
818 DWORD Size;
819 TRACE("(%p)->(%p): Relay\n", This, DDSD);
821 EnterCriticalSection(&ddraw_cs);
822 /* This seems sane */
823 if(!DDSD)
825 LeaveCriticalSection(&ddraw_cs);
826 return DDERR_INVALIDPARAMS;
829 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
830 * so one method can be used for all versions (Hopefully)
832 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
833 0 /* swapchain 0 */,
834 &Mode);
835 if( hr != D3D_OK )
837 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
838 LeaveCriticalSection(&ddraw_cs);
839 return hr;
842 Size = DDSD->dwSize;
843 memset(DDSD, 0, Size);
845 DDSD->dwSize = Size;
846 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
847 DDSD->dwWidth = Mode.Width;
848 DDSD->dwHeight = Mode.Height;
849 DDSD->u2.dwRefreshRate = 60;
850 DDSD->ddsCaps.dwCaps = 0;
851 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
852 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, Mode.Format);
853 DDSD->u1.lPitch = Mode.Width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
855 if(TRACE_ON(ddraw))
857 TRACE("Returning surface desc :\n");
858 DDRAW_dump_surface_desc(DDSD);
861 LeaveCriticalSection(&ddraw_cs);
862 return DD_OK;
865 /*****************************************************************************
866 * IDirectDraw7::GetFourCCCodes
868 * Returns an array of supported FourCC codes.
870 * Exists in Version 1, 2, 4 and 7
872 * Params:
873 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
874 * of enumerated codes
875 * Codes: Pointer to an array of DWORDs where the supported codes are written
876 * to
878 * Returns
879 * Always returns DD_OK, as it's a stub for now
881 *****************************************************************************/
882 static HRESULT WINAPI
883 IDirectDrawImpl_GetFourCCCodes(IDirectDraw7 *iface,
884 DWORD *NumCodes, DWORD *Codes)
886 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
887 FIXME("(%p)->(%p, %p): Stub!\n", This, NumCodes, Codes);
889 if(NumCodes) *NumCodes = 0;
891 return DD_OK;
894 /*****************************************************************************
895 * IDirectDraw7::GetMonitorFrequency
897 * Returns the monitor's frequency
899 * Exists in Version 1, 2, 4 and 7
901 * Params:
902 * Freq: Pointer to a DWORD to write the frequency to
904 * Returns
905 * Always returns DD_OK
907 *****************************************************************************/
908 static HRESULT WINAPI
909 IDirectDrawImpl_GetMonitorFrequency(IDirectDraw7 *iface,
910 DWORD *Freq)
912 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
913 TRACE("(%p)->(%p)\n", This, Freq);
915 /* Ideally this should be in WineD3D, as it concerns the screen setup,
916 * but for now this should make the games happy
918 *Freq = 60;
919 return DD_OK;
922 /*****************************************************************************
923 * IDirectDraw7::GetVerticalBlankStatus
925 * Returns the Vertical blank status of the monitor. This should be in WineD3D
926 * too basically, but as it's a semi stub, I didn't create a function there
928 * Params:
929 * status: Pointer to a BOOL to be filled with the vertical blank status
931 * Returns
932 * DD_OK on success
933 * DDERR_INVALIDPARAMS if status is NULL
935 *****************************************************************************/
936 static HRESULT WINAPI
937 IDirectDrawImpl_GetVerticalBlankStatus(IDirectDraw7 *iface,
938 BOOL *status)
940 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
941 TRACE("(%p)->(%p)\n", This, status);
943 /* This looks sane, the MSDN suggests it too */
944 EnterCriticalSection(&ddraw_cs);
945 if(!status)
947 LeaveCriticalSection(&ddraw_cs);
948 return DDERR_INVALIDPARAMS;
951 *status = This->fake_vblank;
952 This->fake_vblank = !This->fake_vblank;
953 LeaveCriticalSection(&ddraw_cs);
954 return DD_OK;
957 /*****************************************************************************
958 * IDirectDraw7::GetAvailableVidMem
960 * Returns the total and free video memory
962 * Params:
963 * Caps: Specifies the memory type asked for
964 * total: Pointer to a DWORD to be filled with the total memory
965 * free: Pointer to a DWORD to be filled with the free memory
967 * Returns
968 * DD_OK on success
969 * DDERR_INVALIDPARAMS of free and total are NULL
971 *****************************************************************************/
972 static HRESULT WINAPI
973 IDirectDrawImpl_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total, DWORD *free)
975 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
976 TRACE("(%p)->(%p, %p, %p)\n", This, Caps, total, free);
978 if(TRACE_ON(ddraw))
980 TRACE("(%p) Asked for memory with description: ", This);
981 DDRAW_dump_DDSCAPS2(Caps);
983 EnterCriticalSection(&ddraw_cs);
985 /* Todo: System memory vs local video memory vs non-local video memory
986 * The MSDN also mentions differences between texture memory and other
987 * resources, but that's not important
990 if( (!total) && (!free) )
992 LeaveCriticalSection(&ddraw_cs);
993 return DDERR_INVALIDPARAMS;
996 if(total) *total = This->total_vidmem;
997 if(free) *free = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
999 LeaveCriticalSection(&ddraw_cs);
1000 return DD_OK;
1003 /*****************************************************************************
1004 * IDirectDraw7::Initialize
1006 * Initializes a DirectDraw interface.
1008 * Params:
1009 * GUID: Interface identifier. Well, don't know what this is really good
1010 * for
1012 * Returns
1013 * Returns DD_OK on the first call,
1014 * DDERR_ALREADYINITIALIZED on repeated calls
1016 *****************************************************************************/
1017 static HRESULT WINAPI
1018 IDirectDrawImpl_Initialize(IDirectDraw7 *iface,
1019 GUID *Guid)
1021 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1022 TRACE("(%p)->(%s): No-op\n", This, debugstr_guid(Guid));
1024 if(This->initialized)
1026 return DDERR_ALREADYINITIALIZED;
1028 else
1030 return DD_OK;
1034 /*****************************************************************************
1035 * IDirectDraw7::FlipToGDISurface
1037 * "Makes the surface that the GDI writes to the primary surface"
1038 * Looks like some windows specific thing we don't have to care about.
1039 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1040 * show error boxes ;)
1041 * Well, just return DD_OK.
1043 * Returns:
1044 * Always returns DD_OK
1046 *****************************************************************************/
1047 static HRESULT WINAPI
1048 IDirectDrawImpl_FlipToGDISurface(IDirectDraw7 *iface)
1050 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1051 TRACE("(%p)\n", This);
1053 return DD_OK;
1056 /*****************************************************************************
1057 * IDirectDraw7::WaitForVerticalBlank
1059 * This method allows applications to get in sync with the vertical blank
1060 * interval.
1061 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1062 * redraw the screen, most likely because of this stub
1064 * Parameters:
1065 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1066 * or DDWAITVB_BLOCKEND
1067 * h: Not used, according to MSDN
1069 * Returns:
1070 * Always returns DD_OK
1072 *****************************************************************************/
1073 static HRESULT WINAPI
1074 IDirectDrawImpl_WaitForVerticalBlank(IDirectDraw7 *iface,
1075 DWORD Flags,
1076 HANDLE h)
1078 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1079 FIXME("(%p)->(%x,%p): Stub\n", This, Flags, h);
1081 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1082 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1083 return DDERR_UNSUPPORTED; /* unchecked */
1085 return DD_OK;
1088 /*****************************************************************************
1089 * IDirectDraw7::GetScanLine
1091 * Returns the scan line that is being drawn on the monitor
1093 * Parameters:
1094 * Scanline: Address to write the scan line value to
1096 * Returns:
1097 * Always returns DD_OK
1099 *****************************************************************************/
1100 static HRESULT WINAPI IDirectDrawImpl_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1102 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1103 static BOOL hide = FALSE;
1104 WINED3DDISPLAYMODE Mode;
1106 /* This function is called often, so print the fixme only once */
1107 EnterCriticalSection(&ddraw_cs);
1108 if(!hide)
1110 FIXME("(%p)->(%p): Semi-Stub\n", This, Scanline);
1111 hide = TRUE;
1114 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1116 &Mode);
1118 /* Fake the line sweeping of the monitor */
1119 /* FIXME: We should synchronize with a source to keep the refresh rate */
1120 *Scanline = This->cur_scanline++;
1121 /* Assume 20 scan lines in the vertical blank */
1122 if (This->cur_scanline >= Mode.Height + 20)
1123 This->cur_scanline = 0;
1125 LeaveCriticalSection(&ddraw_cs);
1126 return DD_OK;
1129 /*****************************************************************************
1130 * IDirectDraw7::TestCooperativeLevel
1132 * Informs the application about the state of the video adapter, depending
1133 * on the cooperative level
1135 * Returns:
1136 * DD_OK if the device is in a sane state
1137 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1138 * if the state is not correct(See below)
1140 *****************************************************************************/
1141 static HRESULT WINAPI
1142 IDirectDrawImpl_TestCooperativeLevel(IDirectDraw7 *iface)
1144 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1145 HRESULT hr;
1146 TRACE("(%p)\n", This);
1148 EnterCriticalSection(&ddraw_cs);
1149 /* Description from MSDN:
1150 * For fullscreen apps return DDERR_NOEXCLUSIVEMODE if the user switched
1151 * away from the app with e.g. alt-tab. Windowed apps receive
1152 * DDERR_EXCLUSIVEMODEALREADYSET if another application created a
1153 * DirectDraw object in exclusive mode. DDERR_WRONGMODE is returned,
1154 * when the video mode has changed
1157 hr = IWineD3DDevice_TestCooperativeLevel(This->wineD3DDevice);
1159 /* Fix the result value. These values are mapped from their
1160 * d3d9 counterpart.
1162 switch(hr)
1164 case WINED3DERR_DEVICELOST:
1165 if(This->cooperative_level & DDSCL_EXCLUSIVE)
1167 LeaveCriticalSection(&ddraw_cs);
1168 return DDERR_NOEXCLUSIVEMODE;
1170 else
1172 LeaveCriticalSection(&ddraw_cs);
1173 return DDERR_EXCLUSIVEMODEALREADYSET;
1176 case WINED3DERR_DEVICENOTRESET:
1177 LeaveCriticalSection(&ddraw_cs);
1178 return DD_OK;
1180 case WINED3D_OK:
1181 LeaveCriticalSection(&ddraw_cs);
1182 return DD_OK;
1184 case WINED3DERR_DRIVERINTERNALERROR:
1185 default:
1186 ERR("(%p) Unexpected return value %08x from wineD3D, "
1187 " returning DD_OK\n", This, hr);
1190 LeaveCriticalSection(&ddraw_cs);
1191 return DD_OK;
1194 /*****************************************************************************
1195 * IDirectDraw7::GetGDISurface
1197 * Returns the surface that GDI is treating as the primary surface.
1198 * For Wine this is the front buffer
1200 * Params:
1201 * GDISurface: Address to write the surface pointer to
1203 * Returns:
1204 * DD_OK if the surface was found
1205 * DDERR_NOTFOUND if the GDI surface wasn't found
1207 *****************************************************************************/
1208 static HRESULT WINAPI
1209 IDirectDrawImpl_GetGDISurface(IDirectDraw7 *iface,
1210 IDirectDrawSurface7 **GDISurface)
1212 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1213 IWineD3DSurface *Surf;
1214 IDirectDrawSurface7 *ddsurf;
1215 HRESULT hr;
1216 DDSCAPS2 ddsCaps;
1217 TRACE("(%p)->(%p)\n", This, GDISurface);
1219 /* Get the back buffer from the wineD3DDevice and search its
1220 * attached surfaces for the front buffer
1222 EnterCriticalSection(&ddraw_cs);
1223 hr = IWineD3DDevice_GetBackBuffer(This->wineD3DDevice,
1224 0, /* SwapChain */
1225 0, /* first back buffer*/
1226 WINED3DBACKBUFFER_TYPE_MONO,
1227 &Surf);
1229 if( (hr != D3D_OK) ||
1230 (!Surf) )
1232 ERR("IWineD3DDevice::GetBackBuffer failed\n");
1233 LeaveCriticalSection(&ddraw_cs);
1234 return DDERR_NOTFOUND;
1237 /* GetBackBuffer AddRef()ed the surface, release it */
1238 IWineD3DSurface_Release(Surf);
1240 IWineD3DSurface_GetParent(Surf,
1241 (IUnknown **) &ddsurf);
1242 IDirectDrawSurface7_Release(ddsurf); /* For the GetParent */
1244 /* Find the front buffer */
1245 ddsCaps.dwCaps = DDSCAPS_FRONTBUFFER;
1246 hr = IDirectDrawSurface7_GetAttachedSurface(ddsurf,
1247 &ddsCaps,
1248 GDISurface);
1249 if(hr != DD_OK)
1251 ERR("IDirectDrawSurface7::GetAttachedSurface failed, hr = %x\n", hr);
1254 /* The AddRef is OK this time */
1255 LeaveCriticalSection(&ddraw_cs);
1256 return hr;
1259 /*****************************************************************************
1260 * IDirectDraw7::EnumDisplayModes
1262 * Enumerates the supported Display modes. The modes can be filtered with
1263 * the DDSD parameter.
1265 * Params:
1266 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES
1267 * DDSD: Surface description to filter the modes
1268 * Context: Pointer passed back to the callback function
1269 * cb: Application-provided callback function
1271 * Returns:
1272 * DD_OK on success
1273 * DDERR_INVALIDPARAMS if the callback wasn't set
1275 *****************************************************************************/
1276 static HRESULT WINAPI
1277 IDirectDrawImpl_EnumDisplayModes(IDirectDraw7 *iface,
1278 DWORD Flags,
1279 DDSURFACEDESC2 *DDSD,
1280 void *Context,
1281 LPDDENUMMODESCALLBACK2 cb)
1283 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1284 unsigned int modenum, fmt;
1285 WINED3DFORMAT pixelformat = WINED3DFMT_UNKNOWN;
1286 WINED3DDISPLAYMODE mode;
1287 DDSURFACEDESC2 callback_sd;
1288 WINED3DDISPLAYMODE *enum_modes = NULL;
1289 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
1291 WINED3DFORMAT checkFormatList[] =
1293 WINED3DFMT_R8G8B8,
1294 WINED3DFMT_A8R8G8B8,
1295 WINED3DFMT_X8R8G8B8,
1296 WINED3DFMT_R5G6B5,
1297 WINED3DFMT_X1R5G5B5,
1298 WINED3DFMT_A1R5G5B5,
1299 WINED3DFMT_A4R4G4B4,
1300 WINED3DFMT_R3G3B2,
1301 WINED3DFMT_A8R3G3B2,
1302 WINED3DFMT_X4R4G4B4,
1303 WINED3DFMT_A2B10G10R10,
1304 WINED3DFMT_A8B8G8R8,
1305 WINED3DFMT_X8B8G8R8,
1306 WINED3DFMT_A2R10G10B10,
1307 WINED3DFMT_A8P8,
1308 WINED3DFMT_P8
1311 TRACE("(%p)->(%p,%p,%p): Relay\n", This, DDSD, Context, cb);
1313 EnterCriticalSection(&ddraw_cs);
1314 /* This looks sane */
1315 if(!cb)
1317 LeaveCriticalSection(&ddraw_cs);
1318 return DDERR_INVALIDPARAMS;
1321 if(DDSD)
1323 if ((DDSD->dwFlags & DDSD_PIXELFORMAT) && (DDSD->u4.ddpfPixelFormat.dwFlags & DDPF_RGB) )
1324 pixelformat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
1327 if(!(Flags & DDEDM_REFRESHRATES))
1329 enum_mode_array_size = 16;
1330 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1331 if (!enum_modes)
1333 ERR("Out of memory\n");
1334 LeaveCriticalSection(&ddraw_cs);
1335 return DDERR_OUTOFMEMORY;
1339 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
1341 if(pixelformat != WINED3DFMT_UNKNOWN && checkFormatList[fmt] != pixelformat)
1343 continue;
1346 modenum = 0;
1347 while(IWineD3D_EnumAdapterModes(This->wineD3D,
1348 WINED3DADAPTER_DEFAULT,
1349 checkFormatList[fmt],
1350 modenum++,
1351 &mode) == WINED3D_OK)
1353 if(DDSD)
1355 if(DDSD->dwFlags & DDSD_WIDTH && mode.Width != DDSD->dwWidth) continue;
1356 if(DDSD->dwFlags & DDSD_HEIGHT && mode.Height != DDSD->dwHeight) continue;
1359 if(!(Flags & DDEDM_REFRESHRATES))
1361 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
1362 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
1363 * to be reduced to a single unique result in such case.
1365 BOOL found = FALSE;
1366 unsigned i;
1368 for (i = 0; i < enum_mode_count; i++)
1370 if(enum_modes[i].Width == mode.Width && enum_modes[i].Height == mode.Height &&
1371 enum_modes[i].Format == mode.Format)
1373 found = TRUE;
1374 break;
1378 if(found) continue;
1381 memset(&callback_sd, 0, sizeof(callback_sd));
1382 callback_sd.dwSize = sizeof(callback_sd);
1383 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1385 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH;
1386 if(Flags & DDEDM_REFRESHRATES)
1388 callback_sd.dwFlags |= DDSD_REFRESHRATE;
1389 callback_sd.u2.dwRefreshRate = mode.RefreshRate;
1392 callback_sd.dwWidth = mode.Width;
1393 callback_sd.dwHeight = mode.Height;
1395 PixelFormat_WineD3DtoDD(&callback_sd.u4.ddpfPixelFormat, mode.Format);
1397 /* Calc pitch and DWORD align like MSDN says */
1398 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.Width;
1399 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
1401 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
1402 callback_sd.u2.dwRefreshRate);
1404 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
1406 TRACE("Application asked to terminate the enumeration\n");
1407 HeapFree(GetProcessHeap(), 0, enum_modes);
1408 LeaveCriticalSection(&ddraw_cs);
1409 return DD_OK;
1412 if(!(Flags & DDEDM_REFRESHRATES))
1414 if (enum_mode_count == enum_mode_array_size)
1416 WINED3DDISPLAYMODE *new_enum_modes;
1418 enum_mode_array_size *= 2;
1419 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1421 if (!new_enum_modes)
1423 ERR("Out of memory\n");
1424 HeapFree(GetProcessHeap(), 0, enum_modes);
1425 LeaveCriticalSection(&ddraw_cs);
1426 return DDERR_OUTOFMEMORY;
1429 enum_modes = new_enum_modes;
1432 enum_modes[enum_mode_count++] = mode;
1437 TRACE("End of enumeration\n");
1438 HeapFree(GetProcessHeap(), 0, enum_modes);
1439 LeaveCriticalSection(&ddraw_cs);
1440 return DD_OK;
1443 /*****************************************************************************
1444 * IDirectDraw7::EvaluateMode
1446 * Used with IDirectDraw7::StartModeTest to test video modes.
1447 * EvaluateMode is used to pass or fail a mode, and continue with the next
1448 * mode
1450 * Params:
1451 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
1452 * Timeout: Returns the amount of seconds left before the mode would have
1453 * been failed automatically
1455 * Returns:
1456 * This implementation always DD_OK, because it's a stub
1458 *****************************************************************************/
1459 static HRESULT WINAPI
1460 IDirectDrawImpl_EvaluateMode(IDirectDraw7 *iface,
1461 DWORD Flags,
1462 DWORD *Timeout)
1464 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1465 FIXME("(%p)->(%d,%p): Stub!\n", This, Flags, Timeout);
1467 /* When implementing this, implement it in WineD3D */
1469 return DD_OK;
1472 /*****************************************************************************
1473 * IDirectDraw7::GetDeviceIdentifier
1475 * Returns the device identifier, which gives information about the driver
1476 * Our device identifier is defined at the beginning of this file.
1478 * Params:
1479 * DDDI: Address for the returned structure
1480 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
1482 * Returns:
1483 * On success it returns DD_OK
1484 * DDERR_INVALIDPARAMS if DDDI is NULL
1486 *****************************************************************************/
1487 static HRESULT WINAPI
1488 IDirectDrawImpl_GetDeviceIdentifier(IDirectDraw7 *iface,
1489 DDDEVICEIDENTIFIER2 *DDDI,
1490 DWORD Flags)
1492 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1493 TRACE("(%p)->(%p,%08x)\n", This, DDDI, Flags);
1495 if(!DDDI)
1496 return DDERR_INVALIDPARAMS;
1498 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
1499 * host adapter, if there's a secondary 3D adapter. This doesn't apply
1500 * to any modern hardware, nor is it interesting for Wine, so ignore it
1503 *DDDI = deviceidentifier;
1504 return DD_OK;
1507 /*****************************************************************************
1508 * IDirectDraw7::GetSurfaceFromDC
1510 * Returns the Surface for a GDI device context handle.
1511 * Is this related to IDirectDrawSurface::GetDC ???
1513 * Params:
1514 * hdc: hdc to return the surface for
1515 * Surface: Address to write the surface pointer to
1517 * Returns:
1518 * Always returns DD_OK because it's a stub
1520 *****************************************************************************/
1521 static HRESULT WINAPI
1522 IDirectDrawImpl_GetSurfaceFromDC(IDirectDraw7 *iface,
1523 HDC hdc,
1524 IDirectDrawSurface7 **Surface)
1526 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1527 FIXME("(%p)->(%p,%p): Stub!\n", This, hdc, Surface);
1529 /* Implementation idea if needed: Loop through all surfaces and compare
1530 * their hdc with hdc. Implement it in WineD3D! */
1531 return DDERR_NOTFOUND;
1534 /*****************************************************************************
1535 * IDirectDraw7::RestoreAllSurfaces
1537 * Calls the restore method of all surfaces
1539 * Params:
1541 * Returns:
1542 * Always returns DD_OK because it's a stub
1544 *****************************************************************************/
1545 static HRESULT WINAPI
1546 IDirectDrawImpl_RestoreAllSurfaces(IDirectDraw7 *iface)
1548 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1549 FIXME("(%p): Stub\n", This);
1551 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
1552 * get their parent and call their restore method. Do not implement
1553 * it in WineD3D, as restoring a surface means re-creating the
1554 * WineD3DDSurface
1556 return DD_OK;
1559 /*****************************************************************************
1560 * IDirectDraw7::StartModeTest
1562 * Tests the specified video modes to update the system registry with
1563 * refresh rate information. StartModeTest starts the mode test,
1564 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
1565 * isn't called within 15 seconds, the mode is failed automatically
1567 * As refresh rates are handled by the X server, I don't think this
1568 * Method is important
1570 * Params:
1571 * Modes: An array of mode specifications
1572 * NumModes: The number of modes in Modes
1573 * Flags: Some flags...
1575 * Returns:
1576 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
1577 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
1578 * otherwise DD_OK
1580 *****************************************************************************/
1581 static HRESULT WINAPI
1582 IDirectDrawImpl_StartModeTest(IDirectDraw7 *iface,
1583 SIZE *Modes,
1584 DWORD NumModes,
1585 DWORD Flags)
1587 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1588 WARN("(%p)->(%p, %d, %x): Semi-Stub, most likely harmless\n", This, Modes, NumModes, Flags);
1590 /* This looks sane */
1591 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
1593 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
1594 * As it is not, DDERR_TESTFINISHED is returned
1595 * (hopefully that's correct
1597 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
1598 * well, that value doesn't (yet) exist in the wine headers, so ignore it
1601 return DD_OK;
1604 /*****************************************************************************
1605 * IDirectDrawImpl_RecreateSurfacesCallback
1607 * Enumeration callback for IDirectDrawImpl_RecreateAllSurfaces.
1608 * It re-recreates the WineD3DSurface. It's pretty straightforward
1610 *****************************************************************************/
1611 HRESULT WINAPI
1612 IDirectDrawImpl_RecreateSurfacesCallback(IDirectDrawSurface7 *surf,
1613 DDSURFACEDESC2 *desc,
1614 void *Context)
1616 IDirectDrawSurfaceImpl *surfImpl = ICOM_OBJECT(IDirectDrawSurfaceImpl,
1617 IDirectDrawSurface7,
1618 surf);
1619 IDirectDrawImpl *This = surfImpl->ddraw;
1620 IUnknown *Parent;
1621 IParentImpl *parImpl = NULL;
1622 IWineD3DSurface *wineD3DSurface;
1623 IWineD3DSwapChain *swapchain;
1624 HRESULT hr;
1625 void *tmp;
1626 IWineD3DClipper *clipper = NULL;
1628 WINED3DSURFACE_DESC Desc;
1629 WINED3DFORMAT Format;
1630 WINED3DRESOURCETYPE Type;
1631 DWORD Usage;
1632 WINED3DPOOL Pool;
1633 UINT Size;
1635 WINED3DMULTISAMPLE_TYPE MultiSampleType;
1636 DWORD MultiSampleQuality;
1637 UINT Width;
1638 UINT Height;
1640 TRACE("(%p): Enumerated Surface %p\n", This, surfImpl);
1642 /* For the enumeration */
1643 IDirectDrawSurface7_Release(surf);
1645 if(surfImpl->ImplType == This->ImplType) return DDENUMRET_OK; /* Continue */
1647 /* Get the objects */
1648 swapchain = surfImpl->wineD3DSwapChain;
1649 surfImpl->wineD3DSwapChain = NULL;
1650 wineD3DSurface = surfImpl->WineD3DSurface;
1651 IWineD3DSurface_GetParent(wineD3DSurface, &Parent);
1652 IUnknown_Release(Parent); /* For the getParent */
1654 /* Is the parent an IParent interface? */
1655 if(IUnknown_QueryInterface(Parent, &IID_IParent, &tmp) == S_OK)
1657 /* It is a IParent interface! */
1658 IUnknown_Release(Parent); /* For the QueryInterface */
1659 parImpl = ICOM_OBJECT(IParentImpl, IParent, Parent);
1660 /* Release the reference the parent interface is holding */
1661 IWineD3DSurface_Release(wineD3DSurface);
1664 /* get the clipper */
1665 IWineD3DSurface_GetClipper(wineD3DSurface, &clipper);
1667 /* Get the surface properties */
1668 Desc.Format = &Format;
1669 Desc.Type = &Type;
1670 Desc.Usage = &Usage;
1671 Desc.Pool = &Pool;
1672 Desc.Size = &Size;
1673 Desc.MultiSampleType = &MultiSampleType;
1674 Desc.MultiSampleQuality = &MultiSampleQuality;
1675 Desc.Width = &Width;
1676 Desc.Height = &Height;
1678 hr = IWineD3DSurface_GetDesc(wineD3DSurface, &Desc);
1679 if(hr != D3D_OK) return hr;
1681 if(swapchain) {
1682 /* If there's a swapchain, it owns the IParent interface. Create a new one for the
1683 * new surface
1685 parImpl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*parImpl));
1686 ICOM_INIT_INTERFACE(parImpl, IParent, IParent_Vtbl);
1687 parImpl->ref = 1;
1689 Parent = (IUnknown *) parImpl;
1692 /* Create the new surface */
1693 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice,
1694 Width, Height, Format,
1695 TRUE /* Lockable */,
1696 FALSE /* Discard */,
1697 surfImpl->mipmap_level,
1698 &surfImpl->WineD3DSurface,
1699 Type,
1700 Usage,
1701 Pool,
1702 MultiSampleType,
1703 MultiSampleQuality,
1704 0 /* SharedHandle */,
1705 This->ImplType,
1706 Parent);
1708 if(hr != D3D_OK)
1709 return hr;
1711 IWineD3DSurface_SetClipper(surfImpl->WineD3DSurface, clipper);
1713 /* Update the IParent if it exists */
1714 if(parImpl)
1716 parImpl->child = (IUnknown *) surfImpl->WineD3DSurface;
1717 /* Add a reference for the IParent */
1718 IWineD3DSurface_AddRef(surfImpl->WineD3DSurface);
1720 /* TODO: Copy the surface content, except for render targets */
1722 /* If there's a swapchain, it owns the wined3d surfaces. So Destroy
1723 * the swapchain
1725 if(swapchain) {
1726 /* The backbuffers have the swapchain set as well, but the primary
1727 * owns it and destroys it
1729 if(surfImpl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
1730 IWineD3DDevice_UninitGDI(This->wineD3DDevice, D3D7CB_DestroySwapChain);
1732 surfImpl->isRenderTarget = FALSE;
1733 } else {
1734 if(IWineD3DSurface_Release(wineD3DSurface) == 0)
1735 TRACE("Surface released successful, next surface\n");
1736 else
1737 ERR("Something's still holding the old WineD3DSurface\n");
1740 surfImpl->ImplType = This->ImplType;
1742 if(clipper)
1744 IWineD3DClipper_Release(clipper);
1746 return DDENUMRET_OK;
1749 /*****************************************************************************
1750 * IDirectDrawImpl_RecreateAllSurfaces
1752 * A function, that converts all wineD3DSurfaces to the new implementation type
1753 * It enumerates all surfaces with IWineD3DDevice::EnumSurfaces, creates a
1754 * new WineD3DSurface, copies the content and releases the old surface
1756 *****************************************************************************/
1757 static HRESULT
1758 IDirectDrawImpl_RecreateAllSurfaces(IDirectDrawImpl *This)
1760 DDSURFACEDESC2 desc;
1761 TRACE("(%p): Switch to implementation %d\n", This, This->ImplType);
1763 if(This->ImplType != SURFACE_OPENGL && This->d3d_initialized)
1765 /* Should happen almost never */
1766 FIXME("(%p) Switching to non-opengl surfaces with d3d started. Is this a bug?\n", This);
1767 /* Shutdown d3d */
1768 IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroyDepthStencilSurface, D3D7CB_DestroySwapChain);
1770 /* Contrary: D3D starting is handled by the caller, because it knows the render target */
1772 memset(&desc, 0, sizeof(desc));
1773 desc.dwSize = sizeof(desc);
1775 return IDirectDraw7_EnumSurfaces(ICOM_INTERFACE(This, IDirectDraw7),
1777 &desc,
1778 This,
1779 IDirectDrawImpl_RecreateSurfacesCallback);
1782 /*****************************************************************************
1783 * D3D7CB_CreateSurface
1785 * Callback function for IDirect3DDevice_CreateTexture. It searches for the
1786 * correct mipmap sublevel, and returns it to WineD3D.
1787 * The surfaces are created already by IDirectDraw7::CreateSurface
1789 * Params:
1790 * With, Height: With and height of the surface
1791 * Format: The requested format
1792 * Usage, Pool: D3DUSAGE and D3DPOOL of the surface
1793 * level: The mipmap level
1794 * Face: The cube map face type
1795 * Surface: Pointer to pass the created surface back at
1796 * SharedHandle: NULL
1798 * Returns:
1799 * D3D_OK
1801 *****************************************************************************/
1802 static HRESULT WINAPI
1803 D3D7CB_CreateSurface(IUnknown *device,
1804 IUnknown *pSuperior,
1805 UINT Width, UINT Height,
1806 WINED3DFORMAT Format,
1807 DWORD Usage, WINED3DPOOL Pool, UINT level,
1808 WINED3DCUBEMAP_FACES Face,
1809 IWineD3DSurface **Surface,
1810 HANDLE *SharedHandle)
1812 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
1813 IDirectDrawSurfaceImpl *surf = NULL;
1814 int i = 0;
1815 DDSCAPS2 searchcaps = This->tex_root->surface_desc.ddsCaps;
1816 TRACE("(%p) call back. surf=%p. Face %d level %d\n", device, This->tex_root, Face, level);
1818 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
1819 switch(Face)
1821 case WINED3DCUBEMAP_FACE_POSITIVE_X:
1822 TRACE("Asked for positive x\n");
1823 if(searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP) {
1824 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
1826 surf = This->tex_root; break;
1827 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
1828 TRACE("Asked for negative x\n");
1829 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
1830 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
1831 TRACE("Asked for positive y\n");
1832 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
1833 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
1834 TRACE("Asked for negative y\n");
1835 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
1836 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
1837 TRACE("Asked for positive z\n");
1838 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
1839 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
1840 TRACE("Asked for negative z\n");
1841 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
1842 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
1845 if(!surf)
1847 IDirectDrawSurface7 *attached;
1848 IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(This->tex_root, IDirectDrawSurface7),
1849 &searchcaps,
1850 &attached);
1851 surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attached);
1852 IDirectDrawSurface7_Release(attached);
1854 if(!surf) ERR("root search surface not found\n");
1856 /* Find the wanted mipmap. There are enough mipmaps in the chain */
1857 while(i < level)
1859 IDirectDrawSurface7 *attached;
1860 IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(surf, IDirectDrawSurface7),
1861 &searchcaps,
1862 &attached);
1863 if(!attached) ERR("Surface not found\n");
1864 surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attached);
1865 IDirectDrawSurface7_Release(attached);
1866 i++;
1869 /* Return the surface */
1870 *Surface = surf->WineD3DSurface;
1872 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *Surface, surf);
1873 return D3D_OK;
1876 ULONG WINAPI D3D7CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
1877 IUnknown* swapChainParent;
1878 TRACE("(%p) call back\n", pSwapChain);
1880 IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
1881 IUnknown_Release(swapChainParent);
1882 return IUnknown_Release(swapChainParent);
1885 ULONG WINAPI D3D7CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
1886 IUnknown* surfaceParent;
1887 TRACE("(%p) call back\n", pSurface);
1889 IWineD3DSurface_GetParent(pSurface, &surfaceParent);
1890 IUnknown_Release(surfaceParent);
1891 return IUnknown_Release(surfaceParent);
1894 /*****************************************************************************
1895 * IDirectDrawImpl_CreateNewSurface
1897 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
1898 * with the passed parameters.
1900 * Params:
1901 * DDSD: Description of the surface to create
1902 * Surf: Address to store the interface pointer at
1904 * Returns:
1905 * DD_OK on success
1907 *****************************************************************************/
1908 static HRESULT WINAPI
1909 IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This,
1910 DDSURFACEDESC2 *pDDSD,
1911 IDirectDrawSurfaceImpl **ppSurf,
1912 UINT level)
1914 HRESULT hr;
1915 UINT Width = 0, Height = 0;
1916 WINED3DFORMAT Format = WINED3DFMT_UNKNOWN;
1917 WINED3DRESOURCETYPE ResType = WINED3DRTYPE_SURFACE;
1918 DWORD Usage = 0;
1919 WINED3DSURFTYPE ImplType = This->ImplType;
1920 WINED3DSURFACE_DESC Desc;
1921 IUnknown *Parent;
1922 IParentImpl *parImpl = NULL;
1923 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
1925 /* Dummies for GetDesc */
1926 WINED3DPOOL dummy_d3dpool;
1927 WINED3DMULTISAMPLE_TYPE dummy_mst;
1928 UINT dummy_uint;
1929 DWORD dummy_dword;
1931 if (TRACE_ON(ddraw))
1933 TRACE(" (%p) Requesting surface desc :\n", This);
1934 DDRAW_dump_surface_desc(pDDSD);
1937 /* Select the surface type, if it wasn't choosen yet */
1938 if(ImplType == SURFACE_UNKNOWN)
1940 /* Use GL Surfaces if a D3DDEVICE Surface is requested */
1941 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1943 TRACE("(%p) Choosing GL surfaces because a 3DDEVICE Surface was requested\n", This);
1944 ImplType = SURFACE_OPENGL;
1947 /* Otherwise use GDI surfaces for now */
1948 else
1950 TRACE("(%p) Choosing GDI surfaces for 2D rendering\n", This);
1951 ImplType = SURFACE_GDI;
1954 /* Policy if all surface implementations are available:
1955 * First, check if a default type was set with winecfg. If not,
1956 * try Xrender surfaces, and use them if they work. Next, check if
1957 * accelerated OpenGL is available, and use GL surfaces in this
1958 * case. If all else fails, use GDI surfaces. If a 3DDEVICE surface
1959 * was created, always use OpenGL surfaces.
1961 * (Note: Xrender surfaces are not implemented for now, the
1962 * unaccelerated implementation uses GDI to render in Software)
1965 /* Store the type. If it needs to be changed, all WineD3DSurfaces have to
1966 * be re-created. This could be done with IDirectDrawSurface7::Restore
1968 This->ImplType = ImplType;
1970 else
1972 if((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE ) &&
1973 (This->ImplType != SURFACE_OPENGL ) && DefaultSurfaceType == SURFACE_UNKNOWN)
1975 /* We have to change to OpenGL,
1976 * and re-create all WineD3DSurfaces
1978 ImplType = SURFACE_OPENGL;
1979 This->ImplType = ImplType;
1980 TRACE("(%p) Re-creating all surfaces\n", This);
1981 IDirectDrawImpl_RecreateAllSurfaces(This);
1982 TRACE("(%p) Done recreating all surfaces\n", This);
1984 else if(This->ImplType != SURFACE_OPENGL && pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1986 WARN("The application requests a 3D capable surface, but a non-opengl surface was set in the registry\n");
1987 /* Do not fail surface creation, only fail 3D device creation */
1991 if (!(pDDSD->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY)) &&
1992 !((pDDSD->ddsCaps.dwCaps & DDSCAPS_TEXTURE) && (pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)) )
1994 /* Tests show surfaces without memory flags get these flags added right after creation. */
1995 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
1997 /* Get the correct wined3d usage */
1998 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE |
1999 DDSCAPS_BACKBUFFER |
2000 DDSCAPS_3DDEVICE ) )
2002 Usage |= WINED3DUSAGE_RENDERTARGET;
2004 pDDSD->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
2006 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
2008 Usage |= WINED3DUSAGE_OVERLAY;
2010 if(This->depthstencil || (pDDSD->ddsCaps.dwCaps & DDSCAPS_ZBUFFER) )
2012 /* The depth stencil creation callback sets this flag.
2013 * Set the WineD3D usage to let it know that it's a depth
2014 * Stencil surface.
2016 Usage |= WINED3DUSAGE_DEPTHSTENCIL;
2018 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
2020 Pool = WINED3DPOOL_SYSTEMMEM;
2022 else if(pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
2024 Pool = WINED3DPOOL_MANAGED;
2025 /* Managed textures have the system memory flag set */
2026 pDDSD->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
2028 else if(pDDSD->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
2030 /* Videomemory adds localvidmem, this is mutually exclusive with systemmemory
2031 * and texturemanage
2033 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
2036 Format = PixelFormat_DD2WineD3D(&pDDSD->u4.ddpfPixelFormat);
2037 if(Format == WINED3DFMT_UNKNOWN)
2039 ERR("Unsupported / Unknown pixelformat\n");
2040 return DDERR_INVALIDPIXELFORMAT;
2043 /* Create the Surface object */
2044 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
2045 if(!*ppSurf)
2047 ERR("(%p) Error allocating memory for a surface\n", This);
2048 return DDERR_OUTOFVIDEOMEMORY;
2050 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawSurface7, IDirectDrawSurface7_Vtbl);
2051 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawSurface3, IDirectDrawSurface3_Vtbl);
2052 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawGammaControl, IDirectDrawGammaControl_Vtbl);
2053 ICOM_INIT_INTERFACE(*ppSurf, IDirect3DTexture2, IDirect3DTexture2_Vtbl);
2054 ICOM_INIT_INTERFACE(*ppSurf, IDirect3DTexture, IDirect3DTexture1_Vtbl);
2055 (*ppSurf)->ref = 1;
2056 (*ppSurf)->version = 7;
2057 (*ppSurf)->ddraw = This;
2058 (*ppSurf)->surface_desc.dwSize = sizeof(DDSURFACEDESC2);
2059 (*ppSurf)->surface_desc.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2060 DD_STRUCT_COPY_BYSIZE(&(*ppSurf)->surface_desc, pDDSD);
2062 /* Surface attachments */
2063 (*ppSurf)->next_attached = NULL;
2064 (*ppSurf)->first_attached = *ppSurf;
2066 /* Needed to re-create the surface on an implementation change */
2067 (*ppSurf)->ImplType = ImplType;
2069 /* For D3DDevice creation */
2070 (*ppSurf)->isRenderTarget = FALSE;
2072 /* A trace message for debugging */
2073 TRACE("(%p) Created IDirectDrawSurface implementation structure at %p\n", This, *ppSurf);
2075 if(pDDSD->ddsCaps.dwCaps & ( DDSCAPS_PRIMARYSURFACE | DDSCAPS_TEXTURE | DDSCAPS_3DDEVICE) )
2077 /* Render targets and textures need a IParent interface,
2078 * because WineD3D will destroy them when the swapchain
2079 * is released
2081 parImpl = HeapAlloc(GetProcessHeap(), 0, sizeof(IParentImpl));
2082 if(!parImpl)
2084 ERR("Out of memory when allocating memory for a IParent implementation\n");
2085 return DDERR_OUTOFMEMORY;
2087 parImpl->ref = 1;
2088 ICOM_INIT_INTERFACE(parImpl, IParent, IParent_Vtbl);
2089 Parent = (IUnknown *) ICOM_INTERFACE(parImpl, IParent);
2090 TRACE("Using IParent interface %p as parent\n", parImpl);
2092 else
2094 /* Use the surface as parent */
2095 Parent = (IUnknown *) ICOM_INTERFACE(*ppSurf, IDirectDrawSurface7);
2096 TRACE("Using Surface interface %p as parent\n", *ppSurf);
2099 /* Now create the WineD3D Surface */
2100 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice,
2101 pDDSD->dwWidth,
2102 pDDSD->dwHeight,
2103 Format,
2104 TRUE /* Lockable */,
2105 FALSE /* Discard */,
2106 level,
2107 &(*ppSurf)->WineD3DSurface,
2108 ResType, Usage,
2109 Pool,
2110 WINED3DMULTISAMPLE_NONE,
2111 0 /* MultiSampleQuality */,
2112 0 /* SharedHandle */,
2113 ImplType,
2114 Parent);
2116 if(hr != D3D_OK)
2118 ERR("IWineD3DDevice::CreateSurface failed. hr = %08x\n", hr);
2119 return hr;
2122 /* Set the child of the parent implementation if it exists */
2123 if(parImpl)
2125 parImpl->child = (IUnknown *) (*ppSurf)->WineD3DSurface;
2126 /* The IParent releases the WineD3DSurface, and
2127 * the ddraw surface does that too. Hold a reference
2129 IWineD3DSurface_AddRef((*ppSurf)->WineD3DSurface);
2132 /* Increase the surface counter, and attach the surface */
2133 InterlockedIncrement(&This->surfaces);
2134 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
2136 /* Here we could store all created surfaces in the DirectDrawImpl structure,
2137 * But this could also be delegated to WineDDraw, as it keeps track of all its
2138 * resources. Not implemented for now, as there are more important things ;)
2141 /* Get the pixel format of the WineD3DSurface and store it.
2142 * Don't use the Format choosen above, WineD3D might have
2143 * changed it
2145 Desc.Format = &Format;
2146 Desc.Type = &ResType;
2147 Desc.Usage = &Usage;
2148 Desc.Pool = &dummy_d3dpool;
2149 Desc.Size = &dummy_uint;
2150 Desc.MultiSampleType = &dummy_mst;
2151 Desc.MultiSampleQuality = &dummy_dword;
2152 Desc.Width = &Width;
2153 Desc.Height = &Height;
2155 (*ppSurf)->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
2156 hr = IWineD3DSurface_GetDesc((*ppSurf)->WineD3DSurface, &Desc);
2157 if(hr != D3D_OK)
2159 ERR("IWineD3DSurface::GetDesc failed\n");
2160 IDirectDrawSurface7_Release( (IDirectDrawSurface7 *) *ppSurf);
2161 return hr;
2164 if(Format == WINED3DFMT_UNKNOWN)
2166 FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN\n");
2168 PixelFormat_WineD3DtoDD( &(*ppSurf)->surface_desc.u4.ddpfPixelFormat, Format);
2170 /* Anno 1602 stores the pitch right after surface creation, so make sure it's there.
2171 * I can't LockRect() the surface here because if OpenGL surfaces are in use, the
2172 * WineD3DDevice might not be usable for 3D yet, so an extra method was created.
2173 * TODO: Test other fourcc formats
2175 if(Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3 ||
2176 Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5)
2178 (*ppSurf)->surface_desc.dwFlags |= DDSD_LINEARSIZE;
2179 if(Format == WINED3DFMT_DXT1)
2181 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height) / 2;
2183 else
2185 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height);
2188 else
2190 (*ppSurf)->surface_desc.dwFlags |= DDSD_PITCH;
2191 (*ppSurf)->surface_desc.u1.lPitch = IWineD3DSurface_GetPitch((*ppSurf)->WineD3DSurface);
2194 /* Application passed a color key? Set it! */
2195 if(pDDSD->dwFlags & DDSD_CKDESTOVERLAY)
2197 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2198 DDCKEY_DESTOVERLAY,
2199 (WINEDDCOLORKEY *) &pDDSD->u3.ddckCKDestOverlay);
2201 if(pDDSD->dwFlags & DDSD_CKDESTBLT)
2203 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2204 DDCKEY_DESTBLT,
2205 (WINEDDCOLORKEY *) &pDDSD->ddckCKDestBlt);
2207 if(pDDSD->dwFlags & DDSD_CKSRCOVERLAY)
2209 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2210 DDCKEY_SRCOVERLAY,
2211 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcOverlay);
2213 if(pDDSD->dwFlags & DDSD_CKSRCBLT)
2215 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2216 DDCKEY_SRCBLT,
2217 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcBlt);
2219 if ( pDDSD->dwFlags & DDSD_LPSURFACE)
2221 hr = IWineD3DSurface_SetMem((*ppSurf)->WineD3DSurface, pDDSD->lpSurface);
2222 if(hr != WINED3D_OK)
2224 /* No need for a trace here, wined3d does that for us */
2225 IDirectDrawSurface7_Release(ICOM_INTERFACE((*ppSurf), IDirectDrawSurface7));
2226 return hr;
2230 return DD_OK;
2232 /*****************************************************************************
2233 * CreateAdditionalSurfaces
2235 * Creates a new mipmap chain.
2237 * Params:
2238 * root: Root surface to attach the newly created chain to
2239 * count: number of surfaces to create
2240 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2241 * effects on the caller
2242 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2243 * creates an additional surface without the mipmapping flags
2245 *****************************************************************************/
2246 static HRESULT
2247 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2248 IDirectDrawSurfaceImpl *root,
2249 UINT count,
2250 DDSURFACEDESC2 DDSD,
2251 BOOL CubeFaceRoot)
2253 UINT i, j, level = 0;
2254 HRESULT hr;
2255 IDirectDrawSurfaceImpl *last = root;
2257 for(i = 0; i < count; i++)
2259 IDirectDrawSurfaceImpl *object2 = NULL;
2261 /* increase the mipmap level, but only if a mipmap is created
2262 * In this case, also halve the size
2264 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2266 level++;
2267 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2268 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2269 /* Set the mipmap sublevel flag according to msdn */
2270 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2272 else
2274 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2276 CubeFaceRoot = FALSE;
2278 hr = IDirectDrawImpl_CreateNewSurface(This,
2279 &DDSD,
2280 &object2,
2281 level);
2282 if(hr != DD_OK)
2284 return hr;
2287 /* Add the new surface to the complex attachment array */
2288 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2290 if(last->complex_array[j]) continue;
2291 last->complex_array[j] = object2;
2292 break;
2294 last = object2;
2296 /* Remove the (possible) back buffer cap from the new surface description,
2297 * because only one surface in the flipping chain is a back buffer, one
2298 * is a front buffer, the others are just primary surfaces.
2300 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2302 return DD_OK;
2305 /*****************************************************************************
2306 * IDirectDraw7::CreateSurface
2308 * Creates a new IDirectDrawSurface object and returns its interface.
2310 * The surface connections with wined3d are a bit tricky. Basically it works
2311 * like this:
2313 * |------------------------| |-----------------|
2314 * | DDraw surface | | WineD3DSurface |
2315 * | | | |
2316 * | WineD3DSurface |-------------->| |
2317 * | Child |<------------->| Parent |
2318 * |------------------------| |-----------------|
2320 * The DDraw surface is the parent of the wined3d surface, and it releases
2321 * the WineD3DSurface when the ddraw surface is destroyed.
2323 * However, for all surfaces which can be in a container in WineD3D,
2324 * we have to do this. These surfaces are usually complex surfaces,
2325 * so this concerns primary surfaces with a front and a back buffer,
2326 * and textures.
2328 * |------------------------| |-----------------|
2329 * | DDraw surface | | Container |
2330 * | | | |
2331 * | Child |<------------->| Parent |
2332 * | Texture |<------------->| |
2333 * | WineD3DSurface |<----| | Levels |<--|
2334 * | Complex connection | | | | |
2335 * |------------------------| | |-----------------| |
2336 * ^ | |
2337 * | | |
2338 * | | |
2339 * | |------------------| | |-----------------| |
2340 * | | IParent | |-------->| WineD3DSurface | |
2341 * | | | | | |
2342 * | | Child |<------------->| Parent | |
2343 * | | | | Container |<--|
2344 * | |------------------| |-----------------| |
2345 * | |
2346 * | |----------------------| |
2347 * | | DDraw surface 2 | |
2348 * | | | |
2349 * |<->| Complex root Child | |
2350 * | | Texture | |
2351 * | | WineD3DSurface |<----| |
2352 * | |----------------------| | |
2353 * | | |
2354 * | |---------------------| | |-----------------| |
2355 * | | IParent | |----->| WineD3DSurface | |
2356 * | | | | | |
2357 * | | Child |<---------->| Parent | |
2358 * | |---------------------| | Container |<--|
2359 * | |-----------------| |
2360 * | |
2361 * | ---More surfaces can follow--- |
2363 * The reason is that the IWineD3DSwapchain(render target container)
2364 * and the IWineD3DTexure(Texture container) release the parents
2365 * of their surface's children, but by releasing the complex root
2366 * the surfaces which are complexly attached to it are destroyed
2367 * too. See IDirectDrawSurface::Release for a more detailed
2368 * explanation.
2370 * Params:
2371 * DDSD: Description of the surface to create
2372 * Surf: Address to store the interface pointer at
2373 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2374 * aggregation, so it has to be NULL
2376 * Returns:
2377 * DD_OK on success
2378 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2379 * DDERR_* if an error occurs
2381 *****************************************************************************/
2382 static HRESULT WINAPI
2383 IDirectDrawImpl_CreateSurface(IDirectDraw7 *iface,
2384 DDSURFACEDESC2 *DDSD,
2385 IDirectDrawSurface7 **Surf,
2386 IUnknown *UnkOuter)
2388 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2389 IDirectDrawSurfaceImpl *object = NULL;
2390 HRESULT hr;
2391 LONG extra_surfaces = 0;
2392 DDSURFACEDESC2 desc2;
2393 WINED3DDISPLAYMODE Mode;
2395 TRACE("(%p)->(%p,%p,%p)\n", This, DDSD, Surf, UnkOuter);
2397 /* Some checks before we start */
2398 if (TRACE_ON(ddraw))
2400 TRACE(" (%p) Requesting surface desc :\n", This);
2401 DDRAW_dump_surface_desc(DDSD);
2403 EnterCriticalSection(&ddraw_cs);
2405 if (UnkOuter != NULL)
2407 FIXME("(%p) : outer != NULL?\n", This);
2408 LeaveCriticalSection(&ddraw_cs);
2409 return CLASS_E_NOAGGREGATION; /* unchecked */
2412 if (Surf == NULL)
2414 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", This);
2415 LeaveCriticalSection(&ddraw_cs);
2416 return E_POINTER; /* unchecked */
2419 if (!(DDSD->dwFlags & DDSD_CAPS))
2421 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2422 DDSD->dwFlags |= DDSD_CAPS;
2425 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2427 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2428 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2431 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2433 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2434 WARN("(%p) Null surface pointer specified, ignore it!\n", This);
2435 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2438 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2439 !(This->cooperative_level & DDSCL_EXCLUSIVE))
2441 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n", This);
2442 *Surf = NULL;
2443 LeaveCriticalSection(&ddraw_cs);
2444 return DDERR_NOEXCLUSIVEMODE;
2447 if(DDSD->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER)) {
2448 WARN("Application tried to create an explicit front or back buffer\n");
2449 LeaveCriticalSection(&ddraw_cs);
2450 return DDERR_INVALIDCAPS;
2452 /* Check cube maps but only if the size includes them */
2453 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2455 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2456 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2458 WARN("Cube map faces requested without cube map flag\n");
2459 LeaveCriticalSection(&ddraw_cs);
2460 return DDERR_INVALIDCAPS;
2462 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2463 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2465 WARN("Cube map without faces requested\n");
2466 LeaveCriticalSection(&ddraw_cs);
2467 return DDERR_INVALIDPARAMS;
2470 /* Quick tests confirm those can be created, but we don't do that yet */
2471 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2472 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2474 FIXME("Partial cube maps not supported yet\n");
2478 /* According to the msdn this flag is ignored by CreateSurface */
2479 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2480 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2482 /* Modify some flags */
2483 memset(&desc2, 0, sizeof(desc2));
2484 desc2.dwSize = sizeof(desc2); /* For the struct copy */
2485 DD_STRUCT_COPY_BYSIZE(&desc2, DDSD);
2486 desc2.dwSize = sizeof(desc2); /* To override a possibly smaller size */
2487 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2489 /* Get the video mode from WineD3D - we will need it */
2490 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
2491 0, /* Swapchain 0 */
2492 &Mode);
2493 if(FAILED(hr))
2495 ERR("Failed to read display mode from wined3d\n");
2496 switch(This->orig_bpp)
2498 case 8:
2499 Mode.Format = WINED3DFMT_P8;
2500 break;
2502 case 15:
2503 Mode.Format = WINED3DFMT_X1R5G5B5;
2504 break;
2506 case 16:
2507 Mode.Format = WINED3DFMT_R5G6B5;
2508 break;
2510 case 24:
2511 Mode.Format = WINED3DFMT_R8G8B8;
2512 break;
2514 case 32:
2515 Mode.Format = WINED3DFMT_X8R8G8B8;
2516 break;
2518 Mode.Width = This->orig_width;
2519 Mode.Height = This->orig_height;
2522 /* No pixelformat given? Use the current screen format */
2523 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2525 desc2.dwFlags |= DDSD_PIXELFORMAT;
2526 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2528 /* Wait: It could be a Z buffer */
2529 if(desc2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
2531 switch(desc2.u2.dwMipMapCount) /* Who had this glorious idea? */
2533 case 15:
2534 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D15S1);
2535 break;
2536 case 16:
2537 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D16);
2538 break;
2539 case 24:
2540 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D24X8);
2541 break;
2542 case 32:
2543 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D32);
2544 break;
2545 default:
2546 ERR("Unknown Z buffer bit depth\n");
2549 else
2551 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
2555 /* No Width or no Height? Use the original screen size
2557 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2558 !(desc2.dwFlags & DDSD_HEIGHT) )
2560 /* Invalid for non-render targets */
2561 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2563 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2564 *Surf = NULL;
2565 LeaveCriticalSection(&ddraw_cs);
2566 return DDERR_INVALIDPARAMS;
2569 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2570 desc2.dwWidth = Mode.Width;
2571 desc2.dwHeight = Mode.Height;
2574 /* Mipmap count fixes */
2575 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2577 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2579 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2581 /* Mipmap count is given, should not be 0 */
2582 if( desc2.u2.dwMipMapCount == 0 )
2584 LeaveCriticalSection(&ddraw_cs);
2585 return DDERR_INVALIDPARAMS;
2588 else
2590 /* Undocumented feature: Create sublevels until
2591 * either the width or the height is 1
2593 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2594 desc2.dwWidth : desc2.dwHeight;
2595 desc2.u2.dwMipMapCount = 0;
2596 while( min )
2598 desc2.u2.dwMipMapCount += 1;
2599 min >>= 1;
2603 else
2605 /* Not-complex mipmap -> Mipmapcount = 1 */
2606 desc2.u2.dwMipMapCount = 1;
2608 extra_surfaces = desc2.u2.dwMipMapCount - 1;
2610 /* There's a mipmap count in the created surface in any case */
2611 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2613 /* If no mipmap is given, the texture has only one level */
2615 /* The first surface is a front buffer, the back buffer is created afterwards */
2616 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2618 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2621 /* The root surface in a cube map is positive x */
2622 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2624 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2625 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2628 /* Create the first surface */
2629 hr = IDirectDrawImpl_CreateNewSurface(This, &desc2, &object, 0);
2630 if( hr != DD_OK)
2632 ERR("IDirectDrawImpl_CreateNewSurface failed with %08x\n", hr);
2633 LeaveCriticalSection(&ddraw_cs);
2634 return hr;
2636 object->is_complex_root = TRUE;
2638 *Surf = ICOM_INTERFACE(object, IDirectDrawSurface7);
2640 /* Create Additional surfaces if necessary
2641 * This applies to Primary surfaces which have a back buffer count
2642 * set, but not to mipmap textures. In case of Mipmap textures,
2643 * wineD3D takes care of the creation of additional surfaces
2645 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2647 extra_surfaces = DDSD->dwBackBufferCount;
2648 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2649 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2652 hr = DD_OK;
2653 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2655 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2656 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
2657 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2658 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
2659 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
2660 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2661 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
2662 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
2663 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2664 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
2665 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
2666 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2667 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
2668 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
2669 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2670 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
2671 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2674 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces, desc2, FALSE);
2675 if(hr != DD_OK)
2677 /* This destroys and possibly created surfaces too */
2678 IDirectDrawSurface_Release( ICOM_INTERFACE(object, IDirectDrawSurface7) );
2679 LeaveCriticalSection(&ddraw_cs);
2680 return hr;
2683 /* If the implementation is OpenGL and there's no d3ddevice, attach a d3ddevice
2684 * But attach the d3ddevice only if the currently created surface was
2685 * a primary surface (2D app in 3D mode) or a 3DDEVICE surface (3D app)
2686 * The only case I can think of where this doesn't apply is when a
2687 * 2D app was configured by the user to run with OpenGL and it didn't create
2688 * the render target as first surface. In this case the render target creation
2689 * will cause the 3D init.
2691 if( (This->ImplType == SURFACE_OPENGL) && !(This->d3d_initialized) &&
2692 desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE) )
2694 IDirectDrawSurfaceImpl *target = object, *surface;
2695 struct list *entry;
2697 /* Search for the primary to use as render target */
2698 LIST_FOR_EACH(entry, &This->surface_list)
2700 surface = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2701 if((surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER)) ==
2702 (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER))
2704 /* found */
2705 target = surface;
2706 TRACE("Using primary %p as render target\n", target);
2707 break;
2711 TRACE("(%p) Attaching a D3DDevice, rendertarget = %p\n", This, target);
2712 hr = IDirectDrawImpl_AttachD3DDevice(This, target);
2713 if(hr != D3D_OK)
2715 IDirectDrawSurfaceImpl *release_surf;
2716 ERR("IDirectDrawImpl_AttachD3DDevice failed, hr = %x\n", hr);
2717 *Surf = NULL;
2719 /* The before created surface structures are in an incomplete state here.
2720 * WineD3D holds the reference on the IParents, and it released them on the failure
2721 * already. So the regular release method implementation would fail on the attempt
2722 * to destroy either the IParents or the swapchain. So free the surface here.
2723 * The surface structure here is a list, not a tree, because onscreen targets
2724 * cannot be cube textures
2726 while(object)
2728 release_surf = object;
2729 object = object->complex_array[0];
2730 IDirectDrawSurfaceImpl_Destroy(release_surf);
2732 LeaveCriticalSection(&ddraw_cs);
2733 return hr;
2735 } else if(!(This->d3d_initialized) && desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
2736 IDirectDrawImpl_CreateGDISwapChain(This, object);
2739 /* Addref the ddraw interface to keep an reference for each surface */
2740 IDirectDraw7_AddRef(iface);
2741 object->ifaceToRelease = (IUnknown *) iface;
2743 /* Create a WineD3DTexture if a texture was requested */
2744 if(desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
2746 UINT levels;
2747 WINED3DFORMAT Format;
2748 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
2750 This->tex_root = object;
2752 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2754 /* a mipmap is created, create enough levels */
2755 levels = desc2.u2.dwMipMapCount;
2757 else
2759 /* No mipmap is created, create one level */
2760 levels = 1;
2763 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM */
2764 if(DDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
2766 Pool = WINED3DPOOL_SYSTEMMEM;
2768 /* Should I forward the MANAGED cap to the managed pool ? */
2770 /* Get the format. It's set already by CreateNewSurface */
2771 Format = PixelFormat_DD2WineD3D(&object->surface_desc.u4.ddpfPixelFormat);
2773 /* The surfaces are already created, the callback only
2774 * passes the IWineD3DSurface to WineD3D
2776 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2778 hr = IWineD3DDevice_CreateCubeTexture(This->wineD3DDevice,
2779 DDSD->dwWidth, /* Edgelength */
2780 levels,
2781 0, /* usage */
2782 Format,
2783 Pool,
2784 (IWineD3DCubeTexture **) &object->wineD3DTexture,
2785 0, /* SharedHandle */
2786 (IUnknown *) ICOM_INTERFACE(object, IDirectDrawSurface7),
2787 D3D7CB_CreateSurface);
2789 else
2791 hr = IWineD3DDevice_CreateTexture(This->wineD3DDevice,
2792 DDSD->dwWidth, DDSD->dwHeight,
2793 levels, /* MipMapCount = Levels */
2794 0, /* usage */
2795 Format,
2796 Pool,
2797 (IWineD3DTexture **) &object->wineD3DTexture,
2798 0, /* SharedHandle */
2799 (IUnknown *) ICOM_INTERFACE(object, IDirectDrawSurface7),
2800 D3D7CB_CreateSurface );
2802 This->tex_root = NULL;
2805 LeaveCriticalSection(&ddraw_cs);
2806 return hr;
2809 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
2810 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
2812 static BOOL
2813 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
2814 const DDPIXELFORMAT *provided)
2816 /* Some flags must be present in both or neither for a match. */
2817 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
2818 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
2819 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
2821 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2822 return FALSE;
2824 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
2825 return FALSE;
2827 if (requested->dwFlags & DDPF_FOURCC)
2828 if (requested->dwFourCC != provided->dwFourCC)
2829 return FALSE;
2831 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
2832 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2833 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
2834 return FALSE;
2836 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2837 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2838 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
2839 return FALSE;
2841 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
2842 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
2843 return FALSE;
2845 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
2846 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2847 |DDPF_BUMPDUDV))
2848 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
2849 return FALSE;
2851 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
2852 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
2853 return FALSE;
2855 return TRUE;
2858 static BOOL
2859 IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested,
2860 const DDSURFACEDESC2* provided)
2862 struct compare_info
2864 DWORD flag;
2865 ptrdiff_t offset;
2866 size_t size;
2869 #define CMP(FLAG, FIELD) \
2870 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
2871 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
2873 static const struct compare_info compare[] =
2875 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
2876 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
2877 CMP(CAPS, ddsCaps),
2878 CMP(CKDESTBLT, ddckCKDestBlt),
2879 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
2880 CMP(CKSRCBLT, ddckCKSrcBlt),
2881 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
2882 CMP(HEIGHT, dwHeight),
2883 CMP(LINEARSIZE, u1 /* dwLinearSize */),
2884 CMP(LPSURFACE, lpSurface),
2885 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
2886 CMP(PITCH, u1 /* lPitch */),
2887 /* PIXELFORMAT: manual */
2888 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
2889 CMP(TEXTURESTAGE, dwTextureStage),
2890 CMP(WIDTH, dwWidth),
2891 /* ZBUFFERBITDEPTH: "obsolete" */
2894 #undef CMP
2896 unsigned int i;
2898 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2899 return FALSE;
2901 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
2903 if (requested->dwFlags & compare[i].flag
2904 && memcmp((const char *)provided + compare[i].offset,
2905 (const char *)requested + compare[i].offset,
2906 compare[i].size) != 0)
2907 return FALSE;
2910 if (requested->dwFlags & DDSD_PIXELFORMAT)
2912 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
2913 &provided->u4.ddpfPixelFormat))
2914 return FALSE;
2917 return TRUE;
2920 #undef DDENUMSURFACES_SEARCHTYPE
2921 #undef DDENUMSURFACES_MATCHTYPE
2923 /*****************************************************************************
2924 * IDirectDraw7::EnumSurfaces
2926 * Loops through all surfaces attached to this device and calls the
2927 * application callback. This can't be relayed to WineD3DDevice,
2928 * because some WineD3DSurfaces' parents are IParent objects
2930 * Params:
2931 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
2932 * DDSD: Description to filter for
2933 * Context: Application-provided pointer, it's passed unmodified to the
2934 * Callback function
2935 * Callback: Address to call for each surface
2937 * Returns:
2938 * DDERR_INVALIDPARAMS if the callback is NULL
2939 * DD_OK on success
2941 *****************************************************************************/
2942 static HRESULT WINAPI
2943 IDirectDrawImpl_EnumSurfaces(IDirectDraw7 *iface,
2944 DWORD Flags,
2945 DDSURFACEDESC2 *DDSD,
2946 void *Context,
2947 LPDDENUMSURFACESCALLBACK7 Callback)
2949 /* The surface enumeration is handled by WineDDraw,
2950 * because it keeps track of all surfaces attached to
2951 * it. The filtering is done by our callback function,
2952 * because WineDDraw doesn't handle ddraw-like surface
2953 * caps structures
2955 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2956 IDirectDrawSurfaceImpl *surf;
2957 BOOL all, nomatch;
2958 DDSURFACEDESC2 desc;
2959 struct list *entry, *entry2;
2961 all = Flags & DDENUMSURFACES_ALL;
2962 nomatch = Flags & DDENUMSURFACES_NOMATCH;
2964 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, DDSD, Context, Callback);
2965 EnterCriticalSection(&ddraw_cs);
2967 if(!Callback)
2969 LeaveCriticalSection(&ddraw_cs);
2970 return DDERR_INVALIDPARAMS;
2973 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
2974 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
2976 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2977 if (all || (nomatch != IDirectDrawImpl_DDSD_Match(DDSD, &surf->surface_desc)))
2979 desc = surf->surface_desc;
2980 IDirectDrawSurface7_AddRef(ICOM_INTERFACE(surf, IDirectDrawSurface7));
2981 if(Callback( ICOM_INTERFACE(surf, IDirectDrawSurface7), &desc, Context) != DDENUMRET_OK)
2983 LeaveCriticalSection(&ddraw_cs);
2984 return DD_OK;
2988 LeaveCriticalSection(&ddraw_cs);
2989 return DD_OK;
2992 static HRESULT WINAPI
2993 findRenderTarget(IDirectDrawSurface7 *surface,
2994 DDSURFACEDESC2 *desc,
2995 void *ctx)
2997 IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, surface);
2998 IDirectDrawSurfaceImpl **target = (IDirectDrawSurfaceImpl **) ctx;
3000 if(!surf->isRenderTarget) {
3001 *target = surf;
3002 IDirectDrawSurface7_Release(surface);
3003 return DDENUMRET_CANCEL;
3006 /* Recurse into the surface tree */
3007 IDirectDrawSurface7_EnumAttachedSurfaces(surface, ctx, findRenderTarget);
3009 IDirectDrawSurface7_Release(surface);
3010 if(*target) return DDENUMRET_CANCEL;
3011 else return DDENUMRET_OK; /* Continue with the next neighbor surface */
3014 /*****************************************************************************
3015 * D3D7CB_CreateRenderTarget
3017 * Callback called by WineD3D to create Surfaces for render target usage
3018 * This function takes the D3D target from the IDirectDrawImpl structure,
3019 * and returns the WineD3DSurface. To avoid double usage, the surface
3020 * is marked as render target afterwards
3022 * Params
3023 * device: The WineD3DDevice's parent
3024 * Width, Height, Format: Dimensions and pixelformat of the render target
3025 * Ignored, because the surface already exists
3026 * MultiSample, MultisampleQuality, Lockable: Ignored for the same reason
3027 * Lockable: ignored
3028 * ppSurface: Address to pass the surface pointer back at
3029 * pSharedHandle: Ignored
3031 * Returns:
3032 * Always returns D3D_OK
3034 *****************************************************************************/
3035 static HRESULT WINAPI
3036 D3D7CB_CreateRenderTarget(IUnknown *device, IUnknown *pSuperior,
3037 UINT Width, UINT Height,
3038 WINED3DFORMAT Format,
3039 WINED3DMULTISAMPLE_TYPE MultiSample,
3040 DWORD MultisampleQuality,
3041 BOOL Lockable,
3042 IWineD3DSurface** ppSurface,
3043 HANDLE* pSharedHandle)
3045 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
3046 IDirectDrawSurfaceImpl *d3dSurface = This->d3d_target, *target = NULL;
3047 TRACE("(%p) call back\n", device);
3049 if(d3dSurface->isRenderTarget)
3051 IDirectDrawSurface7_EnumAttachedSurfaces(ICOM_INTERFACE(d3dSurface, IDirectDrawSurface7),
3052 &target, findRenderTarget);
3054 else
3056 target = d3dSurface;
3059 if(!target)
3061 target = This->d3d_target;
3062 ERR(" (%p) : No DirectDrawSurface found to create the back buffer. Using the front buffer as back buffer. Uncertain consequences\n", This);
3065 /* TODO: Return failure if the dimensions do not match, but this shouldn't happen */
3067 *ppSurface = target->WineD3DSurface;
3068 target->isRenderTarget = TRUE;
3069 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *ppSurface, d3dSurface);
3070 return D3D_OK;
3073 static HRESULT WINAPI
3074 D3D7CB_CreateDepthStencilSurface(IUnknown *device,
3075 IUnknown *pSuperior,
3076 UINT Width,
3077 UINT Height,
3078 WINED3DFORMAT Format,
3079 WINED3DMULTISAMPLE_TYPE MultiSample,
3080 DWORD MultisampleQuality,
3081 BOOL Discard,
3082 IWineD3DSurface** ppSurface,
3083 HANDLE* pSharedHandle)
3085 /* Create a Depth Stencil surface to make WineD3D happy */
3086 HRESULT hr = D3D_OK;
3087 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
3088 DDSURFACEDESC2 ddsd;
3090 TRACE("(%p) call back\n", device);
3092 *ppSurface = NULL;
3094 /* Create a DirectDraw surface */
3095 memset(&ddsd, 0, sizeof(ddsd));
3096 ddsd.dwSize = sizeof(ddsd);
3097 ddsd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
3098 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
3099 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
3100 ddsd.dwHeight = Height;
3101 ddsd.dwWidth = Width;
3102 if(Format != 0)
3104 PixelFormat_WineD3DtoDD(&ddsd.u4.ddpfPixelFormat, Format);
3106 else
3108 ddsd.dwFlags ^= DDSD_PIXELFORMAT;
3111 This->depthstencil = TRUE;
3112 hr = IDirectDraw7_CreateSurface((IDirectDraw7 *) This,
3113 &ddsd,
3114 (IDirectDrawSurface7 **) &This->DepthStencilBuffer,
3115 NULL);
3116 This->depthstencil = FALSE;
3117 if(FAILED(hr))
3119 ERR(" (%p) Creating a DepthStencil Surface failed, result = %x\n", This, hr);
3120 return hr;
3122 *ppSurface = This->DepthStencilBuffer->WineD3DSurface;
3123 return D3D_OK;
3126 /*****************************************************************************
3127 * D3D7CB_CreateAdditionalSwapChain
3129 * Callback function for WineD3D which creates a new WineD3DSwapchain
3130 * interface. It also creates an IParent interface to store that pointer,
3131 * so the WineD3DSwapchain has a parent and can be released when the D3D
3132 * device is destroyed
3133 *****************************************************************************/
3134 static HRESULT WINAPI
3135 D3D7CB_CreateAdditionalSwapChain(IUnknown *device,
3136 WINED3DPRESENT_PARAMETERS* pPresentationParameters,
3137 IWineD3DSwapChain ** ppSwapChain)
3139 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
3140 IParentImpl *object = NULL;
3141 HRESULT res = D3D_OK;
3142 IWineD3DSwapChain *swapchain;
3143 IDirectDrawSurfaceImpl *iterator;
3144 TRACE("(%p) call back\n", device);
3146 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IParentImpl));
3147 if (NULL == object)
3149 FIXME("Allocation of memory failed\n");
3150 *ppSwapChain = NULL;
3151 return DDERR_OUTOFVIDEOMEMORY;
3154 ICOM_INIT_INTERFACE(object, IParent, IParent_Vtbl);
3155 object->ref = 1;
3157 res = IWineD3DDevice_CreateAdditionalSwapChain(This->wineD3DDevice,
3158 pPresentationParameters,
3159 &swapchain,
3160 (IUnknown*) ICOM_INTERFACE(object, IParent),
3161 D3D7CB_CreateRenderTarget,
3162 D3D7CB_CreateDepthStencilSurface,
3163 This->ImplType);
3164 if (res != D3D_OK)
3166 FIXME("(%p) call to IWineD3DDevice_CreateAdditionalSwapChain failed\n", This);
3167 HeapFree(GetProcessHeap(), 0 , object);
3168 *ppSwapChain = NULL;
3170 else
3172 *ppSwapChain = swapchain;
3173 object->child = (IUnknown *) swapchain;
3174 This->d3d_target->wineD3DSwapChain = swapchain;
3175 iterator = This->d3d_target->complex_array[0];
3176 while(iterator) {
3177 iterator->wineD3DSwapChain = swapchain;
3178 iterator = iterator->complex_array[0];
3182 return res;
3185 static HRESULT WINAPI IDirectDrawImpl_CreateGDISwapChain(IDirectDrawImpl *This,
3186 IDirectDrawSurfaceImpl *primary) {
3187 HRESULT hr;
3188 WINED3DPRESENT_PARAMETERS presentation_parameters;
3189 HWND window;
3191 window = This->dest_window;
3193 memset(&presentation_parameters, 0, sizeof(presentation_parameters));
3195 /* Use the surface description for the device parameters, not the
3196 * Device settings. The app might render to an offscreen surface
3198 presentation_parameters.BackBufferWidth = primary->surface_desc.dwWidth;
3199 presentation_parameters.BackBufferHeight = primary->surface_desc.dwHeight;
3200 presentation_parameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
3201 presentation_parameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
3202 presentation_parameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
3203 presentation_parameters.MultiSampleQuality = 0;
3204 presentation_parameters.SwapEffect = WINED3DSWAPEFFECT_FLIP;
3205 presentation_parameters.hDeviceWindow = window;
3206 presentation_parameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
3207 presentation_parameters.EnableAutoDepthStencil = FALSE; /* Not on GDI swapchains */
3208 presentation_parameters.AutoDepthStencilFormat = 0;
3209 presentation_parameters.Flags = 0;
3210 presentation_parameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
3211 presentation_parameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
3213 This->d3d_target = primary;
3214 hr = IWineD3DDevice_InitGDI(This->wineD3DDevice,
3215 &presentation_parameters,
3216 D3D7CB_CreateAdditionalSwapChain);
3217 This->d3d_target = NULL;
3219 if (hr != D3D_OK)
3221 FIXME("(%p) call to IWineD3DDevice_CreateAdditionalSwapChain failed\n", This);
3222 primary->wineD3DSwapChain = NULL;
3224 return hr;
3227 /*****************************************************************************
3228 * IDirectDrawImpl_AttachD3DDevice
3230 * Initializes the D3D capabilities of WineD3D
3232 * Params:
3233 * primary: The primary surface for D3D
3235 * Returns
3236 * DD_OK on success,
3237 * DDERR_* otherwise
3239 *****************************************************************************/
3240 static HRESULT WINAPI
3241 IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This,
3242 IDirectDrawSurfaceImpl *primary)
3244 HRESULT hr;
3245 HWND window = This->dest_window;
3247 WINED3DPRESENT_PARAMETERS localParameters;
3249 TRACE("(%p)->(%p)\n", This, primary);
3251 /* If there's no window, create a hidden window. WineD3D needs it */
3252 if(window == 0)
3254 window = CreateWindowExA(0, This->classname, "Hidden D3D Window",
3255 WS_DISABLED, 0, 0,
3256 GetSystemMetrics(SM_CXSCREEN),
3257 GetSystemMetrics(SM_CYSCREEN),
3258 NULL, NULL, GetModuleHandleA(0), NULL);
3260 ShowWindow(window, SW_HIDE); /* Just to be sure */
3261 WARN("(%p) No window for the Direct3DDevice, created a hidden window. HWND=%p\n", This, window);
3262 This->d3d_window = window;
3264 else
3266 TRACE("(%p) Using existing window %p for Direct3D rendering\n", This, window);
3269 /* Store the future Render Target surface */
3270 This->d3d_target = primary;
3272 /* Use the surface description for the device parameters, not the
3273 * Device settings. The app might render to an offscreen surface
3275 localParameters.BackBufferWidth = primary->surface_desc.dwWidth;
3276 localParameters.BackBufferHeight = primary->surface_desc.dwHeight;
3277 localParameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
3278 localParameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
3279 localParameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
3280 localParameters.MultiSampleQuality = 0;
3281 localParameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
3282 localParameters.hDeviceWindow = window;
3283 localParameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
3284 localParameters.EnableAutoDepthStencil = TRUE;
3285 localParameters.AutoDepthStencilFormat = WINED3DFMT_D16;
3286 localParameters.Flags = 0;
3287 localParameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
3288 localParameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
3290 TRACE("Passing mode %d\n", localParameters.BackBufferFormat);
3292 /* Set this NOW, otherwise creating the depth stencil surface will cause a
3293 * recursive loop until ram or emulated video memory is full
3295 This->d3d_initialized = TRUE;
3297 hr = IWineD3DDevice_Init3D(This->wineD3DDevice,
3298 &localParameters,
3299 D3D7CB_CreateAdditionalSwapChain);
3300 if(FAILED(hr))
3302 This->d3d_target = NULL;
3303 This->d3d_initialized = FALSE;
3304 return hr;
3307 This->declArraySize = 2;
3308 This->decls = HeapAlloc(GetProcessHeap(),
3309 HEAP_ZERO_MEMORY,
3310 sizeof(*This->decls) * This->declArraySize);
3311 if(!This->decls)
3313 ERR("Error allocating an array for the converted vertex decls\n");
3314 This->declArraySize = 0;
3315 hr = IWineD3DDevice_Uninit3D(This->wineD3DDevice,
3316 D3D7CB_DestroyDepthStencilSurface,
3317 D3D7CB_DestroySwapChain);
3318 return E_OUTOFMEMORY;
3321 /* Create an Index Buffer parent */
3322 TRACE("(%p) Successfully initialized 3D\n", This);
3323 return DD_OK;
3326 /*****************************************************************************
3327 * DirectDrawCreateClipper (DDRAW.@)
3329 * Creates a new IDirectDrawClipper object.
3331 * Params:
3332 * Clipper: Address to write the interface pointer to
3333 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3334 * NULL
3336 * Returns:
3337 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3338 * E_OUTOFMEMORY if allocating the object failed
3340 *****************************************************************************/
3341 HRESULT WINAPI
3342 DirectDrawCreateClipper(DWORD Flags,
3343 LPDIRECTDRAWCLIPPER *Clipper,
3344 IUnknown *UnkOuter)
3346 IDirectDrawClipperImpl* object;
3347 TRACE("(%08x,%p,%p)\n", Flags, Clipper, UnkOuter);
3349 EnterCriticalSection(&ddraw_cs);
3350 if (UnkOuter != NULL)
3352 LeaveCriticalSection(&ddraw_cs);
3353 return CLASS_E_NOAGGREGATION;
3356 if (!LoadWineD3D())
3358 LeaveCriticalSection(&ddraw_cs);
3359 return DDERR_NODIRECTDRAWSUPPORT;
3362 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3363 sizeof(IDirectDrawClipperImpl));
3364 if (object == NULL)
3366 LeaveCriticalSection(&ddraw_cs);
3367 return E_OUTOFMEMORY;
3370 ICOM_INIT_INTERFACE(object, IDirectDrawClipper, IDirectDrawClipper_Vtbl);
3371 object->ref = 1;
3372 object->wineD3DClipper = pWineDirect3DCreateClipper((IUnknown *) object);
3373 if(!object->wineD3DClipper)
3375 HeapFree(GetProcessHeap(), 0, object);
3376 LeaveCriticalSection(&ddraw_cs);
3377 return E_OUTOFMEMORY;
3380 *Clipper = (IDirectDrawClipper *) object;
3381 LeaveCriticalSection(&ddraw_cs);
3382 return DD_OK;
3385 /*****************************************************************************
3386 * IDirectDraw7::CreateClipper
3388 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3390 *****************************************************************************/
3391 static HRESULT WINAPI
3392 IDirectDrawImpl_CreateClipper(IDirectDraw7 *iface,
3393 DWORD Flags,
3394 IDirectDrawClipper **Clipper,
3395 IUnknown *UnkOuter)
3397 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3398 TRACE("(%p)->(%x,%p,%p)\n", This, Flags, Clipper, UnkOuter);
3399 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3402 /*****************************************************************************
3403 * IDirectDraw7::CreatePalette
3405 * Creates a new IDirectDrawPalette object
3407 * Params:
3408 * Flags: The flags for the new clipper
3409 * ColorTable: Color table to assign to the new clipper
3410 * Palette: Address to write the interface pointer to
3411 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3412 * NULL
3414 * Returns:
3415 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3416 * E_OUTOFMEMORY if allocating the object failed
3418 *****************************************************************************/
3419 static HRESULT WINAPI
3420 IDirectDrawImpl_CreatePalette(IDirectDraw7 *iface,
3421 DWORD Flags,
3422 PALETTEENTRY *ColorTable,
3423 IDirectDrawPalette **Palette,
3424 IUnknown *pUnkOuter)
3426 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3427 IDirectDrawPaletteImpl *object;
3428 HRESULT hr = DDERR_GENERIC;
3429 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, ColorTable, Palette, pUnkOuter);
3431 EnterCriticalSection(&ddraw_cs);
3432 if(pUnkOuter != NULL)
3434 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
3435 LeaveCriticalSection(&ddraw_cs);
3436 return CLASS_E_NOAGGREGATION;
3439 /* The refcount test shows that a cooplevel is required for this */
3440 if(!This->cooperative_level)
3442 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3443 LeaveCriticalSection(&ddraw_cs);
3444 return DDERR_NOCOOPERATIVELEVELSET;
3447 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
3448 if(!object)
3450 ERR("Out of memory when allocating memory for a palette implementation\n");
3451 LeaveCriticalSection(&ddraw_cs);
3452 return E_OUTOFMEMORY;
3455 ICOM_INIT_INTERFACE(object, IDirectDrawPalette, IDirectDrawPalette_Vtbl);
3456 object->ref = 1;
3457 object->ddraw_owner = This;
3459 hr = IWineD3DDevice_CreatePalette(This->wineD3DDevice, Flags, ColorTable, &object->wineD3DPalette, (IUnknown *) ICOM_INTERFACE(object, IDirectDrawPalette) );
3460 if(hr != DD_OK)
3462 HeapFree(GetProcessHeap(), 0, object);
3463 LeaveCriticalSection(&ddraw_cs);
3464 return hr;
3467 IDirectDraw7_AddRef(iface);
3468 object->ifaceToRelease = (IUnknown *) iface;
3469 *Palette = ICOM_INTERFACE(object, IDirectDrawPalette);
3470 LeaveCriticalSection(&ddraw_cs);
3471 return DD_OK;
3474 /*****************************************************************************
3475 * IDirectDraw7::DuplicateSurface
3477 * Duplicates a surface. The surface memory points to the same memory as
3478 * the original surface, and it's released when the last surface referencing
3479 * it is released. I guess that's beyond Wine's surface management right now
3480 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3481 * test application to implement this)
3483 * Params:
3484 * Src: Address of the source surface
3485 * Dest: Address to write the new surface pointer to
3487 * Returns:
3488 * See IDirectDraw7::CreateSurface
3490 *****************************************************************************/
3491 static HRESULT WINAPI
3492 IDirectDrawImpl_DuplicateSurface(IDirectDraw7 *iface,
3493 IDirectDrawSurface7 *Src,
3494 IDirectDrawSurface7 **Dest)
3496 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3497 IDirectDrawSurfaceImpl *Surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Src);
3499 FIXME("(%p)->(%p,%p)\n", This, Surf, Dest);
3501 /* For now, simply create a new, independent surface */
3502 return IDirectDraw7_CreateSurface(iface,
3503 &Surf->surface_desc,
3504 Dest,
3505 NULL);
3508 /*****************************************************************************
3509 * IDirectDraw7 VTable
3510 *****************************************************************************/
3511 const IDirectDraw7Vtbl IDirectDraw7_Vtbl =
3513 /*** IUnknown ***/
3514 IDirectDrawImpl_QueryInterface,
3515 IDirectDrawImpl_AddRef,
3516 IDirectDrawImpl_Release,
3517 /*** IDirectDraw ***/
3518 IDirectDrawImpl_Compact,
3519 IDirectDrawImpl_CreateClipper,
3520 IDirectDrawImpl_CreatePalette,
3521 IDirectDrawImpl_CreateSurface,
3522 IDirectDrawImpl_DuplicateSurface,
3523 IDirectDrawImpl_EnumDisplayModes,
3524 IDirectDrawImpl_EnumSurfaces,
3525 IDirectDrawImpl_FlipToGDISurface,
3526 IDirectDrawImpl_GetCaps,
3527 IDirectDrawImpl_GetDisplayMode,
3528 IDirectDrawImpl_GetFourCCCodes,
3529 IDirectDrawImpl_GetGDISurface,
3530 IDirectDrawImpl_GetMonitorFrequency,
3531 IDirectDrawImpl_GetScanLine,
3532 IDirectDrawImpl_GetVerticalBlankStatus,
3533 IDirectDrawImpl_Initialize,
3534 IDirectDrawImpl_RestoreDisplayMode,
3535 IDirectDrawImpl_SetCooperativeLevel,
3536 IDirectDrawImpl_SetDisplayMode,
3537 IDirectDrawImpl_WaitForVerticalBlank,
3538 /*** IDirectDraw2 ***/
3539 IDirectDrawImpl_GetAvailableVidMem,
3540 /*** IDirectDraw7 ***/
3541 IDirectDrawImpl_GetSurfaceFromDC,
3542 IDirectDrawImpl_RestoreAllSurfaces,
3543 IDirectDrawImpl_TestCooperativeLevel,
3544 IDirectDrawImpl_GetDeviceIdentifier,
3545 /*** IDirectDraw7 ***/
3546 IDirectDrawImpl_StartModeTest,
3547 IDirectDrawImpl_EvaluateMode
3550 /*****************************************************************************
3551 * IDirectDrawImpl_FindDecl
3553 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
3554 * if none was found.
3556 * This function is in ddraw.c and the DDraw object space because D3D7
3557 * vertex buffers are created using the IDirect3D interface to the ddraw
3558 * object, so they can be valid across D3D devices(theoretically. The ddraw
3559 * object also owns the wined3d device
3561 * Parameters:
3562 * This: Device
3563 * fvf: Fvf to find the decl for
3565 * Returns:
3566 * NULL in case of an error, the IWineD3DVertexDeclaration interface for the
3567 * fvf otherwise.
3569 *****************************************************************************/
3570 IWineD3DVertexDeclaration *
3571 IDirectDrawImpl_FindDecl(IDirectDrawImpl *This,
3572 DWORD fvf)
3574 HRESULT hr;
3575 IWineD3DVertexDeclaration* pDecl = NULL;
3576 int p, low, high; /* deliberately signed */
3577 struct FvfToDecl *convertedDecls = This->decls;
3579 TRACE("Searching for declaration for fvf %08x... ", fvf);
3581 low = 0;
3582 high = This->numConvertedDecls - 1;
3583 while(low <= high) {
3584 p = (low + high) >> 1;
3585 TRACE("%d ", p);
3586 if(convertedDecls[p].fvf == fvf) {
3587 TRACE("found %p\n", convertedDecls[p].decl);
3588 return convertedDecls[p].decl;
3589 } else if(convertedDecls[p].fvf < fvf) {
3590 low = p + 1;
3591 } else {
3592 high = p - 1;
3595 TRACE("not found. Creating and inserting at position %d.\n", low);
3597 hr = IWineD3DDevice_CreateVertexDeclarationFromFVF(This->wineD3DDevice,
3598 &pDecl,
3599 (IUnknown *) ICOM_INTERFACE(This, IDirectDraw7),
3600 fvf);
3601 if (hr != S_OK) return NULL;
3603 if(This->declArraySize == This->numConvertedDecls) {
3604 int grow = max(This->declArraySize / 2, 8);
3605 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
3606 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
3607 if(!convertedDecls) {
3608 /* This will destroy it */
3609 IWineD3DVertexDeclaration_Release(pDecl);
3610 return NULL;
3612 This->decls = convertedDecls;
3613 This->declArraySize += grow;
3616 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
3617 convertedDecls[low].decl = pDecl;
3618 convertedDecls[low].fvf = fvf;
3619 This->numConvertedDecls++;
3621 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
3622 return pDecl;