push 5b1efc32b5a8acb1d5b5e60584746392dd0c436e
[wine/hacks.git] / dlls / ddraw / ddraw.c
blob42ad0d98db061f3a700c5e5f0ea537355e46026f
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 /* Clear the cooplevel to restore window and display mode */
271 IDirectDraw7_SetCooperativeLevel((IDirectDraw7 *)This, NULL, DDSCL_NORMAL);
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 /* Unregister the window class */
282 UnregisterClassA(This->classname, 0);
284 EnterCriticalSection(&ddraw_cs);
285 list_remove(&This->ddraw_list_entry);
286 LeaveCriticalSection(&ddraw_cs);
288 /* Release the attached WineD3D stuff */
289 IWineD3DDevice_Release(This->wineD3DDevice);
290 IWineD3D_Release(This->wineD3D);
292 /* Now free the object */
293 HeapFree(GetProcessHeap(), 0, This);
296 /*****************************************************************************
297 * IDirectDraw7::Release
299 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
301 * Returns: The new refcount
302 *****************************************************************************/
303 static ULONG WINAPI
304 IDirectDrawImpl_Release(IDirectDraw7 *iface)
306 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
307 ULONG ref = InterlockedDecrement(&This->ref7);
309 TRACE("(%p)->() decrementing IDirectDraw7 refcount from %u.\n", This, ref +1);
311 if(ref == 0)
313 ULONG ifacecount = InterlockedDecrement(&This->numIfaces);
314 if(ifacecount == 0) IDirectDrawImpl_Destroy(This);
317 return ref;
320 /*****************************************************************************
321 * IDirectDraw methods
322 *****************************************************************************/
324 /*****************************************************************************
325 * IDirectDraw7::SetCooperativeLevel
327 * Sets the cooperative level for the DirectDraw object, and the window
328 * assigned to it. The cooperative level determines the general behavior
329 * of the DirectDraw application
331 * Warning: This is quite tricky, as it's not really documented which
332 * cooperative levels can be combined with each other. If a game fails
333 * after this function, try to check the cooperative levels passed on
334 * Windows, and if it returns something different.
336 * If you think that this function caused the failure because it writes a
337 * fixme, be sure to run again with a +ddraw trace.
339 * What is known about cooperative levels (See the ddraw modes test):
340 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
341 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
342 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
343 * DDSCL_FULLSCREEN can be activated
344 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
346 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
347 * DDSCL_SETFOCUSWINDOW (partially),
348 * DDSCL_MULTITHREADED (work in progress)
350 * Unhandled flags, which should be implemented
351 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
352 * expect any difference to a normal window for wine)
353 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
354 * rendering (Possible test case: Half-life)
356 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
358 * These don't seem very important for wine:
359 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
361 * Returns:
362 * DD_OK if the cooperative level was set successfully
363 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
364 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
365 * (Probably others too, have to investigate)
367 *****************************************************************************/
368 static HRESULT WINAPI
369 IDirectDrawImpl_SetCooperativeLevel(IDirectDraw7 *iface,
370 HWND hwnd,
371 DWORD cooplevel)
373 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
374 HWND window;
376 TRACE("(%p)->(%p,%08x)\n",This,hwnd,cooplevel);
377 DDRAW_dump_cooperativelevel(cooplevel);
379 EnterCriticalSection(&ddraw_cs);
381 /* Get the old window */
382 window = This->dest_window;
384 /* Tests suggest that we need one of them: */
385 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
386 DDSCL_NORMAL |
387 DDSCL_EXCLUSIVE )))
389 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
390 LeaveCriticalSection(&ddraw_cs);
391 return DDERR_INVALIDPARAMS;
394 /* Handle those levels first which set various hwnds */
395 if(cooplevel & DDSCL_SETFOCUSWINDOW)
397 /* This isn't compatible with a lot of flags */
398 if(cooplevel & ( DDSCL_MULTITHREADED |
399 DDSCL_FPUSETUP |
400 DDSCL_FPUPRESERVE |
401 DDSCL_ALLOWREBOOT |
402 DDSCL_ALLOWMODEX |
403 DDSCL_SETDEVICEWINDOW |
404 DDSCL_NORMAL |
405 DDSCL_EXCLUSIVE |
406 DDSCL_FULLSCREEN ) )
408 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
409 LeaveCriticalSection(&ddraw_cs);
410 return DDERR_INVALIDPARAMS;
412 else if( (This->cooperative_level & DDSCL_FULLSCREEN) && window)
414 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET\n");
415 LeaveCriticalSection(&ddraw_cs);
416 return DDERR_HWNDALREADYSET;
419 This->focuswindow = hwnd;
420 /* Won't use the hwnd param for anything else */
421 hwnd = NULL;
423 /* Use the focus window for drawing too */
424 This->dest_window = This->focuswindow;
426 /* Destroy the device window, if we have one */
427 if(This->devicewindow)
429 DestroyWindow(This->devicewindow);
430 This->devicewindow = NULL;
433 /* DDSCL_NORMAL or DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE */
434 if(cooplevel & DDSCL_NORMAL)
436 /* Can't coexist with fullscreen or exclusive */
437 if(cooplevel & (DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE) )
439 TRACE("(%p) DDSCL_NORMAL is not compative with DDSCL_FULLSCREEN or DDSCL_EXCLUSIVE\n", This);
440 LeaveCriticalSection(&ddraw_cs);
441 return DDERR_INVALIDPARAMS;
444 /* Switching from fullscreen? */
445 if(This->cooperative_level & DDSCL_FULLSCREEN)
447 /* Restore the display mode */
448 IDirectDraw7_RestoreDisplayMode(iface);
450 This->cooperative_level &= ~DDSCL_FULLSCREEN;
451 This->cooperative_level &= ~DDSCL_EXCLUSIVE;
452 This->cooperative_level &= ~DDSCL_ALLOWMODEX;
455 /* Don't override focus windows or private device windows */
456 if( hwnd &&
457 !(This->focuswindow) &&
458 !(This->devicewindow) &&
459 (hwnd != window) )
461 This->dest_window = hwnd;
464 else if(cooplevel & DDSCL_FULLSCREEN)
466 /* Needs DDSCL_EXCLUSIVE */
467 if(!(cooplevel & DDSCL_EXCLUSIVE) )
469 TRACE("(%p) DDSCL_FULLSCREEN needs DDSCL_EXCLUSIVE\n", This);
470 LeaveCriticalSection(&ddraw_cs);
471 return DDERR_INVALIDPARAMS;
473 /* Need a HWND
474 if(hwnd == 0)
476 TRACE("(%p) DDSCL_FULLSCREEN needs a HWND\n", This);
477 return DDERR_INVALIDPARAMS;
481 This->cooperative_level &= ~DDSCL_NORMAL;
483 /* Don't override focus windows or private device windows */
484 if( hwnd &&
485 !(This->focuswindow) &&
486 !(This->devicewindow) &&
487 (hwnd != window) )
489 BYTE buffer[32];
490 DWORD size = sizeof(buffer);
491 HKEY hkey = 0;
492 HWND drawwin = hwnd;
493 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
494 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey)) {
495 if (!RegQueryValueExA( hkey, "DirectDrawDesktopHack", 0, NULL, buffer, &size)) {
496 if ( IS_OPTION_TRUE( buffer[0] ) ) {
497 TRACE("Enabling DirectDrawDesktopHack hack\n");
498 drawwin = GetDesktopWindow();
502 This->dest_window = drawwin;
505 else if(cooplevel & DDSCL_EXCLUSIVE)
507 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN\n", This);
508 LeaveCriticalSection(&ddraw_cs);
509 return DDERR_INVALIDPARAMS;
512 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
514 /* Don't create a device window if a focus window is set */
515 if( !(This->focuswindow) )
517 HWND devicewindow = CreateWindowExA(0, This->classname, "DDraw device window",
518 WS_POPUP, 0, 0,
519 GetSystemMetrics(SM_CXSCREEN),
520 GetSystemMetrics(SM_CYSCREEN),
521 NULL, NULL, GetModuleHandleA(0), NULL);
523 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
524 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
526 This->devicewindow = devicewindow;
527 This->dest_window = devicewindow;
531 if(cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
533 /* Enable thread safety in wined3d */
534 IWineD3DDevice_SetMultithreaded(This->wineD3DDevice);
537 /* Unhandled flags */
538 if(cooplevel & DDSCL_ALLOWREBOOT)
539 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
540 if(cooplevel & DDSCL_ALLOWMODEX)
541 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
542 if(cooplevel & DDSCL_FPUSETUP)
543 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
545 /* Store the cooperative_level */
546 This->cooperative_level |= cooplevel;
547 TRACE("SetCooperativeLevel retuning DD_OK\n");
548 LeaveCriticalSection(&ddraw_cs);
549 return DD_OK;
552 /*****************************************************************************
554 * Helper function for SetDisplayMode and RestoreDisplayMode
556 * Implements DirectDraw's SetDisplayMode, but ignores the value of
557 * ForceRefreshRate, since it is already handled by
558 * IDirectDrawImpl_SetDisplayMode. RestoreDisplayMode can use this function
559 * without worrying that ForceRefreshRate will override the refresh rate. For
560 * argument and return value documentation, see
561 * IDirectDrawImpl_SetDisplayMode.
563 *****************************************************************************/
564 static HRESULT
565 IDirectDrawImpl_SetDisplayModeNoOverride(IDirectDraw7 *iface,
566 DWORD Width,
567 DWORD Height,
568 DWORD BPP,
569 DWORD RefreshRate,
570 DWORD Flags)
572 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
573 WINED3DDISPLAYMODE Mode;
574 HRESULT hr;
575 TRACE("(%p)->(%d,%d,%d,%d,%x): Relay!\n", This, Width, Height, BPP, RefreshRate, Flags);
577 EnterCriticalSection(&ddraw_cs);
578 if( !Width || !Height )
580 ERR("Width=%d, Height=%d, what to do?\n", Width, Height);
581 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
582 LeaveCriticalSection(&ddraw_cs);
583 return DD_OK;
586 /* Check the exclusive mode
587 if(!(This->cooperative_level & DDSCL_EXCLUSIVE))
588 return DDERR_NOEXCLUSIVEMODE;
589 * This is WRONG. Don't know if the SDK is completely
590 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
591 * is returned, but Half-Life 1.1.1.1 (Steam version)
592 * depends on this
595 Mode.Width = Width;
596 Mode.Height = Height;
597 Mode.RefreshRate = RefreshRate;
598 switch(BPP)
600 case 8: Mode.Format = WINED3DFMT_P8_UINT; break;
601 case 15: Mode.Format = WINED3DFMT_B5G5R5X1_UNORM; break;
602 case 16: Mode.Format = WINED3DFMT_B5G6R5_UNORM; break;
603 case 24: Mode.Format = WINED3DFMT_B8G8R8_UNORM; break;
604 case 32: Mode.Format = WINED3DFMT_B8G8R8X8_UNORM; break;
607 /* TODO: The possible return values from msdn suggest that
608 * the screen mode can't be changed if a surface is locked
609 * or some drawing is in progress
612 /* TODO: Lose the primary surface */
613 hr = IWineD3DDevice_SetDisplayMode(This->wineD3DDevice,
614 0, /* First swapchain */
615 &Mode);
616 LeaveCriticalSection(&ddraw_cs);
617 switch(hr)
619 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
620 default: return hr;
624 /*****************************************************************************
625 * IDirectDraw7::SetDisplayMode
627 * Sets the display screen resolution, color depth and refresh frequency
628 * when in fullscreen mode (in theory).
629 * Possible return values listed in the SDK suggest that this method fails
630 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
631 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
632 * It seems to be valid to pass 0 for With and Height, this has to be tested
633 * It could mean that the current video mode should be left as-is. (But why
634 * call it then?)
636 * Params:
637 * Height, Width: Screen dimension
638 * BPP: Color depth in Bits per pixel
639 * Refreshrate: Screen refresh rate
640 * Flags: Other stuff
642 * Returns
643 * DD_OK on success
645 *****************************************************************************/
646 static HRESULT WINAPI
647 IDirectDrawImpl_SetDisplayMode(IDirectDraw7 *iface,
648 DWORD Width,
649 DWORD Height,
650 DWORD BPP,
651 DWORD RefreshRate,
652 DWORD Flags)
654 if (force_refresh_rate != 0)
656 TRACE("ForceRefreshRate overriding passed-in refresh rate (%d Hz) to %d Hz\n", RefreshRate, force_refresh_rate);
657 RefreshRate = force_refresh_rate;
660 return IDirectDrawImpl_SetDisplayModeNoOverride(iface, Width, Height, BPP,
661 RefreshRate, Flags);
664 /*****************************************************************************
665 * IDirectDraw7::RestoreDisplayMode
667 * Restores the display mode to what it was at creation time. Basically.
669 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
670 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
671 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
672 * -> DD_1 is released. The screen should be left at 1024x768x32.
673 * -> DD_2 is released. The screen should be set to 1400x1050x32
674 * This case is unhandled right now, but Empire Earth does it this way.
675 * (But perhaps there is something in SetCooperativeLevel to prevent this)
677 * The msdn says that this method resets the display mode to what it was before
678 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
680 * Returns
681 * DD_OK on success
682 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
684 *****************************************************************************/
685 static HRESULT WINAPI
686 IDirectDrawImpl_RestoreDisplayMode(IDirectDraw7 *iface)
688 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
689 TRACE("(%p)\n", This);
691 return IDirectDrawImpl_SetDisplayModeNoOverride(iface,
692 This->orig_width, This->orig_height, This->orig_bpp, 0, 0);
695 /*****************************************************************************
696 * IDirectDraw7::GetCaps
698 * Returns the drives capabilities
700 * Used for version 1, 2, 4 and 7
702 * Params:
703 * DriverCaps: Structure to write the Hardware accelerated caps to
704 * HelCaps: Structure to write the emulation caps to
706 * Returns
707 * This implementation returns DD_OK only
709 *****************************************************************************/
710 static HRESULT WINAPI
711 IDirectDrawImpl_GetCaps(IDirectDraw7 *iface,
712 DDCAPS *DriverCaps,
713 DDCAPS *HELCaps)
715 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
716 DDCAPS caps;
717 WINED3DCAPS winecaps;
718 HRESULT hr;
719 DDSCAPS2 ddscaps = {0, 0, 0, 0};
720 TRACE("(%p)->(%p,%p)\n", This, DriverCaps, HELCaps);
722 /* One structure must be != NULL */
723 if( (!DriverCaps) && (!HELCaps) )
725 ERR("(%p) Invalid params to IDirectDrawImpl_GetCaps\n", This);
726 return DDERR_INVALIDPARAMS;
729 memset(&caps, 0, sizeof(caps));
730 memset(&winecaps, 0, sizeof(winecaps));
731 caps.dwSize = sizeof(caps);
732 EnterCriticalSection(&ddraw_cs);
733 hr = IWineD3DDevice_GetDeviceCaps(This->wineD3DDevice, &winecaps);
734 if(FAILED(hr)) {
735 WARN("IWineD3DDevice::GetDeviceCaps failed\n");
736 LeaveCriticalSection(&ddraw_cs);
737 return hr;
740 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
741 LeaveCriticalSection(&ddraw_cs);
742 if(FAILED(hr)) {
743 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
744 return hr;
747 caps.dwCaps = winecaps.DirectDrawCaps.Caps;
748 caps.dwCaps2 = winecaps.DirectDrawCaps.Caps2;
749 caps.dwCKeyCaps = winecaps.DirectDrawCaps.CKeyCaps;
750 caps.dwFXCaps = winecaps.DirectDrawCaps.FXCaps;
751 caps.dwPalCaps = winecaps.DirectDrawCaps.PalCaps;
752 caps.ddsCaps.dwCaps = winecaps.DirectDrawCaps.ddsCaps;
753 caps.dwSVBCaps = winecaps.DirectDrawCaps.SVBCaps;
754 caps.dwSVBCKeyCaps = winecaps.DirectDrawCaps.SVBCKeyCaps;
755 caps.dwSVBFXCaps = winecaps.DirectDrawCaps.SVBFXCaps;
756 caps.dwVSBCaps = winecaps.DirectDrawCaps.VSBCaps;
757 caps.dwVSBCKeyCaps = winecaps.DirectDrawCaps.VSBCKeyCaps;
758 caps.dwVSBFXCaps = winecaps.DirectDrawCaps.VSBFXCaps;
759 caps.dwSSBCaps = winecaps.DirectDrawCaps.SSBCaps;
760 caps.dwSSBCKeyCaps = winecaps.DirectDrawCaps.SSBCKeyCaps;
761 caps.dwSSBFXCaps = winecaps.DirectDrawCaps.SSBFXCaps;
763 /* Even if WineD3D supports 3D rendering, remove the cap if ddraw is configured
764 * not to use it
766 if(DefaultSurfaceType == SURFACE_GDI) {
767 caps.dwCaps &= ~DDCAPS_3D;
768 caps.ddsCaps.dwCaps &= ~(DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER);
770 if(winecaps.DirectDrawCaps.StrideAlign != 0) {
771 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
772 caps.dwAlignStrideAlign = winecaps.DirectDrawCaps.StrideAlign;
775 if(DriverCaps)
777 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
778 if (TRACE_ON(ddraw))
780 TRACE("Driver Caps :\n");
781 DDRAW_dump_DDCAPS(DriverCaps);
785 if(HELCaps)
787 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
788 if (TRACE_ON(ddraw))
790 TRACE("HEL Caps :\n");
791 DDRAW_dump_DDCAPS(HELCaps);
795 return DD_OK;
798 /*****************************************************************************
799 * IDirectDraw7::Compact
801 * No idea what it does, MSDN says it's not implemented.
803 * Returns
804 * DD_OK, but this is unchecked
806 *****************************************************************************/
807 static HRESULT WINAPI
808 IDirectDrawImpl_Compact(IDirectDraw7 *iface)
810 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
811 TRACE("(%p)\n", This);
813 return DD_OK;
816 /*****************************************************************************
817 * IDirectDraw7::GetDisplayMode
819 * Returns information about the current display mode
821 * Exists in Version 1, 2, 4 and 7
823 * Params:
824 * DDSD: Address of a surface description structure to write the info to
826 * Returns
827 * DD_OK
829 *****************************************************************************/
830 static HRESULT WINAPI
831 IDirectDrawImpl_GetDisplayMode(IDirectDraw7 *iface,
832 DDSURFACEDESC2 *DDSD)
834 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
835 HRESULT hr;
836 WINED3DDISPLAYMODE Mode;
837 DWORD Size;
838 TRACE("(%p)->(%p): Relay\n", This, DDSD);
840 EnterCriticalSection(&ddraw_cs);
841 /* This seems sane */
842 if (!DDSD)
844 LeaveCriticalSection(&ddraw_cs);
845 return DDERR_INVALIDPARAMS;
848 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
849 * so one method can be used for all versions (Hopefully)
851 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
852 0 /* swapchain 0 */,
853 &Mode);
854 if( hr != D3D_OK )
856 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
857 LeaveCriticalSection(&ddraw_cs);
858 return hr;
861 Size = DDSD->dwSize;
862 memset(DDSD, 0, Size);
864 DDSD->dwSize = Size;
865 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
866 DDSD->dwWidth = Mode.Width;
867 DDSD->dwHeight = Mode.Height;
868 DDSD->u2.dwRefreshRate = 60;
869 DDSD->ddsCaps.dwCaps = 0;
870 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
871 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, Mode.Format);
872 DDSD->u1.lPitch = Mode.Width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
874 if(TRACE_ON(ddraw))
876 TRACE("Returning surface desc :\n");
877 DDRAW_dump_surface_desc(DDSD);
880 LeaveCriticalSection(&ddraw_cs);
881 return DD_OK;
884 /*****************************************************************************
885 * IDirectDraw7::GetFourCCCodes
887 * Returns an array of supported FourCC codes.
889 * Exists in Version 1, 2, 4 and 7
891 * Params:
892 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
893 * of enumerated codes
894 * Codes: Pointer to an array of DWORDs where the supported codes are written
895 * to
897 * Returns
898 * Always returns DD_OK, as it's a stub for now
900 *****************************************************************************/
901 static HRESULT WINAPI
902 IDirectDrawImpl_GetFourCCCodes(IDirectDraw7 *iface,
903 DWORD *NumCodes, DWORD *Codes)
905 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
906 WINED3DFORMAT formats[] = {
907 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
908 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
909 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
911 DWORD count = 0, i, outsize;
912 HRESULT hr;
913 WINED3DDISPLAYMODE d3ddm;
914 WINED3DSURFTYPE type = This->ImplType;
915 TRACE("(%p)->(%p, %p)\n", This, NumCodes, Codes);
917 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
918 0 /* swapchain 0 */,
919 &d3ddm);
921 outsize = NumCodes && Codes ? *NumCodes : 0;
923 if(type == SURFACE_UNKNOWN) type = SURFACE_GDI;
925 for(i = 0; i < (sizeof(formats) / sizeof(formats[0])); i++) {
926 hr = IWineD3D_CheckDeviceFormat(This->wineD3D,
927 WINED3DADAPTER_DEFAULT,
928 WINED3DDEVTYPE_HAL,
929 d3ddm.Format /* AdapterFormat */,
930 0 /* usage */,
931 WINED3DRTYPE_SURFACE,
932 formats[i],
933 type);
934 if(SUCCEEDED(hr)) {
935 if(count < outsize) {
936 Codes[count] = formats[i];
938 count++;
941 if(NumCodes) {
942 TRACE("Returning %u FourCC codes\n", count);
943 *NumCodes = count;
946 return DD_OK;
949 /*****************************************************************************
950 * IDirectDraw7::GetMonitorFrequency
952 * Returns the monitor's frequency
954 * Exists in Version 1, 2, 4 and 7
956 * Params:
957 * Freq: Pointer to a DWORD to write the frequency to
959 * Returns
960 * Always returns DD_OK
962 *****************************************************************************/
963 static HRESULT WINAPI
964 IDirectDrawImpl_GetMonitorFrequency(IDirectDraw7 *iface,
965 DWORD *Freq)
967 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
968 TRACE("(%p)->(%p)\n", This, Freq);
970 /* Ideally this should be in WineD3D, as it concerns the screen setup,
971 * but for now this should make the games happy
973 *Freq = 60;
974 return DD_OK;
977 /*****************************************************************************
978 * IDirectDraw7::GetVerticalBlankStatus
980 * Returns the Vertical blank status of the monitor. This should be in WineD3D
981 * too basically, but as it's a semi stub, I didn't create a function there
983 * Params:
984 * status: Pointer to a BOOL to be filled with the vertical blank status
986 * Returns
987 * DD_OK on success
988 * DDERR_INVALIDPARAMS if status is NULL
990 *****************************************************************************/
991 static HRESULT WINAPI
992 IDirectDrawImpl_GetVerticalBlankStatus(IDirectDraw7 *iface,
993 BOOL *status)
995 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
996 TRACE("(%p)->(%p)\n", This, status);
998 /* This looks sane, the MSDN suggests it too */
999 EnterCriticalSection(&ddraw_cs);
1000 if(!status)
1002 LeaveCriticalSection(&ddraw_cs);
1003 return DDERR_INVALIDPARAMS;
1006 *status = This->fake_vblank;
1007 This->fake_vblank = !This->fake_vblank;
1008 LeaveCriticalSection(&ddraw_cs);
1009 return DD_OK;
1012 /*****************************************************************************
1013 * IDirectDraw7::GetAvailableVidMem
1015 * Returns the total and free video memory
1017 * Params:
1018 * Caps: Specifies the memory type asked for
1019 * total: Pointer to a DWORD to be filled with the total memory
1020 * free: Pointer to a DWORD to be filled with the free memory
1022 * Returns
1023 * DD_OK on success
1024 * DDERR_INVALIDPARAMS of free and total are NULL
1026 *****************************************************************************/
1027 static HRESULT WINAPI
1028 IDirectDrawImpl_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total, DWORD *free)
1030 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1031 TRACE("(%p)->(%p, %p, %p)\n", This, Caps, total, free);
1033 if(TRACE_ON(ddraw))
1035 TRACE("(%p) Asked for memory with description: ", This);
1036 DDRAW_dump_DDSCAPS2(Caps);
1038 EnterCriticalSection(&ddraw_cs);
1040 /* Todo: System memory vs local video memory vs non-local video memory
1041 * The MSDN also mentions differences between texture memory and other
1042 * resources, but that's not important
1045 if( (!total) && (!free) )
1047 LeaveCriticalSection(&ddraw_cs);
1048 return DDERR_INVALIDPARAMS;
1051 if(total) *total = This->total_vidmem;
1052 if(free) *free = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
1054 LeaveCriticalSection(&ddraw_cs);
1055 return DD_OK;
1058 /*****************************************************************************
1059 * IDirectDraw7::Initialize
1061 * Initializes a DirectDraw interface.
1063 * Params:
1064 * GUID: Interface identifier. Well, don't know what this is really good
1065 * for
1067 * Returns
1068 * Returns DD_OK on the first call,
1069 * DDERR_ALREADYINITIALIZED on repeated calls
1071 *****************************************************************************/
1072 static HRESULT WINAPI
1073 IDirectDrawImpl_Initialize(IDirectDraw7 *iface,
1074 GUID *Guid)
1076 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1077 TRACE("(%p)->(%s): No-op\n", This, debugstr_guid(Guid));
1079 if(This->initialized)
1081 return DDERR_ALREADYINITIALIZED;
1083 else
1085 return DD_OK;
1089 /*****************************************************************************
1090 * IDirectDraw7::FlipToGDISurface
1092 * "Makes the surface that the GDI writes to the primary surface"
1093 * Looks like some windows specific thing we don't have to care about.
1094 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1095 * show error boxes ;)
1096 * Well, just return DD_OK.
1098 * Returns:
1099 * Always returns DD_OK
1101 *****************************************************************************/
1102 static HRESULT WINAPI
1103 IDirectDrawImpl_FlipToGDISurface(IDirectDraw7 *iface)
1105 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1106 TRACE("(%p)\n", This);
1108 return DD_OK;
1111 /*****************************************************************************
1112 * IDirectDraw7::WaitForVerticalBlank
1114 * This method allows applications to get in sync with the vertical blank
1115 * interval.
1116 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1117 * redraw the screen, most likely because of this stub
1119 * Parameters:
1120 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1121 * or DDWAITVB_BLOCKEND
1122 * h: Not used, according to MSDN
1124 * Returns:
1125 * Always returns DD_OK
1127 *****************************************************************************/
1128 static HRESULT WINAPI
1129 IDirectDrawImpl_WaitForVerticalBlank(IDirectDraw7 *iface,
1130 DWORD Flags,
1131 HANDLE h)
1133 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1134 static BOOL hide = FALSE;
1136 /* This function is called often, so print the fixme only once */
1137 if(!hide)
1139 FIXME("(%p)->(%x,%p): Stub\n", This, Flags, h);
1140 hide = TRUE;
1143 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1144 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1145 return DDERR_UNSUPPORTED; /* unchecked */
1147 return DD_OK;
1150 /*****************************************************************************
1151 * IDirectDraw7::GetScanLine
1153 * Returns the scan line that is being drawn on the monitor
1155 * Parameters:
1156 * Scanline: Address to write the scan line value to
1158 * Returns:
1159 * Always returns DD_OK
1161 *****************************************************************************/
1162 static HRESULT WINAPI IDirectDrawImpl_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1164 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1165 static BOOL hide = FALSE;
1166 WINED3DDISPLAYMODE Mode;
1168 /* This function is called often, so print the fixme only once */
1169 EnterCriticalSection(&ddraw_cs);
1170 if(!hide)
1172 FIXME("(%p)->(%p): Semi-Stub\n", This, Scanline);
1173 hide = TRUE;
1176 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1178 &Mode);
1180 /* Fake the line sweeping of the monitor */
1181 /* FIXME: We should synchronize with a source to keep the refresh rate */
1182 *Scanline = This->cur_scanline++;
1183 /* Assume 20 scan lines in the vertical blank */
1184 if (This->cur_scanline >= Mode.Height + 20)
1185 This->cur_scanline = 0;
1187 LeaveCriticalSection(&ddraw_cs);
1188 return DD_OK;
1191 /*****************************************************************************
1192 * IDirectDraw7::TestCooperativeLevel
1194 * Informs the application about the state of the video adapter, depending
1195 * on the cooperative level
1197 * Returns:
1198 * DD_OK if the device is in a sane state
1199 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1200 * if the state is not correct(See below)
1202 *****************************************************************************/
1203 static HRESULT WINAPI
1204 IDirectDrawImpl_TestCooperativeLevel(IDirectDraw7 *iface)
1206 TRACE("iface %p.\n", iface);
1208 return DD_OK;
1211 /*****************************************************************************
1212 * IDirectDraw7::GetGDISurface
1214 * Returns the surface that GDI is treating as the primary surface.
1215 * For Wine this is the front buffer
1217 * Params:
1218 * GDISurface: Address to write the surface pointer to
1220 * Returns:
1221 * DD_OK if the surface was found
1222 * DDERR_NOTFOUND if the GDI surface wasn't found
1224 *****************************************************************************/
1225 static HRESULT WINAPI
1226 IDirectDrawImpl_GetGDISurface(IDirectDraw7 *iface,
1227 IDirectDrawSurface7 **GDISurface)
1229 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1230 IWineD3DSurface *Surf;
1231 IDirectDrawSurface7 *ddsurf;
1232 HRESULT hr;
1233 DDSCAPS2 ddsCaps;
1234 TRACE("(%p)->(%p)\n", This, GDISurface);
1236 /* Get the back buffer from the wineD3DDevice and search its
1237 * attached surfaces for the front buffer
1239 EnterCriticalSection(&ddraw_cs);
1240 hr = IWineD3DDevice_GetBackBuffer(This->wineD3DDevice,
1241 0, /* SwapChain */
1242 0, /* first back buffer*/
1243 WINED3DBACKBUFFER_TYPE_MONO,
1244 &Surf);
1246 if( (hr != D3D_OK) ||
1247 (!Surf) )
1249 ERR("IWineD3DDevice::GetBackBuffer failed\n");
1250 LeaveCriticalSection(&ddraw_cs);
1251 return DDERR_NOTFOUND;
1254 /* GetBackBuffer AddRef()ed the surface, release it */
1255 IWineD3DSurface_Release(Surf);
1257 IWineD3DSurface_GetParent(Surf,
1258 (IUnknown **) &ddsurf);
1259 IDirectDrawSurface7_Release(ddsurf); /* For the GetParent */
1261 /* Find the front buffer */
1262 ddsCaps.dwCaps = DDSCAPS_FRONTBUFFER;
1263 hr = IDirectDrawSurface7_GetAttachedSurface(ddsurf,
1264 &ddsCaps,
1265 GDISurface);
1266 if(hr != DD_OK)
1268 ERR("IDirectDrawSurface7::GetAttachedSurface failed, hr = %x\n", hr);
1271 /* The AddRef is OK this time */
1272 LeaveCriticalSection(&ddraw_cs);
1273 return hr;
1276 /*****************************************************************************
1277 * IDirectDraw7::EnumDisplayModes
1279 * Enumerates the supported Display modes. The modes can be filtered with
1280 * the DDSD parameter.
1282 * Params:
1283 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES
1284 * DDSD: Surface description to filter the modes
1285 * Context: Pointer passed back to the callback function
1286 * cb: Application-provided callback function
1288 * Returns:
1289 * DD_OK on success
1290 * DDERR_INVALIDPARAMS if the callback wasn't set
1292 *****************************************************************************/
1293 static HRESULT WINAPI
1294 IDirectDrawImpl_EnumDisplayModes(IDirectDraw7 *iface,
1295 DWORD Flags,
1296 DDSURFACEDESC2 *DDSD,
1297 void *Context,
1298 LPDDENUMMODESCALLBACK2 cb)
1300 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1301 unsigned int modenum, fmt;
1302 WINED3DFORMAT pixelformat = WINED3DFMT_UNKNOWN;
1303 WINED3DDISPLAYMODE mode;
1304 DDSURFACEDESC2 callback_sd;
1305 WINED3DDISPLAYMODE *enum_modes = NULL;
1306 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
1308 WINED3DFORMAT checkFormatList[] =
1310 WINED3DFMT_B8G8R8X8_UNORM,
1311 WINED3DFMT_B5G6R5_UNORM,
1312 WINED3DFMT_P8_UINT,
1315 TRACE("(%p)->(%p,%p,%p): Relay\n", This, DDSD, Context, cb);
1317 EnterCriticalSection(&ddraw_cs);
1318 /* This looks sane */
1319 if(!cb)
1321 LeaveCriticalSection(&ddraw_cs);
1322 return DDERR_INVALIDPARAMS;
1325 if(DDSD)
1327 if ((DDSD->dwFlags & DDSD_PIXELFORMAT) && (DDSD->u4.ddpfPixelFormat.dwFlags & DDPF_RGB) )
1328 pixelformat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
1331 if(!(Flags & DDEDM_REFRESHRATES))
1333 enum_mode_array_size = 16;
1334 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1335 if (!enum_modes)
1337 ERR("Out of memory\n");
1338 LeaveCriticalSection(&ddraw_cs);
1339 return DDERR_OUTOFMEMORY;
1343 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
1345 if(pixelformat != WINED3DFMT_UNKNOWN && checkFormatList[fmt] != pixelformat)
1347 continue;
1350 modenum = 0;
1351 while(IWineD3D_EnumAdapterModes(This->wineD3D,
1352 WINED3DADAPTER_DEFAULT,
1353 checkFormatList[fmt],
1354 modenum++,
1355 &mode) == WINED3D_OK)
1357 if(DDSD)
1359 if(DDSD->dwFlags & DDSD_WIDTH && mode.Width != DDSD->dwWidth) continue;
1360 if(DDSD->dwFlags & DDSD_HEIGHT && mode.Height != DDSD->dwHeight) continue;
1363 if(!(Flags & DDEDM_REFRESHRATES))
1365 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
1366 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
1367 * to be reduced to a single unique result in such case.
1369 BOOL found = FALSE;
1370 unsigned i;
1372 for (i = 0; i < enum_mode_count; i++)
1374 if(enum_modes[i].Width == mode.Width && enum_modes[i].Height == mode.Height &&
1375 enum_modes[i].Format == mode.Format)
1377 found = TRUE;
1378 break;
1382 if(found) continue;
1385 memset(&callback_sd, 0, sizeof(callback_sd));
1386 callback_sd.dwSize = sizeof(callback_sd);
1387 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1389 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH;
1390 if(Flags & DDEDM_REFRESHRATES)
1392 callback_sd.dwFlags |= DDSD_REFRESHRATE;
1393 callback_sd.u2.dwRefreshRate = mode.RefreshRate;
1396 callback_sd.dwWidth = mode.Width;
1397 callback_sd.dwHeight = mode.Height;
1399 PixelFormat_WineD3DtoDD(&callback_sd.u4.ddpfPixelFormat, mode.Format);
1401 /* Calc pitch and DWORD align like MSDN says */
1402 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.Width;
1403 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
1405 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
1406 callback_sd.u2.dwRefreshRate);
1408 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
1410 TRACE("Application asked to terminate the enumeration\n");
1411 HeapFree(GetProcessHeap(), 0, enum_modes);
1412 LeaveCriticalSection(&ddraw_cs);
1413 return DD_OK;
1416 if(!(Flags & DDEDM_REFRESHRATES))
1418 if (enum_mode_count == enum_mode_array_size)
1420 WINED3DDISPLAYMODE *new_enum_modes;
1422 enum_mode_array_size *= 2;
1423 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1425 if (!new_enum_modes)
1427 ERR("Out of memory\n");
1428 HeapFree(GetProcessHeap(), 0, enum_modes);
1429 LeaveCriticalSection(&ddraw_cs);
1430 return DDERR_OUTOFMEMORY;
1433 enum_modes = new_enum_modes;
1436 enum_modes[enum_mode_count++] = mode;
1441 TRACE("End of enumeration\n");
1442 HeapFree(GetProcessHeap(), 0, enum_modes);
1443 LeaveCriticalSection(&ddraw_cs);
1444 return DD_OK;
1447 /*****************************************************************************
1448 * IDirectDraw7::EvaluateMode
1450 * Used with IDirectDraw7::StartModeTest to test video modes.
1451 * EvaluateMode is used to pass or fail a mode, and continue with the next
1452 * mode
1454 * Params:
1455 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
1456 * Timeout: Returns the amount of seconds left before the mode would have
1457 * been failed automatically
1459 * Returns:
1460 * This implementation always DD_OK, because it's a stub
1462 *****************************************************************************/
1463 static HRESULT WINAPI
1464 IDirectDrawImpl_EvaluateMode(IDirectDraw7 *iface,
1465 DWORD Flags,
1466 DWORD *Timeout)
1468 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1469 FIXME("(%p)->(%d,%p): Stub!\n", This, Flags, Timeout);
1471 /* When implementing this, implement it in WineD3D */
1473 return DD_OK;
1476 /*****************************************************************************
1477 * IDirectDraw7::GetDeviceIdentifier
1479 * Returns the device identifier, which gives information about the driver
1480 * Our device identifier is defined at the beginning of this file.
1482 * Params:
1483 * DDDI: Address for the returned structure
1484 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
1486 * Returns:
1487 * On success it returns DD_OK
1488 * DDERR_INVALIDPARAMS if DDDI is NULL
1490 *****************************************************************************/
1491 static HRESULT WINAPI
1492 IDirectDrawImpl_GetDeviceIdentifier(IDirectDraw7 *iface,
1493 DDDEVICEIDENTIFIER2 *DDDI,
1494 DWORD Flags)
1496 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1497 TRACE("(%p)->(%p,%08x)\n", This, DDDI, Flags);
1499 if(!DDDI)
1500 return DDERR_INVALIDPARAMS;
1502 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
1503 * host adapter, if there's a secondary 3D adapter. This doesn't apply
1504 * to any modern hardware, nor is it interesting for Wine, so ignore it.
1505 * Size of DDDEVICEIDENTIFIER2 may be aligned to 8 bytes and thus 4
1506 * bytes too long. So only copy the relevant part of the structure
1509 memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
1510 return DD_OK;
1513 /*****************************************************************************
1514 * IDirectDraw7::GetSurfaceFromDC
1516 * Returns the Surface for a GDI device context handle.
1517 * Is this related to IDirectDrawSurface::GetDC ???
1519 * Params:
1520 * hdc: hdc to return the surface for
1521 * Surface: Address to write the surface pointer to
1523 * Returns:
1524 * Always returns DD_OK because it's a stub
1526 *****************************************************************************/
1527 static HRESULT WINAPI
1528 IDirectDrawImpl_GetSurfaceFromDC(IDirectDraw7 *iface,
1529 HDC hdc,
1530 IDirectDrawSurface7 **Surface)
1532 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1533 FIXME("(%p)->(%p,%p): Stub!\n", This, hdc, Surface);
1535 /* Implementation idea if needed: Loop through all surfaces and compare
1536 * their hdc with hdc. Implement it in WineD3D! */
1537 return DDERR_NOTFOUND;
1540 /*****************************************************************************
1541 * IDirectDraw7::RestoreAllSurfaces
1543 * Calls the restore method of all surfaces
1545 * Params:
1547 * Returns:
1548 * Always returns DD_OK because it's a stub
1550 *****************************************************************************/
1551 static HRESULT WINAPI
1552 IDirectDrawImpl_RestoreAllSurfaces(IDirectDraw7 *iface)
1554 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1555 FIXME("(%p): Stub\n", This);
1557 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
1558 * get their parent and call their restore method. Do not implement
1559 * it in WineD3D, as restoring a surface means re-creating the
1560 * WineD3DDSurface
1562 return DD_OK;
1565 /*****************************************************************************
1566 * IDirectDraw7::StartModeTest
1568 * Tests the specified video modes to update the system registry with
1569 * refresh rate information. StartModeTest starts the mode test,
1570 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
1571 * isn't called within 15 seconds, the mode is failed automatically
1573 * As refresh rates are handled by the X server, I don't think this
1574 * Method is important
1576 * Params:
1577 * Modes: An array of mode specifications
1578 * NumModes: The number of modes in Modes
1579 * Flags: Some flags...
1581 * Returns:
1582 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
1583 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
1584 * otherwise DD_OK
1586 *****************************************************************************/
1587 static HRESULT WINAPI
1588 IDirectDrawImpl_StartModeTest(IDirectDraw7 *iface,
1589 SIZE *Modes,
1590 DWORD NumModes,
1591 DWORD Flags)
1593 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1594 WARN("(%p)->(%p, %d, %x): Semi-Stub, most likely harmless\n", This, Modes, NumModes, Flags);
1596 /* This looks sane */
1597 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
1599 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
1600 * As it is not, DDERR_TESTFINISHED is returned
1601 * (hopefully that's correct
1603 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
1604 * well, that value doesn't (yet) exist in the wine headers, so ignore it
1607 return DD_OK;
1610 /*****************************************************************************
1611 * IDirectDrawImpl_RecreateSurfacesCallback
1613 * Enumeration callback for IDirectDrawImpl_RecreateAllSurfaces.
1614 * It re-recreates the WineD3DSurface. It's pretty straightforward
1616 *****************************************************************************/
1617 HRESULT WINAPI
1618 IDirectDrawImpl_RecreateSurfacesCallback(IDirectDrawSurface7 *surf,
1619 DDSURFACEDESC2 *desc,
1620 void *Context)
1622 IDirectDrawSurfaceImpl *surfImpl = (IDirectDrawSurfaceImpl *)surf;
1623 IDirectDrawImpl *This = surfImpl->ddraw;
1624 IUnknown *Parent;
1625 IWineD3DSurface *wineD3DSurface;
1626 IWineD3DSwapChain *swapchain;
1627 HRESULT hr;
1628 IWineD3DClipper *clipper = NULL;
1630 WINED3DSURFACE_DESC Desc;
1631 WINED3DFORMAT Format;
1632 DWORD Usage;
1633 WINED3DPOOL Pool;
1635 WINED3DMULTISAMPLE_TYPE MultiSampleType;
1636 DWORD MultiSampleQuality;
1637 UINT Width;
1638 UINT Height;
1640 TRACE("(%p): Enumerated Surface %p\n", This, surfImpl);
1642 /* For the enumeration */
1643 IDirectDrawSurface7_Release(surf);
1645 if(surfImpl->ImplType == This->ImplType) return DDENUMRET_OK; /* Continue */
1647 /* Get the objects */
1648 swapchain = surfImpl->wineD3DSwapChain;
1649 surfImpl->wineD3DSwapChain = NULL;
1650 wineD3DSurface = surfImpl->WineD3DSurface;
1652 /* get the clipper */
1653 IWineD3DSurface_GetClipper(wineD3DSurface, &clipper);
1655 /* Get the surface properties */
1656 hr = IWineD3DSurface_GetDesc(wineD3DSurface, &Desc);
1657 if(hr != D3D_OK) return hr;
1659 Format = Desc.format;
1660 Usage = Desc.usage;
1661 Pool = Desc.pool;
1662 MultiSampleType = Desc.multisample_type;
1663 MultiSampleQuality = Desc.multisample_quality;
1664 Width = Desc.width;
1665 Height = Desc.height;
1667 IWineD3DSurface_GetParent(wineD3DSurface, &Parent);
1669 /* Create the new surface */
1670 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice, Width, Height, Format,
1671 TRUE /* Lockable */, FALSE /* Discard */, surfImpl->mipmap_level, &surfImpl->WineD3DSurface, Usage, Pool,
1672 MultiSampleType, MultiSampleQuality, This->ImplType, Parent, &ddraw_null_wined3d_parent_ops);
1673 IUnknown_Release(Parent);
1675 if(hr != D3D_OK)
1676 return hr;
1678 IWineD3DSurface_SetClipper(surfImpl->WineD3DSurface, clipper);
1680 /* TODO: Copy the surface content, except for render targets */
1682 /* If there's a swapchain, it owns the wined3d surfaces. So Destroy
1683 * the swapchain
1685 if(swapchain) {
1686 /* The backbuffers have the swapchain set as well, but the primary
1687 * owns it and destroys it
1689 if(surfImpl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
1690 IWineD3DDevice_UninitGDI(This->wineD3DDevice, D3D7CB_DestroySwapChain);
1692 surfImpl->isRenderTarget = FALSE;
1693 } else {
1694 if(IWineD3DSurface_Release(wineD3DSurface) == 0)
1695 TRACE("Surface released successful, next surface\n");
1696 else
1697 ERR("Something's still holding the old WineD3DSurface\n");
1700 surfImpl->ImplType = This->ImplType;
1702 if(clipper)
1704 IWineD3DClipper_Release(clipper);
1706 return DDENUMRET_OK;
1709 /*****************************************************************************
1710 * IDirectDrawImpl_RecreateAllSurfaces
1712 * A function, that converts all wineD3DSurfaces to the new implementation type
1713 * It enumerates all surfaces with IWineD3DDevice::EnumSurfaces, creates a
1714 * new WineD3DSurface, copies the content and releases the old surface
1716 *****************************************************************************/
1717 static HRESULT
1718 IDirectDrawImpl_RecreateAllSurfaces(IDirectDrawImpl *This)
1720 DDSURFACEDESC2 desc;
1721 TRACE("(%p): Switch to implementation %d\n", This, This->ImplType);
1723 if(This->ImplType != SURFACE_OPENGL && This->d3d_initialized)
1725 /* Should happen almost never */
1726 FIXME("(%p) Switching to non-opengl surfaces with d3d started. Is this a bug?\n", This);
1727 /* Shutdown d3d */
1728 IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroySwapChain);
1730 /* Contrary: D3D starting is handled by the caller, because it knows the render target */
1732 memset(&desc, 0, sizeof(desc));
1733 desc.dwSize = sizeof(desc);
1735 return IDirectDraw7_EnumSurfaces((IDirectDraw7 *)This, 0, &desc, This, IDirectDrawImpl_RecreateSurfacesCallback);
1738 ULONG WINAPI D3D7CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
1739 IUnknown* swapChainParent;
1740 TRACE("(%p) call back\n", pSwapChain);
1742 IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
1743 IUnknown_Release(swapChainParent);
1744 return IUnknown_Release(swapChainParent);
1747 /*****************************************************************************
1748 * IDirectDrawImpl_CreateNewSurface
1750 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
1751 * with the passed parameters.
1753 * Params:
1754 * DDSD: Description of the surface to create
1755 * Surf: Address to store the interface pointer at
1757 * Returns:
1758 * DD_OK on success
1760 *****************************************************************************/
1761 static HRESULT
1762 IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This,
1763 DDSURFACEDESC2 *pDDSD,
1764 IDirectDrawSurfaceImpl **ppSurf,
1765 UINT level)
1767 HRESULT hr;
1768 UINT Width, Height;
1769 WINED3DFORMAT Format = WINED3DFMT_UNKNOWN;
1770 DWORD Usage = 0;
1771 WINED3DSURFTYPE ImplType = This->ImplType;
1772 WINED3DSURFACE_DESC Desc;
1773 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
1775 if (TRACE_ON(ddraw))
1777 TRACE(" (%p) Requesting surface desc :\n", This);
1778 DDRAW_dump_surface_desc(pDDSD);
1781 /* Select the surface type, if it wasn't choosen yet */
1782 if(ImplType == SURFACE_UNKNOWN)
1784 /* Use GL Surfaces if a D3DDEVICE Surface is requested */
1785 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1787 TRACE("(%p) Choosing GL surfaces because a 3DDEVICE Surface was requested\n", This);
1788 ImplType = SURFACE_OPENGL;
1791 /* Otherwise use GDI surfaces for now */
1792 else
1794 TRACE("(%p) Choosing GDI surfaces for 2D rendering\n", This);
1795 ImplType = SURFACE_GDI;
1798 /* Policy if all surface implementations are available:
1799 * First, check if a default type was set with winecfg. If not,
1800 * try Xrender surfaces, and use them if they work. Next, check if
1801 * accelerated OpenGL is available, and use GL surfaces in this
1802 * case. If all else fails, use GDI surfaces. If a 3DDEVICE surface
1803 * was created, always use OpenGL surfaces.
1805 * (Note: Xrender surfaces are not implemented for now, the
1806 * unaccelerated implementation uses GDI to render in Software)
1809 /* Store the type. If it needs to be changed, all WineD3DSurfaces have to
1810 * be re-created. This could be done with IDirectDrawSurface7::Restore
1812 This->ImplType = ImplType;
1814 else
1816 if ((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1817 && (This->ImplType != SURFACE_OPENGL)
1818 && DefaultSurfaceType == SURFACE_UNKNOWN)
1820 /* We have to change to OpenGL,
1821 * and re-create all WineD3DSurfaces
1823 ImplType = SURFACE_OPENGL;
1824 This->ImplType = ImplType;
1825 TRACE("(%p) Re-creating all surfaces\n", This);
1826 IDirectDrawImpl_RecreateAllSurfaces(This);
1827 TRACE("(%p) Done recreating all surfaces\n", This);
1829 else if(This->ImplType != SURFACE_OPENGL && pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1831 WARN("The application requests a 3D capable surface, but a non-opengl surface was set in the registry\n");
1832 /* Do not fail surface creation, only fail 3D device creation */
1836 if (!(pDDSD->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY)) &&
1837 !((pDDSD->ddsCaps.dwCaps & DDSCAPS_TEXTURE) && (pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)) )
1839 /* Tests show surfaces without memory flags get these flags added right after creation. */
1840 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
1842 /* Get the correct wined3d usage */
1843 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE |
1844 DDSCAPS_3DDEVICE ) )
1846 Usage |= WINED3DUSAGE_RENDERTARGET;
1848 pDDSD->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
1850 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
1852 Usage |= WINED3DUSAGE_OVERLAY;
1854 if(This->depthstencil || (pDDSD->ddsCaps.dwCaps & DDSCAPS_ZBUFFER) )
1856 /* The depth stencil creation callback sets this flag.
1857 * Set the WineD3D usage to let it know that it's a depth
1858 * Stencil surface.
1860 Usage |= WINED3DUSAGE_DEPTHSTENCIL;
1862 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
1864 Pool = WINED3DPOOL_SYSTEMMEM;
1866 else if(pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
1868 Pool = WINED3DPOOL_MANAGED;
1869 /* Managed textures have the system memory flag set */
1870 pDDSD->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
1872 else if(pDDSD->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
1874 /* Videomemory adds localvidmem, this is mutually exclusive with systemmemory
1875 * and texturemanage
1877 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
1880 Format = PixelFormat_DD2WineD3D(&pDDSD->u4.ddpfPixelFormat);
1881 if(Format == WINED3DFMT_UNKNOWN)
1883 ERR("Unsupported / Unknown pixelformat\n");
1884 return DDERR_INVALIDPIXELFORMAT;
1887 /* Create the Surface object */
1888 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
1889 if(!*ppSurf)
1891 ERR("(%p) Error allocating memory for a surface\n", This);
1892 return DDERR_OUTOFVIDEOMEMORY;
1894 (*ppSurf)->lpVtbl = &IDirectDrawSurface7_Vtbl;
1895 (*ppSurf)->IDirectDrawSurface3_vtbl = &IDirectDrawSurface3_Vtbl;
1896 (*ppSurf)->IDirectDrawGammaControl_vtbl = &IDirectDrawGammaControl_Vtbl;
1897 (*ppSurf)->IDirect3DTexture2_vtbl = &IDirect3DTexture2_Vtbl;
1898 (*ppSurf)->IDirect3DTexture_vtbl = &IDirect3DTexture1_Vtbl;
1899 (*ppSurf)->ref = 1;
1900 (*ppSurf)->version = 7;
1901 TRACE("%p->version = %d\n", (*ppSurf), (*ppSurf)->version);
1902 (*ppSurf)->ddraw = This;
1903 (*ppSurf)->surface_desc.dwSize = sizeof(DDSURFACEDESC2);
1904 (*ppSurf)->surface_desc.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1905 DD_STRUCT_COPY_BYSIZE(&(*ppSurf)->surface_desc, pDDSD);
1907 /* Surface attachments */
1908 (*ppSurf)->next_attached = NULL;
1909 (*ppSurf)->first_attached = *ppSurf;
1911 /* Needed to re-create the surface on an implementation change */
1912 (*ppSurf)->ImplType = ImplType;
1914 /* For D3DDevice creation */
1915 (*ppSurf)->isRenderTarget = FALSE;
1917 /* A trace message for debugging */
1918 TRACE("(%p) Created IDirectDrawSurface implementation structure at %p\n", This, *ppSurf);
1920 /* Now create the WineD3D Surface */
1921 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice, pDDSD->dwWidth, pDDSD->dwHeight, Format,
1922 TRUE /* Lockable */, FALSE /* Discard */, level, &(*ppSurf)->WineD3DSurface,
1923 Usage, Pool, WINED3DMULTISAMPLE_NONE, 0 /* MultiSampleQuality */, ImplType,
1924 (IUnknown *)*ppSurf, &ddraw_null_wined3d_parent_ops);
1926 if(hr != D3D_OK)
1928 ERR("IWineD3DDevice::CreateSurface failed. hr = %08x\n", hr);
1929 return hr;
1932 /* Increase the surface counter, and attach the surface */
1933 InterlockedIncrement(&This->surfaces);
1934 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
1936 /* Here we could store all created surfaces in the DirectDrawImpl structure,
1937 * But this could also be delegated to WineDDraw, as it keeps track of all its
1938 * resources. Not implemented for now, as there are more important things ;)
1941 /* Get the pixel format of the WineD3DSurface and store it.
1942 * Don't use the Format choosen above, WineD3D might have
1943 * changed it
1945 (*ppSurf)->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
1946 hr = IWineD3DSurface_GetDesc((*ppSurf)->WineD3DSurface, &Desc);
1947 if(hr != D3D_OK)
1949 ERR("IWineD3DSurface::GetDesc failed\n");
1950 IDirectDrawSurface7_Release( (IDirectDrawSurface7 *) *ppSurf);
1951 return hr;
1954 Format = Desc.format;
1955 Width = Desc.width;
1956 Height = Desc.height;
1958 if(Format == WINED3DFMT_UNKNOWN)
1960 FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN\n");
1962 PixelFormat_WineD3DtoDD( &(*ppSurf)->surface_desc.u4.ddpfPixelFormat, Format);
1964 /* Anno 1602 stores the pitch right after surface creation, so make sure it's there.
1965 * I can't LockRect() the surface here because if OpenGL surfaces are in use, the
1966 * WineD3DDevice might not be usable for 3D yet, so an extra method was created.
1967 * TODO: Test other fourcc formats
1969 if(Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3 ||
1970 Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5)
1972 (*ppSurf)->surface_desc.dwFlags |= DDSD_LINEARSIZE;
1973 if(Format == WINED3DFMT_DXT1)
1975 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height) / 2;
1977 else
1979 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height);
1982 else
1984 (*ppSurf)->surface_desc.dwFlags |= DDSD_PITCH;
1985 (*ppSurf)->surface_desc.u1.lPitch = IWineD3DSurface_GetPitch((*ppSurf)->WineD3DSurface);
1988 /* Application passed a color key? Set it! */
1989 if(pDDSD->dwFlags & DDSD_CKDESTOVERLAY)
1991 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
1992 DDCKEY_DESTOVERLAY,
1993 (WINEDDCOLORKEY *) &pDDSD->u3.ddckCKDestOverlay);
1995 if(pDDSD->dwFlags & DDSD_CKDESTBLT)
1997 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
1998 DDCKEY_DESTBLT,
1999 (WINEDDCOLORKEY *) &pDDSD->ddckCKDestBlt);
2001 if(pDDSD->dwFlags & DDSD_CKSRCOVERLAY)
2003 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2004 DDCKEY_SRCOVERLAY,
2005 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcOverlay);
2007 if(pDDSD->dwFlags & DDSD_CKSRCBLT)
2009 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2010 DDCKEY_SRCBLT,
2011 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcBlt);
2013 if ( pDDSD->dwFlags & DDSD_LPSURFACE)
2015 hr = IWineD3DSurface_SetMem((*ppSurf)->WineD3DSurface, pDDSD->lpSurface);
2016 if(hr != WINED3D_OK)
2018 /* No need for a trace here, wined3d does that for us */
2019 IDirectDrawSurface7_Release((IDirectDrawSurface7 *)*ppSurf);
2020 return hr;
2024 return DD_OK;
2026 /*****************************************************************************
2027 * CreateAdditionalSurfaces
2029 * Creates a new mipmap chain.
2031 * Params:
2032 * root: Root surface to attach the newly created chain to
2033 * count: number of surfaces to create
2034 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2035 * effects on the caller
2036 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2037 * creates an additional surface without the mipmapping flags
2039 *****************************************************************************/
2040 static HRESULT
2041 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2042 IDirectDrawSurfaceImpl *root,
2043 UINT count,
2044 DDSURFACEDESC2 DDSD,
2045 BOOL CubeFaceRoot)
2047 UINT i, j, level = 0;
2048 HRESULT hr;
2049 IDirectDrawSurfaceImpl *last = root;
2051 for(i = 0; i < count; i++)
2053 IDirectDrawSurfaceImpl *object2 = NULL;
2055 /* increase the mipmap level, but only if a mipmap is created
2056 * In this case, also halve the size
2058 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2060 level++;
2061 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2062 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2063 /* Set the mipmap sublevel flag according to msdn */
2064 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2066 else
2068 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2070 CubeFaceRoot = FALSE;
2072 hr = IDirectDrawImpl_CreateNewSurface(This,
2073 &DDSD,
2074 &object2,
2075 level);
2076 if(hr != DD_OK)
2078 return hr;
2081 /* Add the new surface to the complex attachment array */
2082 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2084 if(last->complex_array[j]) continue;
2085 last->complex_array[j] = object2;
2086 break;
2088 last = object2;
2090 /* Remove the (possible) back buffer cap from the new surface description,
2091 * because only one surface in the flipping chain is a back buffer, one
2092 * is a front buffer, the others are just primary surfaces.
2094 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2096 return DD_OK;
2099 /*****************************************************************************
2100 * IDirectDraw7::CreateSurface
2102 * Creates a new IDirectDrawSurface object and returns its interface.
2104 * The surface connections with wined3d are a bit tricky. Basically it works
2105 * like this:
2107 * |------------------------| |-----------------|
2108 * | DDraw surface | | WineD3DSurface |
2109 * | | | |
2110 * | WineD3DSurface |-------------->| |
2111 * | Child |<------------->| Parent |
2112 * |------------------------| |-----------------|
2114 * The DDraw surface is the parent of the wined3d surface, and it releases
2115 * the WineD3DSurface when the ddraw surface is destroyed.
2117 * However, for all surfaces which can be in a container in WineD3D,
2118 * we have to do this. These surfaces are usually complex surfaces,
2119 * so this concerns primary surfaces with a front and a back buffer,
2120 * and textures.
2122 * |------------------------| |-----------------|
2123 * | DDraw surface | | Container |
2124 * | | | |
2125 * | Child |<------------->| Parent |
2126 * | Texture |<------------->| |
2127 * | WineD3DSurface |<----| | Levels |<--|
2128 * | Complex connection | | | | |
2129 * |------------------------| | |-----------------| |
2130 * ^ | |
2131 * | | |
2132 * | | |
2133 * | |------------------| | |-----------------| |
2134 * | | IParent | |-------->| WineD3DSurface | |
2135 * | | | | | |
2136 * | | Child |<------------->| Parent | |
2137 * | | | | Container |<--|
2138 * | |------------------| |-----------------| |
2139 * | |
2140 * | |----------------------| |
2141 * | | DDraw surface 2 | |
2142 * | | | |
2143 * |<->| Complex root Child | |
2144 * | | Texture | |
2145 * | | WineD3DSurface |<----| |
2146 * | |----------------------| | |
2147 * | | |
2148 * | |---------------------| | |-----------------| |
2149 * | | IParent | |----->| WineD3DSurface | |
2150 * | | | | | |
2151 * | | Child |<---------->| Parent | |
2152 * | |---------------------| | Container |<--|
2153 * | |-----------------| |
2154 * | |
2155 * | ---More surfaces can follow--- |
2157 * The reason is that the IWineD3DSwapchain(render target container)
2158 * and the IWineD3DTexure(Texture container) release the parents
2159 * of their surface's children, but by releasing the complex root
2160 * the surfaces which are complexly attached to it are destroyed
2161 * too. See IDirectDrawSurface::Release for a more detailed
2162 * explanation.
2164 * Params:
2165 * DDSD: Description of the surface to create
2166 * Surf: Address to store the interface pointer at
2167 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2168 * aggregation, so it has to be NULL
2170 * Returns:
2171 * DD_OK on success
2172 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2173 * DDERR_* if an error occurs
2175 *****************************************************************************/
2176 static HRESULT WINAPI
2177 IDirectDrawImpl_CreateSurface(IDirectDraw7 *iface,
2178 DDSURFACEDESC2 *DDSD,
2179 IDirectDrawSurface7 **Surf,
2180 IUnknown *UnkOuter)
2182 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
2183 IDirectDrawSurfaceImpl *object = NULL;
2184 HRESULT hr;
2185 LONG extra_surfaces = 0;
2186 DDSURFACEDESC2 desc2;
2187 WINED3DDISPLAYMODE Mode;
2188 const DWORD sysvidmem = DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
2190 TRACE("(%p)->(%p,%p,%p)\n", This, DDSD, Surf, UnkOuter);
2192 /* Some checks before we start */
2193 if (TRACE_ON(ddraw))
2195 TRACE(" (%p) Requesting surface desc :\n", This);
2196 DDRAW_dump_surface_desc(DDSD);
2198 EnterCriticalSection(&ddraw_cs);
2200 if (UnkOuter != NULL)
2202 FIXME("(%p) : outer != NULL?\n", This);
2203 LeaveCriticalSection(&ddraw_cs);
2204 return CLASS_E_NOAGGREGATION; /* unchecked */
2207 if (Surf == NULL)
2209 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", This);
2210 LeaveCriticalSection(&ddraw_cs);
2211 return E_POINTER; /* unchecked */
2214 if (!(DDSD->dwFlags & DDSD_CAPS))
2216 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2217 DDSD->dwFlags |= DDSD_CAPS;
2220 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2222 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2223 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2226 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2228 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2229 WARN("(%p) Null surface pointer specified, ignore it!\n", This);
2230 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2233 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2234 !(This->cooperative_level & DDSCL_EXCLUSIVE))
2236 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n", This);
2237 *Surf = NULL;
2238 LeaveCriticalSection(&ddraw_cs);
2239 return DDERR_NOEXCLUSIVEMODE;
2242 if(DDSD->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER)) {
2243 WARN("Application tried to create an explicit front or back buffer\n");
2244 LeaveCriticalSection(&ddraw_cs);
2245 return DDERR_INVALIDCAPS;
2248 if((DDSD->ddsCaps.dwCaps & sysvidmem) == sysvidmem)
2250 /* This is a special switch in ddrawex.dll, but not allowed in ddraw.dll */
2251 WARN("Application tries to put the surface in both system and video memory\n");
2252 LeaveCriticalSection(&ddraw_cs);
2253 *Surf = NULL;
2254 return DDERR_INVALIDCAPS;
2257 /* Check cube maps but only if the size includes them */
2258 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2260 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2261 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2263 WARN("Cube map faces requested without cube map flag\n");
2264 LeaveCriticalSection(&ddraw_cs);
2265 return DDERR_INVALIDCAPS;
2267 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2268 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2270 WARN("Cube map without faces requested\n");
2271 LeaveCriticalSection(&ddraw_cs);
2272 return DDERR_INVALIDPARAMS;
2275 /* Quick tests confirm those can be created, but we don't do that yet */
2276 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2277 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2279 FIXME("Partial cube maps not supported yet\n");
2283 /* According to the msdn this flag is ignored by CreateSurface */
2284 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2285 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2287 /* Modify some flags */
2288 memset(&desc2, 0, sizeof(desc2));
2289 desc2.dwSize = sizeof(desc2); /* For the struct copy */
2290 DD_STRUCT_COPY_BYSIZE(&desc2, DDSD);
2291 desc2.dwSize = sizeof(desc2); /* To override a possibly smaller size */
2292 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2294 /* Get the video mode from WineD3D - we will need it */
2295 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
2296 0, /* Swapchain 0 */
2297 &Mode);
2298 if(FAILED(hr))
2300 ERR("Failed to read display mode from wined3d\n");
2301 switch(This->orig_bpp)
2303 case 8:
2304 Mode.Format = WINED3DFMT_P8_UINT;
2305 break;
2307 case 15:
2308 Mode.Format = WINED3DFMT_B5G5R5X1_UNORM;
2309 break;
2311 case 16:
2312 Mode.Format = WINED3DFMT_B5G6R5_UNORM;
2313 break;
2315 case 24:
2316 Mode.Format = WINED3DFMT_B8G8R8_UNORM;
2317 break;
2319 case 32:
2320 Mode.Format = WINED3DFMT_B8G8R8X8_UNORM;
2321 break;
2323 Mode.Width = This->orig_width;
2324 Mode.Height = This->orig_height;
2327 /* No pixelformat given? Use the current screen format */
2328 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2330 desc2.dwFlags |= DDSD_PIXELFORMAT;
2331 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2333 /* Wait: It could be a Z buffer */
2334 if(desc2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
2336 switch(desc2.u2.dwMipMapCount) /* Who had this glorious idea? */
2338 case 15:
2339 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_S1_UINT_D15_UNORM);
2340 break;
2341 case 16:
2342 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D16_UNORM);
2343 break;
2344 case 24:
2345 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_X8D24_UNORM);
2346 break;
2347 case 32:
2348 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D32_UNORM);
2349 break;
2350 default:
2351 ERR("Unknown Z buffer bit depth\n");
2354 else
2356 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
2360 /* No Width or no Height? Use the original screen size
2362 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2363 !(desc2.dwFlags & DDSD_HEIGHT) )
2365 /* Invalid for non-render targets */
2366 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2368 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2369 *Surf = NULL;
2370 LeaveCriticalSection(&ddraw_cs);
2371 return DDERR_INVALIDPARAMS;
2374 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2375 desc2.dwWidth = Mode.Width;
2376 desc2.dwHeight = Mode.Height;
2379 /* Mipmap count fixes */
2380 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2382 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2384 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2386 /* Mipmap count is given, should not be 0 */
2387 if( desc2.u2.dwMipMapCount == 0 )
2389 LeaveCriticalSection(&ddraw_cs);
2390 return DDERR_INVALIDPARAMS;
2393 else
2395 /* Undocumented feature: Create sublevels until
2396 * either the width or the height is 1
2398 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2399 desc2.dwWidth : desc2.dwHeight;
2400 desc2.u2.dwMipMapCount = 0;
2401 while( min )
2403 desc2.u2.dwMipMapCount += 1;
2404 min >>= 1;
2408 else
2410 /* Not-complex mipmap -> Mipmapcount = 1 */
2411 desc2.u2.dwMipMapCount = 1;
2413 extra_surfaces = desc2.u2.dwMipMapCount - 1;
2415 /* There's a mipmap count in the created surface in any case */
2416 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2418 /* If no mipmap is given, the texture has only one level */
2420 /* The first surface is a front buffer, the back buffer is created afterwards */
2421 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2423 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2426 /* The root surface in a cube map is positive x */
2427 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2429 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2430 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2433 /* Create the first surface */
2434 hr = IDirectDrawImpl_CreateNewSurface(This, &desc2, &object, 0);
2435 if( hr != DD_OK)
2437 ERR("IDirectDrawImpl_CreateNewSurface failed with %08x\n", hr);
2438 LeaveCriticalSection(&ddraw_cs);
2439 return hr;
2441 object->is_complex_root = TRUE;
2443 *Surf = (IDirectDrawSurface7 *)object;
2445 /* Create Additional surfaces if necessary
2446 * This applies to Primary surfaces which have a back buffer count
2447 * set, but not to mipmap textures. In case of Mipmap textures,
2448 * wineD3D takes care of the creation of additional surfaces
2450 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2452 extra_surfaces = DDSD->dwBackBufferCount;
2453 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2454 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2455 desc2.dwBackBufferCount = 0;
2458 hr = DD_OK;
2459 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2461 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2462 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
2463 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2464 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
2465 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
2466 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2467 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
2468 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
2469 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2470 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
2471 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
2472 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2473 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
2474 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
2475 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2476 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
2477 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2480 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces, desc2, FALSE);
2481 if(hr != DD_OK)
2483 /* This destroys and possibly created surfaces too */
2484 IDirectDrawSurface_Release((IDirectDrawSurface7 *)object);
2485 LeaveCriticalSection(&ddraw_cs);
2486 return hr;
2489 /* If the implementation is OpenGL and there's no d3ddevice, attach a d3ddevice
2490 * But attach the d3ddevice only if the currently created surface was
2491 * a primary surface (2D app in 3D mode) or a 3DDEVICE surface (3D app)
2492 * The only case I can think of where this doesn't apply is when a
2493 * 2D app was configured by the user to run with OpenGL and it didn't create
2494 * the render target as first surface. In this case the render target creation
2495 * will cause the 3D init.
2497 if( (This->ImplType == SURFACE_OPENGL) && !(This->d3d_initialized) &&
2498 desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE) )
2500 IDirectDrawSurfaceImpl *target = object, *surface;
2501 struct list *entry;
2503 /* Search for the primary to use as render target */
2504 LIST_FOR_EACH(entry, &This->surface_list)
2506 surface = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2507 if((surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER)) ==
2508 (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER))
2510 /* found */
2511 target = surface;
2512 TRACE("Using primary %p as render target\n", target);
2513 break;
2517 TRACE("(%p) Attaching a D3DDevice, rendertarget = %p\n", This, target);
2518 hr = IDirectDrawImpl_AttachD3DDevice(This, target);
2519 if(hr != D3D_OK)
2521 IDirectDrawSurfaceImpl *release_surf;
2522 ERR("IDirectDrawImpl_AttachD3DDevice failed, hr = %x\n", hr);
2523 *Surf = NULL;
2525 /* The before created surface structures are in an incomplete state here.
2526 * WineD3D holds the reference on the IParents, and it released them on the failure
2527 * already. So the regular release method implementation would fail on the attempt
2528 * to destroy either the IParents or the swapchain. So free the surface here.
2529 * The surface structure here is a list, not a tree, because onscreen targets
2530 * cannot be cube textures
2532 while(object)
2534 release_surf = object;
2535 object = object->complex_array[0];
2536 IDirectDrawSurfaceImpl_Destroy(release_surf);
2538 LeaveCriticalSection(&ddraw_cs);
2539 return hr;
2541 } else if(!(This->d3d_initialized) && desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
2542 IDirectDrawImpl_CreateGDISwapChain(This, object);
2545 /* Addref the ddraw interface to keep an reference for each surface */
2546 IDirectDraw7_AddRef(iface);
2547 object->ifaceToRelease = (IUnknown *) iface;
2549 /* Create a WineD3DTexture if a texture was requested */
2550 if(desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
2552 UINT levels;
2553 WINED3DFORMAT Format;
2554 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
2556 This->tex_root = object;
2558 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2560 /* a mipmap is created, create enough levels */
2561 levels = desc2.u2.dwMipMapCount;
2563 else
2565 /* No mipmap is created, create one level */
2566 levels = 1;
2569 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM */
2570 if(DDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
2572 Pool = WINED3DPOOL_SYSTEMMEM;
2574 /* Should I forward the MANAGED cap to the managed pool ? */
2576 /* Get the format. It's set already by CreateNewSurface */
2577 Format = PixelFormat_DD2WineD3D(&object->surface_desc.u4.ddpfPixelFormat);
2579 /* The surfaces are already created, the callback only
2580 * passes the IWineD3DSurface to WineD3D
2582 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2584 hr = IWineD3DDevice_CreateCubeTexture(This->wineD3DDevice, DDSD->dwWidth /* Edgelength */,
2585 levels, 0 /* usage */, Format, Pool, (IWineD3DCubeTexture **)&object->wineD3DTexture,
2586 (IUnknown *)object, &ddraw_null_wined3d_parent_ops);
2588 else
2590 hr = IWineD3DDevice_CreateTexture(This->wineD3DDevice, DDSD->dwWidth, DDSD->dwHeight, levels,
2591 0 /* usage */, Format, Pool, (IWineD3DTexture **)&object->wineD3DTexture,
2592 (IUnknown *)object, &ddraw_null_wined3d_parent_ops);
2594 This->tex_root = NULL;
2597 LeaveCriticalSection(&ddraw_cs);
2598 return hr;
2601 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
2602 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
2604 static BOOL
2605 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
2606 const DDPIXELFORMAT *provided)
2608 /* Some flags must be present in both or neither for a match. */
2609 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
2610 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
2611 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
2613 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2614 return FALSE;
2616 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
2617 return FALSE;
2619 if (requested->dwFlags & DDPF_FOURCC)
2620 if (requested->dwFourCC != provided->dwFourCC)
2621 return FALSE;
2623 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
2624 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2625 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
2626 return FALSE;
2628 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2629 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2630 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
2631 return FALSE;
2633 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
2634 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
2635 return FALSE;
2637 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
2638 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2639 |DDPF_BUMPDUDV))
2640 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
2641 return FALSE;
2643 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
2644 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
2645 return FALSE;
2647 return TRUE;
2650 static BOOL
2651 IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested,
2652 const DDSURFACEDESC2* provided)
2654 struct compare_info
2656 DWORD flag;
2657 ptrdiff_t offset;
2658 size_t size;
2661 #define CMP(FLAG, FIELD) \
2662 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
2663 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
2665 static const struct compare_info compare[] =
2667 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
2668 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
2669 CMP(CAPS, ddsCaps),
2670 CMP(CKDESTBLT, ddckCKDestBlt),
2671 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
2672 CMP(CKSRCBLT, ddckCKSrcBlt),
2673 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
2674 CMP(HEIGHT, dwHeight),
2675 CMP(LINEARSIZE, u1 /* dwLinearSize */),
2676 CMP(LPSURFACE, lpSurface),
2677 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
2678 CMP(PITCH, u1 /* lPitch */),
2679 /* PIXELFORMAT: manual */
2680 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
2681 CMP(TEXTURESTAGE, dwTextureStage),
2682 CMP(WIDTH, dwWidth),
2683 /* ZBUFFERBITDEPTH: "obsolete" */
2686 #undef CMP
2688 unsigned int i;
2690 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2691 return FALSE;
2693 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
2695 if (requested->dwFlags & compare[i].flag
2696 && memcmp((const char *)provided + compare[i].offset,
2697 (const char *)requested + compare[i].offset,
2698 compare[i].size) != 0)
2699 return FALSE;
2702 if (requested->dwFlags & DDSD_PIXELFORMAT)
2704 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
2705 &provided->u4.ddpfPixelFormat))
2706 return FALSE;
2709 return TRUE;
2712 #undef DDENUMSURFACES_SEARCHTYPE
2713 #undef DDENUMSURFACES_MATCHTYPE
2715 /*****************************************************************************
2716 * IDirectDraw7::EnumSurfaces
2718 * Loops through all surfaces attached to this device and calls the
2719 * application callback. This can't be relayed to WineD3DDevice,
2720 * because some WineD3DSurfaces' parents are IParent objects
2722 * Params:
2723 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
2724 * DDSD: Description to filter for
2725 * Context: Application-provided pointer, it's passed unmodified to the
2726 * Callback function
2727 * Callback: Address to call for each surface
2729 * Returns:
2730 * DDERR_INVALIDPARAMS if the callback is NULL
2731 * DD_OK on success
2733 *****************************************************************************/
2734 static HRESULT WINAPI
2735 IDirectDrawImpl_EnumSurfaces(IDirectDraw7 *iface,
2736 DWORD Flags,
2737 DDSURFACEDESC2 *DDSD,
2738 void *Context,
2739 LPDDENUMSURFACESCALLBACK7 Callback)
2741 /* The surface enumeration is handled by WineDDraw,
2742 * because it keeps track of all surfaces attached to
2743 * it. The filtering is done by our callback function,
2744 * because WineDDraw doesn't handle ddraw-like surface
2745 * caps structures
2747 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
2748 IDirectDrawSurfaceImpl *surf;
2749 BOOL all, nomatch;
2750 DDSURFACEDESC2 desc;
2751 struct list *entry, *entry2;
2753 all = Flags & DDENUMSURFACES_ALL;
2754 nomatch = Flags & DDENUMSURFACES_NOMATCH;
2756 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, DDSD, Context, Callback);
2757 EnterCriticalSection(&ddraw_cs);
2759 if(!Callback)
2761 LeaveCriticalSection(&ddraw_cs);
2762 return DDERR_INVALIDPARAMS;
2765 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
2766 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
2768 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2769 if (all || (nomatch != IDirectDrawImpl_DDSD_Match(DDSD, &surf->surface_desc)))
2771 desc = surf->surface_desc;
2772 IDirectDrawSurface7_AddRef((IDirectDrawSurface7 *)surf);
2773 if (Callback((IDirectDrawSurface7 *)surf, &desc, Context) != DDENUMRET_OK)
2775 LeaveCriticalSection(&ddraw_cs);
2776 return DD_OK;
2780 LeaveCriticalSection(&ddraw_cs);
2781 return DD_OK;
2784 static HRESULT WINAPI
2785 findRenderTarget(IDirectDrawSurface7 *surface,
2786 DDSURFACEDESC2 *desc,
2787 void *ctx)
2789 IDirectDrawSurfaceImpl *surf = (IDirectDrawSurfaceImpl *)surface;
2790 IDirectDrawSurfaceImpl **target = ctx;
2792 if(!surf->isRenderTarget) {
2793 *target = surf;
2794 IDirectDrawSurface7_Release(surface);
2795 return DDENUMRET_CANCEL;
2798 /* Recurse into the surface tree */
2799 IDirectDrawSurface7_EnumAttachedSurfaces(surface, ctx, findRenderTarget);
2801 IDirectDrawSurface7_Release(surface);
2802 if(*target) return DDENUMRET_CANCEL;
2803 else return DDENUMRET_OK; /* Continue with the next neighbor surface */
2806 static HRESULT IDirectDrawImpl_CreateGDISwapChain(IDirectDrawImpl *This,
2807 IDirectDrawSurfaceImpl *primary) {
2808 HRESULT hr;
2809 WINED3DPRESENT_PARAMETERS presentation_parameters;
2810 HWND window;
2812 window = This->dest_window;
2814 memset(&presentation_parameters, 0, sizeof(presentation_parameters));
2816 /* Use the surface description for the device parameters, not the
2817 * Device settings. The app might render to an offscreen surface
2819 presentation_parameters.BackBufferWidth = primary->surface_desc.dwWidth;
2820 presentation_parameters.BackBufferHeight = primary->surface_desc.dwHeight;
2821 presentation_parameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2822 presentation_parameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
2823 presentation_parameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
2824 presentation_parameters.MultiSampleQuality = 0;
2825 presentation_parameters.SwapEffect = WINED3DSWAPEFFECT_FLIP;
2826 presentation_parameters.hDeviceWindow = window;
2827 presentation_parameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
2828 presentation_parameters.EnableAutoDepthStencil = FALSE; /* Not on GDI swapchains */
2829 presentation_parameters.AutoDepthStencilFormat = 0;
2830 presentation_parameters.Flags = 0;
2831 presentation_parameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
2832 presentation_parameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
2834 This->d3d_target = primary;
2835 hr = IWineD3DDevice_InitGDI(This->wineD3DDevice, &presentation_parameters);
2836 This->d3d_target = NULL;
2838 if (hr != D3D_OK)
2840 FIXME("(%p) call to IWineD3DDevice_InitGDI failed\n", This);
2841 primary->wineD3DSwapChain = NULL;
2843 return hr;
2846 /*****************************************************************************
2847 * IDirectDrawImpl_AttachD3DDevice
2849 * Initializes the D3D capabilities of WineD3D
2851 * Params:
2852 * primary: The primary surface for D3D
2854 * Returns
2855 * DD_OK on success,
2856 * DDERR_* otherwise
2858 *****************************************************************************/
2859 static HRESULT
2860 IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This,
2861 IDirectDrawSurfaceImpl *primary)
2863 HRESULT hr;
2864 HWND window = This->dest_window;
2866 WINED3DPRESENT_PARAMETERS localParameters;
2868 TRACE("(%p)->(%p)\n", This, primary);
2870 /* If there's no window, create a hidden window. WineD3D needs it */
2871 if(window == 0 || window == GetDesktopWindow())
2873 window = CreateWindowExA(0, This->classname, "Hidden D3D Window",
2874 WS_DISABLED, 0, 0,
2875 GetSystemMetrics(SM_CXSCREEN),
2876 GetSystemMetrics(SM_CYSCREEN),
2877 NULL, NULL, GetModuleHandleA(0), NULL);
2879 ShowWindow(window, SW_HIDE); /* Just to be sure */
2880 WARN("(%p) No window for the Direct3DDevice, created a hidden window. HWND=%p\n", This, window);
2882 else
2884 TRACE("(%p) Using existing window %p for Direct3D rendering\n", This, window);
2886 This->d3d_window = window;
2888 /* Store the future Render Target surface */
2889 This->d3d_target = primary;
2891 /* Use the surface description for the device parameters, not the
2892 * Device settings. The app might render to an offscreen surface
2894 localParameters.BackBufferWidth = primary->surface_desc.dwWidth;
2895 localParameters.BackBufferHeight = primary->surface_desc.dwHeight;
2896 localParameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2897 localParameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
2898 localParameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
2899 localParameters.MultiSampleQuality = 0;
2900 localParameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
2901 localParameters.hDeviceWindow = window;
2902 localParameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
2903 localParameters.EnableAutoDepthStencil = TRUE;
2904 localParameters.AutoDepthStencilFormat = WINED3DFMT_D16_UNORM;
2905 localParameters.Flags = 0;
2906 localParameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
2907 localParameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
2909 TRACE("Passing mode %d\n", localParameters.BackBufferFormat);
2911 /* Set this NOW, otherwise creating the depth stencil surface will cause a
2912 * recursive loop until ram or emulated video memory is full
2914 This->d3d_initialized = TRUE;
2916 hr = IWineD3DDevice_Init3D(This->wineD3DDevice, &localParameters);
2917 if(FAILED(hr))
2919 This->d3d_target = NULL;
2920 This->d3d_initialized = FALSE;
2921 return hr;
2924 This->declArraySize = 2;
2925 This->decls = HeapAlloc(GetProcessHeap(),
2926 HEAP_ZERO_MEMORY,
2927 sizeof(*This->decls) * This->declArraySize);
2928 if(!This->decls)
2930 ERR("Error allocating an array for the converted vertex decls\n");
2931 This->declArraySize = 0;
2932 hr = IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroySwapChain);
2933 return E_OUTOFMEMORY;
2936 /* Create an Index Buffer parent */
2937 TRACE("(%p) Successfully initialized 3D\n", This);
2938 return DD_OK;
2941 /*****************************************************************************
2942 * DirectDrawCreateClipper (DDRAW.@)
2944 * Creates a new IDirectDrawClipper object.
2946 * Params:
2947 * Clipper: Address to write the interface pointer to
2948 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
2949 * NULL
2951 * Returns:
2952 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2953 * E_OUTOFMEMORY if allocating the object failed
2955 *****************************************************************************/
2956 HRESULT WINAPI
2957 DirectDrawCreateClipper(DWORD Flags,
2958 LPDIRECTDRAWCLIPPER *Clipper,
2959 IUnknown *UnkOuter)
2961 IDirectDrawClipperImpl* object;
2962 TRACE("(%08x,%p,%p)\n", Flags, Clipper, UnkOuter);
2964 EnterCriticalSection(&ddraw_cs);
2965 if (UnkOuter != NULL)
2967 LeaveCriticalSection(&ddraw_cs);
2968 return CLASS_E_NOAGGREGATION;
2971 if (!LoadWineD3D())
2973 LeaveCriticalSection(&ddraw_cs);
2974 return DDERR_NODIRECTDRAWSUPPORT;
2977 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2978 sizeof(IDirectDrawClipperImpl));
2979 if (object == NULL)
2981 LeaveCriticalSection(&ddraw_cs);
2982 return E_OUTOFMEMORY;
2985 object->lpVtbl = &IDirectDrawClipper_Vtbl;
2986 object->ref = 1;
2987 object->wineD3DClipper = pWineDirect3DCreateClipper((IUnknown *) object);
2988 if(!object->wineD3DClipper)
2990 HeapFree(GetProcessHeap(), 0, object);
2991 LeaveCriticalSection(&ddraw_cs);
2992 return E_OUTOFMEMORY;
2995 *Clipper = (IDirectDrawClipper *) object;
2996 LeaveCriticalSection(&ddraw_cs);
2997 return DD_OK;
3000 /*****************************************************************************
3001 * IDirectDraw7::CreateClipper
3003 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3005 *****************************************************************************/
3006 static HRESULT WINAPI
3007 IDirectDrawImpl_CreateClipper(IDirectDraw7 *iface,
3008 DWORD Flags,
3009 IDirectDrawClipper **Clipper,
3010 IUnknown *UnkOuter)
3012 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
3013 TRACE("(%p)->(%x,%p,%p)\n", This, Flags, Clipper, UnkOuter);
3014 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3017 /*****************************************************************************
3018 * IDirectDraw7::CreatePalette
3020 * Creates a new IDirectDrawPalette object
3022 * Params:
3023 * Flags: The flags for the new clipper
3024 * ColorTable: Color table to assign to the new clipper
3025 * Palette: Address to write the interface pointer to
3026 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3027 * NULL
3029 * Returns:
3030 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3031 * E_OUTOFMEMORY if allocating the object failed
3033 *****************************************************************************/
3034 static HRESULT WINAPI
3035 IDirectDrawImpl_CreatePalette(IDirectDraw7 *iface,
3036 DWORD Flags,
3037 PALETTEENTRY *ColorTable,
3038 IDirectDrawPalette **Palette,
3039 IUnknown *pUnkOuter)
3041 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
3042 IDirectDrawPaletteImpl *object;
3043 HRESULT hr = DDERR_GENERIC;
3044 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, ColorTable, Palette, pUnkOuter);
3046 EnterCriticalSection(&ddraw_cs);
3047 if(pUnkOuter != NULL)
3049 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
3050 LeaveCriticalSection(&ddraw_cs);
3051 return CLASS_E_NOAGGREGATION;
3054 /* The refcount test shows that a cooplevel is required for this */
3055 if(!This->cooperative_level)
3057 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3058 LeaveCriticalSection(&ddraw_cs);
3059 return DDERR_NOCOOPERATIVELEVELSET;
3062 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
3063 if(!object)
3065 ERR("Out of memory when allocating memory for a palette implementation\n");
3066 LeaveCriticalSection(&ddraw_cs);
3067 return E_OUTOFMEMORY;
3070 object->lpVtbl = &IDirectDrawPalette_Vtbl;
3071 object->ref = 1;
3072 object->ddraw_owner = This;
3074 hr = IWineD3DDevice_CreatePalette(This->wineD3DDevice, Flags,
3075 ColorTable, &object->wineD3DPalette, (IUnknown *)object);
3076 if(hr != DD_OK)
3078 HeapFree(GetProcessHeap(), 0, object);
3079 LeaveCriticalSection(&ddraw_cs);
3080 return hr;
3083 IDirectDraw7_AddRef(iface);
3084 object->ifaceToRelease = (IUnknown *) iface;
3085 *Palette = (IDirectDrawPalette *)object;
3086 LeaveCriticalSection(&ddraw_cs);
3087 return DD_OK;
3090 /*****************************************************************************
3091 * IDirectDraw7::DuplicateSurface
3093 * Duplicates a surface. The surface memory points to the same memory as
3094 * the original surface, and it's released when the last surface referencing
3095 * it is released. I guess that's beyond Wine's surface management right now
3096 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3097 * test application to implement this)
3099 * Params:
3100 * Src: Address of the source surface
3101 * Dest: Address to write the new surface pointer to
3103 * Returns:
3104 * See IDirectDraw7::CreateSurface
3106 *****************************************************************************/
3107 static HRESULT WINAPI
3108 IDirectDrawImpl_DuplicateSurface(IDirectDraw7 *iface,
3109 IDirectDrawSurface7 *Src,
3110 IDirectDrawSurface7 **Dest)
3112 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
3113 IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Src;
3115 FIXME("(%p)->(%p,%p)\n", This, Surf, Dest);
3117 /* For now, simply create a new, independent surface */
3118 return IDirectDraw7_CreateSurface(iface,
3119 &Surf->surface_desc,
3120 Dest,
3121 NULL);
3124 /*****************************************************************************
3125 * IDirectDraw7 VTable
3126 *****************************************************************************/
3127 const IDirectDraw7Vtbl IDirectDraw7_Vtbl =
3129 /*** IUnknown ***/
3130 IDirectDrawImpl_QueryInterface,
3131 IDirectDrawImpl_AddRef,
3132 IDirectDrawImpl_Release,
3133 /*** IDirectDraw ***/
3134 IDirectDrawImpl_Compact,
3135 IDirectDrawImpl_CreateClipper,
3136 IDirectDrawImpl_CreatePalette,
3137 IDirectDrawImpl_CreateSurface,
3138 IDirectDrawImpl_DuplicateSurface,
3139 IDirectDrawImpl_EnumDisplayModes,
3140 IDirectDrawImpl_EnumSurfaces,
3141 IDirectDrawImpl_FlipToGDISurface,
3142 IDirectDrawImpl_GetCaps,
3143 IDirectDrawImpl_GetDisplayMode,
3144 IDirectDrawImpl_GetFourCCCodes,
3145 IDirectDrawImpl_GetGDISurface,
3146 IDirectDrawImpl_GetMonitorFrequency,
3147 IDirectDrawImpl_GetScanLine,
3148 IDirectDrawImpl_GetVerticalBlankStatus,
3149 IDirectDrawImpl_Initialize,
3150 IDirectDrawImpl_RestoreDisplayMode,
3151 IDirectDrawImpl_SetCooperativeLevel,
3152 IDirectDrawImpl_SetDisplayMode,
3153 IDirectDrawImpl_WaitForVerticalBlank,
3154 /*** IDirectDraw2 ***/
3155 IDirectDrawImpl_GetAvailableVidMem,
3156 /*** IDirectDraw3 ***/
3157 IDirectDrawImpl_GetSurfaceFromDC,
3158 /*** IDirectDraw4 ***/
3159 IDirectDrawImpl_RestoreAllSurfaces,
3160 IDirectDrawImpl_TestCooperativeLevel,
3161 IDirectDrawImpl_GetDeviceIdentifier,
3162 /*** IDirectDraw7 ***/
3163 IDirectDrawImpl_StartModeTest,
3164 IDirectDrawImpl_EvaluateMode
3167 /*****************************************************************************
3168 * IDirectDrawImpl_FindDecl
3170 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
3171 * if none was found.
3173 * This function is in ddraw.c and the DDraw object space because D3D7
3174 * vertex buffers are created using the IDirect3D interface to the ddraw
3175 * object, so they can be valid across D3D devices(theoretically. The ddraw
3176 * object also owns the wined3d device
3178 * Parameters:
3179 * This: Device
3180 * fvf: Fvf to find the decl for
3182 * Returns:
3183 * NULL in case of an error, the IWineD3DVertexDeclaration interface for the
3184 * fvf otherwise.
3186 *****************************************************************************/
3187 IWineD3DVertexDeclaration *
3188 IDirectDrawImpl_FindDecl(IDirectDrawImpl *This,
3189 DWORD fvf)
3191 HRESULT hr;
3192 IWineD3DVertexDeclaration* pDecl = NULL;
3193 int p, low, high; /* deliberately signed */
3194 struct FvfToDecl *convertedDecls = This->decls;
3196 TRACE("Searching for declaration for fvf %08x... ", fvf);
3198 low = 0;
3199 high = This->numConvertedDecls - 1;
3200 while(low <= high) {
3201 p = (low + high) >> 1;
3202 TRACE("%d ", p);
3203 if(convertedDecls[p].fvf == fvf) {
3204 TRACE("found %p\n", convertedDecls[p].decl);
3205 return convertedDecls[p].decl;
3206 } else if(convertedDecls[p].fvf < fvf) {
3207 low = p + 1;
3208 } else {
3209 high = p - 1;
3212 TRACE("not found. Creating and inserting at position %d.\n", low);
3214 hr = IWineD3DDevice_CreateVertexDeclarationFromFVF(This->wineD3DDevice, &pDecl,
3215 (IUnknown *)This, &ddraw_null_wined3d_parent_ops, fvf);
3216 if (hr != S_OK) return NULL;
3218 if(This->declArraySize == This->numConvertedDecls) {
3219 int grow = max(This->declArraySize / 2, 8);
3220 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
3221 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
3222 if(!convertedDecls) {
3223 /* This will destroy it */
3224 IWineD3DVertexDeclaration_Release(pDecl);
3225 return NULL;
3227 This->decls = convertedDecls;
3228 This->declArraySize += grow;
3231 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
3232 convertedDecls[low].decl = pDecl;
3233 convertedDecls[low].fvf = fvf;
3234 This->numConvertedDecls++;
3236 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
3237 return pDecl;
3240 /* IWineD3DDeviceParent IUnknown methods */
3242 static inline struct IDirectDrawImpl *ddraw_from_device_parent(IWineD3DDeviceParent *iface)
3244 return (struct IDirectDrawImpl *)((char*)iface - FIELD_OFFSET(struct IDirectDrawImpl, device_parent_vtbl));
3247 static HRESULT STDMETHODCALLTYPE device_parent_QueryInterface(IWineD3DDeviceParent *iface, REFIID riid, void **object)
3249 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3250 return IDirectDrawImpl_QueryInterface((IDirectDraw7 *)This, riid, object);
3253 static ULONG STDMETHODCALLTYPE device_parent_AddRef(IWineD3DDeviceParent *iface)
3255 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3256 return IDirectDrawImpl_AddRef((IDirectDraw7 *)This);
3259 static ULONG STDMETHODCALLTYPE device_parent_Release(IWineD3DDeviceParent *iface)
3261 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3262 return IDirectDrawImpl_Release((IDirectDraw7 *)This);
3265 /* IWineD3DDeviceParent methods */
3267 static void STDMETHODCALLTYPE device_parent_WineD3DDeviceCreated(IWineD3DDeviceParent *iface, IWineD3DDevice *device)
3269 TRACE("iface %p, device %p\n", iface, device);
3272 static HRESULT STDMETHODCALLTYPE device_parent_CreateSurface(IWineD3DDeviceParent *iface,
3273 IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, DWORD usage,
3274 WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, IWineD3DSurface **surface)
3276 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3277 IDirectDrawSurfaceImpl *surf = NULL;
3278 UINT i = 0;
3279 DDSCAPS2 searchcaps = This->tex_root->surface_desc.ddsCaps;
3281 TRACE("iface %p, superior %p, width %u, height %u, format %#x, usage %#x,\n"
3282 "\tpool %#x, level %u, face %u, surface %p\n",
3283 iface, superior, width, height, format, usage, pool, level, face, surface);
3285 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
3286 switch(face)
3288 case WINED3DCUBEMAP_FACE_POSITIVE_X:
3289 TRACE("Asked for positive x\n");
3290 if (searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP)
3292 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
3294 surf = This->tex_root; break;
3295 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
3296 TRACE("Asked for negative x\n");
3297 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
3298 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
3299 TRACE("Asked for positive y\n");
3300 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
3301 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
3302 TRACE("Asked for negative y\n");
3303 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
3304 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
3305 TRACE("Asked for positive z\n");
3306 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
3307 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
3308 TRACE("Asked for negative z\n");
3309 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
3310 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
3313 if (!surf)
3315 IDirectDrawSurface7 *attached;
3316 IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)This->tex_root, &searchcaps, &attached);
3317 surf = (IDirectDrawSurfaceImpl *)attached;
3318 IDirectDrawSurface7_Release(attached);
3320 if (!surf) ERR("root search surface not found\n");
3322 /* Find the wanted mipmap. There are enough mipmaps in the chain */
3323 while (i < level)
3325 IDirectDrawSurface7 *attached;
3326 IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)surf, &searchcaps, &attached);
3327 if(!attached) ERR("Surface not found\n");
3328 surf = (IDirectDrawSurfaceImpl *)attached;
3329 IDirectDrawSurface7_Release(attached);
3330 ++i;
3333 /* Return the surface */
3334 *surface = surf->WineD3DSurface;
3335 IWineD3DSurface_AddRef(*surface);
3337 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, surf);
3339 return D3D_OK;
3342 static HRESULT STDMETHODCALLTYPE device_parent_CreateRenderTarget(IWineD3DDeviceParent *iface,
3343 IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
3344 DWORD multisample_quality, BOOL lockable, IWineD3DSurface **surface)
3346 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3347 IDirectDrawSurfaceImpl *d3d_surface = This->d3d_target;
3348 IDirectDrawSurfaceImpl *target = NULL;
3350 TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3351 "\tmultisample_quality %u, lockable %u, surface %p\n",
3352 iface, superior, width, height, format, multisample_type, multisample_quality, lockable, surface);
3354 if (d3d_surface->isRenderTarget)
3356 IDirectDrawSurface7_EnumAttachedSurfaces((IDirectDrawSurface7 *)d3d_surface, &target, findRenderTarget);
3358 else
3360 target = d3d_surface;
3363 if (!target)
3365 target = This->d3d_target;
3366 ERR(" (%p) : No DirectDrawSurface found to create the back buffer. Using the front buffer as back buffer. Uncertain consequences\n", This);
3369 /* TODO: Return failure if the dimensions do not match, but this shouldn't happen */
3371 *surface = target->WineD3DSurface;
3372 IWineD3DSurface_AddRef(*surface);
3373 target->isRenderTarget = TRUE;
3375 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, d3d_surface);
3377 return D3D_OK;
3380 static HRESULT STDMETHODCALLTYPE device_parent_CreateDepthStencilSurface(IWineD3DDeviceParent *iface,
3381 IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
3382 DWORD multisample_quality, BOOL discard, IWineD3DSurface **surface)
3384 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3385 IDirectDrawSurfaceImpl *ddraw_surface;
3386 DDSURFACEDESC2 ddsd;
3387 HRESULT hr;
3389 TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3390 "\tmultisample_quality %u, discard %u, surface %p\n",
3391 iface, superior, width, height, format, multisample_type, multisample_quality, discard, surface);
3393 *surface = NULL;
3395 /* Create a DirectDraw surface */
3396 memset(&ddsd, 0, sizeof(ddsd));
3397 ddsd.dwSize = sizeof(ddsd);
3398 ddsd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
3399 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
3400 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
3401 ddsd.dwHeight = height;
3402 ddsd.dwWidth = width;
3403 if (format)
3405 PixelFormat_WineD3DtoDD(&ddsd.u4.ddpfPixelFormat, format);
3407 else
3409 ddsd.dwFlags ^= DDSD_PIXELFORMAT;
3412 This->depthstencil = TRUE;
3413 hr = IDirectDraw7_CreateSurface((IDirectDraw7 *)This, &ddsd, (IDirectDrawSurface7 **)&ddraw_surface, NULL);
3414 This->depthstencil = FALSE;
3415 if(FAILED(hr))
3417 ERR(" (%p) Creating a DepthStencil Surface failed, result = %x\n", This, hr);
3418 return hr;
3421 *surface = ddraw_surface->WineD3DSurface;
3422 IWineD3DSurface_AddRef(*surface);
3423 IDirectDrawSurface7_Release((IDirectDrawSurface7 *)ddraw_surface);
3425 return D3D_OK;
3428 static HRESULT STDMETHODCALLTYPE device_parent_CreateVolume(IWineD3DDeviceParent *iface,
3429 IUnknown *superior, UINT width, UINT height, UINT depth, WINED3DFORMAT format,
3430 WINED3DPOOL pool, DWORD usage, IWineD3DVolume **volume)
3432 TRACE("iface %p, superior %p, width %u, height %u, depth %u, format %#x, pool %#x, usage %#x, volume %p\n",
3433 iface, superior, width, height, depth, format, pool, usage, volume);
3435 ERR("Not implemented!\n");
3437 return E_NOTIMPL;
3440 static HRESULT STDMETHODCALLTYPE device_parent_CreateSwapChain(IWineD3DDeviceParent *iface,
3441 WINED3DPRESENT_PARAMETERS *present_parameters, IWineD3DSwapChain **swapchain)
3443 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3444 IDirectDrawSurfaceImpl *iterator;
3445 IParentImpl *object;
3446 HRESULT hr;
3448 TRACE("iface %p, present_parameters %p, swapchain %p\n", iface, present_parameters, swapchain);
3450 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IParentImpl));
3451 if (!object)
3453 FIXME("Allocation of memory failed\n");
3454 *swapchain = NULL;
3455 return DDERR_OUTOFVIDEOMEMORY;
3458 object->lpVtbl = &IParent_Vtbl;
3459 object->ref = 1;
3461 hr = IWineD3DDevice_CreateSwapChain(This->wineD3DDevice, present_parameters,
3462 swapchain, (IUnknown *)object, This->ImplType);
3463 if (FAILED(hr))
3465 FIXME("(%p) CreateSwapChain failed, returning %#x\n", iface, hr);
3466 HeapFree(GetProcessHeap(), 0 , object);
3467 *swapchain = NULL;
3468 return hr;
3471 object->child = (IUnknown *)*swapchain;
3472 This->d3d_target->wineD3DSwapChain = *swapchain;
3473 iterator = This->d3d_target->complex_array[0];
3474 while (iterator)
3476 iterator->wineD3DSwapChain = *swapchain;
3477 iterator = iterator->complex_array[0];
3480 return hr;
3483 const IWineD3DDeviceParentVtbl ddraw_wined3d_device_parent_vtbl =
3485 /* IUnknown methods */
3486 device_parent_QueryInterface,
3487 device_parent_AddRef,
3488 device_parent_Release,
3489 /* IWineD3DDeviceParent methods */
3490 device_parent_WineD3DDeviceCreated,
3491 device_parent_CreateSurface,
3492 device_parent_CreateRenderTarget,
3493 device_parent_CreateDepthStencilSurface,
3494 device_parent_CreateVolume,
3495 device_parent_CreateSwapChain,