push abf7776878978b0406e974646c5bcfcf789b045c
[wine/hacks.git] / dlls / ddraw / ddraw.c
blob7b5a800951dddcfc5f4f93f82b67b48f65ed930f
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 "winreg.h"
39 #include "wine/exception.h"
41 #include "ddraw.h"
42 #include "d3d.h"
44 #include "ddraw_private.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
49 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
51 static BOOL IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested, const DDSURFACEDESC2* provided);
52 static HRESULT WINAPI IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This, IDirectDrawSurfaceImpl *primary);
53 static HRESULT WINAPI IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This, DDSURFACEDESC2 *pDDSD, IDirectDrawSurfaceImpl **ppSurf, UINT level);
55 /* Device identifier. Don't relay it to WineD3D */
56 static const DDDEVICEIDENTIFIER2 deviceidentifier =
58 "display",
59 "DirectDraw HAL",
60 { { 0x00010001, 0x00010001 } },
61 0, 0, 0, 0,
62 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
63 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
67 /*****************************************************************************
68 * IUnknown Methods
69 *****************************************************************************/
71 /*****************************************************************************
72 * IDirectDraw7::QueryInterface
74 * Queries different interfaces of the DirectDraw object. It can return
75 * IDirectDraw interfaces in version 1, 2, 4 and 7, and IDirect3D interfaces
76 * in version 1, 2, 3 and 7. An IDirect3DDevice can be created with this
77 * method.
78 * The returned interface is AddRef()-ed before it's returned
80 * Used for version 1, 2, 4 and 7
82 * Params:
83 * refiid: Interface ID asked for
84 * obj: Used to return the interface pointer
86 * Returns:
87 * S_OK if an interface was found
88 * E_NOINTERFACE if the requested interface wasn't found
90 *****************************************************************************/
91 static HRESULT WINAPI
92 IDirectDrawImpl_QueryInterface(IDirectDraw7 *iface,
93 REFIID refiid,
94 void **obj)
96 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
98 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(refiid), obj);
100 /* Can change surface impl type */
101 EnterCriticalSection(&ddraw_cs);
103 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
104 *obj = NULL;
106 if(!refiid)
108 LeaveCriticalSection(&ddraw_cs);
109 return DDERR_INVALIDPARAMS;
112 /* Check DirectDraw Interfaces */
113 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
114 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
116 *obj = ICOM_INTERFACE(This, IDirectDraw7);
117 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
119 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
121 *obj = ICOM_INTERFACE(This, IDirectDraw4);
122 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
124 else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
126 *obj = ICOM_INTERFACE(This, IDirectDraw3);
127 TRACE("(%p) Returning IDirectDraw3 interface at %p\n", This, *obj);
129 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
131 *obj = ICOM_INTERFACE(This, IDirectDraw2);
132 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
134 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
136 *obj = ICOM_INTERFACE(This, IDirectDraw);
137 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
140 /* Direct3D
141 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
142 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
143 * who had this idea and why. The older interfaces can query and IDirect3D version
144 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
145 * and messy to implement with the common creation function, so it has been left out here.
147 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
148 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
149 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
150 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
152 /* Check the surface implementation */
153 if(This->ImplType == SURFACE_UNKNOWN)
155 /* Apps may create the IDirect3D Interface before the primary surface.
156 * set the surface implementation */
157 This->ImplType = SURFACE_OPENGL;
158 TRACE("(%p) Choosing OpenGL surfaces because a Direct3D interface was requested\n", This);
160 else if(This->ImplType != SURFACE_OPENGL && DefaultSurfaceType == SURFACE_UNKNOWN)
162 ERR("(%p) The App is requesting a D3D device, but a non-OpenGL surface type was choosen. Prepare for trouble!\n", This);
163 ERR(" (%p) You may want to contact wine-devel for help\n", This);
164 /* Should I assert(0) here??? */
166 else if(This->ImplType != SURFACE_OPENGL)
168 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
169 /* Do not abort here, only reject 3D Device creation */
172 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
174 This->d3dversion = 1;
175 *obj = ICOM_INTERFACE(This, IDirect3D);
176 TRACE(" returning Direct3D interface at %p.\n", *obj);
178 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
180 This->d3dversion = 2;
181 *obj = ICOM_INTERFACE(This, IDirect3D2);
182 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
184 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
186 This->d3dversion = 3;
187 *obj = ICOM_INTERFACE(This, IDirect3D3);
188 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
190 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
192 This->d3dversion = 7;
193 *obj = ICOM_INTERFACE(This, IDirect3D7);
194 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
198 /* Unknown interface */
199 else
201 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
202 LeaveCriticalSection(&ddraw_cs);
203 return E_NOINTERFACE;
206 IUnknown_AddRef( (IUnknown *) *obj );
207 LeaveCriticalSection(&ddraw_cs);
208 return S_OK;
211 /*****************************************************************************
212 * IDirectDraw7::AddRef
214 * Increases the interfaces refcount, basically
216 * DDraw refcounting is a bit tricky. The different DirectDraw interface
217 * versions have individual refcounts, but the IDirect3D interfaces do not.
218 * All interfaces are from one object, that means calling QueryInterface on an
219 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
220 * IDirectDrawImpl object.
222 * That means all AddRef and Release implementations of IDirectDrawX work
223 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
224 * except of IDirect3D7 which thunks to IDirectDraw7
226 * Returns: The new refcount
228 *****************************************************************************/
229 static ULONG WINAPI
230 IDirectDrawImpl_AddRef(IDirectDraw7 *iface)
232 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
233 ULONG ref = InterlockedIncrement(&This->ref7);
235 TRACE("(%p) : incrementing IDirectDraw7 refcount from %u.\n", This, ref -1);
237 if(ref == 1) InterlockedIncrement(&This->numIfaces);
239 return ref;
242 /*****************************************************************************
243 * IDirectDrawImpl_Destroy
245 * Destroys a ddraw object if all refcounts are 0. This is to share code
246 * between the IDirectDrawX::Release functions
248 * Params:
249 * This: DirectDraw object to destroy
251 *****************************************************************************/
252 void
253 IDirectDrawImpl_Destroy(IDirectDrawImpl *This)
255 /* Clear the cooplevel to restore window and display mode */
256 IDirectDraw7_SetCooperativeLevel(ICOM_INTERFACE(This, IDirectDraw7),
257 NULL,
258 DDSCL_NORMAL);
260 /* Destroy the device window if we created one */
261 if(This->devicewindow != 0)
263 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
264 DestroyWindow(This->devicewindow);
265 This->devicewindow = 0;
268 /* Unregister the window class */
269 UnregisterClassA(This->classname, 0);
271 EnterCriticalSection(&ddraw_cs);
272 list_remove(&This->ddraw_list_entry);
273 LeaveCriticalSection(&ddraw_cs);
275 /* Release the attached WineD3D stuff */
276 IWineD3DDevice_Release(This->wineD3DDevice);
277 IWineD3D_Release(This->wineD3D);
279 /* Now free the object */
280 HeapFree(GetProcessHeap(), 0, This);
283 /*****************************************************************************
284 * IDirectDraw7::Release
286 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
288 * Returns: The new refcount
289 *****************************************************************************/
290 static ULONG WINAPI
291 IDirectDrawImpl_Release(IDirectDraw7 *iface)
293 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
294 ULONG ref = InterlockedDecrement(&This->ref7);
296 TRACE("(%p)->() decrementing IDirectDraw7 refcount from %u.\n", This, ref +1);
298 if(ref == 0)
300 ULONG ifacecount = InterlockedDecrement(&This->numIfaces);
301 if(ifacecount == 0) IDirectDrawImpl_Destroy(This);
304 return ref;
307 /*****************************************************************************
308 * IDirectDraw methods
309 *****************************************************************************/
311 /*****************************************************************************
312 * IDirectDraw7::SetCooperativeLevel
314 * Sets the cooperative level for the DirectDraw object, and the window
315 * assigned to it. The cooperative level determines the general behavior
316 * of the DirectDraw application
318 * Warning: This is quite tricky, as it's not really documented which
319 * cooperative levels can be combined with each other. If a game fails
320 * after this function, try to check the cooperative levels passed on
321 * Windows, and if it returns something different.
323 * If you think that this function caused the failure because it writes a
324 * fixme, be sure to run again with a +ddraw trace.
326 * What is known about cooperative levels (See the ddraw modes test):
327 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
328 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
329 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
330 * DDSCL_FULLSCREEN can be activated
331 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
333 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
334 * DDSCL_SETFOCUSWINDOW (partially),
335 * DDSCL_MULTITHREADED (work in progress)
337 * Unhandled flags, which should be implemented
338 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
339 * expect any difference to a normal window for wine)
340 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
341 * rendering (Possible test case: Half-life)
343 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
345 * These don't seem very important for wine:
346 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
348 * Returns:
349 * DD_OK if the cooperative level was set successfully
350 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
351 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
352 * (Probably others too, have to investigate)
354 *****************************************************************************/
355 static HRESULT WINAPI
356 IDirectDrawImpl_SetCooperativeLevel(IDirectDraw7 *iface,
357 HWND hwnd,
358 DWORD cooplevel)
360 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
361 HWND window;
362 HRESULT hr;
364 TRACE("(%p)->(%p,%08x)\n",This,hwnd,cooplevel);
365 DDRAW_dump_cooperativelevel(cooplevel);
367 EnterCriticalSection(&ddraw_cs);
369 /* Get the old window */
370 hr = IWineD3DDevice_GetHWND(This->wineD3DDevice, &window);
371 if(hr != D3D_OK)
373 ERR("IWineD3DDevice::GetHWND failed, hr = %08x\n", hr);
374 LeaveCriticalSection(&ddraw_cs);
375 return hr;
378 /* Tests suggest that we need one of them: */
379 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
380 DDSCL_NORMAL |
381 DDSCL_EXCLUSIVE )))
383 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
384 LeaveCriticalSection(&ddraw_cs);
385 return DDERR_INVALIDPARAMS;
388 /* Handle those levels first which set various hwnds */
389 if(cooplevel & DDSCL_SETFOCUSWINDOW)
391 /* This isn't compatible with a lot of flags */
392 if(cooplevel & ( DDSCL_MULTITHREADED |
393 DDSCL_FPUSETUP |
394 DDSCL_FPUPRESERVE |
395 DDSCL_ALLOWREBOOT |
396 DDSCL_ALLOWMODEX |
397 DDSCL_SETDEVICEWINDOW |
398 DDSCL_NORMAL |
399 DDSCL_EXCLUSIVE |
400 DDSCL_FULLSCREEN ) )
402 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
403 LeaveCriticalSection(&ddraw_cs);
404 return DDERR_INVALIDPARAMS;
406 else if( (This->cooperative_level & DDSCL_FULLSCREEN) && window)
408 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET\n");
409 LeaveCriticalSection(&ddraw_cs);
410 return DDERR_HWNDALREADYSET;
413 This->focuswindow = hwnd;
414 /* Won't use the hwnd param for anything else */
415 hwnd = NULL;
417 /* Use the focus window for drawing too */
418 IWineD3DDevice_SetHWND(This->wineD3DDevice, This->focuswindow);
420 /* Destroy the device window, if we have one */
421 if(This->devicewindow)
423 DestroyWindow(This->devicewindow);
424 This->devicewindow = NULL;
427 /* DDSCL_NORMAL or DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE */
428 if(cooplevel & DDSCL_NORMAL)
430 /* Can't coexist with fullscreen or exclusive */
431 if(cooplevel & (DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE) )
433 TRACE("(%p) DDSCL_NORMAL is not compative with DDSCL_FULLSCREEN or DDSCL_EXCLUSIVE\n", This);
434 LeaveCriticalSection(&ddraw_cs);
435 return DDERR_INVALIDPARAMS;
438 /* Switching from fullscreen? */
439 if(This->cooperative_level & DDSCL_FULLSCREEN)
441 /* Restore the display mode */
442 IDirectDraw7_RestoreDisplayMode(iface);
444 This->cooperative_level &= ~DDSCL_FULLSCREEN;
445 This->cooperative_level &= ~DDSCL_EXCLUSIVE;
446 This->cooperative_level &= ~DDSCL_ALLOWMODEX;
449 /* Don't override focus windows or private device windows */
450 if( hwnd &&
451 !(This->focuswindow) &&
452 !(This->devicewindow) &&
453 (hwnd != window) )
455 IWineD3DDevice_SetHWND(This->wineD3DDevice, hwnd);
458 IWineD3DDevice_SetFullscreen(This->wineD3DDevice,
459 FALSE);
461 else if(cooplevel & DDSCL_FULLSCREEN)
463 /* Needs DDSCL_EXCLUSIVE */
464 if(!(cooplevel & DDSCL_EXCLUSIVE) )
466 TRACE("(%p) DDSCL_FULLSCREEN needs DDSCL_EXCLUSIVE\n", This);
467 LeaveCriticalSection(&ddraw_cs);
468 return DDERR_INVALIDPARAMS;
470 /* Need a HWND
471 if(hwnd == 0)
473 TRACE("(%p) DDSCL_FULLSCREEN needs a HWND\n", This);
474 return DDERR_INVALIDPARAMS;
478 This->cooperative_level &= ~DDSCL_NORMAL;
479 IWineD3DDevice_SetFullscreen(This->wineD3DDevice,
480 TRUE);
482 /* Don't override focus windows or private device windows */
483 if( hwnd &&
484 !(This->focuswindow) &&
485 !(This->devicewindow) &&
486 (hwnd != window) )
488 BYTE buffer[32];
489 DWORD size = sizeof(buffer);
490 HKEY hkey = 0;
491 HWND drawwin = hwnd;
492 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
493 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey)) {
494 if (!RegQueryValueExA( hkey, "DirectDrawDesktopHack", 0, NULL, buffer, &size)) {
495 if ( IS_OPTION_TRUE( buffer[0] ) ) {
496 TRACE("Enabling DirectDrawDesktopHack hack\n");
497 drawwin = GetDesktopWindow();
501 IWineD3DDevice_SetHWND(This->wineD3DDevice, drawwin);
504 else if(cooplevel & DDSCL_EXCLUSIVE)
506 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN\n", This);
507 LeaveCriticalSection(&ddraw_cs);
508 return DDERR_INVALIDPARAMS;
511 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
513 /* Don't create a device window if a focus window is set */
514 if( !(This->focuswindow) )
516 HWND devicewindow = CreateWindowExA(0, This->classname, "DDraw device window",
517 WS_POPUP, 0, 0,
518 GetSystemMetrics(SM_CXSCREEN),
519 GetSystemMetrics(SM_CYSCREEN),
520 NULL, NULL, GetModuleHandleA(0), NULL);
522 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
523 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
525 IWineD3DDevice_SetHWND(This->wineD3DDevice, devicewindow);
526 This->devicewindow = devicewindow;
530 if(cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
532 /* Enable thread safety in wined3d */
533 IWineD3DDevice_SetMultithreaded(This->wineD3DDevice);
536 /* Unhandled flags */
537 if(cooplevel & DDSCL_ALLOWREBOOT)
538 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
539 if(cooplevel & DDSCL_ALLOWMODEX)
540 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
541 if(cooplevel & DDSCL_FPUSETUP)
542 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
544 /* Store the cooperative_level */
545 This->cooperative_level |= cooplevel;
546 TRACE("SetCooperativeLevel retuning DD_OK\n");
547 LeaveCriticalSection(&ddraw_cs);
548 return DD_OK;
551 /*****************************************************************************
553 * Helper function for SetDisplayMode and RestoreDisplayMode
555 * Implements DirectDraw's SetDisplayMode, but ignores the value of
556 * ForceRefreshRate, since it is already handled by
557 * IDirectDrawImpl_SetDisplayMode. RestoreDisplayMode can use this function
558 * without worrying that ForceRefreshRate will override the refresh rate. For
559 * argument and return value documentation, see
560 * IDirectDrawImpl_SetDisplayMode.
562 *****************************************************************************/
563 static HRESULT
564 IDirectDrawImpl_SetDisplayModeNoOverride(IDirectDraw7 *iface,
565 DWORD Width,
566 DWORD Height,
567 DWORD BPP,
568 DWORD RefreshRate,
569 DWORD Flags)
571 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
572 WINED3DDISPLAYMODE Mode;
573 HRESULT hr;
574 TRACE("(%p)->(%d,%d,%d,%d,%x: Relay!\n", This, Width, Height, BPP, RefreshRate, Flags);
576 EnterCriticalSection(&ddraw_cs);
577 if( !Width || !Height )
579 ERR("Width=%d, Height=%d, what to do?\n", Width, Height);
580 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
581 LeaveCriticalSection(&ddraw_cs);
582 return DD_OK;
585 /* Check the exclusive mode
586 if(!(This->cooperative_level & DDSCL_EXCLUSIVE))
587 return DDERR_NOEXCLUSIVEMODE;
588 * This is WRONG. Don't know if the SDK is completely
589 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
590 * is returned, but Half-Life 1.1.1.1 (Steam version)
591 * depends on this
594 Mode.Width = Width;
595 Mode.Height = Height;
596 Mode.RefreshRate = RefreshRate;
597 switch(BPP)
599 case 8: Mode.Format = WINED3DFMT_P8; break;
600 case 15: Mode.Format = WINED3DFMT_X1R5G5B5; break;
601 case 16: Mode.Format = WINED3DFMT_R5G6B5; break;
602 case 24: Mode.Format = WINED3DFMT_R8G8B8; break;
603 case 32: Mode.Format = WINED3DFMT_X8R8G8B8; break;
606 /* TODO: The possible return values from msdn suggest that
607 * the screen mode can't be changed if a surface is locked
608 * or some drawing is in progress
611 /* TODO: Lose the primary surface */
612 hr = IWineD3DDevice_SetDisplayMode(This->wineD3DDevice,
613 0, /* First swapchain */
614 &Mode);
615 LeaveCriticalSection(&ddraw_cs);
616 switch(hr)
618 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
619 default: return hr;
623 /*****************************************************************************
624 * IDirectDraw7::SetDisplayMode
626 * Sets the display screen resolution, color depth and refresh frequency
627 * when in fullscreen mode (in theory).
628 * Possible return values listed in the SDK suggest that this method fails
629 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
630 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
631 * It seems to be valid to pass 0 for With and Height, this has to be tested
632 * It could mean that the current video mode should be left as-is. (But why
633 * call it then?)
635 * Params:
636 * Height, Width: Screen dimension
637 * BPP: Color depth in Bits per pixel
638 * Refreshrate: Screen refresh rate
639 * Flags: Other stuff
641 * Returns
642 * DD_OK on success
644 *****************************************************************************/
645 static HRESULT WINAPI
646 IDirectDrawImpl_SetDisplayMode(IDirectDraw7 *iface,
647 DWORD Width,
648 DWORD Height,
649 DWORD BPP,
650 DWORD RefreshRate,
651 DWORD Flags)
653 if (force_refresh_rate != 0)
655 TRACE("ForceRefreshRate overriding passed-in refresh rate (%d Hz) to %d Hz\n", RefreshRate, force_refresh_rate);
656 RefreshRate = force_refresh_rate;
659 return IDirectDrawImpl_SetDisplayModeNoOverride(iface, Width, Height, BPP,
660 RefreshRate, Flags);
663 /*****************************************************************************
664 * IDirectDraw7::RestoreDisplayMode
666 * Restores the display mode to what it was at creation time. Basically.
668 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
669 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
670 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
671 * -> DD_1 is released. The screen should be left at 1024x768x32.
672 * -> DD_2 is released. The screen should be set to 1400x1050x32
673 * This case is unhandled right now, but Empire Earth does it this way.
674 * (But perhaps there is something in SetCooperativeLevel to prevent this)
676 * The msdn says that this method resets the display mode to what it was before
677 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
679 * Returns
680 * DD_OK on success
681 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
683 *****************************************************************************/
684 static HRESULT WINAPI
685 IDirectDrawImpl_RestoreDisplayMode(IDirectDraw7 *iface)
687 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
688 TRACE("(%p)\n", This);
690 return IDirectDrawImpl_SetDisplayModeNoOverride(ICOM_INTERFACE(This, IDirectDraw7),
691 This->orig_width,
692 This->orig_height,
693 This->orig_bpp,
698 /*****************************************************************************
699 * IDirectDraw7::GetCaps
701 * Returns the drives capabilities
703 * Used for version 1, 2, 4 and 7
705 * Params:
706 * DriverCaps: Structure to write the Hardware accelerated caps to
707 * HelCaps: Structure to write the emulation caps to
709 * Returns
710 * This implementation returns DD_OK only
712 *****************************************************************************/
713 static HRESULT WINAPI
714 IDirectDrawImpl_GetCaps(IDirectDraw7 *iface,
715 DDCAPS *DriverCaps,
716 DDCAPS *HELCaps)
718 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
719 TRACE("(%p)->(%p,%p)\n", This, DriverCaps, HELCaps);
721 /* One structure must be != NULL */
722 if( (!DriverCaps) && (!HELCaps) )
724 ERR("(%p) Invalid params to IDirectDrawImpl_GetCaps\n", This);
725 return DDERR_INVALIDPARAMS;
728 if(DriverCaps)
730 DD_STRUCT_COPY_BYSIZE(DriverCaps, &This->caps);
731 if (TRACE_ON(ddraw))
733 TRACE("Driver Caps :\n");
734 DDRAW_dump_DDCAPS(DriverCaps);
738 if(HELCaps)
740 DD_STRUCT_COPY_BYSIZE(HELCaps, &This->caps);
741 if (TRACE_ON(ddraw))
743 TRACE("HEL Caps :\n");
744 DDRAW_dump_DDCAPS(HELCaps);
748 return DD_OK;
751 /*****************************************************************************
752 * IDirectDraw7::Compact
754 * No idea what it does, MSDN says it's not implemented.
756 * Returns
757 * DD_OK, but this is unchecked
759 *****************************************************************************/
760 static HRESULT WINAPI
761 IDirectDrawImpl_Compact(IDirectDraw7 *iface)
763 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
764 TRACE("(%p)\n", This);
766 return DD_OK;
769 /*****************************************************************************
770 * IDirectDraw7::GetDisplayMode
772 * Returns information about the current display mode
774 * Exists in Version 1, 2, 4 and 7
776 * Params:
777 * DDSD: Address of a surface description structure to write the info to
779 * Returns
780 * DD_OK
782 *****************************************************************************/
783 static HRESULT WINAPI
784 IDirectDrawImpl_GetDisplayMode(IDirectDraw7 *iface,
785 DDSURFACEDESC2 *DDSD)
787 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
788 HRESULT hr;
789 WINED3DDISPLAYMODE Mode;
790 DWORD Size;
791 TRACE("(%p)->(%p): Relay\n", This, DDSD);
793 EnterCriticalSection(&ddraw_cs);
794 /* This seems sane */
795 if(!DDSD)
797 LeaveCriticalSection(&ddraw_cs);
798 return DDERR_INVALIDPARAMS;
801 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
802 * so one method can be used for all versions (Hopefully)
804 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
805 0 /* swapchain 0 */,
806 &Mode);
807 if( hr != D3D_OK )
809 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
810 LeaveCriticalSection(&ddraw_cs);
811 return hr;
814 Size = DDSD->dwSize;
815 memset(DDSD, 0, Size);
817 DDSD->dwSize = Size;
818 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
819 DDSD->dwWidth = Mode.Width;
820 DDSD->dwHeight = Mode.Height;
821 DDSD->u2.dwRefreshRate = 60;
822 DDSD->ddsCaps.dwCaps = 0;
823 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
824 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, Mode.Format);
825 DDSD->u1.lPitch = Mode.Width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
827 if(TRACE_ON(ddraw))
829 TRACE("Returning surface desc :\n");
830 DDRAW_dump_surface_desc(DDSD);
833 LeaveCriticalSection(&ddraw_cs);
834 return DD_OK;
837 /*****************************************************************************
838 * IDirectDraw7::GetFourCCCodes
840 * Returns an array of supported FourCC codes.
842 * Exists in Version 1, 2, 4 and 7
844 * Params:
845 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
846 * of enumerated codes
847 * Codes: Pointer to an array of DWORDs where the supported codes are written
848 * to
850 * Returns
851 * Always returns DD_OK, as it's a stub for now
853 *****************************************************************************/
854 static HRESULT WINAPI
855 IDirectDrawImpl_GetFourCCCodes(IDirectDraw7 *iface,
856 DWORD *NumCodes, DWORD *Codes)
858 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
859 FIXME("(%p)->(%p, %p): Stub!\n", This, NumCodes, Codes);
861 if(NumCodes) *NumCodes = 0;
863 return DD_OK;
866 /*****************************************************************************
867 * IDirectDraw7::GetMonitorFrequency
869 * Returns the monitor's frequency
871 * Exists in Version 1, 2, 4 and 7
873 * Params:
874 * Freq: Pointer to a DWORD to write the frequency to
876 * Returns
877 * Always returns DD_OK
879 *****************************************************************************/
880 static HRESULT WINAPI
881 IDirectDrawImpl_GetMonitorFrequency(IDirectDraw7 *iface,
882 DWORD *Freq)
884 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
885 TRACE("(%p)->(%p)\n", This, Freq);
887 /* Ideally this should be in WineD3D, as it concerns the screen setup,
888 * but for now this should make the games happy
890 *Freq = 60;
891 return DD_OK;
894 /*****************************************************************************
895 * IDirectDraw7::GetVerticalBlankStatus
897 * Returns the Vertical blank status of the monitor. This should be in WineD3D
898 * too basically, but as it's a semi stub, I didn't create a function there
900 * Params:
901 * status: Pointer to a BOOL to be filled with the vertical blank status
903 * Returns
904 * DD_OK on success
905 * DDERR_INVALIDPARAMS if status is NULL
907 *****************************************************************************/
908 static HRESULT WINAPI
909 IDirectDrawImpl_GetVerticalBlankStatus(IDirectDraw7 *iface,
910 BOOL *status)
912 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
913 TRACE("(%p)->(%p)\n", This, status);
915 /* This looks sane, the MSDN suggests it too */
916 EnterCriticalSection(&ddraw_cs);
917 if(!status)
919 LeaveCriticalSection(&ddraw_cs);
920 return DDERR_INVALIDPARAMS;
923 *status = This->fake_vblank;
924 This->fake_vblank = !This->fake_vblank;
925 LeaveCriticalSection(&ddraw_cs);
926 return DD_OK;
929 /*****************************************************************************
930 * IDirectDraw7::GetAvailableVidMem
932 * Returns the total and free video memory
934 * Params:
935 * Caps: Specifies the memory type asked for
936 * total: Pointer to a DWORD to be filled with the total memory
937 * free: Pointer to a DWORD to be filled with the free memory
939 * Returns
940 * DD_OK on success
941 * DDERR_INVALIDPARAMS of free and total are NULL
943 *****************************************************************************/
944 static HRESULT WINAPI
945 IDirectDrawImpl_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total, DWORD *free)
947 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
948 TRACE("(%p)->(%p, %p, %p)\n", This, Caps, total, free);
950 if(TRACE_ON(ddraw))
952 TRACE("(%p) Asked for memory with description: ", This);
953 DDRAW_dump_DDSCAPS2(Caps);
955 EnterCriticalSection(&ddraw_cs);
957 /* Todo: System memory vs local video memory vs non-local video memory
958 * The MSDN also mentions differences between texture memory and other
959 * resources, but that's not important
962 if( (!total) && (!free) )
964 LeaveCriticalSection(&ddraw_cs);
965 return DDERR_INVALIDPARAMS;
968 if(total) *total = This->total_vidmem;
969 if(free) *free = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
971 LeaveCriticalSection(&ddraw_cs);
972 return DD_OK;
975 /*****************************************************************************
976 * IDirectDraw7::Initialize
978 * Initializes a DirectDraw interface.
980 * Params:
981 * GUID: Interface identifier. Well, don't know what this is really good
982 * for
984 * Returns
985 * Returns DD_OK on the first call,
986 * DDERR_ALREADYINITIALIZED on repeated calls
988 *****************************************************************************/
989 static HRESULT WINAPI
990 IDirectDrawImpl_Initialize(IDirectDraw7 *iface,
991 GUID *Guid)
993 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
994 TRACE("(%p)->(%s): No-op\n", This, debugstr_guid(Guid));
996 if(This->initialized)
998 return DDERR_ALREADYINITIALIZED;
1000 else
1002 return DD_OK;
1006 /*****************************************************************************
1007 * IDirectDraw7::FlipToGDISurface
1009 * "Makes the surface that the GDI writes to the primary surface"
1010 * Looks like some windows specific thing we don't have to care about.
1011 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1012 * show error boxes ;)
1013 * Well, just return DD_OK.
1015 * Returns:
1016 * Always returns DD_OK
1018 *****************************************************************************/
1019 static HRESULT WINAPI
1020 IDirectDrawImpl_FlipToGDISurface(IDirectDraw7 *iface)
1022 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1023 TRACE("(%p)\n", This);
1025 return DD_OK;
1028 /*****************************************************************************
1029 * IDirectDraw7::WaitForVerticalBlank
1031 * This method allows applications to get in sync with the vertical blank
1032 * interval.
1033 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1034 * redraw the screen, most likely because of this stub
1036 * Parameters:
1037 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1038 * or DDWAITVB_BLOCKEND
1039 * h: Not used, according to MSDN
1041 * Returns:
1042 * Always returns DD_OK
1044 *****************************************************************************/
1045 static HRESULT WINAPI
1046 IDirectDrawImpl_WaitForVerticalBlank(IDirectDraw7 *iface,
1047 DWORD Flags,
1048 HANDLE h)
1050 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1051 FIXME("(%p)->(%x,%p): Stub\n", This, Flags, h);
1053 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1054 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1055 return DDERR_UNSUPPORTED; /* unchecked */
1057 return DD_OK;
1060 /*****************************************************************************
1061 * IDirectDraw7::GetScanLine
1063 * Returns the scan line that is being drawn on the monitor
1065 * Parameters:
1066 * Scanline: Address to write the scan line value to
1068 * Returns:
1069 * Always returns DD_OK
1071 *****************************************************************************/
1072 static HRESULT WINAPI IDirectDrawImpl_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1074 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1075 static BOOL hide = FALSE;
1076 WINED3DDISPLAYMODE Mode;
1078 /* This function is called often, so print the fixme only once */
1079 EnterCriticalSection(&ddraw_cs);
1080 if(!hide)
1082 FIXME("(%p)->(%p): Semi-Stub\n", This, Scanline);
1083 hide = TRUE;
1086 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1088 &Mode);
1090 /* Fake the line sweeping of the monitor */
1091 /* FIXME: We should synchronize with a source to keep the refresh rate */
1092 *Scanline = This->cur_scanline++;
1093 /* Assume 20 scan lines in the vertical blank */
1094 if (This->cur_scanline >= Mode.Height + 20)
1095 This->cur_scanline = 0;
1097 LeaveCriticalSection(&ddraw_cs);
1098 return DD_OK;
1101 /*****************************************************************************
1102 * IDirectDraw7::TestCooperativeLevel
1104 * Informs the application about the state of the video adapter, depending
1105 * on the cooperative level
1107 * Returns:
1108 * DD_OK if the device is in a sane state
1109 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1110 * if the state is not correct(See below)
1112 *****************************************************************************/
1113 static HRESULT WINAPI
1114 IDirectDrawImpl_TestCooperativeLevel(IDirectDraw7 *iface)
1116 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1117 HRESULT hr;
1118 TRACE("(%p)\n", This);
1120 EnterCriticalSection(&ddraw_cs);
1121 /* Description from MSDN:
1122 * For fullscreen apps return DDERR_NOEXCLUSIVEMODE if the user switched
1123 * away from the app with e.g. alt-tab. Windowed apps receive
1124 * DDERR_EXCLUSIVEMODEALREADYSET if another application created a
1125 * DirectDraw object in exclusive mode. DDERR_WRONGMODE is returned,
1126 * when the video mode has changed
1129 hr = IWineD3DDevice_TestCooperativeLevel(This->wineD3DDevice);
1131 /* Fix the result value. These values are mapped from their
1132 * d3d9 counterpart.
1134 switch(hr)
1136 case WINED3DERR_DEVICELOST:
1137 if(This->cooperative_level & DDSCL_EXCLUSIVE)
1139 LeaveCriticalSection(&ddraw_cs);
1140 return DDERR_NOEXCLUSIVEMODE;
1142 else
1144 LeaveCriticalSection(&ddraw_cs);
1145 return DDERR_EXCLUSIVEMODEALREADYSET;
1148 case WINED3DERR_DEVICENOTRESET:
1149 LeaveCriticalSection(&ddraw_cs);
1150 return DD_OK;
1152 case WINED3D_OK:
1153 LeaveCriticalSection(&ddraw_cs);
1154 return DD_OK;
1156 case WINED3DERR_DRIVERINTERNALERROR:
1157 default:
1158 ERR("(%p) Unexpected return value %08x from wineD3D, "
1159 " returning DD_OK\n", This, hr);
1162 LeaveCriticalSection(&ddraw_cs);
1163 return DD_OK;
1166 /*****************************************************************************
1167 * IDirectDraw7::GetGDISurface
1169 * Returns the surface that GDI is treating as the primary surface.
1170 * For Wine this is the front buffer
1172 * Params:
1173 * GDISurface: Address to write the surface pointer to
1175 * Returns:
1176 * DD_OK if the surface was found
1177 * DDERR_NOTFOUND if the GDI surface wasn't found
1179 *****************************************************************************/
1180 static HRESULT WINAPI
1181 IDirectDrawImpl_GetGDISurface(IDirectDraw7 *iface,
1182 IDirectDrawSurface7 **GDISurface)
1184 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1185 IWineD3DSurface *Surf;
1186 IDirectDrawSurface7 *ddsurf;
1187 HRESULT hr;
1188 DDSCAPS2 ddsCaps;
1189 TRACE("(%p)->(%p)\n", This, GDISurface);
1191 /* Get the back buffer from the wineD3DDevice and search its
1192 * attached surfaces for the front buffer
1194 EnterCriticalSection(&ddraw_cs);
1195 hr = IWineD3DDevice_GetBackBuffer(This->wineD3DDevice,
1196 0, /* SwapChain */
1197 0, /* first back buffer*/
1198 WINED3DBACKBUFFER_TYPE_MONO,
1199 &Surf);
1201 if( (hr != D3D_OK) ||
1202 (!Surf) )
1204 ERR("IWineD3DDevice::GetBackBuffer failed\n");
1205 LeaveCriticalSection(&ddraw_cs);
1206 return DDERR_NOTFOUND;
1209 /* GetBackBuffer AddRef()ed the surface, release it */
1210 IWineD3DSurface_Release(Surf);
1212 IWineD3DSurface_GetParent(Surf,
1213 (IUnknown **) &ddsurf);
1214 IDirectDrawSurface7_Release(ddsurf); /* For the GetParent */
1216 /* Find the front buffer */
1217 ddsCaps.dwCaps = DDSCAPS_FRONTBUFFER;
1218 hr = IDirectDrawSurface7_GetAttachedSurface(ddsurf,
1219 &ddsCaps,
1220 GDISurface);
1221 if(hr != DD_OK)
1223 ERR("IDirectDrawSurface7::GetAttachedSurface failed, hr = %x\n", hr);
1226 /* The AddRef is OK this time */
1227 LeaveCriticalSection(&ddraw_cs);
1228 return hr;
1231 /*****************************************************************************
1232 * IDirectDraw7::EnumDisplayModes
1234 * Enumerates the supported Display modes. The modes can be filtered with
1235 * the DDSD parameter.
1237 * Params:
1238 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES
1239 * DDSD: Surface description to filter the modes
1240 * Context: Pointer passed back to the callback function
1241 * cb: Application-provided callback function
1243 * Returns:
1244 * DD_OK on success
1245 * DDERR_INVALIDPARAMS if the callback wasn't set
1247 *****************************************************************************/
1248 static HRESULT WINAPI
1249 IDirectDrawImpl_EnumDisplayModes(IDirectDraw7 *iface,
1250 DWORD Flags,
1251 DDSURFACEDESC2 *DDSD,
1252 void *Context,
1253 LPDDENUMMODESCALLBACK2 cb)
1255 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1256 unsigned int modenum, fmt;
1257 WINED3DFORMAT pixelformat = WINED3DFMT_UNKNOWN;
1258 WINED3DDISPLAYMODE mode;
1259 DDSURFACEDESC2 callback_sd;
1260 WINED3DDISPLAYMODE *enum_modes = NULL;
1261 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
1263 WINED3DFORMAT checkFormatList[] =
1265 WINED3DFMT_R8G8B8,
1266 WINED3DFMT_A8R8G8B8,
1267 WINED3DFMT_X8R8G8B8,
1268 WINED3DFMT_R5G6B5,
1269 WINED3DFMT_X1R5G5B5,
1270 WINED3DFMT_A1R5G5B5,
1271 WINED3DFMT_A4R4G4B4,
1272 WINED3DFMT_R3G3B2,
1273 WINED3DFMT_A8R3G3B2,
1274 WINED3DFMT_X4R4G4B4,
1275 WINED3DFMT_A2B10G10R10,
1276 WINED3DFMT_A8B8G8R8,
1277 WINED3DFMT_X8B8G8R8,
1278 WINED3DFMT_A2R10G10B10,
1279 WINED3DFMT_A8P8,
1280 WINED3DFMT_P8
1283 TRACE("(%p)->(%p,%p,%p): Relay\n", This, DDSD, Context, cb);
1285 EnterCriticalSection(&ddraw_cs);
1286 /* This looks sane */
1287 if(!cb)
1289 LeaveCriticalSection(&ddraw_cs);
1290 return DDERR_INVALIDPARAMS;
1293 if(DDSD)
1295 if ((DDSD->dwFlags & DDSD_PIXELFORMAT) && (DDSD->u4.ddpfPixelFormat.dwFlags & DDPF_RGB) )
1296 pixelformat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
1299 if(!(Flags & DDEDM_REFRESHRATES))
1301 enum_mode_array_size = 16;
1302 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1303 if (!enum_modes)
1305 ERR("Out of memory\n");
1306 LeaveCriticalSection(&ddraw_cs);
1307 return DDERR_OUTOFMEMORY;
1311 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
1313 if(pixelformat != WINED3DFMT_UNKNOWN && checkFormatList[fmt] != pixelformat)
1315 continue;
1318 modenum = 0;
1319 while(IWineD3D_EnumAdapterModes(This->wineD3D,
1320 WINED3DADAPTER_DEFAULT,
1321 checkFormatList[fmt],
1322 modenum++,
1323 &mode) == WINED3D_OK)
1325 if(DDSD)
1327 if(DDSD->dwFlags & DDSD_WIDTH && mode.Width != DDSD->dwWidth) continue;
1328 if(DDSD->dwFlags & DDSD_HEIGHT && mode.Height != DDSD->dwHeight) continue;
1331 if(!(Flags & DDEDM_REFRESHRATES))
1333 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
1334 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
1335 * to be reduced to a single unique result in such case.
1337 BOOL found = FALSE;
1338 unsigned i;
1340 for (i = 0; i < enum_mode_count; i++)
1342 if(enum_modes[i].Width == mode.Width && enum_modes[i].Height == mode.Height &&
1343 enum_modes[i].Format == mode.Format)
1345 found = TRUE;
1346 break;
1350 if(found) continue;
1353 memset(&callback_sd, 0, sizeof(callback_sd));
1354 callback_sd.dwSize = sizeof(callback_sd);
1355 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1357 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH;
1358 if(Flags & DDEDM_REFRESHRATES)
1360 callback_sd.dwFlags |= DDSD_REFRESHRATE;
1361 callback_sd.u2.dwRefreshRate = mode.RefreshRate;
1364 callback_sd.dwWidth = mode.Width;
1365 callback_sd.dwHeight = mode.Height;
1367 PixelFormat_WineD3DtoDD(&callback_sd.u4.ddpfPixelFormat, mode.Format);
1369 /* Calc pitch and DWORD align like MSDN says */
1370 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.Width;
1371 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
1373 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
1374 callback_sd.u2.dwRefreshRate);
1376 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
1378 TRACE("Application asked to terminate the enumeration\n");
1379 HeapFree(GetProcessHeap(), 0, enum_modes);
1380 LeaveCriticalSection(&ddraw_cs);
1381 return DD_OK;
1384 if(!(Flags & DDEDM_REFRESHRATES))
1386 if (enum_mode_count == enum_mode_array_size)
1388 WINED3DDISPLAYMODE *new_enum_modes;
1390 enum_mode_array_size *= 2;
1391 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1393 if (!new_enum_modes)
1395 ERR("Out of memory\n");
1396 HeapFree(GetProcessHeap(), 0, enum_modes);
1397 LeaveCriticalSection(&ddraw_cs);
1398 return DDERR_OUTOFMEMORY;
1401 enum_modes = new_enum_modes;
1404 enum_modes[enum_mode_count++] = mode;
1409 TRACE("End of enumeration\n");
1410 HeapFree(GetProcessHeap(), 0, enum_modes);
1411 LeaveCriticalSection(&ddraw_cs);
1412 return DD_OK;
1415 /*****************************************************************************
1416 * IDirectDraw7::EvaluateMode
1418 * Used with IDirectDraw7::StartModeTest to test video modes.
1419 * EvaluateMode is used to pass or fail a mode, and continue with the next
1420 * mode
1422 * Params:
1423 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
1424 * Timeout: Returns the amount of seconds left before the mode would have
1425 * been failed automatically
1427 * Returns:
1428 * This implementation always DD_OK, because it's a stub
1430 *****************************************************************************/
1431 static HRESULT WINAPI
1432 IDirectDrawImpl_EvaluateMode(IDirectDraw7 *iface,
1433 DWORD Flags,
1434 DWORD *Timeout)
1436 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1437 FIXME("(%p)->(%d,%p): Stub!\n", This, Flags, Timeout);
1439 /* When implementing this, implement it in WineD3D */
1441 return DD_OK;
1444 /*****************************************************************************
1445 * IDirectDraw7::GetDeviceIdentifier
1447 * Returns the device identifier, which gives information about the driver
1448 * Our device identifier is defined at the beginning of this file.
1450 * Params:
1451 * DDDI: Address for the returned structure
1452 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
1454 * Returns:
1455 * On success it returns DD_OK
1456 * DDERR_INVALIDPARAMS if DDDI is NULL
1458 *****************************************************************************/
1459 static HRESULT WINAPI
1460 IDirectDrawImpl_GetDeviceIdentifier(IDirectDraw7 *iface,
1461 DDDEVICEIDENTIFIER2 *DDDI,
1462 DWORD Flags)
1464 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1465 TRACE("(%p)->(%p,%08x)\n", This, DDDI, Flags);
1467 if(!DDDI)
1468 return DDERR_INVALIDPARAMS;
1470 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
1471 * host adapter, if there's a secondary 3D adapter. This doesn't apply
1472 * to any modern hardware, nor is it interesting for Wine, so ignore it
1475 *DDDI = deviceidentifier;
1476 return DD_OK;
1479 /*****************************************************************************
1480 * IDirectDraw7::GetSurfaceFromDC
1482 * Returns the Surface for a GDI device context handle.
1483 * Is this related to IDirectDrawSurface::GetDC ???
1485 * Params:
1486 * hdc: hdc to return the surface for
1487 * Surface: Address to write the surface pointer to
1489 * Returns:
1490 * Always returns DD_OK because it's a stub
1492 *****************************************************************************/
1493 static HRESULT WINAPI
1494 IDirectDrawImpl_GetSurfaceFromDC(IDirectDraw7 *iface,
1495 HDC hdc,
1496 IDirectDrawSurface7 **Surface)
1498 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1499 FIXME("(%p)->(%p,%p): Stub!\n", This, hdc, Surface);
1501 /* Implementation idea if needed: Loop through all surfaces and compare
1502 * their hdc with hdc. Implement it in WineD3D! */
1503 return DDERR_NOTFOUND;
1506 /*****************************************************************************
1507 * IDirectDraw7::RestoreAllSurfaces
1509 * Calls the restore method of all surfaces
1511 * Params:
1513 * Returns:
1514 * Always returns DD_OK because it's a stub
1516 *****************************************************************************/
1517 static HRESULT WINAPI
1518 IDirectDrawImpl_RestoreAllSurfaces(IDirectDraw7 *iface)
1520 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1521 FIXME("(%p): Stub\n", This);
1523 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
1524 * get their parent and call their restore method. Do not implement
1525 * it in WineD3D, as restoring a surface means re-creating the
1526 * WineD3DDSurface
1528 return DD_OK;
1531 /*****************************************************************************
1532 * IDirectDraw7::StartModeTest
1534 * Tests the specified video modes to update the system registry with
1535 * refresh rate information. StartModeTest starts the mode test,
1536 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
1537 * isn't called within 15 seconds, the mode is failed automatically
1539 * As refresh rates are handled by the X server, I don't think this
1540 * Method is important
1542 * Params:
1543 * Modes: An array of mode specifications
1544 * NumModes: The number of modes in Modes
1545 * Flags: Some flags...
1547 * Returns:
1548 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
1549 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
1550 * otherwise DD_OK
1552 *****************************************************************************/
1553 static HRESULT WINAPI
1554 IDirectDrawImpl_StartModeTest(IDirectDraw7 *iface,
1555 SIZE *Modes,
1556 DWORD NumModes,
1557 DWORD Flags)
1559 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1560 WARN("(%p)->(%p, %d, %x): Semi-Stub, most likely harmless\n", This, Modes, NumModes, Flags);
1562 /* This looks sane */
1563 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
1565 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
1566 * As it is not, DDERR_TESTFINISHED is returned
1567 * (hopefully that's correct
1569 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
1570 * well, that value doesn't (yet) exist in the wine headers, so ignore it
1573 return DD_OK;
1576 /*****************************************************************************
1577 * IDirectDrawImpl_RecreateSurfacesCallback
1579 * Enumeration callback for IDirectDrawImpl_RecreateAllSurfaces.
1580 * It re-recreates the WineD3DSurface. It's pretty straightforward
1582 *****************************************************************************/
1583 HRESULT WINAPI
1584 IDirectDrawImpl_RecreateSurfacesCallback(IDirectDrawSurface7 *surf,
1585 DDSURFACEDESC2 *desc,
1586 void *Context)
1588 IDirectDrawSurfaceImpl *surfImpl = ICOM_OBJECT(IDirectDrawSurfaceImpl,
1589 IDirectDrawSurface7,
1590 surf);
1591 IDirectDrawImpl *This = surfImpl->ddraw;
1592 IUnknown *Parent;
1593 IParentImpl *parImpl = NULL;
1594 IWineD3DSurface *wineD3DSurface;
1595 HRESULT hr;
1596 void *tmp;
1597 IWineD3DClipper *clipper = NULL;
1599 WINED3DSURFACE_DESC Desc;
1600 WINED3DFORMAT Format;
1601 WINED3DRESOURCETYPE Type;
1602 DWORD Usage;
1603 WINED3DPOOL Pool;
1604 UINT Size;
1606 WINED3DMULTISAMPLE_TYPE MultiSampleType;
1607 DWORD MultiSampleQuality;
1608 UINT Width;
1609 UINT Height;
1611 TRACE("(%p): Enumerated Surface %p\n", This, surfImpl);
1613 /* For the enumeration */
1614 IDirectDrawSurface7_Release(surf);
1616 if(surfImpl->ImplType == This->ImplType) return DDENUMRET_OK; /* Continue */
1618 /* Get the objects */
1619 wineD3DSurface = surfImpl->WineD3DSurface;
1620 IWineD3DSurface_GetParent(wineD3DSurface, &Parent);
1621 IUnknown_Release(Parent); /* For the getParent */
1623 /* Is the parent an IParent interface? */
1624 if(IUnknown_QueryInterface(Parent, &IID_IParent, &tmp) == S_OK)
1626 /* It is a IParent interface! */
1627 IUnknown_Release(Parent); /* For the QueryInterface */
1628 parImpl = ICOM_OBJECT(IParentImpl, IParent, Parent);
1629 /* Release the reference the parent interface is holding */
1630 IWineD3DSurface_Release(wineD3DSurface);
1633 /* get the clipper */
1634 IWineD3DSurface_GetClipper(wineD3DSurface, &clipper);
1636 /* Get the surface properties */
1637 Desc.Format = &Format;
1638 Desc.Type = &Type;
1639 Desc.Usage = &Usage;
1640 Desc.Pool = &Pool;
1641 Desc.Size = &Size;
1642 Desc.MultiSampleType = &MultiSampleType;
1643 Desc.MultiSampleQuality = &MultiSampleQuality;
1644 Desc.Width = &Width;
1645 Desc.Height = &Height;
1647 hr = IWineD3DSurface_GetDesc(wineD3DSurface, &Desc);
1648 if(hr != D3D_OK) return hr;
1650 /* Create the new surface */
1651 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice,
1652 Width, Height, Format,
1653 TRUE /* Lockable */,
1654 FALSE /* Discard */,
1655 surfImpl->mipmap_level,
1656 &surfImpl->WineD3DSurface,
1657 Type,
1658 Usage,
1659 Pool,
1660 MultiSampleType,
1661 MultiSampleQuality,
1662 0 /* SharedHandle */,
1663 This->ImplType,
1664 Parent);
1666 if(hr != D3D_OK)
1667 return hr;
1669 IWineD3DSurface_SetClipper(surfImpl->WineD3DSurface, clipper);
1671 /* Update the IParent if it exists */
1672 if(parImpl)
1674 parImpl->child = (IUnknown *) surfImpl->WineD3DSurface;
1675 /* Add a reference for the IParent */
1676 IWineD3DSurface_AddRef(surfImpl->WineD3DSurface);
1678 /* TODO: Copy the surface content, except for render targets */
1680 if(IWineD3DSurface_Release(wineD3DSurface) == 0)
1681 TRACE("Surface released successful, next surface\n");
1682 else
1683 ERR("Something's still holding the old WineD3DSurface\n");
1685 surfImpl->ImplType = This->ImplType;
1687 if(clipper)
1689 IWineD3DClipper_Release(clipper);
1691 return DDENUMRET_OK;
1694 /*****************************************************************************
1695 * IDirectDrawImpl_RecreateAllSurfaces
1697 * A function, that converts all wineD3DSurfaces to the new implementation type
1698 * It enumerates all surfaces with IWineD3DDevice::EnumSurfaces, creates a
1699 * new WineD3DSurface, copies the content and releases the old surface
1701 *****************************************************************************/
1702 static HRESULT
1703 IDirectDrawImpl_RecreateAllSurfaces(IDirectDrawImpl *This)
1705 DDSURFACEDESC2 desc;
1706 TRACE("(%p): Switch to implementation %d\n", This, This->ImplType);
1708 if(This->ImplType != SURFACE_OPENGL && This->d3d_initialized)
1710 /* Should happen almost never */
1711 FIXME("(%p) Switching to non-opengl surfaces with d3d started. Is this a bug?\n", This);
1712 /* Shutdown d3d */
1713 IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroyDepthStencilSurface, D3D7CB_DestroySwapChain);
1715 /* Contrary: D3D starting is handled by the caller, because it knows the render target */
1717 memset(&desc, 0, sizeof(desc));
1718 desc.dwSize = sizeof(desc);
1720 return IDirectDraw7_EnumSurfaces(ICOM_INTERFACE(This, IDirectDraw7),
1722 &desc,
1723 This,
1724 IDirectDrawImpl_RecreateSurfacesCallback);
1727 /*****************************************************************************
1728 * D3D7CB_CreateSurface
1730 * Callback function for IDirect3DDevice_CreateTexture. It searches for the
1731 * correct mipmap sublevel, and returns it to WineD3D.
1732 * The surfaces are created already by IDirectDraw7::CreateSurface
1734 * Params:
1735 * With, Height: With and height of the surface
1736 * Format: The requested format
1737 * Usage, Pool: D3DUSAGE and D3DPOOL of the surface
1738 * level: The mipmap level
1739 * Face: The cube map face type
1740 * Surface: Pointer to pass the created surface back at
1741 * SharedHandle: NULL
1743 * Returns:
1744 * D3D_OK
1746 *****************************************************************************/
1747 static HRESULT WINAPI
1748 D3D7CB_CreateSurface(IUnknown *device,
1749 IUnknown *pSuperior,
1750 UINT Width, UINT Height,
1751 WINED3DFORMAT Format,
1752 DWORD Usage, WINED3DPOOL Pool, UINT level,
1753 WINED3DCUBEMAP_FACES Face,
1754 IWineD3DSurface **Surface,
1755 HANDLE *SharedHandle)
1757 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
1758 IDirectDrawSurfaceImpl *surf = NULL;
1759 int i = 0;
1760 DDSCAPS2 searchcaps = This->tex_root->surface_desc.ddsCaps;
1761 TRACE("(%p) call back. surf=%p. Face %d level %d\n", device, This->tex_root, Face, level);
1763 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
1764 switch(Face)
1766 case WINED3DCUBEMAP_FACE_POSITIVE_X:
1767 TRACE("Asked for positive x\n");
1768 if(searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP) {
1769 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
1771 surf = This->tex_root; break;
1772 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
1773 TRACE("Asked for negative x\n");
1774 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
1775 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
1776 TRACE("Asked for positive y\n");
1777 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
1778 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
1779 TRACE("Asked for negative y\n");
1780 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
1781 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
1782 TRACE("Asked for positive z\n");
1783 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
1784 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
1785 TRACE("Asked for negative z\n");
1786 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
1787 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
1790 if(!surf)
1792 IDirectDrawSurface7 *attached;
1793 IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(This->tex_root, IDirectDrawSurface7),
1794 &searchcaps,
1795 &attached);
1796 surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attached);
1797 IDirectDrawSurface7_Release(attached);
1799 if(!surf) ERR("root search surface not found\n");
1801 /* Find the wanted mipmap. There are enough mipmaps in the chain */
1802 while(i < level)
1804 IDirectDrawSurface7 *attached;
1805 IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(surf, IDirectDrawSurface7),
1806 &searchcaps,
1807 &attached);
1808 if(!attached) ERR("Surface not found\n");
1809 surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attached);
1810 IDirectDrawSurface7_Release(attached);
1811 i++;
1814 /* Return the surface */
1815 *Surface = surf->WineD3DSurface;
1817 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *Surface, surf);
1818 return D3D_OK;
1821 ULONG WINAPI D3D7CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
1822 IUnknown* swapChainParent;
1823 TRACE("(%p) call back\n", pSwapChain);
1825 IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
1826 IUnknown_Release(swapChainParent);
1827 return IUnknown_Release(swapChainParent);
1830 ULONG WINAPI D3D7CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
1831 IUnknown* surfaceParent;
1832 TRACE("(%p) call back\n", pSurface);
1834 IWineD3DSurface_GetParent(pSurface, &surfaceParent);
1835 IUnknown_Release(surfaceParent);
1836 return IUnknown_Release(surfaceParent);
1839 /*****************************************************************************
1840 * IDirectDrawImpl_CreateNewSurface
1842 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
1843 * with the passed parameters.
1845 * Params:
1846 * DDSD: Description of the surface to create
1847 * Surf: Address to store the interface pointer at
1849 * Returns:
1850 * DD_OK on success
1852 *****************************************************************************/
1853 static HRESULT WINAPI
1854 IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This,
1855 DDSURFACEDESC2 *pDDSD,
1856 IDirectDrawSurfaceImpl **ppSurf,
1857 UINT level)
1859 HRESULT hr;
1860 UINT Width = 0, Height = 0;
1861 WINED3DFORMAT Format = WINED3DFMT_UNKNOWN;
1862 WINED3DRESOURCETYPE ResType = WINED3DRTYPE_SURFACE;
1863 DWORD Usage = 0;
1864 WINED3DSURFTYPE ImplType = This->ImplType;
1865 WINED3DSURFACE_DESC Desc;
1866 IUnknown *Parent;
1867 IParentImpl *parImpl = NULL;
1868 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
1870 /* Dummies for GetDesc */
1871 WINED3DPOOL dummy_d3dpool;
1872 WINED3DMULTISAMPLE_TYPE dummy_mst;
1873 UINT dummy_uint;
1874 DWORD dummy_dword;
1876 if (TRACE_ON(ddraw))
1878 TRACE(" (%p) Requesting surface desc :\n", This);
1879 DDRAW_dump_surface_desc(pDDSD);
1882 /* Select the surface type, if it wasn't choosen yet */
1883 if(ImplType == SURFACE_UNKNOWN)
1885 /* Use GL Surfaces if a D3DDEVICE Surface is requested */
1886 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1888 TRACE("(%p) Choosing GL surfaces because a 3DDEVICE Surface was requested\n", This);
1889 ImplType = SURFACE_OPENGL;
1892 /* Otherwise use GDI surfaces for now */
1893 else
1895 TRACE("(%p) Choosing GDI surfaces for 2D rendering\n", This);
1896 ImplType = SURFACE_GDI;
1899 /* Policy if all surface implementations are available:
1900 * First, check if a default type was set with winecfg. If not,
1901 * try Xrender surfaces, and use them if they work. Next, check if
1902 * accelerated OpenGL is available, and use GL surfaces in this
1903 * case. If all else fails, use GDI surfaces. If a 3DDEVICE surface
1904 * was created, always use OpenGL surfaces.
1906 * (Note: Xrender surfaces are not implemented for now, the
1907 * unaccelerated implementation uses GDI to render in Software)
1910 /* Store the type. If it needs to be changed, all WineD3DSurfaces have to
1911 * be re-created. This could be done with IDirectDrawSurface7::Restore
1913 This->ImplType = ImplType;
1915 else
1917 if((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE ) &&
1918 (This->ImplType != SURFACE_OPENGL ) && DefaultSurfaceType == SURFACE_UNKNOWN)
1920 /* We have to change to OpenGL,
1921 * and re-create all WineD3DSurfaces
1923 ImplType = SURFACE_OPENGL;
1924 This->ImplType = ImplType;
1925 TRACE("(%p) Re-creating all surfaces\n", This);
1926 IDirectDrawImpl_RecreateAllSurfaces(This);
1927 TRACE("(%p) Done recreating all surfaces\n", This);
1929 else if(This->ImplType != SURFACE_OPENGL && pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1931 WARN("The application requests a 3D capable surface, but a non-opengl surface was set in the registry\n");
1932 /* Do not fail surface creation, only fail 3D device creation */
1936 /* Get the correct wined3d usage */
1937 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE |
1938 DDSCAPS_BACKBUFFER |
1939 DDSCAPS_3DDEVICE ) )
1941 Usage |= WINED3DUSAGE_RENDERTARGET;
1943 pDDSD->ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY |
1944 DDSCAPS_VISIBLE;
1946 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
1948 Usage |= WINED3DUSAGE_OVERLAY;
1950 if(This->depthstencil || (pDDSD->ddsCaps.dwCaps & DDSCAPS_ZBUFFER) )
1952 /* The depth stencil creation callback sets this flag.
1953 * Set the WineD3D usage to let it know that it's a depth
1954 * Stencil surface.
1956 Usage |= WINED3DUSAGE_DEPTHSTENCIL;
1958 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
1960 Pool = WINED3DPOOL_SYSTEMMEM;
1962 else if(pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
1964 Pool = WINED3DPOOL_MANAGED;
1965 /* Managed textures have the system memory flag set */
1966 pDDSD->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
1968 else if(pDDSD->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
1970 /* Videomemory adds localvidmem, this is mutually exclusive with systemmemory
1971 * and texturemanage
1973 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
1976 Format = PixelFormat_DD2WineD3D(&pDDSD->u4.ddpfPixelFormat);
1977 if(Format == WINED3DFMT_UNKNOWN)
1979 ERR("Unsupported / Unknown pixelformat\n");
1980 return DDERR_INVALIDPIXELFORMAT;
1983 /* Create the Surface object */
1984 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
1985 if(!*ppSurf)
1987 ERR("(%p) Error allocating memory for a surface\n", This);
1988 return DDERR_OUTOFVIDEOMEMORY;
1990 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawSurface7, IDirectDrawSurface7_Vtbl);
1991 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawSurface3, IDirectDrawSurface3_Vtbl);
1992 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawGammaControl, IDirectDrawGammaControl_Vtbl);
1993 ICOM_INIT_INTERFACE(*ppSurf, IDirect3DTexture2, IDirect3DTexture2_Vtbl);
1994 ICOM_INIT_INTERFACE(*ppSurf, IDirect3DTexture, IDirect3DTexture1_Vtbl);
1995 (*ppSurf)->ref = 1;
1996 (*ppSurf)->version = 7;
1997 (*ppSurf)->ddraw = This;
1998 (*ppSurf)->surface_desc.dwSize = sizeof(DDSURFACEDESC2);
1999 (*ppSurf)->surface_desc.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2000 DD_STRUCT_COPY_BYSIZE(&(*ppSurf)->surface_desc, pDDSD);
2002 /* Surface attachments */
2003 (*ppSurf)->next_attached = NULL;
2004 (*ppSurf)->first_attached = *ppSurf;
2006 /* Needed to re-create the surface on an implementation change */
2007 (*ppSurf)->ImplType = ImplType;
2009 /* For D3DDevice creation */
2010 (*ppSurf)->isRenderTarget = FALSE;
2012 /* A trace message for debugging */
2013 TRACE("(%p) Created IDirectDrawSurface implementation structure at %p\n", This, *ppSurf);
2015 if(pDDSD->ddsCaps.dwCaps & ( DDSCAPS_PRIMARYSURFACE | DDSCAPS_TEXTURE | DDSCAPS_3DDEVICE) )
2017 /* Render targets and textures need a IParent interface,
2018 * because WineD3D will destroy them when the swapchain
2019 * is released
2021 parImpl = HeapAlloc(GetProcessHeap(), 0, sizeof(IParentImpl));
2022 if(!parImpl)
2024 ERR("Out of memory when allocating memory for a IParent implementation\n");
2025 return DDERR_OUTOFMEMORY;
2027 parImpl->ref = 1;
2028 ICOM_INIT_INTERFACE(parImpl, IParent, IParent_Vtbl);
2029 Parent = (IUnknown *) ICOM_INTERFACE(parImpl, IParent);
2030 TRACE("Using IParent interface %p as parent\n", parImpl);
2032 else
2034 /* Use the surface as parent */
2035 Parent = (IUnknown *) ICOM_INTERFACE(*ppSurf, IDirectDrawSurface7);
2036 TRACE("Using Surface interface %p as parent\n", *ppSurf);
2039 /* Now create the WineD3D Surface */
2040 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice,
2041 pDDSD->dwWidth,
2042 pDDSD->dwHeight,
2043 Format,
2044 TRUE /* Lockable */,
2045 FALSE /* Discard */,
2046 level,
2047 &(*ppSurf)->WineD3DSurface,
2048 ResType, Usage,
2049 Pool,
2050 WINED3DMULTISAMPLE_NONE,
2051 0 /* MultiSampleQuality */,
2052 0 /* SharedHandle */,
2053 ImplType,
2054 Parent);
2056 if(hr != D3D_OK)
2058 ERR("IWineD3DDevice::CreateSurface failed. hr = %08x\n", hr);
2059 return hr;
2062 /* Set the child of the parent implementation if it exists */
2063 if(parImpl)
2065 parImpl->child = (IUnknown *) (*ppSurf)->WineD3DSurface;
2066 /* The IParent releases the WineD3DSurface, and
2067 * the ddraw surface does that too. Hold a reference
2069 IWineD3DSurface_AddRef((*ppSurf)->WineD3DSurface);
2072 /* Increase the surface counter, and attach the surface */
2073 InterlockedIncrement(&This->surfaces);
2074 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
2076 /* Here we could store all created surfaces in the DirectDrawImpl structure,
2077 * But this could also be delegated to WineDDraw, as it keeps track of all its
2078 * resources. Not implemented for now, as there are more important things ;)
2081 /* Get the pixel format of the WineD3DSurface and store it.
2082 * Don't use the Format choosen above, WineD3D might have
2083 * changed it
2085 Desc.Format = &Format;
2086 Desc.Type = &ResType;
2087 Desc.Usage = &Usage;
2088 Desc.Pool = &dummy_d3dpool;
2089 Desc.Size = &dummy_uint;
2090 Desc.MultiSampleType = &dummy_mst;
2091 Desc.MultiSampleQuality = &dummy_dword;
2092 Desc.Width = &Width;
2093 Desc.Height = &Height;
2095 (*ppSurf)->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
2096 hr = IWineD3DSurface_GetDesc((*ppSurf)->WineD3DSurface, &Desc);
2097 if(hr != D3D_OK)
2099 ERR("IWineD3DSurface::GetDesc failed\n");
2100 IDirectDrawSurface7_Release( (IDirectDrawSurface7 *) *ppSurf);
2101 return hr;
2104 if(Format == WINED3DFMT_UNKNOWN)
2106 FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN\n");
2108 PixelFormat_WineD3DtoDD( &(*ppSurf)->surface_desc.u4.ddpfPixelFormat, Format);
2110 /* Anno 1602 stores the pitch right after surface creation, so make sure it's there.
2111 * I can't LockRect() the surface here because if OpenGL surfaces are in use, the
2112 * WineD3DDevice might not be usable for 3D yet, so an extra method was created.
2113 * TODO: Test other fourcc formats
2115 if(Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3 ||
2116 Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5)
2118 (*ppSurf)->surface_desc.dwFlags |= DDSD_LINEARSIZE;
2119 if(Format == WINED3DFMT_DXT1)
2121 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height) / 2;
2123 else
2125 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height);
2128 else
2130 (*ppSurf)->surface_desc.dwFlags |= DDSD_PITCH;
2131 (*ppSurf)->surface_desc.u1.lPitch = IWineD3DSurface_GetPitch((*ppSurf)->WineD3DSurface);
2134 /* Application passed a color key? Set it! */
2135 if(pDDSD->dwFlags & DDSD_CKDESTOVERLAY)
2137 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2138 DDCKEY_DESTOVERLAY,
2139 (WINEDDCOLORKEY *) &pDDSD->u3.ddckCKDestOverlay);
2141 if(pDDSD->dwFlags & DDSD_CKDESTBLT)
2143 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2144 DDCKEY_DESTBLT,
2145 (WINEDDCOLORKEY *) &pDDSD->ddckCKDestBlt);
2147 if(pDDSD->dwFlags & DDSD_CKSRCOVERLAY)
2149 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2150 DDCKEY_SRCOVERLAY,
2151 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcOverlay);
2153 if(pDDSD->dwFlags & DDSD_CKSRCBLT)
2155 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2156 DDCKEY_SRCBLT,
2157 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcBlt);
2159 if ( pDDSD->dwFlags & DDSD_LPSURFACE)
2161 hr = IWineD3DSurface_SetMem((*ppSurf)->WineD3DSurface, pDDSD->lpSurface);
2162 if(hr != WINED3D_OK)
2164 /* No need for a trace here, wined3d does that for us */
2165 IDirectDrawSurface7_Release(ICOM_INTERFACE((*ppSurf), IDirectDrawSurface7));
2166 return hr;
2170 return DD_OK;
2172 /*****************************************************************************
2173 * CreateAdditionalSurfaces
2175 * Creates a new mipmap chain.
2177 * Params:
2178 * root: Root surface to attach the newly created chain to
2179 * count: number of surfaces to create
2180 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2181 * effects on the caller
2182 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2183 * creates an additional surface without the mipmapping flags
2185 *****************************************************************************/
2186 static HRESULT
2187 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2188 IDirectDrawSurfaceImpl *root,
2189 UINT count,
2190 DDSURFACEDESC2 DDSD,
2191 BOOL CubeFaceRoot)
2193 UINT i, j, level = 0;
2194 HRESULT hr;
2195 IDirectDrawSurfaceImpl *last = root;
2197 for(i = 0; i < count; i++)
2199 IDirectDrawSurfaceImpl *object2 = NULL;
2201 /* increase the mipmap level, but only if a mipmap is created
2202 * In this case, also halve the size
2204 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2206 level++;
2207 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2208 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2209 /* Set the mipmap sublevel flag according to msdn */
2210 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2212 else
2214 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2216 CubeFaceRoot = FALSE;
2218 hr = IDirectDrawImpl_CreateNewSurface(This,
2219 &DDSD,
2220 &object2,
2221 level);
2222 if(hr != DD_OK)
2224 return hr;
2227 /* Add the new surface to the complex attachment array */
2228 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2230 if(last->complex_array[j]) continue;
2231 last->complex_array[j] = object2;
2232 break;
2234 last = object2;
2236 /* Remove the (possible) back buffer cap from the new surface description,
2237 * because only one surface in the flipping chain is a back buffer, one
2238 * is a front buffer, the others are just primary surfaces.
2240 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2242 return DD_OK;
2245 /*****************************************************************************
2246 * IDirectDraw7::CreateSurface
2248 * Creates a new IDirectDrawSurface object and returns its interface.
2250 * The surface connections with wined3d are a bit tricky. Basically it works
2251 * like this:
2253 * |------------------------| |-----------------|
2254 * | DDraw surface | | WineD3DSurface |
2255 * | | | |
2256 * | WineD3DSurface |-------------->| |
2257 * | Child |<------------->| Parent |
2258 * |------------------------| |-----------------|
2260 * The DDraw surface is the parent of the wined3d surface, and it releases
2261 * the WineD3DSurface when the ddraw surface is destroyed.
2263 * However, for all surfaces which can be in a container in WineD3D,
2264 * we have to do this. These surfaces are usually complex surfaces,
2265 * so this concerns primary surfaces with a front and a back buffer,
2266 * and textures.
2268 * |------------------------| |-----------------|
2269 * | DDraw surface | | Container |
2270 * | | | |
2271 * | Child |<------------->| Parent |
2272 * | Texture |<------------->| |
2273 * | WineD3DSurface |<----| | Levels |<--|
2274 * | Complex connection | | | | |
2275 * |------------------------| | |-----------------| |
2276 * ^ | |
2277 * | | |
2278 * | | |
2279 * | |------------------| | |-----------------| |
2280 * | | IParent | |-------->| WineD3DSurface | |
2281 * | | | | | |
2282 * | | Child |<------------->| Parent | |
2283 * | | | | Container |<--|
2284 * | |------------------| |-----------------| |
2285 * | |
2286 * | |----------------------| |
2287 * | | DDraw surface 2 | |
2288 * | | | |
2289 * |<->| Complex root Child | |
2290 * | | Texture | |
2291 * | | WineD3DSurface |<----| |
2292 * | |----------------------| | |
2293 * | | |
2294 * | |---------------------| | |-----------------| |
2295 * | | IParent | |----->| WineD3DSurface | |
2296 * | | | | | |
2297 * | | Child |<---------->| Parent | |
2298 * | |---------------------| | Container |<--|
2299 * | |-----------------| |
2300 * | |
2301 * | ---More surfaces can follow--- |
2303 * The reason is that the IWineD3DSwapchain(render target container)
2304 * and the IWineD3DTexure(Texture container) release the parents
2305 * of their surface's children, but by releasing the complex root
2306 * the surfaces which are complexly attached to it are destroyed
2307 * too. See IDirectDrawSurface::Release for a more detailed
2308 * explanation.
2310 * Params:
2311 * DDSD: Description of the surface to create
2312 * Surf: Address to store the interface pointer at
2313 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2314 * aggregation, so it has to be NULL
2316 * Returns:
2317 * DD_OK on success
2318 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2319 * DDERR_* if an error occurs
2321 *****************************************************************************/
2322 static HRESULT WINAPI
2323 IDirectDrawImpl_CreateSurface(IDirectDraw7 *iface,
2324 DDSURFACEDESC2 *DDSD,
2325 IDirectDrawSurface7 **Surf,
2326 IUnknown *UnkOuter)
2328 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2329 IDirectDrawSurfaceImpl *object = NULL;
2330 HRESULT hr;
2331 LONG extra_surfaces = 0;
2332 DDSURFACEDESC2 desc2;
2333 WINED3DDISPLAYMODE Mode;
2335 TRACE("(%p)->(%p,%p,%p)\n", This, DDSD, Surf, UnkOuter);
2337 /* Some checks before we start */
2338 if (TRACE_ON(ddraw))
2340 TRACE(" (%p) Requesting surface desc :\n", This);
2341 DDRAW_dump_surface_desc(DDSD);
2343 EnterCriticalSection(&ddraw_cs);
2345 if (UnkOuter != NULL)
2347 FIXME("(%p) : outer != NULL?\n", This);
2348 LeaveCriticalSection(&ddraw_cs);
2349 return CLASS_E_NOAGGREGATION; /* unchecked */
2352 if (Surf == NULL)
2354 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", This);
2355 LeaveCriticalSection(&ddraw_cs);
2356 return E_POINTER; /* unchecked */
2359 if (!(DDSD->dwFlags & DDSD_CAPS))
2361 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2362 DDSD->dwFlags |= DDSD_CAPS;
2364 if (DDSD->ddsCaps.dwCaps == 0)
2366 /* This has been checked on real Windows */
2367 DDSD->ddsCaps.dwCaps = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
2370 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2372 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2373 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2376 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2378 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2379 WARN("(%p) Null surface pointer specified, ignore it!\n", This);
2380 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2383 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2384 !(This->cooperative_level & DDSCL_EXCLUSIVE))
2386 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n", This);
2387 *Surf = NULL;
2388 LeaveCriticalSection(&ddraw_cs);
2389 return DDERR_NOEXCLUSIVEMODE;
2392 if(DDSD->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER)) {
2393 WARN("Application tried to create an explicit front or back buffer\n");
2394 LeaveCriticalSection(&ddraw_cs);
2395 return DDERR_INVALIDCAPS;
2397 /* Check cube maps but only if the size includes them */
2398 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2400 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2401 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2403 WARN("Cube map faces requested without cube map flag\n");
2404 LeaveCriticalSection(&ddraw_cs);
2405 return DDERR_INVALIDCAPS;
2407 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2408 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2410 WARN("Cube map without faces requested\n");
2411 LeaveCriticalSection(&ddraw_cs);
2412 return DDERR_INVALIDPARAMS;
2415 /* Quick tests confirm those can be created, but we don't do that yet */
2416 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2417 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2419 FIXME("Partial cube maps not supported yet\n");
2423 /* According to the msdn this flag is ignored by CreateSurface */
2424 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2425 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2427 /* Modify some flags */
2428 memset(&desc2, 0, sizeof(desc2));
2429 desc2.dwSize = sizeof(desc2); /* For the struct copy */
2430 DD_STRUCT_COPY_BYSIZE(&desc2, DDSD);
2431 desc2.dwSize = sizeof(desc2); /* To override a possibly smaller size */
2432 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2434 /* Get the video mode from WineD3D - we will need it */
2435 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
2436 0, /* Swapchain 0 */
2437 &Mode);
2438 if(FAILED(hr))
2440 ERR("Failed to read display mode from wined3d\n");
2441 switch(This->orig_bpp)
2443 case 8:
2444 Mode.Format = WINED3DFMT_P8;
2445 break;
2447 case 15:
2448 Mode.Format = WINED3DFMT_X1R5G5B5;
2449 break;
2451 case 16:
2452 Mode.Format = WINED3DFMT_R5G6B5;
2453 break;
2455 case 24:
2456 Mode.Format = WINED3DFMT_R8G8B8;
2457 break;
2459 case 32:
2460 Mode.Format = WINED3DFMT_X8R8G8B8;
2461 break;
2463 Mode.Width = This->orig_width;
2464 Mode.Height = This->orig_height;
2467 /* No pixelformat given? Use the current screen format */
2468 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2470 desc2.dwFlags |= DDSD_PIXELFORMAT;
2471 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2473 /* Wait: It could be a Z buffer */
2474 if(desc2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
2476 switch(desc2.u2.dwMipMapCount) /* Who had this glorious idea? */
2478 case 15:
2479 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D15S1);
2480 break;
2481 case 16:
2482 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D16);
2483 break;
2484 case 24:
2485 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D24X8);
2486 break;
2487 case 32:
2488 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D32);
2489 break;
2490 default:
2491 ERR("Unknown Z buffer bit depth\n");
2494 else
2496 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
2500 /* No Width or no Height? Use the original screen size
2502 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2503 !(desc2.dwFlags & DDSD_HEIGHT) )
2505 /* Invalid for non-render targets */
2506 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2508 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2509 *Surf = NULL;
2510 LeaveCriticalSection(&ddraw_cs);
2511 return DDERR_INVALIDPARAMS;
2514 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2515 desc2.dwWidth = Mode.Width;
2516 desc2.dwHeight = Mode.Height;
2519 /* Mipmap count fixes */
2520 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2522 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2524 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2526 /* Mipmap count is given, should not be 0 */
2527 if( desc2.u2.dwMipMapCount == 0 )
2529 LeaveCriticalSection(&ddraw_cs);
2530 return DDERR_INVALIDPARAMS;
2533 else
2535 /* Undocumented feature: Create sublevels until
2536 * either the width or the height is 1
2538 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2539 desc2.dwWidth : desc2.dwHeight;
2540 desc2.u2.dwMipMapCount = 0;
2541 while( min )
2543 desc2.u2.dwMipMapCount += 1;
2544 min >>= 1;
2548 else
2550 /* Not-complex mipmap -> Mipmapcount = 1 */
2551 desc2.u2.dwMipMapCount = 1;
2553 extra_surfaces = desc2.u2.dwMipMapCount - 1;
2555 /* There's a mipmap count in the created surface in any case */
2556 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2558 /* If no mipmap is given, the texture has only one level */
2560 /* The first surface is a front buffer, the back buffer is created afterwards */
2561 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2563 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2566 /* The root surface in a cube map is positive x */
2567 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2569 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2570 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2573 /* Create the first surface */
2574 hr = IDirectDrawImpl_CreateNewSurface(This, &desc2, &object, 0);
2575 if( hr != DD_OK)
2577 ERR("IDirectDrawImpl_CreateNewSurface failed with %08x\n", hr);
2578 LeaveCriticalSection(&ddraw_cs);
2579 return hr;
2581 object->is_complex_root = TRUE;
2583 *Surf = ICOM_INTERFACE(object, IDirectDrawSurface7);
2585 /* Create Additional surfaces if necessary
2586 * This applies to Primary surfaces which have a back buffer count
2587 * set, but not to mipmap textures. In case of Mipmap textures,
2588 * wineD3D takes care of the creation of additional surfaces
2590 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2592 extra_surfaces = DDSD->dwBackBufferCount;
2593 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2594 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2597 hr = DD_OK;
2598 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2600 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2601 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
2602 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2603 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
2604 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
2605 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2606 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
2607 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
2608 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2609 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
2610 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
2611 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2612 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
2613 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
2614 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2615 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
2616 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2619 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces, desc2, FALSE);
2620 if(hr != DD_OK)
2622 /* This destroys and possibly created surfaces too */
2623 IDirectDrawSurface_Release( ICOM_INTERFACE(object, IDirectDrawSurface7) );
2624 LeaveCriticalSection(&ddraw_cs);
2625 return hr;
2628 /* If the implementation is OpenGL and there's no d3ddevice, attach a d3ddevice
2629 * But attach the d3ddevice only if the currently created surface was
2630 * a primary surface (2D app in 3D mode) or a 3DDEVICE surface (3D app)
2631 * The only case I can think of where this doesn't apply is when a
2632 * 2D app was configured by the user to run with OpenGL and it didn't create
2633 * the render target as first surface. In this case the render target creation
2634 * will cause the 3D init.
2636 if( (This->ImplType == SURFACE_OPENGL) && !(This->d3d_initialized) &&
2637 desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE) )
2639 IDirectDrawSurfaceImpl *target = object, *surface;
2640 struct list *entry;
2642 /* Search for the primary to use as render target */
2643 LIST_FOR_EACH(entry, &This->surface_list)
2645 surface = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2646 if((surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER)) ==
2647 (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER))
2649 /* found */
2650 target = surface;
2651 TRACE("Using primary %p as render target\n", target);
2652 break;
2656 TRACE("(%p) Attaching a D3DDevice, rendertarget = %p\n", This, target);
2657 hr = IDirectDrawImpl_AttachD3DDevice(This, target);
2658 if(hr != D3D_OK)
2660 IDirectDrawSurfaceImpl *release_surf;
2661 ERR("IDirectDrawImpl_AttachD3DDevice failed, hr = %x\n", hr);
2662 *Surf = NULL;
2664 /* The before created surface structures are in an incomplete state here.
2665 * WineD3D holds the reference on the IParents, and it released them on the failure
2666 * already. So the regular release method implementation would fail on the attempt
2667 * to destroy either the IParents or the swapchain. So free the surface here.
2668 * The surface structure here is a list, not a tree, because onscreen targets
2669 * cannot be cube textures
2671 while(object)
2673 release_surf = object;
2674 object = object->complex_array[0];
2675 IDirectDrawSurfaceImpl_Destroy(release_surf);
2677 LeaveCriticalSection(&ddraw_cs);
2678 return hr;
2682 /* Addref the ddraw interface to keep an reference for each surface */
2683 IDirectDraw7_AddRef(iface);
2684 object->ifaceToRelease = (IUnknown *) iface;
2686 /* Create a WineD3DTexture if a texture was requested */
2687 if(desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
2689 UINT levels;
2690 WINED3DFORMAT Format;
2691 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
2693 This->tex_root = object;
2695 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2697 /* a mipmap is created, create enough levels */
2698 levels = desc2.u2.dwMipMapCount;
2700 else
2702 /* No mipmap is created, create one level */
2703 levels = 1;
2706 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM */
2707 if(DDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
2709 Pool = WINED3DPOOL_SYSTEMMEM;
2711 /* Should I forward the MANAGED cap to the managed pool ? */
2713 /* Get the format. It's set already by CreateNewSurface */
2714 Format = PixelFormat_DD2WineD3D(&object->surface_desc.u4.ddpfPixelFormat);
2716 /* The surfaces are already created, the callback only
2717 * passes the IWineD3DSurface to WineD3D
2719 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2721 hr = IWineD3DDevice_CreateCubeTexture(This->wineD3DDevice,
2722 DDSD->dwWidth, /* Edgelength */
2723 levels,
2724 0, /* usage */
2725 Format,
2726 Pool,
2727 (IWineD3DCubeTexture **) &object->wineD3DTexture,
2728 0, /* SharedHandle */
2729 (IUnknown *) ICOM_INTERFACE(object, IDirectDrawSurface7),
2730 D3D7CB_CreateSurface);
2732 else
2734 hr = IWineD3DDevice_CreateTexture(This->wineD3DDevice,
2735 DDSD->dwWidth, DDSD->dwHeight,
2736 levels, /* MipMapCount = Levels */
2737 0, /* usage */
2738 Format,
2739 Pool,
2740 (IWineD3DTexture **) &object->wineD3DTexture,
2741 0, /* SharedHandle */
2742 (IUnknown *) ICOM_INTERFACE(object, IDirectDrawSurface7),
2743 D3D7CB_CreateSurface );
2745 This->tex_root = NULL;
2748 LeaveCriticalSection(&ddraw_cs);
2749 return hr;
2752 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
2753 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
2755 static BOOL
2756 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
2757 const DDPIXELFORMAT *provided)
2759 /* Some flags must be present in both or neither for a match. */
2760 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
2761 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
2762 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
2764 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2765 return FALSE;
2767 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
2768 return FALSE;
2770 if (requested->dwFlags & DDPF_FOURCC)
2771 if (requested->dwFourCC != provided->dwFourCC)
2772 return FALSE;
2774 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
2775 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2776 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
2777 return FALSE;
2779 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2780 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2781 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
2782 return FALSE;
2784 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
2785 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
2786 return FALSE;
2788 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
2789 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2790 |DDPF_BUMPDUDV))
2791 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
2792 return FALSE;
2794 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
2795 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
2796 return FALSE;
2798 return TRUE;
2801 static BOOL
2802 IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested,
2803 const DDSURFACEDESC2* provided)
2805 struct compare_info
2807 DWORD flag;
2808 ptrdiff_t offset;
2809 size_t size;
2812 #define CMP(FLAG, FIELD) \
2813 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
2814 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
2816 static const struct compare_info compare[] =
2818 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
2819 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
2820 CMP(CAPS, ddsCaps),
2821 CMP(CKDESTBLT, ddckCKDestBlt),
2822 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
2823 CMP(CKSRCBLT, ddckCKSrcBlt),
2824 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
2825 CMP(HEIGHT, dwHeight),
2826 CMP(LINEARSIZE, u1 /* dwLinearSize */),
2827 CMP(LPSURFACE, lpSurface),
2828 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
2829 CMP(PITCH, u1 /* lPitch */),
2830 /* PIXELFORMAT: manual */
2831 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
2832 CMP(TEXTURESTAGE, dwTextureStage),
2833 CMP(WIDTH, dwWidth),
2834 /* ZBUFFERBITDEPTH: "obsolete" */
2837 #undef CMP
2839 unsigned int i;
2841 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2842 return FALSE;
2844 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
2846 if (requested->dwFlags & compare[i].flag
2847 && memcmp((const char *)provided + compare[i].offset,
2848 (const char *)requested + compare[i].offset,
2849 compare[i].size) != 0)
2850 return FALSE;
2853 if (requested->dwFlags & DDSD_PIXELFORMAT)
2855 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
2856 &provided->u4.ddpfPixelFormat))
2857 return FALSE;
2860 return TRUE;
2863 #undef DDENUMSURFACES_SEARCHTYPE
2864 #undef DDENUMSURFACES_MATCHTYPE
2866 /*****************************************************************************
2867 * IDirectDraw7::EnumSurfaces
2869 * Loops through all surfaces attached to this device and calls the
2870 * application callback. This can't be relayed to WineD3DDevice,
2871 * because some WineD3DSurfaces' parents are IParent objects
2873 * Params:
2874 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
2875 * DDSD: Description to filter for
2876 * Context: Application-provided pointer, it's passed unmodified to the
2877 * Callback function
2878 * Callback: Address to call for each surface
2880 * Returns:
2881 * DDERR_INVALIDPARAMS if the callback is NULL
2882 * DD_OK on success
2884 *****************************************************************************/
2885 static HRESULT WINAPI
2886 IDirectDrawImpl_EnumSurfaces(IDirectDraw7 *iface,
2887 DWORD Flags,
2888 DDSURFACEDESC2 *DDSD,
2889 void *Context,
2890 LPDDENUMSURFACESCALLBACK7 Callback)
2892 /* The surface enumeration is handled by WineDDraw,
2893 * because it keeps track of all surfaces attached to
2894 * it. The filtering is done by our callback function,
2895 * because WineDDraw doesn't handle ddraw-like surface
2896 * caps structures
2898 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2899 IDirectDrawSurfaceImpl *surf;
2900 BOOL all, nomatch;
2901 DDSURFACEDESC2 desc;
2902 struct list *entry, *entry2;
2904 all = Flags & DDENUMSURFACES_ALL;
2905 nomatch = Flags & DDENUMSURFACES_NOMATCH;
2907 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, DDSD, Context, Callback);
2908 EnterCriticalSection(&ddraw_cs);
2910 if(!Callback)
2912 LeaveCriticalSection(&ddraw_cs);
2913 return DDERR_INVALIDPARAMS;
2916 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
2917 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
2919 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2920 if (all || (nomatch != IDirectDrawImpl_DDSD_Match(DDSD, &surf->surface_desc)))
2922 desc = surf->surface_desc;
2923 IDirectDrawSurface7_AddRef(ICOM_INTERFACE(surf, IDirectDrawSurface7));
2924 if(Callback( ICOM_INTERFACE(surf, IDirectDrawSurface7), &desc, Context) != DDENUMRET_OK)
2926 LeaveCriticalSection(&ddraw_cs);
2927 return DD_OK;
2931 LeaveCriticalSection(&ddraw_cs);
2932 return DD_OK;
2935 static HRESULT WINAPI
2936 findRenderTarget(IDirectDrawSurface7 *surface,
2937 DDSURFACEDESC2 *desc,
2938 void *ctx)
2940 IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, surface);
2941 IDirectDrawSurfaceImpl **target = (IDirectDrawSurfaceImpl **) ctx;
2943 if(!surf->isRenderTarget) {
2944 *target = surf;
2945 IDirectDrawSurface7_Release(surface);
2946 return DDENUMRET_CANCEL;
2949 /* Recurse into the surface tree */
2950 IDirectDrawSurface7_EnumAttachedSurfaces(surface, ctx, findRenderTarget);
2952 IDirectDrawSurface7_Release(surface);
2953 if(*target) return DDENUMRET_CANCEL;
2954 else return DDENUMRET_OK; /* Continue with the next neighbor surface */
2957 /*****************************************************************************
2958 * D3D7CB_CreateRenderTarget
2960 * Callback called by WineD3D to create Surfaces for render target usage
2961 * This function takes the D3D target from the IDirectDrawImpl structure,
2962 * and returns the WineD3DSurface. To avoid double usage, the surface
2963 * is marked as render target afterwards
2965 * Params
2966 * device: The WineD3DDevice's parent
2967 * Width, Height, Format: Dimensions and pixelformat of the render target
2968 * Ignored, because the surface already exists
2969 * MultiSample, MultisampleQuality, Lockable: Ignored for the same reason
2970 * Lockable: ignored
2971 * ppSurface: Address to pass the surface pointer back at
2972 * pSharedHandle: Ignored
2974 * Returns:
2975 * Always returns D3D_OK
2977 *****************************************************************************/
2978 static HRESULT WINAPI
2979 D3D7CB_CreateRenderTarget(IUnknown *device, IUnknown *pSuperior,
2980 UINT Width, UINT Height,
2981 WINED3DFORMAT Format,
2982 WINED3DMULTISAMPLE_TYPE MultiSample,
2983 DWORD MultisampleQuality,
2984 BOOL Lockable,
2985 IWineD3DSurface** ppSurface,
2986 HANDLE* pSharedHandle)
2988 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
2989 IDirectDrawSurfaceImpl *d3dSurface = This->d3d_target, *target = NULL;
2990 TRACE("(%p) call back\n", device);
2992 if(d3dSurface->isRenderTarget)
2994 IDirectDrawSurface7_EnumAttachedSurfaces(ICOM_INTERFACE(d3dSurface, IDirectDrawSurface7),
2995 &target, findRenderTarget);
2997 else
2999 target = d3dSurface;
3002 if(!target)
3004 target = This->d3d_target;
3005 ERR(" (%p) : No DirectDrawSurface found to create the back buffer. Using the front buffer as back buffer. Uncertain consequences\n", This);
3008 /* TODO: Return failure if the dimensions do not match, but this shouldn't happen */
3010 *ppSurface = target->WineD3DSurface;
3011 target->isRenderTarget = TRUE;
3012 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *ppSurface, d3dSurface);
3013 return D3D_OK;
3016 static HRESULT WINAPI
3017 D3D7CB_CreateDepthStencilSurface(IUnknown *device,
3018 IUnknown *pSuperior,
3019 UINT Width,
3020 UINT Height,
3021 WINED3DFORMAT Format,
3022 WINED3DMULTISAMPLE_TYPE MultiSample,
3023 DWORD MultisampleQuality,
3024 BOOL Discard,
3025 IWineD3DSurface** ppSurface,
3026 HANDLE* pSharedHandle)
3028 /* Create a Depth Stencil surface to make WineD3D happy */
3029 HRESULT hr = D3D_OK;
3030 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
3031 DDSURFACEDESC2 ddsd;
3033 TRACE("(%p) call back\n", device);
3035 *ppSurface = NULL;
3037 /* Create a DirectDraw surface */
3038 memset(&ddsd, 0, sizeof(ddsd));
3039 ddsd.dwSize = sizeof(ddsd);
3040 ddsd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
3041 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
3042 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
3043 ddsd.dwHeight = Height;
3044 ddsd.dwWidth = Width;
3045 if(Format != 0)
3047 PixelFormat_WineD3DtoDD(&ddsd.u4.ddpfPixelFormat, Format);
3049 else
3051 ddsd.dwFlags ^= DDSD_PIXELFORMAT;
3054 This->depthstencil = TRUE;
3055 hr = IDirectDraw7_CreateSurface((IDirectDraw7 *) This,
3056 &ddsd,
3057 (IDirectDrawSurface7 **) &This->DepthStencilBuffer,
3058 NULL);
3059 This->depthstencil = FALSE;
3060 if(FAILED(hr))
3062 ERR(" (%p) Creating a DepthStencil Surface failed, result = %x\n", This, hr);
3063 return hr;
3065 *ppSurface = This->DepthStencilBuffer->WineD3DSurface;
3066 return D3D_OK;
3069 /*****************************************************************************
3070 * D3D7CB_CreateAdditionalSwapChain
3072 * Callback function for WineD3D which creates a new WineD3DSwapchain
3073 * interface. It also creates an IParent interface to store that pointer,
3074 * so the WineD3DSwapchain has a parent and can be released when the D3D
3075 * device is destroyed
3076 *****************************************************************************/
3077 static HRESULT WINAPI
3078 D3D7CB_CreateAdditionalSwapChain(IUnknown *device,
3079 WINED3DPRESENT_PARAMETERS* pPresentationParameters,
3080 IWineD3DSwapChain ** ppSwapChain)
3082 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
3083 IParentImpl *object = NULL;
3084 HRESULT res = D3D_OK;
3085 IWineD3DSwapChain *swapchain;
3086 TRACE("(%p) call back\n", device);
3088 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IParentImpl));
3089 if (NULL == object)
3091 FIXME("Allocation of memory failed\n");
3092 *ppSwapChain = NULL;
3093 return DDERR_OUTOFVIDEOMEMORY;
3096 ICOM_INIT_INTERFACE(object, IParent, IParent_Vtbl);
3097 object->ref = 1;
3099 res = IWineD3DDevice_CreateAdditionalSwapChain(This->wineD3DDevice,
3100 pPresentationParameters,
3101 &swapchain,
3102 (IUnknown*) ICOM_INTERFACE(object, IParent),
3103 D3D7CB_CreateRenderTarget,
3104 D3D7CB_CreateDepthStencilSurface);
3105 if (res != D3D_OK)
3107 FIXME("(%p) call to IWineD3DDevice_CreateAdditionalSwapChain failed\n", This);
3108 HeapFree(GetProcessHeap(), 0 , object);
3109 *ppSwapChain = NULL;
3111 else
3113 *ppSwapChain = swapchain;
3114 object->child = (IUnknown *) swapchain;
3117 return res;
3120 /*****************************************************************************
3121 * IDirectDrawImpl_AttachD3DDevice
3123 * Initializes the D3D capabilities of WineD3D
3125 * Params:
3126 * primary: The primary surface for D3D
3128 * Returns
3129 * DD_OK on success,
3130 * DDERR_* otherwise
3132 *****************************************************************************/
3133 static HRESULT WINAPI
3134 IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This,
3135 IDirectDrawSurfaceImpl *primary)
3137 HRESULT hr;
3138 HWND window;
3140 WINED3DPRESENT_PARAMETERS localParameters;
3142 TRACE("(%p)->(%p)\n", This, primary);
3144 /* Get the window */
3145 hr = IWineD3DDevice_GetHWND(This->wineD3DDevice,
3146 &window);
3147 if(hr != D3D_OK)
3149 ERR("IWineD3DDevice::GetHWND failed\n");
3150 return hr;
3153 /* If there's no window, create a hidden window. WineD3D needs it */
3154 if(window == 0)
3156 window = CreateWindowExA(0, This->classname, "Hidden D3D Window",
3157 WS_DISABLED, 0, 0,
3158 GetSystemMetrics(SM_CXSCREEN),
3159 GetSystemMetrics(SM_CYSCREEN),
3160 NULL, NULL, GetModuleHandleA(0), NULL);
3162 ShowWindow(window, SW_HIDE); /* Just to be sure */
3163 WARN("(%p) No window for the Direct3DDevice, created a hidden window. HWND=%p\n", This, window);
3164 This->d3d_window = window;
3166 else
3168 TRACE("(%p) Using existing window %p for Direct3D rendering\n", This, window);
3171 /* Store the future Render Target surface */
3172 This->d3d_target = primary;
3174 /* Use the surface description for the device parameters, not the
3175 * Device settings. The app might render to an offscreen surface
3177 localParameters.BackBufferWidth = primary->surface_desc.dwWidth;
3178 localParameters.BackBufferHeight = primary->surface_desc.dwHeight;
3179 localParameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
3180 localParameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
3181 localParameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
3182 localParameters.MultiSampleQuality = 0;
3183 localParameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
3184 localParameters.hDeviceWindow = window;
3185 localParameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
3186 localParameters.EnableAutoDepthStencil = TRUE;
3187 localParameters.AutoDepthStencilFormat = WINED3DFMT_D16;
3188 localParameters.Flags = 0;
3189 localParameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
3190 localParameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
3192 TRACE("Passing mode %d\n", localParameters.BackBufferFormat);
3194 /* Set this NOW, otherwise creating the depth stencil surface will cause a
3195 * recursive loop until ram or emulated video memory is full
3197 This->d3d_initialized = TRUE;
3199 hr = IWineD3DDevice_Init3D(This->wineD3DDevice,
3200 &localParameters,
3201 D3D7CB_CreateAdditionalSwapChain);
3202 if(FAILED(hr))
3204 This->d3d_target = NULL;
3205 This->d3d_initialized = FALSE;
3206 return hr;
3209 This->declArraySize = 2;
3210 This->decls = HeapAlloc(GetProcessHeap(),
3211 HEAP_ZERO_MEMORY,
3212 sizeof(*This->decls) * This->declArraySize);
3213 if(!This->decls)
3215 ERR("Error allocating an array for the converted vertex decls\n");
3216 This->declArraySize = 0;
3217 hr = IWineD3DDevice_Uninit3D(This->wineD3DDevice,
3218 D3D7CB_DestroyDepthStencilSurface,
3219 D3D7CB_DestroySwapChain);
3220 return E_OUTOFMEMORY;
3223 /* Create an Index Buffer parent */
3224 TRACE("(%p) Successfully initialized 3D\n", This);
3225 return DD_OK;
3228 /*****************************************************************************
3229 * DirectDrawCreateClipper (DDRAW.@)
3231 * Creates a new IDirectDrawClipper object.
3233 * Params:
3234 * Clipper: Address to write the interface pointer to
3235 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3236 * NULL
3238 * Returns:
3239 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3240 * E_OUTOFMEMORY if allocating the object failed
3242 *****************************************************************************/
3243 HRESULT WINAPI
3244 DirectDrawCreateClipper(DWORD Flags,
3245 LPDIRECTDRAWCLIPPER *Clipper,
3246 IUnknown *UnkOuter)
3248 IDirectDrawClipperImpl* object;
3249 TRACE("(%08x,%p,%p)\n", Flags, Clipper, UnkOuter);
3251 EnterCriticalSection(&ddraw_cs);
3252 if (UnkOuter != NULL)
3254 LeaveCriticalSection(&ddraw_cs);
3255 return CLASS_E_NOAGGREGATION;
3258 if (!LoadWineD3D())
3260 LeaveCriticalSection(&ddraw_cs);
3261 return DDERR_NODIRECTDRAWSUPPORT;
3264 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3265 sizeof(IDirectDrawClipperImpl));
3266 if (object == NULL)
3268 LeaveCriticalSection(&ddraw_cs);
3269 return E_OUTOFMEMORY;
3272 ICOM_INIT_INTERFACE(object, IDirectDrawClipper, IDirectDrawClipper_Vtbl);
3273 object->ref = 1;
3274 object->wineD3DClipper = pWineDirect3DCreateClipper((IUnknown *) object);
3275 if(!object->wineD3DClipper)
3277 HeapFree(GetProcessHeap(), 0, object);
3278 LeaveCriticalSection(&ddraw_cs);
3279 return E_OUTOFMEMORY;
3282 *Clipper = (IDirectDrawClipper *) object;
3283 LeaveCriticalSection(&ddraw_cs);
3284 return DD_OK;
3287 /*****************************************************************************
3288 * IDirectDraw7::CreateClipper
3290 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3292 *****************************************************************************/
3293 static HRESULT WINAPI
3294 IDirectDrawImpl_CreateClipper(IDirectDraw7 *iface,
3295 DWORD Flags,
3296 IDirectDrawClipper **Clipper,
3297 IUnknown *UnkOuter)
3299 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3300 TRACE("(%p)->(%x,%p,%p)\n", This, Flags, Clipper, UnkOuter);
3301 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3304 /*****************************************************************************
3305 * IDirectDraw7::CreatePalette
3307 * Creates a new IDirectDrawPalette object
3309 * Params:
3310 * Flags: The flags for the new clipper
3311 * ColorTable: Color table to assign to the new clipper
3312 * Palette: Address to write the interface pointer to
3313 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3314 * NULL
3316 * Returns:
3317 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3318 * E_OUTOFMEMORY if allocating the object failed
3320 *****************************************************************************/
3321 static HRESULT WINAPI
3322 IDirectDrawImpl_CreatePalette(IDirectDraw7 *iface,
3323 DWORD Flags,
3324 PALETTEENTRY *ColorTable,
3325 IDirectDrawPalette **Palette,
3326 IUnknown *pUnkOuter)
3328 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3329 IDirectDrawPaletteImpl *object;
3330 HRESULT hr = DDERR_GENERIC;
3331 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, ColorTable, Palette, pUnkOuter);
3333 EnterCriticalSection(&ddraw_cs);
3334 if(pUnkOuter != NULL)
3336 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
3337 LeaveCriticalSection(&ddraw_cs);
3338 return CLASS_E_NOAGGREGATION;
3341 /* The refcount test shows that a cooplevel is required for this */
3342 if(!This->cooperative_level)
3344 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3345 LeaveCriticalSection(&ddraw_cs);
3346 return DDERR_NOCOOPERATIVELEVELSET;
3349 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
3350 if(!object)
3352 ERR("Out of memory when allocating memory for a palette implementation\n");
3353 LeaveCriticalSection(&ddraw_cs);
3354 return E_OUTOFMEMORY;
3357 ICOM_INIT_INTERFACE(object, IDirectDrawPalette, IDirectDrawPalette_Vtbl);
3358 object->ref = 1;
3359 object->ddraw_owner = This;
3361 hr = IWineD3DDevice_CreatePalette(This->wineD3DDevice, Flags, ColorTable, &object->wineD3DPalette, (IUnknown *) ICOM_INTERFACE(object, IDirectDrawPalette) );
3362 if(hr != DD_OK)
3364 HeapFree(GetProcessHeap(), 0, object);
3365 LeaveCriticalSection(&ddraw_cs);
3366 return hr;
3369 IDirectDraw7_AddRef(iface);
3370 object->ifaceToRelease = (IUnknown *) iface;
3371 *Palette = ICOM_INTERFACE(object, IDirectDrawPalette);
3372 LeaveCriticalSection(&ddraw_cs);
3373 return DD_OK;
3376 /*****************************************************************************
3377 * IDirectDraw7::DuplicateSurface
3379 * Duplicates a surface. The surface memory points to the same memory as
3380 * the original surface, and it's released when the last surface referencing
3381 * it is released. I guess that's beyond Wine's surface management right now
3382 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3383 * test application to implement this)
3385 * Params:
3386 * Src: Address of the source surface
3387 * Dest: Address to write the new surface pointer to
3389 * Returns:
3390 * See IDirectDraw7::CreateSurface
3392 *****************************************************************************/
3393 static HRESULT WINAPI
3394 IDirectDrawImpl_DuplicateSurface(IDirectDraw7 *iface,
3395 IDirectDrawSurface7 *Src,
3396 IDirectDrawSurface7 **Dest)
3398 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3399 IDirectDrawSurfaceImpl *Surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Src);
3401 FIXME("(%p)->(%p,%p)\n", This, Surf, Dest);
3403 /* For now, simply create a new, independent surface */
3404 return IDirectDraw7_CreateSurface(iface,
3405 &Surf->surface_desc,
3406 Dest,
3407 NULL);
3410 /*****************************************************************************
3411 * IDirectDraw7 VTable
3412 *****************************************************************************/
3413 const IDirectDraw7Vtbl IDirectDraw7_Vtbl =
3415 /*** IUnknown ***/
3416 IDirectDrawImpl_QueryInterface,
3417 IDirectDrawImpl_AddRef,
3418 IDirectDrawImpl_Release,
3419 /*** IDirectDraw ***/
3420 IDirectDrawImpl_Compact,
3421 IDirectDrawImpl_CreateClipper,
3422 IDirectDrawImpl_CreatePalette,
3423 IDirectDrawImpl_CreateSurface,
3424 IDirectDrawImpl_DuplicateSurface,
3425 IDirectDrawImpl_EnumDisplayModes,
3426 IDirectDrawImpl_EnumSurfaces,
3427 IDirectDrawImpl_FlipToGDISurface,
3428 IDirectDrawImpl_GetCaps,
3429 IDirectDrawImpl_GetDisplayMode,
3430 IDirectDrawImpl_GetFourCCCodes,
3431 IDirectDrawImpl_GetGDISurface,
3432 IDirectDrawImpl_GetMonitorFrequency,
3433 IDirectDrawImpl_GetScanLine,
3434 IDirectDrawImpl_GetVerticalBlankStatus,
3435 IDirectDrawImpl_Initialize,
3436 IDirectDrawImpl_RestoreDisplayMode,
3437 IDirectDrawImpl_SetCooperativeLevel,
3438 IDirectDrawImpl_SetDisplayMode,
3439 IDirectDrawImpl_WaitForVerticalBlank,
3440 /*** IDirectDraw2 ***/
3441 IDirectDrawImpl_GetAvailableVidMem,
3442 /*** IDirectDraw7 ***/
3443 IDirectDrawImpl_GetSurfaceFromDC,
3444 IDirectDrawImpl_RestoreAllSurfaces,
3445 IDirectDrawImpl_TestCooperativeLevel,
3446 IDirectDrawImpl_GetDeviceIdentifier,
3447 /*** IDirectDraw7 ***/
3448 IDirectDrawImpl_StartModeTest,
3449 IDirectDrawImpl_EvaluateMode
3452 /*****************************************************************************
3453 * IDirectDrawImpl_FindDecl
3455 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
3456 * if none was found.
3458 * This function is in ddraw.c and the DDraw object space because D3D7
3459 * vertex buffers are created using the IDirect3D interface to the ddraw
3460 * object, so they can be valid across D3D devices(theoretically. The ddraw
3461 * object also owns the wined3d device
3463 * Parameters:
3464 * This: Device
3465 * fvf: Fvf to find the decl for
3467 * Returns:
3468 * NULL in case of an error, the IWineD3DVertexDeclaration interface for the
3469 * fvf otherwise.
3471 *****************************************************************************/
3472 IWineD3DVertexDeclaration *
3473 IDirectDrawImpl_FindDecl(IDirectDrawImpl *This,
3474 DWORD fvf)
3476 HRESULT hr;
3477 IWineD3DVertexDeclaration* pDecl = NULL;
3478 int p, low, high; /* deliberately signed */
3479 struct FvfToDecl *convertedDecls = This->decls;
3481 TRACE("Searching for declaration for fvf %08x... ", fvf);
3483 low = 0;
3484 high = This->numConvertedDecls - 1;
3485 while(low <= high) {
3486 p = (low + high) >> 1;
3487 TRACE("%d ", p);
3488 if(convertedDecls[p].fvf == fvf) {
3489 TRACE("found %p\n", convertedDecls[p].decl);
3490 return convertedDecls[p].decl;
3491 } else if(convertedDecls[p].fvf < fvf) {
3492 low = p + 1;
3493 } else {
3494 high = p - 1;
3497 TRACE("not found. Creating and inserting at position %d.\n", low);
3499 hr = IWineD3DDevice_CreateVertexDeclarationFromFVF(This->wineD3DDevice,
3500 &pDecl,
3501 (IUnknown *) ICOM_INTERFACE(This, IDirectDraw7),
3502 fvf);
3503 if (hr != S_OK) return NULL;
3505 if(This->declArraySize == This->numConvertedDecls) {
3506 int grow = max(This->declArraySize / 2, 8);
3507 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
3508 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
3509 if(!convertedDecls) {
3510 /* This will destroy it */
3511 IWineD3DVertexDeclaration_Release(pDecl);
3512 return NULL;
3514 This->decls = convertedDecls;
3515 This->declArraySize += grow;
3518 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
3519 convertedDecls[low].decl = pDecl;
3520 convertedDecls[low].fvf = fvf;
3521 This->numConvertedDecls++;
3523 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
3524 return pDecl;