shlwapi: Update German translation.
[wine/multimedia.git] / dlls / ddraw / ddraw.c
blobbd96418a5784525c21ec62fc88c559a1e53553eb
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 IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This, IDirectDrawSurfaceImpl *primary);
50 static HRESULT IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This, DDSURFACEDESC2 *pDDSD, IDirectDrawSurfaceImpl **ppSurf, UINT level);
51 static HRESULT 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 IDirectDrawImpl *This = (IDirectDrawImpl *)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 = This;
115 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
117 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
119 *obj = &This->IDirectDraw4_vtbl;
120 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
122 else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
124 /* This Interface exists in ddrawex.dll, it is implemented in a wrapper */
125 WARN("IDirectDraw3 is not valid in ddraw.dll\n");
126 *obj = NULL;
127 LeaveCriticalSection(&ddraw_cs);
128 return E_NOINTERFACE;
130 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
132 *obj = &This->IDirectDraw2_vtbl;
133 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
135 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
137 *obj = &This->IDirectDraw_vtbl;
138 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
141 /* Direct3D
142 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
143 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
144 * who had this idea and why. The older interfaces can query and IDirect3D version
145 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
146 * and messy to implement with the common creation function, so it has been left out here.
148 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
149 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
150 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
151 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
153 /* Check the surface implementation */
154 if(This->ImplType == SURFACE_UNKNOWN)
156 /* Apps may create the IDirect3D Interface before the primary surface.
157 * set the surface implementation */
158 This->ImplType = SURFACE_OPENGL;
159 TRACE("(%p) Choosing OpenGL surfaces because a Direct3D interface was requested\n", This);
161 else if(This->ImplType != SURFACE_OPENGL && DefaultSurfaceType == SURFACE_UNKNOWN)
163 ERR("(%p) The App is requesting a D3D device, but a non-OpenGL surface type was choosen. Prepare for trouble!\n", This);
164 ERR(" (%p) You may want to contact wine-devel for help\n", This);
165 /* Should I assert(0) here??? */
167 else if(This->ImplType != SURFACE_OPENGL)
169 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
170 /* Do not abort here, only reject 3D Device creation */
173 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
175 This->d3dversion = 1;
176 *obj = &This->IDirect3D_vtbl;
177 TRACE(" returning Direct3D interface at %p.\n", *obj);
179 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
181 This->d3dversion = 2;
182 *obj = &This->IDirect3D2_vtbl;
183 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
185 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
187 This->d3dversion = 3;
188 *obj = &This->IDirect3D3_vtbl;
189 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
191 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
193 This->d3dversion = 7;
194 *obj = &This->IDirect3D7_vtbl;
195 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
198 else if (IsEqualGUID(refiid, &IID_IWineD3DDeviceParent))
200 *obj = &This->device_parent_vtbl;
203 /* Unknown interface */
204 else
206 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
207 LeaveCriticalSection(&ddraw_cs);
208 return E_NOINTERFACE;
211 IUnknown_AddRef( (IUnknown *) *obj );
212 LeaveCriticalSection(&ddraw_cs);
213 return S_OK;
216 /*****************************************************************************
217 * IDirectDraw7::AddRef
219 * Increases the interfaces refcount, basically
221 * DDraw refcounting is a bit tricky. The different DirectDraw interface
222 * versions have individual refcounts, but the IDirect3D interfaces do not.
223 * All interfaces are from one object, that means calling QueryInterface on an
224 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
225 * IDirectDrawImpl object.
227 * That means all AddRef and Release implementations of IDirectDrawX work
228 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
229 * except of IDirect3D7 which thunks to IDirectDraw7
231 * Returns: The new refcount
233 *****************************************************************************/
234 static ULONG WINAPI
235 IDirectDrawImpl_AddRef(IDirectDraw7 *iface)
237 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
238 ULONG ref = InterlockedIncrement(&This->ref7);
240 TRACE("(%p) : incrementing IDirectDraw7 refcount from %u.\n", This, ref -1);
242 if(ref == 1) InterlockedIncrement(&This->numIfaces);
244 return ref;
247 /*****************************************************************************
248 * IDirectDrawImpl_Destroy
250 * Destroys a ddraw object if all refcounts are 0. This is to share code
251 * between the IDirectDrawX::Release functions
253 * Params:
254 * This: DirectDraw object to destroy
256 *****************************************************************************/
257 void
258 IDirectDrawImpl_Destroy(IDirectDrawImpl *This)
260 /* Clear the cooplevel to restore window and display mode */
261 IDirectDraw7_SetCooperativeLevel((IDirectDraw7 *)This, NULL, DDSCL_NORMAL);
263 /* Destroy the device window if we created one */
264 if(This->devicewindow != 0)
266 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
267 DestroyWindow(This->devicewindow);
268 This->devicewindow = 0;
271 /* Unregister the window class */
272 UnregisterClassA(This->classname, 0);
274 EnterCriticalSection(&ddraw_cs);
275 list_remove(&This->ddraw_list_entry);
276 LeaveCriticalSection(&ddraw_cs);
278 /* Release the attached WineD3D stuff */
279 IWineD3DDevice_Release(This->wineD3DDevice);
280 IWineD3D_Release(This->wineD3D);
282 /* Now free the object */
283 HeapFree(GetProcessHeap(), 0, This);
286 /*****************************************************************************
287 * IDirectDraw7::Release
289 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
291 * Returns: The new refcount
292 *****************************************************************************/
293 static ULONG WINAPI
294 IDirectDrawImpl_Release(IDirectDraw7 *iface)
296 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
297 ULONG ref = InterlockedDecrement(&This->ref7);
299 TRACE("(%p)->() decrementing IDirectDraw7 refcount from %u.\n", This, ref +1);
301 if(ref == 0)
303 ULONG ifacecount = InterlockedDecrement(&This->numIfaces);
304 if(ifacecount == 0) IDirectDrawImpl_Destroy(This);
307 return ref;
310 /*****************************************************************************
311 * IDirectDraw methods
312 *****************************************************************************/
314 /*****************************************************************************
315 * IDirectDraw7::SetCooperativeLevel
317 * Sets the cooperative level for the DirectDraw object, and the window
318 * assigned to it. The cooperative level determines the general behavior
319 * of the DirectDraw application
321 * Warning: This is quite tricky, as it's not really documented which
322 * cooperative levels can be combined with each other. If a game fails
323 * after this function, try to check the cooperative levels passed on
324 * Windows, and if it returns something different.
326 * If you think that this function caused the failure because it writes a
327 * fixme, be sure to run again with a +ddraw trace.
329 * What is known about cooperative levels (See the ddraw modes test):
330 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
331 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
332 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
333 * DDSCL_FULLSCREEN can be activated
334 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
336 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
337 * DDSCL_SETFOCUSWINDOW (partially),
338 * DDSCL_MULTITHREADED (work in progress)
340 * Unhandled flags, which should be implemented
341 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
342 * expect any difference to a normal window for wine)
343 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
344 * rendering (Possible test case: Half-life)
346 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
348 * These don't seem very important for wine:
349 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
351 * Returns:
352 * DD_OK if the cooperative level was set successfully
353 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
354 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
355 * (Probably others too, have to investigate)
357 *****************************************************************************/
358 static HRESULT WINAPI
359 IDirectDrawImpl_SetCooperativeLevel(IDirectDraw7 *iface,
360 HWND hwnd,
361 DWORD cooplevel)
363 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
364 HWND window;
366 TRACE("(%p)->(%p,%08x)\n",This,hwnd,cooplevel);
367 DDRAW_dump_cooperativelevel(cooplevel);
369 EnterCriticalSection(&ddraw_cs);
371 /* Get the old window */
372 window = This->dest_window;
374 /* Tests suggest that we need one of them: */
375 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
376 DDSCL_NORMAL |
377 DDSCL_EXCLUSIVE )))
379 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
380 LeaveCriticalSection(&ddraw_cs);
381 return DDERR_INVALIDPARAMS;
384 /* Handle those levels first which set various hwnds */
385 if(cooplevel & DDSCL_SETFOCUSWINDOW)
387 /* This isn't compatible with a lot of flags */
388 if(cooplevel & ( DDSCL_MULTITHREADED |
389 DDSCL_FPUSETUP |
390 DDSCL_FPUPRESERVE |
391 DDSCL_ALLOWREBOOT |
392 DDSCL_ALLOWMODEX |
393 DDSCL_SETDEVICEWINDOW |
394 DDSCL_NORMAL |
395 DDSCL_EXCLUSIVE |
396 DDSCL_FULLSCREEN ) )
398 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
399 LeaveCriticalSection(&ddraw_cs);
400 return DDERR_INVALIDPARAMS;
402 else if( (This->cooperative_level & DDSCL_FULLSCREEN) && window)
404 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET\n");
405 LeaveCriticalSection(&ddraw_cs);
406 return DDERR_HWNDALREADYSET;
409 This->focuswindow = hwnd;
410 /* Won't use the hwnd param for anything else */
411 hwnd = NULL;
413 /* Use the focus window for drawing too */
414 This->dest_window = This->focuswindow;
416 /* Destroy the device window, if we have one */
417 if(This->devicewindow)
419 DestroyWindow(This->devicewindow);
420 This->devicewindow = NULL;
423 /* DDSCL_NORMAL or DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE */
424 if(cooplevel & DDSCL_NORMAL)
426 /* Can't coexist with fullscreen or exclusive */
427 if(cooplevel & (DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE) )
429 TRACE("(%p) DDSCL_NORMAL is not compative with DDSCL_FULLSCREEN or DDSCL_EXCLUSIVE\n", This);
430 LeaveCriticalSection(&ddraw_cs);
431 return DDERR_INVALIDPARAMS;
434 /* Switching from fullscreen? */
435 if(This->cooperative_level & DDSCL_FULLSCREEN)
437 /* Restore the display mode */
438 IDirectDraw7_RestoreDisplayMode(iface);
440 This->cooperative_level &= ~DDSCL_FULLSCREEN;
441 This->cooperative_level &= ~DDSCL_EXCLUSIVE;
442 This->cooperative_level &= ~DDSCL_ALLOWMODEX;
445 /* Don't override focus windows or private device windows */
446 if( hwnd &&
447 !(This->focuswindow) &&
448 !(This->devicewindow) &&
449 (hwnd != window) )
451 This->dest_window = hwnd;
454 else if(cooplevel & DDSCL_FULLSCREEN)
456 /* Needs DDSCL_EXCLUSIVE */
457 if(!(cooplevel & DDSCL_EXCLUSIVE) )
459 TRACE("(%p) DDSCL_FULLSCREEN needs DDSCL_EXCLUSIVE\n", This);
460 LeaveCriticalSection(&ddraw_cs);
461 return DDERR_INVALIDPARAMS;
463 /* Need a HWND
464 if(hwnd == 0)
466 TRACE("(%p) DDSCL_FULLSCREEN needs a HWND\n", This);
467 return DDERR_INVALIDPARAMS;
471 This->cooperative_level &= ~DDSCL_NORMAL;
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 IDirectDrawImpl *This = (IDirectDrawImpl *)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 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
666 TRACE("(%p)\n", This);
668 return IDirectDrawImpl_SetDisplayModeNoOverride(iface,
669 This->orig_width, This->orig_height, This->orig_bpp, 0, 0);
672 /*****************************************************************************
673 * IDirectDraw7::GetCaps
675 * Returns the drives capabilities
677 * Used for version 1, 2, 4 and 7
679 * Params:
680 * DriverCaps: Structure to write the Hardware accelerated caps to
681 * HelCaps: Structure to write the emulation caps to
683 * Returns
684 * This implementation returns DD_OK only
686 *****************************************************************************/
687 static HRESULT WINAPI
688 IDirectDrawImpl_GetCaps(IDirectDraw7 *iface,
689 DDCAPS *DriverCaps,
690 DDCAPS *HELCaps)
692 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
693 DDCAPS caps;
694 WINED3DCAPS winecaps;
695 HRESULT hr;
696 DDSCAPS2 ddscaps = {0, 0, 0, 0};
697 TRACE("(%p)->(%p,%p)\n", This, DriverCaps, HELCaps);
699 /* One structure must be != NULL */
700 if( (!DriverCaps) && (!HELCaps) )
702 ERR("(%p) Invalid params to IDirectDrawImpl_GetCaps\n", This);
703 return DDERR_INVALIDPARAMS;
706 memset(&caps, 0, sizeof(caps));
707 memset(&winecaps, 0, sizeof(winecaps));
708 caps.dwSize = sizeof(caps);
709 EnterCriticalSection(&ddraw_cs);
710 hr = IWineD3DDevice_GetDeviceCaps(This->wineD3DDevice, &winecaps);
711 if(FAILED(hr)) {
712 WARN("IWineD3DDevice::GetDeviceCaps failed\n");
713 LeaveCriticalSection(&ddraw_cs);
714 return hr;
717 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
718 LeaveCriticalSection(&ddraw_cs);
719 if(FAILED(hr)) {
720 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
721 return hr;
724 caps.dwCaps = winecaps.DirectDrawCaps.Caps;
725 caps.dwCaps2 = winecaps.DirectDrawCaps.Caps2;
726 caps.dwCKeyCaps = winecaps.DirectDrawCaps.CKeyCaps;
727 caps.dwFXCaps = winecaps.DirectDrawCaps.FXCaps;
728 caps.dwPalCaps = winecaps.DirectDrawCaps.PalCaps;
729 caps.ddsCaps.dwCaps = winecaps.DirectDrawCaps.ddsCaps;
730 caps.dwSVBCaps = winecaps.DirectDrawCaps.SVBCaps;
731 caps.dwSVBCKeyCaps = winecaps.DirectDrawCaps.SVBCKeyCaps;
732 caps.dwSVBFXCaps = winecaps.DirectDrawCaps.SVBFXCaps;
733 caps.dwVSBCaps = winecaps.DirectDrawCaps.VSBCaps;
734 caps.dwVSBCKeyCaps = winecaps.DirectDrawCaps.VSBCKeyCaps;
735 caps.dwVSBFXCaps = winecaps.DirectDrawCaps.VSBFXCaps;
736 caps.dwSSBCaps = winecaps.DirectDrawCaps.SSBCaps;
737 caps.dwSSBCKeyCaps = winecaps.DirectDrawCaps.SSBCKeyCaps;
738 caps.dwSSBFXCaps = winecaps.DirectDrawCaps.SSBFXCaps;
740 /* Even if WineD3D supports 3D rendering, remove the cap if ddraw is configured
741 * not to use it
743 if(DefaultSurfaceType == SURFACE_GDI) {
744 caps.dwCaps &= ~DDCAPS_3D;
745 caps.ddsCaps.dwCaps &= ~(DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER);
747 if(winecaps.DirectDrawCaps.StrideAlign != 0) {
748 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
749 caps.dwAlignStrideAlign = winecaps.DirectDrawCaps.StrideAlign;
752 if(DriverCaps)
754 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
755 if (TRACE_ON(ddraw))
757 TRACE("Driver Caps :\n");
758 DDRAW_dump_DDCAPS(DriverCaps);
762 if(HELCaps)
764 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
765 if (TRACE_ON(ddraw))
767 TRACE("HEL Caps :\n");
768 DDRAW_dump_DDCAPS(HELCaps);
772 return DD_OK;
775 /*****************************************************************************
776 * IDirectDraw7::Compact
778 * No idea what it does, MSDN says it's not implemented.
780 * Returns
781 * DD_OK, but this is unchecked
783 *****************************************************************************/
784 static HRESULT WINAPI
785 IDirectDrawImpl_Compact(IDirectDraw7 *iface)
787 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
788 TRACE("(%p)\n", This);
790 return DD_OK;
793 /*****************************************************************************
794 * IDirectDraw7::GetDisplayMode
796 * Returns information about the current display mode
798 * Exists in Version 1, 2, 4 and 7
800 * Params:
801 * DDSD: Address of a surface description structure to write the info to
803 * Returns
804 * DD_OK
806 *****************************************************************************/
807 static HRESULT WINAPI
808 IDirectDrawImpl_GetDisplayMode(IDirectDraw7 *iface,
809 DDSURFACEDESC2 *DDSD)
811 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
812 HRESULT hr;
813 WINED3DDISPLAYMODE Mode;
814 DWORD Size;
815 TRACE("(%p)->(%p): Relay\n", This, DDSD);
817 EnterCriticalSection(&ddraw_cs);
818 /* This seems sane */
819 if(!DDSD)
821 LeaveCriticalSection(&ddraw_cs);
822 return DDERR_INVALIDPARAMS;
825 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
826 * so one method can be used for all versions (Hopefully)
828 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
829 0 /* swapchain 0 */,
830 &Mode);
831 if( hr != D3D_OK )
833 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
834 LeaveCriticalSection(&ddraw_cs);
835 return hr;
838 Size = DDSD->dwSize;
839 memset(DDSD, 0, Size);
841 DDSD->dwSize = Size;
842 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
843 DDSD->dwWidth = Mode.Width;
844 DDSD->dwHeight = Mode.Height;
845 DDSD->u2.dwRefreshRate = 60;
846 DDSD->ddsCaps.dwCaps = 0;
847 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
848 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, Mode.Format);
849 DDSD->u1.lPitch = Mode.Width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
851 if(TRACE_ON(ddraw))
853 TRACE("Returning surface desc :\n");
854 DDRAW_dump_surface_desc(DDSD);
857 LeaveCriticalSection(&ddraw_cs);
858 return DD_OK;
861 /*****************************************************************************
862 * IDirectDraw7::GetFourCCCodes
864 * Returns an array of supported FourCC codes.
866 * Exists in Version 1, 2, 4 and 7
868 * Params:
869 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
870 * of enumerated codes
871 * Codes: Pointer to an array of DWORDs where the supported codes are written
872 * to
874 * Returns
875 * Always returns DD_OK, as it's a stub for now
877 *****************************************************************************/
878 static HRESULT WINAPI
879 IDirectDrawImpl_GetFourCCCodes(IDirectDraw7 *iface,
880 DWORD *NumCodes, DWORD *Codes)
882 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
883 WINED3DFORMAT formats[] = {
884 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
885 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
886 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
888 DWORD count = 0, i, outsize;
889 HRESULT hr;
890 WINED3DDISPLAYMODE d3ddm;
891 WINED3DSURFTYPE type = This->ImplType;
892 TRACE("(%p)->(%p, %p)\n", This, NumCodes, Codes);
894 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
895 0 /* swapchain 0 */,
896 &d3ddm);
898 outsize = NumCodes && Codes ? *NumCodes : 0;
900 if(type == SURFACE_UNKNOWN) type = SURFACE_GDI;
902 for(i = 0; i < (sizeof(formats) / sizeof(formats[0])); i++) {
903 hr = IWineD3D_CheckDeviceFormat(This->wineD3D,
904 WINED3DADAPTER_DEFAULT,
905 WINED3DDEVTYPE_HAL,
906 d3ddm.Format /* AdapterFormat */,
907 0 /* usage */,
908 WINED3DRTYPE_SURFACE,
909 formats[i],
910 type);
911 if(SUCCEEDED(hr)) {
912 if(count < outsize) {
913 Codes[count] = formats[i];
915 count++;
918 if(NumCodes) {
919 TRACE("Returning %u FourCC codes\n", count);
920 *NumCodes = count;
923 return DD_OK;
926 /*****************************************************************************
927 * IDirectDraw7::GetMonitorFrequency
929 * Returns the monitor's frequency
931 * Exists in Version 1, 2, 4 and 7
933 * Params:
934 * Freq: Pointer to a DWORD to write the frequency to
936 * Returns
937 * Always returns DD_OK
939 *****************************************************************************/
940 static HRESULT WINAPI
941 IDirectDrawImpl_GetMonitorFrequency(IDirectDraw7 *iface,
942 DWORD *Freq)
944 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
945 TRACE("(%p)->(%p)\n", This, Freq);
947 /* Ideally this should be in WineD3D, as it concerns the screen setup,
948 * but for now this should make the games happy
950 *Freq = 60;
951 return DD_OK;
954 /*****************************************************************************
955 * IDirectDraw7::GetVerticalBlankStatus
957 * Returns the Vertical blank status of the monitor. This should be in WineD3D
958 * too basically, but as it's a semi stub, I didn't create a function there
960 * Params:
961 * status: Pointer to a BOOL to be filled with the vertical blank status
963 * Returns
964 * DD_OK on success
965 * DDERR_INVALIDPARAMS if status is NULL
967 *****************************************************************************/
968 static HRESULT WINAPI
969 IDirectDrawImpl_GetVerticalBlankStatus(IDirectDraw7 *iface,
970 BOOL *status)
972 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
973 TRACE("(%p)->(%p)\n", This, status);
975 /* This looks sane, the MSDN suggests it too */
976 EnterCriticalSection(&ddraw_cs);
977 if(!status)
979 LeaveCriticalSection(&ddraw_cs);
980 return DDERR_INVALIDPARAMS;
983 *status = This->fake_vblank;
984 This->fake_vblank = !This->fake_vblank;
985 LeaveCriticalSection(&ddraw_cs);
986 return DD_OK;
989 /*****************************************************************************
990 * IDirectDraw7::GetAvailableVidMem
992 * Returns the total and free video memory
994 * Params:
995 * Caps: Specifies the memory type asked for
996 * total: Pointer to a DWORD to be filled with the total memory
997 * free: Pointer to a DWORD to be filled with the free memory
999 * Returns
1000 * DD_OK on success
1001 * DDERR_INVALIDPARAMS of free and total are NULL
1003 *****************************************************************************/
1004 static HRESULT WINAPI
1005 IDirectDrawImpl_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total, DWORD *free)
1007 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1008 TRACE("(%p)->(%p, %p, %p)\n", This, Caps, total, free);
1010 if(TRACE_ON(ddraw))
1012 TRACE("(%p) Asked for memory with description: ", This);
1013 DDRAW_dump_DDSCAPS2(Caps);
1015 EnterCriticalSection(&ddraw_cs);
1017 /* Todo: System memory vs local video memory vs non-local video memory
1018 * The MSDN also mentions differences between texture memory and other
1019 * resources, but that's not important
1022 if( (!total) && (!free) )
1024 LeaveCriticalSection(&ddraw_cs);
1025 return DDERR_INVALIDPARAMS;
1028 if(total) *total = This->total_vidmem;
1029 if(free) *free = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
1031 LeaveCriticalSection(&ddraw_cs);
1032 return DD_OK;
1035 /*****************************************************************************
1036 * IDirectDraw7::Initialize
1038 * Initializes a DirectDraw interface.
1040 * Params:
1041 * GUID: Interface identifier. Well, don't know what this is really good
1042 * for
1044 * Returns
1045 * Returns DD_OK on the first call,
1046 * DDERR_ALREADYINITIALIZED on repeated calls
1048 *****************************************************************************/
1049 static HRESULT WINAPI
1050 IDirectDrawImpl_Initialize(IDirectDraw7 *iface,
1051 GUID *Guid)
1053 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1054 TRACE("(%p)->(%s): No-op\n", This, debugstr_guid(Guid));
1056 if(This->initialized)
1058 return DDERR_ALREADYINITIALIZED;
1060 else
1062 return DD_OK;
1066 /*****************************************************************************
1067 * IDirectDraw7::FlipToGDISurface
1069 * "Makes the surface that the GDI writes to the primary surface"
1070 * Looks like some windows specific thing we don't have to care about.
1071 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1072 * show error boxes ;)
1073 * Well, just return DD_OK.
1075 * Returns:
1076 * Always returns DD_OK
1078 *****************************************************************************/
1079 static HRESULT WINAPI
1080 IDirectDrawImpl_FlipToGDISurface(IDirectDraw7 *iface)
1082 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1083 TRACE("(%p)\n", This);
1085 return DD_OK;
1088 /*****************************************************************************
1089 * IDirectDraw7::WaitForVerticalBlank
1091 * This method allows applications to get in sync with the vertical blank
1092 * interval.
1093 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1094 * redraw the screen, most likely because of this stub
1096 * Parameters:
1097 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1098 * or DDWAITVB_BLOCKEND
1099 * h: Not used, according to MSDN
1101 * Returns:
1102 * Always returns DD_OK
1104 *****************************************************************************/
1105 static HRESULT WINAPI
1106 IDirectDrawImpl_WaitForVerticalBlank(IDirectDraw7 *iface,
1107 DWORD Flags,
1108 HANDLE h)
1110 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1111 static BOOL hide = FALSE;
1113 /* This function is called often, so print the fixme only once */
1114 if(!hide)
1116 FIXME("(%p)->(%x,%p): Stub\n", This, Flags, h);
1117 hide = TRUE;
1120 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1121 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1122 return DDERR_UNSUPPORTED; /* unchecked */
1124 return DD_OK;
1127 /*****************************************************************************
1128 * IDirectDraw7::GetScanLine
1130 * Returns the scan line that is being drawn on the monitor
1132 * Parameters:
1133 * Scanline: Address to write the scan line value to
1135 * Returns:
1136 * Always returns DD_OK
1138 *****************************************************************************/
1139 static HRESULT WINAPI IDirectDrawImpl_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1141 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1142 static BOOL hide = FALSE;
1143 WINED3DDISPLAYMODE Mode;
1145 /* This function is called often, so print the fixme only once */
1146 EnterCriticalSection(&ddraw_cs);
1147 if(!hide)
1149 FIXME("(%p)->(%p): Semi-Stub\n", This, Scanline);
1150 hide = TRUE;
1153 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1155 &Mode);
1157 /* Fake the line sweeping of the monitor */
1158 /* FIXME: We should synchronize with a source to keep the refresh rate */
1159 *Scanline = This->cur_scanline++;
1160 /* Assume 20 scan lines in the vertical blank */
1161 if (This->cur_scanline >= Mode.Height + 20)
1162 This->cur_scanline = 0;
1164 LeaveCriticalSection(&ddraw_cs);
1165 return DD_OK;
1168 /*****************************************************************************
1169 * IDirectDraw7::TestCooperativeLevel
1171 * Informs the application about the state of the video adapter, depending
1172 * on the cooperative level
1174 * Returns:
1175 * DD_OK if the device is in a sane state
1176 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1177 * if the state is not correct(See below)
1179 *****************************************************************************/
1180 static HRESULT WINAPI
1181 IDirectDrawImpl_TestCooperativeLevel(IDirectDraw7 *iface)
1183 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1184 HRESULT hr;
1185 TRACE("(%p)\n", This);
1187 EnterCriticalSection(&ddraw_cs);
1188 /* Description from MSDN:
1189 * For fullscreen apps return DDERR_NOEXCLUSIVEMODE if the user switched
1190 * away from the app with e.g. alt-tab. Windowed apps receive
1191 * DDERR_EXCLUSIVEMODEALREADYSET if another application created a
1192 * DirectDraw object in exclusive mode. DDERR_WRONGMODE is returned,
1193 * when the video mode has changed
1196 hr = IWineD3DDevice_TestCooperativeLevel(This->wineD3DDevice);
1198 /* Fix the result value. These values are mapped from their
1199 * d3d9 counterpart.
1201 switch(hr)
1203 case WINED3DERR_DEVICELOST:
1204 if(This->cooperative_level & DDSCL_EXCLUSIVE)
1206 LeaveCriticalSection(&ddraw_cs);
1207 return DDERR_NOEXCLUSIVEMODE;
1209 else
1211 LeaveCriticalSection(&ddraw_cs);
1212 return DDERR_EXCLUSIVEMODEALREADYSET;
1215 case WINED3DERR_DEVICENOTRESET:
1216 LeaveCriticalSection(&ddraw_cs);
1217 return DD_OK;
1219 case WINED3D_OK:
1220 LeaveCriticalSection(&ddraw_cs);
1221 return DD_OK;
1223 case WINED3DERR_DRIVERINTERNALERROR:
1224 default:
1225 ERR("(%p) Unexpected return value %08x from wineD3D, "
1226 " returning DD_OK\n", This, hr);
1229 LeaveCriticalSection(&ddraw_cs);
1230 return DD_OK;
1233 /*****************************************************************************
1234 * IDirectDraw7::GetGDISurface
1236 * Returns the surface that GDI is treating as the primary surface.
1237 * For Wine this is the front buffer
1239 * Params:
1240 * GDISurface: Address to write the surface pointer to
1242 * Returns:
1243 * DD_OK if the surface was found
1244 * DDERR_NOTFOUND if the GDI surface wasn't found
1246 *****************************************************************************/
1247 static HRESULT WINAPI
1248 IDirectDrawImpl_GetGDISurface(IDirectDraw7 *iface,
1249 IDirectDrawSurface7 **GDISurface)
1251 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1252 IWineD3DSurface *Surf;
1253 IDirectDrawSurface7 *ddsurf;
1254 HRESULT hr;
1255 DDSCAPS2 ddsCaps;
1256 TRACE("(%p)->(%p)\n", This, GDISurface);
1258 /* Get the back buffer from the wineD3DDevice and search its
1259 * attached surfaces for the front buffer
1261 EnterCriticalSection(&ddraw_cs);
1262 hr = IWineD3DDevice_GetBackBuffer(This->wineD3DDevice,
1263 0, /* SwapChain */
1264 0, /* first back buffer*/
1265 WINED3DBACKBUFFER_TYPE_MONO,
1266 &Surf);
1268 if( (hr != D3D_OK) ||
1269 (!Surf) )
1271 ERR("IWineD3DDevice::GetBackBuffer failed\n");
1272 LeaveCriticalSection(&ddraw_cs);
1273 return DDERR_NOTFOUND;
1276 /* GetBackBuffer AddRef()ed the surface, release it */
1277 IWineD3DSurface_Release(Surf);
1279 IWineD3DSurface_GetParent(Surf,
1280 (IUnknown **) &ddsurf);
1281 IDirectDrawSurface7_Release(ddsurf); /* For the GetParent */
1283 /* Find the front buffer */
1284 ddsCaps.dwCaps = DDSCAPS_FRONTBUFFER;
1285 hr = IDirectDrawSurface7_GetAttachedSurface(ddsurf,
1286 &ddsCaps,
1287 GDISurface);
1288 if(hr != DD_OK)
1290 ERR("IDirectDrawSurface7::GetAttachedSurface failed, hr = %x\n", hr);
1293 /* The AddRef is OK this time */
1294 LeaveCriticalSection(&ddraw_cs);
1295 return hr;
1298 /*****************************************************************************
1299 * IDirectDraw7::EnumDisplayModes
1301 * Enumerates the supported Display modes. The modes can be filtered with
1302 * the DDSD parameter.
1304 * Params:
1305 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES
1306 * DDSD: Surface description to filter the modes
1307 * Context: Pointer passed back to the callback function
1308 * cb: Application-provided callback function
1310 * Returns:
1311 * DD_OK on success
1312 * DDERR_INVALIDPARAMS if the callback wasn't set
1314 *****************************************************************************/
1315 static HRESULT WINAPI
1316 IDirectDrawImpl_EnumDisplayModes(IDirectDraw7 *iface,
1317 DWORD Flags,
1318 DDSURFACEDESC2 *DDSD,
1319 void *Context,
1320 LPDDENUMMODESCALLBACK2 cb)
1322 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1323 unsigned int modenum, fmt;
1324 WINED3DFORMAT pixelformat = WINED3DFMT_UNKNOWN;
1325 WINED3DDISPLAYMODE mode;
1326 DDSURFACEDESC2 callback_sd;
1327 WINED3DDISPLAYMODE *enum_modes = NULL;
1328 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
1330 WINED3DFORMAT checkFormatList[] =
1332 WINED3DFMT_R8G8B8,
1333 WINED3DFMT_A8R8G8B8,
1334 WINED3DFMT_X8R8G8B8,
1335 WINED3DFMT_R5G6B5,
1336 WINED3DFMT_X1R5G5B5,
1337 WINED3DFMT_A1R5G5B5,
1338 WINED3DFMT_A4R4G4B4,
1339 WINED3DFMT_R3G3B2,
1340 WINED3DFMT_A8R3G3B2,
1341 WINED3DFMT_X4R4G4B4,
1342 WINED3DFMT_R10G10B10A2_UNORM,
1343 WINED3DFMT_R8G8B8A8_UNORM,
1344 WINED3DFMT_X8B8G8R8,
1345 WINED3DFMT_A2R10G10B10,
1346 WINED3DFMT_A8P8,
1347 WINED3DFMT_P8
1350 TRACE("(%p)->(%p,%p,%p): Relay\n", This, DDSD, Context, cb);
1352 EnterCriticalSection(&ddraw_cs);
1353 /* This looks sane */
1354 if(!cb)
1356 LeaveCriticalSection(&ddraw_cs);
1357 return DDERR_INVALIDPARAMS;
1360 if(DDSD)
1362 if ((DDSD->dwFlags & DDSD_PIXELFORMAT) && (DDSD->u4.ddpfPixelFormat.dwFlags & DDPF_RGB) )
1363 pixelformat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
1366 if(!(Flags & DDEDM_REFRESHRATES))
1368 enum_mode_array_size = 16;
1369 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1370 if (!enum_modes)
1372 ERR("Out of memory\n");
1373 LeaveCriticalSection(&ddraw_cs);
1374 return DDERR_OUTOFMEMORY;
1378 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
1380 if(pixelformat != WINED3DFMT_UNKNOWN && checkFormatList[fmt] != pixelformat)
1382 continue;
1385 modenum = 0;
1386 while(IWineD3D_EnumAdapterModes(This->wineD3D,
1387 WINED3DADAPTER_DEFAULT,
1388 checkFormatList[fmt],
1389 modenum++,
1390 &mode) == WINED3D_OK)
1392 if(DDSD)
1394 if(DDSD->dwFlags & DDSD_WIDTH && mode.Width != DDSD->dwWidth) continue;
1395 if(DDSD->dwFlags & DDSD_HEIGHT && mode.Height != DDSD->dwHeight) continue;
1398 if(!(Flags & DDEDM_REFRESHRATES))
1400 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
1401 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
1402 * to be reduced to a single unique result in such case.
1404 BOOL found = FALSE;
1405 unsigned i;
1407 for (i = 0; i < enum_mode_count; i++)
1409 if(enum_modes[i].Width == mode.Width && enum_modes[i].Height == mode.Height &&
1410 enum_modes[i].Format == mode.Format)
1412 found = TRUE;
1413 break;
1417 if(found) continue;
1420 memset(&callback_sd, 0, sizeof(callback_sd));
1421 callback_sd.dwSize = sizeof(callback_sd);
1422 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1424 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH;
1425 if(Flags & DDEDM_REFRESHRATES)
1427 callback_sd.dwFlags |= DDSD_REFRESHRATE;
1428 callback_sd.u2.dwRefreshRate = mode.RefreshRate;
1431 callback_sd.dwWidth = mode.Width;
1432 callback_sd.dwHeight = mode.Height;
1434 PixelFormat_WineD3DtoDD(&callback_sd.u4.ddpfPixelFormat, mode.Format);
1436 /* Calc pitch and DWORD align like MSDN says */
1437 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.Width;
1438 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
1440 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
1441 callback_sd.u2.dwRefreshRate);
1443 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
1445 TRACE("Application asked to terminate the enumeration\n");
1446 HeapFree(GetProcessHeap(), 0, enum_modes);
1447 LeaveCriticalSection(&ddraw_cs);
1448 return DD_OK;
1451 if(!(Flags & DDEDM_REFRESHRATES))
1453 if (enum_mode_count == enum_mode_array_size)
1455 WINED3DDISPLAYMODE *new_enum_modes;
1457 enum_mode_array_size *= 2;
1458 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1460 if (!new_enum_modes)
1462 ERR("Out of memory\n");
1463 HeapFree(GetProcessHeap(), 0, enum_modes);
1464 LeaveCriticalSection(&ddraw_cs);
1465 return DDERR_OUTOFMEMORY;
1468 enum_modes = new_enum_modes;
1471 enum_modes[enum_mode_count++] = mode;
1476 TRACE("End of enumeration\n");
1477 HeapFree(GetProcessHeap(), 0, enum_modes);
1478 LeaveCriticalSection(&ddraw_cs);
1479 return DD_OK;
1482 /*****************************************************************************
1483 * IDirectDraw7::EvaluateMode
1485 * Used with IDirectDraw7::StartModeTest to test video modes.
1486 * EvaluateMode is used to pass or fail a mode, and continue with the next
1487 * mode
1489 * Params:
1490 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
1491 * Timeout: Returns the amount of seconds left before the mode would have
1492 * been failed automatically
1494 * Returns:
1495 * This implementation always DD_OK, because it's a stub
1497 *****************************************************************************/
1498 static HRESULT WINAPI
1499 IDirectDrawImpl_EvaluateMode(IDirectDraw7 *iface,
1500 DWORD Flags,
1501 DWORD *Timeout)
1503 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1504 FIXME("(%p)->(%d,%p): Stub!\n", This, Flags, Timeout);
1506 /* When implementing this, implement it in WineD3D */
1508 return DD_OK;
1511 /*****************************************************************************
1512 * IDirectDraw7::GetDeviceIdentifier
1514 * Returns the device identifier, which gives information about the driver
1515 * Our device identifier is defined at the beginning of this file.
1517 * Params:
1518 * DDDI: Address for the returned structure
1519 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
1521 * Returns:
1522 * On success it returns DD_OK
1523 * DDERR_INVALIDPARAMS if DDDI is NULL
1525 *****************************************************************************/
1526 static HRESULT WINAPI
1527 IDirectDrawImpl_GetDeviceIdentifier(IDirectDraw7 *iface,
1528 DDDEVICEIDENTIFIER2 *DDDI,
1529 DWORD Flags)
1531 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1532 TRACE("(%p)->(%p,%08x)\n", This, DDDI, Flags);
1534 if(!DDDI)
1535 return DDERR_INVALIDPARAMS;
1537 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
1538 * host adapter, if there's a secondary 3D adapter. This doesn't apply
1539 * to any modern hardware, nor is it interesting for Wine, so ignore it
1542 *DDDI = deviceidentifier;
1543 return DD_OK;
1546 /*****************************************************************************
1547 * IDirectDraw7::GetSurfaceFromDC
1549 * Returns the Surface for a GDI device context handle.
1550 * Is this related to IDirectDrawSurface::GetDC ???
1552 * Params:
1553 * hdc: hdc to return the surface for
1554 * Surface: Address to write the surface pointer to
1556 * Returns:
1557 * Always returns DD_OK because it's a stub
1559 *****************************************************************************/
1560 static HRESULT WINAPI
1561 IDirectDrawImpl_GetSurfaceFromDC(IDirectDraw7 *iface,
1562 HDC hdc,
1563 IDirectDrawSurface7 **Surface)
1565 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1566 FIXME("(%p)->(%p,%p): Stub!\n", This, hdc, Surface);
1568 /* Implementation idea if needed: Loop through all surfaces and compare
1569 * their hdc with hdc. Implement it in WineD3D! */
1570 return DDERR_NOTFOUND;
1573 /*****************************************************************************
1574 * IDirectDraw7::RestoreAllSurfaces
1576 * Calls the restore method of all surfaces
1578 * Params:
1580 * Returns:
1581 * Always returns DD_OK because it's a stub
1583 *****************************************************************************/
1584 static HRESULT WINAPI
1585 IDirectDrawImpl_RestoreAllSurfaces(IDirectDraw7 *iface)
1587 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1588 FIXME("(%p): Stub\n", This);
1590 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
1591 * get their parent and call their restore method. Do not implement
1592 * it in WineD3D, as restoring a surface means re-creating the
1593 * WineD3DDSurface
1595 return DD_OK;
1598 /*****************************************************************************
1599 * IDirectDraw7::StartModeTest
1601 * Tests the specified video modes to update the system registry with
1602 * refresh rate information. StartModeTest starts the mode test,
1603 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
1604 * isn't called within 15 seconds, the mode is failed automatically
1606 * As refresh rates are handled by the X server, I don't think this
1607 * Method is important
1609 * Params:
1610 * Modes: An array of mode specifications
1611 * NumModes: The number of modes in Modes
1612 * Flags: Some flags...
1614 * Returns:
1615 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
1616 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
1617 * otherwise DD_OK
1619 *****************************************************************************/
1620 static HRESULT WINAPI
1621 IDirectDrawImpl_StartModeTest(IDirectDraw7 *iface,
1622 SIZE *Modes,
1623 DWORD NumModes,
1624 DWORD Flags)
1626 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1627 WARN("(%p)->(%p, %d, %x): Semi-Stub, most likely harmless\n", This, Modes, NumModes, Flags);
1629 /* This looks sane */
1630 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
1632 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
1633 * As it is not, DDERR_TESTFINISHED is returned
1634 * (hopefully that's correct
1636 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
1637 * well, that value doesn't (yet) exist in the wine headers, so ignore it
1640 return DD_OK;
1643 /*****************************************************************************
1644 * IDirectDrawImpl_RecreateSurfacesCallback
1646 * Enumeration callback for IDirectDrawImpl_RecreateAllSurfaces.
1647 * It re-recreates the WineD3DSurface. It's pretty straightforward
1649 *****************************************************************************/
1650 HRESULT WINAPI
1651 IDirectDrawImpl_RecreateSurfacesCallback(IDirectDrawSurface7 *surf,
1652 DDSURFACEDESC2 *desc,
1653 void *Context)
1655 IDirectDrawSurfaceImpl *surfImpl = (IDirectDrawSurfaceImpl *)surf;
1656 IDirectDrawImpl *This = surfImpl->ddraw;
1657 IUnknown *Parent;
1658 IParentImpl *parImpl = NULL;
1659 IWineD3DSurface *wineD3DSurface;
1660 IWineD3DSwapChain *swapchain;
1661 HRESULT hr;
1662 void *tmp;
1663 IWineD3DClipper *clipper = NULL;
1665 WINED3DSURFACE_DESC Desc;
1666 WINED3DFORMAT Format;
1667 DWORD Usage;
1668 WINED3DPOOL Pool;
1670 WINED3DMULTISAMPLE_TYPE MultiSampleType;
1671 DWORD MultiSampleQuality;
1672 UINT Width;
1673 UINT Height;
1675 TRACE("(%p): Enumerated Surface %p\n", This, surfImpl);
1677 /* For the enumeration */
1678 IDirectDrawSurface7_Release(surf);
1680 if(surfImpl->ImplType == This->ImplType) return DDENUMRET_OK; /* Continue */
1682 /* Get the objects */
1683 swapchain = surfImpl->wineD3DSwapChain;
1684 surfImpl->wineD3DSwapChain = NULL;
1685 wineD3DSurface = surfImpl->WineD3DSurface;
1686 IWineD3DSurface_GetParent(wineD3DSurface, &Parent);
1687 IUnknown_Release(Parent); /* For the getParent */
1689 /* Is the parent an IParent interface? */
1690 if(IUnknown_QueryInterface(Parent, &IID_IParent, &tmp) == S_OK)
1692 /* It is a IParent interface! */
1693 IUnknown_Release(Parent); /* For the QueryInterface */
1694 parImpl = (IParentImpl *)Parent;
1695 /* Release the reference the parent interface is holding */
1696 IWineD3DSurface_Release(wineD3DSurface);
1699 /* get the clipper */
1700 IWineD3DSurface_GetClipper(wineD3DSurface, &clipper);
1702 /* Get the surface properties */
1703 hr = IWineD3DSurface_GetDesc(wineD3DSurface, &Desc);
1704 if(hr != D3D_OK) return hr;
1706 Format = Desc.format;
1707 Usage = Desc.usage;
1708 Pool = Desc.pool;
1709 MultiSampleType = Desc.multisample_type;
1710 MultiSampleQuality = Desc.multisample_quality;
1711 Width = Desc.width;
1712 Height = Desc.height;
1714 if(swapchain) {
1715 /* If there's a swapchain, it owns the IParent interface. Create a new one for the
1716 * new surface
1718 parImpl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*parImpl));
1719 parImpl->lpVtbl = &IParent_Vtbl;
1720 parImpl->ref = 1;
1722 Parent = (IUnknown *) parImpl;
1725 /* Create the new surface */
1726 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice, Width, Height, Format,
1727 TRUE /* Lockable */, FALSE /* Discard */, surfImpl->mipmap_level, &surfImpl->WineD3DSurface,
1728 Usage, Pool, MultiSampleType, MultiSampleQuality, This->ImplType, Parent);
1730 if(hr != D3D_OK)
1731 return hr;
1733 IWineD3DSurface_SetClipper(surfImpl->WineD3DSurface, clipper);
1735 /* Update the IParent if it exists */
1736 if(parImpl)
1738 parImpl->child = (IUnknown *) surfImpl->WineD3DSurface;
1739 /* Add a reference for the IParent */
1740 IWineD3DSurface_AddRef(surfImpl->WineD3DSurface);
1742 /* TODO: Copy the surface content, except for render targets */
1744 /* If there's a swapchain, it owns the wined3d surfaces. So Destroy
1745 * the swapchain
1747 if(swapchain) {
1748 /* The backbuffers have the swapchain set as well, but the primary
1749 * owns it and destroys it
1751 if(surfImpl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
1752 IWineD3DDevice_UninitGDI(This->wineD3DDevice, D3D7CB_DestroySwapChain);
1754 surfImpl->isRenderTarget = FALSE;
1755 } else {
1756 if(IWineD3DSurface_Release(wineD3DSurface) == 0)
1757 TRACE("Surface released successful, next surface\n");
1758 else
1759 ERR("Something's still holding the old WineD3DSurface\n");
1762 surfImpl->ImplType = This->ImplType;
1764 if(clipper)
1766 IWineD3DClipper_Release(clipper);
1768 return DDENUMRET_OK;
1771 /*****************************************************************************
1772 * IDirectDrawImpl_RecreateAllSurfaces
1774 * A function, that converts all wineD3DSurfaces to the new implementation type
1775 * It enumerates all surfaces with IWineD3DDevice::EnumSurfaces, creates a
1776 * new WineD3DSurface, copies the content and releases the old surface
1778 *****************************************************************************/
1779 static HRESULT
1780 IDirectDrawImpl_RecreateAllSurfaces(IDirectDrawImpl *This)
1782 DDSURFACEDESC2 desc;
1783 TRACE("(%p): Switch to implementation %d\n", This, This->ImplType);
1785 if(This->ImplType != SURFACE_OPENGL && This->d3d_initialized)
1787 /* Should happen almost never */
1788 FIXME("(%p) Switching to non-opengl surfaces with d3d started. Is this a bug?\n", This);
1789 /* Shutdown d3d */
1790 IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroyDepthStencilSurface, D3D7CB_DestroySwapChain);
1792 /* Contrary: D3D starting is handled by the caller, because it knows the render target */
1794 memset(&desc, 0, sizeof(desc));
1795 desc.dwSize = sizeof(desc);
1797 return IDirectDraw7_EnumSurfaces((IDirectDraw7 *)This, 0, &desc, This, IDirectDrawImpl_RecreateSurfacesCallback);
1800 ULONG WINAPI D3D7CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
1801 IUnknown* swapChainParent;
1802 TRACE("(%p) call back\n", pSwapChain);
1804 IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
1805 IUnknown_Release(swapChainParent);
1806 return IUnknown_Release(swapChainParent);
1809 ULONG WINAPI D3D7CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
1810 IUnknown* surfaceParent;
1811 TRACE("(%p) call back\n", pSurface);
1813 IWineD3DSurface_GetParent(pSurface, &surfaceParent);
1814 IUnknown_Release(surfaceParent);
1815 return IUnknown_Release(surfaceParent);
1818 /*****************************************************************************
1819 * IDirectDrawImpl_CreateNewSurface
1821 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
1822 * with the passed parameters.
1824 * Params:
1825 * DDSD: Description of the surface to create
1826 * Surf: Address to store the interface pointer at
1828 * Returns:
1829 * DD_OK on success
1831 *****************************************************************************/
1832 static HRESULT
1833 IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This,
1834 DDSURFACEDESC2 *pDDSD,
1835 IDirectDrawSurfaceImpl **ppSurf,
1836 UINT level)
1838 HRESULT hr;
1839 UINT Width, Height;
1840 WINED3DFORMAT Format = WINED3DFMT_UNKNOWN;
1841 DWORD Usage = 0;
1842 WINED3DSURFTYPE ImplType = This->ImplType;
1843 WINED3DSURFACE_DESC Desc;
1844 IUnknown *Parent;
1845 IParentImpl *parImpl = NULL;
1846 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
1848 if (TRACE_ON(ddraw))
1850 TRACE(" (%p) Requesting surface desc :\n", This);
1851 DDRAW_dump_surface_desc(pDDSD);
1854 /* Select the surface type, if it wasn't choosen yet */
1855 if(ImplType == SURFACE_UNKNOWN)
1857 /* Use GL Surfaces if a D3DDEVICE Surface is requested */
1858 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1860 TRACE("(%p) Choosing GL surfaces because a 3DDEVICE Surface was requested\n", This);
1861 ImplType = SURFACE_OPENGL;
1864 /* Otherwise use GDI surfaces for now */
1865 else
1867 TRACE("(%p) Choosing GDI surfaces for 2D rendering\n", This);
1868 ImplType = SURFACE_GDI;
1871 /* Policy if all surface implementations are available:
1872 * First, check if a default type was set with winecfg. If not,
1873 * try Xrender surfaces, and use them if they work. Next, check if
1874 * accelerated OpenGL is available, and use GL surfaces in this
1875 * case. If all else fails, use GDI surfaces. If a 3DDEVICE surface
1876 * was created, always use OpenGL surfaces.
1878 * (Note: Xrender surfaces are not implemented for now, the
1879 * unaccelerated implementation uses GDI to render in Software)
1882 /* Store the type. If it needs to be changed, all WineD3DSurfaces have to
1883 * be re-created. This could be done with IDirectDrawSurface7::Restore
1885 This->ImplType = ImplType;
1887 else
1889 if((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE ) &&
1890 (This->ImplType != SURFACE_OPENGL ) && DefaultSurfaceType == SURFACE_UNKNOWN)
1892 /* We have to change to OpenGL,
1893 * and re-create all WineD3DSurfaces
1895 ImplType = SURFACE_OPENGL;
1896 This->ImplType = ImplType;
1897 TRACE("(%p) Re-creating all surfaces\n", This);
1898 IDirectDrawImpl_RecreateAllSurfaces(This);
1899 TRACE("(%p) Done recreating all surfaces\n", This);
1901 else if(This->ImplType != SURFACE_OPENGL && pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1903 WARN("The application requests a 3D capable surface, but a non-opengl surface was set in the registry\n");
1904 /* Do not fail surface creation, only fail 3D device creation */
1908 if (!(pDDSD->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY)) &&
1909 !((pDDSD->ddsCaps.dwCaps & DDSCAPS_TEXTURE) && (pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)) )
1911 /* Tests show surfaces without memory flags get these flags added right after creation. */
1912 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
1914 /* Get the correct wined3d usage */
1915 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE |
1916 DDSCAPS_3DDEVICE ) )
1918 Usage |= WINED3DUSAGE_RENDERTARGET;
1920 pDDSD->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
1922 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
1924 Usage |= WINED3DUSAGE_OVERLAY;
1926 if(This->depthstencil || (pDDSD->ddsCaps.dwCaps & DDSCAPS_ZBUFFER) )
1928 /* The depth stencil creation callback sets this flag.
1929 * Set the WineD3D usage to let it know that it's a depth
1930 * Stencil surface.
1932 Usage |= WINED3DUSAGE_DEPTHSTENCIL;
1934 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
1936 Pool = WINED3DPOOL_SYSTEMMEM;
1938 else if(pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
1940 Pool = WINED3DPOOL_MANAGED;
1941 /* Managed textures have the system memory flag set */
1942 pDDSD->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
1944 else if(pDDSD->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
1946 /* Videomemory adds localvidmem, this is mutually exclusive with systemmemory
1947 * and texturemanage
1949 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
1952 Format = PixelFormat_DD2WineD3D(&pDDSD->u4.ddpfPixelFormat);
1953 if(Format == WINED3DFMT_UNKNOWN)
1955 ERR("Unsupported / Unknown pixelformat\n");
1956 return DDERR_INVALIDPIXELFORMAT;
1959 /* Create the Surface object */
1960 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
1961 if(!*ppSurf)
1963 ERR("(%p) Error allocating memory for a surface\n", This);
1964 return DDERR_OUTOFVIDEOMEMORY;
1966 (*ppSurf)->lpVtbl = &IDirectDrawSurface7_Vtbl;
1967 (*ppSurf)->IDirectDrawSurface3_vtbl = &IDirectDrawSurface3_Vtbl;
1968 (*ppSurf)->IDirectDrawGammaControl_vtbl = &IDirectDrawGammaControl_Vtbl;
1969 (*ppSurf)->IDirect3DTexture2_vtbl = &IDirect3DTexture2_Vtbl;
1970 (*ppSurf)->IDirect3DTexture_vtbl = &IDirect3DTexture1_Vtbl;
1971 (*ppSurf)->ref = 1;
1972 (*ppSurf)->version = 7;
1973 TRACE("%p->version = %d\n", (*ppSurf), (*ppSurf)->version);
1974 (*ppSurf)->ddraw = This;
1975 (*ppSurf)->surface_desc.dwSize = sizeof(DDSURFACEDESC2);
1976 (*ppSurf)->surface_desc.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1977 DD_STRUCT_COPY_BYSIZE(&(*ppSurf)->surface_desc, pDDSD);
1979 /* Surface attachments */
1980 (*ppSurf)->next_attached = NULL;
1981 (*ppSurf)->first_attached = *ppSurf;
1983 /* Needed to re-create the surface on an implementation change */
1984 (*ppSurf)->ImplType = ImplType;
1986 /* For D3DDevice creation */
1987 (*ppSurf)->isRenderTarget = FALSE;
1989 /* A trace message for debugging */
1990 TRACE("(%p) Created IDirectDrawSurface implementation structure at %p\n", This, *ppSurf);
1992 if(pDDSD->ddsCaps.dwCaps & ( DDSCAPS_PRIMARYSURFACE | DDSCAPS_TEXTURE | DDSCAPS_3DDEVICE) )
1994 /* Render targets and textures need a IParent interface,
1995 * because WineD3D will destroy them when the swapchain
1996 * is released
1998 parImpl = HeapAlloc(GetProcessHeap(), 0, sizeof(IParentImpl));
1999 if(!parImpl)
2001 ERR("Out of memory when allocating memory for a IParent implementation\n");
2002 return DDERR_OUTOFMEMORY;
2004 parImpl->ref = 1;
2005 parImpl->lpVtbl = &IParent_Vtbl;
2006 Parent = (IUnknown *)parImpl;
2007 TRACE("Using IParent interface %p as parent\n", parImpl);
2009 else
2011 /* Use the surface as parent */
2012 Parent = (IUnknown *)*ppSurf;
2013 TRACE("Using Surface interface %p as parent\n", *ppSurf);
2016 /* Now create the WineD3D Surface */
2017 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice, pDDSD->dwWidth, pDDSD->dwHeight, Format,
2018 TRUE /* Lockable */, FALSE /* Discard */, level, &(*ppSurf)->WineD3DSurface,
2019 Usage, Pool, WINED3DMULTISAMPLE_NONE, 0 /* MultiSampleQuality */, ImplType, Parent);
2021 if(hr != D3D_OK)
2023 ERR("IWineD3DDevice::CreateSurface failed. hr = %08x\n", hr);
2024 return hr;
2027 /* Set the child of the parent implementation if it exists */
2028 if(parImpl)
2030 parImpl->child = (IUnknown *) (*ppSurf)->WineD3DSurface;
2031 /* The IParent releases the WineD3DSurface, and
2032 * the ddraw surface does that too. Hold a reference
2034 IWineD3DSurface_AddRef((*ppSurf)->WineD3DSurface);
2037 /* Increase the surface counter, and attach the surface */
2038 InterlockedIncrement(&This->surfaces);
2039 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
2041 /* Here we could store all created surfaces in the DirectDrawImpl structure,
2042 * But this could also be delegated to WineDDraw, as it keeps track of all its
2043 * resources. Not implemented for now, as there are more important things ;)
2046 /* Get the pixel format of the WineD3DSurface and store it.
2047 * Don't use the Format choosen above, WineD3D might have
2048 * changed it
2050 (*ppSurf)->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
2051 hr = IWineD3DSurface_GetDesc((*ppSurf)->WineD3DSurface, &Desc);
2052 if(hr != D3D_OK)
2054 ERR("IWineD3DSurface::GetDesc failed\n");
2055 IDirectDrawSurface7_Release( (IDirectDrawSurface7 *) *ppSurf);
2056 return hr;
2059 Format = Desc.format;
2060 Width = Desc.width;
2061 Height = Desc.height;
2063 if(Format == WINED3DFMT_UNKNOWN)
2065 FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN\n");
2067 PixelFormat_WineD3DtoDD( &(*ppSurf)->surface_desc.u4.ddpfPixelFormat, Format);
2069 /* Anno 1602 stores the pitch right after surface creation, so make sure it's there.
2070 * I can't LockRect() the surface here because if OpenGL surfaces are in use, the
2071 * WineD3DDevice might not be usable for 3D yet, so an extra method was created.
2072 * TODO: Test other fourcc formats
2074 if(Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3 ||
2075 Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5)
2077 (*ppSurf)->surface_desc.dwFlags |= DDSD_LINEARSIZE;
2078 if(Format == WINED3DFMT_DXT1)
2080 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height) / 2;
2082 else
2084 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height);
2087 else
2089 (*ppSurf)->surface_desc.dwFlags |= DDSD_PITCH;
2090 (*ppSurf)->surface_desc.u1.lPitch = IWineD3DSurface_GetPitch((*ppSurf)->WineD3DSurface);
2093 /* Application passed a color key? Set it! */
2094 if(pDDSD->dwFlags & DDSD_CKDESTOVERLAY)
2096 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2097 DDCKEY_DESTOVERLAY,
2098 (WINEDDCOLORKEY *) &pDDSD->u3.ddckCKDestOverlay);
2100 if(pDDSD->dwFlags & DDSD_CKDESTBLT)
2102 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2103 DDCKEY_DESTBLT,
2104 (WINEDDCOLORKEY *) &pDDSD->ddckCKDestBlt);
2106 if(pDDSD->dwFlags & DDSD_CKSRCOVERLAY)
2108 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2109 DDCKEY_SRCOVERLAY,
2110 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcOverlay);
2112 if(pDDSD->dwFlags & DDSD_CKSRCBLT)
2114 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2115 DDCKEY_SRCBLT,
2116 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcBlt);
2118 if ( pDDSD->dwFlags & DDSD_LPSURFACE)
2120 hr = IWineD3DSurface_SetMem((*ppSurf)->WineD3DSurface, pDDSD->lpSurface);
2121 if(hr != WINED3D_OK)
2123 /* No need for a trace here, wined3d does that for us */
2124 IDirectDrawSurface7_Release((IDirectDrawSurface7 *)*ppSurf);
2125 return hr;
2129 return DD_OK;
2131 /*****************************************************************************
2132 * CreateAdditionalSurfaces
2134 * Creates a new mipmap chain.
2136 * Params:
2137 * root: Root surface to attach the newly created chain to
2138 * count: number of surfaces to create
2139 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2140 * effects on the caller
2141 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2142 * creates an additional surface without the mipmapping flags
2144 *****************************************************************************/
2145 static HRESULT
2146 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2147 IDirectDrawSurfaceImpl *root,
2148 UINT count,
2149 DDSURFACEDESC2 DDSD,
2150 BOOL CubeFaceRoot)
2152 UINT i, j, level = 0;
2153 HRESULT hr;
2154 IDirectDrawSurfaceImpl *last = root;
2156 for(i = 0; i < count; i++)
2158 IDirectDrawSurfaceImpl *object2 = NULL;
2160 /* increase the mipmap level, but only if a mipmap is created
2161 * In this case, also halve the size
2163 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2165 level++;
2166 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2167 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2168 /* Set the mipmap sublevel flag according to msdn */
2169 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2171 else
2173 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2175 CubeFaceRoot = FALSE;
2177 hr = IDirectDrawImpl_CreateNewSurface(This,
2178 &DDSD,
2179 &object2,
2180 level);
2181 if(hr != DD_OK)
2183 return hr;
2186 /* Add the new surface to the complex attachment array */
2187 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2189 if(last->complex_array[j]) continue;
2190 last->complex_array[j] = object2;
2191 break;
2193 last = object2;
2195 /* Remove the (possible) back buffer cap from the new surface description,
2196 * because only one surface in the flipping chain is a back buffer, one
2197 * is a front buffer, the others are just primary surfaces.
2199 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2201 return DD_OK;
2204 /*****************************************************************************
2205 * IDirectDraw7::CreateSurface
2207 * Creates a new IDirectDrawSurface object and returns its interface.
2209 * The surface connections with wined3d are a bit tricky. Basically it works
2210 * like this:
2212 * |------------------------| |-----------------|
2213 * | DDraw surface | | WineD3DSurface |
2214 * | | | |
2215 * | WineD3DSurface |-------------->| |
2216 * | Child |<------------->| Parent |
2217 * |------------------------| |-----------------|
2219 * The DDraw surface is the parent of the wined3d surface, and it releases
2220 * the WineD3DSurface when the ddraw surface is destroyed.
2222 * However, for all surfaces which can be in a container in WineD3D,
2223 * we have to do this. These surfaces are usually complex surfaces,
2224 * so this concerns primary surfaces with a front and a back buffer,
2225 * and textures.
2227 * |------------------------| |-----------------|
2228 * | DDraw surface | | Container |
2229 * | | | |
2230 * | Child |<------------->| Parent |
2231 * | Texture |<------------->| |
2232 * | WineD3DSurface |<----| | Levels |<--|
2233 * | Complex connection | | | | |
2234 * |------------------------| | |-----------------| |
2235 * ^ | |
2236 * | | |
2237 * | | |
2238 * | |------------------| | |-----------------| |
2239 * | | IParent | |-------->| WineD3DSurface | |
2240 * | | | | | |
2241 * | | Child |<------------->| Parent | |
2242 * | | | | Container |<--|
2243 * | |------------------| |-----------------| |
2244 * | |
2245 * | |----------------------| |
2246 * | | DDraw surface 2 | |
2247 * | | | |
2248 * |<->| Complex root Child | |
2249 * | | Texture | |
2250 * | | WineD3DSurface |<----| |
2251 * | |----------------------| | |
2252 * | | |
2253 * | |---------------------| | |-----------------| |
2254 * | | IParent | |----->| WineD3DSurface | |
2255 * | | | | | |
2256 * | | Child |<---------->| Parent | |
2257 * | |---------------------| | Container |<--|
2258 * | |-----------------| |
2259 * | |
2260 * | ---More surfaces can follow--- |
2262 * The reason is that the IWineD3DSwapchain(render target container)
2263 * and the IWineD3DTexure(Texture container) release the parents
2264 * of their surface's children, but by releasing the complex root
2265 * the surfaces which are complexly attached to it are destroyed
2266 * too. See IDirectDrawSurface::Release for a more detailed
2267 * explanation.
2269 * Params:
2270 * DDSD: Description of the surface to create
2271 * Surf: Address to store the interface pointer at
2272 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2273 * aggregation, so it has to be NULL
2275 * Returns:
2276 * DD_OK on success
2277 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2278 * DDERR_* if an error occurs
2280 *****************************************************************************/
2281 static HRESULT WINAPI
2282 IDirectDrawImpl_CreateSurface(IDirectDraw7 *iface,
2283 DDSURFACEDESC2 *DDSD,
2284 IDirectDrawSurface7 **Surf,
2285 IUnknown *UnkOuter)
2287 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
2288 IDirectDrawSurfaceImpl *object = NULL;
2289 HRESULT hr;
2290 LONG extra_surfaces = 0;
2291 DDSURFACEDESC2 desc2;
2292 WINED3DDISPLAYMODE Mode;
2293 const DWORD sysvidmem = DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
2295 TRACE("(%p)->(%p,%p,%p)\n", This, DDSD, Surf, UnkOuter);
2297 /* Some checks before we start */
2298 if (TRACE_ON(ddraw))
2300 TRACE(" (%p) Requesting surface desc :\n", This);
2301 DDRAW_dump_surface_desc(DDSD);
2303 EnterCriticalSection(&ddraw_cs);
2305 if (UnkOuter != NULL)
2307 FIXME("(%p) : outer != NULL?\n", This);
2308 LeaveCriticalSection(&ddraw_cs);
2309 return CLASS_E_NOAGGREGATION; /* unchecked */
2312 if (Surf == NULL)
2314 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", This);
2315 LeaveCriticalSection(&ddraw_cs);
2316 return E_POINTER; /* unchecked */
2319 if (!(DDSD->dwFlags & DDSD_CAPS))
2321 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2322 DDSD->dwFlags |= DDSD_CAPS;
2325 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2327 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2328 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2331 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2333 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2334 WARN("(%p) Null surface pointer specified, ignore it!\n", This);
2335 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2338 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2339 !(This->cooperative_level & DDSCL_EXCLUSIVE))
2341 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n", This);
2342 *Surf = NULL;
2343 LeaveCriticalSection(&ddraw_cs);
2344 return DDERR_NOEXCLUSIVEMODE;
2347 if(DDSD->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER)) {
2348 WARN("Application tried to create an explicit front or back buffer\n");
2349 LeaveCriticalSection(&ddraw_cs);
2350 return DDERR_INVALIDCAPS;
2353 if((DDSD->ddsCaps.dwCaps & sysvidmem) == sysvidmem)
2355 /* This is a special switch in ddrawex.dll, but not allowed in ddraw.dll */
2356 WARN("Application tries to put the surface in both system and video memory\n");
2357 LeaveCriticalSection(&ddraw_cs);
2358 *Surf = NULL;
2359 return DDERR_INVALIDCAPS;
2362 /* Check cube maps but only if the size includes them */
2363 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2365 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2366 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2368 WARN("Cube map faces requested without cube map flag\n");
2369 LeaveCriticalSection(&ddraw_cs);
2370 return DDERR_INVALIDCAPS;
2372 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2373 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2375 WARN("Cube map without faces requested\n");
2376 LeaveCriticalSection(&ddraw_cs);
2377 return DDERR_INVALIDPARAMS;
2380 /* Quick tests confirm those can be created, but we don't do that yet */
2381 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2382 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2384 FIXME("Partial cube maps not supported yet\n");
2388 /* According to the msdn this flag is ignored by CreateSurface */
2389 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2390 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2392 /* Modify some flags */
2393 memset(&desc2, 0, sizeof(desc2));
2394 desc2.dwSize = sizeof(desc2); /* For the struct copy */
2395 DD_STRUCT_COPY_BYSIZE(&desc2, DDSD);
2396 desc2.dwSize = sizeof(desc2); /* To override a possibly smaller size */
2397 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2399 /* Get the video mode from WineD3D - we will need it */
2400 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
2401 0, /* Swapchain 0 */
2402 &Mode);
2403 if(FAILED(hr))
2405 ERR("Failed to read display mode from wined3d\n");
2406 switch(This->orig_bpp)
2408 case 8:
2409 Mode.Format = WINED3DFMT_P8;
2410 break;
2412 case 15:
2413 Mode.Format = WINED3DFMT_X1R5G5B5;
2414 break;
2416 case 16:
2417 Mode.Format = WINED3DFMT_R5G6B5;
2418 break;
2420 case 24:
2421 Mode.Format = WINED3DFMT_R8G8B8;
2422 break;
2424 case 32:
2425 Mode.Format = WINED3DFMT_X8R8G8B8;
2426 break;
2428 Mode.Width = This->orig_width;
2429 Mode.Height = This->orig_height;
2432 /* No pixelformat given? Use the current screen format */
2433 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2435 desc2.dwFlags |= DDSD_PIXELFORMAT;
2436 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2438 /* Wait: It could be a Z buffer */
2439 if(desc2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
2441 switch(desc2.u2.dwMipMapCount) /* Who had this glorious idea? */
2443 case 15:
2444 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D15S1);
2445 break;
2446 case 16:
2447 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D16_UNORM);
2448 break;
2449 case 24:
2450 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D24X8);
2451 break;
2452 case 32:
2453 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D32);
2454 break;
2455 default:
2456 ERR("Unknown Z buffer bit depth\n");
2459 else
2461 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
2465 /* No Width or no Height? Use the original screen size
2467 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2468 !(desc2.dwFlags & DDSD_HEIGHT) )
2470 /* Invalid for non-render targets */
2471 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2473 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2474 *Surf = NULL;
2475 LeaveCriticalSection(&ddraw_cs);
2476 return DDERR_INVALIDPARAMS;
2479 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2480 desc2.dwWidth = Mode.Width;
2481 desc2.dwHeight = Mode.Height;
2484 /* Mipmap count fixes */
2485 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2487 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2489 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2491 /* Mipmap count is given, should not be 0 */
2492 if( desc2.u2.dwMipMapCount == 0 )
2494 LeaveCriticalSection(&ddraw_cs);
2495 return DDERR_INVALIDPARAMS;
2498 else
2500 /* Undocumented feature: Create sublevels until
2501 * either the width or the height is 1
2503 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2504 desc2.dwWidth : desc2.dwHeight;
2505 desc2.u2.dwMipMapCount = 0;
2506 while( min )
2508 desc2.u2.dwMipMapCount += 1;
2509 min >>= 1;
2513 else
2515 /* Not-complex mipmap -> Mipmapcount = 1 */
2516 desc2.u2.dwMipMapCount = 1;
2518 extra_surfaces = desc2.u2.dwMipMapCount - 1;
2520 /* There's a mipmap count in the created surface in any case */
2521 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2523 /* If no mipmap is given, the texture has only one level */
2525 /* The first surface is a front buffer, the back buffer is created afterwards */
2526 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2528 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2531 /* The root surface in a cube map is positive x */
2532 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2534 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2535 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2538 /* Create the first surface */
2539 hr = IDirectDrawImpl_CreateNewSurface(This, &desc2, &object, 0);
2540 if( hr != DD_OK)
2542 ERR("IDirectDrawImpl_CreateNewSurface failed with %08x\n", hr);
2543 LeaveCriticalSection(&ddraw_cs);
2544 return hr;
2546 object->is_complex_root = TRUE;
2548 *Surf = (IDirectDrawSurface7 *)object;
2550 /* Create Additional surfaces if necessary
2551 * This applies to Primary surfaces which have a back buffer count
2552 * set, but not to mipmap textures. In case of Mipmap textures,
2553 * wineD3D takes care of the creation of additional surfaces
2555 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2557 extra_surfaces = DDSD->dwBackBufferCount;
2558 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2559 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2562 hr = DD_OK;
2563 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2565 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2566 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
2567 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2568 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
2569 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
2570 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2571 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
2572 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
2573 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2574 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
2575 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
2576 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2577 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
2578 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
2579 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2580 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
2581 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2584 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces, desc2, FALSE);
2585 if(hr != DD_OK)
2587 /* This destroys and possibly created surfaces too */
2588 IDirectDrawSurface_Release((IDirectDrawSurface7 *)object);
2589 LeaveCriticalSection(&ddraw_cs);
2590 return hr;
2593 /* If the implementation is OpenGL and there's no d3ddevice, attach a d3ddevice
2594 * But attach the d3ddevice only if the currently created surface was
2595 * a primary surface (2D app in 3D mode) or a 3DDEVICE surface (3D app)
2596 * The only case I can think of where this doesn't apply is when a
2597 * 2D app was configured by the user to run with OpenGL and it didn't create
2598 * the render target as first surface. In this case the render target creation
2599 * will cause the 3D init.
2601 if( (This->ImplType == SURFACE_OPENGL) && !(This->d3d_initialized) &&
2602 desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE) )
2604 IDirectDrawSurfaceImpl *target = object, *surface;
2605 struct list *entry;
2607 /* Search for the primary to use as render target */
2608 LIST_FOR_EACH(entry, &This->surface_list)
2610 surface = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2611 if((surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER)) ==
2612 (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER))
2614 /* found */
2615 target = surface;
2616 TRACE("Using primary %p as render target\n", target);
2617 break;
2621 TRACE("(%p) Attaching a D3DDevice, rendertarget = %p\n", This, target);
2622 hr = IDirectDrawImpl_AttachD3DDevice(This, target);
2623 if(hr != D3D_OK)
2625 IDirectDrawSurfaceImpl *release_surf;
2626 ERR("IDirectDrawImpl_AttachD3DDevice failed, hr = %x\n", hr);
2627 *Surf = NULL;
2629 /* The before created surface structures are in an incomplete state here.
2630 * WineD3D holds the reference on the IParents, and it released them on the failure
2631 * already. So the regular release method implementation would fail on the attempt
2632 * to destroy either the IParents or the swapchain. So free the surface here.
2633 * The surface structure here is a list, not a tree, because onscreen targets
2634 * cannot be cube textures
2636 while(object)
2638 release_surf = object;
2639 object = object->complex_array[0];
2640 IDirectDrawSurfaceImpl_Destroy(release_surf);
2642 LeaveCriticalSection(&ddraw_cs);
2643 return hr;
2645 } else if(!(This->d3d_initialized) && desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
2646 IDirectDrawImpl_CreateGDISwapChain(This, object);
2649 /* Addref the ddraw interface to keep an reference for each surface */
2650 IDirectDraw7_AddRef(iface);
2651 object->ifaceToRelease = (IUnknown *) iface;
2653 /* Create a WineD3DTexture if a texture was requested */
2654 if(desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
2656 UINT levels;
2657 WINED3DFORMAT Format;
2658 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
2660 This->tex_root = object;
2662 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2664 /* a mipmap is created, create enough levels */
2665 levels = desc2.u2.dwMipMapCount;
2667 else
2669 /* No mipmap is created, create one level */
2670 levels = 1;
2673 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM */
2674 if(DDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
2676 Pool = WINED3DPOOL_SYSTEMMEM;
2678 /* Should I forward the MANAGED cap to the managed pool ? */
2680 /* Get the format. It's set already by CreateNewSurface */
2681 Format = PixelFormat_DD2WineD3D(&object->surface_desc.u4.ddpfPixelFormat);
2683 /* The surfaces are already created, the callback only
2684 * passes the IWineD3DSurface to WineD3D
2686 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2688 hr = IWineD3DDevice_CreateCubeTexture(This->wineD3DDevice, DDSD->dwWidth /* Edgelength */, levels,
2689 0 /* usage */, Format, Pool, (IWineD3DCubeTexture **)&object->wineD3DTexture, (IUnknown *)object);
2691 else
2693 hr = IWineD3DDevice_CreateTexture(This->wineD3DDevice, DDSD->dwWidth, DDSD->dwHeight, levels,
2694 0 /* usage */, Format, Pool, (IWineD3DTexture **)&object->wineD3DTexture, (IUnknown *)object);
2696 This->tex_root = NULL;
2699 LeaveCriticalSection(&ddraw_cs);
2700 return hr;
2703 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
2704 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
2706 static BOOL
2707 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
2708 const DDPIXELFORMAT *provided)
2710 /* Some flags must be present in both or neither for a match. */
2711 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
2712 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
2713 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
2715 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2716 return FALSE;
2718 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
2719 return FALSE;
2721 if (requested->dwFlags & DDPF_FOURCC)
2722 if (requested->dwFourCC != provided->dwFourCC)
2723 return FALSE;
2725 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
2726 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2727 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
2728 return FALSE;
2730 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2731 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2732 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
2733 return FALSE;
2735 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
2736 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
2737 return FALSE;
2739 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
2740 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2741 |DDPF_BUMPDUDV))
2742 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
2743 return FALSE;
2745 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
2746 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
2747 return FALSE;
2749 return TRUE;
2752 static BOOL
2753 IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested,
2754 const DDSURFACEDESC2* provided)
2756 struct compare_info
2758 DWORD flag;
2759 ptrdiff_t offset;
2760 size_t size;
2763 #define CMP(FLAG, FIELD) \
2764 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
2765 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
2767 static const struct compare_info compare[] =
2769 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
2770 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
2771 CMP(CAPS, ddsCaps),
2772 CMP(CKDESTBLT, ddckCKDestBlt),
2773 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
2774 CMP(CKSRCBLT, ddckCKSrcBlt),
2775 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
2776 CMP(HEIGHT, dwHeight),
2777 CMP(LINEARSIZE, u1 /* dwLinearSize */),
2778 CMP(LPSURFACE, lpSurface),
2779 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
2780 CMP(PITCH, u1 /* lPitch */),
2781 /* PIXELFORMAT: manual */
2782 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
2783 CMP(TEXTURESTAGE, dwTextureStage),
2784 CMP(WIDTH, dwWidth),
2785 /* ZBUFFERBITDEPTH: "obsolete" */
2788 #undef CMP
2790 unsigned int i;
2792 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2793 return FALSE;
2795 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
2797 if (requested->dwFlags & compare[i].flag
2798 && memcmp((const char *)provided + compare[i].offset,
2799 (const char *)requested + compare[i].offset,
2800 compare[i].size) != 0)
2801 return FALSE;
2804 if (requested->dwFlags & DDSD_PIXELFORMAT)
2806 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
2807 &provided->u4.ddpfPixelFormat))
2808 return FALSE;
2811 return TRUE;
2814 #undef DDENUMSURFACES_SEARCHTYPE
2815 #undef DDENUMSURFACES_MATCHTYPE
2817 /*****************************************************************************
2818 * IDirectDraw7::EnumSurfaces
2820 * Loops through all surfaces attached to this device and calls the
2821 * application callback. This can't be relayed to WineD3DDevice,
2822 * because some WineD3DSurfaces' parents are IParent objects
2824 * Params:
2825 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
2826 * DDSD: Description to filter for
2827 * Context: Application-provided pointer, it's passed unmodified to the
2828 * Callback function
2829 * Callback: Address to call for each surface
2831 * Returns:
2832 * DDERR_INVALIDPARAMS if the callback is NULL
2833 * DD_OK on success
2835 *****************************************************************************/
2836 static HRESULT WINAPI
2837 IDirectDrawImpl_EnumSurfaces(IDirectDraw7 *iface,
2838 DWORD Flags,
2839 DDSURFACEDESC2 *DDSD,
2840 void *Context,
2841 LPDDENUMSURFACESCALLBACK7 Callback)
2843 /* The surface enumeration is handled by WineDDraw,
2844 * because it keeps track of all surfaces attached to
2845 * it. The filtering is done by our callback function,
2846 * because WineDDraw doesn't handle ddraw-like surface
2847 * caps structures
2849 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
2850 IDirectDrawSurfaceImpl *surf;
2851 BOOL all, nomatch;
2852 DDSURFACEDESC2 desc;
2853 struct list *entry, *entry2;
2855 all = Flags & DDENUMSURFACES_ALL;
2856 nomatch = Flags & DDENUMSURFACES_NOMATCH;
2858 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, DDSD, Context, Callback);
2859 EnterCriticalSection(&ddraw_cs);
2861 if(!Callback)
2863 LeaveCriticalSection(&ddraw_cs);
2864 return DDERR_INVALIDPARAMS;
2867 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
2868 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
2870 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2871 if (all || (nomatch != IDirectDrawImpl_DDSD_Match(DDSD, &surf->surface_desc)))
2873 desc = surf->surface_desc;
2874 IDirectDrawSurface7_AddRef((IDirectDrawSurface7 *)surf);
2875 if (Callback((IDirectDrawSurface7 *)surf, &desc, Context) != DDENUMRET_OK)
2877 LeaveCriticalSection(&ddraw_cs);
2878 return DD_OK;
2882 LeaveCriticalSection(&ddraw_cs);
2883 return DD_OK;
2886 static HRESULT WINAPI
2887 findRenderTarget(IDirectDrawSurface7 *surface,
2888 DDSURFACEDESC2 *desc,
2889 void *ctx)
2891 IDirectDrawSurfaceImpl *surf = (IDirectDrawSurfaceImpl *)surface;
2892 IDirectDrawSurfaceImpl **target = ctx;
2894 if(!surf->isRenderTarget) {
2895 *target = surf;
2896 IDirectDrawSurface7_Release(surface);
2897 return DDENUMRET_CANCEL;
2900 /* Recurse into the surface tree */
2901 IDirectDrawSurface7_EnumAttachedSurfaces(surface, ctx, findRenderTarget);
2903 IDirectDrawSurface7_Release(surface);
2904 if(*target) return DDENUMRET_CANCEL;
2905 else return DDENUMRET_OK; /* Continue with the next neighbor surface */
2908 static HRESULT IDirectDrawImpl_CreateGDISwapChain(IDirectDrawImpl *This,
2909 IDirectDrawSurfaceImpl *primary) {
2910 HRESULT hr;
2911 WINED3DPRESENT_PARAMETERS presentation_parameters;
2912 HWND window;
2914 window = This->dest_window;
2916 memset(&presentation_parameters, 0, sizeof(presentation_parameters));
2918 /* Use the surface description for the device parameters, not the
2919 * Device settings. The app might render to an offscreen surface
2921 presentation_parameters.BackBufferWidth = primary->surface_desc.dwWidth;
2922 presentation_parameters.BackBufferHeight = primary->surface_desc.dwHeight;
2923 presentation_parameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2924 presentation_parameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
2925 presentation_parameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
2926 presentation_parameters.MultiSampleQuality = 0;
2927 presentation_parameters.SwapEffect = WINED3DSWAPEFFECT_FLIP;
2928 presentation_parameters.hDeviceWindow = window;
2929 presentation_parameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
2930 presentation_parameters.EnableAutoDepthStencil = FALSE; /* Not on GDI swapchains */
2931 presentation_parameters.AutoDepthStencilFormat = 0;
2932 presentation_parameters.Flags = 0;
2933 presentation_parameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
2934 presentation_parameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
2936 This->d3d_target = primary;
2937 hr = IWineD3DDevice_InitGDI(This->wineD3DDevice, &presentation_parameters);
2938 This->d3d_target = NULL;
2940 if (hr != D3D_OK)
2942 FIXME("(%p) call to IWineD3DDevice_InitGDI failed\n", This);
2943 primary->wineD3DSwapChain = NULL;
2945 return hr;
2948 /*****************************************************************************
2949 * IDirectDrawImpl_AttachD3DDevice
2951 * Initializes the D3D capabilities of WineD3D
2953 * Params:
2954 * primary: The primary surface for D3D
2956 * Returns
2957 * DD_OK on success,
2958 * DDERR_* otherwise
2960 *****************************************************************************/
2961 static HRESULT
2962 IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This,
2963 IDirectDrawSurfaceImpl *primary)
2965 HRESULT hr;
2966 HWND window = This->dest_window;
2968 WINED3DPRESENT_PARAMETERS localParameters;
2970 TRACE("(%p)->(%p)\n", This, primary);
2972 /* If there's no window, create a hidden window. WineD3D needs it */
2973 if(window == 0 || window == GetDesktopWindow())
2975 window = CreateWindowExA(0, This->classname, "Hidden D3D Window",
2976 WS_DISABLED, 0, 0,
2977 GetSystemMetrics(SM_CXSCREEN),
2978 GetSystemMetrics(SM_CYSCREEN),
2979 NULL, NULL, GetModuleHandleA(0), NULL);
2981 ShowWindow(window, SW_HIDE); /* Just to be sure */
2982 WARN("(%p) No window for the Direct3DDevice, created a hidden window. HWND=%p\n", This, window);
2984 else
2986 TRACE("(%p) Using existing window %p for Direct3D rendering\n", This, window);
2988 This->d3d_window = window;
2990 /* Store the future Render Target surface */
2991 This->d3d_target = primary;
2993 /* Use the surface description for the device parameters, not the
2994 * Device settings. The app might render to an offscreen surface
2996 localParameters.BackBufferWidth = primary->surface_desc.dwWidth;
2997 localParameters.BackBufferHeight = primary->surface_desc.dwHeight;
2998 localParameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2999 localParameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
3000 localParameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
3001 localParameters.MultiSampleQuality = 0;
3002 localParameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
3003 localParameters.hDeviceWindow = window;
3004 localParameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
3005 localParameters.EnableAutoDepthStencil = TRUE;
3006 localParameters.AutoDepthStencilFormat = WINED3DFMT_D16_UNORM;
3007 localParameters.Flags = 0;
3008 localParameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
3009 localParameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
3011 TRACE("Passing mode %d\n", localParameters.BackBufferFormat);
3013 /* Set this NOW, otherwise creating the depth stencil surface will cause a
3014 * recursive loop until ram or emulated video memory is full
3016 This->d3d_initialized = TRUE;
3018 hr = IWineD3DDevice_Init3D(This->wineD3DDevice, &localParameters);
3019 if(FAILED(hr))
3021 This->d3d_target = NULL;
3022 This->d3d_initialized = FALSE;
3023 return hr;
3026 This->declArraySize = 2;
3027 This->decls = HeapAlloc(GetProcessHeap(),
3028 HEAP_ZERO_MEMORY,
3029 sizeof(*This->decls) * This->declArraySize);
3030 if(!This->decls)
3032 ERR("Error allocating an array for the converted vertex decls\n");
3033 This->declArraySize = 0;
3034 hr = IWineD3DDevice_Uninit3D(This->wineD3DDevice,
3035 D3D7CB_DestroyDepthStencilSurface,
3036 D3D7CB_DestroySwapChain);
3037 return E_OUTOFMEMORY;
3040 /* Create an Index Buffer parent */
3041 TRACE("(%p) Successfully initialized 3D\n", This);
3042 return DD_OK;
3045 /*****************************************************************************
3046 * DirectDrawCreateClipper (DDRAW.@)
3048 * Creates a new IDirectDrawClipper object.
3050 * Params:
3051 * Clipper: Address to write the interface pointer to
3052 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3053 * NULL
3055 * Returns:
3056 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3057 * E_OUTOFMEMORY if allocating the object failed
3059 *****************************************************************************/
3060 HRESULT WINAPI
3061 DirectDrawCreateClipper(DWORD Flags,
3062 LPDIRECTDRAWCLIPPER *Clipper,
3063 IUnknown *UnkOuter)
3065 IDirectDrawClipperImpl* object;
3066 TRACE("(%08x,%p,%p)\n", Flags, Clipper, UnkOuter);
3068 EnterCriticalSection(&ddraw_cs);
3069 if (UnkOuter != NULL)
3071 LeaveCriticalSection(&ddraw_cs);
3072 return CLASS_E_NOAGGREGATION;
3075 if (!LoadWineD3D())
3077 LeaveCriticalSection(&ddraw_cs);
3078 return DDERR_NODIRECTDRAWSUPPORT;
3081 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3082 sizeof(IDirectDrawClipperImpl));
3083 if (object == NULL)
3085 LeaveCriticalSection(&ddraw_cs);
3086 return E_OUTOFMEMORY;
3089 object->lpVtbl = &IDirectDrawClipper_Vtbl;
3090 object->ref = 1;
3091 object->wineD3DClipper = pWineDirect3DCreateClipper((IUnknown *) object);
3092 if(!object->wineD3DClipper)
3094 HeapFree(GetProcessHeap(), 0, object);
3095 LeaveCriticalSection(&ddraw_cs);
3096 return E_OUTOFMEMORY;
3099 *Clipper = (IDirectDrawClipper *) object;
3100 LeaveCriticalSection(&ddraw_cs);
3101 return DD_OK;
3104 /*****************************************************************************
3105 * IDirectDraw7::CreateClipper
3107 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3109 *****************************************************************************/
3110 static HRESULT WINAPI
3111 IDirectDrawImpl_CreateClipper(IDirectDraw7 *iface,
3112 DWORD Flags,
3113 IDirectDrawClipper **Clipper,
3114 IUnknown *UnkOuter)
3116 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
3117 TRACE("(%p)->(%x,%p,%p)\n", This, Flags, Clipper, UnkOuter);
3118 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3121 /*****************************************************************************
3122 * IDirectDraw7::CreatePalette
3124 * Creates a new IDirectDrawPalette object
3126 * Params:
3127 * Flags: The flags for the new clipper
3128 * ColorTable: Color table to assign to the new clipper
3129 * Palette: Address to write the interface pointer to
3130 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3131 * NULL
3133 * Returns:
3134 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3135 * E_OUTOFMEMORY if allocating the object failed
3137 *****************************************************************************/
3138 static HRESULT WINAPI
3139 IDirectDrawImpl_CreatePalette(IDirectDraw7 *iface,
3140 DWORD Flags,
3141 PALETTEENTRY *ColorTable,
3142 IDirectDrawPalette **Palette,
3143 IUnknown *pUnkOuter)
3145 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
3146 IDirectDrawPaletteImpl *object;
3147 HRESULT hr = DDERR_GENERIC;
3148 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, ColorTable, Palette, pUnkOuter);
3150 EnterCriticalSection(&ddraw_cs);
3151 if(pUnkOuter != NULL)
3153 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
3154 LeaveCriticalSection(&ddraw_cs);
3155 return CLASS_E_NOAGGREGATION;
3158 /* The refcount test shows that a cooplevel is required for this */
3159 if(!This->cooperative_level)
3161 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3162 LeaveCriticalSection(&ddraw_cs);
3163 return DDERR_NOCOOPERATIVELEVELSET;
3166 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
3167 if(!object)
3169 ERR("Out of memory when allocating memory for a palette implementation\n");
3170 LeaveCriticalSection(&ddraw_cs);
3171 return E_OUTOFMEMORY;
3174 object->lpVtbl = &IDirectDrawPalette_Vtbl;
3175 object->ref = 1;
3176 object->ddraw_owner = This;
3178 hr = IWineD3DDevice_CreatePalette(This->wineD3DDevice, Flags,
3179 ColorTable, &object->wineD3DPalette, (IUnknown *)object);
3180 if(hr != DD_OK)
3182 HeapFree(GetProcessHeap(), 0, object);
3183 LeaveCriticalSection(&ddraw_cs);
3184 return hr;
3187 IDirectDraw7_AddRef(iface);
3188 object->ifaceToRelease = (IUnknown *) iface;
3189 *Palette = (IDirectDrawPalette *)object;
3190 LeaveCriticalSection(&ddraw_cs);
3191 return DD_OK;
3194 /*****************************************************************************
3195 * IDirectDraw7::DuplicateSurface
3197 * Duplicates a surface. The surface memory points to the same memory as
3198 * the original surface, and it's released when the last surface referencing
3199 * it is released. I guess that's beyond Wine's surface management right now
3200 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3201 * test application to implement this)
3203 * Params:
3204 * Src: Address of the source surface
3205 * Dest: Address to write the new surface pointer to
3207 * Returns:
3208 * See IDirectDraw7::CreateSurface
3210 *****************************************************************************/
3211 static HRESULT WINAPI
3212 IDirectDrawImpl_DuplicateSurface(IDirectDraw7 *iface,
3213 IDirectDrawSurface7 *Src,
3214 IDirectDrawSurface7 **Dest)
3216 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
3217 IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Src;
3219 FIXME("(%p)->(%p,%p)\n", This, Surf, Dest);
3221 /* For now, simply create a new, independent surface */
3222 return IDirectDraw7_CreateSurface(iface,
3223 &Surf->surface_desc,
3224 Dest,
3225 NULL);
3228 /*****************************************************************************
3229 * IDirectDraw7 VTable
3230 *****************************************************************************/
3231 const IDirectDraw7Vtbl IDirectDraw7_Vtbl =
3233 /*** IUnknown ***/
3234 IDirectDrawImpl_QueryInterface,
3235 IDirectDrawImpl_AddRef,
3236 IDirectDrawImpl_Release,
3237 /*** IDirectDraw ***/
3238 IDirectDrawImpl_Compact,
3239 IDirectDrawImpl_CreateClipper,
3240 IDirectDrawImpl_CreatePalette,
3241 IDirectDrawImpl_CreateSurface,
3242 IDirectDrawImpl_DuplicateSurface,
3243 IDirectDrawImpl_EnumDisplayModes,
3244 IDirectDrawImpl_EnumSurfaces,
3245 IDirectDrawImpl_FlipToGDISurface,
3246 IDirectDrawImpl_GetCaps,
3247 IDirectDrawImpl_GetDisplayMode,
3248 IDirectDrawImpl_GetFourCCCodes,
3249 IDirectDrawImpl_GetGDISurface,
3250 IDirectDrawImpl_GetMonitorFrequency,
3251 IDirectDrawImpl_GetScanLine,
3252 IDirectDrawImpl_GetVerticalBlankStatus,
3253 IDirectDrawImpl_Initialize,
3254 IDirectDrawImpl_RestoreDisplayMode,
3255 IDirectDrawImpl_SetCooperativeLevel,
3256 IDirectDrawImpl_SetDisplayMode,
3257 IDirectDrawImpl_WaitForVerticalBlank,
3258 /*** IDirectDraw2 ***/
3259 IDirectDrawImpl_GetAvailableVidMem,
3260 /*** IDirectDraw3 ***/
3261 IDirectDrawImpl_GetSurfaceFromDC,
3262 /*** IDirectDraw4 ***/
3263 IDirectDrawImpl_RestoreAllSurfaces,
3264 IDirectDrawImpl_TestCooperativeLevel,
3265 IDirectDrawImpl_GetDeviceIdentifier,
3266 /*** IDirectDraw7 ***/
3267 IDirectDrawImpl_StartModeTest,
3268 IDirectDrawImpl_EvaluateMode
3271 /*****************************************************************************
3272 * IDirectDrawImpl_FindDecl
3274 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
3275 * if none was found.
3277 * This function is in ddraw.c and the DDraw object space because D3D7
3278 * vertex buffers are created using the IDirect3D interface to the ddraw
3279 * object, so they can be valid across D3D devices(theoretically. The ddraw
3280 * object also owns the wined3d device
3282 * Parameters:
3283 * This: Device
3284 * fvf: Fvf to find the decl for
3286 * Returns:
3287 * NULL in case of an error, the IWineD3DVertexDeclaration interface for the
3288 * fvf otherwise.
3290 *****************************************************************************/
3291 IWineD3DVertexDeclaration *
3292 IDirectDrawImpl_FindDecl(IDirectDrawImpl *This,
3293 DWORD fvf)
3295 HRESULT hr;
3296 IWineD3DVertexDeclaration* pDecl = NULL;
3297 int p, low, high; /* deliberately signed */
3298 struct FvfToDecl *convertedDecls = This->decls;
3300 TRACE("Searching for declaration for fvf %08x... ", fvf);
3302 low = 0;
3303 high = This->numConvertedDecls - 1;
3304 while(low <= high) {
3305 p = (low + high) >> 1;
3306 TRACE("%d ", p);
3307 if(convertedDecls[p].fvf == fvf) {
3308 TRACE("found %p\n", convertedDecls[p].decl);
3309 return convertedDecls[p].decl;
3310 } else if(convertedDecls[p].fvf < fvf) {
3311 low = p + 1;
3312 } else {
3313 high = p - 1;
3316 TRACE("not found. Creating and inserting at position %d.\n", low);
3318 hr = IWineD3DDevice_CreateVertexDeclarationFromFVF(This->wineD3DDevice, &pDecl, (IUnknown *)This, fvf);
3319 if (hr != S_OK) return NULL;
3321 if(This->declArraySize == This->numConvertedDecls) {
3322 int grow = max(This->declArraySize / 2, 8);
3323 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
3324 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
3325 if(!convertedDecls) {
3326 /* This will destroy it */
3327 IWineD3DVertexDeclaration_Release(pDecl);
3328 return NULL;
3330 This->decls = convertedDecls;
3331 This->declArraySize += grow;
3334 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
3335 convertedDecls[low].decl = pDecl;
3336 convertedDecls[low].fvf = fvf;
3337 This->numConvertedDecls++;
3339 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
3340 return pDecl;
3343 /* IWineD3DDeviceParent IUnknown methods */
3345 static inline struct IDirectDrawImpl *ddraw_from_device_parent(IWineD3DDeviceParent *iface)
3347 return (struct IDirectDrawImpl *)((char*)iface - FIELD_OFFSET(struct IDirectDrawImpl, device_parent_vtbl));
3350 static HRESULT STDMETHODCALLTYPE device_parent_QueryInterface(IWineD3DDeviceParent *iface, REFIID riid, void **object)
3352 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3353 return IDirectDrawImpl_QueryInterface((IDirectDraw7 *)This, riid, object);
3356 static ULONG STDMETHODCALLTYPE device_parent_AddRef(IWineD3DDeviceParent *iface)
3358 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3359 return IDirectDrawImpl_AddRef((IDirectDraw7 *)This);
3362 static ULONG STDMETHODCALLTYPE device_parent_Release(IWineD3DDeviceParent *iface)
3364 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3365 return IDirectDrawImpl_Release((IDirectDraw7 *)This);
3368 /* IWineD3DDeviceParent methods */
3370 static void STDMETHODCALLTYPE device_parent_WineD3DDeviceCreated(IWineD3DDeviceParent *iface, IWineD3DDevice *device)
3372 TRACE("iface %p, device %p\n", iface, device);
3375 static HRESULT STDMETHODCALLTYPE device_parent_CreateSurface(IWineD3DDeviceParent *iface,
3376 IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, DWORD usage,
3377 WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, IWineD3DSurface **surface)
3379 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3380 IDirectDrawSurfaceImpl *surf = NULL;
3381 UINT i = 0;
3382 DDSCAPS2 searchcaps = This->tex_root->surface_desc.ddsCaps;
3384 TRACE("iface %p, superior %p, width %u, height %u, format %#x, usage %#x,\n"
3385 "\tpool %#x, level %u, face %u, surface %p\n",
3386 iface, superior, width, height, format, usage, pool, level, face, surface);
3388 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
3389 switch(face)
3391 case WINED3DCUBEMAP_FACE_POSITIVE_X:
3392 TRACE("Asked for positive x\n");
3393 if (searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP)
3395 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
3397 surf = This->tex_root; break;
3398 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
3399 TRACE("Asked for negative x\n");
3400 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
3401 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
3402 TRACE("Asked for positive y\n");
3403 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
3404 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
3405 TRACE("Asked for negative y\n");
3406 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
3407 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
3408 TRACE("Asked for positive z\n");
3409 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
3410 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
3411 TRACE("Asked for negative z\n");
3412 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
3413 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
3416 if (!surf)
3418 IDirectDrawSurface7 *attached;
3419 IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)This->tex_root, &searchcaps, &attached);
3420 surf = (IDirectDrawSurfaceImpl *)attached;
3421 IDirectDrawSurface7_Release(attached);
3423 if (!surf) ERR("root search surface not found\n");
3425 /* Find the wanted mipmap. There are enough mipmaps in the chain */
3426 while (i < level)
3428 IDirectDrawSurface7 *attached;
3429 IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)surf, &searchcaps, &attached);
3430 if(!attached) ERR("Surface not found\n");
3431 surf = (IDirectDrawSurfaceImpl *)attached;
3432 IDirectDrawSurface7_Release(attached);
3433 ++i;
3436 /* Return the surface */
3437 *surface = surf->WineD3DSurface;
3439 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, surf);
3441 return D3D_OK;
3444 static HRESULT STDMETHODCALLTYPE device_parent_CreateRenderTarget(IWineD3DDeviceParent *iface,
3445 IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
3446 DWORD multisample_quality, BOOL lockable, IWineD3DSurface **surface)
3448 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3449 IDirectDrawSurfaceImpl *d3d_surface = This->d3d_target;
3450 IDirectDrawSurfaceImpl *target = NULL;
3452 TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3453 "\tmultisample_quality %u, lockable %u, surface %p\n",
3454 iface, superior, width, height, format, multisample_type, multisample_quality, lockable, surface);
3456 if (d3d_surface->isRenderTarget)
3458 IDirectDrawSurface7_EnumAttachedSurfaces((IDirectDrawSurface7 *)d3d_surface, &target, findRenderTarget);
3460 else
3462 target = d3d_surface;
3465 if (!target)
3467 target = This->d3d_target;
3468 ERR(" (%p) : No DirectDrawSurface found to create the back buffer. Using the front buffer as back buffer. Uncertain consequences\n", This);
3471 /* TODO: Return failure if the dimensions do not match, but this shouldn't happen */
3473 *surface = target->WineD3DSurface;
3474 target->isRenderTarget = TRUE;
3476 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, d3d_surface);
3478 return D3D_OK;
3481 static HRESULT STDMETHODCALLTYPE device_parent_CreateDepthStencilSurface(IWineD3DDeviceParent *iface,
3482 IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
3483 DWORD multisample_quality, BOOL discard, IWineD3DSurface **surface)
3485 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3486 /* Create a Depth Stencil surface to make WineD3D happy */
3487 DDSURFACEDESC2 ddsd;
3488 HRESULT hr;
3490 TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3491 "\tmultisample_quality %u, discard %u, surface %p\n",
3492 iface, superior, width, height, format, multisample_type, multisample_quality, discard, surface);
3494 *surface = NULL;
3496 /* Create a DirectDraw surface */
3497 memset(&ddsd, 0, sizeof(ddsd));
3498 ddsd.dwSize = sizeof(ddsd);
3499 ddsd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
3500 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
3501 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
3502 ddsd.dwHeight = height;
3503 ddsd.dwWidth = width;
3504 if (format)
3506 PixelFormat_WineD3DtoDD(&ddsd.u4.ddpfPixelFormat, format);
3508 else
3510 ddsd.dwFlags ^= DDSD_PIXELFORMAT;
3513 This->depthstencil = TRUE;
3514 hr = IDirectDraw7_CreateSurface((IDirectDraw7 *)This,
3515 &ddsd, (IDirectDrawSurface7 **)&This->DepthStencilBuffer, NULL);
3516 This->depthstencil = FALSE;
3517 if(FAILED(hr))
3519 ERR(" (%p) Creating a DepthStencil Surface failed, result = %x\n", This, hr);
3520 return hr;
3523 *surface = This->DepthStencilBuffer->WineD3DSurface;
3525 return D3D_OK;
3528 static HRESULT STDMETHODCALLTYPE device_parent_CreateVolume(IWineD3DDeviceParent *iface,
3529 IUnknown *superior, UINT width, UINT height, UINT depth, WINED3DFORMAT format,
3530 WINED3DPOOL pool, DWORD usage, IWineD3DVolume **volume)
3532 TRACE("iface %p, superior %p, width %u, height %u, depth %u, format %#x, pool %#x, usage %#x, volume %p\n",
3533 iface, superior, width, height, depth, format, pool, usage, volume);
3535 ERR("Not implemented!\n");
3537 return E_NOTIMPL;
3540 static HRESULT STDMETHODCALLTYPE device_parent_CreateSwapChain(IWineD3DDeviceParent *iface,
3541 WINED3DPRESENT_PARAMETERS *present_parameters, IWineD3DSwapChain **swapchain)
3543 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3544 IDirectDrawSurfaceImpl *iterator;
3545 IParentImpl *object;
3546 HRESULT hr;
3548 TRACE("iface %p, present_parameters %p, swapchain %p\n", iface, present_parameters, swapchain);
3550 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IParentImpl));
3551 if (!object)
3553 FIXME("Allocation of memory failed\n");
3554 *swapchain = NULL;
3555 return DDERR_OUTOFVIDEOMEMORY;
3558 object->lpVtbl = &IParent_Vtbl;
3559 object->ref = 1;
3561 hr = IWineD3DDevice_CreateSwapChain(This->wineD3DDevice, present_parameters,
3562 swapchain, (IUnknown *)object, This->ImplType);
3563 if (FAILED(hr))
3565 FIXME("(%p) CreateSwapChain failed, returning %#x\n", iface, hr);
3566 HeapFree(GetProcessHeap(), 0 , object);
3567 *swapchain = NULL;
3568 return hr;
3571 object->child = (IUnknown *)*swapchain;
3572 This->d3d_target->wineD3DSwapChain = *swapchain;
3573 iterator = This->d3d_target->complex_array[0];
3574 while (iterator)
3576 iterator->wineD3DSwapChain = *swapchain;
3577 iterator = iterator->complex_array[0];
3580 return hr;
3583 const IWineD3DDeviceParentVtbl ddraw_wined3d_device_parent_vtbl =
3585 /* IUnknown methods */
3586 device_parent_QueryInterface,
3587 device_parent_AddRef,
3588 device_parent_Release,
3589 /* IWineD3DDeviceParent methods */
3590 device_parent_WineD3DDeviceCreated,
3591 device_parent_CreateSurface,
3592 device_parent_CreateRenderTarget,
3593 device_parent_CreateDepthStencilSurface,
3594 device_parent_CreateVolume,
3595 device_parent_CreateSwapChain,