wined3d: read_from_framebuffer_texture() isn't suitable for readback of onscreen...
[wine/testsucceed.git] / dlls / ddraw / ddraw.c
blob7d6558dcc6f76ffa72c88c13a4856f3995559913
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 EnterCriticalSection(&ddraw_cs);
279 list_remove(&This->ddraw_list_entry);
280 LeaveCriticalSection(&ddraw_cs);
282 /* Release the attached WineD3D stuff */
283 IWineD3DDevice_Release(This->wineD3DDevice);
284 IWineD3D_Release(This->wineD3D);
286 /* Now free the object */
287 HeapFree(GetProcessHeap(), 0, This);
290 /*****************************************************************************
291 * IDirectDraw7::Release
293 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
295 * Returns: The new refcount
296 *****************************************************************************/
297 static ULONG WINAPI
298 IDirectDrawImpl_Release(IDirectDraw7 *iface)
300 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
301 ULONG ref = InterlockedDecrement(&This->ref7);
303 TRACE("(%p)->() decrementing IDirectDraw7 refcount from %u.\n", This, ref +1);
305 if(ref == 0)
307 ULONG ifacecount = InterlockedDecrement(&This->numIfaces);
308 if(ifacecount == 0) IDirectDrawImpl_Destroy(This);
311 return ref;
314 /*****************************************************************************
315 * IDirectDraw methods
316 *****************************************************************************/
318 /*****************************************************************************
319 * IDirectDraw7::SetCooperativeLevel
321 * Sets the cooperative level for the DirectDraw object, and the window
322 * assigned to it. The cooperative level determines the general behavior
323 * of the DirectDraw application
325 * Warning: This is quite tricky, as it's not really documented which
326 * cooperative levels can be combined with each other. If a game fails
327 * after this function, try to check the cooperative levels passed on
328 * Windows, and if it returns something different.
330 * If you think that this function caused the failure because it writes a
331 * fixme, be sure to run again with a +ddraw trace.
333 * What is known about cooperative levels (See the ddraw modes test):
334 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
335 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
336 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
337 * DDSCL_FULLSCREEN can be activated
338 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
340 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
341 * DDSCL_SETFOCUSWINDOW (partially),
342 * DDSCL_MULTITHREADED (work in progress)
344 * Unhandled flags, which should be implemented
345 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
346 * expect any difference to a normal window for wine)
347 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
348 * rendering (Possible test case: Half-life)
350 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
352 * These don't seem very important for wine:
353 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
355 * Returns:
356 * DD_OK if the cooperative level was set successfully
357 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
358 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
359 * (Probably others too, have to investigate)
361 *****************************************************************************/
362 static HRESULT WINAPI
363 IDirectDrawImpl_SetCooperativeLevel(IDirectDraw7 *iface,
364 HWND hwnd,
365 DWORD cooplevel)
367 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
368 HWND window;
370 TRACE("(%p)->(%p,%08x)\n",This,hwnd,cooplevel);
371 DDRAW_dump_cooperativelevel(cooplevel);
373 EnterCriticalSection(&ddraw_cs);
375 /* Get the old window */
376 window = This->dest_window;
378 /* Tests suggest that we need one of them: */
379 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
380 DDSCL_NORMAL |
381 DDSCL_EXCLUSIVE )))
383 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
384 LeaveCriticalSection(&ddraw_cs);
385 return DDERR_INVALIDPARAMS;
388 /* Handle those levels first which set various hwnds */
389 if(cooplevel & DDSCL_SETFOCUSWINDOW)
391 /* This isn't compatible with a lot of flags */
392 if(cooplevel & ( DDSCL_MULTITHREADED |
393 DDSCL_FPUSETUP |
394 DDSCL_FPUPRESERVE |
395 DDSCL_ALLOWREBOOT |
396 DDSCL_ALLOWMODEX |
397 DDSCL_SETDEVICEWINDOW |
398 DDSCL_NORMAL |
399 DDSCL_EXCLUSIVE |
400 DDSCL_FULLSCREEN ) )
402 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
403 LeaveCriticalSection(&ddraw_cs);
404 return DDERR_INVALIDPARAMS;
406 else if( (This->cooperative_level & DDSCL_FULLSCREEN) && window)
408 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET\n");
409 LeaveCriticalSection(&ddraw_cs);
410 return DDERR_HWNDALREADYSET;
413 This->focuswindow = hwnd;
414 /* Won't use the hwnd param for anything else */
415 hwnd = NULL;
417 /* Use the focus window for drawing too */
418 This->dest_window = This->focuswindow;
420 /* Destroy the device window, if we have one */
421 if(This->devicewindow)
423 DestroyWindow(This->devicewindow);
424 This->devicewindow = NULL;
427 /* DDSCL_NORMAL or DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE */
428 if(cooplevel & DDSCL_NORMAL)
430 /* Can't coexist with fullscreen or exclusive */
431 if(cooplevel & (DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE) )
433 TRACE("(%p) DDSCL_NORMAL is not compative with DDSCL_FULLSCREEN or DDSCL_EXCLUSIVE\n", This);
434 LeaveCriticalSection(&ddraw_cs);
435 return DDERR_INVALIDPARAMS;
438 /* Switching from fullscreen? */
439 if(This->cooperative_level & DDSCL_FULLSCREEN)
441 This->cooperative_level &= ~DDSCL_FULLSCREEN;
442 This->cooperative_level &= ~DDSCL_EXCLUSIVE;
443 This->cooperative_level &= ~DDSCL_ALLOWMODEX;
445 IWineD3DDevice_ReleaseFocusWindow(This->wineD3DDevice);
448 /* Don't override focus windows or private device windows */
449 if( hwnd &&
450 !(This->focuswindow) &&
451 !(This->devicewindow) &&
452 (hwnd != window) )
454 This->dest_window = hwnd;
457 else if(cooplevel & DDSCL_FULLSCREEN)
459 /* Needs DDSCL_EXCLUSIVE */
460 if(!(cooplevel & DDSCL_EXCLUSIVE) )
462 TRACE("(%p) DDSCL_FULLSCREEN needs DDSCL_EXCLUSIVE\n", This);
463 LeaveCriticalSection(&ddraw_cs);
464 return DDERR_INVALIDPARAMS;
466 /* Need a HWND
467 if(hwnd == 0)
469 TRACE("(%p) DDSCL_FULLSCREEN needs a HWND\n", This);
470 return DDERR_INVALIDPARAMS;
474 This->cooperative_level &= ~DDSCL_NORMAL;
476 /* Don't override focus windows or private device windows */
477 if( hwnd &&
478 !(This->focuswindow) &&
479 !(This->devicewindow) &&
480 (hwnd != window) )
482 HRESULT hr = IWineD3DDevice_AcquireFocusWindow(This->wineD3DDevice, hwnd);
483 if (FAILED(hr))
485 ERR("Failed to acquire focus window, hr %#x.\n", hr);
486 LeaveCriticalSection(&ddraw_cs);
487 return hr;
489 This->dest_window = hwnd;
492 else if(cooplevel & DDSCL_EXCLUSIVE)
494 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN\n", This);
495 LeaveCriticalSection(&ddraw_cs);
496 return DDERR_INVALIDPARAMS;
499 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
501 /* Don't create a device window if a focus window is set */
502 if( !(This->focuswindow) )
504 HWND devicewindow = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "DDraw device window",
505 WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
506 NULL, NULL, NULL, NULL);
507 if (!devicewindow)
509 ERR("Failed to create window, last error %#x.\n", GetLastError());
510 LeaveCriticalSection(&ddraw_cs);
511 return E_FAIL;
514 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
515 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
517 This->devicewindow = devicewindow;
518 This->dest_window = devicewindow;
522 if(cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
524 /* Enable thread safety in wined3d */
525 IWineD3DDevice_SetMultithreaded(This->wineD3DDevice);
528 /* Unhandled flags */
529 if(cooplevel & DDSCL_ALLOWREBOOT)
530 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
531 if(cooplevel & DDSCL_ALLOWMODEX)
532 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
533 if(cooplevel & DDSCL_FPUSETUP)
534 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
536 /* Store the cooperative_level */
537 This->cooperative_level |= cooplevel;
538 TRACE("SetCooperativeLevel retuning DD_OK\n");
539 LeaveCriticalSection(&ddraw_cs);
540 return DD_OK;
543 /*****************************************************************************
545 * Helper function for SetDisplayMode and RestoreDisplayMode
547 * Implements DirectDraw's SetDisplayMode, but ignores the value of
548 * ForceRefreshRate, since it is already handled by
549 * IDirectDrawImpl_SetDisplayMode. RestoreDisplayMode can use this function
550 * without worrying that ForceRefreshRate will override the refresh rate. For
551 * argument and return value documentation, see
552 * IDirectDrawImpl_SetDisplayMode.
554 *****************************************************************************/
555 static HRESULT
556 IDirectDrawImpl_SetDisplayModeNoOverride(IDirectDraw7 *iface,
557 DWORD Width,
558 DWORD Height,
559 DWORD BPP,
560 DWORD RefreshRate,
561 DWORD Flags)
563 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
564 WINED3DDISPLAYMODE Mode;
565 HRESULT hr;
566 TRACE("(%p)->(%d,%d,%d,%d,%x): Relay!\n", This, Width, Height, BPP, RefreshRate, Flags);
568 EnterCriticalSection(&ddraw_cs);
569 if( !Width || !Height )
571 ERR("Width=%d, Height=%d, what to do?\n", Width, Height);
572 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
573 LeaveCriticalSection(&ddraw_cs);
574 return DD_OK;
577 /* Check the exclusive mode
578 if(!(This->cooperative_level & DDSCL_EXCLUSIVE))
579 return DDERR_NOEXCLUSIVEMODE;
580 * This is WRONG. Don't know if the SDK is completely
581 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
582 * is returned, but Half-Life 1.1.1.1 (Steam version)
583 * depends on this
586 Mode.Width = Width;
587 Mode.Height = Height;
588 Mode.RefreshRate = RefreshRate;
589 switch(BPP)
591 case 8: Mode.Format = WINED3DFMT_P8_UINT; break;
592 case 15: Mode.Format = WINED3DFMT_B5G5R5X1_UNORM; break;
593 case 16: Mode.Format = WINED3DFMT_B5G6R5_UNORM; break;
594 case 24: Mode.Format = WINED3DFMT_B8G8R8_UNORM; break;
595 case 32: Mode.Format = WINED3DFMT_B8G8R8X8_UNORM; break;
598 /* TODO: The possible return values from msdn suggest that
599 * the screen mode can't be changed if a surface is locked
600 * or some drawing is in progress
603 /* TODO: Lose the primary surface */
604 hr = IWineD3DDevice_SetDisplayMode(This->wineD3DDevice,
605 0, /* First swapchain */
606 &Mode);
607 LeaveCriticalSection(&ddraw_cs);
608 switch(hr)
610 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
611 default: return hr;
615 /*****************************************************************************
616 * IDirectDraw7::SetDisplayMode
618 * Sets the display screen resolution, color depth and refresh frequency
619 * when in fullscreen mode (in theory).
620 * Possible return values listed in the SDK suggest that this method fails
621 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
622 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
623 * It seems to be valid to pass 0 for With and Height, this has to be tested
624 * It could mean that the current video mode should be left as-is. (But why
625 * call it then?)
627 * Params:
628 * Height, Width: Screen dimension
629 * BPP: Color depth in Bits per pixel
630 * Refreshrate: Screen refresh rate
631 * Flags: Other stuff
633 * Returns
634 * DD_OK on success
636 *****************************************************************************/
637 static HRESULT WINAPI
638 IDirectDrawImpl_SetDisplayMode(IDirectDraw7 *iface,
639 DWORD Width,
640 DWORD Height,
641 DWORD BPP,
642 DWORD RefreshRate,
643 DWORD Flags)
645 if (force_refresh_rate != 0)
647 TRACE("ForceRefreshRate overriding passed-in refresh rate (%d Hz) to %d Hz\n", RefreshRate, force_refresh_rate);
648 RefreshRate = force_refresh_rate;
651 return IDirectDrawImpl_SetDisplayModeNoOverride(iface, Width, Height, BPP,
652 RefreshRate, Flags);
655 /*****************************************************************************
656 * IDirectDraw7::RestoreDisplayMode
658 * Restores the display mode to what it was at creation time. Basically.
660 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
661 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
662 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
663 * -> DD_1 is released. The screen should be left at 1024x768x32.
664 * -> DD_2 is released. The screen should be set to 1400x1050x32
665 * This case is unhandled right now, but Empire Earth does it this way.
666 * (But perhaps there is something in SetCooperativeLevel to prevent this)
668 * The msdn says that this method resets the display mode to what it was before
669 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
671 * Returns
672 * DD_OK on success
673 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
675 *****************************************************************************/
676 static HRESULT WINAPI
677 IDirectDrawImpl_RestoreDisplayMode(IDirectDraw7 *iface)
679 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
680 TRACE("(%p)\n", This);
682 return IDirectDrawImpl_SetDisplayModeNoOverride(iface,
683 This->orig_width, This->orig_height, This->orig_bpp, 0, 0);
686 /*****************************************************************************
687 * IDirectDraw7::GetCaps
689 * Returns the drives capabilities
691 * Used for version 1, 2, 4 and 7
693 * Params:
694 * DriverCaps: Structure to write the Hardware accelerated caps to
695 * HelCaps: Structure to write the emulation caps to
697 * Returns
698 * This implementation returns DD_OK only
700 *****************************************************************************/
701 static HRESULT WINAPI
702 IDirectDrawImpl_GetCaps(IDirectDraw7 *iface,
703 DDCAPS *DriverCaps,
704 DDCAPS *HELCaps)
706 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
707 DDCAPS caps;
708 WINED3DCAPS winecaps;
709 HRESULT hr;
710 DDSCAPS2 ddscaps = {0, 0, 0, 0};
711 TRACE("(%p)->(%p,%p)\n", This, DriverCaps, HELCaps);
713 /* One structure must be != NULL */
714 if( (!DriverCaps) && (!HELCaps) )
716 ERR("(%p) Invalid params to IDirectDrawImpl_GetCaps\n", This);
717 return DDERR_INVALIDPARAMS;
720 memset(&caps, 0, sizeof(caps));
721 memset(&winecaps, 0, sizeof(winecaps));
722 caps.dwSize = sizeof(caps);
723 EnterCriticalSection(&ddraw_cs);
724 hr = IWineD3DDevice_GetDeviceCaps(This->wineD3DDevice, &winecaps);
725 if(FAILED(hr)) {
726 WARN("IWineD3DDevice::GetDeviceCaps failed\n");
727 LeaveCriticalSection(&ddraw_cs);
728 return hr;
731 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
732 LeaveCriticalSection(&ddraw_cs);
733 if(FAILED(hr)) {
734 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
735 return hr;
738 caps.dwCaps = winecaps.DirectDrawCaps.Caps;
739 caps.dwCaps2 = winecaps.DirectDrawCaps.Caps2;
740 caps.dwCKeyCaps = winecaps.DirectDrawCaps.CKeyCaps;
741 caps.dwFXCaps = winecaps.DirectDrawCaps.FXCaps;
742 caps.dwPalCaps = winecaps.DirectDrawCaps.PalCaps;
743 caps.ddsCaps.dwCaps = winecaps.DirectDrawCaps.ddsCaps;
744 caps.dwSVBCaps = winecaps.DirectDrawCaps.SVBCaps;
745 caps.dwSVBCKeyCaps = winecaps.DirectDrawCaps.SVBCKeyCaps;
746 caps.dwSVBFXCaps = winecaps.DirectDrawCaps.SVBFXCaps;
747 caps.dwVSBCaps = winecaps.DirectDrawCaps.VSBCaps;
748 caps.dwVSBCKeyCaps = winecaps.DirectDrawCaps.VSBCKeyCaps;
749 caps.dwVSBFXCaps = winecaps.DirectDrawCaps.VSBFXCaps;
750 caps.dwSSBCaps = winecaps.DirectDrawCaps.SSBCaps;
751 caps.dwSSBCKeyCaps = winecaps.DirectDrawCaps.SSBCKeyCaps;
752 caps.dwSSBFXCaps = winecaps.DirectDrawCaps.SSBFXCaps;
754 /* Even if WineD3D supports 3D rendering, remove the cap if ddraw is configured
755 * not to use it
757 if(DefaultSurfaceType == SURFACE_GDI) {
758 caps.dwCaps &= ~DDCAPS_3D;
759 caps.ddsCaps.dwCaps &= ~(DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER);
761 if(winecaps.DirectDrawCaps.StrideAlign != 0) {
762 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
763 caps.dwAlignStrideAlign = winecaps.DirectDrawCaps.StrideAlign;
766 if(DriverCaps)
768 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
769 if (TRACE_ON(ddraw))
771 TRACE("Driver Caps :\n");
772 DDRAW_dump_DDCAPS(DriverCaps);
776 if(HELCaps)
778 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
779 if (TRACE_ON(ddraw))
781 TRACE("HEL Caps :\n");
782 DDRAW_dump_DDCAPS(HELCaps);
786 return DD_OK;
789 /*****************************************************************************
790 * IDirectDraw7::Compact
792 * No idea what it does, MSDN says it's not implemented.
794 * Returns
795 * DD_OK, but this is unchecked
797 *****************************************************************************/
798 static HRESULT WINAPI
799 IDirectDrawImpl_Compact(IDirectDraw7 *iface)
801 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
802 TRACE("(%p)\n", This);
804 return DD_OK;
807 /*****************************************************************************
808 * IDirectDraw7::GetDisplayMode
810 * Returns information about the current display mode
812 * Exists in Version 1, 2, 4 and 7
814 * Params:
815 * DDSD: Address of a surface description structure to write the info to
817 * Returns
818 * DD_OK
820 *****************************************************************************/
821 static HRESULT WINAPI
822 IDirectDrawImpl_GetDisplayMode(IDirectDraw7 *iface,
823 DDSURFACEDESC2 *DDSD)
825 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
826 HRESULT hr;
827 WINED3DDISPLAYMODE Mode;
828 DWORD Size;
829 TRACE("(%p)->(%p): Relay\n", This, DDSD);
831 EnterCriticalSection(&ddraw_cs);
832 /* This seems sane */
833 if (!DDSD)
835 LeaveCriticalSection(&ddraw_cs);
836 return DDERR_INVALIDPARAMS;
839 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
840 * so one method can be used for all versions (Hopefully)
842 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
843 0 /* swapchain 0 */,
844 &Mode);
845 if( hr != D3D_OK )
847 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
848 LeaveCriticalSection(&ddraw_cs);
849 return hr;
852 Size = DDSD->dwSize;
853 memset(DDSD, 0, Size);
855 DDSD->dwSize = Size;
856 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
857 DDSD->dwWidth = Mode.Width;
858 DDSD->dwHeight = Mode.Height;
859 DDSD->u2.dwRefreshRate = 60;
860 DDSD->ddsCaps.dwCaps = 0;
861 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
862 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, Mode.Format);
863 DDSD->u1.lPitch = Mode.Width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
865 if(TRACE_ON(ddraw))
867 TRACE("Returning surface desc :\n");
868 DDRAW_dump_surface_desc(DDSD);
871 LeaveCriticalSection(&ddraw_cs);
872 return DD_OK;
875 /*****************************************************************************
876 * IDirectDraw7::GetFourCCCodes
878 * Returns an array of supported FourCC codes.
880 * Exists in Version 1, 2, 4 and 7
882 * Params:
883 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
884 * of enumerated codes
885 * Codes: Pointer to an array of DWORDs where the supported codes are written
886 * to
888 * Returns
889 * Always returns DD_OK, as it's a stub for now
891 *****************************************************************************/
892 static HRESULT WINAPI
893 IDirectDrawImpl_GetFourCCCodes(IDirectDraw7 *iface,
894 DWORD *NumCodes, DWORD *Codes)
896 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
897 WINED3DFORMAT formats[] = {
898 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
899 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
900 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
902 DWORD count = 0, i, outsize;
903 HRESULT hr;
904 WINED3DDISPLAYMODE d3ddm;
905 WINED3DSURFTYPE type = This->ImplType;
906 TRACE("(%p)->(%p, %p)\n", This, NumCodes, Codes);
908 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
909 0 /* swapchain 0 */,
910 &d3ddm);
912 outsize = NumCodes && Codes ? *NumCodes : 0;
914 if(type == SURFACE_UNKNOWN) type = SURFACE_GDI;
916 for(i = 0; i < (sizeof(formats) / sizeof(formats[0])); i++) {
917 hr = IWineD3D_CheckDeviceFormat(This->wineD3D,
918 WINED3DADAPTER_DEFAULT,
919 WINED3DDEVTYPE_HAL,
920 d3ddm.Format /* AdapterFormat */,
921 0 /* usage */,
922 WINED3DRTYPE_SURFACE,
923 formats[i],
924 type);
925 if(SUCCEEDED(hr)) {
926 if(count < outsize) {
927 Codes[count] = formats[i];
929 count++;
932 if(NumCodes) {
933 TRACE("Returning %u FourCC codes\n", count);
934 *NumCodes = count;
937 return DD_OK;
940 /*****************************************************************************
941 * IDirectDraw7::GetMonitorFrequency
943 * Returns the monitor's frequency
945 * Exists in Version 1, 2, 4 and 7
947 * Params:
948 * Freq: Pointer to a DWORD to write the frequency to
950 * Returns
951 * Always returns DD_OK
953 *****************************************************************************/
954 static HRESULT WINAPI
955 IDirectDrawImpl_GetMonitorFrequency(IDirectDraw7 *iface,
956 DWORD *Freq)
958 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
959 TRACE("(%p)->(%p)\n", This, Freq);
961 /* Ideally this should be in WineD3D, as it concerns the screen setup,
962 * but for now this should make the games happy
964 *Freq = 60;
965 return DD_OK;
968 /*****************************************************************************
969 * IDirectDraw7::GetVerticalBlankStatus
971 * Returns the Vertical blank status of the monitor. This should be in WineD3D
972 * too basically, but as it's a semi stub, I didn't create a function there
974 * Params:
975 * status: Pointer to a BOOL to be filled with the vertical blank status
977 * Returns
978 * DD_OK on success
979 * DDERR_INVALIDPARAMS if status is NULL
981 *****************************************************************************/
982 static HRESULT WINAPI
983 IDirectDrawImpl_GetVerticalBlankStatus(IDirectDraw7 *iface,
984 BOOL *status)
986 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
987 TRACE("(%p)->(%p)\n", This, status);
989 /* This looks sane, the MSDN suggests it too */
990 EnterCriticalSection(&ddraw_cs);
991 if(!status)
993 LeaveCriticalSection(&ddraw_cs);
994 return DDERR_INVALIDPARAMS;
997 *status = This->fake_vblank;
998 This->fake_vblank = !This->fake_vblank;
999 LeaveCriticalSection(&ddraw_cs);
1000 return DD_OK;
1003 /*****************************************************************************
1004 * IDirectDraw7::GetAvailableVidMem
1006 * Returns the total and free video memory
1008 * Params:
1009 * Caps: Specifies the memory type asked for
1010 * total: Pointer to a DWORD to be filled with the total memory
1011 * free: Pointer to a DWORD to be filled with the free memory
1013 * Returns
1014 * DD_OK on success
1015 * DDERR_INVALIDPARAMS of free and total are NULL
1017 *****************************************************************************/
1018 static HRESULT WINAPI
1019 IDirectDrawImpl_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total, DWORD *free)
1021 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1022 TRACE("(%p)->(%p, %p, %p)\n", This, Caps, total, free);
1024 if(TRACE_ON(ddraw))
1026 TRACE("(%p) Asked for memory with description: ", This);
1027 DDRAW_dump_DDSCAPS2(Caps);
1029 EnterCriticalSection(&ddraw_cs);
1031 /* Todo: System memory vs local video memory vs non-local video memory
1032 * The MSDN also mentions differences between texture memory and other
1033 * resources, but that's not important
1036 if( (!total) && (!free) )
1038 LeaveCriticalSection(&ddraw_cs);
1039 return DDERR_INVALIDPARAMS;
1042 if(total) *total = This->total_vidmem;
1043 if(free) *free = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
1045 LeaveCriticalSection(&ddraw_cs);
1046 return DD_OK;
1049 /*****************************************************************************
1050 * IDirectDraw7::Initialize
1052 * Initializes a DirectDraw interface.
1054 * Params:
1055 * GUID: Interface identifier. Well, don't know what this is really good
1056 * for
1058 * Returns
1059 * Returns DD_OK on the first call,
1060 * DDERR_ALREADYINITIALIZED on repeated calls
1062 *****************************************************************************/
1063 static HRESULT WINAPI
1064 IDirectDrawImpl_Initialize(IDirectDraw7 *iface,
1065 GUID *Guid)
1067 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1068 TRACE("(%p)->(%s): No-op\n", This, debugstr_guid(Guid));
1070 if(This->initialized)
1072 return DDERR_ALREADYINITIALIZED;
1074 else
1076 return DD_OK;
1080 /*****************************************************************************
1081 * IDirectDraw7::FlipToGDISurface
1083 * "Makes the surface that the GDI writes to the primary surface"
1084 * Looks like some windows specific thing we don't have to care about.
1085 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1086 * show error boxes ;)
1087 * Well, just return DD_OK.
1089 * Returns:
1090 * Always returns DD_OK
1092 *****************************************************************************/
1093 static HRESULT WINAPI
1094 IDirectDrawImpl_FlipToGDISurface(IDirectDraw7 *iface)
1096 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1097 TRACE("(%p)\n", This);
1099 return DD_OK;
1102 /*****************************************************************************
1103 * IDirectDraw7::WaitForVerticalBlank
1105 * This method allows applications to get in sync with the vertical blank
1106 * interval.
1107 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1108 * redraw the screen, most likely because of this stub
1110 * Parameters:
1111 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1112 * or DDWAITVB_BLOCKEND
1113 * h: Not used, according to MSDN
1115 * Returns:
1116 * Always returns DD_OK
1118 *****************************************************************************/
1119 static HRESULT WINAPI
1120 IDirectDrawImpl_WaitForVerticalBlank(IDirectDraw7 *iface,
1121 DWORD Flags,
1122 HANDLE h)
1124 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1125 static BOOL hide = FALSE;
1127 /* This function is called often, so print the fixme only once */
1128 if(!hide)
1130 FIXME("(%p)->(%x,%p): Stub\n", This, Flags, h);
1131 hide = TRUE;
1134 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1135 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1136 return DDERR_UNSUPPORTED; /* unchecked */
1138 return DD_OK;
1141 /*****************************************************************************
1142 * IDirectDraw7::GetScanLine
1144 * Returns the scan line that is being drawn on the monitor
1146 * Parameters:
1147 * Scanline: Address to write the scan line value to
1149 * Returns:
1150 * Always returns DD_OK
1152 *****************************************************************************/
1153 static HRESULT WINAPI IDirectDrawImpl_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1155 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1156 static BOOL hide = FALSE;
1157 WINED3DDISPLAYMODE Mode;
1159 /* This function is called often, so print the fixme only once */
1160 EnterCriticalSection(&ddraw_cs);
1161 if(!hide)
1163 FIXME("(%p)->(%p): Semi-Stub\n", This, Scanline);
1164 hide = TRUE;
1167 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1169 &Mode);
1171 /* Fake the line sweeping of the monitor */
1172 /* FIXME: We should synchronize with a source to keep the refresh rate */
1173 *Scanline = This->cur_scanline++;
1174 /* Assume 20 scan lines in the vertical blank */
1175 if (This->cur_scanline >= Mode.Height + 20)
1176 This->cur_scanline = 0;
1178 LeaveCriticalSection(&ddraw_cs);
1179 return DD_OK;
1182 /*****************************************************************************
1183 * IDirectDraw7::TestCooperativeLevel
1185 * Informs the application about the state of the video adapter, depending
1186 * on the cooperative level
1188 * Returns:
1189 * DD_OK if the device is in a sane state
1190 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1191 * if the state is not correct(See below)
1193 *****************************************************************************/
1194 static HRESULT WINAPI
1195 IDirectDrawImpl_TestCooperativeLevel(IDirectDraw7 *iface)
1197 TRACE("iface %p.\n", iface);
1199 return DD_OK;
1202 /*****************************************************************************
1203 * IDirectDraw7::GetGDISurface
1205 * Returns the surface that GDI is treating as the primary surface.
1206 * For Wine this is the front buffer
1208 * Params:
1209 * GDISurface: Address to write the surface pointer to
1211 * Returns:
1212 * DD_OK if the surface was found
1213 * DDERR_NOTFOUND if the GDI surface wasn't found
1215 *****************************************************************************/
1216 static HRESULT WINAPI
1217 IDirectDrawImpl_GetGDISurface(IDirectDraw7 *iface,
1218 IDirectDrawSurface7 **GDISurface)
1220 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1221 IWineD3DSurface *Surf;
1222 IDirectDrawSurface7 *ddsurf;
1223 HRESULT hr;
1224 DDSCAPS2 ddsCaps;
1225 TRACE("(%p)->(%p)\n", This, GDISurface);
1227 /* Get the back buffer from the wineD3DDevice and search its
1228 * attached surfaces for the front buffer
1230 EnterCriticalSection(&ddraw_cs);
1231 hr = IWineD3DDevice_GetBackBuffer(This->wineD3DDevice,
1232 0, /* SwapChain */
1233 0, /* first back buffer*/
1234 WINED3DBACKBUFFER_TYPE_MONO,
1235 &Surf);
1237 if( (hr != D3D_OK) ||
1238 (!Surf) )
1240 ERR("IWineD3DDevice::GetBackBuffer failed\n");
1241 LeaveCriticalSection(&ddraw_cs);
1242 return DDERR_NOTFOUND;
1245 /* GetBackBuffer AddRef()ed the surface, release it */
1246 IWineD3DSurface_Release(Surf);
1248 IWineD3DSurface_GetParent(Surf,
1249 (IUnknown **) &ddsurf);
1250 IDirectDrawSurface7_Release(ddsurf); /* For the GetParent */
1252 /* Find the front buffer */
1253 ddsCaps.dwCaps = DDSCAPS_FRONTBUFFER;
1254 hr = IDirectDrawSurface7_GetAttachedSurface(ddsurf,
1255 &ddsCaps,
1256 GDISurface);
1257 if(hr != DD_OK)
1259 ERR("IDirectDrawSurface7::GetAttachedSurface failed, hr = %x\n", hr);
1262 /* The AddRef is OK this time */
1263 LeaveCriticalSection(&ddraw_cs);
1264 return hr;
1267 /*****************************************************************************
1268 * IDirectDraw7::EnumDisplayModes
1270 * Enumerates the supported Display modes. The modes can be filtered with
1271 * the DDSD parameter.
1273 * Params:
1274 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES
1275 * DDSD: Surface description to filter the modes
1276 * Context: Pointer passed back to the callback function
1277 * cb: Application-provided callback function
1279 * Returns:
1280 * DD_OK on success
1281 * DDERR_INVALIDPARAMS if the callback wasn't set
1283 *****************************************************************************/
1284 static HRESULT WINAPI
1285 IDirectDrawImpl_EnumDisplayModes(IDirectDraw7 *iface,
1286 DWORD Flags,
1287 DDSURFACEDESC2 *DDSD,
1288 void *Context,
1289 LPDDENUMMODESCALLBACK2 cb)
1291 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1292 unsigned int modenum, fmt;
1293 WINED3DFORMAT pixelformat = WINED3DFMT_UNKNOWN;
1294 WINED3DDISPLAYMODE mode;
1295 DDSURFACEDESC2 callback_sd;
1296 WINED3DDISPLAYMODE *enum_modes = NULL;
1297 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
1299 WINED3DFORMAT checkFormatList[] =
1301 WINED3DFMT_B8G8R8X8_UNORM,
1302 WINED3DFMT_B5G6R5_UNORM,
1303 WINED3DFMT_P8_UINT,
1306 TRACE("(%p)->(%p,%p,%p): Relay\n", This, DDSD, Context, cb);
1308 EnterCriticalSection(&ddraw_cs);
1309 /* This looks sane */
1310 if(!cb)
1312 LeaveCriticalSection(&ddraw_cs);
1313 return DDERR_INVALIDPARAMS;
1316 if(DDSD)
1318 if ((DDSD->dwFlags & DDSD_PIXELFORMAT) && (DDSD->u4.ddpfPixelFormat.dwFlags & DDPF_RGB) )
1319 pixelformat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
1322 if(!(Flags & DDEDM_REFRESHRATES))
1324 enum_mode_array_size = 16;
1325 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1326 if (!enum_modes)
1328 ERR("Out of memory\n");
1329 LeaveCriticalSection(&ddraw_cs);
1330 return DDERR_OUTOFMEMORY;
1334 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
1336 if(pixelformat != WINED3DFMT_UNKNOWN && checkFormatList[fmt] != pixelformat)
1338 continue;
1341 modenum = 0;
1342 while(IWineD3D_EnumAdapterModes(This->wineD3D,
1343 WINED3DADAPTER_DEFAULT,
1344 checkFormatList[fmt],
1345 modenum++,
1346 &mode) == WINED3D_OK)
1348 if(DDSD)
1350 if(DDSD->dwFlags & DDSD_WIDTH && mode.Width != DDSD->dwWidth) continue;
1351 if(DDSD->dwFlags & DDSD_HEIGHT && mode.Height != DDSD->dwHeight) continue;
1354 if(!(Flags & DDEDM_REFRESHRATES))
1356 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
1357 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
1358 * to be reduced to a single unique result in such case.
1360 BOOL found = FALSE;
1361 unsigned i;
1363 for (i = 0; i < enum_mode_count; i++)
1365 if(enum_modes[i].Width == mode.Width && enum_modes[i].Height == mode.Height &&
1366 enum_modes[i].Format == mode.Format)
1368 found = TRUE;
1369 break;
1373 if(found) continue;
1376 memset(&callback_sd, 0, sizeof(callback_sd));
1377 callback_sd.dwSize = sizeof(callback_sd);
1378 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1380 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH;
1381 if(Flags & DDEDM_REFRESHRATES)
1383 callback_sd.dwFlags |= DDSD_REFRESHRATE;
1384 callback_sd.u2.dwRefreshRate = mode.RefreshRate;
1387 callback_sd.dwWidth = mode.Width;
1388 callback_sd.dwHeight = mode.Height;
1390 PixelFormat_WineD3DtoDD(&callback_sd.u4.ddpfPixelFormat, mode.Format);
1392 /* Calc pitch and DWORD align like MSDN says */
1393 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.Width;
1394 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
1396 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
1397 callback_sd.u2.dwRefreshRate);
1399 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
1401 TRACE("Application asked to terminate the enumeration\n");
1402 HeapFree(GetProcessHeap(), 0, enum_modes);
1403 LeaveCriticalSection(&ddraw_cs);
1404 return DD_OK;
1407 if(!(Flags & DDEDM_REFRESHRATES))
1409 if (enum_mode_count == enum_mode_array_size)
1411 WINED3DDISPLAYMODE *new_enum_modes;
1413 enum_mode_array_size *= 2;
1414 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1416 if (!new_enum_modes)
1418 ERR("Out of memory\n");
1419 HeapFree(GetProcessHeap(), 0, enum_modes);
1420 LeaveCriticalSection(&ddraw_cs);
1421 return DDERR_OUTOFMEMORY;
1424 enum_modes = new_enum_modes;
1427 enum_modes[enum_mode_count++] = mode;
1432 TRACE("End of enumeration\n");
1433 HeapFree(GetProcessHeap(), 0, enum_modes);
1434 LeaveCriticalSection(&ddraw_cs);
1435 return DD_OK;
1438 /*****************************************************************************
1439 * IDirectDraw7::EvaluateMode
1441 * Used with IDirectDraw7::StartModeTest to test video modes.
1442 * EvaluateMode is used to pass or fail a mode, and continue with the next
1443 * mode
1445 * Params:
1446 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
1447 * Timeout: Returns the amount of seconds left before the mode would have
1448 * been failed automatically
1450 * Returns:
1451 * This implementation always DD_OK, because it's a stub
1453 *****************************************************************************/
1454 static HRESULT WINAPI
1455 IDirectDrawImpl_EvaluateMode(IDirectDraw7 *iface,
1456 DWORD Flags,
1457 DWORD *Timeout)
1459 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1460 FIXME("(%p)->(%d,%p): Stub!\n", This, Flags, Timeout);
1462 /* When implementing this, implement it in WineD3D */
1464 return DD_OK;
1467 /*****************************************************************************
1468 * IDirectDraw7::GetDeviceIdentifier
1470 * Returns the device identifier, which gives information about the driver
1471 * Our device identifier is defined at the beginning of this file.
1473 * Params:
1474 * DDDI: Address for the returned structure
1475 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
1477 * Returns:
1478 * On success it returns DD_OK
1479 * DDERR_INVALIDPARAMS if DDDI is NULL
1481 *****************************************************************************/
1482 static HRESULT WINAPI
1483 IDirectDrawImpl_GetDeviceIdentifier(IDirectDraw7 *iface,
1484 DDDEVICEIDENTIFIER2 *DDDI,
1485 DWORD Flags)
1487 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1488 TRACE("(%p)->(%p,%08x)\n", This, DDDI, Flags);
1490 if(!DDDI)
1491 return DDERR_INVALIDPARAMS;
1493 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
1494 * host adapter, if there's a secondary 3D adapter. This doesn't apply
1495 * to any modern hardware, nor is it interesting for Wine, so ignore it.
1496 * Size of DDDEVICEIDENTIFIER2 may be aligned to 8 bytes and thus 4
1497 * bytes too long. So only copy the relevant part of the structure
1500 memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
1501 return DD_OK;
1504 /*****************************************************************************
1505 * IDirectDraw7::GetSurfaceFromDC
1507 * Returns the Surface for a GDI device context handle.
1508 * Is this related to IDirectDrawSurface::GetDC ???
1510 * Params:
1511 * hdc: hdc to return the surface for
1512 * Surface: Address to write the surface pointer to
1514 * Returns:
1515 * Always returns DD_OK because it's a stub
1517 *****************************************************************************/
1518 static HRESULT WINAPI
1519 IDirectDrawImpl_GetSurfaceFromDC(IDirectDraw7 *iface,
1520 HDC hdc,
1521 IDirectDrawSurface7 **Surface)
1523 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1524 IWineD3DSurface *wined3d_surface;
1525 HRESULT hr;
1527 TRACE("iface %p, dc %p, surface %p.\n", iface, hdc, Surface);
1529 if (!Surface) return E_INVALIDARG;
1531 hr = IWineD3DDevice_GetSurfaceFromDC(This->wineD3DDevice, hdc, &wined3d_surface);
1532 if (FAILED(hr))
1534 TRACE("No surface found for dc %p.\n", hdc);
1535 *Surface = NULL;
1536 return DDERR_NOTFOUND;
1539 IWineD3DSurface_GetParent(wined3d_surface, (IUnknown **)Surface);
1540 TRACE("Returning surface %p.\n", Surface);
1541 return DD_OK;
1544 /*****************************************************************************
1545 * IDirectDraw7::RestoreAllSurfaces
1547 * Calls the restore method of all surfaces
1549 * Params:
1551 * Returns:
1552 * Always returns DD_OK because it's a stub
1554 *****************************************************************************/
1555 static HRESULT WINAPI
1556 IDirectDrawImpl_RestoreAllSurfaces(IDirectDraw7 *iface)
1558 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1559 FIXME("(%p): Stub\n", This);
1561 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
1562 * get their parent and call their restore method. Do not implement
1563 * it in WineD3D, as restoring a surface means re-creating the
1564 * WineD3DDSurface
1566 return DD_OK;
1569 /*****************************************************************************
1570 * IDirectDraw7::StartModeTest
1572 * Tests the specified video modes to update the system registry with
1573 * refresh rate information. StartModeTest starts the mode test,
1574 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
1575 * isn't called within 15 seconds, the mode is failed automatically
1577 * As refresh rates are handled by the X server, I don't think this
1578 * Method is important
1580 * Params:
1581 * Modes: An array of mode specifications
1582 * NumModes: The number of modes in Modes
1583 * Flags: Some flags...
1585 * Returns:
1586 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
1587 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
1588 * otherwise DD_OK
1590 *****************************************************************************/
1591 static HRESULT WINAPI
1592 IDirectDrawImpl_StartModeTest(IDirectDraw7 *iface,
1593 SIZE *Modes,
1594 DWORD NumModes,
1595 DWORD Flags)
1597 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1598 WARN("(%p)->(%p, %d, %x): Semi-Stub, most likely harmless\n", This, Modes, NumModes, Flags);
1600 /* This looks sane */
1601 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
1603 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
1604 * As it is not, DDERR_TESTFINISHED is returned
1605 * (hopefully that's correct
1607 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
1608 * well, that value doesn't (yet) exist in the wine headers, so ignore it
1611 return DD_OK;
1614 /*****************************************************************************
1615 * IDirectDrawImpl_RecreateSurfacesCallback
1617 * Enumeration callback for IDirectDrawImpl_RecreateAllSurfaces.
1618 * It re-recreates the WineD3DSurface. It's pretty straightforward
1620 *****************************************************************************/
1621 HRESULT WINAPI
1622 IDirectDrawImpl_RecreateSurfacesCallback(IDirectDrawSurface7 *surf,
1623 DDSURFACEDESC2 *desc,
1624 void *Context)
1626 IDirectDrawSurfaceImpl *surfImpl = (IDirectDrawSurfaceImpl *)surf;
1627 IDirectDrawImpl *This = surfImpl->ddraw;
1628 IUnknown *Parent;
1629 IWineD3DSurface *wineD3DSurface;
1630 IWineD3DSwapChain *swapchain;
1631 HRESULT hr;
1632 IWineD3DClipper *clipper = NULL;
1634 WINED3DSURFACE_DESC Desc;
1635 WINED3DFORMAT Format;
1636 DWORD Usage;
1637 WINED3DPOOL Pool;
1639 WINED3DMULTISAMPLE_TYPE MultiSampleType;
1640 DWORD MultiSampleQuality;
1641 UINT Width;
1642 UINT Height;
1644 TRACE("(%p): Enumerated Surface %p\n", This, surfImpl);
1646 /* For the enumeration */
1647 IDirectDrawSurface7_Release(surf);
1649 if(surfImpl->ImplType == This->ImplType) return DDENUMRET_OK; /* Continue */
1651 /* Get the objects */
1652 swapchain = surfImpl->wineD3DSwapChain;
1653 surfImpl->wineD3DSwapChain = NULL;
1654 wineD3DSurface = surfImpl->WineD3DSurface;
1656 /* get the clipper */
1657 IWineD3DSurface_GetClipper(wineD3DSurface, &clipper);
1659 /* Get the surface properties */
1660 hr = IWineD3DSurface_GetDesc(wineD3DSurface, &Desc);
1661 if(hr != D3D_OK) return hr;
1663 Format = Desc.format;
1664 Usage = Desc.usage;
1665 Pool = Desc.pool;
1666 MultiSampleType = Desc.multisample_type;
1667 MultiSampleQuality = Desc.multisample_quality;
1668 Width = Desc.width;
1669 Height = Desc.height;
1671 IWineD3DSurface_GetParent(wineD3DSurface, &Parent);
1673 /* Create the new surface */
1674 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice, Width, Height, Format,
1675 TRUE /* Lockable */, FALSE /* Discard */, surfImpl->mipmap_level, &surfImpl->WineD3DSurface, Usage, Pool,
1676 MultiSampleType, MultiSampleQuality, This->ImplType, Parent, &ddraw_null_wined3d_parent_ops);
1677 IUnknown_Release(Parent);
1678 if (FAILED(hr))
1680 surfImpl->WineD3DSurface = wineD3DSurface;
1681 return hr;
1684 IWineD3DSurface_SetClipper(surfImpl->WineD3DSurface, clipper);
1686 /* TODO: Copy the surface content, except for render targets */
1688 /* If there's a swapchain, it owns the wined3d surfaces. So Destroy
1689 * the swapchain
1691 if(swapchain) {
1692 /* The backbuffers have the swapchain set as well, but the primary
1693 * owns it and destroys it
1695 if(surfImpl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
1696 IWineD3DDevice_UninitGDI(This->wineD3DDevice, D3D7CB_DestroySwapChain);
1698 surfImpl->isRenderTarget = FALSE;
1699 } else {
1700 if(IWineD3DSurface_Release(wineD3DSurface) == 0)
1701 TRACE("Surface released successful, next surface\n");
1702 else
1703 ERR("Something's still holding the old WineD3DSurface\n");
1706 surfImpl->ImplType = This->ImplType;
1708 if(clipper)
1710 IWineD3DClipper_Release(clipper);
1712 return DDENUMRET_OK;
1715 /*****************************************************************************
1716 * IDirectDrawImpl_RecreateAllSurfaces
1718 * A function, that converts all wineD3DSurfaces to the new implementation type
1719 * It enumerates all surfaces with IWineD3DDevice::EnumSurfaces, creates a
1720 * new WineD3DSurface, copies the content and releases the old surface
1722 *****************************************************************************/
1723 static HRESULT
1724 IDirectDrawImpl_RecreateAllSurfaces(IDirectDrawImpl *This)
1726 DDSURFACEDESC2 desc;
1727 TRACE("(%p): Switch to implementation %d\n", This, This->ImplType);
1729 if(This->ImplType != SURFACE_OPENGL && This->d3d_initialized)
1731 /* Should happen almost never */
1732 FIXME("(%p) Switching to non-opengl surfaces with d3d started. Is this a bug?\n", This);
1733 /* Shutdown d3d */
1734 IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroySwapChain);
1736 /* Contrary: D3D starting is handled by the caller, because it knows the render target */
1738 memset(&desc, 0, sizeof(desc));
1739 desc.dwSize = sizeof(desc);
1741 return IDirectDraw7_EnumSurfaces((IDirectDraw7 *)This, 0, &desc, This, IDirectDrawImpl_RecreateSurfacesCallback);
1744 ULONG WINAPI D3D7CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
1745 IUnknown* swapChainParent;
1746 TRACE("(%p) call back\n", pSwapChain);
1748 IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
1749 IUnknown_Release(swapChainParent);
1750 return IUnknown_Release(swapChainParent);
1753 /*****************************************************************************
1754 * IDirectDrawImpl_CreateNewSurface
1756 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
1757 * with the passed parameters.
1759 * Params:
1760 * DDSD: Description of the surface to create
1761 * Surf: Address to store the interface pointer at
1763 * Returns:
1764 * DD_OK on success
1766 *****************************************************************************/
1767 static HRESULT
1768 IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This,
1769 DDSURFACEDESC2 *pDDSD,
1770 IDirectDrawSurfaceImpl **ppSurf,
1771 UINT level)
1773 HRESULT hr;
1774 UINT Width, Height;
1775 WINED3DFORMAT Format = WINED3DFMT_UNKNOWN;
1776 DWORD Usage = 0;
1777 WINED3DSURFTYPE ImplType = This->ImplType;
1778 WINED3DSURFACE_DESC Desc;
1779 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
1781 if (TRACE_ON(ddraw))
1783 TRACE(" (%p) Requesting surface desc :\n", This);
1784 DDRAW_dump_surface_desc(pDDSD);
1787 /* Select the surface type, if it wasn't choosen yet */
1788 if(ImplType == SURFACE_UNKNOWN)
1790 /* Use GL Surfaces if a D3DDEVICE Surface is requested */
1791 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1793 TRACE("(%p) Choosing GL surfaces because a 3DDEVICE Surface was requested\n", This);
1794 ImplType = SURFACE_OPENGL;
1797 /* Otherwise use GDI surfaces for now */
1798 else
1800 TRACE("(%p) Choosing GDI surfaces for 2D rendering\n", This);
1801 ImplType = SURFACE_GDI;
1804 /* Policy if all surface implementations are available:
1805 * First, check if a default type was set with winecfg. If not,
1806 * try Xrender surfaces, and use them if they work. Next, check if
1807 * accelerated OpenGL is available, and use GL surfaces in this
1808 * case. If all else fails, use GDI surfaces. If a 3DDEVICE surface
1809 * was created, always use OpenGL surfaces.
1811 * (Note: Xrender surfaces are not implemented for now, the
1812 * unaccelerated implementation uses GDI to render in Software)
1815 /* Store the type. If it needs to be changed, all WineD3DSurfaces have to
1816 * be re-created. This could be done with IDirectDrawSurface7::Restore
1818 This->ImplType = ImplType;
1820 else
1822 if ((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1823 && (This->ImplType != SURFACE_OPENGL)
1824 && DefaultSurfaceType == SURFACE_UNKNOWN)
1826 /* We have to change to OpenGL,
1827 * and re-create all WineD3DSurfaces
1829 ImplType = SURFACE_OPENGL;
1830 This->ImplType = ImplType;
1831 TRACE("(%p) Re-creating all surfaces\n", This);
1832 IDirectDrawImpl_RecreateAllSurfaces(This);
1833 TRACE("(%p) Done recreating all surfaces\n", This);
1835 else if(This->ImplType != SURFACE_OPENGL && pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1837 WARN("The application requests a 3D capable surface, but a non-opengl surface was set in the registry\n");
1838 /* Do not fail surface creation, only fail 3D device creation */
1842 if (!(pDDSD->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY)) &&
1843 !((pDDSD->ddsCaps.dwCaps & DDSCAPS_TEXTURE) && (pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)) )
1845 /* Tests show surfaces without memory flags get these flags added right after creation. */
1846 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
1848 /* Get the correct wined3d usage */
1849 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE |
1850 DDSCAPS_3DDEVICE ) )
1852 Usage |= WINED3DUSAGE_RENDERTARGET;
1854 pDDSD->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
1856 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
1858 Usage |= WINED3DUSAGE_OVERLAY;
1860 if(This->depthstencil || (pDDSD->ddsCaps.dwCaps & DDSCAPS_ZBUFFER) )
1862 /* The depth stencil creation callback sets this flag.
1863 * Set the WineD3D usage to let it know that it's a depth
1864 * Stencil surface.
1866 Usage |= WINED3DUSAGE_DEPTHSTENCIL;
1868 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
1870 Pool = WINED3DPOOL_SYSTEMMEM;
1872 else if(pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
1874 Pool = WINED3DPOOL_MANAGED;
1875 /* Managed textures have the system memory flag set */
1876 pDDSD->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
1878 else if(pDDSD->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
1880 /* Videomemory adds localvidmem, this is mutually exclusive with systemmemory
1881 * and texturemanage
1883 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
1886 Format = PixelFormat_DD2WineD3D(&pDDSD->u4.ddpfPixelFormat);
1887 if(Format == WINED3DFMT_UNKNOWN)
1889 ERR("Unsupported / Unknown pixelformat\n");
1890 return DDERR_INVALIDPIXELFORMAT;
1893 /* Create the Surface object */
1894 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
1895 if(!*ppSurf)
1897 ERR("(%p) Error allocating memory for a surface\n", This);
1898 return DDERR_OUTOFVIDEOMEMORY;
1900 (*ppSurf)->lpVtbl = &IDirectDrawSurface7_Vtbl;
1901 (*ppSurf)->IDirectDrawSurface3_vtbl = &IDirectDrawSurface3_Vtbl;
1902 (*ppSurf)->IDirectDrawGammaControl_vtbl = &IDirectDrawGammaControl_Vtbl;
1903 (*ppSurf)->IDirect3DTexture2_vtbl = &IDirect3DTexture2_Vtbl;
1904 (*ppSurf)->IDirect3DTexture_vtbl = &IDirect3DTexture1_Vtbl;
1905 (*ppSurf)->ref = 1;
1906 (*ppSurf)->version = 7;
1907 TRACE("%p->version = %d\n", (*ppSurf), (*ppSurf)->version);
1908 (*ppSurf)->ddraw = This;
1909 (*ppSurf)->surface_desc.dwSize = sizeof(DDSURFACEDESC2);
1910 (*ppSurf)->surface_desc.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1911 DD_STRUCT_COPY_BYSIZE(&(*ppSurf)->surface_desc, pDDSD);
1913 /* Surface attachments */
1914 (*ppSurf)->next_attached = NULL;
1915 (*ppSurf)->first_attached = *ppSurf;
1917 /* Needed to re-create the surface on an implementation change */
1918 (*ppSurf)->ImplType = ImplType;
1920 /* For D3DDevice creation */
1921 (*ppSurf)->isRenderTarget = FALSE;
1923 /* A trace message for debugging */
1924 TRACE("(%p) Created IDirectDrawSurface implementation structure at %p\n", This, *ppSurf);
1926 /* Now create the WineD3D Surface */
1927 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice, pDDSD->dwWidth, pDDSD->dwHeight, Format,
1928 TRUE /* Lockable */, FALSE /* Discard */, level, &(*ppSurf)->WineD3DSurface,
1929 Usage, Pool, WINED3DMULTISAMPLE_NONE, 0 /* MultiSampleQuality */, ImplType,
1930 (IUnknown *)*ppSurf, &ddraw_null_wined3d_parent_ops);
1932 if(hr != D3D_OK)
1934 ERR("IWineD3DDevice::CreateSurface failed. hr = %08x\n", hr);
1935 return hr;
1938 /* Increase the surface counter, and attach the surface */
1939 InterlockedIncrement(&This->surfaces);
1940 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
1942 /* Here we could store all created surfaces in the DirectDrawImpl structure,
1943 * But this could also be delegated to WineDDraw, as it keeps track of all its
1944 * resources. Not implemented for now, as there are more important things ;)
1947 /* Get the pixel format of the WineD3DSurface and store it.
1948 * Don't use the Format choosen above, WineD3D might have
1949 * changed it
1951 (*ppSurf)->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
1952 hr = IWineD3DSurface_GetDesc((*ppSurf)->WineD3DSurface, &Desc);
1953 if(hr != D3D_OK)
1955 ERR("IWineD3DSurface::GetDesc failed\n");
1956 IDirectDrawSurface7_Release( (IDirectDrawSurface7 *) *ppSurf);
1957 return hr;
1960 Format = Desc.format;
1961 Width = Desc.width;
1962 Height = Desc.height;
1964 if(Format == WINED3DFMT_UNKNOWN)
1966 FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN\n");
1968 PixelFormat_WineD3DtoDD( &(*ppSurf)->surface_desc.u4.ddpfPixelFormat, Format);
1970 /* Anno 1602 stores the pitch right after surface creation, so make sure it's there.
1971 * I can't LockRect() the surface here because if OpenGL surfaces are in use, the
1972 * WineD3DDevice might not be usable for 3D yet, so an extra method was created.
1973 * TODO: Test other fourcc formats
1975 if(Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3 ||
1976 Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5)
1978 (*ppSurf)->surface_desc.dwFlags |= DDSD_LINEARSIZE;
1979 if(Format == WINED3DFMT_DXT1)
1981 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height) / 2;
1983 else
1985 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height);
1988 else
1990 (*ppSurf)->surface_desc.dwFlags |= DDSD_PITCH;
1991 (*ppSurf)->surface_desc.u1.lPitch = IWineD3DSurface_GetPitch((*ppSurf)->WineD3DSurface);
1994 /* Application passed a color key? Set it! */
1995 if(pDDSD->dwFlags & DDSD_CKDESTOVERLAY)
1997 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
1998 DDCKEY_DESTOVERLAY,
1999 (WINEDDCOLORKEY *) &pDDSD->u3.ddckCKDestOverlay);
2001 if(pDDSD->dwFlags & DDSD_CKDESTBLT)
2003 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2004 DDCKEY_DESTBLT,
2005 (WINEDDCOLORKEY *) &pDDSD->ddckCKDestBlt);
2007 if(pDDSD->dwFlags & DDSD_CKSRCOVERLAY)
2009 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2010 DDCKEY_SRCOVERLAY,
2011 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcOverlay);
2013 if(pDDSD->dwFlags & DDSD_CKSRCBLT)
2015 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2016 DDCKEY_SRCBLT,
2017 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcBlt);
2019 if ( pDDSD->dwFlags & DDSD_LPSURFACE)
2021 hr = IWineD3DSurface_SetMem((*ppSurf)->WineD3DSurface, pDDSD->lpSurface);
2022 if(hr != WINED3D_OK)
2024 /* No need for a trace here, wined3d does that for us */
2025 IDirectDrawSurface7_Release((IDirectDrawSurface7 *)*ppSurf);
2026 return hr;
2030 return DD_OK;
2032 /*****************************************************************************
2033 * CreateAdditionalSurfaces
2035 * Creates a new mipmap chain.
2037 * Params:
2038 * root: Root surface to attach the newly created chain to
2039 * count: number of surfaces to create
2040 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2041 * effects on the caller
2042 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2043 * creates an additional surface without the mipmapping flags
2045 *****************************************************************************/
2046 static HRESULT
2047 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2048 IDirectDrawSurfaceImpl *root,
2049 UINT count,
2050 DDSURFACEDESC2 DDSD,
2051 BOOL CubeFaceRoot)
2053 UINT i, j, level = 0;
2054 HRESULT hr;
2055 IDirectDrawSurfaceImpl *last = root;
2057 for(i = 0; i < count; i++)
2059 IDirectDrawSurfaceImpl *object2 = NULL;
2061 /* increase the mipmap level, but only if a mipmap is created
2062 * In this case, also halve the size
2064 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2066 level++;
2067 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2068 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2069 /* Set the mipmap sublevel flag according to msdn */
2070 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2072 else
2074 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2076 CubeFaceRoot = FALSE;
2078 hr = IDirectDrawImpl_CreateNewSurface(This,
2079 &DDSD,
2080 &object2,
2081 level);
2082 if(hr != DD_OK)
2084 return hr;
2087 /* Add the new surface to the complex attachment array */
2088 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2090 if(last->complex_array[j]) continue;
2091 last->complex_array[j] = object2;
2092 break;
2094 last = object2;
2096 /* Remove the (possible) back buffer cap from the new surface description,
2097 * because only one surface in the flipping chain is a back buffer, one
2098 * is a front buffer, the others are just primary surfaces.
2100 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2102 return DD_OK;
2105 /*****************************************************************************
2106 * IDirectDraw7::CreateSurface
2108 * Creates a new IDirectDrawSurface object and returns its interface.
2110 * The surface connections with wined3d are a bit tricky. Basically it works
2111 * like this:
2113 * |------------------------| |-----------------|
2114 * | DDraw surface | | WineD3DSurface |
2115 * | | | |
2116 * | WineD3DSurface |-------------->| |
2117 * | Child |<------------->| Parent |
2118 * |------------------------| |-----------------|
2120 * The DDraw surface is the parent of the wined3d surface, and it releases
2121 * the WineD3DSurface when the ddraw surface is destroyed.
2123 * However, for all surfaces which can be in a container in WineD3D,
2124 * we have to do this. These surfaces are usually complex surfaces,
2125 * so this concerns primary surfaces with a front and a back buffer,
2126 * and textures.
2128 * |------------------------| |-----------------|
2129 * | DDraw surface | | Container |
2130 * | | | |
2131 * | Child |<------------->| Parent |
2132 * | Texture |<------------->| |
2133 * | WineD3DSurface |<----| | Levels |<--|
2134 * | Complex connection | | | | |
2135 * |------------------------| | |-----------------| |
2136 * ^ | |
2137 * | | |
2138 * | | |
2139 * | |------------------| | |-----------------| |
2140 * | | IParent | |-------->| WineD3DSurface | |
2141 * | | | | | |
2142 * | | Child |<------------->| Parent | |
2143 * | | | | Container |<--|
2144 * | |------------------| |-----------------| |
2145 * | |
2146 * | |----------------------| |
2147 * | | DDraw surface 2 | |
2148 * | | | |
2149 * |<->| Complex root Child | |
2150 * | | Texture | |
2151 * | | WineD3DSurface |<----| |
2152 * | |----------------------| | |
2153 * | | |
2154 * | |---------------------| | |-----------------| |
2155 * | | IParent | |----->| WineD3DSurface | |
2156 * | | | | | |
2157 * | | Child |<---------->| Parent | |
2158 * | |---------------------| | Container |<--|
2159 * | |-----------------| |
2160 * | |
2161 * | ---More surfaces can follow--- |
2163 * The reason is that the IWineD3DSwapchain(render target container)
2164 * and the IWineD3DTexure(Texture container) release the parents
2165 * of their surface's children, but by releasing the complex root
2166 * the surfaces which are complexly attached to it are destroyed
2167 * too. See IDirectDrawSurface::Release for a more detailed
2168 * explanation.
2170 * Params:
2171 * DDSD: Description of the surface to create
2172 * Surf: Address to store the interface pointer at
2173 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2174 * aggregation, so it has to be NULL
2176 * Returns:
2177 * DD_OK on success
2178 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2179 * DDERR_* if an error occurs
2181 *****************************************************************************/
2182 static HRESULT WINAPI
2183 IDirectDrawImpl_CreateSurface(IDirectDraw7 *iface,
2184 DDSURFACEDESC2 *DDSD,
2185 IDirectDrawSurface7 **Surf,
2186 IUnknown *UnkOuter)
2188 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
2189 IDirectDrawSurfaceImpl *object = NULL;
2190 HRESULT hr;
2191 LONG extra_surfaces = 0;
2192 DDSURFACEDESC2 desc2;
2193 WINED3DDISPLAYMODE Mode;
2194 const DWORD sysvidmem = DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
2196 TRACE("(%p)->(%p,%p,%p)\n", This, DDSD, Surf, UnkOuter);
2198 /* Some checks before we start */
2199 if (TRACE_ON(ddraw))
2201 TRACE(" (%p) Requesting surface desc :\n", This);
2202 DDRAW_dump_surface_desc(DDSD);
2204 EnterCriticalSection(&ddraw_cs);
2206 if (UnkOuter != NULL)
2208 FIXME("(%p) : outer != NULL?\n", This);
2209 LeaveCriticalSection(&ddraw_cs);
2210 return CLASS_E_NOAGGREGATION; /* unchecked */
2213 if (Surf == NULL)
2215 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", This);
2216 LeaveCriticalSection(&ddraw_cs);
2217 return E_POINTER; /* unchecked */
2220 if (!(DDSD->dwFlags & DDSD_CAPS))
2222 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2223 DDSD->dwFlags |= DDSD_CAPS;
2226 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2228 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2229 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2232 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2234 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2235 WARN("(%p) Null surface pointer specified, ignore it!\n", This);
2236 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2239 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2240 !(This->cooperative_level & DDSCL_EXCLUSIVE))
2242 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n", This);
2243 *Surf = NULL;
2244 LeaveCriticalSection(&ddraw_cs);
2245 return DDERR_NOEXCLUSIVEMODE;
2248 if(DDSD->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER)) {
2249 WARN("Application tried to create an explicit front or back buffer\n");
2250 LeaveCriticalSection(&ddraw_cs);
2251 return DDERR_INVALIDCAPS;
2254 if((DDSD->ddsCaps.dwCaps & sysvidmem) == sysvidmem)
2256 /* This is a special switch in ddrawex.dll, but not allowed in ddraw.dll */
2257 WARN("Application tries to put the surface in both system and video memory\n");
2258 LeaveCriticalSection(&ddraw_cs);
2259 *Surf = NULL;
2260 return DDERR_INVALIDCAPS;
2263 /* Check cube maps but only if the size includes them */
2264 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2266 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2267 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2269 WARN("Cube map faces requested without cube map flag\n");
2270 LeaveCriticalSection(&ddraw_cs);
2271 return DDERR_INVALIDCAPS;
2273 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2274 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2276 WARN("Cube map without faces requested\n");
2277 LeaveCriticalSection(&ddraw_cs);
2278 return DDERR_INVALIDPARAMS;
2281 /* Quick tests confirm those can be created, but we don't do that yet */
2282 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2283 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2285 FIXME("Partial cube maps not supported yet\n");
2289 /* According to the msdn this flag is ignored by CreateSurface */
2290 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2291 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2293 /* Modify some flags */
2294 memset(&desc2, 0, sizeof(desc2));
2295 desc2.dwSize = sizeof(desc2); /* For the struct copy */
2296 DD_STRUCT_COPY_BYSIZE(&desc2, DDSD);
2297 desc2.dwSize = sizeof(desc2); /* To override a possibly smaller size */
2298 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2300 /* Get the video mode from WineD3D - we will need it */
2301 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
2302 0, /* Swapchain 0 */
2303 &Mode);
2304 if(FAILED(hr))
2306 ERR("Failed to read display mode from wined3d\n");
2307 switch(This->orig_bpp)
2309 case 8:
2310 Mode.Format = WINED3DFMT_P8_UINT;
2311 break;
2313 case 15:
2314 Mode.Format = WINED3DFMT_B5G5R5X1_UNORM;
2315 break;
2317 case 16:
2318 Mode.Format = WINED3DFMT_B5G6R5_UNORM;
2319 break;
2321 case 24:
2322 Mode.Format = WINED3DFMT_B8G8R8_UNORM;
2323 break;
2325 case 32:
2326 Mode.Format = WINED3DFMT_B8G8R8X8_UNORM;
2327 break;
2329 Mode.Width = This->orig_width;
2330 Mode.Height = This->orig_height;
2333 /* No pixelformat given? Use the current screen format */
2334 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2336 desc2.dwFlags |= DDSD_PIXELFORMAT;
2337 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2339 /* Wait: It could be a Z buffer */
2340 if(desc2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
2342 switch(desc2.u2.dwMipMapCount) /* Who had this glorious idea? */
2344 case 15:
2345 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_S1_UINT_D15_UNORM);
2346 break;
2347 case 16:
2348 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D16_UNORM);
2349 break;
2350 case 24:
2351 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_X8D24_UNORM);
2352 break;
2353 case 32:
2354 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D32_UNORM);
2355 break;
2356 default:
2357 ERR("Unknown Z buffer bit depth\n");
2360 else
2362 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
2366 /* No Width or no Height? Use the original screen size
2368 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2369 !(desc2.dwFlags & DDSD_HEIGHT) )
2371 /* Invalid for non-render targets */
2372 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2374 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2375 *Surf = NULL;
2376 LeaveCriticalSection(&ddraw_cs);
2377 return DDERR_INVALIDPARAMS;
2380 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2381 desc2.dwWidth = Mode.Width;
2382 desc2.dwHeight = Mode.Height;
2385 /* Mipmap count fixes */
2386 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2388 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2390 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2392 /* Mipmap count is given, should not be 0 */
2393 if( desc2.u2.dwMipMapCount == 0 )
2395 LeaveCriticalSection(&ddraw_cs);
2396 return DDERR_INVALIDPARAMS;
2399 else
2401 /* Undocumented feature: Create sublevels until
2402 * either the width or the height is 1
2404 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2405 desc2.dwWidth : desc2.dwHeight;
2406 desc2.u2.dwMipMapCount = 0;
2407 while( min )
2409 desc2.u2.dwMipMapCount += 1;
2410 min >>= 1;
2414 else
2416 /* Not-complex mipmap -> Mipmapcount = 1 */
2417 desc2.u2.dwMipMapCount = 1;
2419 extra_surfaces = desc2.u2.dwMipMapCount - 1;
2421 /* There's a mipmap count in the created surface in any case */
2422 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2424 /* If no mipmap is given, the texture has only one level */
2426 /* The first surface is a front buffer, the back buffer is created afterwards */
2427 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2429 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2432 /* The root surface in a cube map is positive x */
2433 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2435 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2436 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2439 /* Create the first surface */
2440 hr = IDirectDrawImpl_CreateNewSurface(This, &desc2, &object, 0);
2441 if( hr != DD_OK)
2443 ERR("IDirectDrawImpl_CreateNewSurface failed with %08x\n", hr);
2444 LeaveCriticalSection(&ddraw_cs);
2445 return hr;
2447 object->is_complex_root = TRUE;
2449 *Surf = (IDirectDrawSurface7 *)object;
2451 /* Create Additional surfaces if necessary
2452 * This applies to Primary surfaces which have a back buffer count
2453 * set, but not to mipmap textures. In case of Mipmap textures,
2454 * wineD3D takes care of the creation of additional surfaces
2456 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2458 extra_surfaces = DDSD->dwBackBufferCount;
2459 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2460 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2461 desc2.dwBackBufferCount = 0;
2464 hr = DD_OK;
2465 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2467 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2468 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
2469 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2470 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
2471 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
2472 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2473 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
2474 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
2475 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2476 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
2477 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
2478 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2479 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
2480 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
2481 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2482 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
2483 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2486 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces, desc2, FALSE);
2487 if(hr != DD_OK)
2489 /* This destroys and possibly created surfaces too */
2490 IDirectDrawSurface_Release((IDirectDrawSurface7 *)object);
2491 LeaveCriticalSection(&ddraw_cs);
2492 return hr;
2495 /* If the implementation is OpenGL and there's no d3ddevice, attach a d3ddevice
2496 * But attach the d3ddevice only if the currently created surface was
2497 * a primary surface (2D app in 3D mode) or a 3DDEVICE surface (3D app)
2498 * The only case I can think of where this doesn't apply is when a
2499 * 2D app was configured by the user to run with OpenGL and it didn't create
2500 * the render target as first surface. In this case the render target creation
2501 * will cause the 3D init.
2503 if( (This->ImplType == SURFACE_OPENGL) && !(This->d3d_initialized) &&
2504 desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE) )
2506 IDirectDrawSurfaceImpl *target = object, *surface;
2507 struct list *entry;
2509 /* Search for the primary to use as render target */
2510 LIST_FOR_EACH(entry, &This->surface_list)
2512 surface = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2513 if((surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER)) ==
2514 (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER))
2516 /* found */
2517 target = surface;
2518 TRACE("Using primary %p as render target\n", target);
2519 break;
2523 TRACE("(%p) Attaching a D3DDevice, rendertarget = %p\n", This, target);
2524 hr = IDirectDrawImpl_AttachD3DDevice(This, target);
2525 if(hr != D3D_OK)
2527 IDirectDrawSurfaceImpl *release_surf;
2528 ERR("IDirectDrawImpl_AttachD3DDevice failed, hr = %x\n", hr);
2529 *Surf = NULL;
2531 /* The before created surface structures are in an incomplete state here.
2532 * WineD3D holds the reference on the IParents, and it released them on the failure
2533 * already. So the regular release method implementation would fail on the attempt
2534 * to destroy either the IParents or the swapchain. So free the surface here.
2535 * The surface structure here is a list, not a tree, because onscreen targets
2536 * cannot be cube textures
2538 while(object)
2540 release_surf = object;
2541 object = object->complex_array[0];
2542 IDirectDrawSurfaceImpl_Destroy(release_surf);
2544 LeaveCriticalSection(&ddraw_cs);
2545 return hr;
2547 } else if(!(This->d3d_initialized) && desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
2548 IDirectDrawImpl_CreateGDISwapChain(This, object);
2551 /* Addref the ddraw interface to keep an reference for each surface */
2552 IDirectDraw7_AddRef(iface);
2553 object->ifaceToRelease = (IUnknown *) iface;
2555 /* Create a WineD3DTexture if a texture was requested */
2556 if(desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
2558 UINT levels;
2559 WINED3DFORMAT Format;
2560 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
2562 This->tex_root = object;
2564 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2566 /* a mipmap is created, create enough levels */
2567 levels = desc2.u2.dwMipMapCount;
2569 else
2571 /* No mipmap is created, create one level */
2572 levels = 1;
2575 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM */
2576 if(DDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
2578 Pool = WINED3DPOOL_SYSTEMMEM;
2580 /* Should I forward the MANAGED cap to the managed pool ? */
2582 /* Get the format. It's set already by CreateNewSurface */
2583 Format = PixelFormat_DD2WineD3D(&object->surface_desc.u4.ddpfPixelFormat);
2585 /* The surfaces are already created, the callback only
2586 * passes the IWineD3DSurface to WineD3D
2588 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2590 hr = IWineD3DDevice_CreateCubeTexture(This->wineD3DDevice, DDSD->dwWidth /* Edgelength */,
2591 levels, 0 /* usage */, Format, Pool, (IWineD3DCubeTexture **)&object->wineD3DTexture,
2592 (IUnknown *)object, &ddraw_null_wined3d_parent_ops);
2594 else
2596 hr = IWineD3DDevice_CreateTexture(This->wineD3DDevice, DDSD->dwWidth, DDSD->dwHeight, levels,
2597 0 /* usage */, Format, Pool, (IWineD3DTexture **)&object->wineD3DTexture,
2598 (IUnknown *)object, &ddraw_null_wined3d_parent_ops);
2600 This->tex_root = NULL;
2603 LeaveCriticalSection(&ddraw_cs);
2604 return hr;
2607 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
2608 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
2610 static BOOL
2611 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
2612 const DDPIXELFORMAT *provided)
2614 /* Some flags must be present in both or neither for a match. */
2615 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
2616 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
2617 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
2619 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2620 return FALSE;
2622 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
2623 return FALSE;
2625 if (requested->dwFlags & DDPF_FOURCC)
2626 if (requested->dwFourCC != provided->dwFourCC)
2627 return FALSE;
2629 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
2630 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2631 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
2632 return FALSE;
2634 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2635 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2636 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
2637 return FALSE;
2639 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
2640 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
2641 return FALSE;
2643 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
2644 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2645 |DDPF_BUMPDUDV))
2646 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
2647 return FALSE;
2649 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
2650 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
2651 return FALSE;
2653 return TRUE;
2656 static BOOL
2657 IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested,
2658 const DDSURFACEDESC2* provided)
2660 struct compare_info
2662 DWORD flag;
2663 ptrdiff_t offset;
2664 size_t size;
2667 #define CMP(FLAG, FIELD) \
2668 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
2669 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
2671 static const struct compare_info compare[] =
2673 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
2674 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
2675 CMP(CAPS, ddsCaps),
2676 CMP(CKDESTBLT, ddckCKDestBlt),
2677 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
2678 CMP(CKSRCBLT, ddckCKSrcBlt),
2679 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
2680 CMP(HEIGHT, dwHeight),
2681 CMP(LINEARSIZE, u1 /* dwLinearSize */),
2682 CMP(LPSURFACE, lpSurface),
2683 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
2684 CMP(PITCH, u1 /* lPitch */),
2685 /* PIXELFORMAT: manual */
2686 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
2687 CMP(TEXTURESTAGE, dwTextureStage),
2688 CMP(WIDTH, dwWidth),
2689 /* ZBUFFERBITDEPTH: "obsolete" */
2692 #undef CMP
2694 unsigned int i;
2696 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2697 return FALSE;
2699 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
2701 if (requested->dwFlags & compare[i].flag
2702 && memcmp((const char *)provided + compare[i].offset,
2703 (const char *)requested + compare[i].offset,
2704 compare[i].size) != 0)
2705 return FALSE;
2708 if (requested->dwFlags & DDSD_PIXELFORMAT)
2710 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
2711 &provided->u4.ddpfPixelFormat))
2712 return FALSE;
2715 return TRUE;
2718 #undef DDENUMSURFACES_SEARCHTYPE
2719 #undef DDENUMSURFACES_MATCHTYPE
2721 /*****************************************************************************
2722 * IDirectDraw7::EnumSurfaces
2724 * Loops through all surfaces attached to this device and calls the
2725 * application callback. This can't be relayed to WineD3DDevice,
2726 * because some WineD3DSurfaces' parents are IParent objects
2728 * Params:
2729 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
2730 * DDSD: Description to filter for
2731 * Context: Application-provided pointer, it's passed unmodified to the
2732 * Callback function
2733 * Callback: Address to call for each surface
2735 * Returns:
2736 * DDERR_INVALIDPARAMS if the callback is NULL
2737 * DD_OK on success
2739 *****************************************************************************/
2740 static HRESULT WINAPI
2741 IDirectDrawImpl_EnumSurfaces(IDirectDraw7 *iface,
2742 DWORD Flags,
2743 DDSURFACEDESC2 *DDSD,
2744 void *Context,
2745 LPDDENUMSURFACESCALLBACK7 Callback)
2747 /* The surface enumeration is handled by WineDDraw,
2748 * because it keeps track of all surfaces attached to
2749 * it. The filtering is done by our callback function,
2750 * because WineDDraw doesn't handle ddraw-like surface
2751 * caps structures
2753 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
2754 IDirectDrawSurfaceImpl *surf;
2755 BOOL all, nomatch;
2756 DDSURFACEDESC2 desc;
2757 struct list *entry, *entry2;
2759 all = Flags & DDENUMSURFACES_ALL;
2760 nomatch = Flags & DDENUMSURFACES_NOMATCH;
2762 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, DDSD, Context, Callback);
2763 EnterCriticalSection(&ddraw_cs);
2765 if(!Callback)
2767 LeaveCriticalSection(&ddraw_cs);
2768 return DDERR_INVALIDPARAMS;
2771 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
2772 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
2774 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2775 if (all || (nomatch != IDirectDrawImpl_DDSD_Match(DDSD, &surf->surface_desc)))
2777 desc = surf->surface_desc;
2778 IDirectDrawSurface7_AddRef((IDirectDrawSurface7 *)surf);
2779 if (Callback((IDirectDrawSurface7 *)surf, &desc, Context) != DDENUMRET_OK)
2781 LeaveCriticalSection(&ddraw_cs);
2782 return DD_OK;
2786 LeaveCriticalSection(&ddraw_cs);
2787 return DD_OK;
2790 static HRESULT WINAPI
2791 findRenderTarget(IDirectDrawSurface7 *surface,
2792 DDSURFACEDESC2 *desc,
2793 void *ctx)
2795 IDirectDrawSurfaceImpl *surf = (IDirectDrawSurfaceImpl *)surface;
2796 IDirectDrawSurfaceImpl **target = ctx;
2798 if(!surf->isRenderTarget) {
2799 *target = surf;
2800 IDirectDrawSurface7_Release(surface);
2801 return DDENUMRET_CANCEL;
2804 /* Recurse into the surface tree */
2805 IDirectDrawSurface7_EnumAttachedSurfaces(surface, ctx, findRenderTarget);
2807 IDirectDrawSurface7_Release(surface);
2808 if(*target) return DDENUMRET_CANCEL;
2809 else return DDENUMRET_OK; /* Continue with the next neighbor surface */
2812 static HRESULT IDirectDrawImpl_CreateGDISwapChain(IDirectDrawImpl *This,
2813 IDirectDrawSurfaceImpl *primary) {
2814 HRESULT hr;
2815 WINED3DPRESENT_PARAMETERS presentation_parameters;
2816 HWND window;
2818 window = This->dest_window;
2820 memset(&presentation_parameters, 0, sizeof(presentation_parameters));
2822 /* Use the surface description for the device parameters, not the
2823 * Device settings. The app might render to an offscreen surface
2825 presentation_parameters.BackBufferWidth = primary->surface_desc.dwWidth;
2826 presentation_parameters.BackBufferHeight = primary->surface_desc.dwHeight;
2827 presentation_parameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2828 presentation_parameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
2829 presentation_parameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
2830 presentation_parameters.MultiSampleQuality = 0;
2831 presentation_parameters.SwapEffect = WINED3DSWAPEFFECT_FLIP;
2832 presentation_parameters.hDeviceWindow = window;
2833 presentation_parameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
2834 presentation_parameters.EnableAutoDepthStencil = FALSE; /* Not on GDI swapchains */
2835 presentation_parameters.AutoDepthStencilFormat = 0;
2836 presentation_parameters.Flags = 0;
2837 presentation_parameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
2838 presentation_parameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
2840 This->d3d_target = primary;
2841 hr = IWineD3DDevice_InitGDI(This->wineD3DDevice, &presentation_parameters);
2842 This->d3d_target = NULL;
2844 if (hr != D3D_OK)
2846 FIXME("(%p) call to IWineD3DDevice_InitGDI failed\n", This);
2847 primary->wineD3DSwapChain = NULL;
2849 return hr;
2852 /*****************************************************************************
2853 * IDirectDrawImpl_AttachD3DDevice
2855 * Initializes the D3D capabilities of WineD3D
2857 * Params:
2858 * primary: The primary surface for D3D
2860 * Returns
2861 * DD_OK on success,
2862 * DDERR_* otherwise
2864 *****************************************************************************/
2865 static HRESULT
2866 IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This,
2867 IDirectDrawSurfaceImpl *primary)
2869 HRESULT hr;
2870 HWND window = This->dest_window;
2872 WINED3DPRESENT_PARAMETERS localParameters;
2874 TRACE("(%p)->(%p)\n", This, primary);
2876 /* If there's no window, create a hidden window. WineD3D needs it */
2877 if(window == 0 || window == GetDesktopWindow())
2879 window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "Hidden D3D Window",
2880 WS_DISABLED, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
2881 NULL, NULL, NULL, NULL);
2882 if (!window)
2884 ERR("Failed to create window, last error %#x.\n", GetLastError());
2885 return E_FAIL;
2888 ShowWindow(window, SW_HIDE); /* Just to be sure */
2889 WARN("(%p) No window for the Direct3DDevice, created a hidden window. HWND=%p\n", This, window);
2891 else
2893 TRACE("(%p) Using existing window %p for Direct3D rendering\n", This, window);
2895 This->d3d_window = window;
2897 /* Store the future Render Target surface */
2898 This->d3d_target = primary;
2900 /* Use the surface description for the device parameters, not the
2901 * Device settings. The app might render to an offscreen surface
2903 localParameters.BackBufferWidth = primary->surface_desc.dwWidth;
2904 localParameters.BackBufferHeight = primary->surface_desc.dwHeight;
2905 localParameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2906 localParameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
2907 localParameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
2908 localParameters.MultiSampleQuality = 0;
2909 localParameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
2910 localParameters.hDeviceWindow = window;
2911 localParameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
2912 localParameters.EnableAutoDepthStencil = TRUE;
2913 localParameters.AutoDepthStencilFormat = WINED3DFMT_D16_UNORM;
2914 localParameters.Flags = 0;
2915 localParameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
2916 localParameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
2918 TRACE("Passing mode %d\n", localParameters.BackBufferFormat);
2920 /* Set this NOW, otherwise creating the depth stencil surface will cause a
2921 * recursive loop until ram or emulated video memory is full
2923 This->d3d_initialized = TRUE;
2925 hr = IWineD3DDevice_Init3D(This->wineD3DDevice, &localParameters);
2926 if(FAILED(hr))
2928 This->d3d_target = NULL;
2929 This->d3d_initialized = FALSE;
2930 return hr;
2933 This->declArraySize = 2;
2934 This->decls = HeapAlloc(GetProcessHeap(),
2935 HEAP_ZERO_MEMORY,
2936 sizeof(*This->decls) * This->declArraySize);
2937 if(!This->decls)
2939 ERR("Error allocating an array for the converted vertex decls\n");
2940 This->declArraySize = 0;
2941 hr = IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroySwapChain);
2942 return E_OUTOFMEMORY;
2945 /* Create an Index Buffer parent */
2946 TRACE("(%p) Successfully initialized 3D\n", This);
2947 return DD_OK;
2950 /*****************************************************************************
2951 * DirectDrawCreateClipper (DDRAW.@)
2953 * Creates a new IDirectDrawClipper object.
2955 * Params:
2956 * Clipper: Address to write the interface pointer to
2957 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
2958 * NULL
2960 * Returns:
2961 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2962 * E_OUTOFMEMORY if allocating the object failed
2964 *****************************************************************************/
2965 HRESULT WINAPI
2966 DirectDrawCreateClipper(DWORD Flags,
2967 LPDIRECTDRAWCLIPPER *Clipper,
2968 IUnknown *UnkOuter)
2970 IDirectDrawClipperImpl* object;
2971 TRACE("(%08x,%p,%p)\n", Flags, Clipper, UnkOuter);
2973 EnterCriticalSection(&ddraw_cs);
2974 if (UnkOuter != NULL)
2976 LeaveCriticalSection(&ddraw_cs);
2977 return CLASS_E_NOAGGREGATION;
2980 if (!LoadWineD3D())
2982 LeaveCriticalSection(&ddraw_cs);
2983 return DDERR_NODIRECTDRAWSUPPORT;
2986 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2987 sizeof(IDirectDrawClipperImpl));
2988 if (object == NULL)
2990 LeaveCriticalSection(&ddraw_cs);
2991 return E_OUTOFMEMORY;
2994 object->lpVtbl = &IDirectDrawClipper_Vtbl;
2995 object->ref = 1;
2996 object->wineD3DClipper = pWineDirect3DCreateClipper((IUnknown *) object);
2997 if(!object->wineD3DClipper)
2999 HeapFree(GetProcessHeap(), 0, object);
3000 LeaveCriticalSection(&ddraw_cs);
3001 return E_OUTOFMEMORY;
3004 *Clipper = (IDirectDrawClipper *) object;
3005 LeaveCriticalSection(&ddraw_cs);
3006 return DD_OK;
3009 /*****************************************************************************
3010 * IDirectDraw7::CreateClipper
3012 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3014 *****************************************************************************/
3015 static HRESULT WINAPI
3016 IDirectDrawImpl_CreateClipper(IDirectDraw7 *iface,
3017 DWORD Flags,
3018 IDirectDrawClipper **Clipper,
3019 IUnknown *UnkOuter)
3021 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
3022 TRACE("(%p)->(%x,%p,%p)\n", This, Flags, Clipper, UnkOuter);
3023 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3026 /*****************************************************************************
3027 * IDirectDraw7::CreatePalette
3029 * Creates a new IDirectDrawPalette object
3031 * Params:
3032 * Flags: The flags for the new clipper
3033 * ColorTable: Color table to assign to the new clipper
3034 * Palette: Address to write the interface pointer to
3035 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3036 * NULL
3038 * Returns:
3039 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3040 * E_OUTOFMEMORY if allocating the object failed
3042 *****************************************************************************/
3043 static HRESULT WINAPI
3044 IDirectDrawImpl_CreatePalette(IDirectDraw7 *iface,
3045 DWORD Flags,
3046 PALETTEENTRY *ColorTable,
3047 IDirectDrawPalette **Palette,
3048 IUnknown *pUnkOuter)
3050 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
3051 IDirectDrawPaletteImpl *object;
3052 HRESULT hr = DDERR_GENERIC;
3053 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, ColorTable, Palette, pUnkOuter);
3055 EnterCriticalSection(&ddraw_cs);
3056 if(pUnkOuter != NULL)
3058 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
3059 LeaveCriticalSection(&ddraw_cs);
3060 return CLASS_E_NOAGGREGATION;
3063 /* The refcount test shows that a cooplevel is required for this */
3064 if(!This->cooperative_level)
3066 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3067 LeaveCriticalSection(&ddraw_cs);
3068 return DDERR_NOCOOPERATIVELEVELSET;
3071 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
3072 if(!object)
3074 ERR("Out of memory when allocating memory for a palette implementation\n");
3075 LeaveCriticalSection(&ddraw_cs);
3076 return E_OUTOFMEMORY;
3079 object->lpVtbl = &IDirectDrawPalette_Vtbl;
3080 object->ref = 1;
3081 object->ddraw_owner = This;
3083 hr = IWineD3DDevice_CreatePalette(This->wineD3DDevice, Flags,
3084 ColorTable, &object->wineD3DPalette, (IUnknown *)object);
3085 if(hr != DD_OK)
3087 HeapFree(GetProcessHeap(), 0, object);
3088 LeaveCriticalSection(&ddraw_cs);
3089 return hr;
3092 IDirectDraw7_AddRef(iface);
3093 object->ifaceToRelease = (IUnknown *) iface;
3094 *Palette = (IDirectDrawPalette *)object;
3095 LeaveCriticalSection(&ddraw_cs);
3096 return DD_OK;
3099 /*****************************************************************************
3100 * IDirectDraw7::DuplicateSurface
3102 * Duplicates a surface. The surface memory points to the same memory as
3103 * the original surface, and it's released when the last surface referencing
3104 * it is released. I guess that's beyond Wine's surface management right now
3105 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3106 * test application to implement this)
3108 * Params:
3109 * Src: Address of the source surface
3110 * Dest: Address to write the new surface pointer to
3112 * Returns:
3113 * See IDirectDraw7::CreateSurface
3115 *****************************************************************************/
3116 static HRESULT WINAPI
3117 IDirectDrawImpl_DuplicateSurface(IDirectDraw7 *iface,
3118 IDirectDrawSurface7 *Src,
3119 IDirectDrawSurface7 **Dest)
3121 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
3122 IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Src;
3124 FIXME("(%p)->(%p,%p)\n", This, Surf, Dest);
3126 /* For now, simply create a new, independent surface */
3127 return IDirectDraw7_CreateSurface(iface,
3128 &Surf->surface_desc,
3129 Dest,
3130 NULL);
3133 /*****************************************************************************
3134 * IDirectDraw7 VTable
3135 *****************************************************************************/
3136 const IDirectDraw7Vtbl IDirectDraw7_Vtbl =
3138 /*** IUnknown ***/
3139 IDirectDrawImpl_QueryInterface,
3140 IDirectDrawImpl_AddRef,
3141 IDirectDrawImpl_Release,
3142 /*** IDirectDraw ***/
3143 IDirectDrawImpl_Compact,
3144 IDirectDrawImpl_CreateClipper,
3145 IDirectDrawImpl_CreatePalette,
3146 IDirectDrawImpl_CreateSurface,
3147 IDirectDrawImpl_DuplicateSurface,
3148 IDirectDrawImpl_EnumDisplayModes,
3149 IDirectDrawImpl_EnumSurfaces,
3150 IDirectDrawImpl_FlipToGDISurface,
3151 IDirectDrawImpl_GetCaps,
3152 IDirectDrawImpl_GetDisplayMode,
3153 IDirectDrawImpl_GetFourCCCodes,
3154 IDirectDrawImpl_GetGDISurface,
3155 IDirectDrawImpl_GetMonitorFrequency,
3156 IDirectDrawImpl_GetScanLine,
3157 IDirectDrawImpl_GetVerticalBlankStatus,
3158 IDirectDrawImpl_Initialize,
3159 IDirectDrawImpl_RestoreDisplayMode,
3160 IDirectDrawImpl_SetCooperativeLevel,
3161 IDirectDrawImpl_SetDisplayMode,
3162 IDirectDrawImpl_WaitForVerticalBlank,
3163 /*** IDirectDraw2 ***/
3164 IDirectDrawImpl_GetAvailableVidMem,
3165 /*** IDirectDraw3 ***/
3166 IDirectDrawImpl_GetSurfaceFromDC,
3167 /*** IDirectDraw4 ***/
3168 IDirectDrawImpl_RestoreAllSurfaces,
3169 IDirectDrawImpl_TestCooperativeLevel,
3170 IDirectDrawImpl_GetDeviceIdentifier,
3171 /*** IDirectDraw7 ***/
3172 IDirectDrawImpl_StartModeTest,
3173 IDirectDrawImpl_EvaluateMode
3176 /*****************************************************************************
3177 * IDirectDrawImpl_FindDecl
3179 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
3180 * if none was found.
3182 * This function is in ddraw.c and the DDraw object space because D3D7
3183 * vertex buffers are created using the IDirect3D interface to the ddraw
3184 * object, so they can be valid across D3D devices(theoretically. The ddraw
3185 * object also owns the wined3d device
3187 * Parameters:
3188 * This: Device
3189 * fvf: Fvf to find the decl for
3191 * Returns:
3192 * NULL in case of an error, the IWineD3DVertexDeclaration interface for the
3193 * fvf otherwise.
3195 *****************************************************************************/
3196 IWineD3DVertexDeclaration *
3197 IDirectDrawImpl_FindDecl(IDirectDrawImpl *This,
3198 DWORD fvf)
3200 HRESULT hr;
3201 IWineD3DVertexDeclaration* pDecl = NULL;
3202 int p, low, high; /* deliberately signed */
3203 struct FvfToDecl *convertedDecls = This->decls;
3205 TRACE("Searching for declaration for fvf %08x... ", fvf);
3207 low = 0;
3208 high = This->numConvertedDecls - 1;
3209 while(low <= high) {
3210 p = (low + high) >> 1;
3211 TRACE("%d ", p);
3212 if(convertedDecls[p].fvf == fvf) {
3213 TRACE("found %p\n", convertedDecls[p].decl);
3214 return convertedDecls[p].decl;
3215 } else if(convertedDecls[p].fvf < fvf) {
3216 low = p + 1;
3217 } else {
3218 high = p - 1;
3221 TRACE("not found. Creating and inserting at position %d.\n", low);
3223 hr = IWineD3DDevice_CreateVertexDeclarationFromFVF(This->wineD3DDevice, &pDecl,
3224 (IUnknown *)This, &ddraw_null_wined3d_parent_ops, fvf);
3225 if (hr != S_OK) return NULL;
3227 if(This->declArraySize == This->numConvertedDecls) {
3228 int grow = max(This->declArraySize / 2, 8);
3229 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
3230 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
3231 if(!convertedDecls) {
3232 /* This will destroy it */
3233 IWineD3DVertexDeclaration_Release(pDecl);
3234 return NULL;
3236 This->decls = convertedDecls;
3237 This->declArraySize += grow;
3240 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
3241 convertedDecls[low].decl = pDecl;
3242 convertedDecls[low].fvf = fvf;
3243 This->numConvertedDecls++;
3245 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
3246 return pDecl;
3249 /* IWineD3DDeviceParent IUnknown methods */
3251 static inline struct IDirectDrawImpl *ddraw_from_device_parent(IWineD3DDeviceParent *iface)
3253 return (struct IDirectDrawImpl *)((char*)iface - FIELD_OFFSET(struct IDirectDrawImpl, device_parent_vtbl));
3256 static HRESULT STDMETHODCALLTYPE device_parent_QueryInterface(IWineD3DDeviceParent *iface, REFIID riid, void **object)
3258 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3259 return IDirectDrawImpl_QueryInterface((IDirectDraw7 *)This, riid, object);
3262 static ULONG STDMETHODCALLTYPE device_parent_AddRef(IWineD3DDeviceParent *iface)
3264 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3265 return IDirectDrawImpl_AddRef((IDirectDraw7 *)This);
3268 static ULONG STDMETHODCALLTYPE device_parent_Release(IWineD3DDeviceParent *iface)
3270 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3271 return IDirectDrawImpl_Release((IDirectDraw7 *)This);
3274 /* IWineD3DDeviceParent methods */
3276 static void STDMETHODCALLTYPE device_parent_WineD3DDeviceCreated(IWineD3DDeviceParent *iface, IWineD3DDevice *device)
3278 TRACE("iface %p, device %p\n", iface, device);
3281 static HRESULT STDMETHODCALLTYPE device_parent_CreateSurface(IWineD3DDeviceParent *iface,
3282 IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, DWORD usage,
3283 WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, IWineD3DSurface **surface)
3285 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3286 IDirectDrawSurfaceImpl *surf = NULL;
3287 UINT i = 0;
3288 DDSCAPS2 searchcaps = This->tex_root->surface_desc.ddsCaps;
3290 TRACE("iface %p, superior %p, width %u, height %u, format %#x, usage %#x,\n"
3291 "\tpool %#x, level %u, face %u, surface %p\n",
3292 iface, superior, width, height, format, usage, pool, level, face, surface);
3294 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
3295 switch(face)
3297 case WINED3DCUBEMAP_FACE_POSITIVE_X:
3298 TRACE("Asked for positive x\n");
3299 if (searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP)
3301 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
3303 surf = This->tex_root; break;
3304 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
3305 TRACE("Asked for negative x\n");
3306 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
3307 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
3308 TRACE("Asked for positive y\n");
3309 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
3310 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
3311 TRACE("Asked for negative y\n");
3312 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
3313 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
3314 TRACE("Asked for positive z\n");
3315 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
3316 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
3317 TRACE("Asked for negative z\n");
3318 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
3319 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
3322 if (!surf)
3324 IDirectDrawSurface7 *attached;
3325 IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)This->tex_root, &searchcaps, &attached);
3326 surf = (IDirectDrawSurfaceImpl *)attached;
3327 IDirectDrawSurface7_Release(attached);
3329 if (!surf) ERR("root search surface not found\n");
3331 /* Find the wanted mipmap. There are enough mipmaps in the chain */
3332 while (i < level)
3334 IDirectDrawSurface7 *attached;
3335 IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)surf, &searchcaps, &attached);
3336 if(!attached) ERR("Surface not found\n");
3337 surf = (IDirectDrawSurfaceImpl *)attached;
3338 IDirectDrawSurface7_Release(attached);
3339 ++i;
3342 /* Return the surface */
3343 *surface = surf->WineD3DSurface;
3344 IWineD3DSurface_AddRef(*surface);
3346 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, surf);
3348 return D3D_OK;
3351 static HRESULT STDMETHODCALLTYPE device_parent_CreateRenderTarget(IWineD3DDeviceParent *iface,
3352 IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
3353 DWORD multisample_quality, BOOL lockable, IWineD3DSurface **surface)
3355 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3356 IDirectDrawSurfaceImpl *d3d_surface = This->d3d_target;
3357 IDirectDrawSurfaceImpl *target = NULL;
3359 TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3360 "\tmultisample_quality %u, lockable %u, surface %p\n",
3361 iface, superior, width, height, format, multisample_type, multisample_quality, lockable, surface);
3363 if (d3d_surface->isRenderTarget)
3365 IDirectDrawSurface7_EnumAttachedSurfaces((IDirectDrawSurface7 *)d3d_surface, &target, findRenderTarget);
3367 else
3369 target = d3d_surface;
3372 if (!target)
3374 target = This->d3d_target;
3375 ERR(" (%p) : No DirectDrawSurface found to create the back buffer. Using the front buffer as back buffer. Uncertain consequences\n", This);
3378 /* TODO: Return failure if the dimensions do not match, but this shouldn't happen */
3380 *surface = target->WineD3DSurface;
3381 IWineD3DSurface_AddRef(*surface);
3382 target->isRenderTarget = TRUE;
3384 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, d3d_surface);
3386 return D3D_OK;
3389 static HRESULT STDMETHODCALLTYPE device_parent_CreateDepthStencilSurface(IWineD3DDeviceParent *iface,
3390 IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
3391 DWORD multisample_quality, BOOL discard, IWineD3DSurface **surface)
3393 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3394 IDirectDrawSurfaceImpl *ddraw_surface;
3395 DDSURFACEDESC2 ddsd;
3396 HRESULT hr;
3398 TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3399 "\tmultisample_quality %u, discard %u, surface %p\n",
3400 iface, superior, width, height, format, multisample_type, multisample_quality, discard, surface);
3402 *surface = NULL;
3404 /* Create a DirectDraw surface */
3405 memset(&ddsd, 0, sizeof(ddsd));
3406 ddsd.dwSize = sizeof(ddsd);
3407 ddsd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
3408 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
3409 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
3410 ddsd.dwHeight = height;
3411 ddsd.dwWidth = width;
3412 if (format)
3414 PixelFormat_WineD3DtoDD(&ddsd.u4.ddpfPixelFormat, format);
3416 else
3418 ddsd.dwFlags ^= DDSD_PIXELFORMAT;
3421 This->depthstencil = TRUE;
3422 hr = IDirectDraw7_CreateSurface((IDirectDraw7 *)This, &ddsd, (IDirectDrawSurface7 **)&ddraw_surface, NULL);
3423 This->depthstencil = FALSE;
3424 if(FAILED(hr))
3426 ERR(" (%p) Creating a DepthStencil Surface failed, result = %x\n", This, hr);
3427 return hr;
3430 *surface = ddraw_surface->WineD3DSurface;
3431 IWineD3DSurface_AddRef(*surface);
3432 IDirectDrawSurface7_Release((IDirectDrawSurface7 *)ddraw_surface);
3434 return D3D_OK;
3437 static HRESULT STDMETHODCALLTYPE device_parent_CreateVolume(IWineD3DDeviceParent *iface,
3438 IUnknown *superior, UINT width, UINT height, UINT depth, WINED3DFORMAT format,
3439 WINED3DPOOL pool, DWORD usage, IWineD3DVolume **volume)
3441 TRACE("iface %p, superior %p, width %u, height %u, depth %u, format %#x, pool %#x, usage %#x, volume %p\n",
3442 iface, superior, width, height, depth, format, pool, usage, volume);
3444 ERR("Not implemented!\n");
3446 return E_NOTIMPL;
3449 static HRESULT STDMETHODCALLTYPE device_parent_CreateSwapChain(IWineD3DDeviceParent *iface,
3450 WINED3DPRESENT_PARAMETERS *present_parameters, IWineD3DSwapChain **swapchain)
3452 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
3453 IDirectDrawSurfaceImpl *iterator;
3454 IParentImpl *object;
3455 HRESULT hr;
3457 TRACE("iface %p, present_parameters %p, swapchain %p\n", iface, present_parameters, swapchain);
3459 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IParentImpl));
3460 if (!object)
3462 FIXME("Allocation of memory failed\n");
3463 *swapchain = NULL;
3464 return DDERR_OUTOFVIDEOMEMORY;
3467 object->lpVtbl = &IParent_Vtbl;
3468 object->ref = 1;
3470 hr = IWineD3DDevice_CreateSwapChain(This->wineD3DDevice, present_parameters,
3471 swapchain, (IUnknown *)object, This->ImplType);
3472 if (FAILED(hr))
3474 FIXME("(%p) CreateSwapChain failed, returning %#x\n", iface, hr);
3475 HeapFree(GetProcessHeap(), 0 , object);
3476 *swapchain = NULL;
3477 return hr;
3480 object->child = (IUnknown *)*swapchain;
3481 This->d3d_target->wineD3DSwapChain = *swapchain;
3482 iterator = This->d3d_target->complex_array[0];
3483 while (iterator)
3485 iterator->wineD3DSwapChain = *swapchain;
3486 iterator = iterator->complex_array[0];
3489 return hr;
3492 const IWineD3DDeviceParentVtbl ddraw_wined3d_device_parent_vtbl =
3494 /* IUnknown methods */
3495 device_parent_QueryInterface,
3496 device_parent_AddRef,
3497 device_parent_Release,
3498 /* IWineD3DDeviceParent methods */
3499 device_parent_WineD3DDeviceCreated,
3500 device_parent_CreateSurface,
3501 device_parent_CreateRenderTarget,
3502 device_parent_CreateDepthStencilSurface,
3503 device_parent_CreateVolume,
3504 device_parent_CreateSwapChain,