ddraw: Avoid enumerating display modes for formats that arent't supported anyway.
[wine/testsucceed.git] / dlls / ddraw / ddraw.c
blob46fd7293a81c770afe429496c4af218d669b5868
1 /*
2 * Copyright 1997-2000 Marcus Meissner
3 * Copyright 1998-2000 Lionel Ulmer
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2006 Stefan Dösinger
6 * Copyright 2008 Denver Gingerich
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <stdlib.h>
31 #define COBJMACROS
32 #define NONAMELESSUNION
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "wingdi.h"
38 #include "wine/exception.h"
40 #include "ddraw.h"
41 #include "d3d.h"
43 #include "ddraw_private.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
48 static BOOL IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested, const DDSURFACEDESC2* provided);
49 static HRESULT IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This, IDirectDrawSurfaceImpl *primary);
50 static HRESULT IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This, DDSURFACEDESC2 *pDDSD, IDirectDrawSurfaceImpl **ppSurf, UINT level);
51 static HRESULT IDirectDrawImpl_CreateGDISwapChain(IDirectDrawImpl *This, IDirectDrawSurfaceImpl *primary);
53 /* Device identifier. Don't relay it to WineD3D */
54 static const DDDEVICEIDENTIFIER2 deviceidentifier =
56 "display",
57 "DirectDraw HAL",
58 { { 0x00010001, 0x00010001 } },
59 0, 0, 0, 0,
60 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
61 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
65 static void STDMETHODCALLTYPE ddraw_null_wined3d_object_destroyed(void *parent) {}
67 const struct wined3d_parent_ops ddraw_null_wined3d_parent_ops =
69 ddraw_null_wined3d_object_destroyed,
72 /*****************************************************************************
73 * IUnknown Methods
74 *****************************************************************************/
76 /*****************************************************************************
77 * IDirectDraw7::QueryInterface
79 * Queries different interfaces of the DirectDraw object. It can return
80 * IDirectDraw interfaces in version 1, 2, 4 and 7, and IDirect3D interfaces
81 * in version 1, 2, 3 and 7. An IDirect3DDevice can be created with this
82 * method.
83 * The returned interface is AddRef()-ed before it's returned
85 * Used for version 1, 2, 4 and 7
87 * Params:
88 * refiid: Interface ID asked for
89 * obj: Used to return the interface pointer
91 * Returns:
92 * S_OK if an interface was found
93 * E_NOINTERFACE if the requested interface wasn't found
95 *****************************************************************************/
96 static HRESULT WINAPI
97 IDirectDrawImpl_QueryInterface(IDirectDraw7 *iface,
98 REFIID refiid,
99 void **obj)
101 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
103 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(refiid), obj);
105 /* Can change surface impl type */
106 EnterCriticalSection(&ddraw_cs);
108 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
109 *obj = NULL;
111 if(!refiid)
113 LeaveCriticalSection(&ddraw_cs);
114 return DDERR_INVALIDPARAMS;
117 /* Check DirectDraw Interfaces */
118 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
119 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
121 *obj = This;
122 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
124 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
126 *obj = &This->IDirectDraw4_vtbl;
127 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
129 else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
131 /* This Interface exists in ddrawex.dll, it is implemented in a wrapper */
132 WARN("IDirectDraw3 is not valid in ddraw.dll\n");
133 *obj = NULL;
134 LeaveCriticalSection(&ddraw_cs);
135 return E_NOINTERFACE;
137 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
139 *obj = &This->IDirectDraw2_vtbl;
140 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
142 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
144 *obj = &This->IDirectDraw_vtbl;
145 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
148 /* Direct3D
149 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
150 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
151 * who had this idea and why. The older interfaces can query and IDirect3D version
152 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
153 * and messy to implement with the common creation function, so it has been left out here.
155 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
156 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
157 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
158 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
160 /* Check the surface implementation */
161 if(This->ImplType == SURFACE_UNKNOWN)
163 /* Apps may create the IDirect3D Interface before the primary surface.
164 * set the surface implementation */
165 This->ImplType = SURFACE_OPENGL;
166 TRACE("(%p) Choosing OpenGL surfaces because a Direct3D interface was requested\n", This);
168 else if(This->ImplType != SURFACE_OPENGL && DefaultSurfaceType == SURFACE_UNKNOWN)
170 ERR("(%p) The App is requesting a D3D device, but a non-OpenGL surface type was choosen. Prepare for trouble!\n", This);
171 ERR(" (%p) You may want to contact wine-devel for help\n", This);
172 /* Should I assert(0) here??? */
174 else if(This->ImplType != SURFACE_OPENGL)
176 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
177 /* Do not abort here, only reject 3D Device creation */
180 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
182 This->d3dversion = 1;
183 *obj = &This->IDirect3D_vtbl;
184 TRACE(" returning Direct3D interface at %p.\n", *obj);
186 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
188 This->d3dversion = 2;
189 *obj = &This->IDirect3D2_vtbl;
190 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
192 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
194 This->d3dversion = 3;
195 *obj = &This->IDirect3D3_vtbl;
196 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
198 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
200 This->d3dversion = 7;
201 *obj = &This->IDirect3D7_vtbl;
202 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
205 else if (IsEqualGUID(refiid, &IID_IWineD3DDeviceParent))
207 *obj = &This->device_parent_vtbl;
210 /* Unknown interface */
211 else
213 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
214 LeaveCriticalSection(&ddraw_cs);
215 return E_NOINTERFACE;
218 IUnknown_AddRef( (IUnknown *) *obj );
219 LeaveCriticalSection(&ddraw_cs);
220 return S_OK;
223 /*****************************************************************************
224 * IDirectDraw7::AddRef
226 * Increases the interfaces refcount, basically
228 * DDraw refcounting is a bit tricky. The different DirectDraw interface
229 * versions have individual refcounts, but the IDirect3D interfaces do not.
230 * All interfaces are from one object, that means calling QueryInterface on an
231 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
232 * IDirectDrawImpl object.
234 * That means all AddRef and Release implementations of IDirectDrawX work
235 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
236 * except of IDirect3D7 which thunks to IDirectDraw7
238 * Returns: The new refcount
240 *****************************************************************************/
241 static ULONG WINAPI
242 IDirectDrawImpl_AddRef(IDirectDraw7 *iface)
244 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
245 ULONG ref = InterlockedIncrement(&This->ref7);
247 TRACE("(%p) : incrementing IDirectDraw7 refcount from %u.\n", This, ref -1);
249 if(ref == 1) InterlockedIncrement(&This->numIfaces);
251 return ref;
254 /*****************************************************************************
255 * IDirectDrawImpl_Destroy
257 * Destroys a ddraw object if all refcounts are 0. This is to share code
258 * between the IDirectDrawX::Release functions
260 * Params:
261 * This: DirectDraw object to destroy
263 *****************************************************************************/
264 void
265 IDirectDrawImpl_Destroy(IDirectDrawImpl *This)
267 /* Clear the cooplevel to restore window and display mode */
268 IDirectDraw7_SetCooperativeLevel((IDirectDraw7 *)This, NULL, DDSCL_NORMAL);
270 /* Destroy the device window if we created one */
271 if(This->devicewindow != 0)
273 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
274 DestroyWindow(This->devicewindow);
275 This->devicewindow = 0;
278 /* Unregister the window class */
279 UnregisterClassA(This->classname, 0);
281 EnterCriticalSection(&ddraw_cs);
282 list_remove(&This->ddraw_list_entry);
283 LeaveCriticalSection(&ddraw_cs);
285 /* Release the attached WineD3D stuff */
286 IWineD3DDevice_Release(This->wineD3DDevice);
287 IWineD3D_Release(This->wineD3D);
289 /* Now free the object */
290 HeapFree(GetProcessHeap(), 0, This);
293 /*****************************************************************************
294 * IDirectDraw7::Release
296 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
298 * Returns: The new refcount
299 *****************************************************************************/
300 static ULONG WINAPI
301 IDirectDrawImpl_Release(IDirectDraw7 *iface)
303 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
304 ULONG ref = InterlockedDecrement(&This->ref7);
306 TRACE("(%p)->() decrementing IDirectDraw7 refcount from %u.\n", This, ref +1);
308 if(ref == 0)
310 ULONG ifacecount = InterlockedDecrement(&This->numIfaces);
311 if(ifacecount == 0) IDirectDrawImpl_Destroy(This);
314 return ref;
317 /*****************************************************************************
318 * IDirectDraw methods
319 *****************************************************************************/
321 /*****************************************************************************
322 * IDirectDraw7::SetCooperativeLevel
324 * Sets the cooperative level for the DirectDraw object, and the window
325 * assigned to it. The cooperative level determines the general behavior
326 * of the DirectDraw application
328 * Warning: This is quite tricky, as it's not really documented which
329 * cooperative levels can be combined with each other. If a game fails
330 * after this function, try to check the cooperative levels passed on
331 * Windows, and if it returns something different.
333 * If you think that this function caused the failure because it writes a
334 * fixme, be sure to run again with a +ddraw trace.
336 * What is known about cooperative levels (See the ddraw modes test):
337 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
338 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
339 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
340 * DDSCL_FULLSCREEN can be activated
341 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
343 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
344 * DDSCL_SETFOCUSWINDOW (partially),
345 * DDSCL_MULTITHREADED (work in progress)
347 * Unhandled flags, which should be implemented
348 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
349 * expect any difference to a normal window for wine)
350 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
351 * rendering (Possible test case: Half-life)
353 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
355 * These don't seem very important for wine:
356 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
358 * Returns:
359 * DD_OK if the cooperative level was set successfully
360 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
361 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
362 * (Probably others too, have to investigate)
364 *****************************************************************************/
365 static HRESULT WINAPI
366 IDirectDrawImpl_SetCooperativeLevel(IDirectDraw7 *iface,
367 HWND hwnd,
368 DWORD cooplevel)
370 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
371 HWND window;
373 TRACE("(%p)->(%p,%08x)\n",This,hwnd,cooplevel);
374 DDRAW_dump_cooperativelevel(cooplevel);
376 EnterCriticalSection(&ddraw_cs);
378 /* Get the old window */
379 window = This->dest_window;
381 /* Tests suggest that we need one of them: */
382 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
383 DDSCL_NORMAL |
384 DDSCL_EXCLUSIVE )))
386 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
387 LeaveCriticalSection(&ddraw_cs);
388 return DDERR_INVALIDPARAMS;
391 /* Handle those levels first which set various hwnds */
392 if(cooplevel & DDSCL_SETFOCUSWINDOW)
394 /* This isn't compatible with a lot of flags */
395 if(cooplevel & ( DDSCL_MULTITHREADED |
396 DDSCL_FPUSETUP |
397 DDSCL_FPUPRESERVE |
398 DDSCL_ALLOWREBOOT |
399 DDSCL_ALLOWMODEX |
400 DDSCL_SETDEVICEWINDOW |
401 DDSCL_NORMAL |
402 DDSCL_EXCLUSIVE |
403 DDSCL_FULLSCREEN ) )
405 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
406 LeaveCriticalSection(&ddraw_cs);
407 return DDERR_INVALIDPARAMS;
409 else if( (This->cooperative_level & DDSCL_FULLSCREEN) && window)
411 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET\n");
412 LeaveCriticalSection(&ddraw_cs);
413 return DDERR_HWNDALREADYSET;
416 This->focuswindow = hwnd;
417 /* Won't use the hwnd param for anything else */
418 hwnd = NULL;
420 /* Use the focus window for drawing too */
421 This->dest_window = This->focuswindow;
423 /* Destroy the device window, if we have one */
424 if(This->devicewindow)
426 DestroyWindow(This->devicewindow);
427 This->devicewindow = NULL;
430 /* DDSCL_NORMAL or DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE */
431 if(cooplevel & DDSCL_NORMAL)
433 /* Can't coexist with fullscreen or exclusive */
434 if(cooplevel & (DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE) )
436 TRACE("(%p) DDSCL_NORMAL is not compative with DDSCL_FULLSCREEN or DDSCL_EXCLUSIVE\n", This);
437 LeaveCriticalSection(&ddraw_cs);
438 return DDERR_INVALIDPARAMS;
441 /* Switching from fullscreen? */
442 if(This->cooperative_level & DDSCL_FULLSCREEN)
444 /* Restore the display mode */
445 IDirectDraw7_RestoreDisplayMode(iface);
447 This->cooperative_level &= ~DDSCL_FULLSCREEN;
448 This->cooperative_level &= ~DDSCL_EXCLUSIVE;
449 This->cooperative_level &= ~DDSCL_ALLOWMODEX;
452 /* Don't override focus windows or private device windows */
453 if( hwnd &&
454 !(This->focuswindow) &&
455 !(This->devicewindow) &&
456 (hwnd != window) )
458 This->dest_window = hwnd;
461 else if(cooplevel & DDSCL_FULLSCREEN)
463 /* Needs DDSCL_EXCLUSIVE */
464 if(!(cooplevel & DDSCL_EXCLUSIVE) )
466 TRACE("(%p) DDSCL_FULLSCREEN needs DDSCL_EXCLUSIVE\n", This);
467 LeaveCriticalSection(&ddraw_cs);
468 return DDERR_INVALIDPARAMS;
470 /* Need a HWND
471 if(hwnd == 0)
473 TRACE("(%p) DDSCL_FULLSCREEN needs a HWND\n", This);
474 return DDERR_INVALIDPARAMS;
478 This->cooperative_level &= ~DDSCL_NORMAL;
480 /* Don't override focus windows or private device windows */
481 if( hwnd &&
482 !(This->focuswindow) &&
483 !(This->devicewindow) &&
484 (hwnd != window) )
486 This->dest_window = hwnd;
489 else if(cooplevel & DDSCL_EXCLUSIVE)
491 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN\n", This);
492 LeaveCriticalSection(&ddraw_cs);
493 return DDERR_INVALIDPARAMS;
496 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
498 /* Don't create a device window if a focus window is set */
499 if( !(This->focuswindow) )
501 HWND devicewindow = CreateWindowExA(0, This->classname, "DDraw device window",
502 WS_POPUP, 0, 0,
503 GetSystemMetrics(SM_CXSCREEN),
504 GetSystemMetrics(SM_CYSCREEN),
505 NULL, NULL, GetModuleHandleA(0), NULL);
507 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
508 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
510 This->devicewindow = devicewindow;
511 This->dest_window = devicewindow;
515 if(cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
517 /* Enable thread safety in wined3d */
518 IWineD3DDevice_SetMultithreaded(This->wineD3DDevice);
521 /* Unhandled flags */
522 if(cooplevel & DDSCL_ALLOWREBOOT)
523 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
524 if(cooplevel & DDSCL_ALLOWMODEX)
525 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
526 if(cooplevel & DDSCL_FPUSETUP)
527 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
529 /* Store the cooperative_level */
530 This->cooperative_level |= cooplevel;
531 TRACE("SetCooperativeLevel retuning DD_OK\n");
532 LeaveCriticalSection(&ddraw_cs);
533 return DD_OK;
536 /*****************************************************************************
538 * Helper function for SetDisplayMode and RestoreDisplayMode
540 * Implements DirectDraw's SetDisplayMode, but ignores the value of
541 * ForceRefreshRate, since it is already handled by
542 * IDirectDrawImpl_SetDisplayMode. RestoreDisplayMode can use this function
543 * without worrying that ForceRefreshRate will override the refresh rate. For
544 * argument and return value documentation, see
545 * IDirectDrawImpl_SetDisplayMode.
547 *****************************************************************************/
548 static HRESULT
549 IDirectDrawImpl_SetDisplayModeNoOverride(IDirectDraw7 *iface,
550 DWORD Width,
551 DWORD Height,
552 DWORD BPP,
553 DWORD RefreshRate,
554 DWORD Flags)
556 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
557 WINED3DDISPLAYMODE Mode;
558 HRESULT hr;
559 TRACE("(%p)->(%d,%d,%d,%d,%x): Relay!\n", This, Width, Height, BPP, RefreshRate, Flags);
561 EnterCriticalSection(&ddraw_cs);
562 if( !Width || !Height )
564 ERR("Width=%d, Height=%d, what to do?\n", Width, Height);
565 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
566 LeaveCriticalSection(&ddraw_cs);
567 return DD_OK;
570 /* Check the exclusive mode
571 if(!(This->cooperative_level & DDSCL_EXCLUSIVE))
572 return DDERR_NOEXCLUSIVEMODE;
573 * This is WRONG. Don't know if the SDK is completely
574 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
575 * is returned, but Half-Life 1.1.1.1 (Steam version)
576 * depends on this
579 Mode.Width = Width;
580 Mode.Height = Height;
581 Mode.RefreshRate = RefreshRate;
582 switch(BPP)
584 case 8: Mode.Format = WINED3DFMT_P8_UINT; break;
585 case 15: Mode.Format = WINED3DFMT_B5G5R5X1_UNORM; break;
586 case 16: Mode.Format = WINED3DFMT_B5G6R5_UNORM; break;
587 case 24: Mode.Format = WINED3DFMT_B8G8R8_UNORM; break;
588 case 32: Mode.Format = WINED3DFMT_B8G8R8X8_UNORM; break;
591 /* TODO: The possible return values from msdn suggest that
592 * the screen mode can't be changed if a surface is locked
593 * or some drawing is in progress
596 /* TODO: Lose the primary surface */
597 hr = IWineD3DDevice_SetDisplayMode(This->wineD3DDevice,
598 0, /* First swapchain */
599 &Mode);
600 LeaveCriticalSection(&ddraw_cs);
601 switch(hr)
603 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
604 default: return hr;
608 /*****************************************************************************
609 * IDirectDraw7::SetDisplayMode
611 * Sets the display screen resolution, color depth and refresh frequency
612 * when in fullscreen mode (in theory).
613 * Possible return values listed in the SDK suggest that this method fails
614 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
615 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
616 * It seems to be valid to pass 0 for With and Height, this has to be tested
617 * It could mean that the current video mode should be left as-is. (But why
618 * call it then?)
620 * Params:
621 * Height, Width: Screen dimension
622 * BPP: Color depth in Bits per pixel
623 * Refreshrate: Screen refresh rate
624 * Flags: Other stuff
626 * Returns
627 * DD_OK on success
629 *****************************************************************************/
630 static HRESULT WINAPI
631 IDirectDrawImpl_SetDisplayMode(IDirectDraw7 *iface,
632 DWORD Width,
633 DWORD Height,
634 DWORD BPP,
635 DWORD RefreshRate,
636 DWORD Flags)
638 if (force_refresh_rate != 0)
640 TRACE("ForceRefreshRate overriding passed-in refresh rate (%d Hz) to %d Hz\n", RefreshRate, force_refresh_rate);
641 RefreshRate = force_refresh_rate;
644 return IDirectDrawImpl_SetDisplayModeNoOverride(iface, Width, Height, BPP,
645 RefreshRate, Flags);
648 /*****************************************************************************
649 * IDirectDraw7::RestoreDisplayMode
651 * Restores the display mode to what it was at creation time. Basically.
653 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
654 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
655 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
656 * -> DD_1 is released. The screen should be left at 1024x768x32.
657 * -> DD_2 is released. The screen should be set to 1400x1050x32
658 * This case is unhandled right now, but Empire Earth does it this way.
659 * (But perhaps there is something in SetCooperativeLevel to prevent this)
661 * The msdn says that this method resets the display mode to what it was before
662 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
664 * Returns
665 * DD_OK on success
666 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
668 *****************************************************************************/
669 static HRESULT WINAPI
670 IDirectDrawImpl_RestoreDisplayMode(IDirectDraw7 *iface)
672 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
673 TRACE("(%p)\n", This);
675 return IDirectDrawImpl_SetDisplayModeNoOverride(iface,
676 This->orig_width, This->orig_height, This->orig_bpp, 0, 0);
679 /*****************************************************************************
680 * IDirectDraw7::GetCaps
682 * Returns the drives capabilities
684 * Used for version 1, 2, 4 and 7
686 * Params:
687 * DriverCaps: Structure to write the Hardware accelerated caps to
688 * HelCaps: Structure to write the emulation caps to
690 * Returns
691 * This implementation returns DD_OK only
693 *****************************************************************************/
694 static HRESULT WINAPI
695 IDirectDrawImpl_GetCaps(IDirectDraw7 *iface,
696 DDCAPS *DriverCaps,
697 DDCAPS *HELCaps)
699 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
700 DDCAPS caps;
701 WINED3DCAPS winecaps;
702 HRESULT hr;
703 DDSCAPS2 ddscaps = {0, 0, 0, 0};
704 TRACE("(%p)->(%p,%p)\n", This, DriverCaps, HELCaps);
706 /* One structure must be != NULL */
707 if( (!DriverCaps) && (!HELCaps) )
709 ERR("(%p) Invalid params to IDirectDrawImpl_GetCaps\n", This);
710 return DDERR_INVALIDPARAMS;
713 memset(&caps, 0, sizeof(caps));
714 memset(&winecaps, 0, sizeof(winecaps));
715 caps.dwSize = sizeof(caps);
716 EnterCriticalSection(&ddraw_cs);
717 hr = IWineD3DDevice_GetDeviceCaps(This->wineD3DDevice, &winecaps);
718 if(FAILED(hr)) {
719 WARN("IWineD3DDevice::GetDeviceCaps failed\n");
720 LeaveCriticalSection(&ddraw_cs);
721 return hr;
724 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
725 LeaveCriticalSection(&ddraw_cs);
726 if(FAILED(hr)) {
727 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
728 return hr;
731 caps.dwCaps = winecaps.DirectDrawCaps.Caps;
732 caps.dwCaps2 = winecaps.DirectDrawCaps.Caps2;
733 caps.dwCKeyCaps = winecaps.DirectDrawCaps.CKeyCaps;
734 caps.dwFXCaps = winecaps.DirectDrawCaps.FXCaps;
735 caps.dwPalCaps = winecaps.DirectDrawCaps.PalCaps;
736 caps.ddsCaps.dwCaps = winecaps.DirectDrawCaps.ddsCaps;
737 caps.dwSVBCaps = winecaps.DirectDrawCaps.SVBCaps;
738 caps.dwSVBCKeyCaps = winecaps.DirectDrawCaps.SVBCKeyCaps;
739 caps.dwSVBFXCaps = winecaps.DirectDrawCaps.SVBFXCaps;
740 caps.dwVSBCaps = winecaps.DirectDrawCaps.VSBCaps;
741 caps.dwVSBCKeyCaps = winecaps.DirectDrawCaps.VSBCKeyCaps;
742 caps.dwVSBFXCaps = winecaps.DirectDrawCaps.VSBFXCaps;
743 caps.dwSSBCaps = winecaps.DirectDrawCaps.SSBCaps;
744 caps.dwSSBCKeyCaps = winecaps.DirectDrawCaps.SSBCKeyCaps;
745 caps.dwSSBFXCaps = winecaps.DirectDrawCaps.SSBFXCaps;
747 /* Even if WineD3D supports 3D rendering, remove the cap if ddraw is configured
748 * not to use it
750 if(DefaultSurfaceType == SURFACE_GDI) {
751 caps.dwCaps &= ~DDCAPS_3D;
752 caps.ddsCaps.dwCaps &= ~(DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER);
754 if(winecaps.DirectDrawCaps.StrideAlign != 0) {
755 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
756 caps.dwAlignStrideAlign = winecaps.DirectDrawCaps.StrideAlign;
759 if(DriverCaps)
761 DD_STRUCT_COPY_BYSIZE(DriverCaps, &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, &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 IDirectDrawImpl *This = (IDirectDrawImpl *)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 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
819 HRESULT hr;
820 WINED3DDISPLAYMODE Mode;
821 DWORD Size;
822 TRACE("(%p)->(%p): Relay\n", This, DDSD);
824 EnterCriticalSection(&ddraw_cs);
825 /* This seems sane */
826 if (!DDSD)
828 LeaveCriticalSection(&ddraw_cs);
829 return DDERR_INVALIDPARAMS;
832 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
833 * so one method can be used for all versions (Hopefully)
835 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
836 0 /* swapchain 0 */,
837 &Mode);
838 if( hr != D3D_OK )
840 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
841 LeaveCriticalSection(&ddraw_cs);
842 return hr;
845 Size = DDSD->dwSize;
846 memset(DDSD, 0, Size);
848 DDSD->dwSize = Size;
849 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
850 DDSD->dwWidth = Mode.Width;
851 DDSD->dwHeight = Mode.Height;
852 DDSD->u2.dwRefreshRate = 60;
853 DDSD->ddsCaps.dwCaps = 0;
854 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
855 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, Mode.Format);
856 DDSD->u1.lPitch = Mode.Width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
858 if(TRACE_ON(ddraw))
860 TRACE("Returning surface desc :\n");
861 DDRAW_dump_surface_desc(DDSD);
864 LeaveCriticalSection(&ddraw_cs);
865 return DD_OK;
868 /*****************************************************************************
869 * IDirectDraw7::GetFourCCCodes
871 * Returns an array of supported FourCC codes.
873 * Exists in Version 1, 2, 4 and 7
875 * Params:
876 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
877 * of enumerated codes
878 * Codes: Pointer to an array of DWORDs where the supported codes are written
879 * to
881 * Returns
882 * Always returns DD_OK, as it's a stub for now
884 *****************************************************************************/
885 static HRESULT WINAPI
886 IDirectDrawImpl_GetFourCCCodes(IDirectDraw7 *iface,
887 DWORD *NumCodes, DWORD *Codes)
889 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
890 WINED3DFORMAT formats[] = {
891 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
892 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
893 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
895 DWORD count = 0, i, outsize;
896 HRESULT hr;
897 WINED3DDISPLAYMODE d3ddm;
898 WINED3DSURFTYPE type = This->ImplType;
899 TRACE("(%p)->(%p, %p)\n", This, NumCodes, Codes);
901 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
902 0 /* swapchain 0 */,
903 &d3ddm);
905 outsize = NumCodes && Codes ? *NumCodes : 0;
907 if(type == SURFACE_UNKNOWN) type = SURFACE_GDI;
909 for(i = 0; i < (sizeof(formats) / sizeof(formats[0])); i++) {
910 hr = IWineD3D_CheckDeviceFormat(This->wineD3D,
911 WINED3DADAPTER_DEFAULT,
912 WINED3DDEVTYPE_HAL,
913 d3ddm.Format /* AdapterFormat */,
914 0 /* usage */,
915 WINED3DRTYPE_SURFACE,
916 formats[i],
917 type);
918 if(SUCCEEDED(hr)) {
919 if(count < outsize) {
920 Codes[count] = formats[i];
922 count++;
925 if(NumCodes) {
926 TRACE("Returning %u FourCC codes\n", count);
927 *NumCodes = count;
930 return DD_OK;
933 /*****************************************************************************
934 * IDirectDraw7::GetMonitorFrequency
936 * Returns the monitor's frequency
938 * Exists in Version 1, 2, 4 and 7
940 * Params:
941 * Freq: Pointer to a DWORD to write the frequency to
943 * Returns
944 * Always returns DD_OK
946 *****************************************************************************/
947 static HRESULT WINAPI
948 IDirectDrawImpl_GetMonitorFrequency(IDirectDraw7 *iface,
949 DWORD *Freq)
951 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
952 TRACE("(%p)->(%p)\n", This, Freq);
954 /* Ideally this should be in WineD3D, as it concerns the screen setup,
955 * but for now this should make the games happy
957 *Freq = 60;
958 return DD_OK;
961 /*****************************************************************************
962 * IDirectDraw7::GetVerticalBlankStatus
964 * Returns the Vertical blank status of the monitor. This should be in WineD3D
965 * too basically, but as it's a semi stub, I didn't create a function there
967 * Params:
968 * status: Pointer to a BOOL to be filled with the vertical blank status
970 * Returns
971 * DD_OK on success
972 * DDERR_INVALIDPARAMS if status is NULL
974 *****************************************************************************/
975 static HRESULT WINAPI
976 IDirectDrawImpl_GetVerticalBlankStatus(IDirectDraw7 *iface,
977 BOOL *status)
979 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
980 TRACE("(%p)->(%p)\n", This, status);
982 /* This looks sane, the MSDN suggests it too */
983 EnterCriticalSection(&ddraw_cs);
984 if(!status)
986 LeaveCriticalSection(&ddraw_cs);
987 return DDERR_INVALIDPARAMS;
990 *status = This->fake_vblank;
991 This->fake_vblank = !This->fake_vblank;
992 LeaveCriticalSection(&ddraw_cs);
993 return DD_OK;
996 /*****************************************************************************
997 * IDirectDraw7::GetAvailableVidMem
999 * Returns the total and free video memory
1001 * Params:
1002 * Caps: Specifies the memory type asked for
1003 * total: Pointer to a DWORD to be filled with the total memory
1004 * free: Pointer to a DWORD to be filled with the free memory
1006 * Returns
1007 * DD_OK on success
1008 * DDERR_INVALIDPARAMS of free and total are NULL
1010 *****************************************************************************/
1011 static HRESULT WINAPI
1012 IDirectDrawImpl_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total, DWORD *free)
1014 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1015 TRACE("(%p)->(%p, %p, %p)\n", This, Caps, total, free);
1017 if(TRACE_ON(ddraw))
1019 TRACE("(%p) Asked for memory with description: ", This);
1020 DDRAW_dump_DDSCAPS2(Caps);
1022 EnterCriticalSection(&ddraw_cs);
1024 /* Todo: System memory vs local video memory vs non-local video memory
1025 * The MSDN also mentions differences between texture memory and other
1026 * resources, but that's not important
1029 if( (!total) && (!free) )
1031 LeaveCriticalSection(&ddraw_cs);
1032 return DDERR_INVALIDPARAMS;
1035 if(total) *total = This->total_vidmem;
1036 if(free) *free = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
1038 LeaveCriticalSection(&ddraw_cs);
1039 return DD_OK;
1042 /*****************************************************************************
1043 * IDirectDraw7::Initialize
1045 * Initializes a DirectDraw interface.
1047 * Params:
1048 * GUID: Interface identifier. Well, don't know what this is really good
1049 * for
1051 * Returns
1052 * Returns DD_OK on the first call,
1053 * DDERR_ALREADYINITIALIZED on repeated calls
1055 *****************************************************************************/
1056 static HRESULT WINAPI
1057 IDirectDrawImpl_Initialize(IDirectDraw7 *iface,
1058 GUID *Guid)
1060 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1061 TRACE("(%p)->(%s): No-op\n", This, debugstr_guid(Guid));
1063 if(This->initialized)
1065 return DDERR_ALREADYINITIALIZED;
1067 else
1069 return DD_OK;
1073 /*****************************************************************************
1074 * IDirectDraw7::FlipToGDISurface
1076 * "Makes the surface that the GDI writes to the primary surface"
1077 * Looks like some windows specific thing we don't have to care about.
1078 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1079 * show error boxes ;)
1080 * Well, just return DD_OK.
1082 * Returns:
1083 * Always returns DD_OK
1085 *****************************************************************************/
1086 static HRESULT WINAPI
1087 IDirectDrawImpl_FlipToGDISurface(IDirectDraw7 *iface)
1089 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1090 TRACE("(%p)\n", This);
1092 return DD_OK;
1095 /*****************************************************************************
1096 * IDirectDraw7::WaitForVerticalBlank
1098 * This method allows applications to get in sync with the vertical blank
1099 * interval.
1100 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1101 * redraw the screen, most likely because of this stub
1103 * Parameters:
1104 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1105 * or DDWAITVB_BLOCKEND
1106 * h: Not used, according to MSDN
1108 * Returns:
1109 * Always returns DD_OK
1111 *****************************************************************************/
1112 static HRESULT WINAPI
1113 IDirectDrawImpl_WaitForVerticalBlank(IDirectDraw7 *iface,
1114 DWORD Flags,
1115 HANDLE h)
1117 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1118 static BOOL hide = FALSE;
1120 /* This function is called often, so print the fixme only once */
1121 if(!hide)
1123 FIXME("(%p)->(%x,%p): Stub\n", This, Flags, h);
1124 hide = TRUE;
1127 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1128 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1129 return DDERR_UNSUPPORTED; /* unchecked */
1131 return DD_OK;
1134 /*****************************************************************************
1135 * IDirectDraw7::GetScanLine
1137 * Returns the scan line that is being drawn on the monitor
1139 * Parameters:
1140 * Scanline: Address to write the scan line value to
1142 * Returns:
1143 * Always returns DD_OK
1145 *****************************************************************************/
1146 static HRESULT WINAPI IDirectDrawImpl_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1148 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1149 static BOOL hide = FALSE;
1150 WINED3DDISPLAYMODE Mode;
1152 /* This function is called often, so print the fixme only once */
1153 EnterCriticalSection(&ddraw_cs);
1154 if(!hide)
1156 FIXME("(%p)->(%p): Semi-Stub\n", This, Scanline);
1157 hide = TRUE;
1160 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1162 &Mode);
1164 /* Fake the line sweeping of the monitor */
1165 /* FIXME: We should synchronize with a source to keep the refresh rate */
1166 *Scanline = This->cur_scanline++;
1167 /* Assume 20 scan lines in the vertical blank */
1168 if (This->cur_scanline >= Mode.Height + 20)
1169 This->cur_scanline = 0;
1171 LeaveCriticalSection(&ddraw_cs);
1172 return DD_OK;
1175 /*****************************************************************************
1176 * IDirectDraw7::TestCooperativeLevel
1178 * Informs the application about the state of the video adapter, depending
1179 * on the cooperative level
1181 * Returns:
1182 * DD_OK if the device is in a sane state
1183 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1184 * if the state is not correct(See below)
1186 *****************************************************************************/
1187 static HRESULT WINAPI
1188 IDirectDrawImpl_TestCooperativeLevel(IDirectDraw7 *iface)
1190 TRACE("iface %p.\n", iface);
1192 return DD_OK;
1195 /*****************************************************************************
1196 * IDirectDraw7::GetGDISurface
1198 * Returns the surface that GDI is treating as the primary surface.
1199 * For Wine this is the front buffer
1201 * Params:
1202 * GDISurface: Address to write the surface pointer to
1204 * Returns:
1205 * DD_OK if the surface was found
1206 * DDERR_NOTFOUND if the GDI surface wasn't found
1208 *****************************************************************************/
1209 static HRESULT WINAPI
1210 IDirectDrawImpl_GetGDISurface(IDirectDraw7 *iface,
1211 IDirectDrawSurface7 **GDISurface)
1213 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1214 IWineD3DSurface *Surf;
1215 IDirectDrawSurface7 *ddsurf;
1216 HRESULT hr;
1217 DDSCAPS2 ddsCaps;
1218 TRACE("(%p)->(%p)\n", This, GDISurface);
1220 /* Get the back buffer from the wineD3DDevice and search its
1221 * attached surfaces for the front buffer
1223 EnterCriticalSection(&ddraw_cs);
1224 hr = IWineD3DDevice_GetBackBuffer(This->wineD3DDevice,
1225 0, /* SwapChain */
1226 0, /* first back buffer*/
1227 WINED3DBACKBUFFER_TYPE_MONO,
1228 &Surf);
1230 if( (hr != D3D_OK) ||
1231 (!Surf) )
1233 ERR("IWineD3DDevice::GetBackBuffer failed\n");
1234 LeaveCriticalSection(&ddraw_cs);
1235 return DDERR_NOTFOUND;
1238 /* GetBackBuffer AddRef()ed the surface, release it */
1239 IWineD3DSurface_Release(Surf);
1241 IWineD3DSurface_GetParent(Surf,
1242 (IUnknown **) &ddsurf);
1243 IDirectDrawSurface7_Release(ddsurf); /* For the GetParent */
1245 /* Find the front buffer */
1246 ddsCaps.dwCaps = DDSCAPS_FRONTBUFFER;
1247 hr = IDirectDrawSurface7_GetAttachedSurface(ddsurf,
1248 &ddsCaps,
1249 GDISurface);
1250 if(hr != DD_OK)
1252 ERR("IDirectDrawSurface7::GetAttachedSurface failed, hr = %x\n", hr);
1255 /* The AddRef is OK this time */
1256 LeaveCriticalSection(&ddraw_cs);
1257 return hr;
1260 /*****************************************************************************
1261 * IDirectDraw7::EnumDisplayModes
1263 * Enumerates the supported Display modes. The modes can be filtered with
1264 * the DDSD parameter.
1266 * Params:
1267 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES
1268 * DDSD: Surface description to filter the modes
1269 * Context: Pointer passed back to the callback function
1270 * cb: Application-provided callback function
1272 * Returns:
1273 * DD_OK on success
1274 * DDERR_INVALIDPARAMS if the callback wasn't set
1276 *****************************************************************************/
1277 static HRESULT WINAPI
1278 IDirectDrawImpl_EnumDisplayModes(IDirectDraw7 *iface,
1279 DWORD Flags,
1280 DDSURFACEDESC2 *DDSD,
1281 void *Context,
1282 LPDDENUMMODESCALLBACK2 cb)
1284 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1285 unsigned int modenum, fmt;
1286 WINED3DFORMAT pixelformat = WINED3DFMT_UNKNOWN;
1287 WINED3DDISPLAYMODE mode;
1288 DDSURFACEDESC2 callback_sd;
1289 WINED3DDISPLAYMODE *enum_modes = NULL;
1290 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
1292 WINED3DFORMAT checkFormatList[] =
1294 WINED3DFMT_B8G8R8X8_UNORM,
1295 WINED3DFMT_B5G6R5_UNORM,
1296 WINED3DFMT_P8_UINT,
1299 TRACE("(%p)->(%p,%p,%p): Relay\n", This, DDSD, Context, cb);
1301 EnterCriticalSection(&ddraw_cs);
1302 /* This looks sane */
1303 if(!cb)
1305 LeaveCriticalSection(&ddraw_cs);
1306 return DDERR_INVALIDPARAMS;
1309 if(DDSD)
1311 if ((DDSD->dwFlags & DDSD_PIXELFORMAT) && (DDSD->u4.ddpfPixelFormat.dwFlags & DDPF_RGB) )
1312 pixelformat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
1315 if(!(Flags & DDEDM_REFRESHRATES))
1317 enum_mode_array_size = 16;
1318 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1319 if (!enum_modes)
1321 ERR("Out of memory\n");
1322 LeaveCriticalSection(&ddraw_cs);
1323 return DDERR_OUTOFMEMORY;
1327 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
1329 if(pixelformat != WINED3DFMT_UNKNOWN && checkFormatList[fmt] != pixelformat)
1331 continue;
1334 modenum = 0;
1335 while(IWineD3D_EnumAdapterModes(This->wineD3D,
1336 WINED3DADAPTER_DEFAULT,
1337 checkFormatList[fmt],
1338 modenum++,
1339 &mode) == WINED3D_OK)
1341 if(DDSD)
1343 if(DDSD->dwFlags & DDSD_WIDTH && mode.Width != DDSD->dwWidth) continue;
1344 if(DDSD->dwFlags & DDSD_HEIGHT && mode.Height != DDSD->dwHeight) continue;
1347 if(!(Flags & DDEDM_REFRESHRATES))
1349 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
1350 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
1351 * to be reduced to a single unique result in such case.
1353 BOOL found = FALSE;
1354 unsigned i;
1356 for (i = 0; i < enum_mode_count; i++)
1358 if(enum_modes[i].Width == mode.Width && enum_modes[i].Height == mode.Height &&
1359 enum_modes[i].Format == mode.Format)
1361 found = TRUE;
1362 break;
1366 if(found) continue;
1369 memset(&callback_sd, 0, sizeof(callback_sd));
1370 callback_sd.dwSize = sizeof(callback_sd);
1371 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1373 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH;
1374 if(Flags & DDEDM_REFRESHRATES)
1376 callback_sd.dwFlags |= DDSD_REFRESHRATE;
1377 callback_sd.u2.dwRefreshRate = mode.RefreshRate;
1380 callback_sd.dwWidth = mode.Width;
1381 callback_sd.dwHeight = mode.Height;
1383 PixelFormat_WineD3DtoDD(&callback_sd.u4.ddpfPixelFormat, mode.Format);
1385 /* Calc pitch and DWORD align like MSDN says */
1386 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.Width;
1387 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
1389 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
1390 callback_sd.u2.dwRefreshRate);
1392 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
1394 TRACE("Application asked to terminate the enumeration\n");
1395 HeapFree(GetProcessHeap(), 0, enum_modes);
1396 LeaveCriticalSection(&ddraw_cs);
1397 return DD_OK;
1400 if(!(Flags & DDEDM_REFRESHRATES))
1402 if (enum_mode_count == enum_mode_array_size)
1404 WINED3DDISPLAYMODE *new_enum_modes;
1406 enum_mode_array_size *= 2;
1407 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1409 if (!new_enum_modes)
1411 ERR("Out of memory\n");
1412 HeapFree(GetProcessHeap(), 0, enum_modes);
1413 LeaveCriticalSection(&ddraw_cs);
1414 return DDERR_OUTOFMEMORY;
1417 enum_modes = new_enum_modes;
1420 enum_modes[enum_mode_count++] = mode;
1425 TRACE("End of enumeration\n");
1426 HeapFree(GetProcessHeap(), 0, enum_modes);
1427 LeaveCriticalSection(&ddraw_cs);
1428 return DD_OK;
1431 /*****************************************************************************
1432 * IDirectDraw7::EvaluateMode
1434 * Used with IDirectDraw7::StartModeTest to test video modes.
1435 * EvaluateMode is used to pass or fail a mode, and continue with the next
1436 * mode
1438 * Params:
1439 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
1440 * Timeout: Returns the amount of seconds left before the mode would have
1441 * been failed automatically
1443 * Returns:
1444 * This implementation always DD_OK, because it's a stub
1446 *****************************************************************************/
1447 static HRESULT WINAPI
1448 IDirectDrawImpl_EvaluateMode(IDirectDraw7 *iface,
1449 DWORD Flags,
1450 DWORD *Timeout)
1452 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1453 FIXME("(%p)->(%d,%p): Stub!\n", This, Flags, Timeout);
1455 /* When implementing this, implement it in WineD3D */
1457 return DD_OK;
1460 /*****************************************************************************
1461 * IDirectDraw7::GetDeviceIdentifier
1463 * Returns the device identifier, which gives information about the driver
1464 * Our device identifier is defined at the beginning of this file.
1466 * Params:
1467 * DDDI: Address for the returned structure
1468 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
1470 * Returns:
1471 * On success it returns DD_OK
1472 * DDERR_INVALIDPARAMS if DDDI is NULL
1474 *****************************************************************************/
1475 static HRESULT WINAPI
1476 IDirectDrawImpl_GetDeviceIdentifier(IDirectDraw7 *iface,
1477 DDDEVICEIDENTIFIER2 *DDDI,
1478 DWORD Flags)
1480 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1481 TRACE("(%p)->(%p,%08x)\n", This, DDDI, Flags);
1483 if(!DDDI)
1484 return DDERR_INVALIDPARAMS;
1486 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
1487 * host adapter, if there's a secondary 3D adapter. This doesn't apply
1488 * to any modern hardware, nor is it interesting for Wine, so ignore it.
1489 * Size of DDDEVICEIDENTIFIER2 may be aligned to 8 bytes and thus 4
1490 * bytes too long. So only copy the relevant part of the structure
1493 memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
1494 return DD_OK;
1497 /*****************************************************************************
1498 * IDirectDraw7::GetSurfaceFromDC
1500 * Returns the Surface for a GDI device context handle.
1501 * Is this related to IDirectDrawSurface::GetDC ???
1503 * Params:
1504 * hdc: hdc to return the surface for
1505 * Surface: Address to write the surface pointer to
1507 * Returns:
1508 * Always returns DD_OK because it's a stub
1510 *****************************************************************************/
1511 static HRESULT WINAPI
1512 IDirectDrawImpl_GetSurfaceFromDC(IDirectDraw7 *iface,
1513 HDC hdc,
1514 IDirectDrawSurface7 **Surface)
1516 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1517 FIXME("(%p)->(%p,%p): Stub!\n", This, hdc, Surface);
1519 /* Implementation idea if needed: Loop through all surfaces and compare
1520 * their hdc with hdc. Implement it in WineD3D! */
1521 return DDERR_NOTFOUND;
1524 /*****************************************************************************
1525 * IDirectDraw7::RestoreAllSurfaces
1527 * Calls the restore method of all surfaces
1529 * Params:
1531 * Returns:
1532 * Always returns DD_OK because it's a stub
1534 *****************************************************************************/
1535 static HRESULT WINAPI
1536 IDirectDrawImpl_RestoreAllSurfaces(IDirectDraw7 *iface)
1538 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1539 FIXME("(%p): Stub\n", This);
1541 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
1542 * get their parent and call their restore method. Do not implement
1543 * it in WineD3D, as restoring a surface means re-creating the
1544 * WineD3DDSurface
1546 return DD_OK;
1549 /*****************************************************************************
1550 * IDirectDraw7::StartModeTest
1552 * Tests the specified video modes to update the system registry with
1553 * refresh rate information. StartModeTest starts the mode test,
1554 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
1555 * isn't called within 15 seconds, the mode is failed automatically
1557 * As refresh rates are handled by the X server, I don't think this
1558 * Method is important
1560 * Params:
1561 * Modes: An array of mode specifications
1562 * NumModes: The number of modes in Modes
1563 * Flags: Some flags...
1565 * Returns:
1566 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
1567 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
1568 * otherwise DD_OK
1570 *****************************************************************************/
1571 static HRESULT WINAPI
1572 IDirectDrawImpl_StartModeTest(IDirectDraw7 *iface,
1573 SIZE *Modes,
1574 DWORD NumModes,
1575 DWORD Flags)
1577 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1578 WARN("(%p)->(%p, %d, %x): Semi-Stub, most likely harmless\n", This, Modes, NumModes, Flags);
1580 /* This looks sane */
1581 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
1583 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
1584 * As it is not, DDERR_TESTFINISHED is returned
1585 * (hopefully that's correct
1587 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
1588 * well, that value doesn't (yet) exist in the wine headers, so ignore it
1591 return DD_OK;
1594 /*****************************************************************************
1595 * IDirectDrawImpl_RecreateSurfacesCallback
1597 * Enumeration callback for IDirectDrawImpl_RecreateAllSurfaces.
1598 * It re-recreates the WineD3DSurface. It's pretty straightforward
1600 *****************************************************************************/
1601 HRESULT WINAPI
1602 IDirectDrawImpl_RecreateSurfacesCallback(IDirectDrawSurface7 *surf,
1603 DDSURFACEDESC2 *desc,
1604 void *Context)
1606 IDirectDrawSurfaceImpl *surfImpl = (IDirectDrawSurfaceImpl *)surf;
1607 IDirectDrawImpl *This = surfImpl->ddraw;
1608 IUnknown *Parent;
1609 IWineD3DSurface *wineD3DSurface;
1610 IWineD3DSwapChain *swapchain;
1611 HRESULT hr;
1612 IWineD3DClipper *clipper = NULL;
1614 WINED3DSURFACE_DESC Desc;
1615 WINED3DFORMAT Format;
1616 DWORD Usage;
1617 WINED3DPOOL Pool;
1619 WINED3DMULTISAMPLE_TYPE MultiSampleType;
1620 DWORD MultiSampleQuality;
1621 UINT Width;
1622 UINT Height;
1624 TRACE("(%p): Enumerated Surface %p\n", This, surfImpl);
1626 /* For the enumeration */
1627 IDirectDrawSurface7_Release(surf);
1629 if(surfImpl->ImplType == This->ImplType) return DDENUMRET_OK; /* Continue */
1631 /* Get the objects */
1632 swapchain = surfImpl->wineD3DSwapChain;
1633 surfImpl->wineD3DSwapChain = NULL;
1634 wineD3DSurface = surfImpl->WineD3DSurface;
1636 /* get the clipper */
1637 IWineD3DSurface_GetClipper(wineD3DSurface, &clipper);
1639 /* Get the surface properties */
1640 hr = IWineD3DSurface_GetDesc(wineD3DSurface, &Desc);
1641 if(hr != D3D_OK) return hr;
1643 Format = Desc.format;
1644 Usage = Desc.usage;
1645 Pool = Desc.pool;
1646 MultiSampleType = Desc.multisample_type;
1647 MultiSampleQuality = Desc.multisample_quality;
1648 Width = Desc.width;
1649 Height = Desc.height;
1651 IWineD3DSurface_GetParent(wineD3DSurface, &Parent);
1653 /* Create the new surface */
1654 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice, Width, Height, Format,
1655 TRUE /* Lockable */, FALSE /* Discard */, surfImpl->mipmap_level, &surfImpl->WineD3DSurface, Usage, Pool,
1656 MultiSampleType, MultiSampleQuality, This->ImplType, Parent, &ddraw_null_wined3d_parent_ops);
1657 IUnknown_Release(Parent);
1659 if(hr != D3D_OK)
1660 return hr;
1662 IWineD3DSurface_SetClipper(surfImpl->WineD3DSurface, clipper);
1664 /* TODO: Copy the surface content, except for render targets */
1666 /* If there's a swapchain, it owns the wined3d surfaces. So Destroy
1667 * the swapchain
1669 if(swapchain) {
1670 /* The backbuffers have the swapchain set as well, but the primary
1671 * owns it and destroys it
1673 if(surfImpl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
1674 IWineD3DDevice_UninitGDI(This->wineD3DDevice, D3D7CB_DestroySwapChain);
1676 surfImpl->isRenderTarget = FALSE;
1677 } else {
1678 if(IWineD3DSurface_Release(wineD3DSurface) == 0)
1679 TRACE("Surface released successful, next surface\n");
1680 else
1681 ERR("Something's still holding the old WineD3DSurface\n");
1684 surfImpl->ImplType = This->ImplType;
1686 if(clipper)
1688 IWineD3DClipper_Release(clipper);
1690 return DDENUMRET_OK;
1693 /*****************************************************************************
1694 * IDirectDrawImpl_RecreateAllSurfaces
1696 * A function, that converts all wineD3DSurfaces to the new implementation type
1697 * It enumerates all surfaces with IWineD3DDevice::EnumSurfaces, creates a
1698 * new WineD3DSurface, copies the content and releases the old surface
1700 *****************************************************************************/
1701 static HRESULT
1702 IDirectDrawImpl_RecreateAllSurfaces(IDirectDrawImpl *This)
1704 DDSURFACEDESC2 desc;
1705 TRACE("(%p): Switch to implementation %d\n", This, This->ImplType);
1707 if(This->ImplType != SURFACE_OPENGL && This->d3d_initialized)
1709 /* Should happen almost never */
1710 FIXME("(%p) Switching to non-opengl surfaces with d3d started. Is this a bug?\n", This);
1711 /* Shutdown d3d */
1712 IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroySwapChain);
1714 /* Contrary: D3D starting is handled by the caller, because it knows the render target */
1716 memset(&desc, 0, sizeof(desc));
1717 desc.dwSize = sizeof(desc);
1719 return IDirectDraw7_EnumSurfaces((IDirectDraw7 *)This, 0, &desc, This, IDirectDrawImpl_RecreateSurfacesCallback);
1722 ULONG WINAPI D3D7CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
1723 IUnknown* swapChainParent;
1724 TRACE("(%p) call back\n", pSwapChain);
1726 IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
1727 IUnknown_Release(swapChainParent);
1728 return IUnknown_Release(swapChainParent);
1731 /*****************************************************************************
1732 * IDirectDrawImpl_CreateNewSurface
1734 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
1735 * with the passed parameters.
1737 * Params:
1738 * DDSD: Description of the surface to create
1739 * Surf: Address to store the interface pointer at
1741 * Returns:
1742 * DD_OK on success
1744 *****************************************************************************/
1745 static HRESULT
1746 IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This,
1747 DDSURFACEDESC2 *pDDSD,
1748 IDirectDrawSurfaceImpl **ppSurf,
1749 UINT level)
1751 HRESULT hr;
1752 UINT Width, Height;
1753 WINED3DFORMAT Format = WINED3DFMT_UNKNOWN;
1754 DWORD Usage = 0;
1755 WINED3DSURFTYPE ImplType = This->ImplType;
1756 WINED3DSURFACE_DESC Desc;
1757 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
1759 if (TRACE_ON(ddraw))
1761 TRACE(" (%p) Requesting surface desc :\n", This);
1762 DDRAW_dump_surface_desc(pDDSD);
1765 /* Select the surface type, if it wasn't choosen yet */
1766 if(ImplType == SURFACE_UNKNOWN)
1768 /* Use GL Surfaces if a D3DDEVICE Surface is requested */
1769 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1771 TRACE("(%p) Choosing GL surfaces because a 3DDEVICE Surface was requested\n", This);
1772 ImplType = SURFACE_OPENGL;
1775 /* Otherwise use GDI surfaces for now */
1776 else
1778 TRACE("(%p) Choosing GDI surfaces for 2D rendering\n", This);
1779 ImplType = SURFACE_GDI;
1782 /* Policy if all surface implementations are available:
1783 * First, check if a default type was set with winecfg. If not,
1784 * try Xrender surfaces, and use them if they work. Next, check if
1785 * accelerated OpenGL is available, and use GL surfaces in this
1786 * case. If all else fails, use GDI surfaces. If a 3DDEVICE surface
1787 * was created, always use OpenGL surfaces.
1789 * (Note: Xrender surfaces are not implemented for now, the
1790 * unaccelerated implementation uses GDI to render in Software)
1793 /* Store the type. If it needs to be changed, all WineD3DSurfaces have to
1794 * be re-created. This could be done with IDirectDrawSurface7::Restore
1796 This->ImplType = ImplType;
1798 else
1800 if ((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1801 && (This->ImplType != SURFACE_OPENGL)
1802 && DefaultSurfaceType == SURFACE_UNKNOWN)
1804 /* We have to change to OpenGL,
1805 * and re-create all WineD3DSurfaces
1807 ImplType = SURFACE_OPENGL;
1808 This->ImplType = ImplType;
1809 TRACE("(%p) Re-creating all surfaces\n", This);
1810 IDirectDrawImpl_RecreateAllSurfaces(This);
1811 TRACE("(%p) Done recreating all surfaces\n", This);
1813 else if(This->ImplType != SURFACE_OPENGL && pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1815 WARN("The application requests a 3D capable surface, but a non-opengl surface was set in the registry\n");
1816 /* Do not fail surface creation, only fail 3D device creation */
1820 if (!(pDDSD->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY)) &&
1821 !((pDDSD->ddsCaps.dwCaps & DDSCAPS_TEXTURE) && (pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)) )
1823 /* Tests show surfaces without memory flags get these flags added right after creation. */
1824 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
1826 /* Get the correct wined3d usage */
1827 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE |
1828 DDSCAPS_3DDEVICE ) )
1830 Usage |= WINED3DUSAGE_RENDERTARGET;
1832 pDDSD->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
1834 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
1836 Usage |= WINED3DUSAGE_OVERLAY;
1838 if(This->depthstencil || (pDDSD->ddsCaps.dwCaps & DDSCAPS_ZBUFFER) )
1840 /* The depth stencil creation callback sets this flag.
1841 * Set the WineD3D usage to let it know that it's a depth
1842 * Stencil surface.
1844 Usage |= WINED3DUSAGE_DEPTHSTENCIL;
1846 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
1848 Pool = WINED3DPOOL_SYSTEMMEM;
1850 else if(pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
1852 Pool = WINED3DPOOL_MANAGED;
1853 /* Managed textures have the system memory flag set */
1854 pDDSD->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
1856 else if(pDDSD->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
1858 /* Videomemory adds localvidmem, this is mutually exclusive with systemmemory
1859 * and texturemanage
1861 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
1864 Format = PixelFormat_DD2WineD3D(&pDDSD->u4.ddpfPixelFormat);
1865 if(Format == WINED3DFMT_UNKNOWN)
1867 ERR("Unsupported / Unknown pixelformat\n");
1868 return DDERR_INVALIDPIXELFORMAT;
1871 /* Create the Surface object */
1872 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
1873 if(!*ppSurf)
1875 ERR("(%p) Error allocating memory for a surface\n", This);
1876 return DDERR_OUTOFVIDEOMEMORY;
1878 (*ppSurf)->lpVtbl = &IDirectDrawSurface7_Vtbl;
1879 (*ppSurf)->IDirectDrawSurface3_vtbl = &IDirectDrawSurface3_Vtbl;
1880 (*ppSurf)->IDirectDrawGammaControl_vtbl = &IDirectDrawGammaControl_Vtbl;
1881 (*ppSurf)->IDirect3DTexture2_vtbl = &IDirect3DTexture2_Vtbl;
1882 (*ppSurf)->IDirect3DTexture_vtbl = &IDirect3DTexture1_Vtbl;
1883 (*ppSurf)->ref = 1;
1884 (*ppSurf)->version = 7;
1885 TRACE("%p->version = %d\n", (*ppSurf), (*ppSurf)->version);
1886 (*ppSurf)->ddraw = This;
1887 (*ppSurf)->surface_desc.dwSize = sizeof(DDSURFACEDESC2);
1888 (*ppSurf)->surface_desc.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1889 DD_STRUCT_COPY_BYSIZE(&(*ppSurf)->surface_desc, pDDSD);
1891 /* Surface attachments */
1892 (*ppSurf)->next_attached = NULL;
1893 (*ppSurf)->first_attached = *ppSurf;
1895 /* Needed to re-create the surface on an implementation change */
1896 (*ppSurf)->ImplType = ImplType;
1898 /* For D3DDevice creation */
1899 (*ppSurf)->isRenderTarget = FALSE;
1901 /* A trace message for debugging */
1902 TRACE("(%p) Created IDirectDrawSurface implementation structure at %p\n", This, *ppSurf);
1904 /* Now create the WineD3D Surface */
1905 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice, pDDSD->dwWidth, pDDSD->dwHeight, Format,
1906 TRUE /* Lockable */, FALSE /* Discard */, level, &(*ppSurf)->WineD3DSurface,
1907 Usage, Pool, WINED3DMULTISAMPLE_NONE, 0 /* MultiSampleQuality */, ImplType,
1908 (IUnknown *)*ppSurf, &ddraw_null_wined3d_parent_ops);
1910 if(hr != D3D_OK)
1912 ERR("IWineD3DDevice::CreateSurface failed. hr = %08x\n", hr);
1913 return hr;
1916 /* Increase the surface counter, and attach the surface */
1917 InterlockedIncrement(&This->surfaces);
1918 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
1920 /* Here we could store all created surfaces in the DirectDrawImpl structure,
1921 * But this could also be delegated to WineDDraw, as it keeps track of all its
1922 * resources. Not implemented for now, as there are more important things ;)
1925 /* Get the pixel format of the WineD3DSurface and store it.
1926 * Don't use the Format choosen above, WineD3D might have
1927 * changed it
1929 (*ppSurf)->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
1930 hr = IWineD3DSurface_GetDesc((*ppSurf)->WineD3DSurface, &Desc);
1931 if(hr != D3D_OK)
1933 ERR("IWineD3DSurface::GetDesc failed\n");
1934 IDirectDrawSurface7_Release( (IDirectDrawSurface7 *) *ppSurf);
1935 return hr;
1938 Format = Desc.format;
1939 Width = Desc.width;
1940 Height = Desc.height;
1942 if(Format == WINED3DFMT_UNKNOWN)
1944 FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN\n");
1946 PixelFormat_WineD3DtoDD( &(*ppSurf)->surface_desc.u4.ddpfPixelFormat, Format);
1948 /* Anno 1602 stores the pitch right after surface creation, so make sure it's there.
1949 * I can't LockRect() the surface here because if OpenGL surfaces are in use, the
1950 * WineD3DDevice might not be usable for 3D yet, so an extra method was created.
1951 * TODO: Test other fourcc formats
1953 if(Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3 ||
1954 Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5)
1956 (*ppSurf)->surface_desc.dwFlags |= DDSD_LINEARSIZE;
1957 if(Format == WINED3DFMT_DXT1)
1959 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height) / 2;
1961 else
1963 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height);
1966 else
1968 (*ppSurf)->surface_desc.dwFlags |= DDSD_PITCH;
1969 (*ppSurf)->surface_desc.u1.lPitch = IWineD3DSurface_GetPitch((*ppSurf)->WineD3DSurface);
1972 /* Application passed a color key? Set it! */
1973 if(pDDSD->dwFlags & DDSD_CKDESTOVERLAY)
1975 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
1976 DDCKEY_DESTOVERLAY,
1977 (WINEDDCOLORKEY *) &pDDSD->u3.ddckCKDestOverlay);
1979 if(pDDSD->dwFlags & DDSD_CKDESTBLT)
1981 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
1982 DDCKEY_DESTBLT,
1983 (WINEDDCOLORKEY *) &pDDSD->ddckCKDestBlt);
1985 if(pDDSD->dwFlags & DDSD_CKSRCOVERLAY)
1987 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
1988 DDCKEY_SRCOVERLAY,
1989 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcOverlay);
1991 if(pDDSD->dwFlags & DDSD_CKSRCBLT)
1993 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
1994 DDCKEY_SRCBLT,
1995 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcBlt);
1997 if ( pDDSD->dwFlags & DDSD_LPSURFACE)
1999 hr = IWineD3DSurface_SetMem((*ppSurf)->WineD3DSurface, pDDSD->lpSurface);
2000 if(hr != WINED3D_OK)
2002 /* No need for a trace here, wined3d does that for us */
2003 IDirectDrawSurface7_Release((IDirectDrawSurface7 *)*ppSurf);
2004 return hr;
2008 return DD_OK;
2010 /*****************************************************************************
2011 * CreateAdditionalSurfaces
2013 * Creates a new mipmap chain.
2015 * Params:
2016 * root: Root surface to attach the newly created chain to
2017 * count: number of surfaces to create
2018 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2019 * effects on the caller
2020 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2021 * creates an additional surface without the mipmapping flags
2023 *****************************************************************************/
2024 static HRESULT
2025 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2026 IDirectDrawSurfaceImpl *root,
2027 UINT count,
2028 DDSURFACEDESC2 DDSD,
2029 BOOL CubeFaceRoot)
2031 UINT i, j, level = 0;
2032 HRESULT hr;
2033 IDirectDrawSurfaceImpl *last = root;
2035 for(i = 0; i < count; i++)
2037 IDirectDrawSurfaceImpl *object2 = NULL;
2039 /* increase the mipmap level, but only if a mipmap is created
2040 * In this case, also halve the size
2042 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2044 level++;
2045 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2046 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2047 /* Set the mipmap sublevel flag according to msdn */
2048 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2050 else
2052 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2054 CubeFaceRoot = FALSE;
2056 hr = IDirectDrawImpl_CreateNewSurface(This,
2057 &DDSD,
2058 &object2,
2059 level);
2060 if(hr != DD_OK)
2062 return hr;
2065 /* Add the new surface to the complex attachment array */
2066 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2068 if(last->complex_array[j]) continue;
2069 last->complex_array[j] = object2;
2070 break;
2072 last = object2;
2074 /* Remove the (possible) back buffer cap from the new surface description,
2075 * because only one surface in the flipping chain is a back buffer, one
2076 * is a front buffer, the others are just primary surfaces.
2078 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2080 return DD_OK;
2083 /*****************************************************************************
2084 * IDirectDraw7::CreateSurface
2086 * Creates a new IDirectDrawSurface object and returns its interface.
2088 * The surface connections with wined3d are a bit tricky. Basically it works
2089 * like this:
2091 * |------------------------| |-----------------|
2092 * | DDraw surface | | WineD3DSurface |
2093 * | | | |
2094 * | WineD3DSurface |-------------->| |
2095 * | Child |<------------->| Parent |
2096 * |------------------------| |-----------------|
2098 * The DDraw surface is the parent of the wined3d surface, and it releases
2099 * the WineD3DSurface when the ddraw surface is destroyed.
2101 * However, for all surfaces which can be in a container in WineD3D,
2102 * we have to do this. These surfaces are usually complex surfaces,
2103 * so this concerns primary surfaces with a front and a back buffer,
2104 * and textures.
2106 * |------------------------| |-----------------|
2107 * | DDraw surface | | Container |
2108 * | | | |
2109 * | Child |<------------->| Parent |
2110 * | Texture |<------------->| |
2111 * | WineD3DSurface |<----| | Levels |<--|
2112 * | Complex connection | | | | |
2113 * |------------------------| | |-----------------| |
2114 * ^ | |
2115 * | | |
2116 * | | |
2117 * | |------------------| | |-----------------| |
2118 * | | IParent | |-------->| WineD3DSurface | |
2119 * | | | | | |
2120 * | | Child |<------------->| Parent | |
2121 * | | | | Container |<--|
2122 * | |------------------| |-----------------| |
2123 * | |
2124 * | |----------------------| |
2125 * | | DDraw surface 2 | |
2126 * | | | |
2127 * |<->| Complex root Child | |
2128 * | | Texture | |
2129 * | | WineD3DSurface |<----| |
2130 * | |----------------------| | |
2131 * | | |
2132 * | |---------------------| | |-----------------| |
2133 * | | IParent | |----->| WineD3DSurface | |
2134 * | | | | | |
2135 * | | Child |<---------->| Parent | |
2136 * | |---------------------| | Container |<--|
2137 * | |-----------------| |
2138 * | |
2139 * | ---More surfaces can follow--- |
2141 * The reason is that the IWineD3DSwapchain(render target container)
2142 * and the IWineD3DTexure(Texture container) release the parents
2143 * of their surface's children, but by releasing the complex root
2144 * the surfaces which are complexly attached to it are destroyed
2145 * too. See IDirectDrawSurface::Release for a more detailed
2146 * explanation.
2148 * Params:
2149 * DDSD: Description of the surface to create
2150 * Surf: Address to store the interface pointer at
2151 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2152 * aggregation, so it has to be NULL
2154 * Returns:
2155 * DD_OK on success
2156 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2157 * DDERR_* if an error occurs
2159 *****************************************************************************/
2160 static HRESULT WINAPI
2161 IDirectDrawImpl_CreateSurface(IDirectDraw7 *iface,
2162 DDSURFACEDESC2 *DDSD,
2163 IDirectDrawSurface7 **Surf,
2164 IUnknown *UnkOuter)
2166 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
2167 IDirectDrawSurfaceImpl *object = NULL;
2168 HRESULT hr;
2169 LONG extra_surfaces = 0;
2170 DDSURFACEDESC2 desc2;
2171 WINED3DDISPLAYMODE Mode;
2172 const DWORD sysvidmem = DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
2174 TRACE("(%p)->(%p,%p,%p)\n", This, DDSD, Surf, UnkOuter);
2176 /* Some checks before we start */
2177 if (TRACE_ON(ddraw))
2179 TRACE(" (%p) Requesting surface desc :\n", This);
2180 DDRAW_dump_surface_desc(DDSD);
2182 EnterCriticalSection(&ddraw_cs);
2184 if (UnkOuter != NULL)
2186 FIXME("(%p) : outer != NULL?\n", This);
2187 LeaveCriticalSection(&ddraw_cs);
2188 return CLASS_E_NOAGGREGATION; /* unchecked */
2191 if (Surf == NULL)
2193 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", This);
2194 LeaveCriticalSection(&ddraw_cs);
2195 return E_POINTER; /* unchecked */
2198 if (!(DDSD->dwFlags & DDSD_CAPS))
2200 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2201 DDSD->dwFlags |= DDSD_CAPS;
2204 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2206 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2207 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2210 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2212 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2213 WARN("(%p) Null surface pointer specified, ignore it!\n", This);
2214 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2217 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2218 !(This->cooperative_level & DDSCL_EXCLUSIVE))
2220 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n", This);
2221 *Surf = NULL;
2222 LeaveCriticalSection(&ddraw_cs);
2223 return DDERR_NOEXCLUSIVEMODE;
2226 if(DDSD->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER)) {
2227 WARN("Application tried to create an explicit front or back buffer\n");
2228 LeaveCriticalSection(&ddraw_cs);
2229 return DDERR_INVALIDCAPS;
2232 if((DDSD->ddsCaps.dwCaps & sysvidmem) == sysvidmem)
2234 /* This is a special switch in ddrawex.dll, but not allowed in ddraw.dll */
2235 WARN("Application tries to put the surface in both system and video memory\n");
2236 LeaveCriticalSection(&ddraw_cs);
2237 *Surf = NULL;
2238 return DDERR_INVALIDCAPS;
2241 /* Check cube maps but only if the size includes them */
2242 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2244 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2245 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2247 WARN("Cube map faces requested without cube map flag\n");
2248 LeaveCriticalSection(&ddraw_cs);
2249 return DDERR_INVALIDCAPS;
2251 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2252 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2254 WARN("Cube map without faces requested\n");
2255 LeaveCriticalSection(&ddraw_cs);
2256 return DDERR_INVALIDPARAMS;
2259 /* Quick tests confirm those can be created, but we don't do that yet */
2260 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2261 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2263 FIXME("Partial cube maps not supported yet\n");
2267 /* According to the msdn this flag is ignored by CreateSurface */
2268 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2269 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2271 /* Modify some flags */
2272 memset(&desc2, 0, sizeof(desc2));
2273 desc2.dwSize = sizeof(desc2); /* For the struct copy */
2274 DD_STRUCT_COPY_BYSIZE(&desc2, DDSD);
2275 desc2.dwSize = sizeof(desc2); /* To override a possibly smaller size */
2276 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2278 /* Get the video mode from WineD3D - we will need it */
2279 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
2280 0, /* Swapchain 0 */
2281 &Mode);
2282 if(FAILED(hr))
2284 ERR("Failed to read display mode from wined3d\n");
2285 switch(This->orig_bpp)
2287 case 8:
2288 Mode.Format = WINED3DFMT_P8_UINT;
2289 break;
2291 case 15:
2292 Mode.Format = WINED3DFMT_B5G5R5X1_UNORM;
2293 break;
2295 case 16:
2296 Mode.Format = WINED3DFMT_B5G6R5_UNORM;
2297 break;
2299 case 24:
2300 Mode.Format = WINED3DFMT_B8G8R8_UNORM;
2301 break;
2303 case 32:
2304 Mode.Format = WINED3DFMT_B8G8R8X8_UNORM;
2305 break;
2307 Mode.Width = This->orig_width;
2308 Mode.Height = This->orig_height;
2311 /* No pixelformat given? Use the current screen format */
2312 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2314 desc2.dwFlags |= DDSD_PIXELFORMAT;
2315 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2317 /* Wait: It could be a Z buffer */
2318 if(desc2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
2320 switch(desc2.u2.dwMipMapCount) /* Who had this glorious idea? */
2322 case 15:
2323 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_S1_UINT_D15_UNORM);
2324 break;
2325 case 16:
2326 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D16_UNORM);
2327 break;
2328 case 24:
2329 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_X8D24_UNORM);
2330 break;
2331 case 32:
2332 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D32_UNORM);
2333 break;
2334 default:
2335 ERR("Unknown Z buffer bit depth\n");
2338 else
2340 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
2344 /* No Width or no Height? Use the original screen size
2346 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2347 !(desc2.dwFlags & DDSD_HEIGHT) )
2349 /* Invalid for non-render targets */
2350 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2352 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2353 *Surf = NULL;
2354 LeaveCriticalSection(&ddraw_cs);
2355 return DDERR_INVALIDPARAMS;
2358 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2359 desc2.dwWidth = Mode.Width;
2360 desc2.dwHeight = Mode.Height;
2363 /* Mipmap count fixes */
2364 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2366 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2368 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2370 /* Mipmap count is given, should not be 0 */
2371 if( desc2.u2.dwMipMapCount == 0 )
2373 LeaveCriticalSection(&ddraw_cs);
2374 return DDERR_INVALIDPARAMS;
2377 else
2379 /* Undocumented feature: Create sublevels until
2380 * either the width or the height is 1
2382 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2383 desc2.dwWidth : desc2.dwHeight;
2384 desc2.u2.dwMipMapCount = 0;
2385 while( min )
2387 desc2.u2.dwMipMapCount += 1;
2388 min >>= 1;
2392 else
2394 /* Not-complex mipmap -> Mipmapcount = 1 */
2395 desc2.u2.dwMipMapCount = 1;
2397 extra_surfaces = desc2.u2.dwMipMapCount - 1;
2399 /* There's a mipmap count in the created surface in any case */
2400 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2402 /* If no mipmap is given, the texture has only one level */
2404 /* The first surface is a front buffer, the back buffer is created afterwards */
2405 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2407 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2410 /* The root surface in a cube map is positive x */
2411 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2413 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2414 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2417 /* Create the first surface */
2418 hr = IDirectDrawImpl_CreateNewSurface(This, &desc2, &object, 0);
2419 if( hr != DD_OK)
2421 ERR("IDirectDrawImpl_CreateNewSurface failed with %08x\n", hr);
2422 LeaveCriticalSection(&ddraw_cs);
2423 return hr;
2425 object->is_complex_root = TRUE;
2427 *Surf = (IDirectDrawSurface7 *)object;
2429 /* Create Additional surfaces if necessary
2430 * This applies to Primary surfaces which have a back buffer count
2431 * set, but not to mipmap textures. In case of Mipmap textures,
2432 * wineD3D takes care of the creation of additional surfaces
2434 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2436 extra_surfaces = DDSD->dwBackBufferCount;
2437 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2438 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2439 desc2.dwBackBufferCount = 0;
2442 hr = DD_OK;
2443 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2445 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2446 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
2447 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2448 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
2449 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
2450 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2451 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
2452 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
2453 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2454 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
2455 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
2456 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2457 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
2458 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
2459 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2460 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
2461 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2464 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces, desc2, FALSE);
2465 if(hr != DD_OK)
2467 /* This destroys and possibly created surfaces too */
2468 IDirectDrawSurface_Release((IDirectDrawSurface7 *)object);
2469 LeaveCriticalSection(&ddraw_cs);
2470 return hr;
2473 /* If the implementation is OpenGL and there's no d3ddevice, attach a d3ddevice
2474 * But attach the d3ddevice only if the currently created surface was
2475 * a primary surface (2D app in 3D mode) or a 3DDEVICE surface (3D app)
2476 * The only case I can think of where this doesn't apply is when a
2477 * 2D app was configured by the user to run with OpenGL and it didn't create
2478 * the render target as first surface. In this case the render target creation
2479 * will cause the 3D init.
2481 if( (This->ImplType == SURFACE_OPENGL) && !(This->d3d_initialized) &&
2482 desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE) )
2484 IDirectDrawSurfaceImpl *target = object, *surface;
2485 struct list *entry;
2487 /* Search for the primary to use as render target */
2488 LIST_FOR_EACH(entry, &This->surface_list)
2490 surface = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2491 if((surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER)) ==
2492 (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER))
2494 /* found */
2495 target = surface;
2496 TRACE("Using primary %p as render target\n", target);
2497 break;
2501 TRACE("(%p) Attaching a D3DDevice, rendertarget = %p\n", This, target);
2502 hr = IDirectDrawImpl_AttachD3DDevice(This, target);
2503 if(hr != D3D_OK)
2505 IDirectDrawSurfaceImpl *release_surf;
2506 ERR("IDirectDrawImpl_AttachD3DDevice failed, hr = %x\n", hr);
2507 *Surf = NULL;
2509 /* The before created surface structures are in an incomplete state here.
2510 * WineD3D holds the reference on the IParents, and it released them on the failure
2511 * already. So the regular release method implementation would fail on the attempt
2512 * to destroy either the IParents or the swapchain. So free the surface here.
2513 * The surface structure here is a list, not a tree, because onscreen targets
2514 * cannot be cube textures
2516 while(object)
2518 release_surf = object;
2519 object = object->complex_array[0];
2520 IDirectDrawSurfaceImpl_Destroy(release_surf);
2522 LeaveCriticalSection(&ddraw_cs);
2523 return hr;
2525 } else if(!(This->d3d_initialized) && desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
2526 IDirectDrawImpl_CreateGDISwapChain(This, object);
2529 /* Addref the ddraw interface to keep an reference for each surface */
2530 IDirectDraw7_AddRef(iface);
2531 object->ifaceToRelease = (IUnknown *) iface;
2533 /* Create a WineD3DTexture if a texture was requested */
2534 if(desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
2536 UINT levels;
2537 WINED3DFORMAT Format;
2538 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
2540 This->tex_root = object;
2542 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2544 /* a mipmap is created, create enough levels */
2545 levels = desc2.u2.dwMipMapCount;
2547 else
2549 /* No mipmap is created, create one level */
2550 levels = 1;
2553 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM */
2554 if(DDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
2556 Pool = WINED3DPOOL_SYSTEMMEM;
2558 /* Should I forward the MANAGED cap to the managed pool ? */
2560 /* Get the format. It's set already by CreateNewSurface */
2561 Format = PixelFormat_DD2WineD3D(&object->surface_desc.u4.ddpfPixelFormat);
2563 /* The surfaces are already created, the callback only
2564 * passes the IWineD3DSurface to WineD3D
2566 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2568 hr = IWineD3DDevice_CreateCubeTexture(This->wineD3DDevice, DDSD->dwWidth /* Edgelength */,
2569 levels, 0 /* usage */, Format, Pool, (IWineD3DCubeTexture **)&object->wineD3DTexture,
2570 (IUnknown *)object, &ddraw_null_wined3d_parent_ops);
2572 else
2574 hr = IWineD3DDevice_CreateTexture(This->wineD3DDevice, DDSD->dwWidth, DDSD->dwHeight, levels,
2575 0 /* usage */, Format, Pool, (IWineD3DTexture **)&object->wineD3DTexture,
2576 (IUnknown *)object, &ddraw_null_wined3d_parent_ops);
2578 This->tex_root = NULL;
2581 LeaveCriticalSection(&ddraw_cs);
2582 return hr;
2585 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
2586 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
2588 static BOOL
2589 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
2590 const DDPIXELFORMAT *provided)
2592 /* Some flags must be present in both or neither for a match. */
2593 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
2594 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
2595 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
2597 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2598 return FALSE;
2600 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
2601 return FALSE;
2603 if (requested->dwFlags & DDPF_FOURCC)
2604 if (requested->dwFourCC != provided->dwFourCC)
2605 return FALSE;
2607 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
2608 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2609 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
2610 return FALSE;
2612 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2613 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2614 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
2615 return FALSE;
2617 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
2618 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
2619 return FALSE;
2621 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
2622 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2623 |DDPF_BUMPDUDV))
2624 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
2625 return FALSE;
2627 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
2628 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
2629 return FALSE;
2631 return TRUE;
2634 static BOOL
2635 IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested,
2636 const DDSURFACEDESC2* provided)
2638 struct compare_info
2640 DWORD flag;
2641 ptrdiff_t offset;
2642 size_t size;
2645 #define CMP(FLAG, FIELD) \
2646 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
2647 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
2649 static const struct compare_info compare[] =
2651 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
2652 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
2653 CMP(CAPS, ddsCaps),
2654 CMP(CKDESTBLT, ddckCKDestBlt),
2655 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
2656 CMP(CKSRCBLT, ddckCKSrcBlt),
2657 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
2658 CMP(HEIGHT, dwHeight),
2659 CMP(LINEARSIZE, u1 /* dwLinearSize */),
2660 CMP(LPSURFACE, lpSurface),
2661 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
2662 CMP(PITCH, u1 /* lPitch */),
2663 /* PIXELFORMAT: manual */
2664 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
2665 CMP(TEXTURESTAGE, dwTextureStage),
2666 CMP(WIDTH, dwWidth),
2667 /* ZBUFFERBITDEPTH: "obsolete" */
2670 #undef CMP
2672 unsigned int i;
2674 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2675 return FALSE;
2677 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
2679 if (requested->dwFlags & compare[i].flag
2680 && memcmp((const char *)provided + compare[i].offset,
2681 (const char *)requested + compare[i].offset,
2682 compare[i].size) != 0)
2683 return FALSE;
2686 if (requested->dwFlags & DDSD_PIXELFORMAT)
2688 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
2689 &provided->u4.ddpfPixelFormat))
2690 return FALSE;
2693 return TRUE;
2696 #undef DDENUMSURFACES_SEARCHTYPE
2697 #undef DDENUMSURFACES_MATCHTYPE
2699 /*****************************************************************************
2700 * IDirectDraw7::EnumSurfaces
2702 * Loops through all surfaces attached to this device and calls the
2703 * application callback. This can't be relayed to WineD3DDevice,
2704 * because some WineD3DSurfaces' parents are IParent objects
2706 * Params:
2707 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
2708 * DDSD: Description to filter for
2709 * Context: Application-provided pointer, it's passed unmodified to the
2710 * Callback function
2711 * Callback: Address to call for each surface
2713 * Returns:
2714 * DDERR_INVALIDPARAMS if the callback is NULL
2715 * DD_OK on success
2717 *****************************************************************************/
2718 static HRESULT WINAPI
2719 IDirectDrawImpl_EnumSurfaces(IDirectDraw7 *iface,
2720 DWORD Flags,
2721 DDSURFACEDESC2 *DDSD,
2722 void *Context,
2723 LPDDENUMSURFACESCALLBACK7 Callback)
2725 /* The surface enumeration is handled by WineDDraw,
2726 * because it keeps track of all surfaces attached to
2727 * it. The filtering is done by our callback function,
2728 * because WineDDraw doesn't handle ddraw-like surface
2729 * caps structures
2731 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
2732 IDirectDrawSurfaceImpl *surf;
2733 BOOL all, nomatch;
2734 DDSURFACEDESC2 desc;
2735 struct list *entry, *entry2;
2737 all = Flags & DDENUMSURFACES_ALL;
2738 nomatch = Flags & DDENUMSURFACES_NOMATCH;
2740 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, DDSD, Context, Callback);
2741 EnterCriticalSection(&ddraw_cs);
2743 if(!Callback)
2745 LeaveCriticalSection(&ddraw_cs);
2746 return DDERR_INVALIDPARAMS;
2749 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
2750 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
2752 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2753 if (all || (nomatch != IDirectDrawImpl_DDSD_Match(DDSD, &surf->surface_desc)))
2755 desc = surf->surface_desc;
2756 IDirectDrawSurface7_AddRef((IDirectDrawSurface7 *)surf);
2757 if (Callback((IDirectDrawSurface7 *)surf, &desc, Context) != DDENUMRET_OK)
2759 LeaveCriticalSection(&ddraw_cs);
2760 return DD_OK;
2764 LeaveCriticalSection(&ddraw_cs);
2765 return DD_OK;
2768 static HRESULT WINAPI
2769 findRenderTarget(IDirectDrawSurface7 *surface,
2770 DDSURFACEDESC2 *desc,
2771 void *ctx)
2773 IDirectDrawSurfaceImpl *surf = (IDirectDrawSurfaceImpl *)surface;
2774 IDirectDrawSurfaceImpl **target = ctx;
2776 if(!surf->isRenderTarget) {
2777 *target = surf;
2778 IDirectDrawSurface7_Release(surface);
2779 return DDENUMRET_CANCEL;
2782 /* Recurse into the surface tree */
2783 IDirectDrawSurface7_EnumAttachedSurfaces(surface, ctx, findRenderTarget);
2785 IDirectDrawSurface7_Release(surface);
2786 if(*target) return DDENUMRET_CANCEL;
2787 else return DDENUMRET_OK; /* Continue with the next neighbor surface */
2790 static HRESULT IDirectDrawImpl_CreateGDISwapChain(IDirectDrawImpl *This,
2791 IDirectDrawSurfaceImpl *primary) {
2792 HRESULT hr;
2793 WINED3DPRESENT_PARAMETERS presentation_parameters;
2794 HWND window;
2796 window = This->dest_window;
2798 memset(&presentation_parameters, 0, sizeof(presentation_parameters));
2800 /* Use the surface description for the device parameters, not the
2801 * Device settings. The app might render to an offscreen surface
2803 presentation_parameters.BackBufferWidth = primary->surface_desc.dwWidth;
2804 presentation_parameters.BackBufferHeight = primary->surface_desc.dwHeight;
2805 presentation_parameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2806 presentation_parameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
2807 presentation_parameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
2808 presentation_parameters.MultiSampleQuality = 0;
2809 presentation_parameters.SwapEffect = WINED3DSWAPEFFECT_FLIP;
2810 presentation_parameters.hDeviceWindow = window;
2811 presentation_parameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
2812 presentation_parameters.EnableAutoDepthStencil = FALSE; /* Not on GDI swapchains */
2813 presentation_parameters.AutoDepthStencilFormat = 0;
2814 presentation_parameters.Flags = 0;
2815 presentation_parameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
2816 presentation_parameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
2818 This->d3d_target = primary;
2819 hr = IWineD3DDevice_InitGDI(This->wineD3DDevice, &presentation_parameters);
2820 This->d3d_target = NULL;
2822 if (hr != D3D_OK)
2824 FIXME("(%p) call to IWineD3DDevice_InitGDI failed\n", This);
2825 primary->wineD3DSwapChain = NULL;
2827 return hr;
2830 /*****************************************************************************
2831 * IDirectDrawImpl_AttachD3DDevice
2833 * Initializes the D3D capabilities of WineD3D
2835 * Params:
2836 * primary: The primary surface for D3D
2838 * Returns
2839 * DD_OK on success,
2840 * DDERR_* otherwise
2842 *****************************************************************************/
2843 static HRESULT
2844 IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This,
2845 IDirectDrawSurfaceImpl *primary)
2847 HRESULT hr;
2848 HWND window = This->dest_window;
2850 WINED3DPRESENT_PARAMETERS localParameters;
2852 TRACE("(%p)->(%p)\n", This, primary);
2854 /* If there's no window, create a hidden window. WineD3D needs it */
2855 if(window == 0 || window == GetDesktopWindow())
2857 window = CreateWindowExA(0, This->classname, "Hidden D3D Window",
2858 WS_DISABLED, 0, 0,
2859 GetSystemMetrics(SM_CXSCREEN),
2860 GetSystemMetrics(SM_CYSCREEN),
2861 NULL, NULL, GetModuleHandleA(0), NULL);
2863 ShowWindow(window, SW_HIDE); /* Just to be sure */
2864 WARN("(%p) No window for the Direct3DDevice, created a hidden window. HWND=%p\n", This, window);
2866 else
2868 TRACE("(%p) Using existing window %p for Direct3D rendering\n", This, window);
2870 This->d3d_window = window;
2872 /* Store the future Render Target surface */
2873 This->d3d_target = primary;
2875 /* Use the surface description for the device parameters, not the
2876 * Device settings. The app might render to an offscreen surface
2878 localParameters.BackBufferWidth = primary->surface_desc.dwWidth;
2879 localParameters.BackBufferHeight = primary->surface_desc.dwHeight;
2880 localParameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2881 localParameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
2882 localParameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
2883 localParameters.MultiSampleQuality = 0;
2884 localParameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
2885 localParameters.hDeviceWindow = window;
2886 localParameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
2887 localParameters.EnableAutoDepthStencil = TRUE;
2888 localParameters.AutoDepthStencilFormat = WINED3DFMT_D16_UNORM;
2889 localParameters.Flags = 0;
2890 localParameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
2891 localParameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
2893 TRACE("Passing mode %d\n", localParameters.BackBufferFormat);
2895 /* Set this NOW, otherwise creating the depth stencil surface will cause a
2896 * recursive loop until ram or emulated video memory is full
2898 This->d3d_initialized = TRUE;
2900 hr = IWineD3DDevice_Init3D(This->wineD3DDevice, &localParameters);
2901 if(FAILED(hr))
2903 This->d3d_target = NULL;
2904 This->d3d_initialized = FALSE;
2905 return hr;
2908 This->declArraySize = 2;
2909 This->decls = HeapAlloc(GetProcessHeap(),
2910 HEAP_ZERO_MEMORY,
2911 sizeof(*This->decls) * This->declArraySize);
2912 if(!This->decls)
2914 ERR("Error allocating an array for the converted vertex decls\n");
2915 This->declArraySize = 0;
2916 hr = IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroySwapChain);
2917 return E_OUTOFMEMORY;
2920 /* Create an Index Buffer parent */
2921 TRACE("(%p) Successfully initialized 3D\n", This);
2922 return DD_OK;
2925 /*****************************************************************************
2926 * DirectDrawCreateClipper (DDRAW.@)
2928 * Creates a new IDirectDrawClipper object.
2930 * Params:
2931 * Clipper: Address to write the interface pointer to
2932 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
2933 * NULL
2935 * Returns:
2936 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2937 * E_OUTOFMEMORY if allocating the object failed
2939 *****************************************************************************/
2940 HRESULT WINAPI
2941 DirectDrawCreateClipper(DWORD Flags,
2942 LPDIRECTDRAWCLIPPER *Clipper,
2943 IUnknown *UnkOuter)
2945 IDirectDrawClipperImpl* object;
2946 TRACE("(%08x,%p,%p)\n", Flags, Clipper, UnkOuter);
2948 EnterCriticalSection(&ddraw_cs);
2949 if (UnkOuter != NULL)
2951 LeaveCriticalSection(&ddraw_cs);
2952 return CLASS_E_NOAGGREGATION;
2955 if (!LoadWineD3D())
2957 LeaveCriticalSection(&ddraw_cs);
2958 return DDERR_NODIRECTDRAWSUPPORT;
2961 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2962 sizeof(IDirectDrawClipperImpl));
2963 if (object == NULL)
2965 LeaveCriticalSection(&ddraw_cs);
2966 return E_OUTOFMEMORY;
2969 object->lpVtbl = &IDirectDrawClipper_Vtbl;
2970 object->ref = 1;
2971 object->wineD3DClipper = pWineDirect3DCreateClipper((IUnknown *) object);
2972 if(!object->wineD3DClipper)
2974 HeapFree(GetProcessHeap(), 0, object);
2975 LeaveCriticalSection(&ddraw_cs);
2976 return E_OUTOFMEMORY;
2979 *Clipper = (IDirectDrawClipper *) object;
2980 LeaveCriticalSection(&ddraw_cs);
2981 return DD_OK;
2984 /*****************************************************************************
2985 * IDirectDraw7::CreateClipper
2987 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
2989 *****************************************************************************/
2990 static HRESULT WINAPI
2991 IDirectDrawImpl_CreateClipper(IDirectDraw7 *iface,
2992 DWORD Flags,
2993 IDirectDrawClipper **Clipper,
2994 IUnknown *UnkOuter)
2996 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
2997 TRACE("(%p)->(%x,%p,%p)\n", This, Flags, Clipper, UnkOuter);
2998 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3001 /*****************************************************************************
3002 * IDirectDraw7::CreatePalette
3004 * Creates a new IDirectDrawPalette object
3006 * Params:
3007 * Flags: The flags for the new clipper
3008 * ColorTable: Color table to assign to the new clipper
3009 * Palette: Address to write the interface pointer to
3010 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3011 * NULL
3013 * Returns:
3014 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3015 * E_OUTOFMEMORY if allocating the object failed
3017 *****************************************************************************/
3018 static HRESULT WINAPI
3019 IDirectDrawImpl_CreatePalette(IDirectDraw7 *iface,
3020 DWORD Flags,
3021 PALETTEENTRY *ColorTable,
3022 IDirectDrawPalette **Palette,
3023 IUnknown *pUnkOuter)
3025 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
3026 IDirectDrawPaletteImpl *object;
3027 HRESULT hr = DDERR_GENERIC;
3028 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, ColorTable, Palette, pUnkOuter);
3030 EnterCriticalSection(&ddraw_cs);
3031 if(pUnkOuter != NULL)
3033 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
3034 LeaveCriticalSection(&ddraw_cs);
3035 return CLASS_E_NOAGGREGATION;
3038 /* The refcount test shows that a cooplevel is required for this */
3039 if(!This->cooperative_level)
3041 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3042 LeaveCriticalSection(&ddraw_cs);
3043 return DDERR_NOCOOPERATIVELEVELSET;
3046 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
3047 if(!object)
3049 ERR("Out of memory when allocating memory for a palette implementation\n");
3050 LeaveCriticalSection(&ddraw_cs);
3051 return E_OUTOFMEMORY;
3054 object->lpVtbl = &IDirectDrawPalette_Vtbl;
3055 object->ref = 1;
3056 object->ddraw_owner = This;
3058 hr = IWineD3DDevice_CreatePalette(This->wineD3DDevice, Flags,
3059 ColorTable, &object->wineD3DPalette, (IUnknown *)object);
3060 if(hr != DD_OK)
3062 HeapFree(GetProcessHeap(), 0, object);
3063 LeaveCriticalSection(&ddraw_cs);
3064 return hr;
3067 IDirectDraw7_AddRef(iface);
3068 object->ifaceToRelease = (IUnknown *) iface;
3069 *Palette = (IDirectDrawPalette *)object;
3070 LeaveCriticalSection(&ddraw_cs);
3071 return DD_OK;
3074 /*****************************************************************************
3075 * IDirectDraw7::DuplicateSurface
3077 * Duplicates a surface. The surface memory points to the same memory as
3078 * the original surface, and it's released when the last surface referencing
3079 * it is released. I guess that's beyond Wine's surface management right now
3080 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3081 * test application to implement this)
3083 * Params:
3084 * Src: Address of the source surface
3085 * Dest: Address to write the new surface pointer to
3087 * Returns:
3088 * See IDirectDraw7::CreateSurface
3090 *****************************************************************************/
3091 static HRESULT WINAPI
3092 IDirectDrawImpl_DuplicateSurface(IDirectDraw7 *iface,
3093 IDirectDrawSurface7 *Src,
3094 IDirectDrawSurface7 **Dest)
3096 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
3097 IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Src;
3099 FIXME("(%p)->(%p,%p)\n", This, Surf, Dest);
3101 /* For now, simply create a new, independent surface */
3102 return IDirectDraw7_CreateSurface(iface,
3103 &Surf->surface_desc,
3104 Dest,
3105 NULL);
3108 /*****************************************************************************
3109 * IDirectDraw7 VTable
3110 *****************************************************************************/
3111 const IDirectDraw7Vtbl IDirectDraw7_Vtbl =
3113 /*** IUnknown ***/
3114 IDirectDrawImpl_QueryInterface,
3115 IDirectDrawImpl_AddRef,
3116 IDirectDrawImpl_Release,
3117 /*** IDirectDraw ***/
3118 IDirectDrawImpl_Compact,
3119 IDirectDrawImpl_CreateClipper,
3120 IDirectDrawImpl_CreatePalette,
3121 IDirectDrawImpl_CreateSurface,
3122 IDirectDrawImpl_DuplicateSurface,
3123 IDirectDrawImpl_EnumDisplayModes,
3124 IDirectDrawImpl_EnumSurfaces,
3125 IDirectDrawImpl_FlipToGDISurface,
3126 IDirectDrawImpl_GetCaps,
3127 IDirectDrawImpl_GetDisplayMode,
3128 IDirectDrawImpl_GetFourCCCodes,
3129 IDirectDrawImpl_GetGDISurface,
3130 IDirectDrawImpl_GetMonitorFrequency,
3131 IDirectDrawImpl_GetScanLine,
3132 IDirectDrawImpl_GetVerticalBlankStatus,
3133 IDirectDrawImpl_Initialize,
3134 IDirectDrawImpl_RestoreDisplayMode,
3135 IDirectDrawImpl_SetCooperativeLevel,
3136 IDirectDrawImpl_SetDisplayMode,
3137 IDirectDrawImpl_WaitForVerticalBlank,
3138 /*** IDirectDraw2 ***/
3139 IDirectDrawImpl_GetAvailableVidMem,
3140 /*** IDirectDraw3 ***/
3141 IDirectDrawImpl_GetSurfaceFromDC,
3142 /*** IDirectDraw4 ***/
3143 IDirectDrawImpl_RestoreAllSurfaces,
3144 IDirectDrawImpl_TestCooperativeLevel,
3145 IDirectDrawImpl_GetDeviceIdentifier,
3146 /*** IDirectDraw7 ***/
3147 IDirectDrawImpl_StartModeTest,
3148 IDirectDrawImpl_EvaluateMode
3151 /*****************************************************************************
3152 * IDirectDrawImpl_FindDecl
3154 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
3155 * if none was found.
3157 * This function is in ddraw.c and the DDraw object space because D3D7
3158 * vertex buffers are created using the IDirect3D interface to the ddraw
3159 * object, so they can be valid across D3D devices(theoretically. The ddraw
3160 * object also owns the wined3d device
3162 * Parameters:
3163 * This: Device
3164 * fvf: Fvf to find the decl for
3166 * Returns:
3167 * NULL in case of an error, the IWineD3DVertexDeclaration interface for the
3168 * fvf otherwise.
3170 *****************************************************************************/
3171 IWineD3DVertexDeclaration *
3172 IDirectDrawImpl_FindDecl(IDirectDrawImpl *This,
3173 DWORD fvf)
3175 HRESULT hr;
3176 IWineD3DVertexDeclaration* pDecl = NULL;
3177 int p, low, high; /* deliberately signed */
3178 struct FvfToDecl *convertedDecls = This->decls;
3180 TRACE("Searching for declaration for fvf %08x... ", fvf);
3182 low = 0;
3183 high = This->numConvertedDecls - 1;
3184 while(low <= high) {
3185 p = (low + high) >> 1;
3186 TRACE("%d ", p);
3187 if(convertedDecls[p].fvf == fvf) {
3188 TRACE("found %p\n", convertedDecls[p].decl);
3189 return convertedDecls[p].decl;
3190 } else if(convertedDecls[p].fvf < fvf) {
3191 low = p + 1;
3192 } else {
3193 high = p - 1;
3196 TRACE("not found. Creating and inserting at position %d.\n", low);
3198 hr = IWineD3DDevice_CreateVertexDeclarationFromFVF(This->wineD3DDevice, &pDecl,
3199 (IUnknown *)This, &ddraw_null_wined3d_parent_ops, fvf);
3200 if (hr != S_OK) return NULL;
3202 if(This->declArraySize == This->numConvertedDecls) {
3203 int grow = max(This->declArraySize / 2, 8);
3204 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
3205 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
3206 if(!convertedDecls) {
3207 /* This will destroy it */
3208 IWineD3DVertexDeclaration_Release(pDecl);
3209 return NULL;
3211 This->decls = convertedDecls;
3212 This->declArraySize += grow;
3215 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
3216 convertedDecls[low].decl = pDecl;
3217 convertedDecls[low].fvf = fvf;
3218 This->numConvertedDecls++;
3220 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
3221 return pDecl;
3224 /* IWineD3DDeviceParent IUnknown methods */
3226 static inline struct IDirectDrawImpl *ddraw_from_device_parent(IWineD3DDeviceParent *iface)
3228 return (struct IDirectDrawImpl *)((char*)iface - FIELD_OFFSET(struct IDirectDrawImpl, device_parent_vtbl));
3231 static HRESULT STDMETHODCALLTYPE device_parent_QueryInterface(IWineD3DDeviceParent *iface, REFIID riid, void **object)
3233 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3234 return IDirectDrawImpl_QueryInterface((IDirectDraw7 *)This, riid, object);
3237 static ULONG STDMETHODCALLTYPE device_parent_AddRef(IWineD3DDeviceParent *iface)
3239 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3240 return IDirectDrawImpl_AddRef((IDirectDraw7 *)This);
3243 static ULONG STDMETHODCALLTYPE device_parent_Release(IWineD3DDeviceParent *iface)
3245 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3246 return IDirectDrawImpl_Release((IDirectDraw7 *)This);
3249 /* IWineD3DDeviceParent methods */
3251 static void STDMETHODCALLTYPE device_parent_WineD3DDeviceCreated(IWineD3DDeviceParent *iface, IWineD3DDevice *device)
3253 TRACE("iface %p, device %p\n", iface, device);
3256 static HRESULT STDMETHODCALLTYPE device_parent_CreateSurface(IWineD3DDeviceParent *iface,
3257 IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, DWORD usage,
3258 WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, IWineD3DSurface **surface)
3260 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3261 IDirectDrawSurfaceImpl *surf = NULL;
3262 UINT i = 0;
3263 DDSCAPS2 searchcaps = This->tex_root->surface_desc.ddsCaps;
3265 TRACE("iface %p, superior %p, width %u, height %u, format %#x, usage %#x,\n"
3266 "\tpool %#x, level %u, face %u, surface %p\n",
3267 iface, superior, width, height, format, usage, pool, level, face, surface);
3269 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
3270 switch(face)
3272 case WINED3DCUBEMAP_FACE_POSITIVE_X:
3273 TRACE("Asked for positive x\n");
3274 if (searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP)
3276 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
3278 surf = This->tex_root; break;
3279 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
3280 TRACE("Asked for negative x\n");
3281 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
3282 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
3283 TRACE("Asked for positive y\n");
3284 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
3285 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
3286 TRACE("Asked for negative y\n");
3287 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
3288 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
3289 TRACE("Asked for positive z\n");
3290 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
3291 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
3292 TRACE("Asked for negative z\n");
3293 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
3294 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
3297 if (!surf)
3299 IDirectDrawSurface7 *attached;
3300 IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)This->tex_root, &searchcaps, &attached);
3301 surf = (IDirectDrawSurfaceImpl *)attached;
3302 IDirectDrawSurface7_Release(attached);
3304 if (!surf) ERR("root search surface not found\n");
3306 /* Find the wanted mipmap. There are enough mipmaps in the chain */
3307 while (i < level)
3309 IDirectDrawSurface7 *attached;
3310 IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)surf, &searchcaps, &attached);
3311 if(!attached) ERR("Surface not found\n");
3312 surf = (IDirectDrawSurfaceImpl *)attached;
3313 IDirectDrawSurface7_Release(attached);
3314 ++i;
3317 /* Return the surface */
3318 *surface = surf->WineD3DSurface;
3319 IWineD3DSurface_AddRef(*surface);
3321 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, surf);
3323 return D3D_OK;
3326 static HRESULT STDMETHODCALLTYPE device_parent_CreateRenderTarget(IWineD3DDeviceParent *iface,
3327 IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
3328 DWORD multisample_quality, BOOL lockable, IWineD3DSurface **surface)
3330 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3331 IDirectDrawSurfaceImpl *d3d_surface = This->d3d_target;
3332 IDirectDrawSurfaceImpl *target = NULL;
3334 TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3335 "\tmultisample_quality %u, lockable %u, surface %p\n",
3336 iface, superior, width, height, format, multisample_type, multisample_quality, lockable, surface);
3338 if (d3d_surface->isRenderTarget)
3340 IDirectDrawSurface7_EnumAttachedSurfaces((IDirectDrawSurface7 *)d3d_surface, &target, findRenderTarget);
3342 else
3344 target = d3d_surface;
3347 if (!target)
3349 target = This->d3d_target;
3350 ERR(" (%p) : No DirectDrawSurface found to create the back buffer. Using the front buffer as back buffer. Uncertain consequences\n", This);
3353 /* TODO: Return failure if the dimensions do not match, but this shouldn't happen */
3355 *surface = target->WineD3DSurface;
3356 IWineD3DSurface_AddRef(*surface);
3357 target->isRenderTarget = TRUE;
3359 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, d3d_surface);
3361 return D3D_OK;
3364 static HRESULT STDMETHODCALLTYPE device_parent_CreateDepthStencilSurface(IWineD3DDeviceParent *iface,
3365 IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
3366 DWORD multisample_quality, BOOL discard, IWineD3DSurface **surface)
3368 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3369 IDirectDrawSurfaceImpl *ddraw_surface;
3370 DDSURFACEDESC2 ddsd;
3371 HRESULT hr;
3373 TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3374 "\tmultisample_quality %u, discard %u, surface %p\n",
3375 iface, superior, width, height, format, multisample_type, multisample_quality, discard, surface);
3377 *surface = NULL;
3379 /* Create a DirectDraw surface */
3380 memset(&ddsd, 0, sizeof(ddsd));
3381 ddsd.dwSize = sizeof(ddsd);
3382 ddsd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
3383 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
3384 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
3385 ddsd.dwHeight = height;
3386 ddsd.dwWidth = width;
3387 if (format)
3389 PixelFormat_WineD3DtoDD(&ddsd.u4.ddpfPixelFormat, format);
3391 else
3393 ddsd.dwFlags ^= DDSD_PIXELFORMAT;
3396 This->depthstencil = TRUE;
3397 hr = IDirectDraw7_CreateSurface((IDirectDraw7 *)This, &ddsd, (IDirectDrawSurface7 **)&ddraw_surface, NULL);
3398 This->depthstencil = FALSE;
3399 if(FAILED(hr))
3401 ERR(" (%p) Creating a DepthStencil Surface failed, result = %x\n", This, hr);
3402 return hr;
3405 *surface = ddraw_surface->WineD3DSurface;
3406 IWineD3DSurface_AddRef(*surface);
3407 IDirectDrawSurface7_Release((IDirectDrawSurface7 *)ddraw_surface);
3409 return D3D_OK;
3412 static HRESULT STDMETHODCALLTYPE device_parent_CreateVolume(IWineD3DDeviceParent *iface,
3413 IUnknown *superior, UINT width, UINT height, UINT depth, WINED3DFORMAT format,
3414 WINED3DPOOL pool, DWORD usage, IWineD3DVolume **volume)
3416 TRACE("iface %p, superior %p, width %u, height %u, depth %u, format %#x, pool %#x, usage %#x, volume %p\n",
3417 iface, superior, width, height, depth, format, pool, usage, volume);
3419 ERR("Not implemented!\n");
3421 return E_NOTIMPL;
3424 static HRESULT STDMETHODCALLTYPE device_parent_CreateSwapChain(IWineD3DDeviceParent *iface,
3425 WINED3DPRESENT_PARAMETERS *present_parameters, IWineD3DSwapChain **swapchain)
3427 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3428 IDirectDrawSurfaceImpl *iterator;
3429 IParentImpl *object;
3430 HRESULT hr;
3432 TRACE("iface %p, present_parameters %p, swapchain %p\n", iface, present_parameters, swapchain);
3434 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IParentImpl));
3435 if (!object)
3437 FIXME("Allocation of memory failed\n");
3438 *swapchain = NULL;
3439 return DDERR_OUTOFVIDEOMEMORY;
3442 object->lpVtbl = &IParent_Vtbl;
3443 object->ref = 1;
3445 hr = IWineD3DDevice_CreateSwapChain(This->wineD3DDevice, present_parameters,
3446 swapchain, (IUnknown *)object, This->ImplType);
3447 if (FAILED(hr))
3449 FIXME("(%p) CreateSwapChain failed, returning %#x\n", iface, hr);
3450 HeapFree(GetProcessHeap(), 0 , object);
3451 *swapchain = NULL;
3452 return hr;
3455 object->child = (IUnknown *)*swapchain;
3456 This->d3d_target->wineD3DSwapChain = *swapchain;
3457 iterator = This->d3d_target->complex_array[0];
3458 while (iterator)
3460 iterator->wineD3DSwapChain = *swapchain;
3461 iterator = iterator->complex_array[0];
3464 return hr;
3467 const IWineD3DDeviceParentVtbl ddraw_wined3d_device_parent_vtbl =
3469 /* IUnknown methods */
3470 device_parent_QueryInterface,
3471 device_parent_AddRef,
3472 device_parent_Release,
3473 /* IWineD3DDeviceParent methods */
3474 device_parent_WineD3DDeviceCreated,
3475 device_parent_CreateSurface,
3476 device_parent_CreateRenderTarget,
3477 device_parent_CreateDepthStencilSurface,
3478 device_parent_CreateVolume,
3479 device_parent_CreateSwapChain,