ddraw: Hack to draw to the desktop window.
[wine/hacks.git] / dlls / ddraw / ddraw.c
blobe4d9d358372d36fcb610f650b0bc80af183fba85
1 /*
2 * Copyright 1997-2000 Marcus Meissner
3 * Copyright 1998-2000 Lionel Ulmer
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2006 Stefan Dösinger
6 * Copyright 2008 Denver Gingerich
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <stdlib.h>
31 #define COBJMACROS
32 #define NONAMELESSUNION
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "wingdi.h"
38 #include "winreg.h"
39 #include "wine/exception.h"
41 #include "ddraw.h"
42 #include "d3d.h"
44 #include "ddraw_private.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
49 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
51 static BOOL IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested, const DDSURFACEDESC2* provided);
52 static HRESULT IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This, IDirectDrawSurfaceImpl *primary);
53 static HRESULT IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This, DDSURFACEDESC2 *pDDSD, IDirectDrawSurfaceImpl **ppSurf, UINT level);
54 static HRESULT IDirectDrawImpl_CreateGDISwapChain(IDirectDrawImpl *This, IDirectDrawSurfaceImpl *primary);
56 /* Device identifier. Don't relay it to WineD3D */
57 static const DDDEVICEIDENTIFIER2 deviceidentifier =
59 "display",
60 "DirectDraw HAL",
61 { { 0x00010001, 0x00010001 } },
62 0, 0, 0, 0,
63 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
64 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
68 static void STDMETHODCALLTYPE ddraw_null_wined3d_object_destroyed(void *parent) {}
70 const struct wined3d_parent_ops ddraw_null_wined3d_parent_ops =
72 ddraw_null_wined3d_object_destroyed,
75 /*****************************************************************************
76 * IUnknown Methods
77 *****************************************************************************/
79 /*****************************************************************************
80 * IDirectDraw7::QueryInterface
82 * Queries different interfaces of the DirectDraw object. It can return
83 * IDirectDraw interfaces in version 1, 2, 4 and 7, and IDirect3D interfaces
84 * in version 1, 2, 3 and 7. An IDirect3DDevice can be created with this
85 * method.
86 * The returned interface is AddRef()-ed before it's returned
88 * Used for version 1, 2, 4 and 7
90 * Params:
91 * refiid: Interface ID asked for
92 * obj: Used to return the interface pointer
94 * Returns:
95 * S_OK if an interface was found
96 * E_NOINTERFACE if the requested interface wasn't found
98 *****************************************************************************/
99 static HRESULT WINAPI
100 IDirectDrawImpl_QueryInterface(IDirectDraw7 *iface,
101 REFIID refiid,
102 void **obj)
104 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
106 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(refiid), obj);
108 /* Can change surface impl type */
109 EnterCriticalSection(&ddraw_cs);
111 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
112 *obj = NULL;
114 if(!refiid)
116 LeaveCriticalSection(&ddraw_cs);
117 return DDERR_INVALIDPARAMS;
120 /* Check DirectDraw Interfaces */
121 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
122 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
124 *obj = This;
125 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
127 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
129 *obj = &This->IDirectDraw4_vtbl;
130 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
132 else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
134 /* This Interface exists in ddrawex.dll, it is implemented in a wrapper */
135 WARN("IDirectDraw3 is not valid in ddraw.dll\n");
136 *obj = NULL;
137 LeaveCriticalSection(&ddraw_cs);
138 return E_NOINTERFACE;
140 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
142 *obj = &This->IDirectDraw2_vtbl;
143 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
145 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
147 *obj = &This->IDirectDraw_vtbl;
148 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
151 /* Direct3D
152 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
153 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
154 * who had this idea and why. The older interfaces can query and IDirect3D version
155 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
156 * and messy to implement with the common creation function, so it has been left out here.
158 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
159 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
160 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
161 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
163 /* Check the surface implementation */
164 if(This->ImplType == SURFACE_UNKNOWN)
166 /* Apps may create the IDirect3D Interface before the primary surface.
167 * set the surface implementation */
168 This->ImplType = SURFACE_OPENGL;
169 TRACE("(%p) Choosing OpenGL surfaces because a Direct3D interface was requested\n", This);
171 else if(This->ImplType != SURFACE_OPENGL && DefaultSurfaceType == SURFACE_UNKNOWN)
173 ERR("(%p) The App is requesting a D3D device, but a non-OpenGL surface type was choosen. Prepare for trouble!\n", This);
174 ERR(" (%p) You may want to contact wine-devel for help\n", This);
175 /* Should I assert(0) here??? */
177 else if(This->ImplType != SURFACE_OPENGL)
179 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
180 /* Do not abort here, only reject 3D Device creation */
183 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
185 This->d3dversion = 1;
186 *obj = &This->IDirect3D_vtbl;
187 TRACE(" returning Direct3D interface at %p.\n", *obj);
189 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
191 This->d3dversion = 2;
192 *obj = &This->IDirect3D2_vtbl;
193 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
195 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
197 This->d3dversion = 3;
198 *obj = &This->IDirect3D3_vtbl;
199 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
201 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
203 This->d3dversion = 7;
204 *obj = &This->IDirect3D7_vtbl;
205 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
208 else if (IsEqualGUID(refiid, &IID_IWineD3DDeviceParent))
210 *obj = &This->device_parent_vtbl;
213 /* Unknown interface */
214 else
216 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
217 LeaveCriticalSection(&ddraw_cs);
218 return E_NOINTERFACE;
221 IUnknown_AddRef( (IUnknown *) *obj );
222 LeaveCriticalSection(&ddraw_cs);
223 return S_OK;
226 /*****************************************************************************
227 * IDirectDraw7::AddRef
229 * Increases the interfaces refcount, basically
231 * DDraw refcounting is a bit tricky. The different DirectDraw interface
232 * versions have individual refcounts, but the IDirect3D interfaces do not.
233 * All interfaces are from one object, that means calling QueryInterface on an
234 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
235 * IDirectDrawImpl object.
237 * That means all AddRef and Release implementations of IDirectDrawX work
238 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
239 * except of IDirect3D7 which thunks to IDirectDraw7
241 * Returns: The new refcount
243 *****************************************************************************/
244 static ULONG WINAPI
245 IDirectDrawImpl_AddRef(IDirectDraw7 *iface)
247 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
248 ULONG ref = InterlockedIncrement(&This->ref7);
250 TRACE("(%p) : incrementing IDirectDraw7 refcount from %u.\n", This, ref -1);
252 if(ref == 1) InterlockedIncrement(&This->numIfaces);
254 return ref;
257 /*****************************************************************************
258 * IDirectDrawImpl_Destroy
260 * Destroys a ddraw object if all refcounts are 0. This is to share code
261 * between the IDirectDrawX::Release functions
263 * Params:
264 * This: DirectDraw object to destroy
266 *****************************************************************************/
267 void
268 IDirectDrawImpl_Destroy(IDirectDrawImpl *This)
270 IDirectDraw7_SetCooperativeLevel((IDirectDraw7 *)This, NULL, DDSCL_NORMAL);
271 IDirectDraw7_RestoreDisplayMode((IDirectDraw7 *)This);
273 /* Destroy the device window if we created one */
274 if(This->devicewindow != 0)
276 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
277 DestroyWindow(This->devicewindow);
278 This->devicewindow = 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 This->cooperative_level &= ~DDSCL_FULLSCREEN;
445 This->cooperative_level &= ~DDSCL_EXCLUSIVE;
446 This->cooperative_level &= ~DDSCL_ALLOWMODEX;
448 IWineD3DDevice_ReleaseFocusWindow(This->wineD3DDevice);
451 /* Don't override focus windows or private device windows */
452 if( hwnd &&
453 !(This->focuswindow) &&
454 !(This->devicewindow) &&
455 (hwnd != window) )
457 This->dest_window = hwnd;
460 else if(cooplevel & DDSCL_FULLSCREEN)
462 /* Needs DDSCL_EXCLUSIVE */
463 if(!(cooplevel & DDSCL_EXCLUSIVE) )
465 TRACE("(%p) DDSCL_FULLSCREEN needs DDSCL_EXCLUSIVE\n", This);
466 LeaveCriticalSection(&ddraw_cs);
467 return DDERR_INVALIDPARAMS;
469 /* Need a HWND
470 if(hwnd == 0)
472 TRACE("(%p) DDSCL_FULLSCREEN needs a HWND\n", This);
473 return DDERR_INVALIDPARAMS;
477 This->cooperative_level &= ~DDSCL_NORMAL;
479 /* Don't override focus windows or private device windows */
480 if( hwnd &&
481 !(This->focuswindow) &&
482 !(This->devicewindow) &&
483 (hwnd != window) )
485 BYTE buffer[32];
486 DWORD size = sizeof(buffer);
487 HKEY hkey = 0;
488 HWND drawwin = hwnd;
489 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
490 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey)) {
491 if (!RegQueryValueExA( hkey, "DirectDrawDesktopHack", 0, NULL, buffer, &size)) {
492 if ( IS_OPTION_TRUE( buffer[0] ) ) {
493 TRACE("Enabling DirectDrawDesktopHack hack\n");
494 drawwin = GetDesktopWindow();
497 } else {
498 HRESULT hr = IWineD3DDevice_AcquireFocusWindow(This->wineD3DDevice, hwnd);
499 if (FAILED(hr))
501 ERR("Failed to acquire focus window, hr %#x.\n", hr);
502 LeaveCriticalSection(&ddraw_cs);
503 return hr;
506 This->dest_window = drawwin;
509 else if(cooplevel & DDSCL_EXCLUSIVE)
511 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN\n", This);
512 LeaveCriticalSection(&ddraw_cs);
513 return DDERR_INVALIDPARAMS;
516 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
518 /* Don't create a device window if a focus window is set */
519 if( !(This->focuswindow) )
521 HWND devicewindow = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "DDraw device window",
522 WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
523 NULL, NULL, NULL, NULL);
524 if (!devicewindow)
526 ERR("Failed to create window, last error %#x.\n", GetLastError());
527 LeaveCriticalSection(&ddraw_cs);
528 return E_FAIL;
531 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
532 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
534 This->devicewindow = devicewindow;
535 This->dest_window = devicewindow;
539 if(cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
541 /* Enable thread safety in wined3d */
542 IWineD3DDevice_SetMultithreaded(This->wineD3DDevice);
545 /* Unhandled flags */
546 if(cooplevel & DDSCL_ALLOWREBOOT)
547 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
548 if(cooplevel & DDSCL_ALLOWMODEX)
549 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
550 if(cooplevel & DDSCL_FPUSETUP)
551 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
553 /* Store the cooperative_level */
554 This->cooperative_level |= cooplevel;
555 TRACE("SetCooperativeLevel retuning DD_OK\n");
556 LeaveCriticalSection(&ddraw_cs);
557 return DD_OK;
560 /*****************************************************************************
562 * Helper function for SetDisplayMode and RestoreDisplayMode
564 * Implements DirectDraw's SetDisplayMode, but ignores the value of
565 * ForceRefreshRate, since it is already handled by
566 * IDirectDrawImpl_SetDisplayMode. RestoreDisplayMode can use this function
567 * without worrying that ForceRefreshRate will override the refresh rate. For
568 * argument and return value documentation, see
569 * IDirectDrawImpl_SetDisplayMode.
571 *****************************************************************************/
572 static HRESULT
573 IDirectDrawImpl_SetDisplayModeNoOverride(IDirectDraw7 *iface,
574 DWORD Width,
575 DWORD Height,
576 DWORD BPP,
577 DWORD RefreshRate,
578 DWORD Flags)
580 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
581 WINED3DDISPLAYMODE Mode;
582 HRESULT hr;
583 TRACE("(%p)->(%d,%d,%d,%d,%x): Relay!\n", This, Width, Height, BPP, RefreshRate, Flags);
585 EnterCriticalSection(&ddraw_cs);
586 if( !Width || !Height )
588 ERR("Width=%d, Height=%d, what to do?\n", Width, Height);
589 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
590 LeaveCriticalSection(&ddraw_cs);
591 return DD_OK;
594 /* Check the exclusive mode
595 if(!(This->cooperative_level & DDSCL_EXCLUSIVE))
596 return DDERR_NOEXCLUSIVEMODE;
597 * This is WRONG. Don't know if the SDK is completely
598 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
599 * is returned, but Half-Life 1.1.1.1 (Steam version)
600 * depends on this
603 Mode.Width = Width;
604 Mode.Height = Height;
605 Mode.RefreshRate = RefreshRate;
606 switch(BPP)
608 case 8: Mode.Format = WINED3DFMT_P8_UINT; break;
609 case 15: Mode.Format = WINED3DFMT_B5G5R5X1_UNORM; break;
610 case 16: Mode.Format = WINED3DFMT_B5G6R5_UNORM; break;
611 case 24: Mode.Format = WINED3DFMT_B8G8R8_UNORM; break;
612 case 32: Mode.Format = WINED3DFMT_B8G8R8X8_UNORM; break;
615 /* TODO: The possible return values from msdn suggest that
616 * the screen mode can't be changed if a surface is locked
617 * or some drawing is in progress
620 /* TODO: Lose the primary surface */
621 hr = IWineD3DDevice_SetDisplayMode(This->wineD3DDevice,
622 0, /* First swapchain */
623 &Mode);
624 LeaveCriticalSection(&ddraw_cs);
625 switch(hr)
627 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
628 default: return hr;
632 /*****************************************************************************
633 * IDirectDraw7::SetDisplayMode
635 * Sets the display screen resolution, color depth and refresh frequency
636 * when in fullscreen mode (in theory).
637 * Possible return values listed in the SDK suggest that this method fails
638 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
639 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
640 * It seems to be valid to pass 0 for With and Height, this has to be tested
641 * It could mean that the current video mode should be left as-is. (But why
642 * call it then?)
644 * Params:
645 * Height, Width: Screen dimension
646 * BPP: Color depth in Bits per pixel
647 * Refreshrate: Screen refresh rate
648 * Flags: Other stuff
650 * Returns
651 * DD_OK on success
653 *****************************************************************************/
654 static HRESULT WINAPI
655 IDirectDrawImpl_SetDisplayMode(IDirectDraw7 *iface,
656 DWORD Width,
657 DWORD Height,
658 DWORD BPP,
659 DWORD RefreshRate,
660 DWORD Flags)
662 if (force_refresh_rate != 0)
664 TRACE("ForceRefreshRate overriding passed-in refresh rate (%d Hz) to %d Hz\n", RefreshRate, force_refresh_rate);
665 RefreshRate = force_refresh_rate;
668 return IDirectDrawImpl_SetDisplayModeNoOverride(iface, Width, Height, BPP,
669 RefreshRate, Flags);
672 /*****************************************************************************
673 * IDirectDraw7::RestoreDisplayMode
675 * Restores the display mode to what it was at creation time. Basically.
677 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
678 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
679 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
680 * -> DD_1 is released. The screen should be left at 1024x768x32.
681 * -> DD_2 is released. The screen should be set to 1400x1050x32
682 * This case is unhandled right now, but Empire Earth does it this way.
683 * (But perhaps there is something in SetCooperativeLevel to prevent this)
685 * The msdn says that this method resets the display mode to what it was before
686 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
688 * Returns
689 * DD_OK on success
690 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
692 *****************************************************************************/
693 static HRESULT WINAPI
694 IDirectDrawImpl_RestoreDisplayMode(IDirectDraw7 *iface)
696 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
697 TRACE("(%p)\n", This);
699 return IDirectDrawImpl_SetDisplayModeNoOverride(iface,
700 This->orig_width, This->orig_height, This->orig_bpp, 0, 0);
703 /*****************************************************************************
704 * IDirectDraw7::GetCaps
706 * Returns the drives capabilities
708 * Used for version 1, 2, 4 and 7
710 * Params:
711 * DriverCaps: Structure to write the Hardware accelerated caps to
712 * HelCaps: Structure to write the emulation caps to
714 * Returns
715 * This implementation returns DD_OK only
717 *****************************************************************************/
718 static HRESULT WINAPI
719 IDirectDrawImpl_GetCaps(IDirectDraw7 *iface,
720 DDCAPS *DriverCaps,
721 DDCAPS *HELCaps)
723 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
724 DDCAPS caps;
725 WINED3DCAPS winecaps;
726 HRESULT hr;
727 DDSCAPS2 ddscaps = {0, 0, 0, 0};
728 TRACE("(%p)->(%p,%p)\n", This, DriverCaps, HELCaps);
730 /* One structure must be != NULL */
731 if( (!DriverCaps) && (!HELCaps) )
733 ERR("(%p) Invalid params to IDirectDrawImpl_GetCaps\n", This);
734 return DDERR_INVALIDPARAMS;
737 memset(&caps, 0, sizeof(caps));
738 memset(&winecaps, 0, sizeof(winecaps));
739 caps.dwSize = sizeof(caps);
740 EnterCriticalSection(&ddraw_cs);
741 hr = IWineD3DDevice_GetDeviceCaps(This->wineD3DDevice, &winecaps);
742 if(FAILED(hr)) {
743 WARN("IWineD3DDevice::GetDeviceCaps failed\n");
744 LeaveCriticalSection(&ddraw_cs);
745 return hr;
748 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
749 LeaveCriticalSection(&ddraw_cs);
750 if(FAILED(hr)) {
751 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
752 return hr;
755 caps.dwCaps = winecaps.DirectDrawCaps.Caps;
756 caps.dwCaps2 = winecaps.DirectDrawCaps.Caps2;
757 caps.dwCKeyCaps = winecaps.DirectDrawCaps.CKeyCaps;
758 caps.dwFXCaps = winecaps.DirectDrawCaps.FXCaps;
759 caps.dwPalCaps = winecaps.DirectDrawCaps.PalCaps;
760 caps.ddsCaps.dwCaps = winecaps.DirectDrawCaps.ddsCaps;
761 caps.dwSVBCaps = winecaps.DirectDrawCaps.SVBCaps;
762 caps.dwSVBCKeyCaps = winecaps.DirectDrawCaps.SVBCKeyCaps;
763 caps.dwSVBFXCaps = winecaps.DirectDrawCaps.SVBFXCaps;
764 caps.dwVSBCaps = winecaps.DirectDrawCaps.VSBCaps;
765 caps.dwVSBCKeyCaps = winecaps.DirectDrawCaps.VSBCKeyCaps;
766 caps.dwVSBFXCaps = winecaps.DirectDrawCaps.VSBFXCaps;
767 caps.dwSSBCaps = winecaps.DirectDrawCaps.SSBCaps;
768 caps.dwSSBCKeyCaps = winecaps.DirectDrawCaps.SSBCKeyCaps;
769 caps.dwSSBFXCaps = winecaps.DirectDrawCaps.SSBFXCaps;
771 /* Even if WineD3D supports 3D rendering, remove the cap if ddraw is configured
772 * not to use it
774 if(DefaultSurfaceType == SURFACE_GDI) {
775 caps.dwCaps &= ~DDCAPS_3D;
776 caps.ddsCaps.dwCaps &= ~(DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER);
778 if(winecaps.DirectDrawCaps.StrideAlign != 0) {
779 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
780 caps.dwAlignStrideAlign = winecaps.DirectDrawCaps.StrideAlign;
783 if(DriverCaps)
785 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
786 if (TRACE_ON(ddraw))
788 TRACE("Driver Caps :\n");
789 DDRAW_dump_DDCAPS(DriverCaps);
793 if(HELCaps)
795 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
796 if (TRACE_ON(ddraw))
798 TRACE("HEL Caps :\n");
799 DDRAW_dump_DDCAPS(HELCaps);
803 return DD_OK;
806 /*****************************************************************************
807 * IDirectDraw7::Compact
809 * No idea what it does, MSDN says it's not implemented.
811 * Returns
812 * DD_OK, but this is unchecked
814 *****************************************************************************/
815 static HRESULT WINAPI
816 IDirectDrawImpl_Compact(IDirectDraw7 *iface)
818 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
819 TRACE("(%p)\n", This);
821 return DD_OK;
824 /*****************************************************************************
825 * IDirectDraw7::GetDisplayMode
827 * Returns information about the current display mode
829 * Exists in Version 1, 2, 4 and 7
831 * Params:
832 * DDSD: Address of a surface description structure to write the info to
834 * Returns
835 * DD_OK
837 *****************************************************************************/
838 static HRESULT WINAPI
839 IDirectDrawImpl_GetDisplayMode(IDirectDraw7 *iface,
840 DDSURFACEDESC2 *DDSD)
842 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
843 HRESULT hr;
844 WINED3DDISPLAYMODE Mode;
845 DWORD Size;
846 TRACE("(%p)->(%p): Relay\n", This, DDSD);
848 EnterCriticalSection(&ddraw_cs);
849 /* This seems sane */
850 if (!DDSD)
852 LeaveCriticalSection(&ddraw_cs);
853 return DDERR_INVALIDPARAMS;
856 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
857 * so one method can be used for all versions (Hopefully)
859 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
860 0 /* swapchain 0 */,
861 &Mode);
862 if( hr != D3D_OK )
864 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
865 LeaveCriticalSection(&ddraw_cs);
866 return hr;
869 Size = DDSD->dwSize;
870 memset(DDSD, 0, Size);
872 DDSD->dwSize = Size;
873 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
874 DDSD->dwWidth = Mode.Width;
875 DDSD->dwHeight = Mode.Height;
876 DDSD->u2.dwRefreshRate = 60;
877 DDSD->ddsCaps.dwCaps = 0;
878 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
879 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, Mode.Format);
880 DDSD->u1.lPitch = Mode.Width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
882 if(TRACE_ON(ddraw))
884 TRACE("Returning surface desc :\n");
885 DDRAW_dump_surface_desc(DDSD);
888 LeaveCriticalSection(&ddraw_cs);
889 return DD_OK;
892 /*****************************************************************************
893 * IDirectDraw7::GetFourCCCodes
895 * Returns an array of supported FourCC codes.
897 * Exists in Version 1, 2, 4 and 7
899 * Params:
900 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
901 * of enumerated codes
902 * Codes: Pointer to an array of DWORDs where the supported codes are written
903 * to
905 * Returns
906 * Always returns DD_OK, as it's a stub for now
908 *****************************************************************************/
909 static HRESULT WINAPI
910 IDirectDrawImpl_GetFourCCCodes(IDirectDraw7 *iface,
911 DWORD *NumCodes, DWORD *Codes)
913 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
914 WINED3DFORMAT formats[] = {
915 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
916 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
917 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
919 DWORD count = 0, i, outsize;
920 HRESULT hr;
921 WINED3DDISPLAYMODE d3ddm;
922 WINED3DSURFTYPE type = This->ImplType;
923 TRACE("(%p)->(%p, %p)\n", This, NumCodes, Codes);
925 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
926 0 /* swapchain 0 */,
927 &d3ddm);
929 outsize = NumCodes && Codes ? *NumCodes : 0;
931 if(type == SURFACE_UNKNOWN) type = SURFACE_GDI;
933 for(i = 0; i < (sizeof(formats) / sizeof(formats[0])); i++) {
934 hr = IWineD3D_CheckDeviceFormat(This->wineD3D,
935 WINED3DADAPTER_DEFAULT,
936 WINED3DDEVTYPE_HAL,
937 d3ddm.Format /* AdapterFormat */,
938 0 /* usage */,
939 WINED3DRTYPE_SURFACE,
940 formats[i],
941 type);
942 if(SUCCEEDED(hr)) {
943 if(count < outsize) {
944 Codes[count] = formats[i];
946 count++;
949 if(NumCodes) {
950 TRACE("Returning %u FourCC codes\n", count);
951 *NumCodes = count;
954 return DD_OK;
957 /*****************************************************************************
958 * IDirectDraw7::GetMonitorFrequency
960 * Returns the monitor's frequency
962 * Exists in Version 1, 2, 4 and 7
964 * Params:
965 * Freq: Pointer to a DWORD to write the frequency to
967 * Returns
968 * Always returns DD_OK
970 *****************************************************************************/
971 static HRESULT WINAPI
972 IDirectDrawImpl_GetMonitorFrequency(IDirectDraw7 *iface,
973 DWORD *Freq)
975 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
976 TRACE("(%p)->(%p)\n", This, Freq);
978 /* Ideally this should be in WineD3D, as it concerns the screen setup,
979 * but for now this should make the games happy
981 *Freq = 60;
982 return DD_OK;
985 /*****************************************************************************
986 * IDirectDraw7::GetVerticalBlankStatus
988 * Returns the Vertical blank status of the monitor. This should be in WineD3D
989 * too basically, but as it's a semi stub, I didn't create a function there
991 * Params:
992 * status: Pointer to a BOOL to be filled with the vertical blank status
994 * Returns
995 * DD_OK on success
996 * DDERR_INVALIDPARAMS if status is NULL
998 *****************************************************************************/
999 static HRESULT WINAPI
1000 IDirectDrawImpl_GetVerticalBlankStatus(IDirectDraw7 *iface,
1001 BOOL *status)
1003 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1004 TRACE("(%p)->(%p)\n", This, status);
1006 /* This looks sane, the MSDN suggests it too */
1007 EnterCriticalSection(&ddraw_cs);
1008 if(!status)
1010 LeaveCriticalSection(&ddraw_cs);
1011 return DDERR_INVALIDPARAMS;
1014 *status = This->fake_vblank;
1015 This->fake_vblank = !This->fake_vblank;
1016 LeaveCriticalSection(&ddraw_cs);
1017 return DD_OK;
1020 /*****************************************************************************
1021 * IDirectDraw7::GetAvailableVidMem
1023 * Returns the total and free video memory
1025 * Params:
1026 * Caps: Specifies the memory type asked for
1027 * total: Pointer to a DWORD to be filled with the total memory
1028 * free: Pointer to a DWORD to be filled with the free memory
1030 * Returns
1031 * DD_OK on success
1032 * DDERR_INVALIDPARAMS of free and total are NULL
1034 *****************************************************************************/
1035 static HRESULT WINAPI
1036 IDirectDrawImpl_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total, DWORD *free)
1038 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1039 TRACE("(%p)->(%p, %p, %p)\n", This, Caps, total, free);
1041 if(TRACE_ON(ddraw))
1043 TRACE("(%p) Asked for memory with description: ", This);
1044 DDRAW_dump_DDSCAPS2(Caps);
1046 EnterCriticalSection(&ddraw_cs);
1048 /* Todo: System memory vs local video memory vs non-local video memory
1049 * The MSDN also mentions differences between texture memory and other
1050 * resources, but that's not important
1053 if( (!total) && (!free) )
1055 LeaveCriticalSection(&ddraw_cs);
1056 return DDERR_INVALIDPARAMS;
1059 if(total) *total = This->total_vidmem;
1060 if(free) *free = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
1062 LeaveCriticalSection(&ddraw_cs);
1063 return DD_OK;
1066 /*****************************************************************************
1067 * IDirectDraw7::Initialize
1069 * Initializes a DirectDraw interface.
1071 * Params:
1072 * GUID: Interface identifier. Well, don't know what this is really good
1073 * for
1075 * Returns
1076 * Returns DD_OK on the first call,
1077 * DDERR_ALREADYINITIALIZED on repeated calls
1079 *****************************************************************************/
1080 static HRESULT WINAPI
1081 IDirectDrawImpl_Initialize(IDirectDraw7 *iface,
1082 GUID *Guid)
1084 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1085 TRACE("(%p)->(%s): No-op\n", This, debugstr_guid(Guid));
1087 if(This->initialized)
1089 return DDERR_ALREADYINITIALIZED;
1091 else
1093 return DD_OK;
1097 /*****************************************************************************
1098 * IDirectDraw7::FlipToGDISurface
1100 * "Makes the surface that the GDI writes to the primary surface"
1101 * Looks like some windows specific thing we don't have to care about.
1102 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1103 * show error boxes ;)
1104 * Well, just return DD_OK.
1106 * Returns:
1107 * Always returns DD_OK
1109 *****************************************************************************/
1110 static HRESULT WINAPI
1111 IDirectDrawImpl_FlipToGDISurface(IDirectDraw7 *iface)
1113 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1114 TRACE("(%p)\n", This);
1116 return DD_OK;
1119 /*****************************************************************************
1120 * IDirectDraw7::WaitForVerticalBlank
1122 * This method allows applications to get in sync with the vertical blank
1123 * interval.
1124 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1125 * redraw the screen, most likely because of this stub
1127 * Parameters:
1128 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1129 * or DDWAITVB_BLOCKEND
1130 * h: Not used, according to MSDN
1132 * Returns:
1133 * Always returns DD_OK
1135 *****************************************************************************/
1136 static HRESULT WINAPI
1137 IDirectDrawImpl_WaitForVerticalBlank(IDirectDraw7 *iface,
1138 DWORD Flags,
1139 HANDLE h)
1141 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1142 static BOOL hide = FALSE;
1144 /* This function is called often, so print the fixme only once */
1145 if(!hide)
1147 FIXME("(%p)->(%x,%p): Stub\n", This, Flags, h);
1148 hide = TRUE;
1151 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1152 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1153 return DDERR_UNSUPPORTED; /* unchecked */
1155 return DD_OK;
1158 /*****************************************************************************
1159 * IDirectDraw7::GetScanLine
1161 * Returns the scan line that is being drawn on the monitor
1163 * Parameters:
1164 * Scanline: Address to write the scan line value to
1166 * Returns:
1167 * Always returns DD_OK
1169 *****************************************************************************/
1170 static HRESULT WINAPI IDirectDrawImpl_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1172 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1173 static BOOL hide = FALSE;
1174 WINED3DDISPLAYMODE Mode;
1176 /* This function is called often, so print the fixme only once */
1177 EnterCriticalSection(&ddraw_cs);
1178 if(!hide)
1180 FIXME("(%p)->(%p): Semi-Stub\n", This, Scanline);
1181 hide = TRUE;
1184 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1186 &Mode);
1188 /* Fake the line sweeping of the monitor */
1189 /* FIXME: We should synchronize with a source to keep the refresh rate */
1190 *Scanline = This->cur_scanline++;
1191 /* Assume 20 scan lines in the vertical blank */
1192 if (This->cur_scanline >= Mode.Height + 20)
1193 This->cur_scanline = 0;
1195 LeaveCriticalSection(&ddraw_cs);
1196 return DD_OK;
1199 /*****************************************************************************
1200 * IDirectDraw7::TestCooperativeLevel
1202 * Informs the application about the state of the video adapter, depending
1203 * on the cooperative level
1205 * Returns:
1206 * DD_OK if the device is in a sane state
1207 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1208 * if the state is not correct(See below)
1210 *****************************************************************************/
1211 static HRESULT WINAPI
1212 IDirectDrawImpl_TestCooperativeLevel(IDirectDraw7 *iface)
1214 TRACE("iface %p.\n", iface);
1216 return DD_OK;
1219 /*****************************************************************************
1220 * IDirectDraw7::GetGDISurface
1222 * Returns the surface that GDI is treating as the primary surface.
1223 * For Wine this is the front buffer
1225 * Params:
1226 * GDISurface: Address to write the surface pointer to
1228 * Returns:
1229 * DD_OK if the surface was found
1230 * DDERR_NOTFOUND if the GDI surface wasn't found
1232 *****************************************************************************/
1233 static HRESULT WINAPI
1234 IDirectDrawImpl_GetGDISurface(IDirectDraw7 *iface,
1235 IDirectDrawSurface7 **GDISurface)
1237 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1238 IWineD3DSurface *Surf;
1239 IDirectDrawSurface7 *ddsurf;
1240 HRESULT hr;
1241 DDSCAPS2 ddsCaps;
1242 TRACE("(%p)->(%p)\n", This, GDISurface);
1244 /* Get the back buffer from the wineD3DDevice and search its
1245 * attached surfaces for the front buffer
1247 EnterCriticalSection(&ddraw_cs);
1248 hr = IWineD3DDevice_GetBackBuffer(This->wineD3DDevice,
1249 0, /* SwapChain */
1250 0, /* first back buffer*/
1251 WINED3DBACKBUFFER_TYPE_MONO,
1252 &Surf);
1254 if( (hr != D3D_OK) ||
1255 (!Surf) )
1257 ERR("IWineD3DDevice::GetBackBuffer failed\n");
1258 LeaveCriticalSection(&ddraw_cs);
1259 return DDERR_NOTFOUND;
1262 /* GetBackBuffer AddRef()ed the surface, release it */
1263 IWineD3DSurface_Release(Surf);
1265 IWineD3DSurface_GetParent(Surf,
1266 (IUnknown **) &ddsurf);
1267 IDirectDrawSurface7_Release(ddsurf); /* For the GetParent */
1269 /* Find the front buffer */
1270 ddsCaps.dwCaps = DDSCAPS_FRONTBUFFER;
1271 hr = IDirectDrawSurface7_GetAttachedSurface(ddsurf,
1272 &ddsCaps,
1273 GDISurface);
1274 if(hr != DD_OK)
1276 ERR("IDirectDrawSurface7::GetAttachedSurface failed, hr = %x\n", hr);
1279 /* The AddRef is OK this time */
1280 LeaveCriticalSection(&ddraw_cs);
1281 return hr;
1284 /*****************************************************************************
1285 * IDirectDraw7::EnumDisplayModes
1287 * Enumerates the supported Display modes. The modes can be filtered with
1288 * the DDSD parameter.
1290 * Params:
1291 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES
1292 * DDSD: Surface description to filter the modes
1293 * Context: Pointer passed back to the callback function
1294 * cb: Application-provided callback function
1296 * Returns:
1297 * DD_OK on success
1298 * DDERR_INVALIDPARAMS if the callback wasn't set
1300 *****************************************************************************/
1301 static HRESULT WINAPI
1302 IDirectDrawImpl_EnumDisplayModes(IDirectDraw7 *iface,
1303 DWORD Flags,
1304 DDSURFACEDESC2 *DDSD,
1305 void *Context,
1306 LPDDENUMMODESCALLBACK2 cb)
1308 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1309 unsigned int modenum, fmt;
1310 WINED3DFORMAT pixelformat = WINED3DFMT_UNKNOWN;
1311 WINED3DDISPLAYMODE mode;
1312 DDSURFACEDESC2 callback_sd;
1313 WINED3DDISPLAYMODE *enum_modes = NULL;
1314 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
1316 WINED3DFORMAT checkFormatList[] =
1318 WINED3DFMT_B8G8R8X8_UNORM,
1319 WINED3DFMT_B5G6R5_UNORM,
1320 WINED3DFMT_P8_UINT,
1323 TRACE("(%p)->(%p,%p,%p): Relay\n", This, DDSD, Context, cb);
1325 EnterCriticalSection(&ddraw_cs);
1326 /* This looks sane */
1327 if(!cb)
1329 LeaveCriticalSection(&ddraw_cs);
1330 return DDERR_INVALIDPARAMS;
1333 if(DDSD)
1335 if ((DDSD->dwFlags & DDSD_PIXELFORMAT) && (DDSD->u4.ddpfPixelFormat.dwFlags & DDPF_RGB) )
1336 pixelformat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
1339 if(!(Flags & DDEDM_REFRESHRATES))
1341 enum_mode_array_size = 16;
1342 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1343 if (!enum_modes)
1345 ERR("Out of memory\n");
1346 LeaveCriticalSection(&ddraw_cs);
1347 return DDERR_OUTOFMEMORY;
1351 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
1353 if(pixelformat != WINED3DFMT_UNKNOWN && checkFormatList[fmt] != pixelformat)
1355 continue;
1358 modenum = 0;
1359 while(IWineD3D_EnumAdapterModes(This->wineD3D,
1360 WINED3DADAPTER_DEFAULT,
1361 checkFormatList[fmt],
1362 modenum++,
1363 &mode) == WINED3D_OK)
1365 if(DDSD)
1367 if(DDSD->dwFlags & DDSD_WIDTH && mode.Width != DDSD->dwWidth) continue;
1368 if(DDSD->dwFlags & DDSD_HEIGHT && mode.Height != DDSD->dwHeight) continue;
1371 if(!(Flags & DDEDM_REFRESHRATES))
1373 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
1374 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
1375 * to be reduced to a single unique result in such case.
1377 BOOL found = FALSE;
1378 unsigned i;
1380 for (i = 0; i < enum_mode_count; i++)
1382 if(enum_modes[i].Width == mode.Width && enum_modes[i].Height == mode.Height &&
1383 enum_modes[i].Format == mode.Format)
1385 found = TRUE;
1386 break;
1390 if(found) continue;
1393 memset(&callback_sd, 0, sizeof(callback_sd));
1394 callback_sd.dwSize = sizeof(callback_sd);
1395 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1397 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH;
1398 if(Flags & DDEDM_REFRESHRATES)
1400 callback_sd.dwFlags |= DDSD_REFRESHRATE;
1401 callback_sd.u2.dwRefreshRate = mode.RefreshRate;
1404 callback_sd.dwWidth = mode.Width;
1405 callback_sd.dwHeight = mode.Height;
1407 PixelFormat_WineD3DtoDD(&callback_sd.u4.ddpfPixelFormat, mode.Format);
1409 /* Calc pitch and DWORD align like MSDN says */
1410 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.Width;
1411 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
1413 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
1414 callback_sd.u2.dwRefreshRate);
1416 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
1418 TRACE("Application asked to terminate the enumeration\n");
1419 HeapFree(GetProcessHeap(), 0, enum_modes);
1420 LeaveCriticalSection(&ddraw_cs);
1421 return DD_OK;
1424 if(!(Flags & DDEDM_REFRESHRATES))
1426 if (enum_mode_count == enum_mode_array_size)
1428 WINED3DDISPLAYMODE *new_enum_modes;
1430 enum_mode_array_size *= 2;
1431 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1433 if (!new_enum_modes)
1435 ERR("Out of memory\n");
1436 HeapFree(GetProcessHeap(), 0, enum_modes);
1437 LeaveCriticalSection(&ddraw_cs);
1438 return DDERR_OUTOFMEMORY;
1441 enum_modes = new_enum_modes;
1444 enum_modes[enum_mode_count++] = mode;
1449 TRACE("End of enumeration\n");
1450 HeapFree(GetProcessHeap(), 0, enum_modes);
1451 LeaveCriticalSection(&ddraw_cs);
1452 return DD_OK;
1455 /*****************************************************************************
1456 * IDirectDraw7::EvaluateMode
1458 * Used with IDirectDraw7::StartModeTest to test video modes.
1459 * EvaluateMode is used to pass or fail a mode, and continue with the next
1460 * mode
1462 * Params:
1463 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
1464 * Timeout: Returns the amount of seconds left before the mode would have
1465 * been failed automatically
1467 * Returns:
1468 * This implementation always DD_OK, because it's a stub
1470 *****************************************************************************/
1471 static HRESULT WINAPI
1472 IDirectDrawImpl_EvaluateMode(IDirectDraw7 *iface,
1473 DWORD Flags,
1474 DWORD *Timeout)
1476 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1477 FIXME("(%p)->(%d,%p): Stub!\n", This, Flags, Timeout);
1479 /* When implementing this, implement it in WineD3D */
1481 return DD_OK;
1484 /*****************************************************************************
1485 * IDirectDraw7::GetDeviceIdentifier
1487 * Returns the device identifier, which gives information about the driver
1488 * Our device identifier is defined at the beginning of this file.
1490 * Params:
1491 * DDDI: Address for the returned structure
1492 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
1494 * Returns:
1495 * On success it returns DD_OK
1496 * DDERR_INVALIDPARAMS if DDDI is NULL
1498 *****************************************************************************/
1499 static HRESULT WINAPI
1500 IDirectDrawImpl_GetDeviceIdentifier(IDirectDraw7 *iface,
1501 DDDEVICEIDENTIFIER2 *DDDI,
1502 DWORD Flags)
1504 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1505 TRACE("(%p)->(%p,%08x)\n", This, DDDI, Flags);
1507 if(!DDDI)
1508 return DDERR_INVALIDPARAMS;
1510 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
1511 * host adapter, if there's a secondary 3D adapter. This doesn't apply
1512 * to any modern hardware, nor is it interesting for Wine, so ignore it.
1513 * Size of DDDEVICEIDENTIFIER2 may be aligned to 8 bytes and thus 4
1514 * bytes too long. So only copy the relevant part of the structure
1517 memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
1518 return DD_OK;
1521 /*****************************************************************************
1522 * IDirectDraw7::GetSurfaceFromDC
1524 * Returns the Surface for a GDI device context handle.
1525 * Is this related to IDirectDrawSurface::GetDC ???
1527 * Params:
1528 * hdc: hdc to return the surface for
1529 * Surface: Address to write the surface pointer to
1531 * Returns:
1532 * Always returns DD_OK because it's a stub
1534 *****************************************************************************/
1535 static HRESULT WINAPI
1536 IDirectDrawImpl_GetSurfaceFromDC(IDirectDraw7 *iface,
1537 HDC hdc,
1538 IDirectDrawSurface7 **Surface)
1540 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1541 IWineD3DSurface *wined3d_surface;
1542 HRESULT hr;
1544 TRACE("iface %p, dc %p, surface %p.\n", iface, hdc, Surface);
1546 if (!Surface) return E_INVALIDARG;
1548 hr = IWineD3DDevice_GetSurfaceFromDC(This->wineD3DDevice, hdc, &wined3d_surface);
1549 if (FAILED(hr))
1551 TRACE("No surface found for dc %p.\n", hdc);
1552 *Surface = NULL;
1553 return DDERR_NOTFOUND;
1556 IWineD3DSurface_GetParent(wined3d_surface, (IUnknown **)Surface);
1557 TRACE("Returning surface %p.\n", Surface);
1558 return DD_OK;
1561 /*****************************************************************************
1562 * IDirectDraw7::RestoreAllSurfaces
1564 * Calls the restore method of all surfaces
1566 * Params:
1568 * Returns:
1569 * Always returns DD_OK because it's a stub
1571 *****************************************************************************/
1572 static HRESULT WINAPI
1573 IDirectDrawImpl_RestoreAllSurfaces(IDirectDraw7 *iface)
1575 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1576 FIXME("(%p): Stub\n", This);
1578 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
1579 * get their parent and call their restore method. Do not implement
1580 * it in WineD3D, as restoring a surface means re-creating the
1581 * WineD3DDSurface
1583 return DD_OK;
1586 /*****************************************************************************
1587 * IDirectDraw7::StartModeTest
1589 * Tests the specified video modes to update the system registry with
1590 * refresh rate information. StartModeTest starts the mode test,
1591 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
1592 * isn't called within 15 seconds, the mode is failed automatically
1594 * As refresh rates are handled by the X server, I don't think this
1595 * Method is important
1597 * Params:
1598 * Modes: An array of mode specifications
1599 * NumModes: The number of modes in Modes
1600 * Flags: Some flags...
1602 * Returns:
1603 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
1604 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
1605 * otherwise DD_OK
1607 *****************************************************************************/
1608 static HRESULT WINAPI
1609 IDirectDrawImpl_StartModeTest(IDirectDraw7 *iface,
1610 SIZE *Modes,
1611 DWORD NumModes,
1612 DWORD Flags)
1614 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1615 WARN("(%p)->(%p, %d, %x): Semi-Stub, most likely harmless\n", This, Modes, NumModes, Flags);
1617 /* This looks sane */
1618 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
1620 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
1621 * As it is not, DDERR_TESTFINISHED is returned
1622 * (hopefully that's correct
1624 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
1625 * well, that value doesn't (yet) exist in the wine headers, so ignore it
1628 return DD_OK;
1631 /*****************************************************************************
1632 * IDirectDrawImpl_RecreateSurfacesCallback
1634 * Enumeration callback for IDirectDrawImpl_RecreateAllSurfaces.
1635 * It re-recreates the WineD3DSurface. It's pretty straightforward
1637 *****************************************************************************/
1638 HRESULT WINAPI
1639 IDirectDrawImpl_RecreateSurfacesCallback(IDirectDrawSurface7 *surf,
1640 DDSURFACEDESC2 *desc,
1641 void *Context)
1643 IDirectDrawSurfaceImpl *surfImpl = (IDirectDrawSurfaceImpl *)surf;
1644 IDirectDrawImpl *This = surfImpl->ddraw;
1645 IUnknown *Parent;
1646 IWineD3DSurface *wineD3DSurface;
1647 IWineD3DSwapChain *swapchain;
1648 HRESULT hr;
1649 IWineD3DClipper *clipper = NULL;
1651 WINED3DSURFACE_DESC Desc;
1652 WINED3DFORMAT Format;
1653 DWORD Usage;
1654 WINED3DPOOL Pool;
1656 WINED3DMULTISAMPLE_TYPE MultiSampleType;
1657 DWORD MultiSampleQuality;
1658 UINT Width;
1659 UINT Height;
1661 TRACE("(%p): Enumerated Surface %p\n", This, surfImpl);
1663 /* For the enumeration */
1664 IDirectDrawSurface7_Release(surf);
1666 if(surfImpl->ImplType == This->ImplType) return DDENUMRET_OK; /* Continue */
1668 /* Get the objects */
1669 swapchain = surfImpl->wineD3DSwapChain;
1670 surfImpl->wineD3DSwapChain = NULL;
1671 wineD3DSurface = surfImpl->WineD3DSurface;
1673 /* get the clipper */
1674 IWineD3DSurface_GetClipper(wineD3DSurface, &clipper);
1676 /* Get the surface properties */
1677 hr = IWineD3DSurface_GetDesc(wineD3DSurface, &Desc);
1678 if(hr != D3D_OK) return hr;
1680 Format = Desc.format;
1681 Usage = Desc.usage;
1682 Pool = Desc.pool;
1683 MultiSampleType = Desc.multisample_type;
1684 MultiSampleQuality = Desc.multisample_quality;
1685 Width = Desc.width;
1686 Height = Desc.height;
1688 IWineD3DSurface_GetParent(wineD3DSurface, &Parent);
1690 /* Create the new surface */
1691 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice, Width, Height, Format,
1692 TRUE /* Lockable */, FALSE /* Discard */, surfImpl->mipmap_level, &surfImpl->WineD3DSurface, Usage, Pool,
1693 MultiSampleType, MultiSampleQuality, This->ImplType, Parent, &ddraw_null_wined3d_parent_ops);
1694 IUnknown_Release(Parent);
1695 if (FAILED(hr))
1697 surfImpl->WineD3DSurface = wineD3DSurface;
1698 return hr;
1701 IWineD3DSurface_SetClipper(surfImpl->WineD3DSurface, clipper);
1703 /* TODO: Copy the surface content, except for render targets */
1705 /* If there's a swapchain, it owns the wined3d surfaces. So Destroy
1706 * the swapchain
1708 if(swapchain) {
1709 /* The backbuffers have the swapchain set as well, but the primary
1710 * owns it and destroys it
1712 if(surfImpl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
1713 IWineD3DDevice_UninitGDI(This->wineD3DDevice, D3D7CB_DestroySwapChain);
1715 surfImpl->isRenderTarget = FALSE;
1716 } else {
1717 if(IWineD3DSurface_Release(wineD3DSurface) == 0)
1718 TRACE("Surface released successful, next surface\n");
1719 else
1720 ERR("Something's still holding the old WineD3DSurface\n");
1723 surfImpl->ImplType = This->ImplType;
1725 if(clipper)
1727 IWineD3DClipper_Release(clipper);
1729 return DDENUMRET_OK;
1732 /*****************************************************************************
1733 * IDirectDrawImpl_RecreateAllSurfaces
1735 * A function, that converts all wineD3DSurfaces to the new implementation type
1736 * It enumerates all surfaces with IWineD3DDevice::EnumSurfaces, creates a
1737 * new WineD3DSurface, copies the content and releases the old surface
1739 *****************************************************************************/
1740 static HRESULT
1741 IDirectDrawImpl_RecreateAllSurfaces(IDirectDrawImpl *This)
1743 DDSURFACEDESC2 desc;
1744 TRACE("(%p): Switch to implementation %d\n", This, This->ImplType);
1746 if(This->ImplType != SURFACE_OPENGL && This->d3d_initialized)
1748 /* Should happen almost never */
1749 FIXME("(%p) Switching to non-opengl surfaces with d3d started. Is this a bug?\n", This);
1750 /* Shutdown d3d */
1751 IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroySwapChain);
1753 /* Contrary: D3D starting is handled by the caller, because it knows the render target */
1755 memset(&desc, 0, sizeof(desc));
1756 desc.dwSize = sizeof(desc);
1758 return IDirectDraw7_EnumSurfaces((IDirectDraw7 *)This, 0, &desc, This, IDirectDrawImpl_RecreateSurfacesCallback);
1761 ULONG WINAPI D3D7CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
1762 IUnknown* swapChainParent;
1763 TRACE("(%p) call back\n", pSwapChain);
1765 IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
1766 IUnknown_Release(swapChainParent);
1767 return IUnknown_Release(swapChainParent);
1770 /*****************************************************************************
1771 * IDirectDrawImpl_CreateNewSurface
1773 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
1774 * with the passed parameters.
1776 * Params:
1777 * DDSD: Description of the surface to create
1778 * Surf: Address to store the interface pointer at
1780 * Returns:
1781 * DD_OK on success
1783 *****************************************************************************/
1784 static HRESULT
1785 IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This,
1786 DDSURFACEDESC2 *pDDSD,
1787 IDirectDrawSurfaceImpl **ppSurf,
1788 UINT level)
1790 HRESULT hr;
1791 UINT Width, Height;
1792 WINED3DFORMAT Format = WINED3DFMT_UNKNOWN;
1793 DWORD Usage = 0;
1794 WINED3DSURFTYPE ImplType = This->ImplType;
1795 WINED3DSURFACE_DESC Desc;
1796 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
1798 if (TRACE_ON(ddraw))
1800 TRACE(" (%p) Requesting surface desc :\n", This);
1801 DDRAW_dump_surface_desc(pDDSD);
1804 /* Select the surface type, if it wasn't choosen yet */
1805 if(ImplType == SURFACE_UNKNOWN)
1807 /* Use GL Surfaces if a D3DDEVICE Surface is requested */
1808 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1810 TRACE("(%p) Choosing GL surfaces because a 3DDEVICE Surface was requested\n", This);
1811 ImplType = SURFACE_OPENGL;
1814 /* Otherwise use GDI surfaces for now */
1815 else
1817 TRACE("(%p) Choosing GDI surfaces for 2D rendering\n", This);
1818 ImplType = SURFACE_GDI;
1821 /* Policy if all surface implementations are available:
1822 * First, check if a default type was set with winecfg. If not,
1823 * try Xrender surfaces, and use them if they work. Next, check if
1824 * accelerated OpenGL is available, and use GL surfaces in this
1825 * case. If all else fails, use GDI surfaces. If a 3DDEVICE surface
1826 * was created, always use OpenGL surfaces.
1828 * (Note: Xrender surfaces are not implemented for now, the
1829 * unaccelerated implementation uses GDI to render in Software)
1832 /* Store the type. If it needs to be changed, all WineD3DSurfaces have to
1833 * be re-created. This could be done with IDirectDrawSurface7::Restore
1835 This->ImplType = ImplType;
1837 else
1839 if ((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1840 && (This->ImplType != SURFACE_OPENGL)
1841 && DefaultSurfaceType == SURFACE_UNKNOWN)
1843 /* We have to change to OpenGL,
1844 * and re-create all WineD3DSurfaces
1846 ImplType = SURFACE_OPENGL;
1847 This->ImplType = ImplType;
1848 TRACE("(%p) Re-creating all surfaces\n", This);
1849 IDirectDrawImpl_RecreateAllSurfaces(This);
1850 TRACE("(%p) Done recreating all surfaces\n", This);
1852 else if(This->ImplType != SURFACE_OPENGL && pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1854 WARN("The application requests a 3D capable surface, but a non-opengl surface was set in the registry\n");
1855 /* Do not fail surface creation, only fail 3D device creation */
1859 if (!(pDDSD->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY)) &&
1860 !((pDDSD->ddsCaps.dwCaps & DDSCAPS_TEXTURE) && (pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)) )
1862 /* Tests show surfaces without memory flags get these flags added right after creation. */
1863 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
1865 /* Get the correct wined3d usage */
1866 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE |
1867 DDSCAPS_3DDEVICE ) )
1869 Usage |= WINED3DUSAGE_RENDERTARGET;
1871 pDDSD->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
1873 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
1875 Usage |= WINED3DUSAGE_OVERLAY;
1877 if(This->depthstencil || (pDDSD->ddsCaps.dwCaps & DDSCAPS_ZBUFFER) )
1879 /* The depth stencil creation callback sets this flag.
1880 * Set the WineD3D usage to let it know that it's a depth
1881 * Stencil surface.
1883 Usage |= WINED3DUSAGE_DEPTHSTENCIL;
1885 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
1887 Pool = WINED3DPOOL_SYSTEMMEM;
1889 else if(pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
1891 Pool = WINED3DPOOL_MANAGED;
1892 /* Managed textures have the system memory flag set */
1893 pDDSD->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
1895 else if(pDDSD->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
1897 /* Videomemory adds localvidmem, this is mutually exclusive with systemmemory
1898 * and texturemanage
1900 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
1903 Format = PixelFormat_DD2WineD3D(&pDDSD->u4.ddpfPixelFormat);
1904 if(Format == WINED3DFMT_UNKNOWN)
1906 ERR("Unsupported / Unknown pixelformat\n");
1907 return DDERR_INVALIDPIXELFORMAT;
1910 /* Create the Surface object */
1911 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
1912 if(!*ppSurf)
1914 ERR("(%p) Error allocating memory for a surface\n", This);
1915 return DDERR_OUTOFVIDEOMEMORY;
1917 (*ppSurf)->lpVtbl = &IDirectDrawSurface7_Vtbl;
1918 (*ppSurf)->IDirectDrawSurface3_vtbl = &IDirectDrawSurface3_Vtbl;
1919 (*ppSurf)->IDirectDrawGammaControl_vtbl = &IDirectDrawGammaControl_Vtbl;
1920 (*ppSurf)->IDirect3DTexture2_vtbl = &IDirect3DTexture2_Vtbl;
1921 (*ppSurf)->IDirect3DTexture_vtbl = &IDirect3DTexture1_Vtbl;
1922 (*ppSurf)->ref = 1;
1923 (*ppSurf)->version = 7;
1924 TRACE("%p->version = %d\n", (*ppSurf), (*ppSurf)->version);
1925 (*ppSurf)->ddraw = This;
1926 (*ppSurf)->surface_desc.dwSize = sizeof(DDSURFACEDESC2);
1927 (*ppSurf)->surface_desc.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1928 DD_STRUCT_COPY_BYSIZE(&(*ppSurf)->surface_desc, pDDSD);
1930 /* Surface attachments */
1931 (*ppSurf)->next_attached = NULL;
1932 (*ppSurf)->first_attached = *ppSurf;
1934 /* Needed to re-create the surface on an implementation change */
1935 (*ppSurf)->ImplType = ImplType;
1937 /* For D3DDevice creation */
1938 (*ppSurf)->isRenderTarget = FALSE;
1940 /* A trace message for debugging */
1941 TRACE("(%p) Created IDirectDrawSurface implementation structure at %p\n", This, *ppSurf);
1943 /* Now create the WineD3D Surface */
1944 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice, pDDSD->dwWidth, pDDSD->dwHeight, Format,
1945 TRUE /* Lockable */, FALSE /* Discard */, level, &(*ppSurf)->WineD3DSurface,
1946 Usage, Pool, WINED3DMULTISAMPLE_NONE, 0 /* MultiSampleQuality */, ImplType,
1947 (IUnknown *)*ppSurf, &ddraw_null_wined3d_parent_ops);
1949 if(hr != D3D_OK)
1951 ERR("IWineD3DDevice::CreateSurface failed. hr = %08x\n", hr);
1952 return hr;
1955 /* Increase the surface counter, and attach the surface */
1956 InterlockedIncrement(&This->surfaces);
1957 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
1959 /* Here we could store all created surfaces in the DirectDrawImpl structure,
1960 * But this could also be delegated to WineDDraw, as it keeps track of all its
1961 * resources. Not implemented for now, as there are more important things ;)
1964 /* Get the pixel format of the WineD3DSurface and store it.
1965 * Don't use the Format choosen above, WineD3D might have
1966 * changed it
1968 (*ppSurf)->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
1969 hr = IWineD3DSurface_GetDesc((*ppSurf)->WineD3DSurface, &Desc);
1970 if(hr != D3D_OK)
1972 ERR("IWineD3DSurface::GetDesc failed\n");
1973 IDirectDrawSurface7_Release( (IDirectDrawSurface7 *) *ppSurf);
1974 return hr;
1977 Format = Desc.format;
1978 Width = Desc.width;
1979 Height = Desc.height;
1981 if(Format == WINED3DFMT_UNKNOWN)
1983 FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN\n");
1985 PixelFormat_WineD3DtoDD( &(*ppSurf)->surface_desc.u4.ddpfPixelFormat, Format);
1987 /* Anno 1602 stores the pitch right after surface creation, so make sure it's there.
1988 * I can't LockRect() the surface here because if OpenGL surfaces are in use, the
1989 * WineD3DDevice might not be usable for 3D yet, so an extra method was created.
1990 * TODO: Test other fourcc formats
1992 if(Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3 ||
1993 Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5)
1995 (*ppSurf)->surface_desc.dwFlags |= DDSD_LINEARSIZE;
1996 if(Format == WINED3DFMT_DXT1)
1998 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height) / 2;
2000 else
2002 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height);
2005 else
2007 (*ppSurf)->surface_desc.dwFlags |= DDSD_PITCH;
2008 (*ppSurf)->surface_desc.u1.lPitch = IWineD3DSurface_GetPitch((*ppSurf)->WineD3DSurface);
2011 /* Application passed a color key? Set it! */
2012 if(pDDSD->dwFlags & DDSD_CKDESTOVERLAY)
2014 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2015 DDCKEY_DESTOVERLAY,
2016 (WINEDDCOLORKEY *) &pDDSD->u3.ddckCKDestOverlay);
2018 if(pDDSD->dwFlags & DDSD_CKDESTBLT)
2020 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2021 DDCKEY_DESTBLT,
2022 (WINEDDCOLORKEY *) &pDDSD->ddckCKDestBlt);
2024 if(pDDSD->dwFlags & DDSD_CKSRCOVERLAY)
2026 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2027 DDCKEY_SRCOVERLAY,
2028 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcOverlay);
2030 if(pDDSD->dwFlags & DDSD_CKSRCBLT)
2032 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2033 DDCKEY_SRCBLT,
2034 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcBlt);
2036 if ( pDDSD->dwFlags & DDSD_LPSURFACE)
2038 hr = IWineD3DSurface_SetMem((*ppSurf)->WineD3DSurface, pDDSD->lpSurface);
2039 if(hr != WINED3D_OK)
2041 /* No need for a trace here, wined3d does that for us */
2042 IDirectDrawSurface7_Release((IDirectDrawSurface7 *)*ppSurf);
2043 return hr;
2047 return DD_OK;
2049 /*****************************************************************************
2050 * CreateAdditionalSurfaces
2052 * Creates a new mipmap chain.
2054 * Params:
2055 * root: Root surface to attach the newly created chain to
2056 * count: number of surfaces to create
2057 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2058 * effects on the caller
2059 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2060 * creates an additional surface without the mipmapping flags
2062 *****************************************************************************/
2063 static HRESULT
2064 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2065 IDirectDrawSurfaceImpl *root,
2066 UINT count,
2067 DDSURFACEDESC2 DDSD,
2068 BOOL CubeFaceRoot)
2070 UINT i, j, level = 0;
2071 HRESULT hr;
2072 IDirectDrawSurfaceImpl *last = root;
2074 for(i = 0; i < count; i++)
2076 IDirectDrawSurfaceImpl *object2 = NULL;
2078 /* increase the mipmap level, but only if a mipmap is created
2079 * In this case, also halve the size
2081 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2083 level++;
2084 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2085 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2086 /* Set the mipmap sublevel flag according to msdn */
2087 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2089 else
2091 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2093 CubeFaceRoot = FALSE;
2095 hr = IDirectDrawImpl_CreateNewSurface(This,
2096 &DDSD,
2097 &object2,
2098 level);
2099 if(hr != DD_OK)
2101 return hr;
2104 /* Add the new surface to the complex attachment array */
2105 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2107 if(last->complex_array[j]) continue;
2108 last->complex_array[j] = object2;
2109 break;
2111 last = object2;
2113 /* Remove the (possible) back buffer cap from the new surface description,
2114 * because only one surface in the flipping chain is a back buffer, one
2115 * is a front buffer, the others are just primary surfaces.
2117 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2119 return DD_OK;
2122 /*****************************************************************************
2123 * IDirectDraw7::CreateSurface
2125 * Creates a new IDirectDrawSurface object and returns its interface.
2127 * The surface connections with wined3d are a bit tricky. Basically it works
2128 * like this:
2130 * |------------------------| |-----------------|
2131 * | DDraw surface | | WineD3DSurface |
2132 * | | | |
2133 * | WineD3DSurface |-------------->| |
2134 * | Child |<------------->| Parent |
2135 * |------------------------| |-----------------|
2137 * The DDraw surface is the parent of the wined3d surface, and it releases
2138 * the WineD3DSurface when the ddraw surface is destroyed.
2140 * However, for all surfaces which can be in a container in WineD3D,
2141 * we have to do this. These surfaces are usually complex surfaces,
2142 * so this concerns primary surfaces with a front and a back buffer,
2143 * and textures.
2145 * |------------------------| |-----------------|
2146 * | DDraw surface | | Container |
2147 * | | | |
2148 * | Child |<------------->| Parent |
2149 * | Texture |<------------->| |
2150 * | WineD3DSurface |<----| | Levels |<--|
2151 * | Complex connection | | | | |
2152 * |------------------------| | |-----------------| |
2153 * ^ | |
2154 * | | |
2155 * | | |
2156 * | |------------------| | |-----------------| |
2157 * | | IParent | |-------->| WineD3DSurface | |
2158 * | | | | | |
2159 * | | Child |<------------->| Parent | |
2160 * | | | | Container |<--|
2161 * | |------------------| |-----------------| |
2162 * | |
2163 * | |----------------------| |
2164 * | | DDraw surface 2 | |
2165 * | | | |
2166 * |<->| Complex root Child | |
2167 * | | Texture | |
2168 * | | WineD3DSurface |<----| |
2169 * | |----------------------| | |
2170 * | | |
2171 * | |---------------------| | |-----------------| |
2172 * | | IParent | |----->| WineD3DSurface | |
2173 * | | | | | |
2174 * | | Child |<---------->| Parent | |
2175 * | |---------------------| | Container |<--|
2176 * | |-----------------| |
2177 * | |
2178 * | ---More surfaces can follow--- |
2180 * The reason is that the IWineD3DSwapchain(render target container)
2181 * and the IWineD3DTexure(Texture container) release the parents
2182 * of their surface's children, but by releasing the complex root
2183 * the surfaces which are complexly attached to it are destroyed
2184 * too. See IDirectDrawSurface::Release for a more detailed
2185 * explanation.
2187 * Params:
2188 * DDSD: Description of the surface to create
2189 * Surf: Address to store the interface pointer at
2190 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2191 * aggregation, so it has to be NULL
2193 * Returns:
2194 * DD_OK on success
2195 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2196 * DDERR_* if an error occurs
2198 *****************************************************************************/
2199 static HRESULT WINAPI
2200 IDirectDrawImpl_CreateSurface(IDirectDraw7 *iface,
2201 DDSURFACEDESC2 *DDSD,
2202 IDirectDrawSurface7 **Surf,
2203 IUnknown *UnkOuter)
2205 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
2206 IDirectDrawSurfaceImpl *object = NULL;
2207 HRESULT hr;
2208 LONG extra_surfaces = 0;
2209 DDSURFACEDESC2 desc2;
2210 WINED3DDISPLAYMODE Mode;
2211 const DWORD sysvidmem = DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
2213 TRACE("(%p)->(%p,%p,%p)\n", This, DDSD, Surf, UnkOuter);
2215 /* Some checks before we start */
2216 if (TRACE_ON(ddraw))
2218 TRACE(" (%p) Requesting surface desc :\n", This);
2219 DDRAW_dump_surface_desc(DDSD);
2221 EnterCriticalSection(&ddraw_cs);
2223 if (UnkOuter != NULL)
2225 FIXME("(%p) : outer != NULL?\n", This);
2226 LeaveCriticalSection(&ddraw_cs);
2227 return CLASS_E_NOAGGREGATION; /* unchecked */
2230 if (Surf == NULL)
2232 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", This);
2233 LeaveCriticalSection(&ddraw_cs);
2234 return E_POINTER; /* unchecked */
2237 if (!(DDSD->dwFlags & DDSD_CAPS))
2239 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2240 DDSD->dwFlags |= DDSD_CAPS;
2243 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2245 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2246 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2249 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2251 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2252 WARN("(%p) Null surface pointer specified, ignore it!\n", This);
2253 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2256 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2257 !(This->cooperative_level & DDSCL_EXCLUSIVE))
2259 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n", This);
2260 *Surf = NULL;
2261 LeaveCriticalSection(&ddraw_cs);
2262 return DDERR_NOEXCLUSIVEMODE;
2265 if(DDSD->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER)) {
2266 WARN("Application tried to create an explicit front or back buffer\n");
2267 LeaveCriticalSection(&ddraw_cs);
2268 return DDERR_INVALIDCAPS;
2271 if((DDSD->ddsCaps.dwCaps & sysvidmem) == sysvidmem)
2273 /* This is a special switch in ddrawex.dll, but not allowed in ddraw.dll */
2274 WARN("Application tries to put the surface in both system and video memory\n");
2275 LeaveCriticalSection(&ddraw_cs);
2276 *Surf = NULL;
2277 return DDERR_INVALIDCAPS;
2280 /* Check cube maps but only if the size includes them */
2281 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2283 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2284 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2286 WARN("Cube map faces requested without cube map flag\n");
2287 LeaveCriticalSection(&ddraw_cs);
2288 return DDERR_INVALIDCAPS;
2290 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2291 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2293 WARN("Cube map without faces requested\n");
2294 LeaveCriticalSection(&ddraw_cs);
2295 return DDERR_INVALIDPARAMS;
2298 /* Quick tests confirm those can be created, but we don't do that yet */
2299 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2300 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2302 FIXME("Partial cube maps not supported yet\n");
2306 /* According to the msdn this flag is ignored by CreateSurface */
2307 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2308 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2310 /* Modify some flags */
2311 memset(&desc2, 0, sizeof(desc2));
2312 desc2.dwSize = sizeof(desc2); /* For the struct copy */
2313 DD_STRUCT_COPY_BYSIZE(&desc2, DDSD);
2314 desc2.dwSize = sizeof(desc2); /* To override a possibly smaller size */
2315 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2317 /* Get the video mode from WineD3D - we will need it */
2318 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
2319 0, /* Swapchain 0 */
2320 &Mode);
2321 if(FAILED(hr))
2323 ERR("Failed to read display mode from wined3d\n");
2324 switch(This->orig_bpp)
2326 case 8:
2327 Mode.Format = WINED3DFMT_P8_UINT;
2328 break;
2330 case 15:
2331 Mode.Format = WINED3DFMT_B5G5R5X1_UNORM;
2332 break;
2334 case 16:
2335 Mode.Format = WINED3DFMT_B5G6R5_UNORM;
2336 break;
2338 case 24:
2339 Mode.Format = WINED3DFMT_B8G8R8_UNORM;
2340 break;
2342 case 32:
2343 Mode.Format = WINED3DFMT_B8G8R8X8_UNORM;
2344 break;
2346 Mode.Width = This->orig_width;
2347 Mode.Height = This->orig_height;
2350 /* No pixelformat given? Use the current screen format */
2351 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2353 desc2.dwFlags |= DDSD_PIXELFORMAT;
2354 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2356 /* Wait: It could be a Z buffer */
2357 if(desc2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
2359 switch(desc2.u2.dwMipMapCount) /* Who had this glorious idea? */
2361 case 15:
2362 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_S1_UINT_D15_UNORM);
2363 break;
2364 case 16:
2365 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D16_UNORM);
2366 break;
2367 case 24:
2368 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_X8D24_UNORM);
2369 break;
2370 case 32:
2371 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D32_UNORM);
2372 break;
2373 default:
2374 ERR("Unknown Z buffer bit depth\n");
2377 else
2379 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
2383 /* No Width or no Height? Use the original screen size
2385 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2386 !(desc2.dwFlags & DDSD_HEIGHT) )
2388 /* Invalid for non-render targets */
2389 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2391 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2392 *Surf = NULL;
2393 LeaveCriticalSection(&ddraw_cs);
2394 return DDERR_INVALIDPARAMS;
2397 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2398 desc2.dwWidth = Mode.Width;
2399 desc2.dwHeight = Mode.Height;
2402 /* Mipmap count fixes */
2403 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2405 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2407 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2409 /* Mipmap count is given, should not be 0 */
2410 if( desc2.u2.dwMipMapCount == 0 )
2412 LeaveCriticalSection(&ddraw_cs);
2413 return DDERR_INVALIDPARAMS;
2416 else
2418 /* Undocumented feature: Create sublevels until
2419 * either the width or the height is 1
2421 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2422 desc2.dwWidth : desc2.dwHeight;
2423 desc2.u2.dwMipMapCount = 0;
2424 while( min )
2426 desc2.u2.dwMipMapCount += 1;
2427 min >>= 1;
2431 else
2433 /* Not-complex mipmap -> Mipmapcount = 1 */
2434 desc2.u2.dwMipMapCount = 1;
2436 extra_surfaces = desc2.u2.dwMipMapCount - 1;
2438 /* There's a mipmap count in the created surface in any case */
2439 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2441 /* If no mipmap is given, the texture has only one level */
2443 /* The first surface is a front buffer, the back buffer is created afterwards */
2444 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2446 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2449 /* The root surface in a cube map is positive x */
2450 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2452 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2453 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2456 /* Create the first surface */
2457 hr = IDirectDrawImpl_CreateNewSurface(This, &desc2, &object, 0);
2458 if( hr != DD_OK)
2460 ERR("IDirectDrawImpl_CreateNewSurface failed with %08x\n", hr);
2461 LeaveCriticalSection(&ddraw_cs);
2462 return hr;
2464 object->is_complex_root = TRUE;
2466 *Surf = (IDirectDrawSurface7 *)object;
2468 /* Create Additional surfaces if necessary
2469 * This applies to Primary surfaces which have a back buffer count
2470 * set, but not to mipmap textures. In case of Mipmap textures,
2471 * wineD3D takes care of the creation of additional surfaces
2473 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2475 extra_surfaces = DDSD->dwBackBufferCount;
2476 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2477 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2478 desc2.dwBackBufferCount = 0;
2481 hr = DD_OK;
2482 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2484 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2485 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
2486 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2487 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
2488 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
2489 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2490 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
2491 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
2492 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2493 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
2494 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
2495 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2496 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
2497 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
2498 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2499 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
2500 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2503 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces, desc2, FALSE);
2504 if(hr != DD_OK)
2506 /* This destroys and possibly created surfaces too */
2507 IDirectDrawSurface_Release((IDirectDrawSurface7 *)object);
2508 LeaveCriticalSection(&ddraw_cs);
2509 return hr;
2512 /* If the implementation is OpenGL and there's no d3ddevice, attach a d3ddevice
2513 * But attach the d3ddevice only if the currently created surface was
2514 * a primary surface (2D app in 3D mode) or a 3DDEVICE surface (3D app)
2515 * The only case I can think of where this doesn't apply is when a
2516 * 2D app was configured by the user to run with OpenGL and it didn't create
2517 * the render target as first surface. In this case the render target creation
2518 * will cause the 3D init.
2520 if( (This->ImplType == SURFACE_OPENGL) && !(This->d3d_initialized) &&
2521 desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE) )
2523 IDirectDrawSurfaceImpl *target = object, *surface;
2524 struct list *entry;
2526 /* Search for the primary to use as render target */
2527 LIST_FOR_EACH(entry, &This->surface_list)
2529 surface = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2530 if((surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER)) ==
2531 (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER))
2533 /* found */
2534 target = surface;
2535 TRACE("Using primary %p as render target\n", target);
2536 break;
2540 TRACE("(%p) Attaching a D3DDevice, rendertarget = %p\n", This, target);
2541 hr = IDirectDrawImpl_AttachD3DDevice(This, target);
2542 if(hr != D3D_OK)
2544 IDirectDrawSurfaceImpl *release_surf;
2545 ERR("IDirectDrawImpl_AttachD3DDevice failed, hr = %x\n", hr);
2546 *Surf = NULL;
2548 /* The before created surface structures are in an incomplete state here.
2549 * WineD3D holds the reference on the IParents, and it released them on the failure
2550 * already. So the regular release method implementation would fail on the attempt
2551 * to destroy either the IParents or the swapchain. So free the surface here.
2552 * The surface structure here is a list, not a tree, because onscreen targets
2553 * cannot be cube textures
2555 while(object)
2557 release_surf = object;
2558 object = object->complex_array[0];
2559 IDirectDrawSurfaceImpl_Destroy(release_surf);
2561 LeaveCriticalSection(&ddraw_cs);
2562 return hr;
2564 } else if(!(This->d3d_initialized) && desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
2565 IDirectDrawImpl_CreateGDISwapChain(This, object);
2568 /* Addref the ddraw interface to keep an reference for each surface */
2569 IDirectDraw7_AddRef(iface);
2570 object->ifaceToRelease = (IUnknown *) iface;
2572 /* Create a WineD3DTexture if a texture was requested */
2573 if(desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
2575 UINT levels;
2576 WINED3DFORMAT Format;
2577 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
2579 This->tex_root = object;
2581 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2583 /* a mipmap is created, create enough levels */
2584 levels = desc2.u2.dwMipMapCount;
2586 else
2588 /* No mipmap is created, create one level */
2589 levels = 1;
2592 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM */
2593 if(DDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
2595 Pool = WINED3DPOOL_SYSTEMMEM;
2597 /* Should I forward the MANAGED cap to the managed pool ? */
2599 /* Get the format. It's set already by CreateNewSurface */
2600 Format = PixelFormat_DD2WineD3D(&object->surface_desc.u4.ddpfPixelFormat);
2602 /* The surfaces are already created, the callback only
2603 * passes the IWineD3DSurface to WineD3D
2605 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2607 hr = IWineD3DDevice_CreateCubeTexture(This->wineD3DDevice, DDSD->dwWidth /* Edgelength */,
2608 levels, 0 /* usage */, Format, Pool, (IWineD3DCubeTexture **)&object->wineD3DTexture,
2609 (IUnknown *)object, &ddraw_null_wined3d_parent_ops);
2611 else
2613 hr = IWineD3DDevice_CreateTexture(This->wineD3DDevice, DDSD->dwWidth, DDSD->dwHeight, levels,
2614 0 /* usage */, Format, Pool, (IWineD3DTexture **)&object->wineD3DTexture,
2615 (IUnknown *)object, &ddraw_null_wined3d_parent_ops);
2617 This->tex_root = NULL;
2620 LeaveCriticalSection(&ddraw_cs);
2621 return hr;
2624 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
2625 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
2627 static BOOL
2628 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
2629 const DDPIXELFORMAT *provided)
2631 /* Some flags must be present in both or neither for a match. */
2632 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
2633 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
2634 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
2636 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2637 return FALSE;
2639 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
2640 return FALSE;
2642 if (requested->dwFlags & DDPF_FOURCC)
2643 if (requested->dwFourCC != provided->dwFourCC)
2644 return FALSE;
2646 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
2647 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2648 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
2649 return FALSE;
2651 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2652 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2653 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
2654 return FALSE;
2656 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
2657 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
2658 return FALSE;
2660 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
2661 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2662 |DDPF_BUMPDUDV))
2663 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
2664 return FALSE;
2666 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
2667 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
2668 return FALSE;
2670 return TRUE;
2673 static BOOL
2674 IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested,
2675 const DDSURFACEDESC2* provided)
2677 struct compare_info
2679 DWORD flag;
2680 ptrdiff_t offset;
2681 size_t size;
2684 #define CMP(FLAG, FIELD) \
2685 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
2686 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
2688 static const struct compare_info compare[] =
2690 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
2691 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
2692 CMP(CAPS, ddsCaps),
2693 CMP(CKDESTBLT, ddckCKDestBlt),
2694 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
2695 CMP(CKSRCBLT, ddckCKSrcBlt),
2696 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
2697 CMP(HEIGHT, dwHeight),
2698 CMP(LINEARSIZE, u1 /* dwLinearSize */),
2699 CMP(LPSURFACE, lpSurface),
2700 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
2701 CMP(PITCH, u1 /* lPitch */),
2702 /* PIXELFORMAT: manual */
2703 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
2704 CMP(TEXTURESTAGE, dwTextureStage),
2705 CMP(WIDTH, dwWidth),
2706 /* ZBUFFERBITDEPTH: "obsolete" */
2709 #undef CMP
2711 unsigned int i;
2713 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2714 return FALSE;
2716 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
2718 if (requested->dwFlags & compare[i].flag
2719 && memcmp((const char *)provided + compare[i].offset,
2720 (const char *)requested + compare[i].offset,
2721 compare[i].size) != 0)
2722 return FALSE;
2725 if (requested->dwFlags & DDSD_PIXELFORMAT)
2727 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
2728 &provided->u4.ddpfPixelFormat))
2729 return FALSE;
2732 return TRUE;
2735 #undef DDENUMSURFACES_SEARCHTYPE
2736 #undef DDENUMSURFACES_MATCHTYPE
2738 /*****************************************************************************
2739 * IDirectDraw7::EnumSurfaces
2741 * Loops through all surfaces attached to this device and calls the
2742 * application callback. This can't be relayed to WineD3DDevice,
2743 * because some WineD3DSurfaces' parents are IParent objects
2745 * Params:
2746 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
2747 * DDSD: Description to filter for
2748 * Context: Application-provided pointer, it's passed unmodified to the
2749 * Callback function
2750 * Callback: Address to call for each surface
2752 * Returns:
2753 * DDERR_INVALIDPARAMS if the callback is NULL
2754 * DD_OK on success
2756 *****************************************************************************/
2757 static HRESULT WINAPI
2758 IDirectDrawImpl_EnumSurfaces(IDirectDraw7 *iface,
2759 DWORD Flags,
2760 DDSURFACEDESC2 *DDSD,
2761 void *Context,
2762 LPDDENUMSURFACESCALLBACK7 Callback)
2764 /* The surface enumeration is handled by WineDDraw,
2765 * because it keeps track of all surfaces attached to
2766 * it. The filtering is done by our callback function,
2767 * because WineDDraw doesn't handle ddraw-like surface
2768 * caps structures
2770 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
2771 IDirectDrawSurfaceImpl *surf;
2772 BOOL all, nomatch;
2773 DDSURFACEDESC2 desc;
2774 struct list *entry, *entry2;
2776 all = Flags & DDENUMSURFACES_ALL;
2777 nomatch = Flags & DDENUMSURFACES_NOMATCH;
2779 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, DDSD, Context, Callback);
2780 EnterCriticalSection(&ddraw_cs);
2782 if(!Callback)
2784 LeaveCriticalSection(&ddraw_cs);
2785 return DDERR_INVALIDPARAMS;
2788 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
2789 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
2791 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2792 if (all || (nomatch != IDirectDrawImpl_DDSD_Match(DDSD, &surf->surface_desc)))
2794 desc = surf->surface_desc;
2795 IDirectDrawSurface7_AddRef((IDirectDrawSurface7 *)surf);
2796 if (Callback((IDirectDrawSurface7 *)surf, &desc, Context) != DDENUMRET_OK)
2798 LeaveCriticalSection(&ddraw_cs);
2799 return DD_OK;
2803 LeaveCriticalSection(&ddraw_cs);
2804 return DD_OK;
2807 static HRESULT WINAPI
2808 findRenderTarget(IDirectDrawSurface7 *surface,
2809 DDSURFACEDESC2 *desc,
2810 void *ctx)
2812 IDirectDrawSurfaceImpl *surf = (IDirectDrawSurfaceImpl *)surface;
2813 IDirectDrawSurfaceImpl **target = ctx;
2815 if(!surf->isRenderTarget) {
2816 *target = surf;
2817 IDirectDrawSurface7_Release(surface);
2818 return DDENUMRET_CANCEL;
2821 /* Recurse into the surface tree */
2822 IDirectDrawSurface7_EnumAttachedSurfaces(surface, ctx, findRenderTarget);
2824 IDirectDrawSurface7_Release(surface);
2825 if(*target) return DDENUMRET_CANCEL;
2826 else return DDENUMRET_OK; /* Continue with the next neighbor surface */
2829 static HRESULT IDirectDrawImpl_CreateGDISwapChain(IDirectDrawImpl *This,
2830 IDirectDrawSurfaceImpl *primary) {
2831 HRESULT hr;
2832 WINED3DPRESENT_PARAMETERS presentation_parameters;
2833 HWND window;
2835 window = This->dest_window;
2837 memset(&presentation_parameters, 0, sizeof(presentation_parameters));
2839 /* Use the surface description for the device parameters, not the
2840 * Device settings. The app might render to an offscreen surface
2842 presentation_parameters.BackBufferWidth = primary->surface_desc.dwWidth;
2843 presentation_parameters.BackBufferHeight = primary->surface_desc.dwHeight;
2844 presentation_parameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2845 presentation_parameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
2846 presentation_parameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
2847 presentation_parameters.MultiSampleQuality = 0;
2848 presentation_parameters.SwapEffect = WINED3DSWAPEFFECT_FLIP;
2849 presentation_parameters.hDeviceWindow = window;
2850 presentation_parameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
2851 presentation_parameters.EnableAutoDepthStencil = FALSE; /* Not on GDI swapchains */
2852 presentation_parameters.AutoDepthStencilFormat = 0;
2853 presentation_parameters.Flags = 0;
2854 presentation_parameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
2855 presentation_parameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
2857 This->d3d_target = primary;
2858 hr = IWineD3DDevice_InitGDI(This->wineD3DDevice, &presentation_parameters);
2859 This->d3d_target = NULL;
2861 if (hr != D3D_OK)
2863 FIXME("(%p) call to IWineD3DDevice_InitGDI failed\n", This);
2864 primary->wineD3DSwapChain = NULL;
2866 return hr;
2869 /*****************************************************************************
2870 * IDirectDrawImpl_AttachD3DDevice
2872 * Initializes the D3D capabilities of WineD3D
2874 * Params:
2875 * primary: The primary surface for D3D
2877 * Returns
2878 * DD_OK on success,
2879 * DDERR_* otherwise
2881 *****************************************************************************/
2882 static HRESULT
2883 IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This,
2884 IDirectDrawSurfaceImpl *primary)
2886 HRESULT hr;
2887 HWND window = This->dest_window;
2889 WINED3DPRESENT_PARAMETERS localParameters;
2891 TRACE("(%p)->(%p)\n", This, primary);
2893 /* If there's no window, create a hidden window. WineD3D needs it */
2894 if(window == 0 || window == GetDesktopWindow())
2896 window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "Hidden D3D Window",
2897 WS_DISABLED, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
2898 NULL, NULL, NULL, NULL);
2899 if (!window)
2901 ERR("Failed to create window, last error %#x.\n", GetLastError());
2902 return E_FAIL;
2905 ShowWindow(window, SW_HIDE); /* Just to be sure */
2906 WARN("(%p) No window for the Direct3DDevice, created a hidden window. HWND=%p\n", This, window);
2908 else
2910 TRACE("(%p) Using existing window %p for Direct3D rendering\n", This, window);
2912 This->d3d_window = window;
2914 /* Store the future Render Target surface */
2915 This->d3d_target = primary;
2917 /* Use the surface description for the device parameters, not the
2918 * Device settings. The app might render to an offscreen surface
2920 localParameters.BackBufferWidth = primary->surface_desc.dwWidth;
2921 localParameters.BackBufferHeight = primary->surface_desc.dwHeight;
2922 localParameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2923 localParameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
2924 localParameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
2925 localParameters.MultiSampleQuality = 0;
2926 localParameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
2927 localParameters.hDeviceWindow = window;
2928 localParameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
2929 localParameters.EnableAutoDepthStencil = TRUE;
2930 localParameters.AutoDepthStencilFormat = WINED3DFMT_D16_UNORM;
2931 localParameters.Flags = 0;
2932 localParameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
2933 localParameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
2935 TRACE("Passing mode %d\n", localParameters.BackBufferFormat);
2937 /* Set this NOW, otherwise creating the depth stencil surface will cause a
2938 * recursive loop until ram or emulated video memory is full
2940 This->d3d_initialized = TRUE;
2942 hr = IWineD3DDevice_Init3D(This->wineD3DDevice, &localParameters);
2943 if(FAILED(hr))
2945 This->d3d_target = NULL;
2946 This->d3d_initialized = FALSE;
2947 return hr;
2950 This->declArraySize = 2;
2951 This->decls = HeapAlloc(GetProcessHeap(),
2952 HEAP_ZERO_MEMORY,
2953 sizeof(*This->decls) * This->declArraySize);
2954 if(!This->decls)
2956 ERR("Error allocating an array for the converted vertex decls\n");
2957 This->declArraySize = 0;
2958 hr = IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroySwapChain);
2959 return E_OUTOFMEMORY;
2962 /* Create an Index Buffer parent */
2963 TRACE("(%p) Successfully initialized 3D\n", This);
2964 return DD_OK;
2967 /*****************************************************************************
2968 * DirectDrawCreateClipper (DDRAW.@)
2970 * Creates a new IDirectDrawClipper object.
2972 * Params:
2973 * Clipper: Address to write the interface pointer to
2974 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
2975 * NULL
2977 * Returns:
2978 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2979 * E_OUTOFMEMORY if allocating the object failed
2981 *****************************************************************************/
2982 HRESULT WINAPI
2983 DirectDrawCreateClipper(DWORD Flags,
2984 LPDIRECTDRAWCLIPPER *Clipper,
2985 IUnknown *UnkOuter)
2987 IDirectDrawClipperImpl* object;
2988 TRACE("(%08x,%p,%p)\n", Flags, Clipper, UnkOuter);
2990 EnterCriticalSection(&ddraw_cs);
2991 if (UnkOuter != NULL)
2993 LeaveCriticalSection(&ddraw_cs);
2994 return CLASS_E_NOAGGREGATION;
2997 if (!LoadWineD3D())
2999 LeaveCriticalSection(&ddraw_cs);
3000 return DDERR_NODIRECTDRAWSUPPORT;
3003 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3004 sizeof(IDirectDrawClipperImpl));
3005 if (object == NULL)
3007 LeaveCriticalSection(&ddraw_cs);
3008 return E_OUTOFMEMORY;
3011 object->lpVtbl = &IDirectDrawClipper_Vtbl;
3012 object->ref = 1;
3013 object->wineD3DClipper = pWineDirect3DCreateClipper((IUnknown *) object);
3014 if(!object->wineD3DClipper)
3016 HeapFree(GetProcessHeap(), 0, object);
3017 LeaveCriticalSection(&ddraw_cs);
3018 return E_OUTOFMEMORY;
3021 *Clipper = (IDirectDrawClipper *) object;
3022 LeaveCriticalSection(&ddraw_cs);
3023 return DD_OK;
3026 /*****************************************************************************
3027 * IDirectDraw7::CreateClipper
3029 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3031 *****************************************************************************/
3032 static HRESULT WINAPI
3033 IDirectDrawImpl_CreateClipper(IDirectDraw7 *iface,
3034 DWORD Flags,
3035 IDirectDrawClipper **Clipper,
3036 IUnknown *UnkOuter)
3038 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
3039 TRACE("(%p)->(%x,%p,%p)\n", This, Flags, Clipper, UnkOuter);
3040 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3043 /*****************************************************************************
3044 * IDirectDraw7::CreatePalette
3046 * Creates a new IDirectDrawPalette object
3048 * Params:
3049 * Flags: The flags for the new clipper
3050 * ColorTable: Color table to assign to the new clipper
3051 * Palette: Address to write the interface pointer to
3052 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3053 * NULL
3055 * Returns:
3056 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3057 * E_OUTOFMEMORY if allocating the object failed
3059 *****************************************************************************/
3060 static HRESULT WINAPI
3061 IDirectDrawImpl_CreatePalette(IDirectDraw7 *iface,
3062 DWORD Flags,
3063 PALETTEENTRY *ColorTable,
3064 IDirectDrawPalette **Palette,
3065 IUnknown *pUnkOuter)
3067 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
3068 IDirectDrawPaletteImpl *object;
3069 HRESULT hr = DDERR_GENERIC;
3070 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, ColorTable, Palette, pUnkOuter);
3072 EnterCriticalSection(&ddraw_cs);
3073 if(pUnkOuter != NULL)
3075 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
3076 LeaveCriticalSection(&ddraw_cs);
3077 return CLASS_E_NOAGGREGATION;
3080 /* The refcount test shows that a cooplevel is required for this */
3081 if(!This->cooperative_level)
3083 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3084 LeaveCriticalSection(&ddraw_cs);
3085 return DDERR_NOCOOPERATIVELEVELSET;
3088 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
3089 if(!object)
3091 ERR("Out of memory when allocating memory for a palette implementation\n");
3092 LeaveCriticalSection(&ddraw_cs);
3093 return E_OUTOFMEMORY;
3096 object->lpVtbl = &IDirectDrawPalette_Vtbl;
3097 object->ref = 1;
3098 object->ddraw_owner = This;
3100 hr = IWineD3DDevice_CreatePalette(This->wineD3DDevice, Flags,
3101 ColorTable, &object->wineD3DPalette, (IUnknown *)object);
3102 if(hr != DD_OK)
3104 HeapFree(GetProcessHeap(), 0, object);
3105 LeaveCriticalSection(&ddraw_cs);
3106 return hr;
3109 IDirectDraw7_AddRef(iface);
3110 object->ifaceToRelease = (IUnknown *) iface;
3111 *Palette = (IDirectDrawPalette *)object;
3112 LeaveCriticalSection(&ddraw_cs);
3113 return DD_OK;
3116 /*****************************************************************************
3117 * IDirectDraw7::DuplicateSurface
3119 * Duplicates a surface. The surface memory points to the same memory as
3120 * the original surface, and it's released when the last surface referencing
3121 * it is released. I guess that's beyond Wine's surface management right now
3122 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3123 * test application to implement this)
3125 * Params:
3126 * Src: Address of the source surface
3127 * Dest: Address to write the new surface pointer to
3129 * Returns:
3130 * See IDirectDraw7::CreateSurface
3132 *****************************************************************************/
3133 static HRESULT WINAPI
3134 IDirectDrawImpl_DuplicateSurface(IDirectDraw7 *iface,
3135 IDirectDrawSurface7 *Src,
3136 IDirectDrawSurface7 **Dest)
3138 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
3139 IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Src;
3141 FIXME("(%p)->(%p,%p)\n", This, Surf, Dest);
3143 /* For now, simply create a new, independent surface */
3144 return IDirectDraw7_CreateSurface(iface,
3145 &Surf->surface_desc,
3146 Dest,
3147 NULL);
3150 /*****************************************************************************
3151 * IDirectDraw7 VTable
3152 *****************************************************************************/
3153 const IDirectDraw7Vtbl IDirectDraw7_Vtbl =
3155 /*** IUnknown ***/
3156 IDirectDrawImpl_QueryInterface,
3157 IDirectDrawImpl_AddRef,
3158 IDirectDrawImpl_Release,
3159 /*** IDirectDraw ***/
3160 IDirectDrawImpl_Compact,
3161 IDirectDrawImpl_CreateClipper,
3162 IDirectDrawImpl_CreatePalette,
3163 IDirectDrawImpl_CreateSurface,
3164 IDirectDrawImpl_DuplicateSurface,
3165 IDirectDrawImpl_EnumDisplayModes,
3166 IDirectDrawImpl_EnumSurfaces,
3167 IDirectDrawImpl_FlipToGDISurface,
3168 IDirectDrawImpl_GetCaps,
3169 IDirectDrawImpl_GetDisplayMode,
3170 IDirectDrawImpl_GetFourCCCodes,
3171 IDirectDrawImpl_GetGDISurface,
3172 IDirectDrawImpl_GetMonitorFrequency,
3173 IDirectDrawImpl_GetScanLine,
3174 IDirectDrawImpl_GetVerticalBlankStatus,
3175 IDirectDrawImpl_Initialize,
3176 IDirectDrawImpl_RestoreDisplayMode,
3177 IDirectDrawImpl_SetCooperativeLevel,
3178 IDirectDrawImpl_SetDisplayMode,
3179 IDirectDrawImpl_WaitForVerticalBlank,
3180 /*** IDirectDraw2 ***/
3181 IDirectDrawImpl_GetAvailableVidMem,
3182 /*** IDirectDraw3 ***/
3183 IDirectDrawImpl_GetSurfaceFromDC,
3184 /*** IDirectDraw4 ***/
3185 IDirectDrawImpl_RestoreAllSurfaces,
3186 IDirectDrawImpl_TestCooperativeLevel,
3187 IDirectDrawImpl_GetDeviceIdentifier,
3188 /*** IDirectDraw7 ***/
3189 IDirectDrawImpl_StartModeTest,
3190 IDirectDrawImpl_EvaluateMode
3193 /*****************************************************************************
3194 * IDirectDrawImpl_FindDecl
3196 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
3197 * if none was found.
3199 * This function is in ddraw.c and the DDraw object space because D3D7
3200 * vertex buffers are created using the IDirect3D interface to the ddraw
3201 * object, so they can be valid across D3D devices(theoretically. The ddraw
3202 * object also owns the wined3d device
3204 * Parameters:
3205 * This: Device
3206 * fvf: Fvf to find the decl for
3208 * Returns:
3209 * NULL in case of an error, the IWineD3DVertexDeclaration interface for the
3210 * fvf otherwise.
3212 *****************************************************************************/
3213 IWineD3DVertexDeclaration *
3214 IDirectDrawImpl_FindDecl(IDirectDrawImpl *This,
3215 DWORD fvf)
3217 HRESULT hr;
3218 IWineD3DVertexDeclaration* pDecl = NULL;
3219 int p, low, high; /* deliberately signed */
3220 struct FvfToDecl *convertedDecls = This->decls;
3222 TRACE("Searching for declaration for fvf %08x... ", fvf);
3224 low = 0;
3225 high = This->numConvertedDecls - 1;
3226 while(low <= high) {
3227 p = (low + high) >> 1;
3228 TRACE("%d ", p);
3229 if(convertedDecls[p].fvf == fvf) {
3230 TRACE("found %p\n", convertedDecls[p].decl);
3231 return convertedDecls[p].decl;
3232 } else if(convertedDecls[p].fvf < fvf) {
3233 low = p + 1;
3234 } else {
3235 high = p - 1;
3238 TRACE("not found. Creating and inserting at position %d.\n", low);
3240 hr = IWineD3DDevice_CreateVertexDeclarationFromFVF(This->wineD3DDevice, &pDecl,
3241 (IUnknown *)This, &ddraw_null_wined3d_parent_ops, fvf);
3242 if (hr != S_OK) return NULL;
3244 if(This->declArraySize == This->numConvertedDecls) {
3245 int grow = max(This->declArraySize / 2, 8);
3246 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
3247 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
3248 if(!convertedDecls) {
3249 /* This will destroy it */
3250 IWineD3DVertexDeclaration_Release(pDecl);
3251 return NULL;
3253 This->decls = convertedDecls;
3254 This->declArraySize += grow;
3257 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
3258 convertedDecls[low].decl = pDecl;
3259 convertedDecls[low].fvf = fvf;
3260 This->numConvertedDecls++;
3262 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
3263 return pDecl;
3266 /* IWineD3DDeviceParent IUnknown methods */
3268 static inline struct IDirectDrawImpl *ddraw_from_device_parent(IWineD3DDeviceParent *iface)
3270 return (struct IDirectDrawImpl *)((char*)iface - FIELD_OFFSET(struct IDirectDrawImpl, device_parent_vtbl));
3273 static HRESULT STDMETHODCALLTYPE device_parent_QueryInterface(IWineD3DDeviceParent *iface, REFIID riid, void **object)
3275 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3276 return IDirectDrawImpl_QueryInterface((IDirectDraw7 *)This, riid, object);
3279 static ULONG STDMETHODCALLTYPE device_parent_AddRef(IWineD3DDeviceParent *iface)
3281 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3282 return IDirectDrawImpl_AddRef((IDirectDraw7 *)This);
3285 static ULONG STDMETHODCALLTYPE device_parent_Release(IWineD3DDeviceParent *iface)
3287 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3288 return IDirectDrawImpl_Release((IDirectDraw7 *)This);
3291 /* IWineD3DDeviceParent methods */
3293 static void STDMETHODCALLTYPE device_parent_WineD3DDeviceCreated(IWineD3DDeviceParent *iface, IWineD3DDevice *device)
3295 TRACE("iface %p, device %p\n", iface, device);
3298 static HRESULT STDMETHODCALLTYPE device_parent_CreateSurface(IWineD3DDeviceParent *iface,
3299 IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, DWORD usage,
3300 WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, IWineD3DSurface **surface)
3302 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3303 IDirectDrawSurfaceImpl *surf = NULL;
3304 UINT i = 0;
3305 DDSCAPS2 searchcaps = This->tex_root->surface_desc.ddsCaps;
3307 TRACE("iface %p, superior %p, width %u, height %u, format %#x, usage %#x,\n"
3308 "\tpool %#x, level %u, face %u, surface %p\n",
3309 iface, superior, width, height, format, usage, pool, level, face, surface);
3311 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
3312 switch(face)
3314 case WINED3DCUBEMAP_FACE_POSITIVE_X:
3315 TRACE("Asked for positive x\n");
3316 if (searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP)
3318 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
3320 surf = This->tex_root; break;
3321 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
3322 TRACE("Asked for negative x\n");
3323 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
3324 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
3325 TRACE("Asked for positive y\n");
3326 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
3327 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
3328 TRACE("Asked for negative y\n");
3329 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
3330 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
3331 TRACE("Asked for positive z\n");
3332 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
3333 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
3334 TRACE("Asked for negative z\n");
3335 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
3336 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
3339 if (!surf)
3341 IDirectDrawSurface7 *attached;
3342 IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)This->tex_root, &searchcaps, &attached);
3343 surf = (IDirectDrawSurfaceImpl *)attached;
3344 IDirectDrawSurface7_Release(attached);
3346 if (!surf) ERR("root search surface not found\n");
3348 /* Find the wanted mipmap. There are enough mipmaps in the chain */
3349 while (i < level)
3351 IDirectDrawSurface7 *attached;
3352 IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)surf, &searchcaps, &attached);
3353 if(!attached) ERR("Surface not found\n");
3354 surf = (IDirectDrawSurfaceImpl *)attached;
3355 IDirectDrawSurface7_Release(attached);
3356 ++i;
3359 /* Return the surface */
3360 *surface = surf->WineD3DSurface;
3361 IWineD3DSurface_AddRef(*surface);
3363 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, surf);
3365 return D3D_OK;
3368 static HRESULT STDMETHODCALLTYPE device_parent_CreateRenderTarget(IWineD3DDeviceParent *iface,
3369 IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
3370 DWORD multisample_quality, BOOL lockable, IWineD3DSurface **surface)
3372 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3373 IDirectDrawSurfaceImpl *d3d_surface = This->d3d_target;
3374 IDirectDrawSurfaceImpl *target = NULL;
3376 TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3377 "\tmultisample_quality %u, lockable %u, surface %p\n",
3378 iface, superior, width, height, format, multisample_type, multisample_quality, lockable, surface);
3380 if (d3d_surface->isRenderTarget)
3382 IDirectDrawSurface7_EnumAttachedSurfaces((IDirectDrawSurface7 *)d3d_surface, &target, findRenderTarget);
3384 else
3386 target = d3d_surface;
3389 if (!target)
3391 target = This->d3d_target;
3392 ERR(" (%p) : No DirectDrawSurface found to create the back buffer. Using the front buffer as back buffer. Uncertain consequences\n", This);
3395 /* TODO: Return failure if the dimensions do not match, but this shouldn't happen */
3397 *surface = target->WineD3DSurface;
3398 IWineD3DSurface_AddRef(*surface);
3399 target->isRenderTarget = TRUE;
3401 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, d3d_surface);
3403 return D3D_OK;
3406 static HRESULT STDMETHODCALLTYPE device_parent_CreateDepthStencilSurface(IWineD3DDeviceParent *iface,
3407 IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
3408 DWORD multisample_quality, BOOL discard, IWineD3DSurface **surface)
3410 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3411 IDirectDrawSurfaceImpl *ddraw_surface;
3412 DDSURFACEDESC2 ddsd;
3413 HRESULT hr;
3415 TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3416 "\tmultisample_quality %u, discard %u, surface %p\n",
3417 iface, superior, width, height, format, multisample_type, multisample_quality, discard, surface);
3419 *surface = NULL;
3421 /* Create a DirectDraw surface */
3422 memset(&ddsd, 0, sizeof(ddsd));
3423 ddsd.dwSize = sizeof(ddsd);
3424 ddsd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
3425 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
3426 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
3427 ddsd.dwHeight = height;
3428 ddsd.dwWidth = width;
3429 if (format)
3431 PixelFormat_WineD3DtoDD(&ddsd.u4.ddpfPixelFormat, format);
3433 else
3435 ddsd.dwFlags ^= DDSD_PIXELFORMAT;
3438 This->depthstencil = TRUE;
3439 hr = IDirectDraw7_CreateSurface((IDirectDraw7 *)This, &ddsd, (IDirectDrawSurface7 **)&ddraw_surface, NULL);
3440 This->depthstencil = FALSE;
3441 if(FAILED(hr))
3443 ERR(" (%p) Creating a DepthStencil Surface failed, result = %x\n", This, hr);
3444 return hr;
3447 *surface = ddraw_surface->WineD3DSurface;
3448 IWineD3DSurface_AddRef(*surface);
3449 IDirectDrawSurface7_Release((IDirectDrawSurface7 *)ddraw_surface);
3451 return D3D_OK;
3454 static HRESULT STDMETHODCALLTYPE device_parent_CreateVolume(IWineD3DDeviceParent *iface,
3455 IUnknown *superior, UINT width, UINT height, UINT depth, WINED3DFORMAT format,
3456 WINED3DPOOL pool, DWORD usage, IWineD3DVolume **volume)
3458 TRACE("iface %p, superior %p, width %u, height %u, depth %u, format %#x, pool %#x, usage %#x, volume %p\n",
3459 iface, superior, width, height, depth, format, pool, usage, volume);
3461 ERR("Not implemented!\n");
3463 return E_NOTIMPL;
3466 static HRESULT STDMETHODCALLTYPE device_parent_CreateSwapChain(IWineD3DDeviceParent *iface,
3467 WINED3DPRESENT_PARAMETERS *present_parameters, IWineD3DSwapChain **swapchain)
3469 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3470 IDirectDrawSurfaceImpl *iterator;
3471 IParentImpl *object;
3472 HRESULT hr;
3474 TRACE("iface %p, present_parameters %p, swapchain %p\n", iface, present_parameters, swapchain);
3476 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IParentImpl));
3477 if (!object)
3479 FIXME("Allocation of memory failed\n");
3480 *swapchain = NULL;
3481 return DDERR_OUTOFVIDEOMEMORY;
3484 object->lpVtbl = &IParent_Vtbl;
3485 object->ref = 1;
3487 hr = IWineD3DDevice_CreateSwapChain(This->wineD3DDevice, present_parameters,
3488 swapchain, (IUnknown *)object, This->ImplType);
3489 if (FAILED(hr))
3491 FIXME("(%p) CreateSwapChain failed, returning %#x\n", iface, hr);
3492 HeapFree(GetProcessHeap(), 0 , object);
3493 *swapchain = NULL;
3494 return hr;
3497 object->child = (IUnknown *)*swapchain;
3498 This->d3d_target->wineD3DSwapChain = *swapchain;
3499 iterator = This->d3d_target->complex_array[0];
3500 while (iterator)
3502 iterator->wineD3DSwapChain = *swapchain;
3503 iterator = iterator->complex_array[0];
3506 return hr;
3509 const IWineD3DDeviceParentVtbl ddraw_wined3d_device_parent_vtbl =
3511 /* IUnknown methods */
3512 device_parent_QueryInterface,
3513 device_parent_AddRef,
3514 device_parent_Release,
3515 /* IWineD3DDeviceParent methods */
3516 device_parent_WineD3DDeviceCreated,
3517 device_parent_CreateSurface,
3518 device_parent_CreateRenderTarget,
3519 device_parent_CreateDepthStencilSurface,
3520 device_parent_CreateVolume,
3521 device_parent_CreateSwapChain,