msi: Directly pass the disk id to installfiles_cb.
[wine/hacks.git] / dlls / ddraw / ddraw.c
bloba8dbafae2ded3e5aa027906672a4a6d9fb55954e
1 /*
2 * Copyright 1997-2000 Marcus Meissner
3 * Copyright 1998-2000 Lionel Ulmer
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2006 Stefan Dösinger
6 * Copyright 2008 Denver Gingerich
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <stdlib.h>
31 #define COBJMACROS
32 #define NONAMELESSUNION
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "wingdi.h"
38 #include "wine/exception.h"
40 #include "ddraw.h"
41 #include "d3d.h"
43 #include "ddraw_private.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
48 static BOOL IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested, const DDSURFACEDESC2* provided);
49 static HRESULT IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This, IDirectDrawSurfaceImpl *primary);
50 static HRESULT IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This, DDSURFACEDESC2 *pDDSD, IDirectDrawSurfaceImpl **ppSurf, UINT level);
51 static HRESULT IDirectDrawImpl_CreateGDISwapChain(IDirectDrawImpl *This, IDirectDrawSurfaceImpl *primary);
53 /* Device identifier. Don't relay it to WineD3D */
54 static const DDDEVICEIDENTIFIER2 deviceidentifier =
56 "display",
57 "DirectDraw HAL",
58 { { 0x00010001, 0x00010001 } },
59 0, 0, 0, 0,
60 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
61 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
65 static void STDMETHODCALLTYPE ddraw_null_wined3d_object_destroyed(void *parent) {}
67 const struct wined3d_parent_ops ddraw_null_wined3d_parent_ops =
69 ddraw_null_wined3d_object_destroyed,
72 /*****************************************************************************
73 * IUnknown Methods
74 *****************************************************************************/
76 /*****************************************************************************
77 * IDirectDraw7::QueryInterface
79 * Queries different interfaces of the DirectDraw object. It can return
80 * IDirectDraw interfaces in version 1, 2, 4 and 7, and IDirect3D interfaces
81 * in version 1, 2, 3 and 7. An IDirect3DDevice can be created with this
82 * method.
83 * The returned interface is AddRef()-ed before it's returned
85 * Used for version 1, 2, 4 and 7
87 * Params:
88 * refiid: Interface ID asked for
89 * obj: Used to return the interface pointer
91 * Returns:
92 * S_OK if an interface was found
93 * E_NOINTERFACE if the requested interface wasn't found
95 *****************************************************************************/
96 static HRESULT WINAPI
97 IDirectDrawImpl_QueryInterface(IDirectDraw7 *iface,
98 REFIID refiid,
99 void **obj)
101 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
103 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(refiid), obj);
105 /* Can change surface impl type */
106 EnterCriticalSection(&ddraw_cs);
108 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
109 *obj = NULL;
111 if(!refiid)
113 LeaveCriticalSection(&ddraw_cs);
114 return DDERR_INVALIDPARAMS;
117 /* Check DirectDraw Interfaces */
118 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
119 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
121 *obj = This;
122 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
124 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
126 *obj = &This->IDirectDraw4_vtbl;
127 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
129 else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
131 /* This Interface exists in ddrawex.dll, it is implemented in a wrapper */
132 WARN("IDirectDraw3 is not valid in ddraw.dll\n");
133 *obj = NULL;
134 LeaveCriticalSection(&ddraw_cs);
135 return E_NOINTERFACE;
137 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
139 *obj = &This->IDirectDraw2_vtbl;
140 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
142 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
144 *obj = &This->IDirectDraw_vtbl;
145 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
148 /* Direct3D
149 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
150 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
151 * who had this idea and why. The older interfaces can query and IDirect3D version
152 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
153 * and messy to implement with the common creation function, so it has been left out here.
155 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
156 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
157 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
158 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
160 /* Check the surface implementation */
161 if(This->ImplType == SURFACE_UNKNOWN)
163 /* Apps may create the IDirect3D Interface before the primary surface.
164 * set the surface implementation */
165 This->ImplType = SURFACE_OPENGL;
166 TRACE("(%p) Choosing OpenGL surfaces because a Direct3D interface was requested\n", This);
168 else if(This->ImplType != SURFACE_OPENGL && DefaultSurfaceType == SURFACE_UNKNOWN)
170 ERR("(%p) The App is requesting a D3D device, but a non-OpenGL surface type was choosen. Prepare for trouble!\n", This);
171 ERR(" (%p) You may want to contact wine-devel for help\n", This);
172 /* Should I assert(0) here??? */
174 else if(This->ImplType != SURFACE_OPENGL)
176 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
177 /* Do not abort here, only reject 3D Device creation */
180 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
182 This->d3dversion = 1;
183 *obj = &This->IDirect3D_vtbl;
184 TRACE(" returning Direct3D interface at %p.\n", *obj);
186 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
188 This->d3dversion = 2;
189 *obj = &This->IDirect3D2_vtbl;
190 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
192 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
194 This->d3dversion = 3;
195 *obj = &This->IDirect3D3_vtbl;
196 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
198 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
200 This->d3dversion = 7;
201 *obj = &This->IDirect3D7_vtbl;
202 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
205 else if (IsEqualGUID(refiid, &IID_IWineD3DDeviceParent))
207 *obj = &This->device_parent_vtbl;
210 /* Unknown interface */
211 else
213 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
214 LeaveCriticalSection(&ddraw_cs);
215 return E_NOINTERFACE;
218 IUnknown_AddRef( (IUnknown *) *obj );
219 LeaveCriticalSection(&ddraw_cs);
220 return S_OK;
223 /*****************************************************************************
224 * IDirectDraw7::AddRef
226 * Increases the interfaces refcount, basically
228 * DDraw refcounting is a bit tricky. The different DirectDraw interface
229 * versions have individual refcounts, but the IDirect3D interfaces do not.
230 * All interfaces are from one object, that means calling QueryInterface on an
231 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
232 * IDirectDrawImpl object.
234 * That means all AddRef and Release implementations of IDirectDrawX work
235 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
236 * except of IDirect3D7 which thunks to IDirectDraw7
238 * Returns: The new refcount
240 *****************************************************************************/
241 static ULONG WINAPI
242 IDirectDrawImpl_AddRef(IDirectDraw7 *iface)
244 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
245 ULONG ref = InterlockedIncrement(&This->ref7);
247 TRACE("(%p) : incrementing IDirectDraw7 refcount from %u.\n", This, ref -1);
249 if(ref == 1) InterlockedIncrement(&This->numIfaces);
251 return ref;
254 /*****************************************************************************
255 * IDirectDrawImpl_Destroy
257 * Destroys a ddraw object if all refcounts are 0. This is to share code
258 * between the IDirectDrawX::Release functions
260 * Params:
261 * This: DirectDraw object to destroy
263 *****************************************************************************/
264 void
265 IDirectDrawImpl_Destroy(IDirectDrawImpl *This)
267 IDirectDraw7_SetCooperativeLevel((IDirectDraw7 *)This, NULL, DDSCL_NORMAL);
268 IDirectDraw7_RestoreDisplayMode((IDirectDraw7 *)This);
270 /* Destroy the device window if we created one */
271 if(This->devicewindow != 0)
273 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
274 DestroyWindow(This->devicewindow);
275 This->devicewindow = 0;
278 /* Unregister the window class */
279 UnregisterClassA(This->classname, 0);
281 EnterCriticalSection(&ddraw_cs);
282 list_remove(&This->ddraw_list_entry);
283 LeaveCriticalSection(&ddraw_cs);
285 /* Release the attached WineD3D stuff */
286 IWineD3DDevice_Release(This->wineD3DDevice);
287 IWineD3D_Release(This->wineD3D);
289 /* Now free the object */
290 HeapFree(GetProcessHeap(), 0, This);
293 /*****************************************************************************
294 * IDirectDraw7::Release
296 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
298 * Returns: The new refcount
299 *****************************************************************************/
300 static ULONG WINAPI
301 IDirectDrawImpl_Release(IDirectDraw7 *iface)
303 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
304 ULONG ref = InterlockedDecrement(&This->ref7);
306 TRACE("(%p)->() decrementing IDirectDraw7 refcount from %u.\n", This, ref +1);
308 if(ref == 0)
310 ULONG ifacecount = InterlockedDecrement(&This->numIfaces);
311 if(ifacecount == 0) IDirectDrawImpl_Destroy(This);
314 return ref;
317 /*****************************************************************************
318 * IDirectDraw methods
319 *****************************************************************************/
321 /*****************************************************************************
322 * IDirectDraw7::SetCooperativeLevel
324 * Sets the cooperative level for the DirectDraw object, and the window
325 * assigned to it. The cooperative level determines the general behavior
326 * of the DirectDraw application
328 * Warning: This is quite tricky, as it's not really documented which
329 * cooperative levels can be combined with each other. If a game fails
330 * after this function, try to check the cooperative levels passed on
331 * Windows, and if it returns something different.
333 * If you think that this function caused the failure because it writes a
334 * fixme, be sure to run again with a +ddraw trace.
336 * What is known about cooperative levels (See the ddraw modes test):
337 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
338 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
339 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
340 * DDSCL_FULLSCREEN can be activated
341 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
343 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
344 * DDSCL_SETFOCUSWINDOW (partially),
345 * DDSCL_MULTITHREADED (work in progress)
347 * Unhandled flags, which should be implemented
348 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
349 * expect any difference to a normal window for wine)
350 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
351 * rendering (Possible test case: Half-life)
353 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
355 * These don't seem very important for wine:
356 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
358 * Returns:
359 * DD_OK if the cooperative level was set successfully
360 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
361 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
362 * (Probably others too, have to investigate)
364 *****************************************************************************/
365 static HRESULT WINAPI
366 IDirectDrawImpl_SetCooperativeLevel(IDirectDraw7 *iface,
367 HWND hwnd,
368 DWORD cooplevel)
370 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
371 HWND window;
373 TRACE("(%p)->(%p,%08x)\n",This,hwnd,cooplevel);
374 DDRAW_dump_cooperativelevel(cooplevel);
376 EnterCriticalSection(&ddraw_cs);
378 /* Get the old window */
379 window = This->dest_window;
381 /* Tests suggest that we need one of them: */
382 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
383 DDSCL_NORMAL |
384 DDSCL_EXCLUSIVE )))
386 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
387 LeaveCriticalSection(&ddraw_cs);
388 return DDERR_INVALIDPARAMS;
391 /* Handle those levels first which set various hwnds */
392 if(cooplevel & DDSCL_SETFOCUSWINDOW)
394 /* This isn't compatible with a lot of flags */
395 if(cooplevel & ( DDSCL_MULTITHREADED |
396 DDSCL_FPUSETUP |
397 DDSCL_FPUPRESERVE |
398 DDSCL_ALLOWREBOOT |
399 DDSCL_ALLOWMODEX |
400 DDSCL_SETDEVICEWINDOW |
401 DDSCL_NORMAL |
402 DDSCL_EXCLUSIVE |
403 DDSCL_FULLSCREEN ) )
405 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
406 LeaveCriticalSection(&ddraw_cs);
407 return DDERR_INVALIDPARAMS;
409 else if( (This->cooperative_level & DDSCL_FULLSCREEN) && window)
411 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET\n");
412 LeaveCriticalSection(&ddraw_cs);
413 return DDERR_HWNDALREADYSET;
416 This->focuswindow = hwnd;
417 /* Won't use the hwnd param for anything else */
418 hwnd = NULL;
420 /* Use the focus window for drawing too */
421 This->dest_window = This->focuswindow;
423 /* Destroy the device window, if we have one */
424 if(This->devicewindow)
426 DestroyWindow(This->devicewindow);
427 This->devicewindow = NULL;
430 /* DDSCL_NORMAL or DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE */
431 if(cooplevel & DDSCL_NORMAL)
433 /* Can't coexist with fullscreen or exclusive */
434 if(cooplevel & (DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE) )
436 TRACE("(%p) DDSCL_NORMAL is not compative with DDSCL_FULLSCREEN or DDSCL_EXCLUSIVE\n", This);
437 LeaveCriticalSection(&ddraw_cs);
438 return DDERR_INVALIDPARAMS;
441 /* Switching from fullscreen? */
442 if(This->cooperative_level & DDSCL_FULLSCREEN)
444 This->cooperative_level &= ~DDSCL_FULLSCREEN;
445 This->cooperative_level &= ~DDSCL_EXCLUSIVE;
446 This->cooperative_level &= ~DDSCL_ALLOWMODEX;
448 IWineD3DDevice_ReleaseFocusWindow(This->wineD3DDevice);
451 /* Don't override focus windows or private device windows */
452 if( hwnd &&
453 !(This->focuswindow) &&
454 !(This->devicewindow) &&
455 (hwnd != window) )
457 This->dest_window = hwnd;
460 else if(cooplevel & DDSCL_FULLSCREEN)
462 /* Needs DDSCL_EXCLUSIVE */
463 if(!(cooplevel & DDSCL_EXCLUSIVE) )
465 TRACE("(%p) DDSCL_FULLSCREEN needs DDSCL_EXCLUSIVE\n", This);
466 LeaveCriticalSection(&ddraw_cs);
467 return DDERR_INVALIDPARAMS;
469 /* Need a HWND
470 if(hwnd == 0)
472 TRACE("(%p) DDSCL_FULLSCREEN needs a HWND\n", This);
473 return DDERR_INVALIDPARAMS;
477 This->cooperative_level &= ~DDSCL_NORMAL;
479 /* Don't override focus windows or private device windows */
480 if( hwnd &&
481 !(This->focuswindow) &&
482 !(This->devicewindow) &&
483 (hwnd != window) )
485 HRESULT hr = IWineD3DDevice_AcquireFocusWindow(This->wineD3DDevice, hwnd);
486 if (FAILED(hr))
488 ERR("Failed to acquire focus window, hr %#x.\n", hr);
489 LeaveCriticalSection(&ddraw_cs);
490 return hr;
492 This->dest_window = hwnd;
495 else if(cooplevel & DDSCL_EXCLUSIVE)
497 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN\n", This);
498 LeaveCriticalSection(&ddraw_cs);
499 return DDERR_INVALIDPARAMS;
502 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
504 /* Don't create a device window if a focus window is set */
505 if( !(This->focuswindow) )
507 HWND devicewindow = CreateWindowExA(0, This->classname, "DDraw device window",
508 WS_POPUP, 0, 0,
509 GetSystemMetrics(SM_CXSCREEN),
510 GetSystemMetrics(SM_CYSCREEN),
511 NULL, NULL, GetModuleHandleA(0), NULL);
513 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
514 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
516 This->devicewindow = devicewindow;
517 This->dest_window = devicewindow;
521 if(cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
523 /* Enable thread safety in wined3d */
524 IWineD3DDevice_SetMultithreaded(This->wineD3DDevice);
527 /* Unhandled flags */
528 if(cooplevel & DDSCL_ALLOWREBOOT)
529 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
530 if(cooplevel & DDSCL_ALLOWMODEX)
531 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
532 if(cooplevel & DDSCL_FPUSETUP)
533 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
535 /* Store the cooperative_level */
536 This->cooperative_level |= cooplevel;
537 TRACE("SetCooperativeLevel retuning DD_OK\n");
538 LeaveCriticalSection(&ddraw_cs);
539 return DD_OK;
542 /*****************************************************************************
544 * Helper function for SetDisplayMode and RestoreDisplayMode
546 * Implements DirectDraw's SetDisplayMode, but ignores the value of
547 * ForceRefreshRate, since it is already handled by
548 * IDirectDrawImpl_SetDisplayMode. RestoreDisplayMode can use this function
549 * without worrying that ForceRefreshRate will override the refresh rate. For
550 * argument and return value documentation, see
551 * IDirectDrawImpl_SetDisplayMode.
553 *****************************************************************************/
554 static HRESULT
555 IDirectDrawImpl_SetDisplayModeNoOverride(IDirectDraw7 *iface,
556 DWORD Width,
557 DWORD Height,
558 DWORD BPP,
559 DWORD RefreshRate,
560 DWORD Flags)
562 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
563 WINED3DDISPLAYMODE Mode;
564 HRESULT hr;
565 TRACE("(%p)->(%d,%d,%d,%d,%x): Relay!\n", This, Width, Height, BPP, RefreshRate, Flags);
567 EnterCriticalSection(&ddraw_cs);
568 if( !Width || !Height )
570 ERR("Width=%d, Height=%d, what to do?\n", Width, Height);
571 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
572 LeaveCriticalSection(&ddraw_cs);
573 return DD_OK;
576 /* Check the exclusive mode
577 if(!(This->cooperative_level & DDSCL_EXCLUSIVE))
578 return DDERR_NOEXCLUSIVEMODE;
579 * This is WRONG. Don't know if the SDK is completely
580 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
581 * is returned, but Half-Life 1.1.1.1 (Steam version)
582 * depends on this
585 Mode.Width = Width;
586 Mode.Height = Height;
587 Mode.RefreshRate = RefreshRate;
588 switch(BPP)
590 case 8: Mode.Format = WINED3DFMT_P8_UINT; break;
591 case 15: Mode.Format = WINED3DFMT_B5G5R5X1_UNORM; break;
592 case 16: Mode.Format = WINED3DFMT_B5G6R5_UNORM; break;
593 case 24: Mode.Format = WINED3DFMT_B8G8R8_UNORM; break;
594 case 32: Mode.Format = WINED3DFMT_B8G8R8X8_UNORM; break;
597 /* TODO: The possible return values from msdn suggest that
598 * the screen mode can't be changed if a surface is locked
599 * or some drawing is in progress
602 /* TODO: Lose the primary surface */
603 hr = IWineD3DDevice_SetDisplayMode(This->wineD3DDevice,
604 0, /* First swapchain */
605 &Mode);
606 LeaveCriticalSection(&ddraw_cs);
607 switch(hr)
609 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
610 default: return hr;
614 /*****************************************************************************
615 * IDirectDraw7::SetDisplayMode
617 * Sets the display screen resolution, color depth and refresh frequency
618 * when in fullscreen mode (in theory).
619 * Possible return values listed in the SDK suggest that this method fails
620 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
621 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
622 * It seems to be valid to pass 0 for With and Height, this has to be tested
623 * It could mean that the current video mode should be left as-is. (But why
624 * call it then?)
626 * Params:
627 * Height, Width: Screen dimension
628 * BPP: Color depth in Bits per pixel
629 * Refreshrate: Screen refresh rate
630 * Flags: Other stuff
632 * Returns
633 * DD_OK on success
635 *****************************************************************************/
636 static HRESULT WINAPI
637 IDirectDrawImpl_SetDisplayMode(IDirectDraw7 *iface,
638 DWORD Width,
639 DWORD Height,
640 DWORD BPP,
641 DWORD RefreshRate,
642 DWORD Flags)
644 if (force_refresh_rate != 0)
646 TRACE("ForceRefreshRate overriding passed-in refresh rate (%d Hz) to %d Hz\n", RefreshRate, force_refresh_rate);
647 RefreshRate = force_refresh_rate;
650 return IDirectDrawImpl_SetDisplayModeNoOverride(iface, Width, Height, BPP,
651 RefreshRate, Flags);
654 /*****************************************************************************
655 * IDirectDraw7::RestoreDisplayMode
657 * Restores the display mode to what it was at creation time. Basically.
659 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
660 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
661 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
662 * -> DD_1 is released. The screen should be left at 1024x768x32.
663 * -> DD_2 is released. The screen should be set to 1400x1050x32
664 * This case is unhandled right now, but Empire Earth does it this way.
665 * (But perhaps there is something in SetCooperativeLevel to prevent this)
667 * The msdn says that this method resets the display mode to what it was before
668 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
670 * Returns
671 * DD_OK on success
672 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
674 *****************************************************************************/
675 static HRESULT WINAPI
676 IDirectDrawImpl_RestoreDisplayMode(IDirectDraw7 *iface)
678 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
679 TRACE("(%p)\n", This);
681 return IDirectDrawImpl_SetDisplayModeNoOverride(iface,
682 This->orig_width, This->orig_height, This->orig_bpp, 0, 0);
685 /*****************************************************************************
686 * IDirectDraw7::GetCaps
688 * Returns the drives capabilities
690 * Used for version 1, 2, 4 and 7
692 * Params:
693 * DriverCaps: Structure to write the Hardware accelerated caps to
694 * HelCaps: Structure to write the emulation caps to
696 * Returns
697 * This implementation returns DD_OK only
699 *****************************************************************************/
700 static HRESULT WINAPI
701 IDirectDrawImpl_GetCaps(IDirectDraw7 *iface,
702 DDCAPS *DriverCaps,
703 DDCAPS *HELCaps)
705 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
706 DDCAPS caps;
707 WINED3DCAPS winecaps;
708 HRESULT hr;
709 DDSCAPS2 ddscaps = {0, 0, 0, 0};
710 TRACE("(%p)->(%p,%p)\n", This, DriverCaps, HELCaps);
712 /* One structure must be != NULL */
713 if( (!DriverCaps) && (!HELCaps) )
715 ERR("(%p) Invalid params to IDirectDrawImpl_GetCaps\n", This);
716 return DDERR_INVALIDPARAMS;
719 memset(&caps, 0, sizeof(caps));
720 memset(&winecaps, 0, sizeof(winecaps));
721 caps.dwSize = sizeof(caps);
722 EnterCriticalSection(&ddraw_cs);
723 hr = IWineD3DDevice_GetDeviceCaps(This->wineD3DDevice, &winecaps);
724 if(FAILED(hr)) {
725 WARN("IWineD3DDevice::GetDeviceCaps failed\n");
726 LeaveCriticalSection(&ddraw_cs);
727 return hr;
730 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
731 LeaveCriticalSection(&ddraw_cs);
732 if(FAILED(hr)) {
733 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
734 return hr;
737 caps.dwCaps = winecaps.DirectDrawCaps.Caps;
738 caps.dwCaps2 = winecaps.DirectDrawCaps.Caps2;
739 caps.dwCKeyCaps = winecaps.DirectDrawCaps.CKeyCaps;
740 caps.dwFXCaps = winecaps.DirectDrawCaps.FXCaps;
741 caps.dwPalCaps = winecaps.DirectDrawCaps.PalCaps;
742 caps.ddsCaps.dwCaps = winecaps.DirectDrawCaps.ddsCaps;
743 caps.dwSVBCaps = winecaps.DirectDrawCaps.SVBCaps;
744 caps.dwSVBCKeyCaps = winecaps.DirectDrawCaps.SVBCKeyCaps;
745 caps.dwSVBFXCaps = winecaps.DirectDrawCaps.SVBFXCaps;
746 caps.dwVSBCaps = winecaps.DirectDrawCaps.VSBCaps;
747 caps.dwVSBCKeyCaps = winecaps.DirectDrawCaps.VSBCKeyCaps;
748 caps.dwVSBFXCaps = winecaps.DirectDrawCaps.VSBFXCaps;
749 caps.dwSSBCaps = winecaps.DirectDrawCaps.SSBCaps;
750 caps.dwSSBCKeyCaps = winecaps.DirectDrawCaps.SSBCKeyCaps;
751 caps.dwSSBFXCaps = winecaps.DirectDrawCaps.SSBFXCaps;
753 /* Even if WineD3D supports 3D rendering, remove the cap if ddraw is configured
754 * not to use it
756 if(DefaultSurfaceType == SURFACE_GDI) {
757 caps.dwCaps &= ~DDCAPS_3D;
758 caps.ddsCaps.dwCaps &= ~(DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER);
760 if(winecaps.DirectDrawCaps.StrideAlign != 0) {
761 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
762 caps.dwAlignStrideAlign = winecaps.DirectDrawCaps.StrideAlign;
765 if(DriverCaps)
767 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
768 if (TRACE_ON(ddraw))
770 TRACE("Driver Caps :\n");
771 DDRAW_dump_DDCAPS(DriverCaps);
775 if(HELCaps)
777 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
778 if (TRACE_ON(ddraw))
780 TRACE("HEL Caps :\n");
781 DDRAW_dump_DDCAPS(HELCaps);
785 return DD_OK;
788 /*****************************************************************************
789 * IDirectDraw7::Compact
791 * No idea what it does, MSDN says it's not implemented.
793 * Returns
794 * DD_OK, but this is unchecked
796 *****************************************************************************/
797 static HRESULT WINAPI
798 IDirectDrawImpl_Compact(IDirectDraw7 *iface)
800 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
801 TRACE("(%p)\n", This);
803 return DD_OK;
806 /*****************************************************************************
807 * IDirectDraw7::GetDisplayMode
809 * Returns information about the current display mode
811 * Exists in Version 1, 2, 4 and 7
813 * Params:
814 * DDSD: Address of a surface description structure to write the info to
816 * Returns
817 * DD_OK
819 *****************************************************************************/
820 static HRESULT WINAPI
821 IDirectDrawImpl_GetDisplayMode(IDirectDraw7 *iface,
822 DDSURFACEDESC2 *DDSD)
824 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
825 HRESULT hr;
826 WINED3DDISPLAYMODE Mode;
827 DWORD Size;
828 TRACE("(%p)->(%p): Relay\n", This, DDSD);
830 EnterCriticalSection(&ddraw_cs);
831 /* This seems sane */
832 if (!DDSD)
834 LeaveCriticalSection(&ddraw_cs);
835 return DDERR_INVALIDPARAMS;
838 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
839 * so one method can be used for all versions (Hopefully)
841 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
842 0 /* swapchain 0 */,
843 &Mode);
844 if( hr != D3D_OK )
846 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
847 LeaveCriticalSection(&ddraw_cs);
848 return hr;
851 Size = DDSD->dwSize;
852 memset(DDSD, 0, Size);
854 DDSD->dwSize = Size;
855 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
856 DDSD->dwWidth = Mode.Width;
857 DDSD->dwHeight = Mode.Height;
858 DDSD->u2.dwRefreshRate = 60;
859 DDSD->ddsCaps.dwCaps = 0;
860 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
861 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, Mode.Format);
862 DDSD->u1.lPitch = Mode.Width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
864 if(TRACE_ON(ddraw))
866 TRACE("Returning surface desc :\n");
867 DDRAW_dump_surface_desc(DDSD);
870 LeaveCriticalSection(&ddraw_cs);
871 return DD_OK;
874 /*****************************************************************************
875 * IDirectDraw7::GetFourCCCodes
877 * Returns an array of supported FourCC codes.
879 * Exists in Version 1, 2, 4 and 7
881 * Params:
882 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
883 * of enumerated codes
884 * Codes: Pointer to an array of DWORDs where the supported codes are written
885 * to
887 * Returns
888 * Always returns DD_OK, as it's a stub for now
890 *****************************************************************************/
891 static HRESULT WINAPI
892 IDirectDrawImpl_GetFourCCCodes(IDirectDraw7 *iface,
893 DWORD *NumCodes, DWORD *Codes)
895 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
896 WINED3DFORMAT formats[] = {
897 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
898 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
899 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
901 DWORD count = 0, i, outsize;
902 HRESULT hr;
903 WINED3DDISPLAYMODE d3ddm;
904 WINED3DSURFTYPE type = This->ImplType;
905 TRACE("(%p)->(%p, %p)\n", This, NumCodes, Codes);
907 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
908 0 /* swapchain 0 */,
909 &d3ddm);
911 outsize = NumCodes && Codes ? *NumCodes : 0;
913 if(type == SURFACE_UNKNOWN) type = SURFACE_GDI;
915 for(i = 0; i < (sizeof(formats) / sizeof(formats[0])); i++) {
916 hr = IWineD3D_CheckDeviceFormat(This->wineD3D,
917 WINED3DADAPTER_DEFAULT,
918 WINED3DDEVTYPE_HAL,
919 d3ddm.Format /* AdapterFormat */,
920 0 /* usage */,
921 WINED3DRTYPE_SURFACE,
922 formats[i],
923 type);
924 if(SUCCEEDED(hr)) {
925 if(count < outsize) {
926 Codes[count] = formats[i];
928 count++;
931 if(NumCodes) {
932 TRACE("Returning %u FourCC codes\n", count);
933 *NumCodes = count;
936 return DD_OK;
939 /*****************************************************************************
940 * IDirectDraw7::GetMonitorFrequency
942 * Returns the monitor's frequency
944 * Exists in Version 1, 2, 4 and 7
946 * Params:
947 * Freq: Pointer to a DWORD to write the frequency to
949 * Returns
950 * Always returns DD_OK
952 *****************************************************************************/
953 static HRESULT WINAPI
954 IDirectDrawImpl_GetMonitorFrequency(IDirectDraw7 *iface,
955 DWORD *Freq)
957 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
958 TRACE("(%p)->(%p)\n", This, Freq);
960 /* Ideally this should be in WineD3D, as it concerns the screen setup,
961 * but for now this should make the games happy
963 *Freq = 60;
964 return DD_OK;
967 /*****************************************************************************
968 * IDirectDraw7::GetVerticalBlankStatus
970 * Returns the Vertical blank status of the monitor. This should be in WineD3D
971 * too basically, but as it's a semi stub, I didn't create a function there
973 * Params:
974 * status: Pointer to a BOOL to be filled with the vertical blank status
976 * Returns
977 * DD_OK on success
978 * DDERR_INVALIDPARAMS if status is NULL
980 *****************************************************************************/
981 static HRESULT WINAPI
982 IDirectDrawImpl_GetVerticalBlankStatus(IDirectDraw7 *iface,
983 BOOL *status)
985 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
986 TRACE("(%p)->(%p)\n", This, status);
988 /* This looks sane, the MSDN suggests it too */
989 EnterCriticalSection(&ddraw_cs);
990 if(!status)
992 LeaveCriticalSection(&ddraw_cs);
993 return DDERR_INVALIDPARAMS;
996 *status = This->fake_vblank;
997 This->fake_vblank = !This->fake_vblank;
998 LeaveCriticalSection(&ddraw_cs);
999 return DD_OK;
1002 /*****************************************************************************
1003 * IDirectDraw7::GetAvailableVidMem
1005 * Returns the total and free video memory
1007 * Params:
1008 * Caps: Specifies the memory type asked for
1009 * total: Pointer to a DWORD to be filled with the total memory
1010 * free: Pointer to a DWORD to be filled with the free memory
1012 * Returns
1013 * DD_OK on success
1014 * DDERR_INVALIDPARAMS of free and total are NULL
1016 *****************************************************************************/
1017 static HRESULT WINAPI
1018 IDirectDrawImpl_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total, DWORD *free)
1020 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1021 TRACE("(%p)->(%p, %p, %p)\n", This, Caps, total, free);
1023 if(TRACE_ON(ddraw))
1025 TRACE("(%p) Asked for memory with description: ", This);
1026 DDRAW_dump_DDSCAPS2(Caps);
1028 EnterCriticalSection(&ddraw_cs);
1030 /* Todo: System memory vs local video memory vs non-local video memory
1031 * The MSDN also mentions differences between texture memory and other
1032 * resources, but that's not important
1035 if( (!total) && (!free) )
1037 LeaveCriticalSection(&ddraw_cs);
1038 return DDERR_INVALIDPARAMS;
1041 if(total) *total = This->total_vidmem;
1042 if(free) *free = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
1044 LeaveCriticalSection(&ddraw_cs);
1045 return DD_OK;
1048 /*****************************************************************************
1049 * IDirectDraw7::Initialize
1051 * Initializes a DirectDraw interface.
1053 * Params:
1054 * GUID: Interface identifier. Well, don't know what this is really good
1055 * for
1057 * Returns
1058 * Returns DD_OK on the first call,
1059 * DDERR_ALREADYINITIALIZED on repeated calls
1061 *****************************************************************************/
1062 static HRESULT WINAPI
1063 IDirectDrawImpl_Initialize(IDirectDraw7 *iface,
1064 GUID *Guid)
1066 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1067 TRACE("(%p)->(%s): No-op\n", This, debugstr_guid(Guid));
1069 if(This->initialized)
1071 return DDERR_ALREADYINITIALIZED;
1073 else
1075 return DD_OK;
1079 /*****************************************************************************
1080 * IDirectDraw7::FlipToGDISurface
1082 * "Makes the surface that the GDI writes to the primary surface"
1083 * Looks like some windows specific thing we don't have to care about.
1084 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1085 * show error boxes ;)
1086 * Well, just return DD_OK.
1088 * Returns:
1089 * Always returns DD_OK
1091 *****************************************************************************/
1092 static HRESULT WINAPI
1093 IDirectDrawImpl_FlipToGDISurface(IDirectDraw7 *iface)
1095 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1096 TRACE("(%p)\n", This);
1098 return DD_OK;
1101 /*****************************************************************************
1102 * IDirectDraw7::WaitForVerticalBlank
1104 * This method allows applications to get in sync with the vertical blank
1105 * interval.
1106 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1107 * redraw the screen, most likely because of this stub
1109 * Parameters:
1110 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1111 * or DDWAITVB_BLOCKEND
1112 * h: Not used, according to MSDN
1114 * Returns:
1115 * Always returns DD_OK
1117 *****************************************************************************/
1118 static HRESULT WINAPI
1119 IDirectDrawImpl_WaitForVerticalBlank(IDirectDraw7 *iface,
1120 DWORD Flags,
1121 HANDLE h)
1123 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1124 static BOOL hide = FALSE;
1126 /* This function is called often, so print the fixme only once */
1127 if(!hide)
1129 FIXME("(%p)->(%x,%p): Stub\n", This, Flags, h);
1130 hide = TRUE;
1133 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1134 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1135 return DDERR_UNSUPPORTED; /* unchecked */
1137 return DD_OK;
1140 /*****************************************************************************
1141 * IDirectDraw7::GetScanLine
1143 * Returns the scan line that is being drawn on the monitor
1145 * Parameters:
1146 * Scanline: Address to write the scan line value to
1148 * Returns:
1149 * Always returns DD_OK
1151 *****************************************************************************/
1152 static HRESULT WINAPI IDirectDrawImpl_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1154 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1155 static BOOL hide = FALSE;
1156 WINED3DDISPLAYMODE Mode;
1158 /* This function is called often, so print the fixme only once */
1159 EnterCriticalSection(&ddraw_cs);
1160 if(!hide)
1162 FIXME("(%p)->(%p): Semi-Stub\n", This, Scanline);
1163 hide = TRUE;
1166 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1168 &Mode);
1170 /* Fake the line sweeping of the monitor */
1171 /* FIXME: We should synchronize with a source to keep the refresh rate */
1172 *Scanline = This->cur_scanline++;
1173 /* Assume 20 scan lines in the vertical blank */
1174 if (This->cur_scanline >= Mode.Height + 20)
1175 This->cur_scanline = 0;
1177 LeaveCriticalSection(&ddraw_cs);
1178 return DD_OK;
1181 /*****************************************************************************
1182 * IDirectDraw7::TestCooperativeLevel
1184 * Informs the application about the state of the video adapter, depending
1185 * on the cooperative level
1187 * Returns:
1188 * DD_OK if the device is in a sane state
1189 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1190 * if the state is not correct(See below)
1192 *****************************************************************************/
1193 static HRESULT WINAPI
1194 IDirectDrawImpl_TestCooperativeLevel(IDirectDraw7 *iface)
1196 TRACE("iface %p.\n", iface);
1198 return DD_OK;
1201 /*****************************************************************************
1202 * IDirectDraw7::GetGDISurface
1204 * Returns the surface that GDI is treating as the primary surface.
1205 * For Wine this is the front buffer
1207 * Params:
1208 * GDISurface: Address to write the surface pointer to
1210 * Returns:
1211 * DD_OK if the surface was found
1212 * DDERR_NOTFOUND if the GDI surface wasn't found
1214 *****************************************************************************/
1215 static HRESULT WINAPI
1216 IDirectDrawImpl_GetGDISurface(IDirectDraw7 *iface,
1217 IDirectDrawSurface7 **GDISurface)
1219 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1220 IWineD3DSurface *Surf;
1221 IDirectDrawSurface7 *ddsurf;
1222 HRESULT hr;
1223 DDSCAPS2 ddsCaps;
1224 TRACE("(%p)->(%p)\n", This, GDISurface);
1226 /* Get the back buffer from the wineD3DDevice and search its
1227 * attached surfaces for the front buffer
1229 EnterCriticalSection(&ddraw_cs);
1230 hr = IWineD3DDevice_GetBackBuffer(This->wineD3DDevice,
1231 0, /* SwapChain */
1232 0, /* first back buffer*/
1233 WINED3DBACKBUFFER_TYPE_MONO,
1234 &Surf);
1236 if( (hr != D3D_OK) ||
1237 (!Surf) )
1239 ERR("IWineD3DDevice::GetBackBuffer failed\n");
1240 LeaveCriticalSection(&ddraw_cs);
1241 return DDERR_NOTFOUND;
1244 /* GetBackBuffer AddRef()ed the surface, release it */
1245 IWineD3DSurface_Release(Surf);
1247 IWineD3DSurface_GetParent(Surf,
1248 (IUnknown **) &ddsurf);
1249 IDirectDrawSurface7_Release(ddsurf); /* For the GetParent */
1251 /* Find the front buffer */
1252 ddsCaps.dwCaps = DDSCAPS_FRONTBUFFER;
1253 hr = IDirectDrawSurface7_GetAttachedSurface(ddsurf,
1254 &ddsCaps,
1255 GDISurface);
1256 if(hr != DD_OK)
1258 ERR("IDirectDrawSurface7::GetAttachedSurface failed, hr = %x\n", hr);
1261 /* The AddRef is OK this time */
1262 LeaveCriticalSection(&ddraw_cs);
1263 return hr;
1266 /*****************************************************************************
1267 * IDirectDraw7::EnumDisplayModes
1269 * Enumerates the supported Display modes. The modes can be filtered with
1270 * the DDSD parameter.
1272 * Params:
1273 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES
1274 * DDSD: Surface description to filter the modes
1275 * Context: Pointer passed back to the callback function
1276 * cb: Application-provided callback function
1278 * Returns:
1279 * DD_OK on success
1280 * DDERR_INVALIDPARAMS if the callback wasn't set
1282 *****************************************************************************/
1283 static HRESULT WINAPI
1284 IDirectDrawImpl_EnumDisplayModes(IDirectDraw7 *iface,
1285 DWORD Flags,
1286 DDSURFACEDESC2 *DDSD,
1287 void *Context,
1288 LPDDENUMMODESCALLBACK2 cb)
1290 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1291 unsigned int modenum, fmt;
1292 WINED3DFORMAT pixelformat = WINED3DFMT_UNKNOWN;
1293 WINED3DDISPLAYMODE mode;
1294 DDSURFACEDESC2 callback_sd;
1295 WINED3DDISPLAYMODE *enum_modes = NULL;
1296 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
1298 WINED3DFORMAT checkFormatList[] =
1300 WINED3DFMT_B8G8R8X8_UNORM,
1301 WINED3DFMT_B5G6R5_UNORM,
1302 WINED3DFMT_P8_UINT,
1305 TRACE("(%p)->(%p,%p,%p): Relay\n", This, DDSD, Context, cb);
1307 EnterCriticalSection(&ddraw_cs);
1308 /* This looks sane */
1309 if(!cb)
1311 LeaveCriticalSection(&ddraw_cs);
1312 return DDERR_INVALIDPARAMS;
1315 if(DDSD)
1317 if ((DDSD->dwFlags & DDSD_PIXELFORMAT) && (DDSD->u4.ddpfPixelFormat.dwFlags & DDPF_RGB) )
1318 pixelformat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
1321 if(!(Flags & DDEDM_REFRESHRATES))
1323 enum_mode_array_size = 16;
1324 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1325 if (!enum_modes)
1327 ERR("Out of memory\n");
1328 LeaveCriticalSection(&ddraw_cs);
1329 return DDERR_OUTOFMEMORY;
1333 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
1335 if(pixelformat != WINED3DFMT_UNKNOWN && checkFormatList[fmt] != pixelformat)
1337 continue;
1340 modenum = 0;
1341 while(IWineD3D_EnumAdapterModes(This->wineD3D,
1342 WINED3DADAPTER_DEFAULT,
1343 checkFormatList[fmt],
1344 modenum++,
1345 &mode) == WINED3D_OK)
1347 if(DDSD)
1349 if(DDSD->dwFlags & DDSD_WIDTH && mode.Width != DDSD->dwWidth) continue;
1350 if(DDSD->dwFlags & DDSD_HEIGHT && mode.Height != DDSD->dwHeight) continue;
1353 if(!(Flags & DDEDM_REFRESHRATES))
1355 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
1356 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
1357 * to be reduced to a single unique result in such case.
1359 BOOL found = FALSE;
1360 unsigned i;
1362 for (i = 0; i < enum_mode_count; i++)
1364 if(enum_modes[i].Width == mode.Width && enum_modes[i].Height == mode.Height &&
1365 enum_modes[i].Format == mode.Format)
1367 found = TRUE;
1368 break;
1372 if(found) continue;
1375 memset(&callback_sd, 0, sizeof(callback_sd));
1376 callback_sd.dwSize = sizeof(callback_sd);
1377 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1379 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH;
1380 if(Flags & DDEDM_REFRESHRATES)
1382 callback_sd.dwFlags |= DDSD_REFRESHRATE;
1383 callback_sd.u2.dwRefreshRate = mode.RefreshRate;
1386 callback_sd.dwWidth = mode.Width;
1387 callback_sd.dwHeight = mode.Height;
1389 PixelFormat_WineD3DtoDD(&callback_sd.u4.ddpfPixelFormat, mode.Format);
1391 /* Calc pitch and DWORD align like MSDN says */
1392 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.Width;
1393 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
1395 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
1396 callback_sd.u2.dwRefreshRate);
1398 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
1400 TRACE("Application asked to terminate the enumeration\n");
1401 HeapFree(GetProcessHeap(), 0, enum_modes);
1402 LeaveCriticalSection(&ddraw_cs);
1403 return DD_OK;
1406 if(!(Flags & DDEDM_REFRESHRATES))
1408 if (enum_mode_count == enum_mode_array_size)
1410 WINED3DDISPLAYMODE *new_enum_modes;
1412 enum_mode_array_size *= 2;
1413 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1415 if (!new_enum_modes)
1417 ERR("Out of memory\n");
1418 HeapFree(GetProcessHeap(), 0, enum_modes);
1419 LeaveCriticalSection(&ddraw_cs);
1420 return DDERR_OUTOFMEMORY;
1423 enum_modes = new_enum_modes;
1426 enum_modes[enum_mode_count++] = mode;
1431 TRACE("End of enumeration\n");
1432 HeapFree(GetProcessHeap(), 0, enum_modes);
1433 LeaveCriticalSection(&ddraw_cs);
1434 return DD_OK;
1437 /*****************************************************************************
1438 * IDirectDraw7::EvaluateMode
1440 * Used with IDirectDraw7::StartModeTest to test video modes.
1441 * EvaluateMode is used to pass or fail a mode, and continue with the next
1442 * mode
1444 * Params:
1445 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
1446 * Timeout: Returns the amount of seconds left before the mode would have
1447 * been failed automatically
1449 * Returns:
1450 * This implementation always DD_OK, because it's a stub
1452 *****************************************************************************/
1453 static HRESULT WINAPI
1454 IDirectDrawImpl_EvaluateMode(IDirectDraw7 *iface,
1455 DWORD Flags,
1456 DWORD *Timeout)
1458 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1459 FIXME("(%p)->(%d,%p): Stub!\n", This, Flags, Timeout);
1461 /* When implementing this, implement it in WineD3D */
1463 return DD_OK;
1466 /*****************************************************************************
1467 * IDirectDraw7::GetDeviceIdentifier
1469 * Returns the device identifier, which gives information about the driver
1470 * Our device identifier is defined at the beginning of this file.
1472 * Params:
1473 * DDDI: Address for the returned structure
1474 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
1476 * Returns:
1477 * On success it returns DD_OK
1478 * DDERR_INVALIDPARAMS if DDDI is NULL
1480 *****************************************************************************/
1481 static HRESULT WINAPI
1482 IDirectDrawImpl_GetDeviceIdentifier(IDirectDraw7 *iface,
1483 DDDEVICEIDENTIFIER2 *DDDI,
1484 DWORD Flags)
1486 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1487 TRACE("(%p)->(%p,%08x)\n", This, DDDI, Flags);
1489 if(!DDDI)
1490 return DDERR_INVALIDPARAMS;
1492 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
1493 * host adapter, if there's a secondary 3D adapter. This doesn't apply
1494 * to any modern hardware, nor is it interesting for Wine, so ignore it.
1495 * Size of DDDEVICEIDENTIFIER2 may be aligned to 8 bytes and thus 4
1496 * bytes too long. So only copy the relevant part of the structure
1499 memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
1500 return DD_OK;
1503 /*****************************************************************************
1504 * IDirectDraw7::GetSurfaceFromDC
1506 * Returns the Surface for a GDI device context handle.
1507 * Is this related to IDirectDrawSurface::GetDC ???
1509 * Params:
1510 * hdc: hdc to return the surface for
1511 * Surface: Address to write the surface pointer to
1513 * Returns:
1514 * Always returns DD_OK because it's a stub
1516 *****************************************************************************/
1517 static HRESULT WINAPI
1518 IDirectDrawImpl_GetSurfaceFromDC(IDirectDraw7 *iface,
1519 HDC hdc,
1520 IDirectDrawSurface7 **Surface)
1522 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1523 IWineD3DSurface *wined3d_surface;
1524 HRESULT hr;
1526 TRACE("iface %p, dc %p, surface %p.\n", iface, hdc, Surface);
1528 if (!Surface) return E_INVALIDARG;
1530 hr = IWineD3DDevice_GetSurfaceFromDC(This->wineD3DDevice, hdc, &wined3d_surface);
1531 if (FAILED(hr))
1533 TRACE("No surface found for dc %p.\n", hdc);
1534 *Surface = NULL;
1535 return DDERR_NOTFOUND;
1538 IWineD3DSurface_GetParent(wined3d_surface, (IUnknown **)Surface);
1539 TRACE("Returning surface %p.\n", Surface);
1540 return DD_OK;
1543 /*****************************************************************************
1544 * IDirectDraw7::RestoreAllSurfaces
1546 * Calls the restore method of all surfaces
1548 * Params:
1550 * Returns:
1551 * Always returns DD_OK because it's a stub
1553 *****************************************************************************/
1554 static HRESULT WINAPI
1555 IDirectDrawImpl_RestoreAllSurfaces(IDirectDraw7 *iface)
1557 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1558 FIXME("(%p): Stub\n", This);
1560 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
1561 * get their parent and call their restore method. Do not implement
1562 * it in WineD3D, as restoring a surface means re-creating the
1563 * WineD3DDSurface
1565 return DD_OK;
1568 /*****************************************************************************
1569 * IDirectDraw7::StartModeTest
1571 * Tests the specified video modes to update the system registry with
1572 * refresh rate information. StartModeTest starts the mode test,
1573 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
1574 * isn't called within 15 seconds, the mode is failed automatically
1576 * As refresh rates are handled by the X server, I don't think this
1577 * Method is important
1579 * Params:
1580 * Modes: An array of mode specifications
1581 * NumModes: The number of modes in Modes
1582 * Flags: Some flags...
1584 * Returns:
1585 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
1586 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
1587 * otherwise DD_OK
1589 *****************************************************************************/
1590 static HRESULT WINAPI
1591 IDirectDrawImpl_StartModeTest(IDirectDraw7 *iface,
1592 SIZE *Modes,
1593 DWORD NumModes,
1594 DWORD Flags)
1596 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1597 WARN("(%p)->(%p, %d, %x): Semi-Stub, most likely harmless\n", This, Modes, NumModes, Flags);
1599 /* This looks sane */
1600 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
1602 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
1603 * As it is not, DDERR_TESTFINISHED is returned
1604 * (hopefully that's correct
1606 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
1607 * well, that value doesn't (yet) exist in the wine headers, so ignore it
1610 return DD_OK;
1613 /*****************************************************************************
1614 * IDirectDrawImpl_RecreateSurfacesCallback
1616 * Enumeration callback for IDirectDrawImpl_RecreateAllSurfaces.
1617 * It re-recreates the WineD3DSurface. It's pretty straightforward
1619 *****************************************************************************/
1620 HRESULT WINAPI
1621 IDirectDrawImpl_RecreateSurfacesCallback(IDirectDrawSurface7 *surf,
1622 DDSURFACEDESC2 *desc,
1623 void *Context)
1625 IDirectDrawSurfaceImpl *surfImpl = (IDirectDrawSurfaceImpl *)surf;
1626 IDirectDrawImpl *This = surfImpl->ddraw;
1627 IUnknown *Parent;
1628 IWineD3DSurface *wineD3DSurface;
1629 IWineD3DSwapChain *swapchain;
1630 HRESULT hr;
1631 IWineD3DClipper *clipper = NULL;
1633 WINED3DSURFACE_DESC Desc;
1634 WINED3DFORMAT Format;
1635 DWORD Usage;
1636 WINED3DPOOL Pool;
1638 WINED3DMULTISAMPLE_TYPE MultiSampleType;
1639 DWORD MultiSampleQuality;
1640 UINT Width;
1641 UINT Height;
1643 TRACE("(%p): Enumerated Surface %p\n", This, surfImpl);
1645 /* For the enumeration */
1646 IDirectDrawSurface7_Release(surf);
1648 if(surfImpl->ImplType == This->ImplType) return DDENUMRET_OK; /* Continue */
1650 /* Get the objects */
1651 swapchain = surfImpl->wineD3DSwapChain;
1652 surfImpl->wineD3DSwapChain = NULL;
1653 wineD3DSurface = surfImpl->WineD3DSurface;
1655 /* get the clipper */
1656 IWineD3DSurface_GetClipper(wineD3DSurface, &clipper);
1658 /* Get the surface properties */
1659 hr = IWineD3DSurface_GetDesc(wineD3DSurface, &Desc);
1660 if(hr != D3D_OK) return hr;
1662 Format = Desc.format;
1663 Usage = Desc.usage;
1664 Pool = Desc.pool;
1665 MultiSampleType = Desc.multisample_type;
1666 MultiSampleQuality = Desc.multisample_quality;
1667 Width = Desc.width;
1668 Height = Desc.height;
1670 IWineD3DSurface_GetParent(wineD3DSurface, &Parent);
1672 /* Create the new surface */
1673 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice, Width, Height, Format,
1674 TRUE /* Lockable */, FALSE /* Discard */, surfImpl->mipmap_level, &surfImpl->WineD3DSurface, Usage, Pool,
1675 MultiSampleType, MultiSampleQuality, This->ImplType, Parent, &ddraw_null_wined3d_parent_ops);
1676 IUnknown_Release(Parent);
1677 if (FAILED(hr))
1679 surfImpl->WineD3DSurface = wineD3DSurface;
1680 return hr;
1683 IWineD3DSurface_SetClipper(surfImpl->WineD3DSurface, clipper);
1685 /* TODO: Copy the surface content, except for render targets */
1687 /* If there's a swapchain, it owns the wined3d surfaces. So Destroy
1688 * the swapchain
1690 if(swapchain) {
1691 /* The backbuffers have the swapchain set as well, but the primary
1692 * owns it and destroys it
1694 if(surfImpl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
1695 IWineD3DDevice_UninitGDI(This->wineD3DDevice, D3D7CB_DestroySwapChain);
1697 surfImpl->isRenderTarget = FALSE;
1698 } else {
1699 if(IWineD3DSurface_Release(wineD3DSurface) == 0)
1700 TRACE("Surface released successful, next surface\n");
1701 else
1702 ERR("Something's still holding the old WineD3DSurface\n");
1705 surfImpl->ImplType = This->ImplType;
1707 if(clipper)
1709 IWineD3DClipper_Release(clipper);
1711 return DDENUMRET_OK;
1714 /*****************************************************************************
1715 * IDirectDrawImpl_RecreateAllSurfaces
1717 * A function, that converts all wineD3DSurfaces to the new implementation type
1718 * It enumerates all surfaces with IWineD3DDevice::EnumSurfaces, creates a
1719 * new WineD3DSurface, copies the content and releases the old surface
1721 *****************************************************************************/
1722 static HRESULT
1723 IDirectDrawImpl_RecreateAllSurfaces(IDirectDrawImpl *This)
1725 DDSURFACEDESC2 desc;
1726 TRACE("(%p): Switch to implementation %d\n", This, This->ImplType);
1728 if(This->ImplType != SURFACE_OPENGL && This->d3d_initialized)
1730 /* Should happen almost never */
1731 FIXME("(%p) Switching to non-opengl surfaces with d3d started. Is this a bug?\n", This);
1732 /* Shutdown d3d */
1733 IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroySwapChain);
1735 /* Contrary: D3D starting is handled by the caller, because it knows the render target */
1737 memset(&desc, 0, sizeof(desc));
1738 desc.dwSize = sizeof(desc);
1740 return IDirectDraw7_EnumSurfaces((IDirectDraw7 *)This, 0, &desc, This, IDirectDrawImpl_RecreateSurfacesCallback);
1743 ULONG WINAPI D3D7CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
1744 IUnknown* swapChainParent;
1745 TRACE("(%p) call back\n", pSwapChain);
1747 IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
1748 IUnknown_Release(swapChainParent);
1749 return IUnknown_Release(swapChainParent);
1752 /*****************************************************************************
1753 * IDirectDrawImpl_CreateNewSurface
1755 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
1756 * with the passed parameters.
1758 * Params:
1759 * DDSD: Description of the surface to create
1760 * Surf: Address to store the interface pointer at
1762 * Returns:
1763 * DD_OK on success
1765 *****************************************************************************/
1766 static HRESULT
1767 IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This,
1768 DDSURFACEDESC2 *pDDSD,
1769 IDirectDrawSurfaceImpl **ppSurf,
1770 UINT level)
1772 HRESULT hr;
1773 UINT Width, Height;
1774 WINED3DFORMAT Format = WINED3DFMT_UNKNOWN;
1775 DWORD Usage = 0;
1776 WINED3DSURFTYPE ImplType = This->ImplType;
1777 WINED3DSURFACE_DESC Desc;
1778 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
1780 if (TRACE_ON(ddraw))
1782 TRACE(" (%p) Requesting surface desc :\n", This);
1783 DDRAW_dump_surface_desc(pDDSD);
1786 /* Select the surface type, if it wasn't choosen yet */
1787 if(ImplType == SURFACE_UNKNOWN)
1789 /* Use GL Surfaces if a D3DDEVICE Surface is requested */
1790 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1792 TRACE("(%p) Choosing GL surfaces because a 3DDEVICE Surface was requested\n", This);
1793 ImplType = SURFACE_OPENGL;
1796 /* Otherwise use GDI surfaces for now */
1797 else
1799 TRACE("(%p) Choosing GDI surfaces for 2D rendering\n", This);
1800 ImplType = SURFACE_GDI;
1803 /* Policy if all surface implementations are available:
1804 * First, check if a default type was set with winecfg. If not,
1805 * try Xrender surfaces, and use them if they work. Next, check if
1806 * accelerated OpenGL is available, and use GL surfaces in this
1807 * case. If all else fails, use GDI surfaces. If a 3DDEVICE surface
1808 * was created, always use OpenGL surfaces.
1810 * (Note: Xrender surfaces are not implemented for now, the
1811 * unaccelerated implementation uses GDI to render in Software)
1814 /* Store the type. If it needs to be changed, all WineD3DSurfaces have to
1815 * be re-created. This could be done with IDirectDrawSurface7::Restore
1817 This->ImplType = ImplType;
1819 else
1821 if ((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1822 && (This->ImplType != SURFACE_OPENGL)
1823 && DefaultSurfaceType == SURFACE_UNKNOWN)
1825 /* We have to change to OpenGL,
1826 * and re-create all WineD3DSurfaces
1828 ImplType = SURFACE_OPENGL;
1829 This->ImplType = ImplType;
1830 TRACE("(%p) Re-creating all surfaces\n", This);
1831 IDirectDrawImpl_RecreateAllSurfaces(This);
1832 TRACE("(%p) Done recreating all surfaces\n", This);
1834 else if(This->ImplType != SURFACE_OPENGL && pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1836 WARN("The application requests a 3D capable surface, but a non-opengl surface was set in the registry\n");
1837 /* Do not fail surface creation, only fail 3D device creation */
1841 if (!(pDDSD->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY)) &&
1842 !((pDDSD->ddsCaps.dwCaps & DDSCAPS_TEXTURE) && (pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)) )
1844 /* Tests show surfaces without memory flags get these flags added right after creation. */
1845 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
1847 /* Get the correct wined3d usage */
1848 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE |
1849 DDSCAPS_3DDEVICE ) )
1851 Usage |= WINED3DUSAGE_RENDERTARGET;
1853 pDDSD->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
1855 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
1857 Usage |= WINED3DUSAGE_OVERLAY;
1859 if(This->depthstencil || (pDDSD->ddsCaps.dwCaps & DDSCAPS_ZBUFFER) )
1861 /* The depth stencil creation callback sets this flag.
1862 * Set the WineD3D usage to let it know that it's a depth
1863 * Stencil surface.
1865 Usage |= WINED3DUSAGE_DEPTHSTENCIL;
1867 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
1869 Pool = WINED3DPOOL_SYSTEMMEM;
1871 else if(pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
1873 Pool = WINED3DPOOL_MANAGED;
1874 /* Managed textures have the system memory flag set */
1875 pDDSD->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
1877 else if(pDDSD->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
1879 /* Videomemory adds localvidmem, this is mutually exclusive with systemmemory
1880 * and texturemanage
1882 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
1885 Format = PixelFormat_DD2WineD3D(&pDDSD->u4.ddpfPixelFormat);
1886 if(Format == WINED3DFMT_UNKNOWN)
1888 ERR("Unsupported / Unknown pixelformat\n");
1889 return DDERR_INVALIDPIXELFORMAT;
1892 /* Create the Surface object */
1893 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
1894 if(!*ppSurf)
1896 ERR("(%p) Error allocating memory for a surface\n", This);
1897 return DDERR_OUTOFVIDEOMEMORY;
1899 (*ppSurf)->lpVtbl = &IDirectDrawSurface7_Vtbl;
1900 (*ppSurf)->IDirectDrawSurface3_vtbl = &IDirectDrawSurface3_Vtbl;
1901 (*ppSurf)->IDirectDrawGammaControl_vtbl = &IDirectDrawGammaControl_Vtbl;
1902 (*ppSurf)->IDirect3DTexture2_vtbl = &IDirect3DTexture2_Vtbl;
1903 (*ppSurf)->IDirect3DTexture_vtbl = &IDirect3DTexture1_Vtbl;
1904 (*ppSurf)->ref = 1;
1905 (*ppSurf)->version = 7;
1906 TRACE("%p->version = %d\n", (*ppSurf), (*ppSurf)->version);
1907 (*ppSurf)->ddraw = This;
1908 (*ppSurf)->surface_desc.dwSize = sizeof(DDSURFACEDESC2);
1909 (*ppSurf)->surface_desc.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1910 DD_STRUCT_COPY_BYSIZE(&(*ppSurf)->surface_desc, pDDSD);
1912 /* Surface attachments */
1913 (*ppSurf)->next_attached = NULL;
1914 (*ppSurf)->first_attached = *ppSurf;
1916 /* Needed to re-create the surface on an implementation change */
1917 (*ppSurf)->ImplType = ImplType;
1919 /* For D3DDevice creation */
1920 (*ppSurf)->isRenderTarget = FALSE;
1922 /* A trace message for debugging */
1923 TRACE("(%p) Created IDirectDrawSurface implementation structure at %p\n", This, *ppSurf);
1925 /* Now create the WineD3D Surface */
1926 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice, pDDSD->dwWidth, pDDSD->dwHeight, Format,
1927 TRUE /* Lockable */, FALSE /* Discard */, level, &(*ppSurf)->WineD3DSurface,
1928 Usage, Pool, WINED3DMULTISAMPLE_NONE, 0 /* MultiSampleQuality */, ImplType,
1929 (IUnknown *)*ppSurf, &ddraw_null_wined3d_parent_ops);
1931 if(hr != D3D_OK)
1933 ERR("IWineD3DDevice::CreateSurface failed. hr = %08x\n", hr);
1934 return hr;
1937 /* Increase the surface counter, and attach the surface */
1938 InterlockedIncrement(&This->surfaces);
1939 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
1941 /* Here we could store all created surfaces in the DirectDrawImpl structure,
1942 * But this could also be delegated to WineDDraw, as it keeps track of all its
1943 * resources. Not implemented for now, as there are more important things ;)
1946 /* Get the pixel format of the WineD3DSurface and store it.
1947 * Don't use the Format choosen above, WineD3D might have
1948 * changed it
1950 (*ppSurf)->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
1951 hr = IWineD3DSurface_GetDesc((*ppSurf)->WineD3DSurface, &Desc);
1952 if(hr != D3D_OK)
1954 ERR("IWineD3DSurface::GetDesc failed\n");
1955 IDirectDrawSurface7_Release( (IDirectDrawSurface7 *) *ppSurf);
1956 return hr;
1959 Format = Desc.format;
1960 Width = Desc.width;
1961 Height = Desc.height;
1963 if(Format == WINED3DFMT_UNKNOWN)
1965 FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN\n");
1967 PixelFormat_WineD3DtoDD( &(*ppSurf)->surface_desc.u4.ddpfPixelFormat, Format);
1969 /* Anno 1602 stores the pitch right after surface creation, so make sure it's there.
1970 * I can't LockRect() the surface here because if OpenGL surfaces are in use, the
1971 * WineD3DDevice might not be usable for 3D yet, so an extra method was created.
1972 * TODO: Test other fourcc formats
1974 if(Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3 ||
1975 Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5)
1977 (*ppSurf)->surface_desc.dwFlags |= DDSD_LINEARSIZE;
1978 if(Format == WINED3DFMT_DXT1)
1980 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height) / 2;
1982 else
1984 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height);
1987 else
1989 (*ppSurf)->surface_desc.dwFlags |= DDSD_PITCH;
1990 (*ppSurf)->surface_desc.u1.lPitch = IWineD3DSurface_GetPitch((*ppSurf)->WineD3DSurface);
1993 /* Application passed a color key? Set it! */
1994 if(pDDSD->dwFlags & DDSD_CKDESTOVERLAY)
1996 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
1997 DDCKEY_DESTOVERLAY,
1998 (WINEDDCOLORKEY *) &pDDSD->u3.ddckCKDestOverlay);
2000 if(pDDSD->dwFlags & DDSD_CKDESTBLT)
2002 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2003 DDCKEY_DESTBLT,
2004 (WINEDDCOLORKEY *) &pDDSD->ddckCKDestBlt);
2006 if(pDDSD->dwFlags & DDSD_CKSRCOVERLAY)
2008 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2009 DDCKEY_SRCOVERLAY,
2010 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcOverlay);
2012 if(pDDSD->dwFlags & DDSD_CKSRCBLT)
2014 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2015 DDCKEY_SRCBLT,
2016 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcBlt);
2018 if ( pDDSD->dwFlags & DDSD_LPSURFACE)
2020 hr = IWineD3DSurface_SetMem((*ppSurf)->WineD3DSurface, pDDSD->lpSurface);
2021 if(hr != WINED3D_OK)
2023 /* No need for a trace here, wined3d does that for us */
2024 IDirectDrawSurface7_Release((IDirectDrawSurface7 *)*ppSurf);
2025 return hr;
2029 return DD_OK;
2031 /*****************************************************************************
2032 * CreateAdditionalSurfaces
2034 * Creates a new mipmap chain.
2036 * Params:
2037 * root: Root surface to attach the newly created chain to
2038 * count: number of surfaces to create
2039 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2040 * effects on the caller
2041 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2042 * creates an additional surface without the mipmapping flags
2044 *****************************************************************************/
2045 static HRESULT
2046 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2047 IDirectDrawSurfaceImpl *root,
2048 UINT count,
2049 DDSURFACEDESC2 DDSD,
2050 BOOL CubeFaceRoot)
2052 UINT i, j, level = 0;
2053 HRESULT hr;
2054 IDirectDrawSurfaceImpl *last = root;
2056 for(i = 0; i < count; i++)
2058 IDirectDrawSurfaceImpl *object2 = NULL;
2060 /* increase the mipmap level, but only if a mipmap is created
2061 * In this case, also halve the size
2063 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2065 level++;
2066 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2067 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2068 /* Set the mipmap sublevel flag according to msdn */
2069 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2071 else
2073 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2075 CubeFaceRoot = FALSE;
2077 hr = IDirectDrawImpl_CreateNewSurface(This,
2078 &DDSD,
2079 &object2,
2080 level);
2081 if(hr != DD_OK)
2083 return hr;
2086 /* Add the new surface to the complex attachment array */
2087 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2089 if(last->complex_array[j]) continue;
2090 last->complex_array[j] = object2;
2091 break;
2093 last = object2;
2095 /* Remove the (possible) back buffer cap from the new surface description,
2096 * because only one surface in the flipping chain is a back buffer, one
2097 * is a front buffer, the others are just primary surfaces.
2099 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2101 return DD_OK;
2104 /*****************************************************************************
2105 * IDirectDraw7::CreateSurface
2107 * Creates a new IDirectDrawSurface object and returns its interface.
2109 * The surface connections with wined3d are a bit tricky. Basically it works
2110 * like this:
2112 * |------------------------| |-----------------|
2113 * | DDraw surface | | WineD3DSurface |
2114 * | | | |
2115 * | WineD3DSurface |-------------->| |
2116 * | Child |<------------->| Parent |
2117 * |------------------------| |-----------------|
2119 * The DDraw surface is the parent of the wined3d surface, and it releases
2120 * the WineD3DSurface when the ddraw surface is destroyed.
2122 * However, for all surfaces which can be in a container in WineD3D,
2123 * we have to do this. These surfaces are usually complex surfaces,
2124 * so this concerns primary surfaces with a front and a back buffer,
2125 * and textures.
2127 * |------------------------| |-----------------|
2128 * | DDraw surface | | Container |
2129 * | | | |
2130 * | Child |<------------->| Parent |
2131 * | Texture |<------------->| |
2132 * | WineD3DSurface |<----| | Levels |<--|
2133 * | Complex connection | | | | |
2134 * |------------------------| | |-----------------| |
2135 * ^ | |
2136 * | | |
2137 * | | |
2138 * | |------------------| | |-----------------| |
2139 * | | IParent | |-------->| WineD3DSurface | |
2140 * | | | | | |
2141 * | | Child |<------------->| Parent | |
2142 * | | | | Container |<--|
2143 * | |------------------| |-----------------| |
2144 * | |
2145 * | |----------------------| |
2146 * | | DDraw surface 2 | |
2147 * | | | |
2148 * |<->| Complex root Child | |
2149 * | | Texture | |
2150 * | | WineD3DSurface |<----| |
2151 * | |----------------------| | |
2152 * | | |
2153 * | |---------------------| | |-----------------| |
2154 * | | IParent | |----->| WineD3DSurface | |
2155 * | | | | | |
2156 * | | Child |<---------->| Parent | |
2157 * | |---------------------| | Container |<--|
2158 * | |-----------------| |
2159 * | |
2160 * | ---More surfaces can follow--- |
2162 * The reason is that the IWineD3DSwapchain(render target container)
2163 * and the IWineD3DTexure(Texture container) release the parents
2164 * of their surface's children, but by releasing the complex root
2165 * the surfaces which are complexly attached to it are destroyed
2166 * too. See IDirectDrawSurface::Release for a more detailed
2167 * explanation.
2169 * Params:
2170 * DDSD: Description of the surface to create
2171 * Surf: Address to store the interface pointer at
2172 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2173 * aggregation, so it has to be NULL
2175 * Returns:
2176 * DD_OK on success
2177 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2178 * DDERR_* if an error occurs
2180 *****************************************************************************/
2181 static HRESULT WINAPI
2182 IDirectDrawImpl_CreateSurface(IDirectDraw7 *iface,
2183 DDSURFACEDESC2 *DDSD,
2184 IDirectDrawSurface7 **Surf,
2185 IUnknown *UnkOuter)
2187 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
2188 IDirectDrawSurfaceImpl *object = NULL;
2189 HRESULT hr;
2190 LONG extra_surfaces = 0;
2191 DDSURFACEDESC2 desc2;
2192 WINED3DDISPLAYMODE Mode;
2193 const DWORD sysvidmem = DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
2195 TRACE("(%p)->(%p,%p,%p)\n", This, DDSD, Surf, UnkOuter);
2197 /* Some checks before we start */
2198 if (TRACE_ON(ddraw))
2200 TRACE(" (%p) Requesting surface desc :\n", This);
2201 DDRAW_dump_surface_desc(DDSD);
2203 EnterCriticalSection(&ddraw_cs);
2205 if (UnkOuter != NULL)
2207 FIXME("(%p) : outer != NULL?\n", This);
2208 LeaveCriticalSection(&ddraw_cs);
2209 return CLASS_E_NOAGGREGATION; /* unchecked */
2212 if (Surf == NULL)
2214 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", This);
2215 LeaveCriticalSection(&ddraw_cs);
2216 return E_POINTER; /* unchecked */
2219 if (!(DDSD->dwFlags & DDSD_CAPS))
2221 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2222 DDSD->dwFlags |= DDSD_CAPS;
2225 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2227 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2228 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2231 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2233 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2234 WARN("(%p) Null surface pointer specified, ignore it!\n", This);
2235 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2238 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2239 !(This->cooperative_level & DDSCL_EXCLUSIVE))
2241 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n", This);
2242 *Surf = NULL;
2243 LeaveCriticalSection(&ddraw_cs);
2244 return DDERR_NOEXCLUSIVEMODE;
2247 if(DDSD->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER)) {
2248 WARN("Application tried to create an explicit front or back buffer\n");
2249 LeaveCriticalSection(&ddraw_cs);
2250 return DDERR_INVALIDCAPS;
2253 if((DDSD->ddsCaps.dwCaps & sysvidmem) == sysvidmem)
2255 /* This is a special switch in ddrawex.dll, but not allowed in ddraw.dll */
2256 WARN("Application tries to put the surface in both system and video memory\n");
2257 LeaveCriticalSection(&ddraw_cs);
2258 *Surf = NULL;
2259 return DDERR_INVALIDCAPS;
2262 /* Check cube maps but only if the size includes them */
2263 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2265 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2266 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2268 WARN("Cube map faces requested without cube map flag\n");
2269 LeaveCriticalSection(&ddraw_cs);
2270 return DDERR_INVALIDCAPS;
2272 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2273 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2275 WARN("Cube map without faces requested\n");
2276 LeaveCriticalSection(&ddraw_cs);
2277 return DDERR_INVALIDPARAMS;
2280 /* Quick tests confirm those can be created, but we don't do that yet */
2281 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2282 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2284 FIXME("Partial cube maps not supported yet\n");
2288 /* According to the msdn this flag is ignored by CreateSurface */
2289 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2290 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2292 /* Modify some flags */
2293 memset(&desc2, 0, sizeof(desc2));
2294 desc2.dwSize = sizeof(desc2); /* For the struct copy */
2295 DD_STRUCT_COPY_BYSIZE(&desc2, DDSD);
2296 desc2.dwSize = sizeof(desc2); /* To override a possibly smaller size */
2297 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2299 /* Get the video mode from WineD3D - we will need it */
2300 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
2301 0, /* Swapchain 0 */
2302 &Mode);
2303 if(FAILED(hr))
2305 ERR("Failed to read display mode from wined3d\n");
2306 switch(This->orig_bpp)
2308 case 8:
2309 Mode.Format = WINED3DFMT_P8_UINT;
2310 break;
2312 case 15:
2313 Mode.Format = WINED3DFMT_B5G5R5X1_UNORM;
2314 break;
2316 case 16:
2317 Mode.Format = WINED3DFMT_B5G6R5_UNORM;
2318 break;
2320 case 24:
2321 Mode.Format = WINED3DFMT_B8G8R8_UNORM;
2322 break;
2324 case 32:
2325 Mode.Format = WINED3DFMT_B8G8R8X8_UNORM;
2326 break;
2328 Mode.Width = This->orig_width;
2329 Mode.Height = This->orig_height;
2332 /* No pixelformat given? Use the current screen format */
2333 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2335 desc2.dwFlags |= DDSD_PIXELFORMAT;
2336 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2338 /* Wait: It could be a Z buffer */
2339 if(desc2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
2341 switch(desc2.u2.dwMipMapCount) /* Who had this glorious idea? */
2343 case 15:
2344 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_S1_UINT_D15_UNORM);
2345 break;
2346 case 16:
2347 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D16_UNORM);
2348 break;
2349 case 24:
2350 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_X8D24_UNORM);
2351 break;
2352 case 32:
2353 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D32_UNORM);
2354 break;
2355 default:
2356 ERR("Unknown Z buffer bit depth\n");
2359 else
2361 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
2365 /* No Width or no Height? Use the original screen size
2367 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2368 !(desc2.dwFlags & DDSD_HEIGHT) )
2370 /* Invalid for non-render targets */
2371 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2373 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2374 *Surf = NULL;
2375 LeaveCriticalSection(&ddraw_cs);
2376 return DDERR_INVALIDPARAMS;
2379 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2380 desc2.dwWidth = Mode.Width;
2381 desc2.dwHeight = Mode.Height;
2384 /* Mipmap count fixes */
2385 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2387 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2389 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2391 /* Mipmap count is given, should not be 0 */
2392 if( desc2.u2.dwMipMapCount == 0 )
2394 LeaveCriticalSection(&ddraw_cs);
2395 return DDERR_INVALIDPARAMS;
2398 else
2400 /* Undocumented feature: Create sublevels until
2401 * either the width or the height is 1
2403 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2404 desc2.dwWidth : desc2.dwHeight;
2405 desc2.u2.dwMipMapCount = 0;
2406 while( min )
2408 desc2.u2.dwMipMapCount += 1;
2409 min >>= 1;
2413 else
2415 /* Not-complex mipmap -> Mipmapcount = 1 */
2416 desc2.u2.dwMipMapCount = 1;
2418 extra_surfaces = desc2.u2.dwMipMapCount - 1;
2420 /* There's a mipmap count in the created surface in any case */
2421 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2423 /* If no mipmap is given, the texture has only one level */
2425 /* The first surface is a front buffer, the back buffer is created afterwards */
2426 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2428 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2431 /* The root surface in a cube map is positive x */
2432 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2434 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2435 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2438 /* Create the first surface */
2439 hr = IDirectDrawImpl_CreateNewSurface(This, &desc2, &object, 0);
2440 if( hr != DD_OK)
2442 ERR("IDirectDrawImpl_CreateNewSurface failed with %08x\n", hr);
2443 LeaveCriticalSection(&ddraw_cs);
2444 return hr;
2446 object->is_complex_root = TRUE;
2448 *Surf = (IDirectDrawSurface7 *)object;
2450 /* Create Additional surfaces if necessary
2451 * This applies to Primary surfaces which have a back buffer count
2452 * set, but not to mipmap textures. In case of Mipmap textures,
2453 * wineD3D takes care of the creation of additional surfaces
2455 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2457 extra_surfaces = DDSD->dwBackBufferCount;
2458 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2459 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2460 desc2.dwBackBufferCount = 0;
2463 hr = DD_OK;
2464 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2466 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2467 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
2468 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2469 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
2470 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
2471 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2472 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
2473 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
2474 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2475 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
2476 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
2477 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2478 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
2479 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
2480 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2481 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
2482 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2485 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces, desc2, FALSE);
2486 if(hr != DD_OK)
2488 /* This destroys and possibly created surfaces too */
2489 IDirectDrawSurface_Release((IDirectDrawSurface7 *)object);
2490 LeaveCriticalSection(&ddraw_cs);
2491 return hr;
2494 /* If the implementation is OpenGL and there's no d3ddevice, attach a d3ddevice
2495 * But attach the d3ddevice only if the currently created surface was
2496 * a primary surface (2D app in 3D mode) or a 3DDEVICE surface (3D app)
2497 * The only case I can think of where this doesn't apply is when a
2498 * 2D app was configured by the user to run with OpenGL and it didn't create
2499 * the render target as first surface. In this case the render target creation
2500 * will cause the 3D init.
2502 if( (This->ImplType == SURFACE_OPENGL) && !(This->d3d_initialized) &&
2503 desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE) )
2505 IDirectDrawSurfaceImpl *target = object, *surface;
2506 struct list *entry;
2508 /* Search for the primary to use as render target */
2509 LIST_FOR_EACH(entry, &This->surface_list)
2511 surface = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2512 if((surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER)) ==
2513 (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER))
2515 /* found */
2516 target = surface;
2517 TRACE("Using primary %p as render target\n", target);
2518 break;
2522 TRACE("(%p) Attaching a D3DDevice, rendertarget = %p\n", This, target);
2523 hr = IDirectDrawImpl_AttachD3DDevice(This, target);
2524 if(hr != D3D_OK)
2526 IDirectDrawSurfaceImpl *release_surf;
2527 ERR("IDirectDrawImpl_AttachD3DDevice failed, hr = %x\n", hr);
2528 *Surf = NULL;
2530 /* The before created surface structures are in an incomplete state here.
2531 * WineD3D holds the reference on the IParents, and it released them on the failure
2532 * already. So the regular release method implementation would fail on the attempt
2533 * to destroy either the IParents or the swapchain. So free the surface here.
2534 * The surface structure here is a list, not a tree, because onscreen targets
2535 * cannot be cube textures
2537 while(object)
2539 release_surf = object;
2540 object = object->complex_array[0];
2541 IDirectDrawSurfaceImpl_Destroy(release_surf);
2543 LeaveCriticalSection(&ddraw_cs);
2544 return hr;
2546 } else if(!(This->d3d_initialized) && desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
2547 IDirectDrawImpl_CreateGDISwapChain(This, object);
2550 /* Addref the ddraw interface to keep an reference for each surface */
2551 IDirectDraw7_AddRef(iface);
2552 object->ifaceToRelease = (IUnknown *) iface;
2554 /* Create a WineD3DTexture if a texture was requested */
2555 if(desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
2557 UINT levels;
2558 WINED3DFORMAT Format;
2559 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
2561 This->tex_root = object;
2563 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2565 /* a mipmap is created, create enough levels */
2566 levels = desc2.u2.dwMipMapCount;
2568 else
2570 /* No mipmap is created, create one level */
2571 levels = 1;
2574 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM */
2575 if(DDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
2577 Pool = WINED3DPOOL_SYSTEMMEM;
2579 /* Should I forward the MANAGED cap to the managed pool ? */
2581 /* Get the format. It's set already by CreateNewSurface */
2582 Format = PixelFormat_DD2WineD3D(&object->surface_desc.u4.ddpfPixelFormat);
2584 /* The surfaces are already created, the callback only
2585 * passes the IWineD3DSurface to WineD3D
2587 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2589 hr = IWineD3DDevice_CreateCubeTexture(This->wineD3DDevice, DDSD->dwWidth /* Edgelength */,
2590 levels, 0 /* usage */, Format, Pool, (IWineD3DCubeTexture **)&object->wineD3DTexture,
2591 (IUnknown *)object, &ddraw_null_wined3d_parent_ops);
2593 else
2595 hr = IWineD3DDevice_CreateTexture(This->wineD3DDevice, DDSD->dwWidth, DDSD->dwHeight, levels,
2596 0 /* usage */, Format, Pool, (IWineD3DTexture **)&object->wineD3DTexture,
2597 (IUnknown *)object, &ddraw_null_wined3d_parent_ops);
2599 This->tex_root = NULL;
2602 LeaveCriticalSection(&ddraw_cs);
2603 return hr;
2606 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
2607 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
2609 static BOOL
2610 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
2611 const DDPIXELFORMAT *provided)
2613 /* Some flags must be present in both or neither for a match. */
2614 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
2615 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
2616 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
2618 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2619 return FALSE;
2621 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
2622 return FALSE;
2624 if (requested->dwFlags & DDPF_FOURCC)
2625 if (requested->dwFourCC != provided->dwFourCC)
2626 return FALSE;
2628 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
2629 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2630 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
2631 return FALSE;
2633 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2634 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2635 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
2636 return FALSE;
2638 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
2639 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
2640 return FALSE;
2642 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
2643 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2644 |DDPF_BUMPDUDV))
2645 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
2646 return FALSE;
2648 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
2649 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
2650 return FALSE;
2652 return TRUE;
2655 static BOOL
2656 IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested,
2657 const DDSURFACEDESC2* provided)
2659 struct compare_info
2661 DWORD flag;
2662 ptrdiff_t offset;
2663 size_t size;
2666 #define CMP(FLAG, FIELD) \
2667 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
2668 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
2670 static const struct compare_info compare[] =
2672 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
2673 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
2674 CMP(CAPS, ddsCaps),
2675 CMP(CKDESTBLT, ddckCKDestBlt),
2676 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
2677 CMP(CKSRCBLT, ddckCKSrcBlt),
2678 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
2679 CMP(HEIGHT, dwHeight),
2680 CMP(LINEARSIZE, u1 /* dwLinearSize */),
2681 CMP(LPSURFACE, lpSurface),
2682 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
2683 CMP(PITCH, u1 /* lPitch */),
2684 /* PIXELFORMAT: manual */
2685 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
2686 CMP(TEXTURESTAGE, dwTextureStage),
2687 CMP(WIDTH, dwWidth),
2688 /* ZBUFFERBITDEPTH: "obsolete" */
2691 #undef CMP
2693 unsigned int i;
2695 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2696 return FALSE;
2698 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
2700 if (requested->dwFlags & compare[i].flag
2701 && memcmp((const char *)provided + compare[i].offset,
2702 (const char *)requested + compare[i].offset,
2703 compare[i].size) != 0)
2704 return FALSE;
2707 if (requested->dwFlags & DDSD_PIXELFORMAT)
2709 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
2710 &provided->u4.ddpfPixelFormat))
2711 return FALSE;
2714 return TRUE;
2717 #undef DDENUMSURFACES_SEARCHTYPE
2718 #undef DDENUMSURFACES_MATCHTYPE
2720 /*****************************************************************************
2721 * IDirectDraw7::EnumSurfaces
2723 * Loops through all surfaces attached to this device and calls the
2724 * application callback. This can't be relayed to WineD3DDevice,
2725 * because some WineD3DSurfaces' parents are IParent objects
2727 * Params:
2728 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
2729 * DDSD: Description to filter for
2730 * Context: Application-provided pointer, it's passed unmodified to the
2731 * Callback function
2732 * Callback: Address to call for each surface
2734 * Returns:
2735 * DDERR_INVALIDPARAMS if the callback is NULL
2736 * DD_OK on success
2738 *****************************************************************************/
2739 static HRESULT WINAPI
2740 IDirectDrawImpl_EnumSurfaces(IDirectDraw7 *iface,
2741 DWORD Flags,
2742 DDSURFACEDESC2 *DDSD,
2743 void *Context,
2744 LPDDENUMSURFACESCALLBACK7 Callback)
2746 /* The surface enumeration is handled by WineDDraw,
2747 * because it keeps track of all surfaces attached to
2748 * it. The filtering is done by our callback function,
2749 * because WineDDraw doesn't handle ddraw-like surface
2750 * caps structures
2752 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
2753 IDirectDrawSurfaceImpl *surf;
2754 BOOL all, nomatch;
2755 DDSURFACEDESC2 desc;
2756 struct list *entry, *entry2;
2758 all = Flags & DDENUMSURFACES_ALL;
2759 nomatch = Flags & DDENUMSURFACES_NOMATCH;
2761 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, DDSD, Context, Callback);
2762 EnterCriticalSection(&ddraw_cs);
2764 if(!Callback)
2766 LeaveCriticalSection(&ddraw_cs);
2767 return DDERR_INVALIDPARAMS;
2770 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
2771 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
2773 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2774 if (all || (nomatch != IDirectDrawImpl_DDSD_Match(DDSD, &surf->surface_desc)))
2776 desc = surf->surface_desc;
2777 IDirectDrawSurface7_AddRef((IDirectDrawSurface7 *)surf);
2778 if (Callback((IDirectDrawSurface7 *)surf, &desc, Context) != DDENUMRET_OK)
2780 LeaveCriticalSection(&ddraw_cs);
2781 return DD_OK;
2785 LeaveCriticalSection(&ddraw_cs);
2786 return DD_OK;
2789 static HRESULT WINAPI
2790 findRenderTarget(IDirectDrawSurface7 *surface,
2791 DDSURFACEDESC2 *desc,
2792 void *ctx)
2794 IDirectDrawSurfaceImpl *surf = (IDirectDrawSurfaceImpl *)surface;
2795 IDirectDrawSurfaceImpl **target = ctx;
2797 if(!surf->isRenderTarget) {
2798 *target = surf;
2799 IDirectDrawSurface7_Release(surface);
2800 return DDENUMRET_CANCEL;
2803 /* Recurse into the surface tree */
2804 IDirectDrawSurface7_EnumAttachedSurfaces(surface, ctx, findRenderTarget);
2806 IDirectDrawSurface7_Release(surface);
2807 if(*target) return DDENUMRET_CANCEL;
2808 else return DDENUMRET_OK; /* Continue with the next neighbor surface */
2811 static HRESULT IDirectDrawImpl_CreateGDISwapChain(IDirectDrawImpl *This,
2812 IDirectDrawSurfaceImpl *primary) {
2813 HRESULT hr;
2814 WINED3DPRESENT_PARAMETERS presentation_parameters;
2815 HWND window;
2817 window = This->dest_window;
2819 memset(&presentation_parameters, 0, sizeof(presentation_parameters));
2821 /* Use the surface description for the device parameters, not the
2822 * Device settings. The app might render to an offscreen surface
2824 presentation_parameters.BackBufferWidth = primary->surface_desc.dwWidth;
2825 presentation_parameters.BackBufferHeight = primary->surface_desc.dwHeight;
2826 presentation_parameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2827 presentation_parameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
2828 presentation_parameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
2829 presentation_parameters.MultiSampleQuality = 0;
2830 presentation_parameters.SwapEffect = WINED3DSWAPEFFECT_FLIP;
2831 presentation_parameters.hDeviceWindow = window;
2832 presentation_parameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
2833 presentation_parameters.EnableAutoDepthStencil = FALSE; /* Not on GDI swapchains */
2834 presentation_parameters.AutoDepthStencilFormat = 0;
2835 presentation_parameters.Flags = 0;
2836 presentation_parameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
2837 presentation_parameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
2839 This->d3d_target = primary;
2840 hr = IWineD3DDevice_InitGDI(This->wineD3DDevice, &presentation_parameters);
2841 This->d3d_target = NULL;
2843 if (hr != D3D_OK)
2845 FIXME("(%p) call to IWineD3DDevice_InitGDI failed\n", This);
2846 primary->wineD3DSwapChain = NULL;
2848 return hr;
2851 /*****************************************************************************
2852 * IDirectDrawImpl_AttachD3DDevice
2854 * Initializes the D3D capabilities of WineD3D
2856 * Params:
2857 * primary: The primary surface for D3D
2859 * Returns
2860 * DD_OK on success,
2861 * DDERR_* otherwise
2863 *****************************************************************************/
2864 static HRESULT
2865 IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This,
2866 IDirectDrawSurfaceImpl *primary)
2868 HRESULT hr;
2869 HWND window = This->dest_window;
2871 WINED3DPRESENT_PARAMETERS localParameters;
2873 TRACE("(%p)->(%p)\n", This, primary);
2875 /* If there's no window, create a hidden window. WineD3D needs it */
2876 if(window == 0 || window == GetDesktopWindow())
2878 window = CreateWindowExA(0, This->classname, "Hidden D3D Window",
2879 WS_DISABLED, 0, 0,
2880 GetSystemMetrics(SM_CXSCREEN),
2881 GetSystemMetrics(SM_CYSCREEN),
2882 NULL, NULL, GetModuleHandleA(0), NULL);
2884 ShowWindow(window, SW_HIDE); /* Just to be sure */
2885 WARN("(%p) No window for the Direct3DDevice, created a hidden window. HWND=%p\n", This, window);
2887 else
2889 TRACE("(%p) Using existing window %p for Direct3D rendering\n", This, window);
2891 This->d3d_window = window;
2893 /* Store the future Render Target surface */
2894 This->d3d_target = primary;
2896 /* Use the surface description for the device parameters, not the
2897 * Device settings. The app might render to an offscreen surface
2899 localParameters.BackBufferWidth = primary->surface_desc.dwWidth;
2900 localParameters.BackBufferHeight = primary->surface_desc.dwHeight;
2901 localParameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2902 localParameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
2903 localParameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
2904 localParameters.MultiSampleQuality = 0;
2905 localParameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
2906 localParameters.hDeviceWindow = window;
2907 localParameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
2908 localParameters.EnableAutoDepthStencil = TRUE;
2909 localParameters.AutoDepthStencilFormat = WINED3DFMT_D16_UNORM;
2910 localParameters.Flags = 0;
2911 localParameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
2912 localParameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
2914 TRACE("Passing mode %d\n", localParameters.BackBufferFormat);
2916 /* Set this NOW, otherwise creating the depth stencil surface will cause a
2917 * recursive loop until ram or emulated video memory is full
2919 This->d3d_initialized = TRUE;
2921 hr = IWineD3DDevice_Init3D(This->wineD3DDevice, &localParameters);
2922 if(FAILED(hr))
2924 This->d3d_target = NULL;
2925 This->d3d_initialized = FALSE;
2926 return hr;
2929 This->declArraySize = 2;
2930 This->decls = HeapAlloc(GetProcessHeap(),
2931 HEAP_ZERO_MEMORY,
2932 sizeof(*This->decls) * This->declArraySize);
2933 if(!This->decls)
2935 ERR("Error allocating an array for the converted vertex decls\n");
2936 This->declArraySize = 0;
2937 hr = IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroySwapChain);
2938 return E_OUTOFMEMORY;
2941 /* Create an Index Buffer parent */
2942 TRACE("(%p) Successfully initialized 3D\n", This);
2943 return DD_OK;
2946 /*****************************************************************************
2947 * DirectDrawCreateClipper (DDRAW.@)
2949 * Creates a new IDirectDrawClipper object.
2951 * Params:
2952 * Clipper: Address to write the interface pointer to
2953 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
2954 * NULL
2956 * Returns:
2957 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2958 * E_OUTOFMEMORY if allocating the object failed
2960 *****************************************************************************/
2961 HRESULT WINAPI
2962 DirectDrawCreateClipper(DWORD Flags,
2963 LPDIRECTDRAWCLIPPER *Clipper,
2964 IUnknown *UnkOuter)
2966 IDirectDrawClipperImpl* object;
2967 TRACE("(%08x,%p,%p)\n", Flags, Clipper, UnkOuter);
2969 EnterCriticalSection(&ddraw_cs);
2970 if (UnkOuter != NULL)
2972 LeaveCriticalSection(&ddraw_cs);
2973 return CLASS_E_NOAGGREGATION;
2976 if (!LoadWineD3D())
2978 LeaveCriticalSection(&ddraw_cs);
2979 return DDERR_NODIRECTDRAWSUPPORT;
2982 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2983 sizeof(IDirectDrawClipperImpl));
2984 if (object == NULL)
2986 LeaveCriticalSection(&ddraw_cs);
2987 return E_OUTOFMEMORY;
2990 object->lpVtbl = &IDirectDrawClipper_Vtbl;
2991 object->ref = 1;
2992 object->wineD3DClipper = pWineDirect3DCreateClipper((IUnknown *) object);
2993 if(!object->wineD3DClipper)
2995 HeapFree(GetProcessHeap(), 0, object);
2996 LeaveCriticalSection(&ddraw_cs);
2997 return E_OUTOFMEMORY;
3000 *Clipper = (IDirectDrawClipper *) object;
3001 LeaveCriticalSection(&ddraw_cs);
3002 return DD_OK;
3005 /*****************************************************************************
3006 * IDirectDraw7::CreateClipper
3008 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3010 *****************************************************************************/
3011 static HRESULT WINAPI
3012 IDirectDrawImpl_CreateClipper(IDirectDraw7 *iface,
3013 DWORD Flags,
3014 IDirectDrawClipper **Clipper,
3015 IUnknown *UnkOuter)
3017 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
3018 TRACE("(%p)->(%x,%p,%p)\n", This, Flags, Clipper, UnkOuter);
3019 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3022 /*****************************************************************************
3023 * IDirectDraw7::CreatePalette
3025 * Creates a new IDirectDrawPalette object
3027 * Params:
3028 * Flags: The flags for the new clipper
3029 * ColorTable: Color table to assign to the new clipper
3030 * Palette: Address to write the interface pointer to
3031 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3032 * NULL
3034 * Returns:
3035 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3036 * E_OUTOFMEMORY if allocating the object failed
3038 *****************************************************************************/
3039 static HRESULT WINAPI
3040 IDirectDrawImpl_CreatePalette(IDirectDraw7 *iface,
3041 DWORD Flags,
3042 PALETTEENTRY *ColorTable,
3043 IDirectDrawPalette **Palette,
3044 IUnknown *pUnkOuter)
3046 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
3047 IDirectDrawPaletteImpl *object;
3048 HRESULT hr = DDERR_GENERIC;
3049 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, ColorTable, Palette, pUnkOuter);
3051 EnterCriticalSection(&ddraw_cs);
3052 if(pUnkOuter != NULL)
3054 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
3055 LeaveCriticalSection(&ddraw_cs);
3056 return CLASS_E_NOAGGREGATION;
3059 /* The refcount test shows that a cooplevel is required for this */
3060 if(!This->cooperative_level)
3062 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3063 LeaveCriticalSection(&ddraw_cs);
3064 return DDERR_NOCOOPERATIVELEVELSET;
3067 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
3068 if(!object)
3070 ERR("Out of memory when allocating memory for a palette implementation\n");
3071 LeaveCriticalSection(&ddraw_cs);
3072 return E_OUTOFMEMORY;
3075 object->lpVtbl = &IDirectDrawPalette_Vtbl;
3076 object->ref = 1;
3077 object->ddraw_owner = This;
3079 hr = IWineD3DDevice_CreatePalette(This->wineD3DDevice, Flags,
3080 ColorTable, &object->wineD3DPalette, (IUnknown *)object);
3081 if(hr != DD_OK)
3083 HeapFree(GetProcessHeap(), 0, object);
3084 LeaveCriticalSection(&ddraw_cs);
3085 return hr;
3088 IDirectDraw7_AddRef(iface);
3089 object->ifaceToRelease = (IUnknown *) iface;
3090 *Palette = (IDirectDrawPalette *)object;
3091 LeaveCriticalSection(&ddraw_cs);
3092 return DD_OK;
3095 /*****************************************************************************
3096 * IDirectDraw7::DuplicateSurface
3098 * Duplicates a surface. The surface memory points to the same memory as
3099 * the original surface, and it's released when the last surface referencing
3100 * it is released. I guess that's beyond Wine's surface management right now
3101 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3102 * test application to implement this)
3104 * Params:
3105 * Src: Address of the source surface
3106 * Dest: Address to write the new surface pointer to
3108 * Returns:
3109 * See IDirectDraw7::CreateSurface
3111 *****************************************************************************/
3112 static HRESULT WINAPI
3113 IDirectDrawImpl_DuplicateSurface(IDirectDraw7 *iface,
3114 IDirectDrawSurface7 *Src,
3115 IDirectDrawSurface7 **Dest)
3117 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
3118 IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Src;
3120 FIXME("(%p)->(%p,%p)\n", This, Surf, Dest);
3122 /* For now, simply create a new, independent surface */
3123 return IDirectDraw7_CreateSurface(iface,
3124 &Surf->surface_desc,
3125 Dest,
3126 NULL);
3129 /*****************************************************************************
3130 * IDirectDraw7 VTable
3131 *****************************************************************************/
3132 const IDirectDraw7Vtbl IDirectDraw7_Vtbl =
3134 /*** IUnknown ***/
3135 IDirectDrawImpl_QueryInterface,
3136 IDirectDrawImpl_AddRef,
3137 IDirectDrawImpl_Release,
3138 /*** IDirectDraw ***/
3139 IDirectDrawImpl_Compact,
3140 IDirectDrawImpl_CreateClipper,
3141 IDirectDrawImpl_CreatePalette,
3142 IDirectDrawImpl_CreateSurface,
3143 IDirectDrawImpl_DuplicateSurface,
3144 IDirectDrawImpl_EnumDisplayModes,
3145 IDirectDrawImpl_EnumSurfaces,
3146 IDirectDrawImpl_FlipToGDISurface,
3147 IDirectDrawImpl_GetCaps,
3148 IDirectDrawImpl_GetDisplayMode,
3149 IDirectDrawImpl_GetFourCCCodes,
3150 IDirectDrawImpl_GetGDISurface,
3151 IDirectDrawImpl_GetMonitorFrequency,
3152 IDirectDrawImpl_GetScanLine,
3153 IDirectDrawImpl_GetVerticalBlankStatus,
3154 IDirectDrawImpl_Initialize,
3155 IDirectDrawImpl_RestoreDisplayMode,
3156 IDirectDrawImpl_SetCooperativeLevel,
3157 IDirectDrawImpl_SetDisplayMode,
3158 IDirectDrawImpl_WaitForVerticalBlank,
3159 /*** IDirectDraw2 ***/
3160 IDirectDrawImpl_GetAvailableVidMem,
3161 /*** IDirectDraw3 ***/
3162 IDirectDrawImpl_GetSurfaceFromDC,
3163 /*** IDirectDraw4 ***/
3164 IDirectDrawImpl_RestoreAllSurfaces,
3165 IDirectDrawImpl_TestCooperativeLevel,
3166 IDirectDrawImpl_GetDeviceIdentifier,
3167 /*** IDirectDraw7 ***/
3168 IDirectDrawImpl_StartModeTest,
3169 IDirectDrawImpl_EvaluateMode
3172 /*****************************************************************************
3173 * IDirectDrawImpl_FindDecl
3175 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
3176 * if none was found.
3178 * This function is in ddraw.c and the DDraw object space because D3D7
3179 * vertex buffers are created using the IDirect3D interface to the ddraw
3180 * object, so they can be valid across D3D devices(theoretically. The ddraw
3181 * object also owns the wined3d device
3183 * Parameters:
3184 * This: Device
3185 * fvf: Fvf to find the decl for
3187 * Returns:
3188 * NULL in case of an error, the IWineD3DVertexDeclaration interface for the
3189 * fvf otherwise.
3191 *****************************************************************************/
3192 IWineD3DVertexDeclaration *
3193 IDirectDrawImpl_FindDecl(IDirectDrawImpl *This,
3194 DWORD fvf)
3196 HRESULT hr;
3197 IWineD3DVertexDeclaration* pDecl = NULL;
3198 int p, low, high; /* deliberately signed */
3199 struct FvfToDecl *convertedDecls = This->decls;
3201 TRACE("Searching for declaration for fvf %08x... ", fvf);
3203 low = 0;
3204 high = This->numConvertedDecls - 1;
3205 while(low <= high) {
3206 p = (low + high) >> 1;
3207 TRACE("%d ", p);
3208 if(convertedDecls[p].fvf == fvf) {
3209 TRACE("found %p\n", convertedDecls[p].decl);
3210 return convertedDecls[p].decl;
3211 } else if(convertedDecls[p].fvf < fvf) {
3212 low = p + 1;
3213 } else {
3214 high = p - 1;
3217 TRACE("not found. Creating and inserting at position %d.\n", low);
3219 hr = IWineD3DDevice_CreateVertexDeclarationFromFVF(This->wineD3DDevice, &pDecl,
3220 (IUnknown *)This, &ddraw_null_wined3d_parent_ops, fvf);
3221 if (hr != S_OK) return NULL;
3223 if(This->declArraySize == This->numConvertedDecls) {
3224 int grow = max(This->declArraySize / 2, 8);
3225 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
3226 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
3227 if(!convertedDecls) {
3228 /* This will destroy it */
3229 IWineD3DVertexDeclaration_Release(pDecl);
3230 return NULL;
3232 This->decls = convertedDecls;
3233 This->declArraySize += grow;
3236 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
3237 convertedDecls[low].decl = pDecl;
3238 convertedDecls[low].fvf = fvf;
3239 This->numConvertedDecls++;
3241 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
3242 return pDecl;
3245 /* IWineD3DDeviceParent IUnknown methods */
3247 static inline struct IDirectDrawImpl *ddraw_from_device_parent(IWineD3DDeviceParent *iface)
3249 return (struct IDirectDrawImpl *)((char*)iface - FIELD_OFFSET(struct IDirectDrawImpl, device_parent_vtbl));
3252 static HRESULT STDMETHODCALLTYPE device_parent_QueryInterface(IWineD3DDeviceParent *iface, REFIID riid, void **object)
3254 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3255 return IDirectDrawImpl_QueryInterface((IDirectDraw7 *)This, riid, object);
3258 static ULONG STDMETHODCALLTYPE device_parent_AddRef(IWineD3DDeviceParent *iface)
3260 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3261 return IDirectDrawImpl_AddRef((IDirectDraw7 *)This);
3264 static ULONG STDMETHODCALLTYPE device_parent_Release(IWineD3DDeviceParent *iface)
3266 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3267 return IDirectDrawImpl_Release((IDirectDraw7 *)This);
3270 /* IWineD3DDeviceParent methods */
3272 static void STDMETHODCALLTYPE device_parent_WineD3DDeviceCreated(IWineD3DDeviceParent *iface, IWineD3DDevice *device)
3274 TRACE("iface %p, device %p\n", iface, device);
3277 static HRESULT STDMETHODCALLTYPE device_parent_CreateSurface(IWineD3DDeviceParent *iface,
3278 IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, DWORD usage,
3279 WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, IWineD3DSurface **surface)
3281 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3282 IDirectDrawSurfaceImpl *surf = NULL;
3283 UINT i = 0;
3284 DDSCAPS2 searchcaps = This->tex_root->surface_desc.ddsCaps;
3286 TRACE("iface %p, superior %p, width %u, height %u, format %#x, usage %#x,\n"
3287 "\tpool %#x, level %u, face %u, surface %p\n",
3288 iface, superior, width, height, format, usage, pool, level, face, surface);
3290 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
3291 switch(face)
3293 case WINED3DCUBEMAP_FACE_POSITIVE_X:
3294 TRACE("Asked for positive x\n");
3295 if (searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP)
3297 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
3299 surf = This->tex_root; break;
3300 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
3301 TRACE("Asked for negative x\n");
3302 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
3303 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
3304 TRACE("Asked for positive y\n");
3305 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
3306 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
3307 TRACE("Asked for negative y\n");
3308 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
3309 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
3310 TRACE("Asked for positive z\n");
3311 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
3312 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
3313 TRACE("Asked for negative z\n");
3314 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
3315 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
3318 if (!surf)
3320 IDirectDrawSurface7 *attached;
3321 IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)This->tex_root, &searchcaps, &attached);
3322 surf = (IDirectDrawSurfaceImpl *)attached;
3323 IDirectDrawSurface7_Release(attached);
3325 if (!surf) ERR("root search surface not found\n");
3327 /* Find the wanted mipmap. There are enough mipmaps in the chain */
3328 while (i < level)
3330 IDirectDrawSurface7 *attached;
3331 IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)surf, &searchcaps, &attached);
3332 if(!attached) ERR("Surface not found\n");
3333 surf = (IDirectDrawSurfaceImpl *)attached;
3334 IDirectDrawSurface7_Release(attached);
3335 ++i;
3338 /* Return the surface */
3339 *surface = surf->WineD3DSurface;
3340 IWineD3DSurface_AddRef(*surface);
3342 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, surf);
3344 return D3D_OK;
3347 static HRESULT STDMETHODCALLTYPE device_parent_CreateRenderTarget(IWineD3DDeviceParent *iface,
3348 IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
3349 DWORD multisample_quality, BOOL lockable, IWineD3DSurface **surface)
3351 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3352 IDirectDrawSurfaceImpl *d3d_surface = This->d3d_target;
3353 IDirectDrawSurfaceImpl *target = NULL;
3355 TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3356 "\tmultisample_quality %u, lockable %u, surface %p\n",
3357 iface, superior, width, height, format, multisample_type, multisample_quality, lockable, surface);
3359 if (d3d_surface->isRenderTarget)
3361 IDirectDrawSurface7_EnumAttachedSurfaces((IDirectDrawSurface7 *)d3d_surface, &target, findRenderTarget);
3363 else
3365 target = d3d_surface;
3368 if (!target)
3370 target = This->d3d_target;
3371 ERR(" (%p) : No DirectDrawSurface found to create the back buffer. Using the front buffer as back buffer. Uncertain consequences\n", This);
3374 /* TODO: Return failure if the dimensions do not match, but this shouldn't happen */
3376 *surface = target->WineD3DSurface;
3377 IWineD3DSurface_AddRef(*surface);
3378 target->isRenderTarget = TRUE;
3380 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, d3d_surface);
3382 return D3D_OK;
3385 static HRESULT STDMETHODCALLTYPE device_parent_CreateDepthStencilSurface(IWineD3DDeviceParent *iface,
3386 IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
3387 DWORD multisample_quality, BOOL discard, IWineD3DSurface **surface)
3389 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3390 IDirectDrawSurfaceImpl *ddraw_surface;
3391 DDSURFACEDESC2 ddsd;
3392 HRESULT hr;
3394 TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3395 "\tmultisample_quality %u, discard %u, surface %p\n",
3396 iface, superior, width, height, format, multisample_type, multisample_quality, discard, surface);
3398 *surface = NULL;
3400 /* Create a DirectDraw surface */
3401 memset(&ddsd, 0, sizeof(ddsd));
3402 ddsd.dwSize = sizeof(ddsd);
3403 ddsd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
3404 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
3405 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
3406 ddsd.dwHeight = height;
3407 ddsd.dwWidth = width;
3408 if (format)
3410 PixelFormat_WineD3DtoDD(&ddsd.u4.ddpfPixelFormat, format);
3412 else
3414 ddsd.dwFlags ^= DDSD_PIXELFORMAT;
3417 This->depthstencil = TRUE;
3418 hr = IDirectDraw7_CreateSurface((IDirectDraw7 *)This, &ddsd, (IDirectDrawSurface7 **)&ddraw_surface, NULL);
3419 This->depthstencil = FALSE;
3420 if(FAILED(hr))
3422 ERR(" (%p) Creating a DepthStencil Surface failed, result = %x\n", This, hr);
3423 return hr;
3426 *surface = ddraw_surface->WineD3DSurface;
3427 IWineD3DSurface_AddRef(*surface);
3428 IDirectDrawSurface7_Release((IDirectDrawSurface7 *)ddraw_surface);
3430 return D3D_OK;
3433 static HRESULT STDMETHODCALLTYPE device_parent_CreateVolume(IWineD3DDeviceParent *iface,
3434 IUnknown *superior, UINT width, UINT height, UINT depth, WINED3DFORMAT format,
3435 WINED3DPOOL pool, DWORD usage, IWineD3DVolume **volume)
3437 TRACE("iface %p, superior %p, width %u, height %u, depth %u, format %#x, pool %#x, usage %#x, volume %p\n",
3438 iface, superior, width, height, depth, format, pool, usage, volume);
3440 ERR("Not implemented!\n");
3442 return E_NOTIMPL;
3445 static HRESULT STDMETHODCALLTYPE device_parent_CreateSwapChain(IWineD3DDeviceParent *iface,
3446 WINED3DPRESENT_PARAMETERS *present_parameters, IWineD3DSwapChain **swapchain)
3448 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3449 IDirectDrawSurfaceImpl *iterator;
3450 IParentImpl *object;
3451 HRESULT hr;
3453 TRACE("iface %p, present_parameters %p, swapchain %p\n", iface, present_parameters, swapchain);
3455 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IParentImpl));
3456 if (!object)
3458 FIXME("Allocation of memory failed\n");
3459 *swapchain = NULL;
3460 return DDERR_OUTOFVIDEOMEMORY;
3463 object->lpVtbl = &IParent_Vtbl;
3464 object->ref = 1;
3466 hr = IWineD3DDevice_CreateSwapChain(This->wineD3DDevice, present_parameters,
3467 swapchain, (IUnknown *)object, This->ImplType);
3468 if (FAILED(hr))
3470 FIXME("(%p) CreateSwapChain failed, returning %#x\n", iface, hr);
3471 HeapFree(GetProcessHeap(), 0 , object);
3472 *swapchain = NULL;
3473 return hr;
3476 object->child = (IUnknown *)*swapchain;
3477 This->d3d_target->wineD3DSwapChain = *swapchain;
3478 iterator = This->d3d_target->complex_array[0];
3479 while (iterator)
3481 iterator->wineD3DSwapChain = *swapchain;
3482 iterator = iterator->complex_array[0];
3485 return hr;
3488 const IWineD3DDeviceParentVtbl ddraw_wined3d_device_parent_vtbl =
3490 /* IUnknown methods */
3491 device_parent_QueryInterface,
3492 device_parent_AddRef,
3493 device_parent_Release,
3494 /* IWineD3DDeviceParent methods */
3495 device_parent_WineD3DDeviceCreated,
3496 device_parent_CreateSurface,
3497 device_parent_CreateRenderTarget,
3498 device_parent_CreateDepthStencilSurface,
3499 device_parent_CreateVolume,
3500 device_parent_CreateSwapChain,