d3d: Remove AddRef from IWineD3DDevice_GetBackBuffer.
[wine.git] / dlls / ddraw / ddraw.c
blobde3c704f3ef75ab1be34aeea4056bbbbfc9f11e6
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
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <stdlib.h>
30 #define COBJMACROS
31 #define NONAMELESSUNION
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winnls.h"
36 #include "winerror.h"
37 #include "wingdi.h"
38 #include "wine/exception.h"
39 #include "excpt.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 static BOOL IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested, const DDSURFACEDESC2* provided);
50 static HRESULT WINAPI IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This, IDirectDrawSurfaceImpl *primary);
51 static HRESULT WINAPI IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This, DDSURFACEDESC2 *pDDSD, IDirectDrawSurfaceImpl **ppSurf, UINT level);
53 /* Device identifier. Don't relay it to WineD3D */
54 static const DDDEVICEIDENTIFIER2 deviceidentifier =
56 "display",
57 "DirectDraw HAL",
58 { { 0x00010001, 0x00010001 } },
59 0, 0, 0, 0,
60 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
61 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
65 /*****************************************************************************
66 * IUnknown Methods
67 *****************************************************************************/
69 /*****************************************************************************
70 * IDirectDraw7::QueryInterface
72 * Queries different interfaces of the DirectDraw object. It can return
73 * IDirectDraw interfaces in version 1, 2, 4 and 7, and IDirect3D interfaces
74 * in version 1, 2, 3 and 7. An IDirect3DDevice can be created with this
75 * method.
76 * The returned interface is AddRef()-ed before it's returned
78 * Rules for QueryInterface:
79 * http://msdn.microsoft.com/library/default.asp? \
80 * url=/library/en-us/com/html/6db17ed8-06e4-4bae-bc26-113176cc7e0e.asp
82 * Used for version 1, 2, 4 and 7
84 * Params:
85 * refiid: Interface ID asked for
86 * obj: Used to return the interface pointer
88 * Returns:
89 * S_OK if an interface was found
90 * E_NOINTERFACE if the requested interface wasn't found
92 *****************************************************************************/
93 static HRESULT WINAPI
94 IDirectDrawImpl_QueryInterface(IDirectDraw7 *iface,
95 REFIID refiid,
96 void **obj)
98 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
100 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(refiid), obj);
102 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
103 *obj = NULL;
105 if(!refiid)
106 return DDERR_INVALIDPARAMS;
108 /* Check DirectDraw Interfaces */
109 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
110 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
112 *obj = ICOM_INTERFACE(This, IDirectDraw7);
113 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
115 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
117 *obj = ICOM_INTERFACE(This, IDirectDraw4);
118 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
120 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
122 *obj = ICOM_INTERFACE(This, IDirectDraw2);
123 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
125 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
127 *obj = ICOM_INTERFACE(This, IDirectDraw);
128 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
131 /* Direct3D
132 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
133 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
134 * who had this idea and why. The older interfaces can query and IDirect3D version
135 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
136 * and messy to implement with the common creation function, so it has been left out here.
138 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
139 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
140 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
141 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
143 /* Check the surface implementation */
144 if(This->ImplType == SURFACE_UNKNOWN)
146 /* Apps may create the IDirect3D Interface before the primary surface.
147 * set the surface implementation */
148 This->ImplType = SURFACE_OPENGL;
149 TRACE("(%p) Choosing OpenGL surfaces because a Direct3D interface was requested\n", This);
151 else if(This->ImplType != SURFACE_OPENGL && DefaultSurfaceType == SURFACE_UNKNOWN)
153 ERR("(%p) The App is requesting a D3D device, but a non-OpenGL surface type was choosen. Prepare for trouble!\n", This);
154 ERR(" (%p) You may want to contact wine-devel for help\n", This);
155 /* Should I assert(0) here??? */
157 else if(This->ImplType != SURFACE_OPENGL)
159 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
160 /* Do not abort here, only reject 3D Device creation */
163 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
165 This->d3dversion = 1;
166 *obj = ICOM_INTERFACE(This, IDirect3D);
167 TRACE(" returning Direct3D interface at %p.\n", *obj);
169 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
171 This->d3dversion = 2;
172 *obj = ICOM_INTERFACE(This, IDirect3D2);
173 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
175 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
177 This->d3dversion = 3;
178 *obj = ICOM_INTERFACE(This, IDirect3D3);
179 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
181 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
183 This->d3dversion = 7;
184 *obj = ICOM_INTERFACE(This, IDirect3D7);
185 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
189 /* Unknown interface */
190 else
192 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
193 return E_NOINTERFACE;
196 IUnknown_AddRef( (IUnknown *) *obj );
197 return S_OK;
200 /*****************************************************************************
201 * IDirectDraw7::AddRef
203 * Increases the interfaces refcount, basically
205 * DDraw refcounting is a bit tricky. The different DirectDraw interface
206 * versions have individual refcounts, but the IDirect3D interfaces do not.
207 * All interfaces are from one object, that means calling QueryInterface on an
208 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
209 * IDirectDrawImpl object.
211 * That means all AddRef and Release implementations of IDirectDrawX work
212 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
213 * except of IDirect3D7 which thunks to IDirectDraw7
215 * Returns: The new refcount
217 *****************************************************************************/
218 static ULONG WINAPI
219 IDirectDrawImpl_AddRef(IDirectDraw7 *iface)
221 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
222 ULONG ref = InterlockedIncrement(&This->ref7);
224 TRACE("(%p) : incrementing IDirectDraw7 refcount from %u.\n", This, ref -1);
226 if(ref == 1) InterlockedIncrement(&This->numIfaces);
228 return ref;
231 /*****************************************************************************
232 * IDirectDrawImpl_Destroy
234 * Destroys a ddraw object if all refcounts are 0. This is to share code
235 * between the IDirectDrawX::Release functions
237 * Params:
238 * This: DirectDraw object to destroy
240 *****************************************************************************/
241 void
242 IDirectDrawImpl_Destroy(IDirectDrawImpl *This)
244 /* Clear the cooplevel to restore window and display mode */
245 IDirectDraw7_SetCooperativeLevel(ICOM_INTERFACE(This, IDirectDraw7),
246 NULL,
247 DDSCL_NORMAL);
249 /* Destroy the device window if we created one */
250 if(This->devicewindow != 0)
252 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
253 DestroyWindow(This->devicewindow);
254 This->devicewindow = 0;
257 /* Unregister the window class */
258 UnregisterClassA(This->classname, 0);
260 remove_ddraw_object(This);
262 /* Release the attached WineD3D stuff */
263 IWineD3DDevice_Release(This->wineD3DDevice);
264 IWineD3D_Release(This->wineD3D);
266 /* Now free the object */
267 HeapFree(GetProcessHeap(), 0, This);
270 /*****************************************************************************
271 * IDirectDraw7::Release
273 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
275 * Returns: The new refcount
276 *****************************************************************************/
277 static ULONG WINAPI
278 IDirectDrawImpl_Release(IDirectDraw7 *iface)
280 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
281 ULONG ref = InterlockedDecrement(&This->ref7);
283 TRACE("(%p)->() decrementing IDirectDraw7 refcount from %u.\n", This, ref +1);
285 if(ref == 0)
287 ULONG ifacecount = InterlockedDecrement(&This->numIfaces);
288 if(ifacecount == 0) IDirectDrawImpl_Destroy(This);
291 return ref;
294 /*****************************************************************************
295 * IDirectDraw methods
296 *****************************************************************************/
298 /*****************************************************************************
299 * IDirectDrawImpl_SetupExclusiveWindow
301 * Helper function that modifies a HWND's Style and ExStyle for proper
302 * fullscreen use.
304 * Params:
305 * This: Pointer to the DirectDraw implementation
306 * HWND: Window to setup
308 *****************************************************************************/
309 static void
310 IDirectDrawImpl_SetupFullscreenWindow(IDirectDrawImpl *This,
311 HWND window)
313 LONG style, exStyle;
314 /* Don't do anything if an original style is stored.
315 * That shouldn't happen
317 TRACE("(%p): Setting up window %p for exclusive mode\n", This, window);
318 if( (This->style != 0) && (This->exStyle != 0) )
320 ERR("(%p) Want to change the window parameters of HWND %p, but \
321 another style is stored for restauration afterwards\n", This, window);
324 /* Get the parameters and save them */
325 style = GetWindowLongW(window, GWL_STYLE);
326 exStyle = GetWindowLongW(window, GWL_EXSTYLE);
327 This->style = style;
328 This->exStyle = exStyle;
330 /* Filter out window decorations */
331 style &= ~WS_CAPTION;
332 style &= ~WS_THICKFRAME;
333 exStyle &= ~WS_EX_WINDOWEDGE;
334 exStyle &= ~WS_EX_CLIENTEDGE;
336 /* Make sure the window is managed, otherwise we won't get keyboard input */
337 style |= WS_POPUP | WS_SYSMENU;
339 TRACE("Old style was %08x,%08x, setting to %08x,%08x\n",
340 This->style, This->exStyle, style, exStyle);
342 SetWindowLongW(window, GWL_STYLE, style);
343 SetWindowLongW(window, GWL_EXSTYLE, exStyle);
345 /* Inform the window about the update.
346 * TODO: Should I move it to 0/0 too?
348 SetWindowPos(window, 0 /* InsertAfter, ignored */,
349 0, 0, 0, 0, /* Pos, Size, ignored */
350 SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER);
353 /*****************************************************************************
354 * IDirectDrawImpl_RestoreWindow
356 * Helper function that restores a windows' properties when taking it out
357 * of fullscreen mode
359 * Params:
360 * This: Pointer to the DirectDraw implementation
361 * HWND: Window to setup
363 *****************************************************************************/
364 static void
365 IDirectDrawImpl_RestoreWindow(IDirectDrawImpl *This,
366 HWND window)
368 if( (This->style == 0) && (This->exStyle == 0) )
370 /* This could be a DDSCL_NORMAL -> DDSCL_NORMAL
371 * switch, do nothing
373 return;
375 TRACE("(%p): Restoring window settings of window %p to %08x, %08x\n",
376 This, window, This->style, This->exStyle);
378 SetWindowLongW(window, GWL_STYLE, This->style);
379 SetWindowLongW(window, GWL_EXSTYLE, This->exStyle);
381 /* Delete the old values */
382 This->style = 0;
383 This->exStyle = 0;
385 /* Inform the window about the update */
386 SetWindowPos(window, 0 /* InsertAfter, ignored */,
387 0, 0, 0, 0, /* Pos, Size, ignored */
388 SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
391 /*****************************************************************************
392 * IDirectDraw7::SetCooperativeLevel
394 * Sets the cooperative level for the DirectDraw object, and the window
395 * assigned to it. The cooperative level determines the general behavior
396 * of the DirectDraw application
398 * Warning: This is quite tricky, as it's not really documented which
399 * cooperative levels can be combined with each other. If a game fails
400 * after this function, try to check the cooperative levels passed on
401 * Windows, and if it returns something different.
403 * If you think that this function caused the failure because it writes a
404 * fixme, be sure to run again with a +ddraw trace.
406 * What is known about cooperative levels (See the ddraw modes test):
407 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
408 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
409 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
410 * DDSCL_FULLSCREEN can be activated
411 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
413 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
414 * DDSCL_SETFOCUSWINDOW (partially)
416 * Unhandled flags, which should be implemented
417 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
418 * expect any difference to a normal window for wine)
419 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
420 * rendering (Possible test case: Half-life)
422 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
424 * These seem not really imporant for wine
425 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX,
426 * DDSCL_MULTITHREDED
428 * Returns:
429 * DD_OK if the cooperative level was set successfully
430 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
431 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
432 * (Probably others too, have to investigate)
434 *****************************************************************************/
435 static HRESULT WINAPI
436 IDirectDrawImpl_SetCooperativeLevel(IDirectDraw7 *iface,
437 HWND hwnd,
438 DWORD cooplevel)
440 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
441 HWND window;
442 HRESULT hr;
444 FIXME("(%p)->(%p,%08x)\n",This,hwnd,cooplevel);
445 DDRAW_dump_cooperativelevel(cooplevel);
447 /* Get the old window */
448 hr = IWineD3DDevice_GetHWND(This->wineD3DDevice, &window);
449 if(hr != D3D_OK)
451 ERR("IWineD3DDevice::GetHWND failed, hr = %08x\n", hr);
452 return hr;
455 /* Tests suggest that we need one of them: */
456 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
457 DDSCL_NORMAL |
458 DDSCL_EXCLUSIVE )))
460 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
461 return DDERR_INVALIDPARAMS;
464 /* Handle those levels first which set various hwnds */
465 if(cooplevel & DDSCL_SETFOCUSWINDOW)
467 /* This isn't compatible with a lot of flags */
468 if(cooplevel & ( DDSCL_MULTITHREADED |
469 DDSCL_FPUSETUP |
470 DDSCL_FPUPRESERVE |
471 DDSCL_ALLOWREBOOT |
472 DDSCL_ALLOWMODEX |
473 DDSCL_SETDEVICEWINDOW |
474 DDSCL_NORMAL |
475 DDSCL_EXCLUSIVE |
476 DDSCL_FULLSCREEN ) )
478 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
479 return DDERR_INVALIDPARAMS;
481 else if( (This->cooperative_level & DDSCL_FULLSCREEN) && window)
483 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET\n");
484 return DDERR_HWNDALREADYSET;
487 This->focuswindow = hwnd;
488 /* Won't use the hwnd param for anything else */
489 hwnd = NULL;
491 /* Use the focus window for drawing too */
492 IWineD3DDevice_SetHWND(This->wineD3DDevice, This->focuswindow);
494 /* Destroy the device window, if we have one */
495 if(This->devicewindow)
497 DestroyWindow(This->devicewindow);
498 This->devicewindow = NULL;
501 /* DDSCL_NORMAL or DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE */
502 if(cooplevel & DDSCL_NORMAL)
504 /* Can't coexist with fullscreen or exclusive */
505 if(cooplevel & (DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE) )
507 TRACE("(%p) DDSCL_NORMAL is not compative with DDSCL_FULLSCREEN or DDSCL_EXCLUSIVE\n", This);
508 return DDERR_INVALIDPARAMS;
511 /* Switching from fullscreen? */
512 if(This->cooperative_level & DDSCL_FULLSCREEN)
514 /* Restore the display mode */
515 IDirectDraw7_RestoreDisplayMode(iface);
517 if(window)
518 IDirectDrawImpl_RestoreWindow(This, window);
520 This->cooperative_level &= ~DDSCL_FULLSCREEN;
521 This->cooperative_level &= ~DDSCL_EXCLUSIVE;
522 This->cooperative_level &= ~DDSCL_ALLOWMODEX;
525 /* Don't override focus windows or private device windows */
526 if( hwnd &&
527 !(This->focuswindow) &&
528 !(This->devicewindow) &&
529 (hwnd != window) )
531 IWineD3DDevice_SetHWND(This->wineD3DDevice, hwnd);
534 IWineD3DDevice_SetFullscreen(This->wineD3DDevice,
535 FALSE);
537 else if(cooplevel & DDSCL_FULLSCREEN)
539 /* Needs DDSCL_EXCLUSIVE */
540 if(!(cooplevel & DDSCL_EXCLUSIVE) )
542 TRACE("(%p) DDSCL_FULLSCREEN needs DDSCL_EXCLUSIVE\n", This);
543 return DDERR_INVALIDPARAMS;
545 /* Need a HWND
546 if(hwnd == 0)
548 TRACE("(%p) DDSCL_FULLSCREEN needs a HWND\n", This);
549 return DDERR_INVALIDPARAMS;
553 /* Switch from normal to full screen mode? */
554 if(This->cooperative_level & DDSCL_NORMAL)
556 This->cooperative_level &= ~DDSCL_NORMAL;
557 IWineD3DDevice_SetFullscreen(This->wineD3DDevice,
558 TRUE);
561 /* Don't override focus windows or private device windows */
562 if( hwnd &&
563 !(This->focuswindow) &&
564 !(This->devicewindow) &&
565 (hwnd != window) )
567 /* On a window change, restore the old window and set the new one */
568 if(window != hwnd)
570 if(window)
571 IDirectDrawImpl_RestoreWindow(This, window);
572 IDirectDrawImpl_SetupFullscreenWindow(This, hwnd);
574 IWineD3DDevice_SetHWND(This->wineD3DDevice, hwnd);
577 else if(cooplevel & DDSCL_EXCLUSIVE)
579 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN\n", This);
580 return DDERR_INVALIDPARAMS;
583 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
585 /* Don't create a device window if a focus window is set */
586 if( !(This->focuswindow) )
588 HWND devicewindow = CreateWindowExA(0, This->classname, "DDraw device window",
589 WS_POPUP, 0, 0,
590 GetSystemMetrics(SM_CXSCREEN),
591 GetSystemMetrics(SM_CYSCREEN),
592 NULL, NULL, GetModuleHandleA(0), NULL);
594 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
595 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
597 IWineD3DDevice_SetHWND(This->wineD3DDevice, devicewindow);
598 This->devicewindow = devicewindow;
602 /* Unhandled flags */
603 if(cooplevel & DDSCL_ALLOWREBOOT)
604 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
605 if(cooplevel & DDSCL_ALLOWMODEX)
606 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
607 if(cooplevel & DDSCL_MULTITHREADED)
608 FIXME("(%p) Unhandled flag DDSCL_MULTITHREADED, Uh Oh...\n", This);
609 if(cooplevel & DDSCL_FPUSETUP)
610 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
611 if(cooplevel & DDSCL_FPUPRESERVE)
612 WARN("(%p) Unhandled flag DDSCL_FPUPRESERVE, harmless\n", This);
614 /* Store the cooperative_level */
615 This->cooperative_level |= cooplevel;
616 TRACE("SetCooperativeLevel retuning DD_OK\n");
617 return DD_OK;
620 /*****************************************************************************
621 * IDirectDraw7::SetDisplayMode
623 * Sets the display screen resolution, color depth and refresh frequency
624 * when in fullscreen mode (in theory).
625 * Possible return values listed in the SDK suggest that this method fails
626 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
627 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
628 * It seems to be valid to pass 0 for With and Height, this has to be tested
629 * It could mean that the current video mode should be left as-is. (But why
630 * call it then?)
632 * Params:
633 * Height, Width: Screen dimension
634 * BPP: Color depth in Bits per pixel
635 * Refreshrate: Screen refresh rate
636 * Flags: Other stuff
638 * Returns
639 * DD_OK on success
641 *****************************************************************************/
642 static HRESULT WINAPI
643 IDirectDrawImpl_SetDisplayMode(IDirectDraw7 *iface,
644 DWORD Width,
645 DWORD Height,
646 DWORD BPP,
647 DWORD RefreshRate,
648 DWORD Flags)
650 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
651 WINED3DDISPLAYMODE Mode;
652 TRACE("(%p)->(%d,%d,%d,%d,%x: Relay!\n", This, Width, Height, BPP, RefreshRate, Flags);
654 if( !Width || !Height )
656 ERR("Width=%d, Height=%d, what to do?\n", Width, Height);
657 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
658 return DD_OK;
661 /* Check the exclusive mode
662 if(!(This->cooperative_level & DDSCL_EXCLUSIVE))
663 return DDERR_NOEXCLUSIVEMODE;
664 * This is WRONG. Don't know if the SDK is completely
665 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
666 * is returned, but Half-Life 1.1.1.1 (Steam version)
667 * depends on this
670 Mode.Width = Width;
671 Mode.Height = Height;
672 Mode.RefreshRate = RefreshRate;
673 switch(BPP)
675 case 8: Mode.Format = WINED3DFMT_P8; break;
676 case 15: Mode.Format = WINED3DFMT_X1R5G5B5; break;
677 case 16: Mode.Format = WINED3DFMT_R5G6B5; break;
678 case 24: Mode.Format = WINED3DFMT_R8G8B8; break;
679 case 32: Mode.Format = WINED3DFMT_A8R8G8B8; break;
682 /* TODO: The possible return values from msdn suggest that
683 * the screen mode can't be changed if a surface is locked
684 * or some drawing is in progress
687 /* TODO: Lose the primary surface */
688 return IWineD3DDevice_SetDisplayMode(This->wineD3DDevice,
689 0, /* First swapchain */
690 &Mode);
694 /*****************************************************************************
695 * IDirectDraw7::RestoreDisplayMode
697 * Restores the display mode to what it was at creation time. Basically.
699 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
700 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
701 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
702 * -> DD_1 is released. The screen should be left at 1024x768x32.
703 * -> DD_2 is released. The screen should be set to 1400x1050x32
704 * This case is unhandled right now, but Empire Earth does it this way.
705 * (But perhaps there is something in SetCooperativeLevel to prevent this)
707 * The msdn says that this method resets the display mode to what it was before
708 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
710 * Returns
711 * DD_OK on success
712 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
714 *****************************************************************************/
715 static HRESULT WINAPI
716 IDirectDrawImpl_RestoreDisplayMode(IDirectDraw7 *iface)
718 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
719 TRACE("(%p)\n", This);
721 return IDirectDraw7_SetDisplayMode(ICOM_INTERFACE(This, IDirectDraw7),
722 This->orig_width,
723 This->orig_height,
724 This->orig_bpp,
729 /*****************************************************************************
730 * IDirectDraw7::GetCaps
732 * Returns the drives capabilities
734 * Used for version 1, 2, 4 and 7
736 * Params:
737 * DriverCaps: Structure to write the Hardware accelerated caps to
738 * HelCaps: Structure to write the emulation caps to
740 * Returns
741 * This implementation returns DD_OK only
743 *****************************************************************************/
744 static HRESULT WINAPI
745 IDirectDrawImpl_GetCaps(IDirectDraw7 *iface,
746 DDCAPS *DriverCaps,
747 DDCAPS *HELCaps)
749 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
750 TRACE("(%p)->(%p,%p)\n", This, DriverCaps, HELCaps);
752 /* One structure must be != NULL */
753 if( (!DriverCaps) && (!HELCaps) )
755 ERR("(%p) Invalid params to IDirectDrawImpl_GetCaps\n", This);
756 return DDERR_INVALIDPARAMS;
759 if(DriverCaps)
761 DD_STRUCT_COPY_BYSIZE(DriverCaps, &This->caps);
762 if (TRACE_ON(ddraw))
764 TRACE("Driver Caps :\n");
765 DDRAW_dump_DDCAPS(DriverCaps);
769 if(HELCaps)
771 DD_STRUCT_COPY_BYSIZE(HELCaps, &This->caps);
772 if (TRACE_ON(ddraw))
774 TRACE("HEL Caps :\n");
775 DDRAW_dump_DDCAPS(HELCaps);
779 return DD_OK;
782 /*****************************************************************************
783 * IDirectDraw7::Compact
785 * No idea what it does, MSDN says it's not implemented.
787 * Returns
788 * DD_OK, but this is unchecked
790 *****************************************************************************/
791 static HRESULT WINAPI
792 IDirectDrawImpl_Compact(IDirectDraw7 *iface)
794 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
795 TRACE("(%p)\n", This);
797 return DD_OK;
800 /*****************************************************************************
801 * IDirectDraw7::GetDisplayMode
803 * Returns information about the current display mode
805 * Exists in Version 1, 2, 4 and 7
807 * Params:
808 * DDSD: Address of a surface description structure to write the info to
810 * Returns
811 * DD_OK
813 *****************************************************************************/
814 static HRESULT WINAPI
815 IDirectDrawImpl_GetDisplayMode(IDirectDraw7 *iface,
816 DDSURFACEDESC2 *DDSD)
818 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
819 HRESULT hr;
820 WINED3DDISPLAYMODE Mode;
821 DWORD Size;
822 TRACE("(%p)->(%p): Relay\n", This, DDSD);
824 /* This seems sane */
825 if(!DDSD)
827 return DDERR_INVALIDPARAMS;
830 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
831 * so one method can be used for all versions (Hopefully)
833 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
834 0 /* swapchain 0 */,
835 &Mode);
836 if( hr != D3D_OK )
838 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
839 return hr;
842 Size = DDSD->dwSize;
843 memset(DDSD, 0, Size);
845 DDSD->dwSize = Size;
846 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
847 DDSD->dwWidth = Mode.Width;
848 DDSD->dwHeight = Mode.Height;
849 DDSD->u2.dwRefreshRate = 60;
850 DDSD->ddsCaps.dwCaps = 0;
851 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
852 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, Mode.Format);
853 DDSD->u1.lPitch = Mode.Width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
855 if(TRACE_ON(ddraw))
857 TRACE("Returning surface desc :\n");
858 DDRAW_dump_surface_desc(DDSD);
861 return DD_OK;
864 /*****************************************************************************
865 * IDirectDraw7::GetFourCCCodes
867 * Returns an array of supported FourCC codes.
869 * Exists in Version 1, 2, 4 and 7
871 * Params:
872 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
873 * of enumerated codes
874 * Codes: Pointer to an array of DWORDs where the supported codes are written
875 * to
877 * Returns
878 * Always returns DD_OK, as it's a stub for now
880 *****************************************************************************/
881 static HRESULT WINAPI
882 IDirectDrawImpl_GetFourCCCodes(IDirectDraw7 *iface,
883 DWORD *NumCodes, DWORD *Codes)
885 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
886 FIXME("(%p)->(%p, %p): Stub!\n", This, NumCodes, Codes);
888 if(NumCodes) *NumCodes = 0;
890 return DD_OK;
893 /*****************************************************************************
894 * IDirectDraw7::GetMonitorFrequency
896 * Returns the monitor's frequency
898 * Exists in Version 1, 2, 4 and 7
900 * Params:
901 * Freq: Pointer to a DWORD to write the frequency to
903 * Returns
904 * Always returns DD_OK
906 *****************************************************************************/
907 static HRESULT WINAPI
908 IDirectDrawImpl_GetMonitorFrequency(IDirectDraw7 *iface,
909 DWORD *Freq)
911 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
912 TRACE("(%p)->(%p)\n", This, Freq);
914 /* Ideally this should be in WineD3D, as it concerns the screen setup,
915 * but for now this should make the games happy
917 *Freq = 60;
918 return DD_OK;
921 /*****************************************************************************
922 * IDirectDraw7::GetVerticalBlankStatus
924 * Returns the Vertical blank status of the monitor. This should be in WineD3D
925 * too basically, but as it's a semi stub, I didn't create a function there
927 * Params:
928 * status: Pointer to a BOOL to be filled with the vertical blank status
930 * Returns
931 * DD_OK on success
932 * DDERR_INVALIDPARAMS if status is NULL
934 *****************************************************************************/
935 static HRESULT WINAPI
936 IDirectDrawImpl_GetVerticalBlankStatus(IDirectDraw7 *iface,
937 BOOL *status)
939 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
940 TRACE("(%p)->(%p)\n", This, status);
942 /* This looks sane, the MSDN suggests it too */
943 if(!status) return DDERR_INVALIDPARAMS;
945 *status = This->fake_vblank;
946 This->fake_vblank = !This->fake_vblank;
947 return DD_OK;
950 /*****************************************************************************
951 * IDirectDraw7::GetAvailableVidMem
953 * Returns the total and free video memory
955 * Params:
956 * Caps: Specifies the memory type asked for
957 * total: Pointer to a DWORD to be filled with the total memory
958 * free: Pointer to a DWORD to be filled with the free memory
960 * Returns
961 * DD_OK on success
962 * DDERR_INVALIDPARAMS of free and total are NULL
964 *****************************************************************************/
965 static HRESULT WINAPI
966 IDirectDrawImpl_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total, DWORD *free)
968 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
969 TRACE("(%p)->(%p, %p, %p)\n", This, Caps, total, free);
971 if(TRACE_ON(ddraw))
973 TRACE("(%p) Asked for memory with description: ", This);
974 DDRAW_dump_DDSCAPS2(Caps);
975 TRACE("\n");
978 /* Todo: System memory vs local video memory vs non-local video memory
979 * The MSDN also mentions differences between texture memory and other
980 * resources, but that's not important
983 if( (!total) && (!free) ) return DDERR_INVALIDPARAMS;
985 if(total) *total = This->total_vidmem;
986 if(free) *free = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
988 return DD_OK;
991 /*****************************************************************************
992 * IDirectDraw7::Initialize
994 * Initializes a DirectDraw interface.
996 * Params:
997 * GUID: Interface identifier. Well, don't know what this is really good
998 * for
1000 * Returns
1001 * Returns DD_OK on the first call,
1002 * DDERR_ALREADYINITIALIZED on repeated calls
1004 *****************************************************************************/
1005 static HRESULT WINAPI
1006 IDirectDrawImpl_Initialize(IDirectDraw7 *iface,
1007 GUID *Guid)
1009 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1010 TRACE("(%p)->(%s): No-op\n", This, debugstr_guid(Guid));
1012 if(This->initialized)
1014 return DDERR_ALREADYINITIALIZED;
1016 else
1018 return DD_OK;
1022 /*****************************************************************************
1023 * IDirectDraw7::FlipToGDISurface
1025 * "Makes the surface that the GDI writes to the primary surface"
1026 * Looks like some windows specific thing we don't have to care about.
1027 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1028 * show error boxes ;)
1029 * Well, just return DD_OK.
1031 * Returns:
1032 * Always returns DD_OK
1034 *****************************************************************************/
1035 static HRESULT WINAPI
1036 IDirectDrawImpl_FlipToGDISurface(IDirectDraw7 *iface)
1038 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1039 TRACE("(%p)\n", This);
1041 return DD_OK;
1044 /*****************************************************************************
1045 * IDirectDraw7::WaitForVerticalBlank
1047 * This method allows applications to get in sync with the vertical blank
1048 * interval.
1049 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1050 * redraw the screen, most likely because of this stub
1052 * Parameters:
1053 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1054 * or DDWAITVB_BLOCKEND
1055 * h: Not used, according to MSDN
1057 * Returns:
1058 * Always returns DD_OK
1060 *****************************************************************************/
1061 static HRESULT WINAPI
1062 IDirectDrawImpl_WaitForVerticalBlank(IDirectDraw7 *iface,
1063 DWORD Flags,
1064 HANDLE h)
1066 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1067 FIXME("(%p)->(%x,%p): Stub\n", This, Flags, h);
1069 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1070 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1071 return DDERR_UNSUPPORTED; /* unchecked */
1073 return DD_OK;
1076 /*****************************************************************************
1077 * IDirectDraw7::GetScanLine
1079 * Returns the scan line that is being drawn on the monitor
1081 * Parameters:
1082 * Scanline: Address to write the scan line value to
1084 * Returns:
1085 * Always returns DD_OK
1087 *****************************************************************************/
1088 static HRESULT WINAPI IDirectDrawImpl_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1090 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1091 static BOOL hide = FALSE;
1092 WINED3DDISPLAYMODE Mode;
1094 /* This function is called often, so print the fixme only once */
1095 if(!hide)
1097 FIXME("(%p)->(%p): Semi-Stub\n", This, Scanline);
1098 hide = TRUE;
1101 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1103 &Mode);
1105 /* Fake the line sweeping of the monitor */
1106 /* FIXME: We should synchronize with a source to keep the refresh rate */
1107 *Scanline = This->cur_scanline++;
1108 /* Assume 20 scan lines in the vertical blank */
1109 if (This->cur_scanline >= Mode.Height + 20)
1110 This->cur_scanline = 0;
1112 return DD_OK;
1115 /*****************************************************************************
1116 * IDirectDraw7::TestCooperativeLevel
1118 * Informs the application about the state of the video adapter, depending
1119 * on the cooperative level
1121 * Returns:
1122 * DD_OK if the device is in a sane state
1123 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1124 * if the state is not correct(See below)
1126 *****************************************************************************/
1127 static HRESULT WINAPI
1128 IDirectDrawImpl_TestCooperativeLevel(IDirectDraw7 *iface)
1130 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1131 HRESULT hr;
1132 TRACE("(%p)\n", This);
1134 /* Description from MSDN:
1135 * For fullscreen apps return DDERR_NOEXCLUSIVEMODE if the user switched
1136 * away from the app with e.g. alt-tab. Windowed apps receive
1137 * DDERR_EXCLUSIVEMODEALREADYSET if another application created a
1138 * DirectDraw object in exclusive mode. DDERR_WRONGMODE is returned,
1139 * when the video mode has changed
1142 hr = IWineD3DDevice_TestCooperativeLevel(This->wineD3DDevice);
1144 /* Fix the result value. These values are mapped from their
1145 * d3d9 counterpart.
1147 switch(hr)
1149 case WINED3DERR_DEVICELOST:
1150 if(This->cooperative_level & DDSCL_EXCLUSIVE)
1152 return DDERR_NOEXCLUSIVEMODE;
1154 else
1156 return DDERR_EXCLUSIVEMODEALREADYSET;
1159 case WINED3DERR_DEVICENOTRESET:
1160 return DD_OK;
1162 case WINED3D_OK:
1163 return DD_OK;
1165 case WINED3DERR_DRIVERINTERNALERROR:
1166 default:
1167 ERR("(%p) Unexpected return value %08x from wineD3D, " \
1168 " returning DD_OK\n", This, hr);
1171 return DD_OK;
1174 /*****************************************************************************
1175 * IDirectDraw7::GetGDISurface
1177 * Returns the surface that GDI is treating as the primary surface.
1178 * For Wine this is the front buffer
1180 * Params:
1181 * GDISurface: Address to write the surface pointer to
1183 * Returns:
1184 * DD_OK if the surface was found
1185 * DDERR_NOTFOUND if the GDI surface wasn't found
1187 *****************************************************************************/
1188 static HRESULT WINAPI
1189 IDirectDrawImpl_GetGDISurface(IDirectDraw7 *iface,
1190 IDirectDrawSurface7 **GDISurface)
1192 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1193 IWineD3DSurface *Surf;
1194 IDirectDrawSurface7 *ddsurf;
1195 HRESULT hr;
1196 DDSCAPS2 ddsCaps;
1197 TRACE("(%p)->(%p)\n", This, GDISurface);
1199 /* Get the back buffer from the wineD3DDevice and search its
1200 * attached surfaces for the front buffer
1202 hr = IWineD3DDevice_GetBackBuffer(This->wineD3DDevice,
1203 0, /* SwapChain */
1204 0, /* first back buffer*/
1205 WINED3DBACKBUFFER_TYPE_MONO,
1206 &Surf);
1208 if( (hr != D3D_OK) ||
1209 (!Surf) )
1211 ERR("IWineD3DDevice::GetBackBuffer failed\n");
1212 return DDERR_NOTFOUND;
1215 IWineD3DSurface_GetParent(Surf,
1216 (IUnknown **) &ddsurf);
1217 IDirectDrawSurface7_Release(ddsurf); /* For the GetParent */
1219 /* Find the front buffer */
1220 ddsCaps.dwCaps = DDSCAPS_FRONTBUFFER;
1221 hr = IDirectDrawSurface7_GetAttachedSurface(ddsurf,
1222 &ddsCaps,
1223 GDISurface);
1224 if(hr != DD_OK)
1226 ERR("IDirectDrawSurface7::GetAttachedSurface failed, hr = %x\n", hr);
1229 /* The AddRef is OK this time */
1230 return hr;
1233 /*****************************************************************************
1234 * IDirectDrawImpl_EnumDisplayModesCB
1236 * Callback function for IDirectDraw7::EnumDisplayModes. Translates
1237 * the wineD3D values to ddraw values and calls the application callback
1239 * Params:
1240 * device: The IDirectDraw7 interface to the current device
1241 * With, Height, Pixelformat, Refresh: Enumerated display mode
1242 * context: the context pointer passed to IWineD3DDevice::EnumDisplayModes
1244 * Returns:
1245 * The return value from the application callback
1247 *****************************************************************************/
1248 static HRESULT WINAPI
1249 IDirectDrawImpl_EnumDisplayModesCB(IUnknown *pDevice,
1250 UINT Width,
1251 UINT Height,
1252 WINED3DFORMAT Pixelformat,
1253 FLOAT Refresh,
1254 void *context)
1256 DDSURFACEDESC2 callback_sd;
1257 EnumDisplayModesCBS *cbs = (EnumDisplayModesCBS *) context;
1259 memset(&callback_sd, 0, sizeof(callback_sd));
1260 callback_sd.dwSize = sizeof(callback_sd);
1261 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1263 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH;
1264 if(Refresh > 0.0)
1266 callback_sd.dwFlags |= DDSD_REFRESHRATE;
1267 callback_sd.u2.dwRefreshRate = 60.0;
1270 callback_sd.dwHeight = Height;
1271 callback_sd.dwWidth = Width;
1273 PixelFormat_WineD3DtoDD(&callback_sd.u4.ddpfPixelFormat, Pixelformat);
1274 return cbs->callback(&callback_sd, cbs->context);
1277 /*****************************************************************************
1278 * IDirectDraw7::EnumDisplayModes
1280 * Enumerates the supported Display modes. The modes can be filtered with
1281 * the DDSD parameter.
1283 * Params:
1284 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES
1285 * DDSD: Surface description to filter the modes
1286 * Context: Pointer passed back to the callback function
1287 * cb: Application-provided callback function
1289 * Returns:
1290 * DD_OK on success
1291 * DDERR_INVALIDPARAMS if the callback wasn't set
1293 *****************************************************************************/
1294 static HRESULT WINAPI
1295 IDirectDrawImpl_EnumDisplayModes(IDirectDraw7 *iface,
1296 DWORD Flags,
1297 DDSURFACEDESC2 *DDSD,
1298 void *Context,
1299 LPDDENUMMODESCALLBACK2 cb)
1301 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1302 UINT Width = 0, Height = 0;
1303 WINED3DFORMAT pixelformat = WINED3DFMT_UNKNOWN;
1304 EnumDisplayModesCBS cbs;
1306 TRACE("(%p)->(%p,%p,%p): Relay\n", This, DDSD, Context, cb);
1308 /* This looks sane */
1309 if(!cb) return DDERR_INVALIDPARAMS;
1311 /* The private callback structure */
1312 cbs.callback = cb;
1313 cbs.context = Context;
1315 if(DDSD)
1317 if (DDSD->dwFlags & DDSD_WIDTH)
1318 Width = DDSD->dwWidth;
1319 if (DDSD->dwFlags & DDSD_HEIGHT)
1320 Height = DDSD->dwHeight;
1321 if ((DDSD->dwFlags & DDSD_PIXELFORMAT) && (DDSD->u4.ddpfPixelFormat.dwFlags & DDPF_RGB) )
1322 pixelformat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
1325 return IWineD3DDevice_EnumDisplayModes(This->wineD3DDevice,
1326 Flags,
1327 Width, Height, pixelformat,
1328 &cbs,
1329 IDirectDrawImpl_EnumDisplayModesCB);
1332 /*****************************************************************************
1333 * IDirectDraw7::EvaluateMode
1335 * Used with IDirectDraw7::StartModeTest to test video modes.
1336 * EvaluateMode is used to pass or fail a mode, and continue with the next
1337 * mode
1339 * Params:
1340 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
1341 * Timeout: Returns the amount of seconds left before the mode would have
1342 * been failed automatically
1344 * Returns:
1345 * This implementation always DD_OK, because it's a stub
1347 *****************************************************************************/
1348 static HRESULT WINAPI
1349 IDirectDrawImpl_EvaluateMode(IDirectDraw7 *iface,
1350 DWORD Flags,
1351 DWORD *Timeout)
1353 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1354 FIXME("(%p)->(%d,%p): Stub!\n", This, Flags, Timeout);
1356 /* When implementing this, implement it in WineD3D */
1358 return DD_OK;
1361 /*****************************************************************************
1362 * IDirectDraw7::GetDeviceIdentifier
1364 * Returns the device identifier, which gives information about the driver
1365 * Our device identifier is defined at the beginning of this file.
1367 * Params:
1368 * DDDI: Address for the returned structure
1369 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
1371 * Returns:
1372 * On success it returns DD_OK
1373 * DDERR_INVALIDPARAMS if DDDI is NULL
1375 *****************************************************************************/
1376 static HRESULT WINAPI
1377 IDirectDrawImpl_GetDeviceIdentifier(IDirectDraw7 *iface,
1378 DDDEVICEIDENTIFIER2 *DDDI,
1379 DWORD Flags)
1381 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1382 TRACE("(%p)->(%p,%08x)\n", This, DDDI, Flags);
1384 if(!DDDI)
1385 return DDERR_INVALIDPARAMS;
1387 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
1388 * host adapter, if there's a secondary 3D adapter. This doesn't apply
1389 * to any modern hardware, nor is it interesting for Wine, so ignore it
1392 *DDDI = deviceidentifier;
1393 return DD_OK;
1396 /*****************************************************************************
1397 * IDirectDraw7::GetSurfaceFromDC
1399 * Returns the Surface for a GDI device context handle.
1400 * Is this related to IDirectDrawSurface::GetDC ???
1402 * Params:
1403 * hdc: hdc to return the surface for
1404 * Surface: Address to write the surface pointer to
1406 * Returns:
1407 * Always returns DD_OK because it's a stub
1409 *****************************************************************************/
1410 static HRESULT WINAPI
1411 IDirectDrawImpl_GetSurfaceFromDC(IDirectDraw7 *iface,
1412 HDC hdc,
1413 IDirectDrawSurface7 **Surface)
1415 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1416 FIXME("(%p)->(%p,%p): Stub!\n", This, hdc, Surface);
1418 /* Implementation idea if needed: Loop through all surfaces and compare
1419 * their hdc with hdc. Implement it in WineD3D! */
1420 return DDERR_NOTFOUND;
1423 /*****************************************************************************
1424 * IDirectDraw7::RestoreAllSurfaces
1426 * Calls the restore method of all surfaces
1428 * Params:
1430 * Returns:
1431 * Always returns DD_OK because it's a stub
1433 *****************************************************************************/
1434 static HRESULT WINAPI
1435 IDirectDrawImpl_RestoreAllSurfaces(IDirectDraw7 *iface)
1437 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1438 FIXME("(%p): Stub\n", This);
1440 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
1441 * get their parent and call their restore method. Do not implement
1442 * it in WineD3D, as restoring a surface means re-creating the
1443 * WineD3DDSurface
1445 return DD_OK;
1448 /*****************************************************************************
1449 * IDirectDraw7::StartModeTest
1451 * Tests the specified video modes to update the system registry with
1452 * refresh rate information. StartModeTest starts the mode test,
1453 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
1454 * isn't called within 15 seconds, the mode is failed automatically
1456 * As refresh rates are handled by the X server, I don't think this
1457 * Method is important
1459 * Params:
1460 * Modes: An array of mode specifications
1461 * NumModes: The number of modes in Modes
1462 * Flags: Some flags...
1464 * Returns:
1465 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
1466 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
1467 * otherwise DD_OK
1469 *****************************************************************************/
1470 static HRESULT WINAPI
1471 IDirectDrawImpl_StartModeTest(IDirectDraw7 *iface,
1472 SIZE *Modes,
1473 DWORD NumModes,
1474 DWORD Flags)
1476 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1477 WARN("(%p)->(%p, %d, %x): Semi-Stub, most likely harmless\n", This, Modes, NumModes, Flags);
1479 /* This looks sane */
1480 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
1482 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
1483 * As it is not, DDERR_TESTFINISHED is returned
1484 * (hopefully that's correct
1486 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
1487 * well, that value doesn't (yet) exist in the wine headers, so ignore it
1490 return DD_OK;
1493 /*****************************************************************************
1494 * IDirectDrawImpl_RecreateSurfacesCallback
1496 * Enumeration callback for IDirectDrawImpl_RecreateAllSurfaces.
1497 * It re-recreates the WineD3DSurface. It's pretty straightforward
1499 *****************************************************************************/
1500 HRESULT WINAPI
1501 IDirectDrawImpl_RecreateSurfacesCallback(IDirectDrawSurface7 *surf,
1502 DDSURFACEDESC2 *desc,
1503 void *Context)
1505 IDirectDrawSurfaceImpl *surfImpl = ICOM_OBJECT(IDirectDrawSurfaceImpl,
1506 IDirectDrawSurface7,
1507 surf);
1508 IDirectDrawImpl *This = surfImpl->ddraw;
1509 IUnknown *Parent;
1510 IParentImpl *parImpl = NULL;
1511 IWineD3DSurface *wineD3DSurface;
1512 HRESULT hr;
1513 void *tmp;
1515 WINED3DSURFACE_DESC Desc;
1516 WINED3DFORMAT Format;
1517 WINED3DRESOURCETYPE Type;
1518 DWORD Usage;
1519 WINED3DPOOL Pool;
1520 UINT Size;
1522 WINED3DMULTISAMPLE_TYPE MultiSampleType;
1523 DWORD MultiSampleQuality;
1524 UINT Width;
1525 UINT Height;
1527 TRACE("(%p): Enumerated Surface %p\n", This, surfImpl);
1529 /* For the enumeration */
1530 IDirectDrawSurface7_Release(surf);
1532 if(surfImpl->ImplType == This->ImplType) return DDENUMRET_OK; /* Continue */
1534 /* Get the objects */
1535 wineD3DSurface = surfImpl->WineD3DSurface;
1536 IWineD3DSurface_GetParent(wineD3DSurface, &Parent);
1537 IUnknown_Release(Parent); /* For the getParent */
1539 /* Is the parent an IParent interface? */
1540 if(IUnknown_QueryInterface(Parent, &IID_IParent, &tmp) == S_OK)
1542 /* It is a IParent interface! */
1543 IUnknown_Release(Parent); /* For the QueryInterface */
1544 parImpl = ICOM_OBJECT(IParentImpl, IParent, Parent);
1545 /* Release the reference the parent interface is holding */
1546 IWineD3DSurface_Release(wineD3DSurface);
1550 /* Get the surface properties */
1551 Desc.Format = &Format;
1552 Desc.Type = &Type;
1553 Desc.Usage = &Usage;
1554 Desc.Pool = &Pool;
1555 Desc.Size = &Size;
1556 Desc.MultiSampleType = &MultiSampleType;
1557 Desc.MultiSampleQuality = &MultiSampleQuality;
1558 Desc.Width = &Width;
1559 Desc.Height = &Height;
1561 hr = IWineD3DSurface_GetDesc(wineD3DSurface, &Desc);
1562 if(hr != D3D_OK) return hr;
1564 /* Create the new surface */
1565 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice,
1566 Width, Height, Format,
1567 TRUE /* Lockable */,
1568 FALSE /* Discard */,
1569 surfImpl->mipmap_level,
1570 &surfImpl->WineD3DSurface,
1571 Type,
1572 Usage,
1573 Pool,
1574 MultiSampleType,
1575 MultiSampleQuality,
1576 0 /* SharedHandle */,
1577 This->ImplType,
1578 Parent);
1580 if(hr != D3D_OK)
1581 return hr;
1583 /* Update the IParent if it exists */
1584 if(parImpl)
1586 parImpl->child = (IUnknown *) surfImpl->WineD3DSurface;
1587 /* Add a reference for the IParent */
1588 IWineD3DSurface_AddRef(surfImpl->WineD3DSurface);
1590 /* TODO: Copy the surface content, except for render targets */
1592 if(IWineD3DSurface_Release(wineD3DSurface) == 0)
1593 TRACE("Surface released successful, next surface\n");
1594 else
1595 ERR("Something's still holding the old WineD3DSurface\n");
1597 surfImpl->ImplType = This->ImplType;
1599 return DDENUMRET_OK;
1602 /*****************************************************************************
1603 * IDirectDrawImpl_RecreateAllSurfaces
1605 * A function, that converts all wineD3DSurfaces to the new implementation type
1606 * It enumerates all surfaces with IWineD3DDevice::EnumSurfaces, creates a
1607 * new WineD3DSurface, copies the content and releases the old surface
1609 *****************************************************************************/
1610 static HRESULT
1611 IDirectDrawImpl_RecreateAllSurfaces(IDirectDrawImpl *This)
1613 DDSURFACEDESC2 desc;
1614 TRACE("(%p): Switch to implementation %d\n", This, This->ImplType);
1616 if(This->ImplType != SURFACE_OPENGL && This->d3d_initialized)
1618 /* Should happen almost never */
1619 FIXME("(%p) Switching to non-opengl surfaces with d3d started. Is this a bug?\n", This);
1620 /* Shutdown d3d */
1621 IWineD3DDevice_Uninit3D(This->wineD3DDevice);
1623 /* Contrary: D3D starting is handled by the caller, because it knows the render target */
1625 memset(&desc, 0, sizeof(desc));
1626 desc.dwSize = sizeof(desc);
1628 return IDirectDraw7_EnumSurfaces(ICOM_INTERFACE(This, IDirectDraw7),
1630 &desc,
1631 This,
1632 IDirectDrawImpl_RecreateSurfacesCallback);
1635 /*****************************************************************************
1636 * D3D7CB_CreateSurface
1638 * Callback function for IDirect3DDevice_CreateTexture. It searches for the
1639 * correct mipmap sublevel, and returns it to WineD3D.
1640 * The surfaces are created already by IDirectDraw7::CreateSurface
1642 * Params:
1643 * With, Height: With and height of the surface
1644 * Format: The requested format
1645 * Usage, Pool: D3DUSAGE and D3DPOOL of the surface
1646 * level: The mipmap level
1647 * Surface: Pointer to pass the created surface back at
1648 * SharedHandle: NULL
1650 * Returns:
1651 * D3D_OK
1653 *****************************************************************************/
1654 static HRESULT WINAPI
1655 D3D7CB_CreateSurface(IUnknown *device,
1656 UINT Width, UINT Height,
1657 WINED3DFORMAT Format,
1658 DWORD Usage, WINED3DPOOL Pool, UINT level,
1659 IWineD3DSurface **Surface,
1660 HANDLE *SharedHandle)
1662 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
1663 IDirectDrawSurfaceImpl *surf = This->tex_root;
1664 int i;
1665 TRACE("(%p) call back. surf=%p\n", device, surf);
1667 /* Find the wanted mipmap. There are enough mipmaps in the chain */
1668 for(i = 0; i < level; i++)
1669 surf = surf->next_complex;
1671 /* Return the surface */
1672 *Surface = surf->WineD3DSurface;
1674 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *Surface, surf);
1675 return D3D_OK;
1678 /*****************************************************************************
1679 * IDirectDrawImpl_CreateNewSurface
1681 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
1682 * with the passed parameters.
1684 * Params:
1685 * DDSD: Description of the surface to create
1686 * Surf: Address to store the interface pointer at
1688 * Returns:
1689 * DD_OK on success
1691 *****************************************************************************/
1692 static HRESULT WINAPI
1693 IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This,
1694 DDSURFACEDESC2 *pDDSD,
1695 IDirectDrawSurfaceImpl **ppSurf,
1696 UINT level)
1698 HRESULT hr;
1699 UINT Width = 0, Height = 0;
1700 WINED3DFORMAT Format = WINED3DFMT_UNKNOWN;
1701 WINED3DRESOURCETYPE ResType = WINED3DRTYPE_SURFACE;
1702 DWORD Usage = 0;
1703 WINED3DSURFTYPE ImplType = This->ImplType;
1704 WINED3DSURFACE_DESC Desc;
1705 IUnknown *Parent;
1706 IParentImpl *parImpl = NULL;
1707 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
1709 /* Dummies for GetDesc */
1710 WINED3DPOOL dummy_d3dpool;
1711 WINED3DMULTISAMPLE_TYPE dummy_mst;
1712 UINT dummy_uint;
1713 DWORD dummy_dword;
1715 if (TRACE_ON(ddraw))
1717 TRACE(" (%p) Requesting surface desc :\n", This);
1718 DDRAW_dump_surface_desc(pDDSD);
1721 /* Select the surface type, if it wasn't choosen yet */
1722 if(ImplType == SURFACE_UNKNOWN)
1724 /* Use GL Surfaces if a D3DDEVICE Surface is requested */
1725 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1727 TRACE("(%p) Choosing GL surfaces because a 3DDEVICE Surface was requested\n", This);
1728 ImplType = SURFACE_OPENGL;
1731 /* Otherwise use GDI surfaces for now */
1732 else
1734 TRACE("(%p) Choosing GDI surfaces for 2D rendering\n", This);
1735 ImplType = SURFACE_GDI;
1738 /* Policy if all surface implementations are available:
1739 * First, check if a default type was set with winecfg. If not,
1740 * try Xrender surfaces, and use them if they work. Next, check if
1741 * accelerated OpenGL is available, and use GL surfaces in this
1742 * case. If all else fails, use GDI surfaces. If a 3DDEVICE surface
1743 * was created, always use OpenGL surfaces.
1745 * (Note: Xrender surfaces are not implemented for now, the
1746 * unaccelerated implementation uses GDI to render in Software)
1749 /* Store the type. If it needs to be changed, all WineD3DSurfaces have to
1750 * be re-created. This could be done with IDirectDrawSurface7::Restore
1752 This->ImplType = ImplType;
1754 else
1756 if((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE ) &&
1757 (This->ImplType != SURFACE_OPENGL ) && DefaultSurfaceType == SURFACE_UNKNOWN)
1759 /* We have to change to OpenGL,
1760 * and re-create all WineD3DSurfaces
1762 ImplType = SURFACE_OPENGL;
1763 This->ImplType = ImplType;
1764 TRACE("(%p) Re-creating all surfaces\n", This);
1765 IDirectDrawImpl_RecreateAllSurfaces(This);
1766 TRACE("(%p) Done recreating all surfaces\n", This);
1768 else if(This->ImplType != SURFACE_OPENGL)
1770 WARN("The application requests a 3D capable surface, but a non-opengl surface was set in the registry\n");
1771 /* Do not fail surface creation, only fail 3D device creation */
1775 /* Get the correct wined3d usage */
1776 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE |
1777 DDSCAPS_BACKBUFFER |
1778 DDSCAPS_3DDEVICE ) )
1780 Usage |= WINED3DUSAGE_RENDERTARGET;
1782 pDDSD->ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY |
1783 DDSCAPS_VISIBLE |
1784 DDSCAPS_LOCALVIDMEM;
1786 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
1788 Usage |= WINED3DUSAGE_OVERLAY;
1790 if(This->depthstencil)
1792 /* The depth stencil creation callback sets this flag.
1793 * Set the WineD3D usage to let it know that it's a depth
1794 * Stencil surface.
1796 Usage |= WINED3DUSAGE_DEPTHSTENCIL;
1798 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
1800 Pool = WINED3DPOOL_SYSTEMMEM;
1803 Format = PixelFormat_DD2WineD3D(&pDDSD->u4.ddpfPixelFormat);
1804 if(Format == WINED3DFMT_UNKNOWN)
1806 ERR("Unsupported / Unknown pixelformat\n");
1807 return DDERR_INVALIDPIXELFORMAT;
1810 /* Create the Surface object */
1811 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
1812 if(!*ppSurf)
1814 ERR("(%p) Error allocating memory for a surface\n", This);
1815 return DDERR_OUTOFVIDEOMEMORY;
1817 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawSurface7, IDirectDrawSurface7_Vtbl);
1818 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawSurface3, IDirectDrawSurface3_Vtbl);
1819 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawGammaControl, IDirectDrawGammaControl_Vtbl);
1820 ICOM_INIT_INTERFACE(*ppSurf, IDirect3DTexture2, IDirect3DTexture2_Vtbl);
1821 ICOM_INIT_INTERFACE(*ppSurf, IDirect3DTexture, IDirect3DTexture1_Vtbl);
1822 (*ppSurf)->ref = 1;
1823 (*ppSurf)->version = 7;
1824 (*ppSurf)->ddraw = This;
1825 (*ppSurf)->surface_desc.dwSize = sizeof(DDSURFACEDESC2);
1826 (*ppSurf)->surface_desc.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1827 DD_STRUCT_COPY_BYSIZE(&(*ppSurf)->surface_desc, pDDSD);
1829 /* Surface attachments */
1830 (*ppSurf)->next_attached = NULL;
1831 (*ppSurf)->first_attached = *ppSurf;
1833 (*ppSurf)->next_complex = NULL;
1834 (*ppSurf)->first_complex = *ppSurf;
1836 /* Needed to re-create the surface on an implementation change */
1837 (*ppSurf)->ImplType = ImplType;
1839 /* For D3DDevice creation */
1840 (*ppSurf)->isRenderTarget = FALSE;
1842 /* A trace message for debugging */
1843 TRACE("(%p) Created IDirectDrawSurface implementation structure at %p\n", This, *ppSurf);
1845 if(pDDSD->ddsCaps.dwCaps & ( DDSCAPS_PRIMARYSURFACE | DDSCAPS_TEXTURE | DDSCAPS_3DDEVICE) )
1847 /* Render targets and textures need a IParent interface,
1848 * because WineD3D will destroy them when the swapchain
1849 * is released
1851 parImpl = HeapAlloc(GetProcessHeap(), 0, sizeof(IParentImpl));
1852 if(!parImpl)
1854 ERR("Out of memory when allocating memory for a IParent implementation\n");
1855 return DDERR_OUTOFMEMORY;
1857 parImpl->ref = 1;
1858 ICOM_INIT_INTERFACE(parImpl, IParent, IParent_Vtbl);
1859 Parent = (IUnknown *) ICOM_INTERFACE(parImpl, IParent);
1860 TRACE("Using IParent interface %p as parent\n", parImpl);
1862 else
1864 /* Use the surface as parent */
1865 Parent = (IUnknown *) ICOM_INTERFACE(*ppSurf, IDirectDrawSurface7);
1866 TRACE("Using Surface interface %p as parent\n", *ppSurf);
1869 /* Now create the WineD3D Surface */
1870 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice,
1871 pDDSD->dwWidth,
1872 pDDSD->dwHeight,
1873 Format,
1874 TRUE /* Lockable */,
1875 FALSE /* Discard */,
1876 level,
1877 &(*ppSurf)->WineD3DSurface,
1878 ResType, Usage,
1879 Pool,
1880 WINED3DMULTISAMPLE_NONE,
1881 0 /* MultiSampleQuality */,
1882 0 /* SharedHandle */,
1883 ImplType,
1884 Parent);
1886 if(hr != D3D_OK)
1888 ERR("IWineD3DDevice::CreateSurface failed. hr = %08x\n", hr);
1889 return hr;
1892 /* Set the child of the parent implementation if it exists */
1893 if(parImpl)
1895 parImpl->child = (IUnknown *) (*ppSurf)->WineD3DSurface;
1896 /* The IParent releases the WineD3DSurface, and
1897 * the ddraw surface does that too. Hold a reference
1899 IWineD3DSurface_AddRef((*ppSurf)->WineD3DSurface);
1902 /* Increase the surface counter, and attach the surface */
1903 InterlockedIncrement(&This->surfaces);
1904 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
1906 /* Here we could store all created surfaces in the DirectDrawImpl structure,
1907 * But this could also be delegated to WineDDraw, as it keeps track of all its
1908 * resources. Not implemented for now, as there are more important things ;)
1911 /* Get the pixel format of the WineD3DSurface and store it.
1912 * Don't use the Format choosen above, WineD3D might have
1913 * changed it
1915 Desc.Format = &Format;
1916 Desc.Type = &ResType;
1917 Desc.Usage = &Usage;
1918 Desc.Pool = &dummy_d3dpool;
1919 Desc.Size = &dummy_uint;
1920 Desc.MultiSampleType = &dummy_mst;
1921 Desc.MultiSampleQuality = &dummy_dword;
1922 Desc.Width = &Width;
1923 Desc.Height = &Height;
1925 (*ppSurf)->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
1926 hr = IWineD3DSurface_GetDesc((*ppSurf)->WineD3DSurface, &Desc);
1927 if(hr != D3D_OK)
1929 ERR("IWineD3DSurface::GetDesc failed\n");
1930 IDirectDrawSurface7_Release( (IDirectDrawSurface7 *) *ppSurf);
1931 return hr;
1934 if(Format == WINED3DFMT_UNKNOWN)
1936 FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN\n");
1938 PixelFormat_WineD3DtoDD( &(*ppSurf)->surface_desc.u4.ddpfPixelFormat, Format);
1940 /* Anno 1602 stores the pitch right after surface creation, so make sure it's there.
1941 * I can't LockRect() the surface here because if OpenGL surfaces are in use, the
1942 * WineD3DDevice might not be useable for 3D yet, so an extra method was created
1944 (*ppSurf)->surface_desc.dwFlags |= DDSD_PITCH;
1945 (*ppSurf)->surface_desc.u1.lPitch = IWineD3DSurface_GetPitch((*ppSurf)->WineD3DSurface);
1947 /* Application passed a color key? Set it! */
1948 if(pDDSD->dwFlags & DDSD_CKDESTOVERLAY)
1950 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
1951 DDCKEY_DESTOVERLAY,
1952 &pDDSD->u3.ddckCKDestOverlay);
1954 if(pDDSD->dwFlags & DDSD_CKDESTBLT)
1956 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
1957 DDCKEY_DESTBLT,
1958 &pDDSD->ddckCKDestBlt);
1960 if(pDDSD->dwFlags & DDSD_CKSRCOVERLAY)
1962 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
1963 DDCKEY_SRCOVERLAY,
1964 &pDDSD->ddckCKSrcOverlay);
1966 if(pDDSD->dwFlags & DDSD_CKSRCBLT)
1968 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
1969 DDCKEY_SRCBLT,
1970 &pDDSD->ddckCKSrcBlt);
1972 if ( pDDSD->dwFlags & DDSD_LPSURFACE)
1974 hr = IWineD3DSurface_SetMem((*ppSurf)->WineD3DSurface, pDDSD->lpSurface);
1975 if(hr != WINED3D_OK)
1977 /* No need for a trace here, wined3d does that for us */
1978 IDirectDrawSurface7_Release(ICOM_INTERFACE((*ppSurf), IDirectDrawSurface7));
1979 return hr;
1983 return DD_OK;
1986 /*****************************************************************************
1987 * IDirectDraw7::CreateSurface
1989 * Creates a new IDirectDrawSurface object and returns its interface.
1991 * The surface connections with wined3d are a bit tricky. Basically it works
1992 * like this:
1994 * |------------------------| |-----------------|
1995 * | DDraw surface | | WineD3DSurface |
1996 * | | | |
1997 * | WineD3DSurface |-------------->| |
1998 * | Child |<------------->| Parent |
1999 * |------------------------| |-----------------|
2001 * The DDraw surface is the parent of the wined3d surface, and it releases
2002 * the WineD3DSurface when the ddraw surface is destroyed.
2004 * However, for all surfaces which can be in a container in WineD3D,
2005 * we have to do this. These surfaces are ususally complex surfaces,
2006 * so this concerns primary surfaces with a front and a back buffer,
2007 * and textures.
2009 * |------------------------| |-----------------|
2010 * | DDraw surface | | Containter |
2011 * | | | |
2012 * | Child |<------------->| Parent |
2013 * | Texture |<------------->| |
2014 * | WineD3DSurface |<----| | Levels |<--|
2015 * | Complex connection | | | | |
2016 * |------------------------| | |-----------------| |
2017 * ^ | |
2018 * | | |
2019 * | | |
2020 * | |------------------| | |-----------------| |
2021 * | | IParent | |-------->| WineD3DSurface | |
2022 * | | | | | |
2023 * | | Child |<------------->| Parent | |
2024 * | | | | Container |<--|
2025 * | |------------------| |-----------------| |
2026 * | |
2027 * | |----------------------| |
2028 * | | DDraw surface 2 | |
2029 * | | | |
2030 * |<->| Complex root Child | |
2031 * | | Texture | |
2032 * | | WineD3DSurface |<----| |
2033 * | |----------------------| | |
2034 * | | |
2035 * | |---------------------| | |-----------------| |
2036 * | | IParent | |----->| WineD3DSurface | |
2037 * | | | | | |
2038 * | | Child |<---------->| Parent | |
2039 * | |---------------------| | Container |<--|
2040 * | |-----------------| |
2041 * | |
2042 * | ---More surfaces can follow--- |
2044 * The reason is that the IWineD3DSwapchain(render target container)
2045 * and the IWineD3DTexure(Texture container) release the parents
2046 * of their surface's children, but by releasing the complex root
2047 * the surfaces which are complexly attached to it are destroyed
2048 * too. See IDirectDrawSurface::Release for a more detailed
2049 * explanation.
2051 * Params:
2052 * DDSD: Description of the surface to create
2053 * Surf: Address to store the interface pointer at
2054 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2055 * aggregation, so it has to be NULL
2057 * Returns:
2058 * DD_OK on success
2059 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2060 * DDERR_* if an error occurs
2062 *****************************************************************************/
2063 static HRESULT WINAPI
2064 IDirectDrawImpl_CreateSurface(IDirectDraw7 *iface,
2065 DDSURFACEDESC2 *DDSD,
2066 IDirectDrawSurface7 **Surf,
2067 IUnknown *UnkOuter)
2069 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2070 IDirectDrawSurfaceImpl *object = NULL;
2071 HRESULT hr;
2072 LONG extra_surfaces = 0, i;
2073 DDSURFACEDESC2 desc2;
2074 UINT level = 0;
2075 WINED3DDISPLAYMODE Mode;
2077 TRACE("(%p)->(%p,%p,%p)\n", This, DDSD, Surf, UnkOuter);
2079 /* Some checks before we start */
2080 if (TRACE_ON(ddraw))
2082 TRACE(" (%p) Requesting surface desc :\n", This);
2083 DDRAW_dump_surface_desc(DDSD);
2086 if (UnkOuter != NULL)
2088 FIXME("(%p) : outer != NULL?\n", This);
2089 return CLASS_E_NOAGGREGATION; /* unchecked */
2092 if (!(DDSD->dwFlags & DDSD_CAPS))
2094 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2095 DDSD->dwFlags |= DDSD_CAPS;
2097 if (DDSD->ddsCaps.dwCaps == 0)
2099 /* This has been checked on real Windows */
2100 DDSD->ddsCaps.dwCaps = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
2103 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2105 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2106 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2109 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2111 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2112 WARN("(%p) Null surface pointer specified, ignore it!\n", This);
2113 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2116 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2117 !(This->cooperative_level & DDSCL_EXCLUSIVE))
2119 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n", This);
2120 *Surf = NULL;
2121 return DDERR_NOEXCLUSIVEMODE;
2124 if (Surf == NULL)
2126 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", This);
2127 return E_POINTER; /* unchecked */
2130 /* According to the msdn this flag is ignored by CreateSurface */
2131 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2132 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2134 /* Modify some flags */
2135 memset(&desc2, 0, sizeof(desc2));
2136 desc2.dwSize = sizeof(desc2); /* For the struct copy */
2137 DD_STRUCT_COPY_BYSIZE(&desc2, DDSD);
2138 desc2.dwSize = sizeof(desc2); /* To override a possibly smaller size */
2139 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2141 /* Get the video mode from WineD3D - we will need it */
2142 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
2143 0, /* Swapchain 0 */
2144 &Mode);
2145 if(FAILED(hr))
2147 ERR("Failed to read display mode from wined3d\n");
2148 switch(This->orig_bpp)
2150 case 8:
2151 Mode.Format = WINED3DFMT_P8;
2152 break;
2154 case 15:
2155 Mode.Format = WINED3DFMT_X1R5G5B5;
2156 break;
2158 case 16:
2159 Mode.Format = WINED3DFMT_R5G6B5;
2160 break;
2162 case 24:
2163 Mode.Format = WINED3DFMT_R8G8B8;
2164 break;
2166 case 32:
2167 Mode.Format = WINED3DFMT_X8R8G8B8;
2168 break;
2170 Mode.Width = This->orig_width;
2171 Mode.Height = This->orig_height;
2174 /* No pixelformat given? Use the current screen format */
2175 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2177 desc2.dwFlags |= DDSD_PIXELFORMAT;
2178 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2180 /* Wait: It could be a Z buffer */
2181 if(desc2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
2183 switch(desc2.u2.dwMipMapCount) /* Who had this glorious idea? */
2185 case 15:
2186 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D15S1);
2187 break;
2188 case 16:
2189 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D16);
2190 break;
2191 case 24:
2192 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D24X8);
2193 break;
2194 case 32:
2195 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D32);
2196 break;
2197 default:
2198 ERR("Unknown Z buffer bit depth\n");
2201 else
2203 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
2207 /* No Width or no Height? Use the current window size or
2208 * the original screen size
2210 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2211 !(desc2.dwFlags & DDSD_HEIGHT) )
2213 HWND window;
2215 /* Fallback: From WineD3D / original mode */
2216 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2217 desc2.dwWidth = Mode.Width;
2218 desc2.dwHeight = Mode.Height;
2220 hr = IWineD3DDevice_GetHWND(This->wineD3DDevice,
2221 &window);
2222 if( (hr == D3D_OK) && (window != 0) )
2224 RECT rect;
2225 if(GetWindowRect(window, &rect) )
2227 /* This is a hack until I find a better solution */
2228 if( (rect.right - rect.left) <= 1 ||
2229 (rect.bottom - rect.top) <= 1 )
2231 FIXME("Wanted to get surface dimensions from window %p, but it has only \
2232 a size of %dx%d. Using full screen dimensions\n",
2233 window, rect.right - rect.left, rect.bottom - rect.top);
2235 else
2237 /* Not sure if this is correct */
2238 desc2.dwWidth = rect.right - rect.left;
2239 desc2.dwHeight = rect.bottom - rect.top;
2240 TRACE("Using window %p's dimensions: %dx%d\n", window, desc2.dwWidth, desc2.dwHeight);
2246 /* Mipmap count fixes */
2247 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2249 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2251 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2253 /* Mipmap count is given, nothing to do */
2255 else
2257 /* Undocumented feature: Create sublevels until
2258 * either the width or the height is 1
2260 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2261 desc2.dwWidth : desc2.dwHeight;
2262 desc2.u2.dwMipMapCount = 0;
2263 while( min )
2265 desc2.u2.dwMipMapCount += 1;
2266 min >>= 1;
2270 else
2272 /* Not-complex mipmap -> Mipmapcount = 1 */
2273 desc2.u2.dwMipMapCount = 1;
2275 extra_surfaces = desc2.u2.dwMipMapCount - 1;
2277 /* There's a mipmap count in the created surface in any case */
2278 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2280 /* If no mipmap is given, the texture has only one level */
2282 /* The first surface is a front buffer, the back buffer is created afterwards */
2283 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2285 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2288 /* Create the first surface */
2289 hr = IDirectDrawImpl_CreateNewSurface(This, &desc2, &object, 0);
2290 if( hr != DD_OK)
2292 ERR("IDirectDrawImpl_CreateNewSurface failed with %08x\n", hr);
2293 return hr;
2296 *Surf = ICOM_INTERFACE(object, IDirectDrawSurface7);
2298 /* Create Additional surfaces if necessary
2299 * This applies to Primary surfaces which have a back buffer count
2300 * set, but not to mipmap textures. In case of Mipmap textures,
2301 * wineD3D takes care of the creation of additional surfaces
2303 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2305 extra_surfaces = DDSD->dwBackBufferCount;
2306 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2307 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2309 /* Set the DDSCAPS2_MIPMAPSUBLEVEL flag on mipmap sublevels according to the msdn */
2310 if(DDSD->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2312 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2315 for(i = 0; i < extra_surfaces; i++)
2317 IDirectDrawSurfaceImpl *object2 = NULL;
2318 IDirectDrawSurfaceImpl *iterator;
2320 /* increase the mipmap level, but only if a mipmap is created
2321 * In this case, also halve the size
2323 if(DDSD->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2325 level++;
2326 if(desc2.dwWidth > 1) desc2.dwWidth /= 2;
2327 if(desc2.dwHeight > 1) desc2.dwHeight /= 2;
2330 hr = IDirectDrawImpl_CreateNewSurface(This,
2331 &desc2,
2332 &object2,
2333 level);
2334 if(hr != DD_OK)
2336 /* This destroys and possibly created surfaces too */
2337 IDirectDrawSurface_Release( ICOM_INTERFACE(object, IDirectDrawSurface7) );
2338 return hr;
2341 /* Add the new surface to the complex attachment list */
2342 object2->first_complex = object;
2343 object2->next_complex = NULL;
2344 iterator = object;
2345 while(iterator->next_complex) iterator = iterator->next_complex;
2346 iterator->next_complex = object2;
2348 /* Remove the (possible) back buffer cap from the new surface description,
2349 * because only one surface in the flipping chain is a back buffer, one
2350 * is a front buffer, the others are just primary surfaces.
2352 desc2.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2355 /* Addref the ddraw interface to keep an reference for each surface */
2356 IDirectDraw7_AddRef(iface);
2357 object->ifaceToRelease = (IUnknown *) iface;
2359 /* If the implementation is OpenGL and there's no d3ddevice, attach a d3ddevice
2360 * But attach the d3ddevice only if the currently created surface was
2361 * a primary surface (2D app in 3D mode) or a 3DDEVICE surface (3D app)
2362 * The only case I can think of where this doesn't apply is when a
2363 * 2D app was configured by the user to run with OpenGL and it didn't create
2364 * the render target as first surface. In this case the render target creation
2365 * will cause the 3D init.
2367 if( (This->ImplType == SURFACE_OPENGL) && !(This->d3d_initialized) &&
2368 desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE) )
2370 IDirectDrawSurfaceImpl *target = object, *surface;
2371 struct list *entry;
2373 /* Search for the primary to use as render target */
2374 LIST_FOR_EACH(entry, &This->surface_list)
2376 surface = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2377 if(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2379 /* found */
2380 target = surface;
2381 TRACE("Using primary %p as render target\n", target);
2382 break;
2386 TRACE("(%p) Attaching a D3DDevice, rendertarget = %p\n", This, target);
2387 hr = IDirectDrawImpl_AttachD3DDevice(This, target->first_complex);
2388 if(hr != D3D_OK)
2390 ERR("IDirectDrawImpl_AttachD3DDevice failed, hr = %x\n", hr);
2394 /* Create a WineD3DTexture if a texture was requested */
2395 if(DDSD->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
2397 UINT levels;
2398 WINED3DFORMAT Format;
2399 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
2401 This->tex_root = object;
2403 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2405 /* a mipmap is created, create enough levels */
2406 levels = desc2.u2.dwMipMapCount;
2408 else
2410 /* No mipmap is created, create one level */
2411 levels = 1;
2414 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM */
2415 if(DDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
2417 Pool = WINED3DPOOL_SYSTEMMEM;
2419 /* Should I forward the MANEGED cap to the managed pool ? */
2421 /* Get the format. It's set already by CreateNewSurface */
2422 Format = PixelFormat_DD2WineD3D(&object->surface_desc.u4.ddpfPixelFormat);
2424 /* The surfaces are already created, the callback only
2425 * passes the IWineD3DSurface to WineD3D
2427 hr = IWineD3DDevice_CreateTexture( This->wineD3DDevice,
2428 DDSD->dwWidth, DDSD->dwHeight,
2429 levels, /* MipMapCount = Levels */
2430 0, /* usage */
2431 Format,
2432 Pool,
2433 &object->wineD3DTexture,
2434 0, /* SharedHandle */
2435 (IUnknown *) ICOM_INTERFACE(object, IDirectDrawSurface7),
2436 D3D7CB_CreateSurface );
2437 This->tex_root = NULL;
2440 return hr;
2443 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
2444 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
2446 static BOOL
2447 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
2448 const DDPIXELFORMAT *provided)
2450 /* Some flags must be present in both or neither for a match. */
2451 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
2452 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
2453 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
2455 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2456 return FALSE;
2458 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
2459 return FALSE;
2461 if (requested->dwFlags & DDPF_FOURCC)
2462 if (requested->dwFourCC != provided->dwFourCC)
2463 return FALSE;
2465 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
2466 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2467 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
2468 return FALSE;
2470 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2471 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2472 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
2473 return FALSE;
2475 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
2476 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
2477 return FALSE;
2479 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
2480 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2481 |DDPF_BUMPDUDV))
2482 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
2483 return FALSE;
2485 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
2486 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
2487 return FALSE;
2489 return TRUE;
2492 static BOOL
2493 IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested,
2494 const DDSURFACEDESC2* provided)
2496 struct compare_info
2498 DWORD flag;
2499 ptrdiff_t offset;
2500 size_t size;
2503 #define CMP(FLAG, FIELD) \
2504 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
2505 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
2507 static const struct compare_info compare[] =
2509 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
2510 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
2511 CMP(CAPS, ddsCaps),
2512 CMP(CKDESTBLT, ddckCKDestBlt),
2513 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
2514 CMP(CKSRCBLT, ddckCKSrcBlt),
2515 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
2516 CMP(HEIGHT, dwHeight),
2517 CMP(LINEARSIZE, u1 /* dwLinearSize */),
2518 CMP(LPSURFACE, lpSurface),
2519 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
2520 CMP(PITCH, u1 /* lPitch */),
2521 /* PIXELFORMAT: manual */
2522 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
2523 CMP(TEXTURESTAGE, dwTextureStage),
2524 CMP(WIDTH, dwWidth),
2525 /* ZBUFFERBITDEPTH: "obsolete" */
2528 #undef CMP
2530 unsigned int i;
2532 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2533 return FALSE;
2535 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
2537 if (requested->dwFlags & compare[i].flag
2538 && memcmp((const char *)provided + compare[i].offset,
2539 (const char *)requested + compare[i].offset,
2540 compare[i].size) != 0)
2541 return FALSE;
2544 if (requested->dwFlags & DDSD_PIXELFORMAT)
2546 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
2547 &provided->u4.ddpfPixelFormat))
2548 return FALSE;
2551 return TRUE;
2554 #undef DDENUMSURFACES_SEARCHTYPE
2555 #undef DDENUMSURFACES_MATCHTYPE
2557 /*****************************************************************************
2558 * IDirectDraw7::EnumSurfaces
2560 * Loops through all surfaces attached to this device and calls the
2561 * application callback. This can't be relayed to WineD3DDevice,
2562 * because some WineD3DSurfaces' parents are IParent objects
2564 * Params:
2565 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
2566 * DDSD: Description to filter for
2567 * Context: Application-provided pointer, it's passed unmodified to the
2568 * Callback function
2569 * Callback: Address to call for each surface
2571 * Returns:
2572 * DDERR_INVALIDPARAMS if the callback is NULL
2573 * DD_OK on success
2575 *****************************************************************************/
2576 static HRESULT WINAPI
2577 IDirectDrawImpl_EnumSurfaces(IDirectDraw7 *iface,
2578 DWORD Flags,
2579 DDSURFACEDESC2 *DDSD,
2580 void *Context,
2581 LPDDENUMSURFACESCALLBACK7 Callback)
2583 /* The surface enumeration is handled by WineDDraw,
2584 * because it keeps track of all surfaces attached to
2585 * it. The filtering is done by our callback function,
2586 * because WineDDraw doesn't handle ddraw-like surface
2587 * caps structures
2589 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2590 IDirectDrawSurfaceImpl *surf;
2591 BOOL all, nomatch;
2592 DDSURFACEDESC2 desc;
2593 struct list *entry, *entry2;
2595 all = Flags & DDENUMSURFACES_ALL;
2596 nomatch = Flags & DDENUMSURFACES_NOMATCH;
2598 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, DDSD, Context, Callback);
2600 if(!Callback)
2601 return DDERR_INVALIDPARAMS;
2603 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
2604 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
2606 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2607 if (all || (nomatch != IDirectDrawImpl_DDSD_Match(DDSD, &surf->surface_desc)))
2609 desc = surf->surface_desc;
2610 IDirectDrawSurface7_AddRef(ICOM_INTERFACE(surf, IDirectDrawSurface7));
2611 if(Callback( ICOM_INTERFACE(surf, IDirectDrawSurface7), &desc, Context) != DDENUMRET_OK)
2612 return DD_OK;
2615 return DD_OK;
2618 /*****************************************************************************
2619 * D3D7CB_CreateRenderTarget
2621 * Callback called by WineD3D to create Surfaces for render target usage
2622 * This function takes the D3D target from the IDirectDrawImpl structure,
2623 * and returns the WineD3DSurface. To avoid double usage, the surface
2624 * is marked as render target afterwards
2626 * Params
2627 * device: The WineD3DDevice's parent
2628 * Width, Height, Format: Dimensions and pixelformat of the render target
2629 * Ignored, because the surface already exists
2630 * MultiSample, MultisampleQuality, Lockable: Ignored for the same reason
2631 * Lockable: ignored
2632 * ppSurface: Address to pass the surface pointer back at
2633 * pSharedHandle: Ignored
2635 * Returns:
2636 * Always returns D3D_OK
2638 *****************************************************************************/
2639 static HRESULT WINAPI
2640 D3D7CB_CreateRenderTarget(IUnknown *device, UINT Width, UINT Height,
2641 WINED3DFORMAT Format,
2642 WINED3DMULTISAMPLE_TYPE MultiSample,
2643 DWORD MultisampleQuality,
2644 BOOL Lockable,
2645 IWineD3DSurface** ppSurface,
2646 HANDLE* pSharedHandle)
2648 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
2649 IDirectDrawSurfaceImpl *d3dSurface = (IDirectDrawSurfaceImpl *) This->d3d_target->first_complex;
2650 TRACE("(%p) call back\n", device);
2652 /* Loop through the complex chain and try to find unused primary surfaces */
2653 while(d3dSurface->isRenderTarget)
2655 d3dSurface = d3dSurface->next_complex;
2656 if(!d3dSurface) break;
2658 if(!d3dSurface)
2660 d3dSurface = This->d3d_target;
2661 ERR(" (%p) : No DirectDrawSurface found to create the back buffer. Using the front buffer as back buffer. Uncertain consequences\n", This);
2664 /* TODO: Return failure if the dimensions do not match, but this shouldn't happen */
2666 *ppSurface = d3dSurface->WineD3DSurface;
2667 d3dSurface->isRenderTarget = TRUE;
2668 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *ppSurface, d3dSurface);
2669 return D3D_OK;
2672 static HRESULT WINAPI
2673 D3D7CB_CreateDepthStencilSurface(IUnknown *device,
2674 UINT Width,
2675 UINT Height,
2676 WINED3DFORMAT Format,
2677 WINED3DMULTISAMPLE_TYPE MultiSample,
2678 DWORD MultisampleQuality,
2679 BOOL Discard,
2680 IWineD3DSurface** ppSurface,
2681 HANDLE* pSharedHandle)
2683 /* Create a Depth Stencil surface to make WineD3D happy */
2684 HRESULT hr = D3D_OK;
2685 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
2686 DDSURFACEDESC2 ddsd;
2688 TRACE("(%p) call back\n", device);
2690 *ppSurface = NULL;
2692 /* Create a DirectDraw surface */
2693 memset(&ddsd, 0, sizeof(ddsd));
2694 ddsd.dwSize = sizeof(ddsd);
2695 ddsd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2696 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
2697 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
2698 ddsd.dwHeight = Height;
2699 ddsd.dwWidth = Width;
2700 if(Format != 0)
2702 PixelFormat_WineD3DtoDD(&ddsd.u4.ddpfPixelFormat, Format);
2704 else
2706 ddsd.dwFlags ^= DDSD_PIXELFORMAT;
2709 This->depthstencil = TRUE;
2710 hr = IDirectDraw7_CreateSurface((IDirectDraw7 *) This,
2711 &ddsd,
2712 (IDirectDrawSurface7 **) &This->DepthStencilBuffer,
2713 NULL);
2714 This->depthstencil = FALSE;
2715 if(FAILED(hr))
2717 ERR(" (%p) Creating a DepthStencil Surface failed, result = %x\n", This, hr);
2718 return hr;
2720 *ppSurface = This->DepthStencilBuffer->WineD3DSurface;
2721 return D3D_OK;
2724 /*****************************************************************************
2725 * D3D7CB_CreateAdditionalSwapChain
2727 * Callback function for WineD3D which creates a new WineD3DSwapchain
2728 * interface. It also creates an IParent interface to store that pointer,
2729 * so the WineD3DSwapchain has a parent and can be released when the D3D
2730 * device is destroyed
2731 *****************************************************************************/
2732 static HRESULT WINAPI
2733 D3D7CB_CreateAdditionalSwapChain(IUnknown *device,
2734 WINED3DPRESENT_PARAMETERS* pPresentationParameters,
2735 IWineD3DSwapChain ** ppSwapChain)
2737 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
2738 IParentImpl *object = NULL;
2739 HRESULT res = D3D_OK;
2740 IWineD3DSwapChain *swapchain;
2741 TRACE("(%p) call back\n", device);
2743 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IParentImpl));
2744 if (NULL == object)
2746 FIXME("Allocation of memory failed\n");
2747 *ppSwapChain = NULL;
2748 return DDERR_OUTOFVIDEOMEMORY;
2751 ICOM_INIT_INTERFACE(object, IParent, IParent_Vtbl);
2752 object->ref = 1;
2754 res = IWineD3DDevice_CreateAdditionalSwapChain(This->wineD3DDevice,
2755 pPresentationParameters,
2756 &swapchain,
2757 (IUnknown*) ICOM_INTERFACE(object, IParent),
2758 D3D7CB_CreateRenderTarget,
2759 D3D7CB_CreateDepthStencilSurface);
2760 if (res != D3D_OK)
2762 FIXME("(%p) call to IWineD3DDevice_CreateAdditionalSwapChain failed\n", This);
2763 HeapFree(GetProcessHeap(), 0 , object);
2764 *ppSwapChain = NULL;
2766 else
2768 *ppSwapChain = swapchain;
2769 object->child = (IUnknown *) swapchain;
2772 return res;
2775 /*****************************************************************************
2776 * IDirectDrawImpl_AttachD3DDevice
2778 * Initializes the D3D capabilities of WineD3D
2780 * Params:
2781 * primary: The primary surface for D3D
2783 * Returns
2784 * DD_OK on success,
2785 * DDERR_* otherwise
2787 *****************************************************************************/
2788 static HRESULT WINAPI
2789 IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This,
2790 IDirectDrawSurfaceImpl *primary)
2792 HRESULT hr;
2793 UINT BackBufferCount = 0;
2794 HWND window;
2796 WINED3DPRESENT_PARAMETERS localParameters;
2797 BOOL isWindowed, EnableAutoDepthStencil;
2798 WINED3DFORMAT AutoDepthStencilFormat;
2799 WINED3DMULTISAMPLE_TYPE MultiSampleType;
2800 WINED3DSWAPEFFECT SwapEffect;
2801 DWORD Flags, MultiSampleQuality;
2802 UINT FullScreen_RefreshRateInHz, PresentationInterval;
2803 WINED3DDISPLAYMODE Mode;
2805 TRACE("(%p)->(%p)\n", This, primary);
2807 /* Get the window */
2808 hr = IWineD3DDevice_GetHWND(This->wineD3DDevice,
2809 &window);
2810 if(hr != D3D_OK)
2812 ERR("IWineD3DDevice::GetHWND failed\n");
2813 return hr;
2816 /* If there's no window, create a hidden window. WineD3D needs it */
2817 if(window == 0)
2819 window = CreateWindowExA(0, This->classname, "Hidden D3D Window",
2820 WS_DISABLED, 0, 0,
2821 GetSystemMetrics(SM_CXSCREEN),
2822 GetSystemMetrics(SM_CYSCREEN),
2823 NULL, NULL, GetModuleHandleA(0), NULL);
2825 ShowWindow(window, SW_HIDE); /* Just to be sure */
2826 WARN("(%p) No window for the Direct3DDevice, created a hidden window. HWND=%p\n", This, window);
2827 This->d3d_window = window;
2829 else
2831 TRACE("(%p) Using existing window %p for Direct3D rendering\n", This, window);
2834 /* use the surface description for the device parameters, not the
2835 * Device settings. The app might render to an offscreen surface
2837 Mode.Width = primary->surface_desc.dwWidth;
2838 Mode.Height = primary->surface_desc.dwHeight;
2839 Mode.Format = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2841 if(primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT)
2843 BackBufferCount = primary->surface_desc.dwBackBufferCount;
2846 /* Store the future Render Target surface */
2847 This->d3d_target = primary;
2849 isWindowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
2850 EnableAutoDepthStencil = FALSE;
2851 AutoDepthStencilFormat = WINED3DFMT_D16;
2852 MultiSampleType = WINED3DMULTISAMPLE_NONE;
2853 SwapEffect = WINED3DSWAPEFFECT_COPY;
2854 Flags = 0;
2855 MultiSampleQuality = 0;
2856 FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
2857 PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
2859 TRACE("Passing mode %d\n", Mode.Format);
2861 localParameters.BackBufferWidth = &Mode.Width;
2862 localParameters.BackBufferHeight = &Mode.Height;
2863 localParameters.BackBufferFormat = (WINED3DFORMAT *) &Mode.Format;
2864 localParameters.BackBufferCount = (UINT *) &BackBufferCount;
2865 localParameters.MultiSampleType = &MultiSampleType;
2866 localParameters.MultiSampleQuality = &MultiSampleQuality;
2867 localParameters.SwapEffect = &SwapEffect;
2868 localParameters.hDeviceWindow = &window;
2869 localParameters.Windowed = &isWindowed;
2870 localParameters.EnableAutoDepthStencil = &EnableAutoDepthStencil;
2871 localParameters.AutoDepthStencilFormat = &AutoDepthStencilFormat;
2872 localParameters.Flags = &Flags;
2873 localParameters.FullScreen_RefreshRateInHz = &FullScreen_RefreshRateInHz;
2874 localParameters.PresentationInterval = &PresentationInterval;
2876 /* Set this NOW, otherwise creating the depth stencil surface will cause a
2877 * recursive loop until ram or emulated video memory is full
2879 This->d3d_initialized = TRUE;
2881 hr = IWineD3DDevice_Init3D(This->wineD3DDevice,
2882 &localParameters,
2883 D3D7CB_CreateAdditionalSwapChain);
2884 if(FAILED(hr))
2886 This->wineD3DDevice = NULL;
2887 return hr;
2890 /* Create an Index Buffer parent */
2891 TRACE("(%p) Successfully initialized 3D\n", This);
2892 return DD_OK;
2895 /*****************************************************************************
2896 * DirectDrawCreateClipper (DDRAW.@)
2898 * Creates a new IDirectDrawClipper object.
2900 * Params:
2901 * Clipper: Address to write the interface pointer to
2902 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
2903 * NULL
2905 * Returns:
2906 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2907 * E_OUTOFMEMORY if allocating the object failed
2909 *****************************************************************************/
2910 HRESULT WINAPI
2911 DirectDrawCreateClipper(DWORD Flags,
2912 IDirectDrawClipper **Clipper,
2913 IUnknown *UnkOuter)
2915 IDirectDrawClipperImpl* object;
2916 TRACE("(%08x,%p,%p)\n", Flags, Clipper, UnkOuter);
2918 if (UnkOuter != NULL) return CLASS_E_NOAGGREGATION;
2920 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2921 sizeof(IDirectDrawClipperImpl));
2922 if (object == NULL) return E_OUTOFMEMORY;
2924 ICOM_INIT_INTERFACE(object, IDirectDrawClipper, IDirectDrawClipper_Vtbl);
2925 object->ref = 1;
2926 object->hWnd = 0;
2927 object->ddraw_owner = NULL;
2929 *Clipper = (IDirectDrawClipper *) object;
2930 return DD_OK;
2933 /*****************************************************************************
2934 * IDirectDraw7::CreateClipper
2936 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
2938 *****************************************************************************/
2939 static HRESULT WINAPI
2940 IDirectDrawImpl_CreateClipper(IDirectDraw7 *iface,
2941 DWORD Flags,
2942 IDirectDrawClipper **Clipper,
2943 IUnknown *UnkOuter)
2945 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2946 TRACE("(%p)->(%x,%p,%p)\n", This, Flags, Clipper, UnkOuter);
2947 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
2950 /*****************************************************************************
2951 * IDirectDraw7::CreatePalette
2953 * Creates a new IDirectDrawPalette object
2955 * Params:
2956 * Flags: The flags for the new clipper
2957 * ColorTable: Color table to assign to the new clipper
2958 * Palette: Address to write the interface pointer to
2959 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
2960 * NULL
2962 * Returns:
2963 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2964 * E_OUTOFMEMORY if allocating the object failed
2966 *****************************************************************************/
2967 static HRESULT WINAPI
2968 IDirectDrawImpl_CreatePalette(IDirectDraw7 *iface,
2969 DWORD Flags,
2970 PALETTEENTRY *ColorTable,
2971 IDirectDrawPalette **Palette,
2972 IUnknown *pUnkOuter)
2974 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2975 IDirectDrawPaletteImpl *object;
2976 HRESULT hr = DDERR_GENERIC;
2977 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, ColorTable, Palette, pUnkOuter);
2979 if(pUnkOuter != NULL)
2981 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
2982 return CLASS_E_NOAGGREGATION;
2985 /* The refcount test shows that a cooplevel is required for this */
2986 if(!This->cooperative_level)
2988 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
2989 return DDERR_NOCOOPERATIVELEVELSET;
2992 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
2993 if(!object)
2995 ERR("Out of memory when allocating memory for a palette implementation\n");
2996 return E_OUTOFMEMORY;
2999 ICOM_INIT_INTERFACE(object, IDirectDrawPalette, IDirectDrawPalette_Vtbl);
3000 object->ref = 1;
3001 object->ddraw_owner = This;
3003 hr = IWineD3DDevice_CreatePalette(This->wineD3DDevice, Flags, ColorTable, &object->wineD3DPalette, (IUnknown *) ICOM_INTERFACE(object, IDirectDrawPalette) );
3004 if(hr != DD_OK)
3006 HeapFree(GetProcessHeap(), 0, object);
3007 return hr;
3010 IDirectDraw7_AddRef(iface);
3011 object->ifaceToRelease = (IUnknown *) iface;
3012 *Palette = ICOM_INTERFACE(object, IDirectDrawPalette);
3013 return DD_OK;
3016 /*****************************************************************************
3017 * IDirectDraw7::DuplicateSurface
3019 * Duplicates a surface. The surface memory points to the same memory as
3020 * the original surface, and it's released when the last surface referencing
3021 * it is released. I guess that's beyond Wine's surface management right now
3022 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3023 * test application to implement this)
3025 * Params:
3026 * Src: Address of the source surface
3027 * Dest: Address to write the new surface pointer to
3029 * Returns:
3030 * See IDirectDraw7::CreateSurface
3032 *****************************************************************************/
3033 static HRESULT WINAPI
3034 IDirectDrawImpl_DuplicateSurface(IDirectDraw7 *iface,
3035 IDirectDrawSurface7 *Src,
3036 IDirectDrawSurface7 **Dest)
3038 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3039 IDirectDrawSurfaceImpl *Surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Src);
3041 FIXME("(%p)->(%p,%p)\n", This, Surf, Dest);
3043 /* For now, simply create a new, independent surface */
3044 return IDirectDraw7_CreateSurface(iface,
3045 &Surf->surface_desc,
3046 Dest,
3047 NULL);
3050 /*****************************************************************************
3051 * IDirectDraw7 VTable
3052 *****************************************************************************/
3053 const IDirectDraw7Vtbl IDirectDraw7_Vtbl =
3055 /*** IUnknown ***/
3056 IDirectDrawImpl_QueryInterface,
3057 IDirectDrawImpl_AddRef,
3058 IDirectDrawImpl_Release,
3059 /*** IDirectDraw ***/
3060 IDirectDrawImpl_Compact,
3061 IDirectDrawImpl_CreateClipper,
3062 IDirectDrawImpl_CreatePalette,
3063 IDirectDrawImpl_CreateSurface,
3064 IDirectDrawImpl_DuplicateSurface,
3065 IDirectDrawImpl_EnumDisplayModes,
3066 IDirectDrawImpl_EnumSurfaces,
3067 IDirectDrawImpl_FlipToGDISurface,
3068 IDirectDrawImpl_GetCaps,
3069 IDirectDrawImpl_GetDisplayMode,
3070 IDirectDrawImpl_GetFourCCCodes,
3071 IDirectDrawImpl_GetGDISurface,
3072 IDirectDrawImpl_GetMonitorFrequency,
3073 IDirectDrawImpl_GetScanLine,
3074 IDirectDrawImpl_GetVerticalBlankStatus,
3075 IDirectDrawImpl_Initialize,
3076 IDirectDrawImpl_RestoreDisplayMode,
3077 IDirectDrawImpl_SetCooperativeLevel,
3078 IDirectDrawImpl_SetDisplayMode,
3079 IDirectDrawImpl_WaitForVerticalBlank,
3080 /*** IDirectDraw2 ***/
3081 IDirectDrawImpl_GetAvailableVidMem,
3082 /*** IDirectDraw7 ***/
3083 IDirectDrawImpl_GetSurfaceFromDC,
3084 IDirectDrawImpl_RestoreAllSurfaces,
3085 IDirectDrawImpl_TestCooperativeLevel,
3086 IDirectDrawImpl_GetDeviceIdentifier,
3087 /*** IDirectDraw7 ***/
3088 IDirectDrawImpl_StartModeTest,
3089 IDirectDrawImpl_EvaluateMode